LLVM  9.0.0svn
Value.h
Go to the documentation of this file.
1 //===- llvm/Value.h - Definition of the Value class -------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file declares the Value class.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_IR_VALUE_H
14 #define LLVM_IR_VALUE_H
15 
16 #include "llvm-c/Types.h"
18 #include "llvm/IR/Use.h"
20 #include "llvm/Support/Casting.h"
21 #include <cassert>
22 #include <iterator>
23 #include <memory>
24 
25 namespace llvm {
26 
27 class APInt;
28 class Argument;
29 class BasicBlock;
30 class Constant;
31 class ConstantData;
32 class ConstantAggregate;
33 class DataLayout;
34 class Function;
35 class GlobalAlias;
36 class GlobalIFunc;
37 class GlobalIndirectSymbol;
38 class GlobalObject;
39 class GlobalValue;
40 class GlobalVariable;
41 class InlineAsm;
42 class Instruction;
43 class LLVMContext;
44 class Module;
45 class ModuleSlotTracker;
46 class raw_ostream;
47 template<typename ValueTy> class StringMapEntry;
48 class StringRef;
49 class Twine;
50 class Type;
51 class User;
52 
54 
55 //===----------------------------------------------------------------------===//
56 // Value Class
57 //===----------------------------------------------------------------------===//
58 
59 /// LLVM Value Representation
60 ///
61 /// This is a very important LLVM class. It is the base class of all values
62 /// computed by a program that may be used as operands to other values. Value is
63 /// the super class of other important classes such as Instruction and Function.
64 /// All Values have a Type. Type is not a subclass of Value. Some values can
65 /// have a name and they belong to some Module. Setting the name on the Value
66 /// automatically updates the module's symbol table.
67 ///
68 /// Every value has a "use list" that keeps track of which other Values are
69 /// using this Value. A Value can also have an arbitrary number of ValueHandle
70 /// objects that watch it and listen to RAUW and Destroy events. See
71 /// llvm/IR/ValueHandle.h for details.
72 class Value {
73  // The least-significant bit of the first word of Value *must* be zero:
74  // http://www.llvm.org/docs/ProgrammersManual.html#the-waymarking-algorithm
75  Type *VTy;
76  Use *UseList;
77 
78  friend class ValueAsMetadata; // Allow access to IsUsedByMD.
79  friend class ValueHandleBase;
80 
81  const unsigned char SubclassID; // Subclass identifier (for isa/dyn_cast)
82  unsigned char HasValueHandle : 1; // Has a ValueHandle pointing to this?
83 
84 protected:
85  /// Hold subclass data that can be dropped.
86  ///
87  /// This member is similar to SubclassData, however it is for holding
88  /// information which may be used to aid optimization, but which may be
89  /// cleared to zero without affecting conservative interpretation.
90  unsigned char SubclassOptionalData : 7;
91 
92 private:
93  /// Hold arbitrary subclass data.
94  ///
95  /// This member is defined by this class, but is not used for anything.
96  /// Subclasses can use it to hold whatever state they find useful. This
97  /// field is initialized to zero by the ctor.
98  unsigned short SubclassData;
99 
100 protected:
101  /// The number of operands in the subclass.
102  ///
103  /// This member is defined by this class, but not used for anything.
104  /// Subclasses can use it to store their number of operands, if they have
105  /// any.
106  ///
107  /// This is stored here to save space in User on 64-bit hosts. Since most
108  /// instances of Value have operands, 32-bit hosts aren't significantly
109  /// affected.
110  ///
111  /// Note, this should *NOT* be used directly by any class other than User.
112  /// User uses this value to find the Use list.
113  enum : unsigned { NumUserOperandsBits = 28 };
115 
116  // Use the same type as the bitfield above so that MSVC will pack them.
117  unsigned IsUsedByMD : 1;
118  unsigned HasName : 1;
119  unsigned HasHungOffUses : 1;
120  unsigned HasDescriptor : 1;
121 
122 private:
123  template <typename UseT> // UseT == 'Use' or 'const Use'
124  class use_iterator_impl
125  : public std::iterator<std::forward_iterator_tag, UseT *> {
126  friend class Value;
127 
128  UseT *U;
129 
130  explicit use_iterator_impl(UseT *u) : U(u) {}
131 
132  public:
133  use_iterator_impl() : U() {}
134 
135  bool operator==(const use_iterator_impl &x) const { return U == x.U; }
136  bool operator!=(const use_iterator_impl &x) const { return !operator==(x); }
137 
138  use_iterator_impl &operator++() { // Preincrement
139  assert(U && "Cannot increment end iterator!");
140  U = U->getNext();
141  return *this;
142  }
143 
144  use_iterator_impl operator++(int) { // Postincrement
145  auto tmp = *this;
146  ++*this;
147  return tmp;
148  }
149 
150  UseT &operator*() const {
151  assert(U && "Cannot dereference end iterator!");
152  return *U;
153  }
154 
155  UseT *operator->() const { return &operator*(); }
156 
157  operator use_iterator_impl<const UseT>() const {
158  return use_iterator_impl<const UseT>(U);
159  }
160  };
161 
162  template <typename UserTy> // UserTy == 'User' or 'const User'
163  class user_iterator_impl
164  : public std::iterator<std::forward_iterator_tag, UserTy *> {
165  use_iterator_impl<Use> UI;
166  explicit user_iterator_impl(Use *U) : UI(U) {}
167  friend class Value;
168 
169  public:
170  user_iterator_impl() = default;
171 
172  bool operator==(const user_iterator_impl &x) const { return UI == x.UI; }
173  bool operator!=(const user_iterator_impl &x) const { return !operator==(x); }
174 
175  /// Returns true if this iterator is equal to user_end() on the value.
176  bool atEnd() const { return *this == user_iterator_impl(); }
177 
178  user_iterator_impl &operator++() { // Preincrement
179  ++UI;
180  return *this;
181  }
182 
183  user_iterator_impl operator++(int) { // Postincrement
184  auto tmp = *this;
185  ++*this;
186  return tmp;
187  }
188 
189  // Retrieve a pointer to the current User.
190  UserTy *operator*() const {
191  return UI->getUser();
192  }
193 
194  UserTy *operator->() const { return operator*(); }
195 
196  operator user_iterator_impl<const UserTy>() const {
197  return user_iterator_impl<const UserTy>(*UI);
198  }
199 
200  Use &getUse() const { return *UI; }
201  };
202 
203 protected:
204  Value(Type *Ty, unsigned scid);
205 
206  /// Value's destructor should be virtual by design, but that would require
207  /// that Value and all of its subclasses have a vtable that effectively
208  /// duplicates the information in the value ID. As a size optimization, the
209  /// destructor has been protected, and the caller should manually call
210  /// deleteValue.
211  ~Value(); // Use deleteValue() to delete a generic Value.
212 
213 public:
214  Value(const Value &) = delete;
215  Value &operator=(const Value &) = delete;
216 
217  /// Delete a pointer to a generic Value.
218  void deleteValue();
219 
220  /// Support for debugging, callable in GDB: V->dump()
221  void dump() const;
222 
223  /// Implement operator<< on Value.
224  /// @{
225  void print(raw_ostream &O, bool IsForDebug = false) const;
226  void print(raw_ostream &O, ModuleSlotTracker &MST,
227  bool IsForDebug = false) const;
228  /// @}
229 
230  /// Print the name of this Value out to the specified raw_ostream.
231  ///
232  /// This is useful when you just want to print 'int %reg126', not the
233  /// instruction that generated it. If you specify a Module for context, then
234  /// even constanst get pretty-printed; for example, the type of a null
235  /// pointer is printed symbolically.
236  /// @{
237  void printAsOperand(raw_ostream &O, bool PrintType = true,
238  const Module *M = nullptr) const;
239  void printAsOperand(raw_ostream &O, bool PrintType,
240  ModuleSlotTracker &MST) const;
241  /// @}
242 
243  /// All values are typed, get the type of this value.
244  Type *getType() const { return VTy; }
245 
246  /// All values hold a context through their type.
247  LLVMContext &getContext() const;
248 
249  // All values can potentially be named.
250  bool hasName() const { return HasName; }
251  ValueName *getValueName() const;
252  void setValueName(ValueName *VN);
253 
254 private:
255  void destroyValueName();
256  enum class ReplaceMetadataUses { No, Yes };
257  void doRAUW(Value *New, ReplaceMetadataUses);
258  void setNameImpl(const Twine &Name);
259 
260 public:
261  /// Return a constant reference to the value's name.
262  ///
263  /// This guaranteed to return the same reference as long as the value is not
264  /// modified. If the value has a name, this does a hashtable lookup, so it's
265  /// not free.
266  StringRef getName() const;
267 
268  /// Change the name of the value.
269  ///
270  /// Choose a new unique name if the provided name is taken.
271  ///
272  /// \param Name The new name; or "" if the value's name should be removed.
273  void setName(const Twine &Name);
274 
275  /// Transfer the name from V to this value.
276  ///
277  /// After taking V's name, sets V's name to empty.
278  ///
279  /// \note It is an error to call V->takeName(V).
280  void takeName(Value *V);
281 
282  /// Change all uses of this to point to a new Value.
283  ///
284  /// Go through the uses list for this definition and make each use point to
285  /// "V" instead of "this". After this completes, 'this's use list is
286  /// guaranteed to be empty.
287  void replaceAllUsesWith(Value *V);
288 
289  /// Change non-metadata uses of this to point to a new Value.
290  ///
291  /// Go through the uses list for this definition and make each use point to
292  /// "V" instead of "this". This function skips metadata entries in the list.
294 
295  /// replaceUsesOutsideBlock - Go through the uses list for this definition and
296  /// make each use point to "V" instead of "this" when the use is outside the
297  /// block. 'This's use list is expected to have at least one element.
298  /// Unlike replaceAllUsesWith this function does not support basic block
299  /// values or constant users.
301 
302  //----------------------------------------------------------------------
303  // Methods for handling the chain of uses of this Value.
304  //
305  // Materializing a function can introduce new uses, so these methods come in
306  // two variants:
307  // The methods that start with materialized_ check the uses that are
308  // currently known given which functions are materialized. Be very careful
309  // when using them since you might not get all uses.
310  // The methods that don't start with materialized_ assert that modules is
311  // fully materialized.
312  void assertModuleIsMaterializedImpl() const;
313  // This indirection exists so we can keep assertModuleIsMaterializedImpl()
314  // around in release builds of Value.cpp to be linked with other code built
315  // in debug mode. But this avoids calling it in any of the release built code.
317 #ifndef NDEBUG
319 #endif
320  }
321 
322  bool use_empty() const {
324  return UseList == nullptr;
325  }
326 
327  bool materialized_use_empty() const {
328  return UseList == nullptr;
329  }
330 
331  using use_iterator = use_iterator_impl<Use>;
332  using const_use_iterator = use_iterator_impl<const Use>;
333 
336  return const_use_iterator(UseList);
337  }
340  return materialized_use_begin();
341  }
344  return materialized_use_begin();
345  }
350  }
353  }
356  return materialized_uses();
357  }
360  return materialized_uses();
361  }
362 
363  bool user_empty() const {
365  return UseList == nullptr;
366  }
367 
368  using user_iterator = user_iterator_impl<User>;
369  using const_user_iterator = user_iterator_impl<const User>;
370 
373  return const_user_iterator(UseList);
374  }
377  return materialized_user_begin();
378  }
381  return materialized_user_begin();
382  }
387  return *materialized_user_begin();
388  }
389  const User *user_back() const {
391  return *materialized_user_begin();
392  }
395  }
398  }
401  return materialized_users();
402  }
405  return materialized_users();
406  }
407 
408  /// Return true if there is exactly one user of this value.
409  ///
410  /// This is specialized because it is a common request and does not require
411  /// traversing the whole use list.
412  bool hasOneUse() const {
414  if (I == E) return false;
415  return ++I == E;
416  }
417 
418  /// Return true if this Value has exactly N users.
419  bool hasNUses(unsigned N) const;
420 
421  /// Return true if this value has N users or more.
422  ///
423  /// This is logically equivalent to getNumUses() >= N.
424  bool hasNUsesOrMore(unsigned N) const;
425 
426  /// Check if this value is used in the specified basic block.
427  bool isUsedInBasicBlock(const BasicBlock *BB) const;
428 
429  /// This method computes the number of uses of this Value.
430  ///
431  /// This is a linear time operation. Use hasOneUse, hasNUses, or
432  /// hasNUsesOrMore to check for specific values.
433  unsigned getNumUses() const;
434 
435  /// This method should only be used by the Use class.
436  void addUse(Use &U) { U.addToList(&UseList); }
437 
438  /// Concrete subclass of this.
439  ///
440  /// An enumeration for keeping track of the concrete subclass of Value that
441  /// is actually instantiated. Values of this enumeration are kept in the
442  /// Value classes SubclassID field. They are used for concrete type
443  /// identification.
444  enum ValueTy {
445 #define HANDLE_VALUE(Name) Name##Val,
446 #include "llvm/IR/Value.def"
447 
448  // Markers:
449 #define HANDLE_CONSTANT_MARKER(Marker, Constant) Marker = Constant##Val,
450 #include "llvm/IR/Value.def"
451  };
452 
453  /// Return an ID for the concrete type of this object.
454  ///
455  /// This is used to implement the classof checks. This should not be used
456  /// for any other purpose, as the values may change as LLVM evolves. Also,
457  /// note that for instructions, the Instruction's opcode is added to
458  /// InstructionVal. So this means three things:
459  /// # there is no value with code InstructionVal (no opcode==0).
460  /// # there are more possible values for the value type than in ValueTy enum.
461  /// # the InstructionVal enumerator must be the highest valued enumerator in
462  /// the ValueTy enum.
463  unsigned getValueID() const {
464  return SubclassID;
465  }
466 
467  /// Return the raw optional flags value contained in this value.
468  ///
469  /// This should only be used when testing two Values for equivalence.
470  unsigned getRawSubclassOptionalData() const {
471  return SubclassOptionalData;
472  }
473 
474  /// Clear the optional flags contained in this value.
477  }
478 
479  /// Check the optional flags for equality.
480  bool hasSameSubclassOptionalData(const Value *V) const {
482  }
483 
484  /// Return true if there is a value handle associated with this value.
485  bool hasValueHandle() const { return HasValueHandle; }
486 
487  /// Return true if there is metadata referencing this value.
488  bool isUsedByMetadata() const { return IsUsedByMD; }
489 
490  /// Return true if this value is a swifterror value.
491  ///
492  /// swifterror values can be either a function argument or an alloca with a
493  /// swifterror attribute.
494  bool isSwiftError() const;
495 
496  /// Strip off pointer casts, all-zero GEPs, address space casts, and aliases.
497  ///
498  /// Returns the original uncasted value. If this is called on a non-pointer
499  /// value, it returns 'this'.
500  const Value *stripPointerCasts() const;
502  return const_cast<Value *>(
503  static_cast<const Value *>(this)->stripPointerCasts());
504  }
505 
506  /// Strip off pointer casts, all-zero GEPs, address space casts, and aliases
507  /// but ensures the representation of the result stays the same.
508  ///
509  /// Returns the original uncasted value with the same representation. If this
510  /// is called on a non-pointer value, it returns 'this'.
513  return const_cast<Value *>(static_cast<const Value *>(this)
515  }
516 
517  /// Strip off pointer casts, all-zero GEPs, aliases and invariant group
518  /// info.
519  ///
520  /// Returns the original uncasted value. If this is called on a non-pointer
521  /// value, it returns 'this'. This function should be used only in
522  /// Alias analysis.
525  return const_cast<Value *>(
526  static_cast<const Value *>(this)->stripPointerCastsAndInvariantGroups());
527  }
528 
529  /// Strip off pointer casts and all-zero GEPs.
530  ///
531  /// Returns the original uncasted value. If this is called on a non-pointer
532  /// value, it returns 'this'.
535  return const_cast<Value *>(
536  static_cast<const Value *>(this)->stripPointerCastsNoFollowAliases());
537  }
538 
539  /// Strip off pointer casts and all-constant inbounds GEPs.
540  ///
541  /// Returns the original pointer value. If this is called on a non-pointer
542  /// value, it returns 'this'.
543  const Value *stripInBoundsConstantOffsets() const;
545  return const_cast<Value *>(
546  static_cast<const Value *>(this)->stripInBoundsConstantOffsets());
547  }
548 
549  /// Accumulate offsets from \a stripInBoundsConstantOffsets().
550  ///
551  /// Stores the resulting constant offset stripped into the APInt provided.
552  /// The provided APInt will be extended or truncated as needed to be the
553  /// correct bitwidth for an offset of this pointer type.
554  ///
555  /// If this is called on a non-pointer value, it returns 'this'.
557  APInt &Offset) const;
559  APInt &Offset) {
560  return const_cast<Value *>(static_cast<const Value *>(this)
562  }
563 
564  /// Strip off pointer casts and inbounds GEPs.
565  ///
566  /// Returns the original pointer value. If this is called on a non-pointer
567  /// value, it returns 'this'.
568  const Value *stripInBoundsOffsets() const;
570  return const_cast<Value *>(
571  static_cast<const Value *>(this)->stripInBoundsOffsets());
572  }
573 
574  /// Returns the number of bytes known to be dereferenceable for the
575  /// pointer value.
576  ///
577  /// If CanBeNull is set by this function the pointer can either be null or be
578  /// dereferenceable up to the returned number of bytes.
579  uint64_t getPointerDereferenceableBytes(const DataLayout &DL,
580  bool &CanBeNull) const;
581 
582  /// Returns an alignment of the pointer value.
583  ///
584  /// Returns an alignment which is either specified explicitly, e.g. via
585  /// align attribute of a function argument, or guaranteed by DataLayout.
586  unsigned getPointerAlignment(const DataLayout &DL) const;
587 
588  /// Translate PHI node to its predecessor from the given basic block.
589  ///
590  /// If this value is a PHI node with CurBB as its parent, return the value in
591  /// the PHI node corresponding to PredBB. If not, return ourself. This is
592  /// useful if you want to know the value something has in a predecessor
593  /// block.
594  const Value *DoPHITranslation(const BasicBlock *CurBB,
595  const BasicBlock *PredBB) const;
596  Value *DoPHITranslation(const BasicBlock *CurBB, const BasicBlock *PredBB) {
597  return const_cast<Value *>(
598  static_cast<const Value *>(this)->DoPHITranslation(CurBB, PredBB));
599  }
600 
601  /// The maximum alignment for instructions.
602  ///
603  /// This is the greatest alignment value supported by load, store, and alloca
604  /// instructions, and global values.
605  static const unsigned MaxAlignmentExponent = 29;
606  static const unsigned MaximumAlignment = 1u << MaxAlignmentExponent;
607 
608  /// Mutate the type of this Value to be of the specified type.
609  ///
610  /// Note that this is an extremely dangerous operation which can create
611  /// completely invalid IR very easily. It is strongly recommended that you
612  /// recreate IR objects with the right types instead of mutating them in
613  /// place.
614  void mutateType(Type *Ty) {
615  VTy = Ty;
616  }
617 
618  /// Sort the use-list.
619  ///
620  /// Sorts the Value's use-list by Cmp using a stable mergesort. Cmp is
621  /// expected to compare two \a Use references.
622  template <class Compare> void sortUseList(Compare Cmp);
623 
624  /// Reverse the use-list.
625  void reverseUseList();
626 
627 private:
628  /// Merge two lists together.
629  ///
630  /// Merges \c L and \c R using \c Cmp. To enable stable sorts, always pushes
631  /// "equal" items from L before items from R.
632  ///
633  /// \return the first element in the list.
634  ///
635  /// \note Completely ignores \a Use::Prev (doesn't read, doesn't update).
636  template <class Compare>
637  static Use *mergeUseLists(Use *L, Use *R, Compare Cmp) {
638  Use *Merged;
639  Use **Next = &Merged;
640 
641  while (true) {
642  if (!L) {
643  *Next = R;
644  break;
645  }
646  if (!R) {
647  *Next = L;
648  break;
649  }
650  if (Cmp(*R, *L)) {
651  *Next = R;
652  Next = &R->Next;
653  R = R->Next;
654  } else {
655  *Next = L;
656  Next = &L->Next;
657  L = L->Next;
658  }
659  }
660 
661  return Merged;
662  }
663 
664 protected:
665  unsigned short getSubclassDataFromValue() const { return SubclassData; }
666  void setValueSubclassData(unsigned short D) { SubclassData = D; }
667 };
668 
669 struct ValueDeleter { void operator()(Value *V) { V->deleteValue(); } };
670 
671 /// Use this instead of std::unique_ptr<Value> or std::unique_ptr<Instruction>.
672 /// Those don't work because Value and Instruction's destructors are protected,
673 /// aren't virtual, and won't destroy the complete object.
674 using unique_value = std::unique_ptr<Value, ValueDeleter>;
675 
676 inline raw_ostream &operator<<(raw_ostream &OS, const Value &V) {
677  V.print(OS);
678  return OS;
679 }
680 
681 void Use::set(Value *V) {
682  if (Val) removeFromList();
683  Val = V;
684  if (V) V->addUse(*this);
685 }
686 
688  set(RHS);
689  return RHS;
690 }
691 
692 const Use &Use::operator=(const Use &RHS) {
693  set(RHS.Val);
694  return *this;
695 }
696 
697 template <class Compare> void Value::sortUseList(Compare Cmp) {
698  if (!UseList || !UseList->Next)
699  // No need to sort 0 or 1 uses.
700  return;
701 
702  // Note: this function completely ignores Prev pointers until the end when
703  // they're fixed en masse.
704 
705  // Create a binomial vector of sorted lists, visiting uses one at a time and
706  // merging lists as necessary.
707  const unsigned MaxSlots = 32;
708  Use *Slots[MaxSlots];
709 
710  // Collect the first use, turning it into a single-item list.
711  Use *Next = UseList->Next;
712  UseList->Next = nullptr;
713  unsigned NumSlots = 1;
714  Slots[0] = UseList;
715 
716  // Collect all but the last use.
717  while (Next->Next) {
718  Use *Current = Next;
719  Next = Current->Next;
720 
721  // Turn Current into a single-item list.
722  Current->Next = nullptr;
723 
724  // Save Current in the first available slot, merging on collisions.
725  unsigned I;
726  for (I = 0; I < NumSlots; ++I) {
727  if (!Slots[I])
728  break;
729 
730  // Merge two lists, doubling the size of Current and emptying slot I.
731  //
732  // Since the uses in Slots[I] originally preceded those in Current, send
733  // Slots[I] in as the left parameter to maintain a stable sort.
734  Current = mergeUseLists(Slots[I], Current, Cmp);
735  Slots[I] = nullptr;
736  }
737  // Check if this is a new slot.
738  if (I == NumSlots) {
739  ++NumSlots;
740  assert(NumSlots <= MaxSlots && "Use list bigger than 2^32");
741  }
742 
743  // Found an open slot.
744  Slots[I] = Current;
745  }
746 
747  // Merge all the lists together.
748  assert(Next && "Expected one more Use");
749  assert(!Next->Next && "Expected only one Use");
750  UseList = Next;
751  for (unsigned I = 0; I < NumSlots; ++I)
752  if (Slots[I])
753  // Since the uses in Slots[I] originally preceded those in UseList, send
754  // Slots[I] in as the left parameter to maintain a stable sort.
755  UseList = mergeUseLists(Slots[I], UseList, Cmp);
756 
757  // Fix the Prev pointers.
758  for (Use *I = UseList, **Prev = &UseList; I; I = I->Next) {
759  I->setPrev(Prev);
760  Prev = &I->Next;
761  }
762 }
763 
764 // isa - Provide some specializations of isa so that we don't have to include
765 // the subtype header files to test to see if the value is a subclass...
766 //
767 template <> struct isa_impl<Constant, Value> {
768  static inline bool doit(const Value &Val) {
769  static_assert(Value::ConstantFirstVal == 0, "Val.getValueID() >= Value::ConstantFirstVal");
770  return Val.getValueID() <= Value::ConstantLastVal;
771  }
772 };
773 
774 template <> struct isa_impl<ConstantData, Value> {
775  static inline bool doit(const Value &Val) {
776  return Val.getValueID() >= Value::ConstantDataFirstVal &&
777  Val.getValueID() <= Value::ConstantDataLastVal;
778  }
779 };
780 
781 template <> struct isa_impl<ConstantAggregate, Value> {
782  static inline bool doit(const Value &Val) {
783  return Val.getValueID() >= Value::ConstantAggregateFirstVal &&
784  Val.getValueID() <= Value::ConstantAggregateLastVal;
785  }
786 };
787 
788 template <> struct isa_impl<Argument, Value> {
789  static inline bool doit (const Value &Val) {
790  return Val.getValueID() == Value::ArgumentVal;
791  }
792 };
793 
794 template <> struct isa_impl<InlineAsm, Value> {
795  static inline bool doit(const Value &Val) {
796  return Val.getValueID() == Value::InlineAsmVal;
797  }
798 };
799 
800 template <> struct isa_impl<Instruction, Value> {
801  static inline bool doit(const Value &Val) {
802  return Val.getValueID() >= Value::InstructionVal;
803  }
804 };
805 
806 template <> struct isa_impl<BasicBlock, Value> {
807  static inline bool doit(const Value &Val) {
808  return Val.getValueID() == Value::BasicBlockVal;
809  }
810 };
811 
812 template <> struct isa_impl<Function, Value> {
813  static inline bool doit(const Value &Val) {
814  return Val.getValueID() == Value::FunctionVal;
815  }
816 };
817 
818 template <> struct isa_impl<GlobalVariable, Value> {
819  static inline bool doit(const Value &Val) {
820  return Val.getValueID() == Value::GlobalVariableVal;
821  }
822 };
823 
824 template <> struct isa_impl<GlobalAlias, Value> {
825  static inline bool doit(const Value &Val) {
826  return Val.getValueID() == Value::GlobalAliasVal;
827  }
828 };
829 
830 template <> struct isa_impl<GlobalIFunc, Value> {
831  static inline bool doit(const Value &Val) {
832  return Val.getValueID() == Value::GlobalIFuncVal;
833  }
834 };
835 
836 template <> struct isa_impl<GlobalIndirectSymbol, Value> {
837  static inline bool doit(const Value &Val) {
838  return isa<GlobalAlias>(Val) || isa<GlobalIFunc>(Val);
839  }
840 };
841 
842 template <> struct isa_impl<GlobalValue, Value> {
843  static inline bool doit(const Value &Val) {
844  return isa<GlobalObject>(Val) || isa<GlobalIndirectSymbol>(Val);
845  }
846 };
847 
848 template <> struct isa_impl<GlobalObject, Value> {
849  static inline bool doit(const Value &Val) {
850  return isa<GlobalVariable>(Val) || isa<Function>(Val);
851  }
852 };
853 
854 // Create wrappers for C Binding types (see CBindingWrapping.h).
856 
857 // Specialized opaque value conversions.
859  return reinterpret_cast<Value**>(Vals);
860 }
861 
862 template<typename T>
863 inline T **unwrap(LLVMValueRef *Vals, unsigned Length) {
864 #ifndef NDEBUG
865  for (LLVMValueRef *I = Vals, *E = Vals + Length; I != E; ++I)
866  unwrap<T>(*I); // For side effect of calling assert on invalid usage.
867 #endif
868  (void)Length;
869  return reinterpret_cast<T**>(Vals);
870 }
871 
872 inline LLVMValueRef *wrap(const Value **Vals) {
873  return reinterpret_cast<LLVMValueRef*>(const_cast<Value**>(Vals));
874 }
875 
876 } // end namespace llvm
877 
878 #endif // LLVM_IR_VALUE_H
This is the common base class of value handles.
Definition: ValueHandle.h:29
unsigned short getSubclassDataFromValue() const
Definition: Value.h:665
void sortUseList(Compare Cmp)
Sort the use-list.
Definition: Value.h:697
use_iterator use_end()
Definition: Value.h:346
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:110
Type
MessagePack types as defined in the standard, with the exception of Integer being divided into a sign...
Definition: MsgPackReader.h:48
unsigned HasDescriptor
Definition: Value.h:120
iterator_range< use_iterator > uses()
Definition: Value.h:354
void operator()(Value *V)
Definition: Value.h:669
This class represents an incoming formal argument to a Function.
Definition: Argument.h:29
unsigned getValueID() const
Return an ID for the concrete type of this object.
Definition: Value.h:463
const Value * stripInBoundsOffsets() const
Strip off pointer casts and inbounds GEPs.
Definition: Value.cpp:600
static bool doit(const Value &Val)
Definition: Value.h:782
This class represents lattice values for constants.
Definition: AllocatorList.h:23
void addUse(Use &U)
This method should only be used by the Use class.
Definition: Value.h:436
StringMapEntry - This is used to represent one value that is inserted into a StringMap.
Definition: StringMap.h:125
Various leaf nodes.
Definition: ISDOpcodes.h:59
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:65
bool user_empty() const
Definition: Value.h:363
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
This provides a very simple, boring adaptor for a begin and end iterator into a range type...
use_iterator materialized_use_begin()
Definition: Value.h:334
bool hasValueHandle() const
Return true if there is a value handle associated with this value.
Definition: Value.h:485
iterator_range< const_user_iterator > users() const
Definition: Value.h:403
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:720
static bool doit(const Value &Val)
Definition: Value.h:819
void deleteValue()
Delete a pointer to a generic Value.
Definition: Value.cpp:98
Manage lifetime of a slot tracker for printing IR.
This defines the Use class.
use_iterator_impl< const Use > const_use_iterator
Definition: Value.h:332
const Value * DoPHITranslation(const BasicBlock *CurBB, const BasicBlock *PredBB) const
Translate PHI node to its predecessor from the given basic block.
Definition: Value.cpp:712
unsigned getPointerAlignment(const DataLayout &DL) const
Returns an alignment of the pointer value.
Definition: Value.cpp:657
iterator_range< user_iterator > materialized_users()
Definition: Value.h:393
void dump() const
Support for debugging, callable in GDB: V->dump()
Definition: AsmWriter.cpp:4382
const_user_iterator materialized_user_begin() const
Definition: Value.h:372
iterator_range< const_user_iterator > materialized_users() const
Definition: Value.h:396
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:80
Attribute unwrap(LLVMAttributeRef Attr)
Definition: Attributes.h:203
A Use represents the edge between a Value definition and its users.
Definition: Use.h:55
void setName(const Twine &Name)
Change the name of the value.
Definition: Value.cpp:285
void assertModuleIsMaterializedImpl() const
Definition: Value.cpp:351
static bool doit(const Value &Val)
Definition: Value.h:807
static const unsigned MaximumAlignment
Definition: Value.h:606
#define DEFINE_ISA_CONVERSION_FUNCTIONS(ty, ref)
APInt operator*(APInt a, uint64_t RHS)
Definition: APInt.h:2090
static bool doit(const Value &Val)
Definition: Value.h:795
Value * stripInBoundsOffsets()
Definition: Value.h:569
unsigned HasHungOffUses
Definition: Value.h:119
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:244
iterator_range< const_use_iterator > materialized_uses() const
Definition: Value.h:351
bool isSwiftError() const
Return true if this value is a swifterror value.
Definition: Value.cpp:741
static bool doit(const Value &Val)
Definition: Value.h:837
const_use_iterator use_end() const
Definition: Value.h:347
const_use_iterator materialized_use_begin() const
Definition: Value.h:335
Value * stripPointerCastsNoFollowAliases()
Definition: Value.h:534
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:429
Value wrapper in the Metadata hierarchy.
Definition: Metadata.h:338
void takeName(Value *V)
Transfer the name from V to this value.
Definition: Value.cpp:291
use_iterator_impl< Use > use_iterator
Definition: Value.h:331
unsigned getRawSubclassOptionalData() const
Return the raw optional flags value contained in this value.
Definition: Value.h:470
const_use_iterator use_begin() const
Definition: Value.h:342
bool hasNUsesOrMore(unsigned N) const
Return true if this value has N users or more.
Definition: Value.cpp:135
bool hasNUses(unsigned N) const
Return true if this Value has exactly N users.
Definition: Value.cpp:131
void set(Value *Val)
Definition: Value.h:681
bool hasName() const
Definition: Value.h:250
LLVM Basic Block Representation.
Definition: BasicBlock.h:57
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:64
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This is an important base class in LLVM.
Definition: Constant.h:41
void replaceNonMetadataUsesWith(Value *V)
Change non-metadata uses of this to point to a new Value.
Definition: Value.cpp:433
amdgpu inline
iterator_range< use_iterator > materialized_uses()
Definition: Value.h:348
bool isUsedInBasicBlock(const BasicBlock *BB) const
Check if this value is used in the specified basic block.
Definition: Value.cpp:139
bool hasSameSubclassOptionalData(const Value *V) const
Check the optional flags for equality.
Definition: Value.h:480
static bool doit(const Value &Val)
Definition: Value.h:831
Value * stripPointerCastsAndInvariantGroups()
Definition: Value.h:524
void print(raw_ostream &O, bool IsForDebug=false) const
Implement operator<< on Value.
Definition: AsmWriter.cpp:4232
Value * stripInBoundsConstantOffsets()
Definition: Value.h:544
const Value * stripPointerCastsSameRepresentation() const
Strip off pointer casts, all-zero GEPs, address space casts, and aliases but ensures the representati...
Definition: Value.cpp:539
const Value * stripPointerCasts() const
Strip off pointer casts, all-zero GEPs, address space casts, and aliases.
Definition: Value.cpp:535
Value(Type *Ty, unsigned scid)
Definition: Value.cpp:52
void printAsOperand(raw_ostream &O, bool PrintType=true, const Module *M=nullptr) const
Print the name of this Value out to the specified raw_ostream.
Definition: AsmWriter.cpp:4309
Value * stripPointerCasts()
Definition: Value.h:501
Value & operator=(const Value &)=delete
unsigned char SubclassOptionalData
Hold subclass data that can be dropped.
Definition: Value.h:90
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
static const unsigned MaxAlignmentExponent
The maximum alignment for instructions.
Definition: Value.h:605
bool materialized_use_empty() const
Definition: Value.h:327
unsigned IsUsedByMD
Definition: Value.h:117
void assertModuleIsMaterialized() const
Definition: Value.h:316
static bool doit(const Value &Val)
Definition: Value.h:849
ValueName * getValueName() const
Definition: Value.cpp:186
const User * user_back() const
Definition: Value.h:389
std::unique_ptr< Value, ValueDeleter > unique_value
Use this instead of std::unique_ptr<Value> or std::unique_ptr<Instruction>.
Definition: Value.h:674
uint64_t getPointerDereferenceableBytes(const DataLayout &DL, bool &CanBeNull) const
Returns the number of bytes known to be dereferenceable for the pointer value.
Definition: Value.cpp:604
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
unsigned NumUserOperands
Definition: Value.h:114
const Value * stripPointerCastsAndInvariantGroups() const
Strip off pointer casts, all-zero GEPs, aliases and invariant group info.
Definition: Value.cpp:552
ValueTy
Concrete subclass of this.
Definition: Value.h:444
void setValueSubclassData(unsigned short D)
Definition: Value.h:666
bool isUsedByMetadata() const
Return true if there is metadata referencing this value.
Definition: Value.h:488
A range adaptor for a pair of iterators.
const Value * stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL, APInt &Offset) const
Accumulate offsets from stripInBoundsConstantOffsets().
Definition: Value.cpp:558
Class for arbitrary precision integers.
Definition: APInt.h:69
const_user_iterator user_begin() const
Definition: Value.h:379
iterator_range< user_iterator > users()
Definition: Value.h:399
user_iterator_impl< const User > const_user_iterator
Definition: Value.h:369
could "use" a pointer
void reverseUseList()
Reverse the use-list.
Definition: Value.cpp:722
iterator_range< const_use_iterator > uses() const
Definition: Value.h:358
use_iterator use_begin()
Definition: Value.h:338
bool operator!=(uint64_t V1, const APInt &V2)
Definition: APInt.h:1968
unsigned getNumUses() const
This method computes the number of uses of this Value.
Definition: Value.cpp:160
unsigned HasName
Definition: Value.h:118
LLVMAttributeRef wrap(Attribute Attr)
Definition: Attributes.h:198
static bool doit(const Value &Val)
Definition: Value.h:801
const Value * stripPointerCastsNoFollowAliases() const
Strip off pointer casts and all-zero GEPs.
Definition: Value.cpp:544
StringRef getName() const
Return a constant reference to the value&#39;s name.
Definition: Value.cpp:214
#define I(x, y, z)
Definition: MD5.cpp:58
#define N
user_iterator_impl< User > user_iterator
Definition: Value.h:368
Value * DoPHITranslation(const BasicBlock *CurBB, const BasicBlock *PredBB)
Definition: Value.h:596
static bool doit(const Value &Val)
Definition: Value.h:768
raw_ostream & operator<<(raw_ostream &OS, const APInt &I)
Definition: APInt.h:2038
void mutateType(Type *Ty)
Mutate the type of this Value to be of the specified type.
Definition: Value.h:614
~Value()
Value&#39;s destructor should be virtual by design, but that would require that Value and all of its subc...
Definition: Value.cpp:72
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Value * stripPointerCastsSameRepresentation()
Definition: Value.h:512
user_iterator user_begin()
Definition: Value.h:375
Value * stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL, APInt &Offset)
Definition: Value.h:558
Base class for aggregate constants (with operands).
Definition: Constants.h:389
LLVM Value Representation.
Definition: Value.h:72
const_user_iterator user_end() const
Definition: Value.h:384
void clearSubclassOptionalData()
Clear the optional flags contained in this value.
Definition: Value.h:475
static bool doit(const Value &Val)
Definition: Value.h:789
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
user_iterator materialized_user_begin()
Definition: Value.h:371
void setValueName(ValueName *VN)
Definition: Value.cpp:197
static bool doit(const Value &Val)
Definition: Value.h:843
bool hasOneUse() const
Return true if there is exactly one user of this value.
Definition: Value.h:412
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
static bool doit(const Value &Val)
Definition: Value.h:825
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:439
bool operator==(uint64_t V1, const APInt &V2)
Definition: APInt.h:1966
const Value * stripInBoundsConstantOffsets() const
Strip off pointer casts and all-constant inbounds GEPs.
Definition: Value.cpp:548
Base class for constants with no operands.
Definition: Constants.h:57
Value * operator=(Value *RHS)
Definition: Value.h:687
bool use_empty() const
Definition: Value.h:322
static bool doit(const Value &Val)
Definition: Value.h:813
struct LLVMOpaqueValue * LLVMValueRef
Represents an individual value in LLVM IR.
Definition: Types.h:76
User * user_back()
Definition: Value.h:385
static bool doit(const Value &Val)
Definition: Value.h:775
user_iterator user_end()
Definition: Value.h:383