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 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
43class Constant : public User {
44protected:
45 /// SubclassOptionalData bits. Low bits are used by ConstantExpr.
46 enum {
47 IsNullValue = (1 << 6),
48 };
49
50 /// Bits reserved in SubclassOptionalData, not to be used for ConstantExpr
51 /// flags.
52 static constexpr unsigned ConstantSubclassBits = IsNullValue;
53
55 : User(ty, vty, AllocInfo) {}
56
57 ~Constant() = default;
58
59public:
60 void operator=(const Constant &) = delete;
61 Constant(const Constant &) = delete;
62
63 /// Return true if this is the value that would be returned by getNullValue.
64 bool isNullValue() const { return SubclassOptionalData & IsNullValue; }
65
66 /// Returns true if the value is one.
67 LLVM_ABI bool isOneValue() const;
68
69 /// Return true if the value is not the one value, or,
70 /// for vectors, does not contain one value elements.
71 LLVM_ABI bool isNotOneValue() const;
72
73 /// Return true if this is the value that would be returned by
74 /// getAllOnesValue.
75 LLVM_ABI bool isAllOnesValue() const;
76
77 /// Return true if the value is what would be returned by
78 /// getZeroValueForNegation.
79 LLVM_ABI bool isNegativeZeroValue() const;
80
81 /// Return true if the value is not the smallest signed value, or,
82 /// for vectors, does not contain smallest signed value elements.
83 LLVM_ABI bool isNotMinSignedValue() const;
84
85 /// Return true if the value is the smallest signed value.
86 LLVM_ABI bool isMinSignedValue() const;
87
88 /// Return true if the value is the largest signed value.
89 LLVM_ABI bool isMaxSignedValue() const;
90
91 /// Return true if this is a finite and non-zero floating-point scalar
92 /// constant or a fixed width vector constant with all finite and non-zero
93 /// elements.
94 LLVM_ABI bool isFiniteNonZeroFP() const;
95
96 /// Return true if this is a normal (as opposed to denormal, infinity, nan,
97 /// or zero) floating-point scalar constant or a vector constant with all
98 /// normal elements. See APFloat::isNormal.
99 LLVM_ABI bool isNormalFP() const;
100
101 /// Return true if this scalar has an exact multiplicative inverse or this
102 /// vector has an exact multiplicative inverse for each element in the vector.
103 LLVM_ABI bool hasExactInverseFP() const;
104
105 /// Return true if this is a floating-point NaN constant or a vector
106 /// floating-point constant with all NaN elements.
107 LLVM_ABI bool isNaN() const;
108
109 /// Return true if this constant and a constant 'Y' are element-wise equal.
110 /// This is identical to just comparing the pointers, with the exception that
111 /// for vectors, if only one of the constants has an `undef` element in some
112 /// lane, the constants still match.
113 LLVM_ABI bool isElementWiseEqual(Value *Y) const;
114
115 /// Return true if this is a vector or aggregate constant that includes any
116 /// undef or poison elements. Nested aggregates (structs, arrays and fixed
117 /// width vectors) are inspected recursively. Since it is impossible to
118 /// inspect a scalable vector element-wise at compile time, this function
119 /// returns true for a scalable vector only if the entire vector is undef or
120 /// poison.
122
123 /// Return true if this is a vector or aggregate constant that includes any
124 /// poison elements. Nested aggregates are inspected recursively.
125 LLVM_ABI bool containsPoisonElement() const;
126
127 /// Return true if this is a vector or aggregate constant that includes any
128 /// strictly undef (not poison) elements. Nested aggregates are inspected
129 /// recursively.
130 LLVM_ABI bool containsUndefElement() const;
131
132 /// Return true if this is a fixed width vector or aggregate constant
133 /// that includes any constant expressions. Nested aggregates are inspected
134 /// recursively.
136
137 /// Return true if this is a vector constant where at least one element
138 /// satisfies the given predicate. Scalable vectors are not checked.
139 LLVM_ABI bool
141
142 /// Return true if the value can vary between threads.
143 LLVM_ABI bool isThreadDependent() const;
144
145 /// Return true if the value is dependent on a dllimport variable.
146 LLVM_ABI bool isDLLImportDependent() const;
147
148 /// Return true if the constant has users other than constant expressions and
149 /// other dangling things.
150 LLVM_ABI bool isConstantUsed() const;
151
152 /// This method classifies the entry according to whether or not it may
153 /// generate a relocation entry (either static or dynamic). This must be
154 /// conservative, so if it might codegen to a relocatable entry, it should say
155 /// so.
156 ///
157 /// FIXME: This really should not be in IR.
158 LLVM_ABI bool needsRelocation() const;
159 LLVM_ABI bool needsDynamicRelocation() const;
160
161 /// For aggregates (struct/array/vector) return the constant that corresponds
162 /// to the specified element if possible, or null if not. This can return null
163 /// if the element index is a ConstantExpr, if 'this' is a constant expr or
164 /// if the constant does not fit into an uint64_t.
165 LLVM_ABI Constant *getAggregateElement(unsigned Elt) const;
167
168 /// If all elements of the vector constant have the same value, return that
169 /// value. Otherwise, return nullptr. Ignore poison elements by setting
170 /// AllowPoison to true.
171 LLVM_ABI Constant *getSplatValue(bool AllowPoison = false) const;
172
173 /// If C is a constant integer then return its value, otherwise C must be a
174 /// vector of constant integers, all equal, and the common value is returned.
175 LLVM_ABI const APInt &getUniqueInteger() const;
176
177 /// Convert constant to an approximate constant range. For vectors, the
178 /// range is the union over the element ranges. Poison elements are ignored.
180
181 /// Called if some element of this constant is no longer valid.
182 /// At this point only other constants may be on the use_list for this
183 /// constant. Any constants on our Use list must also be destroy'd. The
184 /// implementation must be sure to remove the constant from the list of
185 /// available cached constants. Implementations should implement
186 /// destroyConstantImpl to remove constants from any pools/maps they are
187 /// contained it.
189
190 //// Methods for support type inquiry through isa, cast, and dyn_cast:
191 static bool classof(const Value *V) {
192 static_assert(ConstantFirstVal == 0, "V->getValueID() >= ConstantFirstVal always succeeds");
193 return V->getValueID() <= ConstantLastVal;
194 }
195
196 /// This method is a special form of User::replaceUsesOfWith
197 /// (which does not work on constants) that does work
198 /// on constants. Basically this method goes through the trouble of building
199 /// a new constant that is equivalent to the current one, with all uses of
200 /// From replaced with uses of To. After this construction is completed, all
201 /// of the users of 'this' are replaced to use the new constant, and then
202 /// 'this' is deleted. In general, you should not call this method, instead,
203 /// use Value::replaceAllUsesWith, which automatically dispatches to this
204 /// method as needed.
205 ///
207
208 LLVM_ABI static Constant *getNullValue(Type *Ty);
209
210 /// @returns the value for an integer or vector of integer constant of the
211 /// given type that has all its bits set to true.
212 /// Get the all ones value
214
215 /// Return the value for an integer or pointer constant, or a vector thereof,
216 /// with the given scalar value.
217 LLVM_ABI static Constant *getIntegerValue(Type *Ty, const APInt &V);
218
219 /// If there are any dead constant users dangling off of this constant, remove
220 /// them. This method is useful for clients that want to check to see if a
221 /// global is unused, but don't want to deal with potentially dead constants
222 /// hanging off of the globals.
224
225 /// Return true if the constant has exactly one live use.
226 ///
227 /// This returns the same result as calling Value::hasOneUse after
228 /// Constant::removeDeadConstantUsers, but doesn't remove dead constants.
229 LLVM_ABI bool hasOneLiveUse() const;
230
231 /// Return true if the constant has no live uses.
232 ///
233 /// This returns the same result as calling Value::use_empty after
234 /// Constant::removeDeadConstantUsers, but doesn't remove dead constants.
235 LLVM_ABI bool hasZeroLiveUses() const;
236
240
242 return const_cast<Constant*>(
243 static_cast<const Constant *>(this)->stripPointerCasts());
244 }
245
246 /// Try to replace undefined constant C or undefined elements in C with
247 /// Replacement. If no changes are made, the constant C is returned.
249 Constant *Replacement);
250
251 /// Merges undefs of a Constant with another Constant, along with the
252 /// undefs already present. Other doesn't have to be the same type as C, but
253 /// both must either be scalars or vectors with the same element count. If no
254 /// changes are made, the constant C is returned.
256
257 /// Return true if a constant is ConstantData or a ConstantAggregate or
258 /// ConstantExpr that contain only ConstantData.
259 LLVM_ABI bool isManifestConstant() const;
260
261private:
262 enum PossibleRelocationsTy {
263 /// This constant requires no relocations. That is, it holds simple
264 /// constants (like integrals).
265 NoRelocation = 0,
266
267 /// This constant holds static relocations that can be resolved by the
268 /// static linker.
269 LocalRelocation = 1,
270
271 /// This constant holds dynamic relocations that the dynamic linker will
272 /// need to resolve.
273 GlobalRelocation = 2,
274 };
275
276 /// Determine what potential relocations may be needed by this constant.
277 PossibleRelocationsTy getRelocationInfo() const;
278
279 bool hasNLiveUses(unsigned N) const;
280};
281
282} // end namespace llvm
283
284#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:215
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 values.
This is an important base class in LLVM.
Definition Constant.h:43
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:64
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:89
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:50
LLVM_ABI bool isAllOnesValue() const
Return true if this is the value that would be returned by getAllOnesValue.
Definition Constants.cpp:68
Constant(Type *ty, ValueTy vty, AllocInfo AllocInfo)
Definition Constant.h:54
const Constant * stripPointerCasts() const
Definition Constant.h:237
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:241
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:52
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:191
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:712
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