[IR] Add IRBuilderBase::CreateVectorSplat(ElementCount EC) variant

As discussed on D81500, this adds a more general ElementCount variant of the build helper and converts the (non-scalable) unsigned NumElts variant to use it internally.
This commit is contained in:
Simon Pilgrim 2020-08-02 16:55:16 +01:00
parent 4abc69c6f5
commit e202236721
3 changed files with 16 additions and 4 deletions

View File

@ -2484,6 +2484,10 @@ public:
/// NumElts elements.
Value *CreateVectorSplat(unsigned NumElts, Value *V, const Twine &Name = "");
/// Return a vector value that contains \arg V broadcasted to \p
/// EC elements.
Value *CreateVectorSplat(ElementCount EC, Value *V, const Twine &Name = "");
/// Return a value that has been extracted from a larger integer type.
Value *CreateExtractInteger(const DataLayout &DL, Value *From,
IntegerType *ExtractedTy, uint64_t Offset,

View File

@ -996,17 +996,22 @@ Value *IRBuilderBase::CreateStripInvariantGroup(Value *Ptr) {
Value *IRBuilderBase::CreateVectorSplat(unsigned NumElts, Value *V,
const Twine &Name) {
assert(NumElts > 0 && "Cannot splat to an empty vector!");
ElementCount EC(NumElts, false);
return CreateVectorSplat(EC, V, Name);
}
Value *IRBuilderBase::CreateVectorSplat(ElementCount EC, Value *V,
const Twine &Name) {
assert(EC.Min > 0 && "Cannot splat to an empty vector!");
// First insert it into an undef vector so we can shuffle it.
Type *I32Ty = getInt32Ty();
Value *Undef = UndefValue::get(FixedVectorType::get(V->getType(), NumElts));
Value *Undef = UndefValue::get(VectorType::get(V->getType(), EC));
V = CreateInsertElement(Undef, V, ConstantInt::get(I32Ty, 0),
Name + ".splatinsert");
// Shuffle the value across the desired number of elements.
Value *Zeros =
ConstantAggregateZero::get(FixedVectorType::get(I32Ty, NumElts));
Value *Zeros = ConstantAggregateZero::get(VectorType::get(I32Ty, EC));
return CreateShuffleVector(V, Undef, Zeros, Name + ".splat");
}

View File

@ -93,6 +93,9 @@ TEST_F(BasicTest, isSplat) {
Value *SplatC = IRB.CreateVectorSplat(5, ScalarC);
EXPECT_TRUE(isSplatValue(SplatC));
Value *SplatC_SVE = IRB.CreateVectorSplat(ElementCount(5, true), ScalarC);
EXPECT_TRUE(isSplatValue(SplatC_SVE));
// FIXME: Constant splat analysis does not allow undef elements.
Constant *SplatWithUndefC = ConstantVector::get({ScalarC, UndefScalar});
EXPECT_FALSE(isSplatValue(SplatWithUndefC));