Skip to content

Commit f73844d

Browse files
authored
[RISCV] Generate bexti for (select(setcc eq (and x, c))) where c is power of 2. (llvm#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.
1 parent 4b8964d commit f73844d

File tree

2 files changed

+317
-190
lines changed

2 files changed

+317
-190
lines changed

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14236,11 +14236,45 @@ static SDValue foldSelectOfCTTZOrCTLZ(SDNode *N, SelectionDAG &DAG) {
1423614236
return DAG.getZExtOrTrunc(AndNode, SDLoc(N), N->getValueType(0));
1423714237
}
1423814238

14239+
static SDValue useInversedSetcc(SDNode *N, SelectionDAG &DAG,
14240+
const RISCVSubtarget &Subtarget) {
14241+
SDValue Cond = N->getOperand(0);
14242+
SDValue True = N->getOperand(1);
14243+
SDValue False = N->getOperand(2);
14244+
SDLoc DL(N);
14245+
EVT VT = N->getValueType(0);
14246+
EVT CondVT = Cond.getValueType();
14247+
14248+
if (Cond.getOpcode() != ISD::SETCC || !Cond.hasOneUse())
14249+
return SDValue();
14250+
14251+
// Replace (setcc eq (and x, C)) with (setcc ne (and x, C))) to generate
14252+
// BEXTI, where C is power of 2.
14253+
if (Subtarget.hasStdExtZbs() && VT.isScalarInteger() &&
14254+
(Subtarget.hasStdExtZicond() || Subtarget.hasVendorXVentanaCondOps())) {
14255+
SDValue LHS = Cond.getOperand(0);
14256+
SDValue RHS = Cond.getOperand(1);
14257+
ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
14258+
if (CC == ISD::SETEQ && LHS.getOpcode() == ISD::AND &&
14259+
isa<ConstantSDNode>(LHS.getOperand(1)) && isNullConstant(RHS)) {
14260+
uint64_t MaskVal = LHS.getConstantOperandVal(1);
14261+
if (isPowerOf2_64(MaskVal) && !isInt<12>(MaskVal))
14262+
return DAG.getSelect(DL, VT,
14263+
DAG.getSetCC(DL, CondVT, LHS, RHS, ISD::SETNE),
14264+
False, True);
14265+
}
14266+
}
14267+
return SDValue();
14268+
}
14269+
1423914270
static SDValue performSELECTCombine(SDNode *N, SelectionDAG &DAG,
1424014271
const RISCVSubtarget &Subtarget) {
1424114272
if (SDValue Folded = foldSelectOfCTTZOrCTLZ(N, DAG))
1424214273
return Folded;
1424314274

14275+
if (SDValue V = useInversedSetcc(N, DAG, Subtarget))
14276+
return V;
14277+
1424414278
if (Subtarget.hasShortForwardBranchOpt())
1424514279
return SDValue();
1424614280

0 commit comments

Comments
 (0)