blob: 498deba2b49923f5d6cefee8e660108f17cb40f1 [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"
18#include "utils/growable_array.h"
19
20namespace art {
21
22void HGraph::AddBlock(HBasicBlock* block) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +000023 block->SetBlockId(blocks_.Size());
Nicolas Geoffray818f2102014-02-18 16:43:35 +000024 blocks_.Add(block);
25}
26
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000027void HGraph::FindBackEdges(ArenaBitVector* visited) const {
28 ArenaBitVector visiting(arena_, blocks_.Size(), false);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000029 VisitBlockForBackEdges(entry_block_, visited, &visiting);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000030}
31
32void HGraph::RemoveDeadBlocks(const ArenaBitVector& visited) const {
33 for (size_t i = 0; i < blocks_.Size(); i++) {
34 if (!visited.IsBitSet(i)) {
35 HBasicBlock* block = blocks_.Get(i);
Nicolas Geoffray787c3072014-03-17 10:20:19 +000036 for (size_t j = 0; j < block->GetSuccessors()->Size(); j++) {
37 block->GetSuccessors()->Get(j)->RemovePredecessor(block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000038 }
39 }
40 }
41}
42
43void HGraph::VisitBlockForBackEdges(HBasicBlock* block,
44 ArenaBitVector* visited,
45 ArenaBitVector* visiting) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +000046 int id = block->GetBlockId();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000047 if (visited->IsBitSet(id)) return;
48
49 visited->SetBit(id);
50 visiting->SetBit(id);
Nicolas Geoffray787c3072014-03-17 10:20:19 +000051 for (size_t i = 0; i < block->GetSuccessors()->Size(); i++) {
52 HBasicBlock* successor = block->GetSuccessors()->Get(i);
53 if (visiting->IsBitSet(successor->GetBlockId())) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000054 successor->AddBackEdge(block);
55 } else {
56 VisitBlockForBackEdges(successor, visited, visiting);
57 }
58 }
59 visiting->ClearBit(id);
60}
61
62void HGraph::BuildDominatorTree() {
63 ArenaBitVector visited(arena_, blocks_.Size(), false);
64
65 // (1) Find the back edges in the graph doing a DFS traversal.
66 FindBackEdges(&visited);
67
68 // (2) Remove blocks not visited during the initial DFS.
69 // Step (3) requires dead blocks to be removed from the
70 // predecessors list of live blocks.
71 RemoveDeadBlocks(visited);
72
73 // (3) Compute the immediate dominator of each block. We visit
74 // the successors of a block only when all its forward branches
75 // have been processed.
76 GrowableArray<size_t> visits(arena_, blocks_.Size());
77 visits.SetSize(blocks_.Size());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000078 dominator_order_.Add(entry_block_);
Nicolas Geoffray787c3072014-03-17 10:20:19 +000079 for (size_t i = 0; i < entry_block_->GetSuccessors()->Size(); i++) {
80 VisitBlockForDominatorTree(entry_block_->GetSuccessors()->Get(i), entry_block_, &visits);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000081 }
82}
83
84HBasicBlock* HGraph::FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const {
85 ArenaBitVector visited(arena_, blocks_.Size(), false);
86 // Walk the dominator tree of the first block and mark the visited blocks.
87 while (first != nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +000088 visited.SetBit(first->GetBlockId());
89 first = first->GetDominator();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000090 }
91 // Walk the dominator tree of the second block until a marked block is found.
92 while (second != nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +000093 if (visited.IsBitSet(second->GetBlockId())) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000094 return second;
95 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +000096 second = second->GetDominator();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000097 }
98 LOG(ERROR) << "Could not find common dominator";
99 return nullptr;
100}
101
102void HGraph::VisitBlockForDominatorTree(HBasicBlock* block,
103 HBasicBlock* predecessor,
104 GrowableArray<size_t>* visits) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000105 if (block->GetDominator() == nullptr) {
106 block->SetDominator(predecessor);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000107 } else {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000108 block->SetDominator(FindCommonDominator(block->GetDominator(), predecessor));
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000109 }
110
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000111 visits->Increment(block->GetBlockId());
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000112 // Once all the forward edges have been visited, we know the immediate
113 // dominator of the block. We can then start visiting its successors.
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000114 if (visits->Get(block->GetBlockId()) ==
115 block->GetPredecessors()->Size() - block->NumberOfBackEdges()) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000116 dominator_order_.Add(block);
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000117 for (size_t i = 0; i < block->GetSuccessors()->Size(); i++) {
118 VisitBlockForDominatorTree(block->GetSuccessors()->Get(i), block, visits);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000119 }
120 }
121}
122
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000123void HBasicBlock::AddInstruction(HInstruction* instruction) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000124 DCHECK(instruction->GetBlock() == nullptr);
Nicolas Geoffray43c86422014-03-18 11:58:24 +0000125 DCHECK_EQ(instruction->GetId(), -1);
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000126 instruction->SetBlock(this);
127 instruction->SetId(GetGraph()->GetNextInstructionId());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000128 if (first_instruction_ == nullptr) {
129 DCHECK(last_instruction_ == nullptr);
130 first_instruction_ = last_instruction_ = instruction;
131 } else {
132 last_instruction_->next_ = instruction;
133 instruction->previous_ = last_instruction_;
134 last_instruction_ = instruction;
135 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000136 for (int i = 0; i < instruction->InputCount(); i++) {
137 instruction->InputAt(i)->AddUse(instruction);
138 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000139}
140
141#define DEFINE_ACCEPT(name) \
142void H##name::Accept(HGraphVisitor* visitor) { \
143 visitor->Visit##name(this); \
144}
145
146FOR_EACH_INSTRUCTION(DEFINE_ACCEPT)
147
148#undef DEFINE_ACCEPT
149
150void HGraphVisitor::VisitInsertionOrder() {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000151 const GrowableArray<HBasicBlock*>* blocks = graph_->GetBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000152 for (size_t i = 0 ; i < blocks->Size(); i++) {
153 VisitBasicBlock(blocks->Get(i));
154 }
155}
156
157void HGraphVisitor::VisitBasicBlock(HBasicBlock* block) {
158 for (HInstructionIterator it(block); !it.Done(); it.Advance()) {
159 it.Current()->Accept(this);
160 }
161}
162
163} // namespace art