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 | |
Nicolas Geoffray | 8c0c91a | 2015-05-07 11:46:05 +0100 | [diff] [blame] | 503 | void HEnvironment::CopyFrom(const GrowableArray<HInstruction*>& locals) { |
| 504 | for (size_t i = 0; i < locals.Size(); i++) { |
| 505 | HInstruction* instruction = locals.Get(i); |
| 506 | SetRawEnvAt(i, instruction); |
| 507 | if (instruction != nullptr) { |
| 508 | instruction->AddEnvUseAt(this, i); |
| 509 | } |
| 510 | } |
| 511 | } |
| 512 | |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 513 | void HEnvironment::CopyFrom(HEnvironment* env) { |
| 514 | for (size_t i = 0; i < env->Size(); i++) { |
| 515 | HInstruction* instruction = env->GetInstructionAt(i); |
| 516 | SetRawEnvAt(i, instruction); |
| 517 | if (instruction != nullptr) { |
| 518 | instruction->AddEnvUseAt(this, i); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 519 | } |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 520 | } |
| 521 | } |
| 522 | |
Mingyao Yang | 206d6fd | 2015-04-13 16:46:28 -0700 | [diff] [blame] | 523 | void HEnvironment::CopyFromWithLoopPhiAdjustment(HEnvironment* env, |
| 524 | HBasicBlock* loop_header) { |
| 525 | DCHECK(loop_header->IsLoopHeader()); |
| 526 | for (size_t i = 0; i < env->Size(); i++) { |
| 527 | HInstruction* instruction = env->GetInstructionAt(i); |
| 528 | SetRawEnvAt(i, instruction); |
| 529 | if (instruction == nullptr) { |
| 530 | continue; |
| 531 | } |
| 532 | if (instruction->IsLoopHeaderPhi() && (instruction->GetBlock() == loop_header)) { |
| 533 | // At the end of the loop pre-header, the corresponding value for instruction |
| 534 | // is the first input of the phi. |
| 535 | HInstruction* initial = instruction->AsPhi()->InputAt(0); |
| 536 | DCHECK(initial->GetBlock()->Dominates(loop_header)); |
| 537 | SetRawEnvAt(i, initial); |
| 538 | initial->AddEnvUseAt(this, i); |
| 539 | } else { |
| 540 | instruction->AddEnvUseAt(this, i); |
| 541 | } |
| 542 | } |
| 543 | } |
| 544 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 545 | void HEnvironment::RemoveAsUserOfInput(size_t index) const { |
| 546 | const HUserRecord<HEnvironment*> user_record = vregs_.Get(index); |
| 547 | user_record.GetInstruction()->RemoveEnvironmentUser(user_record.GetUseNode()); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 548 | } |
| 549 | |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 550 | HInstruction* HInstruction::GetNextDisregardingMoves() const { |
| 551 | HInstruction* next = GetNext(); |
| 552 | while (next != nullptr && next->IsParallelMove()) { |
| 553 | next = next->GetNext(); |
| 554 | } |
| 555 | return next; |
| 556 | } |
| 557 | |
| 558 | HInstruction* HInstruction::GetPreviousDisregardingMoves() const { |
| 559 | HInstruction* previous = GetPrevious(); |
| 560 | while (previous != nullptr && previous->IsParallelMove()) { |
| 561 | previous = previous->GetPrevious(); |
| 562 | } |
| 563 | return previous; |
| 564 | } |
| 565 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 566 | void HInstructionList::AddInstruction(HInstruction* instruction) { |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 567 | if (first_instruction_ == nullptr) { |
| 568 | DCHECK(last_instruction_ == nullptr); |
| 569 | first_instruction_ = last_instruction_ = instruction; |
| 570 | } else { |
| 571 | last_instruction_->next_ = instruction; |
| 572 | instruction->previous_ = last_instruction_; |
| 573 | last_instruction_ = instruction; |
| 574 | } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 575 | } |
| 576 | |
David Brazdil | c3d743f | 2015-04-22 13:40:50 +0100 | [diff] [blame] | 577 | void HInstructionList::InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor) { |
| 578 | DCHECK(Contains(cursor)); |
| 579 | if (cursor == first_instruction_) { |
| 580 | cursor->previous_ = instruction; |
| 581 | instruction->next_ = cursor; |
| 582 | first_instruction_ = instruction; |
| 583 | } else { |
| 584 | instruction->previous_ = cursor->previous_; |
| 585 | instruction->next_ = cursor; |
| 586 | cursor->previous_ = instruction; |
| 587 | instruction->previous_->next_ = instruction; |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | void HInstructionList::InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor) { |
| 592 | DCHECK(Contains(cursor)); |
| 593 | if (cursor == last_instruction_) { |
| 594 | cursor->next_ = instruction; |
| 595 | instruction->previous_ = cursor; |
| 596 | last_instruction_ = instruction; |
| 597 | } else { |
| 598 | instruction->next_ = cursor->next_; |
| 599 | instruction->previous_ = cursor; |
| 600 | cursor->next_ = instruction; |
| 601 | instruction->next_->previous_ = instruction; |
| 602 | } |
| 603 | } |
| 604 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 605 | void HInstructionList::RemoveInstruction(HInstruction* instruction) { |
| 606 | if (instruction->previous_ != nullptr) { |
| 607 | instruction->previous_->next_ = instruction->next_; |
| 608 | } |
| 609 | if (instruction->next_ != nullptr) { |
| 610 | instruction->next_->previous_ = instruction->previous_; |
| 611 | } |
| 612 | if (instruction == first_instruction_) { |
| 613 | first_instruction_ = instruction->next_; |
| 614 | } |
| 615 | if (instruction == last_instruction_) { |
| 616 | last_instruction_ = instruction->previous_; |
| 617 | } |
| 618 | } |
| 619 | |
Roland Levillain | 6b46923 | 2014-09-25 10:10:38 +0100 | [diff] [blame] | 620 | bool HInstructionList::Contains(HInstruction* instruction) const { |
| 621 | for (HInstructionIterator it(*this); !it.Done(); it.Advance()) { |
| 622 | if (it.Current() == instruction) { |
| 623 | return true; |
| 624 | } |
| 625 | } |
| 626 | return false; |
| 627 | } |
| 628 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 629 | bool HInstructionList::FoundBefore(const HInstruction* instruction1, |
| 630 | const HInstruction* instruction2) const { |
| 631 | DCHECK_EQ(instruction1->GetBlock(), instruction2->GetBlock()); |
| 632 | for (HInstructionIterator it(*this); !it.Done(); it.Advance()) { |
| 633 | if (it.Current() == instruction1) { |
| 634 | return true; |
| 635 | } |
| 636 | if (it.Current() == instruction2) { |
| 637 | return false; |
| 638 | } |
| 639 | } |
| 640 | LOG(FATAL) << "Did not find an order between two instructions of the same block."; |
| 641 | return true; |
| 642 | } |
| 643 | |
Roland Levillain | 6c82d40 | 2014-10-13 16:10:27 +0100 | [diff] [blame] | 644 | bool HInstruction::StrictlyDominates(HInstruction* other_instruction) const { |
| 645 | if (other_instruction == this) { |
| 646 | // An instruction does not strictly dominate itself. |
| 647 | return false; |
| 648 | } |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 649 | HBasicBlock* block = GetBlock(); |
| 650 | HBasicBlock* other_block = other_instruction->GetBlock(); |
| 651 | if (block != other_block) { |
| 652 | return GetBlock()->Dominates(other_instruction->GetBlock()); |
| 653 | } else { |
| 654 | // If both instructions are in the same block, ensure this |
| 655 | // instruction comes before `other_instruction`. |
| 656 | if (IsPhi()) { |
| 657 | if (!other_instruction->IsPhi()) { |
| 658 | // Phis appear before non phi-instructions so this instruction |
| 659 | // dominates `other_instruction`. |
| 660 | return true; |
| 661 | } else { |
| 662 | // There is no order among phis. |
| 663 | LOG(FATAL) << "There is no dominance between phis of a same block."; |
| 664 | return false; |
| 665 | } |
| 666 | } else { |
| 667 | // `this` is not a phi. |
| 668 | if (other_instruction->IsPhi()) { |
| 669 | // Phis appear before non phi-instructions so this instruction |
| 670 | // does not dominate `other_instruction`. |
| 671 | return false; |
| 672 | } else { |
| 673 | // Check whether this instruction comes before |
| 674 | // `other_instruction` in the instruction list. |
| 675 | return block->GetInstructions().FoundBefore(this, other_instruction); |
| 676 | } |
| 677 | } |
| 678 | } |
| 679 | } |
| 680 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 681 | void HInstruction::ReplaceWith(HInstruction* other) { |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 682 | DCHECK(other != nullptr); |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 683 | for (HUseIterator<HInstruction*> it(GetUses()); !it.Done(); it.Advance()) { |
| 684 | HUseListNode<HInstruction*>* current = it.Current(); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 685 | HInstruction* user = current->GetUser(); |
| 686 | size_t input_index = current->GetIndex(); |
| 687 | user->SetRawInputAt(input_index, other); |
| 688 | other->AddUseAt(user, input_index); |
| 689 | } |
| 690 | |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 691 | for (HUseIterator<HEnvironment*> it(GetEnvUses()); !it.Done(); it.Advance()) { |
| 692 | HUseListNode<HEnvironment*>* current = it.Current(); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 693 | HEnvironment* user = current->GetUser(); |
| 694 | size_t input_index = current->GetIndex(); |
| 695 | user->SetRawEnvAt(input_index, other); |
| 696 | other->AddEnvUseAt(user, input_index); |
| 697 | } |
| 698 | |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 699 | uses_.Clear(); |
| 700 | env_uses_.Clear(); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 701 | } |
| 702 | |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 703 | void HInstruction::ReplaceInput(HInstruction* replacement, size_t index) { |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 704 | RemoveAsUserOfInput(index); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 705 | SetRawInputAt(index, replacement); |
| 706 | replacement->AddUseAt(this, index); |
| 707 | } |
| 708 | |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 709 | size_t HInstruction::EnvironmentSize() const { |
| 710 | return HasEnvironment() ? environment_->Size() : 0; |
| 711 | } |
| 712 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 713 | void HPhi::AddInput(HInstruction* input) { |
| 714 | DCHECK(input->GetBlock() != nullptr); |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 715 | inputs_.Add(HUserRecord<HInstruction*>(input)); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 716 | input->AddUseAt(this, inputs_.Size() - 1); |
| 717 | } |
| 718 | |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 719 | void HPhi::RemoveInputAt(size_t index) { |
| 720 | RemoveAsUserOfInput(index); |
| 721 | inputs_.DeleteAt(index); |
Nicolas Geoffray | 5d7b7f8 | 2015-04-28 00:52:43 +0100 | [diff] [blame] | 722 | for (size_t i = index, e = InputCount(); i < e; ++i) { |
| 723 | InputRecordAt(i).GetUseNode()->SetIndex(i); |
| 724 | } |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 725 | } |
| 726 | |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 727 | #define DEFINE_ACCEPT(name, super) \ |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 728 | void H##name::Accept(HGraphVisitor* visitor) { \ |
| 729 | visitor->Visit##name(this); \ |
| 730 | } |
| 731 | |
| 732 | FOR_EACH_INSTRUCTION(DEFINE_ACCEPT) |
| 733 | |
| 734 | #undef DEFINE_ACCEPT |
| 735 | |
| 736 | void HGraphVisitor::VisitInsertionOrder() { |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 737 | const GrowableArray<HBasicBlock*>& blocks = graph_->GetBlocks(); |
| 738 | for (size_t i = 0 ; i < blocks.Size(); i++) { |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 739 | HBasicBlock* block = blocks.Get(i); |
| 740 | if (block != nullptr) { |
| 741 | VisitBasicBlock(block); |
| 742 | } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 743 | } |
| 744 | } |
| 745 | |
Roland Levillain | 633021e | 2014-10-01 14:12:25 +0100 | [diff] [blame] | 746 | void HGraphVisitor::VisitReversePostOrder() { |
| 747 | for (HReversePostOrderIterator it(*graph_); !it.Done(); it.Advance()) { |
| 748 | VisitBasicBlock(it.Current()); |
| 749 | } |
| 750 | } |
| 751 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 752 | void HGraphVisitor::VisitBasicBlock(HBasicBlock* block) { |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 753 | for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) { |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 754 | it.Current()->Accept(this); |
| 755 | } |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 756 | for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) { |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 757 | it.Current()->Accept(this); |
| 758 | } |
| 759 | } |
| 760 | |
Roland Levillain | 9240d6a | 2014-10-20 16:47:04 +0100 | [diff] [blame] | 761 | HConstant* HUnaryOperation::TryStaticEvaluation() const { |
| 762 | if (GetInput()->IsIntConstant()) { |
| 763 | int32_t value = Evaluate(GetInput()->AsIntConstant()->GetValue()); |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 764 | return GetBlock()->GetGraph()->GetIntConstant(value); |
Roland Levillain | 9240d6a | 2014-10-20 16:47:04 +0100 | [diff] [blame] | 765 | } else if (GetInput()->IsLongConstant()) { |
Roland Levillain | b762d2e | 2014-10-22 10:11:06 +0100 | [diff] [blame] | 766 | // TODO: Implement static evaluation of long unary operations. |
| 767 | // |
| 768 | // Do not exit with a fatal condition here. Instead, simply |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 769 | // return `null' to notify the caller that this instruction |
Roland Levillain | b762d2e | 2014-10-22 10:11:06 +0100 | [diff] [blame] | 770 | // cannot (yet) be statically evaluated. |
Roland Levillain | 9240d6a | 2014-10-20 16:47:04 +0100 | [diff] [blame] | 771 | return nullptr; |
| 772 | } |
| 773 | return nullptr; |
| 774 | } |
| 775 | |
| 776 | HConstant* HBinaryOperation::TryStaticEvaluation() const { |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 777 | if (GetLeft()->IsIntConstant() && GetRight()->IsIntConstant()) { |
| 778 | int32_t value = Evaluate(GetLeft()->AsIntConstant()->GetValue(), |
| 779 | GetRight()->AsIntConstant()->GetValue()); |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 780 | return GetBlock()->GetGraph()->GetIntConstant(value); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 781 | } else if (GetLeft()->IsLongConstant() && GetRight()->IsLongConstant()) { |
| 782 | int64_t value = Evaluate(GetLeft()->AsLongConstant()->GetValue(), |
| 783 | GetRight()->AsLongConstant()->GetValue()); |
Nicolas Geoffray | 9ee6618 | 2015-01-16 12:35:40 +0000 | [diff] [blame] | 784 | if (GetResultType() == Primitive::kPrimLong) { |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 785 | return GetBlock()->GetGraph()->GetLongConstant(value); |
Nicolas Geoffray | 9ee6618 | 2015-01-16 12:35:40 +0000 | [diff] [blame] | 786 | } else { |
Nicolas Geoffray | 6c2dff8 | 2015-01-21 14:56:54 +0000 | [diff] [blame] | 787 | DCHECK_EQ(GetResultType(), Primitive::kPrimInt); |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 788 | return GetBlock()->GetGraph()->GetIntConstant(static_cast<int32_t>(value)); |
Nicolas Geoffray | 9ee6618 | 2015-01-16 12:35:40 +0000 | [diff] [blame] | 789 | } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 790 | } |
| 791 | return nullptr; |
| 792 | } |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 793 | |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 794 | HConstant* HBinaryOperation::GetConstantRight() const { |
| 795 | if (GetRight()->IsConstant()) { |
| 796 | return GetRight()->AsConstant(); |
| 797 | } else if (IsCommutative() && GetLeft()->IsConstant()) { |
| 798 | return GetLeft()->AsConstant(); |
| 799 | } else { |
| 800 | return nullptr; |
| 801 | } |
| 802 | } |
| 803 | |
| 804 | // If `GetConstantRight()` returns one of the input, this returns the other |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 805 | // one. Otherwise it returns null. |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 806 | HInstruction* HBinaryOperation::GetLeastConstantLeft() const { |
| 807 | HInstruction* most_constant_right = GetConstantRight(); |
| 808 | if (most_constant_right == nullptr) { |
| 809 | return nullptr; |
| 810 | } else if (most_constant_right == GetLeft()) { |
| 811 | return GetRight(); |
| 812 | } else { |
| 813 | return GetLeft(); |
| 814 | } |
| 815 | } |
| 816 | |
Mingyao Yang | d43b3ac | 2015-04-01 14:03:04 -0700 | [diff] [blame] | 817 | bool HCondition::IsBeforeWhenDisregardMoves(HInstruction* instruction) const { |
| 818 | return this == instruction->GetPreviousDisregardingMoves(); |
Nicolas Geoffray | 18efde5 | 2014-09-22 15:51:11 +0100 | [diff] [blame] | 819 | } |
| 820 | |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 821 | bool HInstruction::Equals(HInstruction* other) const { |
| 822 | if (!InstructionTypeEquals(other)) return false; |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 823 | DCHECK_EQ(GetKind(), other->GetKind()); |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 824 | if (!InstructionDataEquals(other)) return false; |
| 825 | if (GetType() != other->GetType()) return false; |
| 826 | if (InputCount() != other->InputCount()) return false; |
| 827 | |
| 828 | for (size_t i = 0, e = InputCount(); i < e; ++i) { |
| 829 | if (InputAt(i) != other->InputAt(i)) return false; |
| 830 | } |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 831 | DCHECK_EQ(ComputeHashCode(), other->ComputeHashCode()); |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 832 | return true; |
| 833 | } |
| 834 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 835 | std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs) { |
| 836 | #define DECLARE_CASE(type, super) case HInstruction::k##type: os << #type; break; |
| 837 | switch (rhs) { |
| 838 | FOR_EACH_INSTRUCTION(DECLARE_CASE) |
| 839 | default: |
| 840 | os << "Unknown instruction kind " << static_cast<int>(rhs); |
| 841 | break; |
| 842 | } |
| 843 | #undef DECLARE_CASE |
| 844 | return os; |
| 845 | } |
| 846 | |
Nicolas Geoffray | 82091da | 2015-01-26 10:02:45 +0000 | [diff] [blame] | 847 | void HInstruction::MoveBefore(HInstruction* cursor) { |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 848 | next_->previous_ = previous_; |
| 849 | if (previous_ != nullptr) { |
| 850 | previous_->next_ = next_; |
| 851 | } |
| 852 | if (block_->instructions_.first_instruction_ == this) { |
| 853 | block_->instructions_.first_instruction_ = next_; |
| 854 | } |
Nicolas Geoffray | 82091da | 2015-01-26 10:02:45 +0000 | [diff] [blame] | 855 | DCHECK_NE(block_->instructions_.last_instruction_, this); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 856 | |
| 857 | previous_ = cursor->previous_; |
| 858 | if (previous_ != nullptr) { |
| 859 | previous_->next_ = this; |
| 860 | } |
| 861 | next_ = cursor; |
| 862 | cursor->previous_ = this; |
| 863 | block_ = cursor->block_; |
Nicolas Geoffray | 82091da | 2015-01-26 10:02:45 +0000 | [diff] [blame] | 864 | |
| 865 | if (block_->instructions_.first_instruction_ == cursor) { |
| 866 | block_->instructions_.first_instruction_ = this; |
| 867 | } |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 868 | } |
| 869 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 870 | HBasicBlock* HBasicBlock::SplitAfter(HInstruction* cursor) { |
| 871 | DCHECK(!cursor->IsControlFlow()); |
| 872 | DCHECK_NE(instructions_.last_instruction_, cursor); |
| 873 | DCHECK_EQ(cursor->GetBlock(), this); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 874 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 875 | HBasicBlock* new_block = new (GetGraph()->GetArena()) HBasicBlock(GetGraph(), GetDexPc()); |
| 876 | new_block->instructions_.first_instruction_ = cursor->GetNext(); |
| 877 | new_block->instructions_.last_instruction_ = instructions_.last_instruction_; |
| 878 | cursor->next_->previous_ = nullptr; |
| 879 | cursor->next_ = nullptr; |
| 880 | instructions_.last_instruction_ = cursor; |
| 881 | |
| 882 | new_block->instructions_.SetBlockOfInstructions(new_block); |
| 883 | for (size_t i = 0, e = GetSuccessors().Size(); i < e; ++i) { |
| 884 | HBasicBlock* successor = GetSuccessors().Get(i); |
| 885 | new_block->successors_.Add(successor); |
| 886 | successor->predecessors_.Put(successor->GetPredecessorIndexOf(this), new_block); |
| 887 | } |
| 888 | successors_.Reset(); |
| 889 | |
| 890 | for (size_t i = 0, e = GetDominatedBlocks().Size(); i < e; ++i) { |
| 891 | HBasicBlock* dominated = GetDominatedBlocks().Get(i); |
| 892 | dominated->dominator_ = new_block; |
| 893 | new_block->dominated_blocks_.Add(dominated); |
| 894 | } |
| 895 | dominated_blocks_.Reset(); |
| 896 | return new_block; |
| 897 | } |
| 898 | |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 899 | bool HBasicBlock::IsSingleGoto() const { |
| 900 | HLoopInformation* loop_info = GetLoopInformation(); |
| 901 | // TODO: Remove the null check b/19084197. |
| 902 | return GetFirstInstruction() != nullptr |
| 903 | && GetPhis().IsEmpty() |
| 904 | && GetFirstInstruction() == GetLastInstruction() |
| 905 | && GetLastInstruction()->IsGoto() |
| 906 | // Back edges generate the suspend check. |
| 907 | && (loop_info == nullptr || !loop_info->IsBackEdge(*this)); |
| 908 | } |
| 909 | |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 910 | bool HBasicBlock::EndsWithControlFlowInstruction() const { |
| 911 | return !GetInstructions().IsEmpty() && GetLastInstruction()->IsControlFlow(); |
| 912 | } |
| 913 | |
David Brazdil | b2bd1c5 | 2015-03-25 11:17:37 +0000 | [diff] [blame] | 914 | bool HBasicBlock::EndsWithIf() const { |
| 915 | return !GetInstructions().IsEmpty() && GetLastInstruction()->IsIf(); |
| 916 | } |
| 917 | |
| 918 | bool HBasicBlock::HasSinglePhi() const { |
| 919 | return !GetPhis().IsEmpty() && GetFirstPhi()->GetNext() == nullptr; |
| 920 | } |
| 921 | |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 922 | size_t HInstructionList::CountSize() const { |
| 923 | size_t size = 0; |
| 924 | HInstruction* current = first_instruction_; |
| 925 | for (; current != nullptr; current = current->GetNext()) { |
| 926 | size++; |
| 927 | } |
| 928 | return size; |
| 929 | } |
| 930 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 931 | void HInstructionList::SetBlockOfInstructions(HBasicBlock* block) const { |
| 932 | for (HInstruction* current = first_instruction_; |
| 933 | current != nullptr; |
| 934 | current = current->GetNext()) { |
| 935 | current->SetBlock(block); |
| 936 | } |
| 937 | } |
| 938 | |
| 939 | void HInstructionList::AddAfter(HInstruction* cursor, const HInstructionList& instruction_list) { |
| 940 | DCHECK(Contains(cursor)); |
| 941 | if (!instruction_list.IsEmpty()) { |
| 942 | if (cursor == last_instruction_) { |
| 943 | last_instruction_ = instruction_list.last_instruction_; |
| 944 | } else { |
| 945 | cursor->next_->previous_ = instruction_list.last_instruction_; |
| 946 | } |
| 947 | instruction_list.last_instruction_->next_ = cursor->next_; |
| 948 | cursor->next_ = instruction_list.first_instruction_; |
| 949 | instruction_list.first_instruction_->previous_ = cursor; |
| 950 | } |
| 951 | } |
| 952 | |
| 953 | void HInstructionList::Add(const HInstructionList& instruction_list) { |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 954 | if (IsEmpty()) { |
| 955 | first_instruction_ = instruction_list.first_instruction_; |
| 956 | last_instruction_ = instruction_list.last_instruction_; |
| 957 | } else { |
| 958 | AddAfter(last_instruction_, instruction_list); |
| 959 | } |
| 960 | } |
| 961 | |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 962 | void HBasicBlock::DisconnectAndDelete() { |
| 963 | // Dominators must be removed after all the blocks they dominate. This way |
| 964 | // a loop header is removed last, a requirement for correct loop information |
| 965 | // iteration. |
| 966 | DCHECK(dominated_blocks_.IsEmpty()); |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 967 | |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 968 | // Remove the block from all loops it is included in. |
| 969 | for (HLoopInformationOutwardIterator it(*this); !it.Done(); it.Advance()) { |
| 970 | HLoopInformation* loop_info = it.Current(); |
| 971 | loop_info->Remove(this); |
| 972 | if (loop_info->IsBackEdge(*this)) { |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 973 | // If this was the last back edge of the loop, we deliberately leave the |
| 974 | // loop in an inconsistent state and will fail SSAChecker unless the |
| 975 | // entire loop is removed during the pass. |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 976 | loop_info->RemoveBackEdge(this); |
| 977 | } |
| 978 | } |
| 979 | |
| 980 | // Disconnect the block from its predecessors and update their control-flow |
| 981 | // instructions. |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 982 | for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) { |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 983 | HBasicBlock* predecessor = predecessors_.Get(i); |
| 984 | HInstruction* last_instruction = predecessor->GetLastInstruction(); |
| 985 | predecessor->RemoveInstruction(last_instruction); |
| 986 | predecessor->RemoveSuccessor(this); |
| 987 | if (predecessor->GetSuccessors().Size() == 1u) { |
| 988 | DCHECK(last_instruction->IsIf()); |
| 989 | predecessor->AddInstruction(new (graph_->GetArena()) HGoto()); |
| 990 | } else { |
| 991 | // The predecessor has no remaining successors and therefore must be dead. |
| 992 | // We deliberately leave it without a control-flow instruction so that the |
| 993 | // SSAChecker fails unless it is not removed during the pass too. |
| 994 | DCHECK_EQ(predecessor->GetSuccessors().Size(), 0u); |
| 995 | } |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 996 | } |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 997 | predecessors_.Reset(); |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 998 | |
| 999 | // Disconnect the block from its successors and update their dominators |
| 1000 | // and phis. |
| 1001 | for (size_t i = 0, e = successors_.Size(); i < e; ++i) { |
| 1002 | HBasicBlock* successor = successors_.Get(i); |
| 1003 | // Delete this block from the list of predecessors. |
| 1004 | size_t this_index = successor->GetPredecessorIndexOf(this); |
| 1005 | successor->predecessors_.DeleteAt(this_index); |
| 1006 | |
| 1007 | // Check that `successor` has other predecessors, otherwise `this` is the |
| 1008 | // dominator of `successor` which violates the order DCHECKed at the top. |
| 1009 | DCHECK(!successor->predecessors_.IsEmpty()); |
| 1010 | |
| 1011 | // Recompute the successor's dominator. |
| 1012 | HBasicBlock* old_dominator = successor->GetDominator(); |
| 1013 | HBasicBlock* new_dominator = successor->predecessors_.Get(0); |
| 1014 | for (size_t j = 1, f = successor->predecessors_.Size(); j < f; ++j) { |
| 1015 | new_dominator = graph_->FindCommonDominator( |
| 1016 | new_dominator, successor->predecessors_.Get(j)); |
| 1017 | } |
| 1018 | if (old_dominator != new_dominator) { |
| 1019 | successor->SetDominator(new_dominator); |
| 1020 | old_dominator->RemoveDominatedBlock(successor); |
| 1021 | new_dominator->AddDominatedBlock(successor); |
| 1022 | } |
| 1023 | |
| 1024 | // Remove this block's entries in the successor's phis. |
| 1025 | if (successor->predecessors_.Size() == 1u) { |
| 1026 | // The successor has just one predecessor left. Replace phis with the only |
| 1027 | // remaining input. |
| 1028 | for (HInstructionIterator phi_it(successor->GetPhis()); !phi_it.Done(); phi_it.Advance()) { |
| 1029 | HPhi* phi = phi_it.Current()->AsPhi(); |
| 1030 | phi->ReplaceWith(phi->InputAt(1 - this_index)); |
| 1031 | successor->RemovePhi(phi); |
| 1032 | } |
| 1033 | } else { |
| 1034 | for (HInstructionIterator phi_it(successor->GetPhis()); !phi_it.Done(); phi_it.Advance()) { |
| 1035 | phi_it.Current()->AsPhi()->RemoveInputAt(this_index); |
| 1036 | } |
| 1037 | } |
| 1038 | } |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 1039 | successors_.Reset(); |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1040 | |
| 1041 | // Disconnect from the dominator. |
| 1042 | dominator_->RemoveDominatedBlock(this); |
| 1043 | SetDominator(nullptr); |
| 1044 | |
| 1045 | // Delete from the graph. The function safely deletes remaining instructions |
| 1046 | // and updates the reverse post order. |
| 1047 | graph_->DeleteDeadBlock(this); |
| 1048 | SetGraph(nullptr); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1049 | } |
| 1050 | |
David Brazdil | 69a2804 | 2015-04-29 17:16:07 +0100 | [diff] [blame] | 1051 | void HBasicBlock::UpdateLoopInformation() { |
| 1052 | // Check if loop information points to a dismantled loop. If so, replace with |
| 1053 | // the loop information of a larger loop which contains this block, or nullptr |
| 1054 | // otherwise. We iterate in case the larger loop has been destroyed too. |
| 1055 | while (IsInLoop() && loop_information_->GetBackEdges().IsEmpty()) { |
| 1056 | if (IsLoopHeader()) { |
| 1057 | HSuspendCheck* suspend_check = loop_information_->GetSuspendCheck(); |
| 1058 | DCHECK_EQ(suspend_check->GetBlock(), this); |
| 1059 | RemoveInstruction(suspend_check); |
| 1060 | } |
| 1061 | loop_information_ = loop_information_->GetPreHeader()->GetLoopInformation(); |
| 1062 | } |
| 1063 | } |
| 1064 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1065 | void HBasicBlock::MergeWith(HBasicBlock* other) { |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1066 | DCHECK_EQ(GetGraph(), other->GetGraph()); |
| 1067 | DCHECK(GetDominatedBlocks().Contains(other)); |
| 1068 | DCHECK_EQ(GetSuccessors().Size(), 1u); |
| 1069 | DCHECK_EQ(GetSuccessors().Get(0), other); |
| 1070 | DCHECK_EQ(other->GetPredecessors().Size(), 1u); |
| 1071 | DCHECK_EQ(other->GetPredecessors().Get(0), this); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1072 | DCHECK(other->GetPhis().IsEmpty()); |
| 1073 | |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1074 | // Move instructions from `other` to `this`. |
| 1075 | DCHECK(EndsWithControlFlowInstruction()); |
| 1076 | RemoveInstruction(GetLastInstruction()); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1077 | instructions_.Add(other->GetInstructions()); |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1078 | other->instructions_.SetBlockOfInstructions(this); |
| 1079 | other->instructions_.Clear(); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1080 | |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1081 | // Remove `other` from the loops it is included in. |
| 1082 | for (HLoopInformationOutwardIterator it(*other); !it.Done(); it.Advance()) { |
| 1083 | HLoopInformation* loop_info = it.Current(); |
| 1084 | loop_info->Remove(other); |
| 1085 | if (loop_info->IsBackEdge(*other)) { |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 1086 | loop_info->ReplaceBackEdge(other, this); |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1087 | } |
| 1088 | } |
| 1089 | |
| 1090 | // Update links to the successors of `other`. |
| 1091 | successors_.Reset(); |
| 1092 | while (!other->successors_.IsEmpty()) { |
| 1093 | HBasicBlock* successor = other->successors_.Get(0); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1094 | successor->ReplacePredecessor(other, this); |
| 1095 | } |
| 1096 | |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1097 | // Update the dominator tree. |
| 1098 | dominated_blocks_.Delete(other); |
| 1099 | for (size_t i = 0, e = other->GetDominatedBlocks().Size(); i < e; ++i) { |
| 1100 | HBasicBlock* dominated = other->GetDominatedBlocks().Get(i); |
| 1101 | dominated_blocks_.Add(dominated); |
| 1102 | dominated->SetDominator(this); |
| 1103 | } |
| 1104 | other->dominated_blocks_.Reset(); |
| 1105 | other->dominator_ = nullptr; |
| 1106 | |
| 1107 | // Clear the list of predecessors of `other` in preparation of deleting it. |
| 1108 | other->predecessors_.Reset(); |
| 1109 | |
| 1110 | // Delete `other` from the graph. The function updates reverse post order. |
| 1111 | graph_->DeleteDeadBlock(other); |
| 1112 | other->SetGraph(nullptr); |
| 1113 | } |
| 1114 | |
| 1115 | void HBasicBlock::MergeWithInlined(HBasicBlock* other) { |
| 1116 | DCHECK_NE(GetGraph(), other->GetGraph()); |
| 1117 | DCHECK(GetDominatedBlocks().IsEmpty()); |
| 1118 | DCHECK(GetSuccessors().IsEmpty()); |
| 1119 | DCHECK(!EndsWithControlFlowInstruction()); |
| 1120 | DCHECK_EQ(other->GetPredecessors().Size(), 1u); |
| 1121 | DCHECK(other->GetPredecessors().Get(0)->IsEntryBlock()); |
| 1122 | DCHECK(other->GetPhis().IsEmpty()); |
| 1123 | DCHECK(!other->IsInLoop()); |
| 1124 | |
| 1125 | // Move instructions from `other` to `this`. |
| 1126 | instructions_.Add(other->GetInstructions()); |
| 1127 | other->instructions_.SetBlockOfInstructions(this); |
| 1128 | |
| 1129 | // Update links to the successors of `other`. |
| 1130 | successors_.Reset(); |
| 1131 | while (!other->successors_.IsEmpty()) { |
| 1132 | HBasicBlock* successor = other->successors_.Get(0); |
| 1133 | successor->ReplacePredecessor(other, this); |
| 1134 | } |
| 1135 | |
| 1136 | // Update the dominator tree. |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1137 | for (size_t i = 0, e = other->GetDominatedBlocks().Size(); i < e; ++i) { |
| 1138 | HBasicBlock* dominated = other->GetDominatedBlocks().Get(i); |
| 1139 | dominated_blocks_.Add(dominated); |
| 1140 | dominated->SetDominator(this); |
| 1141 | } |
| 1142 | other->dominated_blocks_.Reset(); |
| 1143 | other->dominator_ = nullptr; |
| 1144 | other->graph_ = nullptr; |
| 1145 | } |
| 1146 | |
| 1147 | void HBasicBlock::ReplaceWith(HBasicBlock* other) { |
| 1148 | while (!GetPredecessors().IsEmpty()) { |
| 1149 | HBasicBlock* predecessor = GetPredecessors().Get(0); |
| 1150 | predecessor->ReplaceSuccessor(this, other); |
| 1151 | } |
| 1152 | while (!GetSuccessors().IsEmpty()) { |
| 1153 | HBasicBlock* successor = GetSuccessors().Get(0); |
| 1154 | successor->ReplacePredecessor(this, other); |
| 1155 | } |
| 1156 | for (size_t i = 0; i < dominated_blocks_.Size(); ++i) { |
| 1157 | other->AddDominatedBlock(dominated_blocks_.Get(i)); |
| 1158 | } |
| 1159 | GetDominator()->ReplaceDominatedBlock(this, other); |
| 1160 | other->SetDominator(GetDominator()); |
| 1161 | dominator_ = nullptr; |
| 1162 | graph_ = nullptr; |
| 1163 | } |
| 1164 | |
| 1165 | // Create space in `blocks` for adding `number_of_new_blocks` entries |
| 1166 | // starting at location `at`. Blocks after `at` are moved accordingly. |
| 1167 | static void MakeRoomFor(GrowableArray<HBasicBlock*>* blocks, |
| 1168 | size_t number_of_new_blocks, |
| 1169 | size_t at) { |
| 1170 | size_t old_size = blocks->Size(); |
| 1171 | size_t new_size = old_size + number_of_new_blocks; |
| 1172 | blocks->SetSize(new_size); |
| 1173 | for (size_t i = old_size - 1, j = new_size - 1; i > at; --i, --j) { |
| 1174 | blocks->Put(j, blocks->Get(i)); |
| 1175 | } |
| 1176 | } |
| 1177 | |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1178 | void HGraph::DeleteDeadBlock(HBasicBlock* block) { |
| 1179 | DCHECK_EQ(block->GetGraph(), this); |
| 1180 | DCHECK(block->GetSuccessors().IsEmpty()); |
| 1181 | DCHECK(block->GetPredecessors().IsEmpty()); |
| 1182 | DCHECK(block->GetDominatedBlocks().IsEmpty()); |
| 1183 | DCHECK(block->GetDominator() == nullptr); |
| 1184 | |
| 1185 | for (HBackwardInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) { |
| 1186 | block->RemoveInstruction(it.Current()); |
| 1187 | } |
| 1188 | for (HBackwardInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) { |
| 1189 | block->RemovePhi(it.Current()->AsPhi()); |
| 1190 | } |
| 1191 | |
| 1192 | reverse_post_order_.Delete(block); |
| 1193 | blocks_.Put(block->GetBlockId(), nullptr); |
| 1194 | } |
| 1195 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1196 | void HGraph::InlineInto(HGraph* outer_graph, HInvoke* invoke) { |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1197 | if (GetBlocks().Size() == 3) { |
Nicolas Geoffray | be31ff9 | 2015-02-04 14:52:20 +0000 | [diff] [blame] | 1198 | // Simple case of an entry block, a body block, and an exit block. |
| 1199 | // Put the body block's instruction into `invoke`'s block. |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1200 | HBasicBlock* body = GetBlocks().Get(1); |
Nicolas Geoffray | be31ff9 | 2015-02-04 14:52:20 +0000 | [diff] [blame] | 1201 | DCHECK(GetBlocks().Get(0)->IsEntryBlock()); |
| 1202 | DCHECK(GetBlocks().Get(2)->IsExitBlock()); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1203 | DCHECK(!body->IsExitBlock()); |
| 1204 | HInstruction* last = body->GetLastInstruction(); |
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()->instructions_.AddAfter(invoke, body->GetInstructions()); |
| 1207 | body->GetInstructions().SetBlockOfInstructions(invoke->GetBlock()); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1208 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1209 | // Replace the invoke with the return value of the inlined graph. |
| 1210 | if (last->IsReturn()) { |
| 1211 | invoke->ReplaceWith(last->InputAt(0)); |
| 1212 | } else { |
| 1213 | DCHECK(last->IsReturnVoid()); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1214 | } |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1215 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1216 | invoke->GetBlock()->RemoveInstruction(last); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1217 | } else { |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1218 | // Need to inline multiple blocks. We split `invoke`'s block |
| 1219 | // into two blocks, merge the first block of the inlined graph into |
Nicolas Geoffray | be31ff9 | 2015-02-04 14:52:20 +0000 | [diff] [blame] | 1220 | // the first half, and replace the exit block of the inlined graph |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1221 | // with the second half. |
| 1222 | ArenaAllocator* allocator = outer_graph->GetArena(); |
| 1223 | HBasicBlock* at = invoke->GetBlock(); |
| 1224 | HBasicBlock* to = at->SplitAfter(invoke); |
| 1225 | |
| 1226 | HBasicBlock* first = entry_block_->GetSuccessors().Get(0); |
| 1227 | DCHECK(!first->IsInLoop()); |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1228 | at->MergeWithInlined(first); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1229 | exit_block_->ReplaceWith(to); |
| 1230 | |
| 1231 | // Update all predecessors of the exit block (now the `to` block) |
Nicolas Geoffray | 817bce7 | 2015-02-24 13:35:38 +0000 | [diff] [blame] | 1232 | // to not `HReturn` but `HGoto` instead. |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1233 | HInstruction* return_value = nullptr; |
Nicolas Geoffray | 817bce7 | 2015-02-24 13:35:38 +0000 | [diff] [blame] | 1234 | bool returns_void = to->GetPredecessors().Get(0)->GetLastInstruction()->IsReturnVoid(); |
| 1235 | if (to->GetPredecessors().Size() == 1) { |
| 1236 | HBasicBlock* predecessor = to->GetPredecessors().Get(0); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1237 | HInstruction* last = predecessor->GetLastInstruction(); |
Nicolas Geoffray | 817bce7 | 2015-02-24 13:35:38 +0000 | [diff] [blame] | 1238 | if (!returns_void) { |
| 1239 | return_value = last->InputAt(0); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1240 | } |
| 1241 | predecessor->AddInstruction(new (allocator) HGoto()); |
| 1242 | predecessor->RemoveInstruction(last); |
Nicolas Geoffray | 817bce7 | 2015-02-24 13:35:38 +0000 | [diff] [blame] | 1243 | } else { |
| 1244 | if (!returns_void) { |
| 1245 | // There will be multiple returns. |
Nicolas Geoffray | 4f1a384 | 2015-03-12 10:34:11 +0000 | [diff] [blame] | 1246 | return_value = new (allocator) HPhi( |
| 1247 | allocator, kNoRegNumber, 0, HPhi::ToPhiType(invoke->GetType())); |
Nicolas Geoffray | 817bce7 | 2015-02-24 13:35:38 +0000 | [diff] [blame] | 1248 | to->AddPhi(return_value->AsPhi()); |
| 1249 | } |
| 1250 | for (size_t i = 0, e = to->GetPredecessors().Size(); i < e; ++i) { |
| 1251 | HBasicBlock* predecessor = to->GetPredecessors().Get(i); |
| 1252 | HInstruction* last = predecessor->GetLastInstruction(); |
| 1253 | if (!returns_void) { |
| 1254 | return_value->AsPhi()->AddInput(last->InputAt(0)); |
| 1255 | } |
| 1256 | predecessor->AddInstruction(new (allocator) HGoto()); |
| 1257 | predecessor->RemoveInstruction(last); |
| 1258 | } |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1259 | } |
| 1260 | |
| 1261 | if (return_value != nullptr) { |
| 1262 | invoke->ReplaceWith(return_value); |
| 1263 | } |
| 1264 | |
| 1265 | // Update the meta information surrounding blocks: |
| 1266 | // (1) the graph they are now in, |
| 1267 | // (2) the reverse post order of that graph, |
| 1268 | // (3) the potential loop information they are now in. |
| 1269 | |
| 1270 | // We don't add the entry block, the exit block, and the first block, which |
| 1271 | // has been merged with `at`. |
| 1272 | static constexpr int kNumberOfSkippedBlocksInCallee = 3; |
| 1273 | |
| 1274 | // We add the `to` block. |
| 1275 | static constexpr int kNumberOfNewBlocksInCaller = 1; |
| 1276 | size_t blocks_added = (reverse_post_order_.Size() - kNumberOfSkippedBlocksInCallee) |
| 1277 | + kNumberOfNewBlocksInCaller; |
| 1278 | |
| 1279 | // Find the location of `at` in the outer graph's reverse post order. The new |
| 1280 | // blocks will be added after it. |
| 1281 | size_t index_of_at = 0; |
| 1282 | while (outer_graph->reverse_post_order_.Get(index_of_at) != at) { |
| 1283 | index_of_at++; |
| 1284 | } |
| 1285 | MakeRoomFor(&outer_graph->reverse_post_order_, blocks_added, index_of_at); |
| 1286 | |
| 1287 | // Do a reverse post order of the blocks in the callee and do (1), (2), |
| 1288 | // and (3) to the blocks that apply. |
| 1289 | HLoopInformation* info = at->GetLoopInformation(); |
| 1290 | for (HReversePostOrderIterator it(*this); !it.Done(); it.Advance()) { |
| 1291 | HBasicBlock* current = it.Current(); |
| 1292 | if (current != exit_block_ && current != entry_block_ && current != first) { |
| 1293 | DCHECK(!current->IsInLoop()); |
| 1294 | DCHECK(current->GetGraph() == this); |
| 1295 | current->SetGraph(outer_graph); |
| 1296 | outer_graph->AddBlock(current); |
| 1297 | outer_graph->reverse_post_order_.Put(++index_of_at, current); |
| 1298 | if (info != nullptr) { |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1299 | current->SetLoopInformation(info); |
David Brazdil | 7d27537 | 2015-04-21 16:36:35 +0100 | [diff] [blame] | 1300 | for (HLoopInformationOutwardIterator loop_it(*at); !loop_it.Done(); loop_it.Advance()) { |
| 1301 | loop_it.Current()->Add(current); |
| 1302 | } |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1303 | } |
| 1304 | } |
| 1305 | } |
| 1306 | |
| 1307 | // Do (1), (2), and (3) to `to`. |
| 1308 | to->SetGraph(outer_graph); |
| 1309 | outer_graph->AddBlock(to); |
| 1310 | outer_graph->reverse_post_order_.Put(++index_of_at, to); |
| 1311 | if (info != nullptr) { |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1312 | to->SetLoopInformation(info); |
David Brazdil | 7d27537 | 2015-04-21 16:36:35 +0100 | [diff] [blame] | 1313 | for (HLoopInformationOutwardIterator loop_it(*at); !loop_it.Done(); loop_it.Advance()) { |
| 1314 | loop_it.Current()->Add(to); |
| 1315 | } |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 1316 | if (info->IsBackEdge(*at)) { |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 1317 | // Only `to` can become a back edge, as the inlined blocks |
| 1318 | // are predecessors of `to`. |
| 1319 | info->ReplaceBackEdge(at, to); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1320 | } |
| 1321 | } |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1322 | } |
Nicolas Geoffray | 7c5367b | 2014-12-17 10:13:46 +0000 | [diff] [blame] | 1323 | |
David Brazdil | 05144f4 | 2015-04-16 15:18:00 +0100 | [diff] [blame] | 1324 | // Update the next instruction id of the outer graph, so that instructions |
| 1325 | // added later get bigger ids than those in the inner graph. |
| 1326 | outer_graph->SetCurrentInstructionId(GetNextInstructionId()); |
| 1327 | |
| 1328 | // Walk over the entry block and: |
| 1329 | // - Move constants from the entry block to the outer_graph's entry block, |
| 1330 | // - Replace HParameterValue instructions with their real value. |
| 1331 | // - Remove suspend checks, that hold an environment. |
| 1332 | // We must do this after the other blocks have been inlined, otherwise ids of |
| 1333 | // constants could overlap with the inner graph. |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 1334 | size_t parameter_index = 0; |
David Brazdil | 05144f4 | 2015-04-16 15:18:00 +0100 | [diff] [blame] | 1335 | for (HInstructionIterator it(entry_block_->GetInstructions()); !it.Done(); it.Advance()) { |
| 1336 | HInstruction* current = it.Current(); |
| 1337 | if (current->IsNullConstant()) { |
| 1338 | current->ReplaceWith(outer_graph->GetNullConstant()); |
| 1339 | } else if (current->IsIntConstant()) { |
| 1340 | current->ReplaceWith(outer_graph->GetIntConstant(current->AsIntConstant()->GetValue())); |
| 1341 | } else if (current->IsLongConstant()) { |
| 1342 | current->ReplaceWith(outer_graph->GetLongConstant(current->AsLongConstant()->GetValue())); |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 1343 | } else if (current->IsFloatConstant()) { |
| 1344 | current->ReplaceWith(outer_graph->GetFloatConstant(current->AsFloatConstant()->GetValue())); |
| 1345 | } else if (current->IsDoubleConstant()) { |
| 1346 | current->ReplaceWith(outer_graph->GetDoubleConstant(current->AsDoubleConstant()->GetValue())); |
David Brazdil | 05144f4 | 2015-04-16 15:18:00 +0100 | [diff] [blame] | 1347 | } else if (current->IsParameterValue()) { |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 1348 | if (kIsDebugBuild |
| 1349 | && invoke->IsInvokeStaticOrDirect() |
| 1350 | && invoke->AsInvokeStaticOrDirect()->IsStaticWithExplicitClinitCheck()) { |
| 1351 | // Ensure we do not use the last input of `invoke`, as it |
| 1352 | // contains a clinit check which is not an actual argument. |
| 1353 | size_t last_input_index = invoke->InputCount() - 1; |
| 1354 | DCHECK(parameter_index != last_input_index); |
| 1355 | } |
David Brazdil | 05144f4 | 2015-04-16 15:18:00 +0100 | [diff] [blame] | 1356 | current->ReplaceWith(invoke->InputAt(parameter_index++)); |
| 1357 | } else { |
| 1358 | DCHECK(current->IsGoto() || current->IsSuspendCheck()); |
| 1359 | entry_block_->RemoveInstruction(current); |
| 1360 | } |
| 1361 | } |
| 1362 | |
Nicolas Geoffray | 7c5367b | 2014-12-17 10:13:46 +0000 | [diff] [blame] | 1363 | // Finally remove the invoke from the caller. |
| 1364 | invoke->GetBlock()->RemoveInstruction(invoke); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1365 | } |
| 1366 | |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 1367 | std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs) { |
| 1368 | ScopedObjectAccess soa(Thread::Current()); |
| 1369 | os << "[" |
| 1370 | << " is_top=" << rhs.IsTop() |
| 1371 | << " type=" << (rhs.IsTop() ? "?" : PrettyClass(rhs.GetTypeHandle().Get())) |
| 1372 | << " is_exact=" << rhs.IsExact() |
| 1373 | << " ]"; |
| 1374 | return os; |
| 1375 | } |
| 1376 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1377 | } // namespace art |