Move isKnownNonNull from private implementation detail of BasicAA to a public
[oota-llvm.git] / include / llvm / Constants.h
index 958ebb88c75b5720f838f83b9209b7e7edef06d2..0abe17d365d4e83cc9c704bdba8670e83db3612d 100644 (file)
@@ -39,6 +39,8 @@ class SequentialType;
 template<class ConstantClass, class TypeClass, class ValType>
 struct ConstantCreator;
 template<class ConstantClass, class TypeClass>
+struct ConstantArrayCreator;
+template<class ConstantClass, class TypeClass>
 struct ConvertConstantType;
 
 //===----------------------------------------------------------------------===//
@@ -316,19 +318,19 @@ public:
 
   /// getSequentialElement - If this CAZ has array or vector type, return a zero
   /// with the right element type.
-  Constant *getSequentialElement();
+  Constant *getSequentialElement() const;
 
   /// getStructElement - If this CAZ has struct type, return a zero with the
   /// right element type for the specified element.
-  Constant *getStructElement(unsigned Elt);
+  Constant *getStructElement(unsigned Elt) const;
 
   /// getElementValue - Return a zero of the right value for the specified GEP
   /// index.
-  Constant *getElementValue(Constant *C);
+  Constant *getElementValue(Constant *C) const;
 
   /// getElementValue - Return a zero of the right value for the specified GEP
   /// index.
-  Constant *getElementValue(unsigned Idx);
+  Constant *getElementValue(unsigned Idx) const;
 
   /// Methods for support type inquiry through isa, cast, and dyn_cast:
   ///
@@ -343,8 +345,7 @@ public:
 /// ConstantArray - Constant Array Declarations
 ///
 class ConstantArray : public Constant {
-  friend struct ConstantCreator<ConstantArray, ArrayType,
-                                    std::vector<Constant*> >;
+  friend struct ConstantArrayCreator<ConstantArray, ArrayType>;
   ConstantArray(const ConstantArray &);      // DO NOT IMPLEMENT
 protected:
   ConstantArray(ArrayType *T, ArrayRef<Constant *> Val);
@@ -352,15 +353,6 @@ public:
   // ConstantArray accessors
   static Constant *get(ArrayType *T, ArrayRef<Constant*> V);
                              
-  /// This method constructs a ConstantArray and initializes it with a text
-  /// string. The default behavior (AddNull==true) causes a null terminator to
-  /// be placed at the end of the array. This effectively increases the length
-  /// of the array by one (you've been warned).  However, in some situations 
-  /// this is not desired so if AddNull==false then the string is copied without
-  /// null termination.
-  static Constant *get(LLVMContext &Context, StringRef Initializer,
-                       bool AddNull = true);
-  
   /// Transparently provide more efficient getOperand methods.
   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
 
@@ -371,28 +363,6 @@ public:
     return reinterpret_cast<ArrayType*>(Value::getType());
   }
 
-  /// 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
-  /// @verbatim
-  /// isString) and it ends in a null byte \0 and does not contains any other
-  /// @endverbatim
-  /// null bytes except its terminator.
-  bool isCString() const;
-
-  /// getAsString - If this array is isString(), then this method converts the
-  /// array to an std::string and returns it.  Otherwise, it asserts out.
-  ///
-  std::string getAsString() const;
-
-  /// getAsCString - If this array is isCString(), then this method converts the
-  /// array (without the trailing null byte) to an std::string and returns it.
-  /// Otherwise, it asserts out.
-  ///
-  std::string getAsCString() const;
-
   virtual void destroyConstant();
   virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
 
@@ -414,8 +384,7 @@ DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantArray, Constant)
 // ConstantStruct - Constant Struct Declarations
 //
 class ConstantStruct : public Constant {
-  friend struct ConstantCreator<ConstantStruct, StructType,
-                                    std::vector<Constant*> >;
+  friend struct ConstantArrayCreator<ConstantStruct, StructType>;
   ConstantStruct(const ConstantStruct &);      // DO NOT IMPLEMENT
 protected:
   ConstantStruct(StructType *T, ArrayRef<Constant *> Val);
@@ -475,8 +444,7 @@ DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantStruct, Constant)
 /// ConstantVector - Constant Vector Declarations
 ///
 class ConstantVector : public Constant {
-  friend struct ConstantCreator<ConstantVector, VectorType,
-                                    std::vector<Constant*> >;
+  friend struct ConstantArrayCreator<ConstantVector, VectorType>;
   ConstantVector(const ConstantVector &);      // DO NOT IMPLEMENT
 protected:
   ConstantVector(VectorType *T, ArrayRef<Constant *> Val);
@@ -484,6 +452,10 @@ public:
   // ConstantVector accessors
   static Constant *get(ArrayRef<Constant*> V);
   
+  /// getSplat - Return a ConstantVector with the specified constant in each
+  /// element.
+  static Constant *getSplat(unsigned NumElts, Constant *Elt);
+  
   /// Transparently provide more efficient getOperand methods.
   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
 
@@ -552,10 +524,13 @@ public:
 };
   
 //===----------------------------------------------------------------------===//
