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