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