diff options
author | Bjorn Pettersson <bjorn.a.pettersson@ericsson.com> | 2019-09-07 12:16:23 +0000 |
---|---|---|
committer | Bjorn Pettersson <bjorn.a.pettersson@ericsson.com> | 2019-09-07 12:16:23 +0000 |
commit | d065c811649f0d0df5429741a9a3dd643e88a9fe (patch) | |
tree | 3e99e0e9a83ebaf1d1eed71659a69ffaebedb96d /llvm/lib/CodeGen | |
parent | 5e331e4ce85ad37dca45739846c2a801f06ab573 (diff) | |
download | bcm5719-llvm-d065c811649f0d0df5429741a9a3dd643e88a9fe.tar.gz bcm5719-llvm-d065c811649f0d0df5429741a9a3dd643e88a9fe.zip |
[CodeGen] Handle SMULFIXSAT with scale zero in TargetLowering::expandFixedPointMul
Summary:
Normally TargetLowering::expandFixedPointMul would handle
SMULFIXSAT with scale zero by using an SMULO to compute the
product and determine if saturation is needed (if overflow
happened). But if SMULO isn't custom/legal it falls through
and uses the same technique, using MULHS/SMUL_LOHI, as used
for non-zero scales.
Problem was that when checking for overflow (handling saturation)
when not using MULO we did not expect to find a zero scale. So
we ended up in an assertion when doing
APInt::getLowBitsSet(VTSize, Scale - 1)
This patch fixes the problem by adding a new special case for
how saturation is computed when scale is zero.
Reviewers: RKSimon, bevinh, leonardchan, spatel
Reviewed By: RKSimon
Subscribers: wuzish, nemanjai, hiraditya, MaskRay, jsji, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D67071
llvm-svn: 371309
Diffstat (limited to 'llvm/lib/CodeGen')
-rw-r--r-- | llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp | 31 |
1 files changed, 21 insertions, 10 deletions
diff --git a/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp b/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp index b42e272c3c1..d974c8cf3c7 100644 --- a/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp @@ -6800,26 +6800,37 @@ TargetLowering::expandFixedPointMul(SDNode *Node, SelectionDAG &DAG) const { } // Signed overflow happened if the upper (VTSize - Scale + 1) bits (of the - // widened multiplication) aren't all ones or all zeroes. We handled Scale==0 - // above so all the bits to examine is in Hi. + // widened multiplication) aren't all ones or all zeroes. + + SDValue SatMin = DAG.getConstant(APInt::getSignedMinValue(VTSize), dl, VT); + SDValue SatMax = DAG.getConstant(APInt::getSignedMaxValue(VTSize), dl, VT); + + if (Scale == 0) { + SDValue Sign = DAG.getNode(ISD::SRA, dl, VT, Lo, + DAG.getConstant(VTSize - 1, dl, ShiftTy)); + SDValue Overflow = DAG.getSetCC(dl, BoolVT, Hi, Sign, ISD::SETNE); + // Saturated to SatMin if wide product is negative, and SatMax if wide + // product is positive ... + SDValue Zero = DAG.getConstant(0, dl, VT); + SDValue ResultIfOverflow = DAG.getSelectCC(dl, Hi, Zero, SatMin, SatMax, + ISD::SETLT); + // ... but only if we overflowed. + return DAG.getSelect(dl, VT, Overflow, ResultIfOverflow, Result); + } + + // We handled Scale==0 above so all the bits to examine is in Hi. // Saturate to max if ((Hi >> (Scale - 1)) > 0), // which is the same as if (Hi > (1 << (Scale - 1)) - 1) - APInt MaxVal = APInt::getSignedMaxValue(VTSize); SDValue LowMask = DAG.getConstant(APInt::getLowBitsSet(VTSize, Scale - 1), dl, VT); - Result = DAG.getSelectCC(dl, Hi, LowMask, - DAG.getConstant(MaxVal, dl, VT), Result, - ISD::SETGT); + Result = DAG.getSelectCC(dl, Hi, LowMask, SatMax, Result, ISD::SETGT); // Saturate to min if (Hi >> (Scale - 1)) < -1), // which is the same as if (HI < (-1 << (Scale - 1)) - APInt MinVal = APInt::getSignedMinValue(VTSize); SDValue HighMask = DAG.getConstant(APInt::getHighBitsSet(VTSize, VTSize - Scale + 1), dl, VT); - Result = DAG.getSelectCC(dl, Hi, HighMask, - DAG.getConstant(MinVal, dl, VT), Result, - ISD::SETLT); + Result = DAG.getSelectCC(dl, Hi, HighMask, SatMin, Result, ISD::SETLT); return Result; } |