InstCombine: wrong folding of a constant comparison involving ashr and negative numbers.
| Bugzilla Link | 21222 |
| Resolution | FIXED |
| Resolved on | Oct 09, 2014 12:55 |
| Version | trunk |
| OS | Windows NT |
Extended Description
Reproducible:
;; define i1 @test(i32 %B) { %shr = ashr i32 -93, %B %cmp = icmp eq i32 %shr, -2 ret i1 %cmp } ;;
Function @test should return true when %B is equal to 6.
That is because -93 >> 6 = -2.
Currently the Instruction Combiner wrongly folds the entire function to a return i1 false.
The problem is in the folding logic located in InstCombineCompares.cpp; method InstCombiner::FoldICmpCstShrCst wrongly computes the distance (in binary) between two negative values. With: AP1 < 0 AP2 < 0 AP2 != 0 AP1 != 0 AP1 > AP2
Currently the distance between AP2 and AP1 is wrongly computed as: (-AP2).logBase2() - (-AP1).logBase2()
the distance between AP1 and AP2 should be computed instead taking the ones' complement of AP2 and AP1: (~AP2).logBase2() - (~AP1).logBase2()
This is another regression introduced by revision 213678 which, unfortunately wasn't correctly fixed by revision 217950.
I have a fix for this bug which I plan to send soon for review on the mailing list.
This is now fixed at revision 219406.
assigned to @adibiagio