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 1253 - "optimized" range comparison not simplified
Summary: "optimized" range comparison not simplified
Status: RESOLVED FIXED
Alias: None
Product: libraries
Classification: Unclassified
Component: Scalar Optimizations (show other bugs)
Version: 1.0
Hardware: All Linux
: P normal
Assignee: Chris Lattner
URL:
Keywords: code-quality
Depends on:
Blocks:
 
Reported: 2007-03-12 16:29 PDT by Duncan Sands
Modified: 2010-02-22 12:48 PST (History)
1 user (show)

See Also:
Fixed By Commit(s):


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Duncan Sands 2007-03-12 16:29:40 PDT
The condition a<=x<=b can be transformed into (unsigned)n-a<=b-a.
However if a=INT_MIN then this "optimization" leads to suboptimal
code:

#include <limits.h>
int bad (int n) {
  return ((unsigned)n-INT_MIN<=-1-INT_MIN);
}

->

define i32 @bad(i32 %n) {
entry:
        %tmp2 = xor i32 %n, -2147483648         ; <i32> [#uses=1]
        icmp sgt i32 %tmp2, -1          ; <i1>:0 [#uses=1]
        zext i1 %0 to i32               ; <i32>:0 [#uses=1]
        ret i32 %0
}

This would be better as: "return n<0;"
Comment 1 Duncan Sands 2007-03-14 04:09:06 PDT
i.e. this example shows that replacing ult with slt (and presumably
vice-versa) can be advantageous, but this is not exploited.
Comment 2 Duncan Sands 2007-03-27 04:10:56 PDT
I forgot to mention where this comes from: the gcc -> llvm switch
conversion code lowers wide case ranges to explicit conditional
branches.  The test that x is in the range lo .. hi is emitted as
"x-lo ule hi-lo".  If lo is INT_MIN then this results in suboptimal
code, as explained in this PR.  I could have checked for this case
explicitly in the switch conversion code, but I didn't want to do so
because it is really a job for the optimizers: they should be able to
handle this case.  Oddly enough, if you emit the test as "(x>=lo)&&(x<=hi)"
then it gets simplified optimally.
Comment 3 Chris Lattner 2007-04-02 20:48:22 PDT
Fixed, testcase here: Transforms/InstCombine/xor2.ll:test[01]
Patch here:
http://lists.cs.uiuc.edu/pipermail/llvm-commits/Week-of-Mon-20070402/046701.html

This handles the general case of a xor constant, not just this specific one.

-Chris