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