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 | /** |
| 25 | * This class implements induction variable based range analysis on expressions within loops. |
| 26 | * It takes the results of induction variable analysis in the constructor and provides a public |
| 27 | * API to obtain a conservative lower and upper bound value on each instruction in the HIR. |
| 28 | * |
| 29 | * For example, given a linear induction 2 * i + x where 0 <= i <= 10, range analysis yields lower |
Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 30 | * bound value x and upper bound value x + 20 for the expression, thus, the range [x, x + 20]. |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 31 | */ |
| 32 | class InductionVarRange { |
| 33 | public: |
| 34 | /* |
| 35 | * A value that can be represented as "a * instruction + b" for 32-bit constants, where |
| 36 | * Value(INT_MIN) and Value(INT_MAX) denote an unknown lower and upper bound, respectively. |
| 37 | * Although range analysis could yield more complex values, the format is sufficiently powerful |
| 38 | * to represent useful cases and feeds directly into optimizations like bounds check elimination. |
| 39 | */ |
| 40 | struct Value { |
| 41 | Value(HInstruction* i, int32_t a, int32_t b) |
Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 42 | : instruction(a != 0 ? i : nullptr), |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 43 | a_constant(a), |
| 44 | b_constant(b) {} |
| 45 | explicit Value(int32_t b) : Value(nullptr, 0, b) {} |
| 46 | HInstruction* instruction; |
| 47 | int32_t a_constant; |
| 48 | int32_t b_constant; |
| 49 | }; |
| 50 | |
| 51 | explicit InductionVarRange(HInductionVarAnalysis* induction); |
| 52 | |
| 53 | /** |
| 54 | * Given a context denoted by the first instruction, returns a, |
| 55 | * possibly conservative, lower bound on the instruction's value. |
| 56 | */ |
| 57 | Value GetMinInduction(HInstruction* context, HInstruction* instruction); |
| 58 | |
| 59 | /** |
| 60 | * Given a context denoted by the first instruction, returns a, |
| 61 | * possibly conservative, upper bound on the instruction's value. |
| 62 | */ |
| 63 | Value GetMaxInduction(HInstruction* context, HInstruction* instruction); |
| 64 | |
| 65 | private: |
| 66 | // |
| 67 | // Private helper methods. |
| 68 | // |
| 69 | |
| 70 | HInductionVarAnalysis::InductionInfo* GetTripCount(HLoopInformation* loop, |
| 71 | HInstruction* context); |
| 72 | |
Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 73 | static Value GetFetch(HInstruction* instruction, |
| 74 | HInductionVarAnalysis::InductionInfo* trip, |
| 75 | int32_t fail_value); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 76 | |
| 77 | static Value GetMin(HInductionVarAnalysis::InductionInfo* info, |
| 78 | HInductionVarAnalysis::InductionInfo* trip); |
| 79 | static Value GetMax(HInductionVarAnalysis::InductionInfo* info, |
| 80 | HInductionVarAnalysis::InductionInfo* trip); |
| 81 | static Value GetMul(HInductionVarAnalysis::InductionInfo* info1, |
| 82 | HInductionVarAnalysis::InductionInfo* info2, |
Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 83 | HInductionVarAnalysis::InductionInfo* trip, |
| 84 | int32_t fail_value); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 85 | static Value GetDiv(HInductionVarAnalysis::InductionInfo* info1, |
| 86 | HInductionVarAnalysis::InductionInfo* info2, |
Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 87 | HInductionVarAnalysis::InductionInfo* trip, |
| 88 | int32_t fail_value); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 89 | |
| 90 | static Value AddValue(Value v1, Value v2, int32_t fail_value); |
| 91 | static Value SubValue(Value v1, Value v2, int32_t fail_value); |
| 92 | static Value MulValue(Value v1, Value v2, int32_t fail_value); |
| 93 | static Value DivValue(Value v1, Value v2, int32_t fail_value); |
| 94 | static Value MinValue(Value v1, Value v2); |
| 95 | static Value MaxValue(Value v1, Value v2); |
| 96 | |
| 97 | /** Results of prior induction variable analysis. */ |
| 98 | HInductionVarAnalysis *induction_analysis_; |
| 99 | |
Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 100 | friend class HInductionVarAnalysis; |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 101 | friend class InductionVarRangeTest; |
| 102 | |
| 103 | DISALLOW_COPY_AND_ASSIGN(InductionVarRange); |
| 104 | }; |
| 105 | |
| 106 | } // namespace art |
| 107 | |
| 108 | #endif // ART_COMPILER_OPTIMIZING_INDUCTION_VAR_RANGE_H_ |