Some preliminary call lowering
[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/Target/TargetFrameInfo.h"
22 #include "llvm/Target/TargetInstrInfo.h"
23 #include "llvm/Target/TargetMachine.h"
24 #include "llvm/Target/TargetOptions.h"
25 #include "llvm/ADT/BitVector.h"
26 using namespace llvm;
27
28 SystemZRegisterInfo::SystemZRegisterInfo(SystemZTargetMachine &tm,
29                                          const TargetInstrInfo &tii)
30   : SystemZGenRegisterInfo(SystemZ::ADJCALLSTACKUP, SystemZ::ADJCALLSTACKDOWN),
31     TM(tm), TII(tii) {
32 }
33
34 const unsigned*
35 SystemZRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {
36   static const unsigned CalleeSavedRegs[] = {
37     SystemZ::R6D,  SystemZ::R7D,  SystemZ::R8D,  SystemZ::R9D,
38     SystemZ::R10D, SystemZ::R11D, SystemZ::R12D, SystemZ::R13D, SystemZ::R14D,
39     SystemZ::F1,  SystemZ::F3,  SystemZ::F5,  SystemZ::F7,
40     0
41   };
42
43   return CalleeSavedRegs;
44 }
45
46 const TargetRegisterClass* const*
47 SystemZRegisterInfo::getCalleeSavedRegClasses(const MachineFunction *MF) const {
48   static const TargetRegisterClass * const CalleeSavedRegClasses[] = {
49     &SystemZ::GR64RegClass, &SystemZ::GR64RegClass,
50     &SystemZ::GR64RegClass, &SystemZ::GR64RegClass,
51     &SystemZ::GR64RegClass, &SystemZ::GR64RegClass,
52     &SystemZ::GR64RegClass, &SystemZ::GR64RegClass,
53     &SystemZ::GR64RegClass,
54     &SystemZ::FP64RegClass, &SystemZ::FP64RegClass,
55     &SystemZ::FP64RegClass, &SystemZ::FP64RegClass, 0
56   };
57   return CalleeSavedRegClasses;
58 }
59
60 BitVector SystemZRegisterInfo::getReservedRegs(const MachineFunction &MF) const {
61   BitVector Reserved(getNumRegs());
62   if (hasFP(MF))
63     Reserved.set(SystemZ::R11D);
64   Reserved.set(SystemZ::R14D);
65   Reserved.set(SystemZ::R15D);
66   return Reserved;
67 }
68
69 // needsFP - Return true if the specified function should have a dedicated frame
70 // pointer register.  This is true if the function has variable sized allocas or
71 // if frame pointer elimination is disabled.
72 //
73 bool SystemZRegisterInfo::hasFP(const MachineFunction &MF) const {
74   const MachineFrameInfo *MFI = MF.getFrameInfo();
75   return NoFramePointerElim || MFI->hasVarSizedObjects();
76 }
77
78 bool SystemZRegisterInfo::hasReservedCallFrame(MachineFunction &MF) const {
79   return !MF.getFrameInfo()->hasVarSizedObjects();
80 }
81
82 void SystemZRegisterInfo::
83 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
84                               MachineBasicBlock::iterator I) const {
85   if (!hasReservedCallFrame(MF)) {
86     assert(0 && "Not implemented yet!");
87   }
88
89   MBB.erase(I);
90 }
91
92 int SystemZRegisterInfo::getFrameIndexOffset(MachineFunction &MF, int FI) const {
93   const TargetFrameInfo &TFI = *MF.getTarget().getFrameInfo();
94   MachineFrameInfo *MFI = MF.getFrameInfo();
95   SystemZMachineFunctionInfo *SystemZMFI =
96     MF.getInfo<SystemZMachineFunctionInfo>();
97   int Offset = MFI->getObjectOffset(FI) + MFI->getOffsetAdjustment();
98   uint64_t StackSize = MFI->getStackSize();
99
100   // Fixed objects are really located in the "previous" frame.
101   if (FI < 0)
102     StackSize -= SystemZMFI->getCalleeSavedFrameSize();
103
104   Offset += StackSize - TFI.getOffsetOfLocalArea();
105
106   // Skip the register save area if we generated the stack frame.
107   if (StackSize)
108     Offset -= TFI.getOffsetOfLocalArea();
109
110   return Offset;
111 }
112
113 void SystemZRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
114                                             int SPAdj, 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 20-bit integer.
135   // FIXME: handle "too long" displacements.
136   int Offset = getFrameIndexOffset(MF, FrameIndex) + MI.getOperand(i+1).getImm();
137   MI.getOperand(i+1).ChangeToImmediate(Offset);
138 }
139
140 /// emitSPUpdate - Emit a series of instructions to increment / decrement the
141 /// stack pointer by a constant value.
142 static
143 void emitSPUpdate(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
144                   int64_t NumBytes, const TargetInstrInfo &TII) {
145   // FIXME: Handle different stack sizes here.
146   bool isSub = NumBytes < 0;
147   uint64_t Offset = isSub ? -NumBytes : NumBytes;
148   unsigned Opc = SystemZ::ADD64ri16;
149   uint64_t Chunk = (1LL << 15) - 1;
150   DebugLoc DL = (MBBI != MBB.end() ? MBBI->getDebugLoc() :
151                  DebugLoc::getUnknownLoc());
152
153   while (Offset) {
154     uint64_t ThisVal = (Offset > Chunk) ? Chunk : Offset;
155     MachineInstr *MI =
156       BuildMI(MBB, MBBI, DL, TII.get(Opc), SystemZ::R15D)
157       .addReg(SystemZ::R15D).addImm((isSub ? -(int64_t)ThisVal : ThisVal));
158     // The PSW implicit def is dead.
159     MI->getOperand(3).setIsDead();
160     Offset -= ThisVal;
161   }
162 }
163
164 void SystemZRegisterInfo::emitPrologue(MachineFunction &MF) const {
165   MachineBasicBlock &MBB = MF.front();   // Prolog goes in entry BB
166   const TargetFrameInfo &TFI = *MF.getTarget().getFrameInfo();
167   MachineFrameInfo *MFI = MF.getFrameInfo();
168   SystemZMachineFunctionInfo *SystemZMFI =
169     MF.getInfo<SystemZMachineFunctionInfo>();
170   MachineBasicBlock::iterator MBBI = MBB.begin();
171   DebugLoc DL = (MBBI != MBB.end() ? MBBI->getDebugLoc() :
172                  DebugLoc::getUnknownLoc());
173
174   // Get the number of bytes to allocate from the FrameInfo.
175   // Note that area for callee-saved stuff is already allocated, thus we need to
176   // 'undo' the stack movement.
177   uint64_t StackSize =
178     MFI->getStackSize() - SystemZMFI->getCalleeSavedFrameSize();
179
180   // FIXME: Skip the callee-saved push instructions.
181
182   if (MBBI != MBB.end())
183     DL = MBBI->getDebugLoc();
184
185   uint64_t NumBytes = StackSize - TFI.getOffsetOfLocalArea();
186
187   if (StackSize) // adjust stack pointer: R15 -= numbytes
188     emitSPUpdate(MBB, MBBI, -(int64_t)NumBytes, TII);
189
190   if (hasFP(MF)) {
191     // Update R11 with the new base value...
192     BuildMI(MBB, MBBI, DL, TII.get(SystemZ::MOV64rr), SystemZ::R11D)
193       .addReg(SystemZ::R15D);
194
195     // Mark the FramePtr as live-in in every block except the entry.
196     for (MachineFunction::iterator I = next(MF.begin()), E = MF.end();
197          I != E; ++I)
198       I->addLiveIn(SystemZ::R11D);
199
200   }
201 }
202
203 void SystemZRegisterInfo::emitEpilogue(MachineFunction &MF,
204                                      MachineBasicBlock &MBB) const {
205   const MachineFrameInfo *MFI = MF.getFrameInfo();
206   const TargetFrameInfo &TFI = *MF.getTarget().getFrameInfo();
207   MachineBasicBlock::iterator MBBI = prior(MBB.end());
208   SystemZMachineFunctionInfo *SystemZMFI =
209     MF.getInfo<SystemZMachineFunctionInfo>();
210   unsigned RetOpcode = MBBI->getOpcode();
211   DebugLoc DL = MBBI->getDebugLoc();
212
213   switch (RetOpcode) {
214   case SystemZ::RET: break;  // These are ok
215   default:
216     assert(0 && "Can only insert epilog into returning blocks");
217   }
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 =
223     MFI->getStackSize() - SystemZMFI->getCalleeSavedFrameSize();
224   uint64_t NumBytes = StackSize - TFI.getOffsetOfLocalArea();
225
226   // Skip the callee-saved regs load instructions.
227   MachineBasicBlock::iterator LastCSPop = MBBI;
228   while (MBBI != MBB.begin()) {
229     MachineBasicBlock::iterator PI = prior(MBBI);
230     if (!PI->getDesc().isTerminator())
231       break;
232     --MBBI;
233   }
234
235   DL = MBBI->getDebugLoc();
236
237   if (MFI->hasVarSizedObjects()) {
238     assert(0 && "Not implemented yet!");
239   } else {
240     // adjust stack pointer back: R15 += numbytes
241     if (StackSize)
242       emitSPUpdate(MBB, MBBI, NumBytes, TII);
243   }
244 }
245
246 unsigned SystemZRegisterInfo::getRARegister() const {
247   assert(0 && "What is the return address register");
248   return 0;
249 }
250
251 unsigned SystemZRegisterInfo::getFrameRegister(MachineFunction &MF) const {
252   assert(0 && "What is the frame register");
253   return 0;
254 }
255
256 unsigned SystemZRegisterInfo::getEHExceptionRegister() const {
257   assert(0 && "What is the exception register");
258   return 0;
259 }
260
261 unsigned SystemZRegisterInfo::getEHHandlerRegister() const {
262   assert(0 && "What is the exception handler register");
263   return 0;
264 }
265
266 int SystemZRegisterInfo::getDwarfRegNum(unsigned RegNum, bool isEH) const {
267   assert(0 && "What is the dwarf register number");
268   return -1;
269 }
270
271 #include "SystemZGenRegisterInfo.inc"