1 //===- SystemZRegisterInfo.cpp - SystemZ Register Information -------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the SystemZ implementation of the TargetRegisterInfo class.
12 //===----------------------------------------------------------------------===//
15 #include "SystemZInstrInfo.h"
16 #include "SystemZMachineFunctionInfo.h"
17 #include "SystemZRegisterInfo.h"
18 #include "SystemZSubtarget.h"
19 #include "llvm/CodeGen/MachineInstrBuilder.h"
20 #include "llvm/CodeGen/MachineFrameInfo.h"
21 #include "llvm/CodeGen/MachineFunction.h"
22 #include "llvm/CodeGen/MachineRegisterInfo.h"
23 #include "llvm/Target/TargetFrameInfo.h"
24 #include "llvm/Target/TargetInstrInfo.h"
25 #include "llvm/Target/TargetMachine.h"
26 #include "llvm/Target/TargetOptions.h"
27 #include "llvm/ADT/BitVector.h"
30 SystemZRegisterInfo::SystemZRegisterInfo(SystemZTargetMachine &tm,
31 const SystemZInstrInfo &tii)
32 : SystemZGenRegisterInfo(SystemZ::ADJCALLSTACKUP, SystemZ::ADJCALLSTACKDOWN),
37 SystemZRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {
38 static const unsigned CalleeSavedRegs[] = {
39 SystemZ::R6D, SystemZ::R7D, SystemZ::R8D, SystemZ::R9D,
40 SystemZ::R10D, SystemZ::R11D, SystemZ::R12D, SystemZ::R13D,
41 SystemZ::R14D, SystemZ::R15D,
42 SystemZ::F8L, SystemZ::F9L, SystemZ::F10L, SystemZ::F11L,
43 SystemZ::F12L, SystemZ::F13L, SystemZ::F14L, SystemZ::F15L,
47 return CalleeSavedRegs;
50 BitVector SystemZRegisterInfo::getReservedRegs(const MachineFunction &MF) const {
51 BitVector Reserved(getNumRegs());
53 Reserved.set(SystemZ::R11D);
54 Reserved.set(SystemZ::R14D);
55 Reserved.set(SystemZ::R15D);
59 /// needsFP - Return true if the specified function should have a dedicated
60 /// frame pointer register. This is true if the function has variable sized
61 /// allocas or if frame pointer elimination is disabled.
62 bool SystemZRegisterInfo::hasFP(const MachineFunction &MF) const {
63 const MachineFrameInfo *MFI = MF.getFrameInfo();
64 return DisableFramePointerElim(MF) || MFI->hasVarSizedObjects();
67 void SystemZRegisterInfo::
68 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
69 MachineBasicBlock::iterator I) const {
73 int SystemZRegisterInfo::getFrameIndexOffset(const MachineFunction &MF,
75 const TargetFrameInfo &TFI = *MF.getTarget().getFrameInfo();
76 const MachineFrameInfo *MFI = MF.getFrameInfo();
77 const SystemZMachineFunctionInfo *SystemZMFI =
78 MF.getInfo<SystemZMachineFunctionInfo>();
79 int Offset = MFI->getObjectOffset(FI) + MFI->getOffsetAdjustment();
80 uint64_t StackSize = MFI->getStackSize();
82 // Fixed objects are really located in the "previous" frame.
84 StackSize -= SystemZMFI->getCalleeSavedFrameSize();
86 Offset += StackSize - TFI.getOffsetOfLocalArea();
88 // Skip the register save area if we generated the stack frame.
89 if (StackSize || MFI->hasCalls())
90 Offset -= TFI.getOffsetOfLocalArea();
96 SystemZRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
97 int SPAdj, FrameIndexValue *Value,
98 RegScavenger *RS) const {
99 assert(SPAdj == 0 && "Unxpected");
102 MachineInstr &MI = *II;
103 MachineFunction &MF = *MI.getParent()->getParent();
104 while (!MI.getOperand(i).isFI()) {
106 assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
109 int FrameIndex = MI.getOperand(i).getIndex();
111 unsigned BasePtr = (hasFP(MF) ? SystemZ::R11D : SystemZ::R15D);
113 // This must be part of a rri or ri operand memory reference. Replace the
114 // FrameIndex with base register with BasePtr. Add an offset to the
115 // displacement field.
116 MI.getOperand(i).ChangeToRegister(BasePtr, false);
118 // Offset is a either 12-bit unsigned or 20-bit signed integer.
119 // FIXME: handle "too long" displacements.
120 int Offset = getFrameIndexOffset(MF, FrameIndex) + MI.getOperand(i+1).getImm();
122 // Check whether displacement is too long to fit into 12 bit zext field.
123 MI.setDesc(TII.getMemoryInstr(MI.getOpcode(), Offset));
125 MI.getOperand(i+1).ChangeToImmediate(Offset);
130 SystemZRegisterInfo::processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
131 RegScavenger *RS) const {
132 // Determine whether R15/R14 will ever be clobbered inside the function. And
133 // if yes - mark it as 'callee' saved.
134 MachineFrameInfo *FFI = MF.getFrameInfo();
135 MachineRegisterInfo &MRI = MF.getRegInfo();
137 // Check whether high FPRs are ever used, if yes - we need to save R15 as
139 static const unsigned HighFPRs[] = {
140 SystemZ::F8L, SystemZ::F9L, SystemZ::F10L, SystemZ::F11L,
141 SystemZ::F12L, SystemZ::F13L, SystemZ::F14L, SystemZ::F15L,
142 SystemZ::F8S, SystemZ::F9S, SystemZ::F10S, SystemZ::F11S,
143 SystemZ::F12S, SystemZ::F13S, SystemZ::F14S, SystemZ::F15S,
146 bool HighFPRsUsed = false;
147 for (unsigned i = 0, e = array_lengthof(HighFPRs); i != e; ++i)
148 HighFPRsUsed |= MRI.isPhysRegUsed(HighFPRs[i]);
151 /* FIXME: function is varargs */
152 /* FIXME: function grabs RA */
153 /* FIXME: function calls eh_return */
154 MRI.setPhysRegUsed(SystemZ::R14D);
158 FFI->getObjectIndexEnd() != 0 || // Contains automatic variables
159 FFI->hasVarSizedObjects() // Function calls dynamic alloca's
160 /* FIXME: function is varargs */)
161 MRI.setPhysRegUsed(SystemZ::R15D);
164 /// emitSPUpdate - Emit a series of instructions to increment / decrement the
165 /// stack pointer by a constant value.
167 void emitSPUpdate(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
168 int64_t NumBytes, const TargetInstrInfo &TII) {
169 unsigned Opc; uint64_t Chunk;
170 bool isSub = NumBytes < 0;
171 uint64_t Offset = isSub ? -NumBytes : NumBytes;
173 if (Offset >= (1LL << 15) - 1) {
174 Opc = SystemZ::ADD64ri32;
175 Chunk = (1LL << 31) - 1;
177 Opc = SystemZ::ADD64ri16;
178 Chunk = (1LL << 15) - 1;
181 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
184 uint64_t ThisVal = (Offset > Chunk) ? Chunk : Offset;
186 BuildMI(MBB, MBBI, DL, TII.get(Opc), SystemZ::R15D)
187 .addReg(SystemZ::R15D).addImm(isSub ? -ThisVal : ThisVal);
188 // The PSW implicit def is dead.
189 MI->getOperand(3).setIsDead();
194 void SystemZRegisterInfo::emitPrologue(MachineFunction &MF) const {
195 MachineBasicBlock &MBB = MF.front(); // Prolog goes in entry BB
196 const TargetFrameInfo &TFI = *MF.getTarget().getFrameInfo();
197 MachineFrameInfo *MFI = MF.getFrameInfo();
198 SystemZMachineFunctionInfo *SystemZMFI =
199 MF.getInfo<SystemZMachineFunctionInfo>();
200 MachineBasicBlock::iterator MBBI = MBB.begin();
201 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
203 // Get the number of bytes to allocate from the FrameInfo.
204 // Note that area for callee-saved stuff is already allocated, thus we need to
205 // 'undo' the stack movement.
206 uint64_t StackSize = MFI->getStackSize();
207 StackSize -= SystemZMFI->getCalleeSavedFrameSize();
209 uint64_t NumBytes = StackSize - TFI.getOffsetOfLocalArea();
211 // Skip the callee-saved push instructions.
212 while (MBBI != MBB.end() &&
213 (MBBI->getOpcode() == SystemZ::MOV64mr ||
214 MBBI->getOpcode() == SystemZ::MOV64mrm))
217 if (MBBI != MBB.end())
218 DL = MBBI->getDebugLoc();
220 // adjust stack pointer: R15 -= numbytes
221 if (StackSize || MFI->hasCalls()) {
222 assert(MF.getRegInfo().isPhysRegUsed(SystemZ::R15D) &&
223 "Invalid stack frame calculation!");
224 emitSPUpdate(MBB, MBBI, -(int64_t)NumBytes, TII);
228 // Update R11 with the new base value...
229 BuildMI(MBB, MBBI, DL, TII.get(SystemZ::MOV64rr), SystemZ::R11D)
230 .addReg(SystemZ::R15D);
232 // Mark the FramePtr as live-in in every block except the entry.
233 for (MachineFunction::iterator I = llvm::next(MF.begin()), E = MF.end();
235 I->addLiveIn(SystemZ::R11D);
240 void SystemZRegisterInfo::emitEpilogue(MachineFunction &MF,
241 MachineBasicBlock &MBB) const {
242 const MachineFrameInfo *MFI = MF.getFrameInfo();
243 const TargetFrameInfo &TFI = *MF.getTarget().getFrameInfo();
244 MachineBasicBlock::iterator MBBI = prior(MBB.end());
245 SystemZMachineFunctionInfo *SystemZMFI =
246 MF.getInfo<SystemZMachineFunctionInfo>();
247 unsigned RetOpcode = MBBI->getOpcode();
250 case SystemZ::RET: break; // These are ok
252 assert(0 && "Can only insert epilog into returning blocks");
255 // Get the number of bytes to allocate from the FrameInfo
256 // Note that area for callee-saved stuff is already allocated, thus we need to
257 // 'undo' the stack movement.
259 MFI->getStackSize() - SystemZMFI->getCalleeSavedFrameSize();
260 uint64_t NumBytes = StackSize - TFI.getOffsetOfLocalArea();
262 // Skip the final terminator instruction.
263 while (MBBI != MBB.begin()) {
264 MachineBasicBlock::iterator PI = prior(MBBI);
266 if (!PI->getDesc().isTerminator())
270 // During callee-saved restores emission stack frame was not yet finialized
271 // (and thus - the stack size was unknown). Tune the offset having full stack
273 if (StackSize || MFI->hasCalls()) {
274 assert((MBBI->getOpcode() == SystemZ::MOV64rmm ||
275 MBBI->getOpcode() == SystemZ::MOV64rm) &&
276 "Expected to see callee-save register restore code");
277 assert(MF.getRegInfo().isPhysRegUsed(SystemZ::R15D) &&
278 "Invalid stack frame calculation!");
281 MachineInstr &MI = *MBBI;
282 while (!MI.getOperand(i).isImm()) {
284 assert(i < MI.getNumOperands() && "Unexpected restore code!");
287 uint64_t Offset = NumBytes + MI.getOperand(i).getImm();
288 // If Offset does not fit into 20-bit signed displacement field we need to
289 // emit some additional code...
290 if (Offset > 524287) {
291 // Fold the displacement into load instruction as much as possible.
292 NumBytes = Offset - 524287;
294 emitSPUpdate(MBB, MBBI, NumBytes, TII);
297 MI.getOperand(i).ChangeToImmediate(Offset);
301 unsigned SystemZRegisterInfo::getRARegister() const {
302 assert(0 && "What is the return address register");
307 SystemZRegisterInfo::getFrameRegister(const MachineFunction &MF) const {
308 assert(0 && "What is the frame register");
312 unsigned SystemZRegisterInfo::getEHExceptionRegister() const {
313 assert(0 && "What is the exception register");
317 unsigned SystemZRegisterInfo::getEHHandlerRegister() const {
318 assert(0 && "What is the exception handler register");
322 int SystemZRegisterInfo::getDwarfRegNum(unsigned RegNum, bool isEH) const {
323 assert(0 && "What is the dwarf register number");
327 #include "SystemZGenRegisterInfo.inc"