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