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