[clang] b639091 - Change users of CreateShuffleVector to pass the masks as int instead of Constants
Benjamin Kramer via cfe-commits
cfe-commits at lists.llvm.org
Fri Apr 17 07:34:37 PDT 2020
Author: Benjamin Kramer
Date: 2020-04-17T16:34:29+02:00
New Revision: b639091c02df31954899c8107709597947e33364
URL: https://github.com/llvm/llvm-project/commit/b639091c02df31954899c8107709597947e33364
DIFF: https://github.com/llvm/llvm-project/commit/b639091c02df31954899c8107709597947e33364.diff
LOG: Change users of CreateShuffleVector to pass the masks as int instead of Constants
No functionality change intended.
Added:
Modified:
clang/lib/CodeGen/CGExprScalar.cpp
llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp
llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
Removed:
################################################################################
diff --git a/clang/lib/CodeGen/CGExprScalar.cpp b/clang/lib/CodeGen/CGExprScalar.cpp
index 343c62273ec4..62a0f6c0efe6 100644
--- a/clang/lib/CodeGen/CGExprScalar.cpp
+++ b/clang/lib/CodeGen/CGExprScalar.cpp
@@ -1780,22 +1780,18 @@ Value *ScalarExprEmitter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
return Builder.CreateExtractElement(Base, Idx, "vecext");
}
-static llvm::Constant *getMaskElt(llvm::ShuffleVectorInst *SVI, unsigned Idx,
- unsigned Off, llvm::Type *I32Ty) {
+static int getMaskElt(llvm::ShuffleVectorInst *SVI, unsigned Idx,
+ unsigned Off) {
int MV = SVI->getMaskValue(Idx);
if (MV == -1)
- return llvm::UndefValue::get(I32Ty);
- return llvm::ConstantInt::get(I32Ty, Off+MV);
+ return -1;
+ return Off + MV;
}
-static llvm::Constant *getAsInt32(llvm::ConstantInt *C, llvm::Type *I32Ty) {
- if (C->getBitWidth() != 32) {
- assert(llvm::ConstantInt::isValueValidForType(I32Ty,
- C->getZExtValue()) &&
- "Index operand too large for shufflevector mask!");
- return llvm::ConstantInt::get(I32Ty, C->getZExtValue());
- }
- return C;
+static int getAsInt32(llvm::ConstantInt *C, llvm::Type *I32Ty) {
+ assert(llvm::ConstantInt::isValueValidForType(I32Ty, C->getZExtValue()) &&
+ "Index operand too large for shufflevector mask!");
+ return C->getZExtValue();
}
Value *ScalarExprEmitter::VisitInitListExpr(InitListExpr *E) {
@@ -1832,7 +1828,7 @@ Value *ScalarExprEmitter::VisitInitListExpr(InitListExpr *E) {
for (unsigned i = 0; i != NumInitElements; ++i) {
Expr *IE = E->getInit(i);
Value *Init = Visit(IE);
- SmallVector<llvm::Constant*, 16> Args;
+ SmallVector<int, 16> Args;
llvm::VectorType *VVT = dyn_cast<llvm::VectorType>(Init->getType());
@@ -1850,7 +1846,7 @@ Value *ScalarExprEmitter::VisitInitListExpr(InitListExpr *E) {
// insert into undef -> shuffle (src, undef)
// shufflemask must use an i32
Args.push_back(getAsInt32(C, CGF.Int32Ty));
- Args.resize(ResElts, llvm::UndefValue::get(CGF.Int32Ty));
+ Args.resize(ResElts, -1);
LHS = EI->getVectorOperand();
RHS = V;
@@ -1859,17 +1855,16 @@ Value *ScalarExprEmitter::VisitInitListExpr(InitListExpr *E) {
// insert into undefshuffle && size match -> shuffle (v, src)
llvm::ShuffleVectorInst *SVV = cast<llvm::ShuffleVectorInst>(V);
for (unsigned j = 0; j != CurIdx; ++j)
- Args.push_back(getMaskElt(SVV, j, 0, CGF.Int32Ty));
- Args.push_back(Builder.getInt32(ResElts + C->getZExtValue()));
- Args.resize(ResElts, llvm::UndefValue::get(CGF.Int32Ty));
+ Args.push_back(getMaskElt(SVV, j, 0));
+ Args.push_back(ResElts + C->getZExtValue());
+ Args.resize(ResElts, -1);
LHS = cast<llvm::ShuffleVectorInst>(V)->getOperand(0);
RHS = EI->getVectorOperand();
VIsUndefShuffle = false;
}
if (!Args.empty()) {
- llvm::Constant *Mask = llvm::ConstantVector::get(Args);
- V = Builder.CreateShuffleVector(LHS, RHS, Mask);
+ V = Builder.CreateShuffleVector(LHS, RHS, Args);
++CurIdx;
continue;
}
@@ -1898,15 +1893,14 @@ Value *ScalarExprEmitter::VisitInitListExpr(InitListExpr *E) {
// If the current vector initializer is a shuffle with undef, merge
// this shuffle directly into it.
if (VIsUndefShuffle) {
- Args.push_back(getMaskElt(cast<llvm::ShuffleVectorInst>(V), j, 0,
- CGF.Int32Ty));
+ Args.push_back(getMaskElt(cast<llvm::ShuffleVectorInst>(V), j, 0));
} else {
- Args.push_back(Builder.getInt32(j));
+ Args.push_back(j);
}
}
for (unsigned j = 0, je = InitElts; j != je; ++j)
- Args.push_back(getMaskElt(SVI, j, Offset, CGF.Int32Ty));
- Args.resize(ResElts, llvm::UndefValue::get(CGF.Int32Ty));
+ Args.push_back(getMaskElt(SVI, j, Offset));
+ Args.resize(ResElts, -1);
if (VIsUndefShuffle)
V = cast<llvm::ShuffleVectorInst>(V)->getOperand(0);
@@ -1919,26 +1913,24 @@ Value *ScalarExprEmitter::VisitInitListExpr(InitListExpr *E) {
// to the vector initializer into V.
if (Args.empty()) {
for (unsigned j = 0; j != InitElts; ++j)
- Args.push_back(Builder.getInt32(j));
- Args.resize(ResElts, llvm::UndefValue::get(CGF.Int32Ty));
- llvm::Constant *Mask = llvm::ConstantVector::get(Args);
- Init = Builder.CreateShuffleVector(Init, llvm::UndefValue::get(VVT),
- Mask, "vext");
+ Args.push_back(j);
+ Args.resize(ResElts, -1);
+ Init = Builder.CreateShuffleVector(Init, llvm::UndefValue::get(VVT), Args,
+ "vext");
Args.clear();
for (unsigned j = 0; j != CurIdx; ++j)
- Args.push_back(Builder.getInt32(j));
+ Args.push_back(j);
for (unsigned j = 0; j != InitElts; ++j)
- Args.push_back(Builder.getInt32(j+Offset));
- Args.resize(ResElts, llvm::UndefValue::get(CGF.Int32Ty));
+ Args.push_back(j + Offset);
+ Args.resize(ResElts, -1);
}
// If V is undef, make sure it ends up on the RHS of the shuffle to aid
// merging subsequent shuffles into this one.
if (CurIdx == 0)
std::swap(V, Init);
- llvm::Constant *Mask = llvm::ConstantVector::get(Args);
- V = Builder.CreateShuffleVector(V, Init, Mask, "vecinit");
+ V = Builder.CreateShuffleVector(V, Init, Args, "vecinit");
VIsUndefShuffle = isa<llvm::UndefValue>(Init);
CurIdx += InitElts;
}
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
index 26fe9e659c7e..03c6cf7c4705 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
@@ -840,22 +840,19 @@ static Value *simplifyX86extrq(IntrinsicInst &II, Value *Op0,
Index /= 8;
Type *IntTy8 = Type::getInt8Ty(II.getContext());
- Type *IntTy32 = Type::getInt32Ty(II.getContext());
VectorType *ShufTy = VectorType::get(IntTy8, 16);
- SmallVector<Constant *, 16> ShuffleMask;
+ SmallVector<int, 16> ShuffleMask;
for (int i = 0; i != (int)Length; ++i)
- ShuffleMask.push_back(
- Constant::getIntegerValue(IntTy32, APInt(32, i + Index)));
+ ShuffleMask.push_back(i + Index);
for (int i = Length; i != 8; ++i)
- ShuffleMask.push_back(
- Constant::getIntegerValue(IntTy32, APInt(32, i + 16)));
+ ShuffleMask.push_back(i + 16);
for (int i = 8; i != 16; ++i)
- ShuffleMask.push_back(UndefValue::get(IntTy32));
+ ShuffleMask.push_back(-1);
Value *SV = Builder.CreateShuffleVector(
Builder.CreateBitCast(Op0, ShufTy),
- ConstantAggregateZero::get(ShufTy), ConstantVector::get(ShuffleMask));
+ ConstantAggregateZero::get(ShufTy), ShuffleMask);
return Builder.CreateBitCast(SV, II.getType());
}
@@ -920,23 +917,21 @@ static Value *simplifyX86insertq(IntrinsicInst &II, Value *Op0, Value *Op1,
Index /= 8;
Type *IntTy8 = Type::getInt8Ty(II.getContext());
- Type *IntTy32 = Type::getInt32Ty(II.getContext());
VectorType *ShufTy = VectorType::get(IntTy8, 16);
- SmallVector<Constant *, 16> ShuffleMask;
+ SmallVector<int, 16> ShuffleMask;
for (int i = 0; i != (int)Index; ++i)
- ShuffleMask.push_back(Constant::getIntegerValue(IntTy32, APInt(32, i)));
+ ShuffleMask.push_back(i);
for (int i = 0; i != (int)Length; ++i)
- ShuffleMask.push_back(
- Constant::getIntegerValue(IntTy32, APInt(32, i + 16)));
+ ShuffleMask.push_back(i + 16);
for (int i = Index + Length; i != 8; ++i)
- ShuffleMask.push_back(Constant::getIntegerValue(IntTy32, APInt(32, i)));
+ ShuffleMask.push_back(i);
for (int i = 8; i != 16; ++i)
- ShuffleMask.push_back(UndefValue::get(IntTy32));
+ ShuffleMask.push_back(-1);
Value *SV = Builder.CreateShuffleVector(Builder.CreateBitCast(Op0, ShufTy),
Builder.CreateBitCast(Op1, ShufTy),
- ConstantVector::get(ShuffleMask));
+ ShuffleMask);
return Builder.CreateBitCast(SV, II.getType());
}
@@ -988,13 +983,12 @@ static Value *simplifyX86pshufb(const IntrinsicInst &II,
return nullptr;
auto *VecTy = cast<VectorType>(II.getType());
- auto *MaskEltTy = Type::getInt32Ty(II.getContext());
unsigned NumElts = VecTy->getNumElements();
assert((NumElts == 16 || NumElts == 32 || NumElts == 64) &&
"Unexpected number of elements in shuffle mask!");
// Construct a shuffle mask from constant integers or UNDEFs.
- Constant *Indexes[64] = {nullptr};
+ int Indexes[64];
// Each byte in the shuffle control mask forms an index to permute the
// corresponding byte in the destination operand.
@@ -1004,7 +998,7 @@ static Value *simplifyX86pshufb(const IntrinsicInst &II,
return nullptr;
if (isa<UndefValue>(COp)) {
- Indexes[I] = UndefValue::get(MaskEltTy);
+ Indexes[I] = -1;
continue;
}
@@ -1018,13 +1012,12 @@ static Value *simplifyX86pshufb(const IntrinsicInst &II,
// The value of each index for the high 128-bit lane is the least
// significant 4 bits of the respective shuffle control byte.
Index = ((Index < 0) ? NumElts : Index & 0x0F) + (I & 0xF0);
- Indexes[I] = ConstantInt::get(MaskEltTy, Index);
+ Indexes[I] = Index;
}
- auto ShuffleMask = ConstantVector::get(makeArrayRef(Indexes, NumElts));
auto V1 = II.getArgOperand(0);
auto V2 = Constant::getNullValue(VecTy);
- return Builder.CreateShuffleVector(V1, V2, ShuffleMask);
+ return Builder.CreateShuffleVector(V1, V2, makeArrayRef(Indexes, NumElts));
}
/// Attempt to convert vpermilvar* to shufflevector if the mask is constant.
@@ -1035,14 +1028,13 @@ static Value *simplifyX86vpermilvar(const IntrinsicInst &II,
return nullptr;
auto *VecTy = cast<VectorType>(II.getType());
- auto *MaskEltTy = Type::getInt32Ty(II.getContext());
unsigned NumElts = VecTy->getNumElements();
bool IsPD = VecTy->getScalarType()->isDoubleTy();
unsigned NumLaneElts = IsPD ? 2 : 4;
assert(NumElts == 16 || NumElts == 8 || NumElts == 4 || NumElts == 2);
// Construct a shuffle mask from constant integers or UNDEFs.
- Constant *Indexes[16] = {nullptr};
+ int Indexes[16];
// The intrinsics only read one or two bits, clear the rest.
for (unsigned I = 0; I < NumElts; ++I) {
@@ -1051,7 +1043,7 @@ static Value *simplifyX86vpermilvar(const IntrinsicInst &II,
return nullptr;
if (isa<UndefValue>(COp)) {
- Indexes[I] = UndefValue::get(MaskEltTy);
+ Indexes[I] = -1;
continue;
}
@@ -1068,13 +1060,12 @@ static Value *simplifyX86vpermilvar(const IntrinsicInst &II,
// shuffle, we have to make that explicit.
Index += APInt(32, (I / NumLaneElts) * NumLaneElts);
- Indexes[I] = ConstantInt::get(MaskEltTy, Index);
+ Indexes[I] = Index.getZExtValue();
}
- auto ShuffleMask = ConstantVector::get(makeArrayRef(Indexes, NumElts));
auto V1 = II.getArgOperand(0);
auto V2 = UndefValue::get(V1->getType());
- return Builder.CreateShuffleVector(V1, V2, ShuffleMask);
+ return Builder.CreateShuffleVector(V1, V2, makeArrayRef(Indexes, NumElts));
}
/// Attempt to convert vpermd/vpermps to shufflevector if the mask is constant.
@@ -1085,13 +1076,12 @@ static Value *simplifyX86vpermv(const IntrinsicInst &II,
return nullptr;
auto *VecTy = cast<VectorType>(II.getType());
- auto *MaskEltTy = Type::getInt32Ty(II.getContext());
unsigned Size = VecTy->getNumElements();
assert((Size == 4 || Size == 8 || Size == 16 || Size == 32 || Size == 64) &&
"Unexpected shuffle mask size");
// Construct a shuffle mask from constant integers or UNDEFs.
- Constant *Indexes[64] = {nullptr};
+ int Indexes[64];
for (unsigned I = 0; I < Size; ++I) {
Constant *COp = V->getAggregateElement(I);
@@ -1099,19 +1089,18 @@ static Value *simplifyX86vpermv(const IntrinsicInst &II,
return nullptr;
if (isa<UndefValue>(COp)) {
- Indexes[I] = UndefValue::get(MaskEltTy);
+ Indexes[I] = -1;
continue;
}
uint32_t Index = cast<ConstantInt>(COp)->getZExtValue();
Index &= Size - 1;
- Indexes[I] = ConstantInt::get(MaskEltTy, Index);
+ Indexes[I] = Index;
}
- auto ShuffleMask = ConstantVector::get(makeArrayRef(Indexes, Size));
auto V1 = II.getArgOperand(0);
auto V2 = UndefValue::get(VecTy);
- return Builder.CreateShuffleVector(V1, V2, ShuffleMask);
+ return Builder.CreateShuffleVector(V1, V2, makeArrayRef(Indexes, Size));
}
// TODO, Obvious Missing Transforms:
@@ -1487,7 +1476,7 @@ static Value *simplifyNeonTbl1(const IntrinsicInst &II,
if (!VecTy->getElementType()->isIntegerTy(8) || NumElts != 8)
return nullptr;
- uint32_t Indexes[8];
+ int Indexes[8];
for (unsigned I = 0; I < NumElts; ++I) {
Constant *COp = C->getAggregateElement(I);
@@ -1498,15 +1487,13 @@ static Value *simplifyNeonTbl1(const IntrinsicInst &II,
Indexes[I] = cast<ConstantInt>(COp)->getLimitedValue();
// Make sure the mask indices are in range.
- if (Indexes[I] >= NumElts)
+ if ((unsigned)Indexes[I] >= NumElts)
return nullptr;
}
- auto *ShuffleMask = ConstantDataVector::get(II.getContext(),
- makeArrayRef(Indexes));
auto *V1 = II.getArgOperand(0);
auto *V2 = Constant::getNullValue(V1->getType());
- return Builder.CreateShuffleVector(V1, V2, ShuffleMask);
+ return Builder.CreateShuffleVector(V1, V2, makeArrayRef(Indexes));
}
/// Convert a vector load intrinsic into a simple llvm load instruction.
diff --git a/llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp b/llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp
index 3de7fbf04e58..0eaceb964a9a 100644
--- a/llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp
+++ b/llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp
@@ -3030,14 +3030,12 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
setOriginForNaryOp(I);
}
- Constant *getPclmulMask(IRBuilder<> &IRB, unsigned Width, bool OddElements) {
- SmallVector<Constant *, 8> Mask;
+ SmallVector<int, 8> getPclmulMask(unsigned Width, bool OddElements) {
+ SmallVector<int, 8> Mask;
for (unsigned X = OddElements ? 1 : 0; X < Width; X += 2) {
- Constant *C = ConstantInt::get(IRB.getInt32Ty(), X);
- Mask.push_back(C);
- Mask.push_back(C);
+ Mask.append(2, X);
}
- return ConstantVector::get(Mask);
+ return Mask;
}
// Instrument pclmul intrinsics.
@@ -3058,10 +3056,10 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
unsigned Imm = cast<ConstantInt>(I.getArgOperand(2))->getZExtValue();
Value *Shuf0 =
IRB.CreateShuffleVector(getShadow(&I, 0), UndefValue::get(ShadowTy),
- getPclmulMask(IRB, Width, Imm & 0x01));
+ getPclmulMask(Width, Imm & 0x01));
Value *Shuf1 =
IRB.CreateShuffleVector(getShadow(&I, 1), UndefValue::get(ShadowTy),
- getPclmulMask(IRB, Width, Imm & 0x10));
+ getPclmulMask(Width, Imm & 0x10));
ShadowAndOriginCombiner SOC(this, IRB);
SOC.Add(Shuf0, getOrigin(&I, 0));
SOC.Add(Shuf1, getOrigin(&I, 1));
diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
index 794ca1001c6a..f9e0e7dc1601 100644
--- a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
+++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
@@ -89,6 +89,7 @@
#include "llvm/Transforms/Utils/LoopUtils.h"
#include "llvm/Transforms/Vectorize.h"
#include <algorithm>
+#include <bits/stdint-intn.h>
#include <cassert>
#include <cstdint>
#include <iterator>
@@ -4573,24 +4574,23 @@ Value *BoUpSLP::vectorizeTree(TreeEntry *E) {
// each vector operation.
ValueList OpScalars, AltScalars;
unsigned e = E->Scalars.size();
- SmallVector<Constant *, 8> Mask(e);
+ SmallVector<int, 8> Mask(e);
for (unsigned i = 0; i < e; ++i) {
auto *OpInst = cast<Instruction>(E->Scalars[i]);
assert(E->isOpcodeOrAlt(OpInst) && "Unexpected main/alternate opcode");
if (OpInst->getOpcode() == E->getAltOpcode()) {
- Mask[i] = Builder.getInt32(e + i);
+ Mask[i] = e + i;
AltScalars.push_back(E->Scalars[i]);
} else {
- Mask[i] = Builder.getInt32(i);
+ Mask[i] = i;
OpScalars.push_back(E->Scalars[i]);
}
}
- Value *ShuffleMask = ConstantVector::get(Mask);
propagateIRFlags(V0, OpScalars);
propagateIRFlags(V1, AltScalars);
- Value *V = Builder.CreateShuffleVector(V0, V1, ShuffleMask);
+ Value *V = Builder.CreateShuffleVector(V0, V1, Mask);
if (Instruction *I = dyn_cast<Instruction>(V))
V = propagateMetadata(I, E->Scalars);
if (NeedToShuffleReuses) {
@@ -6083,24 +6083,23 @@ bool SLPVectorizerPass::tryToVectorize(Instruction *I, BoUpSLP &R) {
/// <0,2,...> or <1,3,..> while a splitting reduction will generate
/// <2,3, undef,undef> for a vector of 4 and NumElts = 2.
/// \param IsLeft True will generate a mask of even elements, odd otherwise.
-static Value *createRdxShuffleMask(unsigned VecLen, unsigned NumEltsToRdx,
- bool IsPairwise, bool IsLeft,
- IRBuilder<> &Builder) {
+static SmallVector<int, 32> createRdxShuffleMask(unsigned VecLen,
+ unsigned NumEltsToRdx,
+ bool IsPairwise, bool IsLeft) {
assert((IsPairwise || !IsLeft) && "Don't support a <0,1,undef,...> mask");
- SmallVector<Constant *, 32> ShuffleMask(
- VecLen, UndefValue::get(Builder.getInt32Ty()));
+ SmallVector<int, 32> ShuffleMask(VecLen, -1);
if (IsPairwise)
// Build a mask of 0, 2, ... (left) or 1, 3, ... (right).
for (unsigned i = 0; i != NumEltsToRdx; ++i)
- ShuffleMask[i] = Builder.getInt32(2 * i + !IsLeft);
+ ShuffleMask[i] = 2 * i + !IsLeft;
else
// Move the upper half of the vector to the lower half.
for (unsigned i = 0; i != NumEltsToRdx; ++i)
- ShuffleMask[i] = Builder.getInt32(NumEltsToRdx + i);
+ ShuffleMask[i] = NumEltsToRdx + i;
- return ConstantVector::get(ShuffleMask);
+ return ShuffleMask;
}
namespace {
@@ -6974,10 +6973,8 @@ class HorizontalReduction {
Value *TmpVec = VectorizedValue;
for (unsigned i = ReduxWidth / 2; i != 0; i >>= 1) {
- Value *LeftMask =
- createRdxShuffleMask(ReduxWidth, i, true, true, Builder);
- Value *RightMask =
- createRdxShuffleMask(ReduxWidth, i, true, false, Builder);
+ auto LeftMask = createRdxShuffleMask(ReduxWidth, i, true, true);
+ auto RightMask = createRdxShuffleMask(ReduxWidth, i, true, false);
Value *LeftShuf = Builder.CreateShuffleVector(
TmpVec, UndefValue::get(TmpVec->getType()), LeftMask, "rdx.shuf.l");
More information about the cfe-commits
mailing list