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  // 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
27  if (kIsDebugBuild) {
28    for (HReversePostOrderIterator it(*graph_); !it.Done(); it.Advance()) {
29      HBasicBlock* block = it.Current();
30      SideEffects effects = GetBlockEffects(block);
31      DCHECK(!effects.HasSideEffects() && !effects.HasDependencies());
32      if (block->IsLoopHeader()) {
33        effects = GetLoopEffects(block);
34        DCHECK(!effects.HasSideEffects() && !effects.HasDependencies());
35      }
36    }
37  }
38
39  // Do a post order visit to ensure we visit a loop header after its loop body.
40  for (HPostOrderIterator it(*graph_); !it.Done(); it.Advance()) {
41    HBasicBlock* block = it.Current();
42
43    SideEffects effects = SideEffects::None();
44    // Update `effects` with the side effects of all instructions in this block.
45    for (HInstructionIterator inst_it(block->GetInstructions()); !inst_it.Done();
46         inst_it.Advance()) {
47      HInstruction* instruction = inst_it.Current();
48      effects = effects.Union(instruction->GetSideEffects());
49      if (effects.HasAllSideEffects()) {
50        break;
51      }
52    }
53
54    block_effects_.Put(block->GetBlockId(), effects);
55
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());
76  return loop_effects_.Get(block->GetBlockId());
77}
78
79SideEffects SideEffectsAnalysis::GetBlockEffects(HBasicBlock* block) const {
80  return block_effects_.Get(block->GetBlockId());
81}
82
83void SideEffectsAnalysis::UpdateLoopEffects(HLoopInformation* info, SideEffects effects) {
84  int id = info->GetHeader()->GetBlockId();
85  loop_effects_.Put(id, loop_effects_.Get(id).Union(effects));
86}
87
88}  // namespace art
89