First step of huge frame-related refactoring: move emit{Prologue,Epilogue} out of...
[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 unsigned SystemZRegisterInfo::getRARegister() const {
164   assert(0 && "What is the return address register");
165   return 0;
166 }
167
168 unsigned
169 SystemZRegisterInfo::getFrameRegister(const MachineFunction &MF) const {
170   assert(0 && "What is the frame register");
171   return 0;
172 }
173
174 unsigned SystemZRegisterInfo::getEHExceptionRegister() const {
175   assert(0 && "What is the exception register");
176   return 0;
177 }
178
179 unsigned SystemZRegisterInfo::getEHHandlerRegister() const {
180   assert(0 && "What is the exception handler register");
181   return 0;
182 }
183
184 int SystemZRegisterInfo::getDwarfRegNum(unsigned RegNum, bool isEH) const {
185   assert(0 && "What is the dwarf register number");
186   return -1;
187 }
188
189 #include "SystemZGenRegisterInfo.inc"