Instruction.cpp revision 341825
1178825Sdfr//===-- Instruction.cpp - Implement the Instruction class -----------------===// 2233294Sstas// 3233294Sstas// The LLVM Compiler Infrastructure 4233294Sstas// 5178825Sdfr// This file is distributed under the University of Illinois Open Source 6233294Sstas// License. See LICENSE.TXT for details. 7178825Sdfr// 8233294Sstas//===----------------------------------------------------------------------===// 9233294Sstas// 10233294Sstas// This file implements the Instruction class for the IR library. 11178825Sdfr// 12233294Sstas//===----------------------------------------------------------------------===// 13233294Sstas 14178825Sdfr#include "llvm/IR/Instruction.h" 15233294Sstas#include "llvm/IR/IntrinsicInst.h" 16233294Sstas#include "llvm/ADT/DenseSet.h" 17233294Sstas#include "llvm/IR/Constants.h" 18178825Sdfr#include "llvm/IR/Instructions.h" 19233294Sstas#include "llvm/IR/MDBuilder.h" 20233294Sstas#include "llvm/IR/Operator.h" 21233294Sstas#include "llvm/IR/Type.h" 22233294Sstasusing namespace llvm; 23233294Sstas 24233294SstasInstruction::Instruction(Type *ty, unsigned it, Use *Ops, unsigned NumOps, 25233294Sstas Instruction *InsertBefore) 26233294Sstas : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(nullptr) { 27233294Sstas 28233294Sstas // If requested, insert this instruction into a basic block... 29233294Sstas if (InsertBefore) { 30233294Sstas BasicBlock *BB = InsertBefore->getParent(); 31233294Sstas assert(BB && "Instruction to insert before is not in a basic block!"); 32233294Sstas BB->getInstList().insert(InsertBefore->getIterator(), this); 33233294Sstas } 34178825Sdfr} 35178825Sdfr 36178825SdfrInstruction::Instruction(Type *ty, unsigned it, Use *Ops, unsigned NumOps, 37178825Sdfr BasicBlock *InsertAtEnd) 38178825Sdfr : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(nullptr) { 39178825Sdfr 40178825Sdfr // append this instruction into the basic block 41178825Sdfr assert(InsertAtEnd && "Basic block to append to may not be NULL!"); 42178825Sdfr InsertAtEnd->getInstList().push_back(this); 43178825Sdfr} 44178825Sdfr 45178825SdfrInstruction::~Instruction() { 46233294Sstas assert(!Parent && "Instruction still linked in the program!"); 47233294Sstas if (hasMetadataHashEntry()) 48178825Sdfr clearMetadataHashEntries(); 49178825Sdfr} 50233294Sstas 51233294Sstas 52178825Sdfrvoid Instruction::setParent(BasicBlock *P) { 53178825Sdfr Parent = P; 54178825Sdfr} 55178825Sdfr 56178825Sdfrconst Module *Instruction::getModule() const { 57178825Sdfr return getParent()->getModule(); 58178825Sdfr} 59178825Sdfr 60178825Sdfrconst Function *Instruction::getFunction() const { 61178825Sdfr return getParent()->getParent(); 62178825Sdfr} 63178825Sdfr 64178825Sdfrvoid Instruction::removeFromParent() { 65233294Sstas getParent()->getInstList().remove(getIterator()); 66178825Sdfr} 67178825Sdfr 68178825Sdfriplist<Instruction>::iterator Instruction::eraseFromParent() { 69178825Sdfr return getParent()->getInstList().erase(getIterator()); 70178825Sdfr} 71178825Sdfr 72178825Sdfr/// Insert an unlinked instruction into a basic block immediately before the 73178825Sdfr/// specified instruction. 74233294Sstasvoid Instruction::insertBefore(Instruction *InsertPos) { 75178825Sdfr InsertPos->getParent()->getInstList().insert(InsertPos->getIterator(), this); 76178825Sdfr} 77178825Sdfr 78233294Sstas/// Insert an unlinked instruction into a basic block immediately after the 79178825Sdfr/// specified instruction. 80233294Sstasvoid Instruction::insertAfter(Instruction *InsertPos) { 81233294Sstas InsertPos->getParent()->getInstList().insertAfter(InsertPos->getIterator(), 82233294Sstas this); 83233294Sstas} 84233294Sstas 85233294Sstas/// Unlink this instruction from its current basic block and insert it into the 86233294Sstas/// basic block that MovePos lives in, right before MovePos. 87233294Sstasvoid Instruction::moveBefore(Instruction *MovePos) { 88233294Sstas moveBefore(*MovePos->getParent(), MovePos->getIterator()); 89178825Sdfr} 90178825Sdfr 91233294Sstasvoid Instruction::moveAfter(Instruction *MovePos) { 92233294Sstas moveBefore(*MovePos->getParent(), ++MovePos->getIterator()); 93178825Sdfr} 94178825Sdfr 95178825Sdfrvoid Instruction::moveBefore(BasicBlock &BB, 96178825Sdfr SymbolTableList<Instruction>::iterator I) { 97178825Sdfr assert(I == BB.end() || I->getParent() == &BB); 98178825Sdfr BB.getInstList().splice(I, getParent()->getInstList(), getIterator()); 99178825Sdfr} 100178825Sdfr 101178825Sdfrvoid Instruction::setHasNoUnsignedWrap(bool b) { 102178825Sdfr cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b); 103178825Sdfr} 104178825Sdfr 105178825Sdfrvoid Instruction::setHasNoSignedWrap(bool b) { 106178825Sdfr cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b); 107178825Sdfr} 108178825Sdfr 109178825Sdfrvoid Instruction::setIsExact(bool b) { 110178825Sdfr cast<PossiblyExactOperator>(this)->setIsExact(b); 111233294Sstas} 112233294Sstas 113233294Sstasbool Instruction::hasNoUnsignedWrap() const { 114233294Sstas return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap(); 115233294Sstas} 116233294Sstas 117233294Sstasbool Instruction::hasNoSignedWrap() const { 118178825Sdfr return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap(); 119233294Sstas} 120233294Sstas 121233294Sstasvoid Instruction::dropPoisonGeneratingFlags() { 122233294Sstas switch (getOpcode()) { 123233294Sstas case Instruction::Add: 124233294Sstas case Instruction::Sub: 125233294Sstas case Instruction::Mul: 126233294Sstas case Instruction::Shl: 127233294Sstas cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(false); 128233294Sstas cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(false); 129233294Sstas break; 130233294Sstas 131233294Sstas case Instruction::UDiv: 132233294Sstas case Instruction::SDiv: 133233294Sstas case Instruction::AShr: 134233294Sstas case Instruction::LShr: 135233294Sstas cast<PossiblyExactOperator>(this)->setIsExact(false); 136233294Sstas break; 137233294Sstas 138233294Sstas case Instruction::GetElementPtr: 139233294Sstas cast<GetElementPtrInst>(this)->setIsInBounds(false); 140233294Sstas break; 141233294Sstas } 142233294Sstas} 143233294Sstas 144233294Sstasbool Instruction::isExact() const { 145233294Sstas return cast<PossiblyExactOperator>(this)->isExact(); 146233294Sstas} 147233294Sstas 148233294Sstasvoid Instruction::setFast(bool B) { 149233294Sstas assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op"); 150233294Sstas cast<FPMathOperator>(this)->setFast(B); 151233294Sstas} 152233294Sstas 153233294Sstasvoid Instruction::setHasAllowReassoc(bool B) { 154233294Sstas assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op"); 155233294Sstas cast<FPMathOperator>(this)->setHasAllowReassoc(B); 156233294Sstas} 157233294Sstas 158233294Sstasvoid Instruction::setHasNoNaNs(bool B) { 159233294Sstas assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op"); 160233294Sstas cast<FPMathOperator>(this)->setHasNoNaNs(B); 161233294Sstas} 162233294Sstas 163233294Sstasvoid Instruction::setHasNoInfs(bool B) { 164178825Sdfr assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op"); 165178825Sdfr cast<FPMathOperator>(this)->setHasNoInfs(B); 166178825Sdfr} 167178825Sdfr 168178825Sdfrvoid Instruction::setHasNoSignedZeros(bool B) { 169178825Sdfr assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op"); 170178825Sdfr cast<FPMathOperator>(this)->setHasNoSignedZeros(B); 171178825Sdfr} 172178825Sdfr 173178825Sdfrvoid Instruction::setHasAllowReciprocal(bool B) { 174178825Sdfr assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op"); 175178825Sdfr cast<FPMathOperator>(this)->setHasAllowReciprocal(B); 176178825Sdfr} 177178825Sdfr 178178825Sdfrvoid Instruction::setHasApproxFunc(bool B) { 179178825Sdfr assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op"); 180178825Sdfr cast<FPMathOperator>(this)->setHasApproxFunc(B); 181233294Sstas} 182178825Sdfr 183178825Sdfrvoid Instruction::setFastMathFlags(FastMathFlags FMF) { 184178825Sdfr assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op"); 185178825Sdfr cast<FPMathOperator>(this)->setFastMathFlags(FMF); 186178825Sdfr} 187178825Sdfr 188178825Sdfrvoid Instruction::copyFastMathFlags(FastMathFlags FMF) { 189178825Sdfr assert(isa<FPMathOperator>(this) && "copying fast-math flag on invalid op"); 190178825Sdfr cast<FPMathOperator>(this)->copyFastMathFlags(FMF); 191178825Sdfr} 192178825Sdfr 193178825Sdfrbool Instruction::isFast() const { 194178825Sdfr assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op"); 195178825Sdfr return cast<FPMathOperator>(this)->isFast(); 196178825Sdfr} 197178825Sdfr 198178825Sdfrbool Instruction::hasAllowReassoc() const { 199178825Sdfr assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op"); 200178825Sdfr return cast<FPMathOperator>(this)->hasAllowReassoc(); 201178825Sdfr} 202178825Sdfr 203178825Sdfrbool Instruction::hasNoNaNs() const { 204178825Sdfr assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op"); 205178825Sdfr return cast<FPMathOperator>(this)->hasNoNaNs(); 206178825Sdfr} 207178825Sdfr 208178825Sdfrbool Instruction::hasNoInfs() const { 209178825Sdfr assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op"); 210178825Sdfr return cast<FPMathOperator>(this)->hasNoInfs(); 211178825Sdfr} 212178825Sdfr 213178825Sdfrbool Instruction::hasNoSignedZeros() const { 214178825Sdfr assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op"); 215178825Sdfr return cast<FPMathOperator>(this)->hasNoSignedZeros(); 216178825Sdfr} 217178825Sdfr 218178825Sdfrbool Instruction::hasAllowReciprocal() const { 219178825Sdfr assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op"); 220178825Sdfr return cast<FPMathOperator>(this)->hasAllowReciprocal(); 221178825Sdfr} 222178825Sdfr 223178825Sdfrbool Instruction::hasAllowContract() const { 224178825Sdfr assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op"); 225178825Sdfr return cast<FPMathOperator>(this)->hasAllowContract(); 226178825Sdfr} 227178825Sdfr 228178825Sdfrbool Instruction::hasApproxFunc() const { 229178825Sdfr assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op"); 230178825Sdfr return cast<FPMathOperator>(this)->hasApproxFunc(); 231178825Sdfr} 232178825Sdfr 233178825SdfrFastMathFlags Instruction::getFastMathFlags() const { 234178825Sdfr assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op"); 235178825Sdfr return cast<FPMathOperator>(this)->getFastMathFlags(); 236178825Sdfr} 237178825Sdfr 238178825Sdfrvoid Instruction::copyFastMathFlags(const Instruction *I) { 239178825Sdfr copyFastMathFlags(I->getFastMathFlags()); 240178825Sdfr} 241178825Sdfr 242178825Sdfrvoid Instruction::copyIRFlags(const Value *V, bool IncludeWrapFlags) { 243178825Sdfr // Copy the wrapping flags. 244178825Sdfr if (IncludeWrapFlags && isa<OverflowingBinaryOperator>(this)) { 245178825Sdfr if (auto *OB = dyn_cast<OverflowingBinaryOperator>(V)) { 246178825Sdfr setHasNoSignedWrap(OB->hasNoSignedWrap()); 247178825Sdfr setHasNoUnsignedWrap(OB->hasNoUnsignedWrap()); 248178825Sdfr } 249178825Sdfr } 250178825Sdfr 251178825Sdfr // Copy the exact flag. 252178825Sdfr if (auto *PE = dyn_cast<PossiblyExactOperator>(V)) 253178825Sdfr if (isa<PossiblyExactOperator>(this)) 254233294Sstas setIsExact(PE->isExact()); 255233294Sstas 256233294Sstas // Copy the fast-math flags. 257233294Sstas if (auto *FP = dyn_cast<FPMathOperator>(V)) 258178825Sdfr if (isa<FPMathOperator>(this)) 259233294Sstas copyFastMathFlags(FP->getFastMathFlags()); 260178825Sdfr 261178825Sdfr if (auto *SrcGEP = dyn_cast<GetElementPtrInst>(V)) 262178825Sdfr if (auto *DestGEP = dyn_cast<GetElementPtrInst>(this)) 263233294Sstas DestGEP->setIsInBounds(SrcGEP->isInBounds() | DestGEP->isInBounds()); 264233294Sstas} 265233294Sstas 266233294Sstasvoid Instruction::andIRFlags(const Value *V) { 267178825Sdfr if (auto *OB = dyn_cast<OverflowingBinaryOperator>(V)) { 268233294Sstas if (isa<OverflowingBinaryOperator>(this)) { 269233294Sstas setHasNoSignedWrap(hasNoSignedWrap() & OB->hasNoSignedWrap()); 270178825Sdfr setHasNoUnsignedWrap(hasNoUnsignedWrap() & OB->hasNoUnsignedWrap()); 271178825Sdfr } 272233294Sstas } 273178825Sdfr 274178825Sdfr if (auto *PE = dyn_cast<PossiblyExactOperator>(V)) 275178825Sdfr if (isa<PossiblyExactOperator>(this)) 276178825Sdfr setIsExact(isExact() & PE->isExact()); 277178825Sdfr 278178825Sdfr if (auto *FP = dyn_cast<FPMathOperator>(V)) { 279178825Sdfr if (isa<FPMathOperator>(this)) { 280178825Sdfr FastMathFlags FM = getFastMathFlags(); 281178825Sdfr FM &= FP->getFastMathFlags(); 282178825Sdfr copyFastMathFlags(FM); 283233294Sstas } 284178825Sdfr } 285233294Sstas 286178825Sdfr if (auto *SrcGEP = dyn_cast<GetElementPtrInst>(V)) 287233294Sstas if (auto *DestGEP = dyn_cast<GetElementPtrInst>(this)) 288233294Sstas DestGEP->setIsInBounds(SrcGEP->isInBounds() & DestGEP->isInBounds()); 289233294Sstas} 290233294Sstas 291233294Sstasconst char *Instruction::getOpcodeName(unsigned OpCode) { 292233294Sstas switch (OpCode) { 293233294Sstas // Terminators 294233294Sstas case Ret: return "ret"; 295233294Sstas case Br: return "br"; 296233294Sstas case Switch: return "switch"; 297178825Sdfr case IndirectBr: return "indirectbr"; 298178825Sdfr case Invoke: return "invoke"; 299178825Sdfr case Resume: return "resume"; 300178825Sdfr case Unreachable: return "unreachable"; 301178825Sdfr case CleanupRet: return "cleanupret"; 302178825Sdfr case CatchRet: return "catchret"; 303178825Sdfr case CatchPad: return "catchpad"; 304178825Sdfr case CatchSwitch: return "catchswitch"; 305178825Sdfr 306178825Sdfr // Standard binary operators... 307178825Sdfr case Add: return "add"; 308178825Sdfr case FAdd: return "fadd"; 309178825Sdfr case Sub: return "sub"; 310178825Sdfr case FSub: return "fsub"; 311178825Sdfr case Mul: return "mul"; 312178825Sdfr case FMul: return "fmul"; 313178825Sdfr case UDiv: return "udiv"; 314178825Sdfr case SDiv: return "sdiv"; 315178825Sdfr case FDiv: return "fdiv"; 316178825Sdfr case URem: return "urem"; 317178825Sdfr case SRem: return "srem"; 318178825Sdfr case FRem: return "frem"; 319178825Sdfr 320178825Sdfr // Logical operators... 321178825Sdfr case And: return "and"; 322178825Sdfr case Or : return "or"; 323178825Sdfr case Xor: return "xor"; 324178825Sdfr 325178825Sdfr // Memory instructions... 326178825Sdfr case Alloca: return "alloca"; 327178825Sdfr case Load: return "load"; 328178825Sdfr case Store: return "store"; 329178825Sdfr case AtomicCmpXchg: return "cmpxchg"; 330178825Sdfr case AtomicRMW: return "atomicrmw"; 331178825Sdfr case Fence: return "fence"; 332178825Sdfr case GetElementPtr: return "getelementptr"; 333178825Sdfr 334178825Sdfr // Convert instructions... 335178825Sdfr case Trunc: return "trunc"; 336178825Sdfr case ZExt: return "zext"; 337178825Sdfr case SExt: return "sext"; 338178825Sdfr case FPTrunc: return "fptrunc"; 339178825Sdfr case FPExt: return "fpext"; 340178825Sdfr case FPToUI: return "fptoui"; 341178825Sdfr case FPToSI: return "fptosi"; 342178825Sdfr case UIToFP: return "uitofp"; 343178825Sdfr case SIToFP: return "sitofp"; 344178825Sdfr case IntToPtr: return "inttoptr"; 345178825Sdfr case PtrToInt: return "ptrtoint"; 346178825Sdfr case BitCast: return "bitcast"; 347178825Sdfr case AddrSpaceCast: return "addrspacecast"; 348178825Sdfr 349178825Sdfr // Other instructions... 350178825Sdfr case ICmp: return "icmp"; 351178825Sdfr case FCmp: return "fcmp"; 352178825Sdfr case PHI: return "phi"; 353178825Sdfr case Select: return "select"; 354178825Sdfr case Call: return "call"; 355178825Sdfr case Shl: return "shl"; 356178825Sdfr case LShr: return "lshr"; 357178825Sdfr case AShr: return "ashr"; 358178825Sdfr case VAArg: return "va_arg"; 359178825Sdfr case ExtractElement: return "extractelement"; 360178825Sdfr case InsertElement: return "insertelement"; 361178825Sdfr case ShuffleVector: return "shufflevector"; 362178825Sdfr case ExtractValue: return "extractvalue"; 363178825Sdfr case InsertValue: return "insertvalue"; 364178825Sdfr case LandingPad: return "landingpad"; 365233294Sstas case CleanupPad: return "cleanuppad"; 366178825Sdfr 367178825Sdfr default: return "<Invalid operator> "; 368178825Sdfr } 369178825Sdfr} 370233294Sstas 371178825Sdfr/// Return true if both instructions have the same special state. This must be 372178825Sdfr/// kept in sync with FunctionComparator::cmpOperations in 373178825Sdfr/// lib/Transforms/IPO/MergeFunctions.cpp. 374178825Sdfrstatic bool haveSameSpecialState(const Instruction *I1, const Instruction *I2, 375178825Sdfr bool IgnoreAlignment = false) { 376178825Sdfr assert(I1->getOpcode() == I2->getOpcode() && 377178825Sdfr "Can not compare special state of different instructions"); 378178825Sdfr 379178825Sdfr if (const AllocaInst *AI = dyn_cast<AllocaInst>(I1)) 380178825Sdfr return AI->getAllocatedType() == cast<AllocaInst>(I2)->getAllocatedType() && 381178825Sdfr (AI->getAlignment() == cast<AllocaInst>(I2)->getAlignment() || 382178825Sdfr IgnoreAlignment); 383178825Sdfr if (const LoadInst *LI = dyn_cast<LoadInst>(I1)) 384233294Sstas return LI->isVolatile() == cast<LoadInst>(I2)->isVolatile() && 385178825Sdfr (LI->getAlignment() == cast<LoadInst>(I2)->getAlignment() || 386178825Sdfr IgnoreAlignment) && 387178825Sdfr LI->getOrdering() == cast<LoadInst>(I2)->getOrdering() && 388178825Sdfr LI->getSyncScopeID() == cast<LoadInst>(I2)->getSyncScopeID(); 389178825Sdfr if (const StoreInst *SI = dyn_cast<StoreInst>(I1)) 390178825Sdfr return SI->isVolatile() == cast<StoreInst>(I2)->isVolatile() && 391178825Sdfr (SI->getAlignment() == cast<StoreInst>(I2)->getAlignment() || 392178825Sdfr IgnoreAlignment) && 393178825Sdfr SI->getOrdering() == cast<StoreInst>(I2)->getOrdering() && 394178825Sdfr SI->getSyncScopeID() == cast<StoreInst>(I2)->getSyncScopeID(); 395178825Sdfr if (const CmpInst *CI = dyn_cast<CmpInst>(I1)) 396233294Sstas return CI->getPredicate() == cast<CmpInst>(I2)->getPredicate(); 397178825Sdfr if (const CallInst *CI = dyn_cast<CallInst>(I1)) 398178825Sdfr return CI->isTailCall() == cast<CallInst>(I2)->isTailCall() && 399178825Sdfr CI->getCallingConv() == cast<CallInst>(I2)->getCallingConv() && 400178825Sdfr CI->getAttributes() == cast<CallInst>(I2)->getAttributes() && 401178825Sdfr CI->hasIdenticalOperandBundleSchema(*cast<CallInst>(I2)); 402178825Sdfr if (const InvokeInst *CI = dyn_cast<InvokeInst>(I1)) 403178825Sdfr return CI->getCallingConv() == cast<InvokeInst>(I2)->getCallingConv() && 404178825Sdfr CI->getAttributes() == cast<InvokeInst>(I2)->getAttributes() && 405178825Sdfr CI->hasIdenticalOperandBundleSchema(*cast<InvokeInst>(I2)); 406178825Sdfr if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(I1)) 407178825Sdfr return IVI->getIndices() == cast<InsertValueInst>(I2)->getIndices(); 408178825Sdfr if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(I1)) 409233294Sstas return EVI->getIndices() == cast<ExtractValueInst>(I2)->getIndices(); 410233294Sstas if (const FenceInst *FI = dyn_cast<FenceInst>(I1)) 411178825Sdfr return FI->getOrdering() == cast<FenceInst>(I2)->getOrdering() && 412233294Sstas FI->getSyncScopeID() == cast<FenceInst>(I2)->getSyncScopeID(); 413178825Sdfr if (const AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(I1)) 414233294Sstas return CXI->isVolatile() == cast<AtomicCmpXchgInst>(I2)->isVolatile() && 415178825Sdfr CXI->isWeak() == cast<AtomicCmpXchgInst>(I2)->isWeak() && 416233294Sstas CXI->getSuccessOrdering() == 417178825Sdfr cast<AtomicCmpXchgInst>(I2)->getSuccessOrdering() && 418233294Sstas CXI->getFailureOrdering() == 419233294Sstas cast<AtomicCmpXchgInst>(I2)->getFailureOrdering() && 420233294Sstas CXI->getSyncScopeID() == 421233294Sstas cast<AtomicCmpXchgInst>(I2)->getSyncScopeID(); 422233294Sstas if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(I1)) 423233294Sstas return RMWI->getOperation() == cast<AtomicRMWInst>(I2)->getOperation() && 424233294Sstas RMWI->isVolatile() == cast<AtomicRMWInst>(I2)->isVolatile() && 425233294Sstas RMWI->getOrdering() == cast<AtomicRMWInst>(I2)->getOrdering() && 426178825Sdfr RMWI->getSyncScopeID() == cast<AtomicRMWInst>(I2)->getSyncScopeID(); 427178825Sdfr 428178825Sdfr return true; 429178825Sdfr} 430178825Sdfr 431178825Sdfrbool Instruction::isIdenticalTo(const Instruction *I) const { 432178825Sdfr return isIdenticalToWhenDefined(I) && 433178825Sdfr SubclassOptionalData == I->SubclassOptionalData; 434178825Sdfr} 435178825Sdfr 436178825Sdfrbool Instruction::isIdenticalToWhenDefined(const Instruction *I) const { 437178825Sdfr if (getOpcode() != I->getOpcode() || 438178825Sdfr getNumOperands() != I->getNumOperands() || 439178825Sdfr getType() != I->getType()) 440178825Sdfr return false; 441178825Sdfr 442178825Sdfr // If both instructions have no operands, they are identical. 443178825Sdfr if (getNumOperands() == 0 && I->getNumOperands() == 0) 444178825Sdfr return haveSameSpecialState(this, I); 445178825Sdfr 446178825Sdfr // We have two instructions of identical opcode and #operands. Check to see 447178825Sdfr // if all operands are the same. 448178825Sdfr if (!std::equal(op_begin(), op_end(), I->op_begin())) 449178825Sdfr return false; 450178825Sdfr 451178825Sdfr if (const PHINode *thisPHI = dyn_cast<PHINode>(this)) { 452178825Sdfr const PHINode *otherPHI = cast<PHINode>(I); 453178825Sdfr return std::equal(thisPHI->block_begin(), thisPHI->block_end(), 454178825Sdfr otherPHI->block_begin()); 455178825Sdfr } 456178825Sdfr 457178825Sdfr return haveSameSpecialState(this, I); 458178825Sdfr} 459178825Sdfr 460233294Sstas// Keep this in sync with FunctionComparator::cmpOperations in 461233294Sstas// lib/Transforms/IPO/MergeFunctions.cpp. 462233294Sstasbool Instruction::isSameOperationAs(const Instruction *I, 463233294Sstas unsigned flags) const { 464178825Sdfr bool IgnoreAlignment = flags & CompareIgnoringAlignment; 465233294Sstas bool UseScalarTypes = flags & CompareUsingScalarTypes; 466233294Sstas 467233294Sstas if (getOpcode() != I->getOpcode() || 468233294Sstas getNumOperands() != I->getNumOperands() || 469233294Sstas (UseScalarTypes ? 470233294Sstas getType()->getScalarType() != I->getType()->getScalarType() : 471233294Sstas getType() != I->getType())) 472233294Sstas return false; 473233294Sstas 474233294Sstas // We have two instructions of identical opcode and #operands. Check to see 475233294Sstas // if all operands are the same type 476233294Sstas for (unsigned i = 0, e = getNumOperands(); i != e; ++i) 477233294Sstas if (UseScalarTypes ? 478233294Sstas getOperand(i)->getType()->getScalarType() != 479233294Sstas I->getOperand(i)->getType()->getScalarType() : 480233294Sstas getOperand(i)->getType() != I->getOperand(i)->getType()) 481233294Sstas return false; 482233294Sstas 483233294Sstas return haveSameSpecialState(this, I, IgnoreAlignment); 484233294Sstas} 485233294Sstas 486233294Sstasbool Instruction::isUsedOutsideOfBlock(const BasicBlock *BB) const { 487233294Sstas for (const Use &U : uses()) { 488233294Sstas // PHI nodes uses values in the corresponding predecessor block. For other 489233294Sstas // instructions, just check to see whether the parent of the use matches up. 490233294Sstas const Instruction *I = cast<Instruction>(U.getUser()); 491233294Sstas const PHINode *PN = dyn_cast<PHINode>(I); 492233294Sstas if (!PN) { 493233294Sstas if (I->getParent() != BB) 494233294Sstas return true; 495233294Sstas continue; 496233294Sstas } 497233294Sstas 498233294Sstas if (PN->getIncomingBlock(U) != BB) 499233294Sstas return true; 500233294Sstas } 501233294Sstas return false; 502233294Sstas} 503233294Sstas 504233294Sstasbool Instruction::mayReadFromMemory() const { 505233294Sstas switch (getOpcode()) { 506233294Sstas default: return false; 507233294Sstas case Instruction::VAArg: 508233294Sstas case Instruction::Load: 509178825Sdfr case Instruction::Fence: // FIXME: refine definition of mayReadFromMemory 510178825Sdfr case Instruction::AtomicCmpXchg: 511178825Sdfr case Instruction::AtomicRMW: 512178825Sdfr case Instruction::CatchPad: 513178825Sdfr case Instruction::CatchRet: 514178825Sdfr return true; 515178825Sdfr case Instruction::Call: 516178825Sdfr return !cast<CallInst>(this)->doesNotAccessMemory(); 517178825Sdfr case Instruction::Invoke: 518178825Sdfr return !cast<InvokeInst>(this)->doesNotAccessMemory(); 519178825Sdfr case Instruction::Store: 520178825Sdfr return !cast<StoreInst>(this)->isUnordered(); 521178825Sdfr } 522178825Sdfr} 523178825Sdfr 524178825Sdfrbool Instruction::mayWriteToMemory() const { 525178825Sdfr switch (getOpcode()) { 526178825Sdfr default: return false; 527178825Sdfr case Instruction::Fence: // FIXME: refine definition of mayWriteToMemory 528178825Sdfr case Instruction::Store: 529178825Sdfr case Instruction::VAArg: 530178825Sdfr case Instruction::AtomicCmpXchg: 531178825Sdfr case Instruction::AtomicRMW: 532178825Sdfr case Instruction::CatchPad: 533178825Sdfr case Instruction::CatchRet: 534178825Sdfr return true; 535178825Sdfr case Instruction::Call: 536178825Sdfr return !cast<CallInst>(this)->onlyReadsMemory(); 537233294Sstas case Instruction::Invoke: 538178825Sdfr return !cast<InvokeInst>(this)->onlyReadsMemory(); 539178825Sdfr case Instruction::Load: 540178825Sdfr return !cast<LoadInst>(this)->isUnordered(); 541178825Sdfr } 542233294Sstas} 543178825Sdfr 544178825Sdfrbool Instruction::isAtomic() const { 545178825Sdfr switch (getOpcode()) { 546178825Sdfr default: 547178825Sdfr return false; 548178825Sdfr case Instruction::AtomicCmpXchg: 549178825Sdfr case Instruction::AtomicRMW: 550178825Sdfr case Instruction::Fence: 551178825Sdfr return true; 552233294Sstas case Instruction::Load: 553178825Sdfr return cast<LoadInst>(this)->getOrdering() != AtomicOrdering::NotAtomic; 554233294Sstas case Instruction::Store: 555178825Sdfr return cast<StoreInst>(this)->getOrdering() != AtomicOrdering::NotAtomic; 556178825Sdfr } 557178825Sdfr} 558178825Sdfr 559178825Sdfrbool Instruction::hasAtomicLoad() const { 560178825Sdfr assert(isAtomic()); 561178825Sdfr switch (getOpcode()) { 562233294Sstas default: 563233294Sstas return false; 564233294Sstas case Instruction::AtomicCmpXchg: 565233294Sstas case Instruction::AtomicRMW: 566178825Sdfr case Instruction::Load: 567178825Sdfr return true; 568233294Sstas } 569233294Sstas} 570178825Sdfr 571178825Sdfrbool Instruction::hasAtomicStore() const { 572178825Sdfr assert(isAtomic()); 573178825Sdfr switch (getOpcode()) { 574178825Sdfr default: 575178825Sdfr return false; 576178825Sdfr case Instruction::AtomicCmpXchg: 577178825Sdfr case Instruction::AtomicRMW: 578178825Sdfr case Instruction::Store: 579178825Sdfr return true; 580178825Sdfr } 581178825Sdfr} 582178825Sdfr 583178825Sdfrbool Instruction::mayThrow() const { 584178825Sdfr if (const CallInst *CI = dyn_cast<CallInst>(this)) 585178825Sdfr return !CI->doesNotThrow(); 586178825Sdfr if (const auto *CRI = dyn_cast<CleanupReturnInst>(this)) 587178825Sdfr return CRI->unwindsToCaller(); 588178825Sdfr if (const auto *CatchSwitch = dyn_cast<CatchSwitchInst>(this)) 589178825Sdfr return CatchSwitch->unwindsToCaller(); 590178825Sdfr return isa<ResumeInst>(this); 591178825Sdfr} 592178825Sdfr 593178825Sdfrbool Instruction::isSafeToRemove() const { 594178825Sdfr return (!isa<CallInst>(this) || !this->mayHaveSideEffects()) && 595178825Sdfr !isa<TerminatorInst>(this); 596233294Sstas} 597178825Sdfr 598178825Sdfrconst Instruction *Instruction::getNextNonDebugInstruction() const { 599178825Sdfr for (const Instruction *I = getNextNode(); I; I = I->getNextNode()) 600178825Sdfr if (!isa<DbgInfoIntrinsic>(I)) 601178825Sdfr return I; 602233294Sstas return nullptr; 603178825Sdfr} 604178825Sdfr 605178825Sdfrbool Instruction::isAssociative() const { 606233294Sstas unsigned Opcode = getOpcode(); 607178825Sdfr if (isAssociative(Opcode)) 608178825Sdfr return true; 609178825Sdfr 610178825Sdfr switch (Opcode) { 611233294Sstas case FMul: 612178825Sdfr case FAdd: 613178825Sdfr return cast<FPMathOperator>(this)->hasAllowReassoc() && 614178825Sdfr cast<FPMathOperator>(this)->hasNoSignedZeros(); 615233294Sstas default: 616233294Sstas return false; 617233294Sstas } 618178825Sdfr} 619233294Sstas 620233294SstasInstruction *Instruction::cloneImpl() const { 621178825Sdfr llvm_unreachable("Subclass of Instruction failed to implement cloneImpl"); 622178825Sdfr} 623178825Sdfr 624233294Sstasvoid Instruction::swapProfMetadata() { 625233294Sstas MDNode *ProfileData = getMetadata(LLVMContext::MD_prof); 626233294Sstas if (!ProfileData || ProfileData->getNumOperands() != 3 || 627178825Sdfr !isa<MDString>(ProfileData->getOperand(0))) 628178825Sdfr return; 629178825Sdfr 630178825Sdfr MDString *MDName = cast<MDString>(ProfileData->getOperand(0)); 631178825Sdfr if (MDName->getString() != "branch_weights") 632178825Sdfr return; 633178825Sdfr 634233294Sstas // The first operand is the name. Fetch them backwards and build a new one. 635178825Sdfr Metadata *Ops[] = {ProfileData->getOperand(0), ProfileData->getOperand(2), 636178825Sdfr ProfileData->getOperand(1)}; 637178825Sdfr setMetadata(LLVMContext::MD_prof, 638233294Sstas MDNode::get(ProfileData->getContext(), Ops)); 639233294Sstas} 640233294Sstas 641233294Sstasvoid Instruction::copyMetadata(const Instruction &SrcInst, 642178825Sdfr ArrayRef<unsigned> WL) { 643178825Sdfr if (!SrcInst.hasMetadata()) 644233294Sstas return; 645178825Sdfr 646178825Sdfr DenseSet<unsigned> WLS; 647178825Sdfr for (unsigned M : WL) 648178825Sdfr WLS.insert(M); 649178825Sdfr 650178825Sdfr // Otherwise, enumerate and copy over metadata from the old instruction to the 651178825Sdfr // new one. 652178825Sdfr SmallVector<std::pair<unsigned, MDNode *>, 4> TheMDs; 653178825Sdfr SrcInst.getAllMetadataOtherThanDebugLoc(TheMDs); 654178825Sdfr for (const auto &MD : TheMDs) { 655178825Sdfr if (WL.empty() || WLS.count(MD.first)) 656178825Sdfr setMetadata(MD.first, MD.second); 657178825Sdfr } 658178825Sdfr if (WL.empty() || WLS.count(LLVMContext::MD_dbg)) 659178825Sdfr setDebugLoc(SrcInst.getDebugLoc()); 660178825Sdfr} 661178825Sdfr 662178825SdfrInstruction *Instruction::clone() const { 663178825Sdfr Instruction *New = nullptr; 664178825Sdfr switch (getOpcode()) { 665178825Sdfr default: 666178825Sdfr llvm_unreachable("Unhandled Opcode."); 667178825Sdfr#define HANDLE_INST(num, opc, clas) \ 668178825Sdfr case Instruction::opc: \ 669178825Sdfr New = cast<clas>(this)->cloneImpl(); \ 670178825Sdfr break; 671178825Sdfr#include "llvm/IR/Instruction.def" 672178825Sdfr#undef HANDLE_INST 673178825Sdfr } 674178825Sdfr 675178825Sdfr New->SubclassOptionalData = SubclassOptionalData; 676178825Sdfr New->copyMetadata(*this); 677178825Sdfr return New; 678178825Sdfr} 679178825Sdfr 680178825Sdfrvoid Instruction::updateProfWeight(uint64_t S, uint64_t T) { 681178825Sdfr auto *ProfileData = getMetadata(LLVMContext::MD_prof); 682178825Sdfr if (ProfileData == nullptr) 683178825Sdfr return; 684178825Sdfr 685178825Sdfr auto *ProfDataName = dyn_cast<MDString>(ProfileData->getOperand(0)); 686178825Sdfr if (!ProfDataName || (!ProfDataName->getString().equals("branch_weights") && 687178825Sdfr !ProfDataName->getString().equals("VP"))) 688233294Sstas return; 689178825Sdfr 690178825Sdfr MDBuilder MDB(getContext()); 691178825Sdfr SmallVector<Metadata *, 3> Vals; 692178825Sdfr Vals.push_back(ProfileData->getOperand(0)); 693233294Sstas APInt APS(128, S), APT(128, T); 694178825Sdfr if (ProfDataName->getString().equals("branch_weights")) 695178825Sdfr for (unsigned i = 1; i < ProfileData->getNumOperands(); i++) { 696178825Sdfr // Using APInt::div may be expensive, but most cases should fit 64 bits. 697178825Sdfr APInt Val(128, 698178825Sdfr mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(i)) 699178825Sdfr ->getValue() 700178825Sdfr .getZExtValue()); 701178825Sdfr Val *= APS; 702178825Sdfr Vals.push_back(MDB.createConstant( 703178825Sdfr ConstantInt::get(Type::getInt64Ty(getContext()), 704178825Sdfr Val.udiv(APT).getLimitedValue()))); 705178825Sdfr } 706178825Sdfr else if (ProfDataName->getString().equals("VP")) 707233294Sstas for (unsigned i = 1; i < ProfileData->getNumOperands(); i += 2) { 708233294Sstas // The first value is the key of the value profile, which will not change. 709178825Sdfr Vals.push_back(ProfileData->getOperand(i)); 710178825Sdfr // Using APInt::div may be expensive, but most cases should fit 64 bits. 711178825Sdfr APInt Val(128, 712178825Sdfr mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(i + 1)) 713233294Sstas ->getValue() 714233294Sstas .getZExtValue()); 715233294Sstas Val *= APS; 716233294Sstas Vals.push_back(MDB.createConstant( 717178825Sdfr ConstantInt::get(Type::getInt64Ty(getContext()), 718233294Sstas Val.udiv(APT).getLimitedValue()))); 719178825Sdfr } 720178825Sdfr setMetadata(LLVMContext::MD_prof, MDNode::get(getContext(), Vals)); 721178825Sdfr} 722178825Sdfr 723233294Sstasvoid Instruction::setProfWeight(uint64_t W) { 724233294Sstas assert((isa<CallInst>(this) || isa<InvokeInst>(this)) && 725178825Sdfr "Can only set weights for call and invoke instrucitons"); 726178825Sdfr SmallVector<uint32_t, 1> Weights; 727178825Sdfr Weights.push_back(W); 728178825Sdfr MDBuilder MDB(getContext()); 729178825Sdfr setMetadata(LLVMContext::MD_prof, MDB.createBranchWeights(Weights)); 730178825Sdfr} 731178825Sdfr