Skip to content

Commit c6f06b4

Browse files
RKSimonjustinfargnoli
authored andcommitted
[DAG] Use FoldConstantArithmetic for unary bitops constant folding.
BSWAP/BITREVERSE/CTPOP/CTLZ/CTLZ_ZERO_UNDEF/CTTZ/CTTZ_ZERO_UNDEF are all handled by FoldConstantArithmetic - so use directly instead of testing for isConstantIntBuildVectorOrConstantInt and relying on DAG.getNode() to perform the constant fold.
1 parent 3269aa8 commit c6f06b4

File tree

1 file changed

+26
-20
lines changed

1 file changed

+26
-20
lines changed

llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10999,8 +10999,8 @@ SDValue DAGCombiner::visitBSWAP(SDNode *N) {
1099910999
SDLoc DL(N);
1100011000

1100111001
// fold (bswap c1) -> c2
11002-
if (DAG.isConstantIntBuildVectorOrConstantInt(N0))
11003-
return DAG.getNode(ISD::BSWAP, DL, VT, N0);
11002+
if (SDValue C = DAG.FoldConstantArithmetic(ISD::BSWAP, DL, VT, {N0}))
11003+
return C;
1100411004
// fold (bswap (bswap x)) -> x
1100511005
if (N0.getOpcode() == ISD::BSWAP)
1100611006
return N0.getOperand(0);
@@ -11059,10 +11059,11 @@ SDValue DAGCombiner::visitBSWAP(SDNode *N) {
1105911059
SDValue DAGCombiner::visitBITREVERSE(SDNode *N) {
1106011060
SDValue N0 = N->getOperand(0);
1106111061
EVT VT = N->getValueType(0);
11062+
SDLoc DL(N);
1106211063

1106311064
// fold (bitreverse c1) -> c2
11064-
if (DAG.isConstantIntBuildVectorOrConstantInt(N0))
11065-
return DAG.getNode(ISD::BITREVERSE, SDLoc(N), VT, N0);
11065+
if (SDValue C = DAG.FoldConstantArithmetic(ISD::BITREVERSE, DL, VT, {N0}))
11066+
return C;
1106611067
// fold (bitreverse (bitreverse x)) -> x
1106711068
if (N0.getOpcode() == ISD::BITREVERSE)
1106811069
return N0.getOperand(0);
@@ -11072,64 +11073,69 @@ SDValue DAGCombiner::visitBITREVERSE(SDNode *N) {
1107211073
SDValue DAGCombiner::visitCTLZ(SDNode *N) {
1107311074
SDValue N0 = N->getOperand(0);
1107411075
EVT VT = N->getValueType(0);
11076+
SDLoc DL(N);
1107511077

1107611078
// fold (ctlz c1) -> c2
11077-
if (DAG.isConstantIntBuildVectorOrConstantInt(N0))
11078-
return DAG.getNode(ISD::CTLZ, SDLoc(N), VT, N0);
11079+
if (SDValue C = DAG.FoldConstantArithmetic(ISD::CTLZ, DL, VT, {N0}))
11080+
return C;
1107911081

1108011082
// If the value is known never to be zero, switch to the undef version.
11081-
if (!LegalOperations || TLI.isOperationLegal(ISD::CTLZ_ZERO_UNDEF, VT)) {
11083+
if (!LegalOperations || TLI.isOperationLegal(ISD::CTLZ_ZERO_UNDEF, VT))
1108211084
if (DAG.isKnownNeverZero(N0))
11083-
return DAG.getNode(ISD::CTLZ_ZERO_UNDEF, SDLoc(N), VT, N0);
11084-
}
11085+
return DAG.getNode(ISD::CTLZ_ZERO_UNDEF, DL, VT, N0);
1108511086

1108611087
return SDValue();
1108711088
}
1108811089

1108911090
SDValue DAGCombiner::visitCTLZ_ZERO_UNDEF(SDNode *N) {
1109011091
SDValue N0 = N->getOperand(0);
1109111092
EVT VT = N->getValueType(0);
11093+
SDLoc DL(N);
1109211094

1109311095
// fold (ctlz_zero_undef c1) -> c2
11094-
if (DAG.isConstantIntBuildVectorOrConstantInt(N0))
11095-
return DAG.getNode(ISD::CTLZ_ZERO_UNDEF, SDLoc(N), VT, N0);
11096+
if (SDValue C =
11097+
DAG.FoldConstantArithmetic(ISD::CTLZ_ZERO_UNDEF, DL, VT, {N0}))
11098+
return C;
1109611099
return SDValue();
1109711100
}
1109811101

1109911102
SDValue DAGCombiner::visitCTTZ(SDNode *N) {
1110011103
SDValue N0 = N->getOperand(0);
1110111104
EVT VT = N->getValueType(0);
11105+
SDLoc DL(N);
1110211106

1110311107
// fold (cttz c1) -> c2
11104-
if (DAG.isConstantIntBuildVectorOrConstantInt(N0))
11105-
return DAG.getNode(ISD::CTTZ, SDLoc(N), VT, N0);
11108+
if (SDValue C = DAG.FoldConstantArithmetic(ISD::CTTZ, DL, VT, {N0}))
11109+
return C;
1110611110

1110711111
// If the value is known never to be zero, switch to the undef version.
11108-
if (!LegalOperations || TLI.isOperationLegal(ISD::CTTZ_ZERO_UNDEF, VT)) {
11112+
if (!LegalOperations || TLI.isOperationLegal(ISD::CTTZ_ZERO_UNDEF, VT))
1110911113
if (DAG.isKnownNeverZero(N0))
11110-
return DAG.getNode(ISD::CTTZ_ZERO_UNDEF, SDLoc(N), VT, N0);
11111-
}
11114+
return DAG.getNode(ISD::CTTZ_ZERO_UNDEF, DL, VT, N0);
1111211115

1111311116
return SDValue();
1111411117
}
1111511118

1111611119
SDValue DAGCombiner::visitCTTZ_ZERO_UNDEF(SDNode *N) {
1111711120
SDValue N0 = N->getOperand(0);
1111811121
EVT VT = N->getValueType(0);
11122+
SDLoc DL(N);
1111911123

1112011124
// fold (cttz_zero_undef c1) -> c2
11121-
if (DAG.isConstantIntBuildVectorOrConstantInt(N0))
11122-
return DAG.getNode(ISD::CTTZ_ZERO_UNDEF, SDLoc(N), VT, N0);
11125+
if (SDValue C =
11126+
DAG.FoldConstantArithmetic(ISD::CTTZ_ZERO_UNDEF, DL, VT, {N0}))
11127+
return C;
1112311128
return SDValue();
1112411129
}
1112511130

1112611131
SDValue DAGCombiner::visitCTPOP(SDNode *N) {
1112711132
SDValue N0 = N->getOperand(0);
1112811133
EVT VT = N->getValueType(0);
11134+
SDLoc DL(N);
1112911135

1113011136
// fold (ctpop c1) -> c2
11131-
if (DAG.isConstantIntBuildVectorOrConstantInt(N0))
11132-
return DAG.getNode(ISD::CTPOP, SDLoc(N), VT, N0);
11137+
if (SDValue C = DAG.FoldConstantArithmetic(ISD::CTPOP, DL, VT, {N0}))
11138+
return C;
1113311139
return SDValue();
1113411140
}
1113511141

0 commit comments

Comments
 (0)