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/IR/IRBuilder.h"
10 #include "llvm/IR/BasicBlock.h"
11 #include "llvm/IR/DIBuilder.h"
12 #include "llvm/IR/DataLayout.h"
13 #include "llvm/IR/Function.h"
14 #include "llvm/IR/IntrinsicInst.h"
15 #include "llvm/IR/LLVMContext.h"
16 #include "llvm/IR/MDBuilder.h"
17 #include "llvm/IR/Module.h"
18 #include "llvm/IR/NoFolder.h"
19 #include "llvm/IR/Verifier.h"
20 #include "gtest/gtest.h"
26 class IRBuilderTest
: public testing::Test
{
28 void SetUp() override
{
29 M
.reset(new Module("MyModule", Ctx
));
30 FunctionType
*FTy
= FunctionType::get(Type::getVoidTy(Ctx
),
32 F
= Function::Create(FTy
, Function::ExternalLinkage
, "", M
.get());
33 BB
= BasicBlock::Create(Ctx
, "", F
);
34 GV
= new GlobalVariable(*M
, Type::getFloatTy(Ctx
), true,
35 GlobalValue::ExternalLinkage
, nullptr);
38 void TearDown() override
{
44 std::unique_ptr
<Module
> M
;
50 TEST_F(IRBuilderTest
, Intrinsics
) {
51 IRBuilder
<> Builder(BB
);
57 V
= Builder
.CreateLoad(GV
->getValueType(), GV
);
58 I
= cast
<Instruction
>(Builder
.CreateFAdd(V
, V
));
59 I
->setHasNoInfs(true);
60 I
->setHasNoNaNs(false);
62 Call
= Builder
.CreateMinNum(V
, V
);
63 II
= cast
<IntrinsicInst
>(Call
);
64 EXPECT_EQ(II
->getIntrinsicID(), Intrinsic::minnum
);
66 Call
= Builder
.CreateMaxNum(V
, V
);
67 II
= cast
<IntrinsicInst
>(Call
);
68 EXPECT_EQ(II
->getIntrinsicID(), Intrinsic::maxnum
);
70 Call
= Builder
.CreateMinimum(V
, V
);
71 II
= cast
<IntrinsicInst
>(Call
);
72 EXPECT_EQ(II
->getIntrinsicID(), Intrinsic::minimum
);
74 Call
= Builder
.CreateMaximum(V
, V
);
75 II
= cast
<IntrinsicInst
>(Call
);
76 EXPECT_EQ(II
->getIntrinsicID(), Intrinsic::maximum
);
78 Call
= Builder
.CreateIntrinsic(Intrinsic::readcyclecounter
, {}, {});
79 II
= cast
<IntrinsicInst
>(Call
);
80 EXPECT_EQ(II
->getIntrinsicID(), Intrinsic::readcyclecounter
);
82 Call
= Builder
.CreateUnaryIntrinsic(Intrinsic::fabs
, V
);
83 II
= cast
<IntrinsicInst
>(Call
);
84 EXPECT_EQ(II
->getIntrinsicID(), Intrinsic::fabs
);
85 EXPECT_FALSE(II
->hasNoInfs());
86 EXPECT_FALSE(II
->hasNoNaNs());
88 Call
= Builder
.CreateUnaryIntrinsic(Intrinsic::fabs
, V
, I
);
89 II
= cast
<IntrinsicInst
>(Call
);
90 EXPECT_EQ(II
->getIntrinsicID(), Intrinsic::fabs
);
91 EXPECT_TRUE(II
->hasNoInfs());
92 EXPECT_FALSE(II
->hasNoNaNs());
94 Call
= Builder
.CreateBinaryIntrinsic(Intrinsic::pow
, V
, V
);
95 II
= cast
<IntrinsicInst
>(Call
);
96 EXPECT_EQ(II
->getIntrinsicID(), Intrinsic::pow
);
97 EXPECT_FALSE(II
->hasNoInfs());
98 EXPECT_FALSE(II
->hasNoNaNs());
100 Call
= Builder
.CreateBinaryIntrinsic(Intrinsic::pow
, V
, V
, I
);
101 II
= cast
<IntrinsicInst
>(Call
);
102 EXPECT_EQ(II
->getIntrinsicID(), Intrinsic::pow
);
103 EXPECT_TRUE(II
->hasNoInfs());
104 EXPECT_FALSE(II
->hasNoNaNs());
106 Call
= Builder
.CreateIntrinsic(Intrinsic::fma
, {V
->getType()}, {V
, V
, V
});
107 II
= cast
<IntrinsicInst
>(Call
);
108 EXPECT_EQ(II
->getIntrinsicID(), Intrinsic::fma
);
109 EXPECT_FALSE(II
->hasNoInfs());
110 EXPECT_FALSE(II
->hasNoNaNs());
112 Call
= Builder
.CreateIntrinsic(Intrinsic::fma
, {V
->getType()}, {V
, V
, V
}, I
);
113 II
= cast
<IntrinsicInst
>(Call
);
114 EXPECT_EQ(II
->getIntrinsicID(), Intrinsic::fma
);
115 EXPECT_TRUE(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());
125 TEST_F(IRBuilderTest
, ConstrainedFP
) {
126 IRBuilder
<> Builder(BB
);
131 GlobalVariable
*GVDouble
= new GlobalVariable(*M
, Type::getDoubleTy(Ctx
),
132 true, GlobalValue::ExternalLinkage
, nullptr);
134 V
= Builder
.CreateLoad(GV
->getValueType(), GV
);
135 VDouble
= Builder
.CreateLoad(GVDouble
->getValueType(), GVDouble
);
137 // See if we get constrained intrinsics instead of non-constrained
139 Builder
.setIsFPConstrained(true);
141 V
= Builder
.CreateFAdd(V
, V
);
142 ASSERT_TRUE(isa
<IntrinsicInst
>(V
));
143 II
= cast
<IntrinsicInst
>(V
);
144 EXPECT_EQ(II
->getIntrinsicID(), Intrinsic::experimental_constrained_fadd
);
146 V
= Builder
.CreateFSub(V
, V
);
147 ASSERT_TRUE(isa
<IntrinsicInst
>(V
));
148 II
= cast
<IntrinsicInst
>(V
);
149 EXPECT_EQ(II
->getIntrinsicID(), Intrinsic::experimental_constrained_fsub
);
151 V
= Builder
.CreateFMul(V
, V
);
152 ASSERT_TRUE(isa
<IntrinsicInst
>(V
));
153 II
= cast
<IntrinsicInst
>(V
);
154 EXPECT_EQ(II
->getIntrinsicID(), Intrinsic::experimental_constrained_fmul
);
156 V
= Builder
.CreateFDiv(V
, V
);
157 ASSERT_TRUE(isa
<IntrinsicInst
>(V
));
158 II
= cast
<IntrinsicInst
>(V
);
159 EXPECT_EQ(II
->getIntrinsicID(), Intrinsic::experimental_constrained_fdiv
);
161 V
= Builder
.CreateFRem(V
, V
);
162 ASSERT_TRUE(isa
<IntrinsicInst
>(V
));
163 II
= cast
<IntrinsicInst
>(V
);
164 EXPECT_EQ(II
->getIntrinsicID(), Intrinsic::experimental_constrained_frem
);
166 V
= Builder
.CreateFPTrunc(VDouble
, Type::getFloatTy(Ctx
));
167 ASSERT_TRUE(isa
<IntrinsicInst
>(V
));
168 II
= cast
<IntrinsicInst
>(V
);
169 EXPECT_EQ(II
->getIntrinsicID(), Intrinsic::experimental_constrained_fptrunc
);
171 VDouble
= Builder
.CreateFPExt(V
, Type::getDoubleTy(Ctx
));
172 ASSERT_TRUE(isa
<IntrinsicInst
>(VDouble
));
173 II
= cast
<IntrinsicInst
>(VDouble
);
174 EXPECT_EQ(II
->getIntrinsicID(), Intrinsic::experimental_constrained_fpext
);
176 // Verify the codepaths for setting and overriding the default metadata.
177 V
= Builder
.CreateFAdd(V
, V
);
178 ASSERT_TRUE(isa
<ConstrainedFPIntrinsic
>(V
));
179 auto *CII
= cast
<ConstrainedFPIntrinsic
>(V
);
180 ASSERT_TRUE(CII
->getExceptionBehavior() == ConstrainedFPIntrinsic::ebStrict
);
181 ASSERT_TRUE(CII
->getRoundingMode() == ConstrainedFPIntrinsic::rmDynamic
);
183 Builder
.setDefaultConstrainedExcept(ConstrainedFPIntrinsic::ebIgnore
);
184 Builder
.setDefaultConstrainedRounding(ConstrainedFPIntrinsic::rmUpward
);
185 V
= Builder
.CreateFAdd(V
, V
);
186 CII
= cast
<ConstrainedFPIntrinsic
>(V
);
187 ASSERT_TRUE(CII
->getExceptionBehavior() == ConstrainedFPIntrinsic::ebIgnore
);
188 ASSERT_TRUE(CII
->getRoundingMode() == ConstrainedFPIntrinsic::rmUpward
);
190 Builder
.setDefaultConstrainedExcept(ConstrainedFPIntrinsic::ebIgnore
);
191 Builder
.setDefaultConstrainedRounding(ConstrainedFPIntrinsic::rmToNearest
);
192 V
= Builder
.CreateFAdd(V
, V
);
193 CII
= cast
<ConstrainedFPIntrinsic
>(V
);
194 ASSERT_TRUE(CII
->getExceptionBehavior() == ConstrainedFPIntrinsic::ebIgnore
);
195 ASSERT_TRUE(CII
->getRoundingMode() == ConstrainedFPIntrinsic::rmToNearest
);
197 Builder
.setDefaultConstrainedExcept(ConstrainedFPIntrinsic::ebMayTrap
);
198 Builder
.setDefaultConstrainedRounding(ConstrainedFPIntrinsic::rmDownward
);
199 V
= Builder
.CreateFAdd(V
, V
);
200 CII
= cast
<ConstrainedFPIntrinsic
>(V
);
201 ASSERT_TRUE(CII
->getExceptionBehavior() == ConstrainedFPIntrinsic::ebMayTrap
);
202 ASSERT_TRUE(CII
->getRoundingMode() == ConstrainedFPIntrinsic::rmDownward
);
204 Builder
.setDefaultConstrainedExcept(ConstrainedFPIntrinsic::ebStrict
);
205 Builder
.setDefaultConstrainedRounding(ConstrainedFPIntrinsic::rmTowardZero
);
206 V
= Builder
.CreateFAdd(V
, V
);
207 CII
= cast
<ConstrainedFPIntrinsic
>(V
);
208 ASSERT_TRUE(CII
->getExceptionBehavior() == ConstrainedFPIntrinsic::ebStrict
);
209 ASSERT_TRUE(CII
->getRoundingMode() == ConstrainedFPIntrinsic::rmTowardZero
);
211 Builder
.setDefaultConstrainedExcept(ConstrainedFPIntrinsic::ebIgnore
);
212 Builder
.setDefaultConstrainedRounding(ConstrainedFPIntrinsic::rmDynamic
);
213 V
= Builder
.CreateFAdd(V
, V
);
214 CII
= cast
<ConstrainedFPIntrinsic
>(V
);
215 ASSERT_TRUE(CII
->getExceptionBehavior() == ConstrainedFPIntrinsic::ebIgnore
);
216 ASSERT_TRUE(CII
->getRoundingMode() == ConstrainedFPIntrinsic::rmDynamic
);
218 // Now override the defaults.
219 Call
= Builder
.CreateConstrainedFPBinOp(
220 Intrinsic::experimental_constrained_fadd
, V
, V
, nullptr, "", nullptr,
221 ConstrainedFPIntrinsic::rmDownward
, ConstrainedFPIntrinsic::ebMayTrap
);
222 CII
= cast
<ConstrainedFPIntrinsic
>(Call
);
223 EXPECT_EQ(CII
->getIntrinsicID(), Intrinsic::experimental_constrained_fadd
);
224 ASSERT_TRUE(CII
->getExceptionBehavior() == ConstrainedFPIntrinsic::ebMayTrap
);
225 ASSERT_TRUE(CII
->getRoundingMode() == ConstrainedFPIntrinsic::rmDownward
);
227 Builder
.CreateRetVoid();
228 EXPECT_FALSE(verifyModule(*M
));
231 TEST_F(IRBuilderTest
, Lifetime
) {
232 IRBuilder
<> Builder(BB
);
233 AllocaInst
*Var1
= Builder
.CreateAlloca(Builder
.getInt8Ty());
234 AllocaInst
*Var2
= Builder
.CreateAlloca(Builder
.getInt32Ty());
235 AllocaInst
*Var3
= Builder
.CreateAlloca(Builder
.getInt8Ty(),
236 Builder
.getInt32(123));
238 CallInst
*Start1
= Builder
.CreateLifetimeStart(Var1
);
239 CallInst
*Start2
= Builder
.CreateLifetimeStart(Var2
);
240 CallInst
*Start3
= Builder
.CreateLifetimeStart(Var3
, Builder
.getInt64(100));
242 EXPECT_EQ(Start1
->getArgOperand(0), Builder
.getInt64(-1));
243 EXPECT_EQ(Start2
->getArgOperand(0), Builder
.getInt64(-1));
244 EXPECT_EQ(Start3
->getArgOperand(0), Builder
.getInt64(100));
246 EXPECT_EQ(Start1
->getArgOperand(1), Var1
);
247 EXPECT_NE(Start2
->getArgOperand(1), Var2
);
248 EXPECT_EQ(Start3
->getArgOperand(1), Var3
);
250 Value
*End1
= Builder
.CreateLifetimeEnd(Var1
);
251 Builder
.CreateLifetimeEnd(Var2
);
252 Builder
.CreateLifetimeEnd(Var3
);
254 IntrinsicInst
*II_Start1
= dyn_cast
<IntrinsicInst
>(Start1
);
255 IntrinsicInst
*II_End1
= dyn_cast
<IntrinsicInst
>(End1
);
256 ASSERT_TRUE(II_Start1
!= nullptr);
257 EXPECT_EQ(II_Start1
->getIntrinsicID(), Intrinsic::lifetime_start
);
258 ASSERT_TRUE(II_End1
!= nullptr);
259 EXPECT_EQ(II_End1
->getIntrinsicID(), Intrinsic::lifetime_end
);
262 TEST_F(IRBuilderTest
, CreateCondBr
) {
263 IRBuilder
<> Builder(BB
);
264 BasicBlock
*TBB
= BasicBlock::Create(Ctx
, "", F
);
265 BasicBlock
*FBB
= BasicBlock::Create(Ctx
, "", F
);
267 BranchInst
*BI
= Builder
.CreateCondBr(Builder
.getTrue(), TBB
, FBB
);
268 Instruction
*TI
= BB
->getTerminator();
270 EXPECT_EQ(2u, TI
->getNumSuccessors());
271 EXPECT_EQ(TBB
, TI
->getSuccessor(0));
272 EXPECT_EQ(FBB
, TI
->getSuccessor(1));
274 BI
->eraseFromParent();
275 MDNode
*Weights
= MDBuilder(Ctx
).createBranchWeights(42, 13);
276 BI
= Builder
.CreateCondBr(Builder
.getTrue(), TBB
, FBB
, Weights
);
277 TI
= BB
->getTerminator();
279 EXPECT_EQ(2u, TI
->getNumSuccessors());
280 EXPECT_EQ(TBB
, TI
->getSuccessor(0));
281 EXPECT_EQ(FBB
, TI
->getSuccessor(1));
282 EXPECT_EQ(Weights
, TI
->getMetadata(LLVMContext::MD_prof
));
285 TEST_F(IRBuilderTest
, LandingPadName
) {
286 IRBuilder
<> Builder(BB
);
287 LandingPadInst
*LP
= Builder
.CreateLandingPad(Builder
.getInt32Ty(), 0, "LP");
288 EXPECT_EQ(LP
->getName(), "LP");
291 TEST_F(IRBuilderTest
, DataLayout
) {
292 std::unique_ptr
<Module
> M(new Module("test", Ctx
));
293 M
->setDataLayout("e-n32");
294 EXPECT_TRUE(M
->getDataLayout().isLegalInteger(32));
295 M
->setDataLayout("e");
296 EXPECT_FALSE(M
->getDataLayout().isLegalInteger(32));
299 TEST_F(IRBuilderTest
, GetIntTy
) {
300 IRBuilder
<> Builder(BB
);
301 IntegerType
*Ty1
= Builder
.getInt1Ty();
302 EXPECT_EQ(Ty1
, IntegerType::get(Ctx
, 1));
304 DataLayout
* DL
= new DataLayout(M
.get());
305 IntegerType
*IntPtrTy
= Builder
.getIntPtrTy(*DL
);
306 unsigned IntPtrBitSize
= DL
->getPointerSizeInBits(0);
307 EXPECT_EQ(IntPtrTy
, IntegerType::get(Ctx
, IntPtrBitSize
));
311 TEST_F(IRBuilderTest
, UnaryOperators
) {
312 IRBuilder
<NoFolder
> Builder(BB
);
313 Value
*V
= Builder
.CreateLoad(GV
->getValueType(), GV
);
315 // Test CreateUnOp(X)
316 Value
*U
= Builder
.CreateUnOp(Instruction::FNeg
, V
);
317 ASSERT_TRUE(isa
<Instruction
>(U
));
318 ASSERT_TRUE(isa
<FPMathOperator
>(U
));
319 ASSERT_TRUE(isa
<UnaryOperator
>(U
));
320 ASSERT_FALSE(isa
<BinaryOperator
>(U
));
322 // Test CreateFNegFMF(X)
323 Instruction
*I
= cast
<Instruction
>(V
);
324 I
->setHasNoSignedZeros(true);
325 I
->setHasNoNaNs(true);
326 Value
*VFMF
= Builder
.CreateFNegFMF(V
, I
);
327 Instruction
*IFMF
= cast
<Instruction
>(VFMF
);
328 EXPECT_TRUE(IFMF
->hasNoSignedZeros());
329 EXPECT_TRUE(IFMF
->hasNoNaNs());
330 EXPECT_FALSE(IFMF
->hasAllowReassoc());
333 TEST_F(IRBuilderTest
, FastMathFlags
) {
334 IRBuilder
<> Builder(BB
);
336 Instruction
*FDiv
, *FAdd
, *FCmp
, *FCall
;
338 F
= Builder
.CreateLoad(GV
->getValueType(), GV
);
339 F
= Builder
.CreateFAdd(F
, F
);
341 EXPECT_FALSE(Builder
.getFastMathFlags().any());
342 ASSERT_TRUE(isa
<Instruction
>(F
));
343 FAdd
= cast
<Instruction
>(F
);
344 EXPECT_FALSE(FAdd
->hasNoNaNs());
347 Builder
.setFastMathFlags(FMF
);
349 // By default, no flags are set.
350 F
= Builder
.CreateFAdd(F
, F
);
351 EXPECT_FALSE(Builder
.getFastMathFlags().any());
352 ASSERT_TRUE(isa
<Instruction
>(F
));
353 FAdd
= cast
<Instruction
>(F
);
354 EXPECT_FALSE(FAdd
->hasNoNaNs());
355 EXPECT_FALSE(FAdd
->hasNoInfs());
356 EXPECT_FALSE(FAdd
->hasNoSignedZeros());
357 EXPECT_FALSE(FAdd
->hasAllowReciprocal());
358 EXPECT_FALSE(FAdd
->hasAllowContract());
359 EXPECT_FALSE(FAdd
->hasAllowReassoc());
360 EXPECT_FALSE(FAdd
->hasApproxFunc());
362 // Set all flags in the instruction.
364 EXPECT_TRUE(FAdd
->hasNoNaNs());
365 EXPECT_TRUE(FAdd
->hasNoInfs());
366 EXPECT_TRUE(FAdd
->hasNoSignedZeros());
367 EXPECT_TRUE(FAdd
->hasAllowReciprocal());
368 EXPECT_TRUE(FAdd
->hasAllowContract());
369 EXPECT_TRUE(FAdd
->hasAllowReassoc());
370 EXPECT_TRUE(FAdd
->hasApproxFunc());
372 // All flags are set in the builder.
374 Builder
.setFastMathFlags(FMF
);
376 F
= Builder
.CreateFAdd(F
, F
);
377 EXPECT_TRUE(Builder
.getFastMathFlags().any());
378 EXPECT_TRUE(Builder
.getFastMathFlags().all());
379 ASSERT_TRUE(isa
<Instruction
>(F
));
380 FAdd
= cast
<Instruction
>(F
);
381 EXPECT_TRUE(FAdd
->hasNoNaNs());
382 EXPECT_TRUE(FAdd
->isFast());
384 // Now, try it with CreateBinOp
385 F
= Builder
.CreateBinOp(Instruction::FAdd
, F
, F
);
386 EXPECT_TRUE(Builder
.getFastMathFlags().any());
387 ASSERT_TRUE(isa
<Instruction
>(F
));
388 FAdd
= cast
<Instruction
>(F
);
389 EXPECT_TRUE(FAdd
->hasNoNaNs());
390 EXPECT_TRUE(FAdd
->isFast());
392 F
= Builder
.CreateFDiv(F
, F
);
393 EXPECT_TRUE(Builder
.getFastMathFlags().all());
394 ASSERT_TRUE(isa
<Instruction
>(F
));
395 FDiv
= cast
<Instruction
>(F
);
396 EXPECT_TRUE(FDiv
->hasAllowReciprocal());
398 // Clear all FMF in the builder.
399 Builder
.clearFastMathFlags();
401 F
= Builder
.CreateFDiv(F
, F
);
402 ASSERT_TRUE(isa
<Instruction
>(F
));
403 FDiv
= cast
<Instruction
>(F
);
404 EXPECT_FALSE(FDiv
->hasAllowReciprocal());
406 // Try individual flags.
408 FMF
.setAllowReciprocal();
409 Builder
.setFastMathFlags(FMF
);
411 F
= Builder
.CreateFDiv(F
, F
);
412 EXPECT_TRUE(Builder
.getFastMathFlags().any());
413 EXPECT_TRUE(Builder
.getFastMathFlags().AllowReciprocal
);
414 ASSERT_TRUE(isa
<Instruction
>(F
));
415 FDiv
= cast
<Instruction
>(F
);
416 EXPECT_TRUE(FDiv
->hasAllowReciprocal());
418 Builder
.clearFastMathFlags();
420 FC
= Builder
.CreateFCmpOEQ(F
, F
);
421 ASSERT_TRUE(isa
<Instruction
>(FC
));
422 FCmp
= cast
<Instruction
>(FC
);
423 EXPECT_FALSE(FCmp
->hasAllowReciprocal());
426 FMF
.setAllowReciprocal();
427 Builder
.setFastMathFlags(FMF
);
429 FC
= Builder
.CreateFCmpOEQ(F
, F
);
430 EXPECT_TRUE(Builder
.getFastMathFlags().any());
431 EXPECT_TRUE(Builder
.getFastMathFlags().AllowReciprocal
);
432 ASSERT_TRUE(isa
<Instruction
>(FC
));
433 FCmp
= cast
<Instruction
>(FC
);
434 EXPECT_TRUE(FCmp
->hasAllowReciprocal());
436 Builder
.clearFastMathFlags();
439 FC
= Builder
.CreateFAdd(F
, F
);
440 ASSERT_TRUE(isa
<Instruction
>(FC
));
441 FAdd
= cast
<Instruction
>(FC
);
442 EXPECT_FALSE(FAdd
->hasAllowContract());
445 FMF
.setAllowContract(true);
446 Builder
.setFastMathFlags(FMF
);
448 FC
= Builder
.CreateFAdd(F
, F
);
449 EXPECT_TRUE(Builder
.getFastMathFlags().any());
450 EXPECT_TRUE(Builder
.getFastMathFlags().AllowContract
);
451 ASSERT_TRUE(isa
<Instruction
>(FC
));
452 FAdd
= cast
<Instruction
>(FC
);
453 EXPECT_TRUE(FAdd
->hasAllowContract());
456 Builder
.clearFastMathFlags();
457 Builder
.setFastMathFlags(FMF
);
458 // Now 'aml' and 'contract' are set.
459 F
= Builder
.CreateFMul(F
, F
);
460 FAdd
= cast
<Instruction
>(F
);
461 EXPECT_TRUE(FAdd
->hasApproxFunc());
462 EXPECT_TRUE(FAdd
->hasAllowContract());
463 EXPECT_FALSE(FAdd
->hasAllowReassoc());
465 FMF
.setAllowReassoc();
466 Builder
.clearFastMathFlags();
467 Builder
.setFastMathFlags(FMF
);
468 // Now 'aml' and 'contract' and 'reassoc' are set.
469 F
= Builder
.CreateFMul(F
, F
);
470 FAdd
= cast
<Instruction
>(F
);
471 EXPECT_TRUE(FAdd
->hasApproxFunc());
472 EXPECT_TRUE(FAdd
->hasAllowContract());
473 EXPECT_TRUE(FAdd
->hasAllowReassoc());
475 // Test a call with FMF.
476 auto CalleeTy
= FunctionType::get(Type::getFloatTy(Ctx
),
479 Function::Create(CalleeTy
, Function::ExternalLinkage
, "", M
.get());
481 FCall
= Builder
.CreateCall(Callee
, None
);
482 EXPECT_FALSE(FCall
->hasNoNaNs());
485 Function::Create(CalleeTy
, Function::ExternalLinkage
, "", M
.get());
486 FCall
= Builder
.CreateCall(V
, None
);
487 EXPECT_FALSE(FCall
->hasNoNaNs());
491 Builder
.setFastMathFlags(FMF
);
493 FCall
= Builder
.CreateCall(Callee
, None
);
494 EXPECT_TRUE(Builder
.getFastMathFlags().any());
495 EXPECT_TRUE(Builder
.getFastMathFlags().NoNaNs
);
496 EXPECT_TRUE(FCall
->hasNoNaNs());
498 FCall
= Builder
.CreateCall(V
, None
);
499 EXPECT_TRUE(Builder
.getFastMathFlags().any());
500 EXPECT_TRUE(Builder
.getFastMathFlags().NoNaNs
);
501 EXPECT_TRUE(FCall
->hasNoNaNs());
503 Builder
.clearFastMathFlags();
505 // To test a copy, make sure that a '0' and a '1' change state.
506 F
= Builder
.CreateFDiv(F
, F
);
507 ASSERT_TRUE(isa
<Instruction
>(F
));
508 FDiv
= cast
<Instruction
>(F
);
509 EXPECT_FALSE(FDiv
->getFastMathFlags().any());
510 FDiv
->setHasAllowReciprocal(true);
511 FAdd
->setHasAllowReciprocal(false);
512 FAdd
->setHasNoNaNs(true);
513 FDiv
->copyFastMathFlags(FAdd
);
514 EXPECT_TRUE(FDiv
->hasNoNaNs());
515 EXPECT_FALSE(FDiv
->hasAllowReciprocal());
519 TEST_F(IRBuilderTest
, WrapFlags
) {
520 IRBuilder
<NoFolder
> Builder(BB
);
522 // Test instructions.
523 GlobalVariable
*G
= new GlobalVariable(*M
, Builder
.getInt32Ty(), true,
524 GlobalValue::ExternalLinkage
, nullptr);
525 Value
*V
= Builder
.CreateLoad(G
->getValueType(), G
);
527 cast
<BinaryOperator
>(Builder
.CreateNSWAdd(V
, V
))->hasNoSignedWrap());
529 cast
<BinaryOperator
>(Builder
.CreateNSWMul(V
, V
))->hasNoSignedWrap());
531 cast
<BinaryOperator
>(Builder
.CreateNSWSub(V
, V
))->hasNoSignedWrap());
532 EXPECT_TRUE(cast
<BinaryOperator
>(
533 Builder
.CreateShl(V
, V
, "", /* NUW */ false, /* NSW */ true))
534 ->hasNoSignedWrap());
537 cast
<BinaryOperator
>(Builder
.CreateNUWAdd(V
, V
))->hasNoUnsignedWrap());
539 cast
<BinaryOperator
>(Builder
.CreateNUWMul(V
, V
))->hasNoUnsignedWrap());
541 cast
<BinaryOperator
>(Builder
.CreateNUWSub(V
, V
))->hasNoUnsignedWrap());
542 EXPECT_TRUE(cast
<BinaryOperator
>(
543 Builder
.CreateShl(V
, V
, "", /* NUW */ true, /* NSW */ false))
544 ->hasNoUnsignedWrap());
546 // Test operators created with constants.
547 Constant
*C
= Builder
.getInt32(42);
548 EXPECT_TRUE(cast
<OverflowingBinaryOperator
>(Builder
.CreateNSWAdd(C
, C
))
549 ->hasNoSignedWrap());
550 EXPECT_TRUE(cast
<OverflowingBinaryOperator
>(Builder
.CreateNSWSub(C
, C
))
551 ->hasNoSignedWrap());
552 EXPECT_TRUE(cast
<OverflowingBinaryOperator
>(Builder
.CreateNSWMul(C
, C
))
553 ->hasNoSignedWrap());
554 EXPECT_TRUE(cast
<OverflowingBinaryOperator
>(
555 Builder
.CreateShl(C
, C
, "", /* NUW */ false, /* NSW */ true))
556 ->hasNoSignedWrap());
558 EXPECT_TRUE(cast
<OverflowingBinaryOperator
>(Builder
.CreateNUWAdd(C
, C
))
559 ->hasNoUnsignedWrap());
560 EXPECT_TRUE(cast
<OverflowingBinaryOperator
>(Builder
.CreateNUWSub(C
, C
))
561 ->hasNoUnsignedWrap());
562 EXPECT_TRUE(cast
<OverflowingBinaryOperator
>(Builder
.CreateNUWMul(C
, C
))
563 ->hasNoUnsignedWrap());
564 EXPECT_TRUE(cast
<OverflowingBinaryOperator
>(
565 Builder
.CreateShl(C
, C
, "", /* NUW */ true, /* NSW */ false))
566 ->hasNoUnsignedWrap());
569 TEST_F(IRBuilderTest
, RAIIHelpersTest
) {
570 IRBuilder
<> Builder(BB
);
571 EXPECT_FALSE(Builder
.getFastMathFlags().allowReciprocal());
572 MDBuilder
MDB(M
->getContext());
574 MDNode
*FPMathA
= MDB
.createFPMath(0.01f
);
575 MDNode
*FPMathB
= MDB
.createFPMath(0.1f
);
577 Builder
.setDefaultFPMathTag(FPMathA
);
580 IRBuilder
<>::FastMathFlagGuard
Guard(Builder
);
582 FMF
.setAllowReciprocal();
583 Builder
.setFastMathFlags(FMF
);
584 Builder
.setDefaultFPMathTag(FPMathB
);
585 EXPECT_TRUE(Builder
.getFastMathFlags().allowReciprocal());
586 EXPECT_EQ(FPMathB
, Builder
.getDefaultFPMathTag());
589 EXPECT_FALSE(Builder
.getFastMathFlags().allowReciprocal());
590 EXPECT_EQ(FPMathA
, Builder
.getDefaultFPMathTag());
592 Value
*F
= Builder
.CreateLoad(GV
->getValueType(), GV
);
595 IRBuilder
<>::InsertPointGuard
Guard(Builder
);
596 Builder
.SetInsertPoint(cast
<Instruction
>(F
));
597 EXPECT_EQ(F
, &*Builder
.GetInsertPoint());
600 EXPECT_EQ(BB
->end(), Builder
.GetInsertPoint());
601 EXPECT_EQ(BB
, Builder
.GetInsertBlock());
604 TEST_F(IRBuilderTest
, createFunction
) {
605 IRBuilder
<> Builder(BB
);
607 auto File
= DIB
.createFile("error.swift", "/");
609 DIB
.createCompileUnit(dwarf::DW_LANG_Swift
, File
, "swiftc", true, "", 0);
610 auto Type
= DIB
.createSubroutineType(DIB
.getOrCreateTypeArray(None
));
611 auto NoErr
= DIB
.createFunction(
612 CU
, "noerr", "", File
, 1, Type
, 1, DINode::FlagZero
,
613 DISubprogram::SPFlagDefinition
| DISubprogram::SPFlagOptimized
);
614 EXPECT_TRUE(!NoErr
->getThrownTypes());
615 auto Int
= DIB
.createBasicType("Int", 64, dwarf::DW_ATE_signed
);
616 auto Error
= DIB
.getOrCreateArray({Int
});
617 auto Err
= DIB
.createFunction(
618 CU
, "err", "", File
, 1, Type
, 1, DINode::FlagZero
,
619 DISubprogram::SPFlagDefinition
| DISubprogram::SPFlagOptimized
, nullptr,
620 nullptr, Error
.get());
621 EXPECT_TRUE(Err
->getThrownTypes().get() == Error
.get());
625 TEST_F(IRBuilderTest
, DIBuilder
) {
626 IRBuilder
<> Builder(BB
);
628 auto File
= DIB
.createFile("F.CBL", "/");
629 auto CU
= DIB
.createCompileUnit(dwarf::DW_LANG_Cobol74
,
630 DIB
.createFile("F.CBL", "/"), "llvm-cobol74",
632 auto Type
= DIB
.createSubroutineType(DIB
.getOrCreateTypeArray(None
));
633 auto SP
= DIB
.createFunction(
634 CU
, "foo", "", File
, 1, Type
, 1, DINode::FlagZero
,
635 DISubprogram::SPFlagDefinition
| DISubprogram::SPFlagOptimized
);
636 F
->setSubprogram(SP
);
637 AllocaInst
*I
= Builder
.CreateAlloca(Builder
.getInt8Ty());
638 auto BarSP
= DIB
.createFunction(
639 CU
, "bar", "", File
, 1, Type
, 1, DINode::FlagZero
,
640 DISubprogram::SPFlagDefinition
| DISubprogram::SPFlagOptimized
);
641 auto BadScope
= DIB
.createLexicalBlockFile(BarSP
, File
, 0);
642 I
->setDebugLoc(DebugLoc::get(2, 0, BadScope
));
644 EXPECT_TRUE(verifyModule(*M
));
647 TEST_F(IRBuilderTest
, createArtificialSubprogram
) {
648 IRBuilder
<> Builder(BB
);
650 auto File
= DIB
.createFile("main.c", "/");
651 auto CU
= DIB
.createCompileUnit(dwarf::DW_LANG_C
, File
, "clang",
652 /*isOptimized=*/true, /*Flags=*/"",
653 /*Runtime Version=*/0);
654 auto Type
= DIB
.createSubroutineType(DIB
.getOrCreateTypeArray(None
));
655 auto SP
= DIB
.createFunction(
656 CU
, "foo", /*LinkageName=*/"", File
,
657 /*LineNo=*/1, Type
, /*ScopeLine=*/2, DINode::FlagZero
,
658 DISubprogram::SPFlagDefinition
| DISubprogram::SPFlagOptimized
);
659 EXPECT_TRUE(SP
->isDistinct());
661 F
->setSubprogram(SP
);
662 AllocaInst
*I
= Builder
.CreateAlloca(Builder
.getInt8Ty());
663 ReturnInst
*R
= Builder
.CreateRetVoid();
664 I
->setDebugLoc(DebugLoc::get(3, 2, SP
));
665 R
->setDebugLoc(DebugLoc::get(4, 2, SP
));
667 EXPECT_FALSE(verifyModule(*M
));
669 Function
*G
= Function::Create(F
->getFunctionType(),
670 Function::ExternalLinkage
, "", M
.get());
671 BasicBlock
*GBB
= BasicBlock::Create(Ctx
, "", G
);
672 Builder
.SetInsertPoint(GBB
);
673 I
->removeFromParent();
675 Builder
.CreateRetVoid();
676 EXPECT_FALSE(verifyModule(*M
));
678 DISubprogram
*GSP
= DIBuilder::createArtificialSubprogram(F
->getSubprogram());
679 EXPECT_EQ(SP
->getFile(), GSP
->getFile());
680 EXPECT_EQ(SP
->getType(), GSP
->getType());
681 EXPECT_EQ(SP
->getLine(), GSP
->getLine());
682 EXPECT_EQ(SP
->getScopeLine(), GSP
->getScopeLine());
683 EXPECT_TRUE(GSP
->isDistinct());
685 G
->setSubprogram(GSP
);
686 EXPECT_TRUE(verifyModule(*M
));
688 auto *InlinedAtNode
=
689 DILocation::getDistinct(Ctx
, GSP
->getScopeLine(), 0, GSP
);
690 DebugLoc DL
= I
->getDebugLoc();
691 DenseMap
<const MDNode
*, MDNode
*> IANodes
;
692 auto IA
= DebugLoc::appendInlinedAt(DL
, InlinedAtNode
, Ctx
, IANodes
);
693 auto NewDL
= DebugLoc::get(DL
.getLine(), DL
.getCol(), DL
.getScope(), IA
);
694 I
->setDebugLoc(NewDL
);
695 EXPECT_FALSE(verifyModule(*M
));
697 EXPECT_EQ("foo", SP
->getName());
698 EXPECT_EQ("foo", GSP
->getName());
699 EXPECT_FALSE(SP
->isArtificial());
700 EXPECT_TRUE(GSP
->isArtificial());
703 TEST_F(IRBuilderTest
, InsertExtractElement
) {
704 IRBuilder
<> Builder(BB
);
706 auto VecTy
= VectorType::get(Builder
.getInt64Ty(), 4);
707 auto Elt1
= Builder
.getInt64(-1);
708 auto Elt2
= Builder
.getInt64(-2);
709 Value
*Vec
= UndefValue::get(VecTy
);
710 Vec
= Builder
.CreateInsertElement(Vec
, Elt1
, Builder
.getInt8(1));
711 Vec
= Builder
.CreateInsertElement(Vec
, Elt2
, 2);
712 auto X1
= Builder
.CreateExtractElement(Vec
, 1);
713 auto X2
= Builder
.CreateExtractElement(Vec
, Builder
.getInt32(2));
718 TEST_F(IRBuilderTest
, CreateGlobalStringPtr
) {
719 IRBuilder
<> Builder(BB
);
721 auto String1a
= Builder
.CreateGlobalStringPtr("TestString", "String1a");
722 auto String1b
= Builder
.CreateGlobalStringPtr("TestString", "String1b", 0);
723 auto String2
= Builder
.CreateGlobalStringPtr("TestString", "String2", 1);
724 auto String3
= Builder
.CreateGlobalString("TestString", "String3", 2);
726 EXPECT_TRUE(String1a
->getType()->getPointerAddressSpace() == 0);
727 EXPECT_TRUE(String1b
->getType()->getPointerAddressSpace() == 0);
728 EXPECT_TRUE(String2
->getType()->getPointerAddressSpace() == 1);
729 EXPECT_TRUE(String3
->getType()->getPointerAddressSpace() == 2);
732 TEST_F(IRBuilderTest
, DebugLoc
) {
733 auto CalleeTy
= FunctionType::get(Type::getVoidTy(Ctx
),
736 Function::Create(CalleeTy
, Function::ExternalLinkage
, "", M
.get());
739 auto File
= DIB
.createFile("tmp.cpp", "/");
740 auto CU
= DIB
.createCompileUnit(dwarf::DW_LANG_C_plus_plus_11
,
741 DIB
.createFile("tmp.cpp", "/"), "", true, "",
743 auto SPType
= DIB
.createSubroutineType(DIB
.getOrCreateTypeArray(None
));
745 DIB
.createFunction(CU
, "foo", "foo", File
, 1, SPType
, 1, DINode::FlagZero
,
746 DISubprogram::SPFlagDefinition
);
747 DebugLoc DL1
= DILocation::get(Ctx
, 2, 0, SP
);
748 DebugLoc DL2
= DILocation::get(Ctx
, 3, 0, SP
);
750 auto BB2
= BasicBlock::Create(Ctx
, "bb2", F
);
751 auto Br
= BranchInst::Create(BB2
, BB
);
752 Br
->setDebugLoc(DL1
);
754 IRBuilder
<> Builder(Ctx
);
755 Builder
.SetInsertPoint(Br
);
756 EXPECT_EQ(DL1
, Builder
.getCurrentDebugLocation());
757 auto Call1
= Builder
.CreateCall(Callee
, None
);
758 EXPECT_EQ(DL1
, Call1
->getDebugLoc());
760 Call1
->setDebugLoc(DL2
);
761 Builder
.SetInsertPoint(Call1
->getParent(), Call1
->getIterator());
762 EXPECT_EQ(DL2
, Builder
.getCurrentDebugLocation());
763 auto Call2
= Builder
.CreateCall(Callee
, None
);
764 EXPECT_EQ(DL2
, Call2
->getDebugLoc());
769 TEST_F(IRBuilderTest
, DIImportedEntity
) {
770 IRBuilder
<> Builder(BB
);
772 auto F
= DIB
.createFile("F.CBL", "/");
773 auto CU
= DIB
.createCompileUnit(dwarf::DW_LANG_Cobol74
,
776 DIB
.createImportedDeclaration(CU
, nullptr, F
, 1);
777 DIB
.createImportedDeclaration(CU
, nullptr, F
, 1);
778 DIB
.createImportedModule(CU
, (DIImportedEntity
*)nullptr, F
, 2);
779 DIB
.createImportedModule(CU
, (DIImportedEntity
*)nullptr, F
, 2);
781 EXPECT_TRUE(verifyModule(*M
));
782 EXPECT_TRUE(CU
->getImportedEntities().size() == 2);
785 // 0: #define M0 V0 <-- command line definition
786 // 0: main.c <-- main file
787 // 3: #define M1 V1 <-- M1 definition in main.c
788 // 5: #include "file.h" <-- inclusion of file.h from main.c
789 // 1: #define M2 <-- M2 definition in file.h with no value
790 // 7: #undef M1 V1 <-- M1 un-definition in main.c
791 TEST_F(IRBuilderTest
, DIBuilderMacro
) {
792 IRBuilder
<> Builder(BB
);
794 auto File1
= DIB
.createFile("main.c", "/");
795 auto File2
= DIB
.createFile("file.h", "/");
796 auto CU
= DIB
.createCompileUnit(
797 dwarf::DW_LANG_C
, DIB
.createFile("main.c", "/"), "llvm-c", true, "", 0);
799 DIB
.createMacro(nullptr, 0, dwarf::DW_MACINFO_define
, "M0", "V0");
800 auto TMF1
= DIB
.createTempMacroFile(nullptr, 0, File1
);
801 auto MDef1
= DIB
.createMacro(TMF1
, 3, dwarf::DW_MACINFO_define
, "M1", "V1");
802 auto TMF2
= DIB
.createTempMacroFile(TMF1
, 5, File2
);
803 auto MDef2
= DIB
.createMacro(TMF2
, 1, dwarf::DW_MACINFO_define
, "M2");
804 auto MUndef1
= DIB
.createMacro(TMF1
, 7, dwarf::DW_MACINFO_undef
, "M1");
806 EXPECT_EQ(dwarf::DW_MACINFO_define
, MDef1
->getMacinfoType());
807 EXPECT_EQ(3u, MDef1
->getLine());
808 EXPECT_EQ("M1", MDef1
->getName());
809 EXPECT_EQ("V1", MDef1
->getValue());
811 EXPECT_EQ(dwarf::DW_MACINFO_undef
, MUndef1
->getMacinfoType());
812 EXPECT_EQ(7u, MUndef1
->getLine());
813 EXPECT_EQ("M1", MUndef1
->getName());
814 EXPECT_EQ("", MUndef1
->getValue());
816 EXPECT_EQ(dwarf::DW_MACINFO_start_file
, TMF2
->getMacinfoType());
817 EXPECT_EQ(5u, TMF2
->getLine());
818 EXPECT_EQ(File2
, TMF2
->getFile());
822 SmallVector
<Metadata
*, 4> Elements
;
823 Elements
.push_back(MDef2
);
824 auto MF2
= DIMacroFile::get(Ctx
, dwarf::DW_MACINFO_start_file
, 5, File2
,
825 DIB
.getOrCreateMacroArray(Elements
));
828 Elements
.push_back(MDef1
);
829 Elements
.push_back(MF2
);
830 Elements
.push_back(MUndef1
);
831 auto MF1
= DIMacroFile::get(Ctx
, dwarf::DW_MACINFO_start_file
, 0, File1
,
832 DIB
.getOrCreateMacroArray(Elements
));
835 Elements
.push_back(MDef0
);
836 Elements
.push_back(MF1
);
837 auto MN0
= MDTuple::get(Ctx
, Elements
);
838 EXPECT_EQ(MN0
, CU
->getRawMacros());
841 Elements
.push_back(MDef1
);
842 Elements
.push_back(MF2
);
843 Elements
.push_back(MUndef1
);
844 auto MN1
= MDTuple::get(Ctx
, Elements
);
845 EXPECT_EQ(MN1
, MF1
->getRawElements());
848 Elements
.push_back(MDef2
);
849 auto MN2
= MDTuple::get(Ctx
, Elements
);
850 EXPECT_EQ(MN2
, MF2
->getRawElements());
851 EXPECT_TRUE(verifyModule(*M
));