blob: a6f3f5ad0b66fa3a48820690e04e16d948b37b1d [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) {
23 block->set_block_id(blocks_.Size());
24 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);
36 for (size_t j = 0; j < block->successors()->Size(); j++) {
37 block->successors()->Get(j)->RemovePredecessor(block);
38 }
39 }
40 }
41}
42
43void HGraph::VisitBlockForBackEdges(HBasicBlock* block,
44 ArenaBitVector* visited,
45 ArenaBitVector* visiting) const {
46 int id = block->block_id();
47 if (visited->IsBitSet(id)) return;
48
49 visited->SetBit(id);
50 visiting->SetBit(id);
51 for (size_t i = 0; i < block->successors()->Size(); i++) {
52 HBasicBlock* successor = block->successors()->Get(i);
53 if (visiting->IsBitSet(successor->block_id())) {
54 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_);
79 for (size_t i = 0; i < entry_block_->successors()->Size(); i++) {
80 VisitBlockForDominatorTree(entry_block_->successors()->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) {
88 visited.SetBit(first->block_id());
89 first = first->dominator();
90 }
91 // Walk the dominator tree of the second block until a marked block is found.
92 while (second != nullptr) {
93 if (visited.IsBitSet(second->block_id())) {
94 return second;
95 }
96 second = second->dominator();
97 }
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) {
105 if (block->dominator() == nullptr) {
106 block->set_dominator(predecessor);
107 } else {
108 block->set_dominator(FindCommonDominator(block->dominator(), predecessor));
109 }
110
111 visits->Increment(block->block_id());
112 // Once all the forward edges have been visited, we know the immediate
113 // dominator of the block. We can then start visiting its successors.
114 if (visits->Get(block->block_id()) ==
115 block->predecessors()->Size() - block->NumberOfBackEdges()) {
116 dominator_order_.Add(block);
117 for (size_t i = 0; i < block->successors()->Size(); i++) {
118 VisitBlockForDominatorTree(block->successors()->Get(i), block, visits);
119 }
120 }
121}
122
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000123void HBasicBlock::AddInstruction(HInstruction* instruction) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000124 DCHECK(instruction->block() == nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000125 instruction->set_block(this);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000126 instruction->set_id(graph()->GetNextInstructionId());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000127 if (first_instruction_ == nullptr) {
128 DCHECK(last_instruction_ == nullptr);
129 first_instruction_ = last_instruction_ = instruction;
130 } else {
131 last_instruction_->next_ = instruction;
132 instruction->previous_ = last_instruction_;
133 last_instruction_ = instruction;
134 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000135 for (int i = 0; i < instruction->InputCount(); i++) {
136 instruction->InputAt(i)->AddUse(instruction);
137 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000138}
139
140#define DEFINE_ACCEPT(name) \
141void H##name::Accept(HGraphVisitor* visitor) { \
142 visitor->Visit##name(this); \
143}
144
145FOR_EACH_INSTRUCTION(DEFINE_ACCEPT)
146
147#undef DEFINE_ACCEPT
148
149void HGraphVisitor::VisitInsertionOrder() {
150 const GrowableArray<HBasicBlock*>* blocks = graph_->blocks();
151 for (size_t i = 0 ; i < blocks->Size(); i++) {
152 VisitBasicBlock(blocks->Get(i));
153 }
154}
155
156void HGraphVisitor::VisitBasicBlock(HBasicBlock* block) {
157 for (HInstructionIterator it(block); !it.Done(); it.Advance()) {
158 it.Current()->Accept(this);
159 }
160}
161
162} // namespace art