1 //===-- llvm/Constants.h - Constant class subclass definitions ---*- C++ -*--=//
3 // This file contains the declarations for the subclasses of Constant, which
4 // represent the different type of constant pool values
6 //===----------------------------------------------------------------------===//
8 #ifndef LLVM_CONSTANTS_H
9 #define LLVM_CONSTANTS_H
11 #include "llvm/Constant.h"
12 #include "Support/DataTypes.h"
19 //===---------------------------------------------------------------------------
20 // ConstantIntegral - Shared superclass of boolean and integer constants.
22 // This class just defines some common interfaces to be implemented.
24 class ConstantIntegral : public Constant {
26 ConstantIntegral(const Type *Ty) : Constant(Ty) {}
29 // isNullValue - Return true if this is the value that would be returned by
32 virtual bool isNullValue() const = 0;
34 // isMaxValue - Return true if this is the largest value that may be
35 // represented by this type.
37 virtual bool isMaxValue() const = 0;
39 // isMinValue - Return true if this is the smallest value that may be
40 // represented by this type.
42 virtual bool isMinValue() const = 0;
44 // isAllOnesValue - Return true if every bit in this constant is set to true.
46 virtual bool isAllOnesValue() const = 0;
48 // Static constructor to get the maximum/minimum/allones constant of specified
51 static ConstantIntegral *getMaxValue(const Type *Ty);
52 static ConstantIntegral *getMinValue(const Type *Ty);
53 static ConstantIntegral *getAllOnesValue(const Type *Ty);
55 // Methods for support type inquiry through isa, cast, and dyn_cast:
56 static inline bool classof(const ConstantIntegral *) { return true; }
57 static bool classof(const Constant *CPV); // defined in Constants.cpp
58 static inline bool classof(const Value *V) {
59 return isa<Constant>(V) && classof(cast<Constant>(V));
64 //===---------------------------------------------------------------------------
65 // ConstantBool - Boolean Values
67 class ConstantBool : public ConstantIntegral {
72 static ConstantBool *True, *False; // The True & False values
74 // Factory objects - Return objects of the specified value
75 static ConstantBool *get(bool Value) { return Value ? True : False; }
76 static ConstantBool *get(const Type *Ty, bool Value) { return get(Value); }
78 // inverted - Return the opposite value of the current value.
79 inline ConstantBool *inverted() const { return (this==True) ? False : True; }
81 inline bool getValue() const { return Val; }
83 // isNullValue - Return true if this is the value that would be returned by
86 virtual bool isNullValue() const { return this == False; }
87 virtual bool isMaxValue() const { return this == True; }
88 virtual bool isMinValue() const { return this == False; }
89 virtual bool isAllOnesValue() const { return this == True; }
91 // Methods for support type inquiry through isa, cast, and dyn_cast:
92 static inline bool classof(const ConstantBool *) { return true; }
93 static bool classof(const Constant *CPV) {
94 return (CPV == True) | (CPV == False);
96 static inline bool classof(const Value *V) {
97 return isa<Constant>(V) && classof(cast<Constant>(V));
102 //===---------------------------------------------------------------------------
103 // ConstantInt - Superclass of ConstantSInt & ConstantUInt, to make dealing
104 // with integral constants easier.
106 class ConstantInt : public ConstantIntegral {
112 ConstantInt(const ConstantInt &); // DO NOT IMPLEMENT
113 ConstantInt(const Type *Ty, uint64_t V);
116 // equalsInt - Provide a helper method that can be used to determine if the
117 // constant contained within is equal to a constant. This only works for very
118 // small values, because this is all that can be represented with all types.
120 bool equalsInt(unsigned char V) const {
122 "equals: Can only be used with very small positive constants!");
123 return Val.Unsigned == V;
126 // ConstantInt::get static method: return a constant pool int with the
127 // specified value. as above, we work only with very small values here.
129 static ConstantInt *get(const Type *Ty, unsigned char V);
131 // isNullValue - Return true if this is the value that would be returned by
133 virtual bool isNullValue() const { return Val.Unsigned == 0; }
134 virtual bool isAllOnesValue() const { return Val.Signed == -1; }
135 virtual bool isMaxValue() const = 0;
136 virtual bool isMinValue() const = 0;
138 // Methods for support type inquiry through isa, cast, and dyn_cast:
139 static inline bool classof(const ConstantInt *) { return true; }
140 static bool classof(const Constant *CPV); // defined in Constants.cpp
141 static inline bool classof(const Value *V) {
142 return isa<Constant>(V) && classof(cast<Constant>(V));
147 //===---------------------------------------------------------------------------
148 // ConstantSInt - Signed Integer Values [sbyte, short, int, long]
150 class ConstantSInt : public ConstantInt {
151 ConstantSInt(const ConstantSInt &); // DO NOT IMPLEMENT
153 ConstantSInt(const Type *Ty, int64_t V);
156 static ConstantSInt *get(const Type *Ty, int64_t V);
158 static bool isValueValidForType(const Type *Ty, int64_t V);
159 inline int64_t getValue() const { return Val.Signed; }
161 // isMaxValue - Return true if this is the largest value that may be
162 // represented by this type.
164 virtual bool isMaxValue() const {
165 int64_t V = getValue();
166 if (V < 0) return false; // Be careful about wrap-around on 'long's
168 return !isValueValidForType(getType(), V) || V < 0;
171 // isMinValue - Return true if this is the smallest value that may be
172 // represented by this type.
174 virtual bool isMinValue() const {
175 int64_t V = getValue();
176 if (V > 0) return false; // Be careful about wrap-around on 'long's
178 return !isValueValidForType(getType(), V) || V > 0;
181 // Methods for support type inquiry through isa, cast, and dyn_cast:
182 static inline bool classof(const ConstantSInt *) { return true; }
183 static bool classof(const Constant *CPV); // defined in Constants.cpp
184 static inline bool classof(const Value *V) {
185 return isa<Constant>(V) && classof(cast<Constant>(V));
189 //===---------------------------------------------------------------------------
190 // ConstantUInt - Unsigned Integer Values [ubyte, ushort, uint, ulong]
192 class ConstantUInt : public ConstantInt {
193 ConstantUInt(const ConstantUInt &); // DO NOT IMPLEMENT
195 ConstantUInt(const Type *Ty, uint64_t V);
198 static ConstantUInt *get(const Type *Ty, uint64_t V);
200 static bool isValueValidForType(const Type *Ty, uint64_t V);
201 inline uint64_t getValue() const { return Val.Unsigned; }
203 // isMaxValue - Return true if this is the largest value that may be
204 // represented by this type.
206 virtual bool isMaxValue() const { return isAllOnesValue(); }
207 virtual bool isMinValue() const { return getValue() == 0; }
209 // Methods for support type inquiry through isa, cast, and dyn_cast:
210 static inline bool classof(const ConstantUInt *) { return true; }
211 static bool classof(const Constant *CPV); // defined in Constants.cpp
212 static inline bool classof(const Value *V) {
213 return isa<Constant>(V) && classof(cast<Constant>(V));
218 //===---------------------------------------------------------------------------
219 // ConstantFP - Floating Point Values [float, double]
221 class ConstantFP : public Constant {
223 ConstantFP(const ConstantFP &); // DO NOT IMPLEMENT
225 ConstantFP(const Type *Ty, double V);
228 static ConstantFP *get(const Type *Ty, double V);
230 static bool isValueValidForType(const Type *Ty, double V);
231 inline double getValue() const { return Val; }
233 // isNullValue - Return true if this is the value that would be returned by
235 virtual bool isNullValue() const { return Val == 0; }
237 // Methods for support type inquiry through isa, cast, and dyn_cast:
238 static inline bool classof(const ConstantFP *) { return true; }
239 static bool classof(const Constant *CPV); // defined in Constants.cpp
240 static inline bool classof(const Value *V) {
241 return isa<Constant>(V) && classof(cast<Constant>(V));
246 //===---------------------------------------------------------------------------
247 // ConstantArray - Constant Array Declarations
249 class ConstantArray : public Constant {
250 ConstantArray(const ConstantArray &); // DO NOT IMPLEMENT
252 ConstantArray(const ArrayType *T, const std::vector<Constant*> &Val);
255 virtual void destroyConstant();
257 static ConstantArray *get(const ArrayType *T, const std::vector<Constant*> &);
258 static ConstantArray *get(const std::string &Initializer);
260 inline const ArrayType *getType() const {
261 return (ArrayType*)Value::getType();
264 inline const std::vector<Use> &getValues() const { return Operands; }
266 // isNullValue - Return true if this is the value that would be returned by
268 virtual bool isNullValue() const { return false; }
270 // Methods for support type inquiry through isa, cast, and dyn_cast:
271 static inline bool classof(const ConstantArray *) { return true; }
272 static bool classof(const Constant *CPV); // defined in Constants.cpp
273 static inline bool classof(const Value *V) {
274 return isa<Constant>(V) && classof(cast<Constant>(V));
279 //===---------------------------------------------------------------------------
280 // ConstantStruct - Constant Struct Declarations
282 class ConstantStruct : public Constant {
283 ConstantStruct(const ConstantStruct &); // DO NOT IMPLEMENT
285 ConstantStruct(const StructType *T, const std::vector<Constant*> &Val);
288 virtual void destroyConstant();
290 static ConstantStruct *get(const StructType *T,
291 const std::vector<Constant*> &V);
293 inline const StructType *getType() const {
294 return (StructType*)Value::getType();
297 inline const std::vector<Use> &getValues() const { return Operands; }
299 // isNullValue - Return true if this is the value that would be returned by
301 virtual bool isNullValue() const { return false; }
303 // Methods for support type inquiry through isa, cast, and dyn_cast:
304 static inline bool classof(const ConstantStruct *) { return true; }
305 static bool classof(const Constant *CPV); // defined in Constants.cpp
306 static inline bool classof(const Value *V) {
307 return isa<Constant>(V) && classof(cast<Constant>(V));
311 //===---------------------------------------------------------------------------
312 // ConstantPointer - Constant Pointer Declarations
314 // The ConstantPointer class represents a null pointer of a specific type. For
315 // a more specific/useful instance, a subclass of ConstantPointer should be
318 class ConstantPointer : public Constant {
319 ConstantPointer(const ConstantPointer &); // DO NOT IMPLEMENT
321 inline ConstantPointer(const PointerType *T) : Constant((const Type*)T){}
322 ~ConstantPointer() {}
324 inline const PointerType *getType() const {
325 return (PointerType*)Value::getType();
328 // isNullValue - Return true if this is the value that would be returned by
330 virtual bool isNullValue() const { return false; }
332 // Methods for support type inquiry through isa, cast, and dyn_cast:
333 static inline bool classof(const ConstantPointer *) { return true; }
334 static bool classof(const Constant *CPV); // defined in Constants.cpp
335 static inline bool classof(const Value *V) {
336 return isa<Constant>(V) && classof(cast<Constant>(V));
340 // ConstantPointerNull - a constant pointer value that points to null
342 class ConstantPointerNull : public ConstantPointer {
343 ConstantPointerNull(const ConstantPointerNull &); // DO NOT IMPLEMENT
345 inline ConstantPointerNull(const PointerType *T) : ConstantPointer(T) {}
346 inline ~ConstantPointerNull() {}
349 static ConstantPointerNull *get(const PointerType *T);
351 // isNullValue - Return true if this is the value that would be returned by
353 virtual bool isNullValue() const { return true; }
355 // Methods for support type inquiry through isa, cast, and dyn_cast:
356 static inline bool classof(const ConstantPointerNull *) { return true; }
357 static inline bool classof(const ConstantPointer *P) {
358 return (P->getNumOperands() == 0 && P->isNullValue());
360 static inline bool classof(const Constant *CPV) {
361 return isa<ConstantPointer>(CPV) && classof(cast<ConstantPointer>(CPV));
363 static inline bool classof(const Value *V) {
364 return isa<ConstantPointer>(V) && classof(cast<ConstantPointer>(V));
369 // ConstantPointerRef - a constant pointer value that is initialized to
370 // point to a global value, which lies at a constant, fixed address.
372 class ConstantPointerRef : public ConstantPointer {
373 friend class Module; // Modules maintain these references
374 ConstantPointerRef(const ConstantPointerRef &); // DNI!
377 ConstantPointerRef(GlobalValue *GV);
378 ~ConstantPointerRef() {}
380 virtual void destroyConstant() { destroyConstantImpl(); }
382 static ConstantPointerRef *get(GlobalValue *GV);
384 const GlobalValue *getValue() const {
385 return cast<GlobalValue>(Operands[0].get());
387 GlobalValue *getValue() {
388 return cast<GlobalValue>(Operands[0].get());
391 // Methods for support type inquiry through isa, cast, and dyn_cast:
392 static inline bool classof(const ConstantPointerRef *) { return true; }
393 static inline bool classof(const ConstantPointer *CPV) {
394 // check for a single operand (the target value)
395 return (CPV->getNumOperands() == 1);
397 static inline bool classof(const Constant *CPV) {
398 return isa<ConstantPointer>(CPV) && classof(cast<ConstantPointer>(CPV));
400 static inline bool classof(const Value *V) {
401 return isa<ConstantPointer>(V) && classof(cast<ConstantPointer>(V));
404 // WARNING: Only to be used by Bytecode & Assembly Parsers! USER CODE SHOULD
406 // Returns the number of uses of OldV that were replaced.
407 virtual unsigned mutateReferences(Value* OldV, Value *NewV);
412 // ConstantExpr - a constant value that is initialized with
413 // an expression using other constant values. This is only used
414 // to represent values that cannot be evaluated at compile-time
415 // (e.g., something derived from an address) because it does
416 // not have a mechanism to store the actual value.
417 // Use the appropriate Constant subclass above for known constants.
419 class ConstantExpr : public Constant {
420 unsigned iType; // Operation type
423 ConstantExpr(unsigned Opcode, Constant *C, const Type *Ty);
424 ConstantExpr(unsigned Opcode, Constant *C1, Constant *C2);
425 ConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
429 virtual void destroyConstant();
432 // Static methods to construct a ConstantExpr of different kinds.
434 // Cast constant expr
435 static ConstantExpr *getCast(Constant *C, const Type *Ty);
437 // Binary constant expr - Use with binary operators...
438 static ConstantExpr *get(unsigned Opcode, Constant *C1, Constant *C2);
440 // Getelementptr form...
441 static ConstantExpr *getGetElementPtr(Constant *C,
442 const std::vector<Constant*> &IdxList);
444 // isNullValue - Return true if this is the value that would be returned by
446 virtual bool isNullValue() const { return false; }
448 // getOpcode - Return the opcode at the root of this constant expression
449 unsigned getOpcode() const { return iType; }
451 // getOpcodeName - Return a string representation for an opcode.
452 const char *getOpcodeName() const;
454 // isConstantExpr - Return true if this is a ConstantExpr
455 virtual bool isConstantExpr() const { return true; }
457 // Methods for support type inquiry through isa, cast, and dyn_cast:
458 static inline bool classof(const ConstantExpr *) { return true; }
459 static inline bool classof(const Constant *CPV) {
460 return CPV->isConstantExpr();
462 static inline bool classof(const Value *V) {
463 return isa<Constant>(V) && classof(cast<Constant>(V));
467 // WARNING: Only to be used by Bytecode & Assembly Parsers! USER CODE SHOULD
469 // Returns the number of uses of OldV that were replaced.
470 virtual unsigned mutateReferences(Value* OldV, Value *NewV);