302c418d1ec96bc30cf22f55a2c4d6cfb6a41e4d
[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 "SystemZInstrInfo.h"
16 #include "SystemZMachineFunctionInfo.h"
17 #include "SystemZRegisterInfo.h"
18 #include "SystemZSubtarget.h"
19 #include "llvm/CodeGen/MachineInstrBuilder.h"
20 #include "llvm/CodeGen/MachineFrameInfo.h"
21 #include "llvm/CodeGen/MachineFunction.h"
22 #include "llvm/CodeGen/MachineRegisterInfo.h"
23 #include "llvm/Target/TargetFrameInfo.h"
24 #include "llvm/Target/TargetInstrInfo.h"
25 #include "llvm/Target/TargetMachine.h"
26 #include "llvm/Target/TargetOptions.h"
27 #include "llvm/ADT/BitVector.h"
28 using namespace llvm;
29
30 SystemZRegisterInfo::SystemZRegisterInfo(SystemZTargetMachine &tm,
31                                          const SystemZInstrInfo &tii)
32   : SystemZGenRegisterInfo(SystemZ::ADJCALLSTACKUP, SystemZ::ADJCALLSTACKDOWN),
33     TM(tm), TII(tii) {
34 }
35
36 const unsigned*
37 SystemZRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {
38   static const unsigned CalleeSavedRegs[] = {
39     SystemZ::R6D,  SystemZ::R7D,  SystemZ::R8D,  SystemZ::R9D,
40     SystemZ::R10D, SystemZ::R11D, SystemZ::R12D, SystemZ::R13D,
41     SystemZ::R14D, SystemZ::R15D,
42     SystemZ::F8L,  SystemZ::F9L,  SystemZ::F10L, SystemZ::F11L,
43     SystemZ::F12L, SystemZ::F13L, SystemZ::F14L, SystemZ::F15L,
44     0
45   };
46
47   return CalleeSavedRegs;
48 }
49
50 const TargetRegisterClass* const*
51 SystemZRegisterInfo::getCalleeSavedRegClasses(const MachineFunction *MF) const {
52   static const TargetRegisterClass * const CalleeSavedRegClasses[] = {
53     &SystemZ::GR64RegClass, &SystemZ::GR64RegClass,
54     &SystemZ::GR64RegClass, &SystemZ::GR64RegClass,
55     &SystemZ::GR64RegClass, &SystemZ::GR64RegClass,
56     &SystemZ::GR64RegClass, &SystemZ::GR64RegClass,
57     &SystemZ::GR64RegClass, &SystemZ::GR64RegClass,
58     &SystemZ::FP64RegClass, &SystemZ::FP64RegClass,
59     &SystemZ::FP64RegClass, &SystemZ::FP64RegClass,
60     &SystemZ::FP64RegClass, &SystemZ::FP64RegClass,
61     &SystemZ::FP64RegClass, &SystemZ::FP64RegClass, 0
62   };
63   return CalleeSavedRegClasses;
64 }
65
66 BitVector SystemZRegisterInfo::getReservedRegs(const MachineFunction &MF) const {
67   BitVector Reserved(getNumRegs());
68   if (hasFP(MF))
69     Reserved.set(SystemZ::R11D);
70   Reserved.set(SystemZ::R14D);
71   Reserved.set(SystemZ::R15D);
72   return Reserved;
73 }
74
75 /// needsFP - Return true if the specified function should have a dedicated
76 /// frame pointer register.  This is true if the function has variable sized
77 /// allocas or if frame pointer elimination is disabled.
78 bool SystemZRegisterInfo::hasFP(const MachineFunction &MF) const {
79   const MachineFrameInfo *MFI = MF.getFrameInfo();
80   return NoFramePointerElim || MFI->hasVarSizedObjects();
81 }
82
83 void SystemZRegisterInfo::
84 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
85                               MachineBasicBlock::iterator I) const {
86   MBB.erase(I);
87 }
88
89 int SystemZRegisterInfo::getFrameIndexOffset(const MachineFunction &MF,
90                                              int FI) const {
91   const TargetFrameInfo &TFI = *MF.getTarget().getFrameInfo();
92   const MachineFrameInfo *MFI = MF.getFrameInfo();
93   const SystemZMachineFunctionInfo *SystemZMFI =
94     MF.getInfo<SystemZMachineFunctionInfo>();
95   int Offset = MFI->getObjectOffset(FI) + MFI->getOffsetAdjustment();
96   uint64_t StackSize = MFI->getStackSize();
97
98   // Fixed objects are really located in the "previous" frame.
99   if (FI < 0)
100     StackSize -= SystemZMFI->getCalleeSavedFrameSize();
101
102   Offset += StackSize - TFI.getOffsetOfLocalArea();
103
104   // Skip the register save area if we generated the stack frame.
105   if (StackSize || MFI->hasCalls())
106     Offset -= TFI.getOffsetOfLocalArea();
107
108   return Offset;
109 }
110
111 unsigned
112 SystemZRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
113                                          int SPAdj, FrameIndexValue *Value,
114                                          RegScavenger *RS) const {
115   assert(SPAdj == 0 && "Unxpected");
116
117   unsigned i = 0;
118   MachineInstr &MI = *II;
119   MachineFunction &MF = *MI.getParent()->getParent();
120   while (!MI.getOperand(i).isFI()) {
121     ++i;
122     assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
123   }
124
125   int FrameIndex = MI.getOperand(i).getIndex();
126
127   unsigned BasePtr = (hasFP(MF) ? SystemZ::R11D : SystemZ::R15D);
128
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);
133
134   // Offset is a either 12-bit unsigned or 20-bit signed integer.
135   // FIXME: handle "too long" displacements.
136   int Offset = getFrameIndexOffset(MF, FrameIndex) + MI.getOperand(i+1).getImm();
137
138   // Check whether displacement is too long to fit into 12 bit zext field.
139   MI.setDesc(TII.getMemoryInstr(MI.getOpcode(), Offset));
140
141   MI.getOperand(i+1).ChangeToImmediate(Offset);
142   return 0;
143 }
144
145 void
146 SystemZRegisterInfo::processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
147                                                        RegScavenger *RS) const {
148   // Determine whether R15/R14 will ever be clobbered inside the function. And
149   // if yes - mark it as 'callee' saved.
150   MachineFrameInfo *FFI = MF.getFrameInfo();
151   MachineRegisterInfo &MRI = MF.getRegInfo();
152
153   // Check whether high FPRs are ever used, if yes - we need to save R15 as
154   // well.
155   static const unsigned HighFPRs[] = {
156     SystemZ::F8L,  SystemZ::F9L,  SystemZ::F10L, SystemZ::F11L,
157     SystemZ::F12L, SystemZ::F13L, SystemZ::F14L, SystemZ::F15L,
158     SystemZ::F8S,  SystemZ::F9S,  SystemZ::F10S, SystemZ::F11S,
159     SystemZ::F12S, SystemZ::F13S, SystemZ::F14S, SystemZ::F15S,
160   };
161
162   bool HighFPRsUsed = false;
163   for (unsigned i = 0, e = array_lengthof(HighFPRs); i != e; ++i)
164     HighFPRsUsed |= MRI.isPhysRegUsed(HighFPRs[i]);
165
166   if (FFI->hasCalls())
167     /* FIXME: function is varargs */
168     /* FIXME: function grabs RA */
169     /* FIXME: function calls eh_return */
170     MRI.setPhysRegUsed(SystemZ::R14D);
171
172   if (HighFPRsUsed ||
173       FFI->hasCalls() ||
174       FFI->getObjectIndexEnd() != 0 || // Contains automatic variables
175       FFI->hasVarSizedObjects() // Function calls dynamic alloca's
176       /* FIXME: function is varargs */)
177     MRI.setPhysRegUsed(SystemZ::R15D);
178 }
179
180 /// emitSPUpdate - Emit a series of instructions to increment / decrement the
181 /// stack pointer by a constant value.
182 static
183 void emitSPUpdate(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
184                   int64_t NumBytes, const TargetInstrInfo &TII) {
185   unsigned Opc; uint64_t Chunk;
186   bool isSub = NumBytes < 0;
187   uint64_t Offset = isSub ? -NumBytes : NumBytes;
188
189   if (Offset >= (1LL << 15) - 1) {
190     Opc = SystemZ::ADD64ri32;
191     Chunk = (1LL << 31) - 1;
192   } else {
193     Opc = SystemZ::ADD64ri16;
194     Chunk = (1LL << 15) - 1;
195   }
196
197   DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
198
199   while (Offset) {
200     uint64_t ThisVal = (Offset > Chunk) ? Chunk : Offset;
201     MachineInstr *MI =
202       BuildMI(MBB, MBBI, DL, TII.get(Opc), SystemZ::R15D)
203       .addReg(SystemZ::R15D).addImm((isSub ? -(int64_t)ThisVal : ThisVal));
204     // The PSW implicit def is dead.
205     MI->getOperand(3).setIsDead();
206     Offset -= ThisVal;
207   }
208 }
209
210 void SystemZRegisterInfo::emitPrologue(MachineFunction &MF) const {
211   MachineBasicBlock &MBB = MF.front();   // Prolog goes in entry BB
212   const TargetFrameInfo &TFI = *MF.getTarget().getFrameInfo();
213   MachineFrameInfo *MFI = MF.getFrameInfo();
214   SystemZMachineFunctionInfo *SystemZMFI =
215     MF.getInfo<SystemZMachineFunctionInfo>();
216   MachineBasicBlock::iterator MBBI = MBB.begin();
217   DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
218
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.
222   uint64_t StackSize = MFI->getStackSize();
223   StackSize -= SystemZMFI->getCalleeSavedFrameSize();
224
225   uint64_t NumBytes = StackSize - TFI.getOffsetOfLocalArea();
226
227   // Skip the callee-saved push instructions.
228   while (MBBI != MBB.end() &&
229          (MBBI->getOpcode() == SystemZ::MOV64mr ||
230           MBBI->getOpcode() == SystemZ::MOV64mrm))
231     ++MBBI;
232
233   if (MBBI != MBB.end())
234     DL = MBBI->getDebugLoc();
235
236   // adjust stack pointer: R15 -= numbytes
237   if (StackSize || MFI->hasCalls()) {
238     assert(MF.getRegInfo().isPhysRegUsed(SystemZ::R15D) &&
239            "Invalid stack frame calculation!");
240     emitSPUpdate(MBB, MBBI, -(int64_t)NumBytes, TII);
241   }
242
243   if (hasFP(MF)) {
244     // Update R11 with the new base value...
245     BuildMI(MBB, MBBI, DL, TII.get(SystemZ::MOV64rr), SystemZ::R11D)
246       .addReg(SystemZ::R15D);
247
248     // Mark the FramePtr as live-in in every block except the entry.
249     for (MachineFunction::iterator I = llvm::next(MF.begin()), E = MF.end();
250          I != E; ++I)
251       I->addLiveIn(SystemZ::R11D);
252
253   }
254 }
255
256 void SystemZRegisterInfo::emitEpilogue(MachineFunction &MF,
257                                      MachineBasicBlock &MBB) const {
258   const MachineFrameInfo *MFI = MF.getFrameInfo();
259   const TargetFrameInfo &TFI = *MF.getTarget().getFrameInfo();
260   MachineBasicBlock::iterator MBBI = prior(MBB.end());
261   SystemZMachineFunctionInfo *SystemZMFI =
262     MF.getInfo<SystemZMachineFunctionInfo>();
263   unsigned RetOpcode = MBBI->getOpcode();
264
265   switch (RetOpcode) {
266   case SystemZ::RET: break;  // These are ok
267   default:
268     assert(0 && "Can only insert epilog into returning blocks");
269   }
270
271   // Get the number of bytes to allocate from the FrameInfo
272   // Note that area for callee-saved stuff is already allocated, thus we need to
273   // 'undo' the stack movement.
274   uint64_t StackSize =
275     MFI->getStackSize() - SystemZMFI->getCalleeSavedFrameSize();
276   uint64_t NumBytes = StackSize - TFI.getOffsetOfLocalArea();
277
278   // Skip the final terminator instruction.
279   while (MBBI != MBB.begin()) {
280     MachineBasicBlock::iterator PI = prior(MBBI);
281     --MBBI;
282     if (!PI->getDesc().isTerminator())
283       break;
284   }
285
286   // During callee-saved restores emission stack frame was not yet finialized
287   // (and thus - the stack size was unknown). Tune the offset having full stack
288   // size in hands.
289   if (StackSize || MFI->hasCalls()) {
290     assert((MBBI->getOpcode() == SystemZ::MOV64rmm ||
291             MBBI->getOpcode() == SystemZ::MOV64rm) &&
292            "Expected to see callee-save register restore code");
293     assert(MF.getRegInfo().isPhysRegUsed(SystemZ::R15D) &&
294            "Invalid stack frame calculation!");
295
296     unsigned i = 0;
297     MachineInstr &MI = *MBBI;
298     while (!MI.getOperand(i).isImm()) {
299       ++i;
300       assert(i < MI.getNumOperands() && "Unexpected restore code!");
301     }
302
303     uint64_t Offset = NumBytes + MI.getOperand(i).getImm();
304     // If Offset does not fit into 20-bit signed displacement field we need to
305     // emit some additional code...
306     if (Offset > 524287) {
307       // Fold the displacement into load instruction as much as possible.
308       NumBytes = Offset - 524287;
309       Offset = 524287;
310       emitSPUpdate(MBB, MBBI, NumBytes, TII);
311     }
312
313     MI.getOperand(i).ChangeToImmediate(Offset);
314   }
315 }
316
317 unsigned SystemZRegisterInfo::getRARegister() const {
318   assert(0 && "What is the return address register");
319   return 0;
320 }
321
322 unsigned
323 SystemZRegisterInfo::getFrameRegister(const MachineFunction &MF) const {
324   assert(0 && "What is the frame register");
325   return 0;
326 }
327
328 unsigned SystemZRegisterInfo::getEHExceptionRegister() const {
329   assert(0 && "What is the exception register");
330   return 0;
331 }
332
333 unsigned SystemZRegisterInfo::getEHHandlerRegister() const {
334   assert(0 && "What is the exception handler register");
335   return 0;
336 }
337
338 int SystemZRegisterInfo::getDwarfRegNum(unsigned RegNum, bool isEH) const {
339   assert(0 && "What is the dwarf register number");
340   return -1;
341 }
342
343 #include "SystemZGenRegisterInfo.inc"