Imported Upstream version 5.18.0.234

Former-commit-id: 8071ec1a8c5eaa9be24b41745add19297608001f
This commit is contained in:
Xamarin Public Jenkins (auto-signing)
2019-01-08 08:22:36 +00:00
parent f32dbaf0b2
commit 212f6bafcb
28494 changed files with 359 additions and 3867025 deletions

View File

@@ -1,253 +0,0 @@
//===--- AliasAnalysisTest.cpp - Mixed TBAA unit tests --------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "llvm/Analysis/AliasAnalysis.h"
#include "llvm/ADT/SetVector.h"
#include "llvm/Analysis/AssumptionCache.h"
#include "llvm/Analysis/BasicAliasAnalysis.h"
#include "llvm/Analysis/TargetLibraryInfo.h"
#include "llvm/AsmParser/Parser.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/InstIterator.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/SourceMgr.h"
#include "gtest/gtest.h"
using namespace llvm;
// Set up some test passes.
namespace llvm {
void initializeAATestPassPass(PassRegistry&);
void initializeTestCustomAAWrapperPassPass(PassRegistry&);
}
namespace {
struct AATestPass : FunctionPass {
static char ID;
AATestPass() : FunctionPass(ID) {
initializeAATestPassPass(*PassRegistry::getPassRegistry());
}
void getAnalysisUsage(AnalysisUsage &AU) const override {
AU.addRequired<AAResultsWrapperPass>();
AU.setPreservesAll();
}
bool runOnFunction(Function &F) override {
AliasAnalysis &AA = getAnalysis<AAResultsWrapperPass>().getAAResults();
SetVector<Value *> Pointers;
for (Argument &A : F.args())
if (A.getType()->isPointerTy())
Pointers.insert(&A);
for (Instruction &I : instructions(F))
if (I.getType()->isPointerTy())
Pointers.insert(&I);
for (Value *P1 : Pointers)
for (Value *P2 : Pointers)
(void)AA.alias(P1, MemoryLocation::UnknownSize, P2,
MemoryLocation::UnknownSize);
return false;
}
};
}
char AATestPass::ID = 0;
INITIALIZE_PASS_BEGIN(AATestPass, "aa-test-pas", "Alias Analysis Test Pass",
false, true)
INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
INITIALIZE_PASS_END(AATestPass, "aa-test-pass", "Alias Analysis Test Pass",
false, true)
namespace {
/// A test customizable AA result. It merely accepts a callback to run whenever
/// it receives an alias query. Useful for testing that a particular AA result
/// is reached.
struct TestCustomAAResult : AAResultBase<TestCustomAAResult> {
friend AAResultBase<TestCustomAAResult>;
std::function<void()> CB;
explicit TestCustomAAResult(std::function<void()> CB)
: AAResultBase(), CB(std::move(CB)) {}
TestCustomAAResult(TestCustomAAResult &&Arg)
: AAResultBase(std::move(Arg)), CB(std::move(Arg.CB)) {}
bool invalidate(Function &, const PreservedAnalyses &) { return false; }
AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB) {
CB();
return MayAlias;
}
};
}
namespace {
/// A wrapper pass for the legacy pass manager to use with the above custom AA
/// result.
class TestCustomAAWrapperPass : public ImmutablePass {
std::function<void()> CB;
std::unique_ptr<TestCustomAAResult> Result;
public:
static char ID;
explicit TestCustomAAWrapperPass(
std::function<void()> CB = std::function<void()>())
: ImmutablePass(ID), CB(std::move(CB)) {
initializeTestCustomAAWrapperPassPass(*PassRegistry::getPassRegistry());
}
void getAnalysisUsage(AnalysisUsage &AU) const override {
AU.setPreservesAll();
AU.addRequired<TargetLibraryInfoWrapperPass>();
}
bool doInitialization(Module &M) override {
Result.reset(new TestCustomAAResult(std::move(CB)));
return true;
}
bool doFinalization(Module &M) override {
Result.reset();
return true;
}
TestCustomAAResult &getResult() { return *Result; }
const TestCustomAAResult &getResult() const { return *Result; }
};
}
char TestCustomAAWrapperPass::ID = 0;
INITIALIZE_PASS_BEGIN(TestCustomAAWrapperPass, "test-custom-aa",
"Test Custom AA Wrapper Pass", false, true)
INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
INITIALIZE_PASS_END(TestCustomAAWrapperPass, "test-custom-aa",
"Test Custom AA Wrapper Pass", false, true)
namespace {
class AliasAnalysisTest : public testing::Test {
protected:
LLVMContext C;
Module M;
TargetLibraryInfoImpl TLII;
TargetLibraryInfo TLI;
std::unique_ptr<AssumptionCache> AC;
std::unique_ptr<BasicAAResult> BAR;
std::unique_ptr<AAResults> AAR;
AliasAnalysisTest() : M("AliasAnalysisTest", C), TLI(TLII) {}
AAResults &getAAResults(Function &F) {
// Reset the Function AA results first to clear out any references.
AAR.reset(new AAResults(TLI));
// Build the various AA results and register them.
AC.reset(new AssumptionCache(F));
BAR.reset(new BasicAAResult(M.getDataLayout(), TLI, *AC));
AAR->addAAResult(*BAR);
return *AAR;
}
};
TEST_F(AliasAnalysisTest, getModRefInfo) {
// Setup function.
FunctionType *FTy =
FunctionType::get(Type::getVoidTy(C), std::vector<Type *>(), false);
auto *F = cast<Function>(M.getOrInsertFunction("f", FTy));
auto *BB = BasicBlock::Create(C, "entry", F);
auto IntType = Type::getInt32Ty(C);
auto PtrType = Type::getInt32PtrTy(C);
auto *Value = ConstantInt::get(IntType, 42);
auto *Addr = ConstantPointerNull::get(PtrType);
auto *Store1 = new StoreInst(Value, Addr, BB);
auto *Load1 = new LoadInst(Addr, "load", BB);
auto *Add1 = BinaryOperator::CreateAdd(Value, Value, "add", BB);
auto *VAArg1 = new VAArgInst(Addr, PtrType, "vaarg", BB);
auto *CmpXChg1 = new AtomicCmpXchgInst(
Addr, ConstantInt::get(IntType, 0), ConstantInt::get(IntType, 1),
AtomicOrdering::Monotonic, AtomicOrdering::Monotonic,
SyncScope::System, BB);
auto *AtomicRMW =
new AtomicRMWInst(AtomicRMWInst::Xchg, Addr, ConstantInt::get(IntType, 1),
AtomicOrdering::Monotonic, SyncScope::System, BB);
ReturnInst::Create(C, nullptr, BB);
auto &AA = getAAResults(*F);
// Check basic results
EXPECT_EQ(AA.getModRefInfo(Store1, MemoryLocation()), ModRefInfo::Mod);
EXPECT_EQ(AA.getModRefInfo(Store1, None), ModRefInfo::Mod);
EXPECT_EQ(AA.getModRefInfo(Load1, MemoryLocation()), ModRefInfo::Ref);
EXPECT_EQ(AA.getModRefInfo(Load1, None), ModRefInfo::Ref);
EXPECT_EQ(AA.getModRefInfo(Add1, MemoryLocation()), ModRefInfo::NoModRef);
EXPECT_EQ(AA.getModRefInfo(Add1, None), ModRefInfo::NoModRef);
EXPECT_EQ(AA.getModRefInfo(VAArg1, MemoryLocation()), ModRefInfo::ModRef);
EXPECT_EQ(AA.getModRefInfo(VAArg1, None), ModRefInfo::ModRef);
EXPECT_EQ(AA.getModRefInfo(CmpXChg1, MemoryLocation()), ModRefInfo::ModRef);
EXPECT_EQ(AA.getModRefInfo(CmpXChg1, None), ModRefInfo::ModRef);
EXPECT_EQ(AA.getModRefInfo(AtomicRMW, MemoryLocation()), ModRefInfo::ModRef);
EXPECT_EQ(AA.getModRefInfo(AtomicRMW, None), ModRefInfo::ModRef);
}
class AAPassInfraTest : public testing::Test {
protected:
LLVMContext C;
SMDiagnostic Err;
std::unique_ptr<Module> M;
public:
AAPassInfraTest()
: M(parseAssemblyString("define i32 @f(i32* %x, i32* %y) {\n"
"entry:\n"
" %lx = load i32, i32* %x\n"
" %ly = load i32, i32* %y\n"
" %sum = add i32 %lx, %ly\n"
" ret i32 %sum\n"
"}\n",
Err, C)) {
assert(M && "Failed to build the module!");
}
};
TEST_F(AAPassInfraTest, injectExternalAA) {
legacy::PassManager PM;
// Register our custom AA's wrapper pass manually.
bool IsCustomAAQueried = false;
PM.add(new TestCustomAAWrapperPass([&] { IsCustomAAQueried = true; }));
// Now add the external AA wrapper with a lambda which queries for the
// wrapper around our custom AA and adds it to the results.
PM.add(createExternalAAWrapperPass([](Pass &P, Function &, AAResults &AAR) {
if (auto *WrapperPass = P.getAnalysisIfAvailable<TestCustomAAWrapperPass>())
AAR.addAAResult(WrapperPass->getResult());
}));
// And run a pass that will make some alias queries. This will automatically
// trigger the rest of the alias analysis stack to be run. It is analagous to
// building a full pass pipeline with any of the existing pass manager
// builders.
PM.add(new AATestPass());
PM.run(*M);
// Finally, ensure that our custom AA was indeed queried.
EXPECT_TRUE(IsCustomAAQueried);
}
} // end anonymous namspace

