1//===-- SystemZFrameLowering.h - Frame lowering for SystemZ -----*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_LIB_TARGET_SYSTEMZ_SYSTEMZFRAMELOWERING_H
10#define LLVM_LIB_TARGET_SYSTEMZ_SYSTEMZFRAMELOWERING_H
11
12#include "MCTargetDesc/SystemZMCTargetDesc.h"
13#include "SystemZInstrBuilder.h"
14#include "SystemZMachineFunctionInfo.h"
15#include "llvm/ADT/IndexedMap.h"
16#include "llvm/CodeGen/TargetFrameLowering.h"
17#include "llvm/Support/TypeSize.h"
18
19namespace llvm {
20class SystemZSubtarget;
21
22class SystemZFrameLowering : public TargetFrameLowering {
23public:
24 SystemZFrameLowering(StackDirection D, Align StackAl, int LAO, Align TransAl,
25 bool StackReal, unsigned PointerSize);
26
27 static std::unique_ptr<SystemZFrameLowering>
28 create(const SystemZSubtarget &STI);
29
30 // Override TargetFrameLowering.
31 bool allocateScavengingFrameIndexesNearIncomingSP(
32 const MachineFunction &MF) const override {
33 // SystemZ wants normal register scavenging slots, as close to the stack or
34 // frame pointer as possible.
35 // The default implementation assumes an x86-like layout, where the frame
36 // pointer is at the opposite end of the frame from the stack pointer.
37 // This meant that when frame pointer elimination was disabled,
38 // the slots ended up being as close as possible to the incoming
39 // stack pointer, which is the opposite of what we want on SystemZ.
40 return false;
41 }
42
43 bool hasReservedCallFrame(const MachineFunction &MF) const override;
44
45 // Return the offset of the backchain.
46 virtual unsigned getBackchainOffset(MachineFunction &MF) const = 0;
47
48 // Return the offset of the return address.
49 virtual int getReturnAddressOffset(MachineFunction &MF) const = 0;
50
51 // Get or create the frame index of where the old frame pointer is stored.
52 virtual int getOrCreateFramePointerSaveIndex(MachineFunction &MF) const = 0;
53
54 // Return the size of a pointer (in bytes).
55 unsigned getPointerSize() const { return PointerSize; }
56
57 // Emit instructions before MBBI (in MBB) to add NumBytes to Reg.
58 void emitIncrement(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
59 const DebugLoc &DL, Register Reg, int64_t NumBytes,
60 const TargetInstrInfo *TII) const;
61
62private:
63 unsigned PointerSize;
64};
65
66class SystemZELFFrameLowering : public SystemZFrameLowering {
67 IndexedMap<unsigned> RegSpillOffsets;
68
69public:
70 SystemZELFFrameLowering(unsigned PointerSize);
71
72 // Override TargetFrameLowering.
73 bool
74 assignCalleeSavedSpillSlots(MachineFunction &MF,
75 const TargetRegisterInfo *TRI,
76 std::vector<CalleeSavedInfo> &CSI) const override;
77 void determineCalleeSaves(MachineFunction &MF, BitVector &SavedRegs,
78 RegScavenger *RS) const override;
79 bool spillCalleeSavedRegisters(MachineBasicBlock &MBB,
80 MachineBasicBlock::iterator MBBI,
81 ArrayRef<CalleeSavedInfo> CSI,
82 const TargetRegisterInfo *TRI) const override;
83 bool
84 restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
85 MachineBasicBlock::iterator MBBII,
86 MutableArrayRef<CalleeSavedInfo> CSI,
87 const TargetRegisterInfo *TRI) const override;
88 void processFunctionBeforeFrameFinalized(MachineFunction &MF,
89 RegScavenger *RS) const override;
90 void emitPrologue(MachineFunction &MF, MachineBasicBlock &MBB) const override;
91 void emitEpilogue(MachineFunction &MF, MachineBasicBlock &MBB) const override;
92 void inlineStackProbe(MachineFunction &MF,
93 MachineBasicBlock &PrologMBB) const override;
94 StackOffset getFrameIndexReference(const MachineFunction &MF, int FI,
95 Register &FrameReg) const override;
96 void
97 orderFrameObjects(const MachineFunction &MF,
98 SmallVectorImpl<int> &ObjectsToAllocate) const override;
99
100 // Return the byte offset from the incoming stack pointer of Reg's
101 // ABI-defined save slot. Return 0 if no slot is defined for Reg. Adjust
102 // the offset in case MF has packed-stack.
103 unsigned getRegSpillOffset(MachineFunction &MF, Register Reg) const;
104
105 bool usePackedStack(MachineFunction &MF) const;
106
107 // Return the offset of the backchain.
108 unsigned getBackchainOffset(MachineFunction &MF) const override {
109 // The back chain is stored topmost with packed-stack.
110 return usePackedStack(MF) ? SystemZMC::ELFCallFrameSize - 8 : 0;
111 }
112
113 // Return the offset of the return address.
114 int getReturnAddressOffset(MachineFunction &MF) const override {
115 return (usePackedStack(MF) ? -2 : 14) * getPointerSize();
116 }
117
118 // Get or create the frame index of where the old frame pointer is stored.
119 int getOrCreateFramePointerSaveIndex(MachineFunction &MF) const override;
120
121protected:
122 bool hasFPImpl(const MachineFunction &MF) const override;
123};
124
125class SystemZXPLINKFrameLowering : public SystemZFrameLowering {
126 IndexedMap<unsigned> RegSpillOffsets;
127
128public:
129 SystemZXPLINKFrameLowering(unsigned PointerSize);
130
131 bool
132 assignCalleeSavedSpillSlots(MachineFunction &MF,
133 const TargetRegisterInfo *TRI,
134 std::vector<CalleeSavedInfo> &CSI) const override;
135
136 void determineCalleeSaves(MachineFunction &MF, BitVector &SavedRegs,
137 RegScavenger *RS) const override;
138
139 bool spillCalleeSavedRegisters(MachineBasicBlock &MBB,
140 MachineBasicBlock::iterator MBBI,
141 ArrayRef<CalleeSavedInfo> CSI,
142 const TargetRegisterInfo *TRI) const override;
143
144 bool
145 restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
146 MachineBasicBlock::iterator MBBII,
147 MutableArrayRef<CalleeSavedInfo> CSI,
148 const TargetRegisterInfo *TRI) const override;
149
150 void emitEpilogue(MachineFunction &MF, MachineBasicBlock &MBB) const override;
151
152 void emitPrologue(MachineFunction &MF, MachineBasicBlock &MBB) const override;
153
154 void inlineStackProbe(MachineFunction &MF,
155 MachineBasicBlock &PrologMBB) const override;
156
157 void processFunctionBeforeFrameFinalized(MachineFunction &MF,
158 RegScavenger *RS) const override;
159
160 void determineFrameLayout(MachineFunction &MF) const;
161
162 // Return the offset of the backchain.
163 unsigned getBackchainOffset(MachineFunction &MF) const override {
164 // The back chain is always the first element of the frame.
165 return 0;
166 }
167
168 // Return the offset of the return address.
169 int getReturnAddressOffset(MachineFunction &MF) const override {
170 return 3 * getPointerSize();
171 }
172
173 // Get or create the frame index of where the old frame pointer is stored.
174 int getOrCreateFramePointerSaveIndex(MachineFunction &MF) const override;
175
176protected:
177 bool hasFPImpl(const MachineFunction &MF) const override;
178};
179} // end namespace llvm
180
181#endif
182