blob: 9dbf63844286db08bf294ab11308ad339756964b [file] [log] [blame]
Nicolas Geoffray827eedb2015-01-26 15:18:36 +00001/*
2 * Copyright (C) 2014 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 "side_effects_analysis.h"
18
19namespace art {
20
21void SideEffectsAnalysis::Run() {
Nicolas Geoffray276d9da2015-02-02 18:24:11 +000022 // Inlining might have created more blocks, so we need to increase the size
23 // if needed.
24 block_effects_.SetSize(graph_->GetBlocks().Size());
25 loop_effects_.SetSize(graph_->GetBlocks().Size());
26
Aart Bik854a02b2015-07-14 16:07:00 -070027 // In DEBUG mode, ensure side effects are properly initialized to empty.
Nicolas Geoffray827eedb2015-01-26 15:18:36 +000028 if (kIsDebugBuild) {
29 for (HReversePostOrderIterator it(*graph_); !it.Done(); it.Advance()) {
30 HBasicBlock* block = it.Current();
31 SideEffects effects = GetBlockEffects(block);
Aart Bik854a02b2015-07-14 16:07:00 -070032 DCHECK(effects.DoesNothing());
Nicolas Geoffray827eedb2015-01-26 15:18:36 +000033 if (block->IsLoopHeader()) {
34 effects = GetLoopEffects(block);
Aart Bik854a02b2015-07-14 16:07:00 -070035 DCHECK(effects.DoesNothing());
Nicolas Geoffray827eedb2015-01-26 15:18:36 +000036 }
37 }
38 }
39
40 // Do a post order visit to ensure we visit a loop header after its loop body.
41 for (HPostOrderIterator it(*graph_); !it.Done(); it.Advance()) {
42 HBasicBlock* block = it.Current();
43
44 SideEffects effects = SideEffects::None();
45 // Update `effects` with the side effects of all instructions in this block.
46 for (HInstructionIterator inst_it(block->GetInstructions()); !inst_it.Done();
47 inst_it.Advance()) {
48 HInstruction* instruction = inst_it.Current();
49 effects = effects.Union(instruction->GetSideEffects());
Aart Bik854a02b2015-07-14 16:07:00 -070050 // If every possible write/read is represented, scanning further
51 // will not add any more information to side-effects of this block.
52 if (effects.DoesAll()) {
Nicolas Geoffray827eedb2015-01-26 15:18:36 +000053 break;
54 }
55 }
56
57 block_effects_.Put(block->GetBlockId(), effects);
58
59 if (block->IsLoopHeader()) {
60 // The side effects of the loop header are part of the loop.
61 UpdateLoopEffects(block->GetLoopInformation(), effects);
62 HBasicBlock* pre_header = block->GetLoopInformation()->GetPreHeader();
63 if (pre_header->IsInLoop()) {
64 // Update the side effects of the outer loop with the side effects of the inner loop.
65 // Note that this works because we know all the blocks of the inner loop are visited
66 // before the loop header of the outer loop.
67 UpdateLoopEffects(pre_header->GetLoopInformation(), GetLoopEffects(block));
68 }
69 } else if (block->IsInLoop()) {
70 // Update the side effects of the loop with the side effects of this block.
71 UpdateLoopEffects(block->GetLoopInformation(), effects);
72 }
73 }
74 has_run_ = true;
75}
76
77SideEffects SideEffectsAnalysis::GetLoopEffects(HBasicBlock* block) const {
78 DCHECK(block->IsLoopHeader());
79 return loop_effects_.Get(block->GetBlockId());
80}
81
82SideEffects SideEffectsAnalysis::GetBlockEffects(HBasicBlock* block) const {
83 return block_effects_.Get(block->GetBlockId());
84}
85
86void SideEffectsAnalysis::UpdateLoopEffects(HLoopInformation* info, SideEffects effects) {
87 int id = info->GetHeader()->GetBlockId();
88 loop_effects_.Put(id, loop_effects_.Get(id).Union(effects));
89}
90
91} // namespace art