91112c3d732ff6020815504e5d79ef8cafb82d71
[oota-llvm.git] / lib / Target / MSP430 / MSP430InstrInfo.cpp
1 //===- MSP430InstrInfo.cpp - MSP430 Instruction 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 TargetInstrInfo class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "MSP430.h"
15 #include "MSP430InstrInfo.h"
16 #include "MSP430MachineFunctionInfo.h"
17 #include "MSP430TargetMachine.h"
18 #include "MSP430GenInstrInfo.inc"
19 #include "llvm/Function.h"
20 #include "llvm/CodeGen/MachineFrameInfo.h"
21 #include "llvm/CodeGen/MachineInstrBuilder.h"
22 #include "llvm/CodeGen/MachineRegisterInfo.h"
23 #include "llvm/CodeGen/PseudoSourceValue.h"
24
25 using namespace llvm;
26
27 MSP430InstrInfo::MSP430InstrInfo(MSP430TargetMachine &tm)
28   : TargetInstrInfoImpl(MSP430Insts, array_lengthof(MSP430Insts)),
29     RI(tm, *this), TM(tm) {}
30
31 void MSP430InstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
32                                           MachineBasicBlock::iterator MI,
33                                     unsigned SrcReg, bool isKill, int FrameIdx,
34                                     const TargetRegisterClass *RC) const {
35   DebugLoc DL = DebugLoc::getUnknownLoc();
36   if (MI != MBB.end()) DL = MI->getDebugLoc();
37
38   if (RC == &MSP430::GR16RegClass)
39     BuildMI(MBB, MI, DL, get(MSP430::MOV16mr))
40       .addFrameIndex(FrameIdx).addImm(0)
41       .addReg(SrcReg, getKillRegState(isKill));
42   else if (RC == &MSP430::GR8RegClass)
43     BuildMI(MBB, MI, DL, get(MSP430::MOV8mr))
44       .addFrameIndex(FrameIdx).addImm(0)
45       .addReg(SrcReg, getKillRegState(isKill));
46   else
47     assert(0 && "Cannot store this register to stack slot!");
48 }
49
50 void MSP430InstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
51                                            MachineBasicBlock::iterator MI,
52                                            unsigned DestReg, int FrameIdx,
53                                            const TargetRegisterClass *RC) const{
54   DebugLoc DL = DebugLoc::getUnknownLoc();
55   if (MI != MBB.end()) DL = MI->getDebugLoc();
56
57   if (RC == &MSP430::GR16RegClass)
58     BuildMI(MBB, MI, DL, get(MSP430::MOV16rm))
59       .addReg(DestReg).addFrameIndex(FrameIdx).addImm(0);
60   else if (RC == &MSP430::GR8RegClass)
61     BuildMI(MBB, MI, DL, get(MSP430::MOV8rm))
62       .addReg(DestReg).addFrameIndex(FrameIdx).addImm(0);
63   else
64     assert(0 && "Cannot store this register to stack slot!");
65 }
66
67 bool MSP430InstrInfo::copyRegToReg(MachineBasicBlock &MBB,
68                                    MachineBasicBlock::iterator I,
69                                    unsigned DestReg, unsigned SrcReg,
70                                    const TargetRegisterClass *DestRC,
71                                    const TargetRegisterClass *SrcRC) const {
72   DebugLoc DL = DebugLoc::getUnknownLoc();
73   if (I != MBB.end()) DL = I->getDebugLoc();
74
75   if (DestRC == SrcRC) {
76     unsigned Opc;
77     if (DestRC == &MSP430::GR16RegClass) {
78       Opc = MSP430::MOV16rr;
79     } else if (DestRC == &MSP430::GR8RegClass) {
80       Opc = MSP430::MOV8rr;
81     } else {
82       return false;
83     }
84
85     BuildMI(MBB, I, DL, get(Opc), DestReg).addReg(SrcReg);
86     return true;
87   }
88
89   return false;
90 }
91
92 bool
93 MSP430InstrInfo::isMoveInstr(const MachineInstr& MI,
94                              unsigned &SrcReg, unsigned &DstReg,
95                              unsigned &SrcSubIdx, unsigned &DstSubIdx) const {
96   SrcSubIdx = DstSubIdx = 0; // No sub-registers yet.
97
98   switch (MI.getOpcode()) {
99   default:
100     return false;
101   case MSP430::MOV8rr:
102   case MSP430::MOV16rr:
103    assert(MI.getNumOperands() >= 2 &&
104            MI.getOperand(0).isReg() &&
105            MI.getOperand(1).isReg() &&
106            "invalid register-register move instruction");
107     SrcReg = MI.getOperand(1).getReg();
108     DstReg = MI.getOperand(0).getReg();
109     return true;
110   }
111 }
112
113 bool
114 MSP430InstrInfo::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
115                                            MachineBasicBlock::iterator MI,
116                                 const std::vector<CalleeSavedInfo> &CSI) const {
117   if (CSI.empty())
118     return false;
119
120   DebugLoc DL = DebugLoc::getUnknownLoc();
121   if (MI != MBB.end()) DL = MI->getDebugLoc();
122
123   MachineFunction &MF = *MBB.getParent();
124   MSP430MachineFunctionInfo *MFI = MF.getInfo<MSP430MachineFunctionInfo>();
125   MFI->setCalleeSavedFrameSize(CSI.size() * 2);
126
127   for (unsigned i = CSI.size(); i != 0; --i) {
128     unsigned Reg = CSI[i-1].getReg();
129     // Add the callee-saved register as live-in. It's killed at the spill.
130     MBB.addLiveIn(Reg);
131     BuildMI(MBB, MI, DL, get(MSP430::PUSH16r))
132       .addReg(Reg, RegState::Kill);
133   }
134   return true;
135 }
136
137 bool
138 MSP430InstrInfo::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
139                                              MachineBasicBlock::iterator MI,
140                                 const std::vector<CalleeSavedInfo> &CSI) const {
141   if (CSI.empty())
142     return false;
143
144   DebugLoc DL = DebugLoc::getUnknownLoc();
145   if (MI != MBB.end()) DL = MI->getDebugLoc();
146
147   for (unsigned i = 0, e = CSI.size(); i != e; ++i)
148     BuildMI(MBB, MI, DL, get(MSP430::POP16r), CSI[i].getReg());
149
150   return true;
151 }
152
153 unsigned
154 MSP430InstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
155                               MachineBasicBlock *FBB,
156                             const SmallVectorImpl<MachineOperand> &Cond) const {
157   // FIXME this should probably have a DebugLoc operand
158   DebugLoc dl = DebugLoc::getUnknownLoc();
159
160   // Shouldn't be a fall through.
161   assert(TBB && "InsertBranch must not be told to insert a fallthrough");
162   assert((Cond.size() == 1 || Cond.size() == 0) &&
163          "MSP430 branch conditions have one component!");
164
165   if (Cond.empty()) {
166     // Unconditional branch?
167     assert(!FBB && "Unconditional branch with multiple successors!");
168     BuildMI(&MBB, dl, get(MSP430::JMP)).addMBB(TBB);
169     return 1;
170   }
171
172   // Conditional branch.
173   unsigned Count = 0;
174   assert(0 && "Implement conditional branches!");
175
176   return Count;
177 }