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