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