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 //===----------------------------------------------------------------------===//
8
9 #include "llvm/AsmParser/Parser.h"
10 #include "llvm/IR/Instructions.h"
11 #include "llvm/ADT/STLExtras.h"
12 #include "llvm/Analysis/ValueTracking.h"
13 #include "llvm/IR/BasicBlock.h"
14 #include "llvm/IR/Constants.h"
15 #include "llvm/IR/DataLayout.h"
16 #include "llvm/IR/DerivedTypes.h"
17 #include "llvm/IR/Function.h"
18 #include "llvm/IR/IRBuilder.h"
19 #include "llvm/IR/LLVMContext.h"
20 #include "llvm/IR/MDBuilder.h"
21 #include "llvm/IR/Module.h"
22 #include "llvm/IR/NoFolder.h"
23 #include "llvm/IR/Operator.h"
24 #include "llvm/Support/SourceMgr.h"
25 #include "gmock/gmock-matchers.h"
26 #include "gtest/gtest.h"
27 #include <memory>
28
29 namespace llvm {
30 namespace {
31
parseIR(LLVMContext & C,const char * IR)32 static std::unique_ptr<Module> parseIR(LLVMContext &C, const char *IR) {
33 SMDiagnostic Err;
34 std::unique_ptr<Module> Mod = parseAssemblyString(IR, Err, C);
35 if (!Mod)
36 Err.print("InstructionsTests", errs());
37 return Mod;
38 }
39
TEST(InstructionsTest,ReturnInst)40 TEST(InstructionsTest, ReturnInst) {
41 LLVMContext C;
42
43 // test for PR6589
44 const ReturnInst* r0 = ReturnInst::Create(C);
45 EXPECT_EQ(r0->getNumOperands(), 0U);
46 EXPECT_EQ(r0->op_begin(), r0->op_end());
47
48 IntegerType* Int1 = IntegerType::get(C, 1);
49 Constant* One = ConstantInt::get(Int1, 1, true);
50 const ReturnInst* r1 = ReturnInst::Create(C, One);
51 EXPECT_EQ(1U, r1->getNumOperands());
52 User::const_op_iterator b(r1->op_begin());
53 EXPECT_NE(r1->op_end(), b);
54 EXPECT_EQ(One, *b);
55 EXPECT_EQ(One, r1->getOperand(0));
56 ++b;
57 EXPECT_EQ(r1->op_end(), b);
58
59 // clean up
60 delete r0;
61 delete r1;
62 }
63
64 // Test fixture that provides a module and a single function within it. Useful
65 // for tests that need to refer to the function in some way.
66 class ModuleWithFunctionTest : public testing::Test {
67 protected:
ModuleWithFunctionTest()68 ModuleWithFunctionTest() : M(new Module("MyModule", Ctx)) {
69 FArgTypes.push_back(Type::getInt8Ty(Ctx));
70 FArgTypes.push_back(Type::getInt32Ty(Ctx));
71 FArgTypes.push_back(Type::getInt64Ty(Ctx));
72 FunctionType *FTy =
73 FunctionType::get(Type::getVoidTy(Ctx), FArgTypes, false);
74 F = Function::Create(FTy, Function::ExternalLinkage, "", M.get());
75 }
76
77 LLVMContext Ctx;
78 std::unique_ptr<Module> M;
79 SmallVector<Type *, 3> FArgTypes;
80 Function *F;
81 };
82
TEST_F(ModuleWithFunctionTest,CallInst)83 TEST_F(ModuleWithFunctionTest, CallInst) {
84 Value *Args[] = {ConstantInt::get(Type::getInt8Ty(Ctx), 20),
85 ConstantInt::get(Type::getInt32Ty(Ctx), 9999),
86 ConstantInt::get(Type::getInt64Ty(Ctx), 42)};
87 std::unique_ptr<CallInst> Call(CallInst::Create(F, Args));
88
89 // Make sure iteration over a call's arguments works as expected.
90 unsigned Idx = 0;
91 for (Value *Arg : Call->arg_operands()) {
92 EXPECT_EQ(FArgTypes[Idx], Arg->getType());
93 EXPECT_EQ(Call->getArgOperand(Idx)->getType(), Arg->getType());
94 Idx++;
95 }
96 }
97
TEST_F(ModuleWithFunctionTest,InvokeInst)98 TEST_F(ModuleWithFunctionTest, InvokeInst) {
99 BasicBlock *BB1 = BasicBlock::Create(Ctx, "", F);
100 BasicBlock *BB2 = BasicBlock::Create(Ctx, "", F);
101
102 Value *Args[] = {ConstantInt::get(Type::getInt8Ty(Ctx), 20),
103 ConstantInt::get(Type::getInt32Ty(Ctx), 9999),
104 ConstantInt::get(Type::getInt64Ty(Ctx), 42)};
105 std::unique_ptr<InvokeInst> Invoke(InvokeInst::Create(F, BB1, BB2, Args));
106
107 // Make sure iteration over invoke's arguments works as expected.
108 unsigned Idx = 0;
109 for (Value *Arg : Invoke->arg_operands()) {
110 EXPECT_EQ(FArgTypes[Idx], Arg->getType());
111 EXPECT_EQ(Invoke->getArgOperand(Idx)->getType(), Arg->getType());
112 Idx++;
113 }
114 }
115
TEST(InstructionsTest,BranchInst)116 TEST(InstructionsTest, BranchInst) {
117 LLVMContext C;
118
119 // Make a BasicBlocks
120 BasicBlock* bb0 = BasicBlock::Create(C);
121 BasicBlock* bb1 = BasicBlock::Create(C);
122
123 // Mandatory BranchInst
124 const BranchInst* b0 = BranchInst::Create(bb0);
125
126 EXPECT_TRUE(b0->isUnconditional());
127 EXPECT_FALSE(b0->isConditional());
128 EXPECT_EQ(1U, b0->getNumSuccessors());
129
130 // check num operands
131 EXPECT_EQ(1U, b0->getNumOperands());
132
133 EXPECT_NE(b0->op_begin(), b0->op_end());
134 EXPECT_EQ(b0->op_end(), std::next(b0->op_begin()));
135
136 EXPECT_EQ(b0->op_end(), std::next(b0->op_begin()));
137
138 IntegerType* Int1 = IntegerType::get(C, 1);
139 Constant* One = ConstantInt::get(Int1, 1, true);
140
141 // Conditional BranchInst
142 BranchInst* b1 = BranchInst::Create(bb0, bb1, One);
143
144 EXPECT_FALSE(b1->isUnconditional());
145 EXPECT_TRUE(b1->isConditional());
146 EXPECT_EQ(2U, b1->getNumSuccessors());
147
148 // check num operands
149 EXPECT_EQ(3U, b1->getNumOperands());
150
151 User::const_op_iterator b(b1->op_begin());
152
153 // check COND
154 EXPECT_NE(b, b1->op_end());
155 EXPECT_EQ(One, *b);
156 EXPECT_EQ(One, b1->getOperand(0));
157 EXPECT_EQ(One, b1->getCondition());
158 ++b;
159
160 // check ELSE
161 EXPECT_EQ(bb1, *b);
162 EXPECT_EQ(bb1, b1->getOperand(1));
163 EXPECT_EQ(bb1, b1->getSuccessor(1));
164 ++b;
165
166 // check THEN
167 EXPECT_EQ(bb0, *b);
168 EXPECT_EQ(bb0, b1->getOperand(2));
169 EXPECT_EQ(bb0, b1->getSuccessor(0));
170 ++b;
171
172 EXPECT_EQ(b1->op_end(), b);
173
174 // clean up
175 delete b0;
176 delete b1;
177
178 delete bb0;
179 delete bb1;
180 }
181
TEST(InstructionsTest,CastInst)182 TEST(InstructionsTest, CastInst) {
183 LLVMContext C;
184
185 Type *Int8Ty = Type::getInt8Ty(C);
186 Type *Int16Ty = Type::getInt16Ty(C);
187 Type *Int32Ty = Type::getInt32Ty(C);
188 Type *Int64Ty = Type::getInt64Ty(C);
189 Type *V8x8Ty = VectorType::get(Int8Ty, 8);
190 Type *V8x64Ty = VectorType::get(Int64Ty, 8);
191 Type *X86MMXTy = Type::getX86_MMXTy(C);
192
193 Type *HalfTy = Type::getHalfTy(C);
194 Type *FloatTy = Type::getFloatTy(C);
195 Type *DoubleTy = Type::getDoubleTy(C);
196
197 Type *V2Int32Ty = VectorType::get(Int32Ty, 2);
198 Type *V2Int64Ty = VectorType::get(Int64Ty, 2);
199 Type *V4Int16Ty = VectorType::get(Int16Ty, 4);
200
201 Type *Int32PtrTy = PointerType::get(Int32Ty, 0);
202 Type *Int64PtrTy = PointerType::get(Int64Ty, 0);
203
204 Type *Int32PtrAS1Ty = PointerType::get(Int32Ty, 1);
205 Type *Int64PtrAS1Ty = PointerType::get(Int64Ty, 1);
206
207 Type *V2Int32PtrAS1Ty = VectorType::get(Int32PtrAS1Ty, 2);
208 Type *V2Int64PtrAS1Ty = VectorType::get(Int64PtrAS1Ty, 2);
209 Type *V4Int32PtrAS1Ty = VectorType::get(Int32PtrAS1Ty, 4);
210 Type *V4Int64PtrAS1Ty = VectorType::get(Int64PtrAS1Ty, 4);
211
212 Type *V2Int64PtrTy = VectorType::get(Int64PtrTy, 2);
213 Type *V2Int32PtrTy = VectorType::get(Int32PtrTy, 2);
214 Type *V4Int32PtrTy = VectorType::get(Int32PtrTy, 4);
215
216 const Constant* c8 = Constant::getNullValue(V8x8Ty);
217 const Constant* c64 = Constant::getNullValue(V8x64Ty);
218
219 const Constant *v2ptr32 = Constant::getNullValue(V2Int32PtrTy);
220
221 EXPECT_TRUE(CastInst::isCastable(V8x8Ty, X86MMXTy));
222 EXPECT_TRUE(CastInst::isCastable(X86MMXTy, V8x8Ty));
223 EXPECT_FALSE(CastInst::isCastable(Int64Ty, X86MMXTy));
224 EXPECT_TRUE(CastInst::isCastable(V8x64Ty, V8x8Ty));
225 EXPECT_TRUE(CastInst::isCastable(V8x8Ty, V8x64Ty));
226 EXPECT_EQ(CastInst::Trunc, CastInst::getCastOpcode(c64, true, V8x8Ty, true));
227 EXPECT_EQ(CastInst::SExt, CastInst::getCastOpcode(c8, true, V8x64Ty, true));
228
229 EXPECT_FALSE(CastInst::isBitCastable(V8x8Ty, X86MMXTy));
230 EXPECT_FALSE(CastInst::isBitCastable(X86MMXTy, V8x8Ty));
231 EXPECT_FALSE(CastInst::isBitCastable(Int64Ty, X86MMXTy));
232 EXPECT_FALSE(CastInst::isBitCastable(V8x64Ty, V8x8Ty));
233 EXPECT_FALSE(CastInst::isBitCastable(V8x8Ty, V8x64Ty));
234
235 // Check address space casts are rejected since we don't know the sizes here
236 EXPECT_FALSE(CastInst::isBitCastable(Int32PtrTy, Int32PtrAS1Ty));
237 EXPECT_FALSE(CastInst::isBitCastable(Int32PtrAS1Ty, Int32PtrTy));
238 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrTy, V2Int32PtrAS1Ty));
239 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V2Int32PtrTy));
240 EXPECT_TRUE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V2Int64PtrAS1Ty));
241 EXPECT_TRUE(CastInst::isCastable(V2Int32PtrAS1Ty, V2Int32PtrTy));
242 EXPECT_EQ(CastInst::AddrSpaceCast, CastInst::getCastOpcode(v2ptr32, true,
243 V2Int32PtrAS1Ty,
244 true));
245
246 // Test mismatched number of elements for pointers
247 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V4Int64PtrAS1Ty));
248 EXPECT_FALSE(CastInst::isBitCastable(V4Int64PtrAS1Ty, V2Int32PtrAS1Ty));
249 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V4Int32PtrAS1Ty));
250 EXPECT_FALSE(CastInst::isBitCastable(Int32PtrTy, V2Int32PtrTy));
251 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrTy, Int32PtrTy));
252
253 EXPECT_TRUE(CastInst::isBitCastable(Int32PtrTy, Int64PtrTy));
254 EXPECT_FALSE(CastInst::isBitCastable(DoubleTy, FloatTy));
255 EXPECT_FALSE(CastInst::isBitCastable(FloatTy, DoubleTy));
256 EXPECT_TRUE(CastInst::isBitCastable(FloatTy, FloatTy));
257 EXPECT_TRUE(CastInst::isBitCastable(FloatTy, FloatTy));
258 EXPECT_TRUE(CastInst::isBitCastable(FloatTy, Int32Ty));
259 EXPECT_TRUE(CastInst::isBitCastable(Int16Ty, HalfTy));
260 EXPECT_TRUE(CastInst::isBitCastable(Int32Ty, FloatTy));
261 EXPECT_TRUE(CastInst::isBitCastable(V2Int32Ty, Int64Ty));
262
263 EXPECT_TRUE(CastInst::isBitCastable(V2Int32Ty, V4Int16Ty));
264 EXPECT_FALSE(CastInst::isBitCastable(Int32Ty, Int64Ty));
265 EXPECT_FALSE(CastInst::isBitCastable(Int64Ty, Int32Ty));
266
267 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrTy, Int64Ty));
268 EXPECT_FALSE(CastInst::isBitCastable(Int64Ty, V2Int32PtrTy));
269 EXPECT_TRUE(CastInst::isBitCastable(V2Int64PtrTy, V2Int32PtrTy));
270 EXPECT_TRUE(CastInst::isBitCastable(V2Int32PtrTy, V2Int64PtrTy));
271 EXPECT_FALSE(CastInst::isBitCastable(V2Int32Ty, V2Int64Ty));
272 EXPECT_FALSE(CastInst::isBitCastable(V2Int64Ty, V2Int32Ty));
273
274
275 EXPECT_FALSE(CastInst::castIsValid(Instruction::BitCast,
276 Constant::getNullValue(V4Int32PtrTy),
277 V2Int32PtrTy));
278 EXPECT_FALSE(CastInst::castIsValid(Instruction::BitCast,
279 Constant::getNullValue(V2Int32PtrTy),
280 V4Int32PtrTy));
281
282 EXPECT_FALSE(CastInst::castIsValid(Instruction::AddrSpaceCast,
283 Constant::getNullValue(V4Int32PtrAS1Ty),
284 V2Int32PtrTy));
285 EXPECT_FALSE(CastInst::castIsValid(Instruction::AddrSpaceCast,
286 Constant::getNullValue(V2Int32PtrTy),
287 V4Int32PtrAS1Ty));
288
289
290 // Check that assertion is not hit when creating a cast with a vector of
291 // pointers
292 // First form
293 BasicBlock *BB = BasicBlock::Create(C);
294 Constant *NullV2I32Ptr = Constant::getNullValue(V2Int32PtrTy);
295 auto Inst1 = CastInst::CreatePointerCast(NullV2I32Ptr, V2Int32Ty, "foo", BB);
296
297 // Second form
298 auto Inst2 = CastInst::CreatePointerCast(NullV2I32Ptr, V2Int32Ty);
299
300 delete Inst2;
301 Inst1->eraseFromParent();
302 delete BB;
303 }
304
TEST(InstructionsTest,VectorGep)305 TEST(InstructionsTest, VectorGep) {
306 LLVMContext C;
307
308 // Type Definitions
309 Type *I8Ty = IntegerType::get(C, 8);
310 Type *I32Ty = IntegerType::get(C, 32);
311 PointerType *Ptri8Ty = PointerType::get(I8Ty, 0);
312 PointerType *Ptri32Ty = PointerType::get(I32Ty, 0);
313
314 VectorType *V2xi8PTy = VectorType::get(Ptri8Ty, 2);
315 VectorType *V2xi32PTy = VectorType::get(Ptri32Ty, 2);
316
317 // Test different aspects of the vector-of-pointers type
318 // and GEPs which use this type.
319 ConstantInt *Ci32a = ConstantInt::get(C, APInt(32, 1492));
320 ConstantInt *Ci32b = ConstantInt::get(C, APInt(32, 1948));
321 std::vector<Constant*> ConstVa(2, Ci32a);
322 std::vector<Constant*> ConstVb(2, Ci32b);
323 Constant *C2xi32a = ConstantVector::get(ConstVa);
324 Constant *C2xi32b = ConstantVector::get(ConstVb);
325
326 CastInst *PtrVecA = new IntToPtrInst(C2xi32a, V2xi32PTy);
327 CastInst *PtrVecB = new IntToPtrInst(C2xi32b, V2xi32PTy);
328
329 ICmpInst *ICmp0 = new ICmpInst(ICmpInst::ICMP_SGT, PtrVecA, PtrVecB);
330 ICmpInst *ICmp1 = new ICmpInst(ICmpInst::ICMP_ULT, PtrVecA, PtrVecB);
331 EXPECT_NE(ICmp0, ICmp1); // suppress warning.
332
333 BasicBlock* BB0 = BasicBlock::Create(C);
334 // Test InsertAtEnd ICmpInst constructor.
335 ICmpInst *ICmp2 = new ICmpInst(*BB0, ICmpInst::ICMP_SGE, PtrVecA, PtrVecB);
336 EXPECT_NE(ICmp0, ICmp2); // suppress warning.
337
338 GetElementPtrInst *Gep0 = GetElementPtrInst::Create(I32Ty, PtrVecA, C2xi32a);
339 GetElementPtrInst *Gep1 = GetElementPtrInst::Create(I32Ty, PtrVecA, C2xi32b);
340 GetElementPtrInst *Gep2 = GetElementPtrInst::Create(I32Ty, PtrVecB, C2xi32a);
341 GetElementPtrInst *Gep3 = GetElementPtrInst::Create(I32Ty, PtrVecB, C2xi32b);
342
343 CastInst *BTC0 = new BitCastInst(Gep0, V2xi8PTy);
344 CastInst *BTC1 = new BitCastInst(Gep1, V2xi8PTy);
345 CastInst *BTC2 = new BitCastInst(Gep2, V2xi8PTy);
346 CastInst *BTC3 = new BitCastInst(Gep3, V2xi8PTy);
347
348 Value *S0 = BTC0->stripPointerCasts();
349 Value *S1 = BTC1->stripPointerCasts();
350 Value *S2 = BTC2->stripPointerCasts();
351 Value *S3 = BTC3->stripPointerCasts();
352
353 EXPECT_NE(S0, Gep0);
354 EXPECT_NE(S1, Gep1);
355 EXPECT_NE(S2, Gep2);
356 EXPECT_NE(S3, Gep3);
357
358 int64_t Offset;
359 DataLayout TD("e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f3"
360 "2:32:32-f64:64:64-v64:64:64-v128:128:128-a:0:64-s:64:64-f80"
361 ":128:128-n8:16:32:64-S128");
362 // Make sure we don't crash
363 GetPointerBaseWithConstantOffset(Gep0, Offset, TD);
364 GetPointerBaseWithConstantOffset(Gep1, Offset, TD);
365 GetPointerBaseWithConstantOffset(Gep2, Offset, TD);
366 GetPointerBaseWithConstantOffset(Gep3, Offset, TD);
367
368 // Gep of Geps
369 GetElementPtrInst *GepII0 = GetElementPtrInst::Create(I32Ty, Gep0, C2xi32b);
370 GetElementPtrInst *GepII1 = GetElementPtrInst::Create(I32Ty, Gep1, C2xi32a);
371 GetElementPtrInst *GepII2 = GetElementPtrInst::Create(I32Ty, Gep2, C2xi32b);
372 GetElementPtrInst *GepII3 = GetElementPtrInst::Create(I32Ty, Gep3, C2xi32a);
373
374 EXPECT_EQ(GepII0->getNumIndices(), 1u);
375 EXPECT_EQ(GepII1->getNumIndices(), 1u);
376 EXPECT_EQ(GepII2->getNumIndices(), 1u);
377 EXPECT_EQ(GepII3->getNumIndices(), 1u);
378
379 EXPECT_FALSE(GepII0->hasAllZeroIndices());
380 EXPECT_FALSE(GepII1->hasAllZeroIndices());
381 EXPECT_FALSE(GepII2->hasAllZeroIndices());
382 EXPECT_FALSE(GepII3->hasAllZeroIndices());
383
384 delete GepII0;
385 delete GepII1;
386 delete GepII2;
387 delete GepII3;
388
389 delete BTC0;
390 delete BTC1;
391 delete BTC2;
392 delete BTC3;
393
394 delete Gep0;
395 delete Gep1;
396 delete Gep2;
397 delete Gep3;
398
399 ICmp2->eraseFromParent();
400 delete BB0;
401
402 delete ICmp0;
403 delete ICmp1;
404 delete PtrVecA;
405 delete PtrVecB;
406 }
407
TEST(InstructionsTest,FPMathOperator)408 TEST(InstructionsTest, FPMathOperator) {
409 LLVMContext Context;
410 IRBuilder<> Builder(Context);
411 MDBuilder MDHelper(Context);
412 Instruction *I = Builder.CreatePHI(Builder.getDoubleTy(), 0);
413 MDNode *MD1 = MDHelper.createFPMath(1.0);
414 Value *V1 = Builder.CreateFAdd(I, I, "", MD1);
415 EXPECT_TRUE(isa<FPMathOperator>(V1));
416 FPMathOperator *O1 = cast<FPMathOperator>(V1);
417 EXPECT_EQ(O1->getFPAccuracy(), 1.0);
418 V1->deleteValue();
419 I->deleteValue();
420 }
421
422
TEST(InstructionsTest,isEliminableCastPair)423 TEST(InstructionsTest, isEliminableCastPair) {
424 LLVMContext C;
425
426 Type* Int16Ty = Type::getInt16Ty(C);
427 Type* Int32Ty = Type::getInt32Ty(C);
428 Type* Int64Ty = Type::getInt64Ty(C);
429 Type* Int64PtrTy = Type::getInt64PtrTy(C);
430
431 // Source and destination pointers have same size -> bitcast.
432 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt,
433 CastInst::IntToPtr,
434 Int64PtrTy, Int64Ty, Int64PtrTy,
435 Int32Ty, nullptr, Int32Ty),
436 CastInst::BitCast);
437
438 // Source and destination have unknown sizes, but the same address space and
439 // the intermediate int is the maximum pointer size -> bitcast
440 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt,
441 CastInst::IntToPtr,
442 Int64PtrTy, Int64Ty, Int64PtrTy,
443 nullptr, nullptr, nullptr),
444 CastInst::BitCast);
445
446 // Source and destination have unknown sizes, but the same address space and
447 // the intermediate int is not the maximum pointer size -> nothing
448 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt,
449 CastInst::IntToPtr,
450 Int64PtrTy, Int32Ty, Int64PtrTy,
451 nullptr, nullptr, nullptr),
452 0U);
453
454 // Middle pointer big enough -> bitcast.
455 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
456 CastInst::PtrToInt,
457 Int64Ty, Int64PtrTy, Int64Ty,
458 nullptr, Int64Ty, nullptr),
459 CastInst::BitCast);
460
461 // Middle pointer too small -> fail.
462 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
463 CastInst::PtrToInt,
464 Int64Ty, Int64PtrTy, Int64Ty,
465 nullptr, Int32Ty, nullptr),
466 0U);
467
468 // Test that we don't eliminate bitcasts between different address spaces,
469 // or if we don't have available pointer size information.
470 DataLayout DL("e-p:32:32:32-p1:16:16:16-p2:64:64:64-i1:8:8-i8:8:8-i16:16:16"
471 "-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64"
472 "-v128:128:128-a:0:64-s:64:64-f80:128:128-n8:16:32:64-S128");
473
474 Type* Int64PtrTyAS1 = Type::getInt64PtrTy(C, 1);
475 Type* Int64PtrTyAS2 = Type::getInt64PtrTy(C, 2);
476
477 IntegerType *Int16SizePtr = DL.getIntPtrType(C, 1);
478 IntegerType *Int64SizePtr = DL.getIntPtrType(C, 2);
479
480 // Cannot simplify inttoptr, addrspacecast
481 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
482 CastInst::AddrSpaceCast,
483 Int16Ty, Int64PtrTyAS1, Int64PtrTyAS2,
484 nullptr, Int16SizePtr, Int64SizePtr),
485 0U);
486
487 // Cannot simplify addrspacecast, ptrtoint
488 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::AddrSpaceCast,
489 CastInst::PtrToInt,
490 Int64PtrTyAS1, Int64PtrTyAS2, Int16Ty,
491 Int64SizePtr, Int16SizePtr, nullptr),
492 0U);
493
494 // Pass since the bitcast address spaces are the same
495 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
496 CastInst::BitCast,
497 Int16Ty, Int64PtrTyAS1, Int64PtrTyAS1,
498 nullptr, nullptr, nullptr),
499 CastInst::IntToPtr);
500
501 }
502
TEST(InstructionsTest,CloneCall)503 TEST(InstructionsTest, CloneCall) {
504 LLVMContext C;
505 Type *Int32Ty = Type::getInt32Ty(C);
506 Type *ArgTys[] = {Int32Ty, Int32Ty, Int32Ty};
507 FunctionType *FnTy = FunctionType::get(Int32Ty, ArgTys, /*isVarArg=*/false);
508 Value *Callee = Constant::getNullValue(FnTy->getPointerTo());
509 Value *Args[] = {
510 ConstantInt::get(Int32Ty, 1),
511 ConstantInt::get(Int32Ty, 2),
512 ConstantInt::get(Int32Ty, 3)
513 };
514 std::unique_ptr<CallInst> Call(
515 CallInst::Create(FnTy, Callee, Args, "result"));
516
517 // Test cloning the tail call kind.
518 CallInst::TailCallKind Kinds[] = {CallInst::TCK_None, CallInst::TCK_Tail,
519 CallInst::TCK_MustTail};
520 for (CallInst::TailCallKind TCK : Kinds) {
521 Call->setTailCallKind(TCK);
522 std::unique_ptr<CallInst> Clone(cast<CallInst>(Call->clone()));
523 EXPECT_EQ(Call->getTailCallKind(), Clone->getTailCallKind());
524 }
525 Call->setTailCallKind(CallInst::TCK_None);
526
527 // Test cloning an attribute.
528 {
529 AttrBuilder AB;
530 AB.addAttribute(Attribute::ReadOnly);
531 Call->setAttributes(
532 AttributeList::get(C, AttributeList::FunctionIndex, AB));
533 std::unique_ptr<CallInst> Clone(cast<CallInst>(Call->clone()));
534 EXPECT_TRUE(Clone->onlyReadsMemory());
535 }
536 }
537
TEST(InstructionsTest,AlterCallBundles)538 TEST(InstructionsTest, AlterCallBundles) {
539 LLVMContext C;
540 Type *Int32Ty = Type::getInt32Ty(C);
541 FunctionType *FnTy = FunctionType::get(Int32Ty, Int32Ty, /*isVarArg=*/false);
542 Value *Callee = Constant::getNullValue(FnTy->getPointerTo());
543 Value *Args[] = {ConstantInt::get(Int32Ty, 42)};
544 OperandBundleDef OldBundle("before", UndefValue::get(Int32Ty));
545 std::unique_ptr<CallInst> Call(
546 CallInst::Create(FnTy, Callee, Args, OldBundle, "result"));
547 Call->setTailCallKind(CallInst::TailCallKind::TCK_NoTail);
548 AttrBuilder AB;
549 AB.addAttribute(Attribute::Cold);
550 Call->setAttributes(AttributeList::get(C, AttributeList::FunctionIndex, AB));
551 Call->setDebugLoc(DebugLoc(MDNode::get(C, None)));
552
553 OperandBundleDef NewBundle("after", ConstantInt::get(Int32Ty, 7));
554 std::unique_ptr<CallInst> Clone(CallInst::Create(Call.get(), NewBundle));
555 EXPECT_EQ(Call->getNumArgOperands(), Clone->getNumArgOperands());
556 EXPECT_EQ(Call->getArgOperand(0), Clone->getArgOperand(0));
557 EXPECT_EQ(Call->getCallingConv(), Clone->getCallingConv());
558 EXPECT_EQ(Call->getTailCallKind(), Clone->getTailCallKind());
559 EXPECT_TRUE(Clone->hasFnAttr(Attribute::AttrKind::Cold));
560 EXPECT_EQ(Call->getDebugLoc(), Clone->getDebugLoc());
561 EXPECT_EQ(Clone->getNumOperandBundles(), 1U);
562 EXPECT_TRUE(Clone->getOperandBundle("after").hasValue());
563 }
564
TEST(InstructionsTest,AlterInvokeBundles)565 TEST(InstructionsTest, AlterInvokeBundles) {
566 LLVMContext C;
567 Type *Int32Ty = Type::getInt32Ty(C);
568 FunctionType *FnTy = FunctionType::get(Int32Ty, Int32Ty, /*isVarArg=*/false);
569 Value *Callee = Constant::getNullValue(FnTy->getPointerTo());
570 Value *Args[] = {ConstantInt::get(Int32Ty, 42)};
571 std::unique_ptr<BasicBlock> NormalDest(BasicBlock::Create(C));
572 std::unique_ptr<BasicBlock> UnwindDest(BasicBlock::Create(C));
573 OperandBundleDef OldBundle("before", UndefValue::get(Int32Ty));
574 std::unique_ptr<InvokeInst> Invoke(
575 InvokeInst::Create(FnTy, Callee, NormalDest.get(), UnwindDest.get(), Args,
576 OldBundle, "result"));
577 AttrBuilder AB;
578 AB.addAttribute(Attribute::Cold);
579 Invoke->setAttributes(
580 AttributeList::get(C, AttributeList::FunctionIndex, AB));
581 Invoke->setDebugLoc(DebugLoc(MDNode::get(C, None)));
582
583 OperandBundleDef NewBundle("after", ConstantInt::get(Int32Ty, 7));
584 std::unique_ptr<InvokeInst> Clone(
585 InvokeInst::Create(Invoke.get(), NewBundle));
586 EXPECT_EQ(Invoke->getNormalDest(), Clone->getNormalDest());
587 EXPECT_EQ(Invoke->getUnwindDest(), Clone->getUnwindDest());
588 EXPECT_EQ(Invoke->getNumArgOperands(), Clone->getNumArgOperands());
589 EXPECT_EQ(Invoke->getArgOperand(0), Clone->getArgOperand(0));
590 EXPECT_EQ(Invoke->getCallingConv(), Clone->getCallingConv());
591 EXPECT_TRUE(Clone->hasFnAttr(Attribute::AttrKind::Cold));
592 EXPECT_EQ(Invoke->getDebugLoc(), Clone->getDebugLoc());
593 EXPECT_EQ(Clone->getNumOperandBundles(), 1U);
594 EXPECT_TRUE(Clone->getOperandBundle("after").hasValue());
595 }
596
TEST_F(ModuleWithFunctionTest,DropPoisonGeneratingFlags)597 TEST_F(ModuleWithFunctionTest, DropPoisonGeneratingFlags) {
598 auto *OnlyBB = BasicBlock::Create(Ctx, "bb", F);
599 auto *Arg0 = &*F->arg_begin();
600
601 IRBuilder<NoFolder> B(Ctx);
602 B.SetInsertPoint(OnlyBB);
603
604 {
605 auto *UI =
606 cast<Instruction>(B.CreateUDiv(Arg0, Arg0, "", /*isExact*/ true));
607 ASSERT_TRUE(UI->isExact());
608 UI->dropPoisonGeneratingFlags();
609 ASSERT_FALSE(UI->isExact());
610 }
611
612 {
613 auto *ShrI =
614 cast<Instruction>(B.CreateLShr(Arg0, Arg0, "", /*isExact*/ true));
615 ASSERT_TRUE(ShrI->isExact());
616 ShrI->dropPoisonGeneratingFlags();
617 ASSERT_FALSE(ShrI->isExact());
618 }
619
620 {
621 auto *AI = cast<Instruction>(
622 B.CreateAdd(Arg0, Arg0, "", /*HasNUW*/ true, /*HasNSW*/ false));
623 ASSERT_TRUE(AI->hasNoUnsignedWrap());
624 AI->dropPoisonGeneratingFlags();
625 ASSERT_FALSE(AI->hasNoUnsignedWrap());
626 ASSERT_FALSE(AI->hasNoSignedWrap());
627 }
628
629 {
630 auto *SI = cast<Instruction>(
631 B.CreateAdd(Arg0, Arg0, "", /*HasNUW*/ false, /*HasNSW*/ true));
632 ASSERT_TRUE(SI->hasNoSignedWrap());
633 SI->dropPoisonGeneratingFlags();
634 ASSERT_FALSE(SI->hasNoUnsignedWrap());
635 ASSERT_FALSE(SI->hasNoSignedWrap());
636 }
637
638 {
639 auto *ShlI = cast<Instruction>(
640 B.CreateShl(Arg0, Arg0, "", /*HasNUW*/ true, /*HasNSW*/ true));
641 ASSERT_TRUE(ShlI->hasNoSignedWrap());
642 ASSERT_TRUE(ShlI->hasNoUnsignedWrap());
643 ShlI->dropPoisonGeneratingFlags();
644 ASSERT_FALSE(ShlI->hasNoUnsignedWrap());
645 ASSERT_FALSE(ShlI->hasNoSignedWrap());
646 }
647
648 {
649 Value *GEPBase = Constant::getNullValue(B.getInt8PtrTy());
650 auto *GI = cast<GetElementPtrInst>(
651 B.CreateInBoundsGEP(B.getInt8Ty(), GEPBase, Arg0));
652 ASSERT_TRUE(GI->isInBounds());
653 GI->dropPoisonGeneratingFlags();
654 ASSERT_FALSE(GI->isInBounds());
655 }
656 }
657
TEST(InstructionsTest,GEPIndices)658 TEST(InstructionsTest, GEPIndices) {
659 LLVMContext Context;
660 IRBuilder<NoFolder> Builder(Context);
661 Type *ElementTy = Builder.getInt8Ty();
662 Type *ArrTy = ArrayType::get(ArrayType::get(ElementTy, 64), 64);
663 Value *Indices[] = {
664 Builder.getInt32(0),
665 Builder.getInt32(13),
666 Builder.getInt32(42) };
667
668 Value *V = Builder.CreateGEP(ArrTy, UndefValue::get(PointerType::getUnqual(ArrTy)),
669 Indices);
670 ASSERT_TRUE(isa<GetElementPtrInst>(V));
671
672 auto *GEPI = cast<GetElementPtrInst>(V);
673 ASSERT_NE(GEPI->idx_begin(), GEPI->idx_end());
674 ASSERT_EQ(GEPI->idx_end(), std::next(GEPI->idx_begin(), 3));
675 EXPECT_EQ(Indices[0], GEPI->idx_begin()[0]);
676 EXPECT_EQ(Indices[1], GEPI->idx_begin()[1]);
677 EXPECT_EQ(Indices[2], GEPI->idx_begin()[2]);
678 EXPECT_EQ(GEPI->idx_begin(), GEPI->indices().begin());
679 EXPECT_EQ(GEPI->idx_end(), GEPI->indices().end());
680
681 const auto *CGEPI = GEPI;
682 ASSERT_NE(CGEPI->idx_begin(), CGEPI->idx_end());
683 ASSERT_EQ(CGEPI->idx_end(), std::next(CGEPI->idx_begin(), 3));
684 EXPECT_EQ(Indices[0], CGEPI->idx_begin()[0]);
685 EXPECT_EQ(Indices[1], CGEPI->idx_begin()[1]);
686 EXPECT_EQ(Indices[2], CGEPI->idx_begin()[2]);
687 EXPECT_EQ(CGEPI->idx_begin(), CGEPI->indices().begin());
688 EXPECT_EQ(CGEPI->idx_end(), CGEPI->indices().end());
689
690 delete GEPI;
691 }
692
TEST(InstructionsTest,SwitchInst)693 TEST(InstructionsTest, SwitchInst) {
694 LLVMContext C;
695
696 std::unique_ptr<BasicBlock> BB1, BB2, BB3;
697 BB1.reset(BasicBlock::Create(C));
698 BB2.reset(BasicBlock::Create(C));
699 BB3.reset(BasicBlock::Create(C));
700
701 // We create block 0 after the others so that it gets destroyed first and
702 // clears the uses of the other basic blocks.
703 std::unique_ptr<BasicBlock> BB0(BasicBlock::Create(C));
704
705 auto *Int32Ty = Type::getInt32Ty(C);
706
707 SwitchInst *SI =
708 SwitchInst::Create(UndefValue::get(Int32Ty), BB0.get(), 3, BB0.get());
709 SI->addCase(ConstantInt::get(Int32Ty, 1), BB1.get());
710 SI->addCase(ConstantInt::get(Int32Ty, 2), BB2.get());
711 SI->addCase(ConstantInt::get(Int32Ty, 3), BB3.get());
712
713 auto CI = SI->case_begin();
714 ASSERT_NE(CI, SI->case_end());
715 EXPECT_EQ(1, CI->getCaseValue()->getSExtValue());
716 EXPECT_EQ(BB1.get(), CI->getCaseSuccessor());
717 EXPECT_EQ(2, (CI + 1)->getCaseValue()->getSExtValue());
718 EXPECT_EQ(BB2.get(), (CI + 1)->getCaseSuccessor());
719 EXPECT_EQ(3, (CI + 2)->getCaseValue()->getSExtValue());
720 EXPECT_EQ(BB3.get(), (CI + 2)->getCaseSuccessor());
721 EXPECT_EQ(CI + 1, std::next(CI));
722 EXPECT_EQ(CI + 2, std::next(CI, 2));
723 EXPECT_EQ(CI + 3, std::next(CI, 3));
724 EXPECT_EQ(SI->case_end(), CI + 3);
725 EXPECT_EQ(0, CI - CI);
726 EXPECT_EQ(1, (CI + 1) - CI);
727 EXPECT_EQ(2, (CI + 2) - CI);
728 EXPECT_EQ(3, SI->case_end() - CI);
729 EXPECT_EQ(3, std::distance(CI, SI->case_end()));
730
731 auto CCI = const_cast<const SwitchInst *>(SI)->case_begin();
732 SwitchInst::ConstCaseIt CCE = SI->case_end();
733 ASSERT_NE(CCI, SI->case_end());
734 EXPECT_EQ(1, CCI->getCaseValue()->getSExtValue());
735 EXPECT_EQ(BB1.get(), CCI->getCaseSuccessor());
736 EXPECT_EQ(2, (CCI + 1)->getCaseValue()->getSExtValue());
737 EXPECT_EQ(BB2.get(), (CCI + 1)->getCaseSuccessor());
738 EXPECT_EQ(3, (CCI + 2)->getCaseValue()->getSExtValue());
739 EXPECT_EQ(BB3.get(), (CCI + 2)->getCaseSuccessor());
740 EXPECT_EQ(CCI + 1, std::next(CCI));
741 EXPECT_EQ(CCI + 2, std::next(CCI, 2));
742 EXPECT_EQ(CCI + 3, std::next(CCI, 3));
743 EXPECT_EQ(CCE, CCI + 3);
744 EXPECT_EQ(0, CCI - CCI);
745 EXPECT_EQ(1, (CCI + 1) - CCI);
746 EXPECT_EQ(2, (CCI + 2) - CCI);
747 EXPECT_EQ(3, CCE - CCI);
748 EXPECT_EQ(3, std::distance(CCI, CCE));
749
750 // Make sure that the const iterator is compatible with a const auto ref.
751 const auto &Handle = *CCI;
752 EXPECT_EQ(1, Handle.getCaseValue()->getSExtValue());
753 EXPECT_EQ(BB1.get(), Handle.getCaseSuccessor());
754 }
755
TEST(InstructionsTest,SwitchInstProfUpdateWrapper)756 TEST(InstructionsTest, SwitchInstProfUpdateWrapper) {
757 LLVMContext C;
758
759 std::unique_ptr<BasicBlock> BB1, BB2, BB3;
760 BB1.reset(BasicBlock::Create(C));
761 BB2.reset(BasicBlock::Create(C));
762 BB3.reset(BasicBlock::Create(C));
763
764 // We create block 0 after the others so that it gets destroyed first and
765 // clears the uses of the other basic blocks.
766 std::unique_ptr<BasicBlock> BB0(BasicBlock::Create(C));
767
768 auto *Int32Ty = Type::getInt32Ty(C);
769
770 SwitchInst *SI =
771 SwitchInst::Create(UndefValue::get(Int32Ty), BB0.get(), 4, BB0.get());
772 SI->addCase(ConstantInt::get(Int32Ty, 1), BB1.get());
773 SI->addCase(ConstantInt::get(Int32Ty, 2), BB2.get());
774 SI->setMetadata(LLVMContext::MD_prof,
775 MDBuilder(C).createBranchWeights({ 9, 1, 22 }));
776
777 {
778 SwitchInstProfUpdateWrapper SIW(*SI);
779 EXPECT_EQ(*SIW.getSuccessorWeight(0), 9u);
780 EXPECT_EQ(*SIW.getSuccessorWeight(1), 1u);
781 EXPECT_EQ(*SIW.getSuccessorWeight(2), 22u);
782 SIW.setSuccessorWeight(0, 99u);
783 SIW.setSuccessorWeight(1, 11u);
784 EXPECT_EQ(*SIW.getSuccessorWeight(0), 99u);
785 EXPECT_EQ(*SIW.getSuccessorWeight(1), 11u);
786 EXPECT_EQ(*SIW.getSuccessorWeight(2), 22u);
787 }
788
789 { // Create another wrapper and check that the data persist.
790 SwitchInstProfUpdateWrapper SIW(*SI);
791 EXPECT_EQ(*SIW.getSuccessorWeight(0), 99u);
792 EXPECT_EQ(*SIW.getSuccessorWeight(1), 11u);
793 EXPECT_EQ(*SIW.getSuccessorWeight(2), 22u);
794 }
795 }
796
TEST(InstructionsTest,CommuteShuffleMask)797 TEST(InstructionsTest, CommuteShuffleMask) {
798 SmallVector<int, 16> Indices({-1, 0, 7});
799 ShuffleVectorInst::commuteShuffleMask(Indices, 4);
800 EXPECT_THAT(Indices, testing::ContainerEq(ArrayRef<int>({-1, 4, 3})));
801 }
802
TEST(InstructionsTest,ShuffleMaskQueries)803 TEST(InstructionsTest, ShuffleMaskQueries) {
804 // Create the elements for various constant vectors.
805 LLVMContext Ctx;
806 Type *Int32Ty = Type::getInt32Ty(Ctx);
807 Constant *CU = UndefValue::get(Int32Ty);
808 Constant *C0 = ConstantInt::get(Int32Ty, 0);
809 Constant *C1 = ConstantInt::get(Int32Ty, 1);
810 Constant *C2 = ConstantInt::get(Int32Ty, 2);
811 Constant *C3 = ConstantInt::get(Int32Ty, 3);
812 Constant *C4 = ConstantInt::get(Int32Ty, 4);
813 Constant *C5 = ConstantInt::get(Int32Ty, 5);
814 Constant *C6 = ConstantInt::get(Int32Ty, 6);
815 Constant *C7 = ConstantInt::get(Int32Ty, 7);
816
817 Constant *Identity = ConstantVector::get({C0, CU, C2, C3, C4});
818 EXPECT_TRUE(ShuffleVectorInst::isIdentityMask(Identity));
819 EXPECT_FALSE(ShuffleVectorInst::isSelectMask(Identity)); // identity is distinguished from select
820 EXPECT_FALSE(ShuffleVectorInst::isReverseMask(Identity));
821 EXPECT_TRUE(ShuffleVectorInst::isSingleSourceMask(Identity)); // identity is always single source
822 EXPECT_FALSE(ShuffleVectorInst::isZeroEltSplatMask(Identity));
823 EXPECT_FALSE(ShuffleVectorInst::isTransposeMask(Identity));
824
825 Constant *Select = ConstantVector::get({CU, C1, C5});
826 EXPECT_FALSE(ShuffleVectorInst::isIdentityMask(Select));
827 EXPECT_TRUE(ShuffleVectorInst::isSelectMask(Select));
828 EXPECT_FALSE(ShuffleVectorInst::isReverseMask(Select));
829 EXPECT_FALSE(ShuffleVectorInst::isSingleSourceMask(Select));
830 EXPECT_FALSE(ShuffleVectorInst::isZeroEltSplatMask(Select));
831 EXPECT_FALSE(ShuffleVectorInst::isTransposeMask(Select));
832
833 Constant *Reverse = ConstantVector::get({C3, C2, C1, CU});
834 EXPECT_FALSE(ShuffleVectorInst::isIdentityMask(Reverse));
835 EXPECT_FALSE(ShuffleVectorInst::isSelectMask(Reverse));
836 EXPECT_TRUE(ShuffleVectorInst::isReverseMask(Reverse));
837 EXPECT_TRUE(ShuffleVectorInst::isSingleSourceMask(Reverse)); // reverse is always single source
838 EXPECT_FALSE(ShuffleVectorInst::isZeroEltSplatMask(Reverse));
839 EXPECT_FALSE(ShuffleVectorInst::isTransposeMask(Reverse));
840
841 Constant *SingleSource = ConstantVector::get({C2, C2, C0, CU});
842 EXPECT_FALSE(ShuffleVectorInst::isIdentityMask(SingleSource));
843 EXPECT_FALSE(ShuffleVectorInst::isSelectMask(SingleSource));
844 EXPECT_FALSE(ShuffleVectorInst::isReverseMask(SingleSource));
845 EXPECT_TRUE(ShuffleVectorInst::isSingleSourceMask(SingleSource));
846 EXPECT_FALSE(ShuffleVectorInst::isZeroEltSplatMask(SingleSource));
847 EXPECT_FALSE(ShuffleVectorInst::isTransposeMask(SingleSource));
848
849 Constant *ZeroEltSplat = ConstantVector::get({C0, C0, CU, C0});
850 EXPECT_FALSE(ShuffleVectorInst::isIdentityMask(ZeroEltSplat));
851 EXPECT_FALSE(ShuffleVectorInst::isSelectMask(ZeroEltSplat));
852 EXPECT_FALSE(ShuffleVectorInst::isReverseMask(ZeroEltSplat));
853 EXPECT_TRUE(ShuffleVectorInst::isSingleSourceMask(ZeroEltSplat)); // 0-splat is always single source
854 EXPECT_TRUE(ShuffleVectorInst::isZeroEltSplatMask(ZeroEltSplat));
855 EXPECT_FALSE(ShuffleVectorInst::isTransposeMask(ZeroEltSplat));
856
857 Constant *Transpose = ConstantVector::get({C0, C4, C2, C6});
858 EXPECT_FALSE(ShuffleVectorInst::isIdentityMask(Transpose));
859 EXPECT_FALSE(ShuffleVectorInst::isSelectMask(Transpose));
860 EXPECT_FALSE(ShuffleVectorInst::isReverseMask(Transpose));
861 EXPECT_FALSE(ShuffleVectorInst::isSingleSourceMask(Transpose));
862 EXPECT_FALSE(ShuffleVectorInst::isZeroEltSplatMask(Transpose));
863 EXPECT_TRUE(ShuffleVectorInst::isTransposeMask(Transpose));
864
865 // More tests to make sure the logic is/stays correct...
866 EXPECT_TRUE(ShuffleVectorInst::isIdentityMask(ConstantVector::get({CU, C1, CU, C3})));
867 EXPECT_TRUE(ShuffleVectorInst::isIdentityMask(ConstantVector::get({C4, CU, C6, CU})));
868
869 EXPECT_TRUE(ShuffleVectorInst::isSelectMask(ConstantVector::get({C4, C1, C6, CU})));
870 EXPECT_TRUE(ShuffleVectorInst::isSelectMask(ConstantVector::get({CU, C1, C6, C3})));
871
872 EXPECT_TRUE(ShuffleVectorInst::isReverseMask(ConstantVector::get({C7, C6, CU, C4})));
873 EXPECT_TRUE(ShuffleVectorInst::isReverseMask(ConstantVector::get({C3, CU, C1, CU})));
874
875 EXPECT_TRUE(ShuffleVectorInst::isSingleSourceMask(ConstantVector::get({C7, C5, CU, C7})));
876 EXPECT_TRUE(ShuffleVectorInst::isSingleSourceMask(ConstantVector::get({C3, C0, CU, C3})));
877
878 EXPECT_TRUE(ShuffleVectorInst::isZeroEltSplatMask(ConstantVector::get({C4, CU, CU, C4})));
879 EXPECT_TRUE(ShuffleVectorInst::isZeroEltSplatMask(ConstantVector::get({CU, C0, CU, C0})));
880
881 EXPECT_TRUE(ShuffleVectorInst::isTransposeMask(ConstantVector::get({C1, C5, C3, C7})));
882 EXPECT_TRUE(ShuffleVectorInst::isTransposeMask(ConstantVector::get({C1, C3})));
883
884 // Nothing special about the values here - just re-using inputs to reduce code.
885 Constant *V0 = ConstantVector::get({C0, C1, C2, C3});
886 Constant *V1 = ConstantVector::get({C3, C2, C1, C0});
887
888 // Identity with undef elts.
889 ShuffleVectorInst *Id1 = new ShuffleVectorInst(V0, V1,
890 ConstantVector::get({C0, C1, CU, CU}));
891 EXPECT_TRUE(Id1->isIdentity());
892 EXPECT_FALSE(Id1->isIdentityWithPadding());
893 EXPECT_FALSE(Id1->isIdentityWithExtract());
894 EXPECT_FALSE(Id1->isConcat());
895 delete Id1;
896
897 // Result has less elements than operands.
898 ShuffleVectorInst *Id2 = new ShuffleVectorInst(V0, V1,
899 ConstantVector::get({C0, C1, C2}));
900 EXPECT_FALSE(Id2->isIdentity());
901 EXPECT_FALSE(Id2->isIdentityWithPadding());
902 EXPECT_TRUE(Id2->isIdentityWithExtract());
903 EXPECT_FALSE(Id2->isConcat());
904 delete Id2;
905
906 // Result has less elements than operands; choose from Op1.
907 ShuffleVectorInst *Id3 = new ShuffleVectorInst(V0, V1,
908 ConstantVector::get({C4, CU, C6}));
909 EXPECT_FALSE(Id3->isIdentity());
910 EXPECT_FALSE(Id3->isIdentityWithPadding());
911 EXPECT_TRUE(Id3->isIdentityWithExtract());
912 EXPECT_FALSE(Id3->isConcat());
913 delete Id3;
914
915 // Result has less elements than operands; choose from Op0 and Op1 is not identity.
916 ShuffleVectorInst *Id4 = new ShuffleVectorInst(V0, V1,
917 ConstantVector::get({C4, C1, C6}));
918 EXPECT_FALSE(Id4->isIdentity());
919 EXPECT_FALSE(Id4->isIdentityWithPadding());
920 EXPECT_FALSE(Id4->isIdentityWithExtract());
921 EXPECT_FALSE(Id4->isConcat());
922 delete Id4;
923
924 // Result has more elements than operands, and extra elements are undef.
925 ShuffleVectorInst *Id5 = new ShuffleVectorInst(V0, V1,
926 ConstantVector::get({CU, C1, C2, C3, CU, CU}));
927 EXPECT_FALSE(Id5->isIdentity());
928 EXPECT_TRUE(Id5->isIdentityWithPadding());
929 EXPECT_FALSE(Id5->isIdentityWithExtract());
930 EXPECT_FALSE(Id5->isConcat());
931 delete Id5;
932
933 // Result has more elements than operands, and extra elements are undef; choose from Op1.
934 ShuffleVectorInst *Id6 = new ShuffleVectorInst(V0, V1,
935 ConstantVector::get({C4, C5, C6, CU, CU, CU}));
936 EXPECT_FALSE(Id6->isIdentity());
937 EXPECT_TRUE(Id6->isIdentityWithPadding());
938 EXPECT_FALSE(Id6->isIdentityWithExtract());
939 EXPECT_FALSE(Id6->isConcat());
940 delete Id6;
941
942 // Result has more elements than operands, but extra elements are not undef.
943 ShuffleVectorInst *Id7 = new ShuffleVectorInst(V0, V1,
944 ConstantVector::get({C0, C1, C2, C3, CU, C1}));
945 EXPECT_FALSE(Id7->isIdentity());
946 EXPECT_FALSE(Id7->isIdentityWithPadding());
947 EXPECT_FALSE(Id7->isIdentityWithExtract());
948 EXPECT_FALSE(Id7->isConcat());
949 delete Id7;
950
951 // Result has more elements than operands; choose from Op0 and Op1 is not identity.
952 ShuffleVectorInst *Id8 = new ShuffleVectorInst(V0, V1,
953 ConstantVector::get({C4, CU, C2, C3, CU, CU}));
954 EXPECT_FALSE(Id8->isIdentity());
955 EXPECT_FALSE(Id8->isIdentityWithPadding());
956 EXPECT_FALSE(Id8->isIdentityWithExtract());
957 EXPECT_FALSE(Id8->isConcat());
958 delete Id8;
959
960 // Result has twice as many elements as operands; choose consecutively from Op0 and Op1 is concat.
961 ShuffleVectorInst *Id9 = new ShuffleVectorInst(V0, V1,
962 ConstantVector::get({C0, CU, C2, C3, CU, CU, C6, C7}));
963 EXPECT_FALSE(Id9->isIdentity());
964 EXPECT_FALSE(Id9->isIdentityWithPadding());
965 EXPECT_FALSE(Id9->isIdentityWithExtract());
966 EXPECT_TRUE(Id9->isConcat());
967 delete Id9;
968
969 // Result has less than twice as many elements as operands, so not a concat.
970 ShuffleVectorInst *Id10 = new ShuffleVectorInst(V0, V1,
971 ConstantVector::get({C0, CU, C2, C3, CU, CU, C6}));
972 EXPECT_FALSE(Id10->isIdentity());
973 EXPECT_FALSE(Id10->isIdentityWithPadding());
974 EXPECT_FALSE(Id10->isIdentityWithExtract());
975 EXPECT_FALSE(Id10->isConcat());
976 delete Id10;
977
978 // Result has more than twice as many elements as operands, so not a concat.
979 ShuffleVectorInst *Id11 = new ShuffleVectorInst(V0, V1,
980 ConstantVector::get({C0, CU, C2, C3, CU, CU, C6, C7, CU}));
981 EXPECT_FALSE(Id11->isIdentity());
982 EXPECT_FALSE(Id11->isIdentityWithPadding());
983 EXPECT_FALSE(Id11->isIdentityWithExtract());
984 EXPECT_FALSE(Id11->isConcat());
985 delete Id11;
986
987 // If an input is undef, it's not a concat.
988 // TODO: IdentityWithPadding should be true here even though the high mask values are not undef.
989 ShuffleVectorInst *Id12 = new ShuffleVectorInst(V0, ConstantVector::get({CU, CU, CU, CU}),
990 ConstantVector::get({C0, CU, C2, C3, CU, CU, C6, C7}));
991 EXPECT_FALSE(Id12->isIdentity());
992 EXPECT_FALSE(Id12->isIdentityWithPadding());
993 EXPECT_FALSE(Id12->isIdentityWithExtract());
994 EXPECT_FALSE(Id12->isConcat());
995 delete Id12;
996 }
997
TEST(InstructionsTest,GetSplat)998 TEST(InstructionsTest, GetSplat) {
999 // Create the elements for various constant vectors.
1000 LLVMContext Ctx;
1001 Type *Int32Ty = Type::getInt32Ty(Ctx);
1002 Constant *CU = UndefValue::get(Int32Ty);
1003 Constant *C0 = ConstantInt::get(Int32Ty, 0);
1004 Constant *C1 = ConstantInt::get(Int32Ty, 1);
1005
1006 Constant *Splat0 = ConstantVector::get({C0, C0, C0, C0});
1007 Constant *Splat1 = ConstantVector::get({C1, C1, C1, C1 ,C1});
1008 Constant *Splat0Undef = ConstantVector::get({C0, CU, C0, CU});
1009 Constant *Splat1Undef = ConstantVector::get({CU, CU, C1, CU});
1010 Constant *NotSplat = ConstantVector::get({C1, C1, C0, C1 ,C1});
1011 Constant *NotSplatUndef = ConstantVector::get({CU, C1, CU, CU ,C0});
1012
1013 // Default - undefs are not allowed.
1014 EXPECT_EQ(Splat0->getSplatValue(), C0);
1015 EXPECT_EQ(Splat1->getSplatValue(), C1);
1016 EXPECT_EQ(Splat0Undef->getSplatValue(), nullptr);
1017 EXPECT_EQ(Splat1Undef->getSplatValue(), nullptr);
1018 EXPECT_EQ(NotSplat->getSplatValue(), nullptr);
1019 EXPECT_EQ(NotSplatUndef->getSplatValue(), nullptr);
1020
1021 // Disallow undefs explicitly.
1022 EXPECT_EQ(Splat0->getSplatValue(false), C0);
1023 EXPECT_EQ(Splat1->getSplatValue(false), C1);
1024 EXPECT_EQ(Splat0Undef->getSplatValue(false), nullptr);
1025 EXPECT_EQ(Splat1Undef->getSplatValue(false), nullptr);
1026 EXPECT_EQ(NotSplat->getSplatValue(false), nullptr);
1027 EXPECT_EQ(NotSplatUndef->getSplatValue(false), nullptr);
1028
1029 // Allow undefs.
1030 EXPECT_EQ(Splat0->getSplatValue(true), C0);
1031 EXPECT_EQ(Splat1->getSplatValue(true), C1);
1032 EXPECT_EQ(Splat0Undef->getSplatValue(true), C0);
1033 EXPECT_EQ(Splat1Undef->getSplatValue(true), C1);
1034 EXPECT_EQ(NotSplat->getSplatValue(true), nullptr);
1035 EXPECT_EQ(NotSplatUndef->getSplatValue(true), nullptr);
1036 }
1037
TEST(InstructionsTest,SkipDebug)1038 TEST(InstructionsTest, SkipDebug) {
1039 LLVMContext C;
1040 std::unique_ptr<Module> M = parseIR(C,
1041 R"(
1042 declare void @llvm.dbg.value(metadata, metadata, metadata)
1043
1044 define void @f() {
1045 entry:
1046 call void @llvm.dbg.value(metadata i32 0, metadata !11, metadata !DIExpression()), !dbg !13
1047 ret void
1048 }
1049
1050 !llvm.dbg.cu = !{!0}
1051 !llvm.module.flags = !{!3, !4}
1052 !0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 6.0.0", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
1053 !1 = !DIFile(filename: "t2.c", directory: "foo")
1054 !2 = !{}
1055 !3 = !{i32 2, !"Dwarf Version", i32 4}
1056 !4 = !{i32 2, !"Debug Info Version", i32 3}
1057 !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)
1058 !9 = !DISubroutineType(types: !10)
1059 !10 = !{null}
1060 !11 = !DILocalVariable(name: "x", scope: !8, file: !1, line: 2, type: !12)
1061 !12 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
1062 !13 = !DILocation(line: 2, column: 7, scope: !8)
1063 )");
1064 ASSERT_TRUE(M);
1065 Function *F = cast<Function>(M->getNamedValue("f"));
1066 BasicBlock &BB = F->front();
1067
1068 // The first non-debug instruction is the terminator.
1069 auto *Term = BB.getTerminator();
1070 EXPECT_EQ(Term, BB.begin()->getNextNonDebugInstruction());
1071 EXPECT_EQ(Term->getIterator(), skipDebugIntrinsics(BB.begin()));
1072
1073 // After the terminator, there are no non-debug instructions.
1074 EXPECT_EQ(nullptr, Term->getNextNonDebugInstruction());
1075 }
1076
TEST(InstructionsTest,PhiMightNotBeFPMathOperator)1077 TEST(InstructionsTest, PhiMightNotBeFPMathOperator) {
1078 LLVMContext Context;
1079 IRBuilder<> Builder(Context);
1080 MDBuilder MDHelper(Context);
1081 Instruction *I = Builder.CreatePHI(Builder.getInt32Ty(), 0);
1082 EXPECT_FALSE(isa<FPMathOperator>(I));
1083 I->deleteValue();
1084 Instruction *FP = Builder.CreatePHI(Builder.getDoubleTy(), 0);
1085 EXPECT_TRUE(isa<FPMathOperator>(FP));
1086 FP->deleteValue();
1087 }
1088
TEST(InstructionsTest,FPCallIsFPMathOperator)1089 TEST(InstructionsTest, FPCallIsFPMathOperator) {
1090 LLVMContext C;
1091
1092 Type *ITy = Type::getInt32Ty(C);
1093 FunctionType *IFnTy = FunctionType::get(ITy, {});
1094 Value *ICallee = Constant::getNullValue(IFnTy->getPointerTo());
1095 std::unique_ptr<CallInst> ICall(CallInst::Create(IFnTy, ICallee, {}, ""));
1096 EXPECT_FALSE(isa<FPMathOperator>(ICall));
1097
1098 Type *VITy = VectorType::get(ITy, 2);
1099 FunctionType *VIFnTy = FunctionType::get(VITy, {});
1100 Value *VICallee = Constant::getNullValue(VIFnTy->getPointerTo());
1101 std::unique_ptr<CallInst> VICall(CallInst::Create(VIFnTy, VICallee, {}, ""));
1102 EXPECT_FALSE(isa<FPMathOperator>(VICall));
1103
1104 Type *AITy = ArrayType::get(ITy, 2);
1105 FunctionType *AIFnTy = FunctionType::get(AITy, {});
1106 Value *AICallee = Constant::getNullValue(AIFnTy->getPointerTo());
1107 std::unique_ptr<CallInst> AICall(CallInst::Create(AIFnTy, AICallee, {}, ""));
1108 EXPECT_FALSE(isa<FPMathOperator>(AICall));
1109
1110 Type *FTy = Type::getFloatTy(C);
1111 FunctionType *FFnTy = FunctionType::get(FTy, {});
1112 Value *FCallee = Constant::getNullValue(FFnTy->getPointerTo());
1113 std::unique_ptr<CallInst> FCall(CallInst::Create(FFnTy, FCallee, {}, ""));
1114 EXPECT_TRUE(isa<FPMathOperator>(FCall));
1115
1116 Type *VFTy = VectorType::get(FTy, 2);
1117 FunctionType *VFFnTy = FunctionType::get(VFTy, {});
1118 Value *VFCallee = Constant::getNullValue(VFFnTy->getPointerTo());
1119 std::unique_ptr<CallInst> VFCall(CallInst::Create(VFFnTy, VFCallee, {}, ""));
1120 EXPECT_TRUE(isa<FPMathOperator>(VFCall));
1121
1122 Type *AFTy = ArrayType::get(FTy, 2);
1123 FunctionType *AFFnTy = FunctionType::get(AFTy, {});
1124 Value *AFCallee = Constant::getNullValue(AFFnTy->getPointerTo());
1125 std::unique_ptr<CallInst> AFCall(CallInst::Create(AFFnTy, AFCallee, {}, ""));
1126 EXPECT_TRUE(isa<FPMathOperator>(AFCall));
1127
1128 Type *AVFTy = ArrayType::get(VFTy, 2);
1129 FunctionType *AVFFnTy = FunctionType::get(AVFTy, {});
1130 Value *AVFCallee = Constant::getNullValue(AVFFnTy->getPointerTo());
1131 std::unique_ptr<CallInst> AVFCall(
1132 CallInst::Create(AVFFnTy, AVFCallee, {}, ""));
1133 EXPECT_TRUE(isa<FPMathOperator>(AVFCall));
1134
1135 Type *AAVFTy = ArrayType::get(AVFTy, 2);
1136 FunctionType *AAVFFnTy = FunctionType::get(AAVFTy, {});
1137 Value *AAVFCallee = Constant::getNullValue(AAVFFnTy->getPointerTo());
1138 std::unique_ptr<CallInst> AAVFCall(
1139 CallInst::Create(AAVFFnTy, AAVFCallee, {}, ""));
1140 EXPECT_TRUE(isa<FPMathOperator>(AAVFCall));
1141 }
1142
TEST(InstructionsTest,FNegInstruction)1143 TEST(InstructionsTest, FNegInstruction) {
1144 LLVMContext Context;
1145 Type *FltTy = Type::getFloatTy(Context);
1146 Constant *One = ConstantFP::get(FltTy, 1.0);
1147 BinaryOperator *FAdd = BinaryOperator::CreateFAdd(One, One);
1148 FAdd->setHasNoNaNs(true);
1149 UnaryOperator *FNeg = UnaryOperator::CreateFNegFMF(One, FAdd);
1150 EXPECT_TRUE(FNeg->hasNoNaNs());
1151 EXPECT_FALSE(FNeg->hasNoInfs());
1152 EXPECT_FALSE(FNeg->hasNoSignedZeros());
1153 EXPECT_FALSE(FNeg->hasAllowReciprocal());
1154 EXPECT_FALSE(FNeg->hasAllowContract());
1155 EXPECT_FALSE(FNeg->hasAllowReassoc());
1156 EXPECT_FALSE(FNeg->hasApproxFunc());
1157 FAdd->deleteValue();
1158 FNeg->deleteValue();
1159 }
1160
TEST(InstructionsTest,CallBrInstruction)1161 TEST(InstructionsTest, CallBrInstruction) {
1162 LLVMContext Context;
1163 std::unique_ptr<Module> M = parseIR(Context, R"(
1164 define void @foo() {
1165 entry:
1166 callbr void asm sideeffect "// XXX: ${0:l}", "X"(i8* blockaddress(@foo, %branch_test.exit))
1167 to label %land.rhs.i [label %branch_test.exit]
1168
1169 land.rhs.i:
1170 br label %branch_test.exit
1171
1172 branch_test.exit:
1173 %0 = phi i1 [ true, %entry ], [ false, %land.rhs.i ]
1174 br i1 %0, label %if.end, label %if.then
1175
1176 if.then:
1177 ret void
1178
1179 if.end:
1180 ret void
1181 }
1182 )");
1183 Function *Foo = M->getFunction("foo");
1184 auto BBs = Foo->getBasicBlockList().begin();
1185 CallBrInst &CBI = cast<CallBrInst>(BBs->front());
1186 ++BBs;
1187 ++BBs;
1188 BasicBlock &BranchTestExit = *BBs;
1189 ++BBs;
1190 BasicBlock &IfThen = *BBs;
1191
1192 // Test that setting the first indirect destination of callbr updates the dest
1193 EXPECT_EQ(&BranchTestExit, CBI.getIndirectDest(0));
1194 CBI.setIndirectDest(0, &IfThen);
1195 EXPECT_EQ(&IfThen, CBI.getIndirectDest(0));
1196
1197 // Further, test that changing the indirect destination updates the arg
1198 // operand to use the block address of the new indirect destination basic
1199 // block. This is a critical invariant of CallBrInst.
1200 BlockAddress *IndirectBA = BlockAddress::get(CBI.getIndirectDest(0));
1201 BlockAddress *ArgBA = cast<BlockAddress>(CBI.getArgOperand(0));
1202 EXPECT_EQ(IndirectBA, ArgBA)
1203 << "After setting the indirect destination, callbr had an indirect "
1204 "destination of '"
1205 << CBI.getIndirectDest(0)->getName() << "', but a argument of '"
1206 << ArgBA->getBasicBlock()->getName() << "'. These should always match:\n"
1207 << CBI;
1208 EXPECT_EQ(IndirectBA->getBasicBlock(), &IfThen);
1209 EXPECT_EQ(ArgBA->getBasicBlock(), &IfThen);
1210 }
1211
TEST(InstructionsTest,UnaryOperator)1212 TEST(InstructionsTest, UnaryOperator) {
1213 LLVMContext Context;
1214 IRBuilder<> Builder(Context);
1215 Instruction *I = Builder.CreatePHI(Builder.getDoubleTy(), 0);
1216 Value *F = Builder.CreateFNeg(I);
1217
1218 EXPECT_TRUE(isa<Value>(F));
1219 EXPECT_TRUE(isa<Instruction>(F));
1220 EXPECT_TRUE(isa<UnaryInstruction>(F));
1221 EXPECT_TRUE(isa<UnaryOperator>(F));
1222 EXPECT_FALSE(isa<BinaryOperator>(F));
1223
1224 F->deleteValue();
1225 I->deleteValue();
1226 }
1227
1228 } // end anonymous namespace
1229 } // end namespace llvm
1230