1//===- llvm/unittest/VMCore/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/BasicBlock.h"
11#include "llvm/Function.h"
12#include "llvm/IRBuilder.h"
13#include "llvm/IntrinsicInst.h"
14#include "llvm/LLVMContext.h"
15#include "llvm/MDBuilder.h"
16#include "llvm/Module.h"
17#include "llvm/ADT/OwningPtr.h"
18
19#include "gtest/gtest.h"
20
21using namespace llvm;
22
23namespace {
24
25class IRBuilderTest : public testing::Test {
26protected:
27  virtual void SetUp() {
28    M.reset(new Module("MyModule", getGlobalContext()));
29    FunctionType *FTy = FunctionType::get(Type::getVoidTy(getGlobalContext()),
30                                          /*isVarArg=*/false);
31    F = Function::Create(FTy, Function::ExternalLinkage, "", M.get());
32    BB = BasicBlock::Create(getGlobalContext(), "", F);
33  }
34
35  virtual void TearDown() {
36    BB = 0;
37    M.reset();
38  }
39
40  OwningPtr<Module> M;
41  Function *F;
42  BasicBlock *BB;
43};
44
45TEST_F(IRBuilderTest, Lifetime) {
46  IRBuilder<> Builder(BB);
47  AllocaInst *Var1 = Builder.CreateAlloca(Builder.getInt8Ty());
48  AllocaInst *Var2 = Builder.CreateAlloca(Builder.getInt32Ty());
49  AllocaInst *Var3 = Builder.CreateAlloca(Builder.getInt8Ty(),
50                                          Builder.getInt32(123));
51
52  CallInst *Start1 = Builder.CreateLifetimeStart(Var1);
53  CallInst *Start2 = Builder.CreateLifetimeStart(Var2);
54  CallInst *Start3 = Builder.CreateLifetimeStart(Var3, Builder.getInt64(100));
55
56  EXPECT_EQ(Start1->getArgOperand(0), Builder.getInt64(-1));
57  EXPECT_EQ(Start2->getArgOperand(0), Builder.getInt64(-1));
58  EXPECT_EQ(Start3->getArgOperand(0), Builder.getInt64(100));
59
60  EXPECT_EQ(Start1->getArgOperand(1), Var1);
61  EXPECT_NE(Start2->getArgOperand(1), Var2);
62  EXPECT_EQ(Start3->getArgOperand(1), Var3);
63
64  Value *End1 = Builder.CreateLifetimeEnd(Var1);
65  Builder.CreateLifetimeEnd(Var2);
66  Builder.CreateLifetimeEnd(Var3);
67
68  IntrinsicInst *II_Start1 = dyn_cast<IntrinsicInst>(Start1);
69  IntrinsicInst *II_End1 = dyn_cast<IntrinsicInst>(End1);
70  ASSERT_TRUE(II_Start1 != NULL);
71  EXPECT_EQ(II_Start1->getIntrinsicID(), Intrinsic::lifetime_start);
72  ASSERT_TRUE(II_End1 != NULL);
73  EXPECT_EQ(II_End1->getIntrinsicID(), Intrinsic::lifetime_end);
74}
75
76TEST_F(IRBuilderTest, CreateCondBr) {
77  IRBuilder<> Builder(BB);
78  BasicBlock *TBB = BasicBlock::Create(getGlobalContext(), "", F);
79  BasicBlock *FBB = BasicBlock::Create(getGlobalContext(), "", F);
80
81  BranchInst *BI = Builder.CreateCondBr(Builder.getTrue(), TBB, FBB);
82  TerminatorInst *TI = BB->getTerminator();
83  EXPECT_EQ(BI, TI);
84  EXPECT_EQ(2u, TI->getNumSuccessors());
85  EXPECT_EQ(TBB, TI->getSuccessor(0));
86  EXPECT_EQ(FBB, TI->getSuccessor(1));
87
88  BI->eraseFromParent();
89  MDNode *Weights = MDBuilder(getGlobalContext()).createBranchWeights(42, 13);
90  BI = Builder.CreateCondBr(Builder.getTrue(), TBB, FBB, Weights);
91  TI = BB->getTerminator();
92  EXPECT_EQ(BI, TI);
93  EXPECT_EQ(2u, TI->getNumSuccessors());
94  EXPECT_EQ(TBB, TI->getSuccessor(0));
95  EXPECT_EQ(FBB, TI->getSuccessor(1));
96  EXPECT_EQ(Weights, TI->getMetadata(LLVMContext::MD_prof));
97}
98
99}
100