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 // Now override the defaults.
191 Call
= Builder
.CreateConstrainedFPBinOp(
192 Intrinsic::experimental_constrained_fadd
, V
, V
, nullptr, "", nullptr,
193 ConstrainedFPIntrinsic::rmDownward
, ConstrainedFPIntrinsic::ebMayTrap
);
194 CII
= cast
<ConstrainedFPIntrinsic
>(Call
);
195 EXPECT_EQ(CII
->getIntrinsicID(), Intrinsic::experimental_constrained_fadd
);
196 ASSERT_TRUE(CII
->getExceptionBehavior() == ConstrainedFPIntrinsic::ebMayTrap
);
197 ASSERT_TRUE(CII
->getRoundingMode() == ConstrainedFPIntrinsic::rmDownward
);
199 Builder
.CreateRetVoid();
200 EXPECT_FALSE(verifyModule(*M
));
203 TEST_F(IRBuilderTest
, Lifetime
) {
204 IRBuilder
<> Builder(BB
);
205 AllocaInst
*Var1
= Builder
.CreateAlloca(Builder
.getInt8Ty());
206 AllocaInst
*Var2
= Builder
.CreateAlloca(Builder
.getInt32Ty());
207 AllocaInst
*Var3
= Builder
.CreateAlloca(Builder
.getInt8Ty(),
208 Builder
.getInt32(123));
210 CallInst
*Start1
= Builder
.CreateLifetimeStart(Var1
);
211 CallInst
*Start2
= Builder
.CreateLifetimeStart(Var2
);
212 CallInst
*Start3
= Builder
.CreateLifetimeStart(Var3
, Builder
.getInt64(100));
214 EXPECT_EQ(Start1
->getArgOperand(0), Builder
.getInt64(-1));
215 EXPECT_EQ(Start2
->getArgOperand(0), Builder
.getInt64(-1));
216 EXPECT_EQ(Start3
->getArgOperand(0), Builder
.getInt64(100));
218 EXPECT_EQ(Start1
->getArgOperand(1), Var1
);
219 EXPECT_NE(Start2
->getArgOperand(1), Var2
);
220 EXPECT_EQ(Start3
->getArgOperand(1), Var3
);
222 Value
*End1
= Builder
.CreateLifetimeEnd(Var1
);
223 Builder
.CreateLifetimeEnd(Var2
);
224 Builder
.CreateLifetimeEnd(Var3
);
226 IntrinsicInst
*II_Start1
= dyn_cast
<IntrinsicInst
>(Start1
);
227 IntrinsicInst
*II_End1
= dyn_cast
<IntrinsicInst
>(End1
);
228 ASSERT_TRUE(II_Start1
!= nullptr);
229 EXPECT_EQ(II_Start1
->getIntrinsicID(), Intrinsic::lifetime_start
);
230 ASSERT_TRUE(II_End1
!= nullptr);
231 EXPECT_EQ(II_End1
->getIntrinsicID(), Intrinsic::lifetime_end
);
234 TEST_F(IRBuilderTest
, CreateCondBr
) {
235 IRBuilder
<> Builder(BB
);
236 BasicBlock
*TBB
= BasicBlock::Create(Ctx
, "", F
);
237 BasicBlock
*FBB
= BasicBlock::Create(Ctx
, "", F
);
239 BranchInst
*BI
= Builder
.CreateCondBr(Builder
.getTrue(), TBB
, FBB
);
240 Instruction
*TI
= BB
->getTerminator();
242 EXPECT_EQ(2u, TI
->getNumSuccessors());
243 EXPECT_EQ(TBB
, TI
->getSuccessor(0));
244 EXPECT_EQ(FBB
, TI
->getSuccessor(1));
246 BI
->eraseFromParent();
247 MDNode
*Weights
= MDBuilder(Ctx
).createBranchWeights(42, 13);
248 BI
= Builder
.CreateCondBr(Builder
.getTrue(), TBB
, FBB
, Weights
);
249 TI
= BB
->getTerminator();
251 EXPECT_EQ(2u, TI
->getNumSuccessors());
252 EXPECT_EQ(TBB
, TI
->getSuccessor(0));
253 EXPECT_EQ(FBB
, TI
->getSuccessor(1));
254 EXPECT_EQ(Weights
, TI
->getMetadata(LLVMContext::MD_prof
));
257 TEST_F(IRBuilderTest
, LandingPadName
) {
258 IRBuilder
<> Builder(BB
);
259 LandingPadInst
*LP
= Builder
.CreateLandingPad(Builder
.getInt32Ty(), 0, "LP");
260 EXPECT_EQ(LP
->getName(), "LP");
263 TEST_F(IRBuilderTest
, DataLayout
) {
264 std::unique_ptr
<Module
> M(new Module("test", Ctx
));
265 M
->setDataLayout("e-n32");
266 EXPECT_TRUE(M
->getDataLayout().isLegalInteger(32));
267 M
->setDataLayout("e");
268 EXPECT_FALSE(M
->getDataLayout().isLegalInteger(32));
271 TEST_F(IRBuilderTest
, GetIntTy
) {
272 IRBuilder
<> Builder(BB
);
273 IntegerType
*Ty1
= Builder
.getInt1Ty();
274 EXPECT_EQ(Ty1
, IntegerType::get(Ctx
, 1));
276 DataLayout
* DL
= new DataLayout(M
.get());
277 IntegerType
*IntPtrTy
= Builder
.getIntPtrTy(*DL
);
278 unsigned IntPtrBitSize
= DL
->getPointerSizeInBits(0);
279 EXPECT_EQ(IntPtrTy
, IntegerType::get(Ctx
, IntPtrBitSize
));
283 TEST_F(IRBuilderTest
, UnaryOperators
) {
284 IRBuilder
<NoFolder
> Builder(BB
);
285 Value
*V
= Builder
.CreateLoad(GV
->getValueType(), GV
);
287 // Test CreateUnOp(X)
288 Value
*U
= Builder
.CreateUnOp(Instruction::FNeg
, V
);
289 ASSERT_TRUE(isa
<Instruction
>(U
));
290 ASSERT_TRUE(isa
<FPMathOperator
>(U
));
291 ASSERT_TRUE(isa
<UnaryOperator
>(U
));
292 ASSERT_FALSE(isa
<BinaryOperator
>(U
));
294 // Test CreateFNegFMF(X)
295 Instruction
*I
= cast
<Instruction
>(V
);
296 I
->setHasNoSignedZeros(true);
297 I
->setHasNoNaNs(true);
298 Value
*VFMF
= Builder
.CreateFNegFMF(V
, I
);
299 Instruction
*IFMF
= cast
<Instruction
>(VFMF
);
300 EXPECT_TRUE(IFMF
->hasNoSignedZeros());
301 EXPECT_TRUE(IFMF
->hasNoNaNs());
302 EXPECT_FALSE(IFMF
->hasAllowReassoc());
305 TEST_F(IRBuilderTest
, FastMathFlags
) {
306 IRBuilder
<> Builder(BB
);
308 Instruction
*FDiv
, *FAdd
, *FCmp
, *FCall
;
310 F
= Builder
.CreateLoad(GV
->getValueType(), GV
);
311 F
= Builder
.CreateFAdd(F
, F
);
313 EXPECT_FALSE(Builder
.getFastMathFlags().any());
314 ASSERT_TRUE(isa
<Instruction
>(F
));
315 FAdd
= cast
<Instruction
>(F
);
316 EXPECT_FALSE(FAdd
->hasNoNaNs());
319 Builder
.setFastMathFlags(FMF
);
321 // By default, no flags are set.
322 F
= Builder
.CreateFAdd(F
, F
);
323 EXPECT_FALSE(Builder
.getFastMathFlags().any());
324 ASSERT_TRUE(isa
<Instruction
>(F
));
325 FAdd
= cast
<Instruction
>(F
);
326 EXPECT_FALSE(FAdd
->hasNoNaNs());
327 EXPECT_FALSE(FAdd
->hasNoInfs());
328 EXPECT_FALSE(FAdd
->hasNoSignedZeros());
329 EXPECT_FALSE(FAdd
->hasAllowReciprocal());
330 EXPECT_FALSE(FAdd
->hasAllowContract());
331 EXPECT_FALSE(FAdd
->hasAllowReassoc());
332 EXPECT_FALSE(FAdd
->hasApproxFunc());
334 // Set all flags in the instruction.
336 EXPECT_TRUE(FAdd
->hasNoNaNs());
337 EXPECT_TRUE(FAdd
->hasNoInfs());
338 EXPECT_TRUE(FAdd
->hasNoSignedZeros());
339 EXPECT_TRUE(FAdd
->hasAllowReciprocal());
340 EXPECT_TRUE(FAdd
->hasAllowContract());
341 EXPECT_TRUE(FAdd
->hasAllowReassoc());
342 EXPECT_TRUE(FAdd
->hasApproxFunc());
344 // All flags are set in the builder.
346 Builder
.setFastMathFlags(FMF
);
348 F
= Builder
.CreateFAdd(F
, F
);
349 EXPECT_TRUE(Builder
.getFastMathFlags().any());
350 EXPECT_TRUE(Builder
.getFastMathFlags().all());
351 ASSERT_TRUE(isa
<Instruction
>(F
));
352 FAdd
= cast
<Instruction
>(F
);
353 EXPECT_TRUE(FAdd
->hasNoNaNs());
354 EXPECT_TRUE(FAdd
->isFast());
356 // Now, try it with CreateBinOp
357 F
= Builder
.CreateBinOp(Instruction::FAdd
, F
, F
);
358 EXPECT_TRUE(Builder
.getFastMathFlags().any());
359 ASSERT_TRUE(isa
<Instruction
>(F
));
360 FAdd
= cast
<Instruction
>(F
);
361 EXPECT_TRUE(FAdd
->hasNoNaNs());
362 EXPECT_TRUE(FAdd
->isFast());
364 F
= Builder
.CreateFDiv(F
, F
);
365 EXPECT_TRUE(Builder
.getFastMathFlags().all());
366 ASSERT_TRUE(isa
<Instruction
>(F
));
367 FDiv
= cast
<Instruction
>(F
);
368 EXPECT_TRUE(FDiv
->hasAllowReciprocal());
370 // Clear all FMF in the builder.
371 Builder
.clearFastMathFlags();
373 F
= Builder
.CreateFDiv(F
, F
);
374 ASSERT_TRUE(isa
<Instruction
>(F
));
375 FDiv
= cast
<Instruction
>(F
);
376 EXPECT_FALSE(FDiv
->hasAllowReciprocal());
378 // Try individual flags.
380 FMF
.setAllowReciprocal();
381 Builder
.setFastMathFlags(FMF
);
383 F
= Builder
.CreateFDiv(F
, F
);
384 EXPECT_TRUE(Builder
.getFastMathFlags().any());
385 EXPECT_TRUE(Builder
.getFastMathFlags().AllowReciprocal
);
386 ASSERT_TRUE(isa
<Instruction
>(F
));
387 FDiv
= cast
<Instruction
>(F
);
388 EXPECT_TRUE(FDiv
->hasAllowReciprocal());
390 Builder
.clearFastMathFlags();
392 FC
= Builder
.CreateFCmpOEQ(F
, F
);
393 ASSERT_TRUE(isa
<Instruction
>(FC
));
394 FCmp
= cast
<Instruction
>(FC
);
395 EXPECT_FALSE(FCmp
->hasAllowReciprocal());
398 FMF
.setAllowReciprocal();
399 Builder
.setFastMathFlags(FMF
);
401 FC
= Builder
.CreateFCmpOEQ(F
, F
);
402 EXPECT_TRUE(Builder
.getFastMathFlags().any());
403 EXPECT_TRUE(Builder
.getFastMathFlags().AllowReciprocal
);
404 ASSERT_TRUE(isa
<Instruction
>(FC
));
405 FCmp
= cast
<Instruction
>(FC
);
406 EXPECT_TRUE(FCmp
->hasAllowReciprocal());
408 Builder
.clearFastMathFlags();
411 FC
= Builder
.CreateFAdd(F
, F
);
412 ASSERT_TRUE(isa
<Instruction
>(FC
));
413 FAdd
= cast
<Instruction
>(FC
);
414 EXPECT_FALSE(FAdd
->hasAllowContract());
417 FMF
.setAllowContract(true);
418 Builder
.setFastMathFlags(FMF
);
420 FC
= Builder
.CreateFAdd(F
, F
);
421 EXPECT_TRUE(Builder
.getFastMathFlags().any());
422 EXPECT_TRUE(Builder
.getFastMathFlags().AllowContract
);
423 ASSERT_TRUE(isa
<Instruction
>(FC
));
424 FAdd
= cast
<Instruction
>(FC
);
425 EXPECT_TRUE(FAdd
->hasAllowContract());
428 Builder
.clearFastMathFlags();
429 Builder
.setFastMathFlags(FMF
);
430 // Now 'aml' and 'contract' are set.
431 F
= Builder
.CreateFMul(F
, F
);
432 FAdd
= cast
<Instruction
>(F
);
433 EXPECT_TRUE(FAdd
->hasApproxFunc());
434 EXPECT_TRUE(FAdd
->hasAllowContract());
435 EXPECT_FALSE(FAdd
->hasAllowReassoc());
437 FMF
.setAllowReassoc();
438 Builder
.clearFastMathFlags();
439 Builder
.setFastMathFlags(FMF
);
440 // Now 'aml' and 'contract' and 'reassoc' are set.
441 F
= Builder
.CreateFMul(F
, F
);
442 FAdd
= cast
<Instruction
>(F
);
443 EXPECT_TRUE(FAdd
->hasApproxFunc());
444 EXPECT_TRUE(FAdd
->hasAllowContract());
445 EXPECT_TRUE(FAdd
->hasAllowReassoc());
447 // Test a call with FMF.
448 auto CalleeTy
= FunctionType::get(Type::getFloatTy(Ctx
),
451 Function::Create(CalleeTy
, Function::ExternalLinkage
, "", M
.get());
453 FCall
= Builder
.CreateCall(Callee
, None
);
454 EXPECT_FALSE(FCall
->hasNoNaNs());
457 Function::Create(CalleeTy
, Function::ExternalLinkage
, "", M
.get());
458 FCall
= Builder
.CreateCall(V
, None
);
459 EXPECT_FALSE(FCall
->hasNoNaNs());
463 Builder
.setFastMathFlags(FMF
);
465 FCall
= Builder
.CreateCall(Callee
, None
);
466 EXPECT_TRUE(Builder
.getFastMathFlags().any());
467 EXPECT_TRUE(Builder
.getFastMathFlags().NoNaNs
);
468 EXPECT_TRUE(FCall
->hasNoNaNs());
470 FCall
= Builder
.CreateCall(V
, None
);
471 EXPECT_TRUE(Builder
.getFastMathFlags().any());
472 EXPECT_TRUE(Builder
.getFastMathFlags().NoNaNs
);
473 EXPECT_TRUE(FCall
->hasNoNaNs());
475 Builder
.clearFastMathFlags();
477 // To test a copy, make sure that a '0' and a '1' change state.
478 F
= Builder
.CreateFDiv(F
, F
);
479 ASSERT_TRUE(isa
<Instruction
>(F
));
480 FDiv
= cast
<Instruction
>(F
);
481 EXPECT_FALSE(FDiv
->getFastMathFlags().any());
482 FDiv
->setHasAllowReciprocal(true);
483 FAdd
->setHasAllowReciprocal(false);
484 FAdd
->setHasNoNaNs(true);
485 FDiv
->copyFastMathFlags(FAdd
);
486 EXPECT_TRUE(FDiv
->hasNoNaNs());
487 EXPECT_FALSE(FDiv
->hasAllowReciprocal());
491 TEST_F(IRBuilderTest
, WrapFlags
) {
492 IRBuilder
<NoFolder
> Builder(BB
);
494 // Test instructions.
495 GlobalVariable
*G
= new GlobalVariable(*M
, Builder
.getInt32Ty(), true,
496 GlobalValue::ExternalLinkage
, nullptr);
497 Value
*V
= Builder
.CreateLoad(G
->getValueType(), G
);
499 cast
<BinaryOperator
>(Builder
.CreateNSWAdd(V
, V
))->hasNoSignedWrap());
501 cast
<BinaryOperator
>(Builder
.CreateNSWMul(V
, V
))->hasNoSignedWrap());
503 cast
<BinaryOperator
>(Builder
.CreateNSWSub(V
, V
))->hasNoSignedWrap());
504 EXPECT_TRUE(cast
<BinaryOperator
>(
505 Builder
.CreateShl(V
, V
, "", /* NUW */ false, /* NSW */ true))
506 ->hasNoSignedWrap());
509 cast
<BinaryOperator
>(Builder
.CreateNUWAdd(V
, V
))->hasNoUnsignedWrap());
511 cast
<BinaryOperator
>(Builder
.CreateNUWMul(V
, V
))->hasNoUnsignedWrap());
513 cast
<BinaryOperator
>(Builder
.CreateNUWSub(V
, V
))->hasNoUnsignedWrap());
514 EXPECT_TRUE(cast
<BinaryOperator
>(
515 Builder
.CreateShl(V
, V
, "", /* NUW */ true, /* NSW */ false))
516 ->hasNoUnsignedWrap());
518 // Test operators created with constants.
519 Constant
*C
= Builder
.getInt32(42);
520 EXPECT_TRUE(cast
<OverflowingBinaryOperator
>(Builder
.CreateNSWAdd(C
, C
))
521 ->hasNoSignedWrap());
522 EXPECT_TRUE(cast
<OverflowingBinaryOperator
>(Builder
.CreateNSWSub(C
, C
))
523 ->hasNoSignedWrap());
524 EXPECT_TRUE(cast
<OverflowingBinaryOperator
>(Builder
.CreateNSWMul(C
, C
))
525 ->hasNoSignedWrap());
526 EXPECT_TRUE(cast
<OverflowingBinaryOperator
>(
527 Builder
.CreateShl(C
, C
, "", /* NUW */ false, /* NSW */ true))
528 ->hasNoSignedWrap());
530 EXPECT_TRUE(cast
<OverflowingBinaryOperator
>(Builder
.CreateNUWAdd(C
, C
))
531 ->hasNoUnsignedWrap());
532 EXPECT_TRUE(cast
<OverflowingBinaryOperator
>(Builder
.CreateNUWSub(C
, C
))
533 ->hasNoUnsignedWrap());
534 EXPECT_TRUE(cast
<OverflowingBinaryOperator
>(Builder
.CreateNUWMul(C
, C
))
535 ->hasNoUnsignedWrap());
536 EXPECT_TRUE(cast
<OverflowingBinaryOperator
>(
537 Builder
.CreateShl(C
, C
, "", /* NUW */ true, /* NSW */ false))
538 ->hasNoUnsignedWrap());
541 TEST_F(IRBuilderTest
, RAIIHelpersTest
) {
542 IRBuilder
<> Builder(BB
);
543 EXPECT_FALSE(Builder
.getFastMathFlags().allowReciprocal());
544 MDBuilder
MDB(M
->getContext());
546 MDNode
*FPMathA
= MDB
.createFPMath(0.01f
);
547 MDNode
*FPMathB
= MDB
.createFPMath(0.1f
);
549 Builder
.setDefaultFPMathTag(FPMathA
);
552 IRBuilder
<>::FastMathFlagGuard
Guard(Builder
);
554 FMF
.setAllowReciprocal();
555 Builder
.setFastMathFlags(FMF
);
556 Builder
.setDefaultFPMathTag(FPMathB
);
557 EXPECT_TRUE(Builder
.getFastMathFlags().allowReciprocal());
558 EXPECT_EQ(FPMathB
, Builder
.getDefaultFPMathTag());
561 EXPECT_FALSE(Builder
.getFastMathFlags().allowReciprocal());
562 EXPECT_EQ(FPMathA
, Builder
.getDefaultFPMathTag());
564 Value
*F
= Builder
.CreateLoad(GV
->getValueType(), GV
);
567 IRBuilder
<>::InsertPointGuard
Guard(Builder
);
568 Builder
.SetInsertPoint(cast
<Instruction
>(F
));
569 EXPECT_EQ(F
, &*Builder
.GetInsertPoint());
572 EXPECT_EQ(BB
->end(), Builder
.GetInsertPoint());
573 EXPECT_EQ(BB
, Builder
.GetInsertBlock());
576 TEST_F(IRBuilderTest
, createFunction
) {
577 IRBuilder
<> Builder(BB
);
579 auto File
= DIB
.createFile("error.swift", "/");
581 DIB
.createCompileUnit(dwarf::DW_LANG_Swift
, File
, "swiftc", true, "", 0);
582 auto Type
= DIB
.createSubroutineType(DIB
.getOrCreateTypeArray(None
));
583 auto NoErr
= DIB
.createFunction(
584 CU
, "noerr", "", File
, 1, Type
, 1, DINode::FlagZero
,
585 DISubprogram::SPFlagDefinition
| DISubprogram::SPFlagOptimized
);
586 EXPECT_TRUE(!NoErr
->getThrownTypes());
587 auto Int
= DIB
.createBasicType("Int", 64, dwarf::DW_ATE_signed
);
588 auto Error
= DIB
.getOrCreateArray({Int
});
589 auto Err
= DIB
.createFunction(
590 CU
, "err", "", File
, 1, Type
, 1, DINode::FlagZero
,
591 DISubprogram::SPFlagDefinition
| DISubprogram::SPFlagOptimized
, nullptr,
592 nullptr, Error
.get());
593 EXPECT_TRUE(Err
->getThrownTypes().get() == Error
.get());
597 TEST_F(IRBuilderTest
, DIBuilder
) {
598 IRBuilder
<> Builder(BB
);
600 auto File
= DIB
.createFile("F.CBL", "/");
601 auto CU
= DIB
.createCompileUnit(dwarf::DW_LANG_Cobol74
,
602 DIB
.createFile("F.CBL", "/"), "llvm-cobol74",
604 auto Type
= DIB
.createSubroutineType(DIB
.getOrCreateTypeArray(None
));
605 auto SP
= DIB
.createFunction(
606 CU
, "foo", "", File
, 1, Type
, 1, DINode::FlagZero
,
607 DISubprogram::SPFlagDefinition
| DISubprogram::SPFlagOptimized
);
608 F
->setSubprogram(SP
);
609 AllocaInst
*I
= Builder
.CreateAlloca(Builder
.getInt8Ty());
610 auto BarSP
= DIB
.createFunction(
611 CU
, "bar", "", File
, 1, Type
, 1, DINode::FlagZero
,
612 DISubprogram::SPFlagDefinition
| DISubprogram::SPFlagOptimized
);
613 auto BadScope
= DIB
.createLexicalBlockFile(BarSP
, File
, 0);
614 I
->setDebugLoc(DebugLoc::get(2, 0, BadScope
));
616 EXPECT_TRUE(verifyModule(*M
));
619 TEST_F(IRBuilderTest
, createArtificialSubprogram
) {
620 IRBuilder
<> Builder(BB
);
622 auto File
= DIB
.createFile("main.c", "/");
623 auto CU
= DIB
.createCompileUnit(dwarf::DW_LANG_C
, File
, "clang",
624 /*isOptimized=*/true, /*Flags=*/"",
625 /*Runtime Version=*/0);
626 auto Type
= DIB
.createSubroutineType(DIB
.getOrCreateTypeArray(None
));
627 auto SP
= DIB
.createFunction(
628 CU
, "foo", /*LinkageName=*/"", File
,
629 /*LineNo=*/1, Type
, /*ScopeLine=*/2, DINode::FlagZero
,
630 DISubprogram::SPFlagDefinition
| DISubprogram::SPFlagOptimized
);
631 EXPECT_TRUE(SP
->isDistinct());
633 F
->setSubprogram(SP
);
634 AllocaInst
*I
= Builder
.CreateAlloca(Builder
.getInt8Ty());
635 ReturnInst
*R
= Builder
.CreateRetVoid();
636 I
->setDebugLoc(DebugLoc::get(3, 2, SP
));
637 R
->setDebugLoc(DebugLoc::get(4, 2, SP
));
639 EXPECT_FALSE(verifyModule(*M
));
641 Function
*G
= Function::Create(F
->getFunctionType(),
642 Function::ExternalLinkage
, "", M
.get());
643 BasicBlock
*GBB
= BasicBlock::Create(Ctx
, "", G
);
644 Builder
.SetInsertPoint(GBB
);
645 I
->removeFromParent();
647 Builder
.CreateRetVoid();
648 EXPECT_FALSE(verifyModule(*M
));
650 DISubprogram
*GSP
= DIBuilder::createArtificialSubprogram(F
->getSubprogram());
651 EXPECT_EQ(SP
->getFile(), GSP
->getFile());
652 EXPECT_EQ(SP
->getType(), GSP
->getType());
653 EXPECT_EQ(SP
->getLine(), GSP
->getLine());
654 EXPECT_EQ(SP
->getScopeLine(), GSP
->getScopeLine());
655 EXPECT_TRUE(GSP
->isDistinct());
657 G
->setSubprogram(GSP
);
658 EXPECT_TRUE(verifyModule(*M
));
660 auto *InlinedAtNode
=
661 DILocation::getDistinct(Ctx
, GSP
->getScopeLine(), 0, GSP
);
662 DebugLoc DL
= I
->getDebugLoc();
663 DenseMap
<const MDNode
*, MDNode
*> IANodes
;
664 auto IA
= DebugLoc::appendInlinedAt(DL
, InlinedAtNode
, Ctx
, IANodes
);
665 auto NewDL
= DebugLoc::get(DL
.getLine(), DL
.getCol(), DL
.getScope(), IA
);
666 I
->setDebugLoc(NewDL
);
667 EXPECT_FALSE(verifyModule(*M
));
669 EXPECT_EQ("foo", SP
->getName());
670 EXPECT_EQ("foo", GSP
->getName());
671 EXPECT_FALSE(SP
->isArtificial());
672 EXPECT_TRUE(GSP
->isArtificial());
675 TEST_F(IRBuilderTest
, InsertExtractElement
) {
676 IRBuilder
<> Builder(BB
);
678 auto VecTy
= VectorType::get(Builder
.getInt64Ty(), 4);
679 auto Elt1
= Builder
.getInt64(-1);
680 auto Elt2
= Builder
.getInt64(-2);
681 Value
*Vec
= UndefValue::get(VecTy
);
682 Vec
= Builder
.CreateInsertElement(Vec
, Elt1
, Builder
.getInt8(1));
683 Vec
= Builder
.CreateInsertElement(Vec
, Elt2
, 2);
684 auto X1
= Builder
.CreateExtractElement(Vec
, 1);
685 auto X2
= Builder
.CreateExtractElement(Vec
, Builder
.getInt32(2));
690 TEST_F(IRBuilderTest
, CreateGlobalStringPtr
) {
691 IRBuilder
<> Builder(BB
);
693 auto String1a
= Builder
.CreateGlobalStringPtr("TestString", "String1a");
694 auto String1b
= Builder
.CreateGlobalStringPtr("TestString", "String1b", 0);
695 auto String2
= Builder
.CreateGlobalStringPtr("TestString", "String2", 1);
696 auto String3
= Builder
.CreateGlobalString("TestString", "String3", 2);
698 EXPECT_TRUE(String1a
->getType()->getPointerAddressSpace() == 0);
699 EXPECT_TRUE(String1b
->getType()->getPointerAddressSpace() == 0);
700 EXPECT_TRUE(String2
->getType()->getPointerAddressSpace() == 1);
701 EXPECT_TRUE(String3
->getType()->getPointerAddressSpace() == 2);
704 TEST_F(IRBuilderTest
, DebugLoc
) {
705 auto CalleeTy
= FunctionType::get(Type::getVoidTy(Ctx
),
708 Function::Create(CalleeTy
, Function::ExternalLinkage
, "", M
.get());
711 auto File
= DIB
.createFile("tmp.cpp", "/");
712 auto CU
= DIB
.createCompileUnit(dwarf::DW_LANG_C_plus_plus_11
,
713 DIB
.createFile("tmp.cpp", "/"), "", true, "",
715 auto SPType
= DIB
.createSubroutineType(DIB
.getOrCreateTypeArray(None
));
717 DIB
.createFunction(CU
, "foo", "foo", File
, 1, SPType
, 1, DINode::FlagZero
,
718 DISubprogram::SPFlagDefinition
);
719 DebugLoc DL1
= DILocation::get(Ctx
, 2, 0, SP
);
720 DebugLoc DL2
= DILocation::get(Ctx
, 3, 0, SP
);
722 auto BB2
= BasicBlock::Create(Ctx
, "bb2", F
);
723 auto Br
= BranchInst::Create(BB2
, BB
);
724 Br
->setDebugLoc(DL1
);
726 IRBuilder
<> Builder(Ctx
);
727 Builder
.SetInsertPoint(Br
);
728 EXPECT_EQ(DL1
, Builder
.getCurrentDebugLocation());
729 auto Call1
= Builder
.CreateCall(Callee
, None
);
730 EXPECT_EQ(DL1
, Call1
->getDebugLoc());
732 Call1
->setDebugLoc(DL2
);
733 Builder
.SetInsertPoint(Call1
->getParent(), Call1
->getIterator());
734 EXPECT_EQ(DL2
, Builder
.getCurrentDebugLocation());
735 auto Call2
= Builder
.CreateCall(Callee
, None
);
736 EXPECT_EQ(DL2
, Call2
->getDebugLoc());
741 TEST_F(IRBuilderTest
, DIImportedEntity
) {
742 IRBuilder
<> Builder(BB
);
744 auto F
= DIB
.createFile("F.CBL", "/");
745 auto CU
= DIB
.createCompileUnit(dwarf::DW_LANG_Cobol74
,
748 DIB
.createImportedDeclaration(CU
, nullptr, F
, 1);
749 DIB
.createImportedDeclaration(CU
, nullptr, F
, 1);
750 DIB
.createImportedModule(CU
, (DIImportedEntity
*)nullptr, F
, 2);
751 DIB
.createImportedModule(CU
, (DIImportedEntity
*)nullptr, F
, 2);
753 EXPECT_TRUE(verifyModule(*M
));
754 EXPECT_TRUE(CU
->getImportedEntities().size() == 2);
757 // 0: #define M0 V0 <-- command line definition
758 // 0: main.c <-- main file
759 // 3: #define M1 V1 <-- M1 definition in main.c
760 // 5: #include "file.h" <-- inclusion of file.h from main.c
761 // 1: #define M2 <-- M2 definition in file.h with no value
762 // 7: #undef M1 V1 <-- M1 un-definition in main.c
763 TEST_F(IRBuilderTest
, DIBuilderMacro
) {
764 IRBuilder
<> Builder(BB
);
766 auto File1
= DIB
.createFile("main.c", "/");
767 auto File2
= DIB
.createFile("file.h", "/");
768 auto CU
= DIB
.createCompileUnit(
769 dwarf::DW_LANG_C
, DIB
.createFile("main.c", "/"), "llvm-c", true, "", 0);
771 DIB
.createMacro(nullptr, 0, dwarf::DW_MACINFO_define
, "M0", "V0");
772 auto TMF1
= DIB
.createTempMacroFile(nullptr, 0, File1
);
773 auto MDef1
= DIB
.createMacro(TMF1
, 3, dwarf::DW_MACINFO_define
, "M1", "V1");
774 auto TMF2
= DIB
.createTempMacroFile(TMF1
, 5, File2
);
775 auto MDef2
= DIB
.createMacro(TMF2
, 1, dwarf::DW_MACINFO_define
, "M2");
776 auto MUndef1
= DIB
.createMacro(TMF1
, 7, dwarf::DW_MACINFO_undef
, "M1");
778 EXPECT_EQ(dwarf::DW_MACINFO_define
, MDef1
->getMacinfoType());
779 EXPECT_EQ(3u, MDef1
->getLine());
780 EXPECT_EQ("M1", MDef1
->getName());
781 EXPECT_EQ("V1", MDef1
->getValue());
783 EXPECT_EQ(dwarf::DW_MACINFO_undef
, MUndef1
->getMacinfoType());
784 EXPECT_EQ(7u, MUndef1
->getLine());
785 EXPECT_EQ("M1", MUndef1
->getName());
786 EXPECT_EQ("", MUndef1
->getValue());
788 EXPECT_EQ(dwarf::DW_MACINFO_start_file
, TMF2
->getMacinfoType());
789 EXPECT_EQ(5u, TMF2
->getLine());
790 EXPECT_EQ(File2
, TMF2
->getFile());
794 SmallVector
<Metadata
*, 4> Elements
;
795 Elements
.push_back(MDef2
);
796 auto MF2
= DIMacroFile::get(Ctx
, dwarf::DW_MACINFO_start_file
, 5, File2
,
797 DIB
.getOrCreateMacroArray(Elements
));
800 Elements
.push_back(MDef1
);
801 Elements
.push_back(MF2
);
802 Elements
.push_back(MUndef1
);
803 auto MF1
= DIMacroFile::get(Ctx
, dwarf::DW_MACINFO_start_file
, 0, File1
,
804 DIB
.getOrCreateMacroArray(Elements
));
807 Elements
.push_back(MDef0
);
808 Elements
.push_back(MF1
);
809 auto MN0
= MDTuple::get(Ctx
, Elements
);
810 EXPECT_EQ(MN0
, CU
->getRawMacros());
813 Elements
.push_back(MDef1
);
814 Elements
.push_back(MF2
);
815 Elements
.push_back(MUndef1
);
816 auto MN1
= MDTuple::get(Ctx
, Elements
);
817 EXPECT_EQ(MN1
, MF1
->getRawElements());
820 Elements
.push_back(MDef2
);
821 auto MN2
= MDTuple::get(Ctx
, Elements
);
822 EXPECT_EQ(MN2
, MF2
->getRawElements());
823 EXPECT_TRUE(verifyModule(*M
));