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 "SystemZMachineFunctionInfo.h"
16 #include "SystemZRegisterInfo.h"
17 #include "SystemZSubtarget.h"
18 #include "llvm/CodeGen/MachineInstrBuilder.h"
19 #include "llvm/CodeGen/MachineFrameInfo.h"
20 #include "llvm/CodeGen/MachineFunction.h"
21 #include "llvm/CodeGen/MachineRegisterInfo.h"
22 #include "llvm/Target/TargetFrameInfo.h"
23 #include "llvm/Target/TargetInstrInfo.h"
24 #include "llvm/Target/TargetMachine.h"
25 #include "llvm/Target/TargetOptions.h"
26 #include "llvm/ADT/BitVector.h"
29 SystemZRegisterInfo::SystemZRegisterInfo(SystemZTargetMachine &tm,
30 const TargetInstrInfo &tii)
31 : SystemZGenRegisterInfo(SystemZ::ADJCALLSTACKUP, SystemZ::ADJCALLSTACKDOWN),
36 SystemZRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {
37 static const unsigned CalleeSavedRegs[] = {
38 SystemZ::R6D, SystemZ::R7D, SystemZ::R8D, SystemZ::R9D,
39 SystemZ::R10D, SystemZ::R11D, SystemZ::R12D, SystemZ::R13D,
40 SystemZ::R14D, SystemZ::R15D,
41 SystemZ::F1, SystemZ::F3, SystemZ::F5, SystemZ::F7,
45 return CalleeSavedRegs;
48 const TargetRegisterClass* const*
49 SystemZRegisterInfo::getCalleeSavedRegClasses(const MachineFunction *MF) const {
50 static const TargetRegisterClass * const CalleeSavedRegClasses[] = {
51 &SystemZ::GR64RegClass, &SystemZ::GR64RegClass,
52 &SystemZ::GR64RegClass, &SystemZ::GR64RegClass,
53 &SystemZ::GR64RegClass, &SystemZ::GR64RegClass,
54 &SystemZ::GR64RegClass, &SystemZ::GR64RegClass,
55 &SystemZ::GR64RegClass, &SystemZ::GR64RegClass,
56 &SystemZ::FP64RegClass, &SystemZ::FP64RegClass,
57 &SystemZ::FP64RegClass, &SystemZ::FP64RegClass, 0
59 return CalleeSavedRegClasses;
62 BitVector SystemZRegisterInfo::getReservedRegs(const MachineFunction &MF) const {
63 BitVector Reserved(getNumRegs());
65 Reserved.set(SystemZ::R11D);
66 Reserved.set(SystemZ::R14D);
67 Reserved.set(SystemZ::R15D);
71 /// needsFP - Return true if the specified function should have a dedicated
72 /// frame pointer register. This is true if the function has variable sized
73 /// allocas or if frame pointer elimination is disabled.
74 bool SystemZRegisterInfo::hasFP(const MachineFunction &MF) const {
75 const MachineFrameInfo *MFI = MF.getFrameInfo();
76 return NoFramePointerElim || MFI->hasVarSizedObjects();
79 void SystemZRegisterInfo::
80 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
81 MachineBasicBlock::iterator I) const {
85 int SystemZRegisterInfo::getFrameIndexOffset(MachineFunction &MF, int FI) const {
86 const TargetFrameInfo &TFI = *MF.getTarget().getFrameInfo();
87 MachineFrameInfo *MFI = MF.getFrameInfo();
88 SystemZMachineFunctionInfo *SystemZMFI =
89 MF.getInfo<SystemZMachineFunctionInfo>();
90 int Offset = MFI->getObjectOffset(FI) + MFI->getOffsetAdjustment();
91 uint64_t StackSize = MFI->getStackSize();
93 // Fixed objects are really located in the "previous" frame.
95 StackSize -= SystemZMFI->getCalleeSavedFrameSize();
97 Offset += StackSize - TFI.getOffsetOfLocalArea();
99 // Skip the register save area if we generated the stack frame.
101 Offset -= TFI.getOffsetOfLocalArea();
106 void SystemZRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
107 int SPAdj, RegScavenger *RS) const {
108 assert(SPAdj == 0 && "Unxpected");
111 MachineInstr &MI = *II;
112 MachineFunction &MF = *MI.getParent()->getParent();
113 while (!MI.getOperand(i).isFI()) {
115 assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
118 int FrameIndex = MI.getOperand(i).getIndex();
120 unsigned BasePtr = (hasFP(MF) ? SystemZ::R11D : SystemZ::R15D);
122 // This must be part of a rri or ri operand memory reference. Replace the
123 // FrameIndex with base register with BasePtr. Add an offset to the
124 // displacement field.
125 MI.getOperand(i).ChangeToRegister(BasePtr, false);
127 // Offset is a 20-bit integer.
128 // FIXME: handle "too long" displacements.
129 int Offset = getFrameIndexOffset(MF, FrameIndex) + MI.getOperand(i+1).getImm();
130 MI.getOperand(i+1).ChangeToImmediate(Offset);
134 SystemZRegisterInfo::processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
135 RegScavenger *RS) const {
136 // Determine whether R15/R14 will ever be clobbered inside the function. And
137 // if yes - mark it as 'callee' saved.
138 MachineFrameInfo *FFI = MF.getFrameInfo();
141 /* FIXME: function is varargs */
142 /* FIXME: function grabs RA */
143 /* FIXME: function calls eh_return */)
144 MF.getRegInfo().setPhysRegUsed(SystemZ::R14D);
146 if (FFI->getObjectIndexEnd() != 0 || // Contains automatic variables
147 FFI->hasVarSizedObjects() // Function calls dynamic alloca's
148 /* FIXME: function is varargs */)
149 MF.getRegInfo().setPhysRegUsed(SystemZ::R15D);
152 /// emitSPUpdate - Emit a series of instructions to increment / decrement the
153 /// stack pointer by a constant value.
155 void emitSPUpdate(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
156 int64_t NumBytes, const TargetInstrInfo &TII) {
157 // FIXME: Handle different stack sizes here.
158 bool isSub = NumBytes < 0;
159 uint64_t Offset = isSub ? -NumBytes : NumBytes;
160 unsigned Opc = SystemZ::ADD64ri16;
161 uint64_t Chunk = (1LL << 15) - 1;
162 DebugLoc DL = (MBBI != MBB.end() ? MBBI->getDebugLoc() :
163 DebugLoc::getUnknownLoc());
166 uint64_t ThisVal = (Offset > Chunk) ? Chunk : Offset;
168 BuildMI(MBB, MBBI, DL, TII.get(Opc), SystemZ::R15D)
169 .addReg(SystemZ::R15D).addImm((isSub ? -(int64_t)ThisVal : ThisVal));
170 // The PSW implicit def is dead.
171 MI->getOperand(3).setIsDead();
176 void SystemZRegisterInfo::emitPrologue(MachineFunction &MF) const {
177 MachineBasicBlock &MBB = MF.front(); // Prolog goes in entry BB
178 const TargetFrameInfo &TFI = *MF.getTarget().getFrameInfo();
179 MachineFrameInfo *MFI = MF.getFrameInfo();
180 SystemZMachineFunctionInfo *SystemZMFI =
181 MF.getInfo<SystemZMachineFunctionInfo>();
182 MachineBasicBlock::iterator MBBI = MBB.begin();
183 DebugLoc DL = (MBBI != MBB.end() ? MBBI->getDebugLoc() :
184 DebugLoc::getUnknownLoc());
186 // Get the number of bytes to allocate from the FrameInfo.
187 // Note that area for callee-saved stuff is already allocated, thus we need to
188 // 'undo' the stack movement.
190 MFI->getStackSize() - SystemZMFI->getCalleeSavedFrameSize();
192 // Skip the callee-saved push instructions.
193 while (MBBI != MBB.end() &&
194 (MBBI->getOpcode() == SystemZ::MOV64mr ||
195 MBBI->getOpcode() == SystemZ::MOV64mrm))
198 if (MBBI != MBB.end())
199 DL = MBBI->getDebugLoc();
201 uint64_t NumBytes = StackSize - TFI.getOffsetOfLocalArea();
203 if (StackSize) // adjust stack pointer: R15 -= numbytes
204 emitSPUpdate(MBB, MBBI, -(int64_t)NumBytes, TII);
207 // Update R11 with the new base value...
208 BuildMI(MBB, MBBI, DL, TII.get(SystemZ::MOV64rr), SystemZ::R11D)
209 .addReg(SystemZ::R15D);
211 // Mark the FramePtr as live-in in every block except the entry.
212 for (MachineFunction::iterator I = next(MF.begin()), E = MF.end();
214 I->addLiveIn(SystemZ::R11D);
219 void SystemZRegisterInfo::emitEpilogue(MachineFunction &MF,
220 MachineBasicBlock &MBB) const {
221 const MachineFrameInfo *MFI = MF.getFrameInfo();
222 const TargetFrameInfo &TFI = *MF.getTarget().getFrameInfo();
223 MachineBasicBlock::iterator MBBI = prior(MBB.end());
224 SystemZMachineFunctionInfo *SystemZMFI =
225 MF.getInfo<SystemZMachineFunctionInfo>();
226 unsigned RetOpcode = MBBI->getOpcode();
227 DebugLoc DL = MBBI->getDebugLoc();
230 case SystemZ::RET: break; // These are ok
232 assert(0 && "Can only insert epilog into returning blocks");
235 // Get the number of bytes to allocate from the FrameInfo
236 // Note that area for callee-saved stuff is already allocated, thus we need to
237 // 'undo' the stack movement.
239 MFI->getStackSize() - SystemZMFI->getCalleeSavedFrameSize();
240 uint64_t NumBytes = StackSize - TFI.getOffsetOfLocalArea();
242 // Skip the final terminator instruction.
243 while (MBBI != MBB.begin()) {
244 MachineBasicBlock::iterator PI = prior(MBBI);
246 if (!PI->getDesc().isTerminator())
250 // During callee-saved restores emission stack frame was not yet finialized
251 // (and thus - the stack size was unknown). Tune the offset having full stack
253 if (SystemZMFI->getCalleeSavedFrameSize()) {
254 assert((MBBI->getOpcode() == SystemZ::MOV64rmm ||
255 MBBI->getOpcode() == SystemZ::MOV64rm) &&
256 "Expected to see callee-save register restore code");
259 MachineInstr &MI = *MBBI;
260 while (!MI.getOperand(i).isImm()) {
262 assert(i < MI.getNumOperands() && "Unexpected restore code!");
265 MI.getOperand(i).ChangeToImmediate(NumBytes + MI.getOperand(i).getImm());
269 unsigned SystemZRegisterInfo::getRARegister() const {
270 assert(0 && "What is the return address register");
274 unsigned SystemZRegisterInfo::getFrameRegister(MachineFunction &MF) const {
275 assert(0 && "What is the frame register");
279 unsigned SystemZRegisterInfo::getEHExceptionRegister() const {
280 assert(0 && "What is the exception register");
284 unsigned SystemZRegisterInfo::getEHHandlerRegister() const {
285 assert(0 && "What is the exception handler register");
289 int SystemZRegisterInfo::getDwarfRegNum(unsigned RegNum, bool isEH) const {
290 assert(0 && "What is the dwarf register number");
294 #include "SystemZGenRegisterInfo.inc"