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