1 //===-- ARMBaseInstrInfo.cpp - ARM Instruction Information ----------------===//
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 Base ARM implementation of the TargetInstrInfo class.
12 //===----------------------------------------------------------------------===//
15 #include "ARMBaseInstrInfo.h"
16 #include "ARMBaseRegisterInfo.h"
17 #include "ARMConstantPoolValue.h"
18 #include "ARMFeatures.h"
19 #include "ARMHazardRecognizer.h"
20 #include "ARMMachineFunctionInfo.h"
21 #include "MCTargetDesc/ARMAddressingModes.h"
22 #include "llvm/ADT/STLExtras.h"
23 #include "llvm/CodeGen/LiveVariables.h"
24 #include "llvm/CodeGen/MachineConstantPool.h"
25 #include "llvm/CodeGen/MachineFrameInfo.h"
26 #include "llvm/CodeGen/MachineInstrBuilder.h"
27 #include "llvm/CodeGen/MachineJumpTableInfo.h"
28 #include "llvm/CodeGen/MachineMemOperand.h"
29 #include "llvm/CodeGen/MachineRegisterInfo.h"
30 #include "llvm/CodeGen/SelectionDAGNodes.h"
31 #include "llvm/IR/Constants.h"
32 #include "llvm/IR/Function.h"
33 #include "llvm/IR/GlobalValue.h"
34 #include "llvm/MC/MCAsmInfo.h"
35 #include "llvm/MC/MCExpr.h"
36 #include "llvm/Support/BranchProbability.h"
37 #include "llvm/Support/CommandLine.h"
38 #include "llvm/Support/Debug.h"
39 #include "llvm/Support/ErrorHandling.h"
43 #define DEBUG_TYPE "arm-instrinfo"
45 #define GET_INSTRINFO_CTOR_DTOR
46 #include "ARMGenInstrInfo.inc"
49 EnableARM3Addr("enable-arm-3-addr-conv", cl::Hidden,
50 cl::desc("Enable ARM 2-addr to 3-addr conv"));
53 WidenVMOVS("widen-vmovs", cl::Hidden, cl::init(true),
54 cl::desc("Widen ARM vmovs to vmovd when possible"));
56 static cl::opt<unsigned>
57 SwiftPartialUpdateClearance("swift-partial-update-clearance",
58 cl::Hidden, cl::init(12),
59 cl::desc("Clearance before partial register updates"));
61 /// ARM_MLxEntry - Record information about MLA / MLS instructions.
63 uint16_t MLxOpc; // MLA / MLS opcode
64 uint16_t MulOpc; // Expanded multiplication opcode
65 uint16_t AddSubOpc; // Expanded add / sub opcode
66 bool NegAcc; // True if the acc is negated before the add / sub.
67 bool HasLane; // True if instruction has an extra "lane" operand.
70 static const ARM_MLxEntry ARM_MLxTable[] = {
71 // MLxOpc, MulOpc, AddSubOpc, NegAcc, HasLane
73 { ARM::VMLAS, ARM::VMULS, ARM::VADDS, false, false },
74 { ARM::VMLSS, ARM::VMULS, ARM::VSUBS, false, false },
75 { ARM::VMLAD, ARM::VMULD, ARM::VADDD, false, false },
76 { ARM::VMLSD, ARM::VMULD, ARM::VSUBD, false, false },
77 { ARM::VNMLAS, ARM::VNMULS, ARM::VSUBS, true, false },
78 { ARM::VNMLSS, ARM::VMULS, ARM::VSUBS, true, false },
79 { ARM::VNMLAD, ARM::VNMULD, ARM::VSUBD, true, false },
80 { ARM::VNMLSD, ARM::VMULD, ARM::VSUBD, true, false },
83 { ARM::VMLAfd, ARM::VMULfd, ARM::VADDfd, false, false },
84 { ARM::VMLSfd, ARM::VMULfd, ARM::VSUBfd, false, false },
85 { ARM::VMLAfq, ARM::VMULfq, ARM::VADDfq, false, false },
86 { ARM::VMLSfq, ARM::VMULfq, ARM::VSUBfq, false, false },
87 { ARM::VMLAslfd, ARM::VMULslfd, ARM::VADDfd, false, true },
88 { ARM::VMLSslfd, ARM::VMULslfd, ARM::VSUBfd, false, true },
89 { ARM::VMLAslfq, ARM::VMULslfq, ARM::VADDfq, false, true },
90 { ARM::VMLSslfq, ARM::VMULslfq, ARM::VSUBfq, false, true },
93 ARMBaseInstrInfo::ARMBaseInstrInfo(const ARMSubtarget& STI)
94 : ARMGenInstrInfo(ARM::ADJCALLSTACKDOWN, ARM::ADJCALLSTACKUP),
96 for (unsigned i = 0, e = array_lengthof(ARM_MLxTable); i != e; ++i) {
97 if (!MLxEntryMap.insert(std::make_pair(ARM_MLxTable[i].MLxOpc, i)).second)
98 assert(false && "Duplicated entries?");
99 MLxHazardOpcodes.insert(ARM_MLxTable[i].AddSubOpc);
100 MLxHazardOpcodes.insert(ARM_MLxTable[i].MulOpc);
104 // Use a ScoreboardHazardRecognizer for prepass ARM scheduling. TargetInstrImpl
105 // currently defaults to no prepass hazard recognizer.
106 ScheduleHazardRecognizer *
107 ARMBaseInstrInfo::CreateTargetHazardRecognizer(const TargetSubtargetInfo *STI,
108 const ScheduleDAG *DAG) const {
109 if (usePreRAHazardRecognizer()) {
110 const InstrItineraryData *II =
111 &static_cast<const ARMSubtarget *>(STI)->getInstrItineraryData();
112 return new ScoreboardHazardRecognizer(II, DAG, "pre-RA-sched");
114 return TargetInstrInfo::CreateTargetHazardRecognizer(STI, DAG);
117 ScheduleHazardRecognizer *ARMBaseInstrInfo::
118 CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II,
119 const ScheduleDAG *DAG) const {
120 if (Subtarget.isThumb2() || Subtarget.hasVFP2())
121 return (ScheduleHazardRecognizer *)new ARMHazardRecognizer(II, DAG);
122 return TargetInstrInfo::CreateTargetPostRAHazardRecognizer(II, DAG);
126 ARMBaseInstrInfo::convertToThreeAddress(MachineFunction::iterator &MFI,
127 MachineBasicBlock::iterator &MBBI,
128 LiveVariables *LV) const {
129 // FIXME: Thumb2 support.
134 MachineInstr *MI = MBBI;
135 MachineFunction &MF = *MI->getParent()->getParent();
136 uint64_t TSFlags = MI->getDesc().TSFlags;
138 switch ((TSFlags & ARMII::IndexModeMask) >> ARMII::IndexModeShift) {
139 default: return nullptr;
140 case ARMII::IndexModePre:
143 case ARMII::IndexModePost:
147 // Try splitting an indexed load/store to an un-indexed one plus an add/sub
149 unsigned MemOpc = getUnindexedOpcode(MI->getOpcode());
153 MachineInstr *UpdateMI = nullptr;
154 MachineInstr *MemMI = nullptr;
155 unsigned AddrMode = (TSFlags & ARMII::AddrModeMask);
156 const MCInstrDesc &MCID = MI->getDesc();
157 unsigned NumOps = MCID.getNumOperands();
158 bool isLoad = !MI->mayStore();
159 const MachineOperand &WB = isLoad ? MI->getOperand(1) : MI->getOperand(0);
160 const MachineOperand &Base = MI->getOperand(2);
161 const MachineOperand &Offset = MI->getOperand(NumOps-3);
162 unsigned WBReg = WB.getReg();
163 unsigned BaseReg = Base.getReg();
164 unsigned OffReg = Offset.getReg();
165 unsigned OffImm = MI->getOperand(NumOps-2).getImm();
166 ARMCC::CondCodes Pred = (ARMCC::CondCodes)MI->getOperand(NumOps-1).getImm();
168 default: llvm_unreachable("Unknown indexed op!");
169 case ARMII::AddrMode2: {
170 bool isSub = ARM_AM::getAM2Op(OffImm) == ARM_AM::sub;
171 unsigned Amt = ARM_AM::getAM2Offset(OffImm);
173 if (ARM_AM::getSOImmVal(Amt) == -1)
174 // Can't encode it in a so_imm operand. This transformation will
175 // add more than 1 instruction. Abandon!
177 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
178 get(isSub ? ARM::SUBri : ARM::ADDri), WBReg)
179 .addReg(BaseReg).addImm(Amt)
180 .addImm(Pred).addReg(0).addReg(0);
181 } else if (Amt != 0) {
182 ARM_AM::ShiftOpc ShOpc = ARM_AM::getAM2ShiftOpc(OffImm);
183 unsigned SOOpc = ARM_AM::getSORegOpc(ShOpc, Amt);
184 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
185 get(isSub ? ARM::SUBrsi : ARM::ADDrsi), WBReg)
186 .addReg(BaseReg).addReg(OffReg).addReg(0).addImm(SOOpc)
187 .addImm(Pred).addReg(0).addReg(0);
189 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
190 get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
191 .addReg(BaseReg).addReg(OffReg)
192 .addImm(Pred).addReg(0).addReg(0);
195 case ARMII::AddrMode3 : {
196 bool isSub = ARM_AM::getAM3Op(OffImm) == ARM_AM::sub;
197 unsigned Amt = ARM_AM::getAM3Offset(OffImm);
199 // Immediate is 8-bits. It's guaranteed to fit in a so_imm operand.
200 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
201 get(isSub ? ARM::SUBri : ARM::ADDri), WBReg)
202 .addReg(BaseReg).addImm(Amt)
203 .addImm(Pred).addReg(0).addReg(0);
205 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
206 get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
207 .addReg(BaseReg).addReg(OffReg)
208 .addImm(Pred).addReg(0).addReg(0);
213 std::vector<MachineInstr*> NewMIs;
216 MemMI = BuildMI(MF, MI->getDebugLoc(),
217 get(MemOpc), MI->getOperand(0).getReg())
218 .addReg(WBReg).addImm(0).addImm(Pred);
220 MemMI = BuildMI(MF, MI->getDebugLoc(),
221 get(MemOpc)).addReg(MI->getOperand(1).getReg())
222 .addReg(WBReg).addReg(0).addImm(0).addImm(Pred);
223 NewMIs.push_back(MemMI);
224 NewMIs.push_back(UpdateMI);
227 MemMI = BuildMI(MF, MI->getDebugLoc(),
228 get(MemOpc), MI->getOperand(0).getReg())
229 .addReg(BaseReg).addImm(0).addImm(Pred);
231 MemMI = BuildMI(MF, MI->getDebugLoc(),
232 get(MemOpc)).addReg(MI->getOperand(1).getReg())
233 .addReg(BaseReg).addReg(0).addImm(0).addImm(Pred);
235 UpdateMI->getOperand(0).setIsDead();
236 NewMIs.push_back(UpdateMI);
237 NewMIs.push_back(MemMI);
240 // Transfer LiveVariables states, kill / dead info.
242 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
243 MachineOperand &MO = MI->getOperand(i);
244 if (MO.isReg() && TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
245 unsigned Reg = MO.getReg();
247 LiveVariables::VarInfo &VI = LV->getVarInfo(Reg);
249 MachineInstr *NewMI = (Reg == WBReg) ? UpdateMI : MemMI;
251 LV->addVirtualRegisterDead(Reg, NewMI);
253 if (MO.isUse() && MO.isKill()) {
254 for (unsigned j = 0; j < 2; ++j) {
255 // Look at the two new MI's in reverse order.
256 MachineInstr *NewMI = NewMIs[j];
257 if (!NewMI->readsRegister(Reg))
259 LV->addVirtualRegisterKilled(Reg, NewMI);
260 if (VI.removeKill(MI))
261 VI.Kills.push_back(NewMI);
269 MFI->insert(MBBI, NewMIs[1]);
270 MFI->insert(MBBI, NewMIs[0]);
276 ARMBaseInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
277 MachineBasicBlock *&FBB,
278 SmallVectorImpl<MachineOperand> &Cond,
279 bool AllowModify) const {
283 MachineBasicBlock::iterator I = MBB.end();
284 if (I == MBB.begin())
285 return false; // Empty blocks are easy.
288 // Walk backwards from the end of the basic block until the branch is
289 // analyzed or we give up.
290 while (isPredicated(I) || I->isTerminator() || I->isDebugValue()) {
292 // Flag to be raised on unanalyzeable instructions. This is useful in cases
293 // where we want to clean up on the end of the basic block before we bail
295 bool CantAnalyze = false;
297 // Skip over DEBUG values and predicated nonterminators.
298 while (I->isDebugValue() || !I->isTerminator()) {
299 if (I == MBB.begin())
304 if (isIndirectBranchOpcode(I->getOpcode()) ||
305 isJumpTableBranchOpcode(I->getOpcode())) {
306 // Indirect branches and jump tables can't be analyzed, but we still want
307 // to clean up any instructions at the tail of the basic block.
309 } else if (isUncondBranchOpcode(I->getOpcode())) {
310 TBB = I->getOperand(0).getMBB();
311 } else if (isCondBranchOpcode(I->getOpcode())) {
312 // Bail out if we encounter multiple conditional branches.
316 assert(!FBB && "FBB should have been null.");
318 TBB = I->getOperand(0).getMBB();
319 Cond.push_back(I->getOperand(1));
320 Cond.push_back(I->getOperand(2));
321 } else if (I->isReturn()) {
322 // Returns can't be analyzed, but we should run cleanup.
323 CantAnalyze = !isPredicated(I);
325 // We encountered other unrecognized terminator. Bail out immediately.
329 // Cleanup code - to be run for unpredicated unconditional branches and
331 if (!isPredicated(I) &&
332 (isUncondBranchOpcode(I->getOpcode()) ||
333 isIndirectBranchOpcode(I->getOpcode()) ||
334 isJumpTableBranchOpcode(I->getOpcode()) ||
336 // Forget any previous condition branch information - it no longer applies.
340 // If we can modify the function, delete everything below this
341 // unconditional branch.
343 MachineBasicBlock::iterator DI = std::next(I);
344 while (DI != MBB.end()) {
345 MachineInstr *InstToDelete = DI;
347 InstToDelete->eraseFromParent();
355 if (I == MBB.begin())
361 // We made it past the terminators without bailing out - we must have
362 // analyzed this branch successfully.
367 unsigned ARMBaseInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
368 MachineBasicBlock::iterator I = MBB.end();
369 if (I == MBB.begin()) return 0;
371 while (I->isDebugValue()) {
372 if (I == MBB.begin())
376 if (!isUncondBranchOpcode(I->getOpcode()) &&
377 !isCondBranchOpcode(I->getOpcode()))
380 // Remove the branch.
381 I->eraseFromParent();
385 if (I == MBB.begin()) return 1;
387 if (!isCondBranchOpcode(I->getOpcode()))
390 // Remove the branch.
391 I->eraseFromParent();
396 ARMBaseInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
397 MachineBasicBlock *FBB,
398 const SmallVectorImpl<MachineOperand> &Cond,
400 ARMFunctionInfo *AFI = MBB.getParent()->getInfo<ARMFunctionInfo>();
401 int BOpc = !AFI->isThumbFunction()
402 ? ARM::B : (AFI->isThumb2Function() ? ARM::t2B : ARM::tB);
403 int BccOpc = !AFI->isThumbFunction()
404 ? ARM::Bcc : (AFI->isThumb2Function() ? ARM::t2Bcc : ARM::tBcc);
405 bool isThumb = AFI->isThumbFunction() || AFI->isThumb2Function();
407 // Shouldn't be a fall through.
408 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
409 assert((Cond.size() == 2 || Cond.size() == 0) &&
410 "ARM branch conditions have two components!");
413 if (Cond.empty()) { // Unconditional branch?
415 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB).addImm(ARMCC::AL).addReg(0);
417 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB);
419 BuildMI(&MBB, DL, get(BccOpc)).addMBB(TBB)
420 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg());
424 // Two-way conditional branch.
425 BuildMI(&MBB, DL, get(BccOpc)).addMBB(TBB)
426 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg());
428 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB).addImm(ARMCC::AL).addReg(0);
430 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB);
434 bool ARMBaseInstrInfo::
435 ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
436 ARMCC::CondCodes CC = (ARMCC::CondCodes)(int)Cond[0].getImm();
437 Cond[0].setImm(ARMCC::getOppositeCondition(CC));
441 bool ARMBaseInstrInfo::isPredicated(const MachineInstr *MI) const {
442 if (MI->isBundle()) {
443 MachineBasicBlock::const_instr_iterator I = MI;
444 MachineBasicBlock::const_instr_iterator E = MI->getParent()->instr_end();
445 while (++I != E && I->isInsideBundle()) {
446 int PIdx = I->findFirstPredOperandIdx();
447 if (PIdx != -1 && I->getOperand(PIdx).getImm() != ARMCC::AL)
453 int PIdx = MI->findFirstPredOperandIdx();
454 return PIdx != -1 && MI->getOperand(PIdx).getImm() != ARMCC::AL;
457 bool ARMBaseInstrInfo::
458 PredicateInstruction(MachineInstr *MI,
459 const SmallVectorImpl<MachineOperand> &Pred) const {
460 unsigned Opc = MI->getOpcode();
461 if (isUncondBranchOpcode(Opc)) {
462 MI->setDesc(get(getMatchingCondBranchOpcode(Opc)));
463 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
464 .addImm(Pred[0].getImm())
465 .addReg(Pred[1].getReg());
469 int PIdx = MI->findFirstPredOperandIdx();
471 MachineOperand &PMO = MI->getOperand(PIdx);
472 PMO.setImm(Pred[0].getImm());
473 MI->getOperand(PIdx+1).setReg(Pred[1].getReg());
479 bool ARMBaseInstrInfo::
480 SubsumesPredicate(const SmallVectorImpl<MachineOperand> &Pred1,
481 const SmallVectorImpl<MachineOperand> &Pred2) const {
482 if (Pred1.size() > 2 || Pred2.size() > 2)
485 ARMCC::CondCodes CC1 = (ARMCC::CondCodes)Pred1[0].getImm();
486 ARMCC::CondCodes CC2 = (ARMCC::CondCodes)Pred2[0].getImm();
496 return CC2 == ARMCC::HI;
498 return CC2 == ARMCC::LO || CC2 == ARMCC::EQ;
500 return CC2 == ARMCC::GT;
502 return CC2 == ARMCC::LT;
506 bool ARMBaseInstrInfo::DefinesPredicate(MachineInstr *MI,
507 std::vector<MachineOperand> &Pred) const {
509 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
510 const MachineOperand &MO = MI->getOperand(i);
511 if ((MO.isRegMask() && MO.clobbersPhysReg(ARM::CPSR)) ||
512 (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR)) {
521 /// isPredicable - Return true if the specified instruction can be predicated.
522 /// By default, this returns true for every instruction with a
523 /// PredicateOperand.
524 bool ARMBaseInstrInfo::isPredicable(MachineInstr *MI) const {
525 if (!MI->isPredicable())
528 ARMFunctionInfo *AFI =
529 MI->getParent()->getParent()->getInfo<ARMFunctionInfo>();
531 if (AFI->isThumb2Function()) {
532 if (getSubtarget().restrictIT())
533 return isV8EligibleForIT(MI);
534 } else { // non-Thumb
535 if ((MI->getDesc().TSFlags & ARMII::DomainMask) == ARMII::DomainNEON)
543 template <> bool IsCPSRDead<MachineInstr>(MachineInstr *MI) {
544 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
545 const MachineOperand &MO = MI->getOperand(i);
546 if (!MO.isReg() || MO.isUndef() || MO.isUse())
548 if (MO.getReg() != ARM::CPSR)
553 // all definitions of CPSR are dead
558 /// FIXME: Works around a gcc miscompilation with -fstrict-aliasing.
559 LLVM_ATTRIBUTE_NOINLINE
560 static unsigned getNumJTEntries(const std::vector<MachineJumpTableEntry> &JT,
562 static unsigned getNumJTEntries(const std::vector<MachineJumpTableEntry> &JT,
564 assert(JTI < JT.size());
565 return JT[JTI].MBBs.size();
568 /// GetInstSize - Return the size of the specified MachineInstr.
570 unsigned ARMBaseInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
571 const MachineBasicBlock &MBB = *MI->getParent();
572 const MachineFunction *MF = MBB.getParent();
573 const MCAsmInfo *MAI = MF->getTarget().getMCAsmInfo();
575 const MCInstrDesc &MCID = MI->getDesc();
577 return MCID.getSize();
579 // If this machine instr is an inline asm, measure it.
580 if (MI->getOpcode() == ARM::INLINEASM)
581 return getInlineAsmLength(MI->getOperand(0).getSymbolName(), *MAI);
582 unsigned Opc = MI->getOpcode();
585 // pseudo-instruction sizes are zero.
587 case TargetOpcode::BUNDLE:
588 return getInstBundleLength(MI);
589 case ARM::MOVi16_ga_pcrel:
590 case ARM::MOVTi16_ga_pcrel:
591 case ARM::t2MOVi16_ga_pcrel:
592 case ARM::t2MOVTi16_ga_pcrel:
595 case ARM::t2MOVi32imm:
597 case ARM::CONSTPOOL_ENTRY:
598 // If this machine instr is a constant pool entry, its size is recorded as
600 return MI->getOperand(2).getImm();
601 case ARM::Int_eh_sjlj_longjmp:
603 case ARM::tInt_eh_sjlj_longjmp:
605 case ARM::Int_eh_sjlj_setjmp:
606 case ARM::Int_eh_sjlj_setjmp_nofp:
608 case ARM::tInt_eh_sjlj_setjmp:
609 case ARM::t2Int_eh_sjlj_setjmp:
610 case ARM::t2Int_eh_sjlj_setjmp_nofp:
618 case ARM::t2TBH_JT: {
619 // These are jumptable branches, i.e. a branch followed by an inlined
620 // jumptable. The size is 4 + 4 * number of entries. For TBB, each
621 // entry is one byte; TBH two byte each.
622 unsigned EntrySize = (Opc == ARM::t2TBB_JT)
623 ? 1 : ((Opc == ARM::t2TBH_JT) ? 2 : 4);
624 unsigned NumOps = MCID.getNumOperands();
625 MachineOperand JTOP =
626 MI->getOperand(NumOps - (MI->isPredicable() ? 3 : 2));
627 unsigned JTI = JTOP.getIndex();
628 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
629 assert(MJTI != nullptr);
630 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
631 assert(JTI < JT.size());
632 // Thumb instructions are 2 byte aligned, but JT entries are 4 byte
633 // 4 aligned. The assembler / linker may add 2 byte padding just before
634 // the JT entries. The size does not include this padding; the
635 // constant islands pass does separate bookkeeping for it.
636 // FIXME: If we know the size of the function is less than (1 << 16) *2
637 // bytes, we can use 16-bit entries instead. Then there won't be an
639 unsigned InstSize = (Opc == ARM::tBR_JTr || Opc == ARM::t2BR_JT) ? 2 : 4;
640 unsigned NumEntries = getNumJTEntries(JT, JTI);
641 if (Opc == ARM::t2TBB_JT && (NumEntries & 1))
642 // Make sure the instruction that follows TBB is 2-byte aligned.
643 // FIXME: Constant island pass should insert an "ALIGN" instruction
646 return NumEntries * EntrySize + InstSize;
651 unsigned ARMBaseInstrInfo::getInstBundleLength(const MachineInstr *MI) const {
653 MachineBasicBlock::const_instr_iterator I = MI;
654 MachineBasicBlock::const_instr_iterator E = MI->getParent()->instr_end();
655 while (++I != E && I->isInsideBundle()) {
656 assert(!I->isBundle() && "No nested bundle!");
657 Size += GetInstSizeInBytes(&*I);
662 void ARMBaseInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
663 MachineBasicBlock::iterator I, DebugLoc DL,
664 unsigned DestReg, unsigned SrcReg,
665 bool KillSrc) const {
666 bool GPRDest = ARM::GPRRegClass.contains(DestReg);
667 bool GPRSrc = ARM::GPRRegClass.contains(SrcReg);
669 if (GPRDest && GPRSrc) {
670 AddDefaultCC(AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::MOVr), DestReg)
671 .addReg(SrcReg, getKillRegState(KillSrc))));
675 bool SPRDest = ARM::SPRRegClass.contains(DestReg);
676 bool SPRSrc = ARM::SPRRegClass.contains(SrcReg);
679 if (SPRDest && SPRSrc)
681 else if (GPRDest && SPRSrc)
683 else if (SPRDest && GPRSrc)
685 else if (ARM::DPRRegClass.contains(DestReg, SrcReg))
687 else if (ARM::QPRRegClass.contains(DestReg, SrcReg))
691 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(Opc), DestReg);
692 MIB.addReg(SrcReg, getKillRegState(KillSrc));
693 if (Opc == ARM::VORRq)
694 MIB.addReg(SrcReg, getKillRegState(KillSrc));
699 // Handle register classes that require multiple instructions.
700 unsigned BeginIdx = 0;
701 unsigned SubRegs = 0;
704 // Use VORRq when possible.
705 if (ARM::QQPRRegClass.contains(DestReg, SrcReg)) {
707 BeginIdx = ARM::qsub_0;
709 } else if (ARM::QQQQPRRegClass.contains(DestReg, SrcReg)) {
711 BeginIdx = ARM::qsub_0;
713 // Fall back to VMOVD.
714 } else if (ARM::DPairRegClass.contains(DestReg, SrcReg)) {
716 BeginIdx = ARM::dsub_0;
718 } else if (ARM::DTripleRegClass.contains(DestReg, SrcReg)) {
720 BeginIdx = ARM::dsub_0;
722 } else if (ARM::DQuadRegClass.contains(DestReg, SrcReg)) {
724 BeginIdx = ARM::dsub_0;
726 } else if (ARM::GPRPairRegClass.contains(DestReg, SrcReg)) {
727 Opc = Subtarget.isThumb2() ? ARM::tMOVr : ARM::MOVr;
728 BeginIdx = ARM::gsub_0;
730 } else if (ARM::DPairSpcRegClass.contains(DestReg, SrcReg)) {
732 BeginIdx = ARM::dsub_0;
735 } else if (ARM::DTripleSpcRegClass.contains(DestReg, SrcReg)) {
737 BeginIdx = ARM::dsub_0;
740 } else if (ARM::DQuadSpcRegClass.contains(DestReg, SrcReg)) {
742 BeginIdx = ARM::dsub_0;
747 assert(Opc && "Impossible reg-to-reg copy");
749 const TargetRegisterInfo *TRI = &getRegisterInfo();
750 MachineInstrBuilder Mov;
752 // Copy register tuples backward when the first Dest reg overlaps with SrcReg.
753 if (TRI->regsOverlap(SrcReg, TRI->getSubReg(DestReg, BeginIdx))) {
754 BeginIdx = BeginIdx + ((SubRegs - 1) * Spacing);
758 SmallSet<unsigned, 4> DstRegs;
760 for (unsigned i = 0; i != SubRegs; ++i) {
761 unsigned Dst = TRI->getSubReg(DestReg, BeginIdx + i * Spacing);
762 unsigned Src = TRI->getSubReg(SrcReg, BeginIdx + i * Spacing);
763 assert(Dst && Src && "Bad sub-register");
765 assert(!DstRegs.count(Src) && "destructive vector copy");
768 Mov = BuildMI(MBB, I, I->getDebugLoc(), get(Opc), Dst).addReg(Src);
769 // VORR takes two source operands.
770 if (Opc == ARM::VORRq)
772 Mov = AddDefaultPred(Mov);
774 if (Opc == ARM::MOVr)
775 Mov = AddDefaultCC(Mov);
777 // Add implicit super-register defs and kills to the last instruction.
778 Mov->addRegisterDefined(DestReg, TRI);
780 Mov->addRegisterKilled(SrcReg, TRI);
783 const MachineInstrBuilder &
784 ARMBaseInstrInfo::AddDReg(MachineInstrBuilder &MIB, unsigned Reg,
785 unsigned SubIdx, unsigned State,
786 const TargetRegisterInfo *TRI) const {
788 return MIB.addReg(Reg, State);
790 if (TargetRegisterInfo::isPhysicalRegister(Reg))
791 return MIB.addReg(TRI->getSubReg(Reg, SubIdx), State);
792 return MIB.addReg(Reg, State, SubIdx);
795 void ARMBaseInstrInfo::
796 storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
797 unsigned SrcReg, bool isKill, int FI,
798 const TargetRegisterClass *RC,
799 const TargetRegisterInfo *TRI) const {
801 if (I != MBB.end()) DL = I->getDebugLoc();
802 MachineFunction &MF = *MBB.getParent();
803 MachineFrameInfo &MFI = *MF.getFrameInfo();
804 unsigned Align = MFI.getObjectAlignment(FI);
806 MachineMemOperand *MMO =
807 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
808 MachineMemOperand::MOStore,
809 MFI.getObjectSize(FI),
812 switch (RC->getSize()) {
814 if (ARM::GPRRegClass.hasSubClassEq(RC)) {
815 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::STRi12))
816 .addReg(SrcReg, getKillRegState(isKill))
817 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
818 } else if (ARM::SPRRegClass.hasSubClassEq(RC)) {
819 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTRS))
820 .addReg(SrcReg, getKillRegState(isKill))
821 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
823 llvm_unreachable("Unknown reg class!");
826 if (ARM::DPRRegClass.hasSubClassEq(RC)) {
827 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTRD))
828 .addReg(SrcReg, getKillRegState(isKill))
829 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
830 } else if (ARM::GPRPairRegClass.hasSubClassEq(RC)) {
831 if (Subtarget.hasV5TEOps()) {
832 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::STRD));
833 AddDReg(MIB, SrcReg, ARM::gsub_0, getKillRegState(isKill), TRI);
834 AddDReg(MIB, SrcReg, ARM::gsub_1, 0, TRI);
835 MIB.addFrameIndex(FI).addReg(0).addImm(0).addMemOperand(MMO);
839 // Fallback to STM instruction, which has existed since the dawn of
841 MachineInstrBuilder MIB =
842 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::STMIA))
843 .addFrameIndex(FI).addMemOperand(MMO));
844 AddDReg(MIB, SrcReg, ARM::gsub_0, getKillRegState(isKill), TRI);
845 AddDReg(MIB, SrcReg, ARM::gsub_1, 0, TRI);
848 llvm_unreachable("Unknown reg class!");
851 if (ARM::DPairRegClass.hasSubClassEq(RC)) {
852 // Use aligned spills if the stack can be realigned.
853 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
854 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VST1q64))
855 .addFrameIndex(FI).addImm(16)
856 .addReg(SrcReg, getKillRegState(isKill))
857 .addMemOperand(MMO));
859 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMQIA))
860 .addReg(SrcReg, getKillRegState(isKill))
862 .addMemOperand(MMO));
865 llvm_unreachable("Unknown reg class!");
868 if (ARM::DTripleRegClass.hasSubClassEq(RC)) {
869 // Use aligned spills if the stack can be realigned.
870 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
871 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VST1d64TPseudo))
872 .addFrameIndex(FI).addImm(16)
873 .addReg(SrcReg, getKillRegState(isKill))
874 .addMemOperand(MMO));
876 MachineInstrBuilder MIB =
877 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMDIA))
880 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
881 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
882 AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
885 llvm_unreachable("Unknown reg class!");
888 if (ARM::QQPRRegClass.hasSubClassEq(RC) || ARM::DQuadRegClass.hasSubClassEq(RC)) {
889 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
890 // FIXME: It's possible to only store part of the QQ register if the
891 // spilled def has a sub-register index.
892 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VST1d64QPseudo))
893 .addFrameIndex(FI).addImm(16)
894 .addReg(SrcReg, getKillRegState(isKill))
895 .addMemOperand(MMO));
897 MachineInstrBuilder MIB =
898 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMDIA))
901 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
902 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
903 MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
904 AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI);
907 llvm_unreachable("Unknown reg class!");
910 if (ARM::QQQQPRRegClass.hasSubClassEq(RC)) {
911 MachineInstrBuilder MIB =
912 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMDIA))
915 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
916 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
917 MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
918 MIB = AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI);
919 MIB = AddDReg(MIB, SrcReg, ARM::dsub_4, 0, TRI);
920 MIB = AddDReg(MIB, SrcReg, ARM::dsub_5, 0, TRI);
921 MIB = AddDReg(MIB, SrcReg, ARM::dsub_6, 0, TRI);
922 AddDReg(MIB, SrcReg, ARM::dsub_7, 0, TRI);
924 llvm_unreachable("Unknown reg class!");
927 llvm_unreachable("Unknown reg class!");
932 ARMBaseInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
933 int &FrameIndex) const {
934 switch (MI->getOpcode()) {
937 case ARM::t2STRs: // FIXME: don't use t2STRs to access frame.
938 if (MI->getOperand(1).isFI() &&
939 MI->getOperand(2).isReg() &&
940 MI->getOperand(3).isImm() &&
941 MI->getOperand(2).getReg() == 0 &&
942 MI->getOperand(3).getImm() == 0) {
943 FrameIndex = MI->getOperand(1).getIndex();
944 return MI->getOperand(0).getReg();
952 if (MI->getOperand(1).isFI() &&
953 MI->getOperand(2).isImm() &&
954 MI->getOperand(2).getImm() == 0) {
955 FrameIndex = MI->getOperand(1).getIndex();
956 return MI->getOperand(0).getReg();
960 case ARM::VST1d64TPseudo:
961 case ARM::VST1d64QPseudo:
962 if (MI->getOperand(0).isFI() &&
963 MI->getOperand(2).getSubReg() == 0) {
964 FrameIndex = MI->getOperand(0).getIndex();
965 return MI->getOperand(2).getReg();
969 if (MI->getOperand(1).isFI() &&
970 MI->getOperand(0).getSubReg() == 0) {
971 FrameIndex = MI->getOperand(1).getIndex();
972 return MI->getOperand(0).getReg();
980 unsigned ARMBaseInstrInfo::isStoreToStackSlotPostFE(const MachineInstr *MI,
981 int &FrameIndex) const {
982 const MachineMemOperand *Dummy;
983 return MI->mayStore() && hasStoreToStackSlot(MI, Dummy, FrameIndex);
986 void ARMBaseInstrInfo::
987 loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
988 unsigned DestReg, int FI,
989 const TargetRegisterClass *RC,
990 const TargetRegisterInfo *TRI) const {
992 if (I != MBB.end()) DL = I->getDebugLoc();
993 MachineFunction &MF = *MBB.getParent();
994 MachineFrameInfo &MFI = *MF.getFrameInfo();
995 unsigned Align = MFI.getObjectAlignment(FI);
996 MachineMemOperand *MMO =
997 MF.getMachineMemOperand(
998 MachinePointerInfo::getFixedStack(FI),
999 MachineMemOperand::MOLoad,
1000 MFI.getObjectSize(FI),
1003 switch (RC->getSize()) {
1005 if (ARM::GPRRegClass.hasSubClassEq(RC)) {
1006 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::LDRi12), DestReg)
1007 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
1009 } else if (ARM::SPRRegClass.hasSubClassEq(RC)) {
1010 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDRS), DestReg)
1011 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
1013 llvm_unreachable("Unknown reg class!");
1016 if (ARM::DPRRegClass.hasSubClassEq(RC)) {
1017 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDRD), DestReg)
1018 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
1019 } else if (ARM::GPRPairRegClass.hasSubClassEq(RC)) {
1020 MachineInstrBuilder MIB;
1022 if (Subtarget.hasV5TEOps()) {
1023 MIB = BuildMI(MBB, I, DL, get(ARM::LDRD));
1024 AddDReg(MIB, DestReg, ARM::gsub_0, RegState::DefineNoRead, TRI);
1025 AddDReg(MIB, DestReg, ARM::gsub_1, RegState::DefineNoRead, TRI);
1026 MIB.addFrameIndex(FI).addReg(0).addImm(0).addMemOperand(MMO);
1028 AddDefaultPred(MIB);
1030 // Fallback to LDM instruction, which has existed since the dawn of
1032 MIB = AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::LDMIA))
1033 .addFrameIndex(FI).addMemOperand(MMO));
1034 MIB = AddDReg(MIB, DestReg, ARM::gsub_0, RegState::DefineNoRead, TRI);
1035 MIB = AddDReg(MIB, DestReg, ARM::gsub_1, RegState::DefineNoRead, TRI);
1038 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1039 MIB.addReg(DestReg, RegState::ImplicitDefine);
1041 llvm_unreachable("Unknown reg class!");
1044 if (ARM::DPairRegClass.hasSubClassEq(RC)) {
1045 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
1046 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLD1q64), DestReg)
1047 .addFrameIndex(FI).addImm(16)
1048 .addMemOperand(MMO));
1050 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMQIA), DestReg)
1052 .addMemOperand(MMO));
1055 llvm_unreachable("Unknown reg class!");
1058 if (ARM::DTripleRegClass.hasSubClassEq(RC)) {
1059 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
1060 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLD1d64TPseudo), DestReg)
1061 .addFrameIndex(FI).addImm(16)
1062 .addMemOperand(MMO));
1064 MachineInstrBuilder MIB =
1065 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
1067 .addMemOperand(MMO));
1068 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::DefineNoRead, TRI);
1069 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::DefineNoRead, TRI);
1070 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::DefineNoRead, TRI);
1071 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1072 MIB.addReg(DestReg, RegState::ImplicitDefine);
1075 llvm_unreachable("Unknown reg class!");
1078 if (ARM::QQPRRegClass.hasSubClassEq(RC) || ARM::DQuadRegClass.hasSubClassEq(RC)) {
1079 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
1080 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLD1d64QPseudo), DestReg)
1081 .addFrameIndex(FI).addImm(16)
1082 .addMemOperand(MMO));
1084 MachineInstrBuilder MIB =
1085 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
1087 .addMemOperand(MMO);
1088 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::DefineNoRead, TRI);
1089 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::DefineNoRead, TRI);
1090 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::DefineNoRead, TRI);
1091 MIB = AddDReg(MIB, DestReg, ARM::dsub_3, RegState::DefineNoRead, TRI);
1092 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1093 MIB.addReg(DestReg, RegState::ImplicitDefine);
1096 llvm_unreachable("Unknown reg class!");
1099 if (ARM::QQQQPRRegClass.hasSubClassEq(RC)) {
1100 MachineInstrBuilder MIB =
1101 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
1103 .addMemOperand(MMO);
1104 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::DefineNoRead, TRI);
1105 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::DefineNoRead, TRI);
1106 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::DefineNoRead, TRI);
1107 MIB = AddDReg(MIB, DestReg, ARM::dsub_3, RegState::DefineNoRead, TRI);
1108 MIB = AddDReg(MIB, DestReg, ARM::dsub_4, RegState::DefineNoRead, TRI);
1109 MIB = AddDReg(MIB, DestReg, ARM::dsub_5, RegState::DefineNoRead, TRI);
1110 MIB = AddDReg(MIB, DestReg, ARM::dsub_6, RegState::DefineNoRead, TRI);
1111 MIB = AddDReg(MIB, DestReg, ARM::dsub_7, RegState::DefineNoRead, TRI);
1112 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1113 MIB.addReg(DestReg, RegState::ImplicitDefine);
1115 llvm_unreachable("Unknown reg class!");
1118 llvm_unreachable("Unknown regclass!");
1123 ARMBaseInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
1124 int &FrameIndex) const {
1125 switch (MI->getOpcode()) {
1128 case ARM::t2LDRs: // FIXME: don't use t2LDRs to access frame.
1129 if (MI->getOperand(1).isFI() &&
1130 MI->getOperand(2).isReg() &&
1131 MI->getOperand(3).isImm() &&
1132 MI->getOperand(2).getReg() == 0 &&
1133 MI->getOperand(3).getImm() == 0) {
1134 FrameIndex = MI->getOperand(1).getIndex();
1135 return MI->getOperand(0).getReg();
1143 if (MI->getOperand(1).isFI() &&
1144 MI->getOperand(2).isImm() &&
1145 MI->getOperand(2).getImm() == 0) {
1146 FrameIndex = MI->getOperand(1).getIndex();
1147 return MI->getOperand(0).getReg();
1151 case ARM::VLD1d64TPseudo:
1152 case ARM::VLD1d64QPseudo:
1153 if (MI->getOperand(1).isFI() &&
1154 MI->getOperand(0).getSubReg() == 0) {
1155 FrameIndex = MI->getOperand(1).getIndex();
1156 return MI->getOperand(0).getReg();
1160 if (MI->getOperand(1).isFI() &&
1161 MI->getOperand(0).getSubReg() == 0) {
1162 FrameIndex = MI->getOperand(1).getIndex();
1163 return MI->getOperand(0).getReg();
1171 unsigned ARMBaseInstrInfo::isLoadFromStackSlotPostFE(const MachineInstr *MI,
1172 int &FrameIndex) const {
1173 const MachineMemOperand *Dummy;
1174 return MI->mayLoad() && hasLoadFromStackSlot(MI, Dummy, FrameIndex);
1177 bool ARMBaseInstrInfo::expandPostRAPseudo(MachineBasicBlock::iterator MI) const{
1178 // This hook gets to expand COPY instructions before they become
1179 // copyPhysReg() calls. Look for VMOVS instructions that can legally be
1180 // widened to VMOVD. We prefer the VMOVD when possible because it may be
1181 // changed into a VORR that can go down the NEON pipeline.
1182 if (!WidenVMOVS || !MI->isCopy() || Subtarget.isCortexA15())
1185 // Look for a copy between even S-registers. That is where we keep floats
1186 // when using NEON v2f32 instructions for f32 arithmetic.
1187 unsigned DstRegS = MI->getOperand(0).getReg();
1188 unsigned SrcRegS = MI->getOperand(1).getReg();
1189 if (!ARM::SPRRegClass.contains(DstRegS, SrcRegS))
1192 const TargetRegisterInfo *TRI = &getRegisterInfo();
1193 unsigned DstRegD = TRI->getMatchingSuperReg(DstRegS, ARM::ssub_0,
1195 unsigned SrcRegD = TRI->getMatchingSuperReg(SrcRegS, ARM::ssub_0,
1197 if (!DstRegD || !SrcRegD)
1200 // We want to widen this into a DstRegD = VMOVD SrcRegD copy. This is only
1201 // legal if the COPY already defines the full DstRegD, and it isn't a
1202 // sub-register insertion.
1203 if (!MI->definesRegister(DstRegD, TRI) || MI->readsRegister(DstRegD, TRI))
1206 // A dead copy shouldn't show up here, but reject it just in case.
1207 if (MI->getOperand(0).isDead())
1210 // All clear, widen the COPY.
1211 DEBUG(dbgs() << "widening: " << *MI);
1212 MachineInstrBuilder MIB(*MI->getParent()->getParent(), MI);
1214 // Get rid of the old <imp-def> of DstRegD. Leave it if it defines a Q-reg
1215 // or some other super-register.
1216 int ImpDefIdx = MI->findRegisterDefOperandIdx(DstRegD);
1217 if (ImpDefIdx != -1)
1218 MI->RemoveOperand(ImpDefIdx);
1220 // Change the opcode and operands.
1221 MI->setDesc(get(ARM::VMOVD));
1222 MI->getOperand(0).setReg(DstRegD);
1223 MI->getOperand(1).setReg(SrcRegD);
1224 AddDefaultPred(MIB);
1226 // We are now reading SrcRegD instead of SrcRegS. This may upset the
1227 // register scavenger and machine verifier, so we need to indicate that we
1228 // are reading an undefined value from SrcRegD, but a proper value from
1230 MI->getOperand(1).setIsUndef();
1231 MIB.addReg(SrcRegS, RegState::Implicit);
1233 // SrcRegD may actually contain an unrelated value in the ssub_1
1234 // sub-register. Don't kill it. Only kill the ssub_0 sub-register.
1235 if (MI->getOperand(1).isKill()) {
1236 MI->getOperand(1).setIsKill(false);
1237 MI->addRegisterKilled(SrcRegS, TRI, true);
1240 DEBUG(dbgs() << "replaced by: " << *MI);
1244 /// Create a copy of a const pool value. Update CPI to the new index and return
1246 static unsigned duplicateCPV(MachineFunction &MF, unsigned &CPI) {
1247 MachineConstantPool *MCP = MF.getConstantPool();
1248 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1250 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPI];
1251 assert(MCPE.isMachineConstantPoolEntry() &&
1252 "Expecting a machine constantpool entry!");
1253 ARMConstantPoolValue *ACPV =
1254 static_cast<ARMConstantPoolValue*>(MCPE.Val.MachineCPVal);
1256 unsigned PCLabelId = AFI->createPICLabelUId();
1257 ARMConstantPoolValue *NewCPV = nullptr;
1259 // FIXME: The below assumes PIC relocation model and that the function
1260 // is Thumb mode (t1 or t2). PCAdjustment would be 8 for ARM mode PIC, and
1261 // zero for non-PIC in ARM or Thumb. The callers are all of thumb LDR
1262 // instructions, so that's probably OK, but is PIC always correct when
1264 if (ACPV->isGlobalValue())
1265 NewCPV = ARMConstantPoolConstant::
1266 Create(cast<ARMConstantPoolConstant>(ACPV)->getGV(), PCLabelId,
1268 else if (ACPV->isExtSymbol())
1269 NewCPV = ARMConstantPoolSymbol::
1270 Create(MF.getFunction()->getContext(),
1271 cast<ARMConstantPoolSymbol>(ACPV)->getSymbol(), PCLabelId, 4);
1272 else if (ACPV->isBlockAddress())
1273 NewCPV = ARMConstantPoolConstant::
1274 Create(cast<ARMConstantPoolConstant>(ACPV)->getBlockAddress(), PCLabelId,
1275 ARMCP::CPBlockAddress, 4);
1276 else if (ACPV->isLSDA())
1277 NewCPV = ARMConstantPoolConstant::Create(MF.getFunction(), PCLabelId,
1279 else if (ACPV->isMachineBasicBlock())
1280 NewCPV = ARMConstantPoolMBB::
1281 Create(MF.getFunction()->getContext(),
1282 cast<ARMConstantPoolMBB>(ACPV)->getMBB(), PCLabelId, 4);
1284 llvm_unreachable("Unexpected ARM constantpool value type!!");
1285 CPI = MCP->getConstantPoolIndex(NewCPV, MCPE.getAlignment());
1289 void ARMBaseInstrInfo::
1290 reMaterialize(MachineBasicBlock &MBB,
1291 MachineBasicBlock::iterator I,
1292 unsigned DestReg, unsigned SubIdx,
1293 const MachineInstr *Orig,
1294 const TargetRegisterInfo &TRI) const {
1295 unsigned Opcode = Orig->getOpcode();
1298 MachineInstr *MI = MBB.getParent()->CloneMachineInstr(Orig);
1299 MI->substituteRegister(Orig->getOperand(0).getReg(), DestReg, SubIdx, TRI);
1303 case ARM::tLDRpci_pic:
1304 case ARM::t2LDRpci_pic: {
1305 MachineFunction &MF = *MBB.getParent();
1306 unsigned CPI = Orig->getOperand(1).getIndex();
1307 unsigned PCLabelId = duplicateCPV(MF, CPI);
1308 MachineInstrBuilder MIB = BuildMI(MBB, I, Orig->getDebugLoc(), get(Opcode),
1310 .addConstantPoolIndex(CPI).addImm(PCLabelId);
1311 MIB->setMemRefs(Orig->memoperands_begin(), Orig->memoperands_end());
1318 ARMBaseInstrInfo::duplicate(MachineInstr *Orig, MachineFunction &MF) const {
1319 MachineInstr *MI = TargetInstrInfo::duplicate(Orig, MF);
1320 switch(Orig->getOpcode()) {
1321 case ARM::tLDRpci_pic:
1322 case ARM::t2LDRpci_pic: {
1323 unsigned CPI = Orig->getOperand(1).getIndex();
1324 unsigned PCLabelId = duplicateCPV(MF, CPI);
1325 Orig->getOperand(1).setIndex(CPI);
1326 Orig->getOperand(2).setImm(PCLabelId);
1333 bool ARMBaseInstrInfo::produceSameValue(const MachineInstr *MI0,
1334 const MachineInstr *MI1,
1335 const MachineRegisterInfo *MRI) const {
1336 int Opcode = MI0->getOpcode();
1337 if (Opcode == ARM::t2LDRpci ||
1338 Opcode == ARM::t2LDRpci_pic ||
1339 Opcode == ARM::tLDRpci ||
1340 Opcode == ARM::tLDRpci_pic ||
1341 Opcode == ARM::LDRLIT_ga_pcrel ||
1342 Opcode == ARM::LDRLIT_ga_pcrel_ldr ||
1343 Opcode == ARM::tLDRLIT_ga_pcrel ||
1344 Opcode == ARM::MOV_ga_pcrel ||
1345 Opcode == ARM::MOV_ga_pcrel_ldr ||
1346 Opcode == ARM::t2MOV_ga_pcrel) {
1347 if (MI1->getOpcode() != Opcode)
1349 if (MI0->getNumOperands() != MI1->getNumOperands())
1352 const MachineOperand &MO0 = MI0->getOperand(1);
1353 const MachineOperand &MO1 = MI1->getOperand(1);
1354 if (MO0.getOffset() != MO1.getOffset())
1357 if (Opcode == ARM::LDRLIT_ga_pcrel ||
1358 Opcode == ARM::LDRLIT_ga_pcrel_ldr ||
1359 Opcode == ARM::tLDRLIT_ga_pcrel ||
1360 Opcode == ARM::MOV_ga_pcrel ||
1361 Opcode == ARM::MOV_ga_pcrel_ldr ||
1362 Opcode == ARM::t2MOV_ga_pcrel)
1363 // Ignore the PC labels.
1364 return MO0.getGlobal() == MO1.getGlobal();
1366 const MachineFunction *MF = MI0->getParent()->getParent();
1367 const MachineConstantPool *MCP = MF->getConstantPool();
1368 int CPI0 = MO0.getIndex();
1369 int CPI1 = MO1.getIndex();
1370 const MachineConstantPoolEntry &MCPE0 = MCP->getConstants()[CPI0];
1371 const MachineConstantPoolEntry &MCPE1 = MCP->getConstants()[CPI1];
1372 bool isARMCP0 = MCPE0.isMachineConstantPoolEntry();
1373 bool isARMCP1 = MCPE1.isMachineConstantPoolEntry();
1374 if (isARMCP0 && isARMCP1) {
1375 ARMConstantPoolValue *ACPV0 =
1376 static_cast<ARMConstantPoolValue*>(MCPE0.Val.MachineCPVal);
1377 ARMConstantPoolValue *ACPV1 =
1378 static_cast<ARMConstantPoolValue*>(MCPE1.Val.MachineCPVal);
1379 return ACPV0->hasSameValue(ACPV1);
1380 } else if (!isARMCP0 && !isARMCP1) {
1381 return MCPE0.Val.ConstVal == MCPE1.Val.ConstVal;
1384 } else if (Opcode == ARM::PICLDR) {
1385 if (MI1->getOpcode() != Opcode)
1387 if (MI0->getNumOperands() != MI1->getNumOperands())
1390 unsigned Addr0 = MI0->getOperand(1).getReg();
1391 unsigned Addr1 = MI1->getOperand(1).getReg();
1392 if (Addr0 != Addr1) {
1394 !TargetRegisterInfo::isVirtualRegister(Addr0) ||
1395 !TargetRegisterInfo::isVirtualRegister(Addr1))
1398 // This assumes SSA form.
1399 MachineInstr *Def0 = MRI->getVRegDef(Addr0);
1400 MachineInstr *Def1 = MRI->getVRegDef(Addr1);
1401 // Check if the loaded value, e.g. a constantpool of a global address, are
1403 if (!produceSameValue(Def0, Def1, MRI))
1407 for (unsigned i = 3, e = MI0->getNumOperands(); i != e; ++i) {
1408 // %vreg12<def> = PICLDR %vreg11, 0, pred:14, pred:%noreg
1409 const MachineOperand &MO0 = MI0->getOperand(i);
1410 const MachineOperand &MO1 = MI1->getOperand(i);
1411 if (!MO0.isIdenticalTo(MO1))
1417 return MI0->isIdenticalTo(MI1, MachineInstr::IgnoreVRegDefs);
1420 /// areLoadsFromSameBasePtr - This is used by the pre-regalloc scheduler to
1421 /// determine if two loads are loading from the same base address. It should
1422 /// only return true if the base pointers are the same and the only differences
1423 /// between the two addresses is the offset. It also returns the offsets by
1426 /// FIXME: remove this in favor of the MachineInstr interface once pre-RA-sched
1427 /// is permanently disabled.
1428 bool ARMBaseInstrInfo::areLoadsFromSameBasePtr(SDNode *Load1, SDNode *Load2,
1430 int64_t &Offset2) const {
1431 // Don't worry about Thumb: just ARM and Thumb2.
1432 if (Subtarget.isThumb1Only()) return false;
1434 if (!Load1->isMachineOpcode() || !Load2->isMachineOpcode())
1437 switch (Load1->getMachineOpcode()) {
1451 case ARM::t2LDRSHi8:
1453 case ARM::t2LDRBi12:
1454 case ARM::t2LDRSHi12:
1458 switch (Load2->getMachineOpcode()) {
1471 case ARM::t2LDRSHi8:
1473 case ARM::t2LDRBi12:
1474 case ARM::t2LDRSHi12:
1478 // Check if base addresses and chain operands match.
1479 if (Load1->getOperand(0) != Load2->getOperand(0) ||
1480 Load1->getOperand(4) != Load2->getOperand(4))
1483 // Index should be Reg0.
1484 if (Load1->getOperand(3) != Load2->getOperand(3))
1487 // Determine the offsets.
1488 if (isa<ConstantSDNode>(Load1->getOperand(1)) &&
1489 isa<ConstantSDNode>(Load2->getOperand(1))) {
1490 Offset1 = cast<ConstantSDNode>(Load1->getOperand(1))->getSExtValue();
1491 Offset2 = cast<ConstantSDNode>(Load2->getOperand(1))->getSExtValue();
1498 /// shouldScheduleLoadsNear - This is a used by the pre-regalloc scheduler to
1499 /// determine (in conjunction with areLoadsFromSameBasePtr) if two loads should
1500 /// be scheduled togther. On some targets if two loads are loading from
1501 /// addresses in the same cache line, it's better if they are scheduled
1502 /// together. This function takes two integers that represent the load offsets
1503 /// from the common base address. It returns true if it decides it's desirable
1504 /// to schedule the two loads together. "NumLoads" is the number of loads that
1505 /// have already been scheduled after Load1.
1507 /// FIXME: remove this in favor of the MachineInstr interface once pre-RA-sched
1508 /// is permanently disabled.
1509 bool ARMBaseInstrInfo::shouldScheduleLoadsNear(SDNode *Load1, SDNode *Load2,
1510 int64_t Offset1, int64_t Offset2,
1511 unsigned NumLoads) const {
1512 // Don't worry about Thumb: just ARM and Thumb2.
1513 if (Subtarget.isThumb1Only()) return false;
1515 assert(Offset2 > Offset1);
1517 if ((Offset2 - Offset1) / 8 > 64)
1520 // Check if the machine opcodes are different. If they are different
1521 // then we consider them to not be of the same base address,
1522 // EXCEPT in the case of Thumb2 byte loads where one is LDRBi8 and the other LDRBi12.
1523 // In this case, they are considered to be the same because they are different
1524 // encoding forms of the same basic instruction.
1525 if ((Load1->getMachineOpcode() != Load2->getMachineOpcode()) &&
1526 !((Load1->getMachineOpcode() == ARM::t2LDRBi8 &&
1527 Load2->getMachineOpcode() == ARM::t2LDRBi12) ||
1528 (Load1->getMachineOpcode() == ARM::t2LDRBi12 &&
1529 Load2->getMachineOpcode() == ARM::t2LDRBi8)))
1530 return false; // FIXME: overly conservative?
1532 // Four loads in a row should be sufficient.
1539 bool ARMBaseInstrInfo::isSchedulingBoundary(const MachineInstr *MI,
1540 const MachineBasicBlock *MBB,
1541 const MachineFunction &MF) const {
1542 // Debug info is never a scheduling boundary. It's necessary to be explicit
1543 // due to the special treatment of IT instructions below, otherwise a
1544 // dbg_value followed by an IT will result in the IT instruction being
1545 // considered a scheduling hazard, which is wrong. It should be the actual
1546 // instruction preceding the dbg_value instruction(s), just like it is
1547 // when debug info is not present.
1548 if (MI->isDebugValue())
1551 // Terminators and labels can't be scheduled around.
1552 if (MI->isTerminator() || MI->isPosition())
1555 // Treat the start of the IT block as a scheduling boundary, but schedule
1556 // t2IT along with all instructions following it.
1557 // FIXME: This is a big hammer. But the alternative is to add all potential
1558 // true and anti dependencies to IT block instructions as implicit operands
1559 // to the t2IT instruction. The added compile time and complexity does not
1561 MachineBasicBlock::const_iterator I = MI;
1562 // Make sure to skip any dbg_value instructions
1563 while (++I != MBB->end() && I->isDebugValue())
1565 if (I != MBB->end() && I->getOpcode() == ARM::t2IT)
1568 // Don't attempt to schedule around any instruction that defines
1569 // a stack-oriented pointer, as it's unlikely to be profitable. This
1570 // saves compile time, because it doesn't require every single
1571 // stack slot reference to depend on the instruction that does the
1573 // Calls don't actually change the stack pointer, even if they have imp-defs.
1574 // No ARM calling conventions change the stack pointer. (X86 calling
1575 // conventions sometimes do).
1576 if (!MI->isCall() && MI->definesRegister(ARM::SP))
1582 bool ARMBaseInstrInfo::
1583 isProfitableToIfCvt(MachineBasicBlock &MBB,
1584 unsigned NumCycles, unsigned ExtraPredCycles,
1585 const BranchProbability &Probability) const {
1589 // Attempt to estimate the relative costs of predication versus branching.
1590 unsigned UnpredCost = Probability.getNumerator() * NumCycles;
1591 UnpredCost /= Probability.getDenominator();
1592 UnpredCost += 1; // The branch itself
1593 UnpredCost += Subtarget.getMispredictionPenalty() / 10;
1595 return (NumCycles + ExtraPredCycles) <= UnpredCost;
1598 bool ARMBaseInstrInfo::
1599 isProfitableToIfCvt(MachineBasicBlock &TMBB,
1600 unsigned TCycles, unsigned TExtra,
1601 MachineBasicBlock &FMBB,
1602 unsigned FCycles, unsigned FExtra,
1603 const BranchProbability &Probability) const {
1604 if (!TCycles || !FCycles)
1607 // Attempt to estimate the relative costs of predication versus branching.
1608 unsigned TUnpredCost = Probability.getNumerator() * TCycles;
1609 TUnpredCost /= Probability.getDenominator();
1611 uint32_t Comp = Probability.getDenominator() - Probability.getNumerator();
1612 unsigned FUnpredCost = Comp * FCycles;
1613 FUnpredCost /= Probability.getDenominator();
1615 unsigned UnpredCost = TUnpredCost + FUnpredCost;
1616 UnpredCost += 1; // The branch itself
1617 UnpredCost += Subtarget.getMispredictionPenalty() / 10;
1619 return (TCycles + FCycles + TExtra + FExtra) <= UnpredCost;
1623 ARMBaseInstrInfo::isProfitableToUnpredicate(MachineBasicBlock &TMBB,
1624 MachineBasicBlock &FMBB) const {
1625 // Reduce false anti-dependencies to let Swift's out-of-order execution
1626 // engine do its thing.
1627 return Subtarget.isSwift();
1630 /// getInstrPredicate - If instruction is predicated, returns its predicate
1631 /// condition, otherwise returns AL. It also returns the condition code
1632 /// register by reference.
1634 llvm::getInstrPredicate(const MachineInstr *MI, unsigned &PredReg) {
1635 int PIdx = MI->findFirstPredOperandIdx();
1641 PredReg = MI->getOperand(PIdx+1).getReg();
1642 return (ARMCC::CondCodes)MI->getOperand(PIdx).getImm();
1646 int llvm::getMatchingCondBranchOpcode(int Opc) {
1651 if (Opc == ARM::t2B)
1654 llvm_unreachable("Unknown unconditional branch opcode!");
1657 /// commuteInstruction - Handle commutable instructions.
1659 ARMBaseInstrInfo::commuteInstruction(MachineInstr *MI, bool NewMI) const {
1660 switch (MI->getOpcode()) {
1662 case ARM::t2MOVCCr: {
1663 // MOVCC can be commuted by inverting the condition.
1664 unsigned PredReg = 0;
1665 ARMCC::CondCodes CC = getInstrPredicate(MI, PredReg);
1666 // MOVCC AL can't be inverted. Shouldn't happen.
1667 if (CC == ARMCC::AL || PredReg != ARM::CPSR)
1669 MI = TargetInstrInfo::commuteInstruction(MI, NewMI);
1672 // After swapping the MOVCC operands, also invert the condition.
1673 MI->getOperand(MI->findFirstPredOperandIdx())
1674 .setImm(ARMCC::getOppositeCondition(CC));
1678 return TargetInstrInfo::commuteInstruction(MI, NewMI);
1681 /// Identify instructions that can be folded into a MOVCC instruction, and
1682 /// return the defining instruction.
1683 static MachineInstr *canFoldIntoMOVCC(unsigned Reg,
1684 const MachineRegisterInfo &MRI,
1685 const TargetInstrInfo *TII) {
1686 if (!TargetRegisterInfo::isVirtualRegister(Reg))
1688 if (!MRI.hasOneNonDBGUse(Reg))
1690 MachineInstr *MI = MRI.getVRegDef(Reg);
1693 // MI is folded into the MOVCC by predicating it.
1694 if (!MI->isPredicable())
1696 // Check if MI has any non-dead defs or physreg uses. This also detects
1697 // predicated instructions which will be reading CPSR.
1698 for (unsigned i = 1, e = MI->getNumOperands(); i != e; ++i) {
1699 const MachineOperand &MO = MI->getOperand(i);
1700 // Reject frame index operands, PEI can't handle the predicated pseudos.
1701 if (MO.isFI() || MO.isCPI() || MO.isJTI())
1705 // MI can't have any tied operands, that would conflict with predication.
1708 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
1710 if (MO.isDef() && !MO.isDead())
1713 bool DontMoveAcrossStores = true;
1714 if (!MI->isSafeToMove(TII, /* AliasAnalysis = */ nullptr,
1715 DontMoveAcrossStores))
1720 bool ARMBaseInstrInfo::analyzeSelect(const MachineInstr *MI,
1721 SmallVectorImpl<MachineOperand> &Cond,
1722 unsigned &TrueOp, unsigned &FalseOp,
1723 bool &Optimizable) const {
1724 assert((MI->getOpcode() == ARM::MOVCCr || MI->getOpcode() == ARM::t2MOVCCr) &&
1725 "Unknown select instruction");
1730 // 3: Condition code.
1734 Cond.push_back(MI->getOperand(3));
1735 Cond.push_back(MI->getOperand(4));
1736 // We can always fold a def.
1741 MachineInstr *ARMBaseInstrInfo::optimizeSelect(MachineInstr *MI,
1742 bool PreferFalse) const {
1743 assert((MI->getOpcode() == ARM::MOVCCr || MI->getOpcode() == ARM::t2MOVCCr) &&
1744 "Unknown select instruction");
1745 MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo();
1746 MachineInstr *DefMI = canFoldIntoMOVCC(MI->getOperand(2).getReg(), MRI, this);
1747 bool Invert = !DefMI;
1749 DefMI = canFoldIntoMOVCC(MI->getOperand(1).getReg(), MRI, this);
1753 // Find new register class to use.
1754 MachineOperand FalseReg = MI->getOperand(Invert ? 2 : 1);
1755 unsigned DestReg = MI->getOperand(0).getReg();
1756 const TargetRegisterClass *PreviousClass = MRI.getRegClass(FalseReg.getReg());
1757 if (!MRI.constrainRegClass(DestReg, PreviousClass))
1760 // Create a new predicated version of DefMI.
1761 // Rfalse is the first use.
1762 MachineInstrBuilder NewMI = BuildMI(*MI->getParent(), MI, MI->getDebugLoc(),
1763 DefMI->getDesc(), DestReg);
1765 // Copy all the DefMI operands, excluding its (null) predicate.
1766 const MCInstrDesc &DefDesc = DefMI->getDesc();
1767 for (unsigned i = 1, e = DefDesc.getNumOperands();
1768 i != e && !DefDesc.OpInfo[i].isPredicate(); ++i)
1769 NewMI.addOperand(DefMI->getOperand(i));
1771 unsigned CondCode = MI->getOperand(3).getImm();
1773 NewMI.addImm(ARMCC::getOppositeCondition(ARMCC::CondCodes(CondCode)));
1775 NewMI.addImm(CondCode);
1776 NewMI.addOperand(MI->getOperand(4));
1778 // DefMI is not the -S version that sets CPSR, so add an optional %noreg.
1779 if (NewMI->hasOptionalDef())
1780 AddDefaultCC(NewMI);
1782 // The output register value when the predicate is false is an implicit
1783 // register operand tied to the first def.
1784 // The tie makes the register allocator ensure the FalseReg is allocated the
1785 // same register as operand 0.
1786 FalseReg.setImplicit();
1787 NewMI.addOperand(FalseReg);
1788 NewMI->tieOperands(0, NewMI->getNumOperands() - 1);
1790 // The caller will erase MI, but not DefMI.
1791 DefMI->eraseFromParent();
1795 /// Map pseudo instructions that imply an 'S' bit onto real opcodes. Whether the
1796 /// instruction is encoded with an 'S' bit is determined by the optional CPSR
1799 /// This will go away once we can teach tblgen how to set the optional CPSR def
1801 struct AddSubFlagsOpcodePair {
1803 uint16_t MachineOpc;
1806 static const AddSubFlagsOpcodePair AddSubFlagsOpcodeMap[] = {
1807 {ARM::ADDSri, ARM::ADDri},
1808 {ARM::ADDSrr, ARM::ADDrr},
1809 {ARM::ADDSrsi, ARM::ADDrsi},
1810 {ARM::ADDSrsr, ARM::ADDrsr},
1812 {ARM::SUBSri, ARM::SUBri},
1813 {ARM::SUBSrr, ARM::SUBrr},
1814 {ARM::SUBSrsi, ARM::SUBrsi},
1815 {ARM::SUBSrsr, ARM::SUBrsr},
1817 {ARM::RSBSri, ARM::RSBri},
1818 {ARM::RSBSrsi, ARM::RSBrsi},
1819 {ARM::RSBSrsr, ARM::RSBrsr},
1821 {ARM::t2ADDSri, ARM::t2ADDri},
1822 {ARM::t2ADDSrr, ARM::t2ADDrr},
1823 {ARM::t2ADDSrs, ARM::t2ADDrs},
1825 {ARM::t2SUBSri, ARM::t2SUBri},
1826 {ARM::t2SUBSrr, ARM::t2SUBrr},
1827 {ARM::t2SUBSrs, ARM::t2SUBrs},
1829 {ARM::t2RSBSri, ARM::t2RSBri},
1830 {ARM::t2RSBSrs, ARM::t2RSBrs},
1833 unsigned llvm::convertAddSubFlagsOpcode(unsigned OldOpc) {
1834 for (unsigned i = 0, e = array_lengthof(AddSubFlagsOpcodeMap); i != e; ++i)
1835 if (OldOpc == AddSubFlagsOpcodeMap[i].PseudoOpc)
1836 return AddSubFlagsOpcodeMap[i].MachineOpc;
1840 void llvm::emitARMRegPlusImmediate(MachineBasicBlock &MBB,
1841 MachineBasicBlock::iterator &MBBI, DebugLoc dl,
1842 unsigned DestReg, unsigned BaseReg, int NumBytes,
1843 ARMCC::CondCodes Pred, unsigned PredReg,
1844 const ARMBaseInstrInfo &TII, unsigned MIFlags) {
1845 if (NumBytes == 0 && DestReg != BaseReg) {
1846 BuildMI(MBB, MBBI, dl, TII.get(ARM::MOVr), DestReg)
1847 .addReg(BaseReg, RegState::Kill)
1848 .addImm((unsigned)Pred).addReg(PredReg).addReg(0)
1849 .setMIFlags(MIFlags);
1853 bool isSub = NumBytes < 0;
1854 if (isSub) NumBytes = -NumBytes;
1857 unsigned RotAmt = ARM_AM::getSOImmValRotate(NumBytes);
1858 unsigned ThisVal = NumBytes & ARM_AM::rotr32(0xFF, RotAmt);
1859 assert(ThisVal && "Didn't extract field correctly");
1861 // We will handle these bits from offset, clear them.
1862 NumBytes &= ~ThisVal;
1864 assert(ARM_AM::getSOImmVal(ThisVal) != -1 && "Bit extraction didn't work?");
1866 // Build the new ADD / SUB.
1867 unsigned Opc = isSub ? ARM::SUBri : ARM::ADDri;
1868 BuildMI(MBB, MBBI, dl, TII.get(Opc), DestReg)
1869 .addReg(BaseReg, RegState::Kill).addImm(ThisVal)
1870 .addImm((unsigned)Pred).addReg(PredReg).addReg(0)
1871 .setMIFlags(MIFlags);
1876 static bool isAnySubRegLive(unsigned Reg, const TargetRegisterInfo *TRI,
1878 for (MCSubRegIterator Subreg(Reg, TRI, /* IncludeSelf */ true);
1879 Subreg.isValid(); ++Subreg)
1880 if (MI->getParent()->computeRegisterLiveness(TRI, *Subreg, MI) !=
1881 MachineBasicBlock::LQR_Dead)
1885 bool llvm::tryFoldSPUpdateIntoPushPop(const ARMSubtarget &Subtarget,
1886 MachineFunction &MF, MachineInstr *MI,
1887 unsigned NumBytes) {
1888 // This optimisation potentially adds lots of load and store
1889 // micro-operations, it's only really a great benefit to code-size.
1890 if (!Subtarget.isMinSize())
1893 // If only one register is pushed/popped, LLVM can use an LDR/STR
1894 // instead. We can't modify those so make sure we're dealing with an
1895 // instruction we understand.
1896 bool IsPop = isPopOpcode(MI->getOpcode());
1897 bool IsPush = isPushOpcode(MI->getOpcode());
1898 if (!IsPush && !IsPop)
1901 bool IsVFPPushPop = MI->getOpcode() == ARM::VSTMDDB_UPD ||
1902 MI->getOpcode() == ARM::VLDMDIA_UPD;
1903 bool IsT1PushPop = MI->getOpcode() == ARM::tPUSH ||
1904 MI->getOpcode() == ARM::tPOP ||
1905 MI->getOpcode() == ARM::tPOP_RET;
1907 assert((IsT1PushPop || (MI->getOperand(0).getReg() == ARM::SP &&
1908 MI->getOperand(1).getReg() == ARM::SP)) &&
1909 "trying to fold sp update into non-sp-updating push/pop");
1911 // The VFP push & pop act on D-registers, so we can only fold an adjustment
1912 // by a multiple of 8 bytes in correctly. Similarly rN is 4-bytes. Don't try
1913 // if this is violated.
1914 if (NumBytes % (IsVFPPushPop ? 8 : 4) != 0)
1917 // ARM and Thumb2 push/pop insts have explicit "sp, sp" operands (+
1918 // pred) so the list starts at 4. Thumb1 starts after the predicate.
1919 int RegListIdx = IsT1PushPop ? 2 : 4;
1921 // Calculate the space we'll need in terms of registers.
1922 unsigned FirstReg = MI->getOperand(RegListIdx).getReg();
1923 unsigned RD0Reg, RegsNeeded;
1926 RegsNeeded = NumBytes / 8;
1929 RegsNeeded = NumBytes / 4;
1932 // We're going to have to strip all list operands off before
1933 // re-adding them since the order matters, so save the existing ones
1935 SmallVector<MachineOperand, 4> RegList;
1936 for (int i = MI->getNumOperands() - 1; i >= RegListIdx; --i)
1937 RegList.push_back(MI->getOperand(i));
1939 const TargetRegisterInfo *TRI = MF.getRegInfo().getTargetRegisterInfo();
1940 const MCPhysReg *CSRegs = TRI->getCalleeSavedRegs(&MF);
1942 // Now try to find enough space in the reglist to allocate NumBytes.
1943 for (unsigned CurReg = FirstReg - 1; CurReg >= RD0Reg && RegsNeeded;
1946 // Pushing any register is completely harmless, mark the
1947 // register involved as undef since we don't care about it in
1949 RegList.push_back(MachineOperand::CreateReg(CurReg, false, false,
1950 false, false, true));
1955 // However, we can only pop an extra register if it's not live. For
1956 // registers live within the function we might clobber a return value
1957 // register; the other way a register can be live here is if it's
1959 // TODO: Currently, computeRegisterLiveness() does not report "live" if a
1960 // sub reg is live. When computeRegisterLiveness() works for sub reg, it
1961 // can replace isAnySubRegLive().
1962 if (isCalleeSavedRegister(CurReg, CSRegs) ||
1963 isAnySubRegLive(CurReg, TRI, MI)) {
1964 // VFP pops don't allow holes in the register list, so any skip is fatal
1965 // for our transformation. GPR pops do, so we should just keep looking.
1972 // Mark the unimportant registers as <def,dead> in the POP.
1973 RegList.push_back(MachineOperand::CreateReg(CurReg, true, false, false,
1981 // Finally we know we can profitably perform the optimisation so go
1982 // ahead: strip all existing registers off and add them back again
1983 // in the right order.
1984 for (int i = MI->getNumOperands() - 1; i >= RegListIdx; --i)
1985 MI->RemoveOperand(i);
1987 // Add the complete list back in.
1988 MachineInstrBuilder MIB(MF, &*MI);
1989 for (int i = RegList.size() - 1; i >= 0; --i)
1990 MIB.addOperand(RegList[i]);
1995 bool llvm::rewriteARMFrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
1996 unsigned FrameReg, int &Offset,
1997 const ARMBaseInstrInfo &TII) {
1998 unsigned Opcode = MI.getOpcode();
1999 const MCInstrDesc &Desc = MI.getDesc();
2000 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
2003 // Memory operands in inline assembly always use AddrMode2.
2004 if (Opcode == ARM::INLINEASM)
2005 AddrMode = ARMII::AddrMode2;
2007 if (Opcode == ARM::ADDri) {
2008 Offset += MI.getOperand(FrameRegIdx+1).getImm();
2010 // Turn it into a move.
2011 MI.setDesc(TII.get(ARM::MOVr));
2012 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
2013 MI.RemoveOperand(FrameRegIdx+1);
2016 } else if (Offset < 0) {
2019 MI.setDesc(TII.get(ARM::SUBri));
2022 // Common case: small offset, fits into instruction.
2023 if (ARM_AM::getSOImmVal(Offset) != -1) {
2024 // Replace the FrameIndex with sp / fp
2025 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
2026 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(Offset);
2031 // Otherwise, pull as much of the immedidate into this ADDri/SUBri
2033 unsigned RotAmt = ARM_AM::getSOImmValRotate(Offset);
2034 unsigned ThisImmVal = Offset & ARM_AM::rotr32(0xFF, RotAmt);
2036 // We will handle these bits from offset, clear them.
2037 Offset &= ~ThisImmVal;
2039 // Get the properly encoded SOImmVal field.
2040 assert(ARM_AM::getSOImmVal(ThisImmVal) != -1 &&
2041 "Bit extraction didn't work?");
2042 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(ThisImmVal);
2044 unsigned ImmIdx = 0;
2046 unsigned NumBits = 0;
2049 case ARMII::AddrMode_i12: {
2050 ImmIdx = FrameRegIdx + 1;
2051 InstrOffs = MI.getOperand(ImmIdx).getImm();
2055 case ARMII::AddrMode2: {
2056 ImmIdx = FrameRegIdx+2;
2057 InstrOffs = ARM_AM::getAM2Offset(MI.getOperand(ImmIdx).getImm());
2058 if (ARM_AM::getAM2Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
2063 case ARMII::AddrMode3: {
2064 ImmIdx = FrameRegIdx+2;
2065 InstrOffs = ARM_AM::getAM3Offset(MI.getOperand(ImmIdx).getImm());
2066 if (ARM_AM::getAM3Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
2071 case ARMII::AddrMode4:
2072 case ARMII::AddrMode6:
2073 // Can't fold any offset even if it's zero.
2075 case ARMII::AddrMode5: {
2076 ImmIdx = FrameRegIdx+1;
2077 InstrOffs = ARM_AM::getAM5Offset(MI.getOperand(ImmIdx).getImm());
2078 if (ARM_AM::getAM5Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
2085 llvm_unreachable("Unsupported addressing mode!");
2088 Offset += InstrOffs * Scale;
2089 assert((Offset & (Scale-1)) == 0 && "Can't encode this offset!");
2095 // Attempt to fold address comp. if opcode has offset bits
2097 // Common case: small offset, fits into instruction.
2098 MachineOperand &ImmOp = MI.getOperand(ImmIdx);
2099 int ImmedOffset = Offset / Scale;
2100 unsigned Mask = (1 << NumBits) - 1;
2101 if ((unsigned)Offset <= Mask * Scale) {
2102 // Replace the FrameIndex with sp
2103 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
2104 // FIXME: When addrmode2 goes away, this will simplify (like the
2105 // T2 version), as the LDR.i12 versions don't need the encoding
2106 // tricks for the offset value.
2108 if (AddrMode == ARMII::AddrMode_i12)
2109 ImmedOffset = -ImmedOffset;
2111 ImmedOffset |= 1 << NumBits;
2113 ImmOp.ChangeToImmediate(ImmedOffset);
2118 // Otherwise, it didn't fit. Pull in what we can to simplify the immed.
2119 ImmedOffset = ImmedOffset & Mask;
2121 if (AddrMode == ARMII::AddrMode_i12)
2122 ImmedOffset = -ImmedOffset;
2124 ImmedOffset |= 1 << NumBits;
2126 ImmOp.ChangeToImmediate(ImmedOffset);
2127 Offset &= ~(Mask*Scale);
2131 Offset = (isSub) ? -Offset : Offset;
2135 /// analyzeCompare - For a comparison instruction, return the source registers
2136 /// in SrcReg and SrcReg2 if having two register operands, and the value it
2137 /// compares against in CmpValue. Return true if the comparison instruction
2138 /// can be analyzed.
2139 bool ARMBaseInstrInfo::
2140 analyzeCompare(const MachineInstr *MI, unsigned &SrcReg, unsigned &SrcReg2,
2141 int &CmpMask, int &CmpValue) const {
2142 switch (MI->getOpcode()) {
2146 SrcReg = MI->getOperand(0).getReg();
2149 CmpValue = MI->getOperand(1).getImm();
2153 SrcReg = MI->getOperand(0).getReg();
2154 SrcReg2 = MI->getOperand(1).getReg();
2160 SrcReg = MI->getOperand(0).getReg();
2162 CmpMask = MI->getOperand(1).getImm();
2170 /// isSuitableForMask - Identify a suitable 'and' instruction that
2171 /// operates on the given source register and applies the same mask
2172 /// as a 'tst' instruction. Provide a limited look-through for copies.
2173 /// When successful, MI will hold the found instruction.
2174 static bool isSuitableForMask(MachineInstr *&MI, unsigned SrcReg,
2175 int CmpMask, bool CommonUse) {
2176 switch (MI->getOpcode()) {
2179 if (CmpMask != MI->getOperand(2).getImm())
2181 if (SrcReg == MI->getOperand(CommonUse ? 1 : 0).getReg())
2185 // Walk down one instruction which is potentially an 'and'.
2186 const MachineInstr &Copy = *MI;
2187 MachineBasicBlock::iterator AND(
2188 std::next(MachineBasicBlock::iterator(MI)));
2189 if (AND == MI->getParent()->end()) return false;
2191 return isSuitableForMask(MI, Copy.getOperand(0).getReg(),
2199 /// getSwappedCondition - assume the flags are set by MI(a,b), return
2200 /// the condition code if we modify the instructions such that flags are
2202 inline static ARMCC::CondCodes getSwappedCondition(ARMCC::CondCodes CC) {
2204 default: return ARMCC::AL;
2205 case ARMCC::EQ: return ARMCC::EQ;
2206 case ARMCC::NE: return ARMCC::NE;
2207 case ARMCC::HS: return ARMCC::LS;
2208 case ARMCC::LO: return ARMCC::HI;
2209 case ARMCC::HI: return ARMCC::LO;
2210 case ARMCC::LS: return ARMCC::HS;
2211 case ARMCC::GE: return ARMCC::LE;
2212 case ARMCC::LT: return ARMCC::GT;
2213 case ARMCC::GT: return ARMCC::LT;
2214 case ARMCC::LE: return ARMCC::GE;
2218 /// isRedundantFlagInstr - check whether the first instruction, whose only
2219 /// purpose is to update flags, can be made redundant.
2220 /// CMPrr can be made redundant by SUBrr if the operands are the same.
2221 /// CMPri can be made redundant by SUBri if the operands are the same.
2222 /// This function can be extended later on.
2223 inline static bool isRedundantFlagInstr(MachineInstr *CmpI, unsigned SrcReg,
2224 unsigned SrcReg2, int ImmValue,
2226 if ((CmpI->getOpcode() == ARM::CMPrr ||
2227 CmpI->getOpcode() == ARM::t2CMPrr) &&
2228 (OI->getOpcode() == ARM::SUBrr ||
2229 OI->getOpcode() == ARM::t2SUBrr) &&
2230 ((OI->getOperand(1).getReg() == SrcReg &&
2231 OI->getOperand(2).getReg() == SrcReg2) ||
2232 (OI->getOperand(1).getReg() == SrcReg2 &&
2233 OI->getOperand(2).getReg() == SrcReg)))
2236 if ((CmpI->getOpcode() == ARM::CMPri ||
2237 CmpI->getOpcode() == ARM::t2CMPri) &&
2238 (OI->getOpcode() == ARM::SUBri ||
2239 OI->getOpcode() == ARM::t2SUBri) &&
2240 OI->getOperand(1).getReg() == SrcReg &&
2241 OI->getOperand(2).getImm() == ImmValue)
2246 /// optimizeCompareInstr - Convert the instruction supplying the argument to the
2247 /// comparison into one that sets the zero bit in the flags register;
2248 /// Remove a redundant Compare instruction if an earlier instruction can set the
2249 /// flags in the same way as Compare.
2250 /// E.g. SUBrr(r1,r2) and CMPrr(r1,r2). We also handle the case where two
2251 /// operands are swapped: SUBrr(r1,r2) and CMPrr(r2,r1), by updating the
2252 /// condition code of instructions which use the flags.
2253 bool ARMBaseInstrInfo::
2254 optimizeCompareInstr(MachineInstr *CmpInstr, unsigned SrcReg, unsigned SrcReg2,
2255 int CmpMask, int CmpValue,
2256 const MachineRegisterInfo *MRI) const {
2257 // Get the unique definition of SrcReg.
2258 MachineInstr *MI = MRI->getUniqueVRegDef(SrcReg);
2259 if (!MI) return false;
2261 // Masked compares sometimes use the same register as the corresponding 'and'.
2262 if (CmpMask != ~0) {
2263 if (!isSuitableForMask(MI, SrcReg, CmpMask, false) || isPredicated(MI)) {
2265 for (MachineRegisterInfo::use_instr_iterator
2266 UI = MRI->use_instr_begin(SrcReg), UE = MRI->use_instr_end();
2268 if (UI->getParent() != CmpInstr->getParent()) continue;
2269 MachineInstr *PotentialAND = &*UI;
2270 if (!isSuitableForMask(PotentialAND, SrcReg, CmpMask, true) ||
2271 isPredicated(PotentialAND))
2276 if (!MI) return false;
2280 // Get ready to iterate backward from CmpInstr.
2281 MachineBasicBlock::iterator I = CmpInstr, E = MI,
2282 B = CmpInstr->getParent()->begin();
2284 // Early exit if CmpInstr is at the beginning of the BB.
2285 if (I == B) return false;
2287 // There are two possible candidates which can be changed to set CPSR:
2288 // One is MI, the other is a SUB instruction.
2289 // For CMPrr(r1,r2), we are looking for SUB(r1,r2) or SUB(r2,r1).
2290 // For CMPri(r1, CmpValue), we are looking for SUBri(r1, CmpValue).
2291 MachineInstr *Sub = nullptr;
2293 // MI is not a candidate for CMPrr.
2295 else if (MI->getParent() != CmpInstr->getParent() || CmpValue != 0) {
2296 // Conservatively refuse to convert an instruction which isn't in the same
2297 // BB as the comparison.
2298 // For CMPri, we need to check Sub, thus we can't return here.
2299 if (CmpInstr->getOpcode() == ARM::CMPri ||
2300 CmpInstr->getOpcode() == ARM::t2CMPri)
2306 // Check that CPSR isn't set between the comparison instruction and the one we
2307 // want to change. At the same time, search for Sub.
2308 const TargetRegisterInfo *TRI = &getRegisterInfo();
2310 for (; I != E; --I) {
2311 const MachineInstr &Instr = *I;
2313 if (Instr.modifiesRegister(ARM::CPSR, TRI) ||
2314 Instr.readsRegister(ARM::CPSR, TRI))
2315 // This instruction modifies or uses CPSR after the one we want to
2316 // change. We can't do this transformation.
2319 // Check whether CmpInstr can be made redundant by the current instruction.
2320 if (isRedundantFlagInstr(CmpInstr, SrcReg, SrcReg2, CmpValue, &*I)) {
2326 // The 'and' is below the comparison instruction.
2330 // Return false if no candidates exist.
2334 // The single candidate is called MI.
2337 // We can't use a predicated instruction - it doesn't always write the flags.
2338 if (isPredicated(MI))
2341 switch (MI->getOpcode()) {
2375 case ARM::t2EORri: {
2376 // Scan forward for the use of CPSR
2377 // When checking against MI: if it's a conditional code requires
2378 // checking of V bit, then this is not safe to do.
2379 // It is safe to remove CmpInstr if CPSR is redefined or killed.
2380 // If we are done with the basic block, we need to check whether CPSR is
2382 SmallVector<std::pair<MachineOperand*, ARMCC::CondCodes>, 4>
2384 bool isSafe = false;
2386 E = CmpInstr->getParent()->end();
2387 while (!isSafe && ++I != E) {
2388 const MachineInstr &Instr = *I;
2389 for (unsigned IO = 0, EO = Instr.getNumOperands();
2390 !isSafe && IO != EO; ++IO) {
2391 const MachineOperand &MO = Instr.getOperand(IO);
2392 if (MO.isRegMask() && MO.clobbersPhysReg(ARM::CPSR)) {
2396 if (!MO.isReg() || MO.getReg() != ARM::CPSR)
2402 // Condition code is after the operand before CPSR except for VSELs.
2403 ARMCC::CondCodes CC;
2404 bool IsInstrVSel = true;
2405 switch (Instr.getOpcode()) {
2407 IsInstrVSel = false;
2408 CC = (ARMCC::CondCodes)Instr.getOperand(IO - 1).getImm();
2429 ARMCC::CondCodes NewCC = getSwappedCondition(CC);
2430 if (NewCC == ARMCC::AL)
2432 // If we have SUB(r1, r2) and CMP(r2, r1), the condition code based
2433 // on CMP needs to be updated to be based on SUB.
2434 // Push the condition code operands to OperandsToUpdate.
2435 // If it is safe to remove CmpInstr, the condition code of these
2436 // operands will be modified.
2437 if (SrcReg2 != 0 && Sub->getOperand(1).getReg() == SrcReg2 &&
2438 Sub->getOperand(2).getReg() == SrcReg) {
2439 // VSel doesn't support condition code update.
2442 OperandsToUpdate.push_back(
2443 std::make_pair(&((*I).getOperand(IO - 1)), NewCC));
2448 // CPSR can be used multiple times, we should continue.
2461 // If CPSR is not killed nor re-defined, we should check whether it is
2462 // live-out. If it is live-out, do not optimize.
2464 MachineBasicBlock *MBB = CmpInstr->getParent();
2465 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
2466 SE = MBB->succ_end(); SI != SE; ++SI)
2467 if ((*SI)->isLiveIn(ARM::CPSR))
2471 // Toggle the optional operand to CPSR.
2472 MI->getOperand(5).setReg(ARM::CPSR);
2473 MI->getOperand(5).setIsDef(true);
2474 assert(!isPredicated(MI) && "Can't use flags from predicated instruction");
2475 CmpInstr->eraseFromParent();
2477 // Modify the condition code of operands in OperandsToUpdate.
2478 // Since we have SUB(r1, r2) and CMP(r2, r1), the condition code needs to
2479 // be changed from r2 > r1 to r1 < r2, from r2 < r1 to r1 > r2, etc.
2480 for (unsigned i = 0, e = OperandsToUpdate.size(); i < e; i++)
2481 OperandsToUpdate[i].first->setImm(OperandsToUpdate[i].second);
2489 bool ARMBaseInstrInfo::FoldImmediate(MachineInstr *UseMI,
2490 MachineInstr *DefMI, unsigned Reg,
2491 MachineRegisterInfo *MRI) const {
2492 // Fold large immediates into add, sub, or, xor.
2493 unsigned DefOpc = DefMI->getOpcode();
2494 if (DefOpc != ARM::t2MOVi32imm && DefOpc != ARM::MOVi32imm)
2496 if (!DefMI->getOperand(1).isImm())
2497 // Could be t2MOVi32imm <ga:xx>
2500 if (!MRI->hasOneNonDBGUse(Reg))
2503 const MCInstrDesc &DefMCID = DefMI->getDesc();
2504 if (DefMCID.hasOptionalDef()) {
2505 unsigned NumOps = DefMCID.getNumOperands();
2506 const MachineOperand &MO = DefMI->getOperand(NumOps-1);
2507 if (MO.getReg() == ARM::CPSR && !MO.isDead())
2508 // If DefMI defines CPSR and it is not dead, it's obviously not safe
2513 const MCInstrDesc &UseMCID = UseMI->getDesc();
2514 if (UseMCID.hasOptionalDef()) {
2515 unsigned NumOps = UseMCID.getNumOperands();
2516 if (UseMI->getOperand(NumOps-1).getReg() == ARM::CPSR)
2517 // If the instruction sets the flag, do not attempt this optimization
2518 // since it may change the semantics of the code.
2522 unsigned UseOpc = UseMI->getOpcode();
2523 unsigned NewUseOpc = 0;
2524 uint32_t ImmVal = (uint32_t)DefMI->getOperand(1).getImm();
2525 uint32_t SOImmValV1 = 0, SOImmValV2 = 0;
2526 bool Commute = false;
2528 default: return false;
2536 case ARM::t2EORrr: {
2537 Commute = UseMI->getOperand(2).getReg() != Reg;
2544 NewUseOpc = ARM::SUBri;
2550 if (!ARM_AM::isSOImmTwoPartVal(ImmVal))
2552 SOImmValV1 = (uint32_t)ARM_AM::getSOImmTwoPartFirst(ImmVal);
2553 SOImmValV2 = (uint32_t)ARM_AM::getSOImmTwoPartSecond(ImmVal);
2556 case ARM::ADDrr: NewUseOpc = ARM::ADDri; break;
2557 case ARM::ORRrr: NewUseOpc = ARM::ORRri; break;
2558 case ARM::EORrr: NewUseOpc = ARM::EORri; break;
2562 case ARM::t2SUBrr: {
2566 NewUseOpc = ARM::t2SUBri;
2571 case ARM::t2EORrr: {
2572 if (!ARM_AM::isT2SOImmTwoPartVal(ImmVal))
2574 SOImmValV1 = (uint32_t)ARM_AM::getT2SOImmTwoPartFirst(ImmVal);
2575 SOImmValV2 = (uint32_t)ARM_AM::getT2SOImmTwoPartSecond(ImmVal);
2578 case ARM::t2ADDrr: NewUseOpc = ARM::t2ADDri; break;
2579 case ARM::t2ORRrr: NewUseOpc = ARM::t2ORRri; break;
2580 case ARM::t2EORrr: NewUseOpc = ARM::t2EORri; break;
2588 unsigned OpIdx = Commute ? 2 : 1;
2589 unsigned Reg1 = UseMI->getOperand(OpIdx).getReg();
2590 bool isKill = UseMI->getOperand(OpIdx).isKill();
2591 unsigned NewReg = MRI->createVirtualRegister(MRI->getRegClass(Reg));
2592 AddDefaultCC(AddDefaultPred(BuildMI(*UseMI->getParent(),
2593 UseMI, UseMI->getDebugLoc(),
2594 get(NewUseOpc), NewReg)
2595 .addReg(Reg1, getKillRegState(isKill))
2596 .addImm(SOImmValV1)));
2597 UseMI->setDesc(get(NewUseOpc));
2598 UseMI->getOperand(1).setReg(NewReg);
2599 UseMI->getOperand(1).setIsKill();
2600 UseMI->getOperand(2).ChangeToImmediate(SOImmValV2);
2601 DefMI->eraseFromParent();
2605 static unsigned getNumMicroOpsSwiftLdSt(const InstrItineraryData *ItinData,
2606 const MachineInstr *MI) {
2607 switch (MI->getOpcode()) {
2609 const MCInstrDesc &Desc = MI->getDesc();
2610 int UOps = ItinData->getNumMicroOps(Desc.getSchedClass());
2611 assert(UOps >= 0 && "bad # UOps");
2619 unsigned ShOpVal = MI->getOperand(3).getImm();
2620 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2621 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2624 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2625 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2632 if (!MI->getOperand(2).getReg())
2635 unsigned ShOpVal = MI->getOperand(3).getImm();
2636 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2637 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2640 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2641 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2648 return (ARM_AM::getAM3Op(MI->getOperand(3).getImm()) == ARM_AM::sub) ? 3:2;
2650 case ARM::LDRSB_POST:
2651 case ARM::LDRSH_POST: {
2652 unsigned Rt = MI->getOperand(0).getReg();
2653 unsigned Rm = MI->getOperand(3).getReg();
2654 return (Rt == Rm) ? 4 : 3;
2657 case ARM::LDR_PRE_REG:
2658 case ARM::LDRB_PRE_REG: {
2659 unsigned Rt = MI->getOperand(0).getReg();
2660 unsigned Rm = MI->getOperand(3).getReg();
2663 unsigned ShOpVal = MI->getOperand(4).getImm();
2664 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2665 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2668 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2669 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2674 case ARM::STR_PRE_REG:
2675 case ARM::STRB_PRE_REG: {
2676 unsigned ShOpVal = MI->getOperand(4).getImm();
2677 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2678 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2681 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2682 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2688 case ARM::STRH_PRE: {
2689 unsigned Rt = MI->getOperand(0).getReg();
2690 unsigned Rm = MI->getOperand(3).getReg();
2695 return (ARM_AM::getAM3Op(MI->getOperand(4).getImm()) == ARM_AM::sub)
2699 case ARM::LDR_POST_REG:
2700 case ARM::LDRB_POST_REG:
2701 case ARM::LDRH_POST: {
2702 unsigned Rt = MI->getOperand(0).getReg();
2703 unsigned Rm = MI->getOperand(3).getReg();
2704 return (Rt == Rm) ? 3 : 2;
2707 case ARM::LDR_PRE_IMM:
2708 case ARM::LDRB_PRE_IMM:
2709 case ARM::LDR_POST_IMM:
2710 case ARM::LDRB_POST_IMM:
2711 case ARM::STRB_POST_IMM:
2712 case ARM::STRB_POST_REG:
2713 case ARM::STRB_PRE_IMM:
2714 case ARM::STRH_POST:
2715 case ARM::STR_POST_IMM:
2716 case ARM::STR_POST_REG:
2717 case ARM::STR_PRE_IMM:
2720 case ARM::LDRSB_PRE:
2721 case ARM::LDRSH_PRE: {
2722 unsigned Rm = MI->getOperand(3).getReg();
2725 unsigned Rt = MI->getOperand(0).getReg();
2728 unsigned ShOpVal = MI->getOperand(4).getImm();
2729 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2730 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2733 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2734 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2740 unsigned Rt = MI->getOperand(0).getReg();
2741 unsigned Rn = MI->getOperand(2).getReg();
2742 unsigned Rm = MI->getOperand(3).getReg();
2744 return (ARM_AM::getAM3Op(MI->getOperand(4).getImm()) == ARM_AM::sub) ?4:3;
2745 return (Rt == Rn) ? 3 : 2;
2749 unsigned Rm = MI->getOperand(3).getReg();
2751 return (ARM_AM::getAM3Op(MI->getOperand(4).getImm()) == ARM_AM::sub) ?4:3;
2755 case ARM::LDRD_POST:
2756 case ARM::t2LDRD_POST:
2759 case ARM::STRD_POST:
2760 case ARM::t2STRD_POST:
2763 case ARM::LDRD_PRE: {
2764 unsigned Rt = MI->getOperand(0).getReg();
2765 unsigned Rn = MI->getOperand(3).getReg();
2766 unsigned Rm = MI->getOperand(4).getReg();
2768 return (ARM_AM::getAM3Op(MI->getOperand(5).getImm()) == ARM_AM::sub) ?5:4;
2769 return (Rt == Rn) ? 4 : 3;
2772 case ARM::t2LDRD_PRE: {
2773 unsigned Rt = MI->getOperand(0).getReg();
2774 unsigned Rn = MI->getOperand(3).getReg();
2775 return (Rt == Rn) ? 4 : 3;
2778 case ARM::STRD_PRE: {
2779 unsigned Rm = MI->getOperand(4).getReg();
2781 return (ARM_AM::getAM3Op(MI->getOperand(5).getImm()) == ARM_AM::sub) ?5:4;
2785 case ARM::t2STRD_PRE:
2788 case ARM::t2LDR_POST:
2789 case ARM::t2LDRB_POST:
2790 case ARM::t2LDRB_PRE:
2791 case ARM::t2LDRSBi12:
2792 case ARM::t2LDRSBi8:
2793 case ARM::t2LDRSBpci:
2795 case ARM::t2LDRH_POST:
2796 case ARM::t2LDRH_PRE:
2798 case ARM::t2LDRSB_POST:
2799 case ARM::t2LDRSB_PRE:
2800 case ARM::t2LDRSH_POST:
2801 case ARM::t2LDRSH_PRE:
2802 case ARM::t2LDRSHi12:
2803 case ARM::t2LDRSHi8:
2804 case ARM::t2LDRSHpci:
2808 case ARM::t2LDRDi8: {
2809 unsigned Rt = MI->getOperand(0).getReg();
2810 unsigned Rn = MI->getOperand(2).getReg();
2811 return (Rt == Rn) ? 3 : 2;
2814 case ARM::t2STRB_POST:
2815 case ARM::t2STRB_PRE:
2818 case ARM::t2STRH_POST:
2819 case ARM::t2STRH_PRE:
2821 case ARM::t2STR_POST:
2822 case ARM::t2STR_PRE:
2828 // Return the number of 32-bit words loaded by LDM or stored by STM. If this
2829 // can't be easily determined return 0 (missing MachineMemOperand).
2831 // FIXME: The current MachineInstr design does not support relying on machine
2832 // mem operands to determine the width of a memory access. Instead, we expect
2833 // the target to provide this information based on the instruction opcode and
2834 // operands. However, using MachineMemOperand is a the best solution now for
2837 // 1) getNumMicroOps tries to infer LDM memory width from the total number of MI
2838 // operands. This is much more dangerous than using the MachineMemOperand
2839 // sizes because CodeGen passes can insert/remove optional machine operands. In
2840 // fact, it's totally incorrect for preRA passes and appears to be wrong for
2841 // postRA passes as well.
2843 // 2) getNumLDMAddresses is only used by the scheduling machine model and any
2844 // machine model that calls this should handle the unknown (zero size) case.
2846 // Long term, we should require a target hook that verifies MachineMemOperand
2847 // sizes during MC lowering. That target hook should be local to MC lowering
2848 // because we can't ensure that it is aware of other MI forms. Doing this will
2849 // ensure that MachineMemOperands are correctly propagated through all passes.
2850 unsigned ARMBaseInstrInfo::getNumLDMAddresses(const MachineInstr *MI) const {
2852 for (MachineInstr::mmo_iterator I = MI->memoperands_begin(),
2853 E = MI->memoperands_end(); I != E; ++I) {
2854 Size += (*I)->getSize();
2860 ARMBaseInstrInfo::getNumMicroOps(const InstrItineraryData *ItinData,
2861 const MachineInstr *MI) const {
2862 if (!ItinData || ItinData->isEmpty())
2865 const MCInstrDesc &Desc = MI->getDesc();
2866 unsigned Class = Desc.getSchedClass();
2867 int ItinUOps = ItinData->getNumMicroOps(Class);
2868 if (ItinUOps >= 0) {
2869 if (Subtarget.isSwift() && (Desc.mayLoad() || Desc.mayStore()))
2870 return getNumMicroOpsSwiftLdSt(ItinData, MI);
2875 unsigned Opc = MI->getOpcode();
2878 llvm_unreachable("Unexpected multi-uops instruction!");
2883 // The number of uOps for load / store multiple are determined by the number
2886 // On Cortex-A8, each pair of register loads / stores can be scheduled on the
2887 // same cycle. The scheduling for the first load / store must be done
2888 // separately by assuming the address is not 64-bit aligned.
2890 // On Cortex-A9, the formula is simply (#reg / 2) + (#reg % 2). If the address
2891 // is not 64-bit aligned, then AGU would take an extra cycle. For VFP / NEON
2892 // load / store multiple, the formula is (#reg / 2) + (#reg % 2) + 1.
2894 case ARM::VLDMDIA_UPD:
2895 case ARM::VLDMDDB_UPD:
2897 case ARM::VLDMSIA_UPD:
2898 case ARM::VLDMSDB_UPD:
2900 case ARM::VSTMDIA_UPD:
2901 case ARM::VSTMDDB_UPD:
2903 case ARM::VSTMSIA_UPD:
2904 case ARM::VSTMSDB_UPD: {
2905 unsigned NumRegs = MI->getNumOperands() - Desc.getNumOperands();
2906 return (NumRegs / 2) + (NumRegs % 2) + 1;
2909 case ARM::LDMIA_RET:
2914 case ARM::LDMIA_UPD:
2915 case ARM::LDMDA_UPD:
2916 case ARM::LDMDB_UPD:
2917 case ARM::LDMIB_UPD:
2922 case ARM::STMIA_UPD:
2923 case ARM::STMDA_UPD:
2924 case ARM::STMDB_UPD:
2925 case ARM::STMIB_UPD:
2927 case ARM::tLDMIA_UPD:
2928 case ARM::tSTMIA_UPD:
2932 case ARM::t2LDMIA_RET:
2935 case ARM::t2LDMIA_UPD:
2936 case ARM::t2LDMDB_UPD:
2939 case ARM::t2STMIA_UPD:
2940 case ARM::t2STMDB_UPD: {
2941 unsigned NumRegs = MI->getNumOperands() - Desc.getNumOperands() + 1;
2942 if (Subtarget.isSwift()) {
2943 int UOps = 1 + NumRegs; // One for address computation, one for each ld / st.
2946 case ARM::VLDMDIA_UPD:
2947 case ARM::VLDMDDB_UPD:
2948 case ARM::VLDMSIA_UPD:
2949 case ARM::VLDMSDB_UPD:
2950 case ARM::VSTMDIA_UPD:
2951 case ARM::VSTMDDB_UPD:
2952 case ARM::VSTMSIA_UPD:
2953 case ARM::VSTMSDB_UPD:
2954 case ARM::LDMIA_UPD:
2955 case ARM::LDMDA_UPD:
2956 case ARM::LDMDB_UPD:
2957 case ARM::LDMIB_UPD:
2958 case ARM::STMIA_UPD:
2959 case ARM::STMDA_UPD:
2960 case ARM::STMDB_UPD:
2961 case ARM::STMIB_UPD:
2962 case ARM::tLDMIA_UPD:
2963 case ARM::tSTMIA_UPD:
2964 case ARM::t2LDMIA_UPD:
2965 case ARM::t2LDMDB_UPD:
2966 case ARM::t2STMIA_UPD:
2967 case ARM::t2STMDB_UPD:
2968 ++UOps; // One for base register writeback.
2970 case ARM::LDMIA_RET:
2972 case ARM::t2LDMIA_RET:
2973 UOps += 2; // One for base reg wb, one for write to pc.
2977 } else if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
2980 // 4 registers would be issued: 2, 2.
2981 // 5 registers would be issued: 2, 2, 1.
2982 int A8UOps = (NumRegs / 2);
2986 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
2987 int A9UOps = (NumRegs / 2);
2988 // If there are odd number of registers or if it's not 64-bit aligned,
2989 // then it takes an extra AGU (Address Generation Unit) cycle.
2990 if ((NumRegs % 2) ||
2991 !MI->hasOneMemOperand() ||
2992 (*MI->memoperands_begin())->getAlignment() < 8)
2996 // Assume the worst.
3004 ARMBaseInstrInfo::getVLDMDefCycle(const InstrItineraryData *ItinData,
3005 const MCInstrDesc &DefMCID,
3007 unsigned DefIdx, unsigned DefAlign) const {
3008 int RegNo = (int)(DefIdx+1) - DefMCID.getNumOperands() + 1;
3010 // Def is the address writeback.
3011 return ItinData->getOperandCycle(DefClass, DefIdx);
3014 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
3015 // (regno / 2) + (regno % 2) + 1
3016 DefCycle = RegNo / 2 + 1;
3019 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
3021 bool isSLoad = false;
3023 switch (DefMCID.getOpcode()) {
3026 case ARM::VLDMSIA_UPD:
3027 case ARM::VLDMSDB_UPD:
3032 // If there are odd number of 'S' registers or if it's not 64-bit aligned,
3033 // then it takes an extra cycle.
3034 if ((isSLoad && (RegNo % 2)) || DefAlign < 8)
3037 // Assume the worst.
3038 DefCycle = RegNo + 2;
3045 ARMBaseInstrInfo::getLDMDefCycle(const InstrItineraryData *ItinData,
3046 const MCInstrDesc &DefMCID,
3048 unsigned DefIdx, unsigned DefAlign) const {
3049 int RegNo = (int)(DefIdx+1) - DefMCID.getNumOperands() + 1;
3051 // Def is the address writeback.
3052 return ItinData->getOperandCycle(DefClass, DefIdx);
3055 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
3056 // 4 registers would be issued: 1, 2, 1.
3057 // 5 registers would be issued: 1, 2, 2.
3058 DefCycle = RegNo / 2;
3061 // Result latency is issue cycle + 2: E2.
3063 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
3064 DefCycle = (RegNo / 2);
3065 // If there are odd number of registers or if it's not 64-bit aligned,
3066 // then it takes an extra AGU (Address Generation Unit) cycle.
3067 if ((RegNo % 2) || DefAlign < 8)
3069 // Result latency is AGU cycles + 2.
3072 // Assume the worst.
3073 DefCycle = RegNo + 2;
3080 ARMBaseInstrInfo::getVSTMUseCycle(const InstrItineraryData *ItinData,
3081 const MCInstrDesc &UseMCID,
3083 unsigned UseIdx, unsigned UseAlign) const {
3084 int RegNo = (int)(UseIdx+1) - UseMCID.getNumOperands() + 1;
3086 return ItinData->getOperandCycle(UseClass, UseIdx);
3089 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
3090 // (regno / 2) + (regno % 2) + 1
3091 UseCycle = RegNo / 2 + 1;
3094 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
3096 bool isSStore = false;
3098 switch (UseMCID.getOpcode()) {
3101 case ARM::VSTMSIA_UPD:
3102 case ARM::VSTMSDB_UPD:
3107 // If there are odd number of 'S' registers or if it's not 64-bit aligned,
3108 // then it takes an extra cycle.
3109 if ((isSStore && (RegNo % 2)) || UseAlign < 8)
3112 // Assume the worst.
3113 UseCycle = RegNo + 2;
3120 ARMBaseInstrInfo::getSTMUseCycle(const InstrItineraryData *ItinData,
3121 const MCInstrDesc &UseMCID,
3123 unsigned UseIdx, unsigned UseAlign) const {
3124 int RegNo = (int)(UseIdx+1) - UseMCID.getNumOperands() + 1;
3126 return ItinData->getOperandCycle(UseClass, UseIdx);
3129 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
3130 UseCycle = RegNo / 2;
3135 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
3136 UseCycle = (RegNo / 2);
3137 // If there are odd number of registers or if it's not 64-bit aligned,
3138 // then it takes an extra AGU (Address Generation Unit) cycle.
3139 if ((RegNo % 2) || UseAlign < 8)
3142 // Assume the worst.
3149 ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
3150 const MCInstrDesc &DefMCID,
3151 unsigned DefIdx, unsigned DefAlign,
3152 const MCInstrDesc &UseMCID,
3153 unsigned UseIdx, unsigned UseAlign) const {
3154 unsigned DefClass = DefMCID.getSchedClass();
3155 unsigned UseClass = UseMCID.getSchedClass();
3157 if (DefIdx < DefMCID.getNumDefs() && UseIdx < UseMCID.getNumOperands())
3158 return ItinData->getOperandLatency(DefClass, DefIdx, UseClass, UseIdx);
3160 // This may be a def / use of a variable_ops instruction, the operand
3161 // latency might be determinable dynamically. Let the target try to
3164 bool LdmBypass = false;
3165 switch (DefMCID.getOpcode()) {
3167 DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
3171 case ARM::VLDMDIA_UPD:
3172 case ARM::VLDMDDB_UPD:
3174 case ARM::VLDMSIA_UPD:
3175 case ARM::VLDMSDB_UPD:
3176 DefCycle = getVLDMDefCycle(ItinData, DefMCID, DefClass, DefIdx, DefAlign);
3179 case ARM::LDMIA_RET:
3184 case ARM::LDMIA_UPD:
3185 case ARM::LDMDA_UPD:
3186 case ARM::LDMDB_UPD:
3187 case ARM::LDMIB_UPD:
3189 case ARM::tLDMIA_UPD:
3191 case ARM::t2LDMIA_RET:
3194 case ARM::t2LDMIA_UPD:
3195 case ARM::t2LDMDB_UPD:
3197 DefCycle = getLDMDefCycle(ItinData, DefMCID, DefClass, DefIdx, DefAlign);
3202 // We can't seem to determine the result latency of the def, assume it's 2.
3206 switch (UseMCID.getOpcode()) {
3208 UseCycle = ItinData->getOperandCycle(UseClass, UseIdx);
3212 case ARM::VSTMDIA_UPD:
3213 case ARM::VSTMDDB_UPD:
3215 case ARM::VSTMSIA_UPD:
3216 case ARM::VSTMSDB_UPD:
3217 UseCycle = getVSTMUseCycle(ItinData, UseMCID, UseClass, UseIdx, UseAlign);
3224 case ARM::STMIA_UPD:
3225 case ARM::STMDA_UPD:
3226 case ARM::STMDB_UPD:
3227 case ARM::STMIB_UPD:
3228 case ARM::tSTMIA_UPD:
3233 case ARM::t2STMIA_UPD:
3234 case ARM::t2STMDB_UPD:
3235 UseCycle = getSTMUseCycle(ItinData, UseMCID, UseClass, UseIdx, UseAlign);
3240 // Assume it's read in the first stage.
3243 UseCycle = DefCycle - UseCycle + 1;
3246 // It's a variable_ops instruction so we can't use DefIdx here. Just use
3247 // first def operand.
3248 if (ItinData->hasPipelineForwarding(DefClass, DefMCID.getNumOperands()-1,
3251 } else if (ItinData->hasPipelineForwarding(DefClass, DefIdx,
3252 UseClass, UseIdx)) {
3260 static const MachineInstr *getBundledDefMI(const TargetRegisterInfo *TRI,
3261 const MachineInstr *MI, unsigned Reg,
3262 unsigned &DefIdx, unsigned &Dist) {
3265 MachineBasicBlock::const_iterator I = MI; ++I;
3266 MachineBasicBlock::const_instr_iterator II = std::prev(I.getInstrIterator());
3267 assert(II->isInsideBundle() && "Empty bundle?");
3270 while (II->isInsideBundle()) {
3271 Idx = II->findRegisterDefOperandIdx(Reg, false, true, TRI);
3278 assert(Idx != -1 && "Cannot find bundled definition!");
3283 static const MachineInstr *getBundledUseMI(const TargetRegisterInfo *TRI,
3284 const MachineInstr *MI, unsigned Reg,
3285 unsigned &UseIdx, unsigned &Dist) {
3288 MachineBasicBlock::const_instr_iterator II = MI; ++II;
3289 assert(II->isInsideBundle() && "Empty bundle?");
3290 MachineBasicBlock::const_instr_iterator E = MI->getParent()->instr_end();
3292 // FIXME: This doesn't properly handle multiple uses.
3294 while (II != E && II->isInsideBundle()) {
3295 Idx = II->findRegisterUseOperandIdx(Reg, false, TRI);
3298 if (II->getOpcode() != ARM::t2IT)
3312 /// Return the number of cycles to add to (or subtract from) the static
3313 /// itinerary based on the def opcode and alignment. The caller will ensure that
3314 /// adjusted latency is at least one cycle.
3315 static int adjustDefLatency(const ARMSubtarget &Subtarget,
3316 const MachineInstr *DefMI,
3317 const MCInstrDesc *DefMCID, unsigned DefAlign) {
3319 if (Subtarget.isCortexA8() || Subtarget.isLikeA9() || Subtarget.isCortexA7()) {
3320 // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2]
3321 // variants are one cycle cheaper.
3322 switch (DefMCID->getOpcode()) {
3326 unsigned ShOpVal = DefMI->getOperand(3).getImm();
3327 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3329 (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
3336 case ARM::t2LDRSHs: {
3337 // Thumb2 mode: lsl only.
3338 unsigned ShAmt = DefMI->getOperand(3).getImm();
3339 if (ShAmt == 0 || ShAmt == 2)
3344 } else if (Subtarget.isSwift()) {
3345 // FIXME: Properly handle all of the latency adjustments for address
3347 switch (DefMCID->getOpcode()) {
3351 unsigned ShOpVal = DefMI->getOperand(3).getImm();
3352 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
3353 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3356 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
3357 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
3360 ShImm == 1 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsr)
3367 case ARM::t2LDRSHs: {
3368 // Thumb2 mode: lsl only.
3369 unsigned ShAmt = DefMI->getOperand(3).getImm();
3370 if (ShAmt == 0 || ShAmt == 1 || ShAmt == 2 || ShAmt == 3)
3377 if (DefAlign < 8 && Subtarget.isLikeA9()) {
3378 switch (DefMCID->getOpcode()) {
3384 case ARM::VLD1q8wb_fixed:
3385 case ARM::VLD1q16wb_fixed:
3386 case ARM::VLD1q32wb_fixed:
3387 case ARM::VLD1q64wb_fixed:
3388 case ARM::VLD1q8wb_register:
3389 case ARM::VLD1q16wb_register:
3390 case ARM::VLD1q32wb_register:
3391 case ARM::VLD1q64wb_register:
3398 case ARM::VLD2d8wb_fixed:
3399 case ARM::VLD2d16wb_fixed:
3400 case ARM::VLD2d32wb_fixed:
3401 case ARM::VLD2q8wb_fixed:
3402 case ARM::VLD2q16wb_fixed:
3403 case ARM::VLD2q32wb_fixed:
3404 case ARM::VLD2d8wb_register:
3405 case ARM::VLD2d16wb_register:
3406 case ARM::VLD2d32wb_register:
3407 case ARM::VLD2q8wb_register:
3408 case ARM::VLD2q16wb_register:
3409 case ARM::VLD2q32wb_register:
3414 case ARM::VLD3d8_UPD:
3415 case ARM::VLD3d16_UPD:
3416 case ARM::VLD3d32_UPD:
3417 case ARM::VLD1d64Twb_fixed:
3418 case ARM::VLD1d64Twb_register:
3419 case ARM::VLD3q8_UPD:
3420 case ARM::VLD3q16_UPD:
3421 case ARM::VLD3q32_UPD:
3426 case ARM::VLD4d8_UPD:
3427 case ARM::VLD4d16_UPD:
3428 case ARM::VLD4d32_UPD:
3429 case ARM::VLD1d64Qwb_fixed:
3430 case ARM::VLD1d64Qwb_register:
3431 case ARM::VLD4q8_UPD:
3432 case ARM::VLD4q16_UPD:
3433 case ARM::VLD4q32_UPD:
3434 case ARM::VLD1DUPq8:
3435 case ARM::VLD1DUPq16:
3436 case ARM::VLD1DUPq32:
3437 case ARM::VLD1DUPq8wb_fixed:
3438 case ARM::VLD1DUPq16wb_fixed:
3439 case ARM::VLD1DUPq32wb_fixed:
3440 case ARM::VLD1DUPq8wb_register:
3441 case ARM::VLD1DUPq16wb_register:
3442 case ARM::VLD1DUPq32wb_register:
3443 case ARM::VLD2DUPd8:
3444 case ARM::VLD2DUPd16:
3445 case ARM::VLD2DUPd32:
3446 case ARM::VLD2DUPd8wb_fixed:
3447 case ARM::VLD2DUPd16wb_fixed:
3448 case ARM::VLD2DUPd32wb_fixed:
3449 case ARM::VLD2DUPd8wb_register:
3450 case ARM::VLD2DUPd16wb_register:
3451 case ARM::VLD2DUPd32wb_register:
3452 case ARM::VLD4DUPd8:
3453 case ARM::VLD4DUPd16:
3454 case ARM::VLD4DUPd32:
3455 case ARM::VLD4DUPd8_UPD:
3456 case ARM::VLD4DUPd16_UPD:
3457 case ARM::VLD4DUPd32_UPD:
3459 case ARM::VLD1LNd16:
3460 case ARM::VLD1LNd32:
3461 case ARM::VLD1LNd8_UPD:
3462 case ARM::VLD1LNd16_UPD:
3463 case ARM::VLD1LNd32_UPD:
3465 case ARM::VLD2LNd16:
3466 case ARM::VLD2LNd32:
3467 case ARM::VLD2LNq16:
3468 case ARM::VLD2LNq32:
3469 case ARM::VLD2LNd8_UPD:
3470 case ARM::VLD2LNd16_UPD:
3471 case ARM::VLD2LNd32_UPD:
3472 case ARM::VLD2LNq16_UPD:
3473 case ARM::VLD2LNq32_UPD:
3475 case ARM::VLD4LNd16:
3476 case ARM::VLD4LNd32:
3477 case ARM::VLD4LNq16:
3478 case ARM::VLD4LNq32:
3479 case ARM::VLD4LNd8_UPD:
3480 case ARM::VLD4LNd16_UPD:
3481 case ARM::VLD4LNd32_UPD:
3482 case ARM::VLD4LNq16_UPD:
3483 case ARM::VLD4LNq32_UPD:
3484 // If the address is not 64-bit aligned, the latencies of these
3485 // instructions increases by one.
3496 ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
3497 const MachineInstr *DefMI, unsigned DefIdx,
3498 const MachineInstr *UseMI,
3499 unsigned UseIdx) const {
3500 // No operand latency. The caller may fall back to getInstrLatency.
3501 if (!ItinData || ItinData->isEmpty())
3504 const MachineOperand &DefMO = DefMI->getOperand(DefIdx);
3505 unsigned Reg = DefMO.getReg();
3506 const MCInstrDesc *DefMCID = &DefMI->getDesc();
3507 const MCInstrDesc *UseMCID = &UseMI->getDesc();
3509 unsigned DefAdj = 0;
3510 if (DefMI->isBundle()) {
3511 DefMI = getBundledDefMI(&getRegisterInfo(), DefMI, Reg, DefIdx, DefAdj);
3512 DefMCID = &DefMI->getDesc();
3514 if (DefMI->isCopyLike() || DefMI->isInsertSubreg() ||
3515 DefMI->isRegSequence() || DefMI->isImplicitDef()) {
3519 unsigned UseAdj = 0;
3520 if (UseMI->isBundle()) {
3522 const MachineInstr *NewUseMI = getBundledUseMI(&getRegisterInfo(), UseMI,
3523 Reg, NewUseIdx, UseAdj);
3529 UseMCID = &UseMI->getDesc();
3532 if (Reg == ARM::CPSR) {
3533 if (DefMI->getOpcode() == ARM::FMSTAT) {
3534 // fpscr -> cpsr stalls over 20 cycles on A8 (and earlier?)
3535 return Subtarget.isLikeA9() ? 1 : 20;
3538 // CPSR set and branch can be paired in the same cycle.
3539 if (UseMI->isBranch())
3542 // Otherwise it takes the instruction latency (generally one).
3543 unsigned Latency = getInstrLatency(ItinData, DefMI);
3545 // For Thumb2 and -Os, prefer scheduling CPSR setting instruction close to
3546 // its uses. Instructions which are otherwise scheduled between them may
3547 // incur a code size penalty (not able to use the CPSR setting 16-bit
3549 if (Latency > 0 && Subtarget.isThumb2()) {
3550 const MachineFunction *MF = DefMI->getParent()->getParent();
3551 if (MF->getFunction()->getAttributes().
3552 hasAttribute(AttributeSet::FunctionIndex,
3553 Attribute::OptimizeForSize))
3559 if (DefMO.isImplicit() || UseMI->getOperand(UseIdx).isImplicit())
3562 unsigned DefAlign = DefMI->hasOneMemOperand()
3563 ? (*DefMI->memoperands_begin())->getAlignment() : 0;
3564 unsigned UseAlign = UseMI->hasOneMemOperand()
3565 ? (*UseMI->memoperands_begin())->getAlignment() : 0;
3567 // Get the itinerary's latency if possible, and handle variable_ops.
3568 int Latency = getOperandLatency(ItinData, *DefMCID, DefIdx, DefAlign,
3569 *UseMCID, UseIdx, UseAlign);
3570 // Unable to find operand latency. The caller may resort to getInstrLatency.
3574 // Adjust for IT block position.
3575 int Adj = DefAdj + UseAdj;
3577 // Adjust for dynamic def-side opcode variants not captured by the itinerary.
3578 Adj += adjustDefLatency(Subtarget, DefMI, DefMCID, DefAlign);
3579 if (Adj >= 0 || (int)Latency > -Adj) {
3580 return Latency + Adj;
3582 // Return the itinerary latency, which may be zero but not less than zero.
3587 ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
3588 SDNode *DefNode, unsigned DefIdx,
3589 SDNode *UseNode, unsigned UseIdx) const {
3590 if (!DefNode->isMachineOpcode())
3593 const MCInstrDesc &DefMCID = get(DefNode->getMachineOpcode());
3595 if (isZeroCost(DefMCID.Opcode))
3598 if (!ItinData || ItinData->isEmpty())
3599 return DefMCID.mayLoad() ? 3 : 1;
3601 if (!UseNode->isMachineOpcode()) {
3602 int Latency = ItinData->getOperandCycle(DefMCID.getSchedClass(), DefIdx);
3603 if (Subtarget.isLikeA9() || Subtarget.isSwift())
3604 return Latency <= 2 ? 1 : Latency - 1;
3606 return Latency <= 3 ? 1 : Latency - 2;
3609 const MCInstrDesc &UseMCID = get(UseNode->getMachineOpcode());
3610 const MachineSDNode *DefMN = dyn_cast<MachineSDNode>(DefNode);
3611 unsigned DefAlign = !DefMN->memoperands_empty()
3612 ? (*DefMN->memoperands_begin())->getAlignment() : 0;
3613 const MachineSDNode *UseMN = dyn_cast<MachineSDNode>(UseNode);
3614 unsigned UseAlign = !UseMN->memoperands_empty()
3615 ? (*UseMN->memoperands_begin())->getAlignment() : 0;
3616 int Latency = getOperandLatency(ItinData, DefMCID, DefIdx, DefAlign,
3617 UseMCID, UseIdx, UseAlign);
3620 (Subtarget.isCortexA8() || Subtarget.isLikeA9() ||
3621 Subtarget.isCortexA7())) {
3622 // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2]
3623 // variants are one cycle cheaper.
3624 switch (DefMCID.getOpcode()) {
3629 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
3630 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3632 (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
3639 case ARM::t2LDRSHs: {
3640 // Thumb2 mode: lsl only.
3642 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
3643 if (ShAmt == 0 || ShAmt == 2)
3648 } else if (DefIdx == 0 && Latency > 2 && Subtarget.isSwift()) {
3649 // FIXME: Properly handle all of the latency adjustments for address
3651 switch (DefMCID.getOpcode()) {
3656 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
3657 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3659 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
3660 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
3662 else if (ShImm == 1 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsr)
3669 case ARM::t2LDRSHs: {
3670 // Thumb2 mode: lsl 0-3 only.
3677 if (DefAlign < 8 && Subtarget.isLikeA9())
3678 switch (DefMCID.getOpcode()) {
3684 case ARM::VLD1q8wb_register:
3685 case ARM::VLD1q16wb_register:
3686 case ARM::VLD1q32wb_register:
3687 case ARM::VLD1q64wb_register:
3688 case ARM::VLD1q8wb_fixed:
3689 case ARM::VLD1q16wb_fixed:
3690 case ARM::VLD1q32wb_fixed:
3691 case ARM::VLD1q64wb_fixed:
3695 case ARM::VLD2q8Pseudo:
3696 case ARM::VLD2q16Pseudo:
3697 case ARM::VLD2q32Pseudo:
3698 case ARM::VLD2d8wb_fixed:
3699 case ARM::VLD2d16wb_fixed:
3700 case ARM::VLD2d32wb_fixed:
3701 case ARM::VLD2q8PseudoWB_fixed:
3702 case ARM::VLD2q16PseudoWB_fixed:
3703 case ARM::VLD2q32PseudoWB_fixed:
3704 case ARM::VLD2d8wb_register:
3705 case ARM::VLD2d16wb_register:
3706 case ARM::VLD2d32wb_register:
3707 case ARM::VLD2q8PseudoWB_register:
3708 case ARM::VLD2q16PseudoWB_register:
3709 case ARM::VLD2q32PseudoWB_register:
3710 case ARM::VLD3d8Pseudo:
3711 case ARM::VLD3d16Pseudo:
3712 case ARM::VLD3d32Pseudo:
3713 case ARM::VLD1d64TPseudo:
3714 case ARM::VLD1d64TPseudoWB_fixed:
3715 case ARM::VLD3d8Pseudo_UPD:
3716 case ARM::VLD3d16Pseudo_UPD:
3717 case ARM::VLD3d32Pseudo_UPD:
3718 case ARM::VLD3q8Pseudo_UPD:
3719 case ARM::VLD3q16Pseudo_UPD:
3720 case ARM::VLD3q32Pseudo_UPD:
3721 case ARM::VLD3q8oddPseudo:
3722 case ARM::VLD3q16oddPseudo:
3723 case ARM::VLD3q32oddPseudo:
3724 case ARM::VLD3q8oddPseudo_UPD:
3725 case ARM::VLD3q16oddPseudo_UPD:
3726 case ARM::VLD3q32oddPseudo_UPD:
3727 case ARM::VLD4d8Pseudo:
3728 case ARM::VLD4d16Pseudo:
3729 case ARM::VLD4d32Pseudo:
3730 case ARM::VLD1d64QPseudo:
3731 case ARM::VLD1d64QPseudoWB_fixed:
3732 case ARM::VLD4d8Pseudo_UPD:
3733 case ARM::VLD4d16Pseudo_UPD:
3734 case ARM::VLD4d32Pseudo_UPD:
3735 case ARM::VLD4q8Pseudo_UPD:
3736 case ARM::VLD4q16Pseudo_UPD:
3737 case ARM::VLD4q32Pseudo_UPD:
3738 case ARM::VLD4q8oddPseudo:
3739 case ARM::VLD4q16oddPseudo:
3740 case ARM::VLD4q32oddPseudo:
3741 case ARM::VLD4q8oddPseudo_UPD:
3742 case ARM::VLD4q16oddPseudo_UPD:
3743 case ARM::VLD4q32oddPseudo_UPD:
3744 case ARM::VLD1DUPq8:
3745 case ARM::VLD1DUPq16:
3746 case ARM::VLD1DUPq32:
3747 case ARM::VLD1DUPq8wb_fixed:
3748 case ARM::VLD1DUPq16wb_fixed:
3749 case ARM::VLD1DUPq32wb_fixed:
3750 case ARM::VLD1DUPq8wb_register:
3751 case ARM::VLD1DUPq16wb_register:
3752 case ARM::VLD1DUPq32wb_register:
3753 case ARM::VLD2DUPd8:
3754 case ARM::VLD2DUPd16:
3755 case ARM::VLD2DUPd32:
3756 case ARM::VLD2DUPd8wb_fixed:
3757 case ARM::VLD2DUPd16wb_fixed:
3758 case ARM::VLD2DUPd32wb_fixed:
3759 case ARM::VLD2DUPd8wb_register:
3760 case ARM::VLD2DUPd16wb_register:
3761 case ARM::VLD2DUPd32wb_register:
3762 case ARM::VLD4DUPd8Pseudo:
3763 case ARM::VLD4DUPd16Pseudo:
3764 case ARM::VLD4DUPd32Pseudo:
3765 case ARM::VLD4DUPd8Pseudo_UPD:
3766 case ARM::VLD4DUPd16Pseudo_UPD:
3767 case ARM::VLD4DUPd32Pseudo_UPD:
3768 case ARM::VLD1LNq8Pseudo:
3769 case ARM::VLD1LNq16Pseudo:
3770 case ARM::VLD1LNq32Pseudo:
3771 case ARM::VLD1LNq8Pseudo_UPD:
3772 case ARM::VLD1LNq16Pseudo_UPD:
3773 case ARM::VLD1LNq32Pseudo_UPD:
3774 case ARM::VLD2LNd8Pseudo:
3775 case ARM::VLD2LNd16Pseudo:
3776 case ARM::VLD2LNd32Pseudo:
3777 case ARM::VLD2LNq16Pseudo:
3778 case ARM::VLD2LNq32Pseudo:
3779 case ARM::VLD2LNd8Pseudo_UPD:
3780 case ARM::VLD2LNd16Pseudo_UPD:
3781 case ARM::VLD2LNd32Pseudo_UPD:
3782 case ARM::VLD2LNq16Pseudo_UPD:
3783 case ARM::VLD2LNq32Pseudo_UPD:
3784 case ARM::VLD4LNd8Pseudo:
3785 case ARM::VLD4LNd16Pseudo:
3786 case ARM::VLD4LNd32Pseudo:
3787 case ARM::VLD4LNq16Pseudo:
3788 case ARM::VLD4LNq32Pseudo:
3789 case ARM::VLD4LNd8Pseudo_UPD:
3790 case ARM::VLD4LNd16Pseudo_UPD:
3791 case ARM::VLD4LNd32Pseudo_UPD:
3792 case ARM::VLD4LNq16Pseudo_UPD:
3793 case ARM::VLD4LNq32Pseudo_UPD:
3794 // If the address is not 64-bit aligned, the latencies of these
3795 // instructions increases by one.
3803 unsigned ARMBaseInstrInfo::getPredicationCost(const MachineInstr *MI) const {
3804 if (MI->isCopyLike() || MI->isInsertSubreg() ||
3805 MI->isRegSequence() || MI->isImplicitDef())
3811 const MCInstrDesc &MCID = MI->getDesc();
3813 if (MCID.isCall() || MCID.hasImplicitDefOfPhysReg(ARM::CPSR)) {
3814 // When predicated, CPSR is an additional source operand for CPSR updating
3815 // instructions, this apparently increases their latencies.
3821 unsigned ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
3822 const MachineInstr *MI,
3823 unsigned *PredCost) const {
3824 if (MI->isCopyLike() || MI->isInsertSubreg() ||
3825 MI->isRegSequence() || MI->isImplicitDef())
3828 // An instruction scheduler typically runs on unbundled instructions, however
3829 // other passes may query the latency of a bundled instruction.
3830 if (MI->isBundle()) {
3831 unsigned Latency = 0;
3832 MachineBasicBlock::const_instr_iterator I = MI;
3833 MachineBasicBlock::const_instr_iterator E = MI->getParent()->instr_end();
3834 while (++I != E && I->isInsideBundle()) {
3835 if (I->getOpcode() != ARM::t2IT)
3836 Latency += getInstrLatency(ItinData, I, PredCost);
3841 const MCInstrDesc &MCID = MI->getDesc();
3842 if (PredCost && (MCID.isCall() || MCID.hasImplicitDefOfPhysReg(ARM::CPSR))) {
3843 // When predicated, CPSR is an additional source operand for CPSR updating
3844 // instructions, this apparently increases their latencies.
3847 // Be sure to call getStageLatency for an empty itinerary in case it has a
3848 // valid MinLatency property.
3850 return MI->mayLoad() ? 3 : 1;
3852 unsigned Class = MCID.getSchedClass();
3854 // For instructions with variable uops, use uops as latency.
3855 if (!ItinData->isEmpty() && ItinData->getNumMicroOps(Class) < 0)
3856 return getNumMicroOps(ItinData, MI);
3858 // For the common case, fall back on the itinerary's latency.
3859 unsigned Latency = ItinData->getStageLatency(Class);
3861 // Adjust for dynamic def-side opcode variants not captured by the itinerary.
3862 unsigned DefAlign = MI->hasOneMemOperand()
3863 ? (*MI->memoperands_begin())->getAlignment() : 0;
3864 int Adj = adjustDefLatency(Subtarget, MI, &MCID, DefAlign);
3865 if (Adj >= 0 || (int)Latency > -Adj) {
3866 return Latency + Adj;
3871 int ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
3872 SDNode *Node) const {
3873 if (!Node->isMachineOpcode())
3876 if (!ItinData || ItinData->isEmpty())
3879 unsigned Opcode = Node->getMachineOpcode();
3882 return ItinData->getStageLatency(get(Opcode).getSchedClass());
3889 bool ARMBaseInstrInfo::
3890 hasHighOperandLatency(const InstrItineraryData *ItinData,
3891 const MachineRegisterInfo *MRI,
3892 const MachineInstr *DefMI, unsigned DefIdx,
3893 const MachineInstr *UseMI, unsigned UseIdx) const {
3894 unsigned DDomain = DefMI->getDesc().TSFlags & ARMII::DomainMask;
3895 unsigned UDomain = UseMI->getDesc().TSFlags & ARMII::DomainMask;
3896 if (Subtarget.isCortexA8() &&
3897 (DDomain == ARMII::DomainVFP || UDomain == ARMII::DomainVFP))
3898 // CortexA8 VFP instructions are not pipelined.
3901 // Hoist VFP / NEON instructions with 4 or higher latency.
3902 int Latency = computeOperandLatency(ItinData, DefMI, DefIdx, UseMI, UseIdx);
3904 Latency = getInstrLatency(ItinData, DefMI);
3907 return DDomain == ARMII::DomainVFP || DDomain == ARMII::DomainNEON ||
3908 UDomain == ARMII::DomainVFP || UDomain == ARMII::DomainNEON;
3911 bool ARMBaseInstrInfo::
3912 hasLowDefLatency(const InstrItineraryData *ItinData,
3913 const MachineInstr *DefMI, unsigned DefIdx) const {
3914 if (!ItinData || ItinData->isEmpty())
3917 unsigned DDomain = DefMI->getDesc().TSFlags & ARMII::DomainMask;
3918 if (DDomain == ARMII::DomainGeneral) {
3919 unsigned DefClass = DefMI->getDesc().getSchedClass();
3920 int DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
3921 return (DefCycle != -1 && DefCycle <= 2);
3926 bool ARMBaseInstrInfo::verifyInstruction(const MachineInstr *MI,
3927 StringRef &ErrInfo) const {
3928 if (convertAddSubFlagsOpcode(MI->getOpcode())) {
3929 ErrInfo = "Pseudo flag setting opcodes only exist in Selection DAG";
3936 ARMBaseInstrInfo::isFpMLxInstruction(unsigned Opcode, unsigned &MulOpc,
3937 unsigned &AddSubOpc,
3938 bool &NegAcc, bool &HasLane) const {
3939 DenseMap<unsigned, unsigned>::const_iterator I = MLxEntryMap.find(Opcode);
3940 if (I == MLxEntryMap.end())
3943 const ARM_MLxEntry &Entry = ARM_MLxTable[I->second];
3944 MulOpc = Entry.MulOpc;
3945 AddSubOpc = Entry.AddSubOpc;
3946 NegAcc = Entry.NegAcc;
3947 HasLane = Entry.HasLane;
3951 //===----------------------------------------------------------------------===//
3952 // Execution domains.
3953 //===----------------------------------------------------------------------===//
3955 // Some instructions go down the NEON pipeline, some go down the VFP pipeline,
3956 // and some can go down both. The vmov instructions go down the VFP pipeline,
3957 // but they can be changed to vorr equivalents that are executed by the NEON
3960 // We use the following execution domain numbering:
3968 // Also see ARMInstrFormats.td and Domain* enums in ARMBaseInfo.h
3970 std::pair<uint16_t, uint16_t>
3971 ARMBaseInstrInfo::getExecutionDomain(const MachineInstr *MI) const {
3972 // VMOVD, VMOVRS and VMOVSR are VFP instructions, but can be changed to NEON
3973 // if they are not predicated.
3974 if (MI->getOpcode() == ARM::VMOVD && !isPredicated(MI))
3975 return std::make_pair(ExeVFP, (1<<ExeVFP) | (1<<ExeNEON));
3977 // CortexA9 is particularly picky about mixing the two and wants these
3979 if (Subtarget.isCortexA9() && !isPredicated(MI) &&
3980 (MI->getOpcode() == ARM::VMOVRS ||
3981 MI->getOpcode() == ARM::VMOVSR ||
3982 MI->getOpcode() == ARM::VMOVS))
3983 return std::make_pair(ExeVFP, (1<<ExeVFP) | (1<<ExeNEON));
3985 // No other instructions can be swizzled, so just determine their domain.
3986 unsigned Domain = MI->getDesc().TSFlags & ARMII::DomainMask;
3988 if (Domain & ARMII::DomainNEON)
3989 return std::make_pair(ExeNEON, 0);
3991 // Certain instructions can go either way on Cortex-A8.
3992 // Treat them as NEON instructions.
3993 if ((Domain & ARMII::DomainNEONA8) && Subtarget.isCortexA8())
3994 return std::make_pair(ExeNEON, 0);
3996 if (Domain & ARMII::DomainVFP)
3997 return std::make_pair(ExeVFP, 0);
3999 return std::make_pair(ExeGeneric, 0);
4002 static unsigned getCorrespondingDRegAndLane(const TargetRegisterInfo *TRI,
4003 unsigned SReg, unsigned &Lane) {
4004 unsigned DReg = TRI->getMatchingSuperReg(SReg, ARM::ssub_0, &ARM::DPRRegClass);
4007 if (DReg != ARM::NoRegister)
4011 DReg = TRI->getMatchingSuperReg(SReg, ARM::ssub_1, &ARM::DPRRegClass);
4013 assert(DReg && "S-register with no D super-register?");
4017 /// getImplicitSPRUseForDPRUse - Given a use of a DPR register and lane,
4018 /// set ImplicitSReg to a register number that must be marked as implicit-use or
4019 /// zero if no register needs to be defined as implicit-use.
4021 /// If the function cannot determine if an SPR should be marked implicit use or
4022 /// not, it returns false.
4024 /// This function handles cases where an instruction is being modified from taking
4025 /// an SPR to a DPR[Lane]. A use of the DPR is being added, which may conflict
4026 /// with an earlier def of an SPR corresponding to DPR[Lane^1] (i.e. the other
4027 /// lane of the DPR).
4029 /// If the other SPR is defined, an implicit-use of it should be added. Else,
4030 /// (including the case where the DPR itself is defined), it should not.
4032 static bool getImplicitSPRUseForDPRUse(const TargetRegisterInfo *TRI,
4034 unsigned DReg, unsigned Lane,
4035 unsigned &ImplicitSReg) {
4036 // If the DPR is defined or used already, the other SPR lane will be chained
4037 // correctly, so there is nothing to be done.
4038 if (MI->definesRegister(DReg, TRI) || MI->readsRegister(DReg, TRI)) {
4043 // Otherwise we need to go searching to see if the SPR is set explicitly.
4044 ImplicitSReg = TRI->getSubReg(DReg,
4045 (Lane & 1) ? ARM::ssub_0 : ARM::ssub_1);
4046 MachineBasicBlock::LivenessQueryResult LQR =
4047 MI->getParent()->computeRegisterLiveness(TRI, ImplicitSReg, MI);
4049 if (LQR == MachineBasicBlock::LQR_Live)
4051 else if (LQR == MachineBasicBlock::LQR_Unknown)
4054 // If the register is known not to be live, there is no need to add an
4061 ARMBaseInstrInfo::setExecutionDomain(MachineInstr *MI, unsigned Domain) const {
4062 unsigned DstReg, SrcReg, DReg;
4064 MachineInstrBuilder MIB(*MI->getParent()->getParent(), MI);
4065 const TargetRegisterInfo *TRI = &getRegisterInfo();
4066 switch (MI->getOpcode()) {
4068 llvm_unreachable("cannot handle opcode!");
4071 if (Domain != ExeNEON)
4074 // Zap the predicate operands.
4075 assert(!isPredicated(MI) && "Cannot predicate a VORRd");
4077 // Source instruction is %DDst = VMOVD %DSrc, 14, %noreg (; implicits)
4078 DstReg = MI->getOperand(0).getReg();
4079 SrcReg = MI->getOperand(1).getReg();
4081 for (unsigned i = MI->getDesc().getNumOperands(); i; --i)
4082 MI->RemoveOperand(i-1);
4084 // Change to a %DDst = VORRd %DSrc, %DSrc, 14, %noreg (; implicits)
4085 MI->setDesc(get(ARM::VORRd));
4086 AddDefaultPred(MIB.addReg(DstReg, RegState::Define)
4091 if (Domain != ExeNEON)
4093 assert(!isPredicated(MI) && "Cannot predicate a VGETLN");
4095 // Source instruction is %RDst = VMOVRS %SSrc, 14, %noreg (; implicits)
4096 DstReg = MI->getOperand(0).getReg();
4097 SrcReg = MI->getOperand(1).getReg();
4099 for (unsigned i = MI->getDesc().getNumOperands(); i; --i)
4100 MI->RemoveOperand(i-1);
4102 DReg = getCorrespondingDRegAndLane(TRI, SrcReg, Lane);
4104 // Convert to %RDst = VGETLNi32 %DSrc, Lane, 14, %noreg (; imps)
4105 // Note that DSrc has been widened and the other lane may be undef, which
4106 // contaminates the entire register.
4107 MI->setDesc(get(ARM::VGETLNi32));
4108 AddDefaultPred(MIB.addReg(DstReg, RegState::Define)
4109 .addReg(DReg, RegState::Undef)
4112 // The old source should be an implicit use, otherwise we might think it
4113 // was dead before here.
4114 MIB.addReg(SrcReg, RegState::Implicit);
4117 if (Domain != ExeNEON)
4119 assert(!isPredicated(MI) && "Cannot predicate a VSETLN");
4121 // Source instruction is %SDst = VMOVSR %RSrc, 14, %noreg (; implicits)
4122 DstReg = MI->getOperand(0).getReg();
4123 SrcReg = MI->getOperand(1).getReg();
4125 DReg = getCorrespondingDRegAndLane(TRI, DstReg, Lane);
4127 unsigned ImplicitSReg;
4128 if (!getImplicitSPRUseForDPRUse(TRI, MI, DReg, Lane, ImplicitSReg))
4131 for (unsigned i = MI->getDesc().getNumOperands(); i; --i)
4132 MI->RemoveOperand(i-1);
4134 // Convert to %DDst = VSETLNi32 %DDst, %RSrc, Lane, 14, %noreg (; imps)
4135 // Again DDst may be undefined at the beginning of this instruction.
4136 MI->setDesc(get(ARM::VSETLNi32));
4137 MIB.addReg(DReg, RegState::Define)
4138 .addReg(DReg, getUndefRegState(!MI->readsRegister(DReg, TRI)))
4141 AddDefaultPred(MIB);
4143 // The narrower destination must be marked as set to keep previous chains
4145 MIB.addReg(DstReg, RegState::Define | RegState::Implicit);
4146 if (ImplicitSReg != 0)
4147 MIB.addReg(ImplicitSReg, RegState::Implicit);
4151 if (Domain != ExeNEON)
4154 // Source instruction is %SDst = VMOVS %SSrc, 14, %noreg (; implicits)
4155 DstReg = MI->getOperand(0).getReg();
4156 SrcReg = MI->getOperand(1).getReg();
4158 unsigned DstLane = 0, SrcLane = 0, DDst, DSrc;
4159 DDst = getCorrespondingDRegAndLane(TRI, DstReg, DstLane);
4160 DSrc = getCorrespondingDRegAndLane(TRI, SrcReg, SrcLane);
4162 unsigned ImplicitSReg;
4163 if (!getImplicitSPRUseForDPRUse(TRI, MI, DSrc, SrcLane, ImplicitSReg))
4166 for (unsigned i = MI->getDesc().getNumOperands(); i; --i)
4167 MI->RemoveOperand(i-1);
4170 // Destination can be:
4171 // %DDst = VDUPLN32d %DDst, Lane, 14, %noreg (; implicits)
4172 MI->setDesc(get(ARM::VDUPLN32d));
4173 MIB.addReg(DDst, RegState::Define)
4174 .addReg(DDst, getUndefRegState(!MI->readsRegister(DDst, TRI)))
4176 AddDefaultPred(MIB);
4178 // Neither the source or the destination are naturally represented any
4179 // more, so add them in manually.
4180 MIB.addReg(DstReg, RegState::Implicit | RegState::Define);
4181 MIB.addReg(SrcReg, RegState::Implicit);
4182 if (ImplicitSReg != 0)
4183 MIB.addReg(ImplicitSReg, RegState::Implicit);
4187 // In general there's no single instruction that can perform an S <-> S
4188 // move in NEON space, but a pair of VEXT instructions *can* do the
4189 // job. It turns out that the VEXTs needed will only use DSrc once, with
4190 // the position based purely on the combination of lane-0 and lane-1
4191 // involved. For example
4192 // vmov s0, s2 -> vext.32 d0, d0, d1, #1 vext.32 d0, d0, d0, #1
4193 // vmov s1, s3 -> vext.32 d0, d1, d0, #1 vext.32 d0, d0, d0, #1
4194 // vmov s0, s3 -> vext.32 d0, d0, d0, #1 vext.32 d0, d1, d0, #1
4195 // vmov s1, s2 -> vext.32 d0, d0, d0, #1 vext.32 d0, d0, d1, #1
4197 // Pattern of the MachineInstrs is:
4198 // %DDst = VEXTd32 %DSrc1, %DSrc2, Lane, 14, %noreg (;implicits)
4199 MachineInstrBuilder NewMIB;
4200 NewMIB = BuildMI(*MI->getParent(), MI, MI->getDebugLoc(),
4201 get(ARM::VEXTd32), DDst);
4203 // On the first instruction, both DSrc and DDst may be <undef> if present.
4204 // Specifically when the original instruction didn't have them as an
4206 unsigned CurReg = SrcLane == 1 && DstLane == 1 ? DSrc : DDst;
4207 bool CurUndef = !MI->readsRegister(CurReg, TRI);
4208 NewMIB.addReg(CurReg, getUndefRegState(CurUndef));
4210 CurReg = SrcLane == 0 && DstLane == 0 ? DSrc : DDst;
4211 CurUndef = !MI->readsRegister(CurReg, TRI);
4212 NewMIB.addReg(CurReg, getUndefRegState(CurUndef));
4215 AddDefaultPred(NewMIB);
4217 if (SrcLane == DstLane)
4218 NewMIB.addReg(SrcReg, RegState::Implicit);
4220 MI->setDesc(get(ARM::VEXTd32));
4221 MIB.addReg(DDst, RegState::Define);
4223 // On the second instruction, DDst has definitely been defined above, so
4224 // it is not <undef>. DSrc, if present, can be <undef> as above.
4225 CurReg = SrcLane == 1 && DstLane == 0 ? DSrc : DDst;
4226 CurUndef = CurReg == DSrc && !MI->readsRegister(CurReg, TRI);
4227 MIB.addReg(CurReg, getUndefRegState(CurUndef));
4229 CurReg = SrcLane == 0 && DstLane == 1 ? DSrc : DDst;
4230 CurUndef = CurReg == DSrc && !MI->readsRegister(CurReg, TRI);
4231 MIB.addReg(CurReg, getUndefRegState(CurUndef));
4234 AddDefaultPred(MIB);
4236 if (SrcLane != DstLane)
4237 MIB.addReg(SrcReg, RegState::Implicit);
4239 // As before, the original destination is no longer represented, add it
4241 MIB.addReg(DstReg, RegState::Define | RegState::Implicit);
4242 if (ImplicitSReg != 0)
4243 MIB.addReg(ImplicitSReg, RegState::Implicit);
4250 //===----------------------------------------------------------------------===//
4251 // Partial register updates
4252 //===----------------------------------------------------------------------===//
4254 // Swift renames NEON registers with 64-bit granularity. That means any
4255 // instruction writing an S-reg implicitly reads the containing D-reg. The
4256 // problem is mostly avoided by translating f32 operations to v2f32 operations
4257 // on D-registers, but f32 loads are still a problem.
4259 // These instructions can load an f32 into a NEON register:
4261 // VLDRS - Only writes S, partial D update.
4262 // VLD1LNd32 - Writes all D-regs, explicit partial D update, 2 uops.
4263 // VLD1DUPd32 - Writes all D-regs, no partial reg update, 2 uops.
4265 // FCONSTD can be used as a dependency-breaking instruction.
4266 unsigned ARMBaseInstrInfo::
4267 getPartialRegUpdateClearance(const MachineInstr *MI,
4269 const TargetRegisterInfo *TRI) const {
4270 if (!SwiftPartialUpdateClearance ||
4271 !(Subtarget.isSwift() || Subtarget.isCortexA15()))
4274 assert(TRI && "Need TRI instance");
4276 const MachineOperand &MO = MI->getOperand(OpNum);
4279 unsigned Reg = MO.getReg();
4282 switch(MI->getOpcode()) {
4283 // Normal instructions writing only an S-register.
4288 case ARM::VMOVv4i16:
4289 case ARM::VMOVv2i32:
4290 case ARM::VMOVv2f32:
4291 case ARM::VMOVv1i64:
4292 UseOp = MI->findRegisterUseOperandIdx(Reg, false, TRI);
4295 // Explicitly reads the dependency.
4296 case ARM::VLD1LNd32:
4303 // If this instruction actually reads a value from Reg, there is no unwanted
4305 if (UseOp != -1 && MI->getOperand(UseOp).readsReg())
4308 // We must be able to clobber the whole D-reg.
4309 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
4310 // Virtual register must be a foo:ssub_0<def,undef> operand.
4311 if (!MO.getSubReg() || MI->readsVirtualRegister(Reg))
4313 } else if (ARM::SPRRegClass.contains(Reg)) {
4314 // Physical register: MI must define the full D-reg.
4315 unsigned DReg = TRI->getMatchingSuperReg(Reg, ARM::ssub_0,
4317 if (!DReg || !MI->definesRegister(DReg, TRI))
4321 // MI has an unwanted D-register dependency.
4322 // Avoid defs in the previous N instructrions.
4323 return SwiftPartialUpdateClearance;
4326 // Break a partial register dependency after getPartialRegUpdateClearance
4327 // returned non-zero.
4328 void ARMBaseInstrInfo::
4329 breakPartialRegDependency(MachineBasicBlock::iterator MI,
4331 const TargetRegisterInfo *TRI) const {
4332 assert(MI && OpNum < MI->getDesc().getNumDefs() && "OpNum is not a def");
4333 assert(TRI && "Need TRI instance");
4335 const MachineOperand &MO = MI->getOperand(OpNum);
4336 unsigned Reg = MO.getReg();
4337 assert(TargetRegisterInfo::isPhysicalRegister(Reg) &&
4338 "Can't break virtual register dependencies.");
4339 unsigned DReg = Reg;
4341 // If MI defines an S-reg, find the corresponding D super-register.
4342 if (ARM::SPRRegClass.contains(Reg)) {
4343 DReg = ARM::D0 + (Reg - ARM::S0) / 2;
4344 assert(TRI->isSuperRegister(Reg, DReg) && "Register enums broken");
4347 assert(ARM::DPRRegClass.contains(DReg) && "Can only break D-reg deps");
4348 assert(MI->definesRegister(DReg, TRI) && "MI doesn't clobber full D-reg");
4350 // FIXME: In some cases, VLDRS can be changed to a VLD1DUPd32 which defines
4351 // the full D-register by loading the same value to both lanes. The
4352 // instruction is micro-coded with 2 uops, so don't do this until we can
4353 // properly schedule micro-coded instructions. The dispatcher stalls cause
4354 // too big regressions.
4356 // Insert the dependency-breaking FCONSTD before MI.
4357 // 96 is the encoding of 0.5, but the actual value doesn't matter here.
4358 AddDefaultPred(BuildMI(*MI->getParent(), MI, MI->getDebugLoc(),
4359 get(ARM::FCONSTD), DReg).addImm(96));
4360 MI->addRegisterKilled(DReg, TRI, true);
4363 void ARMBaseInstrInfo::getUnconditionalBranch(
4364 MCInst &Branch, const MCSymbolRefExpr *BranchTarget) const {
4365 if (Subtarget.isThumb())
4366 Branch.setOpcode(ARM::tB);
4367 else if (Subtarget.isThumb2())
4368 Branch.setOpcode(ARM::t2B);
4370 Branch.setOpcode(ARM::Bcc);
4372 Branch.addOperand(MCOperand::CreateExpr(BranchTarget));
4373 Branch.addOperand(MCOperand::CreateImm(ARMCC::AL));
4374 Branch.addOperand(MCOperand::CreateReg(0));
4377 void ARMBaseInstrInfo::getTrap(MCInst &MI) const {
4378 if (Subtarget.isThumb())
4379 MI.setOpcode(ARM::tTRAP);
4380 else if (Subtarget.useNaClTrap())
4381 MI.setOpcode(ARM::TRAPNaCl);
4383 MI.setOpcode(ARM::TRAP);
4386 bool ARMBaseInstrInfo::hasNOP() const {
4387 return (Subtarget.getFeatureBits() & ARM::HasV6T2Ops) != 0;
4390 bool ARMBaseInstrInfo::isSwiftFastImmShift(const MachineInstr *MI) const {
4391 if (MI->getNumOperands() < 4)
4393 unsigned ShOpVal = MI->getOperand(3).getImm();
4394 unsigned ShImm = ARM_AM::getSORegOffset(ShOpVal);
4395 // Swift supports faster shifts for: lsl 2, lsl 1, and lsr 1.
4396 if ((ShImm == 1 && ARM_AM::getSORegShOp(ShOpVal) == ARM_AM::lsr) ||
4397 ((ShImm == 1 || ShImm == 2) &&
4398 ARM_AM::getSORegShOp(ShOpVal) == ARM_AM::lsl))