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