Fix test failures introduced by PR #113697 (#116941)
[llvm-project.git] / llvm / unittests / IR / InstructionsTest.cpp
blob0af812564c02677ee74b9ad3397ad98ca0c8dfb7
1 //===- llvm/unittest/IR/InstructionsTest.cpp - Instructions unit 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/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/CommandLine.h"
29 #include "llvm/Support/SourceMgr.h"
30 #include "llvm-c/Core.h"
31 #include "gmock/gmock-matchers.h"
32 #include "gtest/gtest.h"
33 #include <memory>
35 extern llvm::cl::opt<bool> UseNewDbgInfoFormat;
37 namespace llvm {
38 namespace {
40 static std::unique_ptr<Module> parseIR(LLVMContext &C, const char *IR) {
41 SMDiagnostic Err;
42 std::unique_ptr<Module> Mod = parseAssemblyString(IR, Err, C);
43 if (!Mod)
44 Err.print("InstructionsTests", errs());
45 return Mod;
48 TEST(InstructionsTest, ReturnInst) {
49 LLVMContext C;
51 // test for PR6589
52 const ReturnInst* r0 = ReturnInst::Create(C);
53 EXPECT_EQ(r0->getNumOperands(), 0U);
54 EXPECT_EQ(r0->op_begin(), r0->op_end());
56 IntegerType* Int1 = IntegerType::get(C, 1);
57 Constant* One = ConstantInt::get(Int1, 1, true);
58 const ReturnInst* r1 = ReturnInst::Create(C, One);
59 EXPECT_EQ(1U, r1->getNumOperands());
60 User::const_op_iterator b(r1->op_begin());
61 EXPECT_NE(r1->op_end(), b);
62 EXPECT_EQ(One, *b);
63 EXPECT_EQ(One, r1->getOperand(0));
64 ++b;
65 EXPECT_EQ(r1->op_end(), b);
67 // clean up
68 delete r0;
69 delete r1;
72 // Test fixture that provides a module and a single function within it. Useful
73 // for tests that need to refer to the function in some way.
74 class ModuleWithFunctionTest : public testing::Test {
75 protected:
76 ModuleWithFunctionTest() : M(new Module("MyModule", Ctx)) {
77 FArgTypes.push_back(Type::getInt8Ty(Ctx));
78 FArgTypes.push_back(Type::getInt32Ty(Ctx));
79 FArgTypes.push_back(Type::getInt64Ty(Ctx));
80 FunctionType *FTy =
81 FunctionType::get(Type::getVoidTy(Ctx), FArgTypes, false);
82 F = Function::Create(FTy, Function::ExternalLinkage, "", M.get());
85 LLVMContext Ctx;
86 std::unique_ptr<Module> M;
87 SmallVector<Type *, 3> FArgTypes;
88 Function *F;
91 TEST_F(ModuleWithFunctionTest, CallInst) {
92 Value *Args[] = {ConstantInt::get(Type::getInt8Ty(Ctx), 20),
93 ConstantInt::get(Type::getInt32Ty(Ctx), 9999),
94 ConstantInt::get(Type::getInt64Ty(Ctx), 42)};
95 std::unique_ptr<CallInst> Call(CallInst::Create(F, Args));
97 // Make sure iteration over a call's arguments works as expected.
98 unsigned Idx = 0;
99 for (Value *Arg : Call->args()) {
100 EXPECT_EQ(FArgTypes[Idx], Arg->getType());
101 EXPECT_EQ(Call->getArgOperand(Idx)->getType(), Arg->getType());
102 Idx++;
105 Call->addRetAttr(Attribute::get(Call->getContext(), "test-str-attr"));
106 EXPECT_TRUE(Call->hasRetAttr("test-str-attr"));
107 EXPECT_FALSE(Call->hasRetAttr("not-on-call"));
109 Call->addFnAttr(Attribute::get(Call->getContext(), "test-str-fn-attr"));
110 ASSERT_TRUE(Call->hasFnAttr("test-str-fn-attr"));
111 Call->removeFnAttr("test-str-fn-attr");
112 EXPECT_FALSE(Call->hasFnAttr("test-str-fn-attr"));
115 TEST_F(ModuleWithFunctionTest, InvokeInst) {
116 BasicBlock *BB1 = BasicBlock::Create(Ctx, "", F);
117 BasicBlock *BB2 = BasicBlock::Create(Ctx, "", F);
119 Value *Args[] = {ConstantInt::get(Type::getInt8Ty(Ctx), 20),
120 ConstantInt::get(Type::getInt32Ty(Ctx), 9999),
121 ConstantInt::get(Type::getInt64Ty(Ctx), 42)};
122 std::unique_ptr<InvokeInst> Invoke(InvokeInst::Create(F, BB1, BB2, Args));
124 // Make sure iteration over invoke's arguments works as expected.
125 unsigned Idx = 0;
126 for (Value *Arg : Invoke->args()) {
127 EXPECT_EQ(FArgTypes[Idx], Arg->getType());
128 EXPECT_EQ(Invoke->getArgOperand(Idx)->getType(), Arg->getType());
129 Idx++;
133 TEST(InstructionsTest, BranchInst) {
134 LLVMContext C;
136 // Make a BasicBlocks
137 BasicBlock* bb0 = BasicBlock::Create(C);
138 BasicBlock* bb1 = BasicBlock::Create(C);
140 // Mandatory BranchInst
141 const BranchInst* b0 = BranchInst::Create(bb0);
143 EXPECT_TRUE(b0->isUnconditional());
144 EXPECT_FALSE(b0->isConditional());
145 EXPECT_EQ(1U, b0->getNumSuccessors());
147 // check num operands
148 EXPECT_EQ(1U, b0->getNumOperands());
150 EXPECT_NE(b0->op_begin(), b0->op_end());
151 EXPECT_EQ(b0->op_end(), std::next(b0->op_begin()));
153 EXPECT_EQ(b0->op_end(), std::next(b0->op_begin()));
155 IntegerType* Int1 = IntegerType::get(C, 1);
156 Constant* One = ConstantInt::get(Int1, 1, true);
158 // Conditional BranchInst
159 BranchInst* b1 = BranchInst::Create(bb0, bb1, One);
161 EXPECT_FALSE(b1->isUnconditional());
162 EXPECT_TRUE(b1->isConditional());
163 EXPECT_EQ(2U, b1->getNumSuccessors());
165 // check num operands
166 EXPECT_EQ(3U, b1->getNumOperands());
168 User::const_op_iterator b(b1->op_begin());
170 // check COND
171 EXPECT_NE(b, b1->op_end());
172 EXPECT_EQ(One, *b);
173 EXPECT_EQ(One, b1->getOperand(0));
174 EXPECT_EQ(One, b1->getCondition());
175 ++b;
177 // check ELSE
178 EXPECT_EQ(bb1, *b);
179 EXPECT_EQ(bb1, b1->getOperand(1));
180 EXPECT_EQ(bb1, b1->getSuccessor(1));
181 ++b;
183 // check THEN
184 EXPECT_EQ(bb0, *b);
185 EXPECT_EQ(bb0, b1->getOperand(2));
186 EXPECT_EQ(bb0, b1->getSuccessor(0));
187 ++b;
189 EXPECT_EQ(b1->op_end(), b);
191 // clean up
192 delete b0;
193 delete b1;
195 delete bb0;
196 delete bb1;
199 TEST(InstructionsTest, CastInst) {
200 LLVMContext C;
202 Type *Int8Ty = Type::getInt8Ty(C);
203 Type *Int16Ty = Type::getInt16Ty(C);
204 Type *Int32Ty = Type::getInt32Ty(C);
205 Type *Int64Ty = Type::getInt64Ty(C);
206 Type *V8x8Ty = FixedVectorType::get(Int8Ty, 8);
207 Type *V8x64Ty = FixedVectorType::get(Int64Ty, 8);
209 Type *HalfTy = Type::getHalfTy(C);
210 Type *FloatTy = Type::getFloatTy(C);
211 Type *DoubleTy = Type::getDoubleTy(C);
213 Type *V2Int32Ty = FixedVectorType::get(Int32Ty, 2);
214 Type *V2Int64Ty = FixedVectorType::get(Int64Ty, 2);
215 Type *V4Int16Ty = FixedVectorType::get(Int16Ty, 4);
216 Type *V1Int16Ty = FixedVectorType::get(Int16Ty, 1);
218 Type *VScaleV2Int32Ty = ScalableVectorType::get(Int32Ty, 2);
219 Type *VScaleV2Int64Ty = ScalableVectorType::get(Int64Ty, 2);
220 Type *VScaleV4Int16Ty = ScalableVectorType::get(Int16Ty, 4);
221 Type *VScaleV1Int16Ty = ScalableVectorType::get(Int16Ty, 1);
223 Type *Int32PtrTy = PointerType::get(Int32Ty, 0);
224 Type *Int64PtrTy = PointerType::get(Int64Ty, 0);
226 Type *Int32PtrAS1Ty = PointerType::get(Int32Ty, 1);
227 Type *Int64PtrAS1Ty = PointerType::get(Int64Ty, 1);
229 Type *V2Int32PtrAS1Ty = FixedVectorType::get(Int32PtrAS1Ty, 2);
230 Type *V2Int64PtrAS1Ty = FixedVectorType::get(Int64PtrAS1Ty, 2);
231 Type *V4Int32PtrAS1Ty = FixedVectorType::get(Int32PtrAS1Ty, 4);
232 Type *VScaleV4Int32PtrAS1Ty = ScalableVectorType::get(Int32PtrAS1Ty, 4);
233 Type *V4Int64PtrAS1Ty = FixedVectorType::get(Int64PtrAS1Ty, 4);
235 Type *V2Int64PtrTy = FixedVectorType::get(Int64PtrTy, 2);
236 Type *V2Int32PtrTy = FixedVectorType::get(Int32PtrTy, 2);
237 Type *VScaleV2Int32PtrTy = ScalableVectorType::get(Int32PtrTy, 2);
238 Type *V4Int32PtrTy = FixedVectorType::get(Int32PtrTy, 4);
239 Type *VScaleV4Int32PtrTy = ScalableVectorType::get(Int32PtrTy, 4);
240 Type *VScaleV4Int64PtrTy = ScalableVectorType::get(Int64PtrTy, 4);
242 const Constant* c8 = Constant::getNullValue(V8x8Ty);
243 const Constant* c64 = Constant::getNullValue(V8x64Ty);
245 const Constant *v2ptr32 = Constant::getNullValue(V2Int32PtrTy);
247 EXPECT_EQ(CastInst::Trunc, CastInst::getCastOpcode(c64, true, V8x8Ty, true));
248 EXPECT_EQ(CastInst::SExt, CastInst::getCastOpcode(c8, true, V8x64Ty, true));
250 EXPECT_FALSE(CastInst::isBitCastable(V8x64Ty, V8x8Ty));
251 EXPECT_FALSE(CastInst::isBitCastable(V8x8Ty, V8x64Ty));
253 // Check address space casts are rejected since we don't know the sizes here
254 EXPECT_FALSE(CastInst::isBitCastable(Int32PtrTy, Int32PtrAS1Ty));
255 EXPECT_FALSE(CastInst::isBitCastable(Int32PtrAS1Ty, Int32PtrTy));
256 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrTy, V2Int32PtrAS1Ty));
257 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V2Int32PtrTy));
258 EXPECT_TRUE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V2Int64PtrAS1Ty));
259 EXPECT_EQ(CastInst::AddrSpaceCast, CastInst::getCastOpcode(v2ptr32, true,
260 V2Int32PtrAS1Ty,
261 true));
263 // Test mismatched number of elements for pointers
264 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V4Int64PtrAS1Ty));
265 EXPECT_FALSE(CastInst::isBitCastable(V4Int64PtrAS1Ty, V2Int32PtrAS1Ty));
266 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V4Int32PtrAS1Ty));
267 EXPECT_FALSE(CastInst::isBitCastable(Int32PtrTy, V2Int32PtrTy));
268 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrTy, Int32PtrTy));
270 EXPECT_TRUE(CastInst::isBitCastable(Int32PtrTy, Int64PtrTy));
271 EXPECT_FALSE(CastInst::isBitCastable(DoubleTy, FloatTy));
272 EXPECT_FALSE(CastInst::isBitCastable(FloatTy, DoubleTy));
273 EXPECT_TRUE(CastInst::isBitCastable(FloatTy, FloatTy));
274 EXPECT_TRUE(CastInst::isBitCastable(FloatTy, FloatTy));
275 EXPECT_TRUE(CastInst::isBitCastable(FloatTy, Int32Ty));
276 EXPECT_TRUE(CastInst::isBitCastable(Int16Ty, HalfTy));
277 EXPECT_TRUE(CastInst::isBitCastable(Int32Ty, FloatTy));
278 EXPECT_TRUE(CastInst::isBitCastable(V2Int32Ty, Int64Ty));
280 EXPECT_TRUE(CastInst::isBitCastable(V2Int32Ty, V4Int16Ty));
281 EXPECT_FALSE(CastInst::isBitCastable(Int32Ty, Int64Ty));
282 EXPECT_FALSE(CastInst::isBitCastable(Int64Ty, Int32Ty));
284 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrTy, Int64Ty));
285 EXPECT_FALSE(CastInst::isBitCastable(Int64Ty, V2Int32PtrTy));
286 EXPECT_TRUE(CastInst::isBitCastable(V2Int64PtrTy, V2Int32PtrTy));
287 EXPECT_TRUE(CastInst::isBitCastable(V2Int32PtrTy, V2Int64PtrTy));
288 EXPECT_FALSE(CastInst::isBitCastable(V2Int32Ty, V2Int64Ty));
289 EXPECT_FALSE(CastInst::isBitCastable(V2Int64Ty, V2Int32Ty));
292 EXPECT_FALSE(CastInst::castIsValid(Instruction::BitCast,
293 Constant::getNullValue(V4Int32PtrTy),
294 V2Int32PtrTy));
295 EXPECT_FALSE(CastInst::castIsValid(Instruction::BitCast,
296 Constant::getNullValue(V2Int32PtrTy),
297 V4Int32PtrTy));
299 EXPECT_FALSE(CastInst::castIsValid(Instruction::AddrSpaceCast,
300 Constant::getNullValue(V4Int32PtrAS1Ty),
301 V2Int32PtrTy));
302 EXPECT_FALSE(CastInst::castIsValid(Instruction::AddrSpaceCast,
303 Constant::getNullValue(V2Int32PtrTy),
304 V4Int32PtrAS1Ty));
306 // Address space cast of fixed/scalable vectors of pointers to scalable/fixed
307 // vector of pointers.
308 EXPECT_FALSE(CastInst::castIsValid(
309 Instruction::AddrSpaceCast, Constant::getNullValue(VScaleV4Int32PtrAS1Ty),
310 V4Int32PtrTy));
311 EXPECT_FALSE(CastInst::castIsValid(Instruction::AddrSpaceCast,
312 Constant::getNullValue(V4Int32PtrTy),
313 VScaleV4Int32PtrAS1Ty));
314 // Address space cast of scalable vectors of pointers to scalable vector of
315 // pointers.
316 EXPECT_FALSE(CastInst::castIsValid(
317 Instruction::AddrSpaceCast, Constant::getNullValue(VScaleV4Int32PtrAS1Ty),
318 VScaleV2Int32PtrTy));
319 EXPECT_FALSE(CastInst::castIsValid(Instruction::AddrSpaceCast,
320 Constant::getNullValue(VScaleV2Int32PtrTy),
321 VScaleV4Int32PtrAS1Ty));
322 EXPECT_TRUE(CastInst::castIsValid(Instruction::AddrSpaceCast,
323 Constant::getNullValue(VScaleV4Int64PtrTy),
324 VScaleV4Int32PtrAS1Ty));
325 // Same number of lanes, different address space.
326 EXPECT_TRUE(CastInst::castIsValid(
327 Instruction::AddrSpaceCast, Constant::getNullValue(VScaleV4Int32PtrAS1Ty),
328 VScaleV4Int32PtrTy));
329 // Same number of lanes, same address space.
330 EXPECT_FALSE(CastInst::castIsValid(Instruction::AddrSpaceCast,
331 Constant::getNullValue(VScaleV4Int64PtrTy),
332 VScaleV4Int32PtrTy));
334 // Bit casting fixed/scalable vector to scalable/fixed vectors.
335 EXPECT_FALSE(CastInst::castIsValid(Instruction::BitCast,
336 Constant::getNullValue(V2Int32Ty),
337 VScaleV2Int32Ty));
338 EXPECT_FALSE(CastInst::castIsValid(Instruction::BitCast,
339 Constant::getNullValue(V2Int64Ty),
340 VScaleV2Int64Ty));
341 EXPECT_FALSE(CastInst::castIsValid(Instruction::BitCast,
342 Constant::getNullValue(V4Int16Ty),
343 VScaleV4Int16Ty));
344 EXPECT_FALSE(CastInst::castIsValid(Instruction::BitCast,
345 Constant::getNullValue(VScaleV2Int32Ty),
346 V2Int32Ty));
347 EXPECT_FALSE(CastInst::castIsValid(Instruction::BitCast,
348 Constant::getNullValue(VScaleV2Int64Ty),
349 V2Int64Ty));
350 EXPECT_FALSE(CastInst::castIsValid(Instruction::BitCast,
351 Constant::getNullValue(VScaleV4Int16Ty),
352 V4Int16Ty));
354 // Bit casting scalable vectors to scalable vectors.
355 EXPECT_TRUE(CastInst::castIsValid(Instruction::BitCast,
356 Constant::getNullValue(VScaleV4Int16Ty),
357 VScaleV2Int32Ty));
358 EXPECT_TRUE(CastInst::castIsValid(Instruction::BitCast,
359 Constant::getNullValue(VScaleV2Int32Ty),
360 VScaleV4Int16Ty));
361 EXPECT_FALSE(CastInst::castIsValid(Instruction::BitCast,
362 Constant::getNullValue(VScaleV2Int64Ty),
363 VScaleV2Int32Ty));
364 EXPECT_FALSE(CastInst::castIsValid(Instruction::BitCast,
365 Constant::getNullValue(VScaleV2Int32Ty),
366 VScaleV2Int64Ty));
368 // Bitcasting to/from <vscale x 1 x Ty>
369 EXPECT_FALSE(CastInst::castIsValid(Instruction::BitCast,
370 Constant::getNullValue(VScaleV1Int16Ty),
371 V1Int16Ty));
372 EXPECT_FALSE(CastInst::castIsValid(Instruction::BitCast,
373 Constant::getNullValue(V1Int16Ty),
374 VScaleV1Int16Ty));
376 // Check that assertion is not hit when creating a cast with a vector of
377 // pointers
378 // First form
379 BasicBlock *BB = BasicBlock::Create(C);
380 Constant *NullV2I32Ptr = Constant::getNullValue(V2Int32PtrTy);
381 auto Inst1 = CastInst::CreatePointerCast(NullV2I32Ptr, V2Int32Ty, "foo", BB);
383 Constant *NullVScaleV2I32Ptr = Constant::getNullValue(VScaleV2Int32PtrTy);
384 auto Inst1VScale = CastInst::CreatePointerCast(
385 NullVScaleV2I32Ptr, VScaleV2Int32Ty, "foo.vscale", BB);
387 // Second form
388 auto Inst2 = CastInst::CreatePointerCast(NullV2I32Ptr, V2Int32Ty);
389 auto Inst2VScale =
390 CastInst::CreatePointerCast(NullVScaleV2I32Ptr, VScaleV2Int32Ty);
392 delete Inst2;
393 delete Inst2VScale;
394 Inst1->eraseFromParent();
395 Inst1VScale->eraseFromParent();
396 delete BB;
399 TEST(InstructionsTest, CastCAPI) {
400 LLVMContext C;
402 Type *Int8Ty = Type::getInt8Ty(C);
403 Type *Int32Ty = Type::getInt32Ty(C);
404 Type *Int64Ty = Type::getInt64Ty(C);
406 Type *FloatTy = Type::getFloatTy(C);
407 Type *DoubleTy = Type::getDoubleTy(C);
409 Type *Int8PtrTy = PointerType::get(Int8Ty, 0);
410 Type *Int32PtrTy = PointerType::get(Int32Ty, 0);
412 const Constant *C8 = Constant::getNullValue(Int8Ty);
413 const Constant *C64 = Constant::getNullValue(Int64Ty);
415 EXPECT_EQ(LLVMBitCast,
416 LLVMGetCastOpcode(wrap(C64), true, wrap(Int64Ty), true));
417 EXPECT_EQ(LLVMTrunc, LLVMGetCastOpcode(wrap(C64), true, wrap(Int8Ty), true));
418 EXPECT_EQ(LLVMSExt, LLVMGetCastOpcode(wrap(C8), true, wrap(Int64Ty), true));
419 EXPECT_EQ(LLVMZExt, LLVMGetCastOpcode(wrap(C8), false, wrap(Int64Ty), true));
421 const Constant *CF32 = Constant::getNullValue(FloatTy);
422 const Constant *CF64 = Constant::getNullValue(DoubleTy);
424 EXPECT_EQ(LLVMFPToUI,
425 LLVMGetCastOpcode(wrap(CF32), true, wrap(Int8Ty), false));
426 EXPECT_EQ(LLVMFPToSI,
427 LLVMGetCastOpcode(wrap(CF32), true, wrap(Int8Ty), true));
428 EXPECT_EQ(LLVMUIToFP,
429 LLVMGetCastOpcode(wrap(C8), false, wrap(FloatTy), true));
430 EXPECT_EQ(LLVMSIToFP, LLVMGetCastOpcode(wrap(C8), true, wrap(FloatTy), true));
431 EXPECT_EQ(LLVMFPTrunc,
432 LLVMGetCastOpcode(wrap(CF64), true, wrap(FloatTy), true));
433 EXPECT_EQ(LLVMFPExt,
434 LLVMGetCastOpcode(wrap(CF32), true, wrap(DoubleTy), true));
436 const Constant *CPtr8 = Constant::getNullValue(Int8PtrTy);
438 EXPECT_EQ(LLVMPtrToInt,
439 LLVMGetCastOpcode(wrap(CPtr8), true, wrap(Int8Ty), true));
440 EXPECT_EQ(LLVMIntToPtr,
441 LLVMGetCastOpcode(wrap(C8), true, wrap(Int8PtrTy), true));
443 Type *V8x8Ty = FixedVectorType::get(Int8Ty, 8);
444 Type *V8x64Ty = FixedVectorType::get(Int64Ty, 8);
445 const Constant *CV8 = Constant::getNullValue(V8x8Ty);
446 const Constant *CV64 = Constant::getNullValue(V8x64Ty);
448 EXPECT_EQ(LLVMTrunc, LLVMGetCastOpcode(wrap(CV64), true, wrap(V8x8Ty), true));
449 EXPECT_EQ(LLVMSExt, LLVMGetCastOpcode(wrap(CV8), true, wrap(V8x64Ty), true));
451 Type *Int32PtrAS1Ty = PointerType::get(Int32Ty, 1);
452 Type *V2Int32PtrAS1Ty = FixedVectorType::get(Int32PtrAS1Ty, 2);
453 Type *V2Int32PtrTy = FixedVectorType::get(Int32PtrTy, 2);
454 const Constant *CV2ptr32 = Constant::getNullValue(V2Int32PtrTy);
456 EXPECT_EQ(LLVMAddrSpaceCast, LLVMGetCastOpcode(wrap(CV2ptr32), true,
457 wrap(V2Int32PtrAS1Ty), true));
460 TEST(InstructionsTest, VectorGep) {
461 LLVMContext C;
463 // Type Definitions
464 Type *I8Ty = IntegerType::get(C, 8);
465 Type *I32Ty = IntegerType::get(C, 32);
466 PointerType *Ptri8Ty = PointerType::get(I8Ty, 0);
467 PointerType *Ptri32Ty = PointerType::get(I32Ty, 0);
469 VectorType *V2xi8PTy = FixedVectorType::get(Ptri8Ty, 2);
470 VectorType *V2xi32PTy = FixedVectorType::get(Ptri32Ty, 2);
472 // Test different aspects of the vector-of-pointers type
473 // and GEPs which use this type.
474 ConstantInt *Ci32a = ConstantInt::get(C, APInt(32, 1492));
475 ConstantInt *Ci32b = ConstantInt::get(C, APInt(32, 1948));
476 std::vector<Constant*> ConstVa(2, Ci32a);
477 std::vector<Constant*> ConstVb(2, Ci32b);
478 Constant *C2xi32a = ConstantVector::get(ConstVa);
479 Constant *C2xi32b = ConstantVector::get(ConstVb);
481 CastInst *PtrVecA = new IntToPtrInst(C2xi32a, V2xi32PTy);
482 CastInst *PtrVecB = new IntToPtrInst(C2xi32b, V2xi32PTy);
484 ICmpInst *ICmp0 = new ICmpInst(ICmpInst::ICMP_SGT, PtrVecA, PtrVecB);
485 ICmpInst *ICmp1 = new ICmpInst(ICmpInst::ICMP_ULT, PtrVecA, PtrVecB);
486 EXPECT_NE(ICmp0, ICmp1); // suppress warning.
488 BasicBlock* BB0 = BasicBlock::Create(C);
489 // Test InsertAtEnd ICmpInst constructor.
490 ICmpInst *ICmp2 = new ICmpInst(BB0, ICmpInst::ICMP_SGE, PtrVecA, PtrVecB);
491 EXPECT_NE(ICmp0, ICmp2); // suppress warning.
493 GetElementPtrInst *Gep0 = GetElementPtrInst::Create(I32Ty, PtrVecA, C2xi32a);
494 GetElementPtrInst *Gep1 = GetElementPtrInst::Create(I32Ty, PtrVecA, C2xi32b);
495 GetElementPtrInst *Gep2 = GetElementPtrInst::Create(I32Ty, PtrVecB, C2xi32a);
496 GetElementPtrInst *Gep3 = GetElementPtrInst::Create(I32Ty, PtrVecB, C2xi32b);
498 CastInst *BTC0 = new BitCastInst(Gep0, V2xi8PTy);
499 CastInst *BTC1 = new BitCastInst(Gep1, V2xi8PTy);
500 CastInst *BTC2 = new BitCastInst(Gep2, V2xi8PTy);
501 CastInst *BTC3 = new BitCastInst(Gep3, V2xi8PTy);
503 Value *S0 = BTC0->stripPointerCasts();
504 Value *S1 = BTC1->stripPointerCasts();
505 Value *S2 = BTC2->stripPointerCasts();
506 Value *S3 = BTC3->stripPointerCasts();
508 EXPECT_NE(S0, Gep0);
509 EXPECT_NE(S1, Gep1);
510 EXPECT_NE(S2, Gep2);
511 EXPECT_NE(S3, Gep3);
513 int64_t Offset;
514 DataLayout TD("e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f3"
515 "2:32:32-f64:64:64-v64:64:64-v128:128:128-a:0:64-s:64:64-f80"
516 ":128:128-n8:16:32:64-S128");
517 // Make sure we don't crash
518 GetPointerBaseWithConstantOffset(Gep0, Offset, TD);
519 GetPointerBaseWithConstantOffset(Gep1, Offset, TD);
520 GetPointerBaseWithConstantOffset(Gep2, Offset, TD);
521 GetPointerBaseWithConstantOffset(Gep3, Offset, TD);
523 // Gep of Geps
524 GetElementPtrInst *GepII0 = GetElementPtrInst::Create(I32Ty, Gep0, C2xi32b);
525 GetElementPtrInst *GepII1 = GetElementPtrInst::Create(I32Ty, Gep1, C2xi32a);
526 GetElementPtrInst *GepII2 = GetElementPtrInst::Create(I32Ty, Gep2, C2xi32b);
527 GetElementPtrInst *GepII3 = GetElementPtrInst::Create(I32Ty, Gep3, C2xi32a);
529 EXPECT_EQ(GepII0->getNumIndices(), 1u);
530 EXPECT_EQ(GepII1->getNumIndices(), 1u);
531 EXPECT_EQ(GepII2->getNumIndices(), 1u);
532 EXPECT_EQ(GepII3->getNumIndices(), 1u);
534 EXPECT_FALSE(GepII0->hasAllZeroIndices());
535 EXPECT_FALSE(GepII1->hasAllZeroIndices());
536 EXPECT_FALSE(GepII2->hasAllZeroIndices());
537 EXPECT_FALSE(GepII3->hasAllZeroIndices());
539 delete GepII0;
540 delete GepII1;
541 delete GepII2;
542 delete GepII3;
544 delete BTC0;
545 delete BTC1;
546 delete BTC2;
547 delete BTC3;
549 delete Gep0;
550 delete Gep1;
551 delete Gep2;
552 delete Gep3;
554 ICmp2->eraseFromParent();
555 delete BB0;
557 delete ICmp0;
558 delete ICmp1;
559 delete PtrVecA;
560 delete PtrVecB;
563 TEST(InstructionsTest, FPMathOperator) {
564 LLVMContext Context;
565 IRBuilder<> Builder(Context);
566 MDBuilder MDHelper(Context);
567 Instruction *I = Builder.CreatePHI(Builder.getDoubleTy(), 0);
568 MDNode *MD1 = MDHelper.createFPMath(1.0);
569 Value *V1 = Builder.CreateFAdd(I, I, "", MD1);
570 EXPECT_TRUE(isa<FPMathOperator>(V1));
571 FPMathOperator *O1 = cast<FPMathOperator>(V1);
572 EXPECT_EQ(O1->getFPAccuracy(), 1.0);
573 V1->deleteValue();
574 I->deleteValue();
577 TEST(InstructionTest, ConstrainedTrans) {
578 LLVMContext Context;
579 std::unique_ptr<Module> M(new Module("MyModule", Context));
580 FunctionType *FTy =
581 FunctionType::get(Type::getVoidTy(Context),
582 {Type::getFloatTy(Context), Type::getFloatTy(Context),
583 Type::getInt32Ty(Context)},
584 false);
585 auto *F = Function::Create(FTy, Function::ExternalLinkage, "", M.get());
586 auto *BB = BasicBlock::Create(Context, "bb", F);
587 IRBuilder<> Builder(Context);
588 Builder.SetInsertPoint(BB);
589 auto *Arg0 = F->arg_begin();
590 auto *Arg1 = F->arg_begin() + 1;
593 auto *I = cast<Instruction>(Builder.CreateFAdd(Arg0, Arg1));
594 EXPECT_EQ(Intrinsic::experimental_constrained_fadd,
595 getConstrainedIntrinsicID(*I));
599 auto *I = cast<Instruction>(
600 Builder.CreateFPToSI(Arg0, Type::getInt32Ty(Context)));
601 EXPECT_EQ(Intrinsic::experimental_constrained_fptosi,
602 getConstrainedIntrinsicID(*I));
606 auto *I = cast<Instruction>(Builder.CreateIntrinsic(
607 Intrinsic::ceil, {Type::getFloatTy(Context)}, {Arg0}));
608 EXPECT_EQ(Intrinsic::experimental_constrained_ceil,
609 getConstrainedIntrinsicID(*I));
613 auto *I = cast<Instruction>(Builder.CreateFCmpOEQ(Arg0, Arg1));
614 EXPECT_EQ(Intrinsic::experimental_constrained_fcmp,
615 getConstrainedIntrinsicID(*I));
619 auto *Arg2 = F->arg_begin() + 2;
620 auto *I = cast<Instruction>(Builder.CreateAdd(Arg2, Arg2));
621 EXPECT_EQ(Intrinsic::not_intrinsic, getConstrainedIntrinsicID(*I));
625 auto *I = cast<Instruction>(Builder.CreateConstrainedFPBinOp(
626 Intrinsic::experimental_constrained_fadd, Arg0, Arg0));
627 EXPECT_EQ(Intrinsic::not_intrinsic, getConstrainedIntrinsicID(*I));
631 TEST(InstructionsTest, isEliminableCastPair) {
632 LLVMContext C;
634 Type* Int16Ty = Type::getInt16Ty(C);
635 Type* Int32Ty = Type::getInt32Ty(C);
636 Type* Int64Ty = Type::getInt64Ty(C);
637 Type *Int64PtrTy = PointerType::get(C, 0);
639 // Source and destination pointers have same size -> bitcast.
640 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt,
641 CastInst::IntToPtr,
642 Int64PtrTy, Int64Ty, Int64PtrTy,
643 Int32Ty, nullptr, Int32Ty),
644 CastInst::BitCast);
646 // Source and destination have unknown sizes, but the same address space and
647 // the intermediate int is the maximum pointer size -> bitcast
648 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt,
649 CastInst::IntToPtr,
650 Int64PtrTy, Int64Ty, Int64PtrTy,
651 nullptr, nullptr, nullptr),
652 CastInst::BitCast);
654 // Source and destination have unknown sizes, but the same address space and
655 // the intermediate int is not the maximum pointer size -> nothing
656 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt,
657 CastInst::IntToPtr,
658 Int64PtrTy, Int32Ty, Int64PtrTy,
659 nullptr, nullptr, nullptr),
660 0U);
662 // Middle pointer big enough -> bitcast.
663 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
664 CastInst::PtrToInt,
665 Int64Ty, Int64PtrTy, Int64Ty,
666 nullptr, Int64Ty, nullptr),
667 CastInst::BitCast);
669 // Middle pointer too small -> fail.
670 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
671 CastInst::PtrToInt,
672 Int64Ty, Int64PtrTy, Int64Ty,
673 nullptr, Int32Ty, nullptr),
674 0U);
676 // Test that we don't eliminate bitcasts between different address spaces,
677 // or if we don't have available pointer size information.
678 DataLayout DL("e-p:32:32:32-p1:16:16:16-p2:64:64:64-i1:8:8-i8:8:8-i16:16:16"
679 "-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64"
680 "-v128:128:128-a:0:64-s:64:64-f80:128:128-n8:16:32:64-S128");
682 Type *Int64PtrTyAS1 = PointerType::get(C, 1);
683 Type *Int64PtrTyAS2 = PointerType::get(C, 2);
685 IntegerType *Int16SizePtr = DL.getIntPtrType(C, 1);
686 IntegerType *Int64SizePtr = DL.getIntPtrType(C, 2);
688 // Cannot simplify inttoptr, addrspacecast
689 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
690 CastInst::AddrSpaceCast,
691 Int16Ty, Int64PtrTyAS1, Int64PtrTyAS2,
692 nullptr, Int16SizePtr, Int64SizePtr),
693 0U);
695 // Cannot simplify addrspacecast, ptrtoint
696 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::AddrSpaceCast,
697 CastInst::PtrToInt,
698 Int64PtrTyAS1, Int64PtrTyAS2, Int16Ty,
699 Int64SizePtr, Int16SizePtr, nullptr),
700 0U);
702 // Pass since the bitcast address spaces are the same
703 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
704 CastInst::BitCast,
705 Int16Ty, Int64PtrTyAS1, Int64PtrTyAS1,
706 nullptr, nullptr, nullptr),
707 CastInst::IntToPtr);
711 TEST(InstructionsTest, CloneCall) {
712 LLVMContext C;
713 Type *Int32Ty = Type::getInt32Ty(C);
714 Type *ArgTys[] = {Int32Ty, Int32Ty, Int32Ty};
715 FunctionType *FnTy = FunctionType::get(Int32Ty, ArgTys, /*isVarArg=*/false);
716 Value *Callee = Constant::getNullValue(PointerType::getUnqual(C));
717 Value *Args[] = {
718 ConstantInt::get(Int32Ty, 1),
719 ConstantInt::get(Int32Ty, 2),
720 ConstantInt::get(Int32Ty, 3)
722 std::unique_ptr<CallInst> Call(
723 CallInst::Create(FnTy, Callee, Args, "result"));
725 // Test cloning the tail call kind.
726 CallInst::TailCallKind Kinds[] = {CallInst::TCK_None, CallInst::TCK_Tail,
727 CallInst::TCK_MustTail};
728 for (CallInst::TailCallKind TCK : Kinds) {
729 Call->setTailCallKind(TCK);
730 std::unique_ptr<CallInst> Clone(cast<CallInst>(Call->clone()));
731 EXPECT_EQ(Call->getTailCallKind(), Clone->getTailCallKind());
733 Call->setTailCallKind(CallInst::TCK_None);
735 // Test cloning an attribute.
737 AttrBuilder AB(C);
738 AB.addAttribute(Attribute::NoUnwind);
739 Call->setAttributes(
740 AttributeList::get(C, AttributeList::FunctionIndex, AB));
741 std::unique_ptr<CallInst> Clone(cast<CallInst>(Call->clone()));
742 EXPECT_TRUE(Clone->doesNotThrow());
746 TEST(InstructionsTest, AlterCallBundles) {
747 LLVMContext C;
748 Type *Int32Ty = Type::getInt32Ty(C);
749 FunctionType *FnTy = FunctionType::get(Int32Ty, Int32Ty, /*isVarArg=*/false);
750 Value *Callee = Constant::getNullValue(PointerType::getUnqual(C));
751 Value *Args[] = {ConstantInt::get(Int32Ty, 42)};
752 OperandBundleDef OldBundle("before", UndefValue::get(Int32Ty));
753 std::unique_ptr<CallInst> Call(
754 CallInst::Create(FnTy, Callee, Args, OldBundle, "result"));
755 Call->setTailCallKind(CallInst::TailCallKind::TCK_NoTail);
756 AttrBuilder AB(C);
757 AB.addAttribute(Attribute::Cold);
758 Call->setAttributes(AttributeList::get(C, AttributeList::FunctionIndex, AB));
759 Call->setDebugLoc(DebugLoc(MDNode::get(C, {})));
761 OperandBundleDef NewBundle("after", ConstantInt::get(Int32Ty, 7));
762 std::unique_ptr<CallInst> Clone(CallInst::Create(Call.get(), NewBundle));
763 EXPECT_EQ(Call->arg_size(), Clone->arg_size());
764 EXPECT_EQ(Call->getArgOperand(0), Clone->getArgOperand(0));
765 EXPECT_EQ(Call->getCallingConv(), Clone->getCallingConv());
766 EXPECT_EQ(Call->getTailCallKind(), Clone->getTailCallKind());
767 EXPECT_TRUE(Clone->hasFnAttr(Attribute::AttrKind::Cold));
768 EXPECT_EQ(Call->getDebugLoc(), Clone->getDebugLoc());
769 EXPECT_EQ(Clone->getNumOperandBundles(), 1U);
770 EXPECT_TRUE(Clone->getOperandBundle("after"));
773 TEST(InstructionsTest, AlterInvokeBundles) {
774 LLVMContext C;
775 Type *Int32Ty = Type::getInt32Ty(C);
776 FunctionType *FnTy = FunctionType::get(Int32Ty, Int32Ty, /*isVarArg=*/false);
777 Value *Callee = Constant::getNullValue(PointerType::getUnqual(C));
778 Value *Args[] = {ConstantInt::get(Int32Ty, 42)};
779 std::unique_ptr<BasicBlock> NormalDest(BasicBlock::Create(C));
780 std::unique_ptr<BasicBlock> UnwindDest(BasicBlock::Create(C));
781 OperandBundleDef OldBundle("before", UndefValue::get(Int32Ty));
782 std::unique_ptr<InvokeInst> Invoke(
783 InvokeInst::Create(FnTy, Callee, NormalDest.get(), UnwindDest.get(), Args,
784 OldBundle, "result"));
785 AttrBuilder AB(C);
786 AB.addAttribute(Attribute::Cold);
787 Invoke->setAttributes(
788 AttributeList::get(C, AttributeList::FunctionIndex, AB));
789 Invoke->setDebugLoc(DebugLoc(MDNode::get(C, {})));
791 OperandBundleDef NewBundle("after", ConstantInt::get(Int32Ty, 7));
792 std::unique_ptr<InvokeInst> Clone(
793 InvokeInst::Create(Invoke.get(), NewBundle));
794 EXPECT_EQ(Invoke->getNormalDest(), Clone->getNormalDest());
795 EXPECT_EQ(Invoke->getUnwindDest(), Clone->getUnwindDest());
796 EXPECT_EQ(Invoke->arg_size(), Clone->arg_size());
797 EXPECT_EQ(Invoke->getArgOperand(0), Clone->getArgOperand(0));
798 EXPECT_EQ(Invoke->getCallingConv(), Clone->getCallingConv());
799 EXPECT_TRUE(Clone->hasFnAttr(Attribute::AttrKind::Cold));
800 EXPECT_EQ(Invoke->getDebugLoc(), Clone->getDebugLoc());
801 EXPECT_EQ(Clone->getNumOperandBundles(), 1U);
802 EXPECT_TRUE(Clone->getOperandBundle("after"));
805 TEST_F(ModuleWithFunctionTest, DropPoisonGeneratingFlags) {
806 auto *OnlyBB = BasicBlock::Create(Ctx, "bb", F);
807 auto *Arg0 = &*F->arg_begin();
809 IRBuilder<NoFolder> B(Ctx);
810 B.SetInsertPoint(OnlyBB);
813 auto *UI =
814 cast<Instruction>(B.CreateUDiv(Arg0, Arg0, "", /*isExact*/ true));
815 ASSERT_TRUE(UI->isExact());
816 UI->dropPoisonGeneratingFlags();
817 ASSERT_FALSE(UI->isExact());
821 auto *ShrI =
822 cast<Instruction>(B.CreateLShr(Arg0, Arg0, "", /*isExact*/ true));
823 ASSERT_TRUE(ShrI->isExact());
824 ShrI->dropPoisonGeneratingFlags();
825 ASSERT_FALSE(ShrI->isExact());
829 auto *AI = cast<Instruction>(
830 B.CreateAdd(Arg0, Arg0, "", /*HasNUW*/ true, /*HasNSW*/ false));
831 ASSERT_TRUE(AI->hasNoUnsignedWrap());
832 AI->dropPoisonGeneratingFlags();
833 ASSERT_FALSE(AI->hasNoUnsignedWrap());
834 ASSERT_FALSE(AI->hasNoSignedWrap());
838 auto *SI = cast<Instruction>(
839 B.CreateAdd(Arg0, Arg0, "", /*HasNUW*/ false, /*HasNSW*/ true));
840 ASSERT_TRUE(SI->hasNoSignedWrap());
841 SI->dropPoisonGeneratingFlags();
842 ASSERT_FALSE(SI->hasNoUnsignedWrap());
843 ASSERT_FALSE(SI->hasNoSignedWrap());
847 auto *ShlI = cast<Instruction>(
848 B.CreateShl(Arg0, Arg0, "", /*HasNUW*/ true, /*HasNSW*/ true));
849 ASSERT_TRUE(ShlI->hasNoSignedWrap());
850 ASSERT_TRUE(ShlI->hasNoUnsignedWrap());
851 ShlI->dropPoisonGeneratingFlags();
852 ASSERT_FALSE(ShlI->hasNoUnsignedWrap());
853 ASSERT_FALSE(ShlI->hasNoSignedWrap());
857 Value *GEPBase = Constant::getNullValue(B.getPtrTy());
858 auto *GI = cast<GetElementPtrInst>(
859 B.CreateInBoundsGEP(B.getInt8Ty(), GEPBase, Arg0));
860 ASSERT_TRUE(GI->isInBounds());
861 GI->dropPoisonGeneratingFlags();
862 ASSERT_FALSE(GI->isInBounds());
866 TEST(InstructionsTest, GEPIndices) {
867 LLVMContext Context;
868 IRBuilder<NoFolder> Builder(Context);
869 Type *ElementTy = Builder.getInt8Ty();
870 Type *ArrTy = ArrayType::get(ArrayType::get(ElementTy, 64), 64);
871 Value *Indices[] = {
872 Builder.getInt32(0),
873 Builder.getInt32(13),
874 Builder.getInt32(42) };
876 Value *V = Builder.CreateGEP(ArrTy, UndefValue::get(PointerType::getUnqual(ArrTy)),
877 Indices);
878 ASSERT_TRUE(isa<GetElementPtrInst>(V));
880 auto *GEPI = cast<GetElementPtrInst>(V);
881 ASSERT_NE(GEPI->idx_begin(), GEPI->idx_end());
882 ASSERT_EQ(GEPI->idx_end(), std::next(GEPI->idx_begin(), 3));
883 EXPECT_EQ(Indices[0], GEPI->idx_begin()[0]);
884 EXPECT_EQ(Indices[1], GEPI->idx_begin()[1]);
885 EXPECT_EQ(Indices[2], GEPI->idx_begin()[2]);
886 EXPECT_EQ(GEPI->idx_begin(), GEPI->indices().begin());
887 EXPECT_EQ(GEPI->idx_end(), GEPI->indices().end());
889 const auto *CGEPI = GEPI;
890 ASSERT_NE(CGEPI->idx_begin(), CGEPI->idx_end());
891 ASSERT_EQ(CGEPI->idx_end(), std::next(CGEPI->idx_begin(), 3));
892 EXPECT_EQ(Indices[0], CGEPI->idx_begin()[0]);
893 EXPECT_EQ(Indices[1], CGEPI->idx_begin()[1]);
894 EXPECT_EQ(Indices[2], CGEPI->idx_begin()[2]);
895 EXPECT_EQ(CGEPI->idx_begin(), CGEPI->indices().begin());
896 EXPECT_EQ(CGEPI->idx_end(), CGEPI->indices().end());
898 delete GEPI;
901 TEST(InstructionsTest, SwitchInst) {
902 LLVMContext C;
904 std::unique_ptr<BasicBlock> BB1, BB2, BB3;
905 BB1.reset(BasicBlock::Create(C));
906 BB2.reset(BasicBlock::Create(C));
907 BB3.reset(BasicBlock::Create(C));
909 // We create block 0 after the others so that it gets destroyed first and
910 // clears the uses of the other basic blocks.
911 std::unique_ptr<BasicBlock> BB0(BasicBlock::Create(C));
913 auto *Int32Ty = Type::getInt32Ty(C);
915 SwitchInst *SI =
916 SwitchInst::Create(UndefValue::get(Int32Ty), BB0.get(), 3, BB0.get());
917 SI->addCase(ConstantInt::get(Int32Ty, 1), BB1.get());
918 SI->addCase(ConstantInt::get(Int32Ty, 2), BB2.get());
919 SI->addCase(ConstantInt::get(Int32Ty, 3), BB3.get());
921 auto CI = SI->case_begin();
922 ASSERT_NE(CI, SI->case_end());
923 EXPECT_EQ(1, CI->getCaseValue()->getSExtValue());
924 EXPECT_EQ(BB1.get(), CI->getCaseSuccessor());
925 EXPECT_EQ(2, (CI + 1)->getCaseValue()->getSExtValue());
926 EXPECT_EQ(BB2.get(), (CI + 1)->getCaseSuccessor());
927 EXPECT_EQ(3, (CI + 2)->getCaseValue()->getSExtValue());
928 EXPECT_EQ(BB3.get(), (CI + 2)->getCaseSuccessor());
929 EXPECT_EQ(CI + 1, std::next(CI));
930 EXPECT_EQ(CI + 2, std::next(CI, 2));
931 EXPECT_EQ(CI + 3, std::next(CI, 3));
932 EXPECT_EQ(SI->case_end(), CI + 3);
933 EXPECT_EQ(0, CI - CI);
934 EXPECT_EQ(1, (CI + 1) - CI);
935 EXPECT_EQ(2, (CI + 2) - CI);
936 EXPECT_EQ(3, SI->case_end() - CI);
937 EXPECT_EQ(3, std::distance(CI, SI->case_end()));
939 auto CCI = const_cast<const SwitchInst *>(SI)->case_begin();
940 SwitchInst::ConstCaseIt CCE = SI->case_end();
941 ASSERT_NE(CCI, SI->case_end());
942 EXPECT_EQ(1, CCI->getCaseValue()->getSExtValue());
943 EXPECT_EQ(BB1.get(), CCI->getCaseSuccessor());
944 EXPECT_EQ(2, (CCI + 1)->getCaseValue()->getSExtValue());
945 EXPECT_EQ(BB2.get(), (CCI + 1)->getCaseSuccessor());
946 EXPECT_EQ(3, (CCI + 2)->getCaseValue()->getSExtValue());
947 EXPECT_EQ(BB3.get(), (CCI + 2)->getCaseSuccessor());
948 EXPECT_EQ(CCI + 1, std::next(CCI));
949 EXPECT_EQ(CCI + 2, std::next(CCI, 2));
950 EXPECT_EQ(CCI + 3, std::next(CCI, 3));
951 EXPECT_EQ(CCE, CCI + 3);
952 EXPECT_EQ(0, CCI - CCI);
953 EXPECT_EQ(1, (CCI + 1) - CCI);
954 EXPECT_EQ(2, (CCI + 2) - CCI);
955 EXPECT_EQ(3, CCE - CCI);
956 EXPECT_EQ(3, std::distance(CCI, CCE));
958 // Make sure that the const iterator is compatible with a const auto ref.
959 const auto &Handle = *CCI;
960 EXPECT_EQ(1, Handle.getCaseValue()->getSExtValue());
961 EXPECT_EQ(BB1.get(), Handle.getCaseSuccessor());
964 TEST(InstructionsTest, SwitchInstProfUpdateWrapper) {
965 LLVMContext C;
967 std::unique_ptr<BasicBlock> BB1, BB2, BB3;
968 BB1.reset(BasicBlock::Create(C));
969 BB2.reset(BasicBlock::Create(C));
970 BB3.reset(BasicBlock::Create(C));
972 // We create block 0 after the others so that it gets destroyed first and
973 // clears the uses of the other basic blocks.
974 std::unique_ptr<BasicBlock> BB0(BasicBlock::Create(C));
976 auto *Int32Ty = Type::getInt32Ty(C);
978 SwitchInst *SI =
979 SwitchInst::Create(UndefValue::get(Int32Ty), BB0.get(), 4, BB0.get());
980 SI->addCase(ConstantInt::get(Int32Ty, 1), BB1.get());
981 SI->addCase(ConstantInt::get(Int32Ty, 2), BB2.get());
982 SI->setMetadata(LLVMContext::MD_prof,
983 MDBuilder(C).createBranchWeights({ 9, 1, 22 }));
986 SwitchInstProfUpdateWrapper SIW(*SI);
987 EXPECT_EQ(*SIW.getSuccessorWeight(0), 9u);
988 EXPECT_EQ(*SIW.getSuccessorWeight(1), 1u);
989 EXPECT_EQ(*SIW.getSuccessorWeight(2), 22u);
990 SIW.setSuccessorWeight(0, 99u);
991 SIW.setSuccessorWeight(1, 11u);
992 EXPECT_EQ(*SIW.getSuccessorWeight(0), 99u);
993 EXPECT_EQ(*SIW.getSuccessorWeight(1), 11u);
994 EXPECT_EQ(*SIW.getSuccessorWeight(2), 22u);
997 { // Create another wrapper and check that the data persist.
998 SwitchInstProfUpdateWrapper SIW(*SI);
999 EXPECT_EQ(*SIW.getSuccessorWeight(0), 99u);
1000 EXPECT_EQ(*SIW.getSuccessorWeight(1), 11u);
1001 EXPECT_EQ(*SIW.getSuccessorWeight(2), 22u);
1005 TEST(InstructionsTest, CommuteShuffleMask) {
1006 SmallVector<int, 16> Indices({-1, 0, 7});
1007 ShuffleVectorInst::commuteShuffleMask(Indices, 4);
1008 EXPECT_THAT(Indices, testing::ContainerEq(ArrayRef<int>({-1, 4, 3})));
1011 TEST(InstructionsTest, ShuffleMaskQueries) {
1012 // Create the elements for various constant vectors.
1013 LLVMContext Ctx;
1014 Type *Int32Ty = Type::getInt32Ty(Ctx);
1015 Constant *CU = UndefValue::get(Int32Ty);
1016 Constant *C0 = ConstantInt::get(Int32Ty, 0);
1017 Constant *C1 = ConstantInt::get(Int32Ty, 1);
1018 Constant *C2 = ConstantInt::get(Int32Ty, 2);
1019 Constant *C3 = ConstantInt::get(Int32Ty, 3);
1020 Constant *C4 = ConstantInt::get(Int32Ty, 4);
1021 Constant *C5 = ConstantInt::get(Int32Ty, 5);
1022 Constant *C6 = ConstantInt::get(Int32Ty, 6);
1023 Constant *C7 = ConstantInt::get(Int32Ty, 7);
1025 Constant *Identity = ConstantVector::get({C0, CU, C2, C3, C4});
1026 EXPECT_TRUE(ShuffleVectorInst::isIdentityMask(
1027 Identity, cast<FixedVectorType>(Identity->getType())->getNumElements()));
1028 EXPECT_FALSE(ShuffleVectorInst::isSelectMask(
1029 Identity,
1030 cast<FixedVectorType>(Identity->getType())
1031 ->getNumElements())); // identity is distinguished from select
1032 EXPECT_FALSE(ShuffleVectorInst::isReverseMask(
1033 Identity, cast<FixedVectorType>(Identity->getType())->getNumElements()));
1034 EXPECT_TRUE(ShuffleVectorInst::isSingleSourceMask(
1035 Identity, cast<FixedVectorType>(Identity->getType())
1036 ->getNumElements())); // identity is always single source
1037 EXPECT_FALSE(ShuffleVectorInst::isZeroEltSplatMask(
1038 Identity, cast<FixedVectorType>(Identity->getType())->getNumElements()));
1039 EXPECT_FALSE(ShuffleVectorInst::isTransposeMask(
1040 Identity, cast<FixedVectorType>(Identity->getType())->getNumElements()));
1042 Constant *Select = ConstantVector::get({CU, C1, C5});
1043 EXPECT_FALSE(ShuffleVectorInst::isIdentityMask(
1044 Select, cast<FixedVectorType>(Select->getType())->getNumElements()));
1045 EXPECT_TRUE(ShuffleVectorInst::isSelectMask(
1046 Select, cast<FixedVectorType>(Select->getType())->getNumElements()));
1047 EXPECT_FALSE(ShuffleVectorInst::isReverseMask(
1048 Select, cast<FixedVectorType>(Select->getType())->getNumElements()));
1049 EXPECT_FALSE(ShuffleVectorInst::isSingleSourceMask(
1050 Select, cast<FixedVectorType>(Select->getType())->getNumElements()));
1051 EXPECT_FALSE(ShuffleVectorInst::isZeroEltSplatMask(
1052 Select, cast<FixedVectorType>(Select->getType())->getNumElements()));
1053 EXPECT_FALSE(ShuffleVectorInst::isTransposeMask(
1054 Select, cast<FixedVectorType>(Select->getType())->getNumElements()));
1056 Constant *Reverse = ConstantVector::get({C3, C2, C1, CU});
1057 EXPECT_FALSE(ShuffleVectorInst::isIdentityMask(
1058 Reverse, cast<FixedVectorType>(Reverse->getType())->getNumElements()));
1059 EXPECT_FALSE(ShuffleVectorInst::isSelectMask(
1060 Reverse, cast<FixedVectorType>(Reverse->getType())->getNumElements()));
1061 EXPECT_TRUE(ShuffleVectorInst::isReverseMask(
1062 Reverse, cast<FixedVectorType>(Reverse->getType())->getNumElements()));
1063 EXPECT_TRUE(ShuffleVectorInst::isSingleSourceMask(
1064 Reverse, cast<FixedVectorType>(Reverse->getType())
1065 ->getNumElements())); // reverse is always single source
1066 EXPECT_FALSE(ShuffleVectorInst::isZeroEltSplatMask(
1067 Reverse, cast<FixedVectorType>(Reverse->getType())->getNumElements()));
1068 EXPECT_FALSE(ShuffleVectorInst::isTransposeMask(
1069 Reverse, cast<FixedVectorType>(Reverse->getType())->getNumElements()));
1071 Constant *SingleSource = ConstantVector::get({C2, C2, C0, CU});
1072 EXPECT_FALSE(ShuffleVectorInst::isIdentityMask(
1073 SingleSource,
1074 cast<FixedVectorType>(SingleSource->getType())->getNumElements()));
1075 EXPECT_FALSE(ShuffleVectorInst::isSelectMask(
1076 SingleSource,
1077 cast<FixedVectorType>(SingleSource->getType())->getNumElements()));
1078 EXPECT_FALSE(ShuffleVectorInst::isReverseMask(
1079 SingleSource,
1080 cast<FixedVectorType>(SingleSource->getType())->getNumElements()));
1081 EXPECT_TRUE(ShuffleVectorInst::isSingleSourceMask(
1082 SingleSource,
1083 cast<FixedVectorType>(SingleSource->getType())->getNumElements()));
1084 EXPECT_FALSE(ShuffleVectorInst::isZeroEltSplatMask(
1085 SingleSource,
1086 cast<FixedVectorType>(SingleSource->getType())->getNumElements()));
1087 EXPECT_FALSE(ShuffleVectorInst::isTransposeMask(
1088 SingleSource,
1089 cast<FixedVectorType>(SingleSource->getType())->getNumElements()));
1091 Constant *ZeroEltSplat = ConstantVector::get({C0, C0, CU, C0});
1092 EXPECT_FALSE(ShuffleVectorInst::isIdentityMask(
1093 ZeroEltSplat,
1094 cast<FixedVectorType>(ZeroEltSplat->getType())->getNumElements()));
1095 EXPECT_FALSE(ShuffleVectorInst::isSelectMask(
1096 ZeroEltSplat,
1097 cast<FixedVectorType>(ZeroEltSplat->getType())->getNumElements()));
1098 EXPECT_FALSE(ShuffleVectorInst::isReverseMask(
1099 ZeroEltSplat,
1100 cast<FixedVectorType>(ZeroEltSplat->getType())->getNumElements()));
1101 EXPECT_TRUE(ShuffleVectorInst::isSingleSourceMask(
1102 ZeroEltSplat, cast<FixedVectorType>(ZeroEltSplat->getType())
1103 ->getNumElements())); // 0-splat is always single source
1104 EXPECT_TRUE(ShuffleVectorInst::isZeroEltSplatMask(
1105 ZeroEltSplat,
1106 cast<FixedVectorType>(ZeroEltSplat->getType())->getNumElements()));
1107 EXPECT_FALSE(ShuffleVectorInst::isTransposeMask(
1108 ZeroEltSplat,
1109 cast<FixedVectorType>(ZeroEltSplat->getType())->getNumElements()));
1111 Constant *Transpose = ConstantVector::get({C0, C4, C2, C6});
1112 EXPECT_FALSE(ShuffleVectorInst::isIdentityMask(
1113 Transpose,
1114 cast<FixedVectorType>(Transpose->getType())->getNumElements()));
1115 EXPECT_FALSE(ShuffleVectorInst::isSelectMask(
1116 Transpose,
1117 cast<FixedVectorType>(Transpose->getType())->getNumElements()));
1118 EXPECT_FALSE(ShuffleVectorInst::isReverseMask(
1119 Transpose,
1120 cast<FixedVectorType>(Transpose->getType())->getNumElements()));
1121 EXPECT_FALSE(ShuffleVectorInst::isSingleSourceMask(
1122 Transpose,
1123 cast<FixedVectorType>(Transpose->getType())->getNumElements()));
1124 EXPECT_FALSE(ShuffleVectorInst::isZeroEltSplatMask(
1125 Transpose,
1126 cast<FixedVectorType>(Transpose->getType())->getNumElements()));
1127 EXPECT_TRUE(ShuffleVectorInst::isTransposeMask(
1128 Transpose,
1129 cast<FixedVectorType>(Transpose->getType())->getNumElements()));
1131 // More tests to make sure the logic is/stays correct...
1132 EXPECT_TRUE(ShuffleVectorInst::isIdentityMask(
1133 ConstantVector::get({CU, C1, CU, C3}), 4));
1134 EXPECT_TRUE(ShuffleVectorInst::isIdentityMask(
1135 ConstantVector::get({C4, CU, C6, CU}), 4));
1137 EXPECT_TRUE(ShuffleVectorInst::isSelectMask(
1138 ConstantVector::get({C4, C1, C6, CU}), 4));
1139 EXPECT_TRUE(ShuffleVectorInst::isSelectMask(
1140 ConstantVector::get({CU, C1, C6, C3}), 4));
1142 EXPECT_TRUE(ShuffleVectorInst::isReverseMask(
1143 ConstantVector::get({C7, C6, CU, C4}), 4));
1144 EXPECT_TRUE(ShuffleVectorInst::isReverseMask(
1145 ConstantVector::get({C3, CU, C1, CU}), 4));
1147 EXPECT_TRUE(ShuffleVectorInst::isSingleSourceMask(
1148 ConstantVector::get({C7, C5, CU, C7}), 4));
1149 EXPECT_TRUE(ShuffleVectorInst::isSingleSourceMask(
1150 ConstantVector::get({C3, C0, CU, C3}), 4));
1152 EXPECT_TRUE(ShuffleVectorInst::isZeroEltSplatMask(
1153 ConstantVector::get({C4, CU, CU, C4}), 4));
1154 EXPECT_TRUE(ShuffleVectorInst::isZeroEltSplatMask(
1155 ConstantVector::get({CU, C0, CU, C0}), 4));
1157 EXPECT_TRUE(ShuffleVectorInst::isTransposeMask(
1158 ConstantVector::get({C1, C5, C3, C7}), 4));
1159 EXPECT_TRUE(
1160 ShuffleVectorInst::isTransposeMask(ConstantVector::get({C1, C3}), 2));
1162 // Nothing special about the values here - just re-using inputs to reduce
1163 // 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());
1174 delete Id1;
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());
1183 delete Id2;
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());
1192 delete Id3;
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());
1201 delete Id4;
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());
1210 delete Id5;
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());
1219 delete Id6;
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());
1228 delete Id7;
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());
1237 delete Id8;
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());
1246 delete Id9;
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());
1255 delete Id10;
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());
1264 delete Id11;
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());
1274 delete Id12;
1276 // Not possible to express shuffle mask for scalable vector for extract
1277 // subvector.
1278 Type *VScaleV4Int32Ty = ScalableVectorType::get(Int32Ty, 4);
1279 ShuffleVectorInst *Id13 =
1280 new ShuffleVectorInst(Constant::getAllOnesValue(VScaleV4Int32Ty),
1281 UndefValue::get(VScaleV4Int32Ty),
1282 Constant::getNullValue(VScaleV4Int32Ty));
1283 int Index = 0;
1284 EXPECT_FALSE(Id13->isExtractSubvectorMask(Index));
1285 EXPECT_FALSE(Id13->changesLength());
1286 EXPECT_FALSE(Id13->increasesLength());
1287 delete Id13;
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());
1297 delete Id14;
1299 // Not possible to express these masks for scalable vectors, make sure we
1300 // don't crash.
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());
1308 delete Id15;
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)) {
1322 LLVMContext Ctx;
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),
1327 OpVF == VF);
1328 delete SVI;
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(),
1343 {false, true});
1345 CombinationGenerator<bool, decltype(ElementChoices)::value_type,
1346 /*variable_smallsize=*/4>
1347 G(ElementChoices);
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>
1382 G(ElementChoices);
1384 G.generate([&](ArrayRef<int> Mask) -> bool {
1385 int GuessedReplicationFactor = -1, GuessedVF = -1;
1386 bool Match = ShuffleVectorInst::isReplicationMask(
1387 Mask, GuessedReplicationFactor, GuessedVF);
1388 if (!Match)
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);
1398 if (Elt != -1) {
1399 EXPECT_EQ(Elt, ActualElt);
1403 return /*Abort=*/false;
1408 TEST(InstructionsTest, GetSplat) {
1409 // Create the elements for various constant vectors.
1410 LLVMContext Ctx;
1411 Type *Int32Ty = Type::getInt32Ty(Ctx);
1412 Constant *CU = UndefValue::get(Int32Ty);
1413 Constant *CP = PoisonValue::get(Int32Ty);
1414 Constant *C0 = ConstantInt::get(Int32Ty, 0);
1415 Constant *C1 = ConstantInt::get(Int32Ty, 1);
1417 Constant *Splat0 = ConstantVector::get({C0, C0, C0, C0});
1418 Constant *Splat1 = ConstantVector::get({C1, C1, C1, C1 ,C1});
1419 Constant *Splat0Undef = ConstantVector::get({C0, CU, C0, CU});
1420 Constant *Splat1Undef = ConstantVector::get({CU, CU, C1, CU});
1421 Constant *NotSplat = ConstantVector::get({C1, C1, C0, C1 ,C1});
1422 Constant *NotSplatUndef = ConstantVector::get({CU, C1, CU, CU ,C0});
1423 Constant *Splat0Poison = ConstantVector::get({C0, CP, C0, CP});
1424 Constant *Splat1Poison = ConstantVector::get({CP, CP, C1, CP});
1425 Constant *NotSplatPoison = ConstantVector::get({CP, C1, CP, CP, C0});
1427 // Default - undef/poison is not allowed.
1428 EXPECT_EQ(Splat0->getSplatValue(), C0);
1429 EXPECT_EQ(Splat1->getSplatValue(), C1);
1430 EXPECT_EQ(Splat0Undef->getSplatValue(), nullptr);
1431 EXPECT_EQ(Splat1Undef->getSplatValue(), nullptr);
1432 EXPECT_EQ(Splat0Poison->getSplatValue(), nullptr);
1433 EXPECT_EQ(Splat1Poison->getSplatValue(), nullptr);
1434 EXPECT_EQ(NotSplat->getSplatValue(), nullptr);
1435 EXPECT_EQ(NotSplatUndef->getSplatValue(), nullptr);
1436 EXPECT_EQ(NotSplatPoison->getSplatValue(), nullptr);
1438 // Disallow poison explicitly.
1439 EXPECT_EQ(Splat0->getSplatValue(false), C0);
1440 EXPECT_EQ(Splat1->getSplatValue(false), C1);
1441 EXPECT_EQ(Splat0Undef->getSplatValue(false), nullptr);
1442 EXPECT_EQ(Splat1Undef->getSplatValue(false), nullptr);
1443 EXPECT_EQ(Splat0Poison->getSplatValue(false), nullptr);
1444 EXPECT_EQ(Splat1Poison->getSplatValue(false), nullptr);
1445 EXPECT_EQ(NotSplat->getSplatValue(false), nullptr);
1446 EXPECT_EQ(NotSplatUndef->getSplatValue(false), nullptr);
1447 EXPECT_EQ(NotSplatPoison->getSplatValue(false), nullptr);
1449 // Allow poison but not undef.
1450 EXPECT_EQ(Splat0->getSplatValue(true), C0);
1451 EXPECT_EQ(Splat1->getSplatValue(true), C1);
1452 EXPECT_EQ(Splat0Undef->getSplatValue(true), nullptr);
1453 EXPECT_EQ(Splat1Undef->getSplatValue(true), nullptr);
1454 EXPECT_EQ(Splat0Poison->getSplatValue(true), C0);
1455 EXPECT_EQ(Splat1Poison->getSplatValue(true), C1);
1456 EXPECT_EQ(NotSplat->getSplatValue(true), nullptr);
1457 EXPECT_EQ(NotSplatUndef->getSplatValue(true), nullptr);
1458 EXPECT_EQ(NotSplatPoison->getSplatValue(true), nullptr);
1461 TEST(InstructionsTest, SkipDebug) {
1462 LLVMContext C;
1463 bool OldDbgValueMode = UseNewDbgInfoFormat;
1464 UseNewDbgInfoFormat = false;
1465 std::unique_ptr<Module> M = parseIR(C,
1467 declare void @llvm.dbg.value(metadata, metadata, metadata)
1469 define void @f() {
1470 entry:
1471 call void @llvm.dbg.value(metadata i32 0, metadata !11, metadata !DIExpression()), !dbg !13
1472 ret void
1475 !llvm.dbg.cu = !{!0}
1476 !llvm.module.flags = !{!3, !4}
1477 !0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 6.0.0", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
1478 !1 = !DIFile(filename: "t2.c", directory: "foo")
1479 !2 = !{}
1480 !3 = !{i32 2, !"Dwarf Version", i32 4}
1481 !4 = !{i32 2, !"Debug Info Version", i32 3}
1482 !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)
1483 !9 = !DISubroutineType(types: !10)
1484 !10 = !{null}
1485 !11 = !DILocalVariable(name: "x", scope: !8, file: !1, line: 2, type: !12)
1486 !12 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
1487 !13 = !DILocation(line: 2, column: 7, scope: !8)
1488 )");
1489 ASSERT_TRUE(M);
1490 Function *F = cast<Function>(M->getNamedValue("f"));
1491 BasicBlock &BB = F->front();
1493 // The first non-debug instruction is the terminator.
1494 auto *Term = BB.getTerminator();
1495 EXPECT_EQ(Term, BB.begin()->getNextNonDebugInstruction());
1496 EXPECT_EQ(Term->getIterator(), skipDebugIntrinsics(BB.begin()));
1498 // After the terminator, there are no non-debug instructions.
1499 EXPECT_EQ(nullptr, Term->getNextNonDebugInstruction());
1500 UseNewDbgInfoFormat = OldDbgValueMode;
1503 TEST(InstructionsTest, PhiMightNotBeFPMathOperator) {
1504 LLVMContext Context;
1505 IRBuilder<> Builder(Context);
1506 MDBuilder MDHelper(Context);
1507 Instruction *I = Builder.CreatePHI(Builder.getInt32Ty(), 0);
1508 EXPECT_FALSE(isa<FPMathOperator>(I));
1509 I->deleteValue();
1510 Instruction *FP = Builder.CreatePHI(Builder.getDoubleTy(), 0);
1511 EXPECT_TRUE(isa<FPMathOperator>(FP));
1512 FP->deleteValue();
1515 TEST(InstructionsTest, FPCallIsFPMathOperator) {
1516 LLVMContext C;
1518 Type *ITy = Type::getInt32Ty(C);
1519 FunctionType *IFnTy = FunctionType::get(ITy, {});
1520 PointerType *PtrTy = PointerType::getUnqual(C);
1521 Value *ICallee = Constant::getNullValue(PtrTy);
1522 std::unique_ptr<CallInst> ICall(CallInst::Create(IFnTy, ICallee, {}, ""));
1523 EXPECT_FALSE(isa<FPMathOperator>(ICall));
1525 Type *VITy = FixedVectorType::get(ITy, 2);
1526 FunctionType *VIFnTy = FunctionType::get(VITy, {});
1527 Value *VICallee = Constant::getNullValue(PtrTy);
1528 std::unique_ptr<CallInst> VICall(CallInst::Create(VIFnTy, VICallee, {}, ""));
1529 EXPECT_FALSE(isa<FPMathOperator>(VICall));
1531 Type *AITy = ArrayType::get(ITy, 2);
1532 FunctionType *AIFnTy = FunctionType::get(AITy, {});
1533 Value *AICallee = Constant::getNullValue(PtrTy);
1534 std::unique_ptr<CallInst> AICall(CallInst::Create(AIFnTy, AICallee, {}, ""));
1535 EXPECT_FALSE(isa<FPMathOperator>(AICall));
1537 Type *FTy = Type::getFloatTy(C);
1538 FunctionType *FFnTy = FunctionType::get(FTy, {});
1539 Value *FCallee = Constant::getNullValue(PtrTy);
1540 std::unique_ptr<CallInst> FCall(CallInst::Create(FFnTy, FCallee, {}, ""));
1541 EXPECT_TRUE(isa<FPMathOperator>(FCall));
1543 Type *VFTy = FixedVectorType::get(FTy, 2);
1544 FunctionType *VFFnTy = FunctionType::get(VFTy, {});
1545 Value *VFCallee = Constant::getNullValue(PtrTy);
1546 std::unique_ptr<CallInst> VFCall(CallInst::Create(VFFnTy, VFCallee, {}, ""));
1547 EXPECT_TRUE(isa<FPMathOperator>(VFCall));
1549 Type *AFTy = ArrayType::get(FTy, 2);
1550 FunctionType *AFFnTy = FunctionType::get(AFTy, {});
1551 Value *AFCallee = Constant::getNullValue(PtrTy);
1552 std::unique_ptr<CallInst> AFCall(CallInst::Create(AFFnTy, AFCallee, {}, ""));
1553 EXPECT_TRUE(isa<FPMathOperator>(AFCall));
1555 Type *AVFTy = ArrayType::get(VFTy, 2);
1556 FunctionType *AVFFnTy = FunctionType::get(AVFTy, {});
1557 Value *AVFCallee = Constant::getNullValue(PtrTy);
1558 std::unique_ptr<CallInst> AVFCall(
1559 CallInst::Create(AVFFnTy, AVFCallee, {}, ""));
1560 EXPECT_TRUE(isa<FPMathOperator>(AVFCall));
1562 Type *StructITy = StructType::get(ITy, ITy);
1563 FunctionType *StructIFnTy = FunctionType::get(StructITy, {});
1564 Value *StructICallee = Constant::getNullValue(PtrTy);
1565 std::unique_ptr<CallInst> StructICall(
1566 CallInst::Create(StructIFnTy, StructICallee, {}, ""));
1567 EXPECT_FALSE(isa<FPMathOperator>(StructICall));
1569 Type *EmptyStructTy = StructType::get(C);
1570 FunctionType *EmptyStructFnTy = FunctionType::get(EmptyStructTy, {});
1571 Value *EmptyStructCallee = Constant::getNullValue(PtrTy);
1572 std::unique_ptr<CallInst> EmptyStructCall(
1573 CallInst::Create(EmptyStructFnTy, EmptyStructCallee, {}, ""));
1574 EXPECT_FALSE(isa<FPMathOperator>(EmptyStructCall));
1576 Type *NamedStructFTy = StructType::create({FTy, FTy}, "AStruct");
1577 FunctionType *NamedStructFFnTy = FunctionType::get(NamedStructFTy, {});
1578 Value *NamedStructFCallee = Constant::getNullValue(PtrTy);
1579 std::unique_ptr<CallInst> NamedStructFCall(
1580 CallInst::Create(NamedStructFFnTy, NamedStructFCallee, {}, ""));
1581 EXPECT_FALSE(isa<FPMathOperator>(NamedStructFCall));
1583 Type *MixedStructTy = StructType::get(FTy, ITy);
1584 FunctionType *MixedStructFnTy = FunctionType::get(MixedStructTy, {});
1585 Value *MixedStructCallee = Constant::getNullValue(PtrTy);
1586 std::unique_ptr<CallInst> MixedStructCall(
1587 CallInst::Create(MixedStructFnTy, MixedStructCallee, {}, ""));
1588 EXPECT_FALSE(isa<FPMathOperator>(MixedStructCall));
1590 Type *StructFTy = StructType::get(FTy, FTy, FTy);
1591 FunctionType *StructFFnTy = FunctionType::get(StructFTy, {});
1592 Value *StructFCallee = Constant::getNullValue(PtrTy);
1593 std::unique_ptr<CallInst> StructFCall(
1594 CallInst::Create(StructFFnTy, StructFCallee, {}, ""));
1595 EXPECT_TRUE(isa<FPMathOperator>(StructFCall));
1597 Type *StructVFTy = StructType::get(VFTy, VFTy, VFTy, VFTy);
1598 FunctionType *StructVFFnTy = FunctionType::get(StructVFTy, {});
1599 Value *StructVFCallee = Constant::getNullValue(PtrTy);
1600 std::unique_ptr<CallInst> StructVFCall(
1601 CallInst::Create(StructVFFnTy, StructVFCallee, {}, ""));
1602 EXPECT_TRUE(isa<FPMathOperator>(StructVFCall));
1604 Type *NestedStructFTy = StructType::get(StructFTy, StructFTy, StructFTy);
1605 FunctionType *NestedStructFFnTy = FunctionType::get(NestedStructFTy, {});
1606 Value *NestedStructFCallee = Constant::getNullValue(PtrTy);
1607 std::unique_ptr<CallInst> NestedStructFCall(
1608 CallInst::Create(NestedStructFFnTy, NestedStructFCallee, {}, ""));
1609 EXPECT_FALSE(isa<FPMathOperator>(NestedStructFCall));
1611 Type *AStructFTy = ArrayType::get(StructFTy, 5);
1612 FunctionType *AStructFFnTy = FunctionType::get(AStructFTy, {});
1613 Value *AStructFCallee = Constant::getNullValue(PtrTy);
1614 std::unique_ptr<CallInst> AStructFCall(
1615 CallInst::Create(AStructFFnTy, AStructFCallee, {}, ""));
1616 EXPECT_FALSE(isa<FPMathOperator>(AStructFCall));
1619 TEST(InstructionsTest, FNegInstruction) {
1620 LLVMContext Context;
1621 Type *FltTy = Type::getFloatTy(Context);
1622 Constant *One = ConstantFP::get(FltTy, 1.0);
1623 BinaryOperator *FAdd = BinaryOperator::CreateFAdd(One, One);
1624 FAdd->setHasNoNaNs(true);
1625 UnaryOperator *FNeg = UnaryOperator::CreateFNegFMF(One, FAdd);
1626 EXPECT_TRUE(FNeg->hasNoNaNs());
1627 EXPECT_FALSE(FNeg->hasNoInfs());
1628 EXPECT_FALSE(FNeg->hasNoSignedZeros());
1629 EXPECT_FALSE(FNeg->hasAllowReciprocal());
1630 EXPECT_FALSE(FNeg->hasAllowContract());
1631 EXPECT_FALSE(FNeg->hasAllowReassoc());
1632 EXPECT_FALSE(FNeg->hasApproxFunc());
1633 FAdd->deleteValue();
1634 FNeg->deleteValue();
1637 TEST(InstructionsTest, CallBrInstruction) {
1638 LLVMContext Context;
1639 std::unique_ptr<Module> M = parseIR(Context, R"(
1640 define void @foo() {
1641 entry:
1642 callbr void asm sideeffect "// XXX: ${0:l}", "!i"()
1643 to label %land.rhs.i [label %branch_test.exit]
1645 land.rhs.i:
1646 br label %branch_test.exit
1648 branch_test.exit:
1649 %0 = phi i1 [ true, %entry ], [ false, %land.rhs.i ]
1650 br i1 %0, label %if.end, label %if.then
1652 if.then:
1653 ret void
1655 if.end:
1656 ret void
1658 )");
1659 Function *Foo = M->getFunction("foo");
1660 auto BBs = Foo->begin();
1661 CallBrInst &CBI = cast<CallBrInst>(BBs->front());
1662 ++BBs;
1663 ++BBs;
1664 BasicBlock &BranchTestExit = *BBs;
1665 ++BBs;
1666 BasicBlock &IfThen = *BBs;
1668 // Test that setting the first indirect destination of callbr updates the dest
1669 EXPECT_EQ(&BranchTestExit, CBI.getIndirectDest(0));
1670 CBI.setIndirectDest(0, &IfThen);
1671 EXPECT_EQ(&IfThen, CBI.getIndirectDest(0));
1674 TEST(InstructionsTest, UnaryOperator) {
1675 LLVMContext Context;
1676 IRBuilder<> Builder(Context);
1677 Instruction *I = Builder.CreatePHI(Builder.getDoubleTy(), 0);
1678 Value *F = Builder.CreateFNeg(I);
1680 EXPECT_TRUE(isa<Value>(F));
1681 EXPECT_TRUE(isa<Instruction>(F));
1682 EXPECT_TRUE(isa<UnaryInstruction>(F));
1683 EXPECT_TRUE(isa<UnaryOperator>(F));
1684 EXPECT_FALSE(isa<BinaryOperator>(F));
1686 F->deleteValue();
1687 I->deleteValue();
1690 TEST(InstructionsTest, DropLocation) {
1691 LLVMContext C;
1692 std::unique_ptr<Module> M = parseIR(C,
1694 declare void @callee()
1696 define void @no_parent_scope() {
1697 call void @callee() ; I1: Call with no location.
1698 call void @callee(), !dbg !11 ; I2: Call with location.
1699 ret void, !dbg !11 ; I3: Non-call with location.
1702 define void @with_parent_scope() !dbg !8 {
1703 call void @callee() ; I1: Call with no location.
1704 call void @callee(), !dbg !11 ; I2: Call with location.
1705 ret void, !dbg !11 ; I3: Non-call with location.
1708 !llvm.dbg.cu = !{!0}
1709 !llvm.module.flags = !{!3, !4}
1710 !0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
1711 !1 = !DIFile(filename: "t2.c", directory: "foo")
1712 !2 = !{}
1713 !3 = !{i32 2, !"Dwarf Version", i32 4}
1714 !4 = !{i32 2, !"Debug Info Version", i32 3}
1715 !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)
1716 !9 = !DISubroutineType(types: !10)
1717 !10 = !{null}
1718 !11 = !DILocation(line: 2, column: 7, scope: !8, inlinedAt: !12)
1719 !12 = !DILocation(line: 3, column: 8, scope: !8)
1720 )");
1721 ASSERT_TRUE(M);
1724 Function *NoParentScopeF =
1725 cast<Function>(M->getNamedValue("no_parent_scope"));
1726 BasicBlock &BB = NoParentScopeF->front();
1728 auto *I1 = BB.getFirstNonPHI();
1729 auto *I2 = I1->getNextNode();
1730 auto *I3 = BB.getTerminator();
1732 EXPECT_EQ(I1->getDebugLoc(), DebugLoc());
1733 I1->dropLocation();
1734 EXPECT_EQ(I1->getDebugLoc(), DebugLoc());
1736 EXPECT_EQ(I2->getDebugLoc().getLine(), 2U);
1737 I2->dropLocation();
1738 EXPECT_EQ(I1->getDebugLoc(), DebugLoc());
1740 EXPECT_EQ(I3->getDebugLoc().getLine(), 2U);
1741 I3->dropLocation();
1742 EXPECT_EQ(I3->getDebugLoc(), DebugLoc());
1746 Function *WithParentScopeF =
1747 cast<Function>(M->getNamedValue("with_parent_scope"));
1748 BasicBlock &BB = WithParentScopeF->front();
1750 auto *I2 = BB.getFirstNonPHI()->getNextNode();
1752 MDNode *Scope = cast<MDNode>(WithParentScopeF->getSubprogram());
1753 EXPECT_EQ(I2->getDebugLoc().getLine(), 2U);
1754 I2->dropLocation();
1755 EXPECT_EQ(I2->getDebugLoc().getLine(), 0U);
1756 EXPECT_EQ(I2->getDebugLoc().getScope(), Scope);
1757 EXPECT_EQ(I2->getDebugLoc().getInlinedAt(), nullptr);
1761 TEST(InstructionsTest, BranchWeightOverflow) {
1762 LLVMContext C;
1763 std::unique_ptr<Module> M = parseIR(C,
1765 declare void @callee()
1767 define void @caller() {
1768 call void @callee(), !prof !1
1769 ret void
1772 !1 = !{!"branch_weights", i32 20000}
1773 )");
1774 ASSERT_TRUE(M);
1775 CallInst *CI =
1776 cast<CallInst>(&M->getFunction("caller")->getEntryBlock().front());
1777 uint64_t ProfWeight;
1778 CI->extractProfTotalWeight(ProfWeight);
1779 ASSERT_EQ(ProfWeight, 20000U);
1780 CI->updateProfWeight(10000000, 1);
1781 CI->extractProfTotalWeight(ProfWeight);
1782 ASSERT_EQ(ProfWeight, UINT32_MAX);
1785 TEST(InstructionsTest, FreezeInst) {
1786 LLVMContext C;
1787 std::unique_ptr<Module> M = parseIR(C,
1789 define void @foo(i8 %arg) {
1790 freeze i8 %arg
1791 ret void
1793 )");
1794 ASSERT_TRUE(M);
1795 Value *FI = &M->getFunction("foo")->getEntryBlock().front();
1796 EXPECT_TRUE(isa<UnaryInstruction>(FI));
1799 TEST(InstructionsTest, AllocaInst) {
1800 LLVMContext Ctx;
1801 std::unique_ptr<Module> M = parseIR(Ctx, R"(
1802 %T = type { i64, [3 x i32]}
1803 define void @f(i32 %n) {
1804 entry:
1805 %A = alloca i32, i32 1
1806 %B = alloca i32, i32 4
1807 %C = alloca i32, i32 %n
1808 %D = alloca double
1809 %E = alloca <vscale x 8 x double>
1810 %F = alloca [2 x half]
1811 %G = alloca [2 x [3 x i128]]
1812 %H = alloca %T
1813 %I = alloca i32, i64 9223372036854775807
1814 ret void
1816 )");
1817 const DataLayout &DL = M->getDataLayout();
1818 ASSERT_TRUE(M);
1819 Function *Fun = cast<Function>(M->getNamedValue("f"));
1820 BasicBlock &BB = Fun->front();
1821 auto It = BB.begin();
1822 AllocaInst &A = cast<AllocaInst>(*It++);
1823 AllocaInst &B = cast<AllocaInst>(*It++);
1824 AllocaInst &C = cast<AllocaInst>(*It++);
1825 AllocaInst &D = cast<AllocaInst>(*It++);
1826 AllocaInst &E = cast<AllocaInst>(*It++);
1827 AllocaInst &F = cast<AllocaInst>(*It++);
1828 AllocaInst &G = cast<AllocaInst>(*It++);
1829 AllocaInst &H = cast<AllocaInst>(*It++);
1830 AllocaInst &I = cast<AllocaInst>(*It++);
1831 EXPECT_EQ(A.getAllocationSizeInBits(DL), TypeSize::getFixed(32));
1832 EXPECT_EQ(B.getAllocationSizeInBits(DL), TypeSize::getFixed(128));
1833 EXPECT_FALSE(C.getAllocationSizeInBits(DL));
1834 EXPECT_EQ(DL.getTypeSizeInBits(D.getAllocatedType()), TypeSize::getFixed(64));
1835 EXPECT_EQ(D.getAllocationSizeInBits(DL), TypeSize::getFixed(64));
1836 EXPECT_EQ(E.getAllocationSizeInBits(DL), TypeSize::getScalable(512));
1837 EXPECT_EQ(F.getAllocationSizeInBits(DL), TypeSize::getFixed(32));
1838 EXPECT_EQ(G.getAllocationSizeInBits(DL), TypeSize::getFixed(768));
1839 EXPECT_EQ(H.getAllocationSizeInBits(DL), TypeSize::getFixed(160));
1840 EXPECT_FALSE(I.getAllocationSizeInBits(DL));
1843 TEST(InstructionsTest, InsertAtBegin) {
1844 LLVMContext Ctx;
1845 std::unique_ptr<Module> M = parseIR(Ctx, R"(
1846 define void @f(i32 %a, i32 %b) {
1847 entry:
1848 ret void
1850 )");
1851 Function *F = &*M->begin();
1852 Argument *ArgA = F->getArg(0);
1853 Argument *ArgB = F->getArg(1);
1854 BasicBlock *BB = &*F->begin();
1855 Instruction *Ret = &*BB->begin();
1856 Instruction *I = BinaryOperator::CreateAdd(ArgA, ArgB);
1857 auto It = I->insertInto(BB, BB->begin());
1858 EXPECT_EQ(&*It, I);
1859 EXPECT_EQ(I->getNextNode(), Ret);
1862 TEST(InstructionsTest, InsertAtEnd) {
1863 LLVMContext Ctx;
1864 std::unique_ptr<Module> M = parseIR(Ctx, R"(
1865 define void @f(i32 %a, i32 %b) {
1866 entry:
1867 ret void
1869 )");
1870 Function *F = &*M->begin();
1871 Argument *ArgA = F->getArg(0);
1872 Argument *ArgB = F->getArg(1);
1873 BasicBlock *BB = &*F->begin();
1874 Instruction *Ret = &*BB->begin();
1875 Instruction *I = BinaryOperator::CreateAdd(ArgA, ArgB);
1876 auto It = I->insertInto(BB, BB->end());
1877 EXPECT_EQ(&*It, I);
1878 EXPECT_EQ(Ret->getNextNode(), I);
1881 TEST(InstructionsTest, AtomicSyncscope) {
1882 LLVMContext Ctx;
1884 Module M("Mod", Ctx);
1885 FunctionType *FT = FunctionType::get(Type::getVoidTy(Ctx), {}, false);
1886 Function *F = Function::Create(FT, Function::ExternalLinkage, "Fun", M);
1887 BasicBlock *BB = BasicBlock::Create(Ctx, "Entry", F);
1888 IRBuilder<> Builder(BB);
1890 // SyncScope-variants of LLVM C IRBuilder APIs are tested by llvm-c-test,
1891 // so cover the old versions (with a SingleThreaded argument) here.
1892 Value *Ptr = ConstantPointerNull::get(Builder.getPtrTy());
1893 Value *Val = ConstantInt::get(Type::getInt32Ty(Ctx), 0);
1895 // fence
1896 LLVMValueRef Fence = LLVMBuildFence(
1897 wrap(&Builder), LLVMAtomicOrderingSequentiallyConsistent, 0, "");
1898 EXPECT_FALSE(LLVMIsAtomicSingleThread(Fence));
1899 Fence = LLVMBuildFence(wrap(&Builder),
1900 LLVMAtomicOrderingSequentiallyConsistent, 1, "");
1901 EXPECT_TRUE(LLVMIsAtomicSingleThread(Fence));
1903 // atomicrmw
1904 LLVMValueRef AtomicRMW = LLVMBuildAtomicRMW(
1905 wrap(&Builder), LLVMAtomicRMWBinOpXchg, wrap(Ptr), wrap(Val),
1906 LLVMAtomicOrderingSequentiallyConsistent, 0);
1907 EXPECT_FALSE(LLVMIsAtomicSingleThread(AtomicRMW));
1908 AtomicRMW = LLVMBuildAtomicRMW(wrap(&Builder), LLVMAtomicRMWBinOpXchg,
1909 wrap(Ptr), wrap(Val),
1910 LLVMAtomicOrderingSequentiallyConsistent, 1);
1911 EXPECT_TRUE(LLVMIsAtomicSingleThread(AtomicRMW));
1913 // cmpxchg
1914 LLVMValueRef CmpXchg =
1915 LLVMBuildAtomicCmpXchg(wrap(&Builder), wrap(Ptr), wrap(Val), wrap(Val),
1916 LLVMAtomicOrderingSequentiallyConsistent,
1917 LLVMAtomicOrderingSequentiallyConsistent, 0);
1918 EXPECT_FALSE(LLVMIsAtomicSingleThread(CmpXchg));
1919 CmpXchg =
1920 LLVMBuildAtomicCmpXchg(wrap(&Builder), wrap(Ptr), wrap(Val), wrap(Val),
1921 LLVMAtomicOrderingSequentiallyConsistent,
1922 LLVMAtomicOrderingSequentiallyConsistent, 1);
1923 EXPECT_TRUE(LLVMIsAtomicSingleThread(CmpXchg));
1926 } // end anonymous namespace
1927 } // end namespace llvm