[Alignment][NFC] Use Align with TargetLowering::setMinFunctionAlignment
[llvm-core.git] / unittests / IR / IRBuilderTest.cpp
blob538c2a0dd93862d922cb39332da6d06f539ff65f
1 //===- llvm/unittest/IR/IRBuilderTest.cpp - IRBuilder tests ---------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #include "llvm/IR/IRBuilder.h"
10 #include "llvm/IR/BasicBlock.h"
11 #include "llvm/IR/DIBuilder.h"
12 #include "llvm/IR/DataLayout.h"
13 #include "llvm/IR/Function.h"
14 #include "llvm/IR/IntrinsicInst.h"
15 #include "llvm/IR/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"
22 using namespace llvm;
24 namespace {
26 class IRBuilderTest : public testing::Test {
27 protected:
28 void SetUp() override {
29 M.reset(new Module("MyModule", Ctx));
30 FunctionType *FTy = FunctionType::get(Type::getVoidTy(Ctx),
31 /*isVarArg=*/false);
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 {
39 BB = nullptr;
40 M.reset();
43 LLVMContext Ctx;
44 std::unique_ptr<Module> M;
45 Function *F;
46 BasicBlock *BB;
47 GlobalVariable *GV;
50 TEST_F(IRBuilderTest, Intrinsics) {
51 IRBuilder<> Builder(BB);
52 Value *V;
53 Instruction *I;
54 CallInst *Call;
55 IntrinsicInst *II;
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, IntrinsicsWithScalableVectors) {
126 IRBuilder<> Builder(BB);
127 CallInst *Call;
128 FunctionType *FTy;
130 // Test scalable flag isn't dropped for intrinsic that is explicitly defined
131 // with scalable vectors, e.g. LLVMType<nxv4i32>.
132 Type *SrcVecTy = VectorType::get(Builder.getHalfTy(), 8, true);
133 Type *DstVecTy = VectorType::get(Builder.getInt32Ty(), 4, true);
134 Type *PredTy = VectorType::get(Builder.getInt1Ty(), 16, true);
136 SmallVector<Value*, 3> ArgTys;
137 ArgTys.push_back(UndefValue::get(DstVecTy));
138 ArgTys.push_back(UndefValue::get(PredTy));
139 ArgTys.push_back(UndefValue::get(SrcVecTy));
141 Call = Builder.CreateIntrinsic(Intrinsic::aarch64_sve_fcvtzs_i32f16, {},
142 ArgTys, nullptr, "aarch64.sve.fcvtzs.i32f16");
143 FTy = Call->getFunctionType();
144 EXPECT_EQ(FTy->getReturnType(), DstVecTy);
145 for (unsigned i = 0; i != ArgTys.size(); ++i)
146 EXPECT_EQ(FTy->getParamType(i), ArgTys[i]->getType());
148 // Test scalable flag isn't dropped for intrinsic defined with
149 // LLVMScalarOrSameVectorWidth.
151 Type *VecTy = VectorType::get(Builder.getInt32Ty(), 4, true);
152 Type *PtrToVecTy = VecTy->getPointerTo();
153 PredTy = VectorType::get(Builder.getInt1Ty(), 4, true);
155 ArgTys.clear();
156 ArgTys.push_back(UndefValue::get(PtrToVecTy));
157 ArgTys.push_back(UndefValue::get(Builder.getInt32Ty()));
158 ArgTys.push_back(UndefValue::get(PredTy));
159 ArgTys.push_back(UndefValue::get(VecTy));
161 Call = Builder.CreateIntrinsic(Intrinsic::masked_load,
162 {VecTy, PtrToVecTy}, ArgTys,
163 nullptr, "masked.load");
164 FTy = Call->getFunctionType();
165 EXPECT_EQ(FTy->getReturnType(), VecTy);
166 for (unsigned i = 0; i != ArgTys.size(); ++i)
167 EXPECT_EQ(FTy->getParamType(i), ArgTys[i]->getType());
170 TEST_F(IRBuilderTest, ConstrainedFP) {
171 IRBuilder<> Builder(BB);
172 Value *V;
173 Value *VDouble;
174 CallInst *Call;
175 IntrinsicInst *II;
176 GlobalVariable *GVDouble = new GlobalVariable(*M, Type::getDoubleTy(Ctx),
177 true, GlobalValue::ExternalLinkage, nullptr);
179 V = Builder.CreateLoad(GV->getValueType(), GV);
180 VDouble = Builder.CreateLoad(GVDouble->getValueType(), GVDouble);
182 // See if we get constrained intrinsics instead of non-constrained
183 // instructions.
184 Builder.setIsFPConstrained(true);
186 V = Builder.CreateFAdd(V, V);
187 ASSERT_TRUE(isa<IntrinsicInst>(V));
188 II = cast<IntrinsicInst>(V);
189 EXPECT_EQ(II->getIntrinsicID(), Intrinsic::experimental_constrained_fadd);
191 V = Builder.CreateFSub(V, V);
192 ASSERT_TRUE(isa<IntrinsicInst>(V));
193 II = cast<IntrinsicInst>(V);
194 EXPECT_EQ(II->getIntrinsicID(), Intrinsic::experimental_constrained_fsub);
196 V = Builder.CreateFMul(V, V);
197 ASSERT_TRUE(isa<IntrinsicInst>(V));
198 II = cast<IntrinsicInst>(V);
199 EXPECT_EQ(II->getIntrinsicID(), Intrinsic::experimental_constrained_fmul);
201 V = Builder.CreateFDiv(V, V);
202 ASSERT_TRUE(isa<IntrinsicInst>(V));
203 II = cast<IntrinsicInst>(V);
204 EXPECT_EQ(II->getIntrinsicID(), Intrinsic::experimental_constrained_fdiv);
206 V = Builder.CreateFRem(V, V);
207 ASSERT_TRUE(isa<IntrinsicInst>(V));
208 II = cast<IntrinsicInst>(V);
209 EXPECT_EQ(II->getIntrinsicID(), Intrinsic::experimental_constrained_frem);
211 V = Builder.CreateFPTrunc(VDouble, Type::getFloatTy(Ctx));
212 ASSERT_TRUE(isa<IntrinsicInst>(V));
213 II = cast<IntrinsicInst>(V);
214 EXPECT_EQ(II->getIntrinsicID(), Intrinsic::experimental_constrained_fptrunc);
216 VDouble = Builder.CreateFPExt(V, Type::getDoubleTy(Ctx));
217 ASSERT_TRUE(isa<IntrinsicInst>(VDouble));
218 II = cast<IntrinsicInst>(VDouble);
219 EXPECT_EQ(II->getIntrinsicID(), Intrinsic::experimental_constrained_fpext);
221 // Verify the codepaths for setting and overriding the default metadata.
222 V = Builder.CreateFAdd(V, V);
223 ASSERT_TRUE(isa<ConstrainedFPIntrinsic>(V));
224 auto *CII = cast<ConstrainedFPIntrinsic>(V);
225 ASSERT_TRUE(CII->getExceptionBehavior() == ConstrainedFPIntrinsic::ebStrict);
226 ASSERT_TRUE(CII->getRoundingMode() == ConstrainedFPIntrinsic::rmDynamic);
228 Builder.setDefaultConstrainedExcept(ConstrainedFPIntrinsic::ebIgnore);
229 Builder.setDefaultConstrainedRounding(ConstrainedFPIntrinsic::rmUpward);
230 V = Builder.CreateFAdd(V, V);
231 CII = cast<ConstrainedFPIntrinsic>(V);
232 ASSERT_TRUE(CII->getExceptionBehavior() == ConstrainedFPIntrinsic::ebIgnore);
233 ASSERT_TRUE(CII->getRoundingMode() == ConstrainedFPIntrinsic::rmUpward);
235 Builder.setDefaultConstrainedExcept(ConstrainedFPIntrinsic::ebIgnore);
236 Builder.setDefaultConstrainedRounding(ConstrainedFPIntrinsic::rmToNearest);
237 V = Builder.CreateFAdd(V, V);
238 CII = cast<ConstrainedFPIntrinsic>(V);
239 ASSERT_TRUE(CII->getExceptionBehavior() == ConstrainedFPIntrinsic::ebIgnore);
240 ASSERT_TRUE(CII->getRoundingMode() == ConstrainedFPIntrinsic::rmToNearest);
242 Builder.setDefaultConstrainedExcept(ConstrainedFPIntrinsic::ebMayTrap);
243 Builder.setDefaultConstrainedRounding(ConstrainedFPIntrinsic::rmDownward);
244 V = Builder.CreateFAdd(V, V);
245 CII = cast<ConstrainedFPIntrinsic>(V);
246 ASSERT_TRUE(CII->getExceptionBehavior() == ConstrainedFPIntrinsic::ebMayTrap);
247 ASSERT_TRUE(CII->getRoundingMode() == ConstrainedFPIntrinsic::rmDownward);
249 Builder.setDefaultConstrainedExcept(ConstrainedFPIntrinsic::ebStrict);
250 Builder.setDefaultConstrainedRounding(ConstrainedFPIntrinsic::rmTowardZero);
251 V = Builder.CreateFAdd(V, V);
252 CII = cast<ConstrainedFPIntrinsic>(V);
253 ASSERT_TRUE(CII->getExceptionBehavior() == ConstrainedFPIntrinsic::ebStrict);
254 ASSERT_TRUE(CII->getRoundingMode() == ConstrainedFPIntrinsic::rmTowardZero);
256 Builder.setDefaultConstrainedExcept(ConstrainedFPIntrinsic::ebIgnore);
257 Builder.setDefaultConstrainedRounding(ConstrainedFPIntrinsic::rmDynamic);
258 V = Builder.CreateFAdd(V, V);
259 CII = cast<ConstrainedFPIntrinsic>(V);
260 ASSERT_TRUE(CII->getExceptionBehavior() == ConstrainedFPIntrinsic::ebIgnore);
261 ASSERT_TRUE(CII->getRoundingMode() == ConstrainedFPIntrinsic::rmDynamic);
263 // Now override the defaults.
264 Call = Builder.CreateConstrainedFPBinOp(
265 Intrinsic::experimental_constrained_fadd, V, V, nullptr, "", nullptr,
266 ConstrainedFPIntrinsic::rmDownward, ConstrainedFPIntrinsic::ebMayTrap);
267 CII = cast<ConstrainedFPIntrinsic>(Call);
268 EXPECT_EQ(CII->getIntrinsicID(), Intrinsic::experimental_constrained_fadd);
269 ASSERT_TRUE(CII->getExceptionBehavior() == ConstrainedFPIntrinsic::ebMayTrap);
270 ASSERT_TRUE(CII->getRoundingMode() == ConstrainedFPIntrinsic::rmDownward);
272 Builder.CreateRetVoid();
273 EXPECT_FALSE(verifyModule(*M));
276 TEST_F(IRBuilderTest, Lifetime) {
277 IRBuilder<> Builder(BB);
278 AllocaInst *Var1 = Builder.CreateAlloca(Builder.getInt8Ty());
279 AllocaInst *Var2 = Builder.CreateAlloca(Builder.getInt32Ty());
280 AllocaInst *Var3 = Builder.CreateAlloca(Builder.getInt8Ty(),
281 Builder.getInt32(123));
283 CallInst *Start1 = Builder.CreateLifetimeStart(Var1);
284 CallInst *Start2 = Builder.CreateLifetimeStart(Var2);
285 CallInst *Start3 = Builder.CreateLifetimeStart(Var3, Builder.getInt64(100));
287 EXPECT_EQ(Start1->getArgOperand(0), Builder.getInt64(-1));
288 EXPECT_EQ(Start2->getArgOperand(0), Builder.getInt64(-1));
289 EXPECT_EQ(Start3->getArgOperand(0), Builder.getInt64(100));
291 EXPECT_EQ(Start1->getArgOperand(1), Var1);
292 EXPECT_NE(Start2->getArgOperand(1), Var2);
293 EXPECT_EQ(Start3->getArgOperand(1), Var3);
295 Value *End1 = Builder.CreateLifetimeEnd(Var1);
296 Builder.CreateLifetimeEnd(Var2);
297 Builder.CreateLifetimeEnd(Var3);
299 IntrinsicInst *II_Start1 = dyn_cast<IntrinsicInst>(Start1);
300 IntrinsicInst *II_End1 = dyn_cast<IntrinsicInst>(End1);
301 ASSERT_TRUE(II_Start1 != nullptr);
302 EXPECT_EQ(II_Start1->getIntrinsicID(), Intrinsic::lifetime_start);
303 ASSERT_TRUE(II_End1 != nullptr);
304 EXPECT_EQ(II_End1->getIntrinsicID(), Intrinsic::lifetime_end);
307 TEST_F(IRBuilderTest, CreateCondBr) {
308 IRBuilder<> Builder(BB);
309 BasicBlock *TBB = BasicBlock::Create(Ctx, "", F);
310 BasicBlock *FBB = BasicBlock::Create(Ctx, "", F);
312 BranchInst *BI = Builder.CreateCondBr(Builder.getTrue(), TBB, FBB);
313 Instruction *TI = BB->getTerminator();
314 EXPECT_EQ(BI, TI);
315 EXPECT_EQ(2u, TI->getNumSuccessors());
316 EXPECT_EQ(TBB, TI->getSuccessor(0));
317 EXPECT_EQ(FBB, TI->getSuccessor(1));
319 BI->eraseFromParent();
320 MDNode *Weights = MDBuilder(Ctx).createBranchWeights(42, 13);
321 BI = Builder.CreateCondBr(Builder.getTrue(), TBB, FBB, Weights);
322 TI = BB->getTerminator();
323 EXPECT_EQ(BI, TI);
324 EXPECT_EQ(2u, TI->getNumSuccessors());
325 EXPECT_EQ(TBB, TI->getSuccessor(0));
326 EXPECT_EQ(FBB, TI->getSuccessor(1));
327 EXPECT_EQ(Weights, TI->getMetadata(LLVMContext::MD_prof));
330 TEST_F(IRBuilderTest, LandingPadName) {
331 IRBuilder<> Builder(BB);
332 LandingPadInst *LP = Builder.CreateLandingPad(Builder.getInt32Ty(), 0, "LP");
333 EXPECT_EQ(LP->getName(), "LP");
336 TEST_F(IRBuilderTest, DataLayout) {
337 std::unique_ptr<Module> M(new Module("test", Ctx));
338 M->setDataLayout("e-n32");
339 EXPECT_TRUE(M->getDataLayout().isLegalInteger(32));
340 M->setDataLayout("e");
341 EXPECT_FALSE(M->getDataLayout().isLegalInteger(32));
344 TEST_F(IRBuilderTest, GetIntTy) {
345 IRBuilder<> Builder(BB);
346 IntegerType *Ty1 = Builder.getInt1Ty();
347 EXPECT_EQ(Ty1, IntegerType::get(Ctx, 1));
349 DataLayout* DL = new DataLayout(M.get());
350 IntegerType *IntPtrTy = Builder.getIntPtrTy(*DL);
351 unsigned IntPtrBitSize = DL->getPointerSizeInBits(0);
352 EXPECT_EQ(IntPtrTy, IntegerType::get(Ctx, IntPtrBitSize));
353 delete DL;
356 TEST_F(IRBuilderTest, UnaryOperators) {
357 IRBuilder<NoFolder> Builder(BB);
358 Value *V = Builder.CreateLoad(GV->getValueType(), GV);
360 // Test CreateUnOp(X)
361 Value *U = Builder.CreateUnOp(Instruction::FNeg, V);
362 ASSERT_TRUE(isa<Instruction>(U));
363 ASSERT_TRUE(isa<FPMathOperator>(U));
364 ASSERT_TRUE(isa<UnaryOperator>(U));
365 ASSERT_FALSE(isa<BinaryOperator>(U));
367 // Test CreateFNegFMF(X)
368 Instruction *I = cast<Instruction>(V);
369 I->setHasNoSignedZeros(true);
370 I->setHasNoNaNs(true);
371 Value *VFMF = Builder.CreateFNegFMF(V, I);
372 Instruction *IFMF = cast<Instruction>(VFMF);
373 EXPECT_TRUE(IFMF->hasNoSignedZeros());
374 EXPECT_TRUE(IFMF->hasNoNaNs());
375 EXPECT_FALSE(IFMF->hasAllowReassoc());
378 TEST_F(IRBuilderTest, FastMathFlags) {
379 IRBuilder<> Builder(BB);
380 Value *F, *FC;
381 Instruction *FDiv, *FAdd, *FCmp, *FCall;
383 F = Builder.CreateLoad(GV->getValueType(), GV);
384 F = Builder.CreateFAdd(F, F);
386 EXPECT_FALSE(Builder.getFastMathFlags().any());
387 ASSERT_TRUE(isa<Instruction>(F));
388 FAdd = cast<Instruction>(F);
389 EXPECT_FALSE(FAdd->hasNoNaNs());
391 FastMathFlags FMF;
392 Builder.setFastMathFlags(FMF);
394 // By default, no flags are set.
395 F = Builder.CreateFAdd(F, F);
396 EXPECT_FALSE(Builder.getFastMathFlags().any());
397 ASSERT_TRUE(isa<Instruction>(F));
398 FAdd = cast<Instruction>(F);
399 EXPECT_FALSE(FAdd->hasNoNaNs());
400 EXPECT_FALSE(FAdd->hasNoInfs());
401 EXPECT_FALSE(FAdd->hasNoSignedZeros());
402 EXPECT_FALSE(FAdd->hasAllowReciprocal());
403 EXPECT_FALSE(FAdd->hasAllowContract());
404 EXPECT_FALSE(FAdd->hasAllowReassoc());
405 EXPECT_FALSE(FAdd->hasApproxFunc());
407 // Set all flags in the instruction.
408 FAdd->setFast(true);
409 EXPECT_TRUE(FAdd->hasNoNaNs());
410 EXPECT_TRUE(FAdd->hasNoInfs());
411 EXPECT_TRUE(FAdd->hasNoSignedZeros());
412 EXPECT_TRUE(FAdd->hasAllowReciprocal());
413 EXPECT_TRUE(FAdd->hasAllowContract());
414 EXPECT_TRUE(FAdd->hasAllowReassoc());
415 EXPECT_TRUE(FAdd->hasApproxFunc());
417 // All flags are set in the builder.
418 FMF.setFast();
419 Builder.setFastMathFlags(FMF);
421 F = Builder.CreateFAdd(F, F);
422 EXPECT_TRUE(Builder.getFastMathFlags().any());
423 EXPECT_TRUE(Builder.getFastMathFlags().all());
424 ASSERT_TRUE(isa<Instruction>(F));
425 FAdd = cast<Instruction>(F);
426 EXPECT_TRUE(FAdd->hasNoNaNs());
427 EXPECT_TRUE(FAdd->isFast());
429 // Now, try it with CreateBinOp
430 F = Builder.CreateBinOp(Instruction::FAdd, F, F);
431 EXPECT_TRUE(Builder.getFastMathFlags().any());
432 ASSERT_TRUE(isa<Instruction>(F));
433 FAdd = cast<Instruction>(F);
434 EXPECT_TRUE(FAdd->hasNoNaNs());
435 EXPECT_TRUE(FAdd->isFast());
437 F = Builder.CreateFDiv(F, F);
438 EXPECT_TRUE(Builder.getFastMathFlags().all());
439 ASSERT_TRUE(isa<Instruction>(F));
440 FDiv = cast<Instruction>(F);
441 EXPECT_TRUE(FDiv->hasAllowReciprocal());
443 // Clear all FMF in the builder.
444 Builder.clearFastMathFlags();
446 F = Builder.CreateFDiv(F, F);
447 ASSERT_TRUE(isa<Instruction>(F));
448 FDiv = cast<Instruction>(F);
449 EXPECT_FALSE(FDiv->hasAllowReciprocal());
451 // Try individual flags.
452 FMF.clear();
453 FMF.setAllowReciprocal();
454 Builder.setFastMathFlags(FMF);
456 F = Builder.CreateFDiv(F, F);
457 EXPECT_TRUE(Builder.getFastMathFlags().any());
458 EXPECT_TRUE(Builder.getFastMathFlags().AllowReciprocal);
459 ASSERT_TRUE(isa<Instruction>(F));
460 FDiv = cast<Instruction>(F);
461 EXPECT_TRUE(FDiv->hasAllowReciprocal());
463 Builder.clearFastMathFlags();
465 FC = Builder.CreateFCmpOEQ(F, F);
466 ASSERT_TRUE(isa<Instruction>(FC));
467 FCmp = cast<Instruction>(FC);
468 EXPECT_FALSE(FCmp->hasAllowReciprocal());
470 FMF.clear();
471 FMF.setAllowReciprocal();
472 Builder.setFastMathFlags(FMF);
474 FC = Builder.CreateFCmpOEQ(F, F);
475 EXPECT_TRUE(Builder.getFastMathFlags().any());
476 EXPECT_TRUE(Builder.getFastMathFlags().AllowReciprocal);
477 ASSERT_TRUE(isa<Instruction>(FC));
478 FCmp = cast<Instruction>(FC);
479 EXPECT_TRUE(FCmp->hasAllowReciprocal());
481 Builder.clearFastMathFlags();
483 // Test FP-contract
484 FC = Builder.CreateFAdd(F, F);
485 ASSERT_TRUE(isa<Instruction>(FC));
486 FAdd = cast<Instruction>(FC);
487 EXPECT_FALSE(FAdd->hasAllowContract());
489 FMF.clear();
490 FMF.setAllowContract(true);
491 Builder.setFastMathFlags(FMF);
493 FC = Builder.CreateFAdd(F, F);
494 EXPECT_TRUE(Builder.getFastMathFlags().any());
495 EXPECT_TRUE(Builder.getFastMathFlags().AllowContract);
496 ASSERT_TRUE(isa<Instruction>(FC));
497 FAdd = cast<Instruction>(FC);
498 EXPECT_TRUE(FAdd->hasAllowContract());
500 FMF.setApproxFunc();
501 Builder.clearFastMathFlags();
502 Builder.setFastMathFlags(FMF);
503 // Now 'aml' and 'contract' are set.
504 F = Builder.CreateFMul(F, F);
505 FAdd = cast<Instruction>(F);
506 EXPECT_TRUE(FAdd->hasApproxFunc());
507 EXPECT_TRUE(FAdd->hasAllowContract());
508 EXPECT_FALSE(FAdd->hasAllowReassoc());
510 FMF.setAllowReassoc();
511 Builder.clearFastMathFlags();
512 Builder.setFastMathFlags(FMF);
513 // Now 'aml' and 'contract' and 'reassoc' are set.
514 F = Builder.CreateFMul(F, F);
515 FAdd = cast<Instruction>(F);
516 EXPECT_TRUE(FAdd->hasApproxFunc());
517 EXPECT_TRUE(FAdd->hasAllowContract());
518 EXPECT_TRUE(FAdd->hasAllowReassoc());
520 // Test a call with FMF.
521 auto CalleeTy = FunctionType::get(Type::getFloatTy(Ctx),
522 /*isVarArg=*/false);
523 auto Callee =
524 Function::Create(CalleeTy, Function::ExternalLinkage, "", M.get());
526 FCall = Builder.CreateCall(Callee, None);
527 EXPECT_FALSE(FCall->hasNoNaNs());
529 Function *V =
530 Function::Create(CalleeTy, Function::ExternalLinkage, "", M.get());
531 FCall = Builder.CreateCall(V, None);
532 EXPECT_FALSE(FCall->hasNoNaNs());
534 FMF.clear();
535 FMF.setNoNaNs();
536 Builder.setFastMathFlags(FMF);
538 FCall = Builder.CreateCall(Callee, None);
539 EXPECT_TRUE(Builder.getFastMathFlags().any());
540 EXPECT_TRUE(Builder.getFastMathFlags().NoNaNs);
541 EXPECT_TRUE(FCall->hasNoNaNs());
543 FCall = Builder.CreateCall(V, None);
544 EXPECT_TRUE(Builder.getFastMathFlags().any());
545 EXPECT_TRUE(Builder.getFastMathFlags().NoNaNs);
546 EXPECT_TRUE(FCall->hasNoNaNs());
548 Builder.clearFastMathFlags();
550 // To test a copy, make sure that a '0' and a '1' change state.
551 F = Builder.CreateFDiv(F, F);
552 ASSERT_TRUE(isa<Instruction>(F));
553 FDiv = cast<Instruction>(F);
554 EXPECT_FALSE(FDiv->getFastMathFlags().any());
555 FDiv->setHasAllowReciprocal(true);
556 FAdd->setHasAllowReciprocal(false);
557 FAdd->setHasNoNaNs(true);
558 FDiv->copyFastMathFlags(FAdd);
559 EXPECT_TRUE(FDiv->hasNoNaNs());
560 EXPECT_FALSE(FDiv->hasAllowReciprocal());
564 TEST_F(IRBuilderTest, WrapFlags) {
565 IRBuilder<NoFolder> Builder(BB);
567 // Test instructions.
568 GlobalVariable *G = new GlobalVariable(*M, Builder.getInt32Ty(), true,
569 GlobalValue::ExternalLinkage, nullptr);
570 Value *V = Builder.CreateLoad(G->getValueType(), G);
571 EXPECT_TRUE(
572 cast<BinaryOperator>(Builder.CreateNSWAdd(V, V))->hasNoSignedWrap());
573 EXPECT_TRUE(
574 cast<BinaryOperator>(Builder.CreateNSWMul(V, V))->hasNoSignedWrap());
575 EXPECT_TRUE(
576 cast<BinaryOperator>(Builder.CreateNSWSub(V, V))->hasNoSignedWrap());
577 EXPECT_TRUE(cast<BinaryOperator>(
578 Builder.CreateShl(V, V, "", /* NUW */ false, /* NSW */ true))
579 ->hasNoSignedWrap());
581 EXPECT_TRUE(
582 cast<BinaryOperator>(Builder.CreateNUWAdd(V, V))->hasNoUnsignedWrap());
583 EXPECT_TRUE(
584 cast<BinaryOperator>(Builder.CreateNUWMul(V, V))->hasNoUnsignedWrap());
585 EXPECT_TRUE(
586 cast<BinaryOperator>(Builder.CreateNUWSub(V, V))->hasNoUnsignedWrap());
587 EXPECT_TRUE(cast<BinaryOperator>(
588 Builder.CreateShl(V, V, "", /* NUW */ true, /* NSW */ false))
589 ->hasNoUnsignedWrap());
591 // Test operators created with constants.
592 Constant *C = Builder.getInt32(42);
593 EXPECT_TRUE(cast<OverflowingBinaryOperator>(Builder.CreateNSWAdd(C, C))
594 ->hasNoSignedWrap());
595 EXPECT_TRUE(cast<OverflowingBinaryOperator>(Builder.CreateNSWSub(C, C))
596 ->hasNoSignedWrap());
597 EXPECT_TRUE(cast<OverflowingBinaryOperator>(Builder.CreateNSWMul(C, C))
598 ->hasNoSignedWrap());
599 EXPECT_TRUE(cast<OverflowingBinaryOperator>(
600 Builder.CreateShl(C, C, "", /* NUW */ false, /* NSW */ true))
601 ->hasNoSignedWrap());
603 EXPECT_TRUE(cast<OverflowingBinaryOperator>(Builder.CreateNUWAdd(C, C))
604 ->hasNoUnsignedWrap());
605 EXPECT_TRUE(cast<OverflowingBinaryOperator>(Builder.CreateNUWSub(C, C))
606 ->hasNoUnsignedWrap());
607 EXPECT_TRUE(cast<OverflowingBinaryOperator>(Builder.CreateNUWMul(C, C))
608 ->hasNoUnsignedWrap());
609 EXPECT_TRUE(cast<OverflowingBinaryOperator>(
610 Builder.CreateShl(C, C, "", /* NUW */ true, /* NSW */ false))
611 ->hasNoUnsignedWrap());
614 TEST_F(IRBuilderTest, RAIIHelpersTest) {
615 IRBuilder<> Builder(BB);
616 EXPECT_FALSE(Builder.getFastMathFlags().allowReciprocal());
617 MDBuilder MDB(M->getContext());
619 MDNode *FPMathA = MDB.createFPMath(0.01f);
620 MDNode *FPMathB = MDB.createFPMath(0.1f);
622 Builder.setDefaultFPMathTag(FPMathA);
625 IRBuilder<>::FastMathFlagGuard Guard(Builder);
626 FastMathFlags FMF;
627 FMF.setAllowReciprocal();
628 Builder.setFastMathFlags(FMF);
629 Builder.setDefaultFPMathTag(FPMathB);
630 EXPECT_TRUE(Builder.getFastMathFlags().allowReciprocal());
631 EXPECT_EQ(FPMathB, Builder.getDefaultFPMathTag());
634 EXPECT_FALSE(Builder.getFastMathFlags().allowReciprocal());
635 EXPECT_EQ(FPMathA, Builder.getDefaultFPMathTag());
637 Value *F = Builder.CreateLoad(GV->getValueType(), GV);
640 IRBuilder<>::InsertPointGuard Guard(Builder);
641 Builder.SetInsertPoint(cast<Instruction>(F));
642 EXPECT_EQ(F, &*Builder.GetInsertPoint());
645 EXPECT_EQ(BB->end(), Builder.GetInsertPoint());
646 EXPECT_EQ(BB, Builder.GetInsertBlock());
649 TEST_F(IRBuilderTest, createFunction) {
650 IRBuilder<> Builder(BB);
651 DIBuilder DIB(*M);
652 auto File = DIB.createFile("error.swift", "/");
653 auto CU =
654 DIB.createCompileUnit(dwarf::DW_LANG_Swift, File, "swiftc", true, "", 0);
655 auto Type = DIB.createSubroutineType(DIB.getOrCreateTypeArray(None));
656 auto NoErr = DIB.createFunction(
657 CU, "noerr", "", File, 1, Type, 1, DINode::FlagZero,
658 DISubprogram::SPFlagDefinition | DISubprogram::SPFlagOptimized);
659 EXPECT_TRUE(!NoErr->getThrownTypes());
660 auto Int = DIB.createBasicType("Int", 64, dwarf::DW_ATE_signed);
661 auto Error = DIB.getOrCreateArray({Int});
662 auto Err = DIB.createFunction(
663 CU, "err", "", File, 1, Type, 1, DINode::FlagZero,
664 DISubprogram::SPFlagDefinition | DISubprogram::SPFlagOptimized, nullptr,
665 nullptr, Error.get());
666 EXPECT_TRUE(Err->getThrownTypes().get() == Error.get());
667 DIB.finalize();
670 TEST_F(IRBuilderTest, DIBuilder) {
671 IRBuilder<> Builder(BB);
672 DIBuilder DIB(*M);
673 auto File = DIB.createFile("F.CBL", "/");
674 auto CU = DIB.createCompileUnit(dwarf::DW_LANG_Cobol74,
675 DIB.createFile("F.CBL", "/"), "llvm-cobol74",
676 true, "", 0);
677 auto Type = DIB.createSubroutineType(DIB.getOrCreateTypeArray(None));
678 auto SP = DIB.createFunction(
679 CU, "foo", "", File, 1, Type, 1, DINode::FlagZero,
680 DISubprogram::SPFlagDefinition | DISubprogram::SPFlagOptimized);
681 F->setSubprogram(SP);
682 AllocaInst *I = Builder.CreateAlloca(Builder.getInt8Ty());
683 auto BarSP = DIB.createFunction(
684 CU, "bar", "", File, 1, Type, 1, DINode::FlagZero,
685 DISubprogram::SPFlagDefinition | DISubprogram::SPFlagOptimized);
686 auto BadScope = DIB.createLexicalBlockFile(BarSP, File, 0);
687 I->setDebugLoc(DebugLoc::get(2, 0, BadScope));
688 DIB.finalize();
689 EXPECT_TRUE(verifyModule(*M));
692 TEST_F(IRBuilderTest, createArtificialSubprogram) {
693 IRBuilder<> Builder(BB);
694 DIBuilder DIB(*M);
695 auto File = DIB.createFile("main.c", "/");
696 auto CU = DIB.createCompileUnit(dwarf::DW_LANG_C, File, "clang",
697 /*isOptimized=*/true, /*Flags=*/"",
698 /*Runtime Version=*/0);
699 auto Type = DIB.createSubroutineType(DIB.getOrCreateTypeArray(None));
700 auto SP = DIB.createFunction(
701 CU, "foo", /*LinkageName=*/"", File,
702 /*LineNo=*/1, Type, /*ScopeLine=*/2, DINode::FlagZero,
703 DISubprogram::SPFlagDefinition | DISubprogram::SPFlagOptimized);
704 EXPECT_TRUE(SP->isDistinct());
706 F->setSubprogram(SP);
707 AllocaInst *I = Builder.CreateAlloca(Builder.getInt8Ty());
708 ReturnInst *R = Builder.CreateRetVoid();
709 I->setDebugLoc(DebugLoc::get(3, 2, SP));
710 R->setDebugLoc(DebugLoc::get(4, 2, SP));
711 DIB.finalize();
712 EXPECT_FALSE(verifyModule(*M));
714 Function *G = Function::Create(F->getFunctionType(),
715 Function::ExternalLinkage, "", M.get());
716 BasicBlock *GBB = BasicBlock::Create(Ctx, "", G);
717 Builder.SetInsertPoint(GBB);
718 I->removeFromParent();
719 Builder.Insert(I);
720 Builder.CreateRetVoid();
721 EXPECT_FALSE(verifyModule(*M));
723 DISubprogram *GSP = DIBuilder::createArtificialSubprogram(F->getSubprogram());
724 EXPECT_EQ(SP->getFile(), GSP->getFile());
725 EXPECT_EQ(SP->getType(), GSP->getType());
726 EXPECT_EQ(SP->getLine(), GSP->getLine());
727 EXPECT_EQ(SP->getScopeLine(), GSP->getScopeLine());
728 EXPECT_TRUE(GSP->isDistinct());
730 G->setSubprogram(GSP);
731 EXPECT_TRUE(verifyModule(*M));
733 auto *InlinedAtNode =
734 DILocation::getDistinct(Ctx, GSP->getScopeLine(), 0, GSP);
735 DebugLoc DL = I->getDebugLoc();
736 DenseMap<const MDNode *, MDNode *> IANodes;
737 auto IA = DebugLoc::appendInlinedAt(DL, InlinedAtNode, Ctx, IANodes);
738 auto NewDL = DebugLoc::get(DL.getLine(), DL.getCol(), DL.getScope(), IA);
739 I->setDebugLoc(NewDL);
740 EXPECT_FALSE(verifyModule(*M));
742 EXPECT_EQ("foo", SP->getName());
743 EXPECT_EQ("foo", GSP->getName());
744 EXPECT_FALSE(SP->isArtificial());
745 EXPECT_TRUE(GSP->isArtificial());
748 TEST_F(IRBuilderTest, InsertExtractElement) {
749 IRBuilder<> Builder(BB);
751 auto VecTy = VectorType::get(Builder.getInt64Ty(), 4);
752 auto Elt1 = Builder.getInt64(-1);
753 auto Elt2 = Builder.getInt64(-2);
754 Value *Vec = UndefValue::get(VecTy);
755 Vec = Builder.CreateInsertElement(Vec, Elt1, Builder.getInt8(1));
756 Vec = Builder.CreateInsertElement(Vec, Elt2, 2);
757 auto X1 = Builder.CreateExtractElement(Vec, 1);
758 auto X2 = Builder.CreateExtractElement(Vec, Builder.getInt32(2));
759 EXPECT_EQ(Elt1, X1);
760 EXPECT_EQ(Elt2, X2);
763 TEST_F(IRBuilderTest, CreateGlobalStringPtr) {
764 IRBuilder<> Builder(BB);
766 auto String1a = Builder.CreateGlobalStringPtr("TestString", "String1a");
767 auto String1b = Builder.CreateGlobalStringPtr("TestString", "String1b", 0);
768 auto String2 = Builder.CreateGlobalStringPtr("TestString", "String2", 1);
769 auto String3 = Builder.CreateGlobalString("TestString", "String3", 2);
771 EXPECT_TRUE(String1a->getType()->getPointerAddressSpace() == 0);
772 EXPECT_TRUE(String1b->getType()->getPointerAddressSpace() == 0);
773 EXPECT_TRUE(String2->getType()->getPointerAddressSpace() == 1);
774 EXPECT_TRUE(String3->getType()->getPointerAddressSpace() == 2);
777 TEST_F(IRBuilderTest, DebugLoc) {
778 auto CalleeTy = FunctionType::get(Type::getVoidTy(Ctx),
779 /*isVarArg=*/false);
780 auto Callee =
781 Function::Create(CalleeTy, Function::ExternalLinkage, "", M.get());
783 DIBuilder DIB(*M);
784 auto File = DIB.createFile("tmp.cpp", "/");
785 auto CU = DIB.createCompileUnit(dwarf::DW_LANG_C_plus_plus_11,
786 DIB.createFile("tmp.cpp", "/"), "", true, "",
788 auto SPType = DIB.createSubroutineType(DIB.getOrCreateTypeArray(None));
789 auto SP =
790 DIB.createFunction(CU, "foo", "foo", File, 1, SPType, 1, DINode::FlagZero,
791 DISubprogram::SPFlagDefinition);
792 DebugLoc DL1 = DILocation::get(Ctx, 2, 0, SP);
793 DebugLoc DL2 = DILocation::get(Ctx, 3, 0, SP);
795 auto BB2 = BasicBlock::Create(Ctx, "bb2", F);
796 auto Br = BranchInst::Create(BB2, BB);
797 Br->setDebugLoc(DL1);
799 IRBuilder<> Builder(Ctx);
800 Builder.SetInsertPoint(Br);
801 EXPECT_EQ(DL1, Builder.getCurrentDebugLocation());
802 auto Call1 = Builder.CreateCall(Callee, None);
803 EXPECT_EQ(DL1, Call1->getDebugLoc());
805 Call1->setDebugLoc(DL2);
806 Builder.SetInsertPoint(Call1->getParent(), Call1->getIterator());
807 EXPECT_EQ(DL2, Builder.getCurrentDebugLocation());
808 auto Call2 = Builder.CreateCall(Callee, None);
809 EXPECT_EQ(DL2, Call2->getDebugLoc());
811 DIB.finalize();
814 TEST_F(IRBuilderTest, DIImportedEntity) {
815 IRBuilder<> Builder(BB);
816 DIBuilder DIB(*M);
817 auto F = DIB.createFile("F.CBL", "/");
818 auto CU = DIB.createCompileUnit(dwarf::DW_LANG_Cobol74,
819 F, "llvm-cobol74",
820 true, "", 0);
821 DIB.createImportedDeclaration(CU, nullptr, F, 1);
822 DIB.createImportedDeclaration(CU, nullptr, F, 1);
823 DIB.createImportedModule(CU, (DIImportedEntity *)nullptr, F, 2);
824 DIB.createImportedModule(CU, (DIImportedEntity *)nullptr, F, 2);
825 DIB.finalize();
826 EXPECT_TRUE(verifyModule(*M));
827 EXPECT_TRUE(CU->getImportedEntities().size() == 2);
830 // 0: #define M0 V0 <-- command line definition
831 // 0: main.c <-- main file
832 // 3: #define M1 V1 <-- M1 definition in main.c
833 // 5: #include "file.h" <-- inclusion of file.h from main.c
834 // 1: #define M2 <-- M2 definition in file.h with no value
835 // 7: #undef M1 V1 <-- M1 un-definition in main.c
836 TEST_F(IRBuilderTest, DIBuilderMacro) {
837 IRBuilder<> Builder(BB);
838 DIBuilder DIB(*M);
839 auto File1 = DIB.createFile("main.c", "/");
840 auto File2 = DIB.createFile("file.h", "/");
841 auto CU = DIB.createCompileUnit(
842 dwarf::DW_LANG_C, DIB.createFile("main.c", "/"), "llvm-c", true, "", 0);
843 auto MDef0 =
844 DIB.createMacro(nullptr, 0, dwarf::DW_MACINFO_define, "M0", "V0");
845 auto TMF1 = DIB.createTempMacroFile(nullptr, 0, File1);
846 auto MDef1 = DIB.createMacro(TMF1, 3, dwarf::DW_MACINFO_define, "M1", "V1");
847 auto TMF2 = DIB.createTempMacroFile(TMF1, 5, File2);
848 auto MDef2 = DIB.createMacro(TMF2, 1, dwarf::DW_MACINFO_define, "M2");
849 auto MUndef1 = DIB.createMacro(TMF1, 7, dwarf::DW_MACINFO_undef, "M1");
851 EXPECT_EQ(dwarf::DW_MACINFO_define, MDef1->getMacinfoType());
852 EXPECT_EQ(3u, MDef1->getLine());
853 EXPECT_EQ("M1", MDef1->getName());
854 EXPECT_EQ("V1", MDef1->getValue());
856 EXPECT_EQ(dwarf::DW_MACINFO_undef, MUndef1->getMacinfoType());
857 EXPECT_EQ(7u, MUndef1->getLine());
858 EXPECT_EQ("M1", MUndef1->getName());
859 EXPECT_EQ("", MUndef1->getValue());
861 EXPECT_EQ(dwarf::DW_MACINFO_start_file, TMF2->getMacinfoType());
862 EXPECT_EQ(5u, TMF2->getLine());
863 EXPECT_EQ(File2, TMF2->getFile());
865 DIB.finalize();
867 SmallVector<Metadata *, 4> Elements;
868 Elements.push_back(MDef2);
869 auto MF2 = DIMacroFile::get(Ctx, dwarf::DW_MACINFO_start_file, 5, File2,
870 DIB.getOrCreateMacroArray(Elements));
872 Elements.clear();
873 Elements.push_back(MDef1);
874 Elements.push_back(MF2);
875 Elements.push_back(MUndef1);
876 auto MF1 = DIMacroFile::get(Ctx, dwarf::DW_MACINFO_start_file, 0, File1,
877 DIB.getOrCreateMacroArray(Elements));
879 Elements.clear();
880 Elements.push_back(MDef0);
881 Elements.push_back(MF1);
882 auto MN0 = MDTuple::get(Ctx, Elements);
883 EXPECT_EQ(MN0, CU->getRawMacros());
885 Elements.clear();
886 Elements.push_back(MDef1);
887 Elements.push_back(MF2);
888 Elements.push_back(MUndef1);
889 auto MN1 = MDTuple::get(Ctx, Elements);
890 EXPECT_EQ(MN1, MF1->getRawElements());
892 Elements.clear();
893 Elements.push_back(MDef2);
894 auto MN2 = MDTuple::get(Ctx, Elements);
895 EXPECT_EQ(MN2, MF2->getRawElements());
896 EXPECT_TRUE(verifyModule(*M));