Skip to content

Commit 417aa76

Browse files
committed
[AIX] Add -msave-reg-params to save arguments to stack
In PowerPC ABI, a few initial arguments are passed through registers, but their places in parameter save area are reserved, arguments passed by memory goes after the reserved location. For debugging purpose, we may want to save copy of the pass-by-reg arguments into correct places on stack. The new option achieves by adding new function level attribute and make argument lowering part aware of it.
1 parent 4ca024c commit 417aa76

File tree

9 files changed

+925
-0
lines changed

9 files changed

+925
-0
lines changed

clang/include/clang/Basic/CodeGenOptions.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,9 @@ CODEGENOPT(ForceAAPCSBitfieldLoad, 1, 0)
427427
/// Assume that by-value parameters do not alias any other values.
428428
CODEGENOPT(PassByValueIsNoAlias, 1, 0)
429429

430+
/// Whether to store register parameters to stack.
431+
CODEGENOPT(SaveRegParams, 1, 0)
432+
430433
/// Whether to not follow the AAPCS that enforces volatile bit-field access width to be
431434
/// according to the field declaring type width.
432435
CODEGENOPT(AAPCSBitfieldWidth, 1, 1)

clang/include/clang/Driver/Options.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5065,6 +5065,11 @@ def mspe : Flag<["-"], "mspe">, Group<m_ppc_Features_Group>;
50655065
def mno_spe : Flag<["-"], "mno-spe">, Group<m_ppc_Features_Group>;
50665066
def mefpu2 : Flag<["-"], "mefpu2">, Group<m_ppc_Features_Group>;
50675067
} // let Flags = [TargetSpecific]
5068+
def msave_reg_params : Flag<["-"], "msave-reg-params">, Group<m_Group>,
5069+
Flags<[TargetSpecific]>,
5070+
Visibility<[ClangOption, CC1Option]>,
5071+
HelpText<"Save arguments passed by registers to ABI-defined stack positions">,
5072+
MarshallingInfoFlag<CodeGenOpts<"SaveRegParams">>;
50685073
def mabi_EQ_quadword_atomics : Flag<["-"], "mabi=quadword-atomics">,
50695074
Group<m_Group>, Visibility<[ClangOption, CC1Option]>,
50705075
HelpText<"Enable quadword atomics ABI on AIX (AIX PPC64 only). Uses lqarx/stqcx. instructions.">,

clang/lib/CodeGen/CGCall.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2025,6 +2025,9 @@ static void getTrivialDefaultFunctionAttributes(
20252025
FuncAttrs.addAttribute(llvm::Attribute::NoUnwind);
20262026
}
20272027

2028+
if (CodeGenOpts.SaveRegParams && !AttrOnCallSite)
2029+
FuncAttrs.addAttribute("save-reg-params");
2030+
20282031
for (StringRef Attr : CodeGenOpts.DefaultFunctionAttrs) {
20292032
StringRef Var, Value;
20302033
std::tie(Var, Value) = Attr.split('=');

clang/lib/Driver/ToolChains/AIX.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,9 @@ void AIX::addClangTargetOptions(
548548
options::OPT_mtocdata))
549549
addTocDataOptions(Args, CC1Args, getDriver());
550550

551+
if (Args.hasArg(options::OPT_msave_reg_params))
552+
CC1Args.push_back("-msave-reg-params");
553+
551554
if (Args.hasFlag(options::OPT_fxl_pragma_pack,
552555
options::OPT_fno_xl_pragma_pack, true))
553556
CC1Args.push_back("-fxl-pragma-pack");
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// RUN: %clang_cc1 -triple powerpc64-ibm-aix -emit-llvm -o - %s -msave-reg-params | FileCheck -check-prefix=SAVE %s
2+
// RUN: %clang_cc1 -triple powerpc-ibm-aix -emit-llvm -o - %s -msave-reg-params | FileCheck -check-prefix=SAVE %s
3+
// RUN: %clang_cc1 -triple powerpc64-ibm-aix -emit-llvm -o - %s | FileCheck -check-prefix=NOSAVE %s
4+
// RUN: %clang_cc1 -triple powerpc-ibm-aix -emit-llvm -o - %s | FileCheck -check-prefix=NOSAVE %s
5+
6+
void bar(int);
7+
void foo(int x) { bar(x); }
8+
9+
// SAVE: attributes #{{[0-9]+}} = { {{.+}} "save-reg-params" {{.+}} }
10+
// NOSAVE-NOT: "save-reg-params"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// RUN: %clang -### -target powerpc-ibm-aix-xcoff -msave-reg-params -c %s -o /dev/null 2>&1 | FileCheck %s
2+
// RUN: %clang -### -target powerpc64-ibm-aix-xcoff -msave-reg-params -c %s -o /dev/null 2>&1 | FileCheck %s
3+
// RUN: %clang -### -target powerpc-ibm-aix-xcoff -c %s -o /dev/null 2>&1 | FileCheck %s --check-prefix=DISABLE
4+
// RUN: %clang -### -target powerpc64-ibm-aix-xcoff -c %s -o /dev/null 2>&1 | FileCheck %s --check-prefix=DISABLE
5+
6+
// CHECK: "-msave-reg-params"
7+
// DISABLE-NOT: "-msave-reg-params"

