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" |
| 18 | #include "side_effects_analysis.h" |
| 19 | |
| 20 | #include <iostream> |
| 21 | |
| 22 | namespace art { |
| 23 | |
| 24 | class ReferenceInfo; |
| 25 | |
| 26 | // A cap for the number of heap locations to prevent pathological time/space consumption. |
| 27 | // The number of heap locations for most of the methods stays below this threshold. |
| 28 | constexpr size_t kMaxNumberOfHeapLocations = 32; |
| 29 | |
| 30 | // A ReferenceInfo contains additional info about a reference such as |
| 31 | // whether it's a singleton, returned, etc. |
| 32 | class ReferenceInfo : public ArenaObject<kArenaAllocMisc> { |
| 33 | public: |
| 34 | ReferenceInfo(HInstruction* reference, size_t pos) : reference_(reference), position_(pos) { |
| 35 | is_singleton_ = true; |
| 36 | is_singleton_and_not_returned_ = true; |
| 37 | if (!reference_->IsNewInstance() && !reference_->IsNewArray()) { |
| 38 | // For references not allocated in the method, don't assume anything. |
| 39 | is_singleton_ = false; |
| 40 | is_singleton_and_not_returned_ = false; |
| 41 | return; |
| 42 | } |
| 43 | |
| 44 | // Visit all uses to determine if this reference can spread into the heap, |
| 45 | // a method call, etc. |
| 46 | for (HUseIterator<HInstruction*> use_it(reference_->GetUses()); |
| 47 | !use_it.Done(); |
| 48 | use_it.Advance()) { |
| 49 | HInstruction* use = use_it.Current()->GetUser(); |
| 50 | DCHECK(!use->IsNullCheck()) << "NullCheck should have been eliminated"; |
| 51 | if (use->IsBoundType()) { |
| 52 | // BoundType shouldn't normally be necessary for a NewInstance. |
| 53 | // Just be conservative for the uncommon cases. |
| 54 | is_singleton_ = false; |
| 55 | is_singleton_and_not_returned_ = false; |
| 56 | return; |
| 57 | } |
Mingyao Yang | e5c71f9 | 2016-02-02 20:10:32 -0800 | [diff] [blame] | 58 | if (use->IsPhi() || use->IsSelect() || use->IsInvoke() || |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 59 | (use->IsInstanceFieldSet() && (reference_ == use->InputAt(1))) || |
| 60 | (use->IsUnresolvedInstanceFieldSet() && (reference_ == use->InputAt(1))) || |
| 61 | (use->IsStaticFieldSet() && (reference_ == use->InputAt(1))) || |
Nicolas Geoffray | d930929 | 2015-10-31 22:21:31 +0000 | [diff] [blame] | 62 | (use->IsUnresolvedStaticFieldSet() && (reference_ == use->InputAt(0))) || |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 63 | (use->IsArraySet() && (reference_ == use->InputAt(2)))) { |
Mingyao Yang | e5c71f9 | 2016-02-02 20:10:32 -0800 | [diff] [blame] | 64 | // reference_ is merged to a phi/HSelect, passed to a callee, or stored to heap. |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 65 | // reference_ isn't the only name that can refer to its value anymore. |
| 66 | is_singleton_ = false; |
| 67 | is_singleton_and_not_returned_ = false; |
| 68 | return; |
| 69 | } |
| 70 | if (use->IsReturn()) { |
| 71 | is_singleton_and_not_returned_ = false; |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | HInstruction* GetReference() const { |
| 77 | return reference_; |
| 78 | } |
| 79 | |
| 80 | size_t GetPosition() const { |
| 81 | return position_; |
| 82 | } |
| 83 | |
| 84 | // Returns true if reference_ is the only name that can refer to its value during |
| 85 | // the lifetime of the method. So it's guaranteed to not have any alias in |
| 86 | // the method (including its callees). |
| 87 | bool IsSingleton() const { |
| 88 | return is_singleton_; |
| 89 | } |
| 90 | |
| 91 | // Returns true if reference_ is a singleton and not returned to the caller. |
| 92 | // The allocation and stores into reference_ may be eliminated for such cases. |
| 93 | bool IsSingletonAndNotReturned() const { |
| 94 | return is_singleton_and_not_returned_; |
| 95 | } |
| 96 | |
| 97 | private: |
| 98 | HInstruction* const reference_; |
| 99 | const size_t position_; // position in HeapLocationCollector's ref_info_array_. |
| 100 | bool is_singleton_; // can only be referred to by a single name in the method. |
| 101 | bool is_singleton_and_not_returned_; // reference_ is singleton and not returned to caller. |
| 102 | |
| 103 | DISALLOW_COPY_AND_ASSIGN(ReferenceInfo); |
| 104 | }; |
| 105 | |
| 106 | // A heap location is a reference-offset/index pair that a value can be loaded from |
| 107 | // or stored to. |
| 108 | class HeapLocation : public ArenaObject<kArenaAllocMisc> { |
| 109 | public: |
| 110 | static constexpr size_t kInvalidFieldOffset = -1; |
| 111 | |
| 112 | // TODO: more fine-grained array types. |
| 113 | static constexpr int16_t kDeclaringClassDefIndexForArrays = -1; |
| 114 | |
| 115 | HeapLocation(ReferenceInfo* ref_info, |
| 116 | size_t offset, |
| 117 | HInstruction* index, |
| 118 | int16_t declaring_class_def_index) |
| 119 | : ref_info_(ref_info), |
| 120 | offset_(offset), |
| 121 | index_(index), |
Mingyao Yang | 803cbb9 | 2015-12-01 12:24:36 -0800 | [diff] [blame] | 122 | declaring_class_def_index_(declaring_class_def_index), |
| 123 | value_killed_by_loop_side_effects_(true) { |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 124 | DCHECK(ref_info != nullptr); |
| 125 | DCHECK((offset == kInvalidFieldOffset && index != nullptr) || |
| 126 | (offset != kInvalidFieldOffset && index == nullptr)); |
Mingyao Yang | 803cbb9 | 2015-12-01 12:24:36 -0800 | [diff] [blame] | 127 | if (ref_info->IsSingleton() && !IsArrayElement()) { |
| 128 | // Assume this location's value cannot be killed by loop side effects |
| 129 | // until proven otherwise. |
| 130 | value_killed_by_loop_side_effects_ = false; |
| 131 | } |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | ReferenceInfo* GetReferenceInfo() const { return ref_info_; } |
| 135 | size_t GetOffset() const { return offset_; } |
| 136 | HInstruction* GetIndex() const { return index_; } |
| 137 | |
| 138 | // Returns the definition of declaring class' dex index. |
| 139 | // It's kDeclaringClassDefIndexForArrays for an array element. |
| 140 | int16_t GetDeclaringClassDefIndex() const { |
| 141 | return declaring_class_def_index_; |
| 142 | } |
| 143 | |
| 144 | bool IsArrayElement() const { |
| 145 | return index_ != nullptr; |
| 146 | } |
| 147 | |
Mingyao Yang | 803cbb9 | 2015-12-01 12:24:36 -0800 | [diff] [blame] | 148 | bool IsValueKilledByLoopSideEffects() const { |
| 149 | return value_killed_by_loop_side_effects_; |
| 150 | } |
| 151 | |
| 152 | void SetValueKilledByLoopSideEffects(bool val) { |
| 153 | value_killed_by_loop_side_effects_ = val; |
| 154 | } |
| 155 | |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 156 | private: |
| 157 | ReferenceInfo* const ref_info_; // reference for instance/static field or array access. |
| 158 | const size_t offset_; // offset of static/instance field. |
| 159 | HInstruction* const index_; // index of an array element. |
| 160 | const int16_t declaring_class_def_index_; // declaring class's def's dex index. |
Mingyao Yang | 803cbb9 | 2015-12-01 12:24:36 -0800 | [diff] [blame] | 161 | bool value_killed_by_loop_side_effects_; // value of this location may be killed by loop |
| 162 | // side effects because this location is stored |
| 163 | // into inside a loop. |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 164 | |
| 165 | DISALLOW_COPY_AND_ASSIGN(HeapLocation); |
| 166 | }; |
| 167 | |
| 168 | static HInstruction* HuntForOriginalReference(HInstruction* ref) { |
| 169 | DCHECK(ref != nullptr); |
| 170 | while (ref->IsNullCheck() || ref->IsBoundType()) { |
| 171 | ref = ref->InputAt(0); |
| 172 | } |
| 173 | return ref; |
| 174 | } |
| 175 | |
| 176 | // A HeapLocationCollector collects all relevant heap locations and keeps |
| 177 | // an aliasing matrix for all locations. |
| 178 | class HeapLocationCollector : public HGraphVisitor { |
| 179 | public: |
| 180 | static constexpr size_t kHeapLocationNotFound = -1; |
| 181 | // Start with a single uint32_t word. That's enough bits for pair-wise |
| 182 | // aliasing matrix of 8 heap locations. |
| 183 | static constexpr uint32_t kInitialAliasingMatrixBitVectorSize = 32; |
| 184 | |
| 185 | explicit HeapLocationCollector(HGraph* graph) |
| 186 | : HGraphVisitor(graph), |
| 187 | ref_info_array_(graph->GetArena()->Adapter(kArenaAllocLSE)), |
| 188 | heap_locations_(graph->GetArena()->Adapter(kArenaAllocLSE)), |
| 189 | aliasing_matrix_(graph->GetArena(), kInitialAliasingMatrixBitVectorSize, true), |
| 190 | has_heap_stores_(false), |
| 191 | has_volatile_(false), |
| 192 | has_monitor_operations_(false), |
| 193 | may_deoptimize_(false) {} |
| 194 | |
| 195 | size_t GetNumberOfHeapLocations() const { |
| 196 | return heap_locations_.size(); |
| 197 | } |
| 198 | |
| 199 | HeapLocation* GetHeapLocation(size_t index) const { |
| 200 | return heap_locations_[index]; |
| 201 | } |
| 202 | |
| 203 | ReferenceInfo* FindReferenceInfoOf(HInstruction* ref) const { |
| 204 | for (size_t i = 0; i < ref_info_array_.size(); i++) { |
| 205 | ReferenceInfo* ref_info = ref_info_array_[i]; |
| 206 | if (ref_info->GetReference() == ref) { |
| 207 | DCHECK_EQ(i, ref_info->GetPosition()); |
| 208 | return ref_info; |
| 209 | } |
| 210 | } |
| 211 | return nullptr; |
| 212 | } |
| 213 | |
| 214 | bool HasHeapStores() const { |
| 215 | return has_heap_stores_; |
| 216 | } |
| 217 | |
| 218 | bool HasVolatile() const { |
| 219 | return has_volatile_; |
| 220 | } |
| 221 | |
| 222 | bool HasMonitorOps() const { |
| 223 | return has_monitor_operations_; |
| 224 | } |
| 225 | |
| 226 | // Returns whether this method may be deoptimized. |
| 227 | // Currently we don't have meta data support for deoptimizing |
| 228 | // a method that eliminates allocations/stores. |
| 229 | bool MayDeoptimize() const { |
| 230 | return may_deoptimize_; |
| 231 | } |
| 232 | |
| 233 | // Find and return the heap location index in heap_locations_. |
| 234 | size_t FindHeapLocationIndex(ReferenceInfo* ref_info, |
| 235 | size_t offset, |
| 236 | HInstruction* index, |
| 237 | int16_t declaring_class_def_index) const { |
| 238 | for (size_t i = 0; i < heap_locations_.size(); i++) { |
| 239 | HeapLocation* loc = heap_locations_[i]; |
| 240 | if (loc->GetReferenceInfo() == ref_info && |
| 241 | loc->GetOffset() == offset && |
| 242 | loc->GetIndex() == index && |
| 243 | loc->GetDeclaringClassDefIndex() == declaring_class_def_index) { |
| 244 | return i; |
| 245 | } |
| 246 | } |
| 247 | return kHeapLocationNotFound; |
| 248 | } |
| 249 | |
| 250 | // Returns true if heap_locations_[index1] and heap_locations_[index2] may alias. |
| 251 | bool MayAlias(size_t index1, size_t index2) const { |
| 252 | if (index1 < index2) { |
| 253 | return aliasing_matrix_.IsBitSet(AliasingMatrixPosition(index1, index2)); |
| 254 | } else if (index1 > index2) { |
| 255 | return aliasing_matrix_.IsBitSet(AliasingMatrixPosition(index2, index1)); |
| 256 | } else { |
| 257 | DCHECK(false) << "index1 and index2 are expected to be different"; |
| 258 | return true; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | void BuildAliasingMatrix() { |
| 263 | const size_t number_of_locations = heap_locations_.size(); |
| 264 | if (number_of_locations == 0) { |
| 265 | return; |
| 266 | } |
| 267 | size_t pos = 0; |
| 268 | // Compute aliasing info between every pair of different heap locations. |
| 269 | // Save the result in a matrix represented as a BitVector. |
| 270 | for (size_t i = 0; i < number_of_locations - 1; i++) { |
| 271 | for (size_t j = i + 1; j < number_of_locations; j++) { |
| 272 | if (ComputeMayAlias(i, j)) { |
| 273 | aliasing_matrix_.SetBit(CheckedAliasingMatrixPosition(i, j, pos)); |
| 274 | } |
| 275 | pos++; |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | private: |
| 281 | // An allocation cannot alias with a name which already exists at the point |
| 282 | // of the allocation, such as a parameter or a load happening before the allocation. |
| 283 | bool MayAliasWithPreexistenceChecking(ReferenceInfo* ref_info1, ReferenceInfo* ref_info2) const { |
| 284 | if (ref_info1->GetReference()->IsNewInstance() || ref_info1->GetReference()->IsNewArray()) { |
| 285 | // Any reference that can alias with the allocation must appear after it in the block/in |
| 286 | // the block's successors. In reverse post order, those instructions will be visited after |
| 287 | // the allocation. |
| 288 | return ref_info2->GetPosition() >= ref_info1->GetPosition(); |
| 289 | } |
| 290 | return true; |
| 291 | } |
| 292 | |
| 293 | bool CanReferencesAlias(ReferenceInfo* ref_info1, ReferenceInfo* ref_info2) const { |
| 294 | if (ref_info1 == ref_info2) { |
| 295 | return true; |
| 296 | } else if (ref_info1->IsSingleton()) { |
| 297 | return false; |
| 298 | } else if (ref_info2->IsSingleton()) { |
| 299 | return false; |
| 300 | } else if (!MayAliasWithPreexistenceChecking(ref_info1, ref_info2) || |
| 301 | !MayAliasWithPreexistenceChecking(ref_info2, ref_info1)) { |
| 302 | return false; |
| 303 | } |
| 304 | return true; |
| 305 | } |
| 306 | |
| 307 | // `index1` and `index2` are indices in the array of collected heap locations. |
| 308 | // Returns the position in the bit vector that tracks whether the two heap |
| 309 | // locations may alias. |
| 310 | size_t AliasingMatrixPosition(size_t index1, size_t index2) const { |
| 311 | DCHECK(index2 > index1); |
| 312 | const size_t number_of_locations = heap_locations_.size(); |
| 313 | // It's (num_of_locations - 1) + ... + (num_of_locations - index1) + (index2 - index1 - 1). |
| 314 | return (number_of_locations * index1 - (1 + index1) * index1 / 2 + (index2 - index1 - 1)); |
| 315 | } |
| 316 | |
| 317 | // An additional position is passed in to make sure the calculated position is correct. |
| 318 | size_t CheckedAliasingMatrixPosition(size_t index1, size_t index2, size_t position) { |
| 319 | size_t calculated_position = AliasingMatrixPosition(index1, index2); |
| 320 | DCHECK_EQ(calculated_position, position); |
| 321 | return calculated_position; |
| 322 | } |
| 323 | |
| 324 | // Compute if two locations may alias to each other. |
| 325 | bool ComputeMayAlias(size_t index1, size_t index2) const { |
| 326 | HeapLocation* loc1 = heap_locations_[index1]; |
| 327 | HeapLocation* loc2 = heap_locations_[index2]; |
| 328 | if (loc1->GetOffset() != loc2->GetOffset()) { |
| 329 | // Either two different instance fields, or one is an instance |
| 330 | // field and the other is an array element. |
| 331 | return false; |
| 332 | } |
| 333 | if (loc1->GetDeclaringClassDefIndex() != loc2->GetDeclaringClassDefIndex()) { |
| 334 | // Different types. |
| 335 | return false; |
| 336 | } |
| 337 | if (!CanReferencesAlias(loc1->GetReferenceInfo(), loc2->GetReferenceInfo())) { |
| 338 | return false; |
| 339 | } |
| 340 | if (loc1->IsArrayElement() && loc2->IsArrayElement()) { |
| 341 | HInstruction* array_index1 = loc1->GetIndex(); |
| 342 | HInstruction* array_index2 = loc2->GetIndex(); |
| 343 | DCHECK(array_index1 != nullptr); |
| 344 | DCHECK(array_index2 != nullptr); |
| 345 | if (array_index1->IsIntConstant() && |
| 346 | array_index2->IsIntConstant() && |
| 347 | array_index1->AsIntConstant()->GetValue() != array_index2->AsIntConstant()->GetValue()) { |
| 348 | // Different constant indices do not alias. |
| 349 | return false; |
| 350 | } |
| 351 | } |
| 352 | return true; |
| 353 | } |
| 354 | |
Mingyao Yang | 8ab1d64 | 2015-12-03 14:11:15 -0800 | [diff] [blame] | 355 | ReferenceInfo* GetOrCreateReferenceInfo(HInstruction* instruction) { |
| 356 | ReferenceInfo* ref_info = FindReferenceInfoOf(instruction); |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 357 | if (ref_info == nullptr) { |
| 358 | size_t pos = ref_info_array_.size(); |
Mingyao Yang | 8ab1d64 | 2015-12-03 14:11:15 -0800 | [diff] [blame] | 359 | ref_info = new (GetGraph()->GetArena()) ReferenceInfo(instruction, pos); |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 360 | ref_info_array_.push_back(ref_info); |
| 361 | } |
| 362 | return ref_info; |
| 363 | } |
| 364 | |
Mingyao Yang | 8ab1d64 | 2015-12-03 14:11:15 -0800 | [diff] [blame] | 365 | void CreateReferenceInfoForReferenceType(HInstruction* instruction) { |
| 366 | if (instruction->GetType() != Primitive::kPrimNot) { |
| 367 | return; |
| 368 | } |
| 369 | DCHECK(FindReferenceInfoOf(instruction) == nullptr); |
| 370 | GetOrCreateReferenceInfo(instruction); |
| 371 | } |
| 372 | |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 373 | HeapLocation* GetOrCreateHeapLocation(HInstruction* ref, |
| 374 | size_t offset, |
| 375 | HInstruction* index, |
| 376 | int16_t declaring_class_def_index) { |
| 377 | HInstruction* original_ref = HuntForOriginalReference(ref); |
| 378 | ReferenceInfo* ref_info = GetOrCreateReferenceInfo(original_ref); |
| 379 | size_t heap_location_idx = FindHeapLocationIndex( |
| 380 | ref_info, offset, index, declaring_class_def_index); |
| 381 | if (heap_location_idx == kHeapLocationNotFound) { |
| 382 | HeapLocation* heap_loc = new (GetGraph()->GetArena()) |
| 383 | HeapLocation(ref_info, offset, index, declaring_class_def_index); |
| 384 | heap_locations_.push_back(heap_loc); |
| 385 | return heap_loc; |
| 386 | } |
| 387 | return heap_locations_[heap_location_idx]; |
| 388 | } |
| 389 | |
Mingyao Yang | 803cbb9 | 2015-12-01 12:24:36 -0800 | [diff] [blame] | 390 | HeapLocation* VisitFieldAccess(HInstruction* ref, const FieldInfo& field_info) { |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 391 | if (field_info.IsVolatile()) { |
| 392 | has_volatile_ = true; |
| 393 | } |
| 394 | const uint16_t declaring_class_def_index = field_info.GetDeclaringClassDefIndex(); |
| 395 | const size_t offset = field_info.GetFieldOffset().SizeValue(); |
Mingyao Yang | 803cbb9 | 2015-12-01 12:24:36 -0800 | [diff] [blame] | 396 | return GetOrCreateHeapLocation(ref, offset, nullptr, declaring_class_def_index); |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | void VisitArrayAccess(HInstruction* array, HInstruction* index) { |
| 400 | GetOrCreateHeapLocation(array, HeapLocation::kInvalidFieldOffset, |
| 401 | index, HeapLocation::kDeclaringClassDefIndexForArrays); |
| 402 | } |
| 403 | |
| 404 | void VisitInstanceFieldGet(HInstanceFieldGet* instruction) OVERRIDE { |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 405 | VisitFieldAccess(instruction->InputAt(0), instruction->GetFieldInfo()); |
Mingyao Yang | 8ab1d64 | 2015-12-03 14:11:15 -0800 | [diff] [blame] | 406 | CreateReferenceInfoForReferenceType(instruction); |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | void VisitInstanceFieldSet(HInstanceFieldSet* instruction) OVERRIDE { |
Mingyao Yang | 803cbb9 | 2015-12-01 12:24:36 -0800 | [diff] [blame] | 410 | HeapLocation* location = VisitFieldAccess(instruction->InputAt(0), instruction->GetFieldInfo()); |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 411 | has_heap_stores_ = true; |
Mingyao Yang | 803cbb9 | 2015-12-01 12:24:36 -0800 | [diff] [blame] | 412 | if (instruction->GetBlock()->GetLoopInformation() != nullptr) { |
| 413 | location->SetValueKilledByLoopSideEffects(true); |
| 414 | } |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | void VisitStaticFieldGet(HStaticFieldGet* instruction) OVERRIDE { |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 418 | VisitFieldAccess(instruction->InputAt(0), instruction->GetFieldInfo()); |
Mingyao Yang | 8ab1d64 | 2015-12-03 14:11:15 -0800 | [diff] [blame] | 419 | CreateReferenceInfoForReferenceType(instruction); |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | void VisitStaticFieldSet(HStaticFieldSet* instruction) OVERRIDE { |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 423 | VisitFieldAccess(instruction->InputAt(0), instruction->GetFieldInfo()); |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 424 | has_heap_stores_ = true; |
| 425 | } |
| 426 | |
| 427 | // We intentionally don't collect HUnresolvedInstanceField/HUnresolvedStaticField accesses |
| 428 | // since we cannot accurately track the fields. |
| 429 | |
| 430 | void VisitArrayGet(HArrayGet* instruction) OVERRIDE { |
| 431 | VisitArrayAccess(instruction->InputAt(0), instruction->InputAt(1)); |
Mingyao Yang | 8ab1d64 | 2015-12-03 14:11:15 -0800 | [diff] [blame] | 432 | CreateReferenceInfoForReferenceType(instruction); |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | void VisitArraySet(HArraySet* instruction) OVERRIDE { |
| 436 | VisitArrayAccess(instruction->InputAt(0), instruction->InputAt(1)); |
| 437 | has_heap_stores_ = true; |
| 438 | } |
| 439 | |
| 440 | void VisitNewInstance(HNewInstance* new_instance) OVERRIDE { |
| 441 | // Any references appearing in the ref_info_array_ so far cannot alias with new_instance. |
Mingyao Yang | 8ab1d64 | 2015-12-03 14:11:15 -0800 | [diff] [blame] | 442 | CreateReferenceInfoForReferenceType(new_instance); |
| 443 | } |
| 444 | |
| 445 | void VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* instruction) OVERRIDE { |
| 446 | CreateReferenceInfoForReferenceType(instruction); |
| 447 | } |
| 448 | |
| 449 | void VisitInvokeVirtual(HInvokeVirtual* instruction) OVERRIDE { |
| 450 | CreateReferenceInfoForReferenceType(instruction); |
| 451 | } |
| 452 | |
| 453 | void VisitInvokeInterface(HInvokeInterface* instruction) OVERRIDE { |
| 454 | CreateReferenceInfoForReferenceType(instruction); |
| 455 | } |
| 456 | |
| 457 | void VisitParameterValue(HParameterValue* instruction) OVERRIDE { |
| 458 | CreateReferenceInfoForReferenceType(instruction); |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | void VisitDeoptimize(HDeoptimize* instruction ATTRIBUTE_UNUSED) OVERRIDE { |
| 462 | may_deoptimize_ = true; |
| 463 | } |
| 464 | |
| 465 | void VisitMonitorOperation(HMonitorOperation* monitor ATTRIBUTE_UNUSED) OVERRIDE { |
| 466 | has_monitor_operations_ = true; |
| 467 | } |
| 468 | |
| 469 | ArenaVector<ReferenceInfo*> ref_info_array_; // All references used for heap accesses. |
| 470 | ArenaVector<HeapLocation*> heap_locations_; // All heap locations. |
| 471 | ArenaBitVector aliasing_matrix_; // aliasing info between each pair of locations. |
| 472 | bool has_heap_stores_; // If there is no heap stores, LSE acts as GVN with better |
| 473 | // alias analysis and won't be as effective. |
| 474 | bool has_volatile_; // If there are volatile field accesses. |
| 475 | bool has_monitor_operations_; // If there are monitor operations. |
| 476 | bool may_deoptimize_; |
| 477 | |
| 478 | DISALLOW_COPY_AND_ASSIGN(HeapLocationCollector); |
| 479 | }; |
| 480 | |
| 481 | // 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] | 482 | // A heap location can be set to kUnknownHeapValue when: |
| 483 | // - initially set a value. |
| 484 | // - killed due to aliasing, merging, invocation, or loop side effects. |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 485 | static HInstruction* const kUnknownHeapValue = |
| 486 | reinterpret_cast<HInstruction*>(static_cast<uintptr_t>(-1)); |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 487 | |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 488 | // Default heap value after an allocation. |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 489 | // 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] | 490 | static HInstruction* const kDefaultHeapValue = |
| 491 | reinterpret_cast<HInstruction*>(static_cast<uintptr_t>(-2)); |
| 492 | |
| 493 | class LSEVisitor : public HGraphVisitor { |
| 494 | public: |
| 495 | LSEVisitor(HGraph* graph, |
| 496 | const HeapLocationCollector& heap_locations_collector, |
| 497 | const SideEffectsAnalysis& side_effects) |
| 498 | : HGraphVisitor(graph), |
| 499 | heap_location_collector_(heap_locations_collector), |
| 500 | side_effects_(side_effects), |
| 501 | heap_values_for_(graph->GetBlocks().size(), |
| 502 | ArenaVector<HInstruction*>(heap_locations_collector. |
| 503 | GetNumberOfHeapLocations(), |
| 504 | kUnknownHeapValue, |
| 505 | graph->GetArena()->Adapter(kArenaAllocLSE)), |
| 506 | graph->GetArena()->Adapter(kArenaAllocLSE)), |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 507 | removed_loads_(graph->GetArena()->Adapter(kArenaAllocLSE)), |
| 508 | substitute_instructions_for_loads_(graph->GetArena()->Adapter(kArenaAllocLSE)), |
| 509 | possibly_removed_stores_(graph->GetArena()->Adapter(kArenaAllocLSE)), |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 510 | singleton_new_instances_(graph->GetArena()->Adapter(kArenaAllocLSE)) { |
| 511 | } |
| 512 | |
| 513 | void VisitBasicBlock(HBasicBlock* block) OVERRIDE { |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 514 | // Populate the heap_values array for this block. |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 515 | // TODO: try to reuse the heap_values array from one predecessor if possible. |
| 516 | if (block->IsLoopHeader()) { |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 517 | HandleLoopSideEffects(block); |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 518 | } else { |
| 519 | MergePredecessorValues(block); |
| 520 | } |
| 521 | HGraphVisitor::VisitBasicBlock(block); |
| 522 | } |
| 523 | |
| 524 | // Remove recorded instructions that should be eliminated. |
| 525 | void RemoveInstructions() { |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 526 | size_t size = removed_loads_.size(); |
| 527 | DCHECK_EQ(size, substitute_instructions_for_loads_.size()); |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 528 | for (size_t i = 0; i < size; i++) { |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 529 | HInstruction* load = removed_loads_[i]; |
| 530 | DCHECK(load != nullptr); |
| 531 | DCHECK(load->IsInstanceFieldGet() || |
| 532 | load->IsStaticFieldGet() || |
| 533 | load->IsArrayGet()); |
| 534 | HInstruction* substitute = substitute_instructions_for_loads_[i]; |
| 535 | DCHECK(substitute != nullptr); |
| 536 | // Keep tracing substitute till one that's not removed. |
| 537 | HInstruction* sub_sub = FindSubstitute(substitute); |
| 538 | while (sub_sub != substitute) { |
| 539 | substitute = sub_sub; |
| 540 | sub_sub = FindSubstitute(substitute); |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 541 | } |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 542 | load->ReplaceWith(substitute); |
| 543 | load->GetBlock()->RemoveInstruction(load); |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 544 | } |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 545 | |
| 546 | // At this point, stores in possibly_removed_stores_ can be safely removed. |
| 547 | size = possibly_removed_stores_.size(); |
| 548 | for (size_t i = 0; i < size; i++) { |
| 549 | HInstruction* store = possibly_removed_stores_[i]; |
| 550 | DCHECK(store->IsInstanceFieldSet() || store->IsStaticFieldSet() || store->IsArraySet()); |
| 551 | store->GetBlock()->RemoveInstruction(store); |
| 552 | } |
| 553 | |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 554 | // TODO: remove unnecessary allocations. |
| 555 | // Eliminate instructions in singleton_new_instances_ that: |
| 556 | // - don't have uses, |
| 557 | // - don't have finalizers, |
| 558 | // - are instantiable and accessible, |
| 559 | // - have no/separate clinit check. |
| 560 | } |
| 561 | |
| 562 | private: |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 563 | // If heap_values[index] is an instance field store, need to keep the store. |
| 564 | // This is necessary if a heap value is killed due to merging, or loop side |
| 565 | // effects (which is essentially merging also), since a load later from the |
| 566 | // location won't be eliminated. |
| 567 | void KeepIfIsStore(HInstruction* heap_value) { |
| 568 | if (heap_value == kDefaultHeapValue || |
| 569 | heap_value == kUnknownHeapValue || |
| 570 | !heap_value->IsInstanceFieldSet()) { |
| 571 | return; |
| 572 | } |
| 573 | auto idx = std::find(possibly_removed_stores_.begin(), |
| 574 | possibly_removed_stores_.end(), heap_value); |
| 575 | if (idx != possibly_removed_stores_.end()) { |
| 576 | // Make sure the store is kept. |
| 577 | possibly_removed_stores_.erase(idx); |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | void HandleLoopSideEffects(HBasicBlock* block) { |
| 582 | DCHECK(block->IsLoopHeader()); |
| 583 | int block_id = block->GetBlockId(); |
| 584 | ArenaVector<HInstruction*>& heap_values = heap_values_for_[block_id]; |
Nicolas Geoffray | 15bd228 | 2016-01-05 15:55:41 +0000 | [diff] [blame] | 585 | |
| 586 | // Don't eliminate loads in irreducible loops. This is safe for singletons, because |
| 587 | // they are always used by the non-eliminated loop-phi. |
| 588 | if (block->GetLoopInformation()->IsIrreducible()) { |
| 589 | if (kIsDebugBuild) { |
| 590 | for (size_t i = 0; i < heap_values.size(); i++) { |
| 591 | DCHECK_EQ(heap_values[i], kUnknownHeapValue); |
| 592 | } |
| 593 | } |
| 594 | return; |
| 595 | } |
| 596 | |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 597 | HBasicBlock* pre_header = block->GetLoopInformation()->GetPreHeader(); |
| 598 | ArenaVector<HInstruction*>& pre_header_heap_values = |
| 599 | heap_values_for_[pre_header->GetBlockId()]; |
Nicolas Geoffray | 15bd228 | 2016-01-05 15:55:41 +0000 | [diff] [blame] | 600 | |
Mingyao Yang | 803cbb9 | 2015-12-01 12:24:36 -0800 | [diff] [blame] | 601 | // Inherit the values from pre-header. |
| 602 | for (size_t i = 0; i < heap_values.size(); i++) { |
| 603 | heap_values[i] = pre_header_heap_values[i]; |
| 604 | } |
| 605 | |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 606 | // We do a single pass in reverse post order. For loops, use the side effects as a hint |
| 607 | // to see if the heap values should be killed. |
| 608 | if (side_effects_.GetLoopEffects(block).DoesAnyWrite()) { |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 609 | for (size_t i = 0; i < heap_values.size(); i++) { |
Mingyao Yang | 803cbb9 | 2015-12-01 12:24:36 -0800 | [diff] [blame] | 610 | HeapLocation* location = heap_location_collector_.GetHeapLocation(i); |
| 611 | ReferenceInfo* ref_info = location->GetReferenceInfo(); |
| 612 | if (!ref_info->IsSingleton() || location->IsValueKilledByLoopSideEffects()) { |
| 613 | // heap value is killed by loop side effects (stored into directly, or due to |
| 614 | // aliasing). |
| 615 | KeepIfIsStore(pre_header_heap_values[i]); |
| 616 | heap_values[i] = kUnknownHeapValue; |
| 617 | } else { |
| 618 | // A singleton's field that's not stored into inside a loop is invariant throughout |
| 619 | // the loop. |
| 620 | } |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 621 | } |
| 622 | } |
| 623 | } |
| 624 | |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 625 | void MergePredecessorValues(HBasicBlock* block) { |
| 626 | const ArenaVector<HBasicBlock*>& predecessors = block->GetPredecessors(); |
| 627 | if (predecessors.size() == 0) { |
| 628 | return; |
| 629 | } |
| 630 | ArenaVector<HInstruction*>& heap_values = heap_values_for_[block->GetBlockId()]; |
| 631 | for (size_t i = 0; i < heap_values.size(); i++) { |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 632 | HInstruction* pred0_value = heap_values_for_[predecessors[0]->GetBlockId()][i]; |
| 633 | heap_values[i] = pred0_value; |
| 634 | if (pred0_value != kUnknownHeapValue) { |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 635 | for (size_t j = 1; j < predecessors.size(); j++) { |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 636 | HInstruction* pred_value = heap_values_for_[predecessors[j]->GetBlockId()][i]; |
| 637 | if (pred_value != pred0_value) { |
| 638 | heap_values[i] = kUnknownHeapValue; |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 639 | break; |
| 640 | } |
| 641 | } |
| 642 | } |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 643 | |
| 644 | if (heap_values[i] == kUnknownHeapValue) { |
| 645 | // Keep the last store in each predecessor since future loads cannot be eliminated. |
| 646 | for (size_t j = 0; j < predecessors.size(); j++) { |
| 647 | ArenaVector<HInstruction*>& pred_values = heap_values_for_[predecessors[j]->GetBlockId()]; |
| 648 | KeepIfIsStore(pred_values[i]); |
| 649 | } |
| 650 | } |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 651 | } |
| 652 | } |
| 653 | |
| 654 | // `instruction` is being removed. Try to see if the null check on it |
| 655 | // can be removed. This can happen if the same value is set in two branches |
| 656 | // but not in dominators. Such as: |
| 657 | // int[] a = foo(); |
| 658 | // if () { |
| 659 | // a[0] = 2; |
| 660 | // } else { |
| 661 | // a[0] = 2; |
| 662 | // } |
| 663 | // // a[0] can now be replaced with constant 2, and the null check on it can be removed. |
| 664 | void TryRemovingNullCheck(HInstruction* instruction) { |
| 665 | HInstruction* prev = instruction->GetPrevious(); |
| 666 | if ((prev != nullptr) && prev->IsNullCheck() && (prev == instruction->InputAt(0))) { |
| 667 | // Previous instruction is a null check for this instruction. Remove the null check. |
| 668 | prev->ReplaceWith(prev->InputAt(0)); |
| 669 | prev->GetBlock()->RemoveInstruction(prev); |
| 670 | } |
| 671 | } |
| 672 | |
| 673 | HInstruction* GetDefaultValue(Primitive::Type type) { |
| 674 | switch (type) { |
| 675 | case Primitive::kPrimNot: |
| 676 | return GetGraph()->GetNullConstant(); |
| 677 | case Primitive::kPrimBoolean: |
| 678 | case Primitive::kPrimByte: |
| 679 | case Primitive::kPrimChar: |
| 680 | case Primitive::kPrimShort: |
| 681 | case Primitive::kPrimInt: |
| 682 | return GetGraph()->GetIntConstant(0); |
| 683 | case Primitive::kPrimLong: |
| 684 | return GetGraph()->GetLongConstant(0); |
| 685 | case Primitive::kPrimFloat: |
| 686 | return GetGraph()->GetFloatConstant(0); |
| 687 | case Primitive::kPrimDouble: |
| 688 | return GetGraph()->GetDoubleConstant(0); |
| 689 | default: |
| 690 | UNREACHABLE(); |
| 691 | } |
| 692 | } |
| 693 | |
| 694 | void VisitGetLocation(HInstruction* instruction, |
| 695 | HInstruction* ref, |
| 696 | size_t offset, |
| 697 | HInstruction* index, |
| 698 | int16_t declaring_class_def_index) { |
| 699 | HInstruction* original_ref = HuntForOriginalReference(ref); |
| 700 | ReferenceInfo* ref_info = heap_location_collector_.FindReferenceInfoOf(original_ref); |
| 701 | size_t idx = heap_location_collector_.FindHeapLocationIndex( |
| 702 | ref_info, offset, index, declaring_class_def_index); |
| 703 | DCHECK_NE(idx, HeapLocationCollector::kHeapLocationNotFound); |
| 704 | ArenaVector<HInstruction*>& heap_values = |
| 705 | heap_values_for_[instruction->GetBlock()->GetBlockId()]; |
| 706 | HInstruction* heap_value = heap_values[idx]; |
| 707 | if (heap_value == kDefaultHeapValue) { |
| 708 | HInstruction* constant = GetDefaultValue(instruction->GetType()); |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 709 | removed_loads_.push_back(instruction); |
| 710 | substitute_instructions_for_loads_.push_back(constant); |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 711 | heap_values[idx] = constant; |
| 712 | return; |
| 713 | } |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 714 | if (heap_value != kUnknownHeapValue && heap_value->IsInstanceFieldSet()) { |
| 715 | HInstruction* store = heap_value; |
| 716 | // This load must be from a singleton since it's from the same field |
| 717 | // that a "removed" store puts the value. That store must be to a singleton's field. |
| 718 | DCHECK(ref_info->IsSingleton()); |
| 719 | // Get the real heap value of the store. |
| 720 | heap_value = store->InputAt(1); |
| 721 | } |
David Brazdil | 15693bf | 2015-12-16 10:30:45 +0000 | [diff] [blame] | 722 | if (heap_value == kUnknownHeapValue) { |
| 723 | // Load isn't eliminated. Put the load as the value into the HeapLocation. |
| 724 | // This acts like GVN but with better aliasing analysis. |
| 725 | heap_values[idx] = instruction; |
| 726 | } else { |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 727 | removed_loads_.push_back(instruction); |
| 728 | substitute_instructions_for_loads_.push_back(heap_value); |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 729 | TryRemovingNullCheck(instruction); |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 730 | } |
| 731 | } |
| 732 | |
| 733 | bool Equal(HInstruction* heap_value, HInstruction* value) { |
| 734 | if (heap_value == value) { |
| 735 | return true; |
| 736 | } |
| 737 | if (heap_value == kDefaultHeapValue && GetDefaultValue(value->GetType()) == value) { |
| 738 | return true; |
| 739 | } |
| 740 | return false; |
| 741 | } |
| 742 | |
| 743 | void VisitSetLocation(HInstruction* instruction, |
| 744 | HInstruction* ref, |
| 745 | size_t offset, |
| 746 | HInstruction* index, |
| 747 | int16_t declaring_class_def_index, |
| 748 | HInstruction* value) { |
| 749 | HInstruction* original_ref = HuntForOriginalReference(ref); |
| 750 | ReferenceInfo* ref_info = heap_location_collector_.FindReferenceInfoOf(original_ref); |
| 751 | size_t idx = heap_location_collector_.FindHeapLocationIndex( |
| 752 | ref_info, offset, index, declaring_class_def_index); |
| 753 | DCHECK_NE(idx, HeapLocationCollector::kHeapLocationNotFound); |
| 754 | ArenaVector<HInstruction*>& heap_values = |
| 755 | heap_values_for_[instruction->GetBlock()->GetBlockId()]; |
| 756 | HInstruction* heap_value = heap_values[idx]; |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 757 | bool same_value = false; |
| 758 | bool possibly_redundant = false; |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 759 | if (Equal(heap_value, value)) { |
| 760 | // Store into the heap location with the same value. |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 761 | same_value = true; |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 762 | } else if (index != nullptr) { |
| 763 | // For array element, don't eliminate stores since it can be easily aliased |
| 764 | // with non-constant index. |
| 765 | } else if (!heap_location_collector_.MayDeoptimize() && |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 766 | ref_info->IsSingletonAndNotReturned()) { |
| 767 | // Store into a field of a singleton that's not returned. The value cannot be |
| 768 | // killed due to aliasing/invocation. It can be redundant since future loads can |
| 769 | // directly get the value set by this instruction. The value can still be killed due to |
| 770 | // merging or loop side effects. Stores whose values are killed due to merging/loop side |
| 771 | // effects later will be removed from possibly_removed_stores_ when that is detected. |
| 772 | possibly_redundant = true; |
| 773 | HNewInstance* new_instance = ref_info->GetReference()->AsNewInstance(); |
| 774 | DCHECK(new_instance != nullptr); |
| 775 | if (new_instance->IsFinalizable()) { |
| 776 | // Finalizable objects escape globally. Need to keep the store. |
| 777 | possibly_redundant = false; |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 778 | } else { |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 779 | HLoopInformation* loop_info = instruction->GetBlock()->GetLoopInformation(); |
| 780 | if (loop_info != nullptr) { |
| 781 | // instruction is a store in the loop so the loop must does write. |
| 782 | DCHECK(side_effects_.GetLoopEffects(loop_info->GetHeader()).DoesAnyWrite()); |
Mingyao Yang | 803cbb9 | 2015-12-01 12:24:36 -0800 | [diff] [blame] | 783 | // If it's a singleton, IsValueKilledByLoopSideEffects() must be true. |
| 784 | DCHECK(!ref_info->IsSingleton() || |
| 785 | heap_location_collector_.GetHeapLocation(idx)->IsValueKilledByLoopSideEffects()); |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 786 | |
Mingyao Yang | 4b467ed | 2015-11-19 17:04:22 -0800 | [diff] [blame] | 787 | if (loop_info->IsDefinedOutOfTheLoop(original_ref)) { |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 788 | DCHECK(original_ref->GetBlock()->Dominates(loop_info->GetPreHeader())); |
| 789 | // Keep the store since its value may be needed at the loop header. |
| 790 | possibly_redundant = false; |
| 791 | } else { |
| 792 | // The singleton is created inside the loop. Value stored to it isn't needed at |
| 793 | // the loop header. This is true for outer loops also. |
| 794 | } |
| 795 | } |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 796 | } |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 797 | } |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 798 | if (same_value || possibly_redundant) { |
| 799 | possibly_removed_stores_.push_back(instruction); |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 800 | } |
Mingyao Yang | e9d6e60 | 2015-10-23 17:08:42 -0700 | [diff] [blame] | 801 | |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 802 | if (!same_value) { |
| 803 | if (possibly_redundant) { |
| 804 | DCHECK(instruction->IsInstanceFieldSet()); |
| 805 | // Put the store as the heap value. If the value is loaded from heap |
| 806 | // by a load later, this store isn't really redundant. |
| 807 | heap_values[idx] = instruction; |
| 808 | } else { |
| 809 | heap_values[idx] = value; |
| 810 | } |
| 811 | } |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 812 | // This store may kill values in other heap locations due to aliasing. |
| 813 | for (size_t i = 0; i < heap_values.size(); i++) { |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 814 | if (i == idx) { |
| 815 | continue; |
| 816 | } |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 817 | if (heap_values[i] == value) { |
| 818 | // Same value should be kept even if aliasing happens. |
| 819 | continue; |
| 820 | } |
| 821 | if (heap_values[i] == kUnknownHeapValue) { |
| 822 | // Value is already unknown, no need for aliasing check. |
| 823 | continue; |
| 824 | } |
| 825 | if (heap_location_collector_.MayAlias(i, idx)) { |
| 826 | // Kill heap locations that may alias. |
| 827 | heap_values[i] = kUnknownHeapValue; |
| 828 | } |
| 829 | } |
| 830 | } |
| 831 | |
| 832 | void VisitInstanceFieldGet(HInstanceFieldGet* instruction) OVERRIDE { |
| 833 | HInstruction* obj = instruction->InputAt(0); |
| 834 | size_t offset = instruction->GetFieldInfo().GetFieldOffset().SizeValue(); |
| 835 | int16_t declaring_class_def_index = instruction->GetFieldInfo().GetDeclaringClassDefIndex(); |
| 836 | VisitGetLocation(instruction, obj, offset, nullptr, declaring_class_def_index); |
| 837 | } |
| 838 | |
| 839 | void VisitInstanceFieldSet(HInstanceFieldSet* instruction) OVERRIDE { |
| 840 | HInstruction* obj = instruction->InputAt(0); |
| 841 | size_t offset = instruction->GetFieldInfo().GetFieldOffset().SizeValue(); |
| 842 | int16_t declaring_class_def_index = instruction->GetFieldInfo().GetDeclaringClassDefIndex(); |
| 843 | HInstruction* value = instruction->InputAt(1); |
| 844 | VisitSetLocation(instruction, obj, offset, nullptr, declaring_class_def_index, value); |
| 845 | } |
| 846 | |
| 847 | void VisitStaticFieldGet(HStaticFieldGet* instruction) OVERRIDE { |
| 848 | HInstruction* cls = instruction->InputAt(0); |
| 849 | size_t offset = instruction->GetFieldInfo().GetFieldOffset().SizeValue(); |
| 850 | int16_t declaring_class_def_index = instruction->GetFieldInfo().GetDeclaringClassDefIndex(); |
| 851 | VisitGetLocation(instruction, cls, offset, nullptr, declaring_class_def_index); |
| 852 | } |
| 853 | |
| 854 | void VisitStaticFieldSet(HStaticFieldSet* instruction) OVERRIDE { |
| 855 | HInstruction* cls = instruction->InputAt(0); |
| 856 | size_t offset = instruction->GetFieldInfo().GetFieldOffset().SizeValue(); |
| 857 | int16_t declaring_class_def_index = instruction->GetFieldInfo().GetDeclaringClassDefIndex(); |
| 858 | HInstruction* value = instruction->InputAt(1); |
| 859 | VisitSetLocation(instruction, cls, offset, nullptr, declaring_class_def_index, value); |
| 860 | } |
| 861 | |
| 862 | void VisitArrayGet(HArrayGet* instruction) OVERRIDE { |
| 863 | HInstruction* array = instruction->InputAt(0); |
| 864 | HInstruction* index = instruction->InputAt(1); |
| 865 | VisitGetLocation(instruction, |
| 866 | array, |
| 867 | HeapLocation::kInvalidFieldOffset, |
| 868 | index, |
| 869 | HeapLocation::kDeclaringClassDefIndexForArrays); |
| 870 | } |
| 871 | |
| 872 | void VisitArraySet(HArraySet* instruction) OVERRIDE { |
| 873 | HInstruction* array = instruction->InputAt(0); |
| 874 | HInstruction* index = instruction->InputAt(1); |
| 875 | HInstruction* value = instruction->InputAt(2); |
| 876 | VisitSetLocation(instruction, |
| 877 | array, |
| 878 | HeapLocation::kInvalidFieldOffset, |
| 879 | index, |
| 880 | HeapLocation::kDeclaringClassDefIndexForArrays, |
| 881 | value); |
| 882 | } |
| 883 | |
| 884 | void HandleInvoke(HInstruction* invoke) { |
| 885 | ArenaVector<HInstruction*>& heap_values = |
| 886 | heap_values_for_[invoke->GetBlock()->GetBlockId()]; |
| 887 | for (size_t i = 0; i < heap_values.size(); i++) { |
| 888 | ReferenceInfo* ref_info = heap_location_collector_.GetHeapLocation(i)->GetReferenceInfo(); |
| 889 | if (ref_info->IsSingleton()) { |
| 890 | // Singleton references cannot be seen by the callee. |
| 891 | } else { |
| 892 | heap_values[i] = kUnknownHeapValue; |
| 893 | } |
| 894 | } |
| 895 | } |
| 896 | |
| 897 | void VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) OVERRIDE { |
| 898 | HandleInvoke(invoke); |
| 899 | } |
| 900 | |
| 901 | void VisitInvokeVirtual(HInvokeVirtual* invoke) OVERRIDE { |
| 902 | HandleInvoke(invoke); |
| 903 | } |
| 904 | |
| 905 | void VisitInvokeInterface(HInvokeInterface* invoke) OVERRIDE { |
| 906 | HandleInvoke(invoke); |
| 907 | } |
| 908 | |
| 909 | void VisitInvokeUnresolved(HInvokeUnresolved* invoke) OVERRIDE { |
| 910 | HandleInvoke(invoke); |
| 911 | } |
| 912 | |
| 913 | void VisitClinitCheck(HClinitCheck* clinit) OVERRIDE { |
| 914 | HandleInvoke(clinit); |
| 915 | } |
| 916 | |
| 917 | void VisitUnresolvedInstanceFieldGet(HUnresolvedInstanceFieldGet* instruction) OVERRIDE { |
| 918 | // Conservatively treat it as an invocation. |
| 919 | HandleInvoke(instruction); |
| 920 | } |
| 921 | |
| 922 | void VisitUnresolvedInstanceFieldSet(HUnresolvedInstanceFieldSet* instruction) OVERRIDE { |
| 923 | // Conservatively treat it as an invocation. |
| 924 | HandleInvoke(instruction); |
| 925 | } |
| 926 | |
| 927 | void VisitUnresolvedStaticFieldGet(HUnresolvedStaticFieldGet* instruction) OVERRIDE { |
| 928 | // Conservatively treat it as an invocation. |
| 929 | HandleInvoke(instruction); |
| 930 | } |
| 931 | |
| 932 | void VisitUnresolvedStaticFieldSet(HUnresolvedStaticFieldSet* instruction) OVERRIDE { |
| 933 | // Conservatively treat it as an invocation. |
| 934 | HandleInvoke(instruction); |
| 935 | } |
| 936 | |
| 937 | void VisitNewInstance(HNewInstance* new_instance) OVERRIDE { |
| 938 | ReferenceInfo* ref_info = heap_location_collector_.FindReferenceInfoOf(new_instance); |
| 939 | if (ref_info == nullptr) { |
| 940 | // new_instance isn't used for field accesses. No need to process it. |
| 941 | return; |
| 942 | } |
| 943 | if (!heap_location_collector_.MayDeoptimize() && |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 944 | ref_info->IsSingletonAndNotReturned() && |
| 945 | !new_instance->IsFinalizable() && |
| 946 | !new_instance->CanThrow()) { |
| 947 | // TODO: add new_instance to singleton_new_instances_ and enable allocation elimination. |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 948 | } |
| 949 | ArenaVector<HInstruction*>& heap_values = |
| 950 | heap_values_for_[new_instance->GetBlock()->GetBlockId()]; |
| 951 | for (size_t i = 0; i < heap_values.size(); i++) { |
| 952 | HInstruction* ref = |
| 953 | heap_location_collector_.GetHeapLocation(i)->GetReferenceInfo()->GetReference(); |
| 954 | size_t offset = heap_location_collector_.GetHeapLocation(i)->GetOffset(); |
| 955 | if (ref == new_instance && offset >= mirror::kObjectHeaderSize) { |
| 956 | // Instance fields except the header fields are set to default heap values. |
| 957 | heap_values[i] = kDefaultHeapValue; |
| 958 | } |
| 959 | } |
| 960 | } |
| 961 | |
| 962 | // Find an instruction's substitute if it should be removed. |
| 963 | // Return the same instruction if it should not be removed. |
| 964 | HInstruction* FindSubstitute(HInstruction* instruction) { |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 965 | size_t size = removed_loads_.size(); |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 966 | for (size_t i = 0; i < size; i++) { |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 967 | if (removed_loads_[i] == instruction) { |
| 968 | return substitute_instructions_for_loads_[i]; |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 969 | } |
| 970 | } |
| 971 | return instruction; |
| 972 | } |
| 973 | |
| 974 | const HeapLocationCollector& heap_location_collector_; |
| 975 | const SideEffectsAnalysis& side_effects_; |
| 976 | |
| 977 | // One array of heap values for each block. |
| 978 | ArenaVector<ArenaVector<HInstruction*>> heap_values_for_; |
| 979 | |
| 980 | // We record the instructions that should be eliminated but may be |
| 981 | // used by heap locations. They'll be removed in the end. |
Mingyao Yang | fb8464a | 2015-11-02 10:56:59 -0800 | [diff] [blame] | 982 | ArenaVector<HInstruction*> removed_loads_; |
| 983 | ArenaVector<HInstruction*> substitute_instructions_for_loads_; |
| 984 | |
| 985 | // Stores in this list may be removed from the list later when it's |
| 986 | // found that the store cannot be eliminated. |
| 987 | ArenaVector<HInstruction*> possibly_removed_stores_; |
| 988 | |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 989 | ArenaVector<HInstruction*> singleton_new_instances_; |
| 990 | |
| 991 | DISALLOW_COPY_AND_ASSIGN(LSEVisitor); |
| 992 | }; |
| 993 | |
| 994 | void LoadStoreElimination::Run() { |
David Brazdil | 8993caf | 2015-12-07 10:04:40 +0000 | [diff] [blame] | 995 | if (graph_->IsDebuggable() || graph_->HasTryCatch()) { |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 996 | // Debugger may set heap values or trigger deoptimization of callers. |
David Brazdil | 8993caf | 2015-12-07 10:04:40 +0000 | [diff] [blame] | 997 | // Try/catch support not implemented yet. |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 998 | // Skip this optimization. |
| 999 | return; |
| 1000 | } |
| 1001 | HeapLocationCollector heap_location_collector(graph_); |
| 1002 | for (HReversePostOrderIterator it(*graph_); !it.Done(); it.Advance()) { |
| 1003 | heap_location_collector.VisitBasicBlock(it.Current()); |
| 1004 | } |
| 1005 | if (heap_location_collector.GetNumberOfHeapLocations() > kMaxNumberOfHeapLocations) { |
| 1006 | // Bail out if there are too many heap locations to deal with. |
| 1007 | return; |
| 1008 | } |
| 1009 | if (!heap_location_collector.HasHeapStores()) { |
| 1010 | // Without heap stores, this pass would act mostly as GVN on heap accesses. |
| 1011 | return; |
| 1012 | } |
| 1013 | if (heap_location_collector.HasVolatile() || heap_location_collector.HasMonitorOps()) { |
| 1014 | // Don't do load/store elimination if the method has volatile field accesses or |
| 1015 | // monitor operations, for now. |
| 1016 | // TODO: do it right. |
| 1017 | return; |
| 1018 | } |
| 1019 | heap_location_collector.BuildAliasingMatrix(); |
| 1020 | LSEVisitor lse_visitor(graph_, heap_location_collector, side_effects_); |
| 1021 | for (HReversePostOrderIterator it(*graph_); !it.Done(); it.Advance()) { |
| 1022 | lse_visitor.VisitBasicBlock(it.Current()); |
| 1023 | } |
| 1024 | lse_visitor.RemoveInstructions(); |
| 1025 | } |
| 1026 | |
| 1027 | } // namespace art |