View File

@@ -1,87 +0,0 @@
//=======- AliasSetTrackerTest.cpp - Unit test for the Alias Set Tracker -===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "llvm/Analysis/AliasAnalysis.h"
#include "llvm/Analysis/AliasSetTracker.h"
#include "llvm/Analysis/TypeBasedAliasAnalysis.h"
#include "llvm/AsmParser/Parser.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/SourceMgr.h"
#include "gtest/gtest.h"
using namespace llvm;
TEST(AliasSetTracker, AliasUnknownInst) {
StringRef Assembly = R"(
@a = common global i32 0, align 4
@b = common global float 0.000000e+00, align 4
; Function Attrs: nounwind ssp uwtable
define i32 @read_a() #0 {
%1 = load i32, i32* @a, align 4, !tbaa !3
ret i32 %1
}
; Function Attrs: nounwind ssp uwtable
define void @write_b() #0 {
store float 1.000000e+01, float* @b, align 4, !tbaa !7
ret void
}
; Function Attrs: nounwind ssp uwtable
define void @test() #0 {
%1 = call i32 @read_a(), !tbaa !3
call void @write_b(), !tbaa !7
ret void
}
!3 = !{!4, !4, i64 0}
!4 = !{!"int", !5, i64 0}
!5 = !{!"omnipotent char", !6, i64 0}
!6 = !{!"Simple C/C++ TBAA"}
!7 = !{!8, !8, i64 0}
!8 = !{!"float", !5, i64 0}
)";
// Parse the IR. The two calls in @test can not access aliasing elements.
LLVMContext Context;
SMDiagnostic Error;
auto M = parseAssemblyString(Assembly, Error, Context);
ASSERT_TRUE(M) << "Bad assembly?";
// Initialize the alias result.
Triple Trip(M->getTargetTriple());
TargetLibraryInfoImpl TLII(Trip);
TargetLibraryInfo TLI(TLII);
AAResults AA(TLI);
TypeBasedAAResult TBAAR;
AA.addAAResult(TBAAR);
// Initialize the alias set tracker for the @test function.
Function *Test = M->getFunction("test");
ASSERT_NE(Test, nullptr);
AliasSetTracker AST(AA);
for (auto &BB : *Test)
AST.add(BB);
// There should be 2 disjoint alias sets. 1 from each call.
ASSERT_EQ((int)AST.getAliasSets().size(), 2);
// Directly test aliasesUnknownInst.
// Now every call instruction should only alias one alias set.
for (auto &Inst : *Test->begin()) {
bool FoundAS = false;
for (AliasSet &AS : AST) {
if (!AS.aliasesUnknownInst(&Inst, AA))
continue;
ASSERT_NE(FoundAS, true);
FoundAS = true;
}
}
}

View File

@@ -1,93 +0,0 @@
//===- BlockFrequencyInfoTest.cpp - BlockFrequencyInfo unit tests ---------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "llvm/Analysis/BlockFrequencyInfo.h"
#include "llvm/Analysis/BranchProbabilityInfo.h"
#include "llvm/Analysis/LoopInfo.h"
#include "llvm/AsmParser/Parser.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/Dominators.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/DataTypes.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/Support/raw_ostream.h"
#include "gtest/gtest.h"
namespace llvm {
namespace {
class BlockFrequencyInfoTest : public testing::Test {
protected:
std::unique_ptr<BranchProbabilityInfo> BPI;
std::unique_ptr<DominatorTree> DT;
std::unique_ptr<LoopInfo> LI;
LLVMContext C;
BlockFrequencyInfo buildBFI(Function &F) {
DT.reset(new DominatorTree(F));
LI.reset(new LoopInfo(*DT));
BPI.reset(new BranchProbabilityInfo(F, *LI));
return BlockFrequencyInfo(F, *BPI, *LI);
}
std::unique_ptr<Module> makeLLVMModule() {
const char *ModuleStrig = "define i32 @f(i32 %x) {\n"
"bb0:\n"
" %y1 = icmp eq i32 %x, 0 \n"
" br i1 %y1, label %bb1, label %bb2 \n"
"bb1:\n"
" br label %bb3\n"
"bb2:\n"
" br label %bb3\n"
"bb3:\n"
" %y2 = phi i32 [0, %bb1], [1, %bb2] \n"
" ret i32 %y2\n"
"}\n";
SMDiagnostic Err;
return parseAssemblyString(ModuleStrig, Err, C);
}
};
TEST_F(BlockFrequencyInfoTest, Basic) {
auto M = makeLLVMModule();
Function *F = M->getFunction("f");
F->setEntryCount(100);
BlockFrequencyInfo BFI = buildBFI(*F);
BasicBlock &BB0 = F->getEntryBlock();
BasicBlock *BB1 = BB0.getTerminator()->getSuccessor(0);
BasicBlock *BB2 = BB0.getTerminator()->getSuccessor(1);
BasicBlock *BB3 = BB1->getSingleSuccessor();
uint64_t BB0Freq = BFI.getBlockFreq(&BB0).getFrequency();
uint64_t BB1Freq = BFI.getBlockFreq(BB1).getFrequency();
uint64_t BB2Freq = BFI.getBlockFreq(BB2).getFrequency();
uint64_t BB3Freq = BFI.getBlockFreq(BB3).getFrequency();
EXPECT_EQ(BB0Freq, BB3Freq);
EXPECT_EQ(BB0Freq, BB1Freq + BB2Freq);
EXPECT_EQ(BB0Freq, BB3Freq);
EXPECT_EQ(BFI.getBlockProfileCount(&BB0).getValue(), UINT64_C(100));
EXPECT_EQ(BFI.getBlockProfileCount(BB3).getValue(), UINT64_C(100));
EXPECT_EQ(BFI.getBlockProfileCount(BB1).getValue(), 100 * BB1Freq / BB0Freq);
EXPECT_EQ(BFI.getBlockProfileCount(BB2).getValue(), 100 * BB2Freq / BB0Freq);
// Scale the frequencies of BB0, BB1 and BB2 by a factor of two.
SmallPtrSet<BasicBlock *, 4> BlocksToScale({BB1, BB2});
BFI.setBlockFreqAndScale(&BB0, BB0Freq * 2, BlocksToScale);
EXPECT_EQ(BFI.getBlockFreq(&BB0).getFrequency(), 2 * BB0Freq);
EXPECT_EQ(BFI.getBlockFreq(BB1).getFrequency(), 2 * BB1Freq);
EXPECT_EQ(BFI.getBlockFreq(BB2).getFrequency(), 2 * BB2Freq);
EXPECT_EQ(BFI.getBlockFreq(BB3).getFrequency(), BB3Freq);
}
} // end anonymous namespace
} // end namespace llvm

View File

@@ -1,88 +0,0 @@
//===- BranchProbabilityInfoTest.cpp - BranchProbabilityInfo unit tests ---===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "llvm/Analysis/BranchProbabilityInfo.h"
#include "llvm/Analysis/LoopInfo.h"
#include "llvm/AsmParser/Parser.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/Dominators.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/DataTypes.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/Support/raw_ostream.h"
#include "gtest/gtest.h"
namespace llvm {
namespace {
struct BranchProbabilityInfoTest : public testing::Test {
std::unique_ptr<BranchProbabilityInfo> BPI;
std::unique_ptr<DominatorTree> DT;
std::unique_ptr<LoopInfo> LI;
LLVMContext C;
BranchProbabilityInfo &buildBPI(Function &F) {
DT.reset(new DominatorTree(F));
LI.reset(new LoopInfo(*DT));
BPI.reset(new BranchProbabilityInfo(F, *LI));
return *BPI;
}
std::unique_ptr<Module> makeLLVMModule() {
const char *ModuleString = "define void @f() { exit: ret void }\n";
SMDiagnostic Err;
return parseAssemblyString(ModuleString, Err, C);
}
};
TEST_F(BranchProbabilityInfoTest, StressUnreachableHeuristic) {
auto M = makeLLVMModule();
Function *F = M->getFunction("f");
// define void @f() {
// entry:
// switch i32 undef, label %exit, [
// i32 0, label %preexit
// ... ;;< Add lots of cases to stress the heuristic.
// ]
// preexit:
// unreachable
// exit:
// ret void
// }
auto *ExitBB = &F->back();
auto *EntryBB = BasicBlock::Create(C, "entry", F, /*insertBefore=*/ExitBB);
auto *PreExitBB =
BasicBlock::Create(C, "preexit", F, /*insertBefore=*/ExitBB);
new UnreachableInst(C, PreExitBB);
unsigned NumCases = 4096;
auto *I32 = IntegerType::get(C, 32);
auto *Undef = UndefValue::get(I32);
auto *Switch = SwitchInst::Create(Undef, ExitBB, NumCases, EntryBB);
for (unsigned I = 0; I < NumCases; ++I)
Switch->addCase(ConstantInt::get(I32, I), PreExitBB);
BranchProbabilityInfo &BPI = buildBPI(*F);
// FIXME: This doesn't seem optimal. Since all of the cases handled by the
// switch have the *same* destination block ("preexit"), shouldn't it be the
// hot one? I'd expect the results to be reversed here...
EXPECT_FALSE(BPI.isEdgeHot(EntryBB, PreExitBB));
EXPECT_TRUE(BPI.isEdgeHot(EntryBB, ExitBB));
}
} // end anonymous namespace
} // end namespace llvm

