[PATCH] D46777: [ARM] Back up R4 and LR if calling the stack probe function

Martin Storsjö via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri May 11 14:47:17 PDT 2018


mstorsjo created this revision.
mstorsjo added reviewers: compnerd, peter.smith, t.p.northover, efriedma.
Herald added a reviewer: javed.absar.
Herald added subscribers: chrib, kristof.beyls.

At this point in determineCalleeSaves, try to get a first estimate of the number of register that will be backed up, to estimate whether a stack probe is going to be needed later or not.

I ran into a case with a function that wouldn't otherwise back up https://reviews.llvm.org/source/lld/ at all, clobbering it with the stack probe call. And apparently one existing test case forgot to back up LR even though it calls the chkstk function within the test.


Repository:
  rL LLVM

https://reviews.llvm.org/D46777

Files:
  lib/Target/ARM/ARMFrameLowering.cpp
  test/CodeGen/ARM/Windows/chkstk-movw-movt-isel.ll


Index: test/CodeGen/ARM/Windows/chkstk-movw-movt-isel.ll
===================================================================
--- test/CodeGen/ARM/Windows/chkstk-movw-movt-isel.ll
+++ test/CodeGen/ARM/Windows/chkstk-movw-movt-isel.ll
@@ -18,7 +18,7 @@
 }
 
 ; CHECK-LABEL: isel
-; CHECK: push {r4, r5}
+; CHECK: push {r4, r5, r6, lr}
 ; CHECK: movw r12, #0
 ; CHECK: movt r12, #0
 ; CHECK: movw r4, #{{\d*}}
Index: lib/Target/ARM/ARMFrameLowering.cpp
===================================================================
--- lib/Target/ARM/ARMFrameLowering.cpp
+++ lib/Target/ARM/ARMFrameLowering.cpp
@@ -1616,9 +1616,20 @@
   // since it's not always possible to restore sp from fp in a single
   // instruction.
   // FIXME: It will be better just to find spare register here.
-  if (AFI->isThumb2Function() &&
-      (MFI.hasVarSizedObjects() || RegInfo->needsStackRealignment(MF)))
-    SavedRegs.set(ARM::R4);
+  if (AFI->isThumb2Function()) {
+    if (MFI.hasVarSizedObjects() || RegInfo->needsStackRealignment(MF))
+      SavedRegs.set(ARM::R4);
+
+    unsigned SpillEstimate = SavedRegs.count();
+    unsigned StackEstimate = MFI.estimateStackSize(MF) + 4 * SpillEstimate + 16;
+
+    // If a stack probe will be emitted, spill R4 and LR, since they are
+    // clobbered by the stack probe call.
+    if (STI.isTargetWindows() && WindowsRequiresStackProbe(MF, StackEstimate)) {
+      SavedRegs.set(ARM::R4);
+      SavedRegs.set(ARM::LR);
+    }
+  }
 
   if (AFI->isThumb1OnlyFunction()) {
     // Spill LR if Thumb1 function uses variable length argument lists.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D46777.146416.patch
Type: text/x-patch
Size: 1571 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180511/9bca97ae/attachment.bin>


More information about the llvm-commits mailing list