OSDN Git Service

Add a factory method to ConstantDataArray that allows to pass in the data as StringRef
authorAdrian Kuegel <akuegel@google.com>
Tue, 19 Jun 2018 08:12:28 +0000 (08:12 +0000)
committerAdrian Kuegel <akuegel@google.com>
Tue, 19 Jun 2018 08:12:28 +0000 (08:12 +0000)
This simplifies the case if we already have access to the raw data that we need to store in a ConstantDataArray.
The new factor method can also be reused for implementing the factory method that gets the data as ArrayRef.

Differential Revision: https://reviews.llvm.org/D47706

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@335028 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/IR/Constants.h

index d2e5f2a..b6394f5 100644 (file)
@@ -698,9 +698,8 @@ public:
   template <typename ElementTy>
   static Constant *get(LLVMContext &Context, ArrayRef<ElementTy> Elts) {
     const char *Data = reinterpret_cast<const char *>(Elts.data());
-    Type *Ty =
-        ArrayType::get(Type::getScalarTy<ElementTy>(Context), Elts.size());
-    return getImpl(StringRef(Data, Elts.size() * sizeof(ElementTy)), Ty);
+    return getRaw(StringRef(Data, Elts.size() * sizeof(ElementTy)), Elts.size(),
+                  Type::getScalarTy<ElementTy>(Context));
   }
 
   /// get() constructor - ArrayTy needs to be compatible with
@@ -710,6 +709,17 @@ public:
     return ConstantDataArray::get(Context, makeArrayRef(Elts));
   }
 
+  /// get() constructor - Return a constant with array type with an element
+  /// count and element type matching the NumElements and ElementTy parameters
+  /// passed in. Note that this can return a ConstantAggregateZero object.
+  /// ElementTy needs to be one of i8/i16/i32/i64/float/double. Data is the
+  /// buffer containing the elements. Be careful to make sure Data uses the
+  /// right endianness, the buffer will be used as-is.
+  static Constant *getRaw(StringRef Data, uint64_t NumElements, Type *ElementTy) {
+    Type *Ty = ArrayType::get(ElementTy, NumElements);
+    return getImpl(Data, Ty);
+  }
+
   /// getFP() constructors - Return a constant with array type with an element
   /// count and element type of float with precision matching the number of
   /// bits in the ArrayRef passed in. (i.e. half for 16bits, float for 32bits,