xref: /aosp_15_r20/art/compiler/optimizing/induction_var_range.h (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2015 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #ifndef ART_COMPILER_OPTIMIZING_INDUCTION_VAR_RANGE_H_
18*795d594fSAndroid Build Coastguard Worker #define ART_COMPILER_OPTIMIZING_INDUCTION_VAR_RANGE_H_
19*795d594fSAndroid Build Coastguard Worker 
20*795d594fSAndroid Build Coastguard Worker #include "base/macros.h"
21*795d594fSAndroid Build Coastguard Worker #include "induction_var_analysis.h"
22*795d594fSAndroid Build Coastguard Worker 
23*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
24*795d594fSAndroid Build Coastguard Worker 
25*795d594fSAndroid Build Coastguard Worker /**
26*795d594fSAndroid Build Coastguard Worker  * This class implements range analysis on expressions within loops. It takes the results
27*795d594fSAndroid Build Coastguard Worker  * of induction variable analysis in the constructor and provides a public API to obtain
28*795d594fSAndroid Build Coastguard Worker  * a conservative lower and upper bound value or last value on each instruction in the HIR.
29*795d594fSAndroid Build Coastguard Worker  * The public API also provides a few general-purpose utility methods related to induction.
30*795d594fSAndroid Build Coastguard Worker  *
31*795d594fSAndroid Build Coastguard Worker  * The range analysis is done with a combination of symbolic and partial integral evaluation
32*795d594fSAndroid Build Coastguard Worker  * of expressions. The analysis avoids complications with wrap-around arithmetic on the integral
33*795d594fSAndroid Build Coastguard Worker  * parts but all clients should be aware that wrap-around may occur on any of the symbolic parts.
34*795d594fSAndroid Build Coastguard Worker  * For example, given a known range for [0,100] for i, the evaluation yields range [-100,100]
35*795d594fSAndroid Build Coastguard Worker  * for expression -2*i+100, which is exact, and range [x,x+100] for expression i+x, which may
36*795d594fSAndroid Build Coastguard Worker  * wrap-around anywhere in the range depending on the actual value of x.
37*795d594fSAndroid Build Coastguard Worker  */
38*795d594fSAndroid Build Coastguard Worker class InductionVarRange {
39*795d594fSAndroid Build Coastguard Worker  public:
40*795d594fSAndroid Build Coastguard Worker   /*
41*795d594fSAndroid Build Coastguard Worker    * A value that can be represented as "a * instruction + b" for 32-bit constants, where
42*795d594fSAndroid Build Coastguard Worker    * Value() denotes an unknown lower and upper bound. Although range analysis could yield
43*795d594fSAndroid Build Coastguard Worker    * more complex values, the format is sufficiently powerful to represent useful cases
44*795d594fSAndroid Build Coastguard Worker    * and feeds directly into optimizations like bounds check elimination.
45*795d594fSAndroid Build Coastguard Worker    */
46*795d594fSAndroid Build Coastguard Worker   struct Value {
ValueValue47*795d594fSAndroid Build Coastguard Worker     Value() : instruction(nullptr), a_constant(0), b_constant(0), is_known(false) {}
ValueValue48*795d594fSAndroid Build Coastguard Worker     Value(HInstruction* i, int32_t a, int32_t b)
49*795d594fSAndroid Build Coastguard Worker         : instruction(a != 0 ? i : nullptr), a_constant(a), b_constant(b), is_known(true) {}
ValueValue50*795d594fSAndroid Build Coastguard Worker     explicit Value(int32_t b) : Value(nullptr, 0, b) {}
51*795d594fSAndroid Build Coastguard Worker     // Representation as: a_constant x instruction + b_constant.
52*795d594fSAndroid Build Coastguard Worker     HInstruction* instruction;
53*795d594fSAndroid Build Coastguard Worker     int32_t a_constant;
54*795d594fSAndroid Build Coastguard Worker     int32_t b_constant;
55*795d594fSAndroid Build Coastguard Worker     // If true, represented by prior fields. Otherwise unknown value.
56*795d594fSAndroid Build Coastguard Worker     bool is_known;
57*795d594fSAndroid Build Coastguard Worker   };
58*795d594fSAndroid Build Coastguard Worker 
59*795d594fSAndroid Build Coastguard Worker   explicit InductionVarRange(HInductionVarAnalysis* induction);
60*795d594fSAndroid Build Coastguard Worker 
61*795d594fSAndroid Build Coastguard Worker   /**
62*795d594fSAndroid Build Coastguard Worker    * Given a context block, returns a possibly conservative lower
63*795d594fSAndroid Build Coastguard Worker    * and upper bound on the instruction's value in the output parameters min_val and max_val,
64*795d594fSAndroid Build Coastguard Worker    * respectively. The need_finite_test flag denotes if an additional finite-test is needed
65*795d594fSAndroid Build Coastguard Worker    * to protect the range evaluation inside its loop. The parameter chase_hint defines an
66*795d594fSAndroid Build Coastguard Worker    * instruction at which chasing may stop. Returns false on failure.
67*795d594fSAndroid Build Coastguard Worker    */
68*795d594fSAndroid Build Coastguard Worker   bool GetInductionRange(const HBasicBlock* context,
69*795d594fSAndroid Build Coastguard Worker                          HInstruction* instruction,
70*795d594fSAndroid Build Coastguard Worker                          HInstruction* chase_hint,
71*795d594fSAndroid Build Coastguard Worker                          /*out*/ Value* min_val,
72*795d594fSAndroid Build Coastguard Worker                          /*out*/ Value* max_val,
73*795d594fSAndroid Build Coastguard Worker                          /*out*/ bool* needs_finite_test);
74*795d594fSAndroid Build Coastguard Worker 
75*795d594fSAndroid Build Coastguard Worker   /**
76*795d594fSAndroid Build Coastguard Worker    * Returns true if range analysis is able to generate code for the lower and upper
77*795d594fSAndroid Build Coastguard Worker    * bound expressions on the instruction in the given context. The need_finite_test
78*795d594fSAndroid Build Coastguard Worker    * and need_taken test flags denote if an additional finite-test and/or taken-test
79*795d594fSAndroid Build Coastguard Worker    * are needed to protect the range evaluation inside its loop.
80*795d594fSAndroid Build Coastguard Worker    */
81*795d594fSAndroid Build Coastguard Worker   bool CanGenerateRange(const HBasicBlock* context,
82*795d594fSAndroid Build Coastguard Worker                         HInstruction* instruction,
83*795d594fSAndroid Build Coastguard Worker                         /*out*/ bool* needs_finite_test,
84*795d594fSAndroid Build Coastguard Worker                         /*out*/ bool* needs_taken_test);
85*795d594fSAndroid Build Coastguard Worker 
86*795d594fSAndroid Build Coastguard Worker   /**
87*795d594fSAndroid Build Coastguard Worker    * Generates the actual code in the HIR for the lower and upper bound expressions on the
88*795d594fSAndroid Build Coastguard Worker    * instruction in the given context. Code for the lower and upper bound expression are
89*795d594fSAndroid Build Coastguard Worker    * generated in given block and graph and are returned in the output parameters lower and
90*795d594fSAndroid Build Coastguard Worker    * upper, respectively. For a loop invariant, lower is not set.
91*795d594fSAndroid Build Coastguard Worker    *
92*795d594fSAndroid Build Coastguard Worker    * For example, given expression x+i with range [0, 5] for i, calling this method
93*795d594fSAndroid Build Coastguard Worker    * will generate the following sequence:
94*795d594fSAndroid Build Coastguard Worker    *
95*795d594fSAndroid Build Coastguard Worker    * block:
96*795d594fSAndroid Build Coastguard Worker    *   lower: add x, 0
97*795d594fSAndroid Build Coastguard Worker    *   upper: add x, 5
98*795d594fSAndroid Build Coastguard Worker    *
99*795d594fSAndroid Build Coastguard Worker    * Precondition: CanGenerateRange() returns true.
100*795d594fSAndroid Build Coastguard Worker    */
101*795d594fSAndroid Build Coastguard Worker   void GenerateRange(const HBasicBlock* context,
102*795d594fSAndroid Build Coastguard Worker                      HInstruction* instruction,
103*795d594fSAndroid Build Coastguard Worker                      HGraph* graph,
104*795d594fSAndroid Build Coastguard Worker                      HBasicBlock* block,
105*795d594fSAndroid Build Coastguard Worker                      /*out*/ HInstruction** lower,
106*795d594fSAndroid Build Coastguard Worker                      /*out*/ HInstruction** upper);
107*795d594fSAndroid Build Coastguard Worker 
108*795d594fSAndroid Build Coastguard Worker   /**
109*795d594fSAndroid Build Coastguard Worker    * Generates explicit taken-test for the given `loop_control` instruction. Code is generated in
110*795d594fSAndroid Build Coastguard Worker    * given block and graph. Returns generated taken-test.
111*795d594fSAndroid Build Coastguard Worker    *
112*795d594fSAndroid Build Coastguard Worker    * Precondition: CanGenerateRange() returns true and needs_taken_test is set.
113*795d594fSAndroid Build Coastguard Worker    */
114*795d594fSAndroid Build Coastguard Worker   HInstruction* GenerateTakenTest(HInstruction* loop_control, HGraph* graph, HBasicBlock* block);
115*795d594fSAndroid Build Coastguard Worker 
116*795d594fSAndroid Build Coastguard Worker   /**
117*795d594fSAndroid Build Coastguard Worker    * Returns true if induction analysis is able to generate code for last value of
118*795d594fSAndroid Build Coastguard Worker    * the given instruction inside the closest enveloping loop.
119*795d594fSAndroid Build Coastguard Worker    */
120*795d594fSAndroid Build Coastguard Worker   bool CanGenerateLastValue(HInstruction* instruction);
121*795d594fSAndroid Build Coastguard Worker 
122*795d594fSAndroid Build Coastguard Worker   /**
123*795d594fSAndroid Build Coastguard Worker    * Generates last value of the given instruction in the closest enveloping loop.
124*795d594fSAndroid Build Coastguard Worker    * Code is generated in given block and graph. Returns generated last value.
125*795d594fSAndroid Build Coastguard Worker    *
126*795d594fSAndroid Build Coastguard Worker    * Precondition: CanGenerateLastValue() returns true.
127*795d594fSAndroid Build Coastguard Worker    */
128*795d594fSAndroid Build Coastguard Worker   HInstruction* GenerateLastValue(HInstruction* instruction, HGraph* graph, HBasicBlock* block);
129*795d594fSAndroid Build Coastguard Worker 
130*795d594fSAndroid Build Coastguard Worker   /**
131*795d594fSAndroid Build Coastguard Worker    * Updates all matching `fetch`es with the given `replacement` in all induction information
132*795d594fSAndroid Build Coastguard Worker    * that is present in the loops of the given `instruction`.
133*795d594fSAndroid Build Coastguard Worker    */
134*795d594fSAndroid Build Coastguard Worker   void Replace(HInstruction* instruction, HInstruction* fetch, HInstruction* replacement);
135*795d594fSAndroid Build Coastguard Worker 
136*795d594fSAndroid Build Coastguard Worker   /**
137*795d594fSAndroid Build Coastguard Worker    * Incrementally updates induction information for just the given loop.
138*795d594fSAndroid Build Coastguard Worker    */
ReVisit(const HLoopInformation * loop)139*795d594fSAndroid Build Coastguard Worker   void ReVisit(const HLoopInformation* loop) {
140*795d594fSAndroid Build Coastguard Worker     induction_analysis_->induction_.erase(loop);
141*795d594fSAndroid Build Coastguard Worker     for (HInstructionIterator it(loop->GetHeader()->GetPhis()); !it.Done(); it.Advance()) {
142*795d594fSAndroid Build Coastguard Worker       induction_analysis_->cycles_.erase(it.Current()->AsPhi());
143*795d594fSAndroid Build Coastguard Worker     }
144*795d594fSAndroid Build Coastguard Worker     induction_analysis_->VisitLoop(loop);
145*795d594fSAndroid Build Coastguard Worker   }
146*795d594fSAndroid Build Coastguard Worker 
147*795d594fSAndroid Build Coastguard Worker   /**
148*795d594fSAndroid Build Coastguard Worker    * Lookup an interesting cycle associated with an entry phi.
149*795d594fSAndroid Build Coastguard Worker    */
LookupCycle(HPhi * phi)150*795d594fSAndroid Build Coastguard Worker   ArenaSet<HInstruction*>* LookupCycle(HPhi* phi) const {
151*795d594fSAndroid Build Coastguard Worker     return induction_analysis_->LookupCycle(phi);
152*795d594fSAndroid Build Coastguard Worker   }
153*795d594fSAndroid Build Coastguard Worker 
154*795d594fSAndroid Build Coastguard Worker   /**
155*795d594fSAndroid Build Coastguard Worker    * Checks if the given phi instruction has been classified as anything by
156*795d594fSAndroid Build Coastguard Worker    * induction variable analysis. Returns false for anything that cannot be
157*795d594fSAndroid Build Coastguard Worker    * classified statically, such as reductions or other complex cycles.
158*795d594fSAndroid Build Coastguard Worker    */
IsClassified(HPhi * phi)159*795d594fSAndroid Build Coastguard Worker   bool IsClassified(HPhi* phi) const {
160*795d594fSAndroid Build Coastguard Worker     HLoopInformation* lp = phi->GetBlock()->GetLoopInformation();  // closest enveloping loop
161*795d594fSAndroid Build Coastguard Worker     return (lp != nullptr) && (induction_analysis_->LookupInfo(lp, phi) != nullptr);
162*795d594fSAndroid Build Coastguard Worker   }
163*795d594fSAndroid Build Coastguard Worker 
164*795d594fSAndroid Build Coastguard Worker   /**
165*795d594fSAndroid Build Coastguard Worker    * Checks if header logic of a loop terminates. If trip count is known sets 'trip_count' to its
166*795d594fSAndroid Build Coastguard Worker    * value.
167*795d594fSAndroid Build Coastguard Worker    */
168*795d594fSAndroid Build Coastguard Worker   bool IsFinite(const HLoopInformation* loop, /*out*/ int64_t* trip_count) const;
169*795d594fSAndroid Build Coastguard Worker 
170*795d594fSAndroid Build Coastguard Worker   /**
171*795d594fSAndroid Build Coastguard Worker    * Checks if a trip count is known for the loop and sets 'trip_count' to its value in this case.
172*795d594fSAndroid Build Coastguard Worker    */
173*795d594fSAndroid Build Coastguard Worker   bool HasKnownTripCount(const HLoopInformation* loop, /*out*/ int64_t* trip_count) const;
174*795d594fSAndroid Build Coastguard Worker 
175*795d594fSAndroid Build Coastguard Worker   /**
176*795d594fSAndroid Build Coastguard Worker    * Checks if the given instruction is a unit stride induction inside the closest enveloping
177*795d594fSAndroid Build Coastguard Worker    * loop of the context that is defined by the first parameter (e.g. pass an array reference
178*795d594fSAndroid Build Coastguard Worker    * as context and the index as instruction to make sure the stride is tested against the
179*795d594fSAndroid Build Coastguard Worker    * loop that envelops the reference the closest). Returns invariant offset on success.
180*795d594fSAndroid Build Coastguard Worker    */
181*795d594fSAndroid Build Coastguard Worker   bool IsUnitStride(const HBasicBlock* context,
182*795d594fSAndroid Build Coastguard Worker                     HInstruction* instruction,
183*795d594fSAndroid Build Coastguard Worker                     HGraph* graph,
184*795d594fSAndroid Build Coastguard Worker                     /*out*/ HInstruction** offset) const;
185*795d594fSAndroid Build Coastguard Worker 
186*795d594fSAndroid Build Coastguard Worker   /**
187*795d594fSAndroid Build Coastguard Worker    * Generates the trip count expression for the given loop. Code is generated in given block
188*795d594fSAndroid Build Coastguard Worker    * and graph. The expression is guarded by a taken test if needed. Returns the trip count
189*795d594fSAndroid Build Coastguard Worker    * expression on success or null otherwise.
190*795d594fSAndroid Build Coastguard Worker    */
191*795d594fSAndroid Build Coastguard Worker   HInstruction* GenerateTripCount(const HLoopInformation* loop, HGraph* graph, HBasicBlock* block);
192*795d594fSAndroid Build Coastguard Worker 
193*795d594fSAndroid Build Coastguard Worker  private:
194*795d594fSAndroid Build Coastguard Worker   /*
195*795d594fSAndroid Build Coastguard Worker    * Enum used in IsConstant() request.
196*795d594fSAndroid Build Coastguard Worker    */
197*795d594fSAndroid Build Coastguard Worker   enum ConstantRequest {
198*795d594fSAndroid Build Coastguard Worker     kExact,
199*795d594fSAndroid Build Coastguard Worker     kAtMost,
200*795d594fSAndroid Build Coastguard Worker     kAtLeast
201*795d594fSAndroid Build Coastguard Worker   };
202*795d594fSAndroid Build Coastguard Worker 
203*795d594fSAndroid Build Coastguard Worker   /**
204*795d594fSAndroid Build Coastguard Worker    * Checks if header logic of a loop terminates. If trip count is known (constant) sets
205*795d594fSAndroid Build Coastguard Worker    * 'is_constant' to true and 'trip_count' to the trip count value.
206*795d594fSAndroid Build Coastguard Worker    */
207*795d594fSAndroid Build Coastguard Worker   bool CheckForFiniteAndConstantProps(const HLoopInformation* loop,
208*795d594fSAndroid Build Coastguard Worker                                       /*out*/ bool* is_constant,
209*795d594fSAndroid Build Coastguard Worker                                       /*out*/ int64_t* trip_count) const;
210*795d594fSAndroid Build Coastguard Worker 
211*795d594fSAndroid Build Coastguard Worker   /**
212*795d594fSAndroid Build Coastguard Worker    * Returns true if exact or upper/lower bound on the given induction
213*795d594fSAndroid Build Coastguard Worker    * information is known as a 64-bit constant, which is returned in value.
214*795d594fSAndroid Build Coastguard Worker    */
215*795d594fSAndroid Build Coastguard Worker   bool IsConstant(const HBasicBlock* context,
216*795d594fSAndroid Build Coastguard Worker                   const HLoopInformation* loop,
217*795d594fSAndroid Build Coastguard Worker                   HInductionVarAnalysis::InductionInfo* info,
218*795d594fSAndroid Build Coastguard Worker                   ConstantRequest request,
219*795d594fSAndroid Build Coastguard Worker                   /*out*/ int64_t* value) const;
220*795d594fSAndroid Build Coastguard Worker 
221*795d594fSAndroid Build Coastguard Worker   /** Returns whether induction information can be obtained. */
222*795d594fSAndroid Build Coastguard Worker   bool HasInductionInfo(const HBasicBlock* context,
223*795d594fSAndroid Build Coastguard Worker                         HInstruction* instruction,
224*795d594fSAndroid Build Coastguard Worker                         /*out*/ const HLoopInformation** loop,
225*795d594fSAndroid Build Coastguard Worker                         /*out*/ HInductionVarAnalysis::InductionInfo** info,
226*795d594fSAndroid Build Coastguard Worker                         /*out*/ HInductionVarAnalysis::InductionInfo** trip) const;
227*795d594fSAndroid Build Coastguard Worker 
228*795d594fSAndroid Build Coastguard Worker   bool HasFetchInLoop(HInductionVarAnalysis::InductionInfo* info) const;
229*795d594fSAndroid Build Coastguard Worker   bool NeedsTripCount(const HBasicBlock* context,
230*795d594fSAndroid Build Coastguard Worker                       const HLoopInformation* loop,
231*795d594fSAndroid Build Coastguard Worker                       HInductionVarAnalysis::InductionInfo* info,
232*795d594fSAndroid Build Coastguard Worker                       /*out*/ int64_t* stride_value) const;
233*795d594fSAndroid Build Coastguard Worker   bool IsBodyTripCount(HInductionVarAnalysis::InductionInfo* trip) const;
234*795d594fSAndroid Build Coastguard Worker   bool IsUnsafeTripCount(HInductionVarAnalysis::InductionInfo* trip) const;
235*795d594fSAndroid Build Coastguard Worker   bool IsWellBehavedTripCount(const HBasicBlock* context,
236*795d594fSAndroid Build Coastguard Worker                               const HLoopInformation* loop,
237*795d594fSAndroid Build Coastguard Worker                               HInductionVarAnalysis::InductionInfo* trip) const;
238*795d594fSAndroid Build Coastguard Worker 
239*795d594fSAndroid Build Coastguard Worker   Value GetLinear(const HBasicBlock* context,
240*795d594fSAndroid Build Coastguard Worker                   const HLoopInformation* loop,
241*795d594fSAndroid Build Coastguard Worker                   HInductionVarAnalysis::InductionInfo* info,
242*795d594fSAndroid Build Coastguard Worker                   HInductionVarAnalysis::InductionInfo* trip,
243*795d594fSAndroid Build Coastguard Worker                   bool is_min) const;
244*795d594fSAndroid Build Coastguard Worker   Value GetPolynomial(const HBasicBlock* context,
245*795d594fSAndroid Build Coastguard Worker                       const HLoopInformation* loop,
246*795d594fSAndroid Build Coastguard Worker                       HInductionVarAnalysis::InductionInfo* info,
247*795d594fSAndroid Build Coastguard Worker                       HInductionVarAnalysis::InductionInfo* trip,
248*795d594fSAndroid Build Coastguard Worker                       bool is_min) const;
249*795d594fSAndroid Build Coastguard Worker   Value GetGeometric(const HBasicBlock* context,
250*795d594fSAndroid Build Coastguard Worker                      const HLoopInformation* loop,
251*795d594fSAndroid Build Coastguard Worker                      HInductionVarAnalysis::InductionInfo* info,
252*795d594fSAndroid Build Coastguard Worker                      HInductionVarAnalysis::InductionInfo* trip,
253*795d594fSAndroid Build Coastguard Worker                      bool is_min) const;
254*795d594fSAndroid Build Coastguard Worker   Value GetFetch(const HBasicBlock* context,
255*795d594fSAndroid Build Coastguard Worker                  const HLoopInformation* loop,
256*795d594fSAndroid Build Coastguard Worker                  HInstruction* instruction,
257*795d594fSAndroid Build Coastguard Worker                  HInductionVarAnalysis::InductionInfo* trip,
258*795d594fSAndroid Build Coastguard Worker                  bool is_min) const;
259*795d594fSAndroid Build Coastguard Worker   Value GetVal(const HBasicBlock* context,
260*795d594fSAndroid Build Coastguard Worker                const HLoopInformation* loop,
261*795d594fSAndroid Build Coastguard Worker                HInductionVarAnalysis::InductionInfo* info,
262*795d594fSAndroid Build Coastguard Worker                HInductionVarAnalysis::InductionInfo* trip,
263*795d594fSAndroid Build Coastguard Worker                bool is_min) const;
264*795d594fSAndroid Build Coastguard Worker   Value GetMul(const HBasicBlock* context,
265*795d594fSAndroid Build Coastguard Worker                const HLoopInformation* loop,
266*795d594fSAndroid Build Coastguard Worker                HInductionVarAnalysis::InductionInfo* info1,
267*795d594fSAndroid Build Coastguard Worker                HInductionVarAnalysis::InductionInfo* info2,
268*795d594fSAndroid Build Coastguard Worker                HInductionVarAnalysis::InductionInfo* trip,
269*795d594fSAndroid Build Coastguard Worker                bool is_min) const;
270*795d594fSAndroid Build Coastguard Worker   Value GetDiv(const HBasicBlock* context,
271*795d594fSAndroid Build Coastguard Worker                const HLoopInformation* loop,
272*795d594fSAndroid Build Coastguard Worker                HInductionVarAnalysis::InductionInfo* info1,
273*795d594fSAndroid Build Coastguard Worker                HInductionVarAnalysis::InductionInfo* info2,
274*795d594fSAndroid Build Coastguard Worker                HInductionVarAnalysis::InductionInfo* trip,
275*795d594fSAndroid Build Coastguard Worker                bool is_min) const;
276*795d594fSAndroid Build Coastguard Worker   Value GetRem(const HBasicBlock* context,
277*795d594fSAndroid Build Coastguard Worker                const HLoopInformation* loop,
278*795d594fSAndroid Build Coastguard Worker                HInductionVarAnalysis::InductionInfo* info1,
279*795d594fSAndroid Build Coastguard Worker                HInductionVarAnalysis::InductionInfo* info2) const;
280*795d594fSAndroid Build Coastguard Worker   Value GetXor(const HBasicBlock* context,
281*795d594fSAndroid Build Coastguard Worker                const HLoopInformation* loop,
282*795d594fSAndroid Build Coastguard Worker                HInductionVarAnalysis::InductionInfo* info1,
283*795d594fSAndroid Build Coastguard Worker                HInductionVarAnalysis::InductionInfo* info2) const;
284*795d594fSAndroid Build Coastguard Worker 
285*795d594fSAndroid Build Coastguard Worker   Value MulRangeAndConstant(const HBasicBlock* context,
286*795d594fSAndroid Build Coastguard Worker                             const HLoopInformation* loop,
287*795d594fSAndroid Build Coastguard Worker                             int64_t value,
288*795d594fSAndroid Build Coastguard Worker                             HInductionVarAnalysis::InductionInfo* info,
289*795d594fSAndroid Build Coastguard Worker                             HInductionVarAnalysis::InductionInfo* trip,
290*795d594fSAndroid Build Coastguard Worker                             bool is_min) const;
291*795d594fSAndroid Build Coastguard Worker   Value DivRangeAndConstant(const HBasicBlock* context,
292*795d594fSAndroid Build Coastguard Worker                             const HLoopInformation* loop,
293*795d594fSAndroid Build Coastguard Worker                             int64_t value,
294*795d594fSAndroid Build Coastguard Worker                             HInductionVarAnalysis::InductionInfo* info,
295*795d594fSAndroid Build Coastguard Worker                             HInductionVarAnalysis::InductionInfo* trip,
296*795d594fSAndroid Build Coastguard Worker                             bool is_min) const;
297*795d594fSAndroid Build Coastguard Worker 
298*795d594fSAndroid Build Coastguard Worker   Value AddValue(Value v1, Value v2) const;
299*795d594fSAndroid Build Coastguard Worker   Value SubValue(Value v1, Value v2) const;
300*795d594fSAndroid Build Coastguard Worker   Value MulValue(Value v1, Value v2) const;
301*795d594fSAndroid Build Coastguard Worker   Value DivValue(Value v1, Value v2) const;
302*795d594fSAndroid Build Coastguard Worker   Value MergeVal(Value v1, Value v2, bool is_min) const;
303*795d594fSAndroid Build Coastguard Worker 
304*795d594fSAndroid Build Coastguard Worker   /**
305*795d594fSAndroid Build Coastguard Worker    * Generates code for lower/upper/taken-test or last value in the HIR. Returns true on
306*795d594fSAndroid Build Coastguard Worker    * success. With values nullptr, the method can be used to determine if code generation
307*795d594fSAndroid Build Coastguard Worker    * would be successful without generating actual code yet.
308*795d594fSAndroid Build Coastguard Worker    */
309*795d594fSAndroid Build Coastguard Worker   bool GenerateRangeOrLastValue(const HBasicBlock* context,
310*795d594fSAndroid Build Coastguard Worker                                 HInstruction* instruction,
311*795d594fSAndroid Build Coastguard Worker                                 bool is_last_val,
312*795d594fSAndroid Build Coastguard Worker                                 HGraph* graph,
313*795d594fSAndroid Build Coastguard Worker                                 HBasicBlock* block,
314*795d594fSAndroid Build Coastguard Worker                                 /*out*/ HInstruction** lower,
315*795d594fSAndroid Build Coastguard Worker                                 /*out*/ HInstruction** upper,
316*795d594fSAndroid Build Coastguard Worker                                 /*out*/ HInstruction** taken_test,
317*795d594fSAndroid Build Coastguard Worker                                 /*out*/ int64_t* stride_value,
318*795d594fSAndroid Build Coastguard Worker                                 /*out*/ bool* needs_finite_test,
319*795d594fSAndroid Build Coastguard Worker                                 /*out*/ bool* needs_taken_test) const;
320*795d594fSAndroid Build Coastguard Worker 
321*795d594fSAndroid Build Coastguard Worker   bool GenerateLastValueLinear(const HBasicBlock* context,
322*795d594fSAndroid Build Coastguard Worker                                const HLoopInformation* loop,
323*795d594fSAndroid Build Coastguard Worker                                HInductionVarAnalysis::InductionInfo* info,
324*795d594fSAndroid Build Coastguard Worker                                HInductionVarAnalysis::InductionInfo* trip,
325*795d594fSAndroid Build Coastguard Worker                                HGraph* graph,
326*795d594fSAndroid Build Coastguard Worker                                HBasicBlock* block,
327*795d594fSAndroid Build Coastguard Worker                                bool is_min,
328*795d594fSAndroid Build Coastguard Worker                                /*out*/ HInstruction** result,
329*795d594fSAndroid Build Coastguard Worker                                /*inout*/ bool* needs_taken_test) const;
330*795d594fSAndroid Build Coastguard Worker 
331*795d594fSAndroid Build Coastguard Worker   bool GenerateLastValuePolynomial(const HBasicBlock* context,
332*795d594fSAndroid Build Coastguard Worker                                    const HLoopInformation* loop,
333*795d594fSAndroid Build Coastguard Worker                                    HInductionVarAnalysis::InductionInfo* info,
334*795d594fSAndroid Build Coastguard Worker                                    HInductionVarAnalysis::InductionInfo* trip,
335*795d594fSAndroid Build Coastguard Worker                                    HGraph* graph,
336*795d594fSAndroid Build Coastguard Worker                                    HBasicBlock* block,
337*795d594fSAndroid Build Coastguard Worker                                    /*out*/HInstruction** result) const;
338*795d594fSAndroid Build Coastguard Worker 
339*795d594fSAndroid Build Coastguard Worker   bool GenerateLastValueGeometric(const HBasicBlock* context,
340*795d594fSAndroid Build Coastguard Worker                                   const HLoopInformation* loop,
341*795d594fSAndroid Build Coastguard Worker                                   HInductionVarAnalysis::InductionInfo* info,
342*795d594fSAndroid Build Coastguard Worker                                   HInductionVarAnalysis::InductionInfo* trip,
343*795d594fSAndroid Build Coastguard Worker                                   HGraph* graph,
344*795d594fSAndroid Build Coastguard Worker                                   HBasicBlock* block,
345*795d594fSAndroid Build Coastguard Worker                                   /*out*/HInstruction** result) const;
346*795d594fSAndroid Build Coastguard Worker 
347*795d594fSAndroid Build Coastguard Worker   bool GenerateLastValueWrapAround(const HBasicBlock* context,
348*795d594fSAndroid Build Coastguard Worker                                    const HLoopInformation* loop,
349*795d594fSAndroid Build Coastguard Worker                                    HInductionVarAnalysis::InductionInfo* info,
350*795d594fSAndroid Build Coastguard Worker                                    HInductionVarAnalysis::InductionInfo* trip,
351*795d594fSAndroid Build Coastguard Worker                                    HGraph* graph,
352*795d594fSAndroid Build Coastguard Worker                                    HBasicBlock* block,
353*795d594fSAndroid Build Coastguard Worker                                    /*out*/HInstruction** result) const;
354*795d594fSAndroid Build Coastguard Worker 
355*795d594fSAndroid Build Coastguard Worker   bool GenerateLastValuePeriodic(const HBasicBlock* context,
356*795d594fSAndroid Build Coastguard Worker                                  const HLoopInformation* loop,
357*795d594fSAndroid Build Coastguard Worker                                  HInductionVarAnalysis::InductionInfo* info,
358*795d594fSAndroid Build Coastguard Worker                                  HInductionVarAnalysis::InductionInfo* trip,
359*795d594fSAndroid Build Coastguard Worker                                  HGraph* graph,
360*795d594fSAndroid Build Coastguard Worker                                  HBasicBlock* block,
361*795d594fSAndroid Build Coastguard Worker                                  /*out*/ HInstruction** result,
362*795d594fSAndroid Build Coastguard Worker                                  /*inout*/ bool* needs_taken_test) const;
363*795d594fSAndroid Build Coastguard Worker 
364*795d594fSAndroid Build Coastguard Worker   bool GenerateCode(const HBasicBlock* context,
365*795d594fSAndroid Build Coastguard Worker                     const HLoopInformation* loop,
366*795d594fSAndroid Build Coastguard Worker                     HInductionVarAnalysis::InductionInfo* info,
367*795d594fSAndroid Build Coastguard Worker                     HInductionVarAnalysis::InductionInfo* trip,
368*795d594fSAndroid Build Coastguard Worker                     HGraph* graph,
369*795d594fSAndroid Build Coastguard Worker                     HBasicBlock* block,
370*795d594fSAndroid Build Coastguard Worker                     bool is_min,
371*795d594fSAndroid Build Coastguard Worker                     /*out*/ HInstruction** result,
372*795d594fSAndroid Build Coastguard Worker                     // TODO(solanes): Remove default value when all cases have been assessed.
373*795d594fSAndroid Build Coastguard Worker                     bool allow_potential_overflow = true) const;
374*795d594fSAndroid Build Coastguard Worker 
375*795d594fSAndroid Build Coastguard Worker   bool TryGenerateAddWithoutOverflow(const HBasicBlock* context,
376*795d594fSAndroid Build Coastguard Worker                                      const HLoopInformation* loop,
377*795d594fSAndroid Build Coastguard Worker                                      HInductionVarAnalysis::InductionInfo* info,
378*795d594fSAndroid Build Coastguard Worker                                      HGraph* graph,
379*795d594fSAndroid Build Coastguard Worker                                      /*in*/ HInstruction* opa,
380*795d594fSAndroid Build Coastguard Worker                                      /*in*/ HInstruction* opb,
381*795d594fSAndroid Build Coastguard Worker                                      /*out*/ HInstruction** result) const;
382*795d594fSAndroid Build Coastguard Worker 
383*795d594fSAndroid Build Coastguard Worker   bool TryGenerateSubWithoutOverflow(const HBasicBlock* context,
384*795d594fSAndroid Build Coastguard Worker                                      const HLoopInformation* loop,
385*795d594fSAndroid Build Coastguard Worker                                      HInductionVarAnalysis::InductionInfo* info,
386*795d594fSAndroid Build Coastguard Worker                                      HGraph* graph,
387*795d594fSAndroid Build Coastguard Worker                                      /*in*/ HInstruction* opa,
388*795d594fSAndroid Build Coastguard Worker                                      /*out*/ HInstruction** result) const;
389*795d594fSAndroid Build Coastguard Worker 
390*795d594fSAndroid Build Coastguard Worker   // Try to guard the taken test with an HSelect instruction. Returns true if it can generate the
391*795d594fSAndroid Build Coastguard Worker   // code, or false otherwise. The caller is responsible of updating `needs_taken_test`.
392*795d594fSAndroid Build Coastguard Worker   bool TryGenerateTakenTest(const HBasicBlock* context,
393*795d594fSAndroid Build Coastguard Worker                             const HLoopInformation* loop,
394*795d594fSAndroid Build Coastguard Worker                             HInductionVarAnalysis::InductionInfo* info,
395*795d594fSAndroid Build Coastguard Worker                             HGraph* graph,
396*795d594fSAndroid Build Coastguard Worker                             HBasicBlock* block,
397*795d594fSAndroid Build Coastguard Worker                             /*inout*/ HInstruction** result,
398*795d594fSAndroid Build Coastguard Worker                             /*inout*/ HInstruction* not_taken_result) const;
399*795d594fSAndroid Build Coastguard Worker 
400*795d594fSAndroid Build Coastguard Worker   void ReplaceInduction(HInductionVarAnalysis::InductionInfo* info,
401*795d594fSAndroid Build Coastguard Worker                         HInstruction* fetch,
402*795d594fSAndroid Build Coastguard Worker                         HInstruction* replacement);
403*795d594fSAndroid Build Coastguard Worker 
404*795d594fSAndroid Build Coastguard Worker   /** Results of prior induction variable analysis. */
405*795d594fSAndroid Build Coastguard Worker   HInductionVarAnalysis* induction_analysis_;
406*795d594fSAndroid Build Coastguard Worker 
407*795d594fSAndroid Build Coastguard Worker   /** Instruction at which chasing may stop. */
408*795d594fSAndroid Build Coastguard Worker   HInstruction* chase_hint_;
409*795d594fSAndroid Build Coastguard Worker 
410*795d594fSAndroid Build Coastguard Worker   friend class HInductionVarAnalysis;
411*795d594fSAndroid Build Coastguard Worker   friend class InductionVarRangeTest;
412*795d594fSAndroid Build Coastguard Worker 
413*795d594fSAndroid Build Coastguard Worker   DISALLOW_COPY_AND_ASSIGN(InductionVarRange);
414*795d594fSAndroid Build Coastguard Worker };
415*795d594fSAndroid Build Coastguard Worker 
416*795d594fSAndroid Build Coastguard Worker }  // namespace art
417*795d594fSAndroid Build Coastguard Worker 
418*795d594fSAndroid Build Coastguard Worker #endif  // ART_COMPILER_OPTIMIZING_INDUCTION_VAR_RANGE_H_
419