3c32c566e1f09dd8087408317cc32ef778198bb1
[oota-llvm.git] / lib / VMCore / Value.cpp
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 "LLVMContextImpl.h"
15 #include "llvm/Constant.h"
16 #include "llvm/Constants.h"
17 #include "llvm/DerivedTypes.h"
18 #include "llvm/InstrTypes.h"
19 #include "llvm/Instructions.h"
20 #include "llvm/Operator.h"
21 #include "llvm/Module.h"
22 #include "llvm/ValueSymbolTable.h"
23 #include "llvm/ADT/SmallString.h"
24 #include "llvm/Support/Debug.h"
25 #include "llvm/Support/GetElementPtrTypeIterator.h"
26 #include "llvm/Support/ErrorHandling.h"
27 #include "llvm/Support/LeakDetector.h"
28 #include "llvm/Support/ManagedStatic.h"
29 #include "llvm/Support/ValueHandle.h"
30 #include "llvm/ADT/DenseMap.h"
31 #include <algorithm>
32 using namespace llvm;
33
34 //===----------------------------------------------------------------------===//
35 //                                Value Class
36 //===----------------------------------------------------------------------===//
37
38 static inline const Type *checkType(const Type *Ty) {
39   assert(Ty && "Value defined with a null type: Error!");
40   return Ty;
41 }
42
43 Value::Value(const Type *ty, unsigned scid)
44   : SubclassID(scid), HasValueHandle(0),
45     SubclassOptionalData(0), SubclassData(0), VTy(checkType(ty)),
46     UseList(0), Name(0) {
47   if (isa<CallInst>(this) || isa<InvokeInst>(this))
48     assert((VTy->isFirstClassType() || VTy->isVoidTy() ||
49             ty->isOpaqueTy() || VTy->isStructTy()) &&
50            "invalid CallInst  type!");
51   else if (!isa<Constant>(this) && !isa<BasicBlock>(this))
52     assert((VTy->isFirstClassType() || VTy->isVoidTy() ||
53             ty->isOpaqueTy()) &&
54            "Cannot create non-first-class values except for constants!");
55 }
56
57 Value::~Value() {
58   // Notify all ValueHandles (if present) that this value is going away.
59   if (HasValueHandle)
60     ValueHandleBase::ValueIsDeleted(this);
61
62 #ifndef NDEBUG      // Only in -g mode...
63   // Check to make sure that there are no uses of this value that are still
64   // around when the value is destroyed.  If there are, then we have a dangling
65   // reference and something is wrong.  This code is here to print out what is
66   // still being referenced.  The value in question should be printed as
67   // a <badref>
68   //
69   if (!use_empty()) {
70     dbgs() << "While deleting: " << *VTy << " %" << getNameStr() << "\n";
71     for (use_iterator I = use_begin(), E = use_end(); I != E; ++I)
72       dbgs() << "Use still stuck around after Def is destroyed:"
73            << **I << "\n";
74   }
75 #endif
76   assert(use_empty() && "Uses remain when a value is destroyed!");
77
78   // If this value is named, destroy the name.  This should not be in a symtab
79   // at this point.
80   if (Name)
81     Name->Destroy();
82
83   // There should be no uses of this object anymore, remove it.
84   LeakDetector::removeGarbageObject(this);
85 }
86
87 /// hasNUses - Return true if this Value has exactly N users.
88 ///
89 bool Value::hasNUses(unsigned N) const {
90   const_use_iterator UI = use_begin(), E = use_end();
91
92   for (; N; --N, ++UI)
93     if (UI == E) return false;  // Too few.
94   return UI == E;
95 }
96
97 /// hasNUsesOrMore - Return true if this value has N users or more.  This is
98 /// logically equivalent to getNumUses() >= N.
99 ///
100 bool Value::hasNUsesOrMore(unsigned N) const {
101   const_use_iterator UI = use_begin(), E = use_end();
102
103   for (; N; --N, ++UI)
104     if (UI == E) return false;  // Too few.
105
106   return true;
107 }
108
109 /// isUsedInBasicBlock - Return true if this value is used in the specified
110 /// basic block.
111 bool Value::isUsedInBasicBlock(const BasicBlock *BB) const {
112   for (const_use_iterator I = use_begin(), E = use_end(); I != E; ++I) {
113     const Instruction *User = dyn_cast<Instruction>(*I);
114     if (User && User->getParent() == BB)
115       return true;
116   }
117   return false;
118 }
119
120
121 /// getNumUses - This method computes the number of uses of this Value.  This
122 /// is a linear time operation.  Use hasOneUse or hasNUses to check for specific
123 /// values.
124 unsigned Value::getNumUses() const {
125   return (unsigned)std::distance(use_begin(), use_end());
126 }
127
128 static bool getSymTab(Value *V, ValueSymbolTable *&ST) {
129   ST = 0;
130   if (Instruction *I = dyn_cast<Instruction>(V)) {
131     if (BasicBlock *P = I->getParent())
132       if (Function *PP = P->getParent())
133         ST = &PP->getValueSymbolTable();
134   } else if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
135     if (Function *P = BB->getParent())
136       ST = &P->getValueSymbolTable();
137   } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
138     if (Module *P = GV->getParent())
139       ST = &P->getValueSymbolTable();
140   } else if (Argument *A = dyn_cast<Argument>(V)) {
141     if (Function *P = A->getParent())
142       ST = &P->getValueSymbolTable();
143   } else if (isa<MDString>(V))
144     return true;
145   else {
146     assert(isa<Constant>(V) && "Unknown value type!");
147     return true;  // no name is setable for this.
148   }
149   return false;
150 }
151
152 StringRef Value::getName() const {
153   // Make sure the empty string is still a C string. For historical reasons,
154   // some clients want to call .data() on the result and expect it to be null
155   // terminated.
156   if (!Name) return StringRef("", 0);
157   return Name->getKey();
158 }
159
160 std::string Value::getNameStr() const {
161   return getName().str();
162 }
163
164 void Value::setName(const Twine &NewName) {
165   // Fast path for common IRBuilder case of setName("") when there is no name.
166   if (NewName.isTriviallyEmpty() && !hasName())
167     return;
168
169   SmallString<256> NameData;
170   StringRef NameRef = NewName.toStringRef(NameData);
171
172   // Name isn't changing?
173   if (getName() == NameRef)
174     return;
175
176   assert(!getType()->isVoidTy() && "Cannot assign a name to void values!");
177
178   // Get the symbol table to update for this object.
179   ValueSymbolTable *ST;
180   if (getSymTab(this, ST))
181     return;  // Cannot set a name on this value (e.g. constant).
182
183   if (!ST) { // No symbol table to update?  Just do the change.
184     if (NameRef.empty()) {
185       // Free the name for this value.
186       Name->Destroy();
187       Name = 0;
188       return;
189     }
190
191     if (Name)
192       Name->Destroy();
193
194     // NOTE: Could optimize for the case the name is shrinking to not deallocate
195     // then reallocated.
196
197     // Create the new name.
198     Name = ValueName::Create(NameRef.begin(), NameRef.end());
199     Name->setValue(this);
200     return;
201   }
202
203   // NOTE: Could optimize for the case the name is shrinking to not deallocate
204   // then reallocated.
205   if (hasName()) {
206     // Remove old name.
207     ST->removeValueName(Name);
208     Name->Destroy();
209     Name = 0;
210
211     if (NameRef.empty())
212       return;
213   }
214
215   // Name is changing to something new.
216   Name = ST->createValueName(NameRef, this);
217 }
218
219
220 /// takeName - transfer the name from V to this value, setting V's name to
221 /// empty.  It is an error to call V->takeName(V).
222 void Value::takeName(Value *V) {
223   ValueSymbolTable *ST = 0;
224   // If this value has a name, drop it.
225   if (hasName()) {
226     // Get the symtab this is in.
227     if (getSymTab(this, ST)) {
228       // We can't set a name on this value, but we need to clear V's name if
229       // it has one.
230       if (V->hasName()) V->setName("");
231       return;  // Cannot set a name on this value (e.g. constant).
232     }
233
234     // Remove old name.
235     if (ST)
236       ST->removeValueName(Name);
237     Name->Destroy();
238     Name = 0;
239   }
240
241   // Now we know that this has no name.
242
243   // If V has no name either, we're done.
244   if (!V->hasName()) return;
245
246   // Get this's symtab if we didn't before.
247   if (!ST) {
248     if (getSymTab(this, ST)) {
249       // Clear V's name.
250       V->setName("");
251       return;  // Cannot set a name on this value (e.g. constant).
252     }
253   }
254
255   // Get V's ST, this should always succed, because V has a name.
256   ValueSymbolTable *VST;
257   bool Failure = getSymTab(V, VST);
258   assert(!Failure && "V has a name, so it should have a ST!"); Failure=Failure;
259
260   // If these values are both in the same symtab, we can do this very fast.
261   // This works even if both values have no symtab yet.
262   if (ST == VST) {
263     // Take the name!
264     Name = V->Name;
265     V->Name = 0;
266     Name->setValue(this);
267     return;
268   }
269
270   // Otherwise, things are slightly more complex.  Remove V's name from VST and
271   // then reinsert it into ST.
272
273   if (VST)
274     VST->removeValueName(V->Name);
275   Name = V->Name;
276   V->Name = 0;
277   Name->setValue(this);
278
279   if (ST)
280     ST->reinsertValue(this);
281 }
282
283
284 /// addUse - This method should only be used by the Use class.
285 ///
286 void Value::addUse(Use &U) {
287   U.addToList(&UseList);
288   
289   // Notify all ValueHandles (if present) that this value added a Use.
290   if (HasValueHandle)
291     ValueHandleBase::ValueAddedUse(U);
292 }
293
294 // uncheckedReplaceAllUsesWith - This is exactly the same as replaceAllUsesWith,
295 // except that it doesn't have all of the asserts.  The asserts fail because we
296 // are half-way done resolving types, which causes some types to exist as two
297 // different Type*'s at the same time.  This is a sledgehammer to work around
298 // this problem.
299 //
300 void Value::uncheckedReplaceAllUsesWith(Value *New) {
301   // Notify all ValueHandles (if present) that this value is going away.
302   if (HasValueHandle)
303     ValueHandleBase::ValueIsRAUWd(this, New);
304
305   while (!use_empty()) {
306     Use &U = *UseList;
307     // Must handle Constants specially, we cannot call replaceUsesOfWith on a
308     // constant because they are uniqued.
309     if (Constant *C = dyn_cast<Constant>(U.getUser())) {
310       if (!isa<GlobalValue>(C)) {
311         C->replaceUsesOfWithOnConstant(this, New, &U);
312         continue;
313       }
314     }
315
316     U.set(New);
317   }
318 }
319
320 void Value::replaceAllUsesWith(Value *New) {
321   assert(New && "Value::replaceAllUsesWith(<null>) is invalid!");
322   assert(New != this && "this->replaceAllUsesWith(this) is NOT valid!");
323   assert(New->getType() == getType() &&
324          "replaceAllUses of value with new value of different type!");
325
326   uncheckedReplaceAllUsesWith(New);
327 }
328
329 Value *Value::stripPointerCasts() {
330   if (!getType()->isPointerTy())
331     return this;
332
333   // Even though we don't look through PHI nodes, we could be called on an
334   // instruction in an unreachable block, which may be on a cycle.
335   SmallPtrSet<Value *, 4> Visited;
336
337   Value *V = this;
338   Visited.insert(V);
339   do {
340     if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
341       if (!GEP->hasAllZeroIndices())
342         return V;
343       V = GEP->getPointerOperand();
344     } else if (Operator::getOpcode(V) == Instruction::BitCast) {
345       V = cast<Operator>(V)->getOperand(0);
346     } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
347       if (GA->mayBeOverridden())
348         return V;
349       V = GA->getAliasee();
350     } else {
351       return V;
352     }
353     assert(V->getType()->isPointerTy() && "Unexpected operand type!");
354   } while (Visited.insert(V));
355
356   return V;
357 }
358
359 /// isDereferenceablePointer - Test if this value is always a pointer to
360 /// allocated and suitably aligned memory for a simple load or store.
361 bool Value::isDereferenceablePointer() const {
362   // Note that it is not safe to speculate into a malloc'd region because
363   // malloc may return null.
364   // It's also not always safe to follow a bitcast, for example:
365   //   bitcast i8* (alloca i8) to i32*
366   // would result in a 4-byte load from a 1-byte alloca. Some cases could
367   // be handled using TargetData to check sizes and alignments though.
368
369   // These are obviously ok.
370   if (isa<AllocaInst>(this)) return true;
371
372   // Global variables which can't collapse to null are ok.
373   if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(this))
374     return !GV->hasExternalWeakLinkage();
375
376   // For GEPs, determine if the indexing lands within the allocated object.
377   if (const GEPOperator *GEP = dyn_cast<GEPOperator>(this)) {
378     // Conservatively require that the base pointer be fully dereferenceable.
379     if (!GEP->getOperand(0)->isDereferenceablePointer())
380       return false;
381     // Check the indices.
382     gep_type_iterator GTI = gep_type_begin(GEP);
383     for (User::const_op_iterator I = GEP->op_begin()+1,
384          E = GEP->op_end(); I != E; ++I) {
385       Value *Index = *I;
386       const Type *Ty = *GTI++;
387       // Struct indices can't be out of bounds.
388       if (isa<StructType>(Ty))
389         continue;
390       ConstantInt *CI = dyn_cast<ConstantInt>(Index);
391       if (!CI)
392         return false;
393       // Zero is always ok.
394       if (CI->isZero())
395         continue;
396       // Check to see that it's within the bounds of an array.
397       const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
398       if (!ATy)
399         return false;
400       if (CI->getValue().getActiveBits() > 64)
401         return false;
402       if (CI->getZExtValue() >= ATy->getNumElements())
403         return false;
404     }
405     // Indices check out; this is dereferenceable.
406     return true;
407   }
408
409   // If we don't know, assume the worst.
410   return false;
411 }
412
413 /// DoPHITranslation - If this value is a PHI node with CurBB as its parent,
414 /// return the value in the PHI node corresponding to PredBB.  If not, return
415 /// ourself.  This is useful if you want to know the value something has in a
416 /// predecessor block.
417 Value *Value::DoPHITranslation(const BasicBlock *CurBB,
418                                const BasicBlock *PredBB) {
419   PHINode *PN = dyn_cast<PHINode>(this);
420   if (PN && PN->getParent() == CurBB)
421     return PN->getIncomingValueForBlock(PredBB);
422   return this;
423 }
424
425 LLVMContext &Value::getContext() const { return VTy->getContext(); }
426
427 //===----------------------------------------------------------------------===//
428 //                             ValueHandleBase Class
429 //===----------------------------------------------------------------------===//
430
431 /// AddToExistingUseList - Add this ValueHandle to the use list for VP, where
432 /// List is known to point into the existing use list.
433 void ValueHandleBase::AddToExistingUseList(ValueHandleBase **List) {
434   assert(List && "Handle list is null?");
435
436   // Splice ourselves into the list.
437   Next = *List;
438   *List = this;
439   setPrevPtr(List);
440   if (Next) {
441     Next->setPrevPtr(&Next);
442     assert(VP == Next->VP && "Added to wrong list?");
443   }
444 }
445
446 void ValueHandleBase::AddToExistingUseListAfter(ValueHandleBase *List) {
447   assert(List && "Must insert after existing node");
448
449   Next = List->Next;
450   setPrevPtr(&List->Next);
451   List->Next = this;
452   if (Next)
453     Next->setPrevPtr(&Next);
454 }
455
456 /// AddToUseList - Add this ValueHandle to the use list for VP.
457 void ValueHandleBase::AddToUseList() {
458   assert(VP && "Null pointer doesn't have a use list!");
459
460   LLVMContextImpl *pImpl = VP->getContext().pImpl;
461
462   if (VP->HasValueHandle) {
463     // If this value already has a ValueHandle, then it must be in the
464     // ValueHandles map already.
465     ValueHandleBase *&Entry = pImpl->ValueHandles[VP];
466     assert(Entry != 0 && "Value doesn't have any handles?");
467     AddToExistingUseList(&Entry);
468     return;
469   }
470
471   // Ok, it doesn't have any handles yet, so we must insert it into the
472   // DenseMap.  However, doing this insertion could cause the DenseMap to
473   // reallocate itself, which would invalidate all of the PrevP pointers that
474   // point into the old table.  Handle this by checking for reallocation and
475   // updating the stale pointers only if needed.
476   DenseMap<Value*, ValueHandleBase*> &Handles = pImpl->ValueHandles;
477   const void *OldBucketPtr = Handles.getPointerIntoBucketsArray();
478
479   ValueHandleBase *&Entry = Handles[VP];
480   assert(Entry == 0 && "Value really did already have handles?");
481   AddToExistingUseList(&Entry);
482   VP->HasValueHandle = true;
483
484   // If reallocation didn't happen or if this was the first insertion, don't
485   // walk the table.
486   if (Handles.isPointerIntoBucketsArray(OldBucketPtr) ||
487       Handles.size() == 1) {
488     return;
489   }
490
491   // Okay, reallocation did happen.  Fix the Prev Pointers.
492   for (DenseMap<Value*, ValueHandleBase*>::iterator I = Handles.begin(),
493        E = Handles.end(); I != E; ++I) {
494     assert(I->second && I->first == I->second->VP && "List invariant broken!");
495     I->second->setPrevPtr(&I->second);
496   }
497 }
498
499 /// RemoveFromUseList - Remove this ValueHandle from its current use list.
500 void ValueHandleBase::RemoveFromUseList() {
501   assert(VP && VP->HasValueHandle && "Pointer doesn't have a use list!");
502
503   // Unlink this from its use list.
504   ValueHandleBase **PrevPtr = getPrevPtr();
505   assert(*PrevPtr == this && "List invariant broken");
506
507   *PrevPtr = Next;
508   if (Next) {
509     assert(Next->getPrevPtr() == &Next && "List invariant broken");
510     Next->setPrevPtr(PrevPtr);
511     return;
512   }
513
514   // If the Next pointer was null, then it is possible that this was the last
515   // ValueHandle watching VP.  If so, delete its entry from the ValueHandles
516   // map.
517   LLVMContextImpl *pImpl = VP->getContext().pImpl;
518   DenseMap<Value*, ValueHandleBase*> &Handles = pImpl->ValueHandles;
519   if (Handles.isPointerIntoBucketsArray(PrevPtr)) {
520     Handles.erase(VP);
521     VP->HasValueHandle = false;
522   }
523 }
524
525
526 void ValueHandleBase::ValueIsDeleted(Value *V) {
527   assert(V->HasValueHandle && "Should only be called if ValueHandles present");
528
529   // Get the linked list base, which is guaranteed to exist since the
530   // HasValueHandle flag is set.
531   LLVMContextImpl *pImpl = V->getContext().pImpl;
532   ValueHandleBase *Entry = pImpl->ValueHandles[V];
533   assert(Entry && "Value bit set but no entries exist");
534
535   // We use a local ValueHandleBase as an iterator so that ValueHandles can add
536   // and remove themselves from the list without breaking our iteration.  This
537   // is not really an AssertingVH; we just have to give ValueHandleBase a kind.
538   // Note that we deliberately do not the support the case when dropping a value
539   // handle results in a new value handle being permanently added to the list
540   // (as might occur in theory for CallbackVH's): the new value handle will not
541   // be processed and the checking code will mete out righteous punishment if
542   // the handle is still present once we have finished processing all the other
543   // value handles (it is fine to momentarily add then remove a value handle).
544   for (ValueHandleBase Iterator(Assert, *Entry); Entry; Entry = Iterator.Next) {
545     Iterator.RemoveFromUseList();
546     Iterator.AddToExistingUseListAfter(Entry);
547     assert(Entry->Next == &Iterator && "Loop invariant broken.");
548
549     switch (Entry->getKind()) {
550     case Assert:
551       break;
552     case Tracking:
553       // Mark that this value has been deleted by setting it to an invalid Value
554       // pointer.
555       Entry->operator=(DenseMapInfo<Value *>::getTombstoneKey());
556       break;
557     case Weak:
558       // Weak just goes to null, which will unlink it from the list.
559       Entry->operator=(0);
560       break;
561     case Callback:
562       // Forward to the subclass's implementation.
563       static_cast<CallbackVH*>(Entry)->deleted();
564       break;
565     }
566   }
567
568   // All callbacks, weak references, and assertingVHs should be dropped by now.
569   if (V->HasValueHandle) {
570 #ifndef NDEBUG      // Only in +Asserts mode...
571     dbgs() << "While deleting: " << *V->getType() << " %" << V->getNameStr()
572            << "\n";
573     if (pImpl->ValueHandles[V]->getKind() == Assert)
574       llvm_unreachable("An asserting value handle still pointed to this"
575                        " value!");
576
577 #endif
578     llvm_unreachable("All references to V were not removed?");
579   }
580 }
581
582 void ValueHandleBase::ValueAddedUse(Use &U) {
583   assert(U->HasValueHandle && "Should only be called if ValueHandles present");
584
585   // Get the linked list base, which is guaranteed to exist since the
586   // HasValueHandle flag is set.
587   LLVMContextImpl *pImpl = U->getContext().pImpl;
588   ValueHandleBase *Entry = pImpl->ValueHandles[U.get()];
589
590   assert(Entry && "Value bit set but no entries exist");
591
592   // We use a local ValueHandleBase as an iterator so that
593   // ValueHandles can add and remove themselves from the list without
594   // breaking our iteration.  This is not really an AssertingVH; we
595   // just have to give ValueHandleBase some kind.
596   for (ValueHandleBase Iterator(Assert, *Entry); Entry; Entry = Iterator.Next) {
597     Iterator.RemoveFromUseList();
598     Iterator.AddToExistingUseListAfter(Entry);
599     assert(Entry->Next == &Iterator && "Loop invariant broken.");
600
601     switch (Entry->getKind()) {
602     default:
603       break;
604     case Callback:
605       static_cast<CallbackVH*>(Entry)->addedUse(U);
606       break;
607     }
608   }
609 }
610
611 void ValueHandleBase::ValueIsRAUWd(Value *Old, Value *New) {
612   assert(Old->HasValueHandle &&"Should only be called if ValueHandles present");
613   assert(Old != New && "Changing value into itself!");
614
615   // Get the linked list base, which is guaranteed to exist since the
616   // HasValueHandle flag is set.
617   LLVMContextImpl *pImpl = Old->getContext().pImpl;
618   ValueHandleBase *Entry = pImpl->ValueHandles[Old];
619
620   assert(Entry && "Value bit set but no entries exist");
621
622   // We use a local ValueHandleBase as an iterator so that
623   // ValueHandles can add and remove themselves from the list without
624   // breaking our iteration.  This is not really an AssertingVH; we
625   // just have to give ValueHandleBase some kind.
626   for (ValueHandleBase Iterator(Assert, *Entry); Entry; Entry = Iterator.Next) {
627     Iterator.RemoveFromUseList();
628     Iterator.AddToExistingUseListAfter(Entry);
629     assert(Entry->Next == &Iterator && "Loop invariant broken.");
630
631     switch (Entry->getKind()) {
632     case Assert:
633       // Asserting handle does not follow RAUW implicitly.
634       break;
635     case Tracking:
636       // Tracking goes to new value like a WeakVH. Note that this may make it
637       // something incompatible with its templated type. We don't want to have a
638       // virtual (or inline) interface to handle this though, so instead we make
639       // the TrackingVH accessors guarantee that a client never sees this value.
640
641       // FALLTHROUGH
642     case Weak:
643       // Weak goes to the new value, which will unlink it from Old's list.
644       Entry->operator=(New);
645       break;
646     case Callback:
647       // Forward to the subclass's implementation.
648       static_cast<CallbackVH*>(Entry)->allUsesReplacedWith(New);
649       break;
650     }
651   }
652
653 #ifndef NDEBUG
654   // If any new tracking or weak value handles were added while processing the
655   // list, then complain about it now.
656   if (Old->HasValueHandle)
657     for (Entry = pImpl->ValueHandles[Old]; Entry; Entry = Entry->Next)
658       switch (Entry->getKind()) {
659       case Tracking:
660       case Weak:
661         dbgs() << "After RAUW from " << *Old->getType() << " %"
662           << Old->getNameStr() << " to " << *New->getType() << " %"
663           << New->getNameStr() << "\n";
664         llvm_unreachable("A tracking or weak value handle still pointed to the"
665                          " old value!\n");
666       default:
667         break;
668       }
669 #endif
670 }
671
672 /// ~CallbackVH. Empty, but defined here to avoid emitting the vtable
673 /// more than once.
674 CallbackVH::~CallbackVH() {}
675
676
677 //===----------------------------------------------------------------------===//
678 //                                 User Class
679 //===----------------------------------------------------------------------===//
680
681 // replaceUsesOfWith - Replaces all references to the "From" definition with
682 // references to the "To" definition.
683 //
684 void User::replaceUsesOfWith(Value *From, Value *To) {
685   if (From == To) return;   // Duh what?
686
687   assert((!isa<Constant>(this) || isa<GlobalValue>(this)) &&
688          "Cannot call User::replaceUsesOfWith on a constant!");
689
690   for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
691     if (getOperand(i) == From) {  // Is This operand is pointing to oldval?
692       // The side effects of this setOperand call include linking to
693       // "To", adding "this" to the uses list of To, and
694       // most importantly, removing "this" from the use list of "From".
695       setOperand(i, To); // Fix it now...
696     }
697 }