1 //===- Thumb1InstrInfo.cpp - Thumb-1 Instruction Information --------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the Thumb-1 implementation of the TargetInstrInfo class.
12 //===----------------------------------------------------------------------===//
14 #include "ARMInstrInfo.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"
25 Thumb1InstrInfo::Thumb1InstrInfo(const ARMSubtarget &STI)
26 : ARMBaseInstrInfo(STI), RI(*this, STI) {
29 unsigned Thumb1InstrInfo::getUnindexedOpcode(unsigned Opc) const {
33 unsigned Thumb1InstrInfo::getOpcode(ARMII::Op Op) const {
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;
52 Thumb1InstrInfo::BlockHasNoFallThrough(const MachineBasicBlock &MBB) const {
53 if (MBB.empty()) return false;
55 switch (MBB.back().getOpcode()) {
57 case ARM::tBX_RET_vararg:
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();
77 if (DestRC == ARM::GPRRegisterClass) {
78 if (SrcRC == ARM::GPRRegisterClass) {
79 BuildMI(MBB, I, DL, get(ARM::tMOVgpr2gpr), DestReg).addReg(SrcReg);
81 } else if (SrcRC == ARM::tGPRRegisterClass) {
82 BuildMI(MBB, I, DL, get(ARM::tMOVtgpr2gpr), DestReg).addReg(SrcReg);
85 } else if (DestRC == ARM::tGPRRegisterClass) {
86 if (SrcRC == ARM::GPRRegisterClass) {
87 BuildMI(MBB, I, DL, get(ARM::tMOVgpr2tgpr), DestReg).addReg(SrcReg);
89 } else if (SrcRC == ARM::tGPRRegisterClass) {
90 BuildMI(MBB, I, DL, get(ARM::tMOVr), DestReg).addReg(SrcReg);
98 bool Thumb1InstrInfo::
99 canFoldMemoryOperand(const MachineInstr *MI,
100 const SmallVectorImpl<unsigned> &Ops) const {
101 if (Ops.size() != 1) return false;
103 unsigned OpNum = Ops[0];
104 unsigned Opc = MI->getOpcode();
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.
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.
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();
136 assert(RC == ARM::tGPRRegisterClass && "Unknown regclass!");
138 if (RC == ARM::tGPRRegisterClass) {
139 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::tSpill))
140 .addReg(SrcReg, getKillRegState(isKill))
141 .addFrameIndex(FI).addImm(0));
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();
152 assert(RC == ARM::tGPRRegisterClass && "Unknown regclass!");
154 if (RC == ARM::tGPRRegisterClass) {
155 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::tRestore), DestReg)
156 .addFrameIndex(FI).addImm(0));
160 bool Thumb1InstrInfo::
161 spillCalleeSavedRegisters(MachineBasicBlock &MBB,
162 MachineBasicBlock::iterator MI,
163 const std::vector<CalleeSavedInfo> &CSI) const {
167 DebugLoc DL = DebugLoc::getUnknownLoc();
168 if (MI != MBB.end()) DL = MI->getDebugLoc();
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.
175 MIB.addReg(Reg, RegState::Kill);
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>();
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
198 PopMI->setDesc(get(ARM::tPOP_RET));
201 PopMI->addOperand(MachineOperand::CreateReg(Reg, true));
204 // It's illegal to emit pop instruction without operands.
205 if (PopMI->getNumOperands() > 0)
206 MBB.insert(MI, PopMI);
211 MachineInstr *Thumb1InstrInfo::
212 foldMemoryOperandImpl(MachineFunction &MF, MachineInstr *MI,
213 const SmallVectorImpl<unsigned> &Ops, int FI) const {
214 if (Ops.size() != 1) return NULL;
216 unsigned OpNum = Ops[0];
217 unsigned Opc = MI->getOpcode();
218 MachineInstr *NewMI = NULL;
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.
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.
239 bool isDead = MI->getOperand(0).isDead();
240 NewMI = AddDefaultPred(BuildMI(MF, MI->getDebugLoc(), get(ARM::tRestore))
242 RegState::Define | getDeadRegState(isDead))
243 .addFrameIndex(FI).addImm(0));