Emit callee-saved regs spills / restores
[oota-llvm.git] / lib / Target / SystemZ / SystemZRegisterInfo.cpp
1 //===- SystemZRegisterInfo.cpp - SystemZ Register Information -------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains the SystemZ implementation of the TargetRegisterInfo class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "SystemZ.h"
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"
27 using namespace llvm;
28
29 SystemZRegisterInfo::SystemZRegisterInfo(SystemZTargetMachine &tm,
30                                          const TargetInstrInfo &tii)
31   : SystemZGenRegisterInfo(SystemZ::ADJCALLSTACKUP, SystemZ::ADJCALLSTACKDOWN),
32     TM(tm), TII(tii) {
33 }
34
35 const unsigned*
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,
42     0
43   };
44
45   return CalleeSavedRegs;
46 }
47
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
58   };
59   return CalleeSavedRegClasses;
60 }
61
62 BitVector SystemZRegisterInfo::getReservedRegs(const MachineFunction &MF) const {
63   BitVector Reserved(getNumRegs());
64   if (hasFP(MF))
65     Reserved.set(SystemZ::R11D);
66   Reserved.set(SystemZ::R14D);
67   Reserved.set(SystemZ::R15D);
68   return Reserved;
69 }
70
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();
77 }
78
79 bool SystemZRegisterInfo::hasReservedCallFrame(MachineFunction &MF) const {
80   // FIXME: Should we always have reserved call frame?
81   return !MF.getFrameInfo()->hasVarSizedObjects();
82 }
83
84 void SystemZRegisterInfo::
85 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
86                               MachineBasicBlock::iterator I) const {
87   if (!hasReservedCallFrame(MF)) {
88     assert(0 && "Not implemented yet!");
89   }
90
91   MBB.erase(I);
92 }
93
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();
101
102   // Fixed objects are really located in the "previous" frame.
103   if (FI < 0)
104     StackSize -= SystemZMFI->getCalleeSavedFrameSize();
105
106   Offset += StackSize - TFI.getOffsetOfLocalArea();
107
108   // Skip the register save area if we generated the stack frame.
109   if (StackSize)
110     Offset -= TFI.getOffsetOfLocalArea();
111
112   return Offset;
113 }
114
115 void SystemZRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
116                                             int SPAdj, RegScavenger *RS) const {
117   assert(SPAdj == 0 && "Unxpected");
118
119   unsigned i = 0;
120   MachineInstr &MI = *II;
121   MachineFunction &MF = *MI.getParent()->getParent();
122   while (!MI.getOperand(i).isFI()) {
123     ++i;
124     assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
125   }
126
127   int FrameIndex = MI.getOperand(i).getIndex();
128
129   unsigned BasePtr = (hasFP(MF) ? SystemZ::R11D : SystemZ::R15D);
130
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);
135
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);
140 }
141
142 void
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();
148
149   if (FFI->hasCalls()
150       /* FIXME: function is varargs */
151       /* FIXME: function grabs RA */
152       /* FIXME: function calls eh_return */)
153     MF.getRegInfo().setPhysRegUsed(SystemZ::R14D);
154
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);
159 }
160
161 /// emitSPUpdate - Emit a series of instructions to increment / decrement the
162 /// stack pointer by a constant value.
163 static
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());
173
174   while (Offset) {
175     uint64_t ThisVal = (Offset > Chunk) ? Chunk : Offset;
176     MachineInstr *MI =
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();
181     Offset -= ThisVal;
182   }
183 }
184
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());
194
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.
198   uint64_t StackSize =
199     MFI->getStackSize() - SystemZMFI->getCalleeSavedFrameSize();
200
201   // Skip the callee-saved push instructions.
202   while (MBBI != MBB.end() &&
203          (MBBI->getOpcode() == SystemZ::MOV64mr ||
204           MBBI->getOpcode() == SystemZ::MOV64mrm))
205     ++MBBI;
206
207   if (MBBI != MBB.end())
208     DL = MBBI->getDebugLoc();
209
210   uint64_t NumBytes = StackSize - TFI.getOffsetOfLocalArea();
211
212   if (StackSize) // adjust stack pointer: R15 -= numbytes
213     emitSPUpdate(MBB, MBBI, -(int64_t)NumBytes, TII);
214
215   if (hasFP(MF)) {
216     // Update R11 with the new base value...
217     BuildMI(MBB, MBBI, DL, TII.get(SystemZ::MOV64rr), SystemZ::R11D)
218       .addReg(SystemZ::R15D);
219
220     // Mark the FramePtr as live-in in every block except the entry.
221     for (MachineFunction::iterator I = next(MF.begin()), E = MF.end();
222          I != E; ++I)
223       I->addLiveIn(SystemZ::R11D);
224
225   }
226 }
227
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();
237
238   switch (RetOpcode) {
239   case SystemZ::RET: break;  // These are ok
240   default:
241     assert(0 && "Can only insert epilog into returning blocks");
242   }
243
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.
247   uint64_t StackSize =
248     MFI->getStackSize() - SystemZMFI->getCalleeSavedFrameSize();
249   uint64_t NumBytes = StackSize - TFI.getOffsetOfLocalArea();
250
251   // Skip the final terminator instruction.
252   while (MBBI != MBB.begin()) {
253     MachineBasicBlock::iterator PI = prior(MBBI);
254     --MBBI;
255     if (!PI->getDesc().isTerminator())
256       break;
257   }
258
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
261   // size in hands.
262   if (SystemZMFI->getCalleeSavedFrameSize()) {
263     assert((MBBI->getOpcode() == SystemZ::MOV64rmm ||
264             MBBI->getOpcode() == SystemZ::MOV64rm) &&
265            "Expected to see callee-save register restore code");
266
267     unsigned i = 0;
268     MachineInstr &MI = *MBBI;
269     while (!MI.getOperand(i).isImm()) {
270       ++i;
271       assert(i < MI.getNumOperands() && "Unexpected restore code!");
272     }
273
274     MI.getOperand(i).ChangeToImmediate(NumBytes + MI.getOperand(i).getImm());
275   }
276 }
277
278 unsigned SystemZRegisterInfo::getRARegister() const {
279   assert(0 && "What is the return address register");
280   return 0;
281 }
282
283 unsigned SystemZRegisterInfo::getFrameRegister(MachineFunction &MF) const {
284   assert(0 && "What is the frame register");
285   return 0;
286 }
287
288 unsigned SystemZRegisterInfo::getEHExceptionRegister() const {
289   assert(0 && "What is the exception register");
290   return 0;
291 }
292
293 unsigned SystemZRegisterInfo::getEHHandlerRegister() const {
294   assert(0 && "What is the exception handler register");
295   return 0;
296 }
297
298 int SystemZRegisterInfo::getDwarfRegNum(unsigned RegNum, bool isEH) const {
299   assert(0 && "What is the dwarf register number");
300   return -1;
301 }
302
303 #include "SystemZGenRegisterInfo.inc"