LCOV - code coverage report
Current view: top level - lib/IR - Instruction.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 373 405 92.1 %
Date: 2017-09-14 15:23:50 Functions: 53 56 94.6 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===-- Instruction.cpp - Implement the Instruction class -----------------===//
       2             : //
       3             : //                     The LLVM Compiler Infrastructure
       4             : //
       5             : // This file is distributed under the University of Illinois Open Source
       6             : // License. See LICENSE.TXT for details.
       7             : //
       8             : //===----------------------------------------------------------------------===//
       9             : //
      10             : // This file implements the Instruction class for the IR library.
      11             : //
      12             : //===----------------------------------------------------------------------===//
      13             : 
      14             : #include "llvm/IR/Instruction.h"
      15             : #include "llvm/ADT/DenseSet.h"
      16             : #include "llvm/IR/CallSite.h"
      17             : #include "llvm/IR/Constants.h"
      18             : #include "llvm/IR/Instructions.h"
      19             : #include "llvm/IR/MDBuilder.h"
      20             : #include "llvm/IR/Module.h"
      21             : #include "llvm/IR/Operator.h"
      22             : #include "llvm/IR/Type.h"
      23             : using namespace llvm;
      24             : 
      25     6842859 : Instruction::Instruction(Type *ty, unsigned it, Use *Ops, unsigned NumOps,
      26     6842859 :                          Instruction *InsertBefore)
      27    27371439 :   : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(nullptr) {
      28             : 
      29             :   // If requested, insert this instruction into a basic block...
      30     6842860 :   if (InsertBefore) {
      31      570448 :     BasicBlock *BB = InsertBefore->getParent();
      32             :     assert(BB && "Instruction to insert before is not in a basic block!");
      33     1140896 :     BB->getInstList().insert(InsertBefore->getIterator(), this);
      34             :   }
      35     6842860 : }
      36             : 
      37      217505 : Instruction::Instruction(Type *ty, unsigned it, Use *Ops, unsigned NumOps,
      38      217505 :                          BasicBlock *InsertAtEnd)
      39      870020 :   : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(nullptr) {
      40             : 
      41             :   // append this instruction into the basic block
      42             :   assert(InsertAtEnd && "Basic block to append to may not be NULL!");
      43      435010 :   InsertAtEnd->getInstList().push_back(this);
      44      217505 : }
      45             : 
      46    23002001 : Instruction::~Instruction() {
      47             :   assert(!Parent && "Instruction still linked in the program!");
      48     5750500 :   if (hasMetadataHashEntry())
      49      281080 :     clearMetadataHashEntries();
      50     5750501 : }
      51             : 
      52             : 
      53    16035586 : void Instruction::setParent(BasicBlock *P) {
      54    16035586 :   Parent = P;
      55    16035586 : }
      56             : 
      57    30450682 : const Module *Instruction::getModule() const {
      58    30450682 :   return getParent()->getModule();
      59             : }
      60             : 
      61     3251877 : const Function *Instruction::getFunction() const {
      62     3251877 :   return getParent()->getParent();
      63             : }
      64             : 
      65      455799 : void Instruction::removeFromParent() {
      66     1367397 :   getParent()->getInstList().remove(getIterator());
      67      455799 : }
      68             : 
      69     1918729 : iplist<Instruction>::iterator Instruction::eraseFromParent() {
      70     5756187 :   return getParent()->getInstList().erase(getIterator());
      71             : }
      72             : 
      73             : /// Insert an unlinked instruction into a basic block immediately before the
      74             : /// specified instruction.
      75       16761 : void Instruction::insertBefore(Instruction *InsertPos) {
      76       50283 :   InsertPos->getParent()->getInstList().insert(InsertPos->getIterator(), this);
      77       16761 : }
      78             : 
      79             : /// Insert an unlinked instruction into a basic block immediately after the
      80             : /// specified instruction.
      81       11842 : void Instruction::insertAfter(Instruction *InsertPos) {
      82       23684 :   InsertPos->getParent()->getInstList().insertAfter(InsertPos->getIterator(),
      83       23684 :                                                     this);
      84       11842 : }
      85             : 
      86             : /// Unlink this instruction from its current basic block and insert it into the
      87             : /// basic block that MovePos lives in, right before MovePos.
      88       21750 : void Instruction::moveBefore(Instruction *MovePos) {
      89       43500 :   moveBefore(*MovePos->getParent(), MovePos->getIterator());
      90       21750 : }
      91             : 
      92        2253 : void Instruction::moveAfter(Instruction *MovePos) {
      93        6759 :   moveBefore(*MovePos->getParent(), ++MovePos->getIterator());
      94        2253 : }
      95             : 
      96       24342 : void Instruction::moveBefore(BasicBlock &BB,
      97             :                              SymbolTableList<Instruction>::iterator I) {
      98             :   assert(I == BB.end() || I->getParent() == &BB);
      99       73026 :   BB.getInstList().splice(I, getParent()->getInstList(), getIterator());
     100       24342 : }
     101             : 
     102      220421 : void Instruction::setHasNoUnsignedWrap(bool b) {
     103      440842 :   cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b);
     104      220421 : }
     105             : 
     106      251361 : void Instruction::setHasNoSignedWrap(bool b) {
     107      502722 :   cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b);
     108      251361 : }
     109             : 
     110        3495 : void Instruction::setIsExact(bool b) {
     111        6990 :   cast<PossiblyExactOperator>(this)->setIsExact(b);
     112        3495 : }
     113             : 
     114     8198012 : bool Instruction::hasNoUnsignedWrap() const {
     115    16396024 :   return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap();
     116             : }
     117             : 
     118     8139454 : bool Instruction::hasNoSignedWrap() const {
     119    16278908 :   return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap();
     120             : }
     121             : 
     122         177 : void Instruction::dropPoisonGeneratingFlags() {
     123         177 :   switch (getOpcode()) {
     124          24 :   case Instruction::Add:
     125             :   case Instruction::Sub:
     126             :   case Instruction::Mul:
     127             :   case Instruction::Shl:
     128          48 :     cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(false);
     129          24 :     cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(false);
     130             :     break;
     131             : 
     132           2 :   case Instruction::UDiv:
     133             :   case Instruction::SDiv:
     134             :   case Instruction::AShr:
     135             :   case Instruction::LShr:
     136           2 :     cast<PossiblyExactOperator>(this)->setIsExact(false);
     137             :     break;
     138             : 
     139           1 :   case Instruction::GetElementPtr:
     140           1 :     cast<GetElementPtrInst>(this)->setIsInBounds(false);
     141           1 :     break;
     142             :   }
     143         177 : }
     144             : 
     145       77010 : bool Instruction::isExact() const {
     146      154020 :   return cast<PossiblyExactOperator>(this)->isExact();
     147             : }
     148             : 
     149           8 : void Instruction::setHasUnsafeAlgebra(bool B) {
     150             :   assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
     151          16 :   cast<FPMathOperator>(this)->setHasUnsafeAlgebra(B);
     152           8 : }
     153             : 
     154           1 : void Instruction::setHasNoNaNs(bool B) {
     155             :   assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
     156           2 :   cast<FPMathOperator>(this)->setHasNoNaNs(B);
     157           1 : }
     158             : 
     159           0 : void Instruction::setHasNoInfs(bool B) {
     160             :   assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
     161           0 :   cast<FPMathOperator>(this)->setHasNoInfs(B);
     162           0 : }
     163             : 
     164           0 : void Instruction::setHasNoSignedZeros(bool B) {
     165             :   assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
     166           0 :   cast<FPMathOperator>(this)->setHasNoSignedZeros(B);
     167           0 : }
     168             : 
     169           2 : void Instruction::setHasAllowReciprocal(bool B) {
     170             :   assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
     171           4 :   cast<FPMathOperator>(this)->setHasAllowReciprocal(B);
     172           2 : }
     173             : 
     174       23490 : void Instruction::setFastMathFlags(FastMathFlags FMF) {
     175             :   assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
     176       46980 :   cast<FPMathOperator>(this)->setFastMathFlags(FMF);
     177       23490 : }
     178             : 
     179        8807 : void Instruction::copyFastMathFlags(FastMathFlags FMF) {
     180             :   assert(isa<FPMathOperator>(this) && "copying fast-math flag on invalid op");
     181       17614 :   cast<FPMathOperator>(this)->copyFastMathFlags(FMF);
     182        8807 : }
     183             : 
     184       30772 : bool Instruction::hasUnsafeAlgebra() const {
     185             :   assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
     186       61544 :   return cast<FPMathOperator>(this)->hasUnsafeAlgebra();
     187             : }
     188             : 
     189       22043 : bool Instruction::hasNoNaNs() const {
     190             :   assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
     191       44086 :   return cast<FPMathOperator>(this)->hasNoNaNs();
     192             : }
     193             : 
     194           6 : bool Instruction::hasNoInfs() const {
     195             :   assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
     196          12 :   return cast<FPMathOperator>(this)->hasNoInfs();
     197             : }
     198             : 
     199        7589 : bool Instruction::hasNoSignedZeros() const {
     200             :   assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
     201       15178 :   return cast<FPMathOperator>(this)->hasNoSignedZeros();
     202             : }
     203             : 
     204        1067 : bool Instruction::hasAllowReciprocal() const {
     205             :   assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
     206        2134 :   return cast<FPMathOperator>(this)->hasAllowReciprocal();
     207             : }
     208             : 
     209           2 : bool Instruction::hasAllowContract() const {
     210             :   assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
     211           4 :   return cast<FPMathOperator>(this)->hasAllowContract();
     212             : }
     213             : 
     214       60389 : FastMathFlags Instruction::getFastMathFlags() const {
     215             :   assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
     216      120778 :   return cast<FPMathOperator>(this)->getFastMathFlags();
     217             : }
     218             : 
     219         369 : void Instruction::copyFastMathFlags(const Instruction *I) {
     220         369 :   copyFastMathFlags(I->getFastMathFlags());
     221         369 : }
     222             : 
     223       70616 : void Instruction::copyIRFlags(const Value *V, bool IncludeWrapFlags) {
     224             :   // Copy the wrapping flags.
     225       70616 :   if (IncludeWrapFlags && isa<OverflowingBinaryOperator>(this)) {
     226       68646 :     if (auto *OB = dyn_cast<OverflowingBinaryOperator>(V)) {
     227       68646 :       setHasNoSignedWrap(OB->hasNoSignedWrap());
     228       68646 :       setHasNoUnsignedWrap(OB->hasNoUnsignedWrap());
     229             :     }
     230             :   }
     231             : 
     232             :   // Copy the exact flag.
     233         152 :   if (auto *PE = dyn_cast<PossiblyExactOperator>(V))
     234         152 :     if (isa<PossiblyExactOperator>(this))
     235         152 :       setIsExact(PE->isExact());
     236             : 
     237             :   // Copy the fast-math flags.
     238        1347 :   if (auto *FP = dyn_cast<FPMathOperator>(V))
     239        2694 :     if (isa<FPMathOperator>(this))
     240        1347 :       copyFastMathFlags(FP->getFastMathFlags());
     241             : 
     242           0 :   if (auto *SrcGEP = dyn_cast<GetElementPtrInst>(V))
     243           0 :     if (auto *DestGEP = dyn_cast<GetElementPtrInst>(this))
     244           0 :       DestGEP->setIsInBounds(SrcGEP->isInBounds() | DestGEP->isInBounds());
     245       70616 : }
     246             : 
     247      227396 : void Instruction::andIRFlags(const Value *V) {
     248      140783 :   if (auto *OB = dyn_cast<OverflowingBinaryOperator>(V)) {
     249      140783 :     if (isa<OverflowingBinaryOperator>(this)) {
     250      281520 :       setHasNoSignedWrap(hasNoSignedWrap() & OB->hasNoSignedWrap());
     251      281520 :       setHasNoUnsignedWrap(hasNoUnsignedWrap() & OB->hasNoUnsignedWrap());
     252             :     }
     253             :   }
     254             : 
     255        1555 :   if (auto *PE = dyn_cast<PossiblyExactOperator>(V))
     256        1555 :     if (isa<PossiblyExactOperator>(this))
     257        3110 :       setIsExact(isExact() & PE->isExact());
     258             : 
     259        7091 :   if (auto *FP = dyn_cast<FPMathOperator>(V)) {
     260       14182 :     if (isa<FPMathOperator>(this)) {
     261        7091 :       FastMathFlags FM = getFastMathFlags();
     262       14182 :       FM &= FP->getFastMathFlags();
     263        7091 :       copyFastMathFlags(FM);
     264             :     }
     265             :   }
     266             : 
     267       25396 :   if (auto *SrcGEP = dyn_cast<GetElementPtrInst>(V))
     268       25390 :     if (auto *DestGEP = dyn_cast<GetElementPtrInst>(this))
     269       25390 :       DestGEP->setIsInBounds(SrcGEP->isInBounds() & DestGEP->isInBounds());
     270      227396 : }
     271             : 
     272     1448614 : const char *Instruction::getOpcodeName(unsigned OpCode) {
     273     1448614 :   switch (OpCode) {
     274             :   // Terminators
     275             :   case Ret:    return "ret";
     276       73134 :   case Br:     return "br";
     277         883 :   case Switch: return "switch";
     278         194 :   case IndirectBr: return "indirectbr";
     279        2359 :   case Invoke: return "invoke";
     280         445 :   case Resume: return "resume";
     281        3160 :   case Unreachable: return "unreachable";
     282         226 :   case CleanupRet: return "cleanupret";
     283         148 :   case CatchRet: return "catchret";
     284         226 :   case CatchPad: return "catchpad";
     285         222 :   case CatchSwitch: return "catchswitch";
     286             : 
     287             :   // Standard binary operators...
     288       32173 :   case Add: return "add";
     289        4746 :   case FAdd: return "fadd";
     290        7032 :   case Sub: return "sub";
     291        1293 :   case FSub: return "fsub";
     292        6625 :   case Mul: return "mul";
     293        2425 :   case FMul: return "fmul";
     294         785 :   case UDiv: return "udiv";
     295        1549 :   case SDiv: return "sdiv";
     296         800 :   case FDiv: return "fdiv";
     297         554 :   case URem: return "urem";
     298         660 :   case SRem: return "srem";
     299         119 :   case FRem: return "frem";
     300             : 
     301             :   // Logical operators...
     302        7558 :   case And: return "and";
     303        4377 :   case Or : return "or";
     304        3919 :   case Xor: return "xor";
     305             : 
     306             :   // Memory instructions...
     307      180678 :   case Alloca:        return "alloca";
     308      246783 :   case Load:          return "load";
     309      231057 :   case Store:         return "store";
     310         652 :   case AtomicCmpXchg: return "cmpxchg";
     311         703 :   case AtomicRMW:     return "atomicrmw";
     312         221 :   case Fence:         return "fence";
     313      126112 :   case GetElementPtr: return "getelementptr";
     314             : 
     315             :   // Convert instructions...
     316        8295 :   case Trunc:         return "trunc";
     317        7896 :   case ZExt:          return "zext";
     318        6994 :   case SExt:          return "sext";
     319         690 :   case FPTrunc:       return "fptrunc";
     320        1080 :   case FPExt:         return "fpext";
     321         743 :   case FPToUI:        return "fptoui";
     322        1196 :   case FPToSI:        return "fptosi";
     323         867 :   case UIToFP:        return "uitofp";
     324        2123 :   case SIToFP:        return "sitofp";
     325        3374 :   case IntToPtr:      return "inttoptr";
     326        7960 :   case PtrToInt:      return "ptrtoint";
     327      143992 :   case BitCast:       return "bitcast";
     328         824 :   case AddrSpaceCast: return "addrspacecast";
     329             : 
     330             :   // Other instructions...
     331       35208 :   case ICmp:           return "icmp";
     332        2768 :   case FCmp:           return "fcmp";
     333       16508 :   case PHI:            return "phi";
     334        5731 :   case Select:         return "select";
     335      116030 :   case Call:           return "call";
     336        2935 :   case Shl:            return "shl";
     337        2597 :   case LShr:           return "lshr";
     338        1652 :   case AShr:           return "ashr";
     339          36 :   case VAArg:          return "va_arg";
     340        6102 :   case ExtractElement: return "extractelement";
     341       10884 :   case InsertElement:  return "insertelement";
     342        9478 :   case ShuffleVector:  return "shufflevector";
     343        7023 :   case ExtractValue:   return "extractvalue";
     344        2359 :   case InsertValue:    return "insertvalue";
     345        1330 :   case LandingPad:     return "landingpad";
     346         337 :   case CleanupPad:     return "cleanuppad";
     347             : 
     348           0 :   default: return "<Invalid operator> ";
     349             :   }
     350             : }
     351             : 
     352             : /// Return true if both instructions have the same special state. This must be
     353             : /// kept in sync with FunctionComparator::cmpOperations in
     354             : /// lib/Transforms/IPO/MergeFunctions.cpp.
     355     2065138 : static bool haveSameSpecialState(const Instruction *I1, const Instruction *I2,
     356             :                                  bool IgnoreAlignment = false) {
     357             :   assert(I1->getOpcode() == I2->getOpcode() &&
     358             :          "Can not compare special state of different instructions");
     359             : 
     360           2 :   if (const AllocaInst *AI = dyn_cast<AllocaInst>(I1))
     361           6 :     return AI->getAllocatedType() == cast<AllocaInst>(I2)->getAllocatedType() &&
     362           6 :            (AI->getAlignment() == cast<AllocaInst>(I2)->getAlignment() ||
     363             :             IgnoreAlignment);
     364        6703 :   if (const LoadInst *LI = dyn_cast<LoadInst>(I1))
     365       26812 :     return LI->isVolatile() == cast<LoadInst>(I2)->isVolatile() &&
     366       20113 :            (LI->getAlignment() == cast<LoadInst>(I2)->getAlignment() ||
     367        6699 :             IgnoreAlignment) &&
     368       33469 :            LI->getOrdering() == cast<LoadInst>(I2)->getOrdering() &&
     369       13338 :            LI->getSyncScopeID() == cast<LoadInst>(I2)->getSyncScopeID();
     370       84522 :   if (const StoreInst *SI = dyn_cast<StoreInst>(I1))
     371      338086 :     return SI->isVolatile() == cast<StoreInst>(I2)->isVolatile() &&
     372      267731 :            (SI->getAlignment() == cast<StoreInst>(I2)->getAlignment() ||
     373       70349 :             IgnoreAlignment) &&
     374      365874 :            SI->getOrdering() == cast<StoreInst>(I2)->getOrdering() &&
     375      140610 :            SI->getSyncScopeID() == cast<StoreInst>(I2)->getSyncScopeID();
     376      183086 :   if (const CmpInst *CI = dyn_cast<CmpInst>(I1))
     377      549258 :     return CI->getPredicate() == cast<CmpInst>(I2)->getPredicate();
     378       46194 :   if (const CallInst *CI = dyn_cast<CallInst>(I1))
     379      184776 :     return CI->isTailCall() == cast<CallInst>(I2)->isTailCall() &&
     380      184776 :            CI->getCallingConv() == cast<CallInst>(I2)->getCallingConv() &&
     381      138513 :            CI->getAttributes() == cast<CallInst>(I2)->getAttributes() &&
     382       46125 :            CI->hasIdenticalOperandBundleSchema(*cast<CallInst>(I2));
     383           0 :   if (const InvokeInst *CI = dyn_cast<InvokeInst>(I1))
     384           0 :     return CI->getCallingConv() == cast<InvokeInst>(I2)->getCallingConv() &&
     385           0 :            CI->getAttributes() == cast<InvokeInst>(I2)->getAttributes() &&
     386           0 :            CI->hasIdenticalOperandBundleSchema(*cast<InvokeInst>(I2));
     387        7623 :   if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(I1))
     388       30492 :     return IVI->getIndices() == cast<InsertValueInst>(I2)->getIndices();
     389       78287 :   if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(I1))
     390      313148 :     return EVI->getIndices() == cast<ExtractValueInst>(I2)->getIndices();
     391          10 :   if (const FenceInst *FI = dyn_cast<FenceInst>(I1))
     392          35 :     return FI->getOrdering() == cast<FenceInst>(I2)->getOrdering() &&
     393          10 :            FI->getSyncScopeID() == cast<FenceInst>(I2)->getSyncScopeID();
     394           0 :   if (const AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(I1))
     395           0 :     return CXI->isVolatile() == cast<AtomicCmpXchgInst>(I2)->isVolatile() &&
     396           0 :            CXI->isWeak() == cast<AtomicCmpXchgInst>(I2)->isWeak() &&
     397           0 :            CXI->getSuccessOrdering() ==
     398           0 :                cast<AtomicCmpXchgInst>(I2)->getSuccessOrdering() &&
     399           0 :            CXI->getFailureOrdering() ==
     400           0 :                cast<AtomicCmpXchgInst>(I2)->getFailureOrdering() &&
     401           0 :            CXI->getSyncScopeID() ==
     402           0 :                cast<AtomicCmpXchgInst>(I2)->getSyncScopeID();
     403         103 :   if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(I1))
     404         412 :     return RMWI->getOperation() == cast<AtomicRMWInst>(I2)->getOperation() &&
     405         412 :            RMWI->isVolatile() == cast<AtomicRMWInst>(I2)->isVolatile() &&
     406         465 :            RMWI->getOrdering() == cast<AtomicRMWInst>(I2)->getOrdering() &&
     407         106 :            RMWI->getSyncScopeID() == cast<AtomicRMWInst>(I2)->getSyncScopeID();
     408             : 
     409             :   return true;
     410             : }
     411             : 
     412      104169 : bool Instruction::isIdenticalTo(const Instruction *I) const {
     413      111368 :   return isIdenticalToWhenDefined(I) &&
     414      111368 :          SubclassOptionalData == I->SubclassOptionalData;
     415             : }
     416             : 
     417     2350807 : bool Instruction::isIdenticalToWhenDefined(const Instruction *I) const {
     418     6886600 :   if (getOpcode() != I->getOpcode() ||
     419     8784379 :       getNumOperands() != I->getNumOperands() ||
     420     2063600 :       getType() != I->getType())
     421             :     return false;
     422             : 
     423             :   // If both instructions have no operands, they are identical.
     424     1971515 :   if (getNumOperands() == 0 && I->getNumOperands() == 0)
     425          16 :     return haveSameSpecialState(this, I);
     426             : 
     427             :   // We have two instructions of identical opcode and #operands.  Check to see
     428             :   // if all operands are the same.
     429     7885996 :   if (!std::equal(op_begin(), op_end(), I->op_begin()))
     430             :     return false;
     431             : 
     432     1178274 :   if (const PHINode *thisPHI = dyn_cast<PHINode>(this)) {
     433         868 :     const PHINode *otherPHI = cast<PHINode>(I);
     434        2604 :     return std::equal(thisPHI->block_begin(), thisPHI->block_end(),
     435             :                       otherPHI->block_begin());
     436             :   }
     437             : 
     438     1177406 :   return haveSameSpecialState(this, I);
     439             : }
     440             : 
     441             : // Keep this in sync with FunctionComparator::cmpOperations in
     442             : // lib/Transforms/IPO/MergeFunctions.cpp.
     443     1429941 : bool Instruction::isSameOperationAs(const Instruction *I,
     444             :                                     unsigned flags) const {
     445     1429941 :   bool IgnoreAlignment = flags & CompareIgnoringAlignment;
     446     1429941 :   bool UseScalarTypes  = flags & CompareUsingScalarTypes;
     447             : 
     448     3754834 :   if (getOpcode() != I->getOpcode() ||
     449     4114355 :       getNumOperands() != I->getNumOperands() ||
     450      894510 :       (UseScalarTypes ?
     451           0 :        getType()->getScalarType() != I->getType()->getScalarType() :
     452      894510 :        getType() != I->getType()))
     453             :     return false;
     454             : 
     455             :   // We have two instructions of identical opcode and #operands.  Check to see
     456             :   // if all operands are the same type
     457     4553707 :   for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
     458     3672482 :     if (UseScalarTypes ?
     459           0 :         getOperand(i)->getType()->getScalarType() !=
     460           0 :           I->getOperand(i)->getType()->getScalarType() :
     461     5508723 :         getOperand(i)->getType() != I->getOperand(i)->getType())
     462             :       return false;
     463             : 
     464      887716 :   return haveSameSpecialState(this, I, IgnoreAlignment);
     465             : }
     466             : 
     467          37 : bool Instruction::isUsedOutsideOfBlock(const BasicBlock *BB) const {
     468         148 :   for (const Use &U : uses()) {
     469             :     // PHI nodes uses values in the corresponding predecessor block.  For other
     470             :     // instructions, just check to see whether the parent of the use matches up.
     471          74 :     const Instruction *I = cast<Instruction>(U.getUser());
     472           0 :     const PHINode *PN = dyn_cast<PHINode>(I);
     473          37 :     if (!PN) {
     474          37 :       if (I->getParent() != BB)
     475             :         return true;
     476          37 :       continue;
     477             :     }
     478             : 
     479           0 :     if (PN->getIncomingBlock(U) != BB)
     480             :       return true;
     481             :   }
     482             :   return false;
     483             : }
     484             : 
     485     9970949 : bool Instruction::mayReadFromMemory() const {
     486     9970949 :   switch (getOpcode()) {
     487             :   default: return false;
     488     1118322 :   case Instruction::VAArg:
     489             :   case Instruction::Load:
     490             :   case Instruction::Fence: // FIXME: refine definition of mayReadFromMemory
     491             :   case Instruction::AtomicCmpXchg:
     492             :   case Instruction::AtomicRMW:
     493             :   case Instruction::CatchPad:
     494             :   case Instruction::CatchRet:
     495     1118322 :     return true;
     496      910171 :   case Instruction::Call:
     497     1820342 :     return !cast<CallInst>(this)->doesNotAccessMemory();
     498      123954 :   case Instruction::Invoke:
     499      247908 :     return !cast<InvokeInst>(this)->doesNotAccessMemory();
     500     1507547 :   case Instruction::Store:
     501     3015094 :     return !cast<StoreInst>(this)->isUnordered();
     502             :   }
     503             : }
     504             : 
     505    43508005 : bool Instruction::mayWriteToMemory() const {
     506    43508005 :   switch (getOpcode()) {
     507             :   default: return false;
     508    13747970 :   case Instruction::Fence: // FIXME: refine definition of mayWriteToMemory
     509             :   case Instruction::Store:
     510             :   case Instruction::VAArg:
     511             :   case Instruction::AtomicCmpXchg:
     512             :   case Instruction::AtomicRMW:
     513             :   case Instruction::CatchPad:
     514             :   case Instruction::CatchRet:
     515    13747970 :     return true;
     516    10318773 :   case Instruction::Call:
     517    10318773 :     return !cast<CallInst>(this)->onlyReadsMemory();
     518      169276 :   case Instruction::Invoke:
     519      169276 :     return !cast<InvokeInst>(this)->onlyReadsMemory();
     520     5156574 :   case Instruction::Load:
     521    10313148 :     return !cast<LoadInst>(this)->isUnordered();
     522             :   }
     523             : }
     524             : 
     525    24494459 : bool Instruction::isAtomic() const {
     526    24494459 :   switch (getOpcode()) {
     527             :   default:
     528             :     return false;
     529        3117 :   case Instruction::AtomicCmpXchg:
     530             :   case Instruction::AtomicRMW:
     531             :   case Instruction::Fence:
     532        3117 :     return true;
     533    14219691 :   case Instruction::Load:
     534    28439382 :     return cast<LoadInst>(this)->getOrdering() != AtomicOrdering::NotAtomic;
     535     9042262 :   case Instruction::Store:
     536    18084524 :     return cast<StoreInst>(this)->getOrdering() != AtomicOrdering::NotAtomic;
     537             :   }
     538             : }
     539             : 
     540         492 : bool Instruction::hasAtomicLoad() const {
     541             :   assert(isAtomic());
     542         492 :   switch (getOpcode()) {
     543             :   default:
     544             :     return false;
     545         471 :   case Instruction::AtomicCmpXchg:
     546             :   case Instruction::AtomicRMW:
     547             :   case Instruction::Load:
     548         471 :     return true;
     549             :   }
     550             : }
     551             : 
     552         236 : bool Instruction::hasAtomicStore() const {
     553             :   assert(isAtomic());
     554         236 :   switch (getOpcode()) {
     555             :   default:
     556             :     return false;
     557         201 :   case Instruction::AtomicCmpXchg:
     558             :   case Instruction::AtomicRMW:
     559             :   case Instruction::Store:
     560         201 :     return true;
     561             :   }
     562             : }
     563             : 
     564    12679085 : bool Instruction::mayThrow() const {
     565    12679085 :   if (const CallInst *CI = dyn_cast<CallInst>(this))
     566      781948 :     return !CI->doesNotThrow();
     567    11897137 :   if (const auto *CRI = dyn_cast<CleanupReturnInst>(this))
     568          37 :     return CRI->unwindsToCaller();
     569    11897100 :   if (const auto *CatchSwitch = dyn_cast<CatchSwitchInst>(this))
     570          59 :     return CatchSwitch->unwindsToCaller();
     571    11897041 :   return isa<ResumeInst>(this);
     572             : }
     573             : 
     574     6880816 : bool Instruction::isAssociative() const {
     575     6880816 :   unsigned Opcode = getOpcode();
     576       19453 :   if (isAssociative(Opcode))
     577             :     return true;
     578             : 
     579       19453 :   switch (Opcode) {
     580       10983 :   case FMul:
     581             :   case FAdd:
     582       21966 :     return cast<FPMathOperator>(this)->hasUnsafeAlgebra();
     583             :   default:
     584             :     return false;
     585             :   }
     586             : }
     587             : 
     588           0 : Instruction *Instruction::cloneImpl() const {
     589           0 :   llvm_unreachable("Subclass of Instruction failed to implement cloneImpl");
     590             : }
     591             : 
     592        3502 : void Instruction::swapProfMetadata() {
     593        2800 :   MDNode *ProfileData = getMetadata(LLVMContext::MD_prof);
     594        3837 :   if (!ProfileData || ProfileData->getNumOperands() != 3 ||
     595        2074 :       !isa<MDString>(ProfileData->getOperand(0)))
     596        2466 :     return;
     597             : 
     598        2074 :   MDString *MDName = cast<MDString>(ProfileData->getOperand(0));
     599        2073 :   if (MDName->getString() != "branch_weights")
     600             :     return;
     601             : 
     602             :   // The first operand is the name. Fetch them backwards and build a new one.
     603        2072 :   Metadata *Ops[] = {ProfileData->getOperand(0), ProfileData->getOperand(2),
     604        4144 :                      ProfileData->getOperand(1)};
     605        1036 :   setMetadata(LLVMContext::MD_prof,
     606        3108 :               MDNode::get(ProfileData->getContext(), Ops));
     607             : }
     608             : 
     609     1909017 : void Instruction::copyMetadata(const Instruction &SrcInst,
     610             :                                ArrayRef<unsigned> WL) {
     611             :   if (!SrcInst.hasMetadata())
     612             :     return;
     613             : 
     614     1405044 :   DenseSet<unsigned> WLS;
     615     2913506 :   for (unsigned M : WL)
     616      103418 :     WLS.insert(M);
     617             : 
     618             :   // Otherwise, enumerate and copy over metadata from the old instruction to the
     619             :   // new one.
     620     2810088 :   SmallVector<std::pair<unsigned, MDNode *>, 4> TheMDs;
     621     1405044 :   SrcInst.getAllMetadataOtherThanDebugLoc(TheMDs);
     622     4373668 :   for (const auto &MD : TheMDs) {
     623      223720 :     if (WL.empty() || WLS.count(MD.first))
     624       93382 :       setMetadata(MD.first, MD.second);
     625             :   }
     626     1508426 :   if (WL.empty() || WLS.count(LLVMContext::MD_dbg))
     627     5206696 :     setDebugLoc(SrcInst.getDebugLoc());
     628             :   return;
     629             : }
     630             : 
     631     1791978 : Instruction *Instruction::clone() const {
     632     1791978 :   Instruction *New = nullptr;
     633     1791978 :   switch (getOpcode()) {
     634           0 :   default:
     635           0 :     llvm_unreachable("Unhandled Opcode.");
     636             : #define HANDLE_INST(num, opc, clas)                                            \
     637             :   case Instruction::opc:                                                       \
     638             :     New = cast<clas>(this)->cloneImpl();                                       \
     639             :     break;
     640             : #include "llvm/IR/Instruction.def"
     641             : #undef HANDLE_INST
     642             :   }
     643             : 
     644     1791978 :   New->SubclassOptionalData = SubclassOptionalData;
     645     1791978 :   New->copyMetadata(*this);
     646     1791978 :   return New;
     647             : }
     648             : 
     649          46 : void Instruction::updateProfWeight(uint64_t S, uint64_t T) {
     650           6 :   auto *ProfileData = getMetadata(LLVMContext::MD_prof);
     651           6 :   if (ProfileData == nullptr)
     652          40 :     return;
     653             : 
     654          12 :   auto *ProfDataName = dyn_cast<MDString>(ProfileData->getOperand(0));
     655          12 :   if (!ProfDataName || (!ProfDataName->getString().equals("branch_weights") &&
     656          10 :                         !ProfDataName->getString().equals("VP")))
     657             :     return;
     658             : 
     659          12 :   MDBuilder MDB(getContext());
     660          12 :   SmallVector<Metadata *, 3> Vals;
     661          12 :   Vals.push_back(ProfileData->getOperand(0));
     662          24 :   APInt APS(128, S), APT(128, T);
     663           6 :   if (ProfDataName->getString().equals("branch_weights"))
     664           6 :     for (unsigned i = 1; i < ProfileData->getNumOperands(); i++) {
     665             :       // Using APInt::div may be expensive, but most cases should fit 64 bits.
     666             :       APInt Val(128,
     667             :                 mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(i))
     668           4 :                     ->getValue()
     669           6 :                     .getZExtValue());
     670           2 :       Val *= APS;
     671           2 :       Vals.push_back(MDB.createConstant(
     672           2 :           ConstantInt::get(Type::getInt64Ty(getContext()),
     673           4 :                            Val.udiv(APT).getLimitedValue())));
     674             :     }
     675           4 :   else if (ProfDataName->getString().equals("VP"))
     676          36 :     for (unsigned i = 1; i < ProfileData->getNumOperands(); i += 2) {
     677             :       // The first value is the key of the value profile, which will not change.
     678          32 :       Vals.push_back(ProfileData->getOperand(i));
     679             :       // Using APInt::div may be expensive, but most cases should fit 64 bits.
     680             :       APInt Val(128,
     681             :                 mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(i + 1))
     682          48 :                     ->getValue()
     683          48 :                     .getZExtValue());
     684          16 :       Val *= APS;
     685          16 :       Vals.push_back(MDB.createConstant(
     686          16 :           ConstantInt::get(Type::getInt64Ty(getContext()),
     687          32 :                            Val.udiv(APT).getLimitedValue())));
     688             :     }
     689          12 :   setMetadata(LLVMContext::MD_prof, MDNode::get(getContext(), Vals));
     690             : }
     691             : 
     692           6 : void Instruction::setProfWeight(uint64_t W) {
     693             :   assert((isa<CallInst>(this) || isa<InvokeInst>(this)) &&
     694             :          "Can only set weights for call and invoke instrucitons");
     695          12 :   SmallVector<uint32_t, 1> Weights;
     696           6 :   Weights.push_back(W);
     697          12 :   MDBuilder MDB(getContext());
     698           6 :   setMetadata(LLVMContext::MD_prof, MDB.createBranchWeights(Weights));
     699           6 : }

Generated by: LCOV version 1.13