Home | History | Annotate | Download | only in IR
      1 //===- llvm/unittest/IR/IRBuilderTest.cpp - IRBuilder tests ---------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 #include "llvm/IR/IRBuilder.h"
     11 #include "llvm/IR/BasicBlock.h"
     12 #include "llvm/IR/DataLayout.h"
     13 #include "llvm/IR/DIBuilder.h"
     14 #include "llvm/IR/Function.h"
     15 #include "llvm/IR/IntrinsicInst.h"
     16 #include "llvm/IR/LLVMContext.h"
     17 #include "llvm/IR/MDBuilder.h"
     18 #include "llvm/IR/Module.h"
     19 #include "llvm/IR/NoFolder.h"
     20 #include "llvm/IR/Verifier.h"
     21 #include "gtest/gtest.h"
     22 
     23 using namespace llvm;
     24 
     25 namespace {
     26 
     27 class IRBuilderTest : public testing::Test {
     28 protected:
     29   void SetUp() override {
     30     M.reset(new Module("MyModule", Ctx));
     31     FunctionType *FTy = FunctionType::get(Type::getVoidTy(Ctx),
     32                                           /*isVarArg=*/false);
     33     F = Function::Create(FTy, Function::ExternalLinkage, "", M.get());
     34     BB = BasicBlock::Create(Ctx, "", F);
     35     GV = new GlobalVariable(*M, Type::getFloatTy(Ctx), true,
     36                             GlobalValue::ExternalLinkage, nullptr);
     37   }
     38 
     39   void TearDown() override {
     40     BB = nullptr;
     41     M.reset();
     42   }
     43 
     44   LLVMContext Ctx;
     45   std::unique_ptr<Module> M;
     46   Function *F;
     47   BasicBlock *BB;
     48   GlobalVariable *GV;
     49 };
     50 
     51 TEST_F(IRBuilderTest, Lifetime) {
     52   IRBuilder<> Builder(BB);
     53   AllocaInst *Var1 = Builder.CreateAlloca(Builder.getInt8Ty());
     54   AllocaInst *Var2 = Builder.CreateAlloca(Builder.getInt32Ty());
     55   AllocaInst *Var3 = Builder.CreateAlloca(Builder.getInt8Ty(),
     56                                           Builder.getInt32(123));
     57 
     58   CallInst *Start1 = Builder.CreateLifetimeStart(Var1);
     59   CallInst *Start2 = Builder.CreateLifetimeStart(Var2);
     60   CallInst *Start3 = Builder.CreateLifetimeStart(Var3, Builder.getInt64(100));
     61 
     62   EXPECT_EQ(Start1->getArgOperand(0), Builder.getInt64(-1));
     63   EXPECT_EQ(Start2->getArgOperand(0), Builder.getInt64(-1));
     64   EXPECT_EQ(Start3->getArgOperand(0), Builder.getInt64(100));
     65 
     66   EXPECT_EQ(Start1->getArgOperand(1), Var1);
     67   EXPECT_NE(Start2->getArgOperand(1), Var2);
     68   EXPECT_EQ(Start3->getArgOperand(1), Var3);
     69 
     70   Value *End1 = Builder.CreateLifetimeEnd(Var1);
     71   Builder.CreateLifetimeEnd(Var2);
     72   Builder.CreateLifetimeEnd(Var3);
     73 
     74   IntrinsicInst *II_Start1 = dyn_cast<IntrinsicInst>(Start1);
     75   IntrinsicInst *II_End1 = dyn_cast<IntrinsicInst>(End1);
     76   ASSERT_TRUE(II_Start1 != nullptr);
     77   EXPECT_EQ(II_Start1->getIntrinsicID(), Intrinsic::lifetime_start);
     78   ASSERT_TRUE(II_End1 != nullptr);
     79   EXPECT_EQ(II_End1->getIntrinsicID(), Intrinsic::lifetime_end);
     80 }
     81 
     82 TEST_F(IRBuilderTest, CreateCondBr) {
     83   IRBuilder<> Builder(BB);
     84   BasicBlock *TBB = BasicBlock::Create(Ctx, "", F);
     85   BasicBlock *FBB = BasicBlock::Create(Ctx, "", F);
     86 
     87   BranchInst *BI = Builder.CreateCondBr(Builder.getTrue(), TBB, FBB);
     88   TerminatorInst *TI = BB->getTerminator();
     89   EXPECT_EQ(BI, TI);
     90   EXPECT_EQ(2u, TI->getNumSuccessors());
     91   EXPECT_EQ(TBB, TI->getSuccessor(0));
     92   EXPECT_EQ(FBB, TI->getSuccessor(1));
     93 
     94   BI->eraseFromParent();
     95   MDNode *Weights = MDBuilder(Ctx).createBranchWeights(42, 13);
     96   BI = Builder.CreateCondBr(Builder.getTrue(), TBB, FBB, Weights);
     97   TI = BB->getTerminator();
     98   EXPECT_EQ(BI, TI);
     99   EXPECT_EQ(2u, TI->getNumSuccessors());
    100   EXPECT_EQ(TBB, TI->getSuccessor(0));
    101   EXPECT_EQ(FBB, TI->getSuccessor(1));
    102   EXPECT_EQ(Weights, TI->getMetadata(LLVMContext::MD_prof));
    103 }
    104 
    105 TEST_F(IRBuilderTest, LandingPadName) {
    106   IRBuilder<> Builder(BB);
    107   LandingPadInst *LP = Builder.CreateLandingPad(Builder.getInt32Ty(), 0, "LP");
    108   EXPECT_EQ(LP->getName(), "LP");
    109 }
    110 
    111 TEST_F(IRBuilderTest, DataLayout) {
    112   std::unique_ptr<Module> M(new Module("test", Ctx));
    113   M->setDataLayout("e-n32");
    114   EXPECT_TRUE(M->getDataLayout().isLegalInteger(32));
    115   M->setDataLayout("e");
    116   EXPECT_FALSE(M->getDataLayout().isLegalInteger(32));
    117 }
    118 
    119 TEST_F(IRBuilderTest, GetIntTy) {
    120   IRBuilder<> Builder(BB);
    121   IntegerType *Ty1 = Builder.getInt1Ty();
    122   EXPECT_EQ(Ty1, IntegerType::get(Ctx, 1));
    123 
    124   DataLayout* DL = new DataLayout(M.get());
    125   IntegerType *IntPtrTy = Builder.getIntPtrTy(*DL);
    126   unsigned IntPtrBitSize =  DL->getPointerSizeInBits(0);
    127   EXPECT_EQ(IntPtrTy, IntegerType::get(Ctx, IntPtrBitSize));
    128   delete DL;
    129 }
    130 
    131 TEST_F(IRBuilderTest, FastMathFlags) {
    132   IRBuilder<> Builder(BB);
    133   Value *F, *FC;
    134   Instruction *FDiv, *FAdd, *FCmp, *FCall;
    135 
    136   F = Builder.CreateLoad(GV);
    137   F = Builder.CreateFAdd(F, F);
    138 
    139   EXPECT_FALSE(Builder.getFastMathFlags().any());
    140   ASSERT_TRUE(isa<Instruction>(F));
    141   FAdd = cast<Instruction>(F);
    142   EXPECT_FALSE(FAdd->hasNoNaNs());
    143 
    144   FastMathFlags FMF;
    145   Builder.SetFastMathFlags(FMF);
    146 
    147   F = Builder.CreateFAdd(F, F);
    148   EXPECT_FALSE(Builder.getFastMathFlags().any());
    149 
    150   FMF.setUnsafeAlgebra();
    151   Builder.SetFastMathFlags(FMF);
    152 
    153   F = Builder.CreateFAdd(F, F);
    154   EXPECT_TRUE(Builder.getFastMathFlags().any());
    155   ASSERT_TRUE(isa<Instruction>(F));
    156   FAdd = cast<Instruction>(F);
    157   EXPECT_TRUE(FAdd->hasNoNaNs());
    158 
    159   // Now, try it with CreateBinOp
    160   F = Builder.CreateBinOp(Instruction::FAdd, F, F);
    161   EXPECT_TRUE(Builder.getFastMathFlags().any());
    162   ASSERT_TRUE(isa<Instruction>(F));
    163   FAdd = cast<Instruction>(F);
    164   EXPECT_TRUE(FAdd->hasNoNaNs());
    165 
    166   F = Builder.CreateFDiv(F, F);
    167   EXPECT_TRUE(Builder.getFastMathFlags().any());
    168   EXPECT_TRUE(Builder.getFastMathFlags().UnsafeAlgebra);
    169   ASSERT_TRUE(isa<Instruction>(F));
    170   FDiv = cast<Instruction>(F);
    171   EXPECT_TRUE(FDiv->hasAllowReciprocal());
    172 
    173   Builder.clearFastMathFlags();
    174 
    175   F = Builder.CreateFDiv(F, F);
    176   ASSERT_TRUE(isa<Instruction>(F));
    177   FDiv = cast<Instruction>(F);
    178   EXPECT_FALSE(FDiv->hasAllowReciprocal());
    179 
    180   FMF.clear();
    181   FMF.setAllowReciprocal();
    182   Builder.SetFastMathFlags(FMF);
    183 
    184   F = Builder.CreateFDiv(F, F);
    185   EXPECT_TRUE(Builder.getFastMathFlags().any());
    186   EXPECT_TRUE(Builder.getFastMathFlags().AllowReciprocal);
    187   ASSERT_TRUE(isa<Instruction>(F));
    188   FDiv = cast<Instruction>(F);
    189   EXPECT_TRUE(FDiv->hasAllowReciprocal());
    190 
    191   Builder.clearFastMathFlags();
    192 
    193   FC = Builder.CreateFCmpOEQ(F, F);
    194   ASSERT_TRUE(isa<Instruction>(FC));
    195   FCmp = cast<Instruction>(FC);
    196   EXPECT_FALSE(FCmp->hasAllowReciprocal());
    197 
    198   FMF.clear();
    199   FMF.setAllowReciprocal();
    200   Builder.SetFastMathFlags(FMF);
    201 
    202   FC = Builder.CreateFCmpOEQ(F, F);
    203   EXPECT_TRUE(Builder.getFastMathFlags().any());
    204   EXPECT_TRUE(Builder.getFastMathFlags().AllowReciprocal);
    205   ASSERT_TRUE(isa<Instruction>(FC));
    206   FCmp = cast<Instruction>(FC);
    207   EXPECT_TRUE(FCmp->hasAllowReciprocal());
    208 
    209   Builder.clearFastMathFlags();
    210 
    211   // Test a call with FMF.
    212   auto CalleeTy = FunctionType::get(Type::getFloatTy(Ctx),
    213                                     /*isVarArg=*/false);
    214   auto Callee =
    215       Function::Create(CalleeTy, Function::ExternalLinkage, "", M.get());
    216 
    217   FCall = Builder.CreateCall(Callee, None);
    218   EXPECT_FALSE(FCall->hasNoNaNs());
    219 
    220   FMF.clear();
    221   FMF.setNoNaNs();
    222   Builder.SetFastMathFlags(FMF);
    223 
    224   FCall = Builder.CreateCall(Callee, None);
    225   EXPECT_TRUE(Builder.getFastMathFlags().any());
    226   EXPECT_TRUE(Builder.getFastMathFlags().NoNaNs);
    227   EXPECT_TRUE(FCall->hasNoNaNs());
    228 
    229   Builder.clearFastMathFlags();
    230 
    231   // To test a copy, make sure that a '0' and a '1' change state.
    232   F = Builder.CreateFDiv(F, F);
    233   ASSERT_TRUE(isa<Instruction>(F));
    234   FDiv = cast<Instruction>(F);
    235   EXPECT_FALSE(FDiv->getFastMathFlags().any());
    236   FDiv->setHasAllowReciprocal(true);
    237   FAdd->setHasAllowReciprocal(false);
    238   FDiv->copyFastMathFlags(FAdd);
    239   EXPECT_TRUE(FDiv->hasNoNaNs());
    240   EXPECT_FALSE(FDiv->hasAllowReciprocal());
    241 
    242 }
    243 
    244 TEST_F(IRBuilderTest, WrapFlags) {
    245   IRBuilder<true, NoFolder> Builder(BB);
    246 
    247   // Test instructions.
    248   GlobalVariable *G = new GlobalVariable(*M, Builder.getInt32Ty(), true,
    249                                          GlobalValue::ExternalLinkage, nullptr);
    250   Value *V = Builder.CreateLoad(G);
    251   EXPECT_TRUE(
    252       cast<BinaryOperator>(Builder.CreateNSWAdd(V, V))->hasNoSignedWrap());
    253   EXPECT_TRUE(
    254       cast<BinaryOperator>(Builder.CreateNSWMul(V, V))->hasNoSignedWrap());
    255   EXPECT_TRUE(
    256       cast<BinaryOperator>(Builder.CreateNSWSub(V, V))->hasNoSignedWrap());
    257   EXPECT_TRUE(cast<BinaryOperator>(
    258                   Builder.CreateShl(V, V, "", /* NUW */ false, /* NSW */ true))
    259                   ->hasNoSignedWrap());
    260 
    261   EXPECT_TRUE(
    262       cast<BinaryOperator>(Builder.CreateNUWAdd(V, V))->hasNoUnsignedWrap());
    263   EXPECT_TRUE(
    264       cast<BinaryOperator>(Builder.CreateNUWMul(V, V))->hasNoUnsignedWrap());
    265   EXPECT_TRUE(
    266       cast<BinaryOperator>(Builder.CreateNUWSub(V, V))->hasNoUnsignedWrap());
    267   EXPECT_TRUE(cast<BinaryOperator>(
    268                   Builder.CreateShl(V, V, "", /* NUW */ true, /* NSW */ false))
    269                   ->hasNoUnsignedWrap());
    270 
    271   // Test operators created with constants.
    272   Constant *C = Builder.getInt32(42);
    273   EXPECT_TRUE(cast<OverflowingBinaryOperator>(Builder.CreateNSWAdd(C, C))
    274                   ->hasNoSignedWrap());
    275   EXPECT_TRUE(cast<OverflowingBinaryOperator>(Builder.CreateNSWSub(C, C))
    276                   ->hasNoSignedWrap());
    277   EXPECT_TRUE(cast<OverflowingBinaryOperator>(Builder.CreateNSWMul(C, C))
    278                   ->hasNoSignedWrap());
    279   EXPECT_TRUE(cast<OverflowingBinaryOperator>(
    280                   Builder.CreateShl(C, C, "", /* NUW */ false, /* NSW */ true))
    281                   ->hasNoSignedWrap());
    282 
    283   EXPECT_TRUE(cast<OverflowingBinaryOperator>(Builder.CreateNUWAdd(C, C))
    284                   ->hasNoUnsignedWrap());
    285   EXPECT_TRUE(cast<OverflowingBinaryOperator>(Builder.CreateNUWSub(C, C))
    286                   ->hasNoUnsignedWrap());
    287   EXPECT_TRUE(cast<OverflowingBinaryOperator>(Builder.CreateNUWMul(C, C))
    288                   ->hasNoUnsignedWrap());
    289   EXPECT_TRUE(cast<OverflowingBinaryOperator>(
    290                   Builder.CreateShl(C, C, "", /* NUW */ true, /* NSW */ false))
    291                   ->hasNoUnsignedWrap());
    292 }
    293 
    294 TEST_F(IRBuilderTest, RAIIHelpersTest) {
    295   IRBuilder<> Builder(BB);
    296   EXPECT_FALSE(Builder.getFastMathFlags().allowReciprocal());
    297   MDBuilder MDB(M->getContext());
    298 
    299   MDNode *FPMathA = MDB.createFPMath(0.01f);
    300   MDNode *FPMathB = MDB.createFPMath(0.1f);
    301 
    302   Builder.SetDefaultFPMathTag(FPMathA);
    303 
    304   {
    305     IRBuilder<>::FastMathFlagGuard Guard(Builder);
    306     FastMathFlags FMF;
    307     FMF.setAllowReciprocal();
    308     Builder.SetFastMathFlags(FMF);
    309     Builder.SetDefaultFPMathTag(FPMathB);
    310     EXPECT_TRUE(Builder.getFastMathFlags().allowReciprocal());
    311     EXPECT_EQ(FPMathB, Builder.getDefaultFPMathTag());
    312   }
    313 
    314   EXPECT_FALSE(Builder.getFastMathFlags().allowReciprocal());
    315   EXPECT_EQ(FPMathA, Builder.getDefaultFPMathTag());
    316 
    317   Value *F = Builder.CreateLoad(GV);
    318 
    319   {
    320     IRBuilder<>::InsertPointGuard Guard(Builder);
    321     Builder.SetInsertPoint(cast<Instruction>(F));
    322     EXPECT_EQ(F, &*Builder.GetInsertPoint());
    323   }
    324 
    325   EXPECT_EQ(BB->end(), Builder.GetInsertPoint());
    326   EXPECT_EQ(BB, Builder.GetInsertBlock());
    327 }
    328 
    329 TEST_F(IRBuilderTest, DIBuilder) {
    330   IRBuilder<> Builder(BB);
    331   DIBuilder DIB(*M);
    332   auto File = DIB.createFile("F.CBL", "/");
    333   auto CU = DIB.createCompileUnit(dwarf::DW_LANG_Cobol74, "F.CBL", "/",
    334                                   "llvm-cobol74", true, "", 0);
    335   auto Type = DIB.createSubroutineType(DIB.getOrCreateTypeArray(None));
    336   auto SP =
    337       DIB.createFunction(CU, "foo", "", File, 1, Type, false, true, 1, 0, true);
    338   F->setSubprogram(SP);
    339   AllocaInst *I = Builder.CreateAlloca(Builder.getInt8Ty());
    340   auto BarSP =
    341       DIB.createFunction(CU, "bar", "", File, 1, Type, false, true, 1, 0, true);
    342   auto BadScope = DIB.createLexicalBlockFile(BarSP, File, 0);
    343   I->setDebugLoc(DebugLoc::get(2, 0, BadScope));
    344   DIB.finalize();
    345   EXPECT_TRUE(verifyModule(*M));
    346 }
    347 
    348 TEST_F(IRBuilderTest, InsertExtractElement) {
    349   IRBuilder<> Builder(BB);
    350 
    351   auto VecTy = VectorType::get(Builder.getInt64Ty(), 4);
    352   auto Elt1 = Builder.getInt64(-1);
    353   auto Elt2 = Builder.getInt64(-2);
    354   Value *Vec = UndefValue::get(VecTy);
    355   Vec = Builder.CreateInsertElement(Vec, Elt1, Builder.getInt8(1));
    356   Vec = Builder.CreateInsertElement(Vec, Elt2, 2);
    357   auto X1 = Builder.CreateExtractElement(Vec, 1);
    358   auto X2 = Builder.CreateExtractElement(Vec, Builder.getInt32(2));
    359   EXPECT_EQ(Elt1, X1);
    360   EXPECT_EQ(Elt2, X2);
    361 }
    362 
    363 TEST_F(IRBuilderTest, CreateGlobalStringPtr) {
    364   IRBuilder<> Builder(BB);
    365 
    366   auto String1a = Builder.CreateGlobalStringPtr("TestString", "String1a");
    367   auto String1b = Builder.CreateGlobalStringPtr("TestString", "String1b", 0);
    368   auto String2 = Builder.CreateGlobalStringPtr("TestString", "String2", 1);
    369   auto String3 = Builder.CreateGlobalString("TestString", "String3", 2);
    370 
    371   EXPECT_TRUE(String1a->getType()->getPointerAddressSpace() == 0);
    372   EXPECT_TRUE(String1b->getType()->getPointerAddressSpace() == 0);
    373   EXPECT_TRUE(String2->getType()->getPointerAddressSpace() == 1);
    374   EXPECT_TRUE(String3->getType()->getPointerAddressSpace() == 2);
    375 }
    376 
    377 TEST_F(IRBuilderTest, DebugLoc) {
    378   auto CalleeTy = FunctionType::get(Type::getVoidTy(Ctx),
    379                                     /*isVarArg=*/false);
    380   auto Callee =
    381       Function::Create(CalleeTy, Function::ExternalLinkage, "", M.get());
    382 
    383   DIBuilder DIB(*M);
    384   auto File = DIB.createFile("tmp.cpp", "/");
    385   auto CU = DIB.createCompileUnit(dwarf::DW_LANG_C_plus_plus_11, "tmp.cpp", "/",
    386                                   "", true, "", 0);
    387   auto SPType = DIB.createSubroutineType(DIB.getOrCreateTypeArray(None));
    388   auto SP =
    389       DIB.createFunction(CU, "foo", "foo", File, 1, SPType, false, true, 1);
    390   DebugLoc DL1 = DILocation::get(Ctx, 2, 0, SP);
    391   DebugLoc DL2 = DILocation::get(Ctx, 3, 0, SP);
    392 
    393   auto BB2 = BasicBlock::Create(Ctx, "bb2", F);
    394   auto Br = BranchInst::Create(BB2, BB);
    395   Br->setDebugLoc(DL1);
    396 
    397   IRBuilder<> Builder(Ctx);
    398   Builder.SetInsertPoint(Br);
    399   EXPECT_EQ(DL1, Builder.getCurrentDebugLocation());
    400   auto Call1 = Builder.CreateCall(Callee, None);
    401   EXPECT_EQ(DL1, Call1->getDebugLoc());
    402 
    403   Call1->setDebugLoc(DL2);
    404   Builder.SetInsertPoint(Call1->getParent(), Call1->getIterator());
    405   EXPECT_EQ(DL2, Builder.getCurrentDebugLocation());
    406   auto Call2 = Builder.CreateCall(Callee, None);
    407   EXPECT_EQ(DL2, Call2->getDebugLoc());
    408 
    409   DIB.finalize();
    410 }
    411 }
    412