blob: 1c8426954ba0f5b34983813e2e9b41d245de8ac1 [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
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070017#include "induction_var_range.h"
18
Aart Bikd14c5952015-09-08 15:25:15 -070019#include "base/arena_allocator.h"
20#include "builder.h"
Aart Bikd14c5952015-09-08 15:25:15 -070021#include "induction_var_analysis.h"
Aart Bikd14c5952015-09-08 15:25:15 -070022#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 */
David Brazdil4833f5a2015-12-16 10:37:39 +000032class InductionVarRangeTest : public CommonCompilerTest {
Aart Bikd14c5952015-09-08 15:25:15 -070033 public:
Aart Bik7d57d7f2015-12-09 14:39:48 -080034 InductionVarRangeTest()
35 : pool_(),
36 allocator_(&pool_),
37 graph_(CreateGraph(&allocator_)),
38 iva_(new (&allocator_) HInductionVarAnalysis(graph_)),
39 range_(iva_) {
Aart Bikd14c5952015-09-08 15:25:15 -070040 BuildGraph();
41 }
42
43 ~InductionVarRangeTest() { }
44
45 void ExpectEqual(Value v1, Value v2) {
46 EXPECT_EQ(v1.instruction, v2.instruction);
47 EXPECT_EQ(v1.a_constant, v2.a_constant);
48 EXPECT_EQ(v1.b_constant, v2.b_constant);
Aart Bikb3365e02015-09-21 14:45:05 -070049 EXPECT_EQ(v1.is_known, v2.is_known);
Aart Bikd14c5952015-09-08 15:25:15 -070050 }
51
Aart Bik8e02e3e2017-02-28 14:41:55 -080052 void ExpectInt(int32_t value, HInstruction* i) {
53 ASSERT_TRUE(i->IsIntConstant());
54 EXPECT_EQ(value, i->AsIntConstant()->GetValue());
55 }
56
Aart Bik389b3db2015-10-28 14:23:40 -070057 //
58 // Construction methods.
59 //
60
Aart Bikd14c5952015-09-08 15:25:15 -070061 /** Constructs bare minimum graph. */
62 void BuildGraph() {
63 graph_->SetNumberOfVRegs(1);
Aart Bikaec3cce2015-10-14 17:44:55 -070064 entry_block_ = new (&allocator_) HBasicBlock(graph_);
65 exit_block_ = new (&allocator_) HBasicBlock(graph_);
66 graph_->AddBlock(entry_block_);
67 graph_->AddBlock(exit_block_);
68 graph_->SetEntryBlock(entry_block_);
69 graph_->SetExitBlock(exit_block_);
Aart Bik7d57d7f2015-12-09 14:39:48 -080070 // Two parameters.
Andreas Gampea5b09a62016-11-17 15:21:22 -080071 x_ = new (&allocator_) HParameterValue(graph_->GetDexFile(),
72 dex::TypeIndex(0),
73 0,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010074 DataType::Type::kInt32);
Aart Bik7d57d7f2015-12-09 14:39:48 -080075 entry_block_->AddInstruction(x_);
Andreas Gampea5b09a62016-11-17 15:21:22 -080076 y_ = new (&allocator_) HParameterValue(graph_->GetDexFile(),
77 dex::TypeIndex(0),
78 0,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010079 DataType::Type::kInt32);
Aart Bik7d57d7f2015-12-09 14:39:48 -080080 entry_block_->AddInstruction(y_);
Aart Bik52be7e72016-06-23 11:20:41 -070081 // Set arbitrary range analysis hint while testing private methods.
82 SetHint(x_);
Aart Bikaec3cce2015-10-14 17:44:55 -070083 }
84
85 /** Constructs loop with given upper bound. */
Aart Bik389b3db2015-10-28 14:23:40 -070086 void BuildLoop(int32_t lower, HInstruction* upper, int32_t stride) {
Aart Bikaec3cce2015-10-14 17:44:55 -070087 // Control flow.
88 loop_preheader_ = new (&allocator_) HBasicBlock(graph_);
89 graph_->AddBlock(loop_preheader_);
Aart Bik16d3a652016-09-09 10:33:50 -070090 loop_header_ = new (&allocator_) HBasicBlock(graph_);
91 graph_->AddBlock(loop_header_);
92 loop_body_ = new (&allocator_) HBasicBlock(graph_);
93 graph_->AddBlock(loop_body_);
David Brazdildb51efb2015-11-06 01:36:20 +000094 HBasicBlock* return_block = new (&allocator_) HBasicBlock(graph_);
95 graph_->AddBlock(return_block);
Aart Bikaec3cce2015-10-14 17:44:55 -070096 entry_block_->AddSuccessor(loop_preheader_);
Aart Bik16d3a652016-09-09 10:33:50 -070097 loop_preheader_->AddSuccessor(loop_header_);
98 loop_header_->AddSuccessor(loop_body_);
99 loop_header_->AddSuccessor(return_block);
100 loop_body_->AddSuccessor(loop_header_);
David Brazdildb51efb2015-11-06 01:36:20 +0000101 return_block->AddSuccessor(exit_block_);
Aart Bikaec3cce2015-10-14 17:44:55 -0700102 // Instructions.
Aart Bikaec3cce2015-10-14 17:44:55 -0700103 loop_preheader_->AddInstruction(new (&allocator_) HGoto());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100104 HPhi* phi = new (&allocator_) HPhi(&allocator_, 0, 0, DataType::Type::kInt32);
Aart Bik16d3a652016-09-09 10:33:50 -0700105 loop_header_->AddPhi(phi);
David Brazdilbadd8262016-02-02 16:28:56 +0000106 phi->AddInput(graph_->GetIntConstant(lower)); // i = l
Aart Bik389b3db2015-10-28 14:23:40 -0700107 if (stride > 0) {
David Brazdilbadd8262016-02-02 16:28:56 +0000108 condition_ = new (&allocator_) HLessThan(phi, upper); // i < u
Aart Bik389b3db2015-10-28 14:23:40 -0700109 } else {
David Brazdilbadd8262016-02-02 16:28:56 +0000110 condition_ = new (&allocator_) HGreaterThan(phi, upper); // i > u
Aart Bik389b3db2015-10-28 14:23:40 -0700111 }
Aart Bik16d3a652016-09-09 10:33:50 -0700112 loop_header_->AddInstruction(condition_);
113 loop_header_->AddInstruction(new (&allocator_) HIf(condition_));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100114 increment_ =
115 new (&allocator_) HAdd(DataType::Type::kInt32, phi, graph_->GetIntConstant(stride));
Aart Bik16d3a652016-09-09 10:33:50 -0700116 loop_body_->AddInstruction(increment_); // i += s
David Brazdilbadd8262016-02-02 16:28:56 +0000117 phi->AddInput(increment_);
Aart Bik16d3a652016-09-09 10:33:50 -0700118 loop_body_->AddInstruction(new (&allocator_) HGoto());
David Brazdildb51efb2015-11-06 01:36:20 +0000119 return_block->AddInstruction(new (&allocator_) HReturnVoid());
120 exit_block_->AddInstruction(new (&allocator_) HExit());
Aart Bikaec3cce2015-10-14 17:44:55 -0700121 }
122
Aart Bik7d57d7f2015-12-09 14:39:48 -0800123 /** Constructs SSA and performs induction variable analysis. */
Aart Bikaec3cce2015-10-14 17:44:55 -0700124 void PerformInductionVarAnalysis() {
David Brazdilbadd8262016-02-02 16:28:56 +0000125 graph_->BuildDominatorTree();
Aart Bikaec3cce2015-10-14 17:44:55 -0700126 iva_->Run();
Aart Bikd14c5952015-09-08 15:25:15 -0700127 }
128
Aart Bik52be7e72016-06-23 11:20:41 -0700129 /** Sets hint. */
130 void SetHint(HInstruction* hint) {
131 range_.chase_hint_ = hint;
132 }
133
Aart Bikd14c5952015-09-08 15:25:15 -0700134 /** Constructs an invariant. */
135 HInductionVarAnalysis::InductionInfo* CreateInvariant(char opc,
136 HInductionVarAnalysis::InductionInfo* a,
137 HInductionVarAnalysis::InductionInfo* b) {
138 HInductionVarAnalysis::InductionOp op;
139 switch (opc) {
140 case '+': op = HInductionVarAnalysis::kAdd; break;
141 case '-': op = HInductionVarAnalysis::kSub; break;
142 case 'n': op = HInductionVarAnalysis::kNeg; break;
143 case '*': op = HInductionVarAnalysis::kMul; break;
144 case '/': op = HInductionVarAnalysis::kDiv; break;
Aart Bikdf7822e2016-12-06 10:05:30 -0800145 case '%': op = HInductionVarAnalysis::kRem; break;
146 case '^': op = HInductionVarAnalysis::kXor; break;
Aart Bik52be7e72016-06-23 11:20:41 -0700147 case '<': op = HInductionVarAnalysis::kLT; break;
Aart Bikd14c5952015-09-08 15:25:15 -0700148 default: op = HInductionVarAnalysis::kNop; break;
149 }
150 return iva_->CreateInvariantOp(op, a, b);
151 }
152
153 /** Constructs a fetch. */
154 HInductionVarAnalysis::InductionInfo* CreateFetch(HInstruction* fetch) {
155 return iva_->CreateInvariantFetch(fetch);
156 }
157
158 /** Constructs a constant. */
159 HInductionVarAnalysis::InductionInfo* CreateConst(int32_t c) {
160 return CreateFetch(graph_->GetIntConstant(c));
161 }
162
Aart Bik52be7e72016-06-23 11:20:41 -0700163 /** Constructs a constant trip-count. */
Aart Bik389b3db2015-10-28 14:23:40 -0700164 HInductionVarAnalysis::InductionInfo* CreateTripCount(int32_t tc, bool in_loop, bool safe) {
Aart Bik52be7e72016-06-23 11:20:41 -0700165 HInductionVarAnalysis::InductionOp op = HInductionVarAnalysis::kTripCountInBodyUnsafe;
Aart Bik389b3db2015-10-28 14:23:40 -0700166 if (in_loop && safe) {
Aart Bik52be7e72016-06-23 11:20:41 -0700167 op = HInductionVarAnalysis::kTripCountInLoop;
Aart Bik389b3db2015-10-28 14:23:40 -0700168 } else if (in_loop) {
Aart Bik52be7e72016-06-23 11:20:41 -0700169 op = HInductionVarAnalysis::kTripCountInLoopUnsafe;
Aart Bik389b3db2015-10-28 14:23:40 -0700170 } else if (safe) {
Aart Bik52be7e72016-06-23 11:20:41 -0700171 op = HInductionVarAnalysis::kTripCountInBody;
Aart Bik389b3db2015-10-28 14:23:40 -0700172 }
Aart Bik52be7e72016-06-23 11:20:41 -0700173 // Return TC with taken-test 0 < TC.
174 return iva_->CreateTripCount(op,
175 CreateConst(tc),
176 CreateInvariant('<', CreateConst(0), CreateConst(tc)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100177 DataType::Type::kInt32);
Aart Bikd14c5952015-09-08 15:25:15 -0700178 }
179
180 /** Constructs a linear a * i + b induction. */
181 HInductionVarAnalysis::InductionInfo* CreateLinear(int32_t a, int32_t b) {
Aart Bikc071a012016-12-01 10:22:31 -0800182 return iva_->CreateInduction(HInductionVarAnalysis::kLinear,
183 HInductionVarAnalysis::kNop,
184 CreateConst(a),
185 CreateConst(b),
186 nullptr,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100187 DataType::Type::kInt32);
Aart Bikc071a012016-12-01 10:22:31 -0800188 }
189
Aart Bikdf7822e2016-12-06 10:05:30 -0800190 /** Constructs a polynomial sum(a * i + b) + c induction. */
191 HInductionVarAnalysis::InductionInfo* CreatePolynomial(int32_t a, int32_t b, int32_t c) {
192 return iva_->CreateInduction(HInductionVarAnalysis::kPolynomial,
193 HInductionVarAnalysis::kNop,
194 CreateLinear(a, b),
195 CreateConst(c),
196 nullptr,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100197 DataType::Type::kInt32);
Aart Bikdf7822e2016-12-06 10:05:30 -0800198 }
199
Aart Bikc071a012016-12-01 10:22:31 -0800200 /** Constructs a geometric a * f^i + b induction. */
201 HInductionVarAnalysis::InductionInfo* CreateGeometric(int32_t a, int32_t b, int32_t f, char op) {
202 return iva_->CreateInduction(HInductionVarAnalysis::kGeometric,
Aart Bikdf7822e2016-12-06 10:05:30 -0800203 op == '*' ? HInductionVarAnalysis::kMul
204 : HInductionVarAnalysis::kDiv,
Aart Bikc071a012016-12-01 10:22:31 -0800205 CreateConst(a),
206 CreateConst(b),
207 graph_->GetIntConstant(f),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100208 DataType::Type::kInt32);
Aart Bikd14c5952015-09-08 15:25:15 -0700209 }
210
211 /** Constructs a range [lo, hi] using a periodic induction. */
212 HInductionVarAnalysis::InductionInfo* CreateRange(int32_t lo, int32_t hi) {
Aart Bikc071a012016-12-01 10:22:31 -0800213 return iva_->CreateInduction(HInductionVarAnalysis::kPeriodic,
214 HInductionVarAnalysis::kNop,
215 CreateConst(lo),
216 CreateConst(hi),
217 nullptr,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100218 DataType::Type::kInt32);
Aart Bikd14c5952015-09-08 15:25:15 -0700219 }
220
Aart Bikdf7822e2016-12-06 10:05:30 -0800221 /** Constructs a wrap-around induction consisting of a constant, followed by info. */
Aart Bik389b3db2015-10-28 14:23:40 -0700222 HInductionVarAnalysis::InductionInfo* CreateWrapAround(
223 int32_t initial,
224 HInductionVarAnalysis::InductionInfo* info) {
Aart Bikc071a012016-12-01 10:22:31 -0800225 return iva_->CreateInduction(HInductionVarAnalysis::kWrapAround,
226 HInductionVarAnalysis::kNop,
227 CreateConst(initial),
228 info,
229 nullptr,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100230 DataType::Type::kInt32);
Aart Bik389b3db2015-10-28 14:23:40 -0700231 }
232
Aart Bikd14c5952015-09-08 15:25:15 -0700233 /** Constructs a wrap-around induction consisting of a constant, followed by a range. */
234 HInductionVarAnalysis::InductionInfo* CreateWrapAround(int32_t initial, int32_t lo, int32_t hi) {
Aart Bik389b3db2015-10-28 14:23:40 -0700235 return CreateWrapAround(initial, CreateRange(lo, hi));
Aart Bikd14c5952015-09-08 15:25:15 -0700236 }
237
238 //
239 // Relay methods.
240 //
241
Aart Bik389b3db2015-10-28 14:23:40 -0700242 bool NeedsTripCount(HInductionVarAnalysis::InductionInfo* info) {
Aart Bik16d3a652016-09-09 10:33:50 -0700243 int64_t s = 0;
244 return range_.NeedsTripCount(info, &s);
Aart Bik389b3db2015-10-28 14:23:40 -0700245 }
246
247 bool IsBodyTripCount(HInductionVarAnalysis::InductionInfo* trip) {
Aart Bik7d57d7f2015-12-09 14:39:48 -0800248 return range_.IsBodyTripCount(trip);
Aart Bik389b3db2015-10-28 14:23:40 -0700249 }
250
251 bool IsUnsafeTripCount(HInductionVarAnalysis::InductionInfo* trip) {
Aart Bik7d57d7f2015-12-09 14:39:48 -0800252 return range_.IsUnsafeTripCount(trip);
Aart Bik389b3db2015-10-28 14:23:40 -0700253 }
254
Aart Bikd14c5952015-09-08 15:25:15 -0700255 Value GetMin(HInductionVarAnalysis::InductionInfo* info,
Aart Bik52be7e72016-06-23 11:20:41 -0700256 HInductionVarAnalysis::InductionInfo* trip) {
257 return range_.GetVal(info, trip, /* in_body */ true, /* is_min */ true);
Aart Bikd14c5952015-09-08 15:25:15 -0700258 }
259
260 Value GetMax(HInductionVarAnalysis::InductionInfo* info,
Aart Bik52be7e72016-06-23 11:20:41 -0700261 HInductionVarAnalysis::InductionInfo* trip) {
262 return range_.GetVal(info, trip, /* in_body */ true, /* is_min */ false);
Aart Bikd14c5952015-09-08 15:25:15 -0700263 }
264
265 Value GetMul(HInductionVarAnalysis::InductionInfo* info1,
Aart Bikb3365e02015-09-21 14:45:05 -0700266 HInductionVarAnalysis::InductionInfo* info2,
267 bool is_min) {
Aart Bik7d57d7f2015-12-09 14:39:48 -0800268 return range_.GetMul(info1, info2, nullptr, /* in_body */ true, is_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700269 }
270
271 Value GetDiv(HInductionVarAnalysis::InductionInfo* info1,
Aart Bikb3365e02015-09-21 14:45:05 -0700272 HInductionVarAnalysis::InductionInfo* info2,
273 bool is_min) {
Aart Bik7d57d7f2015-12-09 14:39:48 -0800274 return range_.GetDiv(info1, info2, nullptr, /* in_body */ true, is_min);
Aart Bik9401f532015-09-28 16:25:56 -0700275 }
276
Aart Bikdf7822e2016-12-06 10:05:30 -0800277 Value GetRem(HInductionVarAnalysis::InductionInfo* info1,
278 HInductionVarAnalysis::InductionInfo* info2) {
279 return range_.GetRem(info1, info2);
280 }
281
282 Value GetXor(HInductionVarAnalysis::InductionInfo* info1,
283 HInductionVarAnalysis::InductionInfo* info2) {
284 return range_.GetXor(info1, info2);
285 }
286
Aart Bik97412c92016-02-19 20:14:38 -0800287 bool IsExact(HInductionVarAnalysis::InductionInfo* info, int64_t* value) {
288 return range_.IsConstant(info, InductionVarRange::kExact, value);
289 }
290
291 bool IsAtMost(HInductionVarAnalysis::InductionInfo* info, int64_t* value) {
292 return range_.IsConstant(info, InductionVarRange::kAtMost, value);
293 }
294
295 bool IsAtLeast(HInductionVarAnalysis::InductionInfo* info, int64_t* value) {
296 return range_.IsConstant(info, InductionVarRange::kAtLeast, value);
Aart Bikd14c5952015-09-08 15:25:15 -0700297 }
298
Aart Bik7d57d7f2015-12-09 14:39:48 -0800299 Value AddValue(Value v1, Value v2) { return range_.AddValue(v1, v2); }
300 Value SubValue(Value v1, Value v2) { return range_.SubValue(v1, v2); }
301 Value MulValue(Value v1, Value v2) { return range_.MulValue(v1, v2); }
302 Value DivValue(Value v1, Value v2) { return range_.DivValue(v1, v2); }
303 Value MinValue(Value v1, Value v2) { return range_.MergeVal(v1, v2, true); }
304 Value MaxValue(Value v1, Value v2) { return range_.MergeVal(v1, v2, false); }
Aart Bikd14c5952015-09-08 15:25:15 -0700305
306 // General building fields.
307 ArenaPool pool_;
308 ArenaAllocator allocator_;
309 HGraph* graph_;
Aart Bikaec3cce2015-10-14 17:44:55 -0700310 HBasicBlock* entry_block_;
311 HBasicBlock* exit_block_;
312 HBasicBlock* loop_preheader_;
Aart Bik16d3a652016-09-09 10:33:50 -0700313 HBasicBlock* loop_header_;
314 HBasicBlock* loop_body_;
Aart Bikd14c5952015-09-08 15:25:15 -0700315 HInductionVarAnalysis* iva_;
Aart Bik7d57d7f2015-12-09 14:39:48 -0800316 InductionVarRange range_;
Aart Bikd14c5952015-09-08 15:25:15 -0700317
Aart Bikaec3cce2015-10-14 17:44:55 -0700318 // Instructions.
319 HInstruction* condition_;
320 HInstruction* increment_;
Aart Bik7d57d7f2015-12-09 14:39:48 -0800321 HInstruction* x_;
322 HInstruction* y_;
Aart Bikd14c5952015-09-08 15:25:15 -0700323};
324
325//
Aart Bik7d57d7f2015-12-09 14:39:48 -0800326// Tests on private methods.
Aart Bikd14c5952015-09-08 15:25:15 -0700327//
328
Aart Bik97412c92016-02-19 20:14:38 -0800329TEST_F(InductionVarRangeTest, IsConstant) {
330 int64_t value;
331 // Constant.
332 EXPECT_TRUE(IsExact(CreateConst(12345), &value));
333 EXPECT_EQ(12345, value);
334 EXPECT_TRUE(IsAtMost(CreateConst(12345), &value));
335 EXPECT_EQ(12345, value);
336 EXPECT_TRUE(IsAtLeast(CreateConst(12345), &value));
337 EXPECT_EQ(12345, value);
338 // Constant trivial range.
339 EXPECT_TRUE(IsExact(CreateRange(111, 111), &value));
340 EXPECT_EQ(111, value);
341 EXPECT_TRUE(IsAtMost(CreateRange(111, 111), &value));
342 EXPECT_EQ(111, value);
343 EXPECT_TRUE(IsAtLeast(CreateRange(111, 111), &value));
344 EXPECT_EQ(111, value);
345 // Constant non-trivial range.
346 EXPECT_FALSE(IsExact(CreateRange(11, 22), &value));
347 EXPECT_TRUE(IsAtMost(CreateRange(11, 22), &value));
348 EXPECT_EQ(22, value);
349 EXPECT_TRUE(IsAtLeast(CreateRange(11, 22), &value));
350 EXPECT_EQ(11, value);
351 // Symbolic.
352 EXPECT_FALSE(IsExact(CreateFetch(x_), &value));
353 EXPECT_FALSE(IsAtMost(CreateFetch(x_), &value));
354 EXPECT_FALSE(IsAtLeast(CreateFetch(x_), &value));
355}
356
Aart Bik389b3db2015-10-28 14:23:40 -0700357TEST_F(InductionVarRangeTest, TripCountProperties) {
358 EXPECT_FALSE(NeedsTripCount(nullptr));
359 EXPECT_FALSE(NeedsTripCount(CreateConst(1)));
360 EXPECT_TRUE(NeedsTripCount(CreateLinear(1, 1)));
361 EXPECT_FALSE(NeedsTripCount(CreateWrapAround(1, 2, 3)));
362 EXPECT_TRUE(NeedsTripCount(CreateWrapAround(1, CreateLinear(1, 1))));
363
364 EXPECT_FALSE(IsBodyTripCount(nullptr));
365 EXPECT_FALSE(IsBodyTripCount(CreateTripCount(100, true, true)));
366 EXPECT_FALSE(IsBodyTripCount(CreateTripCount(100, true, false)));
367 EXPECT_TRUE(IsBodyTripCount(CreateTripCount(100, false, true)));
368 EXPECT_TRUE(IsBodyTripCount(CreateTripCount(100, false, false)));
369
370 EXPECT_FALSE(IsUnsafeTripCount(nullptr));
371 EXPECT_FALSE(IsUnsafeTripCount(CreateTripCount(100, true, true)));
372 EXPECT_TRUE(IsUnsafeTripCount(CreateTripCount(100, true, false)));
373 EXPECT_FALSE(IsUnsafeTripCount(CreateTripCount(100, false, true)));
374 EXPECT_TRUE(IsUnsafeTripCount(CreateTripCount(100, false, false)));
375}
376
Aart Bikd14c5952015-09-08 15:25:15 -0700377TEST_F(InductionVarRangeTest, GetMinMaxNull) {
Aart Bikb3365e02015-09-21 14:45:05 -0700378 ExpectEqual(Value(), GetMin(nullptr, nullptr));
379 ExpectEqual(Value(), GetMax(nullptr, nullptr));
Aart Bikd14c5952015-09-08 15:25:15 -0700380}
381
382TEST_F(InductionVarRangeTest, GetMinMaxAdd) {
383 ExpectEqual(Value(12),
384 GetMin(CreateInvariant('+', CreateConst(2), CreateRange(10, 20)), nullptr));
385 ExpectEqual(Value(22),
386 GetMax(CreateInvariant('+', CreateConst(2), CreateRange(10, 20)), nullptr));
Aart Bik7d57d7f2015-12-09 14:39:48 -0800387 ExpectEqual(Value(x_, 1, -20),
388 GetMin(CreateInvariant('+', CreateFetch(x_), CreateRange(-20, -10)), nullptr));
389 ExpectEqual(Value(x_, 1, -10),
390 GetMax(CreateInvariant('+', CreateFetch(x_), CreateRange(-20, -10)), nullptr));
391 ExpectEqual(Value(x_, 1, 10),
392 GetMin(CreateInvariant('+', CreateRange(10, 20), CreateFetch(x_)), nullptr));
393 ExpectEqual(Value(x_, 1, 20),
394 GetMax(CreateInvariant('+', CreateRange(10, 20), CreateFetch(x_)), nullptr));
Aart Bikd14c5952015-09-08 15:25:15 -0700395 ExpectEqual(Value(5),
396 GetMin(CreateInvariant('+', CreateRange(-5, -1), CreateRange(10, 20)), nullptr));
397 ExpectEqual(Value(19),
398 GetMax(CreateInvariant('+', CreateRange(-5, -1), CreateRange(10, 20)), nullptr));
399}
400
401TEST_F(InductionVarRangeTest, GetMinMaxSub) {
402 ExpectEqual(Value(-18),
403 GetMin(CreateInvariant('-', CreateConst(2), CreateRange(10, 20)), nullptr));
404 ExpectEqual(Value(-8),
405 GetMax(CreateInvariant('-', CreateConst(2), CreateRange(10, 20)), nullptr));
Aart Bik7d57d7f2015-12-09 14:39:48 -0800406 ExpectEqual(Value(x_, 1, 10),
407 GetMin(CreateInvariant('-', CreateFetch(x_), CreateRange(-20, -10)), nullptr));
408 ExpectEqual(Value(x_, 1, 20),
409 GetMax(CreateInvariant('-', CreateFetch(x_), CreateRange(-20, -10)), nullptr));
410 ExpectEqual(Value(x_, -1, 10),
411 GetMin(CreateInvariant('-', CreateRange(10, 20), CreateFetch(x_)), nullptr));
412 ExpectEqual(Value(x_, -1, 20),
413 GetMax(CreateInvariant('-', CreateRange(10, 20), CreateFetch(x_)), nullptr));
Aart Bikd14c5952015-09-08 15:25:15 -0700414 ExpectEqual(Value(-25),
415 GetMin(CreateInvariant('-', CreateRange(-5, -1), CreateRange(10, 20)), nullptr));
416 ExpectEqual(Value(-11),
417 GetMax(CreateInvariant('-', CreateRange(-5, -1), CreateRange(10, 20)), nullptr));
418}
419
420TEST_F(InductionVarRangeTest, GetMinMaxNeg) {
421 ExpectEqual(Value(-20), GetMin(CreateInvariant('n', nullptr, CreateRange(10, 20)), nullptr));
422 ExpectEqual(Value(-10), GetMax(CreateInvariant('n', nullptr, CreateRange(10, 20)), nullptr));
423 ExpectEqual(Value(10), GetMin(CreateInvariant('n', nullptr, CreateRange(-20, -10)), nullptr));
424 ExpectEqual(Value(20), GetMax(CreateInvariant('n', nullptr, CreateRange(-20, -10)), nullptr));
Aart Bik7d57d7f2015-12-09 14:39:48 -0800425 ExpectEqual(Value(x_, -1, 0), GetMin(CreateInvariant('n', nullptr, CreateFetch(x_)), nullptr));
426 ExpectEqual(Value(x_, -1, 0), GetMax(CreateInvariant('n', nullptr, CreateFetch(x_)), nullptr));
Aart Bikd14c5952015-09-08 15:25:15 -0700427}
428
429TEST_F(InductionVarRangeTest, GetMinMaxMul) {
430 ExpectEqual(Value(20),
431 GetMin(CreateInvariant('*', CreateConst(2), CreateRange(10, 20)), nullptr));
432 ExpectEqual(Value(40),
433 GetMax(CreateInvariant('*', CreateConst(2), CreateRange(10, 20)), nullptr));
434}
435
436TEST_F(InductionVarRangeTest, GetMinMaxDiv) {
437 ExpectEqual(Value(3),
438 GetMin(CreateInvariant('/', CreateRange(12, 20), CreateConst(4)), nullptr));
439 ExpectEqual(Value(5),
440 GetMax(CreateInvariant('/', CreateRange(12, 20), CreateConst(4)), nullptr));
441}
442
443TEST_F(InductionVarRangeTest, GetMinMaxConstant) {
444 ExpectEqual(Value(12345), GetMin(CreateConst(12345), nullptr));
445 ExpectEqual(Value(12345), GetMax(CreateConst(12345), nullptr));
446}
447
448TEST_F(InductionVarRangeTest, GetMinMaxFetch) {
Aart Bik7d57d7f2015-12-09 14:39:48 -0800449 ExpectEqual(Value(x_, 1, 0), GetMin(CreateFetch(x_), nullptr));
450 ExpectEqual(Value(x_, 1, 0), GetMax(CreateFetch(x_), nullptr));
Aart Bikd14c5952015-09-08 15:25:15 -0700451}
452
453TEST_F(InductionVarRangeTest, GetMinMaxLinear) {
Aart Bik389b3db2015-10-28 14:23:40 -0700454 ExpectEqual(Value(20), GetMin(CreateLinear(10, 20), CreateTripCount(100, true, true)));
455 ExpectEqual(Value(1010), GetMax(CreateLinear(10, 20), CreateTripCount(100, true, true)));
456 ExpectEqual(Value(-970), GetMin(CreateLinear(-10, 20), CreateTripCount(100, true, true)));
457 ExpectEqual(Value(20), GetMax(CreateLinear(-10, 20), CreateTripCount(100, true, true)));
Aart Bikd14c5952015-09-08 15:25:15 -0700458}
459
460TEST_F(InductionVarRangeTest, GetMinMaxWrapAround) {
461 ExpectEqual(Value(-5), GetMin(CreateWrapAround(-5, -1, 10), nullptr));
462 ExpectEqual(Value(10), GetMax(CreateWrapAround(-5, -1, 10), nullptr));
463 ExpectEqual(Value(-1), GetMin(CreateWrapAround(2, -1, 10), nullptr));
464 ExpectEqual(Value(10), GetMax(CreateWrapAround(2, -1, 10), nullptr));
465 ExpectEqual(Value(-1), GetMin(CreateWrapAround(20, -1, 10), nullptr));
466 ExpectEqual(Value(20), GetMax(CreateWrapAround(20, -1, 10), nullptr));
467}
468
Aart Bikdf7822e2016-12-06 10:05:30 -0800469TEST_F(InductionVarRangeTest, GetMinMaxPolynomial) {
470 ExpectEqual(Value(7), GetMin(CreatePolynomial(3, 5, 7), nullptr));
471 ExpectEqual(Value(), GetMax(CreatePolynomial(3, 5, 7), nullptr));
472 ExpectEqual(Value(7), GetMin(CreatePolynomial(3, 5, 7), CreateTripCount(5, true, true)));
473 ExpectEqual(Value(45), GetMax(CreatePolynomial(3, 5, 7), CreateTripCount(5, true, true)));
474 ExpectEqual(Value(7), GetMin(CreatePolynomial(3, 5, 7), CreateTripCount(10, true, true)));
475 ExpectEqual(Value(160), GetMax(CreatePolynomial(3, 5, 7), CreateTripCount(10, true, true)));
476 ExpectEqual(Value(-7), GetMin(CreatePolynomial(11, 13, -7),
477 CreateTripCount(5, true, true)));
478 ExpectEqual(Value(111), GetMax(CreatePolynomial(11, 13, -7),
479 CreateTripCount(5, true, true)));
480 ExpectEqual(Value(-7), GetMin(CreatePolynomial(11, 13, -7),
481 CreateTripCount(10, true, true)));
482 ExpectEqual(Value(506), GetMax(CreatePolynomial(11, 13, -7),
483 CreateTripCount(10, true, true)));
484 ExpectEqual(Value(), GetMin(CreatePolynomial(-3, 5, 7), CreateTripCount(10, true, true)));
485 ExpectEqual(Value(), GetMax(CreatePolynomial(-3, 5, 7), CreateTripCount(10, true, true)));
486 ExpectEqual(Value(), GetMin(CreatePolynomial(3, -5, 7), CreateTripCount(10, true, true)));
487 ExpectEqual(Value(), GetMax(CreatePolynomial(3, -5, 7), CreateTripCount(10, true, true)));
488}
489
Aart Bikc071a012016-12-01 10:22:31 -0800490TEST_F(InductionVarRangeTest, GetMinMaxGeometricMul) {
491 ExpectEqual(Value(), GetMin(CreateGeometric(1, 1, 1, '*'), nullptr));
492 ExpectEqual(Value(), GetMax(CreateGeometric(1, 1, 1, '*'), nullptr));
493}
494
495TEST_F(InductionVarRangeTest, GetMinMaxGeometricDiv) {
496 ExpectEqual(Value(5), GetMin(CreateGeometric(11, 5, 3, '/'), nullptr));
497 ExpectEqual(Value(16), GetMax(CreateGeometric(11, 5, 3, '/'), nullptr));
498 ExpectEqual(Value(-5), GetMin(CreateGeometric(11, -5, 3, '/'), nullptr));
499 ExpectEqual(Value(6), GetMax(CreateGeometric(11, -5, 3, '/'), nullptr));
500 ExpectEqual(Value(-6), GetMin(CreateGeometric(-11, 5, 3, '/'), nullptr));
501 ExpectEqual(Value(5), GetMax(CreateGeometric(-11, 5, 3, '/'), nullptr));
502 ExpectEqual(Value(-16), GetMin(CreateGeometric(-11, -5, 3, '/'), nullptr));
503 ExpectEqual(Value(-5), GetMax(CreateGeometric(-11, -5, 3, '/'), nullptr));
504}
505
Aart Bikd14c5952015-09-08 15:25:15 -0700506TEST_F(InductionVarRangeTest, GetMinMaxPeriodic) {
507 ExpectEqual(Value(-2), GetMin(CreateRange(-2, 99), nullptr));
508 ExpectEqual(Value(99), GetMax(CreateRange(-2, 99), nullptr));
509}
510
511TEST_F(InductionVarRangeTest, GetMulMin) {
Aart Bik97412c92016-02-19 20:14:38 -0800512 ExpectEqual(Value(-14), GetMul(CreateConst(2), CreateRange(-7, 8), true));
513 ExpectEqual(Value(-16), GetMul(CreateConst(-2), CreateRange(-7, 8), true));
514 ExpectEqual(Value(-14), GetMul(CreateRange(-7, 8), CreateConst(2), true));
515 ExpectEqual(Value(-16), GetMul(CreateRange(-7, 8), CreateConst(-2), true));
Aart Bikb3365e02015-09-21 14:45:05 -0700516 ExpectEqual(Value(6), GetMul(CreateRange(2, 10), CreateRange(3, 5), true));
517 ExpectEqual(Value(-50), GetMul(CreateRange(2, 10), CreateRange(-5, -3), true));
Aart Bik7d57d7f2015-12-09 14:39:48 -0800518 ExpectEqual(Value(), GetMul(CreateRange(2, 10), CreateRange(-1, 1), true));
Aart Bikb3365e02015-09-21 14:45:05 -0700519 ExpectEqual(Value(-50), GetMul(CreateRange(-10, -2), CreateRange(3, 5), true));
520 ExpectEqual(Value(6), GetMul(CreateRange(-10, -2), CreateRange(-5, -3), true));
Aart Bik7d57d7f2015-12-09 14:39:48 -0800521 ExpectEqual(Value(), GetMul(CreateRange(-10, -2), CreateRange(-1, 1), true));
522 ExpectEqual(Value(), GetMul(CreateRange(-1, 1), CreateRange(2, 10), true));
523 ExpectEqual(Value(), GetMul(CreateRange(-1, 1), CreateRange(-10, -2), true));
524 ExpectEqual(Value(), GetMul(CreateRange(-1, 1), CreateRange(-1, 1), true));
Aart Bikd14c5952015-09-08 15:25:15 -0700525}
526
527TEST_F(InductionVarRangeTest, GetMulMax) {
Aart Bik97412c92016-02-19 20:14:38 -0800528 ExpectEqual(Value(16), GetMul(CreateConst(2), CreateRange(-7, 8), false));
529 ExpectEqual(Value(14), GetMul(CreateConst(-2), CreateRange(-7, 8), false));
530 ExpectEqual(Value(16), GetMul(CreateRange(-7, 8), CreateConst(2), false));
531 ExpectEqual(Value(14), GetMul(CreateRange(-7, 8), CreateConst(-2), false));
Aart Bikb3365e02015-09-21 14:45:05 -0700532 ExpectEqual(Value(50), GetMul(CreateRange(2, 10), CreateRange(3, 5), false));
533 ExpectEqual(Value(-6), GetMul(CreateRange(2, 10), CreateRange(-5, -3), false));
Aart Bik7d57d7f2015-12-09 14:39:48 -0800534 ExpectEqual(Value(), GetMul(CreateRange(2, 10), CreateRange(-1, 1), false));
Aart Bikb3365e02015-09-21 14:45:05 -0700535 ExpectEqual(Value(-6), GetMul(CreateRange(-10, -2), CreateRange(3, 5), false));
536 ExpectEqual(Value(50), GetMul(CreateRange(-10, -2), CreateRange(-5, -3), false));
Aart Bik7d57d7f2015-12-09 14:39:48 -0800537 ExpectEqual(Value(), GetMul(CreateRange(-10, -2), CreateRange(-1, 1), false));
538 ExpectEqual(Value(), GetMul(CreateRange(-1, 1), CreateRange(2, 10), false));
539 ExpectEqual(Value(), GetMul(CreateRange(-1, 1), CreateRange(-10, -2), false));
540 ExpectEqual(Value(), GetMul(CreateRange(-1, 1), CreateRange(-1, 1), false));
Aart Bikd14c5952015-09-08 15:25:15 -0700541}
542
543TEST_F(InductionVarRangeTest, GetDivMin) {
Aart Bik97412c92016-02-19 20:14:38 -0800544 ExpectEqual(Value(-5), GetDiv(CreateRange(-10, 20), CreateConst(2), true));
545 ExpectEqual(Value(-10), GetDiv(CreateRange(-10, 20), CreateConst(-2), true));
Aart Bikb3365e02015-09-21 14:45:05 -0700546 ExpectEqual(Value(10), GetDiv(CreateRange(40, 1000), CreateRange(2, 4), true));
547 ExpectEqual(Value(-500), GetDiv(CreateRange(40, 1000), CreateRange(-4, -2), true));
Aart Bik7d57d7f2015-12-09 14:39:48 -0800548 ExpectEqual(Value(), GetDiv(CreateRange(40, 1000), CreateRange(-1, 1), true));
Aart Bikb3365e02015-09-21 14:45:05 -0700549 ExpectEqual(Value(-500), GetDiv(CreateRange(-1000, -40), CreateRange(2, 4), true));
550 ExpectEqual(Value(10), GetDiv(CreateRange(-1000, -40), CreateRange(-4, -2), true));
Aart Bik7d57d7f2015-12-09 14:39:48 -0800551 ExpectEqual(Value(), GetDiv(CreateRange(-1000, -40), CreateRange(-1, 1), true));
552 ExpectEqual(Value(), GetDiv(CreateRange(-1, 1), CreateRange(40, 1000), true));
553 ExpectEqual(Value(), GetDiv(CreateRange(-1, 1), CreateRange(-1000, -40), true));
554 ExpectEqual(Value(), GetDiv(CreateRange(-1, 1), CreateRange(-1, 1), true));
Aart Bikd14c5952015-09-08 15:25:15 -0700555}
556
557TEST_F(InductionVarRangeTest, GetDivMax) {
Aart Bik97412c92016-02-19 20:14:38 -0800558 ExpectEqual(Value(10), GetDiv(CreateRange(-10, 20), CreateConst(2), false));
559 ExpectEqual(Value(5), GetDiv(CreateRange(-10, 20), CreateConst(-2), false));
Aart Bikb3365e02015-09-21 14:45:05 -0700560 ExpectEqual(Value(500), GetDiv(CreateRange(40, 1000), CreateRange(2, 4), false));
561 ExpectEqual(Value(-10), GetDiv(CreateRange(40, 1000), CreateRange(-4, -2), false));
Aart Bik7d57d7f2015-12-09 14:39:48 -0800562 ExpectEqual(Value(), GetDiv(CreateRange(40, 1000), CreateRange(-1, 1), false));
Aart Bikb3365e02015-09-21 14:45:05 -0700563 ExpectEqual(Value(-10), GetDiv(CreateRange(-1000, -40), CreateRange(2, 4), false));
564 ExpectEqual(Value(500), GetDiv(CreateRange(-1000, -40), CreateRange(-4, -2), false));
Aart Bik7d57d7f2015-12-09 14:39:48 -0800565 ExpectEqual(Value(), GetDiv(CreateRange(-1000, -40), CreateRange(-1, 1), false));
566 ExpectEqual(Value(), GetDiv(CreateRange(-1, 1), CreateRange(40, 1000), false));
567 ExpectEqual(Value(), GetDiv(CreateRange(-1, 1), CreateRange(-1000, 40), false));
568 ExpectEqual(Value(), GetDiv(CreateRange(-1, 1), CreateRange(-1, 1), false));
Aart Bikd14c5952015-09-08 15:25:15 -0700569}
570
Aart Bikdf7822e2016-12-06 10:05:30 -0800571TEST_F(InductionVarRangeTest, GetMinMaxRem) {
572 ExpectEqual(Value(), GetMin(CreateInvariant('%', CreateConst(2), CreateRange(10, 20)), nullptr));
573 ExpectEqual(Value(), GetMax(CreateInvariant('%', CreateConst(2), CreateRange(10, 20)), nullptr));
574 ExpectEqual(Value(), GetMin(CreateInvariant('%', CreateRange(10, 20), CreateConst(2)), nullptr));
575 ExpectEqual(Value(), GetMax(CreateInvariant('%', CreateRange(10, 20), CreateConst(2)), nullptr));
576 ExpectEqual(Value(2), GetMin(CreateInvariant('%', CreateConst(2), CreateConst(5)), nullptr));
577 ExpectEqual(Value(2), GetMax(CreateInvariant('%', CreateConst(2), CreateConst(5)), nullptr));
578 ExpectEqual(Value(1), GetMin(CreateInvariant('%', CreateConst(11), CreateConst(5)), nullptr));
579 ExpectEqual(Value(1), GetMax(CreateInvariant('%', CreateConst(11), CreateConst(5)), nullptr));
580}
581
582TEST_F(InductionVarRangeTest, GetRem) {
583 ExpectEqual(Value(0), GetRem(CreateConst(1), CreateConst(1)));
584 ExpectEqual(Value(2), GetRem(CreateConst(2), CreateConst(5)));
585 ExpectEqual(Value(1), GetRem(CreateConst(11), CreateConst(5)));
586 ExpectEqual(Value(-2), GetRem(CreateConst(-2), CreateConst(5)));
587 ExpectEqual(Value(-1), GetRem(CreateConst(-11), CreateConst(5)));
588 ExpectEqual(Value(2), GetRem(CreateConst(2), CreateConst(-5)));
589 ExpectEqual(Value(1), GetRem(CreateConst(11), CreateConst(-5)));
590 ExpectEqual(Value(-2), GetRem(CreateConst(-2), CreateConst(-5)));
591 ExpectEqual(Value(-1), GetRem(CreateConst(-11), CreateConst(-5)));
592 ExpectEqual(Value(), GetRem(CreateConst(1), CreateConst(0)));
593}
594
595TEST_F(InductionVarRangeTest, GetMinMaxXor) {
596 ExpectEqual(Value(), GetMin(CreateInvariant('^', CreateConst(2), CreateRange(10, 20)), nullptr));
597 ExpectEqual(Value(), GetMax(CreateInvariant('^', CreateConst(2), CreateRange(10, 20)), nullptr));
598 ExpectEqual(Value(), GetMin(CreateInvariant('^', CreateRange(10, 20), CreateConst(2)), nullptr));
599 ExpectEqual(Value(), GetMax(CreateInvariant('^', CreateRange(10, 20), CreateConst(2)), nullptr));
600 ExpectEqual(Value(3), GetMin(CreateInvariant('^', CreateConst(1), CreateConst(2)), nullptr));
601 ExpectEqual(Value(3), GetMax(CreateInvariant('^', CreateConst(1), CreateConst(2)), nullptr));
602}
603
604TEST_F(InductionVarRangeTest, GetXor) {
605 ExpectEqual(Value(0), GetXor(CreateConst(1), CreateConst(1)));
606 ExpectEqual(Value(3), GetXor(CreateConst(1), CreateConst(2)));
607 ExpectEqual(Value(-2), GetXor(CreateConst(1), CreateConst(-1)));
608 ExpectEqual(Value(0), GetXor(CreateConst(-1), CreateConst(-1)));
609}
610
Aart Bikd14c5952015-09-08 15:25:15 -0700611TEST_F(InductionVarRangeTest, AddValue) {
612 ExpectEqual(Value(110), AddValue(Value(10), Value(100)));
Aart Bik7d57d7f2015-12-09 14:39:48 -0800613 ExpectEqual(Value(-5), AddValue(Value(x_, 1, -4), Value(x_, -1, -1)));
614 ExpectEqual(Value(x_, 3, -5), AddValue(Value(x_, 2, -4), Value(x_, 1, -1)));
615 ExpectEqual(Value(), AddValue(Value(x_, 1, 5), Value(y_, 1, -7)));
616 ExpectEqual(Value(x_, 1, 23), AddValue(Value(x_, 1, 20), Value(3)));
617 ExpectEqual(Value(y_, 1, 5), AddValue(Value(55), Value(y_, 1, -50)));
Aart Bikcd26feb2015-09-23 17:50:50 -0700618 const int32_t max_value = std::numeric_limits<int32_t>::max();
619 ExpectEqual(Value(max_value), AddValue(Value(max_value - 5), Value(5)));
620 ExpectEqual(Value(), AddValue(Value(max_value - 5), Value(6))); // unsafe
Aart Bikd14c5952015-09-08 15:25:15 -0700621}
622
623TEST_F(InductionVarRangeTest, SubValue) {
624 ExpectEqual(Value(-90), SubValue(Value(10), Value(100)));
Aart Bik7d57d7f2015-12-09 14:39:48 -0800625 ExpectEqual(Value(-3), SubValue(Value(x_, 1, -4), Value(x_, 1, -1)));
626 ExpectEqual(Value(x_, 2, -3), SubValue(Value(x_, 3, -4), Value(x_, 1, -1)));
627 ExpectEqual(Value(), SubValue(Value(x_, 1, 5), Value(y_, 1, -7)));
628 ExpectEqual(Value(x_, 1, 17), SubValue(Value(x_, 1, 20), Value(3)));
629 ExpectEqual(Value(y_, -4, 105), SubValue(Value(55), Value(y_, 4, -50)));
Aart Bikcd26feb2015-09-23 17:50:50 -0700630 const int32_t min_value = std::numeric_limits<int32_t>::min();
631 ExpectEqual(Value(min_value), SubValue(Value(min_value + 5), Value(5)));
632 ExpectEqual(Value(), SubValue(Value(min_value + 5), Value(6))); // unsafe
Aart Bikd14c5952015-09-08 15:25:15 -0700633}
634
635TEST_F(InductionVarRangeTest, MulValue) {
636 ExpectEqual(Value(1000), MulValue(Value(10), Value(100)));
Aart Bik7d57d7f2015-12-09 14:39:48 -0800637 ExpectEqual(Value(), MulValue(Value(x_, 1, -4), Value(x_, 1, -1)));
638 ExpectEqual(Value(), MulValue(Value(x_, 1, 5), Value(y_, 1, -7)));
639 ExpectEqual(Value(x_, 9, 60), MulValue(Value(x_, 3, 20), Value(3)));
640 ExpectEqual(Value(y_, 55, -110), MulValue(Value(55), Value(y_, 1, -2)));
Aart Bikb3365e02015-09-21 14:45:05 -0700641 ExpectEqual(Value(), MulValue(Value(90000), Value(-90000))); // unsafe
Aart Bikd14c5952015-09-08 15:25:15 -0700642}
643
Aart Bik97412c92016-02-19 20:14:38 -0800644TEST_F(InductionVarRangeTest, MulValueSpecial) {
645 const int32_t min_value = std::numeric_limits<int32_t>::min();
646 const int32_t max_value = std::numeric_limits<int32_t>::max();
647
648 // Unsafe.
649 ExpectEqual(Value(), MulValue(Value(min_value), Value(min_value)));
650 ExpectEqual(Value(), MulValue(Value(min_value), Value(-1)));
651 ExpectEqual(Value(), MulValue(Value(min_value), Value(max_value)));
652 ExpectEqual(Value(), MulValue(Value(max_value), Value(max_value)));
653
654 // Safe.
655 ExpectEqual(Value(min_value), MulValue(Value(min_value), Value(1)));
656 ExpectEqual(Value(max_value), MulValue(Value(max_value), Value(1)));
657 ExpectEqual(Value(-max_value), MulValue(Value(max_value), Value(-1)));
658 ExpectEqual(Value(-1), MulValue(Value(1), Value(-1)));
659 ExpectEqual(Value(1), MulValue(Value(-1), Value(-1)));
660}
661
Aart Bikd14c5952015-09-08 15:25:15 -0700662TEST_F(InductionVarRangeTest, DivValue) {
663 ExpectEqual(Value(25), DivValue(Value(100), Value(4)));
Aart Bik7d57d7f2015-12-09 14:39:48 -0800664 ExpectEqual(Value(), DivValue(Value(x_, 1, -4), Value(x_, 1, -1)));
665 ExpectEqual(Value(), DivValue(Value(x_, 1, 5), Value(y_, 1, -7)));
666 ExpectEqual(Value(), DivValue(Value(x_, 12, 24), Value(3)));
667 ExpectEqual(Value(), DivValue(Value(55), Value(y_, 1, -50)));
Aart Bikb3365e02015-09-21 14:45:05 -0700668 ExpectEqual(Value(), DivValue(Value(1), Value(0))); // unsafe
Aart Bikd14c5952015-09-08 15:25:15 -0700669}
670
Aart Bik97412c92016-02-19 20:14:38 -0800671TEST_F(InductionVarRangeTest, DivValueSpecial) {
672 const int32_t min_value = std::numeric_limits<int32_t>::min();
673 const int32_t max_value = std::numeric_limits<int32_t>::max();
674
675 // Unsafe.
676 ExpectEqual(Value(), DivValue(Value(min_value), Value(-1)));
677
678 // Safe.
679 ExpectEqual(Value(1), DivValue(Value(min_value), Value(min_value)));
680 ExpectEqual(Value(1), DivValue(Value(max_value), Value(max_value)));
681 ExpectEqual(Value(min_value), DivValue(Value(min_value), Value(1)));
682 ExpectEqual(Value(max_value), DivValue(Value(max_value), Value(1)));
683 ExpectEqual(Value(-max_value), DivValue(Value(max_value), Value(-1)));
684 ExpectEqual(Value(-1), DivValue(Value(1), Value(-1)));
685 ExpectEqual(Value(1), DivValue(Value(-1), Value(-1)));
686}
687
Aart Bikd14c5952015-09-08 15:25:15 -0700688TEST_F(InductionVarRangeTest, MinValue) {
689 ExpectEqual(Value(10), MinValue(Value(10), Value(100)));
Aart Bik7d57d7f2015-12-09 14:39:48 -0800690 ExpectEqual(Value(x_, 1, -4), MinValue(Value(x_, 1, -4), Value(x_, 1, -1)));
691 ExpectEqual(Value(x_, 4, -4), MinValue(Value(x_, 4, -4), Value(x_, 4, -1)));
692 ExpectEqual(Value(), MinValue(Value(x_, 1, 5), Value(y_, 1, -7)));
693 ExpectEqual(Value(), MinValue(Value(x_, 1, 20), Value(3)));
694 ExpectEqual(Value(), MinValue(Value(55), Value(y_, 1, -50)));
Aart Bikd14c5952015-09-08 15:25:15 -0700695}
696
697TEST_F(InductionVarRangeTest, MaxValue) {
698 ExpectEqual(Value(100), MaxValue(Value(10), Value(100)));
Aart Bik7d57d7f2015-12-09 14:39:48 -0800699 ExpectEqual(Value(x_, 1, -1), MaxValue(Value(x_, 1, -4), Value(x_, 1, -1)));
700 ExpectEqual(Value(x_, 4, -1), MaxValue(Value(x_, 4, -4), Value(x_, 4, -1)));
701 ExpectEqual(Value(), MaxValue(Value(x_, 1, 5), Value(y_, 1, -7)));
702 ExpectEqual(Value(), MaxValue(Value(x_, 1, 20), Value(3)));
703 ExpectEqual(Value(), MaxValue(Value(55), Value(y_, 1, -50)));
Aart Bikd14c5952015-09-08 15:25:15 -0700704}
705
Aart Bik52be7e72016-06-23 11:20:41 -0700706TEST_F(InductionVarRangeTest, ArrayLengthAndHints) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +0000707 // We pass a bogus constant for the class to avoid mocking one.
708 HInstruction* new_array = new (&allocator_) HNewArray(x_, x_, 0);
Aart Bik52be7e72016-06-23 11:20:41 -0700709 entry_block_->AddInstruction(new_array);
710 HInstruction* array_length = new (&allocator_) HArrayLength(new_array, 0);
711 entry_block_->AddInstruction(array_length);
712 // With null hint: yields extreme constants.
713 const int32_t max_value = std::numeric_limits<int32_t>::max();
714 SetHint(nullptr);
715 ExpectEqual(Value(0), GetMin(CreateFetch(array_length), nullptr));
716 ExpectEqual(Value(max_value), GetMax(CreateFetch(array_length), nullptr));
717 // With explicit hint: yields the length instruction.
718 SetHint(array_length);
719 ExpectEqual(Value(array_length, 1, 0), GetMin(CreateFetch(array_length), nullptr));
720 ExpectEqual(Value(array_length, 1, 0), GetMax(CreateFetch(array_length), nullptr));
721 // With any non-null hint: chases beyond the length instruction.
722 SetHint(x_);
723 ExpectEqual(Value(x_, 1, 0), GetMin(CreateFetch(array_length), nullptr));
724 ExpectEqual(Value(x_, 1, 0), GetMax(CreateFetch(array_length), nullptr));
725}
726
Aart Bik8e9090b2017-09-08 16:46:50 -0700727TEST_F(InductionVarRangeTest, AddOrSubAndConstant) {
728 HInstruction* add = new (&allocator_)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100729 HAdd(DataType::Type::kInt32, x_, graph_->GetIntConstant(-1));
Aart Bik8e9090b2017-09-08 16:46:50 -0700730 HInstruction* alt = new (&allocator_)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100731 HAdd(DataType::Type::kInt32, graph_->GetIntConstant(-1), x_);
Aart Bik8e9090b2017-09-08 16:46:50 -0700732 HInstruction* sub = new (&allocator_)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100733 HSub(DataType::Type::kInt32, x_, graph_->GetIntConstant(1));
Aart Bik8e9090b2017-09-08 16:46:50 -0700734 HInstruction* rev = new (&allocator_)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100735 HSub(DataType::Type::kInt32, graph_->GetIntConstant(1), x_);
Aart Bik8e9090b2017-09-08 16:46:50 -0700736 entry_block_->AddInstruction(add);
737 entry_block_->AddInstruction(alt);
738 entry_block_->AddInstruction(sub);
739 entry_block_->AddInstruction(rev);
740 ExpectEqual(Value(x_, 1, -1), GetMin(CreateFetch(add), nullptr));
741 ExpectEqual(Value(x_, 1, -1), GetMax(CreateFetch(add), nullptr));
742 ExpectEqual(Value(x_, 1, -1), GetMin(CreateFetch(alt), nullptr));
743 ExpectEqual(Value(x_, 1, -1), GetMax(CreateFetch(alt), nullptr));
744 ExpectEqual(Value(x_, 1, -1), GetMin(CreateFetch(sub), nullptr));
745 ExpectEqual(Value(x_, 1, -1), GetMax(CreateFetch(sub), nullptr));
746 ExpectEqual(Value(x_, -1, 1), GetMin(CreateFetch(rev), nullptr));
747 ExpectEqual(Value(x_, -1, 1), GetMax(CreateFetch(rev), nullptr));
748}
749
Aart Bikaec3cce2015-10-14 17:44:55 -0700750//
Aart Bik7d57d7f2015-12-09 14:39:48 -0800751// Tests on public methods.
Aart Bikaec3cce2015-10-14 17:44:55 -0700752//
753
Aart Bik389b3db2015-10-28 14:23:40 -0700754TEST_F(InductionVarRangeTest, ConstantTripCountUp) {
755 BuildLoop(0, graph_->GetIntConstant(1000), 1);
Aart Bikaec3cce2015-10-14 17:44:55 -0700756 PerformInductionVarAnalysis();
Aart Bikaec3cce2015-10-14 17:44:55 -0700757
Aart Bik389b3db2015-10-28 14:23:40 -0700758 Value v1, v2;
759 bool needs_finite_test = true;
Aart Bik16d3a652016-09-09 10:33:50 -0700760 bool needs_taken_test = true;
761
762 HInstruction* phi = condition_->InputAt(0);
763 HInstruction* exit = exit_block_->GetLastInstruction();
Aart Bik389b3db2015-10-28 14:23:40 -0700764
Aart Bikaec3cce2015-10-14 17:44:55 -0700765 // In context of header: known.
Aart Bik16d3a652016-09-09 10:33:50 -0700766 range_.GetInductionRange(condition_, phi, x_, &v1, &v2, &needs_finite_test);
Aart Bik389b3db2015-10-28 14:23:40 -0700767 EXPECT_FALSE(needs_finite_test);
768 ExpectEqual(Value(0), v1);
769 ExpectEqual(Value(1000), v2);
Aart Bikaec3cce2015-10-14 17:44:55 -0700770
771 // In context of loop-body: known.
Aart Bik16d3a652016-09-09 10:33:50 -0700772 range_.GetInductionRange(increment_, phi, x_, &v1, &v2, &needs_finite_test);
Aart Bik389b3db2015-10-28 14:23:40 -0700773 EXPECT_FALSE(needs_finite_test);
774 ExpectEqual(Value(0), v1);
775 ExpectEqual(Value(999), v2);
Aart Bik52be7e72016-06-23 11:20:41 -0700776 range_.GetInductionRange(increment_, increment_, x_, &v1, &v2, &needs_finite_test);
Aart Bik389b3db2015-10-28 14:23:40 -0700777 EXPECT_FALSE(needs_finite_test);
778 ExpectEqual(Value(1), v1);
779 ExpectEqual(Value(1000), v2);
Aart Bik16d3a652016-09-09 10:33:50 -0700780
781 // Induction vs. no-induction.
782 EXPECT_TRUE(range_.CanGenerateRange(increment_, phi, &needs_finite_test, &needs_taken_test));
783 EXPECT_TRUE(range_.CanGenerateLastValue(phi));
784 EXPECT_FALSE(range_.CanGenerateRange(exit, exit, &needs_finite_test, &needs_taken_test));
785 EXPECT_FALSE(range_.CanGenerateLastValue(exit));
786
787 // Last value (unsimplified).
788 HInstruction* last = range_.GenerateLastValue(phi, graph_, loop_preheader_);
789 ASSERT_TRUE(last->IsAdd());
Aart Bik8e02e3e2017-02-28 14:41:55 -0800790 ExpectInt(1000, last->InputAt(0));
791 ExpectInt(0, last->InputAt(1));
792
793 // Loop logic.
794 int64_t tc = 0;
795 EXPECT_TRUE(range_.IsFinite(loop_header_->GetLoopInformation(), &tc));
796 EXPECT_EQ(1000, tc);
797 HInstruction* offset = nullptr;
Aart Bik37dc4df2017-06-28 14:08:00 -0700798 EXPECT_TRUE(range_.IsUnitStride(phi, phi, graph_, &offset));
799 ExpectInt(0, offset);
Aart Bik8e02e3e2017-02-28 14:41:55 -0800800 HInstruction* tce = range_.GenerateTripCount(
801 loop_header_->GetLoopInformation(), graph_, loop_preheader_);
802 ASSERT_TRUE(tce != nullptr);
803 ExpectInt(1000, tce);
Aart Bikaec3cce2015-10-14 17:44:55 -0700804}
805
Aart Bik389b3db2015-10-28 14:23:40 -0700806TEST_F(InductionVarRangeTest, ConstantTripCountDown) {
807 BuildLoop(1000, graph_->GetIntConstant(0), -1);
Aart Bikaec3cce2015-10-14 17:44:55 -0700808 PerformInductionVarAnalysis();
Aart Bikaec3cce2015-10-14 17:44:55 -0700809
Aart Bik389b3db2015-10-28 14:23:40 -0700810 Value v1, v2;
811 bool needs_finite_test = true;
Aart Bik16d3a652016-09-09 10:33:50 -0700812 bool needs_taken_test = true;
813
814 HInstruction* phi = condition_->InputAt(0);
815 HInstruction* exit = exit_block_->GetLastInstruction();
Aart Bik389b3db2015-10-28 14:23:40 -0700816
817 // In context of header: known.
Aart Bik16d3a652016-09-09 10:33:50 -0700818 range_.GetInductionRange(condition_, phi, x_, &v1, &v2, &needs_finite_test);
Aart Bik389b3db2015-10-28 14:23:40 -0700819 EXPECT_FALSE(needs_finite_test);
820 ExpectEqual(Value(0), v1);
821 ExpectEqual(Value(1000), v2);
Aart Bikaec3cce2015-10-14 17:44:55 -0700822
823 // In context of loop-body: known.
Aart Bik16d3a652016-09-09 10:33:50 -0700824 range_.GetInductionRange(increment_, phi, x_, &v1, &v2, &needs_finite_test);
Aart Bik389b3db2015-10-28 14:23:40 -0700825 EXPECT_FALSE(needs_finite_test);
826 ExpectEqual(Value(1), v1);
827 ExpectEqual(Value(1000), v2);
Aart Bik52be7e72016-06-23 11:20:41 -0700828 range_.GetInductionRange(increment_, increment_, x_, &v1, &v2, &needs_finite_test);
Aart Bik389b3db2015-10-28 14:23:40 -0700829 EXPECT_FALSE(needs_finite_test);
830 ExpectEqual(Value(0), v1);
831 ExpectEqual(Value(999), v2);
Aart Bik16d3a652016-09-09 10:33:50 -0700832
833 // Induction vs. no-induction.
834 EXPECT_TRUE(range_.CanGenerateRange(increment_, phi, &needs_finite_test, &needs_taken_test));
835 EXPECT_TRUE(range_.CanGenerateLastValue(phi));
836 EXPECT_FALSE(range_.CanGenerateRange(exit, exit, &needs_finite_test, &needs_taken_test));
837 EXPECT_FALSE(range_.CanGenerateLastValue(exit));
838
839 // Last value (unsimplified).
840 HInstruction* last = range_.GenerateLastValue(phi, graph_, loop_preheader_);
841 ASSERT_TRUE(last->IsSub());
Aart Bik8e02e3e2017-02-28 14:41:55 -0800842 ExpectInt(1000, last->InputAt(0));
Aart Bik16d3a652016-09-09 10:33:50 -0700843 ASSERT_TRUE(last->InputAt(1)->IsNeg());
844 last = last->InputAt(1)->InputAt(0);
845 ASSERT_TRUE(last->IsSub());
Aart Bik8e02e3e2017-02-28 14:41:55 -0800846 ExpectInt(0, last->InputAt(0));
847 ExpectInt(1000, last->InputAt(1));
848
849 // Loop logic.
850 int64_t tc = 0;
851 EXPECT_TRUE(range_.IsFinite(loop_header_->GetLoopInformation(), &tc));
852 EXPECT_EQ(1000, tc);
853 HInstruction* offset = nullptr;
Aart Bik37dc4df2017-06-28 14:08:00 -0700854 EXPECT_FALSE(range_.IsUnitStride(phi, phi, graph_, &offset));
Aart Bik8e02e3e2017-02-28 14:41:55 -0800855 HInstruction* tce = range_.GenerateTripCount(
856 loop_header_->GetLoopInformation(), graph_, loop_preheader_);
857 ASSERT_TRUE(tce != nullptr);
858 ASSERT_TRUE(tce->IsNeg());
859 last = tce->InputAt(0);
860 EXPECT_TRUE(last->IsSub());
861 ExpectInt(0, last->InputAt(0));
862 ExpectInt(1000, last->InputAt(1));
Aart Bikaec3cce2015-10-14 17:44:55 -0700863}
864
Aart Bik389b3db2015-10-28 14:23:40 -0700865TEST_F(InductionVarRangeTest, SymbolicTripCountUp) {
Aart Bik7d57d7f2015-12-09 14:39:48 -0800866 BuildLoop(0, x_, 1);
Aart Bikaec3cce2015-10-14 17:44:55 -0700867 PerformInductionVarAnalysis();
Aart Bikaec3cce2015-10-14 17:44:55 -0700868
Aart Bik389b3db2015-10-28 14:23:40 -0700869 Value v1, v2;
870 bool needs_finite_test = true;
871 bool needs_taken_test = true;
872
Aart Bik16d3a652016-09-09 10:33:50 -0700873 HInstruction* phi = condition_->InputAt(0);
874
Aart Bik389b3db2015-10-28 14:23:40 -0700875 // In context of header: upper unknown.
Aart Bik16d3a652016-09-09 10:33:50 -0700876 range_.GetInductionRange(condition_, phi, x_, &v1, &v2, &needs_finite_test);
Aart Bik389b3db2015-10-28 14:23:40 -0700877 EXPECT_FALSE(needs_finite_test);
878 ExpectEqual(Value(0), v1);
879 ExpectEqual(Value(), v2);
880
881 // In context of loop-body: known.
Aart Bik16d3a652016-09-09 10:33:50 -0700882 range_.GetInductionRange(increment_, phi, x_, &v1, &v2, &needs_finite_test);
Aart Bik389b3db2015-10-28 14:23:40 -0700883 EXPECT_FALSE(needs_finite_test);
884 ExpectEqual(Value(0), v1);
Aart Bik7d57d7f2015-12-09 14:39:48 -0800885 ExpectEqual(Value(x_, 1, -1), v2);
Aart Bik52be7e72016-06-23 11:20:41 -0700886 range_.GetInductionRange(increment_, increment_, x_, &v1, &v2, &needs_finite_test);
Aart Bik389b3db2015-10-28 14:23:40 -0700887 EXPECT_FALSE(needs_finite_test);
888 ExpectEqual(Value(1), v1);
Aart Bik7d57d7f2015-12-09 14:39:48 -0800889 ExpectEqual(Value(x_, 1, 0), v2);
Aart Bik389b3db2015-10-28 14:23:40 -0700890
Aart Bikaec3cce2015-10-14 17:44:55 -0700891 HInstruction* lower = nullptr;
892 HInstruction* upper = nullptr;
Aart Bikaec3cce2015-10-14 17:44:55 -0700893
894 // Can generate code in context of loop-body only.
Aart Bik16d3a652016-09-09 10:33:50 -0700895 EXPECT_FALSE(range_.CanGenerateRange(condition_, phi, &needs_finite_test, &needs_taken_test));
896 ASSERT_TRUE(range_.CanGenerateRange(increment_, phi, &needs_finite_test, &needs_taken_test));
Aart Bik389b3db2015-10-28 14:23:40 -0700897 EXPECT_FALSE(needs_finite_test);
898 EXPECT_TRUE(needs_taken_test);
Aart Bikaec3cce2015-10-14 17:44:55 -0700899
Aart Bik16d3a652016-09-09 10:33:50 -0700900 // Generates code (unsimplified).
901 range_.GenerateRange(increment_, phi, graph_, loop_preheader_, &lower, &upper);
Aart Bikaec3cce2015-10-14 17:44:55 -0700902
903 // Verify lower is 0+0.
904 ASSERT_TRUE(lower != nullptr);
905 ASSERT_TRUE(lower->IsAdd());
Aart Bik8e02e3e2017-02-28 14:41:55 -0800906 ExpectInt(0, lower->InputAt(0));
907 ExpectInt(0, lower->InputAt(1));
Aart Bikaec3cce2015-10-14 17:44:55 -0700908
Aart Bik389b3db2015-10-28 14:23:40 -0700909 // Verify upper is (V-1)+0.
Aart Bikaec3cce2015-10-14 17:44:55 -0700910 ASSERT_TRUE(upper != nullptr);
911 ASSERT_TRUE(upper->IsAdd());
912 ASSERT_TRUE(upper->InputAt(0)->IsSub());
913 EXPECT_TRUE(upper->InputAt(0)->InputAt(0)->IsParameterValue());
Aart Bik8e02e3e2017-02-28 14:41:55 -0800914 ExpectInt(1, upper->InputAt(0)->InputAt(1));
915 ExpectInt(0, upper->InputAt(1));
Aart Bik389b3db2015-10-28 14:23:40 -0700916
917 // Verify taken-test is 0<V.
Aart Bik16d3a652016-09-09 10:33:50 -0700918 HInstruction* taken = range_.GenerateTakenTest(increment_, graph_, loop_preheader_);
Aart Bik389b3db2015-10-28 14:23:40 -0700919 ASSERT_TRUE(taken != nullptr);
920 ASSERT_TRUE(taken->IsLessThan());
Aart Bik8e02e3e2017-02-28 14:41:55 -0800921 ExpectInt(0, taken->InputAt(0));
Aart Bik389b3db2015-10-28 14:23:40 -0700922 EXPECT_TRUE(taken->InputAt(1)->IsParameterValue());
Aart Bik16d3a652016-09-09 10:33:50 -0700923
924 // Replacement.
925 range_.Replace(loop_header_->GetLastInstruction(), x_, y_);
926 range_.GetInductionRange(increment_, increment_, x_, &v1, &v2, &needs_finite_test);
927 EXPECT_FALSE(needs_finite_test);
928 ExpectEqual(Value(1), v1);
929 ExpectEqual(Value(y_, 1, 0), v2);
Aart Bik8e02e3e2017-02-28 14:41:55 -0800930
931 // Loop logic.
932 int64_t tc = 0;
933 EXPECT_TRUE(range_.IsFinite(loop_header_->GetLoopInformation(), &tc));
934 EXPECT_EQ(0, tc); // unknown
935 HInstruction* offset = nullptr;
Aart Bik37dc4df2017-06-28 14:08:00 -0700936 EXPECT_TRUE(range_.IsUnitStride(phi, phi, graph_, &offset));
937 ExpectInt(0, offset);
Aart Bik8e02e3e2017-02-28 14:41:55 -0800938 HInstruction* tce = range_.GenerateTripCount(
939 loop_header_->GetLoopInformation(), graph_, loop_preheader_);
940 ASSERT_TRUE(tce != nullptr);
941 EXPECT_TRUE(tce->IsSelect()); // guarded by taken-test
942 ExpectInt(0, tce->InputAt(0));
943 EXPECT_TRUE(tce->InputAt(1)->IsParameterValue());
944 EXPECT_TRUE(tce->InputAt(2)->IsLessThan());
Aart Bik389b3db2015-10-28 14:23:40 -0700945}
946
947TEST_F(InductionVarRangeTest, SymbolicTripCountDown) {
Aart Bik7d57d7f2015-12-09 14:39:48 -0800948 BuildLoop(1000, x_, -1);
Aart Bik389b3db2015-10-28 14:23:40 -0700949 PerformInductionVarAnalysis();
Aart Bik389b3db2015-10-28 14:23:40 -0700950
951 Value v1, v2;
952 bool needs_finite_test = true;
953 bool needs_taken_test = true;
954
Aart Bik16d3a652016-09-09 10:33:50 -0700955 HInstruction* phi = condition_->InputAt(0);
956
Aart Bik389b3db2015-10-28 14:23:40 -0700957 // In context of header: lower unknown.
Aart Bik16d3a652016-09-09 10:33:50 -0700958 range_.GetInductionRange(condition_, phi, x_, &v1, &v2, &needs_finite_test);
Aart Bik389b3db2015-10-28 14:23:40 -0700959 EXPECT_FALSE(needs_finite_test);
960 ExpectEqual(Value(), v1);
961 ExpectEqual(Value(1000), v2);
962
963 // In context of loop-body: known.
Aart Bik16d3a652016-09-09 10:33:50 -0700964 range_.GetInductionRange(increment_, phi, x_, &v1, &v2, &needs_finite_test);
Aart Bik389b3db2015-10-28 14:23:40 -0700965 EXPECT_FALSE(needs_finite_test);
Aart Bik7d57d7f2015-12-09 14:39:48 -0800966 ExpectEqual(Value(x_, 1, 1), v1);
Aart Bik389b3db2015-10-28 14:23:40 -0700967 ExpectEqual(Value(1000), v2);
Aart Bik52be7e72016-06-23 11:20:41 -0700968 range_.GetInductionRange(increment_, increment_, x_, &v1, &v2, &needs_finite_test);
Aart Bik389b3db2015-10-28 14:23:40 -0700969 EXPECT_FALSE(needs_finite_test);
Aart Bik7d57d7f2015-12-09 14:39:48 -0800970 ExpectEqual(Value(x_, 1, 0), v1);
Aart Bik389b3db2015-10-28 14:23:40 -0700971 ExpectEqual(Value(999), v2);
972
973 HInstruction* lower = nullptr;
974 HInstruction* upper = nullptr;
Aart Bik389b3db2015-10-28 14:23:40 -0700975
976 // Can generate code in context of loop-body only.
Aart Bik16d3a652016-09-09 10:33:50 -0700977 EXPECT_FALSE(range_.CanGenerateRange(condition_, phi, &needs_finite_test, &needs_taken_test));
978 ASSERT_TRUE(range_.CanGenerateRange(increment_, phi, &needs_finite_test, &needs_taken_test));
Aart Bik389b3db2015-10-28 14:23:40 -0700979 EXPECT_FALSE(needs_finite_test);
980 EXPECT_TRUE(needs_taken_test);
981
Aart Bik16d3a652016-09-09 10:33:50 -0700982 // Generates code (unsimplified).
983 range_.GenerateRange(increment_, phi, graph_, loop_preheader_, &lower, &upper);
Aart Bik389b3db2015-10-28 14:23:40 -0700984
Aart Bik7d57d7f2015-12-09 14:39:48 -0800985 // Verify lower is 1000-((1000-V)-1).
Aart Bik389b3db2015-10-28 14:23:40 -0700986 ASSERT_TRUE(lower != nullptr);
987 ASSERT_TRUE(lower->IsSub());
Aart Bik8e02e3e2017-02-28 14:41:55 -0800988 ExpectInt(1000, lower->InputAt(0));
Aart Bik389b3db2015-10-28 14:23:40 -0700989 lower = lower->InputAt(1);
990 ASSERT_TRUE(lower->IsSub());
Aart Bik8e02e3e2017-02-28 14:41:55 -0800991 ExpectInt(1, lower->InputAt(1));
Aart Bik389b3db2015-10-28 14:23:40 -0700992 lower = lower->InputAt(0);
Aart Bik389b3db2015-10-28 14:23:40 -0700993 ASSERT_TRUE(lower->IsSub());
Aart Bik8e02e3e2017-02-28 14:41:55 -0800994 ExpectInt(1000, lower->InputAt(0));
Aart Bik7d57d7f2015-12-09 14:39:48 -0800995 EXPECT_TRUE(lower->InputAt(1)->IsParameterValue());
Aart Bik389b3db2015-10-28 14:23:40 -0700996
997 // Verify upper is 1000-0.
998 ASSERT_TRUE(upper != nullptr);
999 ASSERT_TRUE(upper->IsSub());
Aart Bik8e02e3e2017-02-28 14:41:55 -08001000 ExpectInt(1000, upper->InputAt(0));
1001 ExpectInt(0, upper->InputAt(1));
Aart Bik389b3db2015-10-28 14:23:40 -07001002
1003 // Verify taken-test is 1000>V.
Aart Bik16d3a652016-09-09 10:33:50 -07001004 HInstruction* taken = range_.GenerateTakenTest(increment_, graph_, loop_preheader_);
Aart Bik389b3db2015-10-28 14:23:40 -07001005 ASSERT_TRUE(taken != nullptr);
1006 ASSERT_TRUE(taken->IsGreaterThan());
Aart Bik8e02e3e2017-02-28 14:41:55 -08001007 ExpectInt(1000, taken->InputAt(0));
Aart Bik389b3db2015-10-28 14:23:40 -07001008 EXPECT_TRUE(taken->InputAt(1)->IsParameterValue());
Aart Bik16d3a652016-09-09 10:33:50 -07001009
1010 // Replacement.
1011 range_.Replace(loop_header_->GetLastInstruction(), x_, y_);
1012 range_.GetInductionRange(increment_, increment_, x_, &v1, &v2, &needs_finite_test);
1013 EXPECT_FALSE(needs_finite_test);
1014 ExpectEqual(Value(y_, 1, 0), v1);
1015 ExpectEqual(Value(999), v2);
Aart Bik8e02e3e2017-02-28 14:41:55 -08001016
1017 // Loop logic.
1018 int64_t tc = 0;
1019 EXPECT_TRUE(range_.IsFinite(loop_header_->GetLoopInformation(), &tc));
1020 EXPECT_EQ(0, tc); // unknown
1021 HInstruction* offset = nullptr;
Aart Bik37dc4df2017-06-28 14:08:00 -07001022 EXPECT_FALSE(range_.IsUnitStride(phi, phi, graph_, &offset));
Aart Bik8e02e3e2017-02-28 14:41:55 -08001023 HInstruction* tce = range_.GenerateTripCount(
1024 loop_header_->GetLoopInformation(), graph_, loop_preheader_);
1025 ASSERT_TRUE(tce != nullptr);
1026 EXPECT_TRUE(tce->IsSelect()); // guarded by taken-test
1027 ExpectInt(0, tce->InputAt(0));
1028 EXPECT_TRUE(tce->InputAt(1)->IsSub());
1029 EXPECT_TRUE(tce->InputAt(2)->IsGreaterThan());
1030 tce = tce->InputAt(1);
1031 ExpectInt(1000, taken->InputAt(0));
1032 EXPECT_TRUE(taken->InputAt(1)->IsParameterValue());
Aart Bikaec3cce2015-10-14 17:44:55 -07001033}
1034
Aart Bikd14c5952015-09-08 15:25:15 -07001035} // namespace art