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