Skip to content

Commit 57b0d33

Browse files
committed
[DAGCombiner] ISD::AND/OR/XOR - use general SelectionDAG::FoldConstantArithmetic
This handles all the constant splat / opaque testing for us.
1 parent a967aa2 commit 57b0d33

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5165,17 +5165,19 @@ SDValue DAGCombiner::visitAND(SDNode *N) {
51655165
}
51665166

51675167
// fold (and c1, c2) -> c1&c2
5168-
ConstantSDNode *N0C = getAsNonOpaqueConstant(N0);
51695168
ConstantSDNode *N1C = isConstOrConstSplat(N1);
5170-
if (N0C && N1C && !N1C->isOpaque())
5171-
return DAG.FoldConstantArithmetic(ISD::AND, SDLoc(N), VT, N0C, N1C);
5169+
if (SDValue C = DAG.FoldConstantArithmetic(ISD::AND, SDLoc(N), VT, {N0, N1}))
5170+
return C;
5171+
51725172
// canonicalize constant to RHS
51735173
if (DAG.isConstantIntBuildVectorOrConstantInt(N0) &&
51745174
!DAG.isConstantIntBuildVectorOrConstantInt(N1))
51755175
return DAG.getNode(ISD::AND, SDLoc(N), VT, N1, N0);
5176+
51765177
// fold (and x, -1) -> x
51775178
if (isAllOnesConstant(N1))
51785179
return N0;
5180+
51795181
// if (and x, c) is known to be zero, return 0
51805182
unsigned BitWidth = VT.getScalarSizeInBits();
51815183
if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
@@ -5866,17 +5868,19 @@ SDValue DAGCombiner::visitOR(SDNode *N) {
58665868
}
58675869

58685870
// fold (or c1, c2) -> c1|c2
5869-
ConstantSDNode *N0C = getAsNonOpaqueConstant(N0);
58705871
ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
5871-
if (N0C && N1C && !N1C->isOpaque())
5872-
return DAG.FoldConstantArithmetic(ISD::OR, SDLoc(N), VT, N0C, N1C);
5872+
if (SDValue C = DAG.FoldConstantArithmetic(ISD::OR, SDLoc(N), VT, {N0, N1}))
5873+
return C;
5874+
58735875
// canonicalize constant to RHS
58745876
if (DAG.isConstantIntBuildVectorOrConstantInt(N0) &&
58755877
!DAG.isConstantIntBuildVectorOrConstantInt(N1))
58765878
return DAG.getNode(ISD::OR, SDLoc(N), VT, N1, N0);
5879+
58775880
// fold (or x, 0) -> x
58785881
if (isNullConstant(N1))
58795882
return N0;
5883+
58805884
// fold (or x, -1) -> -1
58815885
if (isAllOnesConstant(N1))
58825886
return N1;
@@ -7021,20 +7025,22 @@ SDValue DAGCombiner::visitXOR(SDNode *N) {
70217025
SDLoc DL(N);
70227026
if (N0.isUndef() && N1.isUndef())
70237027
return DAG.getConstant(0, DL, VT);
7028+
70247029
// fold (xor x, undef) -> undef
70257030
if (N0.isUndef())
70267031
return N0;
70277032
if (N1.isUndef())
70287033
return N1;
7034+
70297035
// fold (xor c1, c2) -> c1^c2
7030-
ConstantSDNode *N0C = getAsNonOpaqueConstant(N0);
7031-
ConstantSDNode *N1C = getAsNonOpaqueConstant(N1);
7032-
if (N0C && N1C)
7033-
return DAG.FoldConstantArithmetic(ISD::XOR, DL, VT, N0C, N1C);
7036+
if (SDValue C = DAG.FoldConstantArithmetic(ISD::XOR, DL, VT, {N0, N1}))
7037+
return C;
7038+
70347039
// canonicalize constant to RHS
70357040
if (DAG.isConstantIntBuildVectorOrConstantInt(N0) &&
70367041
!DAG.isConstantIntBuildVectorOrConstantInt(N1))
70377042
return DAG.getNode(ISD::XOR, DL, VT, N1, N0);
7043+
70387044
// fold (xor x, 0) -> x
70397045
if (isNullConstant(N1))
70407046
return N0;

0 commit comments

Comments
 (0)