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