1 //===-- llvm/Constant.h - Constant class definition -------------*- 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 contains the declaration of the Constant class. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #ifndef LLVM_IR_CONSTANT_H 14 #define LLVM_IR_CONSTANT_H 15 16 #include "llvm/IR/User.h" 17 #include "llvm/IR/Value.h" 18 #include "llvm/Support/Casting.h" 19 #include "llvm/Support/Compiler.h" 20 21 namespace llvm { 22 23 class ConstantRange; 24 class APInt; 25 26 /// This is an important base class in LLVM. It provides the common facilities 27 /// of all constant values in an LLVM program. A constant is a value that is 28 /// immutable at runtime. Functions are constants because their address is 29 /// immutable. Same with global variables. 30 /// 31 /// All constants share the capabilities provided in this class. All constants 32 /// can have a null value. They can have an operand list. Constants can be 33 /// simple (integer and floating point values), complex (arrays and structures), 34 /// or expression based (computations yielding a constant value composed of 35 /// only certain operators and other constant values). 36 /// 37 /// Note that Constants are immutable (once created they never change) 38 /// and are fully shared by structural equivalence. This means that two 39 /// structurally equivalent constants will always have the same address. 40 /// Constants are created on demand as needed and never deleted: thus clients 41 /// don't have to worry about the lifetime of the objects. 42 /// LLVM Constant Representation 43 class Constant : public User { 44 protected: Constant(Type * ty,ValueTy vty,AllocInfo AllocInfo)45 Constant(Type *ty, ValueTy vty, AllocInfo AllocInfo) 46 : User(ty, vty, AllocInfo) {} 47 48 ~Constant() = default; 49 50 public: 51 void operator=(const Constant &) = delete; 52 Constant(const Constant &) = delete; 53 54 /// Return true if this is the value that would be returned by getNullValue. 55 LLVM_ABI bool isNullValue() const; 56 57 /// Returns true if the value is one. 58 LLVM_ABI bool isOneValue() const; 59 60 /// Return true if the value is not the one value, or, 61 /// for vectors, does not contain one value elements. 62 LLVM_ABI bool isNotOneValue() const; 63 64 /// Return true if this is the value that would be returned by 65 /// getAllOnesValue. 66 LLVM_ABI bool isAllOnesValue() const; 67 68 /// Return true if the value is what would be returned by 69 /// getZeroValueForNegation. 70 LLVM_ABI bool isNegativeZeroValue() const; 71 72 /// Return true if the value is negative zero or null value. 73 LLVM_ABI bool isZeroValue() const; 74 75 /// Return true if the value is not the smallest signed value, or, 76 /// for vectors, does not contain smallest signed value elements. 77 LLVM_ABI bool isNotMinSignedValue() const; 78 79 /// Return true if the value is the smallest signed value. 80 LLVM_ABI bool isMinSignedValue() const; 81 82 /// Return true if this is a finite and non-zero floating-point scalar 83 /// constant or a fixed width vector constant with all finite and non-zero 84 /// elements. 85 LLVM_ABI bool isFiniteNonZeroFP() const; 86 87 /// Return true if this is a normal (as opposed to denormal, infinity, nan, 88 /// or zero) floating-point scalar constant or a vector constant with all 89 /// normal elements. See APFloat::isNormal. 90 LLVM_ABI bool isNormalFP() const; 91 92 /// Return true if this scalar has an exact multiplicative inverse or this 93 /// vector has an exact multiplicative inverse for each element in the vector. 94 LLVM_ABI bool hasExactInverseFP() const; 95 96 /// Return true if this is a floating-point NaN constant or a vector 97 /// floating-point constant with all NaN elements. 98 LLVM_ABI bool isNaN() const; 99 100 /// Return true if this constant and a constant 'Y' are element-wise equal. 101 /// This is identical to just comparing the pointers, with the exception that 102 /// for vectors, if only one of the constants has an `undef` element in some 103 /// lane, the constants still match. 104 LLVM_ABI bool isElementWiseEqual(Value *Y) const; 105 106 /// Return true if this is a vector constant that includes any undef or 107 /// poison elements. Since it is impossible to inspect a scalable vector 108 /// element- wise at compile time, this function returns true only if the 109 /// entire vector is undef or poison. 110 LLVM_ABI bool containsUndefOrPoisonElement() const; 111 112 /// Return true if this is a vector constant that includes any poison 113 /// elements. 114 LLVM_ABI bool containsPoisonElement() const; 115 116 /// Return true if this is a vector constant that includes any strictly undef 117 /// (not poison) elements. 118 LLVM_ABI bool containsUndefElement() const; 119 120 /// Return true if this is a fixed width vector constant that includes 121 /// any constant expressions. 122 LLVM_ABI bool containsConstantExpression() const; 123 124 /// Return true if the value can vary between threads. 125 LLVM_ABI bool isThreadDependent() const; 126 127 /// Return true if the value is dependent on a dllimport variable. 128 LLVM_ABI bool isDLLImportDependent() const; 129 130 /// Return true if the constant has users other than constant expressions and 131 /// other dangling things. 132 LLVM_ABI bool isConstantUsed() const; 133 134 /// This method classifies the entry according to whether or not it may 135 /// generate a relocation entry (either static or dynamic). This must be 136 /// conservative, so if it might codegen to a relocatable entry, it should say 137 /// so. 138 /// 139 /// FIXME: This really should not be in IR. 140 LLVM_ABI bool needsRelocation() const; 141 LLVM_ABI bool needsDynamicRelocation() const; 142 143 /// For aggregates (struct/array/vector) return the constant that corresponds 144 /// to the specified element if possible, or null if not. This can return null 145 /// if the element index is a ConstantExpr, if 'this' is a constant expr or 146 /// if the constant does not fit into an uint64_t. 147 LLVM_ABI Constant *getAggregateElement(unsigned Elt) const; 148 LLVM_ABI Constant *getAggregateElement(Constant *Elt) const; 149 150 /// If all elements of the vector constant have the same value, return that 151 /// value. Otherwise, return nullptr. Ignore poison elements by setting 152 /// AllowPoison to true. 153 LLVM_ABI Constant *getSplatValue(bool AllowPoison = false) const; 154 155 /// If C is a constant integer then return its value, otherwise C must be a 156 /// vector of constant integers, all equal, and the common value is returned. 157 LLVM_ABI const APInt &getUniqueInteger() const; 158 159 /// Convert constant to an approximate constant range. For vectors, the 160 /// range is the union over the element ranges. Poison elements are ignored. 161 LLVM_ABI ConstantRange toConstantRange() const; 162 163 /// Called if some element of this constant is no longer valid. 164 /// At this point only other constants may be on the use_list for this 165 /// constant. Any constants on our Use list must also be destroy'd. The 166 /// implementation must be sure to remove the constant from the list of 167 /// available cached constants. Implementations should implement 168 /// destroyConstantImpl to remove constants from any pools/maps they are 169 /// contained it. 170 LLVM_ABI void destroyConstant(); 171 172 //// Methods for support type inquiry through isa, cast, and dyn_cast: classof(const Value * V)173 static bool classof(const Value *V) { 174 static_assert(ConstantFirstVal == 0, "V->getValueID() >= ConstantFirstVal always succeeds"); 175 return V->getValueID() <= ConstantLastVal; 176 } 177 178 /// This method is a special form of User::replaceUsesOfWith 179 /// (which does not work on constants) that does work 180 /// on constants. Basically this method goes through the trouble of building 181 /// a new constant that is equivalent to the current one, with all uses of 182 /// From replaced with uses of To. After this construction is completed, all 183 /// of the users of 'this' are replaced to use the new constant, and then 184 /// 'this' is deleted. In general, you should not call this method, instead, 185 /// use Value::replaceAllUsesWith, which automatically dispatches to this 186 /// method as needed. 187 /// 188 LLVM_ABI void handleOperandChange(Value *, Value *); 189 190 LLVM_ABI static Constant *getNullValue(Type *Ty); 191 192 /// @returns the value for an integer or vector of integer constant of the 193 /// given type that has all its bits set to true. 194 /// Get the all ones value 195 LLVM_ABI static Constant *getAllOnesValue(Type *Ty); 196 197 /// Return the value for an integer or pointer constant, or a vector thereof, 198 /// with the given scalar value. 199 LLVM_ABI static Constant *getIntegerValue(Type *Ty, const APInt &V); 200 201 /// If there are any dead constant users dangling off of this constant, remove 202 /// them. This method is useful for clients that want to check to see if a 203 /// global is unused, but don't want to deal with potentially dead constants 204 /// hanging off of the globals. 205 LLVM_ABI void removeDeadConstantUsers() const; 206 207 /// Return true if the constant has exactly one live use. 208 /// 209 /// This returns the same result as calling Value::hasOneUse after 210 /// Constant::removeDeadConstantUsers, but doesn't remove dead constants. 211 LLVM_ABI bool hasOneLiveUse() const; 212 213 /// Return true if the constant has no live uses. 214 /// 215 /// This returns the same result as calling Value::use_empty after 216 /// Constant::removeDeadConstantUsers, but doesn't remove dead constants. 217 LLVM_ABI bool hasZeroLiveUses() const; 218 stripPointerCasts()219 const Constant *stripPointerCasts() const { 220 return cast<Constant>(Value::stripPointerCasts()); 221 } 222 stripPointerCasts()223 Constant *stripPointerCasts() { 224 return const_cast<Constant*>( 225 static_cast<const Constant *>(this)->stripPointerCasts()); 226 } 227 228 /// Try to replace undefined constant C or undefined elements in C with 229 /// Replacement. If no changes are made, the constant C is returned. 230 LLVM_ABI static Constant *replaceUndefsWith(Constant *C, 231 Constant *Replacement); 232 233 /// Merges undefs of a Constant with another Constant, along with the 234 /// undefs already present. Other doesn't have to be the same type as C, but 235 /// both must either be scalars or vectors with the same element count. If no 236 /// changes are made, the constant C is returned. 237 LLVM_ABI static Constant *mergeUndefsWith(Constant *C, Constant *Other); 238 239 /// Return true if a constant is ConstantData or a ConstantAggregate or 240 /// ConstantExpr that contain only ConstantData. 241 LLVM_ABI bool isManifestConstant() const; 242 243 private: 244 enum PossibleRelocationsTy { 245 /// This constant requires no relocations. That is, it holds simple 246 /// constants (like integrals). 247 NoRelocation = 0, 248 249 /// This constant holds static relocations that can be resolved by the 250 /// static linker. 251 LocalRelocation = 1, 252 253 /// This constant holds dynamic relocations that the dynamic linker will 254 /// need to resolve. 255 GlobalRelocation = 2, 256 }; 257 258 /// Determine what potential relocations may be needed by this constant. 259 PossibleRelocationsTy getRelocationInfo() const; 260 261 bool hasNLiveUses(unsigned N) const; 262 }; 263 264 } // end namespace llvm 265 266 #endif // LLVM_IR_CONSTANT_H 267