[LLVMdev] LLVM 2.4 problem? (resend)

Tatu Vaajalahti tatuvaaj at mac.com
Wed Oct 15 05:55:17 PDT 2008


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










More information about the llvm-dev mailing list