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/Target/TargetFrameInfo.h"
22 #include "llvm/Target/TargetInstrInfo.h"
23 #include "llvm/Target/TargetMachine.h"
24 #include "llvm/Target/TargetOptions.h"
25 #include "llvm/ADT/BitVector.h"
28 SystemZRegisterInfo::SystemZRegisterInfo(SystemZTargetMachine &tm,
29 const TargetInstrInfo &tii)
30 : SystemZGenRegisterInfo(SystemZ::ADJCALLSTACKUP, SystemZ::ADJCALLSTACKDOWN),
35 SystemZRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {
36 static const unsigned CalleeSavedRegs[] = {
37 SystemZ::R6D, SystemZ::R7D, SystemZ::R8D, SystemZ::R9D,
38 SystemZ::R10D, SystemZ::R11D, SystemZ::R12D, SystemZ::R13D, SystemZ::R14D,
39 SystemZ::F1, SystemZ::F3, SystemZ::F5, SystemZ::F7,
43 return CalleeSavedRegs;
46 const TargetRegisterClass* const*
47 SystemZRegisterInfo::getCalleeSavedRegClasses(const MachineFunction *MF) const {
48 static const TargetRegisterClass * const CalleeSavedRegClasses[] = {
49 &SystemZ::GR64RegClass, &SystemZ::GR64RegClass,
50 &SystemZ::GR64RegClass, &SystemZ::GR64RegClass,
51 &SystemZ::GR64RegClass, &SystemZ::GR64RegClass,
52 &SystemZ::GR64RegClass, &SystemZ::GR64RegClass,
53 &SystemZ::GR64RegClass,
54 &SystemZ::FP64RegClass, &SystemZ::FP64RegClass,
55 &SystemZ::FP64RegClass, &SystemZ::FP64RegClass, 0
57 return CalleeSavedRegClasses;
60 BitVector SystemZRegisterInfo::getReservedRegs(const MachineFunction &MF) const {
61 BitVector Reserved(getNumRegs());
63 Reserved.set(SystemZ::R11D);
64 Reserved.set(SystemZ::R14D);
65 Reserved.set(SystemZ::R15D);
69 // needsFP - Return true if the specified function should have a dedicated frame
70 // pointer register. This is true if the function has variable sized allocas or
71 // if frame pointer elimination is disabled.
73 bool SystemZRegisterInfo::hasFP(const MachineFunction &MF) const {
74 const MachineFrameInfo *MFI = MF.getFrameInfo();
75 return NoFramePointerElim || MFI->hasVarSizedObjects();
78 bool SystemZRegisterInfo::hasReservedCallFrame(MachineFunction &MF) const {
79 return !MF.getFrameInfo()->hasVarSizedObjects();
82 void SystemZRegisterInfo::
83 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
84 MachineBasicBlock::iterator I) const {
85 if (!hasReservedCallFrame(MF)) {
86 assert(0 && "Not implemented yet!");
92 int SystemZRegisterInfo::getFrameIndexOffset(MachineFunction &MF, int FI) const {
93 const TargetFrameInfo &TFI = *MF.getTarget().getFrameInfo();
94 MachineFrameInfo *MFI = MF.getFrameInfo();
95 SystemZMachineFunctionInfo *SystemZMFI =
96 MF.getInfo<SystemZMachineFunctionInfo>();
97 int Offset = MFI->getObjectOffset(FI) + MFI->getOffsetAdjustment();
98 uint64_t StackSize = MFI->getStackSize();
100 // Fixed objects are really located in the "previous" frame.
102 StackSize -= SystemZMFI->getCalleeSavedFrameSize();
104 Offset += StackSize - TFI.getOffsetOfLocalArea();
106 // Skip the register save area if we generated the stack frame.
108 Offset -= TFI.getOffsetOfLocalArea();
113 void SystemZRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
114 int SPAdj, RegScavenger *RS) const {
115 assert(SPAdj == 0 && "Unxpected");
118 MachineInstr &MI = *II;
119 MachineFunction &MF = *MI.getParent()->getParent();
120 while (!MI.getOperand(i).isFI()) {
122 assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
125 int FrameIndex = MI.getOperand(i).getIndex();
127 unsigned BasePtr = (hasFP(MF) ? SystemZ::R11D : SystemZ::R15D);
129 // This must be part of a rri or ri operand memory reference. Replace the
130 // FrameIndex with base register with BasePtr. Add an offset to the
131 // displacement field.
132 MI.getOperand(i).ChangeToRegister(BasePtr, false);
134 // Offset is a 20-bit integer.
135 // FIXME: handle "too long" displacements.
136 int Offset = getFrameIndexOffset(MF, FrameIndex) + MI.getOperand(i+1).getImm();
137 MI.getOperand(i+1).ChangeToImmediate(Offset);
140 /// emitSPUpdate - Emit a series of instructions to increment / decrement the
141 /// stack pointer by a constant value.
143 void emitSPUpdate(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
144 int64_t NumBytes, const TargetInstrInfo &TII) {
145 // FIXME: Handle different stack sizes here.
146 bool isSub = NumBytes < 0;
147 uint64_t Offset = isSub ? -NumBytes : NumBytes;
148 unsigned Opc = SystemZ::ADD64ri16;
149 uint64_t Chunk = (1LL << 15) - 1;
150 DebugLoc DL = (MBBI != MBB.end() ? MBBI->getDebugLoc() :
151 DebugLoc::getUnknownLoc());
154 uint64_t ThisVal = (Offset > Chunk) ? Chunk : Offset;
156 BuildMI(MBB, MBBI, DL, TII.get(Opc), SystemZ::R15D)
157 .addReg(SystemZ::R15D).addImm((isSub ? -(int64_t)ThisVal : ThisVal));
158 // The PSW implicit def is dead.
159 MI->getOperand(3).setIsDead();
164 void SystemZRegisterInfo::emitPrologue(MachineFunction &MF) const {
165 MachineBasicBlock &MBB = MF.front(); // Prolog goes in entry BB
166 const TargetFrameInfo &TFI = *MF.getTarget().getFrameInfo();
167 MachineFrameInfo *MFI = MF.getFrameInfo();
168 SystemZMachineFunctionInfo *SystemZMFI =
169 MF.getInfo<SystemZMachineFunctionInfo>();
170 MachineBasicBlock::iterator MBBI = MBB.begin();
171 DebugLoc DL = (MBBI != MBB.end() ? MBBI->getDebugLoc() :
172 DebugLoc::getUnknownLoc());
174 // Get the number of bytes to allocate from the FrameInfo.
175 // Note that area for callee-saved stuff is already allocated, thus we need to
176 // 'undo' the stack movement.
178 MFI->getStackSize() - SystemZMFI->getCalleeSavedFrameSize();
180 // FIXME: Skip the callee-saved push instructions.
182 if (MBBI != MBB.end())
183 DL = MBBI->getDebugLoc();
185 uint64_t NumBytes = StackSize - TFI.getOffsetOfLocalArea();
187 if (StackSize) // adjust stack pointer: R15 -= numbytes
188 emitSPUpdate(MBB, MBBI, -(int64_t)NumBytes, TII);
191 // Update R11 with the new base value...
192 BuildMI(MBB, MBBI, DL, TII.get(SystemZ::MOV64rr), SystemZ::R11D)
193 .addReg(SystemZ::R15D);
195 // Mark the FramePtr as live-in in every block except the entry.
196 for (MachineFunction::iterator I = next(MF.begin()), E = MF.end();
198 I->addLiveIn(SystemZ::R11D);
203 void SystemZRegisterInfo::emitEpilogue(MachineFunction &MF,
204 MachineBasicBlock &MBB) const {
205 const MachineFrameInfo *MFI = MF.getFrameInfo();
206 const TargetFrameInfo &TFI = *MF.getTarget().getFrameInfo();
207 MachineBasicBlock::iterator MBBI = prior(MBB.end());
208 SystemZMachineFunctionInfo *SystemZMFI =
209 MF.getInfo<SystemZMachineFunctionInfo>();
210 unsigned RetOpcode = MBBI->getOpcode();
211 DebugLoc DL = MBBI->getDebugLoc();
214 case SystemZ::RET: break; // These are ok
216 assert(0 && "Can only insert epilog into returning blocks");
219 // Get the number of bytes to allocate from the FrameInfo
220 // Note that area for callee-saved stuff is already allocated, thus we need to
221 // 'undo' the stack movement.
223 MFI->getStackSize() - SystemZMFI->getCalleeSavedFrameSize();
224 uint64_t NumBytes = StackSize - TFI.getOffsetOfLocalArea();
226 // Skip the callee-saved regs load instructions.
227 MachineBasicBlock::iterator LastCSPop = MBBI;
228 while (MBBI != MBB.begin()) {
229 MachineBasicBlock::iterator PI = prior(MBBI);
230 if (!PI->getDesc().isTerminator())
235 DL = MBBI->getDebugLoc();
237 if (MFI->hasVarSizedObjects()) {
238 assert(0 && "Not implemented yet!");
240 // adjust stack pointer back: R15 += numbytes
242 emitSPUpdate(MBB, MBBI, NumBytes, TII);
246 unsigned SystemZRegisterInfo::getRARegister() const {
247 assert(0 && "What is the return address register");
251 unsigned SystemZRegisterInfo::getFrameRegister(MachineFunction &MF) const {
252 assert(0 && "What is the frame register");
256 unsigned SystemZRegisterInfo::getEHExceptionRegister() const {
257 assert(0 && "What is the exception register");
261 unsigned SystemZRegisterInfo::getEHHandlerRegister() const {
262 assert(0 && "What is the exception handler register");
266 int SystemZRegisterInfo::getDwarfRegNum(unsigned RegNum, bool isEH) const {
267 assert(0 && "What is the dwarf register number");
271 #include "SystemZGenRegisterInfo.inc"