LLVM 22.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:
46 : User(ty, vty, AllocInfo) {}
47
48 ~Constant() = default;
49
50public:
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.
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.
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;
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.
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.
171
172 //// Methods for support type inquiry through isa, cast, and dyn_cast:
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 ///
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
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.
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
220 return cast<Constant>(Value::stripPointerCasts());
221 }
222
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.
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.
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
243private:
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
#define LLVM_ABI
Definition: Compiler.h:213
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
Class for arbitrary precision integers.
Definition: APInt.h:78
This class represents a range of values.
Definition: ConstantRange.h:47
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...
Definition: Constants.cpp:403
~Constant()=default
LLVM_ABI bool hasExactInverseFP() const
Return true if this scalar has an exact multiplicative inverse or this vector has an exact multiplica...
Definition: Constants.cpp:256
static LLVM_ABI Constant * replaceUndefsWith(Constant *C, Constant *Replacement)
Try to replace undefined constant C or undefined elements in C with Replacement.
Definition: Constants.cpp:784
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.
Definition: Constants.cpp:1713
LLVM_ABI bool containsUndefElement() const
Return true if this is a vector constant that includes any strictly undef (not poison) elements.
Definition: Constants.cpp:354
static LLVM_ABI Constant * mergeUndefsWith(Constant *C, Constant *Other)
Merges undefs of a Constant with another Constant, along with the undefs already present.
Definition: Constants.cpp:808
Constant(const Constant &)=delete
LLVM_ABI ConstantRange toConstantRange() const
Convert constant to an approximate constant range.
Definition: Constants.cpp:1792
static LLVM_ABI Constant * getAllOnesValue(Type *Ty)
Definition: Constants.cpp:420
LLVM_ABI bool hasZeroLiveUses() const
Return true if the constant has no live uses.
Definition: Constants.cpp:768
LLVM_ABI bool isOneValue() const
Returns true if the value is one.
Definition: Constants.cpp:124
LLVM_ABI bool isManifestConstant() const
Return true if a constant is ConstantData or a ConstantAggregate or ConstantExpr that contain only Co...
Definition: Constants.cpp:843
LLVM_ABI bool isNegativeZeroValue() const
Return true if the value is what would be returned by getZeroValueForNegation.
Definition: Constants.cpp:56
LLVM_ABI bool isAllOnesValue() const
Return true if this is the value that would be returned by getAllOnesValue.
Definition: Constants.cpp:107
Constant(Type *ty, ValueTy vty, AllocInfo AllocInfo)
Definition: Constant.h:45
const Constant * stripPointerCasts() const
Definition: Constant.h:219
Constant * stripPointerCasts()
Definition: Constant.h:223
LLVM_ABI bool hasOneLiveUse() const
Return true if the constant has exactly one live use.
Definition: Constants.cpp:766
LLVM_ABI bool needsRelocation() const
This method classifies the entry according to whether or not it may generate a relocation entry (eith...
Definition: Constants.cpp:655
LLVM_ABI bool isDLLImportDependent() const
Return true if the value is dependent on a dllimport variable.
Definition: Constants.cpp:632
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,...
Definition: Constants.cpp:1778
LLVM_ABI bool containsConstantExpression() const
Return true if this is a fixed width vector constant that includes any constant expressions.
Definition: Constants.cpp:360
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...
Definition: Constants.cpp:214
LLVM_ABI void removeDeadConstantUsers() const
If there are any dead constant users dangling off of this constant, remove them.
Definition: Constants.cpp:739
LLVM_ABI bool isNormalFP() const
Return true if this is a normal (as opposed to denormal, infinity, nan, or zero) floating-point scala...
Definition: Constants.cpp:235
LLVM_ABI bool needsDynamicRelocation() const
Definition: Constants.cpp:651
static LLVM_ABI Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
Definition: Constants.cpp:373
LLVM_ABI bool isNaN() const
Return true if this is a floating-point NaN constant or a vector floating-point constant with all NaN...
Definition: Constants.cpp:277
LLVM_ABI bool isMinSignedValue() const
Return true if the value is the smallest signed value.
Definition: Constants.cpp:169
LLVM_ABI bool isConstantUsed() const
Return true if the constant has users other than constant expressions and other dangling things.
Definition: Constants.cpp:639
LLVM_ABI Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
Definition: Constants.cpp:435
LLVM_ABI bool isThreadDependent() const
Return true if the value can vary between threads.
Definition: Constants.cpp:625
LLVM_ABI bool isZeroValue() const
Return true if the value is negative zero or null value.
Definition: Constants.cpp:76
LLVM_ABI void destroyConstant()
Called if some element of this constant is no longer valid.
Definition: Constants.cpp:489
LLVM_ABI bool isNotMinSignedValue() const
Return true if the value is not the smallest signed value, or, for vectors, does not contain smallest...
Definition: Constants.cpp:186
LLVM_ABI bool isNullValue() const
Return true if this is the value that would be returned by getNullValue.
Definition: Constants.cpp:90
static bool classof(const Value *V)
Definition: Constant.h:173
LLVM_ABI bool isNotOneValue() const
Return true if the value is not the one value, or, for vectors, does not contain one value elements.
Definition: Constants.cpp:141
LLVM_ABI bool isElementWiseEqual(Value *Y) const
Return true if this constant and a constant 'Y' are element-wise equal.
Definition: Constants.cpp:298
LLVM_ABI bool containsUndefOrPoisonElement() const
Return true if this is a vector constant that includes any undef or poison elements.
Definition: Constants.cpp:344
LLVM_ABI bool containsPoisonElement() const
Return true if this is a vector constant that includes any poison elements.
Definition: Constants.cpp:349
LLVM_ABI void handleOperandChange(Value *, Value *)
This method is a special form of User::replaceUsesOfWith (which does not work on constants) that does...
Definition: Constants.cpp:3261
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
LLVM Value Representation.
Definition: Value.h:75
LLVM_ABI const Value * stripPointerCasts() const
Strip off pointer casts, all-zero GEPs and address space casts.
Definition: Value.cpp:701
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.
Definition: AddressRanges.h:18
@ Other
Any other memory.
#define N
Information about how a User object was allocated, to be passed into the User constructor.
Definition: User.h:79