side_effects_analysis.cc revision 827eedbfa882496407375f22b08243a38a5bd53b
1/*
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() {
22  if (kIsDebugBuild) {
23    for (HReversePostOrderIterator it(*graph_); !it.Done(); it.Advance()) {
24      HBasicBlock* block = it.Current();
25      SideEffects effects = GetBlockEffects(block);
26      DCHECK(!effects.HasSideEffects() && !effects.HasDependencies());
27      if (block->IsLoopHeader()) {
28        effects = GetLoopEffects(block);
29        DCHECK(!effects.HasSideEffects() && !effects.HasDependencies());
30      }
31    }
32  }
33
34  // Do a post order visit to ensure we visit a loop header after its loop body.
35  for (HPostOrderIterator it(*graph_); !it.Done(); it.Advance()) {
36    HBasicBlock* block = it.Current();
37
38    SideEffects effects = SideEffects::None();
39    // Update `effects` with the side effects of all instructions in this block.
40    for (HInstructionIterator inst_it(block->GetInstructions()); !inst_it.Done();
41         inst_it.Advance()) {
42      HInstruction* instruction = inst_it.Current();
43      effects = effects.Union(instruction->GetSideEffects());
44      if (effects.HasAllSideEffects()) {
45        break;
46      }
47    }
48
49    block_effects_.Put(block->GetBlockId(), effects);
50
51    if (block->IsLoopHeader()) {
52      // The side effects of the loop header are part of the loop.
53      UpdateLoopEffects(block->GetLoopInformation(), effects);
54      HBasicBlock* pre_header = block->GetLoopInformation()->GetPreHeader();
55      if (pre_header->IsInLoop()) {
56        // Update the side effects of the outer loop with the side effects of the inner loop.
57        // Note that this works because we know all the blocks of the inner loop are visited
58        // before the loop header of the outer loop.
59        UpdateLoopEffects(pre_header->GetLoopInformation(), GetLoopEffects(block));
60      }
61    } else if (block->IsInLoop()) {
62      // Update the side effects of the loop with the side effects of this block.
63      UpdateLoopEffects(block->GetLoopInformation(), effects);
64    }
65  }
66  has_run_ = true;
67}
68
69SideEffects SideEffectsAnalysis::GetLoopEffects(HBasicBlock* block) const {
70  DCHECK(block->IsLoopHeader());
71  return loop_effects_.Get(block->GetBlockId());
72}
73
74SideEffects SideEffectsAnalysis::GetBlockEffects(HBasicBlock* block) const {
75  return block_effects_.Get(block->GetBlockId());
76}
77
78void SideEffectsAnalysis::UpdateLoopEffects(HLoopInformation* info, SideEffects effects) {
79  int id = info->GetHeader()->GetBlockId();
80  loop_effects_.Put(id, loop_effects_.Get(id).Union(effects));
81}
82
83}  // namespace art
84