Skip to content

Commit

Permalink
[LegalizeTypes] Fix saturation bug for smul.fix.sat
Browse files Browse the repository at this point in the history
Summary:
Make sure we use SETGE instead of SETGT when checking
if the sign bit is zero at SMULFIXSAT expansion.

The faulty expansion occured when doing "expand" of
SMULFIXSAT and the scale was exactly matching the
size of the smaller type. For example doing
  i64 Z = SMULFIXSAT X, Y, 32
and expanding X/Y/Z into using two i32 values.

The problem was that we sometimes did not saturate
to min when overflowing.

Here is an example using Q3.4 numbers:

Consider that we are multiplying X and Y.
  X = 0x80 (-8.0 as Q3.4)
  Y = 0x20 (2.0 as Q3.4)
To avoid loss of precision we do a widening
multiplication, getting a 16 bit result
  Z = 0xF000 (-16.0 as Q7.8)

To detect negative overflow we should check if
the five most significant bits in Z are less than -1.
Assume that we name the 4 most significant bits
as HH and the next 4 bits as HL. Then we can do the
check by examining if
 (HH < -1) or (HH == -1 && "sign bit in HL is zero").

The fault was that we have been doing the check as
 (HH < -1) or (HH == -1 && HL > 0)
instead of
 (HH < -1) or (HH == -1 && HL >= 0).

In our example HH is -1 and HL is 0, so the old
code did not trigger saturation and simply truncated
the result to 0x00 (0.0). With the bugfix we instead
detect that we should saturate to min, and the result
will be set to 0x80 (-8.0).

Reviewers: leonardchan, bevinh

Reviewed By: leonardchan

Subscribers: hiraditya, llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D64331

llvm-svn: 365455
  • Loading branch information
bjope committed Jul 9, 2019
1 parent 6f6e5d8 commit 5902901
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
6 changes: 3 additions & 3 deletions llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp
Expand Up @@ -2902,8 +2902,8 @@ void DAGTypeLegalizer::ExpandIntRes_MULFIX(SDNode *N, SDValue &Lo,
Lo = ResultLH;
Hi = ResultHL;

// We overflow max if HH > 0 or HH == 0 && HL sign is negative.
// We overflow min if HH < -1 or HH == -1 && HL sign is 0.
// We overflow max if HH > 0 or HH == 0 && HL sign bit is 1.
// We overflow min if HH < -1 or HH == -1 && HL sign bit is 0.
if (Saturating) {
SDValue HHPos = DAG.getSetCC(dl, BoolNVT, ResultHH, NVTZero, ISD::SETGT);
SDValue HHZero = DAG.getSetCC(dl, BoolNVT, ResultHH, NVTZero, ISD::SETEQ);
Expand All @@ -2913,7 +2913,7 @@ void DAGTypeLegalizer::ExpandIntRes_MULFIX(SDNode *N, SDValue &Lo,

SDValue HHNeg = DAG.getSetCC(dl, BoolNVT, ResultHH, NVTNeg1, ISD::SETLT);
SDValue HHNeg1 = DAG.getSetCC(dl, BoolNVT, ResultHH, NVTNeg1, ISD::SETEQ);
SDValue HLPos = DAG.getSetCC(dl, BoolNVT, ResultHL, NVTZero, ISD::SETGT);
SDValue HLPos = DAG.getSetCC(dl, BoolNVT, ResultHL, NVTZero, ISD::SETGE);
SatMin = DAG.getNode(ISD::OR, dl, BoolNVT, HHNeg,
DAG.getNode(ISD::AND, dl, BoolNVT, HHNeg1, HLPos));
}
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/CodeGen/X86/smul_fix_sat.ll
Expand Up @@ -629,7 +629,7 @@ define i64 @func7(i64 %x, i64 %y) nounwind {
; X86-NEXT: cmovnsl %esi, %edi
; X86-NEXT: cmovnsl %ecx, %edx
; X86-NEXT: testl %edx, %edx
; X86-NEXT: setg %cl
; X86-NEXT: setns %cl
; X86-NEXT: sets %ch
; X86-NEXT: testl %edi, %edi
; X86-NEXT: setg %bl
Expand Down

0 comments on commit 5902901

Please sign in to comment.