Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:10

0001 //===- llvm/Value.h - Definition of the Value class -------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file declares the Value class.
0010 //
0011 //===----------------------------------------------------------------------===//
0012 
0013 #ifndef LLVM_IR_VALUE_H
0014 #define LLVM_IR_VALUE_H
0015 
0016 #include "llvm-c/Types.h"
0017 #include "llvm/ADT/STLExtras.h"
0018 #include "llvm/ADT/StringRef.h"
0019 #include "llvm/ADT/iterator_range.h"
0020 #include "llvm/IR/Use.h"
0021 #include "llvm/Support/Alignment.h"
0022 #include "llvm/Support/CBindingWrapping.h"
0023 #include "llvm/Support/Casting.h"
0024 #include <cassert>
0025 #include <iterator>
0026 #include <memory>
0027 
0028 namespace llvm {
0029 
0030 class APInt;
0031 class Argument;
0032 class BasicBlock;
0033 class Constant;
0034 class ConstantData;
0035 class ConstantAggregate;
0036 class DataLayout;
0037 class Function;
0038 class GlobalAlias;
0039 class GlobalIFunc;
0040 class GlobalObject;
0041 class GlobalValue;
0042 class GlobalVariable;
0043 class InlineAsm;
0044 class Instruction;
0045 class LLVMContext;
0046 class MDNode;
0047 class Module;
0048 class ModuleSlotTracker;
0049 class raw_ostream;
0050 template<typename ValueTy> class StringMapEntry;
0051 class Twine;
0052 class Type;
0053 class User;
0054 
0055 using ValueName = StringMapEntry<Value *>;
0056 
0057 //===----------------------------------------------------------------------===//
0058 //                                 Value Class
0059 //===----------------------------------------------------------------------===//
0060 
0061 /// LLVM Value Representation
0062 ///
0063 /// This is a very important LLVM class. It is the base class of all values
0064 /// computed by a program that may be used as operands to other values. Value is
0065 /// the super class of other important classes such as Instruction and Function.
0066 /// All Values have a Type. Type is not a subclass of Value. Some values can
0067 /// have a name and they belong to some Module.  Setting the name on the Value
0068 /// automatically updates the module's symbol table.
0069 ///
0070 /// Every value has a "use list" that keeps track of which other Values are
0071 /// using this Value.  A Value can also have an arbitrary number of ValueHandle
0072 /// objects that watch it and listen to RAUW and Destroy events.  See
0073 /// llvm/IR/ValueHandle.h for details.
0074 class Value {
0075   const unsigned char SubclassID;   // Subclass identifier (for isa/dyn_cast)
0076   unsigned char HasValueHandle : 1; // Has a ValueHandle pointing to this?
0077 
0078 protected:
0079   /// Hold subclass data that can be dropped.
0080   ///
0081   /// This member is similar to SubclassData, however it is for holding
0082   /// information which may be used to aid optimization, but which may be
0083   /// cleared to zero without affecting conservative interpretation.
0084   unsigned char SubclassOptionalData : 7;
0085 
0086 private:
0087   /// Hold arbitrary subclass data.
0088   ///
0089   /// This member is defined by this class, but is not used for anything.
0090   /// Subclasses can use it to hold whatever state they find useful.  This
0091   /// field is initialized to zero by the ctor.
0092   unsigned short SubclassData;
0093 
0094 protected:
0095   /// The number of operands in the subclass.
0096   ///
0097   /// This member is defined by this class, but not used for anything.
0098   /// Subclasses can use it to store their number of operands, if they have
0099   /// any.
0100   ///
0101   /// This is stored here to save space in User on 64-bit hosts.  Since most
0102   /// instances of Value have operands, 32-bit hosts aren't significantly
0103   /// affected.
0104   ///
0105   /// Note, this should *NOT* be used directly by any class other than User.
0106   /// User uses this value to find the Use list.
0107   enum : unsigned { NumUserOperandsBits = 27 };
0108   unsigned NumUserOperands : NumUserOperandsBits;
0109 
0110   // Use the same type as the bitfield above so that MSVC will pack them.
0111   unsigned IsUsedByMD : 1;
0112   unsigned HasName : 1;
0113   unsigned HasMetadata : 1; // Has metadata attached to this?
0114   unsigned HasHungOffUses : 1;
0115   unsigned HasDescriptor : 1;
0116 
0117 private:
0118   Type *VTy;
0119   Use *UseList;
0120 
0121   friend class ValueAsMetadata; // Allow access to IsUsedByMD.
0122   friend class ValueHandleBase; // Allow access to HasValueHandle.
0123 
0124   template <typename UseT> // UseT == 'Use' or 'const Use'
0125   class use_iterator_impl {
0126     friend class Value;
0127 
0128     UseT *U;
0129 
0130     explicit use_iterator_impl(UseT *u) : U(u) {}
0131 
0132   public:
0133     using iterator_category = std::forward_iterator_tag;
0134     using value_type = UseT;
0135     using difference_type = std::ptrdiff_t;
0136     using pointer = value_type *;
0137     using reference = value_type &;
0138 
0139     use_iterator_impl() : U() {}
0140 
0141     bool operator==(const use_iterator_impl &x) const { return U == x.U; }
0142     bool operator!=(const use_iterator_impl &x) const { return !operator==(x); }
0143 
0144     use_iterator_impl &operator++() { // Preincrement
0145       assert(U && "Cannot increment end iterator!");
0146       U = U->getNext();
0147       return *this;
0148     }
0149 
0150     use_iterator_impl operator++(int) { // Postincrement
0151       auto tmp = *this;
0152       ++*this;
0153       return tmp;
0154     }
0155 
0156     UseT &operator*() const {
0157       assert(U && "Cannot dereference end iterator!");
0158       return *U;
0159     }
0160 
0161     UseT *operator->() const { return &operator*(); }
0162 
0163     operator use_iterator_impl<const UseT>() const {
0164       return use_iterator_impl<const UseT>(U);
0165     }
0166   };
0167 
0168   template <typename UserTy> // UserTy == 'User' or 'const User'
0169   class user_iterator_impl {
0170     use_iterator_impl<Use> UI;
0171     explicit user_iterator_impl(Use *U) : UI(U) {}
0172     friend class Value;
0173 
0174   public:
0175     using iterator_category = std::forward_iterator_tag;
0176     using value_type = UserTy *;
0177     using difference_type = std::ptrdiff_t;
0178     using pointer = value_type *;
0179     using reference = value_type &;
0180 
0181     user_iterator_impl() = default;
0182 
0183     bool operator==(const user_iterator_impl &x) const { return UI == x.UI; }
0184     bool operator!=(const user_iterator_impl &x) const { return !operator==(x); }
0185 
0186     /// Returns true if this iterator is equal to user_end() on the value.
0187     bool atEnd() const { return *this == user_iterator_impl(); }
0188 
0189     user_iterator_impl &operator++() { // Preincrement
0190       ++UI;
0191       return *this;
0192     }
0193 
0194     user_iterator_impl operator++(int) { // Postincrement
0195       auto tmp = *this;
0196       ++*this;
0197       return tmp;
0198     }
0199 
0200     // Retrieve a pointer to the current User.
0201     UserTy *operator*() const {
0202       return UI->getUser();
0203     }
0204 
0205     UserTy *operator->() const { return operator*(); }
0206 
0207     operator user_iterator_impl<const UserTy>() const {
0208       return user_iterator_impl<const UserTy>(*UI);
0209     }
0210 
0211     Use &getUse() const { return *UI; }
0212   };
0213 
0214 protected:
0215   Value(Type *Ty, unsigned scid);
0216 
0217   /// Value's destructor should be virtual by design, but that would require
0218   /// that Value and all of its subclasses have a vtable that effectively
0219   /// duplicates the information in the value ID. As a size optimization, the
0220   /// destructor has been protected, and the caller should manually call
0221   /// deleteValue.
0222   ~Value(); // Use deleteValue() to delete a generic Value.
0223 
0224 public:
0225   Value(const Value &) = delete;
0226   Value &operator=(const Value &) = delete;
0227 
0228   /// Delete a pointer to a generic Value.
0229   void deleteValue();
0230 
0231   /// Support for debugging, callable in GDB: V->dump()
0232   void dump() const;
0233 
0234   /// Implement operator<< on Value.
0235   /// @{
0236   void print(raw_ostream &O, bool IsForDebug = false) const;
0237   void print(raw_ostream &O, ModuleSlotTracker &MST,
0238              bool IsForDebug = false) const;
0239   /// @}
0240 
0241   /// Print the name of this Value out to the specified raw_ostream.
0242   ///
0243   /// This is useful when you just want to print 'int %reg126', not the
0244   /// instruction that generated it. If you specify a Module for context, then
0245   /// even constants get pretty-printed; for example, the type of a null
0246   /// pointer is printed symbolically.
0247   /// @{
0248   void printAsOperand(raw_ostream &O, bool PrintType = true,
0249                       const Module *M = nullptr) const;
0250   void printAsOperand(raw_ostream &O, bool PrintType,
0251                       ModuleSlotTracker &MST) const;
0252   /// @}
0253 
0254   /// All values are typed, get the type of this value.
0255   Type *getType() const { return VTy; }
0256 
0257   /// All values hold a context through their type.
0258   LLVMContext &getContext() const;
0259 
0260   // All values can potentially be named.
0261   bool hasName() const { return HasName; }
0262   ValueName *getValueName() const;
0263   void setValueName(ValueName *VN);
0264 
0265 private:
0266   void destroyValueName();
0267   enum class ReplaceMetadataUses { No, Yes };
0268   void doRAUW(Value *New, ReplaceMetadataUses);
0269   void setNameImpl(const Twine &Name);
0270 
0271 public:
0272   /// Return a constant reference to the value's name.
0273   ///
0274   /// This guaranteed to return the same reference as long as the value is not
0275   /// modified.  If the value has a name, this does a hashtable lookup, so it's
0276   /// not free.
0277   StringRef getName() const;
0278 
0279   /// Change the name of the value.
0280   ///
0281   /// Choose a new unique name if the provided name is taken.
0282   ///
0283   /// \param Name The new name; or "" if the value's name should be removed.
0284   void setName(const Twine &Name);
0285 
0286   /// Transfer the name from V to this value.
0287   ///
0288   /// After taking V's name, sets V's name to empty.
0289   ///
0290   /// \note It is an error to call V->takeName(V).
0291   void takeName(Value *V);
0292 
0293 #ifndef NDEBUG
0294   std::string getNameOrAsOperand() const;
0295 #endif
0296 
0297   /// Change all uses of this to point to a new Value.
0298   ///
0299   /// Go through the uses list for this definition and make each use point to
0300   /// "V" instead of "this".  After this completes, 'this's use list is
0301   /// guaranteed to be empty.
0302   void replaceAllUsesWith(Value *V);
0303 
0304   /// Change non-metadata uses of this to point to a new Value.
0305   ///
0306   /// Go through the uses list for this definition and make each use point to
0307   /// "V" instead of "this". This function skips metadata entries in the list.
0308   void replaceNonMetadataUsesWith(Value *V);
0309 
0310   /// Go through the uses list for this definition and make each use point
0311   /// to "V" if the callback ShouldReplace returns true for the given Use.
0312   /// Unlike replaceAllUsesWith() this function does not support basic block
0313   /// values.
0314   void replaceUsesWithIf(Value *New,
0315                          llvm::function_ref<bool(Use &U)> ShouldReplace);
0316 
0317   /// replaceUsesOutsideBlock - Go through the uses list for this definition and
0318   /// make each use point to "V" instead of "this" when the use is outside the
0319   /// block. 'This's use list is expected to have at least one element.
0320   /// Unlike replaceAllUsesWith() this function does not support basic block
0321   /// values.
0322   void replaceUsesOutsideBlock(Value *V, BasicBlock *BB);
0323 
0324   //----------------------------------------------------------------------
0325   // Methods for handling the chain of uses of this Value.
0326   //
0327   // Materializing a function can introduce new uses, so these methods come in
0328   // two variants:
0329   // The methods that start with materialized_ check the uses that are
0330   // currently known given which functions are materialized. Be very careful
0331   // when using them since you might not get all uses.
0332   // The methods that don't start with materialized_ assert that modules is
0333   // fully materialized.
0334   void assertModuleIsMaterializedImpl() const;
0335   // This indirection exists so we can keep assertModuleIsMaterializedImpl()
0336   // around in release builds of Value.cpp to be linked with other code built
0337   // in debug mode. But this avoids calling it in any of the release built code.
0338   void assertModuleIsMaterialized() const {
0339 #ifndef NDEBUG
0340     assertModuleIsMaterializedImpl();
0341 #endif
0342   }
0343 
0344   bool use_empty() const {
0345     assertModuleIsMaterialized();
0346     return UseList == nullptr;
0347   }
0348 
0349   bool materialized_use_empty() const {
0350     return UseList == nullptr;
0351   }
0352 
0353   using use_iterator = use_iterator_impl<Use>;
0354   using const_use_iterator = use_iterator_impl<const Use>;
0355 
0356   use_iterator materialized_use_begin() { return use_iterator(UseList); }
0357   const_use_iterator materialized_use_begin() const {
0358     return const_use_iterator(UseList);
0359   }
0360   use_iterator use_begin() {
0361     assertModuleIsMaterialized();
0362     return materialized_use_begin();
0363   }
0364   const_use_iterator use_begin() const {
0365     assertModuleIsMaterialized();
0366     return materialized_use_begin();
0367   }
0368   use_iterator use_end() { return use_iterator(); }
0369   const_use_iterator use_end() const { return const_use_iterator(); }
0370   iterator_range<use_iterator> materialized_uses() {
0371     return make_range(materialized_use_begin(), use_end());
0372   }
0373   iterator_range<const_use_iterator> materialized_uses() const {
0374     return make_range(materialized_use_begin(), use_end());
0375   }
0376   iterator_range<use_iterator> uses() {
0377     assertModuleIsMaterialized();
0378     return materialized_uses();
0379   }
0380   iterator_range<const_use_iterator> uses() const {
0381     assertModuleIsMaterialized();
0382     return materialized_uses();
0383   }
0384 
0385   bool user_empty() const {
0386     assertModuleIsMaterialized();
0387     return UseList == nullptr;
0388   }
0389 
0390   using user_iterator = user_iterator_impl<User>;
0391   using const_user_iterator = user_iterator_impl<const User>;
0392 
0393   user_iterator materialized_user_begin() { return user_iterator(UseList); }
0394   const_user_iterator materialized_user_begin() const {
0395     return const_user_iterator(UseList);
0396   }
0397   user_iterator user_begin() {
0398     assertModuleIsMaterialized();
0399     return materialized_user_begin();
0400   }
0401   const_user_iterator user_begin() const {
0402     assertModuleIsMaterialized();
0403     return materialized_user_begin();
0404   }
0405   user_iterator user_end() { return user_iterator(); }
0406   const_user_iterator user_end() const { return const_user_iterator(); }
0407   User *user_back() {
0408     assertModuleIsMaterialized();
0409     return *materialized_user_begin();
0410   }
0411   const User *user_back() const {
0412     assertModuleIsMaterialized();
0413     return *materialized_user_begin();
0414   }
0415   iterator_range<user_iterator> materialized_users() {
0416     return make_range(materialized_user_begin(), user_end());
0417   }
0418   iterator_range<const_user_iterator> materialized_users() const {
0419     return make_range(materialized_user_begin(), user_end());
0420   }
0421   iterator_range<user_iterator> users() {
0422     assertModuleIsMaterialized();
0423     return materialized_users();
0424   }
0425   iterator_range<const_user_iterator> users() const {
0426     assertModuleIsMaterialized();
0427     return materialized_users();
0428   }
0429 
0430   /// Return true if there is exactly one use of this value.
0431   ///
0432   /// This is specialized because it is a common request and does not require
0433   /// traversing the whole use list.
0434   bool hasOneUse() const { return hasSingleElement(uses()); }
0435 
0436   /// Return true if this Value has exactly N uses.
0437   bool hasNUses(unsigned N) const;
0438 
0439   /// Return true if this value has N uses or more.
0440   ///
0441   /// This is logically equivalent to getNumUses() >= N.
0442   bool hasNUsesOrMore(unsigned N) const;
0443 
0444   /// Return true if there is exactly one user of this value.
0445   ///
0446   /// Note that this is not the same as "has one use". If a value has one use,
0447   /// then there certainly is a single user. But if value has several uses,
0448   /// it is possible that all uses are in a single user, or not.
0449   ///
0450   /// This check is potentially costly, since it requires traversing,
0451   /// in the worst case, the whole use list of a value.
0452   bool hasOneUser() const;
0453 
0454   /// Return true if there is exactly one use of this value that cannot be
0455   /// dropped.
0456   Use *getSingleUndroppableUse();
0457   const Use *getSingleUndroppableUse() const {
0458     return const_cast<Value *>(this)->getSingleUndroppableUse();
0459   }
0460 
0461   /// Return true if there is exactly one unique user of this value that cannot be
0462   /// dropped (that user can have multiple uses of this value).
0463   User *getUniqueUndroppableUser();
0464   const User *getUniqueUndroppableUser() const {
0465     return const_cast<Value *>(this)->getUniqueUndroppableUser();
0466   }
0467 
0468   /// Return true if there this value.
0469   ///
0470   /// This is specialized because it is a common request and does not require
0471   /// traversing the whole use list.
0472   bool hasNUndroppableUses(unsigned N) const;
0473 
0474   /// Return true if this value has N uses or more.
0475   ///
0476   /// This is logically equivalent to getNumUses() >= N.
0477   bool hasNUndroppableUsesOrMore(unsigned N) const;
0478 
0479   /// Remove every uses that can safely be removed.
0480   ///
0481   /// This will remove for example uses in llvm.assume.
0482   /// This should be used when performing want to perform a tranformation but
0483   /// some Droppable uses pervent it.
0484   /// This function optionally takes a filter to only remove some droppable
0485   /// uses.
0486   void dropDroppableUses(llvm::function_ref<bool(const Use *)> ShouldDrop =
0487                              [](const Use *) { return true; });
0488 
0489   /// Remove every use of this value in \p User that can safely be removed.
0490   void dropDroppableUsesIn(User &Usr);
0491 
0492   /// Remove the droppable use \p U.
0493   static void dropDroppableUse(Use &U);
0494 
0495   /// Check if this value is used in the specified basic block.
0496   bool isUsedInBasicBlock(const BasicBlock *BB) const;
0497 
0498   /// This method computes the number of uses of this Value.
0499   ///
0500   /// This is a linear time operation.  Use hasOneUse, hasNUses, or
0501   /// hasNUsesOrMore to check for specific values.
0502   unsigned getNumUses() const;
0503 
0504   /// This method should only be used by the Use class.
0505   void addUse(Use &U) { U.addToList(&UseList); }
0506 
0507   /// Concrete subclass of this.
0508   ///
0509   /// An enumeration for keeping track of the concrete subclass of Value that
0510   /// is actually instantiated. Values of this enumeration are kept in the
0511   /// Value classes SubclassID field. They are used for concrete type
0512   /// identification.
0513   enum ValueTy {
0514 #define HANDLE_VALUE(Name) Name##Val,
0515 #include "llvm/IR/Value.def"
0516 
0517     // Markers:
0518 #define HANDLE_CONSTANT_MARKER(Marker, Constant) Marker = Constant##Val,
0519 #include "llvm/IR/Value.def"
0520   };
0521 
0522   /// Return an ID for the concrete type of this object.
0523   ///
0524   /// This is used to implement the classof checks.  This should not be used
0525   /// for any other purpose, as the values may change as LLVM evolves.  Also,
0526   /// note that for instructions, the Instruction's opcode is added to
0527   /// InstructionVal. So this means three things:
0528   /// # there is no value with code InstructionVal (no opcode==0).
0529   /// # there are more possible values for the value type than in ValueTy enum.
0530   /// # the InstructionVal enumerator must be the highest valued enumerator in
0531   ///   the ValueTy enum.
0532   unsigned getValueID() const {
0533     return SubclassID;
0534   }
0535 
0536   /// Return the raw optional flags value contained in this value.
0537   ///
0538   /// This should only be used when testing two Values for equivalence.
0539   unsigned getRawSubclassOptionalData() const {
0540     return SubclassOptionalData;
0541   }
0542 
0543   /// Clear the optional flags contained in this value.
0544   void clearSubclassOptionalData() {
0545     SubclassOptionalData = 0;
0546   }
0547 
0548   /// Check the optional flags for equality.
0549   bool hasSameSubclassOptionalData(const Value *V) const {
0550     return SubclassOptionalData == V->SubclassOptionalData;
0551   }
0552 
0553   /// Return true if there is a value handle associated with this value.
0554   bool hasValueHandle() const { return HasValueHandle; }
0555 
0556   /// Return true if there is metadata referencing this value.
0557   bool isUsedByMetadata() const { return IsUsedByMD; }
0558 
0559 protected:
0560   /// Get the current metadata attachments for the given kind, if any.
0561   ///
0562   /// These functions require that the value have at most a single attachment
0563   /// of the given kind, and return \c nullptr if such an attachment is missing.
0564   /// @{
0565   MDNode *getMetadata(unsigned KindID) const {
0566     if (!HasMetadata)
0567       return nullptr;
0568     return getMetadataImpl(KindID);
0569   }
0570   MDNode *getMetadata(StringRef Kind) const;
0571   /// @}
0572 
0573   /// Appends all attachments with the given ID to \c MDs in insertion order.
0574   /// If the Value has no attachments with the given ID, or if ID is invalid,
0575   /// leaves MDs unchanged.
0576   /// @{
0577   void getMetadata(unsigned KindID, SmallVectorImpl<MDNode *> &MDs) const;
0578   void getMetadata(StringRef Kind, SmallVectorImpl<MDNode *> &MDs) const;
0579   /// @}
0580 
0581   /// Appends all metadata attached to this value to \c MDs, sorting by
0582   /// KindID. The first element of each pair returned is the KindID, the second
0583   /// element is the metadata value. Attachments with the same ID appear in
0584   /// insertion order.
0585   void
0586   getAllMetadata(SmallVectorImpl<std::pair<unsigned, MDNode *>> &MDs) const;
0587 
0588   /// Return true if this value has any metadata attached to it.
0589   bool hasMetadata() const { return (bool)HasMetadata; }
0590 
0591   /// Return true if this value has the given type of metadata attached.
0592   /// @{
0593   bool hasMetadata(unsigned KindID) const {
0594     return getMetadata(KindID) != nullptr;
0595   }
0596   bool hasMetadata(StringRef Kind) const {
0597     return getMetadata(Kind) != nullptr;
0598   }
0599   /// @}
0600 
0601   /// Set a particular kind of metadata attachment.
0602   ///
0603   /// Sets the given attachment to \c MD, erasing it if \c MD is \c nullptr or
0604   /// replacing it if it already exists.
0605   /// @{
0606   void setMetadata(unsigned KindID, MDNode *Node);
0607   void setMetadata(StringRef Kind, MDNode *Node);
0608   /// @}
0609 
0610   /// Add a metadata attachment.
0611   /// @{
0612   void addMetadata(unsigned KindID, MDNode &MD);
0613   void addMetadata(StringRef Kind, MDNode &MD);
0614   /// @}
0615 
0616   /// Erase all metadata attachments with the given kind.
0617   ///
0618   /// \returns true if any metadata was removed.
0619   bool eraseMetadata(unsigned KindID);
0620 
0621   /// Erase all metadata attachments matching the given predicate.
0622   void eraseMetadataIf(function_ref<bool(unsigned, MDNode *)> Pred);
0623 
0624   /// Erase all metadata attached to this Value.
0625   void clearMetadata();
0626 
0627   /// Get metadata for the given kind, if any.
0628   /// This is an internal function that must only be called after
0629   /// checking that `hasMetadata()` returns true.
0630   MDNode *getMetadataImpl(unsigned KindID) const;
0631 
0632 public:
0633   /// Return true if this value is a swifterror value.
0634   ///
0635   /// swifterror values can be either a function argument or an alloca with a
0636   /// swifterror attribute.
0637   bool isSwiftError() const;
0638 
0639   /// Strip off pointer casts, all-zero GEPs and address space casts.
0640   ///
0641   /// Returns the original uncasted value.  If this is called on a non-pointer
0642   /// value, it returns 'this'.
0643   const Value *stripPointerCasts() const;
0644   Value *stripPointerCasts() {
0645     return const_cast<Value *>(
0646         static_cast<const Value *>(this)->stripPointerCasts());
0647   }
0648 
0649   /// Strip off pointer casts, all-zero GEPs, address space casts, and aliases.
0650   ///
0651   /// Returns the original uncasted value.  If this is called on a non-pointer
0652   /// value, it returns 'this'.
0653   const Value *stripPointerCastsAndAliases() const;
0654   Value *stripPointerCastsAndAliases() {
0655     return const_cast<Value *>(
0656         static_cast<const Value *>(this)->stripPointerCastsAndAliases());
0657   }
0658 
0659   /// Strip off pointer casts, all-zero GEPs and address space casts
0660   /// but ensures the representation of the result stays the same.
0661   ///
0662   /// Returns the original uncasted value with the same representation. If this
0663   /// is called on a non-pointer value, it returns 'this'.
0664   const Value *stripPointerCastsSameRepresentation() const;
0665   Value *stripPointerCastsSameRepresentation() {
0666     return const_cast<Value *>(static_cast<const Value *>(this)
0667                                    ->stripPointerCastsSameRepresentation());
0668   }
0669 
0670   /// Strip off pointer casts, all-zero GEPs, single-argument phi nodes and
0671   /// invariant group info.
0672   ///
0673   /// Returns the original uncasted value.  If this is called on a non-pointer
0674   /// value, it returns 'this'. This function should be used only in
0675   /// Alias analysis.
0676   const Value *stripPointerCastsForAliasAnalysis() const;
0677   Value *stripPointerCastsForAliasAnalysis() {
0678     return const_cast<Value *>(static_cast<const Value *>(this)
0679                                    ->stripPointerCastsForAliasAnalysis());
0680   }
0681 
0682   /// Strip off pointer casts and all-constant inbounds GEPs.
0683   ///
0684   /// Returns the original pointer value.  If this is called on a non-pointer
0685   /// value, it returns 'this'.
0686   const Value *stripInBoundsConstantOffsets() const;
0687   Value *stripInBoundsConstantOffsets() {
0688     return const_cast<Value *>(
0689               static_cast<const Value *>(this)->stripInBoundsConstantOffsets());
0690   }
0691 
0692   /// Accumulate the constant offset this value has compared to a base pointer.
0693   /// Only 'getelementptr' instructions (GEPs) are accumulated but other
0694   /// instructions, e.g., casts, are stripped away as well.
0695   /// The accumulated constant offset is added to \p Offset and the base
0696   /// pointer is returned.
0697   ///
0698   /// The APInt \p Offset has to have a bit-width equal to the IntPtr type for
0699   /// the address space of 'this' pointer value, e.g., use
0700   /// DataLayout::getIndexTypeSizeInBits(Ty).
0701   ///
0702   /// If \p AllowNonInbounds is true, offsets in GEPs are stripped and
0703   /// accumulated even if the GEP is not "inbounds".
0704   ///
0705   /// If \p AllowInvariantGroup is true then this method also looks through
0706   /// strip.invariant.group and launder.invariant.group intrinsics.
0707   ///
0708   /// If \p ExternalAnalysis is provided it will be used to calculate a offset
0709   /// when a operand of GEP is not constant.
0710   /// For example, for a value \p ExternalAnalysis might try to calculate a
0711   /// lower bound. If \p ExternalAnalysis is successful, it should return true.
0712   ///
0713   /// If this is called on a non-pointer value, it returns 'this' and the
0714   /// \p Offset is not modified.
0715   ///
0716   /// Note that this function will never return a nullptr. It will also never
0717   /// manipulate the \p Offset in a way that would not match the difference
0718   /// between the underlying value and the returned one. Thus, if no constant
0719   /// offset was found, the returned value is the underlying one and \p Offset
0720   /// is unchanged.
0721   const Value *stripAndAccumulateConstantOffsets(
0722       const DataLayout &DL, APInt &Offset, bool AllowNonInbounds,
0723       bool AllowInvariantGroup = false,
0724       function_ref<bool(Value &Value, APInt &Offset)> ExternalAnalysis =
0725           nullptr) const;
0726 
0727   Value *stripAndAccumulateConstantOffsets(
0728       const DataLayout &DL, APInt &Offset, bool AllowNonInbounds,
0729       bool AllowInvariantGroup = false,
0730       function_ref<bool(Value &Value, APInt &Offset)> ExternalAnalysis =
0731           nullptr) {
0732     return const_cast<Value *>(
0733         static_cast<const Value *>(this)->stripAndAccumulateConstantOffsets(
0734             DL, Offset, AllowNonInbounds, AllowInvariantGroup,
0735             ExternalAnalysis));
0736   }
0737 
0738   /// This is a wrapper around stripAndAccumulateConstantOffsets with the
0739   /// in-bounds requirement set to false.
0740   const Value *stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL,
0741                                                          APInt &Offset) const {
0742     return stripAndAccumulateConstantOffsets(DL, Offset,
0743                                              /* AllowNonInbounds */ false);
0744   }
0745   Value *stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL,
0746                                                    APInt &Offset) {
0747     return stripAndAccumulateConstantOffsets(DL, Offset,
0748                                              /* AllowNonInbounds */ false);
0749   }
0750 
0751   /// Strip off pointer casts and inbounds GEPs.
0752   ///
0753   /// Returns the original pointer value.  If this is called on a non-pointer
0754   /// value, it returns 'this'.
0755   const Value *stripInBoundsOffsets(function_ref<void(const Value *)> Func =
0756                                         [](const Value *) {}) const;
0757   inline Value *stripInBoundsOffsets(function_ref<void(const Value *)> Func =
0758                                   [](const Value *) {}) {
0759     return const_cast<Value *>(
0760         static_cast<const Value *>(this)->stripInBoundsOffsets(Func));
0761   }
0762 
0763   /// If this ptr is provably equal to \p Other plus a constant offset, return
0764   /// that offset in bytes. Essentially `ptr this` subtract `ptr Other`.
0765   std::optional<int64_t> getPointerOffsetFrom(const Value *Other,
0766                                               const DataLayout &DL) const;
0767 
0768   /// Return true if the memory object referred to by V can by freed in the
0769   /// scope for which the SSA value defining the allocation is statically
0770   /// defined.  E.g.  deallocation after the static scope of a value does not
0771   /// count, but a deallocation before that does.
0772   bool canBeFreed() const;
0773 
0774   /// Returns the number of bytes known to be dereferenceable for the
0775   /// pointer value.
0776   ///
0777   /// If CanBeNull is set by this function the pointer can either be null or be
0778   /// dereferenceable up to the returned number of bytes.
0779   ///
0780   /// IF CanBeFreed is true, the pointer is known to be dereferenceable at
0781   /// point of definition only.  Caller must prove that allocation is not
0782   /// deallocated between point of definition and use.
0783   uint64_t getPointerDereferenceableBytes(const DataLayout &DL,
0784                                           bool &CanBeNull,
0785                                           bool &CanBeFreed) const;
0786 
0787   /// Returns an alignment of the pointer value.
0788   ///
0789   /// Returns an alignment which is either specified explicitly, e.g. via
0790   /// align attribute of a function argument, or guaranteed by DataLayout.
0791   Align getPointerAlignment(const DataLayout &DL) const;
0792 
0793   /// Translate PHI node to its predecessor from the given basic block.
0794   ///
0795   /// If this value is a PHI node with CurBB as its parent, return the value in
0796   /// the PHI node corresponding to PredBB.  If not, return ourself.  This is
0797   /// useful if you want to know the value something has in a predecessor
0798   /// block.
0799   const Value *DoPHITranslation(const BasicBlock *CurBB,
0800                                 const BasicBlock *PredBB) const;
0801   Value *DoPHITranslation(const BasicBlock *CurBB, const BasicBlock *PredBB) {
0802     return const_cast<Value *>(
0803              static_cast<const Value *>(this)->DoPHITranslation(CurBB, PredBB));
0804   }
0805 
0806   /// The maximum alignment for instructions.
0807   ///
0808   /// This is the greatest alignment value supported by load, store, and alloca
0809   /// instructions, and global values.
0810   static constexpr unsigned MaxAlignmentExponent = 32;
0811   static constexpr uint64_t MaximumAlignment = 1ULL << MaxAlignmentExponent;
0812 
0813   /// Mutate the type of this Value to be of the specified type.
0814   ///
0815   /// Note that this is an extremely dangerous operation which can create
0816   /// completely invalid IR very easily.  It is strongly recommended that you
0817   /// recreate IR objects with the right types instead of mutating them in
0818   /// place.
0819   void mutateType(Type *Ty) {
0820     VTy = Ty;
0821   }
0822 
0823   /// Sort the use-list.
0824   ///
0825   /// Sorts the Value's use-list by Cmp using a stable mergesort.  Cmp is
0826   /// expected to compare two \a Use references.
0827   template <class Compare> void sortUseList(Compare Cmp);
0828 
0829   /// Reverse the use-list.
0830   void reverseUseList();
0831 
0832 private:
0833   /// Merge two lists together.
0834   ///
0835   /// Merges \c L and \c R using \c Cmp.  To enable stable sorts, always pushes
0836   /// "equal" items from L before items from R.
0837   ///
0838   /// \return the first element in the list.
0839   ///
0840   /// \note Completely ignores \a Use::Prev (doesn't read, doesn't update).
0841   template <class Compare>
0842   static Use *mergeUseLists(Use *L, Use *R, Compare Cmp) {
0843     Use *Merged;
0844     Use **Next = &Merged;
0845 
0846     while (true) {
0847       if (!L) {
0848         *Next = R;
0849         break;
0850       }
0851       if (!R) {
0852         *Next = L;
0853         break;
0854       }
0855       if (Cmp(*R, *L)) {
0856         *Next = R;
0857         Next = &R->Next;
0858         R = R->Next;
0859       } else {
0860         *Next = L;
0861         Next = &L->Next;
0862         L = L->Next;
0863       }
0864     }
0865 
0866     return Merged;
0867   }
0868 
0869 protected:
0870   unsigned short getSubclassDataFromValue() const { return SubclassData; }
0871   void setValueSubclassData(unsigned short D) { SubclassData = D; }
0872 };
0873 
0874 struct ValueDeleter { void operator()(Value *V) { V->deleteValue(); } };
0875 
0876 /// Use this instead of std::unique_ptr<Value> or std::unique_ptr<Instruction>.
0877 /// Those don't work because Value and Instruction's destructors are protected,
0878 /// aren't virtual, and won't destroy the complete object.
0879 using unique_value = std::unique_ptr<Value, ValueDeleter>;
0880 
0881 inline raw_ostream &operator<<(raw_ostream &OS, const Value &V) {
0882   V.print(OS);
0883   return OS;
0884 }
0885 
0886 void Use::set(Value *V) {
0887   if (Val) removeFromList();
0888   Val = V;
0889   if (V) V->addUse(*this);
0890 }
0891 
0892 Value *Use::operator=(Value *RHS) {
0893   set(RHS);
0894   return RHS;
0895 }
0896 
0897 const Use &Use::operator=(const Use &RHS) {
0898   set(RHS.Val);
0899   return *this;
0900 }
0901 
0902 template <class Compare> void Value::sortUseList(Compare Cmp) {
0903   if (!UseList || !UseList->Next)
0904     // No need to sort 0 or 1 uses.
0905     return;
0906 
0907   // Note: this function completely ignores Prev pointers until the end when
0908   // they're fixed en masse.
0909 
0910   // Create a binomial vector of sorted lists, visiting uses one at a time and
0911   // merging lists as necessary.
0912   const unsigned MaxSlots = 32;
0913   Use *Slots[MaxSlots];
0914 
0915   // Collect the first use, turning it into a single-item list.
0916   Use *Next = UseList->Next;
0917   UseList->Next = nullptr;
0918   unsigned NumSlots = 1;
0919   Slots[0] = UseList;
0920 
0921   // Collect all but the last use.
0922   while (Next->Next) {
0923     Use *Current = Next;
0924     Next = Current->Next;
0925 
0926     // Turn Current into a single-item list.
0927     Current->Next = nullptr;
0928 
0929     // Save Current in the first available slot, merging on collisions.
0930     unsigned I;
0931     for (I = 0; I < NumSlots; ++I) {
0932       if (!Slots[I])
0933         break;
0934 
0935       // Merge two lists, doubling the size of Current and emptying slot I.
0936       //
0937       // Since the uses in Slots[I] originally preceded those in Current, send
0938       // Slots[I] in as the left parameter to maintain a stable sort.
0939       Current = mergeUseLists(Slots[I], Current, Cmp);
0940       Slots[I] = nullptr;
0941     }
0942     // Check if this is a new slot.
0943     if (I == NumSlots) {
0944       ++NumSlots;
0945       assert(NumSlots <= MaxSlots && "Use list bigger than 2^32");
0946     }
0947 
0948     // Found an open slot.
0949     Slots[I] = Current;
0950   }
0951 
0952   // Merge all the lists together.
0953   assert(Next && "Expected one more Use");
0954   assert(!Next->Next && "Expected only one Use");
0955   UseList = Next;
0956   for (unsigned I = 0; I < NumSlots; ++I)
0957     if (Slots[I])
0958       // Since the uses in Slots[I] originally preceded those in UseList, send
0959       // Slots[I] in as the left parameter to maintain a stable sort.
0960       UseList = mergeUseLists(Slots[I], UseList, Cmp);
0961 
0962   // Fix the Prev pointers.
0963   for (Use *I = UseList, **Prev = &UseList; I; I = I->Next) {
0964     I->Prev = Prev;
0965     Prev = &I->Next;
0966   }
0967 }
0968 
0969 // isa - Provide some specializations of isa so that we don't have to include
0970 // the subtype header files to test to see if the value is a subclass...
0971 //
0972 template <> struct isa_impl<Constant, Value> {
0973   static inline bool doit(const Value &Val) {
0974     static_assert(Value::ConstantFirstVal == 0, "Val.getValueID() >= Value::ConstantFirstVal");
0975     return Val.getValueID() <= Value::ConstantLastVal;
0976   }
0977 };
0978 
0979 template <> struct isa_impl<ConstantData, Value> {
0980   static inline bool doit(const Value &Val) {
0981     return Val.getValueID() >= Value::ConstantDataFirstVal &&
0982            Val.getValueID() <= Value::ConstantDataLastVal;
0983   }
0984 };
0985 
0986 template <> struct isa_impl<ConstantAggregate, Value> {
0987   static inline bool doit(const Value &Val) {
0988     return Val.getValueID() >= Value::ConstantAggregateFirstVal &&
0989            Val.getValueID() <= Value::ConstantAggregateLastVal;
0990   }
0991 };
0992 
0993 template <> struct isa_impl<Argument, Value> {
0994   static inline bool doit (const Value &Val) {
0995     return Val.getValueID() == Value::ArgumentVal;
0996   }
0997 };
0998 
0999 template <> struct isa_impl<InlineAsm, Value> {
1000   static inline bool doit(const Value &Val) {
1001     return Val.getValueID() == Value::InlineAsmVal;
1002   }
1003 };
1004 
1005 template <> struct isa_impl<Instruction, Value> {
1006   static inline bool doit(const Value &Val) {
1007     return Val.getValueID() >= Value::InstructionVal;
1008   }
1009 };
1010 
1011 template <> struct isa_impl<BasicBlock, Value> {
1012   static inline bool doit(const Value &Val) {
1013     return Val.getValueID() == Value::BasicBlockVal;
1014   }
1015 };
1016 
1017 template <> struct isa_impl<Function, Value> {
1018   static inline bool doit(const Value &Val) {
1019     return Val.getValueID() == Value::FunctionVal;
1020   }
1021 };
1022 
1023 template <> struct isa_impl<GlobalVariable, Value> {
1024   static inline bool doit(const Value &Val) {
1025     return Val.getValueID() == Value::GlobalVariableVal;
1026   }
1027 };
1028 
1029 template <> struct isa_impl<GlobalAlias, Value> {
1030   static inline bool doit(const Value &Val) {
1031     return Val.getValueID() == Value::GlobalAliasVal;
1032   }
1033 };
1034 
1035 template <> struct isa_impl<GlobalIFunc, Value> {
1036   static inline bool doit(const Value &Val) {
1037     return Val.getValueID() == Value::GlobalIFuncVal;
1038   }
1039 };
1040 
1041 template <> struct isa_impl<GlobalValue, Value> {
1042   static inline bool doit(const Value &Val) {
1043     return isa<GlobalObject>(Val) || isa<GlobalAlias>(Val);
1044   }
1045 };
1046 
1047 template <> struct isa_impl<GlobalObject, Value> {
1048   static inline bool doit(const Value &Val) {
1049     return isa<GlobalVariable>(Val) || isa<Function>(Val) ||
1050            isa<GlobalIFunc>(Val);
1051   }
1052 };
1053 
1054 // Create wrappers for C Binding types (see CBindingWrapping.h).
1055 DEFINE_ISA_CONVERSION_FUNCTIONS(Value, LLVMValueRef)
1056 
1057 // Specialized opaque value conversions.
1058 inline Value **unwrap(LLVMValueRef *Vals) {
1059   return reinterpret_cast<Value**>(Vals);
1060 }
1061 
1062 template<typename T>
1063 inline T **unwrap(LLVMValueRef *Vals, unsigned Length) {
1064 #ifndef NDEBUG
1065   for (LLVMValueRef *I = Vals, *E = Vals + Length; I != E; ++I)
1066     unwrap<T>(*I); // For side effect of calling assert on invalid usage.
1067 #endif
1068   (void)Length;
1069   return reinterpret_cast<T**>(Vals);
1070 }
1071 
1072 inline LLVMValueRef *wrap(const Value **Vals) {
1073   return reinterpret_cast<LLVMValueRef*>(const_cast<Value**>(Vals));
1074 }
1075 
1076 } // end namespace llvm
1077 
1078 #endif // LLVM_IR_VALUE_H