Long live the exception handling!
[oota-llvm.git] / lib / Target / Sparc / SparcRegisterInfo.cpp
1 //===- SparcRegisterInfo.cpp - SPARC Register Information -------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains the SPARC implementation of the MRegisterInfo class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "Sparc.h"
15 #include "SparcRegisterInfo.h"
16 #include "SparcSubtarget.h"
17 #include "llvm/CodeGen/MachineInstrBuilder.h"
18 #include "llvm/CodeGen/MachineFunction.h"
19 #include "llvm/CodeGen/MachineFrameInfo.h"
20 #include "llvm/CodeGen/MachineLocation.h"
21 #include "llvm/Target/TargetInstrInfo.h"
22 #include "llvm/Type.h"
23 #include "llvm/ADT/BitVector.h"
24 #include "llvm/ADT/STLExtras.h"
25 using namespace llvm;
26
27 SparcRegisterInfo::SparcRegisterInfo(SparcSubtarget &st,
28                                      const TargetInstrInfo &tii)
29   : SparcGenRegisterInfo(SP::ADJCALLSTACKDOWN, SP::ADJCALLSTACKUP),
30     Subtarget(st), TII(tii) {
31 }
32
33 void SparcRegisterInfo::
34 storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
35                     unsigned SrcReg, int FI,
36                     const TargetRegisterClass *RC) const {
37   // On the order of operands here: think "[FrameIdx + 0] = SrcReg".
38   if (RC == SP::IntRegsRegisterClass)
39     BuildMI(MBB, I, TII.get(SP::STri)).addFrameIndex(FI).addImm(0)
40       .addReg(SrcReg, false, false, true);
41   else if (RC == SP::FPRegsRegisterClass)
42     BuildMI(MBB, I, TII.get(SP::STFri)).addFrameIndex(FI).addImm(0)
43       .addReg(SrcReg, false, false, true);
44   else if (RC == SP::DFPRegsRegisterClass)
45     BuildMI(MBB, I, TII.get(SP::STDFri)).addFrameIndex(FI).addImm(0)
46       .addReg(SrcReg, false, false, true);
47   else
48     assert(0 && "Can't store this register to stack slot");
49 }
50
51 void SparcRegisterInfo::
52 loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
53                      unsigned DestReg, int FI,
54                      const TargetRegisterClass *RC) const {
55   if (RC == SP::IntRegsRegisterClass)
56     BuildMI(MBB, I, TII.get(SP::LDri), DestReg).addFrameIndex(FI).addImm(0);
57   else if (RC == SP::FPRegsRegisterClass)
58     BuildMI(MBB, I, TII.get(SP::LDFri), DestReg).addFrameIndex(FI).addImm(0);
59   else if (RC == SP::DFPRegsRegisterClass)
60     BuildMI(MBB, I, TII.get(SP::LDDFri), DestReg).addFrameIndex(FI).addImm(0);
61   else
62     assert(0 && "Can't load this register from stack slot");
63 }
64
65 void SparcRegisterInfo::copyRegToReg(MachineBasicBlock &MBB,
66                                      MachineBasicBlock::iterator I,
67                                      unsigned DestReg, unsigned SrcReg,
68                                      const TargetRegisterClass *RC) const {
69   if (RC == SP::IntRegsRegisterClass)
70     BuildMI(MBB, I, TII.get(SP::ORrr), DestReg).addReg(SP::G0).addReg(SrcReg);
71   else if (RC == SP::FPRegsRegisterClass)
72     BuildMI(MBB, I, TII.get(SP::FMOVS), DestReg).addReg(SrcReg);
73   else if (RC == SP::DFPRegsRegisterClass)
74     BuildMI(MBB, I, TII.get(Subtarget.isV9() ? SP::FMOVD : SP::FpMOVD),DestReg)
75       .addReg(SrcReg);
76   else
77     assert (0 && "Can't copy this register");
78 }
79
80 void SparcRegisterInfo::reMaterialize(MachineBasicBlock &MBB,
81                                       MachineBasicBlock::iterator I,
82                                       unsigned DestReg,
83                                       const MachineInstr *Orig) const {
84   MachineInstr *MI = Orig->clone();
85   MI->getOperand(0).setReg(DestReg);
86   MBB.insert(I, MI);
87 }
88
89 MachineInstr *SparcRegisterInfo::foldMemoryOperand(MachineInstr* MI,
90                                                    unsigned OpNum,
91                                                    int FI) const {
92   bool isFloat = false;
93   MachineInstr *NewMI = NULL;
94   switch (MI->getOpcode()) {
95   case SP::ORrr:
96     if (MI->getOperand(1).isRegister() && MI->getOperand(1).getReg() == SP::G0&&
97         MI->getOperand(0).isRegister() && MI->getOperand(2).isRegister()) {
98       if (OpNum == 0)    // COPY -> STORE
99         NewMI = BuildMI(TII.get(SP::STri)).addFrameIndex(FI).addImm(0)
100                                    .addReg(MI->getOperand(2).getReg());
101       else               // COPY -> LOAD
102         NewMI = BuildMI(TII.get(SP::LDri), MI->getOperand(0).getReg())
103                       .addFrameIndex(FI).addImm(0);
104     }
105     break;
106   case SP::FMOVS:
107     isFloat = true;
108     // FALLTHROUGH
109   case SP::FMOVD:
110     if (OpNum == 0)  // COPY -> STORE
111       NewMI = BuildMI(TII.get(isFloat ? SP::STFri : SP::STDFri))
112                .addFrameIndex(FI).addImm(0).addReg(MI->getOperand(1).getReg());
113     else             // COPY -> LOAD
114       NewMI = BuildMI(TII.get(isFloat ? SP::LDFri : SP::LDDFri),
115                      MI->getOperand(0).getReg()).addFrameIndex(FI).addImm(0);
116     break;
117   }
118
119   if (NewMI)
120     NewMI->copyKillDeadInfo(MI);
121   return NewMI;
122 }
123
124 const unsigned* SparcRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF)
125                                                                          const {
126   static const unsigned CalleeSavedRegs[] = { 0 };
127   return CalleeSavedRegs;
128 }
129
130 BitVector SparcRegisterInfo::getReservedRegs(const MachineFunction &MF) const {
131   BitVector Reserved(getNumRegs());
132   Reserved.set(SP::G2);
133   Reserved.set(SP::G3);
134   Reserved.set(SP::G4);
135   Reserved.set(SP::O6);
136   Reserved.set(SP::I6);
137   Reserved.set(SP::I7);
138   Reserved.set(SP::G0);
139   Reserved.set(SP::G5);
140   Reserved.set(SP::G6);
141   Reserved.set(SP::G7);
142   return Reserved;
143 }
144
145
146 const TargetRegisterClass* const*
147 SparcRegisterInfo::getCalleeSavedRegClasses(const MachineFunction *MF) const {
148   static const TargetRegisterClass * const CalleeSavedRegClasses[] = { 0 };
149   return CalleeSavedRegClasses;
150 }
151
152 bool SparcRegisterInfo::hasFP(const MachineFunction &MF) const {
153   return false;
154 }
155
156 void SparcRegisterInfo::
157 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
158                               MachineBasicBlock::iterator I) const {
159   MachineInstr &MI = *I;
160   int Size = MI.getOperand(0).getImmedValue();
161   if (MI.getOpcode() == SP::ADJCALLSTACKDOWN)
162     Size = -Size;
163   if (Size)
164     BuildMI(MBB, I, TII.get(SP::ADDri), SP::O6).addReg(SP::O6).addImm(Size);
165   MBB.erase(I);
166 }
167
168 void SparcRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
169                                             int SPAdj, RegScavenger *RS) const {
170   assert(SPAdj == 0 && "Unexpected");
171
172   unsigned i = 0;
173   MachineInstr &MI = *II;
174   while (!MI.getOperand(i).isFrameIndex()) {
175     ++i;
176     assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
177   }
178
179   int FrameIndex = MI.getOperand(i).getFrameIndex();
180
181   // Addressable stack objects are accessed using neg. offsets from %fp
182   MachineFunction &MF = *MI.getParent()->getParent();
183   int Offset = MF.getFrameInfo()->getObjectOffset(FrameIndex) +
184                MI.getOperand(i+1).getImmedValue();
185
186   // Replace frame index with a frame pointer reference.
187   if (Offset >= -4096 && Offset <= 4095) {
188     // If the offset is small enough to fit in the immediate field, directly
189     // encode it.
190     MI.getOperand(i).ChangeToRegister(SP::I6, false);
191     MI.getOperand(i+1).ChangeToImmediate(Offset);
192   } else {
193     // Otherwise, emit a G1 = SETHI %hi(offset).  FIXME: it would be better to 
194     // scavenge a register here instead of reserving G1 all of the time.
195     unsigned OffHi = (unsigned)Offset >> 10U;
196     BuildMI(*MI.getParent(), II, TII.get(SP::SETHIi), SP::G1).addImm(OffHi);
197     // Emit G1 = G1 + I6
198     BuildMI(*MI.getParent(), II, TII.get(SP::ADDrr), SP::G1).addReg(SP::G1)
199       .addReg(SP::I6);
200     // Insert: G1+%lo(offset) into the user.
201     MI.getOperand(i).ChangeToRegister(SP::G1, false);
202     MI.getOperand(i+1).ChangeToImmediate(Offset & ((1 << 10)-1));
203   }
204 }
205
206 void SparcRegisterInfo::
207 processFunctionBeforeFrameFinalized(MachineFunction &MF) const {}
208
209 void SparcRegisterInfo::emitPrologue(MachineFunction &MF) const {
210   MachineBasicBlock &MBB = MF.front();
211   MachineFrameInfo *MFI = MF.getFrameInfo();
212
213   // Get the number of bytes to allocate from the FrameInfo
214   int NumBytes = (int) MFI->getStackSize();
215
216   // Emit the correct save instruction based on the number of bytes in
217   // the frame. Minimum stack frame size according to V8 ABI is:
218   //   16 words for register window spill
219   //    1 word for address of returned aggregate-value
220   // +  6 words for passing parameters on the stack
221   // ----------
222   //   23 words * 4 bytes per word = 92 bytes
223   NumBytes += 92;
224   // Round up to next doubleword boundary -- a double-word boundary
225   // is required by the ABI.
226   NumBytes = (NumBytes + 7) & ~7;
227   NumBytes = -NumBytes;
228   
229   if (NumBytes >= -4096) {
230     BuildMI(MBB, MBB.begin(), TII.get(SP::SAVEri),
231             SP::O6).addImm(NumBytes).addReg(SP::O6);
232   } else {
233     MachineBasicBlock::iterator InsertPt = MBB.begin();
234     // Emit this the hard way.  This clobbers G1 which we always know is 
235     // available here.
236     unsigned OffHi = (unsigned)NumBytes >> 10U;
237     BuildMI(MBB, InsertPt, TII.get(SP::SETHIi), SP::G1).addImm(OffHi);
238     // Emit G1 = G1 + I6
239     BuildMI(MBB, InsertPt, TII.get(SP::ORri), SP::G1)
240       .addReg(SP::G1).addImm(NumBytes & ((1 << 10)-1));
241     BuildMI(MBB, InsertPt, TII.get(SP::SAVErr), SP::O6)
242       .addReg(SP::O6).addReg(SP::G1);
243   }
244 }
245
246 void SparcRegisterInfo::emitEpilogue(MachineFunction &MF,
247                                      MachineBasicBlock &MBB) const {
248   MachineBasicBlock::iterator MBBI = prior(MBB.end());
249   assert(MBBI->getOpcode() == SP::RETL &&
250          "Can only put epilog before 'retl' instruction!");
251   BuildMI(MBB, MBBI, TII.get(SP::RESTORErr), SP::G0).addReg(SP::G0)
252     .addReg(SP::G0);
253 }
254
255 unsigned SparcRegisterInfo::getRARegister() const {
256   assert(0 && "What is the return address register");
257   return 0;
258 }
259
260 unsigned SparcRegisterInfo::getFrameRegister(MachineFunction &MF) const {
261   assert(0 && "What is the frame register");
262   return SP::G1;
263 }
264
265 unsigned SparcRegisterInfo::getEHExceptionRegister() const {
266   assert(0 && "What is the exception register");
267   return 0;
268 }
269
270 unsigned SparcRegisterInfo::getEHHandlerRegister() const {
271   assert(0 && "What is the exception handler register");
272   return 0;
273 }
274
275 #include "SparcGenRegisterInfo.inc"
276