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