Skip to content

Commit f8147dd

Browse files
committed
Add flags if all the source operations are nuw [+nsw]
I'm not sure this is worth it.
1 parent 1ef41bd commit f8147dd

File tree

1 file changed

+33
-5
lines changed

1 file changed

+33
-5
lines changed

llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2847,11 +2847,23 @@ SDValue DAGCombiner::visitADDLike(SDNode *N) {
28472847
m_ConstInt(CM)))) &&
28482848
TLI.isLegalAddImmediate(
28492849
(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+
}
28502862
SDValue Mul = DAG.getNode(ISD::MUL, SDLoc(N1), VT, A,
2851-
DAG.getConstant(CM, DL, VT));
2863+
DAG.getConstant(CM, DL, VT), Flags);
28522864
return DAG.getNode(
28532865
ISD::ADD, DL, VT, Mul,
2854-
DAG.getConstant(CA * CM + CB->getAPIntValue(), DL, VT));
2866+
DAG.getConstant(CA * CM + CB->getAPIntValue(), DL, VT), Flags);
28552867
}
28562868
// Also look in case there is an intermediate add.
28572869
if (sd_match(N0, m_OneUse(m_Add(
@@ -2860,12 +2872,28 @@ SDValue DAGCombiner::visitADDLike(SDNode *N) {
28602872
m_Value(B)))) &&
28612873
TLI.isLegalAddImmediate(
28622874
(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+
}
28632891
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);
28662894
return DAG.getNode(
28672895
ISD::ADD, DL, VT, Add,
2868-
DAG.getConstant(CA * CM + CB->getAPIntValue(), DL, VT));
2896+
DAG.getConstant(CA * CM + CB->getAPIntValue(), DL, VT), Flags);
28692897
}
28702898
}
28712899
}

0 commit comments

Comments
 (0)