blob: 6c424b78b98fa0b10cf202cc4215ac5e0b387bed [file] [log] [blame]
Aart Bikd14c5952015-09-08 15:25:15 -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#ifndef ART_COMPILER_OPTIMIZING_INDUCTION_VAR_RANGE_H_
18#define ART_COMPILER_OPTIMIZING_INDUCTION_VAR_RANGE_H_
19
20#include "induction_var_analysis.h"
21
22namespace art {
23
24/**
Aart Bikb3365e02015-09-21 14:45:05 -070025 * This class implements range analysis on expressions within loops. It takes the results
26 * of induction variable analysis in the constructor and provides a public API to obtain
27 * a conservative lower and upper bound value on each instruction in the HIR.
Aart Bikd14c5952015-09-08 15:25:15 -070028 *
Aart Bikb3365e02015-09-21 14:45:05 -070029 * The range analysis is done with a combination of symbolic and partial integral evaluation
30 * of expressions. The analysis avoids complications with wrap-around arithmetic on the integral
31 * parts but all clients should be aware that wrap-around may occur on any of the symbolic parts.
32 * For example, given a known range for [0,100] for i, the evaluation yields range [-100,100]
33 * for expression -2*i+100, which is exact, and range [x,x+100] for expression i+x, which may
34 * wrap-around anywhere in the range depending on the actual value of x.
Aart Bikd14c5952015-09-08 15:25:15 -070035 */
36class InductionVarRange {
37 public:
38 /*
39 * A value that can be represented as "a * instruction + b" for 32-bit constants, where
Aart Bikb3365e02015-09-21 14:45:05 -070040 * Value() denotes an unknown lower and upper bound. Although range analysis could yield
41 * more complex values, the format is sufficiently powerful to represent useful cases
42 * and feeds directly into optimizations like bounds check elimination.
Aart Bikd14c5952015-09-08 15:25:15 -070043 */
44 struct Value {
Aart Bikb3365e02015-09-21 14:45:05 -070045 Value() : instruction(nullptr), a_constant(0), b_constant(0), is_known(false) {}
Aart Bikd14c5952015-09-08 15:25:15 -070046 Value(HInstruction* i, int32_t a, int32_t b)
Aart Bikb3365e02015-09-21 14:45:05 -070047 : instruction(a != 0 ? i : nullptr), a_constant(a), b_constant(b), is_known(true) {}
Aart Bikd14c5952015-09-08 15:25:15 -070048 explicit Value(int32_t b) : Value(nullptr, 0, b) {}
Aart Bikb3365e02015-09-21 14:45:05 -070049 // Representation as: a_constant x instruction + b_constant.
Aart Bikd14c5952015-09-08 15:25:15 -070050 HInstruction* instruction;
51 int32_t a_constant;
52 int32_t b_constant;
Aart Bikb3365e02015-09-21 14:45:05 -070053 // If true, represented by prior fields. Otherwise unknown value.
54 bool is_known;
Aart Bikd14c5952015-09-08 15:25:15 -070055 };
56
57 explicit InductionVarRange(HInductionVarAnalysis* induction);
58
59 /**
Aart Bik52be7e72016-06-23 11:20:41 -070060 * Given a context denoted by the first instruction, returns a possibly conservative lower
61 * and upper bound on the instruction's value in the output parameters min_val and max_val,
62 * respectively. The need_finite_test flag denotes if an additional finite-test is needed
63 * to protect the range evaluation inside its loop. The parameter chase_hint defines an
64 * instruction at which chasing may stop. Returns false on failure.
Aart Bikd14c5952015-09-08 15:25:15 -070065 */
Aart Bik1fc3afb2016-02-02 13:26:16 -080066 bool GetInductionRange(HInstruction* context,
Aart Bik389b3db2015-10-28 14:23:40 -070067 HInstruction* instruction,
Aart Bik52be7e72016-06-23 11:20:41 -070068 HInstruction* chase_hint,
Aart Bik1fc3afb2016-02-02 13:26:16 -080069 /*out*/ Value* min_val,
70 /*out*/ Value* max_val,
71 /*out*/ bool* needs_finite_test);
Aart Bikd14c5952015-09-08 15:25:15 -070072
73 /**
Aart Bik389b3db2015-10-28 14:23:40 -070074 * Returns true if range analysis is able to generate code for the lower and upper
75 * bound expressions on the instruction in the given context. The need_finite_test
76 * and need_taken test flags denote if an additional finite-test and/or taken-test
77 * are needed to protect the range evaluation inside its loop.
Aart Bikd14c5952015-09-08 15:25:15 -070078 */
Aart Bik16d3a652016-09-09 10:33:50 -070079 bool CanGenerateRange(HInstruction* context,
80 HInstruction* instruction,
81 /*out*/ bool* needs_finite_test,
82 /*out*/ bool* needs_taken_test);
Aart Bikaec3cce2015-10-14 17:44:55 -070083
84 /**
85 * Generates the actual code in the HIR for the lower and upper bound expressions on the
86 * instruction in the given context. Code for the lower and upper bound expression are
Aart Bik389b3db2015-10-28 14:23:40 -070087 * generated in given block and graph and are returned in the output parameters lower and
88 * upper, respectively. For a loop invariant, lower is not set.
Aart Bikaec3cce2015-10-14 17:44:55 -070089 *
90 * For example, given expression x+i with range [0, 5] for i, calling this method
91 * will generate the following sequence:
92 *
93 * block:
94 * lower: add x, 0
95 * upper: add x, 5
Aart Bik389b3db2015-10-28 14:23:40 -070096 *
Aart Bik16d3a652016-09-09 10:33:50 -070097 * Precondition: CanGenerateRange() returns true.
Aart Bikaec3cce2015-10-14 17:44:55 -070098 */
Aart Bik16d3a652016-09-09 10:33:50 -070099 void GenerateRange(HInstruction* context,
100 HInstruction* instruction,
101 HGraph* graph,
102 HBasicBlock* block,
103 /*out*/ HInstruction** lower,
104 /*out*/ HInstruction** upper);
Aart Bik389b3db2015-10-28 14:23:40 -0700105
106 /**
107 * Generates explicit taken-test for the loop in the given context. Code is generated in
Aart Bik16d3a652016-09-09 10:33:50 -0700108 * given block and graph. Returns generated taken-test.
Aart Bik389b3db2015-10-28 14:23:40 -0700109 *
Aart Bik16d3a652016-09-09 10:33:50 -0700110 * Precondition: CanGenerateRange() returns true and needs_taken_test is set.
Aart Bik389b3db2015-10-28 14:23:40 -0700111 */
Aart Bik16d3a652016-09-09 10:33:50 -0700112 HInstruction* GenerateTakenTest(HInstruction* context, HGraph* graph, HBasicBlock* block);
113
114 /**
115 * Returns true if induction analysis is able to generate code for last value of
116 * the given instruction inside the closest enveloping loop.
117 */
118 bool CanGenerateLastValue(HInstruction* instruction);
119
120 /**
121 * Generates last value of the given instruction in the closest enveloping loop.
122 * Code is generated in given block and graph. Returns generated last value.
123 *
124 * Precondition: CanGenerateLastValue() returns true.
125 */
126 HInstruction* GenerateLastValue(HInstruction* instruction, HGraph* graph, HBasicBlock* block);
127
128 /**
129 * Updates all matching fetches with the given replacement in all induction information
130 * that is associated with the given instruction.
131 */
132 void Replace(HInstruction* instruction, HInstruction* fetch, HInstruction* replacement);
Aart Bikaec3cce2015-10-14 17:44:55 -0700133
Aart Bik482095d2016-10-10 15:39:10 -0700134 /**
135 * Incrementally updates induction information for just the given loop.
136 */
137 void ReVisit(HLoopInformation* loop) {
138 induction_analysis_->induction_.erase(loop);
Aart Bikcc42be02016-10-20 16:14:16 -0700139 for (HInstructionIterator it(loop->GetHeader()->GetPhis()); !it.Done(); it.Advance()) {
140 induction_analysis_->cycles_.erase(it.Current()->AsPhi());
141 }
Aart Bik482095d2016-10-10 15:39:10 -0700142 induction_analysis_->VisitLoop(loop);
143 }
144
Aart Bik9abf8942016-10-14 09:49:42 -0700145 /**
Aart Bikcc42be02016-10-20 16:14:16 -0700146 * Lookup an interesting cycle associated with an entry phi.
147 */
148 ArenaSet<HInstruction*>* LookupCycle(HPhi* phi) const {
149 return induction_analysis_->LookupCycle(phi);
150 }
151
152 /**
Aart Bik6b69e0a2017-01-11 10:20:43 -0800153 * Checks if header logic of a loop terminates. Sets trip-count tc if known.
Aart Bik9abf8942016-10-14 09:49:42 -0700154 */
Aart Bik6b69e0a2017-01-11 10:20:43 -0800155 bool IsFinite(HLoopInformation* loop, /*out*/ int64_t* tc) const;
Aart Bik9abf8942016-10-14 09:49:42 -0700156
Aart Bikd14c5952015-09-08 15:25:15 -0700157 private:
Aart Bik97412c922016-02-19 20:14:38 -0800158 /*
159 * Enum used in IsConstant() request.
160 */
161 enum ConstantRequest {
162 kExact,
163 kAtMost,
164 kAtLeast
165 };
166
167 /**
168 * Returns true if exact or upper/lower bound on the given induction
169 * information is known as a 64-bit constant, which is returned in value.
170 */
171 bool IsConstant(HInductionVarAnalysis::InductionInfo* info,
172 ConstantRequest request,
Aart Bik52be7e72016-06-23 11:20:41 -0700173 /*out*/ int64_t* value) const;
Aart Bik97412c922016-02-19 20:14:38 -0800174
Aart Bik52be7e72016-06-23 11:20:41 -0700175 /** Returns whether induction information can be obtained. */
176 bool HasInductionInfo(HInstruction* context,
177 HInstruction* instruction,
178 /*out*/ HLoopInformation** loop,
179 /*out*/ HInductionVarAnalysis::InductionInfo** info,
180 /*out*/ HInductionVarAnalysis::InductionInfo** trip) const;
181
182 bool HasFetchInLoop(HInductionVarAnalysis::InductionInfo* info) const;
Aart Bik16d3a652016-09-09 10:33:50 -0700183 bool NeedsTripCount(HInductionVarAnalysis::InductionInfo* info,
184 /*out*/ int64_t* stride_value) const;
Aart Bik7d57d7f2015-12-09 14:39:48 -0800185 bool IsBodyTripCount(HInductionVarAnalysis::InductionInfo* trip) const;
186 bool IsUnsafeTripCount(HInductionVarAnalysis::InductionInfo* trip) const;
Aart Bik52be7e72016-06-23 11:20:41 -0700187 bool IsWellBehavedTripCount(HInductionVarAnalysis::InductionInfo* trip) const;
Aart Bikd14c5952015-09-08 15:25:15 -0700188
Aart Bik7d57d7f2015-12-09 14:39:48 -0800189 Value GetLinear(HInductionVarAnalysis::InductionInfo* info,
190 HInductionVarAnalysis::InductionInfo* trip,
191 bool in_body,
192 bool is_min) const;
Aart Bikdf7822e2016-12-06 10:05:30 -0800193 Value GetPolynomial(HInductionVarAnalysis::InductionInfo* info,
194 HInductionVarAnalysis::InductionInfo* trip,
195 bool in_body,
196 bool is_min) const;
Aart Bikc071a012016-12-01 10:22:31 -0800197 Value GetGeometric(HInductionVarAnalysis::InductionInfo* info,
198 HInductionVarAnalysis::InductionInfo* trip,
199 bool in_body,
200 bool is_min) const;
Aart Bik7d57d7f2015-12-09 14:39:48 -0800201 Value GetFetch(HInstruction* instruction,
202 HInductionVarAnalysis::InductionInfo* trip,
203 bool in_body,
204 bool is_min) const;
205 Value GetVal(HInductionVarAnalysis::InductionInfo* info,
206 HInductionVarAnalysis::InductionInfo* trip,
207 bool in_body,
208 bool is_min) const;
209 Value GetMul(HInductionVarAnalysis::InductionInfo* info1,
210 HInductionVarAnalysis::InductionInfo* info2,
211 HInductionVarAnalysis::InductionInfo* trip,
212 bool in_body,
213 bool is_min) const;
214 Value GetDiv(HInductionVarAnalysis::InductionInfo* info1,
215 HInductionVarAnalysis::InductionInfo* info2,
216 HInductionVarAnalysis::InductionInfo* trip,
217 bool in_body,
218 bool is_min) const;
Aart Bikdf7822e2016-12-06 10:05:30 -0800219 Value GetRem(HInductionVarAnalysis::InductionInfo* info1,
220 HInductionVarAnalysis::InductionInfo* info2) const;
Aart Bik7dc96932016-10-12 10:01:05 -0700221 Value GetXor(HInductionVarAnalysis::InductionInfo* info1,
222 HInductionVarAnalysis::InductionInfo* info2) const;
Aart Bikd14c5952015-09-08 15:25:15 -0700223
Aart Bik52be7e72016-06-23 11:20:41 -0700224 Value MulRangeAndConstant(int64_t value,
225 HInductionVarAnalysis::InductionInfo* info,
226 HInductionVarAnalysis::InductionInfo* trip,
227 bool in_body,
228 bool is_min) const;
229 Value DivRangeAndConstant(int64_t value,
230 HInductionVarAnalysis::InductionInfo* info,
231 HInductionVarAnalysis::InductionInfo* trip,
232 bool in_body,
233 bool is_min) const;
Aart Bikd14c5952015-09-08 15:25:15 -0700234
Aart Bik7d57d7f2015-12-09 14:39:48 -0800235 Value AddValue(Value v1, Value v2) const;
236 Value SubValue(Value v1, Value v2) const;
237 Value MulValue(Value v1, Value v2) const;
238 Value DivValue(Value v1, Value v2) const;
239 Value MergeVal(Value v1, Value v2, bool is_min) const;
Aart Bikd14c5952015-09-08 15:25:15 -0700240
Aart Bikaec3cce2015-10-14 17:44:55 -0700241 /**
Aart Bik16d3a652016-09-09 10:33:50 -0700242 * Generates code for lower/upper/taken-test or last value in the HIR. Returns true on
243 * success. With values nullptr, the method can be used to determine if code generation
Aart Bikaec3cce2015-10-14 17:44:55 -0700244 * would be successful without generating actual code yet.
245 */
Aart Bik9abf8942016-10-14 09:49:42 -0700246 bool GenerateRangeOrLastValue(HInstruction* context,
247 HInstruction* instruction,
248 bool is_last_val,
249 HGraph* graph,
250 HBasicBlock* block,
251 /*out*/ HInstruction** lower,
252 /*out*/ HInstruction** upper,
253 /*out*/ HInstruction** taken_test,
254 /*out*/ int64_t* stride_value,
255 /*out*/ bool* needs_finite_test,
256 /*out*/ bool* needs_taken_test) const;
257
Aart Bikdf7822e2016-12-06 10:05:30 -0800258 bool GenerateLastValuePolynomial(HInductionVarAnalysis::InductionInfo* info,
259 HInductionVarAnalysis::InductionInfo* trip,
260 HGraph* graph,
261 HBasicBlock* block,
262 /*out*/HInstruction** result) const;
263
Aart Bikc071a012016-12-01 10:22:31 -0800264 bool GenerateLastValueGeometric(HInductionVarAnalysis::InductionInfo* info,
265 HInductionVarAnalysis::InductionInfo* trip,
266 HGraph* graph,
267 HBasicBlock* block,
268 /*out*/HInstruction** result) const;
269
Aart Bikdf7822e2016-12-06 10:05:30 -0800270 bool GenerateLastValueWrapAround(HInductionVarAnalysis::InductionInfo* info,
271 HInductionVarAnalysis::InductionInfo* trip,
272 HGraph* graph,
273 HBasicBlock* block,
274 /*out*/HInstruction** result) const;
275
Aart Bik9abf8942016-10-14 09:49:42 -0700276 bool GenerateLastValuePeriodic(HInductionVarAnalysis::InductionInfo* info,
277 HInductionVarAnalysis::InductionInfo* trip,
278 HGraph* graph,
279 HBasicBlock* block,
280 /*out*/HInstruction** result,
281 /*out*/ bool* needs_taken_test) const;
Aart Bikaec3cce2015-10-14 17:44:55 -0700282
Aart Bik7d57d7f2015-12-09 14:39:48 -0800283 bool GenerateCode(HInductionVarAnalysis::InductionInfo* info,
284 HInductionVarAnalysis::InductionInfo* trip,
285 HGraph* graph,
286 HBasicBlock* block,
Aart Bik1fc3afb2016-02-02 13:26:16 -0800287 /*out*/ HInstruction** result,
Aart Bik7d57d7f2015-12-09 14:39:48 -0800288 bool in_body,
289 bool is_min) const;
Aart Bikaec3cce2015-10-14 17:44:55 -0700290
Aart Bik16d3a652016-09-09 10:33:50 -0700291 void ReplaceInduction(HInductionVarAnalysis::InductionInfo* info,
292 HInstruction* fetch,
293 HInstruction* replacement);
294
Aart Bikd14c5952015-09-08 15:25:15 -0700295 /** Results of prior induction variable analysis. */
Aart Bik52be7e72016-06-23 11:20:41 -0700296 HInductionVarAnalysis* induction_analysis_;
297
298 /** Instruction at which chasing may stop. */
299 HInstruction* chase_hint_;
Aart Bikd14c5952015-09-08 15:25:15 -0700300
Aart Bik22af3be2015-09-10 12:50:58 -0700301 friend class HInductionVarAnalysis;
Aart Bikd14c5952015-09-08 15:25:15 -0700302 friend class InductionVarRangeTest;
303
304 DISALLOW_COPY_AND_ASSIGN(InductionVarRange);
305};
306
307} // namespace art
308
309#endif // ART_COMPILER_OPTIMIZING_INDUCTION_VAR_RANGE_H_