Add new shorter predicates for testing machine operands for various types:
[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 is distributed under the University of Illinois Open Source
6 // 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, bool isKill, 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, isKill);
41   else if (RC == SP::FPRegsRegisterClass)
42     BuildMI(MBB, I, TII.get(SP::STFri)).addFrameIndex(FI).addImm(0)
43       .addReg(SrcReg, false, false, isKill);
44   else if (RC == SP::DFPRegsRegisterClass)
45     BuildMI(MBB, I, TII.get(SP::STDFri)).addFrameIndex(FI).addImm(0)
46       .addReg(SrcReg, false, false, isKill);
47   else
48     assert(0 && "Can't store this register to stack slot");
49 }
50
51 void SparcRegisterInfo::storeRegToAddr(MachineFunction &MF, unsigned SrcReg,
52                                        bool isKill,
53                                        SmallVectorImpl<MachineOperand> &Addr,
54                                        const TargetRegisterClass *RC,
55                                  SmallVectorImpl<MachineInstr*> &NewMIs) const {
56   unsigned Opc = 0;
57   if (RC == SP::IntRegsRegisterClass)
58     Opc = SP::STri;
59   else if (RC == SP::FPRegsRegisterClass)
60     Opc = SP::STFri;
61   else if (RC == SP::DFPRegsRegisterClass)
62     Opc = SP::STDFri;
63   else
64     assert(0 && "Can't load this register");
65   MachineInstrBuilder MIB = BuildMI(TII.get(Opc));
66   for (unsigned i = 0, e = Addr.size(); i != e; ++i) {
67     MachineOperand &MO = Addr[i];
68     if (MO.isRegister())
69       MIB.addReg(MO.getReg());
70     else if (MO.isImmediate())
71       MIB.addImm(MO.getImm());
72     else {
73       assert(MO.isFI());
74       MIB.addFrameIndex(MO.getIndex());
75     }
76   }
77   MIB.addReg(SrcReg, false, false, isKill);
78   NewMIs.push_back(MIB);
79   return;
80 }
81
82 void SparcRegisterInfo::
83 loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
84                      unsigned DestReg, int FI,
85                      const TargetRegisterClass *RC) const {
86   if (RC == SP::IntRegsRegisterClass)
87     BuildMI(MBB, I, TII.get(SP::LDri), DestReg).addFrameIndex(FI).addImm(0);
88   else if (RC == SP::FPRegsRegisterClass)
89     BuildMI(MBB, I, TII.get(SP::LDFri), DestReg).addFrameIndex(FI).addImm(0);
90   else if (RC == SP::DFPRegsRegisterClass)
91     BuildMI(MBB, I, TII.get(SP::LDDFri), DestReg).addFrameIndex(FI).addImm(0);
92   else
93     assert(0 && "Can't load this register from stack slot");
94 }
95
96 void SparcRegisterInfo::loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
97                                         SmallVectorImpl<MachineOperand> &Addr,
98                                         const TargetRegisterClass *RC,
99                                  SmallVectorImpl<MachineInstr*> &NewMIs) const {
100   unsigned Opc = 0;
101   if (RC == SP::IntRegsRegisterClass)
102     Opc = SP::LDri;
103   else if (RC == SP::FPRegsRegisterClass)
104     Opc = SP::LDFri;
105   else if (RC == SP::DFPRegsRegisterClass)
106     Opc = SP::LDDFri;
107   else
108     assert(0 && "Can't load this register");
109   MachineInstrBuilder MIB = BuildMI(TII.get(Opc), DestReg);
110   for (unsigned i = 0, e = Addr.size(); i != e; ++i) {
111     MachineOperand &MO = Addr[i];
112     if (MO.isReg())
113       MIB.addReg(MO.getReg());
114     else if (MO.isImm())
115       MIB.addImm(MO.getImm());
116     else {
117       assert(MO.isFI());
118       MIB.addFrameIndex(MO.getIndex());
119     }
120   }
121   NewMIs.push_back(MIB);
122   return;
123 }
124
125 void SparcRegisterInfo::copyRegToReg(MachineBasicBlock &MBB,
126                                      MachineBasicBlock::iterator I,
127                                      unsigned DestReg, unsigned SrcReg,
128                                      const TargetRegisterClass *DestRC,
129                                      const TargetRegisterClass *SrcRC) const {
130   if (DestRC != SrcRC) {
131     cerr << "Not yet supported!";
132     abort();
133   }
134
135   if (DestRC == SP::IntRegsRegisterClass)
136     BuildMI(MBB, I, TII.get(SP::ORrr), DestReg).addReg(SP::G0).addReg(SrcReg);
137   else if (DestRC == SP::FPRegsRegisterClass)
138     BuildMI(MBB, I, TII.get(SP::FMOVS), DestReg).addReg(SrcReg);
139   else if (DestRC == SP::DFPRegsRegisterClass)
140     BuildMI(MBB, I, TII.get(Subtarget.isV9() ? SP::FMOVD : SP::FpMOVD),DestReg)
141       .addReg(SrcReg);
142   else
143     assert (0 && "Can't copy this register");
144 }
145
146 void SparcRegisterInfo::reMaterialize(MachineBasicBlock &MBB,
147                                       MachineBasicBlock::iterator I,
148                                       unsigned DestReg,
149                                       const MachineInstr *Orig) const {
150   MachineInstr *MI = Orig->clone();
151   MI->getOperand(0).setReg(DestReg);
152   MBB.insert(I, MI);
153 }
154
155 MachineInstr *SparcRegisterInfo::foldMemoryOperand(MachineInstr* MI,
156                                                  SmallVectorImpl<unsigned> &Ops,
157                                                  int FI) const {
158   if (Ops.size() != 1) return NULL;
159
160   unsigned OpNum = Ops[0];
161   bool isFloat = false;
162   MachineInstr *NewMI = NULL;
163   switch (MI->getOpcode()) {
164   case SP::ORrr:
165     if (MI->getOperand(1).isRegister() && MI->getOperand(1).getReg() == SP::G0&&
166         MI->getOperand(0).isRegister() && MI->getOperand(2).isRegister()) {
167       if (OpNum == 0)    // COPY -> STORE
168         NewMI = BuildMI(TII.get(SP::STri)).addFrameIndex(FI).addImm(0)
169                                    .addReg(MI->getOperand(2).getReg());
170       else               // COPY -> LOAD
171         NewMI = BuildMI(TII.get(SP::LDri), MI->getOperand(0).getReg())
172                       .addFrameIndex(FI).addImm(0);
173     }
174     break;
175   case SP::FMOVS:
176     isFloat = true;
177     // FALLTHROUGH
178   case SP::FMOVD:
179     if (OpNum == 0)  // COPY -> STORE
180       NewMI = BuildMI(TII.get(isFloat ? SP::STFri : SP::STDFri))
181                .addFrameIndex(FI).addImm(0).addReg(MI->getOperand(1).getReg());
182     else             // COPY -> LOAD
183       NewMI = BuildMI(TII.get(isFloat ? SP::LDFri : SP::LDDFri),
184                      MI->getOperand(0).getReg()).addFrameIndex(FI).addImm(0);
185     break;
186   }
187
188   if (NewMI)
189     NewMI->copyKillDeadInfo(MI);
190   return NewMI;
191 }
192
193 const unsigned* SparcRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF)
194                                                                          const {
195   static const unsigned CalleeSavedRegs[] = { 0 };
196   return CalleeSavedRegs;
197 }
198
199 BitVector SparcRegisterInfo::getReservedRegs(const MachineFunction &MF) const {
200   BitVector Reserved(getNumRegs());
201   Reserved.set(SP::G2);
202   Reserved.set(SP::G3);
203   Reserved.set(SP::G4);
204   Reserved.set(SP::O6);
205   Reserved.set(SP::I6);
206   Reserved.set(SP::I7);
207   Reserved.set(SP::G0);
208   Reserved.set(SP::G5);
209   Reserved.set(SP::G6);
210   Reserved.set(SP::G7);
211   return Reserved;
212 }
213
214
215 const TargetRegisterClass* const*
216 SparcRegisterInfo::getCalleeSavedRegClasses(const MachineFunction *MF) const {
217   static const TargetRegisterClass * const CalleeSavedRegClasses[] = { 0 };
218   return CalleeSavedRegClasses;
219 }
220
221 bool SparcRegisterInfo::hasFP(const MachineFunction &MF) const {
222   return false;
223 }
224
225 void SparcRegisterInfo::
226 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
227                               MachineBasicBlock::iterator I) const {
228   MachineInstr &MI = *I;
229   int Size = MI.getOperand(0).getImm();
230   if (MI.getOpcode() == SP::ADJCALLSTACKDOWN)
231     Size = -Size;
232   if (Size)
233     BuildMI(MBB, I, TII.get(SP::ADDri), SP::O6).addReg(SP::O6).addImm(Size);
234   MBB.erase(I);
235 }
236
237 void SparcRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
238                                             int SPAdj, RegScavenger *RS) const {
239   assert(SPAdj == 0 && "Unexpected");
240
241   unsigned i = 0;
242   MachineInstr &MI = *II;
243   while (!MI.getOperand(i).isFrameIndex()) {
244     ++i;
245     assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
246   }
247
248   int FrameIndex = MI.getOperand(i).getIndex();
249
250   // Addressable stack objects are accessed using neg. offsets from %fp
251   MachineFunction &MF = *MI.getParent()->getParent();
252   int Offset = MF.getFrameInfo()->getObjectOffset(FrameIndex) +
253                MI.getOperand(i+1).getImm();
254
255   // Replace frame index with a frame pointer reference.
256   if (Offset >= -4096 && Offset <= 4095) {
257     // If the offset is small enough to fit in the immediate field, directly
258     // encode it.
259     MI.getOperand(i).ChangeToRegister(SP::I6, false);
260     MI.getOperand(i+1).ChangeToImmediate(Offset);
261   } else {
262     // Otherwise, emit a G1 = SETHI %hi(offset).  FIXME: it would be better to 
263     // scavenge a register here instead of reserving G1 all of the time.
264     unsigned OffHi = (unsigned)Offset >> 10U;
265     BuildMI(*MI.getParent(), II, TII.get(SP::SETHIi), SP::G1).addImm(OffHi);
266     // Emit G1 = G1 + I6
267     BuildMI(*MI.getParent(), II, TII.get(SP::ADDrr), SP::G1).addReg(SP::G1)
268       .addReg(SP::I6);
269     // Insert: G1+%lo(offset) into the user.
270     MI.getOperand(i).ChangeToRegister(SP::G1, false);
271     MI.getOperand(i+1).ChangeToImmediate(Offset & ((1 << 10)-1));
272   }
273 }
274
275 void SparcRegisterInfo::
276 processFunctionBeforeFrameFinalized(MachineFunction &MF) const {}
277
278 void SparcRegisterInfo::emitPrologue(MachineFunction &MF) const {
279   MachineBasicBlock &MBB = MF.front();
280   MachineFrameInfo *MFI = MF.getFrameInfo();
281
282   // Get the number of bytes to allocate from the FrameInfo
283   int NumBytes = (int) MFI->getStackSize();
284
285   // Emit the correct save instruction based on the number of bytes in
286   // the frame. Minimum stack frame size according to V8 ABI is:
287   //   16 words for register window spill
288   //    1 word for address of returned aggregate-value
289   // +  6 words for passing parameters on the stack
290   // ----------
291   //   23 words * 4 bytes per word = 92 bytes
292   NumBytes += 92;
293   // Round up to next doubleword boundary -- a double-word boundary
294   // is required by the ABI.
295   NumBytes = (NumBytes + 7) & ~7;
296   NumBytes = -NumBytes;
297   
298   if (NumBytes >= -4096) {
299     BuildMI(MBB, MBB.begin(), TII.get(SP::SAVEri),
300             SP::O6).addImm(NumBytes).addReg(SP::O6);
301   } else {
302     MachineBasicBlock::iterator InsertPt = MBB.begin();
303     // Emit this the hard way.  This clobbers G1 which we always know is 
304     // available here.
305     unsigned OffHi = (unsigned)NumBytes >> 10U;
306     BuildMI(MBB, InsertPt, TII.get(SP::SETHIi), SP::G1).addImm(OffHi);
307     // Emit G1 = G1 + I6
308     BuildMI(MBB, InsertPt, TII.get(SP::ORri), SP::G1)
309       .addReg(SP::G1).addImm(NumBytes & ((1 << 10)-1));
310     BuildMI(MBB, InsertPt, TII.get(SP::SAVErr), SP::O6)
311       .addReg(SP::O6).addReg(SP::G1);
312   }
313 }
314
315 void SparcRegisterInfo::emitEpilogue(MachineFunction &MF,
316                                      MachineBasicBlock &MBB) const {
317   MachineBasicBlock::iterator MBBI = prior(MBB.end());
318   assert(MBBI->getOpcode() == SP::RETL &&
319          "Can only put epilog before 'retl' instruction!");
320   BuildMI(MBB, MBBI, TII.get(SP::RESTORErr), SP::G0).addReg(SP::G0)
321     .addReg(SP::G0);
322 }
323
324 unsigned SparcRegisterInfo::getRARegister() const {
325   assert(0 && "What is the return address register");
326   return 0;
327 }
328
329 unsigned SparcRegisterInfo::getFrameRegister(MachineFunction &MF) const {
330   assert(0 && "What is the frame register");
331   return SP::G1;
332 }
333
334 unsigned SparcRegisterInfo::getEHExceptionRegister() const {
335   assert(0 && "What is the exception register");
336   return 0;
337 }
338
339 unsigned SparcRegisterInfo::getEHHandlerRegister() const {
340   assert(0 && "What is the exception handler register");
341   return 0;
342 }
343
344 int SparcRegisterInfo::getDwarfRegNum(unsigned RegNum, bool isEH) const {
345   assert(0 && "What is the dwarf register number");
346   return -1;
347 }
348
349 #include "SparcGenRegisterInfo.inc"
350