Skip to content

Commit

Permalink
[DAG] refactor related div/rem folds; NFCI
Browse files Browse the repository at this point in the history
This is known incomplete and not called in the right order relative to
other folds, but that's the current behavior. I'm just trying to clean
this up before making actual functional changes to make the patch smaller.

The logic here should mimic the IR equivalents that are in InstSimplify's
simplifyDivRem().


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@297086 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
rotateright committed Mar 6, 2017
1 parent 1855747 commit 5e80a82
Showing 1 changed file with 32 additions and 28 deletions.
60 changes: 32 additions & 28 deletions lib/CodeGen/SelectionDAG/DAGCombiner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2368,6 +2368,31 @@ SDValue DAGCombiner::useDivRem(SDNode *Node) {
return combined;
}

static SDValue simplifyDivRem(SDNode *N, SelectionDAG &DAG) {
SDValue N0 = N->getOperand(0);
SDValue N1 = N->getOperand(1);
EVT VT = N->getValueType(0);
SDLoc DL(N);

// X / undef -> undef
// X % undef -> undef
if (N1.isUndef())
return N1;

// X / 0 --> undef
// X % 0 --> undef
// We don't need to preserve faults!
if (isNullConstantOrNullSplatConstant(N1))
return DAG.getUNDEF(VT);

// undef / X -> 0
// undef % X -> 0
if (N0.isUndef())
return DAG.getConstant(0, DL, VT);

return SDValue();
}

SDValue DAGCombiner::visitSDIV(SDNode *N) {
SDValue N0 = N->getOperand(0);
SDValue N1 = N->getOperand(1);
Expand Down Expand Up @@ -2457,15 +2482,8 @@ SDValue DAGCombiner::visitSDIV(SDNode *N) {
if (SDValue DivRem = useDivRem(N))
return DivRem;

// undef / X -> 0
if (N0.isUndef())
return DAG.getConstant(0, DL, VT);
// X / undef -> undef
if (N1.isUndef())
return N1;
// X / 0 --> undef (we don't need to preserve faults!)
if (N1C && N1C->isNullValue())
return DAG.getUNDEF(VT);
if (SDValue V = simplifyDivRem(N, DAG))
return V;

return SDValue();
}
Expand Down Expand Up @@ -2535,15 +2553,8 @@ SDValue DAGCombiner::visitUDIV(SDNode *N) {
if (SDValue DivRem = useDivRem(N))
return DivRem;

// undef / X -> 0
if (N0.isUndef())
return DAG.getConstant(0, DL, VT);
// X / undef -> undef
if (N1.isUndef())
return N1;
// X / 0 --> undef (we don't need to preserve faults!)
if (N1C && N1C->isNullValue())
return DAG.getUNDEF(VT);
if (SDValue V = simplifyDivRem(N, DAG))
return V;

return SDValue();
}
Expand Down Expand Up @@ -2618,16 +2629,9 @@ SDValue DAGCombiner::visitREM(SDNode *N) {
if (SDValue DivRem = useDivRem(N))
return DivRem.getValue(1);

// undef % X -> 0
if (N0.isUndef())
return DAG.getConstant(0, DL, VT);
// X % undef -> undef
if (N1.isUndef())
return N1;
// X % 0 --> undef (we don't need to preserve faults!)
if (N1C && N1C->isNullValue())
return DAG.getUNDEF(VT);

if (SDValue V = simplifyDivRem(N, DAG))
return V;

return SDValue();
}

Expand Down

0 comments on commit 5e80a82

Please sign in to comment.