blob: 6d82e8e06d0ea61084d0be2b70175084d5fda724 [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.
Vladimir Marko2aaa4b52015-09-17 17:03:26 +010024 block_effects_.resize(graph_->GetBlocks().size());
25 loop_effects_.resize(graph_->GetBlocks().size());
Nicolas Geoffray276d9da2015-02-02 18:24:11 +000026
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) {
Vladimir Marko2c45bc92016-10-25 16:54:12 +010029 for (HBasicBlock* block : graph_->GetReversePostOrder()) {
Nicolas Geoffray827eedb2015-01-26 15:18:36 +000030 SideEffects effects = GetBlockEffects(block);
Aart Bik854a02b2015-07-14 16:07:00 -070031 DCHECK(effects.DoesNothing());
Nicolas Geoffray827eedb2015-01-26 15:18:36 +000032 if (block->IsLoopHeader()) {
33 effects = GetLoopEffects(block);
Aart Bik854a02b2015-07-14 16:07:00 -070034 DCHECK(effects.DoesNothing());
Nicolas Geoffray827eedb2015-01-26 15:18:36 +000035 }
36 }
37 }
38
39 // Do a post order visit to ensure we visit a loop header after its loop body.
Vladimir Marko2c45bc92016-10-25 16:54:12 +010040 for (HBasicBlock* block : graph_->GetPostOrder()) {
Nicolas Geoffray827eedb2015-01-26 15:18:36 +000041 SideEffects effects = SideEffects::None();
42 // Update `effects` with the side effects of all instructions in this block.
43 for (HInstructionIterator inst_it(block->GetInstructions()); !inst_it.Done();
44 inst_it.Advance()) {
45 HInstruction* instruction = inst_it.Current();
46 effects = effects.Union(instruction->GetSideEffects());
Alexandre Rames78e3ef62015-08-12 13:43:29 +010047 // If all side effects are represented, scanning further will not add any
48 // more information to side-effects of this block.
Aart Bik854a02b2015-07-14 16:07:00 -070049 if (effects.DoesAll()) {
Nicolas Geoffray827eedb2015-01-26 15:18:36 +000050 break;
51 }
52 }
53
Vladimir Marko2aaa4b52015-09-17 17:03:26 +010054 block_effects_[block->GetBlockId()] = effects;
Nicolas Geoffray827eedb2015-01-26 15:18:36 +000055
56 if (block->IsLoopHeader()) {
57 // The side effects of the loop header are part of the loop.
58 UpdateLoopEffects(block->GetLoopInformation(), effects);
59 HBasicBlock* pre_header = block->GetLoopInformation()->GetPreHeader();
60 if (pre_header->IsInLoop()) {
61 // Update the side effects of the outer loop with the side effects of the inner loop.
62 // Note that this works because we know all the blocks of the inner loop are visited
63 // before the loop header of the outer loop.
64 UpdateLoopEffects(pre_header->GetLoopInformation(), GetLoopEffects(block));
65 }
66 } else if (block->IsInLoop()) {
67 // Update the side effects of the loop with the side effects of this block.
68 UpdateLoopEffects(block->GetLoopInformation(), effects);
69 }
70 }
71 has_run_ = true;
72}
73
74SideEffects SideEffectsAnalysis::GetLoopEffects(HBasicBlock* block) const {
75 DCHECK(block->IsLoopHeader());
Vladimir Marko2aaa4b52015-09-17 17:03:26 +010076 return loop_effects_[block->GetBlockId()];
Nicolas Geoffray827eedb2015-01-26 15:18:36 +000077}
78
79SideEffects SideEffectsAnalysis::GetBlockEffects(HBasicBlock* block) const {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +010080 return block_effects_[block->GetBlockId()];
Nicolas Geoffray827eedb2015-01-26 15:18:36 +000081}
82
83void SideEffectsAnalysis::UpdateLoopEffects(HLoopInformation* info, SideEffects effects) {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +010084 uint32_t id = info->GetHeader()->GetBlockId();
Vladimir Marko2aaa4b52015-09-17 17:03:26 +010085 loop_effects_[id] = loop_effects_[id].Union(effects);
Nicolas Geoffray827eedb2015-01-26 15:18:36 +000086}
87
88} // namespace art