Skip to content
This repository was archived by the owner on Mar 28, 2020. It is now read-only.

Commit 8a57afc

Browse files
committed
[DAGCombiner] visitSDIV - simplify pow2 handling. NFCI.
Use the builtin constant folding of getNode() etc. instead of doing it manually. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@335720 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 988e5b9 commit 8a57afc

File tree

1 file changed

+12
-29
lines changed

1 file changed

+12
-29
lines changed

lib/CodeGen/SelectionDAG/DAGCombiner.cpp

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3045,9 +3045,7 @@ SDValue DAGCombiner::visitSDIV(SDNode *N) {
30453045
// vector of such elements.
30463046
SmallBitVector KnownNegatives(
30473047
(N1C || !VT.isVector()) ? 1 : VT.getVectorNumElements(), false);
3048-
unsigned EltIndex = 0;
3049-
auto IsPowerOfTwo = [&KnownNegatives, &EltIndex](ConstantSDNode *C) {
3050-
unsigned Idx = EltIndex++;
3048+
auto IsPowerOfTwo = [](ConstantSDNode *C) {
30513049
if (C->isNullValue() || C->isOpaque())
30523050
return false;
30533051
// The instruction sequence to be generated contains shifting C by (op size
@@ -3063,10 +3061,8 @@ SDValue DAGCombiner::visitSDIV(SDNode *N) {
30633061

30643062
if (C->getAPIntValue().isPowerOf2())
30653063
return true;
3066-
if ((-C->getAPIntValue()).isPowerOf2()) {
3067-
KnownNegatives.set(Idx);
3064+
if ((-C->getAPIntValue()).isPowerOf2())
30683065
return true;
3069-
}
30703066
return false;
30713067
};
30723068

@@ -3088,44 +3084,31 @@ SDValue DAGCombiner::visitSDIV(SDNode *N) {
30883084
SDValue Inexact = DAG.getNode(ISD::SUB, DL, ShiftAmtTy, Bits, C1);
30893085
if (!isConstantOrConstantVector(Inexact))
30903086
return SDValue();
3087+
30913088
// Splat the sign bit into the register
30923089
SDValue Sign = DAG.getNode(ISD::SRA, DL, VT, N0,
30933090
DAG.getConstant(BitWidth - 1, DL, ShiftAmtTy));
30943091
AddToWorklist(Sign.getNode());
30953092

30963093
// Add (N0 < 0) ? abs2 - 1 : 0;
30973094
SDValue Srl = DAG.getNode(ISD::SRL, DL, VT, Sign, Inexact);
3098-
SDValue Add = DAG.getNode(ISD::ADD, DL, VT, N0, Srl);
30993095
AddToWorklist(Srl.getNode());
3100-
AddToWorklist(Add.getNode()); // Divide by pow2
3096+
SDValue Add = DAG.getNode(ISD::ADD, DL, VT, N0, Srl);
3097+
AddToWorklist(Add.getNode());
31013098
SDValue Sra = DAG.getNode(ISD::SRA, DL, VT, Add, C1);
3099+
AddToWorklist(Sra.getNode());
31023100

31033101
// If dividing by a positive value, we're done. Otherwise, the result must
31043102
// be negated.
3105-
if (KnownNegatives.none())
3106-
return Sra;
3107-
3108-
AddToWorklist(Sra.getNode());
31093103
SDValue Sub =
31103104
DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, DL, VT), Sra);
3111-
// If all shift amount elements are negative, we're done.
3112-
if (KnownNegatives.all())
3113-
return Sub;
3114-
3115-
// Shift amount has both positive and negative elements.
3116-
assert(VT.isVector() && !N0C &&
3117-
"Expecting a non-splat vector shift amount");
31183105

3119-
SmallVector<SDValue, 64> VSelectMask;
3120-
for (int i = 0, e = VT.getVectorNumElements(); i < e; ++i)
3121-
VSelectMask.push_back(
3122-
DAG.getConstant(KnownNegatives[i] ? -1 : 0, DL, MVT::i1));
3123-
3124-
SDValue Mask =
3125-
DAG.getBuildVector(EVT::getVectorVT(*DAG.getContext(), MVT::i1,
3126-
VT.getVectorElementCount()),
3127-
DL, VSelectMask);
3128-
return DAG.getNode(ISD::VSELECT, DL, VT, Mask, Sub, Sra);
3106+
// FIXME: Use SELECT_CC once we improve SELECT_CC constant-folding.
3107+
SDValue Res = DAG.getSelect(
3108+
DL, VT,
3109+
DAG.getSetCC(DL, VT, N1, DAG.getConstant(0, DL, VT), ISD::SETLT), Sub,
3110+
Sra);
3111+
return Res;
31293112
}
31303113

31313114
// If integer divide is expensive and we satisfy the requirements, emit an

0 commit comments

Comments
 (0)