Skip to content

Commit

Permalink
[RISCV] Generate bexti for (select(setcc eq (and x, c))) where c is p…
Browse files Browse the repository at this point in the history
…ower of 2. (#73649)

Currently, llvm can transform (setcc ne (and x, c)) to (bexti x,
log2(c)) where c is power of 2.
This patch transform (select (setcc ne (and x, c)), T, F) into (select
(setcc eq (and x, c)), F, T).
It is benefit to the case c is not fit to 12-bits.
  • Loading branch information
yetingk committed Nov 29, 2023
1 parent 4b8964d commit f73844d
Show file tree
Hide file tree
Showing 2 changed files with 317 additions and 190 deletions.
34 changes: 34 additions & 0 deletions llvm/lib/Target/RISCV/RISCVISelLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14236,11 +14236,45 @@ static SDValue foldSelectOfCTTZOrCTLZ(SDNode *N, SelectionDAG &DAG) {
return DAG.getZExtOrTrunc(AndNode, SDLoc(N), N->getValueType(0));
}

static SDValue useInversedSetcc(SDNode *N, SelectionDAG &DAG,
const RISCVSubtarget &Subtarget) {
SDValue Cond = N->getOperand(0);
SDValue True = N->getOperand(1);
SDValue False = N->getOperand(2);
SDLoc DL(N);
EVT VT = N->getValueType(0);
EVT CondVT = Cond.getValueType();

if (Cond.getOpcode() != ISD::SETCC || !Cond.hasOneUse())
return SDValue();

// Replace (setcc eq (and x, C)) with (setcc ne (and x, C))) to generate
// BEXTI, where C is power of 2.
if (Subtarget.hasStdExtZbs() && VT.isScalarInteger() &&
(Subtarget.hasStdExtZicond() || Subtarget.hasVendorXVentanaCondOps())) {
SDValue LHS = Cond.getOperand(0);
SDValue RHS = Cond.getOperand(1);
ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
if (CC == ISD::SETEQ && LHS.getOpcode() == ISD::AND &&
isa<ConstantSDNode>(LHS.getOperand(1)) && isNullConstant(RHS)) {
uint64_t MaskVal = LHS.getConstantOperandVal(1);
if (isPowerOf2_64(MaskVal) && !isInt<12>(MaskVal))
return DAG.getSelect(DL, VT,
DAG.getSetCC(DL, CondVT, LHS, RHS, ISD::SETNE),
False, True);
}
}
return SDValue();
}

static SDValue performSELECTCombine(SDNode *N, SelectionDAG &DAG,
const RISCVSubtarget &Subtarget) {
if (SDValue Folded = foldSelectOfCTTZOrCTLZ(N, DAG))
return Folded;

if (SDValue V = useInversedSetcc(N, DAG, Subtarget))
return V;

if (Subtarget.hasShortForwardBranchOpt())
return SDValue();

Expand Down

0 comments on commit f73844d

Please sign in to comment.