1 /**
2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "compiler_logger.h"
17 #include "optimizer/ir/basicblock.h"
18 #include "optimizer/ir/inst.h"
19 #include "optimizer/analysis/alias_analysis.h"
20 #include "optimizer/analysis/bounds_analysis.h"
21 #include "optimizer/analysis/dominators_tree.h"
22 #include "optimizer/analysis/rpo.h"
23 #include "optimizer/analysis/loop_analyzer.h"
24 #include "optimizer/optimizations/code_sink.h"
25
26 namespace panda::compiler {
27 /**
28 * Code sinking optimization attempts to sink operations that is needed only in
29 * a particular execution path but are performed on paths that do not need
30 * them.
31 *
32 * [BB 0] ---------\
33 * | v3 = v1 + v2 \
34 * | \
35 * [BB 1] [BB 2]
36 * return v1 return v3
37 *
38 * Transforms into
39 *
40 * [BB 0] ------\
41 * | \
42 * [BB 1] [BB 2]
43 * return v1 v3 = v1 + v2
44 * return v3
45 *
46 * Generally we can move only instructions with no side-effects.
47 * We can't move:
48 * - Allocations
49 * - Control flow instructions
50 * - Instructions that can throw an exception
51 * - Function calls (they may have side effects and/or throw exceptions as well)
52 * - Monitors
53 * - Store instructions
54 *
55 * We do not move:
56 * - Loads that may be aliased by following stores in a basic block
57 * - Loads if there is any presence of monitors in a function
58 * - Instructions into inner loops
59 */
RunImpl()60 bool CodeSink::RunImpl()
61 {
62 is_applied_ = false;
63 GetGraph()->RunPass<LoopAnalyzer>();
64 // Iteratively sink instructions. On each iteration an instruction can be
65 // sunk to it's basic block dominatee. Iterate sinking until no changes
66 // happens.
67 bool changed = true;
68 for (int i = 0; changed; ++i) {
69 changed = false;
70 COMPILER_LOG(DEBUG, CODE_SINK) << "Sinking iteration " << i;
71 for (auto block : GetGraph()->GetBlocksRPO()) {
72 changed |= ProcessBlock(block);
73 }
74 is_applied_ |= changed;
75 }
76
77 COMPILER_LOG(DEBUG, CODE_SINK) << "Code Sink complete";
78 return is_applied_;
79 }
80
InvalidateAnalyses()81 void CodeSink::InvalidateAnalyses()
82 {
83 if (is_applied_) {
84 // Bounds analysis works with instructions in a particular basic block
85 // reordering breaks it
86 GetGraph()->InvalidateAnalysis<BoundsAnalysis>();
87 }
88 }
89
90 /**
91 * Iterate instructions in reverse order to decrease the number of iterations.
92 * If instruction can be sunk, it is erased from it's basic block and inserted
93 * into appropriate successor.
94 */
ProcessBlock(BasicBlock * block)95 bool CodeSink::ProcessBlock(BasicBlock *block)
96 {
97 if (block->IsStartBlock() || block->IsEndBlock()) {
98 return false;
99 }
100 bool made_change = false;
101 bool mem_barrier = false;
102 InstVector stores(GetGraph()->GetLocalAllocator()->Adapter());
103 for (auto inst : block->InstsSafeReverse()) {
104 if (inst->IsCatchPhi() || !inst->HasUsers()) {
105 continue;
106 }
107 if (inst->GetOpcode() == Opcode::Monitor || (inst->IsStore() && IsVolatileMemInst(inst))) {
108 // Ensures that we do not move in or out monitored section
109 // Also ensures we do not sink over volatile store
110 mem_barrier = true;
111 continue;
112 }
113 BasicBlock *candidate = SinkInstruction(inst, &stores, mem_barrier);
114 if (candidate != nullptr) {
115 COMPILER_LOG(DEBUG, CODE_SINK) << "Sunk v" << inst->GetId() << " to BB " << candidate->GetId();
116 GetGraph()->GetEventWriter().EventCodeSink(inst->GetId(), inst->GetPc(), block->GetId(),
117 candidate->GetId());
118 block->EraseInst(inst, true);
119 // Insertion in the beginning of the block guaranties we do not
120 // enter or exit monitor in candidate block
121 candidate->PrependInst(inst);
122 made_change = true;
123 }
124 }
125 return made_change;
126 }
127
InstHasRefInput(Inst * inst)128 static bool InstHasRefInput(Inst *inst)
129 {
130 for (size_t i = 0; i < inst->GetInputsCount(); ++i) {
131 if (!DataType::IsTypeNumeric(inst->GetInputType(i))) {
132 return true;
133 }
134 }
135 return false;
136 }
137
138 /**
139 * Check that an instruction can be sunk. Then check that dominated blocks are acceptable for sinking.
140 * Finally, return the candidate.
141 */
SinkInstruction(Inst * inst,InstVector * stores,bool barriered)142 BasicBlock *CodeSink::SinkInstruction(Inst *inst, InstVector *stores, bool barriered)
143 {
144 ASSERT(inst->GetOpcode() != Opcode::Phi && inst->GetOpcode() != Opcode::CatchPhi);
145 // Save stores to be sure we do not sink a load instruction that may be
146 // overwritten by later store instruction
147 if (inst->IsStore()) {
148 stores->push_back(inst);
149 return nullptr;
150 }
151 // Check that instruction can be sunk
152 // Volatile memory operations are barriers
153 // We can't move instruction with REFERENCE input throw SaveState(GC can moved or delete the object)
154 // For Bytecode Optimizer, it is not allowed to mix LoadStatic and class initialization (for another LoadStatic),
155 // so LoadStatic cannot be sunk
156 if (inst->IsAllocation() || inst->IsControlFlow() || inst->CanThrow() || inst->IsBarrier() || inst->IsSaveState() ||
157 (!GetGraph()->IsBytecodeOptimizer() && InstHasRefInput(inst)) ||
158 (GetGraph()->IsBytecodeOptimizer() && (inst->GetOpcode() == Opcode::LoadStatic))) {
159 return nullptr;
160 }
161 if (inst->IsLoad()) {
162 // Do not sink over monitors or volatile stores
163 if (barriered) {
164 return nullptr;
165 }
166 for (auto store : *stores) {
167 if (GetGraph()->CheckInstAlias(inst, store) != AliasType::NO_ALIAS) {
168 return nullptr;
169 }
170 }
171 }
172
173 BasicBlock *block = inst->GetBasicBlock();
174 auto dominated = block->GetDominatedBlocks();
175 for (auto cand : dominated) {
176 if (IsAcceptableTarget(inst, cand)) {
177 return cand;
178 }
179 }
180
181 return nullptr;
182 }
183
184 /**
185 * Check that candidate dominates all users of inst.
186 */
IsAcceptableTarget(Inst * inst,BasicBlock * candidate)187 bool CodeSink::IsAcceptableTarget(Inst *inst, BasicBlock *candidate)
188 {
189 ASSERT(inst != nullptr);
190 ASSERT(candidate != nullptr);
191 ASSERT(inst->GetBasicBlock() != candidate);
192 ASSERT(inst->GetBasicBlock()->IsDominate(candidate));
193
194 if (candidate->IsEndBlock() || candidate->IsTryBegin() || candidate->IsTryEnd()) {
195 return false;
196 }
197
198 Loop *cand_loop = candidate->GetLoop();
199 // Do not sink into irreducible loops
200 if (cand_loop->IsIrreducible()) {
201 return false;
202 }
203
204 BasicBlock *block = inst->GetBasicBlock();
205 Loop *loop = block->GetLoop();
206 if (candidate->GetPredsBlocks().size() > 1) {
207 // Do not sink loads across a critical edge there may be stores in
208 // other code paths.
209 if (inst->IsLoad()) {
210 return false;
211 }
212 if (loop != cand_loop) {
213 return false;
214 }
215 }
216 ASSERT_PRINT(loop == cand_loop || loop->IsInside(cand_loop), "Can sink only into outer loop");
217
218 // Check that all uses are dominated by the candidate
219 for (auto &user : inst->GetUsers()) {
220 Inst *uinst = user.GetInst();
221 auto ublock = uinst->GetBasicBlock();
222 // We can't insert instruction before Phi, therefore do not sink into blocks where one of users is a Phi
223 if (!candidate->IsDominate(ublock) || (uinst->IsPhi() && ublock == candidate)) {
224 return false;
225 }
226 }
227 return true;
228 }
229 } // namespace panda::compiler
230