InstCombine: constant comparison involving ashr is wrongly simplified.
| Bugzilla Link | 20945 |
| Resolution | FIXED |
| Resolved on | Sep 17, 2014 07:52 |
| Version | trunk |
| OS | Windows NT |
| Attachments | Possible fix. |
| CC | @hfinkel,@rotateright |
Extended Description
Example:
define i32 @negative_constants(i32 %B) { entry: %shr = ashr i32 -9, %B %cmp = icmp ne i32 %shr, -5 br i1 %cmp, label %if.then, label %return
if.then: br label %return
return: %retval = phi i32 [ 0, %if.then ], [ 42, %entry ] ret i32 %retval }
The instruction combiner wrongly thinks that statement 'ashr i32 -9, %B' can never evaluate to -5. Therefore, it wrongly assumes that %cmp would always be 'true' and the branch to if.then would always be taken.
In this reproducible, if %B is equal to 1, then %cmp is 'false' (since %shr would be equal to -5). Therefore, it is not safe to fold %cmp to 'true'. What instead should be done is converting the comparison between %shr and -5 into a comparison between %B and 1. This would allow us to get rid of the arithmetic shift (and eventually simplify the CFG folding the compare-and-branch into a single select of either 0 or 42).
This is a regression introduced by revision 213678.
I attached a patch that fixes this issue (I plan to send this patch on the mailing list for review).
This bug is now fixed at revision 217950. Link to the phabricator review: http://reviews.llvm.org/D5356