[LLDB] [Windows] Initial support for ARM64 register contexts
[llvm-core.git] / unittests / Analysis / UnrollAnalyzerTest.cpp
blobdfa42678ea8fe83924ab16ea2a97ffe69b94dff6
1 //===- UnrollAnalyzerTest.cpp - UnrollAnalyzer unit tests -----------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Analysis/LoopUnrollAnalyzer.h"
10 #include "llvm/AsmParser/Parser.h"
11 #include "llvm/IR/Dominators.h"
12 #include "llvm/IR/LegacyPassManager.h"
13 #include "llvm/Support/SourceMgr.h"
14 #include "gtest/gtest.h"
16 using namespace llvm;
17 namespace llvm {
18 void initializeUnrollAnalyzerTestPass(PassRegistry &);
20 static SmallVector<DenseMap<Value *, Constant *>, 16> SimplifiedValuesVector;
21 static unsigned TripCount = 0;
23 namespace {
24 struct UnrollAnalyzerTest : public FunctionPass {
25 static char ID;
26 bool runOnFunction(Function &F) override {
27 LoopInfo *LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
28 ScalarEvolution *SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
30 Function::iterator FI = F.begin();
31 FI++; // First basic block is entry - skip it.
32 BasicBlock *Header = &*FI++;
33 Loop *L = LI->getLoopFor(Header);
34 BasicBlock *Exiting = L->getExitingBlock();
36 SimplifiedValuesVector.clear();
37 TripCount = SE->getSmallConstantTripCount(L, Exiting);
38 for (unsigned Iteration = 0; Iteration < TripCount; Iteration++) {
39 DenseMap<Value *, Constant *> SimplifiedValues;
40 UnrolledInstAnalyzer Analyzer(Iteration, SimplifiedValues, *SE, L);
41 for (auto *BB : L->getBlocks())
42 for (Instruction &I : *BB)
43 Analyzer.visit(I);
44 SimplifiedValuesVector.push_back(SimplifiedValues);
46 return false;
48 void getAnalysisUsage(AnalysisUsage &AU) const override {
49 AU.addRequired<DominatorTreeWrapperPass>();
50 AU.addRequired<LoopInfoWrapperPass>();
51 AU.addRequired<ScalarEvolutionWrapperPass>();
52 AU.setPreservesAll();
54 UnrollAnalyzerTest() : FunctionPass(ID) {
55 initializeUnrollAnalyzerTestPass(*PassRegistry::getPassRegistry());
60 char UnrollAnalyzerTest::ID = 0;
62 std::unique_ptr<Module> makeLLVMModule(LLVMContext &Context,
63 const char *ModuleStr) {
64 SMDiagnostic Err;
65 return parseAssemblyString(ModuleStr, Err, Context);
68 TEST(UnrollAnalyzerTest, BasicSimplifications) {
69 const char *ModuleStr =
70 "target datalayout = \"e-m:o-i64:64-f80:128-n8:16:32:64-S128\"\n"
71 "define i64 @propagate_loop_phis() {\n"
72 "entry:\n"
73 " br label %loop\n"
74 "loop:\n"
75 " %iv = phi i64 [ 0, %entry ], [ %inc, %loop ]\n"
76 " %x0 = phi i64 [ 0, %entry ], [ %x2, %loop ]\n"
77 " %x1 = or i64 %x0, 1\n"
78 " %x2 = or i64 %x1, 2\n"
79 " %inc = add nuw nsw i64 %iv, 1\n"
80 " %cond = icmp sge i64 %inc, 8\n"
81 " br i1 %cond, label %loop.end, label %loop\n"
82 "loop.end:\n"
83 " %x.lcssa = phi i64 [ %x2, %loop ]\n"
84 " ret i64 %x.lcssa\n"
85 "}\n";
86 UnrollAnalyzerTest *P = new UnrollAnalyzerTest();
87 LLVMContext Context;
88 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleStr);
89 legacy::PassManager Passes;
90 Passes.add(P);
91 Passes.run(*M);
93 // Perform checks
94 Module::iterator MI = M->begin();
95 Function *F = &*MI++;
96 Function::iterator FI = F->begin();
97 FI++; // First basic block is entry - skip it.
98 BasicBlock *Header = &*FI++;
100 BasicBlock::iterator BBI = Header->begin();
101 std::advance(BBI, 4);
102 Instruction *Y1 = &*BBI++;
103 Instruction *Y2 = &*BBI++;
104 // Check simplification expected on the 1st iteration.
105 // Check that "%inc = add nuw nsw i64 %iv, 1" is simplified to 1
106 auto I1 = SimplifiedValuesVector[0].find(Y1);
107 EXPECT_TRUE(I1 != SimplifiedValuesVector[0].end());
108 EXPECT_EQ(cast<ConstantInt>((*I1).second)->getZExtValue(), 1U);
110 // Check that "%cond = icmp sge i64 %inc, 10" is simplified to false
111 auto I2 = SimplifiedValuesVector[0].find(Y2);
112 EXPECT_TRUE(I2 != SimplifiedValuesVector[0].end());
113 EXPECT_FALSE(cast<ConstantInt>((*I2).second)->getZExtValue());
115 // Check simplification expected on the last iteration.
116 // Check that "%inc = add nuw nsw i64 %iv, 1" is simplified to 8
117 I1 = SimplifiedValuesVector[TripCount - 1].find(Y1);
118 EXPECT_TRUE(I1 != SimplifiedValuesVector[TripCount - 1].end());
119 EXPECT_EQ(cast<ConstantInt>((*I1).second)->getZExtValue(), TripCount);
121 // Check that "%cond = icmp sge i64 %inc, 10" is simplified to false
122 I2 = SimplifiedValuesVector[TripCount - 1].find(Y2);
123 EXPECT_TRUE(I2 != SimplifiedValuesVector[TripCount - 1].end());
124 EXPECT_TRUE(cast<ConstantInt>((*I2).second)->getZExtValue());
127 TEST(UnrollAnalyzerTest, OuterLoopSimplification) {
128 const char *ModuleStr =
129 "target datalayout = \"e-m:o-i64:64-f80:128-n8:16:32:64-S128\"\n"
130 "define void @foo() {\n"
131 "entry:\n"
132 " br label %outer.loop\n"
133 "outer.loop:\n"
134 " %iv.outer = phi i64 [ 0, %entry ], [ %iv.outer.next, %outer.loop.latch ]\n"
135 " %iv.outer.next = add nuw nsw i64 %iv.outer, 1\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 " %exitcond.outer = icmp eq i64 %iv.outer.next, 40\n"
144 " br i1 %exitcond.outer, label %exit, label %outer.loop\n"
145 "exit:\n"
146 " ret void\n"
147 "}\n";
149 UnrollAnalyzerTest *P = new UnrollAnalyzerTest();
150 LLVMContext Context;
151 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleStr);
152 legacy::PassManager Passes;
153 Passes.add(P);
154 Passes.run(*M);
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++;
163 BasicBlock::iterator BBI = Header->begin();
164 BBI++;
165 Instruction *Y1 = &*BBI;
166 BBI = InnerBody->begin();
167 BBI++;
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.
172 // Y1 is %iv.outer.next, Y2 is %iv.inner.next
173 auto I1 = SimplifiedValuesVector[0].find(Y1);
174 EXPECT_TRUE(I1 != SimplifiedValuesVector[0].end());
175 auto I2 = SimplifiedValuesVector[0].find(Y2);
176 EXPECT_TRUE(I2 == SimplifiedValuesVector[0].end());
178 TEST(UnrollAnalyzerTest, CmpSimplifications) {
179 const char *ModuleStr =
180 "target datalayout = \"e-m:o-i64:64-f80:128-n8:16:32:64-S128\"\n"
181 "define void @branch_iv_trunc() {\n"
182 "entry:\n"
183 " br label %for.body\n"
184 "for.body:\n"
185 " %indvars.iv = phi i64 [ 0, %entry ], [ %tmp3, %for.body ]\n"
186 " %tmp2 = trunc i64 %indvars.iv to i32\n"
187 " %cmp3 = icmp eq i32 %tmp2, 5\n"
188 " %tmp3 = add nuw nsw i64 %indvars.iv, 1\n"
189 " %exitcond = icmp eq i64 %tmp3, 10\n"
190 " br i1 %exitcond, label %for.end, label %for.body\n"
191 "for.end:\n"
192 " ret void\n"
193 "}\n";
194 UnrollAnalyzerTest *P = new UnrollAnalyzerTest();
195 LLVMContext Context;
196 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleStr);
197 legacy::PassManager Passes;
198 Passes.add(P);
199 Passes.run(*M);
201 // Perform checks
202 Module::iterator MI = M->begin();
203 Function *F = &*MI++;
204 Function::iterator FI = F->begin();
205 FI++; // First basic block is entry - skip it.
206 BasicBlock *Header = &*FI++;
208 BasicBlock::iterator BBI = Header->begin();
209 BBI++;
210 Instruction *Y1 = &*BBI++;
211 Instruction *Y2 = &*BBI++;
212 // Check simplification expected on the 5th iteration.
213 // Check that "%tmp2 = trunc i64 %indvars.iv to i32" is simplified to 5
214 // and "%cmp3 = icmp eq i32 %tmp2, 5" is simplified to 1 (i.e. true).
215 auto I1 = SimplifiedValuesVector[5].find(Y1);
216 EXPECT_TRUE(I1 != SimplifiedValuesVector[5].end());
217 EXPECT_EQ(cast<ConstantInt>((*I1).second)->getZExtValue(), 5U);
218 auto I2 = SimplifiedValuesVector[5].find(Y2);
219 EXPECT_TRUE(I2 != SimplifiedValuesVector[5].end());
220 EXPECT_EQ(cast<ConstantInt>((*I2).second)->getZExtValue(), 1U);
222 TEST(UnrollAnalyzerTest, PtrCmpSimplifications) {
223 const char *ModuleStr =
224 "target datalayout = \"e-m:o-i64:64-f80:128-n8:16:32:64-S128\"\n"
225 "define void @ptr_cmp(i8 *%a) {\n"
226 "entry:\n"
227 " %limit = getelementptr i8, i8* %a, i64 40\n"
228 " %start.iv2 = getelementptr i8, i8* %a, i64 7\n"
229 " br label %loop.body\n"
230 "loop.body:\n"
231 " %iv.0 = phi i8* [ %a, %entry ], [ %iv.1, %loop.body ]\n"
232 " %iv2.0 = phi i8* [ %start.iv2, %entry ], [ %iv2.1, %loop.body ]\n"
233 " %cmp = icmp eq i8* %iv2.0, %iv.0\n"
234 " %iv.1 = getelementptr inbounds i8, i8* %iv.0, i64 1\n"
235 " %iv2.1 = getelementptr inbounds i8, i8* %iv2.0, i64 1\n"
236 " %exitcond = icmp ne i8* %iv.1, %limit\n"
237 " br i1 %exitcond, label %loop.body, label %loop.exit\n"
238 "loop.exit:\n"
239 " ret void\n"
240 "}\n";
241 UnrollAnalyzerTest *P = new UnrollAnalyzerTest();
242 LLVMContext Context;
243 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleStr);
244 legacy::PassManager Passes;
245 Passes.add(P);
246 Passes.run(*M);
248 // Perform checks
249 Module::iterator MI = M->begin();
250 Function *F = &*MI++;
251 Function::iterator FI = F->begin();
252 FI++; // First basic block is entry - skip it.
253 BasicBlock *Header = &*FI;
255 BasicBlock::iterator BBI = Header->begin();
256 std::advance(BBI, 2);
257 Instruction *Y1 = &*BBI;
258 // Check simplification expected on the 5th iteration.
259 // Check that "%cmp = icmp eq i8* %iv2.0, %iv.0" is simplified to 0.
260 auto I1 = SimplifiedValuesVector[5].find(Y1);
261 EXPECT_TRUE(I1 != SimplifiedValuesVector[5].end());
262 EXPECT_EQ(cast<ConstantInt>((*I1).second)->getZExtValue(), 0U);
264 TEST(UnrollAnalyzerTest, CastSimplifications) {
265 const char *ModuleStr =
266 "target datalayout = \"e-m:o-i64:64-f80:128-n8:16:32:64-S128\"\n"
267 "@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"
268 "define void @const_load_cast() {\n"
269 "entry:\n"
270 " br label %loop\n"
271 "\n"
272 "loop:\n"
273 " %iv = phi i64 [ 0, %entry ], [ %inc, %loop ]\n"
274 " %array_const_idx = getelementptr inbounds [10 x i32], [10 x i32]* @known_constant, i64 0, i64 %iv\n"
275 " %const_array_element = load i32, i32* %array_const_idx, align 4\n"
276 " %se = sext i32 %const_array_element to i64\n"
277 " %ze = zext i32 %const_array_element to i64\n"
278 " %tr = trunc i32 %const_array_element to i8\n"
279 " %inc = add nuw nsw i64 %iv, 1\n"
280 " %exitcond86.i = icmp eq i64 %inc, 10\n"
281 " br i1 %exitcond86.i, label %loop.end, label %loop\n"
282 "\n"
283 "loop.end:\n"
284 " ret void\n"
285 "}\n";
287 UnrollAnalyzerTest *P = new UnrollAnalyzerTest();
288 LLVMContext Context;
289 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleStr);
290 legacy::PassManager Passes;
291 Passes.add(P);
292 Passes.run(*M);
294 // Perform checks
295 Module::iterator MI = M->begin();
296 Function *F = &*MI++;
297 Function::iterator FI = F->begin();
298 FI++; // First basic block is entry - skip it.
299 BasicBlock *Header = &*FI++;
301 BasicBlock::iterator BBI = Header->begin();
302 std::advance(BBI, 3);
303 Instruction *Y1 = &*BBI++;
304 Instruction *Y2 = &*BBI++;
305 Instruction *Y3 = &*BBI++;
306 // Check simplification expected on the 5th iteration.
307 // "%se = sext i32 %const_array_element to i64" should be simplified to 259,
308 // "%ze = zext i32 %const_array_element to i64" should be simplified to 259,
309 // "%tr = trunc i32 %const_array_element to i8" should be simplified to 3.
310 auto I1 = SimplifiedValuesVector[5].find(Y1);
311 EXPECT_TRUE(I1 != SimplifiedValuesVector[5].end());
312 EXPECT_EQ(cast<ConstantInt>((*I1).second)->getZExtValue(), 259U);
313 auto I2 = SimplifiedValuesVector[5].find(Y2);
314 EXPECT_TRUE(I2 != SimplifiedValuesVector[5].end());
315 EXPECT_EQ(cast<ConstantInt>((*I2).second)->getZExtValue(), 259U);
316 auto I3 = SimplifiedValuesVector[5].find(Y3);
317 EXPECT_TRUE(I3 != SimplifiedValuesVector[5].end());
318 EXPECT_EQ(cast<ConstantInt>((*I3).second)->getZExtValue(), 3U);
321 } // end namespace llvm
323 INITIALIZE_PASS_BEGIN(UnrollAnalyzerTest, "unrollanalyzertestpass",
324 "unrollanalyzertestpass", false, false)
325 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
326 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
327 INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)
328 INITIALIZE_PASS_END(UnrollAnalyzerTest, "unrollanalyzertestpass",
329 "unrollanalyzertestpass", false, false)