Add a value_type typedef to SmallVector, to make it more compatible with STL adapters.
[oota-llvm.git] / include / llvm / Constants.h
index 2c7f577e34930c1f49e1c4b67b69203046b0966d..22acbfcd45076afdae4786c2d16ae28973d63bb0 100644 (file)
@@ -2,8 +2,8 @@
 //
 //                     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 is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
 //
 //===----------------------------------------------------------------------===//
 //
 
 #include "llvm/Constant.h"
 #include "llvm/Type.h"
+#include "llvm/OperandTraits.h"
 #include "llvm/ADT/APInt.h"
 #include "llvm/ADT/APFloat.h"
+#include "llvm/ADT/SmallVector.h"
 
 namespace llvm {
 
@@ -43,14 +45,15 @@ struct ConvertConstantType;
 /// @brief Class for constant integers.
 class ConstantInt : public Constant {
   static ConstantInt *TheTrueVal, *TheFalseVal;
+  void *operator new(size_t, unsigned);  // DO NOT IMPLEMENT
   ConstantInt(const ConstantInt &);      // DO NOT IMPLEMENT
   ConstantInt(const IntegerType *Ty, const APInt& V);
   APInt Val;
 protected:
-  static void destroyThis(ConstantInt*v) {
-    Constant::destroyThis(v);
+  // allocate space for exactly zero operands
+  void *operator new(size_t s) {
+    return User::operator new(s, 0);
   }
-  friend class Value;
 public:
   /// Return the constant as an APInt value reference. This allows clients to
   /// obtain a copy of the value, with all its precision in tact.
@@ -220,16 +223,23 @@ private:
 ///
 class ConstantFP : public Constant {
   APFloat Val;
+  void *operator new(size_t, unsigned);// DO NOT IMPLEMENT
   ConstantFP(const ConstantFP &);      // DO NOT IMPLEMENT
 protected:
   ConstantFP(const Type *Ty, const APFloat& V);
-  static void destroyThis(ConstantFP*v) {
-    Constant::destroyThis(v);
+protected:
+  // allocate space for exactly zero operands
+  void *operator new(size_t s) {
+    return User::operator new(s, 0);
   }
-  friend class Value;
 public:
   /// get() - Static factory methods - Return objects of the specified value
-  static ConstantFP *get(const Type *Ty, const APFloat& V);
+  static ConstantFP *get(const APFloat &V);
+
+  /// get() - This returns a constant fp for the specified value in the
+  /// specified type.  This should only be used for simple constant values like
+  /// 2.0/1.0 etc, that are known-valid both as double and as the target format.
+  static ConstantFP *get(const Type *Ty, double V);
 
   /// isValueValidForType - return true if Ty is big enough to represent V.
   static bool isValueValidForType(const Type *Ty, const APFloat& V);
@@ -248,16 +258,16 @@ public:
   /// As such, this method can be used to do an exact bit-for-bit comparison of
   /// two floating point values.  The version with a double operand is retained
   /// because it's so convenient to write isExactlyValue(2.0), but please use
-  /// it only for constants.
+  /// it only for simple constants.
   bool isExactlyValue(const APFloat& V) const;
 
   bool isExactlyValue(double V) const {
-    if (&Val.getSemantics() == &APFloat::IEEEdouble)
-      return isExactlyValue(APFloat(V));
-    else if (&Val.getSemantics() == &APFloat::IEEEsingle)
-      return isExactlyValue(APFloat((float)V));
-    assert(0);
-    return false;
+    // convert is not supported on this type
+    if (&Val.getSemantics() == &APFloat::PPCDoubleDouble)
+      return false;
+    APFloat FV(V);
+    FV.convert(Val.getSemantics(), APFloat::rmNearestTiesToEven);
+    return isExactlyValue(FV);
   }
   /// Methods for support type inquiry through isa, cast, and dyn_cast:
   static inline bool classof(const ConstantFP *) { return true; }
@@ -271,15 +281,16 @@ public:
 ///
 class ConstantAggregateZero : public Constant {
   friend struct ConstantCreator<ConstantAggregateZero, Type, char>;
+  void *operator new(size_t, unsigned);                      // DO NOT IMPLEMENT
   ConstantAggregateZero(const ConstantAggregateZero &);      // DO NOT IMPLEMENT
 protected:
-  explicit ConstantAggregateZero(const Type *Ty)
-    : Constant(Ty, ConstantAggregateZeroVal, 0, 0) {}
-
-  static void destroyThis(ConstantAggregateZero*v) {
-    Constant::destroyThis(v);
+  explicit ConstantAggregateZero(const Type *ty)
+    : Constant(ty, ConstantAggregateZeroVal, 0, 0) {}
+protected:
+  // allocate space for exactly zero operands
+  void *operator new(size_t s) {
+    return User::operator new(s, 0);
   }
-  friend class Value;
 public:
   /// get() - static factory method for creating a null aggregate.  It is
   /// illegal to call this method with a non-aggregate type.
@@ -309,8 +320,6 @@ class ConstantArray : public Constant {
   ConstantArray(const ConstantArray &);      // DO NOT IMPLEMENT
 protected:
   ConstantArray(const ArrayType *T, const std::vector<Constant*> &Val);
-  static void destroyThis(ConstantArray*);
-  friend class Value;
 public:
   /// get() - Static factory methods - Return objects of the specified value
   static Constant *get(const ArrayType *T, const std::vector<Constant*> &);
@@ -328,6 +337,9 @@ public:
   /// null termination. 
   static Constant *get(const std::string &Initializer, bool AddNull = true);
 
+  /// Transparently provide more efficient getOperand methods.
+  DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
+
   /// getType - Specialize the getType() method to always return an ArrayType,
   /// which reduces the amount of casting needed in parts of the compiler.
   ///
@@ -335,8 +347,8 @@ public:
     return reinterpret_cast<const ArrayType*>(Value::getType());
   }
 
-  /// isString - This method returns true if the array is an array of sbyte or
-  /// ubyte, and if the elements of the array are all ConstantInt's.
+  /// isString - This method returns true if the array is an array of i8 and
+  /// the elements of the array are all ConstantInt's.
   bool isString() const;
 
   /// isCString - This method returns true if the array is a string (see
@@ -366,6 +378,11 @@ public:
   }
 };
 
+template <>
+struct OperandTraits<ConstantArray> : VariadicOperandTraits<> {
+};
+
+DEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(ConstantArray, Constant)
 
 //===----------------------------------------------------------------------===//
 // ConstantStruct - Constant Struct Declarations
@@ -376,8 +393,6 @@ class ConstantStruct : public Constant {
   ConstantStruct(const ConstantStruct &);      // DO NOT IMPLEMENT
 protected:
   ConstantStruct(const StructType *T, const std::vector<Constant*> &Val);
-  static void destroyThis(ConstantStruct*);
-  friend class Value;
 public:
   /// get() - Static factory methods - Return objects of the specified value
   ///
@@ -389,6 +404,9 @@ public:
     return get(std::vector<Constant*>(Vals, Vals+NumVals), Packed);
   }
   
+  /// Transparently provide more efficient getOperand methods.
+  DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
+
   /// getType() specialization - Reduce amount of casting...
   ///
   inline const StructType *getType() const {
@@ -412,6 +430,12 @@ public:
   }
 };
 
+template <>
+struct OperandTraits<ConstantStruct> : VariadicOperandTraits<> {
+};
+
+DEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(ConstantStruct, Constant)
+
 //===----------------------------------------------------------------------===//
 /// ConstantVector - Constant Vector Declarations
 ///
@@ -421,8 +445,6 @@ class ConstantVector : public Constant {
   ConstantVector(const ConstantVector &);      // DO NOT IMPLEMENT
 protected:
   ConstantVector(const VectorType *T, const std::vector<Constant*> &Val);
-  static void destroyThis(ConstantVector*v);
-  friend class Value;
 public:
   /// get() - Static factory methods - Return objects of the specified value
   static Constant *get(const VectorType *T, const std::vector<Constant*> &);
@@ -432,6 +454,9 @@ public:
     return get(std::vector<Constant*>(Vals, Vals+NumVals));
   }
   
+  /// Transparently provide more efficient getOperand methods.
+  DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
+
   /// getType - Specialize the getType() method to always return a VectorType,
   /// which reduces the amount of casting needed in parts of the compiler.
   ///
@@ -469,22 +494,30 @@ public:
   }
 };
 
+template <>
+struct OperandTraits<ConstantVector> : VariadicOperandTraits<> {
+};
+
+DEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(ConstantVector, Constant)
+
 //===----------------------------------------------------------------------===//
 /// ConstantPointerNull - a constant pointer value that points to null
 ///
 class ConstantPointerNull : public Constant {
   friend struct ConstantCreator<ConstantPointerNull, PointerType, char>;
+  void *operator new(size_t, unsigned);                  // DO NOT IMPLEMENT
   ConstantPointerNull(const ConstantPointerNull &);      // DO NOT IMPLEMENT
 protected:
   explicit ConstantPointerNull(const PointerType *T)
     : Constant(reinterpret_cast<const Type*>(T),
                Value::ConstantPointerNullVal, 0, 0) {}
-  static void destroyThis(ConstantPointerNull*v) {
-    Constant::destroyThis(v);
+
+protected:
+  // allocate space for exactly zero operands
+  void *operator new(size_t s) {
+    return User::operator new(s, 0);
   }
-  friend class Value;
 public:
-
   /// get() - Static factory methods - Return objects of the specified value
   static ConstantPointerNull *get(const PointerType *T);
 
@@ -521,8 +554,8 @@ class ConstantExpr : public Constant {
   friend struct ConvertConstantType<ConstantExpr, Type>;
 
 protected:
-  ConstantExpr(const Type *Ty, unsigned Opcode, Use *Ops, unsigned NumOps)
-    : Constant(Ty, ConstantExprVal, Ops, NumOps) {
+  ConstantExpr(const Type *ty, unsigned Opcode, Use *Ops, unsigned NumOps)
+    : Constant(ty, ConstantExprVal, Ops, NumOps) {
     // Operation type (an Instruction opcode) is stored as the SubclassData.
     SubclassData = Opcode;
   }
@@ -531,7 +564,7 @@ protected:
   // ConstantExprs in intermediate forms.
   static Constant *getTy(const Type *Ty, unsigned Opcode,
                          Constant *C1, Constant *C2);
-  static Constant *getCompareTy(unsigned short pred, Constant *C1, 
+  static Constant *getCompareTy(unsigned short pred, Constant *C1,
                                 Constant *C2);
   static Constant *getSelectTy(const Type *Ty,
                                Constant *C1, Constant *C2, Constant *C3);
@@ -543,11 +576,12 @@ protected:
                                       Constant *Elt, Constant *Idx);
   static Constant *getShuffleVectorTy(const Type *Ty, Constant *V1,
                                       Constant *V2, Constant *Mask);
+  static Constant *getExtractValueTy(const Type *Ty, Constant *Agg,
+                                     const unsigned *Idxs, unsigned NumIdxs);
+  static Constant *getInsertValueTy(const Type *Ty, Constant *Agg,
+                                    Constant *Val,
+                                    const unsigned *Idxs, unsigned NumIdxs);
 
-  static void destroyThis(ConstantExpr* v) {
-    Constant::destroyThis(v);
-  }
-  friend class Value;
 public:
   // Static methods to construct a ConstantExpr of different kinds.  Note that
   // these methods may return a object that is not an instance of the
@@ -569,6 +603,9 @@ public:
   static Constant *getIntToPtr(Constant *C, const Type *Ty);
   static Constant *getBitCast (Constant *C, const Type *Ty);
 
+  /// Transparently provide more efficient getOperand methods.
+  DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
+
   // @brief Convenience function for getting one of the casting operations
   // using a CastOps opcode.
   static Constant *getCast(
@@ -620,6 +657,10 @@ public:
   /// @brief Return true if this is a compare constant expression
   bool isCompare() const;
 
+  /// @brief Return true if this is an insertvalue or extractvalue expression,
+  /// and the getIndices() method may be used.
+  bool hasIndices() const;
+
   /// Select constant expr
   ///
   static Constant *getSelect(Constant *C, Constant *V1, Constant *V2) {
@@ -636,7 +677,8 @@ public:
   ///
   static Constant *get(unsigned Opcode, Constant *C1, Constant *C2);
 
-  /// @brief Return an ICmp or FCmp comparison operator constant expression.
+  /// @brief Return an ICmp, FCmp, VICmp, or VFCmp comparison operator constant
+  /// expression.
   static Constant *getCompare(unsigned short pred, Constant *C1, Constant *C2);
 
   /// ConstantExpr::get* - Return some common constants without having to
@@ -658,6 +700,8 @@ public:
   static Constant *getXor(Constant *C1, Constant *C2);
   static Constant *getICmp(unsigned short pred, Constant *LHS, Constant *RHS);
   static Constant *getFCmp(unsigned short pred, Constant *LHS, Constant *RHS);
+  static Constant *getVICmp(unsigned short pred, Constant *LHS, Constant *RHS);
+  static Constant *getVFCmp(unsigned short pred, Constant *LHS, Constant *RHS);
   static Constant *getShl(Constant *C1, Constant *C2);
   static Constant *getLShr(Constant *C1, Constant *C2);
   static Constant *getAShr(Constant *C1, Constant *C2);
@@ -673,6 +717,10 @@ public:
   static Constant *getExtractElement(Constant *Vec, Constant *Idx);
   static Constant *getInsertElement(Constant *Vec, Constant *Elt,Constant *Idx);
   static Constant *getShuffleVector(Constant *V1, Constant *V2, Constant *Mask);
+  static Constant *getExtractValue(Constant *Agg,
+                                   const unsigned *IdxList, unsigned NumIdx);
+  static Constant *getInsertValue(Constant *Agg, Constant *Val,
+                                  const unsigned *IdxList, unsigned NumIdx);
 
   /// Floating point negation must be implemented with f(x) = -0.0 - x. This
   /// method returns the negative zero constant for floating point or vector
@@ -690,6 +738,10 @@ public:
   /// not an ICMP or FCMP constant expression.
   unsigned getPredicate() const;
 
+  /// getIndices - Assert that this is an insertvalue or exactvalue
+  /// expression and return the list of indices.
+  const SmallVector<unsigned, 4> &getIndices() const;
+
   /// getOpcodeName - Return a string representation for an opcode.
   const char *getOpcodeName() const;
 
@@ -705,15 +757,6 @@ public:
   virtual void destroyConstant();
   virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
 
-  /// 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 Value *V) {
@@ -721,6 +764,11 @@ public:
   }
 };
 
+template <>
+struct OperandTraits<ConstantExpr> : VariadicOperandTraits<1> {
+};
+
+DEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(ConstantExpr, Constant)
 
 //===----------------------------------------------------------------------===//
 /// UndefValue - 'undef' values are things that do not have specified contents.
@@ -730,13 +778,15 @@ public:
 ///
 class UndefValue : public Constant {
   friend struct ConstantCreator<UndefValue, Type, char>;
+  void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
   UndefValue(const UndefValue &);      // DO NOT IMPLEMENT
 protected:
   explicit UndefValue(const Type *T) : Constant(T, UndefValueVal, 0, 0) {}
-  static void destroyThis(UndefValue*v) {
-    Constant::destroyThis(v);
+protected:
+  // allocate space for exactly zero operands
+  void *operator new(size_t s) {
+    return User::operator new(s, 0);
   }
-  friend class Value;
 public:
   /// get() - Static factory methods - Return an 'undef' object of the specified
   /// type.
@@ -756,120 +806,6 @@ public:
   }
 };
 
-/// GetElementPtrConstantExpr - Helper class for Constants.cpp, 
-/// used behind the scenes to implement getelementpr constant exprs.
-class GetElementPtrConstantExpr : public ConstantExpr {
-protected:
-  static void destroyThis(GetElementPtrConstantExpr*v) {
-    delete [] v->OperandList;
-    ConstantExpr::destroyThis(v);
-  }
-  friend class Value;
-public:
-  GetElementPtrConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
-                                                       const Type *DestTy);
-};
-
-/// UnaryConstantExpr - Helper class for Constants.cpp, used
-/// behind the scenes to implement unary constant exprs.
-class UnaryConstantExpr : public ConstantExpr {
-  Use Op;
-protected:
-  static void destroyThis(UnaryConstantExpr*v) {
-    ConstantExpr::destroyThis(v);
-  }
-  friend class Value;
-public:
-  UnaryConstantExpr(unsigned Opcode, Constant *C, const Type *Ty);
-};
-
-/// BinaryConstantExpr - Helper class for Constants.cpp, used
-/// behind the scenes to implement binary constant exprs.
-class BinaryConstantExpr : public ConstantExpr {
-  Use Ops[2];
-protected:
-  static void destroyThis(BinaryConstantExpr*v) {
-    ConstantExpr::destroyThis(v);
-  }
-  friend class Value;
-public:
-  BinaryConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
-    : ConstantExpr(C1->getType(), Opcode, Ops, 2) {
-    Ops[0].init(C1, this);
-    Ops[1].init(C2, this);
-  }
-};
-
-/// SelectConstantExpr - Helper class for Constants.cpp, used
-/// behind the scenes to implement select constant exprs.
-class SelectConstantExpr : public ConstantExpr {
-  Use Ops[3];
-protected:
-  static void destroyThis(SelectConstantExpr*v) {
-    ConstantExpr::destroyThis(v);
-  }
-  friend class Value;
-public:
-  SelectConstantExpr(Constant *C1, Constant *C2, Constant *C3);
-};
-
-/// ExtractElementConstantExpr - Helper class for Constants.cpp, used 
-/// behind the scenes to implement extractelement constant exprs.
-class ExtractElementConstantExpr : public ConstantExpr {
-  Use Ops[2];
-protected:
-  static void destroyThis(ExtractElementConstantExpr*v) {
-    ConstantExpr::destroyThis(v);
-  }
-  friend class Value;
-public:
-  ExtractElementConstantExpr(Constant *C1, Constant *C2);
-};
-
-/// InsertElementConstantExpr - Helper class for Constants.cpp, used 
-/// behind the scenes to implement insertelement constant exprs.
-class InsertElementConstantExpr : public ConstantExpr {
-  Use Ops[3];
-protected:
-  static void destroyThis(InsertElementConstantExpr*v) {
-    ConstantExpr::destroyThis(v);
-  }
-  friend class Value;
-public:
-  InsertElementConstantExpr(Constant *C1, Constant *C2, Constant *C3);
-};
-
-/// ShuffleVectorConstantExpr - Helper class for Constants.cpp, used 
-/// behind the scenes to implement shufflevector constant exprs.
-class ShuffleVectorConstantExpr : public ConstantExpr {
-  Use Ops[3];
-protected:
-  static void destroyThis(ShuffleVectorConstantExpr*v) {
-    ConstantExpr::destroyThis(v);
-  }
-  friend class Value;
-public:
-  ShuffleVectorConstantExpr(Constant *C1, Constant *C2, Constant *C3);
-};
-
-
-
-// CompareConstantExpr - Helper class for Constants.cpp, used
-// behind the scenes to implement ICmp and FCmp constant expressions. This is
-// needed in order to store the predicate value for these instructions.
-class CompareConstantExpr : public ConstantExpr {
-protected:
-  static void destroyThis(CompareConstantExpr*v) {
-    ConstantExpr::destroyThis(v);
-  }
-  friend class Value;
-public:
-  unsigned short predicate;
-  Use Ops[2];
-  CompareConstantExpr(unsigned opc, unsigned short pred, 
-                      Constant* LHS, Constant* RHS);
-};
-
 } // End llvm namespace
 
 #endif