blob: 533c7368128229d24793893b8624b5a5cd80b460 [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
Chris Lattnerd90183d2009-08-02 05:20:37 +000025Thumb1InstrInfo::Thumb1InstrInfo(const ARMSubtarget &STI) : RI(*this, STI) {
Anton Korobeynikovd49ea772009-06-26 21:28:53 +000026}
27
Evan Cheng446c4282009-07-11 06:43:01 +000028unsigned Thumb1InstrInfo::getUnindexedOpcode(unsigned Opc) const {
David Goodwin334c2642009-07-08 16:09:28 +000029 return 0;
30}
31
David Goodwin334c2642009-07-08 16:09:28 +000032bool
33Thumb1InstrInfo::BlockHasNoFallThrough(const MachineBasicBlock &MBB) const {
34 if (MBB.empty()) return false;
35
36 switch (MBB.back().getOpcode()) {
37 case ARM::tBX_RET:
38 case ARM::tBX_RET_vararg:
39 case ARM::tPOP_RET:
40 case ARM::tB:
41 case ARM::tBR_JTr:
42 return true;
43 default:
44 break;
45 }
46
47 return false;
48}
49
David Goodwinb50ea5c2009-07-02 22:18:33 +000050bool Thumb1InstrInfo::copyRegToReg(MachineBasicBlock &MBB,
51 MachineBasicBlock::iterator I,
52 unsigned DestReg, unsigned SrcReg,
53 const TargetRegisterClass *DestRC,
54 const TargetRegisterClass *SrcRC) const {
Anton Korobeynikovd49ea772009-06-26 21:28:53 +000055 DebugLoc DL = DebugLoc::getUnknownLoc();
56 if (I != MBB.end()) DL = I->getDebugLoc();
57
Anton Korobeynikovd49ea772009-06-26 21:28:53 +000058 if (DestRC == ARM::GPRRegisterClass) {
59 if (SrcRC == ARM::GPRRegisterClass) {
Evan Chengd8336062009-07-26 23:59:01 +000060 BuildMI(MBB, I, DL, get(ARM::tMOVgpr2gpr), DestReg).addReg(SrcReg);
Anton Korobeynikovd49ea772009-06-26 21:28:53 +000061 return true;
62 } else if (SrcRC == ARM::tGPRRegisterClass) {
Evan Chengd8336062009-07-26 23:59:01 +000063 BuildMI(MBB, I, DL, get(ARM::tMOVtgpr2gpr), DestReg).addReg(SrcReg);
Anton Korobeynikovd49ea772009-06-26 21:28:53 +000064 return true;
65 }
66 } else if (DestRC == ARM::tGPRRegisterClass) {
67 if (SrcRC == ARM::GPRRegisterClass) {
Evan Chengd8336062009-07-26 23:59:01 +000068 BuildMI(MBB, I, DL, get(ARM::tMOVgpr2tgpr), DestReg).addReg(SrcReg);
Anton Korobeynikovd49ea772009-06-26 21:28:53 +000069 return true;
70 } else if (SrcRC == ARM::tGPRRegisterClass) {
71 BuildMI(MBB, I, DL, get(ARM::tMOVr), DestReg).addReg(SrcReg);
72 return true;
73 }
74 }
75
76 return false;
77}
78
David Goodwinb50ea5c2009-07-02 22:18:33 +000079bool Thumb1InstrInfo::
Anton Korobeynikova98cbc52009-06-27 12:16:40 +000080canFoldMemoryOperand(const MachineInstr *MI,
81 const SmallVectorImpl<unsigned> &Ops) const {
82 if (Ops.size() != 1) return false;
83
84 unsigned OpNum = Ops[0];
85 unsigned Opc = MI->getOpcode();
86 switch (Opc) {
87 default: break;
88 case ARM::tMOVr:
Evan Chengd8336062009-07-26 23:59:01 +000089 case ARM::tMOVtgpr2gpr:
90 case ARM::tMOVgpr2tgpr:
91 case ARM::tMOVgpr2gpr: {
Anton Korobeynikova98cbc52009-06-27 12:16:40 +000092 if (OpNum == 0) { // move -> store
93 unsigned SrcReg = MI->getOperand(1).getReg();
Anton Korobeynikov55ad1f22009-06-27 12:59:03 +000094 if (RI.isPhysicalRegister(SrcReg) && !isARMLowRegister(SrcReg))
Anton Korobeynikova98cbc52009-06-27 12:16:40 +000095 // tSpill cannot take a high register operand.
96 return false;
97 } else { // move -> load
98 unsigned DstReg = MI->getOperand(0).getReg();
Anton Korobeynikov55ad1f22009-06-27 12:59:03 +000099 if (RI.isPhysicalRegister(DstReg) && !isARMLowRegister(DstReg))
Anton Korobeynikova98cbc52009-06-27 12:16:40 +0000100 // tRestore cannot target a high register operand.
101 return false;
102 }
103 return true;
104 }
105 }
106
107 return false;
108}
109
David Goodwinb50ea5c2009-07-02 22:18:33 +0000110void Thumb1InstrInfo::
Anton Korobeynikovd49ea772009-06-26 21:28:53 +0000111storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
112 unsigned SrcReg, bool isKill, int FI,
113 const TargetRegisterClass *RC) const {
114 DebugLoc DL = DebugLoc::getUnknownLoc();
115 if (I != MBB.end()) DL = I->getDebugLoc();
116
117 assert(RC == ARM::tGPRRegisterClass && "Unknown regclass!");
118
Anton Korobeynikovd49ea772009-06-26 21:28:53 +0000119 if (RC == ARM::tGPRRegisterClass) {
Evan Cheng446c4282009-07-11 06:43:01 +0000120 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::tSpill))
121 .addReg(SrcReg, getKillRegState(isKill))
122 .addFrameIndex(FI).addImm(0));
Anton Korobeynikovd49ea772009-06-26 21:28:53 +0000123 }
124}
125
David Goodwinb50ea5c2009-07-02 22:18:33 +0000126void Thumb1InstrInfo::
Anton Korobeynikovd49ea772009-06-26 21:28:53 +0000127loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
128 unsigned DestReg, int FI,
129 const TargetRegisterClass *RC) const {
130 DebugLoc DL = DebugLoc::getUnknownLoc();
131 if (I != MBB.end()) DL = I->getDebugLoc();
132
Anton Korobeynikovd49ea772009-06-26 21:28:53 +0000133 assert(RC == ARM::tGPRRegisterClass && "Unknown regclass!");
134
135 if (RC == ARM::tGPRRegisterClass) {
Evan Cheng446c4282009-07-11 06:43:01 +0000136 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::tRestore), DestReg)
137 .addFrameIndex(FI).addImm(0));
Anton Korobeynikovd49ea772009-06-26 21:28:53 +0000138 }
139}
140
David Goodwinb50ea5c2009-07-02 22:18:33 +0000141bool Thumb1InstrInfo::
Anton Korobeynikovd49ea772009-06-26 21:28:53 +0000142spillCalleeSavedRegisters(MachineBasicBlock &MBB,
143 MachineBasicBlock::iterator MI,
144 const std::vector<CalleeSavedInfo> &CSI) const {
145 if (CSI.empty())
146 return false;
147
148 DebugLoc DL = DebugLoc::getUnknownLoc();
149 if (MI != MBB.end()) DL = MI->getDebugLoc();
150
151 MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, get(ARM::tPUSH));
Evan Cheng4b322e52009-08-11 21:11:32 +0000152 AddDefaultPred(MIB);
Anton Korobeynikovd49ea772009-06-26 21:28:53 +0000153 for (unsigned i = CSI.size(); i != 0; --i) {
154 unsigned Reg = CSI[i-1].getReg();
155 // Add the callee-saved register as live-in. It's killed at the spill.
156 MBB.addLiveIn(Reg);
157 MIB.addReg(Reg, RegState::Kill);
158 }
159 return true;
160}
161
David Goodwinb50ea5c2009-07-02 22:18:33 +0000162bool Thumb1InstrInfo::
Anton Korobeynikovd49ea772009-06-26 21:28:53 +0000163restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
164 MachineBasicBlock::iterator MI,
165 const std::vector<CalleeSavedInfo> &CSI) const {
166 MachineFunction &MF = *MBB.getParent();
167 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
168 if (CSI.empty())
169 return false;
170
171 bool isVarArg = AFI->getVarArgsRegSaveSize() > 0;
Evan Cheng4b322e52009-08-11 21:11:32 +0000172 DebugLoc DL = MI->getDebugLoc();
173 MachineInstrBuilder MIB = BuildMI(MF, DL, get(ARM::tPOP));
174 AddDefaultPred(MIB);
175
176 bool NumRegs = 0;
Anton Korobeynikovd49ea772009-06-26 21:28:53 +0000177 for (unsigned i = CSI.size(); i != 0; --i) {
178 unsigned Reg = CSI[i-1].getReg();
179 if (Reg == ARM::LR) {
180 // Special epilogue for vararg functions. See emitEpilogue
181 if (isVarArg)
182 continue;
183 Reg = ARM::PC;
Evan Cheng4b322e52009-08-11 21:11:32 +0000184 (*MIB).setDesc(get(ARM::tPOP_RET));
Anton Korobeynikovd49ea772009-06-26 21:28:53 +0000185 MI = MBB.erase(MI);
186 }
Evan Cheng4b322e52009-08-11 21:11:32 +0000187 MIB.addReg(Reg, getDefRegState(true));
188 ++NumRegs;
Anton Korobeynikovd49ea772009-06-26 21:28:53 +0000189 }
190
191 // It's illegal to emit pop instruction without operands.
Evan Cheng4b322e52009-08-11 21:11:32 +0000192 if (NumRegs)
193 MBB.insert(MI, &*MIB);
Anton Korobeynikovd49ea772009-06-26 21:28:53 +0000194
195 return true;
196}
197
David Goodwinb50ea5c2009-07-02 22:18:33 +0000198MachineInstr *Thumb1InstrInfo::
Anton Korobeynikovd49ea772009-06-26 21:28:53 +0000199foldMemoryOperandImpl(MachineFunction &MF, MachineInstr *MI,
200 const SmallVectorImpl<unsigned> &Ops, int FI) const {
201 if (Ops.size() != 1) return NULL;
Anton Korobeynikovd49ea772009-06-26 21:28:53 +0000202
203 unsigned OpNum = Ops[0];
204 unsigned Opc = MI->getOpcode();
205 MachineInstr *NewMI = NULL;
206 switch (Opc) {
207 default: break;
208 case ARM::tMOVr:
Evan Chengd8336062009-07-26 23:59:01 +0000209 case ARM::tMOVtgpr2gpr:
210 case ARM::tMOVgpr2tgpr:
211 case ARM::tMOVgpr2gpr: {
Anton Korobeynikovd49ea772009-06-26 21:28:53 +0000212 if (OpNum == 0) { // move -> store
213 unsigned SrcReg = MI->getOperand(1).getReg();
214 bool isKill = MI->getOperand(1).isKill();
Anton Korobeynikov55ad1f22009-06-27 12:59:03 +0000215 if (RI.isPhysicalRegister(SrcReg) && !isARMLowRegister(SrcReg))
Anton Korobeynikovd49ea772009-06-26 21:28:53 +0000216 // tSpill cannot take a high register operand.
217 break;
Evan Cheng446c4282009-07-11 06:43:01 +0000218 NewMI = AddDefaultPred(BuildMI(MF, MI->getDebugLoc(), get(ARM::tSpill))
219 .addReg(SrcReg, getKillRegState(isKill))
220 .addFrameIndex(FI).addImm(0));
Anton Korobeynikovd49ea772009-06-26 21:28:53 +0000221 } else { // move -> load
222 unsigned DstReg = MI->getOperand(0).getReg();
Anton Korobeynikov55ad1f22009-06-27 12:59:03 +0000223 if (RI.isPhysicalRegister(DstReg) && !isARMLowRegister(DstReg))
Anton Korobeynikovd49ea772009-06-26 21:28:53 +0000224 // tRestore cannot target a high register operand.
225 break;
226 bool isDead = MI->getOperand(0).isDead();
Evan Cheng446c4282009-07-11 06:43:01 +0000227 NewMI = AddDefaultPred(BuildMI(MF, MI->getDebugLoc(), get(ARM::tRestore))
228 .addReg(DstReg,
229 RegState::Define | getDeadRegState(isDead))
230 .addFrameIndex(FI).addImm(0));
Anton Korobeynikovd49ea772009-06-26 21:28:53 +0000231 }
232 break;
233 }
234 }
235
236 return NewMI;
237}