blob: 9ec8e89ff6f8d67c4c4fbde6cfbb445c692ce924 [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);
29 VisitBlockForBackEdges(GetEntryBlock(), visited, &visiting);
30}
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());
78 HBasicBlock* entry = GetEntryBlock();
79 dominator_order_.Add(entry);
80 for (size_t i = 0; i < entry->successors()->Size(); i++) {
81 VisitBlockForDominatorTree(entry->successors()->Get(i), entry, &visits);
82 }
83}
84
85HBasicBlock* HGraph::FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const {
86 ArenaBitVector visited(arena_, blocks_.Size(), false);
87 // Walk the dominator tree of the first block and mark the visited blocks.
88 while (first != nullptr) {
89 visited.SetBit(first->block_id());
90 first = first->dominator();
91 }
92 // Walk the dominator tree of the second block until a marked block is found.
93 while (second != nullptr) {
94 if (visited.IsBitSet(second->block_id())) {
95 return second;
96 }
97 second = second->dominator();
98 }
99 LOG(ERROR) << "Could not find common dominator";
100 return nullptr;
101}
102
103void HGraph::VisitBlockForDominatorTree(HBasicBlock* block,
104 HBasicBlock* predecessor,
105 GrowableArray<size_t>* visits) {
106 if (block->dominator() == nullptr) {
107 block->set_dominator(predecessor);
108 } else {
109 block->set_dominator(FindCommonDominator(block->dominator(), predecessor));
110 }
111
112 visits->Increment(block->block_id());
113 // Once all the forward edges have been visited, we know the immediate
114 // dominator of the block. We can then start visiting its successors.
115 if (visits->Get(block->block_id()) ==
116 block->predecessors()->Size() - block->NumberOfBackEdges()) {
117 dominator_order_.Add(block);
118 for (size_t i = 0; i < block->successors()->Size(); i++) {
119 VisitBlockForDominatorTree(block->successors()->Get(i), block, visits);
120 }
121 }
122}
123
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000124void HBasicBlock::AddInstruction(HInstruction* instruction) {
125 if (first_instruction_ == nullptr) {
126 DCHECK(last_instruction_ == nullptr);
127 first_instruction_ = last_instruction_ = instruction;
128 } else {
129 last_instruction_->next_ = instruction;
130 instruction->previous_ = last_instruction_;
131 last_instruction_ = instruction;
132 }
133}
134
135#define DEFINE_ACCEPT(name) \
136void H##name::Accept(HGraphVisitor* visitor) { \
137 visitor->Visit##name(this); \
138}
139
140FOR_EACH_INSTRUCTION(DEFINE_ACCEPT)
141
142#undef DEFINE_ACCEPT
143
144void HGraphVisitor::VisitInsertionOrder() {
145 const GrowableArray<HBasicBlock*>* blocks = graph_->blocks();
146 for (size_t i = 0 ; i < blocks->Size(); i++) {
147 VisitBasicBlock(blocks->Get(i));
148 }
149}
150
151void HGraphVisitor::VisitBasicBlock(HBasicBlock* block) {
152 for (HInstructionIterator it(block); !it.Done(); it.Advance()) {
153 it.Current()->Accept(this);
154 }
155}
156
157} // namespace art