[llvm] [AArch64] Fix tryMergeAdjacentSTG function in PrologEpilog pass (PR #68873)

Kristof Beyls via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 23 07:17:00 PDT 2023


================
@@ -3763,6 +3774,33 @@ MachineBasicBlock::iterator tryMergeAdjacentSTG(MachineBasicBlock::iterator II,
   MachineBasicBlock::iterator InsertI = Instrs.back().MI;
   InsertI++;
 
+  // All the gathered stack tag instructions are merged and placed after
+  // last tag store in the list. The check should be made if the nzcv
+  // flag that we might be overriding will affect upcoming instructions
+  // that reads the flag(Here NZCV) or if the register is live out. If
+  // the flag is defined again we don't have this problem as the flag
+  // is anyway subject to modification.
+  // Though there could be different ways to curb this maltransformation,
+  // this seems to be simple and straight forward without changing
+  // existing algorithm.
+  // FIXME : This approach of bailing out from merge is also conservative
+  // in some ways like even if stg loops are not present after merge
+  // these checks are done (which are not needed).
+  bool modifiesFirst = false, readsFirst = false;
+  for (MachineBasicBlock::iterator I = InsertI, E = MBB->end(); I != E; I++) {
+    MachineInstr &MI = *I;
+    if (MI.readsRegister(AArch64::NZCV)) {
+      readsFirst = true;
+      break;
+    }
+    if (MI.modifiesRegister(AArch64::NZCV)) {
+      modifiesFirst = true;
+      break;
+    }
+  }
+  if (!modifiesFirst && (readsFirst || isNZCVLiveOut(*MBB)))
----------------
kbeyls wrote:

Do I understand correctly that a more accurate condition for when this transform needs to be blocked would be:
1. The last STG instruction in `Instrs` does not already clobber `NZCV`. e.g. the last STG instruction is not one of the loop instructions.
2. The merged instruction being produced does clobber `NZCV` (e.g. at least one of the "merged" instructions inserted does clobber `NZCV`)
3. `NZCV` is live at the location of the last STG instruction in `Instrs`.

Assuming the above is correct, this code basically checks the 3rd condition: is `NZCV` live at the location of the last STG instruction in `Instrs`?

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


More information about the llvm-commits mailing list