Welcome to mirror list, hosted at ThFree Co, Russian Federation.

multi_function_procedure_optimization.cc « intern « functions « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 728a1aead393b2fa91e21fa8821dd25aa0cf5a42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include "FN_multi_function_procedure_optimization.hh"

namespace blender::fn::procedure_optimization {

static bool uses_variable(const MFInstruction &instr, const MFVariable &variable)
{
  switch (instr.type()) {
    case MFInstructionType::Branch:
      return static_cast<const MFBranchInstruction &>(instr).condition() == &variable;
    case MFInstructionType::Call:
      return static_cast<const MFCallInstruction &>(instr).params().contains(&variable);
    case MFInstructionType::Destruct:
      return static_cast<const MFDestructInstruction &>(instr).variable() == &variable;
    default:
      return false;
  }
}

void move_destructs_up(MFProcedure &procedure)
{
  for (MFDestructInstruction *destruct_instr : procedure.destruct_instructions()) {
    MFVariable *variable = destruct_instr->variable();
    if (variable == nullptr) {
      continue;
    }
    MFInstruction *last_use_in_block_instr = nullptr;
    MFInstruction *current_instr = destruct_instr;
    while (current_instr->prev().size() == 1) {
      current_instr = current_instr->prev()[0].instruction();
      if (current_instr == nullptr) {
        break;
      }
      if (uses_variable(*current_instr, *variable)) {
        last_use_in_block_instr = current_instr;
        break;
      }
    }
    if (last_use_in_block_instr == nullptr) {
      continue;
    }
    if (last_use_in_block_instr->type() == MFInstructionType::Call) {
      MFCallInstruction &call_instr = static_cast<MFCallInstruction &>(*last_use_in_block_instr);
      destruct_instr->prev()[0].set_next(procedure, destruct_instr->next());
      destruct_instr->set_next(call_instr.next());
      call_instr.set_next(destruct_instr);
    }
  }
}

}  // namespace blender::fn::procedure_optimization