1 //===- Instructions.cpp - Implement the LLVM instructions -----------------===// 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 // This file implements all of the non-inline methods for the LLVM instruction 10 // classes. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/IR/Instructions.h" 15 #include "LLVMContextImpl.h" 16 #include "llvm/ADT/SmallBitVector.h" 17 #include "llvm/ADT/SmallVector.h" 18 #include "llvm/ADT/Twine.h" 19 #include "llvm/IR/Attributes.h" 20 #include "llvm/IR/BasicBlock.h" 21 #include "llvm/IR/Constant.h" 22 #include "llvm/IR/ConstantRange.h" 23 #include "llvm/IR/Constants.h" 24 #include "llvm/IR/DataLayout.h" 25 #include "llvm/IR/DerivedTypes.h" 26 #include "llvm/IR/Function.h" 27 #include "llvm/IR/InstrTypes.h" 28 #include "llvm/IR/Instruction.h" 29 #include "llvm/IR/Intrinsics.h" 30 #include "llvm/IR/LLVMContext.h" 31 #include "llvm/IR/MDBuilder.h" 32 #include "llvm/IR/Metadata.h" 33 #include "llvm/IR/Module.h" 34 #include "llvm/IR/Operator.h" 35 #include "llvm/IR/ProfDataUtils.h" 36 #include "llvm/IR/Type.h" 37 #include "llvm/IR/Value.h" 38 #include "llvm/Support/AtomicOrdering.h" 39 #include "llvm/Support/Casting.h" 40 #include "llvm/Support/ErrorHandling.h" 41 #include "llvm/Support/MathExtras.h" 42 #include "llvm/Support/ModRef.h" 43 #include "llvm/Support/TypeSize.h" 44 #include <algorithm> 45 #include <cassert> 46 #include <cstdint> 47 #include <optional> 48 #include <vector> 49 50 using namespace llvm; 51 52 static cl::opt<bool> DisableI2pP2iOpt( 53 "disable-i2p-p2i-opt", cl::init(false), 54 cl::desc("Disables inttoptr/ptrtoint roundtrip optimization")); 55 56 //===----------------------------------------------------------------------===// 57 // AllocaInst Class 58 //===----------------------------------------------------------------------===// 59 60 std::optional<TypeSize> 61 AllocaInst::getAllocationSize(const DataLayout &DL) const { 62 TypeSize Size = DL.getTypeAllocSize(getAllocatedType()); 63 if (isArrayAllocation()) { 64 auto *C = dyn_cast<ConstantInt>(getArraySize()); 65 if (!C) 66 return std::nullopt; 67 assert(!Size.isScalable() && "Array elements cannot have a scalable size"); 68 Size *= C->getZExtValue(); 69 } 70 return Size; 71 } 72 73 std::optional<TypeSize> 74 AllocaInst::getAllocationSizeInBits(const DataLayout &DL) const { 75 std::optional<TypeSize> Size = getAllocationSize(DL); 76 if (Size) 77 return *Size * 8; 78 return std::nullopt; 79 } 80 81 //===----------------------------------------------------------------------===// 82 // SelectInst Class 83 //===----------------------------------------------------------------------===// 84 85 /// areInvalidOperands - Return a string if the specified operands are invalid 86 /// for a select operation, otherwise return null. 87 const char *SelectInst::areInvalidOperands(Value *Op0, Value *Op1, Value *Op2) { 88 if (Op1->getType() != Op2->getType()) 89 return "both values to select must have same type"; 90 91 if (Op1->getType()->isTokenTy()) 92 return "select values cannot have token type"; 93 94 if (VectorType *VT = dyn_cast<VectorType>(Op0->getType())) { 95 // Vector select. 96 if (VT->getElementType() != Type::getInt1Ty(Op0->getContext())) 97 return "vector select condition element type must be i1"; 98 VectorType *ET = dyn_cast<VectorType>(Op1->getType()); 99 if (!ET) 100 return "selected values for vector select must be vectors"; 101 if (ET->getElementCount() != VT->getElementCount()) 102 return "vector select requires selected vectors to have " 103 "the same vector length as select condition"; 104 } else if (Op0->getType() != Type::getInt1Ty(Op0->getContext())) { 105 return "select condition must be i1 or <n x i1>"; 106 } 107 return nullptr; 108 } 109 110 //===----------------------------------------------------------------------===// 111 // PHINode Class 112 //===----------------------------------------------------------------------===// 113 114 PHINode::PHINode(const PHINode &PN) 115 : Instruction(PN.getType(), Instruction::PHI, nullptr, PN.getNumOperands()), 116 ReservedSpace(PN.getNumOperands()) { 117 allocHungoffUses(PN.getNumOperands()); 118 std::copy(PN.op_begin(), PN.op_end(), op_begin()); 119 copyIncomingBlocks(make_range(PN.block_begin(), PN.block_end())); 120 SubclassOptionalData = PN.SubclassOptionalData; 121 } 122 123 // removeIncomingValue - Remove an incoming value. This is useful if a 124 // predecessor basic block is deleted. 125 Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) { 126 Value *Removed = getIncomingValue(Idx); 127 128 // Move everything after this operand down. 129 // 130 // FIXME: we could just swap with the end of the list, then erase. However, 131 // clients might not expect this to happen. The code as it is thrashes the 132 // use/def lists, which is kinda lame. 133 std::copy(op_begin() + Idx + 1, op_end(), op_begin() + Idx); 134 copyIncomingBlocks(drop_begin(blocks(), Idx + 1), Idx); 135 136 // Nuke the last value. 137 Op<-1>().set(nullptr); 138 setNumHungOffUseOperands(getNumOperands() - 1); 139 140 // If the PHI node is dead, because it has zero entries, nuke it now. 141 if (getNumOperands() == 0 && DeletePHIIfEmpty) { 142 // If anyone is using this PHI, make them use a dummy value instead... 143 replaceAllUsesWith(PoisonValue::get(getType())); 144 eraseFromParent(); 145 } 146 return Removed; 147 } 148 149 void PHINode::removeIncomingValueIf(function_ref<bool(unsigned)> Predicate, 150 bool DeletePHIIfEmpty) { 151 SmallDenseSet<unsigned> RemoveIndices; 152 for (unsigned Idx = 0; Idx < getNumIncomingValues(); ++Idx) 153 if (Predicate(Idx)) 154 RemoveIndices.insert(Idx); 155 156 if (RemoveIndices.empty()) 157 return; 158 159 // Remove operands. 160 auto NewOpEnd = remove_if(operands(), [&](Use &U) { 161 return RemoveIndices.contains(U.getOperandNo()); 162 }); 163 for (Use &U : make_range(NewOpEnd, op_end())) 164 U.set(nullptr); 165 166 // Remove incoming blocks. 167 (void)std::remove_if(const_cast<block_iterator>(block_begin()), 168 const_cast<block_iterator>(block_end()), [&](BasicBlock *&BB) { 169 return RemoveIndices.contains(&BB - block_begin()); 170 }); 171 172 setNumHungOffUseOperands(getNumOperands() - RemoveIndices.size()); 173 174 // If the PHI node is dead, because it has zero entries, nuke it now. 175 if (getNumOperands() == 0 && DeletePHIIfEmpty) { 176 // If anyone is using this PHI, make them use a dummy value instead... 177 replaceAllUsesWith(PoisonValue::get(getType())); 178 eraseFromParent(); 179 } 180 } 181 182 /// growOperands - grow operands - This grows the operand list in response 183 /// to a push_back style of operation. This grows the number of ops by 1.5 184 /// times. 185 /// 186 void PHINode::growOperands() { 187 unsigned e = getNumOperands(); 188 unsigned NumOps = e + e / 2; 189 if (NumOps < 2) NumOps = 2; // 2 op PHI nodes are VERY common. 190 191 ReservedSpace = NumOps; 192 growHungoffUses(ReservedSpace, /* IsPhi */ true); 193 } 194 195 /// hasConstantValue - If the specified PHI node always merges together the same 196 /// value, return the value, otherwise return null. 197 Value *PHINode::hasConstantValue() const { 198 // Exploit the fact that phi nodes always have at least one entry. 199 Value *ConstantValue = getIncomingValue(0); 200 for (unsigned i = 1, e = getNumIncomingValues(); i != e; ++i) 201 if (getIncomingValue(i) != ConstantValue && getIncomingValue(i) != this) { 202 if (ConstantValue != this) 203 return nullptr; // Incoming values not all the same. 204 // The case where the first value is this PHI. 205 ConstantValue = getIncomingValue(i); 206 } 207 if (ConstantValue == this) 208 return PoisonValue::get(getType()); 209 return ConstantValue; 210 } 211 212 /// hasConstantOrUndefValue - Whether the specified PHI node always merges 213 /// together the same value, assuming that undefs result in the same value as 214 /// non-undefs. 215 /// Unlike \ref hasConstantValue, this does not return a value because the 216 /// unique non-undef incoming value need not dominate the PHI node. 217 bool PHINode::hasConstantOrUndefValue() const { 218 Value *ConstantValue = nullptr; 219 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i) { 220 Value *Incoming = getIncomingValue(i); 221 if (Incoming != this && !isa<UndefValue>(Incoming)) { 222 if (ConstantValue && ConstantValue != Incoming) 223 return false; 224 ConstantValue = Incoming; 225 } 226 } 227 return true; 228 } 229 230 //===----------------------------------------------------------------------===// 231 // LandingPadInst Implementation 232 //===----------------------------------------------------------------------===// 233 234 LandingPadInst::LandingPadInst(Type *RetTy, unsigned NumReservedValues, 235 const Twine &NameStr, 236 InsertPosition InsertBefore) 237 : Instruction(RetTy, Instruction::LandingPad, nullptr, 0, InsertBefore) { 238 init(NumReservedValues, NameStr); 239 } 240 241 LandingPadInst::LandingPadInst(const LandingPadInst &LP) 242 : Instruction(LP.getType(), Instruction::LandingPad, nullptr, 243 LP.getNumOperands()), 244 ReservedSpace(LP.getNumOperands()) { 245 allocHungoffUses(LP.getNumOperands()); 246 Use *OL = getOperandList(); 247 const Use *InOL = LP.getOperandList(); 248 for (unsigned I = 0, E = ReservedSpace; I != E; ++I) 249 OL[I] = InOL[I]; 250 251 setCleanup(LP.isCleanup()); 252 } 253 254 LandingPadInst *LandingPadInst::Create(Type *RetTy, unsigned NumReservedClauses, 255 const Twine &NameStr, 256 InsertPosition InsertBefore) { 257 return new LandingPadInst(RetTy, NumReservedClauses, NameStr, InsertBefore); 258 } 259 260 void LandingPadInst::init(unsigned NumReservedValues, const Twine &NameStr) { 261 ReservedSpace = NumReservedValues; 262 setNumHungOffUseOperands(0); 263 allocHungoffUses(ReservedSpace); 264 setName(NameStr); 265 setCleanup(false); 266 } 267 268 /// growOperands - grow operands - This grows the operand list in response to a 269 /// push_back style of operation. This grows the number of ops by 2 times. 270 void LandingPadInst::growOperands(unsigned Size) { 271 unsigned e = getNumOperands(); 272 if (ReservedSpace >= e + Size) return; 273 ReservedSpace = (std::max(e, 1U) + Size / 2) * 2; 274 growHungoffUses(ReservedSpace); 275 } 276 277 void LandingPadInst::addClause(Constant *Val) { 278 unsigned OpNo = getNumOperands(); 279 growOperands(1); 280 assert(OpNo < ReservedSpace && "Growing didn't work!"); 281 setNumHungOffUseOperands(getNumOperands() + 1); 282 getOperandList()[OpNo] = Val; 283 } 284 285 //===----------------------------------------------------------------------===// 286 // CallBase Implementation 287 //===----------------------------------------------------------------------===// 288 289 CallBase *CallBase::Create(CallBase *CB, ArrayRef<OperandBundleDef> Bundles, 290 InsertPosition InsertPt) { 291 switch (CB->getOpcode()) { 292 case Instruction::Call: 293 return CallInst::Create(cast<CallInst>(CB), Bundles, InsertPt); 294 case Instruction::Invoke: 295 return InvokeInst::Create(cast<InvokeInst>(CB), Bundles, InsertPt); 296 case Instruction::CallBr: 297 return CallBrInst::Create(cast<CallBrInst>(CB), Bundles, InsertPt); 298 default: 299 llvm_unreachable("Unknown CallBase sub-class!"); 300 } 301 } 302 303 CallBase *CallBase::Create(CallBase *CI, OperandBundleDef OpB, 304 InsertPosition InsertPt) { 305 SmallVector<OperandBundleDef, 2> OpDefs; 306 for (unsigned i = 0, e = CI->getNumOperandBundles(); i < e; ++i) { 307 auto ChildOB = CI->getOperandBundleAt(i); 308 if (ChildOB.getTagName() != OpB.getTag()) 309 OpDefs.emplace_back(ChildOB); 310 } 311 OpDefs.emplace_back(OpB); 312 return CallBase::Create(CI, OpDefs, InsertPt); 313 } 314 315 Function *CallBase::getCaller() { return getParent()->getParent(); } 316 317 unsigned CallBase::getNumSubclassExtraOperandsDynamic() const { 318 assert(getOpcode() == Instruction::CallBr && "Unexpected opcode!"); 319 return cast<CallBrInst>(this)->getNumIndirectDests() + 1; 320 } 321 322 bool CallBase::isIndirectCall() const { 323 const Value *V = getCalledOperand(); 324 if (isa<Function>(V) || isa<Constant>(V)) 325 return false; 326 return !isInlineAsm(); 327 } 328 329 /// Tests if this call site must be tail call optimized. Only a CallInst can 330 /// be tail call optimized. 331 bool CallBase::isMustTailCall() const { 332 if (auto *CI = dyn_cast<CallInst>(this)) 333 return CI->isMustTailCall(); 334 return false; 335 } 336 337 /// Tests if this call site is marked as a tail call. 338 bool CallBase::isTailCall() const { 339 if (auto *CI = dyn_cast<CallInst>(this)) 340 return CI->isTailCall(); 341 return false; 342 } 343 344 Intrinsic::ID CallBase::getIntrinsicID() const { 345 if (auto *F = getCalledFunction()) 346 return F->getIntrinsicID(); 347 return Intrinsic::not_intrinsic; 348 } 349 350 FPClassTest CallBase::getRetNoFPClass() const { 351 FPClassTest Mask = Attrs.getRetNoFPClass(); 352 353 if (const Function *F = getCalledFunction()) 354 Mask |= F->getAttributes().getRetNoFPClass(); 355 return Mask; 356 } 357 358 FPClassTest CallBase::getParamNoFPClass(unsigned i) const { 359 FPClassTest Mask = Attrs.getParamNoFPClass(i); 360 361 if (const Function *F = getCalledFunction()) 362 Mask |= F->getAttributes().getParamNoFPClass(i); 363 return Mask; 364 } 365 366 std::optional<ConstantRange> CallBase::getRange() const { 367 const Attribute RangeAttr = getRetAttr(llvm::Attribute::Range); 368 if (RangeAttr.isValid()) 369 return RangeAttr.getRange(); 370 return std::nullopt; 371 } 372 373 bool CallBase::isReturnNonNull() const { 374 if (hasRetAttr(Attribute::NonNull)) 375 return true; 376 377 if (getRetDereferenceableBytes() > 0 && 378 !NullPointerIsDefined(getCaller(), getType()->getPointerAddressSpace())) 379 return true; 380 381 return false; 382 } 383 384 Value *CallBase::getArgOperandWithAttribute(Attribute::AttrKind Kind) const { 385 unsigned Index; 386 387 if (Attrs.hasAttrSomewhere(Kind, &Index)) 388 return getArgOperand(Index - AttributeList::FirstArgIndex); 389 if (const Function *F = getCalledFunction()) 390 if (F->getAttributes().hasAttrSomewhere(Kind, &Index)) 391 return getArgOperand(Index - AttributeList::FirstArgIndex); 392 393 return nullptr; 394 } 395 396 /// Determine whether the argument or parameter has the given attribute. 397 bool CallBase::paramHasAttr(unsigned ArgNo, Attribute::AttrKind Kind) const { 398 assert(ArgNo < arg_size() && "Param index out of bounds!"); 399 400 if (Attrs.hasParamAttr(ArgNo, Kind)) 401 return true; 402 403 const Function *F = getCalledFunction(); 404 if (!F) 405 return false; 406 407 if (!F->getAttributes().hasParamAttr(ArgNo, Kind)) 408 return false; 409 410 // Take into account mod/ref by operand bundles. 411 switch (Kind) { 412 case Attribute::ReadNone: 413 return !hasReadingOperandBundles() && !hasClobberingOperandBundles(); 414 case Attribute::ReadOnly: 415 return !hasClobberingOperandBundles(); 416 case Attribute::WriteOnly: 417 return !hasReadingOperandBundles(); 418 default: 419 return true; 420 } 421 } 422 423 bool CallBase::hasFnAttrOnCalledFunction(Attribute::AttrKind Kind) const { 424 if (auto *F = dyn_cast<Function>(getCalledOperand())) 425 return F->getAttributes().hasFnAttr(Kind); 426 427 return false; 428 } 429 430 bool CallBase::hasFnAttrOnCalledFunction(StringRef Kind) const { 431 if (auto *F = dyn_cast<Function>(getCalledOperand())) 432 return F->getAttributes().hasFnAttr(Kind); 433 434 return false; 435 } 436 437 template <typename AK> 438 Attribute CallBase::getFnAttrOnCalledFunction(AK Kind) const { 439 if constexpr (std::is_same_v<AK, Attribute::AttrKind>) { 440 // getMemoryEffects() correctly combines memory effects from the call-site, 441 // operand bundles and function. 442 assert(Kind != Attribute::Memory && "Use getMemoryEffects() instead"); 443 } 444 445 if (auto *F = dyn_cast<Function>(getCalledOperand())) 446 return F->getAttributes().getFnAttr(Kind); 447 448 return Attribute(); 449 } 450 451 template Attribute 452 CallBase::getFnAttrOnCalledFunction(Attribute::AttrKind Kind) const; 453 template Attribute CallBase::getFnAttrOnCalledFunction(StringRef Kind) const; 454 455 template <typename AK> 456 Attribute CallBase::getParamAttrOnCalledFunction(unsigned ArgNo, 457 AK Kind) const { 458 Value *V = getCalledOperand(); 459 460 if (auto *F = dyn_cast<Function>(V)) 461 return F->getAttributes().getParamAttr(ArgNo, Kind); 462 463 return Attribute(); 464 } 465 template Attribute 466 CallBase::getParamAttrOnCalledFunction(unsigned ArgNo, 467 Attribute::AttrKind Kind) const; 468 template Attribute CallBase::getParamAttrOnCalledFunction(unsigned ArgNo, 469 StringRef Kind) const; 470 471 void CallBase::getOperandBundlesAsDefs( 472 SmallVectorImpl<OperandBundleDef> &Defs) const { 473 for (unsigned i = 0, e = getNumOperandBundles(); i != e; ++i) 474 Defs.emplace_back(getOperandBundleAt(i)); 475 } 476 477 CallBase::op_iterator 478 CallBase::populateBundleOperandInfos(ArrayRef<OperandBundleDef> Bundles, 479 const unsigned BeginIndex) { 480 auto It = op_begin() + BeginIndex; 481 for (auto &B : Bundles) 482 It = std::copy(B.input_begin(), B.input_end(), It); 483 484 auto *ContextImpl = getContext().pImpl; 485 auto BI = Bundles.begin(); 486 unsigned CurrentIndex = BeginIndex; 487 488 for (auto &BOI : bundle_op_infos()) { 489 assert(BI != Bundles.end() && "Incorrect allocation?"); 490 491 BOI.Tag = ContextImpl->getOrInsertBundleTag(BI->getTag()); 492 BOI.Begin = CurrentIndex; 493 BOI.End = CurrentIndex + BI->input_size(); 494 CurrentIndex = BOI.End; 495 BI++; 496 } 497 498 assert(BI == Bundles.end() && "Incorrect allocation?"); 499 500 return It; 501 } 502 503 CallBase::BundleOpInfo &CallBase::getBundleOpInfoForOperand(unsigned OpIdx) { 504 /// When there isn't many bundles, we do a simple linear search. 505 /// Else fallback to a binary-search that use the fact that bundles usually 506 /// have similar number of argument to get faster convergence. 507 if (bundle_op_info_end() - bundle_op_info_begin() < 8) { 508 for (auto &BOI : bundle_op_infos()) 509 if (BOI.Begin <= OpIdx && OpIdx < BOI.End) 510 return BOI; 511 512 llvm_unreachable("Did not find operand bundle for operand!"); 513 } 514 515 assert(OpIdx >= arg_size() && "the Idx is not in the operand bundles"); 516 assert(bundle_op_info_end() - bundle_op_info_begin() > 0 && 517 OpIdx < std::prev(bundle_op_info_end())->End && 518 "The Idx isn't in the operand bundle"); 519 520 /// We need a decimal number below and to prevent using floating point numbers 521 /// we use an intergal value multiplied by this constant. 522 constexpr unsigned NumberScaling = 1024; 523 524 bundle_op_iterator Begin = bundle_op_info_begin(); 525 bundle_op_iterator End = bundle_op_info_end(); 526 bundle_op_iterator Current = Begin; 527 528 while (Begin != End) { 529 unsigned ScaledOperandPerBundle = 530 NumberScaling * (std::prev(End)->End - Begin->Begin) / (End - Begin); 531 Current = Begin + (((OpIdx - Begin->Begin) * NumberScaling) / 532 ScaledOperandPerBundle); 533 if (Current >= End) 534 Current = std::prev(End); 535 assert(Current < End && Current >= Begin && 536 "the operand bundle doesn't cover every value in the range"); 537 if (OpIdx >= Current->Begin && OpIdx < Current->End) 538 break; 539 if (OpIdx >= Current->End) 540 Begin = Current + 1; 541 else 542 End = Current; 543 } 544 545 assert(OpIdx >= Current->Begin && OpIdx < Current->End && 546 "the operand bundle doesn't cover every value in the range"); 547 return *Current; 548 } 549 550 CallBase *CallBase::addOperandBundle(CallBase *CB, uint32_t ID, 551 OperandBundleDef OB, 552 InsertPosition InsertPt) { 553 if (CB->getOperandBundle(ID)) 554 return CB; 555 556 SmallVector<OperandBundleDef, 1> Bundles; 557 CB->getOperandBundlesAsDefs(Bundles); 558 Bundles.push_back(OB); 559 return Create(CB, Bundles, InsertPt); 560 } 561 562 CallBase *CallBase::removeOperandBundle(CallBase *CB, uint32_t ID, 563 InsertPosition InsertPt) { 564 SmallVector<OperandBundleDef, 1> Bundles; 565 bool CreateNew = false; 566 567 for (unsigned I = 0, E = CB->getNumOperandBundles(); I != E; ++I) { 568 auto Bundle = CB->getOperandBundleAt(I); 569 if (Bundle.getTagID() == ID) { 570 CreateNew = true; 571 continue; 572 } 573 Bundles.emplace_back(Bundle); 574 } 575 576 return CreateNew ? Create(CB, Bundles, InsertPt) : CB; 577 } 578 579 bool CallBase::hasReadingOperandBundles() const { 580 // Implementation note: this is a conservative implementation of operand 581 // bundle semantics, where *any* non-assume operand bundle (other than 582 // ptrauth) forces a callsite to be at least readonly. 583 return hasOperandBundlesOtherThan( 584 {LLVMContext::OB_ptrauth, LLVMContext::OB_kcfi}) && 585 getIntrinsicID() != Intrinsic::assume; 586 } 587 588 bool CallBase::hasClobberingOperandBundles() const { 589 return hasOperandBundlesOtherThan( 590 {LLVMContext::OB_deopt, LLVMContext::OB_funclet, 591 LLVMContext::OB_ptrauth, LLVMContext::OB_kcfi}) && 592 getIntrinsicID() != Intrinsic::assume; 593 } 594 595 MemoryEffects CallBase::getMemoryEffects() const { 596 MemoryEffects ME = getAttributes().getMemoryEffects(); 597 if (auto *Fn = dyn_cast<Function>(getCalledOperand())) { 598 MemoryEffects FnME = Fn->getMemoryEffects(); 599 if (hasOperandBundles()) { 600 // TODO: Add a method to get memory effects for operand bundles instead. 601 if (hasReadingOperandBundles()) 602 FnME |= MemoryEffects::readOnly(); 603 if (hasClobberingOperandBundles()) 604 FnME |= MemoryEffects::writeOnly(); 605 } 606 ME &= FnME; 607 } 608 return ME; 609 } 610 void CallBase::setMemoryEffects(MemoryEffects ME) { 611 addFnAttr(Attribute::getWithMemoryEffects(getContext(), ME)); 612 } 613 614 /// Determine if the function does not access memory. 615 bool CallBase::doesNotAccessMemory() const { 616 return getMemoryEffects().doesNotAccessMemory(); 617 } 618 void CallBase::setDoesNotAccessMemory() { 619 setMemoryEffects(MemoryEffects::none()); 620 } 621 622 /// Determine if the function does not access or only reads memory. 623 bool CallBase::onlyReadsMemory() const { 624 return getMemoryEffects().onlyReadsMemory(); 625 } 626 void CallBase::setOnlyReadsMemory() { 627 setMemoryEffects(getMemoryEffects() & MemoryEffects::readOnly()); 628 } 629 630 /// Determine if the function does not access or only writes memory. 631 bool CallBase::onlyWritesMemory() const { 632 return getMemoryEffects().onlyWritesMemory(); 633 } 634 void CallBase::setOnlyWritesMemory() { 635 setMemoryEffects(getMemoryEffects() & MemoryEffects::writeOnly()); 636 } 637 638 /// Determine if the call can access memmory only using pointers based 639 /// on its arguments. 640 bool CallBase::onlyAccessesArgMemory() const { 641 return getMemoryEffects().onlyAccessesArgPointees(); 642 } 643 void CallBase::setOnlyAccessesArgMemory() { 644 setMemoryEffects(getMemoryEffects() & MemoryEffects::argMemOnly()); 645 } 646 647 /// Determine if the function may only access memory that is 648 /// inaccessible from the IR. 649 bool CallBase::onlyAccessesInaccessibleMemory() const { 650 return getMemoryEffects().onlyAccessesInaccessibleMem(); 651 } 652 void CallBase::setOnlyAccessesInaccessibleMemory() { 653 setMemoryEffects(getMemoryEffects() & MemoryEffects::inaccessibleMemOnly()); 654 } 655 656 /// Determine if the function may only access memory that is 657 /// either inaccessible from the IR or pointed to by its arguments. 658 bool CallBase::onlyAccessesInaccessibleMemOrArgMem() const { 659 return getMemoryEffects().onlyAccessesInaccessibleOrArgMem(); 660 } 661 void CallBase::setOnlyAccessesInaccessibleMemOrArgMem() { 662 setMemoryEffects(getMemoryEffects() & 663 MemoryEffects::inaccessibleOrArgMemOnly()); 664 } 665 666 //===----------------------------------------------------------------------===// 667 // CallInst Implementation 668 //===----------------------------------------------------------------------===// 669 670 void CallInst::init(FunctionType *FTy, Value *Func, ArrayRef<Value *> Args, 671 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr) { 672 this->FTy = FTy; 673 assert(getNumOperands() == Args.size() + CountBundleInputs(Bundles) + 1 && 674 "NumOperands not set up?"); 675 676 #ifndef NDEBUG 677 assert((Args.size() == FTy->getNumParams() || 678 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) && 679 "Calling a function with bad signature!"); 680 681 for (unsigned i = 0; i != Args.size(); ++i) 682 assert((i >= FTy->getNumParams() || 683 FTy->getParamType(i) == Args[i]->getType()) && 684 "Calling a function with a bad signature!"); 685 #endif 686 687 // Set operands in order of their index to match use-list-order 688 // prediction. 689 llvm::copy(Args, op_begin()); 690 setCalledOperand(Func); 691 692 auto It = populateBundleOperandInfos(Bundles, Args.size()); 693 (void)It; 694 assert(It + 1 == op_end() && "Should add up!"); 695 696 setName(NameStr); 697 } 698 699 void CallInst::init(FunctionType *FTy, Value *Func, const Twine &NameStr) { 700 this->FTy = FTy; 701 assert(getNumOperands() == 1 && "NumOperands not set up?"); 702 setCalledOperand(Func); 703 704 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature"); 705 706 setName(NameStr); 707 } 708 709 CallInst::CallInst(FunctionType *Ty, Value *Func, const Twine &Name, 710 InsertPosition InsertBefore) 711 : CallBase(Ty->getReturnType(), Instruction::Call, 712 OperandTraits<CallBase>::op_end(this) - 1, 1, InsertBefore) { 713 init(Ty, Func, Name); 714 } 715 716 CallInst::CallInst(const CallInst &CI) 717 : CallBase(CI.Attrs, CI.FTy, CI.getType(), Instruction::Call, 718 OperandTraits<CallBase>::op_end(this) - CI.getNumOperands(), 719 CI.getNumOperands()) { 720 setTailCallKind(CI.getTailCallKind()); 721 setCallingConv(CI.getCallingConv()); 722 723 std::copy(CI.op_begin(), CI.op_end(), op_begin()); 724 std::copy(CI.bundle_op_info_begin(), CI.bundle_op_info_end(), 725 bundle_op_info_begin()); 726 SubclassOptionalData = CI.SubclassOptionalData; 727 } 728 729 CallInst *CallInst::Create(CallInst *CI, ArrayRef<OperandBundleDef> OpB, 730 InsertPosition InsertPt) { 731 std::vector<Value *> Args(CI->arg_begin(), CI->arg_end()); 732 733 auto *NewCI = CallInst::Create(CI->getFunctionType(), CI->getCalledOperand(), 734 Args, OpB, CI->getName(), InsertPt); 735 NewCI->setTailCallKind(CI->getTailCallKind()); 736 NewCI->setCallingConv(CI->getCallingConv()); 737 NewCI->SubclassOptionalData = CI->SubclassOptionalData; 738 NewCI->setAttributes(CI->getAttributes()); 739 NewCI->setDebugLoc(CI->getDebugLoc()); 740 return NewCI; 741 } 742 743 // Update profile weight for call instruction by scaling it using the ratio 744 // of S/T. The meaning of "branch_weights" meta data for call instruction is 745 // transfered to represent call count. 746 void CallInst::updateProfWeight(uint64_t S, uint64_t T) { 747 if (T == 0) { 748 LLVM_DEBUG(dbgs() << "Attempting to update profile weights will result in " 749 "div by 0. Ignoring. Likely the function " 750 << getParent()->getParent()->getName() 751 << " has 0 entry count, and contains call instructions " 752 "with non-zero prof info."); 753 return; 754 } 755 scaleProfData(*this, S, T); 756 } 757 758 //===----------------------------------------------------------------------===// 759 // InvokeInst Implementation 760 //===----------------------------------------------------------------------===// 761 762 void InvokeInst::init(FunctionType *FTy, Value *Fn, BasicBlock *IfNormal, 763 BasicBlock *IfException, ArrayRef<Value *> Args, 764 ArrayRef<OperandBundleDef> Bundles, 765 const Twine &NameStr) { 766 this->FTy = FTy; 767 768 assert((int)getNumOperands() == 769 ComputeNumOperands(Args.size(), CountBundleInputs(Bundles)) && 770 "NumOperands not set up?"); 771 772 #ifndef NDEBUG 773 assert(((Args.size() == FTy->getNumParams()) || 774 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) && 775 "Invoking a function with bad signature"); 776 777 for (unsigned i = 0, e = Args.size(); i != e; i++) 778 assert((i >= FTy->getNumParams() || 779 FTy->getParamType(i) == Args[i]->getType()) && 780 "Invoking a function with a bad signature!"); 781 #endif 782 783 // Set operands in order of their index to match use-list-order 784 // prediction. 785 llvm::copy(Args, op_begin()); 786 setNormalDest(IfNormal); 787 setUnwindDest(IfException); 788 setCalledOperand(Fn); 789 790 auto It = populateBundleOperandInfos(Bundles, Args.size()); 791 (void)It; 792 assert(It + 3 == op_end() && "Should add up!"); 793 794 setName(NameStr); 795 } 796 797 InvokeInst::InvokeInst(const InvokeInst &II) 798 : CallBase(II.Attrs, II.FTy, II.getType(), Instruction::Invoke, 799 OperandTraits<CallBase>::op_end(this) - II.getNumOperands(), 800 II.getNumOperands()) { 801 setCallingConv(II.getCallingConv()); 802 std::copy(II.op_begin(), II.op_end(), op_begin()); 803 std::copy(II.bundle_op_info_begin(), II.bundle_op_info_end(), 804 bundle_op_info_begin()); 805 SubclassOptionalData = II.SubclassOptionalData; 806 } 807 808 InvokeInst *InvokeInst::Create(InvokeInst *II, ArrayRef<OperandBundleDef> OpB, 809 InsertPosition InsertPt) { 810 std::vector<Value *> Args(II->arg_begin(), II->arg_end()); 811 812 auto *NewII = InvokeInst::Create( 813 II->getFunctionType(), II->getCalledOperand(), II->getNormalDest(), 814 II->getUnwindDest(), Args, OpB, II->getName(), InsertPt); 815 NewII->setCallingConv(II->getCallingConv()); 816 NewII->SubclassOptionalData = II->SubclassOptionalData; 817 NewII->setAttributes(II->getAttributes()); 818 NewII->setDebugLoc(II->getDebugLoc()); 819 return NewII; 820 } 821 822 LandingPadInst *InvokeInst::getLandingPadInst() const { 823 return cast<LandingPadInst>(getUnwindDest()->getFirstNonPHI()); 824 } 825 826 void InvokeInst::updateProfWeight(uint64_t S, uint64_t T) { 827 if (T == 0) { 828 LLVM_DEBUG(dbgs() << "Attempting to update profile weights will result in " 829 "div by 0. Ignoring. Likely the function " 830 << getParent()->getParent()->getName() 831 << " has 0 entry count, and contains call instructions " 832 "with non-zero prof info."); 833 return; 834 } 835 scaleProfData(*this, S, T); 836 } 837 838 //===----------------------------------------------------------------------===// 839 // CallBrInst Implementation 840 //===----------------------------------------------------------------------===// 841 842 void CallBrInst::init(FunctionType *FTy, Value *Fn, BasicBlock *Fallthrough, 843 ArrayRef<BasicBlock *> IndirectDests, 844 ArrayRef<Value *> Args, 845 ArrayRef<OperandBundleDef> Bundles, 846 const Twine &NameStr) { 847 this->FTy = FTy; 848 849 assert((int)getNumOperands() == 850 ComputeNumOperands(Args.size(), IndirectDests.size(), 851 CountBundleInputs(Bundles)) && 852 "NumOperands not set up?"); 853 854 #ifndef NDEBUG 855 assert(((Args.size() == FTy->getNumParams()) || 856 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) && 857 "Calling a function with bad signature"); 858 859 for (unsigned i = 0, e = Args.size(); i != e; i++) 860 assert((i >= FTy->getNumParams() || 861 FTy->getParamType(i) == Args[i]->getType()) && 862 "Calling a function with a bad signature!"); 863 #endif 864 865 // Set operands in order of their index to match use-list-order 866 // prediction. 867 std::copy(Args.begin(), Args.end(), op_begin()); 868 NumIndirectDests = IndirectDests.size(); 869 setDefaultDest(Fallthrough); 870 for (unsigned i = 0; i != NumIndirectDests; ++i) 871 setIndirectDest(i, IndirectDests[i]); 872 setCalledOperand(Fn); 873 874 auto It = populateBundleOperandInfos(Bundles, Args.size()); 875 (void)It; 876 assert(It + 2 + IndirectDests.size() == op_end() && "Should add up!"); 877 878 setName(NameStr); 879 } 880 881 CallBrInst::CallBrInst(const CallBrInst &CBI) 882 : CallBase(CBI.Attrs, CBI.FTy, CBI.getType(), Instruction::CallBr, 883 OperandTraits<CallBase>::op_end(this) - CBI.getNumOperands(), 884 CBI.getNumOperands()) { 885 setCallingConv(CBI.getCallingConv()); 886 std::copy(CBI.op_begin(), CBI.op_end(), op_begin()); 887 std::copy(CBI.bundle_op_info_begin(), CBI.bundle_op_info_end(), 888 bundle_op_info_begin()); 889 SubclassOptionalData = CBI.SubclassOptionalData; 890 NumIndirectDests = CBI.NumIndirectDests; 891 } 892 893 CallBrInst *CallBrInst::Create(CallBrInst *CBI, ArrayRef<OperandBundleDef> OpB, 894 InsertPosition InsertPt) { 895 std::vector<Value *> Args(CBI->arg_begin(), CBI->arg_end()); 896 897 auto *NewCBI = CallBrInst::Create( 898 CBI->getFunctionType(), CBI->getCalledOperand(), CBI->getDefaultDest(), 899 CBI->getIndirectDests(), Args, OpB, CBI->getName(), InsertPt); 900 NewCBI->setCallingConv(CBI->getCallingConv()); 901 NewCBI->SubclassOptionalData = CBI->SubclassOptionalData; 902 NewCBI->setAttributes(CBI->getAttributes()); 903 NewCBI->setDebugLoc(CBI->getDebugLoc()); 904 NewCBI->NumIndirectDests = CBI->NumIndirectDests; 905 return NewCBI; 906 } 907 908 //===----------------------------------------------------------------------===// 909 // ReturnInst Implementation 910 //===----------------------------------------------------------------------===// 911 912 ReturnInst::ReturnInst(const ReturnInst &RI) 913 : Instruction(Type::getVoidTy(RI.getContext()), Instruction::Ret, 914 OperandTraits<ReturnInst>::op_end(this) - RI.getNumOperands(), 915 RI.getNumOperands()) { 916 if (RI.getNumOperands()) 917 Op<0>() = RI.Op<0>(); 918 SubclassOptionalData = RI.SubclassOptionalData; 919 } 920 921 ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, 922 InsertPosition InsertBefore) 923 : Instruction(Type::getVoidTy(C), Instruction::Ret, 924 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal, 925 InsertBefore) { 926 if (retVal) 927 Op<0>() = retVal; 928 } 929 930 //===----------------------------------------------------------------------===// 931 // ResumeInst Implementation 932 //===----------------------------------------------------------------------===// 933 934 ResumeInst::ResumeInst(const ResumeInst &RI) 935 : Instruction(Type::getVoidTy(RI.getContext()), Instruction::Resume, 936 OperandTraits<ResumeInst>::op_begin(this), 1) { 937 Op<0>() = RI.Op<0>(); 938 } 939 940 ResumeInst::ResumeInst(Value *Exn, InsertPosition InsertBefore) 941 : Instruction(Type::getVoidTy(Exn->getContext()), Instruction::Resume, 942 OperandTraits<ResumeInst>::op_begin(this), 1, InsertBefore) { 943 Op<0>() = Exn; 944 } 945 946 //===----------------------------------------------------------------------===// 947 // CleanupReturnInst Implementation 948 //===----------------------------------------------------------------------===// 949 950 CleanupReturnInst::CleanupReturnInst(const CleanupReturnInst &CRI) 951 : Instruction(CRI.getType(), Instruction::CleanupRet, 952 OperandTraits<CleanupReturnInst>::op_end(this) - 953 CRI.getNumOperands(), 954 CRI.getNumOperands()) { 955 setSubclassData<Instruction::OpaqueField>( 956 CRI.getSubclassData<Instruction::OpaqueField>()); 957 Op<0>() = CRI.Op<0>(); 958 if (CRI.hasUnwindDest()) 959 Op<1>() = CRI.Op<1>(); 960 } 961 962 void CleanupReturnInst::init(Value *CleanupPad, BasicBlock *UnwindBB) { 963 if (UnwindBB) 964 setSubclassData<UnwindDestField>(true); 965 966 Op<0>() = CleanupPad; 967 if (UnwindBB) 968 Op<1>() = UnwindBB; 969 } 970 971 CleanupReturnInst::CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB, 972 unsigned Values, 973 InsertPosition InsertBefore) 974 : Instruction(Type::getVoidTy(CleanupPad->getContext()), 975 Instruction::CleanupRet, 976 OperandTraits<CleanupReturnInst>::op_end(this) - Values, 977 Values, InsertBefore) { 978 init(CleanupPad, UnwindBB); 979 } 980 981 //===----------------------------------------------------------------------===// 982 // CatchReturnInst Implementation 983 //===----------------------------------------------------------------------===// 984 void CatchReturnInst::init(Value *CatchPad, BasicBlock *BB) { 985 Op<0>() = CatchPad; 986 Op<1>() = BB; 987 } 988 989 CatchReturnInst::CatchReturnInst(const CatchReturnInst &CRI) 990 : Instruction(Type::getVoidTy(CRI.getContext()), Instruction::CatchRet, 991 OperandTraits<CatchReturnInst>::op_begin(this), 2) { 992 Op<0>() = CRI.Op<0>(); 993 Op<1>() = CRI.Op<1>(); 994 } 995 996 CatchReturnInst::CatchReturnInst(Value *CatchPad, BasicBlock *BB, 997 InsertPosition InsertBefore) 998 : Instruction(Type::getVoidTy(BB->getContext()), Instruction::CatchRet, 999 OperandTraits<CatchReturnInst>::op_begin(this), 2, 1000 InsertBefore) { 1001 init(CatchPad, BB); 1002 } 1003 1004 //===----------------------------------------------------------------------===// 1005 // CatchSwitchInst Implementation 1006 //===----------------------------------------------------------------------===// 1007 1008 CatchSwitchInst::CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest, 1009 unsigned NumReservedValues, 1010 const Twine &NameStr, 1011 InsertPosition InsertBefore) 1012 : Instruction(ParentPad->getType(), Instruction::CatchSwitch, nullptr, 0, 1013 InsertBefore) { 1014 if (UnwindDest) 1015 ++NumReservedValues; 1016 init(ParentPad, UnwindDest, NumReservedValues + 1); 1017 setName(NameStr); 1018 } 1019 1020 CatchSwitchInst::CatchSwitchInst(const CatchSwitchInst &CSI) 1021 : Instruction(CSI.getType(), Instruction::CatchSwitch, nullptr, 1022 CSI.getNumOperands()) { 1023 init(CSI.getParentPad(), CSI.getUnwindDest(), CSI.getNumOperands()); 1024 setNumHungOffUseOperands(ReservedSpace); 1025 Use *OL = getOperandList(); 1026 const Use *InOL = CSI.getOperandList(); 1027 for (unsigned I = 1, E = ReservedSpace; I != E; ++I) 1028 OL[I] = InOL[I]; 1029 } 1030 1031 void CatchSwitchInst::init(Value *ParentPad, BasicBlock *UnwindDest, 1032 unsigned NumReservedValues) { 1033 assert(ParentPad && NumReservedValues); 1034 1035 ReservedSpace = NumReservedValues; 1036 setNumHungOffUseOperands(UnwindDest ? 2 : 1); 1037 allocHungoffUses(ReservedSpace); 1038 1039 Op<0>() = ParentPad; 1040 if (UnwindDest) { 1041 setSubclassData<UnwindDestField>(true); 1042 setUnwindDest(UnwindDest); 1043 } 1044 } 1045 1046 /// growOperands - grow operands - This grows the operand list in response to a 1047 /// push_back style of operation. This grows the number of ops by 2 times. 1048 void CatchSwitchInst::growOperands(unsigned Size) { 1049 unsigned NumOperands = getNumOperands(); 1050 assert(NumOperands >= 1); 1051 if (ReservedSpace >= NumOperands + Size) 1052 return; 1053 ReservedSpace = (NumOperands + Size / 2) * 2; 1054 growHungoffUses(ReservedSpace); 1055 } 1056 1057 void CatchSwitchInst::addHandler(BasicBlock *Handler) { 1058 unsigned OpNo = getNumOperands(); 1059 growOperands(1); 1060 assert(OpNo < ReservedSpace && "Growing didn't work!"); 1061 setNumHungOffUseOperands(getNumOperands() + 1); 1062 getOperandList()[OpNo] = Handler; 1063 } 1064 1065 void CatchSwitchInst::removeHandler(handler_iterator HI) { 1066 // Move all subsequent handlers up one. 1067 Use *EndDst = op_end() - 1; 1068 for (Use *CurDst = HI.getCurrent(); CurDst != EndDst; ++CurDst) 1069 *CurDst = *(CurDst + 1); 1070 // Null out the last handler use. 1071 *EndDst = nullptr; 1072 1073 setNumHungOffUseOperands(getNumOperands() - 1); 1074 } 1075 1076 //===----------------------------------------------------------------------===// 1077 // FuncletPadInst Implementation 1078 //===----------------------------------------------------------------------===// 1079 void FuncletPadInst::init(Value *ParentPad, ArrayRef<Value *> Args, 1080 const Twine &NameStr) { 1081 assert(getNumOperands() == 1 + Args.size() && "NumOperands not set up?"); 1082 llvm::copy(Args, op_begin()); 1083 setParentPad(ParentPad); 1084 setName(NameStr); 1085 } 1086 1087 FuncletPadInst::FuncletPadInst(const FuncletPadInst &FPI) 1088 : Instruction(FPI.getType(), FPI.getOpcode(), 1089 OperandTraits<FuncletPadInst>::op_end(this) - 1090 FPI.getNumOperands(), 1091 FPI.getNumOperands()) { 1092 std::copy(FPI.op_begin(), FPI.op_end(), op_begin()); 1093 setParentPad(FPI.getParentPad()); 1094 } 1095 1096 FuncletPadInst::FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad, 1097 ArrayRef<Value *> Args, unsigned Values, 1098 const Twine &NameStr, 1099 InsertPosition InsertBefore) 1100 : Instruction(ParentPad->getType(), Op, 1101 OperandTraits<FuncletPadInst>::op_end(this) - Values, Values, 1102 InsertBefore) { 1103 init(ParentPad, Args, NameStr); 1104 } 1105 1106 //===----------------------------------------------------------------------===// 1107 // UnreachableInst Implementation 1108 //===----------------------------------------------------------------------===// 1109 1110 UnreachableInst::UnreachableInst(LLVMContext &Context, 1111 InsertPosition InsertBefore) 1112 : Instruction(Type::getVoidTy(Context), Instruction::Unreachable, nullptr, 1113 0, InsertBefore) {} 1114 1115 //===----------------------------------------------------------------------===// 1116 // BranchInst Implementation 1117 //===----------------------------------------------------------------------===// 1118 1119 void BranchInst::AssertOK() { 1120 if (isConditional()) 1121 assert(getCondition()->getType()->isIntegerTy(1) && 1122 "May only branch on boolean predicates!"); 1123 } 1124 1125 BranchInst::BranchInst(BasicBlock *IfTrue, InsertPosition InsertBefore) 1126 : Instruction(Type::getVoidTy(IfTrue->getContext()), Instruction::Br, 1127 OperandTraits<BranchInst>::op_end(this) - 1, 1, 1128 InsertBefore) { 1129 assert(IfTrue && "Branch destination may not be null!"); 1130 Op<-1>() = IfTrue; 1131 } 1132 1133 BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond, 1134 InsertPosition InsertBefore) 1135 : Instruction(Type::getVoidTy(IfTrue->getContext()), Instruction::Br, 1136 OperandTraits<BranchInst>::op_end(this) - 3, 3, 1137 InsertBefore) { 1138 // Assign in order of operand index to make use-list order predictable. 1139 Op<-3>() = Cond; 1140 Op<-2>() = IfFalse; 1141 Op<-1>() = IfTrue; 1142 #ifndef NDEBUG 1143 AssertOK(); 1144 #endif 1145 } 1146 1147 BranchInst::BranchInst(const BranchInst &BI) 1148 : Instruction(Type::getVoidTy(BI.getContext()), Instruction::Br, 1149 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(), 1150 BI.getNumOperands()) { 1151 // Assign in order of operand index to make use-list order predictable. 1152 if (BI.getNumOperands() != 1) { 1153 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!"); 1154 Op<-3>() = BI.Op<-3>(); 1155 Op<-2>() = BI.Op<-2>(); 1156 } 1157 Op<-1>() = BI.Op<-1>(); 1158 SubclassOptionalData = BI.SubclassOptionalData; 1159 } 1160 1161 void BranchInst::swapSuccessors() { 1162 assert(isConditional() && 1163 "Cannot swap successors of an unconditional branch"); 1164 Op<-1>().swap(Op<-2>()); 1165 1166 // Update profile metadata if present and it matches our structural 1167 // expectations. 1168 swapProfMetadata(); 1169 } 1170 1171 //===----------------------------------------------------------------------===// 1172 // AllocaInst Implementation 1173 //===----------------------------------------------------------------------===// 1174 1175 static Value *getAISize(LLVMContext &Context, Value *Amt) { 1176 if (!Amt) 1177 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1); 1178 else { 1179 assert(!isa<BasicBlock>(Amt) && 1180 "Passed basic block into allocation size parameter! Use other ctor"); 1181 assert(Amt->getType()->isIntegerTy() && 1182 "Allocation array size is not an integer!"); 1183 } 1184 return Amt; 1185 } 1186 1187 static Align computeAllocaDefaultAlign(Type *Ty, InsertPosition Pos) { 1188 assert(Pos.isValid() && 1189 "Insertion position cannot be null when alignment not provided!"); 1190 BasicBlock *BB = Pos.getBasicBlock(); 1191 assert(BB->getParent() && 1192 "BB must be in a Function when alignment not provided!"); 1193 const DataLayout &DL = BB->getModule()->getDataLayout(); 1194 return DL.getPrefTypeAlign(Ty); 1195 } 1196 1197 AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, const Twine &Name, 1198 InsertPosition InsertBefore) 1199 : AllocaInst(Ty, AddrSpace, /*ArraySize=*/nullptr, Name, InsertBefore) {} 1200 1201 AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize, 1202 const Twine &Name, InsertPosition InsertBefore) 1203 : AllocaInst(Ty, AddrSpace, ArraySize, 1204 computeAllocaDefaultAlign(Ty, InsertBefore), Name, 1205 InsertBefore) {} 1206 1207 AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize, 1208 Align Align, const Twine &Name, 1209 InsertPosition InsertBefore) 1210 : UnaryInstruction(PointerType::get(Ty, AddrSpace), Alloca, 1211 getAISize(Ty->getContext(), ArraySize), InsertBefore), 1212 AllocatedType(Ty) { 1213 setAlignment(Align); 1214 assert(!Ty->isVoidTy() && "Cannot allocate void!"); 1215 setName(Name); 1216 } 1217 1218 bool AllocaInst::isArrayAllocation() const { 1219 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0))) 1220 return !CI->isOne(); 1221 return true; 1222 } 1223 1224 /// isStaticAlloca - Return true if this alloca is in the entry block of the 1225 /// function and is a constant size. If so, the code generator will fold it 1226 /// into the prolog/epilog code, so it is basically free. 1227 bool AllocaInst::isStaticAlloca() const { 1228 // Must be constant size. 1229 if (!isa<ConstantInt>(getArraySize())) return false; 1230 1231 // Must be in the entry block. 1232 const BasicBlock *Parent = getParent(); 1233 return Parent->isEntryBlock() && !isUsedWithInAlloca(); 1234 } 1235 1236 //===----------------------------------------------------------------------===// 1237 // LoadInst Implementation 1238 //===----------------------------------------------------------------------===// 1239 1240 void LoadInst::AssertOK() { 1241 assert(getOperand(0)->getType()->isPointerTy() && 1242 "Ptr must have pointer type."); 1243 } 1244 1245 static Align computeLoadStoreDefaultAlign(Type *Ty, InsertPosition Pos) { 1246 assert(Pos.isValid() && 1247 "Insertion position cannot be null when alignment not provided!"); 1248 BasicBlock *BB = Pos.getBasicBlock(); 1249 assert(BB->getParent() && 1250 "BB must be in a Function when alignment not provided!"); 1251 const DataLayout &DL = BB->getModule()->getDataLayout(); 1252 return DL.getABITypeAlign(Ty); 1253 } 1254 1255 LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, 1256 InsertPosition InsertBef) 1257 : LoadInst(Ty, Ptr, Name, /*isVolatile=*/false, InsertBef) {} 1258 1259 LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile, 1260 InsertPosition InsertBef) 1261 : LoadInst(Ty, Ptr, Name, isVolatile, 1262 computeLoadStoreDefaultAlign(Ty, InsertBef), InsertBef) {} 1263 1264 LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile, 1265 Align Align, InsertPosition InsertBef) 1266 : LoadInst(Ty, Ptr, Name, isVolatile, Align, AtomicOrdering::NotAtomic, 1267 SyncScope::System, InsertBef) {} 1268 1269 LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile, 1270 Align Align, AtomicOrdering Order, SyncScope::ID SSID, 1271 InsertPosition InsertBef) 1272 : UnaryInstruction(Ty, Load, Ptr, InsertBef) { 1273 setVolatile(isVolatile); 1274 setAlignment(Align); 1275 setAtomic(Order, SSID); 1276 AssertOK(); 1277 setName(Name); 1278 } 1279 1280 //===----------------------------------------------------------------------===// 1281 // StoreInst Implementation 1282 //===----------------------------------------------------------------------===// 1283 1284 void StoreInst::AssertOK() { 1285 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!"); 1286 assert(getOperand(1)->getType()->isPointerTy() && 1287 "Ptr must have pointer type!"); 1288 } 1289 1290 StoreInst::StoreInst(Value *val, Value *addr, InsertPosition InsertBefore) 1291 : StoreInst(val, addr, /*isVolatile=*/false, InsertBefore) {} 1292 1293 StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, 1294 InsertPosition InsertBefore) 1295 : StoreInst(val, addr, isVolatile, 1296 computeLoadStoreDefaultAlign(val->getType(), InsertBefore), 1297 InsertBefore) {} 1298 1299 StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, Align Align, 1300 InsertPosition InsertBefore) 1301 : StoreInst(val, addr, isVolatile, Align, AtomicOrdering::NotAtomic, 1302 SyncScope::System, InsertBefore) {} 1303 1304 StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, Align Align, 1305 AtomicOrdering Order, SyncScope::ID SSID, 1306 InsertPosition InsertBefore) 1307 : Instruction(Type::getVoidTy(val->getContext()), Store, 1308 OperandTraits<StoreInst>::op_begin(this), 1309 OperandTraits<StoreInst>::operands(this), InsertBefore) { 1310 Op<0>() = val; 1311 Op<1>() = addr; 1312 setVolatile(isVolatile); 1313 setAlignment(Align); 1314 setAtomic(Order, SSID); 1315 AssertOK(); 1316 } 1317 1318 //===----------------------------------------------------------------------===// 1319 // AtomicCmpXchgInst Implementation 1320 //===----------------------------------------------------------------------===// 1321 1322 void AtomicCmpXchgInst::Init(Value *Ptr, Value *Cmp, Value *NewVal, 1323 Align Alignment, AtomicOrdering SuccessOrdering, 1324 AtomicOrdering FailureOrdering, 1325 SyncScope::ID SSID) { 1326 Op<0>() = Ptr; 1327 Op<1>() = Cmp; 1328 Op<2>() = NewVal; 1329 setSuccessOrdering(SuccessOrdering); 1330 setFailureOrdering(FailureOrdering); 1331 setSyncScopeID(SSID); 1332 setAlignment(Alignment); 1333 1334 assert(getOperand(0) && getOperand(1) && getOperand(2) && 1335 "All operands must be non-null!"); 1336 assert(getOperand(0)->getType()->isPointerTy() && 1337 "Ptr must have pointer type!"); 1338 assert(getOperand(1)->getType() == getOperand(2)->getType() && 1339 "Cmp type and NewVal type must be same!"); 1340 } 1341 1342 AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal, 1343 Align Alignment, 1344 AtomicOrdering SuccessOrdering, 1345 AtomicOrdering FailureOrdering, 1346 SyncScope::ID SSID, 1347 InsertPosition InsertBefore) 1348 : Instruction( 1349 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext())), 1350 AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this), 1351 OperandTraits<AtomicCmpXchgInst>::operands(this), InsertBefore) { 1352 Init(Ptr, Cmp, NewVal, Alignment, SuccessOrdering, FailureOrdering, SSID); 1353 } 1354 1355 //===----------------------------------------------------------------------===// 1356 // AtomicRMWInst Implementation 1357 //===----------------------------------------------------------------------===// 1358 1359 void AtomicRMWInst::Init(BinOp Operation, Value *Ptr, Value *Val, 1360 Align Alignment, AtomicOrdering Ordering, 1361 SyncScope::ID SSID) { 1362 assert(Ordering != AtomicOrdering::NotAtomic && 1363 "atomicrmw instructions can only be atomic."); 1364 assert(Ordering != AtomicOrdering::Unordered && 1365 "atomicrmw instructions cannot be unordered."); 1366 Op<0>() = Ptr; 1367 Op<1>() = Val; 1368 setOperation(Operation); 1369 setOrdering(Ordering); 1370 setSyncScopeID(SSID); 1371 setAlignment(Alignment); 1372 1373 assert(getOperand(0) && getOperand(1) && "All operands must be non-null!"); 1374 assert(getOperand(0)->getType()->isPointerTy() && 1375 "Ptr must have pointer type!"); 1376 assert(Ordering != AtomicOrdering::NotAtomic && 1377 "AtomicRMW instructions must be atomic!"); 1378 } 1379 1380 AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val, 1381 Align Alignment, AtomicOrdering Ordering, 1382 SyncScope::ID SSID, InsertPosition InsertBefore) 1383 : Instruction(Val->getType(), AtomicRMW, 1384 OperandTraits<AtomicRMWInst>::op_begin(this), 1385 OperandTraits<AtomicRMWInst>::operands(this), InsertBefore) { 1386 Init(Operation, Ptr, Val, Alignment, Ordering, SSID); 1387 } 1388 1389 StringRef AtomicRMWInst::getOperationName(BinOp Op) { 1390 switch (Op) { 1391 case AtomicRMWInst::Xchg: 1392 return "xchg"; 1393 case AtomicRMWInst::Add: 1394 return "add"; 1395 case AtomicRMWInst::Sub: 1396 return "sub"; 1397 case AtomicRMWInst::And: 1398 return "and"; 1399 case AtomicRMWInst::Nand: 1400 return "nand"; 1401 case AtomicRMWInst::Or: 1402 return "or"; 1403 case AtomicRMWInst::Xor: 1404 return "xor"; 1405 case AtomicRMWInst::Max: 1406 return "max"; 1407 case AtomicRMWInst::Min: 1408 return "min"; 1409 case AtomicRMWInst::UMax: 1410 return "umax"; 1411 case AtomicRMWInst::UMin: 1412 return "umin"; 1413 case AtomicRMWInst::FAdd: 1414 return "fadd"; 1415 case AtomicRMWInst::FSub: 1416 return "fsub"; 1417 case AtomicRMWInst::FMax: 1418 return "fmax"; 1419 case AtomicRMWInst::FMin: 1420 return "fmin"; 1421 case AtomicRMWInst::UIncWrap: 1422 return "uinc_wrap"; 1423 case AtomicRMWInst::UDecWrap: 1424 return "udec_wrap"; 1425 case AtomicRMWInst::BAD_BINOP: 1426 return "<invalid operation>"; 1427 } 1428 1429 llvm_unreachable("invalid atomicrmw operation"); 1430 } 1431 1432 //===----------------------------------------------------------------------===// 1433 // FenceInst Implementation 1434 //===----------------------------------------------------------------------===// 1435 1436 FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering, 1437 SyncScope::ID SSID, InsertPosition InsertBefore) 1438 : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertBefore) { 1439 setOrdering(Ordering); 1440 setSyncScopeID(SSID); 1441 } 1442 1443 //===----------------------------------------------------------------------===// 1444 // GetElementPtrInst Implementation 1445 //===----------------------------------------------------------------------===// 1446 1447 void GetElementPtrInst::init(Value *Ptr, ArrayRef<Value *> IdxList, 1448 const Twine &Name) { 1449 assert(getNumOperands() == 1 + IdxList.size() && 1450 "NumOperands not initialized?"); 1451 Op<0>() = Ptr; 1452 llvm::copy(IdxList, op_begin() + 1); 1453 setName(Name); 1454 } 1455 1456 GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI) 1457 : Instruction(GEPI.getType(), GetElementPtr, 1458 OperandTraits<GetElementPtrInst>::op_end(this) - 1459 GEPI.getNumOperands(), 1460 GEPI.getNumOperands()), 1461 SourceElementType(GEPI.SourceElementType), 1462 ResultElementType(GEPI.ResultElementType) { 1463 std::copy(GEPI.op_begin(), GEPI.op_end(), op_begin()); 1464 SubclassOptionalData = GEPI.SubclassOptionalData; 1465 } 1466 1467 Type *GetElementPtrInst::getTypeAtIndex(Type *Ty, Value *Idx) { 1468 if (auto *Struct = dyn_cast<StructType>(Ty)) { 1469 if (!Struct->indexValid(Idx)) 1470 return nullptr; 1471 return Struct->getTypeAtIndex(Idx); 1472 } 1473 if (!Idx->getType()->isIntOrIntVectorTy()) 1474 return nullptr; 1475 if (auto *Array = dyn_cast<ArrayType>(Ty)) 1476 return Array->getElementType(); 1477 if (auto *Vector = dyn_cast<VectorType>(Ty)) 1478 return Vector->getElementType(); 1479 return nullptr; 1480 } 1481 1482 Type *GetElementPtrInst::getTypeAtIndex(Type *Ty, uint64_t Idx) { 1483 if (auto *Struct = dyn_cast<StructType>(Ty)) { 1484 if (Idx >= Struct->getNumElements()) 1485 return nullptr; 1486 return Struct->getElementType(Idx); 1487 } 1488 if (auto *Array = dyn_cast<ArrayType>(Ty)) 1489 return Array->getElementType(); 1490 if (auto *Vector = dyn_cast<VectorType>(Ty)) 1491 return Vector->getElementType(); 1492 return nullptr; 1493 } 1494 1495 template <typename IndexTy> 1496 static Type *getIndexedTypeInternal(Type *Ty, ArrayRef<IndexTy> IdxList) { 1497 if (IdxList.empty()) 1498 return Ty; 1499 for (IndexTy V : IdxList.slice(1)) { 1500 Ty = GetElementPtrInst::getTypeAtIndex(Ty, V); 1501 if (!Ty) 1502 return Ty; 1503 } 1504 return Ty; 1505 } 1506 1507 Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<Value *> IdxList) { 1508 return getIndexedTypeInternal(Ty, IdxList); 1509 } 1510 1511 Type *GetElementPtrInst::getIndexedType(Type *Ty, 1512 ArrayRef<Constant *> IdxList) { 1513 return getIndexedTypeInternal(Ty, IdxList); 1514 } 1515 1516 Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<uint64_t> IdxList) { 1517 return getIndexedTypeInternal(Ty, IdxList); 1518 } 1519 1520 /// hasAllZeroIndices - Return true if all of the indices of this GEP are 1521 /// zeros. If so, the result pointer and the first operand have the same 1522 /// value, just potentially different types. 1523 bool GetElementPtrInst::hasAllZeroIndices() const { 1524 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) { 1525 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) { 1526 if (!CI->isZero()) return false; 1527 } else { 1528 return false; 1529 } 1530 } 1531 return true; 1532 } 1533 1534 /// hasAllConstantIndices - Return true if all of the indices of this GEP are 1535 /// constant integers. If so, the result pointer and the first operand have 1536 /// a constant offset between them. 1537 bool GetElementPtrInst::hasAllConstantIndices() const { 1538 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) { 1539 if (!isa<ConstantInt>(getOperand(i))) 1540 return false; 1541 } 1542 return true; 1543 } 1544 1545 void GetElementPtrInst::setNoWrapFlags(GEPNoWrapFlags NW) { 1546 SubclassOptionalData = NW.getRaw(); 1547 } 1548 1549 void GetElementPtrInst::setIsInBounds(bool B) { 1550 GEPNoWrapFlags NW = cast<GEPOperator>(this)->getNoWrapFlags(); 1551 if (B) 1552 NW |= GEPNoWrapFlags::inBounds(); 1553 else 1554 NW = NW.withoutInBounds(); 1555 setNoWrapFlags(NW); 1556 } 1557 1558 GEPNoWrapFlags GetElementPtrInst::getNoWrapFlags() const { 1559 return cast<GEPOperator>(this)->getNoWrapFlags(); 1560 } 1561 1562 bool GetElementPtrInst::isInBounds() const { 1563 return cast<GEPOperator>(this)->isInBounds(); 1564 } 1565 1566 bool GetElementPtrInst::hasNoUnsignedSignedWrap() const { 1567 return cast<GEPOperator>(this)->hasNoUnsignedSignedWrap(); 1568 } 1569 1570 bool GetElementPtrInst::hasNoUnsignedWrap() const { 1571 return cast<GEPOperator>(this)->hasNoUnsignedWrap(); 1572 } 1573 1574 bool GetElementPtrInst::accumulateConstantOffset(const DataLayout &DL, 1575 APInt &Offset) const { 1576 // Delegate to the generic GEPOperator implementation. 1577 return cast<GEPOperator>(this)->accumulateConstantOffset(DL, Offset); 1578 } 1579 1580 bool GetElementPtrInst::collectOffset( 1581 const DataLayout &DL, unsigned BitWidth, 1582 MapVector<Value *, APInt> &VariableOffsets, 1583 APInt &ConstantOffset) const { 1584 // Delegate to the generic GEPOperator implementation. 1585 return cast<GEPOperator>(this)->collectOffset(DL, BitWidth, VariableOffsets, 1586 ConstantOffset); 1587 } 1588 1589 //===----------------------------------------------------------------------===// 1590 // ExtractElementInst Implementation 1591 //===----------------------------------------------------------------------===// 1592 1593 ExtractElementInst::ExtractElementInst(Value *Val, Value *Index, 1594 const Twine &Name, 1595 InsertPosition InsertBef) 1596 : Instruction( 1597 cast<VectorType>(Val->getType())->getElementType(), ExtractElement, 1598 OperandTraits<ExtractElementInst>::op_begin(this), 2, InsertBef) { 1599 assert(isValidOperands(Val, Index) && 1600 "Invalid extractelement instruction operands!"); 1601 Op<0>() = Val; 1602 Op<1>() = Index; 1603 setName(Name); 1604 } 1605 1606 bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) { 1607 if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy()) 1608 return false; 1609 return true; 1610 } 1611 1612 //===----------------------------------------------------------------------===// 1613 // InsertElementInst Implementation 1614 //===----------------------------------------------------------------------===// 1615 1616 InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index, 1617 const Twine &Name, 1618 InsertPosition InsertBef) 1619 : Instruction(Vec->getType(), InsertElement, 1620 OperandTraits<InsertElementInst>::op_begin(this), 3, 1621 InsertBef) { 1622 assert(isValidOperands(Vec, Elt, Index) && 1623 "Invalid insertelement instruction operands!"); 1624 Op<0>() = Vec; 1625 Op<1>() = Elt; 1626 Op<2>() = Index; 1627 setName(Name); 1628 } 1629 1630 bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt, 1631 const Value *Index) { 1632 if (!Vec->getType()->isVectorTy()) 1633 return false; // First operand of insertelement must be vector type. 1634 1635 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType()) 1636 return false;// Second operand of insertelement must be vector element type. 1637 1638 if (!Index->getType()->isIntegerTy()) 1639 return false; // Third operand of insertelement must be i32. 1640 return true; 1641 } 1642 1643 //===----------------------------------------------------------------------===// 1644 // ShuffleVectorInst Implementation 1645 //===----------------------------------------------------------------------===// 1646 1647 static Value *createPlaceholderForShuffleVector(Value *V) { 1648 assert(V && "Cannot create placeholder of nullptr V"); 1649 return PoisonValue::get(V->getType()); 1650 } 1651 1652 ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *Mask, const Twine &Name, 1653 InsertPosition InsertBefore) 1654 : ShuffleVectorInst(V1, createPlaceholderForShuffleVector(V1), Mask, Name, 1655 InsertBefore) {} 1656 1657 ShuffleVectorInst::ShuffleVectorInst(Value *V1, ArrayRef<int> Mask, 1658 const Twine &Name, 1659 InsertPosition InsertBefore) 1660 : ShuffleVectorInst(V1, createPlaceholderForShuffleVector(V1), Mask, Name, 1661 InsertBefore) {} 1662 1663 ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask, 1664 const Twine &Name, 1665 InsertPosition InsertBefore) 1666 : Instruction( 1667 VectorType::get(cast<VectorType>(V1->getType())->getElementType(), 1668 cast<VectorType>(Mask->getType())->getElementCount()), 1669 ShuffleVector, OperandTraits<ShuffleVectorInst>::op_begin(this), 1670 OperandTraits<ShuffleVectorInst>::operands(this), InsertBefore) { 1671 assert(isValidOperands(V1, V2, Mask) && 1672 "Invalid shuffle vector instruction operands!"); 1673 1674 Op<0>() = V1; 1675 Op<1>() = V2; 1676 SmallVector<int, 16> MaskArr; 1677 getShuffleMask(cast<Constant>(Mask), MaskArr); 1678 setShuffleMask(MaskArr); 1679 setName(Name); 1680 } 1681 1682 ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, ArrayRef<int> Mask, 1683 const Twine &Name, 1684 InsertPosition InsertBefore) 1685 : Instruction( 1686 VectorType::get(cast<VectorType>(V1->getType())->getElementType(), 1687 Mask.size(), isa<ScalableVectorType>(V1->getType())), 1688 ShuffleVector, OperandTraits<ShuffleVectorInst>::op_begin(this), 1689 OperandTraits<ShuffleVectorInst>::operands(this), InsertBefore) { 1690 assert(isValidOperands(V1, V2, Mask) && 1691 "Invalid shuffle vector instruction operands!"); 1692 Op<0>() = V1; 1693 Op<1>() = V2; 1694 setShuffleMask(Mask); 1695 setName(Name); 1696 } 1697 1698 void ShuffleVectorInst::commute() { 1699 int NumOpElts = cast<FixedVectorType>(Op<0>()->getType())->getNumElements(); 1700 int NumMaskElts = ShuffleMask.size(); 1701 SmallVector<int, 16> NewMask(NumMaskElts); 1702 for (int i = 0; i != NumMaskElts; ++i) { 1703 int MaskElt = getMaskValue(i); 1704 if (MaskElt == PoisonMaskElem) { 1705 NewMask[i] = PoisonMaskElem; 1706 continue; 1707 } 1708 assert(MaskElt >= 0 && MaskElt < 2 * NumOpElts && "Out-of-range mask"); 1709 MaskElt = (MaskElt < NumOpElts) ? MaskElt + NumOpElts : MaskElt - NumOpElts; 1710 NewMask[i] = MaskElt; 1711 } 1712 setShuffleMask(NewMask); 1713 Op<0>().swap(Op<1>()); 1714 } 1715 1716 bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2, 1717 ArrayRef<int> Mask) { 1718 // V1 and V2 must be vectors of the same type. 1719 if (!isa<VectorType>(V1->getType()) || V1->getType() != V2->getType()) 1720 return false; 1721 1722 // Make sure the mask elements make sense. 1723 int V1Size = 1724 cast<VectorType>(V1->getType())->getElementCount().getKnownMinValue(); 1725 for (int Elem : Mask) 1726 if (Elem != PoisonMaskElem && Elem >= V1Size * 2) 1727 return false; 1728 1729 if (isa<ScalableVectorType>(V1->getType())) 1730 if ((Mask[0] != 0 && Mask[0] != PoisonMaskElem) || !all_equal(Mask)) 1731 return false; 1732 1733 return true; 1734 } 1735 1736 bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2, 1737 const Value *Mask) { 1738 // V1 and V2 must be vectors of the same type. 1739 if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType()) 1740 return false; 1741 1742 // Mask must be vector of i32, and must be the same kind of vector as the 1743 // input vectors 1744 auto *MaskTy = dyn_cast<VectorType>(Mask->getType()); 1745 if (!MaskTy || !MaskTy->getElementType()->isIntegerTy(32) || 1746 isa<ScalableVectorType>(MaskTy) != isa<ScalableVectorType>(V1->getType())) 1747 return false; 1748 1749 // Check to see if Mask is valid. 1750 if (isa<UndefValue>(Mask) || isa<ConstantAggregateZero>(Mask)) 1751 return true; 1752 1753 if (const auto *MV = dyn_cast<ConstantVector>(Mask)) { 1754 unsigned V1Size = cast<FixedVectorType>(V1->getType())->getNumElements(); 1755 for (Value *Op : MV->operands()) { 1756 if (auto *CI = dyn_cast<ConstantInt>(Op)) { 1757 if (CI->uge(V1Size*2)) 1758 return false; 1759 } else if (!isa<UndefValue>(Op)) { 1760 return false; 1761 } 1762 } 1763 return true; 1764 } 1765 1766 if (const auto *CDS = dyn_cast<ConstantDataSequential>(Mask)) { 1767 unsigned V1Size = cast<FixedVectorType>(V1->getType())->getNumElements(); 1768 for (unsigned i = 0, e = cast<FixedVectorType>(MaskTy)->getNumElements(); 1769 i != e; ++i) 1770 if (CDS->getElementAsInteger(i) >= V1Size*2) 1771 return false; 1772 return true; 1773 } 1774 1775 return false; 1776 } 1777 1778 void ShuffleVectorInst::getShuffleMask(const Constant *Mask, 1779 SmallVectorImpl<int> &Result) { 1780 ElementCount EC = cast<VectorType>(Mask->getType())->getElementCount(); 1781 1782 if (isa<ConstantAggregateZero>(Mask)) { 1783 Result.resize(EC.getKnownMinValue(), 0); 1784 return; 1785 } 1786 1787 Result.reserve(EC.getKnownMinValue()); 1788 1789 if (EC.isScalable()) { 1790 assert((isa<ConstantAggregateZero>(Mask) || isa<UndefValue>(Mask)) && 1791 "Scalable vector shuffle mask must be undef or zeroinitializer"); 1792 int MaskVal = isa<UndefValue>(Mask) ? -1 : 0; 1793 for (unsigned I = 0; I < EC.getKnownMinValue(); ++I) 1794 Result.emplace_back(MaskVal); 1795 return; 1796 } 1797 1798 unsigned NumElts = EC.getKnownMinValue(); 1799 1800 if (auto *CDS = dyn_cast<ConstantDataSequential>(Mask)) { 1801 for (unsigned i = 0; i != NumElts; ++i) 1802 Result.push_back(CDS->getElementAsInteger(i)); 1803 return; 1804 } 1805 for (unsigned i = 0; i != NumElts; ++i) { 1806 Constant *C = Mask->getAggregateElement(i); 1807 Result.push_back(isa<UndefValue>(C) ? -1 : 1808 cast<ConstantInt>(C)->getZExtValue()); 1809 } 1810 } 1811 1812 void ShuffleVectorInst::setShuffleMask(ArrayRef<int> Mask) { 1813 ShuffleMask.assign(Mask.begin(), Mask.end()); 1814 ShuffleMaskForBitcode = convertShuffleMaskForBitcode(Mask, getType()); 1815 } 1816 1817 Constant *ShuffleVectorInst::convertShuffleMaskForBitcode(ArrayRef<int> Mask, 1818 Type *ResultTy) { 1819 Type *Int32Ty = Type::getInt32Ty(ResultTy->getContext()); 1820 if (isa<ScalableVectorType>(ResultTy)) { 1821 assert(all_equal(Mask) && "Unexpected shuffle"); 1822 Type *VecTy = VectorType::get(Int32Ty, Mask.size(), true); 1823 if (Mask[0] == 0) 1824 return Constant::getNullValue(VecTy); 1825 return PoisonValue::get(VecTy); 1826 } 1827 SmallVector<Constant *, 16> MaskConst; 1828 for (int Elem : Mask) { 1829 if (Elem == PoisonMaskElem) 1830 MaskConst.push_back(PoisonValue::get(Int32Ty)); 1831 else 1832 MaskConst.push_back(ConstantInt::get(Int32Ty, Elem)); 1833 } 1834 return ConstantVector::get(MaskConst); 1835 } 1836 1837 static bool isSingleSourceMaskImpl(ArrayRef<int> Mask, int NumOpElts) { 1838 assert(!Mask.empty() && "Shuffle mask must contain elements"); 1839 bool UsesLHS = false; 1840 bool UsesRHS = false; 1841 for (int I : Mask) { 1842 if (I == -1) 1843 continue; 1844 assert(I >= 0 && I < (NumOpElts * 2) && 1845 "Out-of-bounds shuffle mask element"); 1846 UsesLHS |= (I < NumOpElts); 1847 UsesRHS |= (I >= NumOpElts); 1848 if (UsesLHS && UsesRHS) 1849 return false; 1850 } 1851 // Allow for degenerate case: completely undef mask means neither source is used. 1852 return UsesLHS || UsesRHS; 1853 } 1854 1855 bool ShuffleVectorInst::isSingleSourceMask(ArrayRef<int> Mask, int NumSrcElts) { 1856 // We don't have vector operand size information, so assume operands are the 1857 // same size as the mask. 1858 return isSingleSourceMaskImpl(Mask, NumSrcElts); 1859 } 1860 1861 static bool isIdentityMaskImpl(ArrayRef<int> Mask, int NumOpElts) { 1862 if (!isSingleSourceMaskImpl(Mask, NumOpElts)) 1863 return false; 1864 for (int i = 0, NumMaskElts = Mask.size(); i < NumMaskElts; ++i) { 1865 if (Mask[i] == -1) 1866 continue; 1867 if (Mask[i] != i && Mask[i] != (NumOpElts + i)) 1868 return false; 1869 } 1870 return true; 1871 } 1872 1873 bool ShuffleVectorInst::isIdentityMask(ArrayRef<int> Mask, int NumSrcElts) { 1874 if (Mask.size() != static_cast<unsigned>(NumSrcElts)) 1875 return false; 1876 // We don't have vector operand size information, so assume operands are the 1877 // same size as the mask. 1878 return isIdentityMaskImpl(Mask, NumSrcElts); 1879 } 1880 1881 bool ShuffleVectorInst::isReverseMask(ArrayRef<int> Mask, int NumSrcElts) { 1882 if (Mask.size() != static_cast<unsigned>(NumSrcElts)) 1883 return false; 1884 if (!isSingleSourceMask(Mask, NumSrcElts)) 1885 return false; 1886 1887 // The number of elements in the mask must be at least 2. 1888 if (NumSrcElts < 2) 1889 return false; 1890 1891 for (int I = 0, E = Mask.size(); I < E; ++I) { 1892 if (Mask[I] == -1) 1893 continue; 1894 if (Mask[I] != (NumSrcElts - 1 - I) && 1895 Mask[I] != (NumSrcElts + NumSrcElts - 1 - I)) 1896 return false; 1897 } 1898 return true; 1899 } 1900 1901 bool ShuffleVectorInst::isZeroEltSplatMask(ArrayRef<int> Mask, int NumSrcElts) { 1902 if (Mask.size() != static_cast<unsigned>(NumSrcElts)) 1903 return false; 1904 if (!isSingleSourceMask(Mask, NumSrcElts)) 1905 return false; 1906 for (int I = 0, E = Mask.size(); I < E; ++I) { 1907 if (Mask[I] == -1) 1908 continue; 1909 if (Mask[I] != 0 && Mask[I] != NumSrcElts) 1910 return false; 1911 } 1912 return true; 1913 } 1914 1915 bool ShuffleVectorInst::isSelectMask(ArrayRef<int> Mask, int NumSrcElts) { 1916 if (Mask.size() != static_cast<unsigned>(NumSrcElts)) 1917 return false; 1918 // Select is differentiated from identity. It requires using both sources. 1919 if (isSingleSourceMask(Mask, NumSrcElts)) 1920 return false; 1921 for (int I = 0, E = Mask.size(); I < E; ++I) { 1922 if (Mask[I] == -1) 1923 continue; 1924 if (Mask[I] != I && Mask[I] != (NumSrcElts + I)) 1925 return false; 1926 } 1927 return true; 1928 } 1929 1930 bool ShuffleVectorInst::isTransposeMask(ArrayRef<int> Mask, int NumSrcElts) { 1931 // Example masks that will return true: 1932 // v1 = <a, b, c, d> 1933 // v2 = <e, f, g, h> 1934 // trn1 = shufflevector v1, v2 <0, 4, 2, 6> = <a, e, c, g> 1935 // trn2 = shufflevector v1, v2 <1, 5, 3, 7> = <b, f, d, h> 1936 1937 if (Mask.size() != static_cast<unsigned>(NumSrcElts)) 1938 return false; 1939 // 1. The number of elements in the mask must be a power-of-2 and at least 2. 1940 int Sz = Mask.size(); 1941 if (Sz < 2 || !isPowerOf2_32(Sz)) 1942 return false; 1943 1944 // 2. The first element of the mask must be either a 0 or a 1. 1945 if (Mask[0] != 0 && Mask[0] != 1) 1946 return false; 1947 1948 // 3. The difference between the first 2 elements must be equal to the 1949 // number of elements in the mask. 1950 if ((Mask[1] - Mask[0]) != NumSrcElts) 1951 return false; 1952 1953 // 4. The difference between consecutive even-numbered and odd-numbered 1954 // elements must be equal to 2. 1955 for (int I = 2; I < Sz; ++I) { 1956 int MaskEltVal = Mask[I]; 1957 if (MaskEltVal == -1) 1958 return false; 1959 int MaskEltPrevVal = Mask[I - 2]; 1960 if (MaskEltVal - MaskEltPrevVal != 2) 1961 return false; 1962 } 1963 return true; 1964 } 1965 1966 bool ShuffleVectorInst::isSpliceMask(ArrayRef<int> Mask, int NumSrcElts, 1967 int &Index) { 1968 if (Mask.size() != static_cast<unsigned>(NumSrcElts)) 1969 return false; 1970 // Example: shufflevector <4 x n> A, <4 x n> B, <1,2,3,4> 1971 int StartIndex = -1; 1972 for (int I = 0, E = Mask.size(); I != E; ++I) { 1973 int MaskEltVal = Mask[I]; 1974 if (MaskEltVal == -1) 1975 continue; 1976 1977 if (StartIndex == -1) { 1978 // Don't support a StartIndex that begins in the second input, or if the 1979 // first non-undef index would access below the StartIndex. 1980 if (MaskEltVal < I || NumSrcElts <= (MaskEltVal - I)) 1981 return false; 1982 1983 StartIndex = MaskEltVal - I; 1984 continue; 1985 } 1986 1987 // Splice is sequential starting from StartIndex. 1988 if (MaskEltVal != (StartIndex + I)) 1989 return false; 1990 } 1991 1992 if (StartIndex == -1) 1993 return false; 1994 1995 // NOTE: This accepts StartIndex == 0 (COPY). 1996 Index = StartIndex; 1997 return true; 1998 } 1999 2000 bool ShuffleVectorInst::isExtractSubvectorMask(ArrayRef<int> Mask, 2001 int NumSrcElts, int &Index) { 2002 // Must extract from a single source. 2003 if (!isSingleSourceMaskImpl(Mask, NumSrcElts)) 2004 return false; 2005 2006 // Must be smaller (else this is an Identity shuffle). 2007 if (NumSrcElts <= (int)Mask.size()) 2008 return false; 2009 2010 // Find start of extraction, accounting that we may start with an UNDEF. 2011 int SubIndex = -1; 2012 for (int i = 0, e = Mask.size(); i != e; ++i) { 2013 int M = Mask[i]; 2014 if (M < 0) 2015 continue; 2016 int Offset = (M % NumSrcElts) - i; 2017 if (0 <= SubIndex && SubIndex != Offset) 2018 return false; 2019 SubIndex = Offset; 2020 } 2021 2022 if (0 <= SubIndex && SubIndex + (int)Mask.size() <= NumSrcElts) { 2023 Index = SubIndex; 2024 return true; 2025 } 2026 return false; 2027 } 2028 2029 bool ShuffleVectorInst::isInsertSubvectorMask(ArrayRef<int> Mask, 2030 int NumSrcElts, int &NumSubElts, 2031 int &Index) { 2032 int NumMaskElts = Mask.size(); 2033 2034 // Don't try to match if we're shuffling to a smaller size. 2035 if (NumMaskElts < NumSrcElts) 2036 return false; 2037 2038 // TODO: We don't recognize self-insertion/widening. 2039 if (isSingleSourceMaskImpl(Mask, NumSrcElts)) 2040 return false; 2041 2042 // Determine which mask elements are attributed to which source. 2043 APInt UndefElts = APInt::getZero(NumMaskElts); 2044 APInt Src0Elts = APInt::getZero(NumMaskElts); 2045 APInt Src1Elts = APInt::getZero(NumMaskElts); 2046 bool Src0Identity = true; 2047 bool Src1Identity = true; 2048 2049 for (int i = 0; i != NumMaskElts; ++i) { 2050 int M = Mask[i]; 2051 if (M < 0) { 2052 UndefElts.setBit(i); 2053 continue; 2054 } 2055 if (M < NumSrcElts) { 2056 Src0Elts.setBit(i); 2057 Src0Identity &= (M == i); 2058 continue; 2059 } 2060 Src1Elts.setBit(i); 2061 Src1Identity &= (M == (i + NumSrcElts)); 2062 } 2063 assert((Src0Elts | Src1Elts | UndefElts).isAllOnes() && 2064 "unknown shuffle elements"); 2065 assert(!Src0Elts.isZero() && !Src1Elts.isZero() && 2066 "2-source shuffle not found"); 2067 2068 // Determine lo/hi span ranges. 2069 // TODO: How should we handle undefs at the start of subvector insertions? 2070 int Src0Lo = Src0Elts.countr_zero(); 2071 int Src1Lo = Src1Elts.countr_zero(); 2072 int Src0Hi = NumMaskElts - Src0Elts.countl_zero(); 2073 int Src1Hi = NumMaskElts - Src1Elts.countl_zero(); 2074 2075 // If src0 is in place, see if the src1 elements is inplace within its own 2076 // span. 2077 if (Src0Identity) { 2078 int NumSub1Elts = Src1Hi - Src1Lo; 2079 ArrayRef<int> Sub1Mask = Mask.slice(Src1Lo, NumSub1Elts); 2080 if (isIdentityMaskImpl(Sub1Mask, NumSrcElts)) { 2081 NumSubElts = NumSub1Elts; 2082 Index = Src1Lo; 2083 return true; 2084 } 2085 } 2086 2087 // If src1 is in place, see if the src0 elements is inplace within its own 2088 // span. 2089 if (Src1Identity) { 2090 int NumSub0Elts = Src0Hi - Src0Lo; 2091 ArrayRef<int> Sub0Mask = Mask.slice(Src0Lo, NumSub0Elts); 2092 if (isIdentityMaskImpl(Sub0Mask, NumSrcElts)) { 2093 NumSubElts = NumSub0Elts; 2094 Index = Src0Lo; 2095 return true; 2096 } 2097 } 2098 2099 return false; 2100 } 2101 2102 bool ShuffleVectorInst::isIdentityWithPadding() const { 2103 // FIXME: Not currently possible to express a shuffle mask for a scalable 2104 // vector for this case. 2105 if (isa<ScalableVectorType>(getType())) 2106 return false; 2107 2108 int NumOpElts = cast<FixedVectorType>(Op<0>()->getType())->getNumElements(); 2109 int NumMaskElts = cast<FixedVectorType>(getType())->getNumElements(); 2110 if (NumMaskElts <= NumOpElts) 2111 return false; 2112 2113 // The first part of the mask must choose elements from exactly 1 source op. 2114 ArrayRef<int> Mask = getShuffleMask(); 2115 if (!isIdentityMaskImpl(Mask, NumOpElts)) 2116 return false; 2117 2118 // All extending must be with undef elements. 2119 for (int i = NumOpElts; i < NumMaskElts; ++i) 2120 if (Mask[i] != -1) 2121 return false; 2122 2123 return true; 2124 } 2125 2126 bool ShuffleVectorInst::isIdentityWithExtract() const { 2127 // FIXME: Not currently possible to express a shuffle mask for a scalable 2128 // vector for this case. 2129 if (isa<ScalableVectorType>(getType())) 2130 return false; 2131 2132 int NumOpElts = cast<FixedVectorType>(Op<0>()->getType())->getNumElements(); 2133 int NumMaskElts = cast<FixedVectorType>(getType())->getNumElements(); 2134 if (NumMaskElts >= NumOpElts) 2135 return false; 2136 2137 return isIdentityMaskImpl(getShuffleMask(), NumOpElts); 2138 } 2139 2140 bool ShuffleVectorInst::isConcat() const { 2141 // Vector concatenation is differentiated from identity with padding. 2142 if (isa<UndefValue>(Op<0>()) || isa<UndefValue>(Op<1>())) 2143 return false; 2144 2145 // FIXME: Not currently possible to express a shuffle mask for a scalable 2146 // vector for this case. 2147 if (isa<ScalableVectorType>(getType())) 2148 return false; 2149 2150 int NumOpElts = cast<FixedVectorType>(Op<0>()->getType())->getNumElements(); 2151 int NumMaskElts = cast<FixedVectorType>(getType())->getNumElements(); 2152 if (NumMaskElts != NumOpElts * 2) 2153 return false; 2154 2155 // Use the mask length rather than the operands' vector lengths here. We 2156 // already know that the shuffle returns a vector twice as long as the inputs, 2157 // and neither of the inputs are undef vectors. If the mask picks consecutive 2158 // elements from both inputs, then this is a concatenation of the inputs. 2159 return isIdentityMaskImpl(getShuffleMask(), NumMaskElts); 2160 } 2161 2162 static bool isReplicationMaskWithParams(ArrayRef<int> Mask, 2163 int ReplicationFactor, int VF) { 2164 assert(Mask.size() == (unsigned)ReplicationFactor * VF && 2165 "Unexpected mask size."); 2166 2167 for (int CurrElt : seq(VF)) { 2168 ArrayRef<int> CurrSubMask = Mask.take_front(ReplicationFactor); 2169 assert(CurrSubMask.size() == (unsigned)ReplicationFactor && 2170 "Run out of mask?"); 2171 Mask = Mask.drop_front(ReplicationFactor); 2172 if (!all_of(CurrSubMask, [CurrElt](int MaskElt) { 2173 return MaskElt == PoisonMaskElem || MaskElt == CurrElt; 2174 })) 2175 return false; 2176 } 2177 assert(Mask.empty() && "Did not consume the whole mask?"); 2178 2179 return true; 2180 } 2181 2182 bool ShuffleVectorInst::isReplicationMask(ArrayRef<int> Mask, 2183 int &ReplicationFactor, int &VF) { 2184 // undef-less case is trivial. 2185 if (!llvm::is_contained(Mask, PoisonMaskElem)) { 2186 ReplicationFactor = 2187 Mask.take_while([](int MaskElt) { return MaskElt == 0; }).size(); 2188 if (ReplicationFactor == 0 || Mask.size() % ReplicationFactor != 0) 2189 return false; 2190 VF = Mask.size() / ReplicationFactor; 2191 return isReplicationMaskWithParams(Mask, ReplicationFactor, VF); 2192 } 2193 2194 // However, if the mask contains undef's, we have to enumerate possible tuples 2195 // and pick one. There are bounds on replication factor: [1, mask size] 2196 // (where RF=1 is an identity shuffle, RF=mask size is a broadcast shuffle) 2197 // Additionally, mask size is a replication factor multiplied by vector size, 2198 // which further significantly reduces the search space. 2199 2200 // Before doing that, let's perform basic correctness checking first. 2201 int Largest = -1; 2202 for (int MaskElt : Mask) { 2203 if (MaskElt == PoisonMaskElem) 2204 continue; 2205 // Elements must be in non-decreasing order. 2206 if (MaskElt < Largest) 2207 return false; 2208 Largest = std::max(Largest, MaskElt); 2209 } 2210 2211 // Prefer larger replication factor if all else equal. 2212 for (int PossibleReplicationFactor : 2213 reverse(seq_inclusive<unsigned>(1, Mask.size()))) { 2214 if (Mask.size() % PossibleReplicationFactor != 0) 2215 continue; 2216 int PossibleVF = Mask.size() / PossibleReplicationFactor; 2217 if (!isReplicationMaskWithParams(Mask, PossibleReplicationFactor, 2218 PossibleVF)) 2219 continue; 2220 ReplicationFactor = PossibleReplicationFactor; 2221 VF = PossibleVF; 2222 return true; 2223 } 2224 2225 return false; 2226 } 2227 2228 bool ShuffleVectorInst::isReplicationMask(int &ReplicationFactor, 2229 int &VF) const { 2230 // Not possible to express a shuffle mask for a scalable vector for this 2231 // case. 2232 if (isa<ScalableVectorType>(getType())) 2233 return false; 2234 2235 VF = cast<FixedVectorType>(Op<0>()->getType())->getNumElements(); 2236 if (ShuffleMask.size() % VF != 0) 2237 return false; 2238 ReplicationFactor = ShuffleMask.size() / VF; 2239 2240 return isReplicationMaskWithParams(ShuffleMask, ReplicationFactor, VF); 2241 } 2242 2243 bool ShuffleVectorInst::isOneUseSingleSourceMask(ArrayRef<int> Mask, int VF) { 2244 if (VF <= 0 || Mask.size() < static_cast<unsigned>(VF) || 2245 Mask.size() % VF != 0) 2246 return false; 2247 for (unsigned K = 0, Sz = Mask.size(); K < Sz; K += VF) { 2248 ArrayRef<int> SubMask = Mask.slice(K, VF); 2249 if (all_of(SubMask, [](int Idx) { return Idx == PoisonMaskElem; })) 2250 continue; 2251 SmallBitVector Used(VF, false); 2252 for (int Idx : SubMask) { 2253 if (Idx != PoisonMaskElem && Idx < VF) 2254 Used.set(Idx); 2255 } 2256 if (!Used.all()) 2257 return false; 2258 } 2259 return true; 2260 } 2261 2262 /// Return true if this shuffle mask is a replication mask. 2263 bool ShuffleVectorInst::isOneUseSingleSourceMask(int VF) const { 2264 // Not possible to express a shuffle mask for a scalable vector for this 2265 // case. 2266 if (isa<ScalableVectorType>(getType())) 2267 return false; 2268 if (!isSingleSourceMask(ShuffleMask, VF)) 2269 return false; 2270 2271 return isOneUseSingleSourceMask(ShuffleMask, VF); 2272 } 2273 2274 bool ShuffleVectorInst::isInterleave(unsigned Factor) { 2275 FixedVectorType *OpTy = dyn_cast<FixedVectorType>(getOperand(0)->getType()); 2276 // shuffle_vector can only interleave fixed length vectors - for scalable 2277 // vectors, see the @llvm.vector.interleave2 intrinsic 2278 if (!OpTy) 2279 return false; 2280 unsigned OpNumElts = OpTy->getNumElements(); 2281 2282 return isInterleaveMask(ShuffleMask, Factor, OpNumElts * 2); 2283 } 2284 2285 bool ShuffleVectorInst::isInterleaveMask( 2286 ArrayRef<int> Mask, unsigned Factor, unsigned NumInputElts, 2287 SmallVectorImpl<unsigned> &StartIndexes) { 2288 unsigned NumElts = Mask.size(); 2289 if (NumElts % Factor) 2290 return false; 2291 2292 unsigned LaneLen = NumElts / Factor; 2293 if (!isPowerOf2_32(LaneLen)) 2294 return false; 2295 2296 StartIndexes.resize(Factor); 2297 2298 // Check whether each element matches the general interleaved rule. 2299 // Ignore undef elements, as long as the defined elements match the rule. 2300 // Outer loop processes all factors (x, y, z in the above example) 2301 unsigned I = 0, J; 2302 for (; I < Factor; I++) { 2303 unsigned SavedLaneValue; 2304 unsigned SavedNoUndefs = 0; 2305 2306 // Inner loop processes consecutive accesses (x, x+1... in the example) 2307 for (J = 0; J < LaneLen - 1; J++) { 2308 // Lane computes x's position in the Mask 2309 unsigned Lane = J * Factor + I; 2310 unsigned NextLane = Lane + Factor; 2311 int LaneValue = Mask[Lane]; 2312 int NextLaneValue = Mask[NextLane]; 2313 2314 // If both are defined, values must be sequential 2315 if (LaneValue >= 0 && NextLaneValue >= 0 && 2316 LaneValue + 1 != NextLaneValue) 2317 break; 2318 2319 // If the next value is undef, save the current one as reference 2320 if (LaneValue >= 0 && NextLaneValue < 0) { 2321 SavedLaneValue = LaneValue; 2322 SavedNoUndefs = 1; 2323 } 2324 2325 // Undefs are allowed, but defined elements must still be consecutive: 2326 // i.e.: x,..., undef,..., x + 2,..., undef,..., undef,..., x + 5, .... 2327 // Verify this by storing the last non-undef followed by an undef 2328 // Check that following non-undef masks are incremented with the 2329 // corresponding distance. 2330 if (SavedNoUndefs > 0 && LaneValue < 0) { 2331 SavedNoUndefs++; 2332 if (NextLaneValue >= 0 && 2333 SavedLaneValue + SavedNoUndefs != (unsigned)NextLaneValue) 2334 break; 2335 } 2336 } 2337 2338 if (J < LaneLen - 1) 2339 return false; 2340 2341 int StartMask = 0; 2342 if (Mask[I] >= 0) { 2343 // Check that the start of the I range (J=0) is greater than 0 2344 StartMask = Mask[I]; 2345 } else if (Mask[(LaneLen - 1) * Factor + I] >= 0) { 2346 // StartMask defined by the last value in lane 2347 StartMask = Mask[(LaneLen - 1) * Factor + I] - J; 2348 } else if (SavedNoUndefs > 0) { 2349 // StartMask defined by some non-zero value in the j loop 2350 StartMask = SavedLaneValue - (LaneLen - 1 - SavedNoUndefs); 2351 } 2352 // else StartMask remains set to 0, i.e. all elements are undefs 2353 2354 if (StartMask < 0) 2355 return false; 2356 // We must stay within the vectors; This case can happen with undefs. 2357 if (StartMask + LaneLen > NumInputElts) 2358 return false; 2359 2360 StartIndexes[I] = StartMask; 2361 } 2362 2363 return true; 2364 } 2365 2366 /// Check if the mask is a DE-interleave mask of the given factor 2367 /// \p Factor like: 2368 /// <Index, Index+Factor, ..., Index+(NumElts-1)*Factor> 2369 bool ShuffleVectorInst::isDeInterleaveMaskOfFactor(ArrayRef<int> Mask, 2370 unsigned Factor, 2371 unsigned &Index) { 2372 // Check all potential start indices from 0 to (Factor - 1). 2373 for (unsigned Idx = 0; Idx < Factor; Idx++) { 2374 unsigned I = 0; 2375 2376 // Check that elements are in ascending order by Factor. Ignore undef 2377 // elements. 2378 for (; I < Mask.size(); I++) 2379 if (Mask[I] >= 0 && static_cast<unsigned>(Mask[I]) != Idx + I * Factor) 2380 break; 2381 2382 if (I == Mask.size()) { 2383 Index = Idx; 2384 return true; 2385 } 2386 } 2387 2388 return false; 2389 } 2390 2391 /// Try to lower a vector shuffle as a bit rotation. 2392 /// 2393 /// Look for a repeated rotation pattern in each sub group. 2394 /// Returns an element-wise left bit rotation amount or -1 if failed. 2395 static int matchShuffleAsBitRotate(ArrayRef<int> Mask, int NumSubElts) { 2396 int NumElts = Mask.size(); 2397 assert((NumElts % NumSubElts) == 0 && "Illegal shuffle mask"); 2398 2399 int RotateAmt = -1; 2400 for (int i = 0; i != NumElts; i += NumSubElts) { 2401 for (int j = 0; j != NumSubElts; ++j) { 2402 int M = Mask[i + j]; 2403 if (M < 0) 2404 continue; 2405 if (M < i || M >= i + NumSubElts) 2406 return -1; 2407 int Offset = (NumSubElts - (M - (i + j))) % NumSubElts; 2408 if (0 <= RotateAmt && Offset != RotateAmt) 2409 return -1; 2410 RotateAmt = Offset; 2411 } 2412 } 2413 return RotateAmt; 2414 } 2415 2416 bool ShuffleVectorInst::isBitRotateMask( 2417 ArrayRef<int> Mask, unsigned EltSizeInBits, unsigned MinSubElts, 2418 unsigned MaxSubElts, unsigned &NumSubElts, unsigned &RotateAmt) { 2419 for (NumSubElts = MinSubElts; NumSubElts <= MaxSubElts; NumSubElts *= 2) { 2420 int EltRotateAmt = matchShuffleAsBitRotate(Mask, NumSubElts); 2421 if (EltRotateAmt < 0) 2422 continue; 2423 RotateAmt = EltRotateAmt * EltSizeInBits; 2424 return true; 2425 } 2426 2427 return false; 2428 } 2429 2430 //===----------------------------------------------------------------------===// 2431 // InsertValueInst Class 2432 //===----------------------------------------------------------------------===// 2433 2434 void InsertValueInst::init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs, 2435 const Twine &Name) { 2436 assert(getNumOperands() == 2 && "NumOperands not initialized?"); 2437 2438 // There's no fundamental reason why we require at least one index 2439 // (other than weirdness with &*IdxBegin being invalid; see 2440 // getelementptr's init routine for example). But there's no 2441 // present need to support it. 2442 assert(!Idxs.empty() && "InsertValueInst must have at least one index"); 2443 2444 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs) == 2445 Val->getType() && "Inserted value must match indexed type!"); 2446 Op<0>() = Agg; 2447 Op<1>() = Val; 2448 2449 Indices.append(Idxs.begin(), Idxs.end()); 2450 setName(Name); 2451 } 2452 2453 InsertValueInst::InsertValueInst(const InsertValueInst &IVI) 2454 : Instruction(IVI.getType(), InsertValue, 2455 OperandTraits<InsertValueInst>::op_begin(this), 2), 2456 Indices(IVI.Indices) { 2457 Op<0>() = IVI.getOperand(0); 2458 Op<1>() = IVI.getOperand(1); 2459 SubclassOptionalData = IVI.SubclassOptionalData; 2460 } 2461 2462 //===----------------------------------------------------------------------===// 2463 // ExtractValueInst Class 2464 //===----------------------------------------------------------------------===// 2465 2466 void ExtractValueInst::init(ArrayRef<unsigned> Idxs, const Twine &Name) { 2467 assert(getNumOperands() == 1 && "NumOperands not initialized?"); 2468 2469 // There's no fundamental reason why we require at least one index. 2470 // But there's no present need to support it. 2471 assert(!Idxs.empty() && "ExtractValueInst must have at least one index"); 2472 2473 Indices.append(Idxs.begin(), Idxs.end()); 2474 setName(Name); 2475 } 2476 2477 ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI) 2478 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)), 2479 Indices(EVI.Indices) { 2480 SubclassOptionalData = EVI.SubclassOptionalData; 2481 } 2482 2483 // getIndexedType - Returns the type of the element that would be extracted 2484 // with an extractvalue instruction with the specified parameters. 2485 // 2486 // A null type is returned if the indices are invalid for the specified 2487 // pointer type. 2488 // 2489 Type *ExtractValueInst::getIndexedType(Type *Agg, 2490 ArrayRef<unsigned> Idxs) { 2491 for (unsigned Index : Idxs) { 2492 // We can't use CompositeType::indexValid(Index) here. 2493 // indexValid() always returns true for arrays because getelementptr allows 2494 // out-of-bounds indices. Since we don't allow those for extractvalue and 2495 // insertvalue we need to check array indexing manually. 2496 // Since the only other types we can index into are struct types it's just 2497 // as easy to check those manually as well. 2498 if (ArrayType *AT = dyn_cast<ArrayType>(Agg)) { 2499 if (Index >= AT->getNumElements()) 2500 return nullptr; 2501 Agg = AT->getElementType(); 2502 } else if (StructType *ST = dyn_cast<StructType>(Agg)) { 2503 if (Index >= ST->getNumElements()) 2504 return nullptr; 2505 Agg = ST->getElementType(Index); 2506 } else { 2507 // Not a valid type to index into. 2508 return nullptr; 2509 } 2510 } 2511 return const_cast<Type*>(Agg); 2512 } 2513 2514 //===----------------------------------------------------------------------===// 2515 // UnaryOperator Class 2516 //===----------------------------------------------------------------------===// 2517 2518 UnaryOperator::UnaryOperator(UnaryOps iType, Value *S, Type *Ty, 2519 const Twine &Name, InsertPosition InsertBefore) 2520 : UnaryInstruction(Ty, iType, S, InsertBefore) { 2521 Op<0>() = S; 2522 setName(Name); 2523 AssertOK(); 2524 } 2525 2526 UnaryOperator *UnaryOperator::Create(UnaryOps Op, Value *S, const Twine &Name, 2527 InsertPosition InsertBefore) { 2528 return new UnaryOperator(Op, S, S->getType(), Name, InsertBefore); 2529 } 2530 2531 void UnaryOperator::AssertOK() { 2532 Value *LHS = getOperand(0); 2533 (void)LHS; // Silence warnings. 2534 #ifndef NDEBUG 2535 switch (getOpcode()) { 2536 case FNeg: 2537 assert(getType() == LHS->getType() && 2538 "Unary operation should return same type as operand!"); 2539 assert(getType()->isFPOrFPVectorTy() && 2540 "Tried to create a floating-point operation on a " 2541 "non-floating-point type!"); 2542 break; 2543 default: llvm_unreachable("Invalid opcode provided"); 2544 } 2545 #endif 2546 } 2547 2548 //===----------------------------------------------------------------------===// 2549 // BinaryOperator Class 2550 //===----------------------------------------------------------------------===// 2551 2552 BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2, Type *Ty, 2553 const Twine &Name, InsertPosition InsertBefore) 2554 : Instruction(Ty, iType, OperandTraits<BinaryOperator>::op_begin(this), 2555 OperandTraits<BinaryOperator>::operands(this), InsertBefore) { 2556 Op<0>() = S1; 2557 Op<1>() = S2; 2558 setName(Name); 2559 AssertOK(); 2560 } 2561 2562 void BinaryOperator::AssertOK() { 2563 Value *LHS = getOperand(0), *RHS = getOperand(1); 2564 (void)LHS; (void)RHS; // Silence warnings. 2565 assert(LHS->getType() == RHS->getType() && 2566 "Binary operator operand types must match!"); 2567 #ifndef NDEBUG 2568 switch (getOpcode()) { 2569 case Add: case Sub: 2570 case Mul: 2571 assert(getType() == LHS->getType() && 2572 "Arithmetic operation should return same type as operands!"); 2573 assert(getType()->isIntOrIntVectorTy() && 2574 "Tried to create an integer operation on a non-integer type!"); 2575 break; 2576 case FAdd: case FSub: 2577 case FMul: 2578 assert(getType() == LHS->getType() && 2579 "Arithmetic operation should return same type as operands!"); 2580 assert(getType()->isFPOrFPVectorTy() && 2581 "Tried to create a floating-point operation on a " 2582 "non-floating-point type!"); 2583 break; 2584 case UDiv: 2585 case SDiv: 2586 assert(getType() == LHS->getType() && 2587 "Arithmetic operation should return same type as operands!"); 2588 assert(getType()->isIntOrIntVectorTy() && 2589 "Incorrect operand type (not integer) for S/UDIV"); 2590 break; 2591 case FDiv: 2592 assert(getType() == LHS->getType() && 2593 "Arithmetic operation should return same type as operands!"); 2594 assert(getType()->isFPOrFPVectorTy() && 2595 "Incorrect operand type (not floating point) for FDIV"); 2596 break; 2597 case URem: 2598 case SRem: 2599 assert(getType() == LHS->getType() && 2600 "Arithmetic operation should return same type as operands!"); 2601 assert(getType()->isIntOrIntVectorTy() && 2602 "Incorrect operand type (not integer) for S/UREM"); 2603 break; 2604 case FRem: 2605 assert(getType() == LHS->getType() && 2606 "Arithmetic operation should return same type as operands!"); 2607 assert(getType()->isFPOrFPVectorTy() && 2608 "Incorrect operand type (not floating point) for FREM"); 2609 break; 2610 case Shl: 2611 case LShr: 2612 case AShr: 2613 assert(getType() == LHS->getType() && 2614 "Shift operation should return same type as operands!"); 2615 assert(getType()->isIntOrIntVectorTy() && 2616 "Tried to create a shift operation on a non-integral type!"); 2617 break; 2618 case And: case Or: 2619 case Xor: 2620 assert(getType() == LHS->getType() && 2621 "Logical operation should return same type as operands!"); 2622 assert(getType()->isIntOrIntVectorTy() && 2623 "Tried to create a logical operation on a non-integral type!"); 2624 break; 2625 default: llvm_unreachable("Invalid opcode provided"); 2626 } 2627 #endif 2628 } 2629 2630 BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2, 2631 const Twine &Name, 2632 InsertPosition InsertBefore) { 2633 assert(S1->getType() == S2->getType() && 2634 "Cannot create binary operator with two operands of differing type!"); 2635 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore); 2636 } 2637 2638 BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name, 2639 InsertPosition InsertBefore) { 2640 Value *Zero = ConstantInt::get(Op->getType(), 0); 2641 return new BinaryOperator(Instruction::Sub, Zero, Op, Op->getType(), Name, 2642 InsertBefore); 2643 } 2644 2645 BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name, 2646 InsertPosition InsertBefore) { 2647 Value *Zero = ConstantInt::get(Op->getType(), 0); 2648 return BinaryOperator::CreateNSWSub(Zero, Op, Name, InsertBefore); 2649 } 2650 2651 BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name, 2652 InsertPosition InsertBefore) { 2653 Constant *C = Constant::getAllOnesValue(Op->getType()); 2654 return new BinaryOperator(Instruction::Xor, Op, C, 2655 Op->getType(), Name, InsertBefore); 2656 } 2657 2658 // Exchange the two operands to this instruction. This instruction is safe to 2659 // use on any binary instruction and does not modify the semantics of the 2660 // instruction. If the instruction is order-dependent (SetLT f.e.), the opcode 2661 // is changed. 2662 bool BinaryOperator::swapOperands() { 2663 if (!isCommutative()) 2664 return true; // Can't commute operands 2665 Op<0>().swap(Op<1>()); 2666 return false; 2667 } 2668 2669 //===----------------------------------------------------------------------===// 2670 // FPMathOperator Class 2671 //===----------------------------------------------------------------------===// 2672 2673 float FPMathOperator::getFPAccuracy() const { 2674 const MDNode *MD = 2675 cast<Instruction>(this)->getMetadata(LLVMContext::MD_fpmath); 2676 if (!MD) 2677 return 0.0; 2678 ConstantFP *Accuracy = mdconst::extract<ConstantFP>(MD->getOperand(0)); 2679 return Accuracy->getValueAPF().convertToFloat(); 2680 } 2681 2682 //===----------------------------------------------------------------------===// 2683 // CastInst Class 2684 //===----------------------------------------------------------------------===// 2685 2686 // Just determine if this cast only deals with integral->integral conversion. 2687 bool CastInst::isIntegerCast() const { 2688 switch (getOpcode()) { 2689 default: return false; 2690 case Instruction::ZExt: 2691 case Instruction::SExt: 2692 case Instruction::Trunc: 2693 return true; 2694 case Instruction::BitCast: 2695 return getOperand(0)->getType()->isIntegerTy() && 2696 getType()->isIntegerTy(); 2697 } 2698 } 2699 2700 /// This function determines if the CastInst does not require any bits to be 2701 /// changed in order to effect the cast. Essentially, it identifies cases where 2702 /// no code gen is necessary for the cast, hence the name no-op cast. For 2703 /// example, the following are all no-op casts: 2704 /// # bitcast i32* %x to i8* 2705 /// # bitcast <2 x i32> %x to <4 x i16> 2706 /// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only 2707 /// Determine if the described cast is a no-op. 2708 bool CastInst::isNoopCast(Instruction::CastOps Opcode, 2709 Type *SrcTy, 2710 Type *DestTy, 2711 const DataLayout &DL) { 2712 assert(castIsValid(Opcode, SrcTy, DestTy) && "method precondition"); 2713 switch (Opcode) { 2714 default: llvm_unreachable("Invalid CastOp"); 2715 case Instruction::Trunc: 2716 case Instruction::ZExt: 2717 case Instruction::SExt: 2718 case Instruction::FPTrunc: 2719 case Instruction::FPExt: 2720 case Instruction::UIToFP: 2721 case Instruction::SIToFP: 2722 case Instruction::FPToUI: 2723 case Instruction::FPToSI: 2724 case Instruction::AddrSpaceCast: 2725 // TODO: Target informations may give a more accurate answer here. 2726 return false; 2727 case Instruction::BitCast: 2728 return true; // BitCast never modifies bits. 2729 case Instruction::PtrToInt: 2730 return DL.getIntPtrType(SrcTy)->getScalarSizeInBits() == 2731 DestTy->getScalarSizeInBits(); 2732 case Instruction::IntToPtr: 2733 return DL.getIntPtrType(DestTy)->getScalarSizeInBits() == 2734 SrcTy->getScalarSizeInBits(); 2735 } 2736 } 2737 2738 bool CastInst::isNoopCast(const DataLayout &DL) const { 2739 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), DL); 2740 } 2741 2742 /// This function determines if a pair of casts can be eliminated and what 2743 /// opcode should be used in the elimination. This assumes that there are two 2744 /// instructions like this: 2745 /// * %F = firstOpcode SrcTy %x to MidTy 2746 /// * %S = secondOpcode MidTy %F to DstTy 2747 /// The function returns a resultOpcode so these two casts can be replaced with: 2748 /// * %Replacement = resultOpcode %SrcTy %x to DstTy 2749 /// If no such cast is permitted, the function returns 0. 2750 unsigned CastInst::isEliminableCastPair( 2751 Instruction::CastOps firstOp, Instruction::CastOps secondOp, 2752 Type *SrcTy, Type *MidTy, Type *DstTy, Type *SrcIntPtrTy, Type *MidIntPtrTy, 2753 Type *DstIntPtrTy) { 2754 // Define the 144 possibilities for these two cast instructions. The values 2755 // in this matrix determine what to do in a given situation and select the 2756 // case in the switch below. The rows correspond to firstOp, the columns 2757 // correspond to secondOp. In looking at the table below, keep in mind 2758 // the following cast properties: 2759 // 2760 // Size Compare Source Destination 2761 // Operator Src ? Size Type Sign Type Sign 2762 // -------- ------------ ------------------- --------------------- 2763 // TRUNC > Integer Any Integral Any 2764 // ZEXT < Integral Unsigned Integer Any 2765 // SEXT < Integral Signed Integer Any 2766 // FPTOUI n/a FloatPt n/a Integral Unsigned 2767 // FPTOSI n/a FloatPt n/a Integral Signed 2768 // UITOFP n/a Integral Unsigned FloatPt n/a 2769 // SITOFP n/a Integral Signed FloatPt n/a 2770 // FPTRUNC > FloatPt n/a FloatPt n/a 2771 // FPEXT < FloatPt n/a FloatPt n/a 2772 // PTRTOINT n/a Pointer n/a Integral Unsigned 2773 // INTTOPTR n/a Integral Unsigned Pointer n/a 2774 // BITCAST = FirstClass n/a FirstClass n/a 2775 // ADDRSPCST n/a Pointer n/a Pointer n/a 2776 // 2777 // NOTE: some transforms are safe, but we consider them to be non-profitable. 2778 // For example, we could merge "fptoui double to i32" + "zext i32 to i64", 2779 // into "fptoui double to i64", but this loses information about the range 2780 // of the produced value (we no longer know the top-part is all zeros). 2781 // Further this conversion is often much more expensive for typical hardware, 2782 // and causes issues when building libgcc. We disallow fptosi+sext for the 2783 // same reason. 2784 const unsigned numCastOps = 2785 Instruction::CastOpsEnd - Instruction::CastOpsBegin; 2786 static const uint8_t CastResults[numCastOps][numCastOps] = { 2787 // T F F U S F F P I B A -+ 2788 // R Z S P P I I T P 2 N T S | 2789 // U E E 2 2 2 2 R E I T C C +- secondOp 2790 // N X X U S F F N X N 2 V V | 2791 // C T T I I P P C T T P T T -+ 2792 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // Trunc -+ 2793 { 8, 1, 9,99,99, 2,17,99,99,99, 2, 3, 0}, // ZExt | 2794 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3, 0}, // SExt | 2795 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToUI | 2796 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToSI | 2797 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // UIToFP +- firstOp 2798 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // SIToFP | 2799 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // FPTrunc | 2800 { 99,99,99, 2, 2,99,99, 8, 2,99,99, 4, 0}, // FPExt | 2801 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3, 0}, // PtrToInt | 2802 { 99,99,99,99,99,99,99,99,99,11,99,15, 0}, // IntToPtr | 2803 { 5, 5, 5, 0, 0, 5, 5, 0, 0,16, 5, 1,14}, // BitCast | 2804 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,13,12}, // AddrSpaceCast -+ 2805 }; 2806 2807 // TODO: This logic could be encoded into the table above and handled in the 2808 // switch below. 2809 // If either of the casts are a bitcast from scalar to vector, disallow the 2810 // merging. However, any pair of bitcasts are allowed. 2811 bool IsFirstBitcast = (firstOp == Instruction::BitCast); 2812 bool IsSecondBitcast = (secondOp == Instruction::BitCast); 2813 bool AreBothBitcasts = IsFirstBitcast && IsSecondBitcast; 2814 2815 // Check if any of the casts convert scalars <-> vectors. 2816 if ((IsFirstBitcast && isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) || 2817 (IsSecondBitcast && isa<VectorType>(MidTy) != isa<VectorType>(DstTy))) 2818 if (!AreBothBitcasts) 2819 return 0; 2820 2821 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin] 2822 [secondOp-Instruction::CastOpsBegin]; 2823 switch (ElimCase) { 2824 case 0: 2825 // Categorically disallowed. 2826 return 0; 2827 case 1: 2828 // Allowed, use first cast's opcode. 2829 return firstOp; 2830 case 2: 2831 // Allowed, use second cast's opcode. 2832 return secondOp; 2833 case 3: 2834 // No-op cast in second op implies firstOp as long as the DestTy 2835 // is integer and we are not converting between a vector and a 2836 // non-vector type. 2837 if (!SrcTy->isVectorTy() && DstTy->isIntegerTy()) 2838 return firstOp; 2839 return 0; 2840 case 4: 2841 // No-op cast in second op implies firstOp as long as the DestTy 2842 // matches MidTy. 2843 if (DstTy == MidTy) 2844 return firstOp; 2845 return 0; 2846 case 5: 2847 // No-op cast in first op implies secondOp as long as the SrcTy 2848 // is an integer. 2849 if (SrcTy->isIntegerTy()) 2850 return secondOp; 2851 return 0; 2852 case 7: { 2853 // Disable inttoptr/ptrtoint optimization if enabled. 2854 if (DisableI2pP2iOpt) 2855 return 0; 2856 2857 // Cannot simplify if address spaces are different! 2858 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace()) 2859 return 0; 2860 2861 unsigned MidSize = MidTy->getScalarSizeInBits(); 2862 // We can still fold this without knowing the actual sizes as long we 2863 // know that the intermediate pointer is the largest possible 2864 // pointer size. 2865 // FIXME: Is this always true? 2866 if (MidSize == 64) 2867 return Instruction::BitCast; 2868 2869 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size. 2870 if (!SrcIntPtrTy || DstIntPtrTy != SrcIntPtrTy) 2871 return 0; 2872 unsigned PtrSize = SrcIntPtrTy->getScalarSizeInBits(); 2873 if (MidSize >= PtrSize) 2874 return Instruction::BitCast; 2875 return 0; 2876 } 2877 case 8: { 2878 // ext, trunc -> bitcast, if the SrcTy and DstTy are the same 2879 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy) 2880 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy) 2881 unsigned SrcSize = SrcTy->getScalarSizeInBits(); 2882 unsigned DstSize = DstTy->getScalarSizeInBits(); 2883 if (SrcTy == DstTy) 2884 return Instruction::BitCast; 2885 if (SrcSize < DstSize) 2886 return firstOp; 2887 if (SrcSize > DstSize) 2888 return secondOp; 2889 return 0; 2890 } 2891 case 9: 2892 // zext, sext -> zext, because sext can't sign extend after zext 2893 return Instruction::ZExt; 2894 case 11: { 2895 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize 2896 if (!MidIntPtrTy) 2897 return 0; 2898 unsigned PtrSize = MidIntPtrTy->getScalarSizeInBits(); 2899 unsigned SrcSize = SrcTy->getScalarSizeInBits(); 2900 unsigned DstSize = DstTy->getScalarSizeInBits(); 2901 if (SrcSize <= PtrSize && SrcSize == DstSize) 2902 return Instruction::BitCast; 2903 return 0; 2904 } 2905 case 12: 2906 // addrspacecast, addrspacecast -> bitcast, if SrcAS == DstAS 2907 // addrspacecast, addrspacecast -> addrspacecast, if SrcAS != DstAS 2908 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace()) 2909 return Instruction::AddrSpaceCast; 2910 return Instruction::BitCast; 2911 case 13: 2912 // FIXME: this state can be merged with (1), but the following assert 2913 // is useful to check the correcteness of the sequence due to semantic 2914 // change of bitcast. 2915 assert( 2916 SrcTy->isPtrOrPtrVectorTy() && 2917 MidTy->isPtrOrPtrVectorTy() && 2918 DstTy->isPtrOrPtrVectorTy() && 2919 SrcTy->getPointerAddressSpace() != MidTy->getPointerAddressSpace() && 2920 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() && 2921 "Illegal addrspacecast, bitcast sequence!"); 2922 // Allowed, use first cast's opcode 2923 return firstOp; 2924 case 14: 2925 // bitcast, addrspacecast -> addrspacecast 2926 return Instruction::AddrSpaceCast; 2927 case 15: 2928 // FIXME: this state can be merged with (1), but the following assert 2929 // is useful to check the correcteness of the sequence due to semantic 2930 // change of bitcast. 2931 assert( 2932 SrcTy->isIntOrIntVectorTy() && 2933 MidTy->isPtrOrPtrVectorTy() && 2934 DstTy->isPtrOrPtrVectorTy() && 2935 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() && 2936 "Illegal inttoptr, bitcast sequence!"); 2937 // Allowed, use first cast's opcode 2938 return firstOp; 2939 case 16: 2940 // FIXME: this state can be merged with (2), but the following assert 2941 // is useful to check the correcteness of the sequence due to semantic 2942 // change of bitcast. 2943 assert( 2944 SrcTy->isPtrOrPtrVectorTy() && 2945 MidTy->isPtrOrPtrVectorTy() && 2946 DstTy->isIntOrIntVectorTy() && 2947 SrcTy->getPointerAddressSpace() == MidTy->getPointerAddressSpace() && 2948 "Illegal bitcast, ptrtoint sequence!"); 2949 // Allowed, use second cast's opcode 2950 return secondOp; 2951 case 17: 2952 // (sitofp (zext x)) -> (uitofp x) 2953 return Instruction::UIToFP; 2954 case 99: 2955 // Cast combination can't happen (error in input). This is for all cases 2956 // where the MidTy is not the same for the two cast instructions. 2957 llvm_unreachable("Invalid Cast Combination"); 2958 default: 2959 llvm_unreachable("Error in CastResults table!!!"); 2960 } 2961 } 2962 2963 CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty, 2964 const Twine &Name, InsertPosition InsertBefore) { 2965 assert(castIsValid(op, S, Ty) && "Invalid cast!"); 2966 // Construct and return the appropriate CastInst subclass 2967 switch (op) { 2968 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore); 2969 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore); 2970 case SExt: return new SExtInst (S, Ty, Name, InsertBefore); 2971 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore); 2972 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore); 2973 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore); 2974 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore); 2975 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore); 2976 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore); 2977 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore); 2978 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore); 2979 case BitCast: 2980 return new BitCastInst(S, Ty, Name, InsertBefore); 2981 case AddrSpaceCast: 2982 return new AddrSpaceCastInst(S, Ty, Name, InsertBefore); 2983 default: 2984 llvm_unreachable("Invalid opcode provided"); 2985 } 2986 } 2987 2988 CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty, const Twine &Name, 2989 InsertPosition InsertBefore) { 2990 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) 2991 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); 2992 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore); 2993 } 2994 2995 CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty, const Twine &Name, 2996 InsertPosition InsertBefore) { 2997 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) 2998 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); 2999 return Create(Instruction::SExt, S, Ty, Name, InsertBefore); 3000 } 3001 3002 CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty, const Twine &Name, 3003 InsertPosition InsertBefore) { 3004 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) 3005 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); 3006 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore); 3007 } 3008 3009 /// Create a BitCast or a PtrToInt cast instruction 3010 CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty, const Twine &Name, 3011 InsertPosition InsertBefore) { 3012 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast"); 3013 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) && 3014 "Invalid cast"); 3015 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast"); 3016 assert((!Ty->isVectorTy() || 3017 cast<VectorType>(Ty)->getElementCount() == 3018 cast<VectorType>(S->getType())->getElementCount()) && 3019 "Invalid cast"); 3020 3021 if (Ty->isIntOrIntVectorTy()) 3022 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore); 3023 3024 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertBefore); 3025 } 3026 3027 CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast( 3028 Value *S, Type *Ty, const Twine &Name, InsertPosition InsertBefore) { 3029 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast"); 3030 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast"); 3031 3032 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace()) 3033 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertBefore); 3034 3035 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); 3036 } 3037 3038 CastInst *CastInst::CreateBitOrPointerCast(Value *S, Type *Ty, 3039 const Twine &Name, 3040 InsertPosition InsertBefore) { 3041 if (S->getType()->isPointerTy() && Ty->isIntegerTy()) 3042 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore); 3043 if (S->getType()->isIntegerTy() && Ty->isPointerTy()) 3044 return Create(Instruction::IntToPtr, S, Ty, Name, InsertBefore); 3045 3046 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); 3047 } 3048 3049 CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty, bool isSigned, 3050 const Twine &Name, 3051 InsertPosition InsertBefore) { 3052 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() && 3053 "Invalid integer cast"); 3054 unsigned SrcBits = C->getType()->getScalarSizeInBits(); 3055 unsigned DstBits = Ty->getScalarSizeInBits(); 3056 Instruction::CastOps opcode = 3057 (SrcBits == DstBits ? Instruction::BitCast : 3058 (SrcBits > DstBits ? Instruction::Trunc : 3059 (isSigned ? Instruction::SExt : Instruction::ZExt))); 3060 return Create(opcode, C, Ty, Name, InsertBefore); 3061 } 3062 3063 CastInst *CastInst::CreateFPCast(Value *C, Type *Ty, const Twine &Name, 3064 InsertPosition InsertBefore) { 3065 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() && 3066 "Invalid cast"); 3067 unsigned SrcBits = C->getType()->getScalarSizeInBits(); 3068 unsigned DstBits = Ty->getScalarSizeInBits(); 3069 assert((C->getType() == Ty || SrcBits != DstBits) && "Invalid cast"); 3070 Instruction::CastOps opcode = 3071 (SrcBits == DstBits ? Instruction::BitCast : 3072 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt)); 3073 return Create(opcode, C, Ty, Name, InsertBefore); 3074 } 3075 3076 bool CastInst::isBitCastable(Type *SrcTy, Type *DestTy) { 3077 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType()) 3078 return false; 3079 3080 if (SrcTy == DestTy) 3081 return true; 3082 3083 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) { 3084 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy)) { 3085 if (SrcVecTy->getElementCount() == DestVecTy->getElementCount()) { 3086 // An element by element cast. Valid if casting the elements is valid. 3087 SrcTy = SrcVecTy->getElementType(); 3088 DestTy = DestVecTy->getElementType(); 3089 } 3090 } 3091 } 3092 3093 if (PointerType *DestPtrTy = dyn_cast<PointerType>(DestTy)) { 3094 if (PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy)) { 3095 return SrcPtrTy->getAddressSpace() == DestPtrTy->getAddressSpace(); 3096 } 3097 } 3098 3099 TypeSize SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr 3100 TypeSize DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr 3101 3102 // Could still have vectors of pointers if the number of elements doesn't 3103 // match 3104 if (SrcBits.getKnownMinValue() == 0 || DestBits.getKnownMinValue() == 0) 3105 return false; 3106 3107 if (SrcBits != DestBits) 3108 return false; 3109 3110 if (DestTy->isX86_MMXTy() || SrcTy->isX86_MMXTy()) 3111 return false; 3112 3113 return true; 3114 } 3115 3116 bool CastInst::isBitOrNoopPointerCastable(Type *SrcTy, Type *DestTy, 3117 const DataLayout &DL) { 3118 // ptrtoint and inttoptr are not allowed on non-integral pointers 3119 if (auto *PtrTy = dyn_cast<PointerType>(SrcTy)) 3120 if (auto *IntTy = dyn_cast<IntegerType>(DestTy)) 3121 return (IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy) && 3122 !DL.isNonIntegralPointerType(PtrTy)); 3123 if (auto *PtrTy = dyn_cast<PointerType>(DestTy)) 3124 if (auto *IntTy = dyn_cast<IntegerType>(SrcTy)) 3125 return (IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy) && 3126 !DL.isNonIntegralPointerType(PtrTy)); 3127 3128 return isBitCastable(SrcTy, DestTy); 3129 } 3130 3131 // Provide a way to get a "cast" where the cast opcode is inferred from the 3132 // types and size of the operand. This, basically, is a parallel of the 3133 // logic in the castIsValid function below. This axiom should hold: 3134 // castIsValid( getCastOpcode(Val, Ty), Val, Ty) 3135 // should not assert in castIsValid. In other words, this produces a "correct" 3136 // casting opcode for the arguments passed to it. 3137 Instruction::CastOps 3138 CastInst::getCastOpcode( 3139 const Value *Src, bool SrcIsSigned, Type *DestTy, bool DestIsSigned) { 3140 Type *SrcTy = Src->getType(); 3141 3142 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() && 3143 "Only first class types are castable!"); 3144 3145 if (SrcTy == DestTy) 3146 return BitCast; 3147 3148 // FIXME: Check address space sizes here 3149 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) 3150 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy)) 3151 if (SrcVecTy->getElementCount() == DestVecTy->getElementCount()) { 3152 // An element by element cast. Find the appropriate opcode based on the 3153 // element types. 3154 SrcTy = SrcVecTy->getElementType(); 3155 DestTy = DestVecTy->getElementType(); 3156 } 3157 3158 // Get the bit sizes, we'll need these 3159 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr 3160 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr 3161 3162 // Run through the possibilities ... 3163 if (DestTy->isIntegerTy()) { // Casting to integral 3164 if (SrcTy->isIntegerTy()) { // Casting from integral 3165 if (DestBits < SrcBits) 3166 return Trunc; // int -> smaller int 3167 else if (DestBits > SrcBits) { // its an extension 3168 if (SrcIsSigned) 3169 return SExt; // signed -> SEXT 3170 else 3171 return ZExt; // unsigned -> ZEXT 3172 } else { 3173 return BitCast; // Same size, No-op cast 3174 } 3175 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt 3176 if (DestIsSigned) 3177 return FPToSI; // FP -> sint 3178 else 3179 return FPToUI; // FP -> uint 3180 } else if (SrcTy->isVectorTy()) { 3181 assert(DestBits == SrcBits && 3182 "Casting vector to integer of different width"); 3183 return BitCast; // Same size, no-op cast 3184 } else { 3185 assert(SrcTy->isPointerTy() && 3186 "Casting from a value that is not first-class type"); 3187 return PtrToInt; // ptr -> int 3188 } 3189 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt 3190 if (SrcTy->isIntegerTy()) { // Casting from integral 3191 if (SrcIsSigned) 3192 return SIToFP; // sint -> FP 3193 else 3194 return UIToFP; // uint -> FP 3195 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt 3196 if (DestBits < SrcBits) { 3197 return FPTrunc; // FP -> smaller FP 3198 } else if (DestBits > SrcBits) { 3199 return FPExt; // FP -> larger FP 3200 } else { 3201 return BitCast; // same size, no-op cast 3202 } 3203 } else if (SrcTy->isVectorTy()) { 3204 assert(DestBits == SrcBits && 3205 "Casting vector to floating point of different width"); 3206 return BitCast; // same size, no-op cast 3207 } 3208 llvm_unreachable("Casting pointer or non-first class to float"); 3209 } else if (DestTy->isVectorTy()) { 3210 assert(DestBits == SrcBits && 3211 "Illegal cast to vector (wrong type or size)"); 3212 return BitCast; 3213 } else if (DestTy->isPointerTy()) { 3214 if (SrcTy->isPointerTy()) { 3215 if (DestTy->getPointerAddressSpace() != SrcTy->getPointerAddressSpace()) 3216 return AddrSpaceCast; 3217 return BitCast; // ptr -> ptr 3218 } else if (SrcTy->isIntegerTy()) { 3219 return IntToPtr; // int -> ptr 3220 } 3221 llvm_unreachable("Casting pointer to other than pointer or int"); 3222 } else if (DestTy->isX86_MMXTy()) { 3223 if (SrcTy->isVectorTy()) { 3224 assert(DestBits == SrcBits && "Casting vector of wrong width to X86_MMX"); 3225 return BitCast; // 64-bit vector to MMX 3226 } 3227 llvm_unreachable("Illegal cast to X86_MMX"); 3228 } 3229 llvm_unreachable("Casting to type that is not first-class"); 3230 } 3231 3232 //===----------------------------------------------------------------------===// 3233 // CastInst SubClass Constructors 3234 //===----------------------------------------------------------------------===// 3235 3236 /// Check that the construction parameters for a CastInst are correct. This 3237 /// could be broken out into the separate constructors but it is useful to have 3238 /// it in one place and to eliminate the redundant code for getting the sizes 3239 /// of the types involved. 3240 bool 3241 CastInst::castIsValid(Instruction::CastOps op, Type *SrcTy, Type *DstTy) { 3242 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() || 3243 SrcTy->isAggregateType() || DstTy->isAggregateType()) 3244 return false; 3245 3246 // Get the size of the types in bits, and whether we are dealing 3247 // with vector types, we'll need this later. 3248 bool SrcIsVec = isa<VectorType>(SrcTy); 3249 bool DstIsVec = isa<VectorType>(DstTy); 3250 unsigned SrcScalarBitSize = SrcTy->getScalarSizeInBits(); 3251 unsigned DstScalarBitSize = DstTy->getScalarSizeInBits(); 3252 3253 // If these are vector types, get the lengths of the vectors (using zero for 3254 // scalar types means that checking that vector lengths match also checks that 3255 // scalars are not being converted to vectors or vectors to scalars). 3256 ElementCount SrcEC = SrcIsVec ? cast<VectorType>(SrcTy)->getElementCount() 3257 : ElementCount::getFixed(0); 3258 ElementCount DstEC = DstIsVec ? cast<VectorType>(DstTy)->getElementCount() 3259 : ElementCount::getFixed(0); 3260 3261 // Switch on the opcode provided 3262 switch (op) { 3263 default: return false; // This is an input error 3264 case Instruction::Trunc: 3265 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() && 3266 SrcEC == DstEC && SrcScalarBitSize > DstScalarBitSize; 3267 case Instruction::ZExt: 3268 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() && 3269 SrcEC == DstEC && SrcScalarBitSize < DstScalarBitSize; 3270 case Instruction::SExt: 3271 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() && 3272 SrcEC == DstEC && SrcScalarBitSize < DstScalarBitSize; 3273 case Instruction::FPTrunc: 3274 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() && 3275 SrcEC == DstEC && SrcScalarBitSize > DstScalarBitSize; 3276 case Instruction::FPExt: 3277 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() && 3278 SrcEC == DstEC && SrcScalarBitSize < DstScalarBitSize; 3279 case Instruction::UIToFP: 3280 case Instruction::SIToFP: 3281 return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy() && 3282 SrcEC == DstEC; 3283 case Instruction::FPToUI: 3284 case Instruction::FPToSI: 3285 return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy() && 3286 SrcEC == DstEC; 3287 case Instruction::PtrToInt: 3288 if (SrcEC != DstEC) 3289 return false; 3290 return SrcTy->isPtrOrPtrVectorTy() && DstTy->isIntOrIntVectorTy(); 3291 case Instruction::IntToPtr: 3292 if (SrcEC != DstEC) 3293 return false; 3294 return SrcTy->isIntOrIntVectorTy() && DstTy->isPtrOrPtrVectorTy(); 3295 case Instruction::BitCast: { 3296 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType()); 3297 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType()); 3298 3299 // BitCast implies a no-op cast of type only. No bits change. 3300 // However, you can't cast pointers to anything but pointers. 3301 if (!SrcPtrTy != !DstPtrTy) 3302 return false; 3303 3304 // For non-pointer cases, the cast is okay if the source and destination bit 3305 // widths are identical. 3306 if (!SrcPtrTy) 3307 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits(); 3308 3309 // If both are pointers then the address spaces must match. 3310 if (SrcPtrTy->getAddressSpace() != DstPtrTy->getAddressSpace()) 3311 return false; 3312 3313 // A vector of pointers must have the same number of elements. 3314 if (SrcIsVec && DstIsVec) 3315 return SrcEC == DstEC; 3316 if (SrcIsVec) 3317 return SrcEC == ElementCount::getFixed(1); 3318 if (DstIsVec) 3319 return DstEC == ElementCount::getFixed(1); 3320 3321 return true; 3322 } 3323 case Instruction::AddrSpaceCast: { 3324 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType()); 3325 if (!SrcPtrTy) 3326 return false; 3327 3328 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType()); 3329 if (!DstPtrTy) 3330 return false; 3331 3332 if (SrcPtrTy->getAddressSpace() == DstPtrTy->getAddressSpace()) 3333 return false; 3334 3335 return SrcEC == DstEC; 3336 } 3337 } 3338 } 3339 3340 TruncInst::TruncInst(Value *S, Type *Ty, const Twine &Name, 3341 InsertPosition InsertBefore) 3342 : CastInst(Ty, Trunc, S, Name, InsertBefore) { 3343 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc"); 3344 } 3345 3346 ZExtInst::ZExtInst(Value *S, Type *Ty, const Twine &Name, 3347 InsertPosition InsertBefore) 3348 : CastInst(Ty, ZExt, S, Name, InsertBefore) { 3349 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt"); 3350 } 3351 3352 SExtInst::SExtInst(Value *S, Type *Ty, const Twine &Name, 3353 InsertPosition InsertBefore) 3354 : CastInst(Ty, SExt, S, Name, InsertBefore) { 3355 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt"); 3356 } 3357 3358 FPTruncInst::FPTruncInst(Value *S, Type *Ty, const Twine &Name, 3359 InsertPosition InsertBefore) 3360 : CastInst(Ty, FPTrunc, S, Name, InsertBefore) { 3361 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc"); 3362 } 3363 3364 FPExtInst::FPExtInst(Value *S, Type *Ty, const Twine &Name, 3365 InsertPosition InsertBefore) 3366 : CastInst(Ty, FPExt, S, Name, InsertBefore) { 3367 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt"); 3368 } 3369 3370 UIToFPInst::UIToFPInst(Value *S, Type *Ty, const Twine &Name, 3371 InsertPosition InsertBefore) 3372 : CastInst(Ty, UIToFP, S, Name, InsertBefore) { 3373 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP"); 3374 } 3375 3376 SIToFPInst::SIToFPInst(Value *S, Type *Ty, const Twine &Name, 3377 InsertPosition InsertBefore) 3378 : CastInst(Ty, SIToFP, S, Name, InsertBefore) { 3379 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP"); 3380 } 3381 3382 FPToUIInst::FPToUIInst(Value *S, Type *Ty, const Twine &Name, 3383 InsertPosition InsertBefore) 3384 : CastInst(Ty, FPToUI, S, Name, InsertBefore) { 3385 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI"); 3386 } 3387 3388 FPToSIInst::FPToSIInst(Value *S, Type *Ty, const Twine &Name, 3389 InsertPosition InsertBefore) 3390 : CastInst(Ty, FPToSI, S, Name, InsertBefore) { 3391 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI"); 3392 } 3393 3394 PtrToIntInst::PtrToIntInst(Value *S, Type *Ty, const Twine &Name, 3395 InsertPosition InsertBefore) 3396 : CastInst(Ty, PtrToInt, S, Name, InsertBefore) { 3397 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt"); 3398 } 3399 3400 IntToPtrInst::IntToPtrInst(Value *S, Type *Ty, const Twine &Name, 3401 InsertPosition InsertBefore) 3402 : CastInst(Ty, IntToPtr, S, Name, InsertBefore) { 3403 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr"); 3404 } 3405 3406 BitCastInst::BitCastInst(Value *S, Type *Ty, const Twine &Name, 3407 InsertPosition InsertBefore) 3408 : CastInst(Ty, BitCast, S, Name, InsertBefore) { 3409 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast"); 3410 } 3411 3412 AddrSpaceCastInst::AddrSpaceCastInst(Value *S, Type *Ty, const Twine &Name, 3413 InsertPosition InsertBefore) 3414 : CastInst(Ty, AddrSpaceCast, S, Name, InsertBefore) { 3415 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast"); 3416 } 3417 3418 //===----------------------------------------------------------------------===// 3419 // CmpInst Classes 3420 //===----------------------------------------------------------------------===// 3421 3422 CmpInst::CmpInst(Type *ty, OtherOps op, Predicate predicate, Value *LHS, 3423 Value *RHS, const Twine &Name, InsertPosition InsertBefore, 3424 Instruction *FlagsSource) 3425 : Instruction(ty, op, OperandTraits<CmpInst>::op_begin(this), 3426 OperandTraits<CmpInst>::operands(this), InsertBefore) { 3427 Op<0>() = LHS; 3428 Op<1>() = RHS; 3429 setPredicate((Predicate)predicate); 3430 setName(Name); 3431 if (FlagsSource) 3432 copyIRFlags(FlagsSource); 3433 } 3434 3435 CmpInst *CmpInst::Create(OtherOps Op, Predicate predicate, Value *S1, Value *S2, 3436 const Twine &Name, InsertPosition InsertBefore) { 3437 if (Op == Instruction::ICmp) { 3438 if (InsertBefore.isValid()) 3439 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate), 3440 S1, S2, Name); 3441 else 3442 return new ICmpInst(CmpInst::Predicate(predicate), 3443 S1, S2, Name); 3444 } 3445 3446 if (InsertBefore.isValid()) 3447 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate), 3448 S1, S2, Name); 3449 else 3450 return new FCmpInst(CmpInst::Predicate(predicate), 3451 S1, S2, Name); 3452 } 3453 3454 CmpInst *CmpInst::CreateWithCopiedFlags(OtherOps Op, Predicate Pred, Value *S1, 3455 Value *S2, 3456 const Instruction *FlagsSource, 3457 const Twine &Name, 3458 InsertPosition InsertBefore) { 3459 CmpInst *Inst = Create(Op, Pred, S1, S2, Name, InsertBefore); 3460 Inst->copyIRFlags(FlagsSource); 3461 return Inst; 3462 } 3463 3464 void CmpInst::swapOperands() { 3465 if (ICmpInst *IC = dyn_cast<ICmpInst>(this)) 3466 IC->swapOperands(); 3467 else 3468 cast<FCmpInst>(this)->swapOperands(); 3469 } 3470 3471 bool CmpInst::isCommutative() const { 3472 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this)) 3473 return IC->isCommutative(); 3474 return cast<FCmpInst>(this)->isCommutative(); 3475 } 3476 3477 bool CmpInst::isEquality(Predicate P) { 3478 if (ICmpInst::isIntPredicate(P)) 3479 return ICmpInst::isEquality(P); 3480 if (FCmpInst::isFPPredicate(P)) 3481 return FCmpInst::isEquality(P); 3482 llvm_unreachable("Unsupported predicate kind"); 3483 } 3484 3485 CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) { 3486 switch (pred) { 3487 default: llvm_unreachable("Unknown cmp predicate!"); 3488 case ICMP_EQ: return ICMP_NE; 3489 case ICMP_NE: return ICMP_EQ; 3490 case ICMP_UGT: return ICMP_ULE; 3491 case ICMP_ULT: return ICMP_UGE; 3492 case ICMP_UGE: return ICMP_ULT; 3493 case ICMP_ULE: return ICMP_UGT; 3494 case ICMP_SGT: return ICMP_SLE; 3495 case ICMP_SLT: return ICMP_SGE; 3496 case ICMP_SGE: return ICMP_SLT; 3497 case ICMP_SLE: return ICMP_SGT; 3498 3499 case FCMP_OEQ: return FCMP_UNE; 3500 case FCMP_ONE: return FCMP_UEQ; 3501 case FCMP_OGT: return FCMP_ULE; 3502 case FCMP_OLT: return FCMP_UGE; 3503 case FCMP_OGE: return FCMP_ULT; 3504 case FCMP_OLE: return FCMP_UGT; 3505 case FCMP_UEQ: return FCMP_ONE; 3506 case FCMP_UNE: return FCMP_OEQ; 3507 case FCMP_UGT: return FCMP_OLE; 3508 case FCMP_ULT: return FCMP_OGE; 3509 case FCMP_UGE: return FCMP_OLT; 3510 case FCMP_ULE: return FCMP_OGT; 3511 case FCMP_ORD: return FCMP_UNO; 3512 case FCMP_UNO: return FCMP_ORD; 3513 case FCMP_TRUE: return FCMP_FALSE; 3514 case FCMP_FALSE: return FCMP_TRUE; 3515 } 3516 } 3517 3518 StringRef CmpInst::getPredicateName(Predicate Pred) { 3519 switch (Pred) { 3520 default: return "unknown"; 3521 case FCmpInst::FCMP_FALSE: return "false"; 3522 case FCmpInst::FCMP_OEQ: return "oeq"; 3523 case FCmpInst::FCMP_OGT: return "ogt"; 3524 case FCmpInst::FCMP_OGE: return "oge"; 3525 case FCmpInst::FCMP_OLT: return "olt"; 3526 case FCmpInst::FCMP_OLE: return "ole"; 3527 case FCmpInst::FCMP_ONE: return "one"; 3528 case FCmpInst::FCMP_ORD: return "ord"; 3529 case FCmpInst::FCMP_UNO: return "uno"; 3530 case FCmpInst::FCMP_UEQ: return "ueq"; 3531 case FCmpInst::FCMP_UGT: return "ugt"; 3532 case FCmpInst::FCMP_UGE: return "uge"; 3533 case FCmpInst::FCMP_ULT: return "ult"; 3534 case FCmpInst::FCMP_ULE: return "ule"; 3535 case FCmpInst::FCMP_UNE: return "une"; 3536 case FCmpInst::FCMP_TRUE: return "true"; 3537 case ICmpInst::ICMP_EQ: return "eq"; 3538 case ICmpInst::ICMP_NE: return "ne"; 3539 case ICmpInst::ICMP_SGT: return "sgt"; 3540 case ICmpInst::ICMP_SGE: return "sge"; 3541 case ICmpInst::ICMP_SLT: return "slt"; 3542 case ICmpInst::ICMP_SLE: return "sle"; 3543 case ICmpInst::ICMP_UGT: return "ugt"; 3544 case ICmpInst::ICMP_UGE: return "uge"; 3545 case ICmpInst::ICMP_ULT: return "ult"; 3546 case ICmpInst::ICMP_ULE: return "ule"; 3547 } 3548 } 3549 3550 raw_ostream &llvm::operator<<(raw_ostream &OS, CmpInst::Predicate Pred) { 3551 OS << CmpInst::getPredicateName(Pred); 3552 return OS; 3553 } 3554 3555 ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) { 3556 switch (pred) { 3557 default: llvm_unreachable("Unknown icmp predicate!"); 3558 case ICMP_EQ: case ICMP_NE: 3559 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE: 3560 return pred; 3561 case ICMP_UGT: return ICMP_SGT; 3562 case ICMP_ULT: return ICMP_SLT; 3563 case ICMP_UGE: return ICMP_SGE; 3564 case ICMP_ULE: return ICMP_SLE; 3565 } 3566 } 3567 3568 ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) { 3569 switch (pred) { 3570 default: llvm_unreachable("Unknown icmp predicate!"); 3571 case ICMP_EQ: case ICMP_NE: 3572 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE: 3573 return pred; 3574 case ICMP_SGT: return ICMP_UGT; 3575 case ICMP_SLT: return ICMP_ULT; 3576 case ICMP_SGE: return ICMP_UGE; 3577 case ICMP_SLE: return ICMP_ULE; 3578 } 3579 } 3580 3581 CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) { 3582 switch (pred) { 3583 default: llvm_unreachable("Unknown cmp predicate!"); 3584 case ICMP_EQ: case ICMP_NE: 3585 return pred; 3586 case ICMP_SGT: return ICMP_SLT; 3587 case ICMP_SLT: return ICMP_SGT; 3588 case ICMP_SGE: return ICMP_SLE; 3589 case ICMP_SLE: return ICMP_SGE; 3590 case ICMP_UGT: return ICMP_ULT; 3591 case ICMP_ULT: return ICMP_UGT; 3592 case ICMP_UGE: return ICMP_ULE; 3593 case ICMP_ULE: return ICMP_UGE; 3594 3595 case FCMP_FALSE: case FCMP_TRUE: 3596 case FCMP_OEQ: case FCMP_ONE: 3597 case FCMP_UEQ: case FCMP_UNE: 3598 case FCMP_ORD: case FCMP_UNO: 3599 return pred; 3600 case FCMP_OGT: return FCMP_OLT; 3601 case FCMP_OLT: return FCMP_OGT; 3602 case FCMP_OGE: return FCMP_OLE; 3603 case FCMP_OLE: return FCMP_OGE; 3604 case FCMP_UGT: return FCMP_ULT; 3605 case FCMP_ULT: return FCMP_UGT; 3606 case FCMP_UGE: return FCMP_ULE; 3607 case FCMP_ULE: return FCMP_UGE; 3608 } 3609 } 3610 3611 bool CmpInst::isNonStrictPredicate(Predicate pred) { 3612 switch (pred) { 3613 case ICMP_SGE: 3614 case ICMP_SLE: 3615 case ICMP_UGE: 3616 case ICMP_ULE: 3617 case FCMP_OGE: 3618 case FCMP_OLE: 3619 case FCMP_UGE: 3620 case FCMP_ULE: 3621 return true; 3622 default: 3623 return false; 3624 } 3625 } 3626 3627 bool CmpInst::isStrictPredicate(Predicate pred) { 3628 switch (pred) { 3629 case ICMP_SGT: 3630 case ICMP_SLT: 3631 case ICMP_UGT: 3632 case ICMP_ULT: 3633 case FCMP_OGT: 3634 case FCMP_OLT: 3635 case FCMP_UGT: 3636 case FCMP_ULT: 3637 return true; 3638 default: 3639 return false; 3640 } 3641 } 3642 3643 CmpInst::Predicate CmpInst::getStrictPredicate(Predicate pred) { 3644 switch (pred) { 3645 case ICMP_SGE: 3646 return ICMP_SGT; 3647 case ICMP_SLE: 3648 return ICMP_SLT; 3649 case ICMP_UGE: 3650 return ICMP_UGT; 3651 case ICMP_ULE: 3652 return ICMP_ULT; 3653 case FCMP_OGE: 3654 return FCMP_OGT; 3655 case FCMP_OLE: 3656 return FCMP_OLT; 3657 case FCMP_UGE: 3658 return FCMP_UGT; 3659 case FCMP_ULE: 3660 return FCMP_ULT; 3661 default: 3662 return pred; 3663 } 3664 } 3665 3666 CmpInst::Predicate CmpInst::getNonStrictPredicate(Predicate pred) { 3667 switch (pred) { 3668 case ICMP_SGT: 3669 return ICMP_SGE; 3670 case ICMP_SLT: 3671 return ICMP_SLE; 3672 case ICMP_UGT: 3673 return ICMP_UGE; 3674 case ICMP_ULT: 3675 return ICMP_ULE; 3676 case FCMP_OGT: 3677 return FCMP_OGE; 3678 case FCMP_OLT: 3679 return FCMP_OLE; 3680 case FCMP_UGT: 3681 return FCMP_UGE; 3682 case FCMP_ULT: 3683 return FCMP_ULE; 3684 default: 3685 return pred; 3686 } 3687 } 3688 3689 CmpInst::Predicate CmpInst::getFlippedStrictnessPredicate(Predicate pred) { 3690 assert(CmpInst::isRelational(pred) && "Call only with relational predicate!"); 3691 3692 if (isStrictPredicate(pred)) 3693 return getNonStrictPredicate(pred); 3694 if (isNonStrictPredicate(pred)) 3695 return getStrictPredicate(pred); 3696 3697 llvm_unreachable("Unknown predicate!"); 3698 } 3699 3700 CmpInst::Predicate CmpInst::getSignedPredicate(Predicate pred) { 3701 assert(CmpInst::isUnsigned(pred) && "Call only with unsigned predicates!"); 3702 3703 switch (pred) { 3704 default: 3705 llvm_unreachable("Unknown predicate!"); 3706 case CmpInst::ICMP_ULT: 3707 return CmpInst::ICMP_SLT; 3708 case CmpInst::ICMP_ULE: 3709 return CmpInst::ICMP_SLE; 3710 case CmpInst::ICMP_UGT: 3711 return CmpInst::ICMP_SGT; 3712 case CmpInst::ICMP_UGE: 3713 return CmpInst::ICMP_SGE; 3714 } 3715 } 3716 3717 CmpInst::Predicate CmpInst::getUnsignedPredicate(Predicate pred) { 3718 assert(CmpInst::isSigned(pred) && "Call only with signed predicates!"); 3719 3720 switch (pred) { 3721 default: 3722 llvm_unreachable("Unknown predicate!"); 3723 case CmpInst::ICMP_SLT: 3724 return CmpInst::ICMP_ULT; 3725 case CmpInst::ICMP_SLE: 3726 return CmpInst::ICMP_ULE; 3727 case CmpInst::ICMP_SGT: 3728 return CmpInst::ICMP_UGT; 3729 case CmpInst::ICMP_SGE: 3730 return CmpInst::ICMP_UGE; 3731 } 3732 } 3733 3734 bool CmpInst::isUnsigned(Predicate predicate) { 3735 switch (predicate) { 3736 default: return false; 3737 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT: 3738 case ICmpInst::ICMP_UGE: return true; 3739 } 3740 } 3741 3742 bool CmpInst::isSigned(Predicate predicate) { 3743 switch (predicate) { 3744 default: return false; 3745 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT: 3746 case ICmpInst::ICMP_SGE: return true; 3747 } 3748 } 3749 3750 bool ICmpInst::compare(const APInt &LHS, const APInt &RHS, 3751 ICmpInst::Predicate Pred) { 3752 assert(ICmpInst::isIntPredicate(Pred) && "Only for integer predicates!"); 3753 switch (Pred) { 3754 case ICmpInst::Predicate::ICMP_EQ: 3755 return LHS.eq(RHS); 3756 case ICmpInst::Predicate::ICMP_NE: 3757 return LHS.ne(RHS); 3758 case ICmpInst::Predicate::ICMP_UGT: 3759 return LHS.ugt(RHS); 3760 case ICmpInst::Predicate::ICMP_UGE: 3761 return LHS.uge(RHS); 3762 case ICmpInst::Predicate::ICMP_ULT: 3763 return LHS.ult(RHS); 3764 case ICmpInst::Predicate::ICMP_ULE: 3765 return LHS.ule(RHS); 3766 case ICmpInst::Predicate::ICMP_SGT: 3767 return LHS.sgt(RHS); 3768 case ICmpInst::Predicate::ICMP_SGE: 3769 return LHS.sge(RHS); 3770 case ICmpInst::Predicate::ICMP_SLT: 3771 return LHS.slt(RHS); 3772 case ICmpInst::Predicate::ICMP_SLE: 3773 return LHS.sle(RHS); 3774 default: 3775 llvm_unreachable("Unexpected non-integer predicate."); 3776 }; 3777 } 3778 3779 bool FCmpInst::compare(const APFloat &LHS, const APFloat &RHS, 3780 FCmpInst::Predicate Pred) { 3781 APFloat::cmpResult R = LHS.compare(RHS); 3782 switch (Pred) { 3783 default: 3784 llvm_unreachable("Invalid FCmp Predicate"); 3785 case FCmpInst::FCMP_FALSE: 3786 return false; 3787 case FCmpInst::FCMP_TRUE: 3788 return true; 3789 case FCmpInst::FCMP_UNO: 3790 return R == APFloat::cmpUnordered; 3791 case FCmpInst::FCMP_ORD: 3792 return R != APFloat::cmpUnordered; 3793 case FCmpInst::FCMP_UEQ: 3794 return R == APFloat::cmpUnordered || R == APFloat::cmpEqual; 3795 case FCmpInst::FCMP_OEQ: 3796 return R == APFloat::cmpEqual; 3797 case FCmpInst::FCMP_UNE: 3798 return R != APFloat::cmpEqual; 3799 case FCmpInst::FCMP_ONE: 3800 return R == APFloat::cmpLessThan || R == APFloat::cmpGreaterThan; 3801 case FCmpInst::FCMP_ULT: 3802 return R == APFloat::cmpUnordered || R == APFloat::cmpLessThan; 3803 case FCmpInst::FCMP_OLT: 3804 return R == APFloat::cmpLessThan; 3805 case FCmpInst::FCMP_UGT: 3806 return R == APFloat::cmpUnordered || R == APFloat::cmpGreaterThan; 3807 case FCmpInst::FCMP_OGT: 3808 return R == APFloat::cmpGreaterThan; 3809 case FCmpInst::FCMP_ULE: 3810 return R != APFloat::cmpGreaterThan; 3811 case FCmpInst::FCMP_OLE: 3812 return R == APFloat::cmpLessThan || R == APFloat::cmpEqual; 3813 case FCmpInst::FCMP_UGE: 3814 return R != APFloat::cmpLessThan; 3815 case FCmpInst::FCMP_OGE: 3816 return R == APFloat::cmpGreaterThan || R == APFloat::cmpEqual; 3817 } 3818 } 3819 3820 CmpInst::Predicate CmpInst::getFlippedSignednessPredicate(Predicate pred) { 3821 assert(CmpInst::isRelational(pred) && 3822 "Call only with non-equality predicates!"); 3823 3824 if (isSigned(pred)) 3825 return getUnsignedPredicate(pred); 3826 if (isUnsigned(pred)) 3827 return getSignedPredicate(pred); 3828 3829 llvm_unreachable("Unknown predicate!"); 3830 } 3831 3832 bool CmpInst::isOrdered(Predicate predicate) { 3833 switch (predicate) { 3834 default: return false; 3835 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT: 3836 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE: 3837 case FCmpInst::FCMP_ORD: return true; 3838 } 3839 } 3840 3841 bool CmpInst::isUnordered(Predicate predicate) { 3842 switch (predicate) { 3843 default: return false; 3844 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT: 3845 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE: 3846 case FCmpInst::FCMP_UNO: return true; 3847 } 3848 } 3849 3850 bool CmpInst::isTrueWhenEqual(Predicate predicate) { 3851 switch(predicate) { 3852 default: return false; 3853 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE: 3854 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true; 3855 } 3856 } 3857 3858 bool CmpInst::isFalseWhenEqual(Predicate predicate) { 3859 switch(predicate) { 3860 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT: 3861 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true; 3862 default: return false; 3863 } 3864 } 3865 3866 bool CmpInst::isImpliedTrueByMatchingCmp(Predicate Pred1, Predicate Pred2) { 3867 // If the predicates match, then we know the first condition implies the 3868 // second is true. 3869 if (Pred1 == Pred2) 3870 return true; 3871 3872 switch (Pred1) { 3873 default: 3874 break; 3875 case ICMP_EQ: 3876 // A == B implies A >=u B, A <=u B, A >=s B, and A <=s B are true. 3877 return Pred2 == ICMP_UGE || Pred2 == ICMP_ULE || Pred2 == ICMP_SGE || 3878 Pred2 == ICMP_SLE; 3879 case ICMP_UGT: // A >u B implies A != B and A >=u B are true. 3880 return Pred2 == ICMP_NE || Pred2 == ICMP_UGE; 3881 case ICMP_ULT: // A <u B implies A != B and A <=u B are true. 3882 return Pred2 == ICMP_NE || Pred2 == ICMP_ULE; 3883 case ICMP_SGT: // A >s B implies A != B and A >=s B are true. 3884 return Pred2 == ICMP_NE || Pred2 == ICMP_SGE; 3885 case ICMP_SLT: // A <s B implies A != B and A <=s B are true. 3886 return Pred2 == ICMP_NE || Pred2 == ICMP_SLE; 3887 } 3888 return false; 3889 } 3890 3891 bool CmpInst::isImpliedFalseByMatchingCmp(Predicate Pred1, Predicate Pred2) { 3892 return isImpliedTrueByMatchingCmp(Pred1, getInversePredicate(Pred2)); 3893 } 3894 3895 //===----------------------------------------------------------------------===// 3896 // SwitchInst Implementation 3897 //===----------------------------------------------------------------------===// 3898 3899 void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) { 3900 assert(Value && Default && NumReserved); 3901 ReservedSpace = NumReserved; 3902 setNumHungOffUseOperands(2); 3903 allocHungoffUses(ReservedSpace); 3904 3905 Op<0>() = Value; 3906 Op<1>() = Default; 3907 } 3908 3909 /// SwitchInst ctor - Create a new switch instruction, specifying a value to 3910 /// switch on and a default destination. The number of additional cases can 3911 /// be specified here to make memory allocation more efficient. This 3912 /// constructor can also autoinsert before another instruction. 3913 SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases, 3914 InsertPosition InsertBefore) 3915 : Instruction(Type::getVoidTy(Value->getContext()), Instruction::Switch, 3916 nullptr, 0, InsertBefore) { 3917 init(Value, Default, 2+NumCases*2); 3918 } 3919 3920 SwitchInst::SwitchInst(const SwitchInst &SI) 3921 : Instruction(SI.getType(), Instruction::Switch, nullptr, 0) { 3922 init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands()); 3923 setNumHungOffUseOperands(SI.getNumOperands()); 3924 Use *OL = getOperandList(); 3925 const Use *InOL = SI.getOperandList(); 3926 for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) { 3927 OL[i] = InOL[i]; 3928 OL[i+1] = InOL[i+1]; 3929 } 3930 SubclassOptionalData = SI.SubclassOptionalData; 3931 } 3932 3933 /// addCase - Add an entry to the switch instruction... 3934 /// 3935 void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) { 3936 unsigned NewCaseIdx = getNumCases(); 3937 unsigned OpNo = getNumOperands(); 3938 if (OpNo+2 > ReservedSpace) 3939 growOperands(); // Get more space! 3940 // Initialize some new operands. 3941 assert(OpNo+1 < ReservedSpace && "Growing didn't work!"); 3942 setNumHungOffUseOperands(OpNo+2); 3943 CaseHandle Case(this, NewCaseIdx); 3944 Case.setValue(OnVal); 3945 Case.setSuccessor(Dest); 3946 } 3947 3948 /// removeCase - This method removes the specified case and its successor 3949 /// from the switch instruction. 3950 SwitchInst::CaseIt SwitchInst::removeCase(CaseIt I) { 3951 unsigned idx = I->getCaseIndex(); 3952 3953 assert(2 + idx*2 < getNumOperands() && "Case index out of range!!!"); 3954 3955 unsigned NumOps = getNumOperands(); 3956 Use *OL = getOperandList(); 3957 3958 // Overwrite this case with the end of the list. 3959 if (2 + (idx + 1) * 2 != NumOps) { 3960 OL[2 + idx * 2] = OL[NumOps - 2]; 3961 OL[2 + idx * 2 + 1] = OL[NumOps - 1]; 3962 } 3963 3964 // Nuke the last value. 3965 OL[NumOps-2].set(nullptr); 3966 OL[NumOps-2+1].set(nullptr); 3967 setNumHungOffUseOperands(NumOps-2); 3968 3969 return CaseIt(this, idx); 3970 } 3971 3972 /// growOperands - grow operands - This grows the operand list in response 3973 /// to a push_back style of operation. This grows the number of ops by 3 times. 3974 /// 3975 void SwitchInst::growOperands() { 3976 unsigned e = getNumOperands(); 3977 unsigned NumOps = e*3; 3978 3979 ReservedSpace = NumOps; 3980 growHungoffUses(ReservedSpace); 3981 } 3982 3983 MDNode *SwitchInstProfUpdateWrapper::buildProfBranchWeightsMD() { 3984 assert(Changed && "called only if metadata has changed"); 3985 3986 if (!Weights) 3987 return nullptr; 3988 3989 assert(SI.getNumSuccessors() == Weights->size() && 3990 "num of prof branch_weights must accord with num of successors"); 3991 3992 bool AllZeroes = all_of(*Weights, [](uint32_t W) { return W == 0; }); 3993 3994 if (AllZeroes || Weights->size() < 2) 3995 return nullptr; 3996 3997 return MDBuilder(SI.getParent()->getContext()).createBranchWeights(*Weights); 3998 } 3999 4000 void SwitchInstProfUpdateWrapper::init() { 4001 MDNode *ProfileData = getBranchWeightMDNode(SI); 4002 if (!ProfileData) 4003 return; 4004 4005 // FIXME: This check belongs in ProfDataUtils. Its almost equivalent to 4006 // getValidBranchWeightMDNode(), but the need to use llvm_unreachable 4007 // makes them slightly different. 4008 if (ProfileData->getNumOperands() != 4009 SI.getNumSuccessors() + getBranchWeightOffset(ProfileData)) { 4010 llvm_unreachable("number of prof branch_weights metadata operands does " 4011 "not correspond to number of succesors"); 4012 } 4013 4014 SmallVector<uint32_t, 8> Weights; 4015 if (!extractBranchWeights(ProfileData, Weights)) 4016 return; 4017 this->Weights = std::move(Weights); 4018 } 4019 4020 SwitchInst::CaseIt 4021 SwitchInstProfUpdateWrapper::removeCase(SwitchInst::CaseIt I) { 4022 if (Weights) { 4023 assert(SI.getNumSuccessors() == Weights->size() && 4024 "num of prof branch_weights must accord with num of successors"); 4025 Changed = true; 4026 // Copy the last case to the place of the removed one and shrink. 4027 // This is tightly coupled with the way SwitchInst::removeCase() removes 4028 // the cases in SwitchInst::removeCase(CaseIt). 4029 (*Weights)[I->getCaseIndex() + 1] = Weights->back(); 4030 Weights->pop_back(); 4031 } 4032 return SI.removeCase(I); 4033 } 4034 4035 void SwitchInstProfUpdateWrapper::addCase( 4036 ConstantInt *OnVal, BasicBlock *Dest, 4037 SwitchInstProfUpdateWrapper::CaseWeightOpt W) { 4038 SI.addCase(OnVal, Dest); 4039 4040 if (!Weights && W && *W) { 4041 Changed = true; 4042 Weights = SmallVector<uint32_t, 8>(SI.getNumSuccessors(), 0); 4043 (*Weights)[SI.getNumSuccessors() - 1] = *W; 4044 } else if (Weights) { 4045 Changed = true; 4046 Weights->push_back(W.value_or(0)); 4047 } 4048 if (Weights) 4049 assert(SI.getNumSuccessors() == Weights->size() && 4050 "num of prof branch_weights must accord with num of successors"); 4051 } 4052 4053 Instruction::InstListType::iterator 4054 SwitchInstProfUpdateWrapper::eraseFromParent() { 4055 // Instruction is erased. Mark as unchanged to not touch it in the destructor. 4056 Changed = false; 4057 if (Weights) 4058 Weights->resize(0); 4059 return SI.eraseFromParent(); 4060 } 4061 4062 SwitchInstProfUpdateWrapper::CaseWeightOpt 4063 SwitchInstProfUpdateWrapper::getSuccessorWeight(unsigned idx) { 4064 if (!Weights) 4065 return std::nullopt; 4066 return (*Weights)[idx]; 4067 } 4068 4069 void SwitchInstProfUpdateWrapper::setSuccessorWeight( 4070 unsigned idx, SwitchInstProfUpdateWrapper::CaseWeightOpt W) { 4071 if (!W) 4072 return; 4073 4074 if (!Weights && *W) 4075 Weights = SmallVector<uint32_t, 8>(SI.getNumSuccessors(), 0); 4076 4077 if (Weights) { 4078 auto &OldW = (*Weights)[idx]; 4079 if (*W != OldW) { 4080 Changed = true; 4081 OldW = *W; 4082 } 4083 } 4084 } 4085 4086 SwitchInstProfUpdateWrapper::CaseWeightOpt 4087 SwitchInstProfUpdateWrapper::getSuccessorWeight(const SwitchInst &SI, 4088 unsigned idx) { 4089 if (MDNode *ProfileData = getBranchWeightMDNode(SI)) 4090 if (ProfileData->getNumOperands() == SI.getNumSuccessors() + 1) 4091 return mdconst::extract<ConstantInt>(ProfileData->getOperand(idx + 1)) 4092 ->getValue() 4093 .getZExtValue(); 4094 4095 return std::nullopt; 4096 } 4097 4098 //===----------------------------------------------------------------------===// 4099 // IndirectBrInst Implementation 4100 //===----------------------------------------------------------------------===// 4101 4102 void IndirectBrInst::init(Value *Address, unsigned NumDests) { 4103 assert(Address && Address->getType()->isPointerTy() && 4104 "Address of indirectbr must be a pointer"); 4105 ReservedSpace = 1+NumDests; 4106 setNumHungOffUseOperands(1); 4107 allocHungoffUses(ReservedSpace); 4108 4109 Op<0>() = Address; 4110 } 4111 4112 4113 /// growOperands - grow operands - This grows the operand list in response 4114 /// to a push_back style of operation. This grows the number of ops by 2 times. 4115 /// 4116 void IndirectBrInst::growOperands() { 4117 unsigned e = getNumOperands(); 4118 unsigned NumOps = e*2; 4119 4120 ReservedSpace = NumOps; 4121 growHungoffUses(ReservedSpace); 4122 } 4123 4124 IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases, 4125 InsertPosition InsertBefore) 4126 : Instruction(Type::getVoidTy(Address->getContext()), 4127 Instruction::IndirectBr, nullptr, 0, InsertBefore) { 4128 init(Address, NumCases); 4129 } 4130 4131 IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI) 4132 : Instruction(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr, 4133 nullptr, IBI.getNumOperands()) { 4134 allocHungoffUses(IBI.getNumOperands()); 4135 Use *OL = getOperandList(); 4136 const Use *InOL = IBI.getOperandList(); 4137 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i) 4138 OL[i] = InOL[i]; 4139 SubclassOptionalData = IBI.SubclassOptionalData; 4140 } 4141 4142 /// addDestination - Add a destination. 4143 /// 4144 void IndirectBrInst::addDestination(BasicBlock *DestBB) { 4145 unsigned OpNo = getNumOperands(); 4146 if (OpNo+1 > ReservedSpace) 4147 growOperands(); // Get more space! 4148 // Initialize some new operands. 4149 assert(OpNo < ReservedSpace && "Growing didn't work!"); 4150 setNumHungOffUseOperands(OpNo+1); 4151 getOperandList()[OpNo] = DestBB; 4152 } 4153 4154 /// removeDestination - This method removes the specified successor from the 4155 /// indirectbr instruction. 4156 void IndirectBrInst::removeDestination(unsigned idx) { 4157 assert(idx < getNumOperands()-1 && "Successor index out of range!"); 4158 4159 unsigned NumOps = getNumOperands(); 4160 Use *OL = getOperandList(); 4161 4162 // Replace this value with the last one. 4163 OL[idx+1] = OL[NumOps-1]; 4164 4165 // Nuke the last value. 4166 OL[NumOps-1].set(nullptr); 4167 setNumHungOffUseOperands(NumOps-1); 4168 } 4169 4170 //===----------------------------------------------------------------------===// 4171 // FreezeInst Implementation 4172 //===----------------------------------------------------------------------===// 4173 4174 FreezeInst::FreezeInst(Value *S, const Twine &Name, InsertPosition InsertBefore) 4175 : UnaryInstruction(S->getType(), Freeze, S, InsertBefore) { 4176 setName(Name); 4177 } 4178 4179 //===----------------------------------------------------------------------===// 4180 // cloneImpl() implementations 4181 //===----------------------------------------------------------------------===// 4182 4183 // Define these methods here so vtables don't get emitted into every translation 4184 // unit that uses these classes. 4185 4186 GetElementPtrInst *GetElementPtrInst::cloneImpl() const { 4187 return new (getNumOperands()) GetElementPtrInst(*this); 4188 } 4189 4190 UnaryOperator *UnaryOperator::cloneImpl() const { 4191 return Create(getOpcode(), Op<0>()); 4192 } 4193 4194 BinaryOperator *BinaryOperator::cloneImpl() const { 4195 return Create(getOpcode(), Op<0>(), Op<1>()); 4196 } 4197 4198 FCmpInst *FCmpInst::cloneImpl() const { 4199 return new FCmpInst(getPredicate(), Op<0>(), Op<1>()); 4200 } 4201 4202 ICmpInst *ICmpInst::cloneImpl() const { 4203 return new ICmpInst(getPredicate(), Op<0>(), Op<1>()); 4204 } 4205 4206 ExtractValueInst *ExtractValueInst::cloneImpl() const { 4207 return new ExtractValueInst(*this); 4208 } 4209 4210 InsertValueInst *InsertValueInst::cloneImpl() const { 4211 return new InsertValueInst(*this); 4212 } 4213 4214 AllocaInst *AllocaInst::cloneImpl() const { 4215 AllocaInst *Result = new AllocaInst(getAllocatedType(), getAddressSpace(), 4216 getOperand(0), getAlign()); 4217 Result->setUsedWithInAlloca(isUsedWithInAlloca()); 4218 Result->setSwiftError(isSwiftError()); 4219 return Result; 4220 } 4221 4222 LoadInst *LoadInst::cloneImpl() const { 4223 return new LoadInst(getType(), getOperand(0), Twine(), isVolatile(), 4224 getAlign(), getOrdering(), getSyncScopeID()); 4225 } 4226 4227 StoreInst *StoreInst::cloneImpl() const { 4228 return new StoreInst(getOperand(0), getOperand(1), isVolatile(), getAlign(), 4229 getOrdering(), getSyncScopeID()); 4230 } 4231 4232 AtomicCmpXchgInst *AtomicCmpXchgInst::cloneImpl() const { 4233 AtomicCmpXchgInst *Result = new AtomicCmpXchgInst( 4234 getOperand(0), getOperand(1), getOperand(2), getAlign(), 4235 getSuccessOrdering(), getFailureOrdering(), getSyncScopeID()); 4236 Result->setVolatile(isVolatile()); 4237 Result->setWeak(isWeak()); 4238 return Result; 4239 } 4240 4241 AtomicRMWInst *AtomicRMWInst::cloneImpl() const { 4242 AtomicRMWInst *Result = 4243 new AtomicRMWInst(getOperation(), getOperand(0), getOperand(1), 4244 getAlign(), getOrdering(), getSyncScopeID()); 4245 Result->setVolatile(isVolatile()); 4246 return Result; 4247 } 4248 4249 FenceInst *FenceInst::cloneImpl() const { 4250 return new FenceInst(getContext(), getOrdering(), getSyncScopeID()); 4251 } 4252 4253 TruncInst *TruncInst::cloneImpl() const { 4254 return new TruncInst(getOperand(0), getType()); 4255 } 4256 4257 ZExtInst *ZExtInst::cloneImpl() const { 4258 return new ZExtInst(getOperand(0), getType()); 4259 } 4260 4261 SExtInst *SExtInst::cloneImpl() const { 4262 return new SExtInst(getOperand(0), getType()); 4263 } 4264 4265 FPTruncInst *FPTruncInst::cloneImpl() const { 4266 return new FPTruncInst(getOperand(0), getType()); 4267 } 4268 4269 FPExtInst *FPExtInst::cloneImpl() const { 4270 return new FPExtInst(getOperand(0), getType()); 4271 } 4272 4273 UIToFPInst *UIToFPInst::cloneImpl() const { 4274 return new UIToFPInst(getOperand(0), getType()); 4275 } 4276 4277 SIToFPInst *SIToFPInst::cloneImpl() const { 4278 return new SIToFPInst(getOperand(0), getType()); 4279 } 4280 4281 FPToUIInst *FPToUIInst::cloneImpl() const { 4282 return new FPToUIInst(getOperand(0), getType()); 4283 } 4284 4285 FPToSIInst *FPToSIInst::cloneImpl() const { 4286 return new FPToSIInst(getOperand(0), getType()); 4287 } 4288 4289 PtrToIntInst *PtrToIntInst::cloneImpl() const { 4290 return new PtrToIntInst(getOperand(0), getType()); 4291 } 4292 4293 IntToPtrInst *IntToPtrInst::cloneImpl() const { 4294 return new IntToPtrInst(getOperand(0), getType()); 4295 } 4296 4297 BitCastInst *BitCastInst::cloneImpl() const { 4298 return new BitCastInst(getOperand(0), getType()); 4299 } 4300 4301 AddrSpaceCastInst *AddrSpaceCastInst::cloneImpl() const { 4302 return new AddrSpaceCastInst(getOperand(0), getType()); 4303 } 4304 4305 CallInst *CallInst::cloneImpl() const { 4306 if (hasOperandBundles()) { 4307 unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo); 4308 return new(getNumOperands(), DescriptorBytes) CallInst(*this); 4309 } 4310 return new(getNumOperands()) CallInst(*this); 4311 } 4312 4313 SelectInst *SelectInst::cloneImpl() const { 4314 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2)); 4315 } 4316 4317 VAArgInst *VAArgInst::cloneImpl() const { 4318 return new VAArgInst(getOperand(0), getType()); 4319 } 4320 4321 ExtractElementInst *ExtractElementInst::cloneImpl() const { 4322 return ExtractElementInst::Create(getOperand(0), getOperand(1)); 4323 } 4324 4325 InsertElementInst *InsertElementInst::cloneImpl() const { 4326 return InsertElementInst::Create(getOperand(0), getOperand(1), getOperand(2)); 4327 } 4328 4329 ShuffleVectorInst *ShuffleVectorInst::cloneImpl() const { 4330 return new ShuffleVectorInst(getOperand(0), getOperand(1), getShuffleMask()); 4331 } 4332 4333 PHINode *PHINode::cloneImpl() const { return new PHINode(*this); } 4334 4335 LandingPadInst *LandingPadInst::cloneImpl() const { 4336 return new LandingPadInst(*this); 4337 } 4338 4339 ReturnInst *ReturnInst::cloneImpl() const { 4340 return new(getNumOperands()) ReturnInst(*this); 4341 } 4342 4343 BranchInst *BranchInst::cloneImpl() const { 4344 return new(getNumOperands()) BranchInst(*this); 4345 } 4346 4347 SwitchInst *SwitchInst::cloneImpl() const { return new SwitchInst(*this); } 4348 4349 IndirectBrInst *IndirectBrInst::cloneImpl() const { 4350 return new IndirectBrInst(*this); 4351 } 4352 4353 InvokeInst *InvokeInst::cloneImpl() const { 4354 if (hasOperandBundles()) { 4355 unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo); 4356 return new(getNumOperands(), DescriptorBytes) InvokeInst(*this); 4357 } 4358 return new(getNumOperands()) InvokeInst(*this); 4359 } 4360 4361 CallBrInst *CallBrInst::cloneImpl() const { 4362 if (hasOperandBundles()) { 4363 unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo); 4364 return new (getNumOperands(), DescriptorBytes) CallBrInst(*this); 4365 } 4366 return new (getNumOperands()) CallBrInst(*this); 4367 } 4368 4369 ResumeInst *ResumeInst::cloneImpl() const { return new (1) ResumeInst(*this); } 4370 4371 CleanupReturnInst *CleanupReturnInst::cloneImpl() const { 4372 return new (getNumOperands()) CleanupReturnInst(*this); 4373 } 4374 4375 CatchReturnInst *CatchReturnInst::cloneImpl() const { 4376 return new (getNumOperands()) CatchReturnInst(*this); 4377 } 4378 4379 CatchSwitchInst *CatchSwitchInst::cloneImpl() const { 4380 return new CatchSwitchInst(*this); 4381 } 4382 4383 FuncletPadInst *FuncletPadInst::cloneImpl() const { 4384 return new (getNumOperands()) FuncletPadInst(*this); 4385 } 4386 4387 UnreachableInst *UnreachableInst::cloneImpl() const { 4388 LLVMContext &Context = getContext(); 4389 return new UnreachableInst(Context); 4390 } 4391 4392 FreezeInst *FreezeInst::cloneImpl() const { 4393 return new FreezeInst(getOperand(0)); 4394 } 4395