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