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