Change the Value argument to eliminateFrameIndex to a type-tagged value. This
[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 using namespace llvm;
30
31 // FIXME: Provide proper call frame setup / destroy opcodes.
32 MSP430RegisterInfo::MSP430RegisterInfo(MSP430TargetMachine &tm,
33                                        const TargetInstrInfo &tii)
34   : MSP430GenRegisterInfo(MSP430::ADJCALLSTACKDOWN, MSP430::ADJCALLSTACKUP),
35     TM(tm), TII(tii) {
36   StackAlign = TM.getFrameInfo()->getStackAlignment();
37 }
38
39 const unsigned*
40 MSP430RegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {
41   const Function* F = MF->getFunction();
42   static const unsigned CalleeSavedRegs[] = {
43     MSP430::FPW, MSP430::R5W, MSP430::R6W, MSP430::R7W,
44     MSP430::R8W, MSP430::R9W, MSP430::R10W, MSP430::R11W,
45     0
46   };
47   static const unsigned CalleeSavedRegsFP[] = {
48     MSP430::R5W, MSP430::R6W, MSP430::R7W,
49     MSP430::R8W, MSP430::R9W, MSP430::R10W, MSP430::R11W,
50     0
51   };
52   static const unsigned CalleeSavedRegsIntr[] = {
53     MSP430::FPW,  MSP430::R5W,  MSP430::R6W,  MSP430::R7W,
54     MSP430::R8W,  MSP430::R9W,  MSP430::R10W, MSP430::R11W,
55     MSP430::R12W, MSP430::R13W, MSP430::R14W, MSP430::R15W,
56     0
57   };
58   static const unsigned CalleeSavedRegsIntrFP[] = {
59     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
65   if (hasFP(*MF))
66     return (F->getCallingConv() == CallingConv::MSP430_INTR ?
67             CalleeSavedRegsIntrFP : CalleeSavedRegsFP);
68   else
69     return (F->getCallingConv() == CallingConv::MSP430_INTR ?
70             CalleeSavedRegsIntr : CalleeSavedRegs);
71
72 }
73
74 const TargetRegisterClass *const *
75 MSP430RegisterInfo::getCalleeSavedRegClasses(const MachineFunction *MF) const {
76   const Function* F = MF->getFunction();
77   static const TargetRegisterClass * const CalleeSavedRegClasses[] = {
78     &MSP430::GR16RegClass, &MSP430::GR16RegClass,
79     &MSP430::GR16RegClass, &MSP430::GR16RegClass,
80     &MSP430::GR16RegClass, &MSP430::GR16RegClass,
81     &MSP430::GR16RegClass, &MSP430::GR16RegClass,
82     0
83   };
84   static const TargetRegisterClass * const CalleeSavedRegClassesFP[] = {
85     &MSP430::GR16RegClass, &MSP430::GR16RegClass,
86     &MSP430::GR16RegClass, &MSP430::GR16RegClass,
87     &MSP430::GR16RegClass, &MSP430::GR16RegClass,
88     &MSP430::GR16RegClass, 0
89   };
90   static const TargetRegisterClass * const CalleeSavedRegClassesIntr[] = {
91     &MSP430::GR16RegClass, &MSP430::GR16RegClass,
92     &MSP430::GR16RegClass, &MSP430::GR16RegClass,
93     &MSP430::GR16RegClass, &MSP430::GR16RegClass,
94     &MSP430::GR16RegClass, &MSP430::GR16RegClass,
95     &MSP430::GR16RegClass, &MSP430::GR16RegClass,
96     &MSP430::GR16RegClass, &MSP430::GR16RegClass,
97     0
98   };
99   static const TargetRegisterClass * const CalleeSavedRegClassesIntrFP[] = {
100     &MSP430::GR16RegClass, &MSP430::GR16RegClass,
101     &MSP430::GR16RegClass, &MSP430::GR16RegClass,
102     &MSP430::GR16RegClass, &MSP430::GR16RegClass,
103     &MSP430::GR16RegClass, &MSP430::GR16RegClass,
104     &MSP430::GR16RegClass, &MSP430::GR16RegClass,
105     &MSP430::GR16RegClass, 0
106   };
107
108   if (hasFP(*MF))
109     return (F->getCallingConv() == CallingConv::MSP430_INTR ?
110             CalleeSavedRegClassesIntrFP : CalleeSavedRegClassesFP);
111   else
112     return (F->getCallingConv() == CallingConv::MSP430_INTR ?
113             CalleeSavedRegClassesIntr : CalleeSavedRegClasses);
114 }
115
116 BitVector MSP430RegisterInfo::getReservedRegs(const MachineFunction &MF) const {
117   BitVector Reserved(getNumRegs());
118
119   // Mark 4 special registers as reserved.
120   Reserved.set(MSP430::PCW);
121   Reserved.set(MSP430::SPW);
122   Reserved.set(MSP430::SRW);
123   Reserved.set(MSP430::CGW);
124
125   // Mark frame pointer as reserved if needed.
126   if (hasFP(MF))
127     Reserved.set(MSP430::FPW);
128
129   return Reserved;
130 }
131
132 const TargetRegisterClass *
133 MSP430RegisterInfo::getPointerRegClass(unsigned Kind) const {
134   return &MSP430::GR16RegClass;
135 }
136
137
138 bool MSP430RegisterInfo::hasFP(const MachineFunction &MF) const {
139   const MachineFrameInfo *MFI = MF.getFrameInfo();
140
141   return (NoFramePointerElim ||
142           MF.getFrameInfo()->hasVarSizedObjects() ||
143           MFI->isFrameAddressTaken());
144 }
145
146 bool MSP430RegisterInfo::hasReservedCallFrame(MachineFunction &MF) const {
147   return !MF.getFrameInfo()->hasVarSizedObjects();
148 }
149
150 void MSP430RegisterInfo::
151 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
152                               MachineBasicBlock::iterator I) const {
153   if (!hasReservedCallFrame(MF)) {
154     // If the stack pointer can be changed after prologue, turn the
155     // adjcallstackup instruction into a 'sub SPW, <amt>' and the
156     // adjcallstackdown instruction into 'add SPW, <amt>'
157     // TODO: consider using push / pop instead of sub + store / add
158     MachineInstr *Old = I;
159     uint64_t Amount = Old->getOperand(0).getImm();
160     if (Amount != 0) {
161       // We need to keep the stack aligned properly.  To do this, we round the
162       // amount of space needed for the outgoing arguments up to the next
163       // alignment boundary.
164       Amount = (Amount+StackAlign-1)/StackAlign*StackAlign;
165
166       MachineInstr *New = 0;
167       if (Old->getOpcode() == getCallFrameSetupOpcode()) {
168         New = BuildMI(MF, Old->getDebugLoc(),
169                       TII.get(MSP430::SUB16ri), MSP430::SPW)
170           .addReg(MSP430::SPW).addImm(Amount);
171       } else {
172         assert(Old->getOpcode() == getCallFrameDestroyOpcode());
173         // factor out the amount the callee already popped.
174         uint64_t CalleeAmt = Old->getOperand(1).getImm();
175         Amount -= CalleeAmt;
176         if (Amount)
177           New = BuildMI(MF, Old->getDebugLoc(),
178                         TII.get(MSP430::ADD16ri), MSP430::SPW)
179             .addReg(MSP430::SPW).addImm(Amount);
180       }
181
182       if (New) {
183         // The SRW implicit def is dead.
184         New->getOperand(3).setIsDead();
185
186         // Replace the pseudo instruction with a new instruction...
187         MBB.insert(I, New);
188       }
189     }
190   } else if (I->getOpcode() == getCallFrameDestroyOpcode()) {
191     // If we are performing frame pointer elimination and if the callee pops
192     // something off the stack pointer, add it back.
193     if (uint64_t CalleeAmt = I->getOperand(1).getImm()) {
194       MachineInstr *Old = I;
195       MachineInstr *New =
196         BuildMI(MF, Old->getDebugLoc(), TII.get(MSP430::SUB16ri),
197                 MSP430::SPW).addReg(MSP430::SPW).addImm(CalleeAmt);
198       // The SRW implicit def is dead.
199       New->getOperand(3).setIsDead();
200
201       MBB.insert(I, New);
202     }
203   }
204
205   MBB.erase(I);
206 }
207
208 unsigned
209 MSP430RegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
210                                         int SPAdj, FrameIndexValue *Value,
211                                         RegScavenger *RS) const {
212   assert(SPAdj == 0 && "Unexpected");
213
214   unsigned i = 0;
215   MachineInstr &MI = *II;
216   MachineBasicBlock &MBB = *MI.getParent();
217   MachineFunction &MF = *MBB.getParent();
218   DebugLoc dl = MI.getDebugLoc();
219   while (!MI.getOperand(i).isFI()) {
220     ++i;
221     assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
222   }
223
224   int FrameIndex = MI.getOperand(i).getIndex();
225
226   unsigned BasePtr = (hasFP(MF) ? MSP430::FPW : MSP430::SPW);
227   int Offset = MF.getFrameInfo()->getObjectOffset(FrameIndex);
228
229   // Skip the saved PC
230   Offset += 2;
231
232   if (!hasFP(MF))
233     Offset += MF.getFrameInfo()->getStackSize();
234   else
235     Offset += 2; // Skip the saved FPW
236
237   // Fold imm into offset
238   Offset += MI.getOperand(i+1).getImm();
239
240   if (MI.getOpcode() == MSP430::ADD16ri) {
241     // This is actually "load effective address" of the stack slot
242     // instruction. We have only two-address instructions, thus we need to
243     // expand it into mov + add
244
245     MI.setDesc(TII.get(MSP430::MOV16rr));
246     MI.getOperand(i).ChangeToRegister(BasePtr, false);
247
248     if (Offset == 0)
249       return 0;
250
251     // We need to materialize the offset via add instruction.
252     unsigned DstReg = MI.getOperand(0).getReg();
253     if (Offset < 0)
254       BuildMI(MBB, llvm::next(II), dl, TII.get(MSP430::SUB16ri), DstReg)
255         .addReg(DstReg).addImm(-Offset);
256     else
257       BuildMI(MBB, llvm::next(II), dl, TII.get(MSP430::ADD16ri), DstReg)
258         .addReg(DstReg).addImm(Offset);
259
260     return 0;
261   }
262
263   MI.getOperand(i).ChangeToRegister(BasePtr, false);
264   MI.getOperand(i+1).ChangeToImmediate(Offset);
265   return 0;
266 }
267
268 void
269 MSP430RegisterInfo::processFunctionBeforeFrameFinalized(MachineFunction &MF)
270                                                                          const {
271   // Create a frame entry for the FPW register that must be saved.
272   if (hasFP(MF)) {
273     int ATTRIBUTE_UNUSED FrameIdx =
274       MF.getFrameInfo()->CreateFixedObject(2, -4, true, false);
275     assert(FrameIdx == MF.getFrameInfo()->getObjectIndexBegin() &&
276            "Slot for FPW register must be last in order to be found!");
277   }
278 }
279
280
281 void MSP430RegisterInfo::emitPrologue(MachineFunction &MF) const {
282   MachineBasicBlock &MBB = MF.front();   // Prolog goes in entry BB
283   MachineFrameInfo *MFI = MF.getFrameInfo();
284   MSP430MachineFunctionInfo *MSP430FI = MF.getInfo<MSP430MachineFunctionInfo>();
285   MachineBasicBlock::iterator MBBI = MBB.begin();
286   DebugLoc DL = (MBBI != MBB.end() ? MBBI->getDebugLoc() :
287                  DebugLoc::getUnknownLoc());
288
289   // Get the number of bytes to allocate from the FrameInfo.
290   uint64_t StackSize = MFI->getStackSize();
291
292   uint64_t NumBytes = 0;
293   if (hasFP(MF)) {
294     // Calculate required stack adjustment
295     uint64_t FrameSize = StackSize - 2;
296     NumBytes = FrameSize - MSP430FI->getCalleeSavedFrameSize();
297
298     // Get the offset of the stack slot for the EBP register... which is
299     // guaranteed to be the last slot by processFunctionBeforeFrameFinalized.
300     // Update the frame offset adjustment.
301     MFI->setOffsetAdjustment(-NumBytes);
302
303     // Save FPW into the appropriate stack slot...
304     BuildMI(MBB, MBBI, DL, TII.get(MSP430::PUSH16r))
305       .addReg(MSP430::FPW, RegState::Kill);
306
307     // Update FPW with the new base value...
308     BuildMI(MBB, MBBI, DL, TII.get(MSP430::MOV16rr), MSP430::FPW)
309       .addReg(MSP430::SPW);
310
311     // Mark the FramePtr as live-in in every block except the entry.
312     for (MachineFunction::iterator I = llvm::next(MF.begin()), E = MF.end();
313          I != E; ++I)
314       I->addLiveIn(MSP430::FPW);
315
316   } else
317     NumBytes = StackSize - MSP430FI->getCalleeSavedFrameSize();
318
319   // Skip the callee-saved push instructions.
320   while (MBBI != MBB.end() && (MBBI->getOpcode() == MSP430::PUSH16r))
321     ++MBBI;
322
323   if (MBBI != MBB.end())
324     DL = MBBI->getDebugLoc();
325
326   if (NumBytes) { // adjust stack pointer: SPW -= numbytes
327     // If there is an SUB16ri of SPW immediately before this instruction, merge
328     // the two.
329     //NumBytes -= mergeSPUpdates(MBB, MBBI, true);
330     // If there is an ADD16ri or SUB16ri of SPW immediately after this
331     // instruction, merge the two instructions.
332     // mergeSPUpdatesDown(MBB, MBBI, &NumBytes);
333
334     if (NumBytes) {
335       MachineInstr *MI =
336         BuildMI(MBB, MBBI, DL, TII.get(MSP430::SUB16ri), MSP430::SPW)
337         .addReg(MSP430::SPW).addImm(NumBytes);
338       // The SRW implicit def is dead.
339       MI->getOperand(3).setIsDead();
340     }
341   }
342 }
343
344 void MSP430RegisterInfo::emitEpilogue(MachineFunction &MF,
345                                       MachineBasicBlock &MBB) const {
346   const MachineFrameInfo *MFI = MF.getFrameInfo();
347   MSP430MachineFunctionInfo *MSP430FI = MF.getInfo<MSP430MachineFunctionInfo>();
348   MachineBasicBlock::iterator MBBI = prior(MBB.end());
349   unsigned RetOpcode = MBBI->getOpcode();
350   DebugLoc DL = MBBI->getDebugLoc();
351
352   switch (RetOpcode) {
353   case MSP430::RET:
354   case MSP430::RETI: break;  // These are ok
355   default:
356     llvm_unreachable("Can only insert epilog into returning blocks");
357   }
358
359   // Get the number of bytes to allocate from the FrameInfo
360   uint64_t StackSize = MFI->getStackSize();
361   unsigned CSSize = MSP430FI->getCalleeSavedFrameSize();
362   uint64_t NumBytes = 0;
363
364   if (hasFP(MF)) {
365     // Calculate required stack adjustment
366     uint64_t FrameSize = StackSize - 2;
367     NumBytes = FrameSize - CSSize;
368
369     // pop FPW.
370     BuildMI(MBB, MBBI, DL, TII.get(MSP430::POP16r), MSP430::FPW);
371   } else
372     NumBytes = StackSize - CSSize;
373
374   // Skip the callee-saved pop instructions.
375   while (MBBI != MBB.begin()) {
376     MachineBasicBlock::iterator PI = prior(MBBI);
377     unsigned Opc = PI->getOpcode();
378     if (Opc != MSP430::POP16r && !PI->getDesc().isTerminator())
379       break;
380     --MBBI;
381   }
382
383   DL = MBBI->getDebugLoc();
384
385   // If there is an ADD16ri or SUB16ri of SPW immediately before this
386   // instruction, merge the two instructions.
387   //if (NumBytes || MFI->hasVarSizedObjects())
388   //  mergeSPUpdatesUp(MBB, MBBI, StackPtr, &NumBytes);
389
390   if (MFI->hasVarSizedObjects()) {
391     BuildMI(MBB, MBBI, DL,
392             TII.get(MSP430::MOV16rr), MSP430::SPW).addReg(MSP430::FPW);
393     if (CSSize) {
394       MachineInstr *MI =
395         BuildMI(MBB, MBBI, DL,
396                 TII.get(MSP430::SUB16ri), MSP430::SPW)
397         .addReg(MSP430::SPW).addImm(CSSize);
398       // The SRW implicit def is dead.
399       MI->getOperand(3).setIsDead();
400     }
401   } else {
402     // adjust stack pointer back: SPW += numbytes
403     if (NumBytes) {
404       MachineInstr *MI =
405         BuildMI(MBB, MBBI, DL, TII.get(MSP430::ADD16ri), MSP430::SPW)
406         .addReg(MSP430::SPW).addImm(NumBytes);
407       // The SRW implicit def is dead.
408       MI->getOperand(3).setIsDead();
409     }
410   }
411 }
412
413 unsigned MSP430RegisterInfo::getRARegister() const {
414   return MSP430::PCW;
415 }
416
417 unsigned MSP430RegisterInfo::getFrameRegister(const MachineFunction &MF) const {
418   return hasFP(MF) ? MSP430::FPW : MSP430::SPW;
419 }
420
421 int MSP430RegisterInfo::getDwarfRegNum(unsigned RegNum, bool isEH) const {
422   llvm_unreachable("Not implemented yet!");
423   return 0;
424 }
425
426 #include "MSP430GenRegisterInfo.inc"