LLVM 23.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 constant that includes any undef or
116 /// poison elements. Since it is impossible to inspect a scalable vector
117 /// element- wise at compile time, this function returns true only if the
118 /// entire vector is undef or poison.
120
121 /// Return true if this is a vector constant that includes any poison
122 /// elements.
123 LLVM_ABI bool containsPoisonElement() const;
124
125 /// Return true if this is a vector constant that includes any strictly undef
126 /// (not poison) elements.
127 LLVM_ABI bool containsUndefElement() const;
128
129 /// Return true if this is a fixed width vector constant that includes
130 /// any constant expressions.
132
133 /// Return true if the value can vary between threads.
134 LLVM_ABI bool isThreadDependent() const;
135
136 /// Return true if the value is dependent on a dllimport variable.
137 LLVM_ABI bool isDLLImportDependent() const;
138
139 /// Return true if the constant has users other than constant expressions and
140 /// other dangling things.
141 LLVM_ABI bool isConstantUsed() const;
142
143 /// This method classifies the entry according to whether or not it may
144 /// generate a relocation entry (either static or dynamic). This must be
145 /// conservative, so if it might codegen to a relocatable entry, it should say
146 /// so.
147 ///
148 /// FIXME: This really should not be in IR.
149 LLVM_ABI bool needsRelocation() const;
150 LLVM_ABI bool needsDynamicRelocation() const;
151
152 /// For aggregates (struct/array/vector) return the constant that corresponds
153 /// to the specified element if possible, or null if not. This can return null
154 /// if the element index is a ConstantExpr, if 'this' is a constant expr or
155 /// if the constant does not fit into an uint64_t.
156 LLVM_ABI Constant *getAggregateElement(unsigned Elt) const;
158
159 /// If all elements of the vector constant have the same value, return that
160 /// value. Otherwise, return nullptr. Ignore poison elements by setting
161 /// AllowPoison to true.
162 LLVM_ABI Constant *getSplatValue(bool AllowPoison = false) const;
163
164 /// If C is a constant integer then return its value, otherwise C must be a
165 /// vector of constant integers, all equal, and the common value is returned.
166 LLVM_ABI const APInt &getUniqueInteger() const;
167
168 /// Convert constant to an approximate constant range. For vectors, the
169 /// range is the union over the element ranges. Poison elements are ignored.
171
172 /// Called if some element of this constant is no longer valid.
173 /// At this point only other constants may be on the use_list for this
174 /// constant. Any constants on our Use list must also be destroy'd. The
175 /// implementation must be sure to remove the constant from the list of
176 /// available cached constants. Implementations should implement
177 /// destroyConstantImpl to remove constants from any pools/maps they are
178 /// contained it.
180
181 //// Methods for support type inquiry through isa, cast, and dyn_cast:
182 static bool classof(const Value *V) {
183 static_assert(ConstantFirstVal == 0, "V->getValueID() >= ConstantFirstVal always succeeds");
184 return V->getValueID() <= ConstantLastVal;
185 }
186
187 /// This method is a special form of User::replaceUsesOfWith
188 /// (which does not work on constants) that does work
189 /// on constants. Basically this method goes through the trouble of building
190 /// a new constant that is equivalent to the current one, with all uses of
191 /// From replaced with uses of To. After this construction is completed, all
192 /// of the users of 'this' are replaced to use the new constant, and then
193 /// 'this' is deleted. In general, you should not call this method, instead,
194 /// use Value::replaceAllUsesWith, which automatically dispatches to this
195 /// method as needed.
196 ///
198
199 LLVM_ABI static Constant *getNullValue(Type *Ty);
200
201 /// @returns the value for an integer or vector of integer constant of the
202 /// given type that has all its bits set to true.
203 /// Get the all ones value
205
206 /// Return the value for an integer or pointer constant, or a vector thereof,
207 /// with the given scalar value.
208 LLVM_ABI static Constant *getIntegerValue(Type *Ty, const APInt &V);
209
210 /// If there are any dead constant users dangling off of this constant, remove
211 /// them. This method is useful for clients that want to check to see if a
212 /// global is unused, but don't want to deal with potentially dead constants
213 /// hanging off of the globals.
215
216 /// Return true if the constant has exactly one live use.
217 ///
218 /// This returns the same result as calling Value::hasOneUse after
219 /// Constant::removeDeadConstantUsers, but doesn't remove dead constants.
220 LLVM_ABI bool hasOneLiveUse() const;
221
222 /// Return true if the constant has no live uses.
223 ///
224 /// This returns the same result as calling Value::use_empty after
225 /// Constant::removeDeadConstantUsers, but doesn't remove dead constants.
226 LLVM_ABI bool hasZeroLiveUses() const;
227
231
233 return const_cast<Constant*>(
234 static_cast<const Constant *>(this)->stripPointerCasts());
235 }
236
237 /// Try to replace undefined constant C or undefined elements in C with
238 /// Replacement. If no changes are made, the constant C is returned.
240 Constant *Replacement);
241
242 /// Merges undefs of a Constant with another Constant, along with the
243 /// undefs already present. Other doesn't have to be the same type as C, but
244 /// both must either be scalars or vectors with the same element count. If no
245 /// changes are made, the constant C is returned.
247
248 /// Return true if a constant is ConstantData or a ConstantAggregate or
249 /// ConstantExpr that contain only ConstantData.
250 LLVM_ABI bool isManifestConstant() const;
251
252private:
253 enum PossibleRelocationsTy {
254 /// This constant requires no relocations. That is, it holds simple
255 /// constants (like integrals).
256 NoRelocation = 0,
257
258 /// This constant holds static relocations that can be resolved by the
259 /// static linker.
260 LocalRelocation = 1,
261
262 /// This constant holds dynamic relocations that the dynamic linker will
263 /// need to resolve.
264 GlobalRelocation = 2,
265 };
266
267 /// Determine what potential relocations may be needed by this constant.
268 PossibleRelocationsTy getRelocationInfo() const;
269
270 bool hasNLiveUses(unsigned N) const;
271};
272
273} // end namespace llvm
274
275#endif // LLVM_IR_CONSTANT_H
#define LLVM_ABI
Definition Compiler.h:213
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 constant that includes any strictly undef (not poison) elements.
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:228
LLVM_ABI bool isMaxSignedValue() const
Return true if the value is the largest signed value.
Constant * stripPointerCasts()
Definition Constant.h:232
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 constant that includes any constant expressions.
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:182
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 constant that includes any undef or poison elements.
LLVM_ABI bool containsPoisonElement() const
Return true if this is a vector 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:524
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
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