View File

@@ -1,388 +0,0 @@
//===- CFGTest.cpp - CFG tests --------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "llvm/Analysis/CFG.h"
#include "llvm/Analysis/LoopInfo.h"
#include "llvm/AsmParser/Parser.h"
#include "llvm/IR/Dominators.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/InstIterator.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/IR/Module.h"
#include "llvm/Pass.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/SourceMgr.h"
#include "gtest/gtest.h"
using namespace llvm;
namespace {
// This fixture assists in running the isPotentiallyReachable utility four ways
// and ensuring it produces the correct answer each time.
class IsPotentiallyReachableTest : public testing::Test {
protected:
void ParseAssembly(const char *Assembly) {
SMDiagnostic Error;
M = parseAssemblyString(Assembly, Error, Context);
std::string errMsg;
raw_string_ostream os(errMsg);
Error.print("", os);
// A failure here means that the test itself is buggy.
if (!M)
report_fatal_error(os.str().c_str());
Function *F = M->getFunction("test");
if (F == nullptr)
report_fatal_error("Test must have a function named @test");
A = B = nullptr;
for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
if (I->hasName()) {
if (I->getName() == "A")
A = &*I;
else if (I->getName() == "B")
B = &*I;
}
}
if (A == nullptr)
report_fatal_error("@test must have an instruction %A");
if (B == nullptr)
report_fatal_error("@test must have an instruction %B");
}
void ExpectPath(bool ExpectedResult) {
static char ID;
class IsPotentiallyReachableTestPass : public FunctionPass {
public:
IsPotentiallyReachableTestPass(bool ExpectedResult,
Instruction *A, Instruction *B)
: FunctionPass(ID), ExpectedResult(ExpectedResult), A(A), B(B) {}
static int initialize() {
PassInfo *PI = new PassInfo("isPotentiallyReachable testing pass",
"", &ID, nullptr, true, true);
PassRegistry::getPassRegistry()->registerPass(*PI, false);
initializeLoopInfoWrapperPassPass(*PassRegistry::getPassRegistry());
initializeDominatorTreeWrapperPassPass(
*PassRegistry::getPassRegistry());
return 0;
}
void getAnalysisUsage(AnalysisUsage &AU) const override {
AU.setPreservesAll();
AU.addRequired<LoopInfoWrapperPass>();
AU.addRequired<DominatorTreeWrapperPass>();
}
bool runOnFunction(Function &F) override {
if (!F.hasName() || F.getName() != "test")
return false;
LoopInfo *LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
DominatorTree *DT =
&getAnalysis<DominatorTreeWrapperPass>().getDomTree();
EXPECT_EQ(isPotentiallyReachable(A, B, nullptr, nullptr),
ExpectedResult);
EXPECT_EQ(isPotentiallyReachable(A, B, DT, nullptr), ExpectedResult);
EXPECT_EQ(isPotentiallyReachable(A, B, nullptr, LI), ExpectedResult);
EXPECT_EQ(isPotentiallyReachable(A, B, DT, LI), ExpectedResult);
return false;
}
bool ExpectedResult;
Instruction *A, *B;
};
static int initialize = IsPotentiallyReachableTestPass::initialize();
(void)initialize;
IsPotentiallyReachableTestPass *P =
new IsPotentiallyReachableTestPass(ExpectedResult, A, B);
legacy::PassManager PM;
PM.add(P);
PM.run(*M);
}
LLVMContext Context;
std::unique_ptr<Module> M;
Instruction *A, *B;
};
}
TEST_F(IsPotentiallyReachableTest, SameBlockNoPath) {
ParseAssembly(
"define void @test() {\n"
"entry:\n"
" bitcast i8 undef to i8\n"
" %B = bitcast i8 undef to i8\n"
" bitcast i8 undef to i8\n"
" bitcast i8 undef to i8\n"
" %A = bitcast i8 undef to i8\n"
" ret void\n"
"}\n");
ExpectPath(false);
}
TEST_F(IsPotentiallyReachableTest, SameBlockPath) {
ParseAssembly(
"define void @test() {\n"
"entry:\n"
" %A = bitcast i8 undef to i8\n"
" bitcast i8 undef to i8\n"
" bitcast i8 undef to i8\n"
" %B = bitcast i8 undef to i8\n"
" ret void\n"
"}\n");
ExpectPath(true);
}
TEST_F(IsPotentiallyReachableTest, SameBlockNoLoop) {
ParseAssembly(
"define void @test() {\n"
"entry:\n"
" br label %middle\n"
"middle:\n"
" %B = bitcast i8 undef to i8\n"
" bitcast i8 undef to i8\n"
" bitcast i8 undef to i8\n"
" %A = bitcast i8 undef to i8\n"
" br label %nextblock\n"
"nextblock:\n"
" ret void\n"
"}\n");
ExpectPath(false);
}
TEST_F(IsPotentiallyReachableTest, StraightNoPath) {
ParseAssembly(
"define void @test() {\n"
"entry:\n"
" %B = bitcast i8 undef to i8\n"
" br label %exit\n"
"exit:\n"
" %A = bitcast i8 undef to i8\n"
" ret void\n"
"}");
ExpectPath(false);
}
TEST_F(IsPotentiallyReachableTest, StraightPath) {
ParseAssembly(
"define void @test() {\n"
"entry:\n"
" %A = bitcast i8 undef to i8\n"
" br label %exit\n"
"exit:\n"
" %B = bitcast i8 undef to i8\n"
" ret void\n"
"}");
ExpectPath(true);
}
TEST_F(IsPotentiallyReachableTest, DestUnreachable) {
ParseAssembly(
"define void @test() {\n"
"entry:\n"
" br label %midblock\n"
"midblock:\n"
" %A = bitcast i8 undef to i8\n"
" ret void\n"
"unreachable:\n"
" %B = bitcast i8 undef to i8\n"
" br label %midblock\n"
"}");
ExpectPath(false);
}
TEST_F(IsPotentiallyReachableTest, BranchToReturn) {
ParseAssembly(
"define void @test(i1 %x) {\n"
"entry:\n"
" %A = bitcast i8 undef to i8\n"
" br i1 %x, label %block1, label %block2\n"
"block1:\n"
" ret void\n"
"block2:\n"
" %B = bitcast i8 undef to i8\n"
" ret void\n"
"}");
ExpectPath(true);
}
TEST_F(IsPotentiallyReachableTest, SimpleLoop1) {
ParseAssembly(
"declare i1 @switch()\n"
"\n"
"define void @test() {\n"
"entry:\n"
" br label %loop\n"
"loop:\n"
" %B = bitcast i8 undef to i8\n"
" %A = bitcast i8 undef to i8\n"
" %x = call i1 @switch()\n"
" br i1 %x, label %loop, label %exit\n"
"exit:\n"
" ret void\n"
"}");
ExpectPath(true);
}
TEST_F(IsPotentiallyReachableTest, SimpleLoop2) {
ParseAssembly(
"declare i1 @switch()\n"
"\n"
"define void @test() {\n"
"entry:\n"
" %B = bitcast i8 undef to i8\n"
" br label %loop\n"
"loop:\n"
" %A = bitcast i8 undef to i8\n"
" %x = call i1 @switch()\n"
" br i1 %x, label %loop, label %exit\n"
"exit:\n"
" ret void\n"
"}");
ExpectPath(false);
}
TEST_F(IsPotentiallyReachableTest, SimpleLoop3) {
ParseAssembly(
"declare i1 @switch()\n"
"\n"
"define void @test() {\n"
"entry:\n"
" br label %loop\n"
"loop:\n"
" %B = bitcast i8 undef to i8\n"
" %x = call i1 @switch()\n"
" br i1 %x, label %loop, label %exit\n"
"exit:\n"
" %A = bitcast i8 undef to i8\n"
" ret void\n"
"}");
ExpectPath(false);
}
TEST_F(IsPotentiallyReachableTest, OneLoopAfterTheOther1) {
ParseAssembly(
"declare i1 @switch()\n"
"\n"
"define void @test() {\n"
"entry:\n"
" br label %loop1\n"
"loop1:\n"
" %A = bitcast i8 undef to i8\n"
" %x = call i1 @switch()\n"
" br i1 %x, label %loop1, label %loop1exit\n"
"loop1exit:\n"
" br label %loop2\n"
"loop2:\n"
" %B = bitcast i8 undef to i8\n"
" %y = call i1 @switch()\n"
" br i1 %x, label %loop2, label %loop2exit\n"
"loop2exit:"
" ret void\n"
"}");
ExpectPath(true);
}
TEST_F(IsPotentiallyReachableTest, OneLoopAfterTheOther2) {
ParseAssembly(
"declare i1 @switch()\n"
"\n"
"define void @test() {\n"
"entry:\n"
" br label %loop1\n"
"loop1:\n"
" %B = bitcast i8 undef to i8\n"
" %x = call i1 @switch()\n"
" br i1 %x, label %loop1, label %loop1exit\n"
"loop1exit:\n"
" br label %loop2\n"
"loop2:\n"
" %A = bitcast i8 undef to i8\n"
" %y = call i1 @switch()\n"
" br i1 %x, label %loop2, label %loop2exit\n"
"loop2exit:"
" ret void\n"
"}");
ExpectPath(false);
}
TEST_F(IsPotentiallyReachableTest, OneLoopAfterTheOtherInsideAThirdLoop) {
ParseAssembly(
"declare i1 @switch()\n"
"\n"
"define void @test() {\n"
"entry:\n"
" br label %outerloop3\n"
"outerloop3:\n"
" br label %innerloop1\n"
"innerloop1:\n"
" %B = bitcast i8 undef to i8\n"
" %x = call i1 @switch()\n"
" br i1 %x, label %innerloop1, label %innerloop1exit\n"
"innerloop1exit:\n"
" br label %innerloop2\n"
"innerloop2:\n"
" %A = bitcast i8 undef to i8\n"
" %y = call i1 @switch()\n"
" br i1 %x, label %innerloop2, label %innerloop2exit\n"
"innerloop2exit:"
" ;; In outer loop3 now.\n"
" %z = call i1 @switch()\n"
" br i1 %z, label %outerloop3, label %exit\n"
"exit:\n"
" ret void\n"
"}");
ExpectPath(true);
}
static const char *BranchInsideLoopIR =
"declare i1 @switch()\n"
"\n"
"define void @test() {\n"
"entry:\n"
" br label %loop\n"
"loop:\n"
" %x = call i1 @switch()\n"
" br i1 %x, label %nextloopblock, label %exit\n"
"nextloopblock:\n"
" %y = call i1 @switch()\n"
" br i1 %y, label %left, label %right\n"
"left:\n"
" %A = bitcast i8 undef to i8\n"
" br label %loop\n"
"right:\n"
" %B = bitcast i8 undef to i8\n"
" br label %loop\n"
"exit:\n"
" ret void\n"
"}";
TEST_F(IsPotentiallyReachableTest, BranchInsideLoop) {
ParseAssembly(BranchInsideLoopIR);
ExpectPath(true);
}
TEST_F(IsPotentiallyReachableTest, ModifyTest) {
ParseAssembly(BranchInsideLoopIR);
succ_iterator S = succ_begin(&*++M->getFunction("test")->begin());
BasicBlock *OldBB = S[0];
S[0] = S[1];
ExpectPath(false);
S[0] = OldBB;
ExpectPath(true);
}

