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 | |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 17 | #include "induction_var_range.h" |
| 18 | |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 19 | #include "base/arena_allocator.h" |
| 20 | #include "builder.h" |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 21 | #include "induction_var_analysis.h" |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 22 | #include "nodes.h" |
| 23 | #include "optimizing_unit_test.h" |
| 24 | |
| 25 | namespace art { |
| 26 | |
| 27 | using Value = InductionVarRange::Value; |
| 28 | |
| 29 | /** |
| 30 | * Fixture class for the InductionVarRange tests. |
| 31 | */ |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 32 | class InductionVarRangeTest : public CommonCompilerTest { |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 33 | public: |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 34 | InductionVarRangeTest() |
| 35 | : pool_(), |
| 36 | allocator_(&pool_), |
| 37 | graph_(CreateGraph(&allocator_)), |
| 38 | iva_(new (&allocator_) HInductionVarAnalysis(graph_)), |
| 39 | range_(iva_) { |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 40 | BuildGraph(); |
| 41 | } |
| 42 | |
| 43 | ~InductionVarRangeTest() { } |
| 44 | |
| 45 | void ExpectEqual(Value v1, Value v2) { |
| 46 | EXPECT_EQ(v1.instruction, v2.instruction); |
| 47 | EXPECT_EQ(v1.a_constant, v2.a_constant); |
| 48 | EXPECT_EQ(v1.b_constant, v2.b_constant); |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 49 | EXPECT_EQ(v1.is_known, v2.is_known); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Aart Bik | 8e02e3e | 2017-02-28 14:41:55 -0800 | [diff] [blame] | 52 | void ExpectInt(int32_t value, HInstruction* i) { |
| 53 | ASSERT_TRUE(i->IsIntConstant()); |
| 54 | EXPECT_EQ(value, i->AsIntConstant()->GetValue()); |
| 55 | } |
| 56 | |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 57 | // |
| 58 | // Construction methods. |
| 59 | // |
| 60 | |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 61 | /** Constructs bare minimum graph. */ |
| 62 | void BuildGraph() { |
| 63 | graph_->SetNumberOfVRegs(1); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 64 | entry_block_ = new (&allocator_) HBasicBlock(graph_); |
| 65 | exit_block_ = new (&allocator_) HBasicBlock(graph_); |
| 66 | graph_->AddBlock(entry_block_); |
| 67 | graph_->AddBlock(exit_block_); |
| 68 | graph_->SetEntryBlock(entry_block_); |
| 69 | graph_->SetExitBlock(exit_block_); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 70 | // Two parameters. |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 71 | x_ = new (&allocator_) HParameterValue(graph_->GetDexFile(), |
| 72 | dex::TypeIndex(0), |
| 73 | 0, |
| 74 | Primitive::kPrimInt); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 75 | entry_block_->AddInstruction(x_); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 76 | y_ = new (&allocator_) HParameterValue(graph_->GetDexFile(), |
| 77 | dex::TypeIndex(0), |
| 78 | 0, |
| 79 | Primitive::kPrimInt); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 80 | entry_block_->AddInstruction(y_); |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 81 | // Set arbitrary range analysis hint while testing private methods. |
| 82 | SetHint(x_); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | /** Constructs loop with given upper bound. */ |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 86 | void BuildLoop(int32_t lower, HInstruction* upper, int32_t stride) { |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 87 | // Control flow. |
| 88 | loop_preheader_ = new (&allocator_) HBasicBlock(graph_); |
| 89 | graph_->AddBlock(loop_preheader_); |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 90 | loop_header_ = new (&allocator_) HBasicBlock(graph_); |
| 91 | graph_->AddBlock(loop_header_); |
| 92 | loop_body_ = new (&allocator_) HBasicBlock(graph_); |
| 93 | graph_->AddBlock(loop_body_); |
David Brazdil | db51efb | 2015-11-06 01:36:20 +0000 | [diff] [blame] | 94 | HBasicBlock* return_block = new (&allocator_) HBasicBlock(graph_); |
| 95 | graph_->AddBlock(return_block); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 96 | entry_block_->AddSuccessor(loop_preheader_); |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 97 | loop_preheader_->AddSuccessor(loop_header_); |
| 98 | loop_header_->AddSuccessor(loop_body_); |
| 99 | loop_header_->AddSuccessor(return_block); |
| 100 | loop_body_->AddSuccessor(loop_header_); |
David Brazdil | db51efb | 2015-11-06 01:36:20 +0000 | [diff] [blame] | 101 | return_block->AddSuccessor(exit_block_); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 102 | // Instructions. |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 103 | loop_preheader_->AddInstruction(new (&allocator_) HGoto()); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 104 | HPhi* phi = new (&allocator_) HPhi(&allocator_, 0, 0, Primitive::kPrimInt); |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 105 | loop_header_->AddPhi(phi); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 106 | phi->AddInput(graph_->GetIntConstant(lower)); // i = l |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 107 | if (stride > 0) { |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 108 | condition_ = new (&allocator_) HLessThan(phi, upper); // i < u |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 109 | } else { |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 110 | condition_ = new (&allocator_) HGreaterThan(phi, upper); // i > u |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 111 | } |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 112 | loop_header_->AddInstruction(condition_); |
| 113 | loop_header_->AddInstruction(new (&allocator_) HIf(condition_)); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 114 | increment_ = new (&allocator_) HAdd(Primitive::kPrimInt, phi, graph_->GetIntConstant(stride)); |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 115 | loop_body_->AddInstruction(increment_); // i += s |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 116 | phi->AddInput(increment_); |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 117 | loop_body_->AddInstruction(new (&allocator_) HGoto()); |
David Brazdil | db51efb | 2015-11-06 01:36:20 +0000 | [diff] [blame] | 118 | return_block->AddInstruction(new (&allocator_) HReturnVoid()); |
| 119 | exit_block_->AddInstruction(new (&allocator_) HExit()); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 122 | /** Constructs SSA and performs induction variable analysis. */ |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 123 | void PerformInductionVarAnalysis() { |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 124 | graph_->BuildDominatorTree(); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 125 | iva_->Run(); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 128 | /** Sets hint. */ |
| 129 | void SetHint(HInstruction* hint) { |
| 130 | range_.chase_hint_ = hint; |
| 131 | } |
| 132 | |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 133 | /** Constructs an invariant. */ |
| 134 | HInductionVarAnalysis::InductionInfo* CreateInvariant(char opc, |
| 135 | HInductionVarAnalysis::InductionInfo* a, |
| 136 | HInductionVarAnalysis::InductionInfo* b) { |
| 137 | HInductionVarAnalysis::InductionOp op; |
| 138 | switch (opc) { |
| 139 | case '+': op = HInductionVarAnalysis::kAdd; break; |
| 140 | case '-': op = HInductionVarAnalysis::kSub; break; |
| 141 | case 'n': op = HInductionVarAnalysis::kNeg; break; |
| 142 | case '*': op = HInductionVarAnalysis::kMul; break; |
| 143 | case '/': op = HInductionVarAnalysis::kDiv; break; |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 144 | case '%': op = HInductionVarAnalysis::kRem; break; |
| 145 | case '^': op = HInductionVarAnalysis::kXor; break; |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 146 | case '<': op = HInductionVarAnalysis::kLT; break; |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 147 | default: op = HInductionVarAnalysis::kNop; break; |
| 148 | } |
| 149 | return iva_->CreateInvariantOp(op, a, b); |
| 150 | } |
| 151 | |
| 152 | /** Constructs a fetch. */ |
| 153 | HInductionVarAnalysis::InductionInfo* CreateFetch(HInstruction* fetch) { |
| 154 | return iva_->CreateInvariantFetch(fetch); |
| 155 | } |
| 156 | |
| 157 | /** Constructs a constant. */ |
| 158 | HInductionVarAnalysis::InductionInfo* CreateConst(int32_t c) { |
| 159 | return CreateFetch(graph_->GetIntConstant(c)); |
| 160 | } |
| 161 | |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 162 | /** Constructs a constant trip-count. */ |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 163 | HInductionVarAnalysis::InductionInfo* CreateTripCount(int32_t tc, bool in_loop, bool safe) { |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 164 | HInductionVarAnalysis::InductionOp op = HInductionVarAnalysis::kTripCountInBodyUnsafe; |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 165 | if (in_loop && safe) { |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 166 | op = HInductionVarAnalysis::kTripCountInLoop; |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 167 | } else if (in_loop) { |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 168 | op = HInductionVarAnalysis::kTripCountInLoopUnsafe; |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 169 | } else if (safe) { |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 170 | op = HInductionVarAnalysis::kTripCountInBody; |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 171 | } |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 172 | // Return TC with taken-test 0 < TC. |
| 173 | return iva_->CreateTripCount(op, |
| 174 | CreateConst(tc), |
| 175 | CreateInvariant('<', CreateConst(0), CreateConst(tc)), |
| 176 | Primitive::kPrimInt); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | /** Constructs a linear a * i + b induction. */ |
| 180 | HInductionVarAnalysis::InductionInfo* CreateLinear(int32_t a, int32_t b) { |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 181 | return iva_->CreateInduction(HInductionVarAnalysis::kLinear, |
| 182 | HInductionVarAnalysis::kNop, |
| 183 | CreateConst(a), |
| 184 | CreateConst(b), |
| 185 | nullptr, |
| 186 | Primitive::kPrimInt); |
| 187 | } |
| 188 | |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 189 | /** Constructs a polynomial sum(a * i + b) + c induction. */ |
| 190 | HInductionVarAnalysis::InductionInfo* CreatePolynomial(int32_t a, int32_t b, int32_t c) { |
| 191 | return iva_->CreateInduction(HInductionVarAnalysis::kPolynomial, |
| 192 | HInductionVarAnalysis::kNop, |
| 193 | CreateLinear(a, b), |
| 194 | CreateConst(c), |
| 195 | nullptr, |
| 196 | Primitive::kPrimInt); |
| 197 | } |
| 198 | |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 199 | /** Constructs a geometric a * f^i + b induction. */ |
| 200 | HInductionVarAnalysis::InductionInfo* CreateGeometric(int32_t a, int32_t b, int32_t f, char op) { |
| 201 | return iva_->CreateInduction(HInductionVarAnalysis::kGeometric, |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 202 | op == '*' ? HInductionVarAnalysis::kMul |
| 203 | : HInductionVarAnalysis::kDiv, |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 204 | CreateConst(a), |
| 205 | CreateConst(b), |
| 206 | graph_->GetIntConstant(f), |
| 207 | Primitive::kPrimInt); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | /** Constructs a range [lo, hi] using a periodic induction. */ |
| 211 | HInductionVarAnalysis::InductionInfo* CreateRange(int32_t lo, int32_t hi) { |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 212 | return iva_->CreateInduction(HInductionVarAnalysis::kPeriodic, |
| 213 | HInductionVarAnalysis::kNop, |
| 214 | CreateConst(lo), |
| 215 | CreateConst(hi), |
| 216 | nullptr, |
| 217 | Primitive::kPrimInt); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 220 | /** Constructs a wrap-around induction consisting of a constant, followed by info. */ |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 221 | HInductionVarAnalysis::InductionInfo* CreateWrapAround( |
| 222 | int32_t initial, |
| 223 | HInductionVarAnalysis::InductionInfo* info) { |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 224 | return iva_->CreateInduction(HInductionVarAnalysis::kWrapAround, |
| 225 | HInductionVarAnalysis::kNop, |
| 226 | CreateConst(initial), |
| 227 | info, |
| 228 | nullptr, |
| 229 | Primitive::kPrimInt); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 230 | } |
| 231 | |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 232 | /** Constructs a wrap-around induction consisting of a constant, followed by a range. */ |
| 233 | HInductionVarAnalysis::InductionInfo* CreateWrapAround(int32_t initial, int32_t lo, int32_t hi) { |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 234 | return CreateWrapAround(initial, CreateRange(lo, hi)); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | // |
| 238 | // Relay methods. |
| 239 | // |
| 240 | |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 241 | bool NeedsTripCount(HInductionVarAnalysis::InductionInfo* info) { |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 242 | int64_t s = 0; |
| 243 | return range_.NeedsTripCount(info, &s); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | bool IsBodyTripCount(HInductionVarAnalysis::InductionInfo* trip) { |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 247 | return range_.IsBodyTripCount(trip); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | bool IsUnsafeTripCount(HInductionVarAnalysis::InductionInfo* trip) { |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 251 | return range_.IsUnsafeTripCount(trip); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 254 | Value GetMin(HInductionVarAnalysis::InductionInfo* info, |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 255 | HInductionVarAnalysis::InductionInfo* trip) { |
| 256 | return range_.GetVal(info, trip, /* in_body */ true, /* is_min */ true); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | Value GetMax(HInductionVarAnalysis::InductionInfo* info, |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 260 | HInductionVarAnalysis::InductionInfo* trip) { |
| 261 | return range_.GetVal(info, trip, /* in_body */ true, /* is_min */ false); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | Value GetMul(HInductionVarAnalysis::InductionInfo* info1, |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 265 | HInductionVarAnalysis::InductionInfo* info2, |
| 266 | bool is_min) { |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 267 | return range_.GetMul(info1, info2, nullptr, /* in_body */ true, is_min); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | Value GetDiv(HInductionVarAnalysis::InductionInfo* info1, |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 271 | HInductionVarAnalysis::InductionInfo* info2, |
| 272 | bool is_min) { |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 273 | return range_.GetDiv(info1, info2, nullptr, /* in_body */ true, is_min); |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 274 | } |
| 275 | |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 276 | Value GetRem(HInductionVarAnalysis::InductionInfo* info1, |
| 277 | HInductionVarAnalysis::InductionInfo* info2) { |
| 278 | return range_.GetRem(info1, info2); |
| 279 | } |
| 280 | |
| 281 | Value GetXor(HInductionVarAnalysis::InductionInfo* info1, |
| 282 | HInductionVarAnalysis::InductionInfo* info2) { |
| 283 | return range_.GetXor(info1, info2); |
| 284 | } |
| 285 | |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 286 | bool IsExact(HInductionVarAnalysis::InductionInfo* info, int64_t* value) { |
| 287 | return range_.IsConstant(info, InductionVarRange::kExact, value); |
| 288 | } |
| 289 | |
| 290 | bool IsAtMost(HInductionVarAnalysis::InductionInfo* info, int64_t* value) { |
| 291 | return range_.IsConstant(info, InductionVarRange::kAtMost, value); |
| 292 | } |
| 293 | |
| 294 | bool IsAtLeast(HInductionVarAnalysis::InductionInfo* info, int64_t* value) { |
| 295 | return range_.IsConstant(info, InductionVarRange::kAtLeast, value); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 296 | } |
| 297 | |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 298 | Value AddValue(Value v1, Value v2) { return range_.AddValue(v1, v2); } |
| 299 | Value SubValue(Value v1, Value v2) { return range_.SubValue(v1, v2); } |
| 300 | Value MulValue(Value v1, Value v2) { return range_.MulValue(v1, v2); } |
| 301 | Value DivValue(Value v1, Value v2) { return range_.DivValue(v1, v2); } |
| 302 | Value MinValue(Value v1, Value v2) { return range_.MergeVal(v1, v2, true); } |
| 303 | Value MaxValue(Value v1, Value v2) { return range_.MergeVal(v1, v2, false); } |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 304 | |
| 305 | // General building fields. |
| 306 | ArenaPool pool_; |
| 307 | ArenaAllocator allocator_; |
| 308 | HGraph* graph_; |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 309 | HBasicBlock* entry_block_; |
| 310 | HBasicBlock* exit_block_; |
| 311 | HBasicBlock* loop_preheader_; |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 312 | HBasicBlock* loop_header_; |
| 313 | HBasicBlock* loop_body_; |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 314 | HInductionVarAnalysis* iva_; |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 315 | InductionVarRange range_; |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 316 | |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 317 | // Instructions. |
| 318 | HInstruction* condition_; |
| 319 | HInstruction* increment_; |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 320 | HInstruction* x_; |
| 321 | HInstruction* y_; |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 322 | }; |
| 323 | |
| 324 | // |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 325 | // Tests on private methods. |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 326 | // |
| 327 | |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 328 | TEST_F(InductionVarRangeTest, IsConstant) { |
| 329 | int64_t value; |
| 330 | // Constant. |
| 331 | EXPECT_TRUE(IsExact(CreateConst(12345), &value)); |
| 332 | EXPECT_EQ(12345, value); |
| 333 | EXPECT_TRUE(IsAtMost(CreateConst(12345), &value)); |
| 334 | EXPECT_EQ(12345, value); |
| 335 | EXPECT_TRUE(IsAtLeast(CreateConst(12345), &value)); |
| 336 | EXPECT_EQ(12345, value); |
| 337 | // Constant trivial range. |
| 338 | EXPECT_TRUE(IsExact(CreateRange(111, 111), &value)); |
| 339 | EXPECT_EQ(111, value); |
| 340 | EXPECT_TRUE(IsAtMost(CreateRange(111, 111), &value)); |
| 341 | EXPECT_EQ(111, value); |
| 342 | EXPECT_TRUE(IsAtLeast(CreateRange(111, 111), &value)); |
| 343 | EXPECT_EQ(111, value); |
| 344 | // Constant non-trivial range. |
| 345 | EXPECT_FALSE(IsExact(CreateRange(11, 22), &value)); |
| 346 | EXPECT_TRUE(IsAtMost(CreateRange(11, 22), &value)); |
| 347 | EXPECT_EQ(22, value); |
| 348 | EXPECT_TRUE(IsAtLeast(CreateRange(11, 22), &value)); |
| 349 | EXPECT_EQ(11, value); |
| 350 | // Symbolic. |
| 351 | EXPECT_FALSE(IsExact(CreateFetch(x_), &value)); |
| 352 | EXPECT_FALSE(IsAtMost(CreateFetch(x_), &value)); |
| 353 | EXPECT_FALSE(IsAtLeast(CreateFetch(x_), &value)); |
| 354 | } |
| 355 | |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 356 | TEST_F(InductionVarRangeTest, TripCountProperties) { |
| 357 | EXPECT_FALSE(NeedsTripCount(nullptr)); |
| 358 | EXPECT_FALSE(NeedsTripCount(CreateConst(1))); |
| 359 | EXPECT_TRUE(NeedsTripCount(CreateLinear(1, 1))); |
| 360 | EXPECT_FALSE(NeedsTripCount(CreateWrapAround(1, 2, 3))); |
| 361 | EXPECT_TRUE(NeedsTripCount(CreateWrapAround(1, CreateLinear(1, 1)))); |
| 362 | |
| 363 | EXPECT_FALSE(IsBodyTripCount(nullptr)); |
| 364 | EXPECT_FALSE(IsBodyTripCount(CreateTripCount(100, true, true))); |
| 365 | EXPECT_FALSE(IsBodyTripCount(CreateTripCount(100, true, false))); |
| 366 | EXPECT_TRUE(IsBodyTripCount(CreateTripCount(100, false, true))); |
| 367 | EXPECT_TRUE(IsBodyTripCount(CreateTripCount(100, false, false))); |
| 368 | |
| 369 | EXPECT_FALSE(IsUnsafeTripCount(nullptr)); |
| 370 | EXPECT_FALSE(IsUnsafeTripCount(CreateTripCount(100, true, true))); |
| 371 | EXPECT_TRUE(IsUnsafeTripCount(CreateTripCount(100, true, false))); |
| 372 | EXPECT_FALSE(IsUnsafeTripCount(CreateTripCount(100, false, true))); |
| 373 | EXPECT_TRUE(IsUnsafeTripCount(CreateTripCount(100, false, false))); |
| 374 | } |
| 375 | |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 376 | TEST_F(InductionVarRangeTest, GetMinMaxNull) { |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 377 | ExpectEqual(Value(), GetMin(nullptr, nullptr)); |
| 378 | ExpectEqual(Value(), GetMax(nullptr, nullptr)); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | TEST_F(InductionVarRangeTest, GetMinMaxAdd) { |
| 382 | ExpectEqual(Value(12), |
| 383 | GetMin(CreateInvariant('+', CreateConst(2), CreateRange(10, 20)), nullptr)); |
| 384 | ExpectEqual(Value(22), |
| 385 | GetMax(CreateInvariant('+', CreateConst(2), CreateRange(10, 20)), nullptr)); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 386 | ExpectEqual(Value(x_, 1, -20), |
| 387 | GetMin(CreateInvariant('+', CreateFetch(x_), CreateRange(-20, -10)), nullptr)); |
| 388 | ExpectEqual(Value(x_, 1, -10), |
| 389 | GetMax(CreateInvariant('+', CreateFetch(x_), CreateRange(-20, -10)), nullptr)); |
| 390 | ExpectEqual(Value(x_, 1, 10), |
| 391 | GetMin(CreateInvariant('+', CreateRange(10, 20), CreateFetch(x_)), nullptr)); |
| 392 | ExpectEqual(Value(x_, 1, 20), |
| 393 | GetMax(CreateInvariant('+', CreateRange(10, 20), CreateFetch(x_)), nullptr)); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 394 | ExpectEqual(Value(5), |
| 395 | GetMin(CreateInvariant('+', CreateRange(-5, -1), CreateRange(10, 20)), nullptr)); |
| 396 | ExpectEqual(Value(19), |
| 397 | GetMax(CreateInvariant('+', CreateRange(-5, -1), CreateRange(10, 20)), nullptr)); |
| 398 | } |
| 399 | |
| 400 | TEST_F(InductionVarRangeTest, GetMinMaxSub) { |
| 401 | ExpectEqual(Value(-18), |
| 402 | GetMin(CreateInvariant('-', CreateConst(2), CreateRange(10, 20)), nullptr)); |
| 403 | ExpectEqual(Value(-8), |
| 404 | GetMax(CreateInvariant('-', CreateConst(2), CreateRange(10, 20)), nullptr)); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 405 | ExpectEqual(Value(x_, 1, 10), |
| 406 | GetMin(CreateInvariant('-', CreateFetch(x_), CreateRange(-20, -10)), nullptr)); |
| 407 | ExpectEqual(Value(x_, 1, 20), |
| 408 | GetMax(CreateInvariant('-', CreateFetch(x_), CreateRange(-20, -10)), nullptr)); |
| 409 | ExpectEqual(Value(x_, -1, 10), |
| 410 | GetMin(CreateInvariant('-', CreateRange(10, 20), CreateFetch(x_)), nullptr)); |
| 411 | ExpectEqual(Value(x_, -1, 20), |
| 412 | GetMax(CreateInvariant('-', CreateRange(10, 20), CreateFetch(x_)), nullptr)); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 413 | ExpectEqual(Value(-25), |
| 414 | GetMin(CreateInvariant('-', CreateRange(-5, -1), CreateRange(10, 20)), nullptr)); |
| 415 | ExpectEqual(Value(-11), |
| 416 | GetMax(CreateInvariant('-', CreateRange(-5, -1), CreateRange(10, 20)), nullptr)); |
| 417 | } |
| 418 | |
| 419 | TEST_F(InductionVarRangeTest, GetMinMaxNeg) { |
| 420 | ExpectEqual(Value(-20), GetMin(CreateInvariant('n', nullptr, CreateRange(10, 20)), nullptr)); |
| 421 | ExpectEqual(Value(-10), GetMax(CreateInvariant('n', nullptr, CreateRange(10, 20)), nullptr)); |
| 422 | ExpectEqual(Value(10), GetMin(CreateInvariant('n', nullptr, CreateRange(-20, -10)), nullptr)); |
| 423 | ExpectEqual(Value(20), GetMax(CreateInvariant('n', nullptr, CreateRange(-20, -10)), nullptr)); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 424 | ExpectEqual(Value(x_, -1, 0), GetMin(CreateInvariant('n', nullptr, CreateFetch(x_)), nullptr)); |
| 425 | ExpectEqual(Value(x_, -1, 0), GetMax(CreateInvariant('n', nullptr, CreateFetch(x_)), nullptr)); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | TEST_F(InductionVarRangeTest, GetMinMaxMul) { |
| 429 | ExpectEqual(Value(20), |
| 430 | GetMin(CreateInvariant('*', CreateConst(2), CreateRange(10, 20)), nullptr)); |
| 431 | ExpectEqual(Value(40), |
| 432 | GetMax(CreateInvariant('*', CreateConst(2), CreateRange(10, 20)), nullptr)); |
| 433 | } |
| 434 | |
| 435 | TEST_F(InductionVarRangeTest, GetMinMaxDiv) { |
| 436 | ExpectEqual(Value(3), |
| 437 | GetMin(CreateInvariant('/', CreateRange(12, 20), CreateConst(4)), nullptr)); |
| 438 | ExpectEqual(Value(5), |
| 439 | GetMax(CreateInvariant('/', CreateRange(12, 20), CreateConst(4)), nullptr)); |
| 440 | } |
| 441 | |
| 442 | TEST_F(InductionVarRangeTest, GetMinMaxConstant) { |
| 443 | ExpectEqual(Value(12345), GetMin(CreateConst(12345), nullptr)); |
| 444 | ExpectEqual(Value(12345), GetMax(CreateConst(12345), nullptr)); |
| 445 | } |
| 446 | |
| 447 | TEST_F(InductionVarRangeTest, GetMinMaxFetch) { |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 448 | ExpectEqual(Value(x_, 1, 0), GetMin(CreateFetch(x_), nullptr)); |
| 449 | ExpectEqual(Value(x_, 1, 0), GetMax(CreateFetch(x_), nullptr)); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | TEST_F(InductionVarRangeTest, GetMinMaxLinear) { |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 453 | ExpectEqual(Value(20), GetMin(CreateLinear(10, 20), CreateTripCount(100, true, true))); |
| 454 | ExpectEqual(Value(1010), GetMax(CreateLinear(10, 20), CreateTripCount(100, true, true))); |
| 455 | ExpectEqual(Value(-970), GetMin(CreateLinear(-10, 20), CreateTripCount(100, true, true))); |
| 456 | ExpectEqual(Value(20), GetMax(CreateLinear(-10, 20), CreateTripCount(100, true, true))); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | TEST_F(InductionVarRangeTest, GetMinMaxWrapAround) { |
| 460 | ExpectEqual(Value(-5), GetMin(CreateWrapAround(-5, -1, 10), nullptr)); |
| 461 | ExpectEqual(Value(10), GetMax(CreateWrapAround(-5, -1, 10), nullptr)); |
| 462 | ExpectEqual(Value(-1), GetMin(CreateWrapAround(2, -1, 10), nullptr)); |
| 463 | ExpectEqual(Value(10), GetMax(CreateWrapAround(2, -1, 10), nullptr)); |
| 464 | ExpectEqual(Value(-1), GetMin(CreateWrapAround(20, -1, 10), nullptr)); |
| 465 | ExpectEqual(Value(20), GetMax(CreateWrapAround(20, -1, 10), nullptr)); |
| 466 | } |
| 467 | |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 468 | TEST_F(InductionVarRangeTest, GetMinMaxPolynomial) { |
| 469 | ExpectEqual(Value(7), GetMin(CreatePolynomial(3, 5, 7), nullptr)); |
| 470 | ExpectEqual(Value(), GetMax(CreatePolynomial(3, 5, 7), nullptr)); |
| 471 | ExpectEqual(Value(7), GetMin(CreatePolynomial(3, 5, 7), CreateTripCount(5, true, true))); |
| 472 | ExpectEqual(Value(45), GetMax(CreatePolynomial(3, 5, 7), CreateTripCount(5, true, true))); |
| 473 | ExpectEqual(Value(7), GetMin(CreatePolynomial(3, 5, 7), CreateTripCount(10, true, true))); |
| 474 | ExpectEqual(Value(160), GetMax(CreatePolynomial(3, 5, 7), CreateTripCount(10, true, true))); |
| 475 | ExpectEqual(Value(-7), GetMin(CreatePolynomial(11, 13, -7), |
| 476 | CreateTripCount(5, true, true))); |
| 477 | ExpectEqual(Value(111), GetMax(CreatePolynomial(11, 13, -7), |
| 478 | CreateTripCount(5, true, true))); |
| 479 | ExpectEqual(Value(-7), GetMin(CreatePolynomial(11, 13, -7), |
| 480 | CreateTripCount(10, true, true))); |
| 481 | ExpectEqual(Value(506), GetMax(CreatePolynomial(11, 13, -7), |
| 482 | CreateTripCount(10, true, true))); |
| 483 | ExpectEqual(Value(), GetMin(CreatePolynomial(-3, 5, 7), CreateTripCount(10, true, true))); |
| 484 | ExpectEqual(Value(), GetMax(CreatePolynomial(-3, 5, 7), CreateTripCount(10, true, true))); |
| 485 | ExpectEqual(Value(), GetMin(CreatePolynomial(3, -5, 7), CreateTripCount(10, true, true))); |
| 486 | ExpectEqual(Value(), GetMax(CreatePolynomial(3, -5, 7), CreateTripCount(10, true, true))); |
| 487 | } |
| 488 | |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 489 | TEST_F(InductionVarRangeTest, GetMinMaxGeometricMul) { |
| 490 | ExpectEqual(Value(), GetMin(CreateGeometric(1, 1, 1, '*'), nullptr)); |
| 491 | ExpectEqual(Value(), GetMax(CreateGeometric(1, 1, 1, '*'), nullptr)); |
| 492 | } |
| 493 | |
| 494 | TEST_F(InductionVarRangeTest, GetMinMaxGeometricDiv) { |
| 495 | ExpectEqual(Value(5), GetMin(CreateGeometric(11, 5, 3, '/'), nullptr)); |
| 496 | ExpectEqual(Value(16), GetMax(CreateGeometric(11, 5, 3, '/'), nullptr)); |
| 497 | ExpectEqual(Value(-5), GetMin(CreateGeometric(11, -5, 3, '/'), nullptr)); |
| 498 | ExpectEqual(Value(6), GetMax(CreateGeometric(11, -5, 3, '/'), nullptr)); |
| 499 | ExpectEqual(Value(-6), GetMin(CreateGeometric(-11, 5, 3, '/'), nullptr)); |
| 500 | ExpectEqual(Value(5), GetMax(CreateGeometric(-11, 5, 3, '/'), nullptr)); |
| 501 | ExpectEqual(Value(-16), GetMin(CreateGeometric(-11, -5, 3, '/'), nullptr)); |
| 502 | ExpectEqual(Value(-5), GetMax(CreateGeometric(-11, -5, 3, '/'), nullptr)); |
| 503 | } |
| 504 | |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 505 | TEST_F(InductionVarRangeTest, GetMinMaxPeriodic) { |
| 506 | ExpectEqual(Value(-2), GetMin(CreateRange(-2, 99), nullptr)); |
| 507 | ExpectEqual(Value(99), GetMax(CreateRange(-2, 99), nullptr)); |
| 508 | } |
| 509 | |
| 510 | TEST_F(InductionVarRangeTest, GetMulMin) { |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 511 | ExpectEqual(Value(-14), GetMul(CreateConst(2), CreateRange(-7, 8), true)); |
| 512 | ExpectEqual(Value(-16), GetMul(CreateConst(-2), CreateRange(-7, 8), true)); |
| 513 | ExpectEqual(Value(-14), GetMul(CreateRange(-7, 8), CreateConst(2), true)); |
| 514 | ExpectEqual(Value(-16), GetMul(CreateRange(-7, 8), CreateConst(-2), true)); |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 515 | ExpectEqual(Value(6), GetMul(CreateRange(2, 10), CreateRange(3, 5), true)); |
| 516 | ExpectEqual(Value(-50), GetMul(CreateRange(2, 10), CreateRange(-5, -3), true)); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 517 | ExpectEqual(Value(), GetMul(CreateRange(2, 10), CreateRange(-1, 1), true)); |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 518 | ExpectEqual(Value(-50), GetMul(CreateRange(-10, -2), CreateRange(3, 5), true)); |
| 519 | ExpectEqual(Value(6), GetMul(CreateRange(-10, -2), CreateRange(-5, -3), true)); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 520 | ExpectEqual(Value(), GetMul(CreateRange(-10, -2), CreateRange(-1, 1), true)); |
| 521 | ExpectEqual(Value(), GetMul(CreateRange(-1, 1), CreateRange(2, 10), true)); |
| 522 | ExpectEqual(Value(), GetMul(CreateRange(-1, 1), CreateRange(-10, -2), true)); |
| 523 | ExpectEqual(Value(), GetMul(CreateRange(-1, 1), CreateRange(-1, 1), true)); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | TEST_F(InductionVarRangeTest, GetMulMax) { |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 527 | ExpectEqual(Value(16), GetMul(CreateConst(2), CreateRange(-7, 8), false)); |
| 528 | ExpectEqual(Value(14), GetMul(CreateConst(-2), CreateRange(-7, 8), false)); |
| 529 | ExpectEqual(Value(16), GetMul(CreateRange(-7, 8), CreateConst(2), false)); |
| 530 | ExpectEqual(Value(14), GetMul(CreateRange(-7, 8), CreateConst(-2), false)); |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 531 | ExpectEqual(Value(50), GetMul(CreateRange(2, 10), CreateRange(3, 5), false)); |
| 532 | ExpectEqual(Value(-6), GetMul(CreateRange(2, 10), CreateRange(-5, -3), false)); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 533 | ExpectEqual(Value(), GetMul(CreateRange(2, 10), CreateRange(-1, 1), false)); |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 534 | ExpectEqual(Value(-6), GetMul(CreateRange(-10, -2), CreateRange(3, 5), false)); |
| 535 | ExpectEqual(Value(50), GetMul(CreateRange(-10, -2), CreateRange(-5, -3), false)); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 536 | ExpectEqual(Value(), GetMul(CreateRange(-10, -2), CreateRange(-1, 1), false)); |
| 537 | ExpectEqual(Value(), GetMul(CreateRange(-1, 1), CreateRange(2, 10), false)); |
| 538 | ExpectEqual(Value(), GetMul(CreateRange(-1, 1), CreateRange(-10, -2), false)); |
| 539 | ExpectEqual(Value(), GetMul(CreateRange(-1, 1), CreateRange(-1, 1), false)); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | TEST_F(InductionVarRangeTest, GetDivMin) { |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 543 | ExpectEqual(Value(-5), GetDiv(CreateRange(-10, 20), CreateConst(2), true)); |
| 544 | ExpectEqual(Value(-10), GetDiv(CreateRange(-10, 20), CreateConst(-2), true)); |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 545 | ExpectEqual(Value(10), GetDiv(CreateRange(40, 1000), CreateRange(2, 4), true)); |
| 546 | ExpectEqual(Value(-500), GetDiv(CreateRange(40, 1000), CreateRange(-4, -2), true)); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 547 | ExpectEqual(Value(), GetDiv(CreateRange(40, 1000), CreateRange(-1, 1), true)); |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 548 | ExpectEqual(Value(-500), GetDiv(CreateRange(-1000, -40), CreateRange(2, 4), true)); |
| 549 | ExpectEqual(Value(10), GetDiv(CreateRange(-1000, -40), CreateRange(-4, -2), true)); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 550 | ExpectEqual(Value(), GetDiv(CreateRange(-1000, -40), CreateRange(-1, 1), true)); |
| 551 | ExpectEqual(Value(), GetDiv(CreateRange(-1, 1), CreateRange(40, 1000), true)); |
| 552 | ExpectEqual(Value(), GetDiv(CreateRange(-1, 1), CreateRange(-1000, -40), true)); |
| 553 | ExpectEqual(Value(), GetDiv(CreateRange(-1, 1), CreateRange(-1, 1), true)); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | TEST_F(InductionVarRangeTest, GetDivMax) { |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 557 | ExpectEqual(Value(10), GetDiv(CreateRange(-10, 20), CreateConst(2), false)); |
| 558 | ExpectEqual(Value(5), GetDiv(CreateRange(-10, 20), CreateConst(-2), false)); |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 559 | ExpectEqual(Value(500), GetDiv(CreateRange(40, 1000), CreateRange(2, 4), false)); |
| 560 | ExpectEqual(Value(-10), GetDiv(CreateRange(40, 1000), CreateRange(-4, -2), false)); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 561 | ExpectEqual(Value(), GetDiv(CreateRange(40, 1000), CreateRange(-1, 1), false)); |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 562 | ExpectEqual(Value(-10), GetDiv(CreateRange(-1000, -40), CreateRange(2, 4), false)); |
| 563 | ExpectEqual(Value(500), GetDiv(CreateRange(-1000, -40), CreateRange(-4, -2), false)); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 564 | ExpectEqual(Value(), GetDiv(CreateRange(-1000, -40), CreateRange(-1, 1), false)); |
| 565 | ExpectEqual(Value(), GetDiv(CreateRange(-1, 1), CreateRange(40, 1000), false)); |
| 566 | ExpectEqual(Value(), GetDiv(CreateRange(-1, 1), CreateRange(-1000, 40), false)); |
| 567 | ExpectEqual(Value(), GetDiv(CreateRange(-1, 1), CreateRange(-1, 1), false)); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 568 | } |
| 569 | |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 570 | TEST_F(InductionVarRangeTest, GetMinMaxRem) { |
| 571 | ExpectEqual(Value(), GetMin(CreateInvariant('%', CreateConst(2), CreateRange(10, 20)), nullptr)); |
| 572 | ExpectEqual(Value(), GetMax(CreateInvariant('%', CreateConst(2), CreateRange(10, 20)), nullptr)); |
| 573 | ExpectEqual(Value(), GetMin(CreateInvariant('%', CreateRange(10, 20), CreateConst(2)), nullptr)); |
| 574 | ExpectEqual(Value(), GetMax(CreateInvariant('%', CreateRange(10, 20), CreateConst(2)), nullptr)); |
| 575 | ExpectEqual(Value(2), GetMin(CreateInvariant('%', CreateConst(2), CreateConst(5)), nullptr)); |
| 576 | ExpectEqual(Value(2), GetMax(CreateInvariant('%', CreateConst(2), CreateConst(5)), nullptr)); |
| 577 | ExpectEqual(Value(1), GetMin(CreateInvariant('%', CreateConst(11), CreateConst(5)), nullptr)); |
| 578 | ExpectEqual(Value(1), GetMax(CreateInvariant('%', CreateConst(11), CreateConst(5)), nullptr)); |
| 579 | } |
| 580 | |
| 581 | TEST_F(InductionVarRangeTest, GetRem) { |
| 582 | ExpectEqual(Value(0), GetRem(CreateConst(1), CreateConst(1))); |
| 583 | ExpectEqual(Value(2), GetRem(CreateConst(2), CreateConst(5))); |
| 584 | ExpectEqual(Value(1), GetRem(CreateConst(11), CreateConst(5))); |
| 585 | ExpectEqual(Value(-2), GetRem(CreateConst(-2), CreateConst(5))); |
| 586 | ExpectEqual(Value(-1), GetRem(CreateConst(-11), CreateConst(5))); |
| 587 | ExpectEqual(Value(2), GetRem(CreateConst(2), CreateConst(-5))); |
| 588 | ExpectEqual(Value(1), GetRem(CreateConst(11), CreateConst(-5))); |
| 589 | ExpectEqual(Value(-2), GetRem(CreateConst(-2), CreateConst(-5))); |
| 590 | ExpectEqual(Value(-1), GetRem(CreateConst(-11), CreateConst(-5))); |
| 591 | ExpectEqual(Value(), GetRem(CreateConst(1), CreateConst(0))); |
| 592 | } |
| 593 | |
| 594 | TEST_F(InductionVarRangeTest, GetMinMaxXor) { |
| 595 | ExpectEqual(Value(), GetMin(CreateInvariant('^', CreateConst(2), CreateRange(10, 20)), nullptr)); |
| 596 | ExpectEqual(Value(), GetMax(CreateInvariant('^', CreateConst(2), CreateRange(10, 20)), nullptr)); |
| 597 | ExpectEqual(Value(), GetMin(CreateInvariant('^', CreateRange(10, 20), CreateConst(2)), nullptr)); |
| 598 | ExpectEqual(Value(), GetMax(CreateInvariant('^', CreateRange(10, 20), CreateConst(2)), nullptr)); |
| 599 | ExpectEqual(Value(3), GetMin(CreateInvariant('^', CreateConst(1), CreateConst(2)), nullptr)); |
| 600 | ExpectEqual(Value(3), GetMax(CreateInvariant('^', CreateConst(1), CreateConst(2)), nullptr)); |
| 601 | } |
| 602 | |
| 603 | TEST_F(InductionVarRangeTest, GetXor) { |
| 604 | ExpectEqual(Value(0), GetXor(CreateConst(1), CreateConst(1))); |
| 605 | ExpectEqual(Value(3), GetXor(CreateConst(1), CreateConst(2))); |
| 606 | ExpectEqual(Value(-2), GetXor(CreateConst(1), CreateConst(-1))); |
| 607 | ExpectEqual(Value(0), GetXor(CreateConst(-1), CreateConst(-1))); |
| 608 | } |
| 609 | |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 610 | TEST_F(InductionVarRangeTest, AddValue) { |
| 611 | ExpectEqual(Value(110), AddValue(Value(10), Value(100))); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 612 | ExpectEqual(Value(-5), AddValue(Value(x_, 1, -4), Value(x_, -1, -1))); |
| 613 | ExpectEqual(Value(x_, 3, -5), AddValue(Value(x_, 2, -4), Value(x_, 1, -1))); |
| 614 | ExpectEqual(Value(), AddValue(Value(x_, 1, 5), Value(y_, 1, -7))); |
| 615 | ExpectEqual(Value(x_, 1, 23), AddValue(Value(x_, 1, 20), Value(3))); |
| 616 | ExpectEqual(Value(y_, 1, 5), AddValue(Value(55), Value(y_, 1, -50))); |
Aart Bik | cd26feb | 2015-09-23 17:50:50 -0700 | [diff] [blame] | 617 | const int32_t max_value = std::numeric_limits<int32_t>::max(); |
| 618 | ExpectEqual(Value(max_value), AddValue(Value(max_value - 5), Value(5))); |
| 619 | ExpectEqual(Value(), AddValue(Value(max_value - 5), Value(6))); // unsafe |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | TEST_F(InductionVarRangeTest, SubValue) { |
| 623 | ExpectEqual(Value(-90), SubValue(Value(10), Value(100))); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 624 | ExpectEqual(Value(-3), SubValue(Value(x_, 1, -4), Value(x_, 1, -1))); |
| 625 | ExpectEqual(Value(x_, 2, -3), SubValue(Value(x_, 3, -4), Value(x_, 1, -1))); |
| 626 | ExpectEqual(Value(), SubValue(Value(x_, 1, 5), Value(y_, 1, -7))); |
| 627 | ExpectEqual(Value(x_, 1, 17), SubValue(Value(x_, 1, 20), Value(3))); |
| 628 | ExpectEqual(Value(y_, -4, 105), SubValue(Value(55), Value(y_, 4, -50))); |
Aart Bik | cd26feb | 2015-09-23 17:50:50 -0700 | [diff] [blame] | 629 | const int32_t min_value = std::numeric_limits<int32_t>::min(); |
| 630 | ExpectEqual(Value(min_value), SubValue(Value(min_value + 5), Value(5))); |
| 631 | ExpectEqual(Value(), SubValue(Value(min_value + 5), Value(6))); // unsafe |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | TEST_F(InductionVarRangeTest, MulValue) { |
| 635 | ExpectEqual(Value(1000), MulValue(Value(10), Value(100))); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 636 | ExpectEqual(Value(), MulValue(Value(x_, 1, -4), Value(x_, 1, -1))); |
| 637 | ExpectEqual(Value(), MulValue(Value(x_, 1, 5), Value(y_, 1, -7))); |
| 638 | ExpectEqual(Value(x_, 9, 60), MulValue(Value(x_, 3, 20), Value(3))); |
| 639 | ExpectEqual(Value(y_, 55, -110), MulValue(Value(55), Value(y_, 1, -2))); |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 640 | ExpectEqual(Value(), MulValue(Value(90000), Value(-90000))); // unsafe |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 641 | } |
| 642 | |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 643 | TEST_F(InductionVarRangeTest, MulValueSpecial) { |
| 644 | const int32_t min_value = std::numeric_limits<int32_t>::min(); |
| 645 | const int32_t max_value = std::numeric_limits<int32_t>::max(); |
| 646 | |
| 647 | // Unsafe. |
| 648 | ExpectEqual(Value(), MulValue(Value(min_value), Value(min_value))); |
| 649 | ExpectEqual(Value(), MulValue(Value(min_value), Value(-1))); |
| 650 | ExpectEqual(Value(), MulValue(Value(min_value), Value(max_value))); |
| 651 | ExpectEqual(Value(), MulValue(Value(max_value), Value(max_value))); |
| 652 | |
| 653 | // Safe. |
| 654 | ExpectEqual(Value(min_value), MulValue(Value(min_value), Value(1))); |
| 655 | ExpectEqual(Value(max_value), MulValue(Value(max_value), Value(1))); |
| 656 | ExpectEqual(Value(-max_value), MulValue(Value(max_value), Value(-1))); |
| 657 | ExpectEqual(Value(-1), MulValue(Value(1), Value(-1))); |
| 658 | ExpectEqual(Value(1), MulValue(Value(-1), Value(-1))); |
| 659 | } |
| 660 | |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 661 | TEST_F(InductionVarRangeTest, DivValue) { |
| 662 | ExpectEqual(Value(25), DivValue(Value(100), Value(4))); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 663 | ExpectEqual(Value(), DivValue(Value(x_, 1, -4), Value(x_, 1, -1))); |
| 664 | ExpectEqual(Value(), DivValue(Value(x_, 1, 5), Value(y_, 1, -7))); |
| 665 | ExpectEqual(Value(), DivValue(Value(x_, 12, 24), Value(3))); |
| 666 | ExpectEqual(Value(), DivValue(Value(55), Value(y_, 1, -50))); |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 667 | ExpectEqual(Value(), DivValue(Value(1), Value(0))); // unsafe |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 668 | } |
| 669 | |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 670 | TEST_F(InductionVarRangeTest, DivValueSpecial) { |
| 671 | const int32_t min_value = std::numeric_limits<int32_t>::min(); |
| 672 | const int32_t max_value = std::numeric_limits<int32_t>::max(); |
| 673 | |
| 674 | // Unsafe. |
| 675 | ExpectEqual(Value(), DivValue(Value(min_value), Value(-1))); |
| 676 | |
| 677 | // Safe. |
| 678 | ExpectEqual(Value(1), DivValue(Value(min_value), Value(min_value))); |
| 679 | ExpectEqual(Value(1), DivValue(Value(max_value), Value(max_value))); |
| 680 | ExpectEqual(Value(min_value), DivValue(Value(min_value), Value(1))); |
| 681 | ExpectEqual(Value(max_value), DivValue(Value(max_value), Value(1))); |
| 682 | ExpectEqual(Value(-max_value), DivValue(Value(max_value), Value(-1))); |
| 683 | ExpectEqual(Value(-1), DivValue(Value(1), Value(-1))); |
| 684 | ExpectEqual(Value(1), DivValue(Value(-1), Value(-1))); |
| 685 | } |
| 686 | |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 687 | TEST_F(InductionVarRangeTest, MinValue) { |
| 688 | ExpectEqual(Value(10), MinValue(Value(10), Value(100))); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 689 | ExpectEqual(Value(x_, 1, -4), MinValue(Value(x_, 1, -4), Value(x_, 1, -1))); |
| 690 | ExpectEqual(Value(x_, 4, -4), MinValue(Value(x_, 4, -4), Value(x_, 4, -1))); |
| 691 | ExpectEqual(Value(), MinValue(Value(x_, 1, 5), Value(y_, 1, -7))); |
| 692 | ExpectEqual(Value(), MinValue(Value(x_, 1, 20), Value(3))); |
| 693 | ExpectEqual(Value(), MinValue(Value(55), Value(y_, 1, -50))); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | TEST_F(InductionVarRangeTest, MaxValue) { |
| 697 | ExpectEqual(Value(100), MaxValue(Value(10), Value(100))); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 698 | ExpectEqual(Value(x_, 1, -1), MaxValue(Value(x_, 1, -4), Value(x_, 1, -1))); |
| 699 | ExpectEqual(Value(x_, 4, -1), MaxValue(Value(x_, 4, -4), Value(x_, 4, -1))); |
| 700 | ExpectEqual(Value(), MaxValue(Value(x_, 1, 5), Value(y_, 1, -7))); |
| 701 | ExpectEqual(Value(), MaxValue(Value(x_, 1, 20), Value(3))); |
| 702 | ExpectEqual(Value(), MaxValue(Value(55), Value(y_, 1, -50))); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 703 | } |
| 704 | |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 705 | TEST_F(InductionVarRangeTest, ArrayLengthAndHints) { |
Nicolas Geoffray | e761bcc | 2017-01-19 08:59:37 +0000 | [diff] [blame] | 706 | // We pass a bogus constant for the class to avoid mocking one. |
| 707 | HInstruction* new_array = new (&allocator_) HNewArray(x_, x_, 0); |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 708 | entry_block_->AddInstruction(new_array); |
| 709 | HInstruction* array_length = new (&allocator_) HArrayLength(new_array, 0); |
| 710 | entry_block_->AddInstruction(array_length); |
| 711 | // With null hint: yields extreme constants. |
| 712 | const int32_t max_value = std::numeric_limits<int32_t>::max(); |
| 713 | SetHint(nullptr); |
| 714 | ExpectEqual(Value(0), GetMin(CreateFetch(array_length), nullptr)); |
| 715 | ExpectEqual(Value(max_value), GetMax(CreateFetch(array_length), nullptr)); |
| 716 | // With explicit hint: yields the length instruction. |
| 717 | SetHint(array_length); |
| 718 | ExpectEqual(Value(array_length, 1, 0), GetMin(CreateFetch(array_length), nullptr)); |
| 719 | ExpectEqual(Value(array_length, 1, 0), GetMax(CreateFetch(array_length), nullptr)); |
| 720 | // With any non-null hint: chases beyond the length instruction. |
| 721 | SetHint(x_); |
| 722 | ExpectEqual(Value(x_, 1, 0), GetMin(CreateFetch(array_length), nullptr)); |
| 723 | ExpectEqual(Value(x_, 1, 0), GetMax(CreateFetch(array_length), nullptr)); |
| 724 | } |
| 725 | |
Aart Bik | 8e9090b | 2017-09-08 16:46:50 -0700 | [diff] [blame^] | 726 | TEST_F(InductionVarRangeTest, AddOrSubAndConstant) { |
| 727 | HInstruction* add = new (&allocator_) |
| 728 | HAdd(Primitive::kPrimInt, x_, graph_->GetIntConstant(-1)); |
| 729 | HInstruction* alt = new (&allocator_) |
| 730 | HAdd(Primitive::kPrimInt, graph_->GetIntConstant(-1), x_); |
| 731 | HInstruction* sub = new (&allocator_) |
| 732 | HSub(Primitive::kPrimInt, x_, graph_->GetIntConstant(1)); |
| 733 | HInstruction* rev = new (&allocator_) |
| 734 | HSub(Primitive::kPrimInt, graph_->GetIntConstant(1), x_); |
| 735 | entry_block_->AddInstruction(add); |
| 736 | entry_block_->AddInstruction(alt); |
| 737 | entry_block_->AddInstruction(sub); |
| 738 | entry_block_->AddInstruction(rev); |
| 739 | ExpectEqual(Value(x_, 1, -1), GetMin(CreateFetch(add), nullptr)); |
| 740 | ExpectEqual(Value(x_, 1, -1), GetMax(CreateFetch(add), nullptr)); |
| 741 | ExpectEqual(Value(x_, 1, -1), GetMin(CreateFetch(alt), nullptr)); |
| 742 | ExpectEqual(Value(x_, 1, -1), GetMax(CreateFetch(alt), nullptr)); |
| 743 | ExpectEqual(Value(x_, 1, -1), GetMin(CreateFetch(sub), nullptr)); |
| 744 | ExpectEqual(Value(x_, 1, -1), GetMax(CreateFetch(sub), nullptr)); |
| 745 | ExpectEqual(Value(x_, -1, 1), GetMin(CreateFetch(rev), nullptr)); |
| 746 | ExpectEqual(Value(x_, -1, 1), GetMax(CreateFetch(rev), nullptr)); |
| 747 | } |
| 748 | |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 749 | // |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 750 | // Tests on public methods. |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 751 | // |
| 752 | |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 753 | TEST_F(InductionVarRangeTest, ConstantTripCountUp) { |
| 754 | BuildLoop(0, graph_->GetIntConstant(1000), 1); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 755 | PerformInductionVarAnalysis(); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 756 | |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 757 | Value v1, v2; |
| 758 | bool needs_finite_test = true; |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 759 | bool needs_taken_test = true; |
| 760 | |
| 761 | HInstruction* phi = condition_->InputAt(0); |
| 762 | HInstruction* exit = exit_block_->GetLastInstruction(); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 763 | |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 764 | // In context of header: known. |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 765 | range_.GetInductionRange(condition_, phi, x_, &v1, &v2, &needs_finite_test); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 766 | EXPECT_FALSE(needs_finite_test); |
| 767 | ExpectEqual(Value(0), v1); |
| 768 | ExpectEqual(Value(1000), v2); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 769 | |
| 770 | // In context of loop-body: known. |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 771 | range_.GetInductionRange(increment_, phi, x_, &v1, &v2, &needs_finite_test); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 772 | EXPECT_FALSE(needs_finite_test); |
| 773 | ExpectEqual(Value(0), v1); |
| 774 | ExpectEqual(Value(999), v2); |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 775 | range_.GetInductionRange(increment_, increment_, x_, &v1, &v2, &needs_finite_test); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 776 | EXPECT_FALSE(needs_finite_test); |
| 777 | ExpectEqual(Value(1), v1); |
| 778 | ExpectEqual(Value(1000), v2); |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 779 | |
| 780 | // Induction vs. no-induction. |
| 781 | EXPECT_TRUE(range_.CanGenerateRange(increment_, phi, &needs_finite_test, &needs_taken_test)); |
| 782 | EXPECT_TRUE(range_.CanGenerateLastValue(phi)); |
| 783 | EXPECT_FALSE(range_.CanGenerateRange(exit, exit, &needs_finite_test, &needs_taken_test)); |
| 784 | EXPECT_FALSE(range_.CanGenerateLastValue(exit)); |
| 785 | |
| 786 | // Last value (unsimplified). |
| 787 | HInstruction* last = range_.GenerateLastValue(phi, graph_, loop_preheader_); |
| 788 | ASSERT_TRUE(last->IsAdd()); |
Aart Bik | 8e02e3e | 2017-02-28 14:41:55 -0800 | [diff] [blame] | 789 | ExpectInt(1000, last->InputAt(0)); |
| 790 | ExpectInt(0, last->InputAt(1)); |
| 791 | |
| 792 | // Loop logic. |
| 793 | int64_t tc = 0; |
| 794 | EXPECT_TRUE(range_.IsFinite(loop_header_->GetLoopInformation(), &tc)); |
| 795 | EXPECT_EQ(1000, tc); |
| 796 | HInstruction* offset = nullptr; |
Aart Bik | 37dc4df | 2017-06-28 14:08:00 -0700 | [diff] [blame] | 797 | EXPECT_TRUE(range_.IsUnitStride(phi, phi, graph_, &offset)); |
| 798 | ExpectInt(0, offset); |
Aart Bik | 8e02e3e | 2017-02-28 14:41:55 -0800 | [diff] [blame] | 799 | HInstruction* tce = range_.GenerateTripCount( |
| 800 | loop_header_->GetLoopInformation(), graph_, loop_preheader_); |
| 801 | ASSERT_TRUE(tce != nullptr); |
| 802 | ExpectInt(1000, tce); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 803 | } |
| 804 | |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 805 | TEST_F(InductionVarRangeTest, ConstantTripCountDown) { |
| 806 | BuildLoop(1000, graph_->GetIntConstant(0), -1); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 807 | PerformInductionVarAnalysis(); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 808 | |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 809 | Value v1, v2; |
| 810 | bool needs_finite_test = true; |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 811 | bool needs_taken_test = true; |
| 812 | |
| 813 | HInstruction* phi = condition_->InputAt(0); |
| 814 | HInstruction* exit = exit_block_->GetLastInstruction(); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 815 | |
| 816 | // In context of header: known. |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 817 | range_.GetInductionRange(condition_, phi, x_, &v1, &v2, &needs_finite_test); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 818 | EXPECT_FALSE(needs_finite_test); |
| 819 | ExpectEqual(Value(0), v1); |
| 820 | ExpectEqual(Value(1000), v2); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 821 | |
| 822 | // In context of loop-body: known. |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 823 | range_.GetInductionRange(increment_, phi, x_, &v1, &v2, &needs_finite_test); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 824 | EXPECT_FALSE(needs_finite_test); |
| 825 | ExpectEqual(Value(1), v1); |
| 826 | ExpectEqual(Value(1000), v2); |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 827 | range_.GetInductionRange(increment_, increment_, x_, &v1, &v2, &needs_finite_test); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 828 | EXPECT_FALSE(needs_finite_test); |
| 829 | ExpectEqual(Value(0), v1); |
| 830 | ExpectEqual(Value(999), v2); |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 831 | |
| 832 | // Induction vs. no-induction. |
| 833 | EXPECT_TRUE(range_.CanGenerateRange(increment_, phi, &needs_finite_test, &needs_taken_test)); |
| 834 | EXPECT_TRUE(range_.CanGenerateLastValue(phi)); |
| 835 | EXPECT_FALSE(range_.CanGenerateRange(exit, exit, &needs_finite_test, &needs_taken_test)); |
| 836 | EXPECT_FALSE(range_.CanGenerateLastValue(exit)); |
| 837 | |
| 838 | // Last value (unsimplified). |
| 839 | HInstruction* last = range_.GenerateLastValue(phi, graph_, loop_preheader_); |
| 840 | ASSERT_TRUE(last->IsSub()); |
Aart Bik | 8e02e3e | 2017-02-28 14:41:55 -0800 | [diff] [blame] | 841 | ExpectInt(1000, last->InputAt(0)); |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 842 | ASSERT_TRUE(last->InputAt(1)->IsNeg()); |
| 843 | last = last->InputAt(1)->InputAt(0); |
| 844 | ASSERT_TRUE(last->IsSub()); |
Aart Bik | 8e02e3e | 2017-02-28 14:41:55 -0800 | [diff] [blame] | 845 | ExpectInt(0, last->InputAt(0)); |
| 846 | ExpectInt(1000, last->InputAt(1)); |
| 847 | |
| 848 | // Loop logic. |
| 849 | int64_t tc = 0; |
| 850 | EXPECT_TRUE(range_.IsFinite(loop_header_->GetLoopInformation(), &tc)); |
| 851 | EXPECT_EQ(1000, tc); |
| 852 | HInstruction* offset = nullptr; |
Aart Bik | 37dc4df | 2017-06-28 14:08:00 -0700 | [diff] [blame] | 853 | EXPECT_FALSE(range_.IsUnitStride(phi, phi, graph_, &offset)); |
Aart Bik | 8e02e3e | 2017-02-28 14:41:55 -0800 | [diff] [blame] | 854 | HInstruction* tce = range_.GenerateTripCount( |
| 855 | loop_header_->GetLoopInformation(), graph_, loop_preheader_); |
| 856 | ASSERT_TRUE(tce != nullptr); |
| 857 | ASSERT_TRUE(tce->IsNeg()); |
| 858 | last = tce->InputAt(0); |
| 859 | EXPECT_TRUE(last->IsSub()); |
| 860 | ExpectInt(0, last->InputAt(0)); |
| 861 | ExpectInt(1000, last->InputAt(1)); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 862 | } |
| 863 | |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 864 | TEST_F(InductionVarRangeTest, SymbolicTripCountUp) { |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 865 | BuildLoop(0, x_, 1); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 866 | PerformInductionVarAnalysis(); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 867 | |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 868 | Value v1, v2; |
| 869 | bool needs_finite_test = true; |
| 870 | bool needs_taken_test = true; |
| 871 | |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 872 | HInstruction* phi = condition_->InputAt(0); |
| 873 | |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 874 | // In context of header: upper unknown. |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 875 | range_.GetInductionRange(condition_, phi, x_, &v1, &v2, &needs_finite_test); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 876 | EXPECT_FALSE(needs_finite_test); |
| 877 | ExpectEqual(Value(0), v1); |
| 878 | ExpectEqual(Value(), v2); |
| 879 | |
| 880 | // In context of loop-body: known. |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 881 | range_.GetInductionRange(increment_, phi, x_, &v1, &v2, &needs_finite_test); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 882 | EXPECT_FALSE(needs_finite_test); |
| 883 | ExpectEqual(Value(0), v1); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 884 | ExpectEqual(Value(x_, 1, -1), v2); |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 885 | range_.GetInductionRange(increment_, increment_, x_, &v1, &v2, &needs_finite_test); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 886 | EXPECT_FALSE(needs_finite_test); |
| 887 | ExpectEqual(Value(1), v1); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 888 | ExpectEqual(Value(x_, 1, 0), v2); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 889 | |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 890 | HInstruction* lower = nullptr; |
| 891 | HInstruction* upper = nullptr; |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 892 | |
| 893 | // Can generate code in context of loop-body only. |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 894 | EXPECT_FALSE(range_.CanGenerateRange(condition_, phi, &needs_finite_test, &needs_taken_test)); |
| 895 | ASSERT_TRUE(range_.CanGenerateRange(increment_, phi, &needs_finite_test, &needs_taken_test)); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 896 | EXPECT_FALSE(needs_finite_test); |
| 897 | EXPECT_TRUE(needs_taken_test); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 898 | |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 899 | // Generates code (unsimplified). |
| 900 | range_.GenerateRange(increment_, phi, graph_, loop_preheader_, &lower, &upper); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 901 | |
| 902 | // Verify lower is 0+0. |
| 903 | ASSERT_TRUE(lower != nullptr); |
| 904 | ASSERT_TRUE(lower->IsAdd()); |
Aart Bik | 8e02e3e | 2017-02-28 14:41:55 -0800 | [diff] [blame] | 905 | ExpectInt(0, lower->InputAt(0)); |
| 906 | ExpectInt(0, lower->InputAt(1)); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 907 | |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 908 | // Verify upper is (V-1)+0. |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 909 | ASSERT_TRUE(upper != nullptr); |
| 910 | ASSERT_TRUE(upper->IsAdd()); |
| 911 | ASSERT_TRUE(upper->InputAt(0)->IsSub()); |
| 912 | EXPECT_TRUE(upper->InputAt(0)->InputAt(0)->IsParameterValue()); |
Aart Bik | 8e02e3e | 2017-02-28 14:41:55 -0800 | [diff] [blame] | 913 | ExpectInt(1, upper->InputAt(0)->InputAt(1)); |
| 914 | ExpectInt(0, upper->InputAt(1)); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 915 | |
| 916 | // Verify taken-test is 0<V. |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 917 | HInstruction* taken = range_.GenerateTakenTest(increment_, graph_, loop_preheader_); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 918 | ASSERT_TRUE(taken != nullptr); |
| 919 | ASSERT_TRUE(taken->IsLessThan()); |
Aart Bik | 8e02e3e | 2017-02-28 14:41:55 -0800 | [diff] [blame] | 920 | ExpectInt(0, taken->InputAt(0)); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 921 | EXPECT_TRUE(taken->InputAt(1)->IsParameterValue()); |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 922 | |
| 923 | // Replacement. |
| 924 | range_.Replace(loop_header_->GetLastInstruction(), x_, y_); |
| 925 | range_.GetInductionRange(increment_, increment_, x_, &v1, &v2, &needs_finite_test); |
| 926 | EXPECT_FALSE(needs_finite_test); |
| 927 | ExpectEqual(Value(1), v1); |
| 928 | ExpectEqual(Value(y_, 1, 0), v2); |
Aart Bik | 8e02e3e | 2017-02-28 14:41:55 -0800 | [diff] [blame] | 929 | |
| 930 | // Loop logic. |
| 931 | int64_t tc = 0; |
| 932 | EXPECT_TRUE(range_.IsFinite(loop_header_->GetLoopInformation(), &tc)); |
| 933 | EXPECT_EQ(0, tc); // unknown |
| 934 | HInstruction* offset = nullptr; |
Aart Bik | 37dc4df | 2017-06-28 14:08:00 -0700 | [diff] [blame] | 935 | EXPECT_TRUE(range_.IsUnitStride(phi, phi, graph_, &offset)); |
| 936 | ExpectInt(0, offset); |
Aart Bik | 8e02e3e | 2017-02-28 14:41:55 -0800 | [diff] [blame] | 937 | HInstruction* tce = range_.GenerateTripCount( |
| 938 | loop_header_->GetLoopInformation(), graph_, loop_preheader_); |
| 939 | ASSERT_TRUE(tce != nullptr); |
| 940 | EXPECT_TRUE(tce->IsSelect()); // guarded by taken-test |
| 941 | ExpectInt(0, tce->InputAt(0)); |
| 942 | EXPECT_TRUE(tce->InputAt(1)->IsParameterValue()); |
| 943 | EXPECT_TRUE(tce->InputAt(2)->IsLessThan()); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 944 | } |
| 945 | |
| 946 | TEST_F(InductionVarRangeTest, SymbolicTripCountDown) { |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 947 | BuildLoop(1000, x_, -1); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 948 | PerformInductionVarAnalysis(); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 949 | |
| 950 | Value v1, v2; |
| 951 | bool needs_finite_test = true; |
| 952 | bool needs_taken_test = true; |
| 953 | |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 954 | HInstruction* phi = condition_->InputAt(0); |
| 955 | |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 956 | // In context of header: lower unknown. |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 957 | range_.GetInductionRange(condition_, phi, x_, &v1, &v2, &needs_finite_test); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 958 | EXPECT_FALSE(needs_finite_test); |
| 959 | ExpectEqual(Value(), v1); |
| 960 | ExpectEqual(Value(1000), v2); |
| 961 | |
| 962 | // In context of loop-body: known. |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 963 | range_.GetInductionRange(increment_, phi, x_, &v1, &v2, &needs_finite_test); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 964 | EXPECT_FALSE(needs_finite_test); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 965 | ExpectEqual(Value(x_, 1, 1), v1); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 966 | ExpectEqual(Value(1000), v2); |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 967 | range_.GetInductionRange(increment_, increment_, x_, &v1, &v2, &needs_finite_test); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 968 | EXPECT_FALSE(needs_finite_test); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 969 | ExpectEqual(Value(x_, 1, 0), v1); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 970 | ExpectEqual(Value(999), v2); |
| 971 | |
| 972 | HInstruction* lower = nullptr; |
| 973 | HInstruction* upper = nullptr; |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 974 | |
| 975 | // Can generate code in context of loop-body only. |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 976 | EXPECT_FALSE(range_.CanGenerateRange(condition_, phi, &needs_finite_test, &needs_taken_test)); |
| 977 | ASSERT_TRUE(range_.CanGenerateRange(increment_, phi, &needs_finite_test, &needs_taken_test)); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 978 | EXPECT_FALSE(needs_finite_test); |
| 979 | EXPECT_TRUE(needs_taken_test); |
| 980 | |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 981 | // Generates code (unsimplified). |
| 982 | range_.GenerateRange(increment_, phi, graph_, loop_preheader_, &lower, &upper); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 983 | |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 984 | // Verify lower is 1000-((1000-V)-1). |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 985 | ASSERT_TRUE(lower != nullptr); |
| 986 | ASSERT_TRUE(lower->IsSub()); |
Aart Bik | 8e02e3e | 2017-02-28 14:41:55 -0800 | [diff] [blame] | 987 | ExpectInt(1000, lower->InputAt(0)); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 988 | lower = lower->InputAt(1); |
| 989 | ASSERT_TRUE(lower->IsSub()); |
Aart Bik | 8e02e3e | 2017-02-28 14:41:55 -0800 | [diff] [blame] | 990 | ExpectInt(1, lower->InputAt(1)); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 991 | lower = lower->InputAt(0); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 992 | ASSERT_TRUE(lower->IsSub()); |
Aart Bik | 8e02e3e | 2017-02-28 14:41:55 -0800 | [diff] [blame] | 993 | ExpectInt(1000, lower->InputAt(0)); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 994 | EXPECT_TRUE(lower->InputAt(1)->IsParameterValue()); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 995 | |
| 996 | // Verify upper is 1000-0. |
| 997 | ASSERT_TRUE(upper != nullptr); |
| 998 | ASSERT_TRUE(upper->IsSub()); |
Aart Bik | 8e02e3e | 2017-02-28 14:41:55 -0800 | [diff] [blame] | 999 | ExpectInt(1000, upper->InputAt(0)); |
| 1000 | ExpectInt(0, upper->InputAt(1)); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 1001 | |
| 1002 | // Verify taken-test is 1000>V. |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 1003 | HInstruction* taken = range_.GenerateTakenTest(increment_, graph_, loop_preheader_); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 1004 | ASSERT_TRUE(taken != nullptr); |
| 1005 | ASSERT_TRUE(taken->IsGreaterThan()); |
Aart Bik | 8e02e3e | 2017-02-28 14:41:55 -0800 | [diff] [blame] | 1006 | ExpectInt(1000, taken->InputAt(0)); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 1007 | EXPECT_TRUE(taken->InputAt(1)->IsParameterValue()); |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame] | 1008 | |
| 1009 | // Replacement. |
| 1010 | range_.Replace(loop_header_->GetLastInstruction(), x_, y_); |
| 1011 | range_.GetInductionRange(increment_, increment_, x_, &v1, &v2, &needs_finite_test); |
| 1012 | EXPECT_FALSE(needs_finite_test); |
| 1013 | ExpectEqual(Value(y_, 1, 0), v1); |
| 1014 | ExpectEqual(Value(999), v2); |
Aart Bik | 8e02e3e | 2017-02-28 14:41:55 -0800 | [diff] [blame] | 1015 | |
| 1016 | // Loop logic. |
| 1017 | int64_t tc = 0; |
| 1018 | EXPECT_TRUE(range_.IsFinite(loop_header_->GetLoopInformation(), &tc)); |
| 1019 | EXPECT_EQ(0, tc); // unknown |
| 1020 | HInstruction* offset = nullptr; |
Aart Bik | 37dc4df | 2017-06-28 14:08:00 -0700 | [diff] [blame] | 1021 | EXPECT_FALSE(range_.IsUnitStride(phi, phi, graph_, &offset)); |
Aart Bik | 8e02e3e | 2017-02-28 14:41:55 -0800 | [diff] [blame] | 1022 | HInstruction* tce = range_.GenerateTripCount( |
| 1023 | loop_header_->GetLoopInformation(), graph_, loop_preheader_); |
| 1024 | ASSERT_TRUE(tce != nullptr); |
| 1025 | EXPECT_TRUE(tce->IsSelect()); // guarded by taken-test |
| 1026 | ExpectInt(0, tce->InputAt(0)); |
| 1027 | EXPECT_TRUE(tce->InputAt(1)->IsSub()); |
| 1028 | EXPECT_TRUE(tce->InputAt(2)->IsGreaterThan()); |
| 1029 | tce = tce->InputAt(1); |
| 1030 | ExpectInt(1000, taken->InputAt(0)); |
| 1031 | EXPECT_TRUE(taken->InputAt(1)->IsParameterValue()); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 1032 | } |
| 1033 | |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 1034 | } // namespace art |