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