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