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