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 bool SystemZRegisterInfo::hasReservedCallFrame(MachineFunction &MF) const {
80 // FIXME: Should we always have reserved call frame?
81 return !MF.getFrameInfo()->hasVarSizedObjects();
84 void SystemZRegisterInfo::
85 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
86 MachineBasicBlock::iterator I) const {
87 if (!hasReservedCallFrame(MF)) {
88 assert(0 && "Not implemented yet!");
94 int SystemZRegisterInfo::getFrameIndexOffset(MachineFunction &MF, int FI) const {
95 const TargetFrameInfo &TFI = *MF.getTarget().getFrameInfo();
96 MachineFrameInfo *MFI = MF.getFrameInfo();
97 SystemZMachineFunctionInfo *SystemZMFI =
98 MF.getInfo<SystemZMachineFunctionInfo>();
99 int Offset = MFI->getObjectOffset(FI) + MFI->getOffsetAdjustment();
100 uint64_t StackSize = MFI->getStackSize();
102 // Fixed objects are really located in the "previous" frame.
104 StackSize -= SystemZMFI->getCalleeSavedFrameSize();
106 Offset += StackSize - TFI.getOffsetOfLocalArea();
108 // Skip the register save area if we generated the stack frame.
110 Offset -= TFI.getOffsetOfLocalArea();
115 void SystemZRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
116 int SPAdj, RegScavenger *RS) const {
117 assert(SPAdj == 0 && "Unxpected");
120 MachineInstr &MI = *II;
121 MachineFunction &MF = *MI.getParent()->getParent();
122 while (!MI.getOperand(i).isFI()) {
124 assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
127 int FrameIndex = MI.getOperand(i).getIndex();
129 unsigned BasePtr = (hasFP(MF) ? SystemZ::R11D : SystemZ::R15D);
131 // This must be part of a rri or ri operand memory reference. Replace the
132 // FrameIndex with base register with BasePtr. Add an offset to the
133 // displacement field.
134 MI.getOperand(i).ChangeToRegister(BasePtr, false);
136 // Offset is a 20-bit integer.
137 // FIXME: handle "too long" displacements.
138 int Offset = getFrameIndexOffset(MF, FrameIndex) + MI.getOperand(i+1).getImm();
139 MI.getOperand(i+1).ChangeToImmediate(Offset);
143 SystemZRegisterInfo::processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
144 RegScavenger *RS) const {
145 // Determine whether R15/R14 will ever be clobbered inside the function. And
146 // if yes - mark it as 'callee' saved.
147 MachineFrameInfo *FFI = MF.getFrameInfo();
150 /* FIXME: function is varargs */
151 /* FIXME: function grabs RA */
152 /* FIXME: function calls eh_return */)
153 MF.getRegInfo().setPhysRegUsed(SystemZ::R14D);
155 if (FFI->getObjectIndexEnd() != 0 || // Contains automatic variables
156 FFI->hasVarSizedObjects() // Function calls dynamic alloca's
157 /* FIXME: function is varargs */)
158 MF.getRegInfo().setPhysRegUsed(SystemZ::R15D);
161 /// emitSPUpdate - Emit a series of instructions to increment / decrement the
162 /// stack pointer by a constant value.
164 void emitSPUpdate(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
165 int64_t NumBytes, const TargetInstrInfo &TII) {
166 // FIXME: Handle different stack sizes here.
167 bool isSub = NumBytes < 0;
168 uint64_t Offset = isSub ? -NumBytes : NumBytes;
169 unsigned Opc = SystemZ::ADD64ri16;
170 uint64_t Chunk = (1LL << 15) - 1;
171 DebugLoc DL = (MBBI != MBB.end() ? MBBI->getDebugLoc() :
172 DebugLoc::getUnknownLoc());
175 uint64_t ThisVal = (Offset > Chunk) ? Chunk : Offset;
177 BuildMI(MBB, MBBI, DL, TII.get(Opc), SystemZ::R15D)
178 .addReg(SystemZ::R15D).addImm((isSub ? -(int64_t)ThisVal : ThisVal));
179 // The PSW implicit def is dead.
180 MI->getOperand(3).setIsDead();
185 void SystemZRegisterInfo::emitPrologue(MachineFunction &MF) const {
186 MachineBasicBlock &MBB = MF.front(); // Prolog goes in entry BB
187 const TargetFrameInfo &TFI = *MF.getTarget().getFrameInfo();
188 MachineFrameInfo *MFI = MF.getFrameInfo();
189 SystemZMachineFunctionInfo *SystemZMFI =
190 MF.getInfo<SystemZMachineFunctionInfo>();
191 MachineBasicBlock::iterator MBBI = MBB.begin();
192 DebugLoc DL = (MBBI != MBB.end() ? MBBI->getDebugLoc() :
193 DebugLoc::getUnknownLoc());
195 // Get the number of bytes to allocate from the FrameInfo.
196 // Note that area for callee-saved stuff is already allocated, thus we need to
197 // 'undo' the stack movement.
199 MFI->getStackSize() - SystemZMFI->getCalleeSavedFrameSize();
201 // Skip the callee-saved push instructions.
202 while (MBBI != MBB.end() &&
203 (MBBI->getOpcode() == SystemZ::MOV64mr ||
204 MBBI->getOpcode() == SystemZ::MOV64mrm))
207 if (MBBI != MBB.end())
208 DL = MBBI->getDebugLoc();
210 uint64_t NumBytes = StackSize - TFI.getOffsetOfLocalArea();
212 if (StackSize) // adjust stack pointer: R15 -= numbytes
213 emitSPUpdate(MBB, MBBI, -(int64_t)NumBytes, TII);
216 // Update R11 with the new base value...
217 BuildMI(MBB, MBBI, DL, TII.get(SystemZ::MOV64rr), SystemZ::R11D)
218 .addReg(SystemZ::R15D);
220 // Mark the FramePtr as live-in in every block except the entry.
221 for (MachineFunction::iterator I = next(MF.begin()), E = MF.end();
223 I->addLiveIn(SystemZ::R11D);
228 void SystemZRegisterInfo::emitEpilogue(MachineFunction &MF,
229 MachineBasicBlock &MBB) const {
230 const MachineFrameInfo *MFI = MF.getFrameInfo();
231 const TargetFrameInfo &TFI = *MF.getTarget().getFrameInfo();
232 MachineBasicBlock::iterator MBBI = prior(MBB.end());
233 SystemZMachineFunctionInfo *SystemZMFI =
234 MF.getInfo<SystemZMachineFunctionInfo>();
235 unsigned RetOpcode = MBBI->getOpcode();
236 DebugLoc DL = MBBI->getDebugLoc();
239 case SystemZ::RET: break; // These are ok
241 assert(0 && "Can only insert epilog into returning blocks");
244 // Get the number of bytes to allocate from the FrameInfo
245 // Note that area for callee-saved stuff is already allocated, thus we need to
246 // 'undo' the stack movement.
248 MFI->getStackSize() - SystemZMFI->getCalleeSavedFrameSize();
249 uint64_t NumBytes = StackSize - TFI.getOffsetOfLocalArea();
251 // Skip the final terminator instruction.
252 while (MBBI != MBB.begin()) {
253 MachineBasicBlock::iterator PI = prior(MBBI);
255 if (!PI->getDesc().isTerminator())
259 // During callee-saved restores emission stack frame was not yet finialized
260 // (and thus - the stack size was unknown). Tune the offset having full stack
262 if (SystemZMFI->getCalleeSavedFrameSize()) {
263 assert((MBBI->getOpcode() == SystemZ::MOV64rmm ||
264 MBBI->getOpcode() == SystemZ::MOV64rm) &&
265 "Expected to see callee-save register restore code");
268 MachineInstr &MI = *MBBI;
269 while (!MI.getOperand(i).isImm()) {
271 assert(i < MI.getNumOperands() && "Unexpected restore code!");
274 MI.getOperand(i).ChangeToImmediate(NumBytes + MI.getOperand(i).getImm());
278 unsigned SystemZRegisterInfo::getRARegister() const {
279 assert(0 && "What is the return address register");
283 unsigned SystemZRegisterInfo::getFrameRegister(MachineFunction &MF) const {
284 assert(0 && "What is the frame register");
288 unsigned SystemZRegisterInfo::getEHExceptionRegister() const {
289 assert(0 && "What is the exception register");
293 unsigned SystemZRegisterInfo::getEHHandlerRegister() const {
294 assert(0 && "What is the exception handler register");
298 int SystemZRegisterInfo::getDwarfRegNum(unsigned RegNum, bool isEH) const {
299 assert(0 && "What is the dwarf register number");
303 #include "SystemZGenRegisterInfo.inc"