blob: 6fbb6823d6199590f6a87d7d90d2709b7c795cf9 [file] [log] [blame]
Mingyao Yang8df69d42015-10-22 15:40:58 -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))) ||
Nicolas Geoffrayd9309292015-10-31 22:21:31 +000062 (use->IsUnresolvedStaticFieldSet() && (reference_ == use->InputAt(0))) ||
Mingyao Yang8df69d42015-10-22 15:40:58 -070063 (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 // TODO: remove unnecessary allocations.
536 // Eliminate instructions in singleton_new_instances_ that:
537 // - don't have uses,
538 // - don't have finalizers,
539 // - are instantiable and accessible,
540 // - have no/separate clinit check.
541 }
542
543 private:
544 void MergePredecessorValues(HBasicBlock* block) {
545 const ArenaVector<HBasicBlock*>& predecessors = block->GetPredecessors();
546 if (predecessors.size() == 0) {
547 return;
548 }
549 ArenaVector<HInstruction*>& heap_values = heap_values_for_[block->GetBlockId()];
550 for (size_t i = 0; i < heap_values.size(); i++) {
551 HInstruction* value = heap_values_for_[predecessors[0]->GetBlockId()][i];
552 if (value != kUnknownHeapValue) {
553 for (size_t j = 1; j < predecessors.size(); j++) {
554 if (heap_values_for_[predecessors[j]->GetBlockId()][i] != value) {
555 value = kUnknownHeapValue;
556 break;
557 }
558 }
559 }
560 heap_values[i] = value;
561 }
562 }
563
564 // `instruction` is being removed. Try to see if the null check on it
565 // can be removed. This can happen if the same value is set in two branches
566 // but not in dominators. Such as:
567 // int[] a = foo();
568 // if () {
569 // a[0] = 2;
570 // } else {
571 // a[0] = 2;
572 // }
573 // // a[0] can now be replaced with constant 2, and the null check on it can be removed.
574 void TryRemovingNullCheck(HInstruction* instruction) {
575 HInstruction* prev = instruction->GetPrevious();
576 if ((prev != nullptr) && prev->IsNullCheck() && (prev == instruction->InputAt(0))) {
577 // Previous instruction is a null check for this instruction. Remove the null check.
578 prev->ReplaceWith(prev->InputAt(0));
579 prev->GetBlock()->RemoveInstruction(prev);
580 }
581 }
582
583 HInstruction* GetDefaultValue(Primitive::Type type) {
584 switch (type) {
585 case Primitive::kPrimNot:
586 return GetGraph()->GetNullConstant();
587 case Primitive::kPrimBoolean:
588 case Primitive::kPrimByte:
589 case Primitive::kPrimChar:
590 case Primitive::kPrimShort:
591 case Primitive::kPrimInt:
592 return GetGraph()->GetIntConstant(0);
593 case Primitive::kPrimLong:
594 return GetGraph()->GetLongConstant(0);
595 case Primitive::kPrimFloat:
596 return GetGraph()->GetFloatConstant(0);
597 case Primitive::kPrimDouble:
598 return GetGraph()->GetDoubleConstant(0);
599 default:
600 UNREACHABLE();
601 }
602 }
603
604 void VisitGetLocation(HInstruction* instruction,
605 HInstruction* ref,
606 size_t offset,
607 HInstruction* index,
608 int16_t declaring_class_def_index) {
609 HInstruction* original_ref = HuntForOriginalReference(ref);
610 ReferenceInfo* ref_info = heap_location_collector_.FindReferenceInfoOf(original_ref);
611 size_t idx = heap_location_collector_.FindHeapLocationIndex(
612 ref_info, offset, index, declaring_class_def_index);
613 DCHECK_NE(idx, HeapLocationCollector::kHeapLocationNotFound);
614 ArenaVector<HInstruction*>& heap_values =
615 heap_values_for_[instruction->GetBlock()->GetBlockId()];
616 HInstruction* heap_value = heap_values[idx];
617 if (heap_value == kDefaultHeapValue) {
618 HInstruction* constant = GetDefaultValue(instruction->GetType());
619 removed_instructions_.push_back(instruction);
620 substitute_instructions_.push_back(constant);
621 heap_values[idx] = constant;
622 return;
623 }
624 if ((heap_value != kUnknownHeapValue) &&
625 // Keep the load due to possible I/F, J/D array aliasing.
626 // See b/22538329 for details.
627 (heap_value->GetType() == instruction->GetType())) {
628 removed_instructions_.push_back(instruction);
629 substitute_instructions_.push_back(heap_value);
630 TryRemovingNullCheck(instruction);
631 return;
632 }
633
634 if (heap_value == kUnknownHeapValue) {
635 // Put the load as the value into the HeapLocation.
636 // This acts like GVN but with better aliasing analysis.
637 heap_values[idx] = instruction;
638 }
639 }
640
641 bool Equal(HInstruction* heap_value, HInstruction* value) {
642 if (heap_value == value) {
643 return true;
644 }
645 if (heap_value == kDefaultHeapValue && GetDefaultValue(value->GetType()) == value) {
646 return true;
647 }
648 return false;
649 }
650
651 void VisitSetLocation(HInstruction* instruction,
652 HInstruction* ref,
653 size_t offset,
654 HInstruction* index,
655 int16_t declaring_class_def_index,
656 HInstruction* value) {
657 HInstruction* original_ref = HuntForOriginalReference(ref);
658 ReferenceInfo* ref_info = heap_location_collector_.FindReferenceInfoOf(original_ref);
659 size_t idx = heap_location_collector_.FindHeapLocationIndex(
660 ref_info, offset, index, declaring_class_def_index);
661 DCHECK_NE(idx, HeapLocationCollector::kHeapLocationNotFound);
662 ArenaVector<HInstruction*>& heap_values =
663 heap_values_for_[instruction->GetBlock()->GetBlockId()];
664 HInstruction* heap_value = heap_values[idx];
665 bool redundant_store = false;
666 if (Equal(heap_value, value)) {
667 // Store into the heap location with the same value.
668 redundant_store = true;
669 } else if (index != nullptr) {
670 // For array element, don't eliminate stores since it can be easily aliased
671 // with non-constant index.
672 } else if (!heap_location_collector_.MayDeoptimize() &&
673 ref_info->IsSingletonAndNotReturned() &&
674 !heap_location_collector_.GetHeapLocation(idx)->MayBecomeUnknown()) {
675 // Store into a field of a singleton that's not returned. And that value cannot be
676 // killed due to merge. It's redundant since future loads will get the value
677 // set by this instruction.
678 Primitive::Type type = Primitive::kPrimVoid;
679 if (instruction->IsInstanceFieldSet()) {
680 type = instruction->AsInstanceFieldSet()->GetFieldInfo().GetFieldType();
681 } else if (instruction->IsStaticFieldSet()) {
682 type = instruction->AsStaticFieldSet()->GetFieldInfo().GetFieldType();
683 } else {
684 DCHECK(false) << "Must be an instance/static field set instruction.";
685 }
686 if (value->GetType() != type) {
687 // I/F, J/D aliasing should not happen for fields.
688 DCHECK(Primitive::IsIntegralType(value->GetType()));
689 DCHECK(!Primitive::Is64BitType(value->GetType()));
690 DCHECK(Primitive::IsIntegralType(type));
691 DCHECK(!Primitive::Is64BitType(type));
692 // Keep the store since the corresponding load isn't eliminated due to different types.
693 // TODO: handle the different int types so that we can eliminate this store.
694 redundant_store = false;
695 } else {
696 redundant_store = true;
697 }
Andreas Gampe55d02cf2015-10-29 02:59:50 +0000698 // TODO: eliminate the store if the singleton object is not finalizable.
699 redundant_store = false;
Mingyao Yang8df69d42015-10-22 15:40:58 -0700700 }
701 if (redundant_store) {
702 removed_instructions_.push_back(instruction);
703 substitute_instructions_.push_back(nullptr);
704 TryRemovingNullCheck(instruction);
705 }
Mingyao Yange9d6e602015-10-23 17:08:42 -0700706
Mingyao Yang8df69d42015-10-22 15:40:58 -0700707 heap_values[idx] = value;
708 // This store may kill values in other heap locations due to aliasing.
709 for (size_t i = 0; i < heap_values.size(); i++) {
710 if (heap_values[i] == value) {
711 // Same value should be kept even if aliasing happens.
712 continue;
713 }
714 if (heap_values[i] == kUnknownHeapValue) {
715 // Value is already unknown, no need for aliasing check.
716 continue;
717 }
718 if (heap_location_collector_.MayAlias(i, idx)) {
719 // Kill heap locations that may alias.
720 heap_values[i] = kUnknownHeapValue;
721 }
722 }
723 }
724
725 void VisitInstanceFieldGet(HInstanceFieldGet* instruction) OVERRIDE {
726 HInstruction* obj = instruction->InputAt(0);
727 size_t offset = instruction->GetFieldInfo().GetFieldOffset().SizeValue();
728 int16_t declaring_class_def_index = instruction->GetFieldInfo().GetDeclaringClassDefIndex();
729 VisitGetLocation(instruction, obj, offset, nullptr, declaring_class_def_index);
730 }
731
732 void VisitInstanceFieldSet(HInstanceFieldSet* instruction) OVERRIDE {
733 HInstruction* obj = instruction->InputAt(0);
734 size_t offset = instruction->GetFieldInfo().GetFieldOffset().SizeValue();
735 int16_t declaring_class_def_index = instruction->GetFieldInfo().GetDeclaringClassDefIndex();
736 HInstruction* value = instruction->InputAt(1);
737 VisitSetLocation(instruction, obj, offset, nullptr, declaring_class_def_index, value);
738 }
739
740 void VisitStaticFieldGet(HStaticFieldGet* instruction) OVERRIDE {
741 HInstruction* cls = instruction->InputAt(0);
742 size_t offset = instruction->GetFieldInfo().GetFieldOffset().SizeValue();
743 int16_t declaring_class_def_index = instruction->GetFieldInfo().GetDeclaringClassDefIndex();
744 VisitGetLocation(instruction, cls, offset, nullptr, declaring_class_def_index);
745 }
746
747 void VisitStaticFieldSet(HStaticFieldSet* instruction) OVERRIDE {
748 HInstruction* cls = instruction->InputAt(0);
749 size_t offset = instruction->GetFieldInfo().GetFieldOffset().SizeValue();
750 int16_t declaring_class_def_index = instruction->GetFieldInfo().GetDeclaringClassDefIndex();
751 HInstruction* value = instruction->InputAt(1);
752 VisitSetLocation(instruction, cls, offset, nullptr, declaring_class_def_index, value);
753 }
754
755 void VisitArrayGet(HArrayGet* instruction) OVERRIDE {
756 HInstruction* array = instruction->InputAt(0);
757 HInstruction* index = instruction->InputAt(1);
758 VisitGetLocation(instruction,
759 array,
760 HeapLocation::kInvalidFieldOffset,
761 index,
762 HeapLocation::kDeclaringClassDefIndexForArrays);
763 }
764
765 void VisitArraySet(HArraySet* instruction) OVERRIDE {
766 HInstruction* array = instruction->InputAt(0);
767 HInstruction* index = instruction->InputAt(1);
768 HInstruction* value = instruction->InputAt(2);
769 VisitSetLocation(instruction,
770 array,
771 HeapLocation::kInvalidFieldOffset,
772 index,
773 HeapLocation::kDeclaringClassDefIndexForArrays,
774 value);
775 }
776
777 void HandleInvoke(HInstruction* invoke) {
778 ArenaVector<HInstruction*>& heap_values =
779 heap_values_for_[invoke->GetBlock()->GetBlockId()];
780 for (size_t i = 0; i < heap_values.size(); i++) {
781 ReferenceInfo* ref_info = heap_location_collector_.GetHeapLocation(i)->GetReferenceInfo();
782 if (ref_info->IsSingleton()) {
783 // Singleton references cannot be seen by the callee.
784 } else {
785 heap_values[i] = kUnknownHeapValue;
786 }
787 }
788 }
789
790 void VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) OVERRIDE {
791 HandleInvoke(invoke);
792 }
793
794 void VisitInvokeVirtual(HInvokeVirtual* invoke) OVERRIDE {
795 HandleInvoke(invoke);
796 }
797
798 void VisitInvokeInterface(HInvokeInterface* invoke) OVERRIDE {
799 HandleInvoke(invoke);
800 }
801
802 void VisitInvokeUnresolved(HInvokeUnresolved* invoke) OVERRIDE {
803 HandleInvoke(invoke);
804 }
805
806 void VisitClinitCheck(HClinitCheck* clinit) OVERRIDE {
807 HandleInvoke(clinit);
808 }
809
810 void VisitUnresolvedInstanceFieldGet(HUnresolvedInstanceFieldGet* instruction) OVERRIDE {
811 // Conservatively treat it as an invocation.
812 HandleInvoke(instruction);
813 }
814
815 void VisitUnresolvedInstanceFieldSet(HUnresolvedInstanceFieldSet* instruction) OVERRIDE {
816 // Conservatively treat it as an invocation.
817 HandleInvoke(instruction);
818 }
819
820 void VisitUnresolvedStaticFieldGet(HUnresolvedStaticFieldGet* instruction) OVERRIDE {
821 // Conservatively treat it as an invocation.
822 HandleInvoke(instruction);
823 }
824
825 void VisitUnresolvedStaticFieldSet(HUnresolvedStaticFieldSet* instruction) OVERRIDE {
826 // Conservatively treat it as an invocation.
827 HandleInvoke(instruction);
828 }
829
830 void VisitNewInstance(HNewInstance* new_instance) OVERRIDE {
831 ReferenceInfo* ref_info = heap_location_collector_.FindReferenceInfoOf(new_instance);
832 if (ref_info == nullptr) {
833 // new_instance isn't used for field accesses. No need to process it.
834 return;
835 }
836 if (!heap_location_collector_.MayDeoptimize() &&
Andreas Gampe55d02cf2015-10-29 02:59:50 +0000837 ref_info->IsSingletonAndNotReturned()) {
Mingyao Yang8df69d42015-10-22 15:40:58 -0700838 // The allocation might be eliminated.
839 singleton_new_instances_.push_back(new_instance);
840 }
841 ArenaVector<HInstruction*>& heap_values =
842 heap_values_for_[new_instance->GetBlock()->GetBlockId()];
843 for (size_t i = 0; i < heap_values.size(); i++) {
844 HInstruction* ref =
845 heap_location_collector_.GetHeapLocation(i)->GetReferenceInfo()->GetReference();
846 size_t offset = heap_location_collector_.GetHeapLocation(i)->GetOffset();
847 if (ref == new_instance && offset >= mirror::kObjectHeaderSize) {
848 // Instance fields except the header fields are set to default heap values.
849 heap_values[i] = kDefaultHeapValue;
850 }
851 }
852 }
853
854 // Find an instruction's substitute if it should be removed.
855 // Return the same instruction if it should not be removed.
856 HInstruction* FindSubstitute(HInstruction* instruction) {
857 size_t size = removed_instructions_.size();
858 for (size_t i = 0; i < size; i++) {
859 if (removed_instructions_[i] == instruction) {
860 return substitute_instructions_[i];
861 }
862 }
863 return instruction;
864 }
865
866 const HeapLocationCollector& heap_location_collector_;
867 const SideEffectsAnalysis& side_effects_;
868
869 // One array of heap values for each block.
870 ArenaVector<ArenaVector<HInstruction*>> heap_values_for_;
871
872 // We record the instructions that should be eliminated but may be
873 // used by heap locations. They'll be removed in the end.
874 ArenaVector<HInstruction*> removed_instructions_;
875 ArenaVector<HInstruction*> substitute_instructions_;
876 ArenaVector<HInstruction*> singleton_new_instances_;
877
878 DISALLOW_COPY_AND_ASSIGN(LSEVisitor);
879};
880
881void LoadStoreElimination::Run() {
882 if (graph_->IsDebuggable()) {
883 // Debugger may set heap values or trigger deoptimization of callers.
884 // Skip this optimization.
885 return;
886 }
887 HeapLocationCollector heap_location_collector(graph_);
888 for (HReversePostOrderIterator it(*graph_); !it.Done(); it.Advance()) {
889 heap_location_collector.VisitBasicBlock(it.Current());
890 }
891 if (heap_location_collector.GetNumberOfHeapLocations() > kMaxNumberOfHeapLocations) {
892 // Bail out if there are too many heap locations to deal with.
893 return;
894 }
895 if (!heap_location_collector.HasHeapStores()) {
896 // Without heap stores, this pass would act mostly as GVN on heap accesses.
897 return;
898 }
899 if (heap_location_collector.HasVolatile() || heap_location_collector.HasMonitorOps()) {
900 // Don't do load/store elimination if the method has volatile field accesses or
901 // monitor operations, for now.
902 // TODO: do it right.
903 return;
904 }
905 heap_location_collector.BuildAliasingMatrix();
906 LSEVisitor lse_visitor(graph_, heap_location_collector, side_effects_);
907 for (HReversePostOrderIterator it(*graph_); !it.Done(); it.Advance()) {
908 lse_visitor.VisitBasicBlock(it.Current());
909 }
910 lse_visitor.RemoveInstructions();
911}
912
913} // namespace art