Skip to content

[SystemZ] Add backchain target-feature #71668

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions clang/test/CodeGen/SystemZ/mbackchain-4.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// RUN: %clang --target=s390x-linux -O1 -S -o - %s | FileCheck %s

__attribute__((target("backchain")))
void *foo(void) {
return __builtin_return_address(1);
}

// CHECK-LABEL: foo:
// CHECK: lg %r1, 0(%r15)
// CHECK: lg %r2, 112(%r1)
// CHECK: br %r14
5 changes: 5 additions & 0 deletions llvm/lib/Target/SystemZ/SystemZFeatures.td
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ def FeatureSoftFloat : SystemZFeature<
"Use software emulation for floating point"
>;

def FeatureBackChain : SystemZFeature<
"backchain", "BackChain", (all_of FeatureBackChain),
"Store the address of the caller's frame into the callee's stack frame"
>;

//===----------------------------------------------------------------------===//
//
// New features added in the Ninth Edition of the z/Architecture
Expand Down
20 changes: 11 additions & 9 deletions llvm/lib/Target/SystemZ/SystemZFrameLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ void SystemZELFFrameLowering::processFunctionBeforeFrameFinalized(
MachineFrameInfo &MFFrame = MF.getFrameInfo();
SystemZMachineFunctionInfo *ZFI = MF.getInfo<SystemZMachineFunctionInfo>();
MachineRegisterInfo *MRI = &MF.getRegInfo();
bool BackChain = MF.getFunction().hasFnAttribute("backchain");
bool BackChain = MF.getSubtarget<SystemZSubtarget>().hasBackChain();

if (!usePackedStack(MF) || BackChain)
// Create the incoming register save area.
Expand Down Expand Up @@ -628,7 +628,7 @@ void SystemZELFFrameLowering::emitPrologue(MachineFunction &MF,
.addImm(StackSize);
}
else {
bool StoreBackchain = MF.getFunction().hasFnAttribute("backchain");
bool StoreBackchain = MF.getSubtarget<SystemZSubtarget>().hasBackChain();
// If we need backchain, save current stack pointer. R1 is free at
// this point.
if (StoreBackchain)
Expand Down Expand Up @@ -786,7 +786,7 @@ void SystemZELFFrameLowering::inlineStackProbe(
.addMemOperand(MMO);
};

bool StoreBackchain = MF.getFunction().hasFnAttribute("backchain");
bool StoreBackchain = MF.getSubtarget<SystemZSubtarget>().hasBackChain();
if (StoreBackchain)
BuildMI(*MBB, MBBI, DL, ZII->get(SystemZ::LGR))
.addReg(SystemZ::R1D, RegState::Define).addReg(SystemZ::R15D);
Expand Down Expand Up @@ -861,8 +861,9 @@ StackOffset SystemZELFFrameLowering::getFrameIndexReference(
unsigned SystemZELFFrameLowering::getRegSpillOffset(MachineFunction &MF,
Register Reg) const {
bool IsVarArg = MF.getFunction().isVarArg();
bool BackChain = MF.getFunction().hasFnAttribute("backchain");
bool SoftFloat = MF.getSubtarget<SystemZSubtarget>().hasSoftFloat();
const SystemZSubtarget &Subtarget = MF.getSubtarget<SystemZSubtarget>();
bool BackChain = Subtarget.hasBackChain();
bool SoftFloat = Subtarget.hasSoftFloat();
unsigned Offset = RegSpillOffsets[Reg];
if (usePackedStack(MF) && !(IsVarArg && !SoftFloat)) {
if (SystemZ::GR64BitRegClass.contains(Reg))
Expand Down Expand Up @@ -890,8 +891,9 @@ int SystemZELFFrameLowering::getOrCreateFramePointerSaveIndex(

bool SystemZELFFrameLowering::usePackedStack(MachineFunction &MF) const {
bool HasPackedStackAttr = MF.getFunction().hasFnAttribute("packed-stack");
bool BackChain = MF.getFunction().hasFnAttribute("backchain");
bool SoftFloat = MF.getSubtarget<SystemZSubtarget>().hasSoftFloat();
const SystemZSubtarget &Subtarget = MF.getSubtarget<SystemZSubtarget>();
bool BackChain = Subtarget.hasBackChain();
bool SoftFloat = Subtarget.hasSoftFloat();
if (HasPackedStackAttr && BackChain && !SoftFloat)
report_fatal_error("packed-stack + backchain + hard-float is unsupported.");
bool CallConv = MF.getFunction().getCallingConv() != CallingConv::GHC;
Expand Down Expand Up @@ -946,7 +948,7 @@ static bool isXPLeafCandidate(const MachineFunction &MF) {
return false;

// If the backchain pointer should be stored, then it is not a XPLeaf routine.
if (MF.getFunction().hasFnAttribute("backchain"))
if (MF.getSubtarget<SystemZSubtarget>().hasBackChain())
return false;

// If function acquires its own stack frame, then it is not a XPLeaf routine.
Expand Down Expand Up @@ -989,7 +991,7 @@ bool SystemZXPLINKFrameLowering::assignCalleeSavedSpillSlots(

// If the function needs a frame pointer, or if the backchain pointer should
// be stored, then save the stack pointer register R4.
if (hasFP(MF) || MF.getFunction().hasFnAttribute("backchain"))
if (hasFP(MF) || Subtarget.hasBackChain())
CSI.push_back(CalleeSavedInfo(Regs.getStackPointerRegister()));

// Scan the call-saved GPRs and find the bounds of the register spill area.
Expand Down
8 changes: 4 additions & 4 deletions llvm/lib/Target/SystemZ/SystemZISelLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3631,7 +3631,7 @@ SDValue SystemZTargetLowering::lowerFRAMEADDR(SDValue Op,

if (Depth > 0) {
// FIXME The frontend should detect this case.
if (!MF.getFunction().hasFnAttribute("backchain"))
if (!MF.getSubtarget<SystemZSubtarget>().hasBackChain())
report_fatal_error("Unsupported stack frame traversal count");

SDValue Offset = DAG.getConstant(TFL->getBackchainOffset(MF), DL, PtrVT);
Expand Down Expand Up @@ -3660,7 +3660,7 @@ SDValue SystemZTargetLowering::lowerRETURNADDR(SDValue Op,

if (Depth > 0) {
// FIXME The frontend should detect this case.
if (!MF.getFunction().hasFnAttribute("backchain"))
if (!MF.getSubtarget<SystemZSubtarget>().hasBackChain())
report_fatal_error("Unsupported stack frame traversal count");

SDValue FrameAddr = lowerFRAMEADDR(Op, DAG);
Expand Down Expand Up @@ -3886,7 +3886,7 @@ SystemZTargetLowering::lowerDYNAMIC_STACKALLOC_ELF(SDValue Op,
const TargetFrameLowering *TFI = Subtarget.getFrameLowering();
MachineFunction &MF = DAG.getMachineFunction();
bool RealignOpt = !MF.getFunction().hasFnAttribute("no-realign-stack");
bool StoreBackchain = MF.getFunction().hasFnAttribute("backchain");
bool StoreBackchain = MF.getSubtarget<SystemZSubtarget>().hasBackChain();

SDValue Chain = Op.getOperand(0);
SDValue Size = Op.getOperand(1);
Expand Down Expand Up @@ -4563,7 +4563,7 @@ SDValue SystemZTargetLowering::lowerSTACKRESTORE(SDValue Op,
SelectionDAG &DAG) const {
MachineFunction &MF = DAG.getMachineFunction();
auto *Regs = Subtarget.getSpecialRegisters();
bool StoreBackchain = MF.getFunction().hasFnAttribute("backchain");
bool StoreBackchain = MF.getSubtarget<SystemZSubtarget>().hasBackChain();

if (MF.getFunction().getCallingConv() == CallingConv::GHC)
report_fatal_error("Variable-sized stack allocations are not supported "
Expand Down
8 changes: 5 additions & 3 deletions llvm/lib/Target/SystemZ/SystemZTargetMachine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,14 @@ SystemZTargetMachine::getSubtargetImpl(const Function &F) const {
FSAttr.isValid() ? FSAttr.getValueAsString().str() : TargetFS;

// FIXME: This is related to the code below to reset the target options,
// we need to know whether or not the soft float flag is set on the
// function, so we can enable it as a subtarget feature.
// we need to know whether the soft float and backchain flags are set on the
// function, so we can enable them as subtarget features.
bool SoftFloat = F.getFnAttribute("use-soft-float").getValueAsBool();

if (SoftFloat)
FS += FS.empty() ? "+soft-float" : ",+soft-float";
bool BackChain = F.hasFnAttribute("backchain");
if (BackChain)
FS += FS.empty() ? "+backchain" : ",+backchain";

auto &I = SubtargetMap[CPU + TuneCPU + FS];
if (!I) {
Expand Down