If cfi_restore_state is inserted into an empty block placed at the end of the function, this directive would be ignored when generating asm (because of this patch https://reviews.llvm.org/D32246, but cfi_restore_state looks fine, since it does not describe any target address), resulting in a cfi_remember_state/restore_ state mismatch and then crashes in applications like bolt(assertion in BinaryBasicBlock::getCFIStateAtInstr). This patch will allow .cfi_restore_state to be put at the end of the function.
reproduce(mismatch.ll at https://reviews.llvm.org/F26307823):
llc -mtriple=aarch64 mismatch.ll -o mismatch.s
then you can find there are four cfi_remember_state but only three cfi_restore_state.
So, the whole story for this test case is that CFIFixup inserts directives
- insert .cfi_remember_state at bb.0, and insert .cfi_restore_state at bb.4
- insert .cfi_remember_state at bb.4, and insert .cfi_restore_state at bb.6
- insert .cfi_remember_state at bb.6, and insert .cfi_restore_state at bb.7
- insert .cfi_remember_state at bb.7, and insert .cfi_restore_state at bb.8
and, .cfi_restore_state at bb.8 is ignored when emitting asm and then the mismatch happens.