[llvm] r209746 - InstCombine: Improvement to check if signed addition overflows.

Suyog Kamal Sarda suyog.sarda at samsung.com
Tue Jun 3 07:19:42 PDT 2014


Hi Rafael,

Got the point. I Agree with your patch.
I updated my patch with the test cases. Can you please check if this is correct? 
If it looks good to you, can you please commit it on my behalf?

My patch also handles the case, where  one of the operand has only 1 bit that is not known to
be zero, but it is also not know to be one.
((~RHSKnownZero).countpopulation == 1) will be able to determine if only one of the bit is unkown (it may be 1 or 0).

As far as the test case :
 define i32 @foo(i32 %x, i32 %y) {
  %v1 = and i32 %y, 1
  %v2 = add i32 %x, %v1
  ret i32 %v2
}

We know that only one of the bit is unknown of one of the operand, it may be 1 or 0.
But for the other operand, we do not know if it has 0 at any higher bit position. Hence we do not add 'nsw' in this case.

Added this test case as well + test case to handle sext  i1(bitwidth = 1).

Awaiting for your review.

Thanks,
Suyog Sarda  


------- Original Message -------
Sender : Rafael EspĂ­ndola<rafael.espindola at gmail.com>
Date : Jun 03, 2014 06:56 (GMT+09:00)
Title : Re: Re: [llvm] r209746 - InstCombine: Improvement to check if signed addition overflows.

> I am afraid if this is correct. Its adding 'nsw' to all additions, even
> where it is sure not to add 'nsw'.
>
> ex:-
>
> int foo(short x){
> short v =  (x & ~4) ;
> int u = (int)v + 8;
> return u;
> }
>
> In this example we cannot determine if the addition will overflow of not,
> because, one of the operand (x & ~4) has 0 bit at position 2 from LSB (LSB
> position considered 0) while other operand 8 has 1 bit at position 3, which
> is at higher significant position than position of 0. In this case its
> giving IR as 'add nsw' which it should not.

Sorry, I got confused, but the code is actually correct.

In the above example we know that (int)v will be of the form 00... or
11.... since it is an sext. Now, it is being added to 8, so lets
consider both cases

00...  + 8 -> A carry can change the second 0, but not the sign bit.

11... + 8 -> A negative and a positive number are being added, no overflow.

Cheers,
Rafael
-------------- next part --------------
A non-text attachment was scrubbed...
Name: AddOverFlow_4.patch
Type: application/octet-stream
Size: 4843 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20140603/15e28484/attachment.obj>


More information about the llvm-commits mailing list