LCOV - code coverage report
Current view: top level - lib/IR - Instruction.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 319 347 91.9 %
Date: 2018-09-23 13:06:45 Functions: 60 65 92.3 %
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/IR/IntrinsicInst.h"
      16             : #include "llvm/ADT/DenseSet.h"
      17             : #include "llvm/IR/Constants.h"
      18             : #include "llvm/IR/Instructions.h"
      19             : #include "llvm/IR/MDBuilder.h"
      20             : #include "llvm/IR/Operator.h"
      21             : #include "llvm/IR/Type.h"
      22             : using namespace llvm;
      23             : 
      24    65788198 : Instruction::Instruction(Type *ty, unsigned it, Use *Ops, unsigned NumOps,
      25    65788198 :                          Instruction *InsertBefore)
      26    65788198 :   : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(nullptr) {
      27             : 
      28             :   // If requested, insert this instruction into a basic block...
      29    65788198 :   if (InsertBefore) {
      30     3763525 :     BasicBlock *BB = InsertBefore->getParent();
      31             :     assert(BB && "Instruction to insert before is not in a basic block!");
      32     3763525 :     BB->getInstList().insert(InsertBefore->getIterator(), this);
      33             :   }
      34    65788198 : }
      35             : 
      36     1825387 : Instruction::Instruction(Type *ty, unsigned it, Use *Ops, unsigned NumOps,
      37     1825387 :                          BasicBlock *InsertAtEnd)
      38     1825387 :   : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(nullptr) {
      39             : 
      40             :   // append this instruction into the basic block
      41             :   assert(InsertAtEnd && "Basic block to append to may not be NULL!");
      42     1825387 :   InsertAtEnd->getInstList().push_back(this);
      43     1825387 : }
      44             : 
      45    31240783 : Instruction::~Instruction() {
      46             :   assert(!Parent && "Instruction still linked in the program!");
      47    31240783 :   if (hasMetadataHashEntry())
      48      743706 :     clearMetadataHashEntries();
      49    31240783 : }
      50             : 
      51             : 
      52   133739550 : void Instruction::setParent(BasicBlock *P) {
      53   133739550 :   Parent = P;
      54   133739550 : }
      55             : 
      56    55702561 : const Module *Instruction::getModule() const {
      57    55702561 :   return getParent()->getModule();
      58             : }
      59             : 
      60    13888594 : const Function *Instruction::getFunction() const {
      61    13888594 :   return getParent()->getParent();
      62             : }
      63             : 
      64      644636 : void Instruction::removeFromParent() {
      65      644636 :   getParent()->getInstList().remove(getIterator());
      66      644636 : }
      67             : 
      68     7644653 : iplist<Instruction>::iterator Instruction::eraseFromParent() {
      69     7644653 :   return getParent()->getInstList().erase(getIterator());
      70             : }
      71             : 
      72             : /// Insert an unlinked instruction into a basic block immediately before the
      73             : /// specified instruction.
      74       30567 : void Instruction::insertBefore(Instruction *InsertPos) {
      75       30567 :   InsertPos->getParent()->getInstList().insert(InsertPos->getIterator(), this);
      76       30567 : }
      77             : 
      78             : /// Insert an unlinked instruction into a basic block immediately after the
      79             : /// specified instruction.
      80       28039 : void Instruction::insertAfter(Instruction *InsertPos) {
      81       28039 :   InsertPos->getParent()->getInstList().insertAfter(InsertPos->getIterator(),
      82       28039 :                                                     this);
      83       28039 : }
      84             : 
      85             : /// Unlink this instruction from its current basic block and insert it into the
      86             : /// basic block that MovePos lives in, right before MovePos.
      87       38117 : void Instruction::moveBefore(Instruction *MovePos) {
      88       38117 :   moveBefore(*MovePos->getParent(), MovePos->getIterator());
      89       38117 : }
      90             : 
      91        2687 : void Instruction::moveAfter(Instruction *MovePos) {
      92        5374 :   moveBefore(*MovePos->getParent(), ++MovePos->getIterator());
      93        2687 : }
      94             : 
      95       41225 : void Instruction::moveBefore(BasicBlock &BB,
      96             :                              SymbolTableList<Instruction>::iterator I) {
      97             :   assert(I == BB.end() || I->getParent() == &BB);
      98       41225 :   BB.getInstList().splice(I, getParent()->getInstList(), getIterator());
      99       41225 : }
     100             : 
     101      309338 : void Instruction::setHasNoUnsignedWrap(bool b) {
     102             :   cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b);
     103      309338 : }
     104             : 
     105      419530 : void Instruction::setHasNoSignedWrap(bool b) {
     106             :   cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b);
     107      419530 : }
     108             : 
     109       20005 : void Instruction::setIsExact(bool b) {
     110             :   cast<PossiblyExactOperator>(this)->setIsExact(b);
     111       20005 : }
     112             : 
     113    13582640 : bool Instruction::hasNoUnsignedWrap() const {
     114    13582640 :   return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap();
     115             : }
     116             : 
     117    13570272 : bool Instruction::hasNoSignedWrap() const {
     118    13570272 :   return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap();
     119             : }
     120             : 
     121         341 : void Instruction::dropPoisonGeneratingFlags() {
     122             :   switch (getOpcode()) {
     123             :   case Instruction::Add:
     124             :   case Instruction::Sub:
     125             :   case Instruction::Mul:
     126             :   case Instruction::Shl:
     127             :     cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(false);
     128             :     cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(false);
     129             :     break;
     130             : 
     131             :   case Instruction::UDiv:
     132             :   case Instruction::SDiv:
     133             :   case Instruction::AShr:
     134             :   case Instruction::LShr:
     135             :     cast<PossiblyExactOperator>(this)->setIsExact(false);
     136             :     break;
     137             : 
     138             :   case Instruction::GetElementPtr:
     139           1 :     cast<GetElementPtrInst>(this)->setIsInBounds(false);
     140           1 :     break;
     141             :   }
     142         341 : }
     143             : 
     144      166282 : bool Instruction::isExact() const {
     145      166282 :   return cast<PossiblyExactOperator>(this)->isExact();
     146             : }
     147             : 
     148           1 : void Instruction::setFast(bool B) {
     149             :   assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
     150           1 :   cast<FPMathOperator>(this)->setFast(B);
     151           1 : }
     152             : 
     153           0 : void Instruction::setHasAllowReassoc(bool B) {
     154             :   assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
     155             :   cast<FPMathOperator>(this)->setHasAllowReassoc(B);
     156           0 : }
     157             : 
     158           1 : void Instruction::setHasNoNaNs(bool B) {
     159             :   assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
     160             :   cast<FPMathOperator>(this)->setHasNoNaNs(B);
     161           1 : }
     162             : 
     163           0 : void Instruction::setHasNoInfs(bool B) {
     164             :   assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
     165             :   cast<FPMathOperator>(this)->setHasNoInfs(B);
     166           0 : }
     167             : 
     168           0 : void Instruction::setHasNoSignedZeros(bool B) {
     169             :   assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
     170             :   cast<FPMathOperator>(this)->setHasNoSignedZeros(B);
     171           0 : }
     172             : 
     173           2 : void Instruction::setHasAllowReciprocal(bool B) {
     174             :   assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
     175             :   cast<FPMathOperator>(this)->setHasAllowReciprocal(B);
     176           2 : }
     177             : 
     178           0 : void Instruction::setHasApproxFunc(bool B) {
     179             :   assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
     180             :   cast<FPMathOperator>(this)->setHasApproxFunc(B);
     181           0 : }
     182             : 
     183       80692 : void Instruction::setFastMathFlags(FastMathFlags FMF) {
     184             :   assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
     185             :   cast<FPMathOperator>(this)->setFastMathFlags(FMF);
     186       80692 : }
     187             : 
     188       10070 : void Instruction::copyFastMathFlags(FastMathFlags FMF) {
     189             :   assert(isa<FPMathOperator>(this) && "copying fast-math flag on invalid op");
     190             :   cast<FPMathOperator>(this)->copyFastMathFlags(FMF);
     191       10070 : }
     192             : 
     193       33611 : bool Instruction::isFast() const {
     194             :   assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
     195       33611 :   return cast<FPMathOperator>(this)->isFast();
     196             : }
     197             : 
     198       15120 : bool Instruction::hasAllowReassoc() const {
     199             :   assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
     200       15120 :   return cast<FPMathOperator>(this)->hasAllowReassoc();
     201             : }
     202             : 
     203       78675 : bool Instruction::hasNoNaNs() const {
     204             :   assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
     205       78675 :   return cast<FPMathOperator>(this)->hasNoNaNs();
     206             : }
     207             : 
     208          56 : bool Instruction::hasNoInfs() const {
     209             :   assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
     210          56 :   return cast<FPMathOperator>(this)->hasNoInfs();
     211             : }
     212             : 
     213        7243 : bool Instruction::hasNoSignedZeros() const {
     214             :   assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
     215        7243 :   return cast<FPMathOperator>(this)->hasNoSignedZeros();
     216             : }
     217             : 
     218         576 : bool Instruction::hasAllowReciprocal() const {
     219             :   assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
     220         576 :   return cast<FPMathOperator>(this)->hasAllowReciprocal();
     221             : }
     222             : 
     223           6 : bool Instruction::hasAllowContract() const {
     224             :   assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
     225           6 :   return cast<FPMathOperator>(this)->hasAllowContract();
     226             : }
     227             : 
     228           4 : bool Instruction::hasApproxFunc() const {
     229             :   assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
     230           4 :   return cast<FPMathOperator>(this)->hasApproxFunc();
     231             : }
     232             : 
     233      102252 : FastMathFlags Instruction::getFastMathFlags() const {
     234             :   assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
     235      102252 :   return cast<FPMathOperator>(this)->getFastMathFlags();
     236             : }
     237             : 
     238         779 : void Instruction::copyFastMathFlags(const Instruction *I) {
     239         779 :   copyFastMathFlags(I->getFastMathFlags());
     240         779 : }
     241             : 
     242       98195 : void Instruction::copyIRFlags(const Value *V, bool IncludeWrapFlags) {
     243             :   // Copy the wrapping flags.
     244       98195 :   if (IncludeWrapFlags && isa<OverflowingBinaryOperator>(this)) {
     245       95538 :     if (auto *OB = dyn_cast<OverflowingBinaryOperator>(V)) {
     246       95538 :       setHasNoSignedWrap(OB->hasNoSignedWrap());
     247       95538 :       setHasNoUnsignedWrap(OB->hasNoUnsignedWrap());
     248             :     }
     249             :   }
     250             : 
     251             :   // Copy the exact flag.
     252         266 :   if (auto *PE = dyn_cast<PossiblyExactOperator>(V))
     253             :     if (isa<PossiblyExactOperator>(this))
     254         266 :       setIsExact(PE->isExact());
     255             : 
     256             :   // Copy the fast-math flags.
     257        1766 :   if (auto *FP = dyn_cast<FPMathOperator>(V))
     258        1766 :     if (isa<FPMathOperator>(this))
     259        1766 :       copyFastMathFlags(FP->getFastMathFlags());
     260             : 
     261             :   if (auto *SrcGEP = dyn_cast<GetElementPtrInst>(V))
     262             :     if (auto *DestGEP = dyn_cast<GetElementPtrInst>(this))
     263           0 :       DestGEP->setIsInBounds(SrcGEP->isInBounds() | DestGEP->isInBounds());
     264       98195 : }
     265             : 
     266      320685 : void Instruction::andIRFlags(const Value *V) {
     267      195766 :   if (auto *OB = dyn_cast<OverflowingBinaryOperator>(V)) {
     268             :     if (isa<OverflowingBinaryOperator>(this)) {
     269      195742 :       setHasNoSignedWrap(hasNoSignedWrap() & OB->hasNoSignedWrap());
     270      195742 :       setHasNoUnsignedWrap(hasNoUnsignedWrap() & OB->hasNoUnsignedWrap());
     271             :     }
     272             :   }
     273             : 
     274        1964 :   if (auto *PE = dyn_cast<PossiblyExactOperator>(V))
     275             :     if (isa<PossiblyExactOperator>(this))
     276        1962 :       setIsExact(isExact() & PE->isExact());
     277             : 
     278        7415 :   if (auto *FP = dyn_cast<FPMathOperator>(V)) {
     279        7415 :     if (isa<FPMathOperator>(this)) {
     280        7415 :       FastMathFlags FM = getFastMathFlags();
     281             :       FM &= FP->getFastMathFlags();
     282        7415 :       copyFastMathFlags(FM);
     283             :     }
     284             :   }
     285             : 
     286             :   if (auto *SrcGEP = dyn_cast<GetElementPtrInst>(V))
     287             :     if (auto *DestGEP = dyn_cast<GetElementPtrInst>(this))
     288       35855 :       DestGEP->setIsInBounds(SrcGEP->isInBounds() & DestGEP->isInBounds());
     289      320685 : }
     290             : 
     291     2410628 : const char *Instruction::getOpcodeName(unsigned OpCode) {
     292     2410628 :   switch (OpCode) {
     293             :   // Terminators
     294             :   case Ret:    return "ret";
     295      162270 :   case Br:     return "br";
     296        1143 :   case Switch: return "switch";
     297         229 :   case IndirectBr: return "indirectbr";
     298        3266 :   case Invoke: return "invoke";
     299         570 :   case Resume: return "resume";
     300        4766 :   case Unreachable: return "unreachable";
     301         330 :   case CleanupRet: return "cleanupret";
     302         233 :   case CatchRet: return "catchret";
     303         314 :   case CatchPad: return "catchpad";
     304         306 :   case CatchSwitch: return "catchswitch";
     305             : 
     306             :   // Standard binary operators...
     307       64579 :   case Add: return "add";
     308        7046 :   case FAdd: return "fadd";
     309       15756 :   case Sub: return "sub";
     310        1794 :   case FSub: return "fsub";
     311       15891 :   case Mul: return "mul";
     312        3519 :   case FMul: return "fmul";
     313        1470 :   case UDiv: return "udiv";
     314        5353 :   case SDiv: return "sdiv";
     315        1135 :   case FDiv: return "fdiv";
     316        1330 :   case URem: return "urem";
     317        1562 :   case SRem: return "srem";
     318         172 :   case FRem: return "frem";
     319             : 
     320             :   // Logical operators...
     321       10616 :   case And: return "and";
     322        5351 :   case Or : return "or";
     323        4831 :   case Xor: return "xor";
     324             : 
     325             :   // Memory instructions...
     326      319258 :   case Alloca:        return "alloca";
     327      426291 :   case Load:          return "load";
     328      406396 :   case Store:         return "store";
     329         699 :   case AtomicCmpXchg: return "cmpxchg";
     330         981 :   case AtomicRMW:     return "atomicrmw";
     331         255 :   case Fence:         return "fence";
     332      194737 :   case GetElementPtr: return "getelementptr";
     333             : 
     334             :   // Convert instructions...
     335       12772 :   case Trunc:         return "trunc";
     336       11810 :   case ZExt:          return "zext";
     337       13145 :   case SExt:          return "sext";
     338        1260 :   case FPTrunc:       return "fptrunc";
     339        3413 :   case FPExt:         return "fpext";
     340         945 :   case FPToUI:        return "fptoui";
     341        1849 :   case FPToSI:        return "fptosi";
     342        1208 :   case UIToFP:        return "uitofp";
     343        3456 :   case SIToFP:        return "sitofp";
     344        5152 :   case IntToPtr:      return "inttoptr";
     345       12050 :   case PtrToInt:      return "ptrtoint";
     346      189502 :   case BitCast:       return "bitcast";
     347        1240 :   case AddrSpaceCast: return "addrspacecast";
     348             : 
     349             :   // Other instructions...
     350       65386 :   case ICmp:           return "icmp";
     351        4165 :   case FCmp:           return "fcmp";
     352       27900 :   case PHI:            return "phi";
     353        7860 :   case Select:         return "select";
     354      182468 :   case Call:           return "call";
     355        4033 :   case Shl:            return "shl";
     356        3610 :   case LShr:           return "lshr";
     357        2220 :   case AShr:           return "ashr";
     358          50 :   case VAArg:          return "va_arg";
     359        8777 :   case ExtractElement: return "extractelement";
     360       14516 :   case InsertElement:  return "insertelement";
     361       11714 :   case ShuffleVector:  return "shufflevector";
     362        9447 :   case ExtractValue:   return "extractvalue";
     363        2581 :   case InsertValue:    return "insertvalue";
     364        1857 :   case LandingPad:     return "landingpad";
     365         456 :   case CleanupPad:     return "cleanuppad";
     366             : 
     367           0 :   default: return "<Invalid operator> ";
     368             :   }
     369             : }
     370             : 
     371             : /// Return true if both instructions have the same special state. This must be
     372             : /// kept in sync with FunctionComparator::cmpOperations in
     373             : /// lib/Transforms/IPO/MergeFunctions.cpp.
     374     1808323 : static bool haveSameSpecialState(const Instruction *I1, const Instruction *I2,
     375             :                                  bool IgnoreAlignment = false) {
     376             :   assert(I1->getOpcode() == I2->getOpcode() &&
     377             :          "Can not compare special state of different instructions");
     378             : 
     379             :   if (const AllocaInst *AI = dyn_cast<AllocaInst>(I1))
     380           2 :     return AI->getAllocatedType() == cast<AllocaInst>(I2)->getAllocatedType() &&
     381           0 :            (AI->getAlignment() == cast<AllocaInst>(I2)->getAlignment() ||
     382             :             IgnoreAlignment);
     383             :   if (const LoadInst *LI = dyn_cast<LoadInst>(I1))
     384        4203 :     return LI->isVolatile() == cast<LoadInst>(I2)->isVolatile() &&
     385           3 :            (LI->getAlignment() == cast<LoadInst>(I2)->getAlignment() ||
     386        4200 :             IgnoreAlignment) &&
     387        4203 :            LI->getOrdering() == cast<LoadInst>(I2)->getOrdering() &&
     388        4200 :            LI->getSyncScopeID() == cast<LoadInst>(I2)->getSyncScopeID();
     389             :   if (const StoreInst *SI = dyn_cast<StoreInst>(I1))
     390       14633 :     return SI->isVolatile() == cast<StoreInst>(I2)->isVolatile() &&
     391        1204 :            (SI->getAlignment() == cast<StoreInst>(I2)->getAlignment() ||
     392       13429 :             IgnoreAlignment) &&
     393       14634 :            SI->getOrdering() == cast<StoreInst>(I2)->getOrdering() &&
     394       13427 :            SI->getSyncScopeID() == cast<StoreInst>(I2)->getSyncScopeID();
     395             :   if (const CmpInst *CI = dyn_cast<CmpInst>(I1))
     396      302025 :     return CI->getPredicate() == cast<CmpInst>(I2)->getPredicate();
     397             :   if (const CallInst *CI = dyn_cast<CallInst>(I1))
     398       51189 :     return CI->isTailCall() == cast<CallInst>(I2)->isTailCall() &&
     399       51189 :            CI->getCallingConv() == cast<CallInst>(I2)->getCallingConv() &&
     400      102280 :            CI->getAttributes() == cast<CallInst>(I2)->getAttributes() &&
     401       51089 :            CI->hasIdenticalOperandBundleSchema(*cast<CallInst>(I2));
     402             :   if (const InvokeInst *CI = dyn_cast<InvokeInst>(I1))
     403           0 :     return CI->getCallingConv() == cast<InvokeInst>(I2)->getCallingConv() &&
     404           0 :            CI->getAttributes() == cast<InvokeInst>(I2)->getAttributes() &&
     405           0 :            CI->hasIdenticalOperandBundleSchema(*cast<InvokeInst>(I2));
     406             :   if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(I1))
     407       10252 :     return IVI->getIndices() == cast<InsertValueInst>(I2)->getIndices();
     408             :   if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(I1))
     409      105854 :     return EVI->getIndices() == cast<ExtractValueInst>(I2)->getIndices();
     410             :   if (const FenceInst *FI = dyn_cast<FenceInst>(I1))
     411          11 :     return FI->getOrdering() == cast<FenceInst>(I2)->getOrdering() &&
     412           6 :            FI->getSyncScopeID() == cast<FenceInst>(I2)->getSyncScopeID();
     413             :   if (const AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(I1))
     414           6 :     return CXI->isVolatile() == cast<AtomicCmpXchgInst>(I2)->isVolatile() &&
     415           6 :            CXI->isWeak() == cast<AtomicCmpXchgInst>(I2)->isWeak() &&
     416             :            CXI->getSuccessOrdering() ==
     417           6 :                cast<AtomicCmpXchgInst>(I2)->getSuccessOrdering() &&
     418             :            CXI->getFailureOrdering() ==
     419           6 :                cast<AtomicCmpXchgInst>(I2)->getFailureOrdering() &&
     420           0 :            CXI->getSyncScopeID() ==
     421           0 :                cast<AtomicCmpXchgInst>(I2)->getSyncScopeID();
     422             :   if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(I1))
     423           2 :     return RMWI->getOperation() == cast<AtomicRMWInst>(I2)->getOperation() &&
     424           2 :            RMWI->isVolatile() == cast<AtomicRMWInst>(I2)->isVolatile() &&
     425           2 :            RMWI->getOrdering() == cast<AtomicRMWInst>(I2)->getOrdering() &&
     426           2 :            RMWI->getSyncScopeID() == cast<AtomicRMWInst>(I2)->getSyncScopeID();
     427             : 
     428             :   return true;
     429             : }
     430             : 
     431      325289 : bool Instruction::isIdenticalTo(const Instruction *I) const {
     432      325289 :   return isIdenticalToWhenDefined(I) &&
     433       14361 :          SubclassOptionalData == I->SubclassOptionalData;
     434             : }
     435             : 
     436     3957523 : bool Instruction::isIdenticalToWhenDefined(const Instruction *I) const {
     437     3557093 :   if (getOpcode() != I->getOpcode() ||
     438     3957523 :       getNumOperands() != I->getNumOperands() ||
     439     3469910 :       getType() != I->getType())
     440             :     return false;
     441             : 
     442             :   // If both instructions have no operands, they are identical.
     443     3328101 :   if (getNumOperands() == 0 && I->getNumOperands() == 0)
     444          18 :     return haveSameSpecialState(this, I);
     445             : 
     446             :   // We have two instructions of identical opcode and #operands.  Check to see
     447             :   // if all operands are the same.
     448     9984249 :   if (!std::equal(op_begin(), op_end(), I->op_begin()))
     449             :     return false;
     450             : 
     451             :   if (const PHINode *thisPHI = dyn_cast<PHINode>(this)) {
     452             :     const PHINode *otherPHI = cast<PHINode>(I);
     453             :     return std::equal(thisPHI->block_begin(), thisPHI->block_end(),
     454             :                       otherPHI->block_begin());
     455             :   }
     456             : 
     457     1755185 :   return haveSameSpecialState(this, I);
     458             : }
     459             : 
     460             : // Keep this in sync with FunctionComparator::cmpOperations in
     461             : // lib/Transforms/IPO/MergeFunctions.cpp.
     462      117034 : bool Instruction::isSameOperationAs(const Instruction *I,
     463             :                                     unsigned flags) const {
     464      117034 :   bool IgnoreAlignment = flags & CompareIgnoringAlignment;
     465      117034 :   bool UseScalarTypes  = flags & CompareUsingScalarTypes;
     466             : 
     467       53950 :   if (getOpcode() != I->getOpcode() ||
     468      170928 :       getNumOperands() != I->getNumOperands() ||
     469       53894 :       (UseScalarTypes ?
     470           0 :        getType()->getScalarType() != I->getType()->getScalarType() :
     471       53894 :        getType() != I->getType()))
     472             :     return false;
     473             : 
     474             :   // We have two instructions of identical opcode and #operands.  Check to see
     475             :   // if all operands are the same type
     476      151408 :   for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
     477      196576 :     if (UseScalarTypes ?
     478           0 :         getOperand(i)->getType()->getScalarType() !=
     479           0 :           I->getOperand(i)->getType()->getScalarType() :
     480      294864 :         getOperand(i)->getType() != I->getOperand(i)->getType())
     481             :       return false;
     482             : 
     483       53120 :   return haveSameSpecialState(this, I, IgnoreAlignment);
     484             : }
     485             : 
     486         140 : bool Instruction::isUsedOutsideOfBlock(const BasicBlock *BB) const {
     487         286 :   for (const Use &U : uses()) {
     488             :     // PHI nodes uses values in the corresponding predecessor block.  For other
     489             :     // instructions, just check to see whether the parent of the use matches up.
     490         148 :     const Instruction *I = cast<Instruction>(U.getUser());
     491             :     const PHINode *PN = dyn_cast<PHINode>(I);
     492             :     if (!PN) {
     493         148 :       if (I->getParent() != BB)
     494             :         return true;
     495             :       continue;
     496             :     }
     497             : 
     498           0 :     if (PN->getIncomingBlock(U) != BB)
     499             :       return true;
     500             :   }
     501             :   return false;
     502             : }
     503             : 
     504    17931065 : bool Instruction::mayReadFromMemory() const {
     505    17931065 :   switch (getOpcode()) {
     506             :   default: return false;
     507     2289036 :   case Instruction::VAArg:
     508             :   case Instruction::Load:
     509             :   case Instruction::Fence: // FIXME: refine definition of mayReadFromMemory
     510             :   case Instruction::AtomicCmpXchg:
     511             :   case Instruction::AtomicRMW:
     512             :   case Instruction::CatchPad:
     513             :   case Instruction::CatchRet:
     514     2289036 :     return true;
     515             :   case Instruction::Call:
     516     1236662 :     return !cast<CallInst>(this)->doesNotAccessMemory();
     517             :   case Instruction::Invoke:
     518      196345 :     return !cast<InvokeInst>(this)->doesNotAccessMemory();
     519             :   case Instruction::Store:
     520     2851225 :     return !cast<StoreInst>(this)->isUnordered();
     521             :   }
     522             : }
     523             : 
     524   112598992 : bool Instruction::mayWriteToMemory() const {
     525   112598992 :   switch (getOpcode()) {
     526             :   default: return false;
     527    30377289 :   case Instruction::Fence: // FIXME: refine definition of mayWriteToMemory
     528             :   case Instruction::Store:
     529             :   case Instruction::VAArg:
     530             :   case Instruction::AtomicCmpXchg:
     531             :   case Instruction::AtomicRMW:
     532             :   case Instruction::CatchPad:
     533             :   case Instruction::CatchRet:
     534    30377289 :     return true;
     535             :   case Instruction::Call:
     536    14306227 :     return !cast<CallInst>(this)->onlyReadsMemory();
     537             :   case Instruction::Invoke:
     538      772146 :     return !cast<InvokeInst>(this)->onlyReadsMemory();
     539             :   case Instruction::Load:
     540    18821985 :     return !cast<LoadInst>(this)->isUnordered();
     541             :   }
     542             : }
     543             : 
     544    91509993 : bool Instruction::isAtomic() const {
     545    91509993 :   switch (getOpcode()) {
     546             :   default:
     547             :     return false;
     548       13077 :   case Instruction::AtomicCmpXchg:
     549             :   case Instruction::AtomicRMW:
     550             :   case Instruction::Fence:
     551       13077 :     return true;
     552             :   case Instruction::Load:
     553    42135233 :     return cast<LoadInst>(this)->getOrdering() != AtomicOrdering::NotAtomic;
     554             :   case Instruction::Store:
     555    26797068 :     return cast<StoreInst>(this)->getOrdering() != AtomicOrdering::NotAtomic;
     556             :   }
     557             : }
     558             : 
     559         494 : bool Instruction::hasAtomicLoad() const {
     560             :   assert(isAtomic());
     561             :   switch (getOpcode()) {
     562             :   default:
     563             :     return false;
     564             :   case Instruction::AtomicCmpXchg:
     565             :   case Instruction::AtomicRMW:
     566             :   case Instruction::Load:
     567             :     return true;
     568             :   }
     569             : }
     570             : 
     571         253 : bool Instruction::hasAtomicStore() const {
     572             :   assert(isAtomic());
     573             :   switch (getOpcode()) {
     574             :   default:
     575             :     return false;
     576             :   case Instruction::AtomicCmpXchg:
     577             :   case Instruction::AtomicRMW:
     578             :   case Instruction::Store:
     579             :     return true;
     580             :   }
     581             : }
     582             : 
     583    26196266 : bool Instruction::mayThrow() const {
     584             :   if (const CallInst *CI = dyn_cast<CallInst>(this))
     585     2157059 :     return !CI->doesNotThrow();
     586             :   if (const auto *CRI = dyn_cast<CleanupReturnInst>(this))
     587          67 :     return CRI->unwindsToCaller();
     588             :   if (const auto *CatchSwitch = dyn_cast<CatchSwitchInst>(this))
     589         105 :     return CatchSwitch->unwindsToCaller();
     590             :   return isa<ResumeInst>(this);
     591             : }
     592             : 
     593       86570 : bool Instruction::isSafeToRemove() const {
     594       86570 :   return (!isa<CallInst>(this) || !this->mayHaveSideEffects()) &&
     595       86570 :          !this->isTerminator();
     596             : }
     597             : 
     598        1624 : const Instruction *Instruction::getNextNonDebugInstruction() const {
     599        4290 :   for (const Instruction *I = getNextNode(); I; I = I->getNextNode())
     600             :     if (!isa<DbgInfoIntrinsic>(I))
     601             :       return I;
     602             :   return nullptr;
     603             : }
     604             : 
     605    14673963 : bool Instruction::isAssociative() const {
     606             :   unsigned Opcode = getOpcode();
     607             :   if (isAssociative(Opcode))
     608             :     return true;
     609             : 
     610     2666517 :   switch (Opcode) {
     611             :   case FMul:
     612             :   case FAdd:
     613       15039 :     return cast<FPMathOperator>(this)->hasAllowReassoc() &&
     614             :            cast<FPMathOperator>(this)->hasNoSignedZeros();
     615             :   default:
     616             :     return false;
     617             :   }
     618             : }
     619             : 
     620   112988310 : unsigned Instruction::getNumSuccessors() const {
     621   112988310 :   switch (getOpcode()) {
     622             : #define HANDLE_TERM_INST(N, OPC, CLASS)                                        \
     623             :   case Instruction::OPC:                                                       \
     624             :     return static_cast<const CLASS *>(this)->getNumSuccessors();
     625             : #include "llvm/IR/Instruction.def"
     626             :   default:
     627             :     break;
     628             :   }
     629           0 :   llvm_unreachable("not a terminator");
     630             : }
     631             : 
     632    88953855 : BasicBlock *Instruction::getSuccessor(unsigned idx) const {
     633    88953855 :   switch (getOpcode()) {
     634             : #define HANDLE_TERM_INST(N, OPC, CLASS)                                        \
     635             :   case Instruction::OPC:                                                       \
     636             :     return static_cast<const CLASS *>(this)->getSuccessor(idx);
     637             : #include "llvm/IR/Instruction.def"
     638             :   default:
     639             :     break;
     640             :   }
     641           0 :   llvm_unreachable("not a terminator");
     642             : }
     643             : 
     644       11294 : void Instruction::setSuccessor(unsigned idx, BasicBlock *B) {
     645       11294 :   switch (getOpcode()) {
     646             : #define HANDLE_TERM_INST(N, OPC, CLASS)                                        \
     647             :   case Instruction::OPC:                                                       \
     648             :     return static_cast<CLASS *>(this)->setSuccessor(idx, B);
     649             : #include "llvm/IR/Instruction.def"
     650             :   default:
     651             :     break;
     652             :   }
     653           0 :   llvm_unreachable("not a terminator");
     654             : }
     655             : 
     656           0 : Instruction *Instruction::cloneImpl() const {
     657           0 :   llvm_unreachable("Subclass of Instruction failed to implement cloneImpl");
     658             : }
     659             : 
     660       12608 : void Instruction::swapProfMetadata() {
     661             :   MDNode *ProfileData = getMetadata(LLVMContext::MD_prof);
     662       11569 :   if (!ProfileData || ProfileData->getNumOperands() != 3 ||
     663             :       !isa<MDString>(ProfileData->getOperand(0)))
     664       10994 :     return;
     665             : 
     666             :   MDString *MDName = cast<MDString>(ProfileData->getOperand(0));
     667        1615 :   if (MDName->getString() != "branch_weights")
     668             :     return;
     669             : 
     670             :   // The first operand is the name. Fetch them backwards and build a new one.
     671             :   Metadata *Ops[] = {ProfileData->getOperand(0), ProfileData->getOperand(2),
     672        4842 :                      ProfileData->getOperand(1)};
     673        1614 :   setMetadata(LLVMContext::MD_prof,
     674             :               MDNode::get(ProfileData->getContext(), Ops));
     675             : }
     676             : 
     677    40488483 : void Instruction::copyMetadata(const Instruction &SrcInst,
     678             :                                ArrayRef<unsigned> WL) {
     679             :   if (!SrcInst.hasMetadata())
     680    37852773 :     return;
     681             : 
     682             :   DenseSet<unsigned> WLS;
     683     2779364 :   for (unsigned M : WL)
     684             :     WLS.insert(M);
     685             : 
     686             :   // Otherwise, enumerate and copy over metadata from the old instruction to the
     687             :   // new one.
     688             :   SmallVector<std::pair<unsigned, MDNode *>, 4> TheMDs;
     689             :   SrcInst.getAllMetadataOtherThanDebugLoc(TheMDs);
     690     3027276 :   for (const auto &MD : TheMDs) {
     691      391566 :     if (WL.empty() || WLS.count(MD.first))
     692      296347 :       setMetadata(MD.first, MD.second);
     693             :   }
     694     2779280 :   if (WL.empty() || WLS.count(LLVMContext::MD_dbg))
     695     2492165 :     setDebugLoc(SrcInst.getDebugLoc());
     696             : }
     697             : 
     698    40333311 : Instruction *Instruction::clone() const {
     699             :   Instruction *New = nullptr;
     700    40333311 :   switch (getOpcode()) {
     701           0 :   default:
     702           0 :     llvm_unreachable("Unhandled Opcode.");
     703             : #define HANDLE_INST(num, opc, clas)                                            \
     704             :   case Instruction::opc:                                                       \
     705             :     New = cast<clas>(this)->cloneImpl();                                       \
     706             :     break;
     707             : #include "llvm/IR/Instruction.def"
     708             : #undef HANDLE_INST
     709             :   }
     710             : 
     711    40333311 :   New->SubclassOptionalData = SubclassOptionalData;
     712    40333311 :   New->copyMetadata(*this);
     713    40333311 :   return New;
     714             : }
     715             : 
     716          60 : void Instruction::updateProfWeight(uint64_t S, uint64_t T) {
     717             :   auto *ProfileData = getMetadata(LLVMContext::MD_prof);
     718           6 :   if (ProfileData == nullptr)
     719          54 :     return;
     720             : 
     721             :   auto *ProfDataName = dyn_cast<MDString>(ProfileData->getOperand(0));
     722           6 :   if (!ProfDataName || (!ProfDataName->getString().equals("branch_weights") &&
     723           4 :                         !ProfDataName->getString().equals("VP")))
     724           0 :     return;
     725             : 
     726           6 :   MDBuilder MDB(getContext());
     727             :   SmallVector<Metadata *, 3> Vals;
     728           6 :   Vals.push_back(ProfileData->getOperand(0));
     729             :   APInt APS(128, S), APT(128, T);
     730          12 :   if (ProfDataName->getString().equals("branch_weights"))
     731           4 :     for (unsigned i = 1; i < ProfileData->getNumOperands(); i++) {
     732             :       // Using APInt::div may be expensive, but most cases should fit 64 bits.
     733             :       APInt Val(128,
     734             :                 mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(i))
     735             :                     ->getValue()
     736             :                     .getZExtValue());
     737           2 :       Val *= APS;
     738           2 :       Vals.push_back(MDB.createConstant(
     739           2 :           ConstantInt::get(Type::getInt64Ty(getContext()),
     740           4 :                            Val.udiv(APT).getLimitedValue())));
     741             :     }
     742           8 :   else if (ProfDataName->getString().equals("VP"))
     743          20 :     for (unsigned i = 1; i < ProfileData->getNumOperands(); i += 2) {
     744             :       // The first value is the key of the value profile, which will not change.
     745          16 :       Vals.push_back(ProfileData->getOperand(i));
     746             :       // Using APInt::div may be expensive, but most cases should fit 64 bits.
     747             :       APInt Val(128,
     748             :                 mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(i + 1))
     749          16 :                     ->getValue()
     750             :                     .getZExtValue());
     751          16 :       Val *= APS;
     752          16 :       Vals.push_back(MDB.createConstant(
     753          16 :           ConstantInt::get(Type::getInt64Ty(getContext()),
     754          32 :                            Val.udiv(APT).getLimitedValue())));
     755             :     }
     756          12 :   setMetadata(LLVMContext::MD_prof, MDNode::get(getContext(), Vals));
     757             : }
     758             : 
     759           6 : void Instruction::setProfWeight(uint64_t W) {
     760             :   assert((isa<CallInst>(this) || isa<InvokeInst>(this)) &&
     761             :          "Can only set weights for call and invoke instrucitons");
     762             :   SmallVector<uint32_t, 1> Weights;
     763           6 :   Weights.push_back(W);
     764           6 :   MDBuilder MDB(getContext());
     765           6 :   setMetadata(LLVMContext::MD_prof, MDB.createBranchWeights(Weights));
     766           6 : }

Generated by: LCOV version 1.13