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