LCOV - code coverage report
Current view: top level - lib/IR - Value.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 361 373 96.8 %
Date: 2017-09-14 15:23:50 Functions: 42 44 95.5 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===-- Value.cpp - Implement the Value 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 Value, ValueHandle, and User classes.
      11             : //
      12             : //===----------------------------------------------------------------------===//
      13             : 
      14             : #include "llvm/IR/Value.h"
      15             : #include "LLVMContextImpl.h"
      16             : #include "llvm/ADT/DenseMap.h"
      17             : #include "llvm/ADT/SmallString.h"
      18             : #include "llvm/IR/CallSite.h"
      19             : #include "llvm/IR/Constant.h"
      20             : #include "llvm/IR/Constants.h"
      21             : #include "llvm/IR/DataLayout.h"
      22             : #include "llvm/IR/DerivedTypes.h"
      23             : #include "llvm/IR/DerivedUser.h"
      24             : #include "llvm/IR/GetElementPtrTypeIterator.h"
      25             : #include "llvm/IR/InstrTypes.h"
      26             : #include "llvm/IR/Instructions.h"
      27             : #include "llvm/IR/IntrinsicInst.h"
      28             : #include "llvm/IR/Module.h"
      29             : #include "llvm/IR/Operator.h"
      30             : #include "llvm/IR/Statepoint.h"
      31             : #include "llvm/IR/ValueHandle.h"
      32             : #include "llvm/IR/ValueSymbolTable.h"
      33             : #include "llvm/Support/Debug.h"
      34             : #include "llvm/Support/ErrorHandling.h"
      35             : #include "llvm/Support/ManagedStatic.h"
      36             : #include "llvm/Support/raw_ostream.h"
      37             : #include <algorithm>
      38             : 
      39             : using namespace llvm;
      40             : 
      41             : //===----------------------------------------------------------------------===//
      42             : //                                Value Class
      43             : //===----------------------------------------------------------------------===//
      44             : static inline Type *checkType(Type *Ty) {
      45             :   assert(Ty && "Value defined with a null type: Error!");
      46             :   return Ty;
      47             : }
      48             : 
      49    11955889 : Value::Value(Type *ty, unsigned scid)
      50    11955889 :     : VTy(checkType(ty)), UseList(nullptr), SubclassID(scid),
      51             :       HasValueHandle(0), SubclassOptionalData(0), SubclassData(0),
      52    11955889 :       NumUserOperands(0), IsUsedByMD(false), HasName(false) {
      53             :   // FIXME: Why isn't this in the subclass gunk??
      54             :   // Note, we cannot call isa<CallInst> before the CallInst has been
      55             :   // constructed.
      56             :   if (SubclassID == Instruction::Call || SubclassID == Instruction::Invoke)
      57             :     assert((VTy->isFirstClassType() || VTy->isVoidTy() || VTy->isStructTy()) &&
      58             :            "invalid CallInst type!");
      59             :   else if (SubclassID != BasicBlockVal &&
      60             :            (SubclassID < ConstantFirstVal || SubclassID > ConstantLastVal))
      61             :     assert((VTy->isFirstClassType() || VTy->isVoidTy()) &&
      62             :            "Cannot create non-first-class values except for constants!");
      63             :   static_assert(sizeof(Value) == 2 * sizeof(void *) + 2 * sizeof(unsigned),
      64             :                 "Value too big");
      65    11955889 : }
      66             : 
      67    20081706 : Value::~Value() {
      68             :   // Notify all ValueHandles (if present) that this value is going away.
      69    10040852 :   if (HasValueHandle)
      70       80881 :     ValueHandleBase::ValueIsDeleted(this);
      71    10040852 :   if (isUsedByMetadata())
      72       70239 :     ValueAsMetadata::handleDeletion(this);
      73             : 
      74             : #ifndef NDEBUG      // Only in -g mode...
      75             :   // Check to make sure that there are no uses of this value that are still
      76             :   // around when the value is destroyed.  If there are, then we have a dangling
      77             :   // reference and something is wrong.  This code is here to print out where
      78             :   // the value is still being referenced.
      79             :   //
      80             :   if (!use_empty()) {
      81             :     dbgs() << "While deleting: " << *VTy << " %" << getName() << "\n";
      82             :     for (auto *U : users())
      83             :       dbgs() << "Use still stuck around after Def is destroyed:" << *U << "\n";
      84             :   }
      85             : #endif
      86             :   assert(use_empty() && "Uses remain when a value is destroyed!");
      87             : 
      88             :   // If this value is named, destroy the name.  This should not be in a symtab
      89             :   // at this point.
      90    10040852 :   destroyValueName();
      91    10040854 : }
      92             : 
      93     5765010 : void Value::deleteValue() {
      94    11530020 :   switch (getValueID()) {
      95             : #define HANDLE_VALUE(Name)                                                     \
      96             :   case Value::Name##Val:                                                       \
      97             :     delete static_cast<Name *>(this);                                          \
      98             :     break;
      99             : #define HANDLE_MEMORY_VALUE(Name)                                              \
     100             :   case Value::Name##Val:                                                       \
     101             :     static_cast<DerivedUser *>(this)->DeleteValue(                             \
     102             :         static_cast<DerivedUser *>(this));                                     \
     103             :     break;
     104             : #define HANDLE_INSTRUCTION(Name)  /* nothing */
     105             : #include "llvm/IR/Value.def"
     106             : 
     107             : #define HANDLE_INST(N, OPC, CLASS)                                             \
     108             :   case Value::InstructionVal + Instruction::OPC:                               \
     109             :     delete static_cast<CLASS *>(this);                                         \
     110             :     break;
     111             : #define HANDLE_USER_INST(N, OPC, CLASS)
     112             : #include "llvm/IR/Instruction.def"
     113             : 
     114           0 :   default:
     115           0 :     llvm_unreachable("attempting to delete unknown value kind");
     116             :   }
     117     5765011 : }
     118             : 
     119    11233138 : void Value::destroyValueName() {
     120    11233138 :   ValueName *Name = getValueName();
     121    11233139 :   if (Name)
     122             :     Name->Destroy();
     123    11233139 :   setValueName(nullptr);
     124    11233142 : }
     125             : 
     126       32082 : bool Value::hasNUses(unsigned N) const {
     127       64164 :   const_use_iterator UI = use_begin(), E = use_end();
     128             : 
     129      148650 :   for (; N; --N, ++UI)
     130       58288 :     if (UI == E) return false;  // Too few.
     131       32078 :   return UI == E;
     132             : }
     133             : 
     134         605 : bool Value::hasNUsesOrMore(unsigned N) const {
     135        1210 :   const_use_iterator UI = use_begin(), E = use_end();
     136             : 
     137        3545 :   for (; N; --N, ++UI)
     138        1731 :     if (UI == E) return false;  // Too few.
     139             : 
     140             :   return true;
     141             : }
     142             : 
     143      115397 : bool Value::isUsedInBasicBlock(const BasicBlock *BB) const {
     144             :   // This can be computed either by scanning the instructions in BB, or by
     145             :   // scanning the use list of this Value. Both lists can be very long, but
     146             :   // usually one is quite short.
     147             :   //
     148             :   // Scan both lists simultaneously until one is exhausted. This limits the
     149             :   // search to the shorter list.
     150      230794 :   BasicBlock::const_iterator BI = BB->begin(), BE = BB->end();
     151      230794 :   const_user_iterator UI = user_begin(), UE = user_end();
     152      313471 :   for (; BI != BE && UI != UE; ++BI, ++UI) {
     153             :     // Scan basic block: Check if this Value is used by the instruction at BI.
     154      557712 :     if (is_contained(BI->operands(), this))
     155             :       return true;
     156             :     // Scan use list: Check if the use at UI is in BB.
     157      267024 :     const auto *User = dyn_cast<Instruction>(*UI);
     158      133512 :     if (User && User->getParent() == BB)
     159             :       return true;
     160             :   }
     161             :   return false;
     162             : }
     163             : 
     164        2365 : unsigned Value::getNumUses() const {
     165        9460 :   return (unsigned)std::distance(use_begin(), use_end());
     166             : }
     167             : 
     168     2942862 : static bool getSymTab(Value *V, ValueSymbolTable *&ST) {
     169     2942862 :   ST = nullptr;
     170     1332925 :   if (Instruction *I = dyn_cast<Instruction>(V)) {
     171     1332925 :     if (BasicBlock *P = I->getParent())
     172     1147850 :       if (Function *PP = P->getParent())
     173     1146343 :         ST = PP->getValueSymbolTable();
     174      187588 :   } else if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
     175      187588 :     if (Function *P = BB->getParent())
     176      172938 :       ST = P->getValueSymbolTable();
     177      548629 :   } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
     178      548629 :     if (Module *P = GV->getParent())
     179        6005 :       ST = &P->getValueSymbolTable();
     180      873553 :   } else if (Argument *A = dyn_cast<Argument>(V)) {
     181      873553 :     if (Function *P = A->getParent())
     182      859844 :       ST = P->getValueSymbolTable();
     183             :   } else {
     184             :     assert(isa<Constant>(V) && "Unknown value type!");
     185             :     return true;  // no name is setable for this.
     186             :   }
     187             :   return false;
     188             : }
     189             : 
     190   102811815 : ValueName *Value::getValueName() const {
     191   102811815 :   if (!HasName) return nullptr;
     192             : 
     193    94014938 :   LLVMContext &Ctx = getContext();
     194    94014935 :   auto I = Ctx.pImpl->ValueNames.find(this);
     195             :   assert(I != Ctx.pImpl->ValueNames.end() &&
     196             :          "No name entry found!");
     197             : 
     198    94014951 :   return I->second;
     199             : }
     200             : 
     201    13843140 : void Value::setValueName(ValueName *VN) {
     202    13843140 :   LLVMContext &Ctx = getContext();
     203             : 
     204             :   assert(HasName == Ctx.pImpl->ValueNames.count(this) &&
     205             :          "HasName bit out of sync!");
     206             : 
     207    13843140 :   if (!VN) {
     208    11243117 :     if (HasName)
     209     2446212 :       Ctx.pImpl->ValueNames.erase(this);
     210    11243117 :     HasName = false;
     211    11243117 :     return;
     212             :   }
     213             : 
     214     2600023 :   HasName = true;
     215     5200046 :   Ctx.pImpl->ValueNames[this] = VN;
     216             : }
     217             : 
     218    90599966 : StringRef Value::getName() const {
     219             :   // Make sure the empty string is still a C string. For historical reasons,
     220             :   // some clients want to call .data() on the result and expect it to be null
     221             :   // terminated.
     222    90599966 :   if (!hasName())
     223     3183863 :     return StringRef("", 0);
     224    87416103 :   return getValueName()->getKey();
     225             : }
     226             : 
     227    11193563 : void Value::setNameImpl(const Twine &NewName) {
     228             :   // Fast-path: LLVMContext can be set to strip out non-GlobalValue names
     229    11193563 :   if (getContext().shouldDiscardValueNames() && !isa<GlobalValue>(this))
     230     9456407 :     return;
     231             : 
     232             :   // Fast path for common IRBuilder case of setName("") when there is no name.
     233     4265806 :   if (NewName.isTriviallyEmpty() && !hasName())
     234             :     return;
     235             : 
     236     4677981 :   SmallString<256> NameData;
     237     2940824 :   StringRef NameRef = NewName.toStringRef(NameData);
     238             :   assert(NameRef.find_first_of(0) == StringRef::npos &&
     239             :          "Null bytes are not allowed in names");
     240             : 
     241             :   // Name isn't changing?
     242     2940824 :   if (getName() == NameRef)
     243             :     return;
     244             : 
     245             :   assert(!getType()->isVoidTy() && "Cannot assign a name to void values!");
     246             : 
     247             :   // Get the symbol table to update for this object.
     248             :   ValueSymbolTable *ST;
     249     2922438 :   if (getSymTab(this, ST))
     250             :     return;  // Cannot set a name on this value (e.g. constant).
     251             : 
     252     2922273 :   if (!ST) { // No symbol table to update?  Just do the change.
     253      751603 :     if (NameRef.empty()) {
     254             :       // Free the name for this value.
     255           0 :       destroyValueName();
     256           0 :       return;
     257             :     }
     258             : 
     259             :     // NOTE: Could optimize for the case the name is shrinking to not deallocate
     260             :     // then reallocated.
     261      751603 :     destroyValueName();
     262             : 
     263             :     // Create the new name.
     264      751604 :     setValueName(ValueName::Create(NameRef));
     265      751604 :     getValueName()->setValue(this);
     266      751604 :     return;
     267             :   }
     268             : 
     269             :   // NOTE: Could optimize for the case the name is shrinking to not deallocate
     270             :   // then reallocated.
     271     2170670 :   if (hasName()) {
     272             :     // Remove old name.
     273      439668 :     ST->removeValueName(getValueName());
     274      439668 :     destroyValueName();
     275             : 
     276      439668 :     if (NameRef.empty())
     277             :       return;
     278             :   }
     279             : 
     280             :   // Name is changing to something new.
     281     1737157 :   setValueName(ST->createValueName(NameRef, this));
     282             : }
     283             : 
     284    11193563 : void Value::setName(const Twine &NewName) {
     285    11193563 :   setNameImpl(NewName);
     286      429810 :   if (Function *F = dyn_cast<Function>(this))
     287      429810 :     F->recalculateIntrinsicID();
     288    11193564 : }
     289             : 
     290      137250 : void Value::takeName(Value *V) {
     291      137250 :   ValueSymbolTable *ST = nullptr;
     292             :   // If this value has a name, drop it.
     293      137250 :   if (hasName()) {
     294             :     // Get the symtab this is in.
     295        1016 :     if (getSymTab(this, ST)) {
     296             :       // We can't set a name on this value, but we need to clear V's name if
     297             :       // it has one.
     298           0 :       if (V->hasName()) V->setName("");
     299      131400 :       return;  // Cannot set a name on this value (e.g. constant).
     300             :     }
     301             : 
     302             :     // Remove old name.
     303        1016 :     if (ST)
     304         548 :       ST->removeValueName(getValueName());
     305        1016 :     destroyValueName();
     306             :   }
     307             : 
     308             :   // Now we know that this has no name.
     309             : 
     310             :   // If V has no name either, we're done.
     311      137250 :   if (!V->hasName()) return;
     312             : 
     313             :   // Get this's symtab if we didn't before.
     314        9979 :   if (!ST) {
     315        9432 :     if (getSymTab(this, ST)) {
     316             :       // Clear V's name.
     317           3 :       V->setName("");
     318           3 :       return;  // Cannot set a name on this value (e.g. constant).
     319             :     }
     320             :   }
     321             : 
     322             :   // Get V's ST, this should always succed, because V has a name.
     323             :   ValueSymbolTable *VST;
     324        9976 :   bool Failure = getSymTab(V, VST);
     325             :   assert(!Failure && "V has a name, so it should have a ST!"); (void)Failure;
     326             : 
     327             :   // If these values are both in the same symtab, we can do this very fast.
     328             :   // This works even if both values have no symtab yet.
     329        9976 :   if (ST == VST) {
     330             :     // Take the name!
     331        4126 :     setValueName(V->getValueName());
     332        4126 :     V->setValueName(nullptr);
     333        4126 :     getValueName()->setValue(this);
     334        4126 :     return;
     335             :   }
     336             : 
     337             :   // Otherwise, things are slightly more complex.  Remove V's name from VST and
     338             :   // then reinsert it into ST.
     339             : 
     340        5850 :   if (VST)
     341        5756 :     VST->removeValueName(V->getValueName());
     342        5850 :   setValueName(V->getValueName());
     343        5850 :   V->setValueName(nullptr);
     344        5850 :   getValueName()->setValue(this);
     345             : 
     346        5850 :   if (ST)
     347         451 :     ST->reinsertValue(this);
     348             : }
     349             : 
     350           0 : void Value::assertModuleIsMaterializedImpl() const {
     351             : #ifndef NDEBUG
     352             :   const GlobalValue *GV = dyn_cast<GlobalValue>(this);
     353             :   if (!GV)
     354             :     return;
     355             :   const Module *M = GV->getParent();
     356             :   if (!M)
     357             :     return;
     358             :   assert(M->isMaterialized());
     359             : #endif
     360           0 : }
     361             : 
     362             : #ifndef NDEBUG
     363             : static bool contains(SmallPtrSetImpl<ConstantExpr *> &Cache, ConstantExpr *Expr,
     364             :                      Constant *C) {
     365             :   if (!Cache.insert(Expr).second)
     366             :     return false;
     367             : 
     368             :   for (auto &O : Expr->operands()) {
     369             :     if (O == C)
     370             :       return true;
     371             :     auto *CE = dyn_cast<ConstantExpr>(O);
     372             :     if (!CE)
     373             :       continue;
     374             :     if (contains(Cache, CE, C))
     375             :       return true;
     376             :   }
     377             :   return false;
     378             : }
     379             : 
     380             : static bool contains(Value *Expr, Value *V) {
     381             :   if (Expr == V)
     382             :     return true;
     383             : 
     384             :   auto *C = dyn_cast<Constant>(V);
     385             :   if (!C)
     386             :     return false;
     387             : 
     388             :   auto *CE = dyn_cast<ConstantExpr>(Expr);
     389             :   if (!CE)
     390             :     return false;
     391             : 
     392             :   SmallPtrSet<ConstantExpr *, 4> Cache;
     393             :   return contains(Cache, CE, C);
     394             : }
     395             : #endif // NDEBUG
     396             : 
     397     1554163 : void Value::doRAUW(Value *New, bool NoMetadata) {
     398             :   assert(New && "Value::replaceAllUsesWith(<null>) is invalid!");
     399             :   assert(!contains(New, this) &&
     400             :          "this->replaceAllUsesWith(expr(this)) is NOT valid!");
     401             :   assert(New->getType() == getType() &&
     402             :          "replaceAllUses of value with new value of different type!");
     403             : 
     404             :   // Notify all ValueHandles (if present) that this value is going away.
     405     1554163 :   if (HasValueHandle)
     406       25854 :     ValueHandleBase::ValueIsRAUWd(this, New);
     407     3108294 :   if (!NoMetadata && isUsedByMetadata())
     408        6495 :     ValueAsMetadata::handleRAUW(this, New);
     409             : 
     410     5121240 :   while (!use_empty()) {
     411     1006457 :     Use &U = *UseList;
     412             :     // Must handle Constants specially, we cannot call replaceUsesOfWith on a
     413             :     // constant because they are uniqued.
     414     1009806 :     if (auto *C = dyn_cast<Constant>(U.getUser())) {
     415        6484 :       if (!isa<GlobalValue>(C)) {
     416        3135 :         C->handleOperandChange(this, New);
     417        3135 :         continue;
     418             :       }
     419             :     }
     420             : 
     421     1003322 :     U.set(New);
     422             :   }
     423             : 
     424      212288 :   if (BasicBlock *BB = dyn_cast<BasicBlock>(this))
     425      212288 :     BB->replaceSuccessorsPhiUsesWith(cast<BasicBlock>(New));
     426     1554163 : }
     427             : 
     428     1554131 : void Value::replaceAllUsesWith(Value *New) {
     429     1554131 :   doRAUW(New, false /* NoMetadata */);
     430     1554131 : }
     431             : 
     432          32 : void Value::replaceNonMetadataUsesWith(Value *New) {
     433          32 :   doRAUW(New, true /* NoMetadata */);
     434          32 : }
     435             : 
     436             : // Like replaceAllUsesWith except it does not handle constants or basic blocks.
     437             : // This routine leaves uses within BB.
     438          27 : void Value::replaceUsesOutsideBlock(Value *New, BasicBlock *BB) {
     439             :   assert(New && "Value::replaceUsesOutsideBlock(<null>, BB) is invalid!");
     440             :   assert(!contains(New, this) &&
     441             :          "this->replaceUsesOutsideBlock(expr(this), BB) is NOT valid!");
     442             :   assert(New->getType() == getType() &&
     443             :          "replaceUses of value with new value of different type!");
     444             :   assert(BB && "Basic block that may contain a use of 'New' must be defined\n");
     445             : 
     446          54 :   use_iterator UI = use_begin(), E = use_end();
     447          83 :   for (; UI != E;) {
     448          56 :     Use &U = *UI;
     449          56 :     ++UI;
     450         112 :     auto *Usr = dyn_cast<Instruction>(U.getUser());
     451          85 :     if (Usr && Usr->getParent() == BB)
     452          29 :       continue;
     453          27 :     U.set(New);
     454             :   }
     455          27 : }
     456             : 
     457             : namespace {
     458             : // Various metrics for how much to strip off of pointers.
     459             : enum PointerStripKind {
     460             :   PSK_ZeroIndices,
     461             :   PSK_ZeroIndicesAndAliases,
     462             :   PSK_ZeroIndicesAndAliasesAndBarriers,
     463             :   PSK_InBoundsConstantIndices,
     464             :   PSK_InBounds
     465             : };
     466             : 
     467             : template <PointerStripKind StripKind>
     468    77651812 : static const Value *stripPointerCastsAndOffsets(const Value *V) {
     469   155303624 :   if (!V->getType()->isPointerTy())
     470             :     return V;
     471             : 
     472             :   // Even though we don't look through PHI nodes, we could be called on an
     473             :   // instruction in an unreachable block, which may be on a cycle.
     474    76545871 :   SmallPtrSet<const Value *, 4> Visited;
     475             : 
     476    76545871 :   Visited.insert(V);
     477             :   do {
     478    39327624 :     if (auto *GEP = dyn_cast<GEPOperator>(V)) {
     479             :       switch (StripKind) {
     480    39324843 :       case PSK_ZeroIndicesAndAliases:
     481             :       case PSK_ZeroIndicesAndAliasesAndBarriers:
     482             :       case PSK_ZeroIndices:
     483    39324843 :         if (!GEP->hasAllZeroIndices())
     484             :           return V;
     485             :         break;
     486          22 :       case PSK_InBoundsConstantIndices:
     487          22 :         if (!GEP->hasAllConstantIndices())
     488             :           return V;
     489             :         LLVM_FALLTHROUGH;
     490             :       case PSK_InBounds:
     491        2775 :         if (!GEP->isInBounds())
     492             :           return V;
     493             :         break;
     494             :       }
     495    13770163 :       V = GEP->getPointerOperand();
     496    16986248 :     } else if (Operator::getOpcode(V) == Instruction::BitCast ||
     497     4719104 :                Operator::getOpcode(V) == Instruction::AddrSpaceCast) {
     498    15097784 :       V = cast<Operator>(V)->getOperand(0);
     499         351 :     } else if (auto *GA = dyn_cast<GlobalAlias>(V)) {
     500         647 :       if (StripKind == PSK_ZeroIndices || GA->isInterposable())
     501             :         return V;
     502             :       V = GA->getAliasee();
     503             :     } else {
     504   101976876 :       if (auto CS = ImmutableCallSite(V)) {
     505      157697 :         if (const Value *RV = CS.getReturnedArgOperand()) {
     506         122 :           V = RV;
     507         212 :           continue;
     508             :         }
     509             :         // The result of invariant.group.barrier must alias it's argument,
     510             :         // but it can't be marked with returned attribute, that's why it needs
     511             :         // special case.
     512       66777 :         if (StripKind == PSK_ZeroIndicesAndAliasesAndBarriers &&
     513       66748 :             CS.getIntrinsicID() == Intrinsic::invariant_group_barrier) {
     514          29 :           V = CS.getArgOperand(0);
     515          29 :           continue;
     516             :         }
     517             :       }
     518    50988287 :       return V;
     519             :     }
     520             :     assert(V->getType()->isPointerTy() && "Unexpected operand type!");
     521    21319502 :   } while (Visited.insert(V).second);
     522             : 
     523             :   return V;
     524             : }
     525             : } // end anonymous namespace
     526             : 
     527    40890809 : const Value *Value::stripPointerCasts() const {
     528    40890809 :   return stripPointerCastsAndOffsets<PSK_ZeroIndicesAndAliases>(this);
     529             : }
     530             : 
     531        9843 : const Value *Value::stripPointerCastsNoFollowAliases() const {
     532        9843 :   return stripPointerCastsAndOffsets<PSK_ZeroIndices>(this);
     533             : }
     534             : 
     535         707 : const Value *Value::stripInBoundsConstantOffsets() const {
     536         707 :   return stripPointerCastsAndOffsets<PSK_InBoundsConstantIndices>(this);
     537             : }
     538             : 
     539    36679196 : const Value *Value::stripPointerCastsAndBarriers() const {
     540             :   return stripPointerCastsAndOffsets<PSK_ZeroIndicesAndAliasesAndBarriers>(
     541    36679196 :       this);
     542             : }
     543             : 
     544             : const Value *
     545     1113077 : Value::stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL,
     546             :                                                  APInt &Offset) const {
     547     2226154 :   if (!getType()->isPointerTy())
     548             :     return this;
     549             : 
     550             :   assert(Offset.getBitWidth() == DL.getPointerSizeInBits(cast<PointerType>(
     551             :                                      getType())->getAddressSpace()) &&
     552             :          "The offset must have exactly as many bits as our pointer.");
     553             : 
     554             :   // Even though we don't look through PHI nodes, we could be called on an
     555             :   // instruction in an unreachable block, which may be on a cycle.
     556     1113064 :   SmallPtrSet<const Value *, 4> Visited;
     557     1113064 :   Visited.insert(this);
     558     1113064 :   const Value *V = this;
     559             :   do {
     560      983994 :     if (auto *GEP = dyn_cast<GEPOperator>(V)) {
     561      983994 :       if (!GEP->isInBounds())
     562       37849 :         return V;
     563     1926120 :       APInt GEPOffset(Offset);
     564      979975 :       if (!GEP->accumulateConstantOffset(DL, GEPOffset))
     565       33830 :         return V;
     566      946145 :       Offset = GEPOffset;
     567      946145 :       V = GEP->getPointerOperand();
     568      151330 :     } else if (Operator::getOpcode(V) == Instruction::BitCast) {
     569       61824 :       V = cast<Operator>(V)->getOperand(0);
     570           0 :     } else if (auto *GA = dyn_cast<GlobalAlias>(V)) {
     571             :       V = GA->getAliasee();
     572             :     } else {
     573     2150430 :       if (auto CS = ImmutableCallSite(V))
     574       11674 :         if (const Value *RV = CS.getReturnedArgOperand()) {
     575           0 :           V = RV;
     576           0 :           continue;
     577             :         }
     578             : 
     579     1075215 :       return V;
     580             :     }
     581             :     assert(V->getType()->isPointerTy() && "Unexpected operand type!");
     582      977057 :   } while (Visited.insert(V).second);
     583             : 
     584             :   return V;
     585             : }
     586             : 
     587       71257 : const Value *Value::stripInBoundsOffsets() const {
     588       71257 :   return stripPointerCastsAndOffsets<PSK_InBounds>(this);
     589             : }
     590             : 
     591      547081 : unsigned Value::getPointerDereferenceableBytes(const DataLayout &DL,
     592             :                                                bool &CanBeNull) const {
     593             :   assert(getType()->isPointerTy() && "must be pointer");
     594             : 
     595      547081 :   unsigned DerefBytes = 0;
     596      547081 :   CanBeNull = false;
     597      547081 :   if (const Argument *A = dyn_cast<Argument>(this)) {
     598       48956 :     DerefBytes = A->getDereferenceableBytes();
     599       48956 :     if (DerefBytes == 0 && A->hasByValAttr() && A->getType()->isSized()) {
     600        1468 :       DerefBytes = DL.getTypeStoreSize(A->getType());
     601         734 :       CanBeNull = false;
     602             :     }
     603       48956 :     if (DerefBytes == 0) {
     604       46055 :       DerefBytes = A->getDereferenceableOrNullBytes();
     605       46055 :       CanBeNull = true;
     606             :     }
     607      996250 :   } else if (auto CS = ImmutableCallSite(this)) {
     608        1035 :     DerefBytes = CS.getDereferenceableBytes(AttributeList::ReturnIndex);
     609        1035 :     if (DerefBytes == 0) {
     610         652 :       DerefBytes = CS.getDereferenceableOrNullBytes(AttributeList::ReturnIndex);
     611         652 :       CanBeNull = true;
     612             :     }
     613       18500 :   } else if (const LoadInst *LI = dyn_cast<LoadInst>(this)) {
     614       35269 :     if (MDNode *MD = LI->getMetadata(LLVMContext::MD_dereferenceable)) {
     615          24 :       ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0));
     616          12 :       DerefBytes = CI->getLimitedValue();
     617             :     }
     618          12 :     if (DerefBytes == 0) {
     619       16757 :       if (MDNode *MD =
     620       35245 :               LI->getMetadata(LLVMContext::MD_dereferenceable_or_null)) {
     621          16 :         ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0));
     622           8 :         DerefBytes = CI->getLimitedValue();
     623             :       }
     624       18488 :       CanBeNull = true;
     625             :     }
     626       29371 :   } else if (auto *AI = dyn_cast<AllocaInst>(this)) {
     627       29371 :     if (AI->getAllocatedType()->isSized()) {
     628       58742 :       DerefBytes = DL.getTypeStoreSize(AI->getAllocatedType());
     629       29371 :       CanBeNull = false;
     630             :     }
     631      217385 :   } else if (auto *GV = dyn_cast<GlobalVariable>(this)) {
     632      434767 :     if (GV->getValueType()->isSized() && !GV->hasExternalWeakLinkage()) {
     633             :       // TODO: Don't outright reject hasExternalWeakLinkage but set the
     634             :       // CanBeNull flag.
     635      434744 :       DerefBytes = DL.getTypeStoreSize(GV->getValueType());
     636      217372 :       CanBeNull = false;
     637             :     }
     638             :   }
     639      547081 :   return DerefBytes;
     640             : }
     641             : 
     642    19090869 : unsigned Value::getPointerAlignment(const DataLayout &DL) const {
     643             :   assert(getType()->isPointerTy() && "must be pointer");
     644             : 
     645    19090869 :   unsigned Align = 0;
     646    19090869 :   if (auto *GO = dyn_cast<GlobalObject>(this)) {
     647     8934254 :     Align = GO->getAlignment();
     648     8934254 :     if (Align == 0) {
     649     7502764 :       if (auto *GVar = dyn_cast<GlobalVariable>(GO)) {
     650     7502764 :         Type *ObjectType = GVar->getValueType();
     651     7502764 :         if (ObjectType->isSized()) {
     652             :           // If the object is defined in the current Module, we'll be giving
     653             :           // it the preferred alignment. Otherwise, we have to assume that it
     654             :           // may only have the minimum ABI alignment.
     655     7502762 :           if (GVar->isStrongDefinitionForLinker())
     656     7467323 :             Align = DL.getPreferredAlignment(GVar);
     657             :           else
     658       35439 :             Align = DL.getABITypeAlignment(ObjectType);
     659             :         }
     660             :       }
     661             :     }
     662    10156615 :   } else if (const Argument *A = dyn_cast<Argument>(this)) {
     663      897756 :     Align = A->getParamAlignment();
     664             : 
     665      897756 :     if (!Align && A->hasStructRetAttr()) {
     666             :       // An sret parameter has at least the ABI alignment of the return type.
     667       82154 :       Type *EltTy = cast<PointerType>(A->getType())->getElementType();
     668       41077 :       if (EltTy->isSized())
     669       41077 :         Align = DL.getABITypeAlignment(EltTy);
     670             :     }
     671      429045 :   } else if (const AllocaInst *AI = dyn_cast<AllocaInst>(this)) {
     672      429045 :     Align = AI->getAlignment();
     673      429045 :     if (Align == 0) {
     674        2516 :       Type *AllocatedType = AI->getAllocatedType();
     675        2516 :       if (AllocatedType->isSized())
     676        2516 :         Align = DL.getPrefTypeAlignment(AllocatedType);
     677             :     }
     678    17659628 :   } else if (auto CS = ImmutableCallSite(this))
     679      129036 :     Align = CS.getAttributes().getRetAlignment();
     680      704957 :   else if (const LoadInst *LI = dyn_cast<LoadInst>(this))
     681     1400091 :     if (MDNode *MD = LI->getMetadata(LLVMContext::MD_align)) {
     682          10 :       ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0));
     683           5 :       Align = CI->getLimitedValue();
     684             :     }
     685             : 
     686    19090869 :   return Align;
     687             : }
     688             : 
     689        8150 : const Value *Value::DoPHITranslation(const BasicBlock *CurBB,
     690             :                                      const BasicBlock *PredBB) const {
     691        1360 :   auto *PN = dyn_cast<PHINode>(this);
     692        1360 :   if (PN && PN->getParent() == CurBB)
     693        1115 :     return PN->getIncomingValueForBlock(PredBB);
     694             :   return this;
     695             : }
     696             : 
     697   196433705 : LLVMContext &Value::getContext() const { return VTy->getContext(); }
     698             : 
     699         736 : void Value::reverseUseList() {
     700         736 :   if (!UseList || !UseList->Next)
     701             :     // No need to reverse 0 or 1 uses.
     702             :     return;
     703             : 
     704         736 :   Use *Head = UseList;
     705         736 :   Use *Current = UseList->Next;
     706         736 :   Head->Next = nullptr;
     707        4214 :   while (Current) {
     708        1739 :     Use *Next = Current->Next;
     709        1739 :     Current->Next = Head;
     710        3478 :     Head->setPrev(&Current->Next);
     711        1739 :     Head = Current;
     712        1739 :     Current = Next;
     713             :   }
     714         736 :   UseList = Head;
     715         736 :   Head->setPrev(&UseList);
     716             : }
     717             : 
     718     7754002 : bool Value::isSwiftError() const {
     719     7754002 :   auto *Arg = dyn_cast<Argument>(this);
     720             :   if (Arg)
     721       30975 :     return Arg->hasSwiftErrorAttr();
     722       79520 :   auto *Alloca = dyn_cast<AllocaInst>(this);
     723             :   if (!Alloca)
     724             :     return false;
     725       79520 :   return Alloca->isSwiftError();
     726             : }
     727             : 
     728             : //===----------------------------------------------------------------------===//
     729             : //                             ValueHandleBase Class
     730             : //===----------------------------------------------------------------------===//
     731             : 
     732    19307811 : void ValueHandleBase::AddToExistingUseList(ValueHandleBase **List) {
     733             :   assert(List && "Handle list is null?");
     734             : 
     735             :   // Splice ourselves into the list.
     736    19307811 :   Next = *List;
     737    19307811 :   *List = this;
     738    19307811 :   setPrevPtr(List);
     739    19307811 :   if (Next) {
     740    12255779 :     Next->setPrevPtr(&Next);
     741             :     assert(getValPtr() == Next->getValPtr() && "Added to wrong list?");
     742             :   }
     743    19307811 : }
     744             : 
     745      159624 : void ValueHandleBase::AddToExistingUseListAfter(ValueHandleBase *List) {
     746             :   assert(List && "Must insert after existing node");
     747             : 
     748      159624 :   Next = List->Next;
     749      319248 :   setPrevPtr(&List->Next);
     750      159624 :   List->Next = this;
     751      159624 :   if (Next)
     752       49575 :     Next->setPrevPtr(&Next);
     753      159624 : }
     754             : 
     755    10126530 : void ValueHandleBase::AddToUseList() {
     756             :   assert(getValPtr() && "Null pointer doesn't have a use list!");
     757             : 
     758    10126530 :   LLVMContextImpl *pImpl = getValPtr()->getContext().pImpl;
     759             : 
     760    10126531 :   if (getValPtr()->HasValueHandle) {
     761             :     // If this value already has a ValueHandle, then it must be in the
     762             :     // ValueHandles map already.
     763     6148998 :     ValueHandleBase *&Entry = pImpl->ValueHandles[getValPtr()];
     764             :     assert(Entry && "Value doesn't have any handles?");
     765     3074499 :     AddToExistingUseList(&Entry);
     766     3074499 :     return;
     767             :   }
     768             : 
     769             :   // Ok, it doesn't have any handles yet, so we must insert it into the
     770             :   // DenseMap.  However, doing this insertion could cause the DenseMap to
     771             :   // reallocate itself, which would invalidate all of the PrevP pointers that
     772             :   // point into the old table.  Handle this by checking for reallocation and
     773             :   // updating the stale pointers only if needed.
     774     7052032 :   DenseMap<Value*, ValueHandleBase*> &Handles = pImpl->ValueHandles;
     775     7052032 :   const void *OldBucketPtr = Handles.getPointerIntoBucketsArray();
     776             : 
     777    14104064 :   ValueHandleBase *&Entry = Handles[getValPtr()];
     778             :   assert(!Entry && "Value really did already have handles?");
     779     7052032 :   AddToExistingUseList(&Entry);
     780     7052032 :   getValPtr()->HasValueHandle = true;
     781             : 
     782             :   // If reallocation didn't happen or if this was the first insertion, don't
     783             :   // walk the table.
     784       35474 :   if (Handles.isPointerIntoBucketsArray(OldBucketPtr) ||
     785       35474 :       Handles.size() == 1) {
     786             :     return;
     787             :   }
     788             : 
     789             :   // Okay, reallocation did happen.  Fix the Prev Pointers.
     790        6327 :   for (DenseMap<Value*, ValueHandleBase*>::iterator I = Handles.begin(),
     791     3047766 :        E = Handles.end(); I != E; ++I) {
     792             :     assert(I->second && I->first == I->second->getValPtr() &&
     793             :            "List invariant broken!");
     794     6070224 :     I->second->setPrevPtr(&I->second);
     795             :   }
     796             : }
     797             : 
     798    19465734 : void ValueHandleBase::RemoveFromUseList() {
     799             :   assert(getValPtr() && getValPtr()->HasValueHandle &&
     800             :          "Pointer doesn't have a use list!");
     801             : 
     802             :   // Unlink this from its use list.
     803    19465734 :   ValueHandleBase **PrevPtr = getPrevPtr();
     804             :   assert(*PrevPtr == this && "List invariant broken");
     805             : 
     806    19465734 :   *PrevPtr = Next;
     807    19465734 :   if (Next) {
     808             :     assert(Next->getPrevPtr() == &Next && "List invariant broken");
     809     6526923 :     Next->setPrevPtr(PrevPtr);
     810             :     return;
     811             :   }
     812             : 
     813             :   // If the Next pointer was null, then it is possible that this was the last
     814             :   // ValueHandle watching VP.  If so, delete its entry from the ValueHandles
     815             :   // map.
     816    12938811 :   LLVMContextImpl *pImpl = getValPtr()->getContext().pImpl;
     817    12938811 :   DenseMap<Value*, ValueHandleBase*> &Handles = pImpl->ValueHandles;
     818     7050665 :   if (Handles.isPointerIntoBucketsArray(PrevPtr)) {
     819     7050665 :     Handles.erase(getValPtr());
     820     7050665 :     getValPtr()->HasValueHandle = false;
     821             :   }
     822             : }
     823             : 
     824       80881 : void ValueHandleBase::ValueIsDeleted(Value *V) {
     825             :   assert(V->HasValueHandle && "Should only be called if ValueHandles present");
     826             : 
     827             :   // Get the linked list base, which is guaranteed to exist since the
     828             :   // HasValueHandle flag is set.
     829       80881 :   LLVMContextImpl *pImpl = V->getContext().pImpl;
     830      161762 :   ValueHandleBase *Entry = pImpl->ValueHandles[V];
     831             :   assert(Entry && "Value bit set but no entries exist");
     832             : 
     833             :   // We use a local ValueHandleBase as an iterator so that ValueHandles can add
     834             :   // and remove themselves from the list without breaking our iteration.  This
     835             :   // is not really an AssertingVH; we just have to give ValueHandleBase a kind.
     836             :   // Note that we deliberately do not the support the case when dropping a value
     837             :   // handle results in a new value handle being permanently added to the list
     838             :   // (as might occur in theory for CallbackVH's): the new value handle will not
     839             :   // be processed and the checking code will mete out righteous punishment if
     840             :   // the handle is still present once we have finished processing all the other
     841             :   // value handles (it is fine to momentarily add then remove a value handle).
     842      280156 :   for (ValueHandleBase Iterator(Assert, *Entry); Entry; Entry = Iterator.Next) {
     843      118394 :     Iterator.RemoveFromUseList();
     844      118394 :     Iterator.AddToExistingUseListAfter(Entry);
     845             :     assert(Entry->Next == &Iterator && "Loop invariant broken.");
     846             : 
     847      118394 :     switch (Entry->getKind()) {
     848             :     case Assert:
     849             :       break;
     850       25012 :     case Weak:
     851             :     case WeakTracking:
     852             :       // WeakTracking and Weak just go to null, which unlinks them
     853             :       // from the list.
     854       25012 :       Entry->operator=(nullptr);
     855       25012 :       break;
     856             :     case Callback:
     857             :       // Forward to the subclass's implementation.
     858       93382 :       static_cast<CallbackVH*>(Entry)->deleted();
     859       93382 :       break;
     860             :     }
     861             :   }
     862             : 
     863             :   // All callbacks, weak references, and assertingVHs should be dropped by now.
     864       80881 :   if (V->HasValueHandle) {
     865             : #ifndef NDEBUG      // Only in +Asserts mode...
     866             :     dbgs() << "While deleting: " << *V->getType() << " %" << V->getName()
     867             :            << "\n";
     868             :     if (pImpl->ValueHandles[V]->getKind() == Assert)
     869             :       llvm_unreachable("An asserting value handle still pointed to this"
     870             :                        " value!");
     871             : 
     872             : #endif
     873           0 :     llvm_unreachable("All references to V were not removed?");
     874             :   }
     875       80881 : }
     876             : 
     877       29168 : void ValueHandleBase::ValueIsRAUWd(Value *Old, Value *New) {
     878             :   assert(Old->HasValueHandle &&"Should only be called if ValueHandles present");
     879             :   assert(Old != New && "Changing value into itself!");
     880             :   assert(Old->getType() == New->getType() &&
     881             :          "replaceAllUses of value with new value of different type!");
     882             : 
     883             :   // Get the linked list base, which is guaranteed to exist since the
     884             :   // HasValueHandle flag is set.
     885       29168 :   LLVMContextImpl *pImpl = Old->getContext().pImpl;
     886       58336 :   ValueHandleBase *Entry = pImpl->ValueHandles[Old];
     887             : 
     888             :   assert(Entry && "Value bit set but no entries exist");
     889             : 
     890             :   // We use a local ValueHandleBase as an iterator so that
     891             :   // ValueHandles can add and remove themselves from the list without
     892             :   // breaking our iteration.  This is not really an AssertingVH; we
     893             :   // just have to give ValueHandleBase some kind.
     894       99566 :   for (ValueHandleBase Iterator(Assert, *Entry); Entry; Entry = Iterator.Next) {
     895       41230 :     Iterator.RemoveFromUseList();
     896       41230 :     Iterator.AddToExistingUseListAfter(Entry);
     897             :     assert(Entry->Next == &Iterator && "Loop invariant broken.");
     898             : 
     899       41230 :     switch (Entry->getKind()) {
     900             :     case Assert:
     901             :     case Weak:
     902             :       // Asserting and Weak handles do not follow RAUW implicitly.
     903             :       break;
     904       30609 :     case WeakTracking:
     905             :       // Weak goes to the new value, which will unlink it from Old's list.
     906       30609 :       Entry->operator=(New);
     907       30609 :       break;
     908             :     case Callback:
     909             :       // Forward to the subclass's implementation.
     910       10580 :       static_cast<CallbackVH*>(Entry)->allUsesReplacedWith(New);
     911       10580 :       break;
     912             :     }
     913             :   }
     914             : 
     915             : #ifndef NDEBUG
     916             :   // If any new weak value handles were added while processing the
     917             :   // list, then complain about it now.
     918             :   if (Old->HasValueHandle)
     919             :     for (Entry = pImpl->ValueHandles[Old]; Entry; Entry = Entry->Next)
     920             :       switch (Entry->getKind()) {
     921             :       case WeakTracking:
     922             :         dbgs() << "After RAUW from " << *Old->getType() << " %"
     923             :                << Old->getName() << " to " << *New->getType() << " %"
     924             :                << New->getName() << "\n";
     925             :         llvm_unreachable(
     926             :             "A weak tracking value handle still pointed to the  old value!\n");
     927             :       default:
     928             :         break;
     929             :       }
     930             : #endif
     931       29168 : }
     932             : 
     933             : // Pin the vtable to this file.
     934           0 : void CallbackVH::anchor() {}

Generated by: LCOV version 1.13