Skip to content

Commit 154c47d

Browse files
committed
[SDAG] add helper for select->logic folds; NFC
This set of transforms should be extended to handle vector types.
1 parent 564f5b0 commit 154c47d

File tree

1 file changed

+35
-21
lines changed

1 file changed

+35
-21
lines changed

llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9301,6 +9301,39 @@ SDValue DAGCombiner::foldSelectOfConstants(SDNode *N) {
93019301
return SDValue();
93029302
}
93039303

9304+
static SDValue foldBoolSelectToLogic(SDNode *N, SelectionDAG &DAG) {
9305+
assert(N->getOpcode() == ISD::SELECT && "Expected a select");
9306+
SDValue Cond = N->getOperand(0);
9307+
SDValue T = N->getOperand(1), F = N->getOperand(2);
9308+
EVT VT = N->getValueType(0);
9309+
if (VT != Cond.getValueType() || VT != MVT::i1)
9310+
return SDValue();
9311+
9312+
// select Cond, Cond, F --> or Cond, F
9313+
// select Cond, 1, F --> or Cond, F
9314+
if (Cond == T || isOneConstant(T))
9315+
return DAG.getNode(ISD::OR, SDLoc(N), VT, Cond, F);
9316+
9317+
// select Cond, T, Cond --> and Cond, T
9318+
// select Cond, T, 0 --> and Cond, T
9319+
if (Cond == F || isNullConstant(F))
9320+
return DAG.getNode(ISD::AND, SDLoc(N), VT, Cond, T);
9321+
9322+
// select Cond, T, 1 --> or (not Cond), T
9323+
if (isOneConstant(F)) {
9324+
SDValue NotCond = DAG.getNOT(SDLoc(N), Cond, VT);
9325+
return DAG.getNode(ISD::OR, SDLoc(N), VT, NotCond, T);
9326+
}
9327+
9328+
// select Cond, 0, F --> and (not Cond), F
9329+
if (isNullConstant(T)) {
9330+
SDValue NotCond = DAG.getNOT(SDLoc(N), Cond, VT);
9331+
return DAG.getNode(ISD::AND, SDLoc(N), VT, NotCond, F);
9332+
}
9333+
9334+
return SDValue();
9335+
}
9336+
93049337
SDValue DAGCombiner::visitSELECT(SDNode *N) {
93059338
SDValue N0 = N->getOperand(0);
93069339
SDValue N1 = N->getOperand(1);
@@ -9313,30 +9346,11 @@ SDValue DAGCombiner::visitSELECT(SDNode *N) {
93139346
if (SDValue V = DAG.simplifySelect(N0, N1, N2))
93149347
return V;
93159348

9316-
// fold (select X, X, Y) -> (or X, Y)
9317-
// fold (select X, 1, Y) -> (or C, Y)
9318-
if (VT == VT0 && VT == MVT::i1 && (N0 == N1 || isOneConstant(N1)))
9319-
return DAG.getNode(ISD::OR, DL, VT, N0, N2);
9320-
93219349
if (SDValue V = foldSelectOfConstants(N))
93229350
return V;
93239351

9324-
// fold (select C, 0, X) -> (and (not C), X)
9325-
if (VT == VT0 && VT == MVT::i1 && isNullConstant(N1)) {
9326-
SDValue NOTNode = DAG.getNOT(SDLoc(N0), N0, VT);
9327-
AddToWorklist(NOTNode.getNode());
9328-
return DAG.getNode(ISD::AND, DL, VT, NOTNode, N2);
9329-
}
9330-
// fold (select C, X, 1) -> (or (not C), X)
9331-
if (VT == VT0 && VT == MVT::i1 && isOneConstant(N2)) {
9332-
SDValue NOTNode = DAG.getNOT(SDLoc(N0), N0, VT);
9333-
AddToWorklist(NOTNode.getNode());
9334-
return DAG.getNode(ISD::OR, DL, VT, NOTNode, N1);
9335-
}
9336-
// fold (select X, Y, X) -> (and X, Y)
9337-
// fold (select X, Y, 0) -> (and X, Y)
9338-
if (VT == VT0 && VT == MVT::i1 && (N0 == N2 || isNullConstant(N2)))
9339-
return DAG.getNode(ISD::AND, DL, VT, N0, N1);
9352+
if (SDValue V = foldBoolSelectToLogic(N, DAG))
9353+
return V;
93409354

93419355
// If we can fold this based on the true/false value, do so.
93429356
if (SimplifySelectOps(N, N1, N2))

0 commit comments

Comments
 (0)