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 | 97412c9 | 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 | 97412c9 | 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 | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 60 | /** Returns b^e for b,e >= 1. */ |
| 61 | static int64_t IntPow(int64_t b, int64_t e) { |
| 62 | DCHECK_GE(b, 1); |
| 63 | DCHECK_GE(e, 1); |
| 64 | int64_t pow = 1; |
| 65 | while (e) { |
| 66 | if (e & 1) { |
| 67 | pow *= b; |
| 68 | } |
| 69 | e >>= 1; |
| 70 | b *= b; |
| 71 | } |
| 72 | return pow; |
| 73 | } |
| 74 | |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 75 | /** |
Aart Bik | 40fbf74 | 2016-10-31 11:02:50 -0700 | [diff] [blame] | 76 | * Detects an instruction that is >= 0. As long as the value is carried by |
| 77 | * a single instruction, arithmetic wrap-around cannot occur. |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 78 | */ |
Aart Bik | 40fbf74 | 2016-10-31 11:02:50 -0700 | [diff] [blame] | 79 | static bool IsGEZero(HInstruction* instruction) { |
| 80 | DCHECK(instruction != nullptr); |
| 81 | if (instruction->IsArrayLength()) { |
| 82 | return true; |
| 83 | } else if (instruction->IsInvokeStaticOrDirect()) { |
| 84 | switch (instruction->AsInvoke()->GetIntrinsic()) { |
| 85 | case Intrinsics::kMathMinIntInt: |
| 86 | case Intrinsics::kMathMinLongLong: |
| 87 | // Instruction MIN(>=0, >=0) is >= 0. |
| 88 | return IsGEZero(instruction->InputAt(0)) && |
| 89 | IsGEZero(instruction->InputAt(1)); |
| 90 | case Intrinsics::kMathAbsInt: |
| 91 | case Intrinsics::kMathAbsLong: |
| 92 | // Instruction ABS(x) is >= 0. |
| 93 | return true; |
| 94 | default: |
| 95 | break; |
| 96 | } |
| 97 | } |
| 98 | int64_t value = -1; |
| 99 | return IsIntAndGet(instruction, &value) && value >= 0; |
| 100 | } |
| 101 | |
| 102 | /** Hunts "under the hood" for a suitable instruction at the hint. */ |
| 103 | static bool IsMaxAtHint( |
| 104 | HInstruction* instruction, HInstruction* hint, /*out*/HInstruction** suitable) { |
| 105 | if (instruction->IsInvokeStaticOrDirect()) { |
| 106 | switch (instruction->AsInvoke()->GetIntrinsic()) { |
| 107 | case Intrinsics::kMathMinIntInt: |
| 108 | case Intrinsics::kMathMinLongLong: |
| 109 | // For MIN(x, y), return most suitable x or y as maximum. |
| 110 | return IsMaxAtHint(instruction->InputAt(0), hint, suitable) || |
| 111 | IsMaxAtHint(instruction->InputAt(1), hint, suitable); |
| 112 | default: |
| 113 | break; |
| 114 | } |
| 115 | } else { |
| 116 | *suitable = instruction; |
Nicolas Geoffray | e761bcc | 2017-01-19 08:59:37 +0000 | [diff] [blame] | 117 | return HuntForDeclaration(instruction) == hint; |
Aart Bik | 40fbf74 | 2016-10-31 11:02:50 -0700 | [diff] [blame] | 118 | } |
| 119 | return false; |
| 120 | } |
| 121 | |
| 122 | /** Post-analysis simplification of a minimum value that makes the bound more useful to clients. */ |
| 123 | static InductionVarRange::Value SimplifyMin(InductionVarRange::Value v) { |
| 124 | if (v.is_known && v.a_constant == 1 && v.b_constant <= 0) { |
| 125 | // If a == 1, instruction >= 0 and b <= 0, just return the constant b. |
| 126 | // No arithmetic wrap-around can occur. |
| 127 | if (IsGEZero(v.instruction)) { |
| 128 | return InductionVarRange::Value(v.b_constant); |
| 129 | } |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 130 | } |
| 131 | return v; |
| 132 | } |
| 133 | |
Aart Bik | 40fbf74 | 2016-10-31 11:02:50 -0700 | [diff] [blame] | 134 | /** Post-analysis simplification of a maximum value that makes the bound more useful to clients. */ |
| 135 | static InductionVarRange::Value SimplifyMax(InductionVarRange::Value v, HInstruction* hint) { |
| 136 | if (v.is_known && v.a_constant >= 1) { |
| 137 | // An upper bound a * (length / a) + b, where a >= 1, can be conservatively rewritten as |
| 138 | // length + b because length >= 0 is true. |
| 139 | int64_t value; |
| 140 | if (v.instruction->IsDiv() && |
| 141 | v.instruction->InputAt(0)->IsArrayLength() && |
| 142 | IsIntAndGet(v.instruction->InputAt(1), &value) && v.a_constant == value) { |
| 143 | return InductionVarRange::Value(v.instruction->InputAt(0), 1, v.b_constant); |
| 144 | } |
| 145 | // If a == 1, the most suitable one suffices as maximum value. |
| 146 | HInstruction* suitable = nullptr; |
| 147 | if (v.a_constant == 1 && IsMaxAtHint(v.instruction, hint, &suitable)) { |
| 148 | return InductionVarRange::Value(suitable, 1, v.b_constant); |
| 149 | } |
| 150 | } |
| 151 | return v; |
| 152 | } |
| 153 | |
| 154 | /** Tests for a constant value. */ |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 155 | static bool IsConstantValue(InductionVarRange::Value v) { |
| 156 | return v.is_known && v.a_constant == 0; |
| 157 | } |
| 158 | |
| 159 | /** 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] | 160 | static InductionVarRange::Value CorrectForType(InductionVarRange::Value v, Primitive::Type type) { |
| 161 | switch (type) { |
| 162 | case Primitive::kPrimShort: |
| 163 | case Primitive::kPrimChar: |
| 164 | case Primitive::kPrimByte: { |
| 165 | // Constants within range only. |
| 166 | // TODO: maybe some room for improvement, like allowing widening conversions |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 167 | int32_t min = Primitive::MinValueOfIntegralType(type); |
| 168 | int32_t max = Primitive::MaxValueOfIntegralType(type); |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 169 | return (IsConstantValue(v) && min <= v.b_constant && v.b_constant <= max) |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 170 | ? v |
| 171 | : InductionVarRange::Value(); |
| 172 | } |
| 173 | default: |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 174 | return v; |
| 175 | } |
| 176 | } |
| 177 | |
Aart Bik | 40fbf74 | 2016-10-31 11:02:50 -0700 | [diff] [blame] | 178 | /** Inserts an instruction. */ |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 179 | static HInstruction* Insert(HBasicBlock* block, HInstruction* instruction) { |
| 180 | DCHECK(block != nullptr); |
| 181 | DCHECK(block->GetLastInstruction() != nullptr) << block->GetBlockId(); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 182 | DCHECK(instruction != nullptr); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 183 | block->InsertInstructionBefore(instruction, block->GetLastInstruction()); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 184 | return instruction; |
| 185 | } |
| 186 | |
Aart Bik | 40fbf74 | 2016-10-31 11:02:50 -0700 | [diff] [blame] | 187 | /** Obtains loop's control instruction. */ |
Aart Bik | 009cace | 2016-09-16 10:15:19 -0700 | [diff] [blame] | 188 | static HInstruction* GetLoopControl(HLoopInformation* loop) { |
| 189 | DCHECK(loop != nullptr); |
| 190 | return loop->GetHeader()->GetLastInstruction(); |
| 191 | } |
| 192 | |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 193 | // |
| 194 | // Public class methods. |
| 195 | // |
| 196 | |
| 197 | InductionVarRange::InductionVarRange(HInductionVarAnalysis* induction_analysis) |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 198 | : induction_analysis_(induction_analysis), |
| 199 | chase_hint_(nullptr) { |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 200 | DCHECK(induction_analysis != nullptr); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 201 | } |
| 202 | |
Aart Bik | 1fc3afb | 2016-02-02 13:26:16 -0800 | [diff] [blame] | 203 | bool InductionVarRange::GetInductionRange(HInstruction* context, |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 204 | HInstruction* instruction, |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 205 | HInstruction* chase_hint, |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 206 | /*out*/Value* min_val, |
| 207 | /*out*/Value* max_val, |
| 208 | /*out*/bool* needs_finite_test) { |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 209 | HLoopInformation* loop = nullptr; |
| 210 | HInductionVarAnalysis::InductionInfo* info = nullptr; |
| 211 | HInductionVarAnalysis::InductionInfo* trip = nullptr; |
| 212 | if (!HasInductionInfo(context, instruction, &loop, &info, &trip)) { |
| 213 | return false; |
Aart Bik | 97412c9 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 214 | } |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 215 | // Type int or lower (this is not too restrictive since intended clients, like |
| 216 | // bounds check elimination, will have truncated higher precision induction |
| 217 | // at their use point already). |
| 218 | switch (info->type) { |
| 219 | case Primitive::kPrimInt: |
| 220 | case Primitive::kPrimShort: |
| 221 | case Primitive::kPrimChar: |
| 222 | case Primitive::kPrimByte: |
| 223 | break; |
| 224 | default: |
| 225 | return false; |
| 226 | } |
Aart Bik | 97412c9 | 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; |
| 229 | bool in_body = context->GetBlock() != loop->GetHeader(); |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 230 | int64_t stride_value = 0; |
Aart Bik | 40fbf74 | 2016-10-31 11:02:50 -0700 | [diff] [blame] | 231 | *min_val = SimplifyMin(GetVal(info, trip, in_body, /* is_min */ true)); |
| 232 | *max_val = SimplifyMax(GetVal(info, trip, in_body, /* is_min */ false), chase_hint); |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 233 | *needs_finite_test = NeedsTripCount(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) { |
| 237 | *min_val = SimplifyMin(GetVal(info, trip, in_body, /* is_min */ true)); |
| 238 | } |
Aart Bik | 97412c9 | 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 | |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 242 | bool InductionVarRange::CanGenerateRange(HInstruction* context, |
| 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 | |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 264 | void InductionVarRange::GenerateRange(HInstruction* context, |
| 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 | |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 288 | HInstruction* InductionVarRange::GenerateTakenTest(HInstruction* context, |
| 289 | HGraph* graph, |
| 290 | HBasicBlock* block) { |
| 291 | HInstruction* taken_test = nullptr; |
| 292 | bool is_last_value = false; |
| 293 | int64_t stride_value = 0; |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 294 | bool b1, b2; // unused |
Aart Bik | 9abf894 | 2016-10-14 09:49:42 -0700 | [diff] [blame] | 295 | if (!GenerateRangeOrLastValue(context, |
| 296 | context, |
| 297 | is_last_value, |
| 298 | graph, |
| 299 | block, |
| 300 | nullptr, |
| 301 | nullptr, |
| 302 | &taken_test, |
| 303 | &stride_value, |
| 304 | &b1, |
| 305 | &b2)) { |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 306 | LOG(FATAL) << "Failed precondition: CanGenerateRange()"; |
| 307 | } |
| 308 | return taken_test; |
| 309 | } |
| 310 | |
| 311 | bool InductionVarRange::CanGenerateLastValue(HInstruction* instruction) { |
| 312 | bool is_last_value = true; |
| 313 | int64_t stride_value = 0; |
| 314 | bool needs_finite_test = false; |
| 315 | bool needs_taken_test = false; |
Aart Bik | 9abf894 | 2016-10-14 09:49:42 -0700 | [diff] [blame] | 316 | return GenerateRangeOrLastValue(instruction, |
| 317 | instruction, |
| 318 | is_last_value, |
| 319 | nullptr, |
| 320 | nullptr, |
| 321 | nullptr, |
| 322 | nullptr, |
| 323 | nullptr, // nothing generated yet |
| 324 | &stride_value, |
| 325 | &needs_finite_test, |
| 326 | &needs_taken_test) |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 327 | && !needs_finite_test && !needs_taken_test; |
| 328 | } |
| 329 | |
| 330 | HInstruction* InductionVarRange::GenerateLastValue(HInstruction* instruction, |
| 331 | HGraph* graph, |
| 332 | HBasicBlock* block) { |
| 333 | HInstruction* last_value = nullptr; |
| 334 | bool is_last_value = true; |
| 335 | int64_t stride_value = 0; |
| 336 | bool b1, b2; // unused |
Aart Bik | 9abf894 | 2016-10-14 09:49:42 -0700 | [diff] [blame] | 337 | if (!GenerateRangeOrLastValue(instruction, |
| 338 | instruction, |
| 339 | is_last_value, |
| 340 | graph, |
| 341 | block, |
| 342 | &last_value, |
| 343 | &last_value, |
| 344 | nullptr, |
| 345 | &stride_value, |
| 346 | &b1, |
| 347 | &b2)) { |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 348 | LOG(FATAL) << "Failed precondition: CanGenerateLastValue()"; |
| 349 | } |
| 350 | return last_value; |
| 351 | } |
| 352 | |
| 353 | void InductionVarRange::Replace(HInstruction* instruction, |
| 354 | HInstruction* fetch, |
| 355 | HInstruction* replacement) { |
| 356 | for (HLoopInformation* lp = instruction->GetBlock()->GetLoopInformation(); // closest enveloping loop |
| 357 | lp != nullptr; |
| 358 | lp = lp->GetPreHeader()->GetLoopInformation()) { |
Aart Bik | 009cace | 2016-09-16 10:15:19 -0700 | [diff] [blame] | 359 | // Update instruction's information. |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 360 | ReplaceInduction(induction_analysis_->LookupInfo(lp, instruction), fetch, replacement); |
Aart Bik | 009cace | 2016-09-16 10:15:19 -0700 | [diff] [blame] | 361 | // Update loop's trip-count information. |
| 362 | ReplaceInduction(induction_analysis_->LookupInfo(lp, GetLoopControl(lp)), fetch, replacement); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 363 | } |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 364 | } |
| 365 | |
Aart Bik | 6b69e0a | 2017-01-11 10:20:43 -0800 | [diff] [blame] | 366 | bool InductionVarRange::IsFinite(HLoopInformation* loop, /*out*/ int64_t* tc) const { |
Aart Bik | 9abf894 | 2016-10-14 09:49:42 -0700 | [diff] [blame] | 367 | HInductionVarAnalysis::InductionInfo *trip = |
| 368 | induction_analysis_->LookupInfo(loop, GetLoopControl(loop)); |
Aart Bik | 6b69e0a | 2017-01-11 10:20:43 -0800 | [diff] [blame] | 369 | if (trip != nullptr && !IsUnsafeTripCount(trip)) { |
| 370 | IsConstant(trip->op_a, kExact, tc); |
| 371 | return true; |
| 372 | } |
| 373 | return false; |
Aart Bik | 9abf894 | 2016-10-14 09:49:42 -0700 | [diff] [blame] | 374 | } |
| 375 | |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 376 | // |
| 377 | // Private class methods. |
| 378 | // |
| 379 | |
Aart Bik | 97412c9 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 380 | bool InductionVarRange::IsConstant(HInductionVarAnalysis::InductionInfo* info, |
| 381 | ConstantRequest request, |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 382 | /*out*/ int64_t* value) const { |
Aart Bik | 97412c9 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 383 | if (info != nullptr) { |
| 384 | // A direct 32-bit or 64-bit constant fetch. This immediately satisfies |
| 385 | // any of the three requests (kExact, kAtMost, and KAtLeast). |
| 386 | if (info->induction_class == HInductionVarAnalysis::kInvariant && |
| 387 | info->operation == HInductionVarAnalysis::kFetch) { |
| 388 | if (IsIntAndGet(info->fetch, value)) { |
| 389 | return true; |
| 390 | } |
| 391 | } |
Aart Bik | 40fbf74 | 2016-10-31 11:02:50 -0700 | [diff] [blame] | 392 | // Try range analysis on the invariant, only accept a proper range |
| 393 | // to avoid arithmetic wrap-around anomalies. |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 394 | Value min_val = GetVal(info, nullptr, /* in_body */ true, /* is_min */ true); |
| 395 | Value max_val = GetVal(info, nullptr, /* in_body */ true, /* is_min */ false); |
| 396 | if (IsConstantValue(min_val) && |
| 397 | IsConstantValue(max_val) && min_val.b_constant <= max_val.b_constant) { |
| 398 | if ((request == kExact && min_val.b_constant == max_val.b_constant) || request == kAtMost) { |
| 399 | *value = max_val.b_constant; |
| 400 | return true; |
| 401 | } else if (request == kAtLeast) { |
| 402 | *value = min_val.b_constant; |
Aart Bik | 358af83 | 2016-02-24 14:17:53 -0800 | [diff] [blame] | 403 | return true; |
| 404 | } |
| 405 | } |
Aart Bik | 97412c9 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 406 | } |
| 407 | return false; |
| 408 | } |
| 409 | |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 410 | bool InductionVarRange::HasInductionInfo( |
| 411 | HInstruction* context, |
| 412 | HInstruction* instruction, |
| 413 | /*out*/ HLoopInformation** loop, |
| 414 | /*out*/ HInductionVarAnalysis::InductionInfo** info, |
| 415 | /*out*/ HInductionVarAnalysis::InductionInfo** trip) const { |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 416 | DCHECK(context != nullptr); |
| 417 | DCHECK(context->GetBlock() != nullptr); |
Aart Bik | 009cace | 2016-09-16 10:15:19 -0700 | [diff] [blame] | 418 | HLoopInformation* lp = context->GetBlock()->GetLoopInformation(); // closest enveloping loop |
| 419 | if (lp != nullptr) { |
| 420 | HInductionVarAnalysis::InductionInfo* i = induction_analysis_->LookupInfo(lp, instruction); |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 421 | if (i != nullptr) { |
Aart Bik | 009cace | 2016-09-16 10:15:19 -0700 | [diff] [blame] | 422 | *loop = lp; |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 423 | *info = i; |
Aart Bik | 009cace | 2016-09-16 10:15:19 -0700 | [diff] [blame] | 424 | *trip = induction_analysis_->LookupInfo(lp, GetLoopControl(lp)); |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 425 | return true; |
| 426 | } |
| 427 | } |
| 428 | return false; |
| 429 | } |
| 430 | |
| 431 | bool InductionVarRange::IsWellBehavedTripCount(HInductionVarAnalysis::InductionInfo* trip) const { |
| 432 | if (trip != nullptr) { |
| 433 | // Both bounds that define a trip-count are well-behaved if they either are not defined |
| 434 | // in any loop, or are contained in a proper interval. This allows finding the min/max |
| 435 | // of an expression by chasing outward. |
| 436 | InductionVarRange range(induction_analysis_); |
| 437 | HInductionVarAnalysis::InductionInfo* lower = trip->op_b->op_a; |
| 438 | HInductionVarAnalysis::InductionInfo* upper = trip->op_b->op_b; |
| 439 | int64_t not_used = 0; |
| 440 | return (!HasFetchInLoop(lower) || range.IsConstant(lower, kAtLeast, ¬_used)) && |
| 441 | (!HasFetchInLoop(upper) || range.IsConstant(upper, kAtLeast, ¬_used)); |
| 442 | } |
| 443 | return true; |
| 444 | } |
| 445 | |
| 446 | bool InductionVarRange::HasFetchInLoop(HInductionVarAnalysis::InductionInfo* info) const { |
| 447 | if (info != nullptr) { |
| 448 | if (info->induction_class == HInductionVarAnalysis::kInvariant && |
| 449 | info->operation == HInductionVarAnalysis::kFetch) { |
| 450 | return info->fetch->GetBlock()->GetLoopInformation() != nullptr; |
| 451 | } |
| 452 | return HasFetchInLoop(info->op_a) || HasFetchInLoop(info->op_b); |
| 453 | } |
| 454 | return false; |
| 455 | } |
| 456 | |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 457 | bool InductionVarRange::NeedsTripCount(HInductionVarAnalysis::InductionInfo* info, |
| 458 | int64_t* stride_value) const { |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 459 | if (info != nullptr) { |
| 460 | if (info->induction_class == HInductionVarAnalysis::kLinear) { |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 461 | return IsConstant(info->op_a, kExact, stride_value); |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 462 | } else if (info->induction_class == HInductionVarAnalysis::kPolynomial) { |
| 463 | return NeedsTripCount(info->op_a, stride_value); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 464 | } else if (info->induction_class == HInductionVarAnalysis::kWrapAround) { |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 465 | return NeedsTripCount(info->op_b, stride_value); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 466 | } |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 467 | } |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 468 | return false; |
| 469 | } |
| 470 | |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 471 | bool InductionVarRange::IsBodyTripCount(HInductionVarAnalysis::InductionInfo* trip) const { |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 472 | if (trip != nullptr) { |
| 473 | if (trip->induction_class == HInductionVarAnalysis::kInvariant) { |
| 474 | return trip->operation == HInductionVarAnalysis::kTripCountInBody || |
| 475 | trip->operation == HInductionVarAnalysis::kTripCountInBodyUnsafe; |
| 476 | } |
| 477 | } |
| 478 | return false; |
| 479 | } |
| 480 | |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 481 | bool InductionVarRange::IsUnsafeTripCount(HInductionVarAnalysis::InductionInfo* trip) const { |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 482 | if (trip != nullptr) { |
| 483 | if (trip->induction_class == HInductionVarAnalysis::kInvariant) { |
| 484 | return trip->operation == HInductionVarAnalysis::kTripCountInBodyUnsafe || |
| 485 | trip->operation == HInductionVarAnalysis::kTripCountInLoopUnsafe; |
| 486 | } |
| 487 | } |
| 488 | return false; |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 489 | } |
| 490 | |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 491 | InductionVarRange::Value InductionVarRange::GetLinear(HInductionVarAnalysis::InductionInfo* info, |
| 492 | HInductionVarAnalysis::InductionInfo* trip, |
| 493 | bool in_body, |
| 494 | bool is_min) const { |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 495 | DCHECK(info != nullptr); |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 496 | DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kLinear); |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 497 | // 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] | 498 | // analysis (finding max a * (TC - 1) + OFFSET for a == 1 and TC = UPPER - OFFSET or finding |
| 499 | // min a * (TC - 1) + OFFSET for a == -1 and TC = OFFSET - UPPER) to avoid losing information |
| 500 | // with intermediate results that only incorporate single instructions. |
| 501 | if (trip != nullptr) { |
| 502 | HInductionVarAnalysis::InductionInfo* trip_expr = trip->op_a; |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 503 | if (trip_expr->type == info->type && trip_expr->operation == HInductionVarAnalysis::kSub) { |
Aart Bik | 97412c9 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 504 | int64_t stride_value = 0; |
| 505 | if (IsConstant(info->op_a, kExact, &stride_value)) { |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 506 | if (!is_min && stride_value == 1) { |
Aart Bik | 97412c9 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 507 | // 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] | 508 | if (HInductionVarAnalysis::InductionEqual(trip_expr->op_b, info->op_b)) { |
| 509 | // Analyze cancelled trip with just the positive operand (trip_expr->op_a). |
| 510 | HInductionVarAnalysis::InductionInfo cancelled_trip( |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 511 | trip->induction_class, |
| 512 | trip->operation, |
| 513 | trip_expr->op_a, |
| 514 | trip->op_b, |
| 515 | nullptr, |
| 516 | trip->type); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 517 | return GetVal(&cancelled_trip, trip, in_body, is_min); |
| 518 | } |
| 519 | } else if (is_min && stride_value == -1) { |
Aart Bik | 97412c9 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 520 | // 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] | 521 | if (HInductionVarAnalysis::InductionEqual(trip_expr->op_a, info->op_b)) { |
| 522 | // Analyze cancelled trip with just the negative operand (trip_expr->op_b). |
| 523 | HInductionVarAnalysis::InductionInfo neg( |
| 524 | HInductionVarAnalysis::kInvariant, |
| 525 | HInductionVarAnalysis::kNeg, |
| 526 | nullptr, |
| 527 | trip_expr->op_b, |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 528 | nullptr, |
| 529 | trip->type); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 530 | HInductionVarAnalysis::InductionInfo cancelled_trip( |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 531 | trip->induction_class, trip->operation, &neg, trip->op_b, nullptr, trip->type); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 532 | return SubValue(Value(0), GetVal(&cancelled_trip, trip, in_body, !is_min)); |
| 533 | } |
| 534 | } |
| 535 | } |
| 536 | } |
| 537 | } |
| 538 | // General rule of linear induction a * i + b, for normalized 0 <= i < TC. |
| 539 | return AddValue(GetMul(info->op_a, trip, trip, in_body, is_min), |
| 540 | GetVal(info->op_b, trip, in_body, is_min)); |
| 541 | } |
| 542 | |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 543 | InductionVarRange::Value InductionVarRange::GetPolynomial(HInductionVarAnalysis::InductionInfo* info, |
| 544 | HInductionVarAnalysis::InductionInfo* trip, |
| 545 | bool in_body, |
| 546 | bool is_min) const { |
| 547 | DCHECK(info != nullptr); |
| 548 | DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kPolynomial); |
| 549 | int64_t a = 0; |
| 550 | int64_t b = 0; |
| 551 | if (IsConstant(info->op_a->op_a, kExact, &a) && CanLongValueFitIntoInt(a) && a >= 0 && |
| 552 | IsConstant(info->op_a->op_b, kExact, &b) && CanLongValueFitIntoInt(b) && b >= 0) { |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 553 | // 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] | 554 | // maximum index value m as a * (m * (m-1)) / 2 + b * m + c. |
| 555 | Value c = GetVal(info->op_b, trip, in_body, is_min); |
| 556 | if (is_min) { |
| 557 | return c; |
| 558 | } else { |
| 559 | Value m = GetVal(trip, trip, in_body, is_min); |
| 560 | Value t = DivValue(MulValue(m, SubValue(m, Value(1))), Value(2)); |
| 561 | Value x = MulValue(Value(a), t); |
| 562 | Value y = MulValue(Value(b), m); |
| 563 | return AddValue(AddValue(x, y), c); |
| 564 | } |
| 565 | } |
| 566 | return Value(); |
| 567 | } |
| 568 | |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 569 | InductionVarRange::Value InductionVarRange::GetGeometric(HInductionVarAnalysis::InductionInfo* info, |
| 570 | HInductionVarAnalysis::InductionInfo* trip, |
| 571 | bool in_body, |
| 572 | bool is_min) const { |
| 573 | DCHECK(info != nullptr); |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 574 | DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kGeometric); |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 575 | int64_t a = 0; |
| 576 | int64_t f = 0; |
| 577 | if (IsConstant(info->op_a, kExact, &a) && |
| 578 | CanLongValueFitIntoInt(a) && |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 579 | IsIntAndGet(info->fetch, &f) && f >= 1) { |
| 580 | // Conservative bounds on a * f^-i + b with f >= 1 can be computed without |
| 581 | // trip count. Other forms would require a much more elaborate evaluation. |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 582 | const bool is_min_a = a >= 0 ? is_min : !is_min; |
| 583 | if (info->operation == HInductionVarAnalysis::kDiv) { |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 584 | Value b = GetVal(info->op_b, trip, in_body, is_min); |
| 585 | return is_min_a ? b : AddValue(Value(a), b); |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 586 | } |
| 587 | } |
| 588 | return Value(); |
| 589 | } |
| 590 | |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 591 | InductionVarRange::Value InductionVarRange::GetFetch(HInstruction* instruction, |
Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 592 | HInductionVarAnalysis::InductionInfo* trip, |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 593 | bool in_body, |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 594 | bool is_min) const { |
Aart Bik | 40fbf74 | 2016-10-31 11:02:50 -0700 | [diff] [blame] | 595 | // Special case when chasing constants: single instruction that denotes trip count in the |
| 596 | // loop-body is minimal 1 and maximal, with safe trip-count, max int, |
| 597 | if (chase_hint_ == nullptr && in_body && trip != nullptr && instruction == trip->op_a->fetch) { |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 598 | if (is_min) { |
| 599 | return Value(1); |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 600 | } else if (!instruction->IsConstant() && !IsUnsafeTripCount(trip)) { |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 601 | return Value(std::numeric_limits<int32_t>::max()); |
| 602 | } |
| 603 | } |
Aart Bik | 40fbf74 | 2016-10-31 11:02:50 -0700 | [diff] [blame] | 604 | // Unless at a constant or hint, chase the instruction a bit deeper into the HIR tree, so that |
| 605 | // it becomes more likely range analysis will compare the same instructions as terminal nodes. |
| 606 | int64_t value; |
| 607 | if (IsIntAndGet(instruction, &value) && CanLongValueFitIntoInt(value)) { |
| 608 | // Proper constant reveals best information. |
| 609 | return Value(static_cast<int32_t>(value)); |
| 610 | } else if (instruction == chase_hint_) { |
| 611 | // At hint, fetch is represented by itself. |
| 612 | return Value(instruction, 1, 0); |
| 613 | } else if (instruction->IsAdd()) { |
| 614 | // Incorporate suitable constants in the chased value. |
Aart Bik | 97412c9 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 615 | if (IsIntAndGet(instruction->InputAt(0), &value) && CanLongValueFitIntoInt(value)) { |
| 616 | return AddValue(Value(static_cast<int32_t>(value)), |
| 617 | GetFetch(instruction->InputAt(1), trip, in_body, is_min)); |
| 618 | } else if (IsIntAndGet(instruction->InputAt(1), &value) && CanLongValueFitIntoInt(value)) { |
| 619 | return AddValue(GetFetch(instruction->InputAt(0), trip, in_body, is_min), |
| 620 | Value(static_cast<int32_t>(value))); |
Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 621 | } |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 622 | } else if (instruction->IsArrayLength()) { |
Aart Bik | 40fbf74 | 2016-10-31 11:02:50 -0700 | [diff] [blame] | 623 | // 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] | 624 | if (chase_hint_ == nullptr) { |
| 625 | return is_min ? Value(0) : Value(std::numeric_limits<int32_t>::max()); |
| 626 | } else if (instruction->InputAt(0)->IsNewArray()) { |
Nicolas Geoffray | e761bcc | 2017-01-19 08:59:37 +0000 | [diff] [blame] | 627 | return GetFetch(instruction->InputAt(0)->AsNewArray()->GetLength(), trip, in_body, is_min); |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 628 | } |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 629 | } else if (instruction->IsTypeConversion()) { |
Aart Bik | 40fbf74 | 2016-10-31 11:02:50 -0700 | [diff] [blame] | 630 | // 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] | 631 | // For example, this discovers the length in: for (long i = 0; i < a.length; i++); |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 632 | if (instruction->AsTypeConversion()->GetInputType() == Primitive::kPrimInt && |
| 633 | instruction->AsTypeConversion()->GetResultType() == Primitive::kPrimLong) { |
| 634 | return GetFetch(instruction->InputAt(0), trip, in_body, is_min); |
| 635 | } |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 636 | } |
| 637 | // Chase an invariant fetch that is defined by an outer loop if the trip-count used |
| 638 | // so far is well-behaved in both bounds and the next trip-count is safe. |
| 639 | // Example: |
| 640 | // for (int i = 0; i <= 100; i++) // safe |
| 641 | // for (int j = 0; j <= i; j++) // well-behaved |
| 642 | // j is in range [0, i ] (if i is chase hint) |
| 643 | // or in range [0, 100] (otherwise) |
| 644 | HLoopInformation* next_loop = nullptr; |
| 645 | HInductionVarAnalysis::InductionInfo* next_info = nullptr; |
| 646 | HInductionVarAnalysis::InductionInfo* next_trip = nullptr; |
| 647 | bool next_in_body = true; // inner loop is always in body of outer loop |
| 648 | if (HasInductionInfo(instruction, instruction, &next_loop, &next_info, &next_trip) && |
| 649 | IsWellBehavedTripCount(trip) && |
| 650 | !IsUnsafeTripCount(next_trip)) { |
| 651 | return GetVal(next_info, next_trip, next_in_body, is_min); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 652 | } |
Aart Bik | 40fbf74 | 2016-10-31 11:02:50 -0700 | [diff] [blame] | 653 | // Fetch is represented by itself. |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 654 | return Value(instruction, 1, 0); |
| 655 | } |
| 656 | |
Aart Bik | cd26feb | 2015-09-23 17:50:50 -0700 | [diff] [blame] | 657 | InductionVarRange::Value InductionVarRange::GetVal(HInductionVarAnalysis::InductionInfo* info, |
| 658 | HInductionVarAnalysis::InductionInfo* trip, |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 659 | bool in_body, |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 660 | bool is_min) const { |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 661 | if (info != nullptr) { |
| 662 | switch (info->induction_class) { |
| 663 | case HInductionVarAnalysis::kInvariant: |
| 664 | // Invariants. |
| 665 | switch (info->operation) { |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 666 | case HInductionVarAnalysis::kAdd: |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 667 | return AddValue(GetVal(info->op_a, trip, in_body, is_min), |
| 668 | GetVal(info->op_b, trip, in_body, is_min)); |
Aart Bik | cd26feb | 2015-09-23 17:50:50 -0700 | [diff] [blame] | 669 | case HInductionVarAnalysis::kSub: // second reversed! |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 670 | return SubValue(GetVal(info->op_a, trip, in_body, is_min), |
| 671 | GetVal(info->op_b, trip, in_body, !is_min)); |
Aart Bik | cd26feb | 2015-09-23 17:50:50 -0700 | [diff] [blame] | 672 | case HInductionVarAnalysis::kNeg: // second reversed! |
| 673 | return SubValue(Value(0), |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 674 | GetVal(info->op_b, trip, in_body, !is_min)); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 675 | case HInductionVarAnalysis::kMul: |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 676 | 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] | 677 | case HInductionVarAnalysis::kDiv: |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 678 | return GetDiv(info->op_a, info->op_b, trip, in_body, is_min); |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 679 | case HInductionVarAnalysis::kRem: |
| 680 | return GetRem(info->op_a, info->op_b); |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 681 | case HInductionVarAnalysis::kXor: |
| 682 | return GetXor(info->op_a, info->op_b); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 683 | case HInductionVarAnalysis::kFetch: |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 684 | return GetFetch(info->fetch, trip, in_body, is_min); |
| 685 | case HInductionVarAnalysis::kTripCountInLoop: |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 686 | case HInductionVarAnalysis::kTripCountInLoopUnsafe: |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 687 | if (!in_body && !is_min) { // one extra! |
Aart Bik | 22f0587 | 2015-10-27 15:56:28 -0700 | [diff] [blame] | 688 | return GetVal(info->op_a, trip, in_body, is_min); |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 689 | } |
| 690 | FALLTHROUGH_INTENDED; |
| 691 | case HInductionVarAnalysis::kTripCountInBody: |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 692 | case HInductionVarAnalysis::kTripCountInBodyUnsafe: |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 693 | if (is_min) { |
| 694 | return Value(0); |
| 695 | } else if (in_body) { |
Aart Bik | 22f0587 | 2015-10-27 15:56:28 -0700 | [diff] [blame] | 696 | 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] | 697 | } |
| 698 | break; |
| 699 | default: |
| 700 | break; |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 701 | } |
| 702 | break; |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 703 | case HInductionVarAnalysis::kLinear: |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 704 | return CorrectForType(GetLinear(info, trip, in_body, is_min), info->type); |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 705 | case HInductionVarAnalysis::kPolynomial: |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 706 | return GetPolynomial(info, trip, in_body, is_min); |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 707 | case HInductionVarAnalysis::kGeometric: |
| 708 | return GetGeometric(info, trip, in_body, is_min); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 709 | case HInductionVarAnalysis::kWrapAround: |
| 710 | case HInductionVarAnalysis::kPeriodic: |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 711 | return MergeVal(GetVal(info->op_a, trip, in_body, is_min), |
| 712 | GetVal(info->op_b, trip, in_body, is_min), is_min); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 713 | } |
| 714 | } |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 715 | return Value(); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | InductionVarRange::Value InductionVarRange::GetMul(HInductionVarAnalysis::InductionInfo* info1, |
| 719 | HInductionVarAnalysis::InductionInfo* info2, |
| 720 | HInductionVarAnalysis::InductionInfo* trip, |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 721 | bool in_body, |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 722 | bool is_min) const { |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 723 | // Constant times range. |
| 724 | int64_t value = 0; |
| 725 | if (IsConstant(info1, kExact, &value)) { |
| 726 | return MulRangeAndConstant(value, info2, trip, in_body, is_min); |
| 727 | } else if (IsConstant(info2, kExact, &value)) { |
| 728 | return MulRangeAndConstant(value, info1, trip, in_body, is_min); |
| 729 | } |
| 730 | // Interval ranges. |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 731 | Value v1_min = GetVal(info1, trip, in_body, /* is_min */ true); |
| 732 | Value v1_max = GetVal(info1, trip, in_body, /* is_min */ false); |
| 733 | Value v2_min = GetVal(info2, trip, in_body, /* is_min */ true); |
| 734 | Value v2_max = GetVal(info2, trip, in_body, /* is_min */ false); |
Aart Bik | 97412c9 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 735 | // Positive range vs. positive or negative range. |
| 736 | if (IsConstantValue(v1_min) && v1_min.b_constant >= 0) { |
| 737 | if (IsConstantValue(v2_min) && v2_min.b_constant >= 0) { |
| 738 | return is_min ? MulValue(v1_min, v2_min) : MulValue(v1_max, v2_max); |
| 739 | } else if (IsConstantValue(v2_max) && v2_max.b_constant <= 0) { |
| 740 | 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] | 741 | } |
Aart Bik | 97412c9 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 742 | } |
| 743 | // Negative range vs. positive or negative range. |
| 744 | if (IsConstantValue(v1_max) && v1_max.b_constant <= 0) { |
| 745 | if (IsConstantValue(v2_min) && v2_min.b_constant >= 0) { |
| 746 | return is_min ? MulValue(v1_min, v2_max) : MulValue(v1_max, v2_min); |
| 747 | } else if (IsConstantValue(v2_max) && v2_max.b_constant <= 0) { |
| 748 | 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] | 749 | } |
| 750 | } |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 751 | return Value(); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | InductionVarRange::Value InductionVarRange::GetDiv(HInductionVarAnalysis::InductionInfo* info1, |
| 755 | HInductionVarAnalysis::InductionInfo* info2, |
| 756 | HInductionVarAnalysis::InductionInfo* trip, |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 757 | bool in_body, |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 758 | bool is_min) const { |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 759 | // Range divided by constant. |
| 760 | int64_t value = 0; |
| 761 | if (IsConstant(info2, kExact, &value)) { |
| 762 | return DivRangeAndConstant(value, info1, trip, in_body, is_min); |
| 763 | } |
| 764 | // Interval ranges. |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 765 | Value v1_min = GetVal(info1, trip, in_body, /* is_min */ true); |
| 766 | Value v1_max = GetVal(info1, trip, in_body, /* is_min */ false); |
| 767 | Value v2_min = GetVal(info2, trip, in_body, /* is_min */ true); |
| 768 | Value v2_max = GetVal(info2, trip, in_body, /* is_min */ false); |
Aart Bik | 97412c9 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 769 | // Positive range vs. positive or negative range. |
| 770 | if (IsConstantValue(v1_min) && v1_min.b_constant >= 0) { |
| 771 | if (IsConstantValue(v2_min) && v2_min.b_constant >= 0) { |
| 772 | return is_min ? DivValue(v1_min, v2_max) : DivValue(v1_max, v2_min); |
| 773 | } else if (IsConstantValue(v2_max) && v2_max.b_constant <= 0) { |
| 774 | 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] | 775 | } |
Aart Bik | 97412c9 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 776 | } |
| 777 | // Negative range vs. positive or negative range. |
| 778 | if (IsConstantValue(v1_max) && v1_max.b_constant <= 0) { |
| 779 | if (IsConstantValue(v2_min) && v2_min.b_constant >= 0) { |
| 780 | return is_min ? DivValue(v1_min, v2_min) : DivValue(v1_max, v2_max); |
| 781 | } else if (IsConstantValue(v2_max) && v2_max.b_constant <= 0) { |
| 782 | 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] | 783 | } |
| 784 | } |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 785 | return Value(); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 786 | } |
| 787 | |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 788 | InductionVarRange::Value InductionVarRange::GetRem( |
| 789 | HInductionVarAnalysis::InductionInfo* info1, |
| 790 | HInductionVarAnalysis::InductionInfo* info2) const { |
| 791 | int64_t v1 = 0; |
| 792 | int64_t v2 = 0; |
| 793 | // Only accept exact values. |
| 794 | if (IsConstant(info1, kExact, &v1) && IsConstant(info2, kExact, &v2) && v2 != 0) { |
| 795 | int64_t value = v1 % v2; |
| 796 | if (CanLongValueFitIntoInt(value)) { |
| 797 | return Value(static_cast<int32_t>(value)); |
| 798 | } |
| 799 | } |
| 800 | return Value(); |
| 801 | } |
| 802 | |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 803 | InductionVarRange::Value InductionVarRange::GetXor( |
| 804 | HInductionVarAnalysis::InductionInfo* info1, |
| 805 | HInductionVarAnalysis::InductionInfo* info2) const { |
| 806 | int64_t v1 = 0; |
| 807 | int64_t v2 = 0; |
| 808 | // Only accept exact values. |
| 809 | if (IsConstant(info1, kExact, &v1) && IsConstant(info2, kExact, &v2)) { |
| 810 | int64_t value = v1 ^ v2; |
| 811 | if (CanLongValueFitIntoInt(value)) { |
| 812 | return Value(static_cast<int32_t>(value)); |
| 813 | } |
| 814 | } |
| 815 | return Value(); |
| 816 | } |
| 817 | |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 818 | InductionVarRange::Value InductionVarRange::MulRangeAndConstant( |
| 819 | int64_t value, |
| 820 | HInductionVarAnalysis::InductionInfo* info, |
| 821 | HInductionVarAnalysis::InductionInfo* trip, |
| 822 | bool in_body, |
| 823 | bool is_min) const { |
| 824 | if (CanLongValueFitIntoInt(value)) { |
| 825 | Value c(static_cast<int32_t>(value)); |
| 826 | return MulValue(GetVal(info, trip, in_body, is_min == value >= 0), c); |
| 827 | } |
| 828 | return Value(); |
Aart Bik | 97412c9 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 829 | } |
| 830 | |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 831 | InductionVarRange::Value InductionVarRange::DivRangeAndConstant( |
| 832 | int64_t value, |
| 833 | HInductionVarAnalysis::InductionInfo* info, |
| 834 | HInductionVarAnalysis::InductionInfo* trip, |
| 835 | bool in_body, |
| 836 | bool is_min) const { |
| 837 | if (CanLongValueFitIntoInt(value)) { |
| 838 | Value c(static_cast<int32_t>(value)); |
| 839 | return DivValue(GetVal(info, trip, in_body, is_min == value >= 0), c); |
| 840 | } |
| 841 | return Value(); |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 842 | } |
| 843 | |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 844 | InductionVarRange::Value InductionVarRange::AddValue(Value v1, Value v2) const { |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 845 | 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] | 846 | int32_t b = v1.b_constant + v2.b_constant; |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 847 | if (v1.a_constant == 0) { |
| 848 | return Value(v2.instruction, v2.a_constant, b); |
| 849 | } else if (v2.a_constant == 0) { |
| 850 | return Value(v1.instruction, v1.a_constant, b); |
| 851 | } else if (v1.instruction == v2.instruction && IsSafeAdd(v1.a_constant, v2.a_constant)) { |
| 852 | return Value(v1.instruction, v1.a_constant + v2.a_constant, b); |
| 853 | } |
| 854 | } |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 855 | return Value(); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 856 | } |
| 857 | |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 858 | InductionVarRange::Value InductionVarRange::SubValue(Value v1, Value v2) const { |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 859 | 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] | 860 | int32_t b = v1.b_constant - v2.b_constant; |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 861 | if (v1.a_constant == 0 && IsSafeSub(0, v2.a_constant)) { |
| 862 | return Value(v2.instruction, -v2.a_constant, b); |
| 863 | } else if (v2.a_constant == 0) { |
| 864 | return Value(v1.instruction, v1.a_constant, b); |
| 865 | } else if (v1.instruction == v2.instruction && IsSafeSub(v1.a_constant, v2.a_constant)) { |
| 866 | return Value(v1.instruction, v1.a_constant - v2.a_constant, b); |
| 867 | } |
| 868 | } |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 869 | return Value(); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 870 | } |
| 871 | |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 872 | InductionVarRange::Value InductionVarRange::MulValue(Value v1, Value v2) const { |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 873 | if (v1.is_known && v2.is_known) { |
| 874 | if (v1.a_constant == 0) { |
| 875 | if (IsSafeMul(v1.b_constant, v2.a_constant) && IsSafeMul(v1.b_constant, v2.b_constant)) { |
| 876 | return Value(v2.instruction, v1.b_constant * v2.a_constant, v1.b_constant * v2.b_constant); |
| 877 | } |
| 878 | } else if (v2.a_constant == 0) { |
| 879 | if (IsSafeMul(v1.a_constant, v2.b_constant) && IsSafeMul(v1.b_constant, v2.b_constant)) { |
| 880 | return Value(v1.instruction, v1.a_constant * v2.b_constant, v1.b_constant * v2.b_constant); |
| 881 | } |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 882 | } |
| 883 | } |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 884 | return Value(); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 885 | } |
| 886 | |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 887 | InductionVarRange::Value InductionVarRange::DivValue(Value v1, Value v2) const { |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 888 | 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] | 889 | if (IsSafeDiv(v1.b_constant, v2.b_constant)) { |
| 890 | return Value(v1.b_constant / v2.b_constant); |
| 891 | } |
| 892 | } |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 893 | return Value(); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 894 | } |
| 895 | |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 896 | InductionVarRange::Value InductionVarRange::MergeVal(Value v1, Value v2, bool is_min) const { |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 897 | if (v1.is_known && v2.is_known) { |
| 898 | if (v1.instruction == v2.instruction && v1.a_constant == v2.a_constant) { |
Aart Bik | cd26feb | 2015-09-23 17:50:50 -0700 | [diff] [blame] | 899 | return Value(v1.instruction, v1.a_constant, |
| 900 | is_min ? std::min(v1.b_constant, v2.b_constant) |
| 901 | : std::max(v1.b_constant, v2.b_constant)); |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 902 | } |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 903 | } |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 904 | return Value(); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 905 | } |
| 906 | |
Aart Bik | 9abf894 | 2016-10-14 09:49:42 -0700 | [diff] [blame] | 907 | bool InductionVarRange::GenerateRangeOrLastValue(HInstruction* context, |
| 908 | HInstruction* instruction, |
| 909 | bool is_last_value, |
| 910 | HGraph* graph, |
| 911 | HBasicBlock* block, |
| 912 | /*out*/HInstruction** lower, |
| 913 | /*out*/HInstruction** upper, |
| 914 | /*out*/HInstruction** taken_test, |
| 915 | /*out*/int64_t* stride_value, |
| 916 | /*out*/bool* needs_finite_test, |
| 917 | /*out*/bool* needs_taken_test) const { |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 918 | HLoopInformation* loop = nullptr; |
| 919 | HInductionVarAnalysis::InductionInfo* info = nullptr; |
| 920 | HInductionVarAnalysis::InductionInfo* trip = nullptr; |
| 921 | if (!HasInductionInfo(context, instruction, &loop, &info, &trip) || trip == nullptr) { |
| 922 | return false; // codegen needs all information, including tripcount |
Aart Bik | 97412c9 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 923 | } |
| 924 | // Determine what tests are needed. A finite test is needed if the evaluation code uses the |
| 925 | // trip-count and the loop maybe unsafe (because in such cases, the index could "overshoot" |
| 926 | // the computed range). A taken test is needed for any unknown trip-count, even if evaluation |
| 927 | // code does not use the trip-count explicitly (since there could be an implicit relation |
| 928 | // between e.g. an invariant subscript and a not-taken condition). |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 929 | bool in_body = context->GetBlock() != loop->GetHeader(); |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 930 | *stride_value = 0; |
| 931 | *needs_finite_test = NeedsTripCount(info, stride_value) && IsUnsafeTripCount(trip); |
Aart Bik | 97412c9 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 932 | *needs_taken_test = IsBodyTripCount(trip); |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 933 | // Handle last value request. |
| 934 | if (is_last_value) { |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 935 | DCHECK(!in_body); |
| 936 | switch (info->induction_class) { |
| 937 | case HInductionVarAnalysis::kLinear: |
| 938 | if (*stride_value > 0) { |
| 939 | lower = nullptr; |
| 940 | } else { |
| 941 | upper = nullptr; |
| 942 | } |
| 943 | break; |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 944 | case HInductionVarAnalysis::kPolynomial: |
| 945 | return GenerateLastValuePolynomial(info, trip, graph, block, lower); |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 946 | case HInductionVarAnalysis::kGeometric: |
| 947 | return GenerateLastValueGeometric(info, trip, graph, block, lower); |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 948 | case HInductionVarAnalysis::kWrapAround: |
| 949 | return GenerateLastValueWrapAround(info, trip, graph, block, lower); |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 950 | case HInductionVarAnalysis::kPeriodic: |
| 951 | return GenerateLastValuePeriodic(info, trip, graph, block, lower, needs_taken_test); |
| 952 | default: |
| 953 | return false; |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 954 | } |
| 955 | } |
Aart Bik | 97412c9 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 956 | // Code generation for taken test: generate the code when requested or otherwise analyze |
| 957 | // if code generation is feasible when taken test is needed. |
| 958 | if (taken_test != nullptr) { |
| 959 | return GenerateCode(trip->op_b, nullptr, graph, block, taken_test, in_body, /* is_min */ false); |
| 960 | } else if (*needs_taken_test) { |
| 961 | if (!GenerateCode( |
| 962 | trip->op_b, nullptr, nullptr, nullptr, nullptr, in_body, /* is_min */ false)) { |
| 963 | return false; |
| 964 | } |
| 965 | } |
| 966 | // Code generation for lower and upper. |
| 967 | return |
| 968 | // Success on lower if invariant (not set), or code can be generated. |
| 969 | ((info->induction_class == HInductionVarAnalysis::kInvariant) || |
| 970 | GenerateCode(info, trip, graph, block, lower, in_body, /* is_min */ true)) && |
| 971 | // And success on upper. |
| 972 | GenerateCode(info, trip, graph, block, upper, in_body, /* is_min */ false); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 973 | } |
| 974 | |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 975 | bool InductionVarRange::GenerateLastValuePolynomial(HInductionVarAnalysis::InductionInfo* info, |
| 976 | HInductionVarAnalysis::InductionInfo* trip, |
| 977 | HGraph* graph, |
| 978 | HBasicBlock* block, |
| 979 | /*out*/HInstruction** result) const { |
| 980 | DCHECK(info != nullptr); |
| 981 | DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kPolynomial); |
| 982 | // Detect known coefficients and trip count (always taken). |
| 983 | int64_t a = 0; |
| 984 | int64_t b = 0; |
| 985 | int64_t m = 0; |
Aart Bik | d0a022d | 2016-12-13 11:22:31 -0800 | [diff] [blame] | 986 | if (IsConstant(info->op_a->op_a, kExact, &a) && |
| 987 | IsConstant(info->op_a->op_b, kExact, &b) && |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 988 | IsConstant(trip->op_a, kExact, &m) && m >= 1) { |
Aart Bik | d0a022d | 2016-12-13 11:22:31 -0800 | [diff] [blame] | 989 | // 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] | 990 | // 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] | 991 | HInstruction* c = nullptr; |
| 992 | if (GenerateCode(info->op_b, nullptr, graph, block, graph ? &c : nullptr, false, false)) { |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 993 | if (graph != nullptr) { |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 994 | Primitive::Type type = info->type; |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 995 | int64_t sum = a * ((m * (m - 1)) / 2) + b * m; |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 996 | if (type != Primitive::kPrimLong) { |
| 997 | sum = static_cast<int32_t>(sum); // okay to truncate |
| 998 | } |
| 999 | *result = |
| 1000 | Insert(block, new (graph->GetArena()) HAdd(type, graph->GetConstant(type, sum), c)); |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 1001 | } |
| 1002 | return true; |
| 1003 | } |
| 1004 | } |
| 1005 | return false; |
| 1006 | } |
| 1007 | |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 1008 | bool InductionVarRange::GenerateLastValueGeometric(HInductionVarAnalysis::InductionInfo* info, |
| 1009 | HInductionVarAnalysis::InductionInfo* trip, |
| 1010 | HGraph* graph, |
| 1011 | HBasicBlock* block, |
| 1012 | /*out*/HInstruction** result) const { |
| 1013 | DCHECK(info != nullptr); |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 1014 | DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kGeometric); |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 1015 | // Detect known base and trip count (always taken). |
| 1016 | int64_t f = 0; |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 1017 | int64_t m = 0; |
| 1018 | if (IsIntAndGet(info->fetch, &f) && f >= 1 && IsConstant(trip->op_a, kExact, &m) && m >= 1) { |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 1019 | HInstruction* opa = nullptr; |
| 1020 | HInstruction* opb = nullptr; |
| 1021 | if (GenerateCode(info->op_a, nullptr, graph, block, &opa, false, false) && |
| 1022 | GenerateCode(info->op_b, nullptr, graph, block, &opb, false, false)) { |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 1023 | // Compute f ^ m for known maximum index value m. |
| 1024 | int64_t fpow = IntPow(f, m); |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 1025 | if (graph != nullptr) { |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 1026 | DCHECK(info->operation == HInductionVarAnalysis::kMul || |
| 1027 | info->operation == HInductionVarAnalysis::kDiv); |
| 1028 | Primitive::Type type = info->type; |
| 1029 | if (fpow == 0) { |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 1030 | // Special case: repeated mul/div always yields zero. |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 1031 | *result = graph->GetConstant(type, 0); |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 1032 | } else { |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 1033 | // Last value: a * f ^ m + b or a * f ^ -m + b. |
| 1034 | if (type != Primitive::kPrimLong) { |
| 1035 | fpow = static_cast<int32_t>(fpow); // okay to truncate |
| 1036 | } |
| 1037 | HInstruction* e = nullptr; |
| 1038 | if (info->operation == HInductionVarAnalysis::kMul) { |
| 1039 | e = new (graph->GetArena()) HMul(type, opa, graph->GetConstant(type, fpow)); |
| 1040 | } else { |
| 1041 | e = new (graph->GetArena()) HDiv(type, opa, graph->GetConstant(type, fpow), kNoDexPc); |
| 1042 | } |
| 1043 | *result = Insert(block, new (graph->GetArena()) HAdd(type, Insert(block, e), opb)); |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 1044 | } |
| 1045 | } |
| 1046 | return true; |
| 1047 | } |
| 1048 | } |
| 1049 | return false; |
| 1050 | } |
| 1051 | |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 1052 | bool InductionVarRange::GenerateLastValueWrapAround(HInductionVarAnalysis::InductionInfo* info, |
| 1053 | HInductionVarAnalysis::InductionInfo* trip, |
| 1054 | HGraph* graph, |
| 1055 | HBasicBlock* block, |
| 1056 | /*out*/HInstruction** result) const { |
| 1057 | DCHECK(info != nullptr); |
| 1058 | DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kWrapAround); |
| 1059 | // Count depth. |
| 1060 | int32_t depth = 0; |
| 1061 | for (; info->induction_class == HInductionVarAnalysis::kWrapAround; |
| 1062 | info = info->op_b, ++depth) {} |
| 1063 | // 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] | 1064 | // TODO: generalize, but be careful to adjust the terminal. |
| 1065 | int64_t m = 0; |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 1066 | if (info->induction_class == HInductionVarAnalysis::kInvariant && |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 1067 | IsConstant(trip->op_a, kExact, &m) && m >= depth) { |
| 1068 | return GenerateCode(info, nullptr, graph, block, result, false, false); |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 1069 | } |
| 1070 | return false; |
| 1071 | } |
| 1072 | |
Aart Bik | 9abf894 | 2016-10-14 09:49:42 -0700 | [diff] [blame] | 1073 | bool InductionVarRange::GenerateLastValuePeriodic(HInductionVarAnalysis::InductionInfo* info, |
| 1074 | HInductionVarAnalysis::InductionInfo* trip, |
| 1075 | HGraph* graph, |
| 1076 | HBasicBlock* block, |
| 1077 | /*out*/HInstruction** result, |
| 1078 | /*out*/bool* needs_taken_test) const { |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 1079 | DCHECK(info != nullptr); |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 1080 | DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kPeriodic); |
Aart Bik | 9abf894 | 2016-10-14 09:49:42 -0700 | [diff] [blame] | 1081 | // Count period. |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 1082 | int64_t period = 1; |
Aart Bik | 9abf894 | 2016-10-14 09:49:42 -0700 | [diff] [blame] | 1083 | for (HInductionVarAnalysis::InductionInfo* p = info; |
| 1084 | p->induction_class == HInductionVarAnalysis::kPeriodic; |
| 1085 | p = p->op_b, ++period) {} |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 1086 | // Handle any periodic(x, periodic(.., y)) for known maximum index value m. |
| 1087 | int64_t m = 0; |
| 1088 | if (IsConstant(trip->op_a, kExact, &m) && m >= 1) { |
| 1089 | int64_t li = m % period; |
| 1090 | for (int64_t i = 0; i < li; info = info->op_b, i++) {} |
| 1091 | if (info->induction_class == HInductionVarAnalysis::kPeriodic) { |
| 1092 | info = info->op_a; |
| 1093 | } |
| 1094 | return GenerateCode(info, nullptr, graph, block, result, false, false); |
Aart Bik | 9abf894 | 2016-10-14 09:49:42 -0700 | [diff] [blame] | 1095 | } |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 1096 | // Handle periodic(x, y) using even/odd-select on trip count. Enter trip count expression |
| 1097 | // directly to obtain the maximum index value t even if taken test is needed. |
| 1098 | HInstruction* x = nullptr; |
| 1099 | HInstruction* y = nullptr; |
| 1100 | HInstruction* t = nullptr; |
| 1101 | if (period == 2 && |
| 1102 | GenerateCode(info->op_a, nullptr, graph, block, graph ? &x : nullptr, false, false) && |
| 1103 | GenerateCode(info->op_b, nullptr, graph, block, graph ? &y : nullptr, false, false) && |
| 1104 | GenerateCode(trip->op_a, nullptr, graph, block, graph ? &t : nullptr, false, false)) { |
| 1105 | // During actual code generation (graph != nullptr), generate is_even ? x : y. |
Aart Bik | 9abf894 | 2016-10-14 09:49:42 -0700 | [diff] [blame] | 1106 | if (graph != nullptr) { |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 1107 | Primitive::Type type = trip->type; |
| 1108 | HInstruction* msk = |
| 1109 | Insert(block, new (graph->GetArena()) HAnd(type, t, graph->GetConstant(type, 1))); |
| 1110 | HInstruction* is_even = |
| 1111 | Insert(block, new (graph->GetArena()) HEqual(msk, graph->GetConstant(type, 0), kNoDexPc)); |
| 1112 | *result = Insert(block, new (graph->GetArena()) HSelect(is_even, x, y, kNoDexPc)); |
Aart Bik | 9abf894 | 2016-10-14 09:49:42 -0700 | [diff] [blame] | 1113 | } |
| 1114 | // Guard select with taken test if needed. |
| 1115 | if (*needs_taken_test) { |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 1116 | HInstruction* is_taken = nullptr; |
| 1117 | if (GenerateCode(trip->op_b, nullptr, graph, block, graph ? &is_taken : nullptr, false, false)) { |
| 1118 | if (graph != nullptr) { |
| 1119 | *result = Insert(block, new (graph->GetArena()) HSelect(is_taken, *result, x, kNoDexPc)); |
| 1120 | } |
| 1121 | *needs_taken_test = false; // taken care of |
| 1122 | } else { |
Aart Bik | 9abf894 | 2016-10-14 09:49:42 -0700 | [diff] [blame] | 1123 | return false; |
Aart Bik | 9abf894 | 2016-10-14 09:49:42 -0700 | [diff] [blame] | 1124 | } |
Aart Bik | 9abf894 | 2016-10-14 09:49:42 -0700 | [diff] [blame] | 1125 | } |
| 1126 | return true; |
| 1127 | } |
| 1128 | return false; |
| 1129 | } |
| 1130 | |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 1131 | bool InductionVarRange::GenerateCode(HInductionVarAnalysis::InductionInfo* info, |
| 1132 | HInductionVarAnalysis::InductionInfo* trip, |
| 1133 | HGraph* graph, // when set, code is generated |
| 1134 | HBasicBlock* block, |
| 1135 | /*out*/HInstruction** result, |
| 1136 | bool in_body, |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 1137 | bool is_min) const { |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 1138 | if (info != nullptr) { |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 1139 | // If during codegen, the result is not needed (nullptr), simply return success. |
| 1140 | if (graph != nullptr && result == nullptr) { |
| 1141 | return true; |
| 1142 | } |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 1143 | // Handle current operation. |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 1144 | Primitive::Type type = info->type; |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 1145 | HInstruction* opa = nullptr; |
| 1146 | HInstruction* opb = nullptr; |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 1147 | switch (info->induction_class) { |
| 1148 | case HInductionVarAnalysis::kInvariant: |
Aart Bik | 40fbf74 | 2016-10-31 11:02:50 -0700 | [diff] [blame] | 1149 | // Invariants (note that even though is_min does not impact code generation for |
| 1150 | // invariants, some effort is made to keep this parameter consistent). |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 1151 | switch (info->operation) { |
| 1152 | case HInductionVarAnalysis::kAdd: |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 1153 | case HInductionVarAnalysis::kRem: // no proper is_min for second arg |
Aart Bik | 40fbf74 | 2016-10-31 11:02:50 -0700 | [diff] [blame] | 1154 | case HInductionVarAnalysis::kXor: // no proper is_min for second arg |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 1155 | case HInductionVarAnalysis::kLT: |
| 1156 | case HInductionVarAnalysis::kLE: |
| 1157 | case HInductionVarAnalysis::kGT: |
| 1158 | case HInductionVarAnalysis::kGE: |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 1159 | if (GenerateCode(info->op_a, trip, graph, block, &opa, in_body, is_min) && |
| 1160 | GenerateCode(info->op_b, trip, graph, block, &opb, in_body, is_min)) { |
| 1161 | if (graph != nullptr) { |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 1162 | HInstruction* operation = nullptr; |
| 1163 | switch (info->operation) { |
| 1164 | case HInductionVarAnalysis::kAdd: |
| 1165 | operation = new (graph->GetArena()) HAdd(type, opa, opb); break; |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 1166 | case HInductionVarAnalysis::kRem: |
| 1167 | operation = new (graph->GetArena()) HRem(type, opa, opb, kNoDexPc); break; |
Aart Bik | 9abf894 | 2016-10-14 09:49:42 -0700 | [diff] [blame] | 1168 | case HInductionVarAnalysis::kXor: |
| 1169 | operation = new (graph->GetArena()) HXor(type, opa, opb); break; |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 1170 | case HInductionVarAnalysis::kLT: |
| 1171 | operation = new (graph->GetArena()) HLessThan(opa, opb); break; |
| 1172 | case HInductionVarAnalysis::kLE: |
| 1173 | operation = new (graph->GetArena()) HLessThanOrEqual(opa, opb); break; |
| 1174 | case HInductionVarAnalysis::kGT: |
| 1175 | operation = new (graph->GetArena()) HGreaterThan(opa, opb); break; |
| 1176 | case HInductionVarAnalysis::kGE: |
| 1177 | operation = new (graph->GetArena()) HGreaterThanOrEqual(opa, opb); break; |
| 1178 | default: |
| 1179 | LOG(FATAL) << "unknown operation"; |
| 1180 | } |
| 1181 | *result = Insert(block, operation); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 1182 | } |
| 1183 | return true; |
| 1184 | } |
| 1185 | break; |
| 1186 | case HInductionVarAnalysis::kSub: // second reversed! |
| 1187 | if (GenerateCode(info->op_a, trip, graph, block, &opa, in_body, is_min) && |
| 1188 | GenerateCode(info->op_b, trip, graph, block, &opb, in_body, !is_min)) { |
| 1189 | if (graph != nullptr) { |
| 1190 | *result = Insert(block, new (graph->GetArena()) HSub(type, opa, opb)); |
| 1191 | } |
| 1192 | return true; |
| 1193 | } |
| 1194 | break; |
| 1195 | case HInductionVarAnalysis::kNeg: // reversed! |
| 1196 | if (GenerateCode(info->op_b, trip, graph, block, &opb, in_body, !is_min)) { |
| 1197 | if (graph != nullptr) { |
| 1198 | *result = Insert(block, new (graph->GetArena()) HNeg(type, opb)); |
| 1199 | } |
| 1200 | return true; |
| 1201 | } |
| 1202 | break; |
| 1203 | case HInductionVarAnalysis::kFetch: |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 1204 | if (graph != nullptr) { |
| 1205 | *result = info->fetch; // already in HIR |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 1206 | } |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 1207 | return true; |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 1208 | case HInductionVarAnalysis::kTripCountInLoop: |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 1209 | case HInductionVarAnalysis::kTripCountInLoopUnsafe: |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 1210 | if (!in_body && !is_min) { // one extra! |
Aart Bik | 22f0587 | 2015-10-27 15:56:28 -0700 | [diff] [blame] | 1211 | 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] | 1212 | } |
| 1213 | FALLTHROUGH_INTENDED; |
| 1214 | case HInductionVarAnalysis::kTripCountInBody: |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 1215 | case HInductionVarAnalysis::kTripCountInBodyUnsafe: |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 1216 | if (is_min) { |
| 1217 | if (graph != nullptr) { |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 1218 | *result = graph->GetConstant(type, 0); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 1219 | } |
| 1220 | return true; |
| 1221 | } else if (in_body) { |
Aart Bik | 22f0587 | 2015-10-27 15:56:28 -0700 | [diff] [blame] | 1222 | 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] | 1223 | if (graph != nullptr) { |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 1224 | *result = |
| 1225 | Insert(block, |
| 1226 | new (graph->GetArena()) HSub(type, opb, graph->GetConstant(type, 1))); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 1227 | } |
| 1228 | return true; |
| 1229 | } |
| 1230 | } |
| 1231 | break; |
| 1232 | default: |
| 1233 | break; |
| 1234 | } |
| 1235 | break; |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 1236 | case HInductionVarAnalysis::kLinear: { |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 1237 | // Linear induction a * i + b, for normalized 0 <= i < TC. For ranges, this should |
| 1238 | // be restricted to a unit stride to avoid arithmetic wrap-around situations that |
| 1239 | // 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] | 1240 | // known stride yields right value. Always avoid any narrowing linear induction or |
| 1241 | // any type mismatch between the linear induction and the trip count expression. |
| 1242 | // TODO: careful runtime type conversions could generalize this latter restriction. |
| 1243 | if (!HInductionVarAnalysis::IsNarrowingLinear(info) && trip->type == type) { |
| 1244 | int64_t stride_value = 0; |
| 1245 | if (IsConstant(info->op_a, kExact, &stride_value) && |
| 1246 | CanLongValueFitIntoInt(stride_value)) { |
| 1247 | const bool is_min_a = stride_value >= 0 ? is_min : !is_min; |
| 1248 | if (GenerateCode(trip, trip, graph, block, &opa, in_body, is_min_a) && |
| 1249 | GenerateCode(info->op_b, trip, graph, block, &opb, in_body, is_min)) { |
| 1250 | if (graph != nullptr) { |
| 1251 | HInstruction* oper; |
| 1252 | if (stride_value == 1) { |
| 1253 | oper = new (graph->GetArena()) HAdd(type, opa, opb); |
| 1254 | } else if (stride_value == -1) { |
| 1255 | oper = new (graph->GetArena()) HSub(type, opb, opa); |
| 1256 | } else { |
| 1257 | HInstruction* mul = |
| 1258 | new (graph->GetArena()) HMul(type, graph->GetConstant(type, stride_value), opa); |
| 1259 | oper = new (graph->GetArena()) HAdd(type, Insert(block, mul), opb); |
| 1260 | } |
| 1261 | *result = Insert(block, oper); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 1262 | } |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 1263 | return true; |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 1264 | } |
| 1265 | } |
| 1266 | } |
| 1267 | break; |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 1268 | } |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 1269 | case HInductionVarAnalysis::kPolynomial: |
| 1270 | case HInductionVarAnalysis::kGeometric: |
| 1271 | break; |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 1272 | case HInductionVarAnalysis::kWrapAround: |
| 1273 | case HInductionVarAnalysis::kPeriodic: { |
| 1274 | // Wrap-around and periodic inductions are restricted to constants only, so that extreme |
| 1275 | // values are easy to test at runtime without complications of arithmetic wrap-around. |
| 1276 | Value extreme = GetVal(info, trip, in_body, is_min); |
Aart Bik | 97412c9 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 1277 | if (IsConstantValue(extreme)) { |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 1278 | if (graph != nullptr) { |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 1279 | *result = graph->GetConstant(type, extreme.b_constant); |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 1280 | } |
| 1281 | return true; |
| 1282 | } |
| 1283 | break; |
| 1284 | } |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 1285 | } |
| 1286 | } |
| 1287 | return false; |
| 1288 | } |
| 1289 | |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 1290 | void InductionVarRange::ReplaceInduction(HInductionVarAnalysis::InductionInfo* info, |
| 1291 | HInstruction* fetch, |
| 1292 | HInstruction* replacement) { |
| 1293 | if (info != nullptr) { |
| 1294 | if (info->induction_class == HInductionVarAnalysis::kInvariant && |
| 1295 | info->operation == HInductionVarAnalysis::kFetch && |
| 1296 | info->fetch == fetch) { |
| 1297 | info->fetch = replacement; |
| 1298 | } |
| 1299 | ReplaceInduction(info->op_a, fetch, replacement); |
| 1300 | ReplaceInduction(info->op_b, fetch, replacement); |
| 1301 | } |
| 1302 | } |
| 1303 | |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 1304 | } // namespace art |