Remove weak vtables. No functional change.
[oota-llvm.git] / unittests / IR / IRBuilderTest.cpp
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:
27   virtual void SetUp() {
28     M.reset(new Module("MyModule", Ctx));
29     FunctionType *FTy = FunctionType::get(Type::getVoidTy(Ctx),
30                                           /*isVarArg=*/false);
31     F = Function::Create(FTy, Function::ExternalLinkage, "", M.get());
32     BB = BasicBlock::Create(Ctx, "", F);
33     GV = new GlobalVariable(*M, Type::getFloatTy(Ctx), true,
34                             GlobalValue::ExternalLinkage, 0);
35   }
36
37   virtual void TearDown() {
38     BB = 0;
39     M.reset();
40   }
41
42   LLVMContext Ctx;
43   OwningPtr<Module> M;
44   Function *F;
45   BasicBlock *BB;
46   GlobalVariable *GV;
47 };
48
49 TEST_F(IRBuilderTest, Lifetime) {
50   IRBuilder<> Builder(BB);
51   AllocaInst *Var1 = Builder.CreateAlloca(Builder.getInt8Ty());
52   AllocaInst *Var2 = Builder.CreateAlloca(Builder.getInt32Ty());
53   AllocaInst *Var3 = Builder.CreateAlloca(Builder.getInt8Ty(),
54                                           Builder.getInt32(123));
55
56   CallInst *Start1 = Builder.CreateLifetimeStart(Var1);
57   CallInst *Start2 = Builder.CreateLifetimeStart(Var2);
58   CallInst *Start3 = Builder.CreateLifetimeStart(Var3, Builder.getInt64(100));
59
60   EXPECT_EQ(Start1->getArgOperand(0), Builder.getInt64(-1));
61   EXPECT_EQ(Start2->getArgOperand(0), Builder.getInt64(-1));
62   EXPECT_EQ(Start3->getArgOperand(0), Builder.getInt64(100));
63
64   EXPECT_EQ(Start1->getArgOperand(1), Var1);
65   EXPECT_NE(Start2->getArgOperand(1), Var2);
66   EXPECT_EQ(Start3->getArgOperand(1), Var3);
67
68   Value *End1 = Builder.CreateLifetimeEnd(Var1);
69   Builder.CreateLifetimeEnd(Var2);
70   Builder.CreateLifetimeEnd(Var3);
71
72   IntrinsicInst *II_Start1 = dyn_cast<IntrinsicInst>(Start1);
73   IntrinsicInst *II_End1 = dyn_cast<IntrinsicInst>(End1);
74   ASSERT_TRUE(II_Start1 != NULL);
75   EXPECT_EQ(II_Start1->getIntrinsicID(), Intrinsic::lifetime_start);
76   ASSERT_TRUE(II_End1 != NULL);
77   EXPECT_EQ(II_End1->getIntrinsicID(), Intrinsic::lifetime_end);
78 }
79
80 TEST_F(IRBuilderTest, CreateCondBr) {
81   IRBuilder<> Builder(BB);
82   BasicBlock *TBB = BasicBlock::Create(Ctx, "", F);
83   BasicBlock *FBB = BasicBlock::Create(Ctx, "", F);
84
85   BranchInst *BI = Builder.CreateCondBr(Builder.getTrue(), TBB, FBB);
86   TerminatorInst *TI = BB->getTerminator();
87   EXPECT_EQ(BI, TI);
88   EXPECT_EQ(2u, TI->getNumSuccessors());
89   EXPECT_EQ(TBB, TI->getSuccessor(0));
90   EXPECT_EQ(FBB, TI->getSuccessor(1));
91
92   BI->eraseFromParent();
93   MDNode *Weights = MDBuilder(Ctx).createBranchWeights(42, 13);
94   BI = Builder.CreateCondBr(Builder.getTrue(), TBB, FBB, Weights);
95   TI = BB->getTerminator();
96   EXPECT_EQ(BI, TI);
97   EXPECT_EQ(2u, TI->getNumSuccessors());
98   EXPECT_EQ(TBB, TI->getSuccessor(0));
99   EXPECT_EQ(FBB, TI->getSuccessor(1));
100   EXPECT_EQ(Weights, TI->getMetadata(LLVMContext::MD_prof));
101 }
102
103 TEST_F(IRBuilderTest, LandingPadName) {
104   IRBuilder<> Builder(BB);
105   LandingPadInst *LP = Builder.CreateLandingPad(Builder.getInt32Ty(),
106                                                 Builder.getInt32(0), 0, "LP");
107   EXPECT_EQ(LP->getName(), "LP");
108 }
109
110 TEST_F(IRBuilderTest, GetIntTy) {
111   IRBuilder<> Builder(BB);
112   IntegerType *Ty1 = Builder.getInt1Ty();
113   EXPECT_EQ(Ty1, IntegerType::get(Ctx, 1));
114
115   DataLayout* DL = new DataLayout(M.get());
116   IntegerType *IntPtrTy = Builder.getIntPtrTy(DL);
117   unsigned IntPtrBitSize =  DL->getPointerSizeInBits(0);
118   EXPECT_EQ(IntPtrTy, IntegerType::get(Ctx, IntPtrBitSize));
119   delete DL;
120 }
121
122 TEST_F(IRBuilderTest, FastMathFlags) {
123   IRBuilder<> Builder(BB);
124   Value *F;
125   Instruction *FDiv, *FAdd;
126
127   F = Builder.CreateLoad(GV);
128   F = Builder.CreateFAdd(F, F);
129
130   EXPECT_FALSE(Builder.getFastMathFlags().any());
131   ASSERT_TRUE(isa<Instruction>(F));
132   FAdd = cast<Instruction>(F);
133   EXPECT_FALSE(FAdd->hasNoNaNs());
134
135   FastMathFlags FMF;
136   Builder.SetFastMathFlags(FMF);
137
138   F = Builder.CreateFAdd(F, F);
139   EXPECT_FALSE(Builder.getFastMathFlags().any());
140
141   FMF.setUnsafeAlgebra();
142   Builder.SetFastMathFlags(FMF);
143
144   F = Builder.CreateFAdd(F, F);
145   EXPECT_TRUE(Builder.getFastMathFlags().any());
146   ASSERT_TRUE(isa<Instruction>(F));
147   FAdd = cast<Instruction>(F);
148   EXPECT_TRUE(FAdd->hasNoNaNs());
149
150   // Now, try it with CreateBinOp
151   F = Builder.CreateBinOp(Instruction::FAdd, F, F);
152   EXPECT_TRUE(Builder.getFastMathFlags().any());
153   ASSERT_TRUE(isa<Instruction>(F));
154   FAdd = cast<Instruction>(F);
155   EXPECT_TRUE(FAdd->hasNoNaNs());
156
157   F = Builder.CreateFDiv(F, F);
158   EXPECT_TRUE(Builder.getFastMathFlags().any());
159   EXPECT_TRUE(Builder.getFastMathFlags().UnsafeAlgebra);
160   ASSERT_TRUE(isa<Instruction>(F));
161   FDiv = cast<Instruction>(F);
162   EXPECT_TRUE(FDiv->hasAllowReciprocal());
163
164   Builder.clearFastMathFlags();
165
166   F = Builder.CreateFDiv(F, F);
167   ASSERT_TRUE(isa<Instruction>(F));
168   FDiv = cast<Instruction>(F);
169   EXPECT_FALSE(FDiv->hasAllowReciprocal());
170
171   FMF.clear();
172   FMF.setAllowReciprocal();
173   Builder.SetFastMathFlags(FMF);
174
175   F = Builder.CreateFDiv(F, F);
176   EXPECT_TRUE(Builder.getFastMathFlags().any());
177   EXPECT_TRUE(Builder.getFastMathFlags().AllowReciprocal);
178   ASSERT_TRUE(isa<Instruction>(F));
179   FDiv = cast<Instruction>(F);
180   EXPECT_TRUE(FDiv->hasAllowReciprocal());
181
182   Builder.clearFastMathFlags();
183
184   F = Builder.CreateFDiv(F, F);
185   ASSERT_TRUE(isa<Instruction>(F));
186   FDiv = cast<Instruction>(F);
187   EXPECT_FALSE(FDiv->getFastMathFlags().any());
188   FDiv->copyFastMathFlags(FAdd);
189   EXPECT_TRUE(FDiv->hasNoNaNs());
190
191 }
192
193 TEST_F(IRBuilderTest, RAIIHelpersTest) {
194   IRBuilder<> Builder(BB);
195   EXPECT_FALSE(Builder.getFastMathFlags().allowReciprocal());
196   MDBuilder MDB(M->getContext());
197
198   MDNode *FPMathA = MDB.createFPMath(0.01f);
199   MDNode *FPMathB = MDB.createFPMath(0.1f);
200
201   Builder.SetDefaultFPMathTag(FPMathA);
202
203   {
204     IRBuilder<>::FastMathFlagGuard Guard(Builder);
205     FastMathFlags FMF;
206     FMF.setAllowReciprocal();
207     Builder.SetFastMathFlags(FMF);
208     Builder.SetDefaultFPMathTag(FPMathB);
209     EXPECT_TRUE(Builder.getFastMathFlags().allowReciprocal());
210     EXPECT_EQ(FPMathB, Builder.getDefaultFPMathTag());
211   }
212
213   EXPECT_FALSE(Builder.getFastMathFlags().allowReciprocal());
214   EXPECT_EQ(FPMathA, Builder.getDefaultFPMathTag());
215
216   Value *F = Builder.CreateLoad(GV);
217
218   {
219     IRBuilder<>::InsertPointGuard Guard(Builder);
220     Builder.SetInsertPoint(cast<Instruction>(F));
221     EXPECT_EQ(F, Builder.GetInsertPoint());
222   }
223
224   EXPECT_EQ(BB->end(), Builder.GetInsertPoint());
225   EXPECT_EQ(BB, Builder.GetInsertBlock());
226 }
227
228
229 }