From c779e96158cbac4c62df8e2053ab6a933eba5868 Mon Sep 17 00:00:00 2001 From: Chandler Carruth Date: Mon, 7 Jan 2013 15:35:46 +0000 Subject: Rename the VMCore unittest tree to IR. Somehow was missed when doing the library rename. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@171747 91177308-0d34-0410-b5e6-96231b3b80d8 --- unittests/CMakeLists.txt | 2 +- unittests/IR/CMakeLists.txt | 36 +++ unittests/IR/ConstantsTest.cpp | 228 ++++++++++++++ unittests/IR/DominatorTreeTest.cpp | 195 ++++++++++++ unittests/IR/IRBuilderTest.cpp | 184 +++++++++++ unittests/IR/InstructionsTest.cpp | 284 +++++++++++++++++ unittests/IR/MDBuilderTest.cpp | 106 +++++++ unittests/IR/Makefile | 15 + unittests/IR/MetadataTest.cpp | 152 +++++++++ unittests/IR/PassManagerTest.cpp | 552 +++++++++++++++++++++++++++++++++ unittests/IR/TypeBuilderTest.cpp | 253 +++++++++++++++ unittests/IR/TypesTest.cpp | 30 ++ unittests/IR/ValueMapTest.cpp | 294 ++++++++++++++++++ unittests/IR/VerifierTest.cpp | 64 ++++ unittests/IR/WaymarkTest.cpp | 54 ++++ unittests/Makefile | 2 +- unittests/VMCore/CMakeLists.txt | 36 --- unittests/VMCore/ConstantsTest.cpp | 228 -------------- unittests/VMCore/DominatorTreeTest.cpp | 195 ------------ unittests/VMCore/IRBuilderTest.cpp | 184 ----------- unittests/VMCore/InstructionsTest.cpp | 284 ----------------- unittests/VMCore/MDBuilderTest.cpp | 106 ------- unittests/VMCore/Makefile | 15 - unittests/VMCore/MetadataTest.cpp | 152 --------- unittests/VMCore/PassManagerTest.cpp | 552 --------------------------------- unittests/VMCore/TypeBuilderTest.cpp | 253 --------------- unittests/VMCore/TypesTest.cpp | 30 -- unittests/VMCore/ValueMapTest.cpp | 294 ------------------ unittests/VMCore/VerifierTest.cpp | 64 ---- unittests/VMCore/WaymarkTest.cpp | 54 ---- 30 files changed, 2449 insertions(+), 2449 deletions(-) create mode 100644 unittests/IR/CMakeLists.txt create mode 100644 unittests/IR/ConstantsTest.cpp create mode 100644 unittests/IR/DominatorTreeTest.cpp create mode 100644 unittests/IR/IRBuilderTest.cpp create mode 100644 unittests/IR/InstructionsTest.cpp create mode 100644 unittests/IR/MDBuilderTest.cpp create mode 100644 unittests/IR/Makefile create mode 100644 unittests/IR/MetadataTest.cpp create mode 100644 unittests/IR/PassManagerTest.cpp create mode 100644 unittests/IR/TypeBuilderTest.cpp create mode 100644 unittests/IR/TypesTest.cpp create mode 100644 unittests/IR/ValueMapTest.cpp create mode 100644 unittests/IR/VerifierTest.cpp create mode 100644 unittests/IR/WaymarkTest.cpp delete mode 100644 unittests/VMCore/CMakeLists.txt delete mode 100644 unittests/VMCore/ConstantsTest.cpp delete mode 100644 unittests/VMCore/DominatorTreeTest.cpp delete mode 100644 unittests/VMCore/IRBuilderTest.cpp delete mode 100644 unittests/VMCore/InstructionsTest.cpp delete mode 100644 unittests/VMCore/MDBuilderTest.cpp delete mode 100644 unittests/VMCore/Makefile delete mode 100644 unittests/VMCore/MetadataTest.cpp delete mode 100644 unittests/VMCore/PassManagerTest.cpp delete mode 100644 unittests/VMCore/TypeBuilderTest.cpp delete mode 100644 unittests/VMCore/TypesTest.cpp delete mode 100644 unittests/VMCore/ValueMapTest.cpp delete mode 100644 unittests/VMCore/VerifierTest.cpp delete mode 100644 unittests/VMCore/WaymarkTest.cpp diff --git a/unittests/CMakeLists.txt b/unittests/CMakeLists.txt index e2b7563a28..a3f8bf34e7 100644 --- a/unittests/CMakeLists.txt +++ b/unittests/CMakeLists.txt @@ -12,4 +12,4 @@ add_subdirectory(Bitcode) add_subdirectory(Option) add_subdirectory(Support) add_subdirectory(Transforms) -add_subdirectory(VMCore) +add_subdirectory(IR) diff --git a/unittests/IR/CMakeLists.txt b/unittests/IR/CMakeLists.txt new file mode 100644 index 0000000000..4ff94f7035 --- /dev/null +++ b/unittests/IR/CMakeLists.txt @@ -0,0 +1,36 @@ +set(LLVM_LINK_COMPONENTS + asmparser + core + ipa + ) + +set(IRSources + ConstantsTest.cpp + DominatorTreeTest.cpp + IRBuilderTest.cpp + InstructionsTest.cpp + MDBuilderTest.cpp + MetadataTest.cpp + PassManagerTest.cpp + TypeBuilderTest.cpp + TypesTest.cpp + ValueMapTest.cpp + VerifierTest.cpp + WaymarkTest.cpp + ) + +# MSVC9 and 8 cannot compile ValueMapTest.cpp due to their bug. +# See issue#331418 in Visual Studio. +if(MSVC AND MSVC_VERSION LESS 1600) + list(REMOVE_ITEM IRSources ValueMapTest.cpp) +endif() + +# HACK: Declare a couple of source files as optionally compiled to satisfy the +# missing-file-checker in LLVM's weird CMake build. +set(LLVM_OPTIONAL_SOURCES + ValueMapTest.cpp + ) + +add_llvm_unittest(IRTests + ${IRSources} + ) diff --git a/unittests/IR/ConstantsTest.cpp b/unittests/IR/ConstantsTest.cpp new file mode 100644 index 0000000000..be34c1e2a1 --- /dev/null +++ b/unittests/IR/ConstantsTest.cpp @@ -0,0 +1,228 @@ +//===- llvm/unittest/IR/ConstantsTest.cpp - Constants 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/IR/Constants.h" +#include "llvm/IR/DerivedTypes.h" +#include "llvm/IR/InstrTypes.h" +#include "llvm/IR/Instruction.h" +#include "llvm/IR/LLVMContext.h" +#include "llvm/IR/Module.h" +#include "gtest/gtest.h" + +namespace llvm { +namespace { + +TEST(ConstantsTest, Integer_i1) { + IntegerType* Int1 = IntegerType::get(getGlobalContext(), 1); + Constant* One = ConstantInt::get(Int1, 1, true); + Constant* Zero = ConstantInt::get(Int1, 0); + Constant* NegOne = ConstantInt::get(Int1, static_cast(-1), true); + EXPECT_EQ(NegOne, ConstantInt::getSigned(Int1, -1)); + Constant* Undef = UndefValue::get(Int1); + + // Input: @b = constant i1 add(i1 1 , i1 1) + // Output: @b = constant i1 false + EXPECT_EQ(Zero, ConstantExpr::getAdd(One, One)); + + // @c = constant i1 add(i1 -1, i1 1) + // @c = constant i1 false + EXPECT_EQ(Zero, ConstantExpr::getAdd(NegOne, One)); + + // @d = constant i1 add(i1 -1, i1 -1) + // @d = constant i1 false + EXPECT_EQ(Zero, ConstantExpr::getAdd(NegOne, NegOne)); + + // @e = constant i1 sub(i1 -1, i1 1) + // @e = constant i1 false + EXPECT_EQ(Zero, ConstantExpr::getSub(NegOne, One)); + + // @f = constant i1 sub(i1 1 , i1 -1) + // @f = constant i1 false + EXPECT_EQ(Zero, ConstantExpr::getSub(One, NegOne)); + + // @g = constant i1 sub(i1 1 , i1 1) + // @g = constant i1 false + EXPECT_EQ(Zero, ConstantExpr::getSub(One, One)); + + // @h = constant i1 shl(i1 1 , i1 1) ; undefined + // @h = constant i1 undef + EXPECT_EQ(Undef, ConstantExpr::getShl(One, One)); + + // @i = constant i1 shl(i1 1 , i1 0) + // @i = constant i1 true + EXPECT_EQ(One, ConstantExpr::getShl(One, Zero)); + + // @j = constant i1 lshr(i1 1, i1 1) ; undefined + // @j = constant i1 undef + EXPECT_EQ(Undef, ConstantExpr::getLShr(One, One)); + + // @m = constant i1 ashr(i1 1, i1 1) ; undefined + // @m = constant i1 undef + EXPECT_EQ(Undef, ConstantExpr::getAShr(One, One)); + + // @n = constant i1 mul(i1 -1, i1 1) + // @n = constant i1 true + EXPECT_EQ(One, ConstantExpr::getMul(NegOne, One)); + + // @o = constant i1 sdiv(i1 -1, i1 1) ; overflow + // @o = constant i1 true + EXPECT_EQ(One, ConstantExpr::getSDiv(NegOne, One)); + + // @p = constant i1 sdiv(i1 1 , i1 -1); overflow + // @p = constant i1 true + EXPECT_EQ(One, ConstantExpr::getSDiv(One, NegOne)); + + // @q = constant i1 udiv(i1 -1, i1 1) + // @q = constant i1 true + EXPECT_EQ(One, ConstantExpr::getUDiv(NegOne, One)); + + // @r = constant i1 udiv(i1 1, i1 -1) + // @r = constant i1 true + EXPECT_EQ(One, ConstantExpr::getUDiv(One, NegOne)); + + // @s = constant i1 srem(i1 -1, i1 1) ; overflow + // @s = constant i1 false + EXPECT_EQ(Zero, ConstantExpr::getSRem(NegOne, One)); + + // @t = constant i1 urem(i1 -1, i1 1) + // @t = constant i1 false + EXPECT_EQ(Zero, ConstantExpr::getURem(NegOne, One)); + + // @u = constant i1 srem(i1 1, i1 -1) ; overflow + // @u = constant i1 false + EXPECT_EQ(Zero, ConstantExpr::getSRem(One, NegOne)); +} + +TEST(ConstantsTest, IntSigns) { + IntegerType* Int8Ty = Type::getInt8Ty(getGlobalContext()); + EXPECT_EQ(100, ConstantInt::get(Int8Ty, 100, false)->getSExtValue()); + EXPECT_EQ(100, ConstantInt::get(Int8Ty, 100, true)->getSExtValue()); + EXPECT_EQ(100, ConstantInt::getSigned(Int8Ty, 100)->getSExtValue()); + EXPECT_EQ(-50, ConstantInt::get(Int8Ty, 206)->getSExtValue()); + EXPECT_EQ(-50, ConstantInt::getSigned(Int8Ty, -50)->getSExtValue()); + EXPECT_EQ(206U, ConstantInt::getSigned(Int8Ty, -50)->getZExtValue()); + + // Overflow is handled by truncation. + EXPECT_EQ(0x3b, ConstantInt::get(Int8Ty, 0x13b)->getSExtValue()); +} + +TEST(ConstantsTest, FP128Test) { + Type *FP128Ty = Type::getFP128Ty(getGlobalContext()); + + IntegerType *Int128Ty = Type::getIntNTy(getGlobalContext(), 128); + Constant *Zero128 = Constant::getNullValue(Int128Ty); + Constant *X = ConstantExpr::getUIToFP(Zero128, FP128Ty); + EXPECT_TRUE(isa(X)); +} + +#define CHECK(x, y) { \ + std::string __s; \ + raw_string_ostream __o(__s); \ + cast(x)->getAsInstruction()->print(__o); \ + __o.flush(); \ + EXPECT_EQ(std::string(" = " y), __s); \ + } + +TEST(ConstantsTest, AsInstructionsTest) { + Module *M = new Module("MyModule", getGlobalContext()); + + Type *Int64Ty = Type::getInt64Ty(getGlobalContext()); + Type *Int32Ty = Type::getInt32Ty(getGlobalContext()); + Type *Int16Ty = Type::getInt16Ty(getGlobalContext()); + Type *Int1Ty = Type::getInt1Ty(getGlobalContext()); + Type *FloatTy = Type::getFloatTy(getGlobalContext()); + Type *DoubleTy = Type::getDoubleTy(getGlobalContext()); + + Constant *Global = M->getOrInsertGlobal("dummy", + PointerType::getUnqual(Int32Ty)); + Constant *Global2 = M->getOrInsertGlobal("dummy2", + PointerType::getUnqual(Int32Ty)); + + Constant *P0 = ConstantExpr::getPtrToInt(Global, Int32Ty); + Constant *P1 = ConstantExpr::getUIToFP(P0, FloatTy); + Constant *P2 = ConstantExpr::getUIToFP(P0, DoubleTy); + Constant *P3 = ConstantExpr::getTrunc(P0, Int1Ty); + Constant *P4 = ConstantExpr::getPtrToInt(Global2, Int32Ty); + Constant *P5 = ConstantExpr::getUIToFP(P4, FloatTy); + Constant *P6 = ConstantExpr::getBitCast(P4, VectorType::get(Int16Ty, 2)); + + Constant *One = ConstantInt::get(Int32Ty, 1); + + #define P0STR "ptrtoint (i32** @dummy to i32)" + #define P1STR "uitofp (i32 ptrtoint (i32** @dummy to i32) to float)" + #define P2STR "uitofp (i32 ptrtoint (i32** @dummy to i32) to double)" + #define P3STR "ptrtoint (i32** @dummy to i1)" + #define P4STR "ptrtoint (i32** @dummy2 to i32)" + #define P5STR "uitofp (i32 ptrtoint (i32** @dummy2 to i32) to float)" + #define P6STR "bitcast (i32 ptrtoint (i32** @dummy2 to i32) to <2 x i16>)" + + CHECK(ConstantExpr::getNeg(P0), "sub i32 0, " P0STR); + CHECK(ConstantExpr::getFNeg(P1), "fsub float -0.000000e+00, " P1STR); + CHECK(ConstantExpr::getNot(P0), "xor i32 " P0STR ", -1"); + CHECK(ConstantExpr::getAdd(P0, P0), "add i32 " P0STR ", " P0STR); + CHECK(ConstantExpr::getAdd(P0, P0, false, true), "add nsw i32 " P0STR ", " + P0STR); + CHECK(ConstantExpr::getAdd(P0, P0, true, true), "add nuw nsw i32 " P0STR ", " + P0STR); + CHECK(ConstantExpr::getFAdd(P1, P1), "fadd float " P1STR ", " P1STR); + CHECK(ConstantExpr::getSub(P0, P0), "sub i32 " P0STR ", " P0STR); + CHECK(ConstantExpr::getFSub(P1, P1), "fsub float " P1STR ", " P1STR); + CHECK(ConstantExpr::getMul(P0, P0), "mul i32 " P0STR ", " P0STR); + CHECK(ConstantExpr::getFMul(P1, P1), "fmul float " P1STR ", " P1STR); + CHECK(ConstantExpr::getUDiv(P0, P0), "udiv i32 " P0STR ", " P0STR); + CHECK(ConstantExpr::getSDiv(P0, P0), "sdiv i32 " P0STR ", " P0STR); + CHECK(ConstantExpr::getFDiv(P1, P1), "fdiv float " P1STR ", " P1STR); + CHECK(ConstantExpr::getURem(P0, P0), "urem i32 " P0STR ", " P0STR); + CHECK(ConstantExpr::getSRem(P0, P0), "srem i32 " P0STR ", " P0STR); + CHECK(ConstantExpr::getFRem(P1, P1), "frem float " P1STR ", " P1STR); + CHECK(ConstantExpr::getAnd(P0, P0), "and i32 " P0STR ", " P0STR); + CHECK(ConstantExpr::getOr(P0, P0), "or i32 " P0STR ", " P0STR); + CHECK(ConstantExpr::getXor(P0, P0), "xor i32 " P0STR ", " P0STR); + CHECK(ConstantExpr::getShl(P0, P0), "shl i32 " P0STR ", " P0STR); + CHECK(ConstantExpr::getShl(P0, P0, true), "shl nuw i32 " P0STR ", " P0STR); + CHECK(ConstantExpr::getShl(P0, P0, false, true), "shl nsw i32 " P0STR ", " + P0STR); + CHECK(ConstantExpr::getLShr(P0, P0, false), "lshr i32 " P0STR ", " P0STR); + CHECK(ConstantExpr::getLShr(P0, P0, true), "lshr exact i32 " P0STR ", " P0STR); + CHECK(ConstantExpr::getAShr(P0, P0, false), "ashr i32 " P0STR ", " P0STR); + CHECK(ConstantExpr::getAShr(P0, P0, true), "ashr exact i32 " P0STR ", " P0STR); + + CHECK(ConstantExpr::getSExt(P0, Int64Ty), "sext i32 " P0STR " to i64"); + CHECK(ConstantExpr::getZExt(P0, Int64Ty), "zext i32 " P0STR " to i64"); + CHECK(ConstantExpr::getFPTrunc(P2, FloatTy), "fptrunc double " P2STR + " to float"); + CHECK(ConstantExpr::getFPExtend(P1, DoubleTy), "fpext float " P1STR + " to double"); + + CHECK(ConstantExpr::getExactUDiv(P0, P0), "udiv exact i32 " P0STR ", " P0STR); + + CHECK(ConstantExpr::getSelect(P3, P0, P4), "select i1 " P3STR ", i32 " P0STR + ", i32 " P4STR); + CHECK(ConstantExpr::getICmp(CmpInst::ICMP_EQ, P0, P4), "icmp eq i32 " P0STR + ", " P4STR); + CHECK(ConstantExpr::getFCmp(CmpInst::FCMP_ULT, P1, P5), "fcmp ult float " + P1STR ", " P5STR); + + std::vector V; + V.push_back(One); + // FIXME: getGetElementPtr() actually creates an inbounds ConstantGEP, + // not a normal one! + //CHECK(ConstantExpr::getGetElementPtr(Global, V, false), + // "getelementptr i32** @dummy, i32 1"); + CHECK(ConstantExpr::getInBoundsGetElementPtr(Global, V), + "getelementptr inbounds i32** @dummy, i32 1"); + + CHECK(ConstantExpr::getExtractElement(P6, One), "extractelement <2 x i16> " + P6STR ", i32 1"); +} + +#undef CHECK + +} // end anonymous namespace +} // end namespace llvm diff --git a/unittests/IR/DominatorTreeTest.cpp b/unittests/IR/DominatorTreeTest.cpp new file mode 100644 index 0000000000..3a527adbc0 --- /dev/null +++ b/unittests/IR/DominatorTreeTest.cpp @@ -0,0 +1,195 @@ +#include "llvm/Analysis/Dominators.h" +#include "llvm/Assembly/Parser.h" +#include "llvm/IR/Instructions.h" +#include "llvm/IR/LLVMContext.h" +#include "llvm/IR/Module.h" +#include "llvm/PassManager.h" +#include "llvm/Support/SourceMgr.h" +#include "gtest/gtest.h" + +using namespace llvm; + +namespace llvm { + void initializeDPassPass(PassRegistry&); + + namespace { + struct DPass : public FunctionPass { + static char ID; + virtual bool runOnFunction(Function &F) { + DominatorTree *DT = &getAnalysis(); + Function::iterator FI = F.begin(); + + BasicBlock *BB0 = FI++; + BasicBlock::iterator BBI = BB0->begin(); + Instruction *Y1 = BBI++; + Instruction *Y2 = BBI++; + Instruction *Y3 = BBI++; + + BasicBlock *BB1 = FI++; + BBI = BB1->begin(); + Instruction *Y4 = BBI++; + + BasicBlock *BB2 = FI++; + BBI = BB2->begin(); + Instruction *Y5 = BBI++; + + BasicBlock *BB3 = FI++; + BBI = BB3->begin(); + Instruction *Y6 = BBI++; + Instruction *Y7 = BBI++; + + BasicBlock *BB4 = FI++; + BBI = BB4->begin(); + Instruction *Y8 = BBI++; + Instruction *Y9 = BBI++; + + // Reachability + EXPECT_TRUE(DT->isReachableFromEntry(BB0)); + EXPECT_TRUE(DT->isReachableFromEntry(BB1)); + EXPECT_TRUE(DT->isReachableFromEntry(BB2)); + EXPECT_FALSE(DT->isReachableFromEntry(BB3)); + EXPECT_TRUE(DT->isReachableFromEntry(BB4)); + + // BB dominance + EXPECT_TRUE(DT->dominates(BB0, BB0)); + EXPECT_TRUE(DT->dominates(BB0, BB1)); + EXPECT_TRUE(DT->dominates(BB0, BB2)); + EXPECT_TRUE(DT->dominates(BB0, BB3)); + EXPECT_TRUE(DT->dominates(BB0, BB4)); + + EXPECT_FALSE(DT->dominates(BB1, BB0)); + EXPECT_TRUE(DT->dominates(BB1, BB1)); + EXPECT_FALSE(DT->dominates(BB1, BB2)); + EXPECT_TRUE(DT->dominates(BB1, BB3)); + EXPECT_FALSE(DT->dominates(BB1, BB4)); + + EXPECT_FALSE(DT->dominates(BB2, BB0)); + EXPECT_FALSE(DT->dominates(BB2, BB1)); + EXPECT_TRUE(DT->dominates(BB2, BB2)); + EXPECT_TRUE(DT->dominates(BB2, BB3)); + EXPECT_FALSE(DT->dominates(BB2, BB4)); + + EXPECT_FALSE(DT->dominates(BB3, BB0)); + EXPECT_FALSE(DT->dominates(BB3, BB1)); + EXPECT_FALSE(DT->dominates(BB3, BB2)); + EXPECT_TRUE(DT->dominates(BB3, BB3)); + EXPECT_FALSE(DT->dominates(BB3, BB4)); + + // BB proper dominance + EXPECT_FALSE(DT->properlyDominates(BB0, BB0)); + EXPECT_TRUE(DT->properlyDominates(BB0, BB1)); + EXPECT_TRUE(DT->properlyDominates(BB0, BB2)); + EXPECT_TRUE(DT->properlyDominates(BB0, BB3)); + + EXPECT_FALSE(DT->properlyDominates(BB1, BB0)); + EXPECT_FALSE(DT->properlyDominates(BB1, BB1)); + EXPECT_FALSE(DT->properlyDominates(BB1, BB2)); + EXPECT_TRUE(DT->properlyDominates(BB1, BB3)); + + EXPECT_FALSE(DT->properlyDominates(BB2, BB0)); + EXPECT_FALSE(DT->properlyDominates(BB2, BB1)); + EXPECT_FALSE(DT->properlyDominates(BB2, BB2)); + EXPECT_TRUE(DT->properlyDominates(BB2, BB3)); + + EXPECT_FALSE(DT->properlyDominates(BB3, BB0)); + EXPECT_FALSE(DT->properlyDominates(BB3, BB1)); + EXPECT_FALSE(DT->properlyDominates(BB3, BB2)); + EXPECT_FALSE(DT->properlyDominates(BB3, BB3)); + + // Instruction dominance in the same reachable BB + EXPECT_FALSE(DT->dominates(Y1, Y1)); + EXPECT_TRUE(DT->dominates(Y1, Y2)); + EXPECT_FALSE(DT->dominates(Y2, Y1)); + EXPECT_FALSE(DT->dominates(Y2, Y2)); + + // Instruction dominance in the same unreachable BB + EXPECT_TRUE(DT->dominates(Y6, Y6)); + EXPECT_TRUE(DT->dominates(Y6, Y7)); + EXPECT_TRUE(DT->dominates(Y7, Y6)); + EXPECT_TRUE(DT->dominates(Y7, Y7)); + + // Invoke + EXPECT_TRUE(DT->dominates(Y3, Y4)); + EXPECT_FALSE(DT->dominates(Y3, Y5)); + + // Phi + EXPECT_TRUE(DT->dominates(Y2, Y9)); + EXPECT_FALSE(DT->dominates(Y3, Y9)); + EXPECT_FALSE(DT->dominates(Y8, Y9)); + + // Anything dominates unreachable + EXPECT_TRUE(DT->dominates(Y1, Y6)); + EXPECT_TRUE(DT->dominates(Y3, Y6)); + + // Unreachable doesn't dominate reachable + EXPECT_FALSE(DT->dominates(Y6, Y1)); + + // Instruction, BB dominance + EXPECT_FALSE(DT->dominates(Y1, BB0)); + EXPECT_TRUE(DT->dominates(Y1, BB1)); + EXPECT_TRUE(DT->dominates(Y1, BB2)); + EXPECT_TRUE(DT->dominates(Y1, BB3)); + EXPECT_TRUE(DT->dominates(Y1, BB4)); + + EXPECT_FALSE(DT->dominates(Y3, BB0)); + EXPECT_TRUE(DT->dominates(Y3, BB1)); + EXPECT_FALSE(DT->dominates(Y3, BB2)); + EXPECT_TRUE(DT->dominates(Y3, BB3)); + EXPECT_FALSE(DT->dominates(Y3, BB4)); + + EXPECT_TRUE(DT->dominates(Y6, BB3)); + + return false; + } + virtual void getAnalysisUsage(AnalysisUsage &AU) const { + AU.addRequired(); + } + DPass() : FunctionPass(ID) { + initializeDPassPass(*PassRegistry::getPassRegistry()); + } + }; + char DPass::ID = 0; + + + Module* makeLLVMModule(DPass *P) { + const char *ModuleStrig = + "declare i32 @g()\n" \ + "define void @f(i32 %x) {\n" \ + "bb0:\n" \ + " %y1 = add i32 %x, 1\n" \ + " %y2 = add i32 %x, 1\n" \ + " %y3 = invoke i32 @g() to label %bb1 unwind label %bb2\n" \ + "bb1:\n" \ + " %y4 = add i32 %x, 1\n" \ + " br label %bb4\n" \ + "bb2:\n" \ + " %y5 = landingpad i32 personality i32 ()* @g\n" \ + " cleanup\n" \ + " br label %bb4\n" \ + "bb3:\n" \ + " %y6 = add i32 %x, 1\n" \ + " %y7 = add i32 %x, 1\n" \ + " ret void\n" \ + "bb4:\n" \ + " %y8 = phi i32 [0, %bb2], [%y4, %bb1]\n" + " %y9 = phi i32 [0, %bb2], [%y4, %bb1]\n" + " ret void\n" \ + "}\n"; + LLVMContext &C = getGlobalContext(); + SMDiagnostic Err; + return ParseAssemblyString(ModuleStrig, NULL, Err, C); + } + + TEST(DominatorTree, Unreachable) { + DPass *P = new DPass(); + Module *M = makeLLVMModule(P); + PassManager Passes; + Passes.add(P); + Passes.run(*M); + } + } +} + +INITIALIZE_PASS_BEGIN(DPass, "dpass", "dpass", false, false) +INITIALIZE_PASS_DEPENDENCY(DominatorTree) +INITIALIZE_PASS_END(DPass, "dpass", "dpass", false, false) diff --git a/unittests/IR/IRBuilderTest.cpp b/unittests/IR/IRBuilderTest.cpp new file mode 100644 index 0000000000..c56721f51b --- /dev/null +++ b/unittests/IR/IRBuilderTest.cpp @@ -0,0 +1,184 @@ +//===- llvm/unittest/IR/IRBuilderTest.cpp - IRBuilder tests ---------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "llvm/IR/IRBuilder.h" +#include "llvm/ADT/OwningPtr.h" +#include "llvm/IR/BasicBlock.h" +#include "llvm/IR/DataLayout.h" +#include "llvm/IR/Function.h" +#include "llvm/IR/IntrinsicInst.h" +#include "llvm/IR/LLVMContext.h" +#include "llvm/IR/MDBuilder.h" +#include "llvm/IR/Module.h" +#include "gtest/gtest.h" + +using namespace llvm; + +namespace { + +class IRBuilderTest : public testing::Test { +protected: + virtual void SetUp() { + M.reset(new Module("MyModule", getGlobalContext())); + FunctionType *FTy = FunctionType::get(Type::getVoidTy(getGlobalContext()), + /*isVarArg=*/false); + F = Function::Create(FTy, Function::ExternalLinkage, "", M.get()); + BB = BasicBlock::Create(getGlobalContext(), "", F); + GV = new GlobalVariable(Type::getFloatTy(getGlobalContext()), true, + GlobalValue::ExternalLinkage); + } + + virtual void TearDown() { + BB = 0; + M.reset(); + } + + OwningPtr M; + Function *F; + BasicBlock *BB; + GlobalVariable *GV; +}; + +TEST_F(IRBuilderTest, Lifetime) { + IRBuilder<> Builder(BB); + AllocaInst *Var1 = Builder.CreateAlloca(Builder.getInt8Ty()); + AllocaInst *Var2 = Builder.CreateAlloca(Builder.getInt32Ty()); + AllocaInst *Var3 = Builder.CreateAlloca(Builder.getInt8Ty(), + Builder.getInt32(123)); + + CallInst *Start1 = Builder.CreateLifetimeStart(Var1); + CallInst *Start2 = Builder.CreateLifetimeStart(Var2); + CallInst *Start3 = Builder.CreateLifetimeStart(Var3, Builder.getInt64(100)); + + EXPECT_EQ(Start1->getArgOperand(0), Builder.getInt64(-1)); + EXPECT_EQ(Start2->getArgOperand(0), Builder.getInt64(-1)); + EXPECT_EQ(Start3->getArgOperand(0), Builder.getInt64(100)); + + EXPECT_EQ(Start1->getArgOperand(1), Var1); + EXPECT_NE(Start2->getArgOperand(1), Var2); + EXPECT_EQ(Start3->getArgOperand(1), Var3); + + Value *End1 = Builder.CreateLifetimeEnd(Var1); + Builder.CreateLifetimeEnd(Var2); + Builder.CreateLifetimeEnd(Var3); + + IntrinsicInst *II_Start1 = dyn_cast(Start1); + IntrinsicInst *II_End1 = dyn_cast(End1); + ASSERT_TRUE(II_Start1 != NULL); + EXPECT_EQ(II_Start1->getIntrinsicID(), Intrinsic::lifetime_start); + ASSERT_TRUE(II_End1 != NULL); + EXPECT_EQ(II_End1->getIntrinsicID(), Intrinsic::lifetime_end); +} + +TEST_F(IRBuilderTest, CreateCondBr) { + IRBuilder<> Builder(BB); + BasicBlock *TBB = BasicBlock::Create(getGlobalContext(), "", F); + BasicBlock *FBB = BasicBlock::Create(getGlobalContext(), "", F); + + BranchInst *BI = Builder.CreateCondBr(Builder.getTrue(), TBB, FBB); + TerminatorInst *TI = BB->getTerminator(); + EXPECT_EQ(BI, TI); + EXPECT_EQ(2u, TI->getNumSuccessors()); + EXPECT_EQ(TBB, TI->getSuccessor(0)); + EXPECT_EQ(FBB, TI->getSuccessor(1)); + + BI->eraseFromParent(); + MDNode *Weights = MDBuilder(getGlobalContext()).createBranchWeights(42, 13); + BI = Builder.CreateCondBr(Builder.getTrue(), TBB, FBB, Weights); + TI = BB->getTerminator(); + EXPECT_EQ(BI, TI); + EXPECT_EQ(2u, TI->getNumSuccessors()); + EXPECT_EQ(TBB, TI->getSuccessor(0)); + EXPECT_EQ(FBB, TI->getSuccessor(1)); + EXPECT_EQ(Weights, TI->getMetadata(LLVMContext::MD_prof)); +} + +TEST_F(IRBuilderTest, LandingPadName) { + IRBuilder<> Builder(BB); + LandingPadInst *LP = Builder.CreateLandingPad(Builder.getInt32Ty(), + Builder.getInt32(0), 0, "LP"); + EXPECT_EQ(LP->getName(), "LP"); +} + +TEST_F(IRBuilderTest, GetIntTy) { + IRBuilder<> Builder(BB); + IntegerType *Ty1 = Builder.getInt1Ty(); + EXPECT_EQ(Ty1, IntegerType::get(getGlobalContext(), 1)); + + DataLayout* DL = new DataLayout(M.get()); + IntegerType *IntPtrTy = Builder.getIntPtrTy(DL); + unsigned IntPtrBitSize = DL->getPointerSizeInBits(0); + EXPECT_EQ(IntPtrTy, IntegerType::get(getGlobalContext(), IntPtrBitSize)); +} + +TEST_F(IRBuilderTest, FastMathFlags) { + IRBuilder<> Builder(BB); + Value *F; + Instruction *FDiv, *FAdd; + + F = Builder.CreateLoad(GV); + F = Builder.CreateFAdd(F, F); + + EXPECT_FALSE(Builder.getFastMathFlags().any()); + ASSERT_TRUE(isa(F)); + FAdd = cast(F); + EXPECT_FALSE(FAdd->hasNoNaNs()); + + FastMathFlags FMF; + Builder.SetFastMathFlags(FMF); + + F = Builder.CreateFAdd(F, F); + EXPECT_FALSE(Builder.getFastMathFlags().any()); + + FMF.setUnsafeAlgebra(); + Builder.SetFastMathFlags(FMF); + + F = Builder.CreateFAdd(F, F); + EXPECT_TRUE(Builder.getFastMathFlags().any()); + ASSERT_TRUE(isa(F)); + FAdd = cast(F); + EXPECT_TRUE(FAdd->hasNoNaNs()); + + F = Builder.CreateFDiv(F, F); + EXPECT_TRUE(Builder.getFastMathFlags().any()); + EXPECT_TRUE(Builder.getFastMathFlags().UnsafeAlgebra); + ASSERT_TRUE(isa(F)); + FDiv = cast(F); + EXPECT_TRUE(FDiv->hasAllowReciprocal()); + + Builder.clearFastMathFlags(); + + F = Builder.CreateFDiv(F, F); + ASSERT_TRUE(isa(F)); + FDiv = cast(F); + EXPECT_FALSE(FDiv->hasAllowReciprocal()); + + FMF.clear(); + FMF.setAllowReciprocal(); + Builder.SetFastMathFlags(FMF); + + F = Builder.CreateFDiv(F, F); + EXPECT_TRUE(Builder.getFastMathFlags().any()); + EXPECT_TRUE(Builder.getFastMathFlags().AllowReciprocal); + ASSERT_TRUE(isa(F)); + FDiv = cast(F); + EXPECT_TRUE(FDiv->hasAllowReciprocal()); + + Builder.clearFastMathFlags(); + + F = Builder.CreateFDiv(F, F); + ASSERT_TRUE(isa(F)); + FDiv = cast(F); + EXPECT_FALSE(FDiv->getFastMathFlags().any()); + FDiv->copyFastMathFlags(FAdd); + EXPECT_TRUE(FDiv->hasNoNaNs()); + +} + +} diff --git a/unittests/IR/InstructionsTest.cpp b/unittests/IR/InstructionsTest.cpp new file mode 100644 index 0000000000..601a84b2d1 --- /dev/null +++ b/unittests/IR/InstructionsTest.cpp @@ -0,0 +1,284 @@ +//===- llvm/unittest/IR/InstructionsTest.cpp - Instructions 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/IR/Instructions.h" +#include "llvm/ADT/STLExtras.h" +#include "llvm/Analysis/ValueTracking.h" +#include "llvm/IR/BasicBlock.h" +#include "llvm/IR/Constants.h" +#include "llvm/IR/DataLayout.h" +#include "llvm/IR/DerivedTypes.h" +#include "llvm/IR/IRBuilder.h" +#include "llvm/IR/LLVMContext.h" +#include "llvm/IR/MDBuilder.h" +#include "llvm/IR/Operator.h" +#include "gtest/gtest.h" + +namespace llvm { +namespace { + +TEST(InstructionsTest, ReturnInst) { + LLVMContext &C(getGlobalContext()); + + // test for PR6589 + const ReturnInst* r0 = ReturnInst::Create(C); + EXPECT_EQ(r0->getNumOperands(), 0U); + EXPECT_EQ(r0->op_begin(), r0->op_end()); + + IntegerType* Int1 = IntegerType::get(C, 1); + Constant* One = ConstantInt::get(Int1, 1, true); + const ReturnInst* r1 = ReturnInst::Create(C, One); + EXPECT_EQ(1U, r1->getNumOperands()); + User::const_op_iterator b(r1->op_begin()); + EXPECT_NE(r1->op_end(), b); + EXPECT_EQ(One, *b); + EXPECT_EQ(One, r1->getOperand(0)); + ++b; + EXPECT_EQ(r1->op_end(), b); + + // clean up + delete r0; + delete r1; +} + +TEST(InstructionsTest, BranchInst) { + LLVMContext &C(getGlobalContext()); + + // Make a BasicBlocks + BasicBlock* bb0 = BasicBlock::Create(C); + BasicBlock* bb1 = BasicBlock::Create(C); + + // Mandatory BranchInst + const BranchInst* b0 = BranchInst::Create(bb0); + + EXPECT_TRUE(b0->isUnconditional()); + EXPECT_FALSE(b0->isConditional()); + EXPECT_EQ(1U, b0->getNumSuccessors()); + + // check num operands + EXPECT_EQ(1U, b0->getNumOperands()); + + EXPECT_NE(b0->op_begin(), b0->op_end()); + EXPECT_EQ(b0->op_end(), llvm::next(b0->op_begin())); + + EXPECT_EQ(b0->op_end(), llvm::next(b0->op_begin())); + + IntegerType* Int1 = IntegerType::get(C, 1); + Constant* One = ConstantInt::get(Int1, 1, true); + + // Conditional BranchInst + BranchInst* b1 = BranchInst::Create(bb0, bb1, One); + + EXPECT_FALSE(b1->isUnconditional()); + EXPECT_TRUE(b1->isConditional()); + EXPECT_EQ(2U, b1->getNumSuccessors()); + + // check num operands + EXPECT_EQ(3U, b1->getNumOperands()); + + User::const_op_iterator b(b1->op_begin()); + + // check COND + EXPECT_NE(b, b1->op_end()); + EXPECT_EQ(One, *b); + EXPECT_EQ(One, b1->getOperand(0)); + EXPECT_EQ(One, b1->getCondition()); + ++b; + + // check ELSE + EXPECT_EQ(bb1, *b); + EXPECT_EQ(bb1, b1->getOperand(1)); + EXPECT_EQ(bb1, b1->getSuccessor(1)); + ++b; + + // check THEN + EXPECT_EQ(bb0, *b); + EXPECT_EQ(bb0, b1->getOperand(2)); + EXPECT_EQ(bb0, b1->getSuccessor(0)); + ++b; + + EXPECT_EQ(b1->op_end(), b); + + // clean up + delete b0; + delete b1; + + delete bb0; + delete bb1; +} + +TEST(InstructionsTest, CastInst) { + LLVMContext &C(getGlobalContext()); + + Type* Int8Ty = Type::getInt8Ty(C); + Type* Int64Ty = Type::getInt64Ty(C); + Type* V8x8Ty = VectorType::get(Int8Ty, 8); + Type* V8x64Ty = VectorType::get(Int64Ty, 8); + Type* X86MMXTy = Type::getX86_MMXTy(C); + + const Constant* c8 = Constant::getNullValue(V8x8Ty); + const Constant* c64 = Constant::getNullValue(V8x64Ty); + + EXPECT_TRUE(CastInst::isCastable(V8x8Ty, X86MMXTy)); + EXPECT_TRUE(CastInst::isCastable(X86MMXTy, V8x8Ty)); + EXPECT_FALSE(CastInst::isCastable(Int64Ty, X86MMXTy)); + EXPECT_TRUE(CastInst::isCastable(V8x64Ty, V8x8Ty)); + EXPECT_TRUE(CastInst::isCastable(V8x8Ty, V8x64Ty)); + EXPECT_EQ(CastInst::Trunc, CastInst::getCastOpcode(c64, true, V8x8Ty, true)); + EXPECT_EQ(CastInst::SExt, CastInst::getCastOpcode(c8, true, V8x64Ty, true)); +} + + + +TEST(InstructionsTest, VectorGep) { + LLVMContext &C(getGlobalContext()); + + // Type Definitions + PointerType *Ptri8Ty = PointerType::get(IntegerType::get(C, 8), 0); + PointerType *Ptri32Ty = PointerType::get(IntegerType::get(C, 8), 0); + + VectorType *V2xi8PTy = VectorType::get(Ptri8Ty, 2); + VectorType *V2xi32PTy = VectorType::get(Ptri32Ty, 2); + + // Test different aspects of the vector-of-pointers type + // and GEPs which use this type. + ConstantInt *Ci32a = ConstantInt::get(C, APInt(32, 1492)); + ConstantInt *Ci32b = ConstantInt::get(C, APInt(32, 1948)); + std::vector ConstVa(2, Ci32a); + std::vector ConstVb(2, Ci32b); + Constant *C2xi32a = ConstantVector::get(ConstVa); + Constant *C2xi32b = ConstantVector::get(ConstVb); + + CastInst *PtrVecA = new IntToPtrInst(C2xi32a, V2xi32PTy); + CastInst *PtrVecB = new IntToPtrInst(C2xi32b, V2xi32PTy); + + ICmpInst *ICmp0 = new ICmpInst(ICmpInst::ICMP_SGT, PtrVecA, PtrVecB); + ICmpInst *ICmp1 = new ICmpInst(ICmpInst::ICMP_ULT, PtrVecA, PtrVecB); + EXPECT_NE(ICmp0, ICmp1); // suppress warning. + + GetElementPtrInst *Gep0 = GetElementPtrInst::Create(PtrVecA, C2xi32a); + GetElementPtrInst *Gep1 = GetElementPtrInst::Create(PtrVecA, C2xi32b); + GetElementPtrInst *Gep2 = GetElementPtrInst::Create(PtrVecB, C2xi32a); + GetElementPtrInst *Gep3 = GetElementPtrInst::Create(PtrVecB, C2xi32b); + + CastInst *BTC0 = new BitCastInst(Gep0, V2xi8PTy); + CastInst *BTC1 = new BitCastInst(Gep1, V2xi8PTy); + CastInst *BTC2 = new BitCastInst(Gep2, V2xi8PTy); + CastInst *BTC3 = new BitCastInst(Gep3, V2xi8PTy); + + Value *S0 = BTC0->stripPointerCasts(); + Value *S1 = BTC1->stripPointerCasts(); + Value *S2 = BTC2->stripPointerCasts(); + Value *S3 = BTC3->stripPointerCasts(); + + EXPECT_NE(S0, Gep0); + EXPECT_NE(S1, Gep1); + EXPECT_NE(S2, Gep2); + EXPECT_NE(S3, Gep3); + + int64_t Offset; + DataLayout TD("e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f3" + "2:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80" + ":128:128-n8:16:32:64-S128"); + // Make sure we don't crash + GetPointerBaseWithConstantOffset(Gep0, Offset, TD); + GetPointerBaseWithConstantOffset(Gep1, Offset, TD); + GetPointerBaseWithConstantOffset(Gep2, Offset, TD); + GetPointerBaseWithConstantOffset(Gep3, Offset, TD); + + // Gep of Geps + GetElementPtrInst *GepII0 = GetElementPtrInst::Create(Gep0, C2xi32b); + GetElementPtrInst *GepII1 = GetElementPtrInst::Create(Gep1, C2xi32a); + GetElementPtrInst *GepII2 = GetElementPtrInst::Create(Gep2, C2xi32b); + GetElementPtrInst *GepII3 = GetElementPtrInst::Create(Gep3, C2xi32a); + + EXPECT_EQ(GepII0->getNumIndices(), 1u); + EXPECT_EQ(GepII1->getNumIndices(), 1u); + EXPECT_EQ(GepII2->getNumIndices(), 1u); + EXPECT_EQ(GepII3->getNumIndices(), 1u); + + EXPECT_FALSE(GepII0->hasAllZeroIndices()); + EXPECT_FALSE(GepII1->hasAllZeroIndices()); + EXPECT_FALSE(GepII2->hasAllZeroIndices()); + EXPECT_FALSE(GepII3->hasAllZeroIndices()); + + delete GepII0; + delete GepII1; + delete GepII2; + delete GepII3; + + delete BTC0; + delete BTC1; + delete BTC2; + delete BTC3; + + delete Gep0; + delete Gep1; + delete Gep2; + delete Gep3; + + delete ICmp0; + delete ICmp1; + delete PtrVecA; + delete PtrVecB; +} + +TEST(InstructionsTest, FPMathOperator) { + LLVMContext &Context = getGlobalContext(); + IRBuilder<> Builder(Context); + MDBuilder MDHelper(Context); + Instruction *I = Builder.CreatePHI(Builder.getDoubleTy(), 0); + MDNode *MD1 = MDHelper.createFPMath(1.0); + Value *V1 = Builder.CreateFAdd(I, I, "", MD1); + EXPECT_TRUE(isa(V1)); + FPMathOperator *O1 = cast(V1); + EXPECT_EQ(O1->getFPAccuracy(), 1.0); + delete V1; + delete I; +} + + +TEST(InstructionsTest, isEliminableCastPair) { + LLVMContext &C(getGlobalContext()); + + Type* Int32Ty = Type::getInt32Ty(C); + Type* Int64Ty = Type::getInt64Ty(C); + Type* Int64PtrTy = Type::getInt64PtrTy(C); + + // Source and destination pointers have same size -> bitcast. + EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt, + CastInst::IntToPtr, + Int64PtrTy, Int64Ty, Int64PtrTy, + Int32Ty, 0, Int32Ty), + CastInst::BitCast); + + // Source and destination pointers have different sizes -> fail. + EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt, + CastInst::IntToPtr, + Int64PtrTy, Int64Ty, Int64PtrTy, + Int32Ty, 0, Int64Ty), + 0U); + + // Middle pointer big enough -> bitcast. + EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr, + CastInst::PtrToInt, + Int64Ty, Int64PtrTy, Int64Ty, + 0, Int64Ty, 0), + CastInst::BitCast); + + // Middle pointer too small -> fail. + EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr, + CastInst::PtrToInt, + Int64Ty, Int64PtrTy, Int64Ty, + 0, Int32Ty, 0), + 0U); +} + +} // end anonymous namespace +} // end namespace llvm diff --git a/unittests/IR/MDBuilderTest.cpp b/unittests/IR/MDBuilderTest.cpp new file mode 100644 index 0000000000..665d559bf0 --- /dev/null +++ b/unittests/IR/MDBuilderTest.cpp @@ -0,0 +1,106 @@ +//===- llvm/unittests/MDBuilderTest.cpp - MDBuilder 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/IR/MDBuilder.h" +#include "llvm/IR/IRBuilder.h" +#include "llvm/IR/Operator.h" +#include "gtest/gtest.h" + +using namespace llvm; + +namespace { + +class MDBuilderTest : public testing::Test { +protected: + LLVMContext Context; +}; + +TEST_F(MDBuilderTest, createString) { + MDBuilder MDHelper(Context); + MDString *Str0 = MDHelper.createString(""); + MDString *Str1 = MDHelper.createString("string"); + EXPECT_EQ(Str0->getString(), StringRef("")); + EXPECT_EQ(Str1->getString(), StringRef("string")); +} +TEST_F(MDBuilderTest, createFPMath) { + MDBuilder MDHelper(Context); + MDNode *MD0 = MDHelper.createFPMath(0.0); + MDNode *MD1 = MDHelper.createFPMath(1.0); + EXPECT_EQ(MD0, (MDNode *)0); + EXPECT_NE(MD1, (MDNode *)0); + EXPECT_EQ(MD1->getNumOperands(), 1U); + Value *Op = MD1->getOperand(0); + EXPECT_TRUE(isa(Op)); + EXPECT_TRUE(Op->getType()->isFloatingPointTy()); + ConstantFP *Val = cast(Op); + EXPECT_TRUE(Val->isExactlyValue(1.0)); +} +TEST_F(MDBuilderTest, createRangeMetadata) { + MDBuilder MDHelper(Context); + APInt A(8, 1), B(8, 2); + MDNode *R0 = MDHelper.createRange(A, A); + MDNode *R1 = MDHelper.createRange(A, B); + EXPECT_EQ(R0, (MDNode *)0); + EXPECT_NE(R1, (MDNode *)0); + EXPECT_EQ(R1->getNumOperands(), 2U); + EXPECT_TRUE(isa(R1->getOperand(0))); + EXPECT_TRUE(isa(R1->getOperand(1))); + ConstantInt *C0 = cast(R1->getOperand(0)); + ConstantInt *C1 = cast(R1->getOperand(1)); + EXPECT_EQ(C0->getValue(), A); + EXPECT_EQ(C1->getValue(), B); +} +TEST_F(MDBuilderTest, createAnonymousTBAARoot) { + MDBuilder MDHelper(Context); + MDNode *R0 = MDHelper.createAnonymousTBAARoot(); + MDNode *R1 = MDHelper.createAnonymousTBAARoot(); + EXPECT_NE(R0, R1); + EXPECT_GE(R0->getNumOperands(), 1U); + EXPECT_GE(R1->getNumOperands(), 1U); + EXPECT_EQ(R0->getOperand(0), R0); + EXPECT_EQ(R1->getOperand(0), R1); + EXPECT_TRUE(R0->getNumOperands() == 1 || R0->getOperand(1) == 0); + EXPECT_TRUE(R1->getNumOperands() == 1 || R1->getOperand(1) == 0); +} +TEST_F(MDBuilderTest, createTBAARoot) { + MDBuilder MDHelper(Context); + MDNode *R0 = MDHelper.createTBAARoot("Root"); + MDNode *R1 = MDHelper.createTBAARoot("Root"); + EXPECT_EQ(R0, R1); + EXPECT_GE(R0->getNumOperands(), 1U); + EXPECT_TRUE(isa(R0->getOperand(0))); + EXPECT_EQ(cast(R0->getOperand(0))->getString(), "Root"); + EXPECT_TRUE(R0->getNumOperands() == 1 || R0->getOperand(1) == 0); +} +TEST_F(MDBuilderTest, createTBAANode) { + MDBuilder MDHelper(Context); + MDNode *R = MDHelper.createTBAARoot("Root"); + MDNode *N0 = MDHelper.createTBAANode("Node", R); + MDNode *N1 = MDHelper.createTBAANode("edoN", R); + MDNode *N2 = MDHelper.createTBAANode("Node", R, true); + MDNode *N3 = MDHelper.createTBAANode("Node", R); + EXPECT_EQ(N0, N3); + EXPECT_NE(N0, N1); + EXPECT_NE(N0, N2); + EXPECT_GE(N0->getNumOperands(), 2U); + EXPECT_GE(N1->getNumOperands(), 2U); + EXPECT_GE(N2->getNumOperands(), 3U); + EXPECT_TRUE(isa(N0->getOperand(0))); + EXPECT_TRUE(isa(N1->getOperand(0))); + EXPECT_TRUE(isa(N2->getOperand(0))); + EXPECT_EQ(cast(N0->getOperand(0))->getString(), "Node"); + EXPECT_EQ(cast(N1->getOperand(0))->getString(), "edoN"); + EXPECT_EQ(cast(N2->getOperand(0))->getString(), "Node"); + EXPECT_EQ(N0->getOperand(1), R); + EXPECT_EQ(N1->getOperand(1), R); + EXPECT_EQ(N2->getOperand(1), R); + EXPECT_TRUE(isa(N2->getOperand(2))); + EXPECT_EQ(cast(N2->getOperand(2))->getZExtValue(), 1U); +} +} diff --git a/unittests/IR/Makefile b/unittests/IR/Makefile new file mode 100644 index 0000000000..7c59003c92 --- /dev/null +++ b/unittests/IR/Makefile @@ -0,0 +1,15 @@ +##===- unittests/IR/Makefile -------------------------------*- Makefile -*-===## +# +# The LLVM Compiler Infrastructure +# +# This file is distributed under the University of Illinois Open Source +# License. See LICENSE.TXT for details. +# +##===----------------------------------------------------------------------===## + +LEVEL = ../.. +TESTNAME = IR +LINK_COMPONENTS := core ipa asmparser + +include $(LEVEL)/Makefile.config +include $(LLVM_SRC_ROOT)/unittests/Makefile.unittest diff --git a/unittests/IR/MetadataTest.cpp b/unittests/IR/MetadataTest.cpp new file mode 100644 index 0000000000..352e83ee66 --- /dev/null +++ b/unittests/IR/MetadataTest.cpp @@ -0,0 +1,152 @@ +//===- llvm/unittest/IR/Metadata.cpp - Metadata 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/IR/Metadata.h" +#include "llvm/IR/Constants.h" +#include "llvm/IR/Instructions.h" +#include "llvm/IR/LLVMContext.h" +#include "llvm/IR/Module.h" +#include "llvm/IR/Type.h" +#include "llvm/Support/ValueHandle.h" +#include "llvm/Support/raw_ostream.h" +#include "gtest/gtest.h" +using namespace llvm; + +namespace { + +class MetadataTest : public testing::Test { +protected: + LLVMContext Context; +}; +typedef MetadataTest MDStringTest; + +// Test that construction of MDString with different value produces different +// MDString objects, even with the same string pointer and nulls in the string. +TEST_F(MDStringTest, CreateDifferent) { + char x[3] = { 'f', 0, 'A' }; + MDString *s1 = MDString::get(Context, StringRef(&x[0], 3)); + x[2] = 'B'; + MDString *s2 = MDString::get(Context, StringRef(&x[0], 3)); + EXPECT_NE(s1, s2); +} + +// Test that creation of MDStrings with the same string contents produces the +// same MDString object, even with different pointers. +TEST_F(MDStringTest, CreateSame) { + char x[4] = { 'a', 'b', 'c', 'X' }; + char y[4] = { 'a', 'b', 'c', 'Y' }; + + MDString *s1 = MDString::get(Context, StringRef(&x[0], 3)); + MDString *s2 = MDString::get(Context, StringRef(&y[0], 3)); + EXPECT_EQ(s1, s2); +} + +// Test that MDString prints out the string we fed it. +TEST_F(MDStringTest, PrintingSimple) { + char *str = new char[13]; + strncpy(str, "testing 1 2 3", 13); + MDString *s = MDString::get(Context, StringRef(str, 13)); + strncpy(str, "aaaaaaaaaaaaa", 13); + delete[] str; + + std::string Str; + raw_string_ostream oss(Str); + s->print(oss); + EXPECT_STREQ("metadata !\"testing 1 2 3\"", oss.str().c_str()); +} + +// Test printing of MDString with non-printable characters. +TEST_F(MDStringTest, PrintingComplex) { + char str[5] = {0, '\n', '"', '\\', (char)-1}; + MDString *s = MDString::get(Context, StringRef(str+0, 5)); + std::string Str; + raw_string_ostream oss(Str); + s->print(oss); + EXPECT_STREQ("metadata !\"\\00\\0A\\22\\5C\\FF\"", oss.str().c_str()); +} + +typedef MetadataTest MDNodeTest; + +// Test the two constructors, and containing other Constants. +TEST_F(MDNodeTest, Simple) { + char x[3] = { 'a', 'b', 'c' }; + char y[3] = { '1', '2', '3' }; + + MDString *s1 = MDString::get(Context, StringRef(&x[0], 3)); + MDString *s2 = MDString::get(Context, StringRef(&y[0], 3)); + ConstantInt *CI = ConstantInt::get(getGlobalContext(), APInt(8, 0)); + + std::vector V; + V.push_back(s1); + V.push_back(CI); + V.push_back(s2); + + MDNode *n1 = MDNode::get(Context, V); + Value *const c1 = n1; + MDNode *n2 = MDNode::get(Context, c1); + Value *const c2 = n2; + MDNode *n3 = MDNode::get(Context, V); + MDNode *n4 = MDNode::getIfExists(Context, V); + MDNode *n5 = MDNode::getIfExists(Context, c1); + MDNode *n6 = MDNode::getIfExists(Context, c2); + EXPECT_NE(n1, n2); +#ifdef ENABLE_MDNODE_UNIQUING + EXPECT_EQ(n1, n3); +#else + (void) n3; +#endif + EXPECT_EQ(n4, n1); + EXPECT_EQ(n5, n2); + EXPECT_EQ(n6, (Value*)0); + + EXPECT_EQ(3u, n1->getNumOperands()); + EXPECT_EQ(s1, n1->getOperand(0)); + EXPECT_EQ(CI, n1->getOperand(1)); + EXPECT_EQ(s2, n1->getOperand(2)); + + EXPECT_EQ(1u, n2->getNumOperands()); + EXPECT_EQ(n1, n2->getOperand(0)); +} + +TEST_F(MDNodeTest, Delete) { + Constant *C = ConstantInt::get(Type::getInt32Ty(getGlobalContext()), 1); + Instruction *I = new BitCastInst(C, Type::getInt32Ty(getGlobalContext())); + + Value *const V = I; + MDNode *n = MDNode::get(Context, V); + WeakVH wvh = n; + + EXPECT_EQ(n, wvh); + + delete I; +} + +TEST(NamedMDNodeTest, Search) { + LLVMContext Context; + Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 1); + Constant *C2 = ConstantInt::get(Type::getInt32Ty(Context), 2); + + Value *const V = C; + Value *const V2 = C2; + MDNode *n = MDNode::get(Context, V); + MDNode *n2 = MDNode::get(Context, V2); + + Module M("MyModule", Context); + const char *Name = "llvm.NMD1"; + NamedMDNode *NMD = M.getOrInsertNamedMetadata(Name); + NMD->addOperand(n); + NMD->addOperand(n2); + + std::string Str; + raw_string_ostream oss(Str); + NMD->print(oss); + EXPECT_STREQ("!llvm.NMD1 = !{!0, !1}\n", + oss.str().c_str()); +} +} diff --git a/unittests/IR/PassManagerTest.cpp b/unittests/IR/PassManagerTest.cpp new file mode 100644 index 0000000000..1097da61b9 --- /dev/null +++ b/unittests/IR/PassManagerTest.cpp @@ -0,0 +1,552 @@ +//===- llvm/unittest/IR/PassManager.cpp - PassManager 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/PassManager.h" +#include "llvm/ADT/SmallVector.h" +#include "llvm/Analysis/CallGraphSCCPass.h" +#include "llvm/Analysis/LoopInfo.h" +#include "llvm/Analysis/LoopPass.h" +#include "llvm/Analysis/Verifier.h" +#include "llvm/Assembly/PrintModulePass.h" +#include "llvm/IR/BasicBlock.h" +#include "llvm/IR/CallingConv.h" +#include "llvm/IR/Constants.h" +#include "llvm/IR/DataLayout.h" +#include "llvm/IR/DerivedTypes.h" +#include "llvm/IR/Function.h" +#include "llvm/IR/GlobalVariable.h" +#include "llvm/IR/InlineAsm.h" +#include "llvm/IR/Instructions.h" +#include "llvm/IR/LLVMContext.h" +#include "llvm/IR/Module.h" +#include "llvm/Pass.h" +#include "llvm/Support/MathExtras.h" +#include "llvm/Support/raw_ostream.h" +#include "gtest/gtest.h" + +using namespace llvm; + +namespace llvm { + void initializeModuleNDMPass(PassRegistry&); + void initializeFPassPass(PassRegistry&); + void initializeCGPassPass(PassRegistry&); + void initializeLPassPass(PassRegistry&); + void initializeBPassPass(PassRegistry&); + + namespace { + // ND = no deps + // NM = no modifications + struct ModuleNDNM: public ModulePass { + public: + static char run; + static char ID; + ModuleNDNM() : ModulePass(ID) { } + virtual bool runOnModule(Module &M) { + run++; + return false; + } + virtual void getAnalysisUsage(AnalysisUsage &AU) const { + AU.setPreservesAll(); + } + }; + char ModuleNDNM::ID=0; + char ModuleNDNM::run=0; + + struct ModuleNDM : public ModulePass { + public: + static char run; + static char ID; + ModuleNDM() : ModulePass(ID) {} + virtual bool runOnModule(Module &M) { + run++; + return true; + } + }; + char ModuleNDM::ID=0; + char ModuleNDM::run=0; + + struct ModuleNDM2 : public ModulePass { + public: + static char run; + static char ID; + ModuleNDM2() : ModulePass(ID) {} + virtual bool runOnModule(Module &M) { + run++; + return true; + } + }; + char ModuleNDM2::ID=0; + char ModuleNDM2::run=0; + + struct ModuleDNM : public ModulePass { + public: + static char run; + static char ID; + ModuleDNM() : ModulePass(ID) { + initializeModuleNDMPass(*PassRegistry::getPassRegistry()); + } + virtual bool runOnModule(Module &M) { + EXPECT_TRUE(getAnalysisIfAvailable()); + run++; + return false; + } + virtual void getAnalysisUsage(AnalysisUsage &AU) const { + AU.addRequired(); + AU.setPreservesAll(); + } + }; + char ModuleDNM::ID=0; + char ModuleDNM::run=0; + + template + struct PassTestBase : public P { + protected: + static int runc; + static bool initialized; + static bool finalized; + int allocated; + void run() { + EXPECT_TRUE(initialized); + EXPECT_FALSE(finalized); + EXPECT_EQ(0, allocated); + allocated++; + runc++; + } + public: + static char ID; + static void finishedOK(int run) { + EXPECT_GT(runc, 0); + EXPECT_TRUE(initialized); + EXPECT_TRUE(finalized); + EXPECT_EQ(run, runc); + } + PassTestBase() : P(ID), allocated(0) { + initialized = false; + finalized = false; + runc = 0; + } + + virtual void releaseMemory() { + EXPECT_GT(runc, 0); + EXPECT_GT(allocated, 0); + allocated--; + } + }; + template char PassTestBase

