0a2e93ac08bea76f41a3eb367146bd5865ae424a
[oota-llvm.git] / lib / Target / MSP430 / MSP430RegisterInfo.cpp
1 //===- MSP430RegisterInfo.cpp - MSP430 Register Information ---------------===//
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 MSP430 implementation of the TargetRegisterInfo class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #define DEBUG_TYPE "msp430-reg-info"
15
16 #include "MSP430.h"
17 #include "MSP430MachineFunctionInfo.h"
18 #include "MSP430RegisterInfo.h"
19 #include "MSP430TargetMachine.h"
20 #include "llvm/Function.h"
21 #include "llvm/CodeGen/MachineFrameInfo.h"
22 #include "llvm/CodeGen/MachineFunction.h"
23 #include "llvm/CodeGen/MachineInstrBuilder.h"
24 #include "llvm/Target/TargetMachine.h"
25 #include "llvm/Target/TargetOptions.h"
26 #include "llvm/ADT/BitVector.h"
27 #include "llvm/Support/ErrorHandling.h"
28
29 #define GET_REGINFO_MC_DESC
30 #define GET_REGINFO_TARGET_DESC
31 #include "MSP430GenRegisterInfo.inc"
32
33 using namespace llvm;
34
35 // FIXME: Provide proper call frame setup / destroy opcodes.
36 MSP430RegisterInfo::MSP430RegisterInfo(MSP430TargetMachine &tm,
37                                        const TargetInstrInfo &tii)
38   : MSP430GenRegisterInfo(MSP430RegDesc, MSP430RegInfoDesc,
39                           MSP430::ADJCALLSTACKDOWN, MSP430::ADJCALLSTACKUP),
40     TM(tm), TII(tii) {
41   StackAlign = TM.getFrameLowering()->getStackAlignment();
42 }
43
44 const unsigned*
45 MSP430RegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {
46   const TargetFrameLowering *TFI = MF->getTarget().getFrameLowering();
47   const Function* F = MF->getFunction();
48   static const unsigned CalleeSavedRegs[] = {
49     MSP430::FPW, MSP430::R5W, MSP430::R6W, MSP430::R7W,
50     MSP430::R8W, MSP430::R9W, MSP430::R10W, MSP430::R11W,
51     0
52   };
53   static const unsigned CalleeSavedRegsFP[] = {
54     MSP430::R5W, MSP430::R6W, MSP430::R7W,
55     MSP430::R8W, MSP430::R9W, MSP430::R10W, MSP430::R11W,
56     0
57   };
58   static const unsigned CalleeSavedRegsIntr[] = {
59     MSP430::FPW,  MSP430::R5W,  MSP430::R6W,  MSP430::R7W,
60     MSP430::R8W,  MSP430::R9W,  MSP430::R10W, MSP430::R11W,
61     MSP430::R12W, MSP430::R13W, MSP430::R14W, MSP430::R15W,
62     0
63   };
64   static const unsigned CalleeSavedRegsIntrFP[] = {
65     MSP430::R5W,  MSP430::R6W,  MSP430::R7W,
66     MSP430::R8W,  MSP430::R9W,  MSP430::R10W, MSP430::R11W,
67     MSP430::R12W, MSP430::R13W, MSP430::R14W, MSP430::R15W,
68     0
69   };
70
71   if (TFI->hasFP(*MF))
72     return (F->getCallingConv() == CallingConv::MSP430_INTR ?
73             CalleeSavedRegsIntrFP : CalleeSavedRegsFP);
74   else
75     return (F->getCallingConv() == CallingConv::MSP430_INTR ?
76             CalleeSavedRegsIntr : CalleeSavedRegs);
77
78 }
79
80 BitVector MSP430RegisterInfo::getReservedRegs(const MachineFunction &MF) const {
81   BitVector Reserved(getNumRegs());
82   const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
83
84   // Mark 4 special registers with subregisters as reserved.
85   Reserved.set(MSP430::PCB);
86   Reserved.set(MSP430::SPB);
87   Reserved.set(MSP430::SRB);
88   Reserved.set(MSP430::CGB);
89   Reserved.set(MSP430::PCW);
90   Reserved.set(MSP430::SPW);
91   Reserved.set(MSP430::SRW);
92   Reserved.set(MSP430::CGW);
93
94   // Mark frame pointer as reserved if needed.
95   if (TFI->hasFP(MF))
96     Reserved.set(MSP430::FPW);
97
98   return Reserved;
99 }
100
101 const TargetRegisterClass *
102 MSP430RegisterInfo::getPointerRegClass(unsigned Kind) const {
103   return &MSP430::GR16RegClass;
104 }
105
106 void MSP430RegisterInfo::
107 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
108                               MachineBasicBlock::iterator I) const {
109   const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
110
111   if (!TFI->hasReservedCallFrame(MF)) {
112     // If the stack pointer can be changed after prologue, turn the
113     // adjcallstackup instruction into a 'sub SPW, <amt>' and the
114     // adjcallstackdown instruction into 'add SPW, <amt>'
115     // TODO: consider using push / pop instead of sub + store / add
116     MachineInstr *Old = I;
117     uint64_t Amount = Old->getOperand(0).getImm();
118     if (Amount != 0) {
119       // We need to keep the stack aligned properly.  To do this, we round the
120       // amount of space needed for the outgoing arguments up to the next
121       // alignment boundary.
122       Amount = (Amount+StackAlign-1)/StackAlign*StackAlign;
123
124       MachineInstr *New = 0;
125       if (Old->getOpcode() == getCallFrameSetupOpcode()) {
126         New = BuildMI(MF, Old->getDebugLoc(),
127                       TII.get(MSP430::SUB16ri), MSP430::SPW)
128           .addReg(MSP430::SPW).addImm(Amount);
129       } else {
130         assert(Old->getOpcode() == getCallFrameDestroyOpcode());
131         // factor out the amount the callee already popped.
132         uint64_t CalleeAmt = Old->getOperand(1).getImm();
133         Amount -= CalleeAmt;
134         if (Amount)
135           New = BuildMI(MF, Old->getDebugLoc(),
136                         TII.get(MSP430::ADD16ri), MSP430::SPW)
137             .addReg(MSP430::SPW).addImm(Amount);
138       }
139
140       if (New) {
141         // The SRW implicit def is dead.
142         New->getOperand(3).setIsDead();
143
144         // Replace the pseudo instruction with a new instruction...
145         MBB.insert(I, New);
146       }
147     }
148   } else if (I->getOpcode() == getCallFrameDestroyOpcode()) {
149     // If we are performing frame pointer elimination and if the callee pops
150     // something off the stack pointer, add it back.
151     if (uint64_t CalleeAmt = I->getOperand(1).getImm()) {
152       MachineInstr *Old = I;
153       MachineInstr *New =
154         BuildMI(MF, Old->getDebugLoc(), TII.get(MSP430::SUB16ri),
155                 MSP430::SPW).addReg(MSP430::SPW).addImm(CalleeAmt);
156       // The SRW implicit def is dead.
157       New->getOperand(3).setIsDead();
158
159       MBB.insert(I, New);
160     }
161   }
162
163   MBB.erase(I);
164 }
165
166 void
167 MSP430RegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
168                                         int SPAdj, RegScavenger *RS) const {
169   assert(SPAdj == 0 && "Unexpected");
170
171   unsigned i = 0;
172   MachineInstr &MI = *II;
173   MachineBasicBlock &MBB = *MI.getParent();
174   MachineFunction &MF = *MBB.getParent();
175   const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
176   DebugLoc dl = MI.getDebugLoc();
177   while (!MI.getOperand(i).isFI()) {
178     ++i;
179     assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
180   }
181
182   int FrameIndex = MI.getOperand(i).getIndex();
183
184   unsigned BasePtr = (TFI->hasFP(MF) ? MSP430::FPW : MSP430::SPW);
185   int Offset = MF.getFrameInfo()->getObjectOffset(FrameIndex);
186
187   // Skip the saved PC
188   Offset += 2;
189
190   if (!TFI->hasFP(MF))
191     Offset += MF.getFrameInfo()->getStackSize();
192   else
193     Offset += 2; // Skip the saved FPW
194
195   // Fold imm into offset
196   Offset += MI.getOperand(i+1).getImm();
197
198   if (MI.getOpcode() == MSP430::ADD16ri) {
199     // This is actually "load effective address" of the stack slot
200     // instruction. We have only two-address instructions, thus we need to
201     // expand it into mov + add
202
203     MI.setDesc(TII.get(MSP430::MOV16rr));
204     MI.getOperand(i).ChangeToRegister(BasePtr, false);
205
206     if (Offset == 0)
207       return;
208
209     // We need to materialize the offset via add instruction.
210     unsigned DstReg = MI.getOperand(0).getReg();
211     if (Offset < 0)
212       BuildMI(MBB, llvm::next(II), dl, TII.get(MSP430::SUB16ri), DstReg)
213         .addReg(DstReg).addImm(-Offset);
214     else
215       BuildMI(MBB, llvm::next(II), dl, TII.get(MSP430::ADD16ri), DstReg)
216         .addReg(DstReg).addImm(Offset);
217
218     return;
219   }
220
221   MI.getOperand(i).ChangeToRegister(BasePtr, false);
222   MI.getOperand(i+1).ChangeToImmediate(Offset);
223 }
224
225 void
226 MSP430RegisterInfo::processFunctionBeforeFrameFinalized(MachineFunction &MF)
227                                                                          const {
228   const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
229
230   // Create a frame entry for the FPW register that must be saved.
231   if (TFI->hasFP(MF)) {
232     int FrameIdx = MF.getFrameInfo()->CreateFixedObject(2, -4, true);
233     (void)FrameIdx;
234     assert(FrameIdx == MF.getFrameInfo()->getObjectIndexBegin() &&
235            "Slot for FPW register must be last in order to be found!");
236   }
237 }
238
239 unsigned MSP430RegisterInfo::getRARegister() const {
240   return MSP430::PCW;
241 }
242
243 unsigned MSP430RegisterInfo::getFrameRegister(const MachineFunction &MF) const {
244   const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
245
246   return TFI->hasFP(MF) ? MSP430::FPW : MSP430::SPW;
247 }
248
249 int MSP430RegisterInfo::getDwarfRegNum(unsigned RegNum, bool isEH) const {
250   llvm_unreachable("Not implemented yet!");
251   return 0;
252 }
253
254 int MSP430RegisterInfo::getLLVMRegNum(unsigned RegNum, bool isEH) const {
255   llvm_unreachable("Not implemented yet!");
256   return 0;
257 }