LLVM API Documentation

Value.cpp
Go to the documentation of this file.
00001 //===-- Value.cpp - Implement the Value class -----------------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements the Value, ValueHandle, and User classes.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "llvm/IR/Value.h"
00015 #include "LLVMContextImpl.h"
00016 #include "llvm/ADT/DenseMap.h"
00017 #include "llvm/ADT/SmallString.h"
00018 #include "llvm/IR/CallSite.h"
00019 #include "llvm/IR/Constant.h"
00020 #include "llvm/IR/Constants.h"
00021 #include "llvm/IR/DataLayout.h"
00022 #include "llvm/IR/DerivedTypes.h"
00023 #include "llvm/IR/GetElementPtrTypeIterator.h"
00024 #include "llvm/IR/InstrTypes.h"
00025 #include "llvm/IR/Instructions.h"
00026 #include "llvm/IR/LeakDetector.h"
00027 #include "llvm/IR/Module.h"
00028 #include "llvm/IR/Operator.h"
00029 #include "llvm/IR/ValueHandle.h"
00030 #include "llvm/IR/ValueSymbolTable.h"
00031 #include "llvm/Support/Debug.h"
00032 #include "llvm/Support/ErrorHandling.h"
00033 #include "llvm/Support/ManagedStatic.h"
00034 #include <algorithm>
00035 using namespace llvm;
00036 
00037 //===----------------------------------------------------------------------===//
00038 //                                Value Class
00039 //===----------------------------------------------------------------------===//
00040 
00041 static inline Type *checkType(Type *Ty) {
00042   assert(Ty && "Value defined with a null type: Error!");
00043   return Ty;
00044 }
00045 
00046 Value::Value(Type *ty, unsigned scid)
00047     : VTy(checkType(ty)), UseList(nullptr), Name(nullptr), SubclassID(scid),
00048       HasValueHandle(0), SubclassOptionalData(0), SubclassData(0),
00049       NumOperands(0) {
00050   // FIXME: Why isn't this in the subclass gunk??
00051   // Note, we cannot call isa<CallInst> before the CallInst has been
00052   // constructed.
00053   if (SubclassID == Instruction::Call || SubclassID == Instruction::Invoke)
00054     assert((VTy->isFirstClassType() || VTy->isVoidTy() || VTy->isStructTy()) &&
00055            "invalid CallInst type!");
00056   else if (SubclassID != BasicBlockVal &&
00057            (SubclassID < ConstantFirstVal || SubclassID > ConstantLastVal))
00058     assert((VTy->isFirstClassType() || VTy->isVoidTy()) &&
00059            "Cannot create non-first-class values except for constants!");
00060 }
00061 
00062 Value::~Value() {
00063   // Notify all ValueHandles (if present) that this value is going away.
00064   if (HasValueHandle)
00065     ValueHandleBase::ValueIsDeleted(this);
00066 
00067 #ifndef NDEBUG      // Only in -g mode...
00068   // Check to make sure that there are no uses of this value that are still
00069   // around when the value is destroyed.  If there are, then we have a dangling
00070   // reference and something is wrong.  This code is here to print out what is
00071   // still being referenced.  The value in question should be printed as
00072   // a <badref>
00073   //
00074   if (!use_empty()) {
00075     dbgs() << "While deleting: " << *VTy << " %" << getName() << "\n";
00076     for (use_iterator I = use_begin(), E = use_end(); I != E; ++I)
00077       dbgs() << "Use still stuck around after Def is destroyed:"
00078            << **I << "\n";
00079   }
00080 #endif
00081   assert(use_empty() && "Uses remain when a value is destroyed!");
00082 
00083   // If this value is named, destroy the name.  This should not be in a symtab
00084   // at this point.
00085   if (Name && SubclassID != MDStringVal)
00086     Name->Destroy();
00087 
00088   // There should be no uses of this object anymore, remove it.
00089   LeakDetector::removeGarbageObject(this);
00090 }
00091 
00092 bool Value::hasNUses(unsigned N) const {
00093   const_use_iterator UI = use_begin(), E = use_end();
00094 
00095   for (; N; --N, ++UI)
00096     if (UI == E) return false;  // Too few.
00097   return UI == E;
00098 }
00099 
00100 bool Value::hasNUsesOrMore(unsigned N) const {
00101   const_use_iterator UI = use_begin(), E = use_end();
00102 
00103   for (; N; --N, ++UI)
00104     if (UI == E) return false;  // Too few.
00105 
00106   return true;
00107 }
00108 
00109 bool Value::isUsedInBasicBlock(const BasicBlock *BB) const {
00110   // This can be computed either by scanning the instructions in BB, or by
00111   // scanning the use list of this Value. Both lists can be very long, but
00112   // usually one is quite short.
00113   //
00114   // Scan both lists simultaneously until one is exhausted. This limits the
00115   // search to the shorter list.
00116   BasicBlock::const_iterator BI = BB->begin(), BE = BB->end();
00117   const_user_iterator UI = user_begin(), UE = user_end();
00118   for (; BI != BE && UI != UE; ++BI, ++UI) {
00119     // Scan basic block: Check if this Value is used by the instruction at BI.
00120     if (std::find(BI->op_begin(), BI->op_end(), this) != BI->op_end())
00121       return true;
00122     // Scan use list: Check if the use at UI is in BB.
00123     const Instruction *User = dyn_cast<Instruction>(*UI);
00124     if (User && User->getParent() == BB)
00125       return true;
00126   }
00127   return false;
00128 }
00129 
00130 unsigned Value::getNumUses() const {
00131   return (unsigned)std::distance(use_begin(), use_end());
00132 }
00133 
00134 static bool getSymTab(Value *V, ValueSymbolTable *&ST) {
00135   ST = nullptr;
00136   if (Instruction *I = dyn_cast<Instruction>(V)) {
00137     if (BasicBlock *P = I->getParent())
00138       if (Function *PP = P->getParent())
00139         ST = &PP->getValueSymbolTable();
00140   } else if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
00141     if (Function *P = BB->getParent())
00142       ST = &P->getValueSymbolTable();
00143   } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
00144     if (Module *P = GV->getParent())
00145       ST = &P->getValueSymbolTable();
00146   } else if (Argument *A = dyn_cast<Argument>(V)) {
00147     if (Function *P = A->getParent())
00148       ST = &P->getValueSymbolTable();
00149   } else if (isa<MDString>(V))
00150     return true;
00151   else {
00152     assert(isa<Constant>(V) && "Unknown value type!");
00153     return true;  // no name is setable for this.
00154   }
00155   return false;
00156 }
00157 
00158 StringRef Value::getName() const {
00159   // Make sure the empty string is still a C string. For historical reasons,
00160   // some clients want to call .data() on the result and expect it to be null
00161   // terminated.
00162   if (!Name) return StringRef("", 0);
00163   return Name->getKey();
00164 }
00165 
00166 void Value::setName(const Twine &NewName) {
00167   assert(SubclassID != MDStringVal &&
00168          "Cannot set the name of MDString with this method!");
00169 
00170   // Fast path for common IRBuilder case of setName("") when there is no name.
00171   if (NewName.isTriviallyEmpty() && !hasName())
00172     return;
00173 
00174   SmallString<256> NameData;
00175   StringRef NameRef = NewName.toStringRef(NameData);
00176   assert(NameRef.find_first_of(0) == StringRef::npos &&
00177          "Null bytes are not allowed in names");
00178 
00179   // Name isn't changing?
00180   if (getName() == NameRef)
00181     return;
00182 
00183   assert(!getType()->isVoidTy() && "Cannot assign a name to void values!");
00184 
00185   // Get the symbol table to update for this object.
00186   ValueSymbolTable *ST;
00187   if (getSymTab(this, ST))
00188     return;  // Cannot set a name on this value (e.g. constant).
00189 
00190   if (Function *F = dyn_cast<Function>(this))
00191     getContext().pImpl->IntrinsicIDCache.erase(F);
00192 
00193   if (!ST) { // No symbol table to update?  Just do the change.
00194     if (NameRef.empty()) {
00195       // Free the name for this value.
00196       Name->Destroy();
00197       Name = nullptr;
00198       return;
00199     }
00200 
00201     if (Name)
00202       Name->Destroy();
00203 
00204     // NOTE: Could optimize for the case the name is shrinking to not deallocate
00205     // then reallocated.
00206 
00207     // Create the new name.
00208     Name = ValueName::Create(NameRef);
00209     Name->setValue(this);
00210     return;
00211   }
00212 
00213   // NOTE: Could optimize for the case the name is shrinking to not deallocate
00214   // then reallocated.
00215   if (hasName()) {
00216     // Remove old name.
00217     ST->removeValueName(Name);
00218     Name->Destroy();
00219     Name = nullptr;
00220 
00221     if (NameRef.empty())
00222       return;
00223   }
00224 
00225   // Name is changing to something new.
00226   Name = ST->createValueName(NameRef, this);
00227 }
00228 
00229 void Value::takeName(Value *V) {
00230   assert(SubclassID != MDStringVal && "Cannot take the name of an MDString!");
00231 
00232   ValueSymbolTable *ST = nullptr;
00233   // If this value has a name, drop it.
00234   if (hasName()) {
00235     // Get the symtab this is in.
00236     if (getSymTab(this, ST)) {
00237       // We can't set a name on this value, but we need to clear V's name if
00238       // it has one.
00239       if (V->hasName()) V->setName("");
00240       return;  // Cannot set a name on this value (e.g. constant).
00241     }
00242 
00243     // Remove old name.
00244     if (ST)
00245       ST->removeValueName(Name);
00246     Name->Destroy();
00247     Name = nullptr;
00248   }
00249 
00250   // Now we know that this has no name.
00251 
00252   // If V has no name either, we're done.
00253   if (!V->hasName()) return;
00254 
00255   // Get this's symtab if we didn't before.
00256   if (!ST) {
00257     if (getSymTab(this, ST)) {
00258       // Clear V's name.
00259       V->setName("");
00260       return;  // Cannot set a name on this value (e.g. constant).
00261     }
00262   }
00263 
00264   // Get V's ST, this should always succed, because V has a name.
00265   ValueSymbolTable *VST;
00266   bool Failure = getSymTab(V, VST);
00267   assert(!Failure && "V has a name, so it should have a ST!"); (void)Failure;
00268 
00269   // If these values are both in the same symtab, we can do this very fast.
00270   // This works even if both values have no symtab yet.
00271   if (ST == VST) {
00272     // Take the name!
00273     Name = V->Name;
00274     V->Name = nullptr;
00275     Name->setValue(this);
00276     return;
00277   }
00278 
00279   // Otherwise, things are slightly more complex.  Remove V's name from VST and
00280   // then reinsert it into ST.
00281 
00282   if (VST)
00283     VST->removeValueName(V->Name);
00284   Name = V->Name;
00285   V->Name = nullptr;
00286   Name->setValue(this);
00287 
00288   if (ST)
00289     ST->reinsertValue(this);
00290 }
00291 
00292 #ifndef NDEBUG
00293 static bool contains(SmallPtrSetImpl<ConstantExpr *> &Cache, ConstantExpr *Expr,
00294                      Constant *C) {
00295   if (!Cache.insert(Expr))
00296     return false;
00297 
00298   for (auto &O : Expr->operands()) {
00299     if (O == C)
00300       return true;
00301     auto *CE = dyn_cast<ConstantExpr>(O);
00302     if (!CE)
00303       continue;
00304     if (contains(Cache, CE, C))
00305       return true;
00306   }
00307   return false;
00308 }
00309 
00310 static bool contains(Value *Expr, Value *V) {
00311   if (Expr == V)
00312     return true;
00313 
00314   auto *C = dyn_cast<Constant>(V);
00315   if (!C)
00316     return false;
00317 
00318   auto *CE = dyn_cast<ConstantExpr>(Expr);
00319   if (!CE)
00320     return false;
00321 
00322   SmallPtrSet<ConstantExpr *, 4> Cache;
00323   return contains(Cache, CE, C);
00324 }
00325 #endif
00326 
00327 void Value::replaceAllUsesWith(Value *New) {
00328   assert(New && "Value::replaceAllUsesWith(<null>) is invalid!");
00329   assert(!contains(New, this) &&
00330          "this->replaceAllUsesWith(expr(this)) is NOT valid!");
00331   assert(New->getType() == getType() &&
00332          "replaceAllUses of value with new value of different type!");
00333 
00334   // Notify all ValueHandles (if present) that this value is going away.
00335   if (HasValueHandle)
00336     ValueHandleBase::ValueIsRAUWd(this, New);
00337 
00338   while (!use_empty()) {
00339     Use &U = *UseList;
00340     // Must handle Constants specially, we cannot call replaceUsesOfWith on a
00341     // constant because they are uniqued.
00342     if (auto *C = dyn_cast<Constant>(U.getUser())) {
00343       if (!isa<GlobalValue>(C)) {
00344         C->replaceUsesOfWithOnConstant(this, New, &U);
00345         continue;
00346       }
00347     }
00348 
00349     U.set(New);
00350   }
00351 
00352   if (BasicBlock *BB = dyn_cast<BasicBlock>(this))
00353     BB->replaceSuccessorsPhiUsesWith(cast<BasicBlock>(New));
00354 }
00355 
00356 namespace {
00357 // Various metrics for how much to strip off of pointers.
00358 enum PointerStripKind {
00359   PSK_ZeroIndices,
00360   PSK_ZeroIndicesAndAliases,
00361   PSK_InBoundsConstantIndices,
00362   PSK_InBounds
00363 };
00364 
00365 template <PointerStripKind StripKind>
00366 static Value *stripPointerCastsAndOffsets(Value *V) {
00367   if (!V->getType()->isPointerTy())
00368     return V;
00369 
00370   // Even though we don't look through PHI nodes, we could be called on an
00371   // instruction in an unreachable block, which may be on a cycle.
00372   SmallPtrSet<Value *, 4> Visited;
00373 
00374   Visited.insert(V);
00375   do {
00376     if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
00377       switch (StripKind) {
00378       case PSK_ZeroIndicesAndAliases:
00379       case PSK_ZeroIndices:
00380         if (!GEP->hasAllZeroIndices())
00381           return V;
00382         break;
00383       case PSK_InBoundsConstantIndices:
00384         if (!GEP->hasAllConstantIndices())
00385           return V;
00386         // fallthrough
00387       case PSK_InBounds:
00388         if (!GEP->isInBounds())
00389           return V;
00390         break;
00391       }
00392       V = GEP->getPointerOperand();
00393     } else if (Operator::getOpcode(V) == Instruction::BitCast ||
00394                Operator::getOpcode(V) == Instruction::AddrSpaceCast) {
00395       V = cast<Operator>(V)->getOperand(0);
00396     } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
00397       if (StripKind == PSK_ZeroIndices || GA->mayBeOverridden())
00398         return V;
00399       V = GA->getAliasee();
00400     } else {
00401       return V;
00402     }
00403     assert(V->getType()->isPointerTy() && "Unexpected operand type!");
00404   } while (Visited.insert(V));
00405 
00406   return V;
00407 }
00408 } // namespace
00409 
00410 Value *Value::stripPointerCasts() {
00411   return stripPointerCastsAndOffsets<PSK_ZeroIndicesAndAliases>(this);
00412 }
00413 
00414 Value *Value::stripPointerCastsNoFollowAliases() {
00415   return stripPointerCastsAndOffsets<PSK_ZeroIndices>(this);
00416 }
00417 
00418 Value *Value::stripInBoundsConstantOffsets() {
00419   return stripPointerCastsAndOffsets<PSK_InBoundsConstantIndices>(this);
00420 }
00421 
00422 Value *Value::stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL,
00423                                                         APInt &Offset) {
00424   if (!getType()->isPointerTy())
00425     return this;
00426 
00427   assert(Offset.getBitWidth() == DL.getPointerSizeInBits(cast<PointerType>(
00428                                      getType())->getAddressSpace()) &&
00429          "The offset must have exactly as many bits as our pointer.");
00430 
00431   // Even though we don't look through PHI nodes, we could be called on an
00432   // instruction in an unreachable block, which may be on a cycle.
00433   SmallPtrSet<Value *, 4> Visited;
00434   Visited.insert(this);
00435   Value *V = this;
00436   do {
00437     if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
00438       if (!GEP->isInBounds())
00439         return V;
00440       APInt GEPOffset(Offset);
00441       if (!GEP->accumulateConstantOffset(DL, GEPOffset))
00442         return V;
00443       Offset = GEPOffset;
00444       V = GEP->getPointerOperand();
00445     } else if (Operator::getOpcode(V) == Instruction::BitCast ||
00446                Operator::getOpcode(V) == Instruction::AddrSpaceCast) {
00447       V = cast<Operator>(V)->getOperand(0);
00448     } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
00449       V = GA->getAliasee();
00450     } else {
00451       return V;
00452     }
00453     assert(V->getType()->isPointerTy() && "Unexpected operand type!");
00454   } while (Visited.insert(V));
00455 
00456   return V;
00457 }
00458 
00459 Value *Value::stripInBoundsOffsets() {
00460   return stripPointerCastsAndOffsets<PSK_InBounds>(this);
00461 }
00462 
00463 /// \brief Check if Value is always a dereferenceable pointer.
00464 ///
00465 /// Test if V is always a pointer to allocated and suitably aligned memory for
00466 /// a simple load or store.
00467 static bool isDereferenceablePointer(const Value *V, const DataLayout *DL,
00468                                      SmallPtrSetImpl<const Value *> &Visited) {
00469   // Note that it is not safe to speculate into a malloc'd region because
00470   // malloc may return null.
00471 
00472   // These are obviously ok.
00473   if (isa<AllocaInst>(V)) return true;
00474 
00475   // It's not always safe to follow a bitcast, for example:
00476   //   bitcast i8* (alloca i8) to i32*
00477   // would result in a 4-byte load from a 1-byte alloca. However,
00478   // if we're casting from a pointer from a type of larger size
00479   // to a type of smaller size (or the same size), and the alignment
00480   // is at least as large as for the resulting pointer type, then
00481   // we can look through the bitcast.
00482   if (DL)
00483     if (const BitCastInst* BC = dyn_cast<BitCastInst>(V)) {
00484       Type *STy = BC->getSrcTy()->getPointerElementType(),
00485            *DTy = BC->getDestTy()->getPointerElementType();
00486       if (STy->isSized() && DTy->isSized() &&
00487           (DL->getTypeStoreSize(STy) >=
00488            DL->getTypeStoreSize(DTy)) &&
00489           (DL->getABITypeAlignment(STy) >=
00490            DL->getABITypeAlignment(DTy)))
00491         return isDereferenceablePointer(BC->getOperand(0), DL, Visited);
00492     }
00493 
00494   // Global variables which can't collapse to null are ok.
00495   if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
00496     return !GV->hasExternalWeakLinkage();
00497 
00498   // byval arguments are okay. Arguments specifically marked as
00499   // dereferenceable are okay too.
00500   if (const Argument *A = dyn_cast<Argument>(V)) {
00501     if (A->hasByValAttr())
00502       return true;
00503     else if (uint64_t Bytes = A->getDereferenceableBytes()) {
00504       Type *Ty = V->getType()->getPointerElementType();
00505       if (Ty->isSized() && DL && DL->getTypeStoreSize(Ty) <= Bytes)
00506         return true;
00507     }
00508 
00509     return false;
00510   }
00511 
00512   // Return values from call sites specifically marked as dereferenceable are
00513   // also okay.
00514   if (ImmutableCallSite CS = V) {
00515     if (uint64_t Bytes = CS.getDereferenceableBytes(0)) {
00516       Type *Ty = V->getType()->getPointerElementType();
00517       if (Ty->isSized() && DL && DL->getTypeStoreSize(Ty) <= Bytes)
00518         return true;
00519     }
00520   }
00521 
00522   // For GEPs, determine if the indexing lands within the allocated object.
00523   if (const GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
00524     // Conservatively require that the base pointer be fully dereferenceable.
00525     if (!Visited.insert(GEP->getOperand(0)))
00526       return false;
00527     if (!isDereferenceablePointer(GEP->getOperand(0), DL, Visited))
00528       return false;
00529     // Check the indices.
00530     gep_type_iterator GTI = gep_type_begin(GEP);
00531     for (User::const_op_iterator I = GEP->op_begin()+1,
00532          E = GEP->op_end(); I != E; ++I) {
00533       Value *Index = *I;
00534       Type *Ty = *GTI++;
00535       // Struct indices can't be out of bounds.
00536       if (isa<StructType>(Ty))
00537         continue;
00538       ConstantInt *CI = dyn_cast<ConstantInt>(Index);
00539       if (!CI)
00540         return false;
00541       // Zero is always ok.
00542       if (CI->isZero())
00543         continue;
00544       // Check to see that it's within the bounds of an array.
00545       ArrayType *ATy = dyn_cast<ArrayType>(Ty);
00546       if (!ATy)
00547         return false;
00548       if (CI->getValue().getActiveBits() > 64)
00549         return false;
00550       if (CI->getZExtValue() >= ATy->getNumElements())
00551         return false;
00552     }
00553     // Indices check out; this is dereferenceable.
00554     return true;
00555   }
00556 
00557   if (const AddrSpaceCastInst *ASC = dyn_cast<AddrSpaceCastInst>(V))
00558     return isDereferenceablePointer(ASC->getOperand(0), DL, Visited);
00559 
00560   // If we don't know, assume the worst.
00561   return false;
00562 }
00563 
00564 bool Value::isDereferenceablePointer(const DataLayout *DL) const {
00565   // When dereferenceability information is provided by a dereferenceable
00566   // attribute, we know exactly how many bytes are dereferenceable. If we can
00567   // determine the exact offset to the attributed variable, we can use that
00568   // information here.
00569   Type *Ty = getType()->getPointerElementType();
00570   if (Ty->isSized() && DL) {
00571     APInt Offset(DL->getTypeStoreSizeInBits(getType()), 0);
00572     const Value *BV = stripAndAccumulateInBoundsConstantOffsets(*DL, Offset);
00573 
00574     APInt DerefBytes(Offset.getBitWidth(), 0);
00575     if (const Argument *A = dyn_cast<Argument>(BV))
00576       DerefBytes = A->getDereferenceableBytes();
00577     else if (ImmutableCallSite CS = BV)
00578       DerefBytes = CS.getDereferenceableBytes(0);
00579 
00580     if (DerefBytes.getBoolValue() && Offset.isNonNegative()) {
00581       if (DerefBytes.uge(Offset + DL->getTypeStoreSize(Ty)))
00582         return true;
00583     }
00584   }
00585 
00586   SmallPtrSet<const Value *, 32> Visited;
00587   return ::isDereferenceablePointer(this, DL, Visited);
00588 }
00589 
00590 Value *Value::DoPHITranslation(const BasicBlock *CurBB,
00591                                const BasicBlock *PredBB) {
00592   PHINode *PN = dyn_cast<PHINode>(this);
00593   if (PN && PN->getParent() == CurBB)
00594     return PN->getIncomingValueForBlock(PredBB);
00595   return this;
00596 }
00597 
00598 LLVMContext &Value::getContext() const { return VTy->getContext(); }
00599 
00600 void Value::reverseUseList() {
00601   if (!UseList || !UseList->Next)
00602     // No need to reverse 0 or 1 uses.
00603     return;
00604 
00605   Use *Head = UseList;
00606   Use *Current = UseList->Next;
00607   Head->Next = nullptr;
00608   while (Current) {
00609     Use *Next = Current->Next;
00610     Current->Next = Head;
00611     Head->setPrev(&Current->Next);
00612     Head = Current;
00613     Current = Next;
00614   }
00615   UseList = Head;
00616   Head->setPrev(&UseList);
00617 }
00618 
00619 //===----------------------------------------------------------------------===//
00620 //                             ValueHandleBase Class
00621 //===----------------------------------------------------------------------===//
00622 
00623 void ValueHandleBase::AddToExistingUseList(ValueHandleBase **List) {
00624   assert(List && "Handle list is null?");
00625 
00626   // Splice ourselves into the list.
00627   Next = *List;
00628   *List = this;
00629   setPrevPtr(List);
00630   if (Next) {
00631     Next->setPrevPtr(&Next);
00632     assert(VP.getPointer() == Next->VP.getPointer() && "Added to wrong list?");
00633   }
00634 }
00635 
00636 void ValueHandleBase::AddToExistingUseListAfter(ValueHandleBase *List) {
00637   assert(List && "Must insert after existing node");
00638 
00639   Next = List->Next;
00640   setPrevPtr(&List->Next);
00641   List->Next = this;
00642   if (Next)
00643     Next->setPrevPtr(&Next);
00644 }
00645 
00646 void ValueHandleBase::AddToUseList() {
00647   assert(VP.getPointer() && "Null pointer doesn't have a use list!");
00648 
00649   LLVMContextImpl *pImpl = VP.getPointer()->getContext().pImpl;
00650 
00651   if (VP.getPointer()->HasValueHandle) {
00652     // If this value already has a ValueHandle, then it must be in the
00653     // ValueHandles map already.
00654     ValueHandleBase *&Entry = pImpl->ValueHandles[VP.getPointer()];
00655     assert(Entry && "Value doesn't have any handles?");
00656     AddToExistingUseList(&Entry);
00657     return;
00658   }
00659 
00660   // Ok, it doesn't have any handles yet, so we must insert it into the
00661   // DenseMap.  However, doing this insertion could cause the DenseMap to
00662   // reallocate itself, which would invalidate all of the PrevP pointers that
00663   // point into the old table.  Handle this by checking for reallocation and
00664   // updating the stale pointers only if needed.
00665   DenseMap<Value*, ValueHandleBase*> &Handles = pImpl->ValueHandles;
00666   const void *OldBucketPtr = Handles.getPointerIntoBucketsArray();
00667 
00668   ValueHandleBase *&Entry = Handles[VP.getPointer()];
00669   assert(!Entry && "Value really did already have handles?");
00670   AddToExistingUseList(&Entry);
00671   VP.getPointer()->HasValueHandle = true;
00672 
00673   // If reallocation didn't happen or if this was the first insertion, don't
00674   // walk the table.
00675   if (Handles.isPointerIntoBucketsArray(OldBucketPtr) ||
00676       Handles.size() == 1) {
00677     return;
00678   }
00679 
00680   // Okay, reallocation did happen.  Fix the Prev Pointers.
00681   for (DenseMap<Value*, ValueHandleBase*>::iterator I = Handles.begin(),
00682        E = Handles.end(); I != E; ++I) {
00683     assert(I->second && I->first == I->second->VP.getPointer() &&
00684            "List invariant broken!");
00685     I->second->setPrevPtr(&I->second);
00686   }
00687 }
00688 
00689 void ValueHandleBase::RemoveFromUseList() {
00690   assert(VP.getPointer() && VP.getPointer()->HasValueHandle &&
00691          "Pointer doesn't have a use list!");
00692 
00693   // Unlink this from its use list.
00694   ValueHandleBase **PrevPtr = getPrevPtr();
00695   assert(*PrevPtr == this && "List invariant broken");
00696 
00697   *PrevPtr = Next;
00698   if (Next) {
00699     assert(Next->getPrevPtr() == &Next && "List invariant broken");
00700     Next->setPrevPtr(PrevPtr);
00701     return;
00702   }
00703 
00704   // If the Next pointer was null, then it is possible that this was the last
00705   // ValueHandle watching VP.  If so, delete its entry from the ValueHandles
00706   // map.
00707   LLVMContextImpl *pImpl = VP.getPointer()->getContext().pImpl;
00708   DenseMap<Value*, ValueHandleBase*> &Handles = pImpl->ValueHandles;
00709   if (Handles.isPointerIntoBucketsArray(PrevPtr)) {
00710     Handles.erase(VP.getPointer());
00711     VP.getPointer()->HasValueHandle = false;
00712   }
00713 }
00714 
00715 
00716 void ValueHandleBase::ValueIsDeleted(Value *V) {
00717   assert(V->HasValueHandle && "Should only be called if ValueHandles present");
00718 
00719   // Get the linked list base, which is guaranteed to exist since the
00720   // HasValueHandle flag is set.
00721   LLVMContextImpl *pImpl = V->getContext().pImpl;
00722   ValueHandleBase *Entry = pImpl->ValueHandles[V];
00723   assert(Entry && "Value bit set but no entries exist");
00724 
00725   // We use a local ValueHandleBase as an iterator so that ValueHandles can add
00726   // and remove themselves from the list without breaking our iteration.  This
00727   // is not really an AssertingVH; we just have to give ValueHandleBase a kind.
00728   // Note that we deliberately do not the support the case when dropping a value
00729   // handle results in a new value handle being permanently added to the list
00730   // (as might occur in theory for CallbackVH's): the new value handle will not
00731   // be processed and the checking code will mete out righteous punishment if
00732   // the handle is still present once we have finished processing all the other
00733   // value handles (it is fine to momentarily add then remove a value handle).
00734   for (ValueHandleBase Iterator(Assert, *Entry); Entry; Entry = Iterator.Next) {
00735     Iterator.RemoveFromUseList();
00736     Iterator.AddToExistingUseListAfter(Entry);
00737     assert(Entry->Next == &Iterator && "Loop invariant broken.");
00738 
00739     switch (Entry->getKind()) {
00740     case Assert:
00741       break;
00742     case Tracking:
00743       // Mark that this value has been deleted by setting it to an invalid Value
00744       // pointer.
00745       Entry->operator=(DenseMapInfo<Value *>::getTombstoneKey());
00746       break;
00747     case Weak:
00748       // Weak just goes to null, which will unlink it from the list.
00749       Entry->operator=(nullptr);
00750       break;
00751     case Callback:
00752       // Forward to the subclass's implementation.
00753       static_cast<CallbackVH*>(Entry)->deleted();
00754       break;
00755     }
00756   }
00757 
00758   // All callbacks, weak references, and assertingVHs should be dropped by now.
00759   if (V->HasValueHandle) {
00760 #ifndef NDEBUG      // Only in +Asserts mode...
00761     dbgs() << "While deleting: " << *V->getType() << " %" << V->getName()
00762            << "\n";
00763     if (pImpl->ValueHandles[V]->getKind() == Assert)
00764       llvm_unreachable("An asserting value handle still pointed to this"
00765                        " value!");
00766 
00767 #endif
00768     llvm_unreachable("All references to V were not removed?");
00769   }
00770 }
00771 
00772 
00773 void ValueHandleBase::ValueIsRAUWd(Value *Old, Value *New) {
00774   assert(Old->HasValueHandle &&"Should only be called if ValueHandles present");
00775   assert(Old != New && "Changing value into itself!");
00776 
00777   // Get the linked list base, which is guaranteed to exist since the
00778   // HasValueHandle flag is set.
00779   LLVMContextImpl *pImpl = Old->getContext().pImpl;
00780   ValueHandleBase *Entry = pImpl->ValueHandles[Old];
00781 
00782   assert(Entry && "Value bit set but no entries exist");
00783 
00784   // We use a local ValueHandleBase as an iterator so that
00785   // ValueHandles can add and remove themselves from the list without
00786   // breaking our iteration.  This is not really an AssertingVH; we
00787   // just have to give ValueHandleBase some kind.
00788   for (ValueHandleBase Iterator(Assert, *Entry); Entry; Entry = Iterator.Next) {
00789     Iterator.RemoveFromUseList();
00790     Iterator.AddToExistingUseListAfter(Entry);
00791     assert(Entry->Next == &Iterator && "Loop invariant broken.");
00792 
00793     switch (Entry->getKind()) {
00794     case Assert:
00795       // Asserting handle does not follow RAUW implicitly.
00796       break;
00797     case Tracking:
00798       // Tracking goes to new value like a WeakVH. Note that this may make it
00799       // something incompatible with its templated type. We don't want to have a
00800       // virtual (or inline) interface to handle this though, so instead we make
00801       // the TrackingVH accessors guarantee that a client never sees this value.
00802 
00803       // FALLTHROUGH
00804     case Weak:
00805       // Weak goes to the new value, which will unlink it from Old's list.
00806       Entry->operator=(New);
00807       break;
00808     case Callback:
00809       // Forward to the subclass's implementation.
00810       static_cast<CallbackVH*>(Entry)->allUsesReplacedWith(New);
00811       break;
00812     }
00813   }
00814 
00815 #ifndef NDEBUG
00816   // If any new tracking or weak value handles were added while processing the
00817   // list, then complain about it now.
00818   if (Old->HasValueHandle)
00819     for (Entry = pImpl->ValueHandles[Old]; Entry; Entry = Entry->Next)
00820       switch (Entry->getKind()) {
00821       case Tracking:
00822       case Weak:
00823         dbgs() << "After RAUW from " << *Old->getType() << " %"
00824                << Old->getName() << " to " << *New->getType() << " %"
00825                << New->getName() << "\n";
00826         llvm_unreachable("A tracking or weak value handle still pointed to the"
00827                          " old value!\n");
00828       default:
00829         break;
00830       }
00831 #endif
00832 }
00833 
00834 // Pin the vtable to this file.
00835 void CallbackVH::anchor() {}