fececb0c1127e70ed41c60012a889add37434733
[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 #include "llvm/Support/ErrorHandling.h"
25
26 using namespace llvm;
27
28 MSP430InstrInfo::MSP430InstrInfo(MSP430TargetMachine &tm)
29   : TargetInstrInfoImpl(MSP430Insts, array_lengthof(MSP430Insts)),
30     RI(tm, *this), TM(tm) {}
31
32 void MSP430InstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
33                                           MachineBasicBlock::iterator MI,
34                                     unsigned SrcReg, bool isKill, int FrameIdx,
35                                           const TargetRegisterClass *RC,
36                                           const TargetRegisterInfo *TRI) const {
37   DebugLoc DL;
38   if (MI != MBB.end()) DL = MI->getDebugLoc();
39   MachineFunction &MF = *MBB.getParent();
40   MachineFrameInfo &MFI = *MF.getFrameInfo();
41
42   MachineMemOperand *MMO =
43     MF.getMachineMemOperand(PseudoSourceValue::getFixedStack(FrameIdx),
44                             MachineMemOperand::MOStore, 0,
45                             MFI.getObjectSize(FrameIdx),
46                             MFI.getObjectAlignment(FrameIdx));
47
48   if (RC == &MSP430::GR16RegClass)
49     BuildMI(MBB, MI, DL, get(MSP430::MOV16mr))
50       .addFrameIndex(FrameIdx).addImm(0)
51       .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
52   else if (RC == &MSP430::GR8RegClass)
53     BuildMI(MBB, MI, DL, get(MSP430::MOV8mr))
54       .addFrameIndex(FrameIdx).addImm(0)
55       .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
56   else
57     llvm_unreachable("Cannot store this register to stack slot!");
58 }
59
60 void MSP430InstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
61                                            MachineBasicBlock::iterator MI,
62                                            unsigned DestReg, int FrameIdx,
63                                            const TargetRegisterClass *RC,
64                                            const TargetRegisterInfo *TRI) const{
65   DebugLoc DL;
66   if (MI != MBB.end()) DL = MI->getDebugLoc();
67   MachineFunction &MF = *MBB.getParent();
68   MachineFrameInfo &MFI = *MF.getFrameInfo();
69
70   MachineMemOperand *MMO =
71     MF.getMachineMemOperand(PseudoSourceValue::getFixedStack(FrameIdx),
72                             MachineMemOperand::MOLoad, 0,
73                             MFI.getObjectSize(FrameIdx),
74                             MFI.getObjectAlignment(FrameIdx));
75
76   if (RC == &MSP430::GR16RegClass)
77     BuildMI(MBB, MI, DL, get(MSP430::MOV16rm))
78       .addReg(DestReg).addFrameIndex(FrameIdx).addImm(0).addMemOperand(MMO);
79   else if (RC == &MSP430::GR8RegClass)
80     BuildMI(MBB, MI, DL, get(MSP430::MOV8rm))
81       .addReg(DestReg).addFrameIndex(FrameIdx).addImm(0).addMemOperand(MMO);
82   else
83     llvm_unreachable("Cannot store this register to stack slot!");
84 }
85
86 void MSP430InstrInfo::copyPhysReg(MachineBasicBlock &MBB,
87                                   MachineBasicBlock::iterator I, DebugLoc DL,
88                                   unsigned DestReg, unsigned SrcReg,
89                                   bool KillSrc) const {
90   unsigned Opc;
91   if (MSP430::GR16RegClass.contains(DestReg, SrcReg))
92     Opc = MSP430::MOV16rr;
93   else if (MSP430::GR8RegClass.contains(DestReg, SrcReg))
94     Opc = MSP430::MOV8rr;
95   else
96     llvm_unreachable("Impossible reg-to-reg copy");
97
98   BuildMI(MBB, I, DL, get(Opc), DestReg)
99     .addReg(SrcReg, getKillRegState(KillSrc));
100 }
101
102 bool
103 MSP430InstrInfo::isMoveInstr(const MachineInstr& MI,
104                              unsigned &SrcReg, unsigned &DstReg,
105                              unsigned &SrcSubIdx, unsigned &DstSubIdx) const {
106   SrcSubIdx = DstSubIdx = 0; // No sub-registers yet.
107
108   switch (MI.getOpcode()) {
109   default:
110     return false;
111   case MSP430::MOV8rr:
112   case MSP430::MOV16rr:
113    assert(MI.getNumOperands() >= 2 &&
114            MI.getOperand(0).isReg() &&
115            MI.getOperand(1).isReg() &&
116            "invalid register-register move instruction");
117     SrcReg = MI.getOperand(1).getReg();
118     DstReg = MI.getOperand(0).getReg();
119     return true;
120   }
121 }
122
123 bool
124 MSP430InstrInfo::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
125                                            MachineBasicBlock::iterator MI,
126                                         const std::vector<CalleeSavedInfo> &CSI,
127                                           const TargetRegisterInfo *TRI) const {
128   if (CSI.empty())
129     return false;
130
131   DebugLoc DL;
132   if (MI != MBB.end()) DL = MI->getDebugLoc();
133
134   MachineFunction &MF = *MBB.getParent();
135   MSP430MachineFunctionInfo *MFI = MF.getInfo<MSP430MachineFunctionInfo>();
136   MFI->setCalleeSavedFrameSize(CSI.size() * 2);
137
138   for (unsigned i = CSI.size(); i != 0; --i) {
139     unsigned Reg = CSI[i-1].getReg();
140     // Add the callee-saved register as live-in. It's killed at the spill.
141     MBB.addLiveIn(Reg);
142     BuildMI(MBB, MI, DL, get(MSP430::PUSH16r))
143       .addReg(Reg, RegState::Kill);
144   }
145   return true;
146 }
147
148 bool
149 MSP430InstrInfo::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
150                                              MachineBasicBlock::iterator MI,
151                                         const std::vector<CalleeSavedInfo> &CSI,
152                                           const TargetRegisterInfo *TRI) const {
153   if (CSI.empty())
154     return false;
155
156   DebugLoc DL;
157   if (MI != MBB.end()) DL = MI->getDebugLoc();
158
159   for (unsigned i = 0, e = CSI.size(); i != e; ++i)
160     BuildMI(MBB, MI, DL, get(MSP430::POP16r), CSI[i].getReg());
161
162   return true;
163 }
164
165 unsigned MSP430InstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
166   MachineBasicBlock::iterator I = MBB.end();
167   unsigned Count = 0;
168
169   while (I != MBB.begin()) {
170     --I;
171     if (I->isDebugValue())
172       continue;
173     if (I->getOpcode() != MSP430::JMP &&
174         I->getOpcode() != MSP430::JCC &&
175         I->getOpcode() != MSP430::Br &&
176         I->getOpcode() != MSP430::Bm)
177       break;
178     // Remove the branch.
179     I->eraseFromParent();
180     I = MBB.end();
181     ++Count;
182   }
183
184   return Count;
185 }
186
187 bool MSP430InstrInfo::
188 ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
189   assert(Cond.size() == 1 && "Invalid Xbranch condition!");
190
191   MSP430CC::CondCodes CC = static_cast<MSP430CC::CondCodes>(Cond[0].getImm());
192
193   switch (CC) {
194   default:
195     assert(0 && "Invalid branch condition!");
196     break;
197   case MSP430CC::COND_E:
198     CC = MSP430CC::COND_NE;
199     break;
200   case MSP430CC::COND_NE:
201     CC = MSP430CC::COND_E;
202     break;
203   case MSP430CC::COND_L:
204     CC = MSP430CC::COND_GE;
205     break;
206   case MSP430CC::COND_GE:
207     CC = MSP430CC::COND_L;
208     break;
209   case MSP430CC::COND_HS:
210     CC = MSP430CC::COND_LO;
211     break;
212   case MSP430CC::COND_LO:
213     CC = MSP430CC::COND_HS;
214     break;
215   }
216
217   Cond[0].setImm(CC);
218   return false;
219 }
220
221 bool MSP430InstrInfo::isUnpredicatedTerminator(const MachineInstr *MI) const {
222   const TargetInstrDesc &TID = MI->getDesc();
223   if (!TID.isTerminator()) return false;
224
225   // Conditional branch is a special case.
226   if (TID.isBranch() && !TID.isBarrier())
227     return true;
228   if (!TID.isPredicable())
229     return true;
230   return !isPredicated(MI);
231 }
232
233 bool MSP430InstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
234                                     MachineBasicBlock *&TBB,
235                                     MachineBasicBlock *&FBB,
236                                     SmallVectorImpl<MachineOperand> &Cond,
237                                     bool AllowModify) const {
238   // Start from the bottom of the block and work up, examining the
239   // terminator instructions.
240   MachineBasicBlock::iterator I = MBB.end();
241   while (I != MBB.begin()) {
242     --I;
243     if (I->isDebugValue())
244       continue;
245
246     // Working from the bottom, when we see a non-terminator
247     // instruction, we're done.
248     if (!isUnpredicatedTerminator(I))
249       break;
250
251     // A terminator that isn't a branch can't easily be handled
252     // by this analysis.
253     if (!I->getDesc().isBranch())
254       return true;
255
256     // Cannot handle indirect branches.
257     if (I->getOpcode() == MSP430::Br ||
258         I->getOpcode() == MSP430::Bm)
259       return true;
260
261     // Handle unconditional branches.
262     if (I->getOpcode() == MSP430::JMP) {
263       if (!AllowModify) {
264         TBB = I->getOperand(0).getMBB();
265         continue;
266       }
267
268       // If the block has any instructions after a JMP, delete them.
269       while (llvm::next(I) != MBB.end())
270         llvm::next(I)->eraseFromParent();
271       Cond.clear();
272       FBB = 0;
273
274       // Delete the JMP if it's equivalent to a fall-through.
275       if (MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) {
276         TBB = 0;
277         I->eraseFromParent();
278         I = MBB.end();
279         continue;
280       }
281
282       // TBB is used to indicate the unconditinal destination.
283       TBB = I->getOperand(0).getMBB();
284       continue;
285     }
286
287     // Handle conditional branches.
288     assert(I->getOpcode() == MSP430::JCC && "Invalid conditional branch");
289     MSP430CC::CondCodes BranchCode =
290       static_cast<MSP430CC::CondCodes>(I->getOperand(1).getImm());
291     if (BranchCode == MSP430CC::COND_INVALID)
292       return true;  // Can't handle weird stuff.
293
294     // Working from the bottom, handle the first conditional branch.
295     if (Cond.empty()) {
296       FBB = TBB;
297       TBB = I->getOperand(0).getMBB();
298       Cond.push_back(MachineOperand::CreateImm(BranchCode));
299       continue;
300     }
301
302     // Handle subsequent conditional branches. Only handle the case where all
303     // conditional branches branch to the same destination.
304     assert(Cond.size() == 1);
305     assert(TBB);
306
307     // Only handle the case where all conditional branches branch to
308     // the same destination.
309     if (TBB != I->getOperand(0).getMBB())
310       return true;
311
312     MSP430CC::CondCodes OldBranchCode = (MSP430CC::CondCodes)Cond[0].getImm();
313     // If the conditions are the same, we can leave them alone.
314     if (OldBranchCode == BranchCode)
315       continue;
316
317     return true;
318   }
319
320   return false;
321 }
322
323 unsigned
324 MSP430InstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
325                               MachineBasicBlock *FBB,
326                               const SmallVectorImpl<MachineOperand> &Cond,
327                               DebugLoc DL) const {
328   // Shouldn't be a fall through.
329   assert(TBB && "InsertBranch must not be told to insert a fallthrough");
330   assert((Cond.size() == 1 || Cond.size() == 0) &&
331          "MSP430 branch conditions have one component!");
332
333   if (Cond.empty()) {
334     // Unconditional branch?
335     assert(!FBB && "Unconditional branch with multiple successors!");
336     BuildMI(&MBB, DL, get(MSP430::JMP)).addMBB(TBB);
337     return 1;
338   }
339
340   // Conditional branch.
341   unsigned Count = 0;
342   BuildMI(&MBB, DL, get(MSP430::JCC)).addMBB(TBB).addImm(Cond[0].getImm());
343   ++Count;
344
345   if (FBB) {
346     // Two-way Conditional branch. Insert the second branch.
347     BuildMI(&MBB, DL, get(MSP430::JMP)).addMBB(FBB);
348     ++Count;
349   }
350   return Count;
351 }
352
353 /// GetInstSize - Return the number of bytes of code the specified
354 /// instruction may be.  This returns the maximum number of bytes.
355 ///
356 unsigned MSP430InstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
357   const TargetInstrDesc &Desc = MI->getDesc();
358
359   switch (Desc.TSFlags & MSP430II::SizeMask) {
360   default:
361     switch (Desc.getOpcode()) {
362     default:
363       assert(0 && "Unknown instruction size!");
364     case TargetOpcode::PROLOG_LABEL:
365     case TargetOpcode::EH_LABEL:
366     case TargetOpcode::IMPLICIT_DEF:
367     case TargetOpcode::KILL:
368     case TargetOpcode::DBG_VALUE:
369       return 0;
370     case TargetOpcode::INLINEASM: {
371       const MachineFunction *MF = MI->getParent()->getParent();
372       const TargetInstrInfo &TII = *MF->getTarget().getInstrInfo();
373       return TII.getInlineAsmLength(MI->getOperand(0).getSymbolName(),
374                                     *MF->getTarget().getMCAsmInfo());
375     }
376     }
377   case MSP430II::SizeSpecial:
378     switch (MI->getOpcode()) {
379     default:
380       assert(0 && "Unknown instruction size!");
381     case MSP430::SAR8r1c:
382     case MSP430::SAR16r1c:
383       return 4;
384     }
385   case MSP430II::Size2Bytes:
386     return 2;
387   case MSP430II::Size4Bytes:
388     return 4;
389   case MSP430II::Size6Bytes:
390     return 6;
391   }
392
393   return 6;
394 }