[LLVMdev] Illegal Optimization(?): Combining empty type instances

Chris Lattner sabre at nondot.org
Tue Nov 1 16:01:01 PST 2005

On Tue, 1 Nov 2005, Evan Jones wrote:
> I think I may have found an illegal optimization in LLVM. The problem is that 
> if you have an empty type LLVM optimizes it away so it produces weird 
> results. The assembler output from llvm-gcc clearly outputs the "alloca" 
> instructions, so some other pass is removing them. I doubt if any real 
> program relies on this behaviour, and there is a trivial work around. 
> However, my program ran across this problem, so maybe it should be corrected.
> The expected output is that all four pointers will be unique and valid. With

Uniqueness is something guaranteed by C++, but not by C.  In C, empty 
structures have size = 0, which gives you funny cases like this.  Consider 
the following:

struct Empty {};
struct Empty Foo[10];

The size of both 'Empty' and 'Foo' are zero, which means that each element 
of Foo is at the same address.

In the C++ world, this sort of thing is explicitly banned: the smallest 
structure you can get with standard C++ is one byte.

> This caused a problem in my program because I was testing if the two 
> addresses were equal, and the test was always returning true. The workaround 
> is to put something in the structure so that LLVM doesn't optimize it away. 
> The question is what does the C standard require?

I believe that LLVM's behavior is correct in this case.

> I'm assuming that GCC is doing the right thing, but I could be wrong.

GCC may well be doing what you expect in this case, but that may just be a 
side effect of it not optimizing as aggressively.



More information about the llvm-dev mailing list