[LLVMdev] interesting possible compiler bug

David Chisnall David.Chisnall at cl.cam.ac.uk
Tue Oct 2 00:46:30 PDT 2012

On 2 Oct 2012, at 03:40, Nick Lewycky wrote:

> As far as I know, this optimization is legal. Fix the test with a volatile pointer:

Why would that be required?  malloc() is defined by the standard to return a pointer that is distinct from any other valid pointer, or NULL.  Any optimisation that makes any assumptions about its next value is invalid.

> int main() {
>   volatile char *curr;
>   do {
>     curr = malloc(1);
>     int i = *curr;

This, in particular, looks very wrong.  If curr is void, then you are dereferencing an invalid pointer, and so you are doing something undefined.  In fact, this version of the code is completely free to elide the conditional loop, because by dereferencing the pointer you are asserting that it is not NULL (or, at least, that if it is then after this point the program is in an undefined state and so any behaviour is legal) and so it is completely free to generate the code that it in fact does generate without this test.  So here we have another bug, because the testq in your output is redundant after the movb.


More information about the llvm-dev mailing list