1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
//===- LoopUtilsTest.cpp - Unit tests for LoopUtils -----------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "llvm/Transforms/Utils/LoopUtils.h"
#include "llvm/Analysis/AssumptionCache.h"
#include "llvm/Analysis/LoopInfo.h"
#include "llvm/Analysis/ScalarEvolution.h"
#include "llvm/Analysis/TargetLibraryInfo.h"
#include "llvm/AsmParser/Parser.h"
#include "llvm/IR/Dominators.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/SourceMgr.h"
#include "gtest/gtest.h"
using namespace llvm;
static std::unique_ptr<Module> parseIR(LLVMContext &C, const char *IR) {
SMDiagnostic Err;
std::unique_ptr<Module> Mod = parseAssemblyString(IR, Err, C);
if (!Mod)
Err.print("LoopUtilsTests", errs());
return Mod;
}
static void run(Module &M, StringRef FuncName,
function_ref<void(Function &F, DominatorTree &DT,
ScalarEvolution &SE, LoopInfo &LI)>
Test) {
Function *F = M.getFunction(FuncName);
DominatorTree DT(*F);
TargetLibraryInfoImpl TLII;
TargetLibraryInfo TLI(TLII);
AssumptionCache AC(*F);
LoopInfo LI(DT);
ScalarEvolution SE(*F, TLI, AC, DT, LI);
Test(*F, DT, SE, LI);
}
TEST(LoopUtils, DeleteDeadLoopNest) {
LLVMContext C;
std::unique_ptr<Module> M =
parseIR(C, "define void @foo() {\n"
"entry:\n"
" br label %for.i\n"
"for.i:\n"
" %i = phi i64 [ 0, %entry ], [ %inc.i, %for.i.latch ]\n"
" br label %for.j\n"
"for.j:\n"
" %j = phi i64 [ 0, %for.i ], [ %inc.j, %for.j ]\n"
" %inc.j = add nsw i64 %j, 1\n"
" %cmp.j = icmp slt i64 %inc.j, 100\n"
" br i1 %cmp.j, label %for.j, label %for.k.preheader\n"
"for.k.preheader:\n"
" br label %for.k\n"
"for.k:\n"
" %k = phi i64 [ %inc.k, %for.k ], [ 0, %for.k.preheader ]\n"
" %inc.k = add nsw i64 %k, 1\n"
" %cmp.k = icmp slt i64 %inc.k, 100\n"
" br i1 %cmp.k, label %for.k, label %for.i.latch\n"
"for.i.latch:\n"
" %inc.i = add nsw i64 %i, 1\n"
" %cmp.i = icmp slt i64 %inc.i, 100\n"
" br i1 %cmp.i, label %for.i, label %for.end\n"
"for.end:\n"
" ret void\n"
"}\n");
run(*M, "foo",
[&](Function &F, DominatorTree &DT, ScalarEvolution &SE, LoopInfo &LI) {
assert(LI.begin() != LI.end() && "Expecting loops in function F");
Loop *L = *LI.begin();
assert(L && L->getName() == "for.i" && "Expecting loop for.i");
deleteDeadLoop(L, &DT, &SE, &LI);
assert(DT.verify(DominatorTree::VerificationLevel::Fast) &&
"Expecting valid dominator tree");
LI.verify(DT);
assert(LI.begin() == LI.end() &&
"Expecting no loops left in function F");
SE.verify();
Function::iterator FI = F.begin();
BasicBlock *Entry = &*(FI++);
assert(Entry->getName() == "entry" && "Expecting BasicBlock entry");
const BranchInst *BI = dyn_cast<BranchInst>(Entry->getTerminator());
assert(BI && "Expecting valid branch instruction");
EXPECT_EQ(BI->getNumSuccessors(), (unsigned)1);
EXPECT_EQ(BI->getSuccessor(0)->getName(), "for.end");
});
}
TEST(LoopUtils, IsKnownPositiveInLoopTest) {
LLVMContext C;
std::unique_ptr<Module> M =
parseIR(C, "define void @foo(i32 %n, i1 %c) {\n"
"entry:\n"
" %is.positive = icmp sgt i32 %n, 0\n"
" br i1 %is.positive, label %loop, label %exit\n"
"loop:\n"
" br i1 %c, label %loop, label %exit\n"
"exit:\n"
" ret void\n"
"}\n");
run(*M, "foo",
[&](Function &F, DominatorTree &DT, ScalarEvolution &SE, LoopInfo &LI) {
assert(LI.begin() != LI.end() && "Expecting loops in function F");
Loop *L = *LI.begin();
assert(L && L->getName() == "loop" && "Expecting loop 'loop'");
auto *Arg = F.getArg(0);
const SCEV *ArgSCEV = SE.getSCEV(Arg);
EXPECT_EQ(isKnownPositiveInLoop(ArgSCEV, L, SE), true);
});
}
TEST(LoopUtils, IsKnownNonPositiveInLoopTest) {
LLVMContext C;
std::unique_ptr<Module> M =
parseIR(C, "define void @foo(i32 %n, i1 %c) {\n"
"entry:\n"
" %is.non.positive = icmp sle i32 %n, 0\n"
" br i1 %is.non.positive, label %loop, label %exit\n"
"loop:\n"
" br i1 %c, label %loop, label %exit\n"
"exit:\n"
" ret void\n"
"}\n");
run(*M, "foo",
[&](Function &F, DominatorTree &DT, ScalarEvolution &SE, LoopInfo &LI) {
assert(LI.begin() != LI.end() && "Expecting loops in function F");
Loop *L = *LI.begin();
assert(L && L->getName() == "loop" && "Expecting loop 'loop'");
auto *Arg = F.getArg(0);
const SCEV *ArgSCEV = SE.getSCEV(Arg);
EXPECT_EQ(isKnownNonPositiveInLoop(ArgSCEV, L, SE), true);
});
}
|