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