1 //===-- Value.cpp - Implement the Value class -----------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the Value, ValueHandle, and User classes.
12 //===----------------------------------------------------------------------===//
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/GetElementPtrTypeIterator.h"
24 #include "llvm/IR/InstrTypes.h"
25 #include "llvm/IR/Instructions.h"
26 #include "llvm/IR/IntrinsicInst.h"
27 #include "llvm/IR/Module.h"
28 #include "llvm/IR/Operator.h"
29 #include "llvm/IR/Statepoint.h"
30 #include "llvm/IR/ValueHandle.h"
31 #include "llvm/IR/ValueSymbolTable.h"
32 #include "llvm/Support/Debug.h"
33 #include "llvm/Support/ErrorHandling.h"
34 #include "llvm/Support/ManagedStatic.h"
35 #include "llvm/Support/raw_ostream.h"
39 //===----------------------------------------------------------------------===//
41 //===----------------------------------------------------------------------===//
42 static inline Type *checkType(Type *Ty) {
43 assert(Ty && "Value defined with a null type: Error!");
47 Value::Value(Type *ty, unsigned scid)
48 : VTy(checkType(ty)), UseList(nullptr), SubclassID(scid),
49 HasValueHandle(0), SubclassOptionalData(0), SubclassData(0),
50 NumUserOperands(0), IsUsedByMD(false), HasName(false) {
51 // FIXME: Why isn't this in the subclass gunk??
52 // Note, we cannot call isa<CallInst> before the CallInst has been
54 if (SubclassID == Instruction::Call || SubclassID == Instruction::Invoke)
55 assert((VTy->isFirstClassType() || VTy->isVoidTy() || VTy->isStructTy()) &&
56 "invalid CallInst type!");
57 else if (SubclassID != BasicBlockVal &&
58 (SubclassID < ConstantFirstVal || SubclassID > ConstantLastVal))
59 assert((VTy->isFirstClassType() || VTy->isVoidTy()) &&
60 "Cannot create non-first-class values except for constants!");
64 // Notify all ValueHandles (if present) that this value is going away.
66 ValueHandleBase::ValueIsDeleted(this);
67 if (isUsedByMetadata())
68 ValueAsMetadata::handleDeletion(this);
70 #ifndef NDEBUG // Only in -g mode...
71 // Check to make sure that there are no uses of this value that are still
72 // around when the value is destroyed. If there are, then we have a dangling
73 // reference and something is wrong. This code is here to print out where
74 // the value is still being referenced.
77 dbgs() << "While deleting: " << *VTy << " %" << getName() << "\n";
78 for (auto *U : users())
79 dbgs() << "Use still stuck around after Def is destroyed:" << *U << "\n";
82 assert(use_empty() && "Uses remain when a value is destroyed!");
84 // If this value is named, destroy the name. This should not be in a symtab
89 void Value::destroyValueName() {
90 ValueName *Name = getValueName();
93 setValueName(nullptr);
96 bool Value::hasNUses(unsigned N) const {
97 const_use_iterator UI = use_begin(), E = use_end();
100 if (UI == E) return false; // Too few.
104 bool Value::hasNUsesOrMore(unsigned N) const {
105 const_use_iterator UI = use_begin(), E = use_end();
108 if (UI == E) return false; // Too few.
113 bool Value::isUsedInBasicBlock(const BasicBlock *BB) const {
114 // This can be computed either by scanning the instructions in BB, or by
115 // scanning the use list of this Value. Both lists can be very long, but
116 // usually one is quite short.
118 // Scan both lists simultaneously until one is exhausted. This limits the
119 // search to the shorter list.
120 BasicBlock::const_iterator BI = BB->begin(), BE = BB->end();
121 const_user_iterator UI = user_begin(), UE = user_end();
122 for (; BI != BE && UI != UE; ++BI, ++UI) {
123 // Scan basic block: Check if this Value is used by the instruction at BI.
124 if (std::find(BI->op_begin(), BI->op_end(), this) != BI->op_end())
126 // Scan use list: Check if the use at UI is in BB.
127 const Instruction *User = dyn_cast<Instruction>(*UI);
128 if (User && User->getParent() == BB)
134 unsigned Value::getNumUses() const {
135 return (unsigned)std::distance(use_begin(), use_end());
138 static bool getSymTab(Value *V, ValueSymbolTable *&ST) {
140 if (Instruction *I = dyn_cast<Instruction>(V)) {
141 if (BasicBlock *P = I->getParent())
142 if (Function *PP = P->getParent())
143 ST = &PP->getValueSymbolTable();
144 } else if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
145 if (Function *P = BB->getParent())
146 ST = &P->getValueSymbolTable();
147 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
148 if (Module *P = GV->getParent())
149 ST = &P->getValueSymbolTable();
150 } else if (Argument *A = dyn_cast<Argument>(V)) {
151 if (Function *P = A->getParent())
152 ST = &P->getValueSymbolTable();
154 assert(isa<Constant>(V) && "Unknown value type!");
155 return true; // no name is setable for this.
160 ValueName *Value::getValueName() const {
161 if (!HasName) return nullptr;
163 LLVMContext &Ctx = getContext();
164 auto I = Ctx.pImpl->ValueNames.find(this);
165 assert(I != Ctx.pImpl->ValueNames.end() &&
166 "No name entry found!");
171 void Value::setValueName(ValueName *VN) {
172 LLVMContext &Ctx = getContext();
174 assert(HasName == Ctx.pImpl->ValueNames.count(this) &&
175 "HasName bit out of sync!");
179 Ctx.pImpl->ValueNames.erase(this);
185 Ctx.pImpl->ValueNames[this] = VN;
188 StringRef Value::getName() const {
189 // Make sure the empty string is still a C string. For historical reasons,
190 // some clients want to call .data() on the result and expect it to be null
193 return StringRef("", 0);
194 return getValueName()->getKey();
197 void Value::setNameImpl(const Twine &NewName) {
198 // Fast path for common IRBuilder case of setName("") when there is no name.
199 if (NewName.isTriviallyEmpty() && !hasName())
202 SmallString<256> NameData;
203 StringRef NameRef = NewName.toStringRef(NameData);
204 assert(NameRef.find_first_of(0) == StringRef::npos &&
205 "Null bytes are not allowed in names");
207 // Name isn't changing?
208 if (getName() == NameRef)
211 assert(!getType()->isVoidTy() && "Cannot assign a name to void values!");
213 // Get the symbol table to update for this object.
214 ValueSymbolTable *ST;
215 if (getSymTab(this, ST))
216 return; // Cannot set a name on this value (e.g. constant).
218 if (!ST) { // No symbol table to update? Just do the change.
219 if (NameRef.empty()) {
220 // Free the name for this value.
225 // NOTE: Could optimize for the case the name is shrinking to not deallocate
229 // Create the new name.
230 setValueName(ValueName::Create(NameRef));
231 getValueName()->setValue(this);
235 // NOTE: Could optimize for the case the name is shrinking to not deallocate
239 ST->removeValueName(getValueName());
246 // Name is changing to something new.
247 setValueName(ST->createValueName(NameRef, this));
250 void Value::setName(const Twine &NewName) {
251 setNameImpl(NewName);
252 if (Function *F = dyn_cast<Function>(this))
253 F->recalculateIntrinsicID();
256 void Value::takeName(Value *V) {
257 ValueSymbolTable *ST = nullptr;
258 // If this value has a name, drop it.
260 // Get the symtab this is in.
261 if (getSymTab(this, ST)) {
262 // We can't set a name on this value, but we need to clear V's name if
264 if (V->hasName()) V->setName("");
265 return; // Cannot set a name on this value (e.g. constant).
270 ST->removeValueName(getValueName());
274 // Now we know that this has no name.
276 // If V has no name either, we're done.
277 if (!V->hasName()) return;
279 // Get this's symtab if we didn't before.
281 if (getSymTab(this, ST)) {
284 return; // Cannot set a name on this value (e.g. constant).
288 // Get V's ST, this should always succed, because V has a name.
289 ValueSymbolTable *VST;
290 bool Failure = getSymTab(V, VST);
291 assert(!Failure && "V has a name, so it should have a ST!"); (void)Failure;
293 // If these values are both in the same symtab, we can do this very fast.
294 // This works even if both values have no symtab yet.
297 setValueName(V->getValueName());
298 V->setValueName(nullptr);
299 getValueName()->setValue(this);
303 // Otherwise, things are slightly more complex. Remove V's name from VST and
304 // then reinsert it into ST.
307 VST->removeValueName(V->getValueName());
308 setValueName(V->getValueName());
309 V->setValueName(nullptr);
310 getValueName()->setValue(this);
313 ST->reinsertValue(this);
317 void Value::assertModuleIsMaterialized() const {
318 const GlobalValue *GV = dyn_cast<GlobalValue>(this);
321 const Module *M = GV->getParent();
324 assert(M->isMaterialized());
327 static bool contains(SmallPtrSetImpl<ConstantExpr *> &Cache, ConstantExpr *Expr,
329 if (!Cache.insert(Expr).second)
332 for (auto &O : Expr->operands()) {
335 auto *CE = dyn_cast<ConstantExpr>(O);
338 if (contains(Cache, CE, C))
344 static bool contains(Value *Expr, Value *V) {
348 auto *C = dyn_cast<Constant>(V);
352 auto *CE = dyn_cast<ConstantExpr>(Expr);
356 SmallPtrSet<ConstantExpr *, 4> Cache;
357 return contains(Cache, CE, C);
361 void Value::replaceAllUsesWith(Value *New) {
362 assert(New && "Value::replaceAllUsesWith(<null>) is invalid!");
363 assert(!contains(New, this) &&
364 "this->replaceAllUsesWith(expr(this)) is NOT valid!");
365 assert(New->getType() == getType() &&
366 "replaceAllUses of value with new value of different type!");
368 // Notify all ValueHandles (if present) that this value is going away.
370 ValueHandleBase::ValueIsRAUWd(this, New);
371 if (isUsedByMetadata())
372 ValueAsMetadata::handleRAUW(this, New);
374 while (!use_empty()) {
376 // Must handle Constants specially, we cannot call replaceUsesOfWith on a
377 // constant because they are uniqued.
378 if (auto *C = dyn_cast<Constant>(U.getUser())) {
379 if (!isa<GlobalValue>(C)) {
380 C->handleOperandChange(this, New, &U);
388 if (BasicBlock *BB = dyn_cast<BasicBlock>(this))
389 BB->replaceSuccessorsPhiUsesWith(cast<BasicBlock>(New));
392 // Like replaceAllUsesWith except it does not handle constants or basic blocks.
393 // This routine leaves uses within BB.
394 void Value::replaceUsesOutsideBlock(Value *New, BasicBlock *BB) {
395 assert(New && "Value::replaceUsesOutsideBlock(<null>, BB) is invalid!");
396 assert(!contains(New, this) &&
397 "this->replaceUsesOutsideBlock(expr(this), BB) is NOT valid!");
398 assert(New->getType() == getType() &&
399 "replaceUses of value with new value of different type!");
400 assert(BB && "Basic block that may contain a use of 'New' must be defined\n");
402 use_iterator UI = use_begin(), E = use_end();
406 auto *Usr = dyn_cast<Instruction>(U.getUser());
407 if (Usr && Usr->getParent() == BB)
415 // Various metrics for how much to strip off of pointers.
416 enum PointerStripKind {
418 PSK_ZeroIndicesAndAliases,
419 PSK_InBoundsConstantIndices,
423 template <PointerStripKind StripKind>
424 static Value *stripPointerCastsAndOffsets(Value *V) {
425 if (!V->getType()->isPointerTy())
428 // Even though we don't look through PHI nodes, we could be called on an
429 // instruction in an unreachable block, which may be on a cycle.
430 SmallPtrSet<Value *, 4> Visited;
434 if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
436 case PSK_ZeroIndicesAndAliases:
437 case PSK_ZeroIndices:
438 if (!GEP->hasAllZeroIndices())
441 case PSK_InBoundsConstantIndices:
442 if (!GEP->hasAllConstantIndices())
446 if (!GEP->isInBounds())
450 V = GEP->getPointerOperand();
451 } else if (Operator::getOpcode(V) == Instruction::BitCast ||
452 Operator::getOpcode(V) == Instruction::AddrSpaceCast) {
453 V = cast<Operator>(V)->getOperand(0);
454 } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
455 if (StripKind == PSK_ZeroIndices || GA->mayBeOverridden())
457 V = GA->getAliasee();
461 assert(V->getType()->isPointerTy() && "Unexpected operand type!");
462 } while (Visited.insert(V).second);
468 Value *Value::stripPointerCasts() {
469 return stripPointerCastsAndOffsets<PSK_ZeroIndicesAndAliases>(this);
472 Value *Value::stripPointerCastsNoFollowAliases() {
473 return stripPointerCastsAndOffsets<PSK_ZeroIndices>(this);
476 Value *Value::stripInBoundsConstantOffsets() {
477 return stripPointerCastsAndOffsets<PSK_InBoundsConstantIndices>(this);
480 Value *Value::stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL,
482 if (!getType()->isPointerTy())
485 assert(Offset.getBitWidth() == DL.getPointerSizeInBits(cast<PointerType>(
486 getType())->getAddressSpace()) &&
487 "The offset must have exactly as many bits as our pointer.");
489 // Even though we don't look through PHI nodes, we could be called on an
490 // instruction in an unreachable block, which may be on a cycle.
491 SmallPtrSet<Value *, 4> Visited;
492 Visited.insert(this);
495 if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
496 if (!GEP->isInBounds())
498 APInt GEPOffset(Offset);
499 if (!GEP->accumulateConstantOffset(DL, GEPOffset))
502 V = GEP->getPointerOperand();
503 } else if (Operator::getOpcode(V) == Instruction::BitCast) {
504 V = cast<Operator>(V)->getOperand(0);
505 } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
506 V = GA->getAliasee();
510 assert(V->getType()->isPointerTy() && "Unexpected operand type!");
511 } while (Visited.insert(V).second);
516 Value *Value::stripInBoundsOffsets() {
517 return stripPointerCastsAndOffsets<PSK_InBounds>(this);
520 Value *Value::DoPHITranslation(const BasicBlock *CurBB,
521 const BasicBlock *PredBB) {
522 PHINode *PN = dyn_cast<PHINode>(this);
523 if (PN && PN->getParent() == CurBB)
524 return PN->getIncomingValueForBlock(PredBB);
528 LLVMContext &Value::getContext() const { return VTy->getContext(); }
530 void Value::reverseUseList() {
531 if (!UseList || !UseList->Next)
532 // No need to reverse 0 or 1 uses.
536 Use *Current = UseList->Next;
537 Head->Next = nullptr;
539 Use *Next = Current->Next;
540 Current->Next = Head;
541 Head->setPrev(&Current->Next);
546 Head->setPrev(&UseList);
549 //===----------------------------------------------------------------------===//
550 // ValueHandleBase Class
551 //===----------------------------------------------------------------------===//
553 void ValueHandleBase::AddToExistingUseList(ValueHandleBase **List) {
554 assert(List && "Handle list is null?");
556 // Splice ourselves into the list.
561 Next->setPrevPtr(&Next);
562 assert(V == Next->V && "Added to wrong list?");
566 void ValueHandleBase::AddToExistingUseListAfter(ValueHandleBase *List) {
567 assert(List && "Must insert after existing node");
570 setPrevPtr(&List->Next);
573 Next->setPrevPtr(&Next);
576 void ValueHandleBase::AddToUseList() {
577 assert(V && "Null pointer doesn't have a use list!");
579 LLVMContextImpl *pImpl = V->getContext().pImpl;
581 if (V->HasValueHandle) {
582 // If this value already has a ValueHandle, then it must be in the
583 // ValueHandles map already.
584 ValueHandleBase *&Entry = pImpl->ValueHandles[V];
585 assert(Entry && "Value doesn't have any handles?");
586 AddToExistingUseList(&Entry);
590 // Ok, it doesn't have any handles yet, so we must insert it into the
591 // DenseMap. However, doing this insertion could cause the DenseMap to
592 // reallocate itself, which would invalidate all of the PrevP pointers that
593 // point into the old table. Handle this by checking for reallocation and
594 // updating the stale pointers only if needed.
595 DenseMap<Value*, ValueHandleBase*> &Handles = pImpl->ValueHandles;
596 const void *OldBucketPtr = Handles.getPointerIntoBucketsArray();
598 ValueHandleBase *&Entry = Handles[V];
599 assert(!Entry && "Value really did already have handles?");
600 AddToExistingUseList(&Entry);
601 V->HasValueHandle = true;
603 // If reallocation didn't happen or if this was the first insertion, don't
605 if (Handles.isPointerIntoBucketsArray(OldBucketPtr) ||
606 Handles.size() == 1) {
610 // Okay, reallocation did happen. Fix the Prev Pointers.
611 for (DenseMap<Value*, ValueHandleBase*>::iterator I = Handles.begin(),
612 E = Handles.end(); I != E; ++I) {
613 assert(I->second && I->first == I->second->V &&
614 "List invariant broken!");
615 I->second->setPrevPtr(&I->second);
619 void ValueHandleBase::RemoveFromUseList() {
620 assert(V && V->HasValueHandle &&
621 "Pointer doesn't have a use list!");
623 // Unlink this from its use list.
624 ValueHandleBase **PrevPtr = getPrevPtr();
625 assert(*PrevPtr == this && "List invariant broken");
629 assert(Next->getPrevPtr() == &Next && "List invariant broken");
630 Next->setPrevPtr(PrevPtr);
634 // If the Next pointer was null, then it is possible that this was the last
635 // ValueHandle watching VP. If so, delete its entry from the ValueHandles
637 LLVMContextImpl *pImpl = V->getContext().pImpl;
638 DenseMap<Value*, ValueHandleBase*> &Handles = pImpl->ValueHandles;
639 if (Handles.isPointerIntoBucketsArray(PrevPtr)) {
641 V->HasValueHandle = false;
646 void ValueHandleBase::ValueIsDeleted(Value *V) {
647 assert(V->HasValueHandle && "Should only be called if ValueHandles present");
649 // Get the linked list base, which is guaranteed to exist since the
650 // HasValueHandle flag is set.
651 LLVMContextImpl *pImpl = V->getContext().pImpl;
652 ValueHandleBase *Entry = pImpl->ValueHandles[V];
653 assert(Entry && "Value bit set but no entries exist");
655 // We use a local ValueHandleBase as an iterator so that ValueHandles can add
656 // and remove themselves from the list without breaking our iteration. This
657 // is not really an AssertingVH; we just have to give ValueHandleBase a kind.
658 // Note that we deliberately do not the support the case when dropping a value
659 // handle results in a new value handle being permanently added to the list
660 // (as might occur in theory for CallbackVH's): the new value handle will not
661 // be processed and the checking code will mete out righteous punishment if
662 // the handle is still present once we have finished processing all the other
663 // value handles (it is fine to momentarily add then remove a value handle).
664 for (ValueHandleBase Iterator(Assert, *Entry); Entry; Entry = Iterator.Next) {
665 Iterator.RemoveFromUseList();
666 Iterator.AddToExistingUseListAfter(Entry);
667 assert(Entry->Next == &Iterator && "Loop invariant broken.");
669 switch (Entry->getKind()) {
673 // Mark that this value has been deleted by setting it to an invalid Value
675 Entry->operator=(DenseMapInfo<Value *>::getTombstoneKey());
678 // Weak just goes to null, which will unlink it from the list.
679 Entry->operator=(nullptr);
682 // Forward to the subclass's implementation.
683 static_cast<CallbackVH*>(Entry)->deleted();
688 // All callbacks, weak references, and assertingVHs should be dropped by now.
689 if (V->HasValueHandle) {
690 #ifndef NDEBUG // Only in +Asserts mode...
691 dbgs() << "While deleting: " << *V->getType() << " %" << V->getName()
693 if (pImpl->ValueHandles[V]->getKind() == Assert)
694 llvm_unreachable("An asserting value handle still pointed to this"
698 llvm_unreachable("All references to V were not removed?");
703 void ValueHandleBase::ValueIsRAUWd(Value *Old, Value *New) {
704 assert(Old->HasValueHandle &&"Should only be called if ValueHandles present");
705 assert(Old != New && "Changing value into itself!");
706 assert(Old->getType() == New->getType() &&
707 "replaceAllUses of value with new value of different type!");
709 // Get the linked list base, which is guaranteed to exist since the
710 // HasValueHandle flag is set.
711 LLVMContextImpl *pImpl = Old->getContext().pImpl;
712 ValueHandleBase *Entry = pImpl->ValueHandles[Old];
714 assert(Entry && "Value bit set but no entries exist");
716 // We use a local ValueHandleBase as an iterator so that
717 // ValueHandles can add and remove themselves from the list without
718 // breaking our iteration. This is not really an AssertingVH; we
719 // just have to give ValueHandleBase some kind.
720 for (ValueHandleBase Iterator(Assert, *Entry); Entry; Entry = Iterator.Next) {
721 Iterator.RemoveFromUseList();
722 Iterator.AddToExistingUseListAfter(Entry);
723 assert(Entry->Next == &Iterator && "Loop invariant broken.");
725 switch (Entry->getKind()) {
727 // Asserting handle does not follow RAUW implicitly.
730 // Tracking goes to new value like a WeakVH. Note that this may make it
731 // something incompatible with its templated type. We don't want to have a
732 // virtual (or inline) interface to handle this though, so instead we make
733 // the TrackingVH accessors guarantee that a client never sees this value.
737 // Weak goes to the new value, which will unlink it from Old's list.
738 Entry->operator=(New);
741 // Forward to the subclass's implementation.
742 static_cast<CallbackVH*>(Entry)->allUsesReplacedWith(New);
748 // If any new tracking or weak value handles were added while processing the
749 // list, then complain about it now.
750 if (Old->HasValueHandle)
751 for (Entry = pImpl->ValueHandles[Old]; Entry; Entry = Entry->Next)
752 switch (Entry->getKind()) {
755 dbgs() << "After RAUW from " << *Old->getType() << " %"
756 << Old->getName() << " to " << *New->getType() << " %"
757 << New->getName() << "\n";
758 llvm_unreachable("A tracking or weak value handle still pointed to the"
766 // Pin the vtable to this file.
767 void CallbackVH::anchor() {}