Remove support for Not ConstantExpr. This simplifies the unary case to only
[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 specified
49   // (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   // Factory objects - 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   inline bool getValue() const { return Val; }
82
83   // isNullValue - Return true if this is the value that would be returned by
84   // getNullValue.
85   //
86   virtual bool isNullValue() const { return this == False; }
87   virtual bool isMaxValue() const { return this == True; }
88   virtual bool isMinValue() const { return this == False; }
89   virtual bool isAllOnesValue() const { return this == True; }
90
91   // Methods for support type inquiry through isa, cast, and dyn_cast:
92   static inline bool classof(const ConstantBool *) { return true; }
93   static bool classof(const Constant *CPV) {
94     return (CPV == True) | (CPV == False);
95   }
96   static inline bool classof(const Value *V) {
97     return isa<Constant>(V) && classof(cast<Constant>(V));
98   }
99 };
100
101
102 //===---------------------------------------------------------------------------
103 // ConstantInt - Superclass of ConstantSInt & ConstantUInt, to make dealing
104 // with integral constants easier.
105 //
106 class ConstantInt : public ConstantIntegral {
107 protected:
108   union {
109     int64_t  Signed;
110     uint64_t Unsigned;
111   } Val;
112   ConstantInt(const ConstantInt &);      // DO NOT IMPLEMENT
113   ConstantInt(const Type *Ty, uint64_t V);
114   ~ConstantInt() {}
115 public:
116   // equalsInt - Provide a helper method that can be used to determine if the 
117   // constant contained within is equal to a constant.  This only works for very
118   // small values, because this is all that can be represented with all types.
119   //
120   bool equalsInt(unsigned char V) const {
121     assert(V <= 127 &&
122            "equals: Can only be used with very small positive constants!");
123     return Val.Unsigned == V;
124   }
125
126   // ConstantInt::get static method: return a constant pool int with the
127   // specified value.  as above, we work only with very small values here.
128   //
129   static ConstantInt *get(const Type *Ty, unsigned char V);
130
131   // isNullValue - Return true if this is the value that would be returned by
132   // getNullValue.
133   virtual bool isNullValue() const { return Val.Unsigned == 0; }
134   virtual bool isAllOnesValue() const { return Val.Signed == -1; }
135   virtual bool isMaxValue() const = 0;
136   virtual bool isMinValue() const = 0;
137
138   // Methods for support type inquiry through isa, cast, and dyn_cast:
139   static inline bool classof(const ConstantInt *) { return true; }
140   static bool classof(const Constant *CPV);  // defined in Constants.cpp
141   static inline bool classof(const Value *V) {
142     return isa<Constant>(V) && classof(cast<Constant>(V));
143   }
144 };
145
146
147 //===---------------------------------------------------------------------------
148 // ConstantSInt - Signed Integer Values [sbyte, short, int, long]
149 //
150 class ConstantSInt : public ConstantInt {
151   ConstantSInt(const ConstantSInt &);      // DO NOT IMPLEMENT
152 protected:
153   ConstantSInt(const Type *Ty, int64_t V);
154   ~ConstantSInt() {}
155 public:
156   static ConstantSInt *get(const Type *Ty, int64_t V);
157
158   static bool isValueValidForType(const Type *Ty, int64_t V);
159   inline int64_t getValue() const { return Val.Signed; }
160
161   // isMaxValue - Return true if this is the largest value that may be
162   // represented by this type.
163   //
164   virtual bool isMaxValue() const {
165     int64_t V = getValue();
166     if (V < 0) return false;    // Be careful about wrap-around on 'long's
167     ++V;
168     return !isValueValidForType(getType(), V) || V < 0;
169   }
170
171   // isMinValue - Return true if this is the smallest value that may be
172   // represented by this type.
173   //
174   virtual bool isMinValue() const {
175     int64_t V = getValue();
176     if (V > 0) return false;    // Be careful about wrap-around on 'long's
177     --V;
178     return !isValueValidForType(getType(), V) || V > 0;
179   }
180
181   // Methods for support type inquiry through isa, cast, and dyn_cast:
182   static inline bool classof(const ConstantSInt *) { return true; }
183   static bool classof(const Constant *CPV);  // defined in Constants.cpp
184   static inline bool classof(const Value *V) {
185     return isa<Constant>(V) && classof(cast<Constant>(V));
186   }
187 };
188
189 //===---------------------------------------------------------------------------
190 // ConstantUInt - Unsigned Integer Values [ubyte, ushort, uint, ulong]
191 //
192 class ConstantUInt : public ConstantInt {
193   ConstantUInt(const ConstantUInt &);      // DO NOT IMPLEMENT
194 protected:
195   ConstantUInt(const Type *Ty, uint64_t V);
196   ~ConstantUInt() {}
197 public:
198   static ConstantUInt *get(const Type *Ty, uint64_t V);
199
200   static bool isValueValidForType(const Type *Ty, uint64_t V);
201   inline uint64_t getValue() const { return Val.Unsigned; }
202
203   // isMaxValue - Return true if this is the largest value that may be
204   // represented by this type.
205   //
206   virtual bool isMaxValue() const { return isAllOnesValue(); }
207   virtual bool isMinValue() const { return getValue() == 0; }
208
209   // Methods for support type inquiry through isa, cast, and dyn_cast:
210   static inline bool classof(const ConstantUInt *) { return true; }
211   static bool classof(const Constant *CPV);  // defined in Constants.cpp
212   static inline bool classof(const Value *V) {
213     return isa<Constant>(V) && classof(cast<Constant>(V));
214   }
215 };
216
217
218 //===---------------------------------------------------------------------------
219 // ConstantFP - Floating Point Values [float, double]
220 //
221 class ConstantFP : public Constant {
222   double Val;
223   ConstantFP(const ConstantFP &);      // DO NOT IMPLEMENT
224 protected:
225   ConstantFP(const Type *Ty, double V);
226   ~ConstantFP() {}
227 public:
228   static ConstantFP *get(const Type *Ty, double V);
229
230   static bool isValueValidForType(const Type *Ty, double V);
231   inline double getValue() const { return Val; }
232
233   // isNullValue - Return true if this is the value that would be returned by
234   // getNullValue.
235   virtual bool isNullValue() const { return Val == 0; }
236
237   // Methods for support type inquiry through isa, cast, and dyn_cast:
238   static inline bool classof(const ConstantFP *) { return true; }
239   static bool classof(const Constant *CPV);  // defined in Constants.cpp
240   static inline bool classof(const Value *V) {
241     return isa<Constant>(V) && classof(cast<Constant>(V));
242   }
243 };
244
245
246 //===---------------------------------------------------------------------------
247 // ConstantArray - Constant Array Declarations
248 //
249 class ConstantArray : public Constant {
250   ConstantArray(const ConstantArray &);      // DO NOT IMPLEMENT
251 protected:
252   ConstantArray(const ArrayType *T, const std::vector<Constant*> &Val);
253   ~ConstantArray() {}
254
255   virtual void destroyConstant();
256 public:
257   static ConstantArray *get(const ArrayType *T, const std::vector<Constant*> &);
258   static ConstantArray *get(const std::string &Initializer);
259   
260   inline const ArrayType *getType() const {
261     return (ArrayType*)Value::getType();
262   }
263
264   inline const std::vector<Use> &getValues() const { return Operands; }
265
266   // isNullValue - Return true if this is the value that would be returned by
267   // getNullValue.
268   virtual bool isNullValue() const { return false; }
269
270   // Methods for support type inquiry through isa, cast, and dyn_cast:
271   static inline bool classof(const ConstantArray *) { return true; }
272   static bool classof(const Constant *CPV);  // defined in Constants.cpp
273   static inline bool classof(const Value *V) {
274     return isa<Constant>(V) && classof(cast<Constant>(V));
275   }
276 };
277
278
279 //===---------------------------------------------------------------------------
280 // ConstantStruct - Constant Struct Declarations
281 //
282 class ConstantStruct : public Constant {
283   ConstantStruct(const ConstantStruct &);      // DO NOT IMPLEMENT
284 protected:
285   ConstantStruct(const StructType *T, const std::vector<Constant*> &Val);
286   ~ConstantStruct() {}
287
288   virtual void destroyConstant();
289 public:
290   static ConstantStruct *get(const StructType *T,
291                              const std::vector<Constant*> &V);
292
293   inline const StructType *getType() const {
294     return (StructType*)Value::getType();
295   }
296
297   inline const std::vector<Use> &getValues() const { return Operands; }
298
299   // isNullValue - Return true if this is the value that would be returned by
300   // getNullValue.
301   virtual bool isNullValue() const { return false; }
302
303   // Methods for support type inquiry through isa, cast, and dyn_cast:
304   static inline bool classof(const ConstantStruct *) { return true; }
305   static bool classof(const Constant *CPV);  // defined in Constants.cpp
306   static inline bool classof(const Value *V) {
307     return isa<Constant>(V) && classof(cast<Constant>(V));
308   }
309 };
310
311 //===---------------------------------------------------------------------------
312 // ConstantPointer - Constant Pointer Declarations
313 //
314 // The ConstantPointer class represents a null pointer of a specific type. For
315 // a more specific/useful instance, a subclass of ConstantPointer should be
316 // used.
317 //
318 class ConstantPointer : public Constant {
319   ConstantPointer(const ConstantPointer &);      // DO NOT IMPLEMENT
320 protected:
321   inline ConstantPointer(const PointerType *T) : Constant((const Type*)T){}
322   ~ConstantPointer() {}
323 public:
324   inline const PointerType *getType() const {
325     return (PointerType*)Value::getType();
326   }
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   // Methods for support type inquiry through isa, cast, and dyn_cast:
333   static inline bool classof(const ConstantPointer *) { 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 // ConstantPointerNull - a constant pointer value that points to null
341 //
342 class ConstantPointerNull : public ConstantPointer {
343   ConstantPointerNull(const ConstantPointerNull &);      // DO NOT IMPLEMENT
344 protected:
345   inline ConstantPointerNull(const PointerType *T) : ConstantPointer(T) {}
346   inline ~ConstantPointerNull() {}
347 public:
348
349   static ConstantPointerNull *get(const PointerType *T);
350
351   // isNullValue - Return true if this is the value that would be returned by
352   // getNullValue.
353   virtual bool isNullValue() const { return true; }
354
355   // Methods for support type inquiry through isa, cast, and dyn_cast:
356   static inline bool classof(const ConstantPointerNull *) { return true; }
357   static inline bool classof(const ConstantPointer *P) {
358     return (P->getNumOperands() == 0 && P->isNullValue());
359   }
360   static inline bool classof(const Constant *CPV) {
361     return isa<ConstantPointer>(CPV) && classof(cast<ConstantPointer>(CPV));
362   }
363   static inline bool classof(const Value *V) {
364     return isa<ConstantPointer>(V) && classof(cast<ConstantPointer>(V));
365   }
366 };
367
368
369 // ConstantPointerRef - a constant pointer value that is initialized to
370 // point to a global value, which lies at a constant, fixed address.
371 //
372 class ConstantPointerRef : public ConstantPointer {
373   friend class Module;   // Modules maintain these references
374   ConstantPointerRef(const ConstantPointerRef &); // DNI!
375
376 protected:
377   ConstantPointerRef(GlobalValue *GV);
378   ~ConstantPointerRef() {}
379
380   virtual void destroyConstant() { destroyConstantImpl(); }
381 public:
382   static ConstantPointerRef *get(GlobalValue *GV);
383
384   const GlobalValue *getValue() const { 
385     return cast<GlobalValue>(Operands[0].get());
386   }
387   GlobalValue *getValue() {
388     return cast<GlobalValue>(Operands[0].get());
389   }
390
391   // Methods for support type inquiry through isa, cast, and dyn_cast:
392   static inline bool classof(const ConstantPointerRef *) { return true; }
393   static inline bool classof(const ConstantPointer *CPV) {
394     // check for a single operand (the target value)
395     return (CPV->getNumOperands() == 1);
396   }
397   static inline bool classof(const Constant *CPV) {
398     return isa<ConstantPointer>(CPV) && classof(cast<ConstantPointer>(CPV));
399   }
400   static inline bool classof(const Value *V) {
401     return isa<ConstantPointer>(V) && classof(cast<ConstantPointer>(V));
402   }
403
404   // WARNING: Only to be used by Bytecode & Assembly Parsers!  USER CODE SHOULD
405   // NOT USE THIS!!
406   // Returns the number of uses of OldV that were replaced.
407   virtual unsigned mutateReferences(Value* OldV, Value *NewV);
408   // END WARNING!!
409 };
410
411
412 // ConstantExpr - a constant value that is initialized with
413 // an expression using other constant values.  This is only used
414 // to represent values that cannot be evaluated at compile-time
415 // (e.g., something derived from an address) because it does
416 // not have a mechanism to store the actual value.
417 // Use the appropriate Constant subclass above for known constants.
418 //
419 class ConstantExpr : public Constant {
420   unsigned iType;      // Operation type
421   
422 protected:
423   ConstantExpr(unsigned Opcode, Constant *C,  const Type *Ty);
424   ConstantExpr(unsigned Opcode, Constant *C1, Constant *C2);
425   ConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
426                const Type *DestTy);
427   ~ConstantExpr() {}
428   
429   virtual void destroyConstant();
430   
431 public:
432   // Static methods to construct a ConstantExpr of different kinds.
433   
434   // Cast constant expr
435   static ConstantExpr *getCast(Constant *C, const Type *Ty);
436
437   // Binary constant expr - Use with binary operators...
438   static ConstantExpr *get(unsigned Opcode, Constant *C1, Constant *C2);
439
440   // Getelementptr form...
441   static ConstantExpr *getGetElementPtr(Constant *C,
442                                         const std::vector<Constant*> &IdxList);
443   
444   // isNullValue - Return true if this is the value that would be returned by
445   // getNullValue.
446   virtual bool isNullValue() const { return false; }
447   
448   // getOpcode - Return the opcode at the root of this constant expression
449   unsigned getOpcode() const { return iType; }
450
451   // getOpcodeName - Return a string representation for an opcode.
452   const char *getOpcodeName() const;
453   
454   // isConstantExpr - Return true if this is a ConstantExpr
455   virtual bool isConstantExpr() const { return true; }
456   
457   // Methods for support type inquiry through isa, cast, and dyn_cast:
458   static inline bool classof(const ConstantExpr *) { return true; }
459   static inline bool classof(const Constant *CPV) {
460     return CPV->isConstantExpr();
461   }
462   static inline bool classof(const Value *V) {
463     return isa<Constant>(V) && classof(cast<Constant>(V));
464   }
465
466 public:
467   // WARNING: Only to be used by Bytecode & Assembly Parsers!  USER CODE SHOULD
468   // NOT USE THIS!!
469   // Returns the number of uses of OldV that were replaced.
470   virtual unsigned mutateReferences(Value* OldV, Value *NewV);
471   // END WARNING!!
472 };
473
474
475 #endif