On Jan 27, 2009, at 10:47 PM, Ian Lance Taylor wrote:
Is LLVM smart enough to optimize that away, even when using shared
libraries?

Yes absolutely.  Just build with -fvisibility-hidden or use an export
map to say that my_null_pointer is not exported.  If it is static, it
will also do it at -O2.  This is not even a particularly aggressive
optimization.

Sure, but the whole idea here is for the compiler to *not* know that
the pointer has a zero value.  What I was really asking was whether
LLVM would figure that out without taking any special action to
restrict the visibility of the pointer.

I don't think that there is any smartness in assuming that. I would claim that would be outrageously unsafe and incorrect to do that, unless the optimization is done at runtime or the user provides a flag with -fwhole-program. Regardless, many people build their libraries with -fvisibility-hidden, and build settings are often not controlled by engineers hacking code. It would be fragile to assume these aren't being set.

Laurent originally asked for a safe idiom for doing this "likely to work on most compilers without exotic compile flags in 2009". The idiom won't work, because many compilers other than LLVM default to whole-program optimization at sufficiently high -O levels, and you presumably don't want to make fragile assumptions about what *other* flags are getting passed to your build. He said nothing about libraries or where the code may end up.

I'm pretty sure that shared libraries are rare on systems without mmu's anyway.

-Chris

Reply via email to