Expose new print methods
[oota-llvm.git] / include / llvm / Constants.h
1 //===-- llvm/Constants.h - Constant class subclass definitions --*- C++ -*-===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains the declarations for the subclasses of Constant, which
11 // represent the different type of constant pool values
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_CONSTANTS_H
16 #define LLVM_CONSTANTS_H
17
18 #include "llvm/Constant.h"
19 #include "Support/DataTypes.h"
20
21 class ArrayType;
22 class StructType;
23 class PointerType;
24
25 template<class ConstantClass, class TypeClass, class ValType>
26 struct ConstantCreator;
27 template<class ConstantClass, class TypeClass>
28 struct ConvertConstantType;
29
30
31 //===---------------------------------------------------------------------------
32 /// ConstantIntegral - Shared superclass of boolean and integer constants.
33 ///
34 /// This class just defines some common interfaces to be implemented.
35 ///
36 class ConstantIntegral : public Constant {
37 protected:
38   ConstantIntegral(const Type *Ty) : Constant(Ty) {}
39 public:
40
41   /// isNullValue - Return true if this is the value that would be returned by
42   /// getNullValue.
43   ///
44   virtual bool isNullValue() const = 0;
45
46   /// isMaxValue - Return true if this is the largest value that may be
47   /// represented by this type.
48   ///
49   virtual bool isMaxValue() const = 0;
50
51   /// isMinValue - Return true if this is the smallest value that may be
52   /// represented by this type.
53   ///
54   virtual bool isMinValue() const = 0;
55
56   /// isAllOnesValue - Return true if every bit in this constant is set to true.
57   ///
58   virtual bool isAllOnesValue() const = 0;
59
60   /// Static constructor to get the maximum/minimum/allones constant of
61   /// specified (integral) type...
62   ///
63   static ConstantIntegral *getMaxValue(const Type *Ty);
64   static ConstantIntegral *getMinValue(const Type *Ty);
65   static ConstantIntegral *getAllOnesValue(const Type *Ty);
66
67   /// Methods for support type inquiry through isa, cast, and dyn_cast:
68   static inline bool classof(const ConstantIntegral *) { return true; }
69   static bool classof(const Constant *CPV);  // defined in Constants.cpp
70   static inline bool classof(const Value *V) {
71     return isa<Constant>(V) && classof(cast<Constant>(V));
72   }
73 };
74
75
76 //===---------------------------------------------------------------------------
77 /// ConstantBool - Boolean Values
78 ///
79 class ConstantBool : public ConstantIntegral {
80   bool Val;
81   ConstantBool(bool V);
82 public:
83   static ConstantBool *True, *False;  // The True & False values
84
85   /// get() - Static factory methods - Return objects of the specified value
86   static ConstantBool *get(bool Value) { return Value ? True : False; }
87   static ConstantBool *get(const Type *Ty, bool Value) { return get(Value); }
88
89   /// inverted - Return the opposite value of the current value.
90   inline ConstantBool *inverted() const { return (this==True) ? False : True; }
91
92   /// getValue - return the boolean value of this constant.
93   ///
94   inline bool getValue() const { return Val; }
95
96   /// isNullValue - Return true if this is the value that would be returned by
97   /// getNullValue.
98   ///
99   virtual bool isNullValue() const { return this == False; }
100   virtual bool isMaxValue() const { return this == True; }
101   virtual bool isMinValue() const { return this == False; }
102   virtual bool isAllOnesValue() const { return this == True; }
103
104   /// Methods for support type inquiry through isa, cast, and dyn_cast:
105   static inline bool classof(const ConstantBool *) { return true; }
106   static bool classof(const Constant *CPV) {
107     return (CPV == True) | (CPV == False);
108   }
109   static inline bool classof(const Value *V) {
110     return isa<Constant>(V) && classof(cast<Constant>(V));
111   }
112 };
113
114
115 //===---------------------------------------------------------------------------
116 /// ConstantInt - Superclass of ConstantSInt & ConstantUInt, to make dealing
117 /// with integral constants easier.
118 ///
119 class ConstantInt : public ConstantIntegral {
120 protected:
121   union {
122     int64_t  Signed;
123     uint64_t Unsigned;
124   } Val;
125   ConstantInt(const ConstantInt &);      // DO NOT IMPLEMENT
126   ConstantInt(const Type *Ty, uint64_t V);
127 public:
128   /// equalsInt - Provide a helper method that can be used to determine if the
129   /// constant contained within is equal to a constant.  This only works for
130   /// very small values, because this is all that can be represented with all
131   /// types.
132   ///
133   bool equalsInt(unsigned char V) const {
134     assert(V <= 127 &&
135            "equals: Can only be used with very small positive constants!");
136     return Val.Unsigned == V;
137   }
138
139   /// ConstantInt::get static method: return a ConstantInt with the specified
140   /// value.  as above, we work only with very small values here.
141   ///
142   static ConstantInt *get(const Type *Ty, unsigned char V);
143
144   /// getRawValue - return the underlying value of this constant as a 64-bit
145   /// unsigned integer value.
146   ///
147   inline uint64_t getRawValue() const { return Val.Unsigned; }
148
149   /// isNullValue - Return true if this is the value that would be returned by
150   /// getNullValue.
151   virtual bool isNullValue() const { return Val.Unsigned == 0; }
152   virtual bool isMaxValue() const = 0;
153   virtual bool isMinValue() const = 0;
154
155   /// Methods for support type inquiry through isa, cast, and dyn_cast:
156   static inline bool classof(const ConstantInt *) { return true; }
157   static bool classof(const Constant *CPV);  // defined in Constants.cpp
158   static inline bool classof(const Value *V) {
159     return isa<Constant>(V) && classof(cast<Constant>(V));
160   }
161 };
162
163
164 //===---------------------------------------------------------------------------
165 /// ConstantSInt - Signed Integer Values [sbyte, short, int, long]
166 ///
167 class ConstantSInt : public ConstantInt {
168   ConstantSInt(const ConstantSInt &);      // DO NOT IMPLEMENT
169   friend struct ConstantCreator<ConstantSInt, Type, int64_t>;
170
171 protected:
172   ConstantSInt(const Type *Ty, int64_t V);
173 public:
174   /// get() - Static factory methods - Return objects of the specified value
175   ///
176   static ConstantSInt *get(const Type *Ty, int64_t V);
177
178   /// isValueValidForType - return true if Ty is big enough to represent V.
179   ///
180   static bool isValueValidForType(const Type *Ty, int64_t V);
181
182   /// getValue - return the underlying value of this constant.
183   ///
184   inline int64_t getValue() const { return Val.Signed; }
185
186   virtual bool isAllOnesValue() const { return getValue() == -1; }
187
188   /// isMaxValue - Return true if this is the largest value that may be
189   /// represented by this type.
190   ///
191   virtual bool isMaxValue() const {
192     int64_t V = getValue();
193     if (V < 0) return false;    // Be careful about wrap-around on 'long's
194     ++V;
195     return !isValueValidForType(getType(), V) || V < 0;
196   }
197
198   /// isMinValue - Return true if this is the smallest value that may be
199   /// represented by this type.
200   ///
201   virtual bool isMinValue() const {
202     int64_t V = getValue();
203     if (V > 0) return false;    // Be careful about wrap-around on 'long's
204     --V;
205     return !isValueValidForType(getType(), V) || V > 0;
206   }
207
208   /// Methods for support type inquiry through isa, cast, and dyn_cast:
209   ///
210   static inline bool classof(const ConstantSInt *) { 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));
214   }
215 };
216
217 //===---------------------------------------------------------------------------
218 /// ConstantUInt - Unsigned Integer Values [ubyte, ushort, uint, ulong]
219 ///
220 class ConstantUInt : public ConstantInt {
221   ConstantUInt(const ConstantUInt &);      // DO NOT IMPLEMENT
222   friend struct ConstantCreator<ConstantUInt, Type, uint64_t>;
223 protected:
224   ConstantUInt(const Type *Ty, uint64_t V);
225 public:
226   /// get() - Static factory methods - Return objects of the specified value
227   ///
228   static ConstantUInt *get(const Type *Ty, uint64_t V);
229
230   /// isValueValidForType - return true if Ty is big enough to represent V.
231   ///
232   static bool isValueValidForType(const Type *Ty, uint64_t V);
233
234   /// getValue - return the underlying value of this constant.
235   ///
236   inline uint64_t getValue() const { return Val.Unsigned; }
237
238   /// isMaxValue - Return true if this is the largest value that may be
239   /// represented by this type.
240   ///
241   virtual bool isAllOnesValue() const;
242   virtual bool isMaxValue() const { return isAllOnesValue(); }
243   virtual bool isMinValue() const { return getValue() == 0; }
244
245   /// Methods for support type inquiry through isa, cast, and dyn_cast:
246   static inline bool classof(const ConstantUInt *) { return true; }
247   static bool classof(const Constant *CPV);  // defined in Constants.cpp
248   static inline bool classof(const Value *V) {
249     return isa<Constant>(V) && classof(cast<Constant>(V));
250   }
251 };
252
253
254 //===---------------------------------------------------------------------------
255 /// ConstantFP - Floating Point Values [float, double]
256 ///
257 class ConstantFP : public Constant {
258   double Val;
259   friend struct ConstantCreator<ConstantFP, Type, double>;
260   ConstantFP(const ConstantFP &);      // DO NOT IMPLEMENT
261 protected:
262   ConstantFP(const Type *Ty, double V);
263 public:
264   /// get() - Static factory methods - Return objects of the specified value
265   static ConstantFP *get(const Type *Ty, double V);
266
267   /// isValueValidForType - return true if Ty is big enough to represent V.
268   static bool isValueValidForType(const Type *Ty, double V);
269   inline double getValue() const { return Val; }
270
271   /// isNullValue - Return true if this is the value that would be returned by
272   /// getNullValue.
273   virtual bool isNullValue() const { return Val == 0; }
274
275   /// Methods for support type inquiry through isa, cast, and dyn_cast:
276   static inline bool classof(const ConstantFP *) { return true; }
277   static bool classof(const Constant *CPV);  // defined in Constants.cpp
278   static inline bool classof(const Value *V) {
279     return isa<Constant>(V) && classof(cast<Constant>(V));
280   }
281 };
282
283
284 //===---------------------------------------------------------------------------
285 /// ConstantArray - Constant Array Declarations
286 ///
287 class ConstantArray : public Constant {
288   friend struct ConstantCreator<ConstantArray, ArrayType,
289                                     std::vector<Constant*> >;
290   ConstantArray(const ConstantArray &);      // DO NOT IMPLEMENT
291 protected:
292   ConstantArray(const ArrayType *T, const std::vector<Constant*> &Val);
293 public:
294   /// get() - Static factory methods - Return objects of the specified value
295   static ConstantArray *get(const ArrayType *T, const std::vector<Constant*> &);
296   static ConstantArray *get(const std::string &Initializer);
297   
298   /// getType - Specialize the getType() method to always return an ArrayType,
299   /// which reduces the amount of casting needed in parts of the compiler.
300   ///
301   inline const ArrayType *getType() const {
302     return (ArrayType*)Value::getType();
303   }
304
305   /// getAsString - If the sub-element type of this array is either sbyte or
306   /// ubyte, then this method converts the array to an std::string and returns
307   /// it.  Otherwise, it asserts out.
308   ///
309   std::string getAsString() const;
310
311   /// getValues - Return a vector of the component constants that make up this
312   /// array.
313   inline const std::vector<Use> &getValues() const { return Operands; }
314
315   /// isNullValue - Return true if this is the value that would be returned by
316   /// getNullValue.
317   virtual bool isNullValue() const {
318     // FIXME: This should be made to be MUCH faster.  Just check against well
319     // known null value!
320     for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
321       if (!cast<Constant>(getOperand(i))->isNullValue())
322         return false; 
323     return true;
324   }
325
326   virtual void destroyConstant();
327   virtual void replaceUsesOfWithOnConstant(Value *From, Value *To,
328                                            bool DisableChecking = false);
329
330   /// Methods for support type inquiry through isa, cast, and dyn_cast:
331   static inline bool classof(const ConstantArray *) { return true; }
332   static bool classof(const Constant *CPV);  // defined in Constants.cpp
333   static inline bool classof(const Value *V) {
334     return isa<Constant>(V) && classof(cast<Constant>(V));
335   }
336 };
337
338
339 //===---------------------------------------------------------------------------
340 // ConstantStruct - Constant Struct Declarations
341 //
342 class ConstantStruct : public Constant {
343   friend struct ConstantCreator<ConstantStruct, StructType,
344                                     std::vector<Constant*> >;
345   ConstantStruct(const ConstantStruct &);      // DO NOT IMPLEMENT
346 protected:
347   ConstantStruct(const StructType *T, const std::vector<Constant*> &Val);
348 public:
349   /// get() - Static factory methods - Return objects of the specified value
350   static ConstantStruct *get(const StructType *T,
351                              const std::vector<Constant*> &V);
352
353   /// getType() specialization - Reduce amount of casting...
354   inline const StructType *getType() const {
355     return (StructType*)Value::getType();
356   }
357
358   /// getValues - Return a vector of the component constants that make up this
359   /// structure.
360   inline const std::vector<Use> &getValues() const { return Operands; }
361
362   /// isNullValue - Return true if this is the value that would be returned by
363   /// getNullValue.
364   virtual bool isNullValue() const {
365     // FIXME: This should be made to be MUCH faster.  Just check against well
366     // known null value!
367     for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
368       if (!cast<Constant>(getOperand(i))->isNullValue())
369         return false; 
370     return true;
371   }
372
373   virtual void destroyConstant();
374   virtual void replaceUsesOfWithOnConstant(Value *From, Value *To,
375                                            bool DisableChecking = false);
376   
377   /// Methods for support type inquiry through isa, cast, and dyn_cast:
378   static inline bool classof(const ConstantStruct *) { return true; }
379   static bool classof(const Constant *CPV);  // defined in Constants.cpp
380   static inline bool classof(const Value *V) {
381     return isa<Constant>(V) && classof(cast<Constant>(V));
382   }
383 };
384
385 //===---------------------------------------------------------------------------
386 /// ConstantPointer - Constant Pointer Declarations
387 ///
388 /// The ConstantPointer class represents a null pointer of a specific type. For
389 /// a more specific/useful instance, a subclass of ConstantPointer should be
390 /// used.
391 ///
392 class ConstantPointer : public Constant {
393   ConstantPointer(const ConstantPointer &);      // DO NOT IMPLEMENT
394 protected:
395   inline ConstantPointer(const PointerType *T) : Constant((const Type*)T) {}
396 public:
397   inline const PointerType *getType() const {
398     return (PointerType*)Value::getType();
399   }
400
401   /// isNullValue - Return true if this is the value that would be returned by
402   /// getNullValue.
403   virtual bool isNullValue() const { return false; }
404
405   /// Methods for support type inquiry through isa, cast, and dyn_cast:
406   static inline bool classof(const ConstantPointer *) { return true; }
407   static bool classof(const Constant *CPV);  // defined in Constants.cpp
408   static inline bool classof(const Value *V) {
409     return isa<Constant>(V) && classof(cast<Constant>(V));
410   }
411 };
412
413 /// ConstantPointerNull - a constant pointer value that points to null
414 ///
415 class ConstantPointerNull : public ConstantPointer {
416   friend struct ConstantCreator<ConstantPointerNull, PointerType, char>;
417   ConstantPointerNull(const ConstantPointerNull &);      // DO NOT IMPLEMENT
418 protected:
419   ConstantPointerNull(const PointerType *T) : ConstantPointer(T) {}
420
421 public:
422
423   /// get() - Static factory methods - Return objects of the specified value
424   static ConstantPointerNull *get(const PointerType *T);
425
426   /// isNullValue - Return true if this is the value that would be returned by
427   /// getNullValue.
428   virtual bool isNullValue() const { return true; }
429
430   virtual void destroyConstant();
431
432   /// Methods for support type inquiry through isa, cast, and dyn_cast:
433   static inline bool classof(const ConstantPointerNull *) { return true; }
434   static inline bool classof(const ConstantPointer *P) {
435     return (P->getNumOperands() == 0 && P->isNullValue());
436   }
437   static inline bool classof(const Constant *CPV) {
438     return isa<ConstantPointer>(CPV) && classof(cast<ConstantPointer>(CPV));
439   }
440   static inline bool classof(const Value *V) {
441     return isa<ConstantPointer>(V) && classof(cast<ConstantPointer>(V));
442   }
443 };
444
445
446 /// ConstantPointerRef - a constant pointer value that is initialized to
447 /// point to a global value, which lies at a constant, fixed address.
448 ///
449 class ConstantPointerRef : public ConstantPointer {
450   friend class Module;   // Modules maintain these references
451   ConstantPointerRef(const ConstantPointerRef &); // DNI!
452
453 protected:
454   ConstantPointerRef(GlobalValue *GV);
455 public:
456   /// get() - Static factory methods - Return objects of the specified value
457   static ConstantPointerRef *get(GlobalValue *GV);
458
459   const GlobalValue *getValue() const { 
460     return cast<GlobalValue>(Operands[0].get());
461   }
462
463   GlobalValue *getValue() {
464     return cast<GlobalValue>(Operands[0].get());
465   }
466
467   virtual void destroyConstant();
468   virtual void replaceUsesOfWithOnConstant(Value *From, Value *To,
469                                            bool DisableChecking = false);
470
471   /// Methods for support type inquiry through isa, cast, and dyn_cast:
472   static inline bool classof(const ConstantPointerRef *) { return true; }
473   static inline bool classof(const ConstantPointer *CPV) {
474     // check for a single operand (the target value)
475     return (CPV->getNumOperands() == 1);
476   }
477   static inline bool classof(const Constant *CPV) {
478     return isa<ConstantPointer>(CPV) && classof(cast<ConstantPointer>(CPV));
479   }
480   static inline bool classof(const Value *V) {
481     return isa<ConstantPointer>(V) && classof(cast<ConstantPointer>(V));
482   }
483 };
484
485 // ConstantExpr - a constant value that is initialized with an expression using
486 // other constant values.  This is only used to represent values that cannot be
487 // evaluated at compile-time (e.g., something derived from an address) because
488 // it does not have a mechanism to store the actual value.  Use the appropriate
489 // Constant subclass above for known constants.
490 //
491 class ConstantExpr : public Constant {
492   unsigned iType;      // Operation type (an Instruction opcode)
493   friend struct ConstantCreator<ConstantExpr,Type,
494                             std::pair<unsigned, std::vector<Constant*> > >;
495   friend struct ConvertConstantType<ConstantExpr, Type>;
496   
497 protected:
498   // Cast creation ctor
499   ConstantExpr(unsigned Opcode, Constant *C, const Type *Ty);
500   // Binary/Shift instruction creation ctor
501   ConstantExpr(unsigned Opcode, Constant *C1, Constant *C2);
502   // GEP instruction creation ctor
503   ConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
504                const Type *DestTy);
505
506   // These private methods are used by the type resolution code to create
507   // ConstantExprs in intermediate forms.
508   static Constant *getTy(const Type *Ty, unsigned Opcode,
509                          Constant *C1, Constant *C2);
510   static Constant *getShiftTy(const Type *Ty,
511                               unsigned Opcode, Constant *C1, Constant *C2);
512   static Constant *getGetElementPtrTy(const Type *Ty, Constant *C,
513                                       const std::vector<Constant*> &IdxList);
514   
515 public:
516   // Static methods to construct a ConstantExpr of different kinds.  Note that
517   // these methods may return a object that is not an instance of the
518   // ConstantExpr class, because they will attempt to fold the constant
519   // expression into something simpler if possible.
520   
521   /// Cast constant expr
522   ///
523   static Constant *getCast(Constant *C, const Type *Ty);
524
525   /// Binary constant expr - Use with binary operators...
526   ///
527   static Constant *get(unsigned Opcode, Constant *C1, Constant *C2) {
528     return getTy(C1->getType(), Opcode, C1, C2);
529   }
530
531   /// getShift - Return a shift left or shift right constant expr
532   ///
533   static Constant *getShift(unsigned Opcode, Constant *C1, Constant *C2) {
534     return getShiftTy(C1->getType(), Opcode, C1, C2);
535   }
536
537   /// Getelementptr form...
538   ///
539   static Constant *getGetElementPtr(Constant *C,
540                                     const std::vector<Constant*> &IdxList);
541   
542   /// isNullValue - Return true if this is the value that would be returned by
543   /// getNullValue.
544   virtual bool isNullValue() const { return false; }
545   
546   /// getOpcode - Return the opcode at the root of this constant expression
547   unsigned getOpcode() const { return iType; }
548
549   /// getOpcodeName - Return a string representation for an opcode.
550   const char *getOpcodeName() const;
551   
552   /// isConstantExpr - Return true if this is a ConstantExpr
553   virtual bool isConstantExpr() const { return true; }
554
555   virtual void destroyConstant();
556   virtual void replaceUsesOfWithOnConstant(Value *From, Value *To,
557                                            bool DisableChecking = false);
558     
559   /// Override methods to provide more type information...
560   inline Constant *getOperand(unsigned i) { 
561     return cast<Constant>(User::getOperand(i));
562   }
563   inline Constant *getOperand(unsigned i) const {
564     return const_cast<Constant*>(cast<Constant>(User::getOperand(i)));
565   }
566   
567
568   /// Methods for support type inquiry through isa, cast, and dyn_cast:
569   static inline bool classof(const ConstantExpr *) { return true; }
570   static inline bool classof(const Constant *CPV) {
571     return CPV->isConstantExpr();
572   }
573   static inline bool classof(const Value *V) {
574     return isa<Constant>(V) && classof(cast<Constant>(V));
575   }
576 };
577
578 #endif