blob: 510841eb689401e5fe0dcc00e56922e9209ff664 [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"
Aart Bikd14c5952015-09-08 15:25:15 -070019#include "induction_var_analysis.h"
20#include "induction_var_range.h"
21#include "nodes.h"
22#include "optimizing_unit_test.h"
23
24namespace art {
25
26using Value = InductionVarRange::Value;
27
28/**
29 * Fixture class for the InductionVarRange tests.
30 */
David Brazdil4833f5a2015-12-16 10:37:39 +000031class InductionVarRangeTest : public CommonCompilerTest {
Aart Bikd14c5952015-09-08 15:25:15 -070032 public:
Aart Bik7d57d7f2015-12-09 14:39:48 -080033 InductionVarRangeTest()
34 : pool_(),
35 allocator_(&pool_),
36 graph_(CreateGraph(&allocator_)),
37 iva_(new (&allocator_) HInductionVarAnalysis(graph_)),
38 range_(iva_) {
Aart Bikd14c5952015-09-08 15:25:15 -070039 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 Bikb3365e02015-09-21 14:45:05 -070048 EXPECT_EQ(v1.is_known, v2.is_known);
Aart Bikd14c5952015-09-08 15:25:15 -070049 }
50
Aart Bik389b3db2015-10-28 14:23:40 -070051 //
52 // Construction methods.
53 //
54
Aart Bikd14c5952015-09-08 15:25:15 -070055 /** Constructs bare minimum graph. */
56 void BuildGraph() {
57 graph_->SetNumberOfVRegs(1);
Aart Bikaec3cce2015-10-14 17:44:55 -070058 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 Bik7d57d7f2015-12-09 14:39:48 -080064 // Two parameters.
Andreas Gampea5b09a62016-11-17 15:21:22 -080065 x_ = new (&allocator_) HParameterValue(graph_->GetDexFile(),
66 dex::TypeIndex(0),
67 0,
68 Primitive::kPrimInt);
Aart Bik7d57d7f2015-12-09 14:39:48 -080069 entry_block_->AddInstruction(x_);
Andreas Gampea5b09a62016-11-17 15:21:22 -080070 y_ = new (&allocator_) HParameterValue(graph_->GetDexFile(),
71 dex::TypeIndex(0),
72 0,
73 Primitive::kPrimInt);
Aart Bik7d57d7f2015-12-09 14:39:48 -080074 entry_block_->AddInstruction(y_);
Aart Bik52be7e72016-06-23 11:20:41 -070075 // Set arbitrary range analysis hint while testing private methods.
76 SetHint(x_);
Aart Bikaec3cce2015-10-14 17:44:55 -070077 }
78
79 /** Constructs loop with given upper bound. */
Aart Bik389b3db2015-10-28 14:23:40 -070080 void BuildLoop(int32_t lower, HInstruction* upper, int32_t stride) {
Aart Bikaec3cce2015-10-14 17:44:55 -070081 // Control flow.
82 loop_preheader_ = new (&allocator_) HBasicBlock(graph_);
83 graph_->AddBlock(loop_preheader_);
Aart Bik16d3a652016-09-09 10:33:50 -070084 loop_header_ = new (&allocator_) HBasicBlock(graph_);
85 graph_->AddBlock(loop_header_);
86 loop_body_ = new (&allocator_) HBasicBlock(graph_);
87 graph_->AddBlock(loop_body_);
David Brazdildb51efb2015-11-06 01:36:20 +000088 HBasicBlock* return_block = new (&allocator_) HBasicBlock(graph_);
89 graph_->AddBlock(return_block);
Aart Bikaec3cce2015-10-14 17:44:55 -070090 entry_block_->AddSuccessor(loop_preheader_);
Aart Bik16d3a652016-09-09 10:33:50 -070091 loop_preheader_->AddSuccessor(loop_header_);
92 loop_header_->AddSuccessor(loop_body_);
93 loop_header_->AddSuccessor(return_block);
94 loop_body_->AddSuccessor(loop_header_);
David Brazdildb51efb2015-11-06 01:36:20 +000095 return_block->AddSuccessor(exit_block_);
Aart Bikaec3cce2015-10-14 17:44:55 -070096 // Instructions.
Aart Bikaec3cce2015-10-14 17:44:55 -070097 loop_preheader_->AddInstruction(new (&allocator_) HGoto());
David Brazdilbadd8262016-02-02 16:28:56 +000098 HPhi* phi = new (&allocator_) HPhi(&allocator_, 0, 0, Primitive::kPrimInt);
Aart Bik16d3a652016-09-09 10:33:50 -070099 loop_header_->AddPhi(phi);
David Brazdilbadd8262016-02-02 16:28:56 +0000100 phi->AddInput(graph_->GetIntConstant(lower)); // i = l
Aart Bik389b3db2015-10-28 14:23:40 -0700101 if (stride > 0) {
David Brazdilbadd8262016-02-02 16:28:56 +0000102 condition_ = new (&allocator_) HLessThan(phi, upper); // i < u
Aart Bik389b3db2015-10-28 14:23:40 -0700103 } else {
David Brazdilbadd8262016-02-02 16:28:56 +0000104 condition_ = new (&allocator_) HGreaterThan(phi, upper); // i > u
Aart Bik389b3db2015-10-28 14:23:40 -0700105 }
Aart Bik16d3a652016-09-09 10:33:50 -0700106 loop_header_->AddInstruction(condition_);
107 loop_header_->AddInstruction(new (&allocator_) HIf(condition_));
David Brazdilbadd8262016-02-02 16:28:56 +0000108 increment_ = new (&allocator_) HAdd(Primitive::kPrimInt, phi, graph_->GetIntConstant(stride));
Aart Bik16d3a652016-09-09 10:33:50 -0700109 loop_body_->AddInstruction(increment_); // i += s
David Brazdilbadd8262016-02-02 16:28:56 +0000110 phi->AddInput(increment_);
Aart Bik16d3a652016-09-09 10:33:50 -0700111 loop_body_->AddInstruction(new (&allocator_) HGoto());
David Brazdildb51efb2015-11-06 01:36:20 +0000112 return_block->AddInstruction(new (&allocator_) HReturnVoid());
113 exit_block_->AddInstruction(new (&allocator_) HExit());
Aart Bikaec3cce2015-10-14 17:44:55 -0700114 }
115
Aart Bik7d57d7f2015-12-09 14:39:48 -0800116 /** Constructs SSA and performs induction variable analysis. */
Aart Bikaec3cce2015-10-14 17:44:55 -0700117 void PerformInductionVarAnalysis() {
David Brazdilbadd8262016-02-02 16:28:56 +0000118 graph_->BuildDominatorTree();
Aart Bikaec3cce2015-10-14 17:44:55 -0700119 iva_->Run();
Aart Bikd14c5952015-09-08 15:25:15 -0700120 }
121
Aart Bik52be7e72016-06-23 11:20:41 -0700122 /** Sets hint. */
123 void SetHint(HInstruction* hint) {
124 range_.chase_hint_ = hint;
125 }
126
Aart Bikd14c5952015-09-08 15:25:15 -0700127 /** 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 Bik52be7e72016-06-23 11:20:41 -0700138 case '<': op = HInductionVarAnalysis::kLT; break;
Aart Bikd14c5952015-09-08 15:25:15 -0700139 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 Bik52be7e72016-06-23 11:20:41 -0700154 /** Constructs a constant trip-count. */
Aart Bik389b3db2015-10-28 14:23:40 -0700155 HInductionVarAnalysis::InductionInfo* CreateTripCount(int32_t tc, bool in_loop, bool safe) {
Aart Bik52be7e72016-06-23 11:20:41 -0700156 HInductionVarAnalysis::InductionOp op = HInductionVarAnalysis::kTripCountInBodyUnsafe;
Aart Bik389b3db2015-10-28 14:23:40 -0700157 if (in_loop && safe) {
Aart Bik52be7e72016-06-23 11:20:41 -0700158 op = HInductionVarAnalysis::kTripCountInLoop;
Aart Bik389b3db2015-10-28 14:23:40 -0700159 } else if (in_loop) {
Aart Bik52be7e72016-06-23 11:20:41 -0700160 op = HInductionVarAnalysis::kTripCountInLoopUnsafe;
Aart Bik389b3db2015-10-28 14:23:40 -0700161 } else if (safe) {
Aart Bik52be7e72016-06-23 11:20:41 -0700162 op = HInductionVarAnalysis::kTripCountInBody;
Aart Bik389b3db2015-10-28 14:23:40 -0700163 }
Aart Bik52be7e72016-06-23 11:20:41 -0700164 // 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 Bikd14c5952015-09-08 15:25:15 -0700169 }
170
171 /** Constructs a linear a * i + b induction. */
172 HInductionVarAnalysis::InductionInfo* CreateLinear(int32_t a, int32_t b) {
Aart Bikc071a012016-12-01 10:22:31 -0800173 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 Bikd14c5952015-09-08 15:25:15 -0700191 }
192
193 /** Constructs a range [lo, hi] using a periodic induction. */
194 HInductionVarAnalysis::InductionInfo* CreateRange(int32_t lo, int32_t hi) {
Aart Bikc071a012016-12-01 10:22:31 -0800195 return iva_->CreateInduction(HInductionVarAnalysis::kPeriodic,
196 HInductionVarAnalysis::kNop,
197 CreateConst(lo),
198 CreateConst(hi),
199 nullptr,
200 Primitive::kPrimInt);
Aart Bikd14c5952015-09-08 15:25:15 -0700201 }
202
Aart Bik389b3db2015-10-28 14:23:40 -0700203 /** Constructs a wrap-around induction consisting of a constant, followed info */
204 HInductionVarAnalysis::InductionInfo* CreateWrapAround(
205 int32_t initial,
206 HInductionVarAnalysis::InductionInfo* info) {
Aart Bikc071a012016-12-01 10:22:31 -0800207 return iva_->CreateInduction(HInductionVarAnalysis::kWrapAround,
208 HInductionVarAnalysis::kNop,
209 CreateConst(initial),
210 info,
211 nullptr,
212 Primitive::kPrimInt);
Aart Bik389b3db2015-10-28 14:23:40 -0700213 }
214
Aart Bikd14c5952015-09-08 15:25:15 -0700215 /** 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 Bik389b3db2015-10-28 14:23:40 -0700217 return CreateWrapAround(initial, CreateRange(lo, hi));
Aart Bikd14c5952015-09-08 15:25:15 -0700218 }
219
220 //
221 // Relay methods.
222 //
223
Aart Bik389b3db2015-10-28 14:23:40 -0700224 bool NeedsTripCount(HInductionVarAnalysis::InductionInfo* info) {
Aart Bik16d3a652016-09-09 10:33:50 -0700225 int64_t s = 0;
226 return range_.NeedsTripCount(info, &s);
Aart Bik389b3db2015-10-28 14:23:40 -0700227 }
228
229 bool IsBodyTripCount(HInductionVarAnalysis::InductionInfo* trip) {
Aart Bik7d57d7f2015-12-09 14:39:48 -0800230 return range_.IsBodyTripCount(trip);
Aart Bik389b3db2015-10-28 14:23:40 -0700231 }
232
233 bool IsUnsafeTripCount(HInductionVarAnalysis::InductionInfo* trip) {
Aart Bik7d57d7f2015-12-09 14:39:48 -0800234 return range_.IsUnsafeTripCount(trip);
Aart Bik389b3db2015-10-28 14:23:40 -0700235 }
236
Aart Bikd14c5952015-09-08 15:25:15 -0700237 Value GetMin(HInductionVarAnalysis::InductionInfo* info,
Aart Bik52be7e72016-06-23 11:20:41 -0700238 HInductionVarAnalysis::InductionInfo* trip) {
239 return range_.GetVal(info, trip, /* in_body */ true, /* is_min */ true);
Aart Bikd14c5952015-09-08 15:25:15 -0700240 }
241
242 Value GetMax(HInductionVarAnalysis::InductionInfo* info,
Aart Bik52be7e72016-06-23 11:20:41 -0700243 HInductionVarAnalysis::InductionInfo* trip) {
244 return range_.GetVal(info, trip, /* in_body */ true, /* is_min */ false);
Aart Bikd14c5952015-09-08 15:25:15 -0700245 }
246
247 Value GetMul(HInductionVarAnalysis::InductionInfo* info1,
Aart Bikb3365e02015-09-21 14:45:05 -0700248 HInductionVarAnalysis::InductionInfo* info2,
249 bool is_min) {
Aart Bik7d57d7f2015-12-09 14:39:48 -0800250 return range_.GetMul(info1, info2, nullptr, /* in_body */ true, is_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700251 }
252
253 Value GetDiv(HInductionVarAnalysis::InductionInfo* info1,
Aart Bikb3365e02015-09-21 14:45:05 -0700254 HInductionVarAnalysis::InductionInfo* info2,
255 bool is_min) {
Aart Bik7d57d7f2015-12-09 14:39:48 -0800256 return range_.GetDiv(info1, info2, nullptr, /* in_body */ true, is_min);
Aart Bik9401f532015-09-28 16:25:56 -0700257 }
258
Aart Bik97412c92016-02-19 20:14:38 -0800259 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 Bikd14c5952015-09-08 15:25:15 -0700269 }
270
Aart Bik7d57d7f2015-12-09 14:39:48 -0800271 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 Bikd14c5952015-09-08 15:25:15 -0700277
278 // General building fields.
279 ArenaPool pool_;
280 ArenaAllocator allocator_;
281 HGraph* graph_;
Aart Bikaec3cce2015-10-14 17:44:55 -0700282 HBasicBlock* entry_block_;
283 HBasicBlock* exit_block_;
284 HBasicBlock* loop_preheader_;
Aart Bik16d3a652016-09-09 10:33:50 -0700285 HBasicBlock* loop_header_;
286 HBasicBlock* loop_body_;
Aart Bikd14c5952015-09-08 15:25:15 -0700287 HInductionVarAnalysis* iva_;
Aart Bik7d57d7f2015-12-09 14:39:48 -0800288 InductionVarRange range_;
Aart Bikd14c5952015-09-08 15:25:15 -0700289
Aart Bikaec3cce2015-10-14 17:44:55 -0700290 // Instructions.
291 HInstruction* condition_;
292 HInstruction* increment_;
Aart Bik7d57d7f2015-12-09 14:39:48 -0800293 HInstruction* x_;
294 HInstruction* y_;
Aart Bikd14c5952015-09-08 15:25:15 -0700295};
296
297//
Aart Bik7d57d7f2015-12-09 14:39:48 -0800298// Tests on private methods.
Aart Bikd14c5952015-09-08 15:25:15 -0700299//
300
Aart Bik97412c92016-02-19 20:14:38 -0800301TEST_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 Bik389b3db2015-10-28 14:23:40 -0700329TEST_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 Bikd14c5952015-09-08 15:25:15 -0700349TEST_F(InductionVarRangeTest, GetMinMaxNull) {
Aart Bikb3365e02015-09-21 14:45:05 -0700350 ExpectEqual(Value(), GetMin(nullptr, nullptr));
351 ExpectEqual(Value(), GetMax(nullptr, nullptr));
Aart Bikd14c5952015-09-08 15:25:15 -0700352}
353
354TEST_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 Bik7d57d7f2015-12-09 14:39:48 -0800359 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 Bikd14c5952015-09-08 15:25:15 -0700367 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
373TEST_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 Bik7d57d7f2015-12-09 14:39:48 -0800378 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 Bikd14c5952015-09-08 15:25:15 -0700386 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
392TEST_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 Bik7d57d7f2015-12-09 14:39:48 -0800397 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 Bikd14c5952015-09-08 15:25:15 -0700399}
400
401TEST_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
408TEST_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
415TEST_F(InductionVarRangeTest, GetMinMaxConstant) {
416 ExpectEqual(Value(12345), GetMin(CreateConst(12345), nullptr));
417 ExpectEqual(Value(12345), GetMax(CreateConst(12345), nullptr));
418}
419
420TEST_F(InductionVarRangeTest, GetMinMaxFetch) {
Aart Bik7d57d7f2015-12-09 14:39:48 -0800421 ExpectEqual(Value(x_, 1, 0), GetMin(CreateFetch(x_), nullptr));
422 ExpectEqual(Value(x_, 1, 0), GetMax(CreateFetch(x_), nullptr));
Aart Bikd14c5952015-09-08 15:25:15 -0700423}
424
425TEST_F(InductionVarRangeTest, GetMinMaxLinear) {
Aart Bik389b3db2015-10-28 14:23:40 -0700426 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 Bikd14c5952015-09-08 15:25:15 -0700430}
431
432TEST_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 Bikc071a012016-12-01 10:22:31 -0800441TEST_F(InductionVarRangeTest, GetMinMaxGeometricMul) {
442 ExpectEqual(Value(), GetMin(CreateGeometric(1, 1, 1, '*'), nullptr));
443 ExpectEqual(Value(), GetMax(CreateGeometric(1, 1, 1, '*'), nullptr));
444}
445
446TEST_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
457TEST_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 Bikd14c5952015-09-08 15:25:15 -0700468TEST_F(InductionVarRangeTest, GetMinMaxPeriodic) {
469 ExpectEqual(Value(-2), GetMin(CreateRange(-2, 99), nullptr));
470 ExpectEqual(Value(99), GetMax(CreateRange(-2, 99), nullptr));
471}
472
473TEST_F(InductionVarRangeTest, GetMulMin) {
Aart Bik97412c92016-02-19 20:14:38 -0800474 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 Bikb3365e02015-09-21 14:45:05 -0700478 ExpectEqual(Value(6), GetMul(CreateRange(2, 10), CreateRange(3, 5), true));
479 ExpectEqual(Value(-50), GetMul(CreateRange(2, 10), CreateRange(-5, -3), true));
Aart Bik7d57d7f2015-12-09 14:39:48 -0800480 ExpectEqual(Value(), GetMul(CreateRange(2, 10), CreateRange(-1, 1), true));
Aart Bikb3365e02015-09-21 14:45:05 -0700481 ExpectEqual(Value(-50), GetMul(CreateRange(-10, -2), CreateRange(3, 5), true));
482 ExpectEqual(Value(6), GetMul(CreateRange(-10, -2), CreateRange(-5, -3), true));
Aart Bik7d57d7f2015-12-09 14:39:48 -0800483 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 Bikd14c5952015-09-08 15:25:15 -0700487}
488
489TEST_F(InductionVarRangeTest, GetMulMax) {
Aart Bik97412c92016-02-19 20:14:38 -0800490 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 Bikb3365e02015-09-21 14:45:05 -0700494 ExpectEqual(Value(50), GetMul(CreateRange(2, 10), CreateRange(3, 5), false));
495 ExpectEqual(Value(-6), GetMul(CreateRange(2, 10), CreateRange(-5, -3), false));
Aart Bik7d57d7f2015-12-09 14:39:48 -0800496 ExpectEqual(Value(), GetMul(CreateRange(2, 10), CreateRange(-1, 1), false));
Aart Bikb3365e02015-09-21 14:45:05 -0700497 ExpectEqual(Value(-6), GetMul(CreateRange(-10, -2), CreateRange(3, 5), false));
498 ExpectEqual(Value(50), GetMul(CreateRange(-10, -2), CreateRange(-5, -3), false));
Aart Bik7d57d7f2015-12-09 14:39:48 -0800499 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 Bikd14c5952015-09-08 15:25:15 -0700503}
504
505TEST_F(InductionVarRangeTest, GetDivMin) {
Aart Bik97412c92016-02-19 20:14:38 -0800506 ExpectEqual(Value(-5), GetDiv(CreateRange(-10, 20), CreateConst(2), true));
507 ExpectEqual(Value(-10), GetDiv(CreateRange(-10, 20), CreateConst(-2), true));
Aart Bikb3365e02015-09-21 14:45:05 -0700508 ExpectEqual(Value(10), GetDiv(CreateRange(40, 1000), CreateRange(2, 4), true));
509 ExpectEqual(Value(-500), GetDiv(CreateRange(40, 1000), CreateRange(-4, -2), true));
Aart Bik7d57d7f2015-12-09 14:39:48 -0800510 ExpectEqual(Value(), GetDiv(CreateRange(40, 1000), CreateRange(-1, 1), true));
Aart Bikb3365e02015-09-21 14:45:05 -0700511 ExpectEqual(Value(-500), GetDiv(CreateRange(-1000, -40), CreateRange(2, 4), true));
512 ExpectEqual(Value(10), GetDiv(CreateRange(-1000, -40), CreateRange(-4, -2), true));
Aart Bik7d57d7f2015-12-09 14:39:48 -0800513 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 Bikd14c5952015-09-08 15:25:15 -0700517}
518
519TEST_F(InductionVarRangeTest, GetDivMax) {
Aart Bik97412c92016-02-19 20:14:38 -0800520 ExpectEqual(Value(10), GetDiv(CreateRange(-10, 20), CreateConst(2), false));
521 ExpectEqual(Value(5), GetDiv(CreateRange(-10, 20), CreateConst(-2), false));
Aart Bikb3365e02015-09-21 14:45:05 -0700522 ExpectEqual(Value(500), GetDiv(CreateRange(40, 1000), CreateRange(2, 4), false));
523 ExpectEqual(Value(-10), GetDiv(CreateRange(40, 1000), CreateRange(-4, -2), false));
Aart Bik7d57d7f2015-12-09 14:39:48 -0800524 ExpectEqual(Value(), GetDiv(CreateRange(40, 1000), CreateRange(-1, 1), false));
Aart Bikb3365e02015-09-21 14:45:05 -0700525 ExpectEqual(Value(-10), GetDiv(CreateRange(-1000, -40), CreateRange(2, 4), false));
526 ExpectEqual(Value(500), GetDiv(CreateRange(-1000, -40), CreateRange(-4, -2), false));
Aart Bik7d57d7f2015-12-09 14:39:48 -0800527 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 Bikd14c5952015-09-08 15:25:15 -0700531}
532
533TEST_F(InductionVarRangeTest, AddValue) {
534 ExpectEqual(Value(110), AddValue(Value(10), Value(100)));
Aart Bik7d57d7f2015-12-09 14:39:48 -0800535 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 Bikcd26feb2015-09-23 17:50:50 -0700540 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 Bikd14c5952015-09-08 15:25:15 -0700543}
544
545TEST_F(InductionVarRangeTest, SubValue) {
546 ExpectEqual(Value(-90), SubValue(Value(10), Value(100)));
Aart Bik7d57d7f2015-12-09 14:39:48 -0800547 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 Bikcd26feb2015-09-23 17:50:50 -0700552 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 Bikd14c5952015-09-08 15:25:15 -0700555}
556
557TEST_F(InductionVarRangeTest, MulValue) {
558 ExpectEqual(Value(1000), MulValue(Value(10), Value(100)));
Aart Bik7d57d7f2015-12-09 14:39:48 -0800559 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 Bikb3365e02015-09-21 14:45:05 -0700563 ExpectEqual(Value(), MulValue(Value(90000), Value(-90000))); // unsafe
Aart Bikd14c5952015-09-08 15:25:15 -0700564}
565
Aart Bik97412c92016-02-19 20:14:38 -0800566TEST_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 Bikd14c5952015-09-08 15:25:15 -0700584TEST_F(InductionVarRangeTest, DivValue) {
585 ExpectEqual(Value(25), DivValue(Value(100), Value(4)));
Aart Bik7d57d7f2015-12-09 14:39:48 -0800586 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 Bikb3365e02015-09-21 14:45:05 -0700590 ExpectEqual(Value(), DivValue(Value(1), Value(0))); // unsafe
Aart Bikd14c5952015-09-08 15:25:15 -0700591}
592
Aart Bik97412c92016-02-19 20:14:38 -0800593TEST_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 Bikd14c5952015-09-08 15:25:15 -0700610TEST_F(InductionVarRangeTest, MinValue) {
611 ExpectEqual(Value(10), MinValue(Value(10), Value(100)));
Aart Bik7d57d7f2015-12-09 14:39:48 -0800612 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 Bikd14c5952015-09-08 15:25:15 -0700617}
618
619TEST_F(InductionVarRangeTest, MaxValue) {
620 ExpectEqual(Value(100), MaxValue(Value(10), Value(100)));
Aart Bik7d57d7f2015-12-09 14:39:48 -0800621 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 Bikd14c5952015-09-08 15:25:15 -0700626}
627
Aart Bik52be7e72016-06-23 11:20:41 -0700628TEST_F(InductionVarRangeTest, ArrayLengthAndHints) {
629 HInstruction* new_array = new (&allocator_)
630 HNewArray(x_,
631 graph_->GetCurrentMethod(),
Andreas Gampea5b09a62016-11-17 15:21:22 -0800632 0,
633 dex::TypeIndex(Primitive::kPrimInt),
Aart Bik52be7e72016-06-23 11:20:41 -0700634 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 Bikaec3cce2015-10-14 17:44:55 -0700654//
Aart Bik7d57d7f2015-12-09 14:39:48 -0800655// Tests on public methods.
Aart Bikaec3cce2015-10-14 17:44:55 -0700656//
657
Aart Bik389b3db2015-10-28 14:23:40 -0700658TEST_F(InductionVarRangeTest, ConstantTripCountUp) {
659 BuildLoop(0, graph_->GetIntConstant(1000), 1);
Aart Bikaec3cce2015-10-14 17:44:55 -0700660 PerformInductionVarAnalysis();
Aart Bikaec3cce2015-10-14 17:44:55 -0700661
Aart Bik389b3db2015-10-28 14:23:40 -0700662 Value v1, v2;
663 bool needs_finite_test = true;
Aart Bik16d3a652016-09-09 10:33:50 -0700664 bool needs_taken_test = true;
665
666 HInstruction* phi = condition_->InputAt(0);
667 HInstruction* exit = exit_block_->GetLastInstruction();
Aart Bik389b3db2015-10-28 14:23:40 -0700668
Aart Bikaec3cce2015-10-14 17:44:55 -0700669 // In context of header: known.
Aart Bik16d3a652016-09-09 10:33:50 -0700670 range_.GetInductionRange(condition_, phi, x_, &v1, &v2, &needs_finite_test);
Aart Bik389b3db2015-10-28 14:23:40 -0700671 EXPECT_FALSE(needs_finite_test);
672 ExpectEqual(Value(0), v1);
673 ExpectEqual(Value(1000), v2);
Aart Bikaec3cce2015-10-14 17:44:55 -0700674
675 // In context of loop-body: known.
Aart Bik16d3a652016-09-09 10:33:50 -0700676 range_.GetInductionRange(increment_, phi, x_, &v1, &v2, &needs_finite_test);
Aart Bik389b3db2015-10-28 14:23:40 -0700677 EXPECT_FALSE(needs_finite_test);
678 ExpectEqual(Value(0), v1);
679 ExpectEqual(Value(999), v2);
Aart Bik52be7e72016-06-23 11:20:41 -0700680 range_.GetInductionRange(increment_, increment_, x_, &v1, &v2, &needs_finite_test);
Aart Bik389b3db2015-10-28 14:23:40 -0700681 EXPECT_FALSE(needs_finite_test);
682 ExpectEqual(Value(1), v1);
683 ExpectEqual(Value(1000), v2);
Aart Bik16d3a652016-09-09 10:33:50 -0700684
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 Bikaec3cce2015-10-14 17:44:55 -0700698}
699
Aart Bik389b3db2015-10-28 14:23:40 -0700700TEST_F(InductionVarRangeTest, ConstantTripCountDown) {
701 BuildLoop(1000, graph_->GetIntConstant(0), -1);
Aart Bikaec3cce2015-10-14 17:44:55 -0700702 PerformInductionVarAnalysis();
Aart Bikaec3cce2015-10-14 17:44:55 -0700703
Aart Bik389b3db2015-10-28 14:23:40 -0700704 Value v1, v2;
705 bool needs_finite_test = true;
Aart Bik16d3a652016-09-09 10:33:50 -0700706 bool needs_taken_test = true;
707
708 HInstruction* phi = condition_->InputAt(0);
709 HInstruction* exit = exit_block_->GetLastInstruction();
Aart Bik389b3db2015-10-28 14:23:40 -0700710
711 // In context of header: known.
Aart Bik16d3a652016-09-09 10:33:50 -0700712 range_.GetInductionRange(condition_, phi, x_, &v1, &v2, &needs_finite_test);
Aart Bik389b3db2015-10-28 14:23:40 -0700713 EXPECT_FALSE(needs_finite_test);
714 ExpectEqual(Value(0), v1);
715 ExpectEqual(Value(1000), v2);
Aart Bikaec3cce2015-10-14 17:44:55 -0700716
717 // In context of loop-body: known.
Aart Bik16d3a652016-09-09 10:33:50 -0700718 range_.GetInductionRange(increment_, phi, x_, &v1, &v2, &needs_finite_test);
Aart Bik389b3db2015-10-28 14:23:40 -0700719 EXPECT_FALSE(needs_finite_test);
720 ExpectEqual(Value(1), v1);
721 ExpectEqual(Value(1000), v2);
Aart Bik52be7e72016-06-23 11:20:41 -0700722 range_.GetInductionRange(increment_, increment_, x_, &v1, &v2, &needs_finite_test);
Aart Bik389b3db2015-10-28 14:23:40 -0700723 EXPECT_FALSE(needs_finite_test);
724 ExpectEqual(Value(0), v1);
725 ExpectEqual(Value(999), v2);
Aart Bik16d3a652016-09-09 10:33:50 -0700726
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 Bikaec3cce2015-10-14 17:44:55 -0700745}
746
Aart Bik389b3db2015-10-28 14:23:40 -0700747TEST_F(InductionVarRangeTest, SymbolicTripCountUp) {
Aart Bik7d57d7f2015-12-09 14:39:48 -0800748 BuildLoop(0, x_, 1);
Aart Bikaec3cce2015-10-14 17:44:55 -0700749 PerformInductionVarAnalysis();
Aart Bikaec3cce2015-10-14 17:44:55 -0700750
Aart Bik389b3db2015-10-28 14:23:40 -0700751 Value v1, v2;
752 bool needs_finite_test = true;
753 bool needs_taken_test = true;
754
Aart Bik16d3a652016-09-09 10:33:50 -0700755 HInstruction* phi = condition_->InputAt(0);
756
Aart Bik389b3db2015-10-28 14:23:40 -0700757 // In context of header: upper unknown.
Aart Bik16d3a652016-09-09 10:33:50 -0700758 range_.GetInductionRange(condition_, phi, x_, &v1, &v2, &needs_finite_test);
Aart Bik389b3db2015-10-28 14:23:40 -0700759 EXPECT_FALSE(needs_finite_test);
760 ExpectEqual(Value(0), v1);
761 ExpectEqual(Value(), v2);
762
763 // In context of loop-body: known.
Aart Bik16d3a652016-09-09 10:33:50 -0700764 range_.GetInductionRange(increment_, phi, x_, &v1, &v2, &needs_finite_test);
Aart Bik389b3db2015-10-28 14:23:40 -0700765 EXPECT_FALSE(needs_finite_test);
766 ExpectEqual(Value(0), v1);
Aart Bik7d57d7f2015-12-09 14:39:48 -0800767 ExpectEqual(Value(x_, 1, -1), v2);
Aart Bik52be7e72016-06-23 11:20:41 -0700768 range_.GetInductionRange(increment_, increment_, x_, &v1, &v2, &needs_finite_test);
Aart Bik389b3db2015-10-28 14:23:40 -0700769 EXPECT_FALSE(needs_finite_test);
770 ExpectEqual(Value(1), v1);
Aart Bik7d57d7f2015-12-09 14:39:48 -0800771 ExpectEqual(Value(x_, 1, 0), v2);
Aart Bik389b3db2015-10-28 14:23:40 -0700772
Aart Bikaec3cce2015-10-14 17:44:55 -0700773 HInstruction* lower = nullptr;
774 HInstruction* upper = nullptr;
Aart Bikaec3cce2015-10-14 17:44:55 -0700775
776 // Can generate code in context of loop-body only.
Aart Bik16d3a652016-09-09 10:33:50 -0700777 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 Bik389b3db2015-10-28 14:23:40 -0700779 EXPECT_FALSE(needs_finite_test);
780 EXPECT_TRUE(needs_taken_test);
Aart Bikaec3cce2015-10-14 17:44:55 -0700781
Aart Bik16d3a652016-09-09 10:33:50 -0700782 // Generates code (unsimplified).
783 range_.GenerateRange(increment_, phi, graph_, loop_preheader_, &lower, &upper);
Aart Bikaec3cce2015-10-14 17:44:55 -0700784
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 Bik389b3db2015-10-28 14:23:40 -0700793 // Verify upper is (V-1)+0.
Aart Bikaec3cce2015-10-14 17:44:55 -0700794 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 Bik389b3db2015-10-28 14:23:40 -0700802
803 // Verify taken-test is 0<V.
Aart Bik16d3a652016-09-09 10:33:50 -0700804 HInstruction* taken = range_.GenerateTakenTest(increment_, graph_, loop_preheader_);
Aart Bik389b3db2015-10-28 14:23:40 -0700805 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 Bik16d3a652016-09-09 10:33:50 -0700810
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 Bik389b3db2015-10-28 14:23:40 -0700817}
818
819TEST_F(InductionVarRangeTest, SymbolicTripCountDown) {
Aart Bik7d57d7f2015-12-09 14:39:48 -0800820 BuildLoop(1000, x_, -1);
Aart Bik389b3db2015-10-28 14:23:40 -0700821 PerformInductionVarAnalysis();
Aart Bik389b3db2015-10-28 14:23:40 -0700822
823 Value v1, v2;
824 bool needs_finite_test = true;
825 bool needs_taken_test = true;
826
Aart Bik16d3a652016-09-09 10:33:50 -0700827 HInstruction* phi = condition_->InputAt(0);
828
Aart Bik389b3db2015-10-28 14:23:40 -0700829 // In context of header: lower unknown.
Aart Bik16d3a652016-09-09 10:33:50 -0700830 range_.GetInductionRange(condition_, phi, x_, &v1, &v2, &needs_finite_test);
Aart Bik389b3db2015-10-28 14:23:40 -0700831 EXPECT_FALSE(needs_finite_test);
832 ExpectEqual(Value(), v1);
833 ExpectEqual(Value(1000), v2);
834
835 // In context of loop-body: known.
Aart Bik16d3a652016-09-09 10:33:50 -0700836 range_.GetInductionRange(increment_, phi, x_, &v1, &v2, &needs_finite_test);
Aart Bik389b3db2015-10-28 14:23:40 -0700837 EXPECT_FALSE(needs_finite_test);
Aart Bik7d57d7f2015-12-09 14:39:48 -0800838 ExpectEqual(Value(x_, 1, 1), v1);
Aart Bik389b3db2015-10-28 14:23:40 -0700839 ExpectEqual(Value(1000), v2);
Aart Bik52be7e72016-06-23 11:20:41 -0700840 range_.GetInductionRange(increment_, increment_, x_, &v1, &v2, &needs_finite_test);
Aart Bik389b3db2015-10-28 14:23:40 -0700841 EXPECT_FALSE(needs_finite_test);
Aart Bik7d57d7f2015-12-09 14:39:48 -0800842 ExpectEqual(Value(x_, 1, 0), v1);
Aart Bik389b3db2015-10-28 14:23:40 -0700843 ExpectEqual(Value(999), v2);
844
845 HInstruction* lower = nullptr;
846 HInstruction* upper = nullptr;
Aart Bik389b3db2015-10-28 14:23:40 -0700847
848 // Can generate code in context of loop-body only.
Aart Bik16d3a652016-09-09 10:33:50 -0700849 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 Bik389b3db2015-10-28 14:23:40 -0700851 EXPECT_FALSE(needs_finite_test);
852 EXPECT_TRUE(needs_taken_test);
853
Aart Bik16d3a652016-09-09 10:33:50 -0700854 // Generates code (unsimplified).
855 range_.GenerateRange(increment_, phi, graph_, loop_preheader_, &lower, &upper);
Aart Bik389b3db2015-10-28 14:23:40 -0700856
Aart Bik7d57d7f2015-12-09 14:39:48 -0800857 // Verify lower is 1000-((1000-V)-1).
Aart Bik389b3db2015-10-28 14:23:40 -0700858 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 Bik389b3db2015-10-28 14:23:40 -0700867 ASSERT_TRUE(lower->IsSub());
Aart Bik7d57d7f2015-12-09 14:39:48 -0800868 ASSERT_TRUE(lower->InputAt(0)->IsIntConstant());
869 EXPECT_EQ(1000, lower->InputAt(0)->AsIntConstant()->GetValue());
870 EXPECT_TRUE(lower->InputAt(1)->IsParameterValue());
Aart Bik389b3db2015-10-28 14:23:40 -0700871
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 Bik16d3a652016-09-09 10:33:50 -0700881 HInstruction* taken = range_.GenerateTakenTest(increment_, graph_, loop_preheader_);
Aart Bik389b3db2015-10-28 14:23:40 -0700882 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 Bik16d3a652016-09-09 10:33:50 -0700887
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 Bikaec3cce2015-10-14 17:44:55 -0700894}
895
Aart Bikd14c5952015-09-08 15:25:15 -0700896} // namespace art