File diff suppressed because it is too large Load Diff

View File

@@ -1,30 +0,0 @@
set(LLVM_LINK_COMPONENTS
Analysis
AsmParser
Core
Support
)
add_llvm_unittest(AnalysisTests
AliasAnalysisTest.cpp
AliasSetTrackerTest.cpp
BlockFrequencyInfoTest.cpp
BranchProbabilityInfoTest.cpp
CallGraphTest.cpp
CFGTest.cpp
CGSCCPassManagerTest.cpp
GlobalsModRefTest.cpp
ValueLatticeTest.cpp
LazyCallGraphTest.cpp
LoopInfoTest.cpp
MemoryBuiltinsTest.cpp
MemorySSA.cpp
OrderedBasicBlockTest.cpp
ProfileSummaryInfoTest.cpp
ScalarEvolutionTest.cpp
SparsePropagation.cpp
TargetLibraryInfoTest.cpp
TBAATest.cpp
UnrollAnalyzer.cpp
ValueTrackingTest.cpp
)

View File

@@ -1,61 +0,0 @@
//=======- CallGraphTest.cpp - Unit tests for the CG analysis -------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "llvm/Analysis/CallGraph.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "gtest/gtest.h"
using namespace llvm;
namespace {
template <typename Ty> void canSpecializeGraphTraitsIterators(Ty *G) {
typedef typename GraphTraits<Ty *>::NodeRef NodeRef;
auto I = GraphTraits<Ty *>::nodes_begin(G);
auto E = GraphTraits<Ty *>::nodes_end(G);
auto X = ++I;
// Should be able to iterate over all nodes of the graph.
static_assert(std::is_same<decltype(*I), NodeRef>::value,
"Node type does not match");
static_assert(std::is_same<decltype(*X), NodeRef>::value,
"Node type does not match");
static_assert(std::is_same<decltype(*E), NodeRef>::value,
"Node type does not match");
NodeRef N = GraphTraits<Ty *>::getEntryNode(G);
auto S = GraphTraits<NodeRef>::child_begin(N);
auto F = GraphTraits<NodeRef>::child_end(N);
// Should be able to iterate over immediate successors of a node.
static_assert(std::is_same<decltype(*S), NodeRef>::value,
"Node type does not match");
static_assert(std::is_same<decltype(*F), NodeRef>::value,
"Node type does not match");
}
TEST(CallGraphTest, GraphTraitsSpecialization) {
LLVMContext Context;
Module M("", Context);
CallGraph CG(M);
canSpecializeGraphTraitsIterators(&CG);
}
TEST(CallGraphTest, GraphTraitsConstSpecialization) {
LLVMContext Context;
Module M("", Context);
CallGraph CG(M);
canSpecializeGraphTraitsIterators(const_cast<const CallGraph *>(&CG));
}
}

View File

@@ -1,55 +0,0 @@
//===--- GlobalsModRefTest.cpp - Mixed TBAA unit tests --------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "llvm/Analysis/GlobalsModRef.h"
#include "llvm/AsmParser/Parser.h"
#include "llvm/Support/SourceMgr.h"
#include "gtest/gtest.h"
using namespace llvm;
TEST(GlobalsModRef, OptNone) {
StringRef Assembly = R"(
define void @f1() optnone {
ret void
}
define void @f2() optnone readnone {
ret void
}
define void @f3() optnone readonly {
ret void
}
)";
LLVMContext Context;
SMDiagnostic Error;
auto M = parseAssemblyString(Assembly, Error, Context);
ASSERT_TRUE(M) << "Bad assembly?";
const auto &funcs = M->functions();
auto I = funcs.begin();
ASSERT_NE(I, funcs.end());
const Function &F1 = *I;
ASSERT_NE(++I, funcs.end());
const Function &F2 = *I;
ASSERT_NE(++I, funcs.end());
const Function &F3 = *I;
EXPECT_EQ(++I, funcs.end());
Triple Trip(M->getTargetTriple());
TargetLibraryInfoImpl TLII(Trip);
TargetLibraryInfo TLI(TLII);
llvm::CallGraph CG(*M);
auto AAR = GlobalsAAResult::analyzeModule(*M, TLI, CG);
EXPECT_EQ(FMRB_UnknownModRefBehavior, AAR.getModRefBehavior(&F1));
EXPECT_EQ(FMRB_DoesNotAccessMemory, AAR.getModRefBehavior(&F2));
EXPECT_EQ(FMRB_OnlyReadsMemory, AAR.getModRefBehavior(&F3));
}

File diff suppressed because it is too large Load Diff

View File

