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