blob: 15d500a30c8838cd2f25924ad77652b87e6dab32 [file] [log] [blame]
Michael Zolotukhin1da4afd2016-02-08 23:03:59 +00001//===- UnrollAnalyzerTest.cpp - UnrollAnalyzer unit tests -----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/AsmParser/Parser.h"
11#include "llvm/IR/LegacyPassManager.h"
12#include "llvm/Support/SourceMgr.h"
13#include "llvm/Analysis/LoopUnrollAnalyzer.h"
14#include "llvm/IR/Dominators.h"
15#include "gtest/gtest.h"
16
17using namespace llvm;
18namespace llvm {
19void initializeUnrollAnalyzerTestPass(PassRegistry &);
20
21static SmallVector<DenseMap<Value *, Constant *>, 16> SimplifiedValuesVector;
22static unsigned TripCount = 0;
23
24namespace {
25struct UnrollAnalyzerTest : public FunctionPass {
26 static char ID;
27 bool runOnFunction(Function &F) override {
28 LoopInfo *LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
29 ScalarEvolution *SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
30
31 Function::iterator FI = F.begin();
32 FI++; // First basic block is entry - skip it.
33 BasicBlock *Header = &*FI++;
34 Loop *L = LI->getLoopFor(Header);
Michael Zolotukhin374651d2016-02-26 01:44:04 +000035 BasicBlock *Exiting = L->getExitingBlock();
Michael Zolotukhin1da4afd2016-02-08 23:03:59 +000036
37 SimplifiedValuesVector.clear();
Michael Zolotukhin374651d2016-02-26 01:44:04 +000038 TripCount = SE->getSmallConstantTripCount(L, Exiting);
Michael Zolotukhin1da4afd2016-02-08 23:03:59 +000039 for (unsigned Iteration = 0; Iteration < TripCount; Iteration++) {
40 DenseMap<Value *, Constant *> SimplifiedValues;
Michael Zolotukhin9f520eb2016-02-26 02:57:05 +000041 UnrolledInstAnalyzer Analyzer(Iteration, SimplifiedValues, *SE, L);
Michael Zolotukhin374651d2016-02-26 01:44:04 +000042 for (auto *BB : L->getBlocks())
43 for (Instruction &I : *BB)
44 Analyzer.visit(I);
Michael Zolotukhin1da4afd2016-02-08 23:03:59 +000045 SimplifiedValuesVector.push_back(SimplifiedValues);
46 }
47 return false;
48 }
49 void getAnalysisUsage(AnalysisUsage &AU) const override {
50 AU.addRequired<DominatorTreeWrapperPass>();
51 AU.addRequired<LoopInfoWrapperPass>();
52 AU.addRequired<ScalarEvolutionWrapperPass>();
53 AU.setPreservesAll();
54 }
55 UnrollAnalyzerTest() : FunctionPass(ID) {
56 initializeUnrollAnalyzerTestPass(*PassRegistry::getPassRegistry());
57 }
58};
59}
60
61char UnrollAnalyzerTest::ID = 0;
62
63std::unique_ptr<Module> makeLLVMModule(UnrollAnalyzerTest *P,
64 const char *ModuleStr) {
65 LLVMContext &C = getGlobalContext();
66 SMDiagnostic Err;
67 return parseAssemblyString(ModuleStr, Err, C);
68}
69
70TEST(UnrollAnalyzerTest, BasicSimplifications) {
71 const char *ModuleStr =
72 "target datalayout = \"e-m:o-i64:64-f80:128-n8:16:32:64-S128\"\n"
73 "define i64 @propagate_loop_phis() {\n"
74 "entry:\n"
75 " br label %loop\n"
76 "loop:\n"
77 " %iv = phi i64 [ 0, %entry ], [ %inc, %loop ]\n"
78 " %x0 = phi i64 [ 0, %entry ], [ %x2, %loop ]\n"
79 " %x1 = or i64 %x0, 1\n"
80 " %x2 = or i64 %x1, 2\n"
81 " %inc = add nuw nsw i64 %iv, 1\n"
82 " %cond = icmp sge i64 %inc, 8\n"
83 " br i1 %cond, label %loop.end, label %loop\n"
84 "loop.end:\n"
85 " %x.lcssa = phi i64 [ %x2, %loop ]\n"
86 " ret i64 %x.lcssa\n"
87 "}\n";
88 UnrollAnalyzerTest *P = new UnrollAnalyzerTest();
89 std::unique_ptr<Module> M = makeLLVMModule(P, ModuleStr);
90 legacy::PassManager Passes;
91 Passes.add(P);
92 Passes.run(*M);
93
94 // Perform checks
95 Module::iterator MI = M->begin();
96 Function *F = &*MI++;
97 Function::iterator FI = F->begin();
98 FI++; // First basic block is entry - skip it.
99 BasicBlock *Header = &*FI++;
100
101 BasicBlock::iterator BBI = Header->begin();
102 std::advance(BBI, 4);
103 Instruction *Y1 = &*BBI++;
104 Instruction *Y2 = &*BBI++;
105 // Check simplification expected on the 1st iteration.
106 // Check that "%inc = add nuw nsw i64 %iv, 1" is simplified to 1
107 auto I1 = SimplifiedValuesVector[0].find(Y1);
108 EXPECT_TRUE(I1 != SimplifiedValuesVector[0].end());
Michael Zolotukhin789ac452016-03-12 01:28:56 +0000109 EXPECT_EQ(cast<ConstantInt>((*I1).second)->getZExtValue(), 1U);
Michael Zolotukhin1da4afd2016-02-08 23:03:59 +0000110
111 // Check that "%cond = icmp sge i64 %inc, 10" is simplified to false
112 auto I2 = SimplifiedValuesVector[0].find(Y2);
113 EXPECT_TRUE(I2 != SimplifiedValuesVector[0].end());
Michael Zolotukhin789ac452016-03-12 01:28:56 +0000114 EXPECT_FALSE(cast<ConstantInt>((*I2).second)->getZExtValue());
Michael Zolotukhin1da4afd2016-02-08 23:03:59 +0000115
116 // Check simplification expected on the last iteration.
117 // Check that "%inc = add nuw nsw i64 %iv, 1" is simplified to 8
118 I1 = SimplifiedValuesVector[TripCount - 1].find(Y1);
119 EXPECT_TRUE(I1 != SimplifiedValuesVector[TripCount - 1].end());
Michael Zolotukhin789ac452016-03-12 01:28:56 +0000120 EXPECT_EQ(cast<ConstantInt>((*I1).second)->getZExtValue(), TripCount);
Michael Zolotukhin1da4afd2016-02-08 23:03:59 +0000121
122 // Check that "%cond = icmp sge i64 %inc, 10" is simplified to false
123 I2 = SimplifiedValuesVector[TripCount - 1].find(Y2);
124 EXPECT_TRUE(I2 != SimplifiedValuesVector[TripCount - 1].end());
Michael Zolotukhin789ac452016-03-12 01:28:56 +0000125 EXPECT_TRUE(cast<ConstantInt>((*I2).second)->getZExtValue());
Michael Zolotukhin1da4afd2016-02-08 23:03:59 +0000126}
Michael Zolotukhin9f520eb2016-02-26 02:57:05 +0000127
128TEST(UnrollAnalyzerTest, OuterLoopSimplification) {
129 const char *ModuleStr =
130 "target datalayout = \"e-m:o-i64:64-f80:128-n8:16:32:64-S128\"\n"
131 "define void @foo() {\n"
132 "entry:\n"
133 " br label %outer.loop\n"
134 "outer.loop:\n"
135 " %iv.outer = phi i64 [ 0, %entry ], [ %iv.outer.next, %outer.loop.latch ]\n"
136 " br label %inner.loop\n"
137 "inner.loop:\n"
138 " %iv.inner = phi i64 [ 0, %outer.loop ], [ %iv.inner.next, %inner.loop ]\n"
139 " %iv.inner.next = add nuw nsw i64 %iv.inner, 1\n"
140 " %exitcond.inner = icmp eq i64 %iv.inner.next, 1000\n"
141 " br i1 %exitcond.inner, label %outer.loop.latch, label %inner.loop\n"
142 "outer.loop.latch:\n"
143 " %iv.outer.next = add nuw nsw i64 %iv.outer, 1\n"
144 " %exitcond.outer = icmp eq i64 %iv.outer.next, 40\n"
145 " br i1 %exitcond.outer, label %exit, label %outer.loop\n"
146 "exit:\n"
147 " ret void\n"
148 "}\n";
149
150 UnrollAnalyzerTest *P = new UnrollAnalyzerTest();
151 std::unique_ptr<Module> M = makeLLVMModule(P, ModuleStr);
152 legacy::PassManager Passes;
153 Passes.add(P);
154 Passes.run(*M);
155
156 Module::iterator MI = M->begin();
157 Function *F = &*MI++;
158 Function::iterator FI = F->begin();
159 FI++;
160 BasicBlock *Header = &*FI++;
161 BasicBlock *InnerBody = &*FI++;
162
163 BasicBlock::iterator BBI = Header->begin();
164 Instruction *Y1 = &*BBI++;
165 BBI = InnerBody->begin();
166 Instruction *Y2 = &*BBI++;
167 // Check that we can simplify IV of the outer loop, but can't simplify the IV
168 // of the inner loop if we only know the iteration number of the outer loop.
169 auto I1 = SimplifiedValuesVector[0].find(Y1);
170 EXPECT_TRUE(I1 != SimplifiedValuesVector[0].end());
171 auto I2 = SimplifiedValuesVector[0].find(Y2);
172 EXPECT_TRUE(I2 == SimplifiedValuesVector[0].end());
173}
Michael Zolotukhin789ac452016-03-12 01:28:56 +0000174TEST(UnrollAnalyzerTest, CmpSimplifications) {
175 const char *ModuleStr =
176 "target datalayout = \"e-m:o-i64:64-f80:128-n8:16:32:64-S128\"\n"
177 "define void @branch_iv_trunc() {\n"
178 "entry:\n"
179 " br label %for.body\n"
180 "for.body:\n"
181 " %indvars.iv = phi i64 [ 0, %entry ], [ %tmp3, %for.body ]\n"
182 " %tmp2 = trunc i64 %indvars.iv to i32\n"
183 " %cmp3 = icmp eq i32 %tmp2, 5\n"
184 " %tmp3 = add nuw nsw i64 %indvars.iv, 1\n"
185 " %exitcond = icmp eq i64 %tmp3, 10\n"
186 " br i1 %exitcond, label %for.end, label %for.body\n"
187 "for.end:\n"
188 " ret void\n"
189 "}\n";
190 UnrollAnalyzerTest *P = new UnrollAnalyzerTest();
191 std::unique_ptr<Module> M = makeLLVMModule(P, ModuleStr);
192 legacy::PassManager Passes;
193 Passes.add(P);
194 Passes.run(*M);
195
196 // Perform checks
197 Module::iterator MI = M->begin();
198 Function *F = &*MI++;
199 Function::iterator FI = F->begin();
200 FI++; // First basic block is entry - skip it.
201 BasicBlock *Header = &*FI++;
202
203 BasicBlock::iterator BBI = Header->begin();
204 BBI++;
205 Instruction *Y1 = &*BBI++;
206 Instruction *Y2 = &*BBI++;
207 // Check simplification expected on the 5th iteration.
208 // Check that "%tmp2 = trunc i64 %indvars.iv to i32" is simplified to 5
209 // and "%cmp3 = icmp eq i32 %tmp2, 5" is simplified to 1 (i.e. true).
210 auto I1 = SimplifiedValuesVector[5].find(Y1);
211 EXPECT_TRUE(I1 != SimplifiedValuesVector[5].end());
212 EXPECT_EQ(cast<ConstantInt>((*I1).second)->getZExtValue(), 5U);
213 auto I2 = SimplifiedValuesVector[5].find(Y2);
214 EXPECT_TRUE(I2 != SimplifiedValuesVector[5].end());
215 EXPECT_EQ(cast<ConstantInt>((*I2).second)->getZExtValue(), 1U);
216}
217TEST(UnrollAnalyzerTest, PtrCmpSimplifications) {
218 const char *ModuleStr =
219 "target datalayout = \"e-m:o-i64:64-f80:128-n8:16:32:64-S128\"\n"
220 "define void @ptr_cmp(i8 *%a) {\n"
221 "entry:\n"
222 " %limit = getelementptr i8, i8* %a, i64 40\n"
223 " %start.iv2 = getelementptr i8, i8* %a, i64 7\n"
224 " br label %loop.body\n"
225 "loop.body:\n"
226 " %iv.0 = phi i8* [ %a, %entry ], [ %iv.1, %loop.body ]\n"
227 " %iv2.0 = phi i8* [ %start.iv2, %entry ], [ %iv2.1, %loop.body ]\n"
228 " %cmp = icmp eq i8* %iv2.0, %iv.0\n"
229 " %iv.1 = getelementptr inbounds i8, i8* %iv.0, i64 1\n"
230 " %iv2.1 = getelementptr inbounds i8, i8* %iv2.0, i64 1\n"
231 " %exitcond = icmp ne i8* %iv.1, %limit\n"
232 " br i1 %exitcond, label %loop.body, label %loop.exit\n"
233 "loop.exit:\n"
234 " ret void\n"
235 "}\n";
236 UnrollAnalyzerTest *P = new UnrollAnalyzerTest();
237 std::unique_ptr<Module> M = makeLLVMModule(P, ModuleStr);
238 legacy::PassManager Passes;
239 Passes.add(P);
240 Passes.run(*M);
241
242 // Perform checks
243 Module::iterator MI = M->begin();
244 Function *F = &*MI++;
245 Function::iterator FI = F->begin();
246 FI++; // First basic block is entry - skip it.
247 BasicBlock *Header = &*FI;
248
249 BasicBlock::iterator BBI = Header->begin();
250 std::advance(BBI, 2);
251 Instruction *Y1 = &*BBI;
252 // Check simplification expected on the 5th iteration.
253 // Check that "%cmp = icmp eq i8* %iv2.0, %iv.0" is simplified to 0.
254 auto I1 = SimplifiedValuesVector[5].find(Y1);
255 EXPECT_TRUE(I1 != SimplifiedValuesVector[5].end());
256 EXPECT_EQ(cast<ConstantInt>((*I1).second)->getZExtValue(), 0U);
257}
258TEST(UnrollAnalyzerTest, CastSimplifications) {
259 const char *ModuleStr =
260 "target datalayout = \"e-m:o-i64:64-f80:128-n8:16:32:64-S128\"\n"
261 "@known_constant = internal unnamed_addr constant [10 x i32] [i32 0, i32 1, i32 0, i32 1, i32 0, i32 259, i32 0, i32 1, i32 0, i32 1], align 16\n"
262 "define void @const_load_cast() {\n"
263 "entry:\n"
264 " br label %loop\n"
265 "\n"
266 "loop:\n"
267 " %iv = phi i64 [ 0, %entry ], [ %inc, %loop ]\n"
268 " %array_const_idx = getelementptr inbounds [10 x i32], [10 x i32]* @known_constant, i64 0, i64 %iv\n"
269 " %const_array_element = load i32, i32* %array_const_idx, align 4\n"
270 " %se = sext i32 %const_array_element to i64\n"
271 " %ze = zext i32 %const_array_element to i64\n"
272 " %tr = trunc i32 %const_array_element to i8\n"
273 " %inc = add nuw nsw i64 %iv, 1\n"
274 " %exitcond86.i = icmp eq i64 %inc, 10\n"
275 " br i1 %exitcond86.i, label %loop.end, label %loop\n"
276 "\n"
277 "loop.end:\n"
278 " ret void\n"
279 "}\n";
280
281 UnrollAnalyzerTest *P = new UnrollAnalyzerTest();
282 std::unique_ptr<Module> M = makeLLVMModule(P, ModuleStr);
283 legacy::PassManager Passes;
284 Passes.add(P);
285 Passes.run(*M);
286
287 // Perform checks
288 Module::iterator MI = M->begin();
289 Function *F = &*MI++;
290 Function::iterator FI = F->begin();
291 FI++; // First basic block is entry - skip it.
292 BasicBlock *Header = &*FI++;
293
294 BasicBlock::iterator BBI = Header->begin();
295 std::advance(BBI, 3);
296 Instruction *Y1 = &*BBI++;
297 Instruction *Y2 = &*BBI++;
298 Instruction *Y3 = &*BBI++;
299 // Check simplification expected on the 5th iteration.
300 // "%se = sext i32 %const_array_element to i64" should be simplified to 259,
301 // "%ze = zext i32 %const_array_element to i64" should be simplified to 259,
302 // "%tr = trunc i32 %const_array_element to i8" should be simplified to 3.
303 auto I1 = SimplifiedValuesVector[5].find(Y1);
304 EXPECT_TRUE(I1 != SimplifiedValuesVector[5].end());
305 EXPECT_EQ(cast<ConstantInt>((*I1).second)->getZExtValue(), 259U);
306 auto I2 = SimplifiedValuesVector[5].find(Y2);
307 EXPECT_TRUE(I2 != SimplifiedValuesVector[5].end());
308 EXPECT_EQ(cast<ConstantInt>((*I2).second)->getZExtValue(), 259U);
309 auto I3 = SimplifiedValuesVector[5].find(Y3);
310 EXPECT_TRUE(I3 != SimplifiedValuesVector[5].end());
311 EXPECT_EQ(cast<ConstantInt>((*I3).second)->getZExtValue(), 3U);
312}
313
Michael Zolotukhin1da4afd2016-02-08 23:03:59 +0000314} // end namespace llvm
315
316INITIALIZE_PASS_BEGIN(UnrollAnalyzerTest, "unrollanalyzertestpass",
317 "unrollanalyzertestpass", false, false)
318INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
319INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
320INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)
321INITIALIZE_PASS_END(UnrollAnalyzerTest, "unrollanalyzertestpass",
322 "unrollanalyzertestpass", false, false)