Skip to content

Commit 035e501

Browse files
committed
[WebAssembly] Recognise EXTEND_HIGH
When lowering EXTEND_VECTOR_INREG, check whether the operand is a shuffle that is moving the top half of a vector into the lower half. If so, we can EXTEND_HIGH the input to the shuffle instead.
1 parent ee42822 commit 035e501

File tree

2 files changed

+474
-0
lines changed

2 files changed

+474
-0
lines changed

llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2143,6 +2143,32 @@ WebAssemblyTargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op,
21432143
Op.getOperand(1));
21442144
}
21452145

2146+
static SDValue GetExtendHigh(SDValue Op, unsigned UserOpc, EVT VT,
2147+
SelectionDAG &DAG) {
2148+
if (Op.getOpcode() != ISD::VECTOR_SHUFFLE)
2149+
return SDValue();
2150+
2151+
assert((UserOpc == WebAssemblyISD::EXTEND_LOW_U ||
2152+
UserOpc == WebAssemblyISD::EXTEND_LOW_S) &&
2153+
"expected extend_low");
2154+
auto *Shuffle = cast<ShuffleVectorSDNode>(Op.getNode());
2155+
ArrayRef<int> Mask = Shuffle->getMask();
2156+
2157+
// Look for a shuffle which moves from the high half to the low half.
2158+
size_t FirstIdx = Mask.size() / 2;
2159+
for (size_t i = 0; i < Mask.size() / 2; ++i) {
2160+
if (Mask[i] != static_cast<int>(FirstIdx + i)) {
2161+
return SDValue();
2162+
}
2163+
}
2164+
2165+
SDLoc DL(Op);
2166+
unsigned Opc = UserOpc == WebAssemblyISD::EXTEND_LOW_S
2167+
? WebAssemblyISD::EXTEND_HIGH_S
2168+
: WebAssemblyISD::EXTEND_HIGH_U;
2169+
return DAG.getNode(Opc, DL, VT, Shuffle->getOperand(0));
2170+
}
2171+
21462172
SDValue
21472173
WebAssemblyTargetLowering::LowerEXTEND_VECTOR_INREG(SDValue Op,
21482174
SelectionDAG &DAG) const {
@@ -2172,6 +2198,12 @@ WebAssemblyTargetLowering::LowerEXTEND_VECTOR_INREG(SDValue Op,
21722198
break;
21732199
}
21742200

2201+
if (Scale == 2) {
2202+
// See if we can use EXTEND_HIGH.
2203+
if (auto ExtendHigh = GetExtendHigh(Op.getOperand(0), Ext, VT, DAG))
2204+
return ExtendHigh;
2205+
}
2206+
21752207
SDValue Ret = Src;
21762208
while (Scale != 1) {
21772209
Ret = DAG.getNode(Ext, DL,

0 commit comments

Comments
 (0)