• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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 "licm.h"
18 
19 #include "side_effects_analysis.h"
20 
21 namespace art HIDDEN {
22 
IsPhiOf(HInstruction * instruction,HBasicBlock * block)23 static bool IsPhiOf(HInstruction* instruction, HBasicBlock* block) {
24   return instruction->IsPhi() && instruction->GetBlock() == block;
25 }
26 
27 /**
28  * Returns whether `instruction` has all its inputs and environment defined
29  * before the loop it is in.
30  */
InputsAreDefinedBeforeLoop(HInstruction * instruction)31 static bool InputsAreDefinedBeforeLoop(HInstruction* instruction) {
32   DCHECK(instruction->IsInLoop());
33   HLoopInformation* info = instruction->GetBlock()->GetLoopInformation();
34   for (const HInstruction* input : instruction->GetInputs()) {
35     HLoopInformation* input_loop = input->GetBlock()->GetLoopInformation();
36     // We only need to check whether the input is defined in the loop. If it is not
37     // it is defined before the loop.
38     if (input_loop != nullptr && input_loop->IsIn(*info)) {
39       return false;
40     }
41   }
42 
43   for (HEnvironment* environment = instruction->GetEnvironment();
44        environment != nullptr;
45        environment = environment->GetParent()) {
46     for (size_t i = 0, e = environment->Size(); i < e; ++i) {
47       HInstruction* input = environment->GetInstructionAt(i);
48       if (input != nullptr) {
49         HLoopInformation* input_loop = input->GetBlock()->GetLoopInformation();
50         if (input_loop != nullptr && input_loop->IsIn(*info)) {
51           // We can move an instruction that takes a loop header phi in the environment:
52           // we will just replace that phi with its first input later in `UpdateLoopPhisIn`.
53           bool is_loop_header_phi = IsPhiOf(input, info->GetHeader());
54           if (!is_loop_header_phi) {
55             return false;
56           }
57         }
58       }
59     }
60   }
61   return true;
62 }
63 
64 /**
65  * If `environment` has a loop header phi, we replace it with its first input.
66  */
UpdateLoopPhisIn(ArenaAllocator * allocator,HEnvironment * environment,HLoopInformation * info)67 static void UpdateLoopPhisIn(ArenaAllocator* allocator,
68                              HEnvironment* environment,
69                              HLoopInformation* info) {
70   for (; environment != nullptr; environment = environment->GetParent()) {
71     for (size_t i = 0, e = environment->Size(); i < e; ++i) {
72       HInstruction* input = environment->GetInstructionAt(i);
73       if (input != nullptr && IsPhiOf(input, info->GetHeader())) {
74         environment->RemoveAsUserOfInput(i);
75         HInstruction* incoming = input->InputAt(0);
76         environment->SetRawEnvAt(i, incoming);
77         incoming->AddEnvUseAt(allocator, environment, i);
78       }
79     }
80   }
81 }
82 
Run()83 bool LICM::Run() {
84   bool didLICM = false;
85   DCHECK(side_effects_.HasRun());
86 
87   // Only used during debug.
88   ArenaBitVector* visited = nullptr;
89   if (kIsDebugBuild) {
90     visited = new (graph_->GetAllocator()) ArenaBitVector(graph_->GetAllocator(),
91                                                           graph_->GetBlocks().size(),
92                                                           false,
93                                                           kArenaAllocLICM);
94   }
95 
96   // Post order visit to visit inner loops before outer loops.
97   for (HBasicBlock* block : graph_->GetPostOrder()) {
98     if (!block->IsLoopHeader()) {
99       // Only visit the loop when we reach the header.
100       continue;
101     }
102 
103     HLoopInformation* loop_info = block->GetLoopInformation();
104     SideEffects loop_effects = side_effects_.GetLoopEffects(block);
105     HBasicBlock* pre_header = loop_info->GetPreHeader();
106 
107     for (HBlocksInLoopIterator it_loop(*loop_info); !it_loop.Done(); it_loop.Advance()) {
108       HBasicBlock* inner = it_loop.Current();
109       DCHECK(inner->IsInLoop());
110       if (inner->GetLoopInformation() != loop_info) {
111         // Thanks to post order visit, inner loops were already visited.
112         DCHECK(visited->IsBitSet(inner->GetBlockId()));
113         continue;
114       }
115       if (kIsDebugBuild) {
116         visited->SetBit(inner->GetBlockId());
117       }
118 
119       if (loop_info->ContainsIrreducibleLoop()) {
120         // We cannot licm in an irreducible loop, or in a natural loop containing an
121         // irreducible loop.
122         continue;
123       }
124       DCHECK(!loop_info->IsIrreducible());
125 
126       // We can move an instruction that can throw only as long as it is the first visible
127       // instruction (throw or write) in the loop. Note that the first potentially visible
128       // instruction that is not hoisted stops this optimization. Non-throwing instructions,
129       // on the other hand, can still be hoisted.
130       bool found_first_non_hoisted_visible_instruction_in_loop = !inner->IsLoopHeader();
131       for (HInstructionIterator inst_it(inner->GetInstructions());
132            !inst_it.Done();
133            inst_it.Advance()) {
134         HInstruction* instruction = inst_it.Current();
135         bool can_move = false;
136         if (instruction->CanBeMoved() && InputsAreDefinedBeforeLoop(instruction)) {
137           if (instruction->CanThrow()) {
138             if (!found_first_non_hoisted_visible_instruction_in_loop) {
139               DCHECK(instruction->GetBlock()->IsLoopHeader());
140               if (instruction->IsClinitCheck()) {
141                 // clinit is only done once, and since all visible instructions
142                 // in the loop header so far have been hoisted out, we can hoist
143                 // the clinit check out also.
144                 can_move = true;
145               } else if (!instruction->GetSideEffects().MayDependOn(loop_effects)) {
146                 can_move = true;
147               }
148             }
149           } else if (!instruction->GetSideEffects().MayDependOn(loop_effects)) {
150             can_move = true;
151           }
152         }
153         if (can_move) {
154           // We need to update the environment if the instruction has a loop header
155           // phi in it.
156           if (instruction->NeedsEnvironment()) {
157             UpdateLoopPhisIn(graph_->GetAllocator(), instruction->GetEnvironment(), loop_info);
158           } else {
159             DCHECK(!instruction->HasEnvironment());
160           }
161           instruction->MoveBefore(pre_header->GetLastInstruction());
162           MaybeRecordStat(stats_, MethodCompilationStat::kLoopInvariantMoved);
163           didLICM = true;
164         }
165 
166         if (!can_move && (instruction->CanThrow() || instruction->DoesAnyWrite())) {
167           // If `instruction` can do something visible (throw or write),
168           // we cannot move further instructions that can throw.
169           found_first_non_hoisted_visible_instruction_in_loop = true;
170         }
171       }
172     }
173   }
174   return didLICM;
175 }
176 
177 }  // namespace art
178