Implement dynamic allocas
[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     // 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();
93
94     assert((Amount == 0) && "Not implemented yet!");
95   }
96
97   MBB.erase(I);
98 }
99
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();
107
108   // Fixed objects are really located in the "previous" frame.
109   if (FI < 0)
110     StackSize -= SystemZMFI->getCalleeSavedFrameSize();
111
112   Offset += StackSize - TFI.getOffsetOfLocalArea();
113
114   // Skip the register save area if we generated the stack frame.
115   if (StackSize)
116     Offset -= TFI.getOffsetOfLocalArea();
117
118   return Offset;
119 }
120
121 void SystemZRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
122                                             int SPAdj, RegScavenger *RS) const {
123   assert(SPAdj == 0 && "Unxpected");
124
125   unsigned i = 0;
126   MachineInstr &MI = *II;
127   MachineFunction &MF = *MI.getParent()->getParent();
128   while (!MI.getOperand(i).isFI()) {
129     ++i;
130     assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
131   }
132
133   int FrameIndex = MI.getOperand(i).getIndex();
134
135   unsigned BasePtr = (hasFP(MF) ? SystemZ::R11D : SystemZ::R15D);
136
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);
141
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);
146 }
147
148 void
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();
154
155   if (FFI->hasCalls()
156       /* FIXME: function is varargs */
157       /* FIXME: function grabs RA */
158       /* FIXME: function calls eh_return */)
159     MF.getRegInfo().setPhysRegUsed(SystemZ::R14D);
160
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);
165 }
166
167 /// emitSPUpdate - Emit a series of instructions to increment / decrement the
168 /// stack pointer by a constant value.
169 static
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());
179
180   while (Offset) {
181     uint64_t ThisVal = (Offset > Chunk) ? Chunk : Offset;
182     MachineInstr *MI =
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();
187     Offset -= ThisVal;
188   }
189 }
190
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());
200
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.
204   uint64_t StackSize =
205     MFI->getStackSize() - SystemZMFI->getCalleeSavedFrameSize();
206
207   // Skip the callee-saved push instructions.
208   while (MBBI != MBB.end() &&
209          (MBBI->getOpcode() == SystemZ::MOV64mr ||
210           MBBI->getOpcode() == SystemZ::MOV64mrm))
211     ++MBBI;
212
213   if (MBBI != MBB.end())
214     DL = MBBI->getDebugLoc();
215
216   uint64_t NumBytes = StackSize - TFI.getOffsetOfLocalArea();
217
218   if (StackSize) // adjust stack pointer: R15 -= numbytes
219     emitSPUpdate(MBB, MBBI, -(int64_t)NumBytes, TII);
220
221   if (hasFP(MF)) {
222     // Update R11 with the new base value...
223     BuildMI(MBB, MBBI, DL, TII.get(SystemZ::MOV64rr), SystemZ::R11D)
224       .addReg(SystemZ::R15D);
225
226     // Mark the FramePtr as live-in in every block except the entry.
227     for (MachineFunction::iterator I = next(MF.begin()), E = MF.end();
228          I != E; ++I)
229       I->addLiveIn(SystemZ::R11D);
230
231   }
232 }
233
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();
243
244   switch (RetOpcode) {
245   case SystemZ::RET: break;  // These are ok
246   default:
247     assert(0 && "Can only insert epilog into returning blocks");
248   }
249
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.
253   uint64_t StackSize =
254     MFI->getStackSize() - SystemZMFI->getCalleeSavedFrameSize();
255   uint64_t NumBytes = StackSize - TFI.getOffsetOfLocalArea();
256
257   // Skip the final terminator instruction.
258   while (MBBI != MBB.begin()) {
259     MachineBasicBlock::iterator PI = prior(MBBI);
260     --MBBI;
261     if (!PI->getDesc().isTerminator())
262       break;
263   }
264
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
267   // size in hands.
268   if (SystemZMFI->getCalleeSavedFrameSize()) {
269     assert((MBBI->getOpcode() == SystemZ::MOV64rmm ||
270             MBBI->getOpcode() == SystemZ::MOV64rm) &&
271            "Expected to see callee-save register restore code");
272
273     unsigned i = 0;
274     MachineInstr &MI = *MBBI;
275     while (!MI.getOperand(i).isImm()) {
276       ++i;
277       assert(i < MI.getNumOperands() && "Unexpected restore code!");
278     }
279
280     MI.getOperand(i).ChangeToImmediate(NumBytes + MI.getOperand(i).getImm());
281   }
282 }
283
284 unsigned SystemZRegisterInfo::getRARegister() const {
285   assert(0 && "What is the return address register");
286   return 0;
287 }
288
289 unsigned SystemZRegisterInfo::getFrameRegister(MachineFunction &MF) const {
290   assert(0 && "What is the frame register");
291   return 0;
292 }
293
294 unsigned SystemZRegisterInfo::getEHExceptionRegister() const {
295   assert(0 && "What is the exception register");
296   return 0;
297 }
298
299 unsigned SystemZRegisterInfo::getEHHandlerRegister() const {
300   assert(0 && "What is the exception handler register");
301   return 0;
302 }
303
304 int SystemZRegisterInfo::getDwarfRegNum(unsigned RegNum, bool isEH) const {
305   assert(0 && "What is the dwarf register number");
306   return -1;
307 }
308
309 #include "SystemZGenRegisterInfo.inc"