[LLVMdev] Do we abuse the "nsw" flag

David Blaikie dblaikie at gmail.com
Mon May 6 16:59:37 PDT 2013


On Mon, May 6, 2013 at 4:55 PM, Shuxin Yang <shuxin.llvm at gmail.com> wrote:
> Hi, There:
>
>    Clang fails to compile 254.gap @ CPU2000int suite.  The symptom is that
> executable fail to run
> with reference input.
>
>    The root cause is that the compiler mistakenly optimizes expr "x * y / y"
> into x where the x*y is blindly
> flagged with nsw without any analysis.
>
>     The preproceeded code is excerpted bellow:
>
> cat -n integer.i
> ---------------------------------
> 2361 TypHandle ProdInt ( hdL, hdR )
> 2362     TypHandle hdL, hdR;
> 2363 {
> ....
> 2373     if ( (int)hdL & (int)hdR & 1 ) {
> 2374
> 2375
> 2376         i = ((int)hdL - 1) * ((int)hdR >> 1);  /* !!! No barbaric NSW,
> please !!! */
> 2377         if ( ((int)hdR >> 1) == 0 || i / ((int)hdR >> 1) ==
> ((int)hdL-1) ) {
> ---------------------------------
>
>    I catch the case where the multiplication overflow: the 1st and 2nd
> operand hold the
> value 0x4000 and 0x20000, respectively.
>
>    Dose gap source code has some "undefined" behavior according to some spec
> on this planet? Or it is compiler's fault for blindly mark any mul NSW?

Signed arithmetic overflow is undefined in C and C++.

- David



More information about the llvm-dev mailing list