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