-/// ConstantDataSequential - A vector or array of data that contains no
-/// relocations, and whose element type is a simple 1/2/4/8-byte integer or
-/// float/double.  This is the common base class of ConstantDataArray and
-/// ConstantDataVector.
+/// ConstantDataSequential - A vector or array constant whose element type is a
+/// simple 1/2/4/8-byte integer or float/double, and whose elements are just
+/// simple data values (i.e. ConstantInt/ConstantFP).  This Constant node has no
+/// operands because it stores all of the elements of the constant as densely
+/// packed data, instead of as Value*'s.
+///
+/// This is the common base class of ConstantDataArray and ConstantDataVector.
 ///
 class ConstantDataSequential : public Constant {
   friend class LLVMContextImpl;
@@ -572,7 +547,7 @@ class ConstantDataSequential : public Constant {
   ConstantDataSequential(const ConstantDataSequential &);    // DO NOT IMPLEMENT
 protected:
   explicit ConstantDataSequential(Type *ty, ValueTy VT, const char *Data)
-    : Constant(ty, VT, 0, 0), DataElements(Data) {}
+    : Constant(ty, VT, 0, 0), DataElements(Data), Next(0) {}
   ~ConstantDataSequential() { delete Next; }
   
   static Constant *getImpl(StringRef Bytes, Type *Ty);
@@ -603,7 +578,7 @@ public:
   float getElementAsFloat(unsigned i) const;
   
   /// getElementAsDouble - If this is an sequential container of doubles, return
-  /// the specified element as a float.
+  /// the specified element as a double.
   double getElementAsDouble(unsigned i) const;
   
   /// getElementAsConstant - Return a Constant for a specified index's element.
@@ -620,12 +595,46 @@ public:
   
   /// getElementType - Return the element type of the array/vector.
   Type *getElementType() const;
+  
+  /// getNumElements - Return the number of elements in the array or vector.
+  unsigned getNumElements() const;
 
   /// getElementByteSize - Return the size (in bytes) of each element in the
   /// array/vector.  The size of the elements is known to be a multiple of one
   /// byte.
   uint64_t getElementByteSize() const;
 
+  
+  /// isString - This method returns true if this is an array of i8.
+  bool isString() const;
+  
+  /// isCString - This method returns true if the array "isString", ends with a
+  /// nul byte, and does not contains any other nul bytes.
+  bool isCString() const;
+  
+  /// getAsString - If this array is isString(), then this method returns the
+  /// array as a StringRef.  Otherwise, it asserts out.
+  ///
+  StringRef getAsString() const {
+    assert(isString() && "Not a string");
+    return getRawDataValues();
+  }
+  
+  /// getAsCString - If this array is isCString(), then this method returns the
+  /// array (without the trailing null byte) as a StringRef. Otherwise, it
+  /// asserts out.
+  ///
+  StringRef getAsCString() const {
+    assert(isCString() && "Isn't a C string");
+    StringRef Str = getAsString();
+    return Str.substr(0, Str.size()-1);
+  }
+  
+  /// getRawDataValues - Return the raw, underlying, bytes of this data.  Note
+  /// that this is an extremely tricky thing to work with, as it exposes the
+  /// host endianness of the data elements.
+  StringRef getRawDataValues() const;
+  
   virtual void destroyConstant();
   
   /// Methods for support type inquiry through isa, cast, and dyn_cast:
@@ -640,9 +649,11 @@ private:
 };
 
 //===----------------------------------------------------------------------===//
-/// ConstantDataArray - An array of data that contains no relocations, and whose
-/// element type is a simple 1/2/4/8-byte integer or float/double.
-///
+/// ConstantDataArray - An array constant whose element type is a simple
+/// 1/2/4/8-byte integer or float/double, and whose elements are just simple
+/// data values (i.e. ConstantInt/ConstantFP).  This Constant node has no
+/// operands because it stores all of the elements of the constant as densely
+/// packed data, instead of as Value*'s.
 class ConstantDataArray : public ConstantDataSequential {
   void *operator new(size_t, unsigned);            // DO NOT IMPLEMENT
   ConstantDataArray(const ConstantDataArray &);    // DO NOT IMPLEMENT
@@ -660,13 +671,21 @@ public:
   /// get() constructors - Return a constant with array type with an element
   /// count and element type matching the ArrayRef passed in.  Note that this
   /// can return a ConstantAggregateZero object.
-  static Constant *get(ArrayRef<uint8_t> Elts, LLVMContext &Context);
-  static Constant *get(ArrayRef<uint16_t> Elts, LLVMContext &Context);
-  static Constant *get(ArrayRef<uint32_t> Elts, LLVMContext &Context);
-  static Constant *get(ArrayRef<uint64_t> Elts, LLVMContext &Context);
-  static Constant *get(ArrayRef<float> Elts, LLVMContext &Context);
-  static Constant *get(ArrayRef<double> Elts, LLVMContext &Context);
+  static Constant *get(LLVMContext &Context, ArrayRef<uint8_t> Elts);
+  static Constant *get(LLVMContext &Context, ArrayRef<uint16_t> Elts);
+  static Constant *get(LLVMContext &Context, ArrayRef<uint32_t> Elts);
+  static Constant *get(LLVMContext &Context, ArrayRef<uint64_t> Elts);
+  static Constant *get(LLVMContext &Context, ArrayRef<float> Elts);
+  static Constant *get(LLVMContext &Context, ArrayRef<double> Elts);
   
+  /// getString - This method constructs a CDS and initializes it with a text
+  /// string. The default behavior (AddNull==true) causes a null terminator to
+  /// be placed at the end of the array (increasing the length of the string by
+  /// one more than the StringRef would normally indicate.  Pass AddNull=false
+  /// to disable this behavior.
+  static Constant *getString(LLVMContext &Context, StringRef Initializer,
+                             bool AddNull = true);
+
   /// getType - Specialize the getType() method to always return an ArrayType,
   /// which reduces the amount of casting needed in parts of the compiler.
   ///
@@ -683,9 +702,11 @@ public:
 };
   
 //===----------------------------------------------------------------------===//
-/// ConstantDataVector - A vector of data that contains no relocations, and
-/// whose element type is a simple 1/2/4/8-byte integer or float/double.
-///
+/// ConstantDataVector - A vector constant whose element type is a simple
+/// 1/2/4/8-byte integer or float/double, and whose elements are just simple
+/// data values (i.e. ConstantInt/ConstantFP).  This Constant node has no
+/// operands because it stores all of the elements of the constant as densely
+/// packed data, instead of as Value*'s.
 class ConstantDataVector : public ConstantDataSequential {
   void *operator new(size_t, unsigned);              // DO NOT IMPLEMENT
   ConstantDataVector(const ConstantDataVector &);    // DO NOT IMPLEMENT
@@ -703,12 +724,21 @@ public:
   /// get() constructors - Return a constant with vector type with an element
   /// count and element type matching the ArrayRef passed in.  Note that this
   /// can return a ConstantAggregateZero object.
-  static Constant *get(ArrayRef<uint8_t> Elts, LLVMContext &Context);
-  static Constant *get(ArrayRef<uint16_t> Elts, LLVMContext &Context);
-  static Constant *get(ArrayRef<uint32_t> Elts, LLVMContext &Context);
-  static Constant *get(ArrayRef<uint64_t> Elts, LLVMContext &Context);
-  static Constant *get(ArrayRef<float> Elts, LLVMContext &Context);
-  static Constant *get(ArrayRef<double> Elts, LLVMContext &Context);
+  static Constant *get(LLVMContext &Context, ArrayRef<uint8_t> Elts);
+  static Constant *get(LLVMContext &Context, ArrayRef<uint16_t> Elts);
+  static Constant *get(LLVMContext &Context, ArrayRef<uint32_t> Elts);
+  static Constant *get(LLVMContext &Context, ArrayRef<uint64_t> Elts);
+  static Constant *get(LLVMContext &Context, ArrayRef<float> Elts);
+  static Constant *get(LLVMContext &Context, ArrayRef<double> Elts);
+  
+  /// getSplat - Return a ConstantVector with the specified constant in each
+  /// element.  The specified constant has to be a of a compatible type (i8/i16/
+  /// i32/i64/float/double) and must be a ConstantFP or ConstantInt.
+  static Constant *getSplat(unsigned NumElts, Constant *Elt);
+
+  /// getSplatValue - If this is a splat constant, meaning that all of the
+  /// elements have the same value, return that value. Otherwise return NULL.
+  Constant *getSplatValue() const;
   
   /// getType - Specialize the getType() method to always return a VectorType,
   /// which reduces the amount of casting needed in parts of the compiler.
@@ -1101,19 +1131,19 @@ public:
 
   /// getSequentialElement - If this Undef has array or vector type, return a
   /// undef with the right element type.
-  UndefValue *getSequentialElement();
+  UndefValue *getSequentialElement() const;
   
   /// getStructElement - If this undef has struct type, return a undef with the
   /// right element type for the specified element.
-  UndefValue *getStructElement(unsigned Elt);
+  UndefValue *getStructElement(unsigned Elt) const;
   
   /// getElementValue - Return an undef of the right value for the specified GEP
   /// index.
-  UndefValue *getElementValue(Constant *C);
+  UndefValue *getElementValue(Constant *C) const;
 
   /// getElementValue - Return an undef of the right value for the specified GEP
   /// index.
-  UndefValue *getElementValue(unsigned Idx);
+  UndefValue *getElementValue(unsigned Idx) const;
 
   virtual void destroyConstant();