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 // If the stack pointer can be changed after prologue, turn the
89 // adjcallstackup instruction into a 'sub R15, <amt>' and the
90 // adjcallstackdown instruction into 'add R15, <amt>'
91 MachineInstr *Old = I;
92 uint64_t Amount = Old->getOperand(0).getImm();
94 assert((Amount == 0) && "Not implemented yet!");
100 int SystemZRegisterInfo::getFrameIndexOffset(MachineFunction &MF, int FI) const {
101 const TargetFrameInfo &TFI = *MF.getTarget().getFrameInfo();
102 MachineFrameInfo *MFI = MF.getFrameInfo();
103 SystemZMachineFunctionInfo *SystemZMFI =
104 MF.getInfo<SystemZMachineFunctionInfo>();
105 int Offset = MFI->getObjectOffset(FI) + MFI->getOffsetAdjustment();
106 uint64_t StackSize = MFI->getStackSize();
108 // Fixed objects are really located in the "previous" frame.
110 StackSize -= SystemZMFI->getCalleeSavedFrameSize();
112 Offset += StackSize - TFI.getOffsetOfLocalArea();
114 // Skip the register save area if we generated the stack frame.
116 Offset -= TFI.getOffsetOfLocalArea();
121 void SystemZRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
122 int SPAdj, RegScavenger *RS) const {
123 assert(SPAdj == 0 && "Unxpected");
126 MachineInstr &MI = *II;
127 MachineFunction &MF = *MI.getParent()->getParent();
128 while (!MI.getOperand(i).isFI()) {
130 assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
133 int FrameIndex = MI.getOperand(i).getIndex();
135 unsigned BasePtr = (hasFP(MF) ? SystemZ::R11D : SystemZ::R15D);
137 // This must be part of a rri or ri operand memory reference. Replace the
138 // FrameIndex with base register with BasePtr. Add an offset to the
139 // displacement field.
140 MI.getOperand(i).ChangeToRegister(BasePtr, false);
142 // Offset is a 20-bit integer.
143 // FIXME: handle "too long" displacements.
144 int Offset = getFrameIndexOffset(MF, FrameIndex) + MI.getOperand(i+1).getImm();
145 MI.getOperand(i+1).ChangeToImmediate(Offset);
149 SystemZRegisterInfo::processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
150 RegScavenger *RS) const {
151 // Determine whether R15/R14 will ever be clobbered inside the function. And
152 // if yes - mark it as 'callee' saved.
153 MachineFrameInfo *FFI = MF.getFrameInfo();
156 /* FIXME: function is varargs */
157 /* FIXME: function grabs RA */
158 /* FIXME: function calls eh_return */)
159 MF.getRegInfo().setPhysRegUsed(SystemZ::R14D);
161 if (FFI->getObjectIndexEnd() != 0 || // Contains automatic variables
162 FFI->hasVarSizedObjects() // Function calls dynamic alloca's
163 /* FIXME: function is varargs */)
164 MF.getRegInfo().setPhysRegUsed(SystemZ::R15D);
167 /// emitSPUpdate - Emit a series of instructions to increment / decrement the
168 /// stack pointer by a constant value.
170 void emitSPUpdate(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
171 int64_t NumBytes, const TargetInstrInfo &TII) {
172 // FIXME: Handle different stack sizes here.
173 bool isSub = NumBytes < 0;
174 uint64_t Offset = isSub ? -NumBytes : NumBytes;
175 unsigned Opc = SystemZ::ADD64ri16;
176 uint64_t Chunk = (1LL << 15) - 1;
177 DebugLoc DL = (MBBI != MBB.end() ? MBBI->getDebugLoc() :
178 DebugLoc::getUnknownLoc());
181 uint64_t ThisVal = (Offset > Chunk) ? Chunk : Offset;
183 BuildMI(MBB, MBBI, DL, TII.get(Opc), SystemZ::R15D)
184 .addReg(SystemZ::R15D).addImm((isSub ? -(int64_t)ThisVal : ThisVal));
185 // The PSW implicit def is dead.
186 MI->getOperand(3).setIsDead();
191 void SystemZRegisterInfo::emitPrologue(MachineFunction &MF) const {
192 MachineBasicBlock &MBB = MF.front(); // Prolog goes in entry BB
193 const TargetFrameInfo &TFI = *MF.getTarget().getFrameInfo();
194 MachineFrameInfo *MFI = MF.getFrameInfo();
195 SystemZMachineFunctionInfo *SystemZMFI =
196 MF.getInfo<SystemZMachineFunctionInfo>();
197 MachineBasicBlock::iterator MBBI = MBB.begin();
198 DebugLoc DL = (MBBI != MBB.end() ? MBBI->getDebugLoc() :
199 DebugLoc::getUnknownLoc());
201 // Get the number of bytes to allocate from the FrameInfo.
202 // Note that area for callee-saved stuff is already allocated, thus we need to
203 // 'undo' the stack movement.
205 MFI->getStackSize() - SystemZMFI->getCalleeSavedFrameSize();
207 // Skip the callee-saved push instructions.
208 while (MBBI != MBB.end() &&
209 (MBBI->getOpcode() == SystemZ::MOV64mr ||
210 MBBI->getOpcode() == SystemZ::MOV64mrm))
213 if (MBBI != MBB.end())
214 DL = MBBI->getDebugLoc();
216 uint64_t NumBytes = StackSize - TFI.getOffsetOfLocalArea();
218 if (StackSize) // adjust stack pointer: R15 -= numbytes
219 emitSPUpdate(MBB, MBBI, -(int64_t)NumBytes, TII);
222 // Update R11 with the new base value...
223 BuildMI(MBB, MBBI, DL, TII.get(SystemZ::MOV64rr), SystemZ::R11D)
224 .addReg(SystemZ::R15D);
226 // Mark the FramePtr as live-in in every block except the entry.
227 for (MachineFunction::iterator I = next(MF.begin()), E = MF.end();
229 I->addLiveIn(SystemZ::R11D);
234 void SystemZRegisterInfo::emitEpilogue(MachineFunction &MF,
235 MachineBasicBlock &MBB) const {
236 const MachineFrameInfo *MFI = MF.getFrameInfo();
237 const TargetFrameInfo &TFI = *MF.getTarget().getFrameInfo();
238 MachineBasicBlock::iterator MBBI = prior(MBB.end());
239 SystemZMachineFunctionInfo *SystemZMFI =
240 MF.getInfo<SystemZMachineFunctionInfo>();
241 unsigned RetOpcode = MBBI->getOpcode();
242 DebugLoc DL = MBBI->getDebugLoc();
245 case SystemZ::RET: break; // These are ok
247 assert(0 && "Can only insert epilog into returning blocks");
250 // Get the number of bytes to allocate from the FrameInfo
251 // Note that area for callee-saved stuff is already allocated, thus we need to
252 // 'undo' the stack movement.
254 MFI->getStackSize() - SystemZMFI->getCalleeSavedFrameSize();
255 uint64_t NumBytes = StackSize - TFI.getOffsetOfLocalArea();
257 // Skip the final terminator instruction.
258 while (MBBI != MBB.begin()) {
259 MachineBasicBlock::iterator PI = prior(MBBI);
261 if (!PI->getDesc().isTerminator())
265 // During callee-saved restores emission stack frame was not yet finialized
266 // (and thus - the stack size was unknown). Tune the offset having full stack
268 if (SystemZMFI->getCalleeSavedFrameSize()) {
269 assert((MBBI->getOpcode() == SystemZ::MOV64rmm ||
270 MBBI->getOpcode() == SystemZ::MOV64rm) &&
271 "Expected to see callee-save register restore code");
274 MachineInstr &MI = *MBBI;
275 while (!MI.getOperand(i).isImm()) {
277 assert(i < MI.getNumOperands() && "Unexpected restore code!");
280 MI.getOperand(i).ChangeToImmediate(NumBytes + MI.getOperand(i).getImm());
284 unsigned SystemZRegisterInfo::getRARegister() const {
285 assert(0 && "What is the return address register");
289 unsigned SystemZRegisterInfo::getFrameRegister(MachineFunction &MF) const {
290 assert(0 && "What is the frame register");
294 unsigned SystemZRegisterInfo::getEHExceptionRegister() const {
295 assert(0 && "What is the exception register");
299 unsigned SystemZRegisterInfo::getEHHandlerRegister() const {
300 assert(0 && "What is the exception handler register");
304 int SystemZRegisterInfo::getDwarfRegNum(unsigned RegNum, bool isEH) const {
305 assert(0 && "What is the dwarf register number");
309 #include "SystemZGenRegisterInfo.inc"