Home | History | Annotate | Download | only in Support
      1 //===- llvm/unittest/Support/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/Support/IRBuilder.h"
     11 #include "llvm/BasicBlock.h"
     12 #include "llvm/Function.h"
     13 #include "llvm/IntrinsicInst.h"
     14 #include "llvm/LLVMContext.h"
     15 #include "llvm/Module.h"
     16 #include "llvm/ADT/OwningPtr.h"
     17 
     18 #include "gtest/gtest.h"
     19 
     20 using namespace llvm;
     21 
     22 namespace {
     23 class IRBuilderTest : public testing::Test {
     24 protected:
     25   virtual void SetUp() {
     26     M.reset(new Module("MyModule", getGlobalContext()));
     27     FunctionType *FTy = FunctionType::get(Type::getVoidTy(getGlobalContext()),
     28                                           /*isVarArg=*/false);
     29     Function *F = Function::Create(FTy, Function::ExternalLinkage, "", M.get());
     30     BB = BasicBlock::Create(getGlobalContext(), "", F);
     31   }
     32 
     33   virtual void TearDown() {
     34     BB = 0;
     35     M.reset();
     36   }
     37 
     38   OwningPtr<Module> M;
     39   BasicBlock *BB;
     40 };
     41 }
     42 
     43 TEST_F(IRBuilderTest, Lifetime) {
     44   IRBuilder<> Builder(BB);
     45   AllocaInst *Var1 = Builder.CreateAlloca(Builder.getInt8Ty());
     46   AllocaInst *Var2 = Builder.CreateAlloca(Builder.getInt32Ty());
     47   AllocaInst *Var3 = Builder.CreateAlloca(Builder.getInt8Ty(),
     48                                           Builder.getInt32(123));
     49 
     50   CallInst *Start1 = Builder.CreateLifetimeStart(Var1);
     51   CallInst *Start2 = Builder.CreateLifetimeStart(Var2);
     52   CallInst *Start3 = Builder.CreateLifetimeStart(Var3, Builder.getInt64(100));
     53 
     54   EXPECT_EQ(Start1->getArgOperand(0), Builder.getInt64(-1));
     55   EXPECT_EQ(Start2->getArgOperand(0), Builder.getInt64(-1));
     56   EXPECT_EQ(Start3->getArgOperand(0), Builder.getInt64(100));
     57 
     58   EXPECT_EQ(Start1->getArgOperand(1), Var1);
     59   EXPECT_NE(Start2->getArgOperand(1), Var2);
     60   EXPECT_EQ(Start3->getArgOperand(1), Var3);
     61 
     62   Value *End1 = Builder.CreateLifetimeEnd(Var1);
     63   Builder.CreateLifetimeEnd(Var2);
     64   Builder.CreateLifetimeEnd(Var3);
     65 
     66   IntrinsicInst *II_Start1 = dyn_cast<IntrinsicInst>(Start1);
     67   IntrinsicInst *II_End1 = dyn_cast<IntrinsicInst>(End1);
     68   ASSERT_TRUE(II_Start1 != NULL);
     69   EXPECT_EQ(II_Start1->getIntrinsicID(), Intrinsic::lifetime_start);
     70   ASSERT_TRUE(II_End1 != NULL);
     71   EXPECT_EQ(II_End1->getIntrinsicID(), Intrinsic::lifetime_end);
     72 }
     73