Skip to content

Commit 01a7967

Browse files
committed
[CodeGen] Replace CCState's getNextStackOffset with getStackSize (NFC)
The term "next stack offset" is misleading because the next argument is not necessarily allocated at this offset due to alignment constrains. It also does not make much sense when allocating arguments at negative offsets (introduced in a follow-up patch), because the returned offset would be past the end of the next argument. Reviewed By: arsenm Differential Revision: https://reviews.llvm.org/D149566
1 parent dc3069d commit 01a7967

34 files changed

+131
-140
lines changed

llvm/include/llvm/CodeGen/CallingConvLower.h

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ class CCState {
175175
SmallVectorImpl<CCValAssign> &Locs;
176176
LLVMContext &Context;
177177

178-
unsigned StackOffset;
178+
unsigned StackSize;
179179
Align MaxStackArgAlign;
180180
SmallVector<uint32_t, 16> UsedRegs;
181181
SmallVector<CCValAssign, 4> PendingLocs;
@@ -236,17 +236,14 @@ class CCState {
236236
CallingConv::ID getCallingConv() const { return CallingConv; }
237237
bool isVarArg() const { return IsVarArg; }
238238

239-
/// getNextStackOffset - Return the next stack offset such that all stack
240-
/// slots satisfy their alignment requirements.
241-
unsigned getNextStackOffset() const {
242-
return StackOffset;
243-
}
239+
/// Returns the size of the currently allocated portion of the stack.
240+
unsigned getStackSize() const { return StackSize; }
244241

245242
/// getAlignedCallFrameSize - Return the size of the call frame needed to
246243
/// be able to store all arguments and such that the alignment requirement
247244
/// of each of the arguments is satisfied.
248245
unsigned getAlignedCallFrameSize() const {
249-
return alignTo(StackOffset, MaxStackArgAlign);
246+
return alignTo(StackSize, MaxStackArgAlign);
250247
}
251248

252249
/// isAllocated - Return true if the specified register (or an alias) is
@@ -400,9 +397,9 @@ class CCState {
400397
/// AllocateStack - Allocate a chunk of stack space with the specified size
401398
/// and alignment.
402399
unsigned AllocateStack(unsigned Size, Align Alignment) {
403-
StackOffset = alignTo(StackOffset, Alignment);
404-
unsigned Result = StackOffset;
405-
StackOffset += Size;
400+
StackSize = alignTo(StackSize, Alignment);
401+
unsigned Result = StackSize;
402+
StackSize += Size;
406403
MaxStackArgAlign = std::max(Alignment, MaxStackArgAlign);
407404
ensureMaxAlignment(Alignment);
408405
return Result;

llvm/include/llvm/CodeGen/GlobalISel/CallLowering.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ class CallLowering {
188188
if (getAssignFn(State.isVarArg())(ValNo, ValVT, LocVT, LocInfo, Flags,
189189
State))
190190
return true;
191-
StackOffset = State.getNextStackOffset();
191+
StackSize = State.getStackSize();
192192
return false;
193193
}
194194

@@ -199,9 +199,8 @@ class CallLowering {
199199
/// as AssignFn on most targets.
200200
CCAssignFn *AssignFnVarArg;
201201

202-
/// Stack offset for next argument. At the end of argument evaluation, this
203-
/// is typically the total stack size.
204-
uint64_t StackOffset = 0;
202+
/// The size of the currently allocated portion of the stack.
203+
uint64_t StackSize = 0;
205204

206205
/// Select the appropriate assignment function depending on whether this is
207206
/// a variadic call.

llvm/lib/CodeGen/CallingConvLower.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ CCState::CCState(CallingConv::ID CC, bool isVarArg, MachineFunction &mf,
3030
: CallingConv(CC), IsVarArg(isVarArg), MF(mf),
3131
TRI(*MF.getSubtarget().getRegisterInfo()), Locs(locs), Context(C) {
3232
// No stack is used.
33-
StackOffset = 0;
33+
StackSize = 0;
3434

3535
clearByValRegsInfo();
3636
UsedRegs.resize((TRI.getNumRegs()+31)/32);
@@ -197,7 +197,7 @@ static bool isValueTypeInRegForCC(CallingConv::ID CC, MVT VT) {
197197

198198
void CCState::getRemainingRegParmsForType(SmallVectorImpl<MCPhysReg> &Regs,
199199
MVT VT, CCAssignFn Fn) {
200-
unsigned SavedStackOffset = StackOffset;
200+
unsigned SavedStackSize = StackSize;
201201
Align SavedMaxStackArgAlign = MaxStackArgAlign;
202202
unsigned NumLocs = Locs.size();
203203

@@ -229,7 +229,7 @@ void CCState::getRemainingRegParmsForType(SmallVectorImpl<MCPhysReg> &Regs,
229229
// Clear the assigned values and stack memory. We leave the registers marked
230230
// as allocated so that future queries don't return the same registers, i.e.
231231
// when i64 and f64 are both passed in GPRs.
232-
StackOffset = SavedStackOffset;
232+
StackSize = SavedStackSize;
233233
MaxStackArgAlign = SavedMaxStackArgAlign;
234234
Locs.truncate(NumLocs);
235235
}

llvm/lib/Target/AArch64/AArch64FastISel.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3022,7 +3022,7 @@ bool AArch64FastISel::processCallArgs(CallLoweringInfo &CLI,
30223022
CCInfo.AnalyzeCallOperands(OutVTs, CLI.OutFlags, CCAssignFnForCall(CC));
30233023

30243024
// Get a count of how many bytes are to be pushed on the stack.
3025-
NumBytes = CCInfo.getNextStackOffset();
3025+
NumBytes = CCInfo.getStackSize();
30263026

30273027
// Issue CALLSEQ_START
30283028
unsigned AdjStackDown = TII.getCallFrameSetupOpcode();

llvm/lib/Target/AArch64/AArch64ISelLowering.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6560,11 +6560,12 @@ SDValue AArch64TargetLowering::LowerFormalArguments(
65606560
}
65616561

65626562
// This will point to the next argument passed via stack.
6563-
unsigned StackOffset = CCInfo.getNextStackOffset();
6563+
unsigned VarArgsOffset = CCInfo.getStackSize();
65646564
// We currently pass all varargs at 8-byte alignment, or 4 for ILP32
6565-
StackOffset = alignTo(StackOffset, Subtarget->isTargetILP32() ? 4 : 8);
6566-
FuncInfo->setVarArgsStackOffset(StackOffset);
6567-
FuncInfo->setVarArgsStackIndex(MFI.CreateFixedObject(4, StackOffset, true));
6565+
VarArgsOffset = alignTo(VarArgsOffset, Subtarget->isTargetILP32() ? 4 : 8);
6566+
FuncInfo->setVarArgsStackOffset(VarArgsOffset);
6567+
FuncInfo->setVarArgsStackIndex(
6568+
MFI.CreateFixedObject(4, VarArgsOffset, true));
65686569

65696570
if (MFI.hasMustTailInVarArgFunc()) {
65706571
SmallVector<MVT, 2> RegParmTypes;
@@ -6604,7 +6605,7 @@ SDValue AArch64TargetLowering::LowerFormalArguments(
66046605
}
66056606
}
66066607

6607-
unsigned StackArgSize = CCInfo.getNextStackOffset();
6608+
unsigned StackArgSize = CCInfo.getStackSize();
66086609
bool TailCallOpt = MF.getTarget().Options.GuaranteedTailCallOpt;
66096610
if (DoesCalleeRestoreStack(CallConv, TailCallOpt)) {
66106611
// This is a non-standard ABI so by fiat I say we're allowed to make full
@@ -7001,7 +7002,7 @@ bool AArch64TargetLowering::isEligibleForTailCallOptimization(
70017002

70027003
// If the stack arguments for this call do not fit into our own save area then
70037004
// the call cannot be made tail.
7004-
if (CCInfo.getNextStackOffset() > FuncInfo->getBytesInStackArgArea())
7005+
if (CCInfo.getStackSize() > FuncInfo->getBytesInStackArgArea())
70057006
return false;
70067007

70077008
const MachineRegisterInfo &MRI = MF.getRegInfo();
@@ -7165,7 +7166,7 @@ AArch64TargetLowering::LowerCall(CallLoweringInfo &CLI,
71657166
"site marked musttail");
71667167

71677168
// Get a count of how many bytes are to be pushed on the stack.
7168-
unsigned NumBytes = CCInfo.getNextStackOffset();
7169+
unsigned NumBytes = CCInfo.getStackSize();
71697170

71707171
if (IsSibCall) {
71717172
// Since we're not changing the ABI to make this a tail call, the memory

llvm/lib/Target/AArch64/GISel/AArch64CallLowering.cpp

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ struct AArch64OutgoingValueAssigner
124124
} else
125125
Res = AssignFnVarArg(ValNo, ValVT, LocVT, LocInfo, Flags, State);
126126

127-
StackOffset = State.getNextStackOffset();
127+
StackSize = State.getStackSize();
128128
return Res;
129129
}
130130
};
@@ -706,7 +706,7 @@ bool AArch64CallLowering::lowerFormalArguments(
706706
}
707707

708708
AArch64FunctionInfo *FuncInfo = MF.getInfo<AArch64FunctionInfo>();
709-
uint64_t StackOffset = Assigner.StackOffset;
709+
uint64_t StackSize = Assigner.StackSize;
710710
if (F.isVarArg()) {
711711
if ((!Subtarget.isTargetDarwin() && !Subtarget.isWindowsArm64EC()) || IsWin64) {
712712
// The AAPCS variadic function ABI is identical to the non-variadic
@@ -720,22 +720,21 @@ bool AArch64CallLowering::lowerFormalArguments(
720720
}
721721

722722
// We currently pass all varargs at 8-byte alignment, or 4 in ILP32.
723-
StackOffset =
724-
alignTo(Assigner.StackOffset, Subtarget.isTargetILP32() ? 4 : 8);
723+
StackSize = alignTo(Assigner.StackSize, Subtarget.isTargetILP32() ? 4 : 8);
725724

726725
auto &MFI = MIRBuilder.getMF().getFrameInfo();
727-
FuncInfo->setVarArgsStackIndex(MFI.CreateFixedObject(4, StackOffset, true));
726+
FuncInfo->setVarArgsStackIndex(MFI.CreateFixedObject(4, StackSize, true));
728727
}
729728

730729
if (doesCalleeRestoreStack(F.getCallingConv(),
731730
MF.getTarget().Options.GuaranteedTailCallOpt)) {
732731
// We have a non-standard ABI, so why not make full use of the stack that
733732
// we're going to pop? It must be aligned to 16 B in any case.
734-
StackOffset = alignTo(StackOffset, 16);
733+
StackSize = alignTo(StackSize, 16);
735734

736735
// If we're expected to restore the stack (e.g. fastcc), then we'll be
737736
// adding a multiple of 16.
738-
FuncInfo->setArgumentStackToRestore(StackOffset);
737+
FuncInfo->setArgumentStackToRestore(StackSize);
739738

740739
// Our own callers will guarantee that the space is free by giving an
741740
// aligned value to CALLSEQ_START.
@@ -745,7 +744,7 @@ bool AArch64CallLowering::lowerFormalArguments(
745744
// will fit on the caller's stack. So, whenever we lower formal arguments,
746745
// we should keep track of this information, since we might lower a tail call
747746
// in this function later.
748-
FuncInfo->setBytesInStackArgArea(StackOffset);
747+
FuncInfo->setBytesInStackArgArea(StackSize);
749748

750749
if (Subtarget.hasCustomCallingConv())
751750
Subtarget.getRegisterInfo()->UpdateCustomCalleeSavedRegs(MF);
@@ -861,7 +860,7 @@ bool AArch64CallLowering::areCalleeOutgoingArgsTailCallable(
861860

862861
// Make sure that they can fit on the caller's stack.
863862
const AArch64FunctionInfo *FuncInfo = MF.getInfo<AArch64FunctionInfo>();
864-
if (OutInfo.getNextStackOffset() > FuncInfo->getBytesInStackArgArea()) {
863+
if (OutInfo.getStackSize() > FuncInfo->getBytesInStackArgArea()) {
865864
LLVM_DEBUG(dbgs() << "... Cannot fit call operands on caller's stack.\n");
866865
return false;
867866
}
@@ -1110,7 +1109,7 @@ bool AArch64CallLowering::lowerTailCall(
11101109

11111110
// The callee will pop the argument stack as a tail call. Thus, we must
11121111
// keep it 16-byte aligned.
1113-
NumBytes = alignTo(OutInfo.getNextStackOffset(), 16);
1112+
NumBytes = alignTo(OutInfo.getStackSize(), 16);
11141113

11151114
// FPDiff will be negative if this tail call requires more space than we
11161115
// would automatically have in our incoming argument space. Positive if we
@@ -1315,12 +1314,12 @@ bool AArch64CallLowering::lowerCall(MachineIRBuilder &MIRBuilder,
13151314
uint64_t CalleePopBytes =
13161315
doesCalleeRestoreStack(Info.CallConv,
13171316
MF.getTarget().Options.GuaranteedTailCallOpt)
1318-
? alignTo(Assigner.StackOffset, 16)
1317+
? alignTo(Assigner.StackSize, 16)
13191318
: 0;
13201319

1321-
CallSeqStart.addImm(Assigner.StackOffset).addImm(0);
1320+
CallSeqStart.addImm(Assigner.StackSize).addImm(0);
13221321
MIRBuilder.buildInstr(AArch64::ADJCALLSTACKUP)
1323-
.addImm(Assigner.StackOffset)
1322+
.addImm(Assigner.StackSize)
13241323
.addImm(CalleePopBytes);
13251324

13261325
// If Callee is a reg, since it is used by a target specific

llvm/lib/Target/AMDGPU/AMDGPUCallLowering.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,7 @@ bool AMDGPUCallLowering::lowerFormalArguments(
726726
if (!handleAssignments(Handler, SplitArgs, CCInfo, ArgLocs, B))
727727
return false;
728728

729-
uint64_t StackOffset = Assigner.StackOffset;
729+
uint64_t StackSize = Assigner.StackSize;
730730

731731
// Start adding system SGPRs.
732732
if (IsEntryFunc) {
@@ -741,7 +741,7 @@ bool AMDGPUCallLowering::lowerFormalArguments(
741741
// the caller's stack. So, whenever we lower formal arguments, we should keep
742742
// track of this information, since we might lower a tail call in this
743743
// function later.
744-
Info->setBytesInStackArgArea(StackOffset);
744+
Info->setBytesInStackArgArea(StackSize);
745745

746746
// Move back to the end of the basic block.
747747
B.setMBB(MBB);
@@ -1059,7 +1059,7 @@ bool AMDGPUCallLowering::areCalleeOutgoingArgsTailCallable(
10591059

10601060
// Make sure that they can fit on the caller's stack.
10611061
const SIMachineFunctionInfo *FuncInfo = MF.getInfo<SIMachineFunctionInfo>();
1062-
if (OutInfo.getNextStackOffset() > FuncInfo->getBytesInStackArgArea()) {
1062+
if (OutInfo.getStackSize() > FuncInfo->getBytesInStackArgArea()) {
10631063
LLVM_DEBUG(dbgs() << "... Cannot fit call operands on caller's stack.\n");
10641064
return false;
10651065
}
@@ -1230,7 +1230,7 @@ bool AMDGPUCallLowering::lowerTailCall(
12301230

12311231
// The callee will pop the argument stack as a tail call. Thus, we must
12321232
// keep it 16-byte aligned.
1233-
NumBytes = alignTo(OutInfo.getNextStackOffset(), ST.getStackAlignment());
1233+
NumBytes = alignTo(OutInfo.getStackSize(), ST.getStackAlignment());
12341234

12351235
// FPDiff will be negative if this tail call requires more space than we
12361236
// would automatically have in our incoming argument space. Positive if we
@@ -1396,7 +1396,7 @@ bool AMDGPUCallLowering::lowerCall(MachineIRBuilder &MIRBuilder,
13961396
handleImplicitCallArguments(MIRBuilder, MIB, ST, *MFI, ImplicitArgRegs);
13971397

13981398
// Get a count of how many bytes are to be pushed on the stack.
1399-
unsigned NumBytes = CCInfo.getNextStackOffset();
1399+
unsigned NumBytes = CCInfo.getStackSize();
14001400

14011401
// If Callee is a reg, since it is used by a target specific
14021402
// instruction, it must have a register class matching the

llvm/lib/Target/AMDGPU/SIISelLowering.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2658,7 +2658,7 @@ SDValue SITargetLowering::LowerFormalArguments(
26582658
DAG.getPass()->getAnalysis<AMDGPUArgumentUsageInfo>();
26592659
ArgUsageInfo.setFuncArgInfo(Fn, Info->getArgInfo());
26602660

2661-
unsigned StackArgSize = CCInfo.getNextStackOffset();
2661+
unsigned StackArgSize = CCInfo.getStackSize();
26622662
Info->setBytesInStackArgArea(StackArgSize);
26632663

26642664
return Chains.empty() ? Chain :
@@ -3114,7 +3114,7 @@ bool SITargetLowering::isEligibleForTailCallOptimization(
31143114
// If the stack arguments for this call do not fit into our own save area then
31153115
// the call cannot be made tail.
31163116
// TODO: Is this really necessary?
3117-
if (CCInfo.getNextStackOffset() > FuncInfo->getBytesInStackArgArea())
3117+
if (CCInfo.getStackSize() > FuncInfo->getBytesInStackArgArea())
31183118
return false;
31193119

31203120
const MachineRegisterInfo &MRI = MF.getRegInfo();
@@ -3221,7 +3221,7 @@ SDValue SITargetLowering::LowerCall(CallLoweringInfo &CLI,
32213221
CCInfo.AnalyzeCallOperands(Outs, AssignFn);
32223222

32233223
// Get a count of how many bytes are to be pushed on the stack.
3224-
unsigned NumBytes = CCInfo.getNextStackOffset();
3224+
unsigned NumBytes = CCInfo.getStackSize();
32253225

32263226
if (IsSibCall) {
32273227
// Since we're not changing the ABI to make this a tail call, the memory

llvm/lib/Target/ARC/ARCISelLowering.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -283,11 +283,11 @@ SDValue ARCTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
283283
// Analyze return values to determine the number of bytes of stack required.
284284
CCState RetCCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs,
285285
*DAG.getContext());
286-
RetCCInfo.AllocateStack(CCInfo.getNextStackOffset(), Align(4));
286+
RetCCInfo.AllocateStack(CCInfo.getStackSize(), Align(4));
287287
RetCCInfo.AnalyzeCallResult(Ins, RetCC_ARC);
288288

289289
// Get a count of how many bytes are to be pushed on the stack.
290-
unsigned NumBytes = RetCCInfo.getNextStackOffset();
290+
unsigned NumBytes = RetCCInfo.getStackSize();
291291

292292
Chain = DAG.getCALLSEQ_START(Chain, NumBytes, 0, dl);
293293

@@ -498,7 +498,7 @@ SDValue ARCTargetLowering::LowerCallArguments(
498498
unsigned StackSlotSize = 4;
499499

500500
if (!IsVarArg)
501-
AFI->setReturnStackOffset(CCInfo.getNextStackOffset());
501+
AFI->setReturnStackOffset(CCInfo.getStackSize());
502502

503503
// All getCopyFromReg ops must precede any getMemcpys to prevent the
504504
// scheduler clobbering a register before it has been copied.
@@ -565,7 +565,7 @@ SDValue ARCTargetLowering::LowerCallArguments(
565565
// There are (std::size(ArgRegs) - FirstVAReg) registers which
566566
// need to be saved.
567567
int VarFI = MFI.CreateFixedObject((std::size(ArgRegs) - FirstVAReg) * 4,
568-
CCInfo.getNextStackOffset(), true);
568+
CCInfo.getStackSize(), true);
569569
AFI->setVarArgsFrameIndex(VarFI);
570570
SDValue FIN = DAG.getFrameIndex(VarFI, MVT::i32);
571571
for (unsigned i = FirstVAReg; i < std::size(ArgRegs); i++) {
@@ -633,7 +633,7 @@ bool ARCTargetLowering::CanLowerReturn(
633633
CCState CCInfo(CallConv, IsVarArg, MF, RVLocs, Context);
634634
if (!CCInfo.CheckReturn(Outs, RetCC_ARC))
635635
return false;
636-
if (CCInfo.getNextStackOffset() != 0 && IsVarArg)
636+
if (CCInfo.getStackSize() != 0 && IsVarArg)
637637
return false;
638638
return true;
639639
}

llvm/lib/Target/ARM/ARMCallLowering.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -528,12 +528,10 @@ bool ARMCallLowering::lowerCall(MachineIRBuilder &MIRBuilder, CallLoweringInfo &
528528

529529
// We now know the size of the stack - update the ADJCALLSTACKDOWN
530530
// accordingly.
531-
CallSeqStart.addImm(ArgAssigner.StackOffset)
532-
.addImm(0)
533-
.add(predOps(ARMCC::AL));
531+
CallSeqStart.addImm(ArgAssigner.StackSize).addImm(0).add(predOps(ARMCC::AL));
534532

535533
MIRBuilder.buildInstr(ARM::ADJCALLSTACKUP)
536-
.addImm(ArgAssigner.StackOffset)
534+
.addImm(ArgAssigner.StackSize)
537535
.addImm(-1ULL)
538536
.add(predOps(ARMCC::AL));
539537

llvm/lib/Target/ARM/ARMCallingConv.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ static bool CC_ARM_AAPCS_Custom_Aggregate(unsigned ValNo, MVT ValVT,
241241

242242
// Register allocation failed, we'll be needing the stack
243243
unsigned Size = LocVT.getSizeInBits() / 8;
244-
if (LocVT == MVT::i32 && State.getNextStackOffset() == 0) {
244+
if (LocVT == MVT::i32 && State.getStackSize() == 0) {
245245
// If nothing else has used the stack until this point, a non-HFA aggregate
246246
// can be split between regs and stack.
247247
unsigned RegIdx = State.getFirstUnallocated(RegList);

llvm/lib/Target/ARM/ARMFastISel.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1928,7 +1928,7 @@ bool ARMFastISel::ProcessCallArgs(SmallVectorImpl<Value*> &Args,
19281928
// At the point, we are able to handle the call's arguments in fast isel.
19291929

19301930
// Get a count of how many bytes are to be pushed on the stack.
1931-
NumBytes = CCInfo.getNextStackOffset();
1931+
NumBytes = CCInfo.getStackSize();
19321932

19331933
// Issue CALLSEQ_START
19341934
unsigned AdjStackDown = TII.getCallFrameSetupOpcode();

0 commit comments

Comments
 (0)