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" |
Vladimir Marko | 1f8695c | 2015-09-24 13:11:31 +0100 | [diff] [blame] | 23 | #include "base/stl_util.h" |
Nicolas Geoffray | a83a54d | 2015-10-02 17:30:26 +0100 | [diff] [blame] | 24 | #include "intrinsics.h" |
David Brazdil | baf89b8 | 2015-09-15 11:36:54 +0100 | [diff] [blame] | 25 | #include "mirror/class-inl.h" |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 26 | #include "scoped_thread_state_change.h" |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 27 | |
| 28 | namespace art { |
| 29 | |
| 30 | void HGraph::AddBlock(HBasicBlock* block) { |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 31 | block->SetBlockId(blocks_.size()); |
| 32 | blocks_.push_back(block); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 33 | } |
| 34 | |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 35 | void HGraph::FindBackEdges(ArenaBitVector* visited) { |
Vladimir Marko | 1f8695c | 2015-09-24 13:11:31 +0100 | [diff] [blame] | 36 | // "visited" must be empty on entry, it's an output argument for all visited (i.e. live) blocks. |
| 37 | DCHECK_EQ(visited->GetHighestBitSet(), -1); |
| 38 | |
| 39 | // Nodes that we're currently visiting, indexed by block id. |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 40 | ArenaBitVector visiting(arena_, blocks_.size(), false); |
Vladimir Marko | 1f8695c | 2015-09-24 13:11:31 +0100 | [diff] [blame] | 41 | // Number of successors visited from a given node, indexed by block id. |
| 42 | ArenaVector<size_t> successors_visited(blocks_.size(), 0u, arena_->Adapter()); |
| 43 | // Stack of nodes that we're currently visiting (same as marked in "visiting" above). |
| 44 | ArenaVector<HBasicBlock*> worklist(arena_->Adapter()); |
| 45 | constexpr size_t kDefaultWorklistSize = 8; |
| 46 | worklist.reserve(kDefaultWorklistSize); |
| 47 | visited->SetBit(entry_block_->GetBlockId()); |
| 48 | visiting.SetBit(entry_block_->GetBlockId()); |
| 49 | worklist.push_back(entry_block_); |
| 50 | |
| 51 | while (!worklist.empty()) { |
| 52 | HBasicBlock* current = worklist.back(); |
| 53 | uint32_t current_id = current->GetBlockId(); |
| 54 | if (successors_visited[current_id] == current->GetSuccessors().size()) { |
| 55 | visiting.ClearBit(current_id); |
| 56 | worklist.pop_back(); |
| 57 | } else { |
Vladimir Marko | 1f8695c | 2015-09-24 13:11:31 +0100 | [diff] [blame] | 58 | HBasicBlock* successor = current->GetSuccessors()[successors_visited[current_id]++]; |
| 59 | uint32_t successor_id = successor->GetBlockId(); |
| 60 | if (visiting.IsBitSet(successor_id)) { |
| 61 | DCHECK(ContainsElement(worklist, successor)); |
| 62 | successor->AddBackEdge(current); |
| 63 | } else if (!visited->IsBitSet(successor_id)) { |
| 64 | visited->SetBit(successor_id); |
| 65 | visiting.SetBit(successor_id); |
| 66 | worklist.push_back(successor); |
| 67 | } |
| 68 | } |
| 69 | } |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Roland Levillain | fc600dc | 2014-12-02 17:16:31 +0000 | [diff] [blame] | 72 | static void RemoveAsUser(HInstruction* instruction) { |
| 73 | for (size_t i = 0; i < instruction->InputCount(); i++) { |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 74 | instruction->RemoveAsUserOfInput(i); |
Roland Levillain | fc600dc | 2014-12-02 17:16:31 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 77 | for (HEnvironment* environment = instruction->GetEnvironment(); |
| 78 | environment != nullptr; |
| 79 | environment = environment->GetParent()) { |
Roland Levillain | fc600dc | 2014-12-02 17:16:31 +0000 | [diff] [blame] | 80 | for (size_t i = 0, e = environment->Size(); i < e; ++i) { |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 81 | if (environment->GetInstructionAt(i) != nullptr) { |
| 82 | environment->RemoveAsUserOfInput(i); |
Roland Levillain | fc600dc | 2014-12-02 17:16:31 +0000 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | void HGraph::RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const { |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 89 | for (size_t i = 0; i < blocks_.size(); ++i) { |
Roland Levillain | fc600dc | 2014-12-02 17:16:31 +0000 | [diff] [blame] | 90 | if (!visited.IsBitSet(i)) { |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 91 | HBasicBlock* block = blocks_[i]; |
Nicolas Geoffray | f776b92 | 2015-04-15 18:22:45 +0100 | [diff] [blame] | 92 | DCHECK(block->GetPhis().IsEmpty()) << "Phis are not inserted at this stage"; |
Roland Levillain | fc600dc | 2014-12-02 17:16:31 +0000 | [diff] [blame] | 93 | for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) { |
| 94 | RemoveAsUser(it.Current()); |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
Nicolas Geoffray | f776b92 | 2015-04-15 18:22:45 +0100 | [diff] [blame] | 100 | void HGraph::RemoveDeadBlocks(const ArenaBitVector& visited) { |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 101 | for (size_t i = 0; i < blocks_.size(); ++i) { |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 102 | if (!visited.IsBitSet(i)) { |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 103 | HBasicBlock* block = blocks_[i]; |
Nicolas Geoffray | f776b92 | 2015-04-15 18:22:45 +0100 | [diff] [blame] | 104 | // We only need to update the successor, which might be live. |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 105 | for (HBasicBlock* successor : block->GetSuccessors()) { |
| 106 | successor->RemovePredecessor(block); |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 107 | } |
Nicolas Geoffray | f776b92 | 2015-04-15 18:22:45 +0100 | [diff] [blame] | 108 | // Remove the block from the list of blocks, so that further analyses |
| 109 | // never see it. |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 110 | blocks_[i] = nullptr; |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 115 | void HGraph::BuildDominatorTree() { |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 116 | // (1) Simplify the CFG so that catch blocks have only exceptional incoming |
| 117 | // edges. This invariant simplifies building SSA form because Phis cannot |
| 118 | // collect both normal- and exceptional-flow values at the same time. |
| 119 | SimplifyCatchBlocks(); |
| 120 | |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 121 | ArenaBitVector visited(arena_, blocks_.size(), false); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 122 | |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 123 | // (2) Find the back edges in the graph doing a DFS traversal. |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 124 | FindBackEdges(&visited); |
| 125 | |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 126 | // (3) Remove instructions and phis from blocks not visited during |
Roland Levillain | fc600dc | 2014-12-02 17:16:31 +0000 | [diff] [blame] | 127 | // the initial DFS as users from other instructions, so that |
| 128 | // users can be safely removed before uses later. |
| 129 | RemoveInstructionsAsUsersFromDeadBlocks(visited); |
| 130 | |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 131 | // (4) Remove blocks not visited during the initial DFS. |
Roland Levillain | fc600dc | 2014-12-02 17:16:31 +0000 | [diff] [blame] | 132 | // Step (4) requires dead blocks to be removed from the |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 133 | // predecessors list of live blocks. |
| 134 | RemoveDeadBlocks(visited); |
| 135 | |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 136 | // (5) Simplify the CFG now, so that we don't need to recompute |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 137 | // dominators and the reverse post order. |
| 138 | SimplifyCFG(); |
| 139 | |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 140 | // (6) Compute the dominance information and the reverse post order. |
Nicolas Geoffray | 1f82ecc | 2015-06-24 12:20:24 +0100 | [diff] [blame] | 141 | ComputeDominanceInformation(); |
| 142 | } |
| 143 | |
| 144 | void HGraph::ClearDominanceInformation() { |
| 145 | for (HReversePostOrderIterator it(*this); !it.Done(); it.Advance()) { |
| 146 | it.Current()->ClearDominanceInformation(); |
| 147 | } |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 148 | reverse_post_order_.clear(); |
Nicolas Geoffray | 1f82ecc | 2015-06-24 12:20:24 +0100 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | void HBasicBlock::ClearDominanceInformation() { |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 152 | dominated_blocks_.clear(); |
Nicolas Geoffray | 1f82ecc | 2015-06-24 12:20:24 +0100 | [diff] [blame] | 153 | dominator_ = nullptr; |
| 154 | } |
| 155 | |
| 156 | void HGraph::ComputeDominanceInformation() { |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 157 | DCHECK(reverse_post_order_.empty()); |
| 158 | reverse_post_order_.reserve(blocks_.size()); |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 159 | reverse_post_order_.push_back(entry_block_); |
Vladimir Marko | d76d139 | 2015-09-23 16:07:14 +0100 | [diff] [blame] | 160 | |
| 161 | // Number of visits of a given node, indexed by block id. |
| 162 | ArenaVector<size_t> visits(blocks_.size(), 0u, arena_->Adapter()); |
| 163 | // Number of successors visited from a given node, indexed by block id. |
| 164 | ArenaVector<size_t> successors_visited(blocks_.size(), 0u, arena_->Adapter()); |
| 165 | // Nodes for which we need to visit successors. |
| 166 | ArenaVector<HBasicBlock*> worklist(arena_->Adapter()); |
| 167 | constexpr size_t kDefaultWorklistSize = 8; |
| 168 | worklist.reserve(kDefaultWorklistSize); |
| 169 | worklist.push_back(entry_block_); |
| 170 | |
| 171 | while (!worklist.empty()) { |
| 172 | HBasicBlock* current = worklist.back(); |
| 173 | uint32_t current_id = current->GetBlockId(); |
| 174 | if (successors_visited[current_id] == current->GetSuccessors().size()) { |
| 175 | worklist.pop_back(); |
| 176 | } else { |
Vladimir Marko | d76d139 | 2015-09-23 16:07:14 +0100 | [diff] [blame] | 177 | HBasicBlock* successor = current->GetSuccessors()[successors_visited[current_id]++]; |
| 178 | |
| 179 | if (successor->GetDominator() == nullptr) { |
| 180 | successor->SetDominator(current); |
| 181 | } else { |
| 182 | successor->SetDominator(FindCommonDominator(successor->GetDominator(), current)); |
| 183 | } |
| 184 | |
| 185 | // Once all the forward edges have been visited, we know the immediate |
| 186 | // dominator of the block. We can then start visiting its successors. |
Vladimir Marko | d76d139 | 2015-09-23 16:07:14 +0100 | [diff] [blame] | 187 | if (++visits[successor->GetBlockId()] == |
| 188 | successor->GetPredecessors().size() - successor->NumberOfBackEdges()) { |
| 189 | successor->GetDominator()->AddDominatedBlock(successor); |
| 190 | reverse_post_order_.push_back(successor); |
| 191 | worklist.push_back(successor); |
| 192 | } |
| 193 | } |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | |
| 197 | HBasicBlock* HGraph::FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const { |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 198 | ArenaBitVector visited(arena_, blocks_.size(), false); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 199 | // Walk the dominator tree of the first block and mark the visited blocks. |
| 200 | while (first != nullptr) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 201 | visited.SetBit(first->GetBlockId()); |
| 202 | first = first->GetDominator(); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 203 | } |
| 204 | // Walk the dominator tree of the second block until a marked block is found. |
| 205 | while (second != nullptr) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 206 | if (visited.IsBitSet(second->GetBlockId())) { |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 207 | return second; |
| 208 | } |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 209 | second = second->GetDominator(); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 210 | } |
| 211 | LOG(ERROR) << "Could not find common dominator"; |
| 212 | return nullptr; |
| 213 | } |
| 214 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 215 | void HGraph::TransformToSsa() { |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 216 | DCHECK(!reverse_post_order_.empty()); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 217 | SsaBuilder ssa_builder(this); |
| 218 | ssa_builder.BuildSsa(); |
| 219 | } |
| 220 | |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 221 | HBasicBlock* HGraph::SplitEdge(HBasicBlock* block, HBasicBlock* successor) { |
David Brazdil | 3e18738 | 2015-06-26 09:59:52 +0000 | [diff] [blame] | 222 | HBasicBlock* new_block = new (arena_) HBasicBlock(this, successor->GetDexPc()); |
| 223 | AddBlock(new_block); |
David Brazdil | 3e18738 | 2015-06-26 09:59:52 +0000 | [diff] [blame] | 224 | // Use `InsertBetween` to ensure the predecessor index and successor index of |
| 225 | // `block` and `successor` are preserved. |
| 226 | new_block->InsertBetween(block, successor); |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 227 | return new_block; |
| 228 | } |
| 229 | |
| 230 | void HGraph::SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor) { |
| 231 | // Insert a new node between `block` and `successor` to split the |
| 232 | // critical edge. |
| 233 | HBasicBlock* new_block = SplitEdge(block, successor); |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 234 | new_block->AddInstruction(new (arena_) HGoto(successor->GetDexPc())); |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 235 | if (successor->IsLoopHeader()) { |
| 236 | // If we split at a back edge boundary, make the new block the back edge. |
| 237 | HLoopInformation* info = successor->GetLoopInformation(); |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 238 | if (info->IsBackEdge(*block)) { |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 239 | info->RemoveBackEdge(block); |
| 240 | info->AddBackEdge(new_block); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 241 | } |
| 242 | } |
| 243 | } |
| 244 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 245 | void HGraph::SimplifyLoop(HBasicBlock* header) { |
| 246 | HLoopInformation* info = header->GetLoopInformation(); |
| 247 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 248 | // Make sure the loop has only one pre header. This simplifies SSA building by having |
| 249 | // to just look at the pre header to know which locals are initialized at entry of the |
| 250 | // loop. |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 251 | size_t number_of_incomings = header->GetPredecessors().size() - info->NumberOfBackEdges(); |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 252 | if (number_of_incomings != 1) { |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 253 | HBasicBlock* pre_header = new (arena_) HBasicBlock(this, header->GetDexPc()); |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 254 | AddBlock(pre_header); |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 255 | pre_header->AddInstruction(new (arena_) HGoto(header->GetDexPc())); |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 256 | |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 257 | for (size_t pred = 0; pred < header->GetPredecessors().size(); ++pred) { |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 258 | HBasicBlock* predecessor = header->GetPredecessors()[pred]; |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 259 | if (!info->IsBackEdge(*predecessor)) { |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 260 | predecessor->ReplaceSuccessor(header, pre_header); |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 261 | pred--; |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 262 | } |
| 263 | } |
| 264 | pre_header->AddSuccessor(header); |
| 265 | } |
Nicolas Geoffray | 604c6e4 | 2014-09-17 12:08:44 +0100 | [diff] [blame] | 266 | |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 267 | // Make sure the first predecessor of a loop header is the incoming block. |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 268 | if (info->IsBackEdge(*header->GetPredecessors()[0])) { |
| 269 | HBasicBlock* to_swap = header->GetPredecessors()[0]; |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 270 | for (size_t pred = 1, e = header->GetPredecessors().size(); pred < e; ++pred) { |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 271 | HBasicBlock* predecessor = header->GetPredecessors()[pred]; |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 272 | if (!info->IsBackEdge(*predecessor)) { |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 273 | header->predecessors_[pred] = to_swap; |
| 274 | header->predecessors_[0] = predecessor; |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 275 | break; |
| 276 | } |
| 277 | } |
Nicolas Geoffray | 604c6e4 | 2014-09-17 12:08:44 +0100 | [diff] [blame] | 278 | } |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 279 | |
| 280 | // Place the suspend check at the beginning of the header, so that live registers |
| 281 | // will be known when allocating registers. Note that code generation can still |
| 282 | // generate the suspend check at the back edge, but needs to be careful with |
| 283 | // loop phi spill slots (which are not written to at back edge). |
| 284 | HInstruction* first_instruction = header->GetFirstInstruction(); |
| 285 | if (!first_instruction->IsSuspendCheck()) { |
| 286 | HSuspendCheck* check = new (arena_) HSuspendCheck(header->GetDexPc()); |
| 287 | header->InsertInstructionBefore(check, first_instruction); |
| 288 | first_instruction = check; |
| 289 | } |
| 290 | info->SetSuspendCheck(first_instruction->AsSuspendCheck()); |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 291 | } |
| 292 | |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 293 | static bool CheckIfPredecessorAtIsExceptional(const HBasicBlock& block, size_t pred_idx) { |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 294 | HBasicBlock* predecessor = block.GetPredecessors()[pred_idx]; |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 295 | if (!predecessor->EndsWithTryBoundary()) { |
| 296 | // Only edges from HTryBoundary can be exceptional. |
| 297 | return false; |
| 298 | } |
| 299 | HTryBoundary* try_boundary = predecessor->GetLastInstruction()->AsTryBoundary(); |
| 300 | if (try_boundary->GetNormalFlowSuccessor() == &block) { |
| 301 | // This block is the normal-flow successor of `try_boundary`, but it could |
| 302 | // also be one of its exception handlers if catch blocks have not been |
| 303 | // simplified yet. Predecessors are unordered, so we will consider the first |
| 304 | // occurrence to be the normal edge and a possible second occurrence to be |
| 305 | // the exceptional edge. |
| 306 | return !block.IsFirstIndexOfPredecessor(predecessor, pred_idx); |
| 307 | } else { |
| 308 | // This is not the normal-flow successor of `try_boundary`, hence it must be |
| 309 | // one of its exception handlers. |
| 310 | DCHECK(try_boundary->HasExceptionHandler(block)); |
| 311 | return true; |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | void HGraph::SimplifyCatchBlocks() { |
Vladimir Marko | b7d8e8c | 2015-09-17 15:47:05 +0100 | [diff] [blame] | 316 | // NOTE: We're appending new blocks inside the loop, so we need to use index because iterators |
| 317 | // can be invalidated. We remember the initial size to avoid iterating over the new blocks. |
| 318 | for (size_t block_id = 0u, end = blocks_.size(); block_id != end; ++block_id) { |
| 319 | HBasicBlock* catch_block = blocks_[block_id]; |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 320 | if (!catch_block->IsCatchBlock()) { |
| 321 | continue; |
| 322 | } |
| 323 | |
| 324 | bool exceptional_predecessors_only = true; |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 325 | for (size_t j = 0; j < catch_block->GetPredecessors().size(); ++j) { |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 326 | if (!CheckIfPredecessorAtIsExceptional(*catch_block, j)) { |
| 327 | exceptional_predecessors_only = false; |
| 328 | break; |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | if (!exceptional_predecessors_only) { |
| 333 | // Catch block has normal-flow predecessors and needs to be simplified. |
| 334 | // Splitting the block before its first instruction moves all its |
| 335 | // instructions into `normal_block` and links the two blocks with a Goto. |
| 336 | // Afterwards, incoming normal-flow edges are re-linked to `normal_block`, |
| 337 | // leaving `catch_block` with the exceptional edges only. |
David Brazdil | 9bc4361 | 2015-11-05 21:25:24 +0000 | [diff] [blame] | 338 | // |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 339 | // Note that catch blocks with normal-flow predecessors cannot begin with |
David Brazdil | 9bc4361 | 2015-11-05 21:25:24 +0000 | [diff] [blame] | 340 | // a move-exception instruction, as guaranteed by the verifier. However, |
| 341 | // trivially dead predecessors are ignored by the verifier and such code |
| 342 | // has not been removed at this stage. We therefore ignore the assumption |
| 343 | // and rely on GraphChecker to enforce it after initial DCE is run (b/25492628). |
| 344 | HBasicBlock* normal_block = catch_block->SplitCatchBlockAfterMoveException(); |
| 345 | if (normal_block == nullptr) { |
| 346 | // Catch block is either empty or only contains a move-exception. It must |
| 347 | // therefore be dead and will be removed during initial DCE. Do nothing. |
| 348 | DCHECK(!catch_block->EndsWithControlFlowInstruction()); |
| 349 | } else { |
| 350 | // Catch block was split. Re-link normal-flow edges to the new block. |
| 351 | for (size_t j = 0; j < catch_block->GetPredecessors().size(); ++j) { |
| 352 | if (!CheckIfPredecessorAtIsExceptional(*catch_block, j)) { |
| 353 | catch_block->GetPredecessors()[j]->ReplaceSuccessor(catch_block, normal_block); |
| 354 | --j; |
| 355 | } |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 356 | } |
| 357 | } |
| 358 | } |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | void HGraph::ComputeTryBlockInformation() { |
| 363 | // Iterate in reverse post order to propagate try membership information from |
| 364 | // predecessors to their successors. |
| 365 | for (HReversePostOrderIterator it(*this); !it.Done(); it.Advance()) { |
| 366 | HBasicBlock* block = it.Current(); |
| 367 | if (block->IsEntryBlock() || block->IsCatchBlock()) { |
| 368 | // Catch blocks after simplification have only exceptional predecessors |
| 369 | // and hence are never in tries. |
| 370 | continue; |
| 371 | } |
| 372 | |
| 373 | // Infer try membership from the first predecessor. Having simplified loops, |
| 374 | // the first predecessor can never be a back edge and therefore it must have |
| 375 | // been visited already and had its try membership set. |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 376 | HBasicBlock* first_predecessor = block->GetPredecessors()[0]; |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 377 | DCHECK(!block->IsLoopHeader() || !block->GetLoopInformation()->IsBackEdge(*first_predecessor)); |
David Brazdil | ec16f79 | 2015-08-19 15:04:01 +0100 | [diff] [blame] | 378 | const HTryBoundary* try_entry = first_predecessor->ComputeTryEntryOfSuccessors(); |
David Brazdil | 8a7c0fe | 2015-11-02 20:24:55 +0000 | [diff] [blame^] | 379 | if (try_entry != nullptr && |
| 380 | (block->GetTryCatchInformation() == nullptr || |
| 381 | try_entry != &block->GetTryCatchInformation()->GetTryEntry())) { |
| 382 | // We are either setting try block membership for the first time or it |
| 383 | // has changed. |
David Brazdil | ec16f79 | 2015-08-19 15:04:01 +0100 | [diff] [blame] | 384 | block->SetTryCatchInformation(new (arena_) TryCatchInformation(*try_entry)); |
| 385 | } |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 386 | } |
| 387 | } |
| 388 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 389 | void HGraph::SimplifyCFG() { |
| 390 | // Simplify the CFG for future analysis, and code generation: |
| 391 | // (1): Split critical edges. |
| 392 | // (2): Simplify loops by having only one back edge, and one preheader. |
Vladimir Marko | b7d8e8c | 2015-09-17 15:47:05 +0100 | [diff] [blame] | 393 | // NOTE: We're appending new blocks inside the loop, so we need to use index because iterators |
| 394 | // can be invalidated. We remember the initial size to avoid iterating over the new blocks. |
| 395 | for (size_t block_id = 0u, end = blocks_.size(); block_id != end; ++block_id) { |
| 396 | HBasicBlock* block = blocks_[block_id]; |
Nicolas Geoffray | f776b92 | 2015-04-15 18:22:45 +0100 | [diff] [blame] | 397 | if (block == nullptr) continue; |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 398 | if (block->NumberOfNormalSuccessors() > 1) { |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 399 | for (size_t j = 0; j < block->GetSuccessors().size(); ++j) { |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 400 | HBasicBlock* successor = block->GetSuccessors()[j]; |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 401 | DCHECK(!successor->IsCatchBlock()); |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 402 | if (successor->GetPredecessors().size() > 1) { |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 403 | SplitCriticalEdge(block, successor); |
| 404 | --j; |
| 405 | } |
| 406 | } |
| 407 | } |
| 408 | if (block->IsLoopHeader()) { |
| 409 | SimplifyLoop(block); |
| 410 | } |
| 411 | } |
| 412 | } |
| 413 | |
Nicolas Geoffray | f537012 | 2014-12-02 11:51:19 +0000 | [diff] [blame] | 414 | bool HGraph::AnalyzeNaturalLoops() const { |
Nicolas Geoffray | f776b92 | 2015-04-15 18:22:45 +0100 | [diff] [blame] | 415 | // Order does not matter. |
| 416 | for (HReversePostOrderIterator it(*this); !it.Done(); it.Advance()) { |
| 417 | HBasicBlock* block = it.Current(); |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 418 | if (block->IsLoopHeader()) { |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 419 | if (block->IsCatchBlock()) { |
| 420 | // TODO: Dealing with exceptional back edges could be tricky because |
| 421 | // they only approximate the real control flow. Bail out for now. |
| 422 | return false; |
| 423 | } |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 424 | HLoopInformation* info = block->GetLoopInformation(); |
| 425 | if (!info->Populate()) { |
| 426 | // Abort if the loop is non natural. We currently bailout in such cases. |
| 427 | return false; |
| 428 | } |
| 429 | } |
| 430 | } |
| 431 | return true; |
| 432 | } |
| 433 | |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 434 | void HGraph::InsertConstant(HConstant* constant) { |
| 435 | // New constants are inserted before the final control-flow instruction |
| 436 | // of the graph, or at its end if called from the graph builder. |
| 437 | if (entry_block_->EndsWithControlFlowInstruction()) { |
| 438 | entry_block_->InsertInstructionBefore(constant, entry_block_->GetLastInstruction()); |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 439 | } else { |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 440 | entry_block_->AddInstruction(constant); |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 441 | } |
| 442 | } |
| 443 | |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 444 | HNullConstant* HGraph::GetNullConstant(uint32_t dex_pc) { |
Nicolas Geoffray | 18e6873 | 2015-06-17 23:09:05 +0100 | [diff] [blame] | 445 | // For simplicity, don't bother reviving the cached null constant if it is |
| 446 | // not null and not in a block. Otherwise, we need to clear the instruction |
| 447 | // id and/or any invariants the graph is assuming when adding new instructions. |
| 448 | if ((cached_null_constant_ == nullptr) || (cached_null_constant_->GetBlock() == nullptr)) { |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 449 | cached_null_constant_ = new (arena_) HNullConstant(dex_pc); |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 450 | InsertConstant(cached_null_constant_); |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 451 | } |
| 452 | return cached_null_constant_; |
| 453 | } |
| 454 | |
Nicolas Geoffray | 76b1e17 | 2015-05-27 17:18:33 +0100 | [diff] [blame] | 455 | HCurrentMethod* HGraph::GetCurrentMethod() { |
Nicolas Geoffray | f78848f | 2015-06-17 11:57:56 +0100 | [diff] [blame] | 456 | // For simplicity, don't bother reviving the cached current method if it is |
| 457 | // not null and not in a block. Otherwise, we need to clear the instruction |
| 458 | // id and/or any invariants the graph is assuming when adding new instructions. |
| 459 | if ((cached_current_method_ == nullptr) || (cached_current_method_->GetBlock() == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 460 | cached_current_method_ = new (arena_) HCurrentMethod( |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 461 | Is64BitInstructionSet(instruction_set_) ? Primitive::kPrimLong : Primitive::kPrimInt, |
| 462 | entry_block_->GetDexPc()); |
Nicolas Geoffray | 76b1e17 | 2015-05-27 17:18:33 +0100 | [diff] [blame] | 463 | if (entry_block_->GetFirstInstruction() == nullptr) { |
| 464 | entry_block_->AddInstruction(cached_current_method_); |
| 465 | } else { |
| 466 | entry_block_->InsertInstructionBefore( |
| 467 | cached_current_method_, entry_block_->GetFirstInstruction()); |
| 468 | } |
| 469 | } |
| 470 | return cached_current_method_; |
| 471 | } |
| 472 | |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 473 | HConstant* HGraph::GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc) { |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 474 | switch (type) { |
| 475 | case Primitive::Type::kPrimBoolean: |
| 476 | DCHECK(IsUint<1>(value)); |
| 477 | FALLTHROUGH_INTENDED; |
| 478 | case Primitive::Type::kPrimByte: |
| 479 | case Primitive::Type::kPrimChar: |
| 480 | case Primitive::Type::kPrimShort: |
| 481 | case Primitive::Type::kPrimInt: |
| 482 | DCHECK(IsInt(Primitive::ComponentSize(type) * kBitsPerByte, value)); |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 483 | return GetIntConstant(static_cast<int32_t>(value), dex_pc); |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 484 | |
| 485 | case Primitive::Type::kPrimLong: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 486 | return GetLongConstant(value, dex_pc); |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 487 | |
| 488 | default: |
| 489 | LOG(FATAL) << "Unsupported constant type"; |
| 490 | UNREACHABLE(); |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 491 | } |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 492 | } |
| 493 | |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 494 | void HGraph::CacheFloatConstant(HFloatConstant* constant) { |
| 495 | int32_t value = bit_cast<int32_t, float>(constant->GetValue()); |
| 496 | DCHECK(cached_float_constants_.find(value) == cached_float_constants_.end()); |
| 497 | cached_float_constants_.Overwrite(value, constant); |
| 498 | } |
| 499 | |
| 500 | void HGraph::CacheDoubleConstant(HDoubleConstant* constant) { |
| 501 | int64_t value = bit_cast<int64_t, double>(constant->GetValue()); |
| 502 | DCHECK(cached_double_constants_.find(value) == cached_double_constants_.end()); |
| 503 | cached_double_constants_.Overwrite(value, constant); |
| 504 | } |
| 505 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 506 | void HLoopInformation::Add(HBasicBlock* block) { |
| 507 | blocks_.SetBit(block->GetBlockId()); |
| 508 | } |
| 509 | |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 510 | void HLoopInformation::Remove(HBasicBlock* block) { |
| 511 | blocks_.ClearBit(block->GetBlockId()); |
| 512 | } |
| 513 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 514 | void HLoopInformation::PopulateRecursive(HBasicBlock* block) { |
| 515 | if (blocks_.IsBitSet(block->GetBlockId())) { |
| 516 | return; |
| 517 | } |
| 518 | |
| 519 | blocks_.SetBit(block->GetBlockId()); |
| 520 | block->SetInLoop(this); |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 521 | for (HBasicBlock* predecessor : block->GetPredecessors()) { |
| 522 | PopulateRecursive(predecessor); |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 523 | } |
| 524 | } |
| 525 | |
| 526 | bool HLoopInformation::Populate() { |
David Brazdil | a4b8c21 | 2015-05-07 09:59:30 +0100 | [diff] [blame] | 527 | DCHECK_EQ(blocks_.NumSetBits(), 0u) << "Loop information has already been populated"; |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 528 | for (HBasicBlock* back_edge : GetBackEdges()) { |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 529 | DCHECK(back_edge->GetDominator() != nullptr); |
| 530 | if (!header_->Dominates(back_edge)) { |
| 531 | // This loop is not natural. Do not bother going further. |
| 532 | return false; |
| 533 | } |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 534 | |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 535 | // Populate this loop: starting with the back edge, recursively add predecessors |
| 536 | // that are not already part of that loop. Set the header as part of the loop |
| 537 | // to end the recursion. |
| 538 | // This is a recursive implementation of the algorithm described in |
| 539 | // "Advanced Compiler Design & Implementation" (Muchnick) p192. |
| 540 | blocks_.SetBit(header_->GetBlockId()); |
| 541 | PopulateRecursive(back_edge); |
| 542 | } |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 543 | return true; |
| 544 | } |
| 545 | |
David Brazdil | a4b8c21 | 2015-05-07 09:59:30 +0100 | [diff] [blame] | 546 | void HLoopInformation::Update() { |
| 547 | HGraph* graph = header_->GetGraph(); |
| 548 | for (uint32_t id : blocks_.Indexes()) { |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 549 | HBasicBlock* block = graph->GetBlocks()[id]; |
David Brazdil | a4b8c21 | 2015-05-07 09:59:30 +0100 | [diff] [blame] | 550 | // Reset loop information of non-header blocks inside the loop, except |
| 551 | // members of inner nested loops because those should already have been |
| 552 | // updated by their own LoopInformation. |
| 553 | if (block->GetLoopInformation() == this && block != header_) { |
| 554 | block->SetLoopInformation(nullptr); |
| 555 | } |
| 556 | } |
| 557 | blocks_.ClearAllBits(); |
| 558 | |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 559 | if (back_edges_.empty()) { |
David Brazdil | a4b8c21 | 2015-05-07 09:59:30 +0100 | [diff] [blame] | 560 | // The loop has been dismantled, delete its suspend check and remove info |
| 561 | // from the header. |
| 562 | DCHECK(HasSuspendCheck()); |
| 563 | header_->RemoveInstruction(suspend_check_); |
| 564 | header_->SetLoopInformation(nullptr); |
| 565 | header_ = nullptr; |
| 566 | suspend_check_ = nullptr; |
| 567 | } else { |
| 568 | if (kIsDebugBuild) { |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 569 | for (HBasicBlock* back_edge : back_edges_) { |
| 570 | DCHECK(header_->Dominates(back_edge)); |
David Brazdil | a4b8c21 | 2015-05-07 09:59:30 +0100 | [diff] [blame] | 571 | } |
| 572 | } |
| 573 | // This loop still has reachable back edges. Repopulate the list of blocks. |
| 574 | bool populate_successful = Populate(); |
| 575 | DCHECK(populate_successful); |
| 576 | } |
| 577 | } |
| 578 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 579 | HBasicBlock* HLoopInformation::GetPreHeader() const { |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 580 | return header_->GetDominator(); |
| 581 | } |
| 582 | |
| 583 | bool HLoopInformation::Contains(const HBasicBlock& block) const { |
| 584 | return blocks_.IsBitSet(block.GetBlockId()); |
| 585 | } |
| 586 | |
| 587 | bool HLoopInformation::IsIn(const HLoopInformation& other) const { |
| 588 | return other.blocks_.IsBitSet(header_->GetBlockId()); |
| 589 | } |
| 590 | |
Aart Bik | 73f1f3b | 2015-10-28 15:28:08 -0700 | [diff] [blame] | 591 | bool HLoopInformation::IsLoopInvariant(HInstruction* instruction, bool must_dominate) const { |
| 592 | HLoopInformation* other_loop = instruction->GetBlock()->GetLoopInformation(); |
| 593 | if (other_loop != this && (other_loop == nullptr || !other_loop->IsIn(*this))) { |
| 594 | if (must_dominate) { |
| 595 | return instruction->GetBlock()->Dominates(GetHeader()); |
| 596 | } |
| 597 | return true; |
| 598 | } |
| 599 | return false; |
| 600 | } |
| 601 | |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 602 | size_t HLoopInformation::GetLifetimeEnd() const { |
| 603 | size_t last_position = 0; |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 604 | for (HBasicBlock* back_edge : GetBackEdges()) { |
| 605 | last_position = std::max(back_edge->GetLifetimeEnd(), last_position); |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 606 | } |
| 607 | return last_position; |
| 608 | } |
| 609 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 610 | bool HBasicBlock::Dominates(HBasicBlock* other) const { |
| 611 | // Walk up the dominator tree from `other`, to find out if `this` |
| 612 | // is an ancestor. |
| 613 | HBasicBlock* current = other; |
| 614 | while (current != nullptr) { |
| 615 | if (current == this) { |
| 616 | return true; |
| 617 | } |
| 618 | current = current->GetDominator(); |
| 619 | } |
| 620 | return false; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 621 | } |
| 622 | |
Nicolas Geoffray | 191c4b1 | 2014-10-07 14:14:27 +0100 | [diff] [blame] | 623 | static void UpdateInputsUsers(HInstruction* instruction) { |
| 624 | for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) { |
| 625 | instruction->InputAt(i)->AddUseAt(instruction, i); |
| 626 | } |
| 627 | // Environment should be created later. |
| 628 | DCHECK(!instruction->HasEnvironment()); |
| 629 | } |
| 630 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 631 | void HBasicBlock::ReplaceAndRemoveInstructionWith(HInstruction* initial, |
| 632 | HInstruction* replacement) { |
| 633 | DCHECK(initial->GetBlock() == this); |
Mark Mendell | 805b3b5 | 2015-09-18 14:10:29 -0400 | [diff] [blame] | 634 | if (initial->IsControlFlow()) { |
| 635 | // We can only replace a control flow instruction with another control flow instruction. |
| 636 | DCHECK(replacement->IsControlFlow()); |
| 637 | DCHECK_EQ(replacement->GetId(), -1); |
| 638 | DCHECK_EQ(replacement->GetType(), Primitive::kPrimVoid); |
| 639 | DCHECK_EQ(initial->GetBlock(), this); |
| 640 | DCHECK_EQ(initial->GetType(), Primitive::kPrimVoid); |
| 641 | DCHECK(initial->GetUses().IsEmpty()); |
| 642 | DCHECK(initial->GetEnvUses().IsEmpty()); |
| 643 | replacement->SetBlock(this); |
| 644 | replacement->SetId(GetGraph()->GetNextInstructionId()); |
| 645 | instructions_.InsertInstructionBefore(replacement, initial); |
| 646 | UpdateInputsUsers(replacement); |
| 647 | } else { |
| 648 | InsertInstructionBefore(replacement, initial); |
| 649 | initial->ReplaceWith(replacement); |
| 650 | } |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 651 | RemoveInstruction(initial); |
| 652 | } |
| 653 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 654 | static void Add(HInstructionList* instruction_list, |
| 655 | HBasicBlock* block, |
| 656 | HInstruction* instruction) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 657 | DCHECK(instruction->GetBlock() == nullptr); |
Nicolas Geoffray | 43c8642 | 2014-03-18 11:58:24 +0000 | [diff] [blame] | 658 | DCHECK_EQ(instruction->GetId(), -1); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 659 | instruction->SetBlock(block); |
| 660 | instruction->SetId(block->GetGraph()->GetNextInstructionId()); |
Nicolas Geoffray | 191c4b1 | 2014-10-07 14:14:27 +0100 | [diff] [blame] | 661 | UpdateInputsUsers(instruction); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 662 | instruction_list->AddInstruction(instruction); |
| 663 | } |
| 664 | |
| 665 | void HBasicBlock::AddInstruction(HInstruction* instruction) { |
| 666 | Add(&instructions_, this, instruction); |
| 667 | } |
| 668 | |
| 669 | void HBasicBlock::AddPhi(HPhi* phi) { |
| 670 | Add(&phis_, this, phi); |
| 671 | } |
| 672 | |
David Brazdil | c3d743f | 2015-04-22 13:40:50 +0100 | [diff] [blame] | 673 | void HBasicBlock::InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor) { |
| 674 | DCHECK(!cursor->IsPhi()); |
| 675 | DCHECK(!instruction->IsPhi()); |
| 676 | DCHECK_EQ(instruction->GetId(), -1); |
| 677 | DCHECK_NE(cursor->GetId(), -1); |
| 678 | DCHECK_EQ(cursor->GetBlock(), this); |
| 679 | DCHECK(!instruction->IsControlFlow()); |
| 680 | instruction->SetBlock(this); |
| 681 | instruction->SetId(GetGraph()->GetNextInstructionId()); |
| 682 | UpdateInputsUsers(instruction); |
| 683 | instructions_.InsertInstructionBefore(instruction, cursor); |
| 684 | } |
| 685 | |
Guillaume "Vermeille" Sanchez | 2967ec6 | 2015-04-24 16:36:52 +0100 | [diff] [blame] | 686 | void HBasicBlock::InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor) { |
| 687 | DCHECK(!cursor->IsPhi()); |
| 688 | DCHECK(!instruction->IsPhi()); |
| 689 | DCHECK_EQ(instruction->GetId(), -1); |
| 690 | DCHECK_NE(cursor->GetId(), -1); |
| 691 | DCHECK_EQ(cursor->GetBlock(), this); |
| 692 | DCHECK(!instruction->IsControlFlow()); |
| 693 | DCHECK(!cursor->IsControlFlow()); |
| 694 | instruction->SetBlock(this); |
| 695 | instruction->SetId(GetGraph()->GetNextInstructionId()); |
| 696 | UpdateInputsUsers(instruction); |
| 697 | instructions_.InsertInstructionAfter(instruction, cursor); |
| 698 | } |
| 699 | |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 700 | void HBasicBlock::InsertPhiAfter(HPhi* phi, HPhi* cursor) { |
| 701 | DCHECK_EQ(phi->GetId(), -1); |
| 702 | DCHECK_NE(cursor->GetId(), -1); |
| 703 | DCHECK_EQ(cursor->GetBlock(), this); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 704 | phi->SetBlock(this); |
| 705 | phi->SetId(GetGraph()->GetNextInstructionId()); |
| 706 | UpdateInputsUsers(phi); |
David Brazdil | c3d743f | 2015-04-22 13:40:50 +0100 | [diff] [blame] | 707 | phis_.InsertInstructionAfter(phi, cursor); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 708 | } |
| 709 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 710 | static void Remove(HInstructionList* instruction_list, |
| 711 | HBasicBlock* block, |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 712 | HInstruction* instruction, |
| 713 | bool ensure_safety) { |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 714 | DCHECK_EQ(block, instruction->GetBlock()); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 715 | instruction->SetBlock(nullptr); |
| 716 | instruction_list->RemoveInstruction(instruction); |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 717 | if (ensure_safety) { |
| 718 | DCHECK(instruction->GetUses().IsEmpty()); |
| 719 | DCHECK(instruction->GetEnvUses().IsEmpty()); |
| 720 | RemoveAsUser(instruction); |
| 721 | } |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 722 | } |
| 723 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 724 | void HBasicBlock::RemoveInstruction(HInstruction* instruction, bool ensure_safety) { |
David Brazdil | c7508e9 | 2015-04-27 13:28:57 +0100 | [diff] [blame] | 725 | DCHECK(!instruction->IsPhi()); |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 726 | Remove(&instructions_, this, instruction, ensure_safety); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 727 | } |
| 728 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 729 | void HBasicBlock::RemovePhi(HPhi* phi, bool ensure_safety) { |
| 730 | Remove(&phis_, this, phi, ensure_safety); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 731 | } |
| 732 | |
David Brazdil | c7508e9 | 2015-04-27 13:28:57 +0100 | [diff] [blame] | 733 | void HBasicBlock::RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety) { |
| 734 | if (instruction->IsPhi()) { |
| 735 | RemovePhi(instruction->AsPhi(), ensure_safety); |
| 736 | } else { |
| 737 | RemoveInstruction(instruction, ensure_safety); |
| 738 | } |
| 739 | } |
| 740 | |
Vladimir Marko | 71bf809 | 2015-09-15 15:33:14 +0100 | [diff] [blame] | 741 | void HEnvironment::CopyFrom(const ArenaVector<HInstruction*>& locals) { |
| 742 | for (size_t i = 0; i < locals.size(); i++) { |
| 743 | HInstruction* instruction = locals[i]; |
Nicolas Geoffray | 8c0c91a | 2015-05-07 11:46:05 +0100 | [diff] [blame] | 744 | SetRawEnvAt(i, instruction); |
| 745 | if (instruction != nullptr) { |
| 746 | instruction->AddEnvUseAt(this, i); |
| 747 | } |
| 748 | } |
| 749 | } |
| 750 | |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 751 | void HEnvironment::CopyFrom(HEnvironment* env) { |
| 752 | for (size_t i = 0; i < env->Size(); i++) { |
| 753 | HInstruction* instruction = env->GetInstructionAt(i); |
| 754 | SetRawEnvAt(i, instruction); |
| 755 | if (instruction != nullptr) { |
| 756 | instruction->AddEnvUseAt(this, i); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 757 | } |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 758 | } |
| 759 | } |
| 760 | |
Mingyao Yang | 206d6fd | 2015-04-13 16:46:28 -0700 | [diff] [blame] | 761 | void HEnvironment::CopyFromWithLoopPhiAdjustment(HEnvironment* env, |
| 762 | HBasicBlock* loop_header) { |
| 763 | DCHECK(loop_header->IsLoopHeader()); |
| 764 | for (size_t i = 0; i < env->Size(); i++) { |
| 765 | HInstruction* instruction = env->GetInstructionAt(i); |
| 766 | SetRawEnvAt(i, instruction); |
| 767 | if (instruction == nullptr) { |
| 768 | continue; |
| 769 | } |
| 770 | if (instruction->IsLoopHeaderPhi() && (instruction->GetBlock() == loop_header)) { |
| 771 | // At the end of the loop pre-header, the corresponding value for instruction |
| 772 | // is the first input of the phi. |
| 773 | HInstruction* initial = instruction->AsPhi()->InputAt(0); |
| 774 | DCHECK(initial->GetBlock()->Dominates(loop_header)); |
| 775 | SetRawEnvAt(i, initial); |
| 776 | initial->AddEnvUseAt(this, i); |
| 777 | } else { |
| 778 | instruction->AddEnvUseAt(this, i); |
| 779 | } |
| 780 | } |
| 781 | } |
| 782 | |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 783 | void HEnvironment::RemoveAsUserOfInput(size_t index) const { |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 784 | const HUserRecord<HEnvironment*>& user_record = vregs_[index]; |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 785 | user_record.GetInstruction()->RemoveEnvironmentUser(user_record.GetUseNode()); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 786 | } |
| 787 | |
Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 788 | HInstruction* HInstruction::GetNextDisregardingMoves() const { |
| 789 | HInstruction* next = GetNext(); |
| 790 | while (next != nullptr && next->IsParallelMove()) { |
| 791 | next = next->GetNext(); |
| 792 | } |
| 793 | return next; |
| 794 | } |
| 795 | |
| 796 | HInstruction* HInstruction::GetPreviousDisregardingMoves() const { |
| 797 | HInstruction* previous = GetPrevious(); |
| 798 | while (previous != nullptr && previous->IsParallelMove()) { |
| 799 | previous = previous->GetPrevious(); |
| 800 | } |
| 801 | return previous; |
| 802 | } |
| 803 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 804 | void HInstructionList::AddInstruction(HInstruction* instruction) { |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 805 | if (first_instruction_ == nullptr) { |
| 806 | DCHECK(last_instruction_ == nullptr); |
| 807 | first_instruction_ = last_instruction_ = instruction; |
| 808 | } else { |
| 809 | last_instruction_->next_ = instruction; |
| 810 | instruction->previous_ = last_instruction_; |
| 811 | last_instruction_ = instruction; |
| 812 | } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 813 | } |
| 814 | |
David Brazdil | c3d743f | 2015-04-22 13:40:50 +0100 | [diff] [blame] | 815 | void HInstructionList::InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor) { |
| 816 | DCHECK(Contains(cursor)); |
| 817 | if (cursor == first_instruction_) { |
| 818 | cursor->previous_ = instruction; |
| 819 | instruction->next_ = cursor; |
| 820 | first_instruction_ = instruction; |
| 821 | } else { |
| 822 | instruction->previous_ = cursor->previous_; |
| 823 | instruction->next_ = cursor; |
| 824 | cursor->previous_ = instruction; |
| 825 | instruction->previous_->next_ = instruction; |
| 826 | } |
| 827 | } |
| 828 | |
| 829 | void HInstructionList::InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor) { |
| 830 | DCHECK(Contains(cursor)); |
| 831 | if (cursor == last_instruction_) { |
| 832 | cursor->next_ = instruction; |
| 833 | instruction->previous_ = cursor; |
| 834 | last_instruction_ = instruction; |
| 835 | } else { |
| 836 | instruction->next_ = cursor->next_; |
| 837 | instruction->previous_ = cursor; |
| 838 | cursor->next_ = instruction; |
| 839 | instruction->next_->previous_ = instruction; |
| 840 | } |
| 841 | } |
| 842 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 843 | void HInstructionList::RemoveInstruction(HInstruction* instruction) { |
| 844 | if (instruction->previous_ != nullptr) { |
| 845 | instruction->previous_->next_ = instruction->next_; |
| 846 | } |
| 847 | if (instruction->next_ != nullptr) { |
| 848 | instruction->next_->previous_ = instruction->previous_; |
| 849 | } |
| 850 | if (instruction == first_instruction_) { |
| 851 | first_instruction_ = instruction->next_; |
| 852 | } |
| 853 | if (instruction == last_instruction_) { |
| 854 | last_instruction_ = instruction->previous_; |
| 855 | } |
| 856 | } |
| 857 | |
Roland Levillain | 6b46923 | 2014-09-25 10:10:38 +0100 | [diff] [blame] | 858 | bool HInstructionList::Contains(HInstruction* instruction) const { |
| 859 | for (HInstructionIterator it(*this); !it.Done(); it.Advance()) { |
| 860 | if (it.Current() == instruction) { |
| 861 | return true; |
| 862 | } |
| 863 | } |
| 864 | return false; |
| 865 | } |
| 866 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 867 | bool HInstructionList::FoundBefore(const HInstruction* instruction1, |
| 868 | const HInstruction* instruction2) const { |
| 869 | DCHECK_EQ(instruction1->GetBlock(), instruction2->GetBlock()); |
| 870 | for (HInstructionIterator it(*this); !it.Done(); it.Advance()) { |
| 871 | if (it.Current() == instruction1) { |
| 872 | return true; |
| 873 | } |
| 874 | if (it.Current() == instruction2) { |
| 875 | return false; |
| 876 | } |
| 877 | } |
| 878 | LOG(FATAL) << "Did not find an order between two instructions of the same block."; |
| 879 | return true; |
| 880 | } |
| 881 | |
Roland Levillain | 6c82d40 | 2014-10-13 16:10:27 +0100 | [diff] [blame] | 882 | bool HInstruction::StrictlyDominates(HInstruction* other_instruction) const { |
| 883 | if (other_instruction == this) { |
| 884 | // An instruction does not strictly dominate itself. |
| 885 | return false; |
| 886 | } |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 887 | HBasicBlock* block = GetBlock(); |
| 888 | HBasicBlock* other_block = other_instruction->GetBlock(); |
| 889 | if (block != other_block) { |
| 890 | return GetBlock()->Dominates(other_instruction->GetBlock()); |
| 891 | } else { |
| 892 | // If both instructions are in the same block, ensure this |
| 893 | // instruction comes before `other_instruction`. |
| 894 | if (IsPhi()) { |
| 895 | if (!other_instruction->IsPhi()) { |
| 896 | // Phis appear before non phi-instructions so this instruction |
| 897 | // dominates `other_instruction`. |
| 898 | return true; |
| 899 | } else { |
| 900 | // There is no order among phis. |
| 901 | LOG(FATAL) << "There is no dominance between phis of a same block."; |
| 902 | return false; |
| 903 | } |
| 904 | } else { |
| 905 | // `this` is not a phi. |
| 906 | if (other_instruction->IsPhi()) { |
| 907 | // Phis appear before non phi-instructions so this instruction |
| 908 | // does not dominate `other_instruction`. |
| 909 | return false; |
| 910 | } else { |
| 911 | // Check whether this instruction comes before |
| 912 | // `other_instruction` in the instruction list. |
| 913 | return block->GetInstructions().FoundBefore(this, other_instruction); |
| 914 | } |
| 915 | } |
| 916 | } |
| 917 | } |
| 918 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 919 | void HInstruction::ReplaceWith(HInstruction* other) { |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 920 | DCHECK(other != nullptr); |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 921 | for (HUseIterator<HInstruction*> it(GetUses()); !it.Done(); it.Advance()) { |
| 922 | HUseListNode<HInstruction*>* current = it.Current(); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 923 | HInstruction* user = current->GetUser(); |
| 924 | size_t input_index = current->GetIndex(); |
| 925 | user->SetRawInputAt(input_index, other); |
| 926 | other->AddUseAt(user, input_index); |
| 927 | } |
| 928 | |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 929 | for (HUseIterator<HEnvironment*> it(GetEnvUses()); !it.Done(); it.Advance()) { |
| 930 | HUseListNode<HEnvironment*>* current = it.Current(); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 931 | HEnvironment* user = current->GetUser(); |
| 932 | size_t input_index = current->GetIndex(); |
| 933 | user->SetRawEnvAt(input_index, other); |
| 934 | other->AddEnvUseAt(user, input_index); |
| 935 | } |
| 936 | |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [diff] [blame] | 937 | uses_.Clear(); |
| 938 | env_uses_.Clear(); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 939 | } |
| 940 | |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 941 | void HInstruction::ReplaceInput(HInstruction* replacement, size_t index) { |
David Brazdil | 1abb419 | 2015-02-17 18:33:36 +0000 | [diff] [blame] | 942 | RemoveAsUserOfInput(index); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 943 | SetRawInputAt(index, replacement); |
| 944 | replacement->AddUseAt(this, index); |
| 945 | } |
| 946 | |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 947 | size_t HInstruction::EnvironmentSize() const { |
| 948 | return HasEnvironment() ? environment_->Size() : 0; |
| 949 | } |
| 950 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 951 | void HPhi::AddInput(HInstruction* input) { |
| 952 | DCHECK(input->GetBlock() != nullptr); |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 953 | inputs_.push_back(HUserRecord<HInstruction*>(input)); |
| 954 | input->AddUseAt(this, inputs_.size() - 1); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 955 | } |
| 956 | |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 957 | void HPhi::RemoveInputAt(size_t index) { |
| 958 | RemoveAsUserOfInput(index); |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 959 | inputs_.erase(inputs_.begin() + index); |
Nicolas Geoffray | 5d7b7f8 | 2015-04-28 00:52:43 +0100 | [diff] [blame] | 960 | for (size_t i = index, e = InputCount(); i < e; ++i) { |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 961 | DCHECK_EQ(InputRecordAt(i).GetUseNode()->GetIndex(), i + 1u); |
Nicolas Geoffray | 5d7b7f8 | 2015-04-28 00:52:43 +0100 | [diff] [blame] | 962 | InputRecordAt(i).GetUseNode()->SetIndex(i); |
| 963 | } |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 964 | } |
| 965 | |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 966 | #define DEFINE_ACCEPT(name, super) \ |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 967 | void H##name::Accept(HGraphVisitor* visitor) { \ |
| 968 | visitor->Visit##name(this); \ |
| 969 | } |
| 970 | |
| 971 | FOR_EACH_INSTRUCTION(DEFINE_ACCEPT) |
| 972 | |
| 973 | #undef DEFINE_ACCEPT |
| 974 | |
| 975 | void HGraphVisitor::VisitInsertionOrder() { |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 976 | const ArenaVector<HBasicBlock*>& blocks = graph_->GetBlocks(); |
| 977 | for (HBasicBlock* block : blocks) { |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 978 | if (block != nullptr) { |
| 979 | VisitBasicBlock(block); |
| 980 | } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 981 | } |
| 982 | } |
| 983 | |
Roland Levillain | 633021e | 2014-10-01 14:12:25 +0100 | [diff] [blame] | 984 | void HGraphVisitor::VisitReversePostOrder() { |
| 985 | for (HReversePostOrderIterator it(*graph_); !it.Done(); it.Advance()) { |
| 986 | VisitBasicBlock(it.Current()); |
| 987 | } |
| 988 | } |
| 989 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 990 | void HGraphVisitor::VisitBasicBlock(HBasicBlock* block) { |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 991 | for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) { |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 992 | it.Current()->Accept(this); |
| 993 | } |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 994 | for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) { |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 995 | it.Current()->Accept(this); |
| 996 | } |
| 997 | } |
| 998 | |
Mark Mendell | e82549b | 2015-05-06 10:55:34 -0400 | [diff] [blame] | 999 | HConstant* HTypeConversion::TryStaticEvaluation() const { |
| 1000 | HGraph* graph = GetBlock()->GetGraph(); |
| 1001 | if (GetInput()->IsIntConstant()) { |
| 1002 | int32_t value = GetInput()->AsIntConstant()->GetValue(); |
| 1003 | switch (GetResultType()) { |
| 1004 | case Primitive::kPrimLong: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 1005 | return graph->GetLongConstant(static_cast<int64_t>(value), GetDexPc()); |
Mark Mendell | e82549b | 2015-05-06 10:55:34 -0400 | [diff] [blame] | 1006 | case Primitive::kPrimFloat: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 1007 | return graph->GetFloatConstant(static_cast<float>(value), GetDexPc()); |
Mark Mendell | e82549b | 2015-05-06 10:55:34 -0400 | [diff] [blame] | 1008 | case Primitive::kPrimDouble: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 1009 | return graph->GetDoubleConstant(static_cast<double>(value), GetDexPc()); |
Mark Mendell | e82549b | 2015-05-06 10:55:34 -0400 | [diff] [blame] | 1010 | default: |
| 1011 | return nullptr; |
| 1012 | } |
| 1013 | } else if (GetInput()->IsLongConstant()) { |
| 1014 | int64_t value = GetInput()->AsLongConstant()->GetValue(); |
| 1015 | switch (GetResultType()) { |
| 1016 | case Primitive::kPrimInt: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 1017 | return graph->GetIntConstant(static_cast<int32_t>(value), GetDexPc()); |
Mark Mendell | e82549b | 2015-05-06 10:55:34 -0400 | [diff] [blame] | 1018 | case Primitive::kPrimFloat: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 1019 | return graph->GetFloatConstant(static_cast<float>(value), GetDexPc()); |
Mark Mendell | e82549b | 2015-05-06 10:55:34 -0400 | [diff] [blame] | 1020 | case Primitive::kPrimDouble: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 1021 | return graph->GetDoubleConstant(static_cast<double>(value), GetDexPc()); |
Mark Mendell | e82549b | 2015-05-06 10:55:34 -0400 | [diff] [blame] | 1022 | default: |
| 1023 | return nullptr; |
| 1024 | } |
| 1025 | } else if (GetInput()->IsFloatConstant()) { |
| 1026 | float value = GetInput()->AsFloatConstant()->GetValue(); |
| 1027 | switch (GetResultType()) { |
| 1028 | case Primitive::kPrimInt: |
| 1029 | if (std::isnan(value)) |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 1030 | return graph->GetIntConstant(0, GetDexPc()); |
Mark Mendell | e82549b | 2015-05-06 10:55:34 -0400 | [diff] [blame] | 1031 | if (value >= kPrimIntMax) |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 1032 | return graph->GetIntConstant(kPrimIntMax, GetDexPc()); |
Mark Mendell | e82549b | 2015-05-06 10:55:34 -0400 | [diff] [blame] | 1033 | if (value <= kPrimIntMin) |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 1034 | return graph->GetIntConstant(kPrimIntMin, GetDexPc()); |
| 1035 | return graph->GetIntConstant(static_cast<int32_t>(value), GetDexPc()); |
Mark Mendell | e82549b | 2015-05-06 10:55:34 -0400 | [diff] [blame] | 1036 | case Primitive::kPrimLong: |
| 1037 | if (std::isnan(value)) |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 1038 | return graph->GetLongConstant(0, GetDexPc()); |
Mark Mendell | e82549b | 2015-05-06 10:55:34 -0400 | [diff] [blame] | 1039 | if (value >= kPrimLongMax) |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 1040 | return graph->GetLongConstant(kPrimLongMax, GetDexPc()); |
Mark Mendell | e82549b | 2015-05-06 10:55:34 -0400 | [diff] [blame] | 1041 | if (value <= kPrimLongMin) |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 1042 | return graph->GetLongConstant(kPrimLongMin, GetDexPc()); |
| 1043 | return graph->GetLongConstant(static_cast<int64_t>(value), GetDexPc()); |
Mark Mendell | e82549b | 2015-05-06 10:55:34 -0400 | [diff] [blame] | 1044 | case Primitive::kPrimDouble: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 1045 | return graph->GetDoubleConstant(static_cast<double>(value), GetDexPc()); |
Mark Mendell | e82549b | 2015-05-06 10:55:34 -0400 | [diff] [blame] | 1046 | default: |
| 1047 | return nullptr; |
| 1048 | } |
| 1049 | } else if (GetInput()->IsDoubleConstant()) { |
| 1050 | double value = GetInput()->AsDoubleConstant()->GetValue(); |
| 1051 | switch (GetResultType()) { |
| 1052 | case Primitive::kPrimInt: |
| 1053 | if (std::isnan(value)) |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 1054 | return graph->GetIntConstant(0, GetDexPc()); |
Mark Mendell | e82549b | 2015-05-06 10:55:34 -0400 | [diff] [blame] | 1055 | if (value >= kPrimIntMax) |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 1056 | return graph->GetIntConstant(kPrimIntMax, GetDexPc()); |
Mark Mendell | e82549b | 2015-05-06 10:55:34 -0400 | [diff] [blame] | 1057 | if (value <= kPrimLongMin) |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 1058 | return graph->GetIntConstant(kPrimIntMin, GetDexPc()); |
| 1059 | return graph->GetIntConstant(static_cast<int32_t>(value), GetDexPc()); |
Mark Mendell | e82549b | 2015-05-06 10:55:34 -0400 | [diff] [blame] | 1060 | case Primitive::kPrimLong: |
| 1061 | if (std::isnan(value)) |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 1062 | return graph->GetLongConstant(0, GetDexPc()); |
Mark Mendell | e82549b | 2015-05-06 10:55:34 -0400 | [diff] [blame] | 1063 | if (value >= kPrimLongMax) |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 1064 | return graph->GetLongConstant(kPrimLongMax, GetDexPc()); |
Mark Mendell | e82549b | 2015-05-06 10:55:34 -0400 | [diff] [blame] | 1065 | if (value <= kPrimLongMin) |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 1066 | return graph->GetLongConstant(kPrimLongMin, GetDexPc()); |
| 1067 | return graph->GetLongConstant(static_cast<int64_t>(value), GetDexPc()); |
Mark Mendell | e82549b | 2015-05-06 10:55:34 -0400 | [diff] [blame] | 1068 | case Primitive::kPrimFloat: |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 1069 | return graph->GetFloatConstant(static_cast<float>(value), GetDexPc()); |
Mark Mendell | e82549b | 2015-05-06 10:55:34 -0400 | [diff] [blame] | 1070 | default: |
| 1071 | return nullptr; |
| 1072 | } |
| 1073 | } |
| 1074 | return nullptr; |
| 1075 | } |
| 1076 | |
Roland Levillain | 9240d6a | 2014-10-20 16:47:04 +0100 | [diff] [blame] | 1077 | HConstant* HUnaryOperation::TryStaticEvaluation() const { |
| 1078 | if (GetInput()->IsIntConstant()) { |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 1079 | return Evaluate(GetInput()->AsIntConstant()); |
Roland Levillain | 9240d6a | 2014-10-20 16:47:04 +0100 | [diff] [blame] | 1080 | } else if (GetInput()->IsLongConstant()) { |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 1081 | return Evaluate(GetInput()->AsLongConstant()); |
Roland Levillain | 9240d6a | 2014-10-20 16:47:04 +0100 | [diff] [blame] | 1082 | } |
| 1083 | return nullptr; |
| 1084 | } |
| 1085 | |
| 1086 | HConstant* HBinaryOperation::TryStaticEvaluation() const { |
Roland Levillain | 9867bc7 | 2015-08-05 10:21:34 +0100 | [diff] [blame] | 1087 | if (GetLeft()->IsIntConstant()) { |
| 1088 | if (GetRight()->IsIntConstant()) { |
| 1089 | return Evaluate(GetLeft()->AsIntConstant(), GetRight()->AsIntConstant()); |
| 1090 | } else if (GetRight()->IsLongConstant()) { |
| 1091 | return Evaluate(GetLeft()->AsIntConstant(), GetRight()->AsLongConstant()); |
| 1092 | } |
| 1093 | } else if (GetLeft()->IsLongConstant()) { |
| 1094 | if (GetRight()->IsIntConstant()) { |
| 1095 | return Evaluate(GetLeft()->AsLongConstant(), GetRight()->AsIntConstant()); |
| 1096 | } else if (GetRight()->IsLongConstant()) { |
| 1097 | return Evaluate(GetLeft()->AsLongConstant(), GetRight()->AsLongConstant()); |
Nicolas Geoffray | 9ee6618 | 2015-01-16 12:35:40 +0000 | [diff] [blame] | 1098 | } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 1099 | } |
| 1100 | return nullptr; |
| 1101 | } |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1102 | |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 1103 | HConstant* HBinaryOperation::GetConstantRight() const { |
| 1104 | if (GetRight()->IsConstant()) { |
| 1105 | return GetRight()->AsConstant(); |
| 1106 | } else if (IsCommutative() && GetLeft()->IsConstant()) { |
| 1107 | return GetLeft()->AsConstant(); |
| 1108 | } else { |
| 1109 | return nullptr; |
| 1110 | } |
| 1111 | } |
| 1112 | |
| 1113 | // If `GetConstantRight()` returns one of the input, this returns the other |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1114 | // one. Otherwise it returns null. |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 1115 | HInstruction* HBinaryOperation::GetLeastConstantLeft() const { |
| 1116 | HInstruction* most_constant_right = GetConstantRight(); |
| 1117 | if (most_constant_right == nullptr) { |
| 1118 | return nullptr; |
| 1119 | } else if (most_constant_right == GetLeft()) { |
| 1120 | return GetRight(); |
| 1121 | } else { |
| 1122 | return GetLeft(); |
| 1123 | } |
| 1124 | } |
| 1125 | |
Mingyao Yang | d43b3ac | 2015-04-01 14:03:04 -0700 | [diff] [blame] | 1126 | bool HCondition::IsBeforeWhenDisregardMoves(HInstruction* instruction) const { |
| 1127 | return this == instruction->GetPreviousDisregardingMoves(); |
Nicolas Geoffray | 18efde5 | 2014-09-22 15:51:11 +0100 | [diff] [blame] | 1128 | } |
| 1129 | |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1130 | bool HInstruction::Equals(HInstruction* other) const { |
| 1131 | if (!InstructionTypeEquals(other)) return false; |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 1132 | DCHECK_EQ(GetKind(), other->GetKind()); |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1133 | if (!InstructionDataEquals(other)) return false; |
| 1134 | if (GetType() != other->GetType()) return false; |
| 1135 | if (InputCount() != other->InputCount()) return false; |
| 1136 | |
| 1137 | for (size_t i = 0, e = InputCount(); i < e; ++i) { |
| 1138 | if (InputAt(i) != other->InputAt(i)) return false; |
| 1139 | } |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 1140 | DCHECK_EQ(ComputeHashCode(), other->ComputeHashCode()); |
Nicolas Geoffray | 065bf77 | 2014-09-03 14:51:22 +0100 | [diff] [blame] | 1141 | return true; |
| 1142 | } |
| 1143 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1144 | std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs) { |
| 1145 | #define DECLARE_CASE(type, super) case HInstruction::k##type: os << #type; break; |
| 1146 | switch (rhs) { |
| 1147 | FOR_EACH_INSTRUCTION(DECLARE_CASE) |
| 1148 | default: |
| 1149 | os << "Unknown instruction kind " << static_cast<int>(rhs); |
| 1150 | break; |
| 1151 | } |
| 1152 | #undef DECLARE_CASE |
| 1153 | return os; |
| 1154 | } |
| 1155 | |
Nicolas Geoffray | 82091da | 2015-01-26 10:02:45 +0000 | [diff] [blame] | 1156 | void HInstruction::MoveBefore(HInstruction* cursor) { |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1157 | next_->previous_ = previous_; |
| 1158 | if (previous_ != nullptr) { |
| 1159 | previous_->next_ = next_; |
| 1160 | } |
| 1161 | if (block_->instructions_.first_instruction_ == this) { |
| 1162 | block_->instructions_.first_instruction_ = next_; |
| 1163 | } |
Nicolas Geoffray | 82091da | 2015-01-26 10:02:45 +0000 | [diff] [blame] | 1164 | DCHECK_NE(block_->instructions_.last_instruction_, this); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1165 | |
| 1166 | previous_ = cursor->previous_; |
| 1167 | if (previous_ != nullptr) { |
| 1168 | previous_->next_ = this; |
| 1169 | } |
| 1170 | next_ = cursor; |
| 1171 | cursor->previous_ = this; |
| 1172 | block_ = cursor->block_; |
Nicolas Geoffray | 82091da | 2015-01-26 10:02:45 +0000 | [diff] [blame] | 1173 | |
| 1174 | if (block_->instructions_.first_instruction_ == cursor) { |
| 1175 | block_->instructions_.first_instruction_ = this; |
| 1176 | } |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1177 | } |
| 1178 | |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 1179 | HBasicBlock* HBasicBlock::SplitBefore(HInstruction* cursor) { |
David Brazdil | 9bc4361 | 2015-11-05 21:25:24 +0000 | [diff] [blame] | 1180 | DCHECK(!graph_->IsInSsaForm()) << "Support for SSA form not implemented."; |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 1181 | DCHECK_EQ(cursor->GetBlock(), this); |
| 1182 | |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 1183 | HBasicBlock* new_block = new (GetGraph()->GetArena()) HBasicBlock(GetGraph(), |
| 1184 | cursor->GetDexPc()); |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 1185 | new_block->instructions_.first_instruction_ = cursor; |
| 1186 | new_block->instructions_.last_instruction_ = instructions_.last_instruction_; |
| 1187 | instructions_.last_instruction_ = cursor->previous_; |
| 1188 | if (cursor->previous_ == nullptr) { |
| 1189 | instructions_.first_instruction_ = nullptr; |
| 1190 | } else { |
| 1191 | cursor->previous_->next_ = nullptr; |
| 1192 | cursor->previous_ = nullptr; |
| 1193 | } |
| 1194 | |
| 1195 | new_block->instructions_.SetBlockOfInstructions(new_block); |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 1196 | AddInstruction(new (GetGraph()->GetArena()) HGoto(new_block->GetDexPc())); |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 1197 | |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1198 | for (HBasicBlock* successor : GetSuccessors()) { |
| 1199 | new_block->successors_.push_back(successor); |
| 1200 | successor->predecessors_[successor->GetPredecessorIndexOf(this)] = new_block; |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 1201 | } |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1202 | successors_.clear(); |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 1203 | AddSuccessor(new_block); |
| 1204 | |
David Brazdil | 56e1acc | 2015-06-30 15:41:36 +0100 | [diff] [blame] | 1205 | GetGraph()->AddBlock(new_block); |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 1206 | return new_block; |
| 1207 | } |
| 1208 | |
David Brazdil | d7558da | 2015-09-22 13:04:14 +0100 | [diff] [blame] | 1209 | HBasicBlock* HBasicBlock::CreateImmediateDominator() { |
David Brazdil | 9bc4361 | 2015-11-05 21:25:24 +0000 | [diff] [blame] | 1210 | DCHECK(!graph_->IsInSsaForm()) << "Support for SSA form not implemented."; |
David Brazdil | d7558da | 2015-09-22 13:04:14 +0100 | [diff] [blame] | 1211 | DCHECK(!IsCatchBlock()) << "Support for updating try/catch information not implemented."; |
| 1212 | |
| 1213 | HBasicBlock* new_block = new (GetGraph()->GetArena()) HBasicBlock(GetGraph(), GetDexPc()); |
| 1214 | |
| 1215 | for (HBasicBlock* predecessor : GetPredecessors()) { |
| 1216 | new_block->predecessors_.push_back(predecessor); |
| 1217 | predecessor->successors_[predecessor->GetSuccessorIndexOf(this)] = new_block; |
| 1218 | } |
| 1219 | predecessors_.clear(); |
| 1220 | AddPredecessor(new_block); |
| 1221 | |
| 1222 | GetGraph()->AddBlock(new_block); |
| 1223 | return new_block; |
| 1224 | } |
| 1225 | |
David Brazdil | 9bc4361 | 2015-11-05 21:25:24 +0000 | [diff] [blame] | 1226 | HBasicBlock* HBasicBlock::SplitCatchBlockAfterMoveException() { |
| 1227 | DCHECK(!graph_->IsInSsaForm()) << "Support for SSA form not implemented."; |
| 1228 | DCHECK(IsCatchBlock()) << "This method is intended for catch blocks only."; |
| 1229 | |
| 1230 | HInstruction* first_insn = GetFirstInstruction(); |
| 1231 | HInstruction* split_before = nullptr; |
| 1232 | |
| 1233 | if (first_insn != nullptr && first_insn->IsLoadException()) { |
| 1234 | // Catch block starts with a LoadException. Split the block after |
| 1235 | // the StoreLocal and ClearException which must come after the load. |
| 1236 | DCHECK(first_insn->GetNext()->IsStoreLocal()); |
| 1237 | DCHECK(first_insn->GetNext()->GetNext()->IsClearException()); |
| 1238 | split_before = first_insn->GetNext()->GetNext()->GetNext(); |
| 1239 | } else { |
| 1240 | // Catch block does not load the exception. Split at the beginning |
| 1241 | // to create an empty catch block. |
| 1242 | split_before = first_insn; |
| 1243 | } |
| 1244 | |
| 1245 | if (split_before == nullptr) { |
| 1246 | // Catch block has no instructions after the split point (must be dead). |
| 1247 | // Do not split it but rather signal error by returning nullptr. |
| 1248 | return nullptr; |
| 1249 | } else { |
| 1250 | return SplitBefore(split_before); |
| 1251 | } |
| 1252 | } |
| 1253 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1254 | HBasicBlock* HBasicBlock::SplitAfter(HInstruction* cursor) { |
| 1255 | DCHECK(!cursor->IsControlFlow()); |
| 1256 | DCHECK_NE(instructions_.last_instruction_, cursor); |
| 1257 | DCHECK_EQ(cursor->GetBlock(), this); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1258 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1259 | HBasicBlock* new_block = new (GetGraph()->GetArena()) HBasicBlock(GetGraph(), GetDexPc()); |
| 1260 | new_block->instructions_.first_instruction_ = cursor->GetNext(); |
| 1261 | new_block->instructions_.last_instruction_ = instructions_.last_instruction_; |
| 1262 | cursor->next_->previous_ = nullptr; |
| 1263 | cursor->next_ = nullptr; |
| 1264 | instructions_.last_instruction_ = cursor; |
| 1265 | |
| 1266 | new_block->instructions_.SetBlockOfInstructions(new_block); |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1267 | for (HBasicBlock* successor : GetSuccessors()) { |
| 1268 | new_block->successors_.push_back(successor); |
| 1269 | successor->predecessors_[successor->GetPredecessorIndexOf(this)] = new_block; |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1270 | } |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1271 | successors_.clear(); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1272 | |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1273 | for (HBasicBlock* dominated : GetDominatedBlocks()) { |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1274 | dominated->dominator_ = new_block; |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1275 | new_block->dominated_blocks_.push_back(dominated); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1276 | } |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1277 | dominated_blocks_.clear(); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1278 | return new_block; |
| 1279 | } |
| 1280 | |
David Brazdil | ec16f79 | 2015-08-19 15:04:01 +0100 | [diff] [blame] | 1281 | const HTryBoundary* HBasicBlock::ComputeTryEntryOfSuccessors() const { |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 1282 | if (EndsWithTryBoundary()) { |
| 1283 | HTryBoundary* try_boundary = GetLastInstruction()->AsTryBoundary(); |
| 1284 | if (try_boundary->IsEntry()) { |
David Brazdil | ec16f79 | 2015-08-19 15:04:01 +0100 | [diff] [blame] | 1285 | DCHECK(!IsTryBlock()); |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 1286 | return try_boundary; |
| 1287 | } else { |
David Brazdil | ec16f79 | 2015-08-19 15:04:01 +0100 | [diff] [blame] | 1288 | DCHECK(IsTryBlock()); |
| 1289 | DCHECK(try_catch_information_->GetTryEntry().HasSameExceptionHandlersAs(*try_boundary)); |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 1290 | return nullptr; |
| 1291 | } |
David Brazdil | ec16f79 | 2015-08-19 15:04:01 +0100 | [diff] [blame] | 1292 | } else if (IsTryBlock()) { |
| 1293 | return &try_catch_information_->GetTryEntry(); |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 1294 | } else { |
David Brazdil | ec16f79 | 2015-08-19 15:04:01 +0100 | [diff] [blame] | 1295 | return nullptr; |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 1296 | } |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 1297 | } |
| 1298 | |
David Brazdil | d7558da | 2015-09-22 13:04:14 +0100 | [diff] [blame] | 1299 | bool HBasicBlock::HasThrowingInstructions() const { |
| 1300 | for (HInstructionIterator it(GetInstructions()); !it.Done(); it.Advance()) { |
| 1301 | if (it.Current()->CanThrow()) { |
| 1302 | return true; |
| 1303 | } |
| 1304 | } |
| 1305 | return false; |
| 1306 | } |
| 1307 | |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 1308 | static bool HasOnlyOneInstruction(const HBasicBlock& block) { |
| 1309 | return block.GetPhis().IsEmpty() |
| 1310 | && !block.GetInstructions().IsEmpty() |
| 1311 | && block.GetFirstInstruction() == block.GetLastInstruction(); |
| 1312 | } |
| 1313 | |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 1314 | bool HBasicBlock::IsSingleGoto() const { |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 1315 | return HasOnlyOneInstruction(*this) && GetLastInstruction()->IsGoto(); |
| 1316 | } |
| 1317 | |
| 1318 | bool HBasicBlock::IsSingleTryBoundary() const { |
| 1319 | return HasOnlyOneInstruction(*this) && GetLastInstruction()->IsTryBoundary(); |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 1320 | } |
| 1321 | |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 1322 | bool HBasicBlock::EndsWithControlFlowInstruction() const { |
| 1323 | return !GetInstructions().IsEmpty() && GetLastInstruction()->IsControlFlow(); |
| 1324 | } |
| 1325 | |
David Brazdil | b2bd1c5 | 2015-03-25 11:17:37 +0000 | [diff] [blame] | 1326 | bool HBasicBlock::EndsWithIf() const { |
| 1327 | return !GetInstructions().IsEmpty() && GetLastInstruction()->IsIf(); |
| 1328 | } |
| 1329 | |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 1330 | bool HBasicBlock::EndsWithTryBoundary() const { |
| 1331 | return !GetInstructions().IsEmpty() && GetLastInstruction()->IsTryBoundary(); |
| 1332 | } |
| 1333 | |
David Brazdil | b2bd1c5 | 2015-03-25 11:17:37 +0000 | [diff] [blame] | 1334 | bool HBasicBlock::HasSinglePhi() const { |
| 1335 | return !GetPhis().IsEmpty() && GetFirstPhi()->GetNext() == nullptr; |
| 1336 | } |
| 1337 | |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 1338 | bool HTryBoundary::HasSameExceptionHandlersAs(const HTryBoundary& other) const { |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1339 | if (GetBlock()->GetSuccessors().size() != other.GetBlock()->GetSuccessors().size()) { |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 1340 | return false; |
| 1341 | } |
| 1342 | |
David Brazdil | b618ade | 2015-07-29 10:31:29 +0100 | [diff] [blame] | 1343 | // Exception handlers need to be stored in the same order. |
| 1344 | for (HExceptionHandlerIterator it1(*this), it2(other); |
| 1345 | !it1.Done(); |
| 1346 | it1.Advance(), it2.Advance()) { |
| 1347 | DCHECK(!it2.Done()); |
| 1348 | if (it1.Current() != it2.Current()) { |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 1349 | return false; |
| 1350 | } |
| 1351 | } |
| 1352 | return true; |
| 1353 | } |
| 1354 | |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1355 | size_t HInstructionList::CountSize() const { |
| 1356 | size_t size = 0; |
| 1357 | HInstruction* current = first_instruction_; |
| 1358 | for (; current != nullptr; current = current->GetNext()) { |
| 1359 | size++; |
| 1360 | } |
| 1361 | return size; |
| 1362 | } |
| 1363 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1364 | void HInstructionList::SetBlockOfInstructions(HBasicBlock* block) const { |
| 1365 | for (HInstruction* current = first_instruction_; |
| 1366 | current != nullptr; |
| 1367 | current = current->GetNext()) { |
| 1368 | current->SetBlock(block); |
| 1369 | } |
| 1370 | } |
| 1371 | |
| 1372 | void HInstructionList::AddAfter(HInstruction* cursor, const HInstructionList& instruction_list) { |
| 1373 | DCHECK(Contains(cursor)); |
| 1374 | if (!instruction_list.IsEmpty()) { |
| 1375 | if (cursor == last_instruction_) { |
| 1376 | last_instruction_ = instruction_list.last_instruction_; |
| 1377 | } else { |
| 1378 | cursor->next_->previous_ = instruction_list.last_instruction_; |
| 1379 | } |
| 1380 | instruction_list.last_instruction_->next_ = cursor->next_; |
| 1381 | cursor->next_ = instruction_list.first_instruction_; |
| 1382 | instruction_list.first_instruction_->previous_ = cursor; |
| 1383 | } |
| 1384 | } |
| 1385 | |
| 1386 | void HInstructionList::Add(const HInstructionList& instruction_list) { |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 1387 | if (IsEmpty()) { |
| 1388 | first_instruction_ = instruction_list.first_instruction_; |
| 1389 | last_instruction_ = instruction_list.last_instruction_; |
| 1390 | } else { |
| 1391 | AddAfter(last_instruction_, instruction_list); |
| 1392 | } |
| 1393 | } |
| 1394 | |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1395 | void HBasicBlock::DisconnectAndDelete() { |
| 1396 | // Dominators must be removed after all the blocks they dominate. This way |
| 1397 | // a loop header is removed last, a requirement for correct loop information |
| 1398 | // iteration. |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1399 | DCHECK(dominated_blocks_.empty()); |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 1400 | |
David Brazdil | 8a7c0fe | 2015-11-02 20:24:55 +0000 | [diff] [blame^] | 1401 | // (1) Remove the block from all loops it is included in. |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1402 | for (HLoopInformationOutwardIterator it(*this); !it.Done(); it.Advance()) { |
| 1403 | HLoopInformation* loop_info = it.Current(); |
| 1404 | loop_info->Remove(this); |
| 1405 | if (loop_info->IsBackEdge(*this)) { |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 1406 | // If this was the last back edge of the loop, we deliberately leave the |
| 1407 | // loop in an inconsistent state and will fail SSAChecker unless the |
| 1408 | // entire loop is removed during the pass. |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1409 | loop_info->RemoveBackEdge(this); |
| 1410 | } |
| 1411 | } |
| 1412 | |
David Brazdil | 8a7c0fe | 2015-11-02 20:24:55 +0000 | [diff] [blame^] | 1413 | // (2) Disconnect the block from its predecessors and update their |
| 1414 | // control-flow instructions. |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1415 | for (HBasicBlock* predecessor : predecessors_) { |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1416 | HInstruction* last_instruction = predecessor->GetLastInstruction(); |
David Brazdil | 8a7c0fe | 2015-11-02 20:24:55 +0000 | [diff] [blame^] | 1417 | if (last_instruction->IsTryBoundary() && !IsCatchBlock()) { |
| 1418 | // This block is the only normal-flow successor of the TryBoundary which |
| 1419 | // makes `predecessor` dead. Since DCE removes blocks in post order, |
| 1420 | // exception handlers of this TryBoundary were already visited and any |
| 1421 | // remaining handlers therefore must be live. We remove `predecessor` from |
| 1422 | // their list of predecessors. |
| 1423 | DCHECK_EQ(last_instruction->AsTryBoundary()->GetNormalFlowSuccessor(), this); |
| 1424 | while (predecessor->GetSuccessors().size() > 1) { |
| 1425 | HBasicBlock* handler = predecessor->GetSuccessors()[1]; |
| 1426 | DCHECK(handler->IsCatchBlock()); |
| 1427 | predecessor->RemoveSuccessor(handler); |
| 1428 | handler->RemovePredecessor(predecessor); |
| 1429 | } |
| 1430 | } |
| 1431 | |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1432 | predecessor->RemoveSuccessor(this); |
Mark Mendell | fe57faa | 2015-09-18 09:26:15 -0400 | [diff] [blame] | 1433 | uint32_t num_pred_successors = predecessor->GetSuccessors().size(); |
| 1434 | if (num_pred_successors == 1u) { |
| 1435 | // If we have one successor after removing one, then we must have |
David Brazdil | 8a7c0fe | 2015-11-02 20:24:55 +0000 | [diff] [blame^] | 1436 | // had an HIf, HPackedSwitch or HTryBoundary, as they have more than one |
| 1437 | // successor. Replace those with a HGoto. |
| 1438 | DCHECK(last_instruction->IsIf() || |
| 1439 | last_instruction->IsPackedSwitch() || |
| 1440 | (last_instruction->IsTryBoundary() && IsCatchBlock())); |
Mark Mendell | fe57faa | 2015-09-18 09:26:15 -0400 | [diff] [blame] | 1441 | predecessor->RemoveInstruction(last_instruction); |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 1442 | predecessor->AddInstruction(new (graph_->GetArena()) HGoto(last_instruction->GetDexPc())); |
Mark Mendell | fe57faa | 2015-09-18 09:26:15 -0400 | [diff] [blame] | 1443 | } else if (num_pred_successors == 0u) { |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1444 | // The predecessor has no remaining successors and therefore must be dead. |
| 1445 | // We deliberately leave it without a control-flow instruction so that the |
| 1446 | // SSAChecker fails unless it is not removed during the pass too. |
Mark Mendell | fe57faa | 2015-09-18 09:26:15 -0400 | [diff] [blame] | 1447 | predecessor->RemoveInstruction(last_instruction); |
| 1448 | } else { |
David Brazdil | 8a7c0fe | 2015-11-02 20:24:55 +0000 | [diff] [blame^] | 1449 | // There are multiple successors left. The removed block might be a successor |
| 1450 | // of a PackedSwitch which will be completely removed (perhaps replaced with |
| 1451 | // a Goto), or we are deleting a catch block from a TryBoundary. In either |
| 1452 | // case, leave `last_instruction` as is for now. |
| 1453 | DCHECK(last_instruction->IsPackedSwitch() || |
| 1454 | (last_instruction->IsTryBoundary() && IsCatchBlock())); |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1455 | } |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 1456 | } |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1457 | predecessors_.clear(); |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1458 | |
David Brazdil | 8a7c0fe | 2015-11-02 20:24:55 +0000 | [diff] [blame^] | 1459 | // (3) Disconnect the block from its successors and update their phis. |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1460 | for (HBasicBlock* successor : successors_) { |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1461 | // Delete this block from the list of predecessors. |
| 1462 | size_t this_index = successor->GetPredecessorIndexOf(this); |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1463 | successor->predecessors_.erase(successor->predecessors_.begin() + this_index); |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1464 | |
| 1465 | // Check that `successor` has other predecessors, otherwise `this` is the |
| 1466 | // dominator of `successor` which violates the order DCHECKed at the top. |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1467 | DCHECK(!successor->predecessors_.empty()); |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1468 | |
David Brazdil | 8a7c0fe | 2015-11-02 20:24:55 +0000 | [diff] [blame^] | 1469 | // Remove this block's entries in the successor's phis. Skip exceptional |
| 1470 | // successors because catch phi inputs do not correspond to predecessor |
| 1471 | // blocks but throwing instructions. Their inputs will be updated in step (4). |
| 1472 | if (!successor->IsCatchBlock()) { |
| 1473 | if (successor->predecessors_.size() == 1u) { |
| 1474 | // The successor has just one predecessor left. Replace phis with the only |
| 1475 | // remaining input. |
| 1476 | for (HInstructionIterator phi_it(successor->GetPhis()); !phi_it.Done(); phi_it.Advance()) { |
| 1477 | HPhi* phi = phi_it.Current()->AsPhi(); |
| 1478 | phi->ReplaceWith(phi->InputAt(1 - this_index)); |
| 1479 | successor->RemovePhi(phi); |
| 1480 | } |
| 1481 | } else { |
| 1482 | for (HInstructionIterator phi_it(successor->GetPhis()); !phi_it.Done(); phi_it.Advance()) { |
| 1483 | phi_it.Current()->AsPhi()->RemoveInputAt(this_index); |
| 1484 | } |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1485 | } |
| 1486 | } |
| 1487 | } |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1488 | successors_.clear(); |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1489 | |
David Brazdil | 8a7c0fe | 2015-11-02 20:24:55 +0000 | [diff] [blame^] | 1490 | // (4) Remove instructions and phis. Instructions should have no remaining uses |
| 1491 | // except in catch phis. If an instruction is used by a catch phi at `index`, |
| 1492 | // remove `index`-th input of all phis in the catch block since they are |
| 1493 | // guaranteed dead. Note that we may miss dead inputs this way but the |
| 1494 | // graph will always remain consistent. |
| 1495 | for (HBackwardInstructionIterator it(GetInstructions()); !it.Done(); it.Advance()) { |
| 1496 | HInstruction* insn = it.Current(); |
| 1497 | while (insn->HasUses()) { |
| 1498 | DCHECK(IsTryBlock()); |
| 1499 | HUseListNode<HInstruction*>* use = insn->GetUses().GetFirst(); |
| 1500 | size_t use_index = use->GetIndex(); |
| 1501 | HBasicBlock* user_block = use->GetUser()->GetBlock(); |
| 1502 | DCHECK(use->GetUser()->IsPhi() && user_block->IsCatchBlock()); |
| 1503 | for (HInstructionIterator phi_it(user_block->GetPhis()); !phi_it.Done(); phi_it.Advance()) { |
| 1504 | phi_it.Current()->AsPhi()->RemoveInputAt(use_index); |
| 1505 | } |
| 1506 | } |
| 1507 | |
| 1508 | RemoveInstruction(insn); |
| 1509 | } |
| 1510 | for (HInstructionIterator it(GetPhis()); !it.Done(); it.Advance()) { |
| 1511 | RemovePhi(it.Current()->AsPhi()); |
| 1512 | } |
| 1513 | |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1514 | // Disconnect from the dominator. |
| 1515 | dominator_->RemoveDominatedBlock(this); |
| 1516 | SetDominator(nullptr); |
| 1517 | |
David Brazdil | 8a7c0fe | 2015-11-02 20:24:55 +0000 | [diff] [blame^] | 1518 | // Delete from the graph, update reverse post order. |
| 1519 | graph_->DeleteDeadEmptyBlock(this); |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1520 | SetGraph(nullptr); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1521 | } |
| 1522 | |
| 1523 | void HBasicBlock::MergeWith(HBasicBlock* other) { |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1524 | DCHECK_EQ(GetGraph(), other->GetGraph()); |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1525 | DCHECK(ContainsElement(dominated_blocks_, other)); |
| 1526 | DCHECK_EQ(GetSingleSuccessor(), other); |
| 1527 | DCHECK_EQ(other->GetSinglePredecessor(), this); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1528 | DCHECK(other->GetPhis().IsEmpty()); |
| 1529 | |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1530 | // Move instructions from `other` to `this`. |
| 1531 | DCHECK(EndsWithControlFlowInstruction()); |
| 1532 | RemoveInstruction(GetLastInstruction()); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1533 | instructions_.Add(other->GetInstructions()); |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1534 | other->instructions_.SetBlockOfInstructions(this); |
| 1535 | other->instructions_.Clear(); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1536 | |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1537 | // Remove `other` from the loops it is included in. |
| 1538 | for (HLoopInformationOutwardIterator it(*other); !it.Done(); it.Advance()) { |
| 1539 | HLoopInformation* loop_info = it.Current(); |
| 1540 | loop_info->Remove(other); |
| 1541 | if (loop_info->IsBackEdge(*other)) { |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 1542 | loop_info->ReplaceBackEdge(other, this); |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1543 | } |
| 1544 | } |
| 1545 | |
| 1546 | // Update links to the successors of `other`. |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1547 | successors_.clear(); |
| 1548 | while (!other->successors_.empty()) { |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 1549 | HBasicBlock* successor = other->GetSuccessors()[0]; |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1550 | successor->ReplacePredecessor(other, this); |
| 1551 | } |
| 1552 | |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1553 | // Update the dominator tree. |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1554 | RemoveDominatedBlock(other); |
| 1555 | for (HBasicBlock* dominated : other->GetDominatedBlocks()) { |
| 1556 | dominated_blocks_.push_back(dominated); |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1557 | dominated->SetDominator(this); |
| 1558 | } |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1559 | other->dominated_blocks_.clear(); |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1560 | other->dominator_ = nullptr; |
| 1561 | |
| 1562 | // Clear the list of predecessors of `other` in preparation of deleting it. |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1563 | other->predecessors_.clear(); |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1564 | |
| 1565 | // Delete `other` from the graph. The function updates reverse post order. |
David Brazdil | 8a7c0fe | 2015-11-02 20:24:55 +0000 | [diff] [blame^] | 1566 | graph_->DeleteDeadEmptyBlock(other); |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1567 | other->SetGraph(nullptr); |
| 1568 | } |
| 1569 | |
| 1570 | void HBasicBlock::MergeWithInlined(HBasicBlock* other) { |
| 1571 | DCHECK_NE(GetGraph(), other->GetGraph()); |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1572 | DCHECK(GetDominatedBlocks().empty()); |
| 1573 | DCHECK(GetSuccessors().empty()); |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1574 | DCHECK(!EndsWithControlFlowInstruction()); |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1575 | DCHECK(other->GetSinglePredecessor()->IsEntryBlock()); |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1576 | DCHECK(other->GetPhis().IsEmpty()); |
| 1577 | DCHECK(!other->IsInLoop()); |
| 1578 | |
| 1579 | // Move instructions from `other` to `this`. |
| 1580 | instructions_.Add(other->GetInstructions()); |
| 1581 | other->instructions_.SetBlockOfInstructions(this); |
| 1582 | |
| 1583 | // Update links to the successors of `other`. |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1584 | successors_.clear(); |
| 1585 | while (!other->successors_.empty()) { |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 1586 | HBasicBlock* successor = other->GetSuccessors()[0]; |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1587 | successor->ReplacePredecessor(other, this); |
| 1588 | } |
| 1589 | |
| 1590 | // Update the dominator tree. |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1591 | for (HBasicBlock* dominated : other->GetDominatedBlocks()) { |
| 1592 | dominated_blocks_.push_back(dominated); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1593 | dominated->SetDominator(this); |
| 1594 | } |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1595 | other->dominated_blocks_.clear(); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1596 | other->dominator_ = nullptr; |
| 1597 | other->graph_ = nullptr; |
| 1598 | } |
| 1599 | |
| 1600 | void HBasicBlock::ReplaceWith(HBasicBlock* other) { |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1601 | while (!GetPredecessors().empty()) { |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 1602 | HBasicBlock* predecessor = GetPredecessors()[0]; |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1603 | predecessor->ReplaceSuccessor(this, other); |
| 1604 | } |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1605 | while (!GetSuccessors().empty()) { |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 1606 | HBasicBlock* successor = GetSuccessors()[0]; |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1607 | successor->ReplacePredecessor(this, other); |
| 1608 | } |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1609 | for (HBasicBlock* dominated : GetDominatedBlocks()) { |
| 1610 | other->AddDominatedBlock(dominated); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1611 | } |
| 1612 | GetDominator()->ReplaceDominatedBlock(this, other); |
| 1613 | other->SetDominator(GetDominator()); |
| 1614 | dominator_ = nullptr; |
| 1615 | graph_ = nullptr; |
| 1616 | } |
| 1617 | |
| 1618 | // Create space in `blocks` for adding `number_of_new_blocks` entries |
| 1619 | // starting at location `at`. Blocks after `at` are moved accordingly. |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 1620 | static void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks, |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1621 | size_t number_of_new_blocks, |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 1622 | size_t after) { |
| 1623 | DCHECK_LT(after, blocks->size()); |
| 1624 | size_t old_size = blocks->size(); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1625 | size_t new_size = old_size + number_of_new_blocks; |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 1626 | blocks->resize(new_size); |
| 1627 | std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end()); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1628 | } |
| 1629 | |
David Brazdil | 8a7c0fe | 2015-11-02 20:24:55 +0000 | [diff] [blame^] | 1630 | void HGraph::DeleteDeadEmptyBlock(HBasicBlock* block) { |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1631 | DCHECK_EQ(block->GetGraph(), this); |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1632 | DCHECK(block->GetSuccessors().empty()); |
| 1633 | DCHECK(block->GetPredecessors().empty()); |
| 1634 | DCHECK(block->GetDominatedBlocks().empty()); |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1635 | DCHECK(block->GetDominator() == nullptr); |
David Brazdil | 8a7c0fe | 2015-11-02 20:24:55 +0000 | [diff] [blame^] | 1636 | DCHECK(block->GetInstructions().IsEmpty()); |
| 1637 | DCHECK(block->GetPhis().IsEmpty()); |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1638 | |
David Brazdil | c7af85d | 2015-05-26 12:05:55 +0100 | [diff] [blame] | 1639 | if (block->IsExitBlock()) { |
| 1640 | exit_block_ = nullptr; |
| 1641 | } |
| 1642 | |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 1643 | RemoveElement(reverse_post_order_, block); |
| 1644 | blocks_[block->GetBlockId()] = nullptr; |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1645 | } |
| 1646 | |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 1647 | HInstruction* HGraph::InlineInto(HGraph* outer_graph, HInvoke* invoke) { |
David Brazdil | c7af85d | 2015-05-26 12:05:55 +0100 | [diff] [blame] | 1648 | DCHECK(HasExitBlock()) << "Unimplemented scenario"; |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 1649 | // Update the environments in this graph to have the invoke's environment |
| 1650 | // as parent. |
| 1651 | { |
| 1652 | HReversePostOrderIterator it(*this); |
| 1653 | it.Advance(); // Skip the entry block, we do not need to update the entry's suspend check. |
| 1654 | for (; !it.Done(); it.Advance()) { |
| 1655 | HBasicBlock* block = it.Current(); |
| 1656 | for (HInstructionIterator instr_it(block->GetInstructions()); |
| 1657 | !instr_it.Done(); |
| 1658 | instr_it.Advance()) { |
| 1659 | HInstruction* current = instr_it.Current(); |
| 1660 | if (current->NeedsEnvironment()) { |
| 1661 | current->GetEnvironment()->SetAndCopyParentChain( |
| 1662 | outer_graph->GetArena(), invoke->GetEnvironment()); |
| 1663 | } |
| 1664 | } |
| 1665 | } |
| 1666 | } |
| 1667 | outer_graph->UpdateMaximumNumberOfOutVRegs(GetMaximumNumberOfOutVRegs()); |
| 1668 | if (HasBoundsChecks()) { |
| 1669 | outer_graph->SetHasBoundsChecks(true); |
| 1670 | } |
| 1671 | |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 1672 | HInstruction* return_value = nullptr; |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 1673 | if (GetBlocks().size() == 3) { |
Nicolas Geoffray | be31ff9 | 2015-02-04 14:52:20 +0000 | [diff] [blame] | 1674 | // Simple case of an entry block, a body block, and an exit block. |
| 1675 | // Put the body block's instruction into `invoke`'s block. |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 1676 | HBasicBlock* body = GetBlocks()[1]; |
| 1677 | DCHECK(GetBlocks()[0]->IsEntryBlock()); |
| 1678 | DCHECK(GetBlocks()[2]->IsExitBlock()); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1679 | DCHECK(!body->IsExitBlock()); |
| 1680 | HInstruction* last = body->GetLastInstruction(); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1681 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1682 | invoke->GetBlock()->instructions_.AddAfter(invoke, body->GetInstructions()); |
| 1683 | body->GetInstructions().SetBlockOfInstructions(invoke->GetBlock()); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1684 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1685 | // Replace the invoke with the return value of the inlined graph. |
| 1686 | if (last->IsReturn()) { |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 1687 | return_value = last->InputAt(0); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1688 | } else { |
| 1689 | DCHECK(last->IsReturnVoid()); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1690 | } |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1691 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1692 | invoke->GetBlock()->RemoveInstruction(last); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1693 | } else { |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1694 | // Need to inline multiple blocks. We split `invoke`'s block |
| 1695 | // into two blocks, merge the first block of the inlined graph into |
Nicolas Geoffray | be31ff9 | 2015-02-04 14:52:20 +0000 | [diff] [blame] | 1696 | // the first half, and replace the exit block of the inlined graph |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1697 | // with the second half. |
| 1698 | ArenaAllocator* allocator = outer_graph->GetArena(); |
| 1699 | HBasicBlock* at = invoke->GetBlock(); |
| 1700 | HBasicBlock* to = at->SplitAfter(invoke); |
| 1701 | |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 1702 | HBasicBlock* first = entry_block_->GetSuccessors()[0]; |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1703 | DCHECK(!first->IsInLoop()); |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 1704 | at->MergeWithInlined(first); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1705 | exit_block_->ReplaceWith(to); |
| 1706 | |
| 1707 | // Update all predecessors of the exit block (now the `to` block) |
Nicolas Geoffray | 817bce7 | 2015-02-24 13:35:38 +0000 | [diff] [blame] | 1708 | // to not `HReturn` but `HGoto` instead. |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 1709 | bool returns_void = to->GetPredecessors()[0]->GetLastInstruction()->IsReturnVoid(); |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1710 | if (to->GetPredecessors().size() == 1) { |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 1711 | HBasicBlock* predecessor = to->GetPredecessors()[0]; |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1712 | HInstruction* last = predecessor->GetLastInstruction(); |
Nicolas Geoffray | 817bce7 | 2015-02-24 13:35:38 +0000 | [diff] [blame] | 1713 | if (!returns_void) { |
| 1714 | return_value = last->InputAt(0); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1715 | } |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 1716 | predecessor->AddInstruction(new (allocator) HGoto(last->GetDexPc())); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1717 | predecessor->RemoveInstruction(last); |
Nicolas Geoffray | 817bce7 | 2015-02-24 13:35:38 +0000 | [diff] [blame] | 1718 | } else { |
| 1719 | if (!returns_void) { |
| 1720 | // There will be multiple returns. |
Nicolas Geoffray | 4f1a384 | 2015-03-12 10:34:11 +0000 | [diff] [blame] | 1721 | return_value = new (allocator) HPhi( |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 1722 | allocator, kNoRegNumber, 0, HPhi::ToPhiType(invoke->GetType()), to->GetDexPc()); |
Nicolas Geoffray | 817bce7 | 2015-02-24 13:35:38 +0000 | [diff] [blame] | 1723 | to->AddPhi(return_value->AsPhi()); |
| 1724 | } |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1725 | for (HBasicBlock* predecessor : to->GetPredecessors()) { |
Nicolas Geoffray | 817bce7 | 2015-02-24 13:35:38 +0000 | [diff] [blame] | 1726 | HInstruction* last = predecessor->GetLastInstruction(); |
| 1727 | if (!returns_void) { |
| 1728 | return_value->AsPhi()->AddInput(last->InputAt(0)); |
| 1729 | } |
Yevgeny Rouban | 3ecfd65 | 2015-09-07 17:57:00 +0600 | [diff] [blame] | 1730 | predecessor->AddInstruction(new (allocator) HGoto(last->GetDexPc())); |
Nicolas Geoffray | 817bce7 | 2015-02-24 13:35:38 +0000 | [diff] [blame] | 1731 | predecessor->RemoveInstruction(last); |
| 1732 | } |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1733 | } |
| 1734 | |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1735 | // Update the meta information surrounding blocks: |
| 1736 | // (1) the graph they are now in, |
| 1737 | // (2) the reverse post order of that graph, |
David Brazdil | 9517798 | 2015-10-30 12:56:58 -0500 | [diff] [blame] | 1738 | // (3) the potential loop information they are now in, |
| 1739 | // (4) try block membership. |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1740 | |
| 1741 | // We don't add the entry block, the exit block, and the first block, which |
| 1742 | // has been merged with `at`. |
| 1743 | static constexpr int kNumberOfSkippedBlocksInCallee = 3; |
| 1744 | |
| 1745 | // We add the `to` block. |
| 1746 | static constexpr int kNumberOfNewBlocksInCaller = 1; |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 1747 | size_t blocks_added = (reverse_post_order_.size() - kNumberOfSkippedBlocksInCallee) |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1748 | + kNumberOfNewBlocksInCaller; |
| 1749 | |
| 1750 | // Find the location of `at` in the outer graph's reverse post order. The new |
| 1751 | // blocks will be added after it. |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 1752 | size_t index_of_at = IndexOfElement(outer_graph->reverse_post_order_, at); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1753 | MakeRoomFor(&outer_graph->reverse_post_order_, blocks_added, index_of_at); |
| 1754 | |
David Brazdil | 9517798 | 2015-10-30 12:56:58 -0500 | [diff] [blame] | 1755 | HLoopInformation* loop_info = at->GetLoopInformation(); |
| 1756 | // Copy TryCatchInformation if `at` is a try block, not if it is a catch block. |
| 1757 | TryCatchInformation* try_catch_info = at->IsTryBlock() ? at->GetTryCatchInformation() : nullptr; |
| 1758 | |
| 1759 | // Do a reverse post order of the blocks in the callee and do (1), (2), (3) |
| 1760 | // and (4) to the blocks that apply. |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1761 | for (HReversePostOrderIterator it(*this); !it.Done(); it.Advance()) { |
| 1762 | HBasicBlock* current = it.Current(); |
| 1763 | if (current != exit_block_ && current != entry_block_ && current != first) { |
| 1764 | DCHECK(!current->IsInLoop()); |
David Brazdil | 9517798 | 2015-10-30 12:56:58 -0500 | [diff] [blame] | 1765 | DCHECK(current->GetTryCatchInformation() == nullptr); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1766 | DCHECK(current->GetGraph() == this); |
| 1767 | current->SetGraph(outer_graph); |
| 1768 | outer_graph->AddBlock(current); |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 1769 | outer_graph->reverse_post_order_[++index_of_at] = current; |
David Brazdil | 9517798 | 2015-10-30 12:56:58 -0500 | [diff] [blame] | 1770 | if (loop_info != nullptr) { |
| 1771 | current->SetLoopInformation(loop_info); |
David Brazdil | 7d27537 | 2015-04-21 16:36:35 +0100 | [diff] [blame] | 1772 | for (HLoopInformationOutwardIterator loop_it(*at); !loop_it.Done(); loop_it.Advance()) { |
| 1773 | loop_it.Current()->Add(current); |
| 1774 | } |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1775 | } |
David Brazdil | 9517798 | 2015-10-30 12:56:58 -0500 | [diff] [blame] | 1776 | current->SetTryCatchInformation(try_catch_info); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1777 | } |
| 1778 | } |
| 1779 | |
David Brazdil | 9517798 | 2015-10-30 12:56:58 -0500 | [diff] [blame] | 1780 | // Do (1), (2), (3) and (4) to `to`. |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1781 | to->SetGraph(outer_graph); |
| 1782 | outer_graph->AddBlock(to); |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 1783 | outer_graph->reverse_post_order_[++index_of_at] = to; |
David Brazdil | 9517798 | 2015-10-30 12:56:58 -0500 | [diff] [blame] | 1784 | if (loop_info != nullptr) { |
| 1785 | to->SetLoopInformation(loop_info); |
David Brazdil | 7d27537 | 2015-04-21 16:36:35 +0100 | [diff] [blame] | 1786 | for (HLoopInformationOutwardIterator loop_it(*at); !loop_it.Done(); loop_it.Advance()) { |
| 1787 | loop_it.Current()->Add(to); |
| 1788 | } |
David Brazdil | 9517798 | 2015-10-30 12:56:58 -0500 | [diff] [blame] | 1789 | if (loop_info->IsBackEdge(*at)) { |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 1790 | // Only `to` can become a back edge, as the inlined blocks |
| 1791 | // are predecessors of `to`. |
David Brazdil | 9517798 | 2015-10-30 12:56:58 -0500 | [diff] [blame] | 1792 | loop_info->ReplaceBackEdge(at, to); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1793 | } |
| 1794 | } |
David Brazdil | 9517798 | 2015-10-30 12:56:58 -0500 | [diff] [blame] | 1795 | to->SetTryCatchInformation(try_catch_info); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1796 | } |
Nicolas Geoffray | 7c5367b | 2014-12-17 10:13:46 +0000 | [diff] [blame] | 1797 | |
David Brazdil | 05144f4 | 2015-04-16 15:18:00 +0100 | [diff] [blame] | 1798 | // Update the next instruction id of the outer graph, so that instructions |
| 1799 | // added later get bigger ids than those in the inner graph. |
| 1800 | outer_graph->SetCurrentInstructionId(GetNextInstructionId()); |
| 1801 | |
| 1802 | // Walk over the entry block and: |
| 1803 | // - Move constants from the entry block to the outer_graph's entry block, |
| 1804 | // - Replace HParameterValue instructions with their real value. |
| 1805 | // - Remove suspend checks, that hold an environment. |
| 1806 | // We must do this after the other blocks have been inlined, otherwise ids of |
| 1807 | // constants could overlap with the inner graph. |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 1808 | size_t parameter_index = 0; |
David Brazdil | 05144f4 | 2015-04-16 15:18:00 +0100 | [diff] [blame] | 1809 | for (HInstructionIterator it(entry_block_->GetInstructions()); !it.Done(); it.Advance()) { |
| 1810 | HInstruction* current = it.Current(); |
Calin Juravle | 214bbcd | 2015-10-20 14:54:07 +0100 | [diff] [blame] | 1811 | HInstruction* replacement = nullptr; |
David Brazdil | 05144f4 | 2015-04-16 15:18:00 +0100 | [diff] [blame] | 1812 | if (current->IsNullConstant()) { |
Calin Juravle | 214bbcd | 2015-10-20 14:54:07 +0100 | [diff] [blame] | 1813 | replacement = outer_graph->GetNullConstant(current->GetDexPc()); |
David Brazdil | 05144f4 | 2015-04-16 15:18:00 +0100 | [diff] [blame] | 1814 | } else if (current->IsIntConstant()) { |
Calin Juravle | 214bbcd | 2015-10-20 14:54:07 +0100 | [diff] [blame] | 1815 | replacement = outer_graph->GetIntConstant( |
| 1816 | current->AsIntConstant()->GetValue(), current->GetDexPc()); |
David Brazdil | 05144f4 | 2015-04-16 15:18:00 +0100 | [diff] [blame] | 1817 | } else if (current->IsLongConstant()) { |
Calin Juravle | 214bbcd | 2015-10-20 14:54:07 +0100 | [diff] [blame] | 1818 | replacement = outer_graph->GetLongConstant( |
| 1819 | current->AsLongConstant()->GetValue(), current->GetDexPc()); |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 1820 | } else if (current->IsFloatConstant()) { |
Calin Juravle | 214bbcd | 2015-10-20 14:54:07 +0100 | [diff] [blame] | 1821 | replacement = outer_graph->GetFloatConstant( |
| 1822 | current->AsFloatConstant()->GetValue(), current->GetDexPc()); |
Nicolas Geoffray | f213e05 | 2015-04-27 08:53:46 +0000 | [diff] [blame] | 1823 | } else if (current->IsDoubleConstant()) { |
Calin Juravle | 214bbcd | 2015-10-20 14:54:07 +0100 | [diff] [blame] | 1824 | replacement = outer_graph->GetDoubleConstant( |
| 1825 | current->AsDoubleConstant()->GetValue(), current->GetDexPc()); |
David Brazdil | 05144f4 | 2015-04-16 15:18:00 +0100 | [diff] [blame] | 1826 | } else if (current->IsParameterValue()) { |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 1827 | if (kIsDebugBuild |
| 1828 | && invoke->IsInvokeStaticOrDirect() |
| 1829 | && invoke->AsInvokeStaticOrDirect()->IsStaticWithExplicitClinitCheck()) { |
| 1830 | // Ensure we do not use the last input of `invoke`, as it |
| 1831 | // contains a clinit check which is not an actual argument. |
| 1832 | size_t last_input_index = invoke->InputCount() - 1; |
| 1833 | DCHECK(parameter_index != last_input_index); |
| 1834 | } |
Calin Juravle | 214bbcd | 2015-10-20 14:54:07 +0100 | [diff] [blame] | 1835 | replacement = invoke->InputAt(parameter_index++); |
Nicolas Geoffray | 76b1e17 | 2015-05-27 17:18:33 +0100 | [diff] [blame] | 1836 | } else if (current->IsCurrentMethod()) { |
Calin Juravle | 214bbcd | 2015-10-20 14:54:07 +0100 | [diff] [blame] | 1837 | replacement = outer_graph->GetCurrentMethod(); |
David Brazdil | 05144f4 | 2015-04-16 15:18:00 +0100 | [diff] [blame] | 1838 | } else { |
| 1839 | DCHECK(current->IsGoto() || current->IsSuspendCheck()); |
| 1840 | entry_block_->RemoveInstruction(current); |
| 1841 | } |
Calin Juravle | 214bbcd | 2015-10-20 14:54:07 +0100 | [diff] [blame] | 1842 | if (replacement != nullptr) { |
| 1843 | current->ReplaceWith(replacement); |
| 1844 | // If the current is the return value then we need to update the latter. |
| 1845 | if (current == return_value) { |
| 1846 | DCHECK_EQ(entry_block_, return_value->GetBlock()); |
| 1847 | return_value = replacement; |
| 1848 | } |
| 1849 | } |
| 1850 | } |
| 1851 | |
| 1852 | if (return_value != nullptr) { |
| 1853 | invoke->ReplaceWith(return_value); |
David Brazdil | 05144f4 | 2015-04-16 15:18:00 +0100 | [diff] [blame] | 1854 | } |
| 1855 | |
Nicolas Geoffray | 7c5367b | 2014-12-17 10:13:46 +0000 | [diff] [blame] | 1856 | // Finally remove the invoke from the caller. |
| 1857 | invoke->GetBlock()->RemoveInstruction(invoke); |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 1858 | |
| 1859 | return return_value; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1860 | } |
| 1861 | |
Mingyao Yang | 3584bce | 2015-05-19 16:01:59 -0700 | [diff] [blame] | 1862 | /* |
| 1863 | * Loop will be transformed to: |
| 1864 | * old_pre_header |
| 1865 | * | |
| 1866 | * if_block |
| 1867 | * / \ |
| 1868 | * dummy_block deopt_block |
| 1869 | * \ / |
| 1870 | * new_pre_header |
| 1871 | * | |
| 1872 | * header |
| 1873 | */ |
| 1874 | void HGraph::TransformLoopHeaderForBCE(HBasicBlock* header) { |
| 1875 | DCHECK(header->IsLoopHeader()); |
| 1876 | HBasicBlock* pre_header = header->GetDominator(); |
| 1877 | |
| 1878 | // Need this to avoid critical edge. |
| 1879 | HBasicBlock* if_block = new (arena_) HBasicBlock(this, header->GetDexPc()); |
| 1880 | // Need this to avoid critical edge. |
| 1881 | HBasicBlock* dummy_block = new (arena_) HBasicBlock(this, header->GetDexPc()); |
| 1882 | HBasicBlock* deopt_block = new (arena_) HBasicBlock(this, header->GetDexPc()); |
| 1883 | HBasicBlock* new_pre_header = new (arena_) HBasicBlock(this, header->GetDexPc()); |
| 1884 | AddBlock(if_block); |
| 1885 | AddBlock(dummy_block); |
| 1886 | AddBlock(deopt_block); |
| 1887 | AddBlock(new_pre_header); |
| 1888 | |
| 1889 | header->ReplacePredecessor(pre_header, new_pre_header); |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1890 | pre_header->successors_.clear(); |
| 1891 | pre_header->dominated_blocks_.clear(); |
Mingyao Yang | 3584bce | 2015-05-19 16:01:59 -0700 | [diff] [blame] | 1892 | |
| 1893 | pre_header->AddSuccessor(if_block); |
| 1894 | if_block->AddSuccessor(dummy_block); // True successor |
| 1895 | if_block->AddSuccessor(deopt_block); // False successor |
| 1896 | dummy_block->AddSuccessor(new_pre_header); |
| 1897 | deopt_block->AddSuccessor(new_pre_header); |
| 1898 | |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1899 | pre_header->dominated_blocks_.push_back(if_block); |
Mingyao Yang | 3584bce | 2015-05-19 16:01:59 -0700 | [diff] [blame] | 1900 | if_block->SetDominator(pre_header); |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1901 | if_block->dominated_blocks_.push_back(dummy_block); |
Mingyao Yang | 3584bce | 2015-05-19 16:01:59 -0700 | [diff] [blame] | 1902 | dummy_block->SetDominator(if_block); |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1903 | if_block->dominated_blocks_.push_back(deopt_block); |
Mingyao Yang | 3584bce | 2015-05-19 16:01:59 -0700 | [diff] [blame] | 1904 | deopt_block->SetDominator(if_block); |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1905 | if_block->dominated_blocks_.push_back(new_pre_header); |
Mingyao Yang | 3584bce | 2015-05-19 16:01:59 -0700 | [diff] [blame] | 1906 | new_pre_header->SetDominator(if_block); |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1907 | new_pre_header->dominated_blocks_.push_back(header); |
Mingyao Yang | 3584bce | 2015-05-19 16:01:59 -0700 | [diff] [blame] | 1908 | header->SetDominator(new_pre_header); |
| 1909 | |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 1910 | size_t index_of_header = IndexOfElement(reverse_post_order_, header); |
Mingyao Yang | 3584bce | 2015-05-19 16:01:59 -0700 | [diff] [blame] | 1911 | MakeRoomFor(&reverse_post_order_, 4, index_of_header - 1); |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 1912 | reverse_post_order_[index_of_header++] = if_block; |
| 1913 | reverse_post_order_[index_of_header++] = dummy_block; |
| 1914 | reverse_post_order_[index_of_header++] = deopt_block; |
| 1915 | reverse_post_order_[index_of_header++] = new_pre_header; |
Mingyao Yang | 3584bce | 2015-05-19 16:01:59 -0700 | [diff] [blame] | 1916 | |
| 1917 | HLoopInformation* info = pre_header->GetLoopInformation(); |
| 1918 | if (info != nullptr) { |
| 1919 | if_block->SetLoopInformation(info); |
| 1920 | dummy_block->SetLoopInformation(info); |
| 1921 | deopt_block->SetLoopInformation(info); |
| 1922 | new_pre_header->SetLoopInformation(info); |
| 1923 | for (HLoopInformationOutwardIterator loop_it(*pre_header); |
| 1924 | !loop_it.Done(); |
| 1925 | loop_it.Advance()) { |
| 1926 | loop_it.Current()->Add(if_block); |
| 1927 | loop_it.Current()->Add(dummy_block); |
| 1928 | loop_it.Current()->Add(deopt_block); |
| 1929 | loop_it.Current()->Add(new_pre_header); |
| 1930 | } |
| 1931 | } |
| 1932 | } |
| 1933 | |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 1934 | void HInstruction::SetReferenceTypeInfo(ReferenceTypeInfo rti) { |
| 1935 | if (kIsDebugBuild) { |
| 1936 | DCHECK_EQ(GetType(), Primitive::kPrimNot); |
| 1937 | ScopedObjectAccess soa(Thread::Current()); |
| 1938 | DCHECK(rti.IsValid()) << "Invalid RTI for " << DebugName(); |
| 1939 | if (IsBoundType()) { |
| 1940 | // Having the test here spares us from making the method virtual just for |
| 1941 | // the sake of a DCHECK. |
| 1942 | ReferenceTypeInfo upper_bound_rti = AsBoundType()->GetUpperBound(); |
| 1943 | DCHECK(upper_bound_rti.IsSupertypeOf(rti)) |
| 1944 | << " upper_bound_rti: " << upper_bound_rti |
| 1945 | << " rti: " << rti; |
David Brazdil | baf89b8 | 2015-09-15 11:36:54 +0100 | [diff] [blame] | 1946 | DCHECK(!upper_bound_rti.GetTypeHandle()->CannotBeAssignedFromOtherTypes() || rti.IsExact()); |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 1947 | } |
| 1948 | } |
| 1949 | reference_type_info_ = rti; |
| 1950 | } |
| 1951 | |
| 1952 | ReferenceTypeInfo::ReferenceTypeInfo() : type_handle_(TypeHandle()), is_exact_(false) {} |
| 1953 | |
| 1954 | ReferenceTypeInfo::ReferenceTypeInfo(TypeHandle type_handle, bool is_exact) |
| 1955 | : type_handle_(type_handle), is_exact_(is_exact) { |
| 1956 | if (kIsDebugBuild) { |
| 1957 | ScopedObjectAccess soa(Thread::Current()); |
| 1958 | DCHECK(IsValidHandle(type_handle)); |
| 1959 | } |
| 1960 | } |
| 1961 | |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 1962 | std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs) { |
| 1963 | ScopedObjectAccess soa(Thread::Current()); |
| 1964 | os << "[" |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 1965 | << " is_valid=" << rhs.IsValid() |
| 1966 | << " type=" << (!rhs.IsValid() ? "?" : PrettyClass(rhs.GetTypeHandle().Get())) |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 1967 | << " is_exact=" << rhs.IsExact() |
| 1968 | << " ]"; |
| 1969 | return os; |
| 1970 | } |
| 1971 | |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 1972 | bool HInstruction::HasAnyEnvironmentUseBefore(HInstruction* other) { |
| 1973 | // For now, assume that instructions in different blocks may use the |
| 1974 | // environment. |
| 1975 | // TODO: Use the control flow to decide if this is true. |
| 1976 | if (GetBlock() != other->GetBlock()) { |
| 1977 | return true; |
| 1978 | } |
| 1979 | |
| 1980 | // We know that we are in the same block. Walk from 'this' to 'other', |
| 1981 | // checking to see if there is any instruction with an environment. |
| 1982 | HInstruction* current = this; |
| 1983 | for (; current != other && current != nullptr; current = current->GetNext()) { |
| 1984 | // This is a conservative check, as the instruction result may not be in |
| 1985 | // the referenced environment. |
| 1986 | if (current->HasEnvironment()) { |
| 1987 | return true; |
| 1988 | } |
| 1989 | } |
| 1990 | |
| 1991 | // We should have been called with 'this' before 'other' in the block. |
| 1992 | // Just confirm this. |
| 1993 | DCHECK(current != nullptr); |
| 1994 | return false; |
| 1995 | } |
| 1996 | |
Nicolas Geoffray | a83a54d | 2015-10-02 17:30:26 +0100 | [diff] [blame] | 1997 | void HInvoke::SetIntrinsic(Intrinsics intrinsic, |
| 1998 | IntrinsicNeedsEnvironmentOrCache needs_env_or_cache) { |
| 1999 | intrinsic_ = intrinsic; |
| 2000 | IntrinsicOptimizations opt(this); |
| 2001 | if (needs_env_or_cache == kNoEnvironmentOrCache) { |
| 2002 | opt.SetDoesNotNeedDexCache(); |
| 2003 | opt.SetDoesNotNeedEnvironment(); |
| 2004 | } |
| 2005 | } |
| 2006 | |
| 2007 | bool HInvoke::NeedsEnvironment() const { |
| 2008 | if (!IsIntrinsic()) { |
| 2009 | return true; |
| 2010 | } |
| 2011 | IntrinsicOptimizations opt(*this); |
| 2012 | return !opt.GetDoesNotNeedEnvironment(); |
| 2013 | } |
| 2014 | |
Vladimir Marko | dc151b2 | 2015-10-15 18:02:30 +0100 | [diff] [blame] | 2015 | bool HInvokeStaticOrDirect::NeedsDexCacheOfDeclaringClass() const { |
| 2016 | if (GetMethodLoadKind() != MethodLoadKind::kDexCacheViaMethod) { |
Nicolas Geoffray | a83a54d | 2015-10-02 17:30:26 +0100 | [diff] [blame] | 2017 | return false; |
| 2018 | } |
| 2019 | if (!IsIntrinsic()) { |
| 2020 | return true; |
| 2021 | } |
| 2022 | IntrinsicOptimizations opt(*this); |
| 2023 | return !opt.GetDoesNotNeedDexCache(); |
| 2024 | } |
| 2025 | |
Vladimir Marko | b554b5a | 2015-11-06 12:57:55 +0000 | [diff] [blame] | 2026 | void HInvokeStaticOrDirect::RemoveInputAt(size_t index) { |
| 2027 | RemoveAsUserOfInput(index); |
| 2028 | inputs_.erase(inputs_.begin() + index); |
| 2029 | // Update indexes in use nodes of inputs that have been pulled forward by the erase(). |
| 2030 | for (size_t i = index, e = InputCount(); i < e; ++i) { |
| 2031 | DCHECK_EQ(InputRecordAt(i).GetUseNode()->GetIndex(), i + 1u); |
| 2032 | InputRecordAt(i).GetUseNode()->SetIndex(i); |
| 2033 | } |
| 2034 | } |
| 2035 | |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 2036 | void HInstruction::RemoveEnvironmentUsers() { |
| 2037 | for (HUseIterator<HEnvironment*> use_it(GetEnvUses()); !use_it.Done(); use_it.Advance()) { |
| 2038 | HUseListNode<HEnvironment*>* user_node = use_it.Current(); |
| 2039 | HEnvironment* user = user_node->GetUser(); |
| 2040 | user->SetRawEnvAt(user_node->GetIndex(), nullptr); |
| 2041 | } |
| 2042 | env_uses_.Clear(); |
| 2043 | } |
| 2044 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 2045 | } // namespace art |