We already have reserved call frame regardless whether variable sized frame objects...
[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 void SystemZRegisterInfo::
80 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
81                               MachineBasicBlock::iterator I) const {
82   MBB.erase(I);
83 }
84
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();
92
93   // Fixed objects are really located in the "previous" frame.
94   if (FI < 0)
95     StackSize -= SystemZMFI->getCalleeSavedFrameSize();
96
97   Offset += StackSize - TFI.getOffsetOfLocalArea();
98
99   // Skip the register save area if we generated the stack frame.
100   if (StackSize)
101     Offset -= TFI.getOffsetOfLocalArea();
102
103   return Offset;
104 }
105
106 void SystemZRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
107                                             int SPAdj, RegScavenger *RS) const {
108   assert(SPAdj == 0 && "Unxpected");
109
110   unsigned i = 0;
111   MachineInstr &MI = *II;
112   MachineFunction &MF = *MI.getParent()->getParent();
113   while (!MI.getOperand(i).isFI()) {
114     ++i;
115     assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
116   }
117
118   int FrameIndex = MI.getOperand(i).getIndex();
119
120   unsigned BasePtr = (hasFP(MF) ? SystemZ::R11D : SystemZ::R15D);
121
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);
126
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);
131 }
132
133 void
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();
139
140   if (FFI->hasCalls()
141       /* FIXME: function is varargs */
142       /* FIXME: function grabs RA */
143       /* FIXME: function calls eh_return */)
144     MF.getRegInfo().setPhysRegUsed(SystemZ::R14D);
145
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);
150 }
151
152 /// emitSPUpdate - Emit a series of instructions to increment / decrement the
153 /// stack pointer by a constant value.
154 static
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());
164
165   while (Offset) {
166     uint64_t ThisVal = (Offset > Chunk) ? Chunk : Offset;
167     MachineInstr *MI =
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();
172     Offset -= ThisVal;
173   }
174 }
175
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());
185
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.
189   uint64_t StackSize =
190     MFI->getStackSize() - SystemZMFI->getCalleeSavedFrameSize();
191
192   // Skip the callee-saved push instructions.
193   while (MBBI != MBB.end() &&
194          (MBBI->getOpcode() == SystemZ::MOV64mr ||
195           MBBI->getOpcode() == SystemZ::MOV64mrm))
196     ++MBBI;
197
198   if (MBBI != MBB.end())
199     DL = MBBI->getDebugLoc();
200
201   uint64_t NumBytes = StackSize - TFI.getOffsetOfLocalArea();
202
203   if (StackSize) // adjust stack pointer: R15 -= numbytes
204     emitSPUpdate(MBB, MBBI, -(int64_t)NumBytes, TII);
205
206   if (hasFP(MF)) {
207     // Update R11 with the new base value...
208     BuildMI(MBB, MBBI, DL, TII.get(SystemZ::MOV64rr), SystemZ::R11D)
209       .addReg(SystemZ::R15D);
210
211     // Mark the FramePtr as live-in in every block except the entry.
212     for (MachineFunction::iterator I = next(MF.begin()), E = MF.end();
213          I != E; ++I)
214       I->addLiveIn(SystemZ::R11D);
215
216   }
217 }
218
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();
228
229   switch (RetOpcode) {
230   case SystemZ::RET: break;  // These are ok
231   default:
232     assert(0 && "Can only insert epilog into returning blocks");
233   }
234
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.
238   uint64_t StackSize =
239     MFI->getStackSize() - SystemZMFI->getCalleeSavedFrameSize();
240   uint64_t NumBytes = StackSize - TFI.getOffsetOfLocalArea();
241
242   // Skip the final terminator instruction.
243   while (MBBI != MBB.begin()) {
244     MachineBasicBlock::iterator PI = prior(MBBI);
245     --MBBI;
246     if (!PI->getDesc().isTerminator())
247       break;
248   }
249
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
252   // size in hands.
253   if (SystemZMFI->getCalleeSavedFrameSize()) {
254     assert((MBBI->getOpcode() == SystemZ::MOV64rmm ||
255             MBBI->getOpcode() == SystemZ::MOV64rm) &&
256            "Expected to see callee-save register restore code");
257
258     unsigned i = 0;
259     MachineInstr &MI = *MBBI;
260     while (!MI.getOperand(i).isImm()) {
261       ++i;
262       assert(i < MI.getNumOperands() && "Unexpected restore code!");
263     }
264
265     MI.getOperand(i).ChangeToImmediate(NumBytes + MI.getOperand(i).getImm());
266   }
267 }
268
269 unsigned SystemZRegisterInfo::getRARegister() const {
270   assert(0 && "What is the return address register");
271   return 0;
272 }
273
274 unsigned SystemZRegisterInfo::getFrameRegister(MachineFunction &MF) const {
275   assert(0 && "What is the frame register");
276   return 0;
277 }
278
279 unsigned SystemZRegisterInfo::getEHExceptionRegister() const {
280   assert(0 && "What is the exception register");
281   return 0;
282 }
283
284 unsigned SystemZRegisterInfo::getEHHandlerRegister() const {
285   assert(0 && "What is the exception handler register");
286   return 0;
287 }
288
289 int SystemZRegisterInfo::getDwarfRegNum(unsigned RegNum, bool isEH) const {
290   assert(0 && "What is the dwarf register number");
291   return -1;
292 }
293
294 #include "SystemZGenRegisterInfo.inc"