New interface
[oota-llvm.git] / include / llvm / Constants.h
index 396a5ca0930dd7038169a67c94bb222374fb1ecc..077c61c6d5ec99dbf91bcb9132a189d251454808 100644 (file)
@@ -1,4 +1,11 @@
-//===-- llvm/Constants.h - Constant class subclass definitions ---*- C++ -*--=//
+//===-- llvm/Constants.h - Constant class subclass definitions --*- C++ -*-===//
+// 
+//                     The LLVM Compiler Infrastructure
+//
+// This file was developed by the LLVM research group and is distributed under
+// the University of Illinois Open Source License. See LICENSE.TXT for details.
+// 
+//===----------------------------------------------------------------------===//
 //
 // This file contains the declarations for the subclasses of Constant, which
 // represent the different type of constant pool values
@@ -15,6 +22,11 @@ class ArrayType;
 class StructType;
 class PointerType;
 
+template<class ConstantClass, class TypeClass, class ValType>
+struct ConstantCreator;
+template<class ConstantClass, class TypeClass>
+struct ConvertConstantType;
+
 
 //===---------------------------------------------------------------------------
 /// ConstantIntegral - Shared superclass of boolean and integer constants.
@@ -67,7 +79,6 @@ public:
 class ConstantBool : public ConstantIntegral {
   bool Val;
   ConstantBool(bool V);
-  ~ConstantBool() {}
 public:
   static ConstantBool *True, *False;  // The True & False values
 
@@ -113,7 +124,6 @@ protected:
   } Val;
   ConstantInt(const ConstantInt &);      // DO NOT IMPLEMENT
   ConstantInt(const Type *Ty, uint64_t V);
-  ~ConstantInt() {}
 public:
   /// equalsInt - Provide a helper method that can be used to determine if the
   /// constant contained within is equal to a constant.  This only works for
@@ -131,6 +141,11 @@ public:
   ///
   static ConstantInt *get(const Type *Ty, unsigned char V);
 
+  /// getRawValue - return the underlying value of this constant as a 64-bit
+  /// unsigned integer value.
+  ///
+  inline uint64_t getRawValue() const { return Val.Unsigned; }
+
   /// isNullValue - Return true if this is the value that would be returned by
   /// getNullValue.
   virtual bool isNullValue() const { return Val.Unsigned == 0; }
@@ -151,17 +166,21 @@ public:
 ///
 class ConstantSInt : public ConstantInt {
   ConstantSInt(const ConstantSInt &);      // DO NOT IMPLEMENT
+  friend struct ConstantCreator<ConstantSInt, Type, int64_t>;
+
 protected:
   ConstantSInt(const Type *Ty, int64_t V);
-  ~ConstantSInt() {}
 public:
   /// get() - Static factory methods - Return objects of the specified value
+  ///
   static ConstantSInt *get(const Type *Ty, int64_t V);
 
   /// isValueValidForType - return true if Ty is big enough to represent V.
+  ///
   static bool isValueValidForType(const Type *Ty, int64_t V);
 
   /// getValue - return the underlying value of this constant.
+  ///
   inline int64_t getValue() const { return Val.Signed; }
 
   virtual bool isAllOnesValue() const { return getValue() == -1; }
@@ -187,6 +206,7 @@ public:
   }
 
   /// Methods for support type inquiry through isa, cast, and dyn_cast:
