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