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