A lot of this is boilerplate isel; the trapping/checking lowering is the interesting bit. As a platform decision, we emit the trapping sequence by default, but this is obviously something that can be changed for other platforms (as well as based on later hardware features, most interestingly FPAC).
This also uses x16/x17 as "safe registers" purely because of another platform decision: on Darwin we have additional hardening in the kernel for these registers. We use those because 1) they're already used for interesting control flow in linker stubs, and 2) these auth/resign sequences don't have calls, so they're not clobbered by said linker stubs.
The expansion is done as late as possible (in AsmPrinter), to avoid any risks of late backend passes breaking the sequence.