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