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> |
| 20 | |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 21 | namespace art { |
| 22 | |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 23 | /** Returns true if 64-bit constant fits in 32-bit constant. */ |
| 24 | static bool CanLongValueFitIntoInt(int64_t c) { |
Aart Bik | cd26feb | 2015-09-23 17:50:50 -0700 | [diff] [blame] | 25 | 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] | 26 | } |
| 27 | |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 28 | /** Returns true if 32-bit addition can be done safely. */ |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 29 | static bool IsSafeAdd(int32_t c1, int32_t c2) { |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 30 | return CanLongValueFitIntoInt(static_cast<int64_t>(c1) + static_cast<int64_t>(c2)); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 31 | } |
| 32 | |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 33 | /** Returns true if 32-bit subtraction can be done safely. */ |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 34 | static bool IsSafeSub(int32_t c1, int32_t c2) { |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 35 | return CanLongValueFitIntoInt(static_cast<int64_t>(c1) - static_cast<int64_t>(c2)); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 36 | } |
| 37 | |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 38 | /** Returns true if 32-bit multiplication can be done safely. */ |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 39 | static bool IsSafeMul(int32_t c1, int32_t c2) { |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 40 | return CanLongValueFitIntoInt(static_cast<int64_t>(c1) * static_cast<int64_t>(c2)); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 41 | } |
| 42 | |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 43 | /** Returns true if 32-bit division can be done safely. */ |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 44 | static bool IsSafeDiv(int32_t c1, int32_t c2) { |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 45 | 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] | 46 | } |
| 47 | |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 48 | /** Returns true for 32/64-bit constant instruction. */ |
| 49 | static bool IsIntAndGet(HInstruction* instruction, int64_t* value) { |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 50 | if (instruction->IsIntConstant()) { |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 51 | *value = instruction->AsIntConstant()->GetValue(); |
| 52 | return true; |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 53 | } else if (instruction->IsLongConstant()) { |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 54 | *value = instruction->AsLongConstant()->GetValue(); |
| 55 | return true; |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 56 | } |
| 57 | return false; |
| 58 | } |
| 59 | |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 60 | /** |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 61 | * An upper bound a * (length / a) + b, where a >= 1, can be conservatively rewritten as length + b |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 62 | * because length >= 0 is true. This makes it more likely the bound is useful to clients. |
| 63 | */ |
| 64 | static InductionVarRange::Value SimplifyMax(InductionVarRange::Value v) { |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 65 | int64_t value; |
| 66 | if (v.is_known && |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 67 | v.a_constant >= 1 && |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 68 | v.instruction->IsDiv() && |
| 69 | v.instruction->InputAt(0)->IsArrayLength() && |
| 70 | IsIntAndGet(v.instruction->InputAt(1), &value) && v.a_constant == value) { |
| 71 | return InductionVarRange::Value(v.instruction->InputAt(0), 1, v.b_constant); |
| 72 | } |
| 73 | return v; |
| 74 | } |
| 75 | |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 76 | /** Helper method to test for a constant value. */ |
| 77 | static bool IsConstantValue(InductionVarRange::Value v) { |
| 78 | return v.is_known && v.a_constant == 0; |
| 79 | } |
| 80 | |
| 81 | /** Corrects a value for type to account for arithmetic wrap-around in lower precision. */ |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 82 | static InductionVarRange::Value CorrectForType(InductionVarRange::Value v, Primitive::Type type) { |
| 83 | switch (type) { |
| 84 | case Primitive::kPrimShort: |
| 85 | case Primitive::kPrimChar: |
| 86 | case Primitive::kPrimByte: { |
| 87 | // Constants within range only. |
| 88 | // TODO: maybe some room for improvement, like allowing widening conversions |
| 89 | const int32_t min = Primitive::MinValueOfIntegralType(type); |
| 90 | const int32_t max = Primitive::MaxValueOfIntegralType(type); |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 91 | return (IsConstantValue(v) && min <= v.b_constant && v.b_constant <= max) |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 92 | ? v |
| 93 | : InductionVarRange::Value(); |
| 94 | } |
| 95 | default: |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 96 | return v; |
| 97 | } |
| 98 | } |
| 99 | |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 100 | /** Helper method to insert an instruction. */ |
| 101 | static HInstruction* Insert(HBasicBlock* block, HInstruction* instruction) { |
| 102 | DCHECK(block != nullptr); |
| 103 | DCHECK(block->GetLastInstruction() != nullptr) << block->GetBlockId(); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 104 | DCHECK(instruction != nullptr); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 105 | block->InsertInstructionBefore(instruction, block->GetLastInstruction()); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 106 | return instruction; |
| 107 | } |
| 108 | |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 109 | // |
| 110 | // Public class methods. |
| 111 | // |
| 112 | |
| 113 | InductionVarRange::InductionVarRange(HInductionVarAnalysis* induction_analysis) |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 114 | : induction_analysis_(induction_analysis), |
| 115 | chase_hint_(nullptr) { |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 116 | DCHECK(induction_analysis != nullptr); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Aart Bik | 1fc3afb | 2016-02-02 13:26:16 -0800 | [diff] [blame] | 119 | bool InductionVarRange::GetInductionRange(HInstruction* context, |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 120 | HInstruction* instruction, |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 121 | HInstruction* chase_hint, |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 122 | /*out*/Value* min_val, |
| 123 | /*out*/Value* max_val, |
| 124 | /*out*/bool* needs_finite_test) { |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 125 | HLoopInformation* loop = nullptr; |
| 126 | HInductionVarAnalysis::InductionInfo* info = nullptr; |
| 127 | HInductionVarAnalysis::InductionInfo* trip = nullptr; |
| 128 | if (!HasInductionInfo(context, instruction, &loop, &info, &trip)) { |
| 129 | return false; |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 130 | } |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 131 | // Type int or lower (this is not too restrictive since intended clients, like |
| 132 | // bounds check elimination, will have truncated higher precision induction |
| 133 | // at their use point already). |
| 134 | switch (info->type) { |
| 135 | case Primitive::kPrimInt: |
| 136 | case Primitive::kPrimShort: |
| 137 | case Primitive::kPrimChar: |
| 138 | case Primitive::kPrimByte: |
| 139 | break; |
| 140 | default: |
| 141 | return false; |
| 142 | } |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 143 | // Find range. |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 144 | chase_hint_ = chase_hint; |
| 145 | bool in_body = context->GetBlock() != loop->GetHeader(); |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 146 | int64_t stride_value = 0; |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 147 | *min_val = GetVal(info, trip, in_body, /* is_min */ true); |
| 148 | *max_val = SimplifyMax(GetVal(info, trip, in_body, /* is_min */ false)); |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 149 | *needs_finite_test = NeedsTripCount(info, &stride_value) && IsUnsafeTripCount(trip); |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 150 | return true; |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 153 | bool InductionVarRange::CanGenerateRange(HInstruction* context, |
| 154 | HInstruction* instruction, |
| 155 | /*out*/bool* needs_finite_test, |
| 156 | /*out*/bool* needs_taken_test) { |
| 157 | bool is_last_value = false; |
| 158 | int64_t stride_value = 0; |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 159 | return GenerateCode(context, |
| 160 | instruction, |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 161 | is_last_value, |
| 162 | nullptr, |
| 163 | nullptr, |
| 164 | nullptr, |
| 165 | nullptr, |
| 166 | nullptr, // nothing generated yet |
| 167 | &stride_value, |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 168 | needs_finite_test, |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 169 | needs_taken_test) |
| 170 | && (stride_value == -1 || |
| 171 | stride_value == 0 || |
| 172 | stride_value == 1); // avoid wrap-around anomalies. |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 175 | void InductionVarRange::GenerateRange(HInstruction* context, |
| 176 | HInstruction* instruction, |
| 177 | HGraph* graph, |
| 178 | HBasicBlock* block, |
| 179 | /*out*/HInstruction** lower, |
| 180 | /*out*/HInstruction** upper) { |
| 181 | bool is_last_value = false; |
| 182 | int64_t s = 0; |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 183 | bool b1, b2; // unused |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 184 | if (!GenerateCode(context, |
| 185 | instruction, |
| 186 | is_last_value, |
| 187 | graph, |
| 188 | block, |
| 189 | lower, |
| 190 | upper, |
| 191 | nullptr, |
| 192 | &s, |
| 193 | &b1, |
| 194 | &b2)) { |
| 195 | LOG(FATAL) << "Failed precondition: CanGenerateRange()"; |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 199 | HInstruction* InductionVarRange::GenerateTakenTest(HInstruction* context, |
| 200 | HGraph* graph, |
| 201 | HBasicBlock* block) { |
| 202 | HInstruction* taken_test = nullptr; |
| 203 | bool is_last_value = false; |
| 204 | int64_t stride_value = 0; |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 205 | bool b1, b2; // unused |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 206 | if (!GenerateCode(context, |
| 207 | context, |
| 208 | is_last_value, |
| 209 | graph, |
| 210 | block, |
| 211 | nullptr, |
| 212 | nullptr, |
| 213 | &taken_test, |
| 214 | &stride_value, |
| 215 | &b1, |
| 216 | &b2)) { |
| 217 | LOG(FATAL) << "Failed precondition: CanGenerateRange()"; |
| 218 | } |
| 219 | return taken_test; |
| 220 | } |
| 221 | |
| 222 | bool InductionVarRange::CanGenerateLastValue(HInstruction* instruction) { |
| 223 | bool is_last_value = true; |
| 224 | int64_t stride_value = 0; |
| 225 | bool needs_finite_test = false; |
| 226 | bool needs_taken_test = false; |
| 227 | return GenerateCode(instruction, |
| 228 | instruction, |
| 229 | is_last_value, |
| 230 | nullptr, |
| 231 | nullptr, |
| 232 | nullptr, |
| 233 | nullptr, |
| 234 | nullptr, // nothing generated yet |
| 235 | &stride_value, &needs_finite_test, &needs_taken_test) |
| 236 | && !needs_finite_test && !needs_taken_test; |
| 237 | } |
| 238 | |
| 239 | HInstruction* InductionVarRange::GenerateLastValue(HInstruction* instruction, |
| 240 | HGraph* graph, |
| 241 | HBasicBlock* block) { |
| 242 | HInstruction* last_value = nullptr; |
| 243 | bool is_last_value = true; |
| 244 | int64_t stride_value = 0; |
| 245 | bool b1, b2; // unused |
| 246 | if (!GenerateCode(instruction, |
| 247 | instruction, |
| 248 | is_last_value, |
| 249 | graph, |
| 250 | block, |
| 251 | &last_value, |
| 252 | &last_value, |
| 253 | nullptr, |
| 254 | &stride_value, |
| 255 | &b1, |
| 256 | &b2)) { |
| 257 | LOG(FATAL) << "Failed precondition: CanGenerateLastValue()"; |
| 258 | } |
| 259 | return last_value; |
| 260 | } |
| 261 | |
| 262 | void InductionVarRange::Replace(HInstruction* instruction, |
| 263 | HInstruction* fetch, |
| 264 | HInstruction* replacement) { |
| 265 | for (HLoopInformation* lp = instruction->GetBlock()->GetLoopInformation(); // closest enveloping loop |
| 266 | lp != nullptr; |
| 267 | lp = lp->GetPreHeader()->GetLoopInformation()) { |
| 268 | ReplaceInduction(induction_analysis_->LookupInfo(lp, instruction), fetch, replacement); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 269 | } |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 270 | } |
| 271 | |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 272 | // |
| 273 | // Private class methods. |
| 274 | // |
| 275 | |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 276 | bool InductionVarRange::IsConstant(HInductionVarAnalysis::InductionInfo* info, |
| 277 | ConstantRequest request, |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 278 | /*out*/ int64_t* value) const { |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 279 | if (info != nullptr) { |
| 280 | // A direct 32-bit or 64-bit constant fetch. This immediately satisfies |
| 281 | // any of the three requests (kExact, kAtMost, and KAtLeast). |
| 282 | if (info->induction_class == HInductionVarAnalysis::kInvariant && |
| 283 | info->operation == HInductionVarAnalysis::kFetch) { |
| 284 | if (IsIntAndGet(info->fetch, value)) { |
| 285 | return true; |
| 286 | } |
| 287 | } |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 288 | // Try range analysis on the invariant, but only on proper range to avoid wrap-around anomalies. |
| 289 | Value min_val = GetVal(info, nullptr, /* in_body */ true, /* is_min */ true); |
| 290 | Value max_val = GetVal(info, nullptr, /* in_body */ true, /* is_min */ false); |
| 291 | if (IsConstantValue(min_val) && |
| 292 | IsConstantValue(max_val) && min_val.b_constant <= max_val.b_constant) { |
| 293 | if ((request == kExact && min_val.b_constant == max_val.b_constant) || request == kAtMost) { |
| 294 | *value = max_val.b_constant; |
| 295 | return true; |
| 296 | } else if (request == kAtLeast) { |
| 297 | *value = min_val.b_constant; |
Aart Bik | 358af83 | 2016-02-24 14:17:53 -0800 | [diff] [blame] | 298 | return true; |
| 299 | } |
| 300 | } |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 301 | } |
| 302 | return false; |
| 303 | } |
| 304 | |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 305 | bool InductionVarRange::HasInductionInfo( |
| 306 | HInstruction* context, |
| 307 | HInstruction* instruction, |
| 308 | /*out*/ HLoopInformation** loop, |
| 309 | /*out*/ HInductionVarAnalysis::InductionInfo** info, |
| 310 | /*out*/ HInductionVarAnalysis::InductionInfo** trip) const { |
| 311 | HLoopInformation* l = context->GetBlock()->GetLoopInformation(); // closest enveloping loop |
| 312 | if (l != nullptr) { |
| 313 | HInductionVarAnalysis::InductionInfo* i = induction_analysis_->LookupInfo(l, instruction); |
| 314 | if (i != nullptr) { |
| 315 | *loop = l; |
| 316 | *info = i; |
| 317 | *trip = induction_analysis_->LookupInfo(l, l->GetHeader()->GetLastInstruction()); |
| 318 | return true; |
| 319 | } |
| 320 | } |
| 321 | return false; |
| 322 | } |
| 323 | |
| 324 | bool InductionVarRange::IsWellBehavedTripCount(HInductionVarAnalysis::InductionInfo* trip) const { |
| 325 | if (trip != nullptr) { |
| 326 | // Both bounds that define a trip-count are well-behaved if they either are not defined |
| 327 | // in any loop, or are contained in a proper interval. This allows finding the min/max |
| 328 | // of an expression by chasing outward. |
| 329 | InductionVarRange range(induction_analysis_); |
| 330 | HInductionVarAnalysis::InductionInfo* lower = trip->op_b->op_a; |
| 331 | HInductionVarAnalysis::InductionInfo* upper = trip->op_b->op_b; |
| 332 | int64_t not_used = 0; |
| 333 | return (!HasFetchInLoop(lower) || range.IsConstant(lower, kAtLeast, ¬_used)) && |
| 334 | (!HasFetchInLoop(upper) || range.IsConstant(upper, kAtLeast, ¬_used)); |
| 335 | } |
| 336 | return true; |
| 337 | } |
| 338 | |
| 339 | bool InductionVarRange::HasFetchInLoop(HInductionVarAnalysis::InductionInfo* info) const { |
| 340 | if (info != nullptr) { |
| 341 | if (info->induction_class == HInductionVarAnalysis::kInvariant && |
| 342 | info->operation == HInductionVarAnalysis::kFetch) { |
| 343 | return info->fetch->GetBlock()->GetLoopInformation() != nullptr; |
| 344 | } |
| 345 | return HasFetchInLoop(info->op_a) || HasFetchInLoop(info->op_b); |
| 346 | } |
| 347 | return false; |
| 348 | } |
| 349 | |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 350 | bool InductionVarRange::NeedsTripCount(HInductionVarAnalysis::InductionInfo* info, |
| 351 | int64_t* stride_value) const { |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 352 | if (info != nullptr) { |
| 353 | if (info->induction_class == HInductionVarAnalysis::kLinear) { |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 354 | return IsConstant(info->op_a, kExact, stride_value); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 355 | } else if (info->induction_class == HInductionVarAnalysis::kWrapAround) { |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 356 | return NeedsTripCount(info->op_b, stride_value); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 357 | } |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 358 | } |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 359 | return false; |
| 360 | } |
| 361 | |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 362 | bool InductionVarRange::IsBodyTripCount(HInductionVarAnalysis::InductionInfo* trip) const { |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 363 | if (trip != nullptr) { |
| 364 | if (trip->induction_class == HInductionVarAnalysis::kInvariant) { |
| 365 | return trip->operation == HInductionVarAnalysis::kTripCountInBody || |
| 366 | trip->operation == HInductionVarAnalysis::kTripCountInBodyUnsafe; |
| 367 | } |
| 368 | } |
| 369 | return false; |
| 370 | } |
| 371 | |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 372 | bool InductionVarRange::IsUnsafeTripCount(HInductionVarAnalysis::InductionInfo* trip) const { |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 373 | if (trip != nullptr) { |
| 374 | if (trip->induction_class == HInductionVarAnalysis::kInvariant) { |
| 375 | return trip->operation == HInductionVarAnalysis::kTripCountInBodyUnsafe || |
| 376 | trip->operation == HInductionVarAnalysis::kTripCountInLoopUnsafe; |
| 377 | } |
| 378 | } |
| 379 | return false; |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 380 | } |
| 381 | |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 382 | InductionVarRange::Value InductionVarRange::GetLinear(HInductionVarAnalysis::InductionInfo* info, |
| 383 | HInductionVarAnalysis::InductionInfo* trip, |
| 384 | bool in_body, |
| 385 | bool is_min) const { |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 386 | // 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] | 387 | // analysis (finding max a * (TC - 1) + OFFSET for a == 1 and TC = UPPER - OFFSET or finding |
| 388 | // min a * (TC - 1) + OFFSET for a == -1 and TC = OFFSET - UPPER) to avoid losing information |
| 389 | // with intermediate results that only incorporate single instructions. |
| 390 | if (trip != nullptr) { |
| 391 | HInductionVarAnalysis::InductionInfo* trip_expr = trip->op_a; |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 392 | if (trip_expr->type == info->type && trip_expr->operation == HInductionVarAnalysis::kSub) { |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 393 | int64_t stride_value = 0; |
| 394 | if (IsConstant(info->op_a, kExact, &stride_value)) { |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 395 | if (!is_min && stride_value == 1) { |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 396 | // 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] | 397 | if (HInductionVarAnalysis::InductionEqual(trip_expr->op_b, info->op_b)) { |
| 398 | // Analyze cancelled trip with just the positive operand (trip_expr->op_a). |
| 399 | HInductionVarAnalysis::InductionInfo cancelled_trip( |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 400 | trip->induction_class, |
| 401 | trip->operation, |
| 402 | trip_expr->op_a, |
| 403 | trip->op_b, |
| 404 | nullptr, |
| 405 | trip->type); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 406 | return GetVal(&cancelled_trip, trip, in_body, is_min); |
| 407 | } |
| 408 | } else if (is_min && stride_value == -1) { |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 409 | // 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] | 410 | if (HInductionVarAnalysis::InductionEqual(trip_expr->op_a, info->op_b)) { |
| 411 | // Analyze cancelled trip with just the negative operand (trip_expr->op_b). |
| 412 | HInductionVarAnalysis::InductionInfo neg( |
| 413 | HInductionVarAnalysis::kInvariant, |
| 414 | HInductionVarAnalysis::kNeg, |
| 415 | nullptr, |
| 416 | trip_expr->op_b, |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 417 | nullptr, |
| 418 | trip->type); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 419 | HInductionVarAnalysis::InductionInfo cancelled_trip( |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 420 | trip->induction_class, trip->operation, &neg, trip->op_b, nullptr, trip->type); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 421 | return SubValue(Value(0), GetVal(&cancelled_trip, trip, in_body, !is_min)); |
| 422 | } |
| 423 | } |
| 424 | } |
| 425 | } |
| 426 | } |
| 427 | // General rule of linear induction a * i + b, for normalized 0 <= i < TC. |
| 428 | return AddValue(GetMul(info->op_a, trip, trip, in_body, is_min), |
| 429 | GetVal(info->op_b, trip, in_body, is_min)); |
| 430 | } |
| 431 | |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 432 | InductionVarRange::Value InductionVarRange::GetFetch(HInstruction* instruction, |
Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 433 | HInductionVarAnalysis::InductionInfo* trip, |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 434 | bool in_body, |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 435 | bool is_min) const { |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 436 | // Stop chasing the instruction at constant or hint. |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 437 | int64_t value; |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 438 | if (IsIntAndGet(instruction, &value) && CanLongValueFitIntoInt(value)) { |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 439 | return Value(static_cast<int32_t>(value)); |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 440 | } else if (instruction == chase_hint_) { |
| 441 | return Value(instruction, 1, 0); |
| 442 | } |
| 443 | // Special cases when encountering a single instruction that denotes trip count in the |
| 444 | // loop-body: min is 1 and, when chasing constants, max of safe trip-count is max int |
| 445 | if (in_body && trip != nullptr && instruction == trip->op_a->fetch) { |
| 446 | if (is_min) { |
| 447 | return Value(1); |
| 448 | } else if (chase_hint_ == nullptr && !IsUnsafeTripCount(trip)) { |
| 449 | return Value(std::numeric_limits<int32_t>::max()); |
| 450 | } |
| 451 | } |
| 452 | // Chase the instruction a bit deeper into the HIR tree, so that it becomes more likely |
| 453 | // range analysis will compare the same instructions as terminal nodes. |
| 454 | if (instruction->IsAdd()) { |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 455 | if (IsIntAndGet(instruction->InputAt(0), &value) && CanLongValueFitIntoInt(value)) { |
| 456 | return AddValue(Value(static_cast<int32_t>(value)), |
| 457 | GetFetch(instruction->InputAt(1), trip, in_body, is_min)); |
| 458 | } else if (IsIntAndGet(instruction->InputAt(1), &value) && CanLongValueFitIntoInt(value)) { |
| 459 | return AddValue(GetFetch(instruction->InputAt(0), trip, in_body, is_min), |
| 460 | Value(static_cast<int32_t>(value))); |
Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 461 | } |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 462 | } else if (instruction->IsArrayLength()) { |
| 463 | // Return extreme values when chasing constants. Otherwise, chase deeper. |
| 464 | if (chase_hint_ == nullptr) { |
| 465 | return is_min ? Value(0) : Value(std::numeric_limits<int32_t>::max()); |
| 466 | } else if (instruction->InputAt(0)->IsNewArray()) { |
| 467 | return GetFetch(instruction->InputAt(0)->InputAt(0), trip, in_body, is_min); |
| 468 | } |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 469 | } else if (instruction->IsTypeConversion()) { |
| 470 | // Since analysis is 32-bit (or narrower) we allow a widening along the path. |
| 471 | if (instruction->AsTypeConversion()->GetInputType() == Primitive::kPrimInt && |
| 472 | instruction->AsTypeConversion()->GetResultType() == Primitive::kPrimLong) { |
| 473 | return GetFetch(instruction->InputAt(0), trip, in_body, is_min); |
| 474 | } |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 475 | } |
| 476 | // Chase an invariant fetch that is defined by an outer loop if the trip-count used |
| 477 | // so far is well-behaved in both bounds and the next trip-count is safe. |
| 478 | // Example: |
| 479 | // for (int i = 0; i <= 100; i++) // safe |
| 480 | // for (int j = 0; j <= i; j++) // well-behaved |
| 481 | // j is in range [0, i ] (if i is chase hint) |
| 482 | // or in range [0, 100] (otherwise) |
| 483 | HLoopInformation* next_loop = nullptr; |
| 484 | HInductionVarAnalysis::InductionInfo* next_info = nullptr; |
| 485 | HInductionVarAnalysis::InductionInfo* next_trip = nullptr; |
| 486 | bool next_in_body = true; // inner loop is always in body of outer loop |
| 487 | if (HasInductionInfo(instruction, instruction, &next_loop, &next_info, &next_trip) && |
| 488 | IsWellBehavedTripCount(trip) && |
| 489 | !IsUnsafeTripCount(next_trip)) { |
| 490 | return GetVal(next_info, next_trip, next_in_body, is_min); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 491 | } |
| 492 | return Value(instruction, 1, 0); |
| 493 | } |
| 494 | |
Aart Bik | cd26feb | 2015-09-23 17:50:50 -0700 | [diff] [blame] | 495 | InductionVarRange::Value InductionVarRange::GetVal(HInductionVarAnalysis::InductionInfo* info, |
| 496 | HInductionVarAnalysis::InductionInfo* trip, |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 497 | bool in_body, |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 498 | bool is_min) const { |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 499 | if (info != nullptr) { |
| 500 | switch (info->induction_class) { |
| 501 | case HInductionVarAnalysis::kInvariant: |
| 502 | // Invariants. |
| 503 | switch (info->operation) { |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 504 | case HInductionVarAnalysis::kAdd: |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 505 | return AddValue(GetVal(info->op_a, trip, in_body, is_min), |
| 506 | GetVal(info->op_b, trip, in_body, is_min)); |
Aart Bik | cd26feb | 2015-09-23 17:50:50 -0700 | [diff] [blame] | 507 | case HInductionVarAnalysis::kSub: // second reversed! |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 508 | return SubValue(GetVal(info->op_a, trip, in_body, is_min), |
| 509 | GetVal(info->op_b, trip, in_body, !is_min)); |
Aart Bik | cd26feb | 2015-09-23 17:50:50 -0700 | [diff] [blame] | 510 | case HInductionVarAnalysis::kNeg: // second reversed! |
| 511 | return SubValue(Value(0), |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 512 | GetVal(info->op_b, trip, in_body, !is_min)); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 513 | case HInductionVarAnalysis::kMul: |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 514 | return GetMul(info->op_a, info->op_b, trip, in_body, is_min); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 515 | case HInductionVarAnalysis::kDiv: |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 516 | return GetDiv(info->op_a, info->op_b, trip, in_body, is_min); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 517 | case HInductionVarAnalysis::kFetch: |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 518 | return GetFetch(info->fetch, trip, in_body, is_min); |
| 519 | case HInductionVarAnalysis::kTripCountInLoop: |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 520 | case HInductionVarAnalysis::kTripCountInLoopUnsafe: |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 521 | if (!in_body && !is_min) { // one extra! |
Aart Bik | 22f0587 | 2015-10-27 15:56:28 -0700 | [diff] [blame] | 522 | return GetVal(info->op_a, trip, in_body, is_min); |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 523 | } |
| 524 | FALLTHROUGH_INTENDED; |
| 525 | case HInductionVarAnalysis::kTripCountInBody: |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 526 | case HInductionVarAnalysis::kTripCountInBodyUnsafe: |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 527 | if (is_min) { |
| 528 | return Value(0); |
| 529 | } else if (in_body) { |
Aart Bik | 22f0587 | 2015-10-27 15:56:28 -0700 | [diff] [blame] | 530 | return SubValue(GetVal(info->op_a, trip, in_body, is_min), Value(1)); |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 531 | } |
| 532 | break; |
| 533 | default: |
| 534 | break; |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 535 | } |
| 536 | break; |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 537 | case HInductionVarAnalysis::kLinear: |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 538 | return CorrectForType(GetLinear(info, trip, in_body, is_min), info->type); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 539 | case HInductionVarAnalysis::kWrapAround: |
| 540 | case HInductionVarAnalysis::kPeriodic: |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 541 | return MergeVal(GetVal(info->op_a, trip, in_body, is_min), |
| 542 | GetVal(info->op_b, trip, in_body, is_min), is_min); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 543 | } |
| 544 | } |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 545 | return Value(); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | InductionVarRange::Value InductionVarRange::GetMul(HInductionVarAnalysis::InductionInfo* info1, |
| 549 | HInductionVarAnalysis::InductionInfo* info2, |
| 550 | HInductionVarAnalysis::InductionInfo* trip, |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 551 | bool in_body, |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 552 | bool is_min) const { |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 553 | // Constant times range. |
| 554 | int64_t value = 0; |
| 555 | if (IsConstant(info1, kExact, &value)) { |
| 556 | return MulRangeAndConstant(value, info2, trip, in_body, is_min); |
| 557 | } else if (IsConstant(info2, kExact, &value)) { |
| 558 | return MulRangeAndConstant(value, info1, trip, in_body, is_min); |
| 559 | } |
| 560 | // Interval ranges. |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 561 | Value v1_min = GetVal(info1, trip, in_body, /* is_min */ true); |
| 562 | Value v1_max = GetVal(info1, trip, in_body, /* is_min */ false); |
| 563 | Value v2_min = GetVal(info2, trip, in_body, /* is_min */ true); |
| 564 | Value v2_max = GetVal(info2, trip, in_body, /* is_min */ false); |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 565 | // Positive range vs. positive or negative range. |
| 566 | if (IsConstantValue(v1_min) && v1_min.b_constant >= 0) { |
| 567 | if (IsConstantValue(v2_min) && v2_min.b_constant >= 0) { |
| 568 | return is_min ? MulValue(v1_min, v2_min) : MulValue(v1_max, v2_max); |
| 569 | } else if (IsConstantValue(v2_max) && v2_max.b_constant <= 0) { |
| 570 | 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] | 571 | } |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 572 | } |
| 573 | // Negative range vs. positive or negative range. |
| 574 | if (IsConstantValue(v1_max) && v1_max.b_constant <= 0) { |
| 575 | if (IsConstantValue(v2_min) && v2_min.b_constant >= 0) { |
| 576 | return is_min ? MulValue(v1_min, v2_max) : MulValue(v1_max, v2_min); |
| 577 | } else if (IsConstantValue(v2_max) && v2_max.b_constant <= 0) { |
| 578 | 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] | 579 | } |
| 580 | } |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 581 | return Value(); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | InductionVarRange::Value InductionVarRange::GetDiv(HInductionVarAnalysis::InductionInfo* info1, |
| 585 | HInductionVarAnalysis::InductionInfo* info2, |
| 586 | HInductionVarAnalysis::InductionInfo* trip, |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 587 | bool in_body, |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 588 | bool is_min) const { |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 589 | // Range divided by constant. |
| 590 | int64_t value = 0; |
| 591 | if (IsConstant(info2, kExact, &value)) { |
| 592 | return DivRangeAndConstant(value, info1, trip, in_body, is_min); |
| 593 | } |
| 594 | // Interval ranges. |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 595 | Value v1_min = GetVal(info1, trip, in_body, /* is_min */ true); |
| 596 | Value v1_max = GetVal(info1, trip, in_body, /* is_min */ false); |
| 597 | Value v2_min = GetVal(info2, trip, in_body, /* is_min */ true); |
| 598 | Value v2_max = GetVal(info2, trip, in_body, /* is_min */ false); |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 599 | // Positive range vs. positive or negative range. |
| 600 | if (IsConstantValue(v1_min) && v1_min.b_constant >= 0) { |
| 601 | if (IsConstantValue(v2_min) && v2_min.b_constant >= 0) { |
| 602 | return is_min ? DivValue(v1_min, v2_max) : DivValue(v1_max, v2_min); |
| 603 | } else if (IsConstantValue(v2_max) && v2_max.b_constant <= 0) { |
| 604 | 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] | 605 | } |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 606 | } |
| 607 | // Negative range vs. positive or negative range. |
| 608 | if (IsConstantValue(v1_max) && v1_max.b_constant <= 0) { |
| 609 | if (IsConstantValue(v2_min) && v2_min.b_constant >= 0) { |
| 610 | return is_min ? DivValue(v1_min, v2_min) : DivValue(v1_max, v2_max); |
| 611 | } else if (IsConstantValue(v2_max) && v2_max.b_constant <= 0) { |
| 612 | 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] | 613 | } |
| 614 | } |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 615 | return Value(); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 616 | } |
| 617 | |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 618 | InductionVarRange::Value InductionVarRange::MulRangeAndConstant( |
| 619 | int64_t value, |
| 620 | HInductionVarAnalysis::InductionInfo* info, |
| 621 | HInductionVarAnalysis::InductionInfo* trip, |
| 622 | bool in_body, |
| 623 | bool is_min) const { |
| 624 | if (CanLongValueFitIntoInt(value)) { |
| 625 | Value c(static_cast<int32_t>(value)); |
| 626 | return MulValue(GetVal(info, trip, in_body, is_min == value >= 0), c); |
| 627 | } |
| 628 | return Value(); |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 629 | } |
| 630 | |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 631 | InductionVarRange::Value InductionVarRange::DivRangeAndConstant( |
| 632 | int64_t value, |
| 633 | HInductionVarAnalysis::InductionInfo* info, |
| 634 | HInductionVarAnalysis::InductionInfo* trip, |
| 635 | bool in_body, |
| 636 | bool is_min) const { |
| 637 | if (CanLongValueFitIntoInt(value)) { |
| 638 | Value c(static_cast<int32_t>(value)); |
| 639 | return DivValue(GetVal(info, trip, in_body, is_min == value >= 0), c); |
| 640 | } |
| 641 | return Value(); |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 642 | } |
| 643 | |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 644 | InductionVarRange::Value InductionVarRange::AddValue(Value v1, Value v2) const { |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 645 | if (v1.is_known && v2.is_known && IsSafeAdd(v1.b_constant, v2.b_constant)) { |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 646 | const int32_t b = v1.b_constant + v2.b_constant; |
| 647 | if (v1.a_constant == 0) { |
| 648 | return Value(v2.instruction, v2.a_constant, b); |
| 649 | } else if (v2.a_constant == 0) { |
| 650 | return Value(v1.instruction, v1.a_constant, b); |
| 651 | } else if (v1.instruction == v2.instruction && IsSafeAdd(v1.a_constant, v2.a_constant)) { |
| 652 | return Value(v1.instruction, v1.a_constant + v2.a_constant, b); |
| 653 | } |
| 654 | } |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 655 | return Value(); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 656 | } |
| 657 | |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 658 | InductionVarRange::Value InductionVarRange::SubValue(Value v1, Value v2) const { |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 659 | if (v1.is_known && v2.is_known && IsSafeSub(v1.b_constant, v2.b_constant)) { |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 660 | const int32_t b = v1.b_constant - v2.b_constant; |
| 661 | if (v1.a_constant == 0 && IsSafeSub(0, v2.a_constant)) { |
| 662 | return Value(v2.instruction, -v2.a_constant, b); |
| 663 | } else if (v2.a_constant == 0) { |
| 664 | return Value(v1.instruction, v1.a_constant, b); |
| 665 | } else if (v1.instruction == v2.instruction && IsSafeSub(v1.a_constant, v2.a_constant)) { |
| 666 | return Value(v1.instruction, v1.a_constant - v2.a_constant, b); |
| 667 | } |
| 668 | } |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 669 | return Value(); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 670 | } |
| 671 | |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 672 | InductionVarRange::Value InductionVarRange::MulValue(Value v1, Value v2) const { |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 673 | if (v1.is_known && v2.is_known) { |
| 674 | if (v1.a_constant == 0) { |
| 675 | if (IsSafeMul(v1.b_constant, v2.a_constant) && IsSafeMul(v1.b_constant, v2.b_constant)) { |
| 676 | return Value(v2.instruction, v1.b_constant * v2.a_constant, v1.b_constant * v2.b_constant); |
| 677 | } |
| 678 | } else if (v2.a_constant == 0) { |
| 679 | if (IsSafeMul(v1.a_constant, v2.b_constant) && IsSafeMul(v1.b_constant, v2.b_constant)) { |
| 680 | return Value(v1.instruction, v1.a_constant * v2.b_constant, v1.b_constant * v2.b_constant); |
| 681 | } |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 682 | } |
| 683 | } |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 684 | return Value(); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 685 | } |
| 686 | |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 687 | InductionVarRange::Value InductionVarRange::DivValue(Value v1, Value v2) const { |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 688 | 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] | 689 | if (IsSafeDiv(v1.b_constant, v2.b_constant)) { |
| 690 | return Value(v1.b_constant / v2.b_constant); |
| 691 | } |
| 692 | } |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 693 | return Value(); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 694 | } |
| 695 | |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 696 | InductionVarRange::Value InductionVarRange::MergeVal(Value v1, Value v2, bool is_min) const { |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 697 | if (v1.is_known && v2.is_known) { |
| 698 | if (v1.instruction == v2.instruction && v1.a_constant == v2.a_constant) { |
Aart Bik | cd26feb | 2015-09-23 17:50:50 -0700 | [diff] [blame] | 699 | return Value(v1.instruction, v1.a_constant, |
| 700 | is_min ? std::min(v1.b_constant, v2.b_constant) |
| 701 | : std::max(v1.b_constant, v2.b_constant)); |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 702 | } |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 703 | } |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 704 | return Value(); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 705 | } |
| 706 | |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 707 | bool InductionVarRange::GenerateCode(HInstruction* context, |
| 708 | HInstruction* instruction, |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 709 | bool is_last_value, |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 710 | HGraph* graph, |
| 711 | HBasicBlock* block, |
| 712 | /*out*/HInstruction** lower, |
| 713 | /*out*/HInstruction** upper, |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 714 | /*out*/HInstruction** taken_test, |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 715 | /*out*/int64_t* stride_value, |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 716 | /*out*/bool* needs_finite_test, |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 717 | /*out*/bool* needs_taken_test) const { |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 718 | HLoopInformation* loop = nullptr; |
| 719 | HInductionVarAnalysis::InductionInfo* info = nullptr; |
| 720 | HInductionVarAnalysis::InductionInfo* trip = nullptr; |
| 721 | if (!HasInductionInfo(context, instruction, &loop, &info, &trip) || trip == nullptr) { |
| 722 | return false; // codegen needs all information, including tripcount |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 723 | } |
| 724 | // Determine what tests are needed. A finite test is needed if the evaluation code uses the |
| 725 | // trip-count and the loop maybe unsafe (because in such cases, the index could "overshoot" |
| 726 | // the computed range). A taken test is needed for any unknown trip-count, even if evaluation |
| 727 | // code does not use the trip-count explicitly (since there could be an implicit relation |
| 728 | // between e.g. an invariant subscript and a not-taken condition). |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 729 | bool in_body = context->GetBlock() != loop->GetHeader(); |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 730 | *stride_value = 0; |
| 731 | *needs_finite_test = NeedsTripCount(info, stride_value) && IsUnsafeTripCount(trip); |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 732 | *needs_taken_test = IsBodyTripCount(trip); |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 733 | // Handle last value request. |
| 734 | if (is_last_value) { |
| 735 | if (info->induction_class != HInductionVarAnalysis::kLinear) { |
| 736 | return false; |
| 737 | } else if (*stride_value > 0) { |
| 738 | lower = nullptr; |
| 739 | } else { |
| 740 | upper = nullptr; |
| 741 | } |
| 742 | } |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 743 | // Code generation for taken test: generate the code when requested or otherwise analyze |
| 744 | // if code generation is feasible when taken test is needed. |
| 745 | if (taken_test != nullptr) { |
| 746 | return GenerateCode(trip->op_b, nullptr, graph, block, taken_test, in_body, /* is_min */ false); |
| 747 | } else if (*needs_taken_test) { |
| 748 | if (!GenerateCode( |
| 749 | trip->op_b, nullptr, nullptr, nullptr, nullptr, in_body, /* is_min */ false)) { |
| 750 | return false; |
| 751 | } |
| 752 | } |
| 753 | // Code generation for lower and upper. |
| 754 | return |
| 755 | // Success on lower if invariant (not set), or code can be generated. |
| 756 | ((info->induction_class == HInductionVarAnalysis::kInvariant) || |
| 757 | GenerateCode(info, trip, graph, block, lower, in_body, /* is_min */ true)) && |
| 758 | // And success on upper. |
| 759 | GenerateCode(info, trip, graph, block, upper, in_body, /* is_min */ false); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | bool InductionVarRange::GenerateCode(HInductionVarAnalysis::InductionInfo* info, |
| 763 | HInductionVarAnalysis::InductionInfo* trip, |
| 764 | HGraph* graph, // when set, code is generated |
| 765 | HBasicBlock* block, |
| 766 | /*out*/HInstruction** result, |
| 767 | bool in_body, |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 768 | bool is_min) const { |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 769 | if (info != nullptr) { |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 770 | // If during codegen, the result is not needed (nullptr), simply return success. |
| 771 | if (graph != nullptr && result == nullptr) { |
| 772 | return true; |
| 773 | } |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 774 | // Verify type safety. |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 775 | Primitive::Type type = Primitive::kPrimInt; |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 776 | if (info->type != type) { |
| 777 | return false; |
| 778 | } |
| 779 | // Handle current operation. |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 780 | HInstruction* opa = nullptr; |
| 781 | HInstruction* opb = nullptr; |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 782 | switch (info->induction_class) { |
| 783 | case HInductionVarAnalysis::kInvariant: |
| 784 | // Invariants. |
| 785 | switch (info->operation) { |
| 786 | case HInductionVarAnalysis::kAdd: |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 787 | case HInductionVarAnalysis::kLT: |
| 788 | case HInductionVarAnalysis::kLE: |
| 789 | case HInductionVarAnalysis::kGT: |
| 790 | case HInductionVarAnalysis::kGE: |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 791 | if (GenerateCode(info->op_a, trip, graph, block, &opa, in_body, is_min) && |
| 792 | GenerateCode(info->op_b, trip, graph, block, &opb, in_body, is_min)) { |
| 793 | if (graph != nullptr) { |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 794 | HInstruction* operation = nullptr; |
| 795 | switch (info->operation) { |
| 796 | case HInductionVarAnalysis::kAdd: |
| 797 | operation = new (graph->GetArena()) HAdd(type, opa, opb); break; |
| 798 | case HInductionVarAnalysis::kLT: |
| 799 | operation = new (graph->GetArena()) HLessThan(opa, opb); break; |
| 800 | case HInductionVarAnalysis::kLE: |
| 801 | operation = new (graph->GetArena()) HLessThanOrEqual(opa, opb); break; |
| 802 | case HInductionVarAnalysis::kGT: |
| 803 | operation = new (graph->GetArena()) HGreaterThan(opa, opb); break; |
| 804 | case HInductionVarAnalysis::kGE: |
| 805 | operation = new (graph->GetArena()) HGreaterThanOrEqual(opa, opb); break; |
| 806 | default: |
| 807 | LOG(FATAL) << "unknown operation"; |
| 808 | } |
| 809 | *result = Insert(block, operation); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 810 | } |
| 811 | return true; |
| 812 | } |
| 813 | break; |
| 814 | case HInductionVarAnalysis::kSub: // second reversed! |
| 815 | if (GenerateCode(info->op_a, trip, graph, block, &opa, in_body, is_min) && |
| 816 | GenerateCode(info->op_b, trip, graph, block, &opb, in_body, !is_min)) { |
| 817 | if (graph != nullptr) { |
| 818 | *result = Insert(block, new (graph->GetArena()) HSub(type, opa, opb)); |
| 819 | } |
| 820 | return true; |
| 821 | } |
| 822 | break; |
| 823 | case HInductionVarAnalysis::kNeg: // reversed! |
| 824 | if (GenerateCode(info->op_b, trip, graph, block, &opb, in_body, !is_min)) { |
| 825 | if (graph != nullptr) { |
| 826 | *result = Insert(block, new (graph->GetArena()) HNeg(type, opb)); |
| 827 | } |
| 828 | return true; |
| 829 | } |
| 830 | break; |
| 831 | case HInductionVarAnalysis::kFetch: |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 832 | if (graph != nullptr) { |
| 833 | *result = info->fetch; // already in HIR |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 834 | } |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 835 | return true; |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 836 | case HInductionVarAnalysis::kTripCountInLoop: |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 837 | case HInductionVarAnalysis::kTripCountInLoopUnsafe: |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 838 | if (!in_body && !is_min) { // one extra! |
Aart Bik | 22f0587 | 2015-10-27 15:56:28 -0700 | [diff] [blame] | 839 | return GenerateCode(info->op_a, trip, graph, block, result, in_body, is_min); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 840 | } |
| 841 | FALLTHROUGH_INTENDED; |
| 842 | case HInductionVarAnalysis::kTripCountInBody: |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 843 | case HInductionVarAnalysis::kTripCountInBodyUnsafe: |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 844 | if (is_min) { |
| 845 | if (graph != nullptr) { |
| 846 | *result = graph->GetIntConstant(0); |
| 847 | } |
| 848 | return true; |
| 849 | } else if (in_body) { |
Aart Bik | 22f0587 | 2015-10-27 15:56:28 -0700 | [diff] [blame] | 850 | if (GenerateCode(info->op_a, trip, graph, block, &opb, in_body, is_min)) { |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 851 | if (graph != nullptr) { |
| 852 | *result = Insert(block, |
| 853 | new (graph->GetArena()) |
| 854 | HSub(type, opb, graph->GetIntConstant(1))); |
| 855 | } |
| 856 | return true; |
| 857 | } |
| 858 | } |
| 859 | break; |
| 860 | default: |
| 861 | break; |
| 862 | } |
| 863 | break; |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 864 | case HInductionVarAnalysis::kLinear: { |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 865 | // Linear induction a * i + b, for normalized 0 <= i < TC. For ranges, this should |
| 866 | // be restricted to a unit stride to avoid arithmetic wrap-around situations that |
| 867 | // are harder to guard against. For a last value, requesting min/max based on any |
| 868 | // stride yields right value. |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 869 | int64_t stride_value = 0; |
| 870 | if (IsConstant(info->op_a, kExact, &stride_value)) { |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 871 | const bool is_min_a = stride_value >= 0 ? is_min : !is_min; |
| 872 | if (GenerateCode(trip, trip, graph, block, &opa, in_body, is_min_a) && |
| 873 | GenerateCode(info->op_b, trip, graph, block, &opb, in_body, is_min)) { |
| 874 | if (graph != nullptr) { |
| 875 | HInstruction* oper; |
| 876 | if (stride_value == 1) { |
| 877 | oper = new (graph->GetArena()) HAdd(type, opa, opb); |
| 878 | } else if (stride_value == -1) { |
| 879 | oper = new (graph->GetArena()) HSub(type, opb, opa); |
| 880 | } else { |
| 881 | HInstruction* mul = new (graph->GetArena()) HMul(type, graph->GetIntConstant(stride_value), opa); |
| 882 | oper = new (graph->GetArena()) HAdd(type, Insert(block, mul), opb); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 883 | } |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 884 | *result = Insert(block, oper); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 885 | } |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 886 | return true; |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 887 | } |
| 888 | } |
| 889 | break; |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 890 | } |
| 891 | case HInductionVarAnalysis::kWrapAround: |
| 892 | case HInductionVarAnalysis::kPeriodic: { |
| 893 | // Wrap-around and periodic inductions are restricted to constants only, so that extreme |
| 894 | // values are easy to test at runtime without complications of arithmetic wrap-around. |
| 895 | Value extreme = GetVal(info, trip, in_body, is_min); |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 896 | if (IsConstantValue(extreme)) { |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 897 | if (graph != nullptr) { |
| 898 | *result = graph->GetIntConstant(extreme.b_constant); |
| 899 | } |
| 900 | return true; |
| 901 | } |
| 902 | break; |
| 903 | } |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 904 | default: |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 905 | break; |
| 906 | } |
| 907 | } |
| 908 | return false; |
| 909 | } |
| 910 | |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 911 | void InductionVarRange::ReplaceInduction(HInductionVarAnalysis::InductionInfo* info, |
| 912 | HInstruction* fetch, |
| 913 | HInstruction* replacement) { |
| 914 | if (info != nullptr) { |
| 915 | if (info->induction_class == HInductionVarAnalysis::kInvariant && |
| 916 | info->operation == HInductionVarAnalysis::kFetch && |
| 917 | info->fetch == fetch) { |
| 918 | info->fetch = replacement; |
| 919 | } |
| 920 | ReplaceInduction(info->op_a, fetch, replacement); |
| 921 | ReplaceInduction(info->op_b, fetch, replacement); |
| 922 | } |
| 923 | } |
| 924 | |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 925 | } // namespace art |