1 //===- llvm/unittest/IR/InstructionsTest.cpp - Instructions unit 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/Instructions.h"
10 #include "llvm/ADT/CombinationGenerator.h"
11 #include "llvm/ADT/STLExtras.h"
12 #include "llvm/Analysis/ValueTracking.h"
13 #include "llvm/Analysis/VectorUtils.h"
14 #include "llvm/AsmParser/Parser.h"
15 #include "llvm/IR/BasicBlock.h"
16 #include "llvm/IR/Constants.h"
17 #include "llvm/IR/DataLayout.h"
18 #include "llvm/IR/DebugInfoMetadata.h"
19 #include "llvm/IR/DerivedTypes.h"
20 #include "llvm/IR/FPEnv.h"
21 #include "llvm/IR/Function.h"
22 #include "llvm/IR/IRBuilder.h"
23 #include "llvm/IR/LLVMContext.h"
24 #include "llvm/IR/MDBuilder.h"
25 #include "llvm/IR/Module.h"
26 #include "llvm/IR/NoFolder.h"
27 #include "llvm/IR/Operator.h"
28 #include "llvm/Support/SourceMgr.h"
29 #include "llvm-c/Core.h"
30 #include "gmock/gmock-matchers.h"
31 #include "gtest/gtest.h"
37 static std::unique_ptr
<Module
> parseIR(LLVMContext
&C
, const char *IR
) {
39 std::unique_ptr
<Module
> Mod
= parseAssemblyString(IR
, Err
, C
);
41 Err
.print("InstructionsTests", errs());
45 TEST(InstructionsTest
, ReturnInst
) {
49 const ReturnInst
* r0
= ReturnInst::Create(C
);
50 EXPECT_EQ(r0
->getNumOperands(), 0U);
51 EXPECT_EQ(r0
->op_begin(), r0
->op_end());
53 IntegerType
* Int1
= IntegerType::get(C
, 1);
54 Constant
* One
= ConstantInt::get(Int1
, 1, true);
55 const ReturnInst
* r1
= ReturnInst::Create(C
, One
);
56 EXPECT_EQ(1U, r1
->getNumOperands());
57 User::const_op_iterator
b(r1
->op_begin());
58 EXPECT_NE(r1
->op_end(), b
);
60 EXPECT_EQ(One
, r1
->getOperand(0));
62 EXPECT_EQ(r1
->op_end(), b
);
69 // Test fixture that provides a module and a single function within it. Useful
70 // for tests that need to refer to the function in some way.
71 class ModuleWithFunctionTest
: public testing::Test
{
73 ModuleWithFunctionTest() : M(new Module("MyModule", Ctx
)) {
74 FArgTypes
.push_back(Type::getInt8Ty(Ctx
));
75 FArgTypes
.push_back(Type::getInt32Ty(Ctx
));
76 FArgTypes
.push_back(Type::getInt64Ty(Ctx
));
78 FunctionType::get(Type::getVoidTy(Ctx
), FArgTypes
, false);
79 F
= Function::Create(FTy
, Function::ExternalLinkage
, "", M
.get());
83 std::unique_ptr
<Module
> M
;
84 SmallVector
<Type
*, 3> FArgTypes
;
88 TEST_F(ModuleWithFunctionTest
, CallInst
) {
89 Value
*Args
[] = {ConstantInt::get(Type::getInt8Ty(Ctx
), 20),
90 ConstantInt::get(Type::getInt32Ty(Ctx
), 9999),
91 ConstantInt::get(Type::getInt64Ty(Ctx
), 42)};
92 std::unique_ptr
<CallInst
> Call(CallInst::Create(F
, Args
));
94 // Make sure iteration over a call's arguments works as expected.
96 for (Value
*Arg
: Call
->args()) {
97 EXPECT_EQ(FArgTypes
[Idx
], Arg
->getType());
98 EXPECT_EQ(Call
->getArgOperand(Idx
)->getType(), Arg
->getType());
102 Call
->addRetAttr(Attribute::get(Call
->getContext(), "test-str-attr"));
103 EXPECT_TRUE(Call
->hasRetAttr("test-str-attr"));
104 EXPECT_FALSE(Call
->hasRetAttr("not-on-call"));
106 Call
->addFnAttr(Attribute::get(Call
->getContext(), "test-str-fn-attr"));
107 ASSERT_TRUE(Call
->hasFnAttr("test-str-fn-attr"));
108 Call
->removeFnAttr("test-str-fn-attr");
109 EXPECT_FALSE(Call
->hasFnAttr("test-str-fn-attr"));
112 TEST_F(ModuleWithFunctionTest
, InvokeInst
) {
113 BasicBlock
*BB1
= BasicBlock::Create(Ctx
, "", F
);
114 BasicBlock
*BB2
= BasicBlock::Create(Ctx
, "", F
);
116 Value
*Args
[] = {ConstantInt::get(Type::getInt8Ty(Ctx
), 20),
117 ConstantInt::get(Type::getInt32Ty(Ctx
), 9999),
118 ConstantInt::get(Type::getInt64Ty(Ctx
), 42)};
119 std::unique_ptr
<InvokeInst
> Invoke(InvokeInst::Create(F
, BB1
, BB2
, Args
));
121 // Make sure iteration over invoke's arguments works as expected.
123 for (Value
*Arg
: Invoke
->args()) {
124 EXPECT_EQ(FArgTypes
[Idx
], Arg
->getType());
125 EXPECT_EQ(Invoke
->getArgOperand(Idx
)->getType(), Arg
->getType());
130 TEST(InstructionsTest
, BranchInst
) {
133 // Make a BasicBlocks
134 BasicBlock
* bb0
= BasicBlock::Create(C
);
135 BasicBlock
* bb1
= BasicBlock::Create(C
);
137 // Mandatory BranchInst
138 const BranchInst
* b0
= BranchInst::Create(bb0
);
140 EXPECT_TRUE(b0
->isUnconditional());
141 EXPECT_FALSE(b0
->isConditional());
142 EXPECT_EQ(1U, b0
->getNumSuccessors());
144 // check num operands
145 EXPECT_EQ(1U, b0
->getNumOperands());
147 EXPECT_NE(b0
->op_begin(), b0
->op_end());
148 EXPECT_EQ(b0
->op_end(), std::next(b0
->op_begin()));
150 EXPECT_EQ(b0
->op_end(), std::next(b0
->op_begin()));
152 IntegerType
* Int1
= IntegerType::get(C
, 1);
153 Constant
* One
= ConstantInt::get(Int1
, 1, true);
155 // Conditional BranchInst
156 BranchInst
* b1
= BranchInst::Create(bb0
, bb1
, One
);
158 EXPECT_FALSE(b1
->isUnconditional());
159 EXPECT_TRUE(b1
->isConditional());
160 EXPECT_EQ(2U, b1
->getNumSuccessors());
162 // check num operands
163 EXPECT_EQ(3U, b1
->getNumOperands());
165 User::const_op_iterator
b(b1
->op_begin());
168 EXPECT_NE(b
, b1
->op_end());
170 EXPECT_EQ(One
, b1
->getOperand(0));
171 EXPECT_EQ(One
, b1
->getCondition());
176 EXPECT_EQ(bb1
, b1
->getOperand(1));
177 EXPECT_EQ(bb1
, b1
->getSuccessor(1));
182 EXPECT_EQ(bb0
, b1
->getOperand(2));
183 EXPECT_EQ(bb0
, b1
->getSuccessor(0));
186 EXPECT_EQ(b1
->op_end(), b
);
196 TEST(InstructionsTest
, CastInst
) {
199 Type
*Int8Ty
= Type::getInt8Ty(C
);
200 Type
*Int16Ty
= Type::getInt16Ty(C
);
201 Type
*Int32Ty
= Type::getInt32Ty(C
);
202 Type
*Int64Ty
= Type::getInt64Ty(C
);
203 Type
*V8x8Ty
= FixedVectorType::get(Int8Ty
, 8);
204 Type
*V8x64Ty
= FixedVectorType::get(Int64Ty
, 8);
205 Type
*X86MMXTy
= Type::getX86_MMXTy(C
);
207 Type
*HalfTy
= Type::getHalfTy(C
);
208 Type
*FloatTy
= Type::getFloatTy(C
);
209 Type
*DoubleTy
= Type::getDoubleTy(C
);
211 Type
*V2Int32Ty
= FixedVectorType::get(Int32Ty
, 2);
212 Type
*V2Int64Ty
= FixedVectorType::get(Int64Ty
, 2);
213 Type
*V4Int16Ty
= FixedVectorType::get(Int16Ty
, 4);
214 Type
*V1Int16Ty
= FixedVectorType::get(Int16Ty
, 1);
216 Type
*VScaleV2Int32Ty
= ScalableVectorType::get(Int32Ty
, 2);
217 Type
*VScaleV2Int64Ty
= ScalableVectorType::get(Int64Ty
, 2);
218 Type
*VScaleV4Int16Ty
= ScalableVectorType::get(Int16Ty
, 4);
219 Type
*VScaleV1Int16Ty
= ScalableVectorType::get(Int16Ty
, 1);
221 Type
*Int32PtrTy
= PointerType::get(Int32Ty
, 0);
222 Type
*Int64PtrTy
= PointerType::get(Int64Ty
, 0);
224 Type
*Int32PtrAS1Ty
= PointerType::get(Int32Ty
, 1);
225 Type
*Int64PtrAS1Ty
= PointerType::get(Int64Ty
, 1);
227 Type
*V2Int32PtrAS1Ty
= FixedVectorType::get(Int32PtrAS1Ty
, 2);
228 Type
*V2Int64PtrAS1Ty
= FixedVectorType::get(Int64PtrAS1Ty
, 2);
229 Type
*V4Int32PtrAS1Ty
= FixedVectorType::get(Int32PtrAS1Ty
, 4);
230 Type
*VScaleV4Int32PtrAS1Ty
= ScalableVectorType::get(Int32PtrAS1Ty
, 4);
231 Type
*V4Int64PtrAS1Ty
= FixedVectorType::get(Int64PtrAS1Ty
, 4);
233 Type
*V2Int64PtrTy
= FixedVectorType::get(Int64PtrTy
, 2);
234 Type
*V2Int32PtrTy
= FixedVectorType::get(Int32PtrTy
, 2);
235 Type
*VScaleV2Int32PtrTy
= ScalableVectorType::get(Int32PtrTy
, 2);
236 Type
*V4Int32PtrTy
= FixedVectorType::get(Int32PtrTy
, 4);
237 Type
*VScaleV4Int32PtrTy
= ScalableVectorType::get(Int32PtrTy
, 4);
238 Type
*VScaleV4Int64PtrTy
= ScalableVectorType::get(Int64PtrTy
, 4);
240 const Constant
* c8
= Constant::getNullValue(V8x8Ty
);
241 const Constant
* c64
= Constant::getNullValue(V8x64Ty
);
243 const Constant
*v2ptr32
= Constant::getNullValue(V2Int32PtrTy
);
245 EXPECT_EQ(CastInst::Trunc
, CastInst::getCastOpcode(c64
, true, V8x8Ty
, true));
246 EXPECT_EQ(CastInst::SExt
, CastInst::getCastOpcode(c8
, true, V8x64Ty
, true));
248 EXPECT_FALSE(CastInst::isBitCastable(V8x8Ty
, X86MMXTy
));
249 EXPECT_FALSE(CastInst::isBitCastable(X86MMXTy
, V8x8Ty
));
250 EXPECT_FALSE(CastInst::isBitCastable(Int64Ty
, X86MMXTy
));
251 EXPECT_FALSE(CastInst::isBitCastable(V8x64Ty
, V8x8Ty
));
252 EXPECT_FALSE(CastInst::isBitCastable(V8x8Ty
, V8x64Ty
));
254 // Check address space casts are rejected since we don't know the sizes here
255 EXPECT_FALSE(CastInst::isBitCastable(Int32PtrTy
, Int32PtrAS1Ty
));
256 EXPECT_FALSE(CastInst::isBitCastable(Int32PtrAS1Ty
, Int32PtrTy
));
257 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrTy
, V2Int32PtrAS1Ty
));
258 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrAS1Ty
, V2Int32PtrTy
));
259 EXPECT_TRUE(CastInst::isBitCastable(V2Int32PtrAS1Ty
, V2Int64PtrAS1Ty
));
260 EXPECT_EQ(CastInst::AddrSpaceCast
, CastInst::getCastOpcode(v2ptr32
, true,
264 // Test mismatched number of elements for pointers
265 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrAS1Ty
, V4Int64PtrAS1Ty
));
266 EXPECT_FALSE(CastInst::isBitCastable(V4Int64PtrAS1Ty
, V2Int32PtrAS1Ty
));
267 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrAS1Ty
, V4Int32PtrAS1Ty
));
268 EXPECT_FALSE(CastInst::isBitCastable(Int32PtrTy
, V2Int32PtrTy
));
269 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrTy
, Int32PtrTy
));
271 EXPECT_TRUE(CastInst::isBitCastable(Int32PtrTy
, Int64PtrTy
));
272 EXPECT_FALSE(CastInst::isBitCastable(DoubleTy
, FloatTy
));
273 EXPECT_FALSE(CastInst::isBitCastable(FloatTy
, DoubleTy
));
274 EXPECT_TRUE(CastInst::isBitCastable(FloatTy
, FloatTy
));
275 EXPECT_TRUE(CastInst::isBitCastable(FloatTy
, FloatTy
));
276 EXPECT_TRUE(CastInst::isBitCastable(FloatTy
, Int32Ty
));
277 EXPECT_TRUE(CastInst::isBitCastable(Int16Ty
, HalfTy
));
278 EXPECT_TRUE(CastInst::isBitCastable(Int32Ty
, FloatTy
));
279 EXPECT_TRUE(CastInst::isBitCastable(V2Int32Ty
, Int64Ty
));
281 EXPECT_TRUE(CastInst::isBitCastable(V2Int32Ty
, V4Int16Ty
));
282 EXPECT_FALSE(CastInst::isBitCastable(Int32Ty
, Int64Ty
));
283 EXPECT_FALSE(CastInst::isBitCastable(Int64Ty
, Int32Ty
));
285 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrTy
, Int64Ty
));
286 EXPECT_FALSE(CastInst::isBitCastable(Int64Ty
, V2Int32PtrTy
));
287 EXPECT_TRUE(CastInst::isBitCastable(V2Int64PtrTy
, V2Int32PtrTy
));
288 EXPECT_TRUE(CastInst::isBitCastable(V2Int32PtrTy
, V2Int64PtrTy
));
289 EXPECT_FALSE(CastInst::isBitCastable(V2Int32Ty
, V2Int64Ty
));
290 EXPECT_FALSE(CastInst::isBitCastable(V2Int64Ty
, V2Int32Ty
));
293 EXPECT_FALSE(CastInst::castIsValid(Instruction::BitCast
,
294 Constant::getNullValue(V4Int32PtrTy
),
296 EXPECT_FALSE(CastInst::castIsValid(Instruction::BitCast
,
297 Constant::getNullValue(V2Int32PtrTy
),
300 EXPECT_FALSE(CastInst::castIsValid(Instruction::AddrSpaceCast
,
301 Constant::getNullValue(V4Int32PtrAS1Ty
),
303 EXPECT_FALSE(CastInst::castIsValid(Instruction::AddrSpaceCast
,
304 Constant::getNullValue(V2Int32PtrTy
),
307 // Address space cast of fixed/scalable vectors of pointers to scalable/fixed
308 // vector of pointers.
309 EXPECT_FALSE(CastInst::castIsValid(
310 Instruction::AddrSpaceCast
, Constant::getNullValue(VScaleV4Int32PtrAS1Ty
),
312 EXPECT_FALSE(CastInst::castIsValid(Instruction::AddrSpaceCast
,
313 Constant::getNullValue(V4Int32PtrTy
),
314 VScaleV4Int32PtrAS1Ty
));
315 // Address space cast of scalable vectors of pointers to scalable vector of
317 EXPECT_FALSE(CastInst::castIsValid(
318 Instruction::AddrSpaceCast
, Constant::getNullValue(VScaleV4Int32PtrAS1Ty
),
319 VScaleV2Int32PtrTy
));
320 EXPECT_FALSE(CastInst::castIsValid(Instruction::AddrSpaceCast
,
321 Constant::getNullValue(VScaleV2Int32PtrTy
),
322 VScaleV4Int32PtrAS1Ty
));
323 EXPECT_TRUE(CastInst::castIsValid(Instruction::AddrSpaceCast
,
324 Constant::getNullValue(VScaleV4Int64PtrTy
),
325 VScaleV4Int32PtrAS1Ty
));
326 // Same number of lanes, different address space.
327 EXPECT_TRUE(CastInst::castIsValid(
328 Instruction::AddrSpaceCast
, Constant::getNullValue(VScaleV4Int32PtrAS1Ty
),
329 VScaleV4Int32PtrTy
));
330 // Same number of lanes, same address space.
331 EXPECT_FALSE(CastInst::castIsValid(Instruction::AddrSpaceCast
,
332 Constant::getNullValue(VScaleV4Int64PtrTy
),
333 VScaleV4Int32PtrTy
));
335 // Bit casting fixed/scalable vector to scalable/fixed vectors.
336 EXPECT_FALSE(CastInst::castIsValid(Instruction::BitCast
,
337 Constant::getNullValue(V2Int32Ty
),
339 EXPECT_FALSE(CastInst::castIsValid(Instruction::BitCast
,
340 Constant::getNullValue(V2Int64Ty
),
342 EXPECT_FALSE(CastInst::castIsValid(Instruction::BitCast
,
343 Constant::getNullValue(V4Int16Ty
),
345 EXPECT_FALSE(CastInst::castIsValid(Instruction::BitCast
,
346 Constant::getNullValue(VScaleV2Int32Ty
),
348 EXPECT_FALSE(CastInst::castIsValid(Instruction::BitCast
,
349 Constant::getNullValue(VScaleV2Int64Ty
),
351 EXPECT_FALSE(CastInst::castIsValid(Instruction::BitCast
,
352 Constant::getNullValue(VScaleV4Int16Ty
),
355 // Bit casting scalable vectors to scalable vectors.
356 EXPECT_TRUE(CastInst::castIsValid(Instruction::BitCast
,
357 Constant::getNullValue(VScaleV4Int16Ty
),
359 EXPECT_TRUE(CastInst::castIsValid(Instruction::BitCast
,
360 Constant::getNullValue(VScaleV2Int32Ty
),
362 EXPECT_FALSE(CastInst::castIsValid(Instruction::BitCast
,
363 Constant::getNullValue(VScaleV2Int64Ty
),
365 EXPECT_FALSE(CastInst::castIsValid(Instruction::BitCast
,
366 Constant::getNullValue(VScaleV2Int32Ty
),
369 // Bitcasting to/from <vscale x 1 x Ty>
370 EXPECT_FALSE(CastInst::castIsValid(Instruction::BitCast
,
371 Constant::getNullValue(VScaleV1Int16Ty
),
373 EXPECT_FALSE(CastInst::castIsValid(Instruction::BitCast
,
374 Constant::getNullValue(V1Int16Ty
),
377 // Check that assertion is not hit when creating a cast with a vector of
380 BasicBlock
*BB
= BasicBlock::Create(C
);
381 Constant
*NullV2I32Ptr
= Constant::getNullValue(V2Int32PtrTy
);
382 auto Inst1
= CastInst::CreatePointerCast(NullV2I32Ptr
, V2Int32Ty
, "foo", BB
);
384 Constant
*NullVScaleV2I32Ptr
= Constant::getNullValue(VScaleV2Int32PtrTy
);
385 auto Inst1VScale
= CastInst::CreatePointerCast(
386 NullVScaleV2I32Ptr
, VScaleV2Int32Ty
, "foo.vscale", BB
);
389 auto Inst2
= CastInst::CreatePointerCast(NullV2I32Ptr
, V2Int32Ty
);
391 CastInst::CreatePointerCast(NullVScaleV2I32Ptr
, VScaleV2Int32Ty
);
395 Inst1
->eraseFromParent();
396 Inst1VScale
->eraseFromParent();
400 TEST(InstructionsTest
, CastCAPI
) {
403 Type
*Int8Ty
= Type::getInt8Ty(C
);
404 Type
*Int32Ty
= Type::getInt32Ty(C
);
405 Type
*Int64Ty
= Type::getInt64Ty(C
);
407 Type
*FloatTy
= Type::getFloatTy(C
);
408 Type
*DoubleTy
= Type::getDoubleTy(C
);
410 Type
*Int8PtrTy
= PointerType::get(Int8Ty
, 0);
411 Type
*Int32PtrTy
= PointerType::get(Int32Ty
, 0);
413 const Constant
*C8
= Constant::getNullValue(Int8Ty
);
414 const Constant
*C64
= Constant::getNullValue(Int64Ty
);
416 EXPECT_EQ(LLVMBitCast
,
417 LLVMGetCastOpcode(wrap(C64
), true, wrap(Int64Ty
), true));
418 EXPECT_EQ(LLVMTrunc
, LLVMGetCastOpcode(wrap(C64
), true, wrap(Int8Ty
), true));
419 EXPECT_EQ(LLVMSExt
, LLVMGetCastOpcode(wrap(C8
), true, wrap(Int64Ty
), true));
420 EXPECT_EQ(LLVMZExt
, LLVMGetCastOpcode(wrap(C8
), false, wrap(Int64Ty
), true));
422 const Constant
*CF32
= Constant::getNullValue(FloatTy
);
423 const Constant
*CF64
= Constant::getNullValue(DoubleTy
);
425 EXPECT_EQ(LLVMFPToUI
,
426 LLVMGetCastOpcode(wrap(CF32
), true, wrap(Int8Ty
), false));
427 EXPECT_EQ(LLVMFPToSI
,
428 LLVMGetCastOpcode(wrap(CF32
), true, wrap(Int8Ty
), true));
429 EXPECT_EQ(LLVMUIToFP
,
430 LLVMGetCastOpcode(wrap(C8
), false, wrap(FloatTy
), true));
431 EXPECT_EQ(LLVMSIToFP
, LLVMGetCastOpcode(wrap(C8
), true, wrap(FloatTy
), true));
432 EXPECT_EQ(LLVMFPTrunc
,
433 LLVMGetCastOpcode(wrap(CF64
), true, wrap(FloatTy
), true));
435 LLVMGetCastOpcode(wrap(CF32
), true, wrap(DoubleTy
), true));
437 const Constant
*CPtr8
= Constant::getNullValue(Int8PtrTy
);
439 EXPECT_EQ(LLVMPtrToInt
,
440 LLVMGetCastOpcode(wrap(CPtr8
), true, wrap(Int8Ty
), true));
441 EXPECT_EQ(LLVMIntToPtr
,
442 LLVMGetCastOpcode(wrap(C8
), true, wrap(Int8PtrTy
), true));
444 Type
*V8x8Ty
= FixedVectorType::get(Int8Ty
, 8);
445 Type
*V8x64Ty
= FixedVectorType::get(Int64Ty
, 8);
446 const Constant
*CV8
= Constant::getNullValue(V8x8Ty
);
447 const Constant
*CV64
= Constant::getNullValue(V8x64Ty
);
449 EXPECT_EQ(LLVMTrunc
, LLVMGetCastOpcode(wrap(CV64
), true, wrap(V8x8Ty
), true));
450 EXPECT_EQ(LLVMSExt
, LLVMGetCastOpcode(wrap(CV8
), true, wrap(V8x64Ty
), true));
452 Type
*Int32PtrAS1Ty
= PointerType::get(Int32Ty
, 1);
453 Type
*V2Int32PtrAS1Ty
= FixedVectorType::get(Int32PtrAS1Ty
, 2);
454 Type
*V2Int32PtrTy
= FixedVectorType::get(Int32PtrTy
, 2);
455 const Constant
*CV2ptr32
= Constant::getNullValue(V2Int32PtrTy
);
457 EXPECT_EQ(LLVMAddrSpaceCast
, LLVMGetCastOpcode(wrap(CV2ptr32
), true,
458 wrap(V2Int32PtrAS1Ty
), true));
461 TEST(InstructionsTest
, VectorGep
) {
465 Type
*I8Ty
= IntegerType::get(C
, 8);
466 Type
*I32Ty
= IntegerType::get(C
, 32);
467 PointerType
*Ptri8Ty
= PointerType::get(I8Ty
, 0);
468 PointerType
*Ptri32Ty
= PointerType::get(I32Ty
, 0);
470 VectorType
*V2xi8PTy
= FixedVectorType::get(Ptri8Ty
, 2);
471 VectorType
*V2xi32PTy
= FixedVectorType::get(Ptri32Ty
, 2);
473 // Test different aspects of the vector-of-pointers type
474 // and GEPs which use this type.
475 ConstantInt
*Ci32a
= ConstantInt::get(C
, APInt(32, 1492));
476 ConstantInt
*Ci32b
= ConstantInt::get(C
, APInt(32, 1948));
477 std::vector
<Constant
*> ConstVa(2, Ci32a
);
478 std::vector
<Constant
*> ConstVb(2, Ci32b
);
479 Constant
*C2xi32a
= ConstantVector::get(ConstVa
);
480 Constant
*C2xi32b
= ConstantVector::get(ConstVb
);
482 CastInst
*PtrVecA
= new IntToPtrInst(C2xi32a
, V2xi32PTy
);
483 CastInst
*PtrVecB
= new IntToPtrInst(C2xi32b
, V2xi32PTy
);
485 ICmpInst
*ICmp0
= new ICmpInst(ICmpInst::ICMP_SGT
, PtrVecA
, PtrVecB
);
486 ICmpInst
*ICmp1
= new ICmpInst(ICmpInst::ICMP_ULT
, PtrVecA
, PtrVecB
);
487 EXPECT_NE(ICmp0
, ICmp1
); // suppress warning.
489 BasicBlock
* BB0
= BasicBlock::Create(C
);
490 // Test InsertAtEnd ICmpInst constructor.
491 ICmpInst
*ICmp2
= new ICmpInst(*BB0
, ICmpInst::ICMP_SGE
, PtrVecA
, PtrVecB
);
492 EXPECT_NE(ICmp0
, ICmp2
); // suppress warning.
494 GetElementPtrInst
*Gep0
= GetElementPtrInst::Create(I32Ty
, PtrVecA
, C2xi32a
);
495 GetElementPtrInst
*Gep1
= GetElementPtrInst::Create(I32Ty
, PtrVecA
, C2xi32b
);
496 GetElementPtrInst
*Gep2
= GetElementPtrInst::Create(I32Ty
, PtrVecB
, C2xi32a
);
497 GetElementPtrInst
*Gep3
= GetElementPtrInst::Create(I32Ty
, PtrVecB
, C2xi32b
);
499 CastInst
*BTC0
= new BitCastInst(Gep0
, V2xi8PTy
);
500 CastInst
*BTC1
= new BitCastInst(Gep1
, V2xi8PTy
);
501 CastInst
*BTC2
= new BitCastInst(Gep2
, V2xi8PTy
);
502 CastInst
*BTC3
= new BitCastInst(Gep3
, V2xi8PTy
);
504 Value
*S0
= BTC0
->stripPointerCasts();
505 Value
*S1
= BTC1
->stripPointerCasts();
506 Value
*S2
= BTC2
->stripPointerCasts();
507 Value
*S3
= BTC3
->stripPointerCasts();
515 DataLayout
TD("e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f3"
516 "2:32:32-f64:64:64-v64:64:64-v128:128:128-a:0:64-s:64:64-f80"
517 ":128:128-n8:16:32:64-S128");
518 // Make sure we don't crash
519 GetPointerBaseWithConstantOffset(Gep0
, Offset
, TD
);
520 GetPointerBaseWithConstantOffset(Gep1
, Offset
, TD
);
521 GetPointerBaseWithConstantOffset(Gep2
, Offset
, TD
);
522 GetPointerBaseWithConstantOffset(Gep3
, Offset
, TD
);
525 GetElementPtrInst
*GepII0
= GetElementPtrInst::Create(I32Ty
, Gep0
, C2xi32b
);
526 GetElementPtrInst
*GepII1
= GetElementPtrInst::Create(I32Ty
, Gep1
, C2xi32a
);
527 GetElementPtrInst
*GepII2
= GetElementPtrInst::Create(I32Ty
, Gep2
, C2xi32b
);
528 GetElementPtrInst
*GepII3
= GetElementPtrInst::Create(I32Ty
, Gep3
, C2xi32a
);
530 EXPECT_EQ(GepII0
->getNumIndices(), 1u);
531 EXPECT_EQ(GepII1
->getNumIndices(), 1u);
532 EXPECT_EQ(GepII2
->getNumIndices(), 1u);
533 EXPECT_EQ(GepII3
->getNumIndices(), 1u);
535 EXPECT_FALSE(GepII0
->hasAllZeroIndices());
536 EXPECT_FALSE(GepII1
->hasAllZeroIndices());
537 EXPECT_FALSE(GepII2
->hasAllZeroIndices());
538 EXPECT_FALSE(GepII3
->hasAllZeroIndices());
555 ICmp2
->eraseFromParent();
564 TEST(InstructionsTest
, FPMathOperator
) {
566 IRBuilder
<> Builder(Context
);
567 MDBuilder
MDHelper(Context
);
568 Instruction
*I
= Builder
.CreatePHI(Builder
.getDoubleTy(), 0);
569 MDNode
*MD1
= MDHelper
.createFPMath(1.0);
570 Value
*V1
= Builder
.CreateFAdd(I
, I
, "", MD1
);
571 EXPECT_TRUE(isa
<FPMathOperator
>(V1
));
572 FPMathOperator
*O1
= cast
<FPMathOperator
>(V1
);
573 EXPECT_EQ(O1
->getFPAccuracy(), 1.0);
578 TEST(InstructionTest
, ConstrainedTrans
) {
580 std::unique_ptr
<Module
> M(new Module("MyModule", Context
));
582 FunctionType::get(Type::getVoidTy(Context
),
583 {Type::getFloatTy(Context
), Type::getFloatTy(Context
),
584 Type::getInt32Ty(Context
)},
586 auto *F
= Function::Create(FTy
, Function::ExternalLinkage
, "", M
.get());
587 auto *BB
= BasicBlock::Create(Context
, "bb", F
);
588 IRBuilder
<> Builder(Context
);
589 Builder
.SetInsertPoint(BB
);
590 auto *Arg0
= F
->arg_begin();
591 auto *Arg1
= F
->arg_begin() + 1;
594 auto *I
= cast
<Instruction
>(Builder
.CreateFAdd(Arg0
, Arg1
));
595 EXPECT_EQ(Intrinsic::experimental_constrained_fadd
,
596 getConstrainedIntrinsicID(*I
));
600 auto *I
= cast
<Instruction
>(
601 Builder
.CreateFPToSI(Arg0
, Type::getInt32Ty(Context
)));
602 EXPECT_EQ(Intrinsic::experimental_constrained_fptosi
,
603 getConstrainedIntrinsicID(*I
));
607 auto *I
= cast
<Instruction
>(Builder
.CreateIntrinsic(
608 Intrinsic::ceil
, {Type::getFloatTy(Context
)}, {Arg0
}));
609 EXPECT_EQ(Intrinsic::experimental_constrained_ceil
,
610 getConstrainedIntrinsicID(*I
));
614 auto *I
= cast
<Instruction
>(Builder
.CreateFCmpOEQ(Arg0
, Arg1
));
615 EXPECT_EQ(Intrinsic::experimental_constrained_fcmp
,
616 getConstrainedIntrinsicID(*I
));
620 auto *Arg2
= F
->arg_begin() + 2;
621 auto *I
= cast
<Instruction
>(Builder
.CreateAdd(Arg2
, Arg2
));
622 EXPECT_EQ(Intrinsic::not_intrinsic
, getConstrainedIntrinsicID(*I
));
626 auto *I
= cast
<Instruction
>(Builder
.CreateConstrainedFPBinOp(
627 Intrinsic::experimental_constrained_fadd
, Arg0
, Arg0
));
628 EXPECT_EQ(Intrinsic::not_intrinsic
, getConstrainedIntrinsicID(*I
));
632 TEST(InstructionsTest
, isEliminableCastPair
) {
635 Type
* Int16Ty
= Type::getInt16Ty(C
);
636 Type
* Int32Ty
= Type::getInt32Ty(C
);
637 Type
* Int64Ty
= Type::getInt64Ty(C
);
638 Type
*Int64PtrTy
= PointerType::get(C
, 0);
640 // Source and destination pointers have same size -> bitcast.
641 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt
,
643 Int64PtrTy
, Int64Ty
, Int64PtrTy
,
644 Int32Ty
, nullptr, Int32Ty
),
647 // Source and destination have unknown sizes, but the same address space and
648 // the intermediate int is the maximum pointer size -> bitcast
649 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt
,
651 Int64PtrTy
, Int64Ty
, Int64PtrTy
,
652 nullptr, nullptr, nullptr),
655 // Source and destination have unknown sizes, but the same address space and
656 // the intermediate int is not the maximum pointer size -> nothing
657 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt
,
659 Int64PtrTy
, Int32Ty
, Int64PtrTy
,
660 nullptr, nullptr, nullptr),
663 // Middle pointer big enough -> bitcast.
664 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr
,
666 Int64Ty
, Int64PtrTy
, Int64Ty
,
667 nullptr, Int64Ty
, nullptr),
670 // Middle pointer too small -> fail.
671 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr
,
673 Int64Ty
, Int64PtrTy
, Int64Ty
,
674 nullptr, Int32Ty
, nullptr),
677 // Test that we don't eliminate bitcasts between different address spaces,
678 // or if we don't have available pointer size information.
679 DataLayout
DL("e-p:32:32:32-p1:16:16:16-p2:64:64:64-i1:8:8-i8:8:8-i16:16:16"
680 "-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64"
681 "-v128:128:128-a:0:64-s:64:64-f80:128:128-n8:16:32:64-S128");
683 Type
*Int64PtrTyAS1
= PointerType::get(C
, 1);
684 Type
*Int64PtrTyAS2
= PointerType::get(C
, 2);
686 IntegerType
*Int16SizePtr
= DL
.getIntPtrType(C
, 1);
687 IntegerType
*Int64SizePtr
= DL
.getIntPtrType(C
, 2);
689 // Cannot simplify inttoptr, addrspacecast
690 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr
,
691 CastInst::AddrSpaceCast
,
692 Int16Ty
, Int64PtrTyAS1
, Int64PtrTyAS2
,
693 nullptr, Int16SizePtr
, Int64SizePtr
),
696 // Cannot simplify addrspacecast, ptrtoint
697 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::AddrSpaceCast
,
699 Int64PtrTyAS1
, Int64PtrTyAS2
, Int16Ty
,
700 Int64SizePtr
, Int16SizePtr
, nullptr),
703 // Pass since the bitcast address spaces are the same
704 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr
,
706 Int16Ty
, Int64PtrTyAS1
, Int64PtrTyAS1
,
707 nullptr, nullptr, nullptr),
712 TEST(InstructionsTest
, CloneCall
) {
714 Type
*Int32Ty
= Type::getInt32Ty(C
);
715 Type
*ArgTys
[] = {Int32Ty
, Int32Ty
, Int32Ty
};
716 FunctionType
*FnTy
= FunctionType::get(Int32Ty
, ArgTys
, /*isVarArg=*/false);
717 Value
*Callee
= Constant::getNullValue(FnTy
->getPointerTo());
719 ConstantInt::get(Int32Ty
, 1),
720 ConstantInt::get(Int32Ty
, 2),
721 ConstantInt::get(Int32Ty
, 3)
723 std::unique_ptr
<CallInst
> Call(
724 CallInst::Create(FnTy
, Callee
, Args
, "result"));
726 // Test cloning the tail call kind.
727 CallInst::TailCallKind Kinds
[] = {CallInst::TCK_None
, CallInst::TCK_Tail
,
728 CallInst::TCK_MustTail
};
729 for (CallInst::TailCallKind TCK
: Kinds
) {
730 Call
->setTailCallKind(TCK
);
731 std::unique_ptr
<CallInst
> Clone(cast
<CallInst
>(Call
->clone()));
732 EXPECT_EQ(Call
->getTailCallKind(), Clone
->getTailCallKind());
734 Call
->setTailCallKind(CallInst::TCK_None
);
736 // Test cloning an attribute.
739 AB
.addAttribute(Attribute::NoUnwind
);
741 AttributeList::get(C
, AttributeList::FunctionIndex
, AB
));
742 std::unique_ptr
<CallInst
> Clone(cast
<CallInst
>(Call
->clone()));
743 EXPECT_TRUE(Clone
->doesNotThrow());
747 TEST(InstructionsTest
, AlterCallBundles
) {
749 Type
*Int32Ty
= Type::getInt32Ty(C
);
750 FunctionType
*FnTy
= FunctionType::get(Int32Ty
, Int32Ty
, /*isVarArg=*/false);
751 Value
*Callee
= Constant::getNullValue(FnTy
->getPointerTo());
752 Value
*Args
[] = {ConstantInt::get(Int32Ty
, 42)};
753 OperandBundleDef
OldBundle("before", UndefValue::get(Int32Ty
));
754 std::unique_ptr
<CallInst
> Call(
755 CallInst::Create(FnTy
, Callee
, Args
, OldBundle
, "result"));
756 Call
->setTailCallKind(CallInst::TailCallKind::TCK_NoTail
);
758 AB
.addAttribute(Attribute::Cold
);
759 Call
->setAttributes(AttributeList::get(C
, AttributeList::FunctionIndex
, AB
));
760 Call
->setDebugLoc(DebugLoc(MDNode::get(C
, std::nullopt
)));
762 OperandBundleDef
NewBundle("after", ConstantInt::get(Int32Ty
, 7));
763 std::unique_ptr
<CallInst
> Clone(CallInst::Create(Call
.get(), NewBundle
));
764 EXPECT_EQ(Call
->arg_size(), Clone
->arg_size());
765 EXPECT_EQ(Call
->getArgOperand(0), Clone
->getArgOperand(0));
766 EXPECT_EQ(Call
->getCallingConv(), Clone
->getCallingConv());
767 EXPECT_EQ(Call
->getTailCallKind(), Clone
->getTailCallKind());
768 EXPECT_TRUE(Clone
->hasFnAttr(Attribute::AttrKind::Cold
));
769 EXPECT_EQ(Call
->getDebugLoc(), Clone
->getDebugLoc());
770 EXPECT_EQ(Clone
->getNumOperandBundles(), 1U);
771 EXPECT_TRUE(Clone
->getOperandBundle("after"));
774 TEST(InstructionsTest
, AlterInvokeBundles
) {
776 Type
*Int32Ty
= Type::getInt32Ty(C
);
777 FunctionType
*FnTy
= FunctionType::get(Int32Ty
, Int32Ty
, /*isVarArg=*/false);
778 Value
*Callee
= Constant::getNullValue(FnTy
->getPointerTo());
779 Value
*Args
[] = {ConstantInt::get(Int32Ty
, 42)};
780 std::unique_ptr
<BasicBlock
> NormalDest(BasicBlock::Create(C
));
781 std::unique_ptr
<BasicBlock
> UnwindDest(BasicBlock::Create(C
));
782 OperandBundleDef
OldBundle("before", UndefValue::get(Int32Ty
));
783 std::unique_ptr
<InvokeInst
> Invoke(
784 InvokeInst::Create(FnTy
, Callee
, NormalDest
.get(), UnwindDest
.get(), Args
,
785 OldBundle
, "result"));
787 AB
.addAttribute(Attribute::Cold
);
788 Invoke
->setAttributes(
789 AttributeList::get(C
, AttributeList::FunctionIndex
, AB
));
790 Invoke
->setDebugLoc(DebugLoc(MDNode::get(C
, std::nullopt
)));
792 OperandBundleDef
NewBundle("after", ConstantInt::get(Int32Ty
, 7));
793 std::unique_ptr
<InvokeInst
> Clone(
794 InvokeInst::Create(Invoke
.get(), NewBundle
));
795 EXPECT_EQ(Invoke
->getNormalDest(), Clone
->getNormalDest());
796 EXPECT_EQ(Invoke
->getUnwindDest(), Clone
->getUnwindDest());
797 EXPECT_EQ(Invoke
->arg_size(), Clone
->arg_size());
798 EXPECT_EQ(Invoke
->getArgOperand(0), Clone
->getArgOperand(0));
799 EXPECT_EQ(Invoke
->getCallingConv(), Clone
->getCallingConv());
800 EXPECT_TRUE(Clone
->hasFnAttr(Attribute::AttrKind::Cold
));
801 EXPECT_EQ(Invoke
->getDebugLoc(), Clone
->getDebugLoc());
802 EXPECT_EQ(Clone
->getNumOperandBundles(), 1U);
803 EXPECT_TRUE(Clone
->getOperandBundle("after"));
806 TEST_F(ModuleWithFunctionTest
, DropPoisonGeneratingFlags
) {
807 auto *OnlyBB
= BasicBlock::Create(Ctx
, "bb", F
);
808 auto *Arg0
= &*F
->arg_begin();
810 IRBuilder
<NoFolder
> B(Ctx
);
811 B
.SetInsertPoint(OnlyBB
);
815 cast
<Instruction
>(B
.CreateUDiv(Arg0
, Arg0
, "", /*isExact*/ true));
816 ASSERT_TRUE(UI
->isExact());
817 UI
->dropPoisonGeneratingFlags();
818 ASSERT_FALSE(UI
->isExact());
823 cast
<Instruction
>(B
.CreateLShr(Arg0
, Arg0
, "", /*isExact*/ true));
824 ASSERT_TRUE(ShrI
->isExact());
825 ShrI
->dropPoisonGeneratingFlags();
826 ASSERT_FALSE(ShrI
->isExact());
830 auto *AI
= cast
<Instruction
>(
831 B
.CreateAdd(Arg0
, Arg0
, "", /*HasNUW*/ true, /*HasNSW*/ false));
832 ASSERT_TRUE(AI
->hasNoUnsignedWrap());
833 AI
->dropPoisonGeneratingFlags();
834 ASSERT_FALSE(AI
->hasNoUnsignedWrap());
835 ASSERT_FALSE(AI
->hasNoSignedWrap());
839 auto *SI
= cast
<Instruction
>(
840 B
.CreateAdd(Arg0
, Arg0
, "", /*HasNUW*/ false, /*HasNSW*/ true));
841 ASSERT_TRUE(SI
->hasNoSignedWrap());
842 SI
->dropPoisonGeneratingFlags();
843 ASSERT_FALSE(SI
->hasNoUnsignedWrap());
844 ASSERT_FALSE(SI
->hasNoSignedWrap());
848 auto *ShlI
= cast
<Instruction
>(
849 B
.CreateShl(Arg0
, Arg0
, "", /*HasNUW*/ true, /*HasNSW*/ true));
850 ASSERT_TRUE(ShlI
->hasNoSignedWrap());
851 ASSERT_TRUE(ShlI
->hasNoUnsignedWrap());
852 ShlI
->dropPoisonGeneratingFlags();
853 ASSERT_FALSE(ShlI
->hasNoUnsignedWrap());
854 ASSERT_FALSE(ShlI
->hasNoSignedWrap());
858 Value
*GEPBase
= Constant::getNullValue(B
.getInt8PtrTy());
859 auto *GI
= cast
<GetElementPtrInst
>(
860 B
.CreateInBoundsGEP(B
.getInt8Ty(), GEPBase
, Arg0
));
861 ASSERT_TRUE(GI
->isInBounds());
862 GI
->dropPoisonGeneratingFlags();
863 ASSERT_FALSE(GI
->isInBounds());
867 TEST(InstructionsTest
, GEPIndices
) {
869 IRBuilder
<NoFolder
> Builder(Context
);
870 Type
*ElementTy
= Builder
.getInt8Ty();
871 Type
*ArrTy
= ArrayType::get(ArrayType::get(ElementTy
, 64), 64);
874 Builder
.getInt32(13),
875 Builder
.getInt32(42) };
877 Value
*V
= Builder
.CreateGEP(ArrTy
, UndefValue::get(PointerType::getUnqual(ArrTy
)),
879 ASSERT_TRUE(isa
<GetElementPtrInst
>(V
));
881 auto *GEPI
= cast
<GetElementPtrInst
>(V
);
882 ASSERT_NE(GEPI
->idx_begin(), GEPI
->idx_end());
883 ASSERT_EQ(GEPI
->idx_end(), std::next(GEPI
->idx_begin(), 3));
884 EXPECT_EQ(Indices
[0], GEPI
->idx_begin()[0]);
885 EXPECT_EQ(Indices
[1], GEPI
->idx_begin()[1]);
886 EXPECT_EQ(Indices
[2], GEPI
->idx_begin()[2]);
887 EXPECT_EQ(GEPI
->idx_begin(), GEPI
->indices().begin());
888 EXPECT_EQ(GEPI
->idx_end(), GEPI
->indices().end());
890 const auto *CGEPI
= GEPI
;
891 ASSERT_NE(CGEPI
->idx_begin(), CGEPI
->idx_end());
892 ASSERT_EQ(CGEPI
->idx_end(), std::next(CGEPI
->idx_begin(), 3));
893 EXPECT_EQ(Indices
[0], CGEPI
->idx_begin()[0]);
894 EXPECT_EQ(Indices
[1], CGEPI
->idx_begin()[1]);
895 EXPECT_EQ(Indices
[2], CGEPI
->idx_begin()[2]);
896 EXPECT_EQ(CGEPI
->idx_begin(), CGEPI
->indices().begin());
897 EXPECT_EQ(CGEPI
->idx_end(), CGEPI
->indices().end());
902 TEST(InstructionsTest
, SwitchInst
) {
905 std::unique_ptr
<BasicBlock
> BB1
, BB2
, BB3
;
906 BB1
.reset(BasicBlock::Create(C
));
907 BB2
.reset(BasicBlock::Create(C
));
908 BB3
.reset(BasicBlock::Create(C
));
910 // We create block 0 after the others so that it gets destroyed first and
911 // clears the uses of the other basic blocks.
912 std::unique_ptr
<BasicBlock
> BB0(BasicBlock::Create(C
));
914 auto *Int32Ty
= Type::getInt32Ty(C
);
917 SwitchInst::Create(UndefValue::get(Int32Ty
), BB0
.get(), 3, BB0
.get());
918 SI
->addCase(ConstantInt::get(Int32Ty
, 1), BB1
.get());
919 SI
->addCase(ConstantInt::get(Int32Ty
, 2), BB2
.get());
920 SI
->addCase(ConstantInt::get(Int32Ty
, 3), BB3
.get());
922 auto CI
= SI
->case_begin();
923 ASSERT_NE(CI
, SI
->case_end());
924 EXPECT_EQ(1, CI
->getCaseValue()->getSExtValue());
925 EXPECT_EQ(BB1
.get(), CI
->getCaseSuccessor());
926 EXPECT_EQ(2, (CI
+ 1)->getCaseValue()->getSExtValue());
927 EXPECT_EQ(BB2
.get(), (CI
+ 1)->getCaseSuccessor());
928 EXPECT_EQ(3, (CI
+ 2)->getCaseValue()->getSExtValue());
929 EXPECT_EQ(BB3
.get(), (CI
+ 2)->getCaseSuccessor());
930 EXPECT_EQ(CI
+ 1, std::next(CI
));
931 EXPECT_EQ(CI
+ 2, std::next(CI
, 2));
932 EXPECT_EQ(CI
+ 3, std::next(CI
, 3));
933 EXPECT_EQ(SI
->case_end(), CI
+ 3);
934 EXPECT_EQ(0, CI
- CI
);
935 EXPECT_EQ(1, (CI
+ 1) - CI
);
936 EXPECT_EQ(2, (CI
+ 2) - CI
);
937 EXPECT_EQ(3, SI
->case_end() - CI
);
938 EXPECT_EQ(3, std::distance(CI
, SI
->case_end()));
940 auto CCI
= const_cast<const SwitchInst
*>(SI
)->case_begin();
941 SwitchInst::ConstCaseIt CCE
= SI
->case_end();
942 ASSERT_NE(CCI
, SI
->case_end());
943 EXPECT_EQ(1, CCI
->getCaseValue()->getSExtValue());
944 EXPECT_EQ(BB1
.get(), CCI
->getCaseSuccessor());
945 EXPECT_EQ(2, (CCI
+ 1)->getCaseValue()->getSExtValue());
946 EXPECT_EQ(BB2
.get(), (CCI
+ 1)->getCaseSuccessor());
947 EXPECT_EQ(3, (CCI
+ 2)->getCaseValue()->getSExtValue());
948 EXPECT_EQ(BB3
.get(), (CCI
+ 2)->getCaseSuccessor());
949 EXPECT_EQ(CCI
+ 1, std::next(CCI
));
950 EXPECT_EQ(CCI
+ 2, std::next(CCI
, 2));
951 EXPECT_EQ(CCI
+ 3, std::next(CCI
, 3));
952 EXPECT_EQ(CCE
, CCI
+ 3);
953 EXPECT_EQ(0, CCI
- CCI
);
954 EXPECT_EQ(1, (CCI
+ 1) - CCI
);
955 EXPECT_EQ(2, (CCI
+ 2) - CCI
);
956 EXPECT_EQ(3, CCE
- CCI
);
957 EXPECT_EQ(3, std::distance(CCI
, CCE
));
959 // Make sure that the const iterator is compatible with a const auto ref.
960 const auto &Handle
= *CCI
;
961 EXPECT_EQ(1, Handle
.getCaseValue()->getSExtValue());
962 EXPECT_EQ(BB1
.get(), Handle
.getCaseSuccessor());
965 TEST(InstructionsTest
, SwitchInstProfUpdateWrapper
) {
968 std::unique_ptr
<BasicBlock
> BB1
, BB2
, BB3
;
969 BB1
.reset(BasicBlock::Create(C
));
970 BB2
.reset(BasicBlock::Create(C
));
971 BB3
.reset(BasicBlock::Create(C
));
973 // We create block 0 after the others so that it gets destroyed first and
974 // clears the uses of the other basic blocks.
975 std::unique_ptr
<BasicBlock
> BB0(BasicBlock::Create(C
));
977 auto *Int32Ty
= Type::getInt32Ty(C
);
980 SwitchInst::Create(UndefValue::get(Int32Ty
), BB0
.get(), 4, BB0
.get());
981 SI
->addCase(ConstantInt::get(Int32Ty
, 1), BB1
.get());
982 SI
->addCase(ConstantInt::get(Int32Ty
, 2), BB2
.get());
983 SI
->setMetadata(LLVMContext::MD_prof
,
984 MDBuilder(C
).createBranchWeights({ 9, 1, 22 }));
987 SwitchInstProfUpdateWrapper
SIW(*SI
);
988 EXPECT_EQ(*SIW
.getSuccessorWeight(0), 9u);
989 EXPECT_EQ(*SIW
.getSuccessorWeight(1), 1u);
990 EXPECT_EQ(*SIW
.getSuccessorWeight(2), 22u);
991 SIW
.setSuccessorWeight(0, 99u);
992 SIW
.setSuccessorWeight(1, 11u);
993 EXPECT_EQ(*SIW
.getSuccessorWeight(0), 99u);
994 EXPECT_EQ(*SIW
.getSuccessorWeight(1), 11u);
995 EXPECT_EQ(*SIW
.getSuccessorWeight(2), 22u);
998 { // Create another wrapper and check that the data persist.
999 SwitchInstProfUpdateWrapper
SIW(*SI
);
1000 EXPECT_EQ(*SIW
.getSuccessorWeight(0), 99u);
1001 EXPECT_EQ(*SIW
.getSuccessorWeight(1), 11u);
1002 EXPECT_EQ(*SIW
.getSuccessorWeight(2), 22u);
1006 TEST(InstructionsTest
, CommuteShuffleMask
) {
1007 SmallVector
<int, 16> Indices({-1, 0, 7});
1008 ShuffleVectorInst::commuteShuffleMask(Indices
, 4);
1009 EXPECT_THAT(Indices
, testing::ContainerEq(ArrayRef
<int>({-1, 4, 3})));
1012 TEST(InstructionsTest
, ShuffleMaskQueries
) {
1013 // Create the elements for various constant vectors.
1015 Type
*Int32Ty
= Type::getInt32Ty(Ctx
);
1016 Constant
*CU
= UndefValue::get(Int32Ty
);
1017 Constant
*C0
= ConstantInt::get(Int32Ty
, 0);
1018 Constant
*C1
= ConstantInt::get(Int32Ty
, 1);
1019 Constant
*C2
= ConstantInt::get(Int32Ty
, 2);
1020 Constant
*C3
= ConstantInt::get(Int32Ty
, 3);
1021 Constant
*C4
= ConstantInt::get(Int32Ty
, 4);
1022 Constant
*C5
= ConstantInt::get(Int32Ty
, 5);
1023 Constant
*C6
= ConstantInt::get(Int32Ty
, 6);
1024 Constant
*C7
= ConstantInt::get(Int32Ty
, 7);
1026 Constant
*Identity
= ConstantVector::get({C0
, CU
, C2
, C3
, C4
});
1027 EXPECT_TRUE(ShuffleVectorInst::isIdentityMask(
1028 Identity
, cast
<FixedVectorType
>(Identity
->getType())->getNumElements()));
1029 EXPECT_FALSE(ShuffleVectorInst::isSelectMask(
1031 cast
<FixedVectorType
>(Identity
->getType())
1032 ->getNumElements())); // identity is distinguished from select
1033 EXPECT_FALSE(ShuffleVectorInst::isReverseMask(
1034 Identity
, cast
<FixedVectorType
>(Identity
->getType())->getNumElements()));
1035 EXPECT_TRUE(ShuffleVectorInst::isSingleSourceMask(
1036 Identity
, cast
<FixedVectorType
>(Identity
->getType())
1037 ->getNumElements())); // identity is always single source
1038 EXPECT_FALSE(ShuffleVectorInst::isZeroEltSplatMask(
1039 Identity
, cast
<FixedVectorType
>(Identity
->getType())->getNumElements()));
1040 EXPECT_FALSE(ShuffleVectorInst::isTransposeMask(
1041 Identity
, cast
<FixedVectorType
>(Identity
->getType())->getNumElements()));
1043 Constant
*Select
= ConstantVector::get({CU
, C1
, C5
});
1044 EXPECT_FALSE(ShuffleVectorInst::isIdentityMask(
1045 Select
, cast
<FixedVectorType
>(Select
->getType())->getNumElements()));
1046 EXPECT_TRUE(ShuffleVectorInst::isSelectMask(
1047 Select
, cast
<FixedVectorType
>(Select
->getType())->getNumElements()));
1048 EXPECT_FALSE(ShuffleVectorInst::isReverseMask(
1049 Select
, cast
<FixedVectorType
>(Select
->getType())->getNumElements()));
1050 EXPECT_FALSE(ShuffleVectorInst::isSingleSourceMask(
1051 Select
, cast
<FixedVectorType
>(Select
->getType())->getNumElements()));
1052 EXPECT_FALSE(ShuffleVectorInst::isZeroEltSplatMask(
1053 Select
, cast
<FixedVectorType
>(Select
->getType())->getNumElements()));
1054 EXPECT_FALSE(ShuffleVectorInst::isTransposeMask(
1055 Select
, cast
<FixedVectorType
>(Select
->getType())->getNumElements()));
1057 Constant
*Reverse
= ConstantVector::get({C3
, C2
, C1
, CU
});
1058 EXPECT_FALSE(ShuffleVectorInst::isIdentityMask(
1059 Reverse
, cast
<FixedVectorType
>(Reverse
->getType())->getNumElements()));
1060 EXPECT_FALSE(ShuffleVectorInst::isSelectMask(
1061 Reverse
, cast
<FixedVectorType
>(Reverse
->getType())->getNumElements()));
1062 EXPECT_TRUE(ShuffleVectorInst::isReverseMask(
1063 Reverse
, cast
<FixedVectorType
>(Reverse
->getType())->getNumElements()));
1064 EXPECT_TRUE(ShuffleVectorInst::isSingleSourceMask(
1065 Reverse
, cast
<FixedVectorType
>(Reverse
->getType())
1066 ->getNumElements())); // reverse is always single source
1067 EXPECT_FALSE(ShuffleVectorInst::isZeroEltSplatMask(
1068 Reverse
, cast
<FixedVectorType
>(Reverse
->getType())->getNumElements()));
1069 EXPECT_FALSE(ShuffleVectorInst::isTransposeMask(
1070 Reverse
, cast
<FixedVectorType
>(Reverse
->getType())->getNumElements()));
1072 Constant
*SingleSource
= ConstantVector::get({C2
, C2
, C0
, CU
});
1073 EXPECT_FALSE(ShuffleVectorInst::isIdentityMask(
1075 cast
<FixedVectorType
>(SingleSource
->getType())->getNumElements()));
1076 EXPECT_FALSE(ShuffleVectorInst::isSelectMask(
1078 cast
<FixedVectorType
>(SingleSource
->getType())->getNumElements()));
1079 EXPECT_FALSE(ShuffleVectorInst::isReverseMask(
1081 cast
<FixedVectorType
>(SingleSource
->getType())->getNumElements()));
1082 EXPECT_TRUE(ShuffleVectorInst::isSingleSourceMask(
1084 cast
<FixedVectorType
>(SingleSource
->getType())->getNumElements()));
1085 EXPECT_FALSE(ShuffleVectorInst::isZeroEltSplatMask(
1087 cast
<FixedVectorType
>(SingleSource
->getType())->getNumElements()));
1088 EXPECT_FALSE(ShuffleVectorInst::isTransposeMask(
1090 cast
<FixedVectorType
>(SingleSource
->getType())->getNumElements()));
1092 Constant
*ZeroEltSplat
= ConstantVector::get({C0
, C0
, CU
, C0
});
1093 EXPECT_FALSE(ShuffleVectorInst::isIdentityMask(
1095 cast
<FixedVectorType
>(ZeroEltSplat
->getType())->getNumElements()));
1096 EXPECT_FALSE(ShuffleVectorInst::isSelectMask(
1098 cast
<FixedVectorType
>(ZeroEltSplat
->getType())->getNumElements()));
1099 EXPECT_FALSE(ShuffleVectorInst::isReverseMask(
1101 cast
<FixedVectorType
>(ZeroEltSplat
->getType())->getNumElements()));
1102 EXPECT_TRUE(ShuffleVectorInst::isSingleSourceMask(
1103 ZeroEltSplat
, cast
<FixedVectorType
>(ZeroEltSplat
->getType())
1104 ->getNumElements())); // 0-splat is always single source
1105 EXPECT_TRUE(ShuffleVectorInst::isZeroEltSplatMask(
1107 cast
<FixedVectorType
>(ZeroEltSplat
->getType())->getNumElements()));
1108 EXPECT_FALSE(ShuffleVectorInst::isTransposeMask(
1110 cast
<FixedVectorType
>(ZeroEltSplat
->getType())->getNumElements()));
1112 Constant
*Transpose
= ConstantVector::get({C0
, C4
, C2
, C6
});
1113 EXPECT_FALSE(ShuffleVectorInst::isIdentityMask(
1115 cast
<FixedVectorType
>(Transpose
->getType())->getNumElements()));
1116 EXPECT_FALSE(ShuffleVectorInst::isSelectMask(
1118 cast
<FixedVectorType
>(Transpose
->getType())->getNumElements()));
1119 EXPECT_FALSE(ShuffleVectorInst::isReverseMask(
1121 cast
<FixedVectorType
>(Transpose
->getType())->getNumElements()));
1122 EXPECT_FALSE(ShuffleVectorInst::isSingleSourceMask(
1124 cast
<FixedVectorType
>(Transpose
->getType())->getNumElements()));
1125 EXPECT_FALSE(ShuffleVectorInst::isZeroEltSplatMask(
1127 cast
<FixedVectorType
>(Transpose
->getType())->getNumElements()));
1128 EXPECT_TRUE(ShuffleVectorInst::isTransposeMask(
1130 cast
<FixedVectorType
>(Transpose
->getType())->getNumElements()));
1132 // More tests to make sure the logic is/stays correct...
1133 EXPECT_TRUE(ShuffleVectorInst::isIdentityMask(
1134 ConstantVector::get({CU
, C1
, CU
, C3
}), 4));
1135 EXPECT_TRUE(ShuffleVectorInst::isIdentityMask(
1136 ConstantVector::get({C4
, CU
, C6
, CU
}), 4));
1138 EXPECT_TRUE(ShuffleVectorInst::isSelectMask(
1139 ConstantVector::get({C4
, C1
, C6
, CU
}), 4));
1140 EXPECT_TRUE(ShuffleVectorInst::isSelectMask(
1141 ConstantVector::get({CU
, C1
, C6
, C3
}), 4));
1143 EXPECT_TRUE(ShuffleVectorInst::isReverseMask(
1144 ConstantVector::get({C7
, C6
, CU
, C4
}), 4));
1145 EXPECT_TRUE(ShuffleVectorInst::isReverseMask(
1146 ConstantVector::get({C3
, CU
, C1
, CU
}), 4));
1148 EXPECT_TRUE(ShuffleVectorInst::isSingleSourceMask(
1149 ConstantVector::get({C7
, C5
, CU
, C7
}), 4));
1150 EXPECT_TRUE(ShuffleVectorInst::isSingleSourceMask(
1151 ConstantVector::get({C3
, C0
, CU
, C3
}), 4));
1153 EXPECT_TRUE(ShuffleVectorInst::isZeroEltSplatMask(
1154 ConstantVector::get({C4
, CU
, CU
, C4
}), 4));
1155 EXPECT_TRUE(ShuffleVectorInst::isZeroEltSplatMask(
1156 ConstantVector::get({CU
, C0
, CU
, C0
}), 4));
1158 EXPECT_TRUE(ShuffleVectorInst::isTransposeMask(
1159 ConstantVector::get({C1
, C5
, C3
, C7
}), 4));
1161 ShuffleVectorInst::isTransposeMask(ConstantVector::get({C1
, C3
}), 2));
1163 // Nothing special about the values here - just re-using inputs to reduce code.
1164 Constant
*V0
= ConstantVector::get({C0
, C1
, C2
, C3
});
1165 Constant
*V1
= ConstantVector::get({C3
, C2
, C1
, C0
});
1167 // Identity with undef elts.
1168 ShuffleVectorInst
*Id1
= new ShuffleVectorInst(V0
, V1
,
1169 ConstantVector::get({C0
, C1
, CU
, CU
}));
1170 EXPECT_TRUE(Id1
->isIdentity());
1171 EXPECT_FALSE(Id1
->isIdentityWithPadding());
1172 EXPECT_FALSE(Id1
->isIdentityWithExtract());
1173 EXPECT_FALSE(Id1
->isConcat());
1176 // Result has less elements than operands.
1177 ShuffleVectorInst
*Id2
= new ShuffleVectorInst(V0
, V1
,
1178 ConstantVector::get({C0
, C1
, C2
}));
1179 EXPECT_FALSE(Id2
->isIdentity());
1180 EXPECT_FALSE(Id2
->isIdentityWithPadding());
1181 EXPECT_TRUE(Id2
->isIdentityWithExtract());
1182 EXPECT_FALSE(Id2
->isConcat());
1185 // Result has less elements than operands; choose from Op1.
1186 ShuffleVectorInst
*Id3
= new ShuffleVectorInst(V0
, V1
,
1187 ConstantVector::get({C4
, CU
, C6
}));
1188 EXPECT_FALSE(Id3
->isIdentity());
1189 EXPECT_FALSE(Id3
->isIdentityWithPadding());
1190 EXPECT_TRUE(Id3
->isIdentityWithExtract());
1191 EXPECT_FALSE(Id3
->isConcat());
1194 // Result has less elements than operands; choose from Op0 and Op1 is not identity.
1195 ShuffleVectorInst
*Id4
= new ShuffleVectorInst(V0
, V1
,
1196 ConstantVector::get({C4
, C1
, C6
}));
1197 EXPECT_FALSE(Id4
->isIdentity());
1198 EXPECT_FALSE(Id4
->isIdentityWithPadding());
1199 EXPECT_FALSE(Id4
->isIdentityWithExtract());
1200 EXPECT_FALSE(Id4
->isConcat());
1203 // Result has more elements than operands, and extra elements are undef.
1204 ShuffleVectorInst
*Id5
= new ShuffleVectorInst(V0
, V1
,
1205 ConstantVector::get({CU
, C1
, C2
, C3
, CU
, CU
}));
1206 EXPECT_FALSE(Id5
->isIdentity());
1207 EXPECT_TRUE(Id5
->isIdentityWithPadding());
1208 EXPECT_FALSE(Id5
->isIdentityWithExtract());
1209 EXPECT_FALSE(Id5
->isConcat());
1212 // Result has more elements than operands, and extra elements are undef; choose from Op1.
1213 ShuffleVectorInst
*Id6
= new ShuffleVectorInst(V0
, V1
,
1214 ConstantVector::get({C4
, C5
, C6
, CU
, CU
, CU
}));
1215 EXPECT_FALSE(Id6
->isIdentity());
1216 EXPECT_TRUE(Id6
->isIdentityWithPadding());
1217 EXPECT_FALSE(Id6
->isIdentityWithExtract());
1218 EXPECT_FALSE(Id6
->isConcat());
1221 // Result has more elements than operands, but extra elements are not undef.
1222 ShuffleVectorInst
*Id7
= new ShuffleVectorInst(V0
, V1
,
1223 ConstantVector::get({C0
, C1
, C2
, C3
, CU
, C1
}));
1224 EXPECT_FALSE(Id7
->isIdentity());
1225 EXPECT_FALSE(Id7
->isIdentityWithPadding());
1226 EXPECT_FALSE(Id7
->isIdentityWithExtract());
1227 EXPECT_FALSE(Id7
->isConcat());
1230 // Result has more elements than operands; choose from Op0 and Op1 is not identity.
1231 ShuffleVectorInst
*Id8
= new ShuffleVectorInst(V0
, V1
,
1232 ConstantVector::get({C4
, CU
, C2
, C3
, CU
, CU
}));
1233 EXPECT_FALSE(Id8
->isIdentity());
1234 EXPECT_FALSE(Id8
->isIdentityWithPadding());
1235 EXPECT_FALSE(Id8
->isIdentityWithExtract());
1236 EXPECT_FALSE(Id8
->isConcat());
1239 // Result has twice as many elements as operands; choose consecutively from Op0 and Op1 is concat.
1240 ShuffleVectorInst
*Id9
= new ShuffleVectorInst(V0
, V1
,
1241 ConstantVector::get({C0
, CU
, C2
, C3
, CU
, CU
, C6
, C7
}));
1242 EXPECT_FALSE(Id9
->isIdentity());
1243 EXPECT_FALSE(Id9
->isIdentityWithPadding());
1244 EXPECT_FALSE(Id9
->isIdentityWithExtract());
1245 EXPECT_TRUE(Id9
->isConcat());
1248 // Result has less than twice as many elements as operands, so not a concat.
1249 ShuffleVectorInst
*Id10
= new ShuffleVectorInst(V0
, V1
,
1250 ConstantVector::get({C0
, CU
, C2
, C3
, CU
, CU
, C6
}));
1251 EXPECT_FALSE(Id10
->isIdentity());
1252 EXPECT_FALSE(Id10
->isIdentityWithPadding());
1253 EXPECT_FALSE(Id10
->isIdentityWithExtract());
1254 EXPECT_FALSE(Id10
->isConcat());
1257 // Result has more than twice as many elements as operands, so not a concat.
1258 ShuffleVectorInst
*Id11
= new ShuffleVectorInst(V0
, V1
,
1259 ConstantVector::get({C0
, CU
, C2
, C3
, CU
, CU
, C6
, C7
, CU
}));
1260 EXPECT_FALSE(Id11
->isIdentity());
1261 EXPECT_FALSE(Id11
->isIdentityWithPadding());
1262 EXPECT_FALSE(Id11
->isIdentityWithExtract());
1263 EXPECT_FALSE(Id11
->isConcat());
1266 // If an input is undef, it's not a concat.
1267 // TODO: IdentityWithPadding should be true here even though the high mask values are not undef.
1268 ShuffleVectorInst
*Id12
= new ShuffleVectorInst(V0
, ConstantVector::get({CU
, CU
, CU
, CU
}),
1269 ConstantVector::get({C0
, CU
, C2
, C3
, CU
, CU
, C6
, C7
}));
1270 EXPECT_FALSE(Id12
->isIdentity());
1271 EXPECT_FALSE(Id12
->isIdentityWithPadding());
1272 EXPECT_FALSE(Id12
->isIdentityWithExtract());
1273 EXPECT_FALSE(Id12
->isConcat());
1276 // Not possible to express shuffle mask for scalable vector for extract
1278 Type
*VScaleV4Int32Ty
= ScalableVectorType::get(Int32Ty
, 4);
1279 ShuffleVectorInst
*Id13
=
1280 new ShuffleVectorInst(Constant::getAllOnesValue(VScaleV4Int32Ty
),
1281 UndefValue::get(VScaleV4Int32Ty
),
1282 Constant::getNullValue(VScaleV4Int32Ty
));
1284 EXPECT_FALSE(Id13
->isExtractSubvectorMask(Index
));
1285 EXPECT_FALSE(Id13
->changesLength());
1286 EXPECT_FALSE(Id13
->increasesLength());
1289 // Result has twice as many operands.
1290 Type
*VScaleV2Int32Ty
= ScalableVectorType::get(Int32Ty
, 2);
1291 ShuffleVectorInst
*Id14
=
1292 new ShuffleVectorInst(Constant::getAllOnesValue(VScaleV2Int32Ty
),
1293 UndefValue::get(VScaleV2Int32Ty
),
1294 Constant::getNullValue(VScaleV4Int32Ty
));
1295 EXPECT_TRUE(Id14
->changesLength());
1296 EXPECT_TRUE(Id14
->increasesLength());
1299 // Not possible to express these masks for scalable vectors, make sure we
1301 ShuffleVectorInst
*Id15
=
1302 new ShuffleVectorInst(Constant::getAllOnesValue(VScaleV2Int32Ty
),
1303 Constant::getNullValue(VScaleV2Int32Ty
),
1304 Constant::getNullValue(VScaleV2Int32Ty
));
1305 EXPECT_FALSE(Id15
->isIdentityWithPadding());
1306 EXPECT_FALSE(Id15
->isIdentityWithExtract());
1307 EXPECT_FALSE(Id15
->isConcat());
1311 TEST(InstructionsTest
, ShuffleMaskIsReplicationMask
) {
1312 for (int ReplicationFactor
: seq_inclusive(1, 8)) {
1313 for (int VF
: seq_inclusive(1, 8)) {
1314 const auto ReplicatedMask
= createReplicatedMask(ReplicationFactor
, VF
);
1315 int GuessedReplicationFactor
= -1, GuessedVF
= -1;
1316 EXPECT_TRUE(ShuffleVectorInst::isReplicationMask(
1317 ReplicatedMask
, GuessedReplicationFactor
, GuessedVF
));
1318 EXPECT_EQ(GuessedReplicationFactor
, ReplicationFactor
);
1319 EXPECT_EQ(GuessedVF
, VF
);
1321 for (int OpVF
: seq_inclusive(VF
, 2 * VF
+ 1)) {
1323 Type
*OpVFTy
= FixedVectorType::get(IntegerType::getInt1Ty(Ctx
), OpVF
);
1324 Value
*Op
= ConstantVector::getNullValue(OpVFTy
);
1325 ShuffleVectorInst
*SVI
= new ShuffleVectorInst(Op
, Op
, ReplicatedMask
);
1326 EXPECT_EQ(SVI
->isReplicationMask(GuessedReplicationFactor
, GuessedVF
),
1334 TEST(InstructionsTest
, ShuffleMaskIsReplicationMask_undef
) {
1335 for (int ReplicationFactor
: seq_inclusive(1, 4)) {
1336 for (int VF
: seq_inclusive(1, 4)) {
1337 const auto ReplicatedMask
= createReplicatedMask(ReplicationFactor
, VF
);
1338 int GuessedReplicationFactor
= -1, GuessedVF
= -1;
1340 // If we change some mask elements to undef, we should still match.
1342 SmallVector
<SmallVector
<bool>> ElementChoices(ReplicatedMask
.size(),
1345 CombinationGenerator
<bool, decltype(ElementChoices
)::value_type
,
1346 /*variable_smallsize=*/4>
1349 G
.generate([&](ArrayRef
<bool> UndefOverrides
) -> bool {
1350 SmallVector
<int> AdjustedMask
;
1351 AdjustedMask
.reserve(ReplicatedMask
.size());
1352 for (auto I
: zip(ReplicatedMask
, UndefOverrides
))
1353 AdjustedMask
.emplace_back(std::get
<1>(I
) ? -1 : std::get
<0>(I
));
1354 assert(AdjustedMask
.size() == ReplicatedMask
.size() &&
1355 "Size misprediction");
1357 EXPECT_TRUE(ShuffleVectorInst::isReplicationMask(
1358 AdjustedMask
, GuessedReplicationFactor
, GuessedVF
));
1359 // Do not check GuessedReplicationFactor and GuessedVF,
1360 // with enough undef's we may deduce a different tuple.
1362 return /*Abort=*/false;
1368 TEST(InstructionsTest
, ShuffleMaskIsReplicationMask_Exhaustive_Correctness
) {
1369 for (int ShufMaskNumElts
: seq_inclusive(1, 6)) {
1370 SmallVector
<int> PossibleShufMaskElts
;
1371 PossibleShufMaskElts
.reserve(ShufMaskNumElts
+ 2);
1372 for (int PossibleShufMaskElt
: seq_inclusive(-1, ShufMaskNumElts
))
1373 PossibleShufMaskElts
.emplace_back(PossibleShufMaskElt
);
1374 assert(PossibleShufMaskElts
.size() == ShufMaskNumElts
+ 2U &&
1375 "Size misprediction");
1377 SmallVector
<SmallVector
<int>> ElementChoices(ShufMaskNumElts
,
1378 PossibleShufMaskElts
);
1380 CombinationGenerator
<int, decltype(ElementChoices
)::value_type
,
1381 /*variable_smallsize=*/4>
1384 G
.generate([&](ArrayRef
<int> Mask
) -> bool {
1385 int GuessedReplicationFactor
= -1, GuessedVF
= -1;
1386 bool Match
= ShuffleVectorInst::isReplicationMask(
1387 Mask
, GuessedReplicationFactor
, GuessedVF
);
1389 return /*Abort=*/false;
1391 const auto ActualMask
=
1392 createReplicatedMask(GuessedReplicationFactor
, GuessedVF
);
1393 EXPECT_EQ(Mask
.size(), ActualMask
.size());
1394 for (auto I
: zip(Mask
, ActualMask
)) {
1395 int Elt
= std::get
<0>(I
);
1396 int ActualElt
= std::get
<0>(I
);
1399 EXPECT_EQ(Elt
, ActualElt
);
1403 return /*Abort=*/false;
1408 TEST(InstructionsTest
, GetSplat
) {
1409 // Create the elements for various constant vectors.
1411 Type
*Int32Ty
= Type::getInt32Ty(Ctx
);
1412 Constant
*CU
= UndefValue::get(Int32Ty
);
1413 Constant
*C0
= ConstantInt::get(Int32Ty
, 0);
1414 Constant
*C1
= ConstantInt::get(Int32Ty
, 1);
1416 Constant
*Splat0
= ConstantVector::get({C0
, C0
, C0
, C0
});
1417 Constant
*Splat1
= ConstantVector::get({C1
, C1
, C1
, C1
,C1
});
1418 Constant
*Splat0Undef
= ConstantVector::get({C0
, CU
, C0
, CU
});
1419 Constant
*Splat1Undef
= ConstantVector::get({CU
, CU
, C1
, CU
});
1420 Constant
*NotSplat
= ConstantVector::get({C1
, C1
, C0
, C1
,C1
});
1421 Constant
*NotSplatUndef
= ConstantVector::get({CU
, C1
, CU
, CU
,C0
});
1423 // Default - undefs are not allowed.
1424 EXPECT_EQ(Splat0
->getSplatValue(), C0
);
1425 EXPECT_EQ(Splat1
->getSplatValue(), C1
);
1426 EXPECT_EQ(Splat0Undef
->getSplatValue(), nullptr);
1427 EXPECT_EQ(Splat1Undef
->getSplatValue(), nullptr);
1428 EXPECT_EQ(NotSplat
->getSplatValue(), nullptr);
1429 EXPECT_EQ(NotSplatUndef
->getSplatValue(), nullptr);
1431 // Disallow undefs explicitly.
1432 EXPECT_EQ(Splat0
->getSplatValue(false), C0
);
1433 EXPECT_EQ(Splat1
->getSplatValue(false), C1
);
1434 EXPECT_EQ(Splat0Undef
->getSplatValue(false), nullptr);
1435 EXPECT_EQ(Splat1Undef
->getSplatValue(false), nullptr);
1436 EXPECT_EQ(NotSplat
->getSplatValue(false), nullptr);
1437 EXPECT_EQ(NotSplatUndef
->getSplatValue(false), nullptr);
1440 EXPECT_EQ(Splat0
->getSplatValue(true), C0
);
1441 EXPECT_EQ(Splat1
->getSplatValue(true), C1
);
1442 EXPECT_EQ(Splat0Undef
->getSplatValue(true), C0
);
1443 EXPECT_EQ(Splat1Undef
->getSplatValue(true), C1
);
1444 EXPECT_EQ(NotSplat
->getSplatValue(true), nullptr);
1445 EXPECT_EQ(NotSplatUndef
->getSplatValue(true), nullptr);
1448 TEST(InstructionsTest
, SkipDebug
) {
1450 std::unique_ptr
<Module
> M
= parseIR(C
,
1452 declare void @llvm.dbg.value(metadata, metadata, metadata)
1456 call void @llvm.dbg.value(metadata i32 0, metadata !11, metadata !DIExpression()), !dbg !13
1460 !llvm.dbg.cu = !{!0}
1461 !llvm.module.flags = !{!3, !4}
1462 !0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version
6.0.0", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
1463 !1 = !DIFile(filename: "t2
.c
", directory: "foo
")
1465 !3 = !{i32 2, !"Dwarf Version
", i32 4}
1466 !4 = !{i32 2, !"Debug Info Version
", i32 3}
1467 !8 = distinct !DISubprogram(name: "f
", scope: !1, file: !1, line: 1, type: !9, isLocal: false, isDefinition: true, scopeLine: 1, isOptimized: false, unit: !0, retainedNodes: !2)
1468 !9 = !DISubroutineType(types: !10)
1470 !11 = !DILocalVariable(name: "x
", scope: !8, file: !1, line: 2, type: !12)
1471 !12 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
1472 !13 = !DILocation(line: 2, column: 7, scope: !8)
1475 Function
*F
= cast
<Function
>(M
->getNamedValue("f"));
1476 BasicBlock
&BB
= F
->front();
1478 // The first non-debug instruction is the terminator.
1479 auto *Term
= BB
.getTerminator();
1480 EXPECT_EQ(Term
, BB
.begin()->getNextNonDebugInstruction());
1481 EXPECT_EQ(Term
->getIterator(), skipDebugIntrinsics(BB
.begin()));
1483 // After the terminator, there are no non-debug instructions.
1484 EXPECT_EQ(nullptr, Term
->getNextNonDebugInstruction());
1487 TEST(InstructionsTest
, PhiMightNotBeFPMathOperator
) {
1488 LLVMContext Context
;
1489 IRBuilder
<> Builder(Context
);
1490 MDBuilder
MDHelper(Context
);
1491 Instruction
*I
= Builder
.CreatePHI(Builder
.getInt32Ty(), 0);
1492 EXPECT_FALSE(isa
<FPMathOperator
>(I
));
1494 Instruction
*FP
= Builder
.CreatePHI(Builder
.getDoubleTy(), 0);
1495 EXPECT_TRUE(isa
<FPMathOperator
>(FP
));
1499 TEST(InstructionsTest
, FPCallIsFPMathOperator
) {
1502 Type
*ITy
= Type::getInt32Ty(C
);
1503 FunctionType
*IFnTy
= FunctionType::get(ITy
, {});
1504 Value
*ICallee
= Constant::getNullValue(IFnTy
->getPointerTo());
1505 std::unique_ptr
<CallInst
> ICall(CallInst::Create(IFnTy
, ICallee
, {}, ""));
1506 EXPECT_FALSE(isa
<FPMathOperator
>(ICall
));
1508 Type
*VITy
= FixedVectorType::get(ITy
, 2);
1509 FunctionType
*VIFnTy
= FunctionType::get(VITy
, {});
1510 Value
*VICallee
= Constant::getNullValue(VIFnTy
->getPointerTo());
1511 std::unique_ptr
<CallInst
> VICall(CallInst::Create(VIFnTy
, VICallee
, {}, ""));
1512 EXPECT_FALSE(isa
<FPMathOperator
>(VICall
));
1514 Type
*AITy
= ArrayType::get(ITy
, 2);
1515 FunctionType
*AIFnTy
= FunctionType::get(AITy
, {});
1516 Value
*AICallee
= Constant::getNullValue(AIFnTy
->getPointerTo());
1517 std::unique_ptr
<CallInst
> AICall(CallInst::Create(AIFnTy
, AICallee
, {}, ""));
1518 EXPECT_FALSE(isa
<FPMathOperator
>(AICall
));
1520 Type
*FTy
= Type::getFloatTy(C
);
1521 FunctionType
*FFnTy
= FunctionType::get(FTy
, {});
1522 Value
*FCallee
= Constant::getNullValue(FFnTy
->getPointerTo());
1523 std::unique_ptr
<CallInst
> FCall(CallInst::Create(FFnTy
, FCallee
, {}, ""));
1524 EXPECT_TRUE(isa
<FPMathOperator
>(FCall
));
1526 Type
*VFTy
= FixedVectorType::get(FTy
, 2);
1527 FunctionType
*VFFnTy
= FunctionType::get(VFTy
, {});
1528 Value
*VFCallee
= Constant::getNullValue(VFFnTy
->getPointerTo());
1529 std::unique_ptr
<CallInst
> VFCall(CallInst::Create(VFFnTy
, VFCallee
, {}, ""));
1530 EXPECT_TRUE(isa
<FPMathOperator
>(VFCall
));
1532 Type
*AFTy
= ArrayType::get(FTy
, 2);
1533 FunctionType
*AFFnTy
= FunctionType::get(AFTy
, {});
1534 Value
*AFCallee
= Constant::getNullValue(AFFnTy
->getPointerTo());
1535 std::unique_ptr
<CallInst
> AFCall(CallInst::Create(AFFnTy
, AFCallee
, {}, ""));
1536 EXPECT_TRUE(isa
<FPMathOperator
>(AFCall
));
1538 Type
*AVFTy
= ArrayType::get(VFTy
, 2);
1539 FunctionType
*AVFFnTy
= FunctionType::get(AVFTy
, {});
1540 Value
*AVFCallee
= Constant::getNullValue(AVFFnTy
->getPointerTo());
1541 std::unique_ptr
<CallInst
> AVFCall(
1542 CallInst::Create(AVFFnTy
, AVFCallee
, {}, ""));
1543 EXPECT_TRUE(isa
<FPMathOperator
>(AVFCall
));
1545 Type
*AAVFTy
= ArrayType::get(AVFTy
, 2);
1546 FunctionType
*AAVFFnTy
= FunctionType::get(AAVFTy
, {});
1547 Value
*AAVFCallee
= Constant::getNullValue(AAVFFnTy
->getPointerTo());
1548 std::unique_ptr
<CallInst
> AAVFCall(
1549 CallInst::Create(AAVFFnTy
, AAVFCallee
, {}, ""));
1550 EXPECT_TRUE(isa
<FPMathOperator
>(AAVFCall
));
1553 TEST(InstructionsTest
, FNegInstruction
) {
1554 LLVMContext Context
;
1555 Type
*FltTy
= Type::getFloatTy(Context
);
1556 Constant
*One
= ConstantFP::get(FltTy
, 1.0);
1557 BinaryOperator
*FAdd
= BinaryOperator::CreateFAdd(One
, One
);
1558 FAdd
->setHasNoNaNs(true);
1559 UnaryOperator
*FNeg
= UnaryOperator::CreateFNegFMF(One
, FAdd
);
1560 EXPECT_TRUE(FNeg
->hasNoNaNs());
1561 EXPECT_FALSE(FNeg
->hasNoInfs());
1562 EXPECT_FALSE(FNeg
->hasNoSignedZeros());
1563 EXPECT_FALSE(FNeg
->hasAllowReciprocal());
1564 EXPECT_FALSE(FNeg
->hasAllowContract());
1565 EXPECT_FALSE(FNeg
->hasAllowReassoc());
1566 EXPECT_FALSE(FNeg
->hasApproxFunc());
1567 FAdd
->deleteValue();
1568 FNeg
->deleteValue();
1571 TEST(InstructionsTest
, CallBrInstruction
) {
1572 LLVMContext Context
;
1573 std::unique_ptr
<Module
> M
= parseIR(Context
, R
"(
1574 define void @foo() {
1576 callbr void asm sideeffect "// XXX: ${0:l}", "!i"()
1577 to label
%land
.rhs
.i
[label
%branch_test
.exit
]
1580 br label
%branch_test
.exit
1583 %0 = phi i1
[ true, %entry
], [ false, %land
.rhs
.i
]
1584 br i1
%0, label
%if.end
, label
%if.then
1593 Function *Foo = M->getFunction("foo
");
1594 auto BBs = Foo->begin();
1595 CallBrInst &CBI = cast<CallBrInst>(BBs->front());
1598 BasicBlock &BranchTestExit = *BBs;
1600 BasicBlock &IfThen = *BBs;
1602 // Test that setting the first indirect destination of callbr updates the dest
1603 EXPECT_EQ(&BranchTestExit, CBI.getIndirectDest(0));
1604 CBI.setIndirectDest(0, &IfThen);
1605 EXPECT_EQ(&IfThen, CBI.getIndirectDest(0));
1608 TEST(InstructionsTest, UnaryOperator) {
1609 LLVMContext Context;
1610 IRBuilder<> Builder(Context);
1611 Instruction *I = Builder.CreatePHI(Builder.getDoubleTy(), 0);
1612 Value *F = Builder.CreateFNeg(I);
1614 EXPECT_TRUE(isa<Value>(F));
1615 EXPECT_TRUE(isa<Instruction>(F));
1616 EXPECT_TRUE(isa<UnaryInstruction>(F));
1617 EXPECT_TRUE(isa<UnaryOperator>(F));
1618 EXPECT_FALSE(isa<BinaryOperator>(F));
1624 TEST(InstructionsTest, DropLocation) {
1626 std::unique_ptr<Module> M = parseIR(C,
1628 declare
void @
callee()
1630 define
void @
no_parent_scope() {
1631 call
void @
callee() ; I1
: Call with no location
.
1632 call
void @
callee(), !dbg
!11 ; I2
: Call with location
.
1633 ret
void, !dbg
!11 ; I3
: Non
-call with location
.
1636 define
void @
with_parent_scope() !dbg
!8 {
1637 call
void @
callee() ; I1
: Call with no location
.
1638 call
void @
callee(), !dbg
!11 ; I2
: Call with location
.
1639 ret
void, !dbg
!11 ; I3
: Non
-call with location
.
1642 !llvm
.dbg
.cu
= !{!0}
1643 !llvm
.module
.flags
= !{!3, !4}
1644 !0 = distinct
!DICompileUnit(language
: DW_LANG_C99
, file
: !1, producer
: "", isOptimized
: false, runtimeVersion
: 0, emissionKind
: FullDebug
, enums
: !2)
1645 !1 = !DIFile(filename
: "t2.c", directory
: "foo")
1647 !3 = !{i32
2, !"Dwarf Version", i32
4}
1648 !4 = !{i32
2, !"Debug Info Version", i32
3}
1649 !8 = distinct
!DISubprogram(name
: "f", scope
: !1, file
: !1, line
: 1, type
: !9, isLocal
: false, isDefinition
: true, scopeLine
: 1, isOptimized
: false, unit
: !0, retainedNodes
: !2)
1650 !9 = !DISubroutineType(types
: !10)
1652 !11 = !DILocation(line
: 2, column
: 7, scope
: !8, inlinedAt
: !12)
1653 !12 = !DILocation(line
: 3, column
: 8, scope
: !8)
1658 Function *NoParentScopeF =
1659 cast<Function>(M->getNamedValue("no_parent_scope
"));
1660 BasicBlock &BB = NoParentScopeF->front();
1662 auto *I1 = BB.getFirstNonPHI();
1663 auto *I2 = I1->getNextNode();
1664 auto *I3 = BB.getTerminator();
1666 EXPECT_EQ(I1->getDebugLoc(), DebugLoc());
1668 EXPECT_EQ(I1->getDebugLoc(), DebugLoc());
1670 EXPECT_EQ(I2->getDebugLoc().getLine(), 2U);
1672 EXPECT_EQ(I1->getDebugLoc(), DebugLoc());
1674 EXPECT_EQ(I3->getDebugLoc().getLine(), 2U);
1676 EXPECT_EQ(I3->getDebugLoc(), DebugLoc());
1680 Function *WithParentScopeF =
1681 cast<Function>(M->getNamedValue("with_parent_scope
"));
1682 BasicBlock &BB = WithParentScopeF->front();
1684 auto *I2 = BB.getFirstNonPHI()->getNextNode();
1686 MDNode *Scope = cast<MDNode>(WithParentScopeF->getSubprogram());
1687 EXPECT_EQ(I2->getDebugLoc().getLine(), 2U);
1689 EXPECT_EQ(I2->getDebugLoc().getLine(), 0U);
1690 EXPECT_EQ(I2->getDebugLoc().getScope(), Scope);
1691 EXPECT_EQ(I2->getDebugLoc().getInlinedAt(), nullptr);
1695 TEST(InstructionsTest, BranchWeightOverflow) {
1697 std::unique_ptr<Module> M = parseIR(C,
1699 declare
void @
callee()
1701 define
void @
caller() {
1702 call
void @
callee(), !prof
!1
1706 !1 = !{!"branch_weights", i32
20000}
1710 cast<CallInst>(&M->getFunction("caller
")->getEntryBlock().front());
1711 uint64_t ProfWeight;
1712 CI->extractProfTotalWeight(ProfWeight);
1713 ASSERT_EQ(ProfWeight, 20000U);
1714 CI->updateProfWeight(10000000, 1);
1715 CI->extractProfTotalWeight(ProfWeight);
1716 ASSERT_EQ(ProfWeight, UINT32_MAX);
1719 TEST(InstructionsTest, AllocaInst) {
1721 std::unique_ptr<Module> M = parseIR(Ctx, R"(
1722 %T
= type
{ i64
, [3 x i32
]}
1723 define
void @
f(i32
%n
) {
1725 %A
= alloca i32
, i32
1
1726 %B
= alloca i32
, i32
4
1727 %C
= alloca i32
, i32
%n
1728 %D
= alloca
<8 x
double>
1729 %E
= alloca
<vscale x
8 x
double>
1730 %F
= alloca
[2 x half
]
1731 %G
= alloca
[2 x
[3 x i128
]]
1736 const DataLayout &DL = M->getDataLayout();
1738 Function *Fun = cast<Function>(M->getNamedValue("f
"));
1739 BasicBlock &BB = Fun->front();
1740 auto It = BB.begin();
1741 AllocaInst &A = cast<AllocaInst>(*It++);
1742 AllocaInst &B = cast<AllocaInst>(*It++);
1743 AllocaInst &C = cast<AllocaInst>(*It++);
1744 AllocaInst &D = cast<AllocaInst>(*It++);
1745 AllocaInst &E = cast<AllocaInst>(*It++);
1746 AllocaInst &F = cast<AllocaInst>(*It++);
1747 AllocaInst &G = cast<AllocaInst>(*It++);
1748 AllocaInst &H = cast<AllocaInst>(*It++);
1749 EXPECT_EQ(A.getAllocationSizeInBits(DL), TypeSize::Fixed(32));
1750 EXPECT_EQ(B.getAllocationSizeInBits(DL), TypeSize::Fixed(128));
1751 EXPECT_FALSE(C.getAllocationSizeInBits(DL));
1752 EXPECT_EQ(D.getAllocationSizeInBits(DL), TypeSize::Fixed(512));
1753 EXPECT_EQ(E.getAllocationSizeInBits(DL), TypeSize::Scalable(512));
1754 EXPECT_EQ(F.getAllocationSizeInBits(DL), TypeSize::Fixed(32));
1755 EXPECT_EQ(G.getAllocationSizeInBits(DL), TypeSize::Fixed(768));
1756 EXPECT_EQ(H.getAllocationSizeInBits(DL), TypeSize::Fixed(160));
1759 TEST(InstructionsTest, InsertAtBegin) {
1761 std::unique_ptr<Module> M = parseIR(Ctx, R"(
1762 define
void @
f(i32
%a
, i32
%b
) {
1767 Function *F = &*M->begin();
1768 Argument *ArgA = F->getArg(0);
1769 Argument *ArgB = F->getArg(1);
1770 BasicBlock *BB = &*F->begin();
1771 Instruction *Ret = &*BB->begin();
1772 Instruction *I = BinaryOperator::CreateAdd(ArgA, ArgB);
1773 auto It = I->insertInto(BB, BB->begin());
1775 EXPECT_EQ(I->getNextNode(), Ret);
1778 TEST(InstructionsTest, InsertAtEnd) {
1780 std::unique_ptr<Module> M = parseIR(Ctx, R"(
1781 define
void @
f(i32
%a
, i32
%b
) {
1786 Function *F = &*M->begin();
1787 Argument *ArgA = F->getArg(0);
1788 Argument *ArgB = F->getArg(1);
1789 BasicBlock *BB = &*F->begin();
1790 Instruction *Ret = &*BB->begin();
1791 Instruction *I = BinaryOperator::CreateAdd(ArgA, ArgB);
1792 auto It = I->insertInto(BB, BB->end());
1794 EXPECT_EQ(Ret->getNextNode(), I);
1797 } // end anonymous namespace
1798 } // end namespace llvm