blob: df8d2fac7e4e7e92b0a22980e5c51fb14b6c9112 [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());
109 EXPECT_EQ(dyn_cast<ConstantInt>((*I1).second)->getZExtValue(), 1U);
110
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());
114 EXPECT_FALSE(dyn_cast<ConstantInt>((*I2).second)->getZExtValue());
115
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());
120 EXPECT_EQ(dyn_cast<ConstantInt>((*I1).second)->getZExtValue(), TripCount);
121
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());
125 EXPECT_TRUE(dyn_cast<ConstantInt>((*I2).second)->getZExtValue());
126}
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 Zolotukhin1da4afd2016-02-08 23:03:59 +0000174} // end namespace llvm
175
176INITIALIZE_PASS_BEGIN(UnrollAnalyzerTest, "unrollanalyzertestpass",
177 "unrollanalyzertestpass", false, false)
178INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
179INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
180INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)
181INITIALIZE_PASS_END(UnrollAnalyzerTest, "unrollanalyzertestpass",
182 "unrollanalyzertestpass", false, false)