@@ -1,158 +0,0 @@
//===- LoopInfoTest.cpp - LoopInfo unit tests -----------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "llvm/Analysis/LoopInfo.h"
#include "llvm/AsmParser/Parser.h"
#include "llvm/IR/Dominators.h"
#include "llvm/Support/SourceMgr.h"
#include "gtest/gtest.h"
using namespace llvm;
/// Build the loop info for the function and run the Test.
static void
runWithLoopInfo(Module &M, StringRef FuncName,
function_ref<void(Function &F, LoopInfo &LI)> Test) {
auto *F = M.getFunction(FuncName);
ASSERT_NE(F, nullptr) << "Could not find " << FuncName;
// Compute the dominator tree and the loop info for the function.
DominatorTree DT(*F);
LoopInfo LI(DT);
Test(*F, LI);
}
static std::unique_ptr<Module> makeLLVMModule(LLVMContext &Context,
const char *ModuleStr) {
SMDiagnostic Err;
return parseAssemblyString(ModuleStr, Err, Context);
}
// This tests that for a loop with a single latch, we get the loop id from
// its only latch, even in case the loop may not be in a simplified form.
TEST(LoopInfoTest, LoopWithSingleLatch) {
const char *ModuleStr =
"target datalayout = \"e-m:o-i64:64-f80:128-n8:16:32:64-S128\"\n"
"define void @foo(i32 %n) {\n"
"entry:\n"
" br i1 undef, label %for.cond, label %for.end\n"
"for.cond:\n"
" %i.0 = phi i32 [ 0, %entry ], [ %inc, %for.inc ]\n"
" %cmp = icmp slt i32 %i.0, %n\n"
" br i1 %cmp, label %for.inc, label %for.end\n"
"for.inc:\n"
" %inc = add nsw i32 %i.0, 1\n"
" br label %for.cond, !llvm.loop !0\n"
"for.end:\n"
" ret void\n"
"}\n"
"!0 = distinct !{!0, !1}\n"
"!1 = !{!\"llvm.loop.distribute.enable\", i1 true}\n";
// Parse the module.
LLVMContext Context;
std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleStr);
runWithLoopInfo(*M, "foo", [&](Function &F, LoopInfo &LI) {
Function::iterator FI = F.begin();
// First basic block is entry - skip it.
BasicBlock *Header = &*(++FI);
assert(Header->getName() == "for.cond");
Loop *L = LI.getLoopFor(Header);
// This loop is not in simplified form.
EXPECT_FALSE(L->isLoopSimplifyForm());
// Analyze the loop metadata id.
bool loopIDFoundAndSet = false;
// Try to get and set the metadata id for the loop.
if (MDNode *D = L->getLoopID()) {
L->setLoopID(D);
loopIDFoundAndSet = true;
}
// We must have successfully found and set the loop id in the
// only latch the loop has.
EXPECT_TRUE(loopIDFoundAndSet);
});
}
TEST(LoopInfoTest, PreorderTraversals) {
const char *ModuleStr = "define void @f() {\n"
"entry:\n"
" br label %loop.0\n"
"loop.0:\n"
" br i1 undef, label %loop.0.0, label %loop.1\n"
"loop.0.0:\n"
" br i1 undef, label %loop.0.0, label %loop.0.1\n"
"loop.0.1:\n"
" br i1 undef, label %loop.0.1, label %loop.0.2\n"
"loop.0.2:\n"
" br i1 undef, label %loop.0.2, label %loop.0\n"
"loop.1:\n"
" br i1 undef, label %loop.1.0, label %end\n"
"loop.1.0:\n"
" br i1 undef, label %loop.1.0, label %loop.1.1\n"
"loop.1.1:\n"
" br i1 undef, label %loop.1.1, label %loop.1.2\n"
"loop.1.2:\n"
" br i1 undef, label %loop.1.2, label %loop.1\n"
"end:\n"
" ret void\n"
"}\n";
// Parse the module.
LLVMContext Context;
std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleStr);
Function &F = *M->begin();
DominatorTree DT(F);
LoopInfo LI;
LI.analyze(DT);
Function::iterator I = F.begin();
ASSERT_EQ("entry", I->getName());
++I;
Loop &L_0 = *LI.getLoopFor(&*I++);
ASSERT_EQ("loop.0", L_0.getHeader()->getName());
Loop &L_0_0 = *LI.getLoopFor(&*I++);
ASSERT_EQ("loop.0.0", L_0_0.getHeader()->getName());
Loop &L_0_1 = *LI.getLoopFor(&*I++);
ASSERT_EQ("loop.0.1", L_0_1.getHeader()->getName());
Loop &L_0_2 = *LI.getLoopFor(&*I++);
ASSERT_EQ("loop.0.2", L_0_2.getHeader()->getName());
Loop &L_1 = *LI.getLoopFor(&*I++);
ASSERT_EQ("loop.1", L_1.getHeader()->getName());
Loop &L_1_0 = *LI.getLoopFor(&*I++);
ASSERT_EQ("loop.1.0", L_1_0.getHeader()->getName());
Loop &L_1_1 = *LI.getLoopFor(&*I++);
ASSERT_EQ("loop.1.1", L_1_1.getHeader()->getName());
Loop &L_1_2 = *LI.getLoopFor(&*I++);
ASSERT_EQ("loop.1.2", L_1_2.getHeader()->getName());
auto Preorder = LI.getLoopsInPreorder();
ASSERT_EQ(8u, Preorder.size());
EXPECT_EQ(&L_0, Preorder[0]);
EXPECT_EQ(&L_0_0, Preorder[1]);
EXPECT_EQ(&L_0_1, Preorder[2]);
EXPECT_EQ(&L_0_2, Preorder[3]);
EXPECT_EQ(&L_1, Preorder[4]);
EXPECT_EQ(&L_1_0, Preorder[5]);
EXPECT_EQ(&L_1_1, Preorder[6]);
EXPECT_EQ(&L_1_2, Preorder[7]);
auto ReverseSiblingPreorder = LI.getLoopsInReverseSiblingPreorder();
ASSERT_EQ(8u, ReverseSiblingPreorder.size());
EXPECT_EQ(&L_1, ReverseSiblingPreorder[0]);
EXPECT_EQ(&L_1_2, ReverseSiblingPreorder[1]);
EXPECT_EQ(&L_1_1, ReverseSiblingPreorder[2]);
EXPECT_EQ(&L_1_0, ReverseSiblingPreorder[3]);
EXPECT_EQ(&L_0, ReverseSiblingPreorder[4]);
EXPECT_EQ(&L_0_2, ReverseSiblingPreorder[5]);
EXPECT_EQ(&L_0_1, ReverseSiblingPreorder[6]);
EXPECT_EQ(&L_0_0, ReverseSiblingPreorder[7]);
}

View File

@@ -1,50 +0,0 @@
//===- MemoryBuiltinsTest.cpp - Tests for utilities in MemoryBuiltins.h ---===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "llvm/Analysis/MemoryBuiltins.h"
#include "llvm/IR/Attributes.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "gtest/gtest.h"
using namespace llvm;
namespace {
// allocsize should not imply that a function is a traditional allocation
// function (e.g. that can be optimized out/...); it just tells us how many
// bytes exist at the pointer handed back by the function.
TEST(AllocSize, AllocationBuiltinsTest) {
LLVMContext Context;
Module M("", Context);
IntegerType *ArgTy = Type::getInt32Ty(Context);
Function *AllocSizeFn = Function::Create(
FunctionType::get(Type::getInt8PtrTy(Context), {ArgTy}, false),
GlobalValue::ExternalLinkage, "F", &M);
AllocSizeFn->addFnAttr(Attribute::getWithAllocSizeArgs(Context, 1, None));
// 100 is arbitrary.
std::unique_ptr<CallInst> Caller(
CallInst::Create(AllocSizeFn, {ConstantInt::get(ArgTy, 100)}));
const TargetLibraryInfo *TLI = nullptr;
EXPECT_FALSE(isNoAliasFn(Caller.get(), TLI));
EXPECT_FALSE(isMallocLikeFn(Caller.get(), TLI));
EXPECT_FALSE(isCallocLikeFn(Caller.get(), TLI));
EXPECT_FALSE(isAllocLikeFn(Caller.get(), TLI));
// FIXME: We might be able to treat allocsize functions as general allocation
// functions. For the moment, being conservative seems better (and we'd have
// to plumb stuff around `isNoAliasFn`).
EXPECT_FALSE(isAllocationFn(Caller.get(), TLI));
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -1,58 +0,0 @@
//===- OrderedBasicBlockTest.cpp - OrderedBasicBlock unit tests -----------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "llvm/Analysis/OrderedBasicBlock.h"
#include "llvm/AsmParser/Parser.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/DataTypes.h"
#include "llvm/Support/SourceMgr.h"
#include "gtest/gtest.h"
namespace llvm {
namespace {
class OrderedBasicBlockTest : public testing::Test {
protected:
LLVMContext C;
std::unique_ptr<Module> makeLLVMModule() {
const char *ModuleString = R"(define i32 @f(i32 %x) {
%add = add i32 %x, 42
ret i32 %add
})";
SMDiagnostic Err;
auto foo = parseAssemblyString(ModuleString, Err, C);
return foo;
}
};
TEST_F(OrderedBasicBlockTest, Basic) {
auto M = makeLLVMModule();
Function *F = M->getFunction("f");
BasicBlock::iterator I = F->front().begin();
Instruction *Add = &*I++;
Instruction *Ret = &*I++;
OrderedBasicBlock OBB(&F->front());
// Intentionally duplicated to verify cached and uncached are the same.
EXPECT_FALSE(OBB.dominates(Add, Add));
EXPECT_FALSE(OBB.dominates(Add, Add));
EXPECT_TRUE(OBB.dominates(Add, Ret));
EXPECT_TRUE(OBB.dominates(Add, Ret));
EXPECT_FALSE(OBB.dominates(Ret, Add));
EXPECT_FALSE(OBB.dominates(Ret, Add));
EXPECT_FALSE(OBB.dominates(Ret, Ret));
EXPECT_FALSE(OBB.dominates(Ret, Ret));
}
} // end anonymous namespace
} // end namespace llvm

View File

