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