clang/test/Driver/ppc-unsupported.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,8 @@
1616
// RUN: -c %s 2>&1 | FileCheck %s
1717
// RUN: not %clang -target powerpc-unknown-aix -mabi=quadword-atomics \
1818
// RUN: -c %s 2>&1 | FileCheck %s
19+
// RUN: not %clang -target powerpc64le-unknown-linux-gnu -msave-reg-params \
20+
// RUN: -c %s 2>&1 | FileCheck %s
21+
// RUN: not %clang -target powerpc-unknown-unknown -msave-reg-params \
22+
// RUN: -c %s 2>&1 | FileCheck %s
1923
// CHECK: unsupported option

llvm/lib/Target/PowerPC/PPCISelLowering.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7214,6 +7214,8 @@ SDValue PPCTargetLowering::LowerFormalArguments_AIX(
72147214
// Reserve space for the linkage area on the stack.
72157215
const unsigned LinkageSize = Subtarget.getFrameLowering()->getLinkageSize();
72167216
CCInfo.AllocateStack(LinkageSize, Align(PtrByteSize));
7217+
uint64_t SaveStackPos = CCInfo.getStackSize();
7218+
bool SaveParams = MF.getFunction().hasFnAttribute("save-reg-params");
72177219
CCInfo.AnalyzeFormalArguments(Ins, CC_AIX);
72187220

72197221
SmallVector<SDValue, 8> MemOps;
@@ -7232,6 +7234,27 @@ SDValue PPCTargetLowering::LowerFormalArguments_AIX(
72327234
if (VA.isMemLoc() && VA.needsCustom() && ValVT.isFloatingPoint())
72337235
continue;
72347236

7237+
if (SaveParams && VA.isRegLoc() && !Flags.isByVal() && !VA.needsCustom()) {
7238+
const TargetRegisterClass *RegClass = getRegClassForSVT(
7239+
LocVT.SimpleTy, IsPPC64, Subtarget.hasP8Vector(), Subtarget.hasVSX());
7240+
// On PPC64, debugger assumes extended 8-byte values are stored from GPR.
7241+
MVT SaveVT = RegClass == &PPC::G8RCRegClass ? MVT::i64 : LocVT;
7242+
const Register VReg = MF.addLiveIn(VA.getLocReg(), RegClass);
7243+
SDValue Parm = DAG.getCopyFromReg(Chain, dl, VReg, SaveVT);
7244+
int FI = MFI.CreateFixedObject(SaveVT.getStoreSize(), SaveStackPos, true);
7245+
SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
7246+
SDValue StoreReg = DAG.getStore(Chain, dl, Parm, FIN,
7247+
MachinePointerInfo(), Align(PtrByteSize));
7248+
SaveStackPos = alignTo(SaveStackPos + SaveVT.getStoreSize(), PtrByteSize);
7249+
MemOps.push_back(StoreReg);
7250+
}
7251+
7252+
if (SaveParams && (VA.isMemLoc() || Flags.isByVal()) && !VA.needsCustom()) {
7253+
unsigned StoreSize =
7254+
Flags.isByVal() ? Flags.getByValSize() : LocVT.getStoreSize();
7255+
SaveStackPos = alignTo(SaveStackPos + StoreSize, PtrByteSize);
7256+
}
7257+
72357258
auto HandleMemLoc = [&]() {
72367259
const unsigned LocSize = LocVT.getStoreSize();
72377260
const unsigned ValSize = ValVT.getStoreSize();

0 commit comments

Comments
 (0)