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