[PATCH] D39669: DAG: Preserve nuw when reassociating adds
Matt Arsenault via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Sun Nov 5 15:49:43 PST 2017
arsenm created this revision.
Herald added a subscriber: wdng.
DAG version of subset of https://reviews.llvm.org/D39417
https://reviews.llvm.org/D39669
Files:
lib/CodeGen/SelectionDAG/DAGCombiner.cpp
Index: lib/CodeGen/SelectionDAG/DAGCombiner.cpp
===================================================================
--- lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ -386,7 +386,7 @@
SDValue XformToShuffleWithZero(SDNode *N);
SDValue ReassociateOps(unsigned Opc, const SDLoc &DL, SDValue LHS,
- SDValue RHS);
+ SDValue RHS, const SDNodeFlags Flags = SDNodeFlags());
SDValue visitShiftByConstant(SDNode *N, ConstantSDNode *Amt);
@@ -954,14 +954,18 @@
}
SDValue DAGCombiner::ReassociateOps(unsigned Opc, const SDLoc &DL, SDValue N0,
- SDValue N1) {
+ SDValue N1, const SDNodeFlags OrigFlags) {
EVT VT = N0.getValueType();
if (N0.getOpcode() == Opc) {
+ SDNodeFlags Flags;
+ Flags.setNoUnsignedWrap(N0->getFlags().hasNoUnsignedWrap() &&
+ OrigFlags.hasNoUnsignedWrap());
+
if (SDNode *L = DAG.isConstantIntBuildVectorOrConstantInt(N0.getOperand(1))) {
if (SDNode *R = DAG.isConstantIntBuildVectorOrConstantInt(N1)) {
// reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
if (SDValue OpNode = DAG.FoldConstantArithmetic(Opc, DL, VT, L, R))
- return DAG.getNode(Opc, DL, VT, N0.getOperand(0), OpNode);
+ return DAG.getNode(Opc, DL, VT, N0.getOperand(0), OpNode, Flags);
return SDValue();
}
if (N0.hasOneUse()) {
@@ -971,27 +975,33 @@
if (!OpNode.getNode())
return SDValue();
AddToWorklist(OpNode.getNode());
- return DAG.getNode(Opc, DL, VT, OpNode, N0.getOperand(1));
+ return DAG.getNode(Opc, DL, VT, OpNode, N0.getOperand(1), Flags);
}
}
}
if (N1.getOpcode() == Opc) {
+ SDNodeFlags Flags;
+ Flags.setNoUnsignedWrap(OrigFlags.hasNoUnsignedWrap() &&
+ N1->getFlags().hasNoUnsignedWrap());
+
if (SDNode *R = DAG.isConstantIntBuildVectorOrConstantInt(N1.getOperand(1))) {
if (SDNode *L = DAG.isConstantIntBuildVectorOrConstantInt(N0)) {
// reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
- if (SDValue OpNode = DAG.FoldConstantArithmetic(Opc, DL, VT, R, L))
- return DAG.getNode(Opc, DL, VT, N1.getOperand(0), OpNode);
+ if (SDValue OpNode = DAG.FoldConstantArithmetic(Opc, DL, VT, R, L)) {
+ return DAG.getNode(Opc, DL, VT, N1.getOperand(0), OpNode, Flags);
+ }
return SDValue();
}
if (N1.hasOneUse()) {
// reassoc. (op x, (op y, c1)) -> (op (op x, y), c1) iff x+c1 has one
// use
- SDValue OpNode = DAG.getNode(Opc, SDLoc(N0), VT, N0, N1.getOperand(0));
+ SDValue OpNode = DAG.getNode(Opc, SDLoc(N0), VT, N0, N1.getOperand(0),
+ Flags);
if (!OpNode.getNode())
return SDValue();
AddToWorklist(OpNode.getNode());
- return DAG.getNode(Opc, DL, VT, OpNode, N1.getOperand(1));
+ return DAG.getNode(Opc, DL, VT, OpNode, N1.getOperand(1), Flags);
}
}
}
@@ -1993,16 +2003,19 @@
isa<FrameIndexSDNode>(N0.getOperand(0)) &&
isa<ConstantSDNode>(N0.getOperand(1)) &&
DAG.haveNoCommonBitsSet(N0.getOperand(0), N0.getOperand(1))) {
- SDValue Add0 = DAG.getNode(ISD::ADD, DL, VT, N1, N0.getOperand(1));
- return DAG.getNode(ISD::ADD, DL, VT, N0.getOperand(0), Add0);
+ SDNodeFlags Flags;
+ Flags.setNoUnsignedWrap(N->getFlags().hasNoUnsignedWrap());
+
+ SDValue Add0 = DAG.getNode(ISD::ADD, DL, VT, N1, N0.getOperand(1), Flags);
+ return DAG.getNode(ISD::ADD, DL, VT, N0.getOperand(0), Add0, Flags);
}
}
if (SDValue NewSel = foldBinOpIntoSelect(N))
return NewSel;
// reassociate add
- if (SDValue RADD = ReassociateOps(ISD::ADD, DL, N0, N1))
+ if (SDValue RADD = ReassociateOps(ISD::ADD, DL, N0, N1, N->getFlags()))
return RADD;
// fold ((0-A) + B) -> B-A
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D39669.121667.patch
Type: text/x-patch
Size: 4039 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20171105/085fe5f8/attachment.bin>
More information about the llvm-commits
mailing list