Introduce a new ConstantVector::getSplat constructor function to

simplify a really common case.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@148901 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner 2012-01-25 01:53:58 +00:00
parent dc81eae9d2
commit 82385c9a40
2 changed files with 64 additions and 28 deletions

View File

@ -489,6 +489,10 @@ public:
// ConstantVector accessors // ConstantVector accessors
static Constant *get(ArrayRef<Constant*> V); 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. /// Transparently provide more efficient getOperand methods.
DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant); DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
@ -757,6 +761,11 @@ public:
static Constant *get(LLVMContext &Context, ArrayRef<float> Elts); static Constant *get(LLVMContext &Context, ArrayRef<float> Elts);
static Constant *get(LLVMContext &Context, ArrayRef<double> 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);
/// getType - Specialize the getType() method to always return a VectorType, /// getType - Specialize the getType() method to always return a VectorType,
/// which reduces the amount of casting needed in parts of the compiler. /// which reduces the amount of casting needed in parts of the compiler.
/// ///

View File

@ -129,7 +129,7 @@ Constant *Constant::getIntegerValue(Type *Ty, const APInt &V) {
// Broadcast a scalar to a vector, if necessary. // Broadcast a scalar to a vector, if necessary.
if (VectorType *VTy = dyn_cast<VectorType>(Ty)) if (VectorType *VTy = dyn_cast<VectorType>(Ty))
C = ConstantVector::get(std::vector<Constant *>(VTy->getNumElements(), C)); C = ConstantVector::getSplat(VTy->getNumElements(), C);
return C; return C;
} }
@ -145,11 +145,9 @@ Constant *Constant::getAllOnesValue(Type *Ty) {
return ConstantFP::get(Ty->getContext(), FL); return ConstantFP::get(Ty->getContext(), FL);
} }
SmallVector<Constant*, 16> Elts;
VectorType *VTy = cast<VectorType>(Ty); VectorType *VTy = cast<VectorType>(Ty);
Elts.resize(VTy->getNumElements(), getAllOnesValue(VTy->getElementType())); return ConstantVector::getSplat(VTy->getNumElements(),
assert(Elts[0] && "Invalid AllOnes value!"); getAllOnesValue(VTy->getElementType()));
return cast<ConstantVector>(ConstantVector::get(Elts));
} }
void Constant::destroyConstantImpl() { void Constant::destroyConstantImpl() {
@ -394,9 +392,8 @@ Constant *ConstantInt::getTrue(Type *Ty) {
} }
assert(VTy->getElementType()->isIntegerTy(1) && assert(VTy->getElementType()->isIntegerTy(1) &&
"True must be vector of i1 or i1."); "True must be vector of i1 or i1.");
SmallVector<Constant*, 16> Splat(VTy->getNumElements(), return ConstantVector::getSplat(VTy->getNumElements(),
ConstantInt::getTrue(Ty->getContext())); ConstantInt::getTrue(Ty->getContext()));
return ConstantVector::get(Splat);
} }
Constant *ConstantInt::getFalse(Type *Ty) { Constant *ConstantInt::getFalse(Type *Ty) {
@ -407,9 +404,8 @@ Constant *ConstantInt::getFalse(Type *Ty) {
} }
assert(VTy->getElementType()->isIntegerTy(1) && assert(VTy->getElementType()->isIntegerTy(1) &&
"False must be vector of i1 or i1."); "False must be vector of i1 or i1.");
SmallVector<Constant*, 16> Splat(VTy->getNumElements(), return ConstantVector::getSplat(VTy->getNumElements(),
ConstantInt::getFalse(Ty->getContext())); ConstantInt::getFalse(Ty->getContext()));
return ConstantVector::get(Splat);
} }
@ -433,8 +429,7 @@ Constant *ConstantInt::get(Type *Ty, uint64_t V, bool isSigned) {
// For vectors, broadcast the value. // For vectors, broadcast the value.
if (VectorType *VTy = dyn_cast<VectorType>(Ty)) if (VectorType *VTy = dyn_cast<VectorType>(Ty))
return ConstantVector::get(SmallVector<Constant*, return ConstantVector::getSplat(VTy->getNumElements(), C);
16>(VTy->getNumElements(), C));
return C; return C;
} }
@ -459,8 +454,7 @@ Constant *ConstantInt::get(Type* Ty, const APInt& V) {
// For vectors, broadcast the value. // For vectors, broadcast the value.
if (VectorType *VTy = dyn_cast<VectorType>(Ty)) if (VectorType *VTy = dyn_cast<VectorType>(Ty))
return ConstantVector::get( return ConstantVector::getSplat(VTy->getNumElements(), C);
SmallVector<Constant *, 16>(VTy->getNumElements(), C));
return C; return C;
} }
@ -506,8 +500,7 @@ Constant *ConstantFP::get(Type* Ty, double V) {
// For vectors, broadcast the value. // For vectors, broadcast the value.
if (VectorType *VTy = dyn_cast<VectorType>(Ty)) if (VectorType *VTy = dyn_cast<VectorType>(Ty))
return ConstantVector::get( return ConstantVector::getSplat(VTy->getNumElements(), C);
SmallVector<Constant *, 16>(VTy->getNumElements(), C));
return C; return C;
} }
@ -521,8 +514,7 @@ Constant *ConstantFP::get(Type* Ty, StringRef Str) {
// For vectors, broadcast the value. // For vectors, broadcast the value.
if (VectorType *VTy = dyn_cast<VectorType>(Ty)) if (VectorType *VTy = dyn_cast<VectorType>(Ty))
return ConstantVector::get( return ConstantVector::getSplat(VTy->getNumElements(), C);
SmallVector<Constant *, 16>(VTy->getNumElements(), C));
return C; return C;
} }
@ -537,15 +529,12 @@ ConstantFP* ConstantFP::getNegativeZero(Type* Ty) {
Constant *ConstantFP::getZeroValueForNegation(Type* Ty) { Constant *ConstantFP::getZeroValueForNegation(Type* Ty) {
if (VectorType *PTy = dyn_cast<VectorType>(Ty)) if (Ty->getScalarType()->isFloatingPointTy()) {
if (PTy->getElementType()->isFloatingPointTy()) { Constant *C = getNegativeZero(Ty);
SmallVector<Constant*, 16> zeros(PTy->getNumElements(), if (VectorType *VTy = dyn_cast<VectorType>(Ty))
getNegativeZero(PTy->getElementType())); return ConstantVector::getSplat(VTy->getNumElements(), C);
return ConstantVector::get(zeros); return C;
} }
if (Ty->isFloatingPointTy())
return getNegativeZero(Ty);
return Constant::getNullValue(Ty); return Constant::getNullValue(Ty);
} }
@ -818,6 +807,12 @@ Constant *ConstantVector::get(ArrayRef<Constant*> V) {
return pImpl->VectorConstants.getOrCreate(T, V); return pImpl->VectorConstants.getOrCreate(T, V);
} }
Constant *ConstantVector::getSplat(unsigned NumElts, Constant *V) {
SmallVector<Constant*, 32> Elts(NumElts, V);
return get(Elts);
}
// Utility function for determining if a ConstantExpr is a CastOp or not. This // 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 // can't be inline because we don't want to #include Instruction.h into
// Constant.h // Constant.h
@ -2194,6 +2189,38 @@ Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<double> Elts) {
return getImpl(StringRef((char*)Elts.data(), Elts.size()*8), Ty); 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<ConstantInt>(V)) {
if (CI->getType()->isIntegerTy(8)) {
SmallVector<uint8_t, 16> Elts(NumElts, CI->getZExtValue());
return get(V->getContext(), Elts);
}
if (CI->getType()->isIntegerTy(16)) {
SmallVector<uint16_t, 16> Elts(NumElts, CI->getZExtValue());
return get(V->getContext(), Elts);
}
if (CI->getType()->isIntegerTy(32)) {
SmallVector<uint32_t, 16> Elts(NumElts, CI->getZExtValue());
return get(V->getContext(), Elts);
}
assert(CI->getType()->isIntegerTy(64) && "Unsupported ConstantData type");
SmallVector<uint64_t, 16> Elts(NumElts, CI->getZExtValue());
return get(V->getContext(), Elts);
}
ConstantFP *CFP = cast<ConstantFP>(V);
if (CFP->getType()->isFloatTy()) {
SmallVector<float, 16> Elts(NumElts, CFP->getValueAPF().convertToFloat());
return get(V->getContext(), Elts);
}
assert(CFP->getType()->isDoubleTy() && "Unsupported ConstantData type");
SmallVector<double, 16> Elts(NumElts, CFP->getValueAPF().convertToDouble());
return get(V->getContext(), Elts);
}
/// getElementAsInteger - If this is a sequential container of integers (of /// getElementAsInteger - If this is a sequential container of integers (of
/// any size), return the specified element in the low bits of a uint64_t. /// any size), return the specified element in the low bits of a uint64_t.
uint64_t ConstantDataSequential::getElementAsInteger(unsigned Elt) const { uint64_t ConstantDataSequential::getElementAsInteger(unsigned Elt) const {