1 //===- llvm/unittest/IR/InstructionsTest.cpp - Instructions unit tests ----===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "llvm/IR/Instructions.h" 11 #include "llvm/ADT/STLExtras.h" 12 #include "llvm/Analysis/ValueTracking.h" 13 #include "llvm/IR/BasicBlock.h" 14 #include "llvm/IR/Constants.h" 15 #include "llvm/IR/DataLayout.h" 16 #include "llvm/IR/DerivedTypes.h" 17 #include "llvm/IR/Function.h" 18 #include "llvm/IR/IRBuilder.h" 19 #include "llvm/IR/LLVMContext.h" 20 #include "llvm/IR/MDBuilder.h" 21 #include "llvm/IR/Module.h" 22 #include "llvm/IR/NoFolder.h" 23 #include "llvm/IR/Operator.h" 24 #include "gtest/gtest.h" 25 #include <memory> 26 27 namespace llvm { 28 namespace { 29 30 TEST(InstructionsTest, ReturnInst) { 31 LLVMContext C; 32 33 // test for PR6589 34 const ReturnInst* r0 = ReturnInst::Create(C); 35 EXPECT_EQ(r0->getNumOperands(), 0U); 36 EXPECT_EQ(r0->op_begin(), r0->op_end()); 37 38 IntegerType* Int1 = IntegerType::get(C, 1); 39 Constant* One = ConstantInt::get(Int1, 1, true); 40 const ReturnInst* r1 = ReturnInst::Create(C, One); 41 EXPECT_EQ(1U, r1->getNumOperands()); 42 User::const_op_iterator b(r1->op_begin()); 43 EXPECT_NE(r1->op_end(), b); 44 EXPECT_EQ(One, *b); 45 EXPECT_EQ(One, r1->getOperand(0)); 46 ++b; 47 EXPECT_EQ(r1->op_end(), b); 48 49 // clean up 50 delete r0; 51 delete r1; 52 } 53 54 // Test fixture that provides a module and a single function within it. Useful 55 // for tests that need to refer to the function in some way. 56 class ModuleWithFunctionTest : public testing::Test { 57 protected: 58 ModuleWithFunctionTest() : M(new Module("MyModule", Ctx)) { 59 FArgTypes.push_back(Type::getInt8Ty(Ctx)); 60 FArgTypes.push_back(Type::getInt32Ty(Ctx)); 61 FArgTypes.push_back(Type::getInt64Ty(Ctx)); 62 FunctionType *FTy = 63 FunctionType::get(Type::getVoidTy(Ctx), FArgTypes, false); 64 F = Function::Create(FTy, Function::ExternalLinkage, "", M.get()); 65 } 66 67 LLVMContext Ctx; 68 std::unique_ptr<Module> M; 69 SmallVector<Type *, 3> FArgTypes; 70 Function *F; 71 }; 72 73 TEST_F(ModuleWithFunctionTest, CallInst) { 74 Value *Args[] = {ConstantInt::get(Type::getInt8Ty(Ctx), 20), 75 ConstantInt::get(Type::getInt32Ty(Ctx), 9999), 76 ConstantInt::get(Type::getInt64Ty(Ctx), 42)}; 77 std::unique_ptr<CallInst> Call(CallInst::Create(F, Args)); 78 79 // Make sure iteration over a call's arguments works as expected. 80 unsigned Idx = 0; 81 for (Value *Arg : Call->arg_operands()) { 82 EXPECT_EQ(FArgTypes[Idx], Arg->getType()); 83 EXPECT_EQ(Call->getArgOperand(Idx)->getType(), Arg->getType()); 84 Idx++; 85 } 86 } 87 88 TEST_F(ModuleWithFunctionTest, InvokeInst) { 89 BasicBlock *BB1 = BasicBlock::Create(Ctx, "", F); 90 BasicBlock *BB2 = BasicBlock::Create(Ctx, "", F); 91 92 Value *Args[] = {ConstantInt::get(Type::getInt8Ty(Ctx), 20), 93 ConstantInt::get(Type::getInt32Ty(Ctx), 9999), 94 ConstantInt::get(Type::getInt64Ty(Ctx), 42)}; 95 std::unique_ptr<InvokeInst> Invoke(InvokeInst::Create(F, BB1, BB2, Args)); 96 97 // Make sure iteration over invoke's arguments works as expected. 98 unsigned Idx = 0; 99 for (Value *Arg : Invoke->arg_operands()) { 100 EXPECT_EQ(FArgTypes[Idx], Arg->getType()); 101 EXPECT_EQ(Invoke->getArgOperand(Idx)->getType(), Arg->getType()); 102 Idx++; 103 } 104 } 105 106 TEST(InstructionsTest, BranchInst) { 107 LLVMContext C; 108 109 // Make a BasicBlocks 110 BasicBlock* bb0 = BasicBlock::Create(C); 111 BasicBlock* bb1 = BasicBlock::Create(C); 112 113 // Mandatory BranchInst 114 const BranchInst* b0 = BranchInst::Create(bb0); 115 116 EXPECT_TRUE(b0->isUnconditional()); 117 EXPECT_FALSE(b0->isConditional()); 118 EXPECT_EQ(1U, b0->getNumSuccessors()); 119 120 // check num operands 121 EXPECT_EQ(1U, b0->getNumOperands()); 122 123 EXPECT_NE(b0->op_begin(), b0->op_end()); 124 EXPECT_EQ(b0->op_end(), std::next(b0->op_begin())); 125 126 EXPECT_EQ(b0->op_end(), std::next(b0->op_begin())); 127 128 IntegerType* Int1 = IntegerType::get(C, 1); 129 Constant* One = ConstantInt::get(Int1, 1, true); 130 131 // Conditional BranchInst 132 BranchInst* b1 = BranchInst::Create(bb0, bb1, One); 133 134 EXPECT_FALSE(b1->isUnconditional()); 135 EXPECT_TRUE(b1->isConditional()); 136 EXPECT_EQ(2U, b1->getNumSuccessors()); 137 138 // check num operands 139 EXPECT_EQ(3U, b1->getNumOperands()); 140 141 User::const_op_iterator b(b1->op_begin()); 142 143 // check COND 144 EXPECT_NE(b, b1->op_end()); 145 EXPECT_EQ(One, *b); 146 EXPECT_EQ(One, b1->getOperand(0)); 147 EXPECT_EQ(One, b1->getCondition()); 148 ++b; 149 150 // check ELSE 151 EXPECT_EQ(bb1, *b); 152 EXPECT_EQ(bb1, b1->getOperand(1)); 153 EXPECT_EQ(bb1, b1->getSuccessor(1)); 154 ++b; 155 156 // check THEN 157 EXPECT_EQ(bb0, *b); 158 EXPECT_EQ(bb0, b1->getOperand(2)); 159 EXPECT_EQ(bb0, b1->getSuccessor(0)); 160 ++b; 161 162 EXPECT_EQ(b1->op_end(), b); 163 164 // clean up 165 delete b0; 166 delete b1; 167 168 delete bb0; 169 delete bb1; 170 } 171 172 TEST(InstructionsTest, CastInst) { 173 LLVMContext C; 174 175 Type *Int8Ty = Type::getInt8Ty(C); 176 Type *Int16Ty = Type::getInt16Ty(C); 177 Type *Int32Ty = Type::getInt32Ty(C); 178 Type *Int64Ty = Type::getInt64Ty(C); 179 Type *V8x8Ty = VectorType::get(Int8Ty, 8); 180 Type *V8x64Ty = VectorType::get(Int64Ty, 8); 181 Type *X86MMXTy = Type::getX86_MMXTy(C); 182 183 Type *HalfTy = Type::getHalfTy(C); 184 Type *FloatTy = Type::getFloatTy(C); 185 Type *DoubleTy = Type::getDoubleTy(C); 186 187 Type *V2Int32Ty = VectorType::get(Int32Ty, 2); 188 Type *V2Int64Ty = VectorType::get(Int64Ty, 2); 189 Type *V4Int16Ty = VectorType::get(Int16Ty, 4); 190 191 Type *Int32PtrTy = PointerType::get(Int32Ty, 0); 192 Type *Int64PtrTy = PointerType::get(Int64Ty, 0); 193 194 Type *Int32PtrAS1Ty = PointerType::get(Int32Ty, 1); 195 Type *Int64PtrAS1Ty = PointerType::get(Int64Ty, 1); 196 197 Type *V2Int32PtrAS1Ty = VectorType::get(Int32PtrAS1Ty, 2); 198 Type *V2Int64PtrAS1Ty = VectorType::get(Int64PtrAS1Ty, 2); 199 Type *V4Int32PtrAS1Ty = VectorType::get(Int32PtrAS1Ty, 4); 200 Type *V4Int64PtrAS1Ty = VectorType::get(Int64PtrAS1Ty, 4); 201 202 Type *V2Int64PtrTy = VectorType::get(Int64PtrTy, 2); 203 Type *V2Int32PtrTy = VectorType::get(Int32PtrTy, 2); 204 Type *V4Int32PtrTy = VectorType::get(Int32PtrTy, 4); 205 206 const Constant* c8 = Constant::getNullValue(V8x8Ty); 207 const Constant* c64 = Constant::getNullValue(V8x64Ty); 208 209 const Constant *v2ptr32 = Constant::getNullValue(V2Int32PtrTy); 210 211 EXPECT_TRUE(CastInst::isCastable(V8x8Ty, X86MMXTy)); 212 EXPECT_TRUE(CastInst::isCastable(X86MMXTy, V8x8Ty)); 213 EXPECT_FALSE(CastInst::isCastable(Int64Ty, X86MMXTy)); 214 EXPECT_TRUE(CastInst::isCastable(V8x64Ty, V8x8Ty)); 215 EXPECT_TRUE(CastInst::isCastable(V8x8Ty, V8x64Ty)); 216 EXPECT_EQ(CastInst::Trunc, CastInst::getCastOpcode(c64, true, V8x8Ty, true)); 217 EXPECT_EQ(CastInst::SExt, CastInst::getCastOpcode(c8, true, V8x64Ty, true)); 218 219 EXPECT_FALSE(CastInst::isBitCastable(V8x8Ty, X86MMXTy)); 220 EXPECT_FALSE(CastInst::isBitCastable(X86MMXTy, V8x8Ty)); 221 EXPECT_FALSE(CastInst::isBitCastable(Int64Ty, X86MMXTy)); 222 EXPECT_FALSE(CastInst::isBitCastable(V8x64Ty, V8x8Ty)); 223 EXPECT_FALSE(CastInst::isBitCastable(V8x8Ty, V8x64Ty)); 224 225 // Check address space casts are rejected since we don't know the sizes here 226 EXPECT_FALSE(CastInst::isBitCastable(Int32PtrTy, Int32PtrAS1Ty)); 227 EXPECT_FALSE(CastInst::isBitCastable(Int32PtrAS1Ty, Int32PtrTy)); 228 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrTy, V2Int32PtrAS1Ty)); 229 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V2Int32PtrTy)); 230 EXPECT_TRUE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V2Int64PtrAS1Ty)); 231 EXPECT_TRUE(CastInst::isCastable(V2Int32PtrAS1Ty, V2Int32PtrTy)); 232 EXPECT_EQ(CastInst::AddrSpaceCast, CastInst::getCastOpcode(v2ptr32, true, 233 V2Int32PtrAS1Ty, 234 true)); 235 236 // Test mismatched number of elements for pointers 237 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V4Int64PtrAS1Ty)); 238 EXPECT_FALSE(CastInst::isBitCastable(V4Int64PtrAS1Ty, V2Int32PtrAS1Ty)); 239 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V4Int32PtrAS1Ty)); 240 EXPECT_FALSE(CastInst::isBitCastable(Int32PtrTy, V2Int32PtrTy)); 241 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrTy, Int32PtrTy)); 242 243 EXPECT_TRUE(CastInst::isBitCastable(Int32PtrTy, Int64PtrTy)); 244 EXPECT_FALSE(CastInst::isBitCastable(DoubleTy, FloatTy)); 245 EXPECT_FALSE(CastInst::isBitCastable(FloatTy, DoubleTy)); 246 EXPECT_TRUE(CastInst::isBitCastable(FloatTy, FloatTy)); 247 EXPECT_TRUE(CastInst::isBitCastable(FloatTy, FloatTy)); 248 EXPECT_TRUE(CastInst::isBitCastable(FloatTy, Int32Ty)); 249 EXPECT_TRUE(CastInst::isBitCastable(Int16Ty, HalfTy)); 250 EXPECT_TRUE(CastInst::isBitCastable(Int32Ty, FloatTy)); 251 EXPECT_TRUE(CastInst::isBitCastable(V2Int32Ty, Int64Ty)); 252 253 EXPECT_TRUE(CastInst::isBitCastable(V2Int32Ty, V4Int16Ty)); 254 EXPECT_FALSE(CastInst::isBitCastable(Int32Ty, Int64Ty)); 255 EXPECT_FALSE(CastInst::isBitCastable(Int64Ty, Int32Ty)); 256 257 EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrTy, Int64Ty)); 258 EXPECT_FALSE(CastInst::isBitCastable(Int64Ty, V2Int32PtrTy)); 259 EXPECT_TRUE(CastInst::isBitCastable(V2Int64PtrTy, V2Int32PtrTy)); 260 EXPECT_TRUE(CastInst::isBitCastable(V2Int32PtrTy, V2Int64PtrTy)); 261 EXPECT_FALSE(CastInst::isBitCastable(V2Int32Ty, V2Int64Ty)); 262 EXPECT_FALSE(CastInst::isBitCastable(V2Int64Ty, V2Int32Ty)); 263 264 265 EXPECT_FALSE(CastInst::castIsValid(Instruction::BitCast, 266 Constant::getNullValue(V4Int32PtrTy), 267 V2Int32PtrTy)); 268 EXPECT_FALSE(CastInst::castIsValid(Instruction::BitCast, 269 Constant::getNullValue(V2Int32PtrTy), 270 V4Int32PtrTy)); 271 272 EXPECT_FALSE(CastInst::castIsValid(Instruction::AddrSpaceCast, 273 Constant::getNullValue(V4Int32PtrAS1Ty), 274 V2Int32PtrTy)); 275 EXPECT_FALSE(CastInst::castIsValid(Instruction::AddrSpaceCast, 276 Constant::getNullValue(V2Int32PtrTy), 277 V4Int32PtrAS1Ty)); 278 279 280 // Check that assertion is not hit when creating a cast with a vector of 281 // pointers 282 // First form 283 BasicBlock *BB = BasicBlock::Create(C); 284 Constant *NullV2I32Ptr = Constant::getNullValue(V2Int32PtrTy); 285 auto Inst1 = CastInst::CreatePointerCast(NullV2I32Ptr, V2Int32Ty, "foo", BB); 286 287 // Second form 288 auto Inst2 = CastInst::CreatePointerCast(NullV2I32Ptr, V2Int32Ty); 289 290 delete Inst2; 291 Inst1->eraseFromParent(); 292 delete BB; 293 } 294 295 TEST(InstructionsTest, VectorGep) { 296 LLVMContext C; 297 298 // Type Definitions 299 Type *I8Ty = IntegerType::get(C, 8); 300 Type *I32Ty = IntegerType::get(C, 32); 301 PointerType *Ptri8Ty = PointerType::get(I8Ty, 0); 302 PointerType *Ptri32Ty = PointerType::get(I32Ty, 0); 303 304 VectorType *V2xi8PTy = VectorType::get(Ptri8Ty, 2); 305 VectorType *V2xi32PTy = VectorType::get(Ptri32Ty, 2); 306 307 // Test different aspects of the vector-of-pointers type 308 // and GEPs which use this type. 309 ConstantInt *Ci32a = ConstantInt::get(C, APInt(32, 1492)); 310 ConstantInt *Ci32b = ConstantInt::get(C, APInt(32, 1948)); 311 std::vector<Constant*> ConstVa(2, Ci32a); 312 std::vector<Constant*> ConstVb(2, Ci32b); 313 Constant *C2xi32a = ConstantVector::get(ConstVa); 314 Constant *C2xi32b = ConstantVector::get(ConstVb); 315 316 CastInst *PtrVecA = new IntToPtrInst(C2xi32a, V2xi32PTy); 317 CastInst *PtrVecB = new IntToPtrInst(C2xi32b, V2xi32PTy); 318 319 ICmpInst *ICmp0 = new ICmpInst(ICmpInst::ICMP_SGT, PtrVecA, PtrVecB); 320 ICmpInst *ICmp1 = new ICmpInst(ICmpInst::ICMP_ULT, PtrVecA, PtrVecB); 321 EXPECT_NE(ICmp0, ICmp1); // suppress warning. 322 323 BasicBlock* BB0 = BasicBlock::Create(C); 324 // Test InsertAtEnd ICmpInst constructor. 325 ICmpInst *ICmp2 = new ICmpInst(*BB0, ICmpInst::ICMP_SGE, PtrVecA, PtrVecB); 326 EXPECT_NE(ICmp0, ICmp2); // suppress warning. 327 328 GetElementPtrInst *Gep0 = GetElementPtrInst::Create(I32Ty, PtrVecA, C2xi32a); 329 GetElementPtrInst *Gep1 = GetElementPtrInst::Create(I32Ty, PtrVecA, C2xi32b); 330 GetElementPtrInst *Gep2 = GetElementPtrInst::Create(I32Ty, PtrVecB, C2xi32a); 331 GetElementPtrInst *Gep3 = GetElementPtrInst::Create(I32Ty, PtrVecB, C2xi32b); 332 333 CastInst *BTC0 = new BitCastInst(Gep0, V2xi8PTy); 334 CastInst *BTC1 = new BitCastInst(Gep1, V2xi8PTy); 335 CastInst *BTC2 = new BitCastInst(Gep2, V2xi8PTy); 336 CastInst *BTC3 = new BitCastInst(Gep3, V2xi8PTy); 337 338 Value *S0 = BTC0->stripPointerCasts(); 339 Value *S1 = BTC1->stripPointerCasts(); 340 Value *S2 = BTC2->stripPointerCasts(); 341 Value *S3 = BTC3->stripPointerCasts(); 342 343 EXPECT_NE(S0, Gep0); 344 EXPECT_NE(S1, Gep1); 345 EXPECT_NE(S2, Gep2); 346 EXPECT_NE(S3, Gep3); 347 348 int64_t Offset; 349 DataLayout TD("e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f3" 350 "2:32:32-f64:64:64-v64:64:64-v128:128:128-a:0:64-s:64:64-f80" 351 ":128:128-n8:16:32:64-S128"); 352 // Make sure we don't crash 353 GetPointerBaseWithConstantOffset(Gep0, Offset, TD); 354 GetPointerBaseWithConstantOffset(Gep1, Offset, TD); 355 GetPointerBaseWithConstantOffset(Gep2, Offset, TD); 356 GetPointerBaseWithConstantOffset(Gep3, Offset, TD); 357 358 // Gep of Geps 359 GetElementPtrInst *GepII0 = GetElementPtrInst::Create(I32Ty, Gep0, C2xi32b); 360 GetElementPtrInst *GepII1 = GetElementPtrInst::Create(I32Ty, Gep1, C2xi32a); 361 GetElementPtrInst *GepII2 = GetElementPtrInst::Create(I32Ty, Gep2, C2xi32b); 362 GetElementPtrInst *GepII3 = GetElementPtrInst::Create(I32Ty, Gep3, C2xi32a); 363 364 EXPECT_EQ(GepII0->getNumIndices(), 1u); 365 EXPECT_EQ(GepII1->getNumIndices(), 1u); 366 EXPECT_EQ(GepII2->getNumIndices(), 1u); 367 EXPECT_EQ(GepII3->getNumIndices(), 1u); 368 369 EXPECT_FALSE(GepII0->hasAllZeroIndices()); 370 EXPECT_FALSE(GepII1->hasAllZeroIndices()); 371 EXPECT_FALSE(GepII2->hasAllZeroIndices()); 372 EXPECT_FALSE(GepII3->hasAllZeroIndices()); 373 374 delete GepII0; 375 delete GepII1; 376 delete GepII2; 377 delete GepII3; 378 379 delete BTC0; 380 delete BTC1; 381 delete BTC2; 382 delete BTC3; 383 384 delete Gep0; 385 delete Gep1; 386 delete Gep2; 387 delete Gep3; 388 389 ICmp2->eraseFromParent(); 390 delete BB0; 391 392 delete ICmp0; 393 delete ICmp1; 394 delete PtrVecA; 395 delete PtrVecB; 396 } 397 398 TEST(InstructionsTest, FPMathOperator) { 399 LLVMContext Context; 400 IRBuilder<> Builder(Context); 401 MDBuilder MDHelper(Context); 402 Instruction *I = Builder.CreatePHI(Builder.getDoubleTy(), 0); 403 MDNode *MD1 = MDHelper.createFPMath(1.0); 404 Value *V1 = Builder.CreateFAdd(I, I, "", MD1); 405 EXPECT_TRUE(isa<FPMathOperator>(V1)); 406 FPMathOperator *O1 = cast<FPMathOperator>(V1); 407 EXPECT_EQ(O1->getFPAccuracy(), 1.0); 408 delete V1; 409 delete I; 410 } 411 412 413 TEST(InstructionsTest, isEliminableCastPair) { 414 LLVMContext C; 415 416 Type* Int16Ty = Type::getInt16Ty(C); 417 Type* Int32Ty = Type::getInt32Ty(C); 418 Type* Int64Ty = Type::getInt64Ty(C); 419 Type* Int64PtrTy = Type::getInt64PtrTy(C); 420 421 // Source and destination pointers have same size -> bitcast. 422 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt, 423 CastInst::IntToPtr, 424 Int64PtrTy, Int64Ty, Int64PtrTy, 425 Int32Ty, nullptr, Int32Ty), 426 CastInst::BitCast); 427 428 // Source and destination have unknown sizes, but the same address space and 429 // the intermediate int is the maximum pointer size -> bitcast 430 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt, 431 CastInst::IntToPtr, 432 Int64PtrTy, Int64Ty, Int64PtrTy, 433 nullptr, nullptr, nullptr), 434 CastInst::BitCast); 435 436 // Source and destination have unknown sizes, but the same address space and 437 // the intermediate int is not the maximum pointer size -> nothing 438 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::PtrToInt, 439 CastInst::IntToPtr, 440 Int64PtrTy, Int32Ty, Int64PtrTy, 441 nullptr, nullptr, nullptr), 442 0U); 443 444 // Middle pointer big enough -> bitcast. 445 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr, 446 CastInst::PtrToInt, 447 Int64Ty, Int64PtrTy, Int64Ty, 448 nullptr, Int64Ty, nullptr), 449 CastInst::BitCast); 450 451 // Middle pointer too small -> fail. 452 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr, 453 CastInst::PtrToInt, 454 Int64Ty, Int64PtrTy, Int64Ty, 455 nullptr, Int32Ty, nullptr), 456 0U); 457 458 // Test that we don't eliminate bitcasts between different address spaces, 459 // or if we don't have available pointer size information. 460 DataLayout DL("e-p:32:32:32-p1:16:16:16-p2:64:64:64-i1:8:8-i8:8:8-i16:16:16" 461 "-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64" 462 "-v128:128:128-a:0:64-s:64:64-f80:128:128-n8:16:32:64-S128"); 463 464 Type* Int64PtrTyAS1 = Type::getInt64PtrTy(C, 1); 465 Type* Int64PtrTyAS2 = Type::getInt64PtrTy(C, 2); 466 467 IntegerType *Int16SizePtr = DL.getIntPtrType(C, 1); 468 IntegerType *Int64SizePtr = DL.getIntPtrType(C, 2); 469 470 // Cannot simplify inttoptr, addrspacecast 471 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr, 472 CastInst::AddrSpaceCast, 473 Int16Ty, Int64PtrTyAS1, Int64PtrTyAS2, 474 nullptr, Int16SizePtr, Int64SizePtr), 475 0U); 476 477 // Cannot simplify addrspacecast, ptrtoint 478 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::AddrSpaceCast, 479 CastInst::PtrToInt, 480 Int64PtrTyAS1, Int64PtrTyAS2, Int16Ty, 481 Int64SizePtr, Int16SizePtr, nullptr), 482 0U); 483 484 // Pass since the bitcast address spaces are the same 485 EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr, 486 CastInst::BitCast, 487 Int16Ty, Int64PtrTyAS1, Int64PtrTyAS1, 488 nullptr, nullptr, nullptr), 489 CastInst::IntToPtr); 490 491 } 492 493 TEST(InstructionsTest, CloneCall) { 494 LLVMContext C; 495 Type *Int32Ty = Type::getInt32Ty(C); 496 Type *ArgTys[] = {Int32Ty, Int32Ty, Int32Ty}; 497 Type *FnTy = FunctionType::get(Int32Ty, ArgTys, /*isVarArg=*/false); 498 Value *Callee = Constant::getNullValue(FnTy->getPointerTo()); 499 Value *Args[] = { 500 ConstantInt::get(Int32Ty, 1), 501 ConstantInt::get(Int32Ty, 2), 502 ConstantInt::get(Int32Ty, 3) 503 }; 504 std::unique_ptr<CallInst> Call(CallInst::Create(Callee, Args, "result")); 505 506 // Test cloning the tail call kind. 507 CallInst::TailCallKind Kinds[] = {CallInst::TCK_None, CallInst::TCK_Tail, 508 CallInst::TCK_MustTail}; 509 for (CallInst::TailCallKind TCK : Kinds) { 510 Call->setTailCallKind(TCK); 511 std::unique_ptr<CallInst> Clone(cast<CallInst>(Call->clone())); 512 EXPECT_EQ(Call->getTailCallKind(), Clone->getTailCallKind()); 513 } 514 Call->setTailCallKind(CallInst::TCK_None); 515 516 // Test cloning an attribute. 517 { 518 AttrBuilder AB; 519 AB.addAttribute(Attribute::ReadOnly); 520 Call->setAttributes(AttributeSet::get(C, AttributeSet::FunctionIndex, AB)); 521 std::unique_ptr<CallInst> Clone(cast<CallInst>(Call->clone())); 522 EXPECT_TRUE(Clone->onlyReadsMemory()); 523 } 524 } 525 526 TEST(InstructionsTest, AlterCallBundles) { 527 LLVMContext C; 528 Type *Int32Ty = Type::getInt32Ty(C); 529 Type *FnTy = FunctionType::get(Int32Ty, Int32Ty, /*isVarArg=*/false); 530 Value *Callee = Constant::getNullValue(FnTy->getPointerTo()); 531 Value *Args[] = {ConstantInt::get(Int32Ty, 42)}; 532 OperandBundleDef OldBundle("before", UndefValue::get(Int32Ty)); 533 std::unique_ptr<CallInst> Call( 534 CallInst::Create(Callee, Args, OldBundle, "result")); 535 Call->setTailCallKind(CallInst::TailCallKind::TCK_NoTail); 536 AttrBuilder AB; 537 AB.addAttribute(Attribute::Cold); 538 Call->setAttributes(AttributeSet::get(C, AttributeSet::FunctionIndex, AB)); 539 Call->setDebugLoc(DebugLoc(MDNode::get(C, None))); 540 541 OperandBundleDef NewBundle("after", ConstantInt::get(Int32Ty, 7)); 542 std::unique_ptr<CallInst> Clone(CallInst::Create(Call.get(), NewBundle)); 543 EXPECT_EQ(Call->getNumArgOperands(), Clone->getNumArgOperands()); 544 EXPECT_EQ(Call->getArgOperand(0), Clone->getArgOperand(0)); 545 EXPECT_EQ(Call->getCallingConv(), Clone->getCallingConv()); 546 EXPECT_EQ(Call->getTailCallKind(), Clone->getTailCallKind()); 547 EXPECT_TRUE(Clone->hasFnAttr(Attribute::AttrKind::Cold)); 548 EXPECT_EQ(Call->getDebugLoc(), Clone->getDebugLoc()); 549 EXPECT_EQ(Clone->getNumOperandBundles(), 1U); 550 EXPECT_TRUE(Clone->getOperandBundle("after").hasValue()); 551 } 552 553 TEST(InstructionsTest, AlterInvokeBundles) { 554 LLVMContext C; 555 Type *Int32Ty = Type::getInt32Ty(C); 556 Type *FnTy = FunctionType::get(Int32Ty, Int32Ty, /*isVarArg=*/false); 557 Value *Callee = Constant::getNullValue(FnTy->getPointerTo()); 558 Value *Args[] = {ConstantInt::get(Int32Ty, 42)}; 559 std::unique_ptr<BasicBlock> NormalDest(BasicBlock::Create(C)); 560 std::unique_ptr<BasicBlock> UnwindDest(BasicBlock::Create(C)); 561 OperandBundleDef OldBundle("before", UndefValue::get(Int32Ty)); 562 std::unique_ptr<InvokeInst> Invoke(InvokeInst::Create( 563 Callee, NormalDest.get(), UnwindDest.get(), Args, OldBundle, "result")); 564 AttrBuilder AB; 565 AB.addAttribute(Attribute::Cold); 566 Invoke->setAttributes(AttributeSet::get(C, AttributeSet::FunctionIndex, AB)); 567 Invoke->setDebugLoc(DebugLoc(MDNode::get(C, None))); 568 569 OperandBundleDef NewBundle("after", ConstantInt::get(Int32Ty, 7)); 570 std::unique_ptr<InvokeInst> Clone( 571 InvokeInst::Create(Invoke.get(), NewBundle)); 572 EXPECT_EQ(Invoke->getNormalDest(), Clone->getNormalDest()); 573 EXPECT_EQ(Invoke->getUnwindDest(), Clone->getUnwindDest()); 574 EXPECT_EQ(Invoke->getNumArgOperands(), Clone->getNumArgOperands()); 575 EXPECT_EQ(Invoke->getArgOperand(0), Clone->getArgOperand(0)); 576 EXPECT_EQ(Invoke->getCallingConv(), Clone->getCallingConv()); 577 EXPECT_TRUE(Clone->hasFnAttr(Attribute::AttrKind::Cold)); 578 EXPECT_EQ(Invoke->getDebugLoc(), Clone->getDebugLoc()); 579 EXPECT_EQ(Clone->getNumOperandBundles(), 1U); 580 EXPECT_TRUE(Clone->getOperandBundle("after").hasValue()); 581 } 582 583 TEST_F(ModuleWithFunctionTest, DropPoisonGeneratingFlags) { 584 auto *OnlyBB = BasicBlock::Create(Ctx, "bb", F); 585 auto *Arg0 = &*F->arg_begin(); 586 587 IRBuilder<NoFolder> B(Ctx); 588 B.SetInsertPoint(OnlyBB); 589 590 { 591 auto *UI = 592 cast<Instruction>(B.CreateUDiv(Arg0, Arg0, "", /*isExact*/ true)); 593 ASSERT_TRUE(UI->isExact()); 594 UI->dropPoisonGeneratingFlags(); 595 ASSERT_FALSE(UI->isExact()); 596 } 597 598 { 599 auto *ShrI = 600 cast<Instruction>(B.CreateLShr(Arg0, Arg0, "", /*isExact*/ true)); 601 ASSERT_TRUE(ShrI->isExact()); 602 ShrI->dropPoisonGeneratingFlags(); 603 ASSERT_FALSE(ShrI->isExact()); 604 } 605 606 { 607 auto *AI = cast<Instruction>( 608 B.CreateAdd(Arg0, Arg0, "", /*HasNUW*/ true, /*HasNSW*/ false)); 609 ASSERT_TRUE(AI->hasNoUnsignedWrap()); 610 AI->dropPoisonGeneratingFlags(); 611 ASSERT_FALSE(AI->hasNoUnsignedWrap()); 612 ASSERT_FALSE(AI->hasNoSignedWrap()); 613 } 614 615 { 616 auto *SI = cast<Instruction>( 617 B.CreateAdd(Arg0, Arg0, "", /*HasNUW*/ false, /*HasNSW*/ true)); 618 ASSERT_TRUE(SI->hasNoSignedWrap()); 619 SI->dropPoisonGeneratingFlags(); 620 ASSERT_FALSE(SI->hasNoUnsignedWrap()); 621 ASSERT_FALSE(SI->hasNoSignedWrap()); 622 } 623 624 { 625 auto *ShlI = cast<Instruction>( 626 B.CreateShl(Arg0, Arg0, "", /*HasNUW*/ true, /*HasNSW*/ true)); 627 ASSERT_TRUE(ShlI->hasNoSignedWrap()); 628 ASSERT_TRUE(ShlI->hasNoUnsignedWrap()); 629 ShlI->dropPoisonGeneratingFlags(); 630 ASSERT_FALSE(ShlI->hasNoUnsignedWrap()); 631 ASSERT_FALSE(ShlI->hasNoSignedWrap()); 632 } 633 634 { 635 Value *GEPBase = Constant::getNullValue(B.getInt8PtrTy()); 636 auto *GI = cast<GetElementPtrInst>(B.CreateInBoundsGEP(GEPBase, {Arg0})); 637 ASSERT_TRUE(GI->isInBounds()); 638 GI->dropPoisonGeneratingFlags(); 639 ASSERT_FALSE(GI->isInBounds()); 640 } 641 } 642 643 TEST(InstructionsTest, GEPIndices) { 644 LLVMContext Context; 645 IRBuilder<NoFolder> Builder(Context); 646 Type *ElementTy = Builder.getInt8Ty(); 647 Type *ArrTy = ArrayType::get(ArrayType::get(ElementTy, 64), 64); 648 Value *Indices[] = { 649 Builder.getInt32(0), 650 Builder.getInt32(13), 651 Builder.getInt32(42) }; 652 653 Value *V = Builder.CreateGEP(ArrTy, UndefValue::get(PointerType::getUnqual(ArrTy)), 654 Indices); 655 ASSERT_TRUE(isa<GetElementPtrInst>(V)); 656 657 auto *GEPI = cast<GetElementPtrInst>(V); 658 ASSERT_NE(GEPI->idx_begin(), GEPI->idx_end()); 659 ASSERT_EQ(GEPI->idx_end(), std::next(GEPI->idx_begin(), 3)); 660 EXPECT_EQ(Indices[0], GEPI->idx_begin()[0]); 661 EXPECT_EQ(Indices[1], GEPI->idx_begin()[1]); 662 EXPECT_EQ(Indices[2], GEPI->idx_begin()[2]); 663 EXPECT_EQ(GEPI->idx_begin(), GEPI->indices().begin()); 664 EXPECT_EQ(GEPI->idx_end(), GEPI->indices().end()); 665 666 const auto *CGEPI = GEPI; 667 ASSERT_NE(CGEPI->idx_begin(), CGEPI->idx_end()); 668 ASSERT_EQ(CGEPI->idx_end(), std::next(CGEPI->idx_begin(), 3)); 669 EXPECT_EQ(Indices[0], CGEPI->idx_begin()[0]); 670 EXPECT_EQ(Indices[1], CGEPI->idx_begin()[1]); 671 EXPECT_EQ(Indices[2], CGEPI->idx_begin()[2]); 672 EXPECT_EQ(CGEPI->idx_begin(), CGEPI->indices().begin()); 673 EXPECT_EQ(CGEPI->idx_end(), CGEPI->indices().end()); 674 675 delete GEPI; 676 } 677 678 } // end anonymous namespace 679 } // end namespace llvm 680