1 /*
2 * Copyright (C) 2017 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 "code_sinking.h"
18
19 #include <sstream>
20
21 #include "android-base/logging.h"
22 #include "base/arena_bit_vector.h"
23 #include "base/array_ref.h"
24 #include "base/bit_vector-inl.h"
25 #include "base/globals.h"
26 #include "base/logging.h"
27 #include "base/scoped_arena_allocator.h"
28 #include "base/scoped_arena_containers.h"
29 #include "common_dominator.h"
30 #include "nodes.h"
31
32 namespace art HIDDEN {
33
Run()34 bool CodeSinking::Run() {
35 if (graph_->GetExitBlock() == nullptr) {
36 // Infinite loop, just bail.
37 return false;
38 }
39
40 UncommonBranchSinking();
41 ReturnSinking();
42 return true;
43 }
44
UncommonBranchSinking()45 void CodeSinking::UncommonBranchSinking() {
46 HBasicBlock* exit = graph_->GetExitBlock();
47 DCHECK(exit != nullptr);
48 // TODO(ngeoffray): we do not profile branches yet, so use throw instructions
49 // as an indicator of an uncommon branch.
50 for (HBasicBlock* exit_predecessor : exit->GetPredecessors()) {
51 HInstruction* last = exit_predecessor->GetLastInstruction();
52
53 // TryBoundary instructions are sometimes inserted between the last instruction (e.g. Throw,
54 // Return) and Exit. We don't want to use that instruction for our "uncommon branch" heuristic
55 // because they are not as good an indicator as throwing branches, so we skip them and fetch the
56 // actual last instruction.
57 if (last->IsTryBoundary()) {
58 // We have an exit try boundary. Fetch the previous instruction.
59 DCHECK(!last->AsTryBoundary()->IsEntry());
60 if (last->GetPrevious() == nullptr) {
61 DCHECK(exit_predecessor->IsSingleTryBoundary());
62 exit_predecessor = exit_predecessor->GetSinglePredecessor();
63 last = exit_predecessor->GetLastInstruction();
64 } else {
65 last = last->GetPrevious();
66 }
67 }
68
69 // Any predecessor of the exit that does not return, throws an exception.
70 if (!last->IsReturn() && !last->IsReturnVoid()) {
71 SinkCodeToUncommonBranch(exit_predecessor);
72 }
73 }
74 }
75
IsInterestingInstruction(HInstruction * instruction)76 static bool IsInterestingInstruction(HInstruction* instruction) {
77 // Instructions from the entry graph (for example constants) are never interesting to move.
78 if (instruction->GetBlock() == instruction->GetBlock()->GetGraph()->GetEntryBlock()) {
79 return false;
80 }
81 // We want to move moveable instructions that cannot throw, as well as
82 // heap stores and allocations.
83
84 // Volatile stores cannot be moved.
85 if (instruction->IsInstanceFieldSet()) {
86 if (instruction->AsInstanceFieldSet()->IsVolatile()) {
87 return false;
88 }
89 }
90
91 // Check allocations and strings first, as they can throw, but it is safe to move them.
92 if (instruction->IsNewInstance() || instruction->IsNewArray() || instruction->IsLoadString()) {
93 return true;
94 }
95
96 // Check it is safe to move ConstructorFence.
97 // (Safe to move ConstructorFence for only protecting the new-instance but not for finals.)
98 if (instruction->IsConstructorFence()) {
99 HConstructorFence* ctor_fence = instruction->AsConstructorFence();
100
101 // A fence with "0" inputs is dead and should've been removed in a prior pass.
102 DCHECK_NE(0u, ctor_fence->InputCount());
103
104 // TODO: this should be simplified to 'return true' since it's
105 // potentially pessimizing any code sinking for inlined constructors with final fields.
106 // TODO: double check that if the final field assignments are not moved,
107 // then the fence is not moved either.
108
109 return ctor_fence->GetAssociatedAllocation() != nullptr;
110 }
111
112 // All other instructions that can throw cannot be moved.
113 if (instruction->CanThrow()) {
114 return false;
115 }
116
117 // We can only store on local allocations. Other heap references can
118 // be escaping. Note that allocations can escape too, but we only move
119 // allocations if their users can move too, or are in the list of
120 // post dominated blocks.
121 if (instruction->IsInstanceFieldSet()) {
122 if (!instruction->InputAt(0)->IsNewInstance()) {
123 return false;
124 }
125 }
126
127 if (instruction->IsArraySet()) {
128 if (!instruction->InputAt(0)->IsNewArray()) {
129 return false;
130 }
131 }
132
133 // Heap accesses cannot go past instructions that have memory side effects, which
134 // we are not tracking here. Note that the load/store elimination optimization
135 // runs before this optimization, and should have removed interesting ones.
136 // In theory, we could handle loads of local allocations, but this is currently
137 // hard to test, as LSE removes them.
138 if (instruction->IsStaticFieldGet() ||
139 instruction->IsInstanceFieldGet() ||
140 instruction->IsArrayGet()) {
141 return false;
142 }
143
144 if (instruction->IsInstanceFieldSet() ||
145 instruction->IsArraySet() ||
146 instruction->CanBeMoved()) {
147 return true;
148 }
149 return false;
150 }
151
AddInstruction(HInstruction * instruction,BitVectorView<size_t> processed_instructions,BitVectorView<size_t> discard_blocks,ScopedArenaVector<HInstruction * > * worklist)152 static void AddInstruction(HInstruction* instruction,
153 BitVectorView<size_t> processed_instructions,
154 BitVectorView<size_t> discard_blocks,
155 ScopedArenaVector<HInstruction*>* worklist) {
156 // Add to the work list if the instruction is not in the list of blocks
157 // to discard, hasn't been already processed and is of interest.
158 if (!discard_blocks.IsBitSet(instruction->GetBlock()->GetBlockId()) &&
159 !processed_instructions.IsBitSet(instruction->GetId()) &&
160 IsInterestingInstruction(instruction)) {
161 worklist->push_back(instruction);
162 }
163 }
164
AddInputs(HInstruction * instruction,BitVectorView<size_t> processed_instructions,BitVectorView<size_t> discard_blocks,ScopedArenaVector<HInstruction * > * worklist)165 static void AddInputs(HInstruction* instruction,
166 BitVectorView<size_t> processed_instructions,
167 BitVectorView<size_t> discard_blocks,
168 ScopedArenaVector<HInstruction*>* worklist) {
169 for (HInstruction* input : instruction->GetInputs()) {
170 AddInstruction(input, processed_instructions, discard_blocks, worklist);
171 }
172 }
173
AddInputs(HBasicBlock * block,BitVectorView<size_t> processed_instructions,BitVectorView<size_t> discard_blocks,ScopedArenaVector<HInstruction * > * worklist)174 static void AddInputs(HBasicBlock* block,
175 BitVectorView<size_t> processed_instructions,
176 BitVectorView<size_t> discard_blocks,
177 ScopedArenaVector<HInstruction*>* worklist) {
178 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
179 AddInputs(it.Current(), processed_instructions, discard_blocks, worklist);
180 }
181 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
182 AddInputs(it.Current(), processed_instructions, discard_blocks, worklist);
183 }
184 }
185
ShouldFilterUse(HInstruction * instruction,HInstruction * user,BitVectorView<size_t> post_dominated)186 static bool ShouldFilterUse(HInstruction* instruction,
187 HInstruction* user,
188 BitVectorView<size_t> post_dominated) {
189 if (instruction->IsNewInstance()) {
190 return (user->IsInstanceFieldSet() || user->IsConstructorFence()) &&
191 (user->InputAt(0) == instruction) &&
192 !post_dominated.IsBitSet(user->GetBlock()->GetBlockId());
193 } else if (instruction->IsNewArray()) {
194 return (user->IsArraySet() || user->IsConstructorFence()) &&
195 (user->InputAt(0) == instruction) &&
196 !post_dominated.IsBitSet(user->GetBlock()->GetBlockId());
197 }
198 return false;
199 }
200
201 // Find the ideal position for moving `instruction`. If `filter` is true,
202 // we filter out store instructions to that instruction, which are processed
203 // first in the step (3) of the sinking algorithm.
204 // This method is tailored to the sinking algorithm, unlike
205 // the generic HInstruction::MoveBeforeFirstUserAndOutOfLoops.
FindIdealPosition(HInstruction * instruction,BitVectorView<size_t> post_dominated,bool filter=false)206 static HInstruction* FindIdealPosition(HInstruction* instruction,
207 BitVectorView<size_t> post_dominated,
208 bool filter = false) {
209 DCHECK(!instruction->IsPhi()); // Makes no sense for Phi.
210
211 // Find the target block.
212 CommonDominator finder(/* block= */ nullptr);
213 for (const HUseListNode<HInstruction*>& use : instruction->GetUses()) {
214 HInstruction* user = use.GetUser();
215 if (!(filter && ShouldFilterUse(instruction, user, post_dominated))) {
216 HBasicBlock* block = user->GetBlock();
217 if (user->IsPhi()) {
218 // Special case phis by taking the incoming block for regular ones,
219 // or the dominator for catch phis.
220 block = user->AsPhi()->IsCatchPhi()
221 ? block->GetDominator()
222 : block->GetPredecessors()[use.GetIndex()];
223 }
224 finder.Update(block);
225 }
226 }
227 for (const HUseListNode<HEnvironment*>& use : instruction->GetEnvUses()) {
228 DCHECK(!use.GetUser()->GetHolder()->IsPhi());
229 DCHECK_IMPLIES(filter,
230 !ShouldFilterUse(instruction, use.GetUser()->GetHolder(), post_dominated));
231 finder.Update(use.GetUser()->GetHolder()->GetBlock());
232 }
233 HBasicBlock* target_block = finder.Get();
234 if (target_block == nullptr) {
235 // No user we can go next to? Likely a LSE or DCE limitation.
236 return nullptr;
237 }
238
239 // Move to the first dominator not in a loop, if we can. We only do this if we are trying to hoist
240 // `instruction` out of a loop it wasn't a part of.
241 const HLoopInformation* loop_info = instruction->GetBlock()->GetLoopInformation();
242 while (target_block->IsInLoop() && target_block->GetLoopInformation() != loop_info) {
243 if (!post_dominated.IsBitSet(target_block->GetDominator()->GetBlockId())) {
244 break;
245 }
246 target_block = target_block->GetDominator();
247 DCHECK(target_block != nullptr);
248 }
249
250 if (instruction->CanThrow()) {
251 // Consistency check: We shouldn't land in a loop if we weren't in one before traversing up the
252 // dominator tree regarding try catches.
253 const bool was_in_loop = target_block->IsInLoop();
254
255 // We cannot move an instruction that can throw into a try that said instruction is not a part
256 // of already, as that would mean it will throw into a different catch block. In short, for
257 // throwing instructions:
258 // * If the throwing instruction is part of a try, they should only be sunk into that same try.
259 // * If the throwing instruction is not part of any try, they shouldn't be sunk to any try.
260 if (instruction->GetBlock()->IsTryBlock()) {
261 const HTryBoundary& try_entry =
262 instruction->GetBlock()->GetTryCatchInformation()->GetTryEntry();
263 while (!(target_block->IsTryBlock() &&
264 try_entry.HasSameExceptionHandlersAs(
265 target_block->GetTryCatchInformation()->GetTryEntry()))) {
266 target_block = target_block->GetDominator();
267 if (!post_dominated.IsBitSet(target_block->GetBlockId())) {
268 // We couldn't find a suitable block.
269 return nullptr;
270 }
271 }
272 } else {
273 // Search for the first block also not in a try block
274 while (target_block->IsTryBlock()) {
275 target_block = target_block->GetDominator();
276 if (!post_dominated.IsBitSet(target_block->GetBlockId())) {
277 // We couldn't find a suitable block.
278 return nullptr;
279 }
280 }
281 }
282
283 DCHECK_IMPLIES(target_block->IsInLoop(), was_in_loop);
284 }
285
286 // Find insertion position. No need to filter anymore, as we have found a
287 // target block.
288 HInstruction* insert_pos = nullptr;
289 for (const HUseListNode<HInstruction*>& use : instruction->GetUses()) {
290 if (use.GetUser()->GetBlock() == target_block &&
291 (insert_pos == nullptr || use.GetUser()->StrictlyDominates(insert_pos))) {
292 insert_pos = use.GetUser();
293 }
294 }
295 for (const HUseListNode<HEnvironment*>& use : instruction->GetEnvUses()) {
296 HEnvironment* env = use.GetUser();
297 HInstruction* user = env->GetHolder();
298 if (user->GetBlock() == target_block &&
299 (insert_pos == nullptr || user->StrictlyDominates(insert_pos))) {
300 if (target_block->IsCatchBlock() && target_block->GetFirstInstruction() == user) {
301 // We can sink the instructions past the environment setting Nop. If we do that, we have to
302 // remove said instruction from the environment. Since we know that we will be sinking the
303 // instruction to this block and there are no more instructions to consider, we can safely
304 // remove it from the environment now.
305 DCHECK(target_block->GetFirstInstruction()->IsNop());
306 env->RemoveAsUserOfInput(use.GetIndex());
307 env->SetRawEnvAt(use.GetIndex(), /*instruction=*/ nullptr);
308 } else {
309 insert_pos = user;
310 }
311 }
312 }
313 if (insert_pos == nullptr) {
314 // No user in `target_block`, insert before the control flow instruction.
315 insert_pos = target_block->GetLastInstruction();
316 DCHECK(insert_pos->IsControlFlow());
317 // Avoid splitting HCondition from HIf to prevent unnecessary materialization.
318 if (insert_pos->IsIf()) {
319 HInstruction* if_input = insert_pos->AsIf()->InputAt(0);
320 if (if_input == insert_pos->GetPrevious()) {
321 insert_pos = if_input;
322 }
323 }
324 }
325 DCHECK(!insert_pos->IsPhi());
326 return insert_pos;
327 }
328
329
SinkCodeToUncommonBranch(HBasicBlock * end_block)330 void CodeSinking::SinkCodeToUncommonBranch(HBasicBlock* end_block) {
331 // Local allocator to discard data structures created below at the end of this optimization.
332 ScopedArenaAllocator allocator(graph_->GetArenaStack());
333
334 size_t number_of_instructions = graph_->GetCurrentInstructionId();
335 ScopedArenaVector<HInstruction*> worklist(allocator.Adapter(kArenaAllocMisc));
336 BitVectorView<size_t> processed_instructions =
337 ArenaBitVector::CreateFixedSize(&allocator, number_of_instructions);
338 BitVectorView<size_t> post_dominated =
339 ArenaBitVector::CreateFixedSize(&allocator, graph_->GetBlocks().size());
340
341 // Step (1): Visit post order to get a subset of blocks post dominated by `end_block`.
342 // TODO(ngeoffray): Getting the full set of post-dominated should be done by
343 // computing the post dominator tree, but that could be too time consuming. Also,
344 // we should start the analysis from blocks dominated by an uncommon branch, but we
345 // don't profile branches yet.
346 bool found_block = false;
347 for (HBasicBlock* block : graph_->GetPostOrder()) {
348 if (block == end_block) {
349 found_block = true;
350 post_dominated.SetBit(block->GetBlockId());
351 } else if (found_block) {
352 bool is_post_dominated = true;
353 DCHECK_NE(block, graph_->GetExitBlock())
354 << "We shouldn't encounter the exit block after `end_block`.";
355
356 // BasicBlock that are try entries look like this:
357 // BasicBlock i:
358 // instr 1
359 // ...
360 // instr N
361 // TryBoundary kind:entry ---Try begins here---
362 //
363 // Due to how our BasicBlocks are structured, BasicBlock i will have an xhandler successor
364 // since we are starting a try. If we use `GetSuccessors` for this case, we will check if
365 // the catch block is post_dominated.
366 //
367 // However, this catch block doesn't matter: when we sink the instruction into that
368 // BasicBlock i, we do it before the TryBoundary (i.e. outside of the try and outside the
369 // catch's domain). We can ignore catch blocks using `GetNormalSuccessors` to sink code
370 // right before the start of a try block.
371 //
372 // On the other side of the coin, BasicBlock that are try exits look like this:
373 // BasicBlock j:
374 // instr 1
375 // ...
376 // instr N
377 // TryBoundary kind:exit ---Try ends here---
378 //
379 // If we sink to these basic blocks we would be sinking inside of the try so we would like
380 // to check the catch block for post dominance.
381 const bool ends_with_try_boundary_entry =
382 block->EndsWithTryBoundary() && block->GetLastInstruction()->AsTryBoundary()->IsEntry();
383 ArrayRef<HBasicBlock* const> successors =
384 ends_with_try_boundary_entry ? block->GetNormalSuccessors() :
385 ArrayRef<HBasicBlock* const>(block->GetSuccessors());
386 for (HBasicBlock* successor : successors) {
387 if (!post_dominated.IsBitSet(successor->GetBlockId())) {
388 is_post_dominated = false;
389 break;
390 }
391 }
392 if (is_post_dominated) {
393 post_dominated.SetBit(block->GetBlockId());
394 }
395 }
396 }
397
398 // Now that we have found a subset of post-dominated blocks, add to the worklist all inputs
399 // of instructions in these blocks that are not themselves in these blocks.
400 // Also find the common dominator of the found post dominated blocks, to help filtering
401 // out un-movable uses in step (2).
402 CommonDominator finder(end_block);
403 for (size_t i = 0, e = graph_->GetBlocks().size(); i < e; ++i) {
404 if (post_dominated.IsBitSet(i)) {
405 finder.Update(graph_->GetBlocks()[i]);
406 AddInputs(graph_->GetBlocks()[i], processed_instructions, post_dominated, &worklist);
407 }
408 }
409 HBasicBlock* common_dominator = finder.Get();
410
411 // Step (2): iterate over the worklist to find sinking candidates.
412 BitVectorView<size_t> instructions_that_can_move =
413 ArenaBitVector::CreateFixedSize(&allocator, number_of_instructions);
414 ScopedArenaVector<ScopedArenaVector<HInstruction*>> instructions_to_move(
415 graph_->GetBlocks().size(),
416 ScopedArenaVector<HInstruction*>(allocator.Adapter(kArenaAllocMisc)),
417 allocator.Adapter(kArenaAllocMisc));
418 while (!worklist.empty()) {
419 HInstruction* instruction = worklist.back();
420 if (processed_instructions.IsBitSet(instruction->GetId())) {
421 // The instruction has already been processed, continue. This happens
422 // when the instruction is the input/user of multiple instructions.
423 worklist.pop_back();
424 continue;
425 }
426 bool all_users_in_post_dominated_blocks = true;
427 bool can_move = true;
428 // Check users of the instruction.
429 for (const HUseListNode<HInstruction*>& use : instruction->GetUses()) {
430 HInstruction* user = use.GetUser();
431 if (!post_dominated.IsBitSet(user->GetBlock()->GetBlockId()) &&
432 !instructions_that_can_move.IsBitSet(user->GetId())) {
433 all_users_in_post_dominated_blocks = false;
434 // If we've already processed this user, or the user cannot be moved, or
435 // is not dominating the post dominated blocks, bail.
436 // TODO(ngeoffray): The domination check is an approximation. We should
437 // instead check if the dominated blocks post dominate the user's block,
438 // but we do not have post dominance information here.
439 if (processed_instructions.IsBitSet(user->GetId()) ||
440 !IsInterestingInstruction(user) ||
441 !user->GetBlock()->Dominates(common_dominator)) {
442 can_move = false;
443 break;
444 }
445 }
446 }
447
448 // Check environment users of the instruction. Some of these users require
449 // the instruction not to move.
450 if (all_users_in_post_dominated_blocks) {
451 for (const HUseListNode<HEnvironment*>& use : instruction->GetEnvUses()) {
452 HEnvironment* environment = use.GetUser();
453 HInstruction* user = environment->GetHolder();
454 if (!post_dominated.IsBitSet(user->GetBlock()->GetBlockId())) {
455 if (graph_->IsDebuggable() ||
456 user->IsDeoptimize() ||
457 user->CanThrowIntoCatchBlock() ||
458 (user->IsSuspendCheck() && graph_->IsCompilingOsr())) {
459 can_move = false;
460 break;
461 }
462 }
463 }
464 }
465 if (!can_move) {
466 // Instruction cannot be moved, mark it as processed and remove it from the work
467 // list.
468 processed_instructions.SetBit(instruction->GetId());
469 worklist.pop_back();
470 } else if (all_users_in_post_dominated_blocks) {
471 // Instruction is a candidate for being sunk. Mark it as such, remove it from the
472 // work list, and add its inputs to the work list.
473 instructions_that_can_move.SetBit(instruction->GetId());
474 instructions_to_move[instruction->GetBlock()->GetBlockId()].push_back(instruction);
475 processed_instructions.SetBit(instruction->GetId());
476 worklist.pop_back();
477 AddInputs(instruction, processed_instructions, post_dominated, &worklist);
478 // Drop the environment use not in the list of post-dominated block. This is
479 // to help step (3) of this optimization, when we start moving instructions
480 // closer to their use.
481 for (const HUseListNode<HEnvironment*>& use : instruction->GetEnvUses()) {
482 HEnvironment* environment = use.GetUser();
483 HInstruction* user = environment->GetHolder();
484 if (!post_dominated.IsBitSet(user->GetBlock()->GetBlockId())) {
485 environment->RemoveAsUserOfInput(use.GetIndex());
486 environment->SetRawEnvAt(use.GetIndex(), nullptr);
487 }
488 }
489 } else {
490 // The information we have on the users was not enough to decide whether the
491 // instruction could be moved.
492 // Add the users to the work list, and keep the instruction in the work list
493 // to process it again once all users have been processed.
494 for (const HUseListNode<HInstruction*>& use : instruction->GetUses()) {
495 AddInstruction(use.GetUser(), processed_instructions, post_dominated, &worklist);
496 }
497 }
498 }
499
500 // We want to process the instructions in reverse dominated order. This is required for heap
501 // stores. To guarantee this (including the transitivity of incomparability) we have some extra
502 // bookkeeping.
503 ScopedArenaVector<HInstruction*> instructions_to_move_sorted(allocator.Adapter(kArenaAllocMisc));
504 for (HBasicBlock* block : graph_->GetPostOrder()) {
505 const int block_id = block->GetBlockId();
506
507 // Order the block itself first.
508 std::sort(instructions_to_move[block_id].begin(),
509 instructions_to_move[block_id].end(),
510 [&block](HInstruction* a, HInstruction* b) {
511 return block->GetInstructions().FoundBefore(b, a);
512 });
513
514 for (HInstruction* instruction : instructions_to_move[block_id]) {
515 instructions_to_move_sorted.push_back(instruction);
516 }
517 }
518
519 if (kIsDebugBuild) {
520 // We should have ordered the instructions in reverse dominated order. This means that
521 // instructions shouldn't dominate instructions that come after it in the vector.
522 for (size_t i = 0; i < instructions_to_move_sorted.size(); ++i) {
523 for (size_t j = i + 1; j < instructions_to_move_sorted.size(); ++j) {
524 if (instructions_to_move_sorted[i]->StrictlyDominates(instructions_to_move_sorted[j])) {
525 std::stringstream ss;
526 graph_->Dump(ss, nullptr);
527 ss << "\n"
528 << "{";
529 for (HInstruction* instr : instructions_to_move_sorted) {
530 ss << *instr << " in block: " << instr->GetBlock() << ", ";
531 }
532 ss << "}\n";
533 ss << "i = " << i << " which is " << *instructions_to_move_sorted[i]
534 << "strictly dominates j = " << j << " which is " << *instructions_to_move_sorted[j]
535 << "\n";
536 LOG(FATAL) << "Unexpected ordering of code sinking instructions: " << ss.str();
537 }
538 }
539 }
540 }
541
542 // Step (3): Try to move sinking candidates.
543 for (HInstruction* instruction : instructions_to_move_sorted) {
544 HInstruction* position = nullptr;
545 if (instruction->IsArraySet()
546 || instruction->IsInstanceFieldSet()
547 || instruction->IsConstructorFence()) {
548 if (!instructions_that_can_move.IsBitSet(instruction->InputAt(0)->GetId())) {
549 // A store can trivially move, but it can safely do so only if the heap
550 // location it stores to can also move.
551 // TODO(ngeoffray): Handle allocation/store cycles by pruning these instructions
552 // from the set and all their inputs.
553 continue;
554 }
555 // Find the position of the instruction we're storing into, filtering out this
556 // store and all other stores to that instruction.
557 position = FindIdealPosition(instruction->InputAt(0), post_dominated, /* filter= */ true);
558
559 // The position needs to be dominated by the store, in order for the store to move there.
560 if (position == nullptr || !instruction->GetBlock()->Dominates(position->GetBlock())) {
561 continue;
562 }
563 } else {
564 // Find the ideal position within the post dominated blocks.
565 position = FindIdealPosition(instruction, post_dominated);
566 if (position == nullptr) {
567 continue;
568 }
569 }
570 // Bail if we could not find a position in the post dominated blocks (for example,
571 // if there are multiple users whose common dominator is not in the list of
572 // post dominated blocks).
573 if (!post_dominated.IsBitSet(position->GetBlock()->GetBlockId())) {
574 continue;
575 }
576 MaybeRecordStat(stats_, MethodCompilationStat::kInstructionSunk);
577 instruction->MoveBefore(position, /* do_checks= */ false);
578 }
579 }
580
ReturnSinking()581 void CodeSinking::ReturnSinking() {
582 HBasicBlock* exit = graph_->GetExitBlock();
583 DCHECK(exit != nullptr);
584
585 int number_of_returns = 0;
586 bool saw_return = false;
587 for (HBasicBlock* pred : exit->GetPredecessors()) {
588 // TODO(solanes): We might have Return/ReturnVoid->TryBoundary->Exit. We can theoretically
589 // handle them and move them out of the TryBoundary. However, it is a border case and it adds
590 // codebase complexity.
591 if (pred->GetLastInstruction()->IsReturn() || pred->GetLastInstruction()->IsReturnVoid()) {
592 saw_return |= pred->GetLastInstruction()->IsReturn();
593 ++number_of_returns;
594 }
595 }
596
597 if (number_of_returns < 2) {
598 // Nothing to do.
599 return;
600 }
601
602 // `new_block` will coalesce the Return instructions into Phi+Return, or the ReturnVoid
603 // instructions into a ReturnVoid.
604 HBasicBlock* new_block = new (graph_->GetAllocator()) HBasicBlock(graph_, exit->GetDexPc());
605 if (saw_return) {
606 HPhi* new_phi = nullptr;
607 for (size_t i = 0; i < exit->GetPredecessors().size(); /*++i in loop*/) {
608 HBasicBlock* pred = exit->GetPredecessors()[i];
609 if (!pred->GetLastInstruction()->IsReturn()) {
610 ++i;
611 continue;
612 }
613
614 HReturn* ret = pred->GetLastInstruction()->AsReturn();
615 if (new_phi == nullptr) {
616 // Create the new_phi, if we haven't done so yet. We do it here since we need to know the
617 // type to assign to it.
618 new_phi = new (graph_->GetAllocator()) HPhi(graph_->GetAllocator(),
619 kNoRegNumber,
620 /*number_of_inputs=*/0,
621 ret->InputAt(0)->GetType());
622 new_block->AddPhi(new_phi);
623 }
624 new_phi->AddInput(ret->InputAt(0));
625 pred->ReplaceAndRemoveInstructionWith(ret,
626 new (graph_->GetAllocator()) HGoto(ret->GetDexPc()));
627 pred->ReplaceSuccessor(exit, new_block);
628 // Since we are removing a predecessor, there's no need to increment `i`.
629 }
630 new_block->AddInstruction(new (graph_->GetAllocator()) HReturn(new_phi, exit->GetDexPc()));
631 } else {
632 for (size_t i = 0; i < exit->GetPredecessors().size(); /*++i in loop*/) {
633 HBasicBlock* pred = exit->GetPredecessors()[i];
634 if (!pred->GetLastInstruction()->IsReturnVoid()) {
635 ++i;
636 continue;
637 }
638
639 HReturnVoid* ret = pred->GetLastInstruction()->AsReturnVoid();
640 pred->ReplaceAndRemoveInstructionWith(ret,
641 new (graph_->GetAllocator()) HGoto(ret->GetDexPc()));
642 pred->ReplaceSuccessor(exit, new_block);
643 // Since we are removing a predecessor, there's no need to increment `i`.
644 }
645 new_block->AddInstruction(new (graph_->GetAllocator()) HReturnVoid(exit->GetDexPc()));
646 }
647
648 new_block->AddSuccessor(exit);
649 graph_->AddBlock(new_block);
650
651 // Recompute dominance since we added a new block.
652 graph_->ClearDominanceInformation();
653 graph_->ComputeDominanceInformation();
654 }
655
656 } // namespace art
657