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