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