@@ -1,215 +0,0 @@
//===- ProfileSummaryInfoTest.cpp - ProfileSummaryInfo unit tests ---------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "llvm/Analysis/ProfileSummaryInfo.h"
#include "llvm/Analysis/BlockFrequencyInfo.h"
#include "llvm/Analysis/BranchProbabilityInfo.h"
#include "llvm/Analysis/LoopInfo.h"
#include "llvm/AsmParser/Parser.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/CallSite.h"
#include "llvm/IR/Dominators.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/MDBuilder.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/DataTypes.h"
#include "llvm/Support/FormatVariadic.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/Support/raw_ostream.h"
#include "gtest/gtest.h"
namespace llvm {
namespace {
class ProfileSummaryInfoTest : public testing::Test {
protected:
LLVMContext C;
std::unique_ptr<BranchProbabilityInfo> BPI;
std::unique_ptr<DominatorTree> DT;
std::unique_ptr<LoopInfo> LI;
ProfileSummaryInfo buildPSI(Module *M) {
return ProfileSummaryInfo(*M);
}
BlockFrequencyInfo buildBFI(Function &F) {
DT.reset(new DominatorTree(F));
LI.reset(new LoopInfo(*DT));
BPI.reset(new BranchProbabilityInfo(F, *LI));
return BlockFrequencyInfo(F, *BPI, *LI);
}
std::unique_ptr<Module> makeLLVMModule(const char *ProfKind = nullptr) {
const char *ModuleString =
"define i32 @g(i32 %x) !prof !21 {{\n"
" ret i32 0\n"
"}\n"
"define i32 @h(i32 %x) !prof !22 {{\n"
" ret i32 0\n"
"}\n"
"define i32 @f(i32 %x) !prof !20 {{\n"
"bb0:\n"
" %y1 = icmp eq i32 %x, 0 \n"
" br i1 %y1, label %bb1, label %bb2, !prof !23 \n"
"bb1:\n"
" %z1 = call i32 @g(i32 %x)\n"
" br label %bb3\n"
"bb2:\n"
" %z2 = call i32 @h(i32 %x)\n"
" br label %bb3\n"
"bb3:\n"
" %y2 = phi i32 [0, %bb1], [1, %bb2] \n"
" ret i32 %y2\n"
"}\n"
"!20 = !{{!\"function_entry_count\", i64 400}\n"
"!21 = !{{!\"function_entry_count\", i64 1}\n"
"!22 = !{{!\"function_entry_count\", i64 100}\n"
"!23 = !{{!\"branch_weights\", i32 64, i32 4}\n"
"{0}";
const char *SummaryString = "!llvm.module.flags = !{{!1}"
"!1 = !{{i32 1, !\"ProfileSummary\", !2}"
"!2 = !{{!3, !4, !5, !6, !7, !8, !9, !10}"
"!3 = !{{!\"ProfileFormat\", !\"{0}\"}"
"!4 = !{{!\"TotalCount\", i64 10000}"
"!5 = !{{!\"MaxCount\", i64 10}"
"!6 = !{{!\"MaxInternalCount\", i64 1}"
"!7 = !{{!\"MaxFunctionCount\", i64 1000}"
"!8 = !{{!\"NumCounts\", i64 3}"
"!9 = !{{!\"NumFunctions\", i64 3}"
"!10 = !{{!\"DetailedSummary\", !11}"
"!11 = !{{!12, !13, !14}"
"!12 = !{{i32 10000, i64 1000, i32 1}"
"!13 = !{{i32 999000, i64 300, i32 3}"
"!14 = !{{i32 999999, i64 5, i32 10}";
SMDiagnostic Err;
if (ProfKind)
return parseAssemblyString(
formatv(ModuleString, formatv(SummaryString, ProfKind).str()).str(),
Err, C);
else
return parseAssemblyString(formatv(ModuleString, "").str(), Err, C);
}
};
TEST_F(ProfileSummaryInfoTest, TestNoProfile) {
auto M = makeLLVMModule(/*ProfKind=*/nullptr);
Function *F = M->getFunction("f");
ProfileSummaryInfo PSI = buildPSI(M.get());
EXPECT_FALSE(PSI.hasProfileSummary());
EXPECT_FALSE(PSI.hasSampleProfile());
EXPECT_FALSE(PSI.hasInstrumentationProfile());
// In the absence of profiles, is{Hot|Cold}X methods should always return
// false.
EXPECT_FALSE(PSI.isHotCount(1000));
EXPECT_FALSE(PSI.isHotCount(0));
EXPECT_FALSE(PSI.isColdCount(1000));
EXPECT_FALSE(PSI.isColdCount(0));
EXPECT_FALSE(PSI.isFunctionEntryHot(F));
EXPECT_FALSE(PSI.isFunctionEntryCold(F));
BasicBlock &BB0 = F->getEntryBlock();
BasicBlock *BB1 = BB0.getTerminator()->getSuccessor(0);
BlockFrequencyInfo BFI = buildBFI(*F);
EXPECT_FALSE(PSI.isHotBB(&BB0, &BFI));
EXPECT_FALSE(PSI.isColdBB(&BB0, &BFI));
CallSite CS1(BB1->getFirstNonPHI());
EXPECT_FALSE(PSI.isHotCallSite(CS1, &BFI));
EXPECT_FALSE(PSI.isColdCallSite(CS1, &BFI));
}
TEST_F(ProfileSummaryInfoTest, TestCommon) {
auto M = makeLLVMModule("InstrProf");
Function *F = M->getFunction("f");
Function *G = M->getFunction("g");
Function *H = M->getFunction("h");
ProfileSummaryInfo PSI = buildPSI(M.get());
EXPECT_TRUE(PSI.hasProfileSummary());
EXPECT_TRUE(PSI.isHotCount(400));
EXPECT_TRUE(PSI.isColdCount(2));
EXPECT_FALSE(PSI.isColdCount(100));
EXPECT_FALSE(PSI.isHotCount(100));
EXPECT_TRUE(PSI.isFunctionEntryHot(F));
EXPECT_FALSE(PSI.isFunctionEntryHot(G));
EXPECT_FALSE(PSI.isFunctionEntryHot(H));
}
TEST_F(ProfileSummaryInfoTest, InstrProf) {
auto M = makeLLVMModule("InstrProf");
Function *F = M->getFunction("f");
ProfileSummaryInfo PSI = buildPSI(M.get());
EXPECT_TRUE(PSI.hasProfileSummary());
EXPECT_TRUE(PSI.hasInstrumentationProfile());
BasicBlock &BB0 = F->getEntryBlock();
BasicBlock *BB1 = BB0.getTerminator()->getSuccessor(0);
BasicBlock *BB2 = BB0.getTerminator()->getSuccessor(1);
BasicBlock *BB3 = BB1->getSingleSuccessor();
BlockFrequencyInfo BFI = buildBFI(*F);
EXPECT_TRUE(PSI.isHotBB(&BB0, &BFI));
EXPECT_TRUE(PSI.isHotBB(BB1, &BFI));
EXPECT_FALSE(PSI.isHotBB(BB2, &BFI));
EXPECT_TRUE(PSI.isHotBB(BB3, &BFI));
CallSite CS1(BB1->getFirstNonPHI());
auto *CI2 = BB2->getFirstNonPHI();
CallSite CS2(CI2);
EXPECT_TRUE(PSI.isHotCallSite(CS1, &BFI));
EXPECT_FALSE(PSI.isHotCallSite(CS2, &BFI));
// Test that adding an MD_prof metadata with a hot count on CS2 does not
// change its hotness as it has no effect in instrumented profiling.
MDBuilder MDB(M->getContext());
CI2->setMetadata(llvm::LLVMContext::MD_prof, MDB.createBranchWeights({400}));
EXPECT_FALSE(PSI.isHotCallSite(CS2, &BFI));
}
TEST_F(ProfileSummaryInfoTest, SampleProf) {
auto M = makeLLVMModule("SampleProfile");
Function *F = M->getFunction("f");
ProfileSummaryInfo PSI = buildPSI(M.get());
EXPECT_TRUE(PSI.hasProfileSummary());
EXPECT_TRUE(PSI.hasSampleProfile());
BasicBlock &BB0 = F->getEntryBlock();
BasicBlock *BB1 = BB0.getTerminator()->getSuccessor(0);
BasicBlock *BB2 = BB0.getTerminator()->getSuccessor(1);
BasicBlock *BB3 = BB1->getSingleSuccessor();
BlockFrequencyInfo BFI = buildBFI(*F);
EXPECT_TRUE(PSI.isHotBB(&BB0, &BFI));
EXPECT_TRUE(PSI.isHotBB(BB1, &BFI));
EXPECT_FALSE(PSI.isHotBB(BB2, &BFI));
EXPECT_TRUE(PSI.isHotBB(BB3, &BFI));
CallSite CS1(BB1->getFirstNonPHI());
auto *CI2 = BB2->getFirstNonPHI();
// Manually attach branch weights metadata to the call instruction.
SmallVector<uint32_t, 1> Weights;
Weights.push_back(1000);
MDBuilder MDB(M->getContext());
CI2->setMetadata(LLVMContext::MD_prof, MDB.createBranchWeights(Weights));
CallSite CS2(CI2);
EXPECT_FALSE(PSI.isHotCallSite(CS1, &BFI));
EXPECT_TRUE(PSI.isHotCallSite(CS2, &BFI));
// Test that CS2 is considered hot when it gets an MD_prof metadata with
// weights that exceed the hot count threshold.
CI2->setMetadata(llvm::LLVMContext::MD_prof, MDB.createBranchWeights({400}));
EXPECT_TRUE(PSI.isHotCallSite(CS2, &BFI));
}
} // end anonymous namespace
} // end namespace llvm

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -1,91 +0,0 @@
//===--- TBAATest.cpp - Mixed TBAA unit tests -----------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "llvm/Analysis/AliasAnalysisEvaluator.h"
#include "llvm/Analysis/Passes.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/IR/MDBuilder.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Verifier.h"
#include "llvm/Support/CommandLine.h"
#include "gtest/gtest.h"
namespace llvm {
namespace {
class TBAATest : public testing::Test {
protected:
TBAATest() : M("TBAATest", C), MD(C) {}
LLVMContext C;
Module M;
MDBuilder MD;
};
static StoreInst *getFunctionWithSingleStore(Module *M, StringRef Name) {
auto &C = M->getContext();
FunctionType *FTy = FunctionType::get(Type::getVoidTy(C), {});
auto *F = cast<Function>(M->getOrInsertFunction(Name, FTy));
auto *BB = BasicBlock::Create(C, "entry", F);
auto *IntType = Type::getInt32Ty(C);
auto *PtrType = Type::getInt32PtrTy(C);
auto *SI = new StoreInst(ConstantInt::get(IntType, 42),
ConstantPointerNull::get(PtrType), BB);
ReturnInst::Create(C, nullptr, BB);
return SI;
}
TEST_F(TBAATest, checkVerifierBehaviorForOldTBAA) {
auto *SI = getFunctionWithSingleStore(&M, "f1");
auto *F = SI->getFunction();
// C++ unit test case to avoid going through the auto upgrade logic.
auto *RootMD = MD.createTBAARoot("Simple C/C++ TBAA");
auto *MD1 = MD.createTBAANode("omnipotent char", RootMD);
auto *MD2 = MD.createTBAANode("int", MD1);
SI->setMetadata(LLVMContext::MD_tbaa, MD2);
SmallVector<char, 0> ErrorMsg;
raw_svector_ostream Outs(ErrorMsg);
StringRef ExpectedFailureMsg(
"Old-style TBAA is no longer allowed, use struct-path TBAA instead");
EXPECT_TRUE(verifyFunction(*F, &Outs));
EXPECT_TRUE(StringRef(ErrorMsg.begin(), ErrorMsg.size())
.startswith(ExpectedFailureMsg));
}
TEST_F(TBAATest, checkTBAAMerging) {
auto *SI = getFunctionWithSingleStore(&M, "f2");
auto *F = SI->getFunction();
auto *RootMD = MD.createTBAARoot("tbaa-root");
auto *MD1 = MD.createTBAANode("scalar-a", RootMD);
auto *StructTag1 = MD.createTBAAStructTagNode(MD1, MD1, 0);
auto *MD2 = MD.createTBAANode("scalar-b", RootMD);
auto *StructTag2 = MD.createTBAAStructTagNode(MD2, MD2, 0);
auto *GenericMD = MDNode::getMostGenericTBAA(StructTag1, StructTag2);
EXPECT_EQ(GenericMD, nullptr);
// Despite GenericMD being nullptr, we expect the setMetadata call to be well
// defined and produce a well-formed function.
SI->setMetadata(LLVMContext::MD_tbaa, GenericMD);
EXPECT_TRUE(!verifyFunction(*F));
}
} // end anonymous namspace
} // end llvm namespace

File diff suppressed because it is too large Load Diff

View File

@@ -1,330 +0,0 @@
//===- UnrollAnalyzerTest.cpp - UnrollAnalyzer unit tests -----------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "llvm/Analysis/LoopUnrollAnalyzer.h"
#include "llvm/AsmParser/Parser.h"
#include "llvm/IR/Dominators.h"
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/Support/SourceMgr.h"
#include "gtest/gtest.h"
using namespace llvm;
namespace llvm {
void initializeUnrollAnalyzerTestPass(PassRegistry &);
static SmallVector<DenseMap<Value *, Constant *>, 16> SimplifiedValuesVector;
static unsigned TripCount = 0;
namespace {
struct UnrollAnalyzerTest : public FunctionPass {
static char ID;
bool runOnFunction(Function &F) override {
LoopInfo *LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
ScalarEvolution *SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
Function::iterator FI = F.begin();
FI++; // First basic block is entry - skip it.
BasicBlock *Header = &*FI++;
Loop *L = LI->getLoopFor(Header);
BasicBlock *Exiting = L->getExitingBlock();
SimplifiedValuesVector.clear();
TripCount = SE->getSmallConstantTripCount(L, Exiting);
for (unsigned Iteration = 0; Iteration < TripCount; Iteration++) {
DenseMap<Value *, Constant *> SimplifiedValues;
UnrolledInstAnalyzer Analyzer(Iteration, SimplifiedValues, *SE, L);
for (auto *BB : L->getBlocks())
for (Instruction &I : *BB)
Analyzer.visit(I);
SimplifiedValuesVector.push_back(SimplifiedValues);
}
return false;
}
void getAnalysisUsage(AnalysisUsage &AU) const override {
AU.addRequired<DominatorTreeWrapperPass>();
AU.addRequired<LoopInfoWrapperPass>();
AU.addRequired<ScalarEvolutionWrapperPass>();
AU.setPreservesAll();
}
UnrollAnalyzerTest() : FunctionPass(ID) {
initializeUnrollAnalyzerTestPass(*PassRegistry::getPassRegistry());
}
};
}
char UnrollAnalyzerTest::ID = 0;
std::unique_ptr<Module> makeLLVMModule(LLVMContext &Context,
const char *ModuleStr) {
SMDiagnostic Err;
return parseAssemblyString(ModuleStr, Err, Context);
}
TEST(UnrollAnalyzerTest, BasicSimplifications) {
const char *ModuleStr =
"target datalayout = \"e-m:o-i64:64-f80:128-n8:16:32:64-S128\"\n"
"define i64 @propagate_loop_phis() {\n"
"entry:\n"
" br label %loop\n"
"loop:\n"
" %iv = phi i64 [ 0, %entry ], [ %inc, %loop ]\n"
" %x0 = phi i64 [ 0, %entry ], [ %x2, %loop ]\n"
" %x1 = or i64 %x0, 1\n"
" %x2 = or i64 %x1, 2\n"
" %inc = add nuw nsw i64 %iv, 1\n"
" %cond = icmp sge i64 %inc, 8\n"
" br i1 %cond, label %loop.end, label %loop\n"
"loop.end:\n"
" %x.lcssa = phi i64 [ %x2, %loop ]\n"
" ret i64 %x.lcssa\n"
"}\n";
UnrollAnalyzerTest *P = new UnrollAnalyzerTest();
LLVMContext Context;
std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleStr);
legacy::PassManager Passes;
Passes.add(P);
Passes.run(*M);
// Perform checks
Module::iterator MI = M->begin();
Function *F = &*MI++;
Function::iterator FI = F->begin();
FI++; // First basic block is entry - skip it.
BasicBlock *Header = &*FI++;
BasicBlock::iterator BBI = Header->begin();
std::advance(BBI, 4);
Instruction *Y1 = &*BBI++;
Instruction *Y2 = &*BBI++;
// Check simplification expected on the 1st iteration.
// Check that "%inc = add nuw nsw i64 %iv, 1" is simplified to 1
auto I1 = SimplifiedValuesVector[0].find(Y1);
EXPECT_TRUE(I1 != SimplifiedValuesVector[0].end());
EXPECT_EQ(cast<ConstantInt>((*I1).second)->getZExtValue(), 1U);
// Check that "%cond = icmp sge i64 %inc, 10" is simplified to false
auto I2 = SimplifiedValuesVector[0].find(Y2);
EXPECT_TRUE(I2 != SimplifiedValuesVector[0].end());
EXPECT_FALSE(cast<ConstantInt>((*I2).second)->getZExtValue());
// Check simplification expected on the last iteration.
// Check that "%inc = add nuw nsw i64 %iv, 1" is simplified to 8
I1 = SimplifiedValuesVector[TripCount - 1].find(Y1);
EXPECT_TRUE(I1 != SimplifiedValuesVector[TripCount - 1].end());
EXPECT_EQ(cast<ConstantInt>((*I1).second)->getZExtValue(), TripCount);
// Check that "%cond = icmp sge i64 %inc, 10" is simplified to false
I2 = SimplifiedValuesVector[TripCount - 1].find(Y2);
EXPECT_TRUE(I2 != SimplifiedValuesVector[TripCount - 1].end());
EXPECT_TRUE(cast<ConstantInt>((*I2).second)->getZExtValue());
}
TEST(UnrollAnalyzerTest, OuterLoopSimplification) {
const char *ModuleStr =
"target datalayout = \"e-m:o-i64:64-f80:128-n8:16:32:64-S128\"\n"
"define void @foo() {\n"
"entry:\n"
" br label %outer.loop\n"
"outer.loop:\n"
" %iv.outer = phi i64 [ 0, %entry ], [ %iv.outer.next, %outer.loop.latch ]\n"
" %iv.outer.next = add nuw nsw i64 %iv.outer, 1\n"
" br label %inner.loop\n"
"inner.loop:\n"
" %iv.inner = phi i64 [ 0, %outer.loop ], [ %iv.inner.next, %inner.loop ]\n"
" %iv.inner.next = add nuw nsw i64 %iv.inner, 1\n"
" %exitcond.inner = icmp eq i64 %iv.inner.next, 1000\n"
" br i1 %exitcond.inner, label %outer.loop.latch, label %inner.loop\n"
"outer.loop.latch:\n"
" %exitcond.outer = icmp eq i64 %iv.outer.next, 40\n"
" br i1 %exitcond.outer, label %exit, label %outer.loop\n"
"exit:\n"
" ret void\n"
"}\n";
UnrollAnalyzerTest *P = new UnrollAnalyzerTest();
LLVMContext Context;
std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleStr);
legacy::PassManager Passes;
Passes.add(P);
Passes.run(*M);
Module::iterator MI = M->begin();
Function *F = &*MI++;
Function::iterator FI = F->begin();
FI++;
BasicBlock *Header = &*FI++;
BasicBlock *InnerBody = &*FI++;
BasicBlock::iterator BBI = Header->begin();
BBI++;
Instruction *Y1 = &*BBI;
BBI = InnerBody->begin();
BBI++;
Instruction *Y2 = &*BBI;
// Check that we can simplify IV of the outer loop, but can't simplify the IV
// of the inner loop if we only know the iteration number of the outer loop.
//
// Y1 is %iv.outer.next, Y2 is %iv.inner.next
auto I1 = SimplifiedValuesVector[0].find(Y1);
EXPECT_TRUE(I1 != SimplifiedValuesVector[0].end());
auto I2 = SimplifiedValuesVector[0].find(Y2);
EXPECT_TRUE(I2 == SimplifiedValuesVector[0].end());
}
TEST(UnrollAnalyzerTest, CmpSimplifications) {
const char *ModuleStr =
"target datalayout = \"e-m:o-i64:64-f80:128-n8:16:32:64-S128\"\n"
"define void @branch_iv_trunc() {\n"
"entry:\n"
" br label %for.body\n"
"for.body:\n"
" %indvars.iv = phi i64 [ 0, %entry ], [ %tmp3, %for.body ]\n"
" %tmp2 = trunc i64 %indvars.iv to i32\n"
" %cmp3 = icmp eq i32 %tmp2, 5\n"
" %tmp3 = add nuw nsw i64 %indvars.iv, 1\n"
" %exitcond = icmp eq i64 %tmp3, 10\n"
" br i1 %exitcond, label %for.end, label %for.body\n"
"for.end:\n"
" ret void\n"
"}\n";
UnrollAnalyzerTest *P = new UnrollAnalyzerTest();
LLVMContext Context;
std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleStr);
legacy::PassManager Passes;
Passes.add(P);
Passes.run(*M);
// Perform checks
Module::iterator MI = M->begin();
Function *F = &*MI++;
Function::iterator FI = F->begin();
FI++; // First basic block is entry - skip it.
BasicBlock *Header = &*FI++;
BasicBlock::iterator BBI = Header->begin();
BBI++;
Instruction *Y1 = &*BBI++;
Instruction *Y2 = &*BBI++;
// Check simplification expected on the 5th iteration.
// Check that "%tmp2 = trunc i64 %indvars.iv to i32" is simplified to 5
// and "%cmp3 = icmp eq i32 %tmp2, 5" is simplified to 1 (i.e. true).
auto I1 = SimplifiedValuesVector[5].find(Y1);
EXPECT_TRUE(I1 != SimplifiedValuesVector[5].end());
EXPECT_EQ(cast<ConstantInt>((*I1).second)->getZExtValue(), 5U);
auto I2 = SimplifiedValuesVector[5].find(Y2);
EXPECT_TRUE(I2 != SimplifiedValuesVector[5].end());
EXPECT_EQ(cast<ConstantInt>((*I2).second)->getZExtValue(), 1U);
}
TEST(UnrollAnalyzerTest, PtrCmpSimplifications) {
const char *ModuleStr =
"target datalayout = \"e-m:o-i64:64-f80:128-n8:16:32:64-S128\"\n"
"define void @ptr_cmp(i8 *%a) {\n"
"entry:\n"
" %limit = getelementptr i8, i8* %a, i64 40\n"
" %start.iv2 = getelementptr i8, i8* %a, i64 7\n"
" br label %loop.body\n"
"loop.body:\n"
" %iv.0 = phi i8* [ %a, %entry ], [ %iv.1, %loop.body ]\n"
" %iv2.0 = phi i8* [ %start.iv2, %entry ], [ %iv2.1, %loop.body ]\n"
" %cmp = icmp eq i8* %iv2.0, %iv.0\n"
" %iv.1 = getelementptr inbounds i8, i8* %iv.0, i64 1\n"
" %iv2.1 = getelementptr inbounds i8, i8* %iv2.0, i64 1\n"
" %exitcond = icmp ne i8* %iv.1, %limit\n"
" br i1 %exitcond, label %loop.body, label %loop.exit\n"
"loop.exit:\n"
" ret void\n"
"}\n";
UnrollAnalyzerTest *P = new UnrollAnalyzerTest();
LLVMContext Context;
std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleStr);
legacy::PassManager Passes;
Passes.add(P);
Passes.run(*M);
// Perform checks
Module::iterator MI = M->begin();
Function *F = &*MI++;
Function::iterator FI = F->begin();
FI++; // First basic block is entry - skip it.
BasicBlock *Header = &*FI;
BasicBlock::iterator BBI = Header->begin();
std::advance(BBI, 2);
Instruction *Y1 = &*BBI;
// Check simplification expected on the 5th iteration.
// Check that "%cmp = icmp eq i8* %iv2.0, %iv.0" is simplified to 0.
auto I1 = SimplifiedValuesVector[5].find(Y1);
EXPECT_TRUE(I1 != SimplifiedValuesVector[5].end());
EXPECT_EQ(cast<ConstantInt>((*I1).second)->getZExtValue(), 0U);
}
TEST(UnrollAnalyzerTest, CastSimplifications) {
const char *ModuleStr =
"target datalayout = \"e-m:o-i64:64-f80:128-n8:16:32:64-S128\"\n"
"@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"
"define void @const_load_cast() {\n"
"entry:\n"
" br label %loop\n"
"\n"
"loop:\n"
" %iv = phi i64 [ 0, %entry ], [ %inc, %loop ]\n"
" %array_const_idx = getelementptr inbounds [10 x i32], [10 x i32]* @known_constant, i64 0, i64 %iv\n"
" %const_array_element = load i32, i32* %array_const_idx, align 4\n"
" %se = sext i32 %const_array_element to i64\n"
" %ze = zext i32 %const_array_element to i64\n"
" %tr = trunc i32 %const_array_element to i8\n"
" %inc = add nuw nsw i64 %iv, 1\n"
" %exitcond86.i = icmp eq i64 %inc, 10\n"
" br i1 %exitcond86.i, label %loop.end, label %loop\n"
"\n"
"loop.end:\n"
" ret void\n"
"}\n";
UnrollAnalyzerTest *P = new UnrollAnalyzerTest();
LLVMContext Context;
std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleStr);
legacy::PassManager Passes;
Passes.add(P);
Passes.run(*M);
// Perform checks
Module::iterator MI = M->begin();
Function *F = &*MI++;
Function::iterator FI = F->begin();
FI++; // First basic block is entry - skip it.
BasicBlock *Header = &*FI++;
BasicBlock::iterator BBI = Header->begin();
std::advance(BBI, 3);
Instruction *Y1 = &*BBI++;
Instruction *Y2 = &*BBI++;
Instruction *Y3 = &*BBI++;
// Check simplification expected on the 5th iteration.
// "%se = sext i32 %const_array_element to i64" should be simplified to 259,
// "%ze = zext i32 %const_array_element to i64" should be simplified to 259,
// "%tr = trunc i32 %const_array_element to i8" should be simplified to 3.
auto I1 = SimplifiedValuesVector[5].find(Y1);
EXPECT_TRUE(I1 != SimplifiedValuesVector[5].end());
EXPECT_EQ(cast<ConstantInt>((*I1).second)->getZExtValue(), 259U);
auto I2 = SimplifiedValuesVector[5].find(Y2);
EXPECT_TRUE(I2 != SimplifiedValuesVector[5].end());
EXPECT_EQ(cast<ConstantInt>((*I2).second)->getZExtValue(), 259U);
auto I3 = SimplifiedValuesVector[5].find(Y3);
EXPECT_TRUE(I3 != SimplifiedValuesVector[5].end());
EXPECT_EQ(cast<ConstantInt>((*I3).second)->getZExtValue(), 3U);
}
} // end namespace llvm
INITIALIZE_PASS_BEGIN(UnrollAnalyzerTest, "unrollanalyzertestpass",
"unrollanalyzertestpass", false, false)
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)
INITIALIZE_PASS_END(UnrollAnalyzerTest, "unrollanalyzertestpass",
"unrollanalyzertestpass", false, false)

Some files were not shown because too many files have changed in this diff Show More