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);
256 static ConstantArray *get(const ArrayType *T, const std::vector<Constant*> &);
257 static ConstantArray *get(const std::string &Initializer);
259 inline const ArrayType *getType() const {
260 return (ArrayType*)Value::getType();
263 inline const std::vector<Use> &getValues() const { return Operands; }
265 // isNullValue - Return true if this is the value that would be returned by
267 virtual bool isNullValue() const { return false; }
269 virtual void destroyConstant();
271 // Methods for support type inquiry through isa, cast, and dyn_cast:
272 static inline bool classof(const ConstantArray *) { return true; }
273 static bool classof(const Constant *CPV); // defined in Constants.cpp
274 static inline bool classof(const Value *V) {
275 return isa<Constant>(V) && classof(cast<Constant>(V));
280 //===---------------------------------------------------------------------------
281 // ConstantStruct - Constant Struct Declarations
283 class ConstantStruct : public Constant {
284 ConstantStruct(const ConstantStruct &); // DO NOT IMPLEMENT
286 ConstantStruct(const StructType *T, const std::vector<Constant*> &Val);
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 virtual void destroyConstant();
305 // Methods for support type inquiry through isa, cast, and dyn_cast:
306 static inline bool classof(const ConstantStruct *) { return true; }
307 static bool classof(const Constant *CPV); // defined in Constants.cpp
308 static inline bool classof(const Value *V) {
309 return isa<Constant>(V) && classof(cast<Constant>(V));
313 //===---------------------------------------------------------------------------
314 // ConstantPointer - Constant Pointer Declarations
316 // The ConstantPointer class represents a null pointer of a specific type. For
317 // a more specific/useful instance, a subclass of ConstantPointer should be
320 class ConstantPointer : public Constant {
321 ConstantPointer(const ConstantPointer &); // DO NOT IMPLEMENT
323 inline ConstantPointer(const PointerType *T) : Constant((const Type*)T){}
324 ~ConstantPointer() {}
326 inline const PointerType *getType() const {
327 return (PointerType*)Value::getType();
330 // isNullValue - Return true if this is the value that would be returned by
332 virtual bool isNullValue() const { return false; }
334 // Methods for support type inquiry through isa, cast, and dyn_cast:
335 static inline bool classof(const ConstantPointer *) { return true; }
336 static bool classof(const Constant *CPV); // defined in Constants.cpp
337 static inline bool classof(const Value *V) {
338 return isa<Constant>(V) && classof(cast<Constant>(V));
342 // ConstantPointerNull - a constant pointer value that points to null
344 class ConstantPointerNull : public ConstantPointer {
345 ConstantPointerNull(const ConstantPointerNull &); // DO NOT IMPLEMENT
347 inline ConstantPointerNull(const PointerType *T) : ConstantPointer(T) {}
348 inline ~ConstantPointerNull() {}
351 static ConstantPointerNull *get(const PointerType *T);
353 // isNullValue - Return true if this is the value that would be returned by
355 virtual bool isNullValue() const { return true; }
357 virtual void destroyConstant();
359 // Methods for support type inquiry through isa, cast, and dyn_cast:
360 static inline bool classof(const ConstantPointerNull *) { return true; }
361 static inline bool classof(const ConstantPointer *P) {
362 return (P->getNumOperands() == 0 && P->isNullValue());
364 static inline bool classof(const Constant *CPV) {
365 return isa<ConstantPointer>(CPV) && classof(cast<ConstantPointer>(CPV));
367 static inline bool classof(const Value *V) {
368 return isa<ConstantPointer>(V) && classof(cast<ConstantPointer>(V));
373 // ConstantPointerRef - a constant pointer value that is initialized to
374 // point to a global value, which lies at a constant, fixed address.
376 class ConstantPointerRef : public ConstantPointer {
377 friend class Module; // Modules maintain these references
378 ConstantPointerRef(const ConstantPointerRef &); // DNI!
381 ConstantPointerRef(GlobalValue *GV);
382 ~ConstantPointerRef() {}
384 static ConstantPointerRef *get(GlobalValue *GV);
386 const GlobalValue *getValue() const {
387 return cast<GlobalValue>(Operands[0].get());
389 GlobalValue *getValue() {
390 return cast<GlobalValue>(Operands[0].get());
393 virtual void destroyConstant();
395 // Methods for support type inquiry through isa, cast, and dyn_cast:
396 static inline bool classof(const ConstantPointerRef *) { return true; }
397 static inline bool classof(const ConstantPointer *CPV) {
398 // check for a single operand (the target value)
399 return (CPV->getNumOperands() == 1);
401 static inline bool classof(const Constant *CPV) {
402 return isa<ConstantPointer>(CPV) && classof(cast<ConstantPointer>(CPV));
404 static inline bool classof(const Value *V) {
405 return isa<ConstantPointer>(V) && classof(cast<ConstantPointer>(V));
408 // WARNING: Only to be used by Bytecode & Assembly Parsers! USER CODE SHOULD
410 // Returns the number of uses of OldV that were replaced.
411 virtual unsigned mutateReferences(Value* OldV, Value *NewV);
416 // ConstantExpr - a constant value that is initialized with
417 // an expression using other constant values. This is only used
418 // to represent values that cannot be evaluated at compile-time
419 // (e.g., something derived from an address) because it does
420 // not have a mechanism to store the actual value.
421 // Use the appropriate Constant subclass above for known constants.
423 class ConstantExpr : public Constant {
424 unsigned iType; // Operation type
427 ConstantExpr(unsigned Opcode, Constant *C, const Type *Ty);
428 ConstantExpr(unsigned Opcode, Constant *C1, Constant *C2);
429 ConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
434 // Static methods to construct a ConstantExpr of different kinds.
436 // Cast constant expr
437 static ConstantExpr *getCast(Constant *C, const Type *Ty);
439 // Binary constant expr - Use with binary operators...
440 static ConstantExpr *get(unsigned Opcode, Constant *C1, Constant *C2);
442 // Getelementptr form...
443 static ConstantExpr *getGetElementPtr(Constant *C,
444 const std::vector<Constant*> &IdxList);
446 // isNullValue - Return true if this is the value that would be returned by
448 virtual bool isNullValue() const { return false; }
450 // getOpcode - Return the opcode at the root of this constant expression
451 unsigned getOpcode() const { return iType; }
453 // getOpcodeName - Return a string representation for an opcode.
454 const char *getOpcodeName() const;
456 // isConstantExpr - Return true if this is a ConstantExpr
457 virtual bool isConstantExpr() const { return true; }
459 virtual void destroyConstant();
461 // Methods for support type inquiry through isa, cast, and dyn_cast:
462 static inline bool classof(const ConstantExpr *) { return true; }
463 static inline bool classof(const Constant *CPV) {
464 return CPV->isConstantExpr();
466 static inline bool classof(const Value *V) {
467 return isa<Constant>(V) && classof(cast<Constant>(V));
471 // WARNING: Only to be used by Bytecode & Assembly Parsers! USER CODE SHOULD
473 // Returns the number of uses of OldV that were replaced.
474 virtual unsigned mutateReferences(Value* OldV, Value *NewV);