[docs] Fix build-docs.sh
[llvm-project.git] / llvm / unittests / IR / IRBuilderTest.cpp
blobca08329e500b62fd20f9b41ae8ac88ef74176de7
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 //===----------------------------------------------------------------------===//
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 "gmock/gmock.h"
22 #include "gtest/gtest.h"
24 using namespace llvm;
25 using ::testing::UnorderedElementsAre;
27 namespace {
29 class IRBuilderTest : public testing::Test {
30 protected:
31 void SetUp() override {
32 M.reset(new Module("MyModule", Ctx));
33 FunctionType *FTy = FunctionType::get(Type::getVoidTy(Ctx),
34 /*isVarArg=*/false);
35 F = Function::Create(FTy, Function::ExternalLinkage, "", M.get());
36 BB = BasicBlock::Create(Ctx, "", F);
37 GV = new GlobalVariable(*M, Type::getFloatTy(Ctx), true,
38 GlobalValue::ExternalLinkage, nullptr);
41 void TearDown() override {
42 BB = nullptr;
43 M.reset();
46 LLVMContext Ctx;
47 std::unique_ptr<Module> M;
48 Function *F;
49 BasicBlock *BB;
50 GlobalVariable *GV;
53 TEST_F(IRBuilderTest, Intrinsics) {
54 IRBuilder<> Builder(BB);
55 Value *V;
56 Instruction *I;
57 CallInst *Call;
58 IntrinsicInst *II;
60 V = Builder.CreateLoad(GV->getValueType(), GV);
61 I = cast<Instruction>(Builder.CreateFAdd(V, V));
62 I->setHasNoInfs(true);
63 I->setHasNoNaNs(false);
65 Call = Builder.CreateMinNum(V, V);
66 II = cast<IntrinsicInst>(Call);
67 EXPECT_EQ(II->getIntrinsicID(), Intrinsic::minnum);
69 Call = Builder.CreateMaxNum(V, V);
70 II = cast<IntrinsicInst>(Call);
71 EXPECT_EQ(II->getIntrinsicID(), Intrinsic::maxnum);
73 Call = Builder.CreateMinimum(V, V);
74 II = cast<IntrinsicInst>(Call);
75 EXPECT_EQ(II->getIntrinsicID(), Intrinsic::minimum);
77 Call = Builder.CreateMaximum(V, V);
78 II = cast<IntrinsicInst>(Call);
79 EXPECT_EQ(II->getIntrinsicID(), Intrinsic::maximum);
81 Call = Builder.CreateIntrinsic(Intrinsic::readcyclecounter, {}, {});
82 II = cast<IntrinsicInst>(Call);
83 EXPECT_EQ(II->getIntrinsicID(), Intrinsic::readcyclecounter);
85 Call = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, V);
86 II = cast<IntrinsicInst>(Call);
87 EXPECT_EQ(II->getIntrinsicID(), Intrinsic::fabs);
88 EXPECT_FALSE(II->hasNoInfs());
89 EXPECT_FALSE(II->hasNoNaNs());
91 Call = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, V, I);
92 II = cast<IntrinsicInst>(Call);
93 EXPECT_EQ(II->getIntrinsicID(), Intrinsic::fabs);
94 EXPECT_TRUE(II->hasNoInfs());
95 EXPECT_FALSE(II->hasNoNaNs());
97 Call = Builder.CreateBinaryIntrinsic(Intrinsic::pow, V, V);
98 II = cast<IntrinsicInst>(Call);
99 EXPECT_EQ(II->getIntrinsicID(), Intrinsic::pow);
100 EXPECT_FALSE(II->hasNoInfs());
101 EXPECT_FALSE(II->hasNoNaNs());
103 Call = Builder.CreateBinaryIntrinsic(Intrinsic::pow, V, V, I);
104 II = cast<IntrinsicInst>(Call);
105 EXPECT_EQ(II->getIntrinsicID(), Intrinsic::pow);
106 EXPECT_TRUE(II->hasNoInfs());
107 EXPECT_FALSE(II->hasNoNaNs());
109 Call = Builder.CreateIntrinsic(Intrinsic::fma, {V->getType()}, {V, V, V});
110 II = cast<IntrinsicInst>(Call);
111 EXPECT_EQ(II->getIntrinsicID(), Intrinsic::fma);
112 EXPECT_FALSE(II->hasNoInfs());
113 EXPECT_FALSE(II->hasNoNaNs());
115 Call = Builder.CreateIntrinsic(Intrinsic::fma, {V->getType()}, {V, V, V}, I);
116 II = cast<IntrinsicInst>(Call);
117 EXPECT_EQ(II->getIntrinsicID(), Intrinsic::fma);
118 EXPECT_TRUE(II->hasNoInfs());
119 EXPECT_FALSE(II->hasNoNaNs());
121 Call = Builder.CreateIntrinsic(Intrinsic::fma, {V->getType()}, {V, V, V}, I);
122 II = cast<IntrinsicInst>(Call);
123 EXPECT_EQ(II->getIntrinsicID(), Intrinsic::fma);
124 EXPECT_TRUE(II->hasNoInfs());
125 EXPECT_FALSE(II->hasNoNaNs());
127 Call = Builder.CreateUnaryIntrinsic(Intrinsic::roundeven, V);
128 II = cast<IntrinsicInst>(Call);
129 EXPECT_EQ(II->getIntrinsicID(), Intrinsic::roundeven);
130 EXPECT_FALSE(II->hasNoInfs());
131 EXPECT_FALSE(II->hasNoNaNs());
133 Call = Builder.CreateIntrinsic(
134 Intrinsic::set_rounding, {},
135 {Builder.getInt32(static_cast<uint32_t>(RoundingMode::TowardZero))});
136 II = cast<IntrinsicInst>(Call);
137 EXPECT_EQ(II->getIntrinsicID(), Intrinsic::set_rounding);
140 TEST_F(IRBuilderTest, IntrinsicMangling) {
141 IRBuilder<> Builder(BB);
142 Type *VoidTy = Builder.getVoidTy();
143 Type *Int64Ty = Builder.getInt64Ty();
144 Value *Int64Val = Builder.getInt64(0);
145 Value *DoubleVal = PoisonValue::get(Builder.getDoubleTy());
146 CallInst *Call;
148 // Mangled return type, no arguments.
149 Call = Builder.CreateIntrinsic(Int64Ty, Intrinsic::coro_size, {});
150 EXPECT_EQ(Call->getCalledFunction()->getName(), "llvm.coro.size.i64");
152 // Void return type, mangled argument type.
153 Call =
154 Builder.CreateIntrinsic(VoidTy, Intrinsic::set_loop_iterations, Int64Val);
155 EXPECT_EQ(Call->getCalledFunction()->getName(),
156 "llvm.set.loop.iterations.i64");
158 // Mangled return type and argument type.
159 Call = Builder.CreateIntrinsic(Int64Ty, Intrinsic::lround, DoubleVal);
160 EXPECT_EQ(Call->getCalledFunction()->getName(), "llvm.lround.i64.f64");
163 TEST_F(IRBuilderTest, IntrinsicsWithScalableVectors) {
164 IRBuilder<> Builder(BB);
165 CallInst *Call;
166 FunctionType *FTy;
168 // Test scalable flag isn't dropped for intrinsic that is explicitly defined
169 // with scalable vectors, e.g. LLVMType<nxv4i32>.
170 Type *SrcVecTy = VectorType::get(Builder.getHalfTy(), 8, true);
171 Type *DstVecTy = VectorType::get(Builder.getInt32Ty(), 4, true);
172 Type *PredTy = VectorType::get(Builder.getInt1Ty(), 4, true);
174 SmallVector<Value*, 3> ArgTys;
175 ArgTys.push_back(UndefValue::get(DstVecTy));
176 ArgTys.push_back(UndefValue::get(PredTy));
177 ArgTys.push_back(UndefValue::get(SrcVecTy));
179 Call = Builder.CreateIntrinsic(Intrinsic::aarch64_sve_fcvtzs_i32f16, {},
180 ArgTys, nullptr, "aarch64.sve.fcvtzs.i32f16");
181 FTy = Call->getFunctionType();
182 EXPECT_EQ(FTy->getReturnType(), DstVecTy);
183 for (unsigned i = 0; i != ArgTys.size(); ++i)
184 EXPECT_EQ(FTy->getParamType(i), ArgTys[i]->getType());
186 // Test scalable flag isn't dropped for intrinsic defined with
187 // LLVMScalarOrSameVectorWidth.
189 Type *VecTy = VectorType::get(Builder.getInt32Ty(), 4, true);
190 Type *PtrToVecTy = VecTy->getPointerTo();
191 PredTy = VectorType::get(Builder.getInt1Ty(), 4, true);
193 ArgTys.clear();
194 ArgTys.push_back(UndefValue::get(PtrToVecTy));
195 ArgTys.push_back(UndefValue::get(Builder.getInt32Ty()));
196 ArgTys.push_back(UndefValue::get(PredTy));
197 ArgTys.push_back(UndefValue::get(VecTy));
199 Call = Builder.CreateIntrinsic(Intrinsic::masked_load,
200 {VecTy, PtrToVecTy}, ArgTys,
201 nullptr, "masked.load");
202 FTy = Call->getFunctionType();
203 EXPECT_EQ(FTy->getReturnType(), VecTy);
204 for (unsigned i = 0; i != ArgTys.size(); ++i)
205 EXPECT_EQ(FTy->getParamType(i), ArgTys[i]->getType());
208 TEST_F(IRBuilderTest, CreateVScale) {
209 IRBuilder<> Builder(BB);
211 Constant *Zero = Builder.getInt32(0);
212 Value *VScale = Builder.CreateVScale(Zero);
213 EXPECT_TRUE(isa<ConstantInt>(VScale) && cast<ConstantInt>(VScale)->isZero());
216 TEST_F(IRBuilderTest, CreateStepVector) {
217 IRBuilder<> Builder(BB);
219 // Fixed width vectors
220 Type *DstVecTy = VectorType::get(Builder.getInt32Ty(), 4, false);
221 Value *StepVec = Builder.CreateStepVector(DstVecTy);
222 EXPECT_TRUE(isa<Constant>(StepVec));
223 EXPECT_EQ(StepVec->getType(), DstVecTy);
225 const auto *VectorValue = cast<Constant>(StepVec);
226 for (unsigned i = 0; i < 4; i++) {
227 EXPECT_TRUE(isa<ConstantInt>(VectorValue->getAggregateElement(i)));
228 ConstantInt *El = cast<ConstantInt>(VectorValue->getAggregateElement(i));
229 EXPECT_EQ(El->getValue(), i);
232 // Scalable vectors
233 DstVecTy = VectorType::get(Builder.getInt32Ty(), 4, true);
234 StepVec = Builder.CreateStepVector(DstVecTy);
235 EXPECT_TRUE(isa<CallInst>(StepVec));
236 CallInst *Call = cast<CallInst>(StepVec);
237 FunctionType *FTy = Call->getFunctionType();
238 EXPECT_EQ(FTy->getReturnType(), DstVecTy);
239 EXPECT_EQ(Call->getIntrinsicID(), Intrinsic::experimental_stepvector);
242 TEST_F(IRBuilderTest, CreateStepVectorI3) {
243 IRBuilder<> Builder(BB);
245 // Scalable vectors
246 Type *DstVecTy = VectorType::get(IntegerType::get(Ctx, 3), 2, true);
247 Type *VecI8Ty = VectorType::get(Builder.getInt8Ty(), 2, true);
248 Value *StepVec = Builder.CreateStepVector(DstVecTy);
249 EXPECT_TRUE(isa<TruncInst>(StepVec));
250 TruncInst *Trunc = cast<TruncInst>(StepVec);
251 EXPECT_EQ(Trunc->getDestTy(), DstVecTy);
252 EXPECT_EQ(Trunc->getSrcTy(), VecI8Ty);
253 EXPECT_TRUE(isa<CallInst>(Trunc->getOperand(0)));
255 CallInst *Call = cast<CallInst>(Trunc->getOperand(0));
256 FunctionType *FTy = Call->getFunctionType();
257 EXPECT_EQ(FTy->getReturnType(), VecI8Ty);
258 EXPECT_EQ(Call->getIntrinsicID(), Intrinsic::experimental_stepvector);
261 TEST_F(IRBuilderTest, ConstrainedFP) {
262 IRBuilder<> Builder(BB);
263 Value *V;
264 Value *VDouble;
265 Value *VInt;
266 CallInst *Call;
267 IntrinsicInst *II;
268 GlobalVariable *GVDouble = new GlobalVariable(*M, Type::getDoubleTy(Ctx),
269 true, GlobalValue::ExternalLinkage, nullptr);
271 V = Builder.CreateLoad(GV->getValueType(), GV);
272 VDouble = Builder.CreateLoad(GVDouble->getValueType(), GVDouble);
274 // See if we get constrained intrinsics instead of non-constrained
275 // instructions.
276 Builder.setIsFPConstrained(true);
277 auto Parent = BB->getParent();
278 Parent->addFnAttr(Attribute::StrictFP);
280 V = Builder.CreateFAdd(V, V);
281 ASSERT_TRUE(isa<IntrinsicInst>(V));
282 II = cast<IntrinsicInst>(V);
283 EXPECT_EQ(II->getIntrinsicID(), Intrinsic::experimental_constrained_fadd);
285 V = Builder.CreateFSub(V, V);
286 ASSERT_TRUE(isa<IntrinsicInst>(V));
287 II = cast<IntrinsicInst>(V);
288 EXPECT_EQ(II->getIntrinsicID(), Intrinsic::experimental_constrained_fsub);
290 V = Builder.CreateFMul(V, V);
291 ASSERT_TRUE(isa<IntrinsicInst>(V));
292 II = cast<IntrinsicInst>(V);
293 EXPECT_EQ(II->getIntrinsicID(), Intrinsic::experimental_constrained_fmul);
295 V = Builder.CreateFDiv(V, V);
296 ASSERT_TRUE(isa<IntrinsicInst>(V));
297 II = cast<IntrinsicInst>(V);
298 EXPECT_EQ(II->getIntrinsicID(), Intrinsic::experimental_constrained_fdiv);
300 V = Builder.CreateFRem(V, V);
301 ASSERT_TRUE(isa<IntrinsicInst>(V));
302 II = cast<IntrinsicInst>(V);
303 EXPECT_EQ(II->getIntrinsicID(), Intrinsic::experimental_constrained_frem);
305 VInt = Builder.CreateFPToUI(VDouble, Builder.getInt32Ty());
306 ASSERT_TRUE(isa<IntrinsicInst>(VInt));
307 II = cast<IntrinsicInst>(VInt);
308 EXPECT_EQ(II->getIntrinsicID(), Intrinsic::experimental_constrained_fptoui);
310 VInt = Builder.CreateFPToSI(VDouble, Builder.getInt32Ty());
311 ASSERT_TRUE(isa<IntrinsicInst>(VInt));
312 II = cast<IntrinsicInst>(VInt);
313 EXPECT_EQ(II->getIntrinsicID(), Intrinsic::experimental_constrained_fptosi);
315 VDouble = Builder.CreateUIToFP(VInt, Builder.getDoubleTy());
316 ASSERT_TRUE(isa<IntrinsicInst>(VDouble));
317 II = cast<IntrinsicInst>(VDouble);
318 EXPECT_EQ(II->getIntrinsicID(), Intrinsic::experimental_constrained_uitofp);
320 VDouble = Builder.CreateSIToFP(VInt, Builder.getDoubleTy());
321 ASSERT_TRUE(isa<IntrinsicInst>(VDouble));
322 II = cast<IntrinsicInst>(VDouble);
323 EXPECT_EQ(II->getIntrinsicID(), Intrinsic::experimental_constrained_sitofp);
325 V = Builder.CreateFPTrunc(VDouble, Type::getFloatTy(Ctx));
326 ASSERT_TRUE(isa<IntrinsicInst>(V));
327 II = cast<IntrinsicInst>(V);
328 EXPECT_EQ(II->getIntrinsicID(), Intrinsic::experimental_constrained_fptrunc);
330 VDouble = Builder.CreateFPExt(V, Type::getDoubleTy(Ctx));
331 ASSERT_TRUE(isa<IntrinsicInst>(VDouble));
332 II = cast<IntrinsicInst>(VDouble);
333 EXPECT_EQ(II->getIntrinsicID(), Intrinsic::experimental_constrained_fpext);
335 // Verify attributes on the call are created automatically.
336 AttributeSet CallAttrs = II->getAttributes().getFnAttrs();
337 EXPECT_EQ(CallAttrs.hasAttribute(Attribute::StrictFP), true);
339 // Verify attributes on the containing function are created when requested.
340 Builder.setConstrainedFPFunctionAttr();
341 AttributeList Attrs = BB->getParent()->getAttributes();
342 AttributeSet FnAttrs = Attrs.getFnAttrs();
343 EXPECT_EQ(FnAttrs.hasAttribute(Attribute::StrictFP), true);
345 // Verify the codepaths for setting and overriding the default metadata.
346 V = Builder.CreateFAdd(V, V);
347 ASSERT_TRUE(isa<ConstrainedFPIntrinsic>(V));
348 auto *CII = cast<ConstrainedFPIntrinsic>(V);
349 EXPECT_EQ(fp::ebStrict, CII->getExceptionBehavior());
350 EXPECT_EQ(RoundingMode::Dynamic, CII->getRoundingMode());
352 Builder.setDefaultConstrainedExcept(fp::ebIgnore);
353 Builder.setDefaultConstrainedRounding(RoundingMode::TowardPositive);
354 V = Builder.CreateFAdd(V, V);
355 CII = cast<ConstrainedFPIntrinsic>(V);
356 EXPECT_EQ(fp::ebIgnore, CII->getExceptionBehavior());
357 EXPECT_EQ(CII->getRoundingMode(), RoundingMode::TowardPositive);
359 Builder.setDefaultConstrainedExcept(fp::ebIgnore);
360 Builder.setDefaultConstrainedRounding(RoundingMode::NearestTiesToEven);
361 V = Builder.CreateFAdd(V, V);
362 CII = cast<ConstrainedFPIntrinsic>(V);
363 EXPECT_EQ(fp::ebIgnore, CII->getExceptionBehavior());
364 EXPECT_EQ(RoundingMode::NearestTiesToEven, CII->getRoundingMode());
366 Builder.setDefaultConstrainedExcept(fp::ebMayTrap);
367 Builder.setDefaultConstrainedRounding(RoundingMode::TowardNegative);
368 V = Builder.CreateFAdd(V, V);
369 CII = cast<ConstrainedFPIntrinsic>(V);
370 EXPECT_EQ(fp::ebMayTrap, CII->getExceptionBehavior());
371 EXPECT_EQ(RoundingMode::TowardNegative, CII->getRoundingMode());
373 Builder.setDefaultConstrainedExcept(fp::ebStrict);
374 Builder.setDefaultConstrainedRounding(RoundingMode::TowardZero);
375 V = Builder.CreateFAdd(V, V);
376 CII = cast<ConstrainedFPIntrinsic>(V);
377 EXPECT_EQ(fp::ebStrict, CII->getExceptionBehavior());
378 EXPECT_EQ(RoundingMode::TowardZero, CII->getRoundingMode());
380 Builder.setDefaultConstrainedExcept(fp::ebIgnore);
381 Builder.setDefaultConstrainedRounding(RoundingMode::Dynamic);
382 V = Builder.CreateFAdd(V, V);
383 CII = cast<ConstrainedFPIntrinsic>(V);
384 EXPECT_EQ(fp::ebIgnore, CII->getExceptionBehavior());
385 EXPECT_EQ(RoundingMode::Dynamic, CII->getRoundingMode());
387 // Now override the defaults.
388 Call = Builder.CreateConstrainedFPBinOp(
389 Intrinsic::experimental_constrained_fadd, V, V, nullptr, "", nullptr,
390 RoundingMode::TowardNegative, fp::ebMayTrap);
391 CII = cast<ConstrainedFPIntrinsic>(Call);
392 EXPECT_EQ(CII->getIntrinsicID(), Intrinsic::experimental_constrained_fadd);
393 EXPECT_EQ(fp::ebMayTrap, CII->getExceptionBehavior());
394 EXPECT_EQ(RoundingMode::TowardNegative, CII->getRoundingMode());
396 Builder.CreateRetVoid();
397 EXPECT_FALSE(verifyModule(*M));
400 TEST_F(IRBuilderTest, ConstrainedFPIntrinsics) {
401 IRBuilder<> Builder(BB);
402 Value *V;
403 Value *VDouble;
404 ConstrainedFPIntrinsic *CII;
405 GlobalVariable *GVDouble = new GlobalVariable(
406 *M, Type::getDoubleTy(Ctx), true, GlobalValue::ExternalLinkage, nullptr);
407 VDouble = Builder.CreateLoad(GVDouble->getValueType(), GVDouble);
409 Builder.setDefaultConstrainedExcept(fp::ebStrict);
410 Builder.setDefaultConstrainedRounding(RoundingMode::TowardZero);
411 Function *Fn = Intrinsic::getDeclaration(M.get(),
412 Intrinsic::experimental_constrained_roundeven, { Type::getDoubleTy(Ctx) });
413 V = Builder.CreateConstrainedFPCall(Fn, { VDouble });
414 CII = cast<ConstrainedFPIntrinsic>(V);
415 EXPECT_EQ(Intrinsic::experimental_constrained_roundeven, CII->getIntrinsicID());
416 EXPECT_EQ(fp::ebStrict, CII->getExceptionBehavior());
419 TEST_F(IRBuilderTest, ConstrainedFPFunctionCall) {
420 IRBuilder<> Builder(BB);
422 // Create an empty constrained FP function.
423 FunctionType *FTy = FunctionType::get(Type::getVoidTy(Ctx),
424 /*isVarArg=*/false);
425 Function *Callee =
426 Function::Create(FTy, Function::ExternalLinkage, "", M.get());
427 BasicBlock *CalleeBB = BasicBlock::Create(Ctx, "", Callee);
428 IRBuilder<> CalleeBuilder(CalleeBB);
429 CalleeBuilder.setIsFPConstrained(true);
430 CalleeBuilder.setConstrainedFPFunctionAttr();
431 CalleeBuilder.CreateRetVoid();
433 // Now call the empty constrained FP function.
434 Builder.setIsFPConstrained(true);
435 Builder.setConstrainedFPFunctionAttr();
436 CallInst *FCall = Builder.CreateCall(Callee, None);
438 // Check the attributes to verify the strictfp attribute is on the call.
439 EXPECT_TRUE(
440 FCall->getAttributes().getFnAttrs().hasAttribute(Attribute::StrictFP));
442 Builder.CreateRetVoid();
443 EXPECT_FALSE(verifyModule(*M));
446 TEST_F(IRBuilderTest, Lifetime) {
447 IRBuilder<> Builder(BB);
448 AllocaInst *Var1 = Builder.CreateAlloca(Builder.getInt8Ty());
449 AllocaInst *Var2 = Builder.CreateAlloca(Builder.getInt32Ty());
450 AllocaInst *Var3 = Builder.CreateAlloca(Builder.getInt8Ty(),
451 Builder.getInt32(123));
453 CallInst *Start1 = Builder.CreateLifetimeStart(Var1);
454 CallInst *Start2 = Builder.CreateLifetimeStart(Var2);
455 CallInst *Start3 = Builder.CreateLifetimeStart(Var3, Builder.getInt64(100));
457 EXPECT_EQ(Start1->getArgOperand(0), Builder.getInt64(-1));
458 EXPECT_EQ(Start2->getArgOperand(0), Builder.getInt64(-1));
459 EXPECT_EQ(Start3->getArgOperand(0), Builder.getInt64(100));
461 EXPECT_EQ(Start1->getArgOperand(1), Var1);
462 EXPECT_EQ(Start2->getArgOperand(1)->stripPointerCasts(), Var2);
463 EXPECT_EQ(Start3->getArgOperand(1), Var3);
465 Value *End1 = Builder.CreateLifetimeEnd(Var1);
466 Builder.CreateLifetimeEnd(Var2);
467 Builder.CreateLifetimeEnd(Var3);
469 IntrinsicInst *II_Start1 = dyn_cast<IntrinsicInst>(Start1);
470 IntrinsicInst *II_End1 = dyn_cast<IntrinsicInst>(End1);
471 ASSERT_TRUE(II_Start1 != nullptr);
472 EXPECT_EQ(II_Start1->getIntrinsicID(), Intrinsic::lifetime_start);
473 ASSERT_TRUE(II_End1 != nullptr);
474 EXPECT_EQ(II_End1->getIntrinsicID(), Intrinsic::lifetime_end);
477 TEST_F(IRBuilderTest, CreateCondBr) {
478 IRBuilder<> Builder(BB);
479 BasicBlock *TBB = BasicBlock::Create(Ctx, "", F);
480 BasicBlock *FBB = BasicBlock::Create(Ctx, "", F);
482 BranchInst *BI = Builder.CreateCondBr(Builder.getTrue(), TBB, FBB);
483 Instruction *TI = BB->getTerminator();
484 EXPECT_EQ(BI, TI);
485 EXPECT_EQ(2u, TI->getNumSuccessors());
486 EXPECT_EQ(TBB, TI->getSuccessor(0));
487 EXPECT_EQ(FBB, TI->getSuccessor(1));
489 BI->eraseFromParent();
490 MDNode *Weights = MDBuilder(Ctx).createBranchWeights(42, 13);
491 BI = Builder.CreateCondBr(Builder.getTrue(), TBB, FBB, Weights);
492 TI = BB->getTerminator();
493 EXPECT_EQ(BI, TI);
494 EXPECT_EQ(2u, TI->getNumSuccessors());
495 EXPECT_EQ(TBB, TI->getSuccessor(0));
496 EXPECT_EQ(FBB, TI->getSuccessor(1));
497 EXPECT_EQ(Weights, TI->getMetadata(LLVMContext::MD_prof));
500 TEST_F(IRBuilderTest, LandingPadName) {
501 IRBuilder<> Builder(BB);
502 LandingPadInst *LP = Builder.CreateLandingPad(Builder.getInt32Ty(), 0, "LP");
503 EXPECT_EQ(LP->getName(), "LP");
506 TEST_F(IRBuilderTest, DataLayout) {
507 std::unique_ptr<Module> M(new Module("test", Ctx));
508 M->setDataLayout("e-n32");
509 EXPECT_TRUE(M->getDataLayout().isLegalInteger(32));
510 M->setDataLayout("e");
511 EXPECT_FALSE(M->getDataLayout().isLegalInteger(32));
514 TEST_F(IRBuilderTest, GetIntTy) {
515 IRBuilder<> Builder(BB);
516 IntegerType *Ty1 = Builder.getInt1Ty();
517 EXPECT_EQ(Ty1, IntegerType::get(Ctx, 1));
519 DataLayout* DL = new DataLayout(M.get());
520 IntegerType *IntPtrTy = Builder.getIntPtrTy(*DL);
521 unsigned IntPtrBitSize = DL->getPointerSizeInBits(0);
522 EXPECT_EQ(IntPtrTy, IntegerType::get(Ctx, IntPtrBitSize));
523 delete DL;
526 TEST_F(IRBuilderTest, UnaryOperators) {
527 IRBuilder<NoFolder> Builder(BB);
528 Value *V = Builder.CreateLoad(GV->getValueType(), GV);
530 // Test CreateUnOp(X)
531 Value *U = Builder.CreateUnOp(Instruction::FNeg, V);
532 ASSERT_TRUE(isa<Instruction>(U));
533 ASSERT_TRUE(isa<FPMathOperator>(U));
534 ASSERT_TRUE(isa<UnaryOperator>(U));
535 ASSERT_FALSE(isa<BinaryOperator>(U));
537 // Test CreateFNegFMF(X)
538 Instruction *I = cast<Instruction>(U);
539 I->setHasNoSignedZeros(true);
540 I->setHasNoNaNs(true);
541 Value *VFMF = Builder.CreateFNegFMF(V, I);
542 Instruction *IFMF = cast<Instruction>(VFMF);
543 EXPECT_TRUE(IFMF->hasNoSignedZeros());
544 EXPECT_TRUE(IFMF->hasNoNaNs());
545 EXPECT_FALSE(IFMF->hasAllowReassoc());
548 TEST_F(IRBuilderTest, FastMathFlags) {
549 IRBuilder<> Builder(BB);
550 Value *F, *FC;
551 Instruction *FDiv, *FAdd, *FCmp, *FCall;
553 F = Builder.CreateLoad(GV->getValueType(), GV);
554 F = Builder.CreateFAdd(F, F);
556 EXPECT_FALSE(Builder.getFastMathFlags().any());
557 ASSERT_TRUE(isa<Instruction>(F));
558 FAdd = cast<Instruction>(F);
559 EXPECT_FALSE(FAdd->hasNoNaNs());
561 FastMathFlags FMF;
562 Builder.setFastMathFlags(FMF);
564 // By default, no flags are set.
565 F = Builder.CreateFAdd(F, F);
566 EXPECT_FALSE(Builder.getFastMathFlags().any());
567 ASSERT_TRUE(isa<Instruction>(F));
568 FAdd = cast<Instruction>(F);
569 EXPECT_FALSE(FAdd->hasNoNaNs());
570 EXPECT_FALSE(FAdd->hasNoInfs());
571 EXPECT_FALSE(FAdd->hasNoSignedZeros());
572 EXPECT_FALSE(FAdd->hasAllowReciprocal());
573 EXPECT_FALSE(FAdd->hasAllowContract());
574 EXPECT_FALSE(FAdd->hasAllowReassoc());
575 EXPECT_FALSE(FAdd->hasApproxFunc());
577 // Set all flags in the instruction.
578 FAdd->setFast(true);
579 EXPECT_TRUE(FAdd->hasNoNaNs());
580 EXPECT_TRUE(FAdd->hasNoInfs());
581 EXPECT_TRUE(FAdd->hasNoSignedZeros());
582 EXPECT_TRUE(FAdd->hasAllowReciprocal());
583 EXPECT_TRUE(FAdd->hasAllowContract());
584 EXPECT_TRUE(FAdd->hasAllowReassoc());
585 EXPECT_TRUE(FAdd->hasApproxFunc());
587 // All flags are set in the builder.
588 FMF.setFast();
589 Builder.setFastMathFlags(FMF);
591 F = Builder.CreateFAdd(F, F);
592 EXPECT_TRUE(Builder.getFastMathFlags().any());
593 EXPECT_TRUE(Builder.getFastMathFlags().all());
594 ASSERT_TRUE(isa<Instruction>(F));
595 FAdd = cast<Instruction>(F);
596 EXPECT_TRUE(FAdd->hasNoNaNs());
597 EXPECT_TRUE(FAdd->isFast());
599 // Now, try it with CreateBinOp
600 F = Builder.CreateBinOp(Instruction::FAdd, F, F);
601 EXPECT_TRUE(Builder.getFastMathFlags().any());
602 ASSERT_TRUE(isa<Instruction>(F));
603 FAdd = cast<Instruction>(F);
604 EXPECT_TRUE(FAdd->hasNoNaNs());
605 EXPECT_TRUE(FAdd->isFast());
607 F = Builder.CreateFDiv(F, F);
608 EXPECT_TRUE(Builder.getFastMathFlags().all());
609 ASSERT_TRUE(isa<Instruction>(F));
610 FDiv = cast<Instruction>(F);
611 EXPECT_TRUE(FDiv->hasAllowReciprocal());
613 // Clear all FMF in the builder.
614 Builder.clearFastMathFlags();
616 F = Builder.CreateFDiv(F, F);
617 ASSERT_TRUE(isa<Instruction>(F));
618 FDiv = cast<Instruction>(F);
619 EXPECT_FALSE(FDiv->hasAllowReciprocal());
621 // Try individual flags.
622 FMF.clear();
623 FMF.setAllowReciprocal();
624 Builder.setFastMathFlags(FMF);
626 F = Builder.CreateFDiv(F, F);
627 EXPECT_TRUE(Builder.getFastMathFlags().any());
628 EXPECT_TRUE(Builder.getFastMathFlags().AllowReciprocal);
629 ASSERT_TRUE(isa<Instruction>(F));
630 FDiv = cast<Instruction>(F);
631 EXPECT_TRUE(FDiv->hasAllowReciprocal());
633 Builder.clearFastMathFlags();
635 FC = Builder.CreateFCmpOEQ(F, F);
636 ASSERT_TRUE(isa<Instruction>(FC));
637 FCmp = cast<Instruction>(FC);
638 EXPECT_FALSE(FCmp->hasAllowReciprocal());
640 FMF.clear();
641 FMF.setAllowReciprocal();
642 Builder.setFastMathFlags(FMF);
644 FC = Builder.CreateFCmpOEQ(F, F);
645 EXPECT_TRUE(Builder.getFastMathFlags().any());
646 EXPECT_TRUE(Builder.getFastMathFlags().AllowReciprocal);
647 ASSERT_TRUE(isa<Instruction>(FC));
648 FCmp = cast<Instruction>(FC);
649 EXPECT_TRUE(FCmp->hasAllowReciprocal());
651 Builder.clearFastMathFlags();
653 // Test FP-contract
654 FC = Builder.CreateFAdd(F, F);
655 ASSERT_TRUE(isa<Instruction>(FC));
656 FAdd = cast<Instruction>(FC);
657 EXPECT_FALSE(FAdd->hasAllowContract());
659 FMF.clear();
660 FMF.setAllowContract(true);
661 Builder.setFastMathFlags(FMF);
663 FC = Builder.CreateFAdd(F, F);
664 EXPECT_TRUE(Builder.getFastMathFlags().any());
665 EXPECT_TRUE(Builder.getFastMathFlags().AllowContract);
666 ASSERT_TRUE(isa<Instruction>(FC));
667 FAdd = cast<Instruction>(FC);
668 EXPECT_TRUE(FAdd->hasAllowContract());
670 FMF.setApproxFunc();
671 Builder.clearFastMathFlags();
672 Builder.setFastMathFlags(FMF);
673 // Now 'aml' and 'contract' are set.
674 F = Builder.CreateFMul(F, F);
675 FAdd = cast<Instruction>(F);
676 EXPECT_TRUE(FAdd->hasApproxFunc());
677 EXPECT_TRUE(FAdd->hasAllowContract());
678 EXPECT_FALSE(FAdd->hasAllowReassoc());
680 FMF.setAllowReassoc();
681 Builder.clearFastMathFlags();
682 Builder.setFastMathFlags(FMF);
683 // Now 'aml' and 'contract' and 'reassoc' are set.
684 F = Builder.CreateFMul(F, F);
685 FAdd = cast<Instruction>(F);
686 EXPECT_TRUE(FAdd->hasApproxFunc());
687 EXPECT_TRUE(FAdd->hasAllowContract());
688 EXPECT_TRUE(FAdd->hasAllowReassoc());
690 // Test a call with FMF.
691 auto CalleeTy = FunctionType::get(Type::getFloatTy(Ctx),
692 /*isVarArg=*/false);
693 auto Callee =
694 Function::Create(CalleeTy, Function::ExternalLinkage, "", M.get());
696 FCall = Builder.CreateCall(Callee, None);
697 EXPECT_FALSE(FCall->hasNoNaNs());
699 Function *V =
700 Function::Create(CalleeTy, Function::ExternalLinkage, "", M.get());
701 FCall = Builder.CreateCall(V, None);
702 EXPECT_FALSE(FCall->hasNoNaNs());
704 FMF.clear();
705 FMF.setNoNaNs();
706 Builder.setFastMathFlags(FMF);
708 FCall = Builder.CreateCall(Callee, None);
709 EXPECT_TRUE(Builder.getFastMathFlags().any());
710 EXPECT_TRUE(Builder.getFastMathFlags().NoNaNs);
711 EXPECT_TRUE(FCall->hasNoNaNs());
713 FCall = Builder.CreateCall(V, None);
714 EXPECT_TRUE(Builder.getFastMathFlags().any());
715 EXPECT_TRUE(Builder.getFastMathFlags().NoNaNs);
716 EXPECT_TRUE(FCall->hasNoNaNs());
718 Builder.clearFastMathFlags();
720 // To test a copy, make sure that a '0' and a '1' change state.
721 F = Builder.CreateFDiv(F, F);
722 ASSERT_TRUE(isa<Instruction>(F));
723 FDiv = cast<Instruction>(F);
724 EXPECT_FALSE(FDiv->getFastMathFlags().any());
725 FDiv->setHasAllowReciprocal(true);
726 FAdd->setHasAllowReciprocal(false);
727 FAdd->setHasNoNaNs(true);
728 FDiv->copyFastMathFlags(FAdd);
729 EXPECT_TRUE(FDiv->hasNoNaNs());
730 EXPECT_FALSE(FDiv->hasAllowReciprocal());
734 TEST_F(IRBuilderTest, WrapFlags) {
735 IRBuilder<NoFolder> Builder(BB);
737 // Test instructions.
738 GlobalVariable *G = new GlobalVariable(*M, Builder.getInt32Ty(), true,
739 GlobalValue::ExternalLinkage, nullptr);
740 Value *V = Builder.CreateLoad(G->getValueType(), G);
741 EXPECT_TRUE(
742 cast<BinaryOperator>(Builder.CreateNSWAdd(V, V))->hasNoSignedWrap());
743 EXPECT_TRUE(
744 cast<BinaryOperator>(Builder.CreateNSWMul(V, V))->hasNoSignedWrap());
745 EXPECT_TRUE(
746 cast<BinaryOperator>(Builder.CreateNSWSub(V, V))->hasNoSignedWrap());
747 EXPECT_TRUE(cast<BinaryOperator>(
748 Builder.CreateShl(V, V, "", /* NUW */ false, /* NSW */ true))
749 ->hasNoSignedWrap());
751 EXPECT_TRUE(
752 cast<BinaryOperator>(Builder.CreateNUWAdd(V, V))->hasNoUnsignedWrap());
753 EXPECT_TRUE(
754 cast<BinaryOperator>(Builder.CreateNUWMul(V, V))->hasNoUnsignedWrap());
755 EXPECT_TRUE(
756 cast<BinaryOperator>(Builder.CreateNUWSub(V, V))->hasNoUnsignedWrap());
757 EXPECT_TRUE(cast<BinaryOperator>(
758 Builder.CreateShl(V, V, "", /* NUW */ true, /* NSW */ false))
759 ->hasNoUnsignedWrap());
761 // Test operators created with constants.
762 Constant *C = Builder.getInt32(42);
763 EXPECT_TRUE(cast<OverflowingBinaryOperator>(Builder.CreateNSWAdd(C, C))
764 ->hasNoSignedWrap());
765 EXPECT_TRUE(cast<OverflowingBinaryOperator>(Builder.CreateNSWSub(C, C))
766 ->hasNoSignedWrap());
767 EXPECT_TRUE(cast<OverflowingBinaryOperator>(Builder.CreateNSWMul(C, C))
768 ->hasNoSignedWrap());
769 EXPECT_TRUE(cast<OverflowingBinaryOperator>(
770 Builder.CreateShl(C, C, "", /* NUW */ false, /* NSW */ true))
771 ->hasNoSignedWrap());
773 EXPECT_TRUE(cast<OverflowingBinaryOperator>(Builder.CreateNUWAdd(C, C))
774 ->hasNoUnsignedWrap());
775 EXPECT_TRUE(cast<OverflowingBinaryOperator>(Builder.CreateNUWSub(C, C))
776 ->hasNoUnsignedWrap());
777 EXPECT_TRUE(cast<OverflowingBinaryOperator>(Builder.CreateNUWMul(C, C))
778 ->hasNoUnsignedWrap());
779 EXPECT_TRUE(cast<OverflowingBinaryOperator>(
780 Builder.CreateShl(C, C, "", /* NUW */ true, /* NSW */ false))
781 ->hasNoUnsignedWrap());
784 TEST_F(IRBuilderTest, RAIIHelpersTest) {
785 IRBuilder<> Builder(BB);
786 EXPECT_FALSE(Builder.getFastMathFlags().allowReciprocal());
787 MDBuilder MDB(M->getContext());
789 MDNode *FPMathA = MDB.createFPMath(0.01f);
790 MDNode *FPMathB = MDB.createFPMath(0.1f);
792 Builder.setDefaultFPMathTag(FPMathA);
795 IRBuilder<>::FastMathFlagGuard Guard(Builder);
796 FastMathFlags FMF;
797 FMF.setAllowReciprocal();
798 Builder.setFastMathFlags(FMF);
799 Builder.setDefaultFPMathTag(FPMathB);
800 EXPECT_TRUE(Builder.getFastMathFlags().allowReciprocal());
801 EXPECT_EQ(FPMathB, Builder.getDefaultFPMathTag());
804 EXPECT_FALSE(Builder.getFastMathFlags().allowReciprocal());
805 EXPECT_EQ(FPMathA, Builder.getDefaultFPMathTag());
807 Value *F = Builder.CreateLoad(GV->getValueType(), GV);
810 IRBuilder<>::InsertPointGuard Guard(Builder);
811 Builder.SetInsertPoint(cast<Instruction>(F));
812 EXPECT_EQ(F, &*Builder.GetInsertPoint());
815 EXPECT_EQ(BB->end(), Builder.GetInsertPoint());
816 EXPECT_EQ(BB, Builder.GetInsertBlock());
819 TEST_F(IRBuilderTest, createFunction) {
820 IRBuilder<> Builder(BB);
821 DIBuilder DIB(*M);
822 auto File = DIB.createFile("error.swift", "/");
823 auto CU =
824 DIB.createCompileUnit(dwarf::DW_LANG_Swift, File, "swiftc", true, "", 0);
825 auto Type = DIB.createSubroutineType(DIB.getOrCreateTypeArray(None));
826 auto NoErr = DIB.createFunction(
827 CU, "noerr", "", File, 1, Type, 1, DINode::FlagZero,
828 DISubprogram::SPFlagDefinition | DISubprogram::SPFlagOptimized);
829 EXPECT_TRUE(!NoErr->getThrownTypes());
830 auto Int = DIB.createBasicType("Int", 64, dwarf::DW_ATE_signed);
831 auto Error = DIB.getOrCreateArray({Int});
832 auto Err = DIB.createFunction(
833 CU, "err", "", File, 1, Type, 1, DINode::FlagZero,
834 DISubprogram::SPFlagDefinition | DISubprogram::SPFlagOptimized, nullptr,
835 nullptr, Error.get());
836 EXPECT_TRUE(Err->getThrownTypes().get() == Error.get());
837 DIB.finalize();
840 TEST_F(IRBuilderTest, DIBuilder) {
841 IRBuilder<> Builder(BB);
842 DIBuilder DIB(*M);
843 auto File = DIB.createFile("F.CBL", "/");
844 auto CU = DIB.createCompileUnit(dwarf::DW_LANG_Cobol74,
845 DIB.createFile("F.CBL", "/"), "llvm-cobol74",
846 true, "", 0);
847 auto Type = DIB.createSubroutineType(DIB.getOrCreateTypeArray(None));
848 auto SP = DIB.createFunction(
849 CU, "foo", "", File, 1, Type, 1, DINode::FlagZero,
850 DISubprogram::SPFlagDefinition | DISubprogram::SPFlagOptimized);
851 F->setSubprogram(SP);
852 AllocaInst *I = Builder.CreateAlloca(Builder.getInt8Ty());
853 auto BarSP = DIB.createFunction(
854 CU, "bar", "", File, 1, Type, 1, DINode::FlagZero,
855 DISubprogram::SPFlagDefinition | DISubprogram::SPFlagOptimized);
856 auto BadScope = DIB.createLexicalBlockFile(BarSP, File, 0);
857 I->setDebugLoc(DILocation::get(Ctx, 2, 0, BadScope));
858 DIB.finalize();
859 EXPECT_TRUE(verifyModule(*M));
862 TEST_F(IRBuilderTest, createArtificialSubprogram) {
863 IRBuilder<> Builder(BB);
864 DIBuilder DIB(*M);
865 auto File = DIB.createFile("main.c", "/");
866 auto CU = DIB.createCompileUnit(dwarf::DW_LANG_C, File, "clang",
867 /*isOptimized=*/true, /*Flags=*/"",
868 /*Runtime Version=*/0);
869 auto Type = DIB.createSubroutineType(DIB.getOrCreateTypeArray(None));
870 auto SP = DIB.createFunction(
871 CU, "foo", /*LinkageName=*/"", File,
872 /*LineNo=*/1, Type, /*ScopeLine=*/2, DINode::FlagZero,
873 DISubprogram::SPFlagDefinition | DISubprogram::SPFlagOptimized);
874 EXPECT_TRUE(SP->isDistinct());
876 F->setSubprogram(SP);
877 AllocaInst *I = Builder.CreateAlloca(Builder.getInt8Ty());
878 ReturnInst *R = Builder.CreateRetVoid();
879 I->setDebugLoc(DILocation::get(Ctx, 3, 2, SP));
880 R->setDebugLoc(DILocation::get(Ctx, 4, 2, SP));
881 DIB.finalize();
882 EXPECT_FALSE(verifyModule(*M));
884 Function *G = Function::Create(F->getFunctionType(),
885 Function::ExternalLinkage, "", M.get());
886 BasicBlock *GBB = BasicBlock::Create(Ctx, "", G);
887 Builder.SetInsertPoint(GBB);
888 I->removeFromParent();
889 Builder.Insert(I);
890 Builder.CreateRetVoid();
891 EXPECT_FALSE(verifyModule(*M));
893 DISubprogram *GSP = DIBuilder::createArtificialSubprogram(F->getSubprogram());
894 EXPECT_EQ(SP->getFile(), GSP->getFile());
895 EXPECT_EQ(SP->getType(), GSP->getType());
896 EXPECT_EQ(SP->getLine(), GSP->getLine());
897 EXPECT_EQ(SP->getScopeLine(), GSP->getScopeLine());
898 EXPECT_TRUE(GSP->isDistinct());
900 G->setSubprogram(GSP);
901 EXPECT_TRUE(verifyModule(*M));
903 auto *InlinedAtNode =
904 DILocation::getDistinct(Ctx, GSP->getScopeLine(), 0, GSP);
905 DebugLoc DL = I->getDebugLoc();
906 DenseMap<const MDNode *, MDNode *> IANodes;
907 auto IA = DebugLoc::appendInlinedAt(DL, InlinedAtNode, Ctx, IANodes);
908 auto NewDL =
909 DILocation::get(Ctx, DL.getLine(), DL.getCol(), DL.getScope(), IA);
910 I->setDebugLoc(NewDL);
911 EXPECT_FALSE(verifyModule(*M));
913 EXPECT_EQ("foo", SP->getName());
914 EXPECT_EQ("foo", GSP->getName());
915 EXPECT_FALSE(SP->isArtificial());
916 EXPECT_TRUE(GSP->isArtificial());
919 // Check that we can add debug info to an existing DICompileUnit.
920 TEST_F(IRBuilderTest, appendDebugInfo) {
921 IRBuilder<> Builder(BB);
922 Builder.CreateRetVoid();
923 EXPECT_FALSE(verifyModule(*M));
925 auto GetNames = [](DICompileUnit *CU) {
926 SmallVector<StringRef> Names;
927 for (auto *ET : CU->getEnumTypes())
928 Names.push_back(ET->getName());
929 for (auto *RT : CU->getRetainedTypes())
930 Names.push_back(RT->getName());
931 for (auto *GV : CU->getGlobalVariables())
932 Names.push_back(GV->getVariable()->getName());
933 for (auto *IE : CU->getImportedEntities())
934 Names.push_back(IE->getName());
935 for (auto *Node : CU->getMacros())
936 if (auto *MN = dyn_cast_or_null<DIMacro>(Node))
937 Names.push_back(MN->getName());
938 return Names;
941 DICompileUnit *CU;
943 DIBuilder DIB(*M);
944 auto *File = DIB.createFile("main.c", "/");
945 CU = DIB.createCompileUnit(dwarf::DW_LANG_C, File, "clang",
946 /*isOptimized=*/true, /*Flags=*/"",
947 /*Runtime Version=*/0);
948 auto *ByteTy = DIB.createBasicType("byte0", 8, dwarf::DW_ATE_signed);
949 DIB.createEnumerationType(CU, "ET0", File, /*LineNo=*/0, /*SizeInBits=*/8,
950 /*AlignInBits=*/8, /*Elements=*/{}, ByteTy);
951 DIB.retainType(ByteTy);
952 DIB.createGlobalVariableExpression(CU, "GV0", /*LinkageName=*/"", File,
953 /*LineNo=*/1, ByteTy,
954 /*IsLocalToUnit=*/true);
955 DIB.createImportedDeclaration(CU, nullptr, File, /*LineNo=*/2, "IM0");
956 DIB.createMacro(nullptr, /*LineNo=*/0, dwarf::DW_MACINFO_define, "M0");
957 DIB.finalize();
959 EXPECT_FALSE(verifyModule(*M));
960 EXPECT_THAT(GetNames(CU),
961 UnorderedElementsAre("ET0", "byte0", "GV0", "IM0", "M0"));
964 DIBuilder DIB(*M, true, CU);
965 auto *File = CU->getFile();
966 auto *ByteTy = DIB.createBasicType("byte1", 8, dwarf::DW_ATE_signed);
967 DIB.createEnumerationType(CU, "ET1", File, /*LineNo=*/0,
968 /*SizeInBits=*/8, /*AlignInBits=*/8,
969 /*Elements=*/{}, ByteTy);
970 DIB.retainType(ByteTy);
971 DIB.createGlobalVariableExpression(CU, "GV1", /*LinkageName=*/"", File,
972 /*LineNo=*/1, ByteTy,
973 /*IsLocalToUnit=*/true);
974 DIB.createImportedDeclaration(CU, nullptr, File, /*LineNo=*/2, "IM1");
975 DIB.createMacro(nullptr, /*LineNo=*/0, dwarf::DW_MACINFO_define, "M1");
976 DIB.finalize();
978 EXPECT_FALSE(verifyModule(*M));
979 EXPECT_THAT(GetNames(CU),
980 UnorderedElementsAre("ET0", "byte0", "GV0", "IM0", "M0", "ET1",
981 "byte1", "GV1", "IM1", "M1"));
984 TEST_F(IRBuilderTest, InsertExtractElement) {
985 IRBuilder<> Builder(BB);
987 auto VecTy = FixedVectorType::get(Builder.getInt64Ty(), 4);
988 auto Elt1 = Builder.getInt64(-1);
989 auto Elt2 = Builder.getInt64(-2);
990 Value *Vec = Builder.CreateInsertElement(VecTy, Elt1, Builder.getInt8(1));
991 Vec = Builder.CreateInsertElement(Vec, Elt2, 2);
992 auto X1 = Builder.CreateExtractElement(Vec, 1);
993 auto X2 = Builder.CreateExtractElement(Vec, Builder.getInt32(2));
994 EXPECT_EQ(Elt1, X1);
995 EXPECT_EQ(Elt2, X2);
998 TEST_F(IRBuilderTest, CreateGlobalStringPtr) {
999 IRBuilder<> Builder(BB);
1001 auto String1a = Builder.CreateGlobalStringPtr("TestString", "String1a");
1002 auto String1b = Builder.CreateGlobalStringPtr("TestString", "String1b", 0);
1003 auto String2 = Builder.CreateGlobalStringPtr("TestString", "String2", 1);
1004 auto String3 = Builder.CreateGlobalString("TestString", "String3", 2);
1006 EXPECT_TRUE(String1a->getType()->getPointerAddressSpace() == 0);
1007 EXPECT_TRUE(String1b->getType()->getPointerAddressSpace() == 0);
1008 EXPECT_TRUE(String2->getType()->getPointerAddressSpace() == 1);
1009 EXPECT_TRUE(String3->getType()->getPointerAddressSpace() == 2);
1012 TEST_F(IRBuilderTest, CreateThreadLocalAddress) {
1013 IRBuilder<> Builder(BB);
1015 GlobalVariable *G = new GlobalVariable(*M, Builder.getInt64Ty(), /*isConstant*/true,
1016 GlobalValue::ExternalLinkage, nullptr, "", nullptr,
1017 GlobalValue::GeneralDynamicTLSModel);
1019 Constant *CEBC = ConstantExpr::getBitCast(G, Builder.getInt8PtrTy());
1020 // Tests that IRBuilder::CreateThreadLocalAddress wouldn't crash if its operand
1021 // is BitCast ConstExpr. The case should be eliminated after we eliminate the
1022 // abuse of constexpr.
1023 CallInst *CI = Builder.CreateThreadLocalAddress(CEBC);
1024 EXPECT_NE(CI, nullptr);
1027 TEST_F(IRBuilderTest, DebugLoc) {
1028 auto CalleeTy = FunctionType::get(Type::getVoidTy(Ctx),
1029 /*isVarArg=*/false);
1030 auto Callee =
1031 Function::Create(CalleeTy, Function::ExternalLinkage, "", M.get());
1033 DIBuilder DIB(*M);
1034 auto File = DIB.createFile("tmp.cpp", "/");
1035 auto CU = DIB.createCompileUnit(dwarf::DW_LANG_C_plus_plus_11,
1036 DIB.createFile("tmp.cpp", "/"), "", true, "",
1038 auto SPType = DIB.createSubroutineType(DIB.getOrCreateTypeArray(None));
1039 auto SP =
1040 DIB.createFunction(CU, "foo", "foo", File, 1, SPType, 1, DINode::FlagZero,
1041 DISubprogram::SPFlagDefinition);
1042 DebugLoc DL1 = DILocation::get(Ctx, 2, 0, SP);
1043 DebugLoc DL2 = DILocation::get(Ctx, 3, 0, SP);
1045 auto BB2 = BasicBlock::Create(Ctx, "bb2", F);
1046 auto Br = BranchInst::Create(BB2, BB);
1047 Br->setDebugLoc(DL1);
1049 IRBuilder<> Builder(Ctx);
1050 Builder.SetInsertPoint(Br);
1051 EXPECT_EQ(DL1, Builder.getCurrentDebugLocation());
1052 auto Call1 = Builder.CreateCall(Callee, None);
1053 EXPECT_EQ(DL1, Call1->getDebugLoc());
1055 Call1->setDebugLoc(DL2);
1056 Builder.SetInsertPoint(Call1->getParent(), Call1->getIterator());
1057 EXPECT_EQ(DL2, Builder.getCurrentDebugLocation());
1058 auto Call2 = Builder.CreateCall(Callee, None);
1059 EXPECT_EQ(DL2, Call2->getDebugLoc());
1061 DIB.finalize();
1064 TEST_F(IRBuilderTest, DIImportedEntity) {
1065 IRBuilder<> Builder(BB);
1066 DIBuilder DIB(*M);
1067 auto F = DIB.createFile("F.CBL", "/");
1068 auto CU = DIB.createCompileUnit(dwarf::DW_LANG_Cobol74,
1069 F, "llvm-cobol74",
1070 true, "", 0);
1071 MDTuple *Elements = MDTuple::getDistinct(Ctx, None);
1073 DIB.createImportedDeclaration(CU, nullptr, F, 1);
1074 DIB.createImportedDeclaration(CU, nullptr, F, 1);
1075 DIB.createImportedModule(CU, (DIImportedEntity *)nullptr, F, 2);
1076 DIB.createImportedModule(CU, (DIImportedEntity *)nullptr, F, 2);
1077 DIB.createImportedModule(CU, (DIImportedEntity *)nullptr, F, 2, Elements);
1078 DIB.createImportedModule(CU, (DIImportedEntity *)nullptr, F, 2, Elements);
1079 DIB.finalize();
1080 EXPECT_TRUE(verifyModule(*M));
1081 EXPECT_TRUE(CU->getImportedEntities().size() == 3);
1084 // 0: #define M0 V0 <-- command line definition
1085 // 0: main.c <-- main file
1086 // 3: #define M1 V1 <-- M1 definition in main.c
1087 // 5: #include "file.h" <-- inclusion of file.h from main.c
1088 // 1: #define M2 <-- M2 definition in file.h with no value
1089 // 7: #undef M1 V1 <-- M1 un-definition in main.c
1090 TEST_F(IRBuilderTest, DIBuilderMacro) {
1091 IRBuilder<> Builder(BB);
1092 DIBuilder DIB(*M);
1093 auto File1 = DIB.createFile("main.c", "/");
1094 auto File2 = DIB.createFile("file.h", "/");
1095 auto CU = DIB.createCompileUnit(
1096 dwarf::DW_LANG_C, DIB.createFile("main.c", "/"), "llvm-c", true, "", 0);
1097 auto MDef0 =
1098 DIB.createMacro(nullptr, 0, dwarf::DW_MACINFO_define, "M0", "V0");
1099 auto TMF1 = DIB.createTempMacroFile(nullptr, 0, File1);
1100 auto MDef1 = DIB.createMacro(TMF1, 3, dwarf::DW_MACINFO_define, "M1", "V1");
1101 auto TMF2 = DIB.createTempMacroFile(TMF1, 5, File2);
1102 auto MDef2 = DIB.createMacro(TMF2, 1, dwarf::DW_MACINFO_define, "M2");
1103 auto MUndef1 = DIB.createMacro(TMF1, 7, dwarf::DW_MACINFO_undef, "M1");
1105 EXPECT_EQ(dwarf::DW_MACINFO_define, MDef1->getMacinfoType());
1106 EXPECT_EQ(3u, MDef1->getLine());
1107 EXPECT_EQ("M1", MDef1->getName());
1108 EXPECT_EQ("V1", MDef1->getValue());
1110 EXPECT_EQ(dwarf::DW_MACINFO_undef, MUndef1->getMacinfoType());
1111 EXPECT_EQ(7u, MUndef1->getLine());
1112 EXPECT_EQ("M1", MUndef1->getName());
1113 EXPECT_EQ("", MUndef1->getValue());
1115 EXPECT_EQ(dwarf::DW_MACINFO_start_file, TMF2->getMacinfoType());
1116 EXPECT_EQ(5u, TMF2->getLine());
1117 EXPECT_EQ(File2, TMF2->getFile());
1119 DIB.finalize();
1121 SmallVector<Metadata *, 4> Elements;
1122 Elements.push_back(MDef2);
1123 auto MF2 = DIMacroFile::get(Ctx, dwarf::DW_MACINFO_start_file, 5, File2,
1124 DIB.getOrCreateMacroArray(Elements));
1126 Elements.clear();
1127 Elements.push_back(MDef1);
1128 Elements.push_back(MF2);
1129 Elements.push_back(MUndef1);
1130 auto MF1 = DIMacroFile::get(Ctx, dwarf::DW_MACINFO_start_file, 0, File1,
1131 DIB.getOrCreateMacroArray(Elements));
1133 Elements.clear();
1134 Elements.push_back(MDef0);
1135 Elements.push_back(MF1);
1136 auto MN0 = MDTuple::get(Ctx, Elements);
1137 EXPECT_EQ(MN0, CU->getRawMacros());
1139 Elements.clear();
1140 Elements.push_back(MDef1);
1141 Elements.push_back(MF2);
1142 Elements.push_back(MUndef1);
1143 auto MN1 = MDTuple::get(Ctx, Elements);
1144 EXPECT_EQ(MN1, MF1->getRawElements());
1146 Elements.clear();
1147 Elements.push_back(MDef2);
1148 auto MN2 = MDTuple::get(Ctx, Elements);
1149 EXPECT_EQ(MN2, MF2->getRawElements());
1150 EXPECT_TRUE(verifyModule(*M));
1153 TEST_F(IRBuilderTest, NoFolderNames) {
1154 IRBuilder<NoFolder> Builder(BB);
1155 auto *Add =
1156 Builder.CreateAdd(Builder.getInt32(1), Builder.getInt32(2), "add");
1157 EXPECT_EQ(Add->getName(), "add");