blob: dc1814b56f0845f487627207ad1e5ee55dd57271 [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
Vladimir Markoe2727152019-10-10 10:46:42 +010020#include "base/macros.h"
Aart Bikd14c5952015-09-08 15:25:15 -070021#include "induction_var_analysis.h"
22
Vladimir Markoe2727152019-10-10 10:46:42 +010023namespace art HIDDEN {
Aart Bikd14c5952015-09-08 15:25:15 -070024
25/**
Aart Bikb3365e02015-09-21 14:45:05 -070026 * This class implements range analysis on expressions within loops. It takes the results
27 * of induction variable analysis in the constructor and provides a public API to obtain
Aart Bik8e02e3e2017-02-28 14:41:55 -080028 * a conservative lower and upper bound value or last value on each instruction in the HIR.
29 * The public API also provides a few general-purpose utility methods related to induction.
Aart Bikd14c5952015-09-08 15:25:15 -070030 *
Aart Bikb3365e02015-09-21 14:45:05 -070031 * The range analysis is done with a combination of symbolic and partial integral evaluation
32 * of expressions. The analysis avoids complications with wrap-around arithmetic on the integral
33 * parts but all clients should be aware that wrap-around may occur on any of the symbolic parts.
34 * For example, given a known range for [0,100] for i, the evaluation yields range [-100,100]
35 * for expression -2*i+100, which is exact, and range [x,x+100] for expression i+x, which may
36 * wrap-around anywhere in the range depending on the actual value of x.
Aart Bikd14c5952015-09-08 15:25:15 -070037 */
38class InductionVarRange {
39 public:
40 /*
41 * A value that can be represented as "a * instruction + b" for 32-bit constants, where
Aart Bikb3365e02015-09-21 14:45:05 -070042 * Value() denotes an unknown lower and upper bound. Although range analysis could yield
43 * more complex values, the format is sufficiently powerful to represent useful cases
44 * and feeds directly into optimizations like bounds check elimination.
Aart Bikd14c5952015-09-08 15:25:15 -070045 */
46 struct Value {
Aart Bikb3365e02015-09-21 14:45:05 -070047 Value() : instruction(nullptr), a_constant(0), b_constant(0), is_known(false) {}
Aart Bikd14c5952015-09-08 15:25:15 -070048 Value(HInstruction* i, int32_t a, int32_t b)
Aart Bikb3365e02015-09-21 14:45:05 -070049 : instruction(a != 0 ? i : nullptr), a_constant(a), b_constant(b), is_known(true) {}
Aart Bikd14c5952015-09-08 15:25:15 -070050 explicit Value(int32_t b) : Value(nullptr, 0, b) {}
Aart Bikb3365e02015-09-21 14:45:05 -070051 // Representation as: a_constant x instruction + b_constant.
Aart Bikd14c5952015-09-08 15:25:15 -070052 HInstruction* instruction;
53 int32_t a_constant;
54 int32_t b_constant;
Aart Bikb3365e02015-09-21 14:45:05 -070055 // If true, represented by prior fields. Otherwise unknown value.
56 bool is_known;
Aart Bikd14c5952015-09-08 15:25:15 -070057 };
58
59 explicit InductionVarRange(HInductionVarAnalysis* induction);
60
61 /**
Aart Bik52be7e72016-06-23 11:20:41 -070062 * Given a context denoted by the first instruction, returns a possibly conservative lower
63 * and upper bound on the instruction's value in the output parameters min_val and max_val,
64 * respectively. The need_finite_test flag denotes if an additional finite-test is needed
65 * to protect the range evaluation inside its loop. The parameter chase_hint defines an
66 * instruction at which chasing may stop. Returns false on failure.
Aart Bikd14c5952015-09-08 15:25:15 -070067 */
Aart Bik1fc3afb2016-02-02 13:26:16 -080068 bool GetInductionRange(HInstruction* context,
Aart Bik389b3db2015-10-28 14:23:40 -070069 HInstruction* instruction,
Aart Bik52be7e72016-06-23 11:20:41 -070070 HInstruction* chase_hint,
Aart Bik1fc3afb2016-02-02 13:26:16 -080071 /*out*/ Value* min_val,
72 /*out*/ Value* max_val,
73 /*out*/ bool* needs_finite_test);
Aart Bikd14c5952015-09-08 15:25:15 -070074
75 /**
Aart Bik389b3db2015-10-28 14:23:40 -070076 * Returns true if range analysis is able to generate code for the lower and upper
77 * bound expressions on the instruction in the given context. The need_finite_test
78 * and need_taken test flags denote if an additional finite-test and/or taken-test
79 * are needed to protect the range evaluation inside its loop.
Aart Bikd14c5952015-09-08 15:25:15 -070080 */
Aart Bik16d3a652016-09-09 10:33:50 -070081 bool CanGenerateRange(HInstruction* context,
82 HInstruction* instruction,
83 /*out*/ bool* needs_finite_test,
84 /*out*/ bool* needs_taken_test);
Aart Bikaec3cce2015-10-14 17:44:55 -070085
86 /**
87 * Generates the actual code in the HIR for the lower and upper bound expressions on the
88 * instruction in the given context. Code for the lower and upper bound expression are
Aart Bik389b3db2015-10-28 14:23:40 -070089 * generated in given block and graph and are returned in the output parameters lower and
90 * upper, respectively. For a loop invariant, lower is not set.
Aart Bikaec3cce2015-10-14 17:44:55 -070091 *
92 * For example, given expression x+i with range [0, 5] for i, calling this method
93 * will generate the following sequence:
94 *
95 * block:
96 * lower: add x, 0
97 * upper: add x, 5
Aart Bik389b3db2015-10-28 14:23:40 -070098 *
Aart Bik16d3a652016-09-09 10:33:50 -070099 * Precondition: CanGenerateRange() returns true.
Aart Bikaec3cce2015-10-14 17:44:55 -0700100 */
Aart Bik16d3a652016-09-09 10:33:50 -0700101 void GenerateRange(HInstruction* context,
102 HInstruction* instruction,
103 HGraph* graph,
104 HBasicBlock* block,
105 /*out*/ HInstruction** lower,
106 /*out*/ HInstruction** upper);
Aart Bik389b3db2015-10-28 14:23:40 -0700107
108 /**
109 * Generates explicit taken-test for the loop in the given context. Code is generated in
Aart Bik16d3a652016-09-09 10:33:50 -0700110 * given block and graph. Returns generated taken-test.
Aart Bik389b3db2015-10-28 14:23:40 -0700111 *
Aart Bik16d3a652016-09-09 10:33:50 -0700112 * Precondition: CanGenerateRange() returns true and needs_taken_test is set.
Aart Bik389b3db2015-10-28 14:23:40 -0700113 */
Aart Bik16d3a652016-09-09 10:33:50 -0700114 HInstruction* GenerateTakenTest(HInstruction* context, HGraph* graph, HBasicBlock* block);
115
116 /**
117 * Returns true if induction analysis is able to generate code for last value of
118 * the given instruction inside the closest enveloping loop.
119 */
120 bool CanGenerateLastValue(HInstruction* instruction);
121
122 /**
123 * Generates last value of the given instruction in the closest enveloping loop.
124 * Code is generated in given block and graph. Returns generated last value.
125 *
126 * Precondition: CanGenerateLastValue() returns true.
127 */
128 HInstruction* GenerateLastValue(HInstruction* instruction, HGraph* graph, HBasicBlock* block);
129
130 /**
131 * Updates all matching fetches with the given replacement in all induction information
132 * that is associated with the given instruction.
133 */
134 void Replace(HInstruction* instruction, HInstruction* fetch, HInstruction* replacement);
Aart Bikaec3cce2015-10-14 17:44:55 -0700135
Aart Bik482095d2016-10-10 15:39:10 -0700136 /**
137 * Incrementally updates induction information for just the given loop.
138 */
139 void ReVisit(HLoopInformation* loop) {
140 induction_analysis_->induction_.erase(loop);
Aart Bikcc42be02016-10-20 16:14:16 -0700141 for (HInstructionIterator it(loop->GetHeader()->GetPhis()); !it.Done(); it.Advance()) {
142 induction_analysis_->cycles_.erase(it.Current()->AsPhi());
143 }
Aart Bik482095d2016-10-10 15:39:10 -0700144 induction_analysis_->VisitLoop(loop);
145 }
146
Aart Bik9abf8942016-10-14 09:49:42 -0700147 /**
Aart Bikcc42be02016-10-20 16:14:16 -0700148 * Lookup an interesting cycle associated with an entry phi.
149 */
150 ArenaSet<HInstruction*>* LookupCycle(HPhi* phi) const {
151 return induction_analysis_->LookupCycle(phi);
152 }
153
154 /**
Aart Bikb29f6842017-07-28 15:58:41 -0700155 * Checks if the given phi instruction has been classified as anything by
156 * induction variable analysis. Returns false for anything that cannot be
157 * classified statically, such as reductions or other complex cycles.
158 */
159 bool IsClassified(HPhi* phi) const {
160 HLoopInformation* lp = phi->GetBlock()->GetLoopInformation(); // closest enveloping loop
161 return (lp != nullptr) && (induction_analysis_->LookupInfo(lp, phi) != nullptr);
162 }
163
164 /**
Artem Serov121f2032017-10-23 19:19:06 +0100165 * Checks if header logic of a loop terminates. If trip count is known sets 'trip_count' to its
166 * value.
Aart Bik9abf8942016-10-14 09:49:42 -0700167 */
Artem Serov121f2032017-10-23 19:19:06 +0100168 bool IsFinite(HLoopInformation* loop, /*out*/ int64_t* trip_count) const;
169
170 /**
171 * Checks if a trip count is known for the loop and sets 'trip_count' to its value in this case.
172 */
173 bool HasKnownTripCount(HLoopInformation* loop, /*out*/ int64_t* trip_count) const;
Aart Bik9abf8942016-10-14 09:49:42 -0700174
Aart Bik8e02e3e2017-02-28 14:41:55 -0800175 /**
Aart Bikfa762962017-04-07 11:33:37 -0700176 * Checks if the given instruction is a unit stride induction inside the closest enveloping
177 * loop of the context that is defined by the first parameter (e.g. pass an array reference
178 * as context and the index as instruction to make sure the stride is tested against the
179 * loop that envelops the reference the closest). Returns invariant offset on success.
Aart Bik8e02e3e2017-02-28 14:41:55 -0800180 */
Aart Bikfa762962017-04-07 11:33:37 -0700181 bool IsUnitStride(HInstruction* context,
182 HInstruction* instruction,
Aart Bik37dc4df2017-06-28 14:08:00 -0700183 HGraph* graph,
Aart Bikfa762962017-04-07 11:33:37 -0700184 /*out*/ HInstruction** offset) const;
Aart Bik8e02e3e2017-02-28 14:41:55 -0800185
186 /**
187 * Generates the trip count expression for the given loop. Code is generated in given block
188 * and graph. The expression is guarded by a taken test if needed. Returns the trip count
189 * expression on success or null otherwise.
190 */
191 HInstruction* GenerateTripCount(HLoopInformation* loop, HGraph* graph, HBasicBlock* block);
192
Aart Bikd14c5952015-09-08 15:25:15 -0700193 private:
Aart Bik97412c922016-02-19 20:14:38 -0800194 /*
195 * Enum used in IsConstant() request.
196 */
197 enum ConstantRequest {
198 kExact,
199 kAtMost,
200 kAtLeast
201 };
202
203 /**
Artem Serov121f2032017-10-23 19:19:06 +0100204 * Checks if header logic of a loop terminates. If trip count is known (constant) sets
205 * 'is_constant' to true and 'trip_count' to the trip count value.
206 */
207 bool CheckForFiniteAndConstantProps(HLoopInformation* loop,
208 /*out*/ bool* is_constant,
209 /*out*/ int64_t* trip_count) const;
210
211 /**
Aart Bik97412c922016-02-19 20:14:38 -0800212 * Returns true if exact or upper/lower bound on the given induction
213 * information is known as a 64-bit constant, which is returned in value.
214 */
215 bool IsConstant(HInductionVarAnalysis::InductionInfo* info,
216 ConstantRequest request,
Aart Bik52be7e72016-06-23 11:20:41 -0700217 /*out*/ int64_t* value) const;
Aart Bik97412c922016-02-19 20:14:38 -0800218
Aart Bik52be7e72016-06-23 11:20:41 -0700219 /** Returns whether induction information can be obtained. */
220 bool HasInductionInfo(HInstruction* context,
221 HInstruction* instruction,
222 /*out*/ HLoopInformation** loop,
223 /*out*/ HInductionVarAnalysis::InductionInfo** info,
224 /*out*/ HInductionVarAnalysis::InductionInfo** trip) const;
225
226 bool HasFetchInLoop(HInductionVarAnalysis::InductionInfo* info) const;
Aart Bik16d3a652016-09-09 10:33:50 -0700227 bool NeedsTripCount(HInductionVarAnalysis::InductionInfo* info,
228 /*out*/ int64_t* stride_value) const;
Aart Bik7d57d7f2015-12-09 14:39:48 -0800229 bool IsBodyTripCount(HInductionVarAnalysis::InductionInfo* trip) const;
230 bool IsUnsafeTripCount(HInductionVarAnalysis::InductionInfo* trip) const;
Aart Bik52be7e72016-06-23 11:20:41 -0700231 bool IsWellBehavedTripCount(HInductionVarAnalysis::InductionInfo* trip) const;
Aart Bikd14c5952015-09-08 15:25:15 -0700232
Aart Bik7d57d7f2015-12-09 14:39:48 -0800233 Value GetLinear(HInductionVarAnalysis::InductionInfo* info,
234 HInductionVarAnalysis::InductionInfo* trip,
235 bool in_body,
236 bool is_min) const;
Aart Bikdf7822e2016-12-06 10:05:30 -0800237 Value GetPolynomial(HInductionVarAnalysis::InductionInfo* info,
238 HInductionVarAnalysis::InductionInfo* trip,
239 bool in_body,
240 bool is_min) const;
Aart Bikc071a012016-12-01 10:22:31 -0800241 Value GetGeometric(HInductionVarAnalysis::InductionInfo* info,
242 HInductionVarAnalysis::InductionInfo* trip,
243 bool in_body,
244 bool is_min) const;
Aart Bik7d57d7f2015-12-09 14:39:48 -0800245 Value GetFetch(HInstruction* instruction,
246 HInductionVarAnalysis::InductionInfo* trip,
247 bool in_body,
248 bool is_min) const;
249 Value GetVal(HInductionVarAnalysis::InductionInfo* info,
250 HInductionVarAnalysis::InductionInfo* trip,
251 bool in_body,
252 bool is_min) const;
253 Value GetMul(HInductionVarAnalysis::InductionInfo* info1,
254 HInductionVarAnalysis::InductionInfo* info2,
255 HInductionVarAnalysis::InductionInfo* trip,
256 bool in_body,
257 bool is_min) const;
258 Value GetDiv(HInductionVarAnalysis::InductionInfo* info1,
259 HInductionVarAnalysis::InductionInfo* info2,
260 HInductionVarAnalysis::InductionInfo* trip,
261 bool in_body,
262 bool is_min) const;
Aart Bikdf7822e2016-12-06 10:05:30 -0800263 Value GetRem(HInductionVarAnalysis::InductionInfo* info1,
264 HInductionVarAnalysis::InductionInfo* info2) const;
Aart Bik7dc96932016-10-12 10:01:05 -0700265 Value GetXor(HInductionVarAnalysis::InductionInfo* info1,
266 HInductionVarAnalysis::InductionInfo* info2) const;
Aart Bikd14c5952015-09-08 15:25:15 -0700267
Aart Bik52be7e72016-06-23 11:20:41 -0700268 Value MulRangeAndConstant(int64_t value,
269 HInductionVarAnalysis::InductionInfo* info,
270 HInductionVarAnalysis::InductionInfo* trip,
271 bool in_body,
272 bool is_min) const;
273 Value DivRangeAndConstant(int64_t value,
274 HInductionVarAnalysis::InductionInfo* info,
275 HInductionVarAnalysis::InductionInfo* trip,
276 bool in_body,
277 bool is_min) const;
Aart Bikd14c5952015-09-08 15:25:15 -0700278
Aart Bik7d57d7f2015-12-09 14:39:48 -0800279 Value AddValue(Value v1, Value v2) const;
280 Value SubValue(Value v1, Value v2) const;
281 Value MulValue(Value v1, Value v2) const;
282 Value DivValue(Value v1, Value v2) const;
283 Value MergeVal(Value v1, Value v2, bool is_min) const;
Aart Bikd14c5952015-09-08 15:25:15 -0700284
Aart Bikaec3cce2015-10-14 17:44:55 -0700285 /**
Aart Bik16d3a652016-09-09 10:33:50 -0700286 * Generates code for lower/upper/taken-test or last value in the HIR. Returns true on
287 * success. With values nullptr, the method can be used to determine if code generation
Aart Bikaec3cce2015-10-14 17:44:55 -0700288 * would be successful without generating actual code yet.
289 */
Aart Bik9abf8942016-10-14 09:49:42 -0700290 bool GenerateRangeOrLastValue(HInstruction* context,
291 HInstruction* instruction,
292 bool is_last_val,
293 HGraph* graph,
294 HBasicBlock* block,
295 /*out*/ HInstruction** lower,
296 /*out*/ HInstruction** upper,
297 /*out*/ HInstruction** taken_test,
298 /*out*/ int64_t* stride_value,
299 /*out*/ bool* needs_finite_test,
300 /*out*/ bool* needs_taken_test) const;
301
Aart Bikdf7822e2016-12-06 10:05:30 -0800302 bool GenerateLastValuePolynomial(HInductionVarAnalysis::InductionInfo* info,
303 HInductionVarAnalysis::InductionInfo* trip,
304 HGraph* graph,
305 HBasicBlock* block,
306 /*out*/HInstruction** result) const;
307
Aart Bikc071a012016-12-01 10:22:31 -0800308 bool GenerateLastValueGeometric(HInductionVarAnalysis::InductionInfo* info,
309 HInductionVarAnalysis::InductionInfo* trip,
310 HGraph* graph,
311 HBasicBlock* block,
312 /*out*/HInstruction** result) const;
313
Aart Bikdf7822e2016-12-06 10:05:30 -0800314 bool GenerateLastValueWrapAround(HInductionVarAnalysis::InductionInfo* info,
315 HInductionVarAnalysis::InductionInfo* trip,
316 HGraph* graph,
317 HBasicBlock* block,
318 /*out*/HInstruction** result) const;
319
Aart Bik9abf8942016-10-14 09:49:42 -0700320 bool GenerateLastValuePeriodic(HInductionVarAnalysis::InductionInfo* info,
321 HInductionVarAnalysis::InductionInfo* trip,
322 HGraph* graph,
323 HBasicBlock* block,
324 /*out*/HInstruction** result,
325 /*out*/ bool* needs_taken_test) const;
Aart Bikaec3cce2015-10-14 17:44:55 -0700326
Aart Bik7d57d7f2015-12-09 14:39:48 -0800327 bool GenerateCode(HInductionVarAnalysis::InductionInfo* info,
328 HInductionVarAnalysis::InductionInfo* trip,
329 HGraph* graph,
330 HBasicBlock* block,
Aart Bik1fc3afb2016-02-02 13:26:16 -0800331 /*out*/ HInstruction** result,
Aart Bik7d57d7f2015-12-09 14:39:48 -0800332 bool in_body,
333 bool is_min) const;
Aart Bikaec3cce2015-10-14 17:44:55 -0700334
Aart Bik16d3a652016-09-09 10:33:50 -0700335 void ReplaceInduction(HInductionVarAnalysis::InductionInfo* info,
336 HInstruction* fetch,
337 HInstruction* replacement);
338
Aart Bikd14c5952015-09-08 15:25:15 -0700339 /** Results of prior induction variable analysis. */
Aart Bik52be7e72016-06-23 11:20:41 -0700340 HInductionVarAnalysis* induction_analysis_;
341
342 /** Instruction at which chasing may stop. */
343 HInstruction* chase_hint_;
Aart Bikd14c5952015-09-08 15:25:15 -0700344
Aart Bik22af3be2015-09-10 12:50:58 -0700345 friend class HInductionVarAnalysis;
Aart Bikd14c5952015-09-08 15:25:15 -0700346 friend class InductionVarRangeTest;
347
348 DISALLOW_COPY_AND_ASSIGN(InductionVarRange);
349};
350
351} // namespace art
352
353#endif // ART_COMPILER_OPTIMIZING_INDUCTION_VAR_RANGE_H_