• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/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 "llvm/IR/Verifier.h"
21 #include "gtest/gtest.h"
22 
23 using namespace llvm;
24 
25 namespace {
26 
27 class IRBuilderTest : public testing::Test {
28 protected:
SetUp()29   void SetUp() override {
30     M.reset(new Module("MyModule", Ctx));
31     FunctionType *FTy = FunctionType::get(Type::getVoidTy(Ctx),
32                                           /*isVarArg=*/false);
33     F = Function::Create(FTy, Function::ExternalLinkage, "", M.get());
34     BB = BasicBlock::Create(Ctx, "", F);
35     GV = new GlobalVariable(*M, Type::getFloatTy(Ctx), true,
36                             GlobalValue::ExternalLinkage, nullptr);
37   }
38 
TearDown()39   void TearDown() override {
40     BB = nullptr;
41     M.reset();
42   }
43 
44   LLVMContext Ctx;
45   std::unique_ptr<Module> M;
46   Function *F;
47   BasicBlock *BB;
48   GlobalVariable *GV;
49 };
50 
TEST_F(IRBuilderTest,Lifetime)51 TEST_F(IRBuilderTest, Lifetime) {
52   IRBuilder<> Builder(BB);
53   AllocaInst *Var1 = Builder.CreateAlloca(Builder.getInt8Ty());
54   AllocaInst *Var2 = Builder.CreateAlloca(Builder.getInt32Ty());
55   AllocaInst *Var3 = Builder.CreateAlloca(Builder.getInt8Ty(),
56                                           Builder.getInt32(123));
57 
58   CallInst *Start1 = Builder.CreateLifetimeStart(Var1);
59   CallInst *Start2 = Builder.CreateLifetimeStart(Var2);
60   CallInst *Start3 = Builder.CreateLifetimeStart(Var3, Builder.getInt64(100));
61 
62   EXPECT_EQ(Start1->getArgOperand(0), Builder.getInt64(-1));
63   EXPECT_EQ(Start2->getArgOperand(0), Builder.getInt64(-1));
64   EXPECT_EQ(Start3->getArgOperand(0), Builder.getInt64(100));
65 
66   EXPECT_EQ(Start1->getArgOperand(1), Var1);
67   EXPECT_NE(Start2->getArgOperand(1), Var2);
68   EXPECT_EQ(Start3->getArgOperand(1), Var3);
69 
70   Value *End1 = Builder.CreateLifetimeEnd(Var1);
71   Builder.CreateLifetimeEnd(Var2);
72   Builder.CreateLifetimeEnd(Var3);
73 
74   IntrinsicInst *II_Start1 = dyn_cast<IntrinsicInst>(Start1);
75   IntrinsicInst *II_End1 = dyn_cast<IntrinsicInst>(End1);
76   ASSERT_TRUE(II_Start1 != nullptr);
77   EXPECT_EQ(II_Start1->getIntrinsicID(), Intrinsic::lifetime_start);
78   ASSERT_TRUE(II_End1 != nullptr);
79   EXPECT_EQ(II_End1->getIntrinsicID(), Intrinsic::lifetime_end);
80 }
81 
TEST_F(IRBuilderTest,CreateCondBr)82 TEST_F(IRBuilderTest, CreateCondBr) {
83   IRBuilder<> Builder(BB);
84   BasicBlock *TBB = BasicBlock::Create(Ctx, "", F);
85   BasicBlock *FBB = BasicBlock::Create(Ctx, "", F);
86 
87   BranchInst *BI = Builder.CreateCondBr(Builder.getTrue(), TBB, FBB);
88   TerminatorInst *TI = BB->getTerminator();
89   EXPECT_EQ(BI, TI);
90   EXPECT_EQ(2u, TI->getNumSuccessors());
91   EXPECT_EQ(TBB, TI->getSuccessor(0));
92   EXPECT_EQ(FBB, TI->getSuccessor(1));
93 
94   BI->eraseFromParent();
95   MDNode *Weights = MDBuilder(Ctx).createBranchWeights(42, 13);
96   BI = Builder.CreateCondBr(Builder.getTrue(), TBB, FBB, Weights);
97   TI = BB->getTerminator();
98   EXPECT_EQ(BI, TI);
99   EXPECT_EQ(2u, TI->getNumSuccessors());
100   EXPECT_EQ(TBB, TI->getSuccessor(0));
101   EXPECT_EQ(FBB, TI->getSuccessor(1));
102   EXPECT_EQ(Weights, TI->getMetadata(LLVMContext::MD_prof));
103 }
104 
TEST_F(IRBuilderTest,LandingPadName)105 TEST_F(IRBuilderTest, LandingPadName) {
106   IRBuilder<> Builder(BB);
107   LandingPadInst *LP = Builder.CreateLandingPad(Builder.getInt32Ty(), 0, "LP");
108   EXPECT_EQ(LP->getName(), "LP");
109 }
110 
TEST_F(IRBuilderTest,DataLayout)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));
117 }
118 
TEST_F(IRBuilderTest,GetIntTy)119 TEST_F(IRBuilderTest, GetIntTy) {
120   IRBuilder<> Builder(BB);
121   IntegerType *Ty1 = Builder.getInt1Ty();
122   EXPECT_EQ(Ty1, IntegerType::get(Ctx, 1));
123 
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;
129 }
130 
TEST_F(IRBuilderTest,FastMathFlags)131 TEST_F(IRBuilderTest, FastMathFlags) {
132   IRBuilder<> Builder(BB);
133   Value *F, *FC;
134   Instruction *FDiv, *FAdd, *FCmp, *FCall;
135 
136   F = Builder.CreateLoad(GV);
137   F = Builder.CreateFAdd(F, F);
138 
139   EXPECT_FALSE(Builder.getFastMathFlags().any());
140   ASSERT_TRUE(isa<Instruction>(F));
141   FAdd = cast<Instruction>(F);
142   EXPECT_FALSE(FAdd->hasNoNaNs());
143 
144   FastMathFlags FMF;
145   Builder.setFastMathFlags(FMF);
146 
147   F = Builder.CreateFAdd(F, F);
148   EXPECT_FALSE(Builder.getFastMathFlags().any());
149 
150   FMF.setUnsafeAlgebra();
151   Builder.setFastMathFlags(FMF);
152 
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());
158 
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());
165 
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());
172 
173   Builder.clearFastMathFlags();
174 
175   F = Builder.CreateFDiv(F, F);
176   ASSERT_TRUE(isa<Instruction>(F));
177   FDiv = cast<Instruction>(F);
178   EXPECT_FALSE(FDiv->hasAllowReciprocal());
179 
180   FMF.clear();
181   FMF.setAllowReciprocal();
182   Builder.setFastMathFlags(FMF);
183 
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());
190 
191   Builder.clearFastMathFlags();
192 
193   FC = Builder.CreateFCmpOEQ(F, F);
194   ASSERT_TRUE(isa<Instruction>(FC));
195   FCmp = cast<Instruction>(FC);
196   EXPECT_FALSE(FCmp->hasAllowReciprocal());
197 
198   FMF.clear();
199   FMF.setAllowReciprocal();
200   Builder.setFastMathFlags(FMF);
201 
202   FC = Builder.CreateFCmpOEQ(F, F);
203   EXPECT_TRUE(Builder.getFastMathFlags().any());
204   EXPECT_TRUE(Builder.getFastMathFlags().AllowReciprocal);
205   ASSERT_TRUE(isa<Instruction>(FC));
206   FCmp = cast<Instruction>(FC);
207   EXPECT_TRUE(FCmp->hasAllowReciprocal());
208 
209   Builder.clearFastMathFlags();
210 
211   // Test a call with FMF.
212   auto CalleeTy = FunctionType::get(Type::getFloatTy(Ctx),
213                                     /*isVarArg=*/false);
214   auto Callee =
215       Function::Create(CalleeTy, Function::ExternalLinkage, "", M.get());
216 
217   FCall = Builder.CreateCall(Callee, None);
218   EXPECT_FALSE(FCall->hasNoNaNs());
219 
220   Value *V =
221       Function::Create(CalleeTy, Function::ExternalLinkage, "", M.get());
222   FCall = Builder.CreateCall(V, None);
223   EXPECT_FALSE(FCall->hasNoNaNs());
224 
225   FMF.clear();
226   FMF.setNoNaNs();
227   Builder.setFastMathFlags(FMF);
228 
229   FCall = Builder.CreateCall(Callee, None);
230   EXPECT_TRUE(Builder.getFastMathFlags().any());
231   EXPECT_TRUE(Builder.getFastMathFlags().NoNaNs);
232   EXPECT_TRUE(FCall->hasNoNaNs());
233 
234   FCall = Builder.CreateCall(V, None);
235   EXPECT_TRUE(Builder.getFastMathFlags().any());
236   EXPECT_TRUE(Builder.getFastMathFlags().NoNaNs);
237   EXPECT_TRUE(FCall->hasNoNaNs());
238 
239   Builder.clearFastMathFlags();
240 
241   // To test a copy, make sure that a '0' and a '1' change state.
242   F = Builder.CreateFDiv(F, F);
243   ASSERT_TRUE(isa<Instruction>(F));
244   FDiv = cast<Instruction>(F);
245   EXPECT_FALSE(FDiv->getFastMathFlags().any());
246   FDiv->setHasAllowReciprocal(true);
247   FAdd->setHasAllowReciprocal(false);
248   FDiv->copyFastMathFlags(FAdd);
249   EXPECT_TRUE(FDiv->hasNoNaNs());
250   EXPECT_FALSE(FDiv->hasAllowReciprocal());
251 
252 }
253 
TEST_F(IRBuilderTest,WrapFlags)254 TEST_F(IRBuilderTest, WrapFlags) {
255   IRBuilder<NoFolder> Builder(BB);
256 
257   // Test instructions.
258   GlobalVariable *G = new GlobalVariable(*M, Builder.getInt32Ty(), true,
259                                          GlobalValue::ExternalLinkage, nullptr);
260   Value *V = Builder.CreateLoad(G);
261   EXPECT_TRUE(
262       cast<BinaryOperator>(Builder.CreateNSWAdd(V, V))->hasNoSignedWrap());
263   EXPECT_TRUE(
264       cast<BinaryOperator>(Builder.CreateNSWMul(V, V))->hasNoSignedWrap());
265   EXPECT_TRUE(
266       cast<BinaryOperator>(Builder.CreateNSWSub(V, V))->hasNoSignedWrap());
267   EXPECT_TRUE(cast<BinaryOperator>(
268                   Builder.CreateShl(V, V, "", /* NUW */ false, /* NSW */ true))
269                   ->hasNoSignedWrap());
270 
271   EXPECT_TRUE(
272       cast<BinaryOperator>(Builder.CreateNUWAdd(V, V))->hasNoUnsignedWrap());
273   EXPECT_TRUE(
274       cast<BinaryOperator>(Builder.CreateNUWMul(V, V))->hasNoUnsignedWrap());
275   EXPECT_TRUE(
276       cast<BinaryOperator>(Builder.CreateNUWSub(V, V))->hasNoUnsignedWrap());
277   EXPECT_TRUE(cast<BinaryOperator>(
278                   Builder.CreateShl(V, V, "", /* NUW */ true, /* NSW */ false))
279                   ->hasNoUnsignedWrap());
280 
281   // Test operators created with constants.
282   Constant *C = Builder.getInt32(42);
283   EXPECT_TRUE(cast<OverflowingBinaryOperator>(Builder.CreateNSWAdd(C, C))
284                   ->hasNoSignedWrap());
285   EXPECT_TRUE(cast<OverflowingBinaryOperator>(Builder.CreateNSWSub(C, C))
286                   ->hasNoSignedWrap());
287   EXPECT_TRUE(cast<OverflowingBinaryOperator>(Builder.CreateNSWMul(C, C))
288                   ->hasNoSignedWrap());
289   EXPECT_TRUE(cast<OverflowingBinaryOperator>(
290                   Builder.CreateShl(C, C, "", /* NUW */ false, /* NSW */ true))
291                   ->hasNoSignedWrap());
292 
293   EXPECT_TRUE(cast<OverflowingBinaryOperator>(Builder.CreateNUWAdd(C, C))
294                   ->hasNoUnsignedWrap());
295   EXPECT_TRUE(cast<OverflowingBinaryOperator>(Builder.CreateNUWSub(C, C))
296                   ->hasNoUnsignedWrap());
297   EXPECT_TRUE(cast<OverflowingBinaryOperator>(Builder.CreateNUWMul(C, C))
298                   ->hasNoUnsignedWrap());
299   EXPECT_TRUE(cast<OverflowingBinaryOperator>(
300                   Builder.CreateShl(C, C, "", /* NUW */ true, /* NSW */ false))
301                   ->hasNoUnsignedWrap());
302 }
303 
TEST_F(IRBuilderTest,RAIIHelpersTest)304 TEST_F(IRBuilderTest, RAIIHelpersTest) {
305   IRBuilder<> Builder(BB);
306   EXPECT_FALSE(Builder.getFastMathFlags().allowReciprocal());
307   MDBuilder MDB(M->getContext());
308 
309   MDNode *FPMathA = MDB.createFPMath(0.01f);
310   MDNode *FPMathB = MDB.createFPMath(0.1f);
311 
312   Builder.setDefaultFPMathTag(FPMathA);
313 
314   {
315     IRBuilder<>::FastMathFlagGuard Guard(Builder);
316     FastMathFlags FMF;
317     FMF.setAllowReciprocal();
318     Builder.setFastMathFlags(FMF);
319     Builder.setDefaultFPMathTag(FPMathB);
320     EXPECT_TRUE(Builder.getFastMathFlags().allowReciprocal());
321     EXPECT_EQ(FPMathB, Builder.getDefaultFPMathTag());
322   }
323 
324   EXPECT_FALSE(Builder.getFastMathFlags().allowReciprocal());
325   EXPECT_EQ(FPMathA, Builder.getDefaultFPMathTag());
326 
327   Value *F = Builder.CreateLoad(GV);
328 
329   {
330     IRBuilder<>::InsertPointGuard Guard(Builder);
331     Builder.SetInsertPoint(cast<Instruction>(F));
332     EXPECT_EQ(F, &*Builder.GetInsertPoint());
333   }
334 
335   EXPECT_EQ(BB->end(), Builder.GetInsertPoint());
336   EXPECT_EQ(BB, Builder.GetInsertBlock());
337 }
338 
TEST_F(IRBuilderTest,DIBuilder)339 TEST_F(IRBuilderTest, DIBuilder) {
340   IRBuilder<> Builder(BB);
341   DIBuilder DIB(*M);
342   auto File = DIB.createFile("F.CBL", "/");
343   auto CU = DIB.createCompileUnit(dwarf::DW_LANG_Cobol74, "F.CBL", "/",
344                                   "llvm-cobol74", true, "", 0);
345   auto Type = DIB.createSubroutineType(DIB.getOrCreateTypeArray(None));
346   auto SP =
347       DIB.createFunction(CU, "foo", "", File, 1, Type, false, true, 1, 0, true);
348   F->setSubprogram(SP);
349   AllocaInst *I = Builder.CreateAlloca(Builder.getInt8Ty());
350   auto BarSP =
351       DIB.createFunction(CU, "bar", "", File, 1, Type, false, true, 1, 0, true);
352   auto BadScope = DIB.createLexicalBlockFile(BarSP, File, 0);
353   I->setDebugLoc(DebugLoc::get(2, 0, BadScope));
354   DIB.finalize();
355   EXPECT_TRUE(verifyModule(*M));
356 }
357 
TEST_F(IRBuilderTest,InsertExtractElement)358 TEST_F(IRBuilderTest, InsertExtractElement) {
359   IRBuilder<> Builder(BB);
360 
361   auto VecTy = VectorType::get(Builder.getInt64Ty(), 4);
362   auto Elt1 = Builder.getInt64(-1);
363   auto Elt2 = Builder.getInt64(-2);
364   Value *Vec = UndefValue::get(VecTy);
365   Vec = Builder.CreateInsertElement(Vec, Elt1, Builder.getInt8(1));
366   Vec = Builder.CreateInsertElement(Vec, Elt2, 2);
367   auto X1 = Builder.CreateExtractElement(Vec, 1);
368   auto X2 = Builder.CreateExtractElement(Vec, Builder.getInt32(2));
369   EXPECT_EQ(Elt1, X1);
370   EXPECT_EQ(Elt2, X2);
371 }
372 
TEST_F(IRBuilderTest,CreateGlobalStringPtr)373 TEST_F(IRBuilderTest, CreateGlobalStringPtr) {
374   IRBuilder<> Builder(BB);
375 
376   auto String1a = Builder.CreateGlobalStringPtr("TestString", "String1a");
377   auto String1b = Builder.CreateGlobalStringPtr("TestString", "String1b", 0);
378   auto String2 = Builder.CreateGlobalStringPtr("TestString", "String2", 1);
379   auto String3 = Builder.CreateGlobalString("TestString", "String3", 2);
380 
381   EXPECT_TRUE(String1a->getType()->getPointerAddressSpace() == 0);
382   EXPECT_TRUE(String1b->getType()->getPointerAddressSpace() == 0);
383   EXPECT_TRUE(String2->getType()->getPointerAddressSpace() == 1);
384   EXPECT_TRUE(String3->getType()->getPointerAddressSpace() == 2);
385 }
386 
TEST_F(IRBuilderTest,DebugLoc)387 TEST_F(IRBuilderTest, DebugLoc) {
388   auto CalleeTy = FunctionType::get(Type::getVoidTy(Ctx),
389                                     /*isVarArg=*/false);
390   auto Callee =
391       Function::Create(CalleeTy, Function::ExternalLinkage, "", M.get());
392 
393   DIBuilder DIB(*M);
394   auto File = DIB.createFile("tmp.cpp", "/");
395   auto CU = DIB.createCompileUnit(dwarf::DW_LANG_C_plus_plus_11, "tmp.cpp", "/",
396                                   "", true, "", 0);
397   auto SPType = DIB.createSubroutineType(DIB.getOrCreateTypeArray(None));
398   auto SP =
399       DIB.createFunction(CU, "foo", "foo", File, 1, SPType, false, true, 1);
400   DebugLoc DL1 = DILocation::get(Ctx, 2, 0, SP);
401   DebugLoc DL2 = DILocation::get(Ctx, 3, 0, SP);
402 
403   auto BB2 = BasicBlock::Create(Ctx, "bb2", F);
404   auto Br = BranchInst::Create(BB2, BB);
405   Br->setDebugLoc(DL1);
406 
407   IRBuilder<> Builder(Ctx);
408   Builder.SetInsertPoint(Br);
409   EXPECT_EQ(DL1, Builder.getCurrentDebugLocation());
410   auto Call1 = Builder.CreateCall(Callee, None);
411   EXPECT_EQ(DL1, Call1->getDebugLoc());
412 
413   Call1->setDebugLoc(DL2);
414   Builder.SetInsertPoint(Call1->getParent(), Call1->getIterator());
415   EXPECT_EQ(DL2, Builder.getCurrentDebugLocation());
416   auto Call2 = Builder.CreateCall(Callee, None);
417   EXPECT_EQ(DL2, Call2->getDebugLoc());
418 
419   DIB.finalize();
420 }
421 
TEST_F(IRBuilderTest,DIImportedEntity)422 TEST_F(IRBuilderTest, DIImportedEntity) {
423   IRBuilder<> Builder(BB);
424   DIBuilder DIB(*M);
425   auto CU = DIB.createCompileUnit(dwarf::DW_LANG_Cobol74, "F.CBL", "/",
426     "llvm-cobol74", true, "", 0);
427   DIB.createImportedDeclaration(CU, nullptr, 1);
428   DIB.createImportedDeclaration(CU, nullptr, 1);
429   DIB.createImportedModule(CU, (DIImportedEntity *)nullptr, 2);
430   DIB.createImportedModule(CU, (DIImportedEntity *)nullptr, 2);
431   DIB.finalize();
432   EXPECT_TRUE(verifyModule(*M));
433   EXPECT_TRUE(CU->getImportedEntities().size() == 2);
434 }
435 }
436