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"
40 #include "llvm/Support/raw_ostream.h"
44 #define DEBUG_TYPE "arm-instrinfo"
46 #define GET_INSTRINFO_CTOR_DTOR
47 #include "ARMGenInstrInfo.inc"
50 EnableARM3Addr("enable-arm-3-addr-conv", cl::Hidden,
51 cl::desc("Enable ARM 2-addr to 3-addr conv"));
54 WidenVMOVS("widen-vmovs", cl::Hidden, cl::init(true),
55 cl::desc("Widen ARM vmovs to vmovd when possible"));
57 static cl::opt<unsigned>
58 SwiftPartialUpdateClearance("swift-partial-update-clearance",
59 cl::Hidden, cl::init(12),
60 cl::desc("Clearance before partial register updates"));
62 /// ARM_MLxEntry - Record information about MLA / MLS instructions.
64 uint16_t MLxOpc; // MLA / MLS opcode
65 uint16_t MulOpc; // Expanded multiplication opcode
66 uint16_t AddSubOpc; // Expanded add / sub opcode
67 bool NegAcc; // True if the acc is negated before the add / sub.
68 bool HasLane; // True if instruction has an extra "lane" operand.
71 static const ARM_MLxEntry ARM_MLxTable[] = {
72 // MLxOpc, MulOpc, AddSubOpc, NegAcc, HasLane
74 { ARM::VMLAS, ARM::VMULS, ARM::VADDS, false, false },
75 { ARM::VMLSS, ARM::VMULS, ARM::VSUBS, false, false },
76 { ARM::VMLAD, ARM::VMULD, ARM::VADDD, false, false },
77 { ARM::VMLSD, ARM::VMULD, ARM::VSUBD, false, false },
78 { ARM::VNMLAS, ARM::VNMULS, ARM::VSUBS, true, false },
79 { ARM::VNMLSS, ARM::VMULS, ARM::VSUBS, true, false },
80 { ARM::VNMLAD, ARM::VNMULD, ARM::VSUBD, true, false },
81 { ARM::VNMLSD, ARM::VMULD, ARM::VSUBD, true, false },
84 { ARM::VMLAfd, ARM::VMULfd, ARM::VADDfd, false, false },
85 { ARM::VMLSfd, ARM::VMULfd, ARM::VSUBfd, false, false },
86 { ARM::VMLAfq, ARM::VMULfq, ARM::VADDfq, false, false },
87 { ARM::VMLSfq, ARM::VMULfq, ARM::VSUBfq, false, false },
88 { ARM::VMLAslfd, ARM::VMULslfd, ARM::VADDfd, false, true },
89 { ARM::VMLSslfd, ARM::VMULslfd, ARM::VSUBfd, false, true },
90 { ARM::VMLAslfq, ARM::VMULslfq, ARM::VADDfq, false, true },
91 { ARM::VMLSslfq, ARM::VMULslfq, ARM::VSUBfq, false, true },
94 ARMBaseInstrInfo::ARMBaseInstrInfo(const ARMSubtarget& STI)
95 : ARMGenInstrInfo(ARM::ADJCALLSTACKDOWN, ARM::ADJCALLSTACKUP),
97 for (unsigned i = 0, e = array_lengthof(ARM_MLxTable); i != e; ++i) {
98 if (!MLxEntryMap.insert(std::make_pair(ARM_MLxTable[i].MLxOpc, i)).second)
99 assert(false && "Duplicated entries?");
100 MLxHazardOpcodes.insert(ARM_MLxTable[i].AddSubOpc);
101 MLxHazardOpcodes.insert(ARM_MLxTable[i].MulOpc);
105 // Use a ScoreboardHazardRecognizer for prepass ARM scheduling. TargetInstrImpl
106 // currently defaults to no prepass hazard recognizer.
107 ScheduleHazardRecognizer *
108 ARMBaseInstrInfo::CreateTargetHazardRecognizer(const TargetSubtargetInfo *STI,
109 const ScheduleDAG *DAG) const {
110 if (usePreRAHazardRecognizer()) {
111 const InstrItineraryData *II =
112 static_cast<const ARMSubtarget *>(STI)->getInstrItineraryData();
113 return new ScoreboardHazardRecognizer(II, DAG, "pre-RA-sched");
115 return TargetInstrInfo::CreateTargetHazardRecognizer(STI, DAG);
118 ScheduleHazardRecognizer *ARMBaseInstrInfo::
119 CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II,
120 const ScheduleDAG *DAG) const {
121 if (Subtarget.isThumb2() || Subtarget.hasVFP2())
122 return (ScheduleHazardRecognizer *)new ARMHazardRecognizer(II, DAG);
123 return TargetInstrInfo::CreateTargetPostRAHazardRecognizer(II, DAG);
127 ARMBaseInstrInfo::convertToThreeAddress(MachineFunction::iterator &MFI,
128 MachineBasicBlock::iterator &MBBI,
129 LiveVariables *LV) const {
130 // FIXME: Thumb2 support.
135 MachineInstr *MI = MBBI;
136 MachineFunction &MF = *MI->getParent()->getParent();
137 uint64_t TSFlags = MI->getDesc().TSFlags;
139 switch ((TSFlags & ARMII::IndexModeMask) >> ARMII::IndexModeShift) {
140 default: return nullptr;
141 case ARMII::IndexModePre:
144 case ARMII::IndexModePost:
148 // Try splitting an indexed load/store to an un-indexed one plus an add/sub
150 unsigned MemOpc = getUnindexedOpcode(MI->getOpcode());
154 MachineInstr *UpdateMI = nullptr;
155 MachineInstr *MemMI = nullptr;
156 unsigned AddrMode = (TSFlags & ARMII::AddrModeMask);
157 const MCInstrDesc &MCID = MI->getDesc();
158 unsigned NumOps = MCID.getNumOperands();
159 bool isLoad = !MI->mayStore();
160 const MachineOperand &WB = isLoad ? MI->getOperand(1) : MI->getOperand(0);
161 const MachineOperand &Base = MI->getOperand(2);
162 const MachineOperand &Offset = MI->getOperand(NumOps-3);
163 unsigned WBReg = WB.getReg();
164 unsigned BaseReg = Base.getReg();
165 unsigned OffReg = Offset.getReg();
166 unsigned OffImm = MI->getOperand(NumOps-2).getImm();
167 ARMCC::CondCodes Pred = (ARMCC::CondCodes)MI->getOperand(NumOps-1).getImm();
169 default: llvm_unreachable("Unknown indexed op!");
170 case ARMII::AddrMode2: {
171 bool isSub = ARM_AM::getAM2Op(OffImm) == ARM_AM::sub;
172 unsigned Amt = ARM_AM::getAM2Offset(OffImm);
174 if (ARM_AM::getSOImmVal(Amt) == -1)
175 // Can't encode it in a so_imm operand. This transformation will
176 // add more than 1 instruction. Abandon!
178 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
179 get(isSub ? ARM::SUBri : ARM::ADDri), WBReg)
180 .addReg(BaseReg).addImm(Amt)
181 .addImm(Pred).addReg(0).addReg(0);
182 } else if (Amt != 0) {
183 ARM_AM::ShiftOpc ShOpc = ARM_AM::getAM2ShiftOpc(OffImm);
184 unsigned SOOpc = ARM_AM::getSORegOpc(ShOpc, Amt);
185 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
186 get(isSub ? ARM::SUBrsi : ARM::ADDrsi), WBReg)
187 .addReg(BaseReg).addReg(OffReg).addReg(0).addImm(SOOpc)
188 .addImm(Pred).addReg(0).addReg(0);
190 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
191 get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
192 .addReg(BaseReg).addReg(OffReg)
193 .addImm(Pred).addReg(0).addReg(0);
196 case ARMII::AddrMode3 : {
197 bool isSub = ARM_AM::getAM3Op(OffImm) == ARM_AM::sub;
198 unsigned Amt = ARM_AM::getAM3Offset(OffImm);
200 // Immediate is 8-bits. It's guaranteed to fit in a so_imm operand.
201 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
202 get(isSub ? ARM::SUBri : ARM::ADDri), WBReg)
203 .addReg(BaseReg).addImm(Amt)
204 .addImm(Pred).addReg(0).addReg(0);
206 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
207 get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
208 .addReg(BaseReg).addReg(OffReg)
209 .addImm(Pred).addReg(0).addReg(0);
214 std::vector<MachineInstr*> NewMIs;
217 MemMI = BuildMI(MF, MI->getDebugLoc(),
218 get(MemOpc), MI->getOperand(0).getReg())
219 .addReg(WBReg).addImm(0).addImm(Pred);
221 MemMI = BuildMI(MF, MI->getDebugLoc(),
222 get(MemOpc)).addReg(MI->getOperand(1).getReg())
223 .addReg(WBReg).addReg(0).addImm(0).addImm(Pred);
224 NewMIs.push_back(MemMI);
225 NewMIs.push_back(UpdateMI);
228 MemMI = BuildMI(MF, MI->getDebugLoc(),
229 get(MemOpc), MI->getOperand(0).getReg())
230 .addReg(BaseReg).addImm(0).addImm(Pred);
232 MemMI = BuildMI(MF, MI->getDebugLoc(),
233 get(MemOpc)).addReg(MI->getOperand(1).getReg())
234 .addReg(BaseReg).addReg(0).addImm(0).addImm(Pred);
236 UpdateMI->getOperand(0).setIsDead();
237 NewMIs.push_back(UpdateMI);
238 NewMIs.push_back(MemMI);
241 // Transfer LiveVariables states, kill / dead info.
243 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
244 MachineOperand &MO = MI->getOperand(i);
245 if (MO.isReg() && TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
246 unsigned Reg = MO.getReg();
248 LiveVariables::VarInfo &VI = LV->getVarInfo(Reg);
250 MachineInstr *NewMI = (Reg == WBReg) ? UpdateMI : MemMI;
252 LV->addVirtualRegisterDead(Reg, NewMI);
254 if (MO.isUse() && MO.isKill()) {
255 for (unsigned j = 0; j < 2; ++j) {
256 // Look at the two new MI's in reverse order.
257 MachineInstr *NewMI = NewMIs[j];
258 if (!NewMI->readsRegister(Reg))
260 LV->addVirtualRegisterKilled(Reg, NewMI);
261 if (VI.removeKill(MI))
262 VI.Kills.push_back(NewMI);
270 MFI->insert(MBBI, NewMIs[1]);
271 MFI->insert(MBBI, NewMIs[0]);
277 ARMBaseInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
278 MachineBasicBlock *&FBB,
279 SmallVectorImpl<MachineOperand> &Cond,
280 bool AllowModify) const {
284 MachineBasicBlock::iterator I = MBB.end();
285 if (I == MBB.begin())
286 return false; // Empty blocks are easy.
289 // Walk backwards from the end of the basic block until the branch is
290 // analyzed or we give up.
291 while (isPredicated(I) || I->isTerminator() || I->isDebugValue()) {
293 // Flag to be raised on unanalyzeable instructions. This is useful in cases
294 // where we want to clean up on the end of the basic block before we bail
296 bool CantAnalyze = false;
298 // Skip over DEBUG values and predicated nonterminators.
299 while (I->isDebugValue() || !I->isTerminator()) {
300 if (I == MBB.begin())
305 if (isIndirectBranchOpcode(I->getOpcode()) ||
306 isJumpTableBranchOpcode(I->getOpcode())) {
307 // Indirect branches and jump tables can't be analyzed, but we still want
308 // to clean up any instructions at the tail of the basic block.
310 } else if (isUncondBranchOpcode(I->getOpcode())) {
311 TBB = I->getOperand(0).getMBB();
312 } else if (isCondBranchOpcode(I->getOpcode())) {
313 // Bail out if we encounter multiple conditional branches.
317 assert(!FBB && "FBB should have been null.");
319 TBB = I->getOperand(0).getMBB();
320 Cond.push_back(I->getOperand(1));
321 Cond.push_back(I->getOperand(2));
322 } else if (I->isReturn()) {
323 // Returns can't be analyzed, but we should run cleanup.
324 CantAnalyze = !isPredicated(I);
326 // We encountered other unrecognized terminator. Bail out immediately.
330 // Cleanup code - to be run for unpredicated unconditional branches and
332 if (!isPredicated(I) &&
333 (isUncondBranchOpcode(I->getOpcode()) ||
334 isIndirectBranchOpcode(I->getOpcode()) ||
335 isJumpTableBranchOpcode(I->getOpcode()) ||
337 // Forget any previous condition branch information - it no longer applies.
341 // If we can modify the function, delete everything below this
342 // unconditional branch.
344 MachineBasicBlock::iterator DI = std::next(I);
345 while (DI != MBB.end()) {
346 MachineInstr *InstToDelete = DI;
348 InstToDelete->eraseFromParent();
356 if (I == MBB.begin())
362 // We made it past the terminators without bailing out - we must have
363 // analyzed this branch successfully.
368 unsigned ARMBaseInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
369 MachineBasicBlock::iterator I = MBB.end();
370 if (I == MBB.begin()) return 0;
372 while (I->isDebugValue()) {
373 if (I == MBB.begin())
377 if (!isUncondBranchOpcode(I->getOpcode()) &&
378 !isCondBranchOpcode(I->getOpcode()))
381 // Remove the branch.
382 I->eraseFromParent();
386 if (I == MBB.begin()) return 1;
388 if (!isCondBranchOpcode(I->getOpcode()))
391 // Remove the branch.
392 I->eraseFromParent();
397 ARMBaseInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
398 MachineBasicBlock *FBB,
399 ArrayRef<MachineOperand> Cond,
401 ARMFunctionInfo *AFI = MBB.getParent()->getInfo<ARMFunctionInfo>();
402 int BOpc = !AFI->isThumbFunction()
403 ? ARM::B : (AFI->isThumb2Function() ? ARM::t2B : ARM::tB);
404 int BccOpc = !AFI->isThumbFunction()
405 ? ARM::Bcc : (AFI->isThumb2Function() ? ARM::t2Bcc : ARM::tBcc);
406 bool isThumb = AFI->isThumbFunction() || AFI->isThumb2Function();
408 // Shouldn't be a fall through.
409 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
410 assert((Cond.size() == 2 || Cond.size() == 0) &&
411 "ARM branch conditions have two components!");
413 // For conditional branches, we use addOperand to preserve CPSR flags.
416 if (Cond.empty()) { // Unconditional branch?
418 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB).addImm(ARMCC::AL).addReg(0);
420 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB);
422 BuildMI(&MBB, DL, get(BccOpc)).addMBB(TBB)
423 .addImm(Cond[0].getImm()).addOperand(Cond[1]);
427 // Two-way conditional branch.
428 BuildMI(&MBB, DL, get(BccOpc)).addMBB(TBB)
429 .addImm(Cond[0].getImm()).addOperand(Cond[1]);
431 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB).addImm(ARMCC::AL).addReg(0);
433 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB);
437 bool ARMBaseInstrInfo::
438 ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
439 ARMCC::CondCodes CC = (ARMCC::CondCodes)(int)Cond[0].getImm();
440 Cond[0].setImm(ARMCC::getOppositeCondition(CC));
444 bool ARMBaseInstrInfo::isPredicated(const MachineInstr *MI) const {
445 if (MI->isBundle()) {
446 MachineBasicBlock::const_instr_iterator I = MI;
447 MachineBasicBlock::const_instr_iterator E = MI->getParent()->instr_end();
448 while (++I != E && I->isInsideBundle()) {
449 int PIdx = I->findFirstPredOperandIdx();
450 if (PIdx != -1 && I->getOperand(PIdx).getImm() != ARMCC::AL)
456 int PIdx = MI->findFirstPredOperandIdx();
457 return PIdx != -1 && MI->getOperand(PIdx).getImm() != ARMCC::AL;
460 bool ARMBaseInstrInfo::
461 PredicateInstruction(MachineInstr *MI, ArrayRef<MachineOperand> Pred) const {
462 unsigned Opc = MI->getOpcode();
463 if (isUncondBranchOpcode(Opc)) {
464 MI->setDesc(get(getMatchingCondBranchOpcode(Opc)));
465 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
466 .addImm(Pred[0].getImm())
467 .addReg(Pred[1].getReg());
471 int PIdx = MI->findFirstPredOperandIdx();
473 MachineOperand &PMO = MI->getOperand(PIdx);
474 PMO.setImm(Pred[0].getImm());
475 MI->getOperand(PIdx+1).setReg(Pred[1].getReg());
481 bool ARMBaseInstrInfo::SubsumesPredicate(ArrayRef<MachineOperand> Pred1,
482 ArrayRef<MachineOperand> Pred2) const {
483 if (Pred1.size() > 2 || Pred2.size() > 2)
486 ARMCC::CondCodes CC1 = (ARMCC::CondCodes)Pred1[0].getImm();
487 ARMCC::CondCodes CC2 = (ARMCC::CondCodes)Pred2[0].getImm();
497 return CC2 == ARMCC::HI;
499 return CC2 == ARMCC::LO || CC2 == ARMCC::EQ;
501 return CC2 == ARMCC::GT;
503 return CC2 == ARMCC::LT;
507 bool ARMBaseInstrInfo::DefinesPredicate(MachineInstr *MI,
508 std::vector<MachineOperand> &Pred) const {
510 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
511 const MachineOperand &MO = MI->getOperand(i);
512 if ((MO.isRegMask() && MO.clobbersPhysReg(ARM::CPSR)) ||
513 (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR)) {
522 static bool isCPSRDefined(const MachineInstr *MI) {
523 for (const auto &MO : MI->operands())
524 if (MO.isReg() && MO.getReg() == ARM::CPSR && MO.isDef())
529 static bool isEligibleForITBlock(const MachineInstr *MI) {
530 switch (MI->getOpcode()) {
531 default: return true;
532 case ARM::tADC: // ADC (register) T1
533 case ARM::tADDi3: // ADD (immediate) T1
534 case ARM::tADDi8: // ADD (immediate) T2
535 case ARM::tADDrr: // ADD (register) T1
536 case ARM::tAND: // AND (register) T1
537 case ARM::tASRri: // ASR (immediate) T1
538 case ARM::tASRrr: // ASR (register) T1
539 case ARM::tBIC: // BIC (register) T1
540 case ARM::tEOR: // EOR (register) T1
541 case ARM::tLSLri: // LSL (immediate) T1
542 case ARM::tLSLrr: // LSL (register) T1
543 case ARM::tLSRri: // LSR (immediate) T1
544 case ARM::tLSRrr: // LSR (register) T1
545 case ARM::tMUL: // MUL T1
546 case ARM::tMVN: // MVN (register) T1
547 case ARM::tORR: // ORR (register) T1
548 case ARM::tROR: // ROR (register) T1
549 case ARM::tRSB: // RSB (immediate) T1
550 case ARM::tSBC: // SBC (register) T1
551 case ARM::tSUBi3: // SUB (immediate) T1
552 case ARM::tSUBi8: // SUB (immediate) T2
553 case ARM::tSUBrr: // SUB (register) T1
554 return !isCPSRDefined(MI);
558 /// isPredicable - Return true if the specified instruction can be predicated.
559 /// By default, this returns true for every instruction with a
560 /// PredicateOperand.
561 bool ARMBaseInstrInfo::isPredicable(MachineInstr *MI) const {
562 if (!MI->isPredicable())
565 if (!isEligibleForITBlock(MI))
568 ARMFunctionInfo *AFI =
569 MI->getParent()->getParent()->getInfo<ARMFunctionInfo>();
571 if (AFI->isThumb2Function()) {
572 if (getSubtarget().restrictIT())
573 return isV8EligibleForIT(MI);
574 } else { // non-Thumb
575 if ((MI->getDesc().TSFlags & ARMII::DomainMask) == ARMII::DomainNEON)
583 template <> bool IsCPSRDead<MachineInstr>(MachineInstr *MI) {
584 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
585 const MachineOperand &MO = MI->getOperand(i);
586 if (!MO.isReg() || MO.isUndef() || MO.isUse())
588 if (MO.getReg() != ARM::CPSR)
593 // all definitions of CPSR are dead
598 /// GetInstSize - Return the size of the specified MachineInstr.
600 unsigned ARMBaseInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
601 const MachineBasicBlock &MBB = *MI->getParent();
602 const MachineFunction *MF = MBB.getParent();
603 const MCAsmInfo *MAI = MF->getTarget().getMCAsmInfo();
605 const MCInstrDesc &MCID = MI->getDesc();
607 return MCID.getSize();
609 // If this machine instr is an inline asm, measure it.
610 if (MI->getOpcode() == ARM::INLINEASM)
611 return getInlineAsmLength(MI->getOperand(0).getSymbolName(), *MAI);
612 unsigned Opc = MI->getOpcode();
615 // pseudo-instruction sizes are zero.
617 case TargetOpcode::BUNDLE:
618 return getInstBundleLength(MI);
619 case ARM::MOVi16_ga_pcrel:
620 case ARM::MOVTi16_ga_pcrel:
621 case ARM::t2MOVi16_ga_pcrel:
622 case ARM::t2MOVTi16_ga_pcrel:
625 case ARM::t2MOVi32imm:
627 case ARM::CONSTPOOL_ENTRY:
628 case ARM::JUMPTABLE_INSTS:
629 case ARM::JUMPTABLE_ADDRS:
630 case ARM::JUMPTABLE_TBB:
631 case ARM::JUMPTABLE_TBH:
632 // If this machine instr is a constant pool entry, its size is recorded as
634 return MI->getOperand(2).getImm();
635 case ARM::Int_eh_sjlj_longjmp:
637 case ARM::tInt_eh_sjlj_longjmp:
639 case ARM::Int_eh_sjlj_setjmp:
640 case ARM::Int_eh_sjlj_setjmp_nofp:
642 case ARM::tInt_eh_sjlj_setjmp:
643 case ARM::t2Int_eh_sjlj_setjmp:
644 case ARM::t2Int_eh_sjlj_setjmp_nofp:
647 return MI->getOperand(1).getImm();
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::copyFromCPSR(MachineBasicBlock &MBB,
663 MachineBasicBlock::iterator I,
664 unsigned DestReg, bool KillSrc,
665 const ARMSubtarget &Subtarget) const {
666 unsigned Opc = Subtarget.isThumb()
667 ? (Subtarget.isMClass() ? ARM::t2MRS_M : ARM::t2MRS_AR)
670 MachineInstrBuilder MIB =
671 BuildMI(MBB, I, I->getDebugLoc(), get(Opc), DestReg);
673 // There is only 1 A/R class MRS instruction, and it always refers to
674 // APSR. However, there are lots of other possibilities on M-class cores.
675 if (Subtarget.isMClass())
680 MIB.addReg(ARM::CPSR, RegState::Implicit | getKillRegState(KillSrc));
683 void ARMBaseInstrInfo::copyToCPSR(MachineBasicBlock &MBB,
684 MachineBasicBlock::iterator I,
685 unsigned SrcReg, bool KillSrc,
686 const ARMSubtarget &Subtarget) const {
687 unsigned Opc = Subtarget.isThumb()
688 ? (Subtarget.isMClass() ? ARM::t2MSR_M : ARM::t2MSR_AR)
691 MachineInstrBuilder MIB = BuildMI(MBB, I, I->getDebugLoc(), get(Opc));
693 if (Subtarget.isMClass())
698 MIB.addReg(SrcReg, getKillRegState(KillSrc));
702 MIB.addReg(ARM::CPSR, RegState::Implicit | RegState::Define);
705 void ARMBaseInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
706 MachineBasicBlock::iterator I, DebugLoc DL,
707 unsigned DestReg, unsigned SrcReg,
708 bool KillSrc) const {
709 bool GPRDest = ARM::GPRRegClass.contains(DestReg);
710 bool GPRSrc = ARM::GPRRegClass.contains(SrcReg);
712 if (GPRDest && GPRSrc) {
713 AddDefaultCC(AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::MOVr), DestReg)
714 .addReg(SrcReg, getKillRegState(KillSrc))));
718 bool SPRDest = ARM::SPRRegClass.contains(DestReg);
719 bool SPRSrc = ARM::SPRRegClass.contains(SrcReg);
722 if (SPRDest && SPRSrc)
724 else if (GPRDest && SPRSrc)
726 else if (SPRDest && GPRSrc)
728 else if (ARM::DPRRegClass.contains(DestReg, SrcReg) && !Subtarget.isFPOnlySP())
730 else if (ARM::QPRRegClass.contains(DestReg, SrcReg))
734 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(Opc), DestReg);
735 MIB.addReg(SrcReg, getKillRegState(KillSrc));
736 if (Opc == ARM::VORRq)
737 MIB.addReg(SrcReg, getKillRegState(KillSrc));
742 // Handle register classes that require multiple instructions.
743 unsigned BeginIdx = 0;
744 unsigned SubRegs = 0;
747 // Use VORRq when possible.
748 if (ARM::QQPRRegClass.contains(DestReg, SrcReg)) {
750 BeginIdx = ARM::qsub_0;
752 } else if (ARM::QQQQPRRegClass.contains(DestReg, SrcReg)) {
754 BeginIdx = ARM::qsub_0;
756 // Fall back to VMOVD.
757 } else if (ARM::DPairRegClass.contains(DestReg, SrcReg)) {
759 BeginIdx = ARM::dsub_0;
761 } else if (ARM::DTripleRegClass.contains(DestReg, SrcReg)) {
763 BeginIdx = ARM::dsub_0;
765 } else if (ARM::DQuadRegClass.contains(DestReg, SrcReg)) {
767 BeginIdx = ARM::dsub_0;
769 } else if (ARM::GPRPairRegClass.contains(DestReg, SrcReg)) {
770 Opc = Subtarget.isThumb2() ? ARM::tMOVr : ARM::MOVr;
771 BeginIdx = ARM::gsub_0;
773 } else if (ARM::DPairSpcRegClass.contains(DestReg, SrcReg)) {
775 BeginIdx = ARM::dsub_0;
778 } else if (ARM::DTripleSpcRegClass.contains(DestReg, SrcReg)) {
780 BeginIdx = ARM::dsub_0;
783 } else if (ARM::DQuadSpcRegClass.contains(DestReg, SrcReg)) {
785 BeginIdx = ARM::dsub_0;
788 } else if (ARM::DPRRegClass.contains(DestReg, SrcReg) && Subtarget.isFPOnlySP()) {
790 BeginIdx = ARM::ssub_0;
792 } else if (SrcReg == ARM::CPSR) {
793 copyFromCPSR(MBB, I, DestReg, KillSrc, Subtarget);
795 } else if (DestReg == ARM::CPSR) {
796 copyToCPSR(MBB, I, SrcReg, KillSrc, Subtarget);
800 assert(Opc && "Impossible reg-to-reg copy");
802 const TargetRegisterInfo *TRI = &getRegisterInfo();
803 MachineInstrBuilder Mov;
805 // Copy register tuples backward when the first Dest reg overlaps with SrcReg.
806 if (TRI->regsOverlap(SrcReg, TRI->getSubReg(DestReg, BeginIdx))) {
807 BeginIdx = BeginIdx + ((SubRegs - 1) * Spacing);
811 SmallSet<unsigned, 4> DstRegs;
813 for (unsigned i = 0; i != SubRegs; ++i) {
814 unsigned Dst = TRI->getSubReg(DestReg, BeginIdx + i * Spacing);
815 unsigned Src = TRI->getSubReg(SrcReg, BeginIdx + i * Spacing);
816 assert(Dst && Src && "Bad sub-register");
818 assert(!DstRegs.count(Src) && "destructive vector copy");
821 Mov = BuildMI(MBB, I, I->getDebugLoc(), get(Opc), Dst).addReg(Src);
822 // VORR takes two source operands.
823 if (Opc == ARM::VORRq)
825 Mov = AddDefaultPred(Mov);
827 if (Opc == ARM::MOVr)
828 Mov = AddDefaultCC(Mov);
830 // Add implicit super-register defs and kills to the last instruction.
831 Mov->addRegisterDefined(DestReg, TRI);
833 Mov->addRegisterKilled(SrcReg, TRI);
836 const MachineInstrBuilder &
837 ARMBaseInstrInfo::AddDReg(MachineInstrBuilder &MIB, unsigned Reg,
838 unsigned SubIdx, unsigned State,
839 const TargetRegisterInfo *TRI) const {
841 return MIB.addReg(Reg, State);
843 if (TargetRegisterInfo::isPhysicalRegister(Reg))
844 return MIB.addReg(TRI->getSubReg(Reg, SubIdx), State);
845 return MIB.addReg(Reg, State, SubIdx);
848 void ARMBaseInstrInfo::
849 storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
850 unsigned SrcReg, bool isKill, int FI,
851 const TargetRegisterClass *RC,
852 const TargetRegisterInfo *TRI) const {
854 if (I != MBB.end()) DL = I->getDebugLoc();
855 MachineFunction &MF = *MBB.getParent();
856 MachineFrameInfo &MFI = *MF.getFrameInfo();
857 unsigned Align = MFI.getObjectAlignment(FI);
859 MachineMemOperand *MMO =
860 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
861 MachineMemOperand::MOStore,
862 MFI.getObjectSize(FI),
865 switch (RC->getSize()) {
867 if (ARM::GPRRegClass.hasSubClassEq(RC)) {
868 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::STRi12))
869 .addReg(SrcReg, getKillRegState(isKill))
870 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
871 } else if (ARM::SPRRegClass.hasSubClassEq(RC)) {
872 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTRS))
873 .addReg(SrcReg, getKillRegState(isKill))
874 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
876 llvm_unreachable("Unknown reg class!");
879 if (ARM::DPRRegClass.hasSubClassEq(RC)) {
880 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTRD))
881 .addReg(SrcReg, getKillRegState(isKill))
882 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
883 } else if (ARM::GPRPairRegClass.hasSubClassEq(RC)) {
884 if (Subtarget.hasV5TEOps()) {
885 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::STRD));
886 AddDReg(MIB, SrcReg, ARM::gsub_0, getKillRegState(isKill), TRI);
887 AddDReg(MIB, SrcReg, ARM::gsub_1, 0, TRI);
888 MIB.addFrameIndex(FI).addReg(0).addImm(0).addMemOperand(MMO);
892 // Fallback to STM instruction, which has existed since the dawn of
894 MachineInstrBuilder MIB =
895 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::STMIA))
896 .addFrameIndex(FI).addMemOperand(MMO));
897 AddDReg(MIB, SrcReg, ARM::gsub_0, getKillRegState(isKill), TRI);
898 AddDReg(MIB, SrcReg, ARM::gsub_1, 0, TRI);
901 llvm_unreachable("Unknown reg class!");
904 if (ARM::DPairRegClass.hasSubClassEq(RC)) {
905 // Use aligned spills if the stack can be realigned.
906 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
907 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VST1q64))
908 .addFrameIndex(FI).addImm(16)
909 .addReg(SrcReg, getKillRegState(isKill))
910 .addMemOperand(MMO));
912 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMQIA))
913 .addReg(SrcReg, getKillRegState(isKill))
915 .addMemOperand(MMO));
918 llvm_unreachable("Unknown reg class!");
921 if (ARM::DTripleRegClass.hasSubClassEq(RC)) {
922 // Use aligned spills if the stack can be realigned.
923 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
924 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VST1d64TPseudo))
925 .addFrameIndex(FI).addImm(16)
926 .addReg(SrcReg, getKillRegState(isKill))
927 .addMemOperand(MMO));
929 MachineInstrBuilder MIB =
930 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMDIA))
933 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
934 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
935 AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
938 llvm_unreachable("Unknown reg class!");
941 if (ARM::QQPRRegClass.hasSubClassEq(RC) || ARM::DQuadRegClass.hasSubClassEq(RC)) {
942 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
943 // FIXME: It's possible to only store part of the QQ register if the
944 // spilled def has a sub-register index.
945 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VST1d64QPseudo))
946 .addFrameIndex(FI).addImm(16)
947 .addReg(SrcReg, getKillRegState(isKill))
948 .addMemOperand(MMO));
950 MachineInstrBuilder MIB =
951 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMDIA))
954 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
955 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
956 MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
957 AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI);
960 llvm_unreachable("Unknown reg class!");
963 if (ARM::QQQQPRRegClass.hasSubClassEq(RC)) {
964 MachineInstrBuilder MIB =
965 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMDIA))
968 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
969 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
970 MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
971 MIB = AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI);
972 MIB = AddDReg(MIB, SrcReg, ARM::dsub_4, 0, TRI);
973 MIB = AddDReg(MIB, SrcReg, ARM::dsub_5, 0, TRI);
974 MIB = AddDReg(MIB, SrcReg, ARM::dsub_6, 0, TRI);
975 AddDReg(MIB, SrcReg, ARM::dsub_7, 0, TRI);
977 llvm_unreachable("Unknown reg class!");
980 llvm_unreachable("Unknown reg class!");
985 ARMBaseInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
986 int &FrameIndex) const {
987 switch (MI->getOpcode()) {
990 case ARM::t2STRs: // FIXME: don't use t2STRs to access frame.
991 if (MI->getOperand(1).isFI() &&
992 MI->getOperand(2).isReg() &&
993 MI->getOperand(3).isImm() &&
994 MI->getOperand(2).getReg() == 0 &&
995 MI->getOperand(3).getImm() == 0) {
996 FrameIndex = MI->getOperand(1).getIndex();
997 return MI->getOperand(0).getReg();
1005 if (MI->getOperand(1).isFI() &&
1006 MI->getOperand(2).isImm() &&
1007 MI->getOperand(2).getImm() == 0) {
1008 FrameIndex = MI->getOperand(1).getIndex();
1009 return MI->getOperand(0).getReg();
1013 case ARM::VST1d64TPseudo:
1014 case ARM::VST1d64QPseudo:
1015 if (MI->getOperand(0).isFI() &&
1016 MI->getOperand(2).getSubReg() == 0) {
1017 FrameIndex = MI->getOperand(0).getIndex();
1018 return MI->getOperand(2).getReg();
1022 if (MI->getOperand(1).isFI() &&
1023 MI->getOperand(0).getSubReg() == 0) {
1024 FrameIndex = MI->getOperand(1).getIndex();
1025 return MI->getOperand(0).getReg();
1033 unsigned ARMBaseInstrInfo::isStoreToStackSlotPostFE(const MachineInstr *MI,
1034 int &FrameIndex) const {
1035 const MachineMemOperand *Dummy;
1036 return MI->mayStore() && hasStoreToStackSlot(MI, Dummy, FrameIndex);
1039 void ARMBaseInstrInfo::
1040 loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
1041 unsigned DestReg, int FI,
1042 const TargetRegisterClass *RC,
1043 const TargetRegisterInfo *TRI) const {
1045 if (I != MBB.end()) DL = I->getDebugLoc();
1046 MachineFunction &MF = *MBB.getParent();
1047 MachineFrameInfo &MFI = *MF.getFrameInfo();
1048 unsigned Align = MFI.getObjectAlignment(FI);
1049 MachineMemOperand *MMO =
1050 MF.getMachineMemOperand(
1051 MachinePointerInfo::getFixedStack(FI),
1052 MachineMemOperand::MOLoad,
1053 MFI.getObjectSize(FI),
1056 switch (RC->getSize()) {
1058 if (ARM::GPRRegClass.hasSubClassEq(RC)) {
1059 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::LDRi12), DestReg)
1060 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
1062 } else if (ARM::SPRRegClass.hasSubClassEq(RC)) {
1063 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDRS), DestReg)
1064 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
1066 llvm_unreachable("Unknown reg class!");
1069 if (ARM::DPRRegClass.hasSubClassEq(RC)) {
1070 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDRD), DestReg)
1071 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
1072 } else if (ARM::GPRPairRegClass.hasSubClassEq(RC)) {
1073 MachineInstrBuilder MIB;
1075 if (Subtarget.hasV5TEOps()) {
1076 MIB = BuildMI(MBB, I, DL, get(ARM::LDRD));
1077 AddDReg(MIB, DestReg, ARM::gsub_0, RegState::DefineNoRead, TRI);
1078 AddDReg(MIB, DestReg, ARM::gsub_1, RegState::DefineNoRead, TRI);
1079 MIB.addFrameIndex(FI).addReg(0).addImm(0).addMemOperand(MMO);
1081 AddDefaultPred(MIB);
1083 // Fallback to LDM instruction, which has existed since the dawn of
1085 MIB = AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::LDMIA))
1086 .addFrameIndex(FI).addMemOperand(MMO));
1087 MIB = AddDReg(MIB, DestReg, ARM::gsub_0, RegState::DefineNoRead, TRI);
1088 MIB = AddDReg(MIB, DestReg, ARM::gsub_1, RegState::DefineNoRead, TRI);
1091 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1092 MIB.addReg(DestReg, RegState::ImplicitDefine);
1094 llvm_unreachable("Unknown reg class!");
1097 if (ARM::DPairRegClass.hasSubClassEq(RC)) {
1098 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
1099 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLD1q64), DestReg)
1100 .addFrameIndex(FI).addImm(16)
1101 .addMemOperand(MMO));
1103 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMQIA), DestReg)
1105 .addMemOperand(MMO));
1108 llvm_unreachable("Unknown reg class!");
1111 if (ARM::DTripleRegClass.hasSubClassEq(RC)) {
1112 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
1113 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLD1d64TPseudo), DestReg)
1114 .addFrameIndex(FI).addImm(16)
1115 .addMemOperand(MMO));
1117 MachineInstrBuilder MIB =
1118 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
1120 .addMemOperand(MMO));
1121 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::DefineNoRead, TRI);
1122 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::DefineNoRead, TRI);
1123 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::DefineNoRead, TRI);
1124 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1125 MIB.addReg(DestReg, RegState::ImplicitDefine);
1128 llvm_unreachable("Unknown reg class!");
1131 if (ARM::QQPRRegClass.hasSubClassEq(RC) || ARM::DQuadRegClass.hasSubClassEq(RC)) {
1132 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
1133 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLD1d64QPseudo), DestReg)
1134 .addFrameIndex(FI).addImm(16)
1135 .addMemOperand(MMO));
1137 MachineInstrBuilder MIB =
1138 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
1140 .addMemOperand(MMO);
1141 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::DefineNoRead, TRI);
1142 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::DefineNoRead, TRI);
1143 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::DefineNoRead, TRI);
1144 MIB = AddDReg(MIB, DestReg, ARM::dsub_3, RegState::DefineNoRead, TRI);
1145 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1146 MIB.addReg(DestReg, RegState::ImplicitDefine);
1149 llvm_unreachable("Unknown reg class!");
1152 if (ARM::QQQQPRRegClass.hasSubClassEq(RC)) {
1153 MachineInstrBuilder MIB =
1154 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
1156 .addMemOperand(MMO);
1157 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::DefineNoRead, TRI);
1158 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::DefineNoRead, TRI);
1159 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::DefineNoRead, TRI);
1160 MIB = AddDReg(MIB, DestReg, ARM::dsub_3, RegState::DefineNoRead, TRI);
1161 MIB = AddDReg(MIB, DestReg, ARM::dsub_4, RegState::DefineNoRead, TRI);
1162 MIB = AddDReg(MIB, DestReg, ARM::dsub_5, RegState::DefineNoRead, TRI);
1163 MIB = AddDReg(MIB, DestReg, ARM::dsub_6, RegState::DefineNoRead, TRI);
1164 MIB = AddDReg(MIB, DestReg, ARM::dsub_7, RegState::DefineNoRead, TRI);
1165 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1166 MIB.addReg(DestReg, RegState::ImplicitDefine);
1168 llvm_unreachable("Unknown reg class!");
1171 llvm_unreachable("Unknown regclass!");
1176 ARMBaseInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
1177 int &FrameIndex) const {
1178 switch (MI->getOpcode()) {
1181 case ARM::t2LDRs: // FIXME: don't use t2LDRs to access frame.
1182 if (MI->getOperand(1).isFI() &&
1183 MI->getOperand(2).isReg() &&
1184 MI->getOperand(3).isImm() &&
1185 MI->getOperand(2).getReg() == 0 &&
1186 MI->getOperand(3).getImm() == 0) {
1187 FrameIndex = MI->getOperand(1).getIndex();
1188 return MI->getOperand(0).getReg();
1196 if (MI->getOperand(1).isFI() &&
1197 MI->getOperand(2).isImm() &&
1198 MI->getOperand(2).getImm() == 0) {
1199 FrameIndex = MI->getOperand(1).getIndex();
1200 return MI->getOperand(0).getReg();
1204 case ARM::VLD1d64TPseudo:
1205 case ARM::VLD1d64QPseudo:
1206 if (MI->getOperand(1).isFI() &&
1207 MI->getOperand(0).getSubReg() == 0) {
1208 FrameIndex = MI->getOperand(1).getIndex();
1209 return MI->getOperand(0).getReg();
1213 if (MI->getOperand(1).isFI() &&
1214 MI->getOperand(0).getSubReg() == 0) {
1215 FrameIndex = MI->getOperand(1).getIndex();
1216 return MI->getOperand(0).getReg();
1224 unsigned ARMBaseInstrInfo::isLoadFromStackSlotPostFE(const MachineInstr *MI,
1225 int &FrameIndex) const {
1226 const MachineMemOperand *Dummy;
1227 return MI->mayLoad() && hasLoadFromStackSlot(MI, Dummy, FrameIndex);
1231 ARMBaseInstrInfo::expandPostRAPseudo(MachineBasicBlock::iterator MI) const {
1232 MachineFunction &MF = *MI->getParent()->getParent();
1233 Reloc::Model RM = MF.getTarget().getRelocationModel();
1235 if (MI->getOpcode() == TargetOpcode::LOAD_STACK_GUARD) {
1236 assert(getSubtarget().getTargetTriple().getObjectFormat() ==
1238 "LOAD_STACK_GUARD currently supported only for MachO.");
1239 expandLoadStackGuard(MI, RM);
1240 MI->getParent()->erase(MI);
1244 // This hook gets to expand COPY instructions before they become
1245 // copyPhysReg() calls. Look for VMOVS instructions that can legally be
1246 // widened to VMOVD. We prefer the VMOVD when possible because it may be
1247 // changed into a VORR that can go down the NEON pipeline.
1248 if (!WidenVMOVS || !MI->isCopy() || Subtarget.isCortexA15() ||
1249 Subtarget.isFPOnlySP())
1252 // Look for a copy between even S-registers. That is where we keep floats
1253 // when using NEON v2f32 instructions for f32 arithmetic.
1254 unsigned DstRegS = MI->getOperand(0).getReg();
1255 unsigned SrcRegS = MI->getOperand(1).getReg();
1256 if (!ARM::SPRRegClass.contains(DstRegS, SrcRegS))
1259 const TargetRegisterInfo *TRI = &getRegisterInfo();
1260 unsigned DstRegD = TRI->getMatchingSuperReg(DstRegS, ARM::ssub_0,
1262 unsigned SrcRegD = TRI->getMatchingSuperReg(SrcRegS, ARM::ssub_0,
1264 if (!DstRegD || !SrcRegD)
1267 // We want to widen this into a DstRegD = VMOVD SrcRegD copy. This is only
1268 // legal if the COPY already defines the full DstRegD, and it isn't a
1269 // sub-register insertion.
1270 if (!MI->definesRegister(DstRegD, TRI) || MI->readsRegister(DstRegD, TRI))
1273 // A dead copy shouldn't show up here, but reject it just in case.
1274 if (MI->getOperand(0).isDead())
1277 // All clear, widen the COPY.
1278 DEBUG(dbgs() << "widening: " << *MI);
1279 MachineInstrBuilder MIB(*MI->getParent()->getParent(), MI);
1281 // Get rid of the old <imp-def> of DstRegD. Leave it if it defines a Q-reg
1282 // or some other super-register.
1283 int ImpDefIdx = MI->findRegisterDefOperandIdx(DstRegD);
1284 if (ImpDefIdx != -1)
1285 MI->RemoveOperand(ImpDefIdx);
1287 // Change the opcode and operands.
1288 MI->setDesc(get(ARM::VMOVD));
1289 MI->getOperand(0).setReg(DstRegD);
1290 MI->getOperand(1).setReg(SrcRegD);
1291 AddDefaultPred(MIB);
1293 // We are now reading SrcRegD instead of SrcRegS. This may upset the
1294 // register scavenger and machine verifier, so we need to indicate that we
1295 // are reading an undefined value from SrcRegD, but a proper value from
1297 MI->getOperand(1).setIsUndef();
1298 MIB.addReg(SrcRegS, RegState::Implicit);
1300 // SrcRegD may actually contain an unrelated value in the ssub_1
1301 // sub-register. Don't kill it. Only kill the ssub_0 sub-register.
1302 if (MI->getOperand(1).isKill()) {
1303 MI->getOperand(1).setIsKill(false);
1304 MI->addRegisterKilled(SrcRegS, TRI, true);
1307 DEBUG(dbgs() << "replaced by: " << *MI);
1311 /// Create a copy of a const pool value. Update CPI to the new index and return
1313 static unsigned duplicateCPV(MachineFunction &MF, unsigned &CPI) {
1314 MachineConstantPool *MCP = MF.getConstantPool();
1315 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1317 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPI];
1318 assert(MCPE.isMachineConstantPoolEntry() &&
1319 "Expecting a machine constantpool entry!");
1320 ARMConstantPoolValue *ACPV =
1321 static_cast<ARMConstantPoolValue*>(MCPE.Val.MachineCPVal);
1323 unsigned PCLabelId = AFI->createPICLabelUId();
1324 ARMConstantPoolValue *NewCPV = nullptr;
1326 // FIXME: The below assumes PIC relocation model and that the function
1327 // is Thumb mode (t1 or t2). PCAdjustment would be 8 for ARM mode PIC, and
1328 // zero for non-PIC in ARM or Thumb. The callers are all of thumb LDR
1329 // instructions, so that's probably OK, but is PIC always correct when
1331 if (ACPV->isGlobalValue())
1332 NewCPV = ARMConstantPoolConstant::
1333 Create(cast<ARMConstantPoolConstant>(ACPV)->getGV(), PCLabelId,
1335 else if (ACPV->isExtSymbol())
1336 NewCPV = ARMConstantPoolSymbol::
1337 Create(MF.getFunction()->getContext(),
1338 cast<ARMConstantPoolSymbol>(ACPV)->getSymbol(), PCLabelId, 4);
1339 else if (ACPV->isBlockAddress())
1340 NewCPV = ARMConstantPoolConstant::
1341 Create(cast<ARMConstantPoolConstant>(ACPV)->getBlockAddress(), PCLabelId,
1342 ARMCP::CPBlockAddress, 4);
1343 else if (ACPV->isLSDA())
1344 NewCPV = ARMConstantPoolConstant::Create(MF.getFunction(), PCLabelId,
1346 else if (ACPV->isMachineBasicBlock())
1347 NewCPV = ARMConstantPoolMBB::
1348 Create(MF.getFunction()->getContext(),
1349 cast<ARMConstantPoolMBB>(ACPV)->getMBB(), PCLabelId, 4);
1351 llvm_unreachable("Unexpected ARM constantpool value type!!");
1352 CPI = MCP->getConstantPoolIndex(NewCPV, MCPE.getAlignment());
1356 void ARMBaseInstrInfo::
1357 reMaterialize(MachineBasicBlock &MBB,
1358 MachineBasicBlock::iterator I,
1359 unsigned DestReg, unsigned SubIdx,
1360 const MachineInstr *Orig,
1361 const TargetRegisterInfo &TRI) const {
1362 unsigned Opcode = Orig->getOpcode();
1365 MachineInstr *MI = MBB.getParent()->CloneMachineInstr(Orig);
1366 MI->substituteRegister(Orig->getOperand(0).getReg(), DestReg, SubIdx, TRI);
1370 case ARM::tLDRpci_pic:
1371 case ARM::t2LDRpci_pic: {
1372 MachineFunction &MF = *MBB.getParent();
1373 unsigned CPI = Orig->getOperand(1).getIndex();
1374 unsigned PCLabelId = duplicateCPV(MF, CPI);
1375 MachineInstrBuilder MIB = BuildMI(MBB, I, Orig->getDebugLoc(), get(Opcode),
1377 .addConstantPoolIndex(CPI).addImm(PCLabelId);
1378 MIB->setMemRefs(Orig->memoperands_begin(), Orig->memoperands_end());
1385 ARMBaseInstrInfo::duplicate(MachineInstr *Orig, MachineFunction &MF) const {
1386 MachineInstr *MI = TargetInstrInfo::duplicate(Orig, MF);
1387 switch(Orig->getOpcode()) {
1388 case ARM::tLDRpci_pic:
1389 case ARM::t2LDRpci_pic: {
1390 unsigned CPI = Orig->getOperand(1).getIndex();
1391 unsigned PCLabelId = duplicateCPV(MF, CPI);
1392 Orig->getOperand(1).setIndex(CPI);
1393 Orig->getOperand(2).setImm(PCLabelId);
1400 bool ARMBaseInstrInfo::produceSameValue(const MachineInstr *MI0,
1401 const MachineInstr *MI1,
1402 const MachineRegisterInfo *MRI) const {
1403 unsigned Opcode = MI0->getOpcode();
1404 if (Opcode == ARM::t2LDRpci ||
1405 Opcode == ARM::t2LDRpci_pic ||
1406 Opcode == ARM::tLDRpci ||
1407 Opcode == ARM::tLDRpci_pic ||
1408 Opcode == ARM::LDRLIT_ga_pcrel ||
1409 Opcode == ARM::LDRLIT_ga_pcrel_ldr ||
1410 Opcode == ARM::tLDRLIT_ga_pcrel ||
1411 Opcode == ARM::MOV_ga_pcrel ||
1412 Opcode == ARM::MOV_ga_pcrel_ldr ||
1413 Opcode == ARM::t2MOV_ga_pcrel) {
1414 if (MI1->getOpcode() != Opcode)
1416 if (MI0->getNumOperands() != MI1->getNumOperands())
1419 const MachineOperand &MO0 = MI0->getOperand(1);
1420 const MachineOperand &MO1 = MI1->getOperand(1);
1421 if (MO0.getOffset() != MO1.getOffset())
1424 if (Opcode == ARM::LDRLIT_ga_pcrel ||
1425 Opcode == ARM::LDRLIT_ga_pcrel_ldr ||
1426 Opcode == ARM::tLDRLIT_ga_pcrel ||
1427 Opcode == ARM::MOV_ga_pcrel ||
1428 Opcode == ARM::MOV_ga_pcrel_ldr ||
1429 Opcode == ARM::t2MOV_ga_pcrel)
1430 // Ignore the PC labels.
1431 return MO0.getGlobal() == MO1.getGlobal();
1433 const MachineFunction *MF = MI0->getParent()->getParent();
1434 const MachineConstantPool *MCP = MF->getConstantPool();
1435 int CPI0 = MO0.getIndex();
1436 int CPI1 = MO1.getIndex();
1437 const MachineConstantPoolEntry &MCPE0 = MCP->getConstants()[CPI0];
1438 const MachineConstantPoolEntry &MCPE1 = MCP->getConstants()[CPI1];
1439 bool isARMCP0 = MCPE0.isMachineConstantPoolEntry();
1440 bool isARMCP1 = MCPE1.isMachineConstantPoolEntry();
1441 if (isARMCP0 && isARMCP1) {
1442 ARMConstantPoolValue *ACPV0 =
1443 static_cast<ARMConstantPoolValue*>(MCPE0.Val.MachineCPVal);
1444 ARMConstantPoolValue *ACPV1 =
1445 static_cast<ARMConstantPoolValue*>(MCPE1.Val.MachineCPVal);
1446 return ACPV0->hasSameValue(ACPV1);
1447 } else if (!isARMCP0 && !isARMCP1) {
1448 return MCPE0.Val.ConstVal == MCPE1.Val.ConstVal;
1451 } else if (Opcode == ARM::PICLDR) {
1452 if (MI1->getOpcode() != Opcode)
1454 if (MI0->getNumOperands() != MI1->getNumOperands())
1457 unsigned Addr0 = MI0->getOperand(1).getReg();
1458 unsigned Addr1 = MI1->getOperand(1).getReg();
1459 if (Addr0 != Addr1) {
1461 !TargetRegisterInfo::isVirtualRegister(Addr0) ||
1462 !TargetRegisterInfo::isVirtualRegister(Addr1))
1465 // This assumes SSA form.
1466 MachineInstr *Def0 = MRI->getVRegDef(Addr0);
1467 MachineInstr *Def1 = MRI->getVRegDef(Addr1);
1468 // Check if the loaded value, e.g. a constantpool of a global address, are
1470 if (!produceSameValue(Def0, Def1, MRI))
1474 for (unsigned i = 3, e = MI0->getNumOperands(); i != e; ++i) {
1475 // %vreg12<def> = PICLDR %vreg11, 0, pred:14, pred:%noreg
1476 const MachineOperand &MO0 = MI0->getOperand(i);
1477 const MachineOperand &MO1 = MI1->getOperand(i);
1478 if (!MO0.isIdenticalTo(MO1))
1484 return MI0->isIdenticalTo(MI1, MachineInstr::IgnoreVRegDefs);
1487 /// areLoadsFromSameBasePtr - This is used by the pre-regalloc scheduler to
1488 /// determine if two loads are loading from the same base address. It should
1489 /// only return true if the base pointers are the same and the only differences
1490 /// between the two addresses is the offset. It also returns the offsets by
1493 /// FIXME: remove this in favor of the MachineInstr interface once pre-RA-sched
1494 /// is permanently disabled.
1495 bool ARMBaseInstrInfo::areLoadsFromSameBasePtr(SDNode *Load1, SDNode *Load2,
1497 int64_t &Offset2) const {
1498 // Don't worry about Thumb: just ARM and Thumb2.
1499 if (Subtarget.isThumb1Only()) return false;
1501 if (!Load1->isMachineOpcode() || !Load2->isMachineOpcode())
1504 switch (Load1->getMachineOpcode()) {
1518 case ARM::t2LDRSHi8:
1520 case ARM::t2LDRBi12:
1521 case ARM::t2LDRSHi12:
1525 switch (Load2->getMachineOpcode()) {
1538 case ARM::t2LDRSHi8:
1540 case ARM::t2LDRBi12:
1541 case ARM::t2LDRSHi12:
1545 // Check if base addresses and chain operands match.
1546 if (Load1->getOperand(0) != Load2->getOperand(0) ||
1547 Load1->getOperand(4) != Load2->getOperand(4))
1550 // Index should be Reg0.
1551 if (Load1->getOperand(3) != Load2->getOperand(3))
1554 // Determine the offsets.
1555 if (isa<ConstantSDNode>(Load1->getOperand(1)) &&
1556 isa<ConstantSDNode>(Load2->getOperand(1))) {
1557 Offset1 = cast<ConstantSDNode>(Load1->getOperand(1))->getSExtValue();
1558 Offset2 = cast<ConstantSDNode>(Load2->getOperand(1))->getSExtValue();
1565 /// shouldScheduleLoadsNear - This is a used by the pre-regalloc scheduler to
1566 /// determine (in conjunction with areLoadsFromSameBasePtr) if two loads should
1567 /// be scheduled togther. On some targets if two loads are loading from
1568 /// addresses in the same cache line, it's better if they are scheduled
1569 /// together. This function takes two integers that represent the load offsets
1570 /// from the common base address. It returns true if it decides it's desirable
1571 /// to schedule the two loads together. "NumLoads" is the number of loads that
1572 /// have already been scheduled after Load1.
1574 /// FIXME: remove this in favor of the MachineInstr interface once pre-RA-sched
1575 /// is permanently disabled.
1576 bool ARMBaseInstrInfo::shouldScheduleLoadsNear(SDNode *Load1, SDNode *Load2,
1577 int64_t Offset1, int64_t Offset2,
1578 unsigned NumLoads) const {
1579 // Don't worry about Thumb: just ARM and Thumb2.
1580 if (Subtarget.isThumb1Only()) return false;
1582 assert(Offset2 > Offset1);
1584 if ((Offset2 - Offset1) / 8 > 64)
1587 // Check if the machine opcodes are different. If they are different
1588 // then we consider them to not be of the same base address,
1589 // EXCEPT in the case of Thumb2 byte loads where one is LDRBi8 and the other LDRBi12.
1590 // In this case, they are considered to be the same because they are different
1591 // encoding forms of the same basic instruction.
1592 if ((Load1->getMachineOpcode() != Load2->getMachineOpcode()) &&
1593 !((Load1->getMachineOpcode() == ARM::t2LDRBi8 &&
1594 Load2->getMachineOpcode() == ARM::t2LDRBi12) ||
1595 (Load1->getMachineOpcode() == ARM::t2LDRBi12 &&
1596 Load2->getMachineOpcode() == ARM::t2LDRBi8)))
1597 return false; // FIXME: overly conservative?
1599 // Four loads in a row should be sufficient.
1606 bool ARMBaseInstrInfo::isSchedulingBoundary(const MachineInstr *MI,
1607 const MachineBasicBlock *MBB,
1608 const MachineFunction &MF) const {
1609 // Debug info is never a scheduling boundary. It's necessary to be explicit
1610 // due to the special treatment of IT instructions below, otherwise a
1611 // dbg_value followed by an IT will result in the IT instruction being
1612 // considered a scheduling hazard, which is wrong. It should be the actual
1613 // instruction preceding the dbg_value instruction(s), just like it is
1614 // when debug info is not present.
1615 if (MI->isDebugValue())
1618 // Terminators and labels can't be scheduled around.
1619 if (MI->isTerminator() || MI->isPosition())
1622 // Treat the start of the IT block as a scheduling boundary, but schedule
1623 // t2IT along with all instructions following it.
1624 // FIXME: This is a big hammer. But the alternative is to add all potential
1625 // true and anti dependencies to IT block instructions as implicit operands
1626 // to the t2IT instruction. The added compile time and complexity does not
1628 MachineBasicBlock::const_iterator I = MI;
1629 // Make sure to skip any dbg_value instructions
1630 while (++I != MBB->end() && I->isDebugValue())
1632 if (I != MBB->end() && I->getOpcode() == ARM::t2IT)
1635 // Don't attempt to schedule around any instruction that defines
1636 // a stack-oriented pointer, as it's unlikely to be profitable. This
1637 // saves compile time, because it doesn't require every single
1638 // stack slot reference to depend on the instruction that does the
1640 // Calls don't actually change the stack pointer, even if they have imp-defs.
1641 // No ARM calling conventions change the stack pointer. (X86 calling
1642 // conventions sometimes do).
1643 if (!MI->isCall() && MI->definesRegister(ARM::SP))
1649 bool ARMBaseInstrInfo::
1650 isProfitableToIfCvt(MachineBasicBlock &MBB,
1651 unsigned NumCycles, unsigned ExtraPredCycles,
1652 const BranchProbability &Probability) const {
1656 // If we are optimizing for size, see if the branch in the predecessor can be
1657 // lowered to cbn?z by the constant island lowering pass, and return false if
1658 // so. This results in a shorter instruction sequence.
1659 const Function *F = MBB.getParent()->getFunction();
1660 if (F->hasFnAttribute(Attribute::OptimizeForSize) ||
1661 F->hasFnAttribute(Attribute::MinSize)) {
1662 MachineBasicBlock *Pred = *MBB.pred_begin();
1663 if (!Pred->empty()) {
1664 MachineInstr *LastMI = &*Pred->rbegin();
1665 if (LastMI->getOpcode() == ARM::t2Bcc) {
1666 MachineBasicBlock::iterator CmpMI = LastMI;
1667 if (CmpMI != Pred->begin()) {
1669 if (CmpMI->getOpcode() == ARM::tCMPi8 ||
1670 CmpMI->getOpcode() == ARM::t2CMPri) {
1671 unsigned Reg = CmpMI->getOperand(0).getReg();
1672 unsigned PredReg = 0;
1673 ARMCC::CondCodes P = getInstrPredicate(CmpMI, PredReg);
1674 if (P == ARMCC::AL && CmpMI->getOperand(1).getImm() == 0 &&
1675 isARMLowRegister(Reg))
1683 // Attempt to estimate the relative costs of predication versus branching.
1684 unsigned UnpredCost = Probability.getNumerator() * NumCycles;
1685 UnpredCost /= Probability.getDenominator();
1686 UnpredCost += 1; // The branch itself
1687 UnpredCost += Subtarget.getMispredictionPenalty() / 10;
1689 return (NumCycles + ExtraPredCycles) <= UnpredCost;
1692 bool ARMBaseInstrInfo::
1693 isProfitableToIfCvt(MachineBasicBlock &TMBB,
1694 unsigned TCycles, unsigned TExtra,
1695 MachineBasicBlock &FMBB,
1696 unsigned FCycles, unsigned FExtra,
1697 const BranchProbability &Probability) const {
1698 if (!TCycles || !FCycles)
1701 // Attempt to estimate the relative costs of predication versus branching.
1702 unsigned TUnpredCost = Probability.getNumerator() * TCycles;
1703 TUnpredCost /= Probability.getDenominator();
1705 uint32_t Comp = Probability.getDenominator() - Probability.getNumerator();
1706 unsigned FUnpredCost = Comp * FCycles;
1707 FUnpredCost /= Probability.getDenominator();
1709 unsigned UnpredCost = TUnpredCost + FUnpredCost;
1710 UnpredCost += 1; // The branch itself
1711 UnpredCost += Subtarget.getMispredictionPenalty() / 10;
1713 return (TCycles + FCycles + TExtra + FExtra) <= UnpredCost;
1717 ARMBaseInstrInfo::isProfitableToUnpredicate(MachineBasicBlock &TMBB,
1718 MachineBasicBlock &FMBB) const {
1719 // Reduce false anti-dependencies to let Swift's out-of-order execution
1720 // engine do its thing.
1721 return Subtarget.isSwift();
1724 /// getInstrPredicate - If instruction is predicated, returns its predicate
1725 /// condition, otherwise returns AL. It also returns the condition code
1726 /// register by reference.
1728 llvm::getInstrPredicate(const MachineInstr *MI, unsigned &PredReg) {
1729 int PIdx = MI->findFirstPredOperandIdx();
1735 PredReg = MI->getOperand(PIdx+1).getReg();
1736 return (ARMCC::CondCodes)MI->getOperand(PIdx).getImm();
1740 unsigned llvm::getMatchingCondBranchOpcode(unsigned Opc) {
1745 if (Opc == ARM::t2B)
1748 llvm_unreachable("Unknown unconditional branch opcode!");
1751 /// commuteInstruction - Handle commutable instructions.
1753 ARMBaseInstrInfo::commuteInstruction(MachineInstr *MI, bool NewMI) const {
1754 switch (MI->getOpcode()) {
1756 case ARM::t2MOVCCr: {
1757 // MOVCC can be commuted by inverting the condition.
1758 unsigned PredReg = 0;
1759 ARMCC::CondCodes CC = getInstrPredicate(MI, PredReg);
1760 // MOVCC AL can't be inverted. Shouldn't happen.
1761 if (CC == ARMCC::AL || PredReg != ARM::CPSR)
1763 MI = TargetInstrInfo::commuteInstruction(MI, NewMI);
1766 // After swapping the MOVCC operands, also invert the condition.
1767 MI->getOperand(MI->findFirstPredOperandIdx())
1768 .setImm(ARMCC::getOppositeCondition(CC));
1772 return TargetInstrInfo::commuteInstruction(MI, NewMI);
1775 /// Identify instructions that can be folded into a MOVCC instruction, and
1776 /// return the defining instruction.
1777 static MachineInstr *canFoldIntoMOVCC(unsigned Reg,
1778 const MachineRegisterInfo &MRI,
1779 const TargetInstrInfo *TII) {
1780 if (!TargetRegisterInfo::isVirtualRegister(Reg))
1782 if (!MRI.hasOneNonDBGUse(Reg))
1784 MachineInstr *MI = MRI.getVRegDef(Reg);
1787 // MI is folded into the MOVCC by predicating it.
1788 if (!MI->isPredicable())
1790 // Check if MI has any non-dead defs or physreg uses. This also detects
1791 // predicated instructions which will be reading CPSR.
1792 for (unsigned i = 1, e = MI->getNumOperands(); i != e; ++i) {
1793 const MachineOperand &MO = MI->getOperand(i);
1794 // Reject frame index operands, PEI can't handle the predicated pseudos.
1795 if (MO.isFI() || MO.isCPI() || MO.isJTI())
1799 // MI can't have any tied operands, that would conflict with predication.
1802 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
1804 if (MO.isDef() && !MO.isDead())
1807 bool DontMoveAcrossStores = true;
1808 if (!MI->isSafeToMove(/* AliasAnalysis = */ nullptr, DontMoveAcrossStores))
1813 bool ARMBaseInstrInfo::analyzeSelect(const MachineInstr *MI,
1814 SmallVectorImpl<MachineOperand> &Cond,
1815 unsigned &TrueOp, unsigned &FalseOp,
1816 bool &Optimizable) const {
1817 assert((MI->getOpcode() == ARM::MOVCCr || MI->getOpcode() == ARM::t2MOVCCr) &&
1818 "Unknown select instruction");
1823 // 3: Condition code.
1827 Cond.push_back(MI->getOperand(3));
1828 Cond.push_back(MI->getOperand(4));
1829 // We can always fold a def.
1835 ARMBaseInstrInfo::optimizeSelect(MachineInstr *MI,
1836 SmallPtrSetImpl<MachineInstr *> &SeenMIs,
1837 bool PreferFalse) const {
1838 assert((MI->getOpcode() == ARM::MOVCCr || MI->getOpcode() == ARM::t2MOVCCr) &&
1839 "Unknown select instruction");
1840 MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo();
1841 MachineInstr *DefMI = canFoldIntoMOVCC(MI->getOperand(2).getReg(), MRI, this);
1842 bool Invert = !DefMI;
1844 DefMI = canFoldIntoMOVCC(MI->getOperand(1).getReg(), MRI, this);
1848 // Find new register class to use.
1849 MachineOperand FalseReg = MI->getOperand(Invert ? 2 : 1);
1850 unsigned DestReg = MI->getOperand(0).getReg();
1851 const TargetRegisterClass *PreviousClass = MRI.getRegClass(FalseReg.getReg());
1852 if (!MRI.constrainRegClass(DestReg, PreviousClass))
1855 // Create a new predicated version of DefMI.
1856 // Rfalse is the first use.
1857 MachineInstrBuilder NewMI = BuildMI(*MI->getParent(), MI, MI->getDebugLoc(),
1858 DefMI->getDesc(), DestReg);
1860 // Copy all the DefMI operands, excluding its (null) predicate.
1861 const MCInstrDesc &DefDesc = DefMI->getDesc();
1862 for (unsigned i = 1, e = DefDesc.getNumOperands();
1863 i != e && !DefDesc.OpInfo[i].isPredicate(); ++i)
1864 NewMI.addOperand(DefMI->getOperand(i));
1866 unsigned CondCode = MI->getOperand(3).getImm();
1868 NewMI.addImm(ARMCC::getOppositeCondition(ARMCC::CondCodes(CondCode)));
1870 NewMI.addImm(CondCode);
1871 NewMI.addOperand(MI->getOperand(4));
1873 // DefMI is not the -S version that sets CPSR, so add an optional %noreg.
1874 if (NewMI->hasOptionalDef())
1875 AddDefaultCC(NewMI);
1877 // The output register value when the predicate is false is an implicit
1878 // register operand tied to the first def.
1879 // The tie makes the register allocator ensure the FalseReg is allocated the
1880 // same register as operand 0.
1881 FalseReg.setImplicit();
1882 NewMI.addOperand(FalseReg);
1883 NewMI->tieOperands(0, NewMI->getNumOperands() - 1);
1885 // Update SeenMIs set: register newly created MI and erase removed DefMI.
1886 SeenMIs.insert(NewMI);
1887 SeenMIs.erase(DefMI);
1889 // If MI is inside a loop, and DefMI is outside the loop, then kill flags on
1890 // DefMI would be invalid when tranferred inside the loop. Checking for a
1891 // loop is expensive, but at least remove kill flags if they are in different
1893 if (DefMI->getParent() != MI->getParent())
1894 NewMI->clearKillInfo();
1896 // The caller will erase MI, but not DefMI.
1897 DefMI->eraseFromParent();
1901 /// Map pseudo instructions that imply an 'S' bit onto real opcodes. Whether the
1902 /// instruction is encoded with an 'S' bit is determined by the optional CPSR
1905 /// This will go away once we can teach tblgen how to set the optional CPSR def
1907 struct AddSubFlagsOpcodePair {
1909 uint16_t MachineOpc;
1912 static const AddSubFlagsOpcodePair AddSubFlagsOpcodeMap[] = {
1913 {ARM::ADDSri, ARM::ADDri},
1914 {ARM::ADDSrr, ARM::ADDrr},
1915 {ARM::ADDSrsi, ARM::ADDrsi},
1916 {ARM::ADDSrsr, ARM::ADDrsr},
1918 {ARM::SUBSri, ARM::SUBri},
1919 {ARM::SUBSrr, ARM::SUBrr},
1920 {ARM::SUBSrsi, ARM::SUBrsi},
1921 {ARM::SUBSrsr, ARM::SUBrsr},
1923 {ARM::RSBSri, ARM::RSBri},
1924 {ARM::RSBSrsi, ARM::RSBrsi},
1925 {ARM::RSBSrsr, ARM::RSBrsr},
1927 {ARM::t2ADDSri, ARM::t2ADDri},
1928 {ARM::t2ADDSrr, ARM::t2ADDrr},
1929 {ARM::t2ADDSrs, ARM::t2ADDrs},
1931 {ARM::t2SUBSri, ARM::t2SUBri},
1932 {ARM::t2SUBSrr, ARM::t2SUBrr},
1933 {ARM::t2SUBSrs, ARM::t2SUBrs},
1935 {ARM::t2RSBSri, ARM::t2RSBri},
1936 {ARM::t2RSBSrs, ARM::t2RSBrs},
1939 unsigned llvm::convertAddSubFlagsOpcode(unsigned OldOpc) {
1940 for (unsigned i = 0, e = array_lengthof(AddSubFlagsOpcodeMap); i != e; ++i)
1941 if (OldOpc == AddSubFlagsOpcodeMap[i].PseudoOpc)
1942 return AddSubFlagsOpcodeMap[i].MachineOpc;
1946 void llvm::emitARMRegPlusImmediate(MachineBasicBlock &MBB,
1947 MachineBasicBlock::iterator &MBBI, DebugLoc dl,
1948 unsigned DestReg, unsigned BaseReg, int NumBytes,
1949 ARMCC::CondCodes Pred, unsigned PredReg,
1950 const ARMBaseInstrInfo &TII, unsigned MIFlags) {
1951 if (NumBytes == 0 && DestReg != BaseReg) {
1952 BuildMI(MBB, MBBI, dl, TII.get(ARM::MOVr), DestReg)
1953 .addReg(BaseReg, RegState::Kill)
1954 .addImm((unsigned)Pred).addReg(PredReg).addReg(0)
1955 .setMIFlags(MIFlags);
1959 bool isSub = NumBytes < 0;
1960 if (isSub) NumBytes = -NumBytes;
1963 unsigned RotAmt = ARM_AM::getSOImmValRotate(NumBytes);
1964 unsigned ThisVal = NumBytes & ARM_AM::rotr32(0xFF, RotAmt);
1965 assert(ThisVal && "Didn't extract field correctly");
1967 // We will handle these bits from offset, clear them.
1968 NumBytes &= ~ThisVal;
1970 assert(ARM_AM::getSOImmVal(ThisVal) != -1 && "Bit extraction didn't work?");
1972 // Build the new ADD / SUB.
1973 unsigned Opc = isSub ? ARM::SUBri : ARM::ADDri;
1974 BuildMI(MBB, MBBI, dl, TII.get(Opc), DestReg)
1975 .addReg(BaseReg, RegState::Kill).addImm(ThisVal)
1976 .addImm((unsigned)Pred).addReg(PredReg).addReg(0)
1977 .setMIFlags(MIFlags);
1982 static bool isAnySubRegLive(unsigned Reg, const TargetRegisterInfo *TRI,
1984 for (MCSubRegIterator Subreg(Reg, TRI, /* IncludeSelf */ true);
1985 Subreg.isValid(); ++Subreg)
1986 if (MI->getParent()->computeRegisterLiveness(TRI, *Subreg, MI) !=
1987 MachineBasicBlock::LQR_Dead)
1991 bool llvm::tryFoldSPUpdateIntoPushPop(const ARMSubtarget &Subtarget,
1992 MachineFunction &MF, MachineInstr *MI,
1993 unsigned NumBytes) {
1994 // This optimisation potentially adds lots of load and store
1995 // micro-operations, it's only really a great benefit to code-size.
1996 if (!MF.getFunction()->hasFnAttribute(Attribute::MinSize))
1999 // If only one register is pushed/popped, LLVM can use an LDR/STR
2000 // instead. We can't modify those so make sure we're dealing with an
2001 // instruction we understand.
2002 bool IsPop = isPopOpcode(MI->getOpcode());
2003 bool IsPush = isPushOpcode(MI->getOpcode());
2004 if (!IsPush && !IsPop)
2007 bool IsVFPPushPop = MI->getOpcode() == ARM::VSTMDDB_UPD ||
2008 MI->getOpcode() == ARM::VLDMDIA_UPD;
2009 bool IsT1PushPop = MI->getOpcode() == ARM::tPUSH ||
2010 MI->getOpcode() == ARM::tPOP ||
2011 MI->getOpcode() == ARM::tPOP_RET;
2013 assert((IsT1PushPop || (MI->getOperand(0).getReg() == ARM::SP &&
2014 MI->getOperand(1).getReg() == ARM::SP)) &&
2015 "trying to fold sp update into non-sp-updating push/pop");
2017 // The VFP push & pop act on D-registers, so we can only fold an adjustment
2018 // by a multiple of 8 bytes in correctly. Similarly rN is 4-bytes. Don't try
2019 // if this is violated.
2020 if (NumBytes % (IsVFPPushPop ? 8 : 4) != 0)
2023 // ARM and Thumb2 push/pop insts have explicit "sp, sp" operands (+
2024 // pred) so the list starts at 4. Thumb1 starts after the predicate.
2025 int RegListIdx = IsT1PushPop ? 2 : 4;
2027 // Calculate the space we'll need in terms of registers.
2028 unsigned FirstReg = MI->getOperand(RegListIdx).getReg();
2029 unsigned RD0Reg, RegsNeeded;
2032 RegsNeeded = NumBytes / 8;
2035 RegsNeeded = NumBytes / 4;
2038 // We're going to have to strip all list operands off before
2039 // re-adding them since the order matters, so save the existing ones
2041 SmallVector<MachineOperand, 4> RegList;
2042 for (int i = MI->getNumOperands() - 1; i >= RegListIdx; --i)
2043 RegList.push_back(MI->getOperand(i));
2045 const TargetRegisterInfo *TRI = MF.getRegInfo().getTargetRegisterInfo();
2046 const MCPhysReg *CSRegs = TRI->getCalleeSavedRegs(&MF);
2048 // Now try to find enough space in the reglist to allocate NumBytes.
2049 for (unsigned CurReg = FirstReg - 1; CurReg >= RD0Reg && RegsNeeded;
2052 // Pushing any register is completely harmless, mark the
2053 // register involved as undef since we don't care about it in
2055 RegList.push_back(MachineOperand::CreateReg(CurReg, false, false,
2056 false, false, true));
2061 // However, we can only pop an extra register if it's not live. For
2062 // registers live within the function we might clobber a return value
2063 // register; the other way a register can be live here is if it's
2065 // TODO: Currently, computeRegisterLiveness() does not report "live" if a
2066 // sub reg is live. When computeRegisterLiveness() works for sub reg, it
2067 // can replace isAnySubRegLive().
2068 if (isCalleeSavedRegister(CurReg, CSRegs) ||
2069 isAnySubRegLive(CurReg, TRI, MI)) {
2070 // VFP pops don't allow holes in the register list, so any skip is fatal
2071 // for our transformation. GPR pops do, so we should just keep looking.
2078 // Mark the unimportant registers as <def,dead> in the POP.
2079 RegList.push_back(MachineOperand::CreateReg(CurReg, true, false, false,
2087 // Finally we know we can profitably perform the optimisation so go
2088 // ahead: strip all existing registers off and add them back again
2089 // in the right order.
2090 for (int i = MI->getNumOperands() - 1; i >= RegListIdx; --i)
2091 MI->RemoveOperand(i);
2093 // Add the complete list back in.
2094 MachineInstrBuilder MIB(MF, &*MI);
2095 for (int i = RegList.size() - 1; i >= 0; --i)
2096 MIB.addOperand(RegList[i]);
2101 bool llvm::rewriteARMFrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
2102 unsigned FrameReg, int &Offset,
2103 const ARMBaseInstrInfo &TII) {
2104 unsigned Opcode = MI.getOpcode();
2105 const MCInstrDesc &Desc = MI.getDesc();
2106 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
2109 // Memory operands in inline assembly always use AddrMode2.
2110 if (Opcode == ARM::INLINEASM)
2111 AddrMode = ARMII::AddrMode2;
2113 if (Opcode == ARM::ADDri) {
2114 Offset += MI.getOperand(FrameRegIdx+1).getImm();
2116 // Turn it into a move.
2117 MI.setDesc(TII.get(ARM::MOVr));
2118 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
2119 MI.RemoveOperand(FrameRegIdx+1);
2122 } else if (Offset < 0) {
2125 MI.setDesc(TII.get(ARM::SUBri));
2128 // Common case: small offset, fits into instruction.
2129 if (ARM_AM::getSOImmVal(Offset) != -1) {
2130 // Replace the FrameIndex with sp / fp
2131 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
2132 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(Offset);
2137 // Otherwise, pull as much of the immedidate into this ADDri/SUBri
2139 unsigned RotAmt = ARM_AM::getSOImmValRotate(Offset);
2140 unsigned ThisImmVal = Offset & ARM_AM::rotr32(0xFF, RotAmt);
2142 // We will handle these bits from offset, clear them.
2143 Offset &= ~ThisImmVal;
2145 // Get the properly encoded SOImmVal field.
2146 assert(ARM_AM::getSOImmVal(ThisImmVal) != -1 &&
2147 "Bit extraction didn't work?");
2148 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(ThisImmVal);
2150 unsigned ImmIdx = 0;
2152 unsigned NumBits = 0;
2155 case ARMII::AddrMode_i12: {
2156 ImmIdx = FrameRegIdx + 1;
2157 InstrOffs = MI.getOperand(ImmIdx).getImm();
2161 case ARMII::AddrMode2: {
2162 ImmIdx = FrameRegIdx+2;
2163 InstrOffs = ARM_AM::getAM2Offset(MI.getOperand(ImmIdx).getImm());
2164 if (ARM_AM::getAM2Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
2169 case ARMII::AddrMode3: {
2170 ImmIdx = FrameRegIdx+2;
2171 InstrOffs = ARM_AM::getAM3Offset(MI.getOperand(ImmIdx).getImm());
2172 if (ARM_AM::getAM3Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
2177 case ARMII::AddrMode4:
2178 case ARMII::AddrMode6:
2179 // Can't fold any offset even if it's zero.
2181 case ARMII::AddrMode5: {
2182 ImmIdx = FrameRegIdx+1;
2183 InstrOffs = ARM_AM::getAM5Offset(MI.getOperand(ImmIdx).getImm());
2184 if (ARM_AM::getAM5Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
2191 llvm_unreachable("Unsupported addressing mode!");
2194 Offset += InstrOffs * Scale;
2195 assert((Offset & (Scale-1)) == 0 && "Can't encode this offset!");
2201 // Attempt to fold address comp. if opcode has offset bits
2203 // Common case: small offset, fits into instruction.
2204 MachineOperand &ImmOp = MI.getOperand(ImmIdx);
2205 int ImmedOffset = Offset / Scale;
2206 unsigned Mask = (1 << NumBits) - 1;
2207 if ((unsigned)Offset <= Mask * Scale) {
2208 // Replace the FrameIndex with sp
2209 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
2210 // FIXME: When addrmode2 goes away, this will simplify (like the
2211 // T2 version), as the LDR.i12 versions don't need the encoding
2212 // tricks for the offset value.
2214 if (AddrMode == ARMII::AddrMode_i12)
2215 ImmedOffset = -ImmedOffset;
2217 ImmedOffset |= 1 << NumBits;
2219 ImmOp.ChangeToImmediate(ImmedOffset);
2224 // Otherwise, it didn't fit. Pull in what we can to simplify the immed.
2225 ImmedOffset = ImmedOffset & Mask;
2227 if (AddrMode == ARMII::AddrMode_i12)
2228 ImmedOffset = -ImmedOffset;
2230 ImmedOffset |= 1 << NumBits;
2232 ImmOp.ChangeToImmediate(ImmedOffset);
2233 Offset &= ~(Mask*Scale);
2237 Offset = (isSub) ? -Offset : Offset;
2241 /// analyzeCompare - For a comparison instruction, return the source registers
2242 /// in SrcReg and SrcReg2 if having two register operands, and the value it
2243 /// compares against in CmpValue. Return true if the comparison instruction
2244 /// can be analyzed.
2245 bool ARMBaseInstrInfo::
2246 analyzeCompare(const MachineInstr *MI, unsigned &SrcReg, unsigned &SrcReg2,
2247 int &CmpMask, int &CmpValue) const {
2248 switch (MI->getOpcode()) {
2252 SrcReg = MI->getOperand(0).getReg();
2255 CmpValue = MI->getOperand(1).getImm();
2259 SrcReg = MI->getOperand(0).getReg();
2260 SrcReg2 = MI->getOperand(1).getReg();
2266 SrcReg = MI->getOperand(0).getReg();
2268 CmpMask = MI->getOperand(1).getImm();
2276 /// isSuitableForMask - Identify a suitable 'and' instruction that
2277 /// operates on the given source register and applies the same mask
2278 /// as a 'tst' instruction. Provide a limited look-through for copies.
2279 /// When successful, MI will hold the found instruction.
2280 static bool isSuitableForMask(MachineInstr *&MI, unsigned SrcReg,
2281 int CmpMask, bool CommonUse) {
2282 switch (MI->getOpcode()) {
2285 if (CmpMask != MI->getOperand(2).getImm())
2287 if (SrcReg == MI->getOperand(CommonUse ? 1 : 0).getReg())
2295 /// getSwappedCondition - assume the flags are set by MI(a,b), return
2296 /// the condition code if we modify the instructions such that flags are
2298 inline static ARMCC::CondCodes getSwappedCondition(ARMCC::CondCodes CC) {
2300 default: return ARMCC::AL;
2301 case ARMCC::EQ: return ARMCC::EQ;
2302 case ARMCC::NE: return ARMCC::NE;
2303 case ARMCC::HS: return ARMCC::LS;
2304 case ARMCC::LO: return ARMCC::HI;
2305 case ARMCC::HI: return ARMCC::LO;
2306 case ARMCC::LS: return ARMCC::HS;
2307 case ARMCC::GE: return ARMCC::LE;
2308 case ARMCC::LT: return ARMCC::GT;
2309 case ARMCC::GT: return ARMCC::LT;
2310 case ARMCC::LE: return ARMCC::GE;
2314 /// isRedundantFlagInstr - check whether the first instruction, whose only
2315 /// purpose is to update flags, can be made redundant.
2316 /// CMPrr can be made redundant by SUBrr if the operands are the same.
2317 /// CMPri can be made redundant by SUBri if the operands are the same.
2318 /// This function can be extended later on.
2319 inline static bool isRedundantFlagInstr(MachineInstr *CmpI, unsigned SrcReg,
2320 unsigned SrcReg2, int ImmValue,
2322 if ((CmpI->getOpcode() == ARM::CMPrr ||
2323 CmpI->getOpcode() == ARM::t2CMPrr) &&
2324 (OI->getOpcode() == ARM::SUBrr ||
2325 OI->getOpcode() == ARM::t2SUBrr) &&
2326 ((OI->getOperand(1).getReg() == SrcReg &&
2327 OI->getOperand(2).getReg() == SrcReg2) ||
2328 (OI->getOperand(1).getReg() == SrcReg2 &&
2329 OI->getOperand(2).getReg() == SrcReg)))
2332 if ((CmpI->getOpcode() == ARM::CMPri ||
2333 CmpI->getOpcode() == ARM::t2CMPri) &&
2334 (OI->getOpcode() == ARM::SUBri ||
2335 OI->getOpcode() == ARM::t2SUBri) &&
2336 OI->getOperand(1).getReg() == SrcReg &&
2337 OI->getOperand(2).getImm() == ImmValue)
2342 /// optimizeCompareInstr - Convert the instruction supplying the argument to the
2343 /// comparison into one that sets the zero bit in the flags register;
2344 /// Remove a redundant Compare instruction if an earlier instruction can set the
2345 /// flags in the same way as Compare.
2346 /// E.g. SUBrr(r1,r2) and CMPrr(r1,r2). We also handle the case where two
2347 /// operands are swapped: SUBrr(r1,r2) and CMPrr(r2,r1), by updating the
2348 /// condition code of instructions which use the flags.
2349 bool ARMBaseInstrInfo::
2350 optimizeCompareInstr(MachineInstr *CmpInstr, unsigned SrcReg, unsigned SrcReg2,
2351 int CmpMask, int CmpValue,
2352 const MachineRegisterInfo *MRI) const {
2353 // Get the unique definition of SrcReg.
2354 MachineInstr *MI = MRI->getUniqueVRegDef(SrcReg);
2355 if (!MI) return false;
2357 // Masked compares sometimes use the same register as the corresponding 'and'.
2358 if (CmpMask != ~0) {
2359 if (!isSuitableForMask(MI, SrcReg, CmpMask, false) || isPredicated(MI)) {
2361 for (MachineRegisterInfo::use_instr_iterator
2362 UI = MRI->use_instr_begin(SrcReg), UE = MRI->use_instr_end();
2364 if (UI->getParent() != CmpInstr->getParent()) continue;
2365 MachineInstr *PotentialAND = &*UI;
2366 if (!isSuitableForMask(PotentialAND, SrcReg, CmpMask, true) ||
2367 isPredicated(PotentialAND))
2372 if (!MI) return false;
2376 // Get ready to iterate backward from CmpInstr.
2377 MachineBasicBlock::iterator I = CmpInstr, E = MI,
2378 B = CmpInstr->getParent()->begin();
2380 // Early exit if CmpInstr is at the beginning of the BB.
2381 if (I == B) return false;
2383 // There are two possible candidates which can be changed to set CPSR:
2384 // One is MI, the other is a SUB instruction.
2385 // For CMPrr(r1,r2), we are looking for SUB(r1,r2) or SUB(r2,r1).
2386 // For CMPri(r1, CmpValue), we are looking for SUBri(r1, CmpValue).
2387 MachineInstr *Sub = nullptr;
2389 // MI is not a candidate for CMPrr.
2391 else if (MI->getParent() != CmpInstr->getParent() || CmpValue != 0) {
2392 // Conservatively refuse to convert an instruction which isn't in the same
2393 // BB as the comparison.
2394 // For CMPri w/ CmpValue != 0, a Sub may still be a candidate.
2395 // Thus we cannot return here.
2396 if (CmpInstr->getOpcode() == ARM::CMPri ||
2397 CmpInstr->getOpcode() == ARM::t2CMPri)
2403 // Check that CPSR isn't set between the comparison instruction and the one we
2404 // want to change. At the same time, search for Sub.
2405 const TargetRegisterInfo *TRI = &getRegisterInfo();
2407 for (; I != E; --I) {
2408 const MachineInstr &Instr = *I;
2410 if (Instr.modifiesRegister(ARM::CPSR, TRI) ||
2411 Instr.readsRegister(ARM::CPSR, TRI))
2412 // This instruction modifies or uses CPSR after the one we want to
2413 // change. We can't do this transformation.
2416 // Check whether CmpInstr can be made redundant by the current instruction.
2417 if (isRedundantFlagInstr(CmpInstr, SrcReg, SrcReg2, CmpValue, &*I)) {
2423 // The 'and' is below the comparison instruction.
2427 // Return false if no candidates exist.
2431 // The single candidate is called MI.
2434 // We can't use a predicated instruction - it doesn't always write the flags.
2435 if (isPredicated(MI))
2438 switch (MI->getOpcode()) {
2472 case ARM::t2EORri: {
2473 // Scan forward for the use of CPSR
2474 // When checking against MI: if it's a conditional code that requires
2475 // checking of the V bit or C bit, then this is not safe to do.
2476 // It is safe to remove CmpInstr if CPSR is redefined or killed.
2477 // If we are done with the basic block, we need to check whether CPSR is
2479 SmallVector<std::pair<MachineOperand*, ARMCC::CondCodes>, 4>
2481 bool isSafe = false;
2483 E = CmpInstr->getParent()->end();
2484 while (!isSafe && ++I != E) {
2485 const MachineInstr &Instr = *I;
2486 for (unsigned IO = 0, EO = Instr.getNumOperands();
2487 !isSafe && IO != EO; ++IO) {
2488 const MachineOperand &MO = Instr.getOperand(IO);
2489 if (MO.isRegMask() && MO.clobbersPhysReg(ARM::CPSR)) {
2493 if (!MO.isReg() || MO.getReg() != ARM::CPSR)
2499 // Condition code is after the operand before CPSR except for VSELs.
2500 ARMCC::CondCodes CC;
2501 bool IsInstrVSel = true;
2502 switch (Instr.getOpcode()) {
2504 IsInstrVSel = false;
2505 CC = (ARMCC::CondCodes)Instr.getOperand(IO - 1).getImm();
2526 ARMCC::CondCodes NewCC = getSwappedCondition(CC);
2527 if (NewCC == ARMCC::AL)
2529 // If we have SUB(r1, r2) and CMP(r2, r1), the condition code based
2530 // on CMP needs to be updated to be based on SUB.
2531 // Push the condition code operands to OperandsToUpdate.
2532 // If it is safe to remove CmpInstr, the condition code of these
2533 // operands will be modified.
2534 if (SrcReg2 != 0 && Sub->getOperand(1).getReg() == SrcReg2 &&
2535 Sub->getOperand(2).getReg() == SrcReg) {
2536 // VSel doesn't support condition code update.
2539 OperandsToUpdate.push_back(
2540 std::make_pair(&((*I).getOperand(IO - 1)), NewCC));
2543 // No Sub, so this is x = <op> y, z; cmp x, 0.
2545 case ARMCC::EQ: // Z
2546 case ARMCC::NE: // Z
2547 case ARMCC::MI: // N
2548 case ARMCC::PL: // N
2549 case ARMCC::AL: // none
2550 // CPSR can be used multiple times, we should continue.
2552 case ARMCC::HS: // C
2553 case ARMCC::LO: // C
2554 case ARMCC::VS: // V
2555 case ARMCC::VC: // V
2556 case ARMCC::HI: // C Z
2557 case ARMCC::LS: // C Z
2558 case ARMCC::GE: // N V
2559 case ARMCC::LT: // N V
2560 case ARMCC::GT: // Z N V
2561 case ARMCC::LE: // Z N V
2562 // The instruction uses the V bit or C bit which is not safe.
2569 // If CPSR is not killed nor re-defined, we should check whether it is
2570 // live-out. If it is live-out, do not optimize.
2572 MachineBasicBlock *MBB = CmpInstr->getParent();
2573 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
2574 SE = MBB->succ_end(); SI != SE; ++SI)
2575 if ((*SI)->isLiveIn(ARM::CPSR))
2579 // Toggle the optional operand to CPSR.
2580 MI->getOperand(5).setReg(ARM::CPSR);
2581 MI->getOperand(5).setIsDef(true);
2582 assert(!isPredicated(MI) && "Can't use flags from predicated instruction");
2583 CmpInstr->eraseFromParent();
2585 // Modify the condition code of operands in OperandsToUpdate.
2586 // Since we have SUB(r1, r2) and CMP(r2, r1), the condition code needs to
2587 // be changed from r2 > r1 to r1 < r2, from r2 < r1 to r1 > r2, etc.
2588 for (unsigned i = 0, e = OperandsToUpdate.size(); i < e; i++)
2589 OperandsToUpdate[i].first->setImm(OperandsToUpdate[i].second);
2597 bool ARMBaseInstrInfo::FoldImmediate(MachineInstr *UseMI,
2598 MachineInstr *DefMI, unsigned Reg,
2599 MachineRegisterInfo *MRI) const {
2600 // Fold large immediates into add, sub, or, xor.
2601 unsigned DefOpc = DefMI->getOpcode();
2602 if (DefOpc != ARM::t2MOVi32imm && DefOpc != ARM::MOVi32imm)
2604 if (!DefMI->getOperand(1).isImm())
2605 // Could be t2MOVi32imm <ga:xx>
2608 if (!MRI->hasOneNonDBGUse(Reg))
2611 const MCInstrDesc &DefMCID = DefMI->getDesc();
2612 if (DefMCID.hasOptionalDef()) {
2613 unsigned NumOps = DefMCID.getNumOperands();
2614 const MachineOperand &MO = DefMI->getOperand(NumOps-1);
2615 if (MO.getReg() == ARM::CPSR && !MO.isDead())
2616 // If DefMI defines CPSR and it is not dead, it's obviously not safe
2621 const MCInstrDesc &UseMCID = UseMI->getDesc();
2622 if (UseMCID.hasOptionalDef()) {
2623 unsigned NumOps = UseMCID.getNumOperands();
2624 if (UseMI->getOperand(NumOps-1).getReg() == ARM::CPSR)
2625 // If the instruction sets the flag, do not attempt this optimization
2626 // since it may change the semantics of the code.
2630 unsigned UseOpc = UseMI->getOpcode();
2631 unsigned NewUseOpc = 0;
2632 uint32_t ImmVal = (uint32_t)DefMI->getOperand(1).getImm();
2633 uint32_t SOImmValV1 = 0, SOImmValV2 = 0;
2634 bool Commute = false;
2636 default: return false;
2644 case ARM::t2EORrr: {
2645 Commute = UseMI->getOperand(2).getReg() != Reg;
2652 NewUseOpc = ARM::SUBri;
2658 if (!ARM_AM::isSOImmTwoPartVal(ImmVal))
2660 SOImmValV1 = (uint32_t)ARM_AM::getSOImmTwoPartFirst(ImmVal);
2661 SOImmValV2 = (uint32_t)ARM_AM::getSOImmTwoPartSecond(ImmVal);
2664 case ARM::ADDrr: NewUseOpc = ARM::ADDri; break;
2665 case ARM::ORRrr: NewUseOpc = ARM::ORRri; break;
2666 case ARM::EORrr: NewUseOpc = ARM::EORri; break;
2670 case ARM::t2SUBrr: {
2674 NewUseOpc = ARM::t2SUBri;
2679 case ARM::t2EORrr: {
2680 if (!ARM_AM::isT2SOImmTwoPartVal(ImmVal))
2682 SOImmValV1 = (uint32_t)ARM_AM::getT2SOImmTwoPartFirst(ImmVal);
2683 SOImmValV2 = (uint32_t)ARM_AM::getT2SOImmTwoPartSecond(ImmVal);
2686 case ARM::t2ADDrr: NewUseOpc = ARM::t2ADDri; break;
2687 case ARM::t2ORRrr: NewUseOpc = ARM::t2ORRri; break;
2688 case ARM::t2EORrr: NewUseOpc = ARM::t2EORri; break;
2696 unsigned OpIdx = Commute ? 2 : 1;
2697 unsigned Reg1 = UseMI->getOperand(OpIdx).getReg();
2698 bool isKill = UseMI->getOperand(OpIdx).isKill();
2699 unsigned NewReg = MRI->createVirtualRegister(MRI->getRegClass(Reg));
2700 AddDefaultCC(AddDefaultPred(BuildMI(*UseMI->getParent(),
2701 UseMI, UseMI->getDebugLoc(),
2702 get(NewUseOpc), NewReg)
2703 .addReg(Reg1, getKillRegState(isKill))
2704 .addImm(SOImmValV1)));
2705 UseMI->setDesc(get(NewUseOpc));
2706 UseMI->getOperand(1).setReg(NewReg);
2707 UseMI->getOperand(1).setIsKill();
2708 UseMI->getOperand(2).ChangeToImmediate(SOImmValV2);
2709 DefMI->eraseFromParent();
2713 static unsigned getNumMicroOpsSwiftLdSt(const InstrItineraryData *ItinData,
2714 const MachineInstr *MI) {
2715 switch (MI->getOpcode()) {
2717 const MCInstrDesc &Desc = MI->getDesc();
2718 int UOps = ItinData->getNumMicroOps(Desc.getSchedClass());
2719 assert(UOps >= 0 && "bad # UOps");
2727 unsigned ShOpVal = MI->getOperand(3).getImm();
2728 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2729 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2732 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2733 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2740 if (!MI->getOperand(2).getReg())
2743 unsigned ShOpVal = MI->getOperand(3).getImm();
2744 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2745 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2748 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2749 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2756 return (ARM_AM::getAM3Op(MI->getOperand(3).getImm()) == ARM_AM::sub) ? 3:2;
2758 case ARM::LDRSB_POST:
2759 case ARM::LDRSH_POST: {
2760 unsigned Rt = MI->getOperand(0).getReg();
2761 unsigned Rm = MI->getOperand(3).getReg();
2762 return (Rt == Rm) ? 4 : 3;
2765 case ARM::LDR_PRE_REG:
2766 case ARM::LDRB_PRE_REG: {
2767 unsigned Rt = MI->getOperand(0).getReg();
2768 unsigned Rm = MI->getOperand(3).getReg();
2771 unsigned ShOpVal = MI->getOperand(4).getImm();
2772 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2773 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2776 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2777 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2782 case ARM::STR_PRE_REG:
2783 case ARM::STRB_PRE_REG: {
2784 unsigned ShOpVal = MI->getOperand(4).getImm();
2785 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2786 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2789 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2790 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2796 case ARM::STRH_PRE: {
2797 unsigned Rt = MI->getOperand(0).getReg();
2798 unsigned Rm = MI->getOperand(3).getReg();
2803 return (ARM_AM::getAM3Op(MI->getOperand(4).getImm()) == ARM_AM::sub)
2807 case ARM::LDR_POST_REG:
2808 case ARM::LDRB_POST_REG:
2809 case ARM::LDRH_POST: {
2810 unsigned Rt = MI->getOperand(0).getReg();
2811 unsigned Rm = MI->getOperand(3).getReg();
2812 return (Rt == Rm) ? 3 : 2;
2815 case ARM::LDR_PRE_IMM:
2816 case ARM::LDRB_PRE_IMM:
2817 case ARM::LDR_POST_IMM:
2818 case ARM::LDRB_POST_IMM:
2819 case ARM::STRB_POST_IMM:
2820 case ARM::STRB_POST_REG:
2821 case ARM::STRB_PRE_IMM:
2822 case ARM::STRH_POST:
2823 case ARM::STR_POST_IMM:
2824 case ARM::STR_POST_REG:
2825 case ARM::STR_PRE_IMM:
2828 case ARM::LDRSB_PRE:
2829 case ARM::LDRSH_PRE: {
2830 unsigned Rm = MI->getOperand(3).getReg();
2833 unsigned Rt = MI->getOperand(0).getReg();
2836 unsigned ShOpVal = MI->getOperand(4).getImm();
2837 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2838 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2841 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2842 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2848 unsigned Rt = MI->getOperand(0).getReg();
2849 unsigned Rn = MI->getOperand(2).getReg();
2850 unsigned Rm = MI->getOperand(3).getReg();
2852 return (ARM_AM::getAM3Op(MI->getOperand(4).getImm()) == ARM_AM::sub) ?4:3;
2853 return (Rt == Rn) ? 3 : 2;
2857 unsigned Rm = MI->getOperand(3).getReg();
2859 return (ARM_AM::getAM3Op(MI->getOperand(4).getImm()) == ARM_AM::sub) ?4:3;
2863 case ARM::LDRD_POST:
2864 case ARM::t2LDRD_POST:
2867 case ARM::STRD_POST:
2868 case ARM::t2STRD_POST:
2871 case ARM::LDRD_PRE: {
2872 unsigned Rt = MI->getOperand(0).getReg();
2873 unsigned Rn = MI->getOperand(3).getReg();
2874 unsigned Rm = MI->getOperand(4).getReg();
2876 return (ARM_AM::getAM3Op(MI->getOperand(5).getImm()) == ARM_AM::sub) ?5:4;
2877 return (Rt == Rn) ? 4 : 3;
2880 case ARM::t2LDRD_PRE: {
2881 unsigned Rt = MI->getOperand(0).getReg();
2882 unsigned Rn = MI->getOperand(3).getReg();
2883 return (Rt == Rn) ? 4 : 3;
2886 case ARM::STRD_PRE: {
2887 unsigned Rm = MI->getOperand(4).getReg();
2889 return (ARM_AM::getAM3Op(MI->getOperand(5).getImm()) == ARM_AM::sub) ?5:4;
2893 case ARM::t2STRD_PRE:
2896 case ARM::t2LDR_POST:
2897 case ARM::t2LDRB_POST:
2898 case ARM::t2LDRB_PRE:
2899 case ARM::t2LDRSBi12:
2900 case ARM::t2LDRSBi8:
2901 case ARM::t2LDRSBpci:
2903 case ARM::t2LDRH_POST:
2904 case ARM::t2LDRH_PRE:
2906 case ARM::t2LDRSB_POST:
2907 case ARM::t2LDRSB_PRE:
2908 case ARM::t2LDRSH_POST:
2909 case ARM::t2LDRSH_PRE:
2910 case ARM::t2LDRSHi12:
2911 case ARM::t2LDRSHi8:
2912 case ARM::t2LDRSHpci:
2916 case ARM::t2LDRDi8: {
2917 unsigned Rt = MI->getOperand(0).getReg();
2918 unsigned Rn = MI->getOperand(2).getReg();
2919 return (Rt == Rn) ? 3 : 2;
2922 case ARM::t2STRB_POST:
2923 case ARM::t2STRB_PRE:
2926 case ARM::t2STRH_POST:
2927 case ARM::t2STRH_PRE:
2929 case ARM::t2STR_POST:
2930 case ARM::t2STR_PRE:
2936 // Return the number of 32-bit words loaded by LDM or stored by STM. If this
2937 // can't be easily determined return 0 (missing MachineMemOperand).
2939 // FIXME: The current MachineInstr design does not support relying on machine
2940 // mem operands to determine the width of a memory access. Instead, we expect
2941 // the target to provide this information based on the instruction opcode and
2942 // operands. However, using MachineMemOperand is the best solution now for
2945 // 1) getNumMicroOps tries to infer LDM memory width from the total number of MI
2946 // operands. This is much more dangerous than using the MachineMemOperand
2947 // sizes because CodeGen passes can insert/remove optional machine operands. In
2948 // fact, it's totally incorrect for preRA passes and appears to be wrong for
2949 // postRA passes as well.
2951 // 2) getNumLDMAddresses is only used by the scheduling machine model and any
2952 // machine model that calls this should handle the unknown (zero size) case.
2954 // Long term, we should require a target hook that verifies MachineMemOperand
2955 // sizes during MC lowering. That target hook should be local to MC lowering
2956 // because we can't ensure that it is aware of other MI forms. Doing this will
2957 // ensure that MachineMemOperands are correctly propagated through all passes.
2958 unsigned ARMBaseInstrInfo::getNumLDMAddresses(const MachineInstr *MI) const {
2960 for (MachineInstr::mmo_iterator I = MI->memoperands_begin(),
2961 E = MI->memoperands_end(); I != E; ++I) {
2962 Size += (*I)->getSize();
2968 ARMBaseInstrInfo::getNumMicroOps(const InstrItineraryData *ItinData,
2969 const MachineInstr *MI) const {
2970 if (!ItinData || ItinData->isEmpty())
2973 const MCInstrDesc &Desc = MI->getDesc();
2974 unsigned Class = Desc.getSchedClass();
2975 int ItinUOps = ItinData->getNumMicroOps(Class);
2976 if (ItinUOps >= 0) {
2977 if (Subtarget.isSwift() && (Desc.mayLoad() || Desc.mayStore()))
2978 return getNumMicroOpsSwiftLdSt(ItinData, MI);
2983 unsigned Opc = MI->getOpcode();
2986 llvm_unreachable("Unexpected multi-uops instruction!");
2991 // The number of uOps for load / store multiple are determined by the number
2994 // On Cortex-A8, each pair of register loads / stores can be scheduled on the
2995 // same cycle. The scheduling for the first load / store must be done
2996 // separately by assuming the address is not 64-bit aligned.
2998 // On Cortex-A9, the formula is simply (#reg / 2) + (#reg % 2). If the address
2999 // is not 64-bit aligned, then AGU would take an extra cycle. For VFP / NEON
3000 // load / store multiple, the formula is (#reg / 2) + (#reg % 2) + 1.
3002 case ARM::VLDMDIA_UPD:
3003 case ARM::VLDMDDB_UPD:
3005 case ARM::VLDMSIA_UPD:
3006 case ARM::VLDMSDB_UPD:
3008 case ARM::VSTMDIA_UPD:
3009 case ARM::VSTMDDB_UPD:
3011 case ARM::VSTMSIA_UPD:
3012 case ARM::VSTMSDB_UPD: {
3013 unsigned NumRegs = MI->getNumOperands() - Desc.getNumOperands();
3014 return (NumRegs / 2) + (NumRegs % 2) + 1;
3017 case ARM::LDMIA_RET:
3022 case ARM::LDMIA_UPD:
3023 case ARM::LDMDA_UPD:
3024 case ARM::LDMDB_UPD:
3025 case ARM::LDMIB_UPD:
3030 case ARM::STMIA_UPD:
3031 case ARM::STMDA_UPD:
3032 case ARM::STMDB_UPD:
3033 case ARM::STMIB_UPD:
3035 case ARM::tLDMIA_UPD:
3036 case ARM::tSTMIA_UPD:
3040 case ARM::t2LDMIA_RET:
3043 case ARM::t2LDMIA_UPD:
3044 case ARM::t2LDMDB_UPD:
3047 case ARM::t2STMIA_UPD:
3048 case ARM::t2STMDB_UPD: {
3049 unsigned NumRegs = MI->getNumOperands() - Desc.getNumOperands() + 1;
3050 if (Subtarget.isSwift()) {
3051 int UOps = 1 + NumRegs; // One for address computation, one for each ld / st.
3054 case ARM::VLDMDIA_UPD:
3055 case ARM::VLDMDDB_UPD:
3056 case ARM::VLDMSIA_UPD:
3057 case ARM::VLDMSDB_UPD:
3058 case ARM::VSTMDIA_UPD:
3059 case ARM::VSTMDDB_UPD:
3060 case ARM::VSTMSIA_UPD:
3061 case ARM::VSTMSDB_UPD:
3062 case ARM::LDMIA_UPD:
3063 case ARM::LDMDA_UPD:
3064 case ARM::LDMDB_UPD:
3065 case ARM::LDMIB_UPD:
3066 case ARM::STMIA_UPD:
3067 case ARM::STMDA_UPD:
3068 case ARM::STMDB_UPD:
3069 case ARM::STMIB_UPD:
3070 case ARM::tLDMIA_UPD:
3071 case ARM::tSTMIA_UPD:
3072 case ARM::t2LDMIA_UPD:
3073 case ARM::t2LDMDB_UPD:
3074 case ARM::t2STMIA_UPD:
3075 case ARM::t2STMDB_UPD:
3076 ++UOps; // One for base register writeback.
3078 case ARM::LDMIA_RET:
3080 case ARM::t2LDMIA_RET:
3081 UOps += 2; // One for base reg wb, one for write to pc.
3085 } else if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
3088 // 4 registers would be issued: 2, 2.
3089 // 5 registers would be issued: 2, 2, 1.
3090 int A8UOps = (NumRegs / 2);
3094 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
3095 int A9UOps = (NumRegs / 2);
3096 // If there are odd number of registers or if it's not 64-bit aligned,
3097 // then it takes an extra AGU (Address Generation Unit) cycle.
3098 if ((NumRegs % 2) ||
3099 !MI->hasOneMemOperand() ||
3100 (*MI->memoperands_begin())->getAlignment() < 8)
3104 // Assume the worst.
3112 ARMBaseInstrInfo::getVLDMDefCycle(const InstrItineraryData *ItinData,
3113 const MCInstrDesc &DefMCID,
3115 unsigned DefIdx, unsigned DefAlign) const {
3116 int RegNo = (int)(DefIdx+1) - DefMCID.getNumOperands() + 1;
3118 // Def is the address writeback.
3119 return ItinData->getOperandCycle(DefClass, DefIdx);
3122 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
3123 // (regno / 2) + (regno % 2) + 1
3124 DefCycle = RegNo / 2 + 1;
3127 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
3129 bool isSLoad = false;
3131 switch (DefMCID.getOpcode()) {
3134 case ARM::VLDMSIA_UPD:
3135 case ARM::VLDMSDB_UPD:
3140 // If there are odd number of 'S' registers or if it's not 64-bit aligned,
3141 // then it takes an extra cycle.
3142 if ((isSLoad && (RegNo % 2)) || DefAlign < 8)
3145 // Assume the worst.
3146 DefCycle = RegNo + 2;
3153 ARMBaseInstrInfo::getLDMDefCycle(const InstrItineraryData *ItinData,
3154 const MCInstrDesc &DefMCID,
3156 unsigned DefIdx, unsigned DefAlign) const {
3157 int RegNo = (int)(DefIdx+1) - DefMCID.getNumOperands() + 1;
3159 // Def is the address writeback.
3160 return ItinData->getOperandCycle(DefClass, DefIdx);
3163 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
3164 // 4 registers would be issued: 1, 2, 1.
3165 // 5 registers would be issued: 1, 2, 2.
3166 DefCycle = RegNo / 2;
3169 // Result latency is issue cycle + 2: E2.
3171 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
3172 DefCycle = (RegNo / 2);
3173 // If there are odd number of registers or if it's not 64-bit aligned,
3174 // then it takes an extra AGU (Address Generation Unit) cycle.
3175 if ((RegNo % 2) || DefAlign < 8)
3177 // Result latency is AGU cycles + 2.
3180 // Assume the worst.
3181 DefCycle = RegNo + 2;
3188 ARMBaseInstrInfo::getVSTMUseCycle(const InstrItineraryData *ItinData,
3189 const MCInstrDesc &UseMCID,
3191 unsigned UseIdx, unsigned UseAlign) const {
3192 int RegNo = (int)(UseIdx+1) - UseMCID.getNumOperands() + 1;
3194 return ItinData->getOperandCycle(UseClass, UseIdx);
3197 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
3198 // (regno / 2) + (regno % 2) + 1
3199 UseCycle = RegNo / 2 + 1;
3202 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
3204 bool isSStore = false;
3206 switch (UseMCID.getOpcode()) {
3209 case ARM::VSTMSIA_UPD:
3210 case ARM::VSTMSDB_UPD:
3215 // If there are odd number of 'S' registers or if it's not 64-bit aligned,
3216 // then it takes an extra cycle.
3217 if ((isSStore && (RegNo % 2)) || UseAlign < 8)
3220 // Assume the worst.
3221 UseCycle = RegNo + 2;
3228 ARMBaseInstrInfo::getSTMUseCycle(const InstrItineraryData *ItinData,
3229 const MCInstrDesc &UseMCID,
3231 unsigned UseIdx, unsigned UseAlign) const {
3232 int RegNo = (int)(UseIdx+1) - UseMCID.getNumOperands() + 1;
3234 return ItinData->getOperandCycle(UseClass, UseIdx);
3237 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
3238 UseCycle = RegNo / 2;
3243 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
3244 UseCycle = (RegNo / 2);
3245 // If there are odd number of registers or if it's not 64-bit aligned,
3246 // then it takes an extra AGU (Address Generation Unit) cycle.
3247 if ((RegNo % 2) || UseAlign < 8)
3250 // Assume the worst.
3257 ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
3258 const MCInstrDesc &DefMCID,
3259 unsigned DefIdx, unsigned DefAlign,
3260 const MCInstrDesc &UseMCID,
3261 unsigned UseIdx, unsigned UseAlign) const {
3262 unsigned DefClass = DefMCID.getSchedClass();
3263 unsigned UseClass = UseMCID.getSchedClass();
3265 if (DefIdx < DefMCID.getNumDefs() && UseIdx < UseMCID.getNumOperands())
3266 return ItinData->getOperandLatency(DefClass, DefIdx, UseClass, UseIdx);
3268 // This may be a def / use of a variable_ops instruction, the operand
3269 // latency might be determinable dynamically. Let the target try to
3272 bool LdmBypass = false;
3273 switch (DefMCID.getOpcode()) {
3275 DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
3279 case ARM::VLDMDIA_UPD:
3280 case ARM::VLDMDDB_UPD:
3282 case ARM::VLDMSIA_UPD:
3283 case ARM::VLDMSDB_UPD:
3284 DefCycle = getVLDMDefCycle(ItinData, DefMCID, DefClass, DefIdx, DefAlign);
3287 case ARM::LDMIA_RET:
3292 case ARM::LDMIA_UPD:
3293 case ARM::LDMDA_UPD:
3294 case ARM::LDMDB_UPD:
3295 case ARM::LDMIB_UPD:
3297 case ARM::tLDMIA_UPD:
3299 case ARM::t2LDMIA_RET:
3302 case ARM::t2LDMIA_UPD:
3303 case ARM::t2LDMDB_UPD:
3305 DefCycle = getLDMDefCycle(ItinData, DefMCID, DefClass, DefIdx, DefAlign);
3310 // We can't seem to determine the result latency of the def, assume it's 2.
3314 switch (UseMCID.getOpcode()) {
3316 UseCycle = ItinData->getOperandCycle(UseClass, UseIdx);
3320 case ARM::VSTMDIA_UPD:
3321 case ARM::VSTMDDB_UPD:
3323 case ARM::VSTMSIA_UPD:
3324 case ARM::VSTMSDB_UPD:
3325 UseCycle = getVSTMUseCycle(ItinData, UseMCID, UseClass, UseIdx, UseAlign);
3332 case ARM::STMIA_UPD:
3333 case ARM::STMDA_UPD:
3334 case ARM::STMDB_UPD:
3335 case ARM::STMIB_UPD:
3336 case ARM::tSTMIA_UPD:
3341 case ARM::t2STMIA_UPD:
3342 case ARM::t2STMDB_UPD:
3343 UseCycle = getSTMUseCycle(ItinData, UseMCID, UseClass, UseIdx, UseAlign);
3348 // Assume it's read in the first stage.
3351 UseCycle = DefCycle - UseCycle + 1;
3354 // It's a variable_ops instruction so we can't use DefIdx here. Just use
3355 // first def operand.
3356 if (ItinData->hasPipelineForwarding(DefClass, DefMCID.getNumOperands()-1,
3359 } else if (ItinData->hasPipelineForwarding(DefClass, DefIdx,
3360 UseClass, UseIdx)) {
3368 static const MachineInstr *getBundledDefMI(const TargetRegisterInfo *TRI,
3369 const MachineInstr *MI, unsigned Reg,
3370 unsigned &DefIdx, unsigned &Dist) {
3373 MachineBasicBlock::const_iterator I = MI; ++I;
3374 MachineBasicBlock::const_instr_iterator II = std::prev(I.getInstrIterator());
3375 assert(II->isInsideBundle() && "Empty bundle?");
3378 while (II->isInsideBundle()) {
3379 Idx = II->findRegisterDefOperandIdx(Reg, false, true, TRI);
3386 assert(Idx != -1 && "Cannot find bundled definition!");
3391 static const MachineInstr *getBundledUseMI(const TargetRegisterInfo *TRI,
3392 const MachineInstr *MI, unsigned Reg,
3393 unsigned &UseIdx, unsigned &Dist) {
3396 MachineBasicBlock::const_instr_iterator II = MI; ++II;
3397 assert(II->isInsideBundle() && "Empty bundle?");
3398 MachineBasicBlock::const_instr_iterator E = MI->getParent()->instr_end();
3400 // FIXME: This doesn't properly handle multiple uses.
3402 while (II != E && II->isInsideBundle()) {
3403 Idx = II->findRegisterUseOperandIdx(Reg, false, TRI);
3406 if (II->getOpcode() != ARM::t2IT)
3420 /// Return the number of cycles to add to (or subtract from) the static
3421 /// itinerary based on the def opcode and alignment. The caller will ensure that
3422 /// adjusted latency is at least one cycle.
3423 static int adjustDefLatency(const ARMSubtarget &Subtarget,
3424 const MachineInstr *DefMI,
3425 const MCInstrDesc *DefMCID, unsigned DefAlign) {
3427 if (Subtarget.isCortexA8() || Subtarget.isLikeA9() || Subtarget.isCortexA7()) {
3428 // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2]
3429 // variants are one cycle cheaper.
3430 switch (DefMCID->getOpcode()) {
3434 unsigned ShOpVal = DefMI->getOperand(3).getImm();
3435 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3437 (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
3444 case ARM::t2LDRSHs: {
3445 // Thumb2 mode: lsl only.
3446 unsigned ShAmt = DefMI->getOperand(3).getImm();
3447 if (ShAmt == 0 || ShAmt == 2)
3452 } else if (Subtarget.isSwift()) {
3453 // FIXME: Properly handle all of the latency adjustments for address
3455 switch (DefMCID->getOpcode()) {
3459 unsigned ShOpVal = DefMI->getOperand(3).getImm();
3460 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
3461 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3464 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
3465 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
3468 ShImm == 1 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsr)
3475 case ARM::t2LDRSHs: {
3476 // Thumb2 mode: lsl only.
3477 unsigned ShAmt = DefMI->getOperand(3).getImm();
3478 if (ShAmt == 0 || ShAmt == 1 || ShAmt == 2 || ShAmt == 3)
3485 if (DefAlign < 8 && Subtarget.isLikeA9()) {
3486 switch (DefMCID->getOpcode()) {
3492 case ARM::VLD1q8wb_fixed:
3493 case ARM::VLD1q16wb_fixed:
3494 case ARM::VLD1q32wb_fixed:
3495 case ARM::VLD1q64wb_fixed:
3496 case ARM::VLD1q8wb_register:
3497 case ARM::VLD1q16wb_register:
3498 case ARM::VLD1q32wb_register:
3499 case ARM::VLD1q64wb_register:
3506 case ARM::VLD2d8wb_fixed:
3507 case ARM::VLD2d16wb_fixed:
3508 case ARM::VLD2d32wb_fixed:
3509 case ARM::VLD2q8wb_fixed:
3510 case ARM::VLD2q16wb_fixed:
3511 case ARM::VLD2q32wb_fixed:
3512 case ARM::VLD2d8wb_register:
3513 case ARM::VLD2d16wb_register:
3514 case ARM::VLD2d32wb_register:
3515 case ARM::VLD2q8wb_register:
3516 case ARM::VLD2q16wb_register:
3517 case ARM::VLD2q32wb_register:
3522 case ARM::VLD3d8_UPD:
3523 case ARM::VLD3d16_UPD:
3524 case ARM::VLD3d32_UPD:
3525 case ARM::VLD1d64Twb_fixed:
3526 case ARM::VLD1d64Twb_register:
3527 case ARM::VLD3q8_UPD:
3528 case ARM::VLD3q16_UPD:
3529 case ARM::VLD3q32_UPD:
3534 case ARM::VLD4d8_UPD:
3535 case ARM::VLD4d16_UPD:
3536 case ARM::VLD4d32_UPD:
3537 case ARM::VLD1d64Qwb_fixed:
3538 case ARM::VLD1d64Qwb_register:
3539 case ARM::VLD4q8_UPD:
3540 case ARM::VLD4q16_UPD:
3541 case ARM::VLD4q32_UPD:
3542 case ARM::VLD1DUPq8:
3543 case ARM::VLD1DUPq16:
3544 case ARM::VLD1DUPq32:
3545 case ARM::VLD1DUPq8wb_fixed:
3546 case ARM::VLD1DUPq16wb_fixed:
3547 case ARM::VLD1DUPq32wb_fixed:
3548 case ARM::VLD1DUPq8wb_register:
3549 case ARM::VLD1DUPq16wb_register:
3550 case ARM::VLD1DUPq32wb_register:
3551 case ARM::VLD2DUPd8:
3552 case ARM::VLD2DUPd16:
3553 case ARM::VLD2DUPd32:
3554 case ARM::VLD2DUPd8wb_fixed:
3555 case ARM::VLD2DUPd16wb_fixed:
3556 case ARM::VLD2DUPd32wb_fixed:
3557 case ARM::VLD2DUPd8wb_register:
3558 case ARM::VLD2DUPd16wb_register:
3559 case ARM::VLD2DUPd32wb_register:
3560 case ARM::VLD4DUPd8:
3561 case ARM::VLD4DUPd16:
3562 case ARM::VLD4DUPd32:
3563 case ARM::VLD4DUPd8_UPD:
3564 case ARM::VLD4DUPd16_UPD:
3565 case ARM::VLD4DUPd32_UPD:
3567 case ARM::VLD1LNd16:
3568 case ARM::VLD1LNd32:
3569 case ARM::VLD1LNd8_UPD:
3570 case ARM::VLD1LNd16_UPD:
3571 case ARM::VLD1LNd32_UPD:
3573 case ARM::VLD2LNd16:
3574 case ARM::VLD2LNd32:
3575 case ARM::VLD2LNq16:
3576 case ARM::VLD2LNq32:
3577 case ARM::VLD2LNd8_UPD:
3578 case ARM::VLD2LNd16_UPD:
3579 case ARM::VLD2LNd32_UPD:
3580 case ARM::VLD2LNq16_UPD:
3581 case ARM::VLD2LNq32_UPD:
3583 case ARM::VLD4LNd16:
3584 case ARM::VLD4LNd32:
3585 case ARM::VLD4LNq16:
3586 case ARM::VLD4LNq32:
3587 case ARM::VLD4LNd8_UPD:
3588 case ARM::VLD4LNd16_UPD:
3589 case ARM::VLD4LNd32_UPD:
3590 case ARM::VLD4LNq16_UPD:
3591 case ARM::VLD4LNq32_UPD:
3592 // If the address is not 64-bit aligned, the latencies of these
3593 // instructions increases by one.
3604 ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
3605 const MachineInstr *DefMI, unsigned DefIdx,
3606 const MachineInstr *UseMI,
3607 unsigned UseIdx) const {
3608 // No operand latency. The caller may fall back to getInstrLatency.
3609 if (!ItinData || ItinData->isEmpty())
3612 const MachineOperand &DefMO = DefMI->getOperand(DefIdx);
3613 unsigned Reg = DefMO.getReg();
3614 const MCInstrDesc *DefMCID = &DefMI->getDesc();
3615 const MCInstrDesc *UseMCID = &UseMI->getDesc();
3617 unsigned DefAdj = 0;
3618 if (DefMI->isBundle()) {
3619 DefMI = getBundledDefMI(&getRegisterInfo(), DefMI, Reg, DefIdx, DefAdj);
3620 DefMCID = &DefMI->getDesc();
3622 if (DefMI->isCopyLike() || DefMI->isInsertSubreg() ||
3623 DefMI->isRegSequence() || DefMI->isImplicitDef()) {
3627 unsigned UseAdj = 0;
3628 if (UseMI->isBundle()) {
3630 const MachineInstr *NewUseMI = getBundledUseMI(&getRegisterInfo(), UseMI,
3631 Reg, NewUseIdx, UseAdj);
3637 UseMCID = &UseMI->getDesc();
3640 if (Reg == ARM::CPSR) {
3641 if (DefMI->getOpcode() == ARM::FMSTAT) {
3642 // fpscr -> cpsr stalls over 20 cycles on A8 (and earlier?)
3643 return Subtarget.isLikeA9() ? 1 : 20;
3646 // CPSR set and branch can be paired in the same cycle.
3647 if (UseMI->isBranch())
3650 // Otherwise it takes the instruction latency (generally one).
3651 unsigned Latency = getInstrLatency(ItinData, DefMI);
3653 // For Thumb2 and -Os, prefer scheduling CPSR setting instruction close to
3654 // its uses. Instructions which are otherwise scheduled between them may
3655 // incur a code size penalty (not able to use the CPSR setting 16-bit
3657 if (Latency > 0 && Subtarget.isThumb2()) {
3658 const MachineFunction *MF = DefMI->getParent()->getParent();
3659 if (MF->getFunction()->hasFnAttribute(Attribute::OptimizeForSize))
3665 if (DefMO.isImplicit() || UseMI->getOperand(UseIdx).isImplicit())
3668 unsigned DefAlign = DefMI->hasOneMemOperand()
3669 ? (*DefMI->memoperands_begin())->getAlignment() : 0;
3670 unsigned UseAlign = UseMI->hasOneMemOperand()
3671 ? (*UseMI->memoperands_begin())->getAlignment() : 0;
3673 // Get the itinerary's latency if possible, and handle variable_ops.
3674 int Latency = getOperandLatency(ItinData, *DefMCID, DefIdx, DefAlign,
3675 *UseMCID, UseIdx, UseAlign);
3676 // Unable to find operand latency. The caller may resort to getInstrLatency.
3680 // Adjust for IT block position.
3681 int Adj = DefAdj + UseAdj;
3683 // Adjust for dynamic def-side opcode variants not captured by the itinerary.
3684 Adj += adjustDefLatency(Subtarget, DefMI, DefMCID, DefAlign);
3685 if (Adj >= 0 || (int)Latency > -Adj) {
3686 return Latency + Adj;
3688 // Return the itinerary latency, which may be zero but not less than zero.
3693 ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
3694 SDNode *DefNode, unsigned DefIdx,
3695 SDNode *UseNode, unsigned UseIdx) const {
3696 if (!DefNode->isMachineOpcode())
3699 const MCInstrDesc &DefMCID = get(DefNode->getMachineOpcode());
3701 if (isZeroCost(DefMCID.Opcode))
3704 if (!ItinData || ItinData->isEmpty())
3705 return DefMCID.mayLoad() ? 3 : 1;
3707 if (!UseNode->isMachineOpcode()) {
3708 int Latency = ItinData->getOperandCycle(DefMCID.getSchedClass(), DefIdx);
3709 if (Subtarget.isLikeA9() || Subtarget.isSwift())
3710 return Latency <= 2 ? 1 : Latency - 1;
3712 return Latency <= 3 ? 1 : Latency - 2;
3715 const MCInstrDesc &UseMCID = get(UseNode->getMachineOpcode());
3716 const MachineSDNode *DefMN = dyn_cast<MachineSDNode>(DefNode);
3717 unsigned DefAlign = !DefMN->memoperands_empty()
3718 ? (*DefMN->memoperands_begin())->getAlignment() : 0;
3719 const MachineSDNode *UseMN = dyn_cast<MachineSDNode>(UseNode);
3720 unsigned UseAlign = !UseMN->memoperands_empty()
3721 ? (*UseMN->memoperands_begin())->getAlignment() : 0;
3722 int Latency = getOperandLatency(ItinData, DefMCID, DefIdx, DefAlign,
3723 UseMCID, UseIdx, UseAlign);
3726 (Subtarget.isCortexA8() || Subtarget.isLikeA9() ||
3727 Subtarget.isCortexA7())) {
3728 // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2]
3729 // variants are one cycle cheaper.
3730 switch (DefMCID.getOpcode()) {
3735 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
3736 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3738 (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
3745 case ARM::t2LDRSHs: {
3746 // Thumb2 mode: lsl only.
3748 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
3749 if (ShAmt == 0 || ShAmt == 2)
3754 } else if (DefIdx == 0 && Latency > 2 && Subtarget.isSwift()) {
3755 // FIXME: Properly handle all of the latency adjustments for address
3757 switch (DefMCID.getOpcode()) {
3762 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
3763 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3765 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
3766 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
3768 else if (ShImm == 1 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsr)
3775 case ARM::t2LDRSHs: {
3776 // Thumb2 mode: lsl 0-3 only.
3783 if (DefAlign < 8 && Subtarget.isLikeA9())
3784 switch (DefMCID.getOpcode()) {
3790 case ARM::VLD1q8wb_register:
3791 case ARM::VLD1q16wb_register:
3792 case ARM::VLD1q32wb_register:
3793 case ARM::VLD1q64wb_register:
3794 case ARM::VLD1q8wb_fixed:
3795 case ARM::VLD1q16wb_fixed:
3796 case ARM::VLD1q32wb_fixed:
3797 case ARM::VLD1q64wb_fixed:
3801 case ARM::VLD2q8Pseudo:
3802 case ARM::VLD2q16Pseudo:
3803 case ARM::VLD2q32Pseudo:
3804 case ARM::VLD2d8wb_fixed:
3805 case ARM::VLD2d16wb_fixed:
3806 case ARM::VLD2d32wb_fixed:
3807 case ARM::VLD2q8PseudoWB_fixed:
3808 case ARM::VLD2q16PseudoWB_fixed:
3809 case ARM::VLD2q32PseudoWB_fixed:
3810 case ARM::VLD2d8wb_register:
3811 case ARM::VLD2d16wb_register:
3812 case ARM::VLD2d32wb_register:
3813 case ARM::VLD2q8PseudoWB_register:
3814 case ARM::VLD2q16PseudoWB_register:
3815 case ARM::VLD2q32PseudoWB_register:
3816 case ARM::VLD3d8Pseudo:
3817 case ARM::VLD3d16Pseudo:
3818 case ARM::VLD3d32Pseudo:
3819 case ARM::VLD1d64TPseudo:
3820 case ARM::VLD1d64TPseudoWB_fixed:
3821 case ARM::VLD3d8Pseudo_UPD:
3822 case ARM::VLD3d16Pseudo_UPD:
3823 case ARM::VLD3d32Pseudo_UPD:
3824 case ARM::VLD3q8Pseudo_UPD:
3825 case ARM::VLD3q16Pseudo_UPD:
3826 case ARM::VLD3q32Pseudo_UPD:
3827 case ARM::VLD3q8oddPseudo:
3828 case ARM::VLD3q16oddPseudo:
3829 case ARM::VLD3q32oddPseudo:
3830 case ARM::VLD3q8oddPseudo_UPD:
3831 case ARM::VLD3q16oddPseudo_UPD:
3832 case ARM::VLD3q32oddPseudo_UPD:
3833 case ARM::VLD4d8Pseudo:
3834 case ARM::VLD4d16Pseudo:
3835 case ARM::VLD4d32Pseudo:
3836 case ARM::VLD1d64QPseudo:
3837 case ARM::VLD1d64QPseudoWB_fixed:
3838 case ARM::VLD4d8Pseudo_UPD:
3839 case ARM::VLD4d16Pseudo_UPD:
3840 case ARM::VLD4d32Pseudo_UPD:
3841 case ARM::VLD4q8Pseudo_UPD:
3842 case ARM::VLD4q16Pseudo_UPD:
3843 case ARM::VLD4q32Pseudo_UPD:
3844 case ARM::VLD4q8oddPseudo:
3845 case ARM::VLD4q16oddPseudo:
3846 case ARM::VLD4q32oddPseudo:
3847 case ARM::VLD4q8oddPseudo_UPD:
3848 case ARM::VLD4q16oddPseudo_UPD:
3849 case ARM::VLD4q32oddPseudo_UPD:
3850 case ARM::VLD1DUPq8:
3851 case ARM::VLD1DUPq16:
3852 case ARM::VLD1DUPq32:
3853 case ARM::VLD1DUPq8wb_fixed:
3854 case ARM::VLD1DUPq16wb_fixed:
3855 case ARM::VLD1DUPq32wb_fixed:
3856 case ARM::VLD1DUPq8wb_register:
3857 case ARM::VLD1DUPq16wb_register:
3858 case ARM::VLD1DUPq32wb_register:
3859 case ARM::VLD2DUPd8:
3860 case ARM::VLD2DUPd16:
3861 case ARM::VLD2DUPd32:
3862 case ARM::VLD2DUPd8wb_fixed:
3863 case ARM::VLD2DUPd16wb_fixed:
3864 case ARM::VLD2DUPd32wb_fixed:
3865 case ARM::VLD2DUPd8wb_register:
3866 case ARM::VLD2DUPd16wb_register:
3867 case ARM::VLD2DUPd32wb_register:
3868 case ARM::VLD4DUPd8Pseudo:
3869 case ARM::VLD4DUPd16Pseudo:
3870 case ARM::VLD4DUPd32Pseudo:
3871 case ARM::VLD4DUPd8Pseudo_UPD:
3872 case ARM::VLD4DUPd16Pseudo_UPD:
3873 case ARM::VLD4DUPd32Pseudo_UPD:
3874 case ARM::VLD1LNq8Pseudo:
3875 case ARM::VLD1LNq16Pseudo:
3876 case ARM::VLD1LNq32Pseudo:
3877 case ARM::VLD1LNq8Pseudo_UPD:
3878 case ARM::VLD1LNq16Pseudo_UPD:
3879 case ARM::VLD1LNq32Pseudo_UPD:
3880 case ARM::VLD2LNd8Pseudo:
3881 case ARM::VLD2LNd16Pseudo:
3882 case ARM::VLD2LNd32Pseudo:
3883 case ARM::VLD2LNq16Pseudo:
3884 case ARM::VLD2LNq32Pseudo:
3885 case ARM::VLD2LNd8Pseudo_UPD:
3886 case ARM::VLD2LNd16Pseudo_UPD:
3887 case ARM::VLD2LNd32Pseudo_UPD:
3888 case ARM::VLD2LNq16Pseudo_UPD:
3889 case ARM::VLD2LNq32Pseudo_UPD:
3890 case ARM::VLD4LNd8Pseudo:
3891 case ARM::VLD4LNd16Pseudo:
3892 case ARM::VLD4LNd32Pseudo:
3893 case ARM::VLD4LNq16Pseudo:
3894 case ARM::VLD4LNq32Pseudo:
3895 case ARM::VLD4LNd8Pseudo_UPD:
3896 case ARM::VLD4LNd16Pseudo_UPD:
3897 case ARM::VLD4LNd32Pseudo_UPD:
3898 case ARM::VLD4LNq16Pseudo_UPD:
3899 case ARM::VLD4LNq32Pseudo_UPD:
3900 // If the address is not 64-bit aligned, the latencies of these
3901 // instructions increases by one.
3909 unsigned ARMBaseInstrInfo::getPredicationCost(const MachineInstr *MI) const {
3910 if (MI->isCopyLike() || MI->isInsertSubreg() ||
3911 MI->isRegSequence() || MI->isImplicitDef())
3917 const MCInstrDesc &MCID = MI->getDesc();
3919 if (MCID.isCall() || MCID.hasImplicitDefOfPhysReg(ARM::CPSR)) {
3920 // When predicated, CPSR is an additional source operand for CPSR updating
3921 // instructions, this apparently increases their latencies.
3927 unsigned ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
3928 const MachineInstr *MI,
3929 unsigned *PredCost) const {
3930 if (MI->isCopyLike() || MI->isInsertSubreg() ||
3931 MI->isRegSequence() || MI->isImplicitDef())
3934 // An instruction scheduler typically runs on unbundled instructions, however
3935 // other passes may query the latency of a bundled instruction.
3936 if (MI->isBundle()) {
3937 unsigned Latency = 0;
3938 MachineBasicBlock::const_instr_iterator I = MI;
3939 MachineBasicBlock::const_instr_iterator E = MI->getParent()->instr_end();
3940 while (++I != E && I->isInsideBundle()) {
3941 if (I->getOpcode() != ARM::t2IT)
3942 Latency += getInstrLatency(ItinData, I, PredCost);
3947 const MCInstrDesc &MCID = MI->getDesc();
3948 if (PredCost && (MCID.isCall() || MCID.hasImplicitDefOfPhysReg(ARM::CPSR))) {
3949 // When predicated, CPSR is an additional source operand for CPSR updating
3950 // instructions, this apparently increases their latencies.
3953 // Be sure to call getStageLatency for an empty itinerary in case it has a
3954 // valid MinLatency property.
3956 return MI->mayLoad() ? 3 : 1;
3958 unsigned Class = MCID.getSchedClass();
3960 // For instructions with variable uops, use uops as latency.
3961 if (!ItinData->isEmpty() && ItinData->getNumMicroOps(Class) < 0)
3962 return getNumMicroOps(ItinData, MI);
3964 // For the common case, fall back on the itinerary's latency.
3965 unsigned Latency = ItinData->getStageLatency(Class);
3967 // Adjust for dynamic def-side opcode variants not captured by the itinerary.
3968 unsigned DefAlign = MI->hasOneMemOperand()
3969 ? (*MI->memoperands_begin())->getAlignment() : 0;
3970 int Adj = adjustDefLatency(Subtarget, MI, &MCID, DefAlign);
3971 if (Adj >= 0 || (int)Latency > -Adj) {
3972 return Latency + Adj;
3977 int ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
3978 SDNode *Node) const {
3979 if (!Node->isMachineOpcode())
3982 if (!ItinData || ItinData->isEmpty())
3985 unsigned Opcode = Node->getMachineOpcode();
3988 return ItinData->getStageLatency(get(Opcode).getSchedClass());
3995 bool ARMBaseInstrInfo::
3996 hasHighOperandLatency(const InstrItineraryData *ItinData,
3997 const MachineRegisterInfo *MRI,
3998 const MachineInstr *DefMI, unsigned DefIdx,
3999 const MachineInstr *UseMI, unsigned UseIdx) const {
4000 unsigned DDomain = DefMI->getDesc().TSFlags & ARMII::DomainMask;
4001 unsigned UDomain = UseMI->getDesc().TSFlags & ARMII::DomainMask;
4002 if (Subtarget.isCortexA8() &&
4003 (DDomain == ARMII::DomainVFP || UDomain == ARMII::DomainVFP))
4004 // CortexA8 VFP instructions are not pipelined.
4007 // Hoist VFP / NEON instructions with 4 or higher latency.
4008 int Latency = computeOperandLatency(ItinData, DefMI, DefIdx, UseMI, UseIdx);
4010 Latency = getInstrLatency(ItinData, DefMI);
4013 return DDomain == ARMII::DomainVFP || DDomain == ARMII::DomainNEON ||
4014 UDomain == ARMII::DomainVFP || UDomain == ARMII::DomainNEON;
4017 bool ARMBaseInstrInfo::
4018 hasLowDefLatency(const InstrItineraryData *ItinData,
4019 const MachineInstr *DefMI, unsigned DefIdx) const {
4020 if (!ItinData || ItinData->isEmpty())
4023 unsigned DDomain = DefMI->getDesc().TSFlags & ARMII::DomainMask;
4024 if (DDomain == ARMII::DomainGeneral) {
4025 unsigned DefClass = DefMI->getDesc().getSchedClass();
4026 int DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
4027 return (DefCycle != -1 && DefCycle <= 2);
4032 bool ARMBaseInstrInfo::verifyInstruction(const MachineInstr *MI,
4033 StringRef &ErrInfo) const {
4034 if (convertAddSubFlagsOpcode(MI->getOpcode())) {
4035 ErrInfo = "Pseudo flag setting opcodes only exist in Selection DAG";
4041 // LoadStackGuard has so far only been implemented for MachO. Different code
4042 // sequence is needed for other targets.
4043 void ARMBaseInstrInfo::expandLoadStackGuardBase(MachineBasicBlock::iterator MI,
4044 unsigned LoadImmOpc,
4046 Reloc::Model RM) const {
4047 MachineBasicBlock &MBB = *MI->getParent();
4048 DebugLoc DL = MI->getDebugLoc();
4049 unsigned Reg = MI->getOperand(0).getReg();
4050 const GlobalValue *GV =
4051 cast<GlobalValue>((*MI->memoperands_begin())->getValue());
4052 MachineInstrBuilder MIB;
4054 BuildMI(MBB, MI, DL, get(LoadImmOpc), Reg)
4055 .addGlobalAddress(GV, 0, ARMII::MO_NONLAZY);
4057 if (Subtarget.GVIsIndirectSymbol(GV, RM)) {
4058 MIB = BuildMI(MBB, MI, DL, get(LoadOpc), Reg);
4059 MIB.addReg(Reg, RegState::Kill).addImm(0);
4060 unsigned Flag = MachineMemOperand::MOLoad | MachineMemOperand::MOInvariant;
4061 MachineMemOperand *MMO = MBB.getParent()->
4062 getMachineMemOperand(MachinePointerInfo::getGOT(), Flag, 4, 4);
4063 MIB.addMemOperand(MMO);
4064 AddDefaultPred(MIB);
4067 MIB = BuildMI(MBB, MI, DL, get(LoadOpc), Reg);
4068 MIB.addReg(Reg, RegState::Kill).addImm(0);
4069 MIB.setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
4070 AddDefaultPred(MIB);
4074 ARMBaseInstrInfo::isFpMLxInstruction(unsigned Opcode, unsigned &MulOpc,
4075 unsigned &AddSubOpc,
4076 bool &NegAcc, bool &HasLane) const {
4077 DenseMap<unsigned, unsigned>::const_iterator I = MLxEntryMap.find(Opcode);
4078 if (I == MLxEntryMap.end())
4081 const ARM_MLxEntry &Entry = ARM_MLxTable[I->second];
4082 MulOpc = Entry.MulOpc;
4083 AddSubOpc = Entry.AddSubOpc;
4084 NegAcc = Entry.NegAcc;
4085 HasLane = Entry.HasLane;
4089 //===----------------------------------------------------------------------===//
4090 // Execution domains.
4091 //===----------------------------------------------------------------------===//
4093 // Some instructions go down the NEON pipeline, some go down the VFP pipeline,
4094 // and some can go down both. The vmov instructions go down the VFP pipeline,
4095 // but they can be changed to vorr equivalents that are executed by the NEON
4098 // We use the following execution domain numbering:
4106 // Also see ARMInstrFormats.td and Domain* enums in ARMBaseInfo.h
4108 std::pair<uint16_t, uint16_t>
4109 ARMBaseInstrInfo::getExecutionDomain(const MachineInstr *MI) const {
4110 // If we don't have access to NEON instructions then we won't be able
4111 // to swizzle anything to the NEON domain. Check to make sure.
4112 if (Subtarget.hasNEON()) {
4113 // VMOVD, VMOVRS and VMOVSR are VFP instructions, but can be changed to NEON
4114 // if they are not predicated.
4115 if (MI->getOpcode() == ARM::VMOVD && !isPredicated(MI))
4116 return std::make_pair(ExeVFP, (1 << ExeVFP) | (1 << ExeNEON));
4118 // CortexA9 is particularly picky about mixing the two and wants these
4120 if (Subtarget.isCortexA9() && !isPredicated(MI) &&
4121 (MI->getOpcode() == ARM::VMOVRS || MI->getOpcode() == ARM::VMOVSR ||
4122 MI->getOpcode() == ARM::VMOVS))
4123 return std::make_pair(ExeVFP, (1 << ExeVFP) | (1 << ExeNEON));
4125 // No other instructions can be swizzled, so just determine their domain.
4126 unsigned Domain = MI->getDesc().TSFlags & ARMII::DomainMask;
4128 if (Domain & ARMII::DomainNEON)
4129 return std::make_pair(ExeNEON, 0);
4131 // Certain instructions can go either way on Cortex-A8.
4132 // Treat them as NEON instructions.
4133 if ((Domain & ARMII::DomainNEONA8) && Subtarget.isCortexA8())
4134 return std::make_pair(ExeNEON, 0);
4136 if (Domain & ARMII::DomainVFP)
4137 return std::make_pair(ExeVFP, 0);
4139 return std::make_pair(ExeGeneric, 0);
4142 static unsigned getCorrespondingDRegAndLane(const TargetRegisterInfo *TRI,
4143 unsigned SReg, unsigned &Lane) {
4144 unsigned DReg = TRI->getMatchingSuperReg(SReg, ARM::ssub_0, &ARM::DPRRegClass);
4147 if (DReg != ARM::NoRegister)
4151 DReg = TRI->getMatchingSuperReg(SReg, ARM::ssub_1, &ARM::DPRRegClass);
4153 assert(DReg && "S-register with no D super-register?");
4157 /// getImplicitSPRUseForDPRUse - Given a use of a DPR register and lane,
4158 /// set ImplicitSReg to a register number that must be marked as implicit-use or
4159 /// zero if no register needs to be defined as implicit-use.
4161 /// If the function cannot determine if an SPR should be marked implicit use or
4162 /// not, it returns false.
4164 /// This function handles cases where an instruction is being modified from taking
4165 /// an SPR to a DPR[Lane]. A use of the DPR is being added, which may conflict
4166 /// with an earlier def of an SPR corresponding to DPR[Lane^1] (i.e. the other
4167 /// lane of the DPR).
4169 /// If the other SPR is defined, an implicit-use of it should be added. Else,
4170 /// (including the case where the DPR itself is defined), it should not.
4172 static bool getImplicitSPRUseForDPRUse(const TargetRegisterInfo *TRI,
4174 unsigned DReg, unsigned Lane,
4175 unsigned &ImplicitSReg) {
4176 // If the DPR is defined or used already, the other SPR lane will be chained
4177 // correctly, so there is nothing to be done.
4178 if (MI->definesRegister(DReg, TRI) || MI->readsRegister(DReg, TRI)) {
4183 // Otherwise we need to go searching to see if the SPR is set explicitly.
4184 ImplicitSReg = TRI->getSubReg(DReg,
4185 (Lane & 1) ? ARM::ssub_0 : ARM::ssub_1);
4186 MachineBasicBlock::LivenessQueryResult LQR =
4187 MI->getParent()->computeRegisterLiveness(TRI, ImplicitSReg, MI);
4189 if (LQR == MachineBasicBlock::LQR_Live)
4191 else if (LQR == MachineBasicBlock::LQR_Unknown)
4194 // If the register is known not to be live, there is no need to add an
4201 ARMBaseInstrInfo::setExecutionDomain(MachineInstr *MI, unsigned Domain) const {
4202 unsigned DstReg, SrcReg, DReg;
4204 MachineInstrBuilder MIB(*MI->getParent()->getParent(), MI);
4205 const TargetRegisterInfo *TRI = &getRegisterInfo();
4206 switch (MI->getOpcode()) {
4208 llvm_unreachable("cannot handle opcode!");
4211 if (Domain != ExeNEON)
4214 // Zap the predicate operands.
4215 assert(!isPredicated(MI) && "Cannot predicate a VORRd");
4217 // Make sure we've got NEON instructions.
4218 assert(Subtarget.hasNEON() && "VORRd requires NEON");
4220 // Source instruction is %DDst = VMOVD %DSrc, 14, %noreg (; implicits)
4221 DstReg = MI->getOperand(0).getReg();
4222 SrcReg = MI->getOperand(1).getReg();
4224 for (unsigned i = MI->getDesc().getNumOperands(); i; --i)
4225 MI->RemoveOperand(i-1);
4227 // Change to a %DDst = VORRd %DSrc, %DSrc, 14, %noreg (; implicits)
4228 MI->setDesc(get(ARM::VORRd));
4229 AddDefaultPred(MIB.addReg(DstReg, RegState::Define)
4234 if (Domain != ExeNEON)
4236 assert(!isPredicated(MI) && "Cannot predicate a VGETLN");
4238 // Source instruction is %RDst = VMOVRS %SSrc, 14, %noreg (; implicits)
4239 DstReg = MI->getOperand(0).getReg();
4240 SrcReg = MI->getOperand(1).getReg();
4242 for (unsigned i = MI->getDesc().getNumOperands(); i; --i)
4243 MI->RemoveOperand(i-1);
4245 DReg = getCorrespondingDRegAndLane(TRI, SrcReg, Lane);
4247 // Convert to %RDst = VGETLNi32 %DSrc, Lane, 14, %noreg (; imps)
4248 // Note that DSrc has been widened and the other lane may be undef, which
4249 // contaminates the entire register.
4250 MI->setDesc(get(ARM::VGETLNi32));
4251 AddDefaultPred(MIB.addReg(DstReg, RegState::Define)
4252 .addReg(DReg, RegState::Undef)
4255 // The old source should be an implicit use, otherwise we might think it
4256 // was dead before here.
4257 MIB.addReg(SrcReg, RegState::Implicit);
4260 if (Domain != ExeNEON)
4262 assert(!isPredicated(MI) && "Cannot predicate a VSETLN");
4264 // Source instruction is %SDst = VMOVSR %RSrc, 14, %noreg (; implicits)
4265 DstReg = MI->getOperand(0).getReg();
4266 SrcReg = MI->getOperand(1).getReg();
4268 DReg = getCorrespondingDRegAndLane(TRI, DstReg, Lane);
4270 unsigned ImplicitSReg;
4271 if (!getImplicitSPRUseForDPRUse(TRI, MI, DReg, Lane, ImplicitSReg))
4274 for (unsigned i = MI->getDesc().getNumOperands(); i; --i)
4275 MI->RemoveOperand(i-1);
4277 // Convert to %DDst = VSETLNi32 %DDst, %RSrc, Lane, 14, %noreg (; imps)
4278 // Again DDst may be undefined at the beginning of this instruction.
4279 MI->setDesc(get(ARM::VSETLNi32));
4280 MIB.addReg(DReg, RegState::Define)
4281 .addReg(DReg, getUndefRegState(!MI->readsRegister(DReg, TRI)))
4284 AddDefaultPred(MIB);
4286 // The narrower destination must be marked as set to keep previous chains
4288 MIB.addReg(DstReg, RegState::Define | RegState::Implicit);
4289 if (ImplicitSReg != 0)
4290 MIB.addReg(ImplicitSReg, RegState::Implicit);
4294 if (Domain != ExeNEON)
4297 // Source instruction is %SDst = VMOVS %SSrc, 14, %noreg (; implicits)
4298 DstReg = MI->getOperand(0).getReg();
4299 SrcReg = MI->getOperand(1).getReg();
4301 unsigned DstLane = 0, SrcLane = 0, DDst, DSrc;
4302 DDst = getCorrespondingDRegAndLane(TRI, DstReg, DstLane);
4303 DSrc = getCorrespondingDRegAndLane(TRI, SrcReg, SrcLane);
4305 unsigned ImplicitSReg;
4306 if (!getImplicitSPRUseForDPRUse(TRI, MI, DSrc, SrcLane, ImplicitSReg))
4309 for (unsigned i = MI->getDesc().getNumOperands(); i; --i)
4310 MI->RemoveOperand(i-1);
4313 // Destination can be:
4314 // %DDst = VDUPLN32d %DDst, Lane, 14, %noreg (; implicits)
4315 MI->setDesc(get(ARM::VDUPLN32d));
4316 MIB.addReg(DDst, RegState::Define)
4317 .addReg(DDst, getUndefRegState(!MI->readsRegister(DDst, TRI)))
4319 AddDefaultPred(MIB);
4321 // Neither the source or the destination are naturally represented any
4322 // more, so add them in manually.
4323 MIB.addReg(DstReg, RegState::Implicit | RegState::Define);
4324 MIB.addReg(SrcReg, RegState::Implicit);
4325 if (ImplicitSReg != 0)
4326 MIB.addReg(ImplicitSReg, RegState::Implicit);
4330 // In general there's no single instruction that can perform an S <-> S
4331 // move in NEON space, but a pair of VEXT instructions *can* do the
4332 // job. It turns out that the VEXTs needed will only use DSrc once, with
4333 // the position based purely on the combination of lane-0 and lane-1
4334 // involved. For example
4335 // vmov s0, s2 -> vext.32 d0, d0, d1, #1 vext.32 d0, d0, d0, #1
4336 // vmov s1, s3 -> vext.32 d0, d1, d0, #1 vext.32 d0, d0, d0, #1
4337 // vmov s0, s3 -> vext.32 d0, d0, d0, #1 vext.32 d0, d1, d0, #1
4338 // vmov s1, s2 -> vext.32 d0, d0, d0, #1 vext.32 d0, d0, d1, #1
4340 // Pattern of the MachineInstrs is:
4341 // %DDst = VEXTd32 %DSrc1, %DSrc2, Lane, 14, %noreg (;implicits)
4342 MachineInstrBuilder NewMIB;
4343 NewMIB = BuildMI(*MI->getParent(), MI, MI->getDebugLoc(),
4344 get(ARM::VEXTd32), DDst);
4346 // On the first instruction, both DSrc and DDst may be <undef> if present.
4347 // Specifically when the original instruction didn't have them as an
4349 unsigned CurReg = SrcLane == 1 && DstLane == 1 ? DSrc : DDst;
4350 bool CurUndef = !MI->readsRegister(CurReg, TRI);
4351 NewMIB.addReg(CurReg, getUndefRegState(CurUndef));
4353 CurReg = SrcLane == 0 && DstLane == 0 ? DSrc : DDst;
4354 CurUndef = !MI->readsRegister(CurReg, TRI);
4355 NewMIB.addReg(CurReg, getUndefRegState(CurUndef));
4358 AddDefaultPred(NewMIB);
4360 if (SrcLane == DstLane)
4361 NewMIB.addReg(SrcReg, RegState::Implicit);
4363 MI->setDesc(get(ARM::VEXTd32));
4364 MIB.addReg(DDst, RegState::Define);
4366 // On the second instruction, DDst has definitely been defined above, so
4367 // it is not <undef>. DSrc, if present, can be <undef> as above.
4368 CurReg = SrcLane == 1 && DstLane == 0 ? DSrc : DDst;
4369 CurUndef = CurReg == DSrc && !MI->readsRegister(CurReg, TRI);
4370 MIB.addReg(CurReg, getUndefRegState(CurUndef));
4372 CurReg = SrcLane == 0 && DstLane == 1 ? DSrc : DDst;
4373 CurUndef = CurReg == DSrc && !MI->readsRegister(CurReg, TRI);
4374 MIB.addReg(CurReg, getUndefRegState(CurUndef));
4377 AddDefaultPred(MIB);
4379 if (SrcLane != DstLane)
4380 MIB.addReg(SrcReg, RegState::Implicit);
4382 // As before, the original destination is no longer represented, add it
4384 MIB.addReg(DstReg, RegState::Define | RegState::Implicit);
4385 if (ImplicitSReg != 0)
4386 MIB.addReg(ImplicitSReg, RegState::Implicit);
4393 //===----------------------------------------------------------------------===//
4394 // Partial register updates
4395 //===----------------------------------------------------------------------===//
4397 // Swift renames NEON registers with 64-bit granularity. That means any
4398 // instruction writing an S-reg implicitly reads the containing D-reg. The
4399 // problem is mostly avoided by translating f32 operations to v2f32 operations
4400 // on D-registers, but f32 loads are still a problem.
4402 // These instructions can load an f32 into a NEON register:
4404 // VLDRS - Only writes S, partial D update.
4405 // VLD1LNd32 - Writes all D-regs, explicit partial D update, 2 uops.
4406 // VLD1DUPd32 - Writes all D-regs, no partial reg update, 2 uops.
4408 // FCONSTD can be used as a dependency-breaking instruction.
4409 unsigned ARMBaseInstrInfo::
4410 getPartialRegUpdateClearance(const MachineInstr *MI,
4412 const TargetRegisterInfo *TRI) const {
4413 if (!SwiftPartialUpdateClearance ||
4414 !(Subtarget.isSwift() || Subtarget.isCortexA15()))
4417 assert(TRI && "Need TRI instance");
4419 const MachineOperand &MO = MI->getOperand(OpNum);
4422 unsigned Reg = MO.getReg();
4425 switch(MI->getOpcode()) {
4426 // Normal instructions writing only an S-register.
4431 case ARM::VMOVv4i16:
4432 case ARM::VMOVv2i32:
4433 case ARM::VMOVv2f32:
4434 case ARM::VMOVv1i64:
4435 UseOp = MI->findRegisterUseOperandIdx(Reg, false, TRI);
4438 // Explicitly reads the dependency.
4439 case ARM::VLD1LNd32:
4446 // If this instruction actually reads a value from Reg, there is no unwanted
4448 if (UseOp != -1 && MI->getOperand(UseOp).readsReg())
4451 // We must be able to clobber the whole D-reg.
4452 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
4453 // Virtual register must be a foo:ssub_0<def,undef> operand.
4454 if (!MO.getSubReg() || MI->readsVirtualRegister(Reg))
4456 } else if (ARM::SPRRegClass.contains(Reg)) {
4457 // Physical register: MI must define the full D-reg.
4458 unsigned DReg = TRI->getMatchingSuperReg(Reg, ARM::ssub_0,
4460 if (!DReg || !MI->definesRegister(DReg, TRI))
4464 // MI has an unwanted D-register dependency.
4465 // Avoid defs in the previous N instructrions.
4466 return SwiftPartialUpdateClearance;
4469 // Break a partial register dependency after getPartialRegUpdateClearance
4470 // returned non-zero.
4471 void ARMBaseInstrInfo::
4472 breakPartialRegDependency(MachineBasicBlock::iterator MI,
4474 const TargetRegisterInfo *TRI) const {
4475 assert(MI && OpNum < MI->getDesc().getNumDefs() && "OpNum is not a def");
4476 assert(TRI && "Need TRI instance");
4478 const MachineOperand &MO = MI->getOperand(OpNum);
4479 unsigned Reg = MO.getReg();
4480 assert(TargetRegisterInfo::isPhysicalRegister(Reg) &&
4481 "Can't break virtual register dependencies.");
4482 unsigned DReg = Reg;
4484 // If MI defines an S-reg, find the corresponding D super-register.
4485 if (ARM::SPRRegClass.contains(Reg)) {
4486 DReg = ARM::D0 + (Reg - ARM::S0) / 2;
4487 assert(TRI->isSuperRegister(Reg, DReg) && "Register enums broken");
4490 assert(ARM::DPRRegClass.contains(DReg) && "Can only break D-reg deps");
4491 assert(MI->definesRegister(DReg, TRI) && "MI doesn't clobber full D-reg");
4493 // FIXME: In some cases, VLDRS can be changed to a VLD1DUPd32 which defines
4494 // the full D-register by loading the same value to both lanes. The
4495 // instruction is micro-coded with 2 uops, so don't do this until we can
4496 // properly schedule micro-coded instructions. The dispatcher stalls cause
4497 // too big regressions.
4499 // Insert the dependency-breaking FCONSTD before MI.
4500 // 96 is the encoding of 0.5, but the actual value doesn't matter here.
4501 AddDefaultPred(BuildMI(*MI->getParent(), MI, MI->getDebugLoc(),
4502 get(ARM::FCONSTD), DReg).addImm(96));
4503 MI->addRegisterKilled(DReg, TRI, true);
4506 bool ARMBaseInstrInfo::hasNOP() const {
4507 return Subtarget.getFeatureBits()[ARM::HasV6KOps];
4510 bool ARMBaseInstrInfo::isSwiftFastImmShift(const MachineInstr *MI) const {
4511 if (MI->getNumOperands() < 4)
4513 unsigned ShOpVal = MI->getOperand(3).getImm();
4514 unsigned ShImm = ARM_AM::getSORegOffset(ShOpVal);
4515 // Swift supports faster shifts for: lsl 2, lsl 1, and lsr 1.
4516 if ((ShImm == 1 && ARM_AM::getSORegShOp(ShOpVal) == ARM_AM::lsr) ||
4517 ((ShImm == 1 || ShImm == 2) &&
4518 ARM_AM::getSORegShOp(ShOpVal) == ARM_AM::lsl))
4524 bool ARMBaseInstrInfo::getRegSequenceLikeInputs(
4525 const MachineInstr &MI, unsigned DefIdx,
4526 SmallVectorImpl<RegSubRegPairAndIdx> &InputRegs) const {
4527 assert(DefIdx < MI.getDesc().getNumDefs() && "Invalid definition index");
4528 assert(MI.isRegSequenceLike() && "Invalid kind of instruction");
4530 switch (MI.getOpcode()) {
4532 // dX = VMOVDRR rY, rZ
4534 // dX = REG_SEQUENCE rY, ssub_0, rZ, ssub_1
4535 // Populate the InputRegs accordingly.
4537 const MachineOperand *MOReg = &MI.getOperand(1);
4538 InputRegs.push_back(
4539 RegSubRegPairAndIdx(MOReg->getReg(), MOReg->getSubReg(), ARM::ssub_0));
4541 MOReg = &MI.getOperand(2);
4542 InputRegs.push_back(
4543 RegSubRegPairAndIdx(MOReg->getReg(), MOReg->getSubReg(), ARM::ssub_1));
4546 llvm_unreachable("Target dependent opcode missing");
4549 bool ARMBaseInstrInfo::getExtractSubregLikeInputs(
4550 const MachineInstr &MI, unsigned DefIdx,
4551 RegSubRegPairAndIdx &InputReg) const {
4552 assert(DefIdx < MI.getDesc().getNumDefs() && "Invalid definition index");
4553 assert(MI.isExtractSubregLike() && "Invalid kind of instruction");
4555 switch (MI.getOpcode()) {
4557 // rX, rY = VMOVRRD dZ
4559 // rX = EXTRACT_SUBREG dZ, ssub_0
4560 // rY = EXTRACT_SUBREG dZ, ssub_1
4561 const MachineOperand &MOReg = MI.getOperand(2);
4562 InputReg.Reg = MOReg.getReg();
4563 InputReg.SubReg = MOReg.getSubReg();
4564 InputReg.SubIdx = DefIdx == 0 ? ARM::ssub_0 : ARM::ssub_1;
4567 llvm_unreachable("Target dependent opcode missing");
4570 bool ARMBaseInstrInfo::getInsertSubregLikeInputs(
4571 const MachineInstr &MI, unsigned DefIdx, RegSubRegPair &BaseReg,
4572 RegSubRegPairAndIdx &InsertedReg) const {
4573 assert(DefIdx < MI.getDesc().getNumDefs() && "Invalid definition index");
4574 assert(MI.isInsertSubregLike() && "Invalid kind of instruction");
4576 switch (MI.getOpcode()) {
4577 case ARM::VSETLNi32:
4578 // dX = VSETLNi32 dY, rZ, imm
4579 const MachineOperand &MOBaseReg = MI.getOperand(1);
4580 const MachineOperand &MOInsertedReg = MI.getOperand(2);
4581 const MachineOperand &MOIndex = MI.getOperand(3);
4582 BaseReg.Reg = MOBaseReg.getReg();
4583 BaseReg.SubReg = MOBaseReg.getSubReg();
4585 InsertedReg.Reg = MOInsertedReg.getReg();
4586 InsertedReg.SubReg = MOInsertedReg.getSubReg();
4587 InsertedReg.SubIdx = MOIndex.getImm() == 0 ? ARM::ssub_0 : ARM::ssub_1;
4590 llvm_unreachable("Target dependent opcode missing");