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" |
| 19 | #include "gtest/gtest.h" |
| 20 | #include "induction_var_analysis.h" |
| 21 | #include "induction_var_range.h" |
| 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 | */ |
| 32 | class InductionVarRangeTest : public testing::Test { |
| 33 | public: |
| 34 | InductionVarRangeTest() : pool_(), allocator_(&pool_) { |
| 35 | graph_ = CreateGraph(&allocator_); |
| 36 | iva_ = new (&allocator_) HInductionVarAnalysis(graph_); |
| 37 | BuildGraph(); |
| 38 | } |
| 39 | |
| 40 | ~InductionVarRangeTest() { } |
| 41 | |
| 42 | void ExpectEqual(Value v1, Value v2) { |
| 43 | EXPECT_EQ(v1.instruction, v2.instruction); |
| 44 | EXPECT_EQ(v1.a_constant, v2.a_constant); |
| 45 | EXPECT_EQ(v1.b_constant, v2.b_constant); |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 46 | EXPECT_EQ(v1.is_known, v2.is_known); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | /** Constructs bare minimum graph. */ |
| 50 | void BuildGraph() { |
| 51 | graph_->SetNumberOfVRegs(1); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame^] | 52 | entry_block_ = new (&allocator_) HBasicBlock(graph_); |
| 53 | exit_block_ = new (&allocator_) HBasicBlock(graph_); |
| 54 | graph_->AddBlock(entry_block_); |
| 55 | graph_->AddBlock(exit_block_); |
| 56 | graph_->SetEntryBlock(entry_block_); |
| 57 | graph_->SetExitBlock(exit_block_); |
| 58 | } |
| 59 | |
| 60 | /** Constructs loop with given upper bound. */ |
| 61 | void BuildLoop(HInstruction* upper) { |
| 62 | // Control flow. |
| 63 | loop_preheader_ = new (&allocator_) HBasicBlock(graph_); |
| 64 | graph_->AddBlock(loop_preheader_); |
| 65 | HBasicBlock* loop_header = new (&allocator_) HBasicBlock(graph_); |
| 66 | graph_->AddBlock(loop_header); |
| 67 | HBasicBlock* loop_body = new (&allocator_) HBasicBlock(graph_); |
| 68 | graph_->AddBlock(loop_body); |
| 69 | entry_block_->AddSuccessor(loop_preheader_); |
| 70 | loop_preheader_->AddSuccessor(loop_header); |
| 71 | loop_header->AddSuccessor(loop_body); |
| 72 | loop_header->AddSuccessor(exit_block_); |
| 73 | loop_body->AddSuccessor(loop_header); |
| 74 | // Instructions. |
| 75 | HLocal* induc = new (&allocator_) HLocal(0); |
| 76 | entry_block_->AddInstruction(induc); |
| 77 | loop_preheader_->AddInstruction( |
| 78 | new (&allocator_) HStoreLocal(induc, graph_->GetIntConstant(0))); // i = 0 |
| 79 | loop_preheader_->AddInstruction(new (&allocator_) HGoto()); |
| 80 | HInstruction* load = new (&allocator_) HLoadLocal(induc, Primitive::kPrimInt); |
| 81 | loop_header->AddInstruction(load); |
| 82 | condition_ = new (&allocator_) HLessThan(load, upper); |
| 83 | loop_header->AddInstruction(condition_); |
| 84 | loop_header->AddInstruction(new (&allocator_) HIf(condition_)); // i < u |
| 85 | load = new (&allocator_) HLoadLocal(induc, Primitive::kPrimInt); |
| 86 | loop_body->AddInstruction(load); |
| 87 | increment_ = new (&allocator_) HAdd(Primitive::kPrimInt, load, graph_->GetIntConstant(1)); |
| 88 | loop_body->AddInstruction(increment_); |
| 89 | loop_body->AddInstruction(new (&allocator_) HStoreLocal(induc, increment_)); // i++ |
| 90 | loop_body->AddInstruction(new (&allocator_) HGoto()); |
| 91 | exit_block_->AddInstruction(new (&allocator_) HReturnVoid()); |
| 92 | } |
| 93 | |
| 94 | /** Performs induction variable analysis. */ |
| 95 | void PerformInductionVarAnalysis() { |
| 96 | ASSERT_TRUE(graph_->TryBuildingSsa()); |
| 97 | iva_->Run(); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | /** Constructs an invariant. */ |
| 101 | HInductionVarAnalysis::InductionInfo* CreateInvariant(char opc, |
| 102 | HInductionVarAnalysis::InductionInfo* a, |
| 103 | HInductionVarAnalysis::InductionInfo* b) { |
| 104 | HInductionVarAnalysis::InductionOp op; |
| 105 | switch (opc) { |
| 106 | case '+': op = HInductionVarAnalysis::kAdd; break; |
| 107 | case '-': op = HInductionVarAnalysis::kSub; break; |
| 108 | case 'n': op = HInductionVarAnalysis::kNeg; break; |
| 109 | case '*': op = HInductionVarAnalysis::kMul; break; |
| 110 | case '/': op = HInductionVarAnalysis::kDiv; break; |
| 111 | default: op = HInductionVarAnalysis::kNop; break; |
| 112 | } |
| 113 | return iva_->CreateInvariantOp(op, a, b); |
| 114 | } |
| 115 | |
| 116 | /** Constructs a fetch. */ |
| 117 | HInductionVarAnalysis::InductionInfo* CreateFetch(HInstruction* fetch) { |
| 118 | return iva_->CreateInvariantFetch(fetch); |
| 119 | } |
| 120 | |
| 121 | /** Constructs a constant. */ |
| 122 | HInductionVarAnalysis::InductionInfo* CreateConst(int32_t c) { |
| 123 | return CreateFetch(graph_->GetIntConstant(c)); |
| 124 | } |
| 125 | |
| 126 | /** Constructs a trip-count. */ |
| 127 | HInductionVarAnalysis::InductionInfo* CreateTripCount(int32_t tc) { |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 128 | return iva_->CreateTripCount(HInductionVarAnalysis::kTripCountInLoop, CreateConst(tc)); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | /** Constructs a linear a * i + b induction. */ |
| 132 | HInductionVarAnalysis::InductionInfo* CreateLinear(int32_t a, int32_t b) { |
| 133 | return iva_->CreateInduction(HInductionVarAnalysis::kLinear, CreateConst(a), CreateConst(b)); |
| 134 | } |
| 135 | |
| 136 | /** Constructs a range [lo, hi] using a periodic induction. */ |
| 137 | HInductionVarAnalysis::InductionInfo* CreateRange(int32_t lo, int32_t hi) { |
| 138 | return iva_->CreateInduction( |
| 139 | HInductionVarAnalysis::kPeriodic, CreateConst(lo), CreateConst(hi)); |
| 140 | } |
| 141 | |
| 142 | /** Constructs a wrap-around induction consisting of a constant, followed by a range. */ |
| 143 | HInductionVarAnalysis::InductionInfo* CreateWrapAround(int32_t initial, int32_t lo, int32_t hi) { |
| 144 | return iva_->CreateInduction( |
| 145 | HInductionVarAnalysis::kWrapAround, CreateConst(initial), CreateRange(lo, hi)); |
| 146 | } |
| 147 | |
| 148 | // |
| 149 | // Relay methods. |
| 150 | // |
| 151 | |
| 152 | Value GetMin(HInductionVarAnalysis::InductionInfo* info, |
| 153 | HInductionVarAnalysis::InductionInfo* induc) { |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 154 | return InductionVarRange::GetVal(info, induc, /* in_body */ true, /* is_min */ true); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | Value GetMax(HInductionVarAnalysis::InductionInfo* info, |
| 158 | HInductionVarAnalysis::InductionInfo* induc) { |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 159 | return InductionVarRange::GetVal(info, induc, /* in_body */ true, /* is_min */ false); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | Value GetMul(HInductionVarAnalysis::InductionInfo* info1, |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 163 | HInductionVarAnalysis::InductionInfo* info2, |
| 164 | bool is_min) { |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 165 | return InductionVarRange::GetMul(info1, info2, nullptr, /* in_body */ true, is_min); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | Value GetDiv(HInductionVarAnalysis::InductionInfo* info1, |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 169 | HInductionVarAnalysis::InductionInfo* info2, |
| 170 | bool is_min) { |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 171 | return InductionVarRange::GetDiv(info1, info2, nullptr, /* in_body */ true, is_min); |
| 172 | } |
| 173 | |
| 174 | bool GetConstant(HInductionVarAnalysis::InductionInfo* info, int32_t* value) { |
| 175 | return InductionVarRange::GetConstant(info, value); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 178 | Value AddValue(Value v1, Value v2) { return InductionVarRange::AddValue(v1, v2); } |
| 179 | Value SubValue(Value v1, Value v2) { return InductionVarRange::SubValue(v1, v2); } |
| 180 | Value MulValue(Value v1, Value v2) { return InductionVarRange::MulValue(v1, v2); } |
| 181 | Value DivValue(Value v1, Value v2) { return InductionVarRange::DivValue(v1, v2); } |
Aart Bik | cd26feb | 2015-09-23 17:50:50 -0700 | [diff] [blame] | 182 | Value MinValue(Value v1, Value v2) { return InductionVarRange::MergeVal(v1, v2, true); } |
| 183 | Value MaxValue(Value v1, Value v2) { return InductionVarRange::MergeVal(v1, v2, false); } |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 184 | |
| 185 | // General building fields. |
| 186 | ArenaPool pool_; |
| 187 | ArenaAllocator allocator_; |
| 188 | HGraph* graph_; |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame^] | 189 | HBasicBlock* entry_block_; |
| 190 | HBasicBlock* exit_block_; |
| 191 | HBasicBlock* loop_preheader_; |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 192 | HInductionVarAnalysis* iva_; |
| 193 | |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame^] | 194 | // Instructions. |
| 195 | HInstruction* condition_; |
| 196 | HInstruction* increment_; |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 197 | HReturnVoid x_; |
| 198 | HReturnVoid y_; |
| 199 | }; |
| 200 | |
| 201 | // |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame^] | 202 | // Tests on static methods. |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 203 | // |
| 204 | |
| 205 | TEST_F(InductionVarRangeTest, GetMinMaxNull) { |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 206 | ExpectEqual(Value(), GetMin(nullptr, nullptr)); |
| 207 | ExpectEqual(Value(), GetMax(nullptr, nullptr)); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | TEST_F(InductionVarRangeTest, GetMinMaxAdd) { |
| 211 | ExpectEqual(Value(12), |
| 212 | GetMin(CreateInvariant('+', CreateConst(2), CreateRange(10, 20)), nullptr)); |
| 213 | ExpectEqual(Value(22), |
| 214 | GetMax(CreateInvariant('+', CreateConst(2), CreateRange(10, 20)), nullptr)); |
| 215 | ExpectEqual(Value(&x_, 1, -20), |
| 216 | GetMin(CreateInvariant('+', CreateFetch(&x_), CreateRange(-20, -10)), nullptr)); |
| 217 | ExpectEqual(Value(&x_, 1, -10), |
| 218 | GetMax(CreateInvariant('+', CreateFetch(&x_), CreateRange(-20, -10)), nullptr)); |
| 219 | ExpectEqual(Value(&x_, 1, 10), |
| 220 | GetMin(CreateInvariant('+', CreateRange(10, 20), CreateFetch(&x_)), nullptr)); |
| 221 | ExpectEqual(Value(&x_, 1, 20), |
| 222 | GetMax(CreateInvariant('+', CreateRange(10, 20), CreateFetch(&x_)), nullptr)); |
| 223 | ExpectEqual(Value(5), |
| 224 | GetMin(CreateInvariant('+', CreateRange(-5, -1), CreateRange(10, 20)), nullptr)); |
| 225 | ExpectEqual(Value(19), |
| 226 | GetMax(CreateInvariant('+', CreateRange(-5, -1), CreateRange(10, 20)), nullptr)); |
| 227 | } |
| 228 | |
| 229 | TEST_F(InductionVarRangeTest, GetMinMaxSub) { |
| 230 | ExpectEqual(Value(-18), |
| 231 | GetMin(CreateInvariant('-', CreateConst(2), CreateRange(10, 20)), nullptr)); |
| 232 | ExpectEqual(Value(-8), |
| 233 | GetMax(CreateInvariant('-', CreateConst(2), CreateRange(10, 20)), nullptr)); |
| 234 | ExpectEqual(Value(&x_, 1, 10), |
| 235 | GetMin(CreateInvariant('-', CreateFetch(&x_), CreateRange(-20, -10)), nullptr)); |
| 236 | ExpectEqual(Value(&x_, 1, 20), |
| 237 | GetMax(CreateInvariant('-', CreateFetch(&x_), CreateRange(-20, -10)), nullptr)); |
| 238 | ExpectEqual(Value(&x_, -1, 10), |
| 239 | GetMin(CreateInvariant('-', CreateRange(10, 20), CreateFetch(&x_)), nullptr)); |
| 240 | ExpectEqual(Value(&x_, -1, 20), |
| 241 | GetMax(CreateInvariant('-', CreateRange(10, 20), CreateFetch(&x_)), nullptr)); |
| 242 | ExpectEqual(Value(-25), |
| 243 | GetMin(CreateInvariant('-', CreateRange(-5, -1), CreateRange(10, 20)), nullptr)); |
| 244 | ExpectEqual(Value(-11), |
| 245 | GetMax(CreateInvariant('-', CreateRange(-5, -1), CreateRange(10, 20)), nullptr)); |
| 246 | } |
| 247 | |
| 248 | TEST_F(InductionVarRangeTest, GetMinMaxNeg) { |
| 249 | ExpectEqual(Value(-20), GetMin(CreateInvariant('n', nullptr, CreateRange(10, 20)), nullptr)); |
| 250 | ExpectEqual(Value(-10), GetMax(CreateInvariant('n', nullptr, CreateRange(10, 20)), nullptr)); |
| 251 | ExpectEqual(Value(10), GetMin(CreateInvariant('n', nullptr, CreateRange(-20, -10)), nullptr)); |
| 252 | ExpectEqual(Value(20), GetMax(CreateInvariant('n', nullptr, CreateRange(-20, -10)), nullptr)); |
| 253 | ExpectEqual(Value(&x_, -1, 0), GetMin(CreateInvariant('n', nullptr, CreateFetch(&x_)), nullptr)); |
| 254 | ExpectEqual(Value(&x_, -1, 0), GetMax(CreateInvariant('n', nullptr, CreateFetch(&x_)), nullptr)); |
| 255 | } |
| 256 | |
| 257 | TEST_F(InductionVarRangeTest, GetMinMaxMul) { |
| 258 | ExpectEqual(Value(20), |
| 259 | GetMin(CreateInvariant('*', CreateConst(2), CreateRange(10, 20)), nullptr)); |
| 260 | ExpectEqual(Value(40), |
| 261 | GetMax(CreateInvariant('*', CreateConst(2), CreateRange(10, 20)), nullptr)); |
| 262 | } |
| 263 | |
| 264 | TEST_F(InductionVarRangeTest, GetMinMaxDiv) { |
| 265 | ExpectEqual(Value(3), |
| 266 | GetMin(CreateInvariant('/', CreateRange(12, 20), CreateConst(4)), nullptr)); |
| 267 | ExpectEqual(Value(5), |
| 268 | GetMax(CreateInvariant('/', CreateRange(12, 20), CreateConst(4)), nullptr)); |
| 269 | } |
| 270 | |
| 271 | TEST_F(InductionVarRangeTest, GetMinMaxConstant) { |
| 272 | ExpectEqual(Value(12345), GetMin(CreateConst(12345), nullptr)); |
| 273 | ExpectEqual(Value(12345), GetMax(CreateConst(12345), nullptr)); |
| 274 | } |
| 275 | |
| 276 | TEST_F(InductionVarRangeTest, GetMinMaxFetch) { |
| 277 | ExpectEqual(Value(&x_, 1, 0), GetMin(CreateFetch(&x_), nullptr)); |
| 278 | ExpectEqual(Value(&x_, 1, 0), GetMax(CreateFetch(&x_), nullptr)); |
| 279 | } |
| 280 | |
| 281 | TEST_F(InductionVarRangeTest, GetMinMaxLinear) { |
| 282 | ExpectEqual(Value(20), GetMin(CreateLinear(10, 20), CreateTripCount(100))); |
| 283 | ExpectEqual(Value(1010), GetMax(CreateLinear(10, 20), CreateTripCount(100))); |
| 284 | ExpectEqual(Value(-970), GetMin(CreateLinear(-10, 20), CreateTripCount(100))); |
| 285 | ExpectEqual(Value(20), GetMax(CreateLinear(-10, 20), CreateTripCount(100))); |
| 286 | } |
| 287 | |
| 288 | TEST_F(InductionVarRangeTest, GetMinMaxWrapAround) { |
| 289 | ExpectEqual(Value(-5), GetMin(CreateWrapAround(-5, -1, 10), nullptr)); |
| 290 | ExpectEqual(Value(10), GetMax(CreateWrapAround(-5, -1, 10), nullptr)); |
| 291 | ExpectEqual(Value(-1), GetMin(CreateWrapAround(2, -1, 10), nullptr)); |
| 292 | ExpectEqual(Value(10), GetMax(CreateWrapAround(2, -1, 10), nullptr)); |
| 293 | ExpectEqual(Value(-1), GetMin(CreateWrapAround(20, -1, 10), nullptr)); |
| 294 | ExpectEqual(Value(20), GetMax(CreateWrapAround(20, -1, 10), nullptr)); |
| 295 | } |
| 296 | |
| 297 | TEST_F(InductionVarRangeTest, GetMinMaxPeriodic) { |
| 298 | ExpectEqual(Value(-2), GetMin(CreateRange(-2, 99), nullptr)); |
| 299 | ExpectEqual(Value(99), GetMax(CreateRange(-2, 99), nullptr)); |
| 300 | } |
| 301 | |
| 302 | TEST_F(InductionVarRangeTest, GetMulMin) { |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 303 | ExpectEqual(Value(6), GetMul(CreateRange(2, 10), CreateRange(3, 5), true)); |
| 304 | ExpectEqual(Value(-50), GetMul(CreateRange(2, 10), CreateRange(-5, -3), true)); |
| 305 | ExpectEqual(Value(-50), GetMul(CreateRange(-10, -2), CreateRange(3, 5), true)); |
| 306 | ExpectEqual(Value(6), GetMul(CreateRange(-10, -2), CreateRange(-5, -3), true)); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | TEST_F(InductionVarRangeTest, GetMulMax) { |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 310 | ExpectEqual(Value(50), GetMul(CreateRange(2, 10), CreateRange(3, 5), false)); |
| 311 | ExpectEqual(Value(-6), GetMul(CreateRange(2, 10), CreateRange(-5, -3), false)); |
| 312 | ExpectEqual(Value(-6), GetMul(CreateRange(-10, -2), CreateRange(3, 5), false)); |
| 313 | ExpectEqual(Value(50), GetMul(CreateRange(-10, -2), CreateRange(-5, -3), false)); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | TEST_F(InductionVarRangeTest, GetDivMin) { |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 317 | ExpectEqual(Value(10), GetDiv(CreateRange(40, 1000), CreateRange(2, 4), true)); |
| 318 | ExpectEqual(Value(-500), GetDiv(CreateRange(40, 1000), CreateRange(-4, -2), true)); |
| 319 | ExpectEqual(Value(-500), GetDiv(CreateRange(-1000, -40), CreateRange(2, 4), true)); |
| 320 | ExpectEqual(Value(10), GetDiv(CreateRange(-1000, -40), CreateRange(-4, -2), true)); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | TEST_F(InductionVarRangeTest, GetDivMax) { |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 324 | ExpectEqual(Value(500), GetDiv(CreateRange(40, 1000), CreateRange(2, 4), false)); |
| 325 | ExpectEqual(Value(-10), GetDiv(CreateRange(40, 1000), CreateRange(-4, -2), false)); |
| 326 | ExpectEqual(Value(-10), GetDiv(CreateRange(-1000, -40), CreateRange(2, 4), false)); |
| 327 | ExpectEqual(Value(500), GetDiv(CreateRange(-1000, -40), CreateRange(-4, -2), false)); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 328 | } |
| 329 | |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 330 | TEST_F(InductionVarRangeTest, GetConstant) { |
| 331 | int32_t value; |
| 332 | ASSERT_TRUE(GetConstant(CreateConst(12345), &value)); |
| 333 | EXPECT_EQ(12345, value); |
| 334 | EXPECT_FALSE(GetConstant(CreateRange(1, 2), &value)); |
| 335 | } |
| 336 | |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 337 | TEST_F(InductionVarRangeTest, AddValue) { |
| 338 | ExpectEqual(Value(110), AddValue(Value(10), Value(100))); |
| 339 | ExpectEqual(Value(-5), AddValue(Value(&x_, 1, -4), Value(&x_, -1, -1))); |
| 340 | ExpectEqual(Value(&x_, 3, -5), AddValue(Value(&x_, 2, -4), Value(&x_, 1, -1))); |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 341 | ExpectEqual(Value(), AddValue(Value(&x_, 1, 5), Value(&y_, 1, -7))); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 342 | ExpectEqual(Value(&x_, 1, 23), AddValue(Value(&x_, 1, 20), Value(3))); |
| 343 | ExpectEqual(Value(&y_, 1, 5), AddValue(Value(55), Value(&y_, 1, -50))); |
Aart Bik | cd26feb | 2015-09-23 17:50:50 -0700 | [diff] [blame] | 344 | const int32_t max_value = std::numeric_limits<int32_t>::max(); |
| 345 | ExpectEqual(Value(max_value), AddValue(Value(max_value - 5), Value(5))); |
| 346 | ExpectEqual(Value(), AddValue(Value(max_value - 5), Value(6))); // unsafe |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | TEST_F(InductionVarRangeTest, SubValue) { |
| 350 | ExpectEqual(Value(-90), SubValue(Value(10), Value(100))); |
| 351 | ExpectEqual(Value(-3), SubValue(Value(&x_, 1, -4), Value(&x_, 1, -1))); |
| 352 | ExpectEqual(Value(&x_, 2, -3), SubValue(Value(&x_, 3, -4), Value(&x_, 1, -1))); |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 353 | ExpectEqual(Value(), SubValue(Value(&x_, 1, 5), Value(&y_, 1, -7))); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 354 | ExpectEqual(Value(&x_, 1, 17), SubValue(Value(&x_, 1, 20), Value(3))); |
| 355 | ExpectEqual(Value(&y_, -4, 105), SubValue(Value(55), Value(&y_, 4, -50))); |
Aart Bik | cd26feb | 2015-09-23 17:50:50 -0700 | [diff] [blame] | 356 | const int32_t min_value = std::numeric_limits<int32_t>::min(); |
| 357 | ExpectEqual(Value(min_value), SubValue(Value(min_value + 5), Value(5))); |
| 358 | ExpectEqual(Value(), SubValue(Value(min_value + 5), Value(6))); // unsafe |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | TEST_F(InductionVarRangeTest, MulValue) { |
| 362 | ExpectEqual(Value(1000), MulValue(Value(10), Value(100))); |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 363 | ExpectEqual(Value(), MulValue(Value(&x_, 1, -4), Value(&x_, 1, -1))); |
| 364 | ExpectEqual(Value(), MulValue(Value(&x_, 1, 5), Value(&y_, 1, -7))); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 365 | ExpectEqual(Value(&x_, 9, 60), MulValue(Value(&x_, 3, 20), Value(3))); |
| 366 | ExpectEqual(Value(&y_, 55, -110), MulValue(Value(55), Value(&y_, 1, -2))); |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 367 | ExpectEqual(Value(), MulValue(Value(90000), Value(-90000))); // unsafe |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | TEST_F(InductionVarRangeTest, DivValue) { |
| 371 | ExpectEqual(Value(25), DivValue(Value(100), Value(4))); |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 372 | ExpectEqual(Value(), DivValue(Value(&x_, 1, -4), Value(&x_, 1, -1))); |
| 373 | ExpectEqual(Value(), DivValue(Value(&x_, 1, 5), Value(&y_, 1, -7))); |
| 374 | ExpectEqual(Value(), DivValue(Value(&x_, 12, 24), Value(3))); |
| 375 | ExpectEqual(Value(), DivValue(Value(55), Value(&y_, 1, -50))); |
| 376 | ExpectEqual(Value(), DivValue(Value(1), Value(0))); // unsafe |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | TEST_F(InductionVarRangeTest, MinValue) { |
| 380 | ExpectEqual(Value(10), MinValue(Value(10), Value(100))); |
| 381 | ExpectEqual(Value(&x_, 1, -4), MinValue(Value(&x_, 1, -4), Value(&x_, 1, -1))); |
| 382 | ExpectEqual(Value(&x_, 4, -4), MinValue(Value(&x_, 4, -4), Value(&x_, 4, -1))); |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 383 | ExpectEqual(Value(), MinValue(Value(&x_, 1, 5), Value(&y_, 1, -7))); |
| 384 | ExpectEqual(Value(), MinValue(Value(&x_, 1, 20), Value(3))); |
| 385 | ExpectEqual(Value(), MinValue(Value(55), Value(&y_, 1, -50))); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | TEST_F(InductionVarRangeTest, MaxValue) { |
| 389 | ExpectEqual(Value(100), MaxValue(Value(10), Value(100))); |
| 390 | ExpectEqual(Value(&x_, 1, -1), MaxValue(Value(&x_, 1, -4), Value(&x_, 1, -1))); |
| 391 | ExpectEqual(Value(&x_, 4, -1), MaxValue(Value(&x_, 4, -4), Value(&x_, 4, -1))); |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 392 | ExpectEqual(Value(), MaxValue(Value(&x_, 1, 5), Value(&y_, 1, -7))); |
| 393 | ExpectEqual(Value(), MaxValue(Value(&x_, 1, 20), Value(3))); |
| 394 | ExpectEqual(Value(), MaxValue(Value(55), Value(&y_, 1, -50))); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 395 | } |
| 396 | |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame^] | 397 | // |
| 398 | // Tests on instance methods. |
| 399 | // |
| 400 | |
| 401 | TEST_F(InductionVarRangeTest, FindRangeConstantTripCount) { |
| 402 | BuildLoop(graph_->GetIntConstant(1000)); |
| 403 | PerformInductionVarAnalysis(); |
| 404 | InductionVarRange range(iva_); |
| 405 | |
| 406 | // In context of header: known. |
| 407 | ExpectEqual(Value(0), range.GetMinInduction(condition_, condition_->InputAt(0))); |
| 408 | ExpectEqual(Value(1000), range.GetMaxInduction(condition_, condition_->InputAt(0))); |
| 409 | |
| 410 | // In context of loop-body: known. |
| 411 | ExpectEqual(Value(0), range.GetMinInduction(increment_, condition_->InputAt(0))); |
| 412 | ExpectEqual(Value(999), range.GetMaxInduction(increment_, condition_->InputAt(0))); |
| 413 | ExpectEqual(Value(1), range.GetMinInduction(increment_, increment_)); |
| 414 | ExpectEqual(Value(1000), range.GetMaxInduction(increment_, increment_)); |
| 415 | } |
| 416 | |
| 417 | TEST_F(InductionVarRangeTest, FindRangeSymbolicTripCount) { |
| 418 | HInstruction* parameter = new (&allocator_) HParameterValue(0, Primitive::kPrimInt); |
| 419 | entry_block_->AddInstruction(parameter); |
| 420 | BuildLoop(parameter); |
| 421 | PerformInductionVarAnalysis(); |
| 422 | InductionVarRange range(iva_); |
| 423 | |
| 424 | // In context of header: full range unknown. |
| 425 | ExpectEqual(Value(0), range.GetMinInduction(condition_, condition_->InputAt(0))); |
| 426 | ExpectEqual(Value(), range.GetMaxInduction(condition_, condition_->InputAt(0))); |
| 427 | |
| 428 | // In context of loop-body: known. |
| 429 | ExpectEqual(Value(0), range.GetMinInduction(increment_, condition_->InputAt(0))); |
| 430 | ExpectEqual(Value(parameter, 1, -1), range.GetMaxInduction(increment_, condition_->InputAt(0))); |
| 431 | ExpectEqual(Value(1), range.GetMinInduction(increment_, increment_)); |
| 432 | ExpectEqual(Value(parameter, 1, 0), range.GetMaxInduction(increment_, increment_)); |
| 433 | } |
| 434 | |
| 435 | TEST_F(InductionVarRangeTest, CodeGeneration) { |
| 436 | HInstruction* parameter = new (&allocator_) HParameterValue(0, Primitive::kPrimInt); |
| 437 | entry_block_->AddInstruction(parameter); |
| 438 | BuildLoop(parameter); |
| 439 | PerformInductionVarAnalysis(); |
| 440 | InductionVarRange range(iva_); |
| 441 | |
| 442 | HInstruction* lower = nullptr; |
| 443 | HInstruction* upper = nullptr; |
| 444 | bool top_test = false; |
| 445 | |
| 446 | // Can generate code in context of loop-body only. |
| 447 | EXPECT_FALSE(range.CanGenerateCode(condition_, condition_->InputAt(0), &top_test)); |
| 448 | ASSERT_TRUE(range.CanGenerateCode(increment_, condition_->InputAt(0), &top_test)); |
| 449 | EXPECT_TRUE(top_test); |
| 450 | |
| 451 | // Generates code. |
| 452 | EXPECT_TRUE(range.GenerateCode( |
| 453 | increment_, condition_->InputAt(0), graph_, loop_preheader_, &lower, &upper)); |
| 454 | |
| 455 | // Verify lower is 0+0. |
| 456 | ASSERT_TRUE(lower != nullptr); |
| 457 | ASSERT_TRUE(lower->IsAdd()); |
| 458 | ASSERT_TRUE(lower->InputAt(0)->IsIntConstant()); |
| 459 | EXPECT_EQ(0, lower->InputAt(0)->AsIntConstant()->GetValue()); |
| 460 | ASSERT_TRUE(lower->InputAt(1)->IsIntConstant()); |
| 461 | EXPECT_EQ(0, lower->InputAt(1)->AsIntConstant()->GetValue()); |
| 462 | |
| 463 | // Verify upper is (V-1)+0 |
| 464 | ASSERT_TRUE(upper != nullptr); |
| 465 | ASSERT_TRUE(upper->IsAdd()); |
| 466 | ASSERT_TRUE(upper->InputAt(0)->IsSub()); |
| 467 | EXPECT_TRUE(upper->InputAt(0)->InputAt(0)->IsParameterValue()); |
| 468 | ASSERT_TRUE(upper->InputAt(0)->InputAt(1)->IsIntConstant()); |
| 469 | EXPECT_EQ(1, upper->InputAt(0)->InputAt(1)->AsIntConstant()->GetValue()); |
| 470 | ASSERT_TRUE(upper->InputAt(1)->IsIntConstant()); |
| 471 | EXPECT_EQ(0, upper->InputAt(1)->AsIntConstant()->GetValue()); |
| 472 | } |
| 473 | |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 474 | } // namespace art |