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