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