[ARM] Selection for MVE VMOVN
[llvm-core.git] / unittests / IR / IRBuilderTest.cpp
blob8fb5337a2912204efcc7f2918b74be2dd025a8ef
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 Value *VInt;
175 CallInst *Call;
176 IntrinsicInst *II;
177 GlobalVariable *GVDouble = new GlobalVariable(*M, Type::getDoubleTy(Ctx),
178 true, GlobalValue::ExternalLinkage, nullptr);
180 V = Builder.CreateLoad(GV->getValueType(), GV);
181 VDouble = Builder.CreateLoad(GVDouble->getValueType(), GVDouble);
183 // See if we get constrained intrinsics instead of non-constrained
184 // instructions.
185 Builder.setIsFPConstrained(true);
187 V = Builder.CreateFAdd(V, V);
188 ASSERT_TRUE(isa<IntrinsicInst>(V));
189 II = cast<IntrinsicInst>(V);
190 EXPECT_EQ(II->getIntrinsicID(), Intrinsic::experimental_constrained_fadd);
192 V = Builder.CreateFSub(V, V);
193 ASSERT_TRUE(isa<IntrinsicInst>(V));
194 II = cast<IntrinsicInst>(V);
195 EXPECT_EQ(II->getIntrinsicID(), Intrinsic::experimental_constrained_fsub);
197 V = Builder.CreateFMul(V, V);
198 ASSERT_TRUE(isa<IntrinsicInst>(V));
199 II = cast<IntrinsicInst>(V);
200 EXPECT_EQ(II->getIntrinsicID(), Intrinsic::experimental_constrained_fmul);
202 V = Builder.CreateFDiv(V, V);
203 ASSERT_TRUE(isa<IntrinsicInst>(V));
204 II = cast<IntrinsicInst>(V);
205 EXPECT_EQ(II->getIntrinsicID(), Intrinsic::experimental_constrained_fdiv);
207 V = Builder.CreateFRem(V, V);
208 ASSERT_TRUE(isa<IntrinsicInst>(V));
209 II = cast<IntrinsicInst>(V);
210 EXPECT_EQ(II->getIntrinsicID(), Intrinsic::experimental_constrained_frem);
212 VInt = Builder.CreateFPToUI(VDouble, Builder.getInt32Ty());
213 ASSERT_TRUE(isa<IntrinsicInst>(VInt));
214 II = cast<IntrinsicInst>(VInt);
215 EXPECT_EQ(II->getIntrinsicID(), Intrinsic::experimental_constrained_fptoui);
217 VInt = Builder.CreateFPToSI(VDouble, Builder.getInt32Ty());
218 ASSERT_TRUE(isa<IntrinsicInst>(VInt));
219 II = cast<IntrinsicInst>(VInt);
220 EXPECT_EQ(II->getIntrinsicID(), Intrinsic::experimental_constrained_fptosi);
222 V = Builder.CreateFPTrunc(VDouble, Type::getFloatTy(Ctx));
223 ASSERT_TRUE(isa<IntrinsicInst>(V));
224 II = cast<IntrinsicInst>(V);
225 EXPECT_EQ(II->getIntrinsicID(), Intrinsic::experimental_constrained_fptrunc);
227 VDouble = Builder.CreateFPExt(V, Type::getDoubleTy(Ctx));
228 ASSERT_TRUE(isa<IntrinsicInst>(VDouble));
229 II = cast<IntrinsicInst>(VDouble);
230 EXPECT_EQ(II->getIntrinsicID(), Intrinsic::experimental_constrained_fpext);
232 // Verify the codepaths for setting and overriding the default metadata.
233 V = Builder.CreateFAdd(V, V);
234 ASSERT_TRUE(isa<ConstrainedFPIntrinsic>(V));
235 auto *CII = cast<ConstrainedFPIntrinsic>(V);
236 ASSERT_TRUE(CII->getExceptionBehavior() == ConstrainedFPIntrinsic::ebStrict);
237 ASSERT_TRUE(CII->getRoundingMode() == ConstrainedFPIntrinsic::rmDynamic);
239 Builder.setDefaultConstrainedExcept(ConstrainedFPIntrinsic::ebIgnore);
240 Builder.setDefaultConstrainedRounding(ConstrainedFPIntrinsic::rmUpward);
241 V = Builder.CreateFAdd(V, V);
242 CII = cast<ConstrainedFPIntrinsic>(V);
243 ASSERT_TRUE(CII->getExceptionBehavior() == ConstrainedFPIntrinsic::ebIgnore);
244 ASSERT_TRUE(CII->getRoundingMode() == ConstrainedFPIntrinsic::rmUpward);
246 Builder.setDefaultConstrainedExcept(ConstrainedFPIntrinsic::ebIgnore);
247 Builder.setDefaultConstrainedRounding(ConstrainedFPIntrinsic::rmToNearest);
248 V = Builder.CreateFAdd(V, V);
249 CII = cast<ConstrainedFPIntrinsic>(V);
250 ASSERT_TRUE(CII->getExceptionBehavior() == ConstrainedFPIntrinsic::ebIgnore);
251 ASSERT_TRUE(CII->getRoundingMode() == ConstrainedFPIntrinsic::rmToNearest);
253 Builder.setDefaultConstrainedExcept(ConstrainedFPIntrinsic::ebMayTrap);
254 Builder.setDefaultConstrainedRounding(ConstrainedFPIntrinsic::rmDownward);
255 V = Builder.CreateFAdd(V, V);
256 CII = cast<ConstrainedFPIntrinsic>(V);
257 ASSERT_TRUE(CII->getExceptionBehavior() == ConstrainedFPIntrinsic::ebMayTrap);
258 ASSERT_TRUE(CII->getRoundingMode() == ConstrainedFPIntrinsic::rmDownward);
260 Builder.setDefaultConstrainedExcept(ConstrainedFPIntrinsic::ebStrict);
261 Builder.setDefaultConstrainedRounding(ConstrainedFPIntrinsic::rmTowardZero);
262 V = Builder.CreateFAdd(V, V);
263 CII = cast<ConstrainedFPIntrinsic>(V);
264 ASSERT_TRUE(CII->getExceptionBehavior() == ConstrainedFPIntrinsic::ebStrict);
265 ASSERT_TRUE(CII->getRoundingMode() == ConstrainedFPIntrinsic::rmTowardZero);
267 Builder.setDefaultConstrainedExcept(ConstrainedFPIntrinsic::ebIgnore);
268 Builder.setDefaultConstrainedRounding(ConstrainedFPIntrinsic::rmDynamic);
269 V = Builder.CreateFAdd(V, V);
270 CII = cast<ConstrainedFPIntrinsic>(V);
271 ASSERT_TRUE(CII->getExceptionBehavior() == ConstrainedFPIntrinsic::ebIgnore);
272 ASSERT_TRUE(CII->getRoundingMode() == ConstrainedFPIntrinsic::rmDynamic);
274 // Now override the defaults.
275 Call = Builder.CreateConstrainedFPBinOp(
276 Intrinsic::experimental_constrained_fadd, V, V, nullptr, "", nullptr,
277 ConstrainedFPIntrinsic::rmDownward, ConstrainedFPIntrinsic::ebMayTrap);
278 CII = cast<ConstrainedFPIntrinsic>(Call);
279 EXPECT_EQ(CII->getIntrinsicID(), Intrinsic::experimental_constrained_fadd);
280 ASSERT_TRUE(CII->getExceptionBehavior() == ConstrainedFPIntrinsic::ebMayTrap);
281 ASSERT_TRUE(CII->getRoundingMode() == ConstrainedFPIntrinsic::rmDownward);
283 Builder.CreateRetVoid();
284 EXPECT_FALSE(verifyModule(*M));
287 TEST_F(IRBuilderTest, Lifetime) {
288 IRBuilder<> Builder(BB);
289 AllocaInst *Var1 = Builder.CreateAlloca(Builder.getInt8Ty());
290 AllocaInst *Var2 = Builder.CreateAlloca(Builder.getInt32Ty());
291 AllocaInst *Var3 = Builder.CreateAlloca(Builder.getInt8Ty(),
292 Builder.getInt32(123));
294 CallInst *Start1 = Builder.CreateLifetimeStart(Var1);
295 CallInst *Start2 = Builder.CreateLifetimeStart(Var2);
296 CallInst *Start3 = Builder.CreateLifetimeStart(Var3, Builder.getInt64(100));
298 EXPECT_EQ(Start1->getArgOperand(0), Builder.getInt64(-1));
299 EXPECT_EQ(Start2->getArgOperand(0), Builder.getInt64(-1));
300 EXPECT_EQ(Start3->getArgOperand(0), Builder.getInt64(100));
302 EXPECT_EQ(Start1->getArgOperand(1), Var1);
303 EXPECT_NE(Start2->getArgOperand(1), Var2);
304 EXPECT_EQ(Start3->getArgOperand(1), Var3);
306 Value *End1 = Builder.CreateLifetimeEnd(Var1);
307 Builder.CreateLifetimeEnd(Var2);
308 Builder.CreateLifetimeEnd(Var3);
310 IntrinsicInst *II_Start1 = dyn_cast<IntrinsicInst>(Start1);
311 IntrinsicInst *II_End1 = dyn_cast<IntrinsicInst>(End1);
312 ASSERT_TRUE(II_Start1 != nullptr);
313 EXPECT_EQ(II_Start1->getIntrinsicID(), Intrinsic::lifetime_start);
314 ASSERT_TRUE(II_End1 != nullptr);
315 EXPECT_EQ(II_End1->getIntrinsicID(), Intrinsic::lifetime_end);
318 TEST_F(IRBuilderTest, CreateCondBr) {
319 IRBuilder<> Builder(BB);
320 BasicBlock *TBB = BasicBlock::Create(Ctx, "", F);
321 BasicBlock *FBB = BasicBlock::Create(Ctx, "", F);
323 BranchInst *BI = Builder.CreateCondBr(Builder.getTrue(), TBB, FBB);
324 Instruction *TI = BB->getTerminator();
325 EXPECT_EQ(BI, TI);
326 EXPECT_EQ(2u, TI->getNumSuccessors());
327 EXPECT_EQ(TBB, TI->getSuccessor(0));
328 EXPECT_EQ(FBB, TI->getSuccessor(1));
330 BI->eraseFromParent();
331 MDNode *Weights = MDBuilder(Ctx).createBranchWeights(42, 13);
332 BI = Builder.CreateCondBr(Builder.getTrue(), TBB, FBB, Weights);
333 TI = BB->getTerminator();
334 EXPECT_EQ(BI, TI);
335 EXPECT_EQ(2u, TI->getNumSuccessors());
336 EXPECT_EQ(TBB, TI->getSuccessor(0));
337 EXPECT_EQ(FBB, TI->getSuccessor(1));
338 EXPECT_EQ(Weights, TI->getMetadata(LLVMContext::MD_prof));
341 TEST_F(IRBuilderTest, LandingPadName) {
342 IRBuilder<> Builder(BB);
343 LandingPadInst *LP = Builder.CreateLandingPad(Builder.getInt32Ty(), 0, "LP");
344 EXPECT_EQ(LP->getName(), "LP");
347 TEST_F(IRBuilderTest, DataLayout) {
348 std::unique_ptr<Module> M(new Module("test", Ctx));
349 M->setDataLayout("e-n32");
350 EXPECT_TRUE(M->getDataLayout().isLegalInteger(32));
351 M->setDataLayout("e");
352 EXPECT_FALSE(M->getDataLayout().isLegalInteger(32));
355 TEST_F(IRBuilderTest, GetIntTy) {
356 IRBuilder<> Builder(BB);
357 IntegerType *Ty1 = Builder.getInt1Ty();
358 EXPECT_EQ(Ty1, IntegerType::get(Ctx, 1));
360 DataLayout* DL = new DataLayout(M.get());
361 IntegerType *IntPtrTy = Builder.getIntPtrTy(*DL);
362 unsigned IntPtrBitSize = DL->getPointerSizeInBits(0);
363 EXPECT_EQ(IntPtrTy, IntegerType::get(Ctx, IntPtrBitSize));
364 delete DL;
367 TEST_F(IRBuilderTest, UnaryOperators) {
368 IRBuilder<NoFolder> Builder(BB);
369 Value *V = Builder.CreateLoad(GV->getValueType(), GV);
371 // Test CreateUnOp(X)
372 Value *U = Builder.CreateUnOp(Instruction::FNeg, V);
373 ASSERT_TRUE(isa<Instruction>(U));
374 ASSERT_TRUE(isa<FPMathOperator>(U));
375 ASSERT_TRUE(isa<UnaryOperator>(U));
376 ASSERT_FALSE(isa<BinaryOperator>(U));
378 // Test CreateFNegFMF(X)
379 Instruction *I = cast<Instruction>(V);
380 I->setHasNoSignedZeros(true);
381 I->setHasNoNaNs(true);
382 Value *VFMF = Builder.CreateFNegFMF(V, I);
383 Instruction *IFMF = cast<Instruction>(VFMF);
384 EXPECT_TRUE(IFMF->hasNoSignedZeros());
385 EXPECT_TRUE(IFMF->hasNoNaNs());
386 EXPECT_FALSE(IFMF->hasAllowReassoc());
389 TEST_F(IRBuilderTest, FastMathFlags) {
390 IRBuilder<> Builder(BB);
391 Value *F, *FC;
392 Instruction *FDiv, *FAdd, *FCmp, *FCall;
394 F = Builder.CreateLoad(GV->getValueType(), GV);
395 F = Builder.CreateFAdd(F, F);
397 EXPECT_FALSE(Builder.getFastMathFlags().any());
398 ASSERT_TRUE(isa<Instruction>(F));
399 FAdd = cast<Instruction>(F);
400 EXPECT_FALSE(FAdd->hasNoNaNs());
402 FastMathFlags FMF;
403 Builder.setFastMathFlags(FMF);
405 // By default, no flags are set.
406 F = Builder.CreateFAdd(F, F);
407 EXPECT_FALSE(Builder.getFastMathFlags().any());
408 ASSERT_TRUE(isa<Instruction>(F));
409 FAdd = cast<Instruction>(F);
410 EXPECT_FALSE(FAdd->hasNoNaNs());
411 EXPECT_FALSE(FAdd->hasNoInfs());
412 EXPECT_FALSE(FAdd->hasNoSignedZeros());
413 EXPECT_FALSE(FAdd->hasAllowReciprocal());
414 EXPECT_FALSE(FAdd->hasAllowContract());
415 EXPECT_FALSE(FAdd->hasAllowReassoc());
416 EXPECT_FALSE(FAdd->hasApproxFunc());
418 // Set all flags in the instruction.
419 FAdd->setFast(true);
420 EXPECT_TRUE(FAdd->hasNoNaNs());
421 EXPECT_TRUE(FAdd->hasNoInfs());
422 EXPECT_TRUE(FAdd->hasNoSignedZeros());
423 EXPECT_TRUE(FAdd->hasAllowReciprocal());
424 EXPECT_TRUE(FAdd->hasAllowContract());
425 EXPECT_TRUE(FAdd->hasAllowReassoc());
426 EXPECT_TRUE(FAdd->hasApproxFunc());
428 // All flags are set in the builder.
429 FMF.setFast();
430 Builder.setFastMathFlags(FMF);
432 F = Builder.CreateFAdd(F, F);
433 EXPECT_TRUE(Builder.getFastMathFlags().any());
434 EXPECT_TRUE(Builder.getFastMathFlags().all());
435 ASSERT_TRUE(isa<Instruction>(F));
436 FAdd = cast<Instruction>(F);
437 EXPECT_TRUE(FAdd->hasNoNaNs());
438 EXPECT_TRUE(FAdd->isFast());
440 // Now, try it with CreateBinOp
441 F = Builder.CreateBinOp(Instruction::FAdd, F, F);
442 EXPECT_TRUE(Builder.getFastMathFlags().any());
443 ASSERT_TRUE(isa<Instruction>(F));
444 FAdd = cast<Instruction>(F);
445 EXPECT_TRUE(FAdd->hasNoNaNs());
446 EXPECT_TRUE(FAdd->isFast());
448 F = Builder.CreateFDiv(F, F);
449 EXPECT_TRUE(Builder.getFastMathFlags().all());
450 ASSERT_TRUE(isa<Instruction>(F));
451 FDiv = cast<Instruction>(F);
452 EXPECT_TRUE(FDiv->hasAllowReciprocal());
454 // Clear all FMF in the builder.
455 Builder.clearFastMathFlags();
457 F = Builder.CreateFDiv(F, F);
458 ASSERT_TRUE(isa<Instruction>(F));
459 FDiv = cast<Instruction>(F);
460 EXPECT_FALSE(FDiv->hasAllowReciprocal());
462 // Try individual flags.
463 FMF.clear();
464 FMF.setAllowReciprocal();
465 Builder.setFastMathFlags(FMF);
467 F = Builder.CreateFDiv(F, F);
468 EXPECT_TRUE(Builder.getFastMathFlags().any());
469 EXPECT_TRUE(Builder.getFastMathFlags().AllowReciprocal);
470 ASSERT_TRUE(isa<Instruction>(F));
471 FDiv = cast<Instruction>(F);
472 EXPECT_TRUE(FDiv->hasAllowReciprocal());
474 Builder.clearFastMathFlags();
476 FC = Builder.CreateFCmpOEQ(F, F);
477 ASSERT_TRUE(isa<Instruction>(FC));
478 FCmp = cast<Instruction>(FC);
479 EXPECT_FALSE(FCmp->hasAllowReciprocal());
481 FMF.clear();
482 FMF.setAllowReciprocal();
483 Builder.setFastMathFlags(FMF);
485 FC = Builder.CreateFCmpOEQ(F, F);
486 EXPECT_TRUE(Builder.getFastMathFlags().any());
487 EXPECT_TRUE(Builder.getFastMathFlags().AllowReciprocal);
488 ASSERT_TRUE(isa<Instruction>(FC));
489 FCmp = cast<Instruction>(FC);
490 EXPECT_TRUE(FCmp->hasAllowReciprocal());
492 Builder.clearFastMathFlags();
494 // Test FP-contract
495 FC = Builder.CreateFAdd(F, F);
496 ASSERT_TRUE(isa<Instruction>(FC));
497 FAdd = cast<Instruction>(FC);
498 EXPECT_FALSE(FAdd->hasAllowContract());
500 FMF.clear();
501 FMF.setAllowContract(true);
502 Builder.setFastMathFlags(FMF);
504 FC = Builder.CreateFAdd(F, F);
505 EXPECT_TRUE(Builder.getFastMathFlags().any());
506 EXPECT_TRUE(Builder.getFastMathFlags().AllowContract);
507 ASSERT_TRUE(isa<Instruction>(FC));
508 FAdd = cast<Instruction>(FC);
509 EXPECT_TRUE(FAdd->hasAllowContract());
511 FMF.setApproxFunc();
512 Builder.clearFastMathFlags();
513 Builder.setFastMathFlags(FMF);
514 // Now 'aml' and 'contract' are set.
515 F = Builder.CreateFMul(F, F);
516 FAdd = cast<Instruction>(F);
517 EXPECT_TRUE(FAdd->hasApproxFunc());
518 EXPECT_TRUE(FAdd->hasAllowContract());
519 EXPECT_FALSE(FAdd->hasAllowReassoc());
521 FMF.setAllowReassoc();
522 Builder.clearFastMathFlags();
523 Builder.setFastMathFlags(FMF);
524 // Now 'aml' and 'contract' and 'reassoc' are set.
525 F = Builder.CreateFMul(F, F);
526 FAdd = cast<Instruction>(F);
527 EXPECT_TRUE(FAdd->hasApproxFunc());
528 EXPECT_TRUE(FAdd->hasAllowContract());
529 EXPECT_TRUE(FAdd->hasAllowReassoc());
531 // Test a call with FMF.
532 auto CalleeTy = FunctionType::get(Type::getFloatTy(Ctx),
533 /*isVarArg=*/false);
534 auto Callee =
535 Function::Create(CalleeTy, Function::ExternalLinkage, "", M.get());
537 FCall = Builder.CreateCall(Callee, None);
538 EXPECT_FALSE(FCall->hasNoNaNs());
540 Function *V =
541 Function::Create(CalleeTy, Function::ExternalLinkage, "", M.get());
542 FCall = Builder.CreateCall(V, None);
543 EXPECT_FALSE(FCall->hasNoNaNs());
545 FMF.clear();
546 FMF.setNoNaNs();
547 Builder.setFastMathFlags(FMF);
549 FCall = Builder.CreateCall(Callee, None);
550 EXPECT_TRUE(Builder.getFastMathFlags().any());
551 EXPECT_TRUE(Builder.getFastMathFlags().NoNaNs);
552 EXPECT_TRUE(FCall->hasNoNaNs());
554 FCall = Builder.CreateCall(V, None);
555 EXPECT_TRUE(Builder.getFastMathFlags().any());
556 EXPECT_TRUE(Builder.getFastMathFlags().NoNaNs);
557 EXPECT_TRUE(FCall->hasNoNaNs());
559 Builder.clearFastMathFlags();
561 // To test a copy, make sure that a '0' and a '1' change state.
562 F = Builder.CreateFDiv(F, F);
563 ASSERT_TRUE(isa<Instruction>(F));
564 FDiv = cast<Instruction>(F);
565 EXPECT_FALSE(FDiv->getFastMathFlags().any());
566 FDiv->setHasAllowReciprocal(true);
567 FAdd->setHasAllowReciprocal(false);
568 FAdd->setHasNoNaNs(true);
569 FDiv->copyFastMathFlags(FAdd);
570 EXPECT_TRUE(FDiv->hasNoNaNs());
571 EXPECT_FALSE(FDiv->hasAllowReciprocal());
575 TEST_F(IRBuilderTest, WrapFlags) {
576 IRBuilder<NoFolder> Builder(BB);
578 // Test instructions.
579 GlobalVariable *G = new GlobalVariable(*M, Builder.getInt32Ty(), true,
580 GlobalValue::ExternalLinkage, nullptr);
581 Value *V = Builder.CreateLoad(G->getValueType(), G);
582 EXPECT_TRUE(
583 cast<BinaryOperator>(Builder.CreateNSWAdd(V, V))->hasNoSignedWrap());
584 EXPECT_TRUE(
585 cast<BinaryOperator>(Builder.CreateNSWMul(V, V))->hasNoSignedWrap());
586 EXPECT_TRUE(
587 cast<BinaryOperator>(Builder.CreateNSWSub(V, V))->hasNoSignedWrap());
588 EXPECT_TRUE(cast<BinaryOperator>(
589 Builder.CreateShl(V, V, "", /* NUW */ false, /* NSW */ true))
590 ->hasNoSignedWrap());
592 EXPECT_TRUE(
593 cast<BinaryOperator>(Builder.CreateNUWAdd(V, V))->hasNoUnsignedWrap());
594 EXPECT_TRUE(
595 cast<BinaryOperator>(Builder.CreateNUWMul(V, V))->hasNoUnsignedWrap());
596 EXPECT_TRUE(
597 cast<BinaryOperator>(Builder.CreateNUWSub(V, V))->hasNoUnsignedWrap());
598 EXPECT_TRUE(cast<BinaryOperator>(
599 Builder.CreateShl(V, V, "", /* NUW */ true, /* NSW */ false))
600 ->hasNoUnsignedWrap());
602 // Test operators created with constants.
603 Constant *C = Builder.getInt32(42);
604 EXPECT_TRUE(cast<OverflowingBinaryOperator>(Builder.CreateNSWAdd(C, C))
605 ->hasNoSignedWrap());
606 EXPECT_TRUE(cast<OverflowingBinaryOperator>(Builder.CreateNSWSub(C, C))
607 ->hasNoSignedWrap());
608 EXPECT_TRUE(cast<OverflowingBinaryOperator>(Builder.CreateNSWMul(C, C))
609 ->hasNoSignedWrap());
610 EXPECT_TRUE(cast<OverflowingBinaryOperator>(
611 Builder.CreateShl(C, C, "", /* NUW */ false, /* NSW */ true))
612 ->hasNoSignedWrap());
614 EXPECT_TRUE(cast<OverflowingBinaryOperator>(Builder.CreateNUWAdd(C, C))
615 ->hasNoUnsignedWrap());
616 EXPECT_TRUE(cast<OverflowingBinaryOperator>(Builder.CreateNUWSub(C, C))
617 ->hasNoUnsignedWrap());
618 EXPECT_TRUE(cast<OverflowingBinaryOperator>(Builder.CreateNUWMul(C, C))
619 ->hasNoUnsignedWrap());
620 EXPECT_TRUE(cast<OverflowingBinaryOperator>(
621 Builder.CreateShl(C, C, "", /* NUW */ true, /* NSW */ false))
622 ->hasNoUnsignedWrap());
625 TEST_F(IRBuilderTest, RAIIHelpersTest) {
626 IRBuilder<> Builder(BB);
627 EXPECT_FALSE(Builder.getFastMathFlags().allowReciprocal());
628 MDBuilder MDB(M->getContext());
630 MDNode *FPMathA = MDB.createFPMath(0.01f);
631 MDNode *FPMathB = MDB.createFPMath(0.1f);
633 Builder.setDefaultFPMathTag(FPMathA);
636 IRBuilder<>::FastMathFlagGuard Guard(Builder);
637 FastMathFlags FMF;
638 FMF.setAllowReciprocal();
639 Builder.setFastMathFlags(FMF);
640 Builder.setDefaultFPMathTag(FPMathB);
641 EXPECT_TRUE(Builder.getFastMathFlags().allowReciprocal());
642 EXPECT_EQ(FPMathB, Builder.getDefaultFPMathTag());
645 EXPECT_FALSE(Builder.getFastMathFlags().allowReciprocal());
646 EXPECT_EQ(FPMathA, Builder.getDefaultFPMathTag());
648 Value *F = Builder.CreateLoad(GV->getValueType(), GV);
651 IRBuilder<>::InsertPointGuard Guard(Builder);
652 Builder.SetInsertPoint(cast<Instruction>(F));
653 EXPECT_EQ(F, &*Builder.GetInsertPoint());
656 EXPECT_EQ(BB->end(), Builder.GetInsertPoint());
657 EXPECT_EQ(BB, Builder.GetInsertBlock());
660 TEST_F(IRBuilderTest, createFunction) {
661 IRBuilder<> Builder(BB);
662 DIBuilder DIB(*M);
663 auto File = DIB.createFile("error.swift", "/");
664 auto CU =
665 DIB.createCompileUnit(dwarf::DW_LANG_Swift, File, "swiftc", true, "", 0);
666 auto Type = DIB.createSubroutineType(DIB.getOrCreateTypeArray(None));
667 auto NoErr = DIB.createFunction(
668 CU, "noerr", "", File, 1, Type, 1, DINode::FlagZero,
669 DISubprogram::SPFlagDefinition | DISubprogram::SPFlagOptimized);
670 EXPECT_TRUE(!NoErr->getThrownTypes());
671 auto Int = DIB.createBasicType("Int", 64, dwarf::DW_ATE_signed);
672 auto Error = DIB.getOrCreateArray({Int});
673 auto Err = DIB.createFunction(
674 CU, "err", "", File, 1, Type, 1, DINode::FlagZero,
675 DISubprogram::SPFlagDefinition | DISubprogram::SPFlagOptimized, nullptr,
676 nullptr, Error.get());
677 EXPECT_TRUE(Err->getThrownTypes().get() == Error.get());
678 DIB.finalize();
681 TEST_F(IRBuilderTest, DIBuilder) {
682 IRBuilder<> Builder(BB);
683 DIBuilder DIB(*M);
684 auto File = DIB.createFile("F.CBL", "/");
685 auto CU = DIB.createCompileUnit(dwarf::DW_LANG_Cobol74,
686 DIB.createFile("F.CBL", "/"), "llvm-cobol74",
687 true, "", 0);
688 auto Type = DIB.createSubroutineType(DIB.getOrCreateTypeArray(None));
689 auto SP = DIB.createFunction(
690 CU, "foo", "", File, 1, Type, 1, DINode::FlagZero,
691 DISubprogram::SPFlagDefinition | DISubprogram::SPFlagOptimized);
692 F->setSubprogram(SP);
693 AllocaInst *I = Builder.CreateAlloca(Builder.getInt8Ty());
694 auto BarSP = DIB.createFunction(
695 CU, "bar", "", File, 1, Type, 1, DINode::FlagZero,
696 DISubprogram::SPFlagDefinition | DISubprogram::SPFlagOptimized);
697 auto BadScope = DIB.createLexicalBlockFile(BarSP, File, 0);
698 I->setDebugLoc(DebugLoc::get(2, 0, BadScope));
699 DIB.finalize();
700 EXPECT_TRUE(verifyModule(*M));
703 TEST_F(IRBuilderTest, createArtificialSubprogram) {
704 IRBuilder<> Builder(BB);
705 DIBuilder DIB(*M);
706 auto File = DIB.createFile("main.c", "/");
707 auto CU = DIB.createCompileUnit(dwarf::DW_LANG_C, File, "clang",
708 /*isOptimized=*/true, /*Flags=*/"",
709 /*Runtime Version=*/0);
710 auto Type = DIB.createSubroutineType(DIB.getOrCreateTypeArray(None));
711 auto SP = DIB.createFunction(
712 CU, "foo", /*LinkageName=*/"", File,
713 /*LineNo=*/1, Type, /*ScopeLine=*/2, DINode::FlagZero,
714 DISubprogram::SPFlagDefinition | DISubprogram::SPFlagOptimized);
715 EXPECT_TRUE(SP->isDistinct());
717 F->setSubprogram(SP);
718 AllocaInst *I = Builder.CreateAlloca(Builder.getInt8Ty());
719 ReturnInst *R = Builder.CreateRetVoid();
720 I->setDebugLoc(DebugLoc::get(3, 2, SP));
721 R->setDebugLoc(DebugLoc::get(4, 2, SP));
722 DIB.finalize();
723 EXPECT_FALSE(verifyModule(*M));
725 Function *G = Function::Create(F->getFunctionType(),
726 Function::ExternalLinkage, "", M.get());
727 BasicBlock *GBB = BasicBlock::Create(Ctx, "", G);
728 Builder.SetInsertPoint(GBB);
729 I->removeFromParent();
730 Builder.Insert(I);
731 Builder.CreateRetVoid();
732 EXPECT_FALSE(verifyModule(*M));
734 DISubprogram *GSP = DIBuilder::createArtificialSubprogram(F->getSubprogram());
735 EXPECT_EQ(SP->getFile(), GSP->getFile());
736 EXPECT_EQ(SP->getType(), GSP->getType());
737 EXPECT_EQ(SP->getLine(), GSP->getLine());
738 EXPECT_EQ(SP->getScopeLine(), GSP->getScopeLine());
739 EXPECT_TRUE(GSP->isDistinct());
741 G->setSubprogram(GSP);
742 EXPECT_TRUE(verifyModule(*M));
744 auto *InlinedAtNode =
745 DILocation::getDistinct(Ctx, GSP->getScopeLine(), 0, GSP);
746 DebugLoc DL = I->getDebugLoc();
747 DenseMap<const MDNode *, MDNode *> IANodes;
748 auto IA = DebugLoc::appendInlinedAt(DL, InlinedAtNode, Ctx, IANodes);
749 auto NewDL = DebugLoc::get(DL.getLine(), DL.getCol(), DL.getScope(), IA);
750 I->setDebugLoc(NewDL);
751 EXPECT_FALSE(verifyModule(*M));
753 EXPECT_EQ("foo", SP->getName());
754 EXPECT_EQ("foo", GSP->getName());
755 EXPECT_FALSE(SP->isArtificial());
756 EXPECT_TRUE(GSP->isArtificial());
759 TEST_F(IRBuilderTest, InsertExtractElement) {
760 IRBuilder<> Builder(BB);
762 auto VecTy = VectorType::get(Builder.getInt64Ty(), 4);
763 auto Elt1 = Builder.getInt64(-1);
764 auto Elt2 = Builder.getInt64(-2);
765 Value *Vec = UndefValue::get(VecTy);
766 Vec = Builder.CreateInsertElement(Vec, Elt1, Builder.getInt8(1));
767 Vec = Builder.CreateInsertElement(Vec, Elt2, 2);
768 auto X1 = Builder.CreateExtractElement(Vec, 1);
769 auto X2 = Builder.CreateExtractElement(Vec, Builder.getInt32(2));
770 EXPECT_EQ(Elt1, X1);
771 EXPECT_EQ(Elt2, X2);
774 TEST_F(IRBuilderTest, CreateGlobalStringPtr) {
775 IRBuilder<> Builder(BB);
777 auto String1a = Builder.CreateGlobalStringPtr("TestString", "String1a");
778 auto String1b = Builder.CreateGlobalStringPtr("TestString", "String1b", 0);
779 auto String2 = Builder.CreateGlobalStringPtr("TestString", "String2", 1);
780 auto String3 = Builder.CreateGlobalString("TestString", "String3", 2);
782 EXPECT_TRUE(String1a->getType()->getPointerAddressSpace() == 0);
783 EXPECT_TRUE(String1b->getType()->getPointerAddressSpace() == 0);
784 EXPECT_TRUE(String2->getType()->getPointerAddressSpace() == 1);
785 EXPECT_TRUE(String3->getType()->getPointerAddressSpace() == 2);
788 TEST_F(IRBuilderTest, DebugLoc) {
789 auto CalleeTy = FunctionType::get(Type::getVoidTy(Ctx),
790 /*isVarArg=*/false);
791 auto Callee =
792 Function::Create(CalleeTy, Function::ExternalLinkage, "", M.get());
794 DIBuilder DIB(*M);
795 auto File = DIB.createFile("tmp.cpp", "/");
796 auto CU = DIB.createCompileUnit(dwarf::DW_LANG_C_plus_plus_11,
797 DIB.createFile("tmp.cpp", "/"), "", true, "",
799 auto SPType = DIB.createSubroutineType(DIB.getOrCreateTypeArray(None));
800 auto SP =
801 DIB.createFunction(CU, "foo", "foo", File, 1, SPType, 1, DINode::FlagZero,
802 DISubprogram::SPFlagDefinition);
803 DebugLoc DL1 = DILocation::get(Ctx, 2, 0, SP);
804 DebugLoc DL2 = DILocation::get(Ctx, 3, 0, SP);
806 auto BB2 = BasicBlock::Create(Ctx, "bb2", F);
807 auto Br = BranchInst::Create(BB2, BB);
808 Br->setDebugLoc(DL1);
810 IRBuilder<> Builder(Ctx);
811 Builder.SetInsertPoint(Br);
812 EXPECT_EQ(DL1, Builder.getCurrentDebugLocation());
813 auto Call1 = Builder.CreateCall(Callee, None);
814 EXPECT_EQ(DL1, Call1->getDebugLoc());
816 Call1->setDebugLoc(DL2);
817 Builder.SetInsertPoint(Call1->getParent(), Call1->getIterator());
818 EXPECT_EQ(DL2, Builder.getCurrentDebugLocation());
819 auto Call2 = Builder.CreateCall(Callee, None);
820 EXPECT_EQ(DL2, Call2->getDebugLoc());
822 DIB.finalize();
825 TEST_F(IRBuilderTest, DIImportedEntity) {
826 IRBuilder<> Builder(BB);
827 DIBuilder DIB(*M);
828 auto F = DIB.createFile("F.CBL", "/");
829 auto CU = DIB.createCompileUnit(dwarf::DW_LANG_Cobol74,
830 F, "llvm-cobol74",
831 true, "", 0);
832 DIB.createImportedDeclaration(CU, nullptr, F, 1);
833 DIB.createImportedDeclaration(CU, nullptr, F, 1);
834 DIB.createImportedModule(CU, (DIImportedEntity *)nullptr, F, 2);
835 DIB.createImportedModule(CU, (DIImportedEntity *)nullptr, F, 2);
836 DIB.finalize();
837 EXPECT_TRUE(verifyModule(*M));
838 EXPECT_TRUE(CU->getImportedEntities().size() == 2);
841 // 0: #define M0 V0 <-- command line definition
842 // 0: main.c <-- main file
843 // 3: #define M1 V1 <-- M1 definition in main.c
844 // 5: #include "file.h" <-- inclusion of file.h from main.c
845 // 1: #define M2 <-- M2 definition in file.h with no value
846 // 7: #undef M1 V1 <-- M1 un-definition in main.c
847 TEST_F(IRBuilderTest, DIBuilderMacro) {
848 IRBuilder<> Builder(BB);
849 DIBuilder DIB(*M);
850 auto File1 = DIB.createFile("main.c", "/");
851 auto File2 = DIB.createFile("file.h", "/");
852 auto CU = DIB.createCompileUnit(
853 dwarf::DW_LANG_C, DIB.createFile("main.c", "/"), "llvm-c", true, "", 0);
854 auto MDef0 =
855 DIB.createMacro(nullptr, 0, dwarf::DW_MACINFO_define, "M0", "V0");
856 auto TMF1 = DIB.createTempMacroFile(nullptr, 0, File1);
857 auto MDef1 = DIB.createMacro(TMF1, 3, dwarf::DW_MACINFO_define, "M1", "V1");
858 auto TMF2 = DIB.createTempMacroFile(TMF1, 5, File2);
859 auto MDef2 = DIB.createMacro(TMF2, 1, dwarf::DW_MACINFO_define, "M2");
860 auto MUndef1 = DIB.createMacro(TMF1, 7, dwarf::DW_MACINFO_undef, "M1");
862 EXPECT_EQ(dwarf::DW_MACINFO_define, MDef1->getMacinfoType());
863 EXPECT_EQ(3u, MDef1->getLine());
864 EXPECT_EQ("M1", MDef1->getName());
865 EXPECT_EQ("V1", MDef1->getValue());
867 EXPECT_EQ(dwarf::DW_MACINFO_undef, MUndef1->getMacinfoType());
868 EXPECT_EQ(7u, MUndef1->getLine());
869 EXPECT_EQ("M1", MUndef1->getName());
870 EXPECT_EQ("", MUndef1->getValue());
872 EXPECT_EQ(dwarf::DW_MACINFO_start_file, TMF2->getMacinfoType());
873 EXPECT_EQ(5u, TMF2->getLine());
874 EXPECT_EQ(File2, TMF2->getFile());
876 DIB.finalize();
878 SmallVector<Metadata *, 4> Elements;
879 Elements.push_back(MDef2);
880 auto MF2 = DIMacroFile::get(Ctx, dwarf::DW_MACINFO_start_file, 5, File2,
881 DIB.getOrCreateMacroArray(Elements));
883 Elements.clear();
884 Elements.push_back(MDef1);
885 Elements.push_back(MF2);
886 Elements.push_back(MUndef1);
887 auto MF1 = DIMacroFile::get(Ctx, dwarf::DW_MACINFO_start_file, 0, File1,
888 DIB.getOrCreateMacroArray(Elements));
890 Elements.clear();
891 Elements.push_back(MDef0);
892 Elements.push_back(MF1);
893 auto MN0 = MDTuple::get(Ctx, Elements);
894 EXPECT_EQ(MN0, CU->getRawMacros());
896 Elements.clear();
897 Elements.push_back(MDef1);
898 Elements.push_back(MF2);
899 Elements.push_back(MUndef1);
900 auto MN1 = MDTuple::get(Ctx, Elements);
901 EXPECT_EQ(MN1, MF1->getRawElements());
903 Elements.clear();
904 Elements.push_back(MDef2);
905 auto MN2 = MDTuple::get(Ctx, Elements);
906 EXPECT_EQ(MN2, MF2->getRawElements());
907 EXPECT_TRUE(verifyModule(*M));