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