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 | |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 17 | #include "induction_var_range.h" |
| 18 | |
Aart Bik | cd26feb | 2015-09-23 17:50:50 -0700 | [diff] [blame] | 19 | #include <limits> |
Santiago Aboy Solanes | 4f18b94 | 2022-07-21 10:31:21 +0100 | [diff] [blame] | 20 | #include "optimizing/nodes.h" |
Aart Bik | cd26feb | 2015-09-23 17:50:50 -0700 | [diff] [blame] | 21 | |
VladimĂr Marko | 434d968 | 2022-11-04 14:04:17 +0000 | [diff] [blame] | 22 | namespace art HIDDEN { |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 23 | |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 24 | /** Returns true if 64-bit constant fits in 32-bit constant. */ |
| 25 | static bool CanLongValueFitIntoInt(int64_t c) { |
Aart Bik | cd26feb | 2015-09-23 17:50:50 -0700 | [diff] [blame] | 26 | return std::numeric_limits<int32_t>::min() <= c && c <= std::numeric_limits<int32_t>::max(); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 27 | } |
| 28 | |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 29 | /** Returns true if 32-bit addition can be done safely. */ |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 30 | static bool IsSafeAdd(int32_t c1, int32_t c2) { |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 31 | return CanLongValueFitIntoInt(static_cast<int64_t>(c1) + static_cast<int64_t>(c2)); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 32 | } |
| 33 | |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 34 | /** Returns true if 32-bit subtraction can be done safely. */ |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 35 | static bool IsSafeSub(int32_t c1, int32_t c2) { |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 36 | return CanLongValueFitIntoInt(static_cast<int64_t>(c1) - static_cast<int64_t>(c2)); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 37 | } |
| 38 | |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 39 | /** Returns true if 32-bit multiplication can be done safely. */ |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 40 | static bool IsSafeMul(int32_t c1, int32_t c2) { |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 41 | return CanLongValueFitIntoInt(static_cast<int64_t>(c1) * static_cast<int64_t>(c2)); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 42 | } |
| 43 | |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 44 | /** Returns true if 32-bit division can be done safely. */ |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 45 | static bool IsSafeDiv(int32_t c1, int32_t c2) { |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 46 | return c2 != 0 && CanLongValueFitIntoInt(static_cast<int64_t>(c1) / static_cast<int64_t>(c2)); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Aart Bik | b603a5c | 2017-03-06 18:29:39 -0800 | [diff] [blame] | 49 | /** Computes a * b for a,b > 0 (at least until first overflow happens). */ |
| 50 | static int64_t SafeMul(int64_t a, int64_t b, /*out*/ bool* overflow) { |
| 51 | if (a > 0 && b > 0 && a > (std::numeric_limits<int64_t>::max() / b)) { |
| 52 | *overflow = true; |
| 53 | } |
| 54 | return a * b; |
| 55 | } |
| 56 | |
| 57 | /** Returns b^e for b,e > 0. Sets overflow if arithmetic wrap-around occurred. */ |
Aart Bik | d3ba626 | 2017-01-30 14:37:12 -0800 | [diff] [blame] | 58 | static int64_t IntPow(int64_t b, int64_t e, /*out*/ bool* overflow) { |
Aart Bik | b603a5c | 2017-03-06 18:29:39 -0800 | [diff] [blame] | 59 | DCHECK_LT(0, b); |
| 60 | DCHECK_LT(0, e); |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 61 | int64_t pow = 1; |
| 62 | while (e) { |
| 63 | if (e & 1) { |
Aart Bik | b603a5c | 2017-03-06 18:29:39 -0800 | [diff] [blame] | 64 | pow = SafeMul(pow, b, overflow); |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 65 | } |
| 66 | e >>= 1; |
Aart Bik | b603a5c | 2017-03-06 18:29:39 -0800 | [diff] [blame] | 67 | if (e) { |
| 68 | b = SafeMul(b, b, overflow); |
| 69 | } |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 70 | } |
| 71 | return pow; |
| 72 | } |
| 73 | |
Aart Bik | 40fbf74 | 2016-10-31 11:02:50 -0700 | [diff] [blame] | 74 | /** Hunts "under the hood" for a suitable instruction at the hint. */ |
| 75 | static bool IsMaxAtHint( |
| 76 | HInstruction* instruction, HInstruction* hint, /*out*/HInstruction** suitable) { |
Aart Bik | 1f8d51b | 2018-02-15 10:42:37 -0800 | [diff] [blame] | 77 | if (instruction->IsMin()) { |
| 78 | // For MIN(x, y), return most suitable x or y as maximum. |
| 79 | return IsMaxAtHint(instruction->InputAt(0), hint, suitable) || |
| 80 | IsMaxAtHint(instruction->InputAt(1), hint, suitable); |
Aart Bik | 40fbf74 | 2016-10-31 11:02:50 -0700 | [diff] [blame] | 81 | } else { |
| 82 | *suitable = instruction; |
Nicolas Geoffray | e761bcc | 2017-01-19 08:59:37 +0000 | [diff] [blame] | 83 | return HuntForDeclaration(instruction) == hint; |
Aart Bik | 40fbf74 | 2016-10-31 11:02:50 -0700 | [diff] [blame] | 84 | } |
Aart Bik | 40fbf74 | 2016-10-31 11:02:50 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | /** Post-analysis simplification of a minimum value that makes the bound more useful to clients. */ |
| 88 | static InductionVarRange::Value SimplifyMin(InductionVarRange::Value v) { |
| 89 | if (v.is_known && v.a_constant == 1 && v.b_constant <= 0) { |
| 90 | // If a == 1, instruction >= 0 and b <= 0, just return the constant b. |
| 91 | // No arithmetic wrap-around can occur. |
| 92 | if (IsGEZero(v.instruction)) { |
| 93 | return InductionVarRange::Value(v.b_constant); |
| 94 | } |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 95 | } |
| 96 | return v; |
| 97 | } |
| 98 | |
Aart Bik | 40fbf74 | 2016-10-31 11:02:50 -0700 | [diff] [blame] | 99 | /** Post-analysis simplification of a maximum value that makes the bound more useful to clients. */ |
| 100 | static InductionVarRange::Value SimplifyMax(InductionVarRange::Value v, HInstruction* hint) { |
| 101 | if (v.is_known && v.a_constant >= 1) { |
| 102 | // An upper bound a * (length / a) + b, where a >= 1, can be conservatively rewritten as |
| 103 | // length + b because length >= 0 is true. |
| 104 | int64_t value; |
| 105 | if (v.instruction->IsDiv() && |
| 106 | v.instruction->InputAt(0)->IsArrayLength() && |
Aart Bik | f3e61ee | 2017-04-12 17:09:20 -0700 | [diff] [blame] | 107 | IsInt64AndGet(v.instruction->InputAt(1), &value) && v.a_constant == value) { |
Aart Bik | 40fbf74 | 2016-10-31 11:02:50 -0700 | [diff] [blame] | 108 | return InductionVarRange::Value(v.instruction->InputAt(0), 1, v.b_constant); |
| 109 | } |
| 110 | // If a == 1, the most suitable one suffices as maximum value. |
| 111 | HInstruction* suitable = nullptr; |
| 112 | if (v.a_constant == 1 && IsMaxAtHint(v.instruction, hint, &suitable)) { |
| 113 | return InductionVarRange::Value(suitable, 1, v.b_constant); |
| 114 | } |
| 115 | } |
| 116 | return v; |
| 117 | } |
| 118 | |
| 119 | /** Tests for a constant value. */ |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 120 | static bool IsConstantValue(InductionVarRange::Value v) { |
| 121 | return v.is_known && v.a_constant == 0; |
| 122 | } |
| 123 | |
| 124 | /** Corrects a value for type to account for arithmetic wrap-around in lower precision. */ |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 125 | static InductionVarRange::Value CorrectForType(InductionVarRange::Value v, DataType::Type type) { |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 126 | switch (type) { |
Vladimir Marko | d5d2f2c | 2017-09-26 12:37:26 +0100 | [diff] [blame] | 127 | case DataType::Type::kUint8: |
| 128 | case DataType::Type::kInt8: |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 129 | case DataType::Type::kUint16: |
Vladimir Marko | d5d2f2c | 2017-09-26 12:37:26 +0100 | [diff] [blame] | 130 | case DataType::Type::kInt16: { |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 131 | // Constants within range only. |
| 132 | // TODO: maybe some room for improvement, like allowing widening conversions |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 133 | int32_t min = DataType::MinValueOfIntegralType(type); |
| 134 | int32_t max = DataType::MaxValueOfIntegralType(type); |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 135 | return (IsConstantValue(v) && min <= v.b_constant && v.b_constant <= max) |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 136 | ? v |
| 137 | : InductionVarRange::Value(); |
| 138 | } |
| 139 | default: |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 140 | return v; |
| 141 | } |
| 142 | } |
| 143 | |
Aart Bik | 40fbf74 | 2016-10-31 11:02:50 -0700 | [diff] [blame] | 144 | /** Inserts an instruction. */ |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 145 | static HInstruction* Insert(HBasicBlock* block, HInstruction* instruction) { |
| 146 | DCHECK(block != nullptr); |
| 147 | DCHECK(block->GetLastInstruction() != nullptr) << block->GetBlockId(); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 148 | DCHECK(instruction != nullptr); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 149 | block->InsertInstructionBefore(instruction, block->GetLastInstruction()); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 150 | return instruction; |
| 151 | } |
| 152 | |
Aart Bik | 40fbf74 | 2016-10-31 11:02:50 -0700 | [diff] [blame] | 153 | /** Obtains loop's control instruction. */ |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 154 | static HInstruction* GetLoopControl(const HLoopInformation* loop) { |
Aart Bik | 009cace | 2016-09-16 10:15:19 -0700 | [diff] [blame] | 155 | DCHECK(loop != nullptr); |
| 156 | return loop->GetHeader()->GetLastInstruction(); |
| 157 | } |
| 158 | |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 159 | /** Determines whether the `context` is in the body of the `loop`. */ |
| 160 | static bool IsContextInBody(const HBasicBlock* context, const HLoopInformation* loop) { |
| 161 | DCHECK(loop != nullptr); |
| 162 | // We're currently classifying trip count only for the exit condition from loop header. |
| 163 | // All other blocks in the loop are considered loop body. |
| 164 | return context != loop->GetHeader() && loop->Contains(*context); |
| 165 | } |
| 166 | |
| 167 | /** Determines whether to use the full trip count for given `context`, `loop` and `is_min`. */ |
| 168 | bool UseFullTripCount(const HBasicBlock* context, const HLoopInformation* loop, bool is_min) { |
| 169 | // We're currently classifying trip count only for the exit condition from loop header. |
| 170 | // So, we should call this helper function only if the loop control is an `HIf` with |
| 171 | // one edge leaving the loop. The loop header is the only block that's both inside |
| 172 | // the loop and not in the loop body. |
| 173 | DCHECK(GetLoopControl(loop)->IsIf()); |
| 174 | DCHECK_NE(loop->Contains(*GetLoopControl(loop)->AsIf()->IfTrueSuccessor()), |
| 175 | loop->Contains(*GetLoopControl(loop)->AsIf()->IfFalseSuccessor())); |
| 176 | if (loop->Contains(*context)) { |
| 177 | // Use the full trip count if determining the maximum and context is not in the loop body. |
| 178 | DCHECK_NE(context == loop->GetHeader(), IsContextInBody(context, loop)); |
| 179 | return !is_min && context == loop->GetHeader(); |
| 180 | } else { |
| 181 | // Trip count after the loop is always the maximum (ignoring `is_min`), |
| 182 | // as long as the `context` is dominated by the loop control exit block. |
| 183 | // If there are additional exit edges, the value is unknown on those paths. |
| 184 | HInstruction* loop_control = GetLoopControl(loop); |
| 185 | HBasicBlock* then_block = loop_control->AsIf()->IfTrueSuccessor(); |
| 186 | HBasicBlock* else_block = loop_control->AsIf()->IfFalseSuccessor(); |
| 187 | HBasicBlock* loop_exit_block = loop->Contains(*then_block) ? else_block : then_block; |
| 188 | return loop_exit_block->Dominates(context); |
| 189 | } |
| 190 | } |
| 191 | |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 192 | // |
| 193 | // Public class methods. |
| 194 | // |
| 195 | |
| 196 | InductionVarRange::InductionVarRange(HInductionVarAnalysis* induction_analysis) |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 197 | : induction_analysis_(induction_analysis), |
| 198 | chase_hint_(nullptr) { |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 199 | DCHECK(induction_analysis != nullptr); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 202 | bool InductionVarRange::GetInductionRange(const HBasicBlock* context, |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 203 | HInstruction* instruction, |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 204 | HInstruction* chase_hint, |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 205 | /*out*/Value* min_val, |
| 206 | /*out*/Value* max_val, |
| 207 | /*out*/bool* needs_finite_test) { |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 208 | const HLoopInformation* loop = nullptr; |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 209 | HInductionVarAnalysis::InductionInfo* info = nullptr; |
| 210 | HInductionVarAnalysis::InductionInfo* trip = nullptr; |
| 211 | if (!HasInductionInfo(context, instruction, &loop, &info, &trip)) { |
| 212 | return false; |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 213 | } |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 214 | // Type int or lower (this is not too restrictive since intended clients, like |
| 215 | // bounds check elimination, will have truncated higher precision induction |
| 216 | // at their use point already). |
| 217 | switch (info->type) { |
Vladimir Marko | d5d2f2c | 2017-09-26 12:37:26 +0100 | [diff] [blame] | 218 | case DataType::Type::kUint8: |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 219 | case DataType::Type::kInt8: |
Vladimir Marko | d5d2f2c | 2017-09-26 12:37:26 +0100 | [diff] [blame] | 220 | case DataType::Type::kUint16: |
| 221 | case DataType::Type::kInt16: |
| 222 | case DataType::Type::kInt32: |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 223 | break; |
| 224 | default: |
| 225 | return false; |
| 226 | } |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 227 | // Find range. |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 228 | chase_hint_ = chase_hint; |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 229 | int64_t stride_value = 0; |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 230 | *min_val = SimplifyMin(GetVal(context, loop, info, trip, /*is_min=*/ true)); |
| 231 | *max_val = SimplifyMax(GetVal(context, loop, info, trip, /*is_min=*/ false), chase_hint); |
| 232 | *needs_finite_test = |
| 233 | NeedsTripCount(context, loop, info, &stride_value) && IsUnsafeTripCount(trip); |
Aart Bik | 40fbf74 | 2016-10-31 11:02:50 -0700 | [diff] [blame] | 234 | chase_hint_ = nullptr; |
| 235 | // Retry chasing constants for wrap-around (merge sensitive). |
| 236 | if (!min_val->is_known && info->induction_class == HInductionVarAnalysis::kWrapAround) { |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 237 | *min_val = SimplifyMin(GetVal(context, loop, info, trip, /*is_min=*/ true)); |
Aart Bik | 40fbf74 | 2016-10-31 11:02:50 -0700 | [diff] [blame] | 238 | } |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 239 | return true; |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 240 | } |
| 241 | |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 242 | bool InductionVarRange::CanGenerateRange(const HBasicBlock* context, |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 243 | HInstruction* instruction, |
| 244 | /*out*/bool* needs_finite_test, |
| 245 | /*out*/bool* needs_taken_test) { |
| 246 | bool is_last_value = false; |
| 247 | int64_t stride_value = 0; |
Aart Bik | 9abf894 | 2016-10-14 09:49:42 -0700 | [diff] [blame] | 248 | return GenerateRangeOrLastValue(context, |
| 249 | instruction, |
| 250 | is_last_value, |
| 251 | nullptr, |
| 252 | nullptr, |
| 253 | nullptr, |
| 254 | nullptr, |
| 255 | nullptr, // nothing generated yet |
| 256 | &stride_value, |
| 257 | needs_finite_test, |
| 258 | needs_taken_test) |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 259 | && (stride_value == -1 || |
| 260 | stride_value == 0 || |
Aart Bik | 40fbf74 | 2016-10-31 11:02:50 -0700 | [diff] [blame] | 261 | stride_value == 1); // avoid arithmetic wrap-around anomalies. |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 264 | void InductionVarRange::GenerateRange(const HBasicBlock* context, |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 265 | HInstruction* instruction, |
| 266 | HGraph* graph, |
| 267 | HBasicBlock* block, |
| 268 | /*out*/HInstruction** lower, |
| 269 | /*out*/HInstruction** upper) { |
| 270 | bool is_last_value = false; |
Aart Bik | 009cace | 2016-09-16 10:15:19 -0700 | [diff] [blame] | 271 | int64_t stride_value = 0; |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 272 | bool b1, b2; // unused |
Aart Bik | 9abf894 | 2016-10-14 09:49:42 -0700 | [diff] [blame] | 273 | if (!GenerateRangeOrLastValue(context, |
| 274 | instruction, |
| 275 | is_last_value, |
| 276 | graph, |
| 277 | block, |
| 278 | lower, |
| 279 | upper, |
| 280 | nullptr, |
| 281 | &stride_value, |
| 282 | &b1, |
| 283 | &b2)) { |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 284 | LOG(FATAL) << "Failed precondition: CanGenerateRange()"; |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 285 | } |
| 286 | } |
| 287 | |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 288 | HInstruction* InductionVarRange::GenerateTakenTest(HInstruction* loop_control, |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 289 | HGraph* graph, |
| 290 | HBasicBlock* block) { |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 291 | const HBasicBlock* context = loop_control->GetBlock(); |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 292 | HInstruction* taken_test = nullptr; |
| 293 | bool is_last_value = false; |
| 294 | int64_t stride_value = 0; |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 295 | bool b1, b2; // unused |
Aart Bik | 9abf894 | 2016-10-14 09:49:42 -0700 | [diff] [blame] | 296 | if (!GenerateRangeOrLastValue(context, |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 297 | loop_control, |
Aart Bik | 9abf894 | 2016-10-14 09:49:42 -0700 | [diff] [blame] | 298 | is_last_value, |
| 299 | graph, |
| 300 | block, |
| 301 | nullptr, |
| 302 | nullptr, |
| 303 | &taken_test, |
| 304 | &stride_value, |
| 305 | &b1, |
| 306 | &b2)) { |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 307 | LOG(FATAL) << "Failed precondition: CanGenerateRange()"; |
| 308 | } |
| 309 | return taken_test; |
| 310 | } |
| 311 | |
| 312 | bool InductionVarRange::CanGenerateLastValue(HInstruction* instruction) { |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 313 | const HBasicBlock* context = instruction->GetBlock(); |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 314 | bool is_last_value = true; |
| 315 | int64_t stride_value = 0; |
| 316 | bool needs_finite_test = false; |
| 317 | bool needs_taken_test = false; |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 318 | return GenerateRangeOrLastValue(context, |
Aart Bik | 9abf894 | 2016-10-14 09:49:42 -0700 | [diff] [blame] | 319 | instruction, |
| 320 | is_last_value, |
| 321 | nullptr, |
| 322 | nullptr, |
| 323 | nullptr, |
| 324 | nullptr, |
| 325 | nullptr, // nothing generated yet |
| 326 | &stride_value, |
| 327 | &needs_finite_test, |
| 328 | &needs_taken_test) |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 329 | && !needs_finite_test && !needs_taken_test; |
| 330 | } |
| 331 | |
| 332 | HInstruction* InductionVarRange::GenerateLastValue(HInstruction* instruction, |
| 333 | HGraph* graph, |
| 334 | HBasicBlock* block) { |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 335 | const HBasicBlock* context = instruction->GetBlock(); |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 336 | HInstruction* last_value = nullptr; |
| 337 | bool is_last_value = true; |
| 338 | int64_t stride_value = 0; |
| 339 | bool b1, b2; // unused |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 340 | if (!GenerateRangeOrLastValue(context, |
Aart Bik | 9abf894 | 2016-10-14 09:49:42 -0700 | [diff] [blame] | 341 | instruction, |
| 342 | is_last_value, |
| 343 | graph, |
| 344 | block, |
| 345 | &last_value, |
| 346 | &last_value, |
| 347 | nullptr, |
| 348 | &stride_value, |
| 349 | &b1, |
| 350 | &b2)) { |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 351 | LOG(FATAL) << "Failed precondition: CanGenerateLastValue()"; |
| 352 | } |
| 353 | return last_value; |
| 354 | } |
| 355 | |
| 356 | void InductionVarRange::Replace(HInstruction* instruction, |
| 357 | HInstruction* fetch, |
| 358 | HInstruction* replacement) { |
| 359 | for (HLoopInformation* lp = instruction->GetBlock()->GetLoopInformation(); // closest enveloping loop |
| 360 | lp != nullptr; |
| 361 | lp = lp->GetPreHeader()->GetLoopInformation()) { |
Aart Bik | 009cace | 2016-09-16 10:15:19 -0700 | [diff] [blame] | 362 | // Update instruction's information. |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 363 | ReplaceInduction(induction_analysis_->LookupInfo(lp, instruction), fetch, replacement); |
Aart Bik | 009cace | 2016-09-16 10:15:19 -0700 | [diff] [blame] | 364 | // Update loop's trip-count information. |
| 365 | ReplaceInduction(induction_analysis_->LookupInfo(lp, GetLoopControl(lp)), fetch, replacement); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 366 | } |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 367 | } |
| 368 | |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 369 | bool InductionVarRange::IsFinite(const HLoopInformation* loop, /*out*/ int64_t* trip_count) const { |
Artem Serov | 121f203 | 2017-10-23 19:19:06 +0100 | [diff] [blame] | 370 | bool is_constant_unused = false; |
| 371 | return CheckForFiniteAndConstantProps(loop, &is_constant_unused, trip_count); |
| 372 | } |
| 373 | |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 374 | bool InductionVarRange::HasKnownTripCount(const HLoopInformation* loop, |
Artem Serov | 121f203 | 2017-10-23 19:19:06 +0100 | [diff] [blame] | 375 | /*out*/ int64_t* trip_count) const { |
| 376 | bool is_constant = false; |
| 377 | CheckForFiniteAndConstantProps(loop, &is_constant, trip_count); |
| 378 | return is_constant; |
Aart Bik | 9abf894 | 2016-10-14 09:49:42 -0700 | [diff] [blame] | 379 | } |
| 380 | |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 381 | bool InductionVarRange::IsUnitStride(const HBasicBlock* context, |
Aart Bik | fa76296 | 2017-04-07 11:33:37 -0700 | [diff] [blame] | 382 | HInstruction* instruction, |
Aart Bik | 37dc4df | 2017-06-28 14:08:00 -0700 | [diff] [blame] | 383 | HGraph* graph, |
Aart Bik | 8e02e3e | 2017-02-28 14:41:55 -0800 | [diff] [blame] | 384 | /*out*/ HInstruction** offset) const { |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 385 | const HLoopInformation* loop = nullptr; |
Aart Bik | 8e02e3e | 2017-02-28 14:41:55 -0800 | [diff] [blame] | 386 | HInductionVarAnalysis::InductionInfo* info = nullptr; |
| 387 | HInductionVarAnalysis::InductionInfo* trip = nullptr; |
Aart Bik | fa76296 | 2017-04-07 11:33:37 -0700 | [diff] [blame] | 388 | if (HasInductionInfo(context, instruction, &loop, &info, &trip)) { |
Aart Bik | 8e02e3e | 2017-02-28 14:41:55 -0800 | [diff] [blame] | 389 | if (info->induction_class == HInductionVarAnalysis::kLinear && |
Aart Bik | 7adb688 | 2017-03-07 13:28:51 -0800 | [diff] [blame] | 390 | !HInductionVarAnalysis::IsNarrowingLinear(info)) { |
Aart Bik | 8e02e3e | 2017-02-28 14:41:55 -0800 | [diff] [blame] | 391 | int64_t stride_value = 0; |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 392 | if (IsConstant(context, loop, info->op_a, kExact, &stride_value) && stride_value == 1) { |
Aart Bik | 8e02e3e | 2017-02-28 14:41:55 -0800 | [diff] [blame] | 393 | int64_t off_value = 0; |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 394 | if (IsConstant(context, loop, info->op_b, kExact, &off_value)) { |
Aart Bik | 37dc4df | 2017-06-28 14:08:00 -0700 | [diff] [blame] | 395 | *offset = graph->GetConstant(info->op_b->type, off_value); |
| 396 | } else if (info->op_b->operation == HInductionVarAnalysis::kFetch) { |
Aart Bik | 8e02e3e | 2017-02-28 14:41:55 -0800 | [diff] [blame] | 397 | *offset = info->op_b->fetch; |
Aart Bik | 37dc4df | 2017-06-28 14:08:00 -0700 | [diff] [blame] | 398 | } else { |
| 399 | return false; |
Aart Bik | 8e02e3e | 2017-02-28 14:41:55 -0800 | [diff] [blame] | 400 | } |
| 401 | return true; |
| 402 | } |
| 403 | } |
| 404 | } |
| 405 | return false; |
| 406 | } |
| 407 | |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 408 | HInstruction* InductionVarRange::GenerateTripCount(const HLoopInformation* loop, |
Aart Bik | 8e02e3e | 2017-02-28 14:41:55 -0800 | [diff] [blame] | 409 | HGraph* graph, |
| 410 | HBasicBlock* block) { |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 411 | HInstruction* loop_control = GetLoopControl(loop); |
| 412 | HInductionVarAnalysis::InductionInfo *trip = induction_analysis_->LookupInfo(loop, loop_control); |
Aart Bik | 8e02e3e | 2017-02-28 14:41:55 -0800 | [diff] [blame] | 413 | if (trip != nullptr && !IsUnsafeTripCount(trip)) { |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 414 | const HBasicBlock* context = loop_control->GetBlock(); |
Aart Bik | 8e02e3e | 2017-02-28 14:41:55 -0800 | [diff] [blame] | 415 | HInstruction* taken_test = nullptr; |
| 416 | HInstruction* trip_expr = nullptr; |
| 417 | if (IsBodyTripCount(trip)) { |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 418 | if (!GenerateCode(context, |
| 419 | loop, |
| 420 | trip->op_b, |
| 421 | /*trip=*/ nullptr, |
| 422 | graph, |
| 423 | block, |
| 424 | /*is_min=*/ false, |
| 425 | &taken_test)) { |
Aart Bik | 8e02e3e | 2017-02-28 14:41:55 -0800 | [diff] [blame] | 426 | return nullptr; |
| 427 | } |
| 428 | } |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 429 | if (GenerateCode(context, |
| 430 | loop, |
| 431 | trip->op_a, |
| 432 | /*trip=*/ nullptr, |
| 433 | graph, |
| 434 | block, |
| 435 | /*is_min=*/ false, |
| 436 | &trip_expr)) { |
Aart Bik | 8e02e3e | 2017-02-28 14:41:55 -0800 | [diff] [blame] | 437 | if (taken_test != nullptr) { |
| 438 | HInstruction* zero = graph->GetConstant(trip->type, 0); |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 439 | ArenaAllocator* allocator = graph->GetAllocator(); |
| 440 | trip_expr = Insert(block, new (allocator) HSelect(taken_test, trip_expr, zero, kNoDexPc)); |
Aart Bik | 8e02e3e | 2017-02-28 14:41:55 -0800 | [diff] [blame] | 441 | } |
| 442 | return trip_expr; |
| 443 | } |
| 444 | } |
| 445 | return nullptr; |
| 446 | } |
| 447 | |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 448 | // |
| 449 | // Private class methods. |
| 450 | // |
| 451 | |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 452 | bool InductionVarRange::CheckForFiniteAndConstantProps(const HLoopInformation* loop, |
Artem Serov | 121f203 | 2017-10-23 19:19:06 +0100 | [diff] [blame] | 453 | /*out*/ bool* is_constant, |
| 454 | /*out*/ int64_t* trip_count) const { |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 455 | HInstruction* loop_control = GetLoopControl(loop); |
| 456 | HInductionVarAnalysis::InductionInfo *trip = induction_analysis_->LookupInfo(loop, loop_control); |
Artem Serov | 121f203 | 2017-10-23 19:19:06 +0100 | [diff] [blame] | 457 | if (trip != nullptr && !IsUnsafeTripCount(trip)) { |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 458 | const HBasicBlock* context = loop_control->GetBlock(); |
| 459 | *is_constant = IsConstant(context, loop, trip->op_a, kExact, trip_count); |
Artem Serov | 121f203 | 2017-10-23 19:19:06 +0100 | [diff] [blame] | 460 | return true; |
| 461 | } |
| 462 | return false; |
| 463 | } |
| 464 | |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 465 | bool InductionVarRange::IsConstant(const HBasicBlock* context, |
| 466 | const HLoopInformation* loop, |
| 467 | HInductionVarAnalysis::InductionInfo* info, |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 468 | ConstantRequest request, |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 469 | /*out*/ int64_t* value) const { |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 470 | if (info != nullptr) { |
| 471 | // A direct 32-bit or 64-bit constant fetch. This immediately satisfies |
| 472 | // any of the three requests (kExact, kAtMost, and KAtLeast). |
| 473 | if (info->induction_class == HInductionVarAnalysis::kInvariant && |
| 474 | info->operation == HInductionVarAnalysis::kFetch) { |
Aart Bik | f3e61ee | 2017-04-12 17:09:20 -0700 | [diff] [blame] | 475 | if (IsInt64AndGet(info->fetch, value)) { |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 476 | return true; |
| 477 | } |
| 478 | } |
Aart Bik | 40fbf74 | 2016-10-31 11:02:50 -0700 | [diff] [blame] | 479 | // Try range analysis on the invariant, only accept a proper range |
| 480 | // to avoid arithmetic wrap-around anomalies. |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 481 | Value min_val = GetVal(context, loop, info, /*trip=*/ nullptr, /*is_min=*/ true); |
| 482 | Value max_val = GetVal(context, loop, info, /*trip=*/ nullptr, /*is_min=*/ false); |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 483 | if (IsConstantValue(min_val) && |
| 484 | IsConstantValue(max_val) && min_val.b_constant <= max_val.b_constant) { |
| 485 | if ((request == kExact && min_val.b_constant == max_val.b_constant) || request == kAtMost) { |
| 486 | *value = max_val.b_constant; |
| 487 | return true; |
| 488 | } else if (request == kAtLeast) { |
| 489 | *value = min_val.b_constant; |
Aart Bik | 358af83 | 2016-02-24 14:17:53 -0800 | [diff] [blame] | 490 | return true; |
| 491 | } |
| 492 | } |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 493 | } |
| 494 | return false; |
| 495 | } |
| 496 | |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 497 | bool InductionVarRange::HasInductionInfo( |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 498 | const HBasicBlock* context, |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 499 | HInstruction* instruction, |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 500 | /*out*/ const HLoopInformation** loop, |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 501 | /*out*/ HInductionVarAnalysis::InductionInfo** info, |
| 502 | /*out*/ HInductionVarAnalysis::InductionInfo** trip) const { |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 503 | DCHECK(context != nullptr); |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 504 | HLoopInformation* lp = context->GetLoopInformation(); // closest enveloping loop |
Aart Bik | 009cace | 2016-09-16 10:15:19 -0700 | [diff] [blame] | 505 | if (lp != nullptr) { |
| 506 | HInductionVarAnalysis::InductionInfo* i = induction_analysis_->LookupInfo(lp, instruction); |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 507 | if (i != nullptr) { |
Aart Bik | 009cace | 2016-09-16 10:15:19 -0700 | [diff] [blame] | 508 | *loop = lp; |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 509 | *info = i; |
Aart Bik | 009cace | 2016-09-16 10:15:19 -0700 | [diff] [blame] | 510 | *trip = induction_analysis_->LookupInfo(lp, GetLoopControl(lp)); |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 511 | return true; |
| 512 | } |
| 513 | } |
| 514 | return false; |
| 515 | } |
| 516 | |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 517 | bool InductionVarRange::IsWellBehavedTripCount(const HBasicBlock* context, |
| 518 | const HLoopInformation* loop, |
| 519 | HInductionVarAnalysis::InductionInfo* trip) const { |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 520 | if (trip != nullptr) { |
| 521 | // Both bounds that define a trip-count are well-behaved if they either are not defined |
| 522 | // in any loop, or are contained in a proper interval. This allows finding the min/max |
| 523 | // of an expression by chasing outward. |
| 524 | InductionVarRange range(induction_analysis_); |
| 525 | HInductionVarAnalysis::InductionInfo* lower = trip->op_b->op_a; |
| 526 | HInductionVarAnalysis::InductionInfo* upper = trip->op_b->op_b; |
| 527 | int64_t not_used = 0; |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 528 | return |
| 529 | (!HasFetchInLoop(lower) || range.IsConstant(context, loop, lower, kAtLeast, ¬_used)) && |
| 530 | (!HasFetchInLoop(upper) || range.IsConstant(context, loop, upper, kAtLeast, ¬_used)); |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 531 | } |
| 532 | return true; |
| 533 | } |
| 534 | |
| 535 | bool InductionVarRange::HasFetchInLoop(HInductionVarAnalysis::InductionInfo* info) const { |
| 536 | if (info != nullptr) { |
| 537 | if (info->induction_class == HInductionVarAnalysis::kInvariant && |
| 538 | info->operation == HInductionVarAnalysis::kFetch) { |
| 539 | return info->fetch->GetBlock()->GetLoopInformation() != nullptr; |
| 540 | } |
| 541 | return HasFetchInLoop(info->op_a) || HasFetchInLoop(info->op_b); |
| 542 | } |
| 543 | return false; |
| 544 | } |
| 545 | |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 546 | bool InductionVarRange::NeedsTripCount(const HBasicBlock* context, |
| 547 | const HLoopInformation* loop, |
| 548 | HInductionVarAnalysis::InductionInfo* info, |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 549 | int64_t* stride_value) const { |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 550 | if (info != nullptr) { |
| 551 | if (info->induction_class == HInductionVarAnalysis::kLinear) { |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 552 | return IsConstant(context, loop, info->op_a, kExact, stride_value); |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 553 | } else if (info->induction_class == HInductionVarAnalysis::kPolynomial) { |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 554 | return NeedsTripCount(context, loop, info->op_a, stride_value); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 555 | } else if (info->induction_class == HInductionVarAnalysis::kWrapAround) { |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 556 | return NeedsTripCount(context, loop, info->op_b, stride_value); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 557 | } |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 558 | } |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 559 | return false; |
| 560 | } |
| 561 | |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 562 | bool InductionVarRange::IsBodyTripCount(HInductionVarAnalysis::InductionInfo* trip) const { |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 563 | if (trip != nullptr) { |
| 564 | if (trip->induction_class == HInductionVarAnalysis::kInvariant) { |
| 565 | return trip->operation == HInductionVarAnalysis::kTripCountInBody || |
| 566 | trip->operation == HInductionVarAnalysis::kTripCountInBodyUnsafe; |
| 567 | } |
| 568 | } |
| 569 | return false; |
| 570 | } |
| 571 | |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 572 | bool InductionVarRange::IsUnsafeTripCount(HInductionVarAnalysis::InductionInfo* trip) const { |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 573 | if (trip != nullptr) { |
| 574 | if (trip->induction_class == HInductionVarAnalysis::kInvariant) { |
| 575 | return trip->operation == HInductionVarAnalysis::kTripCountInBodyUnsafe || |
| 576 | trip->operation == HInductionVarAnalysis::kTripCountInLoopUnsafe; |
| 577 | } |
| 578 | } |
| 579 | return false; |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 580 | } |
| 581 | |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 582 | InductionVarRange::Value InductionVarRange::GetLinear(const HBasicBlock* context, |
| 583 | const HLoopInformation* loop, |
| 584 | HInductionVarAnalysis::InductionInfo* info, |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 585 | HInductionVarAnalysis::InductionInfo* trip, |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 586 | bool is_min) const { |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 587 | DCHECK(info != nullptr); |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 588 | DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kLinear); |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 589 | // Detect common situation where an offset inside the trip-count cancels out during range |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 590 | // analysis (finding max a * (TC - 1) + OFFSET for a == 1 and TC = UPPER - OFFSET or finding |
| 591 | // min a * (TC - 1) + OFFSET for a == -1 and TC = OFFSET - UPPER) to avoid losing information |
| 592 | // with intermediate results that only incorporate single instructions. |
| 593 | if (trip != nullptr) { |
| 594 | HInductionVarAnalysis::InductionInfo* trip_expr = trip->op_a; |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 595 | if (trip_expr->type == info->type && trip_expr->operation == HInductionVarAnalysis::kSub) { |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 596 | int64_t stride_value = 0; |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 597 | if (IsConstant(context, loop, info->op_a, kExact, &stride_value)) { |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 598 | if (!is_min && stride_value == 1) { |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 599 | // Test original trip's negative operand (trip_expr->op_b) against offset of induction. |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 600 | if (HInductionVarAnalysis::InductionEqual(trip_expr->op_b, info->op_b)) { |
| 601 | // Analyze cancelled trip with just the positive operand (trip_expr->op_a). |
| 602 | HInductionVarAnalysis::InductionInfo cancelled_trip( |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 603 | trip->induction_class, |
| 604 | trip->operation, |
| 605 | trip_expr->op_a, |
| 606 | trip->op_b, |
| 607 | nullptr, |
| 608 | trip->type); |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 609 | return GetVal(context, loop, &cancelled_trip, trip, is_min); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 610 | } |
| 611 | } else if (is_min && stride_value == -1) { |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 612 | // Test original trip's positive operand (trip_expr->op_a) against offset of induction. |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 613 | if (HInductionVarAnalysis::InductionEqual(trip_expr->op_a, info->op_b)) { |
| 614 | // Analyze cancelled trip with just the negative operand (trip_expr->op_b). |
| 615 | HInductionVarAnalysis::InductionInfo neg( |
| 616 | HInductionVarAnalysis::kInvariant, |
| 617 | HInductionVarAnalysis::kNeg, |
| 618 | nullptr, |
| 619 | trip_expr->op_b, |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 620 | nullptr, |
| 621 | trip->type); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 622 | HInductionVarAnalysis::InductionInfo cancelled_trip( |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 623 | trip->induction_class, trip->operation, &neg, trip->op_b, nullptr, trip->type); |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 624 | return SubValue(Value(0), GetVal(context, loop, &cancelled_trip, trip, !is_min)); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 625 | } |
| 626 | } |
| 627 | } |
| 628 | } |
| 629 | } |
| 630 | // General rule of linear induction a * i + b, for normalized 0 <= i < TC. |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 631 | return AddValue(GetMul(context, loop, info->op_a, trip, trip, is_min), |
| 632 | GetVal(context, loop, info->op_b, trip, is_min)); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 633 | } |
| 634 | |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 635 | InductionVarRange::Value InductionVarRange::GetPolynomial( |
| 636 | const HBasicBlock* context, |
| 637 | const HLoopInformation* loop, |
| 638 | HInductionVarAnalysis::InductionInfo* info, |
| 639 | HInductionVarAnalysis::InductionInfo* trip, |
| 640 | bool is_min) const { |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 641 | DCHECK(info != nullptr); |
| 642 | DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kPolynomial); |
| 643 | int64_t a = 0; |
| 644 | int64_t b = 0; |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 645 | if (IsConstant(context, loop, info->op_a->op_a, kExact, &a) && |
| 646 | CanLongValueFitIntoInt(a) && |
| 647 | a >= 0 && |
| 648 | IsConstant(context, loop, info->op_a->op_b, kExact, &b) && |
| 649 | CanLongValueFitIntoInt(b) && |
| 650 | b >= 0) { |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 651 | // Evaluate bounds on sum_i=0^m-1(a * i + b) + c with a,b >= 0 for |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 652 | // maximum index value m as a * (m * (m-1)) / 2 + b * m + c. |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 653 | // Do not simply return `c` as minimum because the trip count may be non-zero |
| 654 | // if the `context` is after the `loop` (and therefore ignoring `is_min`). |
| 655 | Value c = GetVal(context, loop, info->op_b, trip, is_min); |
| 656 | Value m = GetVal(context, loop, trip, trip, is_min); |
| 657 | Value t = DivValue(MulValue(m, SubValue(m, Value(1))), Value(2)); |
| 658 | Value x = MulValue(Value(a), t); |
| 659 | Value y = MulValue(Value(b), m); |
| 660 | return AddValue(AddValue(x, y), c); |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 661 | } |
| 662 | return Value(); |
| 663 | } |
| 664 | |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 665 | InductionVarRange::Value InductionVarRange::GetGeometric(const HBasicBlock* context, |
| 666 | const HLoopInformation* loop, |
| 667 | HInductionVarAnalysis::InductionInfo* info, |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 668 | HInductionVarAnalysis::InductionInfo* trip, |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 669 | bool is_min) const { |
| 670 | DCHECK(info != nullptr); |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 671 | DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kGeometric); |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 672 | int64_t a = 0; |
| 673 | int64_t f = 0; |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 674 | if (IsConstant(context, loop, info->op_a, kExact, &a) && |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 675 | CanLongValueFitIntoInt(a) && |
Aart Bik | f3e61ee | 2017-04-12 17:09:20 -0700 | [diff] [blame] | 676 | IsInt64AndGet(info->fetch, &f) && f >= 1) { |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 677 | // Conservative bounds on a * f^-i + b with f >= 1 can be computed without |
| 678 | // trip count. Other forms would require a much more elaborate evaluation. |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 679 | const bool is_min_a = a >= 0 ? is_min : !is_min; |
| 680 | if (info->operation == HInductionVarAnalysis::kDiv) { |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 681 | Value b = GetVal(context, loop, info->op_b, trip, is_min); |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 682 | return is_min_a ? b : AddValue(Value(a), b); |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 683 | } |
| 684 | } |
| 685 | return Value(); |
| 686 | } |
| 687 | |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 688 | InductionVarRange::Value InductionVarRange::GetFetch(const HBasicBlock* context, |
| 689 | const HLoopInformation* loop, |
| 690 | HInstruction* instruction, |
Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 691 | HInductionVarAnalysis::InductionInfo* trip, |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 692 | bool is_min) const { |
Aart Bik | 40fbf74 | 2016-10-31 11:02:50 -0700 | [diff] [blame] | 693 | // Special case when chasing constants: single instruction that denotes trip count in the |
| 694 | // loop-body is minimal 1 and maximal, with safe trip-count, max int, |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 695 | if (chase_hint_ == nullptr && |
| 696 | IsContextInBody(context, loop) && |
| 697 | trip != nullptr && |
| 698 | instruction == trip->op_a->fetch) { |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 699 | if (is_min) { |
| 700 | return Value(1); |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 701 | } else if (!instruction->IsConstant() && !IsUnsafeTripCount(trip)) { |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 702 | return Value(std::numeric_limits<int32_t>::max()); |
| 703 | } |
| 704 | } |
Aart Bik | 40fbf74 | 2016-10-31 11:02:50 -0700 | [diff] [blame] | 705 | // Unless at a constant or hint, chase the instruction a bit deeper into the HIR tree, so that |
| 706 | // it becomes more likely range analysis will compare the same instructions as terminal nodes. |
| 707 | int64_t value; |
Aart Bik | f3e61ee | 2017-04-12 17:09:20 -0700 | [diff] [blame] | 708 | if (IsInt64AndGet(instruction, &value) && CanLongValueFitIntoInt(value)) { |
Aart Bik | 40fbf74 | 2016-10-31 11:02:50 -0700 | [diff] [blame] | 709 | // Proper constant reveals best information. |
| 710 | return Value(static_cast<int32_t>(value)); |
| 711 | } else if (instruction == chase_hint_) { |
| 712 | // At hint, fetch is represented by itself. |
| 713 | return Value(instruction, 1, 0); |
| 714 | } else if (instruction->IsAdd()) { |
| 715 | // Incorporate suitable constants in the chased value. |
Aart Bik | f3e61ee | 2017-04-12 17:09:20 -0700 | [diff] [blame] | 716 | if (IsInt64AndGet(instruction->InputAt(0), &value) && CanLongValueFitIntoInt(value)) { |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 717 | return AddValue(Value(static_cast<int32_t>(value)), |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 718 | GetFetch(context, loop, instruction->InputAt(1), trip, is_min)); |
Aart Bik | f3e61ee | 2017-04-12 17:09:20 -0700 | [diff] [blame] | 719 | } else if (IsInt64AndGet(instruction->InputAt(1), &value) && CanLongValueFitIntoInt(value)) { |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 720 | return AddValue(GetFetch(context, loop, instruction->InputAt(0), trip, is_min), |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 721 | Value(static_cast<int32_t>(value))); |
Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 722 | } |
Aart Bik | 8e9090b | 2017-09-08 16:46:50 -0700 | [diff] [blame] | 723 | } else if (instruction->IsSub()) { |
| 724 | // Incorporate suitable constants in the chased value. |
| 725 | if (IsInt64AndGet(instruction->InputAt(0), &value) && CanLongValueFitIntoInt(value)) { |
| 726 | return SubValue(Value(static_cast<int32_t>(value)), |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 727 | GetFetch(context, loop, instruction->InputAt(1), trip, !is_min)); |
Aart Bik | 8e9090b | 2017-09-08 16:46:50 -0700 | [diff] [blame] | 728 | } else if (IsInt64AndGet(instruction->InputAt(1), &value) && CanLongValueFitIntoInt(value)) { |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 729 | return SubValue(GetFetch(context, loop, instruction->InputAt(0), trip, is_min), |
Aart Bik | 8e9090b | 2017-09-08 16:46:50 -0700 | [diff] [blame] | 730 | Value(static_cast<int32_t>(value))); |
| 731 | } |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 732 | } else if (instruction->IsArrayLength()) { |
Aart Bik | 40fbf74 | 2016-10-31 11:02:50 -0700 | [diff] [blame] | 733 | // Exploit length properties when chasing constants or chase into a new array declaration. |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 734 | if (chase_hint_ == nullptr) { |
| 735 | return is_min ? Value(0) : Value(std::numeric_limits<int32_t>::max()); |
| 736 | } else if (instruction->InputAt(0)->IsNewArray()) { |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 737 | return GetFetch( |
| 738 | context, loop, instruction->InputAt(0)->AsNewArray()->GetLength(), trip, is_min); |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 739 | } |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 740 | } else if (instruction->IsTypeConversion()) { |
Aart Bik | 40fbf74 | 2016-10-31 11:02:50 -0700 | [diff] [blame] | 741 | // Since analysis is 32-bit (or narrower), chase beyond widening along the path. |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 742 | // For example, this discovers the length in: for (long i = 0; i < a.length; i++); |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 743 | if (instruction->AsTypeConversion()->GetInputType() == DataType::Type::kInt32 && |
| 744 | instruction->AsTypeConversion()->GetResultType() == DataType::Type::kInt64) { |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 745 | return GetFetch(context, loop, instruction->InputAt(0), trip, is_min); |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 746 | } |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 747 | } |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 748 | // Chase an invariant fetch that is defined by another loop if the trip-count used |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 749 | // so far is well-behaved in both bounds and the next trip-count is safe. |
| 750 | // Example: |
| 751 | // for (int i = 0; i <= 100; i++) // safe |
| 752 | // for (int j = 0; j <= i; j++) // well-behaved |
| 753 | // j is in range [0, i ] (if i is chase hint) |
| 754 | // or in range [0, 100] (otherwise) |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 755 | // Example: |
| 756 | // for (i = 0; i < 100; ++i) |
| 757 | // <some-code> |
| 758 | // for (j = 0; j < 10; ++j) |
| 759 | // sum += i; // The `i` is a "fetch" of a loop Phi from the previous loop. |
| 760 | const HLoopInformation* next_loop = nullptr; |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 761 | HInductionVarAnalysis::InductionInfo* next_info = nullptr; |
| 762 | HInductionVarAnalysis::InductionInfo* next_trip = nullptr; |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 763 | if (HasInductionInfo(instruction->GetBlock(), instruction, &next_loop, &next_info, &next_trip) && |
| 764 | IsWellBehavedTripCount(context, next_loop, trip) && |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 765 | !IsUnsafeTripCount(next_trip)) { |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 766 | return GetVal(context, next_loop, next_info, next_trip, is_min); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 767 | } |
Aart Bik | 40fbf74 | 2016-10-31 11:02:50 -0700 | [diff] [blame] | 768 | // Fetch is represented by itself. |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 769 | return Value(instruction, 1, 0); |
| 770 | } |
| 771 | |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 772 | InductionVarRange::Value InductionVarRange::GetVal(const HBasicBlock* context, |
| 773 | const HLoopInformation* loop, |
| 774 | HInductionVarAnalysis::InductionInfo* info, |
Aart Bik | cd26feb | 2015-09-23 17:50:50 -0700 | [diff] [blame] | 775 | HInductionVarAnalysis::InductionInfo* trip, |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 776 | bool is_min) const { |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 777 | if (info != nullptr) { |
| 778 | switch (info->induction_class) { |
| 779 | case HInductionVarAnalysis::kInvariant: |
| 780 | // Invariants. |
| 781 | switch (info->operation) { |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 782 | case HInductionVarAnalysis::kAdd: |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 783 | return AddValue(GetVal(context, loop, info->op_a, trip, is_min), |
| 784 | GetVal(context, loop, info->op_b, trip, is_min)); |
Aart Bik | cd26feb | 2015-09-23 17:50:50 -0700 | [diff] [blame] | 785 | case HInductionVarAnalysis::kSub: // second reversed! |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 786 | return SubValue(GetVal(context, loop, info->op_a, trip, is_min), |
| 787 | GetVal(context, loop, info->op_b, trip, !is_min)); |
Aart Bik | cd26feb | 2015-09-23 17:50:50 -0700 | [diff] [blame] | 788 | case HInductionVarAnalysis::kNeg: // second reversed! |
| 789 | return SubValue(Value(0), |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 790 | GetVal(context, loop, info->op_b, trip, !is_min)); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 791 | case HInductionVarAnalysis::kMul: |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 792 | return GetMul(context, loop, info->op_a, info->op_b, trip, is_min); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 793 | case HInductionVarAnalysis::kDiv: |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 794 | return GetDiv(context, loop, info->op_a, info->op_b, trip, is_min); |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 795 | case HInductionVarAnalysis::kRem: |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 796 | return GetRem(context, loop, info->op_a, info->op_b); |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 797 | case HInductionVarAnalysis::kXor: |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 798 | return GetXor(context, loop, info->op_a, info->op_b); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 799 | case HInductionVarAnalysis::kFetch: |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 800 | return GetFetch(context, loop, info->fetch, trip, is_min); |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 801 | case HInductionVarAnalysis::kTripCountInLoop: |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 802 | case HInductionVarAnalysis::kTripCountInLoopUnsafe: |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 803 | if (UseFullTripCount(context, loop, is_min)) { |
| 804 | // Return the full trip count (do not subtract 1 as we do in loop body). |
| 805 | return GetVal(context, loop, info->op_a, trip, /*is_min=*/ false); |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 806 | } |
| 807 | FALLTHROUGH_INTENDED; |
| 808 | case HInductionVarAnalysis::kTripCountInBody: |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 809 | case HInductionVarAnalysis::kTripCountInBodyUnsafe: |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 810 | if (is_min) { |
| 811 | return Value(0); |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 812 | } else if (IsContextInBody(context, loop)) { |
| 813 | return SubValue(GetVal(context, loop, info->op_a, trip, is_min), Value(1)); |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 814 | } |
| 815 | break; |
| 816 | default: |
| 817 | break; |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 818 | } |
| 819 | break; |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 820 | case HInductionVarAnalysis::kLinear: |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 821 | return CorrectForType(GetLinear(context, loop, info, trip, is_min), info->type); |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 822 | case HInductionVarAnalysis::kPolynomial: |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 823 | return GetPolynomial(context, loop, info, trip, is_min); |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 824 | case HInductionVarAnalysis::kGeometric: |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 825 | return GetGeometric(context, loop, info, trip, is_min); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 826 | case HInductionVarAnalysis::kWrapAround: |
| 827 | case HInductionVarAnalysis::kPeriodic: |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 828 | return MergeVal(GetVal(context, loop, info->op_a, trip, is_min), |
| 829 | GetVal(context, loop, info->op_b, trip, is_min), |
| 830 | is_min); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 831 | } |
| 832 | } |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 833 | return Value(); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 834 | } |
| 835 | |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 836 | InductionVarRange::Value InductionVarRange::GetMul(const HBasicBlock* context, |
| 837 | const HLoopInformation* loop, |
| 838 | HInductionVarAnalysis::InductionInfo* info1, |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 839 | HInductionVarAnalysis::InductionInfo* info2, |
| 840 | HInductionVarAnalysis::InductionInfo* trip, |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 841 | bool is_min) const { |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 842 | // Constant times range. |
| 843 | int64_t value = 0; |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 844 | if (IsConstant(context, loop, info1, kExact, &value)) { |
| 845 | return MulRangeAndConstant(context, loop, value, info2, trip, is_min); |
| 846 | } else if (IsConstant(context, loop, info2, kExact, &value)) { |
| 847 | return MulRangeAndConstant(context, loop, value, info1, trip, is_min); |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 848 | } |
| 849 | // Interval ranges. |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 850 | Value v1_min = GetVal(context, loop, info1, trip, /*is_min=*/ true); |
| 851 | Value v1_max = GetVal(context, loop, info1, trip, /*is_min=*/ false); |
| 852 | Value v2_min = GetVal(context, loop, info2, trip, /*is_min=*/ true); |
| 853 | Value v2_max = GetVal(context, loop, info2, trip, /*is_min=*/ false); |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 854 | // Positive range vs. positive or negative range. |
| 855 | if (IsConstantValue(v1_min) && v1_min.b_constant >= 0) { |
| 856 | if (IsConstantValue(v2_min) && v2_min.b_constant >= 0) { |
| 857 | return is_min ? MulValue(v1_min, v2_min) : MulValue(v1_max, v2_max); |
| 858 | } else if (IsConstantValue(v2_max) && v2_max.b_constant <= 0) { |
| 859 | return is_min ? MulValue(v1_max, v2_min) : MulValue(v1_min, v2_max); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 860 | } |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 861 | } |
| 862 | // Negative range vs. positive or negative range. |
| 863 | if (IsConstantValue(v1_max) && v1_max.b_constant <= 0) { |
| 864 | if (IsConstantValue(v2_min) && v2_min.b_constant >= 0) { |
| 865 | return is_min ? MulValue(v1_min, v2_max) : MulValue(v1_max, v2_min); |
| 866 | } else if (IsConstantValue(v2_max) && v2_max.b_constant <= 0) { |
| 867 | return is_min ? MulValue(v1_max, v2_max) : MulValue(v1_min, v2_min); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 868 | } |
| 869 | } |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 870 | return Value(); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 871 | } |
| 872 | |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 873 | InductionVarRange::Value InductionVarRange::GetDiv(const HBasicBlock* context, |
| 874 | const HLoopInformation* loop, |
| 875 | HInductionVarAnalysis::InductionInfo* info1, |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 876 | HInductionVarAnalysis::InductionInfo* info2, |
| 877 | HInductionVarAnalysis::InductionInfo* trip, |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 878 | bool is_min) const { |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 879 | // Range divided by constant. |
| 880 | int64_t value = 0; |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 881 | if (IsConstant(context, loop, info2, kExact, &value)) { |
| 882 | return DivRangeAndConstant(context, loop, value, info1, trip, is_min); |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 883 | } |
| 884 | // Interval ranges. |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 885 | Value v1_min = GetVal(context, loop, info1, trip, /*is_min=*/ true); |
| 886 | Value v1_max = GetVal(context, loop, info1, trip, /*is_min=*/ false); |
| 887 | Value v2_min = GetVal(context, loop, info2, trip, /*is_min=*/ true); |
| 888 | Value v2_max = GetVal(context, loop, info2, trip, /*is_min=*/ false); |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 889 | // Positive range vs. positive or negative range. |
| 890 | if (IsConstantValue(v1_min) && v1_min.b_constant >= 0) { |
| 891 | if (IsConstantValue(v2_min) && v2_min.b_constant >= 0) { |
| 892 | return is_min ? DivValue(v1_min, v2_max) : DivValue(v1_max, v2_min); |
| 893 | } else if (IsConstantValue(v2_max) && v2_max.b_constant <= 0) { |
| 894 | return is_min ? DivValue(v1_max, v2_max) : DivValue(v1_min, v2_min); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 895 | } |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 896 | } |
| 897 | // Negative range vs. positive or negative range. |
| 898 | if (IsConstantValue(v1_max) && v1_max.b_constant <= 0) { |
| 899 | if (IsConstantValue(v2_min) && v2_min.b_constant >= 0) { |
| 900 | return is_min ? DivValue(v1_min, v2_min) : DivValue(v1_max, v2_max); |
| 901 | } else if (IsConstantValue(v2_max) && v2_max.b_constant <= 0) { |
| 902 | return is_min ? DivValue(v1_max, v2_min) : DivValue(v1_min, v2_max); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 903 | } |
| 904 | } |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 905 | return Value(); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 906 | } |
| 907 | |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 908 | InductionVarRange::Value InductionVarRange::GetRem( |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 909 | const HBasicBlock* context, |
| 910 | const HLoopInformation* loop, |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 911 | HInductionVarAnalysis::InductionInfo* info1, |
| 912 | HInductionVarAnalysis::InductionInfo* info2) const { |
| 913 | int64_t v1 = 0; |
| 914 | int64_t v2 = 0; |
| 915 | // Only accept exact values. |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 916 | if (IsConstant(context, loop, info1, kExact, &v1) && |
| 917 | IsConstant(context, loop, info2, kExact, &v2) && |
| 918 | v2 != 0) { |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 919 | int64_t value = v1 % v2; |
| 920 | if (CanLongValueFitIntoInt(value)) { |
| 921 | return Value(static_cast<int32_t>(value)); |
| 922 | } |
| 923 | } |
| 924 | return Value(); |
| 925 | } |
| 926 | |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 927 | InductionVarRange::Value InductionVarRange::GetXor( |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 928 | const HBasicBlock* context, |
| 929 | const HLoopInformation* loop, |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 930 | HInductionVarAnalysis::InductionInfo* info1, |
| 931 | HInductionVarAnalysis::InductionInfo* info2) const { |
| 932 | int64_t v1 = 0; |
| 933 | int64_t v2 = 0; |
| 934 | // Only accept exact values. |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 935 | if (IsConstant(context, loop, info1, kExact, &v1) && |
| 936 | IsConstant(context, loop, info2, kExact, &v2)) { |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 937 | int64_t value = v1 ^ v2; |
| 938 | if (CanLongValueFitIntoInt(value)) { |
| 939 | return Value(static_cast<int32_t>(value)); |
| 940 | } |
| 941 | } |
| 942 | return Value(); |
| 943 | } |
| 944 | |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 945 | InductionVarRange::Value InductionVarRange::MulRangeAndConstant( |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 946 | const HBasicBlock* context, |
| 947 | const HLoopInformation* loop, |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 948 | int64_t value, |
| 949 | HInductionVarAnalysis::InductionInfo* info, |
| 950 | HInductionVarAnalysis::InductionInfo* trip, |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 951 | bool is_min) const { |
| 952 | if (CanLongValueFitIntoInt(value)) { |
| 953 | Value c(static_cast<int32_t>(value)); |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 954 | return MulValue(GetVal(context, loop, info, trip, is_min == value >= 0), c); |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 955 | } |
| 956 | return Value(); |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 957 | } |
| 958 | |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 959 | InductionVarRange::Value InductionVarRange::DivRangeAndConstant( |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 960 | const HBasicBlock* context, |
| 961 | const HLoopInformation* loop, |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 962 | int64_t value, |
| 963 | HInductionVarAnalysis::InductionInfo* info, |
| 964 | HInductionVarAnalysis::InductionInfo* trip, |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 965 | bool is_min) const { |
| 966 | if (CanLongValueFitIntoInt(value)) { |
| 967 | Value c(static_cast<int32_t>(value)); |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 968 | return DivValue(GetVal(context, loop, info, trip, is_min == value >= 0), c); |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 969 | } |
| 970 | return Value(); |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 971 | } |
| 972 | |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 973 | InductionVarRange::Value InductionVarRange::AddValue(Value v1, Value v2) const { |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 974 | if (v1.is_known && v2.is_known && IsSafeAdd(v1.b_constant, v2.b_constant)) { |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 975 | int32_t b = v1.b_constant + v2.b_constant; |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 976 | if (v1.a_constant == 0) { |
| 977 | return Value(v2.instruction, v2.a_constant, b); |
| 978 | } else if (v2.a_constant == 0) { |
| 979 | return Value(v1.instruction, v1.a_constant, b); |
| 980 | } else if (v1.instruction == v2.instruction && IsSafeAdd(v1.a_constant, v2.a_constant)) { |
| 981 | return Value(v1.instruction, v1.a_constant + v2.a_constant, b); |
| 982 | } |
| 983 | } |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 984 | return Value(); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 985 | } |
| 986 | |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 987 | InductionVarRange::Value InductionVarRange::SubValue(Value v1, Value v2) const { |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 988 | if (v1.is_known && v2.is_known && IsSafeSub(v1.b_constant, v2.b_constant)) { |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 989 | int32_t b = v1.b_constant - v2.b_constant; |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 990 | if (v1.a_constant == 0 && IsSafeSub(0, v2.a_constant)) { |
| 991 | return Value(v2.instruction, -v2.a_constant, b); |
| 992 | } else if (v2.a_constant == 0) { |
| 993 | return Value(v1.instruction, v1.a_constant, b); |
| 994 | } else if (v1.instruction == v2.instruction && IsSafeSub(v1.a_constant, v2.a_constant)) { |
| 995 | return Value(v1.instruction, v1.a_constant - v2.a_constant, b); |
| 996 | } |
| 997 | } |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 998 | return Value(); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 999 | } |
| 1000 | |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 1001 | InductionVarRange::Value InductionVarRange::MulValue(Value v1, Value v2) const { |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 1002 | if (v1.is_known && v2.is_known) { |
| 1003 | if (v1.a_constant == 0) { |
| 1004 | if (IsSafeMul(v1.b_constant, v2.a_constant) && IsSafeMul(v1.b_constant, v2.b_constant)) { |
| 1005 | return Value(v2.instruction, v1.b_constant * v2.a_constant, v1.b_constant * v2.b_constant); |
| 1006 | } |
| 1007 | } else if (v2.a_constant == 0) { |
| 1008 | if (IsSafeMul(v1.a_constant, v2.b_constant) && IsSafeMul(v1.b_constant, v2.b_constant)) { |
| 1009 | return Value(v1.instruction, v1.a_constant * v2.b_constant, v1.b_constant * v2.b_constant); |
| 1010 | } |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 1011 | } |
| 1012 | } |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 1013 | return Value(); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 1014 | } |
| 1015 | |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 1016 | InductionVarRange::Value InductionVarRange::DivValue(Value v1, Value v2) const { |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 1017 | if (v1.is_known && v2.is_known && v1.a_constant == 0 && v2.a_constant == 0) { |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 1018 | if (IsSafeDiv(v1.b_constant, v2.b_constant)) { |
| 1019 | return Value(v1.b_constant / v2.b_constant); |
| 1020 | } |
| 1021 | } |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 1022 | return Value(); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 1023 | } |
| 1024 | |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 1025 | InductionVarRange::Value InductionVarRange::MergeVal(Value v1, Value v2, bool is_min) const { |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 1026 | if (v1.is_known && v2.is_known) { |
| 1027 | if (v1.instruction == v2.instruction && v1.a_constant == v2.a_constant) { |
Aart Bik | cd26feb | 2015-09-23 17:50:50 -0700 | [diff] [blame] | 1028 | return Value(v1.instruction, v1.a_constant, |
| 1029 | is_min ? std::min(v1.b_constant, v2.b_constant) |
| 1030 | : std::max(v1.b_constant, v2.b_constant)); |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 1031 | } |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 1032 | } |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 1033 | return Value(); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 1034 | } |
| 1035 | |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1036 | bool InductionVarRange::GenerateRangeOrLastValue(const HBasicBlock* context, |
Aart Bik | 9abf894 | 2016-10-14 09:49:42 -0700 | [diff] [blame] | 1037 | HInstruction* instruction, |
| 1038 | bool is_last_value, |
| 1039 | HGraph* graph, |
| 1040 | HBasicBlock* block, |
| 1041 | /*out*/HInstruction** lower, |
| 1042 | /*out*/HInstruction** upper, |
| 1043 | /*out*/HInstruction** taken_test, |
| 1044 | /*out*/int64_t* stride_value, |
| 1045 | /*out*/bool* needs_finite_test, |
| 1046 | /*out*/bool* needs_taken_test) const { |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1047 | const HLoopInformation* loop = nullptr; |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 1048 | HInductionVarAnalysis::InductionInfo* info = nullptr; |
| 1049 | HInductionVarAnalysis::InductionInfo* trip = nullptr; |
| 1050 | if (!HasInductionInfo(context, instruction, &loop, &info, &trip) || trip == nullptr) { |
| 1051 | return false; // codegen needs all information, including tripcount |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 1052 | } |
| 1053 | // Determine what tests are needed. A finite test is needed if the evaluation code uses the |
| 1054 | // trip-count and the loop maybe unsafe (because in such cases, the index could "overshoot" |
| 1055 | // the computed range). A taken test is needed for any unknown trip-count, even if evaluation |
| 1056 | // code does not use the trip-count explicitly (since there could be an implicit relation |
| 1057 | // between e.g. an invariant subscript and a not-taken condition). |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 1058 | *stride_value = 0; |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1059 | *needs_finite_test = NeedsTripCount(context, loop, info, stride_value) && IsUnsafeTripCount(trip); |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 1060 | *needs_taken_test = IsBodyTripCount(trip); |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 1061 | // Handle last value request. |
| 1062 | if (is_last_value) { |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1063 | DCHECK(!IsContextInBody(context, loop)); |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 1064 | switch (info->induction_class) { |
| 1065 | case HInductionVarAnalysis::kLinear: |
| 1066 | if (*stride_value > 0) { |
| 1067 | lower = nullptr; |
Santiago Aboy Solanes | 4f18b94 | 2022-07-21 10:31:21 +0100 | [diff] [blame] | 1068 | return GenerateLastValueLinear( |
| 1069 | context, loop, info, trip, graph, block, /*is_min=*/false, upper); |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 1070 | } else { |
| 1071 | upper = nullptr; |
Santiago Aboy Solanes | 4f18b94 | 2022-07-21 10:31:21 +0100 | [diff] [blame] | 1072 | return GenerateLastValueLinear( |
| 1073 | context, loop, info, trip, graph, block, /*is_min=*/true, lower); |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 1074 | } |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 1075 | case HInductionVarAnalysis::kPolynomial: |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1076 | return GenerateLastValuePolynomial(context, loop, info, trip, graph, block, lower); |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 1077 | case HInductionVarAnalysis::kGeometric: |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1078 | return GenerateLastValueGeometric(context, loop, info, trip, graph, block, lower); |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 1079 | case HInductionVarAnalysis::kWrapAround: |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1080 | return GenerateLastValueWrapAround(context, loop, info, trip, graph, block, lower); |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 1081 | case HInductionVarAnalysis::kPeriodic: |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1082 | return GenerateLastValuePeriodic( |
| 1083 | context, loop, info, trip, graph, block, lower, needs_taken_test); |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 1084 | default: |
| 1085 | return false; |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 1086 | } |
| 1087 | } |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 1088 | // Code generation for taken test: generate the code when requested or otherwise analyze |
| 1089 | // if code generation is feasible when taken test is needed. |
| 1090 | if (taken_test != nullptr) { |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1091 | return GenerateCode(context, |
| 1092 | loop, |
| 1093 | trip->op_b, |
| 1094 | /*trip=*/ nullptr, |
| 1095 | graph, |
| 1096 | block, |
| 1097 | /*is_min=*/ false, |
| 1098 | taken_test); |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 1099 | } else if (*needs_taken_test) { |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1100 | if (!GenerateCode(context, |
| 1101 | loop, |
| 1102 | trip->op_b, |
| 1103 | /*trip=*/ nullptr, |
| 1104 | /*graph=*/ nullptr, |
| 1105 | /*block=*/ nullptr, |
| 1106 | /*is_min=*/ false, |
| 1107 | /*result=*/ nullptr)) { |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 1108 | return false; |
| 1109 | } |
| 1110 | } |
| 1111 | // Code generation for lower and upper. |
| 1112 | return |
| 1113 | // Success on lower if invariant (not set), or code can be generated. |
| 1114 | ((info->induction_class == HInductionVarAnalysis::kInvariant) || |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1115 | GenerateCode(context, loop, info, trip, graph, block, /*is_min=*/ true, lower)) && |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 1116 | // And success on upper. |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1117 | GenerateCode(context, loop, info, trip, graph, block, /*is_min=*/ false, upper); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 1118 | } |
| 1119 | |
Santiago Aboy Solanes | 4f18b94 | 2022-07-21 10:31:21 +0100 | [diff] [blame] | 1120 | bool InductionVarRange::GenerateLastValueLinear(const HBasicBlock* context, |
| 1121 | const HLoopInformation* loop, |
| 1122 | HInductionVarAnalysis::InductionInfo* info, |
| 1123 | HInductionVarAnalysis::InductionInfo* trip, |
| 1124 | HGraph* graph, |
| 1125 | HBasicBlock* block, |
| 1126 | bool is_min, |
| 1127 | /*out*/ HInstruction** result) const { |
| 1128 | DataType::Type type = info->type; |
| 1129 | // Avoid any narrowing linear induction or any type mismatch between the linear induction and the |
| 1130 | // trip count expression. |
Santiago Aboy Solanes | db69246 | 2022-08-17 16:34:40 +0100 | [diff] [blame] | 1131 | if (HInductionVarAnalysis::IsNarrowingLinear(info) || trip->type != type) { |
Santiago Aboy Solanes | 4f18b94 | 2022-07-21 10:31:21 +0100 | [diff] [blame] | 1132 | return false; |
| 1133 | } |
| 1134 | |
| 1135 | // Stride value must be a known constant that fits into int32. |
| 1136 | int64_t stride_value = 0; |
| 1137 | if (!IsConstant(context, loop, info->op_a, kExact, &stride_value) || |
| 1138 | !CanLongValueFitIntoInt(stride_value)) { |
| 1139 | return false; |
| 1140 | } |
| 1141 | |
| 1142 | // We require `a` to be a constant value that didn't overflow. |
| 1143 | const bool is_min_a = stride_value >= 0 ? is_min : !is_min; |
| 1144 | Value val_a = GetVal(context, loop, trip, trip, is_min_a); |
| 1145 | HInstruction* opb; |
| 1146 | if (!IsConstantValue(val_a) || |
| 1147 | !GenerateCode(context, loop, info->op_b, trip, graph, block, is_min, &opb)) { |
| 1148 | return false; |
| 1149 | } |
| 1150 | |
| 1151 | if (graph != nullptr) { |
| 1152 | ArenaAllocator* allocator = graph->GetAllocator(); |
| 1153 | HInstruction* oper; |
| 1154 | HInstruction* opa = graph->GetConstant(type, val_a.b_constant); |
| 1155 | if (stride_value == 1) { |
| 1156 | oper = new (allocator) HAdd(type, opa, opb); |
| 1157 | } else if (stride_value == -1) { |
| 1158 | oper = new (graph->GetAllocator()) HSub(type, opb, opa); |
| 1159 | } else { |
| 1160 | HInstruction* mul = new (allocator) HMul(type, graph->GetConstant(type, stride_value), opa); |
| 1161 | oper = new (allocator) HAdd(type, Insert(block, mul), opb); |
| 1162 | } |
| 1163 | *result = Insert(block, oper); |
| 1164 | } |
| 1165 | return true; |
| 1166 | } |
| 1167 | |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1168 | bool InductionVarRange::GenerateLastValuePolynomial(const HBasicBlock* context, |
| 1169 | const HLoopInformation* loop, |
| 1170 | HInductionVarAnalysis::InductionInfo* info, |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 1171 | HInductionVarAnalysis::InductionInfo* trip, |
| 1172 | HGraph* graph, |
| 1173 | HBasicBlock* block, |
| 1174 | /*out*/HInstruction** result) const { |
| 1175 | DCHECK(info != nullptr); |
| 1176 | DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kPolynomial); |
| 1177 | // Detect known coefficients and trip count (always taken). |
| 1178 | int64_t a = 0; |
| 1179 | int64_t b = 0; |
| 1180 | int64_t m = 0; |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1181 | if (IsConstant(context, loop, info->op_a->op_a, kExact, &a) && |
| 1182 | IsConstant(context, loop, info->op_a->op_b, kExact, &b) && |
| 1183 | IsConstant(context, loop, trip->op_a, kExact, &m) && |
| 1184 | m >= 1) { |
Aart Bik | d0a022d | 2016-12-13 11:22:31 -0800 | [diff] [blame] | 1185 | // Evaluate bounds on sum_i=0^m-1(a * i + b) + c for known |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 1186 | // maximum index value m as a * (m * (m-1)) / 2 + b * m + c. |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 1187 | HInstruction* c = nullptr; |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1188 | if (GenerateCode(context, |
| 1189 | loop, |
| 1190 | info->op_b, |
| 1191 | /*trip=*/ nullptr, |
| 1192 | graph, |
| 1193 | block, |
| 1194 | /*is_min=*/ false, |
| 1195 | graph ? &c : nullptr)) { |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 1196 | if (graph != nullptr) { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1197 | DataType::Type type = info->type; |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 1198 | int64_t sum = a * ((m * (m - 1)) / 2) + b * m; |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1199 | if (type != DataType::Type::kInt64) { |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 1200 | sum = static_cast<int32_t>(sum); // okay to truncate |
| 1201 | } |
| 1202 | *result = |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1203 | Insert(block, new (graph->GetAllocator()) HAdd(type, graph->GetConstant(type, sum), c)); |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 1204 | } |
| 1205 | return true; |
| 1206 | } |
| 1207 | } |
| 1208 | return false; |
| 1209 | } |
| 1210 | |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1211 | bool InductionVarRange::GenerateLastValueGeometric(const HBasicBlock* context, |
| 1212 | const HLoopInformation* loop, |
| 1213 | HInductionVarAnalysis::InductionInfo* info, |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 1214 | HInductionVarAnalysis::InductionInfo* trip, |
| 1215 | HGraph* graph, |
| 1216 | HBasicBlock* block, |
| 1217 | /*out*/HInstruction** result) const { |
| 1218 | DCHECK(info != nullptr); |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 1219 | DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kGeometric); |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 1220 | // Detect known base and trip count (always taken). |
| 1221 | int64_t f = 0; |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 1222 | int64_t m = 0; |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1223 | if (IsInt64AndGet(info->fetch, &f) && |
| 1224 | f >= 1 && |
| 1225 | IsConstant(context, loop, trip->op_a, kExact, &m) && |
| 1226 | m >= 1) { |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 1227 | HInstruction* opa = nullptr; |
| 1228 | HInstruction* opb = nullptr; |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1229 | if (GenerateCode( |
| 1230 | context, loop, info->op_a, /*trip=*/ nullptr, graph, block, /*is_min=*/ false, &opa) && |
| 1231 | GenerateCode( |
| 1232 | context, loop, info->op_b, /*trip=*/ nullptr, graph, block, /*is_min=*/ false, &opb)) { |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 1233 | if (graph != nullptr) { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1234 | DataType::Type type = info->type; |
Aart Bik | d3ba626 | 2017-01-30 14:37:12 -0800 | [diff] [blame] | 1235 | // Compute f ^ m for known maximum index value m. |
| 1236 | bool overflow = false; |
| 1237 | int64_t fpow = IntPow(f, m, &overflow); |
| 1238 | if (info->operation == HInductionVarAnalysis::kDiv) { |
| 1239 | // For division, any overflow truncates to zero. |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1240 | if (overflow || (type != DataType::Type::kInt64 && !CanLongValueFitIntoInt(fpow))) { |
Aart Bik | d3ba626 | 2017-01-30 14:37:12 -0800 | [diff] [blame] | 1241 | fpow = 0; |
| 1242 | } |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1243 | } else if (type != DataType::Type::kInt64) { |
Aart Bik | d3ba626 | 2017-01-30 14:37:12 -0800 | [diff] [blame] | 1244 | // For multiplication, okay to truncate to required precision. |
| 1245 | DCHECK(info->operation == HInductionVarAnalysis::kMul); |
| 1246 | fpow = static_cast<int32_t>(fpow); |
| 1247 | } |
| 1248 | // Generate code. |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 1249 | if (fpow == 0) { |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 1250 | // Special case: repeated mul/div always yields zero. |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 1251 | *result = graph->GetConstant(type, 0); |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 1252 | } else { |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 1253 | // Last value: a * f ^ m + b or a * f ^ -m + b. |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 1254 | HInstruction* e = nullptr; |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1255 | ArenaAllocator* allocator = graph->GetAllocator(); |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 1256 | if (info->operation == HInductionVarAnalysis::kMul) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1257 | e = new (allocator) HMul(type, opa, graph->GetConstant(type, fpow)); |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 1258 | } else { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1259 | e = new (allocator) HDiv(type, opa, graph->GetConstant(type, fpow), kNoDexPc); |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 1260 | } |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1261 | *result = Insert(block, new (allocator) HAdd(type, Insert(block, e), opb)); |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 1262 | } |
| 1263 | } |
| 1264 | return true; |
| 1265 | } |
| 1266 | } |
| 1267 | return false; |
| 1268 | } |
| 1269 | |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1270 | bool InductionVarRange::GenerateLastValueWrapAround(const HBasicBlock* context, |
| 1271 | const HLoopInformation* loop, |
| 1272 | HInductionVarAnalysis::InductionInfo* info, |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 1273 | HInductionVarAnalysis::InductionInfo* trip, |
| 1274 | HGraph* graph, |
| 1275 | HBasicBlock* block, |
| 1276 | /*out*/HInstruction** result) const { |
| 1277 | DCHECK(info != nullptr); |
| 1278 | DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kWrapAround); |
| 1279 | // Count depth. |
| 1280 | int32_t depth = 0; |
| 1281 | for (; info->induction_class == HInductionVarAnalysis::kWrapAround; |
| 1282 | info = info->op_b, ++depth) {} |
| 1283 | // Handle wrap(x, wrap(.., y)) if trip count reaches an invariant at end. |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 1284 | // TODO: generalize, but be careful to adjust the terminal. |
| 1285 | int64_t m = 0; |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 1286 | if (info->induction_class == HInductionVarAnalysis::kInvariant && |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1287 | IsConstant(context, loop, trip->op_a, kExact, &m) && |
| 1288 | m >= depth) { |
| 1289 | return GenerateCode( |
| 1290 | context, loop, info, /*trip=*/ nullptr, graph, block, /*is_min=*/ false, result); |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 1291 | } |
| 1292 | return false; |
| 1293 | } |
| 1294 | |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1295 | bool InductionVarRange::GenerateLastValuePeriodic(const HBasicBlock* context, |
| 1296 | const HLoopInformation* loop, |
| 1297 | HInductionVarAnalysis::InductionInfo* info, |
Aart Bik | 9abf894 | 2016-10-14 09:49:42 -0700 | [diff] [blame] | 1298 | HInductionVarAnalysis::InductionInfo* trip, |
| 1299 | HGraph* graph, |
| 1300 | HBasicBlock* block, |
| 1301 | /*out*/HInstruction** result, |
| 1302 | /*out*/bool* needs_taken_test) const { |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 1303 | DCHECK(info != nullptr); |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 1304 | DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kPeriodic); |
Aart Bik | 8523ea1 | 2017-06-01 15:45:09 -0700 | [diff] [blame] | 1305 | // Count period and detect all-invariants. |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 1306 | int64_t period = 1; |
Aart Bik | 8523ea1 | 2017-06-01 15:45:09 -0700 | [diff] [blame] | 1307 | bool all_invariants = true; |
| 1308 | HInductionVarAnalysis::InductionInfo* p = info; |
| 1309 | for (; p->induction_class == HInductionVarAnalysis::kPeriodic; p = p->op_b, ++period) { |
| 1310 | DCHECK_EQ(p->op_a->induction_class, HInductionVarAnalysis::kInvariant); |
| 1311 | if (p->op_a->operation != HInductionVarAnalysis::kFetch) { |
| 1312 | all_invariants = false; |
| 1313 | } |
| 1314 | } |
| 1315 | DCHECK_EQ(p->induction_class, HInductionVarAnalysis::kInvariant); |
| 1316 | if (p->operation != HInductionVarAnalysis::kFetch) { |
| 1317 | all_invariants = false; |
| 1318 | } |
| 1319 | // Don't rely on FP arithmetic to be precise, unless the full period |
| 1320 | // consist of pre-computed expressions only. |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1321 | if (info->type == DataType::Type::kFloat32 || info->type == DataType::Type::kFloat64) { |
Aart Bik | 8523ea1 | 2017-06-01 15:45:09 -0700 | [diff] [blame] | 1322 | if (!all_invariants) { |
| 1323 | return false; |
| 1324 | } |
| 1325 | } |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 1326 | // Handle any periodic(x, periodic(.., y)) for known maximum index value m. |
| 1327 | int64_t m = 0; |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1328 | if (IsConstant(context, loop, trip->op_a, kExact, &m) && m >= 1) { |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 1329 | int64_t li = m % period; |
| 1330 | for (int64_t i = 0; i < li; info = info->op_b, i++) {} |
| 1331 | if (info->induction_class == HInductionVarAnalysis::kPeriodic) { |
| 1332 | info = info->op_a; |
| 1333 | } |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1334 | return GenerateCode( |
| 1335 | context, loop, info, /*trip=*/ nullptr, graph, block, /*is_min=*/ false, result); |
Aart Bik | 9abf894 | 2016-10-14 09:49:42 -0700 | [diff] [blame] | 1336 | } |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 1337 | // Handle periodic(x, y) using even/odd-select on trip count. Enter trip count expression |
| 1338 | // directly to obtain the maximum index value t even if taken test is needed. |
| 1339 | HInstruction* x = nullptr; |
| 1340 | HInstruction* y = nullptr; |
| 1341 | HInstruction* t = nullptr; |
| 1342 | if (period == 2 && |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1343 | GenerateCode(context, |
| 1344 | loop, |
| 1345 | info->op_a, |
| 1346 | /*trip=*/ nullptr, |
| 1347 | graph, |
| 1348 | block, |
| 1349 | /*is_min=*/ false, |
| 1350 | graph ? &x : nullptr) && |
| 1351 | GenerateCode(context, |
| 1352 | loop, |
| 1353 | info->op_b, |
| 1354 | /*trip=*/ nullptr, |
| 1355 | graph, |
| 1356 | block, |
| 1357 | /*is_min=*/ false, |
| 1358 | graph ? &y : nullptr) && |
| 1359 | GenerateCode(context, |
| 1360 | loop, |
| 1361 | trip->op_a, |
| 1362 | /*trip=*/ nullptr, |
| 1363 | graph, |
| 1364 | block, |
| 1365 | /*is_min=*/ false, |
| 1366 | graph ? &t : nullptr)) { |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 1367 | // During actual code generation (graph != nullptr), generate is_even ? x : y. |
Aart Bik | 9abf894 | 2016-10-14 09:49:42 -0700 | [diff] [blame] | 1368 | if (graph != nullptr) { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1369 | DataType::Type type = trip->type; |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1370 | ArenaAllocator* allocator = graph->GetAllocator(); |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 1371 | HInstruction* msk = |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1372 | Insert(block, new (allocator) HAnd(type, t, graph->GetConstant(type, 1))); |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 1373 | HInstruction* is_even = |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1374 | Insert(block, new (allocator) HEqual(msk, graph->GetConstant(type, 0), kNoDexPc)); |
| 1375 | *result = Insert(block, new (graph->GetAllocator()) HSelect(is_even, x, y, kNoDexPc)); |
Aart Bik | 9abf894 | 2016-10-14 09:49:42 -0700 | [diff] [blame] | 1376 | } |
| 1377 | // Guard select with taken test if needed. |
| 1378 | if (*needs_taken_test) { |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 1379 | HInstruction* is_taken = nullptr; |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1380 | if (GenerateCode(context, |
| 1381 | loop, |
| 1382 | trip->op_b, |
| 1383 | /*trip=*/ nullptr, |
| 1384 | graph, |
| 1385 | block, |
| 1386 | /*is_min=*/ false, |
| 1387 | graph ? &is_taken : nullptr)) { |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 1388 | if (graph != nullptr) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1389 | ArenaAllocator* allocator = graph->GetAllocator(); |
| 1390 | *result = Insert(block, new (allocator) HSelect(is_taken, *result, x, kNoDexPc)); |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 1391 | } |
| 1392 | *needs_taken_test = false; // taken care of |
| 1393 | } else { |
Aart Bik | 9abf894 | 2016-10-14 09:49:42 -0700 | [diff] [blame] | 1394 | return false; |
Aart Bik | 9abf894 | 2016-10-14 09:49:42 -0700 | [diff] [blame] | 1395 | } |
Aart Bik | 9abf894 | 2016-10-14 09:49:42 -0700 | [diff] [blame] | 1396 | } |
| 1397 | return true; |
| 1398 | } |
| 1399 | return false; |
| 1400 | } |
| 1401 | |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1402 | bool InductionVarRange::GenerateCode(const HBasicBlock* context, |
| 1403 | const HLoopInformation* loop, |
| 1404 | HInductionVarAnalysis::InductionInfo* info, |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 1405 | HInductionVarAnalysis::InductionInfo* trip, |
| 1406 | HGraph* graph, // when set, code is generated |
| 1407 | HBasicBlock* block, |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1408 | bool is_min, |
| 1409 | /*out*/HInstruction** result) const { |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 1410 | if (info != nullptr) { |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 1411 | // If during codegen, the result is not needed (nullptr), simply return success. |
| 1412 | if (graph != nullptr && result == nullptr) { |
| 1413 | return true; |
| 1414 | } |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 1415 | // Handle current operation. |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 1416 | DataType::Type type = info->type; |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 1417 | HInstruction* opa = nullptr; |
| 1418 | HInstruction* opb = nullptr; |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 1419 | switch (info->induction_class) { |
| 1420 | case HInductionVarAnalysis::kInvariant: |
Aart Bik | 8e02e3e | 2017-02-28 14:41:55 -0800 | [diff] [blame] | 1421 | // Invariants (note that since invariants only have other invariants as |
| 1422 | // sub expressions, viz. no induction, there is no need to adjust is_min). |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 1423 | switch (info->operation) { |
| 1424 | case HInductionVarAnalysis::kAdd: |
Aart Bik | 8e02e3e | 2017-02-28 14:41:55 -0800 | [diff] [blame] | 1425 | case HInductionVarAnalysis::kSub: |
| 1426 | case HInductionVarAnalysis::kMul: |
| 1427 | case HInductionVarAnalysis::kDiv: |
| 1428 | case HInductionVarAnalysis::kRem: |
| 1429 | case HInductionVarAnalysis::kXor: |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 1430 | case HInductionVarAnalysis::kLT: |
| 1431 | case HInductionVarAnalysis::kLE: |
| 1432 | case HInductionVarAnalysis::kGT: |
| 1433 | case HInductionVarAnalysis::kGE: |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1434 | if (GenerateCode(context, loop, info->op_a, trip, graph, block, is_min, &opa) && |
| 1435 | GenerateCode(context, loop, info->op_b, trip, graph, block, is_min, &opb)) { |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 1436 | if (graph != nullptr) { |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 1437 | HInstruction* operation = nullptr; |
| 1438 | switch (info->operation) { |
| 1439 | case HInductionVarAnalysis::kAdd: |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1440 | operation = new (graph->GetAllocator()) HAdd(type, opa, opb); break; |
Aart Bik | 8e02e3e | 2017-02-28 14:41:55 -0800 | [diff] [blame] | 1441 | case HInductionVarAnalysis::kSub: |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1442 | operation = new (graph->GetAllocator()) HSub(type, opa, opb); break; |
Aart Bik | 8e02e3e | 2017-02-28 14:41:55 -0800 | [diff] [blame] | 1443 | case HInductionVarAnalysis::kMul: |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1444 | operation = new (graph->GetAllocator()) HMul(type, opa, opb, kNoDexPc); break; |
Aart Bik | 8e02e3e | 2017-02-28 14:41:55 -0800 | [diff] [blame] | 1445 | case HInductionVarAnalysis::kDiv: |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1446 | operation = new (graph->GetAllocator()) HDiv(type, opa, opb, kNoDexPc); break; |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 1447 | case HInductionVarAnalysis::kRem: |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1448 | operation = new (graph->GetAllocator()) HRem(type, opa, opb, kNoDexPc); break; |
Aart Bik | 9abf894 | 2016-10-14 09:49:42 -0700 | [diff] [blame] | 1449 | case HInductionVarAnalysis::kXor: |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1450 | operation = new (graph->GetAllocator()) HXor(type, opa, opb); break; |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 1451 | case HInductionVarAnalysis::kLT: |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1452 | operation = new (graph->GetAllocator()) HLessThan(opa, opb); break; |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 1453 | case HInductionVarAnalysis::kLE: |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1454 | operation = new (graph->GetAllocator()) HLessThanOrEqual(opa, opb); break; |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 1455 | case HInductionVarAnalysis::kGT: |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1456 | operation = new (graph->GetAllocator()) HGreaterThan(opa, opb); break; |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 1457 | case HInductionVarAnalysis::kGE: |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1458 | operation = new (graph->GetAllocator()) HGreaterThanOrEqual(opa, opb); break; |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 1459 | default: |
| 1460 | LOG(FATAL) << "unknown operation"; |
| 1461 | } |
| 1462 | *result = Insert(block, operation); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 1463 | } |
| 1464 | return true; |
| 1465 | } |
| 1466 | break; |
Aart Bik | 8e02e3e | 2017-02-28 14:41:55 -0800 | [diff] [blame] | 1467 | case HInductionVarAnalysis::kNeg: |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1468 | if (GenerateCode(context, loop, info->op_b, trip, graph, block, !is_min, &opb)) { |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 1469 | if (graph != nullptr) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1470 | *result = Insert(block, new (graph->GetAllocator()) HNeg(type, opb)); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 1471 | } |
| 1472 | return true; |
| 1473 | } |
| 1474 | break; |
| 1475 | case HInductionVarAnalysis::kFetch: |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 1476 | if (graph != nullptr) { |
| 1477 | *result = info->fetch; // already in HIR |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 1478 | } |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 1479 | return true; |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 1480 | case HInductionVarAnalysis::kTripCountInLoop: |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 1481 | case HInductionVarAnalysis::kTripCountInLoopUnsafe: |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1482 | if (UseFullTripCount(context, loop, is_min)) { |
| 1483 | // Generate the full trip count (do not subtract 1 as we do in loop body). |
| 1484 | return GenerateCode( |
| 1485 | context, loop, info->op_a, trip, graph, block, /*is_min=*/ false, result); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 1486 | } |
| 1487 | FALLTHROUGH_INTENDED; |
| 1488 | case HInductionVarAnalysis::kTripCountInBody: |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 1489 | case HInductionVarAnalysis::kTripCountInBodyUnsafe: |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 1490 | if (is_min) { |
| 1491 | if (graph != nullptr) { |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 1492 | *result = graph->GetConstant(type, 0); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 1493 | } |
| 1494 | return true; |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1495 | } else if (IsContextInBody(context, loop)) { |
| 1496 | if (GenerateCode(context, loop, info->op_a, trip, graph, block, is_min, &opb)) { |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 1497 | if (graph != nullptr) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1498 | ArenaAllocator* allocator = graph->GetAllocator(); |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 1499 | *result = |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1500 | Insert(block, new (allocator) HSub(type, opb, graph->GetConstant(type, 1))); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 1501 | } |
| 1502 | return true; |
| 1503 | } |
| 1504 | } |
| 1505 | break; |
Aart Bik | 8e02e3e | 2017-02-28 14:41:55 -0800 | [diff] [blame] | 1506 | case HInductionVarAnalysis::kNop: |
| 1507 | LOG(FATAL) << "unexpected invariant nop"; |
| 1508 | } // switch invariant operation |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 1509 | break; |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 1510 | case HInductionVarAnalysis::kLinear: { |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 1511 | // Linear induction a * i + b, for normalized 0 <= i < TC. For ranges, this should |
| 1512 | // be restricted to a unit stride to avoid arithmetic wrap-around situations that |
| 1513 | // are harder to guard against. For a last value, requesting min/max based on any |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 1514 | // known stride yields right value. Always avoid any narrowing linear induction or |
| 1515 | // any type mismatch between the linear induction and the trip count expression. |
| 1516 | // TODO: careful runtime type conversions could generalize this latter restriction. |
| 1517 | if (!HInductionVarAnalysis::IsNarrowingLinear(info) && trip->type == type) { |
| 1518 | int64_t stride_value = 0; |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1519 | if (IsConstant(context, loop, info->op_a, kExact, &stride_value) && |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 1520 | CanLongValueFitIntoInt(stride_value)) { |
| 1521 | const bool is_min_a = stride_value >= 0 ? is_min : !is_min; |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1522 | if (GenerateCode(context, loop, trip, trip, graph, block, is_min_a, &opa) && |
| 1523 | GenerateCode(context, loop, info->op_b, trip, graph, block, is_min, &opb)) { |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 1524 | if (graph != nullptr) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1525 | ArenaAllocator* allocator = graph->GetAllocator(); |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 1526 | HInstruction* oper; |
| 1527 | if (stride_value == 1) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1528 | oper = new (allocator) HAdd(type, opa, opb); |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 1529 | } else if (stride_value == -1) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1530 | oper = new (graph->GetAllocator()) HSub(type, opb, opa); |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 1531 | } else { |
| 1532 | HInstruction* mul = |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1533 | new (allocator) HMul(type, graph->GetConstant(type, stride_value), opa); |
| 1534 | oper = new (allocator) HAdd(type, Insert(block, mul), opb); |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 1535 | } |
| 1536 | *result = Insert(block, oper); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 1537 | } |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 1538 | return true; |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 1539 | } |
| 1540 | } |
| 1541 | } |
| 1542 | break; |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 1543 | } |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 1544 | case HInductionVarAnalysis::kPolynomial: |
| 1545 | case HInductionVarAnalysis::kGeometric: |
| 1546 | break; |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 1547 | case HInductionVarAnalysis::kWrapAround: |
| 1548 | case HInductionVarAnalysis::kPeriodic: { |
| 1549 | // Wrap-around and periodic inductions are restricted to constants only, so that extreme |
| 1550 | // values are easy to test at runtime without complications of arithmetic wrap-around. |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 1551 | Value extreme = GetVal(context, loop, info, trip, is_min); |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 1552 | if (IsConstantValue(extreme)) { |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 1553 | if (graph != nullptr) { |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 1554 | *result = graph->GetConstant(type, extreme.b_constant); |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 1555 | } |
| 1556 | return true; |
| 1557 | } |
| 1558 | break; |
| 1559 | } |
Aart Bik | 8e02e3e | 2017-02-28 14:41:55 -0800 | [diff] [blame] | 1560 | } // switch induction class |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 1561 | } |
| 1562 | return false; |
| 1563 | } |
| 1564 | |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 1565 | void InductionVarRange::ReplaceInduction(HInductionVarAnalysis::InductionInfo* info, |
| 1566 | HInstruction* fetch, |
| 1567 | HInstruction* replacement) { |
| 1568 | if (info != nullptr) { |
| 1569 | if (info->induction_class == HInductionVarAnalysis::kInvariant && |
| 1570 | info->operation == HInductionVarAnalysis::kFetch && |
| 1571 | info->fetch == fetch) { |
| 1572 | info->fetch = replacement; |
| 1573 | } |
| 1574 | ReplaceInduction(info->op_a, fetch, replacement); |
| 1575 | ReplaceInduction(info->op_b, fetch, replacement); |
| 1576 | } |
| 1577 | } |
| 1578 | |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 1579 | } // namespace art |