Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:57

0001 //===-- llvm/Constant.h - Constant class definition -------------*- 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 contains the declaration of the Constant class.
0010 //
0011 //===----------------------------------------------------------------------===//
0012 
0013 #ifndef LLVM_IR_CONSTANT_H
0014 #define LLVM_IR_CONSTANT_H
0015 
0016 #include "llvm/IR/User.h"
0017 #include "llvm/IR/Value.h"
0018 #include "llvm/Support/Casting.h"
0019 
0020 namespace llvm {
0021 
0022 class ConstantRange;
0023 class APInt;
0024 
0025 /// This is an important base class in LLVM. It provides the common facilities
0026 /// of all constant values in an LLVM program. A constant is a value that is
0027 /// immutable at runtime. Functions are constants because their address is
0028 /// immutable. Same with global variables.
0029 ///
0030 /// All constants share the capabilities provided in this class. All constants
0031 /// can have a null value. They can have an operand list. Constants can be
0032 /// simple (integer and floating point values), complex (arrays and structures),
0033 /// or expression based (computations yielding a constant value composed of
0034 /// only certain operators and other constant values).
0035 ///
0036 /// Note that Constants are immutable (once created they never change)
0037 /// and are fully shared by structural equivalence.  This means that two
0038 /// structurally equivalent constants will always have the same address.
0039 /// Constants are created on demand as needed and never deleted: thus clients
0040 /// don't have to worry about the lifetime of the objects.
0041 /// LLVM Constant Representation
0042 class Constant : public User {
0043 protected:
0044   Constant(Type *ty, ValueTy vty, AllocInfo AllocInfo)
0045       : User(ty, vty, AllocInfo) {}
0046 
0047   ~Constant() = default;
0048 
0049 public:
0050   void operator=(const Constant &) = delete;
0051   Constant(const Constant &) = delete;
0052 
0053   /// Return true if this is the value that would be returned by getNullValue.
0054   bool isNullValue() const;
0055 
0056   /// Returns true if the value is one.
0057   bool isOneValue() const;
0058 
0059   /// Return true if the value is not the one value, or,
0060   /// for vectors, does not contain one value elements.
0061   bool isNotOneValue() const;
0062 
0063   /// Return true if this is the value that would be returned by
0064   /// getAllOnesValue.
0065   bool isAllOnesValue() const;
0066 
0067   /// Return true if the value is what would be returned by
0068   /// getZeroValueForNegation.
0069   bool isNegativeZeroValue() const;
0070 
0071   /// Return true if the value is negative zero or null value.
0072   bool isZeroValue() const;
0073 
0074   /// Return true if the value is not the smallest signed value, or,
0075   /// for vectors, does not contain smallest signed value elements.
0076   bool isNotMinSignedValue() const;
0077 
0078   /// Return true if the value is the smallest signed value.
0079   bool isMinSignedValue() const;
0080 
0081   /// Return true if this is a finite and non-zero floating-point scalar
0082   /// constant or a fixed width vector constant with all finite and non-zero
0083   /// elements.
0084   bool isFiniteNonZeroFP() const;
0085 
0086   /// Return true if this is a normal (as opposed to denormal, infinity, nan,
0087   /// or zero) floating-point scalar constant or a vector constant with all
0088   /// normal elements. See APFloat::isNormal.
0089   bool isNormalFP() const;
0090 
0091   /// Return true if this scalar has an exact multiplicative inverse or this
0092   /// vector has an exact multiplicative inverse for each element in the vector.
0093   bool hasExactInverseFP() const;
0094 
0095   /// Return true if this is a floating-point NaN constant or a vector
0096   /// floating-point constant with all NaN elements.
0097   bool isNaN() const;
0098 
0099   /// Return true if this constant and a constant 'Y' are element-wise equal.
0100   /// This is identical to just comparing the pointers, with the exception that
0101   /// for vectors, if only one of the constants has an `undef` element in some
0102   /// lane, the constants still match.
0103   bool isElementWiseEqual(Value *Y) const;
0104 
0105   /// Return true if this is a vector constant that includes any undef or
0106   /// poison elements. Since it is impossible to inspect a scalable vector
0107   /// element- wise at compile time, this function returns true only if the
0108   /// entire vector is undef or poison.
0109   bool containsUndefOrPoisonElement() const;
0110 
0111   /// Return true if this is a vector constant that includes any poison
0112   /// elements.
0113   bool containsPoisonElement() const;
0114 
0115   /// Return true if this is a vector constant that includes any strictly undef
0116   /// (not poison) elements.
0117   bool containsUndefElement() const;
0118 
0119   /// Return true if this is a fixed width vector constant that includes
0120   /// any constant expressions.
0121   bool containsConstantExpression() const;
0122 
0123   /// Return true if the value can vary between threads.
0124   bool isThreadDependent() const;
0125 
0126   /// Return true if the value is dependent on a dllimport variable.
0127   bool isDLLImportDependent() const;
0128 
0129   /// Return true if the constant has users other than constant expressions and
0130   /// other dangling things.
0131   bool isConstantUsed() const;
0132 
0133   /// This method classifies the entry according to whether or not it may
0134   /// generate a relocation entry (either static or dynamic). This must be
0135   /// conservative, so if it might codegen to a relocatable entry, it should say
0136   /// so.
0137   ///
0138   /// FIXME: This really should not be in IR.
0139   bool needsRelocation() const;
0140   bool needsDynamicRelocation() const;
0141 
0142   /// For aggregates (struct/array/vector) return the constant that corresponds
0143   /// to the specified element if possible, or null if not. This can return null
0144   /// if the element index is a ConstantExpr, if 'this' is a constant expr or
0145   /// if the constant does not fit into an uint64_t.
0146   Constant *getAggregateElement(unsigned Elt) const;
0147   Constant *getAggregateElement(Constant *Elt) const;
0148 
0149   /// If all elements of the vector constant have the same value, return that
0150   /// value. Otherwise, return nullptr. Ignore poison elements by setting
0151   /// AllowPoison to true.
0152   Constant *getSplatValue(bool AllowPoison = false) const;
0153 
0154   /// If C is a constant integer then return its value, otherwise C must be a
0155   /// vector of constant integers, all equal, and the common value is returned.
0156   const APInt &getUniqueInteger() const;
0157 
0158   /// Convert constant to an approximate constant range. For vectors, the
0159   /// range is the union over the element ranges. Poison elements are ignored.
0160   ConstantRange toConstantRange() const;
0161 
0162   /// Called if some element of this constant is no longer valid.
0163   /// At this point only other constants may be on the use_list for this
0164   /// constant.  Any constants on our Use list must also be destroy'd.  The
0165   /// implementation must be sure to remove the constant from the list of
0166   /// available cached constants.  Implementations should implement
0167   /// destroyConstantImpl to remove constants from any pools/maps they are
0168   /// contained it.
0169   void destroyConstant();
0170 
0171   //// Methods for support type inquiry through isa, cast, and dyn_cast:
0172   static bool classof(const Value *V) {
0173     static_assert(ConstantFirstVal == 0, "V->getValueID() >= ConstantFirstVal always succeeds");
0174     return V->getValueID() <= ConstantLastVal;
0175   }
0176 
0177   /// This method is a special form of User::replaceUsesOfWith
0178   /// (which does not work on constants) that does work
0179   /// on constants.  Basically this method goes through the trouble of building
0180   /// a new constant that is equivalent to the current one, with all uses of
0181   /// From replaced with uses of To.  After this construction is completed, all
0182   /// of the users of 'this' are replaced to use the new constant, and then
0183   /// 'this' is deleted.  In general, you should not call this method, instead,
0184   /// use Value::replaceAllUsesWith, which automatically dispatches to this
0185   /// method as needed.
0186   ///
0187   void handleOperandChange(Value *, Value *);
0188 
0189   static Constant *getNullValue(Type* Ty);
0190 
0191   /// @returns the value for an integer or vector of integer constant of the
0192   /// given type that has all its bits set to true.
0193   /// Get the all ones value
0194   static Constant *getAllOnesValue(Type* Ty);
0195 
0196   /// Return the value for an integer or pointer constant, or a vector thereof,
0197   /// with the given scalar value.
0198   static Constant *getIntegerValue(Type *Ty, const APInt &V);
0199 
0200   /// If there are any dead constant users dangling off of this constant, remove
0201   /// them. This method is useful for clients that want to check to see if a
0202   /// global is unused, but don't want to deal with potentially dead constants
0203   /// hanging off of the globals.
0204   void removeDeadConstantUsers() const;
0205 
0206   /// Return true if the constant has exactly one live use.
0207   ///
0208   /// This returns the same result as calling Value::hasOneUse after
0209   /// Constant::removeDeadConstantUsers, but doesn't remove dead constants.
0210   bool hasOneLiveUse() const;
0211 
0212   /// Return true if the constant has no live uses.
0213   ///
0214   /// This returns the same result as calling Value::use_empty after
0215   /// Constant::removeDeadConstantUsers, but doesn't remove dead constants.
0216   bool hasZeroLiveUses() const;
0217 
0218   const Constant *stripPointerCasts() const {
0219     return cast<Constant>(Value::stripPointerCasts());
0220   }
0221 
0222   Constant *stripPointerCasts() {
0223     return const_cast<Constant*>(
0224                       static_cast<const Constant *>(this)->stripPointerCasts());
0225   }
0226 
0227   /// Try to replace undefined constant C or undefined elements in C with
0228   /// Replacement. If no changes are made, the constant C is returned.
0229   static Constant *replaceUndefsWith(Constant *C, Constant *Replacement);
0230 
0231   /// Merges undefs of a Constant with another Constant, along with the
0232   /// undefs already present. Other doesn't have to be the same type as C, but
0233   /// both must either be scalars or vectors with the same element count. If no
0234   /// changes are made, the constant C is returned.
0235   static Constant *mergeUndefsWith(Constant *C, Constant *Other);
0236 
0237   /// Return true if a constant is ConstantData or a ConstantAggregate or
0238   /// ConstantExpr that contain only ConstantData.
0239   bool isManifestConstant() const;
0240 
0241 private:
0242   enum PossibleRelocationsTy {
0243     /// This constant requires no relocations. That is, it holds simple
0244     /// constants (like integrals).
0245     NoRelocation = 0,
0246 
0247     /// This constant holds static relocations that can be resolved by the
0248     /// static linker.
0249     LocalRelocation = 1,
0250 
0251     /// This constant holds dynamic relocations that the dynamic linker will
0252     /// need to resolve.
0253     GlobalRelocation = 2,
0254   };
0255 
0256   /// Determine what potential relocations may be needed by this constant.
0257   PossibleRelocationsTy getRelocationInfo() const;
0258 
0259   bool hasNLiveUses(unsigned N) const;
0260 };
0261 
0262 } // end namespace llvm
0263 
0264 #endif // LLVM_IR_CONSTANT_H