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