Skip to content

Commit 5fcdf76

Browse files
committed
[RISCV] Optimize (brcond (seteq (and X, (1 << C)-1), 0))
Inspired by gcc's assembly: https://godbolt.org/z/54hbzsGYn, while referring to D130203 Replace AND+IMM{32,64} with a slli. But gcc does not handle 0xffff and 0xffffffff, which also seem to be optimizable. The testcases copies all the bits in D130203 and adds 16, 32, and 64 bits. Differential Revision: https://reviews.llvm.org/D141607
1 parent 0ef58c6 commit 5fcdf76

File tree

4 files changed

+1630
-11
lines changed

4 files changed

+1630
-11
lines changed

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

+9-3
Original file line numberDiff line numberDiff line change
@@ -1534,9 +1534,15 @@ static void translateSetCCForBranch(const SDLoc &DL, SDValue &LHS, SDValue &RHS,
15341534
LHS.getOpcode() == ISD::AND && LHS.hasOneUse() &&
15351535
isa<ConstantSDNode>(LHS.getOperand(1))) {
15361536
uint64_t Mask = LHS.getConstantOperandVal(1);
1537-
if (isPowerOf2_64(Mask) && !isInt<12>(Mask)) {
1538-
CC = CC == ISD::SETEQ ? ISD::SETGE : ISD::SETLT;
1539-
unsigned ShAmt = LHS.getValueSizeInBits() - 1 - Log2_64(Mask);
1537+
if ((isPowerOf2_64(Mask) || isMask_64(Mask)) && !isInt<12>(Mask)) {
1538+
unsigned ShAmt = 0;
1539+
if (isPowerOf2_64(Mask)) {
1540+
CC = CC == ISD::SETEQ ? ISD::SETGE : ISD::SETLT;
1541+
ShAmt = LHS.getValueSizeInBits() - 1 - Log2_64(Mask);
1542+
} else {
1543+
ShAmt = LHS.getValueSizeInBits() - (64 - countLeadingZeros(Mask));
1544+
}
1545+
15401546
LHS = LHS.getOperand(0);
15411547
if (ShAmt != 0)
15421548
LHS = DAG.getNode(ISD::SHL, DL, LHS.getValueType(), LHS,

0 commit comments

Comments
 (0)