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