+  ///
   static inline bool classof(const ConstantSInt *) { return true; }
   static bool classof(const Constant *CPV);  // defined in Constants.cpp
   static inline bool classof(const Value *V) {
@@ -199,17 +219,20 @@ public:
 ///
 class ConstantUInt : public ConstantInt {
   ConstantUInt(const ConstantUInt &);      // DO NOT IMPLEMENT
+  friend struct ConstantCreator<ConstantUInt, Type, uint64_t>;
 protected:
   ConstantUInt(const Type *Ty, uint64_t V);
-  ~ConstantUInt() {}
 public:
   /// get() - Static factory methods - Return objects of the specified value
+  ///
   static ConstantUInt *get(const Type *Ty, uint64_t V);
 
   /// isValueValidForType - return true if Ty is big enough to represent V.
+  ///
   static bool isValueValidForType(const Type *Ty, uint64_t V);
 
   /// getValue - return the underlying value of this constant.
+  ///
   inline uint64_t getValue() const { return Val.Unsigned; }
 
   /// isMaxValue - Return true if this is the largest value that may be
@@ -233,10 +256,10 @@ public:
 ///
 class ConstantFP : public Constant {
   double Val;
+  friend struct ConstantCreator<ConstantFP, Type, double>;
   ConstantFP(const ConstantFP &);      // DO NOT IMPLEMENT
 protected:
   ConstantFP(const Type *Ty, double V);
-  ~ConstantFP() {}
 public:
   /// get() - Static factory methods - Return objects of the specified value
   static ConstantFP *get(const Type *Ty, double V);
@@ -262,11 +285,11 @@ public:
 /// ConstantArray - Constant Array Declarations
 ///
 class ConstantArray : public Constant {
+  friend struct ConstantCreator<ConstantArray, ArrayType,
+                                    std::vector<Constant*> >;
   ConstantArray(const ConstantArray &);      // DO NOT IMPLEMENT
 protected:
   ConstantArray(const ArrayType *T, const std::vector<Constant*> &Val);
-  ~ConstantArray() {}
-
 public:
   /// get() - Static factory methods - Return objects of the specified value
   static ConstantArray *get(const ArrayType *T, const std::vector<Constant*> &);
@@ -301,7 +324,8 @@ public:
   }
 
   virtual void destroyConstant();
-  virtual void replaceUsesOfWithOnConstant(Value *From, Value *To);
+  virtual void replaceUsesOfWithOnConstant(Value *From, Value *To,
+                                           bool DisableChecking = false);
 
   /// Methods for support type inquiry through isa, cast, and dyn_cast:
   static inline bool classof(const ConstantArray *) { return true; }
@@ -316,11 +340,11 @@ public:
 // ConstantStruct - Constant Struct Declarations
 //
 class ConstantStruct : public Constant {
+  friend struct ConstantCreator<ConstantStruct, StructType,
+                                    std::vector<Constant*> >;
   ConstantStruct(const ConstantStruct &);      // DO NOT IMPLEMENT
 protected:
   ConstantStruct(const StructType *T, const std::vector<Constant*> &Val);
-  ~ConstantStruct() {}
-
 public:
   /// get() - Static factory methods - Return objects of the specified value
   static ConstantStruct *get(const StructType *T,
@@ -347,7 +371,8 @@ public:
   }
 
   virtual void destroyConstant();
-  virtual void replaceUsesOfWithOnConstant(Value *From, Value *To);
+  virtual void replaceUsesOfWithOnConstant(Value *From, Value *To,
+                                           bool DisableChecking = false);
   
   /// Methods for support type inquiry through isa, cast, and dyn_cast:
   static inline bool classof(const ConstantStruct *) { return true; }
@@ -367,8 +392,7 @@ public:
 class ConstantPointer : public Constant {
   ConstantPointer(const ConstantPointer &);      // DO NOT IMPLEMENT
 protected:
-  inline ConstantPointer(const PointerType *T) : Constant((const Type*)T){}
-  ~ConstantPointer() {}
+  inline ConstantPointer(const PointerType *T) : Constant((const Type*)T) {}
 public:
   inline const PointerType *getType() const {
     return (PointerType*)Value::getType();
@@ -389,10 +413,11 @@ public:
 /// ConstantPointerNull - a constant pointer value that points to null
 ///
 class ConstantPointerNull : public ConstantPointer {
+  friend struct ConstantCreator<ConstantPointerNull, PointerType, char>;
   ConstantPointerNull(const ConstantPointerNull &);      // DO NOT IMPLEMENT
 protected:
-  inline ConstantPointerNull(const PointerType *T) : ConstantPointer(T) {}
-  inline ~ConstantPointerNull() {}
+  ConstantPointerNull(const PointerType *T) : ConstantPointer(T) {}
+
 public:
 
   /// get() - Static factory methods - Return objects of the specified value
@@ -427,7 +452,6 @@ class ConstantPointerRef : public ConstantPointer {
 
 protected:
   ConstantPointerRef(GlobalValue *GV);
-  ~ConstantPointerRef() {}
 public:
   /// get() - Static factory methods - Return objects of the specified value
   static ConstantPointerRef *get(GlobalValue *GV);
@@ -441,7 +465,8 @@ public:
   }
 
   virtual void destroyConstant();
-  virtual void replaceUsesOfWithOnConstant(Value *From, Value *To);
+  virtual void replaceUsesOfWithOnConstant(Value *From, Value *To,
+                                           bool DisableChecking = false);
 
   /// Methods for support type inquiry through isa, cast, and dyn_cast:
   static inline bool classof(const ConstantPointerRef *) { return true; }
@@ -457,36 +482,60 @@ public:
   }
 };
 
-
-// ConstantExpr - a constant value that is initialized with
-// an expression using other constant values.  This is only used
-// to represent values that cannot be evaluated at compile-time
-// (e.g., something derived from an address) because it does
-// not have a mechanism to store the actual value.
-// Use the appropriate Constant subclass above for known constants.
+// ConstantExpr - a constant value that is initialized with an expression using
+// other constant values.  This is only used to represent values that cannot be
+// evaluated at compile-time (e.g., something derived from an address) because
+// it does not have a mechanism to store the actual value.  Use the appropriate
+// Constant subclass above for known constants.
 //
 class ConstantExpr : public Constant {
-  unsigned iType;      // Operation type
+  unsigned iType;      // Operation type (an Instruction opcode)
+  friend struct ConstantCreator<ConstantExpr,Type,
+                            std::pair<unsigned, std::vector<Constant*> > >;
+  friend struct ConvertConstantType<ConstantExpr, Type>;
   
 protected:
-  ConstantExpr(unsigned Opcode, Constant *C,  const Type *Ty);
+  // Cast creation ctor
+  ConstantExpr(unsigned Opcode, Constant *C, const Type *Ty);
+  // Binary/Shift instruction creation ctor
   ConstantExpr(unsigned Opcode, Constant *C1, Constant *C2);
+  // GEP instruction creation ctor
   ConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
                const Type *DestTy);
-  ~ConstantExpr() {}
+
+  // These private methods are used by the type resolution code to create
+  // ConstantExprs in intermediate forms.
+  static Constant *getTy(const Type *Ty, unsigned Opcode,
+                         Constant *C1, Constant *C2);
+  static Constant *getShiftTy(const Type *Ty,
+                              unsigned Opcode, Constant *C1, Constant *C2);
+  static Constant *getGetElementPtrTy(const Type *Ty, Constant *C,
+                                      const std::vector<Constant*> &IdxList);
   
 public:
   // Static methods to construct a ConstantExpr of different kinds.  Note that
-  // these methods can return a constant of an arbitrary type, because they will
-  // attempt to fold the constant expression into something simple if they can.
+  // these methods may return a object that is not an instance of the
+  // ConstantExpr class, because they will attempt to fold the constant
+  // expression into something simpler if possible.
   
   /// Cast constant expr
+  ///
   static Constant *getCast(Constant *C, const Type *Ty);
 
   /// Binary constant expr - Use with binary operators...
-  static Constant *get(unsigned Opcode, Constant *C1, Constant *C2);
+  ///
+  static Constant *get(unsigned Opcode, Constant *C1, Constant *C2) {
+    return getTy(C1->getType(), Opcode, C1, C2);
+  }
+
+  /// getShift - Return a shift left or shift right constant expr
+  ///
+  static Constant *getShift(unsigned Opcode, Constant *C1, Constant *C2) {
+    return getShiftTy(C1->getType(), Opcode, C1, C2);
+  }
 
   /// Getelementptr form...
+  ///
   static Constant *getGetElementPtr(Constant *C,
                                     const std::vector<Constant*> &IdxList);
   
@@ -504,8 +553,18 @@ public:
   virtual bool isConstantExpr() const { return true; }
 
   virtual void destroyConstant();
-  virtual void replaceUsesOfWithOnConstant(Value *From, Value *To);
+  virtual void replaceUsesOfWithOnConstant(Value *From, Value *To,
+                                           bool DisableChecking = false);
     
+  /// Override methods to provide more type information...
+  inline Constant *getOperand(unsigned i) { 
+    return cast<Constant>(User::getOperand(i));
+  }
+  inline Constant *getOperand(unsigned i) const {
+    return const_cast<Constant*>(cast<Constant>(User::getOperand(i)));
+  }
+  
+
   /// Methods for support type inquiry through isa, cast, and dyn_cast:
   static inline bool classof(const ConstantExpr *) { return true; }
   static inline bool classof(const Constant *CPV) {