Remove unused member functions.
[oota-llvm.git] / lib / Target / ARM / Thumb1InstrInfo.cpp
1 //===- Thumb1InstrInfo.cpp - Thumb-1 Instruction Information --------*- C++ -*-===//
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 Thumb-1 implementation of the TargetInstrInfo class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "ARMInstrInfo.h"
15 #include "ARM.h"
16 #include "ARMGenInstrInfo.inc"
17 #include "ARMMachineFunctionInfo.h"
18 #include "llvm/CodeGen/MachineFrameInfo.h"
19 #include "llvm/CodeGen/MachineInstrBuilder.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "Thumb1InstrInfo.h"
22
23 using namespace llvm;
24
25 Thumb1InstrInfo::Thumb1InstrInfo(const ARMSubtarget &STI)
26   : ARMBaseInstrInfo(STI), RI(*this, STI) {
27 }
28
29 unsigned Thumb1InstrInfo::getUnindexedOpcode(unsigned Opc) const {
30   return 0;
31 }
32
33 unsigned Thumb1InstrInfo::getOpcode(ARMII::Op Op) const {
34   switch (Op) {
35   case ARMII::ADDri: return ARM::tADDi8;
36   case ARMII::ADDrs: return 0;
37   case ARMII::ADDrr: return ARM::tADDrr;
38   case ARMII::B: return ARM::tB;
39   case ARMII::Bcc: return ARM::tBcc;
40   case ARMII::BR_JTr: return ARM::tBR_JTr;
41   case ARMII::BR_JTm: return 0;
42   case ARMII::BR_JTadd: return 0;
43   case ARMII::BX_RET: return ARM::tBX_RET;
44   case ARMII::LDRrr: return ARM::tLDR;
45   case ARMII::LDRri: return 0;
46   case ARMII::MOVr: return ARM::tMOVr;
47   case ARMII::STRrr: return ARM::tSTR;
48   case ARMII::STRri: return 0;
49   case ARMII::SUBri: return ARM::tSUBi8;
50   case ARMII::SUBrs: return 0;
51   case ARMII::SUBrr: return ARM::tSUBrr;
52   default:
53     break;
54   }
55
56   return 0;
57 }
58
59 bool
60 Thumb1InstrInfo::BlockHasNoFallThrough(const MachineBasicBlock &MBB) const {
61   if (MBB.empty()) return false;
62
63   switch (MBB.back().getOpcode()) {
64   case ARM::tBX_RET:
65   case ARM::tBX_RET_vararg:
66   case ARM::tPOP_RET:
67   case ARM::tB:
68   case ARM::tBR_JTr:
69     return true;
70   default:
71     break;
72   }
73
74   return false;
75 }
76
77 bool Thumb1InstrInfo::isMoveInstr(const MachineInstr &MI,
78                                   unsigned &SrcReg, unsigned &DstReg,
79                                   unsigned& SrcSubIdx, unsigned& DstSubIdx) const {
80   SrcSubIdx = DstSubIdx = 0; // No sub-registers.
81
82   unsigned oc = MI.getOpcode();
83   switch (oc) {
84   default:
85     return false;
86   case ARM::tMOVr:
87   case ARM::tMOVhir2lor:
88   case ARM::tMOVlor2hir:
89   case ARM::tMOVhir2hir:
90     assert(MI.getDesc().getNumOperands() >= 2 &&
91            MI.getOperand(0).isReg() &&
92            MI.getOperand(1).isReg() &&
93            "Invalid Thumb MOV instruction");
94     SrcReg = MI.getOperand(1).getReg();
95     DstReg = MI.getOperand(0).getReg();
96     return true;
97   }
98 }
99
100 unsigned Thumb1InstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
101                                               int &FrameIndex) const {
102   switch (MI->getOpcode()) {
103   default: break;
104   case ARM::tRestore:
105     if (MI->getOperand(1).isFI() &&
106         MI->getOperand(2).isImm() &&
107         MI->getOperand(2).getImm() == 0) {
108       FrameIndex = MI->getOperand(1).getIndex();
109       return MI->getOperand(0).getReg();
110     }
111     break;
112   }
113   return 0;
114 }
115
116 unsigned Thumb1InstrInfo::isStoreToStackSlot(const MachineInstr *MI,
117                                              int &FrameIndex) const {
118   switch (MI->getOpcode()) {
119   default: break;
120   case ARM::tSpill:
121     if (MI->getOperand(1).isFI() &&
122         MI->getOperand(2).isImm() &&
123         MI->getOperand(2).getImm() == 0) {
124       FrameIndex = MI->getOperand(1).getIndex();
125       return MI->getOperand(0).getReg();
126     }
127     break;
128   }
129   return 0;
130 }
131
132 bool Thumb1InstrInfo::copyRegToReg(MachineBasicBlock &MBB,
133                                    MachineBasicBlock::iterator I,
134                                    unsigned DestReg, unsigned SrcReg,
135                                    const TargetRegisterClass *DestRC,
136                                    const TargetRegisterClass *SrcRC) const {
137   DebugLoc DL = DebugLoc::getUnknownLoc();
138   if (I != MBB.end()) DL = I->getDebugLoc();
139
140   if (DestRC == ARM::GPRRegisterClass) {
141     if (SrcRC == ARM::GPRRegisterClass) {
142       BuildMI(MBB, I, DL, get(ARM::tMOVhir2hir), DestReg).addReg(SrcReg);
143       return true;
144     } else if (SrcRC == ARM::tGPRRegisterClass) {
145       BuildMI(MBB, I, DL, get(ARM::tMOVlor2hir), DestReg).addReg(SrcReg);
146       return true;
147     }
148   } else if (DestRC == ARM::tGPRRegisterClass) {
149     if (SrcRC == ARM::GPRRegisterClass) {
150       BuildMI(MBB, I, DL, get(ARM::tMOVhir2lor), DestReg).addReg(SrcReg);
151       return true;
152     } else if (SrcRC == ARM::tGPRRegisterClass) {
153       BuildMI(MBB, I, DL, get(ARM::tMOVr), DestReg).addReg(SrcReg);
154       return true;
155     }
156   }
157
158   return false;
159 }
160
161 bool Thumb1InstrInfo::
162 canFoldMemoryOperand(const MachineInstr *MI,
163                      const SmallVectorImpl<unsigned> &Ops) const {
164   if (Ops.size() != 1) return false;
165
166   unsigned OpNum = Ops[0];
167   unsigned Opc = MI->getOpcode();
168   switch (Opc) {
169   default: break;
170   case ARM::tMOVr:
171   case ARM::tMOVlor2hir:
172   case ARM::tMOVhir2lor:
173   case ARM::tMOVhir2hir: {
174     if (OpNum == 0) { // move -> store
175       unsigned SrcReg = MI->getOperand(1).getReg();
176       if (RI.isPhysicalRegister(SrcReg) && !isARMLowRegister(SrcReg))
177         // tSpill cannot take a high register operand.
178         return false;
179     } else {          // move -> load
180       unsigned DstReg = MI->getOperand(0).getReg();
181       if (RI.isPhysicalRegister(DstReg) && !isARMLowRegister(DstReg))
182         // tRestore cannot target a high register operand.
183         return false;
184     }
185     return true;
186   }
187   }
188
189   return false;
190 }
191
192 void Thumb1InstrInfo::
193 storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
194                     unsigned SrcReg, bool isKill, int FI,
195                     const TargetRegisterClass *RC) const {
196   DebugLoc DL = DebugLoc::getUnknownLoc();
197   if (I != MBB.end()) DL = I->getDebugLoc();
198
199   assert(RC == ARM::tGPRRegisterClass && "Unknown regclass!");
200
201   if (RC == ARM::tGPRRegisterClass) {
202     AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::tSpill))
203                    .addReg(SrcReg, getKillRegState(isKill))
204                    .addFrameIndex(FI).addImm(0));
205   }
206 }
207
208 void Thumb1InstrInfo::
209 loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
210                      unsigned DestReg, int FI,
211                      const TargetRegisterClass *RC) const {
212   DebugLoc DL = DebugLoc::getUnknownLoc();
213   if (I != MBB.end()) DL = I->getDebugLoc();
214
215   assert(RC == ARM::tGPRRegisterClass && "Unknown regclass!");
216
217   if (RC == ARM::tGPRRegisterClass) {
218     AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::tRestore), DestReg)
219                    .addFrameIndex(FI).addImm(0));
220   }
221 }
222
223 bool Thumb1InstrInfo::
224 spillCalleeSavedRegisters(MachineBasicBlock &MBB,
225                           MachineBasicBlock::iterator MI,
226                           const std::vector<CalleeSavedInfo> &CSI) const {
227   if (CSI.empty())
228     return false;
229
230   DebugLoc DL = DebugLoc::getUnknownLoc();
231   if (MI != MBB.end()) DL = MI->getDebugLoc();
232
233   MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, get(ARM::tPUSH));
234   for (unsigned i = CSI.size(); i != 0; --i) {
235     unsigned Reg = CSI[i-1].getReg();
236     // Add the callee-saved register as live-in. It's killed at the spill.
237     MBB.addLiveIn(Reg);
238     MIB.addReg(Reg, RegState::Kill);
239   }
240   return true;
241 }
242
243 bool Thumb1InstrInfo::
244 restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
245                             MachineBasicBlock::iterator MI,
246                             const std::vector<CalleeSavedInfo> &CSI) const {
247   MachineFunction &MF = *MBB.getParent();
248   ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
249   if (CSI.empty())
250     return false;
251
252   bool isVarArg = AFI->getVarArgsRegSaveSize() > 0;
253   MachineInstr *PopMI = MF.CreateMachineInstr(get(ARM::tPOP),MI->getDebugLoc());
254   for (unsigned i = CSI.size(); i != 0; --i) {
255     unsigned Reg = CSI[i-1].getReg();
256     if (Reg == ARM::LR) {
257       // Special epilogue for vararg functions. See emitEpilogue
258       if (isVarArg)
259         continue;
260       Reg = ARM::PC;
261       PopMI->setDesc(get(ARM::tPOP_RET));
262       MI = MBB.erase(MI);
263     }
264     PopMI->addOperand(MachineOperand::CreateReg(Reg, true));
265   }
266
267   // It's illegal to emit pop instruction without operands.
268   if (PopMI->getNumOperands() > 0)
269     MBB.insert(MI, PopMI);
270
271   return true;
272 }
273
274 MachineInstr *Thumb1InstrInfo::
275 foldMemoryOperandImpl(MachineFunction &MF, MachineInstr *MI,
276                       const SmallVectorImpl<unsigned> &Ops, int FI) const {
277   if (Ops.size() != 1) return NULL;
278
279   unsigned OpNum = Ops[0];
280   unsigned Opc = MI->getOpcode();
281   MachineInstr *NewMI = NULL;
282   switch (Opc) {
283   default: break;
284   case ARM::tMOVr:
285   case ARM::tMOVlor2hir:
286   case ARM::tMOVhir2lor:
287   case ARM::tMOVhir2hir: {
288     if (OpNum == 0) { // move -> store
289       unsigned SrcReg = MI->getOperand(1).getReg();
290       bool isKill = MI->getOperand(1).isKill();
291       if (RI.isPhysicalRegister(SrcReg) && !isARMLowRegister(SrcReg))
292         // tSpill cannot take a high register operand.
293         break;
294       NewMI = AddDefaultPred(BuildMI(MF, MI->getDebugLoc(), get(ARM::tSpill))
295                              .addReg(SrcReg, getKillRegState(isKill))
296                              .addFrameIndex(FI).addImm(0));
297     } else {          // move -> load
298       unsigned DstReg = MI->getOperand(0).getReg();
299       if (RI.isPhysicalRegister(DstReg) && !isARMLowRegister(DstReg))
300         // tRestore cannot target a high register operand.
301         break;
302       bool isDead = MI->getOperand(0).isDead();
303       NewMI = AddDefaultPred(BuildMI(MF, MI->getDebugLoc(), get(ARM::tRestore))
304                              .addReg(DstReg,
305                                      RegState::Define | getDeadRegState(isDead))
306                              .addFrameIndex(FI).addImm(0));
307     }
308     break;
309   }
310   }
311
312   return NewMI;
313 }