[LLVMdev] loads from a null address and optimizations

Nick Lewycky nicholas at mxc.ca
Sat Sep 5 12:30:37 PDT 2009

Zoltan Varga wrote:
>                 Hi,
>   Currently, llvm treats the loads from a null address as unreachable 
> code, i.e.:
>      load i32* null
> is transformed by some optimization pass into
>     unreachable
> This presents problems in JIT compilers like mono which implement null 
> pointer checks by trapping SIGSEGV signals. It also
> looks incorrect since it changes program behavior, which might be 
> undefined in general, but it is quite well defined on unix.
> Is there a way to prevent llvm from doing this besides marking all loads 
> as volatile ?

The other way is to use a custom (ie., non-zero) address space for your 
pointers. I'm not sure what the backend will do with these, but the 
optimizers know not to treat load/store from null as special in 
alternate address spaces. You may have to teach the backend to ignore 
your addrspace though.


More information about the llvm-dev mailing list