1 //===- ARMInstrInfo.cpp - ARM 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 ARM implementation of the TargetInstrInfo class.
12 //===----------------------------------------------------------------------===//
14 #include "ARMInstrInfo.h"
16 #include "ARMAddressingModes.h"
17 #include "ARMGenInstrInfo.inc"
18 #include "ARMMachineFunctionInfo.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/CodeGen/LiveVariables.h"
21 #include "llvm/CodeGen/MachineInstrBuilder.h"
22 #include "llvm/CodeGen/MachineJumpTableInfo.h"
23 #include "llvm/Target/TargetAsmInfo.h"
24 #include "llvm/Support/CommandLine.h"
27 static cl::opt<bool> EnableARM3Addr("enable-arm-3-addr-conv", cl::Hidden,
28 cl::desc("Enable ARM 2-addr to 3-addr conv"));
31 const MachineInstrBuilder &AddDefaultPred(const MachineInstrBuilder &MIB) {
32 return MIB.addImm((int64_t)ARMCC::AL).addReg(0);
36 const MachineInstrBuilder &AddDefaultCC(const MachineInstrBuilder &MIB) {
40 ARMInstrInfo::ARMInstrInfo(const ARMSubtarget &STI)
41 : TargetInstrInfo(ARMInsts, array_lengthof(ARMInsts)),
45 const TargetRegisterClass *ARMInstrInfo::getPointerRegClass() const {
46 return &ARM::GPRRegClass;
49 /// Return true if the instruction is a register to register move and
50 /// leave the source and dest operands in the passed parameters.
52 bool ARMInstrInfo::isMoveInstr(const MachineInstr &MI,
53 unsigned &SrcReg, unsigned &DstReg) const {
54 MachineOpCode oc = MI.getOpcode();
60 SrcReg = MI.getOperand(1).getReg();
61 DstReg = MI.getOperand(0).getReg();
65 assert(MI.getInstrDescriptor()->numOperands >= 2 &&
66 MI.getOperand(0).isRegister() &&
67 MI.getOperand(1).isRegister() &&
68 "Invalid ARM MOV instruction");
69 SrcReg = MI.getOperand(1).getReg();
70 DstReg = MI.getOperand(0).getReg();
75 unsigned ARMInstrInfo::isLoadFromStackSlot(MachineInstr *MI, int &FrameIndex) const{
76 switch (MI->getOpcode()) {
79 if (MI->getOperand(1).isFrameIndex() &&
80 MI->getOperand(2).isRegister() &&
81 MI->getOperand(3).isImmediate() &&
82 MI->getOperand(2).getReg() == 0 &&
83 MI->getOperand(3).getImm() == 0) {
84 FrameIndex = MI->getOperand(1).getIndex();
85 return MI->getOperand(0).getReg();
90 if (MI->getOperand(1).isFrameIndex() &&
91 MI->getOperand(2).isImmediate() &&
92 MI->getOperand(2).getImm() == 0) {
93 FrameIndex = MI->getOperand(1).getIndex();
94 return MI->getOperand(0).getReg();
98 if (MI->getOperand(1).isFrameIndex() &&
99 MI->getOperand(2).isImmediate() &&
100 MI->getOperand(2).getImm() == 0) {
101 FrameIndex = MI->getOperand(1).getIndex();
102 return MI->getOperand(0).getReg();
109 unsigned ARMInstrInfo::isStoreToStackSlot(MachineInstr *MI, int &FrameIndex) const {
110 switch (MI->getOpcode()) {
113 if (MI->getOperand(1).isFrameIndex() &&
114 MI->getOperand(2).isRegister() &&
115 MI->getOperand(3).isImmediate() &&
116 MI->getOperand(2).getReg() == 0 &&
117 MI->getOperand(3).getImm() == 0) {
118 FrameIndex = MI->getOperand(1).getIndex();
119 return MI->getOperand(0).getReg();
124 if (MI->getOperand(1).isFrameIndex() &&
125 MI->getOperand(2).isImmediate() &&
126 MI->getOperand(2).getImm() == 0) {
127 FrameIndex = MI->getOperand(1).getIndex();
128 return MI->getOperand(0).getReg();
132 if (MI->getOperand(1).isFrameIndex() &&
133 MI->getOperand(2).isImmediate() &&
134 MI->getOperand(2).getImm() == 0) {
135 FrameIndex = MI->getOperand(1).getIndex();
136 return MI->getOperand(0).getReg();
143 static unsigned getUnindexedOpcode(unsigned Opc) {
156 case ARM::LDRSH_POST:
159 case ARM::LDRSB_POST:
175 ARMInstrInfo::convertToThreeAddress(MachineFunction::iterator &MFI,
176 MachineBasicBlock::iterator &MBBI,
177 LiveVariables &LV) const {
181 MachineInstr *MI = MBBI;
182 unsigned TSFlags = MI->getInstrDescriptor()->TSFlags;
184 switch ((TSFlags & ARMII::IndexModeMask) >> ARMII::IndexModeShift) {
185 default: return NULL;
186 case ARMII::IndexModePre:
189 case ARMII::IndexModePost:
193 // Try spliting an indexed load / store to a un-indexed one plus an add/sub
195 unsigned MemOpc = getUnindexedOpcode(MI->getOpcode());
199 MachineInstr *UpdateMI = NULL;
200 MachineInstr *MemMI = NULL;
201 unsigned AddrMode = (TSFlags & ARMII::AddrModeMask);
202 const TargetInstrDescriptor *TID = MI->getInstrDescriptor();
203 unsigned NumOps = TID->numOperands;
204 bool isLoad = (TID->Flags & M_LOAD_FLAG) != 0;
205 const MachineOperand &WB = isLoad ? MI->getOperand(1) : MI->getOperand(0);
206 const MachineOperand &Base = MI->getOperand(2);
207 const MachineOperand &Offset = MI->getOperand(NumOps-3);
208 unsigned WBReg = WB.getReg();
209 unsigned BaseReg = Base.getReg();
210 unsigned OffReg = Offset.getReg();
211 unsigned OffImm = MI->getOperand(NumOps-2).getImm();
212 ARMCC::CondCodes Pred = (ARMCC::CondCodes)MI->getOperand(NumOps-1).getImm();
215 assert(false && "Unknown indexed op!");
217 case ARMII::AddrMode2: {
218 bool isSub = ARM_AM::getAM2Op(OffImm) == ARM_AM::sub;
219 unsigned Amt = ARM_AM::getAM2Offset(OffImm);
221 int SOImmVal = ARM_AM::getSOImmVal(Amt);
223 // Can't encode it in a so_imm operand. This transformation will
224 // add more than 1 instruction. Abandon!
226 UpdateMI = BuildMI(get(isSub ? ARM::SUBri : ARM::ADDri), WBReg)
227 .addReg(BaseReg).addImm(SOImmVal)
228 .addImm(Pred).addReg(0).addReg(0);
229 } else if (Amt != 0) {
230 ARM_AM::ShiftOpc ShOpc = ARM_AM::getAM2ShiftOpc(OffImm);
231 unsigned SOOpc = ARM_AM::getSORegOpc(ShOpc, Amt);
232 UpdateMI = BuildMI(get(isSub ? ARM::SUBrs : ARM::ADDrs), WBReg)
233 .addReg(BaseReg).addReg(OffReg).addReg(0).addImm(SOOpc)
234 .addImm(Pred).addReg(0).addReg(0);
236 UpdateMI = BuildMI(get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
237 .addReg(BaseReg).addReg(OffReg)
238 .addImm(Pred).addReg(0).addReg(0);
241 case ARMII::AddrMode3 : {
242 bool isSub = ARM_AM::getAM3Op(OffImm) == ARM_AM::sub;
243 unsigned Amt = ARM_AM::getAM3Offset(OffImm);
245 // Immediate is 8-bits. It's guaranteed to fit in a so_imm operand.
246 UpdateMI = BuildMI(get(isSub ? ARM::SUBri : ARM::ADDri), WBReg)
247 .addReg(BaseReg).addImm(Amt)
248 .addImm(Pred).addReg(0).addReg(0);
250 UpdateMI = BuildMI(get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
251 .addReg(BaseReg).addReg(OffReg)
252 .addImm(Pred).addReg(0).addReg(0);
257 std::vector<MachineInstr*> NewMIs;
260 MemMI = BuildMI(get(MemOpc), MI->getOperand(0).getReg())
261 .addReg(WBReg).addReg(0).addImm(0).addImm(Pred);
263 MemMI = BuildMI(get(MemOpc)).addReg(MI->getOperand(1).getReg())
264 .addReg(WBReg).addReg(0).addImm(0).addImm(Pred);
265 NewMIs.push_back(MemMI);
266 NewMIs.push_back(UpdateMI);
269 MemMI = BuildMI(get(MemOpc), MI->getOperand(0).getReg())
270 .addReg(BaseReg).addReg(0).addImm(0).addImm(Pred);
272 MemMI = BuildMI(get(MemOpc)).addReg(MI->getOperand(1).getReg())
273 .addReg(BaseReg).addReg(0).addImm(0).addImm(Pred);
275 UpdateMI->getOperand(0).setIsDead();
276 NewMIs.push_back(UpdateMI);
277 NewMIs.push_back(MemMI);
280 // Transfer LiveVariables states, kill / dead info.
281 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
282 MachineOperand &MO = MI->getOperand(i);
283 if (MO.isRegister() && MO.getReg() &&
284 MRegisterInfo::isVirtualRegister(MO.getReg())) {
285 unsigned Reg = MO.getReg();
286 LiveVariables::VarInfo &VI = LV.getVarInfo(Reg);
288 MachineInstr *NewMI = (Reg == WBReg) ? UpdateMI : MemMI;
290 LV.addVirtualRegisterDead(Reg, NewMI);
291 // Update the defining instruction.
292 if (VI.DefInst == MI)
295 if (MO.isUse() && MO.isKill()) {
296 for (unsigned j = 0; j < 2; ++j) {
297 // Look at the two new MI's in reverse order.
298 MachineInstr *NewMI = NewMIs[j];
299 int NIdx = NewMI->findRegisterUseOperandIdx(Reg);
302 LV.addVirtualRegisterKilled(Reg, NewMI);
303 if (VI.removeKill(MI))
304 VI.Kills.push_back(NewMI);
311 MFI->insert(MBBI, NewMIs[1]);
312 MFI->insert(MBBI, NewMIs[0]);
317 bool ARMInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
318 MachineBasicBlock *&FBB,
319 std::vector<MachineOperand> &Cond) const {
320 // If the block has no terminators, it just falls into the block after it.
321 MachineBasicBlock::iterator I = MBB.end();
322 if (I == MBB.begin() || !isUnpredicatedTerminator(--I))
325 // Get the last instruction in the block.
326 MachineInstr *LastInst = I;
328 // If there is only one terminator instruction, process it.
329 unsigned LastOpc = LastInst->getOpcode();
330 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
331 if (LastOpc == ARM::B || LastOpc == ARM::tB) {
332 TBB = LastInst->getOperand(0).getMBB();
335 if (LastOpc == ARM::Bcc || LastOpc == ARM::tBcc) {
336 // Block ends with fall-through condbranch.
337 TBB = LastInst->getOperand(0).getMBB();
338 Cond.push_back(LastInst->getOperand(1));
339 Cond.push_back(LastInst->getOperand(2));
342 return true; // Can't handle indirect branch.
345 // Get the instruction before it if it is a terminator.
346 MachineInstr *SecondLastInst = I;
348 // If there are three terminators, we don't know what sort of block this is.
349 if (SecondLastInst && I != MBB.begin() && isUnpredicatedTerminator(--I))
352 // If the block ends with ARM::B/ARM::tB and a ARM::Bcc/ARM::tBcc, handle it.
353 unsigned SecondLastOpc = SecondLastInst->getOpcode();
354 if ((SecondLastOpc == ARM::Bcc && LastOpc == ARM::B) ||
355 (SecondLastOpc == ARM::tBcc && LastOpc == ARM::tB)) {
356 TBB = SecondLastInst->getOperand(0).getMBB();
357 Cond.push_back(SecondLastInst->getOperand(1));
358 Cond.push_back(SecondLastInst->getOperand(2));
359 FBB = LastInst->getOperand(0).getMBB();
363 // If the block ends with two unconditional branches, handle it. The second
364 // one is not executed, so remove it.
365 if ((SecondLastOpc == ARM::B || SecondLastOpc==ARM::tB) &&
366 (LastOpc == ARM::B || LastOpc == ARM::tB)) {
367 TBB = SecondLastInst->getOperand(0).getMBB();
369 I->eraseFromParent();
373 // Likewise if it ends with a branch table followed by an unconditional branch.
374 // The branch folder can create these, and we must get rid of them for
375 // correctness of Thumb constant islands.
376 if ((SecondLastOpc == ARM::BR_JTr || SecondLastOpc==ARM::BR_JTm ||
377 SecondLastOpc == ARM::BR_JTadd || SecondLastOpc==ARM::tBR_JTr) &&
378 (LastOpc == ARM::B || LastOpc == ARM::tB)) {
380 I->eraseFromParent();
384 // Otherwise, can't handle this.
389 unsigned ARMInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
390 MachineFunction &MF = *MBB.getParent();
391 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
392 int BOpc = AFI->isThumbFunction() ? ARM::tB : ARM::B;
393 int BccOpc = AFI->isThumbFunction() ? ARM::tBcc : ARM::Bcc;
395 MachineBasicBlock::iterator I = MBB.end();
396 if (I == MBB.begin()) return 0;
398 if (I->getOpcode() != BOpc && I->getOpcode() != BccOpc)
401 // Remove the branch.
402 I->eraseFromParent();
406 if (I == MBB.begin()) return 1;
408 if (I->getOpcode() != BccOpc)
411 // Remove the branch.
412 I->eraseFromParent();
416 unsigned ARMInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
417 MachineBasicBlock *FBB,
418 const std::vector<MachineOperand> &Cond) const {
419 MachineFunction &MF = *MBB.getParent();
420 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
421 int BOpc = AFI->isThumbFunction() ? ARM::tB : ARM::B;
422 int BccOpc = AFI->isThumbFunction() ? ARM::tBcc : ARM::Bcc;
424 // Shouldn't be a fall through.
425 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
426 assert((Cond.size() == 2 || Cond.size() == 0) &&
427 "ARM branch conditions have two components!");
430 if (Cond.empty()) // Unconditional branch?
431 BuildMI(&MBB, get(BOpc)).addMBB(TBB);
433 BuildMI(&MBB, get(BccOpc)).addMBB(TBB)
434 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg());
438 // Two-way conditional branch.
439 BuildMI(&MBB, get(BccOpc)).addMBB(TBB)
440 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg());
441 BuildMI(&MBB, get(BOpc)).addMBB(FBB);
445 void ARMInstrInfo::copyRegToReg(MachineBasicBlock &MBB,
446 MachineBasicBlock::iterator I,
447 unsigned DestReg, unsigned SrcReg,
448 const TargetRegisterClass *DestRC,
449 const TargetRegisterClass *SrcRC) const {
450 if (DestRC != SrcRC) {
451 cerr << "Not yet supported!";
455 if (DestRC == ARM::GPRRegisterClass) {
456 MachineFunction &MF = *MBB.getParent();
457 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
458 if (AFI->isThumbFunction())
459 BuildMI(MBB, I, get(ARM::tMOVr), DestReg).addReg(SrcReg);
461 AddDefaultCC(AddDefaultPred(BuildMI(MBB, I, get(ARM::MOVr), DestReg)
463 } else if (DestRC == ARM::SPRRegisterClass)
464 AddDefaultPred(BuildMI(MBB, I, get(ARM::FCPYS), DestReg)
466 else if (DestRC == ARM::DPRRegisterClass)
467 AddDefaultPred(BuildMI(MBB, I, get(ARM::FCPYD), DestReg)
473 bool ARMInstrInfo::BlockHasNoFallThrough(MachineBasicBlock &MBB) const {
474 if (MBB.empty()) return false;
476 switch (MBB.back().getOpcode()) {
477 case ARM::BX_RET: // Return.
480 case ARM::tBX_RET_vararg:
483 case ARM::tB: // Uncond branch.
485 case ARM::BR_JTr: // Jumptable branch.
486 case ARM::BR_JTm: // Jumptable branch through mem.
487 case ARM::BR_JTadd: // Jumptable branch add to pc.
489 default: return false;
494 ReverseBranchCondition(std::vector<MachineOperand> &Cond) const {
495 ARMCC::CondCodes CC = (ARMCC::CondCodes)(int)Cond[0].getImm();
496 Cond[0].setImm(ARMCC::getOppositeCondition(CC));
500 bool ARMInstrInfo::isPredicated(const MachineInstr *MI) const {
501 int PIdx = MI->findFirstPredOperandIdx();
502 return PIdx != -1 && MI->getOperand(PIdx).getImm() != ARMCC::AL;
505 bool ARMInstrInfo::PredicateInstruction(MachineInstr *MI,
506 const std::vector<MachineOperand> &Pred) const {
507 unsigned Opc = MI->getOpcode();
508 if (Opc == ARM::B || Opc == ARM::tB) {
509 MI->setInstrDescriptor(get(Opc == ARM::B ? ARM::Bcc : ARM::tBcc));
510 MI->addOperand(MachineOperand::CreateImm(Pred[0].getImm()));
511 MI->addOperand(MachineOperand::CreateReg(Pred[1].getReg(), false));
515 int PIdx = MI->findFirstPredOperandIdx();
517 MachineOperand &PMO = MI->getOperand(PIdx);
518 PMO.setImm(Pred[0].getImm());
519 MI->getOperand(PIdx+1).setReg(Pred[1].getReg());
526 ARMInstrInfo::SubsumesPredicate(const std::vector<MachineOperand> &Pred1,
527 const std::vector<MachineOperand> &Pred2) const{
528 if (Pred1.size() > 2 || Pred2.size() > 2)
531 ARMCC::CondCodes CC1 = (ARMCC::CondCodes)Pred1[0].getImm();
532 ARMCC::CondCodes CC2 = (ARMCC::CondCodes)Pred2[0].getImm();
542 return CC2 == ARMCC::HI;
544 return CC2 == ARMCC::LO || CC2 == ARMCC::EQ;
546 return CC2 == ARMCC::GT;
548 return CC2 == ARMCC::LT;
552 bool ARMInstrInfo::DefinesPredicate(MachineInstr *MI,
553 std::vector<MachineOperand> &Pred) const {
554 const TargetInstrDescriptor *TID = MI->getInstrDescriptor();
555 if (!TID->ImplicitDefs && (TID->Flags & M_HAS_OPTIONAL_DEF) == 0)
559 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
560 const MachineOperand &MO = MI->getOperand(i);
561 if (MO.isRegister() && MO.getReg() == ARM::CPSR) {
571 /// FIXME: Works around a gcc miscompilation with -fstrict-aliasing
572 static unsigned getNumJTEntries(const std::vector<MachineJumpTableEntry> &JT,
573 unsigned JTI) DISABLE_INLINE;
574 static unsigned getNumJTEntries(const std::vector<MachineJumpTableEntry> &JT,
576 return JT[JTI].MBBs.size();
579 /// GetInstSize - Return the size of the specified MachineInstr.
581 unsigned ARM::GetInstSize(MachineInstr *MI) {
582 MachineBasicBlock &MBB = *MI->getParent();
583 const MachineFunction *MF = MBB.getParent();
584 const TargetAsmInfo *TAI = MF->getTarget().getTargetAsmInfo();
586 // Basic size info comes from the TSFlags field.
587 const TargetInstrDescriptor *TID = MI->getInstrDescriptor();
588 unsigned TSFlags = TID->TSFlags;
590 switch ((TSFlags & ARMII::SizeMask) >> ARMII::SizeShift) {
592 // If this machine instr is an inline asm, measure it.
593 if (MI->getOpcode() == ARM::INLINEASM)
594 return TAI->getInlineAsmLength(MI->getOperand(0).getSymbolName());
595 if (MI->getOpcode() == ARM::LABEL)
597 assert(0 && "Unknown or unset size field for instr!");
599 case ARMII::Size8Bytes: return 8; // Arm instruction x 2.
600 case ARMII::Size4Bytes: return 4; // Arm instruction.
601 case ARMII::Size2Bytes: return 2; // Thumb instruction.
602 case ARMII::SizeSpecial: {
603 switch (MI->getOpcode()) {
604 case ARM::CONSTPOOL_ENTRY:
605 // If this machine instr is a constant pool entry, its size is recorded as
607 return MI->getOperand(2).getImm();
612 // These are jumptable branches, i.e. a branch followed by an inlined
613 // jumptable. The size is 4 + 4 * number of entries.
614 unsigned NumOps = TID->numOperands;
615 MachineOperand JTOP =
616 MI->getOperand(NumOps - ((TID->Flags & M_PREDICABLE) ? 3 : 2));
617 unsigned JTI = JTOP.getIndex();
618 MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
619 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
620 assert(JTI < JT.size());
621 // Thumb instructions are 2 byte aligned, but JT entries are 4 byte
622 // 4 aligned. The assembler / linker may add 2 byte padding just before
623 // the JT entries. The size does not include this padding; the
624 // constant islands pass does separate bookkeeping for it.
625 // FIXME: If we know the size of the function is less than (1 << 16) *2
626 // bytes, we can use 16-bit entries instead. Then there won't be an
628 return getNumJTEntries(JT, JTI) * 4 +
629 (MI->getOpcode()==ARM::tBR_JTr ? 2 : 4);
632 // Otherwise, pseudo-instruction sizes are zero.
639 /// GetFunctionSize - Returns the size of the specified MachineFunction.
641 unsigned ARM::GetFunctionSize(MachineFunction &MF) {
643 for (MachineFunction::iterator MBBI = MF.begin(), E = MF.end();
645 MachineBasicBlock &MBB = *MBBI;
646 for (MachineBasicBlock::iterator I = MBB.begin(),E = MBB.end(); I != E; ++I)
647 FnSize += ARM::GetInstSize(I);