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