[llvm] [LFI][AArch64] Add guard elimination optimization (PR #204693)

Peter Smith via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 19 08:47:06 PDT 2026


================
@@ -245,14 +251,36 @@ MCRegister AArch64MCLFIRewriter::mayModifyReserved(const MCInst &Inst) const {
   return {};
 }
 
+void AArch64MCLFIRewriter::onLabel(const MCSymbol *) {
+  // Invalidate guard state since the label is a potential branch target.
+  ActiveGuard = false;
+}
+
 void AArch64MCLFIRewriter::emitInst(const MCInst &Inst, MCStreamer &Out,
                                     const MCSubtargetInfo &STI) {
+  // Invalidate the active guard if this instruction modifies the guarded
+  // register, modifies x28 itself, or may affect control flow.
+  if (ActiveGuard) {
+    const MCInstrDesc &Desc = InstInfo->get(Inst.getOpcode());
+    if (Desc.mayAffectControlFlow(Inst, *RegInfo) ||
+        mayModifyRegister(Inst, ActiveGuardReg) ||
+        mayModifyRegister(Inst, getWRegFromXReg(ActiveGuardReg)) ||
+        mayModifyRegister(Inst, LFIAddrReg))
+      ActiveGuard = false;
+  }
+
   Out.emitInstruction(Inst, STI);
 }
 
 void AArch64MCLFIRewriter::emitAddMask(MCRegister Dest, MCRegister Src,
                                        MCStreamer &Out,
                                        const MCSubtargetInfo &STI) {
+  // If x28 already holds the guarded value of Src, this guard is redundant and
+  // can be skipped.
+  if (!NoLFIGuardElim && Dest == LFIAddrReg && ActiveGuard &&
----------------
smithp35 wrote:

Could the double negative of `!NoLFIGuardElim` be replaced by `LFIGuardElim` - which defaults to true. 

https://github.com/llvm/llvm-project/pull/204693


More information about the llvm-commits mailing list