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