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