Skip to content

Commit 1fd60c3

Browse files
[AArch64] Refactor creation of a shuffle mask for TBL (NFC)
1 parent 451f3fc commit 1fd60c3

File tree

1 file changed

+50
-38
lines changed

1 file changed

+50
-38
lines changed

llvm/lib/Target/AArch64/AArch64ISelLowering.cpp

Lines changed: 50 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -15854,48 +15854,51 @@ bool AArch64TargetLowering::shouldSinkOperands(
1585415854
return false;
1585515855
}
1585615856

15857-
static bool createTblShuffleForZExt(ZExtInst *ZExt, FixedVectorType *DstTy,
15858-
bool IsLittleEndian) {
15859-
Value *Op = ZExt->getOperand(0);
15860-
auto *SrcTy = cast<FixedVectorType>(Op->getType());
15861-
auto SrcWidth = cast<IntegerType>(SrcTy->getElementType())->getBitWidth();
15862-
auto DstWidth = cast<IntegerType>(DstTy->getElementType())->getBitWidth();
15857+
static bool createTblShuffleMask(unsigned SrcWidth, unsigned DstWidth,
15858+
unsigned NumElts, bool IsLittleEndian,
15859+
SmallVectorImpl<int> &Mask) {
1586315860
if (DstWidth % 8 != 0 || DstWidth <= 16 || DstWidth >= 64)
1586415861
return false;
1586515862

15866-
assert(DstWidth % SrcWidth == 0 &&
15867-
"TBL lowering is not supported for a ZExt instruction with this "
15868-
"source & destination element type.");
15869-
unsigned ZExtFactor = DstWidth / SrcWidth;
15863+
if (DstWidth % SrcWidth != 0)
15864+
return false;
15865+
15866+
unsigned Factor = DstWidth / SrcWidth;
15867+
unsigned MaskLen = NumElts * Factor;
15868+
15869+
Mask.clear();
15870+
Mask.resize(MaskLen, NumElts);
15871+
15872+
unsigned SrcIndex = 0;
15873+
for (unsigned I = 0; I < MaskLen; I += Factor)
15874+
Mask[I] = SrcIndex++;
15875+
15876+
if (!IsLittleEndian)
15877+
std::rotate(Mask.rbegin(), Mask.rbegin() + Factor - 1, Mask.rend());
15878+
15879+
return true;
15880+
}
15881+
15882+
static Value *createTblShuffleForZExt(IRBuilderBase &Builder, Value *Op,
15883+
FixedVectorType *ZExtTy,
15884+
FixedVectorType *DstTy,
15885+
bool IsLittleEndian) {
15886+
auto *SrcTy = cast<FixedVectorType>(Op->getType());
1587015887
unsigned NumElts = SrcTy->getNumElements();
15871-
IRBuilder<> Builder(ZExt);
15888+
auto SrcWidth = cast<IntegerType>(SrcTy->getElementType())->getBitWidth();
15889+
auto DstWidth = cast<IntegerType>(DstTy->getElementType())->getBitWidth();
15890+
1587215891
SmallVector<int> Mask;
15873-
// Create a mask that selects <0,...,Op[i]> for each lane of the destination
15874-
// vector to replace the original ZExt. This can later be lowered to a set of
15875-
// tbl instructions.
15876-
for (unsigned i = 0; i < NumElts * ZExtFactor; i++) {
15877-
if (IsLittleEndian) {
15878-
if (i % ZExtFactor == 0)
15879-
Mask.push_back(i / ZExtFactor);
15880-
else
15881-
Mask.push_back(NumElts);
15882-
} else {
15883-
if ((i + 1) % ZExtFactor == 0)
15884-
Mask.push_back((i - ZExtFactor + 1) / ZExtFactor);
15885-
else
15886-
Mask.push_back(NumElts);
15887-
}
15888-
}
15892+
if (!createTblShuffleMask(SrcWidth, DstWidth, NumElts, IsLittleEndian, Mask))
15893+
return nullptr;
1588915894

1589015895
auto *FirstEltZero = Builder.CreateInsertElement(
1589115896
PoisonValue::get(SrcTy), Builder.getInt8(0), uint64_t(0));
1589215897
Value *Result = Builder.CreateShuffleVector(Op, FirstEltZero, Mask);
1589315898
Result = Builder.CreateBitCast(Result, DstTy);
15894-
if (DstTy != ZExt->getType())
15895-
Result = Builder.CreateZExt(Result, ZExt->getType());
15896-
ZExt->replaceAllUsesWith(Result);
15897-
ZExt->eraseFromParent();
15898-
return true;
15899+
if (DstTy != ZExtTy)
15900+
Result = Builder.CreateZExt(Result, ZExtTy);
15901+
return Result;
1589915902
}
1590015903

1590115904
static void createTblForTrunc(TruncInst *TI, bool IsLittleEndian) {
@@ -16060,21 +16063,30 @@ bool AArch64TargetLowering::optimizeExtendOrTruncateConversion(
1606016063

1606116064
DstTy = TruncDstType;
1606216065
}
16063-
16064-
return createTblShuffleForZExt(ZExt, DstTy, Subtarget->isLittleEndian());
16066+
IRBuilder<> Builder(ZExt);
16067+
Value *Result = createTblShuffleForZExt(
16068+
Builder, ZExt->getOperand(0), cast<FixedVectorType>(ZExt->getType()),
16069+
DstTy, Subtarget->isLittleEndian());
16070+
if (!Result)
16071+
return false;
16072+
ZExt->replaceAllUsesWith(Result);
16073+
ZExt->eraseFromParent();
16074+
return true;
1606516075
}
1606616076

1606716077
auto *UIToFP = dyn_cast<UIToFPInst>(I);
1606816078
if (UIToFP && SrcTy->getElementType()->isIntegerTy(8) &&
1606916079
DstTy->getElementType()->isFloatTy()) {
1607016080
IRBuilder<> Builder(I);
16071-
auto *ZExt = cast<ZExtInst>(
16072-
Builder.CreateZExt(I->getOperand(0), VectorType::getInteger(DstTy)));
16081+
Value *ZExt = createTblShuffleForZExt(
16082+
Builder, I->getOperand(0), FixedVectorType::getInteger(DstTy),
16083+
FixedVectorType::getInteger(DstTy), Subtarget->isLittleEndian());
16084+
if (!ZExt)
16085+
return false;
1607316086
auto *UI = Builder.CreateUIToFP(ZExt, DstTy);
1607416087
I->replaceAllUsesWith(UI);
1607516088
I->eraseFromParent();
16076-
return createTblShuffleForZExt(ZExt, cast<FixedVectorType>(ZExt->getType()),
16077-
Subtarget->isLittleEndian());
16089+
return true;
1607816090
}
1607916091

1608016092
// Convert 'fptoui <(8|16) x float> to <(8|16) x i8>' to a wide fptoui

0 commit comments

Comments
 (0)