@@ -2847,11 +2847,23 @@ SDValue DAGCombiner::visitADDLike(SDNode *N) {
2847
2847
m_ConstInt(CM)))) &&
2848
2848
TLI.isLegalAddImmediate(
2849
2849
(CA * CM + CB->getAPIntValue()).getSExtValue())) {
2850
+ SDNodeFlags Flags;
2851
+ // If all the inputs are nuw, the outputs can be nuw. If all the input
2852
+ // are _also_ nsw the outputs can be too.
2853
+ if (N->getFlags().hasNoUnsignedWrap() &&
2854
+ N0->getFlags().hasNoUnsignedWrap() &&
2855
+ N0.getOperand(0)->getFlags().hasNoUnsignedWrap()) {
2856
+ Flags.setNoUnsignedWrap(true);
2857
+ if (N->getFlags().hasNoSignedWrap() &&
2858
+ N0->getFlags().hasNoSignedWrap() &&
2859
+ N0.getOperand(0)->getFlags().hasNoSignedWrap())
2860
+ Flags.setNoSignedWrap(true);
2861
+ }
2850
2862
SDValue Mul = DAG.getNode(ISD::MUL, SDLoc(N1), VT, A,
2851
- DAG.getConstant(CM, DL, VT));
2863
+ DAG.getConstant(CM, DL, VT), Flags );
2852
2864
return DAG.getNode(
2853
2865
ISD::ADD, DL, VT, Mul,
2854
- DAG.getConstant(CA * CM + CB->getAPIntValue(), DL, VT));
2866
+ DAG.getConstant(CA * CM + CB->getAPIntValue(), DL, VT), Flags );
2855
2867
}
2856
2868
// Also look in case there is an intermediate add.
2857
2869
if (sd_match(N0, m_OneUse(m_Add(
@@ -2860,12 +2872,28 @@ SDValue DAGCombiner::visitADDLike(SDNode *N) {
2860
2872
m_Value(B)))) &&
2861
2873
TLI.isLegalAddImmediate(
2862
2874
(CA * CM + CB->getAPIntValue()).getSExtValue())) {
2875
+ SDNodeFlags Flags;
2876
+ // If all the inputs are nuw, the outputs can be nuw. If all the input
2877
+ // are _also_ nsw the outputs can be too.
2878
+ SDValue OMul =
2879
+ N0.getOperand(0) == B ? N0.getOperand(1) : N0.getOperand(0);
2880
+ if (N->getFlags().hasNoUnsignedWrap() &&
2881
+ N0->getFlags().hasNoUnsignedWrap() &&
2882
+ OMul->getFlags().hasNoUnsignedWrap() &&
2883
+ OMul.getOperand(0)->getFlags().hasNoUnsignedWrap()) {
2884
+ Flags.setNoUnsignedWrap(true);
2885
+ if (N->getFlags().hasNoSignedWrap() &&
2886
+ N0->getFlags().hasNoSignedWrap() &&
2887
+ OMul->getFlags().hasNoSignedWrap() &&
2888
+ OMul.getOperand(0)->getFlags().hasNoSignedWrap())
2889
+ Flags.setNoSignedWrap(true);
2890
+ }
2863
2891
SDValue Mul = DAG.getNode(ISD::MUL, SDLoc(N1), VT, A,
2864
- DAG.getConstant(CM, DL, VT));
2865
- SDValue Add = DAG.getNode(ISD::ADD, SDLoc(N1), VT, Mul, B);
2892
+ DAG.getConstant(CM, DL, VT), Flags );
2893
+ SDValue Add = DAG.getNode(ISD::ADD, SDLoc(N1), VT, Mul, B, Flags );
2866
2894
return DAG.getNode(
2867
2895
ISD::ADD, DL, VT, Add,
2868
- DAG.getConstant(CA * CM + CB->getAPIntValue(), DL, VT));
2896
+ DAG.getConstant(CA * CM + CB->getAPIntValue(), DL, VT), Flags );
2869
2897
}
2870
2898
}
2871
2899
}
0 commit comments