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) { |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 38 | block->GetSuccessors().Get(j)->RemovePredecessor(block); |
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()) { |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 127 | block->GetDominator()->AddDominatedBlock(block); |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 128 | reverse_post_order_.Add(block); |
| 129 | for (size_t i = 0; i < block->GetSuccessors().Size(); i++) { |
| 130 | VisitBlockForDominatorTree(block->GetSuccessors().Get(i), block, visits); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 135 | void HGraph::TransformToSSA() { |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 136 | DCHECK(!reverse_post_order_.IsEmpty()); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 137 | SsaBuilder ssa_builder(this); |
| 138 | ssa_builder.BuildSsa(); |
| 139 | } |
| 140 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 141 | void HGraph::SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor) { |
| 142 | // Insert a new node between `block` and `successor` to split the |
| 143 | // critical edge. |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 144 | HBasicBlock* new_block = new (arena_) HBasicBlock(this, successor->GetDexPc()); |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 145 | AddBlock(new_block); |
| 146 | new_block->AddInstruction(new (arena_) HGoto()); |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 147 | block->ReplaceSuccessor(successor, new_block); |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 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. |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 165 | // Also, if the loop is a do/while (that is the back edge is an if), change the |
| 166 | // back edge to be a goto. This simplifies code generation of suspend cheks. |
| 167 | if (info->NumberOfBackEdges() > 1 || info->GetBackEdges().Get(0)->GetLastInstruction()->IsIf()) { |
| 168 | HBasicBlock* new_back_edge = new (arena_) HBasicBlock(this, header->GetDexPc()); |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 169 | AddBlock(new_back_edge); |
| 170 | new_back_edge->AddInstruction(new (arena_) HGoto()); |
| 171 | for (size_t pred = 0, e = info->GetBackEdges().Size(); pred < e; ++pred) { |
| 172 | HBasicBlock* back_edge = info->GetBackEdges().Get(pred); |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 173 | back_edge->ReplaceSuccessor(header, new_back_edge); |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 174 | } |
| 175 | info->ClearBackEdges(); |
| 176 | info->AddBackEdge(new_back_edge); |
| 177 | new_back_edge->AddSuccessor(header); |
| 178 | } |
| 179 | |
| 180 | // Make sure the loop has only one pre header. This simplifies SSA building by having |
| 181 | // to just look at the pre header to know which locals are initialized at entry of the |
| 182 | // loop. |
| 183 | size_t number_of_incomings = header->GetPredecessors().Size() - info->NumberOfBackEdges(); |
| 184 | if (number_of_incomings != 1) { |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 185 | HBasicBlock* pre_header = new (arena_) HBasicBlock(this, header->GetDexPc()); |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 186 | AddBlock(pre_header); |
| 187 | pre_header->AddInstruction(new (arena_) HGoto()); |
| 188 | |
| 189 | ArenaBitVector back_edges(arena_, GetBlocks().Size(), false); |
| 190 | HBasicBlock* back_edge = info->GetBackEdges().Get(0); |
| 191 | for (size_t pred = 0; pred < header->GetPredecessors().Size(); ++pred) { |
| 192 | HBasicBlock* predecessor = header->GetPredecessors().Get(pred); |
| 193 | if (predecessor != back_edge) { |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 194 | predecessor->ReplaceSuccessor(header, pre_header); |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 195 | pred--; |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | pre_header->AddSuccessor(header); |
| 199 | } |
Nicolas Geoffray | 604c6e4 | 2014-09-17 12:08:44 +0100 | [diff] [blame] | 200 | |
| 201 | // Make sure the second predecessor of a loop header is the back edge. |
| 202 | if (header->GetPredecessors().Get(1) != info->GetBackEdges().Get(0)) { |
| 203 | header->SwapPredecessors(); |
| 204 | } |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 205 | |
| 206 | // Place the suspend check at the beginning of the header, so that live registers |
| 207 | // will be known when allocating registers. Note that code generation can still |
| 208 | // generate the suspend check at the back edge, but needs to be careful with |
| 209 | // loop phi spill slots (which are not written to at back edge). |
| 210 | HInstruction* first_instruction = header->GetFirstInstruction(); |
| 211 | if (!first_instruction->IsSuspendCheck()) { |
| 212 | HSuspendCheck* check = new (arena_) HSuspendCheck(header->GetDexPc()); |
| 213 | header->InsertInstructionBefore(check, first_instruction); |
| 214 | first_instruction = check; |
| 215 | } |
| 216 | info->SetSuspendCheck(first_instruction->AsSuspendCheck()); |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | void HGraph::SimplifyCFG() { |
| 220 | // Simplify the CFG for future analysis, and code generation: |
| 221 | // (1): Split critical edges. |
| 222 | // (2): Simplify loops by having only one back edge, and one preheader. |
| 223 | for (size_t i = 0; i < blocks_.Size(); ++i) { |
| 224 | HBasicBlock* block = blocks_.Get(i); |
| 225 | if (block->GetSuccessors().Size() > 1) { |
| 226 | for (size_t j = 0; j < block->GetSuccessors().Size(); ++j) { |
| 227 | HBasicBlock* successor = block->GetSuccessors().Get(j); |
| 228 | if (successor->GetPredecessors().Size() > 1) { |
| 229 | SplitCriticalEdge(block, successor); |
| 230 | --j; |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | if (block->IsLoopHeader()) { |
| 235 | SimplifyLoop(block); |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | bool HGraph::FindNaturalLoops() const { |
| 241 | for (size_t i = 0; i < blocks_.Size(); ++i) { |
| 242 | HBasicBlock* block = blocks_.Get(i); |
| 243 | if (block->IsLoopHeader()) { |
| 244 | HLoopInformation* info = block->GetLoopInformation(); |
| 245 | if (!info->Populate()) { |
| 246 | // Abort if the loop is non natural. We currently bailout in such cases. |
| 247 | return false; |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | return true; |
| 252 | } |
| 253 | |
| 254 | void HLoopInformation::PopulateRecursive(HBasicBlock* block) { |
| 255 | if (blocks_.IsBitSet(block->GetBlockId())) { |
| 256 | return; |
| 257 | } |
| 258 | |
| 259 | blocks_.SetBit(block->GetBlockId()); |
| 260 | block->SetInLoop(this); |
| 261 | for (size_t i = 0, e = block->GetPredecessors().Size(); i < e; ++i) { |
| 262 | PopulateRecursive(block->GetPredecessors().Get(i)); |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | bool HLoopInformation::Populate() { |
| 267 | DCHECK_EQ(GetBackEdges().Size(), 1u); |
| 268 | HBasicBlock* back_edge = GetBackEdges().Get(0); |
| 269 | DCHECK(back_edge->GetDominator() != nullptr); |
| 270 | if (!header_->Dominates(back_edge)) { |
| 271 | // This loop is not natural. Do not bother going further. |
| 272 | return false; |
| 273 | } |
| 274 | |
| 275 | // Populate this loop: starting with the back edge, recursively add predecessors |
| 276 | // that are not already part of that loop. Set the header as part of the loop |
| 277 | // to end the recursion. |
| 278 | // This is a recursive implementation of the algorithm described in |
| 279 | // "Advanced Compiler Design & Implementation" (Muchnick) p192. |
| 280 | blocks_.SetBit(header_->GetBlockId()); |
| 281 | PopulateRecursive(back_edge); |
| 282 | return true; |
| 283 | } |
| 284 | |
| 285 | HBasicBlock* HLoopInformation::GetPreHeader() const { |
| 286 | DCHECK_EQ(header_->GetPredecessors().Size(), 2u); |
| 287 | return header_->GetDominator(); |
| 288 | } |
| 289 | |
| 290 | bool HLoopInformation::Contains(const HBasicBlock& block) const { |
| 291 | return blocks_.IsBitSet(block.GetBlockId()); |
| 292 | } |
| 293 | |
| 294 | bool HLoopInformation::IsIn(const HLoopInformation& other) const { |
| 295 | return other.blocks_.IsBitSet(header_->GetBlockId()); |
| 296 | } |
| 297 | |
| 298 | bool HBasicBlock::Dominates(HBasicBlock* other) const { |
| 299 | // Walk up the dominator tree from `other`, to find out if `this` |
| 300 | // is an ancestor. |
| 301 | HBasicBlock* current = other; |
| 302 | while (current != nullptr) { |
| 303 | if (current == this) { |
| 304 | return true; |
| 305 | } |
| 306 | current = current->GetDominator(); |
| 307 | } |
| 308 | return false; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 309 | } |
| 310 | |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 311 | void HBasicBlock::InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor) { |
| 312 | DCHECK(cursor->AsPhi() == nullptr); |
| 313 | DCHECK(instruction->AsPhi() == nullptr); |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 314 | DCHECK_EQ(instruction->GetId(), -1); |
| 315 | DCHECK_NE(cursor->GetId(), -1); |
| 316 | DCHECK_EQ(cursor->GetBlock(), this); |
| 317 | DCHECK(!instruction->IsControlFlow()); |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 318 | instruction->next_ = cursor; |
| 319 | instruction->previous_ = cursor->previous_; |
| 320 | cursor->previous_ = instruction; |
| 321 | if (GetFirstInstruction() == cursor) { |
| 322 | instructions_.first_instruction_ = instruction; |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 323 | } else { |
| 324 | instruction->previous_->next_ = instruction; |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 325 | } |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 326 | instruction->SetBlock(this); |
| 327 | instruction->SetId(GetGraph()->GetNextInstructionId()); |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 328 | } |
| 329 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 330 | void HBasicBlock::ReplaceAndRemoveInstructionWith(HInstruction* initial, |
| 331 | HInstruction* replacement) { |
| 332 | DCHECK(initial->GetBlock() == this); |
| 333 | InsertInstructionBefore(replacement, initial); |
| 334 | initial->ReplaceWith(replacement); |
| 335 | RemoveInstruction(initial); |
| 336 | } |
| 337 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 338 | static void Add(HInstructionList* instruction_list, |
| 339 | HBasicBlock* block, |
| 340 | HInstruction* instruction) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 341 | DCHECK(instruction->GetBlock() == nullptr); |
Nicolas Geoffray | 43c8642 | 2014-03-18 11:58:24 +0000 | [diff] [blame] | 342 | DCHECK_EQ(instruction->GetId(), -1); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 343 | instruction->SetBlock(block); |
| 344 | instruction->SetId(block->GetGraph()->GetNextInstructionId()); |
| 345 | instruction_list->AddInstruction(instruction); |
| 346 | } |
| 347 | |
| 348 | void HBasicBlock::AddInstruction(HInstruction* instruction) { |
| 349 | Add(&instructions_, this, instruction); |
| 350 | } |
| 351 | |
| 352 | void HBasicBlock::AddPhi(HPhi* phi) { |
| 353 | Add(&phis_, this, phi); |
| 354 | } |
| 355 | |
| 356 | static void Remove(HInstructionList* instruction_list, |
| 357 | HBasicBlock* block, |
| 358 | HInstruction* instruction) { |
| 359 | DCHECK_EQ(block, instruction->GetBlock()); |
| 360 | DCHECK(instruction->GetUses() == nullptr); |
| 361 | DCHECK(instruction->GetEnvUses() == nullptr); |
| 362 | instruction->SetBlock(nullptr); |
| 363 | instruction_list->RemoveInstruction(instruction); |
| 364 | |
| 365 | for (size_t i = 0; i < instruction->InputCount(); i++) { |
| 366 | instruction->InputAt(i)->RemoveUser(instruction, i); |
| 367 | } |
Nicolas Geoffray | 724c963 | 2014-09-22 12:27:27 +0100 | [diff] [blame] | 368 | |
| 369 | HEnvironment* environment = instruction->GetEnvironment(); |
| 370 | if (environment != nullptr) { |
| 371 | for (size_t i = 0, e = environment->Size(); i < e; ++i) { |
| 372 | HInstruction* vreg = environment->GetInstructionAt(i); |
| 373 | if (vreg != nullptr) { |
| 374 | vreg->RemoveEnvironmentUser(environment, i); |
| 375 | } |
| 376 | } |
| 377 | } |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | void HBasicBlock::RemoveInstruction(HInstruction* instruction) { |
| 381 | Remove(&instructions_, this, instruction); |
| 382 | } |
| 383 | |
| 384 | void HBasicBlock::RemovePhi(HPhi* phi) { |
| 385 | Remove(&phis_, this, phi); |
| 386 | } |
| 387 | |
Nicolas Geoffray | 724c963 | 2014-09-22 12:27:27 +0100 | [diff] [blame] | 388 | template <typename T> |
| 389 | static void RemoveFromUseList(T* user, |
| 390 | size_t input_index, |
| 391 | HUseListNode<T>** list) { |
| 392 | HUseListNode<T>* previous = nullptr; |
| 393 | HUseListNode<T>* current = *list; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 394 | while (current != nullptr) { |
| 395 | if (current->GetUser() == user && current->GetIndex() == input_index) { |
| 396 | if (previous == NULL) { |
Nicolas Geoffray | 724c963 | 2014-09-22 12:27:27 +0100 | [diff] [blame] | 397 | *list = current->GetTail(); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 398 | } else { |
| 399 | previous->SetTail(current->GetTail()); |
| 400 | } |
| 401 | } |
| 402 | previous = current; |
| 403 | current = current->GetTail(); |
| 404 | } |
| 405 | } |
| 406 | |
Nicolas Geoffray | 724c963 | 2014-09-22 12:27:27 +0100 | [diff] [blame] | 407 | void HInstruction::RemoveUser(HInstruction* user, size_t input_index) { |
| 408 | RemoveFromUseList(user, input_index, &uses_); |
| 409 | } |
| 410 | |
| 411 | void HInstruction::RemoveEnvironmentUser(HEnvironment* user, size_t input_index) { |
| 412 | RemoveFromUseList(user, input_index, &env_uses_); |
| 413 | } |
| 414 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 415 | void HInstructionList::AddInstruction(HInstruction* instruction) { |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 416 | if (first_instruction_ == nullptr) { |
| 417 | DCHECK(last_instruction_ == nullptr); |
| 418 | first_instruction_ = last_instruction_ = instruction; |
| 419 | } else { |
| 420 | last_instruction_->next_ = instruction; |
| 421 | instruction->previous_ = last_instruction_; |
| 422 | last_instruction_ = instruction; |
| 423 | } |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 424 | for (size_t i = 0; i < instruction->InputCount(); i++) { |
| 425 | instruction->InputAt(i)->AddUseAt(instruction, i); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 426 | } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 427 | } |
| 428 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 429 | void HInstructionList::RemoveInstruction(HInstruction* instruction) { |
| 430 | if (instruction->previous_ != nullptr) { |
| 431 | instruction->previous_->next_ = instruction->next_; |
| 432 | } |
| 433 | if (instruction->next_ != nullptr) { |
| 434 | instruction->next_->previous_ = instruction->previous_; |
| 435 | } |
| 436 | if (instruction == first_instruction_) { |
| 437 | first_instruction_ = instruction->next_; |
| 438 | } |
| 439 | if (instruction == last_instruction_) { |
| 440 | last_instruction_ = instruction->previous_; |
| 441 | } |
| 442 | } |
| 443 | |
Roland Levillain | 6b46923 | 2014-09-25 10:10:38 +0100 | [diff] [blame] | 444 | bool HInstructionList::Contains(HInstruction* instruction) const { |
| 445 | for (HInstructionIterator it(*this); !it.Done(); it.Advance()) { |
| 446 | if (it.Current() == instruction) { |
| 447 | return true; |
| 448 | } |
| 449 | } |
| 450 | return false; |
| 451 | } |
| 452 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 453 | bool HInstructionList::FoundBefore(const HInstruction* instruction1, |
| 454 | const HInstruction* instruction2) const { |
| 455 | DCHECK_EQ(instruction1->GetBlock(), instruction2->GetBlock()); |
| 456 | for (HInstructionIterator it(*this); !it.Done(); it.Advance()) { |
| 457 | if (it.Current() == instruction1) { |
| 458 | return true; |
| 459 | } |
| 460 | if (it.Current() == instruction2) { |
| 461 | return false; |
| 462 | } |
| 463 | } |
| 464 | LOG(FATAL) << "Did not find an order between two instructions of the same block."; |
| 465 | return true; |
| 466 | } |
| 467 | |
| 468 | bool HInstruction::Dominates(HInstruction* other_instruction) const { |
| 469 | HBasicBlock* block = GetBlock(); |
| 470 | HBasicBlock* other_block = other_instruction->GetBlock(); |
| 471 | if (block != other_block) { |
| 472 | return GetBlock()->Dominates(other_instruction->GetBlock()); |
| 473 | } else { |
| 474 | // If both instructions are in the same block, ensure this |
| 475 | // instruction comes before `other_instruction`. |
| 476 | if (IsPhi()) { |
| 477 | if (!other_instruction->IsPhi()) { |
| 478 | // Phis appear before non phi-instructions so this instruction |
| 479 | // dominates `other_instruction`. |
| 480 | return true; |
| 481 | } else { |
| 482 | // There is no order among phis. |
| 483 | LOG(FATAL) << "There is no dominance between phis of a same block."; |
| 484 | return false; |
| 485 | } |
| 486 | } else { |
| 487 | // `this` is not a phi. |
| 488 | if (other_instruction->IsPhi()) { |
| 489 | // Phis appear before non phi-instructions so this instruction |
| 490 | // does not dominate `other_instruction`. |
| 491 | return false; |
| 492 | } else { |
| 493 | // Check whether this instruction comes before |
| 494 | // `other_instruction` in the instruction list. |
| 495 | return block->GetInstructions().FoundBefore(this, other_instruction); |
| 496 | } |
| 497 | } |
| 498 | } |
| 499 | } |
| 500 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 501 | void HInstruction::ReplaceWith(HInstruction* other) { |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 502 | DCHECK(other != nullptr); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 503 | for (HUseIterator<HInstruction> it(GetUses()); !it.Done(); it.Advance()) { |
| 504 | HUseListNode<HInstruction>* current = it.Current(); |
| 505 | HInstruction* user = current->GetUser(); |
| 506 | size_t input_index = current->GetIndex(); |
| 507 | user->SetRawInputAt(input_index, other); |
| 508 | other->AddUseAt(user, input_index); |
| 509 | } |
| 510 | |
| 511 | for (HUseIterator<HEnvironment> it(GetEnvUses()); !it.Done(); it.Advance()) { |
| 512 | HUseListNode<HEnvironment>* current = it.Current(); |
| 513 | HEnvironment* user = current->GetUser(); |
| 514 | size_t input_index = current->GetIndex(); |
| 515 | user->SetRawEnvAt(input_index, other); |
| 516 | other->AddEnvUseAt(user, input_index); |
| 517 | } |
| 518 | |
| 519 | uses_ = nullptr; |
| 520 | env_uses_ = nullptr; |
| 521 | } |
| 522 | |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 523 | size_t HInstruction::EnvironmentSize() const { |
| 524 | return HasEnvironment() ? environment_->Size() : 0; |
| 525 | } |
| 526 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 527 | void HPhi::AddInput(HInstruction* input) { |
| 528 | DCHECK(input->GetBlock() != nullptr); |
| 529 | inputs_.Add(input); |
| 530 | input->AddUseAt(this, inputs_.Size() - 1); |
| 531 | } |
| 532 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 533 | #define DEFINE_ACCEPT(name) \ |
| 534 | void H##name::Accept(HGraphVisitor* visitor) { \ |
| 535 | visitor->Visit##name(this); \ |
| 536 | } |
| 537 | |
| 538 | FOR_EACH_INSTRUCTION(DEFINE_ACCEPT) |
| 539 | |
| 540 | #undef DEFINE_ACCEPT |
| 541 | |
| 542 | void HGraphVisitor::VisitInsertionOrder() { |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 543 | const GrowableArray<HBasicBlock*>& blocks = graph_->GetBlocks(); |
| 544 | for (size_t i = 0 ; i < blocks.Size(); i++) { |
| 545 | VisitBasicBlock(blocks.Get(i)); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 546 | } |
| 547 | } |
| 548 | |
| 549 | void HGraphVisitor::VisitBasicBlock(HBasicBlock* block) { |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 550 | for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) { |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 551 | it.Current()->Accept(this); |
| 552 | } |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 553 | for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) { |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 554 | it.Current()->Accept(this); |
| 555 | } |
| 556 | } |
| 557 | |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 558 | HConstant* HBinaryOperation::TryStaticEvaluation(ArenaAllocator* allocator) const { |
| 559 | if (GetLeft()->IsIntConstant() && GetRight()->IsIntConstant()) { |
| 560 | int32_t value = Evaluate(GetLeft()->AsIntConstant()->GetValue(), |
| 561 | GetRight()->AsIntConstant()->GetValue()); |
| 562 | return new(allocator) HIntConstant(value); |
| 563 | } else if (GetLeft()->IsLongConstant() && GetRight()->IsLongConstant()) { |
| 564 | int64_t value = Evaluate(GetLeft()->AsLongConstant()->GetValue(), |
| 565 | GetRight()->AsLongConstant()->GetValue()); |
| 566 | return new(allocator) HLongConstant(value); |
| 567 | } |
| 568 | return nullptr; |
| 569 | } |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 570 | |
| 571 | bool HCondition::NeedsMaterialization() const { |
| 572 | if (!HasOnlyOneUse()) { |
| 573 | return true; |
| 574 | } |
| 575 | HUseListNode<HInstruction>* uses = GetUses(); |
| 576 | HInstruction* user = uses->GetUser(); |
| 577 | if (!user->IsIf()) { |
| 578 | return true; |
| 579 | } |
| 580 | |
Nicolas Geoffray | 18efde5 | 2014-09-22 15:51:11 +0100 | [diff] [blame] | 581 | // TODO: if there is no intervening instructions with side-effect between this condition |
| 582 | // and the If instruction, we should move the condition just before the If. |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 583 | if (GetNext() != user) { |
| 584 | return true; |
| 585 | } |
| 586 | return false; |
| 587 | } |
| 588 | |
Nicolas Geoffray | 18efde5 | 2014-09-22 15:51:11 +0100 | [diff] [blame] | 589 | bool HCondition::IsBeforeWhenDisregardMoves(HIf* if_) const { |
| 590 | HInstruction* previous = if_->GetPrevious(); |
| 591 | while (previous != nullptr && previous->IsParallelMove()) { |
| 592 | previous = previous->GetPrevious(); |
| 593 | } |
| 594 | return previous == this; |
| 595 | } |
| 596 | |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 597 | bool HInstruction::Equals(HInstruction* other) const { |
| 598 | if (!InstructionTypeEquals(other)) return false; |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 599 | DCHECK_EQ(GetKind(), other->GetKind()); |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 600 | if (!InstructionDataEquals(other)) return false; |
| 601 | if (GetType() != other->GetType()) return false; |
| 602 | if (InputCount() != other->InputCount()) return false; |
| 603 | |
| 604 | for (size_t i = 0, e = InputCount(); i < e; ++i) { |
| 605 | if (InputAt(i) != other->InputAt(i)) return false; |
| 606 | } |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 607 | DCHECK_EQ(ComputeHashCode(), other->ComputeHashCode()); |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 608 | return true; |
| 609 | } |
| 610 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 611 | } // namespace art |