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