* Standardize how analysis results/passes as printed with the print() virtual
[oota-llvm.git] / include / llvm / Constants.h
1 //===-- llvm/Constants.h - Constant class subclass definitions ---*- C++ -*--=//
2 //
3 // This file contains the declarations for the subclasses of Constant, which
4 // represent the different type of constant pool values
5 //
6 //===----------------------------------------------------------------------===//
7
8 #ifndef LLVM_CONSTANTS_H
9 #define LLVM_CONSTANTS_H
10
11 #include "llvm/Constant.h"
12 #include "Support/DataTypes.h"
13
14
15 class ArrayType;
16 class StructType;
17 class PointerType;
18 class ConstantExpr;
19
20 //===---------------------------------------------------------------------------
21 // ConstantBool - Boolean Values
22 //
23 class ConstantBool : public Constant {
24   bool Val;
25   ConstantBool(bool V);
26   ~ConstantBool() {}
27 public:
28   static ConstantBool *True, *False;  // The True & False values
29
30   // Factory objects - Return objects of the specified value
31   static ConstantBool *get(bool Value) { return Value ? True : False; }
32   static ConstantBool *get(const Type *Ty, bool Value) { return get(Value); }
33
34   // inverted - Return the opposite value of the current value.
35   inline ConstantBool *inverted() const { return (this==True) ? False : True; }
36
37   inline bool getValue() const { return Val; }
38
39   // isNullValue - Return true if this is the value that would be returned by
40   // getNullValue.
41   virtual bool isNullValue() const { return this == False; }
42
43   // Methods for support type inquiry through isa, cast, and dyn_cast:
44   static inline bool classof(const ConstantBool *) { return true; }
45   static bool classof(const Constant *CPV) {
46     return (CPV == True) | (CPV == False);
47   }
48   static inline bool classof(const Value *V) {
49     return isa<Constant>(V) && classof(cast<Constant>(V));
50   }
51 };
52
53
54 //===---------------------------------------------------------------------------
55 // ConstantInt - Superclass of ConstantSInt & ConstantUInt, to make dealing
56 // with integral constants easier.
57 //
58 class ConstantInt : public Constant {
59 protected:
60   union {
61     int64_t  Signed;
62     uint64_t Unsigned;
63   } Val;
64   ConstantInt(const ConstantInt &);      // DO NOT IMPLEMENT
65   ConstantInt(const Type *Ty, uint64_t V);
66   ~ConstantInt() {}
67 public:
68   // equalsInt - Provide a helper method that can be used to determine if the 
69   // constant contained within is equal to a constant.  This only works for very
70   // small values, because this is all that can be represented with all types.
71   //
72   bool equalsInt(unsigned char V) const {
73     assert(V <= 127 &&
74            "equals: Can only be used with very small positive constants!");
75     return Val.Unsigned == V;
76   }
77
78   // ConstantInt::get static method: return a constant pool int with the
79   // specified value.  as above, we work only with very small values here.
80   //
81   static ConstantInt *get(const Type *Ty, unsigned char V);
82
83   // isNullValue - Return true if this is the value that would be returned by
84   // getNullValue.
85   virtual bool isNullValue() const { return Val.Unsigned == 0; }
86
87   // Methods for support type inquiry through isa, cast, and dyn_cast:
88   static inline bool classof(const ConstantInt *) { return true; }
89   static bool classof(const Constant *CPV);  // defined in CPV.cpp
90   static inline bool classof(const Value *V) {
91     return isa<Constant>(V) && classof(cast<Constant>(V));
92   }
93 };
94
95
96 //===---------------------------------------------------------------------------
97 // ConstantSInt - Signed Integer Values [sbyte, short, int, long]
98 //
99 class ConstantSInt : public ConstantInt {
100   ConstantSInt(const ConstantSInt &);      // DO NOT IMPLEMENT
101 protected:
102   ConstantSInt(const Type *Ty, int64_t V);
103   ~ConstantSInt() {}
104 public:
105   static ConstantSInt *get(const Type *Ty, int64_t V);
106
107   static bool isValueValidForType(const Type *Ty, int64_t V);
108   inline int64_t getValue() const { return Val.Signed; }
109
110   // Methods for support type inquiry through isa, cast, and dyn_cast:
111   static inline bool classof(const ConstantSInt *) { return true; }
112   static bool classof(const Constant *CPV);  // defined in CPV.cpp
113   static inline bool classof(const Value *V) {
114     return isa<Constant>(V) && classof(cast<Constant>(V));
115   }
116 };
117
118 //===---------------------------------------------------------------------------
119 // ConstantUInt - Unsigned Integer Values [ubyte, ushort, uint, ulong]
120 //
121 class ConstantUInt : public ConstantInt {
122   ConstantUInt(const ConstantUInt &);      // DO NOT IMPLEMENT
123 protected:
124   ConstantUInt(const Type *Ty, uint64_t V);
125   ~ConstantUInt() {}
126 public:
127   static ConstantUInt *get(const Type *Ty, uint64_t V);
128
129   static bool isValueValidForType(const Type *Ty, uint64_t V);
130   inline uint64_t getValue() const { return Val.Unsigned; }
131
132   // Methods for support type inquiry through isa, cast, and dyn_cast:
133   static inline bool classof(const ConstantUInt *) { return true; }
134   static bool classof(const Constant *CPV);  // defined in CPV.cpp
135   static inline bool classof(const Value *V) {
136     return isa<Constant>(V) && classof(cast<Constant>(V));
137   }
138 };
139
140
141 //===---------------------------------------------------------------------------
142 // ConstantFP - Floating Point Values [float, double]
143 //
144 class ConstantFP : public Constant {
145   double Val;
146   ConstantFP(const ConstantFP &);      // DO NOT IMPLEMENT
147 protected:
148   ConstantFP(const Type *Ty, double V);
149   ~ConstantFP() {}
150 public:
151   static ConstantFP *get(const Type *Ty, double V);
152
153   static bool isValueValidForType(const Type *Ty, double V);
154   inline double getValue() const { return Val; }
155
156   // isNullValue - Return true if this is the value that would be returned by
157   // getNullValue.
158   virtual bool isNullValue() const { return Val == 0; }
159
160   // Methods for support type inquiry through isa, cast, and dyn_cast:
161   static inline bool classof(const ConstantFP *) { return true; }
162   static bool classof(const Constant *CPV);  // defined in CPV.cpp
163   static inline bool classof(const Value *V) {
164     return isa<Constant>(V) && classof(cast<Constant>(V));
165   }
166 };
167
168
169 //===---------------------------------------------------------------------------
170 // ConstantArray - Constant Array Declarations
171 //
172 class ConstantArray : public Constant {
173   ConstantArray(const ConstantArray &);      // DO NOT IMPLEMENT
174 protected:
175   ConstantArray(const ArrayType *T, const std::vector<Constant*> &Val);
176   ~ConstantArray() {}
177
178   virtual void destroyConstant();
179 public:
180   static ConstantArray *get(const ArrayType *T, const std::vector<Constant*> &);
181   static ConstantArray *get(const std::string &Initializer);
182   
183   inline const ArrayType *getType() const {
184     return (ArrayType*)Value::getType();
185   }
186
187   inline const std::vector<Use> &getValues() const { return Operands; }
188
189   // isNullValue - Return true if this is the value that would be returned by
190   // getNullValue.
191   virtual bool isNullValue() const { return false; }
192
193   // Methods for support type inquiry through isa, cast, and dyn_cast:
194   static inline bool classof(const ConstantArray *) { return true; }
195   static bool classof(const Constant *CPV);  // defined in CPV.cpp
196   static inline bool classof(const Value *V) {
197     return isa<Constant>(V) && classof(cast<Constant>(V));
198   }
199 };
200
201
202 //===---------------------------------------------------------------------------
203 // ConstantStruct - Constant Struct Declarations
204 //
205 class ConstantStruct : public Constant {
206   ConstantStruct(const ConstantStruct &);      // DO NOT IMPLEMENT
207 protected:
208   ConstantStruct(const StructType *T, const std::vector<Constant*> &Val);
209   ~ConstantStruct() {}
210
211   virtual void destroyConstant();
212 public:
213   static ConstantStruct *get(const StructType *T,
214                              const std::vector<Constant*> &V);
215
216   inline const StructType *getType() const {
217     return (StructType*)Value::getType();
218   }
219
220   inline const std::vector<Use> &getValues() const { return Operands; }
221
222   // isNullValue - Return true if this is the value that would be returned by
223   // getNullValue.
224   virtual bool isNullValue() const { return false; }
225
226   // Methods for support type inquiry through isa, cast, and dyn_cast:
227   static inline bool classof(const ConstantStruct *) { return true; }
228   static bool classof(const Constant *CPV);  // defined in CPV.cpp
229   static inline bool classof(const Value *V) {
230     return isa<Constant>(V) && classof(cast<Constant>(V));
231   }
232 };
233
234 //===---------------------------------------------------------------------------
235 // ConstantPointer - Constant Pointer Declarations
236 //
237 // The ConstantPointer class represents a null pointer of a specific type. For
238 // a more specific/useful instance, a subclass of ConstantPointer should be
239 // used.
240 //
241 class ConstantPointer : public Constant {
242   ConstantPointer(const ConstantPointer &);      // DO NOT IMPLEMENT
243 protected:
244   inline ConstantPointer(const PointerType *T) : Constant((const Type*)T){}
245   ~ConstantPointer() {}
246 public:
247   inline const PointerType *getType() const {
248     return (PointerType*)Value::getType();
249   }
250
251   // isNullValue - Return true if this is the value that would be returned by
252   // getNullValue.
253   virtual bool isNullValue() const { return false; }
254
255   // Methods for support type inquiry through isa, cast, and dyn_cast:
256   static inline bool classof(const ConstantPointer *) { return true; }
257   static bool classof(const Constant *CPV);  // defined in Constants.cpp
258   static inline bool classof(const Value *V) {
259     return isa<Constant>(V) && classof(cast<Constant>(V));
260   }
261 };
262
263 // ConstantPointerNull - a constant pointer value that points to null
264 //
265 class ConstantPointerNull : public ConstantPointer {
266   ConstantPointerNull(const ConstantPointerNull &);      // DO NOT IMPLEMENT
267 protected:
268   inline ConstantPointerNull(const PointerType *T) : ConstantPointer(T) {}
269   inline ~ConstantPointerNull() {}
270 public:
271
272   static ConstantPointerNull *get(const PointerType *T);
273
274   // isNullValue - Return true if this is the value that would be returned by
275   // getNullValue.
276   virtual bool isNullValue() const { return true; }
277
278   // Methods for support type inquiry through isa, cast, and dyn_cast:
279   static inline bool classof(const ConstantPointerNull *) { return true; }
280   static inline bool classof(const ConstantPointer *P) {
281     return (P->getNumOperands() == 0 && P->isNullValue());
282   }
283   static inline bool classof(const Constant *CPV) {
284     return isa<ConstantPointer>(CPV) && classof(cast<ConstantPointer>(CPV));
285   }
286   static inline bool classof(const Value *V) {
287     return isa<ConstantPointer>(V) && classof(cast<ConstantPointer>(V));
288   }
289 };
290
291
292 // ConstantPointerRef - a constant pointer value that is initialized to
293 // point to a global value, which lies at a constant, fixed address.
294 //
295 class ConstantPointerRef : public ConstantPointer {
296   friend class Module;   // Modules maintain these references
297   ConstantPointerRef(const ConstantPointerRef &); // DNI!
298
299 protected:
300   ConstantPointerRef(GlobalValue *GV);
301   ~ConstantPointerRef() {}
302
303   virtual void destroyConstant() { destroyConstantImpl(); }
304 public:
305   static ConstantPointerRef *get(GlobalValue *GV);
306
307   const GlobalValue *getValue() const { 
308     return cast<GlobalValue>(Operands[0].get());
309   }
310   GlobalValue *getValue() {
311     return cast<GlobalValue>(Operands[0].get());
312   }
313
314   // Methods for support type inquiry through isa, cast, and dyn_cast:
315   static inline bool classof(const ConstantPointerRef *) { return true; }
316   static inline bool classof(const ConstantPointer *CPV) {
317     // check for a single operand (the target value)
318     return (CPV->getNumOperands() == 1);
319   }
320   static inline bool classof(const Constant *CPV) {
321     return isa<ConstantPointer>(CPV) && classof(cast<ConstantPointer>(CPV));
322   }
323   static inline bool classof(const Value *V) {
324     return isa<ConstantPointer>(V) && classof(cast<ConstantPointer>(V));
325   }
326
327   // WARNING: Only to be used by Bytecode & Assembly Parsers!  USER CODE SHOULD
328   // NOT USE THIS!!
329   // Returns the number of uses of OldV that were replaced.
330   virtual unsigned mutateReferences(Value* OldV, Value *NewV);
331   // END WARNING!!
332 };
333
334
335 // ConstantExpr - a constant value that is initialized with
336 // an expression using other constant values.  This is only used
337 // to represent values that cannot be evaluated at compile-time
338 // (e.g., something derived from an address) because it does
339 // not have a mechanism to store the actual value.
340 // Use the appropriate Constant subclass above for known constants.
341 //
342 class ConstantExpr : public Constant {
343   unsigned iType;      // operation type
344   
345 protected:
346   ConstantExpr(unsigned opCode, Constant *C,  const Type *Ty);
347   ConstantExpr(unsigned opCode, Constant* C1, Constant* C2, const Type *Ty);
348   ConstantExpr(unsigned opCode, Constant* C,
349                const std::vector<Constant*> &IdxList, const Type *Ty);
350   ~ConstantExpr() {}
351   
352   virtual void destroyConstant();
353   
354 public:
355   // Static methods to construct a ConstantExpr of different kinds.
356   static ConstantExpr *get(unsigned Opcode, Constant *C, const Type *Ty);
357   static ConstantExpr *get(unsigned Opcode,
358                            Constant *C1, Constant *C2, const Type *Ty);
359   static ConstantExpr *get(unsigned Opcode, Constant *C,
360                            const std::vector<Constant*> &IdxList,
361                            const Type *Ty);
362   
363   // isNullValue - Return true if this is the value that would be returned by
364   // getNullValue.
365   virtual bool isNullValue() const { return false; }
366   
367   // getOpcode - Return the opcode at the root of this constant expression
368   unsigned getOpcode() const { return iType; }
369
370   // getOpcodeName - Return a string representation for an opcode.
371   static const char *getOpcodeName(unsigned opCode);
372   const char *getOpcodeName() const {
373     return getOpcodeName(getOpcode());
374   }
375   
376   // isConstantExpr - Return true if this is a ConstantExpr
377   virtual bool isConstantExpr() const { return true; }
378   
379   // Methods for support type inquiry through isa, cast, and dyn_cast:
380   static inline bool classof(const ConstantExpr *) { return true; }
381   static inline bool classof(const Constant *CPV) {
382     return CPV->isConstantExpr();
383   }
384   static inline bool classof(const Value *V) {
385     return isa<Constant>(V) && classof(cast<Constant>(V));
386   }
387
388 public:
389   // WARNING: Only to be used by Bytecode & Assembly Parsers!  USER CODE SHOULD
390   // NOT USE THIS!!
391   // Returns the number of uses of OldV that were replaced.
392   virtual unsigned mutateReferences(Value* OldV, Value *NewV);
393   // END WARNING!!
394 };
395
396
397 #endif