[llvm] [AArch64][SME] Fixup ABI routine insertion points to avoid clobbering NZCV (PR #161353)

Benjamin Maxwell via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 7 03:04:35 PDT 2025


================
@@ -476,6 +491,41 @@ MachineSMEABI::assignBundleZAStates(const EdgeBundles &Bundles,
   return BundleStates;
 }
 
+std::pair<MachineBasicBlock::iterator, LiveRegs>
+MachineSMEABI::findStateChangeInsertionPoint(
+    MachineBasicBlock &MBB, const BlockInfo &Block,
+    SmallVectorImpl<InstInfo>::const_iterator Inst) {
+  LiveRegs PhysLiveRegs;
+  MachineBasicBlock::iterator InsertPt;
+  if (Inst != Block.Insts.end()) {
+    InsertPt = Inst->InsertPt;
+    PhysLiveRegs = Inst->PhysLiveRegs;
+  } else {
+    InsertPt = MBB.getFirstTerminator();
+    PhysLiveRegs = Block.PhysLiveRegsAtExit;
+  }
+  if (!(PhysLiveRegs & LiveRegs::NZCV))
+    return {InsertPt, PhysLiveRegs}; // Nothing to do (no live flags).
+  // Find the previous state change. We can not move before this point.
+  MachineBasicBlock::iterator PrevStateChangeI;
+  if (Inst == Block.Insts.begin())
+    PrevStateChangeI = MBB.begin();
+  else
+    PrevStateChangeI = std::prev(Inst)->InsertPt;
----------------
MacDue wrote:

A little confusing, but `std::prev(Inst)->InsertPt` is not the point of the previous instruction. `Inst` is a `SmallVectorImpl<InstInfo>::const_iterator`, i.e., an iterator for the list of `InstInfo` objects collected in `collectNeededZAStates()`. An `InstInfo` object is only created for instructions that need a ZA state other than "ANY". 

So, `std::prev(Inst)->InsertPt` is the first instruction before "Inst" that requires a ZA state other than "ANY", which is normally at least a few instructions away. 

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


More information about the llvm-commits mailing list