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" |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame^] | 18 | #include "ssa_builder.h" |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 19 | #include "utils/growable_array.h" |
| 20 | |
| 21 | namespace art { |
| 22 | |
| 23 | void HGraph::AddBlock(HBasicBlock* block) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 24 | block->SetBlockId(blocks_.Size()); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 25 | blocks_.Add(block); |
| 26 | } |
| 27 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 28 | void HGraph::FindBackEdges(ArenaBitVector* visited) const { |
| 29 | ArenaBitVector visiting(arena_, blocks_.Size(), false); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 30 | VisitBlockForBackEdges(entry_block_, visited, &visiting); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | void HGraph::RemoveDeadBlocks(const ArenaBitVector& visited) const { |
| 34 | for (size_t i = 0; i < blocks_.Size(); i++) { |
| 35 | if (!visited.IsBitSet(i)) { |
| 36 | HBasicBlock* block = blocks_.Get(i); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 37 | for (size_t j = 0; j < block->GetSuccessors()->Size(); j++) { |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame^] | 38 | block->GetSuccessors()->Get(j)->RemovePredecessor(block, false); |
| 39 | } |
| 40 | for (HInstructionIterator it(*block->GetPhis()); !it.Done(); it.Advance()) { |
| 41 | block->RemovePhi(it.Current()->AsPhi()); |
| 42 | } |
| 43 | for (HInstructionIterator it(*block->GetInstructions()); !it.Done(); it.Advance()) { |
| 44 | block->RemoveInstruction(it.Current()); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 45 | } |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | void HGraph::VisitBlockForBackEdges(HBasicBlock* block, |
| 51 | ArenaBitVector* visited, |
| 52 | ArenaBitVector* visiting) const { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 53 | int id = block->GetBlockId(); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 54 | if (visited->IsBitSet(id)) return; |
| 55 | |
| 56 | visited->SetBit(id); |
| 57 | visiting->SetBit(id); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 58 | for (size_t i = 0; i < block->GetSuccessors()->Size(); i++) { |
| 59 | HBasicBlock* successor = block->GetSuccessors()->Get(i); |
| 60 | if (visiting->IsBitSet(successor->GetBlockId())) { |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 61 | successor->AddBackEdge(block); |
| 62 | } else { |
| 63 | VisitBlockForBackEdges(successor, visited, visiting); |
| 64 | } |
| 65 | } |
| 66 | visiting->ClearBit(id); |
| 67 | } |
| 68 | |
| 69 | void 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 | |
| 80 | // (3) Compute the immediate dominator of each block. We visit |
| 81 | // the successors of a block only when all its forward branches |
| 82 | // have been processed. |
| 83 | GrowableArray<size_t> visits(arena_, blocks_.Size()); |
| 84 | visits.SetSize(blocks_.Size()); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 85 | dominator_order_.Add(entry_block_); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 86 | for (size_t i = 0; i < entry_block_->GetSuccessors()->Size(); i++) { |
| 87 | VisitBlockForDominatorTree(entry_block_->GetSuccessors()->Get(i), entry_block_, &visits); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | |
| 91 | HBasicBlock* HGraph::FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const { |
| 92 | ArenaBitVector visited(arena_, blocks_.Size(), false); |
| 93 | // Walk the dominator tree of the first block and mark the visited blocks. |
| 94 | while (first != nullptr) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 95 | visited.SetBit(first->GetBlockId()); |
| 96 | first = first->GetDominator(); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 97 | } |
| 98 | // Walk the dominator tree of the second block until a marked block is found. |
| 99 | while (second != nullptr) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 100 | if (visited.IsBitSet(second->GetBlockId())) { |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 101 | return second; |
| 102 | } |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 103 | second = second->GetDominator(); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 104 | } |
| 105 | LOG(ERROR) << "Could not find common dominator"; |
| 106 | return nullptr; |
| 107 | } |
| 108 | |
| 109 | void HGraph::VisitBlockForDominatorTree(HBasicBlock* block, |
| 110 | HBasicBlock* predecessor, |
| 111 | GrowableArray<size_t>* visits) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 112 | if (block->GetDominator() == nullptr) { |
| 113 | block->SetDominator(predecessor); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 114 | } else { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 115 | block->SetDominator(FindCommonDominator(block->GetDominator(), predecessor)); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 118 | visits->Increment(block->GetBlockId()); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 119 | // Once all the forward edges have been visited, we know the immediate |
| 120 | // dominator of the block. We can then start visiting its successors. |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 121 | if (visits->Get(block->GetBlockId()) == |
| 122 | block->GetPredecessors()->Size() - block->NumberOfBackEdges()) { |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 123 | dominator_order_.Add(block); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 124 | for (size_t i = 0; i < block->GetSuccessors()->Size(); i++) { |
| 125 | VisitBlockForDominatorTree(block->GetSuccessors()->Get(i), block, visits); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame^] | 130 | void HGraph::TransformToSSA() { |
| 131 | DCHECK(!dominator_order_.IsEmpty()); |
| 132 | SimplifyCFG(); |
| 133 | SsaBuilder ssa_builder(this); |
| 134 | ssa_builder.BuildSsa(); |
| 135 | } |
| 136 | |
| 137 | void HGraph::SimplifyCFG() { |
| 138 | for (size_t i = 0; i < dominator_order_.Size(); i++) { |
| 139 | HBasicBlock* current = dominator_order_.Get(i); |
| 140 | if (current->IsLoopHeader()) { |
| 141 | // Make sure the loop has only one pre header. This simplifies SSA building by having |
| 142 | // to just look at the pre header to know which locals are initialized at entry of the |
| 143 | // loop. |
| 144 | HLoopInformation* info = current->GetLoopInformation(); |
| 145 | size_t number_of_incomings = current->GetPredecessors()->Size() - info->NumberOfBackEdges(); |
| 146 | if (number_of_incomings != 1) { |
| 147 | HBasicBlock* pre_header = new (arena_) HBasicBlock(this); |
| 148 | AddBlock(pre_header); |
| 149 | pre_header->AddInstruction(new (arena_) HGoto()); |
| 150 | pre_header->SetDominator(current->GetDominator()); |
| 151 | current->SetDominator(pre_header); |
| 152 | dominator_order_.InsertAt(i, pre_header); |
| 153 | i++; |
| 154 | |
| 155 | ArenaBitVector back_edges(arena_, GetBlocks()->Size(), false); |
| 156 | for (size_t pred = 0; pred < info->GetBackEdges()->Size(); pred++) { |
| 157 | back_edges.SetBit(info->GetBackEdges()->Get(pred)->GetBlockId()); |
| 158 | } |
| 159 | for (size_t pred = 0; pred < current->GetPredecessors()->Size(); pred++) { |
| 160 | HBasicBlock* predecessor = current->GetPredecessors()->Get(pred); |
| 161 | if (!back_edges.IsBitSet(predecessor->GetBlockId())) { |
| 162 | current->RemovePredecessor(predecessor); |
| 163 | pred--; |
| 164 | predecessor->AddSuccessor(pre_header); |
| 165 | } |
| 166 | } |
| 167 | pre_header->AddSuccessor(current); |
| 168 | } |
| 169 | info->SetPreHeader(current->GetDominator()); |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | void HLoopInformation::SetPreHeader(HBasicBlock* block) { |
| 175 | DCHECK_EQ(header_->GetDominator(), block); |
| 176 | pre_header_ = block; |
| 177 | } |
| 178 | |
| 179 | static void Add(HInstructionList* instruction_list, |
| 180 | HBasicBlock* block, |
| 181 | HInstruction* instruction) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 182 | DCHECK(instruction->GetBlock() == nullptr); |
Nicolas Geoffray | 43c8642 | 2014-03-18 11:58:24 +0000 | [diff] [blame] | 183 | DCHECK_EQ(instruction->GetId(), -1); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame^] | 184 | instruction->SetBlock(block); |
| 185 | instruction->SetId(block->GetGraph()->GetNextInstructionId()); |
| 186 | instruction_list->AddInstruction(instruction); |
| 187 | } |
| 188 | |
| 189 | void HBasicBlock::AddInstruction(HInstruction* instruction) { |
| 190 | Add(&instructions_, this, instruction); |
| 191 | } |
| 192 | |
| 193 | void HBasicBlock::AddPhi(HPhi* phi) { |
| 194 | Add(&phis_, this, phi); |
| 195 | } |
| 196 | |
| 197 | static void Remove(HInstructionList* instruction_list, |
| 198 | HBasicBlock* block, |
| 199 | HInstruction* instruction) { |
| 200 | DCHECK_EQ(block, instruction->GetBlock()); |
| 201 | DCHECK(instruction->GetUses() == nullptr); |
| 202 | DCHECK(instruction->GetEnvUses() == nullptr); |
| 203 | instruction->SetBlock(nullptr); |
| 204 | instruction_list->RemoveInstruction(instruction); |
| 205 | |
| 206 | for (size_t i = 0; i < instruction->InputCount(); i++) { |
| 207 | instruction->InputAt(i)->RemoveUser(instruction, i); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | void HBasicBlock::RemoveInstruction(HInstruction* instruction) { |
| 212 | Remove(&instructions_, this, instruction); |
| 213 | } |
| 214 | |
| 215 | void HBasicBlock::RemovePhi(HPhi* phi) { |
| 216 | Remove(&phis_, this, phi); |
| 217 | } |
| 218 | |
| 219 | void HInstruction::RemoveUser(HInstruction* user, size_t input_index) { |
| 220 | HUseListNode<HInstruction>* previous = nullptr; |
| 221 | HUseListNode<HInstruction>* current = uses_; |
| 222 | while (current != nullptr) { |
| 223 | if (current->GetUser() == user && current->GetIndex() == input_index) { |
| 224 | if (previous == NULL) { |
| 225 | uses_ = current->GetTail(); |
| 226 | } else { |
| 227 | previous->SetTail(current->GetTail()); |
| 228 | } |
| 229 | } |
| 230 | previous = current; |
| 231 | current = current->GetTail(); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | void HInstructionList::AddInstruction(HInstruction* instruction) { |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 236 | if (first_instruction_ == nullptr) { |
| 237 | DCHECK(last_instruction_ == nullptr); |
| 238 | first_instruction_ = last_instruction_ = instruction; |
| 239 | } else { |
| 240 | last_instruction_->next_ = instruction; |
| 241 | instruction->previous_ = last_instruction_; |
| 242 | last_instruction_ = instruction; |
| 243 | } |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame^] | 244 | for (size_t i = 0; i < instruction->InputCount(); i++) { |
| 245 | instruction->InputAt(i)->AddUseAt(instruction, i); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 246 | } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 247 | } |
| 248 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame^] | 249 | void HInstructionList::RemoveInstruction(HInstruction* instruction) { |
| 250 | if (instruction->previous_ != nullptr) { |
| 251 | instruction->previous_->next_ = instruction->next_; |
| 252 | } |
| 253 | if (instruction->next_ != nullptr) { |
| 254 | instruction->next_->previous_ = instruction->previous_; |
| 255 | } |
| 256 | if (instruction == first_instruction_) { |
| 257 | first_instruction_ = instruction->next_; |
| 258 | } |
| 259 | if (instruction == last_instruction_) { |
| 260 | last_instruction_ = instruction->previous_; |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | void HInstruction::ReplaceWith(HInstruction* other) { |
| 265 | for (HUseIterator<HInstruction> it(GetUses()); !it.Done(); it.Advance()) { |
| 266 | HUseListNode<HInstruction>* current = it.Current(); |
| 267 | HInstruction* user = current->GetUser(); |
| 268 | size_t input_index = current->GetIndex(); |
| 269 | user->SetRawInputAt(input_index, other); |
| 270 | other->AddUseAt(user, input_index); |
| 271 | } |
| 272 | |
| 273 | for (HUseIterator<HEnvironment> it(GetEnvUses()); !it.Done(); it.Advance()) { |
| 274 | HUseListNode<HEnvironment>* current = it.Current(); |
| 275 | HEnvironment* user = current->GetUser(); |
| 276 | size_t input_index = current->GetIndex(); |
| 277 | user->SetRawEnvAt(input_index, other); |
| 278 | other->AddEnvUseAt(user, input_index); |
| 279 | } |
| 280 | |
| 281 | uses_ = nullptr; |
| 282 | env_uses_ = nullptr; |
| 283 | } |
| 284 | |
| 285 | void HPhi::AddInput(HInstruction* input) { |
| 286 | DCHECK(input->GetBlock() != nullptr); |
| 287 | inputs_.Add(input); |
| 288 | input->AddUseAt(this, inputs_.Size() - 1); |
| 289 | } |
| 290 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 291 | #define DEFINE_ACCEPT(name) \ |
| 292 | void H##name::Accept(HGraphVisitor* visitor) { \ |
| 293 | visitor->Visit##name(this); \ |
| 294 | } |
| 295 | |
| 296 | FOR_EACH_INSTRUCTION(DEFINE_ACCEPT) |
| 297 | |
| 298 | #undef DEFINE_ACCEPT |
| 299 | |
| 300 | void HGraphVisitor::VisitInsertionOrder() { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 301 | const GrowableArray<HBasicBlock*>* blocks = graph_->GetBlocks(); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 302 | for (size_t i = 0 ; i < blocks->Size(); i++) { |
| 303 | VisitBasicBlock(blocks->Get(i)); |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | void HGraphVisitor::VisitBasicBlock(HBasicBlock* block) { |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame^] | 308 | for (HInstructionIterator it(*block->GetPhis()); !it.Done(); it.Advance()) { |
| 309 | it.Current()->Accept(this); |
| 310 | } |
| 311 | for (HInstructionIterator it(*block->GetInstructions()); !it.Done(); it.Advance()) { |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 312 | it.Current()->Accept(this); |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | } // namespace art |