xref: /aosp_15_r20/art/compiler/optimizing/side_effects_analysis.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2014 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 "side_effects_analysis.h"
18*795d594fSAndroid Build Coastguard Worker 
19*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
20*795d594fSAndroid Build Coastguard Worker 
Run()21*795d594fSAndroid Build Coastguard Worker bool SideEffectsAnalysis::Run() {
22*795d594fSAndroid Build Coastguard Worker   // Inlining might have created more blocks, so we need to increase the size
23*795d594fSAndroid Build Coastguard Worker   // if needed.
24*795d594fSAndroid Build Coastguard Worker   block_effects_.resize(graph_->GetBlocks().size());
25*795d594fSAndroid Build Coastguard Worker   loop_effects_.resize(graph_->GetBlocks().size());
26*795d594fSAndroid Build Coastguard Worker 
27*795d594fSAndroid Build Coastguard Worker   // In DEBUG mode, ensure side effects are properly initialized to empty.
28*795d594fSAndroid Build Coastguard Worker   if (kIsDebugBuild) {
29*795d594fSAndroid Build Coastguard Worker     for (HBasicBlock* block : graph_->GetReversePostOrder()) {
30*795d594fSAndroid Build Coastguard Worker       SideEffects effects = GetBlockEffects(block);
31*795d594fSAndroid Build Coastguard Worker       DCHECK(effects.DoesNothing());
32*795d594fSAndroid Build Coastguard Worker       if (block->IsLoopHeader()) {
33*795d594fSAndroid Build Coastguard Worker         effects = GetLoopEffects(block);
34*795d594fSAndroid Build Coastguard Worker         DCHECK(effects.DoesNothing());
35*795d594fSAndroid Build Coastguard Worker       }
36*795d594fSAndroid Build Coastguard Worker     }
37*795d594fSAndroid Build Coastguard Worker   }
38*795d594fSAndroid Build Coastguard Worker 
39*795d594fSAndroid Build Coastguard Worker   // Do a post order visit to ensure we visit a loop header after its loop body.
40*795d594fSAndroid Build Coastguard Worker   for (HBasicBlock* block : graph_->GetPostOrder()) {
41*795d594fSAndroid Build Coastguard Worker     SideEffects effects = SideEffects::None();
42*795d594fSAndroid Build Coastguard Worker     // Update `effects` with the side effects of all instructions in this block.
43*795d594fSAndroid Build Coastguard Worker     for (HInstructionIterator inst_it(block->GetInstructions()); !inst_it.Done();
44*795d594fSAndroid Build Coastguard Worker          inst_it.Advance()) {
45*795d594fSAndroid Build Coastguard Worker       HInstruction* instruction = inst_it.Current();
46*795d594fSAndroid Build Coastguard Worker       effects = effects.Union(instruction->GetSideEffects());
47*795d594fSAndroid Build Coastguard Worker       // If all side effects are represented, scanning further will not add any
48*795d594fSAndroid Build Coastguard Worker       // more information to side-effects of this block.
49*795d594fSAndroid Build Coastguard Worker       if (effects.DoesAll()) {
50*795d594fSAndroid Build Coastguard Worker         break;
51*795d594fSAndroid Build Coastguard Worker       }
52*795d594fSAndroid Build Coastguard Worker     }
53*795d594fSAndroid Build Coastguard Worker 
54*795d594fSAndroid Build Coastguard Worker     block_effects_[block->GetBlockId()] = effects;
55*795d594fSAndroid Build Coastguard Worker 
56*795d594fSAndroid Build Coastguard Worker     if (block->IsLoopHeader()) {
57*795d594fSAndroid Build Coastguard Worker       // The side effects of the loop header are part of the loop.
58*795d594fSAndroid Build Coastguard Worker       UpdateLoopEffects(block->GetLoopInformation(), effects);
59*795d594fSAndroid Build Coastguard Worker       HBasicBlock* pre_header = block->GetLoopInformation()->GetPreHeader();
60*795d594fSAndroid Build Coastguard Worker       if (pre_header->IsInLoop()) {
61*795d594fSAndroid Build Coastguard Worker         // Update the side effects of the outer loop with the side effects of the inner loop.
62*795d594fSAndroid Build Coastguard Worker         // Note that this works because we know all the blocks of the inner loop are visited
63*795d594fSAndroid Build Coastguard Worker         // before the loop header of the outer loop.
64*795d594fSAndroid Build Coastguard Worker         UpdateLoopEffects(pre_header->GetLoopInformation(), GetLoopEffects(block));
65*795d594fSAndroid Build Coastguard Worker       }
66*795d594fSAndroid Build Coastguard Worker     } else if (block->IsInLoop()) {
67*795d594fSAndroid Build Coastguard Worker       // Update the side effects of the loop with the side effects of this block.
68*795d594fSAndroid Build Coastguard Worker       UpdateLoopEffects(block->GetLoopInformation(), effects);
69*795d594fSAndroid Build Coastguard Worker     }
70*795d594fSAndroid Build Coastguard Worker   }
71*795d594fSAndroid Build Coastguard Worker   has_run_ = true;
72*795d594fSAndroid Build Coastguard Worker   return true;
73*795d594fSAndroid Build Coastguard Worker }
74*795d594fSAndroid Build Coastguard Worker 
GetLoopEffects(HBasicBlock * block) const75*795d594fSAndroid Build Coastguard Worker SideEffects SideEffectsAnalysis::GetLoopEffects(HBasicBlock* block) const {
76*795d594fSAndroid Build Coastguard Worker   DCHECK(block->IsLoopHeader());
77*795d594fSAndroid Build Coastguard Worker   return loop_effects_[block->GetBlockId()];
78*795d594fSAndroid Build Coastguard Worker }
79*795d594fSAndroid Build Coastguard Worker 
GetBlockEffects(HBasicBlock * block) const80*795d594fSAndroid Build Coastguard Worker SideEffects SideEffectsAnalysis::GetBlockEffects(HBasicBlock* block) const {
81*795d594fSAndroid Build Coastguard Worker   return block_effects_[block->GetBlockId()];
82*795d594fSAndroid Build Coastguard Worker }
83*795d594fSAndroid Build Coastguard Worker 
UpdateLoopEffects(HLoopInformation * info,SideEffects effects)84*795d594fSAndroid Build Coastguard Worker void SideEffectsAnalysis::UpdateLoopEffects(HLoopInformation* info, SideEffects effects) {
85*795d594fSAndroid Build Coastguard Worker   uint32_t id = info->GetHeader()->GetBlockId();
86*795d594fSAndroid Build Coastguard Worker   loop_effects_[id] = loop_effects_[id].Union(effects);
87*795d594fSAndroid Build Coastguard Worker }
88*795d594fSAndroid Build Coastguard Worker 
89*795d594fSAndroid Build Coastguard Worker }  // namespace art
90