Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 22 | namespace art { |
| 23 | |
| 24 | /** |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 25 | * 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 Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 28 | * |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 29 | * 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 Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 35 | */ |
| 36 | class InductionVarRange { |
| 37 | public: |
| 38 | /* |
| 39 | * A value that can be represented as "a * instruction + b" for 32-bit constants, where |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 40 | * 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 Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 43 | */ |
| 44 | struct Value { |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 45 | Value() : instruction(nullptr), a_constant(0), b_constant(0), is_known(false) {} |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 46 | Value(HInstruction* i, int32_t a, int32_t b) |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 47 | : instruction(a != 0 ? i : nullptr), a_constant(a), b_constant(b), is_known(true) {} |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 48 | explicit Value(int32_t b) : Value(nullptr, 0, b) {} |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 49 | // Representation as: a_constant x instruction + b_constant. |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 50 | HInstruction* instruction; |
| 51 | int32_t a_constant; |
| 52 | int32_t b_constant; |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 53 | // If true, represented by prior fields. Otherwise unknown value. |
| 54 | bool is_known; |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | explicit InductionVarRange(HInductionVarAnalysis* induction); |
| 58 | |
| 59 | /** |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 60 | * 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 Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 65 | */ |
Aart Bik | 1fc3afb | 2016-02-02 13:26:16 -0800 | [diff] [blame] | 66 | bool GetInductionRange(HInstruction* context, |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 67 | HInstruction* instruction, |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 68 | HInstruction* chase_hint, |
Aart Bik | 1fc3afb | 2016-02-02 13:26:16 -0800 | [diff] [blame] | 69 | /*out*/ Value* min_val, |
| 70 | /*out*/ Value* max_val, |
| 71 | /*out*/ bool* needs_finite_test); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 72 | |
| 73 | /** |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 74 | * 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 Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 78 | */ |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 79 | bool CanGenerateCode(HInstruction* context, |
| 80 | HInstruction* instruction, |
Aart Bik | 1fc3afb | 2016-02-02 13:26:16 -0800 | [diff] [blame] | 81 | /*out*/ bool* needs_finite_test, |
| 82 | /*out*/ bool* needs_taken_test); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 83 | |
| 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 Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 87 | * 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 Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 89 | * |
| 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 Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 96 | * |
| 97 | * Precondition: CanGenerateCode() returns true. |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 98 | */ |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 99 | void GenerateRangeCode(HInstruction* context, |
| 100 | HInstruction* instruction, |
| 101 | HGraph* graph, |
| 102 | HBasicBlock* block, |
Aart Bik | 1fc3afb | 2016-02-02 13:26:16 -0800 | [diff] [blame] | 103 | /*out*/ HInstruction** lower, |
| 104 | /*out*/ HInstruction** upper); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 105 | |
| 106 | /** |
| 107 | * Generates explicit taken-test for the loop in the given context. Code is generated in |
| 108 | * given block and graph. The taken-test is returned in parameter test. |
| 109 | * |
| 110 | * Precondition: CanGenerateCode() returns true and needs_taken_test is set. |
| 111 | */ |
| 112 | void GenerateTakenTest(HInstruction* context, |
| 113 | HGraph* graph, |
| 114 | HBasicBlock* block, |
Aart Bik | 1fc3afb | 2016-02-02 13:26:16 -0800 | [diff] [blame] | 115 | /*out*/ HInstruction** taken_test); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 116 | |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 117 | private: |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 118 | /* |
| 119 | * Enum used in IsConstant() request. |
| 120 | */ |
| 121 | enum ConstantRequest { |
| 122 | kExact, |
| 123 | kAtMost, |
| 124 | kAtLeast |
| 125 | }; |
| 126 | |
| 127 | /** |
| 128 | * Returns true if exact or upper/lower bound on the given induction |
| 129 | * information is known as a 64-bit constant, which is returned in value. |
| 130 | */ |
| 131 | bool IsConstant(HInductionVarAnalysis::InductionInfo* info, |
| 132 | ConstantRequest request, |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 133 | /*out*/ int64_t* value) const; |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 134 | |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 135 | /** Returns whether induction information can be obtained. */ |
| 136 | bool HasInductionInfo(HInstruction* context, |
| 137 | HInstruction* instruction, |
| 138 | /*out*/ HLoopInformation** loop, |
| 139 | /*out*/ HInductionVarAnalysis::InductionInfo** info, |
| 140 | /*out*/ HInductionVarAnalysis::InductionInfo** trip) const; |
| 141 | |
| 142 | bool HasFetchInLoop(HInductionVarAnalysis::InductionInfo* info) const; |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 143 | bool NeedsTripCount(HInductionVarAnalysis::InductionInfo* info) const; |
| 144 | bool IsBodyTripCount(HInductionVarAnalysis::InductionInfo* trip) const; |
| 145 | bool IsUnsafeTripCount(HInductionVarAnalysis::InductionInfo* trip) const; |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 146 | bool IsWellBehavedTripCount(HInductionVarAnalysis::InductionInfo* trip) const; |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 147 | |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 148 | Value GetLinear(HInductionVarAnalysis::InductionInfo* info, |
| 149 | HInductionVarAnalysis::InductionInfo* trip, |
| 150 | bool in_body, |
| 151 | bool is_min) const; |
| 152 | Value GetFetch(HInstruction* instruction, |
| 153 | HInductionVarAnalysis::InductionInfo* trip, |
| 154 | bool in_body, |
| 155 | bool is_min) const; |
| 156 | Value GetVal(HInductionVarAnalysis::InductionInfo* info, |
| 157 | HInductionVarAnalysis::InductionInfo* trip, |
| 158 | bool in_body, |
| 159 | bool is_min) const; |
| 160 | Value GetMul(HInductionVarAnalysis::InductionInfo* info1, |
| 161 | HInductionVarAnalysis::InductionInfo* info2, |
| 162 | HInductionVarAnalysis::InductionInfo* trip, |
| 163 | bool in_body, |
| 164 | bool is_min) const; |
| 165 | Value GetDiv(HInductionVarAnalysis::InductionInfo* info1, |
| 166 | HInductionVarAnalysis::InductionInfo* info2, |
| 167 | HInductionVarAnalysis::InductionInfo* trip, |
| 168 | bool in_body, |
| 169 | bool is_min) const; |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 170 | |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 171 | Value MulRangeAndConstant(int64_t value, |
| 172 | HInductionVarAnalysis::InductionInfo* info, |
| 173 | HInductionVarAnalysis::InductionInfo* trip, |
| 174 | bool in_body, |
| 175 | bool is_min) const; |
| 176 | Value DivRangeAndConstant(int64_t value, |
| 177 | HInductionVarAnalysis::InductionInfo* info, |
| 178 | HInductionVarAnalysis::InductionInfo* trip, |
| 179 | bool in_body, |
| 180 | bool is_min) const; |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 181 | |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 182 | Value AddValue(Value v1, Value v2) const; |
| 183 | Value SubValue(Value v1, Value v2) const; |
| 184 | Value MulValue(Value v1, Value v2) const; |
| 185 | Value DivValue(Value v1, Value v2) const; |
| 186 | Value MergeVal(Value v1, Value v2, bool is_min) const; |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 187 | |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 188 | /** |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 189 | * Generates code for lower/upper/taken-test in the HIR. Returns true on success. |
| 190 | * With values nullptr, the method can be used to determine if code generation |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 191 | * would be successful without generating actual code yet. |
| 192 | */ |
| 193 | bool GenerateCode(HInstruction* context, |
| 194 | HInstruction* instruction, |
| 195 | HGraph* graph, |
| 196 | HBasicBlock* block, |
Aart Bik | 1fc3afb | 2016-02-02 13:26:16 -0800 | [diff] [blame] | 197 | /*out*/ HInstruction** lower, |
| 198 | /*out*/ HInstruction** upper, |
| 199 | /*out*/ HInstruction** taken_test, |
| 200 | /*out*/ bool* needs_finite_test, |
| 201 | /*out*/ bool* needs_taken_test) const; |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 202 | |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 203 | bool GenerateCode(HInductionVarAnalysis::InductionInfo* info, |
| 204 | HInductionVarAnalysis::InductionInfo* trip, |
| 205 | HGraph* graph, |
| 206 | HBasicBlock* block, |
Aart Bik | 1fc3afb | 2016-02-02 13:26:16 -0800 | [diff] [blame] | 207 | /*out*/ HInstruction** result, |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 208 | bool in_body, |
| 209 | bool is_min) const; |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 210 | |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 211 | /** Results of prior induction variable analysis. */ |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 212 | HInductionVarAnalysis* induction_analysis_; |
| 213 | |
| 214 | /** Instruction at which chasing may stop. */ |
| 215 | HInstruction* chase_hint_; |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 216 | |
Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 217 | friend class HInductionVarAnalysis; |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 218 | friend class InductionVarRangeTest; |
| 219 | |
| 220 | DISALLOW_COPY_AND_ASSIGN(InductionVarRange); |
| 221 | }; |
| 222 | |
| 223 | } // namespace art |
| 224 | |
| 225 | #endif // ART_COMPILER_OPTIMIZING_INDUCTION_VAR_RANGE_H_ |