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