[SPARC] Fix stupid oversight in stack realignment support.
authorJames Y Knight <jyknight@google.com>
Wed, 26 Aug 2015 17:57:51 +0000 (17:57 +0000)
committerJames Y Knight <jyknight@google.com>
Wed, 26 Aug 2015 17:57:51 +0000 (17:57 +0000)
If you're going to realign %sp to get object alignment properly (which
the code does), and stack offsets and alignments are calculated going
down from %fp (which they are), then the total stack size had better
be a multiple of the alignment. LLVM did indeed ensure that.

And then, after aligning, the sparc frame code added 96 (for sparcv8)
to the frame size, making any requested alignment of 64-bytes or
higher *guaranteed* to be misaligned. The test case added with r245668
even tests this exact scenario, and asserted the incorrect behavior,
which I somehow failed to notice. D'oh.

This change fixes the frame lowering code to align the stack size
*after* adding the spill area, instead.

Differential Revision: http://reviews.llvm.org/D12349

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@246042 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Target/Sparc/SparcFrameLowering.cpp
lib/Target/Sparc/SparcFrameLowering.h
lib/Target/Sparc/SparcSubtarget.cpp
test/CodeGen/SPARC/stack-align.ll

index 5ce51cfcabfc7f04c54894cdf80995c3169afd62..b92450962f0b587d4476fb2fd0e2f54472ca7ed6 100644 (file)
@@ -118,8 +118,37 @@ void SparcFrameLowering::emitPrologue(MachineFunction &MF,
     SAVErr = SP::ADDrr;
   }
 
+  // The SPARC ABI is a bit odd in that it requires a reserved 92-byte
+  // (128 in v9) area in the user's stack, starting at %sp. Thus, the
+  // first part of the stack that can actually be used is located at
+  // %sp + 92.
+  //
+  // We therefore need to add that offset to the total stack size
+  // after all the stack objects are placed by
+  // PrologEpilogInserter calculateFrameObjectOffsets. However, since the stack needs to be
+  // aligned *after* the extra size is added, we need to disable
+  // calculateFrameObjectOffsets's built-in stack alignment, by having
+  // targetHandlesStackFrameRounding return true.
+
+
+  // Add the extra call frame stack size, if needed. (This is the same
+  // code as in PrologEpilogInserter, but also gets disabled by
+  // targetHandlesStackFrameRounding)
+  if (MFI->adjustsStack() && hasReservedCallFrame(MF))
+    NumBytes += MFI->getMaxCallFrameSize();
+
+  // Adds the SPARC subtarget-specific spill area to the stack
+  // size. Also ensures target-required alignment.
   NumBytes = MF.getSubtarget<SparcSubtarget>().getAdjustedFrameSize(NumBytes);
-  MFI->setStackSize(NumBytes); // Update stack size with corrected value.
+
+  // Finally, ensure that the size is sufficiently aligned for the
+  // data on the stack.
+  if (MFI->getMaxAlignment() > 0) {
+    NumBytes = RoundUpToAlignment(NumBytes, MFI->getMaxAlignment());
+  }
+
+  // Update stack size with corrected value.
+  MFI->setStackSize(NumBytes);
 
   emitSPAdjustment(MF, MBB, MBBI, -NumBytes, SAVErr, SAVEri);
 
index 67ef3084ab00aea715148c6e1222e3c0d80123ff..cbb4dc04fc236212ac222ac7a57606aaaee2de7a 100644 (file)
@@ -41,6 +41,12 @@ public:
 
   int getFrameIndexReference(const MachineFunction &MF, int FI,
                              unsigned &FrameReg) const override;
+
+  /// targetHandlesStackFrameRounding - Returns true if the target is
+  /// responsible for rounding up the stack frame (probably at emitPrologue
+  /// time).
+  bool targetHandlesStackFrameRounding() const override { return true; }
+
 private:
   // Remap input registers to output registers for leaf procedure.
   void remapRegsForLeafProc(MachineFunction &MF) const;
index d69da409e428d5385dd8303bf9f51ab5aad89578..6bf8d41306263449b5e5499b7a0f43a3bc498296 100644 (file)
@@ -64,7 +64,7 @@ int SparcSubtarget::getAdjustedFrameSize(int frameSize) const {
     frameSize += 128;
     // Frames with calls must also reserve space for 6 outgoing arguments
     // whether they are used or not. LowerCall_64 takes care of that.
-    assert(frameSize % 16 == 0 && "Stack size not 16-byte aligned");
+    frameSize = RoundUpToAlignment(frameSize, 16);
   } else {
     // Emit the correct save instruction based on the number of bytes in
     // the frame. Minimum stack frame size according to V8 ABI is:
index acdb6ecaf502b05c6d7f4ee63458d9ff861481d1..2554ee821fcd27638a3a204b3e775247e426a1d4 100644 (file)
@@ -12,7 +12,7 @@ declare void @stack_realign_helper(i32 %a, i32* %b)
 ;; CHECK:      andn %sp, 63, %sp
 ;; CHECK-NEXT: ld [%fp+92], %o0
 ;; CHECK-NEXT: call stack_realign_helper
-;; CHECK-NEXT: add %sp, 96, %o1
+;; CHECK-NEXT: add %sp, 128, %o1
 
 define void @stack_realign(i32 %a, i32 %b, i32 %c, i32 %d, i32 %e, i32 %f, i32 %g) {
 entry: