Get rid of some more getOpcode calls.
[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::LDRri: return 0;
39   case ARMII::MOVr: return ARM::tMOVr;
40   case ARMII::STRri: return 0;
41   case ARMII::SUBri: return ARM::tSUBi8;
42   case ARMII::SUBrs: return 0;
43   case ARMII::SUBrr: return ARM::tSUBrr;
44   default:
45     break;
46   }
47
48   return 0;
49 }
50
51 bool
52 Thumb1InstrInfo::BlockHasNoFallThrough(const MachineBasicBlock &MBB) const {
53   if (MBB.empty()) return false;
54
55   switch (MBB.back().getOpcode()) {
56   case ARM::tBX_RET:
57   case ARM::tBX_RET_vararg:
58   case ARM::tPOP_RET:
59   case ARM::tB:
60   case ARM::tBR_JTr:
61     return true;
62   default:
63     break;
64   }
65
66   return false;
67 }
68
69 bool Thumb1InstrInfo::copyRegToReg(MachineBasicBlock &MBB,
70                                    MachineBasicBlock::iterator I,
71                                    unsigned DestReg, unsigned SrcReg,
72                                    const TargetRegisterClass *DestRC,
73                                    const TargetRegisterClass *SrcRC) const {
74   DebugLoc DL = DebugLoc::getUnknownLoc();
75   if (I != MBB.end()) DL = I->getDebugLoc();
76
77   if (DestRC == ARM::GPRRegisterClass) {
78     if (SrcRC == ARM::GPRRegisterClass) {
79       BuildMI(MBB, I, DL, get(ARM::tMOVgpr2gpr), DestReg).addReg(SrcReg);
80       return true;
81     } else if (SrcRC == ARM::tGPRRegisterClass) {
82       BuildMI(MBB, I, DL, get(ARM::tMOVtgpr2gpr), DestReg).addReg(SrcReg);
83       return true;
84     }
85   } else if (DestRC == ARM::tGPRRegisterClass) {
86     if (SrcRC == ARM::GPRRegisterClass) {
87       BuildMI(MBB, I, DL, get(ARM::tMOVgpr2tgpr), DestReg).addReg(SrcReg);
88       return true;
89     } else if (SrcRC == ARM::tGPRRegisterClass) {
90       BuildMI(MBB, I, DL, get(ARM::tMOVr), DestReg).addReg(SrcReg);
91       return true;
92     }
93   }
94
95   return false;
96 }
97
98 bool Thumb1InstrInfo::
99 canFoldMemoryOperand(const MachineInstr *MI,
100                      const SmallVectorImpl<unsigned> &Ops) const {
101   if (Ops.size() != 1) return false;
102
103   unsigned OpNum = Ops[0];
104   unsigned Opc = MI->getOpcode();
105   switch (Opc) {
106   default: break;
107   case ARM::tMOVr:
108   case ARM::tMOVtgpr2gpr:
109   case ARM::tMOVgpr2tgpr:
110   case ARM::tMOVgpr2gpr: {
111     if (OpNum == 0) { // move -> store
112       unsigned SrcReg = MI->getOperand(1).getReg();
113       if (RI.isPhysicalRegister(SrcReg) && !isARMLowRegister(SrcReg))
114         // tSpill cannot take a high register operand.
115         return false;
116     } else {          // move -> load
117       unsigned DstReg = MI->getOperand(0).getReg();
118       if (RI.isPhysicalRegister(DstReg) && !isARMLowRegister(DstReg))
119         // tRestore cannot target a high register operand.
120         return false;
121     }
122     return true;
123   }
124   }
125
126   return false;
127 }
128
129 void Thumb1InstrInfo::
130 storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
131                     unsigned SrcReg, bool isKill, int FI,
132                     const TargetRegisterClass *RC) const {
133   DebugLoc DL = DebugLoc::getUnknownLoc();
134   if (I != MBB.end()) DL = I->getDebugLoc();
135
136   assert(RC == ARM::tGPRRegisterClass && "Unknown regclass!");
137
138   if (RC == ARM::tGPRRegisterClass) {
139     AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::tSpill))
140                    .addReg(SrcReg, getKillRegState(isKill))
141                    .addFrameIndex(FI).addImm(0));
142   }
143 }
144
145 void Thumb1InstrInfo::
146 loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
147                      unsigned DestReg, int FI,
148                      const TargetRegisterClass *RC) const {
149   DebugLoc DL = DebugLoc::getUnknownLoc();
150   if (I != MBB.end()) DL = I->getDebugLoc();
151
152   assert(RC == ARM::tGPRRegisterClass && "Unknown regclass!");
153
154   if (RC == ARM::tGPRRegisterClass) {
155     AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::tRestore), DestReg)
156                    .addFrameIndex(FI).addImm(0));
157   }
158 }
159
160 bool Thumb1InstrInfo::
161 spillCalleeSavedRegisters(MachineBasicBlock &MBB,
162                           MachineBasicBlock::iterator MI,
163                           const std::vector<CalleeSavedInfo> &CSI) const {
164   if (CSI.empty())
165     return false;
166
167   DebugLoc DL = DebugLoc::getUnknownLoc();
168   if (MI != MBB.end()) DL = MI->getDebugLoc();
169
170   MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, get(ARM::tPUSH));
171   for (unsigned i = CSI.size(); i != 0; --i) {
172     unsigned Reg = CSI[i-1].getReg();
173     // Add the callee-saved register as live-in. It's killed at the spill.
174     MBB.addLiveIn(Reg);
175     MIB.addReg(Reg, RegState::Kill);
176   }
177   return true;
178 }
179
180 bool Thumb1InstrInfo::
181 restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
182                             MachineBasicBlock::iterator MI,
183                             const std::vector<CalleeSavedInfo> &CSI) const {
184   MachineFunction &MF = *MBB.getParent();
185   ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
186   if (CSI.empty())
187     return false;
188
189   bool isVarArg = AFI->getVarArgsRegSaveSize() > 0;
190   MachineInstr *PopMI = MF.CreateMachineInstr(get(ARM::tPOP),MI->getDebugLoc());
191   for (unsigned i = CSI.size(); i != 0; --i) {
192     unsigned Reg = CSI[i-1].getReg();
193     if (Reg == ARM::LR) {
194       // Special epilogue for vararg functions. See emitEpilogue
195       if (isVarArg)
196         continue;
197       Reg = ARM::PC;
198       PopMI->setDesc(get(ARM::tPOP_RET));
199       MI = MBB.erase(MI);
200     }
201     PopMI->addOperand(MachineOperand::CreateReg(Reg, true));
202   }
203
204   // It's illegal to emit pop instruction without operands.
205   if (PopMI->getNumOperands() > 0)
206     MBB.insert(MI, PopMI);
207
208   return true;
209 }
210
211 MachineInstr *Thumb1InstrInfo::
212 foldMemoryOperandImpl(MachineFunction &MF, MachineInstr *MI,
213                       const SmallVectorImpl<unsigned> &Ops, int FI) const {
214   if (Ops.size() != 1) return NULL;
215
216   unsigned OpNum = Ops[0];
217   unsigned Opc = MI->getOpcode();
218   MachineInstr *NewMI = NULL;
219   switch (Opc) {
220   default: break;
221   case ARM::tMOVr:
222   case ARM::tMOVtgpr2gpr:
223   case ARM::tMOVgpr2tgpr:
224   case ARM::tMOVgpr2gpr: {
225     if (OpNum == 0) { // move -> store
226       unsigned SrcReg = MI->getOperand(1).getReg();
227       bool isKill = MI->getOperand(1).isKill();
228       if (RI.isPhysicalRegister(SrcReg) && !isARMLowRegister(SrcReg))
229         // tSpill cannot take a high register operand.
230         break;
231       NewMI = AddDefaultPred(BuildMI(MF, MI->getDebugLoc(), get(ARM::tSpill))
232                              .addReg(SrcReg, getKillRegState(isKill))
233                              .addFrameIndex(FI).addImm(0));
234     } else {          // move -> load
235       unsigned DstReg = MI->getOperand(0).getReg();
236       if (RI.isPhysicalRegister(DstReg) && !isARMLowRegister(DstReg))
237         // tRestore cannot target a high register operand.
238         break;
239       bool isDead = MI->getOperand(0).isDead();
240       NewMI = AddDefaultPred(BuildMI(MF, MI->getDebugLoc(), get(ARM::tRestore))
241                              .addReg(DstReg,
242                                      RegState::Define | getDeadRegState(isDead))
243                              .addFrameIndex(FI).addImm(0));
244     }
245     break;
246   }
247   }
248
249   return NewMI;
250 }