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