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" |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 18 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 19 | #include "ssa_builder.h" |
David Brazdil | a4b8c21 | 2015-05-07 09:59:30 +0100 | [diff] [blame^] | 20 | #include "base/bit_vector-inl.h" |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 21 | #include "utils/growable_array.h" |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 22 | #include "scoped_thread_state_change.h" |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 23 | |
| 24 | namespace art { |
| 25 | |
| 26 | void HGraph::AddBlock(HBasicBlock* block) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 27 | block->SetBlockId(blocks_.Size()); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 28 | blocks_.Add(block); |
| 29 | } |
| 30 | |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 31 | void HGraph::FindBackEdges(ArenaBitVector* visited) { |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 32 | ArenaBitVector visiting(arena_, blocks_.Size(), false); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 33 | VisitBlockForBackEdges(entry_block_, visited, &visiting); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 34 | } |
| 35 | |
Roland Levillain | fc600dc | 2014-12-02 17:16:31 +0000 | [diff] [blame] | 36 | static void RemoveAsUser(HInstruction* instruction) { |
| 37 | for (size_t i = 0; i < instruction->InputCount(); i++) { |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 38 | instruction->RemoveAsUserOfInput(i); |
Roland Levillain | fc600dc | 2014-12-02 17:16:31 +0000 | [diff] [blame] | 39 | } |
| 40 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 41 | for (HEnvironment* environment = instruction->GetEnvironment(); |
| 42 | environment != nullptr; |
| 43 | environment = environment->GetParent()) { |
Roland Levillain | fc600dc | 2014-12-02 17:16:31 +0000 | [diff] [blame] | 44 | for (size_t i = 0, e = environment->Size(); i < e; ++i) { |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 45 | if (environment->GetInstructionAt(i) != nullptr) { |
| 46 | environment->RemoveAsUserOfInput(i); |
Roland Levillain | fc600dc | 2014-12-02 17:16:31 +0000 | [diff] [blame] | 47 | } |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | void HGraph::RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const { |
| 53 | for (size_t i = 0; i < blocks_.Size(); ++i) { |
| 54 | if (!visited.IsBitSet(i)) { |
| 55 | HBasicBlock* block = blocks_.Get(i); |
Nicolas Geoffray | f776b92 | 2015-04-15 18:22:45 +0100 | [diff] [blame] | 56 | DCHECK(block->GetPhis().IsEmpty()) << "Phis are not inserted at this stage"; |
Roland Levillain | fc600dc | 2014-12-02 17:16:31 +0000 | [diff] [blame] | 57 | for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) { |
| 58 | RemoveAsUser(it.Current()); |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
Nicolas Geoffray | f776b92 | 2015-04-15 18:22:45 +0100 | [diff] [blame] | 64 | void HGraph::RemoveDeadBlocks(const ArenaBitVector& visited) { |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 65 | for (size_t i = 0; i < blocks_.Size(); ++i) { |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 66 | if (!visited.IsBitSet(i)) { |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 67 | HBasicBlock* block = blocks_.Get(i); |
Nicolas Geoffray | f776b92 | 2015-04-15 18:22:45 +0100 | [diff] [blame] | 68 | // We only need to update the successor, which might be live. |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 69 | for (size_t j = 0; j < block->GetSuccessors().Size(); ++j) { |
| 70 | block->GetSuccessors().Get(j)->RemovePredecessor(block); |
| 71 | } |
Nicolas Geoffray | f776b92 | 2015-04-15 18:22:45 +0100 | [diff] [blame] | 72 | // Remove the block from the list of blocks, so that further analyses |
| 73 | // never see it. |
| 74 | blocks_.Put(i, nullptr); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | void HGraph::VisitBlockForBackEdges(HBasicBlock* block, |
| 80 | ArenaBitVector* visited, |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 81 | ArenaBitVector* visiting) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 82 | int id = block->GetBlockId(); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 83 | if (visited->IsBitSet(id)) return; |
| 84 | |
| 85 | visited->SetBit(id); |
| 86 | visiting->SetBit(id); |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 87 | for (size_t i = 0; i < block->GetSuccessors().Size(); i++) { |
| 88 | HBasicBlock* successor = block->GetSuccessors().Get(i); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 89 | if (visiting->IsBitSet(successor->GetBlockId())) { |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 90 | successor->AddBackEdge(block); |
| 91 | } else { |
| 92 | VisitBlockForBackEdges(successor, visited, visiting); |
| 93 | } |
| 94 | } |
| 95 | visiting->ClearBit(id); |
| 96 | } |
| 97 | |
| 98 | void HGraph::BuildDominatorTree() { |
| 99 | ArenaBitVector visited(arena_, blocks_.Size(), false); |
| 100 | |
| 101 | // (1) Find the back edges in the graph doing a DFS traversal. |
| 102 | FindBackEdges(&visited); |
| 103 | |
Roland Levillain | fc600dc | 2014-12-02 17:16:31 +0000 | [diff] [blame] | 104 | // (2) Remove instructions and phis from blocks not visited during |
| 105 | // the initial DFS as users from other instructions, so that |
| 106 | // users can be safely removed before uses later. |
| 107 | RemoveInstructionsAsUsersFromDeadBlocks(visited); |
| 108 | |
| 109 | // (3) Remove blocks not visited during the initial DFS. |
| 110 | // Step (4) requires dead blocks to be removed from the |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 111 | // predecessors list of live blocks. |
| 112 | RemoveDeadBlocks(visited); |
| 113 | |
Roland Levillain | fc600dc | 2014-12-02 17:16:31 +0000 | [diff] [blame] | 114 | // (4) Simplify the CFG now, so that we don't need to recompute |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 115 | // dominators and the reverse post order. |
| 116 | SimplifyCFG(); |
| 117 | |
Roland Levillain | fc600dc | 2014-12-02 17:16:31 +0000 | [diff] [blame] | 118 | // (5) Compute the immediate dominator of each block. We visit |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 119 | // the successors of a block only when all its forward branches |
| 120 | // have been processed. |
| 121 | GrowableArray<size_t> visits(arena_, blocks_.Size()); |
| 122 | visits.SetSize(blocks_.Size()); |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 123 | reverse_post_order_.Add(entry_block_); |
| 124 | for (size_t i = 0; i < entry_block_->GetSuccessors().Size(); i++) { |
| 125 | VisitBlockForDominatorTree(entry_block_->GetSuccessors().Get(i), entry_block_, &visits); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | |
| 129 | HBasicBlock* HGraph::FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const { |
| 130 | ArenaBitVector visited(arena_, blocks_.Size(), false); |
| 131 | // Walk the dominator tree of the first block and mark the visited blocks. |
| 132 | while (first != nullptr) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 133 | visited.SetBit(first->GetBlockId()); |
| 134 | first = first->GetDominator(); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 135 | } |
| 136 | // Walk the dominator tree of the second block until a marked block is found. |
| 137 | while (second != nullptr) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 138 | if (visited.IsBitSet(second->GetBlockId())) { |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 139 | return second; |
| 140 | } |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 141 | second = second->GetDominator(); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 142 | } |
| 143 | LOG(ERROR) << "Could not find common dominator"; |
| 144 | return nullptr; |
| 145 | } |
| 146 | |
| 147 | void HGraph::VisitBlockForDominatorTree(HBasicBlock* block, |
| 148 | HBasicBlock* predecessor, |
| 149 | GrowableArray<size_t>* visits) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 150 | if (block->GetDominator() == nullptr) { |
| 151 | block->SetDominator(predecessor); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 152 | } else { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 153 | block->SetDominator(FindCommonDominator(block->GetDominator(), predecessor)); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 156 | visits->Increment(block->GetBlockId()); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 157 | // Once all the forward edges have been visited, we know the immediate |
| 158 | // dominator of the block. We can then start visiting its successors. |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 159 | if (visits->Get(block->GetBlockId()) == |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 160 | block->GetPredecessors().Size() - block->NumberOfBackEdges()) { |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 161 | block->GetDominator()->AddDominatedBlock(block); |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 162 | reverse_post_order_.Add(block); |
| 163 | for (size_t i = 0; i < block->GetSuccessors().Size(); i++) { |
| 164 | VisitBlockForDominatorTree(block->GetSuccessors().Get(i), block, visits); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 169 | void HGraph::TransformToSsa() { |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 170 | DCHECK(!reverse_post_order_.IsEmpty()); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 171 | SsaBuilder ssa_builder(this); |
| 172 | ssa_builder.BuildSsa(); |
| 173 | } |
| 174 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 175 | void HGraph::SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor) { |
| 176 | // Insert a new node between `block` and `successor` to split the |
| 177 | // critical edge. |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 178 | HBasicBlock* new_block = new (arena_) HBasicBlock(this, successor->GetDexPc()); |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 179 | AddBlock(new_block); |
| 180 | new_block->AddInstruction(new (arena_) HGoto()); |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 181 | block->ReplaceSuccessor(successor, new_block); |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 182 | new_block->AddSuccessor(successor); |
| 183 | if (successor->IsLoopHeader()) { |
| 184 | // If we split at a back edge boundary, make the new block the back edge. |
| 185 | HLoopInformation* info = successor->GetLoopInformation(); |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 186 | if (info->IsBackEdge(*block)) { |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 187 | info->RemoveBackEdge(block); |
| 188 | info->AddBackEdge(new_block); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | } |
| 192 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 193 | void HGraph::SimplifyLoop(HBasicBlock* header) { |
| 194 | HLoopInformation* info = header->GetLoopInformation(); |
| 195 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 196 | // Make sure the loop has only one pre header. This simplifies SSA building by having |
| 197 | // to just look at the pre header to know which locals are initialized at entry of the |
| 198 | // loop. |
| 199 | size_t number_of_incomings = header->GetPredecessors().Size() - info->NumberOfBackEdges(); |
| 200 | if (number_of_incomings != 1) { |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 201 | HBasicBlock* pre_header = new (arena_) HBasicBlock(this, header->GetDexPc()); |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 202 | AddBlock(pre_header); |
| 203 | pre_header->AddInstruction(new (arena_) HGoto()); |
| 204 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 205 | for (size_t pred = 0; pred < header->GetPredecessors().Size(); ++pred) { |
| 206 | HBasicBlock* predecessor = header->GetPredecessors().Get(pred); |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 207 | if (!info->IsBackEdge(*predecessor)) { |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 208 | predecessor->ReplaceSuccessor(header, pre_header); |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 209 | pred--; |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | pre_header->AddSuccessor(header); |
| 213 | } |
Nicolas Geoffray | 604c6e4 | 2014-09-17 12:08:44 +0100 | [diff] [blame] | 214 | |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 215 | // Make sure the first predecessor of a loop header is the incoming block. |
| 216 | if (info->IsBackEdge(*header->GetPredecessors().Get(0))) { |
| 217 | HBasicBlock* to_swap = header->GetPredecessors().Get(0); |
| 218 | for (size_t pred = 1, e = header->GetPredecessors().Size(); pred < e; ++pred) { |
| 219 | HBasicBlock* predecessor = header->GetPredecessors().Get(pred); |
| 220 | if (!info->IsBackEdge(*predecessor)) { |
| 221 | header->predecessors_.Put(pred, to_swap); |
| 222 | header->predecessors_.Put(0, predecessor); |
| 223 | break; |
| 224 | } |
| 225 | } |
Nicolas Geoffray | 604c6e4 | 2014-09-17 12:08:44 +0100 | [diff] [blame] | 226 | } |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 227 | |
| 228 | // Place the suspend check at the beginning of the header, so that live registers |
| 229 | // will be known when allocating registers. Note that code generation can still |
| 230 | // generate the suspend check at the back edge, but needs to be careful with |
| 231 | // loop phi spill slots (which are not written to at back edge). |
| 232 | HInstruction* first_instruction = header->GetFirstInstruction(); |
| 233 | if (!first_instruction->IsSuspendCheck()) { |
| 234 | HSuspendCheck* check = new (arena_) HSuspendCheck(header->GetDexPc()); |
| 235 | header->InsertInstructionBefore(check, first_instruction); |
| 236 | first_instruction = check; |
| 237 | } |
| 238 | info->SetSuspendCheck(first_instruction->AsSuspendCheck()); |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | void HGraph::SimplifyCFG() { |
| 242 | // Simplify the CFG for future analysis, and code generation: |
| 243 | // (1): Split critical edges. |
| 244 | // (2): Simplify loops by having only one back edge, and one preheader. |
| 245 | for (size_t i = 0; i < blocks_.Size(); ++i) { |
| 246 | HBasicBlock* block = blocks_.Get(i); |
Nicolas Geoffray | f776b92 | 2015-04-15 18:22:45 +0100 | [diff] [blame] | 247 | if (block == nullptr) continue; |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 248 | if (block->GetSuccessors().Size() > 1) { |
| 249 | for (size_t j = 0; j < block->GetSuccessors().Size(); ++j) { |
| 250 | HBasicBlock* successor = block->GetSuccessors().Get(j); |
| 251 | if (successor->GetPredecessors().Size() > 1) { |
| 252 | SplitCriticalEdge(block, successor); |
| 253 | --j; |
| 254 | } |
| 255 | } |
| 256 | } |
| 257 | if (block->IsLoopHeader()) { |
| 258 | SimplifyLoop(block); |
| 259 | } |
| 260 | } |
| 261 | } |
| 262 | |
Nicolas Geoffray | f537012 | 2014-12-02 11:51:19 +0000 | [diff] [blame] | 263 | bool HGraph::AnalyzeNaturalLoops() const { |
Nicolas Geoffray | f776b92 | 2015-04-15 18:22:45 +0100 | [diff] [blame] | 264 | // Order does not matter. |
| 265 | for (HReversePostOrderIterator it(*this); !it.Done(); it.Advance()) { |
| 266 | HBasicBlock* block = it.Current(); |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 267 | if (block->IsLoopHeader()) { |
| 268 | HLoopInformation* info = block->GetLoopInformation(); |
| 269 | if (!info->Populate()) { |
| 270 | // Abort if the loop is non natural. We currently bailout in such cases. |
| 271 | return false; |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | return true; |
| 276 | } |
| 277 | |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 278 | void HGraph::InsertConstant(HConstant* constant) { |
| 279 | // New constants are inserted before the final control-flow instruction |
| 280 | // of the graph, or at its end if called from the graph builder. |
| 281 | if (entry_block_->EndsWithControlFlowInstruction()) { |
| 282 | entry_block_->InsertInstructionBefore(constant, entry_block_->GetLastInstruction()); |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 283 | } else { |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 284 | entry_block_->AddInstruction(constant); |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 285 | } |
| 286 | } |
| 287 | |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 288 | HNullConstant* HGraph::GetNullConstant() { |
| 289 | if (cached_null_constant_ == nullptr) { |
| 290 | cached_null_constant_ = new (arena_) HNullConstant(); |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 291 | InsertConstant(cached_null_constant_); |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 292 | } |
| 293 | return cached_null_constant_; |
| 294 | } |
| 295 | |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 296 | HConstant* HGraph::GetConstant(Primitive::Type type, int64_t value) { |
| 297 | switch (type) { |
| 298 | case Primitive::Type::kPrimBoolean: |
| 299 | DCHECK(IsUint<1>(value)); |
| 300 | FALLTHROUGH_INTENDED; |
| 301 | case Primitive::Type::kPrimByte: |
| 302 | case Primitive::Type::kPrimChar: |
| 303 | case Primitive::Type::kPrimShort: |
| 304 | case Primitive::Type::kPrimInt: |
| 305 | DCHECK(IsInt(Primitive::ComponentSize(type) * kBitsPerByte, value)); |
| 306 | return GetIntConstant(static_cast<int32_t>(value)); |
| 307 | |
| 308 | case Primitive::Type::kPrimLong: |
| 309 | return GetLongConstant(value); |
| 310 | |
| 311 | default: |
| 312 | LOG(FATAL) << "Unsupported constant type"; |
| 313 | UNREACHABLE(); |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 314 | } |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 315 | } |
| 316 | |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 317 | void HGraph::CacheFloatConstant(HFloatConstant* constant) { |
| 318 | int32_t value = bit_cast<int32_t, float>(constant->GetValue()); |
| 319 | DCHECK(cached_float_constants_.find(value) == cached_float_constants_.end()); |
| 320 | cached_float_constants_.Overwrite(value, constant); |
| 321 | } |
| 322 | |
| 323 | void HGraph::CacheDoubleConstant(HDoubleConstant* constant) { |
| 324 | int64_t value = bit_cast<int64_t, double>(constant->GetValue()); |
| 325 | DCHECK(cached_double_constants_.find(value) == cached_double_constants_.end()); |
| 326 | cached_double_constants_.Overwrite(value, constant); |
| 327 | } |
| 328 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 329 | void HLoopInformation::Add(HBasicBlock* block) { |
| 330 | blocks_.SetBit(block->GetBlockId()); |
| 331 | } |
| 332 | |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 333 | void HLoopInformation::Remove(HBasicBlock* block) { |
| 334 | blocks_.ClearBit(block->GetBlockId()); |
| 335 | } |
| 336 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 337 | void HLoopInformation::PopulateRecursive(HBasicBlock* block) { |
| 338 | if (blocks_.IsBitSet(block->GetBlockId())) { |
| 339 | return; |
| 340 | } |
| 341 | |
| 342 | blocks_.SetBit(block->GetBlockId()); |
| 343 | block->SetInLoop(this); |
| 344 | for (size_t i = 0, e = block->GetPredecessors().Size(); i < e; ++i) { |
| 345 | PopulateRecursive(block->GetPredecessors().Get(i)); |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | bool HLoopInformation::Populate() { |
David Brazdil | a4b8c21 | 2015-05-07 09:59:30 +0100 | [diff] [blame^] | 350 | DCHECK_EQ(blocks_.NumSetBits(), 0u) << "Loop information has already been populated"; |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 351 | for (size_t i = 0, e = GetBackEdges().Size(); i < e; ++i) { |
| 352 | HBasicBlock* back_edge = GetBackEdges().Get(i); |
| 353 | DCHECK(back_edge->GetDominator() != nullptr); |
| 354 | if (!header_->Dominates(back_edge)) { |
| 355 | // This loop is not natural. Do not bother going further. |
| 356 | return false; |
| 357 | } |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 358 | |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 359 | // Populate this loop: starting with the back edge, recursively add predecessors |
| 360 | // that are not already part of that loop. Set the header as part of the loop |
| 361 | // to end the recursion. |
| 362 | // This is a recursive implementation of the algorithm described in |
| 363 | // "Advanced Compiler Design & Implementation" (Muchnick) p192. |
| 364 | blocks_.SetBit(header_->GetBlockId()); |
| 365 | PopulateRecursive(back_edge); |
| 366 | } |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 367 | return true; |
| 368 | } |
| 369 | |
David Brazdil | a4b8c21 | 2015-05-07 09:59:30 +0100 | [diff] [blame^] | 370 | void HLoopInformation::Update() { |
| 371 | HGraph* graph = header_->GetGraph(); |
| 372 | for (uint32_t id : blocks_.Indexes()) { |
| 373 | HBasicBlock* block = graph->GetBlocks().Get(id); |
| 374 | // Reset loop information of non-header blocks inside the loop, except |
| 375 | // members of inner nested loops because those should already have been |
| 376 | // updated by their own LoopInformation. |
| 377 | if (block->GetLoopInformation() == this && block != header_) { |
| 378 | block->SetLoopInformation(nullptr); |
| 379 | } |
| 380 | } |
| 381 | blocks_.ClearAllBits(); |
| 382 | |
| 383 | if (back_edges_.IsEmpty()) { |
| 384 | // The loop has been dismantled, delete its suspend check and remove info |
| 385 | // from the header. |
| 386 | DCHECK(HasSuspendCheck()); |
| 387 | header_->RemoveInstruction(suspend_check_); |
| 388 | header_->SetLoopInformation(nullptr); |
| 389 | header_ = nullptr; |
| 390 | suspend_check_ = nullptr; |
| 391 | } else { |
| 392 | if (kIsDebugBuild) { |
| 393 | for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) { |
| 394 | DCHECK(header_->Dominates(back_edges_.Get(i))); |
| 395 | } |
| 396 | } |
| 397 | // This loop still has reachable back edges. Repopulate the list of blocks. |
| 398 | bool populate_successful = Populate(); |
| 399 | DCHECK(populate_successful); |
| 400 | } |
| 401 | } |
| 402 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 403 | HBasicBlock* HLoopInformation::GetPreHeader() const { |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 404 | return header_->GetDominator(); |
| 405 | } |
| 406 | |
| 407 | bool HLoopInformation::Contains(const HBasicBlock& block) const { |
| 408 | return blocks_.IsBitSet(block.GetBlockId()); |
| 409 | } |
| 410 | |
| 411 | bool HLoopInformation::IsIn(const HLoopInformation& other) const { |
| 412 | return other.blocks_.IsBitSet(header_->GetBlockId()); |
| 413 | } |
| 414 | |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 415 | size_t HLoopInformation::GetLifetimeEnd() const { |
| 416 | size_t last_position = 0; |
| 417 | for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) { |
| 418 | last_position = std::max(back_edges_.Get(i)->GetLifetimeEnd(), last_position); |
| 419 | } |
| 420 | return last_position; |
| 421 | } |
| 422 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 423 | bool HBasicBlock::Dominates(HBasicBlock* other) const { |
| 424 | // Walk up the dominator tree from `other`, to find out if `this` |
| 425 | // is an ancestor. |
| 426 | HBasicBlock* current = other; |
| 427 | while (current != nullptr) { |
| 428 | if (current == this) { |
| 429 | return true; |
| 430 | } |
| 431 | current = current->GetDominator(); |
| 432 | } |
| 433 | return false; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 434 | } |
| 435 | |
Nicolas Geoffray | 191c4b1 | 2014-10-07 14:14:27 +0100 | [diff] [blame] | 436 | static void UpdateInputsUsers(HInstruction* instruction) { |
| 437 | for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) { |
| 438 | instruction->InputAt(i)->AddUseAt(instruction, i); |
| 439 | } |
| 440 | // Environment should be created later. |
| 441 | DCHECK(!instruction->HasEnvironment()); |
| 442 | } |
| 443 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 444 | void HBasicBlock::ReplaceAndRemoveInstructionWith(HInstruction* initial, |
| 445 | HInstruction* replacement) { |
| 446 | DCHECK(initial->GetBlock() == this); |
| 447 | InsertInstructionBefore(replacement, initial); |
| 448 | initial->ReplaceWith(replacement); |
| 449 | RemoveInstruction(initial); |
| 450 | } |
| 451 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 452 | static void Add(HInstructionList* instruction_list, |
| 453 | HBasicBlock* block, |
| 454 | HInstruction* instruction) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 455 | DCHECK(instruction->GetBlock() == nullptr); |
Nicolas Geoffray | 43c8642 | 2014-03-18 11:58:24 +0000 | [diff] [blame] | 456 | DCHECK_EQ(instruction->GetId(), -1); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 457 | instruction->SetBlock(block); |
| 458 | instruction->SetId(block->GetGraph()->GetNextInstructionId()); |
Nicolas Geoffray | 191c4b1 | 2014-10-07 14:14:27 +0100 | [diff] [blame] | 459 | UpdateInputsUsers(instruction); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 460 | instruction_list->AddInstruction(instruction); |
| 461 | } |
| 462 | |
| 463 | void HBasicBlock::AddInstruction(HInstruction* instruction) { |
| 464 | Add(&instructions_, this, instruction); |
| 465 | } |
| 466 | |
| 467 | void HBasicBlock::AddPhi(HPhi* phi) { |
| 468 | Add(&phis_, this, phi); |
| 469 | } |
| 470 | |
David Brazdil | c3d743f | 2015-04-22 13:40:50 +0100 | [diff] [blame] | 471 | void HBasicBlock::InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor) { |
| 472 | DCHECK(!cursor->IsPhi()); |
| 473 | DCHECK(!instruction->IsPhi()); |
| 474 | DCHECK_EQ(instruction->GetId(), -1); |
| 475 | DCHECK_NE(cursor->GetId(), -1); |
| 476 | DCHECK_EQ(cursor->GetBlock(), this); |
| 477 | DCHECK(!instruction->IsControlFlow()); |
| 478 | instruction->SetBlock(this); |
| 479 | instruction->SetId(GetGraph()->GetNextInstructionId()); |
| 480 | UpdateInputsUsers(instruction); |
| 481 | instructions_.InsertInstructionBefore(instruction, cursor); |
| 482 | } |
| 483 | |
Guillaume "Vermeille" Sanchez | 2967ec6 | 2015-04-24 16:36:52 +0100 | [diff] [blame] | 484 | void HBasicBlock::InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor) { |
| 485 | DCHECK(!cursor->IsPhi()); |
| 486 | DCHECK(!instruction->IsPhi()); |
| 487 | DCHECK_EQ(instruction->GetId(), -1); |
| 488 | DCHECK_NE(cursor->GetId(), -1); |
| 489 | DCHECK_EQ(cursor->GetBlock(), this); |
| 490 | DCHECK(!instruction->IsControlFlow()); |
| 491 | DCHECK(!cursor->IsControlFlow()); |
| 492 | instruction->SetBlock(this); |
| 493 | instruction->SetId(GetGraph()->GetNextInstructionId()); |
| 494 | UpdateInputsUsers(instruction); |
| 495 | instructions_.InsertInstructionAfter(instruction, cursor); |
| 496 | } |
| 497 | |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 498 | void HBasicBlock::InsertPhiAfter(HPhi* phi, HPhi* cursor) { |
| 499 | DCHECK_EQ(phi->GetId(), -1); |
| 500 | DCHECK_NE(cursor->GetId(), -1); |
| 501 | DCHECK_EQ(cursor->GetBlock(), this); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 502 | phi->SetBlock(this); |
| 503 | phi->SetId(GetGraph()->GetNextInstructionId()); |
| 504 | UpdateInputsUsers(phi); |
David Brazdil | c3d743f | 2015-04-22 13:40:50 +0100 | [diff] [blame] | 505 | phis_.InsertInstructionAfter(phi, cursor); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 506 | } |
| 507 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 508 | static void Remove(HInstructionList* instruction_list, |
| 509 | HBasicBlock* block, |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 510 | HInstruction* instruction, |
| 511 | bool ensure_safety) { |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 512 | DCHECK_EQ(block, instruction->GetBlock()); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 513 | instruction->SetBlock(nullptr); |
| 514 | instruction_list->RemoveInstruction(instruction); |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 515 | if (ensure_safety) { |
| 516 | DCHECK(instruction->GetUses().IsEmpty()); |
| 517 | DCHECK(instruction->GetEnvUses().IsEmpty()); |
| 518 | RemoveAsUser(instruction); |
| 519 | } |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 520 | } |
| 521 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 522 | void HBasicBlock::RemoveInstruction(HInstruction* instruction, bool ensure_safety) { |
David Brazdil | c7508e9 | 2015-04-27 13:28:57 +0100 | [diff] [blame] | 523 | DCHECK(!instruction->IsPhi()); |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 524 | Remove(&instructions_, this, instruction, ensure_safety); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 525 | } |
| 526 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 527 | void HBasicBlock::RemovePhi(HPhi* phi, bool ensure_safety) { |
| 528 | Remove(&phis_, this, phi, ensure_safety); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 529 | } |
| 530 | |
David Brazdil | c7508e9 | 2015-04-27 13:28:57 +0100 | [diff] [blame] | 531 | void HBasicBlock::RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety) { |
| 532 | if (instruction->IsPhi()) { |
| 533 | RemovePhi(instruction->AsPhi(), ensure_safety); |
| 534 | } else { |
| 535 | RemoveInstruction(instruction, ensure_safety); |
| 536 | } |
| 537 | } |
| 538 | |
Nicolas Geoffray | 8c0c91a | 2015-05-07 11:46:05 +0100 | [diff] [blame] | 539 | void HEnvironment::CopyFrom(const GrowableArray<HInstruction*>& locals) { |
| 540 | for (size_t i = 0; i < locals.Size(); i++) { |
| 541 | HInstruction* instruction = locals.Get(i); |
| 542 | SetRawEnvAt(i, instruction); |
| 543 | if (instruction != nullptr) { |
| 544 | instruction->AddEnvUseAt(this, i); |
| 545 | } |
| 546 | } |
| 547 | } |
| 548 | |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 549 | void HEnvironment::CopyFrom(HEnvironment* env) { |
| 550 | for (size_t i = 0; i < env->Size(); i++) { |
| 551 | HInstruction* instruction = env->GetInstructionAt(i); |
| 552 | SetRawEnvAt(i, instruction); |
| 553 | if (instruction != nullptr) { |
| 554 | instruction->AddEnvUseAt(this, i); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 555 | } |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 556 | } |
| 557 | } |
| 558 | |
Mingyao Yang | 206d6fd | 2015-04-13 16:46:28 -0700 | [diff] [blame] | 559 | void HEnvironment::CopyFromWithLoopPhiAdjustment(HEnvironment* env, |
| 560 | HBasicBlock* loop_header) { |
| 561 | DCHECK(loop_header->IsLoopHeader()); |
| 562 | for (size_t i = 0; i < env->Size(); i++) { |
| 563 | HInstruction* instruction = env->GetInstructionAt(i); |
| 564 | SetRawEnvAt(i, instruction); |
| 565 | if (instruction == nullptr) { |
| 566 | continue; |
| 567 | } |
| 568 | if (instruction->IsLoopHeaderPhi() && (instruction->GetBlock() == loop_header)) { |
| 569 | // At the end of the loop pre-header, the corresponding value for instruction |
| 570 | // is the first input of the phi. |
| 571 | HInstruction* initial = instruction->AsPhi()->InputAt(0); |
| 572 | DCHECK(initial->GetBlock()->Dominates(loop_header)); |
| 573 | SetRawEnvAt(i, initial); |
| 574 | initial->AddEnvUseAt(this, i); |
| 575 | } else { |
| 576 | instruction->AddEnvUseAt(this, i); |
| 577 | } |
| 578 | } |
| 579 | } |
| 580 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 581 | void HEnvironment::RemoveAsUserOfInput(size_t index) const { |
| 582 | const HUserRecord<HEnvironment*> user_record = vregs_.Get(index); |
| 583 | user_record.GetInstruction()->RemoveEnvironmentUser(user_record.GetUseNode()); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 584 | } |
| 585 | |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 586 | HInstruction* HInstruction::GetNextDisregardingMoves() const { |
| 587 | HInstruction* next = GetNext(); |
| 588 | while (next != nullptr && next->IsParallelMove()) { |
| 589 | next = next->GetNext(); |
| 590 | } |
| 591 | return next; |
| 592 | } |
| 593 | |
| 594 | HInstruction* HInstruction::GetPreviousDisregardingMoves() const { |
| 595 | HInstruction* previous = GetPrevious(); |
| 596 | while (previous != nullptr && previous->IsParallelMove()) { |
| 597 | previous = previous->GetPrevious(); |
| 598 | } |
| 599 | return previous; |
| 600 | } |
| 601 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 602 | void HInstructionList::AddInstruction(HInstruction* instruction) { |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 603 | if (first_instruction_ == nullptr) { |
| 604 | DCHECK(last_instruction_ == nullptr); |
| 605 | first_instruction_ = last_instruction_ = instruction; |
| 606 | } else { |
| 607 | last_instruction_->next_ = instruction; |
| 608 | instruction->previous_ = last_instruction_; |
| 609 | last_instruction_ = instruction; |
| 610 | } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 611 | } |
| 612 | |
David Brazdil | c3d743f | 2015-04-22 13:40:50 +0100 | [diff] [blame] | 613 | void HInstructionList::InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor) { |
| 614 | DCHECK(Contains(cursor)); |
| 615 | if (cursor == first_instruction_) { |
| 616 | cursor->previous_ = instruction; |
| 617 | instruction->next_ = cursor; |
| 618 | first_instruction_ = instruction; |
| 619 | } else { |
| 620 | instruction->previous_ = cursor->previous_; |
| 621 | instruction->next_ = cursor; |
| 622 | cursor->previous_ = instruction; |
| 623 | instruction->previous_->next_ = instruction; |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | void HInstructionList::InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor) { |
| 628 | DCHECK(Contains(cursor)); |
| 629 | if (cursor == last_instruction_) { |
| 630 | cursor->next_ = instruction; |
| 631 | instruction->previous_ = cursor; |
| 632 | last_instruction_ = instruction; |
| 633 | } else { |
| 634 | instruction->next_ = cursor->next_; |
| 635 | instruction->previous_ = cursor; |
| 636 | cursor->next_ = instruction; |
| 637 | instruction->next_->previous_ = instruction; |
| 638 | } |
| 639 | } |
| 640 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 641 | void HInstructionList::RemoveInstruction(HInstruction* instruction) { |
| 642 | if (instruction->previous_ != nullptr) { |
| 643 | instruction->previous_->next_ = instruction->next_; |
| 644 | } |
| 645 | if (instruction->next_ != nullptr) { |
| 646 | instruction->next_->previous_ = instruction->previous_; |
| 647 | } |
| 648 | if (instruction == first_instruction_) { |
| 649 | first_instruction_ = instruction->next_; |
| 650 | } |
| 651 | if (instruction == last_instruction_) { |
| 652 | last_instruction_ = instruction->previous_; |
| 653 | } |
| 654 | } |
| 655 | |
Roland Levillain | 6b46923 | 2014-09-25 10:10:38 +0100 | [diff] [blame] | 656 | bool HInstructionList::Contains(HInstruction* instruction) const { |
| 657 | for (HInstructionIterator it(*this); !it.Done(); it.Advance()) { |
| 658 | if (it.Current() == instruction) { |
| 659 | return true; |
| 660 | } |
| 661 | } |
| 662 | return false; |
| 663 | } |
| 664 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 665 | bool HInstructionList::FoundBefore(const HInstruction* instruction1, |
| 666 | const HInstruction* instruction2) const { |
| 667 | DCHECK_EQ(instruction1->GetBlock(), instruction2->GetBlock()); |
| 668 | for (HInstructionIterator it(*this); !it.Done(); it.Advance()) { |
| 669 | if (it.Current() == instruction1) { |
| 670 | return true; |
| 671 | } |
| 672 | if (it.Current() == instruction2) { |
| 673 | return false; |
| 674 | } |
| 675 | } |
| 676 | LOG(FATAL) << "Did not find an order between two instructions of the same block."; |
| 677 | return true; |
| 678 | } |
| 679 | |
Roland Levillain | 6c82d40 | 2014-10-13 16:10:27 +0100 | [diff] [blame] | 680 | bool HInstruction::StrictlyDominates(HInstruction* other_instruction) const { |
| 681 | if (other_instruction == this) { |
| 682 | // An instruction does not strictly dominate itself. |
| 683 | return false; |
| 684 | } |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 685 | HBasicBlock* block = GetBlock(); |
| 686 | HBasicBlock* other_block = other_instruction->GetBlock(); |
| 687 | if (block != other_block) { |
| 688 | return GetBlock()->Dominates(other_instruction->GetBlock()); |
| 689 | } else { |
| 690 | // If both instructions are in the same block, ensure this |
| 691 | // instruction comes before `other_instruction`. |
| 692 | if (IsPhi()) { |
| 693 | if (!other_instruction->IsPhi()) { |
| 694 | // Phis appear before non phi-instructions so this instruction |
| 695 | // dominates `other_instruction`. |
| 696 | return true; |
| 697 | } else { |
| 698 | // There is no order among phis. |
| 699 | LOG(FATAL) << "There is no dominance between phis of a same block."; |
| 700 | return false; |
| 701 | } |
| 702 | } else { |
| 703 | // `this` is not a phi. |
| 704 | if (other_instruction->IsPhi()) { |
| 705 | // Phis appear before non phi-instructions so this instruction |
| 706 | // does not dominate `other_instruction`. |
| 707 | return false; |
| 708 | } else { |
| 709 | // Check whether this instruction comes before |
| 710 | // `other_instruction` in the instruction list. |
| 711 | return block->GetInstructions().FoundBefore(this, other_instruction); |
| 712 | } |
| 713 | } |
| 714 | } |
| 715 | } |
| 716 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 717 | void HInstruction::ReplaceWith(HInstruction* other) { |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 718 | DCHECK(other != nullptr); |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 719 | for (HUseIterator<HInstruction*> it(GetUses()); !it.Done(); it.Advance()) { |
| 720 | HUseListNode<HInstruction*>* current = it.Current(); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 721 | HInstruction* user = current->GetUser(); |
| 722 | size_t input_index = current->GetIndex(); |
| 723 | user->SetRawInputAt(input_index, other); |
| 724 | other->AddUseAt(user, input_index); |
| 725 | } |
| 726 | |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 727 | for (HUseIterator<HEnvironment*> it(GetEnvUses()); !it.Done(); it.Advance()) { |
| 728 | HUseListNode<HEnvironment*>* current = it.Current(); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 729 | HEnvironment* user = current->GetUser(); |
| 730 | size_t input_index = current->GetIndex(); |
| 731 | user->SetRawEnvAt(input_index, other); |
| 732 | other->AddEnvUseAt(user, input_index); |
| 733 | } |
| 734 | |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 735 | uses_.Clear(); |
| 736 | env_uses_.Clear(); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 737 | } |
| 738 | |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 739 | void HInstruction::ReplaceInput(HInstruction* replacement, size_t index) { |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 740 | RemoveAsUserOfInput(index); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 741 | SetRawInputAt(index, replacement); |
| 742 | replacement->AddUseAt(this, index); |
| 743 | } |
| 744 | |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 745 | size_t HInstruction::EnvironmentSize() const { |
| 746 | return HasEnvironment() ? environment_->Size() : 0; |
| 747 | } |
| 748 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 749 | void HPhi::AddInput(HInstruction* input) { |
| 750 | DCHECK(input->GetBlock() != nullptr); |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 751 | inputs_.Add(HUserRecord<HInstruction*>(input)); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 752 | input->AddUseAt(this, inputs_.Size() - 1); |
| 753 | } |
| 754 | |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 755 | void HPhi::RemoveInputAt(size_t index) { |
| 756 | RemoveAsUserOfInput(index); |
| 757 | inputs_.DeleteAt(index); |
Nicolas Geoffray | 5d7b7f8 | 2015-04-28 00:52:43 +0100 | [diff] [blame] | 758 | for (size_t i = index, e = InputCount(); i < e; ++i) { |
| 759 | InputRecordAt(i).GetUseNode()->SetIndex(i); |
| 760 | } |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 761 | } |
| 762 | |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 763 | #define DEFINE_ACCEPT(name, super) \ |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 764 | void H##name::Accept(HGraphVisitor* visitor) { \ |
| 765 | visitor->Visit##name(this); \ |
| 766 | } |
| 767 | |
| 768 | FOR_EACH_INSTRUCTION(DEFINE_ACCEPT) |
| 769 | |
| 770 | #undef DEFINE_ACCEPT |
| 771 | |
| 772 | void HGraphVisitor::VisitInsertionOrder() { |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 773 | const GrowableArray<HBasicBlock*>& blocks = graph_->GetBlocks(); |
| 774 | for (size_t i = 0 ; i < blocks.Size(); i++) { |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 775 | HBasicBlock* block = blocks.Get(i); |
| 776 | if (block != nullptr) { |
| 777 | VisitBasicBlock(block); |
| 778 | } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 779 | } |
| 780 | } |
| 781 | |
Roland Levillain | 633021e | 2014-10-01 14:12:25 +0100 | [diff] [blame] | 782 | void HGraphVisitor::VisitReversePostOrder() { |
| 783 | for (HReversePostOrderIterator it(*graph_); !it.Done(); it.Advance()) { |
| 784 | VisitBasicBlock(it.Current()); |
| 785 | } |
| 786 | } |
| 787 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 788 | void HGraphVisitor::VisitBasicBlock(HBasicBlock* block) { |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 789 | for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) { |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 790 | it.Current()->Accept(this); |
| 791 | } |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 792 | for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) { |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 793 | it.Current()->Accept(this); |
| 794 | } |
| 795 | } |
| 796 | |
Roland Levillain | 9240d6a | 2014-10-20 16:47:04 +0100 | [diff] [blame] | 797 | HConstant* HUnaryOperation::TryStaticEvaluation() const { |
| 798 | if (GetInput()->IsIntConstant()) { |
| 799 | int32_t value = Evaluate(GetInput()->AsIntConstant()->GetValue()); |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 800 | return GetBlock()->GetGraph()->GetIntConstant(value); |
Roland Levillain | 9240d6a | 2014-10-20 16:47:04 +0100 | [diff] [blame] | 801 | } else if (GetInput()->IsLongConstant()) { |
Roland Levillain | b762d2e | 2014-10-22 10:11:06 +0100 | [diff] [blame] | 802 | // TODO: Implement static evaluation of long unary operations. |
| 803 | // |
| 804 | // Do not exit with a fatal condition here. Instead, simply |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 805 | // return `null' to notify the caller that this instruction |
Roland Levillain | b762d2e | 2014-10-22 10:11:06 +0100 | [diff] [blame] | 806 | // cannot (yet) be statically evaluated. |
Roland Levillain | 9240d6a | 2014-10-20 16:47:04 +0100 | [diff] [blame] | 807 | return nullptr; |
| 808 | } |
| 809 | return nullptr; |
| 810 | } |
| 811 | |
| 812 | HConstant* HBinaryOperation::TryStaticEvaluation() const { |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 813 | if (GetLeft()->IsIntConstant() && GetRight()->IsIntConstant()) { |
| 814 | int32_t value = Evaluate(GetLeft()->AsIntConstant()->GetValue(), |
| 815 | GetRight()->AsIntConstant()->GetValue()); |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 816 | return GetBlock()->GetGraph()->GetIntConstant(value); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 817 | } else if (GetLeft()->IsLongConstant() && GetRight()->IsLongConstant()) { |
| 818 | int64_t value = Evaluate(GetLeft()->AsLongConstant()->GetValue(), |
| 819 | GetRight()->AsLongConstant()->GetValue()); |
Nicolas Geoffray | 9ee6618 | 2015-01-16 12:35:40 +0000 | [diff] [blame] | 820 | if (GetResultType() == Primitive::kPrimLong) { |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 821 | return GetBlock()->GetGraph()->GetLongConstant(value); |
Nicolas Geoffray | 9ee6618 | 2015-01-16 12:35:40 +0000 | [diff] [blame] | 822 | } else { |
Nicolas Geoffray | 6c2dff8 | 2015-01-21 14:56:54 +0000 | [diff] [blame] | 823 | DCHECK_EQ(GetResultType(), Primitive::kPrimInt); |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 824 | return GetBlock()->GetGraph()->GetIntConstant(static_cast<int32_t>(value)); |
Nicolas Geoffray | 9ee6618 | 2015-01-16 12:35:40 +0000 | [diff] [blame] | 825 | } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 826 | } |
| 827 | return nullptr; |
| 828 | } |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 829 | |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 830 | HConstant* HBinaryOperation::GetConstantRight() const { |
| 831 | if (GetRight()->IsConstant()) { |
| 832 | return GetRight()->AsConstant(); |
| 833 | } else if (IsCommutative() && GetLeft()->IsConstant()) { |
| 834 | return GetLeft()->AsConstant(); |
| 835 | } else { |
| 836 | return nullptr; |
| 837 | } |
| 838 | } |
| 839 | |
| 840 | // If `GetConstantRight()` returns one of the input, this returns the other |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 841 | // one. Otherwise it returns null. |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 842 | HInstruction* HBinaryOperation::GetLeastConstantLeft() const { |
| 843 | HInstruction* most_constant_right = GetConstantRight(); |
| 844 | if (most_constant_right == nullptr) { |
| 845 | return nullptr; |
| 846 | } else if (most_constant_right == GetLeft()) { |
| 847 | return GetRight(); |
| 848 | } else { |
| 849 | return GetLeft(); |
| 850 | } |
| 851 | } |
| 852 | |
Mingyao Yang | d43b3ac | 2015-04-01 14:03:04 -0700 | [diff] [blame] | 853 | bool HCondition::IsBeforeWhenDisregardMoves(HInstruction* instruction) const { |
| 854 | return this == instruction->GetPreviousDisregardingMoves(); |
Nicolas Geoffray | 18efde5 | 2014-09-22 15:51:11 +0100 | [diff] [blame] | 855 | } |
| 856 | |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 857 | bool HInstruction::Equals(HInstruction* other) const { |
| 858 | if (!InstructionTypeEquals(other)) return false; |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 859 | DCHECK_EQ(GetKind(), other->GetKind()); |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 860 | if (!InstructionDataEquals(other)) return false; |
| 861 | if (GetType() != other->GetType()) return false; |
| 862 | if (InputCount() != other->InputCount()) return false; |
| 863 | |
| 864 | for (size_t i = 0, e = InputCount(); i < e; ++i) { |
| 865 | if (InputAt(i) != other->InputAt(i)) return false; |
| 866 | } |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 867 | DCHECK_EQ(ComputeHashCode(), other->ComputeHashCode()); |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 868 | return true; |
| 869 | } |
| 870 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 871 | std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs) { |
| 872 | #define DECLARE_CASE(type, super) case HInstruction::k##type: os << #type; break; |
| 873 | switch (rhs) { |
| 874 | FOR_EACH_INSTRUCTION(DECLARE_CASE) |
| 875 | default: |
| 876 | os << "Unknown instruction kind " << static_cast<int>(rhs); |
| 877 | break; |
| 878 | } |
| 879 | #undef DECLARE_CASE |
| 880 | return os; |
| 881 | } |
| 882 | |
Nicolas Geoffray | 82091da | 2015-01-26 10:02:45 +0000 | [diff] [blame] | 883 | void HInstruction::MoveBefore(HInstruction* cursor) { |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 884 | next_->previous_ = previous_; |
| 885 | if (previous_ != nullptr) { |
| 886 | previous_->next_ = next_; |
| 887 | } |
| 888 | if (block_->instructions_.first_instruction_ == this) { |
| 889 | block_->instructions_.first_instruction_ = next_; |
| 890 | } |
Nicolas Geoffray | 82091da | 2015-01-26 10:02:45 +0000 | [diff] [blame] | 891 | DCHECK_NE(block_->instructions_.last_instruction_, this); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 892 | |
| 893 | previous_ = cursor->previous_; |
| 894 | if (previous_ != nullptr) { |
| 895 | previous_->next_ = this; |
| 896 | } |
| 897 | next_ = cursor; |
| 898 | cursor->previous_ = this; |
| 899 | block_ = cursor->block_; |
Nicolas Geoffray | 82091da | 2015-01-26 10:02:45 +0000 | [diff] [blame] | 900 | |
| 901 | if (block_->instructions_.first_instruction_ == cursor) { |
| 902 | block_->instructions_.first_instruction_ = this; |
| 903 | } |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 904 | } |
| 905 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 906 | HBasicBlock* HBasicBlock::SplitAfter(HInstruction* cursor) { |
| 907 | DCHECK(!cursor->IsControlFlow()); |
| 908 | DCHECK_NE(instructions_.last_instruction_, cursor); |
| 909 | DCHECK_EQ(cursor->GetBlock(), this); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 910 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 911 | HBasicBlock* new_block = new (GetGraph()->GetArena()) HBasicBlock(GetGraph(), GetDexPc()); |
| 912 | new_block->instructions_.first_instruction_ = cursor->GetNext(); |
| 913 | new_block->instructions_.last_instruction_ = instructions_.last_instruction_; |
| 914 | cursor->next_->previous_ = nullptr; |
| 915 | cursor->next_ = nullptr; |
| 916 | instructions_.last_instruction_ = cursor; |
| 917 | |
| 918 | new_block->instructions_.SetBlockOfInstructions(new_block); |
| 919 | for (size_t i = 0, e = GetSuccessors().Size(); i < e; ++i) { |
| 920 | HBasicBlock* successor = GetSuccessors().Get(i); |
| 921 | new_block->successors_.Add(successor); |
| 922 | successor->predecessors_.Put(successor->GetPredecessorIndexOf(this), new_block); |
| 923 | } |
| 924 | successors_.Reset(); |
| 925 | |
| 926 | for (size_t i = 0, e = GetDominatedBlocks().Size(); i < e; ++i) { |
| 927 | HBasicBlock* dominated = GetDominatedBlocks().Get(i); |
| 928 | dominated->dominator_ = new_block; |
| 929 | new_block->dominated_blocks_.Add(dominated); |
| 930 | } |
| 931 | dominated_blocks_.Reset(); |
| 932 | return new_block; |
| 933 | } |
| 934 | |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 935 | bool HBasicBlock::IsSingleGoto() const { |
| 936 | HLoopInformation* loop_info = GetLoopInformation(); |
| 937 | // TODO: Remove the null check b/19084197. |
| 938 | return GetFirstInstruction() != nullptr |
| 939 | && GetPhis().IsEmpty() |
| 940 | && GetFirstInstruction() == GetLastInstruction() |
| 941 | && GetLastInstruction()->IsGoto() |
| 942 | // Back edges generate the suspend check. |
| 943 | && (loop_info == nullptr || !loop_info->IsBackEdge(*this)); |
| 944 | } |
| 945 | |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 946 | bool HBasicBlock::EndsWithControlFlowInstruction() const { |
| 947 | return !GetInstructions().IsEmpty() && GetLastInstruction()->IsControlFlow(); |
| 948 | } |
| 949 | |
David Brazdil | b2bd1c5 | 2015-03-25 11:17:37 +0000 | [diff] [blame] | 950 | bool HBasicBlock::EndsWithIf() const { |
| 951 | return !GetInstructions().IsEmpty() && GetLastInstruction()->IsIf(); |
| 952 | } |
| 953 | |
| 954 | bool HBasicBlock::HasSinglePhi() const { |
| 955 | return !GetPhis().IsEmpty() && GetFirstPhi()->GetNext() == nullptr; |
| 956 | } |
| 957 | |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 958 | size_t HInstructionList::CountSize() const { |
| 959 | size_t size = 0; |
| 960 | HInstruction* current = first_instruction_; |
| 961 | for (; current != nullptr; current = current->GetNext()) { |
| 962 | size++; |
| 963 | } |
| 964 | return size; |
| 965 | } |
| 966 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 967 | void HInstructionList::SetBlockOfInstructions(HBasicBlock* block) const { |
| 968 | for (HInstruction* current = first_instruction_; |
| 969 | current != nullptr; |
| 970 | current = current->GetNext()) { |
| 971 | current->SetBlock(block); |
| 972 | } |
| 973 | } |
| 974 | |
| 975 | void HInstructionList::AddAfter(HInstruction* cursor, const HInstructionList& instruction_list) { |
| 976 | DCHECK(Contains(cursor)); |
| 977 | if (!instruction_list.IsEmpty()) { |
| 978 | if (cursor == last_instruction_) { |
| 979 | last_instruction_ = instruction_list.last_instruction_; |
| 980 | } else { |
| 981 | cursor->next_->previous_ = instruction_list.last_instruction_; |
| 982 | } |
| 983 | instruction_list.last_instruction_->next_ = cursor->next_; |
| 984 | cursor->next_ = instruction_list.first_instruction_; |
| 985 | instruction_list.first_instruction_->previous_ = cursor; |
| 986 | } |
| 987 | } |
| 988 | |
| 989 | void HInstructionList::Add(const HInstructionList& instruction_list) { |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 990 | if (IsEmpty()) { |
| 991 | first_instruction_ = instruction_list.first_instruction_; |
| 992 | last_instruction_ = instruction_list.last_instruction_; |
| 993 | } else { |
| 994 | AddAfter(last_instruction_, instruction_list); |
| 995 | } |
| 996 | } |
| 997 | |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 998 | void HBasicBlock::DisconnectAndDelete() { |
| 999 | // Dominators must be removed after all the blocks they dominate. This way |
| 1000 | // a loop header is removed last, a requirement for correct loop information |
| 1001 | // iteration. |
| 1002 | DCHECK(dominated_blocks_.IsEmpty()); |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 1003 | |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1004 | // Remove the block from all loops it is included in. |
| 1005 | for (HLoopInformationOutwardIterator it(*this); !it.Done(); it.Advance()) { |
| 1006 | HLoopInformation* loop_info = it.Current(); |
| 1007 | loop_info->Remove(this); |
| 1008 | if (loop_info->IsBackEdge(*this)) { |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 1009 | // If this was the last back edge of the loop, we deliberately leave the |
| 1010 | // loop in an inconsistent state and will fail SSAChecker unless the |
| 1011 | // entire loop is removed during the pass. |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1012 | loop_info->RemoveBackEdge(this); |
| 1013 | } |
| 1014 | } |
| 1015 | |
| 1016 | // Disconnect the block from its predecessors and update their control-flow |
| 1017 | // instructions. |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 1018 | for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) { |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1019 | HBasicBlock* predecessor = predecessors_.Get(i); |
| 1020 | HInstruction* last_instruction = predecessor->GetLastInstruction(); |
| 1021 | predecessor->RemoveInstruction(last_instruction); |
| 1022 | predecessor->RemoveSuccessor(this); |
| 1023 | if (predecessor->GetSuccessors().Size() == 1u) { |
| 1024 | DCHECK(last_instruction->IsIf()); |
| 1025 | predecessor->AddInstruction(new (graph_->GetArena()) HGoto()); |
| 1026 | } else { |
| 1027 | // The predecessor has no remaining successors and therefore must be dead. |
| 1028 | // We deliberately leave it without a control-flow instruction so that the |
| 1029 | // SSAChecker fails unless it is not removed during the pass too. |
| 1030 | DCHECK_EQ(predecessor->GetSuccessors().Size(), 0u); |
| 1031 | } |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 1032 | } |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 1033 | predecessors_.Reset(); |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1034 | |
| 1035 | // Disconnect the block from its successors and update their dominators |
| 1036 | // and phis. |
| 1037 | for (size_t i = 0, e = successors_.Size(); i < e; ++i) { |
| 1038 | HBasicBlock* successor = successors_.Get(i); |
| 1039 | // Delete this block from the list of predecessors. |
| 1040 | size_t this_index = successor->GetPredecessorIndexOf(this); |
| 1041 | successor->predecessors_.DeleteAt(this_index); |
| 1042 | |
| 1043 | // Check that `successor` has other predecessors, otherwise `this` is the |
| 1044 | // dominator of `successor` which violates the order DCHECKed at the top. |
| 1045 | DCHECK(!successor->predecessors_.IsEmpty()); |
| 1046 | |
| 1047 | // Recompute the successor's dominator. |
| 1048 | HBasicBlock* old_dominator = successor->GetDominator(); |
| 1049 | HBasicBlock* new_dominator = successor->predecessors_.Get(0); |
| 1050 | for (size_t j = 1, f = successor->predecessors_.Size(); j < f; ++j) { |
| 1051 | new_dominator = graph_->FindCommonDominator( |
| 1052 | new_dominator, successor->predecessors_.Get(j)); |
| 1053 | } |
| 1054 | if (old_dominator != new_dominator) { |
| 1055 | successor->SetDominator(new_dominator); |
| 1056 | old_dominator->RemoveDominatedBlock(successor); |
| 1057 | new_dominator->AddDominatedBlock(successor); |
| 1058 | } |
| 1059 | |
| 1060 | // Remove this block's entries in the successor's phis. |
| 1061 | if (successor->predecessors_.Size() == 1u) { |
| 1062 | // The successor has just one predecessor left. Replace phis with the only |
| 1063 | // remaining input. |
| 1064 | for (HInstructionIterator phi_it(successor->GetPhis()); !phi_it.Done(); phi_it.Advance()) { |
| 1065 | HPhi* phi = phi_it.Current()->AsPhi(); |
| 1066 | phi->ReplaceWith(phi->InputAt(1 - this_index)); |
| 1067 | successor->RemovePhi(phi); |
| 1068 | } |
| 1069 | } else { |
| 1070 | for (HInstructionIterator phi_it(successor->GetPhis()); !phi_it.Done(); phi_it.Advance()) { |
| 1071 | phi_it.Current()->AsPhi()->RemoveInputAt(this_index); |
| 1072 | } |
| 1073 | } |
| 1074 | } |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 1075 | successors_.Reset(); |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1076 | |
| 1077 | // Disconnect from the dominator. |
| 1078 | dominator_->RemoveDominatedBlock(this); |
| 1079 | SetDominator(nullptr); |
| 1080 | |
| 1081 | // Delete from the graph. The function safely deletes remaining instructions |
| 1082 | // and updates the reverse post order. |
| 1083 | graph_->DeleteDeadBlock(this); |
| 1084 | SetGraph(nullptr); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1085 | } |
| 1086 | |
| 1087 | void HBasicBlock::MergeWith(HBasicBlock* other) { |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1088 | DCHECK_EQ(GetGraph(), other->GetGraph()); |
| 1089 | DCHECK(GetDominatedBlocks().Contains(other)); |
| 1090 | DCHECK_EQ(GetSuccessors().Size(), 1u); |
| 1091 | DCHECK_EQ(GetSuccessors().Get(0), other); |
| 1092 | DCHECK_EQ(other->GetPredecessors().Size(), 1u); |
| 1093 | DCHECK_EQ(other->GetPredecessors().Get(0), this); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1094 | DCHECK(other->GetPhis().IsEmpty()); |
| 1095 | |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1096 | // Move instructions from `other` to `this`. |
| 1097 | DCHECK(EndsWithControlFlowInstruction()); |
| 1098 | RemoveInstruction(GetLastInstruction()); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1099 | instructions_.Add(other->GetInstructions()); |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1100 | other->instructions_.SetBlockOfInstructions(this); |
| 1101 | other->instructions_.Clear(); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1102 | |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1103 | // Remove `other` from the loops it is included in. |
| 1104 | for (HLoopInformationOutwardIterator it(*other); !it.Done(); it.Advance()) { |
| 1105 | HLoopInformation* loop_info = it.Current(); |
| 1106 | loop_info->Remove(other); |
| 1107 | if (loop_info->IsBackEdge(*other)) { |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 1108 | loop_info->ReplaceBackEdge(other, this); |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1109 | } |
| 1110 | } |
| 1111 | |
| 1112 | // Update links to the successors of `other`. |
| 1113 | successors_.Reset(); |
| 1114 | while (!other->successors_.IsEmpty()) { |
| 1115 | HBasicBlock* successor = other->successors_.Get(0); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1116 | successor->ReplacePredecessor(other, this); |
| 1117 | } |
| 1118 | |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1119 | // Update the dominator tree. |
| 1120 | dominated_blocks_.Delete(other); |
| 1121 | for (size_t i = 0, e = other->GetDominatedBlocks().Size(); i < e; ++i) { |
| 1122 | HBasicBlock* dominated = other->GetDominatedBlocks().Get(i); |
| 1123 | dominated_blocks_.Add(dominated); |
| 1124 | dominated->SetDominator(this); |
| 1125 | } |
| 1126 | other->dominated_blocks_.Reset(); |
| 1127 | other->dominator_ = nullptr; |
| 1128 | |
| 1129 | // Clear the list of predecessors of `other` in preparation of deleting it. |
| 1130 | other->predecessors_.Reset(); |
| 1131 | |
| 1132 | // Delete `other` from the graph. The function updates reverse post order. |
| 1133 | graph_->DeleteDeadBlock(other); |
| 1134 | other->SetGraph(nullptr); |
| 1135 | } |
| 1136 | |
| 1137 | void HBasicBlock::MergeWithInlined(HBasicBlock* other) { |
| 1138 | DCHECK_NE(GetGraph(), other->GetGraph()); |
| 1139 | DCHECK(GetDominatedBlocks().IsEmpty()); |
| 1140 | DCHECK(GetSuccessors().IsEmpty()); |
| 1141 | DCHECK(!EndsWithControlFlowInstruction()); |
| 1142 | DCHECK_EQ(other->GetPredecessors().Size(), 1u); |
| 1143 | DCHECK(other->GetPredecessors().Get(0)->IsEntryBlock()); |
| 1144 | DCHECK(other->GetPhis().IsEmpty()); |
| 1145 | DCHECK(!other->IsInLoop()); |
| 1146 | |
| 1147 | // Move instructions from `other` to `this`. |
| 1148 | instructions_.Add(other->GetInstructions()); |
| 1149 | other->instructions_.SetBlockOfInstructions(this); |
| 1150 | |
| 1151 | // Update links to the successors of `other`. |
| 1152 | successors_.Reset(); |
| 1153 | while (!other->successors_.IsEmpty()) { |
| 1154 | HBasicBlock* successor = other->successors_.Get(0); |
| 1155 | successor->ReplacePredecessor(other, this); |
| 1156 | } |
| 1157 | |
| 1158 | // Update the dominator tree. |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1159 | for (size_t i = 0, e = other->GetDominatedBlocks().Size(); i < e; ++i) { |
| 1160 | HBasicBlock* dominated = other->GetDominatedBlocks().Get(i); |
| 1161 | dominated_blocks_.Add(dominated); |
| 1162 | dominated->SetDominator(this); |
| 1163 | } |
| 1164 | other->dominated_blocks_.Reset(); |
| 1165 | other->dominator_ = nullptr; |
| 1166 | other->graph_ = nullptr; |
| 1167 | } |
| 1168 | |
| 1169 | void HBasicBlock::ReplaceWith(HBasicBlock* other) { |
| 1170 | while (!GetPredecessors().IsEmpty()) { |
| 1171 | HBasicBlock* predecessor = GetPredecessors().Get(0); |
| 1172 | predecessor->ReplaceSuccessor(this, other); |
| 1173 | } |
| 1174 | while (!GetSuccessors().IsEmpty()) { |
| 1175 | HBasicBlock* successor = GetSuccessors().Get(0); |
| 1176 | successor->ReplacePredecessor(this, other); |
| 1177 | } |
| 1178 | for (size_t i = 0; i < dominated_blocks_.Size(); ++i) { |
| 1179 | other->AddDominatedBlock(dominated_blocks_.Get(i)); |
| 1180 | } |
| 1181 | GetDominator()->ReplaceDominatedBlock(this, other); |
| 1182 | other->SetDominator(GetDominator()); |
| 1183 | dominator_ = nullptr; |
| 1184 | graph_ = nullptr; |
| 1185 | } |
| 1186 | |
| 1187 | // Create space in `blocks` for adding `number_of_new_blocks` entries |
| 1188 | // starting at location `at`. Blocks after `at` are moved accordingly. |
| 1189 | static void MakeRoomFor(GrowableArray<HBasicBlock*>* blocks, |
| 1190 | size_t number_of_new_blocks, |
| 1191 | size_t at) { |
| 1192 | size_t old_size = blocks->Size(); |
| 1193 | size_t new_size = old_size + number_of_new_blocks; |
| 1194 | blocks->SetSize(new_size); |
| 1195 | for (size_t i = old_size - 1, j = new_size - 1; i > at; --i, --j) { |
| 1196 | blocks->Put(j, blocks->Get(i)); |
| 1197 | } |
| 1198 | } |
| 1199 | |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1200 | void HGraph::DeleteDeadBlock(HBasicBlock* block) { |
| 1201 | DCHECK_EQ(block->GetGraph(), this); |
| 1202 | DCHECK(block->GetSuccessors().IsEmpty()); |
| 1203 | DCHECK(block->GetPredecessors().IsEmpty()); |
| 1204 | DCHECK(block->GetDominatedBlocks().IsEmpty()); |
| 1205 | DCHECK(block->GetDominator() == nullptr); |
| 1206 | |
| 1207 | for (HBackwardInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) { |
| 1208 | block->RemoveInstruction(it.Current()); |
| 1209 | } |
| 1210 | for (HBackwardInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) { |
| 1211 | block->RemovePhi(it.Current()->AsPhi()); |
| 1212 | } |
| 1213 | |
| 1214 | reverse_post_order_.Delete(block); |
| 1215 | blocks_.Put(block->GetBlockId(), nullptr); |
| 1216 | } |
| 1217 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1218 | void HGraph::InlineInto(HGraph* outer_graph, HInvoke* invoke) { |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1219 | if (GetBlocks().Size() == 3) { |
Nicolas Geoffray | be31ff9 | 2015-02-04 14:52:20 +0000 | [diff] [blame] | 1220 | // Simple case of an entry block, a body block, and an exit block. |
| 1221 | // Put the body block's instruction into `invoke`'s block. |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1222 | HBasicBlock* body = GetBlocks().Get(1); |
Nicolas Geoffray | be31ff9 | 2015-02-04 14:52:20 +0000 | [diff] [blame] | 1223 | DCHECK(GetBlocks().Get(0)->IsEntryBlock()); |
| 1224 | DCHECK(GetBlocks().Get(2)->IsExitBlock()); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1225 | DCHECK(!body->IsExitBlock()); |
| 1226 | HInstruction* last = body->GetLastInstruction(); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1227 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1228 | invoke->GetBlock()->instructions_.AddAfter(invoke, body->GetInstructions()); |
| 1229 | body->GetInstructions().SetBlockOfInstructions(invoke->GetBlock()); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1230 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1231 | // Replace the invoke with the return value of the inlined graph. |
| 1232 | if (last->IsReturn()) { |
| 1233 | invoke->ReplaceWith(last->InputAt(0)); |
| 1234 | } else { |
| 1235 | DCHECK(last->IsReturnVoid()); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1236 | } |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1237 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1238 | invoke->GetBlock()->RemoveInstruction(last); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1239 | } else { |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1240 | // Need to inline multiple blocks. We split `invoke`'s block |
| 1241 | // into two blocks, merge the first block of the inlined graph into |
Nicolas Geoffray | be31ff9 | 2015-02-04 14:52:20 +0000 | [diff] [blame] | 1242 | // the first half, and replace the exit block of the inlined graph |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1243 | // with the second half. |
| 1244 | ArenaAllocator* allocator = outer_graph->GetArena(); |
| 1245 | HBasicBlock* at = invoke->GetBlock(); |
| 1246 | HBasicBlock* to = at->SplitAfter(invoke); |
| 1247 | |
| 1248 | HBasicBlock* first = entry_block_->GetSuccessors().Get(0); |
| 1249 | DCHECK(!first->IsInLoop()); |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1250 | at->MergeWithInlined(first); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1251 | exit_block_->ReplaceWith(to); |
| 1252 | |
| 1253 | // Update all predecessors of the exit block (now the `to` block) |
Nicolas Geoffray | 817bce7 | 2015-02-24 13:35:38 +0000 | [diff] [blame] | 1254 | // to not `HReturn` but `HGoto` instead. |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1255 | HInstruction* return_value = nullptr; |
Nicolas Geoffray | 817bce7 | 2015-02-24 13:35:38 +0000 | [diff] [blame] | 1256 | bool returns_void = to->GetPredecessors().Get(0)->GetLastInstruction()->IsReturnVoid(); |
| 1257 | if (to->GetPredecessors().Size() == 1) { |
| 1258 | HBasicBlock* predecessor = to->GetPredecessors().Get(0); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1259 | HInstruction* last = predecessor->GetLastInstruction(); |
Nicolas Geoffray | 817bce7 | 2015-02-24 13:35:38 +0000 | [diff] [blame] | 1260 | if (!returns_void) { |
| 1261 | return_value = last->InputAt(0); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1262 | } |
| 1263 | predecessor->AddInstruction(new (allocator) HGoto()); |
| 1264 | predecessor->RemoveInstruction(last); |
Nicolas Geoffray | 817bce7 | 2015-02-24 13:35:38 +0000 | [diff] [blame] | 1265 | } else { |
| 1266 | if (!returns_void) { |
| 1267 | // There will be multiple returns. |
Nicolas Geoffray | 4f1a384 | 2015-03-12 10:34:11 +0000 | [diff] [blame] | 1268 | return_value = new (allocator) HPhi( |
| 1269 | allocator, kNoRegNumber, 0, HPhi::ToPhiType(invoke->GetType())); |
Nicolas Geoffray | 817bce7 | 2015-02-24 13:35:38 +0000 | [diff] [blame] | 1270 | to->AddPhi(return_value->AsPhi()); |
| 1271 | } |
| 1272 | for (size_t i = 0, e = to->GetPredecessors().Size(); i < e; ++i) { |
| 1273 | HBasicBlock* predecessor = to->GetPredecessors().Get(i); |
| 1274 | HInstruction* last = predecessor->GetLastInstruction(); |
| 1275 | if (!returns_void) { |
| 1276 | return_value->AsPhi()->AddInput(last->InputAt(0)); |
| 1277 | } |
| 1278 | predecessor->AddInstruction(new (allocator) HGoto()); |
| 1279 | predecessor->RemoveInstruction(last); |
| 1280 | } |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1281 | } |
| 1282 | |
| 1283 | if (return_value != nullptr) { |
| 1284 | invoke->ReplaceWith(return_value); |
| 1285 | } |
| 1286 | |
| 1287 | // Update the meta information surrounding blocks: |
| 1288 | // (1) the graph they are now in, |
| 1289 | // (2) the reverse post order of that graph, |
| 1290 | // (3) the potential loop information they are now in. |
| 1291 | |
| 1292 | // We don't add the entry block, the exit block, and the first block, which |
| 1293 | // has been merged with `at`. |
| 1294 | static constexpr int kNumberOfSkippedBlocksInCallee = 3; |
| 1295 | |
| 1296 | // We add the `to` block. |
| 1297 | static constexpr int kNumberOfNewBlocksInCaller = 1; |
| 1298 | size_t blocks_added = (reverse_post_order_.Size() - kNumberOfSkippedBlocksInCallee) |
| 1299 | + kNumberOfNewBlocksInCaller; |
| 1300 | |
| 1301 | // Find the location of `at` in the outer graph's reverse post order. The new |
| 1302 | // blocks will be added after it. |
| 1303 | size_t index_of_at = 0; |
| 1304 | while (outer_graph->reverse_post_order_.Get(index_of_at) != at) { |
| 1305 | index_of_at++; |
| 1306 | } |
| 1307 | MakeRoomFor(&outer_graph->reverse_post_order_, blocks_added, index_of_at); |
| 1308 | |
| 1309 | // Do a reverse post order of the blocks in the callee and do (1), (2), |
| 1310 | // and (3) to the blocks that apply. |
| 1311 | HLoopInformation* info = at->GetLoopInformation(); |
| 1312 | for (HReversePostOrderIterator it(*this); !it.Done(); it.Advance()) { |
| 1313 | HBasicBlock* current = it.Current(); |
| 1314 | if (current != exit_block_ && current != entry_block_ && current != first) { |
| 1315 | DCHECK(!current->IsInLoop()); |
| 1316 | DCHECK(current->GetGraph() == this); |
| 1317 | current->SetGraph(outer_graph); |
| 1318 | outer_graph->AddBlock(current); |
| 1319 | outer_graph->reverse_post_order_.Put(++index_of_at, current); |
| 1320 | if (info != nullptr) { |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1321 | current->SetLoopInformation(info); |
David Brazdil | 7d27537 | 2015-04-21 16:36:35 +0100 | [diff] [blame] | 1322 | for (HLoopInformationOutwardIterator loop_it(*at); !loop_it.Done(); loop_it.Advance()) { |
| 1323 | loop_it.Current()->Add(current); |
| 1324 | } |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1325 | } |
| 1326 | } |
| 1327 | } |
| 1328 | |
| 1329 | // Do (1), (2), and (3) to `to`. |
| 1330 | to->SetGraph(outer_graph); |
| 1331 | outer_graph->AddBlock(to); |
| 1332 | outer_graph->reverse_post_order_.Put(++index_of_at, to); |
| 1333 | if (info != nullptr) { |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1334 | to->SetLoopInformation(info); |
David Brazdil | 7d27537 | 2015-04-21 16:36:35 +0100 | [diff] [blame] | 1335 | for (HLoopInformationOutwardIterator loop_it(*at); !loop_it.Done(); loop_it.Advance()) { |
| 1336 | loop_it.Current()->Add(to); |
| 1337 | } |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 1338 | if (info->IsBackEdge(*at)) { |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 1339 | // Only `to` can become a back edge, as the inlined blocks |
| 1340 | // are predecessors of `to`. |
| 1341 | info->ReplaceBackEdge(at, to); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1342 | } |
| 1343 | } |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1344 | } |
Nicolas Geoffray | 7c5367b | 2014-12-17 10:13:46 +0000 | [diff] [blame] | 1345 | |
David Brazdil | 05144f4 | 2015-04-16 15:18:00 +0100 | [diff] [blame] | 1346 | // Update the next instruction id of the outer graph, so that instructions |
| 1347 | // added later get bigger ids than those in the inner graph. |
| 1348 | outer_graph->SetCurrentInstructionId(GetNextInstructionId()); |
| 1349 | |
| 1350 | // Walk over the entry block and: |
| 1351 | // - Move constants from the entry block to the outer_graph's entry block, |
| 1352 | // - Replace HParameterValue instructions with their real value. |
| 1353 | // - Remove suspend checks, that hold an environment. |
| 1354 | // We must do this after the other blocks have been inlined, otherwise ids of |
| 1355 | // constants could overlap with the inner graph. |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 1356 | size_t parameter_index = 0; |
David Brazdil | 05144f4 | 2015-04-16 15:18:00 +0100 | [diff] [blame] | 1357 | for (HInstructionIterator it(entry_block_->GetInstructions()); !it.Done(); it.Advance()) { |
| 1358 | HInstruction* current = it.Current(); |
| 1359 | if (current->IsNullConstant()) { |
| 1360 | current->ReplaceWith(outer_graph->GetNullConstant()); |
| 1361 | } else if (current->IsIntConstant()) { |
| 1362 | current->ReplaceWith(outer_graph->GetIntConstant(current->AsIntConstant()->GetValue())); |
| 1363 | } else if (current->IsLongConstant()) { |
| 1364 | current->ReplaceWith(outer_graph->GetLongConstant(current->AsLongConstant()->GetValue())); |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 1365 | } else if (current->IsFloatConstant()) { |
| 1366 | current->ReplaceWith(outer_graph->GetFloatConstant(current->AsFloatConstant()->GetValue())); |
| 1367 | } else if (current->IsDoubleConstant()) { |
| 1368 | current->ReplaceWith(outer_graph->GetDoubleConstant(current->AsDoubleConstant()->GetValue())); |
David Brazdil | 05144f4 | 2015-04-16 15:18:00 +0100 | [diff] [blame] | 1369 | } else if (current->IsParameterValue()) { |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 1370 | if (kIsDebugBuild |
| 1371 | && invoke->IsInvokeStaticOrDirect() |
| 1372 | && invoke->AsInvokeStaticOrDirect()->IsStaticWithExplicitClinitCheck()) { |
| 1373 | // Ensure we do not use the last input of `invoke`, as it |
| 1374 | // contains a clinit check which is not an actual argument. |
| 1375 | size_t last_input_index = invoke->InputCount() - 1; |
| 1376 | DCHECK(parameter_index != last_input_index); |
| 1377 | } |
David Brazdil | 05144f4 | 2015-04-16 15:18:00 +0100 | [diff] [blame] | 1378 | current->ReplaceWith(invoke->InputAt(parameter_index++)); |
| 1379 | } else { |
| 1380 | DCHECK(current->IsGoto() || current->IsSuspendCheck()); |
| 1381 | entry_block_->RemoveInstruction(current); |
| 1382 | } |
| 1383 | } |
| 1384 | |
Nicolas Geoffray | 7c5367b | 2014-12-17 10:13:46 +0000 | [diff] [blame] | 1385 | // Finally remove the invoke from the caller. |
| 1386 | invoke->GetBlock()->RemoveInstruction(invoke); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1387 | } |
| 1388 | |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 1389 | std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs) { |
| 1390 | ScopedObjectAccess soa(Thread::Current()); |
| 1391 | os << "[" |
| 1392 | << " is_top=" << rhs.IsTop() |
| 1393 | << " type=" << (rhs.IsTop() ? "?" : PrettyClass(rhs.GetTypeHandle().Get())) |
| 1394 | << " is_exact=" << rhs.IsExact() |
| 1395 | << " ]"; |
| 1396 | return os; |
| 1397 | } |
| 1398 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1399 | } // namespace art |