::ID; + template int PassTestBase

::runc; + template bool PassTestBase

::initialized; + template bool PassTestBase

::finalized; + + template + struct PassTest : public PassTestBase

{ + public: +#ifndef _MSC_VER // MSVC complains that Pass is not base class. + using llvm::Pass::doInitialization; + using llvm::Pass::doFinalization; +#endif + virtual bool doInitialization(T &t) { + EXPECT_FALSE(PassTestBase

::initialized); + PassTestBase

::initialized = true; + return false; + } + virtual bool doFinalization(T &t) { + EXPECT_FALSE(PassTestBase

::finalized); + PassTestBase

::finalized = true; + EXPECT_EQ(0, PassTestBase

::allocated); + return false; + } + }; + + struct CGPass : public PassTest { + public: + CGPass() { + initializeCGPassPass(*PassRegistry::getPassRegistry()); + } + virtual bool runOnSCC(CallGraphSCC &SCMM) { + EXPECT_TRUE(getAnalysisIfAvailable()); + run(); + return false; + } + }; + + struct FPass : public PassTest { + public: + virtual bool runOnFunction(Function &F) { + // FIXME: PR4112 + // EXPECT_TRUE(getAnalysisIfAvailable()); + run(); + return false; + } + }; + + struct LPass : public PassTestBase { + private: + static int initcount; + static int fincount; + public: + LPass() { + initializeLPassPass(*PassRegistry::getPassRegistry()); + initcount = 0; fincount=0; + EXPECT_FALSE(initialized); + } + static void finishedOK(int run, int finalized) { + PassTestBase::finishedOK(run); + EXPECT_EQ(run, initcount); + EXPECT_EQ(finalized, fincount); + } + using llvm::Pass::doInitialization; + using llvm::Pass::doFinalization; + virtual bool doInitialization(Loop* L, LPPassManager &LPM) { + initialized = true; + initcount++; + return false; + } + virtual bool runOnLoop(Loop *L, LPPassManager &LPM) { + EXPECT_TRUE(getAnalysisIfAvailable()); + run(); + return false; + } + virtual bool doFinalization() { + fincount++; + finalized = true; + return false; + } + }; + int LPass::initcount=0; + int LPass::fincount=0; + + struct BPass : public PassTestBase { + private: + static int inited; + static int fin; + public: + static void finishedOK(int run, int N) { + PassTestBase::finishedOK(run); + EXPECT_EQ(inited, N); + EXPECT_EQ(fin, N); + } + BPass() { + inited = 0; + fin = 0; + } + virtual bool doInitialization(Module &M) { + EXPECT_FALSE(initialized); + initialized = true; + return false; + } + virtual bool doInitialization(Function &F) { + inited++; + return false; + } + virtual bool runOnBasicBlock(BasicBlock &BB) { + EXPECT_TRUE(getAnalysisIfAvailable()); + run(); + return false; + } + virtual bool doFinalization(Function &F) { + fin++; + return false; + } + virtual bool doFinalization(Module &M) { + EXPECT_FALSE(finalized); + finalized = true; + EXPECT_EQ(0, allocated); + return false; + } + }; + int BPass::inited=0; + int BPass::fin=0; + + struct OnTheFlyTest: public ModulePass { + public: + static char ID; + OnTheFlyTest() : ModulePass(ID) { + initializeFPassPass(*PassRegistry::getPassRegistry()); + } + virtual bool runOnModule(Module &M) { + EXPECT_TRUE(getAnalysisIfAvailable()); + for (Module::iterator I=M.begin(),E=M.end(); I != E; ++I) { + Function &F = *I; + { + SCOPED_TRACE("Running on the fly function pass"); + getAnalysis(F); + } + } + return false; + } + virtual void getAnalysisUsage(AnalysisUsage &AU) const { + AU.addRequired(); + } + }; + char OnTheFlyTest::ID=0; + + TEST(PassManager, RunOnce) { + Module M("test-once", getGlobalContext()); + struct ModuleNDNM *mNDNM = new ModuleNDNM(); + struct ModuleDNM *mDNM = new ModuleDNM(); + struct ModuleNDM *mNDM = new ModuleNDM(); + struct ModuleNDM2 *mNDM2 = new ModuleNDM2(); + + mNDM->run = mNDNM->run = mDNM->run = mNDM2->run = 0; + + PassManager Passes; + Passes.add(new DataLayout(&M)); + Passes.add(mNDM2); + Passes.add(mNDM); + Passes.add(mNDNM); + Passes.add(mDNM); + + Passes.run(M); + // each pass must be run exactly once, since nothing invalidates them + EXPECT_EQ(1, mNDM->run); + EXPECT_EQ(1, mNDNM->run); + EXPECT_EQ(1, mDNM->run); + EXPECT_EQ(1, mNDM2->run); + } + + TEST(PassManager, ReRun) { + Module M("test-rerun", getGlobalContext()); + struct ModuleNDNM *mNDNM = new ModuleNDNM(); + struct ModuleDNM *mDNM = new ModuleDNM(); + struct ModuleNDM *mNDM = new ModuleNDM(); + struct ModuleNDM2 *mNDM2 = new ModuleNDM2(); + + mNDM->run = mNDNM->run = mDNM->run = mNDM2->run = 0; + + PassManager Passes; + Passes.add(new DataLayout(&M)); + Passes.add(mNDM); + Passes.add(mNDNM); + Passes.add(mNDM2);// invalidates mNDM needed by mDNM + Passes.add(mDNM); + + Passes.run(M); + // Some passes must be rerun because a pass that modified the + // module/function was run in between + EXPECT_EQ(2, mNDM->run); + EXPECT_EQ(1, mNDNM->run); + EXPECT_EQ(1, mNDM2->run); + EXPECT_EQ(1, mDNM->run); + } + + Module* makeLLVMModule(); + + template + void MemoryTestHelper(int run) { + OwningPtr M(makeLLVMModule()); + T *P = new T(); + PassManager Passes; + Passes.add(new DataLayout(M.get())); + Passes.add(P); + Passes.run(*M); + T::finishedOK(run); + } + + template + void MemoryTestHelper(int run, int N) { + Module *M = makeLLVMModule(); + T *P = new T(); + PassManager Passes; + Passes.add(new DataLayout(M)); + Passes.add(P); + Passes.run(*M); + T::finishedOK(run, N); + delete M; + } + + TEST(PassManager, Memory) { + // SCC#1: test1->test2->test3->test1 + // SCC#2: test4 + // SCC#3: indirect call node + { + SCOPED_TRACE("Callgraph pass"); + MemoryTestHelper(3); + } + + { + SCOPED_TRACE("Function pass"); + MemoryTestHelper(4);// 4 functions + } + + { + SCOPED_TRACE("Loop pass"); + MemoryTestHelper(2, 1); //2 loops, 1 function + } + { + SCOPED_TRACE("Basic block pass"); + MemoryTestHelper(7, 4); //9 basic blocks + } + + } + + TEST(PassManager, MemoryOnTheFly) { + Module *M = makeLLVMModule(); + { + SCOPED_TRACE("Running OnTheFlyTest"); + struct OnTheFlyTest *O = new OnTheFlyTest(); + PassManager Passes; + Passes.add(new DataLayout(M)); + Passes.add(O); + Passes.run(*M); + + FPass::finishedOK(4); + } + delete M; + } + + Module* makeLLVMModule() { + // Module Construction + Module* mod = new Module("test-mem", getGlobalContext()); + mod->setDataLayout("e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-" + "i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-" + "a0:0:64-s0:64:64-f80:128:128"); + mod->setTargetTriple("x86_64-unknown-linux-gnu"); + + // Type Definitions + std::vectorFuncTy_0_args; + FunctionType* FuncTy_0 = FunctionType::get( + /*Result=*/IntegerType::get(getGlobalContext(), 32), + /*Params=*/FuncTy_0_args, + /*isVarArg=*/false); + + std::vectorFuncTy_2_args; + FuncTy_2_args.push_back(IntegerType::get(getGlobalContext(), 1)); + FunctionType* FuncTy_2 = FunctionType::get( + /*Result=*/Type::getVoidTy(getGlobalContext()), + /*Params=*/FuncTy_2_args, + /*isVarArg=*/false); + + + // Function Declarations + + Function* func_test1 = Function::Create( + /*Type=*/FuncTy_0, + /*Linkage=*/GlobalValue::ExternalLinkage, + /*Name=*/"test1", mod); + func_test1->setCallingConv(CallingConv::C); + AttributeSet func_test1_PAL; + func_test1->setAttributes(func_test1_PAL); + + Function* func_test2 = Function::Create( + /*Type=*/FuncTy_0, + /*Linkage=*/GlobalValue::ExternalLinkage, + /*Name=*/"test2", mod); + func_test2->setCallingConv(CallingConv::C); + AttributeSet func_test2_PAL; + func_test2->setAttributes(func_test2_PAL); + + Function* func_test3 = Function::Create( + /*Type=*/FuncTy_0, + /*Linkage=*/GlobalValue::ExternalLinkage, + /*Name=*/"test3", mod); + func_test3->setCallingConv(CallingConv::C); + AttributeSet func_test3_PAL; + func_test3->setAttributes(func_test3_PAL); + + Function* func_test4 = Function::Create( + /*Type=*/FuncTy_2, + /*Linkage=*/GlobalValue::ExternalLinkage, + /*Name=*/"test4", mod); + func_test4->setCallingConv(CallingConv::C); + AttributeSet func_test4_PAL; + func_test4->setAttributes(func_test4_PAL); + + // Global Variable Declarations + + + // Constant Definitions + + // Global Variable Definitions + + // Function Definitions + + // Function: test1 (func_test1) + { + + BasicBlock* label_entry = BasicBlock::Create(getGlobalContext(), "entry",func_test1,0); + + // Block entry (label_entry) + CallInst* int32_3 = CallInst::Create(func_test2, "", label_entry); + int32_3->setCallingConv(CallingConv::C); + int32_3->setTailCall(false);AttributeSet int32_3_PAL; + int32_3->setAttributes(int32_3_PAL); + + ReturnInst::Create(getGlobalContext(), int32_3, label_entry); + + } + + // Function: test2 (func_test2) + { + + BasicBlock* label_entry_5 = BasicBlock::Create(getGlobalContext(), "entry",func_test2,0); + + // Block entry (label_entry_5) + CallInst* int32_6 = CallInst::Create(func_test3, "", label_entry_5); + int32_6->setCallingConv(CallingConv::C); + int32_6->setTailCall(false);AttributeSet int32_6_PAL; + int32_6->setAttributes(int32_6_PAL); + + ReturnInst::Create(getGlobalContext(), int32_6, label_entry_5); + + } + + // Function: test3 (func_test3) + { + + BasicBlock* label_entry_8 = BasicBlock::Create(getGlobalContext(), "entry",func_test3,0); + + // Block entry (label_entry_8) + CallInst* int32_9 = CallInst::Create(func_test1, "", label_entry_8); + int32_9->setCallingConv(CallingConv::C); + int32_9->setTailCall(false);AttributeSet int32_9_PAL; + int32_9->setAttributes(int32_9_PAL); + + ReturnInst::Create(getGlobalContext(), int32_9, label_entry_8); + + } + + // Function: test4 (func_test4) + { + Function::arg_iterator args = func_test4->arg_begin(); + Value* int1_f = args++; + int1_f->setName("f"); + + BasicBlock* label_entry_11 = BasicBlock::Create(getGlobalContext(), "entry",func_test4,0); + BasicBlock* label_bb = BasicBlock::Create(getGlobalContext(), "bb",func_test4,0); + BasicBlock* label_bb1 = BasicBlock::Create(getGlobalContext(), "bb1",func_test4,0); + BasicBlock* label_return = BasicBlock::Create(getGlobalContext(), "return",func_test4,0); + + // Block entry (label_entry_11) + BranchInst::Create(label_bb, label_entry_11); + + // Block bb (label_bb) + BranchInst::Create(label_bb, label_bb1, int1_f, label_bb); + + // Block bb1 (label_bb1) + BranchInst::Create(label_bb1, label_return, int1_f, label_bb1); + + // Block return (label_return) + ReturnInst::Create(getGlobalContext(), label_return); + + } + return mod; + } + + } +} + +INITIALIZE_PASS(ModuleNDM, "mndm", "mndm", false, false) +INITIALIZE_PASS_BEGIN(CGPass, "cgp","cgp", false, false) +INITIALIZE_AG_DEPENDENCY(CallGraph) +INITIALIZE_PASS_END(CGPass, "cgp","cgp", false, false) +INITIALIZE_PASS(FPass, "fp","fp", false, false) +INITIALIZE_PASS_BEGIN(LPass, "lp","lp", false, false) +INITIALIZE_PASS_DEPENDENCY(LoopInfo) +INITIALIZE_PASS_END(LPass, "lp","lp", false, false) +INITIALIZE_PASS(BPass, "bp","bp", false, false) diff --git a/unittests/IR/TypeBuilderTest.cpp b/unittests/IR/TypeBuilderTest.cpp new file mode 100644 index 0000000000..be493cdc63 --- /dev/null +++ b/unittests/IR/TypeBuilderTest.cpp @@ -0,0 +1,253 @@ +//===- llvm/unittest/TypeBuilderTest.cpp - TypeBuilder tests --------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "llvm/IR/TypeBuilder.h" +#include "llvm/ADT/ArrayRef.h" +#include "llvm/IR/LLVMContext.h" +#include "gtest/gtest.h" + +using namespace llvm; + +namespace { + +TEST(TypeBuilderTest, Void) { + EXPECT_EQ(Type::getVoidTy(getGlobalContext()), (TypeBuilder::get(getGlobalContext()))); + EXPECT_EQ(Type::getVoidTy(getGlobalContext()), (TypeBuilder::get(getGlobalContext()))); + // Special cases for C compatibility: + EXPECT_EQ(Type::getInt8PtrTy(getGlobalContext()), + (TypeBuilder::get(getGlobalContext()))); + EXPECT_EQ(Type::getInt8PtrTy(getGlobalContext()), + (TypeBuilder::get(getGlobalContext()))); + EXPECT_EQ(Type::getInt8PtrTy(getGlobalContext()), + (TypeBuilder::get(getGlobalContext()))); + EXPECT_EQ(Type::getInt8PtrTy(getGlobalContext()), + (TypeBuilder::get( + getGlobalContext()))); +} + +TEST(TypeBuilderTest, HostIntegers) { + EXPECT_EQ(Type::getInt8Ty(getGlobalContext()), (TypeBuilder::get(getGlobalContext()))); + EXPECT_EQ(Type::getInt8Ty(getGlobalContext()), (TypeBuilder::get(getGlobalContext()))); + EXPECT_EQ(Type::getInt16Ty(getGlobalContext()), (TypeBuilder::get(getGlobalContext()))); + EXPECT_EQ(Type::getInt16Ty(getGlobalContext()), (TypeBuilder::get(getGlobalContext()))); + EXPECT_EQ(Type::getInt32Ty(getGlobalContext()), (TypeBuilder::get(getGlobalContext()))); + EXPECT_EQ(Type::getInt32Ty(getGlobalContext()), (TypeBuilder::get(getGlobalContext()))); + EXPECT_EQ(Type::getInt64Ty(getGlobalContext()), (TypeBuilder::get(getGlobalContext()))); + EXPECT_EQ(Type::getInt64Ty(getGlobalContext()), (TypeBuilder::get(getGlobalContext()))); + + EXPECT_EQ(IntegerType::get(getGlobalContext(), sizeof(size_t) * CHAR_BIT), + (TypeBuilder::get(getGlobalContext()))); + EXPECT_EQ(IntegerType::get(getGlobalContext(), sizeof(ptrdiff_t) * CHAR_BIT), + (TypeBuilder::get(getGlobalContext()))); +} + +TEST(TypeBuilderTest, CrossCompilableIntegers) { + EXPECT_EQ(IntegerType::get(getGlobalContext(), 1), (TypeBuilder, true>::get(getGlobalContext()))); + EXPECT_EQ(IntegerType::get(getGlobalContext(), 1), (TypeBuilder, false>::get(getGlobalContext()))); + EXPECT_EQ(IntegerType::get(getGlobalContext(), 72), (TypeBuilder, true>::get(getGlobalContext()))); + EXPECT_EQ(IntegerType::get(getGlobalContext(), 72), (TypeBuilder, false>::get(getGlobalContext()))); +} + +TEST(TypeBuilderTest, Float) { + EXPECT_EQ(Type::getFloatTy(getGlobalContext()), (TypeBuilder::get(getGlobalContext()))); + EXPECT_EQ(Type::getDoubleTy(getGlobalContext()), (TypeBuilder::get(getGlobalContext()))); + // long double isn't supported yet. + EXPECT_EQ(Type::getFloatTy(getGlobalContext()), (TypeBuilder::get(getGlobalContext()))); + EXPECT_EQ(Type::getFloatTy(getGlobalContext()), (TypeBuilder::get(getGlobalContext()))); + EXPECT_EQ(Type::getDoubleTy(getGlobalContext()), (TypeBuilder::get(getGlobalContext()))); + EXPECT_EQ(Type::getDoubleTy(getGlobalContext()), (TypeBuilder::get(getGlobalContext()))); + EXPECT_EQ(Type::getX86_FP80Ty(getGlobalContext()), (TypeBuilder::get(getGlobalContext()))); + EXPECT_EQ(Type::getX86_FP80Ty(getGlobalContext()), (TypeBuilder::get(getGlobalContext()))); + EXPECT_EQ(Type::getFP128Ty(getGlobalContext()), (TypeBuilder::get(getGlobalContext()))); + EXPECT_EQ(Type::getFP128Ty(getGlobalContext()), (TypeBuilder::get(getGlobalContext()))); + EXPECT_EQ(Type::getPPC_FP128Ty(getGlobalContext()), (TypeBuilder::get(getGlobalContext()))); + EXPECT_EQ(Type::getPPC_FP128Ty(getGlobalContext()), (TypeBuilder::get(getGlobalContext()))); +} + +TEST(TypeBuilderTest, Derived) { + EXPECT_EQ(PointerType::getUnqual(Type::getInt8PtrTy(getGlobalContext())), + (TypeBuilder::get(getGlobalContext()))); + EXPECT_EQ(ArrayType::get(Type::getInt8Ty(getGlobalContext()), 7), + (TypeBuilder::get(getGlobalContext()))); + EXPECT_EQ(ArrayType::get(Type::getInt8Ty(getGlobalContext()), 0), + (TypeBuilder::get(getGlobalContext()))); + + EXPECT_EQ(PointerType::getUnqual(Type::getInt8PtrTy(getGlobalContext())), + (TypeBuilder**, false>::get(getGlobalContext()))); + EXPECT_EQ(ArrayType::get(Type::getInt8Ty(getGlobalContext()), 7), + (TypeBuilder[7], false>::get(getGlobalContext()))); + EXPECT_EQ(ArrayType::get(Type::getInt8Ty(getGlobalContext()), 0), + (TypeBuilder[], false>::get(getGlobalContext()))); + + EXPECT_EQ(PointerType::getUnqual(Type::getInt8PtrTy(getGlobalContext())), + (TypeBuilder**, true>::get(getGlobalContext()))); + EXPECT_EQ(ArrayType::get(Type::getInt8Ty(getGlobalContext()), 7), + (TypeBuilder[7], true>::get(getGlobalContext()))); + EXPECT_EQ(ArrayType::get(Type::getInt8Ty(getGlobalContext()), 0), + (TypeBuilder[], true>::get(getGlobalContext()))); + + + EXPECT_EQ(Type::getInt8Ty(getGlobalContext()), + (TypeBuilder::get(getGlobalContext()))); + EXPECT_EQ(Type::getInt8Ty(getGlobalContext()), + (TypeBuilder::get(getGlobalContext()))); + EXPECT_EQ(Type::getInt8Ty(getGlobalContext()), + (TypeBuilder::get(getGlobalContext()))); + + EXPECT_EQ(Type::getInt8Ty(getGlobalContext()), + (TypeBuilder, false>::get(getGlobalContext()))); + EXPECT_EQ(Type::getInt8Ty(getGlobalContext()), + (TypeBuilder, false>::get(getGlobalContext()))); + EXPECT_EQ(Type::getInt8Ty(getGlobalContext()), + (TypeBuilder, false>::get(getGlobalContext()))); + + EXPECT_EQ(Type::getInt8Ty(getGlobalContext()), + (TypeBuilder, true>::get(getGlobalContext()))); + EXPECT_EQ(Type::getInt8Ty(getGlobalContext()), + (TypeBuilder, true>::get(getGlobalContext()))); + EXPECT_EQ(Type::getInt8Ty(getGlobalContext()), + (TypeBuilder, true>::get(getGlobalContext()))); + + EXPECT_EQ(Type::getInt8PtrTy(getGlobalContext()), + (TypeBuilder::get(getGlobalContext()))); +} + +TEST(TypeBuilderTest, Functions) { + std::vector params; + EXPECT_EQ(FunctionType::get(Type::getVoidTy(getGlobalContext()), params, false), + (TypeBuilder::get(getGlobalContext()))); + EXPECT_EQ(FunctionType::get(Type::getInt8Ty(getGlobalContext()), params, true), + (TypeBuilder::get(getGlobalContext()))); + params.push_back(TypeBuilder::get(getGlobalContext())); + EXPECT_EQ(FunctionType::get(Type::getInt8Ty(getGlobalContext()), params, false), + (TypeBuilder::get(getGlobalContext()))); + EXPECT_EQ(FunctionType::get(Type::getInt8Ty(getGlobalContext()), params, true), + (TypeBuilder::get(getGlobalContext()))); + params.push_back(TypeBuilder::get(getGlobalContext())); + EXPECT_EQ(FunctionType::get(Type::getInt8Ty(getGlobalContext()), params, false), + (TypeBuilder::get(getGlobalContext()))); + EXPECT_EQ(FunctionType::get(Type::getInt8Ty(getGlobalContext()), params, true), + (TypeBuilder::get(getGlobalContext()))); + params.push_back(TypeBuilder::get(getGlobalContext())); + EXPECT_EQ(FunctionType::get(Type::getInt8Ty(getGlobalContext()), params, false), + (TypeBuilder::get(getGlobalContext()))); + EXPECT_EQ(FunctionType::get(Type::getInt8Ty(getGlobalContext()), params, true), + (TypeBuilder::get(getGlobalContext()))); + params.push_back(TypeBuilder::get(getGlobalContext())); + EXPECT_EQ(FunctionType::get(Type::getInt8Ty(getGlobalContext()), params, false), + (TypeBuilder::get(getGlobalContext()))); + EXPECT_EQ(FunctionType::get(Type::getInt8Ty(getGlobalContext()), params, true), + (TypeBuilder::get(getGlobalContext()))); + params.push_back(TypeBuilder::get(getGlobalContext())); + EXPECT_EQ(FunctionType::get(Type::getInt8Ty(getGlobalContext()), params, false), + (TypeBuilder::get(getGlobalContext()))); + EXPECT_EQ(FunctionType::get(Type::getInt8Ty(getGlobalContext()), params, true), + (TypeBuilder::get(getGlobalContext()))); +} + +TEST(TypeBuilderTest, Context) { + // We used to cache TypeBuilder results in static local variables. This + // produced the same type for different contexts, which of course broke + // things. + LLVMContext context1; + EXPECT_EQ(&context1, + &(TypeBuilder, true>::get(context1))->getContext()); + LLVMContext context2; + EXPECT_EQ(&context2, + &(TypeBuilder, true>::get(context2))->getContext()); +} + +struct MyType { + int a; + int *b; + void *array[1]; +}; + +struct MyPortableType { + int32_t a; + int32_t *b; + void *array[1]; +}; + +} // anonymous namespace + +namespace llvm { +template class TypeBuilder { +public: + static StructType *get(LLVMContext &Context) { + // Using the static result variable ensures that the type is + // only looked up once. + std::vector st; + st.push_back(TypeBuilder::get(Context)); + st.push_back(TypeBuilder::get(Context)); + st.push_back(TypeBuilder::get(Context)); + static StructType *const result = StructType::get(Context, st); + return result; + } + + // You may find this a convenient place to put some constants + // to help with getelementptr. They don't have any effect on + // the operation of TypeBuilder. + enum Fields { + FIELD_A, + FIELD_B, + FIELD_ARRAY + }; +}; + +template class TypeBuilder { +public: + static StructType *get(LLVMContext &Context) { + // Using the static result variable ensures that the type is + // only looked up once. + std::vector st; + st.push_back(TypeBuilder, cross>::get(Context)); + st.push_back(TypeBuilder*, cross>::get(Context)); + st.push_back(TypeBuilder*[], cross>::get(Context)); + static StructType *const result = StructType::get(Context, st); + return result; + } + + // You may find this a convenient place to put some constants + // to help with getelementptr. They don't have any effect on + // the operation of TypeBuilder. + enum Fields { + FIELD_A, + FIELD_B, + FIELD_ARRAY + }; +}; +} // namespace llvm +namespace { + +TEST(TypeBuilderTest, Extensions) { + EXPECT_EQ(PointerType::getUnqual(StructType::get( + TypeBuilder::get(getGlobalContext()), + TypeBuilder::get(getGlobalContext()), + TypeBuilder::get(getGlobalContext()), + (void*)0)), + (TypeBuilder::get(getGlobalContext()))); + EXPECT_EQ(PointerType::getUnqual(StructType::get( + TypeBuilder, false>::get(getGlobalContext()), + TypeBuilder*, false>::get(getGlobalContext()), + TypeBuilder*[], false>::get(getGlobalContext()), + (void*)0)), + (TypeBuilder::get(getGlobalContext()))); + EXPECT_EQ(PointerType::getUnqual(StructType::get( + TypeBuilder, false>::get(getGlobalContext()), + TypeBuilder*, false>::get(getGlobalContext()), + TypeBuilder*[], false>::get(getGlobalContext()), + (void*)0)), + (TypeBuilder::get(getGlobalContext()))); +} + +} // anonymous namespace diff --git a/unittests/IR/TypesTest.cpp b/unittests/IR/TypesTest.cpp new file mode 100644 index 0000000000..2cee640a13 --- /dev/null +++ b/unittests/IR/TypesTest.cpp @@ -0,0 +1,30 @@ +//===- llvm/unittest/IR/TypesTest.cpp - Type 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/IR/DerivedTypes.h" +#include "llvm/IR/LLVMContext.h" +#include "gtest/gtest.h" +using namespace llvm; + +namespace { + +TEST(TypesTest, StructType) { + LLVMContext C; + + // PR13522 + StructType *Struct = StructType::create(C, "FooBar"); + EXPECT_EQ("FooBar", Struct->getName()); + Struct->setName(Struct->getName().substr(0, 3)); + EXPECT_EQ("Foo", Struct->getName()); + Struct->setName(""); + EXPECT_TRUE(Struct->getName().empty()); + EXPECT_FALSE(Struct->hasName()); +} + +} // end anonymous namespace diff --git a/unittests/IR/ValueMapTest.cpp b/unittests/IR/ValueMapTest.cpp new file mode 100644 index 0000000000..5aaf905836 --- /dev/null +++ b/unittests/IR/ValueMapTest.cpp @@ -0,0 +1,294 @@ +//===- llvm/unittest/ADT/ValueMapTest.cpp - ValueMap unit tests -*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "llvm/ADT/ValueMap.h" +#include "llvm/ADT/OwningPtr.h" +#include "llvm/Config/llvm-config.h" +#include "llvm/IR/Constants.h" +#include "llvm/IR/Instructions.h" +#include "llvm/IR/LLVMContext.h" +#include "gtest/gtest.h" + +using namespace llvm; + +namespace { + +// Test fixture +template +class ValueMapTest : public testing::Test { +protected: + Constant *ConstantV; + OwningPtr BitcastV; + OwningPtr AddV; + + ValueMapTest() : + ConstantV(ConstantInt::get(Type::getInt32Ty(getGlobalContext()), 0)), + BitcastV(new BitCastInst(ConstantV, Type::getInt32Ty(getGlobalContext()))), + AddV(BinaryOperator::CreateAdd(ConstantV, ConstantV)) { + } +}; + +// Run everything on Value*, a subtype to make sure that casting works as +// expected, and a const subtype to make sure we cast const correctly. +typedef ::testing::Types KeyTypes; +TYPED_TEST_CASE(ValueMapTest, KeyTypes); + +TYPED_TEST(ValueMapTest, Null) { + ValueMap VM1; + VM1[NULL] = 7; + EXPECT_EQ(7, VM1.lookup(NULL)); +} + +TYPED_TEST(ValueMapTest, FollowsValue) { + ValueMap VM; + VM[this->BitcastV.get()] = 7; + EXPECT_EQ(7, VM.lookup(this->BitcastV.get())); + EXPECT_EQ(0, VM.count(this->AddV.get())); + this->BitcastV->replaceAllUsesWith(this->AddV.get()); + EXPECT_EQ(7, VM.lookup(this->AddV.get())); + EXPECT_EQ(0, VM.count(this->BitcastV.get())); + this->AddV.reset(); + EXPECT_EQ(0, VM.count(this->AddV.get())); + EXPECT_EQ(0, VM.count(this->BitcastV.get())); + EXPECT_EQ(0U, VM.size()); +} + +TYPED_TEST(ValueMapTest, OperationsWork) { + ValueMap VM; + ValueMap VM2(16); (void)VM2; + typename ValueMapConfig::ExtraData Data; + ValueMap VM3(Data, 16); (void)VM3; + EXPECT_TRUE(VM.empty()); + + VM[this->BitcastV.get()] = 7; + + // Find: + typename ValueMap::iterator I = + VM.find(this->BitcastV.get()); + ASSERT_TRUE(I != VM.end()); + EXPECT_EQ(this->BitcastV.get(), I->first); + EXPECT_EQ(7, I->second); + EXPECT_TRUE(VM.find(this->AddV.get()) == VM.end()); + + // Const find: + const ValueMap &CVM = VM; + typename ValueMap::const_iterator CI = + CVM.find(this->BitcastV.get()); + ASSERT_TRUE(CI != CVM.end()); + EXPECT_EQ(this->BitcastV.get(), CI->first); + EXPECT_EQ(7, CI->second); + EXPECT_TRUE(CVM.find(this->AddV.get()) == CVM.end()); + + // Insert: + std::pair::iterator, bool> InsertResult1 = + VM.insert(std::make_pair(this->AddV.get(), 3)); + EXPECT_EQ(this->AddV.get(), InsertResult1.first->first); + EXPECT_EQ(3, InsertResult1.first->second); + EXPECT_TRUE(InsertResult1.second); + EXPECT_EQ(true, VM.count(this->AddV.get())); + std::pair::iterator, bool> InsertResult2 = + VM.insert(std::make_pair(this->AddV.get(), 5)); + EXPECT_EQ(this->AddV.get(), InsertResult2.first->first); + EXPECT_EQ(3, InsertResult2.first->second); + EXPECT_FALSE(InsertResult2.second); + + // Erase: + VM.erase(InsertResult2.first); + EXPECT_EQ(0U, VM.count(this->AddV.get())); + EXPECT_EQ(1U, VM.count(this->BitcastV.get())); + VM.erase(this->BitcastV.get()); + EXPECT_EQ(0U, VM.count(this->BitcastV.get())); + EXPECT_EQ(0U, VM.size()); + + // Range insert: + SmallVector, 2> Elems; + Elems.push_back(std::make_pair(this->AddV.get(), 1)); + Elems.push_back(std::make_pair(this->BitcastV.get(), 2)); + VM.insert(Elems.begin(), Elems.end()); + EXPECT_EQ(1, VM.lookup(this->AddV.get())); + EXPECT_EQ(2, VM.lookup(this->BitcastV.get())); +} + +template +void CompileAssertHasType(VarType) { + typedef char assert[is_same::value ? 1 : -1]; +} + +TYPED_TEST(ValueMapTest, Iteration) { + ValueMap VM; + VM[this->BitcastV.get()] = 2; + VM[this->AddV.get()] = 3; + size_t size = 0; + for (typename ValueMap::iterator I = VM.begin(), E = VM.end(); + I != E; ++I) { + ++size; + std::pair value = *I; (void)value; + CompileAssertHasType(I->first); + if (I->second == 2) { + EXPECT_EQ(this->BitcastV.get(), I->first); + I->second = 5; + } else if (I->second == 3) { + EXPECT_EQ(this->AddV.get(), I->first); + I->second = 6; + } else { + ADD_FAILURE() << "Iterated through an extra value."; + } + } + EXPECT_EQ(2U, size); + EXPECT_EQ(5, VM[this->BitcastV.get()]); + EXPECT_EQ(6, VM[this->AddV.get()]); + + size = 0; + // Cast to const ValueMap to avoid a bug in DenseMap's iterators. + const ValueMap& CVM = VM; + for (typename ValueMap::const_iterator I = CVM.begin(), + E = CVM.end(); I != E; ++I) { + ++size; + std::pair value = *I; (void)value; + CompileAssertHasType(I->first); + if (I->second == 5) { + EXPECT_EQ(this->BitcastV.get(), I->first); + } else if (I->second == 6) { + EXPECT_EQ(this->AddV.get(), I->first); + } else { + ADD_FAILURE() << "Iterated through an extra value."; + } + } + EXPECT_EQ(2U, size); +} + +TYPED_TEST(ValueMapTest, DefaultCollisionBehavior) { + // By default, we overwrite the old value with the replaced value. + ValueMap VM; + VM[this->BitcastV.get()] = 7; + VM[this->AddV.get()] = 9; + this->BitcastV->replaceAllUsesWith(this->AddV.get()); + EXPECT_EQ(0, VM.count(this->BitcastV.get())); + EXPECT_EQ(9, VM.lookup(this->AddV.get())); +} + +TYPED_TEST(ValueMapTest, ConfiguredCollisionBehavior) { + // TODO: Implement this when someone needs it. +} + +template +struct LockMutex : ValueMapConfig { + struct ExtraData { + sys::Mutex *M; + bool *CalledRAUW; + bool *CalledDeleted; + }; + static void onRAUW(const ExtraData &Data, KeyT Old, KeyT New) { + *Data.CalledRAUW = true; + EXPECT_FALSE(Data.M->tryacquire()) << "Mutex should already be locked."; + } + static void onDelete(const ExtraData &Data, KeyT Old) { + *Data.CalledDeleted = true; + EXPECT_FALSE(Data.M->tryacquire()) << "Mutex should already be locked."; + } + static sys::Mutex *getMutex(const ExtraData &Data) { return Data.M; } +}; +#if LLVM_ENABLE_THREADS +TYPED_TEST(ValueMapTest, LocksMutex) { + sys::Mutex M(false); // Not recursive. + bool CalledRAUW = false, CalledDeleted = false; + typename LockMutex::ExtraData Data = + {&M, &CalledRAUW, &CalledDeleted}; + ValueMap > VM(Data); + VM[this->BitcastV.get()] = 7; + this->BitcastV->replaceAllUsesWith(this->AddV.get()); + this->AddV.reset(); + EXPECT_TRUE(CalledRAUW); + EXPECT_TRUE(CalledDeleted); +} +#endif + +template +struct NoFollow : ValueMapConfig { + enum { FollowRAUW = false }; +}; + +TYPED_TEST(ValueMapTest, NoFollowRAUW) { + ValueMap > VM; + VM[this->BitcastV.get()] = 7; + EXPECT_EQ(7, VM.lookup(this->BitcastV.get())); + EXPECT_EQ(0, VM.count(this->AddV.get())); + this->BitcastV->replaceAllUsesWith(this->AddV.get()); + EXPECT_EQ(7, VM.lookup(this->BitcastV.get())); + EXPECT_EQ(0, VM.lookup(this->AddV.get())); + this->AddV.reset(); + EXPECT_EQ(7, VM.lookup(this->BitcastV.get())); + EXPECT_EQ(0, VM.lookup(this->AddV.get())); + this->BitcastV.reset(); + EXPECT_EQ(0, VM.lookup(this->BitcastV.get())); + EXPECT_EQ(0, VM.lookup(this->AddV.get())); + EXPECT_EQ(0U, VM.size()); +} + +template +struct CountOps : ValueMapConfig { + struct ExtraData { + int *Deletions; + int *RAUWs; + }; + + static void onRAUW(const ExtraData &Data, KeyT Old, KeyT New) { + ++*Data.RAUWs; + } + static void onDelete(const ExtraData &Data, KeyT Old) { + ++*Data.Deletions; + } +}; + +TYPED_TEST(ValueMapTest, CallsConfig) { + int Deletions = 0, RAUWs = 0; + typename CountOps::ExtraData Data = {&Deletions, &RAUWs}; + ValueMap > VM(Data); + VM[this->BitcastV.get()] = 7; + this->BitcastV->replaceAllUsesWith(this->AddV.get()); + EXPECT_EQ(0, Deletions); + EXPECT_EQ(1, RAUWs); + this->AddV.reset(); + EXPECT_EQ(1, Deletions); + EXPECT_EQ(1, RAUWs); + this->BitcastV.reset(); + EXPECT_EQ(1, Deletions); + EXPECT_EQ(1, RAUWs); +} + +template +struct ModifyingConfig : ValueMapConfig { + // We'll put a pointer here back to the ValueMap this key is in, so + // that we can modify it (and clobber *this) before the ValueMap + // tries to do the same modification. In previous versions of + // ValueMap, that exploded. + typedef ValueMap > **ExtraData; + + static void onRAUW(ExtraData Map, KeyT Old, KeyT New) { + (*Map)->erase(Old); + } + static void onDelete(ExtraData Map, KeyT Old) { + (*Map)->erase(Old); + } +}; +TYPED_TEST(ValueMapTest, SurvivesModificationByConfig) { + ValueMap > *MapAddress; + ValueMap > VM(&MapAddress); + MapAddress = &VM; + // Now the ModifyingConfig can modify the Map inside a callback. + VM[this->BitcastV.get()] = 7; + this->BitcastV->replaceAllUsesWith(this->AddV.get()); + EXPECT_FALSE(VM.count(this->BitcastV.get())); + EXPECT_FALSE(VM.count(this->AddV.get())); + VM[this->AddV.get()] = 7; + this->AddV.reset(); + EXPECT_FALSE(VM.count(this->AddV.get())); +} + +} diff --git a/unittests/IR/VerifierTest.cpp b/unittests/IR/VerifierTest.cpp new file mode 100644 index 0000000000..89119368fb --- /dev/null +++ b/unittests/IR/VerifierTest.cpp @@ -0,0 +1,64 @@ +//===- llvm/unittest/IR/VerifierTest.cpp - Verifier 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/Verifier.h" +#include "llvm/ADT/OwningPtr.h" +#include "llvm/IR/Constants.h" +#include "llvm/IR/DerivedTypes.h" +#include "llvm/IR/Function.h" +#include "llvm/IR/GlobalAlias.h" +#include "llvm/IR/GlobalVariable.h" +#include "llvm/IR/Instructions.h" +#include "llvm/IR/LLVMContext.h" +#include "llvm/IR/Module.h" +#include "gtest/gtest.h" + +namespace llvm { +namespace { + +TEST(VerifierTest, Branch_i1) { + LLVMContext &C = getGlobalContext(); + FunctionType *FTy = FunctionType::get(Type::getVoidTy(C), /*isVarArg=*/false); + OwningPtr F(Function::Create(FTy, GlobalValue::ExternalLinkage)); + BasicBlock *Entry = BasicBlock::Create(C, "entry", F.get()); + BasicBlock *Exit = BasicBlock::Create(C, "exit", F.get()); + ReturnInst::Create(C, Exit); + + // To avoid triggering an assertion in BranchInst::Create, we first create + // a branch with an 'i1' condition ... + + Constant *False = ConstantInt::getFalse(C); + BranchInst *BI = BranchInst::Create(Exit, Exit, False, Entry); + + // ... then use setOperand to redirect it to a value of different type. + + Constant *Zero32 = ConstantInt::get(IntegerType::get(C, 32), 0); + BI->setOperand(0, Zero32); + + EXPECT_TRUE(verifyFunction(*F, ReturnStatusAction)); +} + +TEST(VerifierTest, AliasUnnamedAddr) { + LLVMContext &C = getGlobalContext(); + Module M("M", C); + Type *Ty = Type::getInt8Ty(C); + Constant *Init = Constant::getNullValue(Ty); + GlobalVariable *Aliasee = new GlobalVariable(M, Ty, true, + GlobalValue::ExternalLinkage, + Init, "foo"); + GlobalAlias *GA = new GlobalAlias(Type::getInt8PtrTy(C), + GlobalValue::ExternalLinkage, + "bar", Aliasee, &M); + GA->setUnnamedAddr(true); + std::string Error; + EXPECT_TRUE(verifyModule(M, ReturnStatusAction, &Error)); + EXPECT_TRUE(StringRef(Error).startswith("Alias cannot have unnamed_addr")); +} +} +} diff --git a/unittests/IR/WaymarkTest.cpp b/unittests/IR/WaymarkTest.cpp new file mode 100644 index 0000000000..69fc4da5a6 --- /dev/null +++ b/unittests/IR/WaymarkTest.cpp @@ -0,0 +1,54 @@ +//===- llvm/unittest/IR/WaymarkTest.cpp - getUser() unit tests ------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// we perform white-box tests +// +#include "llvm/IR/Function.h" +#include "llvm/IR/Instructions.h" +#include "llvm/IR/LLVMContext.h" +#include "gtest/gtest.h" +#include + +namespace llvm { +namespace { + +Constant *char2constant(char c) { + return ConstantInt::get(Type::getInt8Ty(getGlobalContext()), c); +} + + +TEST(WaymarkTest, NativeArray) { + static uint8_t tail[22] = "s02s33s30y2y0s1x0syxS"; + Value * values[22]; + std::transform(tail, tail + 22, values, char2constant); + FunctionType *FT = FunctionType::get(Type::getVoidTy(getGlobalContext()), true); + Function *F = Function::Create(FT, GlobalValue::ExternalLinkage); + const CallInst *A = CallInst::Create(F, makeArrayRef(values)); + ASSERT_NE(A, (const CallInst*)NULL); + ASSERT_EQ(1U + 22, A->getNumOperands()); + const Use *U = &A->getOperandUse(0); + const Use *Ue = &A->getOperandUse(22); + for (; U != Ue; ++U) + { + EXPECT_EQ(A, U->getUser()); + } +} + +TEST(WaymarkTest, TwoBit) { + Use* many = (Use*)calloc(sizeof(Use), 8212 + 1); + ASSERT_TRUE(many); + Use::initTags(many, many + 8212); + for (const Use *U = many, *Ue = many + 8212 - 1; U != Ue; ++U) + { + EXPECT_EQ((User*)(Ue + 1), U->getUser()); + } +} + +} // end anonymous namespace +} // end namespace llvm diff --git a/unittests/Makefile b/unittests/Makefile index 27afccf02e..926459ac08 100644 --- a/unittests/Makefile +++ b/unittests/Makefile @@ -9,7 +9,7 @@ LEVEL = .. -PARALLEL_DIRS = ADT ExecutionEngine Support Transforms VMCore Analysis Bitcode +PARALLEL_DIRS = ADT ExecutionEngine Support Transforms IR Analysis Bitcode include $(LEVEL)/Makefile.common diff --git a/unittests/VMCore/CMakeLists.txt b/unittests/VMCore/CMakeLists.txt deleted file mode 100644 index 8d8bb3bb4d..0000000000 --- a/unittests/VMCore/CMakeLists.txt +++ /dev/null @@ -1,36 +0,0 @@ -set(LLVM_LINK_COMPONENTS - asmparser - core - ipa - ) - -set(VMCoreSources - ConstantsTest.cpp - DominatorTreeTest.cpp - IRBuilderTest.cpp - InstructionsTest.cpp - MDBuilderTest.cpp - MetadataTest.cpp - PassManagerTest.cpp - TypeBuilderTest.cpp - TypesTest.cpp - ValueMapTest.cpp - VerifierTest.cpp - WaymarkTest.cpp - ) - -# MSVC9 and 8 cannot compile ValueMapTest.cpp due to their bug. -# See issue#331418 in Visual Studio. -if(MSVC AND MSVC_VERSION LESS 1600) - list(REMOVE_ITEM VMCoreSources ValueMapTest.cpp) -endif() - -# HACK: Declare a couple of source files as optionally compiled to satisfy the -# missing-file-checker in LLVM's weird CMake build. -set(LLVM_OPTIONAL_SOURCES - ValueMapTest.cpp - ) - -add_llvm_unittest(VMCoreTests - ${VMCoreSources} - ) diff --git a/unittests/VMCore/ConstantsTest.cpp b/unittests/VMCore/ConstantsTest.cpp deleted file mode 100644 index 01ab5f2638..0000000000 --- a/unittests/VMCore/ConstantsTest.cpp +++ /dev/null @@ -1,228 +0,0 @@ -//===- llvm/unittest/VMCore/ConstantsTest.cpp - Constants 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/IR/Constants.h" -#include "llvm/IR/DerivedTypes.h" -#include "llvm/IR/InstrTypes.h" -#include "llvm/IR/Instruction.h" -#include "llvm/IR/LLVMContext.h" -#include "llvm/IR/Module.h" -#include "gtest/gtest.h" - -namespace llvm { -namespace { - -TEST(ConstantsTest, Integer_i1) { - IntegerType* Int1 = IntegerType::get(getGlobalContext(), 1); - Constant* One = ConstantInt::get(Int1, 1, true); - Constant* Zero = ConstantInt::get(Int1, 0); - Constant* NegOne = ConstantInt::get(Int1, static_cast(-1), true); - EXPECT_EQ(NegOne, ConstantInt::getSigned(Int1, -1)); - Constant* Undef = UndefValue::get(Int1); - - // Input: @b = constant i1 add(i1 1 , i1 1) - // Output: @b = constant i1 false - EXPECT_EQ(Zero, ConstantExpr::getAdd(One, One)); - - // @c = constant i1 add(i1 -1, i1 1) - // @c = constant i1 false - EXPECT_EQ(Zero, ConstantExpr::getAdd(NegOne, One)); - - // @d = constant i1 add(i1 -1, i1 -1) - // @d = constant i1 false - EXPECT_EQ(Zero, ConstantExpr::getAdd(NegOne, NegOne)); - - // @e = constant i1 sub(i1 -1, i1 1) - // @e = constant i1 false - EXPECT_EQ(Zero, ConstantExpr::getSub(NegOne, One)); - - // @f = constant i1 sub(i1 1 , i1 -1) - // @f = constant i1 false - EXPECT_EQ(Zero, ConstantExpr::getSub(One, NegOne)); - - // @g = constant i1 sub(i1 1 , i1 1) - // @g = constant i1 false - EXPECT_EQ(Zero, ConstantExpr::getSub(One, One)); - - // @h = constant i1 shl(i1 1 , i1 1) ; undefined - // @h = constant i1 undef - EXPECT_EQ(Undef, ConstantExpr::getShl(One, One)); - - // @i = constant i1 shl(i1 1 , i1 0) - // @i = constant i1 true - EXPECT_EQ(One, ConstantExpr::getShl(One, Zero)); - - // @j = constant i1 lshr(i1 1, i1 1) ; undefined - // @j = constant i1 undef - EXPECT_EQ(Undef, ConstantExpr::getLShr(One, One)); - - // @m = constant i1 ashr(i1 1, i1 1) ; undefined - // @m = constant i1 undef - EXPECT_EQ(Undef, ConstantExpr::getAShr(One, One)); - - // @n = constant i1 mul(i1 -1, i1 1) - // @n = constant i1 true - EXPECT_EQ(One, ConstantExpr::getMul(NegOne, One)); - - // @o = constant i1 sdiv(i1 -1, i1 1) ; overflow - // @o = constant i1 true - EXPECT_EQ(One, ConstantExpr::getSDiv(NegOne, One)); - - // @p = constant i1 sdiv(i1 1 , i1 -1); overflow - // @p = constant i1 true - EXPECT_EQ(One, ConstantExpr::getSDiv(One, NegOne)); - - // @q = constant i1 udiv(i1 -1, i1 1) - // @q = constant i1 true - EXPECT_EQ(One, ConstantExpr::getUDiv(NegOne, One)); - - // @r = constant i1 udiv(i1 1, i1 -1) - // @r = constant i1 true - EXPECT_EQ(One, ConstantExpr::getUDiv(One, NegOne)); - - // @s = constant i1 srem(i1 -1, i1 1) ; overflow - // @s = constant i1 false - EXPECT_EQ(Zero, ConstantExpr::getSRem(NegOne, One)); - - // @t = constant i1 urem(i1 -1, i1 1) - // @t = constant i1 false - EXPECT_EQ(Zero, ConstantExpr::getURem(NegOne, One)); - - // @u = constant i1 srem(i1 1, i1 -1) ; overflow - // @u = constant i1 false - EXPECT_EQ(Zero, ConstantExpr::getSRem(One, NegOne)); -} - -TEST(ConstantsTest, IntSigns) { - IntegerType* Int8Ty = Type::getInt8Ty(getGlobalContext()); - EXPECT_EQ(100, ConstantInt::get(Int8Ty, 100, false)->getSExtValue()); - EXPECT_EQ(100, ConstantInt::get(Int8Ty, 100, true)->getSExtValue()); - EXPECT_EQ(100, ConstantInt::getSigned(Int8Ty, 100)->getSExtValue()); - EXPECT_EQ(-50, ConstantInt::get(Int8Ty, 206)->getSExtValue()); - EXPECT_EQ(-50, ConstantInt::getSigned(Int8Ty, -50)->getSExtValue()); - EXPECT_EQ(206U, ConstantInt::getSigned(Int8Ty, -50)->getZExtValue()); - - // Overflow is handled by truncation. - EXPECT_EQ(0x3b, ConstantInt::get(Int8Ty, 0x13b)->getSExtValue()); -} - -TEST(ConstantsTest, FP128Test) { - Type *FP128Ty = Type::getFP128Ty(getGlobalContext()); - - IntegerType *Int128Ty = Type::getIntNTy(getGlobalContext(), 128); - Constant *Zero128 = Constant::getNullValue(Int128Ty); - Constant *X = ConstantExpr::getUIToFP(Zero128, FP128Ty); - EXPECT_TRUE(isa(X)); -} - -#define CHECK(x, y) { \ - std::string __s; \ - raw_string_ostream __o(__s); \ - cast(x)->getAsInstruction()->print(__o); \ - __o.flush(); \ - EXPECT_EQ(std::string(" = " y), __s); \ - } - -TEST(ConstantsTest, AsInstructionsTest) { - Module *M = new Module("MyModule", getGlobalContext()); - - Type *Int64Ty = Type::getInt64Ty(getGlobalContext()); - Type *Int32Ty = Type::getInt32Ty(getGlobalContext()); - Type *Int16Ty = Type::getInt16Ty(getGlobalContext()); - Type *Int1Ty = Type::getInt1Ty(getGlobalContext()); - Type *FloatTy = Type::getFloatTy(getGlobalContext()); - Type *DoubleTy = Type::getDoubleTy(getGlobalContext()); - - Constant *Global = M->getOrInsertGlobal("dummy", - PointerType::getUnqual(Int32Ty)); - Constant *Global2 = M->getOrInsertGlobal("dummy2", - PointerType::getUnqual(Int32Ty)); - - Constant *P0 = ConstantExpr::getPtrToInt(Global, Int32Ty); - Constant *P1 = ConstantExpr::getUIToFP(P0, FloatTy); - Constant *P2 = ConstantExpr::getUIToFP(P0, DoubleTy); - Constant *P3 = ConstantExpr::getTrunc(P0, Int1Ty); - Constant *P4 = ConstantExpr::getPtrToInt(Global2, Int32Ty); - Constant *P5 = ConstantExpr::getUIToFP(P4, FloatTy); - Constant *P6 = ConstantExpr::getBitCast(P4, VectorType::get(Int16Ty, 2)); - - Constant *One = ConstantInt::get(Int32Ty, 1); - - #define P0STR "ptrtoint (i32** @dummy to i32)" - #define P1STR "uitofp (i32 ptrtoint (i32** @dummy to i32) to float)" - #define P2STR "uitofp (i32 ptrtoint (i32** @dummy to i32) to double)" - #define P3STR "ptrtoint (i32** @dummy to i1)" - #define P4STR "ptrtoint (i32** @dummy2 to i32)" - #define P5STR "uitofp (i32 ptrtoint (i32** @dummy2 to i32) to float)" - #define P6STR "bitcast (i32 ptrtoint (i32** @dummy2 to i32) to <2 x i16>)" - - CHECK(ConstantExpr::getNeg(P0), "sub i32 0, " P0STR); - CHECK(ConstantExpr::getFNeg(P1), "fsub float -0.000000e+00, " P1STR); - CHECK(ConstantExpr::getNot(P0), "xor i32 " P0STR ", -1"); - CHECK(ConstantExpr::getAdd(P0, P0), "add i32 " P0STR ", " P0STR); - CHECK(ConstantExpr::getAdd(P0, P0, false, true), "add nsw i32 " P0STR ", " - P0STR); - CHECK(ConstantExpr::getAdd(P0, P0, true, true), "add nuw nsw i32 " P0STR ", " - P0STR); - CHECK(ConstantExpr::getFAdd(P1, P1), "fadd float " P1STR ", " P1STR); - CHECK(ConstantExpr::getSub(P0, P0), "sub i32 " P0STR ", " P0STR); - CHECK(ConstantExpr::getFSub(P1, P1), "fsub float " P1STR ", " P1STR); - CHECK(ConstantExpr::getMul(P0, P0), "mul i32 " P0STR ", " P0STR); - CHECK(ConstantExpr::getFMul(P1, P1), "fmul float " P1STR ", " P1STR); - CHECK(ConstantExpr::getUDiv(P0, P0), "udiv i32 " P0STR ", " P0STR); - CHECK(ConstantExpr::getSDiv(P0, P0), "sdiv i32 " P0STR ", " P0STR); - CHECK(ConstantExpr::getFDiv(P1, P1), "fdiv float " P1STR ", " P1STR); - CHECK(ConstantExpr::getURem(P0, P0), "urem i32 " P0STR ", " P0STR); - CHECK(ConstantExpr::getSRem(P0, P0), "srem i32 " P0STR ", " P0STR); - CHECK(ConstantExpr::getFRem(P1, P1), "frem float " P1STR ", " P1STR); - CHECK(ConstantExpr::getAnd(P0, P0), "and i32 " P0STR ", " P0STR); - CHECK(ConstantExpr::getOr(P0, P0), "or i32 " P0STR ", " P0STR); - CHECK(ConstantExpr::getXor(P0, P0), "xor i32 " P0STR ", " P0STR); - CHECK(ConstantExpr::getShl(P0, P0), "shl i32 " P0STR ", " P0STR); - CHECK(ConstantExpr::getShl(P0, P0, true), "shl nuw i32 " P0STR ", " P0STR); - CHECK(ConstantExpr::getShl(P0, P0, false, true), "shl nsw i32 " P0STR ", " - P0STR); - CHECK(ConstantExpr::getLShr(P0, P0, false), "lshr i32 " P0STR ", " P0STR); - CHECK(ConstantExpr::getLShr(P0, P0, true), "lshr exact i32 " P0STR ", " P0STR); - CHECK(ConstantExpr::getAShr(P0, P0, false), "ashr i32 " P0STR ", " P0STR); - CHECK(ConstantExpr::getAShr(P0, P0, true), "ashr exact i32 " P0STR ", " P0STR); - - CHECK(ConstantExpr::getSExt(P0, Int64Ty), "sext i32 " P0STR " to i64"); - CHECK(ConstantExpr::getZExt(P0, Int64Ty), "zext i32 " P0STR " to i64"); - CHECK(ConstantExpr::getFPTrunc(P2, FloatTy), "fptrunc double " P2STR - " to float"); - CHECK(ConstantExpr::getFPExtend(P1, DoubleTy), "fpext float " P1STR - " to double"); - - CHECK(ConstantExpr::getExactUDiv(P0, P0), "udiv exact i32 " P0STR ", " P0STR); - - CHECK(ConstantExpr::getSelect(P3, P0, P4), "select i1 " P3STR ", i32 " P0STR - ", i32 " P4STR); - CHECK(ConstantExpr::getICmp(CmpInst::ICMP_EQ, P0, P4), "icmp eq i32 " P0STR - ", " P4STR); - CHECK(ConstantExpr::getFCmp(CmpInst::FCMP_ULT, P1, P5), "fcmp ult float " - P1STR ", " P5STR); - - std::vector V; - V.push_back(One); - // FIXME: getGetElementPtr() actually creates an inbounds ConstantGEP, - // not a normal one! - //CHECK(ConstantExpr::getGetElementPtr(Global, V, false), - // "getelementptr i32** @dummy, i32 1"); - CHECK(ConstantExpr::getInBoundsGetElementPtr(Global, V), - "getelementptr inbounds i32** @dummy, i32 1"); - - CHECK(ConstantExpr::getExtractElement(P6, One), "extractelement <2 x i16> " - P6STR ", i32 1"); -} - -#undef CHECK - -} // end anonymous namespace -} // end namespace llvm diff --git a/unittests/VMCore/DominatorTreeTest.cpp b/unittests/VMCore/DominatorTreeTest.cpp deleted file mode 100644 index 3a527adbc0..0000000000 --- a/unittests/VMCore/DominatorTreeTest.cpp +++ /dev/null @@ -1,195 +0,0 @@ -#include "llvm/Analysis/Dominators.h" -#include "llvm/Assembly/Parser.h" -#include "llvm/IR/Instructions.h" -#include "llvm/IR/LLVMContext.h" -#include "llvm/IR/Module.h" -#include "llvm/PassManager.h" -#include "llvm/Support/SourceMgr.h" -#include "gtest/gtest.h" - -using namespace llvm; - -namespace llvm { - void initializeDPassPass(PassRegistry&); - - namespace { - struct DPass : public FunctionPass { - static char ID; - virtual bool runOnFunction(Function &F) { - DominatorTree *DT = &getAnalysis(); - Function::iterator FI = F.begin(); - - BasicBlock *BB0 = FI++; - BasicBlock::iterator BBI = BB0->begin(); - Instruction *Y1 = BBI++; - Instruction *Y2 = BBI++; - Instruction *Y3 = BBI++; - - BasicBlock *BB1 = FI++; - BBI = BB1->begin(); - Instruction *Y4 = BBI++; - - BasicBlock *BB2 = FI++; - BBI = BB2->begin(); - Instruction *Y5 = BBI++; - - BasicBlock *BB3 = FI++; - BBI = BB3->begin(); - Instruction *Y6 = BBI++; - Instruction *Y7 = BBI++; - - BasicBlock *BB4 = FI++; - BBI = BB4->begin(); - Instruction *Y8 = BBI++; - Instruction *Y9 = BBI++; - - // Reachability - EXPECT_TRUE(DT->isReachableFromEntry(BB0)); - EXPECT_TRUE(DT->isReachableFromEntry(BB1)); - EXPECT_TRUE(DT->isReachableFromEntry(BB2)); - EXPECT_FALSE(DT->isReachableFromEntry(BB3)); - EXPECT_TRUE(DT->isReachableFromEntry(BB4)); - - // BB dominance - EXPECT_TRUE(DT->dominates(BB0, BB0)); - EXPECT_TRUE(DT->dominates(BB0, BB1)); - EXPECT_TRUE(DT->dominates(BB0, BB2)); - EXPECT_TRUE(DT->dominates(BB0, BB3)); - EXPECT_TRUE(DT->dominates(BB0, BB4)); - - EXPECT_FALSE(DT->dominates(BB1, BB0)); - EXPECT_TRUE(DT->dominates(BB1, BB1)); - EXPECT_FALSE(DT->dominates(BB1, BB2)); - EXPECT_TRUE(DT->dominates(BB1, BB3)); - EXPECT_FALSE(DT->dominates(BB1, BB4)); - - EXPECT_FALSE(DT->dominates(BB2, BB0)); - EXPECT_FALSE(DT->dominates(BB2, BB1)); - EXPECT_TRUE(DT->dominates(BB2, BB2)); - EXPECT_TRUE(DT->dominates(BB2, BB3)); - EXPECT_FALSE(DT->dominates(BB2, BB4)); - - EXPECT_FALSE(DT->dominates(BB3, BB0)); - EXPECT_FALSE(DT->dominates(BB3, BB1)); - EXPECT_FALSE(DT->dominates(BB3, BB2)); - EXPECT_TRUE(DT->dominates(BB3, BB3)); - EXPECT_FALSE(DT->dominates(BB3, BB4)); - - // BB proper dominance - EXPECT_FALSE(DT->properlyDominates(BB0, BB0)); - EXPECT_TRUE(DT->properlyDominates(BB0, BB1)); - EXPECT_TRUE(DT->properlyDominates(BB0, BB2)); - EXPECT_TRUE(DT->properlyDominates(BB0, BB3)); - - EXPECT_FALSE(DT->properlyDominates(BB1, BB0)); - EXPECT_FALSE(DT->properlyDominates(BB1, BB1)); - EXPECT_FALSE(DT->properlyDominates(BB1, BB2)); - EXPECT_TRUE(DT->properlyDominates(BB1, BB3)); - - EXPECT_FALSE(DT->properlyDominates(BB2, BB0)); - EXPECT_FALSE(DT->properlyDominates(BB2, BB1)); - EXPECT_FALSE(DT->properlyDominates(BB2, BB2)); - EXPECT_TRUE(DT->properlyDominates(BB2, BB3)); - - EXPECT_FALSE(DT->properlyDominates(BB3, BB0)); - EXPECT_FALSE(DT->properlyDominates(BB3, BB1)); - EXPECT_FALSE(DT->properlyDominates(BB3, BB2)); - EXPECT_FALSE(DT->properlyDominates(BB3, BB3)); - - // Instruction dominance in the same reachable BB - EXPECT_FALSE(DT->dominates(Y1, Y1)); - EXPECT_TRUE(DT->dominates(Y1, Y2)); - EXPECT_FALSE(DT->dominates(Y2, Y1)); - EXPECT_FALSE(DT->dominates(Y2, Y2)); - - // Instruction dominance in the same unreachable BB - EXPECT_TRUE(DT->dominates(Y6, Y6)); - EXPECT_TRUE(DT->dominates(Y6, Y7)); - EXPECT_TRUE(DT->dominates(Y7, Y6)); - EXPECT_TRUE(DT->dominates(Y7, Y7)); - - // Invoke - EXPECT_TRUE(DT->dominates(Y3, Y4)); - EXPECT_FALSE(DT->dominates(Y3, Y5)); - - // Phi - EXPECT_TRUE(DT->dominates(Y2, Y9)); - EXPECT_FALSE(DT->dominates(Y3, Y9)); - EXPECT_FALSE(DT->dominates(Y8, Y9)); - - // Anything dominates unreachable - EXPECT_TRUE(DT->dominates(Y1, Y6)); - EXPECT_TRUE(DT->dominates(Y3, Y6)); - - // Unreachable doesn't dominate reachable - EXPECT_FALSE(DT->dominates(Y6, Y1)); - - // Instruction, BB dominance - EXPECT_FALSE(DT->dominates(Y1, BB0)); - EXPECT_TRUE(DT->dominates(Y1, BB1)); - EXPECT_TRUE(DT->dominates(Y1, BB2)); - EXPECT_TRUE(DT->dominates(Y1, BB3)); - EXPECT_TRUE(DT->dominates(Y1, BB4)); - - EXPECT_FALSE(DT->dominates(Y3, BB0)); - EXPECT_TRUE(DT->dominates(Y3, BB1)); - EXPECT_FALSE(DT->dominates(Y3, BB2)); - EXPECT_TRUE(DT->dominates(Y3, BB3)); - EXPECT_FALSE(DT->dominates(Y3, BB4)); - - EXPECT_TRUE(DT->dominates(Y6, BB3)); - - return false; - } - virtual void getAnalysisUsage(AnalysisUsage &AU) const { - AU.addRequired(); - } - DPass() : FunctionPass(ID) { - initializeDPassPass(*PassRegistry::getPassRegistry()); - } - }; - char DPass::ID = 0; - - - Module* makeLLVMModule(DPass *P) { - const char *ModuleStrig = - "declare i32 @g()\n" \ - "define void @f(i32 %x) {\n" \ - "bb0:\n" \ - " %y1 = add i32 %x, 1\n" \ - " %y2 = add i32 %x, 1\n" \ - " %y3 = invoke i32 @g() to label %bb1 unwind label %bb2\n" \ - "bb1:\n" \ - " %y4 = add i32 %x, 1\n" \ - " br label %bb4\n" \ - "bb2:\n" \ - " %y5 = landingpad i32 personality i32 ()* @g\n" \ - " cleanup\n" \ - " br label %bb4\n" \ - "bb3:\n" \ - " %y6 = add i32 %x, 1\n" \ - " %y7 = add i32 %x, 1\n" \ - " ret void\n" \ - "bb4:\n" \ - " %y8 = phi i32 [0, %bb2], [%y4, %bb1]\n" - " %y9 = phi i32 [0, %bb2], [%y4, %bb1]\n" - " ret void\n" \ - "}\n"; - LLVMContext &C = getGlobalContext(); - SMDiagnostic Err; - return ParseAssemblyString(ModuleStrig, NULL, Err, C); - } - - TEST(DominatorTree, Unreachable) { - DPass *P = new DPass(); - Module *M = makeLLVMModule(P); - PassManager Passes; - Passes.add(P); - Passes.run(*M); - } - } -} - -INITIALIZE_PASS_BEGIN(DPass, "dpass", "dpass", false, false) -INITIALIZE_PASS_DEPENDENCY(DominatorTree) -INITIALIZE_PASS_END(DPass, "dpass", "dpass", false, false) diff --git a/unittests/VMCore/IRBuilderTest.cpp b/unittests/VMCore/IRBuilderTest.cpp deleted file mode 100644 index a9df2f6911..0000000000 --- a/unittests/VMCore/IRBuilderTest.cpp +++ /dev/null @@ -1,184 +0,0 @@ -//===- llvm/unittest/VMCore/IRBuilderTest.cpp - IRBuilder tests -----------===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// - -#include "llvm/IR/IRBuilder.h" -#include "llvm/ADT/OwningPtr.h" -#include "llvm/IR/BasicBlock.h" -#include "llvm/IR/DataLayout.h" -#include "llvm/IR/Function.h" -#include "llvm/IR/IntrinsicInst.h" -#include "llvm/IR/LLVMContext.h" -#include "llvm/IR/MDBuilder.h" -#include "llvm/IR/Module.h" -#include "gtest/gtest.h" - -using namespace llvm; - -namespace { - -class IRBuilderTest : public testing::Test { -protected: - virtual void SetUp() { - M.reset(new Module("MyModule", getGlobalContext())); - FunctionType *FTy = FunctionType::get(Type::getVoidTy(getGlobalContext()), - /*isVarArg=*/false); - F = Function::Create(FTy, Function::ExternalLinkage, "", M.get()); - BB = BasicBlock::Create(getGlobalContext(), "", F); - GV = new GlobalVariable(Type::getFloatTy(getGlobalContext()), true, - GlobalValue::ExternalLinkage); - } - - virtual void TearDown() { - BB = 0; - M.reset(); - } - - OwningPtr M; - Function *F; - BasicBlock *BB; - GlobalVariable *GV; -}; - -TEST_F(IRBuilderTest, Lifetime) { - IRBuilder<> Builder(BB); - AllocaInst *Var1 = Builder.CreateAlloca(Builder.getInt8Ty()); - AllocaInst *Var2 = Builder.CreateAlloca(Builder.getInt32Ty()); - AllocaInst *Var3 = Builder.CreateAlloca(Builder.getInt8Ty(), - Builder.getInt32(123)); - - CallInst *Start1 = Builder.CreateLifetimeStart(Var1); - CallInst *Start2 = Builder.CreateLifetimeStart(Var2); - CallInst *Start3 = Builder.CreateLifetimeStart(Var3, Builder.getInt64(100)); - - EXPECT_EQ(Start1->getArgOperand(0), Builder.getInt64(-1)); - EXPECT_EQ(Start2->getArgOperand(0), Builder.getInt64(-1)); - EXPECT_EQ(Start3->getArgOperand(0), Builder.getInt64(100)); - - EXPECT_EQ(Start1->getArgOperand(1), Var1); - EXPECT_NE(Start2->getArgOperand(1), Var2); - EXPECT_EQ(Start3->getArgOperand(1), Var3); - - Value *End1 = Builder.CreateLifetimeEnd(Var1); - Builder.CreateLifetimeEnd(Var2); - Builder.CreateLifetimeEnd(Var3); - - IntrinsicInst *II_Start1 = dyn_cast(Start1); - IntrinsicInst *II_End1 = dyn_cast(End1); - ASSERT_TRUE(II_Start1 != NULL); - EXPECT_EQ(II_Start1->getIntrinsicID(), Intrinsic::lifetime_start); - ASSERT_TRUE(II_End1 != NULL); - EXPECT_EQ(II_End1->getIntrinsicID(), Intrinsic::lifetime_end); -} - -TEST_F(IRBuilderTest, CreateCondBr) { - IRBuilder<> Builder(BB); - BasicBlock *TBB = BasicBlock::Create(getGlobalContext(), "", F); - BasicBlock *FBB = BasicBlock::Create(getGlobalContext(), "", F); - - BranchInst *BI = Builder.CreateCondBr(Builder.getTrue(), TBB, FBB); - TerminatorInst *TI = BB->getTerminator(); - EXPECT_EQ(BI, TI); - EXPECT_EQ(2u, TI->getNumSuccessors()); - EXPECT_EQ(TBB, TI->getSuccessor(0)); - EXPECT_EQ(FBB, TI->getSuccessor(1)); - - BI->eraseFromParent(); - MDNode *Weights = MDBuilder(getGlobalContext()).createBranchWeights(42, 13); - BI = Builder.CreateCondBr(Builder.getTrue(), TBB, FBB, Weights); - TI = BB->getTerminator(); - EXPECT_EQ(BI, TI); - EXPECT_EQ(2u, TI->getNumSuccessors()); - EXPECT_EQ(TBB, TI->getSuccessor(0)); - EXPECT_EQ(FBB, TI->getSuccessor(1)); - EXPECT_EQ(Weights, TI->getMetadata(LLVMContext::MD_prof)); -} - -TEST_F(IRBuilderTest, LandingPadName) { - IRBuilder<> Builder(BB); - LandingPadInst *LP = Builder.CreateLandingPad(Builder.getInt32Ty(), - Builder.getInt32(0), 0, "LP"); - EXPECT_EQ(LP->getName(), "LP"); -} - -TEST_F(IRBuilderTest, GetIntTy) { - IRBuilder<> Builder(BB); - IntegerType *Ty1 = Builder.getInt1Ty(); - EXPECT_EQ(Ty1, IntegerType::get(getGlobalContext(), 1)); - - DataLayout* DL = new DataLayout(M.get()); - IntegerType *IntPtrTy = Builder.getIntPtrTy(DL); - unsigned IntPtrBitSize = DL->getPointerSizeInBits(0); - EXPECT_EQ(IntPtrTy, IntegerType::get(getGlobalContext(), IntPtrBitSize)); -} - -TEST_F(IRBuilderTest, FastMathFlags) { - IRBuilder<> Builder(BB); - Value *F; - Instruction *FDiv, *FAdd; - - F = Builder.CreateLoad(GV); - F = Builder.CreateFAdd(F, F); - - EXPECT_FALSE(Builder.getFastMathFlags().any()); - ASSERT_TRUE(isa(F)); - FAdd = cast(F); - EXPECT_FALSE(FAdd->hasNoNaNs()); - - FastMathFlags FMF; - Builder.SetFastMathFlags(FMF); - - F = Builder.CreateFAdd(F, F); - EXPECT_FALSE(Builder.getFastMathFlags().any()); - - FMF.setUnsafeAlgebra(); - Builder.SetFastMathFlags(FMF); - - F = Builder.CreateFAdd(F, F); - EXPECT_TRUE(Builder.getFastMathFlags().any()); - ASSERT_TRUE(isa(F)); - FAdd = cast(F); - EXPECT_TRUE(FAdd->hasNoNaNs()); - - F = Builder.CreateFDiv(F, F); - EXPECT_TRUE(Builder.getFastMathFlags().any()); - EXPECT_TRUE(Builder.getFastMathFlags().UnsafeAlgebra); - ASSERT_TRUE(isa(F)); - FDiv = cast(F); - EXPECT_TRUE(FDiv->hasAllowReciprocal()); - - Builder.clearFastMathFlags(); - - F = Builder.CreateFDiv(F, F); - ASSERT_TRUE(isa(F)); - FDiv = cast(F); - EXPECT_FALSE(FDiv->hasAllowReciprocal()); - - FMF.clear(); - FMF.setAllowReciprocal(); - Builder.SetFastMathFlags(FMF); - - F = Builder.CreateFDiv(F, F); - EXPECT_TRUE(Builder.getFastMathFlags().any()); - EXPECT_TRUE(Builder.getFastMathFlags().AllowReciprocal); - ASSERT_TRUE(isa(F)); - FDiv = cast(F); - EXPECT_TRUE(FDiv->hasAllowReciprocal()); - - Builder.clearFastMathFlags(); - - F = Builder.CreateFDiv(F, F); - ASSERT_TRUE(isa(F)); - FDiv = cast(F); - EXPECT_FALSE(FDiv->getFastMathFlags().any()); - FDiv->copyFastMathFlags(FAdd); - EXPECT_TRUE(FDiv->hasNoNaNs()); - -} - -} diff --git a/unittests/VMCore/InstructionsTest.cpp b/unittests/VMCore/InstructionsTest.cpp deleted file mode 100644 index 6559b1d0ee..0000000000 --- a/unittests/VMCore/InstructionsTest.cpp +++ /dev/null @@ -1,284 +0,0 @@ -//===- llvm/unittest/VMCore/InstructionsTest.cpp - Instructions 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/IR/Instructions.h" -#include "llvm/ADT/STLExtras.h" -#include "llvm/Analysis/ValueTracking.h" -#include "llvm/IR/BasicBlock.h" -#include "llvm/IR/Constants.h" -#include "llvm/IR/DataLayout.h" -#include "llvm/IR/DerivedTypes.h" -#include "llvm/IR/IRBuilder.h" -#include "llvm/IR/LLVMContext.h" -#include "llvm/IR/MDBuilder.h" -#include "llvm/IR/Operator.h" -#include "gtest/gtest.h" - -namespace llvm { -namespace { - -TEST(InstructionsTest, ReturnInst) { - LLVMContext &C(getGlobalContext()); - - // test for PR6589 - const ReturnInst* r0 = ReturnInst::Create(C); - EXPECT_EQ(r0->getNumOperands(), 0U); - EXPECT_EQ(r0->op_begin(), r0->op_end()); - - IntegerType* Int1 = IntegerType::get(C, 1); - Constant* One = ConstantInt::get(Int1, 1, true); - const ReturnInst* r1 = ReturnInst::Create(C, One); - EXPECT_EQ(1U, r1->getNumOperands()); - User::const_op_iterator b(r1->op_begin()); - EXPECT_NE(r1->op_end(), b); - EXPECT_EQ(One, *b); - EXPECT_EQ(One, r1->getOperand(0)); - ++b; - EXPECT_EQ(r1->op_end(), b); - - // clean up - delete r0; - delete r1; -} - -TEST(InstructionsTest, BranchInst) { - LLVMContext &C(getGlobalContext()); - - // Make a BasicBlocks - BasicBlock* bb0 = BasicBlock::Create(C); - BasicBlock* bb1 = BasicBlock::Create(C); - - // Mandatory BranchInst - const BranchInst* b0 = BranchInst::Create(bb0); - - EXPECT_TRUE(b0->isUnconditional()); - EXPECT_FALSE(b0->isConditional()); - EXPECT_EQ(1U, b0->getNumSuccessors()); - - // check num operands - EXPECT_EQ(1U, b0->getNumOperands()); - - EXPECT_NE(b0->op_begin(), b0->op_end()); - EXPECT_EQ(b0->op_end(), llvm::next(b0->op_begin())); - - EXPECT_EQ(b0->op_end(), llvm::next(b0->op_begin())); - - IntegerType* Int1 = IntegerType::get(C, 1); - Constant* One = ConstantInt::get(Int1, 1, true); - - // Conditional BranchInst - BranchInst* b1 = BranchInst::Create(bb0, bb1, One); - - EXPECT_FALSE(b1->isUnconditional()); - EXPECT_TRUE(b1->isConditional()); - EXPECT_EQ(2U, b1->getNumSuccessors()); - - // check num operands - EXPECT_EQ(3U, b1->getNumOperands()); - - User::const_op_iterator b(b1->op_begin()); - - // check COND - EXPECT_NE(b, b1->op_end()); - EXPECT_EQ(One, *b); - EXPECT_EQ(One, b1->getOperand(0)); - EXPECT_EQ(One, b1->getCondition()); - ++b; - - // check ELSE - EXPECT_EQ(bb1, *b); - EXPECT_EQ(bb1, b1->getOperand(1)); - EXPECT_EQ(bb1, b1->getSuccessor(1)); - ++b; - - // check THEN - EXPECT_EQ(bb0, *b); - EXPECT_EQ(bb0, b1->getOperand(2)); - EXPECT_EQ(bb0, b1->getSuccessor(0)); - ++b; - - EXPECT_EQ(b1->op_end(), b); - - // clean up - delete b0; - delete b1; - - delete bb0; - delete bb1; -} - -TEST(InstructionsTest, CastInst) { - LLVMContext &C(getGlobalContext()); - - Type* Int8Ty = Type::getInt8Ty(C); - Type* Int64Ty = Type::getInt64Ty(C); - Type* V8x8Ty = VectorType::get(Int8Ty, 8); - Type* V8x64Ty = VectorType::get(Int64Ty, 8); - Type* X86MMXTy = Type::getX86_MMXTy(C); - - const Constant* c8 = Constant::getNullValue(V8x8Ty); - const Constant* c64 = Constant::getNullValue(V8x64Ty); - - EXPECT_TRUE(CastInst::isCastable(V8x8Ty, X86MMXTy)); - EXPECT_TRUE(CastInst::isCastable(X86MMXTy, V8x8Ty)); - EXPECT_FALSE(CastInst::isCastable(Int64Ty, X86MMXTy)); - EXPECT_TRUE(CastInst::isCastable(V8x64Ty, V8x8Ty)); - EXPECT_TRUE(CastInst::isCastable(V8x8Ty, V8x64Ty)); - EXPECT_EQ(CastInst::Trunc, CastInst::getCastOpcode(c64, true, V8x8Ty, true)); - EXPECT_EQ(CastInst::SExt, CastInst::getCastOpcode(c8, true, V8x64Ty, true)); -} - - - -TEST(InstructionsTest, VectorGep) { - LLVMContext &C(getGlobalContext()); - - // Type Definitions - PointerType *Ptri8Ty = PointerType::get(IntegerType::get(C, 8), 0); - PointerType *Ptri32Ty = PointerType::get(IntegerType::get(C, 8), 0); - - VectorType *V2xi8PTy = VectorType::get(Ptri8Ty, 2); - VectorType *V2xi32PTy = VectorType::get(Ptri32Ty, 2); - - // Test different aspects of the vector-of-pointers type - // and GEPs which use this type. - ConstantInt *Ci32a = ConstantInt::get(C, APInt(32, 1492)); - ConstantInt *Ci32b = ConstantInt::get(C, APInt(32, 1948)); - std::vector ConstVa(2, Ci32a); - std::vector ConstVb(2, Ci32b); - Constant *C2xi32a = ConstantVector::get(ConstVa); - Constant *C2xi32b = ConstantVector::get(ConstVb); - - CastInst *PtrVecA = new IntToPtrInst(C2xi32a, V2xi32PTy); - CastInst *PtrVecB = new IntToPtrInst(C2xi32b, V2xi32PTy); - - ICmpInst *ICmp0 = new ICmpInst(ICmpInst::ICMP_SGT, PtrVecA, PtrVecB); - ICmpInst *ICmp1 = new ICmpInst(ICmpInst::ICMP_ULT, PtrVecA, PtrVecB); - EXPECT_NE(ICmp0, ICmp1); // suppress warning. - - GetElementPtrInst *Gep0 = GetElementPtrInst::Create(PtrVecA, C2xi32a); - GetElementPtrInst *Gep1 = GetElementPtrInst::Create(PtrVecA, C2xi32b); - GetElementPtrInst *Gep2 = GetElementPtrInst::Create(PtrVecB, C2xi32a); - GetElementPtrInst *Gep3 = GetElementPtrInst::Create(PtrVecB, C2xi32b); - - CastInst *BTC0 = new BitCastInst(Gep0, V2xi8PTy); - CastInst *BTC1 = new BitCastInst(Gep1, V2xi8PTy); - CastInst *BTC2 = new BitCastInst(Gep2, V2xi8PTy); - CastInst *BTC3 = new BitCastInst(Gep3, V2xi8PTy); - - Value *S0 = BTC0->stripPointerCasts(); - Value *S1 = BTC1->stripPointerCasts(); - Value *S2 = BTC2->stripPointerCasts(); - Value *S3 = BTC3->stripPointerCasts(); - - EXPECT_NE(S0, Gep0); - EXPECT_NE(S1, Gep1); - EXPECT_NE(S2, Gep2); - EXPECT_NE(S3, Gep3); - - int64_t Offset; - DataLayout TD("e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f3" - "2:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80" - ":128:128-n8:16:32:64-S128"); - // Make sure we don't crash - GetPointerBaseWithConstantOffset(Gep0, Offset, TD); - GetPointerBaseWithConstantOffset(Gep1, Offset, TD); - GetPointerBaseWithConstantOffset(Gep2, Offset, TD); - GetPointerBaseWithConstantOffset(Gep3, Offset, TD); - - // Gep of Geps - GetElementPtrInst *GepII0 = GetElementPtrInst::Create(Gep0, C2xi32b); - GetElementPtrInst *GepII1 = GetElementPtrInst::Create(Gep1, C2xi32a); - GetElementPtrInst *GepII2 = GetElementPtrInst::Create(Gep2, C2xi32b); - GetElementPtrInst *GepII3 = GetElementPtrInst::Create(Gep3, C2xi32a); - - EXPECT_EQ(GepII0->getNumIndices(), 1u); - EXPECT_EQ(GepII1->getNumIndices(), 1u); - EXPECT_EQ(GepII2->getNumIndices(), 1u); - EXPECT_EQ(GepII3->getNumIndices(), 1u); - - EXPECT_FALSE(GepII0->hasAllZeroIndices()); - EXPECT_FALSE(GepII1->hasAllZeroIndices()); - EXPECT_FALSE(GepII2->hasAllZeroIndices()); - EXPECT_FALSE(GepII3->hasAllZeroIndices()); - - delete GepII0; - delete GepII1; - delete GepII2; - delete GepII3; - - delete BTC0; - delete BTC1; - delete BTC2; - delete BTC3; - - delete Gep0; - delete Gep1; - delete Gep2; - delete Gep3; - - delete ICmp0; - delete ICmp1; - delete PtrVecA; - delete PtrVecB; -} - -TEST(InstructionsTest, FPMathOperator) { - LLVMContext &Context = getGlobalContext(); - IRBuilder<> Builder(Context); - MDBuilder MDHelper(Context); - Instruction *I = Builder.CreatePHI(Builder.getDoubleTy(), 0); - MDNode *MD1 = MDHelper.createFPMath(1.0); - Value *V1 = Builder.CreateFAdd(I, I, "", MD1); - EXPECT_TRUE(isa(V1)); - FPMathOperator *O1 = cast(V1); - EXPECT_EQ(O1->getFPAccuracy(), 1.0); - delete V1; - delete I; -} - - -TEST(InstructionsTest, isEliminableCastPair) { - LLVMContext &C(getGlobalContext()); - - Type* Int32Ty = Type::getInt32Ty(C); - Type* Int64Ty = Type::getInt64Ty(C); - Type* Int64PtrTy = Type::getInt64PtrTy(C); - - // Source and destination pointers have same size -> bitcast. - EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt, - CastInst::IntToPtr, - Int64PtrTy, Int64Ty, Int64PtrTy, - Int32Ty, 0, Int32Ty), - CastInst::BitCast); - - // Source and destination pointers have different sizes -> fail. - EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt, - CastInst::IntToPtr, - Int64PtrTy, Int64Ty, Int64PtrTy, - Int32Ty, 0, Int64Ty), - 0U); - - // Middle pointer big enough -> bitcast. - EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr, - CastInst::PtrToInt, - Int64Ty, Int64PtrTy, Int64Ty, - 0, Int64Ty, 0), - CastInst::BitCast); - - // Middle pointer too small -> fail. - EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr, - CastInst::PtrToInt, - Int64Ty, Int64PtrTy, Int64Ty, - 0, Int32Ty, 0), - 0U); -} - -} // end anonymous namespace -} // end namespace llvm diff --git a/unittests/VMCore/MDBuilderTest.cpp b/unittests/VMCore/MDBuilderTest.cpp deleted file mode 100644 index 665d559bf0..0000000000 --- a/unittests/VMCore/MDBuilderTest.cpp +++ /dev/null @@ -1,106 +0,0 @@ -//===- llvm/unittests/MDBuilderTest.cpp - MDBuilder 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/IR/MDBuilder.h" -#include "llvm/IR/IRBuilder.h" -#include "llvm/IR/Operator.h" -#include "gtest/gtest.h" - -using namespace llvm; - -namespace { - -class MDBuilderTest : public testing::Test { -protected: - LLVMContext Context; -}; - -TEST_F(MDBuilderTest, createString) { - MDBuilder MDHelper(Context); - MDString *Str0 = MDHelper.createString(""); - MDString *Str1 = MDHelper.createString("string"); - EXPECT_EQ(Str0->getString(), StringRef("")); - EXPECT_EQ(Str1->getString(), StringRef("string")); -} -TEST_F(MDBuilderTest, createFPMath) { - MDBuilder MDHelper(Context); - MDNode *MD0 = MDHelper.createFPMath(0.0); - MDNode *MD1 = MDHelper.createFPMath(1.0); - EXPECT_EQ(MD0, (MDNode *)0); - EXPECT_NE(MD1, (MDNode *)0); - EXPECT_EQ(MD1->getNumOperands(), 1U); - Value *Op = MD1->getOperand(0); - EXPECT_TRUE(isa(Op)); - EXPECT_TRUE(Op->getType()->isFloatingPointTy()); - ConstantFP *Val = cast(Op); - EXPECT_TRUE(Val->isExactlyValue(1.0)); -} -TEST_F(MDBuilderTest, createRangeMetadata) { - MDBuilder MDHelper(Context); - APInt A(8, 1), B(8, 2); - MDNode *R0 = MDHelper.createRange(A, A); - MDNode *R1 = MDHelper.createRange(A, B); - EXPECT_EQ(R0, (MDNode *)0); - EXPECT_NE(R1, (MDNode *)0); - EXPECT_EQ(R1->getNumOperands(), 2U); - EXPECT_TRUE(isa(R1->getOperand(0))); - EXPECT_TRUE(isa(R1->getOperand(1))); - ConstantInt *C0 = cast(R1->getOperand(0)); - ConstantInt *C1 = cast(R1->getOperand(1)); - EXPECT_EQ(C0->getValue(), A); - EXPECT_EQ(C1->getValue(), B); -} -TEST_F(MDBuilderTest, createAnonymousTBAARoot) { - MDBuilder MDHelper(Context); - MDNode *R0 = MDHelper.createAnonymousTBAARoot(); - MDNode *R1 = MDHelper.createAnonymousTBAARoot(); - EXPECT_NE(R0, R1); - EXPECT_GE(R0->getNumOperands(), 1U); - EXPECT_GE(R1->getNumOperands(), 1U); - EXPECT_EQ(R0->getOperand(0), R0); - EXPECT_EQ(R1->getOperand(0), R1); - EXPECT_TRUE(R0->getNumOperands() == 1 || R0->getOperand(1) == 0); - EXPECT_TRUE(R1->getNumOperands() == 1 || R1->getOperand(1) == 0); -} -TEST_F(MDBuilderTest, createTBAARoot) { - MDBuilder MDHelper(Context); - MDNode *R0 = MDHelper.createTBAARoot("Root"); - MDNode *R1 = MDHelper.createTBAARoot("Root"); - EXPECT_EQ(R0, R1); - EXPECT_GE(R0->getNumOperands(), 1U); - EXPECT_TRUE(isa(R0->getOperand(0))); - EXPECT_EQ(cast(R0->getOperand(0))->getString(), "Root"); - EXPECT_TRUE(R0->getNumOperands() == 1 || R0->getOperand(1) == 0); -} -TEST_F(MDBuilderTest, createTBAANode) { - MDBuilder MDHelper(Context); - MDNode *R = MDHelper.createTBAARoot("Root"); - MDNode *N0 = MDHelper.createTBAANode("Node", R); - MDNode *N1 = MDHelper.createTBAANode("edoN", R); - MDNode *N2 = MDHelper.createTBAANode("Node", R, true); - MDNode *N3 = MDHelper.createTBAANode("Node", R); - EXPECT_EQ(N0, N3); - EXPECT_NE(N0, N1); - EXPECT_NE(N0, N2); - EXPECT_GE(N0->getNumOperands(), 2U); - EXPECT_GE(N1->getNumOperands(), 2U); - EXPECT_GE(N2->getNumOperands(), 3U); - EXPECT_TRUE(isa(N0->getOperand(0))); - EXPECT_TRUE(isa(N1->getOperand(0))); - EXPECT_TRUE(isa(N2->getOperand(0))); - EXPECT_EQ(cast(N0->getOperand(0))->getString(), "Node"); - EXPECT_EQ(cast(N1->getOperand(0))->getString(), "edoN"); - EXPECT_EQ(cast(N2->getOperand(0))->getString(), "Node"); - EXPECT_EQ(N0->getOperand(1), R); - EXPECT_EQ(N1->getOperand(1), R); - EXPECT_EQ(N2->getOperand(1), R); - EXPECT_TRUE(isa(N2->getOperand(2))); - EXPECT_EQ(cast(N2->getOperand(2))->getZExtValue(), 1U); -} -} diff --git a/unittests/VMCore/Makefile b/unittests/VMCore/Makefile deleted file mode 100644 index d743dc5d5b..0000000000 --- a/unittests/VMCore/Makefile +++ /dev/null @@ -1,15 +0,0 @@ -##===- unittests/VMCore/Makefile ---------------------------*- Makefile -*-===## -# -# The LLVM Compiler Infrastructure -# -# This file is distributed under the University of Illinois Open Source -# License. See LICENSE.TXT for details. -# -##===----------------------------------------------------------------------===## - -LEVEL = ../.. -TESTNAME = VMCore -LINK_COMPONENTS := core ipa asmparser - -include $(LEVEL)/Makefile.config -include $(LLVM_SRC_ROOT)/unittests/Makefile.unittest diff --git a/unittests/VMCore/MetadataTest.cpp b/unittests/VMCore/MetadataTest.cpp deleted file mode 100644 index d0b6c520b6..0000000000 --- a/unittests/VMCore/MetadataTest.cpp +++ /dev/null @@ -1,152 +0,0 @@ -//===- llvm/unittest/VMCore/Metadata.cpp - Metadata 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/IR/Metadata.h" -#include "llvm/IR/Constants.h" -#include "llvm/IR/Instructions.h" -#include "llvm/IR/LLVMContext.h" -#include "llvm/IR/Module.h" -#include "llvm/IR/Type.h" -#include "llvm/Support/ValueHandle.h" -#include "llvm/Support/raw_ostream.h" -#include "gtest/gtest.h" -using namespace llvm; - -namespace { - -class MetadataTest : public testing::Test { -protected: - LLVMContext Context; -}; -typedef MetadataTest MDStringTest; - -// Test that construction of MDString with different value produces different -// MDString objects, even with the same string pointer and nulls in the string. -TEST_F(MDStringTest, CreateDifferent) { - char x[3] = { 'f', 0, 'A' }; - MDString *s1 = MDString::get(Context, StringRef(&x[0], 3)); - x[2] = 'B'; - MDString *s2 = MDString::get(Context, StringRef(&x[0], 3)); - EXPECT_NE(s1, s2); -} - -// Test that creation of MDStrings with the same string contents produces the -// same MDString object, even with different pointers. -TEST_F(MDStringTest, CreateSame) { - char x[4] = { 'a', 'b', 'c', 'X' }; - char y[4] = { 'a', 'b', 'c', 'Y' }; - - MDString *s1 = MDString::get(Context, StringRef(&x[0], 3)); - MDString *s2 = MDString::get(Context, StringRef(&y[0], 3)); - EXPECT_EQ(s1, s2); -} - -// Test that MDString prints out the string we fed it. -TEST_F(MDStringTest, PrintingSimple) { - char *str = new char[13]; - strncpy(str, "testing 1 2 3", 13); - MDString *s = MDString::get(Context, StringRef(str, 13)); - strncpy(str, "aaaaaaaaaaaaa", 13); - delete[] str; - - std::string Str; - raw_string_ostream oss(Str); - s->print(oss); - EXPECT_STREQ("metadata !\"testing 1 2 3\"", oss.str().c_str()); -} - -// Test printing of MDString with non-printable characters. -TEST_F(MDStringTest, PrintingComplex) { - char str[5] = {0, '\n', '"', '\\', (char)-1}; - MDString *s = MDString::get(Context, StringRef(str+0, 5)); - std::string Str; - raw_string_ostream oss(Str); - s->print(oss); - EXPECT_STREQ("metadata !\"\\00\\0A\\22\\5C\\FF\"", oss.str().c_str()); -} - -typedef MetadataTest MDNodeTest; - -// Test the two constructors, and containing other Constants. -TEST_F(MDNodeTest, Simple) { - char x[3] = { 'a', 'b', 'c' }; - char y[3] = { '1', '2', '3' }; - - MDString *s1 = MDString::get(Context, StringRef(&x[0], 3)); - MDString *s2 = MDString::get(Context, StringRef(&y[0], 3)); - ConstantInt *CI = ConstantInt::get(getGlobalContext(), APInt(8, 0)); - - std::vector V; - V.push_back(s1); - V.push_back(CI); - V.push_back(s2); - - MDNode *n1 = MDNode::get(Context, V); - Value *const c1 = n1; - MDNode *n2 = MDNode::get(Context, c1); - Value *const c2 = n2; - MDNode *n3 = MDNode::get(Context, V); - MDNode *n4 = MDNode::getIfExists(Context, V); - MDNode *n5 = MDNode::getIfExists(Context, c1); - MDNode *n6 = MDNode::getIfExists(Context, c2); - EXPECT_NE(n1, n2); -#ifdef ENABLE_MDNODE_UNIQUING - EXPECT_EQ(n1, n3); -#else - (void) n3; -#endif - EXPECT_EQ(n4, n1); - EXPECT_EQ(n5, n2); - EXPECT_EQ(n6, (Value*)0); - - EXPECT_EQ(3u, n1->getNumOperands()); - EXPECT_EQ(s1, n1->getOperand(0)); - EXPECT_EQ(CI, n1->getOperand(1)); - EXPECT_EQ(s2, n1->getOperand(2)); - - EXPECT_EQ(1u, n2->getNumOperands()); - EXPECT_EQ(n1, n2->getOperand(0)); -} - -TEST_F(MDNodeTest, Delete) { - Constant *C = ConstantInt::get(Type::getInt32Ty(getGlobalContext()), 1); - Instruction *I = new BitCastInst(C, Type::getInt32Ty(getGlobalContext())); - - Value *const V = I; - MDNode *n = MDNode::get(Context, V); - WeakVH wvh = n; - - EXPECT_EQ(n, wvh); - - delete I; -} - -TEST(NamedMDNodeTest, Search) { - LLVMContext Context; - Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 1); - Constant *C2 = ConstantInt::get(Type::getInt32Ty(Context), 2); - - Value *const V = C; - Value *const V2 = C2; - MDNode *n = MDNode::get(Context, V); - MDNode *n2 = MDNode::get(Context, V2); - - Module M("MyModule", Context); - const char *Name = "llvm.NMD1"; - NamedMDNode *NMD = M.getOrInsertNamedMetadata(Name); - NMD->addOperand(n); - NMD->addOperand(n2); - - std::string Str; - raw_string_ostream oss(Str); - NMD->print(oss); - EXPECT_STREQ("!llvm.NMD1 = !{!0, !1}\n", - oss.str().c_str()); -} -} diff --git a/unittests/VMCore/PassManagerTest.cpp b/unittests/VMCore/PassManagerTest.cpp deleted file mode 100644 index 8cb7b24f27..0000000000 --- a/unittests/VMCore/PassManagerTest.cpp +++ /dev/null @@ -1,552 +0,0 @@ -//===- llvm/unittest/VMCore/PassManager.cpp - Constants 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/PassManager.h" -#include "llvm/ADT/SmallVector.h" -#include "llvm/Analysis/CallGraphSCCPass.h" -#include "llvm/Analysis/LoopInfo.h" -#include "llvm/Analysis/LoopPass.h" -#include "llvm/Analysis/Verifier.h" -#include "llvm/Assembly/PrintModulePass.h" -#include "llvm/IR/BasicBlock.h" -#include "llvm/IR/CallingConv.h" -#include "llvm/IR/Constants.h" -#include "llvm/IR/DataLayout.h" -#include "llvm/IR/DerivedTypes.h" -#include "llvm/IR/Function.h" -#include "llvm/IR/GlobalVariable.h" -#include "llvm/IR/InlineAsm.h" -#include "llvm/IR/Instructions.h" -#include "llvm/IR/LLVMContext.h" -#include "llvm/IR/Module.h" -#include "llvm/Pass.h" -#include "llvm/Support/MathExtras.h" -#include "llvm/Support/raw_ostream.h" -#include "gtest/gtest.h" - -using namespace llvm; - -namespace llvm { - void initializeModuleNDMPass(PassRegistry&); - void initializeFPassPass(PassRegistry&); - void initializeCGPassPass(PassRegistry&); - void initializeLPassPass(PassRegistry&); - void initializeBPassPass(PassRegistry&); - - namespace { - // ND = no deps - // NM = no modifications - struct ModuleNDNM: public ModulePass { - public: - static char run; - static char ID; - ModuleNDNM() : ModulePass(ID) { } - virtual bool runOnModule(Module &M) { - run++; - return false; - } - virtual void getAnalysisUsage(AnalysisUsage &AU) const { - AU.setPreservesAll(); - } - }; - char ModuleNDNM::ID=0; - char ModuleNDNM::run=0; - - struct ModuleNDM : public ModulePass { - public: - static char run; - static char ID; - ModuleNDM() : ModulePass(ID) {} - virtual bool runOnModule(Module &M) { - run++; - return true; - } - }; - char ModuleNDM::ID=0; - char ModuleNDM::run=0; - - struct ModuleNDM2 : public ModulePass { - public: - static char run; - static char ID; - ModuleNDM2() : ModulePass(ID) {} - virtual bool runOnModule(Module &M) { - run++; - return true; - } - }; - char ModuleNDM2::ID=0; - char ModuleNDM2::run=0; - - struct ModuleDNM : public ModulePass { - public: - static char run; - static char ID; - ModuleDNM() : ModulePass(ID) { - initializeModuleNDMPass(*PassRegistry::getPassRegistry()); - } - virtual bool runOnModule(Module &M) { - EXPECT_TRUE(getAnalysisIfAvailable()); - run++; - return false; - } - virtual void getAnalysisUsage(AnalysisUsage &AU) const { - AU.addRequired(); - AU.setPreservesAll(); - } - }; - char ModuleDNM::ID=0; - char ModuleDNM::run=0; - - template - struct PassTestBase : public P { - protected: - static int runc; - static bool initialized; - static bool finalized; - int allocated; - void run() { - EXPECT_TRUE(initialized); - EXPECT_FALSE(finalized); - EXPECT_EQ(0, allocated); - allocated++; - runc++; - } - public: - static char ID; - static void finishedOK(int run) { - EXPECT_GT(runc, 0); - EXPECT_TRUE(initialized); - EXPECT_TRUE(finalized); - EXPECT_EQ(run, runc); - } - PassTestBase() : P(ID), allocated(0) { - initialized = false; - finalized = false; - runc = 0; - } - - virtual void releaseMemory() { - EXPECT_GT(runc, 0); - EXPECT_GT(allocated, 0); - allocated--; - } - }; - template char PassTestBase

::ID; - template int PassTestBase

::runc; - template bool PassTestBase

::initialized; - template bool PassTestBase

::finalized; - - template - struct PassTest : public PassTestBase

{ - public: -#ifndef _MSC_VER // MSVC complains that Pass is not base class. - using llvm::Pass::doInitialization; - using llvm::Pass::doFinalization; -#endif - virtual bool doInitialization(T &t) { - EXPECT_FALSE(PassTestBase

::initialized); - PassTestBase

::initialized = true; - return false; - } - virtual bool doFinalization(T &t) { - EXPECT_FALSE(PassTestBase

::finalized); - PassTestBase

::finalized = true; - EXPECT_EQ(0, PassTestBase

::allocated); - return false; - } - }; - - struct CGPass : public PassTest { - public: - CGPass() { - initializeCGPassPass(*PassRegistry::getPassRegistry()); - } - virtual bool runOnSCC(CallGraphSCC &SCMM) { - EXPECT_TRUE(getAnalysisIfAvailable()); - run(); - return false; - } - }; - - struct FPass : public PassTest { - public: - virtual bool runOnFunction(Function &F) { - // FIXME: PR4112 - // EXPECT_TRUE(getAnalysisIfAvailable()); - run(); - return false; - } - }; - - struct LPass : public PassTestBase { - private: - static int initcount; - static int fincount; - public: - LPass() { - initializeLPassPass(*PassRegistry::getPassRegistry()); - initcount = 0; fincount=0; - EXPECT_FALSE(initialized); - } - static void finishedOK(int run, int finalized) { - PassTestBase::finishedOK(run); - EXPECT_EQ(run, initcount); - EXPECT_EQ(finalized, fincount); - } - using llvm::Pass::doInitialization; - using llvm::Pass::doFinalization; - virtual bool doInitialization(Loop* L, LPPassManager &LPM) { - initialized = true; - initcount++; - return false; - } - virtual bool runOnLoop(Loop *L, LPPassManager &LPM) { - EXPECT_TRUE(getAnalysisIfAvailable()); - run(); - return false; - } - virtual bool doFinalization() { - fincount++; - finalized = true; - return false; - } - }; - int LPass::initcount=0; - int LPass::fincount=0; - - struct BPass : public PassTestBase { - private: - static int inited; - static int fin; - public: - static void finishedOK(int run, int N) { - PassTestBase::finishedOK(run); - EXPECT_EQ(inited, N); - EXPECT_EQ(fin, N); - } - BPass() { - inited = 0; - fin = 0; - } - virtual bool doInitialization(Module &M) { - EXPECT_FALSE(initialized); - initialized = true; - return false; - } - virtual bool doInitialization(Function &F) { - inited++; - return false; - } - virtual bool runOnBasicBlock(BasicBlock &BB) { - EXPECT_TRUE(getAnalysisIfAvailable()); - run(); - return false; - } - virtual bool doFinalization(Function &F) { - fin++; - return false; - } - virtual bool doFinalization(Module &M) { - EXPECT_FALSE(finalized); - finalized = true; - EXPECT_EQ(0, allocated); - return false; - } - }; - int BPass::inited=0; - int BPass::fin=0; - - struct OnTheFlyTest: public ModulePass { - public: - static char ID; - OnTheFlyTest() : ModulePass(ID) { - initializeFPassPass(*PassRegistry::getPassRegistry()); - } - virtual bool runOnModule(Module &M) { - EXPECT_TRUE(getAnalysisIfAvailable()); - for (Module::iterator I=M.begin(),E=M.end(); I != E; ++I) { - Function &F = *I; - { - SCOPED_TRACE("Running on the fly function pass"); - getAnalysis(F); - } - } - return false; - } - virtual void getAnalysisUsage(AnalysisUsage &AU) const { - AU.addRequired(); - } - }; - char OnTheFlyTest::ID=0; - - TEST(PassManager, RunOnce) { - Module M("test-once", getGlobalContext()); - struct ModuleNDNM *mNDNM = new ModuleNDNM(); - struct ModuleDNM *mDNM = new ModuleDNM(); - struct ModuleNDM *mNDM = new ModuleNDM(); - struct ModuleNDM2 *mNDM2 = new ModuleNDM2(); - - mNDM->run = mNDNM->run = mDNM->run = mNDM2->run = 0; - - PassManager Passes; - Passes.add(new DataLayout(&M)); - Passes.add(mNDM2); - Passes.add(mNDM); - Passes.add(mNDNM); - Passes.add(mDNM); - - Passes.run(M); - // each pass must be run exactly once, since nothing invalidates them - EXPECT_EQ(1, mNDM->run); - EXPECT_EQ(1, mNDNM->run); - EXPECT_EQ(1, mDNM->run); - EXPECT_EQ(1, mNDM2->run); - } - - TEST(PassManager, ReRun) { - Module M("test-rerun", getGlobalContext()); - struct ModuleNDNM *mNDNM = new ModuleNDNM(); - struct ModuleDNM *mDNM = new ModuleDNM(); - struct ModuleNDM *mNDM = new ModuleNDM(); - struct ModuleNDM2 *mNDM2 = new ModuleNDM2(); - - mNDM->run = mNDNM->run = mDNM->run = mNDM2->run = 0; - - PassManager Passes; - Passes.add(new DataLayout(&M)); - Passes.add(mNDM); - Passes.add(mNDNM); - Passes.add(mNDM2);// invalidates mNDM needed by mDNM - Passes.add(mDNM); - - Passes.run(M); - // Some passes must be rerun because a pass that modified the - // module/function was run in between - EXPECT_EQ(2, mNDM->run); - EXPECT_EQ(1, mNDNM->run); - EXPECT_EQ(1, mNDM2->run); - EXPECT_EQ(1, mDNM->run); - } - - Module* makeLLVMModule(); - - template - void MemoryTestHelper(int run) { - OwningPtr M(makeLLVMModule()); - T *P = new T(); - PassManager Passes; - Passes.add(new DataLayout(M.get())); - Passes.add(P); - Passes.run(*M); - T::finishedOK(run); - } - - template - void MemoryTestHelper(int run, int N) { - Module *M = makeLLVMModule(); - T *P = new T(); - PassManager Passes; - Passes.add(new DataLayout(M)); - Passes.add(P); - Passes.run(*M); - T::finishedOK(run, N); - delete M; - } - - TEST(PassManager, Memory) { - // SCC#1: test1->test2->test3->test1 - // SCC#2: test4 - // SCC#3: indirect call node - { - SCOPED_TRACE("Callgraph pass"); - MemoryTestHelper(3); - } - - { - SCOPED_TRACE("Function pass"); - MemoryTestHelper(4);// 4 functions - } - - { - SCOPED_TRACE("Loop pass"); - MemoryTestHelper(2, 1); //2 loops, 1 function - } - { - SCOPED_TRACE("Basic block pass"); - MemoryTestHelper(7, 4); //9 basic blocks - } - - } - - TEST(PassManager, MemoryOnTheFly) { - Module *M = makeLLVMModule(); - { - SCOPED_TRACE("Running OnTheFlyTest"); - struct OnTheFlyTest *O = new OnTheFlyTest(); - PassManager Passes; - Passes.add(new DataLayout(M)); - Passes.add(O); - Passes.run(*M); - - FPass::finishedOK(4); - } - delete M; - } - - Module* makeLLVMModule() { - // Module Construction - Module* mod = new Module("test-mem", getGlobalContext()); - mod->setDataLayout("e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-" - "i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-" - "a0:0:64-s0:64:64-f80:128:128"); - mod->setTargetTriple("x86_64-unknown-linux-gnu"); - - // Type Definitions - std::vectorFuncTy_0_args; - FunctionType* FuncTy_0 = FunctionType::get( - /*Result=*/IntegerType::get(getGlobalContext(), 32), - /*Params=*/FuncTy_0_args, - /*isVarArg=*/false); - - std::vectorFuncTy_2_args; - FuncTy_2_args.push_back(IntegerType::get(getGlobalContext(), 1)); - FunctionType* FuncTy_2 = FunctionType::get( - /*Result=*/Type::getVoidTy(getGlobalContext()), - /*Params=*/FuncTy_2_args, - /*isVarArg=*/false); - - - // Function Declarations - - Function* func_test1 = Function::Create( - /*Type=*/FuncTy_0, - /*Linkage=*/GlobalValue::ExternalLinkage, - /*Name=*/"test1", mod); - func_test1->setCallingConv(CallingConv::C); - AttributeSet func_test1_PAL; - func_test1->setAttributes(func_test1_PAL); - - Function* func_test2 = Function::Create( - /*Type=*/FuncTy_0, - /*Linkage=*/GlobalValue::ExternalLinkage, - /*Name=*/"test2", mod); - func_test2->setCallingConv(CallingConv::C); - AttributeSet func_test2_PAL; - func_test2->setAttributes(func_test2_PAL); - - Function* func_test3 = Function::Create( - /*Type=*/FuncTy_0, - /*Linkage=*/GlobalValue::ExternalLinkage, - /*Name=*/"test3", mod); - func_test3->setCallingConv(CallingConv::C); - AttributeSet func_test3_PAL; - func_test3->setAttributes(func_test3_PAL); - - Function* func_test4 = Function::Create( - /*Type=*/FuncTy_2, - /*Linkage=*/GlobalValue::ExternalLinkage, - /*Name=*/"test4", mod); - func_test4->setCallingConv(CallingConv::C); - AttributeSet func_test4_PAL; - func_test4->setAttributes(func_test4_PAL); - - // Global Variable Declarations - - - // Constant Definitions - - // Global Variable Definitions - - // Function Definitions - - // Function: test1 (func_test1) - { - - BasicBlock* label_entry = BasicBlock::Create(getGlobalContext(), "entry",func_test1,0); - - // Block entry (label_entry) - CallInst* int32_3 = CallInst::Create(func_test2, "", label_entry); - int32_3->setCallingConv(CallingConv::C); - int32_3->setTailCall(false);AttributeSet int32_3_PAL; - int32_3->setAttributes(int32_3_PAL); - - ReturnInst::Create(getGlobalContext(), int32_3, label_entry); - - } - - // Function: test2 (func_test2) - { - - BasicBlock* label_entry_5 = BasicBlock::Create(getGlobalContext(), "entry",func_test2,0); - - // Block entry (label_entry_5) - CallInst* int32_6 = CallInst::Create(func_test3, "", label_entry_5); - int32_6->setCallingConv(CallingConv::C); - int32_6->setTailCall(false);AttributeSet int32_6_PAL; - int32_6->setAttributes(int32_6_PAL); - - ReturnInst::Create(getGlobalContext(), int32_6, label_entry_5); - - } - - // Function: test3 (func_test3) - { - - BasicBlock* label_entry_8 = BasicBlock::Create(getGlobalContext(), "entry",func_test3,0); - - // Block entry (label_entry_8) - CallInst* int32_9 = CallInst::Create(func_test1, "", label_entry_8); - int32_9->setCallingConv(CallingConv::C); - int32_9->setTailCall(false);AttributeSet int32_9_PAL; - int32_9->setAttributes(int32_9_PAL); - - ReturnInst::Create(getGlobalContext(), int32_9, label_entry_8); - - } - - // Function: test4 (func_test4) - { - Function::arg_iterator args = func_test4->arg_begin(); - Value* int1_f = args++; - int1_f->setName("f"); - - BasicBlock* label_entry_11 = BasicBlock::Create(getGlobalContext(), "entry",func_test4,0); - BasicBlock* label_bb = BasicBlock::Create(getGlobalContext(), "bb",func_test4,0); - BasicBlock* label_bb1 = BasicBlock::Create(getGlobalContext(), "bb1",func_test4,0); - BasicBlock* label_return = BasicBlock::Create(getGlobalContext(), "return",func_test4,0); - - // Block entry (label_entry_11) - BranchInst::Create(label_bb, label_entry_11); - - // Block bb (label_bb) - BranchInst::Create(label_bb, label_bb1, int1_f, label_bb); - - // Block bb1 (label_bb1) - BranchInst::Create(label_bb1, label_return, int1_f, label_bb1); - - // Block return (label_return) - ReturnInst::Create(getGlobalContext(), label_return); - - } - return mod; - } - - } -} - -INITIALIZE_PASS(ModuleNDM, "mndm", "mndm", false, false) -INITIALIZE_PASS_BEGIN(CGPass, "cgp","cgp", false, false) -INITIALIZE_AG_DEPENDENCY(CallGraph) -INITIALIZE_PASS_END(CGPass, "cgp","cgp", false, false) -INITIALIZE_PASS(FPass, "fp","fp", false, false) -INITIALIZE_PASS_BEGIN(LPass, "lp","lp", false, false) -INITIALIZE_PASS_DEPENDENCY(LoopInfo) -INITIALIZE_PASS_END(LPass, "lp","lp", false, false) -INITIALIZE_PASS(BPass, "bp","bp", false, false) diff --git a/unittests/VMCore/TypeBuilderTest.cpp b/unittests/VMCore/TypeBuilderTest.cpp deleted file mode 100644 index be493cdc63..0000000000 --- a/unittests/VMCore/TypeBuilderTest.cpp +++ /dev/null @@ -1,253 +0,0 @@ -//===- llvm/unittest/TypeBuilderTest.cpp - TypeBuilder tests --------------===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// - -#include "llvm/IR/TypeBuilder.h" -#include "llvm/ADT/ArrayRef.h" -#include "llvm/IR/LLVMContext.h" -#include "gtest/gtest.h" - -using namespace llvm; - -namespace { - -TEST(TypeBuilderTest, Void) { - EXPECT_EQ(Type::getVoidTy(getGlobalContext()), (TypeBuilder::get(getGlobalContext()))); - EXPECT_EQ(Type::getVoidTy(getGlobalContext()), (TypeBuilder::get(getGlobalContext()))); - // Special cases for C compatibility: - EXPECT_EQ(Type::getInt8PtrTy(getGlobalContext()), - (TypeBuilder::get(getGlobalContext()))); - EXPECT_EQ(Type::getInt8PtrTy(getGlobalContext()), - (TypeBuilder::get(getGlobalContext()))); - EXPECT_EQ(Type::getInt8PtrTy(getGlobalContext()), - (TypeBuilder::get(getGlobalContext()))); - EXPECT_EQ(Type::getInt8PtrTy(getGlobalContext()), - (TypeBuilder::get( - getGlobalContext()))); -} - -TEST(TypeBuilderTest, HostIntegers) { - EXPECT_EQ(Type::getInt8Ty(getGlobalContext()), (TypeBuilder::get(getGlobalContext()))); - EXPECT_EQ(Type::getInt8Ty(getGlobalContext()), (TypeBuilder::get(getGlobalContext()))); - EXPECT_EQ(Type::getInt16Ty(getGlobalContext()), (TypeBuilder::get(getGlobalContext()))); - EXPECT_EQ(Type::getInt16Ty(getGlobalContext()), (TypeBuilder::get(getGlobalContext()))); - EXPECT_EQ(Type::getInt32Ty(getGlobalContext()), (TypeBuilder::get(getGlobalContext()))); - EXPECT_EQ(Type::getInt32Ty(getGlobalContext()), (TypeBuilder::get(getGlobalContext()))); - EXPECT_EQ(Type::getInt64Ty(getGlobalContext()), (TypeBuilder::get(getGlobalContext()))); - EXPECT_EQ(Type::getInt64Ty(getGlobalContext()), (TypeBuilder::get(getGlobalContext()))); - - EXPECT_EQ(IntegerType::get(getGlobalContext(), sizeof(size_t) * CHAR_BIT), - (TypeBuilder::get(getGlobalContext()))); - EXPECT_EQ(IntegerType::get(getGlobalContext(), sizeof(ptrdiff_t) * CHAR_BIT), - (TypeBuilder::get(getGlobalContext()))); -} - -TEST(TypeBuilderTest, CrossCompilableIntegers) { - EXPECT_EQ(IntegerType::get(getGlobalContext(), 1), (TypeBuilder, true>::get(getGlobalContext()))); - EXPECT_EQ(IntegerType::get(getGlobalContext(), 1), (TypeBuilder, false>::get(getGlobalContext()))); - EXPECT_EQ(IntegerType::get(getGlobalContext(), 72), (TypeBuilder, true>::get(getGlobalContext()))); - EXPECT_EQ(IntegerType::get(getGlobalContext(), 72), (TypeBuilder, false>::get(getGlobalContext()))); -} - -TEST(TypeBuilderTest, Float) { - EXPECT_EQ(Type::getFloatTy(getGlobalContext()), (TypeBuilder::get(getGlobalContext()))); - EXPECT_EQ(Type::getDoubleTy(getGlobalContext()), (TypeBuilder::get(getGlobalContext()))); - // long double isn't supported yet. - EXPECT_EQ(Type::getFloatTy(getGlobalContext()), (TypeBuilder::get(getGlobalContext()))); - EXPECT_EQ(Type::getFloatTy(getGlobalContext()), (TypeBuilder::get(getGlobalContext()))); - EXPECT_EQ(Type::getDoubleTy(getGlobalContext()), (TypeBuilder::get(getGlobalContext()))); - EXPECT_EQ(Type::getDoubleTy(getGlobalContext()), (TypeBuilder::get(getGlobalContext()))); - EXPECT_EQ(Type::getX86_FP80Ty(getGlobalContext()), (TypeBuilder::get(getGlobalContext()))); - EXPECT_EQ(Type::getX86_FP80Ty(getGlobalContext()), (TypeBuilder::get(getGlobalContext()))); - EXPECT_EQ(Type::getFP128Ty(getGlobalContext()), (TypeBuilder::get(getGlobalContext()))); - EXPECT_EQ(Type::getFP128Ty(getGlobalContext()), (TypeBuilder::get(getGlobalContext()))); - EXPECT_EQ(Type::getPPC_FP128Ty(getGlobalContext()), (TypeBuilder::get(getGlobalContext()))); - EXPECT_EQ(Type::getPPC_FP128Ty(getGlobalContext()), (TypeBuilder::get(getGlobalContext()))); -} - -TEST(TypeBuilderTest, Derived) { - EXPECT_EQ(PointerType::getUnqual(Type::getInt8PtrTy(getGlobalContext())), - (TypeBuilder::get(getGlobalContext()))); - EXPECT_EQ(ArrayType::get(Type::getInt8Ty(getGlobalContext()), 7), - (TypeBuilder::get(getGlobalContext()))); - EXPECT_EQ(ArrayType::get(Type::getInt8Ty(getGlobalContext()), 0), - (TypeBuilder::get(getGlobalContext()))); - - EXPECT_EQ(PointerType::getUnqual(Type::getInt8PtrTy(getGlobalContext())), - (TypeBuilder**, false>::get(getGlobalContext()))); - EXPECT_EQ(ArrayType::get(Type::getInt8Ty(getGlobalContext()), 7), - (TypeBuilder[7], false>::get(getGlobalContext()))); - EXPECT_EQ(ArrayType::get(Type::getInt8Ty(getGlobalContext()), 0), - (TypeBuilder[], false>::get(getGlobalContext()))); - - EXPECT_EQ(PointerType::getUnqual(Type::getInt8PtrTy(getGlobalContext())), - (TypeBuilder**, true>::get(getGlobalContext()))); - EXPECT_EQ(ArrayType::get(Type::getInt8Ty(getGlobalContext()), 7), - (TypeBuilder[7], true>::get(getGlobalContext()))); - EXPECT_EQ(ArrayType::get(Type::getInt8Ty(getGlobalContext()), 0), - (TypeBuilder[], true>::get(getGlobalContext()))); - - - EXPECT_EQ(Type::getInt8Ty(getGlobalContext()), - (TypeBuilder::get(getGlobalContext()))); - EXPECT_EQ(Type::getInt8Ty(getGlobalContext()), - (TypeBuilder::get(getGlobalContext()))); - EXPECT_EQ(Type::getInt8Ty(getGlobalContext()), - (TypeBuilder::get(getGlobalContext()))); - - EXPECT_EQ(Type::getInt8Ty(getGlobalContext()), - (TypeBuilder, false>::get(getGlobalContext()))); - EXPECT_EQ(Type::getInt8Ty(getGlobalContext()), - (TypeBuilder, false>::get(getGlobalContext()))); - EXPECT_EQ(Type::getInt8Ty(getGlobalContext()), - (TypeBuilder, false>::get(getGlobalContext()))); - - EXPECT_EQ(Type::getInt8Ty(getGlobalContext()), - (TypeBuilder, true>::get(getGlobalContext()))); - EXPECT_EQ(Type::getInt8Ty(getGlobalContext()), - (TypeBuilder, true>::get(getGlobalContext()))); - EXPECT_EQ(Type::getInt8Ty(getGlobalContext()), - (TypeBuilder, true>::get(getGlobalContext()))); - - EXPECT_EQ(Type::getInt8PtrTy(getGlobalContext()), - (TypeBuilder::get(getGlobalContext()))); -} - -TEST(TypeBuilderTest, Functions) { - std::vector params; - EXPECT_EQ(FunctionType::get(Type::getVoidTy(getGlobalContext()), params, false), - (TypeBuilder::get(getGlobalContext()))); - EXPECT_EQ(FunctionType::get(Type::getInt8Ty(getGlobalContext()), params, true), - (TypeBuilder::get(getGlobalContext()))); - params.push_back(TypeBuilder::get(getGlobalContext())); - EXPECT_EQ(FunctionType::get(Type::getInt8Ty(getGlobalContext()), params, false), - (TypeBuilder::get(getGlobalContext()))); - EXPECT_EQ(FunctionType::get(Type::getInt8Ty(getGlobalContext()), params, true), - (TypeBuilder::get(getGlobalContext()))); - params.push_back(TypeBuilder::get(getGlobalContext())); - EXPECT_EQ(FunctionType::get(Type::getInt8Ty(getGlobalContext()), params, false), - (TypeBuilder::get(getGlobalContext()))); - EXPECT_EQ(FunctionType::get(Type::getInt8Ty(getGlobalContext()), params, true), - (TypeBuilder::get(getGlobalContext()))); - params.push_back(TypeBuilder::get(getGlobalContext())); - EXPECT_EQ(FunctionType::get(Type::getInt8Ty(getGlobalContext()), params, false), - (TypeBuilder::get(getGlobalContext()))); - EXPECT_EQ(FunctionType::get(Type::getInt8Ty(getGlobalContext()), params, true), - (TypeBuilder::get(getGlobalContext()))); - params.push_back(TypeBuilder::get(getGlobalContext())); - EXPECT_EQ(FunctionType::get(Type::getInt8Ty(getGlobalContext()), params, false), - (TypeBuilder::get(getGlobalContext()))); - EXPECT_EQ(FunctionType::get(Type::getInt8Ty(getGlobalContext()), params, true), - (TypeBuilder::get(getGlobalContext()))); - params.push_back(TypeBuilder::get(getGlobalContext())); - EXPECT_EQ(FunctionType::get(Type::getInt8Ty(getGlobalContext()), params, false), - (TypeBuilder::get(getGlobalContext()))); - EXPECT_EQ(FunctionType::get(Type::getInt8Ty(getGlobalContext()), params, true), - (TypeBuilder::get(getGlobalContext()))); -} - -TEST(TypeBuilderTest, Context) { - // We used to cache TypeBuilder results in static local variables. This - // produced the same type for different contexts, which of course broke - // things. - LLVMContext context1; - EXPECT_EQ(&context1, - &(TypeBuilder, true>::get(context1))->getContext()); - LLVMContext context2; - EXPECT_EQ(&context2, - &(TypeBuilder, true>::get(context2))->getContext()); -} - -struct MyType { - int a; - int *b; - void *array[1]; -}; - -struct MyPortableType { - int32_t a; - int32_t *b; - void *array[1]; -}; - -} // anonymous namespace - -namespace llvm { -template class TypeBuilder { -public: - static StructType *get(LLVMContext &Context) { - // Using the static result variable ensures that the type is - // only looked up once. - std::vector st; - st.push_back(TypeBuilder::get(Context)); - st.push_back(TypeBuilder::get(Context)); - st.push_back(TypeBuilder::get(Context)); - static StructType *const result = StructType::get(Context, st); - return result; - } - - // You may find this a convenient place to put some constants - // to help with getelementptr. They don't have any effect on - // the operation of TypeBuilder. - enum Fields { - FIELD_A, - FIELD_B, - FIELD_ARRAY - }; -}; - -template class TypeBuilder { -public: - static StructType *get(LLVMContext &Context) { - // Using the static result variable ensures that the type is - // only looked up once. - std::vector st; - st.push_back(TypeBuilder, cross>::get(Context)); - st.push_back(TypeBuilder*, cross>::get(Context)); - st.push_back(TypeBuilder*[], cross>::get(Context)); - static StructType *const result = StructType::get(Context, st); - return result; - } - - // You may find this a convenient place to put some constants - // to help with getelementptr. They don't have any effect on - // the operation of TypeBuilder. - enum Fields { - FIELD_A, - FIELD_B, - FIELD_ARRAY - }; -}; -} // namespace llvm -namespace { - -TEST(TypeBuilderTest, Extensions) { - EXPECT_EQ(PointerType::getUnqual(StructType::get( - TypeBuilder::get(getGlobalContext()), - TypeBuilder::get(getGlobalContext()), - TypeBuilder::get(getGlobalContext()), - (void*)0)), - (TypeBuilder::get(getGlobalContext()))); - EXPECT_EQ(PointerType::getUnqual(StructType::get( - TypeBuilder, false>::get(getGlobalContext()), - TypeBuilder*, false>::get(getGlobalContext()), - TypeBuilder*[], false>::get(getGlobalContext()), - (void*)0)), - (TypeBuilder::get(getGlobalContext()))); - EXPECT_EQ(PointerType::getUnqual(StructType::get( - TypeBuilder, false>::get(getGlobalContext()), - TypeBuilder*, false>::get(getGlobalContext()), - TypeBuilder*[], false>::get(getGlobalContext()), - (void*)0)), - (TypeBuilder::get(getGlobalContext()))); -} - -} // anonymous namespace diff --git a/unittests/VMCore/TypesTest.cpp b/unittests/VMCore/TypesTest.cpp deleted file mode 100644 index fe0935341c..0000000000 --- a/unittests/VMCore/TypesTest.cpp +++ /dev/null @@ -1,30 +0,0 @@ -//===- llvm/unittest/VMCore/TypesTest.cpp - Type 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/IR/DerivedTypes.h" -#include "llvm/IR/LLVMContext.h" -#include "gtest/gtest.h" -using namespace llvm; - -namespace { - -TEST(TypesTest, StructType) { - LLVMContext C; - - // PR13522 - StructType *Struct = StructType::create(C, "FooBar"); - EXPECT_EQ("FooBar", Struct->getName()); - Struct->setName(Struct->getName().substr(0, 3)); - EXPECT_EQ("Foo", Struct->getName()); - Struct->setName(""); - EXPECT_TRUE(Struct->getName().empty()); - EXPECT_FALSE(Struct->hasName()); -} - -} // end anonymous namespace diff --git a/unittests/VMCore/ValueMapTest.cpp b/unittests/VMCore/ValueMapTest.cpp deleted file mode 100644 index 5aaf905836..0000000000 --- a/unittests/VMCore/ValueMapTest.cpp +++ /dev/null @@ -1,294 +0,0 @@ -//===- llvm/unittest/ADT/ValueMapTest.cpp - ValueMap unit tests -*- C++ -*-===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// - -#include "llvm/ADT/ValueMap.h" -#include "llvm/ADT/OwningPtr.h" -#include "llvm/Config/llvm-config.h" -#include "llvm/IR/Constants.h" -#include "llvm/IR/Instructions.h" -#include "llvm/IR/LLVMContext.h" -#include "gtest/gtest.h" - -using namespace llvm; - -namespace { - -// Test fixture -template -class ValueMapTest : public testing::Test { -protected: - Constant *ConstantV; - OwningPtr BitcastV; - OwningPtr AddV; - - ValueMapTest() : - ConstantV(ConstantInt::get(Type::getInt32Ty(getGlobalContext()), 0)), - BitcastV(new BitCastInst(ConstantV, Type::getInt32Ty(getGlobalContext()))), - AddV(BinaryOperator::CreateAdd(ConstantV, ConstantV)) { - } -}; - -// Run everything on Value*, a subtype to make sure that casting works as -// expected, and a const subtype to make sure we cast const correctly. -typedef ::testing::Types KeyTypes; -TYPED_TEST_CASE(ValueMapTest, KeyTypes); - -TYPED_TEST(ValueMapTest, Null) { - ValueMap VM1; - VM1[NULL] = 7; - EXPECT_EQ(7, VM1.lookup(NULL)); -} - -TYPED_TEST(ValueMapTest, FollowsValue) { - ValueMap VM; - VM[this->BitcastV.get()] = 7; - EXPECT_EQ(7, VM.lookup(this->BitcastV.get())); - EXPECT_EQ(0, VM.count(this->AddV.get())); - this->BitcastV->replaceAllUsesWith(this->AddV.get()); - EXPECT_EQ(7, VM.lookup(this->AddV.get())); - EXPECT_EQ(0, VM.count(this->BitcastV.get())); - this->AddV.reset(); - EXPECT_EQ(0, VM.count(this->AddV.get())); - EXPECT_EQ(0, VM.count(this->BitcastV.get())); - EXPECT_EQ(0U, VM.size()); -} - -TYPED_TEST(ValueMapTest, OperationsWork) { - ValueMap VM; - ValueMap VM2(16); (void)VM2; - typename ValueMapConfig::ExtraData Data; - ValueMap VM3(Data, 16); (void)VM3; - EXPECT_TRUE(VM.empty()); - - VM[this->BitcastV.get()] = 7; - - // Find: - typename ValueMap::iterator I = - VM.find(this->BitcastV.get()); - ASSERT_TRUE(I != VM.end()); - EXPECT_EQ(this->BitcastV.get(), I->first); - EXPECT_EQ(7, I->second); - EXPECT_TRUE(VM.find(this->AddV.get()) == VM.end()); - - // Const find: - const ValueMap &CVM = VM; - typename ValueMap::const_iterator CI = - CVM.find(this->BitcastV.get()); - ASSERT_TRUE(CI != CVM.end()); - EXPECT_EQ(this->BitcastV.get(), CI->first); - EXPECT_EQ(7, CI->second); - EXPECT_TRUE(CVM.find(this->AddV.get()) == CVM.end()); - - // Insert: - std::pair::iterator, bool> InsertResult1 = - VM.insert(std::make_pair(this->AddV.get(), 3)); - EXPECT_EQ(this->AddV.get(), InsertResult1.first->first); - EXPECT_EQ(3, InsertResult1.first->second); - EXPECT_TRUE(InsertResult1.second); - EXPECT_EQ(true, VM.count(this->AddV.get())); - std::pair::iterator, bool> InsertResult2 = - VM.insert(std::make_pair(this->AddV.get(), 5)); - EXPECT_EQ(this->AddV.get(), InsertResult2.first->first); - EXPECT_EQ(3, InsertResult2.first->second); - EXPECT_FALSE(InsertResult2.second); - - // Erase: - VM.erase(InsertResult2.first); - EXPECT_EQ(0U, VM.count(this->AddV.get())); - EXPECT_EQ(1U, VM.count(this->BitcastV.get())); - VM.erase(this->BitcastV.get()); - EXPECT_EQ(0U, VM.count(this->BitcastV.get())); - EXPECT_EQ(0U, VM.size()); - - // Range insert: - SmallVector, 2> Elems; - Elems.push_back(std::make_pair(this->AddV.get(), 1)); - Elems.push_back(std::make_pair(this->BitcastV.get(), 2)); - VM.insert(Elems.begin(), Elems.end()); - EXPECT_EQ(1, VM.lookup(this->AddV.get())); - EXPECT_EQ(2, VM.lookup(this->BitcastV.get())); -} - -template -void CompileAssertHasType(VarType) { - typedef char assert[is_same::value ? 1 : -1]; -} - -TYPED_TEST(ValueMapTest, Iteration) { - ValueMap VM; - VM[this->BitcastV.get()] = 2; - VM[this->AddV.get()] = 3; - size_t size = 0; - for (typename ValueMap::iterator I = VM.begin(), E = VM.end(); - I != E; ++I) { - ++size; - std::pair value = *I; (void)value; - CompileAssertHasType(I->first); - if (I->second == 2) { - EXPECT_EQ(this->BitcastV.get(), I->first); - I->second = 5; - } else if (I->second == 3) { - EXPECT_EQ(this->AddV.get(), I->first); - I->second = 6; - } else { - ADD_FAILURE() << "Iterated through an extra value."; - } - } - EXPECT_EQ(2U, size); - EXPECT_EQ(5, VM[this->BitcastV.get()]); - EXPECT_EQ(6, VM[this->AddV.get()]); - - size = 0; - // Cast to const ValueMap to avoid a bug in DenseMap's iterators. - const ValueMap& CVM = VM; - for (typename ValueMap::const_iterator I = CVM.begin(), - E = CVM.end(); I != E; ++I) { - ++size; - std::pair value = *I; (void)value; - CompileAssertHasType(I->first); - if (I->second == 5) { - EXPECT_EQ(this->BitcastV.get(), I->first); - } else if (I->second == 6) { - EXPECT_EQ(this->AddV.get(), I->first); - } else { - ADD_FAILURE() << "Iterated through an extra value."; - } - } - EXPECT_EQ(2U, size); -} - -TYPED_TEST(ValueMapTest, DefaultCollisionBehavior) { - // By default, we overwrite the old value with the replaced value. - ValueMap VM; - VM[this->BitcastV.get()] = 7; - VM[this->AddV.get()] = 9; - this->BitcastV->replaceAllUsesWith(this->AddV.get()); - EXPECT_EQ(0, VM.count(this->BitcastV.get())); - EXPECT_EQ(9, VM.lookup(this->AddV.get())); -} - -TYPED_TEST(ValueMapTest, ConfiguredCollisionBehavior) { - // TODO: Implement this when someone needs it. -} - -template -struct LockMutex : ValueMapConfig { - struct ExtraData { - sys::Mutex *M; - bool *CalledRAUW; - bool *CalledDeleted; - }; - static void onRAUW(const ExtraData &Data, KeyT Old, KeyT New) { - *Data.CalledRAUW = true; - EXPECT_FALSE(Data.M->tryacquire()) << "Mutex should already be locked."; - } - static void onDelete(const ExtraData &Data, KeyT Old) { - *Data.CalledDeleted = true; - EXPECT_FALSE(Data.M->tryacquire()) << "Mutex should already be locked."; - } - static sys::Mutex *getMutex(const ExtraData &Data) { return Data.M; } -}; -#if LLVM_ENABLE_THREADS -TYPED_TEST(ValueMapTest, LocksMutex) { - sys::Mutex M(false); // Not recursive. - bool CalledRAUW = false, CalledDeleted = false; - typename LockMutex::ExtraData Data = - {&M, &CalledRAUW, &CalledDeleted}; - ValueMap > VM(Data); - VM[this->BitcastV.get()] = 7; - this->BitcastV->replaceAllUsesWith(this->AddV.get()); - this->AddV.reset(); - EXPECT_TRUE(CalledRAUW); - EXPECT_TRUE(CalledDeleted); -} -#endif - -template -struct NoFollow : ValueMapConfig { - enum { FollowRAUW = false }; -}; - -TYPED_TEST(ValueMapTest, NoFollowRAUW) { - ValueMap > VM; - VM[this->BitcastV.get()] = 7; - EXPECT_EQ(7, VM.lookup(this->BitcastV.get())); - EXPECT_EQ(0, VM.count(this->AddV.get())); - this->BitcastV->replaceAllUsesWith(this->AddV.get()); - EXPECT_EQ(7, VM.lookup(this->BitcastV.get())); - EXPECT_EQ(0, VM.lookup(this->AddV.get())); - this->AddV.reset(); - EXPECT_EQ(7, VM.lookup(this->BitcastV.get())); - EXPECT_EQ(0, VM.lookup(this->AddV.get())); - this->BitcastV.reset(); - EXPECT_EQ(0, VM.lookup(this->BitcastV.get())); - EXPECT_EQ(0, VM.lookup(this->AddV.get())); - EXPECT_EQ(0U, VM.size()); -} - -template -struct CountOps : ValueMapConfig { - struct ExtraData { - int *Deletions; - int *RAUWs; - }; - - static void onRAUW(const ExtraData &Data, KeyT Old, KeyT New) { - ++*Data.RAUWs; - } - static void onDelete(const ExtraData &Data, KeyT Old) { - ++*Data.Deletions; - } -}; - -TYPED_TEST(ValueMapTest, CallsConfig) { - int Deletions = 0, RAUWs = 0; - typename CountOps::ExtraData Data = {&Deletions, &RAUWs}; - ValueMap > VM(Data); - VM[this->BitcastV.get()] = 7; - this->BitcastV->replaceAllUsesWith(this->AddV.get()); - EXPECT_EQ(0, Deletions); - EXPECT_EQ(1, RAUWs); - this->AddV.reset(); - EXPECT_EQ(1, Deletions); - EXPECT_EQ(1, RAUWs); - this->BitcastV.reset(); - EXPECT_EQ(1, Deletions); - EXPECT_EQ(1, RAUWs); -} - -template -struct ModifyingConfig : ValueMapConfig { - // We'll put a pointer here back to the ValueMap this key is in, so - // that we can modify it (and clobber *this) before the ValueMap - // tries to do the same modification. In previous versions of - // ValueMap, that exploded. - typedef ValueMap > **ExtraData; - - static void onRAUW(ExtraData Map, KeyT Old, KeyT New) { - (*Map)->erase(Old); - } - static void onDelete(ExtraData Map, KeyT Old) { - (*Map)->erase(Old); - } -}; -TYPED_TEST(ValueMapTest, SurvivesModificationByConfig) { - ValueMap > *MapAddress; - ValueMap > VM(&MapAddress); - MapAddress = &VM; - // Now the ModifyingConfig can modify the Map inside a callback. - VM[this->BitcastV.get()] = 7; - this->BitcastV->replaceAllUsesWith(this->AddV.get()); - EXPECT_FALSE(VM.count(this->BitcastV.get())); - EXPECT_FALSE(VM.count(this->AddV.get())); - VM[this->AddV.get()] = 7; - this->AddV.reset(); - EXPECT_FALSE(VM.count(this->AddV.get())); -} - -} diff --git a/unittests/VMCore/VerifierTest.cpp b/unittests/VMCore/VerifierTest.cpp deleted file mode 100644 index 674a785ad5..0000000000 --- a/unittests/VMCore/VerifierTest.cpp +++ /dev/null @@ -1,64 +0,0 @@ -//===- llvm/unittest/VMCore/VerifierTest.cpp - Verifier 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/Verifier.h" -#include "llvm/ADT/OwningPtr.h" -#include "llvm/IR/Constants.h" -#include "llvm/IR/DerivedTypes.h" -#include "llvm/IR/Function.h" -#include "llvm/IR/GlobalAlias.h" -#include "llvm/IR/GlobalVariable.h" -#include "llvm/IR/Instructions.h" -#include "llvm/IR/LLVMContext.h" -#include "llvm/IR/Module.h" -#include "gtest/gtest.h" - -namespace llvm { -namespace { - -TEST(VerifierTest, Branch_i1) { - LLVMContext &C = getGlobalContext(); - FunctionType *FTy = FunctionType::get(Type::getVoidTy(C), /*isVarArg=*/false); - OwningPtr F(Function::Create(FTy, GlobalValue::ExternalLinkage)); - BasicBlock *Entry = BasicBlock::Create(C, "entry", F.get()); - BasicBlock *Exit = BasicBlock::Create(C, "exit", F.get()); - ReturnInst::Create(C, Exit); - - // To avoid triggering an assertion in BranchInst::Create, we first create - // a branch with an 'i1' condition ... - - Constant *False = ConstantInt::getFalse(C); - BranchInst *BI = BranchInst::Create(Exit, Exit, False, Entry); - - // ... then use setOperand to redirect it to a value of different type. - - Constant *Zero32 = ConstantInt::get(IntegerType::get(C, 32), 0); - BI->setOperand(0, Zero32); - - EXPECT_TRUE(verifyFunction(*F, ReturnStatusAction)); -} - -TEST(VerifierTest, AliasUnnamedAddr) { - LLVMContext &C = getGlobalContext(); - Module M("M", C); - Type *Ty = Type::getInt8Ty(C); - Constant *Init = Constant::getNullValue(Ty); - GlobalVariable *Aliasee = new GlobalVariable(M, Ty, true, - GlobalValue::ExternalLinkage, - Init, "foo"); - GlobalAlias *GA = new GlobalAlias(Type::getInt8PtrTy(C), - GlobalValue::ExternalLinkage, - "bar", Aliasee, &M); - GA->setUnnamedAddr(true); - std::string Error; - EXPECT_TRUE(verifyModule(M, ReturnStatusAction, &Error)); - EXPECT_TRUE(StringRef(Error).startswith("Alias cannot have unnamed_addr")); -} -} -} diff --git a/unittests/VMCore/WaymarkTest.cpp b/unittests/VMCore/WaymarkTest.cpp deleted file mode 100644 index 0e766e1c05..0000000000 --- a/unittests/VMCore/WaymarkTest.cpp +++ /dev/null @@ -1,54 +0,0 @@ -//===- llvm/unittest/VMCore/WaymarkTest.cpp - getUser() unit tests --------===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// - -// we perform white-box tests -// -#include "llvm/IR/Function.h" -#include "llvm/IR/Instructions.h" -#include "llvm/IR/LLVMContext.h" -#include "gtest/gtest.h" -#include - -namespace llvm { -namespace { - -Constant *char2constant(char c) { - return ConstantInt::get(Type::getInt8Ty(getGlobalContext()), c); -} - - -TEST(WaymarkTest, NativeArray) { - static uint8_t tail[22] = "s02s33s30y2y0s1x0syxS"; - Value * values[22]; - std::transform(tail, tail + 22, values, char2constant); - FunctionType *FT = FunctionType::get(Type::getVoidTy(getGlobalContext()), true); - Function *F = Function::Create(FT, GlobalValue::ExternalLinkage); - const CallInst *A = CallInst::Create(F, makeArrayRef(values)); - ASSERT_NE(A, (const CallInst*)NULL); - ASSERT_EQ(1U + 22, A->getNumOperands()); - const Use *U = &A->getOperandUse(0); - const Use *Ue = &A->getOperandUse(22); - for (; U != Ue; ++U) - { - EXPECT_EQ(A, U->getUser()); - } -} - -TEST(WaymarkTest, TwoBit) { - Use* many = (Use*)calloc(sizeof(Use), 8212 + 1); - ASSERT_TRUE(many); - Use::initTags(many, many + 8212); - for (const Use *U = many, *Ue = many + 8212 - 1; U != Ue; ++U) - { - EXPECT_EQ((User*)(Ue + 1), U->getUser()); - } -} - -} // end anonymous namespace -} // end namespace llvm -- cgit v1.2.3