Hi,

Here is a case where the instruction combiner wrongly folds an 'icmp' instruction.

;;

define i1 @test(i32 %B) {

%shr = ashr i32 -93, %B %cmp = icmp eq i32 %shr, -2 ret i1 %cmp

}

;;

Function @test returns true when %B is equal to 6 (that is because -93 >> 6 = -2).

However, the Instruction Combiner wrongly thinks that there is no 'B' that solves equation: -93 >> B == -2.

Consequently, the entire body of function @test is wrongly folded into a `return i1 false`.

The problem is in the folding logic located in method InstCombiner::FoldICmpCstShrCst (InstCombineCompares.cpp) which wrongly computes the distance between two negative values.

With:

AP1 < 0 AP2 < 0 AP2 != 0 AP1 != 0 AP1 > AP2

The distance between AP2 and AP1 (i.e. the distance between the highest bits set) is wrongly computed as:

(-AP2).logBase2() - (-AP1).logBase2()

It should be computed instead taking the ones' complement of AP2 and AP1:

(~AP2).logBase2() - (~AP1).logBase2()

This patch fixes the problem with the wrong folding of icmp.

Please let me know if ok to submit.

Thanks!

Andrea