[llvm] [AArch64] Optimise materialisation of large stack offset calculations (PR #201856)

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 10 02:13:37 PDT 2026


================
@@ -7252,8 +7252,26 @@ int llvm::isAArch64FrameOffsetLegal(const MachineInstr &MI,
   if (MinOff <= NewOffset && NewOffset <= MaxOff)
     Offset = Remainder;
   else {
-    NewOffset = NewOffset < 0 ? MinOff : MaxOff;
-    Offset = Offset - (NewOffset * Scale);
+    // Try to minimise the number of instructions required to materialise the
+    // offset calculation. Specifically, for fixed offsets, if masking out the
+    // low 12 bits leaves a legal add immediate, we can realise the offset
+    // calculation with a single add instruction. Whenever this is possible,
+    // prefer this split.
+    const TargetLowering *TLI = MI.getMF()->getSubtarget().getTargetLowering();
+    int64_t HighPart = Offset & ~0xFFF;
+    int64_t LowPart = Offset & 0xFFF;
+    int64_t LowScaled = LowPart / Scale;
+    if (!IsMulVL && NewOffset >= 0 && LowPart % Scale == 0 &&
+        MinOff <= LowScaled && LowScaled <= MaxOff &&
+        TLI->isLegalAddImmediate(HighPart)) {
----------------
arsenm wrote:

I'd assume there's a helper for this that avoids going through TargetLowering 

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


More information about the llvm-commits mailing list