Skip to content

Commit fae3111

Browse files
committed
Change stack growth direction
1 parent cefd647 commit fae3111

File tree

7 files changed

+41
-32
lines changed

7 files changed

+41
-32
lines changed

llvm/lib/Target/SBF/SBFFrameLowering.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,11 @@ class SBFSubtarget;
2020

2121
class SBFFrameLowering : public TargetFrameLowering {
2222
public:
23-
explicit SBFFrameLowering(const SBFSubtarget &sti)
24-
: TargetFrameLowering(
25-
TargetFrameLowering::StackGrowsDown,
26-
Align(64),
27-
0,
28-
Align(64)) {}
23+
explicit SBFFrameLowering(const bool hasStackFramesV3)
24+
: TargetFrameLowering(hasStackFramesV3
25+
? TargetFrameLowering::StackGrowsUp
26+
: TargetFrameLowering::StackGrowsDown,
27+
Align(64), 0, Align(64)) {}
2928

3029
void emitPrologue(MachineFunction &MF, MachineBasicBlock &MBB) const override;
3130
void emitEpilogue(MachineFunction &MF, MachineBasicBlock &MBB) const override;

llvm/lib/Target/SBF/SBFISelLowering.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -397,11 +397,16 @@ SDValue SBFTargetLowering::LowerFormalArguments(
397397
// In the new convention, arguments are in at the end of the callee
398398
// frame.
399399
uint64_t Size = VA.getLocVT().getFixedSizeInBits() / 8;
400-
int64_t Offset = -static_cast<int64_t>(VA.getLocMemOffset() + Size);
401-
int FrameIndex =
400+
int64_t Offset = static_cast<int64_t>(VA.getLocMemOffset() + Size);
401+
// Since the stack grows to the opposite direction in V3, the offset
402+
// is inverted.
403+
if (!Subtarget->getHasDynamicFramesV3())
404+
Offset = -Offset;
405+
406+
const int FrameIndex =
402407
MF.getFrameInfo().CreateFixedObject(Size, Offset, false);
403-
SDValue DstAddr = DAG.getFrameIndex(FrameIndex, PtrVT);
404-
MachinePointerInfo DstInfo =
408+
const SDValue DstAddr = DAG.getFrameIndex(FrameIndex, PtrVT);
409+
const MachinePointerInfo DstInfo =
405410
MachinePointerInfo::getFixedStack(MF, FrameIndex, Offset);
406411
SDV = DAG.getLoad(LocVT, DL, Chain, DstAddr, DstInfo);
407412
} else {
@@ -520,6 +525,11 @@ SDValue SBFTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
520525
Offset += Size;
521526
}
522527

528+
// Since the stack grows to the opposite direction in V3, the offset
529+
// is inverted.
530+
if (Subtarget->getHasDynamicFramesV3())
531+
Offset = -Offset;
532+
523533
int FrameIndex = MF.getFrameInfo().CreateFixedObject(
524534
Size, Offset, false);
525535
SBFFuncInfo->storeFrameIndexArgument(FrameIndex);

llvm/lib/Target/SBF/SBFRegisterInfo.cpp

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,9 @@ int SBFRegisterInfo::resolveInternalFrameIndex(
149149
const SBFFunctionInfo *SBFFuncInfo = MF.getInfo<SBFFunctionInfo>();
150150
int Offset = MFI.getObjectOffset(FI);
151151
const SBFSubtarget & SubTarget = MF.getSubtarget<SBFSubtarget>();
152-
uint64_t StackSize = MFI.getStackSize();
152+
const uint64_t StackSize = MFI.getStackSize();
153153

154-
if (!SubTarget.getHasDynamicFrames() &&
155-
SBFFuncInfo->containsFrameIndex(FI)) {
154+
if (!SubTarget.getHasDynamicFrames() && SBFFuncInfo->containsFrameIndex(FI)) {
156155
Offset = SBFRegisterInfo::FrameLength - Offset;
157156
if (static_cast<uint64_t>(Offset) < StackSize) {
158157
dbgs() << "Error: A function call in method "
@@ -165,12 +164,8 @@ int SBFRegisterInfo::resolveInternalFrameIndex(
165164
return -Offset;
166165
}
167166

168-
if (SubTarget.getHasDynamicFrames() &&
169-
SBFFuncInfo->containsFrameIndex(FI)) {
170-
if (SubTarget.isDynamicFramesV1())
171-
return -Offset;
172-
173-
return Offset;
167+
if (SubTarget.getHasDynamicFrames() && SBFFuncInfo->containsFrameIndex(FI)) {
168+
return -Offset;
174169
}
175170

176171
Offset += Imm.value_or(0);
@@ -180,9 +175,10 @@ int SBFRegisterInfo::resolveInternalFrameIndex(
180175
return Offset + static_cast<int>(StackSize);
181176

182177
if (SubTarget.getOptimizeStackSpace())
183-
return -(Offset + static_cast<int>(StackSize));
178+
return Offset -static_cast<int>(StackSize);
184179

185-
return -(Offset + std::max(static_cast<int>(StackSize), static_cast<int>(FrameLength)));
180+
return Offset -
181+
std::max(static_cast<int>(StackSize), static_cast<int>(FrameLength));
186182
}
187183

188184
return Offset;

llvm/lib/Target/SBF/SBFSubtarget.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,12 @@ void SBFSubtarget::initSubtargetFeatures(StringRef CPU, StringRef FS) {
6666

6767
SBFSubtarget::SBFSubtarget(const Triple &TT, const std::string &CPU,
6868
const std::string &FS, const TargetMachine &TM)
69-
: SBFGenSubtargetInfo(TT, cpuFromSubArch(TT, CPU), /*TuneCPU*/ cpuFromSubArch(TT, CPU), FS), InstrInfo(),
70-
FrameLowering(initializeSubtargetDependencies(TT, cpuFromSubArch(TT, CPU), FS)),
71-
TLInfo(TM, *this) {
69+
: SBFGenSubtargetInfo(TT, cpuFromSubArch(TT, CPU),
70+
/*TuneCPU*/ cpuFromSubArch(TT, CPU), FS),
71+
InstrInfo(), FrameLowering(initializeSubtargetDependencies(
72+
TT, cpuFromSubArch(TT, CPU), FS)
73+
.getHasDynamicFramesV3()),
74+
TLInfo(TM, *this) {
7275
assert(TT.getArch() == Triple::sbf && "expected Triple::sbf");
7376

7477
CallLoweringInfo.reset(new SBFCallLowering(*getTargetLowering()));

llvm/lib/Target/SBF/SBFSubtarget.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ class SBFSubtarget : public SBFGenSubtargetInfo {
122122
bool isDynamicFramesV1() const {
123123
return HasDynamicFrames;
124124
}
125+
bool getHasDynamicFramesV3() const { return HasDynamicFramesV3; }
125126
bool getUseDwarfRIS() const { return UseDwarfRIS; }
126127
bool getDisableNeg() const { return DisableNeg; }
127128
bool getReverseSubImm() const { return ReverseSubImm; }

llvm/test/CodeGen/SBF/many_args_new_conv.ll

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ define i32 @caller_alloca(i32 %a, i32 %b, i32 %c) #0 {
4040
; 88 is 8*7 + 32
4141

4242
; CHECK-V3: add64 r10, 64
43-
; CHECK-V3: ldxw r1, [r10 - 88]
43+
; CHECK-V3: ldxw r1, [r10 - 4104]
44+
; 4096 + 64 - 8*7 = 4104
4445

4546
; Saving arguments in the callee's frame
4647

@@ -70,7 +71,7 @@ define i32 @caller_alloca(i32 %a, i32 %b, i32 %c) #0 {
7071
; CHECK: mov64 r5, 2
7172
; CHECK: call callee_no_alloca
7273
; CHECK: ldxw r1, [r10 + 16]
73-
; CHECK-V3: ldxw r1, [r10 - 16]
74+
; CHECK-V3: ldxw r1, [r10 - 32]
7475

7576
entry:
7677
%g = alloca [4128 x i8], align 8
@@ -104,7 +105,7 @@ define i32 @callee_alloca(i32 %a, i32 %b, i32 %c, i32 %d, i32 %e, i32 %f, i32 %p
104105
; CHECK-V3: ldxw r2, [r10 - 5024]
105106
; CHECK-V3: ldxw r2, [r10 - 5016]
106107
; Loading allocated i32
107-
; CHECK-V3: ldxw r0, [r10 - 16]
108+
; CHECK-V3: ldxw r0, [r10 - 5008]
108109

109110

110111
; CHECK-NOT: add64 r10, 128

llvm/test/CodeGen/SBF/many_args_new_conv_opt.ll

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,8 @@ entry:
2727
define i32 @caller_alloca(i32 %a, i32 %b, i32 %c) #0 {
2828
; CHECK-LABEL: caller_alloca
2929
; CHECK: add64 r10, -896
30-
; CHECK: ldxw r1, [r10 - 128]
31-
; 88 is 8*7 + 32
32-
30+
; CHECK: ldxw r1, [r10 - 3144]
31+
; 4096 - 896 + 56 = 3144
3332

3433
; Saving arguments in the callee's frame
3534

@@ -47,7 +46,7 @@ define i32 @caller_alloca(i32 %a, i32 %b, i32 %c) #0 {
4746
; CHECK: mov64 r4, 1
4847
; CHECK: mov64 r5, 2
4948
; CHECK: call callee_no_alloca
50-
; CHECK: ldxw r1, [r10 - 56]
49+
; CHECK: ldxw r1, [r10 - 72]
5150

5251
entry:
5352
%g = alloca [3128 x i8], align 8
@@ -72,7 +71,7 @@ define i32 @callee_alloca(i32 %a, i32 %b, i32 %c, i32 %d, i32 %e, i32 %f, i32 %p
7271
; CHECK: ldxw r2, [r10 - 3040]
7372
; CHECK: ldxw r2, [r10 - 3032]
7473
; Loading allocated i32
75-
; CHECK-V3: ldxw r0, [r10 - 32]
74+
; CHECK-V3: ldxw r0, [r10 - 3024]
7675

7776

7877
; CHECK-NOT: add64 r10, 128

0 commit comments

Comments
 (0)