Blender V5.3
multi_function_procedure_optimization.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
10
12
13void move_destructs_up(Procedure &procedure, Instruction &block_end_instr)
14{
15 /* A mapping from a variable to its destruct instruction. */
16 Map<Variable *, DestructInstruction *> destruct_instructions;
17 Instruction *current_instr = &block_end_instr;
18 while (true) {
19 InstructionType instr_type = current_instr->type();
20 switch (instr_type) {
22 DestructInstruction &destruct_instr = static_cast<DestructInstruction &>(*current_instr);
23 Variable *variable = destruct_instr.variable();
24 if (variable == nullptr) {
25 continue;
26 }
27 /* Remember this destruct instruction so that it can be moved up later on when the last use
28 * of the variable is found. */
29 destruct_instructions.add(variable, &destruct_instr);
30 break;
31 }
33 CallInstruction &call_instr = static_cast<CallInstruction &>(*current_instr);
34 /* For each variable, place the corresponding remembered destruct instruction right after
35 * this call instruction. */
36 for (Variable *variable : call_instr.params()) {
37 if (variable == nullptr) {
38 continue;
39 }
40 DestructInstruction *destruct_instr = destruct_instructions.pop_default(variable,
41 nullptr);
42 if (destruct_instr == nullptr) {
43 continue;
44 }
45
46 /* Unlink destruct instruction from previous position. */
47 Instruction *after_destruct_instr = destruct_instr->next();
48 while (!destruct_instr->prev().is_empty()) {
49 /* Do a copy of the cursor here, because `destruct_instr->prev()` changes when
50 * #set_next is called below. */
51 const InstructionCursor cursor = destruct_instr->prev()[0];
52 cursor.set_next(procedure, after_destruct_instr);
53 }
54
55 /* Insert destruct instruction in new position. */
56 Instruction *next_instr = call_instr.next();
57 call_instr.set_next(destruct_instr);
58 destruct_instr->set_next(next_instr);
59 }
60 break;
61 }
62 default: {
63 break;
64 }
65 }
66
67 const Span<InstructionCursor> prev_cursors = current_instr->prev();
68 if (prev_cursors.size() != 1) {
69 /* Stop when there is some branching before this instruction. */
70 break;
71 }
72 const InstructionCursor &prev_cursor = prev_cursors[0];
73 current_instr = prev_cursor.instruction();
74 if (current_instr == nullptr) {
75 /* Stop when there is no previous instruction. E.g. when this is the first instruction. */
76 break;
77 }
78 }
79}
80
81} // namespace blender::fn::multi_function::procedure_optimization
bool add(const Key &key, const Value &value)
Definition BLI_map.hh:295
Value pop_default(const Key &key, const Value &default_value)
Definition BLI_map.hh:439
constexpr int64_t size() const
Definition BLI_span.hh:256
void set_next(Procedure &procedure, Instruction *new_instruction) const
void move_destructs_up(Procedure &procedure, Instruction &block_end_instr)