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