@@ -9301,6 +9301,39 @@ SDValue DAGCombiner::foldSelectOfConstants(SDNode *N) {
9301
9301
return SDValue();
9302
9302
}
9303
9303
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
+
9304
9337
SDValue DAGCombiner::visitSELECT(SDNode *N) {
9305
9338
SDValue N0 = N->getOperand(0);
9306
9339
SDValue N1 = N->getOperand(1);
@@ -9313,30 +9346,11 @@ SDValue DAGCombiner::visitSELECT(SDNode *N) {
9313
9346
if (SDValue V = DAG.simplifySelect(N0, N1, N2))
9314
9347
return V;
9315
9348
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
-
9321
9349
if (SDValue V = foldSelectOfConstants(N))
9322
9350
return V;
9323
9351
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;
9340
9354
9341
9355
// If we can fold this based on the true/false value, do so.
9342
9356
if (SimplifySelectOps(N, N1, N2))
0 commit comments