[cfe-dev] Unexpected behaviour for constant expression initializer

Arnaud Allard de Grandmaison Arnaud.AllardDeGrandMaison at dibcom.com
Thu Oct 21 06:01:56 PDT 2010


Hi All,

The following reduced C testcase :
$ cat initializer.c
unsigned long ok = 8509568; // 0x81D880
unsigned long bogus = 0x800000 + 2*3780*16; // 0x81D880

when compiled with clang on my host (x86_64):
$ clang -O0 -S -emit-llvm -o initializer.ll initializer.c
$ cat initializer.ll
...
@ok = global i64 8509568, align 8                 ; <i64*> [#uses=0]
@bogus = global i64 8509568, align 8              ; <i64*> [#uses=0]
...

but when compiled for the MSP430:
$ clang -ccc-host-triple msp430-unknown-unknown -O0 -S -emit-llvm -o initializer.ll initializer.c
$ cat initializer.ll
...
@ok = global i32 8509568, align 2                 ; <i32*> [#uses=0]
@bogus = global i32 8378496, align 2              ; <i32*> [#uses=0]
...

llvm-2.7, llvm-2.8 and trunk are giving consistent results.

sizeof(unsigned long) is as least 32bits on both platforms, and the final computation values, as well as the intermediate values always fit in 32 bits. The problem here is that it seems that 2*3780*16 expression is evaluated using 16bits arithmetics, thus with an overflow.

I am not a language lawyer, but this behaviour is looking at best surprising to me.

Should I file this as a bug ?

Best regards,
--
Arnaud de Grandmaison

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20101021/005dc44f/attachment.html>


More information about the cfe-dev mailing list