1 //===- llvm/unittest/IR/IRBuilderTest.cpp - IRBuilder tests ---------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/IR/IRBuilder.h"
10 #include "llvm/IR/BasicBlock.h"
11 #include "llvm/IR/DIBuilder.h"
12 #include "llvm/IR/DataLayout.h"
13 #include "llvm/IR/Function.h"
14 #include "llvm/IR/IntrinsicInst.h"
15 #include "llvm/IR/IntrinsicsAArch64.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 Instruction *I;
55 CallInst *Call;
56 IntrinsicInst *II;
57
58 V = Builder.CreateLoad(GV->getValueType(), GV);
59 I = cast<Instruction>(Builder.CreateFAdd(V, V));
60 I->setHasNoInfs(true);
61 I->setHasNoNaNs(false);
62
63 Call = Builder.CreateMinNum(V, V);
64 II = cast<IntrinsicInst>(Call);
65 EXPECT_EQ(II->getIntrinsicID(), Intrinsic::minnum);
66
67 Call = Builder.CreateMaxNum(V, V);
68 II = cast<IntrinsicInst>(Call);
69 EXPECT_EQ(II->getIntrinsicID(), Intrinsic::maxnum);
70
71 Call = Builder.CreateMinimum(V, V);
72 II = cast<IntrinsicInst>(Call);
73 EXPECT_EQ(II->getIntrinsicID(), Intrinsic::minimum);
74
75 Call = Builder.CreateMaximum(V, V);
76 II = cast<IntrinsicInst>(Call);
77 EXPECT_EQ(II->getIntrinsicID(), Intrinsic::maximum);
78
79 Call = Builder.CreateIntrinsic(Intrinsic::readcyclecounter, {}, {});
80 II = cast<IntrinsicInst>(Call);
81 EXPECT_EQ(II->getIntrinsicID(), Intrinsic::readcyclecounter);
82
83 Call = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, V);
84 II = cast<IntrinsicInst>(Call);
85 EXPECT_EQ(II->getIntrinsicID(), Intrinsic::fabs);
86 EXPECT_FALSE(II->hasNoInfs());
87 EXPECT_FALSE(II->hasNoNaNs());
88
89 Call = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, V, I);
90 II = cast<IntrinsicInst>(Call);
91 EXPECT_EQ(II->getIntrinsicID(), Intrinsic::fabs);
92 EXPECT_TRUE(II->hasNoInfs());
93 EXPECT_FALSE(II->hasNoNaNs());
94
95 Call = Builder.CreateBinaryIntrinsic(Intrinsic::pow, V, V);
96 II = cast<IntrinsicInst>(Call);
97 EXPECT_EQ(II->getIntrinsicID(), Intrinsic::pow);
98 EXPECT_FALSE(II->hasNoInfs());
99 EXPECT_FALSE(II->hasNoNaNs());
100
101 Call = Builder.CreateBinaryIntrinsic(Intrinsic::pow, V, V, I);
102 II = cast<IntrinsicInst>(Call);
103 EXPECT_EQ(II->getIntrinsicID(), Intrinsic::pow);
104 EXPECT_TRUE(II->hasNoInfs());
105 EXPECT_FALSE(II->hasNoNaNs());
106
107 Call = Builder.CreateIntrinsic(Intrinsic::fma, {V->getType()}, {V, V, V});
108 II = cast<IntrinsicInst>(Call);
109 EXPECT_EQ(II->getIntrinsicID(), Intrinsic::fma);
110 EXPECT_FALSE(II->hasNoInfs());
111 EXPECT_FALSE(II->hasNoNaNs());
112
113 Call = Builder.CreateIntrinsic(Intrinsic::fma, {V->getType()}, {V, V, V}, I);
114 II = cast<IntrinsicInst>(Call);
115 EXPECT_EQ(II->getIntrinsicID(), Intrinsic::fma);
116 EXPECT_TRUE(II->hasNoInfs());
117 EXPECT_FALSE(II->hasNoNaNs());
118
119 Call = Builder.CreateIntrinsic(Intrinsic::fma, {V->getType()}, {V, V, V}, I);
120 II = cast<IntrinsicInst>(Call);
121 EXPECT_EQ(II->getIntrinsicID(), Intrinsic::fma);
122 EXPECT_TRUE(II->hasNoInfs());
123 EXPECT_FALSE(II->hasNoNaNs());
124
125 Call = Builder.CreateUnaryIntrinsic(Intrinsic::roundeven, V);
126 II = cast<IntrinsicInst>(Call);
127 EXPECT_EQ(II->getIntrinsicID(), Intrinsic::roundeven);
128 EXPECT_FALSE(II->hasNoInfs());
129 EXPECT_FALSE(II->hasNoNaNs());
130 }
131
TEST_F(IRBuilderTest,IntrinsicsWithScalableVectors)132 TEST_F(IRBuilderTest, IntrinsicsWithScalableVectors) {
133 IRBuilder<> Builder(BB);
134 CallInst *Call;
135 FunctionType *FTy;
136
137 // Test scalable flag isn't dropped for intrinsic that is explicitly defined
138 // with scalable vectors, e.g. LLVMType<nxv4i32>.
139 Type *SrcVecTy = VectorType::get(Builder.getHalfTy(), 8, true);
140 Type *DstVecTy = VectorType::get(Builder.getInt32Ty(), 4, true);
141 Type *PredTy = VectorType::get(Builder.getInt1Ty(), 4, true);
142
143 SmallVector<Value*, 3> ArgTys;
144 ArgTys.push_back(UndefValue::get(DstVecTy));
145 ArgTys.push_back(UndefValue::get(PredTy));
146 ArgTys.push_back(UndefValue::get(SrcVecTy));
147
148 Call = Builder.CreateIntrinsic(Intrinsic::aarch64_sve_fcvtzs_i32f16, {},
149 ArgTys, nullptr, "aarch64.sve.fcvtzs.i32f16");
150 FTy = Call->getFunctionType();
151 EXPECT_EQ(FTy->getReturnType(), DstVecTy);
152 for (unsigned i = 0; i != ArgTys.size(); ++i)
153 EXPECT_EQ(FTy->getParamType(i), ArgTys[i]->getType());
154
155 // Test scalable flag isn't dropped for intrinsic defined with
156 // LLVMScalarOrSameVectorWidth.
157
158 Type *VecTy = VectorType::get(Builder.getInt32Ty(), 4, true);
159 Type *PtrToVecTy = VecTy->getPointerTo();
160 PredTy = VectorType::get(Builder.getInt1Ty(), 4, true);
161
162 ArgTys.clear();
163 ArgTys.push_back(UndefValue::get(PtrToVecTy));
164 ArgTys.push_back(UndefValue::get(Builder.getInt32Ty()));
165 ArgTys.push_back(UndefValue::get(PredTy));
166 ArgTys.push_back(UndefValue::get(VecTy));
167
168 Call = Builder.CreateIntrinsic(Intrinsic::masked_load,
169 {VecTy, PtrToVecTy}, ArgTys,
170 nullptr, "masked.load");
171 FTy = Call->getFunctionType();
172 EXPECT_EQ(FTy->getReturnType(), VecTy);
173 for (unsigned i = 0; i != ArgTys.size(); ++i)
174 EXPECT_EQ(FTy->getParamType(i), ArgTys[i]->getType());
175 }
176
TEST_F(IRBuilderTest,ConstrainedFP)177 TEST_F(IRBuilderTest, ConstrainedFP) {
178 IRBuilder<> Builder(BB);
179 Value *V;
180 Value *VDouble;
181 Value *VInt;
182 CallInst *Call;
183 IntrinsicInst *II;
184 GlobalVariable *GVDouble = new GlobalVariable(*M, Type::getDoubleTy(Ctx),
185 true, GlobalValue::ExternalLinkage, nullptr);
186
187 V = Builder.CreateLoad(GV->getValueType(), GV);
188 VDouble = Builder.CreateLoad(GVDouble->getValueType(), GVDouble);
189
190 // See if we get constrained intrinsics instead of non-constrained
191 // instructions.
192 Builder.setIsFPConstrained(true);
193 auto Parent = BB->getParent();
194 Parent->addFnAttr(Attribute::StrictFP);
195
196 V = Builder.CreateFAdd(V, V);
197 ASSERT_TRUE(isa<IntrinsicInst>(V));
198 II = cast<IntrinsicInst>(V);
199 EXPECT_EQ(II->getIntrinsicID(), Intrinsic::experimental_constrained_fadd);
200
201 V = Builder.CreateFSub(V, V);
202 ASSERT_TRUE(isa<IntrinsicInst>(V));
203 II = cast<IntrinsicInst>(V);
204 EXPECT_EQ(II->getIntrinsicID(), Intrinsic::experimental_constrained_fsub);
205
206 V = Builder.CreateFMul(V, V);
207 ASSERT_TRUE(isa<IntrinsicInst>(V));
208 II = cast<IntrinsicInst>(V);
209 EXPECT_EQ(II->getIntrinsicID(), Intrinsic::experimental_constrained_fmul);
210
211 V = Builder.CreateFDiv(V, V);
212 ASSERT_TRUE(isa<IntrinsicInst>(V));
213 II = cast<IntrinsicInst>(V);
214 EXPECT_EQ(II->getIntrinsicID(), Intrinsic::experimental_constrained_fdiv);
215
216 V = Builder.CreateFRem(V, V);
217 ASSERT_TRUE(isa<IntrinsicInst>(V));
218 II = cast<IntrinsicInst>(V);
219 EXPECT_EQ(II->getIntrinsicID(), Intrinsic::experimental_constrained_frem);
220
221 VInt = Builder.CreateFPToUI(VDouble, Builder.getInt32Ty());
222 ASSERT_TRUE(isa<IntrinsicInst>(VInt));
223 II = cast<IntrinsicInst>(VInt);
224 EXPECT_EQ(II->getIntrinsicID(), Intrinsic::experimental_constrained_fptoui);
225
226 VInt = Builder.CreateFPToSI(VDouble, Builder.getInt32Ty());
227 ASSERT_TRUE(isa<IntrinsicInst>(VInt));
228 II = cast<IntrinsicInst>(VInt);
229 EXPECT_EQ(II->getIntrinsicID(), Intrinsic::experimental_constrained_fptosi);
230
231 VDouble = Builder.CreateUIToFP(VInt, Builder.getDoubleTy());
232 ASSERT_TRUE(isa<IntrinsicInst>(VDouble));
233 II = cast<IntrinsicInst>(VDouble);
234 EXPECT_EQ(II->getIntrinsicID(), Intrinsic::experimental_constrained_uitofp);
235
236 VDouble = Builder.CreateSIToFP(VInt, Builder.getDoubleTy());
237 ASSERT_TRUE(isa<IntrinsicInst>(VDouble));
238 II = cast<IntrinsicInst>(VDouble);
239 EXPECT_EQ(II->getIntrinsicID(), Intrinsic::experimental_constrained_sitofp);
240
241 V = Builder.CreateFPTrunc(VDouble, Type::getFloatTy(Ctx));
242 ASSERT_TRUE(isa<IntrinsicInst>(V));
243 II = cast<IntrinsicInst>(V);
244 EXPECT_EQ(II->getIntrinsicID(), Intrinsic::experimental_constrained_fptrunc);
245
246 VDouble = Builder.CreateFPExt(V, Type::getDoubleTy(Ctx));
247 ASSERT_TRUE(isa<IntrinsicInst>(VDouble));
248 II = cast<IntrinsicInst>(VDouble);
249 EXPECT_EQ(II->getIntrinsicID(), Intrinsic::experimental_constrained_fpext);
250
251 // Verify attributes on the call are created automatically.
252 AttributeSet CallAttrs = II->getAttributes().getFnAttributes();
253 EXPECT_EQ(CallAttrs.hasAttribute(Attribute::StrictFP), true);
254
255 // Verify attributes on the containing function are created when requested.
256 Builder.setConstrainedFPFunctionAttr();
257 AttributeList Attrs = BB->getParent()->getAttributes();
258 AttributeSet FnAttrs = Attrs.getFnAttributes();
259 EXPECT_EQ(FnAttrs.hasAttribute(Attribute::StrictFP), true);
260
261 // Verify the codepaths for setting and overriding the default metadata.
262 V = Builder.CreateFAdd(V, V);
263 ASSERT_TRUE(isa<ConstrainedFPIntrinsic>(V));
264 auto *CII = cast<ConstrainedFPIntrinsic>(V);
265 EXPECT_EQ(fp::ebStrict, CII->getExceptionBehavior());
266 EXPECT_EQ(RoundingMode::Dynamic, CII->getRoundingMode());
267
268 Builder.setDefaultConstrainedExcept(fp::ebIgnore);
269 Builder.setDefaultConstrainedRounding(RoundingMode::TowardPositive);
270 V = Builder.CreateFAdd(V, V);
271 CII = cast<ConstrainedFPIntrinsic>(V);
272 EXPECT_EQ(fp::ebIgnore, CII->getExceptionBehavior());
273 EXPECT_EQ(CII->getRoundingMode(), RoundingMode::TowardPositive);
274
275 Builder.setDefaultConstrainedExcept(fp::ebIgnore);
276 Builder.setDefaultConstrainedRounding(RoundingMode::NearestTiesToEven);
277 V = Builder.CreateFAdd(V, V);
278 CII = cast<ConstrainedFPIntrinsic>(V);
279 EXPECT_EQ(fp::ebIgnore, CII->getExceptionBehavior());
280 EXPECT_EQ(RoundingMode::NearestTiesToEven, CII->getRoundingMode());
281
282 Builder.setDefaultConstrainedExcept(fp::ebMayTrap);
283 Builder.setDefaultConstrainedRounding(RoundingMode::TowardNegative);
284 V = Builder.CreateFAdd(V, V);
285 CII = cast<ConstrainedFPIntrinsic>(V);
286 EXPECT_EQ(fp::ebMayTrap, CII->getExceptionBehavior());
287 EXPECT_EQ(RoundingMode::TowardNegative, CII->getRoundingMode());
288
289 Builder.setDefaultConstrainedExcept(fp::ebStrict);
290 Builder.setDefaultConstrainedRounding(RoundingMode::TowardZero);
291 V = Builder.CreateFAdd(V, V);
292 CII = cast<ConstrainedFPIntrinsic>(V);
293 EXPECT_EQ(fp::ebStrict, CII->getExceptionBehavior());
294 EXPECT_EQ(RoundingMode::TowardZero, CII->getRoundingMode());
295
296 Builder.setDefaultConstrainedExcept(fp::ebIgnore);
297 Builder.setDefaultConstrainedRounding(RoundingMode::Dynamic);
298 V = Builder.CreateFAdd(V, V);
299 CII = cast<ConstrainedFPIntrinsic>(V);
300 EXPECT_EQ(fp::ebIgnore, CII->getExceptionBehavior());
301 EXPECT_EQ(RoundingMode::Dynamic, CII->getRoundingMode());
302
303 // Now override the defaults.
304 Call = Builder.CreateConstrainedFPBinOp(
305 Intrinsic::experimental_constrained_fadd, V, V, nullptr, "", nullptr,
306 RoundingMode::TowardNegative, fp::ebMayTrap);
307 CII = cast<ConstrainedFPIntrinsic>(Call);
308 EXPECT_EQ(CII->getIntrinsicID(), Intrinsic::experimental_constrained_fadd);
309 EXPECT_EQ(fp::ebMayTrap, CII->getExceptionBehavior());
310 EXPECT_EQ(RoundingMode::TowardNegative, CII->getRoundingMode());
311
312 Builder.CreateRetVoid();
313 EXPECT_FALSE(verifyModule(*M));
314 }
315
TEST_F(IRBuilderTest,ConstrainedFPIntrinsics)316 TEST_F(IRBuilderTest, ConstrainedFPIntrinsics) {
317 IRBuilder<> Builder(BB);
318 Value *V;
319 Value *VDouble;
320 ConstrainedFPIntrinsic *CII;
321 GlobalVariable *GVDouble = new GlobalVariable(
322 *M, Type::getDoubleTy(Ctx), true, GlobalValue::ExternalLinkage, nullptr);
323 VDouble = Builder.CreateLoad(GVDouble->getValueType(), GVDouble);
324
325 Builder.setDefaultConstrainedExcept(fp::ebStrict);
326 Builder.setDefaultConstrainedRounding(RoundingMode::TowardZero);
327 Function *Fn = Intrinsic::getDeclaration(M.get(),
328 Intrinsic::experimental_constrained_roundeven, { Type::getDoubleTy(Ctx) });
329 V = Builder.CreateConstrainedFPCall(Fn, { VDouble });
330 CII = cast<ConstrainedFPIntrinsic>(V);
331 EXPECT_EQ(Intrinsic::experimental_constrained_roundeven, CII->getIntrinsicID());
332 EXPECT_EQ(fp::ebStrict, CII->getExceptionBehavior());
333 }
334
TEST_F(IRBuilderTest,ConstrainedFPFunctionCall)335 TEST_F(IRBuilderTest, ConstrainedFPFunctionCall) {
336 IRBuilder<> Builder(BB);
337
338 // Create an empty constrained FP function.
339 FunctionType *FTy = FunctionType::get(Type::getVoidTy(Ctx),
340 /*isVarArg=*/false);
341 Function *Callee =
342 Function::Create(FTy, Function::ExternalLinkage, "", M.get());
343 BasicBlock *CalleeBB = BasicBlock::Create(Ctx, "", Callee);
344 IRBuilder<> CalleeBuilder(CalleeBB);
345 CalleeBuilder.setIsFPConstrained(true);
346 CalleeBuilder.setConstrainedFPFunctionAttr();
347 CalleeBuilder.CreateRetVoid();
348
349 // Now call the empty constrained FP function.
350 Builder.setIsFPConstrained(true);
351 Builder.setConstrainedFPFunctionAttr();
352 CallInst *FCall = Builder.CreateCall(Callee, None);
353
354 // Check the attributes to verify the strictfp attribute is on the call.
355 EXPECT_TRUE(FCall->getAttributes().getFnAttributes().hasAttribute(
356 Attribute::StrictFP));
357
358 Builder.CreateRetVoid();
359 EXPECT_FALSE(verifyModule(*M));
360 }
361
TEST_F(IRBuilderTest,Lifetime)362 TEST_F(IRBuilderTest, Lifetime) {
363 IRBuilder<> Builder(BB);
364 AllocaInst *Var1 = Builder.CreateAlloca(Builder.getInt8Ty());
365 AllocaInst *Var2 = Builder.CreateAlloca(Builder.getInt32Ty());
366 AllocaInst *Var3 = Builder.CreateAlloca(Builder.getInt8Ty(),
367 Builder.getInt32(123));
368
369 CallInst *Start1 = Builder.CreateLifetimeStart(Var1);
370 CallInst *Start2 = Builder.CreateLifetimeStart(Var2);
371 CallInst *Start3 = Builder.CreateLifetimeStart(Var3, Builder.getInt64(100));
372
373 EXPECT_EQ(Start1->getArgOperand(0), Builder.getInt64(-1));
374 EXPECT_EQ(Start2->getArgOperand(0), Builder.getInt64(-1));
375 EXPECT_EQ(Start3->getArgOperand(0), Builder.getInt64(100));
376
377 EXPECT_EQ(Start1->getArgOperand(1), Var1);
378 EXPECT_NE(Start2->getArgOperand(1), Var2);
379 EXPECT_EQ(Start3->getArgOperand(1), Var3);
380
381 Value *End1 = Builder.CreateLifetimeEnd(Var1);
382 Builder.CreateLifetimeEnd(Var2);
383 Builder.CreateLifetimeEnd(Var3);
384
385 IntrinsicInst *II_Start1 = dyn_cast<IntrinsicInst>(Start1);
386 IntrinsicInst *II_End1 = dyn_cast<IntrinsicInst>(End1);
387 ASSERT_TRUE(II_Start1 != nullptr);
388 EXPECT_EQ(II_Start1->getIntrinsicID(), Intrinsic::lifetime_start);
389 ASSERT_TRUE(II_End1 != nullptr);
390 EXPECT_EQ(II_End1->getIntrinsicID(), Intrinsic::lifetime_end);
391 }
392
TEST_F(IRBuilderTest,CreateCondBr)393 TEST_F(IRBuilderTest, CreateCondBr) {
394 IRBuilder<> Builder(BB);
395 BasicBlock *TBB = BasicBlock::Create(Ctx, "", F);
396 BasicBlock *FBB = BasicBlock::Create(Ctx, "", F);
397
398 BranchInst *BI = Builder.CreateCondBr(Builder.getTrue(), TBB, FBB);
399 Instruction *TI = BB->getTerminator();
400 EXPECT_EQ(BI, TI);
401 EXPECT_EQ(2u, TI->getNumSuccessors());
402 EXPECT_EQ(TBB, TI->getSuccessor(0));
403 EXPECT_EQ(FBB, TI->getSuccessor(1));
404
405 BI->eraseFromParent();
406 MDNode *Weights = MDBuilder(Ctx).createBranchWeights(42, 13);
407 BI = Builder.CreateCondBr(Builder.getTrue(), TBB, FBB, Weights);
408 TI = BB->getTerminator();
409 EXPECT_EQ(BI, TI);
410 EXPECT_EQ(2u, TI->getNumSuccessors());
411 EXPECT_EQ(TBB, TI->getSuccessor(0));
412 EXPECT_EQ(FBB, TI->getSuccessor(1));
413 EXPECT_EQ(Weights, TI->getMetadata(LLVMContext::MD_prof));
414 }
415
TEST_F(IRBuilderTest,LandingPadName)416 TEST_F(IRBuilderTest, LandingPadName) {
417 IRBuilder<> Builder(BB);
418 LandingPadInst *LP = Builder.CreateLandingPad(Builder.getInt32Ty(), 0, "LP");
419 EXPECT_EQ(LP->getName(), "LP");
420 }
421
TEST_F(IRBuilderTest,DataLayout)422 TEST_F(IRBuilderTest, DataLayout) {
423 std::unique_ptr<Module> M(new Module("test", Ctx));
424 M->setDataLayout("e-n32");
425 EXPECT_TRUE(M->getDataLayout().isLegalInteger(32));
426 M->setDataLayout("e");
427 EXPECT_FALSE(M->getDataLayout().isLegalInteger(32));
428 }
429
TEST_F(IRBuilderTest,GetIntTy)430 TEST_F(IRBuilderTest, GetIntTy) {
431 IRBuilder<> Builder(BB);
432 IntegerType *Ty1 = Builder.getInt1Ty();
433 EXPECT_EQ(Ty1, IntegerType::get(Ctx, 1));
434
435 DataLayout* DL = new DataLayout(M.get());
436 IntegerType *IntPtrTy = Builder.getIntPtrTy(*DL);
437 unsigned IntPtrBitSize = DL->getPointerSizeInBits(0);
438 EXPECT_EQ(IntPtrTy, IntegerType::get(Ctx, IntPtrBitSize));
439 delete DL;
440 }
441
TEST_F(IRBuilderTest,UnaryOperators)442 TEST_F(IRBuilderTest, UnaryOperators) {
443 IRBuilder<NoFolder> Builder(BB);
444 Value *V = Builder.CreateLoad(GV->getValueType(), GV);
445
446 // Test CreateUnOp(X)
447 Value *U = Builder.CreateUnOp(Instruction::FNeg, V);
448 ASSERT_TRUE(isa<Instruction>(U));
449 ASSERT_TRUE(isa<FPMathOperator>(U));
450 ASSERT_TRUE(isa<UnaryOperator>(U));
451 ASSERT_FALSE(isa<BinaryOperator>(U));
452
453 // Test CreateFNegFMF(X)
454 Instruction *I = cast<Instruction>(U);
455 I->setHasNoSignedZeros(true);
456 I->setHasNoNaNs(true);
457 Value *VFMF = Builder.CreateFNegFMF(V, I);
458 Instruction *IFMF = cast<Instruction>(VFMF);
459 EXPECT_TRUE(IFMF->hasNoSignedZeros());
460 EXPECT_TRUE(IFMF->hasNoNaNs());
461 EXPECT_FALSE(IFMF->hasAllowReassoc());
462 }
463
TEST_F(IRBuilderTest,FastMathFlags)464 TEST_F(IRBuilderTest, FastMathFlags) {
465 IRBuilder<> Builder(BB);
466 Value *F, *FC;
467 Instruction *FDiv, *FAdd, *FCmp, *FCall;
468
469 F = Builder.CreateLoad(GV->getValueType(), GV);
470 F = Builder.CreateFAdd(F, F);
471
472 EXPECT_FALSE(Builder.getFastMathFlags().any());
473 ASSERT_TRUE(isa<Instruction>(F));
474 FAdd = cast<Instruction>(F);
475 EXPECT_FALSE(FAdd->hasNoNaNs());
476
477 FastMathFlags FMF;
478 Builder.setFastMathFlags(FMF);
479
480 // By default, no flags are set.
481 F = Builder.CreateFAdd(F, F);
482 EXPECT_FALSE(Builder.getFastMathFlags().any());
483 ASSERT_TRUE(isa<Instruction>(F));
484 FAdd = cast<Instruction>(F);
485 EXPECT_FALSE(FAdd->hasNoNaNs());
486 EXPECT_FALSE(FAdd->hasNoInfs());
487 EXPECT_FALSE(FAdd->hasNoSignedZeros());
488 EXPECT_FALSE(FAdd->hasAllowReciprocal());
489 EXPECT_FALSE(FAdd->hasAllowContract());
490 EXPECT_FALSE(FAdd->hasAllowReassoc());
491 EXPECT_FALSE(FAdd->hasApproxFunc());
492
493 // Set all flags in the instruction.
494 FAdd->setFast(true);
495 EXPECT_TRUE(FAdd->hasNoNaNs());
496 EXPECT_TRUE(FAdd->hasNoInfs());
497 EXPECT_TRUE(FAdd->hasNoSignedZeros());
498 EXPECT_TRUE(FAdd->hasAllowReciprocal());
499 EXPECT_TRUE(FAdd->hasAllowContract());
500 EXPECT_TRUE(FAdd->hasAllowReassoc());
501 EXPECT_TRUE(FAdd->hasApproxFunc());
502
503 // All flags are set in the builder.
504 FMF.setFast();
505 Builder.setFastMathFlags(FMF);
506
507 F = Builder.CreateFAdd(F, F);
508 EXPECT_TRUE(Builder.getFastMathFlags().any());
509 EXPECT_TRUE(Builder.getFastMathFlags().all());
510 ASSERT_TRUE(isa<Instruction>(F));
511 FAdd = cast<Instruction>(F);
512 EXPECT_TRUE(FAdd->hasNoNaNs());
513 EXPECT_TRUE(FAdd->isFast());
514
515 // Now, try it with CreateBinOp
516 F = Builder.CreateBinOp(Instruction::FAdd, F, F);
517 EXPECT_TRUE(Builder.getFastMathFlags().any());
518 ASSERT_TRUE(isa<Instruction>(F));
519 FAdd = cast<Instruction>(F);
520 EXPECT_TRUE(FAdd->hasNoNaNs());
521 EXPECT_TRUE(FAdd->isFast());
522
523 F = Builder.CreateFDiv(F, F);
524 EXPECT_TRUE(Builder.getFastMathFlags().all());
525 ASSERT_TRUE(isa<Instruction>(F));
526 FDiv = cast<Instruction>(F);
527 EXPECT_TRUE(FDiv->hasAllowReciprocal());
528
529 // Clear all FMF in the builder.
530 Builder.clearFastMathFlags();
531
532 F = Builder.CreateFDiv(F, F);
533 ASSERT_TRUE(isa<Instruction>(F));
534 FDiv = cast<Instruction>(F);
535 EXPECT_FALSE(FDiv->hasAllowReciprocal());
536
537 // Try individual flags.
538 FMF.clear();
539 FMF.setAllowReciprocal();
540 Builder.setFastMathFlags(FMF);
541
542 F = Builder.CreateFDiv(F, F);
543 EXPECT_TRUE(Builder.getFastMathFlags().any());
544 EXPECT_TRUE(Builder.getFastMathFlags().AllowReciprocal);
545 ASSERT_TRUE(isa<Instruction>(F));
546 FDiv = cast<Instruction>(F);
547 EXPECT_TRUE(FDiv->hasAllowReciprocal());
548
549 Builder.clearFastMathFlags();
550
551 FC = Builder.CreateFCmpOEQ(F, F);
552 ASSERT_TRUE(isa<Instruction>(FC));
553 FCmp = cast<Instruction>(FC);
554 EXPECT_FALSE(FCmp->hasAllowReciprocal());
555
556 FMF.clear();
557 FMF.setAllowReciprocal();
558 Builder.setFastMathFlags(FMF);
559
560 FC = Builder.CreateFCmpOEQ(F, F);
561 EXPECT_TRUE(Builder.getFastMathFlags().any());
562 EXPECT_TRUE(Builder.getFastMathFlags().AllowReciprocal);
563 ASSERT_TRUE(isa<Instruction>(FC));
564 FCmp = cast<Instruction>(FC);
565 EXPECT_TRUE(FCmp->hasAllowReciprocal());
566
567 Builder.clearFastMathFlags();
568
569 // Test FP-contract
570 FC = Builder.CreateFAdd(F, F);
571 ASSERT_TRUE(isa<Instruction>(FC));
572 FAdd = cast<Instruction>(FC);
573 EXPECT_FALSE(FAdd->hasAllowContract());
574
575 FMF.clear();
576 FMF.setAllowContract(true);
577 Builder.setFastMathFlags(FMF);
578
579 FC = Builder.CreateFAdd(F, F);
580 EXPECT_TRUE(Builder.getFastMathFlags().any());
581 EXPECT_TRUE(Builder.getFastMathFlags().AllowContract);
582 ASSERT_TRUE(isa<Instruction>(FC));
583 FAdd = cast<Instruction>(FC);
584 EXPECT_TRUE(FAdd->hasAllowContract());
585
586 FMF.setApproxFunc();
587 Builder.clearFastMathFlags();
588 Builder.setFastMathFlags(FMF);
589 // Now 'aml' and 'contract' are set.
590 F = Builder.CreateFMul(F, F);
591 FAdd = cast<Instruction>(F);
592 EXPECT_TRUE(FAdd->hasApproxFunc());
593 EXPECT_TRUE(FAdd->hasAllowContract());
594 EXPECT_FALSE(FAdd->hasAllowReassoc());
595
596 FMF.setAllowReassoc();
597 Builder.clearFastMathFlags();
598 Builder.setFastMathFlags(FMF);
599 // Now 'aml' and 'contract' and 'reassoc' are set.
600 F = Builder.CreateFMul(F, F);
601 FAdd = cast<Instruction>(F);
602 EXPECT_TRUE(FAdd->hasApproxFunc());
603 EXPECT_TRUE(FAdd->hasAllowContract());
604 EXPECT_TRUE(FAdd->hasAllowReassoc());
605
606 // Test a call with FMF.
607 auto CalleeTy = FunctionType::get(Type::getFloatTy(Ctx),
608 /*isVarArg=*/false);
609 auto Callee =
610 Function::Create(CalleeTy, Function::ExternalLinkage, "", M.get());
611
612 FCall = Builder.CreateCall(Callee, None);
613 EXPECT_FALSE(FCall->hasNoNaNs());
614
615 Function *V =
616 Function::Create(CalleeTy, Function::ExternalLinkage, "", M.get());
617 FCall = Builder.CreateCall(V, None);
618 EXPECT_FALSE(FCall->hasNoNaNs());
619
620 FMF.clear();
621 FMF.setNoNaNs();
622 Builder.setFastMathFlags(FMF);
623
624 FCall = Builder.CreateCall(Callee, None);
625 EXPECT_TRUE(Builder.getFastMathFlags().any());
626 EXPECT_TRUE(Builder.getFastMathFlags().NoNaNs);
627 EXPECT_TRUE(FCall->hasNoNaNs());
628
629 FCall = Builder.CreateCall(V, None);
630 EXPECT_TRUE(Builder.getFastMathFlags().any());
631 EXPECT_TRUE(Builder.getFastMathFlags().NoNaNs);
632 EXPECT_TRUE(FCall->hasNoNaNs());
633
634 Builder.clearFastMathFlags();
635
636 // To test a copy, make sure that a '0' and a '1' change state.
637 F = Builder.CreateFDiv(F, F);
638 ASSERT_TRUE(isa<Instruction>(F));
639 FDiv = cast<Instruction>(F);
640 EXPECT_FALSE(FDiv->getFastMathFlags().any());
641 FDiv->setHasAllowReciprocal(true);
642 FAdd->setHasAllowReciprocal(false);
643 FAdd->setHasNoNaNs(true);
644 FDiv->copyFastMathFlags(FAdd);
645 EXPECT_TRUE(FDiv->hasNoNaNs());
646 EXPECT_FALSE(FDiv->hasAllowReciprocal());
647
648 }
649
TEST_F(IRBuilderTest,WrapFlags)650 TEST_F(IRBuilderTest, WrapFlags) {
651 IRBuilder<NoFolder> Builder(BB);
652
653 // Test instructions.
654 GlobalVariable *G = new GlobalVariable(*M, Builder.getInt32Ty(), true,
655 GlobalValue::ExternalLinkage, nullptr);
656 Value *V = Builder.CreateLoad(G->getValueType(), G);
657 EXPECT_TRUE(
658 cast<BinaryOperator>(Builder.CreateNSWAdd(V, V))->hasNoSignedWrap());
659 EXPECT_TRUE(
660 cast<BinaryOperator>(Builder.CreateNSWMul(V, V))->hasNoSignedWrap());
661 EXPECT_TRUE(
662 cast<BinaryOperator>(Builder.CreateNSWSub(V, V))->hasNoSignedWrap());
663 EXPECT_TRUE(cast<BinaryOperator>(
664 Builder.CreateShl(V, V, "", /* NUW */ false, /* NSW */ true))
665 ->hasNoSignedWrap());
666
667 EXPECT_TRUE(
668 cast<BinaryOperator>(Builder.CreateNUWAdd(V, V))->hasNoUnsignedWrap());
669 EXPECT_TRUE(
670 cast<BinaryOperator>(Builder.CreateNUWMul(V, V))->hasNoUnsignedWrap());
671 EXPECT_TRUE(
672 cast<BinaryOperator>(Builder.CreateNUWSub(V, V))->hasNoUnsignedWrap());
673 EXPECT_TRUE(cast<BinaryOperator>(
674 Builder.CreateShl(V, V, "", /* NUW */ true, /* NSW */ false))
675 ->hasNoUnsignedWrap());
676
677 // Test operators created with constants.
678 Constant *C = Builder.getInt32(42);
679 EXPECT_TRUE(cast<OverflowingBinaryOperator>(Builder.CreateNSWAdd(C, C))
680 ->hasNoSignedWrap());
681 EXPECT_TRUE(cast<OverflowingBinaryOperator>(Builder.CreateNSWSub(C, C))
682 ->hasNoSignedWrap());
683 EXPECT_TRUE(cast<OverflowingBinaryOperator>(Builder.CreateNSWMul(C, C))
684 ->hasNoSignedWrap());
685 EXPECT_TRUE(cast<OverflowingBinaryOperator>(
686 Builder.CreateShl(C, C, "", /* NUW */ false, /* NSW */ true))
687 ->hasNoSignedWrap());
688
689 EXPECT_TRUE(cast<OverflowingBinaryOperator>(Builder.CreateNUWAdd(C, C))
690 ->hasNoUnsignedWrap());
691 EXPECT_TRUE(cast<OverflowingBinaryOperator>(Builder.CreateNUWSub(C, C))
692 ->hasNoUnsignedWrap());
693 EXPECT_TRUE(cast<OverflowingBinaryOperator>(Builder.CreateNUWMul(C, C))
694 ->hasNoUnsignedWrap());
695 EXPECT_TRUE(cast<OverflowingBinaryOperator>(
696 Builder.CreateShl(C, C, "", /* NUW */ true, /* NSW */ false))
697 ->hasNoUnsignedWrap());
698 }
699
TEST_F(IRBuilderTest,RAIIHelpersTest)700 TEST_F(IRBuilderTest, RAIIHelpersTest) {
701 IRBuilder<> Builder(BB);
702 EXPECT_FALSE(Builder.getFastMathFlags().allowReciprocal());
703 MDBuilder MDB(M->getContext());
704
705 MDNode *FPMathA = MDB.createFPMath(0.01f);
706 MDNode *FPMathB = MDB.createFPMath(0.1f);
707
708 Builder.setDefaultFPMathTag(FPMathA);
709
710 {
711 IRBuilder<>::FastMathFlagGuard Guard(Builder);
712 FastMathFlags FMF;
713 FMF.setAllowReciprocal();
714 Builder.setFastMathFlags(FMF);
715 Builder.setDefaultFPMathTag(FPMathB);
716 EXPECT_TRUE(Builder.getFastMathFlags().allowReciprocal());
717 EXPECT_EQ(FPMathB, Builder.getDefaultFPMathTag());
718 }
719
720 EXPECT_FALSE(Builder.getFastMathFlags().allowReciprocal());
721 EXPECT_EQ(FPMathA, Builder.getDefaultFPMathTag());
722
723 Value *F = Builder.CreateLoad(GV->getValueType(), GV);
724
725 {
726 IRBuilder<>::InsertPointGuard Guard(Builder);
727 Builder.SetInsertPoint(cast<Instruction>(F));
728 EXPECT_EQ(F, &*Builder.GetInsertPoint());
729 }
730
731 EXPECT_EQ(BB->end(), Builder.GetInsertPoint());
732 EXPECT_EQ(BB, Builder.GetInsertBlock());
733 }
734
TEST_F(IRBuilderTest,createFunction)735 TEST_F(IRBuilderTest, createFunction) {
736 IRBuilder<> Builder(BB);
737 DIBuilder DIB(*M);
738 auto File = DIB.createFile("error.swift", "/");
739 auto CU =
740 DIB.createCompileUnit(dwarf::DW_LANG_Swift, File, "swiftc", true, "", 0);
741 auto Type = DIB.createSubroutineType(DIB.getOrCreateTypeArray(None));
742 auto NoErr = DIB.createFunction(
743 CU, "noerr", "", File, 1, Type, 1, DINode::FlagZero,
744 DISubprogram::SPFlagDefinition | DISubprogram::SPFlagOptimized);
745 EXPECT_TRUE(!NoErr->getThrownTypes());
746 auto Int = DIB.createBasicType("Int", 64, dwarf::DW_ATE_signed);
747 auto Error = DIB.getOrCreateArray({Int});
748 auto Err = DIB.createFunction(
749 CU, "err", "", File, 1, Type, 1, DINode::FlagZero,
750 DISubprogram::SPFlagDefinition | DISubprogram::SPFlagOptimized, nullptr,
751 nullptr, Error.get());
752 EXPECT_TRUE(Err->getThrownTypes().get() == Error.get());
753 DIB.finalize();
754 }
755
TEST_F(IRBuilderTest,DIBuilder)756 TEST_F(IRBuilderTest, DIBuilder) {
757 IRBuilder<> Builder(BB);
758 DIBuilder DIB(*M);
759 auto File = DIB.createFile("F.CBL", "/");
760 auto CU = DIB.createCompileUnit(dwarf::DW_LANG_Cobol74,
761 DIB.createFile("F.CBL", "/"), "llvm-cobol74",
762 true, "", 0);
763 auto Type = DIB.createSubroutineType(DIB.getOrCreateTypeArray(None));
764 auto SP = DIB.createFunction(
765 CU, "foo", "", File, 1, Type, 1, DINode::FlagZero,
766 DISubprogram::SPFlagDefinition | DISubprogram::SPFlagOptimized);
767 F->setSubprogram(SP);
768 AllocaInst *I = Builder.CreateAlloca(Builder.getInt8Ty());
769 auto BarSP = DIB.createFunction(
770 CU, "bar", "", File, 1, Type, 1, DINode::FlagZero,
771 DISubprogram::SPFlagDefinition | DISubprogram::SPFlagOptimized);
772 auto BadScope = DIB.createLexicalBlockFile(BarSP, File, 0);
773 I->setDebugLoc(DebugLoc::get(2, 0, BadScope));
774 DIB.finalize();
775 EXPECT_TRUE(verifyModule(*M));
776 }
777
TEST_F(IRBuilderTest,createArtificialSubprogram)778 TEST_F(IRBuilderTest, createArtificialSubprogram) {
779 IRBuilder<> Builder(BB);
780 DIBuilder DIB(*M);
781 auto File = DIB.createFile("main.c", "/");
782 auto CU = DIB.createCompileUnit(dwarf::DW_LANG_C, File, "clang",
783 /*isOptimized=*/true, /*Flags=*/"",
784 /*Runtime Version=*/0);
785 auto Type = DIB.createSubroutineType(DIB.getOrCreateTypeArray(None));
786 auto SP = DIB.createFunction(
787 CU, "foo", /*LinkageName=*/"", File,
788 /*LineNo=*/1, Type, /*ScopeLine=*/2, DINode::FlagZero,
789 DISubprogram::SPFlagDefinition | DISubprogram::SPFlagOptimized);
790 EXPECT_TRUE(SP->isDistinct());
791
792 F->setSubprogram(SP);
793 AllocaInst *I = Builder.CreateAlloca(Builder.getInt8Ty());
794 ReturnInst *R = Builder.CreateRetVoid();
795 I->setDebugLoc(DebugLoc::get(3, 2, SP));
796 R->setDebugLoc(DebugLoc::get(4, 2, SP));
797 DIB.finalize();
798 EXPECT_FALSE(verifyModule(*M));
799
800 Function *G = Function::Create(F->getFunctionType(),
801 Function::ExternalLinkage, "", M.get());
802 BasicBlock *GBB = BasicBlock::Create(Ctx, "", G);
803 Builder.SetInsertPoint(GBB);
804 I->removeFromParent();
805 Builder.Insert(I);
806 Builder.CreateRetVoid();
807 EXPECT_FALSE(verifyModule(*M));
808
809 DISubprogram *GSP = DIBuilder::createArtificialSubprogram(F->getSubprogram());
810 EXPECT_EQ(SP->getFile(), GSP->getFile());
811 EXPECT_EQ(SP->getType(), GSP->getType());
812 EXPECT_EQ(SP->getLine(), GSP->getLine());
813 EXPECT_EQ(SP->getScopeLine(), GSP->getScopeLine());
814 EXPECT_TRUE(GSP->isDistinct());
815
816 G->setSubprogram(GSP);
817 EXPECT_TRUE(verifyModule(*M));
818
819 auto *InlinedAtNode =
820 DILocation::getDistinct(Ctx, GSP->getScopeLine(), 0, GSP);
821 DebugLoc DL = I->getDebugLoc();
822 DenseMap<const MDNode *, MDNode *> IANodes;
823 auto IA = DebugLoc::appendInlinedAt(DL, InlinedAtNode, Ctx, IANodes);
824 auto NewDL = DebugLoc::get(DL.getLine(), DL.getCol(), DL.getScope(), IA);
825 I->setDebugLoc(NewDL);
826 EXPECT_FALSE(verifyModule(*M));
827
828 EXPECT_EQ("foo", SP->getName());
829 EXPECT_EQ("foo", GSP->getName());
830 EXPECT_FALSE(SP->isArtificial());
831 EXPECT_TRUE(GSP->isArtificial());
832 }
833
TEST_F(IRBuilderTest,InsertExtractElement)834 TEST_F(IRBuilderTest, InsertExtractElement) {
835 IRBuilder<> Builder(BB);
836
837 auto VecTy = FixedVectorType::get(Builder.getInt64Ty(), 4);
838 auto Elt1 = Builder.getInt64(-1);
839 auto Elt2 = Builder.getInt64(-2);
840 Value *Vec = UndefValue::get(VecTy);
841 Vec = Builder.CreateInsertElement(Vec, Elt1, Builder.getInt8(1));
842 Vec = Builder.CreateInsertElement(Vec, Elt2, 2);
843 auto X1 = Builder.CreateExtractElement(Vec, 1);
844 auto X2 = Builder.CreateExtractElement(Vec, Builder.getInt32(2));
845 EXPECT_EQ(Elt1, X1);
846 EXPECT_EQ(Elt2, X2);
847 }
848
TEST_F(IRBuilderTest,CreateGlobalStringPtr)849 TEST_F(IRBuilderTest, CreateGlobalStringPtr) {
850 IRBuilder<> Builder(BB);
851
852 auto String1a = Builder.CreateGlobalStringPtr("TestString", "String1a");
853 auto String1b = Builder.CreateGlobalStringPtr("TestString", "String1b", 0);
854 auto String2 = Builder.CreateGlobalStringPtr("TestString", "String2", 1);
855 auto String3 = Builder.CreateGlobalString("TestString", "String3", 2);
856
857 EXPECT_TRUE(String1a->getType()->getPointerAddressSpace() == 0);
858 EXPECT_TRUE(String1b->getType()->getPointerAddressSpace() == 0);
859 EXPECT_TRUE(String2->getType()->getPointerAddressSpace() == 1);
860 EXPECT_TRUE(String3->getType()->getPointerAddressSpace() == 2);
861 }
862
TEST_F(IRBuilderTest,DebugLoc)863 TEST_F(IRBuilderTest, DebugLoc) {
864 auto CalleeTy = FunctionType::get(Type::getVoidTy(Ctx),
865 /*isVarArg=*/false);
866 auto Callee =
867 Function::Create(CalleeTy, Function::ExternalLinkage, "", M.get());
868
869 DIBuilder DIB(*M);
870 auto File = DIB.createFile("tmp.cpp", "/");
871 auto CU = DIB.createCompileUnit(dwarf::DW_LANG_C_plus_plus_11,
872 DIB.createFile("tmp.cpp", "/"), "", true, "",
873 0);
874 auto SPType = DIB.createSubroutineType(DIB.getOrCreateTypeArray(None));
875 auto SP =
876 DIB.createFunction(CU, "foo", "foo", File, 1, SPType, 1, DINode::FlagZero,
877 DISubprogram::SPFlagDefinition);
878 DebugLoc DL1 = DILocation::get(Ctx, 2, 0, SP);
879 DebugLoc DL2 = DILocation::get(Ctx, 3, 0, SP);
880
881 auto BB2 = BasicBlock::Create(Ctx, "bb2", F);
882 auto Br = BranchInst::Create(BB2, BB);
883 Br->setDebugLoc(DL1);
884
885 IRBuilder<> Builder(Ctx);
886 Builder.SetInsertPoint(Br);
887 EXPECT_EQ(DL1, Builder.getCurrentDebugLocation());
888 auto Call1 = Builder.CreateCall(Callee, None);
889 EXPECT_EQ(DL1, Call1->getDebugLoc());
890
891 Call1->setDebugLoc(DL2);
892 Builder.SetInsertPoint(Call1->getParent(), Call1->getIterator());
893 EXPECT_EQ(DL2, Builder.getCurrentDebugLocation());
894 auto Call2 = Builder.CreateCall(Callee, None);
895 EXPECT_EQ(DL2, Call2->getDebugLoc());
896
897 DIB.finalize();
898 }
899
TEST_F(IRBuilderTest,DIImportedEntity)900 TEST_F(IRBuilderTest, DIImportedEntity) {
901 IRBuilder<> Builder(BB);
902 DIBuilder DIB(*M);
903 auto F = DIB.createFile("F.CBL", "/");
904 auto CU = DIB.createCompileUnit(dwarf::DW_LANG_Cobol74,
905 F, "llvm-cobol74",
906 true, "", 0);
907 DIB.createImportedDeclaration(CU, nullptr, F, 1);
908 DIB.createImportedDeclaration(CU, nullptr, F, 1);
909 DIB.createImportedModule(CU, (DIImportedEntity *)nullptr, F, 2);
910 DIB.createImportedModule(CU, (DIImportedEntity *)nullptr, F, 2);
911 DIB.finalize();
912 EXPECT_TRUE(verifyModule(*M));
913 EXPECT_TRUE(CU->getImportedEntities().size() == 2);
914 }
915
916 // 0: #define M0 V0 <-- command line definition
917 // 0: main.c <-- main file
918 // 3: #define M1 V1 <-- M1 definition in main.c
919 // 5: #include "file.h" <-- inclusion of file.h from main.c
920 // 1: #define M2 <-- M2 definition in file.h with no value
921 // 7: #undef M1 V1 <-- M1 un-definition in main.c
TEST_F(IRBuilderTest,DIBuilderMacro)922 TEST_F(IRBuilderTest, DIBuilderMacro) {
923 IRBuilder<> Builder(BB);
924 DIBuilder DIB(*M);
925 auto File1 = DIB.createFile("main.c", "/");
926 auto File2 = DIB.createFile("file.h", "/");
927 auto CU = DIB.createCompileUnit(
928 dwarf::DW_LANG_C, DIB.createFile("main.c", "/"), "llvm-c", true, "", 0);
929 auto MDef0 =
930 DIB.createMacro(nullptr, 0, dwarf::DW_MACINFO_define, "M0", "V0");
931 auto TMF1 = DIB.createTempMacroFile(nullptr, 0, File1);
932 auto MDef1 = DIB.createMacro(TMF1, 3, dwarf::DW_MACINFO_define, "M1", "V1");
933 auto TMF2 = DIB.createTempMacroFile(TMF1, 5, File2);
934 auto MDef2 = DIB.createMacro(TMF2, 1, dwarf::DW_MACINFO_define, "M2");
935 auto MUndef1 = DIB.createMacro(TMF1, 7, dwarf::DW_MACINFO_undef, "M1");
936
937 EXPECT_EQ(dwarf::DW_MACINFO_define, MDef1->getMacinfoType());
938 EXPECT_EQ(3u, MDef1->getLine());
939 EXPECT_EQ("M1", MDef1->getName());
940 EXPECT_EQ("V1", MDef1->getValue());
941
942 EXPECT_EQ(dwarf::DW_MACINFO_undef, MUndef1->getMacinfoType());
943 EXPECT_EQ(7u, MUndef1->getLine());
944 EXPECT_EQ("M1", MUndef1->getName());
945 EXPECT_EQ("", MUndef1->getValue());
946
947 EXPECT_EQ(dwarf::DW_MACINFO_start_file, TMF2->getMacinfoType());
948 EXPECT_EQ(5u, TMF2->getLine());
949 EXPECT_EQ(File2, TMF2->getFile());
950
951 DIB.finalize();
952
953 SmallVector<Metadata *, 4> Elements;
954 Elements.push_back(MDef2);
955 auto MF2 = DIMacroFile::get(Ctx, dwarf::DW_MACINFO_start_file, 5, File2,
956 DIB.getOrCreateMacroArray(Elements));
957
958 Elements.clear();
959 Elements.push_back(MDef1);
960 Elements.push_back(MF2);
961 Elements.push_back(MUndef1);
962 auto MF1 = DIMacroFile::get(Ctx, dwarf::DW_MACINFO_start_file, 0, File1,
963 DIB.getOrCreateMacroArray(Elements));
964
965 Elements.clear();
966 Elements.push_back(MDef0);
967 Elements.push_back(MF1);
968 auto MN0 = MDTuple::get(Ctx, Elements);
969 EXPECT_EQ(MN0, CU->getRawMacros());
970
971 Elements.clear();
972 Elements.push_back(MDef1);
973 Elements.push_back(MF2);
974 Elements.push_back(MUndef1);
975 auto MN1 = MDTuple::get(Ctx, Elements);
976 EXPECT_EQ(MN1, MF1->getRawElements());
977
978 Elements.clear();
979 Elements.push_back(MDef2);
980 auto MN2 = MDTuple::get(Ctx, Elements);
981 EXPECT_EQ(MN2, MF2->getRawElements());
982 EXPECT_TRUE(verifyModule(*M));
983 }
984
TEST_F(IRBuilderTest,NoFolderNames)985 TEST_F(IRBuilderTest, NoFolderNames) {
986 IRBuilder<NoFolder> Builder(BB);
987 auto *Add =
988 Builder.CreateAdd(Builder.getInt32(1), Builder.getInt32(2), "add");
989 EXPECT_EQ(Add->getName(), "add");
990 }
991 }
992