Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 20 | namespace art { |
| 21 | |
| 22 | void HGraph::AddBlock(HBasicBlock* block) { |
| 23 | block->set_block_id(blocks_.Size()); |
| 24 | blocks_.Add(block); |
| 25 | } |
| 26 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 27 | void HGraph::FindBackEdges(ArenaBitVector* visited) const { |
| 28 | ArenaBitVector visiting(arena_, blocks_.Size(), false); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 29 | VisitBlockForBackEdges(entry_block_, visited, &visiting); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | void 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 | |
| 43 | void 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 | |
| 62 | void 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 Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 78 | 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 Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
| 84 | HBasicBlock* 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 | |
| 102 | void 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 Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 123 | void HBasicBlock::AddInstruction(HInstruction* instruction) { |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame^] | 124 | DCHECK(instruction->block() == nullptr); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 125 | instruction->set_block(this); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 126 | instruction->set_id(graph()->GetNextInstructionId()); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 127 | 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 Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 135 | for (int i = 0; i < instruction->InputCount(); i++) { |
| 136 | instruction->InputAt(i)->AddUse(instruction); |
| 137 | } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | #define DEFINE_ACCEPT(name) \ |
| 141 | void H##name::Accept(HGraphVisitor* visitor) { \ |
| 142 | visitor->Visit##name(this); \ |
| 143 | } |
| 144 | |
| 145 | FOR_EACH_INSTRUCTION(DEFINE_ACCEPT) |
| 146 | |
| 147 | #undef DEFINE_ACCEPT |
| 148 | |
| 149 | void 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 | |
| 156 | void HGraphVisitor::VisitBasicBlock(HBasicBlock* block) { |
| 157 | for (HInstructionIterator it(block); !it.Done(); it.Advance()) { |
| 158 | it.Current()->Accept(this); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | } // namespace art |