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