018914da8c92b65645e7486b1785abf9a06ec91e
[opencl/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 //===----------------------------------------------------------------------===//
10 #include "llvm/IR/IRBuilder.h"
11 #include "llvm/IR/BasicBlock.h"
12 #include "llvm/IR/DataLayout.h"
13 #include "llvm/IR/DIBuilder.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 "llvm/IR/NoFolder.h"
20 #include "gtest/gtest.h"
22 using namespace llvm;
24 namespace {
26 class IRBuilderTest : public testing::Test {
27 protected:
28   virtual void SetUp() {
29     M.reset(new Module("MyModule", Ctx));
30     FunctionType *FTy = FunctionType::get(Type::getVoidTy(Ctx),
31                                           /*isVarArg=*/false);
32     F = Function::Create(FTy, Function::ExternalLinkage, "", M.get());
33     BB = BasicBlock::Create(Ctx, "", F);
34     GV = new GlobalVariable(*M, Type::getFloatTy(Ctx), true,
35                             GlobalValue::ExternalLinkage, nullptr);
36   }
38   virtual void TearDown() {
39     BB = nullptr;
40     M.reset();
41   }
43   LLVMContext Ctx;
44   std::unique_ptr<Module> M;
45   Function *F;
46   BasicBlock *BB;
47   GlobalVariable *GV;
48 };
50 TEST_F(IRBuilderTest, Lifetime) {
51   IRBuilder<> Builder(BB);
52   AllocaInst *Var1 = Builder.CreateAlloca(Builder.getInt8Ty());
53   AllocaInst *Var2 = Builder.CreateAlloca(Builder.getInt32Ty());
54   AllocaInst *Var3 = Builder.CreateAlloca(Builder.getInt8Ty(),
55                                           Builder.getInt32(123));
57   CallInst *Start1 = Builder.CreateLifetimeStart(Var1);
58   CallInst *Start2 = Builder.CreateLifetimeStart(Var2);
59   CallInst *Start3 = Builder.CreateLifetimeStart(Var3, Builder.getInt64(100));
61   EXPECT_EQ(Start1->getArgOperand(0), Builder.getInt64(-1));
62   EXPECT_EQ(Start2->getArgOperand(0), Builder.getInt64(-1));
63   EXPECT_EQ(Start3->getArgOperand(0), Builder.getInt64(100));
65   EXPECT_EQ(Start1->getArgOperand(1), Var1);
66   EXPECT_NE(Start2->getArgOperand(1), Var2);
67   EXPECT_EQ(Start3->getArgOperand(1), Var3);
69   Value *End1 = Builder.CreateLifetimeEnd(Var1);
70   Builder.CreateLifetimeEnd(Var2);
71   Builder.CreateLifetimeEnd(Var3);
73   IntrinsicInst *II_Start1 = dyn_cast<IntrinsicInst>(Start1);
74   IntrinsicInst *II_End1 = dyn_cast<IntrinsicInst>(End1);
75   ASSERT_TRUE(II_Start1 != nullptr);
76   EXPECT_EQ(II_Start1->getIntrinsicID(), Intrinsic::lifetime_start);
77   ASSERT_TRUE(II_End1 != nullptr);
78   EXPECT_EQ(II_End1->getIntrinsicID(), Intrinsic::lifetime_end);
79 }
81 TEST_F(IRBuilderTest, CreateCondBr) {
82   IRBuilder<> Builder(BB);
83   BasicBlock *TBB = BasicBlock::Create(Ctx, "", F);
84   BasicBlock *FBB = BasicBlock::Create(Ctx, "", F);
86   BranchInst *BI = Builder.CreateCondBr(Builder.getTrue(), TBB, FBB);
87   TerminatorInst *TI = BB->getTerminator();
88   EXPECT_EQ(BI, TI);
89   EXPECT_EQ(2u, TI->getNumSuccessors());
90   EXPECT_EQ(TBB, TI->getSuccessor(0));
91   EXPECT_EQ(FBB, TI->getSuccessor(1));
93   BI->eraseFromParent();
94   MDNode *Weights = MDBuilder(Ctx).createBranchWeights(42, 13);
95   BI = Builder.CreateCondBr(Builder.getTrue(), TBB, FBB, Weights);
96   TI = BB->getTerminator();
97   EXPECT_EQ(BI, TI);
98   EXPECT_EQ(2u, TI->getNumSuccessors());
99   EXPECT_EQ(TBB, TI->getSuccessor(0));
100   EXPECT_EQ(FBB, TI->getSuccessor(1));
101   EXPECT_EQ(Weights, TI->getMetadata(LLVMContext::MD_prof));
104 TEST_F(IRBuilderTest, LandingPadName) {
105   IRBuilder<> Builder(BB);
106   LandingPadInst *LP = Builder.CreateLandingPad(Builder.getInt32Ty(),
107                                                 Builder.getInt32(0), 0, "LP");
108   EXPECT_EQ(LP->getName(), "LP");
111 TEST_F(IRBuilderTest, DataLayout) {
112   std::unique_ptr<Module> M(new Module("test", Ctx));
113   M->setDataLayout("e-n32");
114   EXPECT_TRUE(M->getDataLayout()->isLegalInteger(32));
115   M->setDataLayout("e");
116   EXPECT_FALSE(M->getDataLayout()->isLegalInteger(32));
119 TEST_F(IRBuilderTest, GetIntTy) {
120   IRBuilder<> Builder(BB);
121   IntegerType *Ty1 = Builder.getInt1Ty();
122   EXPECT_EQ(Ty1, IntegerType::get(Ctx, 1));
124   DataLayout* DL = new DataLayout(M.get());
125   IntegerType *IntPtrTy = Builder.getIntPtrTy(DL);
126   unsigned IntPtrBitSize =  DL->getPointerSizeInBits(0);
127   EXPECT_EQ(IntPtrTy, IntegerType::get(Ctx, IntPtrBitSize));
128   delete DL;
131 TEST_F(IRBuilderTest, FastMathFlags) {
132   IRBuilder<> Builder(BB);
133   Value *F;
134   Instruction *FDiv, *FAdd;
136   F = Builder.CreateLoad(GV);
137   F = Builder.CreateFAdd(F, F);
139   EXPECT_FALSE(Builder.getFastMathFlags().any());
140   ASSERT_TRUE(isa<Instruction>(F));
141   FAdd = cast<Instruction>(F);
142   EXPECT_FALSE(FAdd->hasNoNaNs());
144   FastMathFlags FMF;
145   Builder.SetFastMathFlags(FMF);
147   F = Builder.CreateFAdd(F, F);
148   EXPECT_FALSE(Builder.getFastMathFlags().any());
150   FMF.setUnsafeAlgebra();
151   Builder.SetFastMathFlags(FMF);
153   F = Builder.CreateFAdd(F, F);
154   EXPECT_TRUE(Builder.getFastMathFlags().any());
155   ASSERT_TRUE(isa<Instruction>(F));
156   FAdd = cast<Instruction>(F);
157   EXPECT_TRUE(FAdd->hasNoNaNs());
159   // Now, try it with CreateBinOp
160   F = Builder.CreateBinOp(Instruction::FAdd, F, F);
161   EXPECT_TRUE(Builder.getFastMathFlags().any());
162   ASSERT_TRUE(isa<Instruction>(F));
163   FAdd = cast<Instruction>(F);
164   EXPECT_TRUE(FAdd->hasNoNaNs());
166   F = Builder.CreateFDiv(F, F);
167   EXPECT_TRUE(Builder.getFastMathFlags().any());
168   EXPECT_TRUE(Builder.getFastMathFlags().UnsafeAlgebra);
169   ASSERT_TRUE(isa<Instruction>(F));
170   FDiv = cast<Instruction>(F);
171   EXPECT_TRUE(FDiv->hasAllowReciprocal());
173   Builder.clearFastMathFlags();
175   F = Builder.CreateFDiv(F, F);
176   ASSERT_TRUE(isa<Instruction>(F));
177   FDiv = cast<Instruction>(F);
178   EXPECT_FALSE(FDiv->hasAllowReciprocal());
180   FMF.clear();
181   FMF.setAllowReciprocal();
182   Builder.SetFastMathFlags(FMF);
184   F = Builder.CreateFDiv(F, F);
185   EXPECT_TRUE(Builder.getFastMathFlags().any());
186   EXPECT_TRUE(Builder.getFastMathFlags().AllowReciprocal);
187   ASSERT_TRUE(isa<Instruction>(F));
188   FDiv = cast<Instruction>(F);
189   EXPECT_TRUE(FDiv->hasAllowReciprocal());
191   Builder.clearFastMathFlags();
193   // To test a copy, make sure that a '0' and a '1' change state. 
194   F = Builder.CreateFDiv(F, F);
195   ASSERT_TRUE(isa<Instruction>(F));
196   FDiv = cast<Instruction>(F);
197   EXPECT_FALSE(FDiv->getFastMathFlags().any());
198   FDiv->setHasAllowReciprocal(true);
199   FAdd->setHasAllowReciprocal(false);
200   FDiv->copyFastMathFlags(FAdd);
201   EXPECT_TRUE(FDiv->hasNoNaNs());
202   EXPECT_FALSE(FDiv->hasAllowReciprocal());
206 TEST_F(IRBuilderTest, WrapFlags) {
207   IRBuilder<true, NoFolder> Builder(BB);
209   // Test instructions.
210   GlobalVariable *G = new GlobalVariable(*M, Builder.getInt32Ty(), true,
211                                          GlobalValue::ExternalLinkage, nullptr);
212   Value *V = Builder.CreateLoad(G);
213   EXPECT_TRUE(
214       cast<BinaryOperator>(Builder.CreateNSWAdd(V, V))->hasNoSignedWrap());
215   EXPECT_TRUE(
216       cast<BinaryOperator>(Builder.CreateNSWMul(V, V))->hasNoSignedWrap());
217   EXPECT_TRUE(
218       cast<BinaryOperator>(Builder.CreateNSWSub(V, V))->hasNoSignedWrap());
219   EXPECT_TRUE(cast<BinaryOperator>(
220                   Builder.CreateShl(V, V, "", /* NUW */ false, /* NSW */ true))
221                   ->hasNoSignedWrap());
223   EXPECT_TRUE(
224       cast<BinaryOperator>(Builder.CreateNUWAdd(V, V))->hasNoUnsignedWrap());
225   EXPECT_TRUE(
226       cast<BinaryOperator>(Builder.CreateNUWMul(V, V))->hasNoUnsignedWrap());
227   EXPECT_TRUE(
228       cast<BinaryOperator>(Builder.CreateNUWSub(V, V))->hasNoUnsignedWrap());
229   EXPECT_TRUE(cast<BinaryOperator>(
230                   Builder.CreateShl(V, V, "", /* NUW */ true, /* NSW */ false))
231                   ->hasNoUnsignedWrap());
233   // Test operators created with constants.
234   Constant *C = Builder.getInt32(42);
235   EXPECT_TRUE(cast<OverflowingBinaryOperator>(Builder.CreateNSWAdd(C, C))
236                   ->hasNoSignedWrap());
237   EXPECT_TRUE(cast<OverflowingBinaryOperator>(Builder.CreateNSWSub(C, C))
238                   ->hasNoSignedWrap());
239   EXPECT_TRUE(cast<OverflowingBinaryOperator>(Builder.CreateNSWMul(C, C))
240                   ->hasNoSignedWrap());
241   EXPECT_TRUE(cast<OverflowingBinaryOperator>(
242                   Builder.CreateShl(C, C, "", /* NUW */ false, /* NSW */ true))
243                   ->hasNoSignedWrap());
245   EXPECT_TRUE(cast<OverflowingBinaryOperator>(Builder.CreateNUWAdd(C, C))
246                   ->hasNoUnsignedWrap());
247   EXPECT_TRUE(cast<OverflowingBinaryOperator>(Builder.CreateNUWSub(C, C))
248                   ->hasNoUnsignedWrap());
249   EXPECT_TRUE(cast<OverflowingBinaryOperator>(Builder.CreateNUWMul(C, C))
250                   ->hasNoUnsignedWrap());
251   EXPECT_TRUE(cast<OverflowingBinaryOperator>(
252                   Builder.CreateShl(C, C, "", /* NUW */ true, /* NSW */ false))
253                   ->hasNoUnsignedWrap());
256 TEST_F(IRBuilderTest, RAIIHelpersTest) {
257   IRBuilder<> Builder(BB);
258   EXPECT_FALSE(Builder.getFastMathFlags().allowReciprocal());
259   MDBuilder MDB(M->getContext());
261   MDNode *FPMathA = MDB.createFPMath(0.01f);
262   MDNode *FPMathB = MDB.createFPMath(0.1f);
264   Builder.SetDefaultFPMathTag(FPMathA);
266   {
267     IRBuilder<>::FastMathFlagGuard Guard(Builder);
268     FastMathFlags FMF;
269     FMF.setAllowReciprocal();
270     Builder.SetFastMathFlags(FMF);
271     Builder.SetDefaultFPMathTag(FPMathB);
272     EXPECT_TRUE(Builder.getFastMathFlags().allowReciprocal());
273     EXPECT_EQ(FPMathB, Builder.getDefaultFPMathTag());
274   }
276   EXPECT_FALSE(Builder.getFastMathFlags().allowReciprocal());
277   EXPECT_EQ(FPMathA, Builder.getDefaultFPMathTag());
279   Value *F = Builder.CreateLoad(GV);
281   {
282     IRBuilder<>::InsertPointGuard Guard(Builder);
283     Builder.SetInsertPoint(cast<Instruction>(F));
284     EXPECT_EQ(F, Builder.GetInsertPoint());
285   }
287   EXPECT_EQ(BB->end(), Builder.GetInsertPoint());
288   EXPECT_EQ(BB, Builder.GetInsertBlock());
291 TEST_F(IRBuilderTest, DIBuilder) {
292   IRBuilder<> Builder(BB);
293   DIBuilder DIB(*M);
294   auto File = DIB.createFile("F.CBL", "/");
295   auto CU = DIB.createCompileUnit(dwarf::DW_LANG_Cobol74, "F.CBL", "/",
296                                   "llvm-cobol74", true, "", 0);
297   auto Type =
298     DIB.createSubroutineType(File,
299                              DIB.getOrCreateTypeArray(ArrayRef<Metadata*>()));
300   auto SP = DIB.createFunction(CU, "foo", "", File, 1, Type,
301                                false, true, 1, 0, true, F);
302   EXPECT_TRUE(SP.Verify());
303   AllocaInst *I = Builder.CreateAlloca(Builder.getInt8Ty());
304   auto BadScope = DIB.createLexicalBlockFile(DIDescriptor(), File, 0);
305   I->setDebugLoc(DebugLoc::get(2, 0, BadScope));
306   EXPECT_FALSE(SP.Verify());
307   DIB.finalize();