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 | |
| 28 | namespace art { |
| 29 | |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 30 | // 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] | 31 | // A heap location can be set to kUnknownHeapValue when: |
| 32 | // - initially set a value. |
| 33 | // - killed due to aliasing, merging, invocation, or loop side effects. |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 34 | static HInstruction* const kUnknownHeapValue = |
| 35 | reinterpret_cast<HInstruction*>(static_cast<uintptr_t>(-1)); |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 36 | |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 37 | // Default heap value after an allocation. |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 38 | // 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] | 39 | static HInstruction* const kDefaultHeapValue = |
| 40 | reinterpret_cast<HInstruction*>(static_cast<uintptr_t>(-2)); |
| 41 | |
Mingyao Yang | c62b7ec | 2017-10-25 16:42:15 -0700 | [diff] [blame] | 42 | // Use HGraphDelegateVisitor for which all VisitInvokeXXX() delegate to VisitInvoke(). |
| 43 | class LSEVisitor : public HGraphDelegateVisitor { |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 44 | public: |
| 45 | LSEVisitor(HGraph* graph, |
| 46 | const HeapLocationCollector& heap_locations_collector, |
Igor Murashkin | 6ef4567 | 2017-08-08 13:59:55 -0700 | [diff] [blame] | 47 | const SideEffectsAnalysis& side_effects, |
| 48 | OptimizingCompilerStats* stats) |
Mingyao Yang | c62b7ec | 2017-10-25 16:42:15 -0700 | [diff] [blame] | 49 | : HGraphDelegateVisitor(graph, stats), |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 50 | heap_location_collector_(heap_locations_collector), |
| 51 | side_effects_(side_effects), |
Vladimir Marko | 009d166 | 2017-10-10 13:21:15 +0100 | [diff] [blame] | 52 | allocator_(graph->GetArenaStack()), |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 53 | heap_values_for_(graph->GetBlocks().size(), |
Vladimir Marko | 009d166 | 2017-10-10 13:21:15 +0100 | [diff] [blame] | 54 | ScopedArenaVector<HInstruction*>(heap_locations_collector. |
| 55 | GetNumberOfHeapLocations(), |
| 56 | kUnknownHeapValue, |
| 57 | allocator_.Adapter(kArenaAllocLSE)), |
| 58 | allocator_.Adapter(kArenaAllocLSE)), |
| 59 | removed_loads_(allocator_.Adapter(kArenaAllocLSE)), |
| 60 | substitute_instructions_for_loads_(allocator_.Adapter(kArenaAllocLSE)), |
| 61 | possibly_removed_stores_(allocator_.Adapter(kArenaAllocLSE)), |
| 62 | singleton_new_instances_(allocator_.Adapter(kArenaAllocLSE)), |
| 63 | singleton_new_arrays_(allocator_.Adapter(kArenaAllocLSE)) { |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | void VisitBasicBlock(HBasicBlock* block) OVERRIDE { |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 67 | // Populate the heap_values array for this block. |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 68 | // TODO: try to reuse the heap_values array from one predecessor if possible. |
| 69 | if (block->IsLoopHeader()) { |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 70 | HandleLoopSideEffects(block); |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 71 | } else { |
| 72 | MergePredecessorValues(block); |
| 73 | } |
| 74 | HGraphVisitor::VisitBasicBlock(block); |
| 75 | } |
| 76 | |
| 77 | // Remove recorded instructions that should be eliminated. |
| 78 | void RemoveInstructions() { |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 79 | size_t size = removed_loads_.size(); |
| 80 | DCHECK_EQ(size, substitute_instructions_for_loads_.size()); |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 81 | for (size_t i = 0; i < size; i++) { |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 82 | HInstruction* load = removed_loads_[i]; |
| 83 | DCHECK(load != nullptr); |
| 84 | DCHECK(load->IsInstanceFieldGet() || |
| 85 | load->IsStaticFieldGet() || |
Nicolas Geoffray | 8c4ddb2 | 2017-11-13 11:49:53 +0000 | [diff] [blame] | 86 | load->IsArrayGet()); |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 87 | HInstruction* substitute = substitute_instructions_for_loads_[i]; |
| 88 | DCHECK(substitute != nullptr); |
| 89 | // Keep tracing substitute till one that's not removed. |
| 90 | HInstruction* sub_sub = FindSubstitute(substitute); |
| 91 | while (sub_sub != substitute) { |
| 92 | substitute = sub_sub; |
| 93 | sub_sub = FindSubstitute(substitute); |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 94 | } |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 95 | load->ReplaceWith(substitute); |
| 96 | load->GetBlock()->RemoveInstruction(load); |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 97 | } |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 98 | |
| 99 | // At this point, stores in possibly_removed_stores_ can be safely removed. |
Mingyao Yang | 8697490 | 2017-03-01 14:03:51 -0800 | [diff] [blame] | 100 | for (HInstruction* store : possibly_removed_stores_) { |
Nicolas Geoffray | 8c4ddb2 | 2017-11-13 11:49:53 +0000 | [diff] [blame] | 101 | DCHECK(store->IsInstanceFieldSet() || store->IsStaticFieldSet() || store->IsArraySet()); |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 102 | store->GetBlock()->RemoveInstruction(store); |
| 103 | } |
| 104 | |
Igor Murashkin | d01745e | 2017-04-05 16:40:31 -0700 | [diff] [blame] | 105 | // Eliminate singleton-classified instructions: |
| 106 | // * - Constructor fences (they never escape this thread). |
| 107 | // * - Allocations (if they are unused). |
Mingyao Yang | 8697490 | 2017-03-01 14:03:51 -0800 | [diff] [blame] | 108 | for (HInstruction* new_instance : singleton_new_instances_) { |
Igor Murashkin | 6ef4567 | 2017-08-08 13:59:55 -0700 | [diff] [blame] | 109 | size_t removed = HConstructorFence::RemoveConstructorFences(new_instance); |
| 110 | MaybeRecordStat(stats_, |
| 111 | MethodCompilationStat::kConstructorFenceRemovedLSE, |
| 112 | removed); |
Igor Murashkin | d01745e | 2017-04-05 16:40:31 -0700 | [diff] [blame] | 113 | |
Mingyao Yang | 062157f | 2016-03-02 10:15:36 -0800 | [diff] [blame] | 114 | if (!new_instance->HasNonEnvironmentUses()) { |
| 115 | new_instance->RemoveEnvironmentUsers(); |
| 116 | new_instance->GetBlock()->RemoveInstruction(new_instance); |
| 117 | } |
| 118 | } |
Mingyao Yang | 8697490 | 2017-03-01 14:03:51 -0800 | [diff] [blame] | 119 | for (HInstruction* new_array : singleton_new_arrays_) { |
Igor Murashkin | 6ef4567 | 2017-08-08 13:59:55 -0700 | [diff] [blame] | 120 | size_t removed = HConstructorFence::RemoveConstructorFences(new_array); |
| 121 | MaybeRecordStat(stats_, |
| 122 | MethodCompilationStat::kConstructorFenceRemovedLSE, |
| 123 | removed); |
Igor Murashkin | d01745e | 2017-04-05 16:40:31 -0700 | [diff] [blame] | 124 | |
Mingyao Yang | 8697490 | 2017-03-01 14:03:51 -0800 | [diff] [blame] | 125 | if (!new_array->HasNonEnvironmentUses()) { |
| 126 | new_array->RemoveEnvironmentUsers(); |
| 127 | new_array->GetBlock()->RemoveInstruction(new_array); |
| 128 | } |
| 129 | } |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | private: |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 133 | // If heap_values[index] is an instance field store, need to keep the store. |
| 134 | // This is necessary if a heap value is killed due to merging, or loop side |
| 135 | // effects (which is essentially merging also), since a load later from the |
| 136 | // location won't be eliminated. |
| 137 | void KeepIfIsStore(HInstruction* heap_value) { |
| 138 | if (heap_value == kDefaultHeapValue || |
| 139 | heap_value == kUnknownHeapValue || |
Nicolas Geoffray | 8c4ddb2 | 2017-11-13 11:49:53 +0000 | [diff] [blame] | 140 | !(heap_value->IsInstanceFieldSet() || heap_value->IsArraySet())) { |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 141 | return; |
| 142 | } |
| 143 | auto idx = std::find(possibly_removed_stores_.begin(), |
| 144 | possibly_removed_stores_.end(), heap_value); |
| 145 | if (idx != possibly_removed_stores_.end()) { |
| 146 | // Make sure the store is kept. |
| 147 | possibly_removed_stores_.erase(idx); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | void HandleLoopSideEffects(HBasicBlock* block) { |
| 152 | DCHECK(block->IsLoopHeader()); |
| 153 | int block_id = block->GetBlockId(); |
Vladimir Marko | 009d166 | 2017-10-10 13:21:15 +0100 | [diff] [blame] | 154 | ScopedArenaVector<HInstruction*>& heap_values = heap_values_for_[block_id]; |
Nicolas Geoffray | 15bd228 | 2016-01-05 15:55:41 +0000 | [diff] [blame] | 155 | |
| 156 | // Don't eliminate loads in irreducible loops. This is safe for singletons, because |
| 157 | // they are always used by the non-eliminated loop-phi. |
| 158 | if (block->GetLoopInformation()->IsIrreducible()) { |
| 159 | if (kIsDebugBuild) { |
| 160 | for (size_t i = 0; i < heap_values.size(); i++) { |
| 161 | DCHECK_EQ(heap_values[i], kUnknownHeapValue); |
| 162 | } |
| 163 | } |
| 164 | return; |
| 165 | } |
| 166 | |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 167 | HBasicBlock* pre_header = block->GetLoopInformation()->GetPreHeader(); |
Vladimir Marko | 009d166 | 2017-10-10 13:21:15 +0100 | [diff] [blame] | 168 | ScopedArenaVector<HInstruction*>& pre_header_heap_values = |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 169 | heap_values_for_[pre_header->GetBlockId()]; |
Nicolas Geoffray | 15bd228 | 2016-01-05 15:55:41 +0000 | [diff] [blame] | 170 | |
Mingyao Yang | 803cbb9 | 2015-12-01 12:24:36 -0800 | [diff] [blame] | 171 | // Inherit the values from pre-header. |
| 172 | for (size_t i = 0; i < heap_values.size(); i++) { |
| 173 | heap_values[i] = pre_header_heap_values[i]; |
| 174 | } |
| 175 | |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 176 | // We do a single pass in reverse post order. For loops, use the side effects as a hint |
| 177 | // to see if the heap values should be killed. |
| 178 | if (side_effects_.GetLoopEffects(block).DoesAnyWrite()) { |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 179 | for (size_t i = 0; i < heap_values.size(); i++) { |
Mingyao Yang | 803cbb9 | 2015-12-01 12:24:36 -0800 | [diff] [blame] | 180 | HeapLocation* location = heap_location_collector_.GetHeapLocation(i); |
| 181 | ReferenceInfo* ref_info = location->GetReferenceInfo(); |
Mingyao Yang | eb2d2d346e | 2017-03-02 13:26:17 -0800 | [diff] [blame] | 182 | if (ref_info->IsSingletonAndRemovable() && |
| 183 | !location->IsValueKilledByLoopSideEffects()) { |
| 184 | // A removable singleton's field that's not stored into inside a loop is |
| 185 | // invariant throughout the loop. Nothing to do. |
Mingyao Yang | eb2d2d346e | 2017-03-02 13:26:17 -0800 | [diff] [blame] | 186 | } else { |
| 187 | // heap value is killed by loop side effects (stored into directly, or |
| 188 | // due to aliasing). Or the heap value may be needed after method return |
| 189 | // or deoptimization. |
Mingyao Yang | 803cbb9 | 2015-12-01 12:24:36 -0800 | [diff] [blame] | 190 | KeepIfIsStore(pre_header_heap_values[i]); |
| 191 | heap_values[i] = kUnknownHeapValue; |
Mingyao Yang | 803cbb9 | 2015-12-01 12:24:36 -0800 | [diff] [blame] | 192 | } |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | } |
| 196 | |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 197 | void MergePredecessorValues(HBasicBlock* block) { |
Vladimir Marko | 009d166 | 2017-10-10 13:21:15 +0100 | [diff] [blame] | 198 | ArrayRef<HBasicBlock* const> predecessors(block->GetPredecessors()); |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 199 | if (predecessors.size() == 0) { |
| 200 | return; |
| 201 | } |
Mingyao Yang | 58d9bfc | 2016-11-01 13:31:58 -0700 | [diff] [blame] | 202 | |
Vladimir Marko | 009d166 | 2017-10-10 13:21:15 +0100 | [diff] [blame] | 203 | ScopedArenaVector<HInstruction*>& heap_values = heap_values_for_[block->GetBlockId()]; |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 204 | for (size_t i = 0; i < heap_values.size(); i++) { |
Mingyao Yang | 58d9bfc | 2016-11-01 13:31:58 -0700 | [diff] [blame] | 205 | HInstruction* merged_value = nullptr; |
| 206 | // Whether merged_value is a result that's merged from all predecessors. |
| 207 | bool from_all_predecessors = true; |
| 208 | ReferenceInfo* ref_info = heap_location_collector_.GetHeapLocation(i)->GetReferenceInfo(); |
| 209 | HInstruction* singleton_ref = nullptr; |
Mingyao Yang | eb2d2d346e | 2017-03-02 13:26:17 -0800 | [diff] [blame] | 210 | if (ref_info->IsSingleton()) { |
Mingyao Yang | 58d9bfc | 2016-11-01 13:31:58 -0700 | [diff] [blame] | 211 | // We do more analysis of liveness when merging heap values for such |
| 212 | // cases since stores into such references may potentially be eliminated. |
| 213 | singleton_ref = ref_info->GetReference(); |
| 214 | } |
| 215 | |
| 216 | for (HBasicBlock* predecessor : predecessors) { |
| 217 | HInstruction* pred_value = heap_values_for_[predecessor->GetBlockId()][i]; |
| 218 | if ((singleton_ref != nullptr) && |
| 219 | !singleton_ref->GetBlock()->Dominates(predecessor)) { |
| 220 | // singleton_ref is not live in this predecessor. Skip this predecessor since |
| 221 | // it does not really have the location. |
| 222 | DCHECK_EQ(pred_value, kUnknownHeapValue); |
| 223 | from_all_predecessors = false; |
| 224 | continue; |
| 225 | } |
| 226 | if (merged_value == nullptr) { |
| 227 | // First seen heap value. |
| 228 | merged_value = pred_value; |
| 229 | } else if (pred_value != merged_value) { |
| 230 | // There are conflicting values. |
| 231 | merged_value = kUnknownHeapValue; |
| 232 | break; |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 233 | } |
| 234 | } |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 235 | |
Mingyao Yang | eb2d2d346e | 2017-03-02 13:26:17 -0800 | [diff] [blame] | 236 | if (merged_value == kUnknownHeapValue || ref_info->IsSingletonAndNonRemovable()) { |
| 237 | // There are conflicting heap values from different predecessors, |
| 238 | // or the heap value may be needed after method return or deoptimization. |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 239 | // Keep the last store in each predecessor since future loads cannot be eliminated. |
Mingyao Yang | 58d9bfc | 2016-11-01 13:31:58 -0700 | [diff] [blame] | 240 | for (HBasicBlock* predecessor : predecessors) { |
Vladimir Marko | 009d166 | 2017-10-10 13:21:15 +0100 | [diff] [blame] | 241 | ScopedArenaVector<HInstruction*>& pred_values = |
| 242 | heap_values_for_[predecessor->GetBlockId()]; |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 243 | KeepIfIsStore(pred_values[i]); |
| 244 | } |
| 245 | } |
Mingyao Yang | 58d9bfc | 2016-11-01 13:31:58 -0700 | [diff] [blame] | 246 | |
| 247 | if ((merged_value == nullptr) || !from_all_predecessors) { |
| 248 | DCHECK(singleton_ref != nullptr); |
| 249 | DCHECK((singleton_ref->GetBlock() == block) || |
| 250 | !singleton_ref->GetBlock()->Dominates(block)); |
| 251 | // singleton_ref is not defined before block or defined only in some of its |
| 252 | // predecessors, so block doesn't really have the location at its entry. |
| 253 | heap_values[i] = kUnknownHeapValue; |
| 254 | } else { |
| 255 | heap_values[i] = merged_value; |
| 256 | } |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 257 | } |
| 258 | } |
| 259 | |
| 260 | // `instruction` is being removed. Try to see if the null check on it |
| 261 | // can be removed. This can happen if the same value is set in two branches |
| 262 | // but not in dominators. Such as: |
| 263 | // int[] a = foo(); |
| 264 | // if () { |
| 265 | // a[0] = 2; |
| 266 | // } else { |
| 267 | // a[0] = 2; |
| 268 | // } |
| 269 | // // a[0] can now be replaced with constant 2, and the null check on it can be removed. |
| 270 | void TryRemovingNullCheck(HInstruction* instruction) { |
| 271 | HInstruction* prev = instruction->GetPrevious(); |
| 272 | if ((prev != nullptr) && prev->IsNullCheck() && (prev == instruction->InputAt(0))) { |
| 273 | // Previous instruction is a null check for this instruction. Remove the null check. |
| 274 | prev->ReplaceWith(prev->InputAt(0)); |
| 275 | prev->GetBlock()->RemoveInstruction(prev); |
| 276 | } |
| 277 | } |
| 278 | |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 279 | HInstruction* GetDefaultValue(DataType::Type type) { |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 280 | switch (type) { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 281 | case DataType::Type::kReference: |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 282 | return GetGraph()->GetNullConstant(); |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 283 | case DataType::Type::kBool: |
Vladimir Marko | d5d2f2c | 2017-09-26 12:37:26 +0100 | [diff] [blame] | 284 | case DataType::Type::kUint8: |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 285 | case DataType::Type::kInt8: |
| 286 | case DataType::Type::kUint16: |
| 287 | case DataType::Type::kInt16: |
| 288 | case DataType::Type::kInt32: |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 289 | return GetGraph()->GetIntConstant(0); |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 290 | case DataType::Type::kInt64: |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 291 | return GetGraph()->GetLongConstant(0); |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 292 | case DataType::Type::kFloat32: |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 293 | return GetGraph()->GetFloatConstant(0); |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 294 | case DataType::Type::kFloat64: |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 295 | return GetGraph()->GetDoubleConstant(0); |
| 296 | default: |
| 297 | UNREACHABLE(); |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | void VisitGetLocation(HInstruction* instruction, |
| 302 | HInstruction* ref, |
| 303 | size_t offset, |
| 304 | HInstruction* index, |
xueliang.zhong | b50b16a | 2017-09-19 17:43:29 +0100 | [diff] [blame] | 305 | size_t vector_length, |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 306 | int16_t declaring_class_def_index) { |
xueliang.zhong | c239a2b | 2017-04-27 15:31:37 +0100 | [diff] [blame] | 307 | HInstruction* original_ref = heap_location_collector_.HuntForOriginalReference(ref); |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 308 | ReferenceInfo* ref_info = heap_location_collector_.FindReferenceInfoOf(original_ref); |
| 309 | size_t idx = heap_location_collector_.FindHeapLocationIndex( |
xueliang.zhong | b50b16a | 2017-09-19 17:43:29 +0100 | [diff] [blame] | 310 | ref_info, offset, index, vector_length, declaring_class_def_index); |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 311 | DCHECK_NE(idx, HeapLocationCollector::kHeapLocationNotFound); |
Vladimir Marko | 009d166 | 2017-10-10 13:21:15 +0100 | [diff] [blame] | 312 | ScopedArenaVector<HInstruction*>& heap_values = |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 313 | heap_values_for_[instruction->GetBlock()->GetBlockId()]; |
| 314 | HInstruction* heap_value = heap_values[idx]; |
| 315 | if (heap_value == kDefaultHeapValue) { |
| 316 | HInstruction* constant = GetDefaultValue(instruction->GetType()); |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 317 | removed_loads_.push_back(instruction); |
| 318 | substitute_instructions_for_loads_.push_back(constant); |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 319 | heap_values[idx] = constant; |
| 320 | return; |
| 321 | } |
Mingyao Yang | 8697490 | 2017-03-01 14:03:51 -0800 | [diff] [blame] | 322 | if (heap_value != kUnknownHeapValue) { |
Nicolas Geoffray | 8c4ddb2 | 2017-11-13 11:49:53 +0000 | [diff] [blame] | 323 | if (heap_value->IsInstanceFieldSet() || heap_value->IsArraySet()) { |
Mingyao Yang | 8697490 | 2017-03-01 14:03:51 -0800 | [diff] [blame] | 324 | HInstruction* store = heap_value; |
| 325 | // This load must be from a singleton since it's from the same |
| 326 | // field/element that a "removed" store puts the value. That store |
| 327 | // must be to a singleton's field/element. |
| 328 | DCHECK(ref_info->IsSingleton()); |
| 329 | // Get the real heap value of the store. |
| 330 | heap_value = heap_value->IsInstanceFieldSet() ? store->InputAt(1) : store->InputAt(2); |
| 331 | } |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 332 | } |
David Brazdil | 15693bf | 2015-12-16 10:30:45 +0000 | [diff] [blame] | 333 | if (heap_value == kUnknownHeapValue) { |
| 334 | // Load isn't eliminated. Put the load as the value into the HeapLocation. |
| 335 | // This acts like GVN but with better aliasing analysis. |
| 336 | heap_values[idx] = instruction; |
| 337 | } else { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 338 | if (DataType::Kind(heap_value->GetType()) != DataType::Kind(instruction->GetType())) { |
Nicolas Geoffray | 0397163 | 2016-03-17 10:44:24 +0000 | [diff] [blame] | 339 | // The only situation where the same heap location has different type is when |
Nicolas Geoffray | 65fef30 | 2016-05-04 14:00:12 +0100 | [diff] [blame] | 340 | // we do an array get on an instruction that originates from the null constant |
| 341 | // (the null could be behind a field access, an array access, a null check or |
| 342 | // a bound type). |
| 343 | // In order to stay properly typed on primitive types, we do not eliminate |
| 344 | // the array gets. |
Nicolas Geoffray | 0397163 | 2016-03-17 10:44:24 +0000 | [diff] [blame] | 345 | if (kIsDebugBuild) { |
| 346 | DCHECK(heap_value->IsArrayGet()) << heap_value->DebugName(); |
| 347 | DCHECK(instruction->IsArrayGet()) << instruction->DebugName(); |
Nicolas Geoffray | 0397163 | 2016-03-17 10:44:24 +0000 | [diff] [blame] | 348 | } |
| 349 | return; |
| 350 | } |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 351 | removed_loads_.push_back(instruction); |
| 352 | substitute_instructions_for_loads_.push_back(heap_value); |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 353 | TryRemovingNullCheck(instruction); |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 354 | } |
| 355 | } |
| 356 | |
| 357 | bool Equal(HInstruction* heap_value, HInstruction* value) { |
| 358 | if (heap_value == value) { |
| 359 | return true; |
| 360 | } |
| 361 | if (heap_value == kDefaultHeapValue && GetDefaultValue(value->GetType()) == value) { |
| 362 | return true; |
| 363 | } |
| 364 | return false; |
| 365 | } |
| 366 | |
| 367 | void VisitSetLocation(HInstruction* instruction, |
| 368 | HInstruction* ref, |
| 369 | size_t offset, |
| 370 | HInstruction* index, |
xueliang.zhong | b50b16a | 2017-09-19 17:43:29 +0100 | [diff] [blame] | 371 | size_t vector_length, |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 372 | int16_t declaring_class_def_index, |
| 373 | HInstruction* value) { |
xueliang.zhong | c239a2b | 2017-04-27 15:31:37 +0100 | [diff] [blame] | 374 | HInstruction* original_ref = heap_location_collector_.HuntForOriginalReference(ref); |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 375 | ReferenceInfo* ref_info = heap_location_collector_.FindReferenceInfoOf(original_ref); |
| 376 | size_t idx = heap_location_collector_.FindHeapLocationIndex( |
xueliang.zhong | b50b16a | 2017-09-19 17:43:29 +0100 | [diff] [blame] | 377 | ref_info, offset, index, vector_length, declaring_class_def_index); |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 378 | DCHECK_NE(idx, HeapLocationCollector::kHeapLocationNotFound); |
Vladimir Marko | 009d166 | 2017-10-10 13:21:15 +0100 | [diff] [blame] | 379 | ScopedArenaVector<HInstruction*>& heap_values = |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 380 | heap_values_for_[instruction->GetBlock()->GetBlockId()]; |
| 381 | HInstruction* heap_value = heap_values[idx]; |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 382 | bool same_value = false; |
| 383 | bool possibly_redundant = false; |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 384 | if (Equal(heap_value, value)) { |
| 385 | // Store into the heap location with the same value. |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 386 | same_value = true; |
Mingyao Yang | 0e3151b | 2017-10-30 11:19:57 -0700 | [diff] [blame] | 387 | } else if (index != nullptr && |
| 388 | heap_location_collector_.GetHeapLocation(idx)->HasAliasedLocations()) { |
| 389 | // For array element, don't eliminate stores if the location can be aliased |
| 390 | // (due to either ref or index aliasing). |
Mingyao Yang | eb2d2d346e | 2017-03-02 13:26:17 -0800 | [diff] [blame] | 391 | } else if (ref_info->IsSingleton()) { |
Mingyao Yang | 025c1a6 | 2017-10-30 11:19:57 -0700 | [diff] [blame] | 392 | // Store into a field/element of a singleton. The value cannot be killed due to |
Mingyao Yang | eb2d2d346e | 2017-03-02 13:26:17 -0800 | [diff] [blame] | 393 | // aliasing/invocation. It can be redundant since future loads can |
| 394 | // directly get the value set by this instruction. The value can still be killed due to |
| 395 | // merging or loop side effects. Stores whose values are killed due to merging/loop side |
| 396 | // effects later will be removed from possibly_removed_stores_ when that is detected. |
| 397 | // Stores whose values may be needed after method return or deoptimization |
| 398 | // are also removed from possibly_removed_stores_ when that is detected. |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 399 | possibly_redundant = true; |
Mingyao Yang | 025c1a6 | 2017-10-30 11:19:57 -0700 | [diff] [blame] | 400 | HLoopInformation* loop_info = instruction->GetBlock()->GetLoopInformation(); |
| 401 | if (loop_info != nullptr) { |
| 402 | // instruction is a store in the loop so the loop must does write. |
| 403 | DCHECK(side_effects_.GetLoopEffects(loop_info->GetHeader()).DoesAnyWrite()); |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 404 | |
Mingyao Yang | 025c1a6 | 2017-10-30 11:19:57 -0700 | [diff] [blame] | 405 | if (loop_info->IsDefinedOutOfTheLoop(original_ref)) { |
| 406 | DCHECK(original_ref->GetBlock()->Dominates(loop_info->GetPreHeader())); |
| 407 | // Keep the store since its value may be needed at the loop header. |
| 408 | possibly_redundant = false; |
| 409 | } else { |
| 410 | // The singleton is created inside the loop. Value stored to it isn't needed at |
| 411 | // the loop header. This is true for outer loops also. |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 412 | } |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 413 | } |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 414 | } |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 415 | if (same_value || possibly_redundant) { |
| 416 | possibly_removed_stores_.push_back(instruction); |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 417 | } |
Mingyao Yang | e9d6e60 | 2015-10-23 17:08:42 -0700 | [diff] [blame] | 418 | |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 419 | if (!same_value) { |
| 420 | if (possibly_redundant) { |
Nicolas Geoffray | 8c4ddb2 | 2017-11-13 11:49:53 +0000 | [diff] [blame] | 421 | DCHECK(instruction->IsInstanceFieldSet() || instruction->IsArraySet()); |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 422 | // Put the store as the heap value. If the value is loaded from heap |
| 423 | // by a load later, this store isn't really redundant. |
| 424 | heap_values[idx] = instruction; |
| 425 | } else { |
| 426 | heap_values[idx] = value; |
| 427 | } |
| 428 | } |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 429 | // This store may kill values in other heap locations due to aliasing. |
| 430 | for (size_t i = 0; i < heap_values.size(); i++) { |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 431 | if (i == idx) { |
| 432 | continue; |
| 433 | } |
Nicolas Geoffray | 8c4ddb2 | 2017-11-13 11:49:53 +0000 | [diff] [blame] | 434 | if (heap_values[i] == value) { |
| 435 | // Same value should be kept even if aliasing happens. |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 436 | continue; |
| 437 | } |
| 438 | if (heap_values[i] == kUnknownHeapValue) { |
| 439 | // Value is already unknown, no need for aliasing check. |
| 440 | continue; |
| 441 | } |
| 442 | if (heap_location_collector_.MayAlias(i, idx)) { |
| 443 | // Kill heap locations that may alias. |
| 444 | heap_values[i] = kUnknownHeapValue; |
| 445 | } |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | void VisitInstanceFieldGet(HInstanceFieldGet* instruction) OVERRIDE { |
| 450 | HInstruction* obj = instruction->InputAt(0); |
| 451 | size_t offset = instruction->GetFieldInfo().GetFieldOffset().SizeValue(); |
| 452 | int16_t declaring_class_def_index = instruction->GetFieldInfo().GetDeclaringClassDefIndex(); |
xueliang.zhong | b50b16a | 2017-09-19 17:43:29 +0100 | [diff] [blame] | 453 | VisitGetLocation(instruction, |
| 454 | obj, |
| 455 | offset, |
| 456 | nullptr, |
| 457 | HeapLocation::kScalar, |
| 458 | declaring_class_def_index); |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | void VisitInstanceFieldSet(HInstanceFieldSet* instruction) OVERRIDE { |
| 462 | HInstruction* obj = instruction->InputAt(0); |
| 463 | size_t offset = instruction->GetFieldInfo().GetFieldOffset().SizeValue(); |
| 464 | int16_t declaring_class_def_index = instruction->GetFieldInfo().GetDeclaringClassDefIndex(); |
| 465 | HInstruction* value = instruction->InputAt(1); |
xueliang.zhong | b50b16a | 2017-09-19 17:43:29 +0100 | [diff] [blame] | 466 | VisitSetLocation(instruction, |
| 467 | obj, |
| 468 | offset, |
| 469 | nullptr, |
| 470 | HeapLocation::kScalar, |
| 471 | declaring_class_def_index, |
| 472 | value); |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | void VisitStaticFieldGet(HStaticFieldGet* instruction) OVERRIDE { |
| 476 | HInstruction* cls = instruction->InputAt(0); |
| 477 | size_t offset = instruction->GetFieldInfo().GetFieldOffset().SizeValue(); |
| 478 | int16_t declaring_class_def_index = instruction->GetFieldInfo().GetDeclaringClassDefIndex(); |
xueliang.zhong | b50b16a | 2017-09-19 17:43:29 +0100 | [diff] [blame] | 479 | VisitGetLocation(instruction, |
| 480 | cls, |
| 481 | offset, |
| 482 | nullptr, |
| 483 | HeapLocation::kScalar, |
| 484 | declaring_class_def_index); |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | void VisitStaticFieldSet(HStaticFieldSet* instruction) OVERRIDE { |
| 488 | HInstruction* cls = instruction->InputAt(0); |
| 489 | size_t offset = instruction->GetFieldInfo().GetFieldOffset().SizeValue(); |
| 490 | int16_t declaring_class_def_index = instruction->GetFieldInfo().GetDeclaringClassDefIndex(); |
| 491 | HInstruction* value = instruction->InputAt(1); |
xueliang.zhong | b50b16a | 2017-09-19 17:43:29 +0100 | [diff] [blame] | 492 | VisitSetLocation(instruction, |
| 493 | cls, |
| 494 | offset, |
| 495 | nullptr, |
| 496 | HeapLocation::kScalar, |
| 497 | declaring_class_def_index, |
| 498 | value); |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | void VisitArrayGet(HArrayGet* instruction) OVERRIDE { |
| 502 | HInstruction* array = instruction->InputAt(0); |
| 503 | HInstruction* index = instruction->InputAt(1); |
| 504 | VisitGetLocation(instruction, |
| 505 | array, |
| 506 | HeapLocation::kInvalidFieldOffset, |
| 507 | index, |
xueliang.zhong | b50b16a | 2017-09-19 17:43:29 +0100 | [diff] [blame] | 508 | HeapLocation::kScalar, |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 509 | HeapLocation::kDeclaringClassDefIndexForArrays); |
| 510 | } |
| 511 | |
| 512 | void VisitArraySet(HArraySet* instruction) OVERRIDE { |
| 513 | HInstruction* array = instruction->InputAt(0); |
| 514 | HInstruction* index = instruction->InputAt(1); |
| 515 | HInstruction* value = instruction->InputAt(2); |
| 516 | VisitSetLocation(instruction, |
| 517 | array, |
| 518 | HeapLocation::kInvalidFieldOffset, |
| 519 | index, |
xueliang.zhong | b50b16a | 2017-09-19 17:43:29 +0100 | [diff] [blame] | 520 | HeapLocation::kScalar, |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 521 | HeapLocation::kDeclaringClassDefIndexForArrays, |
| 522 | value); |
| 523 | } |
| 524 | |
Mingyao Yang | eb2d2d346e | 2017-03-02 13:26:17 -0800 | [diff] [blame] | 525 | void VisitDeoptimize(HDeoptimize* instruction) { |
Vladimir Marko | 009d166 | 2017-10-10 13:21:15 +0100 | [diff] [blame] | 526 | const ScopedArenaVector<HInstruction*>& heap_values = |
Mingyao Yang | eb2d2d346e | 2017-03-02 13:26:17 -0800 | [diff] [blame] | 527 | heap_values_for_[instruction->GetBlock()->GetBlockId()]; |
| 528 | for (HInstruction* heap_value : heap_values) { |
| 529 | // Filter out fake instructions before checking instruction kind below. |
| 530 | if (heap_value == kUnknownHeapValue || heap_value == kDefaultHeapValue) { |
| 531 | continue; |
| 532 | } |
| 533 | // A store is kept as the heap value for possibly removed stores. |
Nicolas Geoffray | 8c4ddb2 | 2017-11-13 11:49:53 +0000 | [diff] [blame] | 534 | if (heap_value->IsInstanceFieldSet() || heap_value->IsArraySet()) { |
Mingyao Yang | eb2d2d346e | 2017-03-02 13:26:17 -0800 | [diff] [blame] | 535 | // Check whether the reference for a store is used by an environment local of |
| 536 | // HDeoptimize. |
| 537 | HInstruction* reference = heap_value->InputAt(0); |
| 538 | DCHECK(heap_location_collector_.FindReferenceInfoOf(reference)->IsSingleton()); |
| 539 | for (const HUseListNode<HEnvironment*>& use : reference->GetEnvUses()) { |
| 540 | HEnvironment* user = use.GetUser(); |
| 541 | if (user->GetHolder() == instruction) { |
| 542 | // The singleton for the store is visible at this deoptimization |
| 543 | // point. Need to keep the store so that the heap value is |
| 544 | // seen by the interpreter. |
| 545 | KeepIfIsStore(heap_value); |
| 546 | } |
| 547 | } |
| 548 | } |
| 549 | } |
| 550 | } |
| 551 | |
Mingyao Yang | 293f1c0 | 2017-11-08 15:22:17 -0800 | [diff] [blame] | 552 | void HandleInvoke(HInstruction* instruction) { |
| 553 | SideEffects side_effects = instruction->GetSideEffects(); |
Vladimir Marko | 009d166 | 2017-10-10 13:21:15 +0100 | [diff] [blame] | 554 | ScopedArenaVector<HInstruction*>& heap_values = |
Mingyao Yang | 293f1c0 | 2017-11-08 15:22:17 -0800 | [diff] [blame] | 555 | heap_values_for_[instruction->GetBlock()->GetBlockId()]; |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 556 | for (size_t i = 0; i < heap_values.size(); i++) { |
| 557 | ReferenceInfo* ref_info = heap_location_collector_.GetHeapLocation(i)->GetReferenceInfo(); |
| 558 | if (ref_info->IsSingleton()) { |
| 559 | // Singleton references cannot be seen by the callee. |
| 560 | } else { |
Mingyao Yang | 293f1c0 | 2017-11-08 15:22:17 -0800 | [diff] [blame] | 561 | if (side_effects.DoesAnyRead()) { |
| 562 | KeepIfIsStore(heap_values[i]); |
| 563 | } |
| 564 | if (side_effects.DoesAnyWrite()) { |
| 565 | heap_values[i] = kUnknownHeapValue; |
| 566 | } |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 567 | } |
| 568 | } |
| 569 | } |
| 570 | |
Mingyao Yang | c62b7ec | 2017-10-25 16:42:15 -0700 | [diff] [blame] | 571 | void VisitInvoke(HInvoke* invoke) OVERRIDE { |
Orion Hodson | ac14139 | 2017-01-13 11:53:47 +0000 | [diff] [blame] | 572 | HandleInvoke(invoke); |
| 573 | } |
| 574 | |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 575 | void VisitClinitCheck(HClinitCheck* clinit) OVERRIDE { |
| 576 | HandleInvoke(clinit); |
| 577 | } |
| 578 | |
| 579 | void VisitUnresolvedInstanceFieldGet(HUnresolvedInstanceFieldGet* instruction) OVERRIDE { |
| 580 | // Conservatively treat it as an invocation. |
| 581 | HandleInvoke(instruction); |
| 582 | } |
| 583 | |
| 584 | void VisitUnresolvedInstanceFieldSet(HUnresolvedInstanceFieldSet* instruction) OVERRIDE { |
| 585 | // Conservatively treat it as an invocation. |
| 586 | HandleInvoke(instruction); |
| 587 | } |
| 588 | |
| 589 | void VisitUnresolvedStaticFieldGet(HUnresolvedStaticFieldGet* instruction) OVERRIDE { |
| 590 | // Conservatively treat it as an invocation. |
| 591 | HandleInvoke(instruction); |
| 592 | } |
| 593 | |
| 594 | void VisitUnresolvedStaticFieldSet(HUnresolvedStaticFieldSet* instruction) OVERRIDE { |
| 595 | // Conservatively treat it as an invocation. |
| 596 | HandleInvoke(instruction); |
| 597 | } |
| 598 | |
| 599 | void VisitNewInstance(HNewInstance* new_instance) OVERRIDE { |
| 600 | ReferenceInfo* ref_info = heap_location_collector_.FindReferenceInfoOf(new_instance); |
| 601 | if (ref_info == nullptr) { |
| 602 | // new_instance isn't used for field accesses. No need to process it. |
| 603 | return; |
| 604 | } |
Mingyao Yang | 025c1a6 | 2017-10-30 11:19:57 -0700 | [diff] [blame] | 605 | if (ref_info->IsSingletonAndRemovable() && !new_instance->NeedsChecks()) { |
| 606 | DCHECK(!new_instance->IsFinalizable()); |
Mingyao Yang | 062157f | 2016-03-02 10:15:36 -0800 | [diff] [blame] | 607 | singleton_new_instances_.push_back(new_instance); |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 608 | } |
Vladimir Marko | 009d166 | 2017-10-10 13:21:15 +0100 | [diff] [blame] | 609 | ScopedArenaVector<HInstruction*>& heap_values = |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 610 | heap_values_for_[new_instance->GetBlock()->GetBlockId()]; |
| 611 | for (size_t i = 0; i < heap_values.size(); i++) { |
| 612 | HInstruction* ref = |
| 613 | heap_location_collector_.GetHeapLocation(i)->GetReferenceInfo()->GetReference(); |
| 614 | size_t offset = heap_location_collector_.GetHeapLocation(i)->GetOffset(); |
| 615 | if (ref == new_instance && offset >= mirror::kObjectHeaderSize) { |
| 616 | // Instance fields except the header fields are set to default heap values. |
| 617 | heap_values[i] = kDefaultHeapValue; |
| 618 | } |
| 619 | } |
| 620 | } |
| 621 | |
Mingyao Yang | 8697490 | 2017-03-01 14:03:51 -0800 | [diff] [blame] | 622 | void VisitNewArray(HNewArray* new_array) OVERRIDE { |
| 623 | ReferenceInfo* ref_info = heap_location_collector_.FindReferenceInfoOf(new_array); |
| 624 | if (ref_info == nullptr) { |
| 625 | // new_array isn't used for array accesses. No need to process it. |
| 626 | return; |
| 627 | } |
| 628 | if (ref_info->IsSingletonAndRemovable()) { |
| 629 | singleton_new_arrays_.push_back(new_array); |
| 630 | } |
Vladimir Marko | 009d166 | 2017-10-10 13:21:15 +0100 | [diff] [blame] | 631 | ScopedArenaVector<HInstruction*>& heap_values = |
Mingyao Yang | 8697490 | 2017-03-01 14:03:51 -0800 | [diff] [blame] | 632 | heap_values_for_[new_array->GetBlock()->GetBlockId()]; |
| 633 | for (size_t i = 0; i < heap_values.size(); i++) { |
| 634 | HeapLocation* location = heap_location_collector_.GetHeapLocation(i); |
| 635 | HInstruction* ref = location->GetReferenceInfo()->GetReference(); |
| 636 | if (ref == new_array && location->GetIndex() != nullptr) { |
| 637 | // Array elements are set to default heap values. |
| 638 | heap_values[i] = kDefaultHeapValue; |
| 639 | } |
| 640 | } |
| 641 | } |
| 642 | |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 643 | // Find an instruction's substitute if it should be removed. |
| 644 | // Return the same instruction if it should not be removed. |
| 645 | HInstruction* FindSubstitute(HInstruction* instruction) { |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 646 | size_t size = removed_loads_.size(); |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 647 | for (size_t i = 0; i < size; i++) { |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 648 | if (removed_loads_[i] == instruction) { |
| 649 | return substitute_instructions_for_loads_[i]; |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 650 | } |
| 651 | } |
| 652 | return instruction; |
| 653 | } |
| 654 | |
| 655 | const HeapLocationCollector& heap_location_collector_; |
| 656 | const SideEffectsAnalysis& side_effects_; |
| 657 | |
Vladimir Marko | 009d166 | 2017-10-10 13:21:15 +0100 | [diff] [blame] | 658 | // Use local allocator for allocating memory. |
| 659 | ScopedArenaAllocator allocator_; |
| 660 | |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 661 | // One array of heap values for each block. |
Vladimir Marko | 009d166 | 2017-10-10 13:21:15 +0100 | [diff] [blame] | 662 | ScopedArenaVector<ScopedArenaVector<HInstruction*>> heap_values_for_; |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 663 | |
| 664 | // We record the instructions that should be eliminated but may be |
| 665 | // used by heap locations. They'll be removed in the end. |
Vladimir Marko | 009d166 | 2017-10-10 13:21:15 +0100 | [diff] [blame] | 666 | ScopedArenaVector<HInstruction*> removed_loads_; |
| 667 | ScopedArenaVector<HInstruction*> substitute_instructions_for_loads_; |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 668 | |
| 669 | // Stores in this list may be removed from the list later when it's |
| 670 | // found that the store cannot be eliminated. |
Vladimir Marko | 009d166 | 2017-10-10 13:21:15 +0100 | [diff] [blame] | 671 | ScopedArenaVector<HInstruction*> possibly_removed_stores_; |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 672 | |
Vladimir Marko | 009d166 | 2017-10-10 13:21:15 +0100 | [diff] [blame] | 673 | ScopedArenaVector<HInstruction*> singleton_new_instances_; |
| 674 | ScopedArenaVector<HInstruction*> singleton_new_arrays_; |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 675 | |
| 676 | DISALLOW_COPY_AND_ASSIGN(LSEVisitor); |
| 677 | }; |
| 678 | |
| 679 | void LoadStoreElimination::Run() { |
David Brazdil | 8993caf | 2015-12-07 10:04:40 +0000 | [diff] [blame] | 680 | if (graph_->IsDebuggable() || graph_->HasTryCatch()) { |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 681 | // Debugger may set heap values or trigger deoptimization of callers. |
David Brazdil | 8993caf | 2015-12-07 10:04:40 +0000 | [diff] [blame] | 682 | // Try/catch support not implemented yet. |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 683 | // Skip this optimization. |
| 684 | return; |
| 685 | } |
xueliang.zhong | c239a2b | 2017-04-27 15:31:37 +0100 | [diff] [blame] | 686 | const HeapLocationCollector& heap_location_collector = lsa_.GetHeapLocationCollector(); |
| 687 | if (heap_location_collector.GetNumberOfHeapLocations() == 0) { |
| 688 | // No HeapLocation information from LSA, skip this optimization. |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 689 | return; |
| 690 | } |
xueliang.zhong | c239a2b | 2017-04-27 15:31:37 +0100 | [diff] [blame] | 691 | |
Nicolas Geoffray | 8c4ddb2 | 2017-11-13 11:49:53 +0000 | [diff] [blame] | 692 | // TODO: analyze VecLoad/VecStore better. |
| 693 | if (graph_->HasSIMD()) { |
| 694 | return; |
| 695 | } |
| 696 | |
Igor Murashkin | 6ef4567 | 2017-08-08 13:59:55 -0700 | [diff] [blame] | 697 | LSEVisitor lse_visitor(graph_, heap_location_collector, side_effects_, stats_); |
Vladimir Marko | 2c45bc9 | 2016-10-25 16:54:12 +0100 | [diff] [blame] | 698 | for (HBasicBlock* block : graph_->GetReversePostOrder()) { |
| 699 | lse_visitor.VisitBasicBlock(block); |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 700 | } |
| 701 | lse_visitor.RemoveInstructions(); |
| 702 | } |
| 703 | |
| 704 | } // namespace art |