1 //===- llvm/unittest/IR/IRBuilderTest.cpp - IRBuilder tests ---------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Analysis/InstSimplifyFolder.h"
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/IntrinsicsAArch64.h"
17 #include "llvm/IR/LLVMContext.h"
18 #include "llvm/IR/MDBuilder.h"
19 #include "llvm/IR/Module.h"
20 #include "llvm/IR/NoFolder.h"
21 #include "llvm/IR/Verifier.h"
22 #include "gmock/gmock.h"
23 #include "gtest/gtest.h"
25 #include <type_traits>
28 using ::testing::UnorderedElementsAre
;
32 class IRBuilderTest
: public testing::Test
{
34 void SetUp() override
{
35 M
.reset(new Module("MyModule", Ctx
));
36 FunctionType
*FTy
= FunctionType::get(Type::getVoidTy(Ctx
),
38 F
= Function::Create(FTy
, Function::ExternalLinkage
, "", M
.get());
39 BB
= BasicBlock::Create(Ctx
, "", F
);
40 GV
= new GlobalVariable(*M
, Type::getFloatTy(Ctx
), true,
41 GlobalValue::ExternalLinkage
, nullptr);
44 void TearDown() override
{
50 std::unique_ptr
<Module
> M
;
56 TEST_F(IRBuilderTest
, Intrinsics
) {
57 IRBuilder
<> Builder(BB
);
63 V
= Builder
.CreateLoad(GV
->getValueType(), GV
);
64 I
= cast
<Instruction
>(Builder
.CreateFAdd(V
, V
));
65 I
->setHasNoInfs(true);
66 I
->setHasNoNaNs(false);
68 Result
= Builder
.CreateMinNum(V
, V
);
69 II
= cast
<IntrinsicInst
>(Result
);
70 EXPECT_EQ(II
->getIntrinsicID(), Intrinsic::minnum
);
72 Result
= Builder
.CreateMaxNum(V
, V
);
73 II
= cast
<IntrinsicInst
>(Result
);
74 EXPECT_EQ(II
->getIntrinsicID(), Intrinsic::maxnum
);
76 Result
= Builder
.CreateMinimum(V
, V
);
77 II
= cast
<IntrinsicInst
>(Result
);
78 EXPECT_EQ(II
->getIntrinsicID(), Intrinsic::minimum
);
80 Result
= Builder
.CreateMaximum(V
, V
);
81 II
= cast
<IntrinsicInst
>(Result
);
82 EXPECT_EQ(II
->getIntrinsicID(), Intrinsic::maximum
);
84 Result
= Builder
.CreateIntrinsic(Intrinsic::readcyclecounter
, {}, {});
85 II
= cast
<IntrinsicInst
>(Result
);
86 EXPECT_EQ(II
->getIntrinsicID(), Intrinsic::readcyclecounter
);
88 Result
= Builder
.CreateUnaryIntrinsic(Intrinsic::fabs
, V
);
89 II
= cast
<IntrinsicInst
>(Result
);
90 EXPECT_EQ(II
->getIntrinsicID(), Intrinsic::fabs
);
91 EXPECT_FALSE(II
->hasNoInfs());
92 EXPECT_FALSE(II
->hasNoNaNs());
94 Result
= Builder
.CreateUnaryIntrinsic(Intrinsic::fabs
, V
, I
);
95 II
= cast
<IntrinsicInst
>(Result
);
96 EXPECT_EQ(II
->getIntrinsicID(), Intrinsic::fabs
);
97 EXPECT_TRUE(II
->hasNoInfs());
98 EXPECT_FALSE(II
->hasNoNaNs());
100 Result
= Builder
.CreateBinaryIntrinsic(Intrinsic::pow
, V
, V
);
101 II
= cast
<IntrinsicInst
>(Result
);
102 EXPECT_EQ(II
->getIntrinsicID(), Intrinsic::pow
);
103 EXPECT_FALSE(II
->hasNoInfs());
104 EXPECT_FALSE(II
->hasNoNaNs());
106 Result
= Builder
.CreateBinaryIntrinsic(Intrinsic::pow
, V
, V
, I
);
107 II
= cast
<IntrinsicInst
>(Result
);
108 EXPECT_EQ(II
->getIntrinsicID(), Intrinsic::pow
);
109 EXPECT_TRUE(II
->hasNoInfs());
110 EXPECT_FALSE(II
->hasNoNaNs());
112 Result
= Builder
.CreateIntrinsic(Intrinsic::fma
, {V
->getType()}, {V
, V
, V
});
113 II
= cast
<IntrinsicInst
>(Result
);
114 EXPECT_EQ(II
->getIntrinsicID(), Intrinsic::fma
);
115 EXPECT_FALSE(II
->hasNoInfs());
116 EXPECT_FALSE(II
->hasNoNaNs());
119 Builder
.CreateIntrinsic(Intrinsic::fma
, {V
->getType()}, {V
, V
, V
}, I
);
120 II
= cast
<IntrinsicInst
>(Result
);
121 EXPECT_EQ(II
->getIntrinsicID(), Intrinsic::fma
);
122 EXPECT_TRUE(II
->hasNoInfs());
123 EXPECT_FALSE(II
->hasNoNaNs());
126 Builder
.CreateIntrinsic(Intrinsic::fma
, {V
->getType()}, {V
, V
, V
}, I
);
127 II
= cast
<IntrinsicInst
>(Result
);
128 EXPECT_EQ(II
->getIntrinsicID(), Intrinsic::fma
);
129 EXPECT_TRUE(II
->hasNoInfs());
130 EXPECT_FALSE(II
->hasNoNaNs());
132 Result
= Builder
.CreateUnaryIntrinsic(Intrinsic::roundeven
, V
);
133 II
= cast
<IntrinsicInst
>(Result
);
134 EXPECT_EQ(II
->getIntrinsicID(), Intrinsic::roundeven
);
135 EXPECT_FALSE(II
->hasNoInfs());
136 EXPECT_FALSE(II
->hasNoNaNs());
138 Result
= Builder
.CreateIntrinsic(
139 Intrinsic::set_rounding
, {},
140 {Builder
.getInt32(static_cast<uint32_t>(RoundingMode::TowardZero
))});
141 II
= cast
<IntrinsicInst
>(Result
);
142 EXPECT_EQ(II
->getIntrinsicID(), Intrinsic::set_rounding
);
145 TEST_F(IRBuilderTest
, IntrinsicMangling
) {
146 IRBuilder
<> Builder(BB
);
147 Type
*VoidTy
= Builder
.getVoidTy();
148 Type
*Int64Ty
= Builder
.getInt64Ty();
149 Value
*Int64Val
= Builder
.getInt64(0);
150 Value
*DoubleVal
= PoisonValue::get(Builder
.getDoubleTy());
153 // Mangled return type, no arguments.
154 Call
= Builder
.CreateIntrinsic(Int64Ty
, Intrinsic::coro_size
, {});
155 EXPECT_EQ(Call
->getCalledFunction()->getName(), "llvm.coro.size.i64");
157 // Void return type, mangled argument type.
159 Builder
.CreateIntrinsic(VoidTy
, Intrinsic::set_loop_iterations
, Int64Val
);
160 EXPECT_EQ(Call
->getCalledFunction()->getName(),
161 "llvm.set.loop.iterations.i64");
163 // Mangled return type and argument type.
164 Call
= Builder
.CreateIntrinsic(Int64Ty
, Intrinsic::lround
, DoubleVal
);
165 EXPECT_EQ(Call
->getCalledFunction()->getName(), "llvm.lround.i64.f64");
168 TEST_F(IRBuilderTest
, IntrinsicsWithScalableVectors
) {
169 IRBuilder
<> Builder(BB
);
173 // Test scalable flag isn't dropped for intrinsic that is explicitly defined
174 // with scalable vectors, e.g. LLVMType<nxv4i32>.
175 Type
*SrcVecTy
= VectorType::get(Builder
.getHalfTy(), 8, true);
176 Type
*DstVecTy
= VectorType::get(Builder
.getInt32Ty(), 4, true);
177 Type
*PredTy
= VectorType::get(Builder
.getInt1Ty(), 4, true);
179 SmallVector
<Value
*, 3> ArgTys
;
180 ArgTys
.push_back(UndefValue::get(DstVecTy
));
181 ArgTys
.push_back(UndefValue::get(PredTy
));
182 ArgTys
.push_back(UndefValue::get(SrcVecTy
));
184 Call
= Builder
.CreateIntrinsic(Intrinsic::aarch64_sve_fcvtzs_i32f16
, {},
185 ArgTys
, nullptr, "aarch64.sve.fcvtzs.i32f16");
186 FTy
= Call
->getFunctionType();
187 EXPECT_EQ(FTy
->getReturnType(), DstVecTy
);
188 for (unsigned i
= 0; i
!= ArgTys
.size(); ++i
)
189 EXPECT_EQ(FTy
->getParamType(i
), ArgTys
[i
]->getType());
191 // Test scalable flag isn't dropped for intrinsic defined with
192 // LLVMScalarOrSameVectorWidth.
194 Type
*VecTy
= VectorType::get(Builder
.getInt32Ty(), 4, true);
195 Type
*PtrToVecTy
= Builder
.getPtrTy();
196 PredTy
= VectorType::get(Builder
.getInt1Ty(), 4, true);
199 ArgTys
.push_back(UndefValue::get(PtrToVecTy
));
200 ArgTys
.push_back(UndefValue::get(Builder
.getInt32Ty()));
201 ArgTys
.push_back(UndefValue::get(PredTy
));
202 ArgTys
.push_back(UndefValue::get(VecTy
));
204 Call
= Builder
.CreateIntrinsic(Intrinsic::masked_load
,
205 {VecTy
, PtrToVecTy
}, ArgTys
,
206 nullptr, "masked.load");
207 FTy
= Call
->getFunctionType();
208 EXPECT_EQ(FTy
->getReturnType(), VecTy
);
209 for (unsigned i
= 0; i
!= ArgTys
.size(); ++i
)
210 EXPECT_EQ(FTy
->getParamType(i
), ArgTys
[i
]->getType());
213 TEST_F(IRBuilderTest
, CreateVScale
) {
214 IRBuilder
<> Builder(BB
);
216 Constant
*Zero
= Builder
.getInt32(0);
217 Value
*VScale
= Builder
.CreateVScale(Zero
);
218 EXPECT_TRUE(isa
<ConstantInt
>(VScale
) && cast
<ConstantInt
>(VScale
)->isZero());
221 TEST_F(IRBuilderTest
, CreateStepVector
) {
222 IRBuilder
<> Builder(BB
);
224 // Fixed width vectors
225 Type
*DstVecTy
= VectorType::get(Builder
.getInt32Ty(), 4, false);
226 Value
*StepVec
= Builder
.CreateStepVector(DstVecTy
);
227 EXPECT_TRUE(isa
<Constant
>(StepVec
));
228 EXPECT_EQ(StepVec
->getType(), DstVecTy
);
230 const auto *VectorValue
= cast
<Constant
>(StepVec
);
231 for (unsigned i
= 0; i
< 4; i
++) {
232 EXPECT_TRUE(isa
<ConstantInt
>(VectorValue
->getAggregateElement(i
)));
233 ConstantInt
*El
= cast
<ConstantInt
>(VectorValue
->getAggregateElement(i
));
234 EXPECT_EQ(El
->getValue(), i
);
238 DstVecTy
= VectorType::get(Builder
.getInt32Ty(), 4, true);
239 StepVec
= Builder
.CreateStepVector(DstVecTy
);
240 EXPECT_TRUE(isa
<CallInst
>(StepVec
));
241 CallInst
*Call
= cast
<CallInst
>(StepVec
);
242 FunctionType
*FTy
= Call
->getFunctionType();
243 EXPECT_EQ(FTy
->getReturnType(), DstVecTy
);
244 EXPECT_EQ(Call
->getIntrinsicID(), Intrinsic::experimental_stepvector
);
247 TEST_F(IRBuilderTest
, CreateStepVectorI3
) {
248 IRBuilder
<> Builder(BB
);
251 Type
*DstVecTy
= VectorType::get(IntegerType::get(Ctx
, 3), 2, true);
252 Type
*VecI8Ty
= VectorType::get(Builder
.getInt8Ty(), 2, true);
253 Value
*StepVec
= Builder
.CreateStepVector(DstVecTy
);
254 EXPECT_TRUE(isa
<TruncInst
>(StepVec
));
255 TruncInst
*Trunc
= cast
<TruncInst
>(StepVec
);
256 EXPECT_EQ(Trunc
->getDestTy(), DstVecTy
);
257 EXPECT_EQ(Trunc
->getSrcTy(), VecI8Ty
);
258 EXPECT_TRUE(isa
<CallInst
>(Trunc
->getOperand(0)));
260 CallInst
*Call
= cast
<CallInst
>(Trunc
->getOperand(0));
261 FunctionType
*FTy
= Call
->getFunctionType();
262 EXPECT_EQ(FTy
->getReturnType(), VecI8Ty
);
263 EXPECT_EQ(Call
->getIntrinsicID(), Intrinsic::experimental_stepvector
);
266 TEST_F(IRBuilderTest
, ConstrainedFP
) {
267 IRBuilder
<> Builder(BB
);
273 GlobalVariable
*GVDouble
= new GlobalVariable(*M
, Type::getDoubleTy(Ctx
),
274 true, GlobalValue::ExternalLinkage
, nullptr);
276 V
= Builder
.CreateLoad(GV
->getValueType(), GV
);
277 VDouble
= Builder
.CreateLoad(GVDouble
->getValueType(), GVDouble
);
279 // See if we get constrained intrinsics instead of non-constrained
281 Builder
.setIsFPConstrained(true);
282 auto Parent
= BB
->getParent();
283 Parent
->addFnAttr(Attribute::StrictFP
);
285 V
= Builder
.CreateFAdd(V
, V
);
286 ASSERT_TRUE(isa
<IntrinsicInst
>(V
));
287 II
= cast
<IntrinsicInst
>(V
);
288 EXPECT_EQ(II
->getIntrinsicID(), Intrinsic::experimental_constrained_fadd
);
290 V
= Builder
.CreateFSub(V
, V
);
291 ASSERT_TRUE(isa
<IntrinsicInst
>(V
));
292 II
= cast
<IntrinsicInst
>(V
);
293 EXPECT_EQ(II
->getIntrinsicID(), Intrinsic::experimental_constrained_fsub
);
295 V
= Builder
.CreateFMul(V
, V
);
296 ASSERT_TRUE(isa
<IntrinsicInst
>(V
));
297 II
= cast
<IntrinsicInst
>(V
);
298 EXPECT_EQ(II
->getIntrinsicID(), Intrinsic::experimental_constrained_fmul
);
300 V
= Builder
.CreateFDiv(V
, V
);
301 ASSERT_TRUE(isa
<IntrinsicInst
>(V
));
302 II
= cast
<IntrinsicInst
>(V
);
303 EXPECT_EQ(II
->getIntrinsicID(), Intrinsic::experimental_constrained_fdiv
);
305 V
= Builder
.CreateFRem(V
, V
);
306 ASSERT_TRUE(isa
<IntrinsicInst
>(V
));
307 II
= cast
<IntrinsicInst
>(V
);
308 EXPECT_EQ(II
->getIntrinsicID(), Intrinsic::experimental_constrained_frem
);
310 VInt
= Builder
.CreateFPToUI(VDouble
, Builder
.getInt32Ty());
311 ASSERT_TRUE(isa
<IntrinsicInst
>(VInt
));
312 II
= cast
<IntrinsicInst
>(VInt
);
313 EXPECT_EQ(II
->getIntrinsicID(), Intrinsic::experimental_constrained_fptoui
);
315 VInt
= Builder
.CreateFPToSI(VDouble
, Builder
.getInt32Ty());
316 ASSERT_TRUE(isa
<IntrinsicInst
>(VInt
));
317 II
= cast
<IntrinsicInst
>(VInt
);
318 EXPECT_EQ(II
->getIntrinsicID(), Intrinsic::experimental_constrained_fptosi
);
320 VDouble
= Builder
.CreateUIToFP(VInt
, Builder
.getDoubleTy());
321 ASSERT_TRUE(isa
<IntrinsicInst
>(VDouble
));
322 II
= cast
<IntrinsicInst
>(VDouble
);
323 EXPECT_EQ(II
->getIntrinsicID(), Intrinsic::experimental_constrained_uitofp
);
325 VDouble
= Builder
.CreateSIToFP(VInt
, Builder
.getDoubleTy());
326 ASSERT_TRUE(isa
<IntrinsicInst
>(VDouble
));
327 II
= cast
<IntrinsicInst
>(VDouble
);
328 EXPECT_EQ(II
->getIntrinsicID(), Intrinsic::experimental_constrained_sitofp
);
330 V
= Builder
.CreateFPTrunc(VDouble
, Type::getFloatTy(Ctx
));
331 ASSERT_TRUE(isa
<IntrinsicInst
>(V
));
332 II
= cast
<IntrinsicInst
>(V
);
333 EXPECT_EQ(II
->getIntrinsicID(), Intrinsic::experimental_constrained_fptrunc
);
335 VDouble
= Builder
.CreateFPExt(V
, Type::getDoubleTy(Ctx
));
336 ASSERT_TRUE(isa
<IntrinsicInst
>(VDouble
));
337 II
= cast
<IntrinsicInst
>(VDouble
);
338 EXPECT_EQ(II
->getIntrinsicID(), Intrinsic::experimental_constrained_fpext
);
340 // Verify attributes on the call are created automatically.
341 AttributeSet CallAttrs
= II
->getAttributes().getFnAttrs();
342 EXPECT_EQ(CallAttrs
.hasAttribute(Attribute::StrictFP
), true);
344 // Verify attributes on the containing function are created when requested.
345 Builder
.setConstrainedFPFunctionAttr();
346 AttributeList Attrs
= BB
->getParent()->getAttributes();
347 AttributeSet FnAttrs
= Attrs
.getFnAttrs();
348 EXPECT_EQ(FnAttrs
.hasAttribute(Attribute::StrictFP
), true);
350 // Verify the codepaths for setting and overriding the default metadata.
351 V
= Builder
.CreateFAdd(V
, V
);
352 ASSERT_TRUE(isa
<ConstrainedFPIntrinsic
>(V
));
353 auto *CII
= cast
<ConstrainedFPIntrinsic
>(V
);
354 EXPECT_EQ(fp::ebStrict
, CII
->getExceptionBehavior());
355 EXPECT_EQ(RoundingMode::Dynamic
, CII
->getRoundingMode());
357 Builder
.setDefaultConstrainedExcept(fp::ebIgnore
);
358 Builder
.setDefaultConstrainedRounding(RoundingMode::TowardPositive
);
359 V
= Builder
.CreateFAdd(V
, V
);
360 CII
= cast
<ConstrainedFPIntrinsic
>(V
);
361 EXPECT_EQ(fp::ebIgnore
, CII
->getExceptionBehavior());
362 EXPECT_EQ(CII
->getRoundingMode(), RoundingMode::TowardPositive
);
364 Builder
.setDefaultConstrainedExcept(fp::ebIgnore
);
365 Builder
.setDefaultConstrainedRounding(RoundingMode::NearestTiesToEven
);
366 V
= Builder
.CreateFAdd(V
, V
);
367 CII
= cast
<ConstrainedFPIntrinsic
>(V
);
368 EXPECT_EQ(fp::ebIgnore
, CII
->getExceptionBehavior());
369 EXPECT_EQ(RoundingMode::NearestTiesToEven
, CII
->getRoundingMode());
371 Builder
.setDefaultConstrainedExcept(fp::ebMayTrap
);
372 Builder
.setDefaultConstrainedRounding(RoundingMode::TowardNegative
);
373 V
= Builder
.CreateFAdd(V
, V
);
374 CII
= cast
<ConstrainedFPIntrinsic
>(V
);
375 EXPECT_EQ(fp::ebMayTrap
, CII
->getExceptionBehavior());
376 EXPECT_EQ(RoundingMode::TowardNegative
, CII
->getRoundingMode());
378 Builder
.setDefaultConstrainedExcept(fp::ebStrict
);
379 Builder
.setDefaultConstrainedRounding(RoundingMode::TowardZero
);
380 V
= Builder
.CreateFAdd(V
, V
);
381 CII
= cast
<ConstrainedFPIntrinsic
>(V
);
382 EXPECT_EQ(fp::ebStrict
, CII
->getExceptionBehavior());
383 EXPECT_EQ(RoundingMode::TowardZero
, CII
->getRoundingMode());
385 Builder
.setDefaultConstrainedExcept(fp::ebIgnore
);
386 Builder
.setDefaultConstrainedRounding(RoundingMode::Dynamic
);
387 V
= Builder
.CreateFAdd(V
, V
);
388 CII
= cast
<ConstrainedFPIntrinsic
>(V
);
389 EXPECT_EQ(fp::ebIgnore
, CII
->getExceptionBehavior());
390 EXPECT_EQ(RoundingMode::Dynamic
, CII
->getRoundingMode());
392 // Now override the defaults.
393 Call
= Builder
.CreateConstrainedFPBinOp(
394 Intrinsic::experimental_constrained_fadd
, V
, V
, nullptr, "", nullptr,
395 RoundingMode::TowardNegative
, fp::ebMayTrap
);
396 CII
= cast
<ConstrainedFPIntrinsic
>(Call
);
397 EXPECT_EQ(CII
->getIntrinsicID(), Intrinsic::experimental_constrained_fadd
);
398 EXPECT_EQ(fp::ebMayTrap
, CII
->getExceptionBehavior());
399 EXPECT_EQ(RoundingMode::TowardNegative
, CII
->getRoundingMode());
401 Builder
.CreateRetVoid();
402 EXPECT_FALSE(verifyModule(*M
));
405 TEST_F(IRBuilderTest
, ConstrainedFPIntrinsics
) {
406 IRBuilder
<> Builder(BB
);
409 ConstrainedFPIntrinsic
*CII
;
410 GlobalVariable
*GVDouble
= new GlobalVariable(
411 *M
, Type::getDoubleTy(Ctx
), true, GlobalValue::ExternalLinkage
, nullptr);
412 VDouble
= Builder
.CreateLoad(GVDouble
->getValueType(), GVDouble
);
414 Builder
.setDefaultConstrainedExcept(fp::ebStrict
);
415 Builder
.setDefaultConstrainedRounding(RoundingMode::TowardZero
);
416 Function
*Fn
= Intrinsic::getDeclaration(M
.get(),
417 Intrinsic::experimental_constrained_roundeven
, { Type::getDoubleTy(Ctx
) });
418 V
= Builder
.CreateConstrainedFPCall(Fn
, { VDouble
});
419 CII
= cast
<ConstrainedFPIntrinsic
>(V
);
420 EXPECT_EQ(Intrinsic::experimental_constrained_roundeven
, CII
->getIntrinsicID());
421 EXPECT_EQ(fp::ebStrict
, CII
->getExceptionBehavior());
424 TEST_F(IRBuilderTest
, ConstrainedFPFunctionCall
) {
425 IRBuilder
<> Builder(BB
);
427 // Create an empty constrained FP function.
428 FunctionType
*FTy
= FunctionType::get(Type::getVoidTy(Ctx
),
431 Function::Create(FTy
, Function::ExternalLinkage
, "", M
.get());
432 BasicBlock
*CalleeBB
= BasicBlock::Create(Ctx
, "", Callee
);
433 IRBuilder
<> CalleeBuilder(CalleeBB
);
434 CalleeBuilder
.setIsFPConstrained(true);
435 CalleeBuilder
.setConstrainedFPFunctionAttr();
436 CalleeBuilder
.CreateRetVoid();
438 // Now call the empty constrained FP function.
439 Builder
.setIsFPConstrained(true);
440 Builder
.setConstrainedFPFunctionAttr();
441 CallInst
*FCall
= Builder
.CreateCall(Callee
, std::nullopt
);
443 // Check the attributes to verify the strictfp attribute is on the call.
445 FCall
->getAttributes().getFnAttrs().hasAttribute(Attribute::StrictFP
));
447 Builder
.CreateRetVoid();
448 EXPECT_FALSE(verifyModule(*M
));
451 TEST_F(IRBuilderTest
, Lifetime
) {
452 IRBuilder
<> Builder(BB
);
453 AllocaInst
*Var1
= Builder
.CreateAlloca(Builder
.getInt8Ty());
454 AllocaInst
*Var2
= Builder
.CreateAlloca(Builder
.getInt32Ty());
455 AllocaInst
*Var3
= Builder
.CreateAlloca(Builder
.getInt8Ty(),
456 Builder
.getInt32(123));
458 CallInst
*Start1
= Builder
.CreateLifetimeStart(Var1
);
459 CallInst
*Start2
= Builder
.CreateLifetimeStart(Var2
);
460 CallInst
*Start3
= Builder
.CreateLifetimeStart(Var3
, Builder
.getInt64(100));
462 EXPECT_EQ(Start1
->getArgOperand(0), Builder
.getInt64(-1));
463 EXPECT_EQ(Start2
->getArgOperand(0), Builder
.getInt64(-1));
464 EXPECT_EQ(Start3
->getArgOperand(0), Builder
.getInt64(100));
466 EXPECT_EQ(Start1
->getArgOperand(1), Var1
);
467 EXPECT_EQ(Start2
->getArgOperand(1)->stripPointerCasts(), Var2
);
468 EXPECT_EQ(Start3
->getArgOperand(1), Var3
);
470 Value
*End1
= Builder
.CreateLifetimeEnd(Var1
);
471 Builder
.CreateLifetimeEnd(Var2
);
472 Builder
.CreateLifetimeEnd(Var3
);
474 IntrinsicInst
*II_Start1
= dyn_cast
<IntrinsicInst
>(Start1
);
475 IntrinsicInst
*II_End1
= dyn_cast
<IntrinsicInst
>(End1
);
476 ASSERT_TRUE(II_Start1
!= nullptr);
477 EXPECT_EQ(II_Start1
->getIntrinsicID(), Intrinsic::lifetime_start
);
478 ASSERT_TRUE(II_End1
!= nullptr);
479 EXPECT_EQ(II_End1
->getIntrinsicID(), Intrinsic::lifetime_end
);
482 TEST_F(IRBuilderTest
, CreateCondBr
) {
483 IRBuilder
<> Builder(BB
);
484 BasicBlock
*TBB
= BasicBlock::Create(Ctx
, "", F
);
485 BasicBlock
*FBB
= BasicBlock::Create(Ctx
, "", F
);
487 BranchInst
*BI
= Builder
.CreateCondBr(Builder
.getTrue(), TBB
, FBB
);
488 Instruction
*TI
= BB
->getTerminator();
490 EXPECT_EQ(2u, TI
->getNumSuccessors());
491 EXPECT_EQ(TBB
, TI
->getSuccessor(0));
492 EXPECT_EQ(FBB
, TI
->getSuccessor(1));
494 BI
->eraseFromParent();
495 MDNode
*Weights
= MDBuilder(Ctx
).createBranchWeights(42, 13);
496 BI
= Builder
.CreateCondBr(Builder
.getTrue(), TBB
, FBB
, Weights
);
497 TI
= BB
->getTerminator();
499 EXPECT_EQ(2u, TI
->getNumSuccessors());
500 EXPECT_EQ(TBB
, TI
->getSuccessor(0));
501 EXPECT_EQ(FBB
, TI
->getSuccessor(1));
502 EXPECT_EQ(Weights
, TI
->getMetadata(LLVMContext::MD_prof
));
505 TEST_F(IRBuilderTest
, LandingPadName
) {
506 IRBuilder
<> Builder(BB
);
507 LandingPadInst
*LP
= Builder
.CreateLandingPad(Builder
.getInt32Ty(), 0, "LP");
508 EXPECT_EQ(LP
->getName(), "LP");
511 TEST_F(IRBuilderTest
, DataLayout
) {
512 std::unique_ptr
<Module
> M(new Module("test", Ctx
));
513 M
->setDataLayout("e-n32");
514 EXPECT_TRUE(M
->getDataLayout().isLegalInteger(32));
515 M
->setDataLayout("e");
516 EXPECT_FALSE(M
->getDataLayout().isLegalInteger(32));
519 TEST_F(IRBuilderTest
, GetIntTy
) {
520 IRBuilder
<> Builder(BB
);
521 IntegerType
*Ty1
= Builder
.getInt1Ty();
522 EXPECT_EQ(Ty1
, IntegerType::get(Ctx
, 1));
524 DataLayout
* DL
= new DataLayout(M
.get());
525 IntegerType
*IntPtrTy
= Builder
.getIntPtrTy(*DL
);
526 unsigned IntPtrBitSize
= DL
->getPointerSizeInBits(0);
527 EXPECT_EQ(IntPtrTy
, IntegerType::get(Ctx
, IntPtrBitSize
));
531 TEST_F(IRBuilderTest
, UnaryOperators
) {
532 IRBuilder
<NoFolder
> Builder(BB
);
533 Value
*V
= Builder
.CreateLoad(GV
->getValueType(), GV
);
535 // Test CreateUnOp(X)
536 Value
*U
= Builder
.CreateUnOp(Instruction::FNeg
, V
);
537 ASSERT_TRUE(isa
<Instruction
>(U
));
538 ASSERT_TRUE(isa
<FPMathOperator
>(U
));
539 ASSERT_TRUE(isa
<UnaryOperator
>(U
));
540 ASSERT_FALSE(isa
<BinaryOperator
>(U
));
542 // Test CreateFNegFMF(X)
543 Instruction
*I
= cast
<Instruction
>(U
);
544 I
->setHasNoSignedZeros(true);
545 I
->setHasNoNaNs(true);
546 Value
*VFMF
= Builder
.CreateFNegFMF(V
, I
);
547 Instruction
*IFMF
= cast
<Instruction
>(VFMF
);
548 EXPECT_TRUE(IFMF
->hasNoSignedZeros());
549 EXPECT_TRUE(IFMF
->hasNoNaNs());
550 EXPECT_FALSE(IFMF
->hasAllowReassoc());
553 TEST_F(IRBuilderTest
, FastMathFlags
) {
554 IRBuilder
<> Builder(BB
);
556 Instruction
*FDiv
, *FAdd
, *FCmp
, *FCall
, *FNeg
, *FSub
, *FMul
, *FRem
;
558 F
= Builder
.CreateLoad(GV
->getValueType(), GV
);
559 F
= Builder
.CreateFAdd(F
, F
);
561 EXPECT_FALSE(Builder
.getFastMathFlags().any());
562 ASSERT_TRUE(isa
<Instruction
>(F
));
563 FAdd
= cast
<Instruction
>(F
);
564 EXPECT_FALSE(FAdd
->hasNoNaNs());
567 Builder
.setFastMathFlags(FMF
);
569 // By default, no flags are set.
570 F
= Builder
.CreateFAdd(F
, F
);
571 EXPECT_FALSE(Builder
.getFastMathFlags().any());
572 ASSERT_TRUE(isa
<Instruction
>(F
));
573 FAdd
= cast
<Instruction
>(F
);
574 EXPECT_FALSE(FAdd
->hasNoNaNs());
575 EXPECT_FALSE(FAdd
->hasNoInfs());
576 EXPECT_FALSE(FAdd
->hasNoSignedZeros());
577 EXPECT_FALSE(FAdd
->hasAllowReciprocal());
578 EXPECT_FALSE(FAdd
->hasAllowContract());
579 EXPECT_FALSE(FAdd
->hasAllowReassoc());
580 EXPECT_FALSE(FAdd
->hasApproxFunc());
582 // Set all flags in the instruction.
584 EXPECT_TRUE(FAdd
->hasNoNaNs());
585 EXPECT_TRUE(FAdd
->hasNoInfs());
586 EXPECT_TRUE(FAdd
->hasNoSignedZeros());
587 EXPECT_TRUE(FAdd
->hasAllowReciprocal());
588 EXPECT_TRUE(FAdd
->hasAllowContract());
589 EXPECT_TRUE(FAdd
->hasAllowReassoc());
590 EXPECT_TRUE(FAdd
->hasApproxFunc());
592 // All flags are set in the builder.
594 Builder
.setFastMathFlags(FMF
);
596 F
= Builder
.CreateFAdd(F
, F
);
597 EXPECT_TRUE(Builder
.getFastMathFlags().any());
598 EXPECT_TRUE(Builder
.getFastMathFlags().all());
599 ASSERT_TRUE(isa
<Instruction
>(F
));
600 FAdd
= cast
<Instruction
>(F
);
601 EXPECT_TRUE(FAdd
->hasNoNaNs());
602 EXPECT_TRUE(FAdd
->isFast());
604 // Now, try it with CreateBinOp
605 F
= Builder
.CreateBinOp(Instruction::FAdd
, F
, F
);
606 EXPECT_TRUE(Builder
.getFastMathFlags().any());
607 ASSERT_TRUE(isa
<Instruction
>(F
));
608 FAdd
= cast
<Instruction
>(F
);
609 EXPECT_TRUE(FAdd
->hasNoNaNs());
610 EXPECT_TRUE(FAdd
->isFast());
612 F
= Builder
.CreateFDiv(F
, F
);
613 EXPECT_TRUE(Builder
.getFastMathFlags().all());
614 ASSERT_TRUE(isa
<Instruction
>(F
));
615 FDiv
= cast
<Instruction
>(F
);
616 EXPECT_TRUE(FDiv
->hasAllowReciprocal());
618 // Clear all FMF in the builder.
619 Builder
.clearFastMathFlags();
621 F
= Builder
.CreateFDiv(F
, F
);
622 ASSERT_TRUE(isa
<Instruction
>(F
));
623 FDiv
= cast
<Instruction
>(F
);
624 EXPECT_FALSE(FDiv
->hasAllowReciprocal());
626 // Try individual flags.
628 FMF
.setAllowReciprocal();
629 Builder
.setFastMathFlags(FMF
);
631 F
= Builder
.CreateFDiv(F
, F
);
632 EXPECT_TRUE(Builder
.getFastMathFlags().any());
633 EXPECT_TRUE(Builder
.getFastMathFlags().AllowReciprocal
);
634 ASSERT_TRUE(isa
<Instruction
>(F
));
635 FDiv
= cast
<Instruction
>(F
);
636 EXPECT_TRUE(FDiv
->hasAllowReciprocal());
638 Builder
.clearFastMathFlags();
640 FC
= Builder
.CreateFCmpOEQ(F
, F
);
641 ASSERT_TRUE(isa
<Instruction
>(FC
));
642 FCmp
= cast
<Instruction
>(FC
);
643 EXPECT_FALSE(FCmp
->hasAllowReciprocal());
646 FMF
.setAllowReciprocal();
647 Builder
.setFastMathFlags(FMF
);
649 FC
= Builder
.CreateFCmpOEQ(F
, F
);
650 EXPECT_TRUE(Builder
.getFastMathFlags().any());
651 EXPECT_TRUE(Builder
.getFastMathFlags().AllowReciprocal
);
652 ASSERT_TRUE(isa
<Instruction
>(FC
));
653 FCmp
= cast
<Instruction
>(FC
);
654 EXPECT_TRUE(FCmp
->hasAllowReciprocal());
656 Builder
.clearFastMathFlags();
659 FC
= Builder
.CreateFAdd(F
, F
);
660 ASSERT_TRUE(isa
<Instruction
>(FC
));
661 FAdd
= cast
<Instruction
>(FC
);
662 EXPECT_FALSE(FAdd
->hasAllowContract());
665 FMF
.setAllowContract(true);
666 Builder
.setFastMathFlags(FMF
);
668 FC
= Builder
.CreateFAdd(F
, F
);
669 EXPECT_TRUE(Builder
.getFastMathFlags().any());
670 EXPECT_TRUE(Builder
.getFastMathFlags().AllowContract
);
671 ASSERT_TRUE(isa
<Instruction
>(FC
));
672 FAdd
= cast
<Instruction
>(FC
);
673 EXPECT_TRUE(FAdd
->hasAllowContract());
676 Builder
.clearFastMathFlags();
677 Builder
.setFastMathFlags(FMF
);
678 // Now 'aml' and 'contract' are set.
679 F
= Builder
.CreateFMul(F
, F
);
680 FAdd
= cast
<Instruction
>(F
);
681 EXPECT_TRUE(FAdd
->hasApproxFunc());
682 EXPECT_TRUE(FAdd
->hasAllowContract());
683 EXPECT_FALSE(FAdd
->hasAllowReassoc());
685 FMF
.setAllowReassoc();
686 Builder
.clearFastMathFlags();
687 Builder
.setFastMathFlags(FMF
);
688 // Now 'aml' and 'contract' and 'reassoc' are set.
689 F
= Builder
.CreateFMul(F
, F
);
690 FAdd
= cast
<Instruction
>(F
);
691 EXPECT_TRUE(FAdd
->hasApproxFunc());
692 EXPECT_TRUE(FAdd
->hasAllowContract());
693 EXPECT_TRUE(FAdd
->hasAllowReassoc());
695 // Test a call with FMF.
696 auto CalleeTy
= FunctionType::get(Type::getFloatTy(Ctx
),
699 Function::Create(CalleeTy
, Function::ExternalLinkage
, "", M
.get());
701 FCall
= Builder
.CreateCall(Callee
, std::nullopt
);
702 EXPECT_FALSE(FCall
->hasNoNaNs());
705 Function::Create(CalleeTy
, Function::ExternalLinkage
, "", M
.get());
706 FCall
= Builder
.CreateCall(V
, std::nullopt
);
707 EXPECT_FALSE(FCall
->hasNoNaNs());
711 Builder
.setFastMathFlags(FMF
);
713 FCall
= Builder
.CreateCall(Callee
, std::nullopt
);
714 EXPECT_TRUE(Builder
.getFastMathFlags().any());
715 EXPECT_TRUE(Builder
.getFastMathFlags().NoNaNs
);
716 EXPECT_TRUE(FCall
->hasNoNaNs());
718 FCall
= Builder
.CreateCall(V
, std::nullopt
);
719 EXPECT_TRUE(Builder
.getFastMathFlags().any());
720 EXPECT_TRUE(Builder
.getFastMathFlags().NoNaNs
);
721 EXPECT_TRUE(FCall
->hasNoNaNs());
723 Builder
.clearFastMathFlags();
725 // To test a copy, make sure that a '0' and a '1' change state.
726 F
= Builder
.CreateFDiv(F
, F
);
727 ASSERT_TRUE(isa
<Instruction
>(F
));
728 FDiv
= cast
<Instruction
>(F
);
729 EXPECT_FALSE(FDiv
->getFastMathFlags().any());
730 FDiv
->setHasAllowReciprocal(true);
731 FAdd
->setHasAllowReciprocal(false);
732 FAdd
->setHasNoNaNs(true);
733 FDiv
->copyFastMathFlags(FAdd
);
734 EXPECT_TRUE(FDiv
->hasNoNaNs());
735 EXPECT_FALSE(FDiv
->hasAllowReciprocal());
737 // Test that CreateF*FMF functions copy flags from the source instruction
738 // instead of using the builder default.
739 Instruction
*const FMFSource
= FAdd
;
740 EXPECT_FALSE(Builder
.getFastMathFlags().noNaNs());
741 EXPECT_TRUE(FMFSource
->hasNoNaNs());
743 F
= Builder
.CreateFNegFMF(F
, FMFSource
);
744 ASSERT_TRUE(isa
<Instruction
>(F
));
745 FNeg
= cast
<Instruction
>(F
);
746 EXPECT_TRUE(FNeg
->hasNoNaNs());
747 F
= Builder
.CreateFAddFMF(F
, F
, FMFSource
);
748 ASSERT_TRUE(isa
<Instruction
>(F
));
749 FAdd
= cast
<Instruction
>(F
);
750 EXPECT_TRUE(FAdd
->hasNoNaNs());
751 F
= Builder
.CreateFSubFMF(F
, F
, FMFSource
);
752 ASSERT_TRUE(isa
<Instruction
>(F
));
753 FSub
= cast
<Instruction
>(F
);
754 EXPECT_TRUE(FSub
->hasNoNaNs());
755 F
= Builder
.CreateFMulFMF(F
, F
, FMFSource
);
756 ASSERT_TRUE(isa
<Instruction
>(F
));
757 FMul
= cast
<Instruction
>(F
);
758 EXPECT_TRUE(FMul
->hasNoNaNs());
759 F
= Builder
.CreateFDivFMF(F
, F
, FMFSource
);
760 ASSERT_TRUE(isa
<Instruction
>(F
));
761 FDiv
= cast
<Instruction
>(F
);
762 EXPECT_TRUE(FDiv
->hasNoNaNs());
763 F
= Builder
.CreateFRemFMF(F
, F
, FMFSource
);
764 ASSERT_TRUE(isa
<Instruction
>(F
));
765 FRem
= cast
<Instruction
>(F
);
766 EXPECT_TRUE(FRem
->hasNoNaNs());
769 TEST_F(IRBuilderTest
, WrapFlags
) {
770 IRBuilder
<NoFolder
> Builder(BB
);
772 // Test instructions.
773 GlobalVariable
*G
= new GlobalVariable(*M
, Builder
.getInt32Ty(), true,
774 GlobalValue::ExternalLinkage
, nullptr);
775 Value
*V
= Builder
.CreateLoad(G
->getValueType(), G
);
777 cast
<BinaryOperator
>(Builder
.CreateNSWAdd(V
, V
))->hasNoSignedWrap());
779 cast
<BinaryOperator
>(Builder
.CreateNSWMul(V
, V
))->hasNoSignedWrap());
781 cast
<BinaryOperator
>(Builder
.CreateNSWSub(V
, V
))->hasNoSignedWrap());
782 EXPECT_TRUE(cast
<BinaryOperator
>(
783 Builder
.CreateShl(V
, V
, "", /* NUW */ false, /* NSW */ true))
784 ->hasNoSignedWrap());
787 cast
<BinaryOperator
>(Builder
.CreateNUWAdd(V
, V
))->hasNoUnsignedWrap());
789 cast
<BinaryOperator
>(Builder
.CreateNUWMul(V
, V
))->hasNoUnsignedWrap());
791 cast
<BinaryOperator
>(Builder
.CreateNUWSub(V
, V
))->hasNoUnsignedWrap());
792 EXPECT_TRUE(cast
<BinaryOperator
>(
793 Builder
.CreateShl(V
, V
, "", /* NUW */ true, /* NSW */ false))
794 ->hasNoUnsignedWrap());
796 // Test operators created with constants.
797 Constant
*C
= Builder
.getInt32(42);
798 EXPECT_TRUE(cast
<OverflowingBinaryOperator
>(Builder
.CreateNSWAdd(C
, C
))
799 ->hasNoSignedWrap());
800 EXPECT_TRUE(cast
<OverflowingBinaryOperator
>(Builder
.CreateNSWSub(C
, C
))
801 ->hasNoSignedWrap());
802 EXPECT_TRUE(cast
<OverflowingBinaryOperator
>(Builder
.CreateNSWMul(C
, C
))
803 ->hasNoSignedWrap());
804 EXPECT_TRUE(cast
<OverflowingBinaryOperator
>(
805 Builder
.CreateShl(C
, C
, "", /* NUW */ false, /* NSW */ true))
806 ->hasNoSignedWrap());
808 EXPECT_TRUE(cast
<OverflowingBinaryOperator
>(Builder
.CreateNUWAdd(C
, C
))
809 ->hasNoUnsignedWrap());
810 EXPECT_TRUE(cast
<OverflowingBinaryOperator
>(Builder
.CreateNUWSub(C
, C
))
811 ->hasNoUnsignedWrap());
812 EXPECT_TRUE(cast
<OverflowingBinaryOperator
>(Builder
.CreateNUWMul(C
, C
))
813 ->hasNoUnsignedWrap());
814 EXPECT_TRUE(cast
<OverflowingBinaryOperator
>(
815 Builder
.CreateShl(C
, C
, "", /* NUW */ true, /* NSW */ false))
816 ->hasNoUnsignedWrap());
819 TEST_F(IRBuilderTest
, RAIIHelpersTest
) {
820 IRBuilder
<> Builder(BB
);
821 EXPECT_FALSE(Builder
.getFastMathFlags().allowReciprocal());
822 MDBuilder
MDB(M
->getContext());
824 MDNode
*FPMathA
= MDB
.createFPMath(0.01f
);
825 MDNode
*FPMathB
= MDB
.createFPMath(0.1f
);
827 Builder
.setDefaultFPMathTag(FPMathA
);
830 IRBuilder
<>::FastMathFlagGuard
Guard(Builder
);
832 FMF
.setAllowReciprocal();
833 Builder
.setFastMathFlags(FMF
);
834 Builder
.setDefaultFPMathTag(FPMathB
);
835 EXPECT_TRUE(Builder
.getFastMathFlags().allowReciprocal());
836 EXPECT_EQ(FPMathB
, Builder
.getDefaultFPMathTag());
839 EXPECT_FALSE(Builder
.getFastMathFlags().allowReciprocal());
840 EXPECT_EQ(FPMathA
, Builder
.getDefaultFPMathTag());
842 Value
*F
= Builder
.CreateLoad(GV
->getValueType(), GV
);
845 IRBuilder
<>::InsertPointGuard
Guard(Builder
);
846 Builder
.SetInsertPoint(cast
<Instruction
>(F
));
847 EXPECT_EQ(F
, &*Builder
.GetInsertPoint());
850 EXPECT_EQ(BB
->end(), Builder
.GetInsertPoint());
851 EXPECT_EQ(BB
, Builder
.GetInsertBlock());
854 TEST_F(IRBuilderTest
, createFunction
) {
855 IRBuilder
<> Builder(BB
);
857 auto File
= DIB
.createFile("error.swift", "/");
859 DIB
.createCompileUnit(dwarf::DW_LANG_Swift
, File
, "swiftc", true, "", 0);
860 auto Type
= DIB
.createSubroutineType(DIB
.getOrCreateTypeArray(std::nullopt
));
861 auto NoErr
= DIB
.createFunction(
862 CU
, "noerr", "", File
, 1, Type
, 1, DINode::FlagZero
,
863 DISubprogram::SPFlagDefinition
| DISubprogram::SPFlagOptimized
);
864 EXPECT_TRUE(!NoErr
->getThrownTypes());
865 auto Int
= DIB
.createBasicType("Int", 64, dwarf::DW_ATE_signed
);
866 auto Error
= DIB
.getOrCreateArray({Int
});
867 auto Err
= DIB
.createFunction(
868 CU
, "err", "", File
, 1, Type
, 1, DINode::FlagZero
,
869 DISubprogram::SPFlagDefinition
| DISubprogram::SPFlagOptimized
, nullptr,
870 nullptr, Error
.get());
871 EXPECT_TRUE(Err
->getThrownTypes().get() == Error
.get());
875 TEST_F(IRBuilderTest
, DIBuilder
) {
876 auto GetLastDbgRecord
= [](const Instruction
*I
) -> DbgRecord
* {
877 if (I
->getDbgRecordRange().empty())
879 return &*std::prev(I
->getDbgRecordRange().end());
882 auto ExpectOrder
= [&](DbgInstPtr First
, BasicBlock::iterator Second
) {
883 if (M
->IsNewDbgInfoFormat
) {
884 EXPECT_TRUE(First
.is
<DbgRecord
*>());
885 EXPECT_FALSE(Second
->getDbgRecordRange().empty());
886 EXPECT_EQ(GetLastDbgRecord(&*Second
), First
.get
<DbgRecord
*>());
888 EXPECT_TRUE(First
.is
<Instruction
*>());
889 EXPECT_EQ(&*std::prev(Second
), First
.get
<Instruction
*>());
893 auto RunTest
= [&]() {
894 IRBuilder
<> Builder(BB
);
896 auto File
= DIB
.createFile("F.CBL", "/");
897 auto CU
= DIB
.createCompileUnit(dwarf::DW_LANG_Cobol74
,
898 DIB
.createFile("F.CBL", "/"),
899 "llvm-cobol74", true, "", 0);
901 DIB
.createSubroutineType(DIB
.getOrCreateTypeArray(std::nullopt
));
902 auto SP
= DIB
.createFunction(
903 CU
, "foo", "", File
, 1, Type
, 1, DINode::FlagZero
,
904 DISubprogram::SPFlagDefinition
| DISubprogram::SPFlagOptimized
);
905 F
->setSubprogram(SP
);
906 AllocaInst
*I
= Builder
.CreateAlloca(Builder
.getInt8Ty());
907 auto BarSP
= DIB
.createFunction(
908 CU
, "bar", "", File
, 1, Type
, 1, DINode::FlagZero
,
909 DISubprogram::SPFlagDefinition
| DISubprogram::SPFlagOptimized
);
910 auto BarScope
= DIB
.createLexicalBlockFile(BarSP
, File
, 0);
911 I
->setDebugLoc(DILocation::get(Ctx
, 2, 0, BarScope
));
913 // Create another instruction so that there's one before the alloca we're
914 // inserting debug intrinsics before, to make end-checking easier.
915 I
= Builder
.CreateAlloca(Builder
.getInt1Ty());
917 // Label metadata and records
918 // --------------------------
919 DILocation
*LabelLoc
= DILocation::get(Ctx
, 1, 0, BarScope
);
920 DILabel
*AlwaysPreserveLabel
= DIB
.createLabel(
921 BarScope
, "meles_meles", File
, 1, /*AlwaysPreserve*/ true);
923 DIB
.createLabel(BarScope
, "badger", File
, 1, /*AlwaysPreserve*/ false);
925 { /* dbg.label | DbgLabelRecord */
926 // Insert before I and check order.
927 ExpectOrder(DIB
.insertLabel(Label
, LabelLoc
, I
), I
->getIterator());
929 // We should be able to insert at the end of the block, even if there's
930 // no terminator yet. Note that in RemoveDIs mode this record won't get
931 // inserted into the block untill another instruction is added.
932 DbgInstPtr LabelRecord
= DIB
.insertLabel(Label
, LabelLoc
, BB
);
933 // Specifically do not insert a terminator, to check this works. `I`
934 // should have absorbed the DbgLabelRecord in the new debug info mode.
935 I
= Builder
.CreateAlloca(Builder
.getInt32Ty());
936 ExpectOrder(LabelRecord
, I
->getIterator());
939 // Variable metadata and records
940 // -----------------------------
941 DILocation
*VarLoc
= DILocation::get(Ctx
, 2, 0, BarScope
);
942 auto *IntType
= DIB
.createBasicType("int", 32, dwarf::DW_ATE_signed
);
943 DILocalVariable
*VarX
=
944 DIB
.createAutoVariable(BarSP
, "X", File
, 2, IntType
, true);
945 DILocalVariable
*VarY
=
946 DIB
.createAutoVariable(BarSP
, "Y", File
, 2, IntType
, true);
947 { /* dbg.value | DbgVariableRecord::Value */
948 ExpectOrder(DIB
.insertDbgValueIntrinsic(I
, VarX
, DIB
.createExpression(),
951 // Check inserting at end of the block works as with labels.
952 DbgInstPtr VarXValue
= DIB
.insertDbgValueIntrinsic(
953 I
, VarX
, DIB
.createExpression(), VarLoc
, BB
);
954 I
= Builder
.CreateAlloca(Builder
.getInt32Ty());
955 ExpectOrder(VarXValue
, I
->getIterator());
956 EXPECT_EQ(BB
->getTrailingDbgRecords(), nullptr);
958 { /* dbg.declare | DbgVariableRecord::Declare */
959 ExpectOrder(DIB
.insertDeclare(I
, VarY
, DIB
.createExpression(), VarLoc
, I
),
961 // Check inserting at end of the block works as with labels.
962 DbgInstPtr VarYDeclare
=
963 DIB
.insertDeclare(I
, VarY
, DIB
.createExpression(), VarLoc
, BB
);
964 I
= Builder
.CreateAlloca(Builder
.getInt32Ty());
965 ExpectOrder(VarYDeclare
, I
->getIterator());
966 EXPECT_EQ(BB
->getTrailingDbgRecords(), nullptr);
968 { /* dbg.assign | DbgVariableRecord::Assign */
969 I
= Builder
.CreateAlloca(Builder
.getInt32Ty());
970 I
->setMetadata(LLVMContext::MD_DIAssignID
, DIAssignID::getDistinct(Ctx
));
971 // DbgAssign interface is slightly different - it always inserts after the
972 // linked instr. Check we can do this with no instruction to insert
974 DbgInstPtr VarXAssign
=
975 DIB
.insertDbgAssign(I
, I
, VarX
, DIB
.createExpression(), I
,
976 DIB
.createExpression(), VarLoc
);
977 I
= Builder
.CreateAlloca(Builder
.getInt32Ty());
978 ExpectOrder(VarXAssign
, I
->getIterator());
979 EXPECT_EQ(BB
->getTrailingDbgRecords(), nullptr);
982 Builder
.CreateRet(nullptr);
984 // Check the labels are not/are added to Bar's retainedNodes array
986 EXPECT_EQ(find(BarSP
->getRetainedNodes(), Label
),
987 BarSP
->getRetainedNodes().end());
988 EXPECT_NE(find(BarSP
->getRetainedNodes(), AlwaysPreserveLabel
),
989 BarSP
->getRetainedNodes().end());
990 EXPECT_NE(find(BarSP
->getRetainedNodes(), VarX
),
991 BarSP
->getRetainedNodes().end());
992 EXPECT_NE(find(BarSP
->getRetainedNodes(), VarY
),
993 BarSP
->getRetainedNodes().end());
994 EXPECT_TRUE(verifyModule(*M
));
997 // Test in new-debug mode.
998 EXPECT_TRUE(M
->IsNewDbgInfoFormat
);
1001 // Test in old-debug mode.
1002 // Reset the test then call convertFromNewDbgValues to flip the flag
1003 // on the test's Module, Function and BasicBlock.
1006 M
->convertFromNewDbgValues();
1007 EXPECT_FALSE(M
->IsNewDbgInfoFormat
);
1011 TEST_F(IRBuilderTest
, createArtificialSubprogram
) {
1012 IRBuilder
<> Builder(BB
);
1014 auto File
= DIB
.createFile("main.c", "/");
1015 auto CU
= DIB
.createCompileUnit(dwarf::DW_LANG_C
, File
, "clang",
1016 /*isOptimized=*/true, /*Flags=*/"",
1017 /*Runtime Version=*/0);
1018 auto Type
= DIB
.createSubroutineType(DIB
.getOrCreateTypeArray(std::nullopt
));
1019 auto SP
= DIB
.createFunction(
1020 CU
, "foo", /*LinkageName=*/"", File
,
1021 /*LineNo=*/1, Type
, /*ScopeLine=*/2, DINode::FlagZero
,
1022 DISubprogram::SPFlagDefinition
| DISubprogram::SPFlagOptimized
);
1023 EXPECT_TRUE(SP
->isDistinct());
1025 F
->setSubprogram(SP
);
1026 AllocaInst
*I
= Builder
.CreateAlloca(Builder
.getInt8Ty());
1027 ReturnInst
*R
= Builder
.CreateRetVoid();
1028 I
->setDebugLoc(DILocation::get(Ctx
, 3, 2, SP
));
1029 R
->setDebugLoc(DILocation::get(Ctx
, 4, 2, SP
));
1031 EXPECT_FALSE(verifyModule(*M
));
1033 Function
*G
= Function::Create(F
->getFunctionType(),
1034 Function::ExternalLinkage
, "", M
.get());
1035 BasicBlock
*GBB
= BasicBlock::Create(Ctx
, "", G
);
1036 Builder
.SetInsertPoint(GBB
);
1037 I
->removeFromParent();
1039 Builder
.CreateRetVoid();
1040 EXPECT_FALSE(verifyModule(*M
));
1042 DISubprogram
*GSP
= DIBuilder::createArtificialSubprogram(F
->getSubprogram());
1043 EXPECT_EQ(SP
->getFile(), GSP
->getFile());
1044 EXPECT_EQ(SP
->getType(), GSP
->getType());
1045 EXPECT_EQ(SP
->getLine(), GSP
->getLine());
1046 EXPECT_EQ(SP
->getScopeLine(), GSP
->getScopeLine());
1047 EXPECT_TRUE(GSP
->isDistinct());
1049 G
->setSubprogram(GSP
);
1050 EXPECT_TRUE(verifyModule(*M
));
1052 auto *InlinedAtNode
=
1053 DILocation::getDistinct(Ctx
, GSP
->getScopeLine(), 0, GSP
);
1054 DebugLoc DL
= I
->getDebugLoc();
1055 DenseMap
<const MDNode
*, MDNode
*> IANodes
;
1056 auto IA
= DebugLoc::appendInlinedAt(DL
, InlinedAtNode
, Ctx
, IANodes
);
1058 DILocation::get(Ctx
, DL
.getLine(), DL
.getCol(), DL
.getScope(), IA
);
1059 I
->setDebugLoc(NewDL
);
1060 EXPECT_FALSE(verifyModule(*M
));
1062 EXPECT_EQ("foo", SP
->getName());
1063 EXPECT_EQ("foo", GSP
->getName());
1064 EXPECT_FALSE(SP
->isArtificial());
1065 EXPECT_TRUE(GSP
->isArtificial());
1068 // Check that we can add debug info to an existing DICompileUnit.
1069 TEST_F(IRBuilderTest
, appendDebugInfo
) {
1070 IRBuilder
<> Builder(BB
);
1071 Builder
.CreateRetVoid();
1072 EXPECT_FALSE(verifyModule(*M
));
1074 auto GetNames
= [](DICompileUnit
*CU
) {
1075 SmallVector
<StringRef
> Names
;
1076 for (auto *ET
: CU
->getEnumTypes())
1077 Names
.push_back(ET
->getName());
1078 for (auto *RT
: CU
->getRetainedTypes())
1079 Names
.push_back(RT
->getName());
1080 for (auto *GV
: CU
->getGlobalVariables())
1081 Names
.push_back(GV
->getVariable()->getName());
1082 for (auto *IE
: CU
->getImportedEntities())
1083 Names
.push_back(IE
->getName());
1084 for (auto *Node
: CU
->getMacros())
1085 if (auto *MN
= dyn_cast_or_null
<DIMacro
>(Node
))
1086 Names
.push_back(MN
->getName());
1093 auto *File
= DIB
.createFile("main.c", "/");
1094 CU
= DIB
.createCompileUnit(dwarf::DW_LANG_C
, File
, "clang",
1095 /*isOptimized=*/true, /*Flags=*/"",
1096 /*Runtime Version=*/0);
1097 auto *ByteTy
= DIB
.createBasicType("byte0", 8, dwarf::DW_ATE_signed
);
1098 DIB
.createEnumerationType(CU
, "ET0", File
, /*LineNo=*/0, /*SizeInBits=*/8,
1099 /*AlignInBits=*/8, /*Elements=*/{}, ByteTy
);
1100 DIB
.retainType(ByteTy
);
1101 DIB
.createGlobalVariableExpression(CU
, "GV0", /*LinkageName=*/"", File
,
1102 /*LineNo=*/1, ByteTy
,
1103 /*IsLocalToUnit=*/true);
1104 DIB
.createImportedDeclaration(CU
, nullptr, File
, /*LineNo=*/2, "IM0");
1105 DIB
.createMacro(nullptr, /*LineNo=*/0, dwarf::DW_MACINFO_define
, "M0");
1108 EXPECT_FALSE(verifyModule(*M
));
1109 EXPECT_THAT(GetNames(CU
),
1110 UnorderedElementsAre("ET0", "byte0", "GV0", "IM0", "M0"));
1113 DIBuilder
DIB(*M
, true, CU
);
1114 auto *File
= CU
->getFile();
1115 auto *ByteTy
= DIB
.createBasicType("byte1", 8, dwarf::DW_ATE_signed
);
1116 DIB
.createEnumerationType(CU
, "ET1", File
, /*LineNo=*/0,
1117 /*SizeInBits=*/8, /*AlignInBits=*/8,
1118 /*Elements=*/{}, ByteTy
);
1119 DIB
.retainType(ByteTy
);
1120 DIB
.createGlobalVariableExpression(CU
, "GV1", /*LinkageName=*/"", File
,
1121 /*LineNo=*/1, ByteTy
,
1122 /*IsLocalToUnit=*/true);
1123 DIB
.createImportedDeclaration(CU
, nullptr, File
, /*LineNo=*/2, "IM1");
1124 DIB
.createMacro(nullptr, /*LineNo=*/0, dwarf::DW_MACINFO_define
, "M1");
1127 EXPECT_FALSE(verifyModule(*M
));
1128 EXPECT_THAT(GetNames(CU
),
1129 UnorderedElementsAre("ET0", "byte0", "GV0", "IM0", "M0", "ET1",
1130 "byte1", "GV1", "IM1", "M1"));
1133 TEST_F(IRBuilderTest
, InsertExtractElement
) {
1134 IRBuilder
<> Builder(BB
);
1136 auto VecTy
= FixedVectorType::get(Builder
.getInt64Ty(), 4);
1137 auto Elt1
= Builder
.getInt64(-1);
1138 auto Elt2
= Builder
.getInt64(-2);
1139 Value
*Vec
= Builder
.CreateInsertElement(VecTy
, Elt1
, Builder
.getInt8(1));
1140 Vec
= Builder
.CreateInsertElement(Vec
, Elt2
, 2);
1141 auto X1
= Builder
.CreateExtractElement(Vec
, 1);
1142 auto X2
= Builder
.CreateExtractElement(Vec
, Builder
.getInt32(2));
1143 EXPECT_EQ(Elt1
, X1
);
1144 EXPECT_EQ(Elt2
, X2
);
1147 TEST_F(IRBuilderTest
, CreateGlobalStringPtr
) {
1148 IRBuilder
<> Builder(BB
);
1150 auto String1a
= Builder
.CreateGlobalStringPtr("TestString", "String1a");
1151 auto String1b
= Builder
.CreateGlobalStringPtr("TestString", "String1b", 0);
1152 auto String2
= Builder
.CreateGlobalStringPtr("TestString", "String2", 1);
1153 auto String3
= Builder
.CreateGlobalString("TestString", "String3", 2);
1155 EXPECT_TRUE(String1a
->getType()->getPointerAddressSpace() == 0);
1156 EXPECT_TRUE(String1b
->getType()->getPointerAddressSpace() == 0);
1157 EXPECT_TRUE(String2
->getType()->getPointerAddressSpace() == 1);
1158 EXPECT_TRUE(String3
->getType()->getPointerAddressSpace() == 2);
1161 TEST_F(IRBuilderTest
, DebugLoc
) {
1162 auto CalleeTy
= FunctionType::get(Type::getVoidTy(Ctx
),
1163 /*isVarArg=*/false);
1165 Function::Create(CalleeTy
, Function::ExternalLinkage
, "", M
.get());
1168 auto File
= DIB
.createFile("tmp.cpp", "/");
1169 auto CU
= DIB
.createCompileUnit(dwarf::DW_LANG_C_plus_plus_11
,
1170 DIB
.createFile("tmp.cpp", "/"), "", true, "",
1173 DIB
.createSubroutineType(DIB
.getOrCreateTypeArray(std::nullopt
));
1175 DIB
.createFunction(CU
, "foo", "foo", File
, 1, SPType
, 1, DINode::FlagZero
,
1176 DISubprogram::SPFlagDefinition
);
1177 DebugLoc DL1
= DILocation::get(Ctx
, 2, 0, SP
);
1178 DebugLoc DL2
= DILocation::get(Ctx
, 3, 0, SP
);
1180 auto BB2
= BasicBlock::Create(Ctx
, "bb2", F
);
1181 auto Br
= BranchInst::Create(BB2
, BB
);
1182 Br
->setDebugLoc(DL1
);
1184 IRBuilder
<> Builder(Ctx
);
1185 Builder
.SetInsertPoint(Br
);
1186 EXPECT_EQ(DL1
, Builder
.getCurrentDebugLocation());
1187 auto Call1
= Builder
.CreateCall(Callee
, std::nullopt
);
1188 EXPECT_EQ(DL1
, Call1
->getDebugLoc());
1190 Call1
->setDebugLoc(DL2
);
1191 Builder
.SetInsertPoint(Call1
->getParent(), Call1
->getIterator());
1192 EXPECT_EQ(DL2
, Builder
.getCurrentDebugLocation());
1193 auto Call2
= Builder
.CreateCall(Callee
, std::nullopt
);
1194 EXPECT_EQ(DL2
, Call2
->getDebugLoc());
1199 TEST_F(IRBuilderTest
, DIImportedEntity
) {
1200 IRBuilder
<> Builder(BB
);
1202 auto F
= DIB
.createFile("F.CBL", "/");
1203 auto CU
= DIB
.createCompileUnit(dwarf::DW_LANG_Cobol74
,
1206 MDTuple
*Elements
= MDTuple::getDistinct(Ctx
, std::nullopt
);
1208 DIB
.createImportedDeclaration(CU
, nullptr, F
, 1);
1209 DIB
.createImportedDeclaration(CU
, nullptr, F
, 1);
1210 DIB
.createImportedModule(CU
, (DIImportedEntity
*)nullptr, F
, 2);
1211 DIB
.createImportedModule(CU
, (DIImportedEntity
*)nullptr, F
, 2);
1212 DIB
.createImportedModule(CU
, (DIImportedEntity
*)nullptr, F
, 2, Elements
);
1213 DIB
.createImportedModule(CU
, (DIImportedEntity
*)nullptr, F
, 2, Elements
);
1215 EXPECT_TRUE(verifyModule(*M
));
1216 EXPECT_TRUE(CU
->getImportedEntities().size() == 3);
1219 // 0: #define M0 V0 <-- command line definition
1220 // 0: main.c <-- main file
1221 // 3: #define M1 V1 <-- M1 definition in main.c
1222 // 5: #include "file.h" <-- inclusion of file.h from main.c
1223 // 1: #define M2 <-- M2 definition in file.h with no value
1224 // 7: #undef M1 V1 <-- M1 un-definition in main.c
1225 TEST_F(IRBuilderTest
, DIBuilderMacro
) {
1226 IRBuilder
<> Builder(BB
);
1228 auto File1
= DIB
.createFile("main.c", "/");
1229 auto File2
= DIB
.createFile("file.h", "/");
1230 auto CU
= DIB
.createCompileUnit(
1231 dwarf::DW_LANG_C
, DIB
.createFile("main.c", "/"), "llvm-c", true, "", 0);
1233 DIB
.createMacro(nullptr, 0, dwarf::DW_MACINFO_define
, "M0", "V0");
1234 auto TMF1
= DIB
.createTempMacroFile(nullptr, 0, File1
);
1235 auto MDef1
= DIB
.createMacro(TMF1
, 3, dwarf::DW_MACINFO_define
, "M1", "V1");
1236 auto TMF2
= DIB
.createTempMacroFile(TMF1
, 5, File2
);
1237 auto MDef2
= DIB
.createMacro(TMF2
, 1, dwarf::DW_MACINFO_define
, "M2");
1238 auto MUndef1
= DIB
.createMacro(TMF1
, 7, dwarf::DW_MACINFO_undef
, "M1");
1240 EXPECT_EQ(dwarf::DW_MACINFO_define
, MDef1
->getMacinfoType());
1241 EXPECT_EQ(3u, MDef1
->getLine());
1242 EXPECT_EQ("M1", MDef1
->getName());
1243 EXPECT_EQ("V1", MDef1
->getValue());
1245 EXPECT_EQ(dwarf::DW_MACINFO_undef
, MUndef1
->getMacinfoType());
1246 EXPECT_EQ(7u, MUndef1
->getLine());
1247 EXPECT_EQ("M1", MUndef1
->getName());
1248 EXPECT_EQ("", MUndef1
->getValue());
1250 EXPECT_EQ(dwarf::DW_MACINFO_start_file
, TMF2
->getMacinfoType());
1251 EXPECT_EQ(5u, TMF2
->getLine());
1252 EXPECT_EQ(File2
, TMF2
->getFile());
1256 SmallVector
<Metadata
*, 4> Elements
;
1257 Elements
.push_back(MDef2
);
1258 auto MF2
= DIMacroFile::get(Ctx
, dwarf::DW_MACINFO_start_file
, 5, File2
,
1259 DIB
.getOrCreateMacroArray(Elements
));
1262 Elements
.push_back(MDef1
);
1263 Elements
.push_back(MF2
);
1264 Elements
.push_back(MUndef1
);
1265 auto MF1
= DIMacroFile::get(Ctx
, dwarf::DW_MACINFO_start_file
, 0, File1
,
1266 DIB
.getOrCreateMacroArray(Elements
));
1269 Elements
.push_back(MDef0
);
1270 Elements
.push_back(MF1
);
1271 auto MN0
= MDTuple::get(Ctx
, Elements
);
1272 EXPECT_EQ(MN0
, CU
->getRawMacros());
1275 Elements
.push_back(MDef1
);
1276 Elements
.push_back(MF2
);
1277 Elements
.push_back(MUndef1
);
1278 auto MN1
= MDTuple::get(Ctx
, Elements
);
1279 EXPECT_EQ(MN1
, MF1
->getRawElements());
1282 Elements
.push_back(MDef2
);
1283 auto MN2
= MDTuple::get(Ctx
, Elements
);
1284 EXPECT_EQ(MN2
, MF2
->getRawElements());
1285 EXPECT_TRUE(verifyModule(*M
));
1288 TEST_F(IRBuilderTest
, NoFolderNames
) {
1289 IRBuilder
<NoFolder
> Builder(BB
);
1291 Builder
.CreateAdd(Builder
.getInt32(1), Builder
.getInt32(2), "add");
1292 EXPECT_EQ(Add
->getName(), "add");
1295 TEST_F(IRBuilderTest
, CTAD
) {
1296 struct TestInserter
: public IRBuilderDefaultInserter
{
1297 TestInserter() = default;
1299 InstSimplifyFolder
Folder(M
->getDataLayout());
1301 IRBuilder
Builder1(Ctx
, Folder
, TestInserter());
1302 static_assert(std::is_same_v
<decltype(Builder1
),
1303 IRBuilder
<InstSimplifyFolder
, TestInserter
>>);
1304 IRBuilder
Builder2(Ctx
);
1305 static_assert(std::is_same_v
<decltype(Builder2
), IRBuilder
<>>);
1306 IRBuilder
Builder3(BB
, Folder
);
1308 std::is_same_v
<decltype(Builder3
), IRBuilder
<InstSimplifyFolder
>>);
1309 IRBuilder
Builder4(BB
);
1310 static_assert(std::is_same_v
<decltype(Builder4
), IRBuilder
<>>);
1311 // The block BB is empty, so don't test this one.
1312 // IRBuilder Builder5(BB->getTerminator());
1313 // static_assert(std::is_same_v<decltype(Builder5), IRBuilder<>>);
1314 IRBuilder
Builder6(BB
, BB
->end(), Folder
);
1316 std::is_same_v
<decltype(Builder6
), IRBuilder
<InstSimplifyFolder
>>);
1317 IRBuilder
Builder7(BB
, BB
->end());
1318 static_assert(std::is_same_v
<decltype(Builder7
), IRBuilder
<>>);