LLVM  3.7.0
Value.cpp
Go to the documentation of this file.
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"
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"
32 #include "llvm/Support/Debug.h"
36 #include <algorithm>
37 using namespace llvm;
38 
39 //===----------------------------------------------------------------------===//
40 // Value Class
41 //===----------------------------------------------------------------------===//
42 static inline Type *checkType(Type *Ty) {
43  assert(Ty && "Value defined with a null type: Error!");
44  return Ty;
45 }
46 
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
53  // constructed.
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!");
61 }
62 
64  // Notify all ValueHandles (if present) that this value is going away.
65  if (HasValueHandle)
67  if (isUsedByMetadata())
69 
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.
75  //
76  if (!use_empty()) {
77  dbgs() << "While deleting: " << *VTy << " %" << getName() << "\n";
78  for (auto *U : users())
79  dbgs() << "Use still stuck around after Def is destroyed:" << *U << "\n";
80  }
81 #endif
82  assert(use_empty() && "Uses remain when a value is destroyed!");
83 
84  // If this value is named, destroy the name. This should not be in a symtab
85  // at this point.
86  destroyValueName();
87 }
88 
89 void Value::destroyValueName() {
91  if (Name)
92  Name->Destroy();
93  setValueName(nullptr);
94 }
95 
96 bool Value::hasNUses(unsigned N) const {
97  const_use_iterator UI = use_begin(), E = use_end();
98 
99  for (; N; --N, ++UI)
100  if (UI == E) return false; // Too few.
101  return UI == E;
102 }
103 
104 bool Value::hasNUsesOrMore(unsigned N) const {
105  const_use_iterator UI = use_begin(), E = use_end();
106 
107  for (; N; --N, ++UI)
108  if (UI == E) return false; // Too few.
109 
110  return true;
111 }
112 
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.
117  //
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())
125  return true;
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)
129  return true;
130  }
131  return false;
132 }
133 
134 unsigned Value::getNumUses() const {
135  return (unsigned)std::distance(use_begin(), use_end());
136 }
137 
138 static bool getSymTab(Value *V, ValueSymbolTable *&ST) {
139  ST = nullptr;
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();
153  } else {
154  assert(isa<Constant>(V) && "Unknown value type!");
155  return true; // no name is setable for this.
156  }
157  return false;
158 }
159 
161  if (!HasName) return nullptr;
162 
163  LLVMContext &Ctx = getContext();
164  auto I = Ctx.pImpl->ValueNames.find(this);
165  assert(I != Ctx.pImpl->ValueNames.end() &&
166  "No name entry found!");
167 
168  return I->second;
169 }
170 
172  LLVMContext &Ctx = getContext();
173 
174  assert(HasName == Ctx.pImpl->ValueNames.count(this) &&
175  "HasName bit out of sync!");
176 
177  if (!VN) {
178  if (HasName)
179  Ctx.pImpl->ValueNames.erase(this);
180  HasName = false;
181  return;
182  }
183 
184  HasName = true;
185  Ctx.pImpl->ValueNames[this] = VN;
186 }
187 
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
191  // terminated.
192  if (!hasName())
193  return StringRef("", 0);
194  return getValueName()->getKey();
195 }
196 
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())
200  return;
201 
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");
206 
207  // Name isn't changing?
208  if (getName() == NameRef)
209  return;
210 
211  assert(!getType()->isVoidTy() && "Cannot assign a name to void values!");
212 
213  // Get the symbol table to update for this object.
215  if (getSymTab(this, ST))
216  return; // Cannot set a name on this value (e.g. constant).
217 
218  if (!ST) { // No symbol table to update? Just do the change.
219  if (NameRef.empty()) {
220  // Free the name for this value.
221  destroyValueName();
222  return;
223  }
224 
225  // NOTE: Could optimize for the case the name is shrinking to not deallocate
226  // then reallocated.
227  destroyValueName();
228 
229  // Create the new name.
231  getValueName()->setValue(this);
232  return;
233  }
234 
235  // NOTE: Could optimize for the case the name is shrinking to not deallocate
236  // then reallocated.
237  if (hasName()) {
238  // Remove old name.
239  ST->removeValueName(getValueName());
240  destroyValueName();
241 
242  if (NameRef.empty())
243  return;
244  }
245 
246  // Name is changing to something new.
247  setValueName(ST->createValueName(NameRef, this));
248 }
249 
250 void Value::setName(const Twine &NewName) {
251  setNameImpl(NewName);
252  if (Function *F = dyn_cast<Function>(this))
253  F->recalculateIntrinsicID();
254 }
255 
257  ValueSymbolTable *ST = nullptr;
258  // If this value has a name, drop it.
259  if (hasName()) {
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
263  // it has one.
264  if (V->hasName()) V->setName("");
265  return; // Cannot set a name on this value (e.g. constant).
266  }
267 
268  // Remove old name.
269  if (ST)
270  ST->removeValueName(getValueName());
271  destroyValueName();
272  }
273 
274  // Now we know that this has no name.
275 
276  // If V has no name either, we're done.
277  if (!V->hasName()) return;
278 
279  // Get this's symtab if we didn't before.
280  if (!ST) {
281  if (getSymTab(this, ST)) {
282  // Clear V's name.
283  V->setName("");
284  return; // Cannot set a name on this value (e.g. constant).
285  }
286  }
287 
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;
292 
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.
295  if (ST == VST) {
296  // Take the name!
298  V->setValueName(nullptr);
299  getValueName()->setValue(this);
300  return;
301  }
302 
303  // Otherwise, things are slightly more complex. Remove V's name from VST and
304  // then reinsert it into ST.
305 
306  if (VST)
307  VST->removeValueName(V->getValueName());
309  V->setValueName(nullptr);
310  getValueName()->setValue(this);
311 
312  if (ST)
313  ST->reinsertValue(this);
314 }
315 
316 #ifndef NDEBUG
318  Constant *C) {
319  if (!Cache.insert(Expr).second)
320  return false;
321 
322  for (auto &O : Expr->operands()) {
323  if (O == C)
324  return true;
325  auto *CE = dyn_cast<ConstantExpr>(O);
326  if (!CE)
327  continue;
328  if (contains(Cache, CE, C))
329  return true;
330  }
331  return false;
332 }
333 
334 static bool contains(Value *Expr, Value *V) {
335  if (Expr == V)
336  return true;
337 
338  auto *C = dyn_cast<Constant>(V);
339  if (!C)
340  return false;
341 
342  auto *CE = dyn_cast<ConstantExpr>(Expr);
343  if (!CE)
344  return false;
345 
347  return contains(Cache, CE, C);
348 }
349 #endif
350 
352  assert(New && "Value::replaceAllUsesWith(<null>) is invalid!");
353  assert(!contains(New, this) &&
354  "this->replaceAllUsesWith(expr(this)) is NOT valid!");
355  assert(New->getType() == getType() &&
356  "replaceAllUses of value with new value of different type!");
357 
358  // Notify all ValueHandles (if present) that this value is going away.
359  if (HasValueHandle)
361  if (isUsedByMetadata())
362  ValueAsMetadata::handleRAUW(this, New);
363 
364  while (!use_empty()) {
365  Use &U = *UseList;
366  // Must handle Constants specially, we cannot call replaceUsesOfWith on a
367  // constant because they are uniqued.
368  if (auto *C = dyn_cast<Constant>(U.getUser())) {
369  if (!isa<GlobalValue>(C)) {
370  C->handleOperandChange(this, New, &U);
371  continue;
372  }
373  }
374 
375  U.set(New);
376  }
377 
378  if (BasicBlock *BB = dyn_cast<BasicBlock>(this))
379  BB->replaceSuccessorsPhiUsesWith(cast<BasicBlock>(New));
380 }
381 
382 // Like replaceAllUsesWith except it does not handle constants or basic blocks.
383 // This routine leaves uses within BB.
385  assert(New && "Value::replaceUsesOutsideBlock(<null>, BB) is invalid!");
386  assert(!contains(New, this) &&
387  "this->replaceUsesOutsideBlock(expr(this), BB) is NOT valid!");
388  assert(New->getType() == getType() &&
389  "replaceUses of value with new value of different type!");
390  assert(BB && "Basic block that may contain a use of 'New' must be defined\n");
391 
392  use_iterator UI = use_begin(), E = use_end();
393  for (; UI != E;) {
394  Use &U = *UI;
395  ++UI;
396  auto *Usr = dyn_cast<Instruction>(U.getUser());
397  if (Usr && Usr->getParent() == BB)
398  continue;
399  U.set(New);
400  }
401  return;
402 }
403 
404 namespace {
405 // Various metrics for how much to strip off of pointers.
407  PSK_ZeroIndices,
408  PSK_ZeroIndicesAndAliases,
409  PSK_InBoundsConstantIndices,
410  PSK_InBounds
411 };
412 
413 template <PointerStripKind StripKind>
414 static Value *stripPointerCastsAndOffsets(Value *V) {
415  if (!V->getType()->isPointerTy())
416  return V;
417 
418  // Even though we don't look through PHI nodes, we could be called on an
419  // instruction in an unreachable block, which may be on a cycle.
420  SmallPtrSet<Value *, 4> Visited;
421 
422  Visited.insert(V);
423  do {
424  if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
425  switch (StripKind) {
426  case PSK_ZeroIndicesAndAliases:
427  case PSK_ZeroIndices:
428  if (!GEP->hasAllZeroIndices())
429  return V;
430  break;
431  case PSK_InBoundsConstantIndices:
432  if (!GEP->hasAllConstantIndices())
433  return V;
434  // fallthrough
435  case PSK_InBounds:
436  if (!GEP->isInBounds())
437  return V;
438  break;
439  }
440  V = GEP->getPointerOperand();
441  } else if (Operator::getOpcode(V) == Instruction::BitCast ||
442  Operator::getOpcode(V) == Instruction::AddrSpaceCast) {
443  V = cast<Operator>(V)->getOperand(0);
444  } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
445  if (StripKind == PSK_ZeroIndices || GA->mayBeOverridden())
446  return V;
447  V = GA->getAliasee();
448  } else {
449  return V;
450  }
451  assert(V->getType()->isPointerTy() && "Unexpected operand type!");
452  } while (Visited.insert(V).second);
453 
454  return V;
455 }
456 } // namespace
457 
459  return stripPointerCastsAndOffsets<PSK_ZeroIndicesAndAliases>(this);
460 }
461 
463  return stripPointerCastsAndOffsets<PSK_ZeroIndices>(this);
464 }
465 
467  return stripPointerCastsAndOffsets<PSK_InBoundsConstantIndices>(this);
468 }
469 
471  APInt &Offset) {
472  if (!getType()->isPointerTy())
473  return this;
474 
475  assert(Offset.getBitWidth() == DL.getPointerSizeInBits(cast<PointerType>(
476  getType())->getAddressSpace()) &&
477  "The offset must have exactly as many bits as our pointer.");
478 
479  // Even though we don't look through PHI nodes, we could be called on an
480  // instruction in an unreachable block, which may be on a cycle.
481  SmallPtrSet<Value *, 4> Visited;
482  Visited.insert(this);
483  Value *V = this;
484  do {
485  if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
486  if (!GEP->isInBounds())
487  return V;
488  APInt GEPOffset(Offset);
489  if (!GEP->accumulateConstantOffset(DL, GEPOffset))
490  return V;
491  Offset = GEPOffset;
492  V = GEP->getPointerOperand();
493  } else if (Operator::getOpcode(V) == Instruction::BitCast ||
494  Operator::getOpcode(V) == Instruction::AddrSpaceCast) {
495  V = cast<Operator>(V)->getOperand(0);
496  } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
497  V = GA->getAliasee();
498  } else {
499  return V;
500  }
501  assert(V->getType()->isPointerTy() && "Unexpected operand type!");
502  } while (Visited.insert(V).second);
503 
504  return V;
505 }
506 
508  return stripPointerCastsAndOffsets<PSK_InBounds>(this);
509 }
510 
512  const BasicBlock *PredBB) {
513  PHINode *PN = dyn_cast<PHINode>(this);
514  if (PN && PN->getParent() == CurBB)
515  return PN->getIncomingValueForBlock(PredBB);
516  return this;
517 }
518 
519 LLVMContext &Value::getContext() const { return VTy->getContext(); }
520 
522  if (!UseList || !UseList->Next)
523  // No need to reverse 0 or 1 uses.
524  return;
525 
526  Use *Head = UseList;
527  Use *Current = UseList->Next;
528  Head->Next = nullptr;
529  while (Current) {
530  Use *Next = Current->Next;
531  Current->Next = Head;
532  Head->setPrev(&Current->Next);
533  Head = Current;
534  Current = Next;
535  }
536  UseList = Head;
537  Head->setPrev(&UseList);
538 }
539 
540 //===----------------------------------------------------------------------===//
541 // ValueHandleBase Class
542 //===----------------------------------------------------------------------===//
543 
544 void ValueHandleBase::AddToExistingUseList(ValueHandleBase **List) {
545  assert(List && "Handle list is null?");
546 
547  // Splice ourselves into the list.
548  Next = *List;
549  *List = this;
550  setPrevPtr(List);
551  if (Next) {
552  Next->setPrevPtr(&Next);
553  assert(V == Next->V && "Added to wrong list?");
554  }
555 }
556 
557 void ValueHandleBase::AddToExistingUseListAfter(ValueHandleBase *List) {
558  assert(List && "Must insert after existing node");
559 
560  Next = List->Next;
561  setPrevPtr(&List->Next);
562  List->Next = this;
563  if (Next)
564  Next->setPrevPtr(&Next);
565 }
566 
567 void ValueHandleBase::AddToUseList() {
568  assert(V && "Null pointer doesn't have a use list!");
569 
570  LLVMContextImpl *pImpl = V->getContext().pImpl;
571 
572  if (V->HasValueHandle) {
573  // If this value already has a ValueHandle, then it must be in the
574  // ValueHandles map already.
575  ValueHandleBase *&Entry = pImpl->ValueHandles[V];
576  assert(Entry && "Value doesn't have any handles?");
577  AddToExistingUseList(&Entry);
578  return;
579  }
580 
581  // Ok, it doesn't have any handles yet, so we must insert it into the
582  // DenseMap. However, doing this insertion could cause the DenseMap to
583  // reallocate itself, which would invalidate all of the PrevP pointers that
584  // point into the old table. Handle this by checking for reallocation and
585  // updating the stale pointers only if needed.
587  const void *OldBucketPtr = Handles.getPointerIntoBucketsArray();
588 
589  ValueHandleBase *&Entry = Handles[V];
590  assert(!Entry && "Value really did already have handles?");
591  AddToExistingUseList(&Entry);
592  V->HasValueHandle = true;
593 
594  // If reallocation didn't happen or if this was the first insertion, don't
595  // walk the table.
596  if (Handles.isPointerIntoBucketsArray(OldBucketPtr) ||
597  Handles.size() == 1) {
598  return;
599  }
600 
601  // Okay, reallocation did happen. Fix the Prev Pointers.
603  E = Handles.end(); I != E; ++I) {
604  assert(I->second && I->first == I->second->V &&
605  "List invariant broken!");
606  I->second->setPrevPtr(&I->second);
607  }
608 }
609 
610 void ValueHandleBase::RemoveFromUseList() {
611  assert(V && V->HasValueHandle &&
612  "Pointer doesn't have a use list!");
613 
614  // Unlink this from its use list.
615  ValueHandleBase **PrevPtr = getPrevPtr();
616  assert(*PrevPtr == this && "List invariant broken");
617 
618  *PrevPtr = Next;
619  if (Next) {
620  assert(Next->getPrevPtr() == &Next && "List invariant broken");
621  Next->setPrevPtr(PrevPtr);
622  return;
623  }
624 
625  // If the Next pointer was null, then it is possible that this was the last
626  // ValueHandle watching VP. If so, delete its entry from the ValueHandles
627  // map.
628  LLVMContextImpl *pImpl = V->getContext().pImpl;
630  if (Handles.isPointerIntoBucketsArray(PrevPtr)) {
631  Handles.erase(V);
632  V->HasValueHandle = false;
633  }
634 }
635 
636 
638  assert(V->HasValueHandle && "Should only be called if ValueHandles present");
639 
640  // Get the linked list base, which is guaranteed to exist since the
641  // HasValueHandle flag is set.
642  LLVMContextImpl *pImpl = V->getContext().pImpl;
643  ValueHandleBase *Entry = pImpl->ValueHandles[V];
644  assert(Entry && "Value bit set but no entries exist");
645 
646  // We use a local ValueHandleBase as an iterator so that ValueHandles can add
647  // and remove themselves from the list without breaking our iteration. This
648  // is not really an AssertingVH; we just have to give ValueHandleBase a kind.
649  // Note that we deliberately do not the support the case when dropping a value
650  // handle results in a new value handle being permanently added to the list
651  // (as might occur in theory for CallbackVH's): the new value handle will not
652  // be processed and the checking code will mete out righteous punishment if
653  // the handle is still present once we have finished processing all the other
654  // value handles (it is fine to momentarily add then remove a value handle).
655  for (ValueHandleBase Iterator(Assert, *Entry); Entry; Entry = Iterator.Next) {
656  Iterator.RemoveFromUseList();
657  Iterator.AddToExistingUseListAfter(Entry);
658  assert(Entry->Next == &Iterator && "Loop invariant broken.");
659 
660  switch (Entry->getKind()) {
661  case Assert:
662  break;
663  case Tracking:
664  // Mark that this value has been deleted by setting it to an invalid Value
665  // pointer.
666  Entry->operator=(DenseMapInfo<Value *>::getTombstoneKey());
667  break;
668  case Weak:
669  // Weak just goes to null, which will unlink it from the list.
670  Entry->operator=(nullptr);
671  break;
672  case Callback:
673  // Forward to the subclass's implementation.
674  static_cast<CallbackVH*>(Entry)->deleted();
675  break;
676  }
677  }
678 
679  // All callbacks, weak references, and assertingVHs should be dropped by now.
680  if (V->HasValueHandle) {
681 #ifndef NDEBUG // Only in +Asserts mode...
682  dbgs() << "While deleting: " << *V->getType() << " %" << V->getName()
683  << "\n";
684  if (pImpl->ValueHandles[V]->getKind() == Assert)
685  llvm_unreachable("An asserting value handle still pointed to this"
686  " value!");
687 
688 #endif
689  llvm_unreachable("All references to V were not removed?");
690  }
691 }
692 
693 
695  assert(Old->HasValueHandle &&"Should only be called if ValueHandles present");
696  assert(Old != New && "Changing value into itself!");
697  assert(Old->getType() == New->getType() &&
698  "replaceAllUses of value with new value of different type!");
699 
700  // Get the linked list base, which is guaranteed to exist since the
701  // HasValueHandle flag is set.
702  LLVMContextImpl *pImpl = Old->getContext().pImpl;
703  ValueHandleBase *Entry = pImpl->ValueHandles[Old];
704 
705  assert(Entry && "Value bit set but no entries exist");
706 
707  // We use a local ValueHandleBase as an iterator so that
708  // ValueHandles can add and remove themselves from the list without
709  // breaking our iteration. This is not really an AssertingVH; we
710  // just have to give ValueHandleBase some kind.
711  for (ValueHandleBase Iterator(Assert, *Entry); Entry; Entry = Iterator.Next) {
712  Iterator.RemoveFromUseList();
713  Iterator.AddToExistingUseListAfter(Entry);
714  assert(Entry->Next == &Iterator && "Loop invariant broken.");
715 
716  switch (Entry->getKind()) {
717  case Assert:
718  // Asserting handle does not follow RAUW implicitly.
719  break;
720  case Tracking:
721  // Tracking goes to new value like a WeakVH. Note that this may make it
722  // something incompatible with its templated type. We don't want to have a
723  // virtual (or inline) interface to handle this though, so instead we make
724  // the TrackingVH accessors guarantee that a client never sees this value.
725 
726  // FALLTHROUGH
727  case Weak:
728  // Weak goes to the new value, which will unlink it from Old's list.
729  Entry->operator=(New);
730  break;
731  case Callback:
732  // Forward to the subclass's implementation.
733  static_cast<CallbackVH*>(Entry)->allUsesReplacedWith(New);
734  break;
735  }
736  }
737 
738 #ifndef NDEBUG
739  // If any new tracking or weak value handles were added while processing the
740  // list, then complain about it now.
741  if (Old->HasValueHandle)
742  for (Entry = pImpl->ValueHandles[Old]; Entry; Entry = Entry->Next)
743  switch (Entry->getKind()) {
744  case Tracking:
745  case Weak:
746  dbgs() << "After RAUW from " << *Old->getType() << " %"
747  << Old->getName() << " to " << *New->getType() << " %"
748  << New->getName() << "\n";
749  llvm_unreachable("A tracking or weak value handle still pointed to the"
750  " old value!\n");
751  default:
752  break;
753  }
754 #endif
755 }
756 
757 // Pin the vtable to this file.
758 void CallbackVH::anchor() {}
This is the common base class of value handles.
Definition: ValueHandle.h:41
use_iterator use_end()
Definition: Value.h:281
use_iterator_impl< Use > use_iterator
Definition: Value.h:277
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:104
This class provides a symbol table of name/value pairs.
static void ValueIsDeleted(Value *V)
Definition: Value.cpp:637
Value * stripInBoundsConstantOffsets()
Strip off pointer casts and all-constant inbounds GEPs.
Definition: Value.cpp:466
LLVM Argument representation.
Definition: Argument.h:35
bool hasName() const
Definition: Value.h:228
StringMapEntry - This is used to represent one value that is inserted into a StringMap.
Definition: StringMap.h:28
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:114
void setValue(const ValueTy &V)
Definition: StringMap.h:131
bool hasNUses(unsigned N) const
Return true if this Value has exactly N users.
Definition: Value.cpp:96
static StringMapEntry * Create(StringRef Key, AllocatorTy &Allocator, InitType &&InitVal)
Create - Create a StringMapEntry for the specified key and default construct the value.
Definition: StringMap.h:143
F(f)
Hexagon Common GEP
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:188
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
Definition: SmallPtrSet.h:242
iterator begin()
Instruction iterator methods.
Definition: BasicBlock.h:231
static Type * checkType(Type *Ty)
Definition: Value.cpp:42
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:79
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:98
A Use represents the edge between a Value definition and its users.
Definition: Use.h:69
const void * getPointerIntoBucketsArray() const
getPointerIntoBucketsArray() - Return an opaque pointer into the buckets array.
Definition: DenseMap.h:259
void setName(const Twine &Name)
Change the name of the value.
Definition: Value.cpp:250
static bool contains(SmallPtrSetImpl< ConstantExpr * > &Cache, ConstantExpr *Expr, Constant *C)
Definition: Value.cpp:317
DenseMap< const Value *, ValueName * > ValueNames
Value * stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL, APInt &Offset)
Accumulate offsets from stripInBoundsConstantOffsets().
Definition: Value.cpp:470
#define false
Definition: ConvertUTF.c:65
void Destroy(AllocatorTy &Allocator)
Destroy - Destroy this StringMapEntry, releasing memory back to the specified allocator.
Definition: StringMap.h:193
ConstantExpr - a constant value that is initialized with an expression using other constant values...
Definition: Constants.h:852
static void handleRAUW(Value *From, Value *To)
Definition: Metadata.cpp:295
bool HasName
Definition: Value.h:111
LLVMContext & getContext() const
getContext - Return the LLVMContext in which this type was uniqued.
Definition: Type.h:125
bool isFirstClassType() const
isFirstClassType - Return true if the type is "first class", meaning it is a valid type for a Value...
Definition: Type.h:242
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:351
static void ValueIsRAUWd(Value *Old, Value *New)
Definition: Value.cpp:694
void takeName(Value *V)
Transfer the name from V to this value.
Definition: Value.cpp:256
ValueName * getValueName() const
Definition: Value.cpp:160
Value * stripPointerCastsNoFollowAliases()
Strip off pointer casts and all-zero GEPs.
Definition: Value.cpp:462
#define P(N)
bool erase(const KeyT &Val)
Definition: DenseMap.h:206
void set(Value *Val)
Definition: Value.h:516
LLVM Basic Block Representation.
Definition: BasicBlock.h:65
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:41
This is an important base class in LLVM.
Definition: Constant.h:41
This file contains the declarations for the subclasses of Constant, which represent the different fla...
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:264
Value * stripInBoundsOffsets()
Strip off pointer casts and inbounds GEPs.
Definition: Value.cpp:507
bool isUsedByMetadata() const
Return true if there is metadata referencing this value.
Definition: Value.h:392
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition: APInt.h:1273
ValueHandlesTy ValueHandles
User * getUser() const
Returns the User that contains this Use.
Definition: Use.cpp:41
op_range operands()
Definition: User.h:191
bool isPointerTy() const
isPointerTy - True if this is an instance of PointerType.
Definition: Type.h:217
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:519
use_iterator_impl< const Use > const_use_iterator
Definition: Value.h:278
bool isUsedInBasicBlock(const BasicBlock *BB) const
Check if this value is used in the specified basic block.
Definition: Value.cpp:113
LLVMContextImpl *const pImpl
Definition: LLVMContext.h:43
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Definition: SmallPtrSet.h:299
Value * DoPHITranslation(const BasicBlock *CurBB, const BasicBlock *PredBB)
Translate PHI node to its predecessor from the given basic block.
Definition: Value.cpp:511
iterator end()
Definition: BasicBlock.h:233
StringRef toStringRef(SmallVectorImpl< char > &Out) const
This returns the twine as a single StringRef if it can be represented as such.
Definition: Twine.h:454
Module.h This file contains the declarations for the Module class.
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:222
PointerStripKind
Definition: Value.cpp:406
Value * stripPointerCasts()
Strip off pointer casts, all-zero GEPs, and aliases.
Definition: Value.cpp:458
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:123
Class for arbitrary precision integers.
Definition: APInt.h:73
StringRef getKey() const
Definition: StringMap.h:124
Value * getIncomingValueForBlock(const BasicBlock *BB) const
iterator_range< user_iterator > users()
Definition: Value.h:300
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:285
bool isPointerIntoBucketsArray(const void *Ptr) const
isPointerIntoBucketsArray - Return true if the specified pointer points somewhere into the DenseMap's...
Definition: DenseMap.h:252
bool isStructTy() const
isStructTy - True if this is an instance of StructType.
Definition: Type.h:209
unsigned getOpcode() const
Return the opcode for this Instruction or ConstantExpr.
Definition: Operator.h:48
void reverseUseList()
Reverse the use-list.
Definition: Value.cpp:521
use_iterator use_begin()
Definition: Value.h:279
user_iterator_impl< const User > const_user_iterator
Definition: Value.h:293
unsigned size() const
Definition: DenseMap.h:82
static const size_t npos
Definition: StringRef.h:44
iterator begin()
Definition: DenseMap.h:64
#define I(x, y, z)
Definition: MD5.cpp:54
#define N
unsigned getPointerSizeInBits(unsigned AS=0) const
Layout pointer size, in bits FIXME: The defaults need to be removed once all of the backends/clients ...
Definition: DataLayout.h:329
static bool getSymTab(Value *V, ValueSymbolTable *&ST)
Definition: Value.cpp:138
iterator end()
Definition: DenseMap.h:68
size_t find_first_of(char C, size_t From=0) const
Find the first character in the string that is C, or npos if not found.
Definition: StringRef.h:279
bool hasNUsesOrMore(unsigned N) const
Return true if this value has N users or more.
Definition: Value.cpp:104
virtual ~Value()
Definition: Value.cpp:63
bool use_empty() const
Definition: Value.h:275
user_iterator user_begin()
Definition: Value.h:294
static void handleDeletion(Value *V)
Definition: Metadata.cpp:276
LLVM Value Representation.
Definition: Value.h:69
bool isTriviallyEmpty() const
Check if this twine is trivially empty; a false return value does not necessarily mean the twine is e...
Definition: Twine.h:399
void setValueName(ValueName *VN)
Definition: Value.cpp:171
Value handle with callbacks on RAUW and destruction.
Definition: ValueHandle.h:344
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40
void replaceUsesOutsideBlock(Value *V, BasicBlock *BB)
replaceUsesOutsideBlock - Go through the uses list for this definition and make each use point to "V"...
Definition: Value.cpp:384
unsigned getNumUses() const
This method computes the number of uses of this Value.
Definition: Value.cpp:134
const BasicBlock * getParent() const
Definition: Instruction.h:72
bool isVoidTy() const
isVoidTy - Return true if this is 'void'.
Definition: Type.h:137
bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:110
user_iterator user_end()
Definition: Value.h:296