blob: 5a8ac5919523533f5e7808aed81587b2e02bd8bf [file] [log] [blame]
xueliang.zhongc239a2b2017-04-27 15:31:37 +01001/*
2 * Copyright (C) 2017 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_analysis.h"
18
19namespace art {
20
21// A cap for the number of heap locations to prevent pathological time/space consumption.
22// The number of heap locations for most of the methods stays below this threshold.
23constexpr size_t kMaxNumberOfHeapLocations = 32;
24
xueliang.zhong016c0f12017-05-12 18:16:31 +010025// Check if array indices array[idx1 +/- CONST] and array[idx2] MAY alias.
26static bool BinaryOpAndIndexMayAlias(const HBinaryOperation* idx1, const HInstruction* idx2) {
27 DCHECK(idx1 != nullptr);
28 DCHECK(idx2 != nullptr);
29
30 if (!idx1->IsAdd() && !idx1->IsSub()) {
31 // We currently only support Add and Sub operations.
32 return true;
33 }
34
35 HConstant* cst = idx1->GetConstantRight();
36 if (cst == nullptr || cst->IsArithmeticZero()) {
37 return true;
38 }
39
40 if (idx1->GetLeastConstantLeft() == idx2) {
41 // for example, array[idx1 + 1] and array[idx1]
42 return false;
43 }
44
45 return true;
46}
47
48// Check if Add and Sub MAY alias when used as indices in arrays.
49static bool BinaryOpsMayAlias(const HBinaryOperation* idx1, const HBinaryOperation* idx2) {
50 DCHECK(idx1!= nullptr);
51 DCHECK(idx2 != nullptr);
52
53 HConstant* idx1_cst = idx1->GetConstantRight();
54 HInstruction* idx1_other = idx1->GetLeastConstantLeft();
55 HConstant* idx2_cst = idx2->GetConstantRight();
56 HInstruction* idx2_other = idx2->GetLeastConstantLeft();
57
58 if (idx1_cst == nullptr || idx1_other == nullptr ||
59 idx2_cst == nullptr || idx2_other == nullptr) {
60 // We only analyze patterns like [i +/- CONST].
61 return true;
62 }
63
64 if (idx1_other != idx2_other) {
65 // For example, [j+1] and [k+1] MAY alias.
66 return true;
67 }
68
69 if ((idx1->IsAdd() && idx2->IsAdd()) ||
70 (idx1->IsSub() && idx2->IsSub())) {
71 return idx1_cst->AsIntConstant()->GetValue() == idx2_cst->AsIntConstant()->GetValue();
72 }
73
74 if ((idx1->IsAdd() && idx2->IsSub()) ||
75 (idx1->IsSub() && idx2->IsAdd())) {
76 // [i + CONST1] and [i - CONST2] MAY alias iff CONST1 == -CONST2.
77 // By checking CONST1 == -CONST2, following cases are handled:
78 // - Zero constants case [i+0] and [i-0] is handled.
79 // - Overflow cases are handled, for example:
80 // [i+0x80000000] and [i-0x80000000];
81 // [i+0x10] and [i-0xFFFFFFF0].
82 // - Other cases [i+CONST1] and [i-CONST2] without any overflow is handled.
83 return idx1_cst->AsIntConstant()->GetValue() == -(idx2_cst->AsIntConstant()->GetValue());
84 }
85
86 // All other cases, MAY alias.
87 return true;
88}
89
90// The following array index cases are handled:
91// [i] and [i]
92// [CONST1] and [CONST2]
93// [i] and [i+CONST]
94// [i] and [i-CONST]
95// [i+CONST1] and [i+CONST2]
96// [i-CONST1] and [i-CONST2]
97// [i+CONST1] and [i-CONST2]
98// [i-CONST1] and [i+CONST2]
99// For other complicated cases, we rely on other passes like GVN and simpilfier
100// to optimize these cases before this pass.
101// For example: [i+j+k+10] and [i+k+10+j] shall be optimized to [i7+10] and [i7+10].
102bool HeapLocationCollector::CanArrayIndicesAlias(const HInstruction* idx1,
103 const HInstruction* idx2) const {
104 DCHECK(idx1 != nullptr);
105 DCHECK(idx2 != nullptr);
106
107 if (idx1 == idx2) {
108 // [i] and [i]
109 return true;
110 }
111 if (idx1->IsIntConstant() && idx2->IsIntConstant()) {
112 // [CONST1] and [CONST2]
113 return idx1->AsIntConstant()->GetValue() == idx2->AsIntConstant()->GetValue();
114 }
115
116 if (idx1->IsBinaryOperation() && !BinaryOpAndIndexMayAlias(idx1->AsBinaryOperation(), idx2)) {
117 // [i] and [i+/-CONST]
118 return false;
119 }
120 if (idx2->IsBinaryOperation() && !BinaryOpAndIndexMayAlias(idx2->AsBinaryOperation(), idx1)) {
121 // [i+/-CONST] and [i]
122 return false;
123 }
124
125 if (idx1->IsBinaryOperation() && idx2->IsBinaryOperation()) {
126 // [i+/-CONST1] and [i+/-CONST2]
127 if (!BinaryOpsMayAlias(idx1->AsBinaryOperation(), idx2->AsBinaryOperation())) {
128 return false;
129 }
130 }
131
132 // By default, MAY alias.
133 return true;
134}
135
xueliang.zhongc239a2b2017-04-27 15:31:37 +0100136void LoadStoreAnalysis::Run() {
137 for (HBasicBlock* block : graph_->GetReversePostOrder()) {
138 heap_location_collector_.VisitBasicBlock(block);
139 }
140
141 if (heap_location_collector_.GetNumberOfHeapLocations() > kMaxNumberOfHeapLocations) {
142 // Bail out if there are too many heap locations to deal with.
143 heap_location_collector_.CleanUp();
144 return;
145 }
146 if (!heap_location_collector_.HasHeapStores()) {
147 // Without heap stores, this pass would act mostly as GVN on heap accesses.
148 heap_location_collector_.CleanUp();
149 return;
150 }
151 if (heap_location_collector_.HasVolatile() || heap_location_collector_.HasMonitorOps()) {
152 // Don't do load/store elimination if the method has volatile field accesses or
153 // monitor operations, for now.
154 // TODO: do it right.
155 heap_location_collector_.CleanUp();
156 return;
157 }
158
159 heap_location_collector_.BuildAliasingMatrix();
160}
161
162} // namespace art