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/DIBuilder.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 "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,Intrinsics)51 TEST_F(IRBuilderTest, Intrinsics) {
52 IRBuilder<> Builder(BB);
53 Value *V;
54 CallInst *Call;
55 IntrinsicInst *II;
56
57 V = Builder.CreateLoad(GV);
58
59 Call = Builder.CreateMinNum(V, V);
60 II = cast<IntrinsicInst>(Call);
61 EXPECT_EQ(II->getIntrinsicID(), Intrinsic::minnum);
62
63 Call = Builder.CreateMaxNum(V, V);
64 II = cast<IntrinsicInst>(Call);
65 EXPECT_EQ(II->getIntrinsicID(), Intrinsic::maxnum);
66
67 Call = Builder.CreateIntrinsic(Intrinsic::readcyclecounter);
68 II = cast<IntrinsicInst>(Call);
69 EXPECT_EQ(II->getIntrinsicID(), Intrinsic::readcyclecounter);
70 }
71
TEST_F(IRBuilderTest,Lifetime)72 TEST_F(IRBuilderTest, Lifetime) {
73 IRBuilder<> Builder(BB);
74 AllocaInst *Var1 = Builder.CreateAlloca(Builder.getInt8Ty());
75 AllocaInst *Var2 = Builder.CreateAlloca(Builder.getInt32Ty());
76 AllocaInst *Var3 = Builder.CreateAlloca(Builder.getInt8Ty(),
77 Builder.getInt32(123));
78
79 CallInst *Start1 = Builder.CreateLifetimeStart(Var1);
80 CallInst *Start2 = Builder.CreateLifetimeStart(Var2);
81 CallInst *Start3 = Builder.CreateLifetimeStart(Var3, Builder.getInt64(100));
82
83 EXPECT_EQ(Start1->getArgOperand(0), Builder.getInt64(-1));
84 EXPECT_EQ(Start2->getArgOperand(0), Builder.getInt64(-1));
85 EXPECT_EQ(Start3->getArgOperand(0), Builder.getInt64(100));
86
87 EXPECT_EQ(Start1->getArgOperand(1), Var1);
88 EXPECT_NE(Start2->getArgOperand(1), Var2);
89 EXPECT_EQ(Start3->getArgOperand(1), Var3);
90
91 Value *End1 = Builder.CreateLifetimeEnd(Var1);
92 Builder.CreateLifetimeEnd(Var2);
93 Builder.CreateLifetimeEnd(Var3);
94
95 IntrinsicInst *II_Start1 = dyn_cast<IntrinsicInst>(Start1);
96 IntrinsicInst *II_End1 = dyn_cast<IntrinsicInst>(End1);
97 ASSERT_TRUE(II_Start1 != nullptr);
98 EXPECT_EQ(II_Start1->getIntrinsicID(), Intrinsic::lifetime_start);
99 ASSERT_TRUE(II_End1 != nullptr);
100 EXPECT_EQ(II_End1->getIntrinsicID(), Intrinsic::lifetime_end);
101 }
102
TEST_F(IRBuilderTest,CreateCondBr)103 TEST_F(IRBuilderTest, CreateCondBr) {
104 IRBuilder<> Builder(BB);
105 BasicBlock *TBB = BasicBlock::Create(Ctx, "", F);
106 BasicBlock *FBB = BasicBlock::Create(Ctx, "", F);
107
108 BranchInst *BI = Builder.CreateCondBr(Builder.getTrue(), TBB, FBB);
109 TerminatorInst *TI = BB->getTerminator();
110 EXPECT_EQ(BI, TI);
111 EXPECT_EQ(2u, TI->getNumSuccessors());
112 EXPECT_EQ(TBB, TI->getSuccessor(0));
113 EXPECT_EQ(FBB, TI->getSuccessor(1));
114
115 BI->eraseFromParent();
116 MDNode *Weights = MDBuilder(Ctx).createBranchWeights(42, 13);
117 BI = Builder.CreateCondBr(Builder.getTrue(), TBB, FBB, Weights);
118 TI = BB->getTerminator();
119 EXPECT_EQ(BI, TI);
120 EXPECT_EQ(2u, TI->getNumSuccessors());
121 EXPECT_EQ(TBB, TI->getSuccessor(0));
122 EXPECT_EQ(FBB, TI->getSuccessor(1));
123 EXPECT_EQ(Weights, TI->getMetadata(LLVMContext::MD_prof));
124 }
125
TEST_F(IRBuilderTest,LandingPadName)126 TEST_F(IRBuilderTest, LandingPadName) {
127 IRBuilder<> Builder(BB);
128 LandingPadInst *LP = Builder.CreateLandingPad(Builder.getInt32Ty(), 0, "LP");
129 EXPECT_EQ(LP->getName(), "LP");
130 }
131
TEST_F(IRBuilderTest,DataLayout)132 TEST_F(IRBuilderTest, DataLayout) {
133 std::unique_ptr<Module> M(new Module("test", Ctx));
134 M->setDataLayout("e-n32");
135 EXPECT_TRUE(M->getDataLayout().isLegalInteger(32));
136 M->setDataLayout("e");
137 EXPECT_FALSE(M->getDataLayout().isLegalInteger(32));
138 }
139
TEST_F(IRBuilderTest,GetIntTy)140 TEST_F(IRBuilderTest, GetIntTy) {
141 IRBuilder<> Builder(BB);
142 IntegerType *Ty1 = Builder.getInt1Ty();
143 EXPECT_EQ(Ty1, IntegerType::get(Ctx, 1));
144
145 DataLayout* DL = new DataLayout(M.get());
146 IntegerType *IntPtrTy = Builder.getIntPtrTy(*DL);
147 unsigned IntPtrBitSize = DL->getPointerSizeInBits(0);
148 EXPECT_EQ(IntPtrTy, IntegerType::get(Ctx, IntPtrBitSize));
149 delete DL;
150 }
151
TEST_F(IRBuilderTest,FastMathFlags)152 TEST_F(IRBuilderTest, FastMathFlags) {
153 IRBuilder<> Builder(BB);
154 Value *F, *FC;
155 Instruction *FDiv, *FAdd, *FCmp, *FCall;
156
157 F = Builder.CreateLoad(GV);
158 F = Builder.CreateFAdd(F, F);
159
160 EXPECT_FALSE(Builder.getFastMathFlags().any());
161 ASSERT_TRUE(isa<Instruction>(F));
162 FAdd = cast<Instruction>(F);
163 EXPECT_FALSE(FAdd->hasNoNaNs());
164
165 FastMathFlags FMF;
166 Builder.setFastMathFlags(FMF);
167
168 // By default, no flags are set.
169 F = Builder.CreateFAdd(F, F);
170 EXPECT_FALSE(Builder.getFastMathFlags().any());
171 ASSERT_TRUE(isa<Instruction>(F));
172 FAdd = cast<Instruction>(F);
173 EXPECT_FALSE(FAdd->hasNoNaNs());
174 EXPECT_FALSE(FAdd->hasNoInfs());
175 EXPECT_FALSE(FAdd->hasNoSignedZeros());
176 EXPECT_FALSE(FAdd->hasAllowReciprocal());
177 EXPECT_FALSE(FAdd->hasAllowContract());
178 EXPECT_FALSE(FAdd->hasAllowReassoc());
179 EXPECT_FALSE(FAdd->hasApproxFunc());
180
181 // Set all flags in the instruction.
182 FAdd->setFast(true);
183 EXPECT_TRUE(FAdd->hasNoNaNs());
184 EXPECT_TRUE(FAdd->hasNoInfs());
185 EXPECT_TRUE(FAdd->hasNoSignedZeros());
186 EXPECT_TRUE(FAdd->hasAllowReciprocal());
187 EXPECT_TRUE(FAdd->hasAllowContract());
188 EXPECT_TRUE(FAdd->hasAllowReassoc());
189 EXPECT_TRUE(FAdd->hasApproxFunc());
190
191 // All flags are set in the builder.
192 FMF.setFast();
193 Builder.setFastMathFlags(FMF);
194
195 F = Builder.CreateFAdd(F, F);
196 EXPECT_TRUE(Builder.getFastMathFlags().any());
197 EXPECT_TRUE(Builder.getFastMathFlags().all());
198 ASSERT_TRUE(isa<Instruction>(F));
199 FAdd = cast<Instruction>(F);
200 EXPECT_TRUE(FAdd->hasNoNaNs());
201 EXPECT_TRUE(FAdd->isFast());
202
203 // Now, try it with CreateBinOp
204 F = Builder.CreateBinOp(Instruction::FAdd, F, F);
205 EXPECT_TRUE(Builder.getFastMathFlags().any());
206 ASSERT_TRUE(isa<Instruction>(F));
207 FAdd = cast<Instruction>(F);
208 EXPECT_TRUE(FAdd->hasNoNaNs());
209 EXPECT_TRUE(FAdd->isFast());
210
211 F = Builder.CreateFDiv(F, F);
212 EXPECT_TRUE(Builder.getFastMathFlags().all());
213 ASSERT_TRUE(isa<Instruction>(F));
214 FDiv = cast<Instruction>(F);
215 EXPECT_TRUE(FDiv->hasAllowReciprocal());
216
217 // Clear all FMF in the builder.
218 Builder.clearFastMathFlags();
219
220 F = Builder.CreateFDiv(F, F);
221 ASSERT_TRUE(isa<Instruction>(F));
222 FDiv = cast<Instruction>(F);
223 EXPECT_FALSE(FDiv->hasAllowReciprocal());
224
225 // Try individual flags.
226 FMF.clear();
227 FMF.setAllowReciprocal();
228 Builder.setFastMathFlags(FMF);
229
230 F = Builder.CreateFDiv(F, F);
231 EXPECT_TRUE(Builder.getFastMathFlags().any());
232 EXPECT_TRUE(Builder.getFastMathFlags().AllowReciprocal);
233 ASSERT_TRUE(isa<Instruction>(F));
234 FDiv = cast<Instruction>(F);
235 EXPECT_TRUE(FDiv->hasAllowReciprocal());
236
237 Builder.clearFastMathFlags();
238
239 FC = Builder.CreateFCmpOEQ(F, F);
240 ASSERT_TRUE(isa<Instruction>(FC));
241 FCmp = cast<Instruction>(FC);
242 EXPECT_FALSE(FCmp->hasAllowReciprocal());
243
244 FMF.clear();
245 FMF.setAllowReciprocal();
246 Builder.setFastMathFlags(FMF);
247
248 FC = Builder.CreateFCmpOEQ(F, F);
249 EXPECT_TRUE(Builder.getFastMathFlags().any());
250 EXPECT_TRUE(Builder.getFastMathFlags().AllowReciprocal);
251 ASSERT_TRUE(isa<Instruction>(FC));
252 FCmp = cast<Instruction>(FC);
253 EXPECT_TRUE(FCmp->hasAllowReciprocal());
254
255 Builder.clearFastMathFlags();
256
257 // Test FP-contract
258 FC = Builder.CreateFAdd(F, F);
259 ASSERT_TRUE(isa<Instruction>(FC));
260 FAdd = cast<Instruction>(FC);
261 EXPECT_FALSE(FAdd->hasAllowContract());
262
263 FMF.clear();
264 FMF.setAllowContract(true);
265 Builder.setFastMathFlags(FMF);
266
267 FC = Builder.CreateFAdd(F, F);
268 EXPECT_TRUE(Builder.getFastMathFlags().any());
269 EXPECT_TRUE(Builder.getFastMathFlags().AllowContract);
270 ASSERT_TRUE(isa<Instruction>(FC));
271 FAdd = cast<Instruction>(FC);
272 EXPECT_TRUE(FAdd->hasAllowContract());
273
274 FMF.setApproxFunc();
275 Builder.clearFastMathFlags();
276 Builder.setFastMathFlags(FMF);
277 // Now 'aml' and 'contract' are set.
278 F = Builder.CreateFMul(F, F);
279 FAdd = cast<Instruction>(F);
280 EXPECT_TRUE(FAdd->hasApproxFunc());
281 EXPECT_TRUE(FAdd->hasAllowContract());
282 EXPECT_FALSE(FAdd->hasAllowReassoc());
283
284 FMF.setAllowReassoc();
285 Builder.clearFastMathFlags();
286 Builder.setFastMathFlags(FMF);
287 // Now 'aml' and 'contract' and 'reassoc' are set.
288 F = Builder.CreateFMul(F, F);
289 FAdd = cast<Instruction>(F);
290 EXPECT_TRUE(FAdd->hasApproxFunc());
291 EXPECT_TRUE(FAdd->hasAllowContract());
292 EXPECT_TRUE(FAdd->hasAllowReassoc());
293
294 // Test a call with FMF.
295 auto CalleeTy = FunctionType::get(Type::getFloatTy(Ctx),
296 /*isVarArg=*/false);
297 auto Callee =
298 Function::Create(CalleeTy, Function::ExternalLinkage, "", M.get());
299
300 FCall = Builder.CreateCall(Callee, None);
301 EXPECT_FALSE(FCall->hasNoNaNs());
302
303 Value *V =
304 Function::Create(CalleeTy, Function::ExternalLinkage, "", M.get());
305 FCall = Builder.CreateCall(V, None);
306 EXPECT_FALSE(FCall->hasNoNaNs());
307
308 FMF.clear();
309 FMF.setNoNaNs();
310 Builder.setFastMathFlags(FMF);
311
312 FCall = Builder.CreateCall(Callee, None);
313 EXPECT_TRUE(Builder.getFastMathFlags().any());
314 EXPECT_TRUE(Builder.getFastMathFlags().NoNaNs);
315 EXPECT_TRUE(FCall->hasNoNaNs());
316
317 FCall = Builder.CreateCall(V, None);
318 EXPECT_TRUE(Builder.getFastMathFlags().any());
319 EXPECT_TRUE(Builder.getFastMathFlags().NoNaNs);
320 EXPECT_TRUE(FCall->hasNoNaNs());
321
322 Builder.clearFastMathFlags();
323
324 // To test a copy, make sure that a '0' and a '1' change state.
325 F = Builder.CreateFDiv(F, F);
326 ASSERT_TRUE(isa<Instruction>(F));
327 FDiv = cast<Instruction>(F);
328 EXPECT_FALSE(FDiv->getFastMathFlags().any());
329 FDiv->setHasAllowReciprocal(true);
330 FAdd->setHasAllowReciprocal(false);
331 FAdd->setHasNoNaNs(true);
332 FDiv->copyFastMathFlags(FAdd);
333 EXPECT_TRUE(FDiv->hasNoNaNs());
334 EXPECT_FALSE(FDiv->hasAllowReciprocal());
335
336 }
337
TEST_F(IRBuilderTest,WrapFlags)338 TEST_F(IRBuilderTest, WrapFlags) {
339 IRBuilder<NoFolder> Builder(BB);
340
341 // Test instructions.
342 GlobalVariable *G = new GlobalVariable(*M, Builder.getInt32Ty(), true,
343 GlobalValue::ExternalLinkage, nullptr);
344 Value *V = Builder.CreateLoad(G);
345 EXPECT_TRUE(
346 cast<BinaryOperator>(Builder.CreateNSWAdd(V, V))->hasNoSignedWrap());
347 EXPECT_TRUE(
348 cast<BinaryOperator>(Builder.CreateNSWMul(V, V))->hasNoSignedWrap());
349 EXPECT_TRUE(
350 cast<BinaryOperator>(Builder.CreateNSWSub(V, V))->hasNoSignedWrap());
351 EXPECT_TRUE(cast<BinaryOperator>(
352 Builder.CreateShl(V, V, "", /* NUW */ false, /* NSW */ true))
353 ->hasNoSignedWrap());
354
355 EXPECT_TRUE(
356 cast<BinaryOperator>(Builder.CreateNUWAdd(V, V))->hasNoUnsignedWrap());
357 EXPECT_TRUE(
358 cast<BinaryOperator>(Builder.CreateNUWMul(V, V))->hasNoUnsignedWrap());
359 EXPECT_TRUE(
360 cast<BinaryOperator>(Builder.CreateNUWSub(V, V))->hasNoUnsignedWrap());
361 EXPECT_TRUE(cast<BinaryOperator>(
362 Builder.CreateShl(V, V, "", /* NUW */ true, /* NSW */ false))
363 ->hasNoUnsignedWrap());
364
365 // Test operators created with constants.
366 Constant *C = Builder.getInt32(42);
367 EXPECT_TRUE(cast<OverflowingBinaryOperator>(Builder.CreateNSWAdd(C, C))
368 ->hasNoSignedWrap());
369 EXPECT_TRUE(cast<OverflowingBinaryOperator>(Builder.CreateNSWSub(C, C))
370 ->hasNoSignedWrap());
371 EXPECT_TRUE(cast<OverflowingBinaryOperator>(Builder.CreateNSWMul(C, C))
372 ->hasNoSignedWrap());
373 EXPECT_TRUE(cast<OverflowingBinaryOperator>(
374 Builder.CreateShl(C, C, "", /* NUW */ false, /* NSW */ true))
375 ->hasNoSignedWrap());
376
377 EXPECT_TRUE(cast<OverflowingBinaryOperator>(Builder.CreateNUWAdd(C, C))
378 ->hasNoUnsignedWrap());
379 EXPECT_TRUE(cast<OverflowingBinaryOperator>(Builder.CreateNUWSub(C, C))
380 ->hasNoUnsignedWrap());
381 EXPECT_TRUE(cast<OverflowingBinaryOperator>(Builder.CreateNUWMul(C, C))
382 ->hasNoUnsignedWrap());
383 EXPECT_TRUE(cast<OverflowingBinaryOperator>(
384 Builder.CreateShl(C, C, "", /* NUW */ true, /* NSW */ false))
385 ->hasNoUnsignedWrap());
386 }
387
TEST_F(IRBuilderTest,RAIIHelpersTest)388 TEST_F(IRBuilderTest, RAIIHelpersTest) {
389 IRBuilder<> Builder(BB);
390 EXPECT_FALSE(Builder.getFastMathFlags().allowReciprocal());
391 MDBuilder MDB(M->getContext());
392
393 MDNode *FPMathA = MDB.createFPMath(0.01f);
394 MDNode *FPMathB = MDB.createFPMath(0.1f);
395
396 Builder.setDefaultFPMathTag(FPMathA);
397
398 {
399 IRBuilder<>::FastMathFlagGuard Guard(Builder);
400 FastMathFlags FMF;
401 FMF.setAllowReciprocal();
402 Builder.setFastMathFlags(FMF);
403 Builder.setDefaultFPMathTag(FPMathB);
404 EXPECT_TRUE(Builder.getFastMathFlags().allowReciprocal());
405 EXPECT_EQ(FPMathB, Builder.getDefaultFPMathTag());
406 }
407
408 EXPECT_FALSE(Builder.getFastMathFlags().allowReciprocal());
409 EXPECT_EQ(FPMathA, Builder.getDefaultFPMathTag());
410
411 Value *F = Builder.CreateLoad(GV);
412
413 {
414 IRBuilder<>::InsertPointGuard Guard(Builder);
415 Builder.SetInsertPoint(cast<Instruction>(F));
416 EXPECT_EQ(F, &*Builder.GetInsertPoint());
417 }
418
419 EXPECT_EQ(BB->end(), Builder.GetInsertPoint());
420 EXPECT_EQ(BB, Builder.GetInsertBlock());
421 }
422
TEST_F(IRBuilderTest,createFunction)423 TEST_F(IRBuilderTest, createFunction) {
424 IRBuilder<> Builder(BB);
425 DIBuilder DIB(*M);
426 auto File = DIB.createFile("error.swift", "/");
427 auto CU =
428 DIB.createCompileUnit(dwarf::DW_LANG_Swift, File, "swiftc", true, "", 0);
429 auto Type = DIB.createSubroutineType(DIB.getOrCreateTypeArray(None));
430 auto NoErr = DIB.createFunction(CU, "noerr", "", File, 1, Type, false, true, 1,
431 DINode::FlagZero, true);
432 EXPECT_TRUE(!NoErr->getThrownTypes());
433 auto Int = DIB.createBasicType("Int", 64, dwarf::DW_ATE_signed);
434 auto Error = DIB.getOrCreateArray({Int});
435 auto Err =
436 DIB.createFunction(CU, "err", "", File, 1, Type, false, true, 1,
437 DINode::FlagZero, true, nullptr, nullptr, Error.get());
438 EXPECT_TRUE(Err->getThrownTypes().get() == Error.get());
439 DIB.finalize();
440 }
441
TEST_F(IRBuilderTest,DIBuilder)442 TEST_F(IRBuilderTest, DIBuilder) {
443 IRBuilder<> Builder(BB);
444 DIBuilder DIB(*M);
445 auto File = DIB.createFile("F.CBL", "/");
446 auto CU = DIB.createCompileUnit(dwarf::DW_LANG_Cobol74,
447 DIB.createFile("F.CBL", "/"), "llvm-cobol74",
448 true, "", 0);
449 auto Type = DIB.createSubroutineType(DIB.getOrCreateTypeArray(None));
450 auto SP = DIB.createFunction(CU, "foo", "", File, 1, Type, false, true, 1,
451 DINode::FlagZero, true);
452 F->setSubprogram(SP);
453 AllocaInst *I = Builder.CreateAlloca(Builder.getInt8Ty());
454 auto BarSP = DIB.createFunction(CU, "bar", "", File, 1, Type, false, true, 1,
455 DINode::FlagZero, true);
456 auto BadScope = DIB.createLexicalBlockFile(BarSP, File, 0);
457 I->setDebugLoc(DebugLoc::get(2, 0, BadScope));
458 DIB.finalize();
459 EXPECT_TRUE(verifyModule(*M));
460 }
461
TEST_F(IRBuilderTest,createArtificialSubprogram)462 TEST_F(IRBuilderTest, createArtificialSubprogram) {
463 IRBuilder<> Builder(BB);
464 DIBuilder DIB(*M);
465 auto File = DIB.createFile("main.c", "/");
466 auto CU = DIB.createCompileUnit(dwarf::DW_LANG_C, File, "clang",
467 /*isOptimized=*/true, /*Flags=*/"",
468 /*Runtime Version=*/0);
469 auto Type = DIB.createSubroutineType(DIB.getOrCreateTypeArray(None));
470 auto SP = DIB.createFunction(CU, "foo", /*LinkageName=*/"", File,
471 /*LineNo=*/1, Type, /*isLocalToUnit=*/false,
472 /*isDefinition=*/true, /*ScopeLine=*/2,
473 DINode::FlagZero, /*isOptimized=*/true);
474 EXPECT_TRUE(SP->isDistinct());
475
476 F->setSubprogram(SP);
477 AllocaInst *I = Builder.CreateAlloca(Builder.getInt8Ty());
478 ReturnInst *R = Builder.CreateRetVoid();
479 I->setDebugLoc(DebugLoc::get(3, 2, SP));
480 R->setDebugLoc(DebugLoc::get(4, 2, SP));
481 DIB.finalize();
482 EXPECT_FALSE(verifyModule(*M));
483
484 Function *G = Function::Create(F->getFunctionType(),
485 Function::ExternalLinkage, "", M.get());
486 BasicBlock *GBB = BasicBlock::Create(Ctx, "", G);
487 Builder.SetInsertPoint(GBB);
488 I->removeFromParent();
489 Builder.Insert(I);
490 Builder.CreateRetVoid();
491 EXPECT_FALSE(verifyModule(*M));
492
493 DISubprogram *GSP = DIBuilder::createArtificialSubprogram(F->getSubprogram());
494 EXPECT_EQ(SP->getFile(), GSP->getFile());
495 EXPECT_EQ(SP->getType(), GSP->getType());
496 EXPECT_EQ(SP->getLine(), GSP->getLine());
497 EXPECT_EQ(SP->getScopeLine(), GSP->getScopeLine());
498 EXPECT_TRUE(GSP->isDistinct());
499
500 G->setSubprogram(GSP);
501 EXPECT_TRUE(verifyModule(*M));
502
503 auto *InlinedAtNode =
504 DILocation::getDistinct(Ctx, GSP->getScopeLine(), 0, GSP);
505 DebugLoc DL = I->getDebugLoc();
506 DenseMap<const MDNode *, MDNode *> IANodes;
507 auto IA = DebugLoc::appendInlinedAt(DL, InlinedAtNode, Ctx, IANodes);
508 auto NewDL = DebugLoc::get(DL.getLine(), DL.getCol(), DL.getScope(), IA);
509 I->setDebugLoc(NewDL);
510 EXPECT_FALSE(verifyModule(*M));
511
512 EXPECT_EQ("foo", SP->getName());
513 EXPECT_EQ("foo", GSP->getName());
514 EXPECT_FALSE(SP->isArtificial());
515 EXPECT_TRUE(GSP->isArtificial());
516 }
517
TEST_F(IRBuilderTest,InsertExtractElement)518 TEST_F(IRBuilderTest, InsertExtractElement) {
519 IRBuilder<> Builder(BB);
520
521 auto VecTy = VectorType::get(Builder.getInt64Ty(), 4);
522 auto Elt1 = Builder.getInt64(-1);
523 auto Elt2 = Builder.getInt64(-2);
524 Value *Vec = UndefValue::get(VecTy);
525 Vec = Builder.CreateInsertElement(Vec, Elt1, Builder.getInt8(1));
526 Vec = Builder.CreateInsertElement(Vec, Elt2, 2);
527 auto X1 = Builder.CreateExtractElement(Vec, 1);
528 auto X2 = Builder.CreateExtractElement(Vec, Builder.getInt32(2));
529 EXPECT_EQ(Elt1, X1);
530 EXPECT_EQ(Elt2, X2);
531 }
532
TEST_F(IRBuilderTest,CreateGlobalStringPtr)533 TEST_F(IRBuilderTest, CreateGlobalStringPtr) {
534 IRBuilder<> Builder(BB);
535
536 auto String1a = Builder.CreateGlobalStringPtr("TestString", "String1a");
537 auto String1b = Builder.CreateGlobalStringPtr("TestString", "String1b", 0);
538 auto String2 = Builder.CreateGlobalStringPtr("TestString", "String2", 1);
539 auto String3 = Builder.CreateGlobalString("TestString", "String3", 2);
540
541 EXPECT_TRUE(String1a->getType()->getPointerAddressSpace() == 0);
542 EXPECT_TRUE(String1b->getType()->getPointerAddressSpace() == 0);
543 EXPECT_TRUE(String2->getType()->getPointerAddressSpace() == 1);
544 EXPECT_TRUE(String3->getType()->getPointerAddressSpace() == 2);
545 }
546
TEST_F(IRBuilderTest,DebugLoc)547 TEST_F(IRBuilderTest, DebugLoc) {
548 auto CalleeTy = FunctionType::get(Type::getVoidTy(Ctx),
549 /*isVarArg=*/false);
550 auto Callee =
551 Function::Create(CalleeTy, Function::ExternalLinkage, "", M.get());
552
553 DIBuilder DIB(*M);
554 auto File = DIB.createFile("tmp.cpp", "/");
555 auto CU = DIB.createCompileUnit(dwarf::DW_LANG_C_plus_plus_11,
556 DIB.createFile("tmp.cpp", "/"), "", true, "",
557 0);
558 auto SPType = DIB.createSubroutineType(DIB.getOrCreateTypeArray(None));
559 auto SP =
560 DIB.createFunction(CU, "foo", "foo", File, 1, SPType, false, true, 1);
561 DebugLoc DL1 = DILocation::get(Ctx, 2, 0, SP);
562 DebugLoc DL2 = DILocation::get(Ctx, 3, 0, SP);
563
564 auto BB2 = BasicBlock::Create(Ctx, "bb2", F);
565 auto Br = BranchInst::Create(BB2, BB);
566 Br->setDebugLoc(DL1);
567
568 IRBuilder<> Builder(Ctx);
569 Builder.SetInsertPoint(Br);
570 EXPECT_EQ(DL1, Builder.getCurrentDebugLocation());
571 auto Call1 = Builder.CreateCall(Callee, None);
572 EXPECT_EQ(DL1, Call1->getDebugLoc());
573
574 Call1->setDebugLoc(DL2);
575 Builder.SetInsertPoint(Call1->getParent(), Call1->getIterator());
576 EXPECT_EQ(DL2, Builder.getCurrentDebugLocation());
577 auto Call2 = Builder.CreateCall(Callee, None);
578 EXPECT_EQ(DL2, Call2->getDebugLoc());
579
580 DIB.finalize();
581 }
582
TEST_F(IRBuilderTest,DIImportedEntity)583 TEST_F(IRBuilderTest, DIImportedEntity) {
584 IRBuilder<> Builder(BB);
585 DIBuilder DIB(*M);
586 auto F = DIB.createFile("F.CBL", "/");
587 auto CU = DIB.createCompileUnit(dwarf::DW_LANG_Cobol74,
588 F, "llvm-cobol74",
589 true, "", 0);
590 DIB.createImportedDeclaration(CU, nullptr, F, 1);
591 DIB.createImportedDeclaration(CU, nullptr, F, 1);
592 DIB.createImportedModule(CU, (DIImportedEntity *)nullptr, F, 2);
593 DIB.createImportedModule(CU, (DIImportedEntity *)nullptr, F, 2);
594 DIB.finalize();
595 EXPECT_TRUE(verifyModule(*M));
596 EXPECT_TRUE(CU->getImportedEntities().size() == 2);
597 }
598
599 // 0: #define M0 V0 <-- command line definition
600 // 0: main.c <-- main file
601 // 3: #define M1 V1 <-- M1 definition in main.c
602 // 5: #include "file.h" <-- inclusion of file.h from main.c
603 // 1: #define M2 <-- M2 definition in file.h with no value
604 // 7: #undef M1 V1 <-- M1 un-definition in main.c
TEST_F(IRBuilderTest,DIBuilderMacro)605 TEST_F(IRBuilderTest, DIBuilderMacro) {
606 IRBuilder<> Builder(BB);
607 DIBuilder DIB(*M);
608 auto File1 = DIB.createFile("main.c", "/");
609 auto File2 = DIB.createFile("file.h", "/");
610 auto CU = DIB.createCompileUnit(
611 dwarf::DW_LANG_C, DIB.createFile("main.c", "/"), "llvm-c", true, "", 0);
612 auto MDef0 =
613 DIB.createMacro(nullptr, 0, dwarf::DW_MACINFO_define, "M0", "V0");
614 auto TMF1 = DIB.createTempMacroFile(nullptr, 0, File1);
615 auto MDef1 = DIB.createMacro(TMF1, 3, dwarf::DW_MACINFO_define, "M1", "V1");
616 auto TMF2 = DIB.createTempMacroFile(TMF1, 5, File2);
617 auto MDef2 = DIB.createMacro(TMF2, 1, dwarf::DW_MACINFO_define, "M2");
618 auto MUndef1 = DIB.createMacro(TMF1, 7, dwarf::DW_MACINFO_undef, "M1");
619
620 EXPECT_EQ(dwarf::DW_MACINFO_define, MDef1->getMacinfoType());
621 EXPECT_EQ(3u, MDef1->getLine());
622 EXPECT_EQ("M1", MDef1->getName());
623 EXPECT_EQ("V1", MDef1->getValue());
624
625 EXPECT_EQ(dwarf::DW_MACINFO_undef, MUndef1->getMacinfoType());
626 EXPECT_EQ(7u, MUndef1->getLine());
627 EXPECT_EQ("M1", MUndef1->getName());
628 EXPECT_EQ("", MUndef1->getValue());
629
630 EXPECT_EQ(dwarf::DW_MACINFO_start_file, TMF2->getMacinfoType());
631 EXPECT_EQ(5u, TMF2->getLine());
632 EXPECT_EQ(File2, TMF2->getFile());
633
634 DIB.finalize();
635
636 SmallVector<Metadata *, 4> Elements;
637 Elements.push_back(MDef2);
638 auto MF2 = DIMacroFile::get(Ctx, dwarf::DW_MACINFO_start_file, 5, File2,
639 DIB.getOrCreateMacroArray(Elements));
640
641 Elements.clear();
642 Elements.push_back(MDef1);
643 Elements.push_back(MF2);
644 Elements.push_back(MUndef1);
645 auto MF1 = DIMacroFile::get(Ctx, dwarf::DW_MACINFO_start_file, 0, File1,
646 DIB.getOrCreateMacroArray(Elements));
647
648 Elements.clear();
649 Elements.push_back(MDef0);
650 Elements.push_back(MF1);
651 auto MN0 = MDTuple::get(Ctx, Elements);
652 EXPECT_EQ(MN0, CU->getRawMacros());
653
654 Elements.clear();
655 Elements.push_back(MDef1);
656 Elements.push_back(MF2);
657 Elements.push_back(MUndef1);
658 auto MN1 = MDTuple::get(Ctx, Elements);
659 EXPECT_EQ(MN1, MF1->getRawElements());
660
661 Elements.clear();
662 Elements.push_back(MDef2);
663 auto MN2 = MDTuple::get(Ctx, Elements);
664 EXPECT_EQ(MN2, MF2->getRawElements());
665 EXPECT_TRUE(verifyModule(*M));
666 }
667 }
668