[LLVMdev] Speculative loads and alignment

Artur Pilipenko apilipenko at azulsystems.com
Fri Apr 24 09:51:08 PDT 2015


Hi,

There are several optimizations where we try to load speculatively. There are also two similar functions to determine whether it's a safe transformation:
* isSafeToLoadUnconditionally
* isSafeToSpeculativelyExecute

isSafeToLoadUnconditionally tries to take load alignment into account but fails to do this in some cases. It checks alignment for pointers derived from allocas and global variables. But in other cases it scans the local BB to see if the pointer is already being loaded or stored from/to. In the latter case there can be a load/store with an alignment smaller than requested. 

In most cases this function is used in conjunction with isDereferenceablePointer: isDereferenceablePointer || isSafeToLoadUnconditionally. So if a pointer is dereferenceable there will be no alignment check.  

isSafeToSpeculativelyExecute doesn't care about alignment at all. It only checks for pointer dereferenceability. However according to the description it must check for undefined behavior which misaligned load (pointer alignment < instruction specified alignment) is.  

Is it safe to load speculatively if we can't prove that the load is well-aligned (pointer alignment >= instruction specified alignment)? 

Thanks,
Artur



More information about the llvm-dev mailing list