Land the long talked about "type system rewrite" patch. This
[llvm/stm8.git] / unittests / Support / IRBuilderTest.cpp
blob5d635ae361e05cea587d3b9adef9fd7afa37834b
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 //===----------------------------------------------------------------------===//
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"
18 #include "gtest/gtest.h"
20 using namespace llvm;
22 class IRBuilderTest : public testing::Test {
23 protected:
24 virtual void SetUp() {
25 M.reset(new Module("MyModule", getGlobalContext()));
26 FunctionType *FTy = FunctionType::get(Type::getVoidTy(getGlobalContext()),
27 /*isVarArg=*/false);
28 Function *F = Function::Create(FTy, Function::ExternalLinkage, "", M.get());
29 BB = BasicBlock::Create(getGlobalContext(), "", F);
32 virtual void TearDown() {
33 BB = 0;
34 M.reset();
37 OwningPtr<Module> M;
38 BasicBlock *BB;
41 TEST_F(IRBuilderTest, Lifetime) {
42 IRBuilder<> Builder(BB);
43 AllocaInst *Var1 = Builder.CreateAlloca(Builder.getInt8Ty());
44 AllocaInst *Var2 = Builder.CreateAlloca(Builder.getInt32Ty());
45 AllocaInst *Var3 = Builder.CreateAlloca(Builder.getInt8Ty(),
46 Builder.getInt32(123));
48 CallInst *Start1 = Builder.CreateLifetimeStart(Var1);
49 CallInst *Start2 = Builder.CreateLifetimeStart(Var2);
50 CallInst *Start3 = Builder.CreateLifetimeStart(Var3, Builder.getInt64(100));
52 EXPECT_EQ(Start1->getArgOperand(0), Builder.getInt64(-1));
53 EXPECT_EQ(Start2->getArgOperand(0), Builder.getInt64(-1));
54 EXPECT_EQ(Start3->getArgOperand(0), Builder.getInt64(100));
56 EXPECT_EQ(Start1->getArgOperand(1), Var1);
57 EXPECT_NE(Start2->getArgOperand(1), Var2);
58 EXPECT_EQ(Start3->getArgOperand(1), Var3);
60 Value *End1 = Builder.CreateLifetimeEnd(Var1);
61 Builder.CreateLifetimeEnd(Var2);
62 Builder.CreateLifetimeEnd(Var3);
64 IntrinsicInst *II_Start1 = dyn_cast<IntrinsicInst>(Start1);
65 IntrinsicInst *II_End1 = dyn_cast<IntrinsicInst>(End1);
66 ASSERT_TRUE(II_Start1 != NULL);
67 EXPECT_EQ(II_Start1->getIntrinsicID(), Intrinsic::lifetime_start);
68 ASSERT_TRUE(II_End1 != NULL);
69 EXPECT_EQ(II_End1->getIntrinsicID(), Intrinsic::lifetime_end);