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 | /** |
| 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 | |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame^] | 71 | /** |
| 72 | * Returns true if range analysis is able to generate code for the lower and upper bound |
| 73 | * expressions on the instruction in the given context. Output parameter top_test denotes |
| 74 | * whether a top test is needed to protect the trip-count expression evaluation. |
| 75 | */ |
| 76 | bool CanGenerateCode(HInstruction* context, HInstruction* instruction, /*out*/bool* top_test); |
| 77 | |
| 78 | /** |
| 79 | * Generates the actual code in the HIR for the lower and upper bound expressions on the |
| 80 | * instruction in the given context. Code for the lower and upper bound expression are |
| 81 | * generated in given block and graph and are returned in lower and upper, respectively. |
| 82 | * For a loop invariant, lower is not set. |
| 83 | * |
| 84 | * For example, given expression x+i with range [0, 5] for i, calling this method |
| 85 | * will generate the following sequence: |
| 86 | * |
| 87 | * block: |
| 88 | * lower: add x, 0 |
| 89 | * upper: add x, 5 |
| 90 | */ |
| 91 | bool GenerateCode(HInstruction* context, |
| 92 | HInstruction* instruction, |
| 93 | HGraph* graph, |
| 94 | HBasicBlock* block, |
| 95 | /*out*/HInstruction** lower, |
| 96 | /*out*/HInstruction** upper); |
| 97 | |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 98 | private: |
| 99 | // |
| 100 | // Private helper methods. |
| 101 | // |
| 102 | |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 103 | Value GetInduction(HInstruction* context, HInstruction* instruction, bool is_min); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 104 | |
Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 105 | static Value GetFetch(HInstruction* instruction, |
| 106 | HInductionVarAnalysis::InductionInfo* trip, |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 107 | bool in_body, |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 108 | bool is_min); |
Aart Bik | cd26feb | 2015-09-23 17:50:50 -0700 | [diff] [blame] | 109 | static Value GetVal(HInductionVarAnalysis::InductionInfo* info, |
| 110 | HInductionVarAnalysis::InductionInfo* trip, |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 111 | bool in_body, |
Aart Bik | cd26feb | 2015-09-23 17:50:50 -0700 | [diff] [blame] | 112 | bool is_min); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 113 | static Value GetMul(HInductionVarAnalysis::InductionInfo* info1, |
| 114 | HInductionVarAnalysis::InductionInfo* info2, |
Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 115 | HInductionVarAnalysis::InductionInfo* trip, |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 116 | bool in_body, |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 117 | bool is_min); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 118 | static Value GetDiv(HInductionVarAnalysis::InductionInfo* info1, |
| 119 | HInductionVarAnalysis::InductionInfo* info2, |
Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 120 | HInductionVarAnalysis::InductionInfo* trip, |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 121 | bool in_body, |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 122 | bool is_min); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 123 | |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 124 | static bool GetConstant(HInductionVarAnalysis::InductionInfo* info, int32_t *value); |
| 125 | |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 126 | static Value AddValue(Value v1, Value v2); |
| 127 | static Value SubValue(Value v1, Value v2); |
| 128 | static Value MulValue(Value v1, Value v2); |
| 129 | static Value DivValue(Value v1, Value v2); |
Aart Bik | cd26feb | 2015-09-23 17:50:50 -0700 | [diff] [blame] | 130 | static Value MergeVal(Value v1, Value v2, bool is_min); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 131 | |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame^] | 132 | /** |
| 133 | * Generates code for lower/upper expression in the HIR. Returns true on success. |
| 134 | * With graph == nullptr, the method can be used to determine if code generation |
| 135 | * would be successful without generating actual code yet. |
| 136 | */ |
| 137 | bool GenerateCode(HInstruction* context, |
| 138 | HInstruction* instruction, |
| 139 | HGraph* graph, |
| 140 | HBasicBlock* block, |
| 141 | /*out*/HInstruction** lower, |
| 142 | /*out*/HInstruction** upper, |
| 143 | bool* top_test); |
| 144 | |
| 145 | static bool GenerateCode(HInductionVarAnalysis::InductionInfo* info, |
| 146 | HInductionVarAnalysis::InductionInfo* trip, |
| 147 | HGraph* graph, |
| 148 | HBasicBlock* block, |
| 149 | /*out*/HInstruction** result, |
| 150 | bool in_body, |
| 151 | bool is_min); |
| 152 | |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 153 | /** Results of prior induction variable analysis. */ |
| 154 | HInductionVarAnalysis *induction_analysis_; |
| 155 | |
Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 156 | friend class HInductionVarAnalysis; |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 157 | friend class InductionVarRangeTest; |
| 158 | |
| 159 | DISALLOW_COPY_AND_ASSIGN(InductionVarRange); |
| 160 | }; |
| 161 | |
| 162 | } // namespace art |
| 163 | |
| 164 | #endif // ART_COMPILER_OPTIMIZING_INDUCTION_VAR_RANGE_H_ |