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 Call
= Builder
.CreateMinNum(V
, V
);
69 II
= cast
<IntrinsicInst
>(Call
);
70 EXPECT_EQ(II
->getIntrinsicID(), Intrinsic::minnum
);
72 Call
= Builder
.CreateMaxNum(V
, V
);
73 II
= cast
<IntrinsicInst
>(Call
);
74 EXPECT_EQ(II
->getIntrinsicID(), Intrinsic::maxnum
);
76 Call
= Builder
.CreateMinimum(V
, V
);
77 II
= cast
<IntrinsicInst
>(Call
);
78 EXPECT_EQ(II
->getIntrinsicID(), Intrinsic::minimum
);
80 Call
= Builder
.CreateMaximum(V
, V
);
81 II
= cast
<IntrinsicInst
>(Call
);
82 EXPECT_EQ(II
->getIntrinsicID(), Intrinsic::maximum
);
84 Call
= Builder
.CreateIntrinsic(Intrinsic::readcyclecounter
, {}, {});
85 II
= cast
<IntrinsicInst
>(Call
);
86 EXPECT_EQ(II
->getIntrinsicID(), Intrinsic::readcyclecounter
);
88 Call
= Builder
.CreateUnaryIntrinsic(Intrinsic::fabs
, V
);
89 II
= cast
<IntrinsicInst
>(Call
);
90 EXPECT_EQ(II
->getIntrinsicID(), Intrinsic::fabs
);
91 EXPECT_FALSE(II
->hasNoInfs());
92 EXPECT_FALSE(II
->hasNoNaNs());
94 Call
= Builder
.CreateUnaryIntrinsic(Intrinsic::fabs
, V
, I
);
95 II
= cast
<IntrinsicInst
>(Call
);
96 EXPECT_EQ(II
->getIntrinsicID(), Intrinsic::fabs
);
97 EXPECT_TRUE(II
->hasNoInfs());
98 EXPECT_FALSE(II
->hasNoNaNs());
100 Call
= Builder
.CreateBinaryIntrinsic(Intrinsic::pow
, V
, V
);
101 II
= cast
<IntrinsicInst
>(Call
);
102 EXPECT_EQ(II
->getIntrinsicID(), Intrinsic::pow
);
103 EXPECT_FALSE(II
->hasNoInfs());
104 EXPECT_FALSE(II
->hasNoNaNs());
106 Call
= Builder
.CreateBinaryIntrinsic(Intrinsic::pow
, V
, V
, I
);
107 II
= cast
<IntrinsicInst
>(Call
);
108 EXPECT_EQ(II
->getIntrinsicID(), Intrinsic::pow
);
109 EXPECT_TRUE(II
->hasNoInfs());
110 EXPECT_FALSE(II
->hasNoNaNs());
112 Call
= Builder
.CreateIntrinsic(Intrinsic::fma
, {V
->getType()}, {V
, V
, V
});
113 II
= cast
<IntrinsicInst
>(Call
);
114 EXPECT_EQ(II
->getIntrinsicID(), Intrinsic::fma
);
115 EXPECT_FALSE(II
->hasNoInfs());
116 EXPECT_FALSE(II
->hasNoNaNs());
118 Call
= Builder
.CreateIntrinsic(Intrinsic::fma
, {V
->getType()}, {V
, V
, V
}, I
);
119 II
= cast
<IntrinsicInst
>(Call
);
120 EXPECT_EQ(II
->getIntrinsicID(), Intrinsic::fma
);
121 EXPECT_TRUE(II
->hasNoInfs());
122 EXPECT_FALSE(II
->hasNoNaNs());
124 Call
= Builder
.CreateIntrinsic(Intrinsic::fma
, {V
->getType()}, {V
, V
, V
}, I
);
125 II
= cast
<IntrinsicInst
>(Call
);
126 EXPECT_EQ(II
->getIntrinsicID(), Intrinsic::fma
);
127 EXPECT_TRUE(II
->hasNoInfs());
128 EXPECT_FALSE(II
->hasNoNaNs());
130 Call
= Builder
.CreateUnaryIntrinsic(Intrinsic::roundeven
, V
);
131 II
= cast
<IntrinsicInst
>(Call
);
132 EXPECT_EQ(II
->getIntrinsicID(), Intrinsic::roundeven
);
133 EXPECT_FALSE(II
->hasNoInfs());
134 EXPECT_FALSE(II
->hasNoNaNs());
136 Call
= Builder
.CreateIntrinsic(
137 Intrinsic::set_rounding
, {},
138 {Builder
.getInt32(static_cast<uint32_t>(RoundingMode::TowardZero
))});
139 II
= cast
<IntrinsicInst
>(Call
);
140 EXPECT_EQ(II
->getIntrinsicID(), Intrinsic::set_rounding
);
143 TEST_F(IRBuilderTest
, IntrinsicMangling
) {
144 IRBuilder
<> Builder(BB
);
145 Type
*VoidTy
= Builder
.getVoidTy();
146 Type
*Int64Ty
= Builder
.getInt64Ty();
147 Value
*Int64Val
= Builder
.getInt64(0);
148 Value
*DoubleVal
= PoisonValue::get(Builder
.getDoubleTy());
151 // Mangled return type, no arguments.
152 Call
= Builder
.CreateIntrinsic(Int64Ty
, Intrinsic::coro_size
, {});
153 EXPECT_EQ(Call
->getCalledFunction()->getName(), "llvm.coro.size.i64");
155 // Void return type, mangled argument type.
157 Builder
.CreateIntrinsic(VoidTy
, Intrinsic::set_loop_iterations
, Int64Val
);
158 EXPECT_EQ(Call
->getCalledFunction()->getName(),
159 "llvm.set.loop.iterations.i64");
161 // Mangled return type and argument type.
162 Call
= Builder
.CreateIntrinsic(Int64Ty
, Intrinsic::lround
, DoubleVal
);
163 EXPECT_EQ(Call
->getCalledFunction()->getName(), "llvm.lround.i64.f64");
166 TEST_F(IRBuilderTest
, IntrinsicsWithScalableVectors
) {
167 IRBuilder
<> Builder(BB
);
171 // Test scalable flag isn't dropped for intrinsic that is explicitly defined
172 // with scalable vectors, e.g. LLVMType<nxv4i32>.
173 Type
*SrcVecTy
= VectorType::get(Builder
.getHalfTy(), 8, true);
174 Type
*DstVecTy
= VectorType::get(Builder
.getInt32Ty(), 4, true);
175 Type
*PredTy
= VectorType::get(Builder
.getInt1Ty(), 4, true);
177 SmallVector
<Value
*, 3> ArgTys
;
178 ArgTys
.push_back(UndefValue::get(DstVecTy
));
179 ArgTys
.push_back(UndefValue::get(PredTy
));
180 ArgTys
.push_back(UndefValue::get(SrcVecTy
));
182 Call
= Builder
.CreateIntrinsic(Intrinsic::aarch64_sve_fcvtzs_i32f16
, {},
183 ArgTys
, nullptr, "aarch64.sve.fcvtzs.i32f16");
184 FTy
= Call
->getFunctionType();
185 EXPECT_EQ(FTy
->getReturnType(), DstVecTy
);
186 for (unsigned i
= 0; i
!= ArgTys
.size(); ++i
)
187 EXPECT_EQ(FTy
->getParamType(i
), ArgTys
[i
]->getType());
189 // Test scalable flag isn't dropped for intrinsic defined with
190 // LLVMScalarOrSameVectorWidth.
192 Type
*VecTy
= VectorType::get(Builder
.getInt32Ty(), 4, true);
193 Type
*PtrToVecTy
= Builder
.getPtrTy();
194 PredTy
= VectorType::get(Builder
.getInt1Ty(), 4, true);
197 ArgTys
.push_back(UndefValue::get(PtrToVecTy
));
198 ArgTys
.push_back(UndefValue::get(Builder
.getInt32Ty()));
199 ArgTys
.push_back(UndefValue::get(PredTy
));
200 ArgTys
.push_back(UndefValue::get(VecTy
));
202 Call
= Builder
.CreateIntrinsic(Intrinsic::masked_load
,
203 {VecTy
, PtrToVecTy
}, ArgTys
,
204 nullptr, "masked.load");
205 FTy
= Call
->getFunctionType();
206 EXPECT_EQ(FTy
->getReturnType(), VecTy
);
207 for (unsigned i
= 0; i
!= ArgTys
.size(); ++i
)
208 EXPECT_EQ(FTy
->getParamType(i
), ArgTys
[i
]->getType());
211 TEST_F(IRBuilderTest
, CreateVScale
) {
212 IRBuilder
<> Builder(BB
);
214 Constant
*Zero
= Builder
.getInt32(0);
215 Value
*VScale
= Builder
.CreateVScale(Zero
);
216 EXPECT_TRUE(isa
<ConstantInt
>(VScale
) && cast
<ConstantInt
>(VScale
)->isZero());
219 TEST_F(IRBuilderTest
, CreateStepVector
) {
220 IRBuilder
<> Builder(BB
);
222 // Fixed width vectors
223 Type
*DstVecTy
= VectorType::get(Builder
.getInt32Ty(), 4, false);
224 Value
*StepVec
= Builder
.CreateStepVector(DstVecTy
);
225 EXPECT_TRUE(isa
<Constant
>(StepVec
));
226 EXPECT_EQ(StepVec
->getType(), DstVecTy
);
228 const auto *VectorValue
= cast
<Constant
>(StepVec
);
229 for (unsigned i
= 0; i
< 4; i
++) {
230 EXPECT_TRUE(isa
<ConstantInt
>(VectorValue
->getAggregateElement(i
)));
231 ConstantInt
*El
= cast
<ConstantInt
>(VectorValue
->getAggregateElement(i
));
232 EXPECT_EQ(El
->getValue(), i
);
236 DstVecTy
= VectorType::get(Builder
.getInt32Ty(), 4, true);
237 StepVec
= Builder
.CreateStepVector(DstVecTy
);
238 EXPECT_TRUE(isa
<CallInst
>(StepVec
));
239 CallInst
*Call
= cast
<CallInst
>(StepVec
);
240 FunctionType
*FTy
= Call
->getFunctionType();
241 EXPECT_EQ(FTy
->getReturnType(), DstVecTy
);
242 EXPECT_EQ(Call
->getIntrinsicID(), Intrinsic::experimental_stepvector
);
245 TEST_F(IRBuilderTest
, CreateStepVectorI3
) {
246 IRBuilder
<> Builder(BB
);
249 Type
*DstVecTy
= VectorType::get(IntegerType::get(Ctx
, 3), 2, true);
250 Type
*VecI8Ty
= VectorType::get(Builder
.getInt8Ty(), 2, true);
251 Value
*StepVec
= Builder
.CreateStepVector(DstVecTy
);
252 EXPECT_TRUE(isa
<TruncInst
>(StepVec
));
253 TruncInst
*Trunc
= cast
<TruncInst
>(StepVec
);
254 EXPECT_EQ(Trunc
->getDestTy(), DstVecTy
);
255 EXPECT_EQ(Trunc
->getSrcTy(), VecI8Ty
);
256 EXPECT_TRUE(isa
<CallInst
>(Trunc
->getOperand(0)));
258 CallInst
*Call
= cast
<CallInst
>(Trunc
->getOperand(0));
259 FunctionType
*FTy
= Call
->getFunctionType();
260 EXPECT_EQ(FTy
->getReturnType(), VecI8Ty
);
261 EXPECT_EQ(Call
->getIntrinsicID(), Intrinsic::experimental_stepvector
);
264 TEST_F(IRBuilderTest
, ConstrainedFP
) {
265 IRBuilder
<> Builder(BB
);
271 GlobalVariable
*GVDouble
= new GlobalVariable(*M
, Type::getDoubleTy(Ctx
),
272 true, GlobalValue::ExternalLinkage
, nullptr);
274 V
= Builder
.CreateLoad(GV
->getValueType(), GV
);
275 VDouble
= Builder
.CreateLoad(GVDouble
->getValueType(), GVDouble
);
277 // See if we get constrained intrinsics instead of non-constrained
279 Builder
.setIsFPConstrained(true);
280 auto Parent
= BB
->getParent();
281 Parent
->addFnAttr(Attribute::StrictFP
);
283 V
= Builder
.CreateFAdd(V
, V
);
284 ASSERT_TRUE(isa
<IntrinsicInst
>(V
));
285 II
= cast
<IntrinsicInst
>(V
);
286 EXPECT_EQ(II
->getIntrinsicID(), Intrinsic::experimental_constrained_fadd
);
288 V
= Builder
.CreateFSub(V
, V
);
289 ASSERT_TRUE(isa
<IntrinsicInst
>(V
));
290 II
= cast
<IntrinsicInst
>(V
);
291 EXPECT_EQ(II
->getIntrinsicID(), Intrinsic::experimental_constrained_fsub
);
293 V
= Builder
.CreateFMul(V
, V
);
294 ASSERT_TRUE(isa
<IntrinsicInst
>(V
));
295 II
= cast
<IntrinsicInst
>(V
);
296 EXPECT_EQ(II
->getIntrinsicID(), Intrinsic::experimental_constrained_fmul
);
298 V
= Builder
.CreateFDiv(V
, V
);
299 ASSERT_TRUE(isa
<IntrinsicInst
>(V
));
300 II
= cast
<IntrinsicInst
>(V
);
301 EXPECT_EQ(II
->getIntrinsicID(), Intrinsic::experimental_constrained_fdiv
);
303 V
= Builder
.CreateFRem(V
, V
);
304 ASSERT_TRUE(isa
<IntrinsicInst
>(V
));
305 II
= cast
<IntrinsicInst
>(V
);
306 EXPECT_EQ(II
->getIntrinsicID(), Intrinsic::experimental_constrained_frem
);
308 VInt
= Builder
.CreateFPToUI(VDouble
, Builder
.getInt32Ty());
309 ASSERT_TRUE(isa
<IntrinsicInst
>(VInt
));
310 II
= cast
<IntrinsicInst
>(VInt
);
311 EXPECT_EQ(II
->getIntrinsicID(), Intrinsic::experimental_constrained_fptoui
);
313 VInt
= Builder
.CreateFPToSI(VDouble
, Builder
.getInt32Ty());
314 ASSERT_TRUE(isa
<IntrinsicInst
>(VInt
));
315 II
= cast
<IntrinsicInst
>(VInt
);
316 EXPECT_EQ(II
->getIntrinsicID(), Intrinsic::experimental_constrained_fptosi
);
318 VDouble
= Builder
.CreateUIToFP(VInt
, Builder
.getDoubleTy());
319 ASSERT_TRUE(isa
<IntrinsicInst
>(VDouble
));
320 II
= cast
<IntrinsicInst
>(VDouble
);
321 EXPECT_EQ(II
->getIntrinsicID(), Intrinsic::experimental_constrained_uitofp
);
323 VDouble
= Builder
.CreateSIToFP(VInt
, Builder
.getDoubleTy());
324 ASSERT_TRUE(isa
<IntrinsicInst
>(VDouble
));
325 II
= cast
<IntrinsicInst
>(VDouble
);
326 EXPECT_EQ(II
->getIntrinsicID(), Intrinsic::experimental_constrained_sitofp
);
328 V
= Builder
.CreateFPTrunc(VDouble
, Type::getFloatTy(Ctx
));
329 ASSERT_TRUE(isa
<IntrinsicInst
>(V
));
330 II
= cast
<IntrinsicInst
>(V
);
331 EXPECT_EQ(II
->getIntrinsicID(), Intrinsic::experimental_constrained_fptrunc
);
333 VDouble
= Builder
.CreateFPExt(V
, Type::getDoubleTy(Ctx
));
334 ASSERT_TRUE(isa
<IntrinsicInst
>(VDouble
));
335 II
= cast
<IntrinsicInst
>(VDouble
);
336 EXPECT_EQ(II
->getIntrinsicID(), Intrinsic::experimental_constrained_fpext
);
338 // Verify attributes on the call are created automatically.
339 AttributeSet CallAttrs
= II
->getAttributes().getFnAttrs();
340 EXPECT_EQ(CallAttrs
.hasAttribute(Attribute::StrictFP
), true);
342 // Verify attributes on the containing function are created when requested.
343 Builder
.setConstrainedFPFunctionAttr();
344 AttributeList Attrs
= BB
->getParent()->getAttributes();
345 AttributeSet FnAttrs
= Attrs
.getFnAttrs();
346 EXPECT_EQ(FnAttrs
.hasAttribute(Attribute::StrictFP
), true);
348 // Verify the codepaths for setting and overriding the default metadata.
349 V
= Builder
.CreateFAdd(V
, V
);
350 ASSERT_TRUE(isa
<ConstrainedFPIntrinsic
>(V
));
351 auto *CII
= cast
<ConstrainedFPIntrinsic
>(V
);
352 EXPECT_EQ(fp::ebStrict
, CII
->getExceptionBehavior());
353 EXPECT_EQ(RoundingMode::Dynamic
, CII
->getRoundingMode());
355 Builder
.setDefaultConstrainedExcept(fp::ebIgnore
);
356 Builder
.setDefaultConstrainedRounding(RoundingMode::TowardPositive
);
357 V
= Builder
.CreateFAdd(V
, V
);
358 CII
= cast
<ConstrainedFPIntrinsic
>(V
);
359 EXPECT_EQ(fp::ebIgnore
, CII
->getExceptionBehavior());
360 EXPECT_EQ(CII
->getRoundingMode(), RoundingMode::TowardPositive
);
362 Builder
.setDefaultConstrainedExcept(fp::ebIgnore
);
363 Builder
.setDefaultConstrainedRounding(RoundingMode::NearestTiesToEven
);
364 V
= Builder
.CreateFAdd(V
, V
);
365 CII
= cast
<ConstrainedFPIntrinsic
>(V
);
366 EXPECT_EQ(fp::ebIgnore
, CII
->getExceptionBehavior());
367 EXPECT_EQ(RoundingMode::NearestTiesToEven
, CII
->getRoundingMode());
369 Builder
.setDefaultConstrainedExcept(fp::ebMayTrap
);
370 Builder
.setDefaultConstrainedRounding(RoundingMode::TowardNegative
);
371 V
= Builder
.CreateFAdd(V
, V
);
372 CII
= cast
<ConstrainedFPIntrinsic
>(V
);
373 EXPECT_EQ(fp::ebMayTrap
, CII
->getExceptionBehavior());
374 EXPECT_EQ(RoundingMode::TowardNegative
, CII
->getRoundingMode());
376 Builder
.setDefaultConstrainedExcept(fp::ebStrict
);
377 Builder
.setDefaultConstrainedRounding(RoundingMode::TowardZero
);
378 V
= Builder
.CreateFAdd(V
, V
);
379 CII
= cast
<ConstrainedFPIntrinsic
>(V
);
380 EXPECT_EQ(fp::ebStrict
, CII
->getExceptionBehavior());
381 EXPECT_EQ(RoundingMode::TowardZero
, CII
->getRoundingMode());
383 Builder
.setDefaultConstrainedExcept(fp::ebIgnore
);
384 Builder
.setDefaultConstrainedRounding(RoundingMode::Dynamic
);
385 V
= Builder
.CreateFAdd(V
, V
);
386 CII
= cast
<ConstrainedFPIntrinsic
>(V
);
387 EXPECT_EQ(fp::ebIgnore
, CII
->getExceptionBehavior());
388 EXPECT_EQ(RoundingMode::Dynamic
, CII
->getRoundingMode());
390 // Now override the defaults.
391 Call
= Builder
.CreateConstrainedFPBinOp(
392 Intrinsic::experimental_constrained_fadd
, V
, V
, nullptr, "", nullptr,
393 RoundingMode::TowardNegative
, fp::ebMayTrap
);
394 CII
= cast
<ConstrainedFPIntrinsic
>(Call
);
395 EXPECT_EQ(CII
->getIntrinsicID(), Intrinsic::experimental_constrained_fadd
);
396 EXPECT_EQ(fp::ebMayTrap
, CII
->getExceptionBehavior());
397 EXPECT_EQ(RoundingMode::TowardNegative
, CII
->getRoundingMode());
399 Builder
.CreateRetVoid();
400 EXPECT_FALSE(verifyModule(*M
));
403 TEST_F(IRBuilderTest
, ConstrainedFPIntrinsics
) {
404 IRBuilder
<> Builder(BB
);
407 ConstrainedFPIntrinsic
*CII
;
408 GlobalVariable
*GVDouble
= new GlobalVariable(
409 *M
, Type::getDoubleTy(Ctx
), true, GlobalValue::ExternalLinkage
, nullptr);
410 VDouble
= Builder
.CreateLoad(GVDouble
->getValueType(), GVDouble
);
412 Builder
.setDefaultConstrainedExcept(fp::ebStrict
);
413 Builder
.setDefaultConstrainedRounding(RoundingMode::TowardZero
);
414 Function
*Fn
= Intrinsic::getDeclaration(M
.get(),
415 Intrinsic::experimental_constrained_roundeven
, { Type::getDoubleTy(Ctx
) });
416 V
= Builder
.CreateConstrainedFPCall(Fn
, { VDouble
});
417 CII
= cast
<ConstrainedFPIntrinsic
>(V
);
418 EXPECT_EQ(Intrinsic::experimental_constrained_roundeven
, CII
->getIntrinsicID());
419 EXPECT_EQ(fp::ebStrict
, CII
->getExceptionBehavior());
422 TEST_F(IRBuilderTest
, ConstrainedFPFunctionCall
) {
423 IRBuilder
<> Builder(BB
);
425 // Create an empty constrained FP function.
426 FunctionType
*FTy
= FunctionType::get(Type::getVoidTy(Ctx
),
429 Function::Create(FTy
, Function::ExternalLinkage
, "", M
.get());
430 BasicBlock
*CalleeBB
= BasicBlock::Create(Ctx
, "", Callee
);
431 IRBuilder
<> CalleeBuilder(CalleeBB
);
432 CalleeBuilder
.setIsFPConstrained(true);
433 CalleeBuilder
.setConstrainedFPFunctionAttr();
434 CalleeBuilder
.CreateRetVoid();
436 // Now call the empty constrained FP function.
437 Builder
.setIsFPConstrained(true);
438 Builder
.setConstrainedFPFunctionAttr();
439 CallInst
*FCall
= Builder
.CreateCall(Callee
, std::nullopt
);
441 // Check the attributes to verify the strictfp attribute is on the call.
443 FCall
->getAttributes().getFnAttrs().hasAttribute(Attribute::StrictFP
));
445 Builder
.CreateRetVoid();
446 EXPECT_FALSE(verifyModule(*M
));
449 TEST_F(IRBuilderTest
, Lifetime
) {
450 IRBuilder
<> Builder(BB
);
451 AllocaInst
*Var1
= Builder
.CreateAlloca(Builder
.getInt8Ty());
452 AllocaInst
*Var2
= Builder
.CreateAlloca(Builder
.getInt32Ty());
453 AllocaInst
*Var3
= Builder
.CreateAlloca(Builder
.getInt8Ty(),
454 Builder
.getInt32(123));
456 CallInst
*Start1
= Builder
.CreateLifetimeStart(Var1
);
457 CallInst
*Start2
= Builder
.CreateLifetimeStart(Var2
);
458 CallInst
*Start3
= Builder
.CreateLifetimeStart(Var3
, Builder
.getInt64(100));
460 EXPECT_EQ(Start1
->getArgOperand(0), Builder
.getInt64(-1));
461 EXPECT_EQ(Start2
->getArgOperand(0), Builder
.getInt64(-1));
462 EXPECT_EQ(Start3
->getArgOperand(0), Builder
.getInt64(100));
464 EXPECT_EQ(Start1
->getArgOperand(1), Var1
);
465 EXPECT_EQ(Start2
->getArgOperand(1)->stripPointerCasts(), Var2
);
466 EXPECT_EQ(Start3
->getArgOperand(1), Var3
);
468 Value
*End1
= Builder
.CreateLifetimeEnd(Var1
);
469 Builder
.CreateLifetimeEnd(Var2
);
470 Builder
.CreateLifetimeEnd(Var3
);
472 IntrinsicInst
*II_Start1
= dyn_cast
<IntrinsicInst
>(Start1
);
473 IntrinsicInst
*II_End1
= dyn_cast
<IntrinsicInst
>(End1
);
474 ASSERT_TRUE(II_Start1
!= nullptr);
475 EXPECT_EQ(II_Start1
->getIntrinsicID(), Intrinsic::lifetime_start
);
476 ASSERT_TRUE(II_End1
!= nullptr);
477 EXPECT_EQ(II_End1
->getIntrinsicID(), Intrinsic::lifetime_end
);
480 TEST_F(IRBuilderTest
, CreateCondBr
) {
481 IRBuilder
<> Builder(BB
);
482 BasicBlock
*TBB
= BasicBlock::Create(Ctx
, "", F
);
483 BasicBlock
*FBB
= BasicBlock::Create(Ctx
, "", F
);
485 BranchInst
*BI
= Builder
.CreateCondBr(Builder
.getTrue(), TBB
, FBB
);
486 Instruction
*TI
= BB
->getTerminator();
488 EXPECT_EQ(2u, TI
->getNumSuccessors());
489 EXPECT_EQ(TBB
, TI
->getSuccessor(0));
490 EXPECT_EQ(FBB
, TI
->getSuccessor(1));
492 BI
->eraseFromParent();
493 MDNode
*Weights
= MDBuilder(Ctx
).createBranchWeights(42, 13);
494 BI
= Builder
.CreateCondBr(Builder
.getTrue(), TBB
, FBB
, Weights
);
495 TI
= BB
->getTerminator();
497 EXPECT_EQ(2u, TI
->getNumSuccessors());
498 EXPECT_EQ(TBB
, TI
->getSuccessor(0));
499 EXPECT_EQ(FBB
, TI
->getSuccessor(1));
500 EXPECT_EQ(Weights
, TI
->getMetadata(LLVMContext::MD_prof
));
503 TEST_F(IRBuilderTest
, LandingPadName
) {
504 IRBuilder
<> Builder(BB
);
505 LandingPadInst
*LP
= Builder
.CreateLandingPad(Builder
.getInt32Ty(), 0, "LP");
506 EXPECT_EQ(LP
->getName(), "LP");
509 TEST_F(IRBuilderTest
, DataLayout
) {
510 std::unique_ptr
<Module
> M(new Module("test", Ctx
));
511 M
->setDataLayout("e-n32");
512 EXPECT_TRUE(M
->getDataLayout().isLegalInteger(32));
513 M
->setDataLayout("e");
514 EXPECT_FALSE(M
->getDataLayout().isLegalInteger(32));
517 TEST_F(IRBuilderTest
, GetIntTy
) {
518 IRBuilder
<> Builder(BB
);
519 IntegerType
*Ty1
= Builder
.getInt1Ty();
520 EXPECT_EQ(Ty1
, IntegerType::get(Ctx
, 1));
522 DataLayout
* DL
= new DataLayout(M
.get());
523 IntegerType
*IntPtrTy
= Builder
.getIntPtrTy(*DL
);
524 unsigned IntPtrBitSize
= DL
->getPointerSizeInBits(0);
525 EXPECT_EQ(IntPtrTy
, IntegerType::get(Ctx
, IntPtrBitSize
));
529 TEST_F(IRBuilderTest
, UnaryOperators
) {
530 IRBuilder
<NoFolder
> Builder(BB
);
531 Value
*V
= Builder
.CreateLoad(GV
->getValueType(), GV
);
533 // Test CreateUnOp(X)
534 Value
*U
= Builder
.CreateUnOp(Instruction::FNeg
, V
);
535 ASSERT_TRUE(isa
<Instruction
>(U
));
536 ASSERT_TRUE(isa
<FPMathOperator
>(U
));
537 ASSERT_TRUE(isa
<UnaryOperator
>(U
));
538 ASSERT_FALSE(isa
<BinaryOperator
>(U
));
540 // Test CreateFNegFMF(X)
541 Instruction
*I
= cast
<Instruction
>(U
);
542 I
->setHasNoSignedZeros(true);
543 I
->setHasNoNaNs(true);
544 Value
*VFMF
= Builder
.CreateFNegFMF(V
, I
);
545 Instruction
*IFMF
= cast
<Instruction
>(VFMF
);
546 EXPECT_TRUE(IFMF
->hasNoSignedZeros());
547 EXPECT_TRUE(IFMF
->hasNoNaNs());
548 EXPECT_FALSE(IFMF
->hasAllowReassoc());
551 TEST_F(IRBuilderTest
, FastMathFlags
) {
552 IRBuilder
<> Builder(BB
);
554 Instruction
*FDiv
, *FAdd
, *FCmp
, *FCall
, *FNeg
, *FSub
, *FMul
, *FRem
;
556 F
= Builder
.CreateLoad(GV
->getValueType(), GV
);
557 F
= Builder
.CreateFAdd(F
, F
);
559 EXPECT_FALSE(Builder
.getFastMathFlags().any());
560 ASSERT_TRUE(isa
<Instruction
>(F
));
561 FAdd
= cast
<Instruction
>(F
);
562 EXPECT_FALSE(FAdd
->hasNoNaNs());
565 Builder
.setFastMathFlags(FMF
);
567 // By default, no flags are set.
568 F
= Builder
.CreateFAdd(F
, F
);
569 EXPECT_FALSE(Builder
.getFastMathFlags().any());
570 ASSERT_TRUE(isa
<Instruction
>(F
));
571 FAdd
= cast
<Instruction
>(F
);
572 EXPECT_FALSE(FAdd
->hasNoNaNs());
573 EXPECT_FALSE(FAdd
->hasNoInfs());
574 EXPECT_FALSE(FAdd
->hasNoSignedZeros());
575 EXPECT_FALSE(FAdd
->hasAllowReciprocal());
576 EXPECT_FALSE(FAdd
->hasAllowContract());
577 EXPECT_FALSE(FAdd
->hasAllowReassoc());
578 EXPECT_FALSE(FAdd
->hasApproxFunc());
580 // Set all flags in the instruction.
582 EXPECT_TRUE(FAdd
->hasNoNaNs());
583 EXPECT_TRUE(FAdd
->hasNoInfs());
584 EXPECT_TRUE(FAdd
->hasNoSignedZeros());
585 EXPECT_TRUE(FAdd
->hasAllowReciprocal());
586 EXPECT_TRUE(FAdd
->hasAllowContract());
587 EXPECT_TRUE(FAdd
->hasAllowReassoc());
588 EXPECT_TRUE(FAdd
->hasApproxFunc());
590 // All flags are set in the builder.
592 Builder
.setFastMathFlags(FMF
);
594 F
= Builder
.CreateFAdd(F
, F
);
595 EXPECT_TRUE(Builder
.getFastMathFlags().any());
596 EXPECT_TRUE(Builder
.getFastMathFlags().all());
597 ASSERT_TRUE(isa
<Instruction
>(F
));
598 FAdd
= cast
<Instruction
>(F
);
599 EXPECT_TRUE(FAdd
->hasNoNaNs());
600 EXPECT_TRUE(FAdd
->isFast());
602 // Now, try it with CreateBinOp
603 F
= Builder
.CreateBinOp(Instruction::FAdd
, F
, F
);
604 EXPECT_TRUE(Builder
.getFastMathFlags().any());
605 ASSERT_TRUE(isa
<Instruction
>(F
));
606 FAdd
= cast
<Instruction
>(F
);
607 EXPECT_TRUE(FAdd
->hasNoNaNs());
608 EXPECT_TRUE(FAdd
->isFast());
610 F
= Builder
.CreateFDiv(F
, F
);
611 EXPECT_TRUE(Builder
.getFastMathFlags().all());
612 ASSERT_TRUE(isa
<Instruction
>(F
));
613 FDiv
= cast
<Instruction
>(F
);
614 EXPECT_TRUE(FDiv
->hasAllowReciprocal());
616 // Clear all FMF in the builder.
617 Builder
.clearFastMathFlags();
619 F
= Builder
.CreateFDiv(F
, F
);
620 ASSERT_TRUE(isa
<Instruction
>(F
));
621 FDiv
= cast
<Instruction
>(F
);
622 EXPECT_FALSE(FDiv
->hasAllowReciprocal());
624 // Try individual flags.
626 FMF
.setAllowReciprocal();
627 Builder
.setFastMathFlags(FMF
);
629 F
= Builder
.CreateFDiv(F
, F
);
630 EXPECT_TRUE(Builder
.getFastMathFlags().any());
631 EXPECT_TRUE(Builder
.getFastMathFlags().AllowReciprocal
);
632 ASSERT_TRUE(isa
<Instruction
>(F
));
633 FDiv
= cast
<Instruction
>(F
);
634 EXPECT_TRUE(FDiv
->hasAllowReciprocal());
636 Builder
.clearFastMathFlags();
638 FC
= Builder
.CreateFCmpOEQ(F
, F
);
639 ASSERT_TRUE(isa
<Instruction
>(FC
));
640 FCmp
= cast
<Instruction
>(FC
);
641 EXPECT_FALSE(FCmp
->hasAllowReciprocal());
644 FMF
.setAllowReciprocal();
645 Builder
.setFastMathFlags(FMF
);
647 FC
= Builder
.CreateFCmpOEQ(F
, F
);
648 EXPECT_TRUE(Builder
.getFastMathFlags().any());
649 EXPECT_TRUE(Builder
.getFastMathFlags().AllowReciprocal
);
650 ASSERT_TRUE(isa
<Instruction
>(FC
));
651 FCmp
= cast
<Instruction
>(FC
);
652 EXPECT_TRUE(FCmp
->hasAllowReciprocal());
654 Builder
.clearFastMathFlags();
657 FC
= Builder
.CreateFAdd(F
, F
);
658 ASSERT_TRUE(isa
<Instruction
>(FC
));
659 FAdd
= cast
<Instruction
>(FC
);
660 EXPECT_FALSE(FAdd
->hasAllowContract());
663 FMF
.setAllowContract(true);
664 Builder
.setFastMathFlags(FMF
);
666 FC
= Builder
.CreateFAdd(F
, F
);
667 EXPECT_TRUE(Builder
.getFastMathFlags().any());
668 EXPECT_TRUE(Builder
.getFastMathFlags().AllowContract
);
669 ASSERT_TRUE(isa
<Instruction
>(FC
));
670 FAdd
= cast
<Instruction
>(FC
);
671 EXPECT_TRUE(FAdd
->hasAllowContract());
674 Builder
.clearFastMathFlags();
675 Builder
.setFastMathFlags(FMF
);
676 // Now 'aml' and 'contract' are set.
677 F
= Builder
.CreateFMul(F
, F
);
678 FAdd
= cast
<Instruction
>(F
);
679 EXPECT_TRUE(FAdd
->hasApproxFunc());
680 EXPECT_TRUE(FAdd
->hasAllowContract());
681 EXPECT_FALSE(FAdd
->hasAllowReassoc());
683 FMF
.setAllowReassoc();
684 Builder
.clearFastMathFlags();
685 Builder
.setFastMathFlags(FMF
);
686 // Now 'aml' and 'contract' and 'reassoc' are set.
687 F
= Builder
.CreateFMul(F
, F
);
688 FAdd
= cast
<Instruction
>(F
);
689 EXPECT_TRUE(FAdd
->hasApproxFunc());
690 EXPECT_TRUE(FAdd
->hasAllowContract());
691 EXPECT_TRUE(FAdd
->hasAllowReassoc());
693 // Test a call with FMF.
694 auto CalleeTy
= FunctionType::get(Type::getFloatTy(Ctx
),
697 Function::Create(CalleeTy
, Function::ExternalLinkage
, "", M
.get());
699 FCall
= Builder
.CreateCall(Callee
, std::nullopt
);
700 EXPECT_FALSE(FCall
->hasNoNaNs());
703 Function::Create(CalleeTy
, Function::ExternalLinkage
, "", M
.get());
704 FCall
= Builder
.CreateCall(V
, std::nullopt
);
705 EXPECT_FALSE(FCall
->hasNoNaNs());
709 Builder
.setFastMathFlags(FMF
);
711 FCall
= Builder
.CreateCall(Callee
, std::nullopt
);
712 EXPECT_TRUE(Builder
.getFastMathFlags().any());
713 EXPECT_TRUE(Builder
.getFastMathFlags().NoNaNs
);
714 EXPECT_TRUE(FCall
->hasNoNaNs());
716 FCall
= Builder
.CreateCall(V
, std::nullopt
);
717 EXPECT_TRUE(Builder
.getFastMathFlags().any());
718 EXPECT_TRUE(Builder
.getFastMathFlags().NoNaNs
);
719 EXPECT_TRUE(FCall
->hasNoNaNs());
721 Builder
.clearFastMathFlags();
723 // To test a copy, make sure that a '0' and a '1' change state.
724 F
= Builder
.CreateFDiv(F
, F
);
725 ASSERT_TRUE(isa
<Instruction
>(F
));
726 FDiv
= cast
<Instruction
>(F
);
727 EXPECT_FALSE(FDiv
->getFastMathFlags().any());
728 FDiv
->setHasAllowReciprocal(true);
729 FAdd
->setHasAllowReciprocal(false);
730 FAdd
->setHasNoNaNs(true);
731 FDiv
->copyFastMathFlags(FAdd
);
732 EXPECT_TRUE(FDiv
->hasNoNaNs());
733 EXPECT_FALSE(FDiv
->hasAllowReciprocal());
735 // Test that CreateF*FMF functions copy flags from the source instruction
736 // instead of using the builder default.
737 Instruction
*const FMFSource
= FAdd
;
738 EXPECT_FALSE(Builder
.getFastMathFlags().noNaNs());
739 EXPECT_TRUE(FMFSource
->hasNoNaNs());
741 F
= Builder
.CreateFNegFMF(F
, FMFSource
);
742 ASSERT_TRUE(isa
<Instruction
>(F
));
743 FNeg
= cast
<Instruction
>(F
);
744 EXPECT_TRUE(FNeg
->hasNoNaNs());
745 F
= Builder
.CreateFAddFMF(F
, F
, FMFSource
);
746 ASSERT_TRUE(isa
<Instruction
>(F
));
747 FAdd
= cast
<Instruction
>(F
);
748 EXPECT_TRUE(FAdd
->hasNoNaNs());
749 F
= Builder
.CreateFSubFMF(F
, F
, FMFSource
);
750 ASSERT_TRUE(isa
<Instruction
>(F
));
751 FSub
= cast
<Instruction
>(F
);
752 EXPECT_TRUE(FSub
->hasNoNaNs());
753 F
= Builder
.CreateFMulFMF(F
, F
, FMFSource
);
754 ASSERT_TRUE(isa
<Instruction
>(F
));
755 FMul
= cast
<Instruction
>(F
);
756 EXPECT_TRUE(FMul
->hasNoNaNs());
757 F
= Builder
.CreateFDivFMF(F
, F
, FMFSource
);
758 ASSERT_TRUE(isa
<Instruction
>(F
));
759 FDiv
= cast
<Instruction
>(F
);
760 EXPECT_TRUE(FDiv
->hasNoNaNs());
761 F
= Builder
.CreateFRemFMF(F
, F
, FMFSource
);
762 ASSERT_TRUE(isa
<Instruction
>(F
));
763 FRem
= cast
<Instruction
>(F
);
764 EXPECT_TRUE(FRem
->hasNoNaNs());
767 TEST_F(IRBuilderTest
, WrapFlags
) {
768 IRBuilder
<NoFolder
> Builder(BB
);
770 // Test instructions.
771 GlobalVariable
*G
= new GlobalVariable(*M
, Builder
.getInt32Ty(), true,
772 GlobalValue::ExternalLinkage
, nullptr);
773 Value
*V
= Builder
.CreateLoad(G
->getValueType(), G
);
775 cast
<BinaryOperator
>(Builder
.CreateNSWAdd(V
, V
))->hasNoSignedWrap());
777 cast
<BinaryOperator
>(Builder
.CreateNSWMul(V
, V
))->hasNoSignedWrap());
779 cast
<BinaryOperator
>(Builder
.CreateNSWSub(V
, V
))->hasNoSignedWrap());
780 EXPECT_TRUE(cast
<BinaryOperator
>(
781 Builder
.CreateShl(V
, V
, "", /* NUW */ false, /* NSW */ true))
782 ->hasNoSignedWrap());
785 cast
<BinaryOperator
>(Builder
.CreateNUWAdd(V
, V
))->hasNoUnsignedWrap());
787 cast
<BinaryOperator
>(Builder
.CreateNUWMul(V
, V
))->hasNoUnsignedWrap());
789 cast
<BinaryOperator
>(Builder
.CreateNUWSub(V
, V
))->hasNoUnsignedWrap());
790 EXPECT_TRUE(cast
<BinaryOperator
>(
791 Builder
.CreateShl(V
, V
, "", /* NUW */ true, /* NSW */ false))
792 ->hasNoUnsignedWrap());
794 // Test operators created with constants.
795 Constant
*C
= Builder
.getInt32(42);
796 EXPECT_TRUE(cast
<OverflowingBinaryOperator
>(Builder
.CreateNSWAdd(C
, C
))
797 ->hasNoSignedWrap());
798 EXPECT_TRUE(cast
<OverflowingBinaryOperator
>(Builder
.CreateNSWSub(C
, C
))
799 ->hasNoSignedWrap());
800 EXPECT_TRUE(cast
<OverflowingBinaryOperator
>(Builder
.CreateNSWMul(C
, C
))
801 ->hasNoSignedWrap());
802 EXPECT_TRUE(cast
<OverflowingBinaryOperator
>(
803 Builder
.CreateShl(C
, C
, "", /* NUW */ false, /* NSW */ true))
804 ->hasNoSignedWrap());
806 EXPECT_TRUE(cast
<OverflowingBinaryOperator
>(Builder
.CreateNUWAdd(C
, C
))
807 ->hasNoUnsignedWrap());
808 EXPECT_TRUE(cast
<OverflowingBinaryOperator
>(Builder
.CreateNUWSub(C
, C
))
809 ->hasNoUnsignedWrap());
810 EXPECT_TRUE(cast
<OverflowingBinaryOperator
>(Builder
.CreateNUWMul(C
, C
))
811 ->hasNoUnsignedWrap());
812 EXPECT_TRUE(cast
<OverflowingBinaryOperator
>(
813 Builder
.CreateShl(C
, C
, "", /* NUW */ true, /* NSW */ false))
814 ->hasNoUnsignedWrap());
817 TEST_F(IRBuilderTest
, RAIIHelpersTest
) {
818 IRBuilder
<> Builder(BB
);
819 EXPECT_FALSE(Builder
.getFastMathFlags().allowReciprocal());
820 MDBuilder
MDB(M
->getContext());
822 MDNode
*FPMathA
= MDB
.createFPMath(0.01f
);
823 MDNode
*FPMathB
= MDB
.createFPMath(0.1f
);
825 Builder
.setDefaultFPMathTag(FPMathA
);
828 IRBuilder
<>::FastMathFlagGuard
Guard(Builder
);
830 FMF
.setAllowReciprocal();
831 Builder
.setFastMathFlags(FMF
);
832 Builder
.setDefaultFPMathTag(FPMathB
);
833 EXPECT_TRUE(Builder
.getFastMathFlags().allowReciprocal());
834 EXPECT_EQ(FPMathB
, Builder
.getDefaultFPMathTag());
837 EXPECT_FALSE(Builder
.getFastMathFlags().allowReciprocal());
838 EXPECT_EQ(FPMathA
, Builder
.getDefaultFPMathTag());
840 Value
*F
= Builder
.CreateLoad(GV
->getValueType(), GV
);
843 IRBuilder
<>::InsertPointGuard
Guard(Builder
);
844 Builder
.SetInsertPoint(cast
<Instruction
>(F
));
845 EXPECT_EQ(F
, &*Builder
.GetInsertPoint());
848 EXPECT_EQ(BB
->end(), Builder
.GetInsertPoint());
849 EXPECT_EQ(BB
, Builder
.GetInsertBlock());
852 TEST_F(IRBuilderTest
, createFunction
) {
853 IRBuilder
<> Builder(BB
);
855 auto File
= DIB
.createFile("error.swift", "/");
857 DIB
.createCompileUnit(dwarf::DW_LANG_Swift
, File
, "swiftc", true, "", 0);
858 auto Type
= DIB
.createSubroutineType(DIB
.getOrCreateTypeArray(std::nullopt
));
859 auto NoErr
= DIB
.createFunction(
860 CU
, "noerr", "", File
, 1, Type
, 1, DINode::FlagZero
,
861 DISubprogram::SPFlagDefinition
| DISubprogram::SPFlagOptimized
);
862 EXPECT_TRUE(!NoErr
->getThrownTypes());
863 auto Int
= DIB
.createBasicType("Int", 64, dwarf::DW_ATE_signed
);
864 auto Error
= DIB
.getOrCreateArray({Int
});
865 auto Err
= DIB
.createFunction(
866 CU
, "err", "", File
, 1, Type
, 1, DINode::FlagZero
,
867 DISubprogram::SPFlagDefinition
| DISubprogram::SPFlagOptimized
, nullptr,
868 nullptr, Error
.get());
869 EXPECT_TRUE(Err
->getThrownTypes().get() == Error
.get());
873 TEST_F(IRBuilderTest
, DIBuilder
) {
874 IRBuilder
<> Builder(BB
);
876 auto File
= DIB
.createFile("F.CBL", "/");
877 auto CU
= DIB
.createCompileUnit(dwarf::DW_LANG_Cobol74
,
878 DIB
.createFile("F.CBL", "/"), "llvm-cobol74",
880 auto Type
= DIB
.createSubroutineType(DIB
.getOrCreateTypeArray(std::nullopt
));
881 auto SP
= DIB
.createFunction(
882 CU
, "foo", "", File
, 1, Type
, 1, DINode::FlagZero
,
883 DISubprogram::SPFlagDefinition
| DISubprogram::SPFlagOptimized
);
884 F
->setSubprogram(SP
);
885 AllocaInst
*I
= Builder
.CreateAlloca(Builder
.getInt8Ty());
886 auto BarSP
= DIB
.createFunction(
887 CU
, "bar", "", File
, 1, Type
, 1, DINode::FlagZero
,
888 DISubprogram::SPFlagDefinition
| DISubprogram::SPFlagOptimized
);
889 auto BadScope
= DIB
.createLexicalBlockFile(BarSP
, File
, 0);
890 I
->setDebugLoc(DILocation::get(Ctx
, 2, 0, BadScope
));
892 EXPECT_TRUE(verifyModule(*M
));
895 TEST_F(IRBuilderTest
, createArtificialSubprogram
) {
896 IRBuilder
<> Builder(BB
);
898 auto File
= DIB
.createFile("main.c", "/");
899 auto CU
= DIB
.createCompileUnit(dwarf::DW_LANG_C
, File
, "clang",
900 /*isOptimized=*/true, /*Flags=*/"",
901 /*Runtime Version=*/0);
902 auto Type
= DIB
.createSubroutineType(DIB
.getOrCreateTypeArray(std::nullopt
));
903 auto SP
= DIB
.createFunction(
904 CU
, "foo", /*LinkageName=*/"", File
,
905 /*LineNo=*/1, Type
, /*ScopeLine=*/2, DINode::FlagZero
,
906 DISubprogram::SPFlagDefinition
| DISubprogram::SPFlagOptimized
);
907 EXPECT_TRUE(SP
->isDistinct());
909 F
->setSubprogram(SP
);
910 AllocaInst
*I
= Builder
.CreateAlloca(Builder
.getInt8Ty());
911 ReturnInst
*R
= Builder
.CreateRetVoid();
912 I
->setDebugLoc(DILocation::get(Ctx
, 3, 2, SP
));
913 R
->setDebugLoc(DILocation::get(Ctx
, 4, 2, SP
));
915 EXPECT_FALSE(verifyModule(*M
));
917 Function
*G
= Function::Create(F
->getFunctionType(),
918 Function::ExternalLinkage
, "", M
.get());
919 BasicBlock
*GBB
= BasicBlock::Create(Ctx
, "", G
);
920 Builder
.SetInsertPoint(GBB
);
921 I
->removeFromParent();
923 Builder
.CreateRetVoid();
924 EXPECT_FALSE(verifyModule(*M
));
926 DISubprogram
*GSP
= DIBuilder::createArtificialSubprogram(F
->getSubprogram());
927 EXPECT_EQ(SP
->getFile(), GSP
->getFile());
928 EXPECT_EQ(SP
->getType(), GSP
->getType());
929 EXPECT_EQ(SP
->getLine(), GSP
->getLine());
930 EXPECT_EQ(SP
->getScopeLine(), GSP
->getScopeLine());
931 EXPECT_TRUE(GSP
->isDistinct());
933 G
->setSubprogram(GSP
);
934 EXPECT_TRUE(verifyModule(*M
));
936 auto *InlinedAtNode
=
937 DILocation::getDistinct(Ctx
, GSP
->getScopeLine(), 0, GSP
);
938 DebugLoc DL
= I
->getDebugLoc();
939 DenseMap
<const MDNode
*, MDNode
*> IANodes
;
940 auto IA
= DebugLoc::appendInlinedAt(DL
, InlinedAtNode
, Ctx
, IANodes
);
942 DILocation::get(Ctx
, DL
.getLine(), DL
.getCol(), DL
.getScope(), IA
);
943 I
->setDebugLoc(NewDL
);
944 EXPECT_FALSE(verifyModule(*M
));
946 EXPECT_EQ("foo", SP
->getName());
947 EXPECT_EQ("foo", GSP
->getName());
948 EXPECT_FALSE(SP
->isArtificial());
949 EXPECT_TRUE(GSP
->isArtificial());
952 // Check that we can add debug info to an existing DICompileUnit.
953 TEST_F(IRBuilderTest
, appendDebugInfo
) {
954 IRBuilder
<> Builder(BB
);
955 Builder
.CreateRetVoid();
956 EXPECT_FALSE(verifyModule(*M
));
958 auto GetNames
= [](DICompileUnit
*CU
) {
959 SmallVector
<StringRef
> Names
;
960 for (auto *ET
: CU
->getEnumTypes())
961 Names
.push_back(ET
->getName());
962 for (auto *RT
: CU
->getRetainedTypes())
963 Names
.push_back(RT
->getName());
964 for (auto *GV
: CU
->getGlobalVariables())
965 Names
.push_back(GV
->getVariable()->getName());
966 for (auto *IE
: CU
->getImportedEntities())
967 Names
.push_back(IE
->getName());
968 for (auto *Node
: CU
->getMacros())
969 if (auto *MN
= dyn_cast_or_null
<DIMacro
>(Node
))
970 Names
.push_back(MN
->getName());
977 auto *File
= DIB
.createFile("main.c", "/");
978 CU
= DIB
.createCompileUnit(dwarf::DW_LANG_C
, File
, "clang",
979 /*isOptimized=*/true, /*Flags=*/"",
980 /*Runtime Version=*/0);
981 auto *ByteTy
= DIB
.createBasicType("byte0", 8, dwarf::DW_ATE_signed
);
982 DIB
.createEnumerationType(CU
, "ET0", File
, /*LineNo=*/0, /*SizeInBits=*/8,
983 /*AlignInBits=*/8, /*Elements=*/{}, ByteTy
);
984 DIB
.retainType(ByteTy
);
985 DIB
.createGlobalVariableExpression(CU
, "GV0", /*LinkageName=*/"", File
,
986 /*LineNo=*/1, ByteTy
,
987 /*IsLocalToUnit=*/true);
988 DIB
.createImportedDeclaration(CU
, nullptr, File
, /*LineNo=*/2, "IM0");
989 DIB
.createMacro(nullptr, /*LineNo=*/0, dwarf::DW_MACINFO_define
, "M0");
992 EXPECT_FALSE(verifyModule(*M
));
993 EXPECT_THAT(GetNames(CU
),
994 UnorderedElementsAre("ET0", "byte0", "GV0", "IM0", "M0"));
997 DIBuilder
DIB(*M
, true, CU
);
998 auto *File
= CU
->getFile();
999 auto *ByteTy
= DIB
.createBasicType("byte1", 8, dwarf::DW_ATE_signed
);
1000 DIB
.createEnumerationType(CU
, "ET1", File
, /*LineNo=*/0,
1001 /*SizeInBits=*/8, /*AlignInBits=*/8,
1002 /*Elements=*/{}, ByteTy
);
1003 DIB
.retainType(ByteTy
);
1004 DIB
.createGlobalVariableExpression(CU
, "GV1", /*LinkageName=*/"", File
,
1005 /*LineNo=*/1, ByteTy
,
1006 /*IsLocalToUnit=*/true);
1007 DIB
.createImportedDeclaration(CU
, nullptr, File
, /*LineNo=*/2, "IM1");
1008 DIB
.createMacro(nullptr, /*LineNo=*/0, dwarf::DW_MACINFO_define
, "M1");
1011 EXPECT_FALSE(verifyModule(*M
));
1012 EXPECT_THAT(GetNames(CU
),
1013 UnorderedElementsAre("ET0", "byte0", "GV0", "IM0", "M0", "ET1",
1014 "byte1", "GV1", "IM1", "M1"));
1017 TEST_F(IRBuilderTest
, InsertExtractElement
) {
1018 IRBuilder
<> Builder(BB
);
1020 auto VecTy
= FixedVectorType::get(Builder
.getInt64Ty(), 4);
1021 auto Elt1
= Builder
.getInt64(-1);
1022 auto Elt2
= Builder
.getInt64(-2);
1023 Value
*Vec
= Builder
.CreateInsertElement(VecTy
, Elt1
, Builder
.getInt8(1));
1024 Vec
= Builder
.CreateInsertElement(Vec
, Elt2
, 2);
1025 auto X1
= Builder
.CreateExtractElement(Vec
, 1);
1026 auto X2
= Builder
.CreateExtractElement(Vec
, Builder
.getInt32(2));
1027 EXPECT_EQ(Elt1
, X1
);
1028 EXPECT_EQ(Elt2
, X2
);
1031 TEST_F(IRBuilderTest
, CreateGlobalStringPtr
) {
1032 IRBuilder
<> Builder(BB
);
1034 auto String1a
= Builder
.CreateGlobalStringPtr("TestString", "String1a");
1035 auto String1b
= Builder
.CreateGlobalStringPtr("TestString", "String1b", 0);
1036 auto String2
= Builder
.CreateGlobalStringPtr("TestString", "String2", 1);
1037 auto String3
= Builder
.CreateGlobalString("TestString", "String3", 2);
1039 EXPECT_TRUE(String1a
->getType()->getPointerAddressSpace() == 0);
1040 EXPECT_TRUE(String1b
->getType()->getPointerAddressSpace() == 0);
1041 EXPECT_TRUE(String2
->getType()->getPointerAddressSpace() == 1);
1042 EXPECT_TRUE(String3
->getType()->getPointerAddressSpace() == 2);
1045 TEST_F(IRBuilderTest
, CreateThreadLocalAddress
) {
1046 IRBuilder
<> Builder(BB
);
1048 GlobalVariable
*G
= new GlobalVariable(*M
, Builder
.getInt64Ty(), /*isConstant*/true,
1049 GlobalValue::ExternalLinkage
, nullptr, "", nullptr,
1050 GlobalValue::GeneralDynamicTLSModel
);
1052 Constant
*CEBC
= ConstantExpr::getBitCast(G
, Builder
.getInt8PtrTy());
1053 // Tests that IRBuilder::CreateThreadLocalAddress wouldn't crash if its operand
1054 // is BitCast ConstExpr. The case should be eliminated after we eliminate the
1055 // abuse of constexpr.
1056 CallInst
*CI
= Builder
.CreateThreadLocalAddress(CEBC
);
1057 EXPECT_NE(CI
, nullptr);
1060 TEST_F(IRBuilderTest
, DebugLoc
) {
1061 auto CalleeTy
= FunctionType::get(Type::getVoidTy(Ctx
),
1062 /*isVarArg=*/false);
1064 Function::Create(CalleeTy
, Function::ExternalLinkage
, "", M
.get());
1067 auto File
= DIB
.createFile("tmp.cpp", "/");
1068 auto CU
= DIB
.createCompileUnit(dwarf::DW_LANG_C_plus_plus_11
,
1069 DIB
.createFile("tmp.cpp", "/"), "", true, "",
1072 DIB
.createSubroutineType(DIB
.getOrCreateTypeArray(std::nullopt
));
1074 DIB
.createFunction(CU
, "foo", "foo", File
, 1, SPType
, 1, DINode::FlagZero
,
1075 DISubprogram::SPFlagDefinition
);
1076 DebugLoc DL1
= DILocation::get(Ctx
, 2, 0, SP
);
1077 DebugLoc DL2
= DILocation::get(Ctx
, 3, 0, SP
);
1079 auto BB2
= BasicBlock::Create(Ctx
, "bb2", F
);
1080 auto Br
= BranchInst::Create(BB2
, BB
);
1081 Br
->setDebugLoc(DL1
);
1083 IRBuilder
<> Builder(Ctx
);
1084 Builder
.SetInsertPoint(Br
);
1085 EXPECT_EQ(DL1
, Builder
.getCurrentDebugLocation());
1086 auto Call1
= Builder
.CreateCall(Callee
, std::nullopt
);
1087 EXPECT_EQ(DL1
, Call1
->getDebugLoc());
1089 Call1
->setDebugLoc(DL2
);
1090 Builder
.SetInsertPoint(Call1
->getParent(), Call1
->getIterator());
1091 EXPECT_EQ(DL2
, Builder
.getCurrentDebugLocation());
1092 auto Call2
= Builder
.CreateCall(Callee
, std::nullopt
);
1093 EXPECT_EQ(DL2
, Call2
->getDebugLoc());
1098 TEST_F(IRBuilderTest
, DIImportedEntity
) {
1099 IRBuilder
<> Builder(BB
);
1101 auto F
= DIB
.createFile("F.CBL", "/");
1102 auto CU
= DIB
.createCompileUnit(dwarf::DW_LANG_Cobol74
,
1105 MDTuple
*Elements
= MDTuple::getDistinct(Ctx
, std::nullopt
);
1107 DIB
.createImportedDeclaration(CU
, nullptr, F
, 1);
1108 DIB
.createImportedDeclaration(CU
, nullptr, F
, 1);
1109 DIB
.createImportedModule(CU
, (DIImportedEntity
*)nullptr, F
, 2);
1110 DIB
.createImportedModule(CU
, (DIImportedEntity
*)nullptr, F
, 2);
1111 DIB
.createImportedModule(CU
, (DIImportedEntity
*)nullptr, F
, 2, Elements
);
1112 DIB
.createImportedModule(CU
, (DIImportedEntity
*)nullptr, F
, 2, Elements
);
1114 EXPECT_TRUE(verifyModule(*M
));
1115 EXPECT_TRUE(CU
->getImportedEntities().size() == 3);
1118 // 0: #define M0 V0 <-- command line definition
1119 // 0: main.c <-- main file
1120 // 3: #define M1 V1 <-- M1 definition in main.c
1121 // 5: #include "file.h" <-- inclusion of file.h from main.c
1122 // 1: #define M2 <-- M2 definition in file.h with no value
1123 // 7: #undef M1 V1 <-- M1 un-definition in main.c
1124 TEST_F(IRBuilderTest
, DIBuilderMacro
) {
1125 IRBuilder
<> Builder(BB
);
1127 auto File1
= DIB
.createFile("main.c", "/");
1128 auto File2
= DIB
.createFile("file.h", "/");
1129 auto CU
= DIB
.createCompileUnit(
1130 dwarf::DW_LANG_C
, DIB
.createFile("main.c", "/"), "llvm-c", true, "", 0);
1132 DIB
.createMacro(nullptr, 0, dwarf::DW_MACINFO_define
, "M0", "V0");
1133 auto TMF1
= DIB
.createTempMacroFile(nullptr, 0, File1
);
1134 auto MDef1
= DIB
.createMacro(TMF1
, 3, dwarf::DW_MACINFO_define
, "M1", "V1");
1135 auto TMF2
= DIB
.createTempMacroFile(TMF1
, 5, File2
);
1136 auto MDef2
= DIB
.createMacro(TMF2
, 1, dwarf::DW_MACINFO_define
, "M2");
1137 auto MUndef1
= DIB
.createMacro(TMF1
, 7, dwarf::DW_MACINFO_undef
, "M1");
1139 EXPECT_EQ(dwarf::DW_MACINFO_define
, MDef1
->getMacinfoType());
1140 EXPECT_EQ(3u, MDef1
->getLine());
1141 EXPECT_EQ("M1", MDef1
->getName());
1142 EXPECT_EQ("V1", MDef1
->getValue());
1144 EXPECT_EQ(dwarf::DW_MACINFO_undef
, MUndef1
->getMacinfoType());
1145 EXPECT_EQ(7u, MUndef1
->getLine());
1146 EXPECT_EQ("M1", MUndef1
->getName());
1147 EXPECT_EQ("", MUndef1
->getValue());
1149 EXPECT_EQ(dwarf::DW_MACINFO_start_file
, TMF2
->getMacinfoType());
1150 EXPECT_EQ(5u, TMF2
->getLine());
1151 EXPECT_EQ(File2
, TMF2
->getFile());
1155 SmallVector
<Metadata
*, 4> Elements
;
1156 Elements
.push_back(MDef2
);
1157 auto MF2
= DIMacroFile::get(Ctx
, dwarf::DW_MACINFO_start_file
, 5, File2
,
1158 DIB
.getOrCreateMacroArray(Elements
));
1161 Elements
.push_back(MDef1
);
1162 Elements
.push_back(MF2
);
1163 Elements
.push_back(MUndef1
);
1164 auto MF1
= DIMacroFile::get(Ctx
, dwarf::DW_MACINFO_start_file
, 0, File1
,
1165 DIB
.getOrCreateMacroArray(Elements
));
1168 Elements
.push_back(MDef0
);
1169 Elements
.push_back(MF1
);
1170 auto MN0
= MDTuple::get(Ctx
, Elements
);
1171 EXPECT_EQ(MN0
, CU
->getRawMacros());
1174 Elements
.push_back(MDef1
);
1175 Elements
.push_back(MF2
);
1176 Elements
.push_back(MUndef1
);
1177 auto MN1
= MDTuple::get(Ctx
, Elements
);
1178 EXPECT_EQ(MN1
, MF1
->getRawElements());
1181 Elements
.push_back(MDef2
);
1182 auto MN2
= MDTuple::get(Ctx
, Elements
);
1183 EXPECT_EQ(MN2
, MF2
->getRawElements());
1184 EXPECT_TRUE(verifyModule(*M
));
1187 TEST_F(IRBuilderTest
, NoFolderNames
) {
1188 IRBuilder
<NoFolder
> Builder(BB
);
1190 Builder
.CreateAdd(Builder
.getInt32(1), Builder
.getInt32(2), "add");
1191 EXPECT_EQ(Add
->getName(), "add");
1194 TEST_F(IRBuilderTest
, CTAD
) {
1195 struct TestInserter
: public IRBuilderDefaultInserter
{
1196 TestInserter() = default;
1198 InstSimplifyFolder
Folder(M
->getDataLayout());
1200 IRBuilder
Builder1(Ctx
, Folder
, TestInserter());
1201 static_assert(std::is_same_v
<decltype(Builder1
),
1202 IRBuilder
<InstSimplifyFolder
, TestInserter
>>);
1203 IRBuilder
Builder2(Ctx
);
1204 static_assert(std::is_same_v
<decltype(Builder2
), IRBuilder
<>>);
1205 IRBuilder
Builder3(BB
, Folder
);
1207 std::is_same_v
<decltype(Builder3
), IRBuilder
<InstSimplifyFolder
>>);
1208 IRBuilder
Builder4(BB
);
1209 static_assert(std::is_same_v
<decltype(Builder4
), IRBuilder
<>>);
1210 // The block BB is empty, so don't test this one.
1211 // IRBuilder Builder5(BB->getTerminator());
1212 // static_assert(std::is_same_v<decltype(Builder5), IRBuilder<>>);
1213 IRBuilder
Builder6(BB
, BB
->end(), Folder
);
1215 std::is_same_v
<decltype(Builder6
), IRBuilder
<InstSimplifyFolder
>>);
1216 IRBuilder
Builder7(BB
, BB
->end());
1217 static_assert(std::is_same_v
<decltype(Builder7
), IRBuilder
<>>);