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 
20 namespace llvm {
21 
22 class APInt;
23 
24 /// This is an important base class in LLVM. It provides the common facilities
25 /// of all constant values in an LLVM program. A constant is a value that is
26 /// immutable at runtime. Functions are constants because their address is
27 /// immutable. Same with global variables.
28 ///
29 /// All constants share the capabilities provided in this class. All constants
30 /// can have a null value. They can have an operand list. Constants can be
31 /// simple (integer and floating point values), complex (arrays and structures),
32 /// or expression based (computations yielding a constant value composed of
33 /// only certain operators and other constant values).
34 ///
35 /// Note that Constants are immutable (once created they never change)
36 /// and are fully shared by structural equivalence.  This means that two
37 /// structurally equivalent constants will always have the same address.
38 /// Constants are created on demand as needed and never deleted: thus clients
39 /// don't have to worry about the lifetime of the objects.
40 /// LLVM Constant Representation
41 class Constant : public User {
42 protected:
43   Constant(Type *ty, ValueTy vty, Use *Ops, unsigned NumOps)
44     : User(ty, vty, Ops, NumOps) {}
45 
46 public:
47   void operator=(const Constant &) = delete;
48   Constant(const Constant &) = delete;
49 
50   /// Return true if this is the value that would be returned by getNullValue.
51   bool isNullValue() const;
52 
53   /// Returns true if the value is one.
54   bool isOneValue() const;
55 
56   /// Return true if the value is not the one value, or,
57   /// for vectors, does not contain one value elements.
58   bool isNotOneValue() const;
59 
60   /// Return true if this is the value that would be returned by
61   /// getAllOnesValue.
62   bool isAllOnesValue() const;
63 
64   /// Return true if the value is what would be returned by
65   /// getZeroValueForNegation.
66   bool isNegativeZeroValue() const;
67 
68   /// Return true if the value is negative zero or null value.
69   bool isZeroValue() const;
70 
71   /// Return true if the value is not the smallest signed value, or,
72   /// for vectors, does not contain smallest signed value elements.
73   bool isNotMinSignedValue() const;
74 
75   /// Return true if the value is the smallest signed value.
76   bool isMinSignedValue() const;
77 
78   /// Return true if this is a finite and non-zero floating-point scalar
79   /// constant or a vector constant with all finite and non-zero elements.
80   bool isFiniteNonZeroFP() const;
81 
82   /// Return true if this is a normal (as opposed to denormal) floating-point
83   /// scalar constant or a vector constant with all normal elements.
84   bool isNormalFP() const;
85 
86   /// Return true if this scalar has an exact multiplicative inverse or this
87   /// vector has an exact multiplicative inverse for each element in the vector.
88   bool hasExactInverseFP() const;
89 
90   /// Return true if this is a floating-point NaN constant or a vector
91   /// floating-point constant with all NaN elements.
92   bool isNaN() const;
93 
94   /// Return true if this constant and a constant 'Y' are element-wise equal.
95   /// This is identical to just comparing the pointers, with the exception that
96   /// for vectors, if only one of the constants has an `undef` element in some
97   /// lane, the constants still match.
98   bool isElementWiseEqual(Value *Y) const;
99 
100   /// Return true if this is a vector constant that includes any undefined
101   /// elements.
102   bool containsUndefElement() const;
103 
104   /// Return true if this is a vector constant that includes any constant
105   /// expressions.
106   bool containsConstantExpression() const;
107 
108   /// Return true if evaluation of this constant could trap. This is true for
109   /// things like constant expressions that could divide by zero.
110   bool canTrap() const;
111 
112   /// Return true if the value can vary between threads.
113   bool isThreadDependent() const;
114 
115   /// Return true if the value is dependent on a dllimport variable.
116   bool isDLLImportDependent() const;
117 
118   /// Return true if the constant has users other than constant expressions and
119   /// other dangling things.
120   bool isConstantUsed() const;
121 
122   /// This method classifies the entry according to whether or not it may
123   /// generate a relocation entry.  This must be conservative, so if it might
124   /// codegen to a relocatable entry, it should say so.
125   ///
126   /// FIXME: This really should not be in IR.
127   bool needsRelocation() const;
128 
129   /// For aggregates (struct/array/vector) return the constant that corresponds
130   /// to the specified element if possible, or null if not. This can return null
131   /// if the element index is a ConstantExpr, if 'this' is a constant expr or
132   /// if the constant does not fit into an uint64_t.
133   Constant *getAggregateElement(unsigned Elt) const;
134   Constant *getAggregateElement(Constant *Elt) const;
135 
136   /// If all elements of the vector constant have the same value, return that
137   /// value. Otherwise, return nullptr. Ignore undefined elements by setting
138   /// AllowUndefs to true.
139   Constant *getSplatValue(bool AllowUndefs = false) const;
140 
141   /// If C is a constant integer then return its value, otherwise C must be a
142   /// vector of constant integers, all equal, and the common value is returned.
143   const APInt &getUniqueInteger() const;
144 
145   /// Called if some element of this constant is no longer valid.
146   /// At this point only other constants may be on the use_list for this
147   /// constant.  Any constants on our Use list must also be destroy'd.  The
148   /// implementation must be sure to remove the constant from the list of
149   /// available cached constants.  Implementations should implement
150   /// destroyConstantImpl to remove constants from any pools/maps they are
151   /// contained it.
152   void destroyConstant();
153 
154   //// Methods for support type inquiry through isa, cast, and dyn_cast:
155   static bool classof(const Value *V) {
156     static_assert(ConstantFirstVal == 0, "V->getValueID() >= ConstantFirstVal always succeeds");
157     return V->getValueID() <= ConstantLastVal;
158   }
159 
160   /// This method is a special form of User::replaceUsesOfWith
161   /// (which does not work on constants) that does work
162   /// on constants.  Basically this method goes through the trouble of building
163   /// a new constant that is equivalent to the current one, with all uses of
164   /// From replaced with uses of To.  After this construction is completed, all
165   /// of the users of 'this' are replaced to use the new constant, and then
166   /// 'this' is deleted.  In general, you should not call this method, instead,
167   /// use Value::replaceAllUsesWith, which automatically dispatches to this
168   /// method as needed.
169   ///
170   void handleOperandChange(Value *, Value *);
171 
172   static Constant *getNullValue(Type* Ty);
173 
174   /// @returns the value for an integer or vector of integer constant of the
175   /// given type that has all its bits set to true.
176   /// Get the all ones value
177   static Constant *getAllOnesValue(Type* Ty);
178 
179   /// Return the value for an integer or pointer constant, or a vector thereof,
180   /// with the given scalar value.
181   static Constant *getIntegerValue(Type *Ty, const APInt &V);
182 
183   /// If there are any dead constant users dangling off of this constant, remove
184   /// them. This method is useful for clients that want to check to see if a
185   /// global is unused, but don't want to deal with potentially dead constants
186   /// hanging off of the globals.
187   void removeDeadConstantUsers() const;
188 
189   const Constant *stripPointerCasts() const {
190     return cast<Constant>(Value::stripPointerCasts());
191   }
192 
193   Constant *stripPointerCasts() {
194     return const_cast<Constant*>(
195                       static_cast<const Constant *>(this)->stripPointerCasts());
196   }
197 
198   /// Try to replace undefined constant C or undefined elements in C with
199   /// Replacement. If no changes are made, the constant C is returned.
200   static Constant *replaceUndefsWith(Constant *C, Constant *Replacement);
201 };
202 
203 } // end namespace llvm
204 
205 #endif // LLVM_IR_CONSTANT_H
206