@@ -7843,69 +7843,110 @@ SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7843
7843
}
7844
7844
}
7845
7845
7846
- // Perform trivial constant folding.
7847
- if (SDValue SV = FoldConstantArithmetic(Opcode, DL, VT, {N1, N2}, Flags))
7848
- return SV;
7846
+ if (N1.getOpcode() == ISD::POISON || N2.getOpcode() == ISD::POISON) {
7847
+ switch (Opcode) {
7848
+ case ISD::XOR:
7849
+ case ISD::ADD:
7850
+ case ISD::PTRADD:
7851
+ case ISD::SUB:
7852
+ case ISD::SIGN_EXTEND_INREG:
7853
+ case ISD::UDIV:
7854
+ case ISD::SDIV:
7855
+ case ISD::UREM:
7856
+ case ISD::SREM:
7857
+ case ISD::MUL:
7858
+ case ISD::AND:
7859
+ case ISD::SSUBSAT:
7860
+ case ISD::USUBSAT:
7861
+ case ISD::UMIN:
7862
+ case ISD::OR:
7863
+ case ISD::SADDSAT:
7864
+ case ISD::UADDSAT:
7865
+ case ISD::UMAX:
7866
+ case ISD::SMAX:
7867
+ case ISD::SMIN:
7868
+ // fold op(arg1, poison) -> poison, fold op(poison, arg2) -> poison.
7869
+ return N2.getOpcode() == ISD::POISON ? N2 : N1;
7870
+ }
7871
+ }
7849
7872
7850
7873
// Canonicalize an UNDEF to the RHS, even over a constant.
7851
- if (N1.isUndef() ) {
7874
+ if (N1.getOpcode() == ISD::UNDEF && N2.getOpcode() != ISD::UNDEF ) {
7852
7875
if (TLI->isCommutativeBinOp(Opcode)) {
7853
7876
std::swap(N1, N2);
7854
7877
} else {
7855
7878
switch (Opcode) {
7856
7879
case ISD::PTRADD:
7857
7880
case ISD::SUB:
7858
- // fold op(undef, arg2 ) -> undef, fold op(poison, arg2) ->poison .
7859
- return N1.getOpcode() == ISD::POISON ? getPOISON(VT) : getUNDEF(VT) ;
7881
+ // fold op(undef, non_undef_arg2 ) -> undef.
7882
+ return N1;
7860
7883
case ISD::SIGN_EXTEND_INREG:
7861
7884
case ISD::UDIV:
7862
7885
case ISD::SDIV:
7863
7886
case ISD::UREM:
7864
7887
case ISD::SREM:
7865
7888
case ISD::SSUBSAT:
7866
7889
case ISD::USUBSAT:
7867
- // fold op(undef, arg2) -> 0, fold op(poison, arg2) -> poison.
7868
- return N1.getOpcode() == ISD::POISON ? getPOISON(VT)
7869
- : getConstant(0, DL, VT);
7890
+ // fold op(undef, non_undef_arg2) -> 0.
7891
+ return getConstant(0, DL, VT);
7870
7892
}
7871
7893
}
7872
7894
}
7873
7895
7874
7896
// Fold a bunch of operators when the RHS is undef.
7875
- if (N2.isUndef() ) {
7897
+ if (N2.getOpcode() == ISD::UNDEF ) {
7876
7898
switch (Opcode) {
7877
7899
case ISD::XOR:
7878
- if (N1.isUndef() )
7900
+ if (N1.getOpcode() == ISD::UNDEF )
7879
7901
// Handle undef ^ undef -> 0 special case. This is a common
7880
7902
// idiom (misuse).
7881
7903
return getConstant(0, DL, VT);
7882
7904
[[fallthrough]];
7883
7905
case ISD::ADD:
7884
7906
case ISD::PTRADD:
7885
7907
case ISD::SUB:
7908
+ // fold op(arg1, undef) -> undef.
7909
+ return N2;
7886
7910
case ISD::UDIV:
7887
7911
case ISD::SDIV:
7888
7912
case ISD::UREM:
7889
7913
case ISD::SREM:
7890
- // fold op(arg1, undef) -> undef, fold op(arg1, poison) -> poison.
7891
- return N2.getOpcode() == ISD::POISON ? getPOISON(VT) : getUNDEF (VT);
7914
+ // fold op(arg1, undef) -> poison.
7915
+ return getPOISON(VT);
7892
7916
case ISD::MUL:
7893
7917
case ISD::AND:
7894
7918
case ISD::SSUBSAT:
7895
7919
case ISD::USUBSAT:
7896
- // fold op(arg1, undef) -> 0, fold op(arg1, poison) -> poison.
7897
- return N2.getOpcode() == ISD::POISON ? getPOISON(VT)
7898
- : getConstant(0, DL, VT);
7920
+ case ISD::UMIN:
7921
+ // fold op(undef, undef) -> undef, fold op(arg1, undef) -> 0.
7922
+ return N1.getOpcode() == ISD::UNDEF ? N2 : getConstant(0, DL, VT);
7899
7923
case ISD::OR:
7900
7924
case ISD::SADDSAT:
7901
7925
case ISD::UADDSAT:
7902
- // fold op(arg1, undef) -> an all-ones constant, fold op(arg1, poison) ->
7903
- // poison.
7904
- return N2.getOpcode() == ISD::POISON ? getPOISON(VT)
7905
- : getAllOnesConstant(DL, VT);
7926
+ case ISD::UMAX:
7927
+ // fold op(undef, undef) -> undef, fold op(arg1, undef) -> -1.
7928
+ return N1.getOpcode() == ISD::UNDEF ? N2 : getAllOnesConstant(DL, VT);
7929
+ case ISD::SMAX:
7930
+ // fold op(undef, undef) -> undef, fold op(arg1, undef) -> MAX_INT.
7931
+ return N1.getOpcode() == ISD::UNDEF
7932
+ ? N2
7933
+ : getConstant(
7934
+ APInt::getSignedMaxValue(VT.getScalarSizeInBits()), DL,
7935
+ VT);
7936
+ case ISD::SMIN:
7937
+ // fold op(undef, undef) -> undef, fold op(arg1, undef) -> MIN_INT.
7938
+ return N1.getOpcode() == ISD::UNDEF
7939
+ ? N2
7940
+ : getConstant(
7941
+ APInt::getSignedMinValue(VT.getScalarSizeInBits()), DL,
7942
+ VT);
7906
7943
}
7907
7944
}
7908
7945
7946
+ // Perform trivial constant folding.
7947
+ if (SDValue SV = FoldConstantArithmetic(Opcode, DL, VT, {N1, N2}, Flags))
7948
+ return SV;
7949
+
7909
7950
// Memoize this node if possible.
7910
7951
SDNode *N;
7911
7952
SDVTList VTs = getVTList(VT);
0 commit comments