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