1 //===- ARMInstrInfo.cpp - ARM Instruction Information -----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the "Instituto Nokia de Tecnologia" and
6 // is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
11 // This file contains the ARM implementation of the TargetInstrInfo class.
13 //===----------------------------------------------------------------------===//
15 #include "ARMInstrInfo.h"
17 #include "ARMAddressingModes.h"
18 #include "ARMGenInstrInfo.inc"
19 #include "ARMMachineFunctionInfo.h"
20 #include "llvm/CodeGen/MachineInstrBuilder.h"
21 #include "llvm/CodeGen/LiveVariables.h"
22 #include "llvm/Support/CommandLine.h"
25 static cl::opt<bool> EnableARM3Addr("enable-arm-3-addr-conv", cl::Hidden,
26 cl::desc("Enable ARM 2-addr to 3-addr conv"));
28 ARMInstrInfo::ARMInstrInfo(const ARMSubtarget &STI)
29 : TargetInstrInfo(ARMInsts, sizeof(ARMInsts)/sizeof(ARMInsts[0])),
33 const TargetRegisterClass *ARMInstrInfo::getPointerRegClass() const {
34 return &ARM::GPRRegClass;
37 /// Return true if the instruction is a register to register move and
38 /// leave the source and dest operands in the passed parameters.
40 bool ARMInstrInfo::isMoveInstr(const MachineInstr &MI,
41 unsigned &SrcReg, unsigned &DstReg) const {
42 MachineOpCode oc = MI.getOpcode();
48 SrcReg = MI.getOperand(1).getReg();
49 DstReg = MI.getOperand(0).getReg();
53 assert(MI.getNumOperands() == 2 && MI.getOperand(0).isRegister() &&
54 MI.getOperand(1).isRegister() &&
55 "Invalid ARM MOV instruction");
56 SrcReg = MI.getOperand(1).getReg();
57 DstReg = MI.getOperand(0).getReg();
62 unsigned ARMInstrInfo::isLoadFromStackSlot(MachineInstr *MI, int &FrameIndex) const{
63 switch (MI->getOpcode()) {
66 if (MI->getOperand(1).isFrameIndex() &&
67 MI->getOperand(2).isReg() &&
68 MI->getOperand(3).isImmediate() &&
69 MI->getOperand(2).getReg() == 0 &&
70 MI->getOperand(3).getImmedValue() == 0) {
71 FrameIndex = MI->getOperand(1).getFrameIndex();
72 return MI->getOperand(0).getReg();
77 if (MI->getOperand(1).isFrameIndex() &&
78 MI->getOperand(2).isImmediate() &&
79 MI->getOperand(2).getImmedValue() == 0) {
80 FrameIndex = MI->getOperand(1).getFrameIndex();
81 return MI->getOperand(0).getReg();
85 if (MI->getOperand(1).isFrameIndex() &&
86 MI->getOperand(2).isImmediate() &&
87 MI->getOperand(2).getImmedValue() == 0) {
88 FrameIndex = MI->getOperand(1).getFrameIndex();
89 return MI->getOperand(0).getReg();
96 unsigned ARMInstrInfo::isStoreToStackSlot(MachineInstr *MI, int &FrameIndex) const {
97 switch (MI->getOpcode()) {
100 if (MI->getOperand(1).isFrameIndex() &&
101 MI->getOperand(2).isReg() &&
102 MI->getOperand(3).isImmediate() &&
103 MI->getOperand(2).getReg() == 0 &&
104 MI->getOperand(3).getImmedValue() == 0) {
105 FrameIndex = MI->getOperand(1).getFrameIndex();
106 return MI->getOperand(0).getReg();
111 if (MI->getOperand(1).isFrameIndex() &&
112 MI->getOperand(2).isImmediate() &&
113 MI->getOperand(2).getImmedValue() == 0) {
114 FrameIndex = MI->getOperand(1).getFrameIndex();
115 return MI->getOperand(0).getReg();
119 if (MI->getOperand(1).isFrameIndex() &&
120 MI->getOperand(2).isImmediate() &&
121 MI->getOperand(2).getImmedValue() == 0) {
122 FrameIndex = MI->getOperand(1).getFrameIndex();
123 return MI->getOperand(0).getReg();
130 static unsigned getUnindexedOpcode(unsigned Opc) {
143 case ARM::LDRSH_POST:
146 case ARM::LDRSB_POST:
162 ARMInstrInfo::convertToThreeAddress(MachineFunction::iterator &MFI,
163 MachineBasicBlock::iterator &MBBI,
164 LiveVariables &LV) const {
168 MachineInstr *MI = MBBI;
169 unsigned TSFlags = MI->getInstrDescriptor()->TSFlags;
171 switch ((TSFlags & ARMII::IndexModeMask) >> ARMII::IndexModeShift) {
172 default: return NULL;
173 case ARMII::IndexModePre:
176 case ARMII::IndexModePost:
180 // Try spliting an indexed load / store to a un-indexed one plus an add/sub
182 unsigned MemOpc = getUnindexedOpcode(MI->getOpcode());
186 MachineInstr *UpdateMI = NULL;
187 MachineInstr *MemMI = NULL;
188 unsigned AddrMode = (TSFlags & ARMII::AddrModeMask);
189 unsigned NumOps = MI->getNumOperands();
190 bool isLoad = (MI->getInstrDescriptor()->Flags & M_LOAD_FLAG) != 0;
191 const MachineOperand &WB = isLoad ? MI->getOperand(1) : MI->getOperand(0);
192 const MachineOperand &Base = MI->getOperand(2);
193 const MachineOperand &Offset = MI->getOperand(NumOps-2);
194 unsigned WBReg = WB.getReg();
195 unsigned BaseReg = Base.getReg();
196 unsigned OffReg = Offset.getReg();
197 unsigned OffImm = MI->getOperand(NumOps-1).getImm();
200 assert(false && "Unknown indexed op!");
202 case ARMII::AddrMode2: {
203 bool isSub = ARM_AM::getAM2Op(OffImm) == ARM_AM::sub;
204 unsigned Amt = ARM_AM::getAM2Offset(OffImm);
206 int SOImmVal = ARM_AM::getSOImmVal(Amt);
208 // Can't encode it in a so_imm operand. This transformation will
209 // add more than 1 instruction. Abandon!
211 UpdateMI = BuildMI(get(isSub ? ARM::SUBri : ARM::ADDri), WBReg)
212 .addReg(BaseReg).addImm(SOImmVal);
213 } else if (Amt != 0) {
214 ARM_AM::ShiftOpc ShOpc = ARM_AM::getAM2ShiftOpc(OffImm);
215 unsigned SOOpc = ARM_AM::getSORegOpc(ShOpc, Amt);
216 UpdateMI = BuildMI(get(isSub ? ARM::SUBrs : ARM::ADDrs), WBReg)
217 .addReg(BaseReg).addReg(OffReg).addReg(0).addImm(SOOpc);
219 UpdateMI = BuildMI(get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
220 .addReg(BaseReg).addReg(OffReg);
223 case ARMII::AddrMode3 : {
224 bool isSub = ARM_AM::getAM3Op(OffImm) == ARM_AM::sub;
225 unsigned Amt = ARM_AM::getAM3Offset(OffImm);
227 // Immediate is 8-bits. It's guaranteed to fit in a so_imm operand.
228 UpdateMI = BuildMI(get(isSub ? ARM::SUBri : ARM::ADDri), WBReg)
229 .addReg(BaseReg).addImm(Amt);
231 UpdateMI = BuildMI(get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
232 .addReg(BaseReg).addReg(OffReg);
237 std::vector<MachineInstr*> NewMIs;
240 MemMI = BuildMI(get(MemOpc), MI->getOperand(0).getReg())
241 .addReg(WBReg).addReg(0).addImm(0);
243 MemMI = BuildMI(get(MemOpc)).addReg(MI->getOperand(1).getReg())
244 .addReg(WBReg).addReg(0).addImm(0);
245 NewMIs.push_back(MemMI);
246 NewMIs.push_back(UpdateMI);
249 MemMI = BuildMI(get(MemOpc), MI->getOperand(0).getReg())
250 .addReg(BaseReg).addReg(0).addImm(0);
252 MemMI = BuildMI(get(MemOpc)).addReg(MI->getOperand(1).getReg())
253 .addReg(BaseReg).addReg(0).addImm(0);
255 UpdateMI->getOperand(0).setIsDead();
256 NewMIs.push_back(UpdateMI);
257 NewMIs.push_back(MemMI);
260 // Transfer LiveVariables states, kill / dead info.
261 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
262 MachineOperand &MO = MI->getOperand(i);
263 if (MO.isRegister() && MO.getReg() &&
264 MRegisterInfo::isVirtualRegister(MO.getReg())) {
265 unsigned Reg = MO.getReg();
266 LiveVariables::VarInfo &VI = LV.getVarInfo(Reg);
268 MachineInstr *NewMI = (Reg == WBReg) ? UpdateMI : MemMI;
270 LV.addVirtualRegisterDead(Reg, NewMI);
271 // Update the defining instruction.
272 if (VI.DefInst == MI)
275 if (MO.isUse() && MO.isKill()) {
276 for (unsigned j = 0; j < 2; ++j) {
277 // Look at the two new MI's in reverse order.
278 MachineInstr *NewMI = NewMIs[j];
279 MachineOperand *NMO = NewMI->findRegisterUseOperand(Reg);
282 LV.addVirtualRegisterKilled(Reg, NewMI);
283 if (VI.removeKill(MI))
284 VI.Kills.push_back(NewMI);
291 MFI->insert(MBBI, NewMIs[1]);
292 MFI->insert(MBBI, NewMIs[0]);
297 bool ARMInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
298 MachineBasicBlock *&FBB,
299 std::vector<MachineOperand> &Cond) const {
300 // If the block has no terminators, it just falls into the block after it.
301 MachineBasicBlock::iterator I = MBB.end();
302 if (I == MBB.begin() || !isTerminatorInstr((--I)->getOpcode()))
305 // Get the last instruction in the block.
306 MachineInstr *LastInst = I;
308 // If there is only one terminator instruction, process it.
309 unsigned LastOpc = LastInst->getOpcode();
310 if (I == MBB.begin() || !isTerminatorInstr((--I)->getOpcode())) {
311 if (LastOpc == ARM::B || LastOpc == ARM::tB) {
312 TBB = LastInst->getOperand(0).getMachineBasicBlock();
315 if (LastOpc == ARM::Bcc || LastOpc == ARM::tBcc) {
316 // Block ends with fall-through condbranch.
317 TBB = LastInst->getOperand(0).getMachineBasicBlock();
318 Cond.push_back(LastInst->getOperand(1));
321 return true; // Can't handle indirect branch.
324 // Get the instruction before it if it is a terminator.
325 MachineInstr *SecondLastInst = I;
327 // If there are three terminators, we don't know what sort of block this is.
328 if (SecondLastInst && I != MBB.begin() &&
329 isTerminatorInstr((--I)->getOpcode()))
332 // If the block ends with ARM::B/ARM::tB and a ARM::Bcc/ARM::tBcc, handle it.
333 unsigned SecondLastOpc = SecondLastInst->getOpcode();
334 if ((SecondLastOpc == ARM::Bcc && LastOpc == ARM::B) ||
335 (SecondLastOpc == ARM::tBcc && LastOpc == ARM::tB)) {
336 TBB = SecondLastInst->getOperand(0).getMachineBasicBlock();
337 Cond.push_back(SecondLastInst->getOperand(1));
338 FBB = LastInst->getOperand(0).getMachineBasicBlock();
342 // Otherwise, can't handle this.
347 void ARMInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
348 MachineFunction &MF = *MBB.getParent();
349 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
350 int BOpc = AFI->isThumbFunction() ? ARM::tB : ARM::B;
351 int BccOpc = AFI->isThumbFunction() ? ARM::tBcc : ARM::Bcc;
353 MachineBasicBlock::iterator I = MBB.end();
354 if (I == MBB.begin()) return;
356 if (I->getOpcode() != BOpc && I->getOpcode() != BccOpc)
359 // Remove the branch.
360 I->eraseFromParent();
364 if (I == MBB.begin()) return;
366 if (I->getOpcode() != BccOpc)
369 // Remove the branch.
370 I->eraseFromParent();
373 void ARMInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
374 MachineBasicBlock *FBB,
375 const std::vector<MachineOperand> &Cond) const {
376 MachineFunction &MF = *MBB.getParent();
377 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
378 int BOpc = AFI->isThumbFunction() ? ARM::tB : ARM::B;
379 int BccOpc = AFI->isThumbFunction() ? ARM::tBcc : ARM::Bcc;
381 // Shouldn't be a fall through.
382 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
383 assert((Cond.size() == 1 || Cond.size() == 0) &&
384 "ARM branch conditions have two components!");
387 if (Cond.empty()) // Unconditional branch?
388 BuildMI(&MBB, get(BOpc)).addMBB(TBB);
390 BuildMI(&MBB, get(BccOpc)).addMBB(TBB).addImm(Cond[0].getImm());
394 // Two-way conditional branch.
395 BuildMI(&MBB, get(BccOpc)).addMBB(TBB).addImm(Cond[0].getImm());
396 BuildMI(&MBB, get(BOpc)).addMBB(FBB);
399 bool ARMInstrInfo::BlockHasNoFallThrough(MachineBasicBlock &MBB) const {
400 if (MBB.empty()) return false;
402 switch (MBB.back().getOpcode()) {
404 case ARM::tB: // Uncond branch.
405 case ARM::BR_JTr: // Jumptable branch.
406 case ARM::BR_JTm: // Jumptable branch through mem.
407 case ARM::BR_JTadd: // Jumptable branch add to pc.
409 default: return false;
414 ReverseBranchCondition(std::vector<MachineOperand> &Cond) const {
415 ARMCC::CondCodes CC = (ARMCC::CondCodes)(int)Cond[0].getImm();
416 Cond[0].setImm(ARMCC::getOppositeCondition(CC));