xref: /aosp_15_r20/art/compiler/optimizing/licm.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2015 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #include "licm.h"
18*795d594fSAndroid Build Coastguard Worker 
19*795d594fSAndroid Build Coastguard Worker #include "side_effects_analysis.h"
20*795d594fSAndroid Build Coastguard Worker 
21*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
22*795d594fSAndroid Build Coastguard Worker 
IsPhiOf(HInstruction * instruction,HBasicBlock * block)23*795d594fSAndroid Build Coastguard Worker static bool IsPhiOf(HInstruction* instruction, HBasicBlock* block) {
24*795d594fSAndroid Build Coastguard Worker   return instruction->IsPhi() && instruction->GetBlock() == block;
25*795d594fSAndroid Build Coastguard Worker }
26*795d594fSAndroid Build Coastguard Worker 
27*795d594fSAndroid Build Coastguard Worker /**
28*795d594fSAndroid Build Coastguard Worker  * Returns whether `instruction` has all its inputs and environment defined
29*795d594fSAndroid Build Coastguard Worker  * before the loop it is in.
30*795d594fSAndroid Build Coastguard Worker  */
InputsAreDefinedBeforeLoop(HInstruction * instruction)31*795d594fSAndroid Build Coastguard Worker static bool InputsAreDefinedBeforeLoop(HInstruction* instruction) {
32*795d594fSAndroid Build Coastguard Worker   DCHECK(instruction->IsInLoop());
33*795d594fSAndroid Build Coastguard Worker   HLoopInformation* info = instruction->GetBlock()->GetLoopInformation();
34*795d594fSAndroid Build Coastguard Worker   for (const HInstruction* input : instruction->GetInputs()) {
35*795d594fSAndroid Build Coastguard Worker     HLoopInformation* input_loop = input->GetBlock()->GetLoopInformation();
36*795d594fSAndroid Build Coastguard Worker     // We only need to check whether the input is defined in the loop. If it is not
37*795d594fSAndroid Build Coastguard Worker     // it is defined before the loop.
38*795d594fSAndroid Build Coastguard Worker     if (input_loop != nullptr && input_loop->IsIn(*info)) {
39*795d594fSAndroid Build Coastguard Worker       return false;
40*795d594fSAndroid Build Coastguard Worker     }
41*795d594fSAndroid Build Coastguard Worker   }
42*795d594fSAndroid Build Coastguard Worker 
43*795d594fSAndroid Build Coastguard Worker   for (HEnvironment* environment = instruction->GetEnvironment();
44*795d594fSAndroid Build Coastguard Worker        environment != nullptr;
45*795d594fSAndroid Build Coastguard Worker        environment = environment->GetParent()) {
46*795d594fSAndroid Build Coastguard Worker     for (size_t i = 0, e = environment->Size(); i < e; ++i) {
47*795d594fSAndroid Build Coastguard Worker       HInstruction* input = environment->GetInstructionAt(i);
48*795d594fSAndroid Build Coastguard Worker       if (input != nullptr) {
49*795d594fSAndroid Build Coastguard Worker         HLoopInformation* input_loop = input->GetBlock()->GetLoopInformation();
50*795d594fSAndroid Build Coastguard Worker         if (input_loop != nullptr && input_loop->IsIn(*info)) {
51*795d594fSAndroid Build Coastguard Worker           // We can move an instruction that takes a loop header phi in the environment:
52*795d594fSAndroid Build Coastguard Worker           // we will just replace that phi with its first input later in `UpdateLoopPhisIn`.
53*795d594fSAndroid Build Coastguard Worker           bool is_loop_header_phi = IsPhiOf(input, info->GetHeader());
54*795d594fSAndroid Build Coastguard Worker           if (!is_loop_header_phi) {
55*795d594fSAndroid Build Coastguard Worker             return false;
56*795d594fSAndroid Build Coastguard Worker           }
57*795d594fSAndroid Build Coastguard Worker         }
58*795d594fSAndroid Build Coastguard Worker       }
59*795d594fSAndroid Build Coastguard Worker     }
60*795d594fSAndroid Build Coastguard Worker   }
61*795d594fSAndroid Build Coastguard Worker   return true;
62*795d594fSAndroid Build Coastguard Worker }
63*795d594fSAndroid Build Coastguard Worker 
64*795d594fSAndroid Build Coastguard Worker /**
65*795d594fSAndroid Build Coastguard Worker  * If `environment` has a loop header phi, we replace it with its first input.
66*795d594fSAndroid Build Coastguard Worker  */
UpdateLoopPhisIn(HEnvironment * environment,HLoopInformation * info)67*795d594fSAndroid Build Coastguard Worker static void UpdateLoopPhisIn(HEnvironment* environment, HLoopInformation* info) {
68*795d594fSAndroid Build Coastguard Worker   for (; environment != nullptr; environment = environment->GetParent()) {
69*795d594fSAndroid Build Coastguard Worker     for (size_t i = 0, e = environment->Size(); i < e; ++i) {
70*795d594fSAndroid Build Coastguard Worker       HInstruction* input = environment->GetInstructionAt(i);
71*795d594fSAndroid Build Coastguard Worker       if (input != nullptr && IsPhiOf(input, info->GetHeader())) {
72*795d594fSAndroid Build Coastguard Worker         environment->RemoveAsUserOfInput(i);
73*795d594fSAndroid Build Coastguard Worker         HInstruction* incoming = input->InputAt(0);
74*795d594fSAndroid Build Coastguard Worker         environment->SetRawEnvAt(i, incoming);
75*795d594fSAndroid Build Coastguard Worker         incoming->AddEnvUseAt(environment, i);
76*795d594fSAndroid Build Coastguard Worker       }
77*795d594fSAndroid Build Coastguard Worker     }
78*795d594fSAndroid Build Coastguard Worker   }
79*795d594fSAndroid Build Coastguard Worker }
80*795d594fSAndroid Build Coastguard Worker 
Run()81*795d594fSAndroid Build Coastguard Worker bool LICM::Run() {
82*795d594fSAndroid Build Coastguard Worker   bool didLICM = false;
83*795d594fSAndroid Build Coastguard Worker   DCHECK(side_effects_.HasRun());
84*795d594fSAndroid Build Coastguard Worker 
85*795d594fSAndroid Build Coastguard Worker   // Only used during debug.
86*795d594fSAndroid Build Coastguard Worker   ArenaBitVector* visited = nullptr;
87*795d594fSAndroid Build Coastguard Worker   if (kIsDebugBuild) {
88*795d594fSAndroid Build Coastguard Worker     visited = new (graph_->GetAllocator()) ArenaBitVector(graph_->GetAllocator(),
89*795d594fSAndroid Build Coastguard Worker                                                           graph_->GetBlocks().size(),
90*795d594fSAndroid Build Coastguard Worker                                                           false,
91*795d594fSAndroid Build Coastguard Worker                                                           kArenaAllocLICM);
92*795d594fSAndroid Build Coastguard Worker   }
93*795d594fSAndroid Build Coastguard Worker 
94*795d594fSAndroid Build Coastguard Worker   // Post order visit to visit inner loops before outer loops.
95*795d594fSAndroid Build Coastguard Worker   for (HBasicBlock* block : graph_->GetPostOrder()) {
96*795d594fSAndroid Build Coastguard Worker     if (!block->IsLoopHeader()) {
97*795d594fSAndroid Build Coastguard Worker       // Only visit the loop when we reach the header.
98*795d594fSAndroid Build Coastguard Worker       continue;
99*795d594fSAndroid Build Coastguard Worker     }
100*795d594fSAndroid Build Coastguard Worker 
101*795d594fSAndroid Build Coastguard Worker     HLoopInformation* loop_info = block->GetLoopInformation();
102*795d594fSAndroid Build Coastguard Worker     SideEffects loop_effects = side_effects_.GetLoopEffects(block);
103*795d594fSAndroid Build Coastguard Worker     HBasicBlock* pre_header = loop_info->GetPreHeader();
104*795d594fSAndroid Build Coastguard Worker 
105*795d594fSAndroid Build Coastguard Worker     for (HBlocksInLoopIterator it_loop(*loop_info); !it_loop.Done(); it_loop.Advance()) {
106*795d594fSAndroid Build Coastguard Worker       HBasicBlock* inner = it_loop.Current();
107*795d594fSAndroid Build Coastguard Worker       DCHECK(inner->IsInLoop());
108*795d594fSAndroid Build Coastguard Worker       if (inner->GetLoopInformation() != loop_info) {
109*795d594fSAndroid Build Coastguard Worker         // Thanks to post order visit, inner loops were already visited.
110*795d594fSAndroid Build Coastguard Worker         DCHECK(visited->IsBitSet(inner->GetBlockId()));
111*795d594fSAndroid Build Coastguard Worker         continue;
112*795d594fSAndroid Build Coastguard Worker       }
113*795d594fSAndroid Build Coastguard Worker       if (kIsDebugBuild) {
114*795d594fSAndroid Build Coastguard Worker         visited->SetBit(inner->GetBlockId());
115*795d594fSAndroid Build Coastguard Worker       }
116*795d594fSAndroid Build Coastguard Worker 
117*795d594fSAndroid Build Coastguard Worker       if (loop_info->ContainsIrreducibleLoop()) {
118*795d594fSAndroid Build Coastguard Worker         // We cannot licm in an irreducible loop, or in a natural loop containing an
119*795d594fSAndroid Build Coastguard Worker         // irreducible loop.
120*795d594fSAndroid Build Coastguard Worker         continue;
121*795d594fSAndroid Build Coastguard Worker       }
122*795d594fSAndroid Build Coastguard Worker       DCHECK(!loop_info->IsIrreducible());
123*795d594fSAndroid Build Coastguard Worker 
124*795d594fSAndroid Build Coastguard Worker       // We can move an instruction that can throw only as long as it is the first visible
125*795d594fSAndroid Build Coastguard Worker       // instruction (throw or write) in the loop. Note that the first potentially visible
126*795d594fSAndroid Build Coastguard Worker       // instruction that is not hoisted stops this optimization. Non-throwing instructions,
127*795d594fSAndroid Build Coastguard Worker       // on the other hand, can still be hoisted.
128*795d594fSAndroid Build Coastguard Worker       bool found_first_non_hoisted_visible_instruction_in_loop = !inner->IsLoopHeader();
129*795d594fSAndroid Build Coastguard Worker       for (HInstructionIterator inst_it(inner->GetInstructions());
130*795d594fSAndroid Build Coastguard Worker            !inst_it.Done();
131*795d594fSAndroid Build Coastguard Worker            inst_it.Advance()) {
132*795d594fSAndroid Build Coastguard Worker         HInstruction* instruction = inst_it.Current();
133*795d594fSAndroid Build Coastguard Worker         bool can_move = false;
134*795d594fSAndroid Build Coastguard Worker         if (instruction->CanBeMoved() && InputsAreDefinedBeforeLoop(instruction)) {
135*795d594fSAndroid Build Coastguard Worker           if (instruction->CanThrow()) {
136*795d594fSAndroid Build Coastguard Worker             if (!found_first_non_hoisted_visible_instruction_in_loop) {
137*795d594fSAndroid Build Coastguard Worker               DCHECK(instruction->GetBlock()->IsLoopHeader());
138*795d594fSAndroid Build Coastguard Worker               if (instruction->IsClinitCheck()) {
139*795d594fSAndroid Build Coastguard Worker                 // clinit is only done once, and since all visible instructions
140*795d594fSAndroid Build Coastguard Worker                 // in the loop header so far have been hoisted out, we can hoist
141*795d594fSAndroid Build Coastguard Worker                 // the clinit check out also.
142*795d594fSAndroid Build Coastguard Worker                 can_move = true;
143*795d594fSAndroid Build Coastguard Worker               } else if (!instruction->GetSideEffects().MayDependOn(loop_effects)) {
144*795d594fSAndroid Build Coastguard Worker                 can_move = true;
145*795d594fSAndroid Build Coastguard Worker               }
146*795d594fSAndroid Build Coastguard Worker             }
147*795d594fSAndroid Build Coastguard Worker           } else if (!instruction->GetSideEffects().MayDependOn(loop_effects)) {
148*795d594fSAndroid Build Coastguard Worker             can_move = true;
149*795d594fSAndroid Build Coastguard Worker           }
150*795d594fSAndroid Build Coastguard Worker         }
151*795d594fSAndroid Build Coastguard Worker         if (can_move) {
152*795d594fSAndroid Build Coastguard Worker           // We need to update the environment if the instruction has a loop header
153*795d594fSAndroid Build Coastguard Worker           // phi in it.
154*795d594fSAndroid Build Coastguard Worker           if (instruction->NeedsEnvironment()) {
155*795d594fSAndroid Build Coastguard Worker             UpdateLoopPhisIn(instruction->GetEnvironment(), loop_info);
156*795d594fSAndroid Build Coastguard Worker           } else {
157*795d594fSAndroid Build Coastguard Worker             DCHECK(!instruction->HasEnvironment());
158*795d594fSAndroid Build Coastguard Worker           }
159*795d594fSAndroid Build Coastguard Worker           instruction->MoveBefore(pre_header->GetLastInstruction());
160*795d594fSAndroid Build Coastguard Worker           MaybeRecordStat(stats_, MethodCompilationStat::kLoopInvariantMoved);
161*795d594fSAndroid Build Coastguard Worker           didLICM = true;
162*795d594fSAndroid Build Coastguard Worker         }
163*795d594fSAndroid Build Coastguard Worker 
164*795d594fSAndroid Build Coastguard Worker         if (!can_move && (instruction->CanThrow() || instruction->DoesAnyWrite())) {
165*795d594fSAndroid Build Coastguard Worker           // If `instruction` can do something visible (throw or write),
166*795d594fSAndroid Build Coastguard Worker           // we cannot move further instructions that can throw.
167*795d594fSAndroid Build Coastguard Worker           found_first_non_hoisted_visible_instruction_in_loop = true;
168*795d594fSAndroid Build Coastguard Worker         }
169*795d594fSAndroid Build Coastguard Worker       }
170*795d594fSAndroid Build Coastguard Worker     }
171*795d594fSAndroid Build Coastguard Worker   }
172*795d594fSAndroid Build Coastguard Worker   return didLICM;
173*795d594fSAndroid Build Coastguard Worker }
174*795d594fSAndroid Build Coastguard Worker 
175*795d594fSAndroid Build Coastguard Worker }  // namespace art
176