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 | b603a5c | 2017-03-06 18:29:39 -0800 | [diff] [blame] | 48 | /** Computes a * b for a,b > 0 (at least until first overflow happens). */ |
| 49 | static int64_t SafeMul(int64_t a, int64_t b, /*out*/ bool* overflow) { |
| 50 | if (a > 0 && b > 0 && a > (std::numeric_limits<int64_t>::max() / b)) { |
| 51 | *overflow = true; |
| 52 | } |
| 53 | return a * b; |
| 54 | } |
| 55 | |
| 56 | /** Returns b^e for b,e > 0. Sets overflow if arithmetic wrap-around occurred. */ |
Aart Bik | d3ba626 | 2017-01-30 14:37:12 -0800 | [diff] [blame] | 57 | static int64_t IntPow(int64_t b, int64_t e, /*out*/ bool* overflow) { |
Aart Bik | b603a5c | 2017-03-06 18:29:39 -0800 | [diff] [blame] | 58 | DCHECK_LT(0, b); |
| 59 | DCHECK_LT(0, e); |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 60 | int64_t pow = 1; |
| 61 | while (e) { |
| 62 | if (e & 1) { |
Aart Bik | b603a5c | 2017-03-06 18:29:39 -0800 | [diff] [blame] | 63 | pow = SafeMul(pow, b, overflow); |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 64 | } |
| 65 | e >>= 1; |
Aart Bik | b603a5c | 2017-03-06 18:29:39 -0800 | [diff] [blame] | 66 | if (e) { |
| 67 | b = SafeMul(b, b, overflow); |
| 68 | } |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 69 | } |
| 70 | return pow; |
| 71 | } |
| 72 | |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 73 | /** |
Aart Bik | 40fbf74 | 2016-10-31 11:02:50 -0700 | [diff] [blame] | 74 | * Detects an instruction that is >= 0. As long as the value is carried by |
| 75 | * a single instruction, arithmetic wrap-around cannot occur. |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 76 | */ |
Aart Bik | 40fbf74 | 2016-10-31 11:02:50 -0700 | [diff] [blame] | 77 | static bool IsGEZero(HInstruction* instruction) { |
| 78 | DCHECK(instruction != nullptr); |
| 79 | if (instruction->IsArrayLength()) { |
| 80 | return true; |
| 81 | } else if (instruction->IsInvokeStaticOrDirect()) { |
| 82 | switch (instruction->AsInvoke()->GetIntrinsic()) { |
| 83 | case Intrinsics::kMathMinIntInt: |
| 84 | case Intrinsics::kMathMinLongLong: |
| 85 | // Instruction MIN(>=0, >=0) is >= 0. |
| 86 | return IsGEZero(instruction->InputAt(0)) && |
| 87 | IsGEZero(instruction->InputAt(1)); |
| 88 | case Intrinsics::kMathAbsInt: |
| 89 | case Intrinsics::kMathAbsLong: |
Aart Bik | 7f56ff4 | 2017-08-30 10:20:47 -0700 | [diff] [blame] | 90 | // Instruction ABS(>=0) is >= 0. |
| 91 | // NOTE: ABS(minint) = minint prevents assuming |
| 92 | // >= 0 without looking at the argument. |
| 93 | return IsGEZero(instruction->InputAt(0)); |
Aart Bik | 40fbf74 | 2016-10-31 11:02:50 -0700 | [diff] [blame] | 94 | default: |
| 95 | break; |
| 96 | } |
| 97 | } |
| 98 | int64_t value = -1; |
Aart Bik | f3e61ee | 2017-04-12 17:09:20 -0700 | [diff] [blame] | 99 | return IsInt64AndGet(instruction, &value) && value >= 0; |
Aart Bik | 40fbf74 | 2016-10-31 11:02:50 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | /** Hunts "under the hood" for a suitable instruction at the hint. */ |
| 103 | static bool IsMaxAtHint( |
| 104 | HInstruction* instruction, HInstruction* hint, /*out*/HInstruction** suitable) { |
| 105 | if (instruction->IsInvokeStaticOrDirect()) { |
| 106 | switch (instruction->AsInvoke()->GetIntrinsic()) { |
| 107 | case Intrinsics::kMathMinIntInt: |
| 108 | case Intrinsics::kMathMinLongLong: |
| 109 | // For MIN(x, y), return most suitable x or y as maximum. |
| 110 | return IsMaxAtHint(instruction->InputAt(0), hint, suitable) || |
| 111 | IsMaxAtHint(instruction->InputAt(1), hint, suitable); |
| 112 | default: |
| 113 | break; |
| 114 | } |
| 115 | } else { |
| 116 | *suitable = instruction; |
Nicolas Geoffray | e761bcc | 2017-01-19 08:59:37 +0000 | [diff] [blame] | 117 | return HuntForDeclaration(instruction) == hint; |
Aart Bik | 40fbf74 | 2016-10-31 11:02:50 -0700 | [diff] [blame] | 118 | } |
| 119 | return false; |
| 120 | } |
| 121 | |
| 122 | /** Post-analysis simplification of a minimum value that makes the bound more useful to clients. */ |
| 123 | static InductionVarRange::Value SimplifyMin(InductionVarRange::Value v) { |
| 124 | if (v.is_known && v.a_constant == 1 && v.b_constant <= 0) { |
| 125 | // If a == 1, instruction >= 0 and b <= 0, just return the constant b. |
| 126 | // No arithmetic wrap-around can occur. |
| 127 | if (IsGEZero(v.instruction)) { |
| 128 | return InductionVarRange::Value(v.b_constant); |
| 129 | } |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 130 | } |
| 131 | return v; |
| 132 | } |
| 133 | |
Aart Bik | 40fbf74 | 2016-10-31 11:02:50 -0700 | [diff] [blame] | 134 | /** Post-analysis simplification of a maximum value that makes the bound more useful to clients. */ |
| 135 | static InductionVarRange::Value SimplifyMax(InductionVarRange::Value v, HInstruction* hint) { |
| 136 | if (v.is_known && v.a_constant >= 1) { |
| 137 | // An upper bound a * (length / a) + b, where a >= 1, can be conservatively rewritten as |
| 138 | // length + b because length >= 0 is true. |
| 139 | int64_t value; |
| 140 | if (v.instruction->IsDiv() && |
| 141 | v.instruction->InputAt(0)->IsArrayLength() && |
Aart Bik | f3e61ee | 2017-04-12 17:09:20 -0700 | [diff] [blame] | 142 | IsInt64AndGet(v.instruction->InputAt(1), &value) && v.a_constant == value) { |
Aart Bik | 40fbf74 | 2016-10-31 11:02:50 -0700 | [diff] [blame] | 143 | return InductionVarRange::Value(v.instruction->InputAt(0), 1, v.b_constant); |
| 144 | } |
| 145 | // If a == 1, the most suitable one suffices as maximum value. |
| 146 | HInstruction* suitable = nullptr; |
| 147 | if (v.a_constant == 1 && IsMaxAtHint(v.instruction, hint, &suitable)) { |
| 148 | return InductionVarRange::Value(suitable, 1, v.b_constant); |
| 149 | } |
| 150 | } |
| 151 | return v; |
| 152 | } |
| 153 | |
| 154 | /** Tests for a constant value. */ |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 155 | static bool IsConstantValue(InductionVarRange::Value v) { |
| 156 | return v.is_known && v.a_constant == 0; |
| 157 | } |
| 158 | |
| 159 | /** Corrects a value for type to account for arithmetic wrap-around in lower precision. */ |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 160 | static InductionVarRange::Value CorrectForType(InductionVarRange::Value v, Primitive::Type type) { |
| 161 | switch (type) { |
| 162 | case Primitive::kPrimShort: |
| 163 | case Primitive::kPrimChar: |
| 164 | case Primitive::kPrimByte: { |
| 165 | // Constants within range only. |
| 166 | // TODO: maybe some room for improvement, like allowing widening conversions |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 167 | int32_t min = Primitive::MinValueOfIntegralType(type); |
| 168 | int32_t max = Primitive::MaxValueOfIntegralType(type); |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 169 | return (IsConstantValue(v) && min <= v.b_constant && v.b_constant <= max) |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 170 | ? v |
| 171 | : InductionVarRange::Value(); |
| 172 | } |
| 173 | default: |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 174 | return v; |
| 175 | } |
| 176 | } |
| 177 | |
Aart Bik | 40fbf74 | 2016-10-31 11:02:50 -0700 | [diff] [blame] | 178 | /** Inserts an instruction. */ |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 179 | static HInstruction* Insert(HBasicBlock* block, HInstruction* instruction) { |
| 180 | DCHECK(block != nullptr); |
| 181 | DCHECK(block->GetLastInstruction() != nullptr) << block->GetBlockId(); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 182 | DCHECK(instruction != nullptr); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 183 | block->InsertInstructionBefore(instruction, block->GetLastInstruction()); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 184 | return instruction; |
| 185 | } |
| 186 | |
Aart Bik | 40fbf74 | 2016-10-31 11:02:50 -0700 | [diff] [blame] | 187 | /** Obtains loop's control instruction. */ |
Aart Bik | 009cace | 2016-09-16 10:15:19 -0700 | [diff] [blame] | 188 | static HInstruction* GetLoopControl(HLoopInformation* loop) { |
| 189 | DCHECK(loop != nullptr); |
| 190 | return loop->GetHeader()->GetLastInstruction(); |
| 191 | } |
| 192 | |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 193 | // |
| 194 | // Public class methods. |
| 195 | // |
| 196 | |
| 197 | InductionVarRange::InductionVarRange(HInductionVarAnalysis* induction_analysis) |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 198 | : induction_analysis_(induction_analysis), |
| 199 | chase_hint_(nullptr) { |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 200 | DCHECK(induction_analysis != nullptr); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 201 | } |
| 202 | |
Aart Bik | 1fc3afb | 2016-02-02 13:26:16 -0800 | [diff] [blame] | 203 | bool InductionVarRange::GetInductionRange(HInstruction* context, |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 204 | HInstruction* instruction, |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 205 | HInstruction* chase_hint, |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 206 | /*out*/Value* min_val, |
| 207 | /*out*/Value* max_val, |
| 208 | /*out*/bool* needs_finite_test) { |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 209 | HLoopInformation* loop = nullptr; |
| 210 | HInductionVarAnalysis::InductionInfo* info = nullptr; |
| 211 | HInductionVarAnalysis::InductionInfo* trip = nullptr; |
| 212 | if (!HasInductionInfo(context, instruction, &loop, &info, &trip)) { |
| 213 | return false; |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 214 | } |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 215 | // Type int or lower (this is not too restrictive since intended clients, like |
| 216 | // bounds check elimination, will have truncated higher precision induction |
| 217 | // at their use point already). |
| 218 | switch (info->type) { |
| 219 | case Primitive::kPrimInt: |
| 220 | case Primitive::kPrimShort: |
| 221 | case Primitive::kPrimChar: |
| 222 | case Primitive::kPrimByte: |
| 223 | break; |
| 224 | default: |
| 225 | return false; |
| 226 | } |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 227 | // Find range. |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 228 | chase_hint_ = chase_hint; |
| 229 | bool in_body = context->GetBlock() != loop->GetHeader(); |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 230 | int64_t stride_value = 0; |
Aart Bik | 40fbf74 | 2016-10-31 11:02:50 -0700 | [diff] [blame] | 231 | *min_val = SimplifyMin(GetVal(info, trip, in_body, /* is_min */ true)); |
| 232 | *max_val = SimplifyMax(GetVal(info, trip, in_body, /* is_min */ false), chase_hint); |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 233 | *needs_finite_test = NeedsTripCount(info, &stride_value) && IsUnsafeTripCount(trip); |
Aart Bik | 40fbf74 | 2016-10-31 11:02:50 -0700 | [diff] [blame] | 234 | chase_hint_ = nullptr; |
| 235 | // Retry chasing constants for wrap-around (merge sensitive). |
| 236 | if (!min_val->is_known && info->induction_class == HInductionVarAnalysis::kWrapAround) { |
| 237 | *min_val = SimplifyMin(GetVal(info, trip, in_body, /* is_min */ true)); |
| 238 | } |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 239 | return true; |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 240 | } |
| 241 | |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 242 | bool InductionVarRange::CanGenerateRange(HInstruction* context, |
| 243 | HInstruction* instruction, |
| 244 | /*out*/bool* needs_finite_test, |
| 245 | /*out*/bool* needs_taken_test) { |
| 246 | bool is_last_value = false; |
| 247 | int64_t stride_value = 0; |
Aart Bik | 9abf894 | 2016-10-14 09:49:42 -0700 | [diff] [blame] | 248 | return GenerateRangeOrLastValue(context, |
| 249 | instruction, |
| 250 | is_last_value, |
| 251 | nullptr, |
| 252 | nullptr, |
| 253 | nullptr, |
| 254 | nullptr, |
| 255 | nullptr, // nothing generated yet |
| 256 | &stride_value, |
| 257 | needs_finite_test, |
| 258 | needs_taken_test) |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 259 | && (stride_value == -1 || |
| 260 | stride_value == 0 || |
Aart Bik | 40fbf74 | 2016-10-31 11:02:50 -0700 | [diff] [blame] | 261 | stride_value == 1); // avoid arithmetic wrap-around anomalies. |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 264 | void InductionVarRange::GenerateRange(HInstruction* context, |
| 265 | HInstruction* instruction, |
| 266 | HGraph* graph, |
| 267 | HBasicBlock* block, |
| 268 | /*out*/HInstruction** lower, |
| 269 | /*out*/HInstruction** upper) { |
| 270 | bool is_last_value = false; |
Aart Bik | 009cace | 2016-09-16 10:15:19 -0700 | [diff] [blame] | 271 | int64_t stride_value = 0; |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 272 | bool b1, b2; // unused |
Aart Bik | 9abf894 | 2016-10-14 09:49:42 -0700 | [diff] [blame] | 273 | if (!GenerateRangeOrLastValue(context, |
| 274 | instruction, |
| 275 | is_last_value, |
| 276 | graph, |
| 277 | block, |
| 278 | lower, |
| 279 | upper, |
| 280 | nullptr, |
| 281 | &stride_value, |
| 282 | &b1, |
| 283 | &b2)) { |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 284 | LOG(FATAL) << "Failed precondition: CanGenerateRange()"; |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 285 | } |
| 286 | } |
| 287 | |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 288 | HInstruction* InductionVarRange::GenerateTakenTest(HInstruction* context, |
| 289 | HGraph* graph, |
| 290 | HBasicBlock* block) { |
| 291 | HInstruction* taken_test = nullptr; |
| 292 | bool is_last_value = false; |
| 293 | int64_t stride_value = 0; |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 294 | bool b1, b2; // unused |
Aart Bik | 9abf894 | 2016-10-14 09:49:42 -0700 | [diff] [blame] | 295 | if (!GenerateRangeOrLastValue(context, |
| 296 | context, |
| 297 | is_last_value, |
| 298 | graph, |
| 299 | block, |
| 300 | nullptr, |
| 301 | nullptr, |
| 302 | &taken_test, |
| 303 | &stride_value, |
| 304 | &b1, |
| 305 | &b2)) { |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 306 | LOG(FATAL) << "Failed precondition: CanGenerateRange()"; |
| 307 | } |
| 308 | return taken_test; |
| 309 | } |
| 310 | |
| 311 | bool InductionVarRange::CanGenerateLastValue(HInstruction* instruction) { |
| 312 | bool is_last_value = true; |
| 313 | int64_t stride_value = 0; |
| 314 | bool needs_finite_test = false; |
| 315 | bool needs_taken_test = false; |
Aart Bik | 9abf894 | 2016-10-14 09:49:42 -0700 | [diff] [blame] | 316 | return GenerateRangeOrLastValue(instruction, |
| 317 | instruction, |
| 318 | is_last_value, |
| 319 | nullptr, |
| 320 | nullptr, |
| 321 | nullptr, |
| 322 | nullptr, |
| 323 | nullptr, // nothing generated yet |
| 324 | &stride_value, |
| 325 | &needs_finite_test, |
| 326 | &needs_taken_test) |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 327 | && !needs_finite_test && !needs_taken_test; |
| 328 | } |
| 329 | |
| 330 | HInstruction* InductionVarRange::GenerateLastValue(HInstruction* instruction, |
| 331 | HGraph* graph, |
| 332 | HBasicBlock* block) { |
| 333 | HInstruction* last_value = nullptr; |
| 334 | bool is_last_value = true; |
| 335 | int64_t stride_value = 0; |
| 336 | bool b1, b2; // unused |
Aart Bik | 9abf894 | 2016-10-14 09:49:42 -0700 | [diff] [blame] | 337 | if (!GenerateRangeOrLastValue(instruction, |
| 338 | instruction, |
| 339 | is_last_value, |
| 340 | graph, |
| 341 | block, |
| 342 | &last_value, |
| 343 | &last_value, |
| 344 | nullptr, |
| 345 | &stride_value, |
| 346 | &b1, |
| 347 | &b2)) { |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 348 | LOG(FATAL) << "Failed precondition: CanGenerateLastValue()"; |
| 349 | } |
| 350 | return last_value; |
| 351 | } |
| 352 | |
| 353 | void InductionVarRange::Replace(HInstruction* instruction, |
| 354 | HInstruction* fetch, |
| 355 | HInstruction* replacement) { |
| 356 | for (HLoopInformation* lp = instruction->GetBlock()->GetLoopInformation(); // closest enveloping loop |
| 357 | lp != nullptr; |
| 358 | lp = lp->GetPreHeader()->GetLoopInformation()) { |
Aart Bik | 009cace | 2016-09-16 10:15:19 -0700 | [diff] [blame] | 359 | // Update instruction's information. |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 360 | ReplaceInduction(induction_analysis_->LookupInfo(lp, instruction), fetch, replacement); |
Aart Bik | 009cace | 2016-09-16 10:15:19 -0700 | [diff] [blame] | 361 | // Update loop's trip-count information. |
| 362 | ReplaceInduction(induction_analysis_->LookupInfo(lp, GetLoopControl(lp)), fetch, replacement); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 363 | } |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 364 | } |
| 365 | |
Aart Bik | 6b69e0a | 2017-01-11 10:20:43 -0800 | [diff] [blame] | 366 | bool InductionVarRange::IsFinite(HLoopInformation* loop, /*out*/ int64_t* tc) const { |
Aart Bik | 9abf894 | 2016-10-14 09:49:42 -0700 | [diff] [blame] | 367 | HInductionVarAnalysis::InductionInfo *trip = |
| 368 | induction_analysis_->LookupInfo(loop, GetLoopControl(loop)); |
Aart Bik | 6b69e0a | 2017-01-11 10:20:43 -0800 | [diff] [blame] | 369 | if (trip != nullptr && !IsUnsafeTripCount(trip)) { |
| 370 | IsConstant(trip->op_a, kExact, tc); |
| 371 | return true; |
| 372 | } |
| 373 | return false; |
Aart Bik | 9abf894 | 2016-10-14 09:49:42 -0700 | [diff] [blame] | 374 | } |
| 375 | |
Aart Bik | fa76296 | 2017-04-07 11:33:37 -0700 | [diff] [blame] | 376 | bool InductionVarRange::IsUnitStride(HInstruction* context, |
| 377 | HInstruction* instruction, |
Aart Bik | 37dc4df | 2017-06-28 14:08:00 -0700 | [diff] [blame] | 378 | HGraph* graph, |
Aart Bik | 8e02e3e | 2017-02-28 14:41:55 -0800 | [diff] [blame] | 379 | /*out*/ HInstruction** offset) const { |
| 380 | HLoopInformation* loop = nullptr; |
| 381 | HInductionVarAnalysis::InductionInfo* info = nullptr; |
| 382 | HInductionVarAnalysis::InductionInfo* trip = nullptr; |
Aart Bik | fa76296 | 2017-04-07 11:33:37 -0700 | [diff] [blame] | 383 | if (HasInductionInfo(context, instruction, &loop, &info, &trip)) { |
Aart Bik | 8e02e3e | 2017-02-28 14:41:55 -0800 | [diff] [blame] | 384 | if (info->induction_class == HInductionVarAnalysis::kLinear && |
Aart Bik | 7adb688 | 2017-03-07 13:28:51 -0800 | [diff] [blame] | 385 | !HInductionVarAnalysis::IsNarrowingLinear(info)) { |
Aart Bik | 8e02e3e | 2017-02-28 14:41:55 -0800 | [diff] [blame] | 386 | int64_t stride_value = 0; |
| 387 | if (IsConstant(info->op_a, kExact, &stride_value) && stride_value == 1) { |
| 388 | int64_t off_value = 0; |
Aart Bik | 37dc4df | 2017-06-28 14:08:00 -0700 | [diff] [blame] | 389 | if (IsConstant(info->op_b, kExact, &off_value)) { |
| 390 | *offset = graph->GetConstant(info->op_b->type, off_value); |
| 391 | } else if (info->op_b->operation == HInductionVarAnalysis::kFetch) { |
Aart Bik | 8e02e3e | 2017-02-28 14:41:55 -0800 | [diff] [blame] | 392 | *offset = info->op_b->fetch; |
Aart Bik | 37dc4df | 2017-06-28 14:08:00 -0700 | [diff] [blame] | 393 | } else { |
| 394 | return false; |
Aart Bik | 8e02e3e | 2017-02-28 14:41:55 -0800 | [diff] [blame] | 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 | 97412c92 | 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 | 97412c92 | 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) { |
Aart Bik | f3e61ee | 2017-04-12 17:09:20 -0700 | [diff] [blame] | 439 | if (IsInt64AndGet(info->fetch, value)) { |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 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 | 97412c92 | 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 | 97412c92 | 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 | 97412c92 | 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 | 97412c92 | 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 | f3e61ee | 2017-04-12 17:09:20 -0700 | [diff] [blame] | 630 | IsInt64AndGet(info->fetch, &f) && f >= 1) { |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 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; |
Aart Bik | f3e61ee | 2017-04-12 17:09:20 -0700 | [diff] [blame] | 658 | if (IsInt64AndGet(instruction, &value) && CanLongValueFitIntoInt(value)) { |
Aart Bik | 40fbf74 | 2016-10-31 11:02:50 -0700 | [diff] [blame] | 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 | f3e61ee | 2017-04-12 17:09:20 -0700 | [diff] [blame] | 666 | if (IsInt64AndGet(instruction->InputAt(0), &value) && CanLongValueFitIntoInt(value)) { |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 667 | return AddValue(Value(static_cast<int32_t>(value)), |
| 668 | GetFetch(instruction->InputAt(1), trip, in_body, is_min)); |
Aart Bik | f3e61ee | 2017-04-12 17:09:20 -0700 | [diff] [blame] | 669 | } else if (IsInt64AndGet(instruction->InputAt(1), &value) && CanLongValueFitIntoInt(value)) { |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 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 | 8e9090b | 2017-09-08 16:46:50 -0700 | [diff] [blame] | 673 | } else if (instruction->IsSub()) { |
| 674 | // Incorporate suitable constants in the chased value. |
| 675 | if (IsInt64AndGet(instruction->InputAt(0), &value) && CanLongValueFitIntoInt(value)) { |
| 676 | return SubValue(Value(static_cast<int32_t>(value)), |
| 677 | GetFetch(instruction->InputAt(1), trip, in_body, !is_min)); |
| 678 | } else if (IsInt64AndGet(instruction->InputAt(1), &value) && CanLongValueFitIntoInt(value)) { |
| 679 | return SubValue(GetFetch(instruction->InputAt(0), trip, in_body, is_min), |
| 680 | Value(static_cast<int32_t>(value))); |
| 681 | } |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 682 | } else if (instruction->IsArrayLength()) { |
Aart Bik | 40fbf74 | 2016-10-31 11:02:50 -0700 | [diff] [blame] | 683 | // 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] | 684 | if (chase_hint_ == nullptr) { |
| 685 | return is_min ? Value(0) : Value(std::numeric_limits<int32_t>::max()); |
| 686 | } else if (instruction->InputAt(0)->IsNewArray()) { |
Nicolas Geoffray | e761bcc | 2017-01-19 08:59:37 +0000 | [diff] [blame] | 687 | return GetFetch(instruction->InputAt(0)->AsNewArray()->GetLength(), trip, in_body, is_min); |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 688 | } |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 689 | } else if (instruction->IsTypeConversion()) { |
Aart Bik | 40fbf74 | 2016-10-31 11:02:50 -0700 | [diff] [blame] | 690 | // 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] | 691 | // 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] | 692 | if (instruction->AsTypeConversion()->GetInputType() == Primitive::kPrimInt && |
| 693 | instruction->AsTypeConversion()->GetResultType() == Primitive::kPrimLong) { |
| 694 | return GetFetch(instruction->InputAt(0), trip, in_body, is_min); |
| 695 | } |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 696 | } |
| 697 | // Chase an invariant fetch that is defined by an outer loop if the trip-count used |
| 698 | // so far is well-behaved in both bounds and the next trip-count is safe. |
| 699 | // Example: |
| 700 | // for (int i = 0; i <= 100; i++) // safe |
| 701 | // for (int j = 0; j <= i; j++) // well-behaved |
| 702 | // j is in range [0, i ] (if i is chase hint) |
| 703 | // or in range [0, 100] (otherwise) |
| 704 | HLoopInformation* next_loop = nullptr; |
| 705 | HInductionVarAnalysis::InductionInfo* next_info = nullptr; |
| 706 | HInductionVarAnalysis::InductionInfo* next_trip = nullptr; |
| 707 | bool next_in_body = true; // inner loop is always in body of outer loop |
| 708 | if (HasInductionInfo(instruction, instruction, &next_loop, &next_info, &next_trip) && |
| 709 | IsWellBehavedTripCount(trip) && |
| 710 | !IsUnsafeTripCount(next_trip)) { |
| 711 | return GetVal(next_info, next_trip, next_in_body, is_min); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 712 | } |
Aart Bik | 40fbf74 | 2016-10-31 11:02:50 -0700 | [diff] [blame] | 713 | // Fetch is represented by itself. |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 714 | return Value(instruction, 1, 0); |
| 715 | } |
| 716 | |
Aart Bik | cd26feb | 2015-09-23 17:50:50 -0700 | [diff] [blame] | 717 | InductionVarRange::Value InductionVarRange::GetVal(HInductionVarAnalysis::InductionInfo* info, |
| 718 | HInductionVarAnalysis::InductionInfo* trip, |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 719 | bool in_body, |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 720 | bool is_min) const { |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 721 | if (info != nullptr) { |
| 722 | switch (info->induction_class) { |
| 723 | case HInductionVarAnalysis::kInvariant: |
| 724 | // Invariants. |
| 725 | switch (info->operation) { |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 726 | case HInductionVarAnalysis::kAdd: |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 727 | return AddValue(GetVal(info->op_a, trip, in_body, is_min), |
| 728 | GetVal(info->op_b, trip, in_body, is_min)); |
Aart Bik | cd26feb | 2015-09-23 17:50:50 -0700 | [diff] [blame] | 729 | case HInductionVarAnalysis::kSub: // second reversed! |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 730 | return SubValue(GetVal(info->op_a, trip, in_body, is_min), |
| 731 | GetVal(info->op_b, trip, in_body, !is_min)); |
Aart Bik | cd26feb | 2015-09-23 17:50:50 -0700 | [diff] [blame] | 732 | case HInductionVarAnalysis::kNeg: // second reversed! |
| 733 | return SubValue(Value(0), |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 734 | GetVal(info->op_b, trip, in_body, !is_min)); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 735 | case HInductionVarAnalysis::kMul: |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 736 | 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] | 737 | case HInductionVarAnalysis::kDiv: |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 738 | 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] | 739 | case HInductionVarAnalysis::kRem: |
| 740 | return GetRem(info->op_a, info->op_b); |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 741 | case HInductionVarAnalysis::kXor: |
| 742 | return GetXor(info->op_a, info->op_b); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 743 | case HInductionVarAnalysis::kFetch: |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 744 | return GetFetch(info->fetch, trip, in_body, is_min); |
| 745 | case HInductionVarAnalysis::kTripCountInLoop: |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 746 | case HInductionVarAnalysis::kTripCountInLoopUnsafe: |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 747 | if (!in_body && !is_min) { // one extra! |
Aart Bik | 22f0587 | 2015-10-27 15:56:28 -0700 | [diff] [blame] | 748 | return GetVal(info->op_a, trip, in_body, is_min); |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 749 | } |
| 750 | FALLTHROUGH_INTENDED; |
| 751 | case HInductionVarAnalysis::kTripCountInBody: |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 752 | case HInductionVarAnalysis::kTripCountInBodyUnsafe: |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 753 | if (is_min) { |
| 754 | return Value(0); |
| 755 | } else if (in_body) { |
Aart Bik | 22f0587 | 2015-10-27 15:56:28 -0700 | [diff] [blame] | 756 | 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] | 757 | } |
| 758 | break; |
| 759 | default: |
| 760 | break; |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 761 | } |
| 762 | break; |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 763 | case HInductionVarAnalysis::kLinear: |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 764 | return CorrectForType(GetLinear(info, trip, in_body, is_min), info->type); |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 765 | case HInductionVarAnalysis::kPolynomial: |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 766 | return GetPolynomial(info, trip, in_body, is_min); |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 767 | case HInductionVarAnalysis::kGeometric: |
| 768 | return GetGeometric(info, trip, in_body, is_min); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 769 | case HInductionVarAnalysis::kWrapAround: |
| 770 | case HInductionVarAnalysis::kPeriodic: |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 771 | return MergeVal(GetVal(info->op_a, trip, in_body, is_min), |
| 772 | GetVal(info->op_b, trip, in_body, is_min), is_min); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 773 | } |
| 774 | } |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 775 | return Value(); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | InductionVarRange::Value InductionVarRange::GetMul(HInductionVarAnalysis::InductionInfo* info1, |
| 779 | HInductionVarAnalysis::InductionInfo* info2, |
| 780 | HInductionVarAnalysis::InductionInfo* trip, |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 781 | bool in_body, |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 782 | bool is_min) const { |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 783 | // Constant times range. |
| 784 | int64_t value = 0; |
| 785 | if (IsConstant(info1, kExact, &value)) { |
| 786 | return MulRangeAndConstant(value, info2, trip, in_body, is_min); |
| 787 | } else if (IsConstant(info2, kExact, &value)) { |
| 788 | return MulRangeAndConstant(value, info1, trip, in_body, is_min); |
| 789 | } |
| 790 | // Interval ranges. |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 791 | Value v1_min = GetVal(info1, trip, in_body, /* is_min */ true); |
| 792 | Value v1_max = GetVal(info1, trip, in_body, /* is_min */ false); |
| 793 | Value v2_min = GetVal(info2, trip, in_body, /* is_min */ true); |
| 794 | Value v2_max = GetVal(info2, trip, in_body, /* is_min */ false); |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 795 | // Positive range vs. positive or negative range. |
| 796 | if (IsConstantValue(v1_min) && v1_min.b_constant >= 0) { |
| 797 | if (IsConstantValue(v2_min) && v2_min.b_constant >= 0) { |
| 798 | return is_min ? MulValue(v1_min, v2_min) : MulValue(v1_max, v2_max); |
| 799 | } else if (IsConstantValue(v2_max) && v2_max.b_constant <= 0) { |
| 800 | 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] | 801 | } |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 802 | } |
| 803 | // Negative range vs. positive or negative range. |
| 804 | if (IsConstantValue(v1_max) && v1_max.b_constant <= 0) { |
| 805 | if (IsConstantValue(v2_min) && v2_min.b_constant >= 0) { |
| 806 | return is_min ? MulValue(v1_min, v2_max) : MulValue(v1_max, v2_min); |
| 807 | } else if (IsConstantValue(v2_max) && v2_max.b_constant <= 0) { |
| 808 | 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] | 809 | } |
| 810 | } |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 811 | return Value(); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | InductionVarRange::Value InductionVarRange::GetDiv(HInductionVarAnalysis::InductionInfo* info1, |
| 815 | HInductionVarAnalysis::InductionInfo* info2, |
| 816 | HInductionVarAnalysis::InductionInfo* trip, |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 817 | bool in_body, |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 818 | bool is_min) const { |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 819 | // Range divided by constant. |
| 820 | int64_t value = 0; |
| 821 | if (IsConstant(info2, kExact, &value)) { |
| 822 | return DivRangeAndConstant(value, info1, trip, in_body, is_min); |
| 823 | } |
| 824 | // Interval ranges. |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 825 | Value v1_min = GetVal(info1, trip, in_body, /* is_min */ true); |
| 826 | Value v1_max = GetVal(info1, trip, in_body, /* is_min */ false); |
| 827 | Value v2_min = GetVal(info2, trip, in_body, /* is_min */ true); |
| 828 | Value v2_max = GetVal(info2, trip, in_body, /* is_min */ false); |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 829 | // Positive range vs. positive or negative range. |
| 830 | if (IsConstantValue(v1_min) && v1_min.b_constant >= 0) { |
| 831 | if (IsConstantValue(v2_min) && v2_min.b_constant >= 0) { |
| 832 | return is_min ? DivValue(v1_min, v2_max) : DivValue(v1_max, v2_min); |
| 833 | } else if (IsConstantValue(v2_max) && v2_max.b_constant <= 0) { |
| 834 | 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] | 835 | } |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 836 | } |
| 837 | // Negative range vs. positive or negative range. |
| 838 | if (IsConstantValue(v1_max) && v1_max.b_constant <= 0) { |
| 839 | if (IsConstantValue(v2_min) && v2_min.b_constant >= 0) { |
| 840 | return is_min ? DivValue(v1_min, v2_min) : DivValue(v1_max, v2_max); |
| 841 | } else if (IsConstantValue(v2_max) && v2_max.b_constant <= 0) { |
| 842 | 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] | 843 | } |
| 844 | } |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 845 | return Value(); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 846 | } |
| 847 | |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 848 | InductionVarRange::Value InductionVarRange::GetRem( |
| 849 | HInductionVarAnalysis::InductionInfo* info1, |
| 850 | HInductionVarAnalysis::InductionInfo* info2) const { |
| 851 | int64_t v1 = 0; |
| 852 | int64_t v2 = 0; |
| 853 | // Only accept exact values. |
| 854 | if (IsConstant(info1, kExact, &v1) && IsConstant(info2, kExact, &v2) && v2 != 0) { |
| 855 | int64_t value = v1 % v2; |
| 856 | if (CanLongValueFitIntoInt(value)) { |
| 857 | return Value(static_cast<int32_t>(value)); |
| 858 | } |
| 859 | } |
| 860 | return Value(); |
| 861 | } |
| 862 | |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 863 | InductionVarRange::Value InductionVarRange::GetXor( |
| 864 | HInductionVarAnalysis::InductionInfo* info1, |
| 865 | HInductionVarAnalysis::InductionInfo* info2) const { |
| 866 | int64_t v1 = 0; |
| 867 | int64_t v2 = 0; |
| 868 | // Only accept exact values. |
| 869 | if (IsConstant(info1, kExact, &v1) && IsConstant(info2, kExact, &v2)) { |
| 870 | int64_t value = v1 ^ v2; |
| 871 | if (CanLongValueFitIntoInt(value)) { |
| 872 | return Value(static_cast<int32_t>(value)); |
| 873 | } |
| 874 | } |
| 875 | return Value(); |
| 876 | } |
| 877 | |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 878 | InductionVarRange::Value InductionVarRange::MulRangeAndConstant( |
| 879 | int64_t value, |
| 880 | HInductionVarAnalysis::InductionInfo* info, |
| 881 | HInductionVarAnalysis::InductionInfo* trip, |
| 882 | bool in_body, |
| 883 | bool is_min) const { |
| 884 | if (CanLongValueFitIntoInt(value)) { |
| 885 | Value c(static_cast<int32_t>(value)); |
| 886 | return MulValue(GetVal(info, trip, in_body, is_min == value >= 0), c); |
| 887 | } |
| 888 | return Value(); |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 889 | } |
| 890 | |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 891 | InductionVarRange::Value InductionVarRange::DivRangeAndConstant( |
| 892 | int64_t value, |
| 893 | HInductionVarAnalysis::InductionInfo* info, |
| 894 | HInductionVarAnalysis::InductionInfo* trip, |
| 895 | bool in_body, |
| 896 | bool is_min) const { |
| 897 | if (CanLongValueFitIntoInt(value)) { |
| 898 | Value c(static_cast<int32_t>(value)); |
| 899 | return DivValue(GetVal(info, trip, in_body, is_min == value >= 0), c); |
| 900 | } |
| 901 | return Value(); |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 902 | } |
| 903 | |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 904 | InductionVarRange::Value InductionVarRange::AddValue(Value v1, Value v2) const { |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 905 | 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] | 906 | int32_t b = v1.b_constant + v2.b_constant; |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 907 | if (v1.a_constant == 0) { |
| 908 | return Value(v2.instruction, v2.a_constant, b); |
| 909 | } else if (v2.a_constant == 0) { |
| 910 | return Value(v1.instruction, v1.a_constant, b); |
| 911 | } else if (v1.instruction == v2.instruction && IsSafeAdd(v1.a_constant, v2.a_constant)) { |
| 912 | return Value(v1.instruction, v1.a_constant + v2.a_constant, b); |
| 913 | } |
| 914 | } |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 915 | return Value(); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 916 | } |
| 917 | |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 918 | InductionVarRange::Value InductionVarRange::SubValue(Value v1, Value v2) const { |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 919 | 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] | 920 | int32_t b = v1.b_constant - v2.b_constant; |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 921 | if (v1.a_constant == 0 && IsSafeSub(0, v2.a_constant)) { |
| 922 | return Value(v2.instruction, -v2.a_constant, b); |
| 923 | } else if (v2.a_constant == 0) { |
| 924 | return Value(v1.instruction, v1.a_constant, b); |
| 925 | } else if (v1.instruction == v2.instruction && IsSafeSub(v1.a_constant, v2.a_constant)) { |
| 926 | return Value(v1.instruction, v1.a_constant - v2.a_constant, b); |
| 927 | } |
| 928 | } |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 929 | return Value(); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 930 | } |
| 931 | |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 932 | InductionVarRange::Value InductionVarRange::MulValue(Value v1, Value v2) const { |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 933 | if (v1.is_known && v2.is_known) { |
| 934 | if (v1.a_constant == 0) { |
| 935 | if (IsSafeMul(v1.b_constant, v2.a_constant) && IsSafeMul(v1.b_constant, v2.b_constant)) { |
| 936 | return Value(v2.instruction, v1.b_constant * v2.a_constant, v1.b_constant * v2.b_constant); |
| 937 | } |
| 938 | } else if (v2.a_constant == 0) { |
| 939 | if (IsSafeMul(v1.a_constant, v2.b_constant) && IsSafeMul(v1.b_constant, v2.b_constant)) { |
| 940 | return Value(v1.instruction, v1.a_constant * v2.b_constant, v1.b_constant * v2.b_constant); |
| 941 | } |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 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::DivValue(Value v1, Value v2) const { |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 948 | 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] | 949 | if (IsSafeDiv(v1.b_constant, v2.b_constant)) { |
| 950 | return Value(v1.b_constant / v2.b_constant); |
| 951 | } |
| 952 | } |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 953 | return Value(); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 954 | } |
| 955 | |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 956 | InductionVarRange::Value InductionVarRange::MergeVal(Value v1, Value v2, bool is_min) const { |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 957 | if (v1.is_known && v2.is_known) { |
| 958 | if (v1.instruction == v2.instruction && v1.a_constant == v2.a_constant) { |
Aart Bik | cd26feb | 2015-09-23 17:50:50 -0700 | [diff] [blame] | 959 | return Value(v1.instruction, v1.a_constant, |
| 960 | is_min ? std::min(v1.b_constant, v2.b_constant) |
| 961 | : std::max(v1.b_constant, v2.b_constant)); |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 962 | } |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 963 | } |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 964 | return Value(); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 965 | } |
| 966 | |
Aart Bik | 9abf894 | 2016-10-14 09:49:42 -0700 | [diff] [blame] | 967 | bool InductionVarRange::GenerateRangeOrLastValue(HInstruction* context, |
| 968 | HInstruction* instruction, |
| 969 | bool is_last_value, |
| 970 | HGraph* graph, |
| 971 | HBasicBlock* block, |
| 972 | /*out*/HInstruction** lower, |
| 973 | /*out*/HInstruction** upper, |
| 974 | /*out*/HInstruction** taken_test, |
| 975 | /*out*/int64_t* stride_value, |
| 976 | /*out*/bool* needs_finite_test, |
| 977 | /*out*/bool* needs_taken_test) const { |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 978 | HLoopInformation* loop = nullptr; |
| 979 | HInductionVarAnalysis::InductionInfo* info = nullptr; |
| 980 | HInductionVarAnalysis::InductionInfo* trip = nullptr; |
| 981 | if (!HasInductionInfo(context, instruction, &loop, &info, &trip) || trip == nullptr) { |
| 982 | return false; // codegen needs all information, including tripcount |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 983 | } |
| 984 | // Determine what tests are needed. A finite test is needed if the evaluation code uses the |
| 985 | // trip-count and the loop maybe unsafe (because in such cases, the index could "overshoot" |
| 986 | // the computed range). A taken test is needed for any unknown trip-count, even if evaluation |
| 987 | // code does not use the trip-count explicitly (since there could be an implicit relation |
| 988 | // between e.g. an invariant subscript and a not-taken condition). |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 989 | bool in_body = context->GetBlock() != loop->GetHeader(); |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 990 | *stride_value = 0; |
| 991 | *needs_finite_test = NeedsTripCount(info, stride_value) && IsUnsafeTripCount(trip); |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 992 | *needs_taken_test = IsBodyTripCount(trip); |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 993 | // Handle last value request. |
| 994 | if (is_last_value) { |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 995 | DCHECK(!in_body); |
| 996 | switch (info->induction_class) { |
| 997 | case HInductionVarAnalysis::kLinear: |
| 998 | if (*stride_value > 0) { |
| 999 | lower = nullptr; |
| 1000 | } else { |
| 1001 | upper = nullptr; |
| 1002 | } |
| 1003 | break; |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 1004 | case HInductionVarAnalysis::kPolynomial: |
| 1005 | return GenerateLastValuePolynomial(info, trip, graph, block, lower); |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 1006 | case HInductionVarAnalysis::kGeometric: |
| 1007 | return GenerateLastValueGeometric(info, trip, graph, block, lower); |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 1008 | case HInductionVarAnalysis::kWrapAround: |
| 1009 | return GenerateLastValueWrapAround(info, trip, graph, block, lower); |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 1010 | case HInductionVarAnalysis::kPeriodic: |
| 1011 | return GenerateLastValuePeriodic(info, trip, graph, block, lower, needs_taken_test); |
| 1012 | default: |
| 1013 | return false; |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 1014 | } |
| 1015 | } |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 1016 | // Code generation for taken test: generate the code when requested or otherwise analyze |
| 1017 | // if code generation is feasible when taken test is needed. |
| 1018 | if (taken_test != nullptr) { |
| 1019 | return GenerateCode(trip->op_b, nullptr, graph, block, taken_test, in_body, /* is_min */ false); |
| 1020 | } else if (*needs_taken_test) { |
| 1021 | if (!GenerateCode( |
| 1022 | trip->op_b, nullptr, nullptr, nullptr, nullptr, in_body, /* is_min */ false)) { |
| 1023 | return false; |
| 1024 | } |
| 1025 | } |
| 1026 | // Code generation for lower and upper. |
| 1027 | return |
| 1028 | // Success on lower if invariant (not set), or code can be generated. |
| 1029 | ((info->induction_class == HInductionVarAnalysis::kInvariant) || |
| 1030 | GenerateCode(info, trip, graph, block, lower, in_body, /* is_min */ true)) && |
| 1031 | // And success on upper. |
| 1032 | GenerateCode(info, trip, graph, block, upper, in_body, /* is_min */ false); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 1033 | } |
| 1034 | |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 1035 | bool InductionVarRange::GenerateLastValuePolynomial(HInductionVarAnalysis::InductionInfo* info, |
| 1036 | HInductionVarAnalysis::InductionInfo* trip, |
| 1037 | HGraph* graph, |
| 1038 | HBasicBlock* block, |
| 1039 | /*out*/HInstruction** result) const { |
| 1040 | DCHECK(info != nullptr); |
| 1041 | DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kPolynomial); |
| 1042 | // Detect known coefficients and trip count (always taken). |
| 1043 | int64_t a = 0; |
| 1044 | int64_t b = 0; |
| 1045 | int64_t m = 0; |
Aart Bik | d0a022d | 2016-12-13 11:22:31 -0800 | [diff] [blame] | 1046 | if (IsConstant(info->op_a->op_a, kExact, &a) && |
| 1047 | IsConstant(info->op_a->op_b, kExact, &b) && |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 1048 | IsConstant(trip->op_a, kExact, &m) && m >= 1) { |
Aart Bik | d0a022d | 2016-12-13 11:22:31 -0800 | [diff] [blame] | 1049 | // 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] | 1050 | // 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] | 1051 | HInstruction* c = nullptr; |
| 1052 | 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] | 1053 | if (graph != nullptr) { |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 1054 | Primitive::Type type = info->type; |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 1055 | int64_t sum = a * ((m * (m - 1)) / 2) + b * m; |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 1056 | if (type != Primitive::kPrimLong) { |
| 1057 | sum = static_cast<int32_t>(sum); // okay to truncate |
| 1058 | } |
| 1059 | *result = |
| 1060 | Insert(block, new (graph->GetArena()) HAdd(type, graph->GetConstant(type, sum), c)); |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 1061 | } |
| 1062 | return true; |
| 1063 | } |
| 1064 | } |
| 1065 | return false; |
| 1066 | } |
| 1067 | |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 1068 | bool InductionVarRange::GenerateLastValueGeometric(HInductionVarAnalysis::InductionInfo* info, |
| 1069 | HInductionVarAnalysis::InductionInfo* trip, |
| 1070 | HGraph* graph, |
| 1071 | HBasicBlock* block, |
| 1072 | /*out*/HInstruction** result) const { |
| 1073 | DCHECK(info != nullptr); |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 1074 | DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kGeometric); |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 1075 | // Detect known base and trip count (always taken). |
| 1076 | int64_t f = 0; |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 1077 | int64_t m = 0; |
Aart Bik | f3e61ee | 2017-04-12 17:09:20 -0700 | [diff] [blame] | 1078 | if (IsInt64AndGet(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] | 1079 | HInstruction* opa = nullptr; |
| 1080 | HInstruction* opb = nullptr; |
| 1081 | if (GenerateCode(info->op_a, nullptr, graph, block, &opa, false, false) && |
| 1082 | GenerateCode(info->op_b, nullptr, graph, block, &opb, false, false)) { |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 1083 | if (graph != nullptr) { |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 1084 | Primitive::Type type = info->type; |
Aart Bik | d3ba626 | 2017-01-30 14:37:12 -0800 | [diff] [blame] | 1085 | // Compute f ^ m for known maximum index value m. |
| 1086 | bool overflow = false; |
| 1087 | int64_t fpow = IntPow(f, m, &overflow); |
| 1088 | if (info->operation == HInductionVarAnalysis::kDiv) { |
| 1089 | // For division, any overflow truncates to zero. |
| 1090 | if (overflow || (type != Primitive::kPrimLong && !CanLongValueFitIntoInt(fpow))) { |
| 1091 | fpow = 0; |
| 1092 | } |
| 1093 | } else if (type != Primitive::kPrimLong) { |
| 1094 | // For multiplication, okay to truncate to required precision. |
| 1095 | DCHECK(info->operation == HInductionVarAnalysis::kMul); |
| 1096 | fpow = static_cast<int32_t>(fpow); |
| 1097 | } |
| 1098 | // Generate code. |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 1099 | if (fpow == 0) { |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 1100 | // Special case: repeated mul/div always yields zero. |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 1101 | *result = graph->GetConstant(type, 0); |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 1102 | } else { |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 1103 | // Last value: a * f ^ m + b or a * f ^ -m + b. |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 1104 | HInstruction* e = nullptr; |
| 1105 | if (info->operation == HInductionVarAnalysis::kMul) { |
| 1106 | e = new (graph->GetArena()) HMul(type, opa, graph->GetConstant(type, fpow)); |
| 1107 | } else { |
| 1108 | e = new (graph->GetArena()) HDiv(type, opa, graph->GetConstant(type, fpow), kNoDexPc); |
| 1109 | } |
| 1110 | *result = Insert(block, new (graph->GetArena()) HAdd(type, Insert(block, e), opb)); |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 1111 | } |
| 1112 | } |
| 1113 | return true; |
| 1114 | } |
| 1115 | } |
| 1116 | return false; |
| 1117 | } |
| 1118 | |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 1119 | bool InductionVarRange::GenerateLastValueWrapAround(HInductionVarAnalysis::InductionInfo* info, |
| 1120 | HInductionVarAnalysis::InductionInfo* trip, |
| 1121 | HGraph* graph, |
| 1122 | HBasicBlock* block, |
| 1123 | /*out*/HInstruction** result) const { |
| 1124 | DCHECK(info != nullptr); |
| 1125 | DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kWrapAround); |
| 1126 | // Count depth. |
| 1127 | int32_t depth = 0; |
| 1128 | for (; info->induction_class == HInductionVarAnalysis::kWrapAround; |
| 1129 | info = info->op_b, ++depth) {} |
| 1130 | // 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] | 1131 | // TODO: generalize, but be careful to adjust the terminal. |
| 1132 | int64_t m = 0; |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 1133 | if (info->induction_class == HInductionVarAnalysis::kInvariant && |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 1134 | IsConstant(trip->op_a, kExact, &m) && m >= depth) { |
| 1135 | return GenerateCode(info, nullptr, graph, block, result, false, false); |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 1136 | } |
| 1137 | return false; |
| 1138 | } |
| 1139 | |
Aart Bik | 9abf894 | 2016-10-14 09:49:42 -0700 | [diff] [blame] | 1140 | bool InductionVarRange::GenerateLastValuePeriodic(HInductionVarAnalysis::InductionInfo* info, |
| 1141 | HInductionVarAnalysis::InductionInfo* trip, |
| 1142 | HGraph* graph, |
| 1143 | HBasicBlock* block, |
| 1144 | /*out*/HInstruction** result, |
| 1145 | /*out*/bool* needs_taken_test) const { |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 1146 | DCHECK(info != nullptr); |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 1147 | DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kPeriodic); |
Aart Bik | 8523ea1 | 2017-06-01 15:45:09 -0700 | [diff] [blame] | 1148 | // Count period and detect all-invariants. |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 1149 | int64_t period = 1; |
Aart Bik | 8523ea1 | 2017-06-01 15:45:09 -0700 | [diff] [blame] | 1150 | bool all_invariants = true; |
| 1151 | HInductionVarAnalysis::InductionInfo* p = info; |
| 1152 | for (; p->induction_class == HInductionVarAnalysis::kPeriodic; p = p->op_b, ++period) { |
| 1153 | DCHECK_EQ(p->op_a->induction_class, HInductionVarAnalysis::kInvariant); |
| 1154 | if (p->op_a->operation != HInductionVarAnalysis::kFetch) { |
| 1155 | all_invariants = false; |
| 1156 | } |
| 1157 | } |
| 1158 | DCHECK_EQ(p->induction_class, HInductionVarAnalysis::kInvariant); |
| 1159 | if (p->operation != HInductionVarAnalysis::kFetch) { |
| 1160 | all_invariants = false; |
| 1161 | } |
| 1162 | // Don't rely on FP arithmetic to be precise, unless the full period |
| 1163 | // consist of pre-computed expressions only. |
| 1164 | if (info->type == Primitive::kPrimFloat || info->type == Primitive::kPrimDouble) { |
| 1165 | if (!all_invariants) { |
| 1166 | return false; |
| 1167 | } |
| 1168 | } |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 1169 | // Handle any periodic(x, periodic(.., y)) for known maximum index value m. |
| 1170 | int64_t m = 0; |
| 1171 | if (IsConstant(trip->op_a, kExact, &m) && m >= 1) { |
| 1172 | int64_t li = m % period; |
| 1173 | for (int64_t i = 0; i < li; info = info->op_b, i++) {} |
| 1174 | if (info->induction_class == HInductionVarAnalysis::kPeriodic) { |
| 1175 | info = info->op_a; |
| 1176 | } |
| 1177 | return GenerateCode(info, nullptr, graph, block, result, false, false); |
Aart Bik | 9abf894 | 2016-10-14 09:49:42 -0700 | [diff] [blame] | 1178 | } |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 1179 | // Handle periodic(x, y) using even/odd-select on trip count. Enter trip count expression |
| 1180 | // directly to obtain the maximum index value t even if taken test is needed. |
| 1181 | HInstruction* x = nullptr; |
| 1182 | HInstruction* y = nullptr; |
| 1183 | HInstruction* t = nullptr; |
| 1184 | if (period == 2 && |
| 1185 | GenerateCode(info->op_a, nullptr, graph, block, graph ? &x : nullptr, false, false) && |
| 1186 | GenerateCode(info->op_b, nullptr, graph, block, graph ? &y : nullptr, false, false) && |
| 1187 | GenerateCode(trip->op_a, nullptr, graph, block, graph ? &t : nullptr, false, false)) { |
| 1188 | // During actual code generation (graph != nullptr), generate is_even ? x : y. |
Aart Bik | 9abf894 | 2016-10-14 09:49:42 -0700 | [diff] [blame] | 1189 | if (graph != nullptr) { |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 1190 | Primitive::Type type = trip->type; |
| 1191 | HInstruction* msk = |
| 1192 | Insert(block, new (graph->GetArena()) HAnd(type, t, graph->GetConstant(type, 1))); |
| 1193 | HInstruction* is_even = |
| 1194 | Insert(block, new (graph->GetArena()) HEqual(msk, graph->GetConstant(type, 0), kNoDexPc)); |
| 1195 | *result = Insert(block, new (graph->GetArena()) HSelect(is_even, x, y, kNoDexPc)); |
Aart Bik | 9abf894 | 2016-10-14 09:49:42 -0700 | [diff] [blame] | 1196 | } |
| 1197 | // Guard select with taken test if needed. |
| 1198 | if (*needs_taken_test) { |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 1199 | HInstruction* is_taken = nullptr; |
| 1200 | if (GenerateCode(trip->op_b, nullptr, graph, block, graph ? &is_taken : nullptr, false, false)) { |
| 1201 | if (graph != nullptr) { |
| 1202 | *result = Insert(block, new (graph->GetArena()) HSelect(is_taken, *result, x, kNoDexPc)); |
| 1203 | } |
| 1204 | *needs_taken_test = false; // taken care of |
| 1205 | } else { |
Aart Bik | 9abf894 | 2016-10-14 09:49:42 -0700 | [diff] [blame] | 1206 | return false; |
Aart Bik | 9abf894 | 2016-10-14 09:49:42 -0700 | [diff] [blame] | 1207 | } |
Aart Bik | 9abf894 | 2016-10-14 09:49:42 -0700 | [diff] [blame] | 1208 | } |
| 1209 | return true; |
| 1210 | } |
| 1211 | return false; |
| 1212 | } |
| 1213 | |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 1214 | bool InductionVarRange::GenerateCode(HInductionVarAnalysis::InductionInfo* info, |
| 1215 | HInductionVarAnalysis::InductionInfo* trip, |
| 1216 | HGraph* graph, // when set, code is generated |
| 1217 | HBasicBlock* block, |
| 1218 | /*out*/HInstruction** result, |
| 1219 | bool in_body, |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 1220 | bool is_min) const { |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 1221 | if (info != nullptr) { |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 1222 | // If during codegen, the result is not needed (nullptr), simply return success. |
| 1223 | if (graph != nullptr && result == nullptr) { |
| 1224 | return true; |
| 1225 | } |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 1226 | // Handle current operation. |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 1227 | Primitive::Type type = info->type; |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 1228 | HInstruction* opa = nullptr; |
| 1229 | HInstruction* opb = nullptr; |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 1230 | switch (info->induction_class) { |
| 1231 | case HInductionVarAnalysis::kInvariant: |
Aart Bik | 8e02e3e | 2017-02-28 14:41:55 -0800 | [diff] [blame] | 1232 | // Invariants (note that since invariants only have other invariants as |
| 1233 | // 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] | 1234 | switch (info->operation) { |
| 1235 | case HInductionVarAnalysis::kAdd: |
Aart Bik | 8e02e3e | 2017-02-28 14:41:55 -0800 | [diff] [blame] | 1236 | case HInductionVarAnalysis::kSub: |
| 1237 | case HInductionVarAnalysis::kMul: |
| 1238 | case HInductionVarAnalysis::kDiv: |
| 1239 | case HInductionVarAnalysis::kRem: |
| 1240 | case HInductionVarAnalysis::kXor: |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 1241 | case HInductionVarAnalysis::kLT: |
| 1242 | case HInductionVarAnalysis::kLE: |
| 1243 | case HInductionVarAnalysis::kGT: |
| 1244 | case HInductionVarAnalysis::kGE: |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 1245 | if (GenerateCode(info->op_a, trip, graph, block, &opa, in_body, is_min) && |
| 1246 | GenerateCode(info->op_b, trip, graph, block, &opb, in_body, is_min)) { |
| 1247 | if (graph != nullptr) { |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 1248 | HInstruction* operation = nullptr; |
| 1249 | switch (info->operation) { |
| 1250 | case HInductionVarAnalysis::kAdd: |
| 1251 | operation = new (graph->GetArena()) HAdd(type, opa, opb); break; |
Aart Bik | 8e02e3e | 2017-02-28 14:41:55 -0800 | [diff] [blame] | 1252 | case HInductionVarAnalysis::kSub: |
| 1253 | operation = new (graph->GetArena()) HSub(type, opa, opb); break; |
| 1254 | case HInductionVarAnalysis::kMul: |
| 1255 | operation = new (graph->GetArena()) HMul(type, opa, opb, kNoDexPc); break; |
| 1256 | case HInductionVarAnalysis::kDiv: |
| 1257 | operation = new (graph->GetArena()) HDiv(type, opa, opb, kNoDexPc); break; |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 1258 | case HInductionVarAnalysis::kRem: |
| 1259 | operation = new (graph->GetArena()) HRem(type, opa, opb, kNoDexPc); break; |
Aart Bik | 9abf894 | 2016-10-14 09:49:42 -0700 | [diff] [blame] | 1260 | case HInductionVarAnalysis::kXor: |
| 1261 | operation = new (graph->GetArena()) HXor(type, opa, opb); break; |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 1262 | case HInductionVarAnalysis::kLT: |
| 1263 | operation = new (graph->GetArena()) HLessThan(opa, opb); break; |
| 1264 | case HInductionVarAnalysis::kLE: |
| 1265 | operation = new (graph->GetArena()) HLessThanOrEqual(opa, opb); break; |
| 1266 | case HInductionVarAnalysis::kGT: |
| 1267 | operation = new (graph->GetArena()) HGreaterThan(opa, opb); break; |
| 1268 | case HInductionVarAnalysis::kGE: |
| 1269 | operation = new (graph->GetArena()) HGreaterThanOrEqual(opa, opb); break; |
| 1270 | default: |
| 1271 | LOG(FATAL) << "unknown operation"; |
| 1272 | } |
| 1273 | *result = Insert(block, operation); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 1274 | } |
| 1275 | return true; |
| 1276 | } |
| 1277 | break; |
Aart Bik | 8e02e3e | 2017-02-28 14:41:55 -0800 | [diff] [blame] | 1278 | case HInductionVarAnalysis::kNeg: |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 1279 | if (GenerateCode(info->op_b, trip, graph, block, &opb, in_body, !is_min)) { |
| 1280 | if (graph != nullptr) { |
| 1281 | *result = Insert(block, new (graph->GetArena()) HNeg(type, opb)); |
| 1282 | } |
| 1283 | return true; |
| 1284 | } |
| 1285 | break; |
| 1286 | case HInductionVarAnalysis::kFetch: |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 1287 | if (graph != nullptr) { |
| 1288 | *result = info->fetch; // already in HIR |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 1289 | } |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 1290 | return true; |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 1291 | case HInductionVarAnalysis::kTripCountInLoop: |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 1292 | case HInductionVarAnalysis::kTripCountInLoopUnsafe: |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 1293 | if (!in_body && !is_min) { // one extra! |
Aart Bik | 22f0587 | 2015-10-27 15:56:28 -0700 | [diff] [blame] | 1294 | 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] | 1295 | } |
| 1296 | FALLTHROUGH_INTENDED; |
| 1297 | case HInductionVarAnalysis::kTripCountInBody: |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 1298 | case HInductionVarAnalysis::kTripCountInBodyUnsafe: |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 1299 | if (is_min) { |
| 1300 | if (graph != nullptr) { |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 1301 | *result = graph->GetConstant(type, 0); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 1302 | } |
| 1303 | return true; |
| 1304 | } else if (in_body) { |
Aart Bik | 22f0587 | 2015-10-27 15:56:28 -0700 | [diff] [blame] | 1305 | 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] | 1306 | if (graph != nullptr) { |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 1307 | *result = |
| 1308 | Insert(block, |
| 1309 | new (graph->GetArena()) HSub(type, opb, graph->GetConstant(type, 1))); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 1310 | } |
| 1311 | return true; |
| 1312 | } |
| 1313 | } |
| 1314 | break; |
Aart Bik | 8e02e3e | 2017-02-28 14:41:55 -0800 | [diff] [blame] | 1315 | case HInductionVarAnalysis::kNop: |
| 1316 | LOG(FATAL) << "unexpected invariant nop"; |
| 1317 | } // switch invariant operation |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 1318 | break; |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 1319 | case HInductionVarAnalysis::kLinear: { |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 1320 | // Linear induction a * i + b, for normalized 0 <= i < TC. For ranges, this should |
| 1321 | // be restricted to a unit stride to avoid arithmetic wrap-around situations that |
| 1322 | // 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] | 1323 | // known stride yields right value. Always avoid any narrowing linear induction or |
| 1324 | // any type mismatch between the linear induction and the trip count expression. |
| 1325 | // TODO: careful runtime type conversions could generalize this latter restriction. |
| 1326 | if (!HInductionVarAnalysis::IsNarrowingLinear(info) && trip->type == type) { |
| 1327 | int64_t stride_value = 0; |
| 1328 | if (IsConstant(info->op_a, kExact, &stride_value) && |
| 1329 | CanLongValueFitIntoInt(stride_value)) { |
| 1330 | const bool is_min_a = stride_value >= 0 ? is_min : !is_min; |
| 1331 | if (GenerateCode(trip, trip, graph, block, &opa, in_body, is_min_a) && |
| 1332 | GenerateCode(info->op_b, trip, graph, block, &opb, in_body, is_min)) { |
| 1333 | if (graph != nullptr) { |
| 1334 | HInstruction* oper; |
| 1335 | if (stride_value == 1) { |
| 1336 | oper = new (graph->GetArena()) HAdd(type, opa, opb); |
| 1337 | } else if (stride_value == -1) { |
| 1338 | oper = new (graph->GetArena()) HSub(type, opb, opa); |
| 1339 | } else { |
| 1340 | HInstruction* mul = |
| 1341 | new (graph->GetArena()) HMul(type, graph->GetConstant(type, stride_value), opa); |
| 1342 | oper = new (graph->GetArena()) HAdd(type, Insert(block, mul), opb); |
| 1343 | } |
| 1344 | *result = Insert(block, oper); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 1345 | } |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 1346 | return true; |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 1347 | } |
| 1348 | } |
| 1349 | } |
| 1350 | break; |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 1351 | } |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 1352 | case HInductionVarAnalysis::kPolynomial: |
| 1353 | case HInductionVarAnalysis::kGeometric: |
| 1354 | break; |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 1355 | case HInductionVarAnalysis::kWrapAround: |
| 1356 | case HInductionVarAnalysis::kPeriodic: { |
| 1357 | // Wrap-around and periodic inductions are restricted to constants only, so that extreme |
| 1358 | // values are easy to test at runtime without complications of arithmetic wrap-around. |
| 1359 | Value extreme = GetVal(info, trip, in_body, is_min); |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 1360 | if (IsConstantValue(extreme)) { |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 1361 | if (graph != nullptr) { |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 1362 | *result = graph->GetConstant(type, extreme.b_constant); |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 1363 | } |
| 1364 | return true; |
| 1365 | } |
| 1366 | break; |
| 1367 | } |
Aart Bik | 8e02e3e | 2017-02-28 14:41:55 -0800 | [diff] [blame] | 1368 | } // switch induction class |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 1369 | } |
| 1370 | return false; |
| 1371 | } |
| 1372 | |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 1373 | void InductionVarRange::ReplaceInduction(HInductionVarAnalysis::InductionInfo* info, |
| 1374 | HInstruction* fetch, |
| 1375 | HInstruction* replacement) { |
| 1376 | if (info != nullptr) { |
| 1377 | if (info->induction_class == HInductionVarAnalysis::kInvariant && |
| 1378 | info->operation == HInductionVarAnalysis::kFetch && |
| 1379 | info->fetch == fetch) { |
| 1380 | info->fetch = replacement; |
| 1381 | } |
| 1382 | ReplaceInduction(info->op_a, fetch, replacement); |
| 1383 | ReplaceInduction(info->op_b, fetch, replacement); |
| 1384 | } |
| 1385 | } |
| 1386 | |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 1387 | } // namespace art |