blob: 89e4690de20bc3a3b1c2bbb7958b156de882fe62 [file] [log] [blame]
Aart Bik30efb4e2015-07-30 12:14:31 -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
17#include <regex>
18
19#include "base/arena_allocator.h"
20#include "builder.h"
Aart Bik30efb4e2015-07-30 12:14:31 -070021#include "induction_var_analysis.h"
22#include "nodes.h"
23#include "optimizing_unit_test.h"
24
25namespace art {
26
27/**
28 * Fixture class for the InductionVarAnalysis tests.
29 */
David Brazdil4833f5a2015-12-16 10:37:39 +000030class InductionVarAnalysisTest : public CommonCompilerTest {
Aart Bik30efb4e2015-07-30 12:14:31 -070031 public:
32 InductionVarAnalysisTest() : pool_(), allocator_(&pool_) {
33 graph_ = CreateGraph(&allocator_);
34 }
35
36 ~InductionVarAnalysisTest() { }
37
38 // Builds single for-loop at depth d.
39 void BuildForLoop(int d, int n) {
40 ASSERT_LT(d, n);
41 loop_preheader_[d] = new (&allocator_) HBasicBlock(graph_);
42 graph_->AddBlock(loop_preheader_[d]);
43 loop_header_[d] = new (&allocator_) HBasicBlock(graph_);
44 graph_->AddBlock(loop_header_[d]);
45 loop_preheader_[d]->AddSuccessor(loop_header_[d]);
46 if (d < (n - 1)) {
47 BuildForLoop(d + 1, n);
48 }
49 loop_body_[d] = new (&allocator_) HBasicBlock(graph_);
50 graph_->AddBlock(loop_body_[d]);
51 loop_body_[d]->AddSuccessor(loop_header_[d]);
52 if (d < (n - 1)) {
53 loop_header_[d]->AddSuccessor(loop_preheader_[d + 1]);
54 loop_header_[d + 1]->AddSuccessor(loop_body_[d]);
55 } else {
56 loop_header_[d]->AddSuccessor(loop_body_[d]);
57 }
58 }
59
60 // Builds a n-nested loop in CFG where each loop at depth 0 <= d < n
61 // is defined as "for (int i_d = 0; i_d < 100; i_d++)". Tests can further
62 // populate the loop with instructions to set up interesting scenarios.
63 void BuildLoopNest(int n) {
64 ASSERT_LE(n, 10);
Aart Bike609b7c2015-08-27 13:46:58 -070065 graph_->SetNumberOfVRegs(n + 3);
Aart Bik30efb4e2015-07-30 12:14:31 -070066
67 // Build basic blocks with entry, nested loop, exit.
68 entry_ = new (&allocator_) HBasicBlock(graph_);
69 graph_->AddBlock(entry_);
70 BuildForLoop(0, n);
David Brazdildb51efb2015-11-06 01:36:20 +000071 return_ = new (&allocator_) HBasicBlock(graph_);
72 graph_->AddBlock(return_);
Aart Bik30efb4e2015-07-30 12:14:31 -070073 exit_ = new (&allocator_) HBasicBlock(graph_);
74 graph_->AddBlock(exit_);
75 entry_->AddSuccessor(loop_preheader_[0]);
David Brazdildb51efb2015-11-06 01:36:20 +000076 loop_header_[0]->AddSuccessor(return_);
77 return_->AddSuccessor(exit_);
Aart Bik30efb4e2015-07-30 12:14:31 -070078 graph_->SetEntryBlock(entry_);
79 graph_->SetExitBlock(exit_);
80
81 // Provide entry and exit instructions.
Calin Juravlee6e3bea2015-10-14 13:53:10 +000082 parameter_ = new (&allocator_) HParameterValue(
83 graph_->GetDexFile(), 0, 0, Primitive::kPrimNot, true);
Aart Bik30efb4e2015-07-30 12:14:31 -070084 entry_->AddInstruction(parameter_);
Aart Bike609b7c2015-08-27 13:46:58 -070085 constant0_ = graph_->GetIntConstant(0);
86 constant1_ = graph_->GetIntConstant(1);
87 constant100_ = graph_->GetIntConstant(100);
David Brazdil15693bf2015-12-16 10:30:45 +000088 float_constant0_ = graph_->GetFloatConstant(0.0f);
David Brazdildb51efb2015-11-06 01:36:20 +000089 return_->AddInstruction(new (&allocator_) HReturnVoid());
Aart Bike609b7c2015-08-27 13:46:58 -070090 exit_->AddInstruction(new (&allocator_) HExit());
Aart Bik30efb4e2015-07-30 12:14:31 -070091
92 // Provide loop instructions.
93 for (int d = 0; d < n; d++) {
David Brazdilbadd8262016-02-02 16:28:56 +000094 basic_[d] = new (&allocator_) HPhi(&allocator_, d, 0, Primitive::kPrimInt);
David Brazdil4833f5a2015-12-16 10:37:39 +000095 loop_preheader_[d]->AddInstruction(new (&allocator_) HGoto());
David Brazdilbadd8262016-02-02 16:28:56 +000096 loop_header_[d]->AddPhi(basic_[d]);
97 HInstruction* compare = new (&allocator_) HLessThan(basic_[d], constant100_);
Aart Bik30efb4e2015-07-30 12:14:31 -070098 loop_header_[d]->AddInstruction(compare);
99 loop_header_[d]->AddInstruction(new (&allocator_) HIf(compare));
David Brazdilbadd8262016-02-02 16:28:56 +0000100 increment_[d] = new (&allocator_) HAdd(Primitive::kPrimInt, basic_[d], constant1_);
Aart Bik30efb4e2015-07-30 12:14:31 -0700101 loop_body_[d]->AddInstruction(increment_[d]);
Aart Bik30efb4e2015-07-30 12:14:31 -0700102 loop_body_[d]->AddInstruction(new (&allocator_) HGoto());
David Brazdilbadd8262016-02-02 16:28:56 +0000103
104 basic_[d]->AddInput(constant0_);
105 basic_[d]->AddInput(increment_[d]);
Aart Bik30efb4e2015-07-30 12:14:31 -0700106 }
107 }
108
109 // Builds if-statement at depth d.
David Brazdilbadd8262016-02-02 16:28:56 +0000110 HPhi* BuildIf(int d, HBasicBlock** ifT, HBasicBlock **ifF) {
Aart Bik30efb4e2015-07-30 12:14:31 -0700111 HBasicBlock* cond = new (&allocator_) HBasicBlock(graph_);
112 HBasicBlock* ifTrue = new (&allocator_) HBasicBlock(graph_);
113 HBasicBlock* ifFalse = new (&allocator_) HBasicBlock(graph_);
114 graph_->AddBlock(cond);
115 graph_->AddBlock(ifTrue);
116 graph_->AddBlock(ifFalse);
117 // Conditional split.
118 loop_header_[d]->ReplaceSuccessor(loop_body_[d], cond);
119 cond->AddSuccessor(ifTrue);
120 cond->AddSuccessor(ifFalse);
121 ifTrue->AddSuccessor(loop_body_[d]);
122 ifFalse->AddSuccessor(loop_body_[d]);
123 cond->AddInstruction(new (&allocator_) HIf(parameter_));
124 *ifT = ifTrue;
125 *ifF = ifFalse;
David Brazdilbadd8262016-02-02 16:28:56 +0000126
127 HPhi* select_phi = new (&allocator_) HPhi(&allocator_, -1, 0, Primitive::kPrimInt);
128 loop_body_[d]->AddPhi(select_phi);
129 return select_phi;
Aart Bik30efb4e2015-07-30 12:14:31 -0700130 }
131
132 // Inserts instruction right before increment at depth d.
133 HInstruction* InsertInstruction(HInstruction* instruction, int d) {
134 loop_body_[d]->InsertInstructionBefore(instruction, increment_[d]);
135 return instruction;
136 }
137
David Brazdilbadd8262016-02-02 16:28:56 +0000138 // Inserts a phi to loop header at depth d and returns it.
139 HPhi* InsertLoopPhi(int vreg, int d) {
140 HPhi* phi = new (&allocator_) HPhi(&allocator_, vreg, 0, Primitive::kPrimInt);
141 loop_header_[d]->AddPhi(phi);
142 return phi;
Aart Bik30efb4e2015-07-30 12:14:31 -0700143 }
144
David Brazdilbadd8262016-02-02 16:28:56 +0000145 // Inserts an array store with given `subscript` at depth d to
Aart Bik30efb4e2015-07-30 12:14:31 -0700146 // enable tests to inspect the computed induction at that point easily.
David Brazdilbadd8262016-02-02 16:28:56 +0000147 HInstruction* InsertArrayStore(HInstruction* subscript, int d) {
David Brazdil15693bf2015-12-16 10:30:45 +0000148 // ArraySet is given a float value in order to avoid SsaBuilder typing
149 // it from the array's non-existent reference type info.
Aart Bik30efb4e2015-07-30 12:14:31 -0700150 return InsertInstruction(new (&allocator_) HArraySet(
David Brazdilbadd8262016-02-02 16:28:56 +0000151 parameter_, subscript, float_constant0_, Primitive::kPrimFloat, 0), d);
Aart Bik30efb4e2015-07-30 12:14:31 -0700152 }
153
Aart Bike609b7c2015-08-27 13:46:58 -0700154 // Returns induction information of instruction in loop at depth d.
155 std::string GetInductionInfo(HInstruction* instruction, int d) {
156 return HInductionVarAnalysis::InductionToString(
157 iva_->LookupInfo(loop_body_[d]->GetLoopInformation(), instruction));
Aart Bik30efb4e2015-07-30 12:14:31 -0700158 }
159
160 // Performs InductionVarAnalysis (after proper set up).
161 void PerformInductionVarAnalysis() {
David Brazdilbadd8262016-02-02 16:28:56 +0000162 graph_->BuildDominatorTree();
Aart Bik30efb4e2015-07-30 12:14:31 -0700163 iva_ = new (&allocator_) HInductionVarAnalysis(graph_);
164 iva_->Run();
165 }
166
167 // General building fields.
168 ArenaPool pool_;
169 ArenaAllocator allocator_;
170 HGraph* graph_;
171 HInductionVarAnalysis* iva_;
172
173 // Fixed basic blocks and instructions.
174 HBasicBlock* entry_;
David Brazdildb51efb2015-11-06 01:36:20 +0000175 HBasicBlock* return_;
Aart Bik30efb4e2015-07-30 12:14:31 -0700176 HBasicBlock* exit_;
177 HInstruction* parameter_; // "this"
178 HInstruction* constant0_;
179 HInstruction* constant1_;
180 HInstruction* constant100_;
David Brazdil15693bf2015-12-16 10:30:45 +0000181 HInstruction* float_constant0_;
Aart Bik30efb4e2015-07-30 12:14:31 -0700182
183 // Loop specifics.
184 HBasicBlock* loop_preheader_[10];
185 HBasicBlock* loop_header_[10];
186 HBasicBlock* loop_body_[10];
187 HInstruction* increment_[10];
David Brazdilbadd8262016-02-02 16:28:56 +0000188 HPhi* basic_[10]; // "vreg_d", the "i_d"
Aart Bik30efb4e2015-07-30 12:14:31 -0700189};
190
191//
192// The actual InductionVarAnalysis tests.
193//
194
195TEST_F(InductionVarAnalysisTest, ProperLoopSetup) {
196 // Setup:
197 // for (int i_0 = 0; i_0 < 100; i_0++) {
198 // ..
199 // for (int i_9 = 0; i_9 < 100; i_9++) {
200 // }
201 // ..
202 // }
203 BuildLoopNest(10);
David Brazdilbadd8262016-02-02 16:28:56 +0000204 graph_->BuildDominatorTree();
Aart Bik30efb4e2015-07-30 12:14:31 -0700205 ASSERT_EQ(entry_->GetLoopInformation(), nullptr);
206 for (int d = 0; d < 1; d++) {
207 ASSERT_EQ(loop_preheader_[d]->GetLoopInformation(),
208 (d == 0) ? nullptr
209 : loop_header_[d - 1]->GetLoopInformation());
210 ASSERT_NE(loop_header_[d]->GetLoopInformation(), nullptr);
211 ASSERT_NE(loop_body_[d]->GetLoopInformation(), nullptr);
212 ASSERT_EQ(loop_header_[d]->GetLoopInformation(),
213 loop_body_[d]->GetLoopInformation());
214 }
215 ASSERT_EQ(exit_->GetLoopInformation(), nullptr);
216}
217
Aart Bike609b7c2015-08-27 13:46:58 -0700218TEST_F(InductionVarAnalysisTest, FindBasicInduction) {
Aart Bik30efb4e2015-07-30 12:14:31 -0700219 // Setup:
220 // for (int i = 0; i < 100; i++) {
Aart Bike609b7c2015-08-27 13:46:58 -0700221 // a[i] = 0;
Aart Bik30efb4e2015-07-30 12:14:31 -0700222 // }
223 BuildLoopNest(1);
224 HInstruction* store = InsertArrayStore(basic_[0], 0);
225 PerformInductionVarAnalysis();
226
Aart Bike609b7c2015-08-27 13:46:58 -0700227 EXPECT_STREQ("((1) * i + (0))", GetInductionInfo(store->InputAt(1), 0).c_str());
Aart Bik471a2032015-09-04 18:22:11 -0700228 EXPECT_STREQ("((1) * i + (1))", GetInductionInfo(increment_[0], 0).c_str());
Aart Bikd14c5952015-09-08 15:25:15 -0700229
230 // Trip-count.
Aart Bik22f05872015-10-27 15:56:28 -0700231 EXPECT_STREQ("((100) (TC-loop) ((0) < (100)))",
Aart Bik9401f532015-09-28 16:25:56 -0700232 GetInductionInfo(loop_header_[0]->GetLastInstruction(), 0).c_str());
Aart Bik30efb4e2015-07-30 12:14:31 -0700233}
234
Aart Bike609b7c2015-08-27 13:46:58 -0700235TEST_F(InductionVarAnalysisTest, FindDerivedInduction) {
Aart Bik30efb4e2015-07-30 12:14:31 -0700236 // Setup:
237 // for (int i = 0; i < 100; i++) {
Aart Bike609b7c2015-08-27 13:46:58 -0700238 // k = 100 + i;
239 // k = 100 - i;
240 // k = 100 * i;
241 // k = i << 1;
242 // k = - i;
Aart Bik30efb4e2015-07-30 12:14:31 -0700243 // }
244 BuildLoopNest(1);
245 HInstruction *add = InsertInstruction(
David Brazdilbadd8262016-02-02 16:28:56 +0000246 new (&allocator_) HAdd(Primitive::kPrimInt, constant100_, basic_[0]), 0);
Aart Bik30efb4e2015-07-30 12:14:31 -0700247 HInstruction *sub = InsertInstruction(
David Brazdilbadd8262016-02-02 16:28:56 +0000248 new (&allocator_) HSub(Primitive::kPrimInt, constant100_, basic_[0]), 0);
Aart Bik30efb4e2015-07-30 12:14:31 -0700249 HInstruction *mul = InsertInstruction(
David Brazdilbadd8262016-02-02 16:28:56 +0000250 new (&allocator_) HMul(Primitive::kPrimInt, constant100_, basic_[0]), 0);
Aart Bike609b7c2015-08-27 13:46:58 -0700251 HInstruction *shl = InsertInstruction(
David Brazdilbadd8262016-02-02 16:28:56 +0000252 new (&allocator_) HShl(Primitive::kPrimInt, basic_[0], constant1_), 0);
Aart Bik30efb4e2015-07-30 12:14:31 -0700253 HInstruction *neg = InsertInstruction(
David Brazdilbadd8262016-02-02 16:28:56 +0000254 new (&allocator_) HNeg(Primitive::kPrimInt, basic_[0]), 0);
Aart Bik30efb4e2015-07-30 12:14:31 -0700255 PerformInductionVarAnalysis();
256
Aart Bik471a2032015-09-04 18:22:11 -0700257 EXPECT_STREQ("((1) * i + (100))", GetInductionInfo(add, 0).c_str());
258 EXPECT_STREQ("(( - (1)) * i + (100))", GetInductionInfo(sub, 0).c_str());
259 EXPECT_STREQ("((100) * i + (0))", GetInductionInfo(mul, 0).c_str());
260 EXPECT_STREQ("((2) * i + (0))", GetInductionInfo(shl, 0).c_str());
261 EXPECT_STREQ("(( - (1)) * i + (0))", GetInductionInfo(neg, 0).c_str());
Aart Bik30efb4e2015-07-30 12:14:31 -0700262}
263
264TEST_F(InductionVarAnalysisTest, FindChainInduction) {
265 // Setup:
266 // k = 0;
267 // for (int i = 0; i < 100; i++) {
Aart Bike609b7c2015-08-27 13:46:58 -0700268 // k = k + 100;
269 // a[k] = 0;
270 // k = k - 1;
271 // a[k] = 0;
Aart Bik30efb4e2015-07-30 12:14:31 -0700272 // }
273 BuildLoopNest(1);
David Brazdilbadd8262016-02-02 16:28:56 +0000274 HPhi* k = InsertLoopPhi(0, 0);
275 k->AddInput(constant0_);
276
Aart Bik30efb4e2015-07-30 12:14:31 -0700277 HInstruction *add = InsertInstruction(
David Brazdilbadd8262016-02-02 16:28:56 +0000278 new (&allocator_) HAdd(Primitive::kPrimInt, k, constant100_), 0);
279 HInstruction* store1 = InsertArrayStore(add, 0);
Aart Bik30efb4e2015-07-30 12:14:31 -0700280 HInstruction *sub = InsertInstruction(
David Brazdilbadd8262016-02-02 16:28:56 +0000281 new (&allocator_) HSub(Primitive::kPrimInt, add, constant1_), 0);
282 HInstruction* store2 = InsertArrayStore(sub, 0);
283 k->AddInput(sub);
Aart Bik30efb4e2015-07-30 12:14:31 -0700284 PerformInductionVarAnalysis();
285
Aart Bik471a2032015-09-04 18:22:11 -0700286 EXPECT_STREQ("(((100) - (1)) * i + (100))",
Aart Bike609b7c2015-08-27 13:46:58 -0700287 GetInductionInfo(store1->InputAt(1), 0).c_str());
Aart Bik471a2032015-09-04 18:22:11 -0700288 EXPECT_STREQ("(((100) - (1)) * i + ((100) - (1)))",
Aart Bike609b7c2015-08-27 13:46:58 -0700289 GetInductionInfo(store2->InputAt(1), 0).c_str());
Aart Bik30efb4e2015-07-30 12:14:31 -0700290}
291
292TEST_F(InductionVarAnalysisTest, FindTwoWayBasicInduction) {
293 // Setup:
294 // k = 0;
295 // for (int i = 0; i < 100; i++) {
Aart Bike609b7c2015-08-27 13:46:58 -0700296 // if () k = k + 1;
297 // else k = k + 1;
298 // a[k] = 0;
Aart Bik30efb4e2015-07-30 12:14:31 -0700299 // }
300 BuildLoopNest(1);
David Brazdilbadd8262016-02-02 16:28:56 +0000301 HPhi* k_header = InsertLoopPhi(0, 0);
302 k_header->AddInput(constant0_);
303
Aart Bik30efb4e2015-07-30 12:14:31 -0700304 HBasicBlock* ifTrue;
305 HBasicBlock* ifFalse;
David Brazdilbadd8262016-02-02 16:28:56 +0000306 HPhi* k_body = BuildIf(0, &ifTrue, &ifFalse);
307
Aart Bik30efb4e2015-07-30 12:14:31 -0700308 // True-branch.
David Brazdilbadd8262016-02-02 16:28:56 +0000309 HInstruction* inc1 = new (&allocator_) HAdd(Primitive::kPrimInt, k_header, constant1_);
Aart Bik30efb4e2015-07-30 12:14:31 -0700310 ifTrue->AddInstruction(inc1);
David Brazdilbadd8262016-02-02 16:28:56 +0000311 k_body->AddInput(inc1);
Aart Bik30efb4e2015-07-30 12:14:31 -0700312 // False-branch.
David Brazdilbadd8262016-02-02 16:28:56 +0000313 HInstruction* inc2 = new (&allocator_) HAdd(Primitive::kPrimInt, k_header, constant1_);
Aart Bik30efb4e2015-07-30 12:14:31 -0700314 ifFalse->AddInstruction(inc2);
David Brazdilbadd8262016-02-02 16:28:56 +0000315 k_body->AddInput(inc2);
Aart Bik30efb4e2015-07-30 12:14:31 -0700316 // Merge over a phi.
David Brazdilbadd8262016-02-02 16:28:56 +0000317 HInstruction* store = InsertArrayStore(k_body, 0);
318 k_header->AddInput(k_body);
Aart Bik30efb4e2015-07-30 12:14:31 -0700319 PerformInductionVarAnalysis();
320
Aart Bik471a2032015-09-04 18:22:11 -0700321 EXPECT_STREQ("((1) * i + (1))", GetInductionInfo(store->InputAt(1), 0).c_str());
Aart Bik30efb4e2015-07-30 12:14:31 -0700322}
323
324TEST_F(InductionVarAnalysisTest, FindTwoWayDerivedInduction) {
325 // Setup:
326 // for (int i = 0; i < 100; i++) {
Aart Bike609b7c2015-08-27 13:46:58 -0700327 // if () k = i + 1;
328 // else k = i + 1;
329 // a[k] = 0;
Aart Bik30efb4e2015-07-30 12:14:31 -0700330 // }
331 BuildLoopNest(1);
332 HBasicBlock* ifTrue;
333 HBasicBlock* ifFalse;
David Brazdilbadd8262016-02-02 16:28:56 +0000334 HPhi* k = BuildIf(0, &ifTrue, &ifFalse);
335
Aart Bik30efb4e2015-07-30 12:14:31 -0700336 // True-branch.
David Brazdilbadd8262016-02-02 16:28:56 +0000337 HInstruction* inc1 = new (&allocator_) HAdd(Primitive::kPrimInt, basic_[0], constant1_);
Aart Bik30efb4e2015-07-30 12:14:31 -0700338 ifTrue->AddInstruction(inc1);
David Brazdilbadd8262016-02-02 16:28:56 +0000339 k->AddInput(inc1);
Aart Bik30efb4e2015-07-30 12:14:31 -0700340 // False-branch.
David Brazdilbadd8262016-02-02 16:28:56 +0000341 HInstruction* inc2 = new (&allocator_) HAdd(Primitive::kPrimInt, basic_[0], constant1_);
Aart Bik30efb4e2015-07-30 12:14:31 -0700342 ifFalse->AddInstruction(inc2);
David Brazdilbadd8262016-02-02 16:28:56 +0000343 k->AddInput(inc2);
Aart Bik30efb4e2015-07-30 12:14:31 -0700344 // Merge over a phi.
David Brazdilbadd8262016-02-02 16:28:56 +0000345 HInstruction* store = InsertArrayStore(k, 0);
Aart Bik30efb4e2015-07-30 12:14:31 -0700346 PerformInductionVarAnalysis();
347
Aart Bik471a2032015-09-04 18:22:11 -0700348 EXPECT_STREQ("((1) * i + (1))", GetInductionInfo(store->InputAt(1), 0).c_str());
Aart Bik30efb4e2015-07-30 12:14:31 -0700349}
350
351TEST_F(InductionVarAnalysisTest, FindFirstOrderWrapAroundInduction) {
352 // Setup:
353 // k = 0;
354 // for (int i = 0; i < 100; i++) {
Aart Bike609b7c2015-08-27 13:46:58 -0700355 // a[k] = 0;
356 // k = 100 - i;
Aart Bik30efb4e2015-07-30 12:14:31 -0700357 // }
358 BuildLoopNest(1);
David Brazdilbadd8262016-02-02 16:28:56 +0000359 HPhi* k = InsertLoopPhi(0, 0);
360 k->AddInput(constant0_);
361
362 HInstruction* store = InsertArrayStore(k, 0);
Aart Bik30efb4e2015-07-30 12:14:31 -0700363 HInstruction *sub = InsertInstruction(
David Brazdilbadd8262016-02-02 16:28:56 +0000364 new (&allocator_) HSub(Primitive::kPrimInt, constant100_, basic_[0]), 0);
365 k->AddInput(sub);
Aart Bik30efb4e2015-07-30 12:14:31 -0700366 PerformInductionVarAnalysis();
367
Aart Bik471a2032015-09-04 18:22:11 -0700368 EXPECT_STREQ("wrap((0), (( - (1)) * i + (100)))",
Aart Bike609b7c2015-08-27 13:46:58 -0700369 GetInductionInfo(store->InputAt(1), 0).c_str());
Aart Bik30efb4e2015-07-30 12:14:31 -0700370}
371
372TEST_F(InductionVarAnalysisTest, FindSecondOrderWrapAroundInduction) {
373 // Setup:
374 // k = 0;
375 // t = 100;
376 // for (int i = 0; i < 100; i++) {
Aart Bike609b7c2015-08-27 13:46:58 -0700377 // a[k] = 0;
378 // k = t;
379 // t = 100 - i;
Aart Bik30efb4e2015-07-30 12:14:31 -0700380 // }
381 BuildLoopNest(1);
David Brazdilbadd8262016-02-02 16:28:56 +0000382 HPhi* k = InsertLoopPhi(0, 0);
383 k->AddInput(constant0_);
384 HPhi* t = InsertLoopPhi(1, 0);
385 t->AddInput(constant100_);
386
387 HInstruction* store = InsertArrayStore(k, 0);
388 k->AddInput(t);
Aart Bik30efb4e2015-07-30 12:14:31 -0700389 HInstruction *sub = InsertInstruction(
David Brazdilbadd8262016-02-02 16:28:56 +0000390 new (&allocator_) HSub(Primitive::kPrimInt, constant100_, basic_[0], 0), 0);
391 t->AddInput(sub);
Aart Bik30efb4e2015-07-30 12:14:31 -0700392 PerformInductionVarAnalysis();
393
Aart Bik471a2032015-09-04 18:22:11 -0700394 EXPECT_STREQ("wrap((0), wrap((100), (( - (1)) * i + (100))))",
Aart Bike609b7c2015-08-27 13:46:58 -0700395 GetInductionInfo(store->InputAt(1), 0).c_str());
396}
397
398TEST_F(InductionVarAnalysisTest, FindWrapAroundDerivedInduction) {
399 // Setup:
400 // k = 0;
401 // for (int i = 0; i < 100; i++) {
402 // t = k + 100;
403 // t = k - 100;
404 // t = k * 100;
405 // t = k << 1;
406 // t = - k;
407 // k = i << 1;
408 // }
409 BuildLoopNest(1);
David Brazdilbadd8262016-02-02 16:28:56 +0000410 HPhi* k = InsertLoopPhi(0, 0);
411 k->AddInput(constant0_);
412
Aart Bike609b7c2015-08-27 13:46:58 -0700413 HInstruction *add = InsertInstruction(
David Brazdilbadd8262016-02-02 16:28:56 +0000414 new (&allocator_) HAdd(Primitive::kPrimInt, k, constant100_), 0);
Aart Bike609b7c2015-08-27 13:46:58 -0700415 HInstruction *sub = InsertInstruction(
David Brazdilbadd8262016-02-02 16:28:56 +0000416 new (&allocator_) HSub(Primitive::kPrimInt, k, constant100_), 0);
Aart Bike609b7c2015-08-27 13:46:58 -0700417 HInstruction *mul = InsertInstruction(
David Brazdilbadd8262016-02-02 16:28:56 +0000418 new (&allocator_) HMul(Primitive::kPrimInt, k, constant100_), 0);
Aart Bike609b7c2015-08-27 13:46:58 -0700419 HInstruction *shl = InsertInstruction(
David Brazdilbadd8262016-02-02 16:28:56 +0000420 new (&allocator_) HShl(Primitive::kPrimInt, k, constant1_), 0);
Aart Bike609b7c2015-08-27 13:46:58 -0700421 HInstruction *neg = InsertInstruction(
David Brazdilbadd8262016-02-02 16:28:56 +0000422 new (&allocator_) HNeg(Primitive::kPrimInt, k), 0);
423 k->AddInput(
424 InsertInstruction(new (&allocator_) HShl(Primitive::kPrimInt, basic_[0], constant1_), 0));
Aart Bike609b7c2015-08-27 13:46:58 -0700425 PerformInductionVarAnalysis();
426
Aart Bik471a2032015-09-04 18:22:11 -0700427 EXPECT_STREQ("wrap((100), ((2) * i + (100)))", GetInductionInfo(add, 0).c_str());
428 EXPECT_STREQ("wrap(((0) - (100)), ((2) * i + ((0) - (100))))", GetInductionInfo(sub, 0).c_str());
429 EXPECT_STREQ("wrap((0), (((2) * (100)) * i + (0)))", GetInductionInfo(mul, 0).c_str());
430 EXPECT_STREQ("wrap((0), (((2) * (2)) * i + (0)))", GetInductionInfo(shl, 0).c_str());
431 EXPECT_STREQ("wrap((0), (( - (2)) * i + (0)))", GetInductionInfo(neg, 0).c_str());
Aart Bike609b7c2015-08-27 13:46:58 -0700432}
433
434TEST_F(InductionVarAnalysisTest, FindPeriodicInduction) {
435 // Setup:
436 // k = 0;
437 // t = 100;
438 // for (int i = 0; i < 100; i++) {
439 // a[k] = 0;
440 // a[t] = 0;
441 // // Swap t <-> k.
442 // d = t;
443 // t = k;
444 // k = d;
445 // }
446 BuildLoopNest(1);
David Brazdilbadd8262016-02-02 16:28:56 +0000447 HPhi* k = InsertLoopPhi(0, 0);
448 k->AddInput(constant0_);
449 HPhi* t = InsertLoopPhi(1, 0);
450 t->AddInput(constant100_);
451
452 HInstruction* store1 = InsertArrayStore(k, 0);
453 HInstruction* store2 = InsertArrayStore(t, 0);
454 k->AddInput(t);
455 t->AddInput(k);
Aart Bike609b7c2015-08-27 13:46:58 -0700456 PerformInductionVarAnalysis();
457
458 EXPECT_STREQ("periodic((0), (100))", GetInductionInfo(store1->InputAt(1), 0).c_str());
459 EXPECT_STREQ("periodic((100), (0))", GetInductionInfo(store2->InputAt(1), 0).c_str());
460}
461
462TEST_F(InductionVarAnalysisTest, FindIdiomaticPeriodicInduction) {
463 // Setup:
464 // k = 0;
465 // for (int i = 0; i < 100; i++) {
466 // a[k] = 0;
467 // k = 1 - k;
468 // }
469 BuildLoopNest(1);
David Brazdilbadd8262016-02-02 16:28:56 +0000470 HPhi* k = InsertLoopPhi(0, 0);
471 k->AddInput(constant0_);
472
473 HInstruction* store = InsertArrayStore(k, 0);
Aart Bike609b7c2015-08-27 13:46:58 -0700474 HInstruction *sub = InsertInstruction(
David Brazdilbadd8262016-02-02 16:28:56 +0000475 new (&allocator_) HSub(Primitive::kPrimInt, constant1_, k), 0);
476 k->AddInput(sub);
Aart Bike609b7c2015-08-27 13:46:58 -0700477 PerformInductionVarAnalysis();
478
Aart Bik471a2032015-09-04 18:22:11 -0700479 EXPECT_STREQ("periodic((0), (1))", GetInductionInfo(store->InputAt(1), 0).c_str());
480 EXPECT_STREQ("periodic((1), (0))", GetInductionInfo(sub, 0).c_str());
Aart Bike609b7c2015-08-27 13:46:58 -0700481}
482
483TEST_F(InductionVarAnalysisTest, FindDerivedPeriodicInduction) {
484 // Setup:
485 // k = 0;
486 // for (int i = 0; i < 100; i++) {
487 // k = 1 - k;
488 // t = k + 100;
489 // t = k - 100;
490 // t = k * 100;
491 // t = k << 1;
492 // t = - k;
493 // }
494 BuildLoopNest(1);
David Brazdilbadd8262016-02-02 16:28:56 +0000495 HPhi* k_header = InsertLoopPhi(0, 0);
496 k_header->AddInput(constant0_);
497
498 HInstruction* k_body = InsertInstruction(
499 new (&allocator_) HSub(Primitive::kPrimInt, constant1_, k_header), 0);
500 k_header->AddInput(k_body);
501
Aart Bike609b7c2015-08-27 13:46:58 -0700502 // Derived expressions.
503 HInstruction *add = InsertInstruction(
David Brazdilbadd8262016-02-02 16:28:56 +0000504 new (&allocator_) HAdd(Primitive::kPrimInt, k_body, constant100_), 0);
Aart Bike609b7c2015-08-27 13:46:58 -0700505 HInstruction *sub = InsertInstruction(
David Brazdilbadd8262016-02-02 16:28:56 +0000506 new (&allocator_) HSub(Primitive::kPrimInt, k_body, constant100_), 0);
Aart Bike609b7c2015-08-27 13:46:58 -0700507 HInstruction *mul = InsertInstruction(
David Brazdilbadd8262016-02-02 16:28:56 +0000508 new (&allocator_) HMul(Primitive::kPrimInt, k_body, constant100_), 0);
Aart Bike609b7c2015-08-27 13:46:58 -0700509 HInstruction *shl = InsertInstruction(
David Brazdilbadd8262016-02-02 16:28:56 +0000510 new (&allocator_) HShl(Primitive::kPrimInt, k_body, constant1_), 0);
Aart Bike609b7c2015-08-27 13:46:58 -0700511 HInstruction *neg = InsertInstruction(
David Brazdilbadd8262016-02-02 16:28:56 +0000512 new (&allocator_) HNeg(Primitive::kPrimInt, k_body), 0);
Aart Bike609b7c2015-08-27 13:46:58 -0700513 PerformInductionVarAnalysis();
514
Aart Bik471a2032015-09-04 18:22:11 -0700515 EXPECT_STREQ("periodic(((1) + (100)), (100))", GetInductionInfo(add, 0).c_str());
516 EXPECT_STREQ("periodic(((1) - (100)), ((0) - (100)))", GetInductionInfo(sub, 0).c_str());
517 EXPECT_STREQ("periodic((100), (0))", GetInductionInfo(mul, 0).c_str());
518 EXPECT_STREQ("periodic((2), (0))", GetInductionInfo(shl, 0).c_str());
519 EXPECT_STREQ("periodic(( - (1)), (0))", GetInductionInfo(neg, 0).c_str());
Aart Bik30efb4e2015-07-30 12:14:31 -0700520}
521
522TEST_F(InductionVarAnalysisTest, FindDeepLoopInduction) {
523 // Setup:
524 // k = 0;
525 // for (int i_0 = 0; i_0 < 100; i_0++) {
526 // ..
527 // for (int i_9 = 0; i_9 < 100; i_9++) {
528 // k = 1 + k;
529 // a[k] = 0;
530 // }
531 // ..
532 // }
533 BuildLoopNest(10);
David Brazdilbadd8262016-02-02 16:28:56 +0000534
535 HPhi* k[10];
536 for (int d = 0; d < 10; d++) {
537 k[d] = InsertLoopPhi(0, d);
538 }
539
Aart Bik30efb4e2015-07-30 12:14:31 -0700540 HInstruction *inc = InsertInstruction(
David Brazdilbadd8262016-02-02 16:28:56 +0000541 new (&allocator_) HAdd(Primitive::kPrimInt, constant1_, k[9]), 9);
542 HInstruction* store = InsertArrayStore(inc, 9);
543
544 for (int d = 0; d < 10; d++) {
David Brazdil6dd10fa2016-02-15 17:14:31 +0000545 k[d]->AddInput((d != 0) ? k[d - 1] : constant0_);
David Brazdilbadd8262016-02-02 16:28:56 +0000546 k[d]->AddInput((d != 9) ? k[d + 1] : inc);
547 }
Aart Bik30efb4e2015-07-30 12:14:31 -0700548 PerformInductionVarAnalysis();
549
Aart Bike609b7c2015-08-27 13:46:58 -0700550 // Avoid exact phi number, since that depends on the SSA building phase.
551 std::regex r("\\(\\(1\\) \\* i \\+ "
552 "\\(\\(1\\) \\+ \\(\\d+:Phi\\)\\)\\)");
Aart Bik30efb4e2015-07-30 12:14:31 -0700553
554 for (int d = 0; d < 10; d++) {
555 if (d == 9) {
Aart Bike609b7c2015-08-27 13:46:58 -0700556 EXPECT_TRUE(std::regex_match(GetInductionInfo(store->InputAt(1), d), r));
Aart Bik30efb4e2015-07-30 12:14:31 -0700557 } else {
Aart Bike609b7c2015-08-27 13:46:58 -0700558 EXPECT_STREQ("", GetInductionInfo(store->InputAt(1), d).c_str());
Aart Bik30efb4e2015-07-30 12:14:31 -0700559 }
Aart Bik471a2032015-09-04 18:22:11 -0700560 EXPECT_STREQ("((1) * i + (1))", GetInductionInfo(increment_[d], d).c_str());
Aart Bikd14c5952015-09-08 15:25:15 -0700561 // Trip-count.
Aart Bik22f05872015-10-27 15:56:28 -0700562 EXPECT_STREQ("((100) (TC-loop) ((0) < (100)))",
Aart Bik9401f532015-09-28 16:25:56 -0700563 GetInductionInfo(loop_header_[d]->GetLastInstruction(), d).c_str());
Aart Bik30efb4e2015-07-30 12:14:31 -0700564 }
565}
566
567} // namespace art