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/ADT/OwningPtr.h"
12 #include "llvm/IR/BasicBlock.h"
13 #include "llvm/IR/DataLayout.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 "gtest/gtest.h"
20
21 using namespace llvm;
22
23 namespace {
24
25 class IRBuilderTest : public testing::Test {
26 protected:
SetUp()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 GV = new GlobalVariable(*M, Type::getFloatTy(getGlobalContext()), true,
34 GlobalValue::ExternalLinkage, 0);
35 }
36
TearDown()37 virtual void TearDown() {
38 BB = 0;
39 M.reset();
40 }
41
42 OwningPtr<Module> M;
43 Function *F;
44 BasicBlock *BB;
45 GlobalVariable *GV;
46 };
47
TEST_F(IRBuilderTest,Lifetime)48 TEST_F(IRBuilderTest, Lifetime) {
49 IRBuilder<> Builder(BB);
50 AllocaInst *Var1 = Builder.CreateAlloca(Builder.getInt8Ty());
51 AllocaInst *Var2 = Builder.CreateAlloca(Builder.getInt32Ty());
52 AllocaInst *Var3 = Builder.CreateAlloca(Builder.getInt8Ty(),
53 Builder.getInt32(123));
54
55 CallInst *Start1 = Builder.CreateLifetimeStart(Var1);
56 CallInst *Start2 = Builder.CreateLifetimeStart(Var2);
57 CallInst *Start3 = Builder.CreateLifetimeStart(Var3, Builder.getInt64(100));
58
59 EXPECT_EQ(Start1->getArgOperand(0), Builder.getInt64(-1));
60 EXPECT_EQ(Start2->getArgOperand(0), Builder.getInt64(-1));
61 EXPECT_EQ(Start3->getArgOperand(0), Builder.getInt64(100));
62
63 EXPECT_EQ(Start1->getArgOperand(1), Var1);
64 EXPECT_NE(Start2->getArgOperand(1), Var2);
65 EXPECT_EQ(Start3->getArgOperand(1), Var3);
66
67 Value *End1 = Builder.CreateLifetimeEnd(Var1);
68 Builder.CreateLifetimeEnd(Var2);
69 Builder.CreateLifetimeEnd(Var3);
70
71 IntrinsicInst *II_Start1 = dyn_cast<IntrinsicInst>(Start1);
72 IntrinsicInst *II_End1 = dyn_cast<IntrinsicInst>(End1);
73 ASSERT_TRUE(II_Start1 != NULL);
74 EXPECT_EQ(II_Start1->getIntrinsicID(), Intrinsic::lifetime_start);
75 ASSERT_TRUE(II_End1 != NULL);
76 EXPECT_EQ(II_End1->getIntrinsicID(), Intrinsic::lifetime_end);
77 }
78
TEST_F(IRBuilderTest,CreateCondBr)79 TEST_F(IRBuilderTest, CreateCondBr) {
80 IRBuilder<> Builder(BB);
81 BasicBlock *TBB = BasicBlock::Create(getGlobalContext(), "", F);
82 BasicBlock *FBB = BasicBlock::Create(getGlobalContext(), "", F);
83
84 BranchInst *BI = Builder.CreateCondBr(Builder.getTrue(), TBB, FBB);
85 TerminatorInst *TI = BB->getTerminator();
86 EXPECT_EQ(BI, TI);
87 EXPECT_EQ(2u, TI->getNumSuccessors());
88 EXPECT_EQ(TBB, TI->getSuccessor(0));
89 EXPECT_EQ(FBB, TI->getSuccessor(1));
90
91 BI->eraseFromParent();
92 MDNode *Weights = MDBuilder(getGlobalContext()).createBranchWeights(42, 13);
93 BI = Builder.CreateCondBr(Builder.getTrue(), TBB, FBB, Weights);
94 TI = BB->getTerminator();
95 EXPECT_EQ(BI, TI);
96 EXPECT_EQ(2u, TI->getNumSuccessors());
97 EXPECT_EQ(TBB, TI->getSuccessor(0));
98 EXPECT_EQ(FBB, TI->getSuccessor(1));
99 EXPECT_EQ(Weights, TI->getMetadata(LLVMContext::MD_prof));
100 }
101
TEST_F(IRBuilderTest,LandingPadName)102 TEST_F(IRBuilderTest, LandingPadName) {
103 IRBuilder<> Builder(BB);
104 LandingPadInst *LP = Builder.CreateLandingPad(Builder.getInt32Ty(),
105 Builder.getInt32(0), 0, "LP");
106 EXPECT_EQ(LP->getName(), "LP");
107 }
108
TEST_F(IRBuilderTest,GetIntTy)109 TEST_F(IRBuilderTest, GetIntTy) {
110 IRBuilder<> Builder(BB);
111 IntegerType *Ty1 = Builder.getInt1Ty();
112 EXPECT_EQ(Ty1, IntegerType::get(getGlobalContext(), 1));
113
114 DataLayout* DL = new DataLayout(M.get());
115 IntegerType *IntPtrTy = Builder.getIntPtrTy(DL);
116 unsigned IntPtrBitSize = DL->getPointerSizeInBits(0);
117 EXPECT_EQ(IntPtrTy, IntegerType::get(getGlobalContext(), IntPtrBitSize));
118 delete DL;
119 }
120
TEST_F(IRBuilderTest,FastMathFlags)121 TEST_F(IRBuilderTest, FastMathFlags) {
122 IRBuilder<> Builder(BB);
123 Value *F;
124 Instruction *FDiv, *FAdd;
125
126 F = Builder.CreateLoad(GV);
127 F = Builder.CreateFAdd(F, F);
128
129 EXPECT_FALSE(Builder.getFastMathFlags().any());
130 ASSERT_TRUE(isa<Instruction>(F));
131 FAdd = cast<Instruction>(F);
132 EXPECT_FALSE(FAdd->hasNoNaNs());
133
134 FastMathFlags FMF;
135 Builder.SetFastMathFlags(FMF);
136
137 F = Builder.CreateFAdd(F, F);
138 EXPECT_FALSE(Builder.getFastMathFlags().any());
139
140 FMF.setUnsafeAlgebra();
141 Builder.SetFastMathFlags(FMF);
142
143 F = Builder.CreateFAdd(F, F);
144 EXPECT_TRUE(Builder.getFastMathFlags().any());
145 ASSERT_TRUE(isa<Instruction>(F));
146 FAdd = cast<Instruction>(F);
147 EXPECT_TRUE(FAdd->hasNoNaNs());
148
149 F = Builder.CreateFDiv(F, F);
150 EXPECT_TRUE(Builder.getFastMathFlags().any());
151 EXPECT_TRUE(Builder.getFastMathFlags().UnsafeAlgebra);
152 ASSERT_TRUE(isa<Instruction>(F));
153 FDiv = cast<Instruction>(F);
154 EXPECT_TRUE(FDiv->hasAllowReciprocal());
155
156 Builder.clearFastMathFlags();
157
158 F = Builder.CreateFDiv(F, F);
159 ASSERT_TRUE(isa<Instruction>(F));
160 FDiv = cast<Instruction>(F);
161 EXPECT_FALSE(FDiv->hasAllowReciprocal());
162
163 FMF.clear();
164 FMF.setAllowReciprocal();
165 Builder.SetFastMathFlags(FMF);
166
167 F = Builder.CreateFDiv(F, F);
168 EXPECT_TRUE(Builder.getFastMathFlags().any());
169 EXPECT_TRUE(Builder.getFastMathFlags().AllowReciprocal);
170 ASSERT_TRUE(isa<Instruction>(F));
171 FDiv = cast<Instruction>(F);
172 EXPECT_TRUE(FDiv->hasAllowReciprocal());
173
174 Builder.clearFastMathFlags();
175
176 F = Builder.CreateFDiv(F, F);
177 ASSERT_TRUE(isa<Instruction>(F));
178 FDiv = cast<Instruction>(F);
179 EXPECT_FALSE(FDiv->getFastMathFlags().any());
180 FDiv->copyFastMathFlags(FAdd);
181 EXPECT_TRUE(FDiv->hasNoNaNs());
182
183 }
184
185 }
186