diff --git a/include/llvm/Constants.h b/include/llvm/Constants.h index d3fd766ee7e..1823f1d49ed 100644 --- a/include/llvm/Constants.h +++ b/include/llvm/Constants.h @@ -489,6 +489,10 @@ public: // ConstantVector accessors static Constant *get(ArrayRef 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); @@ -757,6 +761,11 @@ public: static Constant *get(LLVMContext &Context, ArrayRef Elts); static Constant *get(LLVMContext &Context, ArrayRef 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); + /// getType - Specialize the getType() method to always return a VectorType, /// which reduces the amount of casting needed in parts of the compiler. /// diff --git a/lib/VMCore/Constants.cpp b/lib/VMCore/Constants.cpp index 6cb36bf21b3..dd13ace600e 100644 --- a/lib/VMCore/Constants.cpp +++ b/lib/VMCore/Constants.cpp @@ -129,7 +129,7 @@ Constant *Constant::getIntegerValue(Type *Ty, const APInt &V) { // Broadcast a scalar to a vector, if necessary. if (VectorType *VTy = dyn_cast(Ty)) - C = ConstantVector::get(std::vector(VTy->getNumElements(), C)); + C = ConstantVector::getSplat(VTy->getNumElements(), C); return C; } @@ -145,11 +145,9 @@ Constant *Constant::getAllOnesValue(Type *Ty) { return ConstantFP::get(Ty->getContext(), FL); } - SmallVector Elts; VectorType *VTy = cast(Ty); - Elts.resize(VTy->getNumElements(), getAllOnesValue(VTy->getElementType())); - assert(Elts[0] && "Invalid AllOnes value!"); - return cast(ConstantVector::get(Elts)); + return ConstantVector::getSplat(VTy->getNumElements(), + getAllOnesValue(VTy->getElementType())); } void Constant::destroyConstantImpl() { @@ -394,9 +392,8 @@ Constant *ConstantInt::getTrue(Type *Ty) { } assert(VTy->getElementType()->isIntegerTy(1) && "True must be vector of i1 or i1."); - SmallVector Splat(VTy->getNumElements(), - ConstantInt::getTrue(Ty->getContext())); - return ConstantVector::get(Splat); + return ConstantVector::getSplat(VTy->getNumElements(), + ConstantInt::getTrue(Ty->getContext())); } Constant *ConstantInt::getFalse(Type *Ty) { @@ -407,9 +404,8 @@ Constant *ConstantInt::getFalse(Type *Ty) { } assert(VTy->getElementType()->isIntegerTy(1) && "False must be vector of i1 or i1."); - SmallVector Splat(VTy->getNumElements(), - ConstantInt::getFalse(Ty->getContext())); - return ConstantVector::get(Splat); + return ConstantVector::getSplat(VTy->getNumElements(), + ConstantInt::getFalse(Ty->getContext())); } @@ -433,8 +429,7 @@ Constant *ConstantInt::get(Type *Ty, uint64_t V, bool isSigned) { // For vectors, broadcast the value. if (VectorType *VTy = dyn_cast(Ty)) - return ConstantVector::get(SmallVector(VTy->getNumElements(), C)); + return ConstantVector::getSplat(VTy->getNumElements(), C); return C; } @@ -459,8 +454,7 @@ Constant *ConstantInt::get(Type* Ty, const APInt& V) { // For vectors, broadcast the value. if (VectorType *VTy = dyn_cast(Ty)) - return ConstantVector::get( - SmallVector(VTy->getNumElements(), C)); + return ConstantVector::getSplat(VTy->getNumElements(), C); return C; } @@ -506,8 +500,7 @@ Constant *ConstantFP::get(Type* Ty, double V) { // For vectors, broadcast the value. if (VectorType *VTy = dyn_cast(Ty)) - return ConstantVector::get( - SmallVector(VTy->getNumElements(), C)); + return ConstantVector::getSplat(VTy->getNumElements(), C); return C; } @@ -521,31 +514,28 @@ Constant *ConstantFP::get(Type* Ty, StringRef Str) { // For vectors, broadcast the value. if (VectorType *VTy = dyn_cast(Ty)) - return ConstantVector::get( - SmallVector(VTy->getNumElements(), C)); + return ConstantVector::getSplat(VTy->getNumElements(), C); return C; } -ConstantFP* ConstantFP::getNegativeZero(Type* Ty) { +ConstantFP *ConstantFP::getNegativeZero(Type *Ty) { LLVMContext &Context = Ty->getContext(); - APFloat apf = cast (Constant::getNullValue(Ty))->getValueAPF(); + APFloat apf = cast(Constant::getNullValue(Ty))->getValueAPF(); apf.changeSign(); return get(Context, apf); } -Constant *ConstantFP::getZeroValueForNegation(Type* Ty) { - if (VectorType *PTy = dyn_cast(Ty)) - if (PTy->getElementType()->isFloatingPointTy()) { - SmallVector zeros(PTy->getNumElements(), - getNegativeZero(PTy->getElementType())); - return ConstantVector::get(zeros); - } - - if (Ty->isFloatingPointTy()) - return getNegativeZero(Ty); +Constant *ConstantFP::getZeroValueForNegation(Type *Ty) { + Type *ScalarTy = Ty->getScalarType(); + if (ScalarTy->isFloatingPointTy()) { + Constant *C = getNegativeZero(ScalarTy); + if (VectorType *VTy = dyn_cast(Ty)) + return ConstantVector::getSplat(VTy->getNumElements(), C); + return C; + } return Constant::getNullValue(Ty); } @@ -818,6 +808,12 @@ Constant *ConstantVector::get(ArrayRef V) { return pImpl->VectorConstants.getOrCreate(T, V); } +Constant *ConstantVector::getSplat(unsigned NumElts, Constant *V) { + SmallVector Elts(NumElts, V); + return get(Elts); +} + + // Utility function for determining if a ConstantExpr is a CastOp or not. This // can't be inline because we don't want to #include Instruction.h into // Constant.h @@ -2196,6 +2192,38 @@ Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef Elts) { return getImpl(StringRef((char*)Elts.data(), Elts.size()*8), Ty); } +Constant *ConstantDataVector::getSplat(unsigned NumElts, Constant *V) { + assert(isElementTypeCompatible(V->getType()) && + "Element type not compatible with ConstantData"); + if (ConstantInt *CI = dyn_cast(V)) { + if (CI->getType()->isIntegerTy(8)) { + SmallVector Elts(NumElts, CI->getZExtValue()); + return get(V->getContext(), Elts); + } + if (CI->getType()->isIntegerTy(16)) { + SmallVector Elts(NumElts, CI->getZExtValue()); + return get(V->getContext(), Elts); + } + if (CI->getType()->isIntegerTy(32)) { + SmallVector Elts(NumElts, CI->getZExtValue()); + return get(V->getContext(), Elts); + } + assert(CI->getType()->isIntegerTy(64) && "Unsupported ConstantData type"); + SmallVector Elts(NumElts, CI->getZExtValue()); + return get(V->getContext(), Elts); + } + + ConstantFP *CFP = cast(V); + if (CFP->getType()->isFloatTy()) { + SmallVector Elts(NumElts, CFP->getValueAPF().convertToFloat()); + return get(V->getContext(), Elts); + } + assert(CFP->getType()->isDoubleTy() && "Unsupported ConstantData type"); + SmallVector Elts(NumElts, CFP->getValueAPF().convertToDouble()); + return get(V->getContext(), Elts); +} + + /// getElementAsInteger - If this is a sequential container of integers (of /// any size), return the specified element in the low bits of a uint64_t. uint64_t ConstantDataSequential::getElementAsInteger(unsigned Elt) const {