blob: fda5153d43e513cd7fd6f95170367565ba03e91b [file] [log] [blame]
Aart Bikd14c5952015-09-08 15:25:15 -07001/*
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 Bikd14c5952015-09-08 15:25:15 -070017#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
25namespace art {
26
27using Value = InductionVarRange::Value;
28
29/**
30 * Fixture class for the InductionVarRange tests.
31 */
32class 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 Bikb3365e02015-09-21 14:45:05 -070046 EXPECT_EQ(v1.is_known, v2.is_known);
Aart Bikd14c5952015-09-08 15:25:15 -070047 }
48
Aart Bik389b3db2015-10-28 14:23:40 -070049 //
50 // Construction methods.
51 //
52
Aart Bikd14c5952015-09-08 15:25:15 -070053 /** Constructs bare minimum graph. */
54 void BuildGraph() {
55 graph_->SetNumberOfVRegs(1);
Aart Bikaec3cce2015-10-14 17:44:55 -070056 entry_block_ = new (&allocator_) HBasicBlock(graph_);
57 exit_block_ = new (&allocator_) HBasicBlock(graph_);
58 graph_->AddBlock(entry_block_);
59 graph_->AddBlock(exit_block_);
60 graph_->SetEntryBlock(entry_block_);
61 graph_->SetExitBlock(exit_block_);
62 }
63
64 /** Constructs loop with given upper bound. */
Aart Bik389b3db2015-10-28 14:23:40 -070065 void BuildLoop(int32_t lower, HInstruction* upper, int32_t stride) {
Aart Bikaec3cce2015-10-14 17:44:55 -070066 // Control flow.
67 loop_preheader_ = new (&allocator_) HBasicBlock(graph_);
68 graph_->AddBlock(loop_preheader_);
69 HBasicBlock* loop_header = new (&allocator_) HBasicBlock(graph_);
70 graph_->AddBlock(loop_header);
71 HBasicBlock* loop_body = new (&allocator_) HBasicBlock(graph_);
72 graph_->AddBlock(loop_body);
73 entry_block_->AddSuccessor(loop_preheader_);
74 loop_preheader_->AddSuccessor(loop_header);
75 loop_header->AddSuccessor(loop_body);
76 loop_header->AddSuccessor(exit_block_);
77 loop_body->AddSuccessor(loop_header);
78 // Instructions.
79 HLocal* induc = new (&allocator_) HLocal(0);
80 entry_block_->AddInstruction(induc);
81 loop_preheader_->AddInstruction(
Aart Bik389b3db2015-10-28 14:23:40 -070082 new (&allocator_) HStoreLocal(induc, graph_->GetIntConstant(lower))); // i = l
Aart Bikaec3cce2015-10-14 17:44:55 -070083 loop_preheader_->AddInstruction(new (&allocator_) HGoto());
84 HInstruction* load = new (&allocator_) HLoadLocal(induc, Primitive::kPrimInt);
85 loop_header->AddInstruction(load);
Aart Bik389b3db2015-10-28 14:23:40 -070086 if (stride > 0) {
87 condition_ = new (&allocator_) HLessThan(load, upper); // i < u
88 } else {
89 condition_ = new (&allocator_) HGreaterThan(load, upper); // i > u
90 }
Aart Bikaec3cce2015-10-14 17:44:55 -070091 loop_header->AddInstruction(condition_);
Aart Bik389b3db2015-10-28 14:23:40 -070092 loop_header->AddInstruction(new (&allocator_) HIf(condition_));
Aart Bikaec3cce2015-10-14 17:44:55 -070093 load = new (&allocator_) HLoadLocal(induc, Primitive::kPrimInt);
94 loop_body->AddInstruction(load);
Aart Bik389b3db2015-10-28 14:23:40 -070095 increment_ = new (&allocator_) HAdd(Primitive::kPrimInt, load, graph_->GetIntConstant(stride));
Aart Bikaec3cce2015-10-14 17:44:55 -070096 loop_body->AddInstruction(increment_);
Aart Bik389b3db2015-10-28 14:23:40 -070097 loop_body->AddInstruction(new (&allocator_) HStoreLocal(induc, increment_)); // i += s
Aart Bikaec3cce2015-10-14 17:44:55 -070098 loop_body->AddInstruction(new (&allocator_) HGoto());
99 exit_block_->AddInstruction(new (&allocator_) HReturnVoid());
100 }
101
102 /** Performs induction variable analysis. */
103 void PerformInductionVarAnalysis() {
104 ASSERT_TRUE(graph_->TryBuildingSsa());
105 iva_->Run();
Aart Bikd14c5952015-09-08 15:25:15 -0700106 }
107
108 /** Constructs an invariant. */
109 HInductionVarAnalysis::InductionInfo* CreateInvariant(char opc,
110 HInductionVarAnalysis::InductionInfo* a,
111 HInductionVarAnalysis::InductionInfo* b) {
112 HInductionVarAnalysis::InductionOp op;
113 switch (opc) {
114 case '+': op = HInductionVarAnalysis::kAdd; break;
115 case '-': op = HInductionVarAnalysis::kSub; break;
116 case 'n': op = HInductionVarAnalysis::kNeg; break;
117 case '*': op = HInductionVarAnalysis::kMul; break;
118 case '/': op = HInductionVarAnalysis::kDiv; break;
119 default: op = HInductionVarAnalysis::kNop; break;
120 }
121 return iva_->CreateInvariantOp(op, a, b);
122 }
123
124 /** Constructs a fetch. */
125 HInductionVarAnalysis::InductionInfo* CreateFetch(HInstruction* fetch) {
126 return iva_->CreateInvariantFetch(fetch);
127 }
128
129 /** Constructs a constant. */
130 HInductionVarAnalysis::InductionInfo* CreateConst(int32_t c) {
131 return CreateFetch(graph_->GetIntConstant(c));
132 }
133
134 /** Constructs a trip-count. */
Aart Bik389b3db2015-10-28 14:23:40 -0700135 HInductionVarAnalysis::InductionInfo* CreateTripCount(int32_t tc, bool in_loop, bool safe) {
136 if (in_loop && safe) {
137 return iva_->CreateTripCount(
138 HInductionVarAnalysis::kTripCountInLoop, CreateConst(tc), nullptr);
139 } else if (in_loop) {
140 return iva_->CreateTripCount(
141 HInductionVarAnalysis::kTripCountInLoopUnsafe, CreateConst(tc), nullptr);
142 } else if (safe) {
143 return iva_->CreateTripCount(
144 HInductionVarAnalysis::kTripCountInBody, CreateConst(tc), nullptr);
145 } else {
146 return iva_->CreateTripCount(
147 HInductionVarAnalysis::kTripCountInBodyUnsafe, CreateConst(tc), nullptr);
148 }
Aart Bikd14c5952015-09-08 15:25:15 -0700149 }
150
151 /** Constructs a linear a * i + b induction. */
152 HInductionVarAnalysis::InductionInfo* CreateLinear(int32_t a, int32_t b) {
153 return iva_->CreateInduction(HInductionVarAnalysis::kLinear, CreateConst(a), CreateConst(b));
154 }
155
156 /** Constructs a range [lo, hi] using a periodic induction. */
157 HInductionVarAnalysis::InductionInfo* CreateRange(int32_t lo, int32_t hi) {
158 return iva_->CreateInduction(
159 HInductionVarAnalysis::kPeriodic, CreateConst(lo), CreateConst(hi));
160 }
161
Aart Bik389b3db2015-10-28 14:23:40 -0700162 /** Constructs a wrap-around induction consisting of a constant, followed info */
163 HInductionVarAnalysis::InductionInfo* CreateWrapAround(
164 int32_t initial,
165 HInductionVarAnalysis::InductionInfo* info) {
166 return iva_->CreateInduction(HInductionVarAnalysis::kWrapAround, CreateConst(initial), info);
167 }
168
Aart Bikd14c5952015-09-08 15:25:15 -0700169 /** Constructs a wrap-around induction consisting of a constant, followed by a range. */
170 HInductionVarAnalysis::InductionInfo* CreateWrapAround(int32_t initial, int32_t lo, int32_t hi) {
Aart Bik389b3db2015-10-28 14:23:40 -0700171 return CreateWrapAround(initial, CreateRange(lo, hi));
Aart Bikd14c5952015-09-08 15:25:15 -0700172 }
173
174 //
175 // Relay methods.
176 //
177
Aart Bik389b3db2015-10-28 14:23:40 -0700178 bool NeedsTripCount(HInductionVarAnalysis::InductionInfo* info) {
179 return InductionVarRange::NeedsTripCount(info);
180 }
181
182 bool IsBodyTripCount(HInductionVarAnalysis::InductionInfo* trip) {
183 return InductionVarRange::IsBodyTripCount(trip);
184 }
185
186 bool IsUnsafeTripCount(HInductionVarAnalysis::InductionInfo* trip) {
187 return InductionVarRange::IsUnsafeTripCount(trip);
188 }
189
Aart Bikd14c5952015-09-08 15:25:15 -0700190 Value GetMin(HInductionVarAnalysis::InductionInfo* info,
191 HInductionVarAnalysis::InductionInfo* induc) {
Aart Bik9401f532015-09-28 16:25:56 -0700192 return InductionVarRange::GetVal(info, induc, /* in_body */ true, /* is_min */ true);
Aart Bikd14c5952015-09-08 15:25:15 -0700193 }
194
195 Value GetMax(HInductionVarAnalysis::InductionInfo* info,
196 HInductionVarAnalysis::InductionInfo* induc) {
Aart Bik9401f532015-09-28 16:25:56 -0700197 return InductionVarRange::GetVal(info, induc, /* in_body */ true, /* is_min */ false);
Aart Bikd14c5952015-09-08 15:25:15 -0700198 }
199
200 Value GetMul(HInductionVarAnalysis::InductionInfo* info1,
Aart Bikb3365e02015-09-21 14:45:05 -0700201 HInductionVarAnalysis::InductionInfo* info2,
202 bool is_min) {
Aart Bik9401f532015-09-28 16:25:56 -0700203 return InductionVarRange::GetMul(info1, info2, nullptr, /* in_body */ true, is_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700204 }
205
206 Value GetDiv(HInductionVarAnalysis::InductionInfo* info1,
Aart Bikb3365e02015-09-21 14:45:05 -0700207 HInductionVarAnalysis::InductionInfo* info2,
208 bool is_min) {
Aart Bik9401f532015-09-28 16:25:56 -0700209 return InductionVarRange::GetDiv(info1, info2, nullptr, /* in_body */ true, is_min);
210 }
211
212 bool GetConstant(HInductionVarAnalysis::InductionInfo* info, int32_t* value) {
213 return InductionVarRange::GetConstant(info, value);
Aart Bikd14c5952015-09-08 15:25:15 -0700214 }
215
Aart Bikb3365e02015-09-21 14:45:05 -0700216 Value AddValue(Value v1, Value v2) { return InductionVarRange::AddValue(v1, v2); }
217 Value SubValue(Value v1, Value v2) { return InductionVarRange::SubValue(v1, v2); }
218 Value MulValue(Value v1, Value v2) { return InductionVarRange::MulValue(v1, v2); }
219 Value DivValue(Value v1, Value v2) { return InductionVarRange::DivValue(v1, v2); }
Aart Bikcd26feb2015-09-23 17:50:50 -0700220 Value MinValue(Value v1, Value v2) { return InductionVarRange::MergeVal(v1, v2, true); }
221 Value MaxValue(Value v1, Value v2) { return InductionVarRange::MergeVal(v1, v2, false); }
Aart Bikd14c5952015-09-08 15:25:15 -0700222
223 // General building fields.
224 ArenaPool pool_;
225 ArenaAllocator allocator_;
226 HGraph* graph_;
Aart Bikaec3cce2015-10-14 17:44:55 -0700227 HBasicBlock* entry_block_;
228 HBasicBlock* exit_block_;
229 HBasicBlock* loop_preheader_;
Aart Bikd14c5952015-09-08 15:25:15 -0700230 HInductionVarAnalysis* iva_;
231
Aart Bikaec3cce2015-10-14 17:44:55 -0700232 // Instructions.
233 HInstruction* condition_;
234 HInstruction* increment_;
Aart Bikd14c5952015-09-08 15:25:15 -0700235 HReturnVoid x_;
236 HReturnVoid y_;
237};
238
239//
Aart Bikaec3cce2015-10-14 17:44:55 -0700240// Tests on static methods.
Aart Bikd14c5952015-09-08 15:25:15 -0700241//
242
Aart Bik389b3db2015-10-28 14:23:40 -0700243TEST_F(InductionVarRangeTest, TripCountProperties) {
244 EXPECT_FALSE(NeedsTripCount(nullptr));
245 EXPECT_FALSE(NeedsTripCount(CreateConst(1)));
246 EXPECT_TRUE(NeedsTripCount(CreateLinear(1, 1)));
247 EXPECT_FALSE(NeedsTripCount(CreateWrapAround(1, 2, 3)));
248 EXPECT_TRUE(NeedsTripCount(CreateWrapAround(1, CreateLinear(1, 1))));
249
250 EXPECT_FALSE(IsBodyTripCount(nullptr));
251 EXPECT_FALSE(IsBodyTripCount(CreateTripCount(100, true, true)));
252 EXPECT_FALSE(IsBodyTripCount(CreateTripCount(100, true, false)));
253 EXPECT_TRUE(IsBodyTripCount(CreateTripCount(100, false, true)));
254 EXPECT_TRUE(IsBodyTripCount(CreateTripCount(100, false, false)));
255
256 EXPECT_FALSE(IsUnsafeTripCount(nullptr));
257 EXPECT_FALSE(IsUnsafeTripCount(CreateTripCount(100, true, true)));
258 EXPECT_TRUE(IsUnsafeTripCount(CreateTripCount(100, true, false)));
259 EXPECT_FALSE(IsUnsafeTripCount(CreateTripCount(100, false, true)));
260 EXPECT_TRUE(IsUnsafeTripCount(CreateTripCount(100, false, false)));
261}
262
Aart Bikd14c5952015-09-08 15:25:15 -0700263TEST_F(InductionVarRangeTest, GetMinMaxNull) {
Aart Bikb3365e02015-09-21 14:45:05 -0700264 ExpectEqual(Value(), GetMin(nullptr, nullptr));
265 ExpectEqual(Value(), GetMax(nullptr, nullptr));
Aart Bikd14c5952015-09-08 15:25:15 -0700266}
267
268TEST_F(InductionVarRangeTest, GetMinMaxAdd) {
269 ExpectEqual(Value(12),
270 GetMin(CreateInvariant('+', CreateConst(2), CreateRange(10, 20)), nullptr));
271 ExpectEqual(Value(22),
272 GetMax(CreateInvariant('+', CreateConst(2), CreateRange(10, 20)), nullptr));
273 ExpectEqual(Value(&x_, 1, -20),
274 GetMin(CreateInvariant('+', CreateFetch(&x_), CreateRange(-20, -10)), nullptr));
275 ExpectEqual(Value(&x_, 1, -10),
276 GetMax(CreateInvariant('+', CreateFetch(&x_), CreateRange(-20, -10)), nullptr));
277 ExpectEqual(Value(&x_, 1, 10),
278 GetMin(CreateInvariant('+', CreateRange(10, 20), CreateFetch(&x_)), nullptr));
279 ExpectEqual(Value(&x_, 1, 20),
280 GetMax(CreateInvariant('+', CreateRange(10, 20), CreateFetch(&x_)), nullptr));
281 ExpectEqual(Value(5),
282 GetMin(CreateInvariant('+', CreateRange(-5, -1), CreateRange(10, 20)), nullptr));
283 ExpectEqual(Value(19),
284 GetMax(CreateInvariant('+', CreateRange(-5, -1), CreateRange(10, 20)), nullptr));
285}
286
287TEST_F(InductionVarRangeTest, GetMinMaxSub) {
288 ExpectEqual(Value(-18),
289 GetMin(CreateInvariant('-', CreateConst(2), CreateRange(10, 20)), nullptr));
290 ExpectEqual(Value(-8),
291 GetMax(CreateInvariant('-', CreateConst(2), CreateRange(10, 20)), nullptr));
292 ExpectEqual(Value(&x_, 1, 10),
293 GetMin(CreateInvariant('-', CreateFetch(&x_), CreateRange(-20, -10)), nullptr));
294 ExpectEqual(Value(&x_, 1, 20),
295 GetMax(CreateInvariant('-', CreateFetch(&x_), CreateRange(-20, -10)), nullptr));
296 ExpectEqual(Value(&x_, -1, 10),
297 GetMin(CreateInvariant('-', CreateRange(10, 20), CreateFetch(&x_)), nullptr));
298 ExpectEqual(Value(&x_, -1, 20),
299 GetMax(CreateInvariant('-', CreateRange(10, 20), CreateFetch(&x_)), nullptr));
300 ExpectEqual(Value(-25),
301 GetMin(CreateInvariant('-', CreateRange(-5, -1), CreateRange(10, 20)), nullptr));
302 ExpectEqual(Value(-11),
303 GetMax(CreateInvariant('-', CreateRange(-5, -1), CreateRange(10, 20)), nullptr));
304}
305
306TEST_F(InductionVarRangeTest, GetMinMaxNeg) {
307 ExpectEqual(Value(-20), GetMin(CreateInvariant('n', nullptr, CreateRange(10, 20)), nullptr));
308 ExpectEqual(Value(-10), GetMax(CreateInvariant('n', nullptr, CreateRange(10, 20)), nullptr));
309 ExpectEqual(Value(10), GetMin(CreateInvariant('n', nullptr, CreateRange(-20, -10)), nullptr));
310 ExpectEqual(Value(20), GetMax(CreateInvariant('n', nullptr, CreateRange(-20, -10)), nullptr));
311 ExpectEqual(Value(&x_, -1, 0), GetMin(CreateInvariant('n', nullptr, CreateFetch(&x_)), nullptr));
312 ExpectEqual(Value(&x_, -1, 0), GetMax(CreateInvariant('n', nullptr, CreateFetch(&x_)), nullptr));
313}
314
315TEST_F(InductionVarRangeTest, GetMinMaxMul) {
316 ExpectEqual(Value(20),
317 GetMin(CreateInvariant('*', CreateConst(2), CreateRange(10, 20)), nullptr));
318 ExpectEqual(Value(40),
319 GetMax(CreateInvariant('*', CreateConst(2), CreateRange(10, 20)), nullptr));
320}
321
322TEST_F(InductionVarRangeTest, GetMinMaxDiv) {
323 ExpectEqual(Value(3),
324 GetMin(CreateInvariant('/', CreateRange(12, 20), CreateConst(4)), nullptr));
325 ExpectEqual(Value(5),
326 GetMax(CreateInvariant('/', CreateRange(12, 20), CreateConst(4)), nullptr));
327}
328
329TEST_F(InductionVarRangeTest, GetMinMaxConstant) {
330 ExpectEqual(Value(12345), GetMin(CreateConst(12345), nullptr));
331 ExpectEqual(Value(12345), GetMax(CreateConst(12345), nullptr));
332}
333
334TEST_F(InductionVarRangeTest, GetMinMaxFetch) {
335 ExpectEqual(Value(&x_, 1, 0), GetMin(CreateFetch(&x_), nullptr));
336 ExpectEqual(Value(&x_, 1, 0), GetMax(CreateFetch(&x_), nullptr));
337}
338
339TEST_F(InductionVarRangeTest, GetMinMaxLinear) {
Aart Bik389b3db2015-10-28 14:23:40 -0700340 ExpectEqual(Value(20), GetMin(CreateLinear(10, 20), CreateTripCount(100, true, true)));
341 ExpectEqual(Value(1010), GetMax(CreateLinear(10, 20), CreateTripCount(100, true, true)));
342 ExpectEqual(Value(-970), GetMin(CreateLinear(-10, 20), CreateTripCount(100, true, true)));
343 ExpectEqual(Value(20), GetMax(CreateLinear(-10, 20), CreateTripCount(100, true, true)));
Aart Bikd14c5952015-09-08 15:25:15 -0700344}
345
346TEST_F(InductionVarRangeTest, GetMinMaxWrapAround) {
347 ExpectEqual(Value(-5), GetMin(CreateWrapAround(-5, -1, 10), nullptr));
348 ExpectEqual(Value(10), GetMax(CreateWrapAround(-5, -1, 10), nullptr));
349 ExpectEqual(Value(-1), GetMin(CreateWrapAround(2, -1, 10), nullptr));
350 ExpectEqual(Value(10), GetMax(CreateWrapAround(2, -1, 10), nullptr));
351 ExpectEqual(Value(-1), GetMin(CreateWrapAround(20, -1, 10), nullptr));
352 ExpectEqual(Value(20), GetMax(CreateWrapAround(20, -1, 10), nullptr));
353}
354
355TEST_F(InductionVarRangeTest, GetMinMaxPeriodic) {
356 ExpectEqual(Value(-2), GetMin(CreateRange(-2, 99), nullptr));
357 ExpectEqual(Value(99), GetMax(CreateRange(-2, 99), nullptr));
358}
359
360TEST_F(InductionVarRangeTest, GetMulMin) {
Aart Bikb3365e02015-09-21 14:45:05 -0700361 ExpectEqual(Value(6), GetMul(CreateRange(2, 10), CreateRange(3, 5), true));
362 ExpectEqual(Value(-50), GetMul(CreateRange(2, 10), CreateRange(-5, -3), true));
363 ExpectEqual(Value(-50), GetMul(CreateRange(-10, -2), CreateRange(3, 5), true));
364 ExpectEqual(Value(6), GetMul(CreateRange(-10, -2), CreateRange(-5, -3), true));
Aart Bikd14c5952015-09-08 15:25:15 -0700365}
366
367TEST_F(InductionVarRangeTest, GetMulMax) {
Aart Bikb3365e02015-09-21 14:45:05 -0700368 ExpectEqual(Value(50), GetMul(CreateRange(2, 10), CreateRange(3, 5), false));
369 ExpectEqual(Value(-6), GetMul(CreateRange(2, 10), CreateRange(-5, -3), false));
370 ExpectEqual(Value(-6), GetMul(CreateRange(-10, -2), CreateRange(3, 5), false));
371 ExpectEqual(Value(50), GetMul(CreateRange(-10, -2), CreateRange(-5, -3), false));
Aart Bikd14c5952015-09-08 15:25:15 -0700372}
373
374TEST_F(InductionVarRangeTest, GetDivMin) {
Aart Bikb3365e02015-09-21 14:45:05 -0700375 ExpectEqual(Value(10), GetDiv(CreateRange(40, 1000), CreateRange(2, 4), true));
376 ExpectEqual(Value(-500), GetDiv(CreateRange(40, 1000), CreateRange(-4, -2), true));
377 ExpectEqual(Value(-500), GetDiv(CreateRange(-1000, -40), CreateRange(2, 4), true));
378 ExpectEqual(Value(10), GetDiv(CreateRange(-1000, -40), CreateRange(-4, -2), true));
Aart Bikd14c5952015-09-08 15:25:15 -0700379}
380
381TEST_F(InductionVarRangeTest, GetDivMax) {
Aart Bikb3365e02015-09-21 14:45:05 -0700382 ExpectEqual(Value(500), GetDiv(CreateRange(40, 1000), CreateRange(2, 4), false));
383 ExpectEqual(Value(-10), GetDiv(CreateRange(40, 1000), CreateRange(-4, -2), false));
384 ExpectEqual(Value(-10), GetDiv(CreateRange(-1000, -40), CreateRange(2, 4), false));
385 ExpectEqual(Value(500), GetDiv(CreateRange(-1000, -40), CreateRange(-4, -2), false));
Aart Bikd14c5952015-09-08 15:25:15 -0700386}
387
Aart Bik9401f532015-09-28 16:25:56 -0700388TEST_F(InductionVarRangeTest, GetConstant) {
389 int32_t value;
390 ASSERT_TRUE(GetConstant(CreateConst(12345), &value));
391 EXPECT_EQ(12345, value);
392 EXPECT_FALSE(GetConstant(CreateRange(1, 2), &value));
393}
394
Aart Bikd14c5952015-09-08 15:25:15 -0700395TEST_F(InductionVarRangeTest, AddValue) {
396 ExpectEqual(Value(110), AddValue(Value(10), Value(100)));
397 ExpectEqual(Value(-5), AddValue(Value(&x_, 1, -4), Value(&x_, -1, -1)));
398 ExpectEqual(Value(&x_, 3, -5), AddValue(Value(&x_, 2, -4), Value(&x_, 1, -1)));
Aart Bikb3365e02015-09-21 14:45:05 -0700399 ExpectEqual(Value(), AddValue(Value(&x_, 1, 5), Value(&y_, 1, -7)));
Aart Bikd14c5952015-09-08 15:25:15 -0700400 ExpectEqual(Value(&x_, 1, 23), AddValue(Value(&x_, 1, 20), Value(3)));
401 ExpectEqual(Value(&y_, 1, 5), AddValue(Value(55), Value(&y_, 1, -50)));
Aart Bikcd26feb2015-09-23 17:50:50 -0700402 const int32_t max_value = std::numeric_limits<int32_t>::max();
403 ExpectEqual(Value(max_value), AddValue(Value(max_value - 5), Value(5)));
404 ExpectEqual(Value(), AddValue(Value(max_value - 5), Value(6))); // unsafe
Aart Bikd14c5952015-09-08 15:25:15 -0700405}
406
407TEST_F(InductionVarRangeTest, SubValue) {
408 ExpectEqual(Value(-90), SubValue(Value(10), Value(100)));
409 ExpectEqual(Value(-3), SubValue(Value(&x_, 1, -4), Value(&x_, 1, -1)));
410 ExpectEqual(Value(&x_, 2, -3), SubValue(Value(&x_, 3, -4), Value(&x_, 1, -1)));
Aart Bikb3365e02015-09-21 14:45:05 -0700411 ExpectEqual(Value(), SubValue(Value(&x_, 1, 5), Value(&y_, 1, -7)));
Aart Bikd14c5952015-09-08 15:25:15 -0700412 ExpectEqual(Value(&x_, 1, 17), SubValue(Value(&x_, 1, 20), Value(3)));
413 ExpectEqual(Value(&y_, -4, 105), SubValue(Value(55), Value(&y_, 4, -50)));
Aart Bikcd26feb2015-09-23 17:50:50 -0700414 const int32_t min_value = std::numeric_limits<int32_t>::min();
415 ExpectEqual(Value(min_value), SubValue(Value(min_value + 5), Value(5)));
416 ExpectEqual(Value(), SubValue(Value(min_value + 5), Value(6))); // unsafe
Aart Bikd14c5952015-09-08 15:25:15 -0700417}
418
419TEST_F(InductionVarRangeTest, MulValue) {
420 ExpectEqual(Value(1000), MulValue(Value(10), Value(100)));
Aart Bikb3365e02015-09-21 14:45:05 -0700421 ExpectEqual(Value(), MulValue(Value(&x_, 1, -4), Value(&x_, 1, -1)));
422 ExpectEqual(Value(), MulValue(Value(&x_, 1, 5), Value(&y_, 1, -7)));
Aart Bikd14c5952015-09-08 15:25:15 -0700423 ExpectEqual(Value(&x_, 9, 60), MulValue(Value(&x_, 3, 20), Value(3)));
424 ExpectEqual(Value(&y_, 55, -110), MulValue(Value(55), Value(&y_, 1, -2)));
Aart Bikb3365e02015-09-21 14:45:05 -0700425 ExpectEqual(Value(), MulValue(Value(90000), Value(-90000))); // unsafe
Aart Bikd14c5952015-09-08 15:25:15 -0700426}
427
428TEST_F(InductionVarRangeTest, DivValue) {
429 ExpectEqual(Value(25), DivValue(Value(100), Value(4)));
Aart Bikb3365e02015-09-21 14:45:05 -0700430 ExpectEqual(Value(), DivValue(Value(&x_, 1, -4), Value(&x_, 1, -1)));
431 ExpectEqual(Value(), DivValue(Value(&x_, 1, 5), Value(&y_, 1, -7)));
432 ExpectEqual(Value(), DivValue(Value(&x_, 12, 24), Value(3)));
433 ExpectEqual(Value(), DivValue(Value(55), Value(&y_, 1, -50)));
434 ExpectEqual(Value(), DivValue(Value(1), Value(0))); // unsafe
Aart Bikd14c5952015-09-08 15:25:15 -0700435}
436
437TEST_F(InductionVarRangeTest, MinValue) {
438 ExpectEqual(Value(10), MinValue(Value(10), Value(100)));
439 ExpectEqual(Value(&x_, 1, -4), MinValue(Value(&x_, 1, -4), Value(&x_, 1, -1)));
440 ExpectEqual(Value(&x_, 4, -4), MinValue(Value(&x_, 4, -4), Value(&x_, 4, -1)));
Aart Bikb3365e02015-09-21 14:45:05 -0700441 ExpectEqual(Value(), MinValue(Value(&x_, 1, 5), Value(&y_, 1, -7)));
442 ExpectEqual(Value(), MinValue(Value(&x_, 1, 20), Value(3)));
443 ExpectEqual(Value(), MinValue(Value(55), Value(&y_, 1, -50)));
Aart Bikd14c5952015-09-08 15:25:15 -0700444}
445
446TEST_F(InductionVarRangeTest, MaxValue) {
447 ExpectEqual(Value(100), MaxValue(Value(10), Value(100)));
448 ExpectEqual(Value(&x_, 1, -1), MaxValue(Value(&x_, 1, -4), Value(&x_, 1, -1)));
449 ExpectEqual(Value(&x_, 4, -1), MaxValue(Value(&x_, 4, -4), Value(&x_, 4, -1)));
Aart Bikb3365e02015-09-21 14:45:05 -0700450 ExpectEqual(Value(), MaxValue(Value(&x_, 1, 5), Value(&y_, 1, -7)));
451 ExpectEqual(Value(), MaxValue(Value(&x_, 1, 20), Value(3)));
452 ExpectEqual(Value(), MaxValue(Value(55), Value(&y_, 1, -50)));
Aart Bikd14c5952015-09-08 15:25:15 -0700453}
454
Aart Bikaec3cce2015-10-14 17:44:55 -0700455//
456// Tests on instance methods.
457//
458
Aart Bik389b3db2015-10-28 14:23:40 -0700459TEST_F(InductionVarRangeTest, ConstantTripCountUp) {
460 BuildLoop(0, graph_->GetIntConstant(1000), 1);
Aart Bikaec3cce2015-10-14 17:44:55 -0700461 PerformInductionVarAnalysis();
462 InductionVarRange range(iva_);
463
Aart Bik389b3db2015-10-28 14:23:40 -0700464 Value v1, v2;
465 bool needs_finite_test = true;
466
Aart Bikaec3cce2015-10-14 17:44:55 -0700467 // In context of header: known.
Aart Bik389b3db2015-10-28 14:23:40 -0700468 range.GetInductionRange(condition_, condition_->InputAt(0), &v1, &v2, &needs_finite_test);
469 EXPECT_FALSE(needs_finite_test);
470 ExpectEqual(Value(0), v1);
471 ExpectEqual(Value(1000), v2);
Aart Bikaec3cce2015-10-14 17:44:55 -0700472
473 // In context of loop-body: known.
Aart Bik389b3db2015-10-28 14:23:40 -0700474 range.GetInductionRange(increment_, condition_->InputAt(0), &v1, &v2, &needs_finite_test);
475 EXPECT_FALSE(needs_finite_test);
476 ExpectEqual(Value(0), v1);
477 ExpectEqual(Value(999), v2);
478 range.GetInductionRange(increment_, increment_, &v1, &v2, &needs_finite_test);
479 EXPECT_FALSE(needs_finite_test);
480 ExpectEqual(Value(1), v1);
481 ExpectEqual(Value(1000), v2);
Aart Bikaec3cce2015-10-14 17:44:55 -0700482}
483
Aart Bik389b3db2015-10-28 14:23:40 -0700484TEST_F(InductionVarRangeTest, ConstantTripCountDown) {
485 BuildLoop(1000, graph_->GetIntConstant(0), -1);
Aart Bikaec3cce2015-10-14 17:44:55 -0700486 PerformInductionVarAnalysis();
487 InductionVarRange range(iva_);
488
Aart Bik389b3db2015-10-28 14:23:40 -0700489 Value v1, v2;
490 bool needs_finite_test = true;
491
492 // In context of header: known.
493 range.GetInductionRange(condition_, condition_->InputAt(0), &v1, &v2, &needs_finite_test);
494 EXPECT_FALSE(needs_finite_test);
495 ExpectEqual(Value(0), v1);
496 ExpectEqual(Value(1000), v2);
Aart Bikaec3cce2015-10-14 17:44:55 -0700497
498 // In context of loop-body: known.
Aart Bik389b3db2015-10-28 14:23:40 -0700499 range.GetInductionRange(increment_, condition_->InputAt(0), &v1, &v2, &needs_finite_test);
500 EXPECT_FALSE(needs_finite_test);
501 ExpectEqual(Value(1), v1);
502 ExpectEqual(Value(1000), v2);
503 range.GetInductionRange(increment_, increment_, &v1, &v2, &needs_finite_test);
504 EXPECT_FALSE(needs_finite_test);
505 ExpectEqual(Value(0), v1);
506 ExpectEqual(Value(999), v2);
Aart Bikaec3cce2015-10-14 17:44:55 -0700507}
508
Aart Bik389b3db2015-10-28 14:23:40 -0700509TEST_F(InductionVarRangeTest, SymbolicTripCountUp) {
Calin Juravle27cfad02015-10-20 11:29:36 +0100510 HInstruction* parameter = new (&allocator_) HParameterValue(
511 graph_->GetDexFile(), 0, 0, Primitive::kPrimInt);
Aart Bikaec3cce2015-10-14 17:44:55 -0700512 entry_block_->AddInstruction(parameter);
Aart Bik389b3db2015-10-28 14:23:40 -0700513 BuildLoop(0, parameter, 1);
Aart Bikaec3cce2015-10-14 17:44:55 -0700514 PerformInductionVarAnalysis();
515 InductionVarRange range(iva_);
516
Aart Bik389b3db2015-10-28 14:23:40 -0700517 Value v1, v2;
518 bool needs_finite_test = true;
519 bool needs_taken_test = true;
520
521 // In context of header: upper unknown.
522 range.GetInductionRange(condition_, condition_->InputAt(0), &v1, &v2, &needs_finite_test);
523 EXPECT_FALSE(needs_finite_test);
524 ExpectEqual(Value(0), v1);
525 ExpectEqual(Value(), v2);
526
527 // In context of loop-body: known.
528 range.GetInductionRange(increment_, condition_->InputAt(0), &v1, &v2, &needs_finite_test);
529 EXPECT_FALSE(needs_finite_test);
530 ExpectEqual(Value(0), v1);
531 ExpectEqual(Value(parameter, 1, -1), v2);
532 range.GetInductionRange(increment_, increment_, &v1, &v2, &needs_finite_test);
533 EXPECT_FALSE(needs_finite_test);
534 ExpectEqual(Value(1), v1);
535 ExpectEqual(Value(parameter, 1, 0), v2);
536
Aart Bikaec3cce2015-10-14 17:44:55 -0700537 HInstruction* lower = nullptr;
538 HInstruction* upper = nullptr;
Aart Bik389b3db2015-10-28 14:23:40 -0700539 HInstruction* taken = nullptr;
Aart Bikaec3cce2015-10-14 17:44:55 -0700540
541 // Can generate code in context of loop-body only.
Aart Bik389b3db2015-10-28 14:23:40 -0700542 EXPECT_FALSE(range.CanGenerateCode(
543 condition_, condition_->InputAt(0), &needs_finite_test, &needs_taken_test));
544 ASSERT_TRUE(range.CanGenerateCode(
545 increment_, condition_->InputAt(0), &needs_finite_test, &needs_taken_test));
546 EXPECT_FALSE(needs_finite_test);
547 EXPECT_TRUE(needs_taken_test);
Aart Bikaec3cce2015-10-14 17:44:55 -0700548
549 // Generates code.
Aart Bik389b3db2015-10-28 14:23:40 -0700550 range.GenerateRangeCode(increment_, condition_->InputAt(0), graph_, loop_preheader_, &lower, &upper);
Aart Bikaec3cce2015-10-14 17:44:55 -0700551
552 // Verify lower is 0+0.
553 ASSERT_TRUE(lower != nullptr);
554 ASSERT_TRUE(lower->IsAdd());
555 ASSERT_TRUE(lower->InputAt(0)->IsIntConstant());
556 EXPECT_EQ(0, lower->InputAt(0)->AsIntConstant()->GetValue());
557 ASSERT_TRUE(lower->InputAt(1)->IsIntConstant());
558 EXPECT_EQ(0, lower->InputAt(1)->AsIntConstant()->GetValue());
559
Aart Bik389b3db2015-10-28 14:23:40 -0700560 // Verify upper is (V-1)+0.
Aart Bikaec3cce2015-10-14 17:44:55 -0700561 ASSERT_TRUE(upper != nullptr);
562 ASSERT_TRUE(upper->IsAdd());
563 ASSERT_TRUE(upper->InputAt(0)->IsSub());
564 EXPECT_TRUE(upper->InputAt(0)->InputAt(0)->IsParameterValue());
565 ASSERT_TRUE(upper->InputAt(0)->InputAt(1)->IsIntConstant());
566 EXPECT_EQ(1, upper->InputAt(0)->InputAt(1)->AsIntConstant()->GetValue());
567 ASSERT_TRUE(upper->InputAt(1)->IsIntConstant());
568 EXPECT_EQ(0, upper->InputAt(1)->AsIntConstant()->GetValue());
Aart Bik389b3db2015-10-28 14:23:40 -0700569
570 // Verify taken-test is 0<V.
571 range.GenerateTakenTest(increment_, graph_, loop_preheader_, &taken);
572 ASSERT_TRUE(taken != nullptr);
573 ASSERT_TRUE(taken->IsLessThan());
574 ASSERT_TRUE(taken->InputAt(0)->IsIntConstant());
575 EXPECT_EQ(0, taken->InputAt(0)->AsIntConstant()->GetValue());
576 EXPECT_TRUE(taken->InputAt(1)->IsParameterValue());
577}
578
579TEST_F(InductionVarRangeTest, SymbolicTripCountDown) {
580 HInstruction* parameter = new (&allocator_) HParameterValue(
581 graph_->GetDexFile(), 0, 0, Primitive::kPrimInt);
582 entry_block_->AddInstruction(parameter);
583 BuildLoop(1000, parameter, -1);
584 PerformInductionVarAnalysis();
585 InductionVarRange range(iva_);
586
587 Value v1, v2;
588 bool needs_finite_test = true;
589 bool needs_taken_test = true;
590
591 // In context of header: lower unknown.
592 range.GetInductionRange(condition_, condition_->InputAt(0), &v1, &v2, &needs_finite_test);
593 EXPECT_FALSE(needs_finite_test);
594 ExpectEqual(Value(), v1);
595 ExpectEqual(Value(1000), v2);
596
597 // In context of loop-body: known.
598 range.GetInductionRange(increment_, condition_->InputAt(0), &v1, &v2, &needs_finite_test);
599 EXPECT_FALSE(needs_finite_test);
600 ExpectEqual(Value(parameter, 1, 1), v1);
601 ExpectEqual(Value(1000), v2);
602 range.GetInductionRange(increment_, increment_, &v1, &v2, &needs_finite_test);
603 EXPECT_FALSE(needs_finite_test);
604 ExpectEqual(Value(parameter, 1, 0), v1);
605 ExpectEqual(Value(999), v2);
606
607 HInstruction* lower = nullptr;
608 HInstruction* upper = nullptr;
609 HInstruction* taken = nullptr;
610
611 // Can generate code in context of loop-body only.
612 EXPECT_FALSE(range.CanGenerateCode(
613 condition_, condition_->InputAt(0), &needs_finite_test, &needs_taken_test));
614 ASSERT_TRUE(range.CanGenerateCode(
615 increment_, condition_->InputAt(0), &needs_finite_test, &needs_taken_test));
616 EXPECT_FALSE(needs_finite_test);
617 EXPECT_TRUE(needs_taken_test);
618
619 // Generates code.
620 range.GenerateRangeCode(increment_, condition_->InputAt(0), graph_, loop_preheader_, &lower, &upper);
621
622 // Verify lower is 1000-(-(V-1000)-1).
623 ASSERT_TRUE(lower != nullptr);
624 ASSERT_TRUE(lower->IsSub());
625 ASSERT_TRUE(lower->InputAt(0)->IsIntConstant());
626 EXPECT_EQ(1000, lower->InputAt(0)->AsIntConstant()->GetValue());
627 lower = lower->InputAt(1);
628 ASSERT_TRUE(lower->IsSub());
629 ASSERT_TRUE(lower->InputAt(1)->IsIntConstant());
630 EXPECT_EQ(1, lower->InputAt(1)->AsIntConstant()->GetValue());
631 lower = lower->InputAt(0);
632 ASSERT_TRUE(lower->IsNeg());
633 lower = lower->InputAt(0);
634 ASSERT_TRUE(lower->IsSub());
635 EXPECT_TRUE(lower->InputAt(0)->IsParameterValue());
636 ASSERT_TRUE(lower->InputAt(1)->IsIntConstant());
637 EXPECT_EQ(1000, lower->InputAt(1)->AsIntConstant()->GetValue());
638
639 // Verify upper is 1000-0.
640 ASSERT_TRUE(upper != nullptr);
641 ASSERT_TRUE(upper->IsSub());
642 ASSERT_TRUE(upper->InputAt(0)->IsIntConstant());
643 EXPECT_EQ(1000, upper->InputAt(0)->AsIntConstant()->GetValue());
644 ASSERT_TRUE(upper->InputAt(1)->IsIntConstant());
645 EXPECT_EQ(0, upper->InputAt(1)->AsIntConstant()->GetValue());
646
647 // Verify taken-test is 1000>V.
648 range.GenerateTakenTest(increment_, graph_, loop_preheader_, &taken);
649 ASSERT_TRUE(taken != nullptr);
650 ASSERT_TRUE(taken->IsGreaterThan());
651 ASSERT_TRUE(taken->InputAt(0)->IsIntConstant());
652 EXPECT_EQ(1000, taken->InputAt(0)->AsIntConstant()->GetValue());
653 EXPECT_TRUE(taken->InputAt(1)->IsParameterValue());
Aart Bikaec3cce2015-10-14 17:44:55 -0700654}
655
Aart Bikd14c5952015-09-08 15:25:15 -0700656} // namespace art