| Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | */ |
| 16 | |
| 17 | #include "load_store_elimination.h" |
| Aart Bik | 96fd51d | 2016-11-28 11:22:35 -0800 | [diff] [blame] | 18 | |
| Vladimir Marko | 009d166 | 2017-10-10 13:21:15 +0100 | [diff] [blame] | 19 | #include "base/array_ref.h" |
| 20 | #include "base/scoped_arena_allocator.h" |
| 21 | #include "base/scoped_arena_containers.h" |
| Aart Bik | 96fd51d | 2016-11-28 11:22:35 -0800 | [diff] [blame] | 22 | #include "escape.h" |
| Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 23 | #include "load_store_analysis.h" |
| Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 24 | #include "side_effects_analysis.h" |
| 25 | |
| Mingyao Yang | a354053 | 2018-01-25 12:17:28 -0800 | [diff] [blame] | 26 | /** |
| 27 | * The general algorithm of load-store elimination (LSE). |
| 28 | * Load-store analysis in the previous pass collects a list of heap locations |
| 29 | * and does alias analysis of those heap locations. |
| 30 | * LSE keeps track of a list of heap values corresponding to the heap |
| 31 | * locations. It visits basic blocks in reverse post order and for |
| 32 | * each basic block, visits instructions sequentially, and processes |
| 33 | * instructions as follows: |
| 34 | * - If the instruction is a load, and the heap location for that load has a |
| 35 | * valid heap value, the load can be eliminated. In order to maintain the |
| 36 | * validity of all heap locations during the optimization phase, the real |
| 37 | * elimination is delayed till the end of LSE. |
| 38 | * - If the instruction is a store, it updates the heap value for the heap |
| 39 | * location of the store with the store instruction. The real heap value |
| 40 | * can be fetched from the store instruction. Heap values are invalidated |
| 41 | * for heap locations that may alias with the store instruction's heap |
| 42 | * location. The store instruction can be eliminated unless the value stored |
| 43 | * is later needed e.g. by a load from the same/aliased heap location or |
| 44 | * the heap location persists at method return/deoptimization. |
| 45 | * The store instruction is also needed if it's not used to track the heap |
| 46 | * value anymore, e.g. when it fails to merge with the heap values from other |
| 47 | * predecessors. |
| 48 | * - A store that stores the same value as the heap value is eliminated. |
| 49 | * - The list of heap values are merged at basic block entry from the basic |
| 50 | * block's predecessors. The algorithm is single-pass, so loop side-effects is |
| 51 | * used as best effort to decide if a heap location is stored inside the loop. |
| 52 | * - A special type of objects called singletons are instantiated in the method |
| 53 | * and have a single name, i.e. no aliases. Singletons have exclusive heap |
| 54 | * locations since they have no aliases. Singletons are helpful in narrowing |
| 55 | * down the life span of a heap location such that they do not always |
| 56 | * need to participate in merging heap values. Allocation of a singleton |
| 57 | * can be eliminated if that singleton is not used and does not persist |
| 58 | * at method return/deoptimization. |
| 59 | * - For newly instantiated instances, their heap values are initialized to |
| 60 | * language defined default values. |
| 61 | * - Some instructions such as invokes are treated as loading and invalidating |
| 62 | * all the heap values, depending on the instruction's side effects. |
| 63 | * - Finalizable objects are considered as persisting at method |
| 64 | * return/deoptimization. |
| xueliang.zhong | d71f1dc | 2018-01-24 17:24:16 +0000 | [diff] [blame] | 65 | * - SIMD graphs (with VecLoad and VecStore instructions) are also handled. Any |
| 66 | * partial overlap access among ArrayGet/ArraySet/VecLoad/Store is seen as |
| 67 | * alias and no load/store is eliminated in such case. |
| Mingyao Yang | a354053 | 2018-01-25 12:17:28 -0800 | [diff] [blame] | 68 | * - Currently this LSE algorithm doesn't handle graph with try-catch, due to |
| 69 | * the special block merging structure. |
| 70 | */ |
| 71 | |
| Vladimir Marko | 0a51605 | 2019-10-14 13:00:44 +0000 | [diff] [blame] | 72 | namespace art { |
| Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 73 | |
| Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 74 | // An unknown heap value. Loads with such a value in the heap location cannot be eliminated. |
| Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 75 | // A heap location can be set to kUnknownHeapValue when: |
| 76 | // - initially set a value. |
| 77 | // - killed due to aliasing, merging, invocation, or loop side effects. |
| Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 78 | static HInstruction* const kUnknownHeapValue = |
| 79 | reinterpret_cast<HInstruction*>(static_cast<uintptr_t>(-1)); |
| Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 80 | |
| Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 81 | // Default heap value after an allocation. |
| Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 82 | // A heap location can be set to that value right after an allocation. |
| Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 83 | static HInstruction* const kDefaultHeapValue = |
| 84 | reinterpret_cast<HInstruction*>(static_cast<uintptr_t>(-2)); |
| 85 | |
| Mingyao Yang | c62b7ec | 2017-10-25 16:42:15 -0700 | [diff] [blame] | 86 | // Use HGraphDelegateVisitor for which all VisitInvokeXXX() delegate to VisitInvoke(). |
| 87 | class LSEVisitor : public HGraphDelegateVisitor { |
| Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 88 | public: |
| 89 | LSEVisitor(HGraph* graph, |
| 90 | const HeapLocationCollector& heap_locations_collector, |
| Igor Murashkin | 6ef4567 | 2017-08-08 13:59:55 -0700 | [diff] [blame] | 91 | const SideEffectsAnalysis& side_effects, |
| 92 | OptimizingCompilerStats* stats) |
| Mingyao Yang | c62b7ec | 2017-10-25 16:42:15 -0700 | [diff] [blame] | 93 | : HGraphDelegateVisitor(graph, stats), |
| Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 94 | heap_location_collector_(heap_locations_collector), |
| 95 | side_effects_(side_effects), |
| Vladimir Marko | 009d166 | 2017-10-10 13:21:15 +0100 | [diff] [blame] | 96 | allocator_(graph->GetArenaStack()), |
| Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 97 | heap_values_for_(graph->GetBlocks().size(), |
| Vladimir Marko | 009d166 | 2017-10-10 13:21:15 +0100 | [diff] [blame] | 98 | ScopedArenaVector<HInstruction*>(heap_locations_collector. |
| 99 | GetNumberOfHeapLocations(), |
| 100 | kUnknownHeapValue, |
| 101 | allocator_.Adapter(kArenaAllocLSE)), |
| 102 | allocator_.Adapter(kArenaAllocLSE)), |
| 103 | removed_loads_(allocator_.Adapter(kArenaAllocLSE)), |
| 104 | substitute_instructions_for_loads_(allocator_.Adapter(kArenaAllocLSE)), |
| 105 | possibly_removed_stores_(allocator_.Adapter(kArenaAllocLSE)), |
| Mingyao Yang | a354053 | 2018-01-25 12:17:28 -0800 | [diff] [blame] | 106 | singleton_new_instances_(allocator_.Adapter(kArenaAllocLSE)) { |
| Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 109 | void VisitBasicBlock(HBasicBlock* block) override { |
| Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 110 | // Populate the heap_values array for this block. |
| Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 111 | // TODO: try to reuse the heap_values array from one predecessor if possible. |
| 112 | if (block->IsLoopHeader()) { |
| Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 113 | HandleLoopSideEffects(block); |
| Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 114 | } else { |
| 115 | MergePredecessorValues(block); |
| 116 | } |
| 117 | HGraphVisitor::VisitBasicBlock(block); |
| 118 | } |
| 119 | |
| Vladimir Marko | 94539fd | 2017-11-15 17:52:46 +0000 | [diff] [blame] | 120 | HTypeConversion* AddTypeConversionIfNecessary(HInstruction* instruction, |
| 121 | HInstruction* value, |
| 122 | DataType::Type expected_type) { |
| 123 | HTypeConversion* type_conversion = nullptr; |
| 124 | // Should never add type conversion into boolean value. |
| 125 | if (expected_type != DataType::Type::kBool && |
| 126 | !DataType::IsTypeConversionImplicit(value->GetType(), expected_type)) { |
| 127 | type_conversion = new (GetGraph()->GetAllocator()) HTypeConversion( |
| 128 | expected_type, value, instruction->GetDexPc()); |
| 129 | instruction->GetBlock()->InsertInstructionBefore(type_conversion, instruction); |
| 130 | } |
| 131 | return type_conversion; |
| 132 | } |
| 133 | |
| Mingyao Yang | a354053 | 2018-01-25 12:17:28 -0800 | [diff] [blame] | 134 | // Find an instruction's substitute if it's a removed load. |
| Mingyao Yang | 206070c | 2017-11-29 23:01:58 -0800 | [diff] [blame] | 135 | // Return the same instruction if it should not be removed. |
| 136 | HInstruction* FindSubstitute(HInstruction* instruction) { |
| Mingyao Yang | a354053 | 2018-01-25 12:17:28 -0800 | [diff] [blame] | 137 | if (!IsLoad(instruction)) { |
| 138 | return instruction; |
| 139 | } |
| Mingyao Yang | 206070c | 2017-11-29 23:01:58 -0800 | [diff] [blame] | 140 | size_t size = removed_loads_.size(); |
| 141 | for (size_t i = 0; i < size; i++) { |
| 142 | if (removed_loads_[i] == instruction) { |
| Mingyao Yang | a354053 | 2018-01-25 12:17:28 -0800 | [diff] [blame] | 143 | HInstruction* substitute = substitute_instructions_for_loads_[i]; |
| 144 | // The substitute list is a flat hierarchy. |
| 145 | DCHECK_EQ(FindSubstitute(substitute), substitute); |
| 146 | return substitute; |
| Mingyao Yang | 206070c | 2017-11-29 23:01:58 -0800 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | return instruction; |
| 150 | } |
| 151 | |
| Vladimir Marko | 94539fd | 2017-11-15 17:52:46 +0000 | [diff] [blame] | 152 | void AddRemovedLoad(HInstruction* load, HInstruction* heap_value) { |
| Mingyao Yang | a354053 | 2018-01-25 12:17:28 -0800 | [diff] [blame] | 153 | DCHECK(IsLoad(load)); |
| Vladimir Marko | 94539fd | 2017-11-15 17:52:46 +0000 | [diff] [blame] | 154 | DCHECK_EQ(FindSubstitute(heap_value), heap_value) << |
| 155 | "Unexpected heap_value that has a substitute " << heap_value->DebugName(); |
| 156 | removed_loads_.push_back(load); |
| 157 | substitute_instructions_for_loads_.push_back(heap_value); |
| 158 | } |
| 159 | |
| 160 | // Scan the list of removed loads to see if we can reuse `type_conversion`, if |
| 161 | // the other removed load has the same substitute and type and is dominated |
| Aart Bik | b765a3f | 2018-05-10 14:47:48 -0700 | [diff] [blame] | 162 | // by `type_conversion`. |
| Vladimir Marko | 94539fd | 2017-11-15 17:52:46 +0000 | [diff] [blame] | 163 | void TryToReuseTypeConversion(HInstruction* type_conversion, size_t index) { |
| 164 | size_t size = removed_loads_.size(); |
| 165 | HInstruction* load = removed_loads_[index]; |
| 166 | HInstruction* substitute = substitute_instructions_for_loads_[index]; |
| 167 | for (size_t j = index + 1; j < size; j++) { |
| 168 | HInstruction* load2 = removed_loads_[j]; |
| 169 | HInstruction* substitute2 = substitute_instructions_for_loads_[j]; |
| 170 | if (load2 == nullptr) { |
| 171 | DCHECK(substitute2->IsTypeConversion()); |
| 172 | continue; |
| 173 | } |
| xueliang.zhong | d71f1dc | 2018-01-24 17:24:16 +0000 | [diff] [blame] | 174 | DCHECK(IsLoad(load2)); |
| Vladimir Marko | 94539fd | 2017-11-15 17:52:46 +0000 | [diff] [blame] | 175 | DCHECK(substitute2 != nullptr); |
| 176 | if (substitute2 == substitute && |
| 177 | load2->GetType() == load->GetType() && |
| 178 | type_conversion->GetBlock()->Dominates(load2->GetBlock()) && |
| 179 | // Don't share across irreducible loop headers. |
| 180 | // TODO: can be more fine-grained than this by testing each dominator. |
| 181 | (load2->GetBlock() == type_conversion->GetBlock() || |
| 182 | !GetGraph()->HasIrreducibleLoops())) { |
| 183 | // The removed_loads_ are added in reverse post order. |
| 184 | DCHECK(type_conversion->StrictlyDominates(load2)); |
| 185 | load2->ReplaceWith(type_conversion); |
| 186 | load2->GetBlock()->RemoveInstruction(load2); |
| 187 | removed_loads_[j] = nullptr; |
| 188 | substitute_instructions_for_loads_[j] = type_conversion; |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | |
| Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 193 | // Remove recorded instructions that should be eliminated. |
| 194 | void RemoveInstructions() { |
| Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 195 | size_t size = removed_loads_.size(); |
| 196 | DCHECK_EQ(size, substitute_instructions_for_loads_.size()); |
| Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 197 | for (size_t i = 0; i < size; i++) { |
| Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 198 | HInstruction* load = removed_loads_[i]; |
| Vladimir Marko | 94539fd | 2017-11-15 17:52:46 +0000 | [diff] [blame] | 199 | if (load == nullptr) { |
| 200 | // The load has been handled in the scan for type conversion below. |
| 201 | DCHECK(substitute_instructions_for_loads_[i]->IsTypeConversion()); |
| 202 | continue; |
| 203 | } |
| xueliang.zhong | d71f1dc | 2018-01-24 17:24:16 +0000 | [diff] [blame] | 204 | DCHECK(IsLoad(load)); |
| Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 205 | HInstruction* substitute = substitute_instructions_for_loads_[i]; |
| 206 | DCHECK(substitute != nullptr); |
| Mingyao Yang | 206070c | 2017-11-29 23:01:58 -0800 | [diff] [blame] | 207 | // We proactively retrieve the substitute for a removed load, so |
| 208 | // a load that has a substitute should not be observed as a heap |
| 209 | // location value. |
| 210 | DCHECK_EQ(FindSubstitute(substitute), substitute); |
| Vladimir Marko | 94539fd | 2017-11-15 17:52:46 +0000 | [diff] [blame] | 211 | |
| 212 | // The load expects to load the heap value as type load->GetType(). |
| 213 | // However the tracked heap value may not be of that type. An explicit |
| 214 | // type conversion may be needed. |
| 215 | // There are actually three types involved here: |
| 216 | // (1) tracked heap value's type (type A) |
| 217 | // (2) heap location (field or element)'s type (type B) |
| 218 | // (3) load's type (type C) |
| 219 | // We guarantee that type A stored as type B and then fetched out as |
| 220 | // type C is the same as casting from type A to type C directly, since |
| 221 | // type B and type C will have the same size which is guarenteed in |
| xueliang.zhong | d71f1dc | 2018-01-24 17:24:16 +0000 | [diff] [blame] | 222 | // HInstanceFieldGet/HStaticFieldGet/HArrayGet/HVecLoad's SetType(). |
| Vladimir Marko | 94539fd | 2017-11-15 17:52:46 +0000 | [diff] [blame] | 223 | // So we only need one type conversion from type A to type C. |
| 224 | HTypeConversion* type_conversion = AddTypeConversionIfNecessary( |
| 225 | load, substitute, load->GetType()); |
| 226 | if (type_conversion != nullptr) { |
| 227 | TryToReuseTypeConversion(type_conversion, i); |
| 228 | load->ReplaceWith(type_conversion); |
| 229 | substitute_instructions_for_loads_[i] = type_conversion; |
| 230 | } else { |
| 231 | load->ReplaceWith(substitute); |
| 232 | } |
| Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 233 | load->GetBlock()->RemoveInstruction(load); |
| Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 234 | } |
| Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 235 | |
| 236 | // At this point, stores in possibly_removed_stores_ can be safely removed. |
| Mingyao Yang | 8697490 | 2017-03-01 14:03:51 -0800 | [diff] [blame] | 237 | for (HInstruction* store : possibly_removed_stores_) { |
| xueliang.zhong | d71f1dc | 2018-01-24 17:24:16 +0000 | [diff] [blame] | 238 | DCHECK(IsStore(store)); |
| Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 239 | store->GetBlock()->RemoveInstruction(store); |
| 240 | } |
| 241 | |
| Igor Murashkin | d01745e | 2017-04-05 16:40:31 -0700 | [diff] [blame] | 242 | // Eliminate singleton-classified instructions: |
| 243 | // * - Constructor fences (they never escape this thread). |
| 244 | // * - Allocations (if they are unused). |
| Mingyao Yang | 8697490 | 2017-03-01 14:03:51 -0800 | [diff] [blame] | 245 | for (HInstruction* new_instance : singleton_new_instances_) { |
| Igor Murashkin | 6ef4567 | 2017-08-08 13:59:55 -0700 | [diff] [blame] | 246 | size_t removed = HConstructorFence::RemoveConstructorFences(new_instance); |
| 247 | MaybeRecordStat(stats_, |
| 248 | MethodCompilationStat::kConstructorFenceRemovedLSE, |
| 249 | removed); |
| Igor Murashkin | d01745e | 2017-04-05 16:40:31 -0700 | [diff] [blame] | 250 | |
| Mingyao Yang | 062157f | 2016-03-02 10:15:36 -0800 | [diff] [blame] | 251 | if (!new_instance->HasNonEnvironmentUses()) { |
| 252 | new_instance->RemoveEnvironmentUsers(); |
| 253 | new_instance->GetBlock()->RemoveInstruction(new_instance); |
| 254 | } |
| 255 | } |
| Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | private: |
| xueliang.zhong | d71f1dc | 2018-01-24 17:24:16 +0000 | [diff] [blame] | 259 | static bool IsLoad(const HInstruction* instruction) { |
| Mingyao Yang | a354053 | 2018-01-25 12:17:28 -0800 | [diff] [blame] | 260 | if (instruction == kUnknownHeapValue || instruction == kDefaultHeapValue) { |
| 261 | return false; |
| 262 | } |
| 263 | // Unresolved load is not treated as a load. |
| 264 | return instruction->IsInstanceFieldGet() || |
| 265 | instruction->IsStaticFieldGet() || |
| xueliang.zhong | d71f1dc | 2018-01-24 17:24:16 +0000 | [diff] [blame] | 266 | instruction->IsVecLoad() || |
| Mingyao Yang | a354053 | 2018-01-25 12:17:28 -0800 | [diff] [blame] | 267 | instruction->IsArrayGet(); |
| 268 | } |
| 269 | |
| xueliang.zhong | d71f1dc | 2018-01-24 17:24:16 +0000 | [diff] [blame] | 270 | static bool IsStore(const HInstruction* instruction) { |
| Mingyao Yang | a354053 | 2018-01-25 12:17:28 -0800 | [diff] [blame] | 271 | if (instruction == kUnknownHeapValue || instruction == kDefaultHeapValue) { |
| 272 | return false; |
| 273 | } |
| 274 | // Unresolved store is not treated as a store. |
| 275 | return instruction->IsInstanceFieldSet() || |
| 276 | instruction->IsArraySet() || |
| xueliang.zhong | d71f1dc | 2018-01-24 17:24:16 +0000 | [diff] [blame] | 277 | instruction->IsVecStore() || |
| Mingyao Yang | a354053 | 2018-01-25 12:17:28 -0800 | [diff] [blame] | 278 | instruction->IsStaticFieldSet(); |
| 279 | } |
| 280 | |
| xueliang.zhong | d71f1dc | 2018-01-24 17:24:16 +0000 | [diff] [blame] | 281 | // Check if it is allowed to use default values for the specified load. |
| 282 | static bool IsDefaultAllowedForLoad(const HInstruction* load) { |
| 283 | DCHECK(IsLoad(load)); |
| 284 | // Using defaults for VecLoads requires to create additional vector operations. |
| 285 | // As there are some issues with scheduling vector operations it is better to avoid creating |
| 286 | // them. |
| 287 | return !load->IsVecOperation(); |
| 288 | } |
| 289 | |
| Mingyao Yang | a354053 | 2018-01-25 12:17:28 -0800 | [diff] [blame] | 290 | // Returns the real heap value by finding its substitute or by "peeling" |
| 291 | // a store instruction. |
| 292 | HInstruction* GetRealHeapValue(HInstruction* heap_value) { |
| 293 | if (IsLoad(heap_value)) { |
| 294 | return FindSubstitute(heap_value); |
| 295 | } |
| 296 | if (!IsStore(heap_value)) { |
| 297 | return heap_value; |
| 298 | } |
| 299 | |
| 300 | // We keep track of store instructions as the heap values which might be |
| 301 | // eliminated if the stores are later found not necessary. The real stored |
| 302 | // value needs to be fetched from the store instruction. |
| 303 | if (heap_value->IsInstanceFieldSet()) { |
| 304 | heap_value = heap_value->AsInstanceFieldSet()->GetValue(); |
| 305 | } else if (heap_value->IsStaticFieldSet()) { |
| 306 | heap_value = heap_value->AsStaticFieldSet()->GetValue(); |
| xueliang.zhong | d71f1dc | 2018-01-24 17:24:16 +0000 | [diff] [blame] | 307 | } else if (heap_value->IsVecStore()) { |
| 308 | heap_value = heap_value->AsVecStore()->GetValue(); |
| Mingyao Yang | a354053 | 2018-01-25 12:17:28 -0800 | [diff] [blame] | 309 | } else { |
| 310 | DCHECK(heap_value->IsArraySet()); |
| 311 | heap_value = heap_value->AsArraySet()->GetValue(); |
| 312 | } |
| 313 | // heap_value may already be a removed load. |
| 314 | return FindSubstitute(heap_value); |
| 315 | } |
| 316 | |
| 317 | // If heap_value is a store, need to keep the store. |
| 318 | // This is necessary if a heap value is killed or replaced by another value, |
| 319 | // so that the store is no longer used to track heap value. |
| Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 320 | void KeepIfIsStore(HInstruction* heap_value) { |
| Mingyao Yang | a354053 | 2018-01-25 12:17:28 -0800 | [diff] [blame] | 321 | if (!IsStore(heap_value)) { |
| Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 322 | return; |
| 323 | } |
| 324 | auto idx = std::find(possibly_removed_stores_.begin(), |
| 325 | possibly_removed_stores_.end(), heap_value); |
| 326 | if (idx != possibly_removed_stores_.end()) { |
| 327 | // Make sure the store is kept. |
| 328 | possibly_removed_stores_.erase(idx); |
| 329 | } |
| 330 | } |
| 331 | |
| Mingyao Yang | a354053 | 2018-01-25 12:17:28 -0800 | [diff] [blame] | 332 | // If a heap location X may alias with heap location at `loc_index` |
| 333 | // and heap_values of that heap location X holds a store, keep that store. |
| 334 | // It's needed for a dependent load that's not eliminated since any store |
| 335 | // that may put value into the load's heap location needs to be kept. |
| 336 | void KeepStoresIfAliasedToLocation(ScopedArenaVector<HInstruction*>& heap_values, |
| 337 | size_t loc_index) { |
| 338 | for (size_t i = 0; i < heap_values.size(); i++) { |
| 339 | if ((i == loc_index) || heap_location_collector_.MayAlias(i, loc_index)) { |
| 340 | KeepIfIsStore(heap_values[i]); |
| 341 | } |
| 342 | } |
| 343 | } |
| 344 | |
| Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 345 | void HandleLoopSideEffects(HBasicBlock* block) { |
| 346 | DCHECK(block->IsLoopHeader()); |
| 347 | int block_id = block->GetBlockId(); |
| Vladimir Marko | 009d166 | 2017-10-10 13:21:15 +0100 | [diff] [blame] | 348 | ScopedArenaVector<HInstruction*>& heap_values = heap_values_for_[block_id]; |
| Mingyao Yang | a354053 | 2018-01-25 12:17:28 -0800 | [diff] [blame] | 349 | HBasicBlock* pre_header = block->GetLoopInformation()->GetPreHeader(); |
| 350 | ScopedArenaVector<HInstruction*>& pre_header_heap_values = |
| 351 | heap_values_for_[pre_header->GetBlockId()]; |
| Nicolas Geoffray | 15bd228 | 2016-01-05 15:55:41 +0000 | [diff] [blame] | 352 | |
| Mingyao Yang | a354053 | 2018-01-25 12:17:28 -0800 | [diff] [blame] | 353 | // Don't eliminate loads in irreducible loops. |
| 354 | // Also keep the stores before the loop. |
| Nicolas Geoffray | 15bd228 | 2016-01-05 15:55:41 +0000 | [diff] [blame] | 355 | if (block->GetLoopInformation()->IsIrreducible()) { |
| 356 | if (kIsDebugBuild) { |
| 357 | for (size_t i = 0; i < heap_values.size(); i++) { |
| 358 | DCHECK_EQ(heap_values[i], kUnknownHeapValue); |
| 359 | } |
| 360 | } |
| Mingyao Yang | a354053 | 2018-01-25 12:17:28 -0800 | [diff] [blame] | 361 | for (size_t i = 0; i < heap_values.size(); i++) { |
| 362 | KeepIfIsStore(pre_header_heap_values[i]); |
| 363 | } |
| Nicolas Geoffray | 15bd228 | 2016-01-05 15:55:41 +0000 | [diff] [blame] | 364 | return; |
| 365 | } |
| 366 | |
| Mingyao Yang | 803cbb9 | 2015-12-01 12:24:36 -0800 | [diff] [blame] | 367 | // Inherit the values from pre-header. |
| 368 | for (size_t i = 0; i < heap_values.size(); i++) { |
| 369 | heap_values[i] = pre_header_heap_values[i]; |
| 370 | } |
| 371 | |
| Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 372 | // We do a single pass in reverse post order. For loops, use the side effects as a hint |
| 373 | // to see if the heap values should be killed. |
| 374 | if (side_effects_.GetLoopEffects(block).DoesAnyWrite()) { |
| Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 375 | for (size_t i = 0; i < heap_values.size(); i++) { |
| Mingyao Yang | 803cbb9 | 2015-12-01 12:24:36 -0800 | [diff] [blame] | 376 | HeapLocation* location = heap_location_collector_.GetHeapLocation(i); |
| 377 | ReferenceInfo* ref_info = location->GetReferenceInfo(); |
| Mingyao Yang | a354053 | 2018-01-25 12:17:28 -0800 | [diff] [blame] | 378 | if (ref_info->IsSingleton() && !location->IsValueKilledByLoopSideEffects()) { |
| 379 | // A singleton's field that's not stored into inside a loop is |
| Mingyao Yang | eb2d2d346e | 2017-03-02 13:26:17 -0800 | [diff] [blame] | 380 | // invariant throughout the loop. Nothing to do. |
| Mingyao Yang | eb2d2d346e | 2017-03-02 13:26:17 -0800 | [diff] [blame] | 381 | } else { |
| Mingyao Yang | a354053 | 2018-01-25 12:17:28 -0800 | [diff] [blame] | 382 | // heap value is killed by loop side effects. |
| Mingyao Yang | 803cbb9 | 2015-12-01 12:24:36 -0800 | [diff] [blame] | 383 | KeepIfIsStore(pre_header_heap_values[i]); |
| 384 | heap_values[i] = kUnknownHeapValue; |
| Mingyao Yang | 803cbb9 | 2015-12-01 12:24:36 -0800 | [diff] [blame] | 385 | } |
| Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 386 | } |
| Mingyao Yang | a354053 | 2018-01-25 12:17:28 -0800 | [diff] [blame] | 387 | } else { |
| 388 | // The loop doesn't kill any value. |
| Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 389 | } |
| 390 | } |
| 391 | |
| Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 392 | void MergePredecessorValues(HBasicBlock* block) { |
| Vladimir Marko | 009d166 | 2017-10-10 13:21:15 +0100 | [diff] [blame] | 393 | ArrayRef<HBasicBlock* const> predecessors(block->GetPredecessors()); |
| Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 394 | if (predecessors.size() == 0) { |
| 395 | return; |
| 396 | } |
| Mingyao Yang | 46721ef | 2017-10-05 14:45:17 -0700 | [diff] [blame] | 397 | if (block->IsExitBlock()) { |
| 398 | // Exit block doesn't really merge values since the control flow ends in |
| 399 | // its predecessors. Each predecessor needs to make sure stores are kept |
| 400 | // if necessary. |
| 401 | return; |
| 402 | } |
| Mingyao Yang | 58d9bfc | 2016-11-01 13:31:58 -0700 | [diff] [blame] | 403 | |
| Vladimir Marko | 009d166 | 2017-10-10 13:21:15 +0100 | [diff] [blame] | 404 | ScopedArenaVector<HInstruction*>& heap_values = heap_values_for_[block->GetBlockId()]; |
| Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 405 | for (size_t i = 0; i < heap_values.size(); i++) { |
| Mingyao Yang | 58d9bfc | 2016-11-01 13:31:58 -0700 | [diff] [blame] | 406 | HInstruction* merged_value = nullptr; |
| Mingyao Yang | a354053 | 2018-01-25 12:17:28 -0800 | [diff] [blame] | 407 | // If we can merge the store itself from the predecessors, we keep |
| 408 | // the store as the heap value as long as possible. In case we cannot |
| 409 | // merge the store, we try to merge the values of the stores. |
| 410 | HInstruction* merged_store_value = nullptr; |
| Mingyao Yang | 58d9bfc | 2016-11-01 13:31:58 -0700 | [diff] [blame] | 411 | // Whether merged_value is a result that's merged from all predecessors. |
| 412 | bool from_all_predecessors = true; |
| 413 | ReferenceInfo* ref_info = heap_location_collector_.GetHeapLocation(i)->GetReferenceInfo(); |
| Mingyao Yang | a354053 | 2018-01-25 12:17:28 -0800 | [diff] [blame] | 414 | HInstruction* ref = ref_info->GetReference(); |
| Mingyao Yang | 58d9bfc | 2016-11-01 13:31:58 -0700 | [diff] [blame] | 415 | HInstruction* singleton_ref = nullptr; |
| Mingyao Yang | eb2d2d346e | 2017-03-02 13:26:17 -0800 | [diff] [blame] | 416 | if (ref_info->IsSingleton()) { |
| Mingyao Yang | a354053 | 2018-01-25 12:17:28 -0800 | [diff] [blame] | 417 | // We do more analysis based on singleton's liveness when merging |
| 418 | // heap values for such cases. |
| 419 | singleton_ref = ref; |
| Mingyao Yang | 58d9bfc | 2016-11-01 13:31:58 -0700 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | for (HBasicBlock* predecessor : predecessors) { |
| 423 | HInstruction* pred_value = heap_values_for_[predecessor->GetBlockId()][i]; |
| Mingyao Yang | a354053 | 2018-01-25 12:17:28 -0800 | [diff] [blame] | 424 | if (!IsStore(pred_value)) { |
| 425 | pred_value = FindSubstitute(pred_value); |
| 426 | } |
| 427 | DCHECK(pred_value != nullptr); |
| 428 | HInstruction* pred_store_value = GetRealHeapValue(pred_value); |
| Mingyao Yang | 58d9bfc | 2016-11-01 13:31:58 -0700 | [diff] [blame] | 429 | if ((singleton_ref != nullptr) && |
| 430 | !singleton_ref->GetBlock()->Dominates(predecessor)) { |
| Mingyao Yang | a354053 | 2018-01-25 12:17:28 -0800 | [diff] [blame] | 431 | // singleton_ref is not live in this predecessor. No need to merge |
| 432 | // since singleton_ref is not live at the beginning of this block. |
| Mingyao Yang | 58d9bfc | 2016-11-01 13:31:58 -0700 | [diff] [blame] | 433 | DCHECK_EQ(pred_value, kUnknownHeapValue); |
| 434 | from_all_predecessors = false; |
| Mingyao Yang | a354053 | 2018-01-25 12:17:28 -0800 | [diff] [blame] | 435 | break; |
| Mingyao Yang | 58d9bfc | 2016-11-01 13:31:58 -0700 | [diff] [blame] | 436 | } |
| 437 | if (merged_value == nullptr) { |
| 438 | // First seen heap value. |
| Mingyao Yang | a354053 | 2018-01-25 12:17:28 -0800 | [diff] [blame] | 439 | DCHECK(pred_value != nullptr); |
| Mingyao Yang | 58d9bfc | 2016-11-01 13:31:58 -0700 | [diff] [blame] | 440 | merged_value = pred_value; |
| 441 | } else if (pred_value != merged_value) { |
| 442 | // There are conflicting values. |
| 443 | merged_value = kUnknownHeapValue; |
| Mingyao Yang | a354053 | 2018-01-25 12:17:28 -0800 | [diff] [blame] | 444 | // We may still be able to merge store values. |
| 445 | } |
| 446 | |
| 447 | // Conflicting stores may be storing the same value. We do another merge |
| 448 | // of real stored values. |
| 449 | if (merged_store_value == nullptr) { |
| 450 | // First seen store value. |
| 451 | DCHECK(pred_store_value != nullptr); |
| 452 | merged_store_value = pred_store_value; |
| 453 | } else if (pred_store_value != merged_store_value) { |
| 454 | // There are conflicting store values. |
| 455 | merged_store_value = kUnknownHeapValue; |
| 456 | // There must be conflicting stores also. |
| 457 | DCHECK_EQ(merged_value, kUnknownHeapValue); |
| 458 | // No need to merge anymore. |
| Mingyao Yang | 58d9bfc | 2016-11-01 13:31:58 -0700 | [diff] [blame] | 459 | break; |
| Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 460 | } |
| 461 | } |
| Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 462 | |
| Mingyao Yang | a354053 | 2018-01-25 12:17:28 -0800 | [diff] [blame] | 463 | if (merged_value == nullptr) { |
| 464 | DCHECK(!from_all_predecessors); |
| 465 | DCHECK(singleton_ref != nullptr); |
| 466 | } |
| 467 | if (from_all_predecessors) { |
| 468 | if (ref_info->IsSingletonAndRemovable() && |
| Aart Bik | 4dc09e7 | 2018-05-11 14:40:31 -0700 | [diff] [blame] | 469 | (block->IsSingleReturnOrReturnVoidAllowingPhis() || |
| 470 | (block->EndsWithReturn() && (merged_value != kUnknownHeapValue || |
| 471 | merged_store_value != kUnknownHeapValue)))) { |
| 472 | // Values in the singleton are not needed anymore: |
| 473 | // (1) if this block consists of a sole return, or |
| 474 | // (2) if this block returns and a usable merged value is obtained |
| 475 | // (loads prior to the return will always use that value). |
| Mingyao Yang | a354053 | 2018-01-25 12:17:28 -0800 | [diff] [blame] | 476 | } else if (!IsStore(merged_value)) { |
| 477 | // We don't track merged value as a store anymore. We have to |
| 478 | // hold the stores in predecessors live here. |
| Mingyao Yang | 46721ef | 2017-10-05 14:45:17 -0700 | [diff] [blame] | 479 | for (HBasicBlock* predecessor : predecessors) { |
| 480 | ScopedArenaVector<HInstruction*>& pred_values = |
| 481 | heap_values_for_[predecessor->GetBlockId()]; |
| 482 | KeepIfIsStore(pred_values[i]); |
| 483 | } |
| Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 484 | } |
| Mingyao Yang | 46721ef | 2017-10-05 14:45:17 -0700 | [diff] [blame] | 485 | } else { |
| Mingyao Yang | a354053 | 2018-01-25 12:17:28 -0800 | [diff] [blame] | 486 | DCHECK(singleton_ref != nullptr); |
| 487 | // singleton_ref is non-existing at the beginning of the block. There is |
| 488 | // no need to keep the stores. |
| Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 489 | } |
| Mingyao Yang | 58d9bfc | 2016-11-01 13:31:58 -0700 | [diff] [blame] | 490 | |
| Mingyao Yang | a354053 | 2018-01-25 12:17:28 -0800 | [diff] [blame] | 491 | if (!from_all_predecessors) { |
| Mingyao Yang | 58d9bfc | 2016-11-01 13:31:58 -0700 | [diff] [blame] | 492 | DCHECK(singleton_ref != nullptr); |
| 493 | DCHECK((singleton_ref->GetBlock() == block) || |
| Mingyao Yang | a354053 | 2018-01-25 12:17:28 -0800 | [diff] [blame] | 494 | !singleton_ref->GetBlock()->Dominates(block)) |
| 495 | << "method: " << GetGraph()->GetMethodName(); |
| Mingyao Yang | 58d9bfc | 2016-11-01 13:31:58 -0700 | [diff] [blame] | 496 | // singleton_ref is not defined before block or defined only in some of its |
| 497 | // predecessors, so block doesn't really have the location at its entry. |
| 498 | heap_values[i] = kUnknownHeapValue; |
| Mingyao Yang | a354053 | 2018-01-25 12:17:28 -0800 | [diff] [blame] | 499 | } else if (predecessors.size() == 1) { |
| 500 | // Inherit heap value from the single predecessor. |
| 501 | DCHECK_EQ(heap_values_for_[predecessors[0]->GetBlockId()][i], merged_value); |
| Nicolas Geoffray | e97949e | 2018-01-25 10:42:01 +0000 | [diff] [blame] | 502 | heap_values[i] = merged_value; |
| Mingyao Yang | a354053 | 2018-01-25 12:17:28 -0800 | [diff] [blame] | 503 | } else { |
| 504 | DCHECK(merged_value == kUnknownHeapValue || |
| 505 | merged_value == kDefaultHeapValue || |
| 506 | merged_value->GetBlock()->Dominates(block)); |
| 507 | if (merged_value != kUnknownHeapValue) { |
| 508 | heap_values[i] = merged_value; |
| 509 | } else { |
| 510 | // Stores in different predecessors may be storing the same value. |
| 511 | heap_values[i] = merged_store_value; |
| 512 | } |
| Mingyao Yang | 58d9bfc | 2016-11-01 13:31:58 -0700 | [diff] [blame] | 513 | } |
| Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 514 | } |
| 515 | } |
| 516 | |
| 517 | // `instruction` is being removed. Try to see if the null check on it |
| 518 | // can be removed. This can happen if the same value is set in two branches |
| 519 | // but not in dominators. Such as: |
| 520 | // int[] a = foo(); |
| 521 | // if () { |
| 522 | // a[0] = 2; |
| 523 | // } else { |
| 524 | // a[0] = 2; |
| 525 | // } |
| 526 | // // a[0] can now be replaced with constant 2, and the null check on it can be removed. |
| 527 | void TryRemovingNullCheck(HInstruction* instruction) { |
| 528 | HInstruction* prev = instruction->GetPrevious(); |
| 529 | if ((prev != nullptr) && prev->IsNullCheck() && (prev == instruction->InputAt(0))) { |
| 530 | // Previous instruction is a null check for this instruction. Remove the null check. |
| 531 | prev->ReplaceWith(prev->InputAt(0)); |
| 532 | prev->GetBlock()->RemoveInstruction(prev); |
| 533 | } |
| 534 | } |
| 535 | |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 536 | HInstruction* GetDefaultValue(DataType::Type type) { |
| Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 537 | switch (type) { |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 538 | case DataType::Type::kReference: |
| Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 539 | return GetGraph()->GetNullConstant(); |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 540 | case DataType::Type::kBool: |
| Vladimir Marko | d5d2f2c | 2017-09-26 12:37:26 +0100 | [diff] [blame] | 541 | case DataType::Type::kUint8: |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 542 | case DataType::Type::kInt8: |
| 543 | case DataType::Type::kUint16: |
| 544 | case DataType::Type::kInt16: |
| 545 | case DataType::Type::kInt32: |
| Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 546 | return GetGraph()->GetIntConstant(0); |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 547 | case DataType::Type::kInt64: |
| Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 548 | return GetGraph()->GetLongConstant(0); |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 549 | case DataType::Type::kFloat32: |
| Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 550 | return GetGraph()->GetFloatConstant(0); |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 551 | case DataType::Type::kFloat64: |
| Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 552 | return GetGraph()->GetDoubleConstant(0); |
| 553 | default: |
| 554 | UNREACHABLE(); |
| 555 | } |
| 556 | } |
| 557 | |
| Aart Bik | b765a3f | 2018-05-10 14:47:48 -0700 | [diff] [blame] | 558 | void VisitGetLocation(HInstruction* instruction, size_t idx) { |
| Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 559 | DCHECK_NE(idx, HeapLocationCollector::kHeapLocationNotFound); |
| Vladimir Marko | 009d166 | 2017-10-10 13:21:15 +0100 | [diff] [blame] | 560 | ScopedArenaVector<HInstruction*>& heap_values = |
| Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 561 | heap_values_for_[instruction->GetBlock()->GetBlockId()]; |
| 562 | HInstruction* heap_value = heap_values[idx]; |
| 563 | if (heap_value == kDefaultHeapValue) { |
| xueliang.zhong | d71f1dc | 2018-01-24 17:24:16 +0000 | [diff] [blame] | 564 | if (IsDefaultAllowedForLoad(instruction)) { |
| 565 | HInstruction* constant = GetDefaultValue(instruction->GetType()); |
| 566 | AddRemovedLoad(instruction, constant); |
| 567 | heap_values[idx] = constant; |
| 568 | return; |
| 569 | } else { |
| 570 | heap_values[idx] = kUnknownHeapValue; |
| 571 | heap_value = kUnknownHeapValue; |
| 572 | } |
| Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 573 | } |
| Mingyao Yang | a354053 | 2018-01-25 12:17:28 -0800 | [diff] [blame] | 574 | heap_value = GetRealHeapValue(heap_value); |
| David Brazdil | 15693bf | 2015-12-16 10:30:45 +0000 | [diff] [blame] | 575 | if (heap_value == kUnknownHeapValue) { |
| 576 | // Load isn't eliminated. Put the load as the value into the HeapLocation. |
| 577 | // This acts like GVN but with better aliasing analysis. |
| 578 | heap_values[idx] = instruction; |
| Mingyao Yang | a354053 | 2018-01-25 12:17:28 -0800 | [diff] [blame] | 579 | KeepStoresIfAliasedToLocation(heap_values, idx); |
| David Brazdil | 15693bf | 2015-12-16 10:30:45 +0000 | [diff] [blame] | 580 | } else { |
| Aart Bik | b765a3f | 2018-05-10 14:47:48 -0700 | [diff] [blame] | 581 | // Load is eliminated. |
| Vladimir Marko | 94539fd | 2017-11-15 17:52:46 +0000 | [diff] [blame] | 582 | AddRemovedLoad(instruction, heap_value); |
| Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 583 | TryRemovingNullCheck(instruction); |
| Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 584 | } |
| 585 | } |
| 586 | |
| 587 | bool Equal(HInstruction* heap_value, HInstruction* value) { |
| Mingyao Yang | a354053 | 2018-01-25 12:17:28 -0800 | [diff] [blame] | 588 | DCHECK(!IsStore(value)) << value->DebugName(); |
| 589 | if (heap_value == kUnknownHeapValue) { |
| 590 | // Don't compare kUnknownHeapValue with other values. |
| 591 | return false; |
| 592 | } |
| Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 593 | if (heap_value == value) { |
| 594 | return true; |
| 595 | } |
| 596 | if (heap_value == kDefaultHeapValue && GetDefaultValue(value->GetType()) == value) { |
| 597 | return true; |
| 598 | } |
| Mingyao Yang | a354053 | 2018-01-25 12:17:28 -0800 | [diff] [blame] | 599 | HInstruction* real_heap_value = GetRealHeapValue(heap_value); |
| 600 | if (real_heap_value != heap_value) { |
| 601 | return Equal(real_heap_value, value); |
| 602 | } |
| Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 603 | return false; |
| 604 | } |
| 605 | |
| xueliang.zhong | d71f1dc | 2018-01-24 17:24:16 +0000 | [diff] [blame] | 606 | bool CanValueBeKeptIfSameAsNew(HInstruction* value, |
| 607 | HInstruction* new_value, |
| 608 | HInstruction* new_value_set_instr) { |
| 609 | // For field/array set location operations, if the value is the same as the new_value |
| 610 | // it can be kept even if aliasing happens. All aliased operations will access the same memory |
| 611 | // range. |
| 612 | // For vector values, this is not true. For example: |
| 613 | // packed_data = [0xA, 0xB, 0xC, 0xD]; <-- Different values in each lane. |
| 614 | // VecStore array[i ,i+1,i+2,i+3] = packed_data; |
| 615 | // VecStore array[i+1,i+2,i+3,i+4] = packed_data; <-- We are here (partial overlap). |
| 616 | // VecLoad vx = array[i,i+1,i+2,i+3]; <-- Cannot be eliminated because the value |
| 617 | // here is not packed_data anymore. |
| 618 | // |
| 619 | // TODO: to allow such 'same value' optimization on vector data, |
| 620 | // LSA needs to report more fine-grain MAY alias information: |
| 621 | // (1) May alias due to two vector data partial overlap. |
| 622 | // e.g. a[i..i+3] and a[i+1,..,i+4]. |
| 623 | // (2) May alias due to two vector data may complete overlap each other. |
| 624 | // e.g. a[i..i+3] and b[i..i+3]. |
| 625 | // (3) May alias but the exact relationship between two locations is unknown. |
| 626 | // e.g. a[i..i+3] and b[j..j+3], where values of a,b,i,j are all unknown. |
| 627 | // This 'same value' optimization can apply only on case (2). |
| 628 | if (new_value_set_instr->IsVecOperation()) { |
| 629 | return false; |
| 630 | } |
| 631 | |
| 632 | return Equal(value, new_value); |
| 633 | } |
| 634 | |
| Aart Bik | b765a3f | 2018-05-10 14:47:48 -0700 | [diff] [blame] | 635 | void VisitSetLocation(HInstruction* instruction, size_t idx, HInstruction* value) { |
| 636 | DCHECK_NE(idx, HeapLocationCollector::kHeapLocationNotFound); |
| Mingyao Yang | a354053 | 2018-01-25 12:17:28 -0800 | [diff] [blame] | 637 | DCHECK(!IsStore(value)) << value->DebugName(); |
| Mingyao Yang | 206070c | 2017-11-29 23:01:58 -0800 | [diff] [blame] | 638 | // value may already have a substitute. |
| 639 | value = FindSubstitute(value); |
| Vladimir Marko | 009d166 | 2017-10-10 13:21:15 +0100 | [diff] [blame] | 640 | ScopedArenaVector<HInstruction*>& heap_values = |
| Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 641 | heap_values_for_[instruction->GetBlock()->GetBlockId()]; |
| 642 | HInstruction* heap_value = heap_values[idx]; |
| Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 643 | bool possibly_redundant = false; |
| Mingyao Yang | a354053 | 2018-01-25 12:17:28 -0800 | [diff] [blame] | 644 | |
| Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 645 | if (Equal(heap_value, value)) { |
| 646 | // Store into the heap location with the same value. |
| Mingyao Yang | a354053 | 2018-01-25 12:17:28 -0800 | [diff] [blame] | 647 | // This store can be eliminated right away. |
| 648 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 649 | return; |
| 650 | } else { |
| Mingyao Yang | 025c1a6 | 2017-10-30 11:19:57 -0700 | [diff] [blame] | 651 | HLoopInformation* loop_info = instruction->GetBlock()->GetLoopInformation(); |
| Mingyao Yang | a354053 | 2018-01-25 12:17:28 -0800 | [diff] [blame] | 652 | if (loop_info == nullptr) { |
| 653 | // Store is not in a loop. We try to precisely track the heap value by |
| 654 | // the store. |
| 655 | possibly_redundant = true; |
| 656 | } else if (!loop_info->IsIrreducible()) { |
| 657 | // instruction is a store in the loop so the loop must do write. |
| Mingyao Yang | 025c1a6 | 2017-10-30 11:19:57 -0700 | [diff] [blame] | 658 | DCHECK(side_effects_.GetLoopEffects(loop_info->GetHeader()).DoesAnyWrite()); |
| Aart Bik | b765a3f | 2018-05-10 14:47:48 -0700 | [diff] [blame] | 659 | ReferenceInfo* ref_info = heap_location_collector_.GetHeapLocation(idx)->GetReferenceInfo(); |
| 660 | if (ref_info->IsSingleton() && !loop_info->IsDefinedOutOfTheLoop(ref_info->GetReference())) { |
| Mingyao Yang | a354053 | 2018-01-25 12:17:28 -0800 | [diff] [blame] | 661 | // original_ref is created inside the loop. Value stored to it isn't needed at |
| Nicolas Geoffray | e97949e | 2018-01-25 10:42:01 +0000 | [diff] [blame] | 662 | // the loop header. This is true for outer loops also. |
| Mingyao Yang | a354053 | 2018-01-25 12:17:28 -0800 | [diff] [blame] | 663 | possibly_redundant = true; |
| 664 | } else { |
| 665 | // Keep the store since its value may be needed at the loop header. |
| Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 666 | } |
| Mingyao Yang | a354053 | 2018-01-25 12:17:28 -0800 | [diff] [blame] | 667 | } else { |
| 668 | // Keep the store inside irreducible loops. |
| Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 669 | } |
| Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 670 | } |
| Nicolas Geoffray | 85af16e | 2020-05-12 15:36:52 +0100 | [diff] [blame] | 671 | if (possibly_redundant && !instruction->CanThrow()) { |
| Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 672 | possibly_removed_stores_.push_back(instruction); |
| Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 673 | } |
| Mingyao Yang | e9d6e60 | 2015-10-23 17:08:42 -0700 | [diff] [blame] | 674 | |
| Mingyao Yang | a354053 | 2018-01-25 12:17:28 -0800 | [diff] [blame] | 675 | // Put the store as the heap value. If the value is loaded or needed after |
| 676 | // return/deoptimization later, this store isn't really redundant. |
| 677 | heap_values[idx] = instruction; |
| 678 | |
| Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 679 | // This store may kill values in other heap locations due to aliasing. |
| 680 | for (size_t i = 0; i < heap_values.size(); i++) { |
| xueliang.zhong | d71f1dc | 2018-01-24 17:24:16 +0000 | [diff] [blame] | 681 | if (i == idx || |
| 682 | heap_values[i] == kUnknownHeapValue || |
| 683 | CanValueBeKeptIfSameAsNew(heap_values[i], value, instruction) || |
| 684 | !heap_location_collector_.MayAlias(i, idx)) { |
| Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 685 | continue; |
| 686 | } |
| xueliang.zhong | d71f1dc | 2018-01-24 17:24:16 +0000 | [diff] [blame] | 687 | // Kill heap locations that may alias and as a result if the heap value |
| 688 | // is a store, the store needs to be kept. |
| 689 | KeepIfIsStore(heap_values[i]); |
| 690 | heap_values[i] = kUnknownHeapValue; |
| Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 691 | } |
| 692 | } |
| 693 | |
| Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 694 | void VisitInstanceFieldGet(HInstanceFieldGet* instruction) override { |
| Aart Bik | b765a3f | 2018-05-10 14:47:48 -0700 | [diff] [blame] | 695 | HInstruction* object = instruction->InputAt(0); |
| 696 | const FieldInfo& field = instruction->GetFieldInfo(); |
| 697 | VisitGetLocation(instruction, heap_location_collector_.GetFieldHeapLocation(object, &field)); |
| Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 698 | } |
| 699 | |
| Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 700 | void VisitInstanceFieldSet(HInstanceFieldSet* instruction) override { |
| Aart Bik | b765a3f | 2018-05-10 14:47:48 -0700 | [diff] [blame] | 701 | HInstruction* object = instruction->InputAt(0); |
| 702 | const FieldInfo& field = instruction->GetFieldInfo(); |
| Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 703 | HInstruction* value = instruction->InputAt(1); |
| Aart Bik | b765a3f | 2018-05-10 14:47:48 -0700 | [diff] [blame] | 704 | size_t idx = heap_location_collector_.GetFieldHeapLocation(object, &field); |
| 705 | VisitSetLocation(instruction, idx, value); |
| Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 706 | } |
| 707 | |
| Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 708 | void VisitStaticFieldGet(HStaticFieldGet* instruction) override { |
| Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 709 | HInstruction* cls = instruction->InputAt(0); |
| Aart Bik | b765a3f | 2018-05-10 14:47:48 -0700 | [diff] [blame] | 710 | const FieldInfo& field = instruction->GetFieldInfo(); |
| 711 | VisitGetLocation(instruction, heap_location_collector_.GetFieldHeapLocation(cls, &field)); |
| Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 712 | } |
| 713 | |
| Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 714 | void VisitStaticFieldSet(HStaticFieldSet* instruction) override { |
| Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 715 | HInstruction* cls = instruction->InputAt(0); |
| Aart Bik | b765a3f | 2018-05-10 14:47:48 -0700 | [diff] [blame] | 716 | const FieldInfo& field = instruction->GetFieldInfo(); |
| 717 | size_t idx = heap_location_collector_.GetFieldHeapLocation(cls, &field); |
| 718 | VisitSetLocation(instruction, idx, instruction->InputAt(1)); |
| Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 719 | } |
| 720 | |
| Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 721 | void VisitArrayGet(HArrayGet* instruction) override { |
| Aart Bik | b765a3f | 2018-05-10 14:47:48 -0700 | [diff] [blame] | 722 | VisitGetLocation(instruction, heap_location_collector_.GetArrayHeapLocation(instruction)); |
| Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 723 | } |
| 724 | |
| Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 725 | void VisitArraySet(HArraySet* instruction) override { |
| Aart Bik | b765a3f | 2018-05-10 14:47:48 -0700 | [diff] [blame] | 726 | size_t idx = heap_location_collector_.GetArrayHeapLocation(instruction); |
| xueliang.zhong | d71f1dc | 2018-01-24 17:24:16 +0000 | [diff] [blame] | 727 | VisitSetLocation(instruction, idx, instruction->GetValue()); |
| 728 | } |
| 729 | |
| 730 | void VisitVecLoad(HVecLoad* instruction) override { |
| 731 | VisitGetLocation(instruction, heap_location_collector_.GetArrayHeapLocation(instruction)); |
| 732 | } |
| 733 | |
| 734 | void VisitVecStore(HVecStore* instruction) override { |
| 735 | size_t idx = heap_location_collector_.GetArrayHeapLocation(instruction); |
| 736 | VisitSetLocation(instruction, idx, instruction->GetValue()); |
| Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 737 | } |
| 738 | |
| Andreas Gampe | fa6a1b0 | 2018-09-07 08:11:55 -0700 | [diff] [blame] | 739 | void VisitDeoptimize(HDeoptimize* instruction) override { |
| Vladimir Marko | 009d166 | 2017-10-10 13:21:15 +0100 | [diff] [blame] | 740 | const ScopedArenaVector<HInstruction*>& heap_values = |
| Mingyao Yang | eb2d2d346e | 2017-03-02 13:26:17 -0800 | [diff] [blame] | 741 | heap_values_for_[instruction->GetBlock()->GetBlockId()]; |
| 742 | for (HInstruction* heap_value : heap_values) { |
| Mingyao Yang | eb2d2d346e | 2017-03-02 13:26:17 -0800 | [diff] [blame] | 743 | // A store is kept as the heap value for possibly removed stores. |
| Mingyao Yang | a354053 | 2018-01-25 12:17:28 -0800 | [diff] [blame] | 744 | // That value stored is generally observeable after deoptimization, except |
| 745 | // for singletons that don't escape after deoptimization. |
| 746 | if (IsStore(heap_value)) { |
| 747 | if (heap_value->IsStaticFieldSet()) { |
| 748 | KeepIfIsStore(heap_value); |
| 749 | continue; |
| 750 | } |
| Mingyao Yang | eb2d2d346e | 2017-03-02 13:26:17 -0800 | [diff] [blame] | 751 | HInstruction* reference = heap_value->InputAt(0); |
| Mingyao Yang | a354053 | 2018-01-25 12:17:28 -0800 | [diff] [blame] | 752 | if (heap_location_collector_.FindReferenceInfoOf(reference)->IsSingleton()) { |
| 753 | if (reference->IsNewInstance() && reference->AsNewInstance()->IsFinalizable()) { |
| 754 | // Finalizable objects alway escape. |
| Mingyao Yang | eb2d2d346e | 2017-03-02 13:26:17 -0800 | [diff] [blame] | 755 | KeepIfIsStore(heap_value); |
| Mingyao Yang | a354053 | 2018-01-25 12:17:28 -0800 | [diff] [blame] | 756 | continue; |
| Mingyao Yang | eb2d2d346e | 2017-03-02 13:26:17 -0800 | [diff] [blame] | 757 | } |
| Mingyao Yang | a354053 | 2018-01-25 12:17:28 -0800 | [diff] [blame] | 758 | // Check whether the reference for a store is used by an environment local of |
| 759 | // HDeoptimize. If not, the singleton is not observed after |
| 760 | // deoptimizion. |
| 761 | for (const HUseListNode<HEnvironment*>& use : reference->GetEnvUses()) { |
| 762 | HEnvironment* user = use.GetUser(); |
| 763 | if (user->GetHolder() == instruction) { |
| 764 | // The singleton for the store is visible at this deoptimization |
| 765 | // point. Need to keep the store so that the heap value is |
| 766 | // seen by the interpreter. |
| 767 | KeepIfIsStore(heap_value); |
| 768 | } |
| 769 | } |
| 770 | } else { |
| 771 | KeepIfIsStore(heap_value); |
| Mingyao Yang | eb2d2d346e | 2017-03-02 13:26:17 -0800 | [diff] [blame] | 772 | } |
| 773 | } |
| 774 | } |
| 775 | } |
| 776 | |
| Mingyao Yang | 46721ef | 2017-10-05 14:45:17 -0700 | [diff] [blame] | 777 | // Keep necessary stores before exiting a method via return/throw. |
| 778 | void HandleExit(HBasicBlock* block) { |
| 779 | const ScopedArenaVector<HInstruction*>& heap_values = |
| 780 | heap_values_for_[block->GetBlockId()]; |
| 781 | for (size_t i = 0; i < heap_values.size(); i++) { |
| 782 | HInstruction* heap_value = heap_values[i]; |
| 783 | ReferenceInfo* ref_info = heap_location_collector_.GetHeapLocation(i)->GetReferenceInfo(); |
| 784 | if (!ref_info->IsSingletonAndRemovable()) { |
| 785 | KeepIfIsStore(heap_value); |
| 786 | } |
| 787 | } |
| 788 | } |
| 789 | |
| Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 790 | void VisitReturn(HReturn* instruction) override { |
| Mingyao Yang | 46721ef | 2017-10-05 14:45:17 -0700 | [diff] [blame] | 791 | HandleExit(instruction->GetBlock()); |
| 792 | } |
| 793 | |
| Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 794 | void VisitReturnVoid(HReturnVoid* return_void) override { |
| Mingyao Yang | 46721ef | 2017-10-05 14:45:17 -0700 | [diff] [blame] | 795 | HandleExit(return_void->GetBlock()); |
| 796 | } |
| 797 | |
| Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 798 | void VisitThrow(HThrow* throw_instruction) override { |
| Mingyao Yang | 46721ef | 2017-10-05 14:45:17 -0700 | [diff] [blame] | 799 | HandleExit(throw_instruction->GetBlock()); |
| 800 | } |
| 801 | |
| Mingyao Yang | 293f1c0 | 2017-11-08 15:22:17 -0800 | [diff] [blame] | 802 | void HandleInvoke(HInstruction* instruction) { |
| 803 | SideEffects side_effects = instruction->GetSideEffects(); |
| Vladimir Marko | 009d166 | 2017-10-10 13:21:15 +0100 | [diff] [blame] | 804 | ScopedArenaVector<HInstruction*>& heap_values = |
| Mingyao Yang | 293f1c0 | 2017-11-08 15:22:17 -0800 | [diff] [blame] | 805 | heap_values_for_[instruction->GetBlock()->GetBlockId()]; |
| Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 806 | for (size_t i = 0; i < heap_values.size(); i++) { |
| 807 | ReferenceInfo* ref_info = heap_location_collector_.GetHeapLocation(i)->GetReferenceInfo(); |
| 808 | if (ref_info->IsSingleton()) { |
| 809 | // Singleton references cannot be seen by the callee. |
| 810 | } else { |
| Mingyao Yang | 293f1c0 | 2017-11-08 15:22:17 -0800 | [diff] [blame] | 811 | if (side_effects.DoesAnyRead()) { |
| Mingyao Yang | a354053 | 2018-01-25 12:17:28 -0800 | [diff] [blame] | 812 | // Invocation may read the heap value. |
| Mingyao Yang | 293f1c0 | 2017-11-08 15:22:17 -0800 | [diff] [blame] | 813 | KeepIfIsStore(heap_values[i]); |
| 814 | } |
| 815 | if (side_effects.DoesAnyWrite()) { |
| Mingyao Yang | a354053 | 2018-01-25 12:17:28 -0800 | [diff] [blame] | 816 | // Keep the store since it's not used to track the heap value anymore. |
| 817 | KeepIfIsStore(heap_values[i]); |
| Mingyao Yang | 293f1c0 | 2017-11-08 15:22:17 -0800 | [diff] [blame] | 818 | heap_values[i] = kUnknownHeapValue; |
| 819 | } |
| Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 820 | } |
| 821 | } |
| 822 | } |
| 823 | |
| Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 824 | void VisitInvoke(HInvoke* invoke) override { |
| Orion Hodson | ac14139 | 2017-01-13 11:53:47 +0000 | [diff] [blame] | 825 | HandleInvoke(invoke); |
| 826 | } |
| 827 | |
| Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 828 | void VisitClinitCheck(HClinitCheck* clinit) override { |
| Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 829 | HandleInvoke(clinit); |
| 830 | } |
| 831 | |
| Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 832 | void VisitUnresolvedInstanceFieldGet(HUnresolvedInstanceFieldGet* instruction) override { |
| Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 833 | // Conservatively treat it as an invocation. |
| 834 | HandleInvoke(instruction); |
| 835 | } |
| 836 | |
| Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 837 | void VisitUnresolvedInstanceFieldSet(HUnresolvedInstanceFieldSet* instruction) override { |
| Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 838 | // Conservatively treat it as an invocation. |
| 839 | HandleInvoke(instruction); |
| 840 | } |
| 841 | |
| Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 842 | void VisitUnresolvedStaticFieldGet(HUnresolvedStaticFieldGet* instruction) override { |
| Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 843 | // Conservatively treat it as an invocation. |
| 844 | HandleInvoke(instruction); |
| 845 | } |
| 846 | |
| Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 847 | void VisitUnresolvedStaticFieldSet(HUnresolvedStaticFieldSet* instruction) override { |
| Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 848 | // Conservatively treat it as an invocation. |
| 849 | HandleInvoke(instruction); |
| 850 | } |
| 851 | |
| Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 852 | void VisitNewInstance(HNewInstance* new_instance) override { |
| Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 853 | ReferenceInfo* ref_info = heap_location_collector_.FindReferenceInfoOf(new_instance); |
| 854 | if (ref_info == nullptr) { |
| 855 | // new_instance isn't used for field accesses. No need to process it. |
| 856 | return; |
| 857 | } |
| Mingyao Yang | 025c1a6 | 2017-10-30 11:19:57 -0700 | [diff] [blame] | 858 | if (ref_info->IsSingletonAndRemovable() && !new_instance->NeedsChecks()) { |
| 859 | DCHECK(!new_instance->IsFinalizable()); |
| Mingyao Yang | 7cf9af2 | 2018-02-06 15:02:42 -0800 | [diff] [blame] | 860 | // new_instance can potentially be eliminated. |
| Mingyao Yang | 062157f | 2016-03-02 10:15:36 -0800 | [diff] [blame] | 861 | singleton_new_instances_.push_back(new_instance); |
| Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 862 | } |
| Vladimir Marko | 009d166 | 2017-10-10 13:21:15 +0100 | [diff] [blame] | 863 | ScopedArenaVector<HInstruction*>& heap_values = |
| Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 864 | heap_values_for_[new_instance->GetBlock()->GetBlockId()]; |
| 865 | for (size_t i = 0; i < heap_values.size(); i++) { |
| 866 | HInstruction* ref = |
| 867 | heap_location_collector_.GetHeapLocation(i)->GetReferenceInfo()->GetReference(); |
| 868 | size_t offset = heap_location_collector_.GetHeapLocation(i)->GetOffset(); |
| 869 | if (ref == new_instance && offset >= mirror::kObjectHeaderSize) { |
| 870 | // Instance fields except the header fields are set to default heap values. |
| 871 | heap_values[i] = kDefaultHeapValue; |
| 872 | } |
| 873 | } |
| 874 | } |
| 875 | |
| Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 876 | void VisitNewArray(HNewArray* new_array) override { |
| Mingyao Yang | 8697490 | 2017-03-01 14:03:51 -0800 | [diff] [blame] | 877 | ReferenceInfo* ref_info = heap_location_collector_.FindReferenceInfoOf(new_array); |
| 878 | if (ref_info == nullptr) { |
| 879 | // new_array isn't used for array accesses. No need to process it. |
| 880 | return; |
| 881 | } |
| 882 | if (ref_info->IsSingletonAndRemovable()) { |
| Mingyao Yang | 7cf9af2 | 2018-02-06 15:02:42 -0800 | [diff] [blame] | 883 | if (new_array->GetLength()->IsIntConstant() && |
| 884 | new_array->GetLength()->AsIntConstant()->GetValue() >= 0) { |
| 885 | // new_array can potentially be eliminated. |
| 886 | singleton_new_instances_.push_back(new_array); |
| 887 | } else { |
| 888 | // new_array may throw NegativeArraySizeException. Keep it. |
| 889 | } |
| Mingyao Yang | 8697490 | 2017-03-01 14:03:51 -0800 | [diff] [blame] | 890 | } |
| Vladimir Marko | 009d166 | 2017-10-10 13:21:15 +0100 | [diff] [blame] | 891 | ScopedArenaVector<HInstruction*>& heap_values = |
| Mingyao Yang | 8697490 | 2017-03-01 14:03:51 -0800 | [diff] [blame] | 892 | heap_values_for_[new_array->GetBlock()->GetBlockId()]; |
| 893 | for (size_t i = 0; i < heap_values.size(); i++) { |
| 894 | HeapLocation* location = heap_location_collector_.GetHeapLocation(i); |
| 895 | HInstruction* ref = location->GetReferenceInfo()->GetReference(); |
| 896 | if (ref == new_array && location->GetIndex() != nullptr) { |
| 897 | // Array elements are set to default heap values. |
| 898 | heap_values[i] = kDefaultHeapValue; |
| 899 | } |
| 900 | } |
| 901 | } |
| 902 | |
| Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 903 | const HeapLocationCollector& heap_location_collector_; |
| 904 | const SideEffectsAnalysis& side_effects_; |
| 905 | |
| Vladimir Marko | 009d166 | 2017-10-10 13:21:15 +0100 | [diff] [blame] | 906 | // Use local allocator for allocating memory. |
| 907 | ScopedArenaAllocator allocator_; |
| 908 | |
| Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 909 | // One array of heap values for each block. |
| Vladimir Marko | 009d166 | 2017-10-10 13:21:15 +0100 | [diff] [blame] | 910 | ScopedArenaVector<ScopedArenaVector<HInstruction*>> heap_values_for_; |
| Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 911 | |
| 912 | // We record the instructions that should be eliminated but may be |
| 913 | // used by heap locations. They'll be removed in the end. |
| Vladimir Marko | 009d166 | 2017-10-10 13:21:15 +0100 | [diff] [blame] | 914 | ScopedArenaVector<HInstruction*> removed_loads_; |
| 915 | ScopedArenaVector<HInstruction*> substitute_instructions_for_loads_; |
| Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 916 | |
| 917 | // Stores in this list may be removed from the list later when it's |
| 918 | // found that the store cannot be eliminated. |
| Vladimir Marko | 009d166 | 2017-10-10 13:21:15 +0100 | [diff] [blame] | 919 | ScopedArenaVector<HInstruction*> possibly_removed_stores_; |
| Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 920 | |
| Vladimir Marko | 009d166 | 2017-10-10 13:21:15 +0100 | [diff] [blame] | 921 | ScopedArenaVector<HInstruction*> singleton_new_instances_; |
| Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 922 | |
| 923 | DISALLOW_COPY_AND_ASSIGN(LSEVisitor); |
| 924 | }; |
| 925 | |
| Aart Bik | 2477320 | 2018-04-26 10:28:51 -0700 | [diff] [blame] | 926 | bool LoadStoreElimination::Run() { |
| David Brazdil | 8993caf | 2015-12-07 10:04:40 +0000 | [diff] [blame] | 927 | if (graph_->IsDebuggable() || graph_->HasTryCatch()) { |
| Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 928 | // Debugger may set heap values or trigger deoptimization of callers. |
| David Brazdil | 8993caf | 2015-12-07 10:04:40 +0000 | [diff] [blame] | 929 | // Try/catch support not implemented yet. |
| Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 930 | // Skip this optimization. |
| Aart Bik | 2477320 | 2018-04-26 10:28:51 -0700 | [diff] [blame] | 931 | return false; |
| Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 932 | } |
| Vladimir Marko | ef89842 | 2020-06-08 10:26:06 +0100 | [diff] [blame] | 933 | ScopedArenaAllocator allocator(graph_->GetArenaStack()); |
| 934 | LoadStoreAnalysis lsa(graph_, &allocator); |
| 935 | lsa.Run(); |
| 936 | const HeapLocationCollector& heap_location_collector = lsa.GetHeapLocationCollector(); |
| xueliang.zhong | c239a2b | 2017-04-27 15:31:37 +0100 | [diff] [blame] | 937 | if (heap_location_collector.GetNumberOfHeapLocations() == 0) { |
| 938 | // No HeapLocation information from LSA, skip this optimization. |
| Aart Bik | 2477320 | 2018-04-26 10:28:51 -0700 | [diff] [blame] | 939 | return false; |
| Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 940 | } |
| xueliang.zhong | c239a2b | 2017-04-27 15:31:37 +0100 | [diff] [blame] | 941 | |
| Igor Murashkin | 6ef4567 | 2017-08-08 13:59:55 -0700 | [diff] [blame] | 942 | LSEVisitor lse_visitor(graph_, heap_location_collector, side_effects_, stats_); |
| Vladimir Marko | 2c45bc9 | 2016-10-25 16:54:12 +0100 | [diff] [blame] | 943 | for (HBasicBlock* block : graph_->GetReversePostOrder()) { |
| 944 | lse_visitor.VisitBasicBlock(block); |
| Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 945 | } |
| 946 | lse_visitor.RemoveInstructions(); |
| Aart Bik | b765a3f | 2018-05-10 14:47:48 -0700 | [diff] [blame] | 947 | |
| Aart Bik | 2477320 | 2018-04-26 10:28:51 -0700 | [diff] [blame] | 948 | return true; |
| Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 949 | } |
| 950 | |
| 951 | } // namespace art |