LCOV - code coverage report
Current view: top level - lib/IR - Instruction.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 313 341 91.8 %
Date: 2018-06-17 00:07:59 Functions: 56 61 91.8 %
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/Constants.h"
      17             : #include "llvm/IR/Instructions.h"
      18             : #include "llvm/IR/MDBuilder.h"
      19             : #include "llvm/IR/Operator.h"
      20             : #include "llvm/IR/Type.h"
      21             : using namespace llvm;
      22             : 
      23     9275649 : Instruction::Instruction(Type *ty, unsigned it, Use *Ops, unsigned NumOps,
      24     9275649 :                          Instruction *InsertBefore)
      25    18551298 :   : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(nullptr) {
      26             : 
      27             :   // If requested, insert this instruction into a basic block...
      28     9275649 :   if (InsertBefore) {
      29      829582 :     BasicBlock *BB = InsertBefore->getParent();
      30             :     assert(BB && "Instruction to insert before is not in a basic block!");
      31      829582 :     BB->getInstList().insert(InsertBefore->getIterator(), this);
      32             :   }
      33     9275649 : }
      34             : 
      35      321565 : Instruction::Instruction(Type *ty, unsigned it, Use *Ops, unsigned NumOps,
      36      321565 :                          BasicBlock *InsertAtEnd)
      37      643130 :   : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(nullptr) {
      38             : 
      39             :   // append this instruction into the basic block
      40             :   assert(InsertAtEnd && "Basic block to append to may not be NULL!");
      41      321565 :   InsertAtEnd->getInstList().push_back(this);
      42      321565 : }
      43             : 
      44    15173944 : Instruction::~Instruction() {
      45             :   assert(!Parent && "Instruction still linked in the program!");
      46     7586972 :   if (hasMetadataHashEntry())
      47      472894 :     clearMetadataHashEntries();
      48     7586972 : }
      49             : 
      50             : 
      51    20696177 : void Instruction::setParent(BasicBlock *P) {
      52    20696177 :   Parent = P;
      53    20696177 : }
      54             : 
      55    33169907 : const Module *Instruction::getModule() const {
      56    33169907 :   return getParent()->getModule();
      57             : }
      58             : 
      59     5140440 : const Function *Instruction::getFunction() const {
      60     5140440 :   return getParent()->getParent();
      61             : }
      62             : 
      63      486631 : void Instruction::removeFromParent() {
      64      486631 :   getParent()->getInstList().remove(getIterator());
      65      486631 : }
      66             : 
      67     2231413 : iplist<Instruction>::iterator Instruction::eraseFromParent() {
      68     4462826 :   return getParent()->getInstList().erase(getIterator());
      69             : }
      70             : 
      71             : /// Insert an unlinked instruction into a basic block immediately before the
      72             : /// specified instruction.
      73       19888 : void Instruction::insertBefore(Instruction *InsertPos) {
      74       19888 :   InsertPos->getParent()->getInstList().insert(InsertPos->getIterator(), this);
      75       19888 : }
      76             : 
      77             : /// Insert an unlinked instruction into a basic block immediately after the
      78             : /// specified instruction.
      79       13896 : void Instruction::insertAfter(Instruction *InsertPos) {
      80       27792 :   InsertPos->getParent()->getInstList().insertAfter(InsertPos->getIterator(),
      81       13896 :                                                     this);
      82       13896 : }
      83             : 
      84             : /// Unlink this instruction from its current basic block and insert it into the
      85             : /// basic block that MovePos lives in, right before MovePos.
      86       22939 : void Instruction::moveBefore(Instruction *MovePos) {
      87       22939 :   moveBefore(*MovePos->getParent(), MovePos->getIterator());
      88       22939 : }
      89             : 
      90        1443 : void Instruction::moveAfter(Instruction *MovePos) {
      91        2886 :   moveBefore(*MovePos->getParent(), ++MovePos->getIterator());
      92        1443 : }
      93             : 
      94       24730 : void Instruction::moveBefore(BasicBlock &BB,
      95             :                              SymbolTableList<Instruction>::iterator I) {
      96             :   assert(I == BB.end() || I->getParent() == &BB);
      97       24730 :   BB.getInstList().splice(I, getParent()->getInstList(), getIterator());
      98       24730 : }
      99             : 
     100      238278 : void Instruction::setHasNoUnsignedWrap(bool b) {
     101             :   cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b);
     102      238278 : }
     103             : 
     104      319031 : void Instruction::setHasNoSignedWrap(bool b) {
     105             :   cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b);
     106      319031 : }
     107             : 
     108        5090 : void Instruction::setIsExact(bool b) {
     109             :   cast<PossiblyExactOperator>(this)->setIsExact(b);
     110        5090 : }
     111             : 
     112     8927708 : bool Instruction::hasNoUnsignedWrap() const {
     113     8927708 :   return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap();
     114             : }
     115             : 
     116     8886018 : bool Instruction::hasNoSignedWrap() const {
     117     8886018 :   return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap();
     118             : }
     119             : 
     120         319 : void Instruction::dropPoisonGeneratingFlags() {
     121             :   switch (getOpcode()) {
     122             :   case Instruction::Add:
     123             :   case Instruction::Sub:
     124             :   case Instruction::Mul:
     125             :   case Instruction::Shl:
     126             :     cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(false);
     127             :     cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(false);
     128             :     break;
     129             : 
     130             :   case Instruction::UDiv:
     131             :   case Instruction::SDiv:
     132             :   case Instruction::AShr:
     133             :   case Instruction::LShr:
     134             :     cast<PossiblyExactOperator>(this)->setIsExact(false);
     135             :     break;
     136             : 
     137             :   case Instruction::GetElementPtr:
     138           1 :     cast<GetElementPtrInst>(this)->setIsInBounds(false);
     139           1 :     break;
     140             :   }
     141         319 : }
     142             : 
     143       90883 : bool Instruction::isExact() const {
     144       90883 :   return cast<PossiblyExactOperator>(this)->isExact();
     145             : }
     146             : 
     147           1 : void Instruction::setFast(bool B) {
     148             :   assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
     149           1 :   cast<FPMathOperator>(this)->setFast(B);
     150           1 : }
     151             : 
     152           0 : void Instruction::setHasAllowReassoc(bool B) {
     153             :   assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
     154             :   cast<FPMathOperator>(this)->setHasAllowReassoc(B);
     155           0 : }
     156             : 
     157           1 : void Instruction::setHasNoNaNs(bool B) {
     158             :   assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
     159             :   cast<FPMathOperator>(this)->setHasNoNaNs(B);
     160           1 : }
     161             : 
     162           0 : void Instruction::setHasNoInfs(bool B) {
     163             :   assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
     164             :   cast<FPMathOperator>(this)->setHasNoInfs(B);
     165           0 : }
     166             : 
     167           0 : void Instruction::setHasNoSignedZeros(bool B) {
     168             :   assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
     169             :   cast<FPMathOperator>(this)->setHasNoSignedZeros(B);
     170           0 : }
     171             : 
     172           2 : void Instruction::setHasAllowReciprocal(bool B) {
     173             :   assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
     174             :   cast<FPMathOperator>(this)->setHasAllowReciprocal(B);
     175           2 : }
     176             : 
     177           0 : void Instruction::setHasApproxFunc(bool B) {
     178             :   assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
     179             :   cast<FPMathOperator>(this)->setHasApproxFunc(B);
     180           0 : }
     181             : 
     182       37487 : void Instruction::setFastMathFlags(FastMathFlags FMF) {
     183             :   assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
     184             :   cast<FPMathOperator>(this)->setFastMathFlags(FMF);
     185       37487 : }
     186             : 
     187        9042 : void Instruction::copyFastMathFlags(FastMathFlags FMF) {
     188             :   assert(isa<FPMathOperator>(this) && "copying fast-math flag on invalid op");
     189             :   cast<FPMathOperator>(this)->copyFastMathFlags(FMF);
     190        9042 : }
     191             : 
     192       33242 : bool Instruction::isFast() const {
     193             :   assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
     194       33242 :   return cast<FPMathOperator>(this)->isFast();
     195             : }
     196             : 
     197       14209 : bool Instruction::hasAllowReassoc() const {
     198             :   assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
     199       14209 :   return cast<FPMathOperator>(this)->hasAllowReassoc();
     200             : }
     201             : 
     202       78694 : bool Instruction::hasNoNaNs() const {
     203             :   assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
     204       78694 :   return cast<FPMathOperator>(this)->hasNoNaNs();
     205             : }
     206             : 
     207           2 : bool Instruction::hasNoInfs() const {
     208             :   assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
     209           2 :   return cast<FPMathOperator>(this)->hasNoInfs();
     210             : }
     211             : 
     212        5979 : bool Instruction::hasNoSignedZeros() const {
     213             :   assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
     214        5979 :   return cast<FPMathOperator>(this)->hasNoSignedZeros();
     215             : }
     216             : 
     217         521 : bool Instruction::hasAllowReciprocal() const {
     218             :   assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
     219         521 :   return cast<FPMathOperator>(this)->hasAllowReciprocal();
     220             : }
     221             : 
     222           6 : bool Instruction::hasAllowContract() const {
     223             :   assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
     224           6 :   return cast<FPMathOperator>(this)->hasAllowContract();
     225             : }
     226             : 
     227           4 : bool Instruction::hasApproxFunc() const {
     228             :   assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
     229           4 :   return cast<FPMathOperator>(this)->hasApproxFunc();
     230             : }
     231             : 
     232       71468 : FastMathFlags Instruction::getFastMathFlags() const {
     233             :   assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
     234       71468 :   return cast<FPMathOperator>(this)->getFastMathFlags();
     235             : }
     236             : 
     237         385 : void Instruction::copyFastMathFlags(const Instruction *I) {
     238         385 :   copyFastMathFlags(I->getFastMathFlags());
     239         385 : }
     240             : 
     241       75437 : void Instruction::copyIRFlags(const Value *V, bool IncludeWrapFlags) {
     242             :   // Copy the wrapping flags.
     243       75437 :   if (IncludeWrapFlags && isa<OverflowingBinaryOperator>(this)) {
     244       73148 :     if (auto *OB = dyn_cast<OverflowingBinaryOperator>(V)) {
     245       73148 :       setHasNoSignedWrap(OB->hasNoSignedWrap());
     246       73148 :       setHasNoUnsignedWrap(OB->hasNoUnsignedWrap());
     247             :     }
     248             :   }
     249             : 
     250             :   // Copy the exact flag.
     251         197 :   if (auto *PE = dyn_cast<PossiblyExactOperator>(V))
     252             :     if (isa<PossiblyExactOperator>(this))
     253         197 :       setIsExact(PE->isExact());
     254             : 
     255             :   // Copy the fast-math flags.
     256        1531 :   if (auto *FP = dyn_cast<FPMathOperator>(V))
     257        1531 :     if (isa<FPMathOperator>(this))
     258        1531 :       copyFastMathFlags(FP->getFastMathFlags());
     259             : 
     260             :   if (auto *SrcGEP = dyn_cast<GetElementPtrInst>(V))
     261             :     if (auto *DestGEP = dyn_cast<GetElementPtrInst>(this))
     262           0 :       DestGEP->setIsInBounds(SrcGEP->isInBounds() | DestGEP->isInBounds());
     263       75437 : }
     264             : 
     265      245370 : void Instruction::andIRFlags(const Value *V) {
     266      149666 :   if (auto *OB = dyn_cast<OverflowingBinaryOperator>(V)) {
     267             :     if (isa<OverflowingBinaryOperator>(this)) {
     268      299282 :       setHasNoSignedWrap(hasNoSignedWrap() & OB->hasNoSignedWrap());
     269      299282 :       setHasNoUnsignedWrap(hasNoUnsignedWrap() & OB->hasNoUnsignedWrap());
     270             :     }
     271             :   }
     272             : 
     273        1601 :   if (auto *PE = dyn_cast<PossiblyExactOperator>(V))
     274             :     if (isa<PossiblyExactOperator>(this))
     275        3202 :       setIsExact(isExact() & PE->isExact());
     276             : 
     277        7126 :   if (auto *FP = dyn_cast<FPMathOperator>(V)) {
     278        7126 :     if (isa<FPMathOperator>(this)) {
     279        7126 :       FastMathFlags FM = getFastMathFlags();
     280             :       FM &= FP->getFastMathFlags();
     281        7126 :       copyFastMathFlags(FM);
     282             :     }
     283             :   }
     284             : 
     285             :   if (auto *SrcGEP = dyn_cast<GetElementPtrInst>(V))
     286             :     if (auto *DestGEP = dyn_cast<GetElementPtrInst>(this))
     287       29967 :       DestGEP->setIsInBounds(SrcGEP->isInBounds() & DestGEP->isInBounds());
     288      245370 : }
     289             : 
     290     2303426 : const char *Instruction::getOpcodeName(unsigned OpCode) {
     291     2303426 :   switch (OpCode) {
     292             :   // Terminators
     293             :   case Ret:    return "ret";
     294      151525 :   case Br:     return "br";
     295        1102 :   case Switch: return "switch";
     296         222 :   case IndirectBr: return "indirectbr";
     297        3136 :   case Invoke: return "invoke";
     298         544 :   case Resume: return "resume";
     299        4479 :   case Unreachable: return "unreachable";
     300         318 :   case CleanupRet: return "cleanupret";
     301         211 :   case CatchRet: return "catchret";
     302         303 :   case CatchPad: return "catchpad";
     303         295 :   case CatchSwitch: return "catchswitch";
     304             : 
     305             :   // Standard binary operators...
     306       59952 :   case Add: return "add";
     307        6877 :   case FAdd: return "fadd";
     308       14747 :   case Sub: return "sub";
     309        1625 :   case FSub: return "fsub";
     310       15005 :   case Mul: return "mul";
     311        3278 :   case FMul: return "fmul";
     312        1401 :   case UDiv: return "udiv";
     313        4890 :   case SDiv: return "sdiv";
     314         979 :   case FDiv: return "fdiv";
     315        1315 :   case URem: return "urem";
     316        1560 :   case SRem: return "srem";
     317         161 :   case FRem: return "frem";
     318             : 
     319             :   // Logical operators...
     320        9701 :   case And: return "and";
     321        5051 :   case Or : return "or";
     322        4492 :   case Xor: return "xor";
     323             : 
     324             :   // Memory instructions...
     325      307237 :   case Alloca:        return "alloca";
     326      413506 :   case Load:          return "load";
     327      390118 :   case Store:         return "store";
     328         682 :   case AtomicCmpXchg: return "cmpxchg";
     329         925 :   case AtomicRMW:     return "atomicrmw";
     330         227 :   case Fence:         return "fence";
     331      189242 :   case GetElementPtr: return "getelementptr";
     332             : 
     333             :   // Convert instructions...
     334       11895 :   case Trunc:         return "trunc";
     335       11104 :   case ZExt:          return "zext";
     336       12186 :   case SExt:          return "sext";
     337        1213 :   case FPTrunc:       return "fptrunc";
     338        3375 :   case FPExt:         return "fpext";
     339         870 :   case FPToUI:        return "fptoui";
     340        1761 :   case FPToSI:        return "fptosi";
     341        1101 :   case UIToFP:        return "uitofp";
     342        3313 :   case SIToFP:        return "sitofp";
     343        3733 :   case IntToPtr:      return "inttoptr";
     344        9620 :   case PtrToInt:      return "ptrtoint";
     345      184090 :   case BitCast:       return "bitcast";
     346        1148 :   case AddrSpaceCast: return "addrspacecast";
     347             : 
     348             :   // Other instructions...
     349       61022 :   case ICmp:           return "icmp";
     350        3103 :   case FCmp:           return "fcmp";
     351       25639 :   case PHI:            return "phi";
     352        7019 :   case Select:         return "select";
     353      173453 :   case Call:           return "call";
     354        3732 :   case Shl:            return "shl";
     355        3244 :   case LShr:           return "lshr";
     356        2090 :   case AShr:           return "ashr";
     357          49 :   case VAArg:          return "va_arg";
     358        7899 :   case ExtractElement: return "extractelement";
     359       13946 :   case InsertElement:  return "insertelement";
     360       10977 :   case ShuffleVector:  return "shufflevector";
     361        9357 :   case ExtractValue:   return "extractvalue";
     362        2712 :   case InsertValue:    return "insertvalue";
     363        1772 :   case LandingPad:     return "landingpad";
     364         441 :   case CleanupPad:     return "cleanuppad";
     365             : 
     366           0 :   default: return "<Invalid operator> ";
     367             :   }
     368             : }
     369             : 
     370             : /// Return true if both instructions have the same special state. This must be
     371             : /// kept in sync with FunctionComparator::cmpOperations in
     372             : /// lib/Transforms/IPO/MergeFunctions.cpp.
     373     1329438 : static bool haveSameSpecialState(const Instruction *I1, const Instruction *I2,
     374             :                                  bool IgnoreAlignment = false) {
     375             :   assert(I1->getOpcode() == I2->getOpcode() &&
     376             :          "Can not compare special state of different instructions");
     377             : 
     378             :   if (const AllocaInst *AI = dyn_cast<AllocaInst>(I1))
     379           4 :     return AI->getAllocatedType() == cast<AllocaInst>(I2)->getAllocatedType() &&
     380           0 :            (AI->getAlignment() == cast<AllocaInst>(I2)->getAlignment() ||
     381             :             IgnoreAlignment);
     382             :   if (const LoadInst *LI = dyn_cast<LoadInst>(I1))
     383        2958 :     return LI->isVolatile() == cast<LoadInst>(I2)->isVolatile() &&
     384           3 :            (LI->getAlignment() == cast<LoadInst>(I2)->getAlignment() ||
     385        2955 :             IgnoreAlignment) &&
     386        5913 :            LI->getOrdering() == cast<LoadInst>(I2)->getOrdering() &&
     387        2955 :            LI->getSyncScopeID() == cast<LoadInst>(I2)->getSyncScopeID();
     388             :   if (const StoreInst *SI = dyn_cast<StoreInst>(I1))
     389        6623 :     return SI->isVolatile() == cast<StoreInst>(I2)->isVolatile() &&
     390        1265 :            (SI->getAlignment() == cast<StoreInst>(I2)->getAlignment() ||
     391        5358 :             IgnoreAlignment) &&
     392       11980 :            SI->getOrdering() == cast<StoreInst>(I2)->getOrdering() &&
     393        5356 :            SI->getSyncScopeID() == cast<StoreInst>(I2)->getSyncScopeID();
     394             :   if (const CmpInst *CI = dyn_cast<CmpInst>(I1))
     395      195503 :     return CI->getPredicate() == cast<CmpInst>(I2)->getPredicate();
     396             :   if (const CallInst *CI = dyn_cast<CallInst>(I1))
     397       47648 :     return CI->isTailCall() == cast<CallInst>(I2)->isTailCall() &&
     398       47648 :            CI->getCallingConv() == cast<CallInst>(I2)->getCallingConv() &&
     399       95192 :            CI->getAttributes() == cast<CallInst>(I2)->getAttributes() &&
     400       47544 :            CI->hasIdenticalOperandBundleSchema(*cast<CallInst>(I2));
     401             :   if (const InvokeInst *CI = dyn_cast<InvokeInst>(I1))
     402           0 :     return CI->getCallingConv() == cast<InvokeInst>(I2)->getCallingConv() &&
     403           0 :            CI->getAttributes() == cast<InvokeInst>(I2)->getAttributes() &&
     404           0 :            CI->hasIdenticalOperandBundleSchema(*cast<InvokeInst>(I2));
     405             :   if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(I1))
     406        8751 :     return IVI->getIndices() == cast<InsertValueInst>(I2)->getIndices();
     407             :   if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(I1))
     408       83084 :     return EVI->getIndices() == cast<ExtractValueInst>(I2)->getIndices();
     409             :   if (const FenceInst *FI = dyn_cast<FenceInst>(I1))
     410          17 :     return FI->getOrdering() == cast<FenceInst>(I2)->getOrdering() &&
     411           6 :            FI->getSyncScopeID() == cast<FenceInst>(I2)->getSyncScopeID();
     412             :   if (const AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(I1))
     413           0 :     return CXI->isVolatile() == cast<AtomicCmpXchgInst>(I2)->isVolatile() &&
     414           0 :            CXI->isWeak() == cast<AtomicCmpXchgInst>(I2)->isWeak() &&
     415             :            CXI->getSuccessOrdering() ==
     416           0 :                cast<AtomicCmpXchgInst>(I2)->getSuccessOrdering() &&
     417             :            CXI->getFailureOrdering() ==
     418           0 :                cast<AtomicCmpXchgInst>(I2)->getFailureOrdering() &&
     419           0 :            CXI->getSyncScopeID() ==
     420           0 :                cast<AtomicCmpXchgInst>(I2)->getSyncScopeID();
     421             :   if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(I1))
     422           3 :     return RMWI->getOperation() == cast<AtomicRMWInst>(I2)->getOperation() &&
     423           3 :            RMWI->isVolatile() == cast<AtomicRMWInst>(I2)->isVolatile() &&
     424           6 :            RMWI->getOrdering() == cast<AtomicRMWInst>(I2)->getOrdering() &&
     425           3 :            RMWI->getSyncScopeID() == cast<AtomicRMWInst>(I2)->getSyncScopeID();
     426             : 
     427             :   return true;
     428             : }
     429             : 
     430      141805 : bool Instruction::isIdenticalTo(const Instruction *I) const {
     431      150207 :   return isIdenticalToWhenDefined(I) &&
     432      150207 :          SubclassOptionalData == I->SubclassOptionalData;
     433             : }
     434             : 
     435     2595661 : bool Instruction::isIdenticalToWhenDefined(const Instruction *I) const {
     436     2394818 :   if (getOpcode() != I->getOpcode() ||
     437     4856701 :       getNumOperands() != I->getNumOperands() ||
     438     2261040 :       getType() != I->getType())
     439             :     return false;
     440             : 
     441             :   // If both instructions have no operands, they are identical.
     442     2151256 :   if (getNumOperands() == 0 && I->getNumOperands() == 0)
     443          17 :     return haveSameSpecialState(this, I);
     444             : 
     445             :   // We have two instructions of identical opcode and #operands.  Check to see
     446             :   // if all operands are the same.
     447     6453717 :   if (!std::equal(op_begin(), op_end(), I->op_begin()))
     448             :     return false;
     449             : 
     450             :   if (const PHINode *thisPHI = dyn_cast<PHINode>(this)) {
     451             :     const PHINode *otherPHI = cast<PHINode>(I);
     452             :     return std::equal(thisPHI->block_begin(), thisPHI->block_end(),
     453             :                       otherPHI->block_begin());
     454             :   }
     455             : 
     456     1290296 :   return haveSameSpecialState(this, I);
     457             : }
     458             : 
     459             : // Keep this in sync with FunctionComparator::cmpOperations in
     460             : // lib/Transforms/IPO/MergeFunctions.cpp.
     461       94370 : bool Instruction::isSameOperationAs(const Instruction *I,
     462             :                                     unsigned flags) const {
     463       94370 :   bool IgnoreAlignment = flags & CompareIgnoringAlignment;
     464       94370 :   bool UseScalarTypes  = flags & CompareUsingScalarTypes;
     465             : 
     466       39742 :   if (getOpcode() != I->getOpcode() ||
     467      134084 :       getNumOperands() != I->getNumOperands() ||
     468       39714 :       (UseScalarTypes ?
     469           0 :        getType()->getScalarType() != I->getType()->getScalarType() :
     470       39714 :        getType() != I->getType()))
     471             :     return false;
     472             : 
     473             :   // We have two instructions of identical opcode and #operands.  Check to see
     474             :   // if all operands are the same type
     475      186872 :   for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
     476      148178 :     if (UseScalarTypes ?
     477           0 :         getOperand(i)->getType()->getScalarType() !=
     478           0 :           I->getOperand(i)->getType()->getScalarType() :
     479      222267 :         getOperand(i)->getType() != I->getOperand(i)->getType())
     480             :       return false;
     481             : 
     482       39125 :   return haveSameSpecialState(this, I, IgnoreAlignment);
     483             : }
     484             : 
     485         135 : bool Instruction::isUsedOutsideOfBlock(const BasicBlock *BB) const {
     486         276 :   for (const Use &U : uses()) {
     487             :     // PHI nodes uses values in the corresponding predecessor block.  For other
     488             :     // instructions, just check to see whether the parent of the use matches up.
     489         143 :     const Instruction *I = cast<Instruction>(U.getUser());
     490             :     const PHINode *PN = dyn_cast<PHINode>(I);
     491         141 :     if (!PN) {
     492         143 :       if (I->getParent() != BB)
     493             :         return true;
     494         141 :       continue;
     495             :     }
     496             : 
     497           0 :     if (PN->getIncomingBlock(U) != BB)
     498             :       return true;
     499             :   }
     500             :   return false;
     501             : }
     502             : 
     503    10748560 : bool Instruction::mayReadFromMemory() const {
     504    10748560 :   switch (getOpcode()) {
     505             :   default: return false;
     506     1225631 :   case Instruction::VAArg:
     507             :   case Instruction::Load:
     508             :   case Instruction::Fence: // FIXME: refine definition of mayReadFromMemory
     509             :   case Instruction::AtomicCmpXchg:
     510             :   case Instruction::AtomicRMW:
     511             :   case Instruction::CatchPad:
     512             :   case Instruction::CatchRet:
     513     1225631 :     return true;
     514             :   case Instruction::Call:
     515     1821098 :     return !cast<CallInst>(this)->doesNotAccessMemory();
     516             :   case Instruction::Invoke:
     517      269652 :     return !cast<InvokeInst>(this)->doesNotAccessMemory();
     518             :   case Instruction::Store:
     519     1651190 :     return !cast<StoreInst>(this)->isUnordered();
     520             :   }
     521             : }
     522             : 
     523    47048144 : bool Instruction::mayWriteToMemory() const {
     524    47048144 :   switch (getOpcode()) {
     525             :   default: return false;
     526    15019651 :   case Instruction::Fence: // FIXME: refine definition of mayWriteToMemory
     527             :   case Instruction::Store:
     528             :   case Instruction::VAArg:
     529             :   case Instruction::AtomicCmpXchg:
     530             :   case Instruction::AtomicRMW:
     531             :   case Instruction::CatchPad:
     532             :   case Instruction::CatchRet:
     533    15019651 :     return true;
     534             :   case Instruction::Call:
     535    10350371 :     return !cast<CallInst>(this)->onlyReadsMemory();
     536             :   case Instruction::Invoke:
     537      196233 :     return !cast<InvokeInst>(this)->onlyReadsMemory();
     538             :   case Instruction::Load:
     539     5741904 :     return !cast<LoadInst>(this)->isUnordered();
     540             :   }
     541             : }
     542             : 
     543    28622409 : bool Instruction::isAtomic() const {
     544    28622409 :   switch (getOpcode()) {
     545             :   default:
     546             :     return false;
     547        4209 :   case Instruction::AtomicCmpXchg:
     548             :   case Instruction::AtomicRMW:
     549             :   case Instruction::Fence:
     550        4209 :     return true;
     551             :   case Instruction::Load:
     552    16345797 :     return cast<LoadInst>(this)->getOrdering() != AtomicOrdering::NotAtomic;
     553             :   case Instruction::Store:
     554    10413353 :     return cast<StoreInst>(this)->getOrdering() != AtomicOrdering::NotAtomic;
     555             :   }
     556             : }
     557             : 
     558         494 : bool Instruction::hasAtomicLoad() const {
     559             :   assert(isAtomic());
     560             :   switch (getOpcode()) {
     561             :   default:
     562             :     return false;
     563         473 :   case Instruction::AtomicCmpXchg:
     564             :   case Instruction::AtomicRMW:
     565             :   case Instruction::Load:
     566         473 :     return true;
     567             :   }
     568             : }
     569             : 
     570         236 : bool Instruction::hasAtomicStore() const {
     571             :   assert(isAtomic());
     572             :   switch (getOpcode()) {
     573             :   default:
     574             :     return false;
     575         201 :   case Instruction::AtomicCmpXchg:
     576             :   case Instruction::AtomicRMW:
     577             :   case Instruction::Store:
     578         201 :     return true;
     579             :   }
     580             : }
     581             : 
     582    14526244 : bool Instruction::mayThrow() const {
     583             :   if (const CallInst *CI = dyn_cast<CallInst>(this))
     584     1932048 :     return !CI->doesNotThrow();
     585             :   if (const auto *CRI = dyn_cast<CleanupReturnInst>(this))
     586          67 :     return CRI->unwindsToCaller();
     587             :   if (const auto *CatchSwitch = dyn_cast<CatchSwitchInst>(this))
     588          99 :     return CatchSwitch->unwindsToCaller();
     589             :   return isa<ResumeInst>(this);
     590             : }
     591             : 
     592        1925 : bool Instruction::isSafeToRemove() const {
     593        3692 :   return (!isa<CallInst>(this) || !this->mayHaveSideEffects()) &&
     594        1925 :          !isa<TerminatorInst>(this);
     595             : }
     596             : 
     597     9722632 : bool Instruction::isAssociative() const {
     598             :   unsigned Opcode = getOpcode();
     599             :   if (isAssociative(Opcode))
     600             :     return true;
     601             : 
     602     1901141 :   switch (Opcode) {
     603             :   case FMul:
     604             :   case FAdd:
     605       14620 :     return cast<FPMathOperator>(this)->hasAllowReassoc() &&
     606             :            cast<FPMathOperator>(this)->hasNoSignedZeros();
     607             :   default:
     608             :     return false;
     609             :   }
     610             : }
     611             : 
     612           0 : Instruction *Instruction::cloneImpl() const {
     613           0 :   llvm_unreachable("Subclass of Instruction failed to implement cloneImpl");
     614             : }
     615             : 
     616        3922 : void Instruction::swapProfMetadata() {
     617             :   MDNode *ProfileData = getMetadata(LLVMContext::MD_prof);
     618        4097 :   if (!ProfileData || ProfileData->getNumOperands() != 3 ||
     619             :       !isa<MDString>(ProfileData->getOperand(0)))
     620        2875 :     return;
     621             : 
     622             :   MDString *MDName = cast<MDString>(ProfileData->getOperand(0));
     623        1048 :   if (MDName->getString() != "branch_weights")
     624             :     return;
     625             : 
     626             :   // The first operand is the name. Fetch them backwards and build a new one.
     627             :   Metadata *Ops[] = {ProfileData->getOperand(0), ProfileData->getOperand(2),
     628        3141 :                      ProfileData->getOperand(1)};
     629        1047 :   setMetadata(LLVMContext::MD_prof,
     630             :               MDNode::get(ProfileData->getContext(), Ops));
     631             : }
     632             : 
     633     2120904 : void Instruction::copyMetadata(const Instruction &SrcInst,
     634             :                                ArrayRef<unsigned> WL) {
     635             :   if (!SrcInst.hasMetadata())
     636      546878 :     return;
     637             : 
     638             :   DenseSet<unsigned> WLS;
     639     1798272 :   for (unsigned M : WL)
     640             :     WLS.insert(M);
     641             : 
     642             :   // Otherwise, enumerate and copy over metadata from the old instruction to the
     643             :   // new one.
     644             :   SmallVector<std::pair<unsigned, MDNode *>, 4> TheMDs;
     645             :   SrcInst.getAllMetadataOtherThanDebugLoc(TheMDs);
     646     1926090 :   for (const auto &MD : TheMDs) {
     647      176032 :     if (WL.empty() || WLS.count(MD.first))
     648      104868 :       setMetadata(MD.first, MD.second);
     649             :   }
     650     1686086 :   if (WL.empty() || WLS.count(LLVMContext::MD_dbg))
     651     1461966 :     setDebugLoc(SrcInst.getDebugLoc());
     652             : }
     653             : 
     654     1993727 : Instruction *Instruction::clone() const {
     655             :   Instruction *New = nullptr;
     656     1993727 :   switch (getOpcode()) {
     657           0 :   default:
     658           0 :     llvm_unreachable("Unhandled Opcode.");
     659             : #define HANDLE_INST(num, opc, clas)                                            \
     660             :   case Instruction::opc:                                                       \
     661             :     New = cast<clas>(this)->cloneImpl();                                       \
     662             :     break;
     663             : #include "llvm/IR/Instruction.def"
     664             : #undef HANDLE_INST
     665             :   }
     666             : 
     667     1993728 :   New->SubclassOptionalData = SubclassOptionalData;
     668     1993728 :   New->copyMetadata(*this);
     669     1993728 :   return New;
     670             : }
     671             : 
     672          60 : void Instruction::updateProfWeight(uint64_t S, uint64_t T) {
     673             :   auto *ProfileData = getMetadata(LLVMContext::MD_prof);
     674           6 :   if (ProfileData == nullptr)
     675          54 :     return;
     676             : 
     677             :   auto *ProfDataName = dyn_cast<MDString>(ProfileData->getOperand(0));
     678          12 :   if (!ProfDataName || (!ProfDataName->getString().equals("branch_weights") &&
     679          10 :                         !ProfDataName->getString().equals("VP")))
     680             :     return;
     681             : 
     682           6 :   MDBuilder MDB(getContext());
     683             :   SmallVector<Metadata *, 3> Vals;
     684           6 :   Vals.push_back(ProfileData->getOperand(0));
     685             :   APInt APS(128, S), APT(128, T);
     686           6 :   if (ProfDataName->getString().equals("branch_weights"))
     687           6 :     for (unsigned i = 1; i < ProfileData->getNumOperands(); i++) {
     688             :       // Using APInt::div may be expensive, but most cases should fit 64 bits.
     689             :       APInt Val(128,
     690             :                 mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(i))
     691             :                     ->getValue()
     692             :                     .getZExtValue());
     693           2 :       Val *= APS;
     694           2 :       Vals.push_back(MDB.createConstant(
     695           2 :           ConstantInt::get(Type::getInt64Ty(getContext()),
     696           4 :                            Val.udiv(APT).getLimitedValue())));
     697             :     }
     698           4 :   else if (ProfDataName->getString().equals("VP"))
     699          36 :     for (unsigned i = 1; i < ProfileData->getNumOperands(); i += 2) {
     700             :       // The first value is the key of the value profile, which will not change.
     701          16 :       Vals.push_back(ProfileData->getOperand(i));
     702             :       // Using APInt::div may be expensive, but most cases should fit 64 bits.
     703             :       APInt Val(128,
     704             :                 mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(i + 1))
     705          16 :                     ->getValue()
     706             :                     .getZExtValue());
     707          16 :       Val *= APS;
     708          16 :       Vals.push_back(MDB.createConstant(
     709          16 :           ConstantInt::get(Type::getInt64Ty(getContext()),
     710          32 :                            Val.udiv(APT).getLimitedValue())));
     711             :     }
     712          12 :   setMetadata(LLVMContext::MD_prof, MDNode::get(getContext(), Vals));
     713             : }
     714             : 
     715           6 : void Instruction::setProfWeight(uint64_t W) {
     716             :   assert((isa<CallInst>(this) || isa<InvokeInst>(this)) &&
     717             :          "Can only set weights for call and invoke instrucitons");
     718             :   SmallVector<uint32_t, 1> Weights;
     719           6 :   Weights.push_back(W);
     720           6 :   MDBuilder MDB(getContext());
     721           6 :   setMetadata(LLVMContext::MD_prof, MDB.createBranchWeights(Weights));
     722           6 : }

Generated by: LCOV version 1.13