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 | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 28 | void HGraph::FindBackEdges(ArenaBitVector* visited) { |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 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 { |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 34 | for (size_t i = 0; i < blocks_.Size(); ++i) { |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 35 | if (!visited.IsBitSet(i)) { |
| 36 | HBasicBlock* block = blocks_.Get(i); |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 37 | for (size_t j = 0; j < block->GetSuccessors().Size(); ++j) { |
| 38 | block->GetSuccessors().Get(j)->RemovePredecessor(block, false); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 39 | } |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 40 | for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) { |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 41 | block->RemovePhi(it.Current()->AsPhi()); |
| 42 | } |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 43 | for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) { |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 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, |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 52 | ArenaBitVector* visiting) { |
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 | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 58 | for (size_t i = 0; i < block->GetSuccessors().Size(); i++) { |
| 59 | HBasicBlock* successor = block->GetSuccessors().Get(i); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 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 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 80 | // (3) Simplify the CFG now, so that we don't need to recompute |
| 81 | // dominators and the reverse post order. |
| 82 | SimplifyCFG(); |
| 83 | |
| 84 | // (4) Compute the immediate dominator of each block. We visit |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 85 | // the successors of a block only when all its forward branches |
| 86 | // have been processed. |
| 87 | GrowableArray<size_t> visits(arena_, blocks_.Size()); |
| 88 | visits.SetSize(blocks_.Size()); |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 89 | reverse_post_order_.Add(entry_block_); |
| 90 | for (size_t i = 0; i < entry_block_->GetSuccessors().Size(); i++) { |
| 91 | VisitBlockForDominatorTree(entry_block_->GetSuccessors().Get(i), entry_block_, &visits); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | |
| 95 | HBasicBlock* HGraph::FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const { |
| 96 | ArenaBitVector visited(arena_, blocks_.Size(), false); |
| 97 | // Walk the dominator tree of the first block and mark the visited blocks. |
| 98 | while (first != nullptr) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 99 | visited.SetBit(first->GetBlockId()); |
| 100 | first = first->GetDominator(); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 101 | } |
| 102 | // Walk the dominator tree of the second block until a marked block is found. |
| 103 | while (second != nullptr) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 104 | if (visited.IsBitSet(second->GetBlockId())) { |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 105 | return second; |
| 106 | } |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 107 | second = second->GetDominator(); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 108 | } |
| 109 | LOG(ERROR) << "Could not find common dominator"; |
| 110 | return nullptr; |
| 111 | } |
| 112 | |
| 113 | void HGraph::VisitBlockForDominatorTree(HBasicBlock* block, |
| 114 | HBasicBlock* predecessor, |
| 115 | GrowableArray<size_t>* visits) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 116 | if (block->GetDominator() == nullptr) { |
| 117 | block->SetDominator(predecessor); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 118 | } else { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 119 | block->SetDominator(FindCommonDominator(block->GetDominator(), predecessor)); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 122 | visits->Increment(block->GetBlockId()); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 123 | // Once all the forward edges have been visited, we know the immediate |
| 124 | // dominator of the block. We can then start visiting its successors. |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 125 | if (visits->Get(block->GetBlockId()) == |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 126 | block->GetPredecessors().Size() - block->NumberOfBackEdges()) { |
| 127 | reverse_post_order_.Add(block); |
| 128 | for (size_t i = 0; i < block->GetSuccessors().Size(); i++) { |
| 129 | VisitBlockForDominatorTree(block->GetSuccessors().Get(i), block, visits); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 134 | void HGraph::TransformToSSA() { |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 135 | DCHECK(!reverse_post_order_.IsEmpty()); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 136 | SsaBuilder ssa_builder(this); |
| 137 | ssa_builder.BuildSsa(); |
| 138 | } |
| 139 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 140 | void HGraph::SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor) { |
| 141 | // Insert a new node between `block` and `successor` to split the |
| 142 | // critical edge. |
| 143 | HBasicBlock* new_block = new (arena_) HBasicBlock(this); |
| 144 | AddBlock(new_block); |
| 145 | new_block->AddInstruction(new (arena_) HGoto()); |
| 146 | block->RemoveSuccessor(successor); |
| 147 | block->AddSuccessor(new_block); |
| 148 | new_block->AddSuccessor(successor); |
| 149 | if (successor->IsLoopHeader()) { |
| 150 | // If we split at a back edge boundary, make the new block the back edge. |
| 151 | HLoopInformation* info = successor->GetLoopInformation(); |
| 152 | if (info->IsBackEdge(block)) { |
| 153 | info->RemoveBackEdge(block); |
| 154 | info->AddBackEdge(new_block); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | } |
| 158 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 159 | void HGraph::SimplifyLoop(HBasicBlock* header) { |
| 160 | HLoopInformation* info = header->GetLoopInformation(); |
| 161 | |
| 162 | // If there are more than one back edge, make them branch to the same block that |
| 163 | // will become the only back edge. This simplifies finding natural loops in the |
| 164 | // graph. |
| 165 | if (info->NumberOfBackEdges() > 1) { |
| 166 | HBasicBlock* new_back_edge = new (arena_) HBasicBlock(this); |
| 167 | AddBlock(new_back_edge); |
| 168 | new_back_edge->AddInstruction(new (arena_) HGoto()); |
| 169 | for (size_t pred = 0, e = info->GetBackEdges().Size(); pred < e; ++pred) { |
| 170 | HBasicBlock* back_edge = info->GetBackEdges().Get(pred); |
| 171 | header->RemovePredecessor(back_edge); |
| 172 | back_edge->AddSuccessor(new_back_edge); |
| 173 | } |
| 174 | info->ClearBackEdges(); |
| 175 | info->AddBackEdge(new_back_edge); |
| 176 | new_back_edge->AddSuccessor(header); |
| 177 | } |
| 178 | |
| 179 | // Make sure the loop has only one pre header. This simplifies SSA building by having |
| 180 | // to just look at the pre header to know which locals are initialized at entry of the |
| 181 | // loop. |
| 182 | size_t number_of_incomings = header->GetPredecessors().Size() - info->NumberOfBackEdges(); |
| 183 | if (number_of_incomings != 1) { |
| 184 | HBasicBlock* pre_header = new (arena_) HBasicBlock(this); |
| 185 | AddBlock(pre_header); |
| 186 | pre_header->AddInstruction(new (arena_) HGoto()); |
| 187 | |
| 188 | ArenaBitVector back_edges(arena_, GetBlocks().Size(), false); |
| 189 | HBasicBlock* back_edge = info->GetBackEdges().Get(0); |
| 190 | for (size_t pred = 0; pred < header->GetPredecessors().Size(); ++pred) { |
| 191 | HBasicBlock* predecessor = header->GetPredecessors().Get(pred); |
| 192 | if (predecessor != back_edge) { |
| 193 | header->RemovePredecessor(predecessor); |
| 194 | pred--; |
| 195 | predecessor->AddSuccessor(pre_header); |
| 196 | } |
| 197 | } |
| 198 | pre_header->AddSuccessor(header); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | void HGraph::SimplifyCFG() { |
| 203 | // Simplify the CFG for future analysis, and code generation: |
| 204 | // (1): Split critical edges. |
| 205 | // (2): Simplify loops by having only one back edge, and one preheader. |
| 206 | for (size_t i = 0; i < blocks_.Size(); ++i) { |
| 207 | HBasicBlock* block = blocks_.Get(i); |
| 208 | if (block->GetSuccessors().Size() > 1) { |
| 209 | for (size_t j = 0; j < block->GetSuccessors().Size(); ++j) { |
| 210 | HBasicBlock* successor = block->GetSuccessors().Get(j); |
| 211 | if (successor->GetPredecessors().Size() > 1) { |
| 212 | SplitCriticalEdge(block, successor); |
| 213 | --j; |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | if (block->IsLoopHeader()) { |
| 218 | SimplifyLoop(block); |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | bool HGraph::FindNaturalLoops() const { |
| 224 | for (size_t i = 0; i < blocks_.Size(); ++i) { |
| 225 | HBasicBlock* block = blocks_.Get(i); |
| 226 | if (block->IsLoopHeader()) { |
| 227 | HLoopInformation* info = block->GetLoopInformation(); |
| 228 | if (!info->Populate()) { |
| 229 | // Abort if the loop is non natural. We currently bailout in such cases. |
| 230 | return false; |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | return true; |
| 235 | } |
| 236 | |
| 237 | void HLoopInformation::PopulateRecursive(HBasicBlock* block) { |
| 238 | if (blocks_.IsBitSet(block->GetBlockId())) { |
| 239 | return; |
| 240 | } |
| 241 | |
| 242 | blocks_.SetBit(block->GetBlockId()); |
| 243 | block->SetInLoop(this); |
| 244 | for (size_t i = 0, e = block->GetPredecessors().Size(); i < e; ++i) { |
| 245 | PopulateRecursive(block->GetPredecessors().Get(i)); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | bool HLoopInformation::Populate() { |
| 250 | DCHECK_EQ(GetBackEdges().Size(), 1u); |
| 251 | HBasicBlock* back_edge = GetBackEdges().Get(0); |
| 252 | DCHECK(back_edge->GetDominator() != nullptr); |
| 253 | if (!header_->Dominates(back_edge)) { |
| 254 | // This loop is not natural. Do not bother going further. |
| 255 | return false; |
| 256 | } |
| 257 | |
| 258 | // Populate this loop: starting with the back edge, recursively add predecessors |
| 259 | // that are not already part of that loop. Set the header as part of the loop |
| 260 | // to end the recursion. |
| 261 | // This is a recursive implementation of the algorithm described in |
| 262 | // "Advanced Compiler Design & Implementation" (Muchnick) p192. |
| 263 | blocks_.SetBit(header_->GetBlockId()); |
| 264 | PopulateRecursive(back_edge); |
| 265 | return true; |
| 266 | } |
| 267 | |
| 268 | HBasicBlock* HLoopInformation::GetPreHeader() const { |
| 269 | DCHECK_EQ(header_->GetPredecessors().Size(), 2u); |
| 270 | return header_->GetDominator(); |
| 271 | } |
| 272 | |
| 273 | bool HLoopInformation::Contains(const HBasicBlock& block) const { |
| 274 | return blocks_.IsBitSet(block.GetBlockId()); |
| 275 | } |
| 276 | |
| 277 | bool HLoopInformation::IsIn(const HLoopInformation& other) const { |
| 278 | return other.blocks_.IsBitSet(header_->GetBlockId()); |
| 279 | } |
| 280 | |
| 281 | bool HBasicBlock::Dominates(HBasicBlock* other) const { |
| 282 | // Walk up the dominator tree from `other`, to find out if `this` |
| 283 | // is an ancestor. |
| 284 | HBasicBlock* current = other; |
| 285 | while (current != nullptr) { |
| 286 | if (current == this) { |
| 287 | return true; |
| 288 | } |
| 289 | current = current->GetDominator(); |
| 290 | } |
| 291 | return false; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 292 | } |
| 293 | |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 294 | void HBasicBlock::InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor) { |
| 295 | DCHECK(cursor->AsPhi() == nullptr); |
| 296 | DCHECK(instruction->AsPhi() == nullptr); |
| 297 | instruction->next_ = cursor; |
| 298 | instruction->previous_ = cursor->previous_; |
| 299 | cursor->previous_ = instruction; |
| 300 | if (GetFirstInstruction() == cursor) { |
| 301 | instructions_.first_instruction_ = instruction; |
| 302 | } |
| 303 | } |
| 304 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 305 | static void Add(HInstructionList* instruction_list, |
| 306 | HBasicBlock* block, |
| 307 | HInstruction* instruction) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 308 | DCHECK(instruction->GetBlock() == nullptr); |
Nicolas Geoffray | 43c8642 | 2014-03-18 11:58:24 +0000 | [diff] [blame] | 309 | DCHECK_EQ(instruction->GetId(), -1); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 310 | instruction->SetBlock(block); |
| 311 | instruction->SetId(block->GetGraph()->GetNextInstructionId()); |
| 312 | instruction_list->AddInstruction(instruction); |
| 313 | } |
| 314 | |
| 315 | void HBasicBlock::AddInstruction(HInstruction* instruction) { |
| 316 | Add(&instructions_, this, instruction); |
| 317 | } |
| 318 | |
| 319 | void HBasicBlock::AddPhi(HPhi* phi) { |
| 320 | Add(&phis_, this, phi); |
| 321 | } |
| 322 | |
| 323 | static void Remove(HInstructionList* instruction_list, |
| 324 | HBasicBlock* block, |
| 325 | HInstruction* instruction) { |
| 326 | DCHECK_EQ(block, instruction->GetBlock()); |
| 327 | DCHECK(instruction->GetUses() == nullptr); |
| 328 | DCHECK(instruction->GetEnvUses() == nullptr); |
| 329 | instruction->SetBlock(nullptr); |
| 330 | instruction_list->RemoveInstruction(instruction); |
| 331 | |
| 332 | for (size_t i = 0; i < instruction->InputCount(); i++) { |
| 333 | instruction->InputAt(i)->RemoveUser(instruction, i); |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | void HBasicBlock::RemoveInstruction(HInstruction* instruction) { |
| 338 | Remove(&instructions_, this, instruction); |
| 339 | } |
| 340 | |
| 341 | void HBasicBlock::RemovePhi(HPhi* phi) { |
| 342 | Remove(&phis_, this, phi); |
| 343 | } |
| 344 | |
| 345 | void HInstruction::RemoveUser(HInstruction* user, size_t input_index) { |
| 346 | HUseListNode<HInstruction>* previous = nullptr; |
| 347 | HUseListNode<HInstruction>* current = uses_; |
| 348 | while (current != nullptr) { |
| 349 | if (current->GetUser() == user && current->GetIndex() == input_index) { |
| 350 | if (previous == NULL) { |
| 351 | uses_ = current->GetTail(); |
| 352 | } else { |
| 353 | previous->SetTail(current->GetTail()); |
| 354 | } |
| 355 | } |
| 356 | previous = current; |
| 357 | current = current->GetTail(); |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | void HInstructionList::AddInstruction(HInstruction* instruction) { |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 362 | if (first_instruction_ == nullptr) { |
| 363 | DCHECK(last_instruction_ == nullptr); |
| 364 | first_instruction_ = last_instruction_ = instruction; |
| 365 | } else { |
| 366 | last_instruction_->next_ = instruction; |
| 367 | instruction->previous_ = last_instruction_; |
| 368 | last_instruction_ = instruction; |
| 369 | } |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 370 | for (size_t i = 0; i < instruction->InputCount(); i++) { |
| 371 | instruction->InputAt(i)->AddUseAt(instruction, i); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 372 | } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 373 | } |
| 374 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 375 | void HInstructionList::RemoveInstruction(HInstruction* instruction) { |
| 376 | if (instruction->previous_ != nullptr) { |
| 377 | instruction->previous_->next_ = instruction->next_; |
| 378 | } |
| 379 | if (instruction->next_ != nullptr) { |
| 380 | instruction->next_->previous_ = instruction->previous_; |
| 381 | } |
| 382 | if (instruction == first_instruction_) { |
| 383 | first_instruction_ = instruction->next_; |
| 384 | } |
| 385 | if (instruction == last_instruction_) { |
| 386 | last_instruction_ = instruction->previous_; |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | void HInstruction::ReplaceWith(HInstruction* other) { |
| 391 | for (HUseIterator<HInstruction> it(GetUses()); !it.Done(); it.Advance()) { |
| 392 | HUseListNode<HInstruction>* current = it.Current(); |
| 393 | HInstruction* user = current->GetUser(); |
| 394 | size_t input_index = current->GetIndex(); |
| 395 | user->SetRawInputAt(input_index, other); |
| 396 | other->AddUseAt(user, input_index); |
| 397 | } |
| 398 | |
| 399 | for (HUseIterator<HEnvironment> it(GetEnvUses()); !it.Done(); it.Advance()) { |
| 400 | HUseListNode<HEnvironment>* current = it.Current(); |
| 401 | HEnvironment* user = current->GetUser(); |
| 402 | size_t input_index = current->GetIndex(); |
| 403 | user->SetRawEnvAt(input_index, other); |
| 404 | other->AddEnvUseAt(user, input_index); |
| 405 | } |
| 406 | |
| 407 | uses_ = nullptr; |
| 408 | env_uses_ = nullptr; |
| 409 | } |
| 410 | |
| 411 | void HPhi::AddInput(HInstruction* input) { |
| 412 | DCHECK(input->GetBlock() != nullptr); |
| 413 | inputs_.Add(input); |
| 414 | input->AddUseAt(this, inputs_.Size() - 1); |
| 415 | } |
| 416 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 417 | #define DEFINE_ACCEPT(name) \ |
| 418 | void H##name::Accept(HGraphVisitor* visitor) { \ |
| 419 | visitor->Visit##name(this); \ |
| 420 | } |
| 421 | |
| 422 | FOR_EACH_INSTRUCTION(DEFINE_ACCEPT) |
| 423 | |
| 424 | #undef DEFINE_ACCEPT |
| 425 | |
| 426 | void HGraphVisitor::VisitInsertionOrder() { |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 427 | const GrowableArray<HBasicBlock*>& blocks = graph_->GetBlocks(); |
| 428 | for (size_t i = 0 ; i < blocks.Size(); i++) { |
| 429 | VisitBasicBlock(blocks.Get(i)); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 430 | } |
| 431 | } |
| 432 | |
| 433 | void HGraphVisitor::VisitBasicBlock(HBasicBlock* block) { |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 434 | for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) { |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 435 | it.Current()->Accept(this); |
| 436 | } |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 437 | for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) { |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 438 | it.Current()->Accept(this); |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | } // namespace art |