Skip to content

Commit bd5152a

Browse files
committed
[Instructions] reimplement shufflevector property caching
Simplify implementation
1 parent 4666be3 commit bd5152a

File tree

2 files changed

+48
-20
lines changed

2 files changed

+48
-20
lines changed

llvm/include/llvm/IR/Instructions.h

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1886,6 +1886,23 @@ DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
18861886
// ShuffleVectorInst Class
18871887
//===----------------------------------------------------------------------===//
18881888

1889+
struct ShuffleMaskAttrs {
1890+
bool SingleSource : 1;
1891+
bool Identity : 1;
1892+
// bool IdentityWithPadding : 1;
1893+
// bool IdentityWithExtract : 1;
1894+
// bool Concat : 1;
1895+
bool Reverse : 1;
1896+
bool ZeroEltSplat : 1;
1897+
bool Select : 1;
1898+
bool Transpose : 1;
1899+
bool Splice : 1;
1900+
int SpliceIndex;
1901+
};
1902+
1903+
static_assert(sizeof(ShuffleMaskAttrs) <= sizeof(uint64_t),
1904+
"ShuffleMaskAttrs is too large!");
1905+
18891906
constexpr int PoisonMaskElem = -1;
18901907

18911908
/// This instruction constructs a fixed permutation of two
@@ -1903,6 +1920,27 @@ class ShuffleVectorInst : public Instruction {
19031920

19041921
SmallVector<int, 4> ShuffleMask;
19051922
Constant *ShuffleMaskForBitcode;
1923+
mutable ShuffleMaskAttrs ShuffleAttrs;
1924+
1925+
// This function is const so that it's callable from other const functions.
1926+
void computeShuffleAttrs() const {
1927+
ShuffleAttrs = {}; // reset
1928+
ShuffleAttrs.SingleSource =
1929+
!changesLength() && isSingleSourceMask(ShuffleMask, ShuffleMask.size());
1930+
ShuffleAttrs.Identity =
1931+
!changesLength() && isIdentityMask(ShuffleMask, ShuffleMask.size());
1932+
ShuffleAttrs.Select =
1933+
!changesLength() && isSelectMask(ShuffleMask, ShuffleMask.size());
1934+
ShuffleAttrs.Reverse =
1935+
!changesLength() && isReverseMask(ShuffleMask, ShuffleMask.size());
1936+
ShuffleAttrs.ZeroEltSplat =
1937+
!changesLength() && isZeroEltSplatMask(ShuffleMask, ShuffleMask.size());
1938+
ShuffleAttrs.Transpose =
1939+
!changesLength() && isTransposeMask(ShuffleMask, ShuffleMask.size());
1940+
ShuffleAttrs.Splice =
1941+
!changesLength() &&
1942+
isSpliceMask(ShuffleMask, ShuffleMask.size(), ShuffleAttrs.SpliceIndex);
1943+
}
19061944

19071945
protected:
19081946
// Note: Instruction needs to be a friend here to call cloneImpl.
@@ -2013,10 +2051,7 @@ class ShuffleVectorInst : public Instruction {
20132051
/// vector without changing the length of that vector.
20142052
/// Example: shufflevector <4 x n> A, <4 x n> B, <3,0,undef,3>
20152053
/// TODO: Optionally allow length-changing shuffles.
2016-
bool isSingleSource() const {
2017-
return !changesLength() &&
2018-
isSingleSourceMask(ShuffleMask, ShuffleMask.size());
2019-
}
2054+
bool isSingleSource() const { return ShuffleAttrs.SingleSource; }
20202055

20212056
/// Return true if this shuffle mask chooses elements from exactly one source
20222057
/// vector without lane crossings. A shuffle using this mask is not
@@ -2047,7 +2082,7 @@ class ShuffleVectorInst : public Instruction {
20472082
if (isa<ScalableVectorType>(getType()))
20482083
return false;
20492084

2050-
return !changesLength() && isIdentityMask(ShuffleMask, ShuffleMask.size());
2085+
return ShuffleAttrs.Identity;
20512086
}
20522087

20532088
/// Return true if this shuffle lengthens exactly one source vector with
@@ -2087,9 +2122,7 @@ class ShuffleVectorInst : public Instruction {
20872122
/// This returns false if the mask does not choose from both input vectors.
20882123
/// In that case, the shuffle is better classified as an identity shuffle.
20892124
/// TODO: Optionally allow length-changing shuffles.
2090-
bool isSelect() const {
2091-
return !changesLength() && isSelectMask(ShuffleMask, ShuffleMask.size());
2092-
}
2125+
bool isSelect() const { return ShuffleAttrs.Select; }
20932126

20942127
/// Return true if this shuffle mask swaps the order of elements from exactly
20952128
/// one source vector.
@@ -2108,9 +2141,7 @@ class ShuffleVectorInst : public Instruction {
21082141
/// one source vector.
21092142
/// Example: shufflevector <4 x n> A, <4 x n> B, <3,undef,1,undef>
21102143
/// TODO: Optionally allow length-changing shuffles.
2111-
bool isReverse() const {
2112-
return !changesLength() && isReverseMask(ShuffleMask, ShuffleMask.size());
2113-
}
2144+
bool isReverse() const { return ShuffleAttrs.Reverse; }
21142145

21152146
/// Return true if this shuffle mask chooses all elements with the same value
21162147
/// as the first element of exactly one source vector.
@@ -2131,10 +2162,7 @@ class ShuffleVectorInst : public Instruction {
21312162
/// Example: shufflevector <4 x n> A, <4 x n> B, <undef,0,undef,0>
21322163
/// TODO: Optionally allow length-changing shuffles.
21332164
/// TODO: Optionally allow splats from other elements.
2134-
bool isZeroEltSplat() const {
2135-
return !changesLength() &&
2136-
isZeroEltSplatMask(ShuffleMask, ShuffleMask.size());
2137-
}
2165+
bool isZeroEltSplat() const { return ShuffleAttrs.ZeroEltSplat; }
21382166

21392167
/// Return true if this shuffle mask is a transpose mask.
21402168
/// Transpose vector masks transpose a 2xn matrix. They read corresponding
@@ -2181,9 +2209,7 @@ class ShuffleVectorInst : public Instruction {
21812209
/// merge or interleave. See the description for isTransposeMask() for the
21822210
/// exact specification.
21832211
/// Example: shufflevector <4 x n> A, <4 x n> B, <0,4,2,6>
2184-
bool isTranspose() const {
2185-
return !changesLength() && isTransposeMask(ShuffleMask, ShuffleMask.size());
2186-
}
2212+
bool isTranspose() const { return ShuffleAttrs.Transpose; }
21872213

21882214
/// Return true if this shuffle mask is a splice mask, concatenating the two
21892215
/// inputs together and then extracts an original width vector starting from
@@ -2204,8 +2230,9 @@ class ShuffleVectorInst : public Instruction {
22042230
/// then extracts an original width vector starting from the splice index.
22052231
/// Example: shufflevector <4 x n> A, <4 x n> B, <1,2,3,4>
22062232
bool isSplice(int &Index) const {
2207-
return !changesLength() &&
2208-
isSpliceMask(ShuffleMask, ShuffleMask.size(), Index);
2233+
if (ShuffleAttrs.Splice)
2234+
Index = ShuffleAttrs.SpliceIndex;
2235+
return ShuffleAttrs.Splice;
22092236
}
22102237

22112238
/// Return true if this shuffle mask is an extract subvector mask.

llvm/lib/IR/Instructions.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1822,6 +1822,7 @@ void ShuffleVectorInst::getShuffleMask(const Constant *Mask,
18221822
void ShuffleVectorInst::setShuffleMask(ArrayRef<int> Mask) {
18231823
ShuffleMask.assign(Mask.begin(), Mask.end());
18241824
ShuffleMaskForBitcode = convertShuffleMaskForBitcode(Mask, getType());
1825+
computeShuffleAttrs();
18251826
}
18261827

18271828
Constant *ShuffleVectorInst::convertShuffleMaskForBitcode(ArrayRef<int> Mask,

0 commit comments

Comments
 (0)