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