Skip to content

Commit cb89ffd

Browse files
authored
[RISCV] Fix incorrect folding of select on ctlz/cttz (#155231)
This patch tries to fix [#155014](#155014). The pattern of `ctlz`/`cttz` -> `icmp` -> `select` can occur when accounting for targets which don't support `cttz(0)` or `ctlz(0)`. We can replace this with a mask, but **only on power-of-2 bitwidths**.
1 parent c33ccfa commit cb89ffd

File tree

2 files changed

+766
-1
lines changed

2 files changed

+766
-1
lines changed

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18837,6 +18837,10 @@ static SDValue foldSelectOfCTTZOrCTLZ(SDNode *N, SelectionDAG &DAG) {
1883718837
if (Cond->getOperand(0) != CountZeroesArgument)
1883818838
return SDValue();
1883918839

18840+
unsigned BitWidth = CountZeroes.getValueSizeInBits();
18841+
if (!isPowerOf2_32(BitWidth))
18842+
return SDValue();
18843+
1884018844
if (CountZeroes.getOpcode() == ISD::CTTZ_ZERO_UNDEF) {
1884118845
CountZeroes = DAG.getNode(ISD::CTTZ, SDLoc(CountZeroes),
1884218846
CountZeroes.getValueType(), CountZeroesArgument);
@@ -18845,7 +18849,6 @@ static SDValue foldSelectOfCTTZOrCTLZ(SDNode *N, SelectionDAG &DAG) {
1884518849
CountZeroes.getValueType(), CountZeroesArgument);
1884618850
}
1884718851

18848-
unsigned BitWidth = CountZeroes.getValueSizeInBits();
1884918852
SDValue BitWidthMinusOne =
1885018853
DAG.getConstant(BitWidth - 1, SDLoc(N), CountZeroes.getValueType());
1885118854

0 commit comments

Comments
 (0)