blob: 9d73e2960272dbcd9a9117b8b8ff78ca9df819a0 [file] [log] [blame]
Aart Bik281c6812016-08-26 11:31:48 -07001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "loop_optimization.h"
18
Aart Bik96202302016-10-04 17:33:56 -070019#include "linear_order.h"
Aart Bik281c6812016-08-26 11:31:48 -070020
21namespace art {
22
Aart Bik9abf8942016-10-14 09:49:42 -070023// Remove the instruction from the graph. A bit more elaborate than the usual
24// instruction removal, since there may be a cycle in the use structure.
Aart Bik281c6812016-08-26 11:31:48 -070025static void RemoveFromCycle(HInstruction* instruction) {
Aart Bik281c6812016-08-26 11:31:48 -070026 instruction->RemoveAsUserOfAllInputs();
27 instruction->RemoveEnvironmentUsers();
28 instruction->GetBlock()->RemoveInstructionOrPhi(instruction, /*ensure_safety=*/ false);
29}
30
Aart Bik807868e2016-11-03 17:51:43 -070031// Detect a goto block and sets succ to the single successor.
Aart Bike3dedc52016-11-02 17:50:27 -070032static bool IsGotoBlock(HBasicBlock* block, /*out*/ HBasicBlock** succ) {
33 if (block->GetPredecessors().size() == 1 &&
34 block->GetSuccessors().size() == 1 &&
35 block->IsSingleGoto()) {
36 *succ = block->GetSingleSuccessor();
37 return true;
38 }
39 return false;
40}
41
Aart Bik807868e2016-11-03 17:51:43 -070042// Detect an early exit loop.
43static bool IsEarlyExit(HLoopInformation* loop_info) {
44 HBlocksInLoopReversePostOrderIterator it_loop(*loop_info);
45 for (it_loop.Advance(); !it_loop.Done(); it_loop.Advance()) {
46 for (HBasicBlock* successor : it_loop.Current()->GetSuccessors()) {
47 if (!loop_info->Contains(*successor)) {
48 return true;
49 }
50 }
51 }
52 return false;
53}
54
Aart Bik281c6812016-08-26 11:31:48 -070055//
56// Class methods.
57//
58
59HLoopOptimization::HLoopOptimization(HGraph* graph,
60 HInductionVarAnalysis* induction_analysis)
61 : HOptimization(graph, kLoopOptimizationPassName),
62 induction_range_(induction_analysis),
Aart Bik96202302016-10-04 17:33:56 -070063 loop_allocator_(nullptr),
Aart Bik281c6812016-08-26 11:31:48 -070064 top_loop_(nullptr),
Aart Bik8c4a8542016-10-06 11:36:57 -070065 last_loop_(nullptr),
Aart Bik482095d2016-10-10 15:39:10 -070066 iset_(nullptr),
Aart Bikdf7822e2016-12-06 10:05:30 -080067 induction_simplication_count_(0),
68 simplified_(false) {
Aart Bik281c6812016-08-26 11:31:48 -070069}
70
71void HLoopOptimization::Run() {
72 // Well-behaved loops only.
73 // TODO: make this less of a sledgehammer.
Aart Bik96202302016-10-04 17:33:56 -070074 if (graph_->HasTryCatch() || graph_->HasIrreducibleLoops()) {
Aart Bik281c6812016-08-26 11:31:48 -070075 return;
76 }
77
Aart Bik96202302016-10-04 17:33:56 -070078 // Phase-local allocator that draws from the global pool. Since the allocator
79 // itself resides on the stack, it is destructed on exiting Run(), which
80 // implies its underlying memory is released immediately.
Nicolas Geoffrayebe16742016-10-05 09:55:42 +010081 ArenaAllocator allocator(graph_->GetArena()->GetArenaPool());
Aart Bik96202302016-10-04 17:33:56 -070082 loop_allocator_ = &allocator;
Nicolas Geoffrayebe16742016-10-05 09:55:42 +010083
Aart Bik96202302016-10-04 17:33:56 -070084 // Perform loop optimizations.
85 LocalRun();
86
87 // Detach.
88 loop_allocator_ = nullptr;
89 last_loop_ = top_loop_ = nullptr;
90}
91
92void HLoopOptimization::LocalRun() {
93 // Build the linear order using the phase-local allocator. This step enables building
94 // a loop hierarchy that properly reflects the outer-inner and previous-next relation.
95 ArenaVector<HBasicBlock*> linear_order(loop_allocator_->Adapter(kArenaAllocLinearOrder));
96 LinearizeGraph(graph_, loop_allocator_, &linear_order);
97
Aart Bik281c6812016-08-26 11:31:48 -070098 // Build the loop hierarchy.
Aart Bik96202302016-10-04 17:33:56 -070099 for (HBasicBlock* block : linear_order) {
Aart Bik281c6812016-08-26 11:31:48 -0700100 if (block->IsLoopHeader()) {
101 AddLoop(block->GetLoopInformation());
102 }
103 }
Aart Bik96202302016-10-04 17:33:56 -0700104
Aart Bik8c4a8542016-10-06 11:36:57 -0700105 // Traverse the loop hierarchy inner-to-outer and optimize. Traversal can use
106 // a temporary set that stores instructions using the phase-local allocator.
107 if (top_loop_ != nullptr) {
108 ArenaSet<HInstruction*> iset(loop_allocator_->Adapter(kArenaAllocLoopOptimization));
109 iset_ = &iset;
110 TraverseLoopsInnerToOuter(top_loop_);
111 iset_ = nullptr; // detach
112 }
Aart Bik281c6812016-08-26 11:31:48 -0700113}
114
115void HLoopOptimization::AddLoop(HLoopInformation* loop_info) {
116 DCHECK(loop_info != nullptr);
Nicolas Geoffrayebe16742016-10-05 09:55:42 +0100117 LoopNode* node = new (loop_allocator_) LoopNode(loop_info); // phase-local allocator
Aart Bik281c6812016-08-26 11:31:48 -0700118 if (last_loop_ == nullptr) {
119 // First loop.
120 DCHECK(top_loop_ == nullptr);
121 last_loop_ = top_loop_ = node;
122 } else if (loop_info->IsIn(*last_loop_->loop_info)) {
123 // Inner loop.
124 node->outer = last_loop_;
125 DCHECK(last_loop_->inner == nullptr);
126 last_loop_ = last_loop_->inner = node;
127 } else {
128 // Subsequent loop.
129 while (last_loop_->outer != nullptr && !loop_info->IsIn(*last_loop_->outer->loop_info)) {
130 last_loop_ = last_loop_->outer;
131 }
132 node->outer = last_loop_->outer;
133 node->previous = last_loop_;
134 DCHECK(last_loop_->next == nullptr);
135 last_loop_ = last_loop_->next = node;
136 }
137}
138
139void HLoopOptimization::RemoveLoop(LoopNode* node) {
140 DCHECK(node != nullptr);
Aart Bik8c4a8542016-10-06 11:36:57 -0700141 DCHECK(node->inner == nullptr);
142 if (node->previous != nullptr) {
143 // Within sequence.
144 node->previous->next = node->next;
145 if (node->next != nullptr) {
146 node->next->previous = node->previous;
147 }
148 } else {
149 // First of sequence.
150 if (node->outer != nullptr) {
151 node->outer->inner = node->next;
152 } else {
153 top_loop_ = node->next;
154 }
155 if (node->next != nullptr) {
156 node->next->outer = node->outer;
157 node->next->previous = nullptr;
158 }
159 }
Aart Bik281c6812016-08-26 11:31:48 -0700160}
161
162void HLoopOptimization::TraverseLoopsInnerToOuter(LoopNode* node) {
163 for ( ; node != nullptr; node = node->next) {
Aart Bik482095d2016-10-10 15:39:10 -0700164 int current_induction_simplification_count = induction_simplication_count_;
Aart Bik281c6812016-08-26 11:31:48 -0700165 if (node->inner != nullptr) {
166 TraverseLoopsInnerToOuter(node->inner);
167 }
Aart Bik482095d2016-10-10 15:39:10 -0700168 // Visit loop after its inner loops have been visited. If the induction of any inner
169 // loop has been simplified, recompute the induction information of this loop first.
170 if (current_induction_simplification_count != induction_simplication_count_) {
171 induction_range_.ReVisit(node->loop_info);
172 }
Aart Bikdf7822e2016-12-06 10:05:30 -0800173 // Repeat simplifications until no more changes occur. Note that since
174 // each simplification consists of eliminating code (without introducing
175 // new code), this process is always finite.
176 do {
177 simplified_ = false;
178 SimplifyBlocks(node);
179 SimplifyInduction(node);
180 } while (simplified_);
181 // Remove inner loops when empty.
Aart Bik9abf8942016-10-14 09:49:42 -0700182 if (node->inner == nullptr) {
183 RemoveIfEmptyInnerLoop(node);
184 }
Aart Bik281c6812016-08-26 11:31:48 -0700185 }
186}
187
188void HLoopOptimization::SimplifyInduction(LoopNode* node) {
189 HBasicBlock* header = node->loop_info->GetHeader();
190 HBasicBlock* preheader = node->loop_info->GetPreHeader();
Aart Bik8c4a8542016-10-06 11:36:57 -0700191 // Scan the phis in the header to find opportunities to simplify an induction
192 // cycle that is only used outside the loop. Replace these uses, if any, with
193 // the last value and remove the induction cycle.
194 // Examples: for (int i = 0; x != null; i++) { .... no i .... }
195 // for (int i = 0; i < 10; i++, k++) { .... no k .... } return k;
Aart Bik281c6812016-08-26 11:31:48 -0700196 for (HInstructionIterator it(header->GetPhis()); !it.Done(); it.Advance()) {
197 HPhi* phi = it.Current()->AsPhi();
Aart Bik8c4a8542016-10-06 11:36:57 -0700198 iset_->clear();
199 int32_t use_count = 0;
Aart Bikcc42be02016-10-20 16:14:16 -0700200 if (IsPhiInduction(phi) &&
Aart Bik482095d2016-10-10 15:39:10 -0700201 IsOnlyUsedAfterLoop(node->loop_info, phi, &use_count) &&
Aart Bik807868e2016-11-03 17:51:43 -0700202 // No uses, or no early-exit with proper replacement.
203 (use_count == 0 ||
204 (!IsEarlyExit(node->loop_info) && TryReplaceWithLastValue(phi, preheader)))) {
Aart Bik8c4a8542016-10-06 11:36:57 -0700205 for (HInstruction* i : *iset_) {
206 RemoveFromCycle(i);
Aart Bik281c6812016-08-26 11:31:48 -0700207 }
Aart Bikdf7822e2016-12-06 10:05:30 -0800208 simplified_ = true;
Aart Bik482095d2016-10-10 15:39:10 -0700209 induction_simplication_count_++;
210 }
211 }
212}
213
214void HLoopOptimization::SimplifyBlocks(LoopNode* node) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800215 // Iterate over all basic blocks in the loop-body.
216 for (HBlocksInLoopIterator it(*node->loop_info); !it.Done(); it.Advance()) {
217 HBasicBlock* block = it.Current();
218 // Remove dead instructions from the loop-body.
219 for (HBackwardInstructionIterator i(block->GetInstructions()); !i.Done(); i.Advance()) {
220 HInstruction* instruction = i.Current();
221 if (instruction->IsDeadAndRemovable()) {
222 simplified_ = true;
223 block->RemoveInstruction(instruction);
Aart Bik482095d2016-10-10 15:39:10 -0700224 }
Aart Bikdf7822e2016-12-06 10:05:30 -0800225 }
226 // Remove trivial control flow blocks from the loop-body.
227 HBasicBlock* succ = nullptr;
228 if (IsGotoBlock(block, &succ) && succ->GetPredecessors().size() == 1) {
229 // Trivial goto block can be removed.
230 HBasicBlock* pred = block->GetSinglePredecessor();
231 simplified_ = true;
232 pred->ReplaceSuccessor(block, succ);
233 block->RemoveDominatedBlock(succ);
234 block->DisconnectAndDelete();
235 pred->AddDominatedBlock(succ);
236 succ->SetDominator(pred);
237 } else if (block->GetSuccessors().size() == 2) {
238 // Trivial if block can be bypassed to either branch.
239 HBasicBlock* succ0 = block->GetSuccessors()[0];
240 HBasicBlock* succ1 = block->GetSuccessors()[1];
241 HBasicBlock* meet0 = nullptr;
242 HBasicBlock* meet1 = nullptr;
243 if (succ0 != succ1 &&
244 IsGotoBlock(succ0, &meet0) &&
245 IsGotoBlock(succ1, &meet1) &&
246 meet0 == meet1 && // meets again
247 meet0 != block && // no self-loop
248 meet0->GetPhis().IsEmpty()) { // not used for merging
249 simplified_ = true;
250 succ0->DisconnectAndDelete();
251 if (block->Dominates(meet0)) {
252 block->RemoveDominatedBlock(meet0);
253 succ1->AddDominatedBlock(meet0);
254 meet0->SetDominator(succ1);
Aart Bike3dedc52016-11-02 17:50:27 -0700255 }
Aart Bik482095d2016-10-10 15:39:10 -0700256 }
Aart Bik281c6812016-08-26 11:31:48 -0700257 }
Aart Bikdf7822e2016-12-06 10:05:30 -0800258 }
Aart Bik281c6812016-08-26 11:31:48 -0700259}
260
Aart Bik9abf8942016-10-14 09:49:42 -0700261void HLoopOptimization::RemoveIfEmptyInnerLoop(LoopNode* node) {
Aart Bik281c6812016-08-26 11:31:48 -0700262 HBasicBlock* header = node->loop_info->GetHeader();
263 HBasicBlock* preheader = node->loop_info->GetPreHeader();
Aart Bik9abf8942016-10-14 09:49:42 -0700264 // Ensure loop header logic is finite.
265 if (!induction_range_.IsFinite(node->loop_info)) {
266 return;
267 }
Aart Bik281c6812016-08-26 11:31:48 -0700268 // Ensure there is only a single loop-body (besides the header).
269 HBasicBlock* body = nullptr;
270 for (HBlocksInLoopIterator it(*node->loop_info); !it.Done(); it.Advance()) {
271 if (it.Current() != header) {
272 if (body != nullptr) {
273 return;
274 }
275 body = it.Current();
276 }
277 }
278 // Ensure there is only a single exit point.
279 if (header->GetSuccessors().size() != 2) {
280 return;
281 }
282 HBasicBlock* exit = (header->GetSuccessors()[0] == body)
283 ? header->GetSuccessors()[1]
284 : header->GetSuccessors()[0];
Aart Bik8c4a8542016-10-06 11:36:57 -0700285 // Ensure exit can only be reached by exiting loop.
Aart Bik281c6812016-08-26 11:31:48 -0700286 if (exit->GetPredecessors().size() != 1) {
287 return;
288 }
Aart Bik8c4a8542016-10-06 11:36:57 -0700289 // Detect an empty loop: no side effects other than plain iteration. Replace
290 // subsequent index uses, if any, with the last value and remove the loop.
291 iset_->clear();
292 int32_t use_count = 0;
Aart Bikcc42be02016-10-20 16:14:16 -0700293 if (IsEmptyHeader(header) &&
294 IsEmptyBody(body) &&
Aart Bik482095d2016-10-10 15:39:10 -0700295 IsOnlyUsedAfterLoop(node->loop_info, header->GetFirstPhi(), &use_count) &&
Aart Bik807868e2016-11-03 17:51:43 -0700296 // No uses, or proper replacement.
297 (use_count == 0 || TryReplaceWithLastValue(header->GetFirstPhi(), preheader))) {
Aart Bik281c6812016-08-26 11:31:48 -0700298 body->DisconnectAndDelete();
299 exit->RemovePredecessor(header);
300 header->RemoveSuccessor(exit);
Aart Bike3dedc52016-11-02 17:50:27 -0700301 header->RemoveDominatedBlock(exit);
Aart Bik281c6812016-08-26 11:31:48 -0700302 header->DisconnectAndDelete();
Aart Bik482095d2016-10-10 15:39:10 -0700303 preheader->AddSuccessor(exit);
304 preheader->AddInstruction(new (graph_->GetArena()) HGoto()); // global allocator
305 preheader->AddDominatedBlock(exit);
306 exit->SetDominator(preheader);
Aart Bik281c6812016-08-26 11:31:48 -0700307 // Update hierarchy.
308 RemoveLoop(node);
309 }
310}
311
Aart Bikcc42be02016-10-20 16:14:16 -0700312bool HLoopOptimization::IsPhiInduction(HPhi* phi) {
313 ArenaSet<HInstruction*>* set = induction_range_.LookupCycle(phi);
314 if (set != nullptr) {
Aart Bike3dedc52016-11-02 17:50:27 -0700315 DCHECK(iset_->empty());
Aart Bikcc42be02016-10-20 16:14:16 -0700316 for (HInstruction* i : *set) {
Aart Bike3dedc52016-11-02 17:50:27 -0700317 // Check that, other than instructions that are no longer in the graph (removed earlier)
318 // each instruction is removable and, other than the phi, uses are contained in the cycle.
319 if (!i->IsInBlock()) {
320 continue;
321 } else if (!i->IsRemovable()) {
322 return false;
323 } else if (i != phi) {
Aart Bikcc42be02016-10-20 16:14:16 -0700324 for (const HUseListNode<HInstruction*>& use : i->GetUses()) {
325 if (set->find(use.GetUser()) == set->end()) {
326 return false;
327 }
328 }
329 }
Aart Bike3dedc52016-11-02 17:50:27 -0700330 iset_->insert(i); // copy
Aart Bikcc42be02016-10-20 16:14:16 -0700331 }
Aart Bikcc42be02016-10-20 16:14:16 -0700332 return true;
333 }
334 return false;
335}
336
337// Find: phi: Phi(init, addsub)
338// s: SuspendCheck
339// c: Condition(phi, bound)
340// i: If(c)
341// TODO: Find a less pattern matching approach?
342bool HLoopOptimization::IsEmptyHeader(HBasicBlock* block) {
343 DCHECK(iset_->empty());
344 HInstruction* phi = block->GetFirstPhi();
345 if (phi != nullptr && phi->GetNext() == nullptr && IsPhiInduction(phi->AsPhi())) {
346 HInstruction* s = block->GetFirstInstruction();
347 if (s != nullptr && s->IsSuspendCheck()) {
348 HInstruction* c = s->GetNext();
349 if (c != nullptr && c->IsCondition() && c->GetUses().HasExactlyOneElement()) {
350 HInstruction* i = c->GetNext();
351 if (i != nullptr && i->IsIf() && i->InputAt(0) == c) {
352 iset_->insert(c);
353 iset_->insert(s);
354 return true;
355 }
356 }
357 }
358 }
359 return false;
360}
361
362bool HLoopOptimization::IsEmptyBody(HBasicBlock* block) {
363 if (block->GetFirstPhi() == nullptr) {
364 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
365 HInstruction* instruction = it.Current();
366 if (!instruction->IsGoto() && iset_->find(instruction) == iset_->end()) {
367 return false;
368 }
369 }
370 return true;
371 }
372 return false;
373}
374
Aart Bik482095d2016-10-10 15:39:10 -0700375bool HLoopOptimization::IsOnlyUsedAfterLoop(HLoopInformation* loop_info,
Aart Bik8c4a8542016-10-06 11:36:57 -0700376 HInstruction* instruction,
377 /*out*/ int32_t* use_count) {
378 for (const HUseListNode<HInstruction*>& use : instruction->GetUses()) {
379 HInstruction* user = use.GetUser();
380 if (iset_->find(user) == iset_->end()) { // not excluded?
381 HLoopInformation* other_loop_info = user->GetBlock()->GetLoopInformation();
Aart Bik482095d2016-10-10 15:39:10 -0700382 if (other_loop_info != nullptr && other_loop_info->IsIn(*loop_info)) {
Aart Bik8c4a8542016-10-06 11:36:57 -0700383 return false;
384 }
385 ++*use_count;
386 }
387 }
388 return true;
389}
390
391void HLoopOptimization::ReplaceAllUses(HInstruction* instruction, HInstruction* replacement) {
Aart Bik281c6812016-08-26 11:31:48 -0700392 const HUseList<HInstruction*>& uses = instruction->GetUses();
393 for (auto it = uses.begin(), end = uses.end(); it != end;) {
394 HInstruction* user = it->GetUser();
395 size_t index = it->GetIndex();
396 ++it; // increment before replacing
Aart Bik8c4a8542016-10-06 11:36:57 -0700397 if (iset_->find(user) == iset_->end()) { // not excluded?
Aart Bik281c6812016-08-26 11:31:48 -0700398 user->ReplaceInput(replacement, index);
399 induction_range_.Replace(user, instruction, replacement); // update induction
400 }
401 }
402 const HUseList<HEnvironment*>& env_uses = instruction->GetEnvUses();
403 for (auto it = env_uses.begin(), end = env_uses.end(); it != end;) {
404 HEnvironment* user = it->GetUser();
405 size_t index = it->GetIndex();
406 ++it; // increment before replacing
Aart Bik8c4a8542016-10-06 11:36:57 -0700407 if (iset_->find(user->GetHolder()) == iset_->end()) { // not excluded?
Aart Bik281c6812016-08-26 11:31:48 -0700408 user->RemoveAsUserOfInput(index);
409 user->SetRawEnvAt(index, replacement);
410 replacement->AddEnvUseAt(user, index);
411 }
412 }
413}
414
Aart Bik807868e2016-11-03 17:51:43 -0700415bool HLoopOptimization::TryReplaceWithLastValue(HInstruction* instruction, HBasicBlock* block) {
416 // Try to replace outside uses with the last value. Environment uses can consume this
417 // value too, since any first true use is outside the loop (although this may imply
418 // that de-opting may look "ahead" a bit on the phi value). If there are only environment
419 // uses, the value is dropped altogether, since the computations have no effect.
420 if (induction_range_.CanGenerateLastValue(instruction)) {
Aart Bik8c4a8542016-10-06 11:36:57 -0700421 ReplaceAllUses(instruction, induction_range_.GenerateLastValue(instruction, graph_, block));
Aart Bik807868e2016-11-03 17:51:43 -0700422 return true;
Aart Bik8c4a8542016-10-06 11:36:57 -0700423 }
Aart Bik807868e2016-11-03 17:51:43 -0700424 return false;
Aart Bik8c4a8542016-10-06 11:36:57 -0700425}
426
Aart Bik281c6812016-08-26 11:31:48 -0700427} // namespace art