Index: llvm/trunk/include/llvm/IR/Constants.h =================================================================== --- llvm/trunk/include/llvm/IR/Constants.h +++ llvm/trunk/include/llvm/IR/Constants.h @@ -692,15 +692,23 @@ public: ConstantDataArray(const ConstantDataArray &) = delete; - /// get() constructors - Return a constant with array type with an element + /// get() constructor - 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(LLVMContext &Context, ArrayRef Elts); - static Constant *get(LLVMContext &Context, ArrayRef Elts); - static Constant *get(LLVMContext &Context, ArrayRef Elts); - static Constant *get(LLVMContext &Context, ArrayRef Elts); - static Constant *get(LLVMContext &Context, ArrayRef Elts); - static Constant *get(LLVMContext &Context, ArrayRef Elts); + template + static Constant *get(LLVMContext &Context, ArrayRef Elts) { + const char *Data = reinterpret_cast(Elts.data()); + Type *Ty = + ArrayType::get(Type::getScalarTy(Context), Elts.size()); + return getImpl(StringRef(Data, Elts.size() * sizeof(ElementTy)), Ty); + } + + /// get() constructor - ArrayTy needs to be compatible with + /// ArrayRef. Calls get(LLVMContext, ArrayRef). + template + static Constant *get(LLVMContext &Context, ArrayTy &Elts) { + return ConstantDataArray::get(Context, makeArrayRef(Elts)); + } /// getFP() constructors - Return a constant with array type with an element /// count and element type of float with precision matching the number of Index: llvm/trunk/include/llvm/IR/Type.h =================================================================== --- llvm/trunk/include/llvm/IR/Type.h +++ llvm/trunk/include/llvm/IR/Type.h @@ -407,6 +407,20 @@ static IntegerType *getInt32Ty(LLVMContext &C); static IntegerType *getInt64Ty(LLVMContext &C); static IntegerType *getInt128Ty(LLVMContext &C); + template static Type *getScalarTy(LLVMContext &C) { + int noOfBits = sizeof(ScalarTy) * CHAR_BIT; + if (std::is_integral::value) { + return Type::getIntNTy(C, noOfBits); + } else if (std::is_floating_point::value) { + switch (noOfBits) { + case 32: + return Type::getFloatTy(C); + case 64: + return Type::getDoubleTy(C); + } + } + llvm_unreachable("Unsupported type in Type::getScalarTy"); + } //===--------------------------------------------------------------------===// // Convenience methods for getting pointer types with one of the above builtin Index: llvm/trunk/lib/IR/Constants.cpp =================================================================== --- llvm/trunk/lib/IR/Constants.cpp +++ llvm/trunk/lib/IR/Constants.cpp @@ -2452,40 +2452,6 @@ Next = nullptr; } -/// 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. -Constant *ConstantDataArray::get(LLVMContext &Context, ArrayRef Elts) { - Type *Ty = ArrayType::get(Type::getInt8Ty(Context), Elts.size()); - const char *Data = reinterpret_cast(Elts.data()); - return getImpl(StringRef(Data, Elts.size() * 1), Ty); -} -Constant *ConstantDataArray::get(LLVMContext &Context, ArrayRef Elts){ - Type *Ty = ArrayType::get(Type::getInt16Ty(Context), Elts.size()); - const char *Data = reinterpret_cast(Elts.data()); - return getImpl(StringRef(Data, Elts.size() * 2), Ty); -} -Constant *ConstantDataArray::get(LLVMContext &Context, ArrayRef Elts){ - Type *Ty = ArrayType::get(Type::getInt32Ty(Context), Elts.size()); - const char *Data = reinterpret_cast(Elts.data()); - return getImpl(StringRef(Data, Elts.size() * 4), Ty); -} -Constant *ConstantDataArray::get(LLVMContext &Context, ArrayRef Elts){ - Type *Ty = ArrayType::get(Type::getInt64Ty(Context), Elts.size()); - const char *Data = reinterpret_cast(Elts.data()); - return getImpl(StringRef(Data, Elts.size() * 8), Ty); -} -Constant *ConstantDataArray::get(LLVMContext &Context, ArrayRef Elts) { - Type *Ty = ArrayType::get(Type::getFloatTy(Context), Elts.size()); - const char *Data = reinterpret_cast(Elts.data()); - return getImpl(StringRef(Data, Elts.size() * 4), Ty); -} -Constant *ConstantDataArray::get(LLVMContext &Context, ArrayRef Elts) { - Type *Ty = ArrayType::get(Type::getDoubleTy(Context), Elts.size()); - const char *Data = reinterpret_cast(Elts.data()); - return getImpl(StringRef(Data, Elts.size() * 8), 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,