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