blob: bb4efa43563f30afe1d07fb44bbafe350b1e2be7 [file] [log] [blame]
David Goodwinb50ea5c2009-07-02 22:18:33 +00001//===- Thumb1InstrInfo.cpp - Thumb-1 Instruction Information --------*- C++ -*-===//
Anton Korobeynikovd49ea772009-06-26 21:28:53 +00002//
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//
David Goodwinb50ea5c2009-07-02 22:18:33 +000010// This file contains the Thumb-1 implementation of the TargetInstrInfo class.
Anton Korobeynikovd49ea772009-06-26 21:28:53 +000011//
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"
David Goodwinb50ea5c2009-07-02 22:18:33 +000021#include "Thumb1InstrInfo.h"
Anton Korobeynikovd49ea772009-06-26 21:28:53 +000022
23using namespace llvm;
24
David Goodwinb50ea5c2009-07-02 22:18:33 +000025Thumb1InstrInfo::Thumb1InstrInfo(const ARMSubtarget &STI)
Anton Korobeynikova98cbc52009-06-27 12:16:40 +000026 : ARMBaseInstrInfo(STI), RI(*this, STI) {
Anton Korobeynikovd49ea772009-06-26 21:28:53 +000027}
28
Evan Cheng446c4282009-07-11 06:43:01 +000029unsigned Thumb1InstrInfo::getUnindexedOpcode(unsigned Opc) const {
David Goodwin334c2642009-07-08 16:09:28 +000030 return 0;
31}
32
Evan Cheng446c4282009-07-11 06:43:01 +000033unsigned Thumb1InstrInfo::getOpcode(ARMII::Op Op) const {
David Goodwin334c2642009-07-08 16:09:28 +000034 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;
David Goodwin77521f52009-07-08 20:28:28 +000040 case ARMII::BX_RET: return ARM::tBX_RET;
David Goodwin5ff58b52009-07-24 00:16:18 +000041 case ARMII::LDRrr: return ARM::tLDR;
42 case ARMII::LDRri: return 0;
David Goodwin334c2642009-07-08 16:09:28 +000043 case ARMII::MOVr: return ARM::tMOVr;
David Goodwin5ff58b52009-07-24 00:16:18 +000044 case ARMII::STRrr: return ARM::tSTR;
45 case ARMII::STRri: return 0;
David Goodwin334c2642009-07-08 16:09:28 +000046 case ARMII::SUBri: return ARM::tSUBi8;
47 case ARMII::SUBrs: return 0;
48 case ARMII::SUBrr: return ARM::tSUBrr;
David Goodwin334c2642009-07-08 16:09:28 +000049 default:
50 break;
51 }
52
53 return 0;
54}
55
56bool
57Thumb1InstrInfo::BlockHasNoFallThrough(const MachineBasicBlock &MBB) const {
58 if (MBB.empty()) return false;
59
60 switch (MBB.back().getOpcode()) {
61 case ARM::tBX_RET:
62 case ARM::tBX_RET_vararg:
63 case ARM::tPOP_RET:
64 case ARM::tB:
65 case ARM::tBR_JTr:
66 return true;
67 default:
68 break;
69 }
70
71 return false;
72}
73
David Goodwinb50ea5c2009-07-02 22:18:33 +000074bool Thumb1InstrInfo::isMoveInstr(const MachineInstr &MI,
75 unsigned &SrcReg, unsigned &DstReg,
76 unsigned& SrcSubIdx, unsigned& DstSubIdx) const {
Anton Korobeynikovd49ea772009-06-26 21:28:53 +000077 SrcSubIdx = DstSubIdx = 0; // No sub-registers.
78
79 unsigned oc = MI.getOpcode();
80 switch (oc) {
81 default:
82 return false;
Anton Korobeynikovd49ea772009-06-26 21:28:53 +000083 case ARM::tMOVr:
84 case ARM::tMOVhir2lor:
85 case ARM::tMOVlor2hir:
86 case ARM::tMOVhir2hir:
87 assert(MI.getDesc().getNumOperands() >= 2 &&
88 MI.getOperand(0).isReg() &&
89 MI.getOperand(1).isReg() &&
90 "Invalid Thumb MOV instruction");
91 SrcReg = MI.getOperand(1).getReg();
92 DstReg = MI.getOperand(0).getReg();
93 return true;
94 }
95}
96
David Goodwinb50ea5c2009-07-02 22:18:33 +000097unsigned Thumb1InstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
98 int &FrameIndex) const {
Anton Korobeynikovd49ea772009-06-26 21:28:53 +000099 switch (MI->getOpcode()) {
100 default: break;
Anton Korobeynikovd49ea772009-06-26 21:28:53 +0000101 case ARM::tRestore:
102 if (MI->getOperand(1).isFI() &&
103 MI->getOperand(2).isImm() &&
104 MI->getOperand(2).getImm() == 0) {
105 FrameIndex = MI->getOperand(1).getIndex();
106 return MI->getOperand(0).getReg();
107 }
108 break;
109 }
110 return 0;
111}
112
David Goodwinb50ea5c2009-07-02 22:18:33 +0000113unsigned Thumb1InstrInfo::isStoreToStackSlot(const MachineInstr *MI,
114 int &FrameIndex) const {
Anton Korobeynikovd49ea772009-06-26 21:28:53 +0000115 switch (MI->getOpcode()) {
116 default: break;
Anton Korobeynikovd49ea772009-06-26 21:28:53 +0000117 case ARM::tSpill:
118 if (MI->getOperand(1).isFI() &&
119 MI->getOperand(2).isImm() &&
120 MI->getOperand(2).getImm() == 0) {
121 FrameIndex = MI->getOperand(1).getIndex();
122 return MI->getOperand(0).getReg();
123 }
124 break;
125 }
126 return 0;
127}
128
David Goodwinb50ea5c2009-07-02 22:18:33 +0000129bool Thumb1InstrInfo::copyRegToReg(MachineBasicBlock &MBB,
130 MachineBasicBlock::iterator I,
131 unsigned DestReg, unsigned SrcReg,
132 const TargetRegisterClass *DestRC,
133 const TargetRegisterClass *SrcRC) const {
Anton Korobeynikovd49ea772009-06-26 21:28:53 +0000134 DebugLoc DL = DebugLoc::getUnknownLoc();
135 if (I != MBB.end()) DL = I->getDebugLoc();
136
Anton Korobeynikovd49ea772009-06-26 21:28:53 +0000137 if (DestRC == ARM::GPRRegisterClass) {
138 if (SrcRC == ARM::GPRRegisterClass) {
139 BuildMI(MBB, I, DL, get(ARM::tMOVhir2hir), DestReg).addReg(SrcReg);
140 return true;
141 } else if (SrcRC == ARM::tGPRRegisterClass) {
142 BuildMI(MBB, I, DL, get(ARM::tMOVlor2hir), DestReg).addReg(SrcReg);
143 return true;
144 }
145 } else if (DestRC == ARM::tGPRRegisterClass) {
146 if (SrcRC == ARM::GPRRegisterClass) {
147 BuildMI(MBB, I, DL, get(ARM::tMOVhir2lor), DestReg).addReg(SrcReg);
148 return true;
149 } else if (SrcRC == ARM::tGPRRegisterClass) {
150 BuildMI(MBB, I, DL, get(ARM::tMOVr), DestReg).addReg(SrcReg);
151 return true;
152 }
153 }
154
155 return false;
156}
157
David Goodwinb50ea5c2009-07-02 22:18:33 +0000158bool Thumb1InstrInfo::
Anton Korobeynikova98cbc52009-06-27 12:16:40 +0000159canFoldMemoryOperand(const MachineInstr *MI,
160 const SmallVectorImpl<unsigned> &Ops) const {
161 if (Ops.size() != 1) return false;
162
163 unsigned OpNum = Ops[0];
164 unsigned Opc = MI->getOpcode();
165 switch (Opc) {
166 default: break;
167 case ARM::tMOVr:
168 case ARM::tMOVlor2hir:
169 case ARM::tMOVhir2lor:
170 case ARM::tMOVhir2hir: {
171 if (OpNum == 0) { // move -> store
172 unsigned SrcReg = MI->getOperand(1).getReg();
Anton Korobeynikov55ad1f22009-06-27 12:59:03 +0000173 if (RI.isPhysicalRegister(SrcReg) && !isARMLowRegister(SrcReg))
Anton Korobeynikova98cbc52009-06-27 12:16:40 +0000174 // tSpill cannot take a high register operand.
175 return false;
176 } else { // move -> load
177 unsigned DstReg = MI->getOperand(0).getReg();
Anton Korobeynikov55ad1f22009-06-27 12:59:03 +0000178 if (RI.isPhysicalRegister(DstReg) && !isARMLowRegister(DstReg))
Anton Korobeynikova98cbc52009-06-27 12:16:40 +0000179 // tRestore cannot target a high register operand.
180 return false;
181 }
182 return true;
183 }
184 }
185
186 return false;
187}
188
David Goodwinb50ea5c2009-07-02 22:18:33 +0000189void Thumb1InstrInfo::
Anton Korobeynikovd49ea772009-06-26 21:28:53 +0000190storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
191 unsigned SrcReg, bool isKill, int FI,
192 const TargetRegisterClass *RC) const {
193 DebugLoc DL = DebugLoc::getUnknownLoc();
194 if (I != MBB.end()) DL = I->getDebugLoc();
195
196 assert(RC == ARM::tGPRRegisterClass && "Unknown regclass!");
197
Anton Korobeynikovd49ea772009-06-26 21:28:53 +0000198 if (RC == ARM::tGPRRegisterClass) {
Evan Cheng446c4282009-07-11 06:43:01 +0000199 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::tSpill))
200 .addReg(SrcReg, getKillRegState(isKill))
201 .addFrameIndex(FI).addImm(0));
Anton Korobeynikovd49ea772009-06-26 21:28:53 +0000202 }
203}
204
David Goodwinb50ea5c2009-07-02 22:18:33 +0000205void Thumb1InstrInfo::
Anton Korobeynikovd49ea772009-06-26 21:28:53 +0000206loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
207 unsigned DestReg, int FI,
208 const TargetRegisterClass *RC) const {
209 DebugLoc DL = DebugLoc::getUnknownLoc();
210 if (I != MBB.end()) DL = I->getDebugLoc();
211
Anton Korobeynikovd49ea772009-06-26 21:28:53 +0000212 assert(RC == ARM::tGPRRegisterClass && "Unknown regclass!");
213
214 if (RC == ARM::tGPRRegisterClass) {
Evan Cheng446c4282009-07-11 06:43:01 +0000215 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::tRestore), DestReg)
216 .addFrameIndex(FI).addImm(0));
Anton Korobeynikovd49ea772009-06-26 21:28:53 +0000217 }
218}
219
David Goodwinb50ea5c2009-07-02 22:18:33 +0000220bool Thumb1InstrInfo::
Anton Korobeynikovd49ea772009-06-26 21:28:53 +0000221spillCalleeSavedRegisters(MachineBasicBlock &MBB,
222 MachineBasicBlock::iterator MI,
223 const std::vector<CalleeSavedInfo> &CSI) const {
224 if (CSI.empty())
225 return false;
226
227 DebugLoc DL = DebugLoc::getUnknownLoc();
228 if (MI != MBB.end()) DL = MI->getDebugLoc();
229
230 MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, get(ARM::tPUSH));
231 for (unsigned i = CSI.size(); i != 0; --i) {
232 unsigned Reg = CSI[i-1].getReg();
233 // Add the callee-saved register as live-in. It's killed at the spill.
234 MBB.addLiveIn(Reg);
235 MIB.addReg(Reg, RegState::Kill);
236 }
237 return true;
238}
239
David Goodwinb50ea5c2009-07-02 22:18:33 +0000240bool Thumb1InstrInfo::
Anton Korobeynikovd49ea772009-06-26 21:28:53 +0000241restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
242 MachineBasicBlock::iterator MI,
243 const std::vector<CalleeSavedInfo> &CSI) const {
244 MachineFunction &MF = *MBB.getParent();
245 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
246 if (CSI.empty())
247 return false;
248
249 bool isVarArg = AFI->getVarArgsRegSaveSize() > 0;
250 MachineInstr *PopMI = MF.CreateMachineInstr(get(ARM::tPOP),MI->getDebugLoc());
251 for (unsigned i = CSI.size(); i != 0; --i) {
252 unsigned Reg = CSI[i-1].getReg();
253 if (Reg == ARM::LR) {
254 // Special epilogue for vararg functions. See emitEpilogue
255 if (isVarArg)
256 continue;
257 Reg = ARM::PC;
258 PopMI->setDesc(get(ARM::tPOP_RET));
259 MI = MBB.erase(MI);
260 }
261 PopMI->addOperand(MachineOperand::CreateReg(Reg, true));
262 }
263
264 // It's illegal to emit pop instruction without operands.
265 if (PopMI->getNumOperands() > 0)
266 MBB.insert(MI, PopMI);
267
268 return true;
269}
270
David Goodwinb50ea5c2009-07-02 22:18:33 +0000271MachineInstr *Thumb1InstrInfo::
Anton Korobeynikovd49ea772009-06-26 21:28:53 +0000272foldMemoryOperandImpl(MachineFunction &MF, MachineInstr *MI,
273 const SmallVectorImpl<unsigned> &Ops, int FI) const {
274 if (Ops.size() != 1) return NULL;
Anton Korobeynikovd49ea772009-06-26 21:28:53 +0000275
276 unsigned OpNum = Ops[0];
277 unsigned Opc = MI->getOpcode();
278 MachineInstr *NewMI = NULL;
279 switch (Opc) {
280 default: break;
281 case ARM::tMOVr:
282 case ARM::tMOVlor2hir:
283 case ARM::tMOVhir2lor:
284 case ARM::tMOVhir2hir: {
285 if (OpNum == 0) { // move -> store
286 unsigned SrcReg = MI->getOperand(1).getReg();
287 bool isKill = MI->getOperand(1).isKill();
Anton Korobeynikov55ad1f22009-06-27 12:59:03 +0000288 if (RI.isPhysicalRegister(SrcReg) && !isARMLowRegister(SrcReg))
Anton Korobeynikovd49ea772009-06-26 21:28:53 +0000289 // tSpill cannot take a high register operand.
290 break;
Evan Cheng446c4282009-07-11 06:43:01 +0000291 NewMI = AddDefaultPred(BuildMI(MF, MI->getDebugLoc(), get(ARM::tSpill))
292 .addReg(SrcReg, getKillRegState(isKill))
293 .addFrameIndex(FI).addImm(0));
Anton Korobeynikovd49ea772009-06-26 21:28:53 +0000294 } else { // move -> load
295 unsigned DstReg = MI->getOperand(0).getReg();
Anton Korobeynikov55ad1f22009-06-27 12:59:03 +0000296 if (RI.isPhysicalRegister(DstReg) && !isARMLowRegister(DstReg))
Anton Korobeynikovd49ea772009-06-26 21:28:53 +0000297 // tRestore cannot target a high register operand.
298 break;
299 bool isDead = MI->getOperand(0).isDead();
Evan Cheng446c4282009-07-11 06:43:01 +0000300 NewMI = AddDefaultPred(BuildMI(MF, MI->getDebugLoc(), get(ARM::tRestore))
301 .addReg(DstReg,
302 RegState::Define | getDeadRegState(isDead))
303 .addFrameIndex(FI).addImm(0));
Anton Korobeynikovd49ea772009-06-26 21:28:53 +0000304 }
305 break;
306 }
307 }
308
309 return NewMI;
310}