blob: 8280c8bedc252bae4c9b10706782e574e196182f [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 /**
60 * Given a context denoted by the first instruction, returns a,
61 * possibly conservative, lower bound on the instruction's value.
62 */
63 Value GetMinInduction(HInstruction* context, HInstruction* instruction);
64
65 /**
66 * Given a context denoted by the first instruction, returns a,
67 * possibly conservative, upper bound on the instruction's value.
68 */
69 Value GetMaxInduction(HInstruction* context, HInstruction* instruction);
70
71 private:
72 //
73 // Private helper methods.
74 //
75
Aart Bikb3365e02015-09-21 14:45:05 -070076 HInductionVarAnalysis::InductionInfo* GetTripCount(HLoopInformation* loop, HInstruction* context);
Aart Bikd14c5952015-09-08 15:25:15 -070077
Aart Bik22af3be2015-09-10 12:50:58 -070078 static Value GetFetch(HInstruction* instruction,
79 HInductionVarAnalysis::InductionInfo* trip,
Aart Bikb3365e02015-09-21 14:45:05 -070080 bool is_min);
Aart Bikd14c5952015-09-08 15:25:15 -070081
Aart Bikcd26feb2015-09-23 17:50:50 -070082 static Value GetVal(HInductionVarAnalysis::InductionInfo* info,
83 HInductionVarAnalysis::InductionInfo* trip,
84 bool is_min);
Aart Bikd14c5952015-09-08 15:25:15 -070085 static Value GetMul(HInductionVarAnalysis::InductionInfo* info1,
86 HInductionVarAnalysis::InductionInfo* info2,
Aart Bik22af3be2015-09-10 12:50:58 -070087 HInductionVarAnalysis::InductionInfo* trip,
Aart Bikb3365e02015-09-21 14:45:05 -070088 bool is_min);
Aart Bikd14c5952015-09-08 15:25:15 -070089 static Value GetDiv(HInductionVarAnalysis::InductionInfo* info1,
90 HInductionVarAnalysis::InductionInfo* info2,
Aart Bik22af3be2015-09-10 12:50:58 -070091 HInductionVarAnalysis::InductionInfo* trip,
Aart Bikb3365e02015-09-21 14:45:05 -070092 bool is_min);
Aart Bikd14c5952015-09-08 15:25:15 -070093
Aart Bikb3365e02015-09-21 14:45:05 -070094 static Value AddValue(Value v1, Value v2);
95 static Value SubValue(Value v1, Value v2);
96 static Value MulValue(Value v1, Value v2);
97 static Value DivValue(Value v1, Value v2);
Aart Bikcd26feb2015-09-23 17:50:50 -070098 static Value MergeVal(Value v1, Value v2, bool is_min);
Aart Bikd14c5952015-09-08 15:25:15 -070099
100 /** Results of prior induction variable analysis. */
101 HInductionVarAnalysis *induction_analysis_;
102
Aart Bik22af3be2015-09-10 12:50:58 -0700103 friend class HInductionVarAnalysis;
Aart Bikd14c5952015-09-08 15:25:15 -0700104 friend class InductionVarRangeTest;
105
106 DISALLOW_COPY_AND_ASSIGN(InductionVarRange);
107};
108
109} // namespace art
110
111#endif // ART_COMPILER_OPTIMIZING_INDUCTION_VAR_RANGE_H_