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