Skip to content

Commit 865de57

Browse files
committedJan 29, 2018
[Sparc] Account for bias in stack readjustment
Summary: This was broken long ago in D12208, which failed to account for the fact that 64-bit SPARC uses a stack bias of 2047, and it is the *unbiased* value which should be aligned, not the biased one. This was seen to be an issue with Rust. Patch by: jrtc27 (James Clarke) Reviewers: jyknight, venkatra Reviewed By: jyknight Subscribers: jacob_hansen, JDevlieghere, fhahn, fedor.sergeev, llvm-commits Differential Revision: https://reviews.llvm.org/D39425 llvm-svn: 323643
1 parent 3460957 commit 865de57

File tree

2 files changed

+35
-10
lines changed

2 files changed

+35
-10
lines changed
 

‎llvm/lib/Target/Sparc/SparcFrameLowering.cpp

+24-5
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,11 @@ void SparcFrameLowering::emitPrologue(MachineFunction &MF,
8888

8989
assert(&MF.front() == &MBB && "Shrink-wrapping not yet supported");
9090
MachineFrameInfo &MFI = MF.getFrameInfo();
91+
const SparcSubtarget &Subtarget = MF.getSubtarget<SparcSubtarget>();
9192
const SparcInstrInfo &TII =
92-
*static_cast<const SparcInstrInfo *>(MF.getSubtarget().getInstrInfo());
93+
*static_cast<const SparcInstrInfo *>(Subtarget.getInstrInfo());
9394
const SparcRegisterInfo &RegInfo =
94-
*static_cast<const SparcRegisterInfo *>(MF.getSubtarget().getRegisterInfo());
95+
*static_cast<const SparcRegisterInfo *>(Subtarget.getRegisterInfo());
9596
MachineBasicBlock::iterator MBBI = MBB.begin();
9697
// Debug location must be unknown since the first debug location is used
9798
// to determine the end of the prologue.
@@ -141,7 +142,7 @@ void SparcFrameLowering::emitPrologue(MachineFunction &MF,
141142

142143
// Adds the SPARC subtarget-specific spill area to the stack
143144
// size. Also ensures target-required alignment.
144-
NumBytes = MF.getSubtarget<SparcSubtarget>().getAdjustedFrameSize(NumBytes);
145+
NumBytes = Subtarget.getAdjustedFrameSize(NumBytes);
145146

146147
// Finally, ensure that the size is sufficiently aligned for the
147148
// data on the stack.
@@ -176,9 +177,27 @@ void SparcFrameLowering::emitPrologue(MachineFunction &MF,
176177
.addCFIIndex(CFIIndex);
177178

178179
if (NeedsStackRealignment) {
179-
// andn %o6, MaxAlign-1, %o6
180+
int64_t Bias = Subtarget.getStackPointerBias();
181+
unsigned regUnbiased;
182+
if (Bias) {
183+
// This clobbers G1 which we always know is available here.
184+
regUnbiased = SP::G1;
185+
// add %o6, BIAS, %g1
186+
BuildMI(MBB, MBBI, dl, TII.get(SP::ADDri), regUnbiased)
187+
.addReg(SP::O6).addImm(Bias);
188+
} else
189+
regUnbiased = SP::O6;
190+
191+
// andn %regUnbiased, MaxAlign-1, %regUnbiased
180192
int MaxAlign = MFI.getMaxAlignment();
181-
BuildMI(MBB, MBBI, dl, TII.get(SP::ANDNri), SP::O6).addReg(SP::O6).addImm(MaxAlign - 1);
193+
BuildMI(MBB, MBBI, dl, TII.get(SP::ANDNri), regUnbiased)
194+
.addReg(regUnbiased).addImm(MaxAlign - 1);
195+
196+
if (Bias) {
197+
// add %g1, -BIAS, %o6
198+
BuildMI(MBB, MBBI, dl, TII.get(SP::ADDri), SP::O6)
199+
.addReg(regUnbiased).addImm(-Bias);
200+
}
182201
}
183202
}
184203

‎llvm/test/CodeGen/SPARC/stack-align.ll

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
; RUN: llc -march=sparc < %s | FileCheck %s
1+
; RUN: llc -march=sparc < %s | FileCheck %s --check-prefixes=CHECK,CHECK32
2+
; RUN: llc -march=sparcv9 < %s | FileCheck %s --check-prefixes=CHECK,CHECK64
23
declare void @stack_realign_helper(i32 %a, i32* %b)
34

45
;; This is a function where we have a local variable of 64-byte
@@ -7,10 +8,15 @@ declare void @stack_realign_helper(i32 %a, i32* %b)
78
;; the argument is accessed via frame pointer not stack pointer (to %o0).
89

910
;; CHECK-LABEL: stack_realign:
10-
;; CHECK: andn %sp, 63, %sp
11-
;; CHECK-NEXT: ld [%fp+92], %o0
12-
;; CHECK-NEXT: call stack_realign_helper
13-
;; CHECK-NEXT: add %sp, 128, %o1
11+
;; CHECK32: andn %sp, 63, %sp
12+
;; CHECK32-NEXT: ld [%fp+92], %o0
13+
;; CHECK64: add %sp, 2047, %g1
14+
;; CHECK64-NEXT: andn %g1, 63, %g1
15+
;; CHECK64-NEXT: add %g1, -2047, %sp
16+
;; CHECK64-NEXT: ld [%fp+2227], %o0
17+
;; CHECK-NEXT: call stack_realign_helper
18+
;; CHECK32-NEXT: add %sp, 128, %o1
19+
;; CHECK64-NEXT: add %sp, 2239, %o1
1420

1521
define void @stack_realign(i32 %a, i32 %b, i32 %c, i32 %d, i32 %e, i32 %f, i32 %g) {
1622
entry:

0 commit comments

Comments
 (0)
Please sign in to comment.