blob: 3d6aeb730010cad87f5407045d44fd308bce95d3 [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +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 "nodes.h"
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010018#include "ssa_builder.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000019#include "utils/growable_array.h"
20
21namespace art {
22
23void HGraph::AddBlock(HBasicBlock* block) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +000024 block->SetBlockId(blocks_.Size());
Nicolas Geoffray818f2102014-02-18 16:43:35 +000025 blocks_.Add(block);
26}
27
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000028void HGraph::FindBackEdges(ArenaBitVector* visited) const {
29 ArenaBitVector visiting(arena_, blocks_.Size(), false);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000030 VisitBlockForBackEdges(entry_block_, visited, &visiting);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000031}
32
33void HGraph::RemoveDeadBlocks(const ArenaBitVector& visited) const {
34 for (size_t i = 0; i < blocks_.Size(); i++) {
35 if (!visited.IsBitSet(i)) {
36 HBasicBlock* block = blocks_.Get(i);
Nicolas Geoffray787c3072014-03-17 10:20:19 +000037 for (size_t j = 0; j < block->GetSuccessors()->Size(); j++) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010038 block->GetSuccessors()->Get(j)->RemovePredecessor(block, false);
39 }
40 for (HInstructionIterator it(*block->GetPhis()); !it.Done(); it.Advance()) {
41 block->RemovePhi(it.Current()->AsPhi());
42 }
43 for (HInstructionIterator it(*block->GetInstructions()); !it.Done(); it.Advance()) {
44 block->RemoveInstruction(it.Current());
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000045 }
46 }
47 }
48}
49
50void HGraph::VisitBlockForBackEdges(HBasicBlock* block,
51 ArenaBitVector* visited,
52 ArenaBitVector* visiting) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +000053 int id = block->GetBlockId();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000054 if (visited->IsBitSet(id)) return;
55
56 visited->SetBit(id);
57 visiting->SetBit(id);
Nicolas Geoffray787c3072014-03-17 10:20:19 +000058 for (size_t i = 0; i < block->GetSuccessors()->Size(); i++) {
59 HBasicBlock* successor = block->GetSuccessors()->Get(i);
60 if (visiting->IsBitSet(successor->GetBlockId())) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000061 successor->AddBackEdge(block);
62 } else {
63 VisitBlockForBackEdges(successor, visited, visiting);
64 }
65 }
66 visiting->ClearBit(id);
67}
68
69void HGraph::BuildDominatorTree() {
70 ArenaBitVector visited(arena_, blocks_.Size(), false);
71
72 // (1) Find the back edges in the graph doing a DFS traversal.
73 FindBackEdges(&visited);
74
75 // (2) Remove blocks not visited during the initial DFS.
76 // Step (3) requires dead blocks to be removed from the
77 // predecessors list of live blocks.
78 RemoveDeadBlocks(visited);
79
80 // (3) Compute the immediate dominator of each block. We visit
81 // the successors of a block only when all its forward branches
82 // have been processed.
83 GrowableArray<size_t> visits(arena_, blocks_.Size());
84 visits.SetSize(blocks_.Size());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000085 dominator_order_.Add(entry_block_);
Nicolas Geoffray787c3072014-03-17 10:20:19 +000086 for (size_t i = 0; i < entry_block_->GetSuccessors()->Size(); i++) {
87 VisitBlockForDominatorTree(entry_block_->GetSuccessors()->Get(i), entry_block_, &visits);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000088 }
89}
90
91HBasicBlock* HGraph::FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const {
92 ArenaBitVector visited(arena_, blocks_.Size(), false);
93 // Walk the dominator tree of the first block and mark the visited blocks.
94 while (first != nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +000095 visited.SetBit(first->GetBlockId());
96 first = first->GetDominator();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000097 }
98 // Walk the dominator tree of the second block until a marked block is found.
99 while (second != nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000100 if (visited.IsBitSet(second->GetBlockId())) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000101 return second;
102 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000103 second = second->GetDominator();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000104 }
105 LOG(ERROR) << "Could not find common dominator";
106 return nullptr;
107}
108
109void HGraph::VisitBlockForDominatorTree(HBasicBlock* block,
110 HBasicBlock* predecessor,
111 GrowableArray<size_t>* visits) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000112 if (block->GetDominator() == nullptr) {
113 block->SetDominator(predecessor);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000114 } else {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000115 block->SetDominator(FindCommonDominator(block->GetDominator(), predecessor));
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000116 }
117
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000118 visits->Increment(block->GetBlockId());
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000119 // Once all the forward edges have been visited, we know the immediate
120 // dominator of the block. We can then start visiting its successors.
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000121 if (visits->Get(block->GetBlockId()) ==
122 block->GetPredecessors()->Size() - block->NumberOfBackEdges()) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000123 dominator_order_.Add(block);
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000124 for (size_t i = 0; i < block->GetSuccessors()->Size(); i++) {
125 VisitBlockForDominatorTree(block->GetSuccessors()->Get(i), block, visits);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000126 }
127 }
128}
129
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100130void HGraph::TransformToSSA() {
131 DCHECK(!dominator_order_.IsEmpty());
132 SimplifyCFG();
133 SsaBuilder ssa_builder(this);
134 ssa_builder.BuildSsa();
135}
136
137void HGraph::SimplifyCFG() {
138 for (size_t i = 0; i < dominator_order_.Size(); i++) {
139 HBasicBlock* current = dominator_order_.Get(i);
140 if (current->IsLoopHeader()) {
141 // Make sure the loop has only one pre header. This simplifies SSA building by having
142 // to just look at the pre header to know which locals are initialized at entry of the
143 // loop.
144 HLoopInformation* info = current->GetLoopInformation();
145 size_t number_of_incomings = current->GetPredecessors()->Size() - info->NumberOfBackEdges();
146 if (number_of_incomings != 1) {
147 HBasicBlock* pre_header = new (arena_) HBasicBlock(this);
148 AddBlock(pre_header);
149 pre_header->AddInstruction(new (arena_) HGoto());
150 pre_header->SetDominator(current->GetDominator());
151 current->SetDominator(pre_header);
152 dominator_order_.InsertAt(i, pre_header);
153 i++;
154
155 ArenaBitVector back_edges(arena_, GetBlocks()->Size(), false);
156 for (size_t pred = 0; pred < info->GetBackEdges()->Size(); pred++) {
157 back_edges.SetBit(info->GetBackEdges()->Get(pred)->GetBlockId());
158 }
159 for (size_t pred = 0; pred < current->GetPredecessors()->Size(); pred++) {
160 HBasicBlock* predecessor = current->GetPredecessors()->Get(pred);
161 if (!back_edges.IsBitSet(predecessor->GetBlockId())) {
162 current->RemovePredecessor(predecessor);
163 pred--;
164 predecessor->AddSuccessor(pre_header);
165 }
166 }
167 pre_header->AddSuccessor(current);
168 }
169 info->SetPreHeader(current->GetDominator());
170 }
171 }
172}
173
174void HLoopInformation::SetPreHeader(HBasicBlock* block) {
175 DCHECK_EQ(header_->GetDominator(), block);
176 pre_header_ = block;
177}
178
179static void Add(HInstructionList* instruction_list,
180 HBasicBlock* block,
181 HInstruction* instruction) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000182 DCHECK(instruction->GetBlock() == nullptr);
Nicolas Geoffray43c86422014-03-18 11:58:24 +0000183 DCHECK_EQ(instruction->GetId(), -1);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100184 instruction->SetBlock(block);
185 instruction->SetId(block->GetGraph()->GetNextInstructionId());
186 instruction_list->AddInstruction(instruction);
187}
188
189void HBasicBlock::AddInstruction(HInstruction* instruction) {
190 Add(&instructions_, this, instruction);
191}
192
193void HBasicBlock::AddPhi(HPhi* phi) {
194 Add(&phis_, this, phi);
195}
196
197static void Remove(HInstructionList* instruction_list,
198 HBasicBlock* block,
199 HInstruction* instruction) {
200 DCHECK_EQ(block, instruction->GetBlock());
201 DCHECK(instruction->GetUses() == nullptr);
202 DCHECK(instruction->GetEnvUses() == nullptr);
203 instruction->SetBlock(nullptr);
204 instruction_list->RemoveInstruction(instruction);
205
206 for (size_t i = 0; i < instruction->InputCount(); i++) {
207 instruction->InputAt(i)->RemoveUser(instruction, i);
208 }
209}
210
211void HBasicBlock::RemoveInstruction(HInstruction* instruction) {
212 Remove(&instructions_, this, instruction);
213}
214
215void HBasicBlock::RemovePhi(HPhi* phi) {
216 Remove(&phis_, this, phi);
217}
218
219void HInstruction::RemoveUser(HInstruction* user, size_t input_index) {
220 HUseListNode<HInstruction>* previous = nullptr;
221 HUseListNode<HInstruction>* current = uses_;
222 while (current != nullptr) {
223 if (current->GetUser() == user && current->GetIndex() == input_index) {
224 if (previous == NULL) {
225 uses_ = current->GetTail();
226 } else {
227 previous->SetTail(current->GetTail());
228 }
229 }
230 previous = current;
231 current = current->GetTail();
232 }
233}
234
235void HInstructionList::AddInstruction(HInstruction* instruction) {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000236 if (first_instruction_ == nullptr) {
237 DCHECK(last_instruction_ == nullptr);
238 first_instruction_ = last_instruction_ = instruction;
239 } else {
240 last_instruction_->next_ = instruction;
241 instruction->previous_ = last_instruction_;
242 last_instruction_ = instruction;
243 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100244 for (size_t i = 0; i < instruction->InputCount(); i++) {
245 instruction->InputAt(i)->AddUseAt(instruction, i);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000246 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000247}
248
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100249void HInstructionList::RemoveInstruction(HInstruction* instruction) {
250 if (instruction->previous_ != nullptr) {
251 instruction->previous_->next_ = instruction->next_;
252 }
253 if (instruction->next_ != nullptr) {
254 instruction->next_->previous_ = instruction->previous_;
255 }
256 if (instruction == first_instruction_) {
257 first_instruction_ = instruction->next_;
258 }
259 if (instruction == last_instruction_) {
260 last_instruction_ = instruction->previous_;
261 }
262}
263
264void HInstruction::ReplaceWith(HInstruction* other) {
265 for (HUseIterator<HInstruction> it(GetUses()); !it.Done(); it.Advance()) {
266 HUseListNode<HInstruction>* current = it.Current();
267 HInstruction* user = current->GetUser();
268 size_t input_index = current->GetIndex();
269 user->SetRawInputAt(input_index, other);
270 other->AddUseAt(user, input_index);
271 }
272
273 for (HUseIterator<HEnvironment> it(GetEnvUses()); !it.Done(); it.Advance()) {
274 HUseListNode<HEnvironment>* current = it.Current();
275 HEnvironment* user = current->GetUser();
276 size_t input_index = current->GetIndex();
277 user->SetRawEnvAt(input_index, other);
278 other->AddEnvUseAt(user, input_index);
279 }
280
281 uses_ = nullptr;
282 env_uses_ = nullptr;
283}
284
285void HPhi::AddInput(HInstruction* input) {
286 DCHECK(input->GetBlock() != nullptr);
287 inputs_.Add(input);
288 input->AddUseAt(this, inputs_.Size() - 1);
289}
290
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000291#define DEFINE_ACCEPT(name) \
292void H##name::Accept(HGraphVisitor* visitor) { \
293 visitor->Visit##name(this); \
294}
295
296FOR_EACH_INSTRUCTION(DEFINE_ACCEPT)
297
298#undef DEFINE_ACCEPT
299
300void HGraphVisitor::VisitInsertionOrder() {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000301 const GrowableArray<HBasicBlock*>* blocks = graph_->GetBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000302 for (size_t i = 0 ; i < blocks->Size(); i++) {
303 VisitBasicBlock(blocks->Get(i));
304 }
305}
306
307void HGraphVisitor::VisitBasicBlock(HBasicBlock* block) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100308 for (HInstructionIterator it(*block->GetPhis()); !it.Done(); it.Advance()) {
309 it.Current()->Accept(this);
310 }
311 for (HInstructionIterator it(*block->GetInstructions()); !it.Done(); it.Advance()) {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000312 it.Current()->Accept(this);
313 }
314}
315
316} // namespace art