Remove the TargetMachine forwards for TargetSubtargetInfo based
[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 #include "MSP430RegisterInfo.h"
15 #include "MSP430.h"
16 #include "MSP430MachineFunctionInfo.h"
17 #include "MSP430TargetMachine.h"
18 #include "llvm/ADT/BitVector.h"
19 #include "llvm/CodeGen/MachineFrameInfo.h"
20 #include "llvm/CodeGen/MachineFunction.h"
21 #include "llvm/CodeGen/MachineInstrBuilder.h"
22 #include "llvm/IR/Function.h"
23 #include "llvm/Support/ErrorHandling.h"
24 #include "llvm/Target/TargetMachine.h"
25 #include "llvm/Target/TargetOptions.h"
26
27 using namespace llvm;
28
29 #define DEBUG_TYPE "msp430-reg-info"
30
31 #define GET_REGINFO_TARGET_DESC
32 #include "MSP430GenRegisterInfo.inc"
33
34 // FIXME: Provide proper call frame setup / destroy opcodes.
35 MSP430RegisterInfo::MSP430RegisterInfo()
36   : MSP430GenRegisterInfo(MSP430::PCW) {}
37
38 const MCPhysReg*
39 MSP430RegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {
40   const TargetFrameLowering *TFI =
41       MF->getTarget().getSubtargetImpl()->getFrameLowering();
42   const Function* F = MF->getFunction();
43   static const MCPhysReg CalleeSavedRegs[] = {
44     MSP430::FPW, MSP430::R5W, MSP430::R6W, MSP430::R7W,
45     MSP430::R8W, MSP430::R9W, MSP430::R10W, MSP430::R11W,
46     0
47   };
48   static const MCPhysReg CalleeSavedRegsFP[] = {
49     MSP430::R5W, MSP430::R6W, MSP430::R7W,
50     MSP430::R8W, MSP430::R9W, MSP430::R10W, MSP430::R11W,
51     0
52   };
53   static const MCPhysReg CalleeSavedRegsIntr[] = {
54     MSP430::FPW,  MSP430::R5W,  MSP430::R6W,  MSP430::R7W,
55     MSP430::R8W,  MSP430::R9W,  MSP430::R10W, MSP430::R11W,
56     MSP430::R12W, MSP430::R13W, MSP430::R14W, MSP430::R15W,
57     0
58   };
59   static const MCPhysReg CalleeSavedRegsIntrFP[] = {
60     MSP430::R5W,  MSP430::R6W,  MSP430::R7W,
61     MSP430::R8W,  MSP430::R9W,  MSP430::R10W, MSP430::R11W,
62     MSP430::R12W, MSP430::R13W, MSP430::R14W, MSP430::R15W,
63     0
64   };
65
66   if (TFI->hasFP(*MF))
67     return (F->getCallingConv() == CallingConv::MSP430_INTR ?
68             CalleeSavedRegsIntrFP : CalleeSavedRegsFP);
69   else
70     return (F->getCallingConv() == CallingConv::MSP430_INTR ?
71             CalleeSavedRegsIntr : CalleeSavedRegs);
72
73 }
74
75 BitVector MSP430RegisterInfo::getReservedRegs(const MachineFunction &MF) const {
76   BitVector Reserved(getNumRegs());
77   const TargetFrameLowering *TFI =
78       MF.getTarget().getSubtargetImpl()->getFrameLowering();
79
80   // Mark 4 special registers with subregisters as reserved.
81   Reserved.set(MSP430::PCB);
82   Reserved.set(MSP430::SPB);
83   Reserved.set(MSP430::SRB);
84   Reserved.set(MSP430::CGB);
85   Reserved.set(MSP430::PCW);
86   Reserved.set(MSP430::SPW);
87   Reserved.set(MSP430::SRW);
88   Reserved.set(MSP430::CGW);
89
90   // Mark frame pointer as reserved if needed.
91   if (TFI->hasFP(MF)) {
92     Reserved.set(MSP430::FPB);
93     Reserved.set(MSP430::FPW);
94   }
95
96   return Reserved;
97 }
98
99 const TargetRegisterClass *
100 MSP430RegisterInfo::getPointerRegClass(const MachineFunction &MF, unsigned Kind)
101                                                                          const {
102   return &MSP430::GR16RegClass;
103 }
104
105 void
106 MSP430RegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
107                                         int SPAdj, unsigned FIOperandNum,
108                                         RegScavenger *RS) const {
109   assert(SPAdj == 0 && "Unexpected");
110
111   MachineInstr &MI = *II;
112   MachineBasicBlock &MBB = *MI.getParent();
113   MachineFunction &MF = *MBB.getParent();
114   const TargetFrameLowering *TFI =
115       MF.getTarget().getSubtargetImpl()->getFrameLowering();
116   DebugLoc dl = MI.getDebugLoc();
117   int FrameIndex = MI.getOperand(FIOperandNum).getIndex();
118
119   unsigned BasePtr = (TFI->hasFP(MF) ? MSP430::FPW : MSP430::SPW);
120   int Offset = MF.getFrameInfo()->getObjectOffset(FrameIndex);
121
122   // Skip the saved PC
123   Offset += 2;
124
125   if (!TFI->hasFP(MF))
126     Offset += MF.getFrameInfo()->getStackSize();
127   else
128     Offset += 2; // Skip the saved FPW
129
130   // Fold imm into offset
131   Offset += MI.getOperand(FIOperandNum + 1).getImm();
132
133   if (MI.getOpcode() == MSP430::ADD16ri) {
134     // This is actually "load effective address" of the stack slot
135     // instruction. We have only two-address instructions, thus we need to
136     // expand it into mov + add
137     const TargetInstrInfo &TII =
138         *MF.getTarget().getSubtargetImpl()->getInstrInfo();
139
140     MI.setDesc(TII.get(MSP430::MOV16rr));
141     MI.getOperand(FIOperandNum).ChangeToRegister(BasePtr, false);
142
143     if (Offset == 0)
144       return;
145
146     // We need to materialize the offset via add instruction.
147     unsigned DstReg = MI.getOperand(0).getReg();
148     if (Offset < 0)
149       BuildMI(MBB, std::next(II), dl, TII.get(MSP430::SUB16ri), DstReg)
150         .addReg(DstReg).addImm(-Offset);
151     else
152       BuildMI(MBB, std::next(II), dl, TII.get(MSP430::ADD16ri), DstReg)
153         .addReg(DstReg).addImm(Offset);
154
155     return;
156   }
157
158   MI.getOperand(FIOperandNum).ChangeToRegister(BasePtr, false);
159   MI.getOperand(FIOperandNum + 1).ChangeToImmediate(Offset);
160 }
161
162 unsigned MSP430RegisterInfo::getFrameRegister(const MachineFunction &MF) const {
163   const TargetFrameLowering *TFI =
164       MF.getTarget().getSubtargetImpl()->getFrameLowering();
165
166   return TFI->hasFP(MF) ? MSP430::FPW : MSP430::SPW;
167 }