2013-01-07 23:35:46 +08:00
|
|
|
//===- llvm/unittest/IR/IRBuilderTest.cpp - IRBuilder tests ---------------===//
|
2011-05-22 07:14:36 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/IRBuilder.h"
|
|
|
|
#include "llvm/IR/BasicBlock.h"
|
|
|
|
#include "llvm/IR/DataLayout.h"
|
2015-01-22 02:32:56 +08:00
|
|
|
#include "llvm/IR/DIBuilder.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#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"
|
2014-03-04 20:05:47 +08:00
|
|
|
#include "llvm/IR/NoFolder.h"
|
2015-03-31 10:09:55 +08:00
|
|
|
#include "llvm/IR/Verifier.h"
|
2011-05-22 07:14:36 +08:00
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2011-12-20 10:50:00 +08:00
|
|
|
namespace {
|
2012-06-20 16:39:27 +08:00
|
|
|
|
2011-05-22 07:14:36 +08:00
|
|
|
class IRBuilderTest : public testing::Test {
|
|
|
|
protected:
|
2015-04-11 10:11:45 +08:00
|
|
|
void SetUp() override {
|
2013-11-15 17:34:33 +08:00
|
|
|
M.reset(new Module("MyModule", Ctx));
|
|
|
|
FunctionType *FTy = FunctionType::get(Type::getVoidTy(Ctx),
|
2011-05-22 07:14:36 +08:00
|
|
|
/*isVarArg=*/false);
|
2012-07-16 15:44:51 +08:00
|
|
|
F = Function::Create(FTy, Function::ExternalLinkage, "", M.get());
|
2013-11-15 17:34:33 +08:00
|
|
|
BB = BasicBlock::Create(Ctx, "", F);
|
|
|
|
GV = new GlobalVariable(*M, Type::getFloatTy(Ctx), true,
|
2014-06-09 06:29:17 +08:00
|
|
|
GlobalValue::ExternalLinkage, nullptr);
|
2011-05-22 07:14:36 +08:00
|
|
|
}
|
|
|
|
|
2015-04-11 10:11:45 +08:00
|
|
|
void TearDown() override {
|
2014-06-09 06:29:17 +08:00
|
|
|
BB = nullptr;
|
2011-05-22 07:14:36 +08:00
|
|
|
M.reset();
|
|
|
|
}
|
|
|
|
|
2013-11-15 17:34:33 +08:00
|
|
|
LLVMContext Ctx;
|
2014-03-06 13:51:42 +08:00
|
|
|
std::unique_ptr<Module> M;
|
2012-07-16 15:44:51 +08:00
|
|
|
Function *F;
|
2011-05-22 07:14:36 +08:00
|
|
|
BasicBlock *BB;
|
2012-11-29 05:17:34 +08:00
|
|
|
GlobalVariable *GV;
|
2011-05-22 07:14:36 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
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<IntrinsicInst>(Start1);
|
|
|
|
IntrinsicInst *II_End1 = dyn_cast<IntrinsicInst>(End1);
|
2014-06-09 06:29:17 +08:00
|
|
|
ASSERT_TRUE(II_Start1 != nullptr);
|
2011-05-22 07:14:36 +08:00
|
|
|
EXPECT_EQ(II_Start1->getIntrinsicID(), Intrinsic::lifetime_start);
|
2014-06-09 06:29:17 +08:00
|
|
|
ASSERT_TRUE(II_End1 != nullptr);
|
2011-05-22 07:14:36 +08:00
|
|
|
EXPECT_EQ(II_End1->getIntrinsicID(), Intrinsic::lifetime_end);
|
|
|
|
}
|
2012-06-20 16:39:27 +08:00
|
|
|
|
2012-07-16 15:44:51 +08:00
|
|
|
TEST_F(IRBuilderTest, CreateCondBr) {
|
|
|
|
IRBuilder<> Builder(BB);
|
2013-11-15 17:34:33 +08:00
|
|
|
BasicBlock *TBB = BasicBlock::Create(Ctx, "", F);
|
|
|
|
BasicBlock *FBB = BasicBlock::Create(Ctx, "", F);
|
2012-07-16 15:44:51 +08:00
|
|
|
|
|
|
|
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));
|
2012-07-16 15:45:06 +08:00
|
|
|
|
|
|
|
BI->eraseFromParent();
|
2013-11-15 17:34:33 +08:00
|
|
|
MDNode *Weights = MDBuilder(Ctx).createBranchWeights(42, 13);
|
2012-07-16 15:45:06 +08:00
|
|
|
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));
|
2012-07-16 15:44:51 +08:00
|
|
|
}
|
|
|
|
|
2012-12-21 20:03:03 +08:00
|
|
|
TEST_F(IRBuilderTest, LandingPadName) {
|
|
|
|
IRBuilder<> Builder(BB);
|
2015-06-18 04:52:32 +08:00
|
|
|
LandingPadInst *LP = Builder.CreateLandingPad(Builder.getInt32Ty(), 0, "LP");
|
2012-12-21 20:03:03 +08:00
|
|
|
EXPECT_EQ(LP->getName(), "LP");
|
|
|
|
}
|
|
|
|
|
2014-02-26 06:23:04 +08:00
|
|
|
TEST_F(IRBuilderTest, DataLayout) {
|
2014-03-06 13:51:42 +08:00
|
|
|
std::unique_ptr<Module> M(new Module("test", Ctx));
|
2014-02-26 06:23:04 +08:00
|
|
|
M->setDataLayout("e-n32");
|
2015-03-05 02:43:29 +08:00
|
|
|
EXPECT_TRUE(M->getDataLayout().isLegalInteger(32));
|
2014-02-26 06:23:04 +08:00
|
|
|
M->setDataLayout("e");
|
2015-03-05 02:43:29 +08:00
|
|
|
EXPECT_FALSE(M->getDataLayout().isLegalInteger(32));
|
2014-02-26 06:23:04 +08:00
|
|
|
}
|
|
|
|
|
2012-10-31 17:50:01 +08:00
|
|
|
TEST_F(IRBuilderTest, GetIntTy) {
|
|
|
|
IRBuilder<> Builder(BB);
|
|
|
|
IntegerType *Ty1 = Builder.getInt1Ty();
|
2013-11-15 17:34:33 +08:00
|
|
|
EXPECT_EQ(Ty1, IntegerType::get(Ctx, 1));
|
2012-10-31 17:50:01 +08:00
|
|
|
|
|
|
|
DataLayout* DL = new DataLayout(M.get());
|
2015-03-10 10:37:25 +08:00
|
|
|
IntegerType *IntPtrTy = Builder.getIntPtrTy(*DL);
|
2012-10-31 17:50:01 +08:00
|
|
|
unsigned IntPtrBitSize = DL->getPointerSizeInBits(0);
|
2013-11-15 17:34:33 +08:00
|
|
|
EXPECT_EQ(IntPtrTy, IntegerType::get(Ctx, IntPtrBitSize));
|
2013-01-23 16:31:28 +08:00
|
|
|
delete DL;
|
2012-10-31 17:50:01 +08:00
|
|
|
}
|
|
|
|
|
2012-11-29 05:19:52 +08:00
|
|
|
TEST_F(IRBuilderTest, FastMathFlags) {
|
|
|
|
IRBuilder<> Builder(BB);
|
2015-07-10 20:52:00 +08:00
|
|
|
Value *F, *FC;
|
2015-12-15 05:59:03 +08:00
|
|
|
Instruction *FDiv, *FAdd, *FCmp, *FCall;
|
2012-11-29 05:19:52 +08:00
|
|
|
|
|
|
|
F = Builder.CreateLoad(GV);
|
|
|
|
F = Builder.CreateFAdd(F, F);
|
|
|
|
|
|
|
|
EXPECT_FALSE(Builder.getFastMathFlags().any());
|
|
|
|
ASSERT_TRUE(isa<Instruction>(F));
|
|
|
|
FAdd = cast<Instruction>(F);
|
|
|
|
EXPECT_FALSE(FAdd->hasNoNaNs());
|
|
|
|
|
|
|
|
FastMathFlags FMF;
|
2016-01-13 02:03:37 +08:00
|
|
|
Builder.setFastMathFlags(FMF);
|
2012-11-29 05:19:52 +08:00
|
|
|
|
|
|
|
F = Builder.CreateFAdd(F, F);
|
|
|
|
EXPECT_FALSE(Builder.getFastMathFlags().any());
|
|
|
|
|
2012-12-10 05:12:04 +08:00
|
|
|
FMF.setUnsafeAlgebra();
|
2016-01-13 02:03:37 +08:00
|
|
|
Builder.setFastMathFlags(FMF);
|
2012-11-29 05:19:52 +08:00
|
|
|
|
|
|
|
F = Builder.CreateFAdd(F, F);
|
|
|
|
EXPECT_TRUE(Builder.getFastMathFlags().any());
|
|
|
|
ASSERT_TRUE(isa<Instruction>(F));
|
|
|
|
FAdd = cast<Instruction>(F);
|
|
|
|
EXPECT_TRUE(FAdd->hasNoNaNs());
|
2013-12-05 08:32:09 +08:00
|
|
|
|
|
|
|
// Now, try it with CreateBinOp
|
|
|
|
F = Builder.CreateBinOp(Instruction::FAdd, F, F);
|
|
|
|
EXPECT_TRUE(Builder.getFastMathFlags().any());
|
|
|
|
ASSERT_TRUE(isa<Instruction>(F));
|
|
|
|
FAdd = cast<Instruction>(F);
|
|
|
|
EXPECT_TRUE(FAdd->hasNoNaNs());
|
2012-11-29 05:19:52 +08:00
|
|
|
|
|
|
|
F = Builder.CreateFDiv(F, F);
|
|
|
|
EXPECT_TRUE(Builder.getFastMathFlags().any());
|
|
|
|
EXPECT_TRUE(Builder.getFastMathFlags().UnsafeAlgebra);
|
|
|
|
ASSERT_TRUE(isa<Instruction>(F));
|
|
|
|
FDiv = cast<Instruction>(F);
|
|
|
|
EXPECT_TRUE(FDiv->hasAllowReciprocal());
|
|
|
|
|
|
|
|
Builder.clearFastMathFlags();
|
|
|
|
|
|
|
|
F = Builder.CreateFDiv(F, F);
|
|
|
|
ASSERT_TRUE(isa<Instruction>(F));
|
|
|
|
FDiv = cast<Instruction>(F);
|
|
|
|
EXPECT_FALSE(FDiv->hasAllowReciprocal());
|
|
|
|
|
|
|
|
FMF.clear();
|
2012-12-10 05:12:04 +08:00
|
|
|
FMF.setAllowReciprocal();
|
2016-01-13 02:03:37 +08:00
|
|
|
Builder.setFastMathFlags(FMF);
|
2012-11-29 05:19:52 +08:00
|
|
|
|
|
|
|
F = Builder.CreateFDiv(F, F);
|
|
|
|
EXPECT_TRUE(Builder.getFastMathFlags().any());
|
|
|
|
EXPECT_TRUE(Builder.getFastMathFlags().AllowReciprocal);
|
|
|
|
ASSERT_TRUE(isa<Instruction>(F));
|
|
|
|
FDiv = cast<Instruction>(F);
|
|
|
|
EXPECT_TRUE(FDiv->hasAllowReciprocal());
|
|
|
|
|
2012-11-30 05:25:12 +08:00
|
|
|
Builder.clearFastMathFlags();
|
|
|
|
|
2015-07-10 20:52:00 +08:00
|
|
|
FC = Builder.CreateFCmpOEQ(F, F);
|
|
|
|
ASSERT_TRUE(isa<Instruction>(FC));
|
|
|
|
FCmp = cast<Instruction>(FC);
|
|
|
|
EXPECT_FALSE(FCmp->hasAllowReciprocal());
|
|
|
|
|
|
|
|
FMF.clear();
|
|
|
|
FMF.setAllowReciprocal();
|
2016-01-13 02:03:37 +08:00
|
|
|
Builder.setFastMathFlags(FMF);
|
2015-07-10 20:52:00 +08:00
|
|
|
|
|
|
|
FC = Builder.CreateFCmpOEQ(F, F);
|
|
|
|
EXPECT_TRUE(Builder.getFastMathFlags().any());
|
|
|
|
EXPECT_TRUE(Builder.getFastMathFlags().AllowReciprocal);
|
|
|
|
ASSERT_TRUE(isa<Instruction>(FC));
|
|
|
|
FCmp = cast<Instruction>(FC);
|
|
|
|
EXPECT_TRUE(FCmp->hasAllowReciprocal());
|
|
|
|
|
2015-12-15 05:59:03 +08:00
|
|
|
Builder.clearFastMathFlags();
|
|
|
|
|
|
|
|
// Test a call with FMF.
|
|
|
|
auto CalleeTy = FunctionType::get(Type::getFloatTy(Ctx),
|
|
|
|
/*isVarArg=*/false);
|
|
|
|
auto Callee =
|
|
|
|
Function::Create(CalleeTy, Function::ExternalLinkage, "", M.get());
|
|
|
|
|
|
|
|
FCall = Builder.CreateCall(Callee, None);
|
|
|
|
EXPECT_FALSE(FCall->hasNoNaNs());
|
|
|
|
|
2015-12-31 23:39:34 +08:00
|
|
|
Value *V =
|
|
|
|
Function::Create(CalleeTy, Function::ExternalLinkage, "", M.get());
|
|
|
|
FCall = Builder.CreateCall(V, None);
|
|
|
|
EXPECT_FALSE(FCall->hasNoNaNs());
|
|
|
|
|
2015-12-15 05:59:03 +08:00
|
|
|
FMF.clear();
|
|
|
|
FMF.setNoNaNs();
|
2016-01-13 02:03:37 +08:00
|
|
|
Builder.setFastMathFlags(FMF);
|
2015-12-15 05:59:03 +08:00
|
|
|
|
|
|
|
FCall = Builder.CreateCall(Callee, None);
|
|
|
|
EXPECT_TRUE(Builder.getFastMathFlags().any());
|
|
|
|
EXPECT_TRUE(Builder.getFastMathFlags().NoNaNs);
|
|
|
|
EXPECT_TRUE(FCall->hasNoNaNs());
|
|
|
|
|
2015-12-31 23:39:34 +08:00
|
|
|
FCall = Builder.CreateCall(V, None);
|
|
|
|
EXPECT_TRUE(Builder.getFastMathFlags().any());
|
|
|
|
EXPECT_TRUE(Builder.getFastMathFlags().NoNaNs);
|
|
|
|
EXPECT_TRUE(FCall->hasNoNaNs());
|
|
|
|
|
2015-07-10 20:52:00 +08:00
|
|
|
Builder.clearFastMathFlags();
|
|
|
|
|
2014-09-03 04:03:00 +08:00
|
|
|
// To test a copy, make sure that a '0' and a '1' change state.
|
2012-11-30 05:25:12 +08:00
|
|
|
F = Builder.CreateFDiv(F, F);
|
|
|
|
ASSERT_TRUE(isa<Instruction>(F));
|
|
|
|
FDiv = cast<Instruction>(F);
|
|
|
|
EXPECT_FALSE(FDiv->getFastMathFlags().any());
|
2014-09-03 04:03:00 +08:00
|
|
|
FDiv->setHasAllowReciprocal(true);
|
|
|
|
FAdd->setHasAllowReciprocal(false);
|
2012-11-30 05:25:12 +08:00
|
|
|
FDiv->copyFastMathFlags(FAdd);
|
|
|
|
EXPECT_TRUE(FDiv->hasNoNaNs());
|
2014-09-03 04:03:00 +08:00
|
|
|
EXPECT_FALSE(FDiv->hasAllowReciprocal());
|
2012-11-30 05:25:12 +08:00
|
|
|
|
2012-11-29 05:19:52 +08:00
|
|
|
}
|
|
|
|
|
2014-01-05 11:22:33 +08:00
|
|
|
TEST_F(IRBuilderTest, WrapFlags) {
|
|
|
|
IRBuilder<true, NoFolder> Builder(BB);
|
|
|
|
|
|
|
|
// Test instructions.
|
|
|
|
GlobalVariable *G = new GlobalVariable(*M, Builder.getInt32Ty(), true,
|
2014-06-09 06:29:17 +08:00
|
|
|
GlobalValue::ExternalLinkage, nullptr);
|
2014-01-05 11:22:33 +08:00
|
|
|
Value *V = Builder.CreateLoad(G);
|
|
|
|
EXPECT_TRUE(
|
|
|
|
cast<BinaryOperator>(Builder.CreateNSWAdd(V, V))->hasNoSignedWrap());
|
|
|
|
EXPECT_TRUE(
|
|
|
|
cast<BinaryOperator>(Builder.CreateNSWMul(V, V))->hasNoSignedWrap());
|
|
|
|
EXPECT_TRUE(
|
|
|
|
cast<BinaryOperator>(Builder.CreateNSWSub(V, V))->hasNoSignedWrap());
|
|
|
|
EXPECT_TRUE(cast<BinaryOperator>(
|
|
|
|
Builder.CreateShl(V, V, "", /* NUW */ false, /* NSW */ true))
|
|
|
|
->hasNoSignedWrap());
|
|
|
|
|
|
|
|
EXPECT_TRUE(
|
|
|
|
cast<BinaryOperator>(Builder.CreateNUWAdd(V, V))->hasNoUnsignedWrap());
|
|
|
|
EXPECT_TRUE(
|
|
|
|
cast<BinaryOperator>(Builder.CreateNUWMul(V, V))->hasNoUnsignedWrap());
|
|
|
|
EXPECT_TRUE(
|
|
|
|
cast<BinaryOperator>(Builder.CreateNUWSub(V, V))->hasNoUnsignedWrap());
|
|
|
|
EXPECT_TRUE(cast<BinaryOperator>(
|
|
|
|
Builder.CreateShl(V, V, "", /* NUW */ true, /* NSW */ false))
|
|
|
|
->hasNoUnsignedWrap());
|
|
|
|
|
|
|
|
// Test operators created with constants.
|
|
|
|
Constant *C = Builder.getInt32(42);
|
|
|
|
EXPECT_TRUE(cast<OverflowingBinaryOperator>(Builder.CreateNSWAdd(C, C))
|
|
|
|
->hasNoSignedWrap());
|
|
|
|
EXPECT_TRUE(cast<OverflowingBinaryOperator>(Builder.CreateNSWSub(C, C))
|
|
|
|
->hasNoSignedWrap());
|
|
|
|
EXPECT_TRUE(cast<OverflowingBinaryOperator>(Builder.CreateNSWMul(C, C))
|
|
|
|
->hasNoSignedWrap());
|
|
|
|
EXPECT_TRUE(cast<OverflowingBinaryOperator>(
|
|
|
|
Builder.CreateShl(C, C, "", /* NUW */ false, /* NSW */ true))
|
|
|
|
->hasNoSignedWrap());
|
|
|
|
|
|
|
|
EXPECT_TRUE(cast<OverflowingBinaryOperator>(Builder.CreateNUWAdd(C, C))
|
|
|
|
->hasNoUnsignedWrap());
|
|
|
|
EXPECT_TRUE(cast<OverflowingBinaryOperator>(Builder.CreateNUWSub(C, C))
|
|
|
|
->hasNoUnsignedWrap());
|
|
|
|
EXPECT_TRUE(cast<OverflowingBinaryOperator>(Builder.CreateNUWMul(C, C))
|
|
|
|
->hasNoUnsignedWrap());
|
|
|
|
EXPECT_TRUE(cast<OverflowingBinaryOperator>(
|
|
|
|
Builder.CreateShl(C, C, "", /* NUW */ true, /* NSW */ false))
|
|
|
|
->hasNoUnsignedWrap());
|
|
|
|
}
|
|
|
|
|
2013-09-30 23:39:48 +08:00
|
|
|
TEST_F(IRBuilderTest, RAIIHelpersTest) {
|
|
|
|
IRBuilder<> Builder(BB);
|
|
|
|
EXPECT_FALSE(Builder.getFastMathFlags().allowReciprocal());
|
|
|
|
MDBuilder MDB(M->getContext());
|
|
|
|
|
2013-10-06 03:41:41 +08:00
|
|
|
MDNode *FPMathA = MDB.createFPMath(0.01f);
|
|
|
|
MDNode *FPMathB = MDB.createFPMath(0.1f);
|
2013-09-30 23:39:48 +08:00
|
|
|
|
2016-01-13 02:03:37 +08:00
|
|
|
Builder.setDefaultFPMathTag(FPMathA);
|
2013-09-30 23:39:48 +08:00
|
|
|
|
|
|
|
{
|
|
|
|
IRBuilder<>::FastMathFlagGuard Guard(Builder);
|
|
|
|
FastMathFlags FMF;
|
|
|
|
FMF.setAllowReciprocal();
|
2016-01-13 02:03:37 +08:00
|
|
|
Builder.setFastMathFlags(FMF);
|
|
|
|
Builder.setDefaultFPMathTag(FPMathB);
|
2013-09-30 23:39:48 +08:00
|
|
|
EXPECT_TRUE(Builder.getFastMathFlags().allowReciprocal());
|
|
|
|
EXPECT_EQ(FPMathB, Builder.getDefaultFPMathTag());
|
|
|
|
}
|
|
|
|
|
|
|
|
EXPECT_FALSE(Builder.getFastMathFlags().allowReciprocal());
|
|
|
|
EXPECT_EQ(FPMathA, Builder.getDefaultFPMathTag());
|
|
|
|
|
|
|
|
Value *F = Builder.CreateLoad(GV);
|
|
|
|
|
|
|
|
{
|
|
|
|
IRBuilder<>::InsertPointGuard Guard(Builder);
|
|
|
|
Builder.SetInsertPoint(cast<Instruction>(F));
|
2015-10-21 02:30:20 +08:00
|
|
|
EXPECT_EQ(F, &*Builder.GetInsertPoint());
|
2013-09-30 23:39:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
EXPECT_EQ(BB->end(), Builder.GetInsertPoint());
|
|
|
|
EXPECT_EQ(BB, Builder.GetInsertBlock());
|
|
|
|
}
|
|
|
|
|
2015-01-22 02:32:56 +08:00
|
|
|
TEST_F(IRBuilderTest, DIBuilder) {
|
|
|
|
IRBuilder<> Builder(BB);
|
|
|
|
DIBuilder DIB(*M);
|
|
|
|
auto File = DIB.createFile("F.CBL", "/");
|
|
|
|
auto CU = DIB.createCompileUnit(dwarf::DW_LANG_Cobol74, "F.CBL", "/",
|
|
|
|
"llvm-cobol74", true, "", 0);
|
2015-10-15 14:56:10 +08:00
|
|
|
auto Type = DIB.createSubroutineType(DIB.getOrCreateTypeArray(None));
|
2015-11-06 06:03:56 +08:00
|
|
|
auto SP =
|
|
|
|
DIB.createFunction(CU, "foo", "", File, 1, Type, false, true, 1, 0, true);
|
|
|
|
F->setSubprogram(SP);
|
2015-01-22 02:32:56 +08:00
|
|
|
AllocaInst *I = Builder.CreateAlloca(Builder.getInt8Ty());
|
2015-11-06 06:03:56 +08:00
|
|
|
auto BarSP =
|
|
|
|
DIB.createFunction(CU, "bar", "", File, 1, Type, false, true, 1, 0, true);
|
2015-03-31 00:37:48 +08:00
|
|
|
auto BadScope = DIB.createLexicalBlockFile(BarSP, File, 0);
|
2015-01-22 02:32:56 +08:00
|
|
|
I->setDebugLoc(DebugLoc::get(2, 0, BadScope));
|
2015-01-22 03:25:35 +08:00
|
|
|
DIB.finalize();
|
2015-03-31 10:09:55 +08:00
|
|
|
EXPECT_TRUE(verifyModule(*M));
|
2015-01-22 02:32:56 +08:00
|
|
|
}
|
|
|
|
|
2015-04-30 17:01:22 +08:00
|
|
|
TEST_F(IRBuilderTest, InsertExtractElement) {
|
|
|
|
IRBuilder<> Builder(BB);
|
|
|
|
|
|
|
|
auto VecTy = VectorType::get(Builder.getInt64Ty(), 4);
|
|
|
|
auto Elt1 = Builder.getInt64(-1);
|
|
|
|
auto Elt2 = Builder.getInt64(-2);
|
|
|
|
Value *Vec = UndefValue::get(VecTy);
|
|
|
|
Vec = Builder.CreateInsertElement(Vec, Elt1, Builder.getInt8(1));
|
|
|
|
Vec = Builder.CreateInsertElement(Vec, Elt2, 2);
|
|
|
|
auto X1 = Builder.CreateExtractElement(Vec, 1);
|
|
|
|
auto X2 = Builder.CreateExtractElement(Vec, Builder.getInt32(2));
|
|
|
|
EXPECT_EQ(Elt1, X1);
|
|
|
|
EXPECT_EQ(Elt2, X2);
|
|
|
|
}
|
|
|
|
|
2015-06-19 15:19:17 +08:00
|
|
|
TEST_F(IRBuilderTest, CreateGlobalStringPtr) {
|
|
|
|
IRBuilder<> Builder(BB);
|
|
|
|
|
|
|
|
auto String1a = Builder.CreateGlobalStringPtr("TestString", "String1a");
|
|
|
|
auto String1b = Builder.CreateGlobalStringPtr("TestString", "String1b", 0);
|
|
|
|
auto String2 = Builder.CreateGlobalStringPtr("TestString", "String2", 1);
|
|
|
|
auto String3 = Builder.CreateGlobalString("TestString", "String3", 2);
|
2013-09-30 23:39:48 +08:00
|
|
|
|
2015-06-19 15:19:17 +08:00
|
|
|
EXPECT_TRUE(String1a->getType()->getPointerAddressSpace() == 0);
|
|
|
|
EXPECT_TRUE(String1b->getType()->getPointerAddressSpace() == 0);
|
|
|
|
EXPECT_TRUE(String2->getType()->getPointerAddressSpace() == 1);
|
|
|
|
EXPECT_TRUE(String3->getType()->getPointerAddressSpace() == 2);
|
|
|
|
}
|
2015-07-01 03:07:20 +08:00
|
|
|
|
|
|
|
TEST_F(IRBuilderTest, DebugLoc) {
|
|
|
|
auto CalleeTy = FunctionType::get(Type::getVoidTy(Ctx),
|
|
|
|
/*isVarArg=*/false);
|
|
|
|
auto Callee =
|
|
|
|
Function::Create(CalleeTy, Function::ExternalLinkage, "", M.get());
|
|
|
|
|
|
|
|
DIBuilder DIB(*M);
|
|
|
|
auto File = DIB.createFile("tmp.cpp", "/");
|
2015-07-01 06:17:29 +08:00
|
|
|
auto CU = DIB.createCompileUnit(dwarf::DW_LANG_C_plus_plus_11, "tmp.cpp", "/",
|
|
|
|
"", true, "", 0);
|
2015-10-15 14:56:10 +08:00
|
|
|
auto SPType = DIB.createSubroutineType(DIB.getOrCreateTypeArray(None));
|
2015-07-01 03:07:20 +08:00
|
|
|
auto SP =
|
2015-07-01 06:17:29 +08:00
|
|
|
DIB.createFunction(CU, "foo", "foo", File, 1, SPType, false, true, 1);
|
2015-07-01 03:07:20 +08:00
|
|
|
DebugLoc DL1 = DILocation::get(Ctx, 2, 0, SP);
|
|
|
|
DebugLoc DL2 = DILocation::get(Ctx, 3, 0, SP);
|
|
|
|
|
|
|
|
auto BB2 = BasicBlock::Create(Ctx, "bb2", F);
|
|
|
|
auto Br = BranchInst::Create(BB2, BB);
|
|
|
|
Br->setDebugLoc(DL1);
|
|
|
|
|
|
|
|
IRBuilder<> Builder(Ctx);
|
|
|
|
Builder.SetInsertPoint(Br);
|
|
|
|
EXPECT_EQ(DL1, Builder.getCurrentDebugLocation());
|
|
|
|
auto Call1 = Builder.CreateCall(Callee, None);
|
|
|
|
EXPECT_EQ(DL1, Call1->getDebugLoc());
|
|
|
|
|
|
|
|
Call1->setDebugLoc(DL2);
|
2015-10-21 02:30:20 +08:00
|
|
|
Builder.SetInsertPoint(Call1->getParent(), Call1->getIterator());
|
2015-07-01 03:07:20 +08:00
|
|
|
EXPECT_EQ(DL2, Builder.getCurrentDebugLocation());
|
|
|
|
auto Call2 = Builder.CreateCall(Callee, None);
|
|
|
|
EXPECT_EQ(DL2, Call2->getDebugLoc());
|
2015-07-01 06:17:29 +08:00
|
|
|
|
|
|
|
DIB.finalize();
|
2015-07-01 03:07:20 +08:00
|
|
|
}
|
2012-06-20 16:39:27 +08:00
|
|
|
}
|