Navigation

Code generated by managed language runtimes tend to have checks that
are required for safety but never fail in practice. In such cases, it
is profitable to make the non-failing case cheaper even if it makes
the failing case significantly more expensive. This asymmetry can be
exploited by folding such safety checks into operations that can be
made to fault reliably if the check would have failed, and recovering
from such a fault by using a signal handler.

For example, Java requires null checks on objects before they are read
from or written to. If the object is null then a
NullPointerException has to be thrown, interrupting normal
execution. In practice, however, dereferencing a null pointer is
extremely rare in well-behaved Java programs, and typically the null
check can be folded into a nearby memory operation that operates on
the same memory location.

This transform happens at the MachineInstr level, not the LLVM IR
level (so the above example is only representative, not literal). The
ImplicitNullChecks pass runs during codegen, if
-enable-implicit-null-checks is passed to llc.

The ImplicitNullChecks pass adds entries to the
__llvm_faultmaps section described above as needed.

Making null checks implicit is an aggressive optimization, and it can
be a net performance pessimization if too many memory operations end
up faulting because of it. A language runtime typically needs to
ensure that only a negligible number of implicit null checks actually
fault once the application has reached a steady state. A standard way
of doing this is by healing failed implicit null checks into explicit
null checks via code patching or recompilation. It follows that there
are two requirements an explicit null check needs to satisfy for it to
be profitable to convert it to an implicit null check:

The case where the pointer is actually null (i.e. the “failing”
case) is extremely rare.

The failing path heals the implicit null check into an explicit
null check so that the application does not repeatedly page
fault.

The frontend is expected to mark branches that satisfy (1) and (2)
using a !make.implicit metadata node (the actual content of the
metadata node is ignored). Only branches that are marked with
!make.implicit metadata are considered as candidates for
conversion into implicit null checks.

(Note that while we could deal with (1) using profiling data, dealing
with (2) requires some information not present in branch profiles.)