LLVM Bugzilla is read-only and represents the historical archive of all LLVM issues filled before November 26, 2021. Use github to submit LLVM bugs

Bug 1248 - Another InstCombine issue
Summary: Another InstCombine issue
Status: RESOLVED FIXED
Alias: None
Product: libraries
Classification: Unclassified
Component: Scalar Optimizations (show other bugs)
Version: trunk
Hardware: All All
: P normal
Assignee: Reid Spencer
URL:
Keywords: miscompilation, regression
Depends on:
Blocks: 1166
  Show dependency tree
 
Reported: 2007-03-10 12:25 PST by Anton Korobeynikov
Modified: 2010-02-22 12:44 PST (History)
2 users (show)

See Also:
Fixed By Commit(s):


Attachments
Bytecode (501 bytes, application/octet-stream)
2007-03-10 12:25 PST, Anton Korobeynikov
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Anton Korobeynikov 2007-03-10 12:25:04 PST
Consider the attached bytecode. This is just -O0 compiled bytecode from gcc
testsuite.

Running "opt -instcombine" will lead to "eq" comparison turn to "slt", which is
definitely wrong.
Comment 1 Anton Korobeynikov 2007-03-10 12:25:51 PST
Created attachment 701 [details]
Bytecode
Comment 2 Chris Lattner 2007-03-13 10:06:34 PDT
I think that this is trying to simplify (x sdiv 12) != -6 
Comment 3 Chris Lattner 2007-03-13 10:09:36 PDT
Here's a reduced testcase:
define i1 @test(i32 %tmp6) {
        %tmp7 = sdiv i32 %tmp6, 12              ; <i32> [#uses=1]
        icmp ne i32 %tmp7, -6           ; <i1>:0 [#uses=1]
        ret i1 %0
}

instcombine compiles it to:

define i1 @test(i32 %tmp6) {
        icmp sgt i32 %tmp6, -73         ; <i1>:0 [#uses=1]
        ret i1 %0
}

which is wrong for an input of -70.

-Chris
Comment 4 Chris Lattner 2007-03-13 10:15:43 PDT
This appears to be fallout from the signless types change.

The problem is AddWithOverflow in Instcombine.  It is not calculating overflow correctly if the operands 
are signed.  The getZExtValue() < getZExtValue() comparison needs to do a signed comparison if the input 
operands are signed (requiring a bool to be passed in).

Reid, plz investigate.

-Chris
Comment 5 Reid Spencer 2007-03-13 17:08:43 PDT
Mine.
Comment 6 Reid Spencer 2007-03-21 14:48:49 PDT
Making the AddWithOverflow change doesn't fix the problem.
Comment 7 Reid Spencer 2007-03-21 18:21:54 PDT
Turns out there were two bugs. The AddWithOverflow needed to be APIntified and
to deal with the sign properly. Also, in the case of:
   %Y = sdiv iN %X, C1
   icmp pred iN %Y, -C2
there was an off-by-one error in the high bound for the range test that is
produced in the replacement. 

This patch was applied:
http://lists.cs.uiuc.edu/pipermail/llvm-commits/Week-of-Mon-20070319/046072.html