On 15.10.2008, at 3.42, Chris Lattner wrote:> > On Oct 14, 2008, at 12:52 PM, Tatu Vaajalahti wrote: > >> >> Hi, >> >> I don't know enough C to know for certain if this is a programmer or >> compiler error: > > Hi Tatu, > > With this information it is impossible to tell if it is your fault or > llvm's fault. Please file a bug with a testcase that demonstrates the > problem, thanks! > > -ChrisHi Chris, With this program llvm-gcc -O2 optimizes test2 away even though it's address is taken in program (gcc-4.2 does not, neither does llvm-gcc with -O or -O0): #include <stdio.h> static const char test1 = 'x'; static const char test2 = 'x'; int main(int argc, char **argv) { printf("%p\n", &test1); printf("%p\n", &test2); return 0; } --- Tatu Vaajalahti Tampere, Finland
> With this program llvm-gcc -O2 optimizes test2 away even though it's > address is taken in program (gcc-4.2 does not, neither does llvm-gcc > with -O or -O0):I can confirm that test2 is replaced with test1 everywhere using llvm-gcc from svn head.> #include <stdio.h> > > static const char test1 = 'x'; > static const char test2 = 'x'; > > int main(int argc, char **argv) > { > printf("%p\n", &test1); > printf("%p\n", &test2); > > return 0; > }Ciao, Duncan.
On 15.10.2008, at 13.28, Duncan Sands wrote:>> With this program llvm-gcc -O2 optimizes test2 away even though it's >> address is taken in program (gcc-4.2 does not, neither does llvm-gcc >> with -O or -O0):I guess this is a bit academic case since you can easily circumvent this by marking variables as "static const volatile" or by just not initializing the variables to the same value (or leaving the initialization out). Just wanted to report the mismatch between gcc and llvm-gcc :)> I can confirm that test2 is replaced with test1 everywhere using > llvm-gcc > from svn head. > >> #include <stdio.h> >> >> static const char test1 = 'x'; >> static const char test2 = 'x'; >> >> int main(int argc, char **argv) >> { >> printf("%p\n", &test1); >> printf("%p\n", &test2); >> >> return 0; >> } > > Ciao, > > Duncan.--- Tatu Vaajalahti Tampere, Finland
Tatu Vaajalahti wrote:> With this program llvm-gcc -O2 optimizes test2 away even though it's > address is taken in program (gcc-4.2 does not, neither does llvm-gcc > with -O or -O0): > > > #include <stdio.h> > > static const char test1 = 'x'; > static const char test2 = 'x'; > > int main(int argc, char **argv) > { > printf("%p\n", &test1); > printf("%p\n", &test2); > > return 0; > }Seems to me that it is perfectly legitimate for the compiler to fold the two char constants together. Since they are both static and const, they cannot be changed from outside the compilation unit, and the compiler sees that they are not changed within the unit. If you give test2 the value 'y', both test1 and test2 are retained. -- Pertti
On 15.10.2008, at 14.01, Pertti Kellomäki wrote:> Tatu Vaajalahti wrote: >> With this program llvm-gcc -O2 optimizes test2 away even though it's >> address is taken in program (gcc-4.2 does not, neither does llvm-gcc >> with -O or -O0): >> >> >> #include <stdio.h> >> >> static const char test1 = 'x'; >> static const char test2 = 'x'; >> >> int main(int argc, char **argv) >> { >> printf("%p\n", &test1); >> printf("%p\n", &test2); >> >> return 0; >> } > > Seems to me that it is perfectly legitimate for the compiler to fold > the > two char constants together. Since they are both static and const, > they cannot be changed from outside the compilation unit, and the > compiler sees that they are not changed within the unit.True, but note that it is the address of a variable that is used, not the value. What is more troublesome is that llvm-gcc combines these also across files with -O4: test.c: #include <stdio.h> static const char test1 = 'a'; static const char test2 = 'a'; void testf(); int main(int argc, char **argv) { printf("%p\n", &test1); printf("%p\n", &test2); testf(); return 0; } test2.c: #include <stdio.h> static const char test1 = 'a'; static const char test2 = 'a'; void testf(void) { printf("%p\n", &test1); printf("%p\n", &test2); } llvm-gcc -O3 test.c test2.c : 0x1ffd 0x1ffd 0x1ffe 0x1ffe llvm-gcc -O4 test.c test2.c : 0x1ffd 0x1ffd 0x1ffd 0x1ffd --- Tatu Vaajalahti Tampere, Finland