blob: 027b3d4ff3d2586db7bcd48ed3525ae4acd94068 [file] [log] [blame]
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001/*
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 "gvn.h"
18
19namespace art {
20
21void GlobalValueNumberer::Run() {
22 ComputeSideEffects();
23
24 sets_.Put(graph_->GetEntryBlock()->GetBlockId(), new (allocator_) ValueSet(allocator_));
25
26 // Do reverse post order to ensure the non back-edge predecessors of a block are
27 // visited before the block itself.
28 for (HReversePostOrderIterator it(*graph_); !it.Done(); it.Advance()) {
29 VisitBasicBlock(it.Current());
30 }
31}
32
33void GlobalValueNumberer::UpdateLoopEffects(HLoopInformation* info, SideEffects effects) {
34 int id = info->GetHeader()->GetBlockId();
35 loop_effects_.Put(id, loop_effects_.Get(id).Union(effects));
36}
37
38void GlobalValueNumberer::ComputeSideEffects() {
39 if (kIsDebugBuild) {
40 for (HReversePostOrderIterator it(*graph_); !it.Done(); it.Advance()) {
41 HBasicBlock* block = it.Current();
42 SideEffects effects = GetBlockEffects(block);
43 DCHECK(!effects.HasSideEffects() && !effects.HasDependencies());
44 if (block->IsLoopHeader()) {
45 effects = GetLoopEffects(block);
46 DCHECK(!effects.HasSideEffects() && !effects.HasDependencies());
47 }
48 }
49 }
50
51 // Do a post order visit to ensure we visit a loop header after its loop body.
52 for (HPostOrderIterator it(*graph_); !it.Done(); it.Advance()) {
53 HBasicBlock* block = it.Current();
54
55 SideEffects effects = SideEffects::None();
56 // Update `effects` with the side effects of all instructions in this block.
57 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
58 HInstruction* instruction = it.Current();
59 effects = effects.Union(instruction->GetSideEffects());
60 if (effects.HasAllSideEffects()) {
61 break;
62 }
63 }
64
65 block_effects_.Put(block->GetBlockId(), effects);
66
67 if (block->IsLoopHeader()) {
68 // The side effects of the loop header are part of the loop.
69 UpdateLoopEffects(block->GetLoopInformation(), effects);
70 HBasicBlock* pre_header = block->GetLoopInformation()->GetPreHeader();
71 if (pre_header->IsInLoop()) {
72 // Update the side effects of the outer loop with the side effects of the inner loop.
73 // Note that this works because we know all the blocks of the inner loop are visited
74 // before the loop header of the outer loop.
75 UpdateLoopEffects(pre_header->GetLoopInformation(), GetLoopEffects(block));
76 }
77 } else if (block->IsInLoop()) {
78 // Update the side effects of the loop with the side effects of this block.
79 UpdateLoopEffects(block->GetLoopInformation(), effects);
80 }
81 }
82}
83
84SideEffects GlobalValueNumberer::GetLoopEffects(HBasicBlock* block) const {
85 DCHECK(block->IsLoopHeader());
86 return loop_effects_.Get(block->GetBlockId());
87}
88
89SideEffects GlobalValueNumberer::GetBlockEffects(HBasicBlock* block) const {
90 return block_effects_.Get(block->GetBlockId());
91}
92
93static bool IsLoopExit(HBasicBlock* block, HBasicBlock* successor) {
94 HLoopInformation* block_info = block->GetLoopInformation();
95 HLoopInformation* other_info = successor->GetLoopInformation();
96 return block_info != other_info && (other_info == nullptr || block_info->IsIn(*other_info));
97}
98
99void GlobalValueNumberer::VisitBasicBlock(HBasicBlock* block) {
100 if (kIsDebugBuild) {
101 // Check that all non back-edge processors have been visited.
102 for (size_t i = 0, e = block->GetPredecessors().Size(); i < e; ++i) {
103 HBasicBlock* predecessor = block->GetPredecessors().Get(i);
104 DCHECK(visited_.Get(predecessor->GetBlockId())
105 || (block->GetLoopInformation() != nullptr
106 && (block->GetLoopInformation()->GetBackEdges().Get(0) == predecessor)));
107 }
108 visited_.Put(block->GetBlockId(), true);
109 }
110
111 ValueSet* set = sets_.Get(block->GetBlockId());
112
113 if (block->IsLoopHeader()) {
114 set->Kill(GetLoopEffects(block));
115 }
116
117 HInstruction* current = block->GetFirstInstruction();
118 while (current != nullptr) {
119 set->Kill(current->GetSideEffects());
120 // Save the next instruction in case `current` is removed from the graph.
121 HInstruction* next = current->GetNext();
122 if (current->CanBeMoved()) {
123 HInstruction* existing = set->Lookup(current);
124 if (existing != nullptr) {
125 current->ReplaceWith(existing);
126 current->GetBlock()->RemoveInstruction(current);
127 } else {
128 set->Add(current);
129 }
130 }
131 current = next;
132 }
133
134 if (block == graph_->GetEntryBlock()) {
135 // The entry block should only accumulate constant instructions, and
136 // the builder puts constants only in the entry block.
137 // Therefore, there is no need to propagate the value set to the next block.
138 DCHECK_EQ(block->GetDominatedBlocks().Size(), 1u);
139 HBasicBlock* dominated = block->GetDominatedBlocks().Get(0);
140 sets_.Put(dominated->GetBlockId(), new (allocator_) ValueSet(allocator_));
141 return;
142 }
143
144 // Copy the value set to dominated blocks. We can re-use
145 // the current set for the last dominated block because we are done visiting
146 // this block.
147 for (size_t i = 0, e = block->GetDominatedBlocks().Size(); i < e; ++i) {
148 HBasicBlock* dominated = block->GetDominatedBlocks().Get(i);
149 sets_.Put(dominated->GetBlockId(), i == e - 1 ? set : set->Copy());
150 }
151
152 // Kill instructions in the value set of each successor. If the successor
153 // is a loop exit, then we use the side effects of the loop. If not, we use
154 // the side effects of this block.
155 for (size_t i = 0, e = block->GetSuccessors().Size(); i < e; ++i) {
156 HBasicBlock* successor = block->GetSuccessors().Get(i);
157 if (successor->IsLoopHeader()
158 && successor->GetLoopInformation()->GetBackEdges().Get(0) == block) {
159 // In case of a back edge, we already have visited the loop header.
160 // We should not update its value set, because the last dominated block
161 // of the loop header uses the same value set.
162 DCHECK(visited_.Get(successor->GetBlockId()));
163 continue;
164 }
165 DCHECK(!visited_.Get(successor->GetBlockId()));
166 ValueSet* successor_set = sets_.Get(successor->GetBlockId());
167 // The dominator sets the set, and we are guaranteed to have visited it already.
168 DCHECK(successor_set != nullptr);
169
170 // If this block dominates this successor there is nothing to do.
171 // Also if the set is empty, there is nothing to kill.
172 if (successor->GetDominator() != block && !successor_set->IsEmpty()) {
173 if (block->IsInLoop() && IsLoopExit(block, successor)) {
174 // All instructions killed in the loop must be killed for a loop exit.
175 SideEffects effects = GetLoopEffects(block->GetLoopInformation()->GetHeader());
176 sets_.Get(successor->GetBlockId())->Kill(effects);
177 } else {
178 // Following block (that might be in the same loop).
179 // Just kill instructions based on this block's side effects.
180 sets_.Get(successor->GetBlockId())->Kill(GetBlockEffects(block));
181 }
182 }
183 }
184}
185
186} // namespace art