Skip to content

Commit 0fb3ebb

Browse files
committed
[RISCV] Generalize 'tryFoldSelectIntOp` to other operations.
Currently, only `SUB`, `ADD`, `OR` and `XOR` are covered. This patch adds `AND`, `SHL`, `SRA`, `SRL`. Reviewed By: craig.topper Differential Revision: https://reviews.llvm.org/D155344
1 parent e3821e4 commit 0fb3ebb

File tree

4 files changed

+238
-333
lines changed

4 files changed

+238
-333
lines changed

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12406,9 +12406,13 @@ static SDValue tryFoldSelectIntoOp(SDNode *N, SelectionDAG &DAG,
1240612406
SDValue TrueVal, SDValue FalseVal,
1240712407
bool Swapped) {
1240812408
bool Commutative = true;
12409-
switch (TrueVal.getOpcode()) {
12409+
unsigned Opc = TrueVal.getOpcode();
12410+
switch (Opc) {
1241012411
default:
1241112412
return SDValue();
12413+
case ISD::SHL:
12414+
case ISD::SRA:
12415+
case ISD::SRL:
1241212416
case ISD::SUB:
1241312417
Commutative = false;
1241412418
break;
@@ -12431,12 +12435,18 @@ static SDValue tryFoldSelectIntoOp(SDNode *N, SelectionDAG &DAG,
1243112435

1243212436
EVT VT = N->getValueType(0);
1243312437
SDLoc DL(N);
12434-
SDValue Zero = DAG.getConstant(0, DL, VT);
1243512438
SDValue OtherOp = TrueVal.getOperand(1 - OpToFold);
12439+
EVT OtherOpVT = OtherOp->getValueType(0);
12440+
SDValue IdentityOperand =
12441+
DAG.getNeutralElement(Opc, DL, OtherOpVT, N->getFlags());
12442+
if (!Commutative)
12443+
IdentityOperand = DAG.getConstant(0, DL, OtherOpVT);
12444+
assert(IdentityOperand && "No identity operand!");
1243612445

1243712446
if (Swapped)
12438-
std::swap(OtherOp, Zero);
12439-
SDValue NewSel = DAG.getSelect(DL, VT, N->getOperand(0), OtherOp, Zero);
12447+
std::swap(OtherOp, IdentityOperand);
12448+
SDValue NewSel =
12449+
DAG.getSelect(DL, OtherOpVT, N->getOperand(0), OtherOp, IdentityOperand);
1244012450
return DAG.getNode(TrueVal.getOpcode(), DL, VT, FalseVal, NewSel);
1244112451
}
1244212452

0 commit comments

Comments
 (0)