1 //===- LoopUtilsTest.cpp - Unit tests for LoopUtils -----------------------===//
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 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/Transforms/Utils/LoopUtils.h"
10 #include "llvm/AsmParser/Parser.h"
11 #include "llvm/Support/SourceMgr.h"
12 #include "gtest/gtest.h"
13 
14 using namespace llvm;
15 
16 static std::unique_ptr<Module> parseIR(LLVMContext &C, const char *IR) {
17   SMDiagnostic Err;
18   std::unique_ptr<Module> Mod = parseAssemblyString(IR, Err, C);
19   if (!Mod)
20     Err.print("LoopUtilsTests", errs());
21   return Mod;
22 }
23 
24 static void run(Module &M, StringRef FuncName,
25                 function_ref<void(Function &F, DominatorTree &DT,
26                                   ScalarEvolution &SE, LoopInfo &LI)>
27                     Test) {
28   Function *F = M.getFunction(FuncName);
29   DominatorTree DT(*F);
30   TargetLibraryInfoImpl TLII;
31   TargetLibraryInfo TLI(TLII);
32   AssumptionCache AC(*F);
33   LoopInfo LI(DT);
34   ScalarEvolution SE(*F, TLI, AC, DT, LI);
35   Test(*F, DT, SE, LI);
36 }
37 
38 TEST(LoopUtils, DeleteDeadLoopNest) {
39   LLVMContext C;
40   std::unique_ptr<Module> M =
41       parseIR(C, "define void @foo() {\n"
42                  "entry:\n"
43                  "  br label %for.i\n"
44                  "for.i:\n"
45                  "  %i = phi i64 [ 0, %entry ], [ %inc.i, %for.i.latch ]\n"
46                  "  br label %for.j\n"
47                  "for.j:\n"
48                  "  %j = phi i64 [ 0, %for.i ], [ %inc.j, %for.j ]\n"
49                  "  %inc.j = add nsw i64 %j, 1\n"
50                  "  %cmp.j = icmp slt i64 %inc.j, 100\n"
51                  "  br i1 %cmp.j, label %for.j, label %for.k.preheader\n"
52                  "for.k.preheader:\n"
53                  "  br label %for.k\n"
54                  "for.k:\n"
55                  "  %k = phi i64 [ %inc.k, %for.k ], [ 0, %for.k.preheader ]\n"
56                  "  %inc.k = add nsw i64 %k, 1\n"
57                  "  %cmp.k = icmp slt i64 %inc.k, 100\n"
58                  "  br i1 %cmp.k, label %for.k, label %for.i.latch\n"
59                  "for.i.latch:\n"
60                  "  %inc.i = add nsw i64 %i, 1\n"
61                  "  %cmp.i = icmp slt i64 %inc.i, 100\n"
62                  "  br i1 %cmp.i, label %for.i, label %for.end\n"
63                  "for.end:\n"
64                  "  ret void\n"
65                  "}\n");
66 
67   run(*M, "foo",
68       [&](Function &F, DominatorTree &DT, ScalarEvolution &SE, LoopInfo &LI) {
69         assert(LI.begin() != LI.end() && "Expecting loops in function F");
70         Loop *L = *LI.begin();
71         assert(L && L->getName() == "for.i" && "Expecting loop for.i");
72 
73         deleteDeadLoop(L, &DT, &SE, &LI);
74 
75         assert(DT.verify(DominatorTree::VerificationLevel::Fast) &&
76                "Expecting valid dominator tree");
77         LI.verify(DT);
78         assert(LI.begin() == LI.end() &&
79                "Expecting no loops left in function F");
80         SE.verify();
81 
82         Function::iterator FI = F.begin();
83         BasicBlock *Entry = &*(FI++);
84         assert(Entry->getName() == "entry" && "Expecting BasicBlock entry");
85         const BranchInst *BI = dyn_cast<BranchInst>(Entry->getTerminator());
86         assert(BI && "Expecting valid branch instruction");
87         EXPECT_EQ(BI->getNumSuccessors(), (unsigned)1);
88         EXPECT_EQ(BI->getSuccessor(0)->getName(), "for.end");
89       });
90 }
91