X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=unittests%2FAnalysis%2FCFGTest.cpp;h=44f0fe681dffd32587d8fa2f16d8cac473df744a;hb=0a3cc8fdf572dd4598952013b8d90f6ae6ce87bb;hp=23588037784e8ad41f9f9288b5e2f15c206fdee9;hpb=81e480463d8bb57776d03cebfd083762909023f1;p=oota-llvm.git diff --git a/unittests/Analysis/CFGTest.cpp b/unittests/Analysis/CFGTest.cpp index 23588037784..44f0fe681df 100644 --- a/unittests/Analysis/CFGTest.cpp +++ b/unittests/Analysis/CFGTest.cpp @@ -8,18 +8,17 @@ //===----------------------------------------------------------------------===// #include "llvm/Analysis/CFG.h" -#include "llvm/ADT/OwningPtr.h" -#include "llvm/Analysis/Dominators.h" #include "llvm/Analysis/LoopInfo.h" -#include "llvm/Assembly/Parser.h" -#include "llvm/IR/LLVMContext.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/Module.h" +#include "llvm/Pass.h" +#include "llvm/IR/LegacyPassManager.h" #include "llvm/Support/ErrorHandling.h" -#include "llvm/Support/InstIterator.h" #include "llvm/Support/SourceMgr.h" -#include "llvm/Pass.h" -#include "llvm/PassManager.h" #include "gtest/gtest.h" using namespace llvm; @@ -31,26 +30,22 @@ namespace { class IsPotentiallyReachableTest : public testing::Test { protected: void ParseAssembly(const char *Assembly) { - M.reset(new Module("Module", getGlobalContext())); - SMDiagnostic Error; - bool Parsed = ParseAssemblyString(Assembly, M.get(), - Error, M->getContext()) == M.get(); + M = parseAssemblyString(Assembly, Error, getGlobalContext()); std::string errMsg; raw_string_ostream os(errMsg); Error.print("", os); - if (!Parsed) { - // A failure here means that the test itself is buggy. + // 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 == NULL) + if (F == nullptr) report_fatal_error("Test must have a function named @test"); - A = B = NULL; + A = B = nullptr; for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) { if (I->hasName()) { if (I->getName() == "A") @@ -59,9 +54,9 @@ protected: B = &*I; } } - if (A == NULL) + if (A == nullptr) report_fatal_error("@test must have an instruction %A"); - if (B == NULL) + if (B == nullptr) report_fatal_error("@test must have an instruction %B"); } @@ -75,28 +70,31 @@ protected: static int initialize() { PassInfo *PI = new PassInfo("isPotentiallyReachable testing pass", - "", &ID, 0, true, true); + "", &ID, nullptr, true, true); PassRegistry::getPassRegistry()->registerPass(*PI, false); - initializeLoopInfoPass(*PassRegistry::getPassRegistry()); - initializeDominatorTreePass(*PassRegistry::getPassRegistry()); + initializeLoopInfoWrapperPassPass(*PassRegistry::getPassRegistry()); + initializeDominatorTreeWrapperPassPass( + *PassRegistry::getPassRegistry()); return 0; } - void getAnalysisUsage(AnalysisUsage &AU) const { + void getAnalysisUsage(AnalysisUsage &AU) const override { AU.setPreservesAll(); - AU.addRequired(); - AU.addRequired(); + AU.addRequired(); + AU.addRequired(); } - bool runOnFunction(Function &F) { + bool runOnFunction(Function &F) override { if (!F.hasName() || F.getName() != "test") return false; - LoopInfo *LI = &getAnalysis(); - DominatorTree *DT = &getAnalysis(); - EXPECT_EQ(isPotentiallyReachable(A, B, 0, 0), ExpectedResult); - EXPECT_EQ(isPotentiallyReachable(A, B, DT, 0), ExpectedResult); - EXPECT_EQ(isPotentiallyReachable(A, B, 0, LI), ExpectedResult); + LoopInfo *LI = &getAnalysis().getLoopInfo(); + DominatorTree *DT = + &getAnalysis().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; } @@ -109,12 +107,12 @@ protected: IsPotentiallyReachableTestPass *P = new IsPotentiallyReachableTestPass(ExpectedResult, A, B); - PassManager PM; + legacy::PassManager PM; PM.add(P); PM.run(*M); } -private: - OwningPtr M; + + std::unique_ptr M; Instruction *A, *B; }; @@ -147,6 +145,23 @@ TEST_F(IsPotentiallyReachableTest, SameBlockPath) { 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" @@ -333,27 +348,40 @@ TEST_F(IsPotentiallyReachableTest, OneLoopAfterTheOtherInsideAThirdLoop) { 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( - "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" - "}"); + 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); }