blob: 83d57f524690299d25893077cc11ae34338d2127 [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
Mehdi Amini03b42e42016-04-14 21:59:01 +000063std::unique_ptr<Module> makeLLVMModule(LLVMContext &Context,
64 UnrollAnalyzerTest *P,
Michael Zolotukhin1da4afd2016-02-08 23:03:59 +000065 const char *ModuleStr) {
Michael Zolotukhin1da4afd2016-02-08 23:03:59 +000066 SMDiagnostic Err;
Mehdi Amini03b42e42016-04-14 21:59:01 +000067 return parseAssemblyString(ModuleStr, Err, Context);
Michael Zolotukhin1da4afd2016-02-08 23:03:59 +000068}
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();
Mehdi Amini03b42e42016-04-14 21:59:01 +000089 LLVMContext Context;
90 std::unique_ptr<Module> M = makeLLVMModule(Context, P, ModuleStr);
Michael Zolotukhin1da4afd2016-02-08 23:03:59 +000091 legacy::PassManager Passes;
92 Passes.add(P);
93 Passes.run(*M);
94
95 // Perform checks
96 Module::iterator MI = M->begin();
97 Function *F = &*MI++;
98 Function::iterator FI = F->begin();
99 FI++; // First basic block is entry - skip it.
100 BasicBlock *Header = &*FI++;
101
102 BasicBlock::iterator BBI = Header->begin();
103 std::advance(BBI, 4);
104 Instruction *Y1 = &*BBI++;
105 Instruction *Y2 = &*BBI++;
106 // Check simplification expected on the 1st iteration.
107 // Check that "%inc = add nuw nsw i64 %iv, 1" is simplified to 1
108 auto I1 = SimplifiedValuesVector[0].find(Y1);
109 EXPECT_TRUE(I1 != SimplifiedValuesVector[0].end());
Michael Zolotukhin789ac452016-03-12 01:28:56 +0000110 EXPECT_EQ(cast<ConstantInt>((*I1).second)->getZExtValue(), 1U);
Michael Zolotukhin1da4afd2016-02-08 23:03:59 +0000111
112 // Check that "%cond = icmp sge i64 %inc, 10" is simplified to false
113 auto I2 = SimplifiedValuesVector[0].find(Y2);
114 EXPECT_TRUE(I2 != SimplifiedValuesVector[0].end());
Michael Zolotukhin789ac452016-03-12 01:28:56 +0000115 EXPECT_FALSE(cast<ConstantInt>((*I2).second)->getZExtValue());
Michael Zolotukhin1da4afd2016-02-08 23:03:59 +0000116
117 // Check simplification expected on the last iteration.
118 // Check that "%inc = add nuw nsw i64 %iv, 1" is simplified to 8
119 I1 = SimplifiedValuesVector[TripCount - 1].find(Y1);
120 EXPECT_TRUE(I1 != SimplifiedValuesVector[TripCount - 1].end());
Michael Zolotukhin789ac452016-03-12 01:28:56 +0000121 EXPECT_EQ(cast<ConstantInt>((*I1).second)->getZExtValue(), TripCount);
Michael Zolotukhin1da4afd2016-02-08 23:03:59 +0000122
123 // Check that "%cond = icmp sge i64 %inc, 10" is simplified to false
124 I2 = SimplifiedValuesVector[TripCount - 1].find(Y2);
125 EXPECT_TRUE(I2 != SimplifiedValuesVector[TripCount - 1].end());
Michael Zolotukhin789ac452016-03-12 01:28:56 +0000126 EXPECT_TRUE(cast<ConstantInt>((*I2).second)->getZExtValue());
Michael Zolotukhin1da4afd2016-02-08 23:03:59 +0000127}
Michael Zolotukhin9f520eb2016-02-26 02:57:05 +0000128
129TEST(UnrollAnalyzerTest, OuterLoopSimplification) {
130 const char *ModuleStr =
131 "target datalayout = \"e-m:o-i64:64-f80:128-n8:16:32:64-S128\"\n"
132 "define void @foo() {\n"
133 "entry:\n"
134 " br label %outer.loop\n"
135 "outer.loop:\n"
136 " %iv.outer = phi i64 [ 0, %entry ], [ %iv.outer.next, %outer.loop.latch ]\n"
137 " br label %inner.loop\n"
138 "inner.loop:\n"
139 " %iv.inner = phi i64 [ 0, %outer.loop ], [ %iv.inner.next, %inner.loop ]\n"
140 " %iv.inner.next = add nuw nsw i64 %iv.inner, 1\n"
141 " %exitcond.inner = icmp eq i64 %iv.inner.next, 1000\n"
142 " br i1 %exitcond.inner, label %outer.loop.latch, label %inner.loop\n"
143 "outer.loop.latch:\n"
144 " %iv.outer.next = add nuw nsw i64 %iv.outer, 1\n"
145 " %exitcond.outer = icmp eq i64 %iv.outer.next, 40\n"
146 " br i1 %exitcond.outer, label %exit, label %outer.loop\n"
147 "exit:\n"
148 " ret void\n"
149 "}\n";
150
151 UnrollAnalyzerTest *P = new UnrollAnalyzerTest();
Mehdi Amini03b42e42016-04-14 21:59:01 +0000152 LLVMContext Context;
153 std::unique_ptr<Module> M = makeLLVMModule(Context, P, ModuleStr);
Michael Zolotukhin9f520eb2016-02-26 02:57:05 +0000154 legacy::PassManager Passes;
155 Passes.add(P);
156 Passes.run(*M);
157
158 Module::iterator MI = M->begin();
159 Function *F = &*MI++;
160 Function::iterator FI = F->begin();
161 FI++;
162 BasicBlock *Header = &*FI++;
163 BasicBlock *InnerBody = &*FI++;
164
165 BasicBlock::iterator BBI = Header->begin();
166 Instruction *Y1 = &*BBI++;
167 BBI = InnerBody->begin();
168 Instruction *Y2 = &*BBI++;
169 // Check that we can simplify IV of the outer loop, but can't simplify the IV
170 // of the inner loop if we only know the iteration number of the outer loop.
171 auto I1 = SimplifiedValuesVector[0].find(Y1);
172 EXPECT_TRUE(I1 != SimplifiedValuesVector[0].end());
173 auto I2 = SimplifiedValuesVector[0].find(Y2);
174 EXPECT_TRUE(I2 == SimplifiedValuesVector[0].end());
175}
Michael Zolotukhin789ac452016-03-12 01:28:56 +0000176TEST(UnrollAnalyzerTest, CmpSimplifications) {
177 const char *ModuleStr =
178 "target datalayout = \"e-m:o-i64:64-f80:128-n8:16:32:64-S128\"\n"
179 "define void @branch_iv_trunc() {\n"
180 "entry:\n"
181 " br label %for.body\n"
182 "for.body:\n"
183 " %indvars.iv = phi i64 [ 0, %entry ], [ %tmp3, %for.body ]\n"
184 " %tmp2 = trunc i64 %indvars.iv to i32\n"
185 " %cmp3 = icmp eq i32 %tmp2, 5\n"
186 " %tmp3 = add nuw nsw i64 %indvars.iv, 1\n"
187 " %exitcond = icmp eq i64 %tmp3, 10\n"
188 " br i1 %exitcond, label %for.end, label %for.body\n"
189 "for.end:\n"
190 " ret void\n"
191 "}\n";
192 UnrollAnalyzerTest *P = new UnrollAnalyzerTest();
Mehdi Amini03b42e42016-04-14 21:59:01 +0000193 LLVMContext Context;
194 std::unique_ptr<Module> M = makeLLVMModule(Context, P, ModuleStr);
Michael Zolotukhin789ac452016-03-12 01:28:56 +0000195 legacy::PassManager Passes;
196 Passes.add(P);
197 Passes.run(*M);
198
199 // Perform checks
200 Module::iterator MI = M->begin();
201 Function *F = &*MI++;
202 Function::iterator FI = F->begin();
203 FI++; // First basic block is entry - skip it.
204 BasicBlock *Header = &*FI++;
205
206 BasicBlock::iterator BBI = Header->begin();
207 BBI++;
208 Instruction *Y1 = &*BBI++;
209 Instruction *Y2 = &*BBI++;
210 // Check simplification expected on the 5th iteration.
211 // Check that "%tmp2 = trunc i64 %indvars.iv to i32" is simplified to 5
212 // and "%cmp3 = icmp eq i32 %tmp2, 5" is simplified to 1 (i.e. true).
213 auto I1 = SimplifiedValuesVector[5].find(Y1);
214 EXPECT_TRUE(I1 != SimplifiedValuesVector[5].end());
215 EXPECT_EQ(cast<ConstantInt>((*I1).second)->getZExtValue(), 5U);
216 auto I2 = SimplifiedValuesVector[5].find(Y2);
217 EXPECT_TRUE(I2 != SimplifiedValuesVector[5].end());
218 EXPECT_EQ(cast<ConstantInt>((*I2).second)->getZExtValue(), 1U);
219}
220TEST(UnrollAnalyzerTest, PtrCmpSimplifications) {
221 const char *ModuleStr =
222 "target datalayout = \"e-m:o-i64:64-f80:128-n8:16:32:64-S128\"\n"
223 "define void @ptr_cmp(i8 *%a) {\n"
224 "entry:\n"
225 " %limit = getelementptr i8, i8* %a, i64 40\n"
226 " %start.iv2 = getelementptr i8, i8* %a, i64 7\n"
227 " br label %loop.body\n"
228 "loop.body:\n"
229 " %iv.0 = phi i8* [ %a, %entry ], [ %iv.1, %loop.body ]\n"
230 " %iv2.0 = phi i8* [ %start.iv2, %entry ], [ %iv2.1, %loop.body ]\n"
231 " %cmp = icmp eq i8* %iv2.0, %iv.0\n"
232 " %iv.1 = getelementptr inbounds i8, i8* %iv.0, i64 1\n"
233 " %iv2.1 = getelementptr inbounds i8, i8* %iv2.0, i64 1\n"
234 " %exitcond = icmp ne i8* %iv.1, %limit\n"
235 " br i1 %exitcond, label %loop.body, label %loop.exit\n"
236 "loop.exit:\n"
237 " ret void\n"
238 "}\n";
239 UnrollAnalyzerTest *P = new UnrollAnalyzerTest();
Mehdi Amini03b42e42016-04-14 21:59:01 +0000240 LLVMContext Context;
241 std::unique_ptr<Module> M = makeLLVMModule(Context, P, ModuleStr);
Michael Zolotukhin789ac452016-03-12 01:28:56 +0000242 legacy::PassManager Passes;
243 Passes.add(P);
244 Passes.run(*M);
245
246 // Perform checks
247 Module::iterator MI = M->begin();
248 Function *F = &*MI++;
249 Function::iterator FI = F->begin();
250 FI++; // First basic block is entry - skip it.
251 BasicBlock *Header = &*FI;
252
253 BasicBlock::iterator BBI = Header->begin();
254 std::advance(BBI, 2);
255 Instruction *Y1 = &*BBI;
256 // Check simplification expected on the 5th iteration.
257 // Check that "%cmp = icmp eq i8* %iv2.0, %iv.0" is simplified to 0.
258 auto I1 = SimplifiedValuesVector[5].find(Y1);
259 EXPECT_TRUE(I1 != SimplifiedValuesVector[5].end());
260 EXPECT_EQ(cast<ConstantInt>((*I1).second)->getZExtValue(), 0U);
261}
262TEST(UnrollAnalyzerTest, CastSimplifications) {
263 const char *ModuleStr =
264 "target datalayout = \"e-m:o-i64:64-f80:128-n8:16:32:64-S128\"\n"
265 "@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"
266 "define void @const_load_cast() {\n"
267 "entry:\n"
268 " br label %loop\n"
269 "\n"
270 "loop:\n"
271 " %iv = phi i64 [ 0, %entry ], [ %inc, %loop ]\n"
272 " %array_const_idx = getelementptr inbounds [10 x i32], [10 x i32]* @known_constant, i64 0, i64 %iv\n"
273 " %const_array_element = load i32, i32* %array_const_idx, align 4\n"
274 " %se = sext i32 %const_array_element to i64\n"
275 " %ze = zext i32 %const_array_element to i64\n"
276 " %tr = trunc i32 %const_array_element to i8\n"
277 " %inc = add nuw nsw i64 %iv, 1\n"
278 " %exitcond86.i = icmp eq i64 %inc, 10\n"
279 " br i1 %exitcond86.i, label %loop.end, label %loop\n"
280 "\n"
281 "loop.end:\n"
282 " ret void\n"
283 "}\n";
284
285 UnrollAnalyzerTest *P = new UnrollAnalyzerTest();
Mehdi Amini03b42e42016-04-14 21:59:01 +0000286 LLVMContext Context;
287 std::unique_ptr<Module> M = makeLLVMModule(Context, P, ModuleStr);
Michael Zolotukhin789ac452016-03-12 01:28:56 +0000288 legacy::PassManager Passes;
289 Passes.add(P);
290 Passes.run(*M);
291
292 // Perform checks
293 Module::iterator MI = M->begin();
294 Function *F = &*MI++;
295 Function::iterator FI = F->begin();
296 FI++; // First basic block is entry - skip it.
297 BasicBlock *Header = &*FI++;
298
299 BasicBlock::iterator BBI = Header->begin();
300 std::advance(BBI, 3);
301 Instruction *Y1 = &*BBI++;
302 Instruction *Y2 = &*BBI++;
303 Instruction *Y3 = &*BBI++;
304 // Check simplification expected on the 5th iteration.
305 // "%se = sext i32 %const_array_element to i64" should be simplified to 259,
306 // "%ze = zext i32 %const_array_element to i64" should be simplified to 259,
307 // "%tr = trunc i32 %const_array_element to i8" should be simplified to 3.
308 auto I1 = SimplifiedValuesVector[5].find(Y1);
309 EXPECT_TRUE(I1 != SimplifiedValuesVector[5].end());
310 EXPECT_EQ(cast<ConstantInt>((*I1).second)->getZExtValue(), 259U);
311 auto I2 = SimplifiedValuesVector[5].find(Y2);
312 EXPECT_TRUE(I2 != SimplifiedValuesVector[5].end());
313 EXPECT_EQ(cast<ConstantInt>((*I2).second)->getZExtValue(), 259U);
314 auto I3 = SimplifiedValuesVector[5].find(Y3);
315 EXPECT_TRUE(I3 != SimplifiedValuesVector[5].end());
316 EXPECT_EQ(cast<ConstantInt>((*I3).second)->getZExtValue(), 3U);
317}
318
Michael Zolotukhin1da4afd2016-02-08 23:03:59 +0000319} // end namespace llvm
320
321INITIALIZE_PASS_BEGIN(UnrollAnalyzerTest, "unrollanalyzertestpass",
322 "unrollanalyzertestpass", false, false)
323INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
324INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
325INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)
326INITIALIZE_PASS_END(UnrollAnalyzerTest, "unrollanalyzertestpass",
327 "unrollanalyzertestpass", false, false)