blob: 752466b0b371ae62a87ff44d8ad7f593c545596f [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 Geoffray804d0932014-05-02 08:46:00 +010028void HGraph::FindBackEdges(ArenaBitVector* visited) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000029 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 {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010034 for (size_t i = 0; i < blocks_.Size(); ++i) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000035 if (!visited.IsBitSet(i)) {
36 HBasicBlock* block = blocks_.Get(i);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010037 for (size_t j = 0; j < block->GetSuccessors().Size(); ++j) {
38 block->GetSuccessors().Get(j)->RemovePredecessor(block, false);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010039 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010040 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010041 block->RemovePhi(it.Current()->AsPhi());
42 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010043 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010044 block->RemoveInstruction(it.Current());
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000045 }
46 }
47 }
48}
49
50void HGraph::VisitBlockForBackEdges(HBasicBlock* block,
51 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +010052 ArenaBitVector* visiting) {
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 Geoffray622d9c32014-05-12 16:11:02 +010058 for (size_t i = 0; i < block->GetSuccessors().Size(); i++) {
59 HBasicBlock* successor = block->GetSuccessors().Get(i);
Nicolas Geoffray787c3072014-03-17 10:20:19 +000060 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
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010080 // (3) Simplify the CFG now, so that we don't need to recompute
81 // dominators and the reverse post order.
82 SimplifyCFG();
83
84 // (4) Compute the immediate dominator of each block. We visit
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000085 // the successors of a block only when all its forward branches
86 // have been processed.
87 GrowableArray<size_t> visits(arena_, blocks_.Size());
88 visits.SetSize(blocks_.Size());
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010089 reverse_post_order_.Add(entry_block_);
90 for (size_t i = 0; i < entry_block_->GetSuccessors().Size(); i++) {
91 VisitBlockForDominatorTree(entry_block_->GetSuccessors().Get(i), entry_block_, &visits);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000092 }
93}
94
95HBasicBlock* HGraph::FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const {
96 ArenaBitVector visited(arena_, blocks_.Size(), false);
97 // Walk the dominator tree of the first block and mark the visited blocks.
98 while (first != nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +000099 visited.SetBit(first->GetBlockId());
100 first = first->GetDominator();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000101 }
102 // Walk the dominator tree of the second block until a marked block is found.
103 while (second != nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000104 if (visited.IsBitSet(second->GetBlockId())) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000105 return second;
106 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000107 second = second->GetDominator();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000108 }
109 LOG(ERROR) << "Could not find common dominator";
110 return nullptr;
111}
112
113void HGraph::VisitBlockForDominatorTree(HBasicBlock* block,
114 HBasicBlock* predecessor,
115 GrowableArray<size_t>* visits) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000116 if (block->GetDominator() == nullptr) {
117 block->SetDominator(predecessor);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000118 } else {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000119 block->SetDominator(FindCommonDominator(block->GetDominator(), predecessor));
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000120 }
121
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000122 visits->Increment(block->GetBlockId());
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000123 // Once all the forward edges have been visited, we know the immediate
124 // dominator of the block. We can then start visiting its successors.
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000125 if (visits->Get(block->GetBlockId()) ==
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100126 block->GetPredecessors().Size() - block->NumberOfBackEdges()) {
127 reverse_post_order_.Add(block);
128 for (size_t i = 0; i < block->GetSuccessors().Size(); i++) {
129 VisitBlockForDominatorTree(block->GetSuccessors().Get(i), block, visits);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000130 }
131 }
132}
133
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100134void HGraph::TransformToSSA() {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100135 DCHECK(!reverse_post_order_.IsEmpty());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100136 SsaBuilder ssa_builder(this);
137 ssa_builder.BuildSsa();
138}
139
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100140void HGraph::SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor) {
141 // Insert a new node between `block` and `successor` to split the
142 // critical edge.
143 HBasicBlock* new_block = new (arena_) HBasicBlock(this);
144 AddBlock(new_block);
145 new_block->AddInstruction(new (arena_) HGoto());
146 block->RemoveSuccessor(successor);
147 block->AddSuccessor(new_block);
148 new_block->AddSuccessor(successor);
149 if (successor->IsLoopHeader()) {
150 // If we split at a back edge boundary, make the new block the back edge.
151 HLoopInformation* info = successor->GetLoopInformation();
152 if (info->IsBackEdge(block)) {
153 info->RemoveBackEdge(block);
154 info->AddBackEdge(new_block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100155 }
156 }
157}
158
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100159void HGraph::SimplifyLoop(HBasicBlock* header) {
160 HLoopInformation* info = header->GetLoopInformation();
161
162 // If there are more than one back edge, make them branch to the same block that
163 // will become the only back edge. This simplifies finding natural loops in the
164 // graph.
165 if (info->NumberOfBackEdges() > 1) {
166 HBasicBlock* new_back_edge = new (arena_) HBasicBlock(this);
167 AddBlock(new_back_edge);
168 new_back_edge->AddInstruction(new (arena_) HGoto());
169 for (size_t pred = 0, e = info->GetBackEdges().Size(); pred < e; ++pred) {
170 HBasicBlock* back_edge = info->GetBackEdges().Get(pred);
171 header->RemovePredecessor(back_edge);
172 back_edge->AddSuccessor(new_back_edge);
173 }
174 info->ClearBackEdges();
175 info->AddBackEdge(new_back_edge);
176 new_back_edge->AddSuccessor(header);
177 }
178
179 // Make sure the loop has only one pre header. This simplifies SSA building by having
180 // to just look at the pre header to know which locals are initialized at entry of the
181 // loop.
182 size_t number_of_incomings = header->GetPredecessors().Size() - info->NumberOfBackEdges();
183 if (number_of_incomings != 1) {
184 HBasicBlock* pre_header = new (arena_) HBasicBlock(this);
185 AddBlock(pre_header);
186 pre_header->AddInstruction(new (arena_) HGoto());
187
188 ArenaBitVector back_edges(arena_, GetBlocks().Size(), false);
189 HBasicBlock* back_edge = info->GetBackEdges().Get(0);
190 for (size_t pred = 0; pred < header->GetPredecessors().Size(); ++pred) {
191 HBasicBlock* predecessor = header->GetPredecessors().Get(pred);
192 if (predecessor != back_edge) {
193 header->RemovePredecessor(predecessor);
194 pred--;
195 predecessor->AddSuccessor(pre_header);
196 }
197 }
198 pre_header->AddSuccessor(header);
199 }
200}
201
202void HGraph::SimplifyCFG() {
203 // Simplify the CFG for future analysis, and code generation:
204 // (1): Split critical edges.
205 // (2): Simplify loops by having only one back edge, and one preheader.
206 for (size_t i = 0; i < blocks_.Size(); ++i) {
207 HBasicBlock* block = blocks_.Get(i);
208 if (block->GetSuccessors().Size() > 1) {
209 for (size_t j = 0; j < block->GetSuccessors().Size(); ++j) {
210 HBasicBlock* successor = block->GetSuccessors().Get(j);
211 if (successor->GetPredecessors().Size() > 1) {
212 SplitCriticalEdge(block, successor);
213 --j;
214 }
215 }
216 }
217 if (block->IsLoopHeader()) {
218 SimplifyLoop(block);
219 }
220 }
221}
222
223bool HGraph::FindNaturalLoops() const {
224 for (size_t i = 0; i < blocks_.Size(); ++i) {
225 HBasicBlock* block = blocks_.Get(i);
226 if (block->IsLoopHeader()) {
227 HLoopInformation* info = block->GetLoopInformation();
228 if (!info->Populate()) {
229 // Abort if the loop is non natural. We currently bailout in such cases.
230 return false;
231 }
232 }
233 }
234 return true;
235}
236
237void HLoopInformation::PopulateRecursive(HBasicBlock* block) {
238 if (blocks_.IsBitSet(block->GetBlockId())) {
239 return;
240 }
241
242 blocks_.SetBit(block->GetBlockId());
243 block->SetInLoop(this);
244 for (size_t i = 0, e = block->GetPredecessors().Size(); i < e; ++i) {
245 PopulateRecursive(block->GetPredecessors().Get(i));
246 }
247}
248
249bool HLoopInformation::Populate() {
250 DCHECK_EQ(GetBackEdges().Size(), 1u);
251 HBasicBlock* back_edge = GetBackEdges().Get(0);
252 DCHECK(back_edge->GetDominator() != nullptr);
253 if (!header_->Dominates(back_edge)) {
254 // This loop is not natural. Do not bother going further.
255 return false;
256 }
257
258 // Populate this loop: starting with the back edge, recursively add predecessors
259 // that are not already part of that loop. Set the header as part of the loop
260 // to end the recursion.
261 // This is a recursive implementation of the algorithm described in
262 // "Advanced Compiler Design & Implementation" (Muchnick) p192.
263 blocks_.SetBit(header_->GetBlockId());
264 PopulateRecursive(back_edge);
265 return true;
266}
267
268HBasicBlock* HLoopInformation::GetPreHeader() const {
269 DCHECK_EQ(header_->GetPredecessors().Size(), 2u);
270 return header_->GetDominator();
271}
272
273bool HLoopInformation::Contains(const HBasicBlock& block) const {
274 return blocks_.IsBitSet(block.GetBlockId());
275}
276
277bool HLoopInformation::IsIn(const HLoopInformation& other) const {
278 return other.blocks_.IsBitSet(header_->GetBlockId());
279}
280
281bool HBasicBlock::Dominates(HBasicBlock* other) const {
282 // Walk up the dominator tree from `other`, to find out if `this`
283 // is an ancestor.
284 HBasicBlock* current = other;
285 while (current != nullptr) {
286 if (current == this) {
287 return true;
288 }
289 current = current->GetDominator();
290 }
291 return false;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100292}
293
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100294void HBasicBlock::InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor) {
295 DCHECK(cursor->AsPhi() == nullptr);
296 DCHECK(instruction->AsPhi() == nullptr);
297 instruction->next_ = cursor;
298 instruction->previous_ = cursor->previous_;
299 cursor->previous_ = instruction;
300 if (GetFirstInstruction() == cursor) {
301 instructions_.first_instruction_ = instruction;
302 }
303}
304
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100305static void Add(HInstructionList* instruction_list,
306 HBasicBlock* block,
307 HInstruction* instruction) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000308 DCHECK(instruction->GetBlock() == nullptr);
Nicolas Geoffray43c86422014-03-18 11:58:24 +0000309 DCHECK_EQ(instruction->GetId(), -1);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100310 instruction->SetBlock(block);
311 instruction->SetId(block->GetGraph()->GetNextInstructionId());
312 instruction_list->AddInstruction(instruction);
313}
314
315void HBasicBlock::AddInstruction(HInstruction* instruction) {
316 Add(&instructions_, this, instruction);
317}
318
319void HBasicBlock::AddPhi(HPhi* phi) {
320 Add(&phis_, this, phi);
321}
322
323static void Remove(HInstructionList* instruction_list,
324 HBasicBlock* block,
325 HInstruction* instruction) {
326 DCHECK_EQ(block, instruction->GetBlock());
327 DCHECK(instruction->GetUses() == nullptr);
328 DCHECK(instruction->GetEnvUses() == nullptr);
329 instruction->SetBlock(nullptr);
330 instruction_list->RemoveInstruction(instruction);
331
332 for (size_t i = 0; i < instruction->InputCount(); i++) {
333 instruction->InputAt(i)->RemoveUser(instruction, i);
334 }
335}
336
337void HBasicBlock::RemoveInstruction(HInstruction* instruction) {
338 Remove(&instructions_, this, instruction);
339}
340
341void HBasicBlock::RemovePhi(HPhi* phi) {
342 Remove(&phis_, this, phi);
343}
344
345void HInstruction::RemoveUser(HInstruction* user, size_t input_index) {
346 HUseListNode<HInstruction>* previous = nullptr;
347 HUseListNode<HInstruction>* current = uses_;
348 while (current != nullptr) {
349 if (current->GetUser() == user && current->GetIndex() == input_index) {
350 if (previous == NULL) {
351 uses_ = current->GetTail();
352 } else {
353 previous->SetTail(current->GetTail());
354 }
355 }
356 previous = current;
357 current = current->GetTail();
358 }
359}
360
361void HInstructionList::AddInstruction(HInstruction* instruction) {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000362 if (first_instruction_ == nullptr) {
363 DCHECK(last_instruction_ == nullptr);
364 first_instruction_ = last_instruction_ = instruction;
365 } else {
366 last_instruction_->next_ = instruction;
367 instruction->previous_ = last_instruction_;
368 last_instruction_ = instruction;
369 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100370 for (size_t i = 0; i < instruction->InputCount(); i++) {
371 instruction->InputAt(i)->AddUseAt(instruction, i);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000372 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000373}
374
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100375void HInstructionList::RemoveInstruction(HInstruction* instruction) {
376 if (instruction->previous_ != nullptr) {
377 instruction->previous_->next_ = instruction->next_;
378 }
379 if (instruction->next_ != nullptr) {
380 instruction->next_->previous_ = instruction->previous_;
381 }
382 if (instruction == first_instruction_) {
383 first_instruction_ = instruction->next_;
384 }
385 if (instruction == last_instruction_) {
386 last_instruction_ = instruction->previous_;
387 }
388}
389
390void HInstruction::ReplaceWith(HInstruction* other) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100391 DCHECK(other != nullptr);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100392 for (HUseIterator<HInstruction> it(GetUses()); !it.Done(); it.Advance()) {
393 HUseListNode<HInstruction>* current = it.Current();
394 HInstruction* user = current->GetUser();
395 size_t input_index = current->GetIndex();
396 user->SetRawInputAt(input_index, other);
397 other->AddUseAt(user, input_index);
398 }
399
400 for (HUseIterator<HEnvironment> it(GetEnvUses()); !it.Done(); it.Advance()) {
401 HUseListNode<HEnvironment>* current = it.Current();
402 HEnvironment* user = current->GetUser();
403 size_t input_index = current->GetIndex();
404 user->SetRawEnvAt(input_index, other);
405 other->AddEnvUseAt(user, input_index);
406 }
407
408 uses_ = nullptr;
409 env_uses_ = nullptr;
410}
411
412void HPhi::AddInput(HInstruction* input) {
413 DCHECK(input->GetBlock() != nullptr);
414 inputs_.Add(input);
415 input->AddUseAt(this, inputs_.Size() - 1);
416}
417
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000418#define DEFINE_ACCEPT(name) \
419void H##name::Accept(HGraphVisitor* visitor) { \
420 visitor->Visit##name(this); \
421}
422
423FOR_EACH_INSTRUCTION(DEFINE_ACCEPT)
424
425#undef DEFINE_ACCEPT
426
427void HGraphVisitor::VisitInsertionOrder() {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100428 const GrowableArray<HBasicBlock*>& blocks = graph_->GetBlocks();
429 for (size_t i = 0 ; i < blocks.Size(); i++) {
430 VisitBasicBlock(blocks.Get(i));
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000431 }
432}
433
434void HGraphVisitor::VisitBasicBlock(HBasicBlock* block) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100435 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100436 it.Current()->Accept(this);
437 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100438 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000439 it.Current()->Accept(this);
440 }
441}
442
443} // namespace art