[llvm] [AArch64] Resolve FIXME: Use scavengeRegisterBackwards to find the best unused register (PR #78910)

Jay Foad via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 24 02:22:02 PST 2024


================
@@ -296,17 +296,16 @@ bool AArch64SpeculationHardening::instrumentControlFlow(
     // The RegScavenger represents registers available *after* the MI
     // instruction pointed to by RS.getCurrentPosition().
     // We need to have a register that is available *before* the MI is executed.
-    if (I == MBB.begin())
+    Register TmpReg;
+    if (I == MBB.begin()) {
       RS.enterBasicBlock(MBB);
-    else
+      // Cannot scavenge backwards as we are at the start of the basic block
+      TmpReg = RS.FindUnusedReg(&AArch64::GPR64commonRegClass);
+    } else {
       RS.backward(I);
-    // FIXME: The below just finds *a* unused register. Maybe code could be
-    // optimized more if this looks for the register that isn't used for the
-    // longest time around this place, to enable more scheduling freedom. Not
-    // sure if that would actually result in a big performance difference
-    // though. Maybe RegisterScavenger::findSurvivorBackwards has some logic
-    // already to do this - but it's unclear if that could easily be used here.
-    Register TmpReg = RS.FindUnusedReg(&AArch64::GPR64commonRegClass);
+      TmpReg = RS.scavengeRegisterBackwards(AArch64::GPR64commonRegClass, I,
+                                            false, 0, false);
+    }
----------------
jayfoad wrote:

I think you can call scavengeRegisterBackwards even at the start of a block - can't you?
```
    if (I == MBB.begin())
      RS.enterBasicBlock(MBB);
    else
      RS.backward(I);
    TmpReg = RS.scavengeRegisterBackwards(AArch64::GPR64commonRegClass, I,
                                          false, 0, false);
```
The only reason this code was special casing `I == MBB.begin()` in the first place is for efficiency, not for correctness. The assumption is that `enterBasicBlock` is more efficient than `backward` which might have to step over arbitrarily many instructions.

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


More information about the llvm-commits mailing list