Fix comment order
[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 TargetRegisterInfo 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/Support/ErrorHandling.h"
22 #include "llvm/Target/TargetInstrInfo.h"
23 #include "llvm/Type.h"
24 #include "llvm/ADT/BitVector.h"
25 #include "llvm/ADT/STLExtras.h"
26 using namespace llvm;
27
28 SparcRegisterInfo::SparcRegisterInfo(SparcSubtarget &st,
29                                      const TargetInstrInfo &tii)
30   : SparcGenRegisterInfo(SP::ADJCALLSTACKDOWN, SP::ADJCALLSTACKUP),
31     Subtarget(st), TII(tii) {
32 }
33
34 const unsigned* SparcRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF)
35                                                                          const {
36   static const unsigned CalleeSavedRegs[] = { 0 };
37   return CalleeSavedRegs;
38 }
39
40 BitVector SparcRegisterInfo::getReservedRegs(const MachineFunction &MF) const {
41   BitVector Reserved(getNumRegs());
42   Reserved.set(SP::G2);
43   Reserved.set(SP::G3);
44   Reserved.set(SP::G4);
45   Reserved.set(SP::O6);
46   Reserved.set(SP::I6);
47   Reserved.set(SP::I7);
48   Reserved.set(SP::G0);
49   Reserved.set(SP::G5);
50   Reserved.set(SP::G6);
51   Reserved.set(SP::G7);
52   return Reserved;
53 }
54
55 bool SparcRegisterInfo::hasFP(const MachineFunction &MF) const {
56   return false;
57 }
58
59 void SparcRegisterInfo::
60 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
61                               MachineBasicBlock::iterator I) const {
62   MachineInstr &MI = *I;
63   DebugLoc dl = MI.getDebugLoc();
64   int Size = MI.getOperand(0).getImm();
65   if (MI.getOpcode() == SP::ADJCALLSTACKDOWN)
66     Size = -Size;
67   if (Size)
68     BuildMI(MBB, I, dl, TII.get(SP::ADDri), SP::O6).addReg(SP::O6).addImm(Size);
69   MBB.erase(I);
70 }
71
72 unsigned
73 SparcRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
74                                        int SPAdj, FrameIndexValue *Value,
75                                        RegScavenger *RS) const {
76   assert(SPAdj == 0 && "Unexpected");
77
78   unsigned i = 0;
79   MachineInstr &MI = *II;
80   DebugLoc dl = MI.getDebugLoc();
81   while (!MI.getOperand(i).isFI()) {
82     ++i;
83     assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
84   }
85
86   int FrameIndex = MI.getOperand(i).getIndex();
87
88   // Addressable stack objects are accessed using neg. offsets from %fp
89   MachineFunction &MF = *MI.getParent()->getParent();
90   int Offset = MF.getFrameInfo()->getObjectOffset(FrameIndex) +
91                MI.getOperand(i+1).getImm();
92
93   // Replace frame index with a frame pointer reference.
94   if (Offset >= -4096 && Offset <= 4095) {
95     // If the offset is small enough to fit in the immediate field, directly
96     // encode it.
97     MI.getOperand(i).ChangeToRegister(SP::I6, false);
98     MI.getOperand(i+1).ChangeToImmediate(Offset);
99   } else {
100     // Otherwise, emit a G1 = SETHI %hi(offset).  FIXME: it would be better to 
101     // scavenge a register here instead of reserving G1 all of the time.
102     unsigned OffHi = (unsigned)Offset >> 10U;
103     BuildMI(*MI.getParent(), II, dl, TII.get(SP::SETHIi), SP::G1).addImm(OffHi);
104     // Emit G1 = G1 + I6
105     BuildMI(*MI.getParent(), II, dl, TII.get(SP::ADDrr), SP::G1).addReg(SP::G1)
106       .addReg(SP::I6);
107     // Insert: G1+%lo(offset) into the user.
108     MI.getOperand(i).ChangeToRegister(SP::G1, false);
109     MI.getOperand(i+1).ChangeToImmediate(Offset & ((1 << 10)-1));
110   }
111   return 0;
112 }
113
114 void SparcRegisterInfo::
115 processFunctionBeforeFrameFinalized(MachineFunction &MF) const {}
116
117 void SparcRegisterInfo::emitPrologue(MachineFunction &MF) const {
118   MachineBasicBlock &MBB = MF.front();
119   MachineFrameInfo *MFI = MF.getFrameInfo();
120   MachineBasicBlock::iterator MBBI = MBB.begin();
121   DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
122
123   // Get the number of bytes to allocate from the FrameInfo
124   int NumBytes = (int) MFI->getStackSize();
125
126   // Emit the correct save instruction based on the number of bytes in
127   // the frame. Minimum stack frame size according to V8 ABI is:
128   //   16 words for register window spill
129   //    1 word for address of returned aggregate-value
130   // +  6 words for passing parameters on the stack
131   // ----------
132   //   23 words * 4 bytes per word = 92 bytes
133   NumBytes += 92;
134
135   // Round up to next doubleword boundary -- a double-word boundary
136   // is required by the ABI.
137   NumBytes = (NumBytes + 7) & ~7;
138   NumBytes = -NumBytes;
139   
140   if (NumBytes >= -4096) {
141     BuildMI(MBB, MBBI, dl, TII.get(SP::SAVEri), SP::O6)
142       .addReg(SP::O6).addImm(NumBytes);
143   } else {
144     // Emit this the hard way.  This clobbers G1 which we always know is 
145     // available here.
146     unsigned OffHi = (unsigned)NumBytes >> 10U;
147     BuildMI(MBB, MBBI, dl, TII.get(SP::SETHIi), SP::G1).addImm(OffHi);
148     // Emit G1 = G1 + I6
149     BuildMI(MBB, MBBI, dl, TII.get(SP::ORri), SP::G1)
150       .addReg(SP::G1).addImm(NumBytes & ((1 << 10)-1));
151     BuildMI(MBB, MBBI, dl, TII.get(SP::SAVErr), SP::O6)
152       .addReg(SP::O6).addReg(SP::G1);
153   }
154 }
155
156 void SparcRegisterInfo::emitEpilogue(MachineFunction &MF,
157                                      MachineBasicBlock &MBB) const {
158   MachineBasicBlock::iterator MBBI = prior(MBB.end());
159   DebugLoc dl = MBBI->getDebugLoc();
160   assert(MBBI->getOpcode() == SP::RETL &&
161          "Can only put epilog before 'retl' instruction!");
162   BuildMI(MBB, MBBI, dl, TII.get(SP::RESTORErr), SP::G0).addReg(SP::G0)
163     .addReg(SP::G0);
164 }
165
166 unsigned SparcRegisterInfo::getRARegister() const {
167   return SP::I7;
168 }
169
170 unsigned SparcRegisterInfo::getFrameRegister(const MachineFunction &MF) const {
171   return SP::I6;
172 }
173
174 unsigned SparcRegisterInfo::getEHExceptionRegister() const {
175   llvm_unreachable("What is the exception register");
176   return 0;
177 }
178
179 unsigned SparcRegisterInfo::getEHHandlerRegister() const {
180   llvm_unreachable("What is the exception handler register");
181   return 0;
182 }
183
184 int SparcRegisterInfo::getDwarfRegNum(unsigned RegNum, bool isEH) const {
185   return SparcGenRegisterInfo::getDwarfRegNumFull(RegNum, 0);
186 }
187
188 #include "SparcGenRegisterInfo.inc"
189