Skip to content

Commit 7e1f502

Browse files
committed
[DAG] move fold (select C, 0, 1 -> xor C, 1) to a helper function; NFC
We're missing at least 3 other similar folds based on what we have in InstCombine. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@283596 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent b44dba3 commit 7e1f502

File tree

1 file changed

+31
-16
lines changed

1 file changed

+31
-16
lines changed

lib/CodeGen/SelectionDAG/DAGCombiner.cpp

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ namespace {
334334

335335
SDValue visitShiftByConstant(SDNode *N, ConstantSDNode *Amt);
336336

337+
SDValue foldSelectOfConstants(SDNode *N);
337338
bool SimplifySelectOps(SDNode *SELECT, SDValue LHS, SDValue RHS);
338339
SDValue SimplifyBinOpWithSameOpcodeHands(SDNode *N);
339340
SDValue SimplifySelect(const SDLoc &DL, SDValue N0, SDValue N1, SDValue N2);
@@ -5105,24 +5106,14 @@ static SDValue combineMinNumMaxNum(const SDLoc &DL, EVT VT, SDValue LHS,
51055106
}
51065107
}
51075108

5108-
SDValue DAGCombiner::visitSELECT(SDNode *N) {
5109+
// TODO: We should handle other cases of selecting between {-1,0,1} here.
5110+
SDValue DAGCombiner::foldSelectOfConstants(SDNode *N) {
51095111
SDValue N0 = N->getOperand(0);
51105112
SDValue N1 = N->getOperand(1);
51115113
SDValue N2 = N->getOperand(2);
51125114
EVT VT = N->getValueType(0);
51135115
EVT VT0 = N0.getValueType();
51145116

5115-
// fold (select C, X, X) -> X
5116-
if (N1 == N2)
5117-
return N1;
5118-
if (const ConstantSDNode *N0C = dyn_cast<const ConstantSDNode>(N0)) {
5119-
// fold (select true, X, Y) -> X
5120-
// fold (select false, X, Y) -> Y
5121-
return !N0C->isNullValue() ? N1 : N2;
5122-
}
5123-
// fold (select C, 1, X) -> (or C, X)
5124-
if (VT == MVT::i1 && isOneConstant(N1))
5125-
return DAG.getNode(ISD::OR, SDLoc(N), VT, N0, N2);
51265117
// fold (select C, 0, 1) -> (xor C, 1)
51275118
// We can't do this reliably if integer based booleans have different contents
51285119
// to floating point based booleans. This is because we can't tell whether we
@@ -5142,17 +5133,41 @@ SDValue DAGCombiner::visitSELECT(SDNode *N) {
51425133
SDValue XORNode;
51435134
if (VT == VT0) {
51445135
SDLoc DL(N);
5145-
return DAG.getNode(ISD::XOR, DL, VT0,
5146-
N0, DAG.getConstant(1, DL, VT0));
5136+
return DAG.getNode(ISD::XOR, DL, VT0, N0, DAG.getConstant(1, DL, VT0));
51475137
}
51485138
SDLoc DL0(N0);
5149-
XORNode = DAG.getNode(ISD::XOR, DL0, VT0,
5150-
N0, DAG.getConstant(1, DL0, VT0));
5139+
XORNode = DAG.getNode(ISD::XOR, DL0, VT0, N0, DAG.getConstant(1, DL0, VT0));
51515140
AddToWorklist(XORNode.getNode());
51525141
if (VT.bitsGT(VT0))
51535142
return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, XORNode);
51545143
return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, XORNode);
51555144
}
5145+
5146+
return SDValue();
5147+
}
5148+
5149+
SDValue DAGCombiner::visitSELECT(SDNode *N) {
5150+
SDValue N0 = N->getOperand(0);
5151+
SDValue N1 = N->getOperand(1);
5152+
SDValue N2 = N->getOperand(2);
5153+
EVT VT = N->getValueType(0);
5154+
EVT VT0 = N0.getValueType();
5155+
5156+
// fold (select C, X, X) -> X
5157+
if (N1 == N2)
5158+
return N1;
5159+
if (const ConstantSDNode *N0C = dyn_cast<const ConstantSDNode>(N0)) {
5160+
// fold (select true, X, Y) -> X
5161+
// fold (select false, X, Y) -> Y
5162+
return !N0C->isNullValue() ? N1 : N2;
5163+
}
5164+
// fold (select C, 1, X) -> (or C, X)
5165+
if (VT == MVT::i1 && isOneConstant(N1))
5166+
return DAG.getNode(ISD::OR, SDLoc(N), VT, N0, N2);
5167+
5168+
if (SDValue V = foldSelectOfConstants(N))
5169+
return V;
5170+
51565171
// fold (select C, 0, X) -> (and (not C), X)
51575172
if (VT == VT0 && VT == MVT::i1 && isNullConstant(N1)) {
51585173
SDValue NOTNode = DAG.getNOT(SDLoc(N0), N0, VT);

0 commit comments

Comments
 (0)