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/CodeGen/TargetSchedule.h"
32 #include "llvm/IR/Constants.h"
33 #include "llvm/IR/Function.h"
34 #include "llvm/IR/GlobalValue.h"
35 #include "llvm/MC/MCAsmInfo.h"
36 #include "llvm/MC/MCExpr.h"
37 #include "llvm/Support/BranchProbability.h"
38 #include "llvm/Support/CommandLine.h"
39 #include "llvm/Support/Debug.h"
40 #include "llvm/Support/ErrorHandling.h"
41 #include "llvm/Support/raw_ostream.h"
45 #define DEBUG_TYPE "arm-instrinfo"
47 #define GET_INSTRINFO_CTOR_DTOR
48 #include "ARMGenInstrInfo.inc"
51 EnableARM3Addr("enable-arm-3-addr-conv", cl::Hidden,
52 cl::desc("Enable ARM 2-addr to 3-addr conv"));
55 WidenVMOVS("widen-vmovs", cl::Hidden, cl::init(true),
56 cl::desc("Widen ARM vmovs to vmovd when possible"));
58 static cl::opt<unsigned>
59 SwiftPartialUpdateClearance("swift-partial-update-clearance",
60 cl::Hidden, cl::init(12),
61 cl::desc("Clearance before partial register updates"));
63 /// ARM_MLxEntry - Record information about MLA / MLS instructions.
65 uint16_t MLxOpc; // MLA / MLS opcode
66 uint16_t MulOpc; // Expanded multiplication opcode
67 uint16_t AddSubOpc; // Expanded add / sub opcode
68 bool NegAcc; // True if the acc is negated before the add / sub.
69 bool HasLane; // True if instruction has an extra "lane" operand.
72 static const ARM_MLxEntry ARM_MLxTable[] = {
73 // MLxOpc, MulOpc, AddSubOpc, NegAcc, HasLane
75 { ARM::VMLAS, ARM::VMULS, ARM::VADDS, false, false },
76 { ARM::VMLSS, ARM::VMULS, ARM::VSUBS, false, false },
77 { ARM::VMLAD, ARM::VMULD, ARM::VADDD, false, false },
78 { ARM::VMLSD, ARM::VMULD, ARM::VSUBD, false, false },
79 { ARM::VNMLAS, ARM::VNMULS, ARM::VSUBS, true, false },
80 { ARM::VNMLSS, ARM::VMULS, ARM::VSUBS, true, false },
81 { ARM::VNMLAD, ARM::VNMULD, ARM::VSUBD, true, false },
82 { ARM::VNMLSD, ARM::VMULD, ARM::VSUBD, true, false },
85 { ARM::VMLAfd, ARM::VMULfd, ARM::VADDfd, false, false },
86 { ARM::VMLSfd, ARM::VMULfd, ARM::VSUBfd, false, false },
87 { ARM::VMLAfq, ARM::VMULfq, ARM::VADDfq, false, false },
88 { ARM::VMLSfq, ARM::VMULfq, ARM::VSUBfq, false, false },
89 { ARM::VMLAslfd, ARM::VMULslfd, ARM::VADDfd, false, true },
90 { ARM::VMLSslfd, ARM::VMULslfd, ARM::VSUBfd, false, true },
91 { ARM::VMLAslfq, ARM::VMULslfq, ARM::VADDfq, false, true },
92 { ARM::VMLSslfq, ARM::VMULslfq, ARM::VSUBfq, false, true },
95 ARMBaseInstrInfo::ARMBaseInstrInfo(const ARMSubtarget& STI)
96 : ARMGenInstrInfo(ARM::ADJCALLSTACKDOWN, ARM::ADJCALLSTACKUP),
98 for (unsigned i = 0, e = array_lengthof(ARM_MLxTable); i != e; ++i) {
99 if (!MLxEntryMap.insert(std::make_pair(ARM_MLxTable[i].MLxOpc, i)).second)
100 assert(false && "Duplicated entries?");
101 MLxHazardOpcodes.insert(ARM_MLxTable[i].AddSubOpc);
102 MLxHazardOpcodes.insert(ARM_MLxTable[i].MulOpc);
106 // Use a ScoreboardHazardRecognizer for prepass ARM scheduling. TargetInstrImpl
107 // currently defaults to no prepass hazard recognizer.
108 ScheduleHazardRecognizer *
109 ARMBaseInstrInfo::CreateTargetHazardRecognizer(const TargetSubtargetInfo *STI,
110 const ScheduleDAG *DAG) const {
111 if (usePreRAHazardRecognizer()) {
112 const InstrItineraryData *II =
113 static_cast<const ARMSubtarget *>(STI)->getInstrItineraryData();
114 return new ScoreboardHazardRecognizer(II, DAG, "pre-RA-sched");
116 return TargetInstrInfo::CreateTargetHazardRecognizer(STI, DAG);
119 ScheduleHazardRecognizer *ARMBaseInstrInfo::
120 CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II,
121 const ScheduleDAG *DAG) const {
122 if (Subtarget.isThumb2() || Subtarget.hasVFP2())
123 return (ScheduleHazardRecognizer *)new ARMHazardRecognizer(II, DAG);
124 return TargetInstrInfo::CreateTargetPostRAHazardRecognizer(II, DAG);
128 ARMBaseInstrInfo::convertToThreeAddress(MachineFunction::iterator &MFI,
129 MachineBasicBlock::iterator &MBBI,
130 LiveVariables *LV) const {
131 // FIXME: Thumb2 support.
136 MachineInstr *MI = MBBI;
137 MachineFunction &MF = *MI->getParent()->getParent();
138 uint64_t TSFlags = MI->getDesc().TSFlags;
140 switch ((TSFlags & ARMII::IndexModeMask) >> ARMII::IndexModeShift) {
141 default: return nullptr;
142 case ARMII::IndexModePre:
145 case ARMII::IndexModePost:
149 // Try splitting an indexed load/store to an un-indexed one plus an add/sub
151 unsigned MemOpc = getUnindexedOpcode(MI->getOpcode());
155 MachineInstr *UpdateMI = nullptr;
156 MachineInstr *MemMI = nullptr;
157 unsigned AddrMode = (TSFlags & ARMII::AddrModeMask);
158 const MCInstrDesc &MCID = MI->getDesc();
159 unsigned NumOps = MCID.getNumOperands();
160 bool isLoad = !MI->mayStore();
161 const MachineOperand &WB = isLoad ? MI->getOperand(1) : MI->getOperand(0);
162 const MachineOperand &Base = MI->getOperand(2);
163 const MachineOperand &Offset = MI->getOperand(NumOps-3);
164 unsigned WBReg = WB.getReg();
165 unsigned BaseReg = Base.getReg();
166 unsigned OffReg = Offset.getReg();
167 unsigned OffImm = MI->getOperand(NumOps-2).getImm();
168 ARMCC::CondCodes Pred = (ARMCC::CondCodes)MI->getOperand(NumOps-1).getImm();
170 default: llvm_unreachable("Unknown indexed op!");
171 case ARMII::AddrMode2: {
172 bool isSub = ARM_AM::getAM2Op(OffImm) == ARM_AM::sub;
173 unsigned Amt = ARM_AM::getAM2Offset(OffImm);
175 if (ARM_AM::getSOImmVal(Amt) == -1)
176 // Can't encode it in a so_imm operand. This transformation will
177 // add more than 1 instruction. Abandon!
179 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
180 get(isSub ? ARM::SUBri : ARM::ADDri), WBReg)
181 .addReg(BaseReg).addImm(Amt)
182 .addImm(Pred).addReg(0).addReg(0);
183 } else if (Amt != 0) {
184 ARM_AM::ShiftOpc ShOpc = ARM_AM::getAM2ShiftOpc(OffImm);
185 unsigned SOOpc = ARM_AM::getSORegOpc(ShOpc, Amt);
186 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
187 get(isSub ? ARM::SUBrsi : ARM::ADDrsi), WBReg)
188 .addReg(BaseReg).addReg(OffReg).addReg(0).addImm(SOOpc)
189 .addImm(Pred).addReg(0).addReg(0);
191 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
192 get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
193 .addReg(BaseReg).addReg(OffReg)
194 .addImm(Pred).addReg(0).addReg(0);
197 case ARMII::AddrMode3 : {
198 bool isSub = ARM_AM::getAM3Op(OffImm) == ARM_AM::sub;
199 unsigned Amt = ARM_AM::getAM3Offset(OffImm);
201 // Immediate is 8-bits. It's guaranteed to fit in a so_imm operand.
202 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
203 get(isSub ? ARM::SUBri : ARM::ADDri), WBReg)
204 .addReg(BaseReg).addImm(Amt)
205 .addImm(Pred).addReg(0).addReg(0);
207 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
208 get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
209 .addReg(BaseReg).addReg(OffReg)
210 .addImm(Pred).addReg(0).addReg(0);
215 std::vector<MachineInstr*> NewMIs;
218 MemMI = BuildMI(MF, MI->getDebugLoc(),
219 get(MemOpc), MI->getOperand(0).getReg())
220 .addReg(WBReg).addImm(0).addImm(Pred);
222 MemMI = BuildMI(MF, MI->getDebugLoc(),
223 get(MemOpc)).addReg(MI->getOperand(1).getReg())
224 .addReg(WBReg).addReg(0).addImm(0).addImm(Pred);
225 NewMIs.push_back(MemMI);
226 NewMIs.push_back(UpdateMI);
229 MemMI = BuildMI(MF, MI->getDebugLoc(),
230 get(MemOpc), MI->getOperand(0).getReg())
231 .addReg(BaseReg).addImm(0).addImm(Pred);
233 MemMI = BuildMI(MF, MI->getDebugLoc(),
234 get(MemOpc)).addReg(MI->getOperand(1).getReg())
235 .addReg(BaseReg).addReg(0).addImm(0).addImm(Pred);
237 UpdateMI->getOperand(0).setIsDead();
238 NewMIs.push_back(UpdateMI);
239 NewMIs.push_back(MemMI);
242 // Transfer LiveVariables states, kill / dead info.
244 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
245 MachineOperand &MO = MI->getOperand(i);
246 if (MO.isReg() && TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
247 unsigned Reg = MO.getReg();
249 LiveVariables::VarInfo &VI = LV->getVarInfo(Reg);
251 MachineInstr *NewMI = (Reg == WBReg) ? UpdateMI : MemMI;
253 LV->addVirtualRegisterDead(Reg, NewMI);
255 if (MO.isUse() && MO.isKill()) {
256 for (unsigned j = 0; j < 2; ++j) {
257 // Look at the two new MI's in reverse order.
258 MachineInstr *NewMI = NewMIs[j];
259 if (!NewMI->readsRegister(Reg))
261 LV->addVirtualRegisterKilled(Reg, NewMI);
262 if (VI.removeKill(MI))
263 VI.Kills.push_back(NewMI);
271 MFI->insert(MBBI, NewMIs[1]);
272 MFI->insert(MBBI, NewMIs[0]);
278 ARMBaseInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
279 MachineBasicBlock *&FBB,
280 SmallVectorImpl<MachineOperand> &Cond,
281 bool AllowModify) const {
285 MachineBasicBlock::iterator I = MBB.end();
286 if (I == MBB.begin())
287 return false; // Empty blocks are easy.
290 // Walk backwards from the end of the basic block until the branch is
291 // analyzed or we give up.
292 while (isPredicated(I) || I->isTerminator() || I->isDebugValue()) {
294 // Flag to be raised on unanalyzeable instructions. This is useful in cases
295 // where we want to clean up on the end of the basic block before we bail
297 bool CantAnalyze = false;
299 // Skip over DEBUG values and predicated nonterminators.
300 while (I->isDebugValue() || !I->isTerminator()) {
301 if (I == MBB.begin())
306 if (isIndirectBranchOpcode(I->getOpcode()) ||
307 isJumpTableBranchOpcode(I->getOpcode())) {
308 // Indirect branches and jump tables can't be analyzed, but we still want
309 // to clean up any instructions at the tail of the basic block.
311 } else if (isUncondBranchOpcode(I->getOpcode())) {
312 TBB = I->getOperand(0).getMBB();
313 } else if (isCondBranchOpcode(I->getOpcode())) {
314 // Bail out if we encounter multiple conditional branches.
318 assert(!FBB && "FBB should have been null.");
320 TBB = I->getOperand(0).getMBB();
321 Cond.push_back(I->getOperand(1));
322 Cond.push_back(I->getOperand(2));
323 } else if (I->isReturn()) {
324 // Returns can't be analyzed, but we should run cleanup.
325 CantAnalyze = !isPredicated(I);
327 // We encountered other unrecognized terminator. Bail out immediately.
331 // Cleanup code - to be run for unpredicated unconditional branches and
333 if (!isPredicated(I) &&
334 (isUncondBranchOpcode(I->getOpcode()) ||
335 isIndirectBranchOpcode(I->getOpcode()) ||
336 isJumpTableBranchOpcode(I->getOpcode()) ||
338 // Forget any previous condition branch information - it no longer applies.
342 // If we can modify the function, delete everything below this
343 // unconditional branch.
345 MachineBasicBlock::iterator DI = std::next(I);
346 while (DI != MBB.end()) {
347 MachineInstr *InstToDelete = DI;
349 InstToDelete->eraseFromParent();
357 if (I == MBB.begin())
363 // We made it past the terminators without bailing out - we must have
364 // analyzed this branch successfully.
369 unsigned ARMBaseInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
370 MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr();
374 if (!isUncondBranchOpcode(I->getOpcode()) &&
375 !isCondBranchOpcode(I->getOpcode()))
378 // Remove the branch.
379 I->eraseFromParent();
383 if (I == MBB.begin()) return 1;
385 if (!isCondBranchOpcode(I->getOpcode()))
388 // Remove the branch.
389 I->eraseFromParent();
394 ARMBaseInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
395 MachineBasicBlock *FBB,
396 ArrayRef<MachineOperand> Cond,
398 ARMFunctionInfo *AFI = MBB.getParent()->getInfo<ARMFunctionInfo>();
399 int BOpc = !AFI->isThumbFunction()
400 ? ARM::B : (AFI->isThumb2Function() ? ARM::t2B : ARM::tB);
401 int BccOpc = !AFI->isThumbFunction()
402 ? ARM::Bcc : (AFI->isThumb2Function() ? ARM::t2Bcc : ARM::tBcc);
403 bool isThumb = AFI->isThumbFunction() || AFI->isThumb2Function();
405 // Shouldn't be a fall through.
406 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
407 assert((Cond.size() == 2 || Cond.size() == 0) &&
408 "ARM branch conditions have two components!");
410 // For conditional branches, we use addOperand to preserve CPSR flags.
413 if (Cond.empty()) { // Unconditional branch?
415 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB).addImm(ARMCC::AL).addReg(0);
417 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB);
419 BuildMI(&MBB, DL, get(BccOpc)).addMBB(TBB)
420 .addImm(Cond[0].getImm()).addOperand(Cond[1]);
424 // Two-way conditional branch.
425 BuildMI(&MBB, DL, get(BccOpc)).addMBB(TBB)
426 .addImm(Cond[0].getImm()).addOperand(Cond[1]);
428 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB).addImm(ARMCC::AL).addReg(0);
430 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB);
434 bool ARMBaseInstrInfo::
435 ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
436 ARMCC::CondCodes CC = (ARMCC::CondCodes)(int)Cond[0].getImm();
437 Cond[0].setImm(ARMCC::getOppositeCondition(CC));
441 bool ARMBaseInstrInfo::isPredicated(const MachineInstr *MI) const {
442 if (MI->isBundle()) {
443 MachineBasicBlock::const_instr_iterator I = MI;
444 MachineBasicBlock::const_instr_iterator E = MI->getParent()->instr_end();
445 while (++I != E && I->isInsideBundle()) {
446 int PIdx = I->findFirstPredOperandIdx();
447 if (PIdx != -1 && I->getOperand(PIdx).getImm() != ARMCC::AL)
453 int PIdx = MI->findFirstPredOperandIdx();
454 return PIdx != -1 && MI->getOperand(PIdx).getImm() != ARMCC::AL;
457 bool ARMBaseInstrInfo::
458 PredicateInstruction(MachineInstr *MI, ArrayRef<MachineOperand> Pred) const {
459 unsigned Opc = MI->getOpcode();
460 if (isUncondBranchOpcode(Opc)) {
461 MI->setDesc(get(getMatchingCondBranchOpcode(Opc)));
462 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
463 .addImm(Pred[0].getImm())
464 .addReg(Pred[1].getReg());
468 int PIdx = MI->findFirstPredOperandIdx();
470 MachineOperand &PMO = MI->getOperand(PIdx);
471 PMO.setImm(Pred[0].getImm());
472 MI->getOperand(PIdx+1).setReg(Pred[1].getReg());
478 bool ARMBaseInstrInfo::SubsumesPredicate(ArrayRef<MachineOperand> Pred1,
479 ArrayRef<MachineOperand> Pred2) const {
480 if (Pred1.size() > 2 || Pred2.size() > 2)
483 ARMCC::CondCodes CC1 = (ARMCC::CondCodes)Pred1[0].getImm();
484 ARMCC::CondCodes CC2 = (ARMCC::CondCodes)Pred2[0].getImm();
494 return CC2 == ARMCC::HI;
496 return CC2 == ARMCC::LO || CC2 == ARMCC::EQ;
498 return CC2 == ARMCC::GT;
500 return CC2 == ARMCC::LT;
504 bool ARMBaseInstrInfo::DefinesPredicate(MachineInstr *MI,
505 std::vector<MachineOperand> &Pred) const {
507 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
508 const MachineOperand &MO = MI->getOperand(i);
509 if ((MO.isRegMask() && MO.clobbersPhysReg(ARM::CPSR)) ||
510 (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR)) {
519 static bool isCPSRDefined(const MachineInstr *MI) {
520 for (const auto &MO : MI->operands())
521 if (MO.isReg() && MO.getReg() == ARM::CPSR && MO.isDef() && !MO.isDead())
526 static bool isEligibleForITBlock(const MachineInstr *MI) {
527 switch (MI->getOpcode()) {
528 default: return true;
529 case ARM::tADC: // ADC (register) T1
530 case ARM::tADDi3: // ADD (immediate) T1
531 case ARM::tADDi8: // ADD (immediate) T2
532 case ARM::tADDrr: // ADD (register) T1
533 case ARM::tAND: // AND (register) T1
534 case ARM::tASRri: // ASR (immediate) T1
535 case ARM::tASRrr: // ASR (register) T1
536 case ARM::tBIC: // BIC (register) T1
537 case ARM::tEOR: // EOR (register) T1
538 case ARM::tLSLri: // LSL (immediate) T1
539 case ARM::tLSLrr: // LSL (register) T1
540 case ARM::tLSRri: // LSR (immediate) T1
541 case ARM::tLSRrr: // LSR (register) T1
542 case ARM::tMUL: // MUL T1
543 case ARM::tMVN: // MVN (register) T1
544 case ARM::tORR: // ORR (register) T1
545 case ARM::tROR: // ROR (register) T1
546 case ARM::tRSB: // RSB (immediate) T1
547 case ARM::tSBC: // SBC (register) T1
548 case ARM::tSUBi3: // SUB (immediate) T1
549 case ARM::tSUBi8: // SUB (immediate) T2
550 case ARM::tSUBrr: // SUB (register) T1
551 return !isCPSRDefined(MI);
555 /// isPredicable - Return true if the specified instruction can be predicated.
556 /// By default, this returns true for every instruction with a
557 /// PredicateOperand.
558 bool ARMBaseInstrInfo::isPredicable(MachineInstr *MI) const {
559 if (!MI->isPredicable())
562 if (!isEligibleForITBlock(MI))
565 ARMFunctionInfo *AFI =
566 MI->getParent()->getParent()->getInfo<ARMFunctionInfo>();
568 if (AFI->isThumb2Function()) {
569 if (getSubtarget().restrictIT())
570 return isV8EligibleForIT(MI);
571 } else { // non-Thumb
572 if ((MI->getDesc().TSFlags & ARMII::DomainMask) == ARMII::DomainNEON)
580 template <> bool IsCPSRDead<MachineInstr>(MachineInstr *MI) {
581 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
582 const MachineOperand &MO = MI->getOperand(i);
583 if (!MO.isReg() || MO.isUndef() || MO.isUse())
585 if (MO.getReg() != ARM::CPSR)
590 // all definitions of CPSR are dead
595 /// GetInstSize - Return the size of the specified MachineInstr.
597 unsigned ARMBaseInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
598 const MachineBasicBlock &MBB = *MI->getParent();
599 const MachineFunction *MF = MBB.getParent();
600 const MCAsmInfo *MAI = MF->getTarget().getMCAsmInfo();
602 const MCInstrDesc &MCID = MI->getDesc();
604 return MCID.getSize();
606 // If this machine instr is an inline asm, measure it.
607 if (MI->getOpcode() == ARM::INLINEASM)
608 return getInlineAsmLength(MI->getOperand(0).getSymbolName(), *MAI);
609 unsigned Opc = MI->getOpcode();
612 // pseudo-instruction sizes are zero.
614 case TargetOpcode::BUNDLE:
615 return getInstBundleLength(MI);
616 case ARM::MOVi16_ga_pcrel:
617 case ARM::MOVTi16_ga_pcrel:
618 case ARM::t2MOVi16_ga_pcrel:
619 case ARM::t2MOVTi16_ga_pcrel:
622 case ARM::t2MOVi32imm:
624 case ARM::CONSTPOOL_ENTRY:
625 case ARM::JUMPTABLE_INSTS:
626 case ARM::JUMPTABLE_ADDRS:
627 case ARM::JUMPTABLE_TBB:
628 case ARM::JUMPTABLE_TBH:
629 // If this machine instr is a constant pool entry, its size is recorded as
631 return MI->getOperand(2).getImm();
632 case ARM::Int_eh_sjlj_longjmp:
634 case ARM::tInt_eh_sjlj_longjmp:
636 case ARM::Int_eh_sjlj_setjmp:
637 case ARM::Int_eh_sjlj_setjmp_nofp:
639 case ARM::tInt_eh_sjlj_setjmp:
640 case ARM::t2Int_eh_sjlj_setjmp:
641 case ARM::t2Int_eh_sjlj_setjmp_nofp:
644 return MI->getOperand(1).getImm();
648 unsigned ARMBaseInstrInfo::getInstBundleLength(const MachineInstr *MI) const {
650 MachineBasicBlock::const_instr_iterator I = MI;
651 MachineBasicBlock::const_instr_iterator E = MI->getParent()->instr_end();
652 while (++I != E && I->isInsideBundle()) {
653 assert(!I->isBundle() && "No nested bundle!");
654 Size += GetInstSizeInBytes(&*I);
659 void ARMBaseInstrInfo::copyFromCPSR(MachineBasicBlock &MBB,
660 MachineBasicBlock::iterator I,
661 unsigned DestReg, bool KillSrc,
662 const ARMSubtarget &Subtarget) const {
663 unsigned Opc = Subtarget.isThumb()
664 ? (Subtarget.isMClass() ? ARM::t2MRS_M : ARM::t2MRS_AR)
667 MachineInstrBuilder MIB =
668 BuildMI(MBB, I, I->getDebugLoc(), get(Opc), DestReg);
670 // There is only 1 A/R class MRS instruction, and it always refers to
671 // APSR. However, there are lots of other possibilities on M-class cores.
672 if (Subtarget.isMClass())
677 MIB.addReg(ARM::CPSR, RegState::Implicit | getKillRegState(KillSrc));
680 void ARMBaseInstrInfo::copyToCPSR(MachineBasicBlock &MBB,
681 MachineBasicBlock::iterator I,
682 unsigned SrcReg, bool KillSrc,
683 const ARMSubtarget &Subtarget) const {
684 unsigned Opc = Subtarget.isThumb()
685 ? (Subtarget.isMClass() ? ARM::t2MSR_M : ARM::t2MSR_AR)
688 MachineInstrBuilder MIB = BuildMI(MBB, I, I->getDebugLoc(), get(Opc));
690 if (Subtarget.isMClass())
695 MIB.addReg(SrcReg, getKillRegState(KillSrc));
699 MIB.addReg(ARM::CPSR, RegState::Implicit | RegState::Define);
702 void ARMBaseInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
703 MachineBasicBlock::iterator I, DebugLoc DL,
704 unsigned DestReg, unsigned SrcReg,
705 bool KillSrc) const {
706 bool GPRDest = ARM::GPRRegClass.contains(DestReg);
707 bool GPRSrc = ARM::GPRRegClass.contains(SrcReg);
709 if (GPRDest && GPRSrc) {
710 AddDefaultCC(AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::MOVr), DestReg)
711 .addReg(SrcReg, getKillRegState(KillSrc))));
715 bool SPRDest = ARM::SPRRegClass.contains(DestReg);
716 bool SPRSrc = ARM::SPRRegClass.contains(SrcReg);
719 if (SPRDest && SPRSrc)
721 else if (GPRDest && SPRSrc)
723 else if (SPRDest && GPRSrc)
725 else if (ARM::DPRRegClass.contains(DestReg, SrcReg) && !Subtarget.isFPOnlySP())
727 else if (ARM::QPRRegClass.contains(DestReg, SrcReg))
731 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(Opc), DestReg);
732 MIB.addReg(SrcReg, getKillRegState(KillSrc));
733 if (Opc == ARM::VORRq)
734 MIB.addReg(SrcReg, getKillRegState(KillSrc));
739 // Handle register classes that require multiple instructions.
740 unsigned BeginIdx = 0;
741 unsigned SubRegs = 0;
744 // Use VORRq when possible.
745 if (ARM::QQPRRegClass.contains(DestReg, SrcReg)) {
747 BeginIdx = ARM::qsub_0;
749 } else if (ARM::QQQQPRRegClass.contains(DestReg, SrcReg)) {
751 BeginIdx = ARM::qsub_0;
753 // Fall back to VMOVD.
754 } else if (ARM::DPairRegClass.contains(DestReg, SrcReg)) {
756 BeginIdx = ARM::dsub_0;
758 } else if (ARM::DTripleRegClass.contains(DestReg, SrcReg)) {
760 BeginIdx = ARM::dsub_0;
762 } else if (ARM::DQuadRegClass.contains(DestReg, SrcReg)) {
764 BeginIdx = ARM::dsub_0;
766 } else if (ARM::GPRPairRegClass.contains(DestReg, SrcReg)) {
767 Opc = Subtarget.isThumb2() ? ARM::tMOVr : ARM::MOVr;
768 BeginIdx = ARM::gsub_0;
770 } else if (ARM::DPairSpcRegClass.contains(DestReg, SrcReg)) {
772 BeginIdx = ARM::dsub_0;
775 } else if (ARM::DTripleSpcRegClass.contains(DestReg, SrcReg)) {
777 BeginIdx = ARM::dsub_0;
780 } else if (ARM::DQuadSpcRegClass.contains(DestReg, SrcReg)) {
782 BeginIdx = ARM::dsub_0;
785 } else if (ARM::DPRRegClass.contains(DestReg, SrcReg) && Subtarget.isFPOnlySP()) {
787 BeginIdx = ARM::ssub_0;
789 } else if (SrcReg == ARM::CPSR) {
790 copyFromCPSR(MBB, I, DestReg, KillSrc, Subtarget);
792 } else if (DestReg == ARM::CPSR) {
793 copyToCPSR(MBB, I, SrcReg, KillSrc, Subtarget);
797 assert(Opc && "Impossible reg-to-reg copy");
799 const TargetRegisterInfo *TRI = &getRegisterInfo();
800 MachineInstrBuilder Mov;
802 // Copy register tuples backward when the first Dest reg overlaps with SrcReg.
803 if (TRI->regsOverlap(SrcReg, TRI->getSubReg(DestReg, BeginIdx))) {
804 BeginIdx = BeginIdx + ((SubRegs - 1) * Spacing);
808 SmallSet<unsigned, 4> DstRegs;
810 for (unsigned i = 0; i != SubRegs; ++i) {
811 unsigned Dst = TRI->getSubReg(DestReg, BeginIdx + i * Spacing);
812 unsigned Src = TRI->getSubReg(SrcReg, BeginIdx + i * Spacing);
813 assert(Dst && Src && "Bad sub-register");
815 assert(!DstRegs.count(Src) && "destructive vector copy");
818 Mov = BuildMI(MBB, I, I->getDebugLoc(), get(Opc), Dst).addReg(Src);
819 // VORR takes two source operands.
820 if (Opc == ARM::VORRq)
822 Mov = AddDefaultPred(Mov);
824 if (Opc == ARM::MOVr)
825 Mov = AddDefaultCC(Mov);
827 // Add implicit super-register defs and kills to the last instruction.
828 Mov->addRegisterDefined(DestReg, TRI);
830 Mov->addRegisterKilled(SrcReg, TRI);
833 const MachineInstrBuilder &
834 ARMBaseInstrInfo::AddDReg(MachineInstrBuilder &MIB, unsigned Reg,
835 unsigned SubIdx, unsigned State,
836 const TargetRegisterInfo *TRI) const {
838 return MIB.addReg(Reg, State);
840 if (TargetRegisterInfo::isPhysicalRegister(Reg))
841 return MIB.addReg(TRI->getSubReg(Reg, SubIdx), State);
842 return MIB.addReg(Reg, State, SubIdx);
845 void ARMBaseInstrInfo::
846 storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
847 unsigned SrcReg, bool isKill, int FI,
848 const TargetRegisterClass *RC,
849 const TargetRegisterInfo *TRI) const {
851 if (I != MBB.end()) DL = I->getDebugLoc();
852 MachineFunction &MF = *MBB.getParent();
853 MachineFrameInfo &MFI = *MF.getFrameInfo();
854 unsigned Align = MFI.getObjectAlignment(FI);
856 MachineMemOperand *MMO = MF.getMachineMemOperand(
857 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOStore,
858 MFI.getObjectSize(FI), Align);
860 switch (RC->getSize()) {
862 if (ARM::GPRRegClass.hasSubClassEq(RC)) {
863 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::STRi12))
864 .addReg(SrcReg, getKillRegState(isKill))
865 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
866 } else if (ARM::SPRRegClass.hasSubClassEq(RC)) {
867 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTRS))
868 .addReg(SrcReg, getKillRegState(isKill))
869 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
871 llvm_unreachable("Unknown reg class!");
874 if (ARM::DPRRegClass.hasSubClassEq(RC)) {
875 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTRD))
876 .addReg(SrcReg, getKillRegState(isKill))
877 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
878 } else if (ARM::GPRPairRegClass.hasSubClassEq(RC)) {
879 if (Subtarget.hasV5TEOps()) {
880 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::STRD));
881 AddDReg(MIB, SrcReg, ARM::gsub_0, getKillRegState(isKill), TRI);
882 AddDReg(MIB, SrcReg, ARM::gsub_1, 0, TRI);
883 MIB.addFrameIndex(FI).addReg(0).addImm(0).addMemOperand(MMO);
887 // Fallback to STM instruction, which has existed since the dawn of
889 MachineInstrBuilder MIB =
890 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::STMIA))
891 .addFrameIndex(FI).addMemOperand(MMO));
892 AddDReg(MIB, SrcReg, ARM::gsub_0, getKillRegState(isKill), TRI);
893 AddDReg(MIB, SrcReg, ARM::gsub_1, 0, TRI);
896 llvm_unreachable("Unknown reg class!");
899 if (ARM::DPairRegClass.hasSubClassEq(RC)) {
900 // Use aligned spills if the stack can be realigned.
901 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
902 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VST1q64))
903 .addFrameIndex(FI).addImm(16)
904 .addReg(SrcReg, getKillRegState(isKill))
905 .addMemOperand(MMO));
907 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMQIA))
908 .addReg(SrcReg, getKillRegState(isKill))
910 .addMemOperand(MMO));
913 llvm_unreachable("Unknown reg class!");
916 if (ARM::DTripleRegClass.hasSubClassEq(RC)) {
917 // Use aligned spills if the stack can be realigned.
918 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
919 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VST1d64TPseudo))
920 .addFrameIndex(FI).addImm(16)
921 .addReg(SrcReg, getKillRegState(isKill))
922 .addMemOperand(MMO));
924 MachineInstrBuilder MIB =
925 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMDIA))
928 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
929 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
930 AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
933 llvm_unreachable("Unknown reg class!");
936 if (ARM::QQPRRegClass.hasSubClassEq(RC) || ARM::DQuadRegClass.hasSubClassEq(RC)) {
937 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
938 // FIXME: It's possible to only store part of the QQ register if the
939 // spilled def has a sub-register index.
940 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VST1d64QPseudo))
941 .addFrameIndex(FI).addImm(16)
942 .addReg(SrcReg, getKillRegState(isKill))
943 .addMemOperand(MMO));
945 MachineInstrBuilder MIB =
946 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMDIA))
949 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
950 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
951 MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
952 AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI);
955 llvm_unreachable("Unknown reg class!");
958 if (ARM::QQQQPRRegClass.hasSubClassEq(RC)) {
959 MachineInstrBuilder MIB =
960 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMDIA))
963 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
964 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
965 MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
966 MIB = AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI);
967 MIB = AddDReg(MIB, SrcReg, ARM::dsub_4, 0, TRI);
968 MIB = AddDReg(MIB, SrcReg, ARM::dsub_5, 0, TRI);
969 MIB = AddDReg(MIB, SrcReg, ARM::dsub_6, 0, TRI);
970 AddDReg(MIB, SrcReg, ARM::dsub_7, 0, TRI);
972 llvm_unreachable("Unknown reg class!");
975 llvm_unreachable("Unknown reg class!");
980 ARMBaseInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
981 int &FrameIndex) const {
982 switch (MI->getOpcode()) {
985 case ARM::t2STRs: // FIXME: don't use t2STRs to access frame.
986 if (MI->getOperand(1).isFI() &&
987 MI->getOperand(2).isReg() &&
988 MI->getOperand(3).isImm() &&
989 MI->getOperand(2).getReg() == 0 &&
990 MI->getOperand(3).getImm() == 0) {
991 FrameIndex = MI->getOperand(1).getIndex();
992 return MI->getOperand(0).getReg();
1000 if (MI->getOperand(1).isFI() &&
1001 MI->getOperand(2).isImm() &&
1002 MI->getOperand(2).getImm() == 0) {
1003 FrameIndex = MI->getOperand(1).getIndex();
1004 return MI->getOperand(0).getReg();
1008 case ARM::VST1d64TPseudo:
1009 case ARM::VST1d64QPseudo:
1010 if (MI->getOperand(0).isFI() &&
1011 MI->getOperand(2).getSubReg() == 0) {
1012 FrameIndex = MI->getOperand(0).getIndex();
1013 return MI->getOperand(2).getReg();
1017 if (MI->getOperand(1).isFI() &&
1018 MI->getOperand(0).getSubReg() == 0) {
1019 FrameIndex = MI->getOperand(1).getIndex();
1020 return MI->getOperand(0).getReg();
1028 unsigned ARMBaseInstrInfo::isStoreToStackSlotPostFE(const MachineInstr *MI,
1029 int &FrameIndex) const {
1030 const MachineMemOperand *Dummy;
1031 return MI->mayStore() && hasStoreToStackSlot(MI, Dummy, FrameIndex);
1034 void ARMBaseInstrInfo::
1035 loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
1036 unsigned DestReg, int FI,
1037 const TargetRegisterClass *RC,
1038 const TargetRegisterInfo *TRI) const {
1040 if (I != MBB.end()) DL = I->getDebugLoc();
1041 MachineFunction &MF = *MBB.getParent();
1042 MachineFrameInfo &MFI = *MF.getFrameInfo();
1043 unsigned Align = MFI.getObjectAlignment(FI);
1044 MachineMemOperand *MMO = MF.getMachineMemOperand(
1045 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOLoad,
1046 MFI.getObjectSize(FI), Align);
1048 switch (RC->getSize()) {
1050 if (ARM::GPRRegClass.hasSubClassEq(RC)) {
1051 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::LDRi12), DestReg)
1052 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
1054 } else if (ARM::SPRRegClass.hasSubClassEq(RC)) {
1055 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDRS), DestReg)
1056 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
1058 llvm_unreachable("Unknown reg class!");
1061 if (ARM::DPRRegClass.hasSubClassEq(RC)) {
1062 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDRD), DestReg)
1063 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
1064 } else if (ARM::GPRPairRegClass.hasSubClassEq(RC)) {
1065 MachineInstrBuilder MIB;
1067 if (Subtarget.hasV5TEOps()) {
1068 MIB = BuildMI(MBB, I, DL, get(ARM::LDRD));
1069 AddDReg(MIB, DestReg, ARM::gsub_0, RegState::DefineNoRead, TRI);
1070 AddDReg(MIB, DestReg, ARM::gsub_1, RegState::DefineNoRead, TRI);
1071 MIB.addFrameIndex(FI).addReg(0).addImm(0).addMemOperand(MMO);
1073 AddDefaultPred(MIB);
1075 // Fallback to LDM instruction, which has existed since the dawn of
1077 MIB = AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::LDMIA))
1078 .addFrameIndex(FI).addMemOperand(MMO));
1079 MIB = AddDReg(MIB, DestReg, ARM::gsub_0, RegState::DefineNoRead, TRI);
1080 MIB = AddDReg(MIB, DestReg, ARM::gsub_1, RegState::DefineNoRead, TRI);
1083 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1084 MIB.addReg(DestReg, RegState::ImplicitDefine);
1086 llvm_unreachable("Unknown reg class!");
1089 if (ARM::DPairRegClass.hasSubClassEq(RC)) {
1090 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
1091 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLD1q64), DestReg)
1092 .addFrameIndex(FI).addImm(16)
1093 .addMemOperand(MMO));
1095 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMQIA), DestReg)
1097 .addMemOperand(MMO));
1100 llvm_unreachable("Unknown reg class!");
1103 if (ARM::DTripleRegClass.hasSubClassEq(RC)) {
1104 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
1105 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLD1d64TPseudo), DestReg)
1106 .addFrameIndex(FI).addImm(16)
1107 .addMemOperand(MMO));
1109 MachineInstrBuilder MIB =
1110 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
1112 .addMemOperand(MMO));
1113 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::DefineNoRead, TRI);
1114 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::DefineNoRead, TRI);
1115 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::DefineNoRead, TRI);
1116 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1117 MIB.addReg(DestReg, RegState::ImplicitDefine);
1120 llvm_unreachable("Unknown reg class!");
1123 if (ARM::QQPRRegClass.hasSubClassEq(RC) || ARM::DQuadRegClass.hasSubClassEq(RC)) {
1124 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
1125 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLD1d64QPseudo), DestReg)
1126 .addFrameIndex(FI).addImm(16)
1127 .addMemOperand(MMO));
1129 MachineInstrBuilder MIB =
1130 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
1132 .addMemOperand(MMO);
1133 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::DefineNoRead, TRI);
1134 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::DefineNoRead, TRI);
1135 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::DefineNoRead, TRI);
1136 MIB = AddDReg(MIB, DestReg, ARM::dsub_3, RegState::DefineNoRead, TRI);
1137 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1138 MIB.addReg(DestReg, RegState::ImplicitDefine);
1141 llvm_unreachable("Unknown reg class!");
1144 if (ARM::QQQQPRRegClass.hasSubClassEq(RC)) {
1145 MachineInstrBuilder MIB =
1146 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
1148 .addMemOperand(MMO);
1149 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::DefineNoRead, TRI);
1150 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::DefineNoRead, TRI);
1151 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::DefineNoRead, TRI);
1152 MIB = AddDReg(MIB, DestReg, ARM::dsub_3, RegState::DefineNoRead, TRI);
1153 MIB = AddDReg(MIB, DestReg, ARM::dsub_4, RegState::DefineNoRead, TRI);
1154 MIB = AddDReg(MIB, DestReg, ARM::dsub_5, RegState::DefineNoRead, TRI);
1155 MIB = AddDReg(MIB, DestReg, ARM::dsub_6, RegState::DefineNoRead, TRI);
1156 MIB = AddDReg(MIB, DestReg, ARM::dsub_7, RegState::DefineNoRead, TRI);
1157 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1158 MIB.addReg(DestReg, RegState::ImplicitDefine);
1160 llvm_unreachable("Unknown reg class!");
1163 llvm_unreachable("Unknown regclass!");
1168 ARMBaseInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
1169 int &FrameIndex) const {
1170 switch (MI->getOpcode()) {
1173 case ARM::t2LDRs: // FIXME: don't use t2LDRs to access frame.
1174 if (MI->getOperand(1).isFI() &&
1175 MI->getOperand(2).isReg() &&
1176 MI->getOperand(3).isImm() &&
1177 MI->getOperand(2).getReg() == 0 &&
1178 MI->getOperand(3).getImm() == 0) {
1179 FrameIndex = MI->getOperand(1).getIndex();
1180 return MI->getOperand(0).getReg();
1188 if (MI->getOperand(1).isFI() &&
1189 MI->getOperand(2).isImm() &&
1190 MI->getOperand(2).getImm() == 0) {
1191 FrameIndex = MI->getOperand(1).getIndex();
1192 return MI->getOperand(0).getReg();
1196 case ARM::VLD1d64TPseudo:
1197 case ARM::VLD1d64QPseudo:
1198 if (MI->getOperand(1).isFI() &&
1199 MI->getOperand(0).getSubReg() == 0) {
1200 FrameIndex = MI->getOperand(1).getIndex();
1201 return MI->getOperand(0).getReg();
1205 if (MI->getOperand(1).isFI() &&
1206 MI->getOperand(0).getSubReg() == 0) {
1207 FrameIndex = MI->getOperand(1).getIndex();
1208 return MI->getOperand(0).getReg();
1216 unsigned ARMBaseInstrInfo::isLoadFromStackSlotPostFE(const MachineInstr *MI,
1217 int &FrameIndex) const {
1218 const MachineMemOperand *Dummy;
1219 return MI->mayLoad() && hasLoadFromStackSlot(MI, Dummy, FrameIndex);
1223 ARMBaseInstrInfo::expandPostRAPseudo(MachineBasicBlock::iterator MI) const {
1224 MachineFunction &MF = *MI->getParent()->getParent();
1225 Reloc::Model RM = MF.getTarget().getRelocationModel();
1227 if (MI->getOpcode() == TargetOpcode::LOAD_STACK_GUARD) {
1228 assert(getSubtarget().getTargetTriple().isOSBinFormatMachO() &&
1229 "LOAD_STACK_GUARD currently supported only for MachO.");
1230 expandLoadStackGuard(MI, RM);
1231 MI->getParent()->erase(MI);
1235 // This hook gets to expand COPY instructions before they become
1236 // copyPhysReg() calls. Look for VMOVS instructions that can legally be
1237 // widened to VMOVD. We prefer the VMOVD when possible because it may be
1238 // changed into a VORR that can go down the NEON pipeline.
1239 if (!WidenVMOVS || !MI->isCopy() || Subtarget.isCortexA15() ||
1240 Subtarget.isFPOnlySP())
1243 // Look for a copy between even S-registers. That is where we keep floats
1244 // when using NEON v2f32 instructions for f32 arithmetic.
1245 unsigned DstRegS = MI->getOperand(0).getReg();
1246 unsigned SrcRegS = MI->getOperand(1).getReg();
1247 if (!ARM::SPRRegClass.contains(DstRegS, SrcRegS))
1250 const TargetRegisterInfo *TRI = &getRegisterInfo();
1251 unsigned DstRegD = TRI->getMatchingSuperReg(DstRegS, ARM::ssub_0,
1253 unsigned SrcRegD = TRI->getMatchingSuperReg(SrcRegS, ARM::ssub_0,
1255 if (!DstRegD || !SrcRegD)
1258 // We want to widen this into a DstRegD = VMOVD SrcRegD copy. This is only
1259 // legal if the COPY already defines the full DstRegD, and it isn't a
1260 // sub-register insertion.
1261 if (!MI->definesRegister(DstRegD, TRI) || MI->readsRegister(DstRegD, TRI))
1264 // A dead copy shouldn't show up here, but reject it just in case.
1265 if (MI->getOperand(0).isDead())
1268 // All clear, widen the COPY.
1269 DEBUG(dbgs() << "widening: " << *MI);
1270 MachineInstrBuilder MIB(*MI->getParent()->getParent(), MI);
1272 // Get rid of the old <imp-def> of DstRegD. Leave it if it defines a Q-reg
1273 // or some other super-register.
1274 int ImpDefIdx = MI->findRegisterDefOperandIdx(DstRegD);
1275 if (ImpDefIdx != -1)
1276 MI->RemoveOperand(ImpDefIdx);
1278 // Change the opcode and operands.
1279 MI->setDesc(get(ARM::VMOVD));
1280 MI->getOperand(0).setReg(DstRegD);
1281 MI->getOperand(1).setReg(SrcRegD);
1282 AddDefaultPred(MIB);
1284 // We are now reading SrcRegD instead of SrcRegS. This may upset the
1285 // register scavenger and machine verifier, so we need to indicate that we
1286 // are reading an undefined value from SrcRegD, but a proper value from
1288 MI->getOperand(1).setIsUndef();
1289 MIB.addReg(SrcRegS, RegState::Implicit);
1291 // SrcRegD may actually contain an unrelated value in the ssub_1
1292 // sub-register. Don't kill it. Only kill the ssub_0 sub-register.
1293 if (MI->getOperand(1).isKill()) {
1294 MI->getOperand(1).setIsKill(false);
1295 MI->addRegisterKilled(SrcRegS, TRI, true);
1298 DEBUG(dbgs() << "replaced by: " << *MI);
1302 /// Create a copy of a const pool value. Update CPI to the new index and return
1304 static unsigned duplicateCPV(MachineFunction &MF, unsigned &CPI) {
1305 MachineConstantPool *MCP = MF.getConstantPool();
1306 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1308 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPI];
1309 assert(MCPE.isMachineConstantPoolEntry() &&
1310 "Expecting a machine constantpool entry!");
1311 ARMConstantPoolValue *ACPV =
1312 static_cast<ARMConstantPoolValue*>(MCPE.Val.MachineCPVal);
1314 unsigned PCLabelId = AFI->createPICLabelUId();
1315 ARMConstantPoolValue *NewCPV = nullptr;
1317 // FIXME: The below assumes PIC relocation model and that the function
1318 // is Thumb mode (t1 or t2). PCAdjustment would be 8 for ARM mode PIC, and
1319 // zero for non-PIC in ARM or Thumb. The callers are all of thumb LDR
1320 // instructions, so that's probably OK, but is PIC always correct when
1322 if (ACPV->isGlobalValue())
1323 NewCPV = ARMConstantPoolConstant::
1324 Create(cast<ARMConstantPoolConstant>(ACPV)->getGV(), PCLabelId,
1326 else if (ACPV->isExtSymbol())
1327 NewCPV = ARMConstantPoolSymbol::
1328 Create(MF.getFunction()->getContext(),
1329 cast<ARMConstantPoolSymbol>(ACPV)->getSymbol(), PCLabelId, 4);
1330 else if (ACPV->isBlockAddress())
1331 NewCPV = ARMConstantPoolConstant::
1332 Create(cast<ARMConstantPoolConstant>(ACPV)->getBlockAddress(), PCLabelId,
1333 ARMCP::CPBlockAddress, 4);
1334 else if (ACPV->isLSDA())
1335 NewCPV = ARMConstantPoolConstant::Create(MF.getFunction(), PCLabelId,
1337 else if (ACPV->isMachineBasicBlock())
1338 NewCPV = ARMConstantPoolMBB::
1339 Create(MF.getFunction()->getContext(),
1340 cast<ARMConstantPoolMBB>(ACPV)->getMBB(), PCLabelId, 4);
1342 llvm_unreachable("Unexpected ARM constantpool value type!!");
1343 CPI = MCP->getConstantPoolIndex(NewCPV, MCPE.getAlignment());
1347 void ARMBaseInstrInfo::
1348 reMaterialize(MachineBasicBlock &MBB,
1349 MachineBasicBlock::iterator I,
1350 unsigned DestReg, unsigned SubIdx,
1351 const MachineInstr *Orig,
1352 const TargetRegisterInfo &TRI) const {
1353 unsigned Opcode = Orig->getOpcode();
1356 MachineInstr *MI = MBB.getParent()->CloneMachineInstr(Orig);
1357 MI->substituteRegister(Orig->getOperand(0).getReg(), DestReg, SubIdx, TRI);
1361 case ARM::tLDRpci_pic:
1362 case ARM::t2LDRpci_pic: {
1363 MachineFunction &MF = *MBB.getParent();
1364 unsigned CPI = Orig->getOperand(1).getIndex();
1365 unsigned PCLabelId = duplicateCPV(MF, CPI);
1366 MachineInstrBuilder MIB = BuildMI(MBB, I, Orig->getDebugLoc(), get(Opcode),
1368 .addConstantPoolIndex(CPI).addImm(PCLabelId);
1369 MIB->setMemRefs(Orig->memoperands_begin(), Orig->memoperands_end());
1376 ARMBaseInstrInfo::duplicate(MachineInstr *Orig, MachineFunction &MF) const {
1377 MachineInstr *MI = TargetInstrInfo::duplicate(Orig, MF);
1378 switch(Orig->getOpcode()) {
1379 case ARM::tLDRpci_pic:
1380 case ARM::t2LDRpci_pic: {
1381 unsigned CPI = Orig->getOperand(1).getIndex();
1382 unsigned PCLabelId = duplicateCPV(MF, CPI);
1383 Orig->getOperand(1).setIndex(CPI);
1384 Orig->getOperand(2).setImm(PCLabelId);
1391 bool ARMBaseInstrInfo::produceSameValue(const MachineInstr *MI0,
1392 const MachineInstr *MI1,
1393 const MachineRegisterInfo *MRI) const {
1394 unsigned Opcode = MI0->getOpcode();
1395 if (Opcode == ARM::t2LDRpci ||
1396 Opcode == ARM::t2LDRpci_pic ||
1397 Opcode == ARM::tLDRpci ||
1398 Opcode == ARM::tLDRpci_pic ||
1399 Opcode == ARM::LDRLIT_ga_pcrel ||
1400 Opcode == ARM::LDRLIT_ga_pcrel_ldr ||
1401 Opcode == ARM::tLDRLIT_ga_pcrel ||
1402 Opcode == ARM::MOV_ga_pcrel ||
1403 Opcode == ARM::MOV_ga_pcrel_ldr ||
1404 Opcode == ARM::t2MOV_ga_pcrel) {
1405 if (MI1->getOpcode() != Opcode)
1407 if (MI0->getNumOperands() != MI1->getNumOperands())
1410 const MachineOperand &MO0 = MI0->getOperand(1);
1411 const MachineOperand &MO1 = MI1->getOperand(1);
1412 if (MO0.getOffset() != MO1.getOffset())
1415 if (Opcode == ARM::LDRLIT_ga_pcrel ||
1416 Opcode == ARM::LDRLIT_ga_pcrel_ldr ||
1417 Opcode == ARM::tLDRLIT_ga_pcrel ||
1418 Opcode == ARM::MOV_ga_pcrel ||
1419 Opcode == ARM::MOV_ga_pcrel_ldr ||
1420 Opcode == ARM::t2MOV_ga_pcrel)
1421 // Ignore the PC labels.
1422 return MO0.getGlobal() == MO1.getGlobal();
1424 const MachineFunction *MF = MI0->getParent()->getParent();
1425 const MachineConstantPool *MCP = MF->getConstantPool();
1426 int CPI0 = MO0.getIndex();
1427 int CPI1 = MO1.getIndex();
1428 const MachineConstantPoolEntry &MCPE0 = MCP->getConstants()[CPI0];
1429 const MachineConstantPoolEntry &MCPE1 = MCP->getConstants()[CPI1];
1430 bool isARMCP0 = MCPE0.isMachineConstantPoolEntry();
1431 bool isARMCP1 = MCPE1.isMachineConstantPoolEntry();
1432 if (isARMCP0 && isARMCP1) {
1433 ARMConstantPoolValue *ACPV0 =
1434 static_cast<ARMConstantPoolValue*>(MCPE0.Val.MachineCPVal);
1435 ARMConstantPoolValue *ACPV1 =
1436 static_cast<ARMConstantPoolValue*>(MCPE1.Val.MachineCPVal);
1437 return ACPV0->hasSameValue(ACPV1);
1438 } else if (!isARMCP0 && !isARMCP1) {
1439 return MCPE0.Val.ConstVal == MCPE1.Val.ConstVal;
1442 } else if (Opcode == ARM::PICLDR) {
1443 if (MI1->getOpcode() != Opcode)
1445 if (MI0->getNumOperands() != MI1->getNumOperands())
1448 unsigned Addr0 = MI0->getOperand(1).getReg();
1449 unsigned Addr1 = MI1->getOperand(1).getReg();
1450 if (Addr0 != Addr1) {
1452 !TargetRegisterInfo::isVirtualRegister(Addr0) ||
1453 !TargetRegisterInfo::isVirtualRegister(Addr1))
1456 // This assumes SSA form.
1457 MachineInstr *Def0 = MRI->getVRegDef(Addr0);
1458 MachineInstr *Def1 = MRI->getVRegDef(Addr1);
1459 // Check if the loaded value, e.g. a constantpool of a global address, are
1461 if (!produceSameValue(Def0, Def1, MRI))
1465 for (unsigned i = 3, e = MI0->getNumOperands(); i != e; ++i) {
1466 // %vreg12<def> = PICLDR %vreg11, 0, pred:14, pred:%noreg
1467 const MachineOperand &MO0 = MI0->getOperand(i);
1468 const MachineOperand &MO1 = MI1->getOperand(i);
1469 if (!MO0.isIdenticalTo(MO1))
1475 return MI0->isIdenticalTo(MI1, MachineInstr::IgnoreVRegDefs);
1478 /// areLoadsFromSameBasePtr - This is used by the pre-regalloc scheduler to
1479 /// determine if two loads are loading from the same base address. It should
1480 /// only return true if the base pointers are the same and the only differences
1481 /// between the two addresses is the offset. It also returns the offsets by
1484 /// FIXME: remove this in favor of the MachineInstr interface once pre-RA-sched
1485 /// is permanently disabled.
1486 bool ARMBaseInstrInfo::areLoadsFromSameBasePtr(SDNode *Load1, SDNode *Load2,
1488 int64_t &Offset2) const {
1489 // Don't worry about Thumb: just ARM and Thumb2.
1490 if (Subtarget.isThumb1Only()) return false;
1492 if (!Load1->isMachineOpcode() || !Load2->isMachineOpcode())
1495 switch (Load1->getMachineOpcode()) {
1509 case ARM::t2LDRSHi8:
1511 case ARM::t2LDRBi12:
1512 case ARM::t2LDRSHi12:
1516 switch (Load2->getMachineOpcode()) {
1529 case ARM::t2LDRSHi8:
1531 case ARM::t2LDRBi12:
1532 case ARM::t2LDRSHi12:
1536 // Check if base addresses and chain operands match.
1537 if (Load1->getOperand(0) != Load2->getOperand(0) ||
1538 Load1->getOperand(4) != Load2->getOperand(4))
1541 // Index should be Reg0.
1542 if (Load1->getOperand(3) != Load2->getOperand(3))
1545 // Determine the offsets.
1546 if (isa<ConstantSDNode>(Load1->getOperand(1)) &&
1547 isa<ConstantSDNode>(Load2->getOperand(1))) {
1548 Offset1 = cast<ConstantSDNode>(Load1->getOperand(1))->getSExtValue();
1549 Offset2 = cast<ConstantSDNode>(Load2->getOperand(1))->getSExtValue();
1556 /// shouldScheduleLoadsNear - This is a used by the pre-regalloc scheduler to
1557 /// determine (in conjunction with areLoadsFromSameBasePtr) if two loads should
1558 /// be scheduled togther. On some targets if two loads are loading from
1559 /// addresses in the same cache line, it's better if they are scheduled
1560 /// together. This function takes two integers that represent the load offsets
1561 /// from the common base address. It returns true if it decides it's desirable
1562 /// to schedule the two loads together. "NumLoads" is the number of loads that
1563 /// have already been scheduled after Load1.
1565 /// FIXME: remove this in favor of the MachineInstr interface once pre-RA-sched
1566 /// is permanently disabled.
1567 bool ARMBaseInstrInfo::shouldScheduleLoadsNear(SDNode *Load1, SDNode *Load2,
1568 int64_t Offset1, int64_t Offset2,
1569 unsigned NumLoads) const {
1570 // Don't worry about Thumb: just ARM and Thumb2.
1571 if (Subtarget.isThumb1Only()) return false;
1573 assert(Offset2 > Offset1);
1575 if ((Offset2 - Offset1) / 8 > 64)
1578 // Check if the machine opcodes are different. If they are different
1579 // then we consider them to not be of the same base address,
1580 // EXCEPT in the case of Thumb2 byte loads where one is LDRBi8 and the other LDRBi12.
1581 // In this case, they are considered to be the same because they are different
1582 // encoding forms of the same basic instruction.
1583 if ((Load1->getMachineOpcode() != Load2->getMachineOpcode()) &&
1584 !((Load1->getMachineOpcode() == ARM::t2LDRBi8 &&
1585 Load2->getMachineOpcode() == ARM::t2LDRBi12) ||
1586 (Load1->getMachineOpcode() == ARM::t2LDRBi12 &&
1587 Load2->getMachineOpcode() == ARM::t2LDRBi8)))
1588 return false; // FIXME: overly conservative?
1590 // Four loads in a row should be sufficient.
1597 bool ARMBaseInstrInfo::isSchedulingBoundary(const MachineInstr *MI,
1598 const MachineBasicBlock *MBB,
1599 const MachineFunction &MF) const {
1600 // Debug info is never a scheduling boundary. It's necessary to be explicit
1601 // due to the special treatment of IT instructions below, otherwise a
1602 // dbg_value followed by an IT will result in the IT instruction being
1603 // considered a scheduling hazard, which is wrong. It should be the actual
1604 // instruction preceding the dbg_value instruction(s), just like it is
1605 // when debug info is not present.
1606 if (MI->isDebugValue())
1609 // Terminators and labels can't be scheduled around.
1610 if (MI->isTerminator() || MI->isPosition())
1613 // Treat the start of the IT block as a scheduling boundary, but schedule
1614 // t2IT along with all instructions following it.
1615 // FIXME: This is a big hammer. But the alternative is to add all potential
1616 // true and anti dependencies to IT block instructions as implicit operands
1617 // to the t2IT instruction. The added compile time and complexity does not
1619 MachineBasicBlock::const_iterator I = MI;
1620 // Make sure to skip any dbg_value instructions
1621 while (++I != MBB->end() && I->isDebugValue())
1623 if (I != MBB->end() && I->getOpcode() == ARM::t2IT)
1626 // Don't attempt to schedule around any instruction that defines
1627 // a stack-oriented pointer, as it's unlikely to be profitable. This
1628 // saves compile time, because it doesn't require every single
1629 // stack slot reference to depend on the instruction that does the
1631 // Calls don't actually change the stack pointer, even if they have imp-defs.
1632 // No ARM calling conventions change the stack pointer. (X86 calling
1633 // conventions sometimes do).
1634 if (!MI->isCall() && MI->definesRegister(ARM::SP))
1640 bool ARMBaseInstrInfo::
1641 isProfitableToIfCvt(MachineBasicBlock &MBB,
1642 unsigned NumCycles, unsigned ExtraPredCycles,
1643 const BranchProbability &Probability) const {
1647 // If we are optimizing for size, see if the branch in the predecessor can be
1648 // lowered to cbn?z by the constant island lowering pass, and return false if
1649 // so. This results in a shorter instruction sequence.
1650 if (MBB.getParent()->getFunction()->optForSize()) {
1651 MachineBasicBlock *Pred = *MBB.pred_begin();
1652 if (!Pred->empty()) {
1653 MachineInstr *LastMI = &*Pred->rbegin();
1654 if (LastMI->getOpcode() == ARM::t2Bcc) {
1655 MachineBasicBlock::iterator CmpMI = LastMI;
1656 if (CmpMI != Pred->begin()) {
1658 if (CmpMI->getOpcode() == ARM::tCMPi8 ||
1659 CmpMI->getOpcode() == ARM::t2CMPri) {
1660 unsigned Reg = CmpMI->getOperand(0).getReg();
1661 unsigned PredReg = 0;
1662 ARMCC::CondCodes P = getInstrPredicate(CmpMI, PredReg);
1663 if (P == ARMCC::AL && CmpMI->getOperand(1).getImm() == 0 &&
1664 isARMLowRegister(Reg))
1672 // Attempt to estimate the relative costs of predication versus branching.
1673 unsigned UnpredCost = Probability.getNumerator() * NumCycles;
1674 UnpredCost /= Probability.getDenominator();
1675 UnpredCost += 1; // The branch itself
1676 UnpredCost += Subtarget.getMispredictionPenalty() / 10;
1678 return (NumCycles + ExtraPredCycles) <= UnpredCost;
1681 bool ARMBaseInstrInfo::
1682 isProfitableToIfCvt(MachineBasicBlock &TMBB,
1683 unsigned TCycles, unsigned TExtra,
1684 MachineBasicBlock &FMBB,
1685 unsigned FCycles, unsigned FExtra,
1686 const BranchProbability &Probability) const {
1687 if (!TCycles || !FCycles)
1690 // Attempt to estimate the relative costs of predication versus branching.
1691 unsigned TUnpredCost = Probability.getNumerator() * TCycles;
1692 TUnpredCost /= Probability.getDenominator();
1694 uint32_t Comp = Probability.getDenominator() - Probability.getNumerator();
1695 unsigned FUnpredCost = Comp * FCycles;
1696 FUnpredCost /= Probability.getDenominator();
1698 unsigned UnpredCost = TUnpredCost + FUnpredCost;
1699 UnpredCost += 1; // The branch itself
1700 UnpredCost += Subtarget.getMispredictionPenalty() / 10;
1702 return (TCycles + FCycles + TExtra + FExtra) <= UnpredCost;
1706 ARMBaseInstrInfo::isProfitableToUnpredicate(MachineBasicBlock &TMBB,
1707 MachineBasicBlock &FMBB) const {
1708 // Reduce false anti-dependencies to let Swift's out-of-order execution
1709 // engine do its thing.
1710 return Subtarget.isSwift();
1713 /// getInstrPredicate - If instruction is predicated, returns its predicate
1714 /// condition, otherwise returns AL. It also returns the condition code
1715 /// register by reference.
1717 llvm::getInstrPredicate(const MachineInstr *MI, unsigned &PredReg) {
1718 int PIdx = MI->findFirstPredOperandIdx();
1724 PredReg = MI->getOperand(PIdx+1).getReg();
1725 return (ARMCC::CondCodes)MI->getOperand(PIdx).getImm();
1729 unsigned llvm::getMatchingCondBranchOpcode(unsigned Opc) {
1734 if (Opc == ARM::t2B)
1737 llvm_unreachable("Unknown unconditional branch opcode!");
1740 /// commuteInstruction - Handle commutable instructions.
1742 ARMBaseInstrInfo::commuteInstruction(MachineInstr *MI, bool NewMI) const {
1743 switch (MI->getOpcode()) {
1745 case ARM::t2MOVCCr: {
1746 // MOVCC can be commuted by inverting the condition.
1747 unsigned PredReg = 0;
1748 ARMCC::CondCodes CC = getInstrPredicate(MI, PredReg);
1749 // MOVCC AL can't be inverted. Shouldn't happen.
1750 if (CC == ARMCC::AL || PredReg != ARM::CPSR)
1752 MI = TargetInstrInfo::commuteInstruction(MI, NewMI);
1755 // After swapping the MOVCC operands, also invert the condition.
1756 MI->getOperand(MI->findFirstPredOperandIdx())
1757 .setImm(ARMCC::getOppositeCondition(CC));
1761 return TargetInstrInfo::commuteInstruction(MI, NewMI);
1764 /// Identify instructions that can be folded into a MOVCC instruction, and
1765 /// return the defining instruction.
1766 static MachineInstr *canFoldIntoMOVCC(unsigned Reg,
1767 const MachineRegisterInfo &MRI,
1768 const TargetInstrInfo *TII) {
1769 if (!TargetRegisterInfo::isVirtualRegister(Reg))
1771 if (!MRI.hasOneNonDBGUse(Reg))
1773 MachineInstr *MI = MRI.getVRegDef(Reg);
1776 // MI is folded into the MOVCC by predicating it.
1777 if (!MI->isPredicable())
1779 // Check if MI has any non-dead defs or physreg uses. This also detects
1780 // predicated instructions which will be reading CPSR.
1781 for (unsigned i = 1, e = MI->getNumOperands(); i != e; ++i) {
1782 const MachineOperand &MO = MI->getOperand(i);
1783 // Reject frame index operands, PEI can't handle the predicated pseudos.
1784 if (MO.isFI() || MO.isCPI() || MO.isJTI())
1788 // MI can't have any tied operands, that would conflict with predication.
1791 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
1793 if (MO.isDef() && !MO.isDead())
1796 bool DontMoveAcrossStores = true;
1797 if (!MI->isSafeToMove(/* AliasAnalysis = */ nullptr, DontMoveAcrossStores))
1802 bool ARMBaseInstrInfo::analyzeSelect(const MachineInstr *MI,
1803 SmallVectorImpl<MachineOperand> &Cond,
1804 unsigned &TrueOp, unsigned &FalseOp,
1805 bool &Optimizable) const {
1806 assert((MI->getOpcode() == ARM::MOVCCr || MI->getOpcode() == ARM::t2MOVCCr) &&
1807 "Unknown select instruction");
1812 // 3: Condition code.
1816 Cond.push_back(MI->getOperand(3));
1817 Cond.push_back(MI->getOperand(4));
1818 // We can always fold a def.
1824 ARMBaseInstrInfo::optimizeSelect(MachineInstr *MI,
1825 SmallPtrSetImpl<MachineInstr *> &SeenMIs,
1826 bool PreferFalse) const {
1827 assert((MI->getOpcode() == ARM::MOVCCr || MI->getOpcode() == ARM::t2MOVCCr) &&
1828 "Unknown select instruction");
1829 MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo();
1830 MachineInstr *DefMI = canFoldIntoMOVCC(MI->getOperand(2).getReg(), MRI, this);
1831 bool Invert = !DefMI;
1833 DefMI = canFoldIntoMOVCC(MI->getOperand(1).getReg(), MRI, this);
1837 // Find new register class to use.
1838 MachineOperand FalseReg = MI->getOperand(Invert ? 2 : 1);
1839 unsigned DestReg = MI->getOperand(0).getReg();
1840 const TargetRegisterClass *PreviousClass = MRI.getRegClass(FalseReg.getReg());
1841 if (!MRI.constrainRegClass(DestReg, PreviousClass))
1844 // Create a new predicated version of DefMI.
1845 // Rfalse is the first use.
1846 MachineInstrBuilder NewMI = BuildMI(*MI->getParent(), MI, MI->getDebugLoc(),
1847 DefMI->getDesc(), DestReg);
1849 // Copy all the DefMI operands, excluding its (null) predicate.
1850 const MCInstrDesc &DefDesc = DefMI->getDesc();
1851 for (unsigned i = 1, e = DefDesc.getNumOperands();
1852 i != e && !DefDesc.OpInfo[i].isPredicate(); ++i)
1853 NewMI.addOperand(DefMI->getOperand(i));
1855 unsigned CondCode = MI->getOperand(3).getImm();
1857 NewMI.addImm(ARMCC::getOppositeCondition(ARMCC::CondCodes(CondCode)));
1859 NewMI.addImm(CondCode);
1860 NewMI.addOperand(MI->getOperand(4));
1862 // DefMI is not the -S version that sets CPSR, so add an optional %noreg.
1863 if (NewMI->hasOptionalDef())
1864 AddDefaultCC(NewMI);
1866 // The output register value when the predicate is false is an implicit
1867 // register operand tied to the first def.
1868 // The tie makes the register allocator ensure the FalseReg is allocated the
1869 // same register as operand 0.
1870 FalseReg.setImplicit();
1871 NewMI.addOperand(FalseReg);
1872 NewMI->tieOperands(0, NewMI->getNumOperands() - 1);
1874 // Update SeenMIs set: register newly created MI and erase removed DefMI.
1875 SeenMIs.insert(NewMI);
1876 SeenMIs.erase(DefMI);
1878 // If MI is inside a loop, and DefMI is outside the loop, then kill flags on
1879 // DefMI would be invalid when tranferred inside the loop. Checking for a
1880 // loop is expensive, but at least remove kill flags if they are in different
1882 if (DefMI->getParent() != MI->getParent())
1883 NewMI->clearKillInfo();
1885 // The caller will erase MI, but not DefMI.
1886 DefMI->eraseFromParent();
1890 /// Map pseudo instructions that imply an 'S' bit onto real opcodes. Whether the
1891 /// instruction is encoded with an 'S' bit is determined by the optional CPSR
1894 /// This will go away once we can teach tblgen how to set the optional CPSR def
1896 struct AddSubFlagsOpcodePair {
1898 uint16_t MachineOpc;
1901 static const AddSubFlagsOpcodePair AddSubFlagsOpcodeMap[] = {
1902 {ARM::ADDSri, ARM::ADDri},
1903 {ARM::ADDSrr, ARM::ADDrr},
1904 {ARM::ADDSrsi, ARM::ADDrsi},
1905 {ARM::ADDSrsr, ARM::ADDrsr},
1907 {ARM::SUBSri, ARM::SUBri},
1908 {ARM::SUBSrr, ARM::SUBrr},
1909 {ARM::SUBSrsi, ARM::SUBrsi},
1910 {ARM::SUBSrsr, ARM::SUBrsr},
1912 {ARM::RSBSri, ARM::RSBri},
1913 {ARM::RSBSrsi, ARM::RSBrsi},
1914 {ARM::RSBSrsr, ARM::RSBrsr},
1916 {ARM::t2ADDSri, ARM::t2ADDri},
1917 {ARM::t2ADDSrr, ARM::t2ADDrr},
1918 {ARM::t2ADDSrs, ARM::t2ADDrs},
1920 {ARM::t2SUBSri, ARM::t2SUBri},
1921 {ARM::t2SUBSrr, ARM::t2SUBrr},
1922 {ARM::t2SUBSrs, ARM::t2SUBrs},
1924 {ARM::t2RSBSri, ARM::t2RSBri},
1925 {ARM::t2RSBSrs, ARM::t2RSBrs},
1928 unsigned llvm::convertAddSubFlagsOpcode(unsigned OldOpc) {
1929 for (unsigned i = 0, e = array_lengthof(AddSubFlagsOpcodeMap); i != e; ++i)
1930 if (OldOpc == AddSubFlagsOpcodeMap[i].PseudoOpc)
1931 return AddSubFlagsOpcodeMap[i].MachineOpc;
1935 void llvm::emitARMRegPlusImmediate(MachineBasicBlock &MBB,
1936 MachineBasicBlock::iterator &MBBI, DebugLoc dl,
1937 unsigned DestReg, unsigned BaseReg, int NumBytes,
1938 ARMCC::CondCodes Pred, unsigned PredReg,
1939 const ARMBaseInstrInfo &TII, unsigned MIFlags) {
1940 if (NumBytes == 0 && DestReg != BaseReg) {
1941 BuildMI(MBB, MBBI, dl, TII.get(ARM::MOVr), DestReg)
1942 .addReg(BaseReg, RegState::Kill)
1943 .addImm((unsigned)Pred).addReg(PredReg).addReg(0)
1944 .setMIFlags(MIFlags);
1948 bool isSub = NumBytes < 0;
1949 if (isSub) NumBytes = -NumBytes;
1952 unsigned RotAmt = ARM_AM::getSOImmValRotate(NumBytes);
1953 unsigned ThisVal = NumBytes & ARM_AM::rotr32(0xFF, RotAmt);
1954 assert(ThisVal && "Didn't extract field correctly");
1956 // We will handle these bits from offset, clear them.
1957 NumBytes &= ~ThisVal;
1959 assert(ARM_AM::getSOImmVal(ThisVal) != -1 && "Bit extraction didn't work?");
1961 // Build the new ADD / SUB.
1962 unsigned Opc = isSub ? ARM::SUBri : ARM::ADDri;
1963 BuildMI(MBB, MBBI, dl, TII.get(Opc), DestReg)
1964 .addReg(BaseReg, RegState::Kill).addImm(ThisVal)
1965 .addImm((unsigned)Pred).addReg(PredReg).addReg(0)
1966 .setMIFlags(MIFlags);
1971 static bool isAnySubRegLive(unsigned Reg, const TargetRegisterInfo *TRI,
1973 for (MCSubRegIterator Subreg(Reg, TRI, /* IncludeSelf */ true);
1974 Subreg.isValid(); ++Subreg)
1975 if (MI->getParent()->computeRegisterLiveness(TRI, *Subreg, MI) !=
1976 MachineBasicBlock::LQR_Dead)
1980 bool llvm::tryFoldSPUpdateIntoPushPop(const ARMSubtarget &Subtarget,
1981 MachineFunction &MF, MachineInstr *MI,
1982 unsigned NumBytes) {
1983 // This optimisation potentially adds lots of load and store
1984 // micro-operations, it's only really a great benefit to code-size.
1985 if (!MF.getFunction()->optForMinSize())
1988 // If only one register is pushed/popped, LLVM can use an LDR/STR
1989 // instead. We can't modify those so make sure we're dealing with an
1990 // instruction we understand.
1991 bool IsPop = isPopOpcode(MI->getOpcode());
1992 bool IsPush = isPushOpcode(MI->getOpcode());
1993 if (!IsPush && !IsPop)
1996 bool IsVFPPushPop = MI->getOpcode() == ARM::VSTMDDB_UPD ||
1997 MI->getOpcode() == ARM::VLDMDIA_UPD;
1998 bool IsT1PushPop = MI->getOpcode() == ARM::tPUSH ||
1999 MI->getOpcode() == ARM::tPOP ||
2000 MI->getOpcode() == ARM::tPOP_RET;
2002 assert((IsT1PushPop || (MI->getOperand(0).getReg() == ARM::SP &&
2003 MI->getOperand(1).getReg() == ARM::SP)) &&
2004 "trying to fold sp update into non-sp-updating push/pop");
2006 // The VFP push & pop act on D-registers, so we can only fold an adjustment
2007 // by a multiple of 8 bytes in correctly. Similarly rN is 4-bytes. Don't try
2008 // if this is violated.
2009 if (NumBytes % (IsVFPPushPop ? 8 : 4) != 0)
2012 // ARM and Thumb2 push/pop insts have explicit "sp, sp" operands (+
2013 // pred) so the list starts at 4. Thumb1 starts after the predicate.
2014 int RegListIdx = IsT1PushPop ? 2 : 4;
2016 // Calculate the space we'll need in terms of registers.
2017 unsigned FirstReg = MI->getOperand(RegListIdx).getReg();
2018 unsigned RD0Reg, RegsNeeded;
2021 RegsNeeded = NumBytes / 8;
2024 RegsNeeded = NumBytes / 4;
2027 // We're going to have to strip all list operands off before
2028 // re-adding them since the order matters, so save the existing ones
2030 SmallVector<MachineOperand, 4> RegList;
2031 for (int i = MI->getNumOperands() - 1; i >= RegListIdx; --i)
2032 RegList.push_back(MI->getOperand(i));
2034 const TargetRegisterInfo *TRI = MF.getRegInfo().getTargetRegisterInfo();
2035 const MCPhysReg *CSRegs = TRI->getCalleeSavedRegs(&MF);
2037 // Now try to find enough space in the reglist to allocate NumBytes.
2038 for (unsigned CurReg = FirstReg - 1; CurReg >= RD0Reg && RegsNeeded;
2041 // Pushing any register is completely harmless, mark the
2042 // register involved as undef since we don't care about it in
2044 RegList.push_back(MachineOperand::CreateReg(CurReg, false, false,
2045 false, false, true));
2050 // However, we can only pop an extra register if it's not live. For
2051 // registers live within the function we might clobber a return value
2052 // register; the other way a register can be live here is if it's
2054 // TODO: Currently, computeRegisterLiveness() does not report "live" if a
2055 // sub reg is live. When computeRegisterLiveness() works for sub reg, it
2056 // can replace isAnySubRegLive().
2057 if (isCalleeSavedRegister(CurReg, CSRegs) ||
2058 isAnySubRegLive(CurReg, TRI, MI)) {
2059 // VFP pops don't allow holes in the register list, so any skip is fatal
2060 // for our transformation. GPR pops do, so we should just keep looking.
2067 // Mark the unimportant registers as <def,dead> in the POP.
2068 RegList.push_back(MachineOperand::CreateReg(CurReg, true, false, false,
2076 // Finally we know we can profitably perform the optimisation so go
2077 // ahead: strip all existing registers off and add them back again
2078 // in the right order.
2079 for (int i = MI->getNumOperands() - 1; i >= RegListIdx; --i)
2080 MI->RemoveOperand(i);
2082 // Add the complete list back in.
2083 MachineInstrBuilder MIB(MF, &*MI);
2084 for (int i = RegList.size() - 1; i >= 0; --i)
2085 MIB.addOperand(RegList[i]);
2090 bool llvm::rewriteARMFrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
2091 unsigned FrameReg, int &Offset,
2092 const ARMBaseInstrInfo &TII) {
2093 unsigned Opcode = MI.getOpcode();
2094 const MCInstrDesc &Desc = MI.getDesc();
2095 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
2098 // Memory operands in inline assembly always use AddrMode2.
2099 if (Opcode == ARM::INLINEASM)
2100 AddrMode = ARMII::AddrMode2;
2102 if (Opcode == ARM::ADDri) {
2103 Offset += MI.getOperand(FrameRegIdx+1).getImm();
2105 // Turn it into a move.
2106 MI.setDesc(TII.get(ARM::MOVr));
2107 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
2108 MI.RemoveOperand(FrameRegIdx+1);
2111 } else if (Offset < 0) {
2114 MI.setDesc(TII.get(ARM::SUBri));
2117 // Common case: small offset, fits into instruction.
2118 if (ARM_AM::getSOImmVal(Offset) != -1) {
2119 // Replace the FrameIndex with sp / fp
2120 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
2121 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(Offset);
2126 // Otherwise, pull as much of the immedidate into this ADDri/SUBri
2128 unsigned RotAmt = ARM_AM::getSOImmValRotate(Offset);
2129 unsigned ThisImmVal = Offset & ARM_AM::rotr32(0xFF, RotAmt);
2131 // We will handle these bits from offset, clear them.
2132 Offset &= ~ThisImmVal;
2134 // Get the properly encoded SOImmVal field.
2135 assert(ARM_AM::getSOImmVal(ThisImmVal) != -1 &&
2136 "Bit extraction didn't work?");
2137 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(ThisImmVal);
2139 unsigned ImmIdx = 0;
2141 unsigned NumBits = 0;
2144 case ARMII::AddrMode_i12: {
2145 ImmIdx = FrameRegIdx + 1;
2146 InstrOffs = MI.getOperand(ImmIdx).getImm();
2150 case ARMII::AddrMode2: {
2151 ImmIdx = FrameRegIdx+2;
2152 InstrOffs = ARM_AM::getAM2Offset(MI.getOperand(ImmIdx).getImm());
2153 if (ARM_AM::getAM2Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
2158 case ARMII::AddrMode3: {
2159 ImmIdx = FrameRegIdx+2;
2160 InstrOffs = ARM_AM::getAM3Offset(MI.getOperand(ImmIdx).getImm());
2161 if (ARM_AM::getAM3Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
2166 case ARMII::AddrMode4:
2167 case ARMII::AddrMode6:
2168 // Can't fold any offset even if it's zero.
2170 case ARMII::AddrMode5: {
2171 ImmIdx = FrameRegIdx+1;
2172 InstrOffs = ARM_AM::getAM5Offset(MI.getOperand(ImmIdx).getImm());
2173 if (ARM_AM::getAM5Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
2180 llvm_unreachable("Unsupported addressing mode!");
2183 Offset += InstrOffs * Scale;
2184 assert((Offset & (Scale-1)) == 0 && "Can't encode this offset!");
2190 // Attempt to fold address comp. if opcode has offset bits
2192 // Common case: small offset, fits into instruction.
2193 MachineOperand &ImmOp = MI.getOperand(ImmIdx);
2194 int ImmedOffset = Offset / Scale;
2195 unsigned Mask = (1 << NumBits) - 1;
2196 if ((unsigned)Offset <= Mask * Scale) {
2197 // Replace the FrameIndex with sp
2198 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
2199 // FIXME: When addrmode2 goes away, this will simplify (like the
2200 // T2 version), as the LDR.i12 versions don't need the encoding
2201 // tricks for the offset value.
2203 if (AddrMode == ARMII::AddrMode_i12)
2204 ImmedOffset = -ImmedOffset;
2206 ImmedOffset |= 1 << NumBits;
2208 ImmOp.ChangeToImmediate(ImmedOffset);
2213 // Otherwise, it didn't fit. Pull in what we can to simplify the immed.
2214 ImmedOffset = ImmedOffset & Mask;
2216 if (AddrMode == ARMII::AddrMode_i12)
2217 ImmedOffset = -ImmedOffset;
2219 ImmedOffset |= 1 << NumBits;
2221 ImmOp.ChangeToImmediate(ImmedOffset);
2222 Offset &= ~(Mask*Scale);
2226 Offset = (isSub) ? -Offset : Offset;
2230 /// analyzeCompare - For a comparison instruction, return the source registers
2231 /// in SrcReg and SrcReg2 if having two register operands, and the value it
2232 /// compares against in CmpValue. Return true if the comparison instruction
2233 /// can be analyzed.
2234 bool ARMBaseInstrInfo::
2235 analyzeCompare(const MachineInstr *MI, unsigned &SrcReg, unsigned &SrcReg2,
2236 int &CmpMask, int &CmpValue) const {
2237 switch (MI->getOpcode()) {
2241 SrcReg = MI->getOperand(0).getReg();
2244 CmpValue = MI->getOperand(1).getImm();
2248 SrcReg = MI->getOperand(0).getReg();
2249 SrcReg2 = MI->getOperand(1).getReg();
2255 SrcReg = MI->getOperand(0).getReg();
2257 CmpMask = MI->getOperand(1).getImm();
2265 /// isSuitableForMask - Identify a suitable 'and' instruction that
2266 /// operates on the given source register and applies the same mask
2267 /// as a 'tst' instruction. Provide a limited look-through for copies.
2268 /// When successful, MI will hold the found instruction.
2269 static bool isSuitableForMask(MachineInstr *&MI, unsigned SrcReg,
2270 int CmpMask, bool CommonUse) {
2271 switch (MI->getOpcode()) {
2274 if (CmpMask != MI->getOperand(2).getImm())
2276 if (SrcReg == MI->getOperand(CommonUse ? 1 : 0).getReg())
2284 /// getSwappedCondition - assume the flags are set by MI(a,b), return
2285 /// the condition code if we modify the instructions such that flags are
2287 inline static ARMCC::CondCodes getSwappedCondition(ARMCC::CondCodes CC) {
2289 default: return ARMCC::AL;
2290 case ARMCC::EQ: return ARMCC::EQ;
2291 case ARMCC::NE: return ARMCC::NE;
2292 case ARMCC::HS: return ARMCC::LS;
2293 case ARMCC::LO: return ARMCC::HI;
2294 case ARMCC::HI: return ARMCC::LO;
2295 case ARMCC::LS: return ARMCC::HS;
2296 case ARMCC::GE: return ARMCC::LE;
2297 case ARMCC::LT: return ARMCC::GT;
2298 case ARMCC::GT: return ARMCC::LT;
2299 case ARMCC::LE: return ARMCC::GE;
2303 /// isRedundantFlagInstr - check whether the first instruction, whose only
2304 /// purpose is to update flags, can be made redundant.
2305 /// CMPrr can be made redundant by SUBrr if the operands are the same.
2306 /// CMPri can be made redundant by SUBri if the operands are the same.
2307 /// This function can be extended later on.
2308 inline static bool isRedundantFlagInstr(MachineInstr *CmpI, unsigned SrcReg,
2309 unsigned SrcReg2, int ImmValue,
2311 if ((CmpI->getOpcode() == ARM::CMPrr ||
2312 CmpI->getOpcode() == ARM::t2CMPrr) &&
2313 (OI->getOpcode() == ARM::SUBrr ||
2314 OI->getOpcode() == ARM::t2SUBrr) &&
2315 ((OI->getOperand(1).getReg() == SrcReg &&
2316 OI->getOperand(2).getReg() == SrcReg2) ||
2317 (OI->getOperand(1).getReg() == SrcReg2 &&
2318 OI->getOperand(2).getReg() == SrcReg)))
2321 if ((CmpI->getOpcode() == ARM::CMPri ||
2322 CmpI->getOpcode() == ARM::t2CMPri) &&
2323 (OI->getOpcode() == ARM::SUBri ||
2324 OI->getOpcode() == ARM::t2SUBri) &&
2325 OI->getOperand(1).getReg() == SrcReg &&
2326 OI->getOperand(2).getImm() == ImmValue)
2331 /// optimizeCompareInstr - Convert the instruction supplying the argument to the
2332 /// comparison into one that sets the zero bit in the flags register;
2333 /// Remove a redundant Compare instruction if an earlier instruction can set the
2334 /// flags in the same way as Compare.
2335 /// E.g. SUBrr(r1,r2) and CMPrr(r1,r2). We also handle the case where two
2336 /// operands are swapped: SUBrr(r1,r2) and CMPrr(r2,r1), by updating the
2337 /// condition code of instructions which use the flags.
2338 bool ARMBaseInstrInfo::
2339 optimizeCompareInstr(MachineInstr *CmpInstr, unsigned SrcReg, unsigned SrcReg2,
2340 int CmpMask, int CmpValue,
2341 const MachineRegisterInfo *MRI) const {
2342 // Get the unique definition of SrcReg.
2343 MachineInstr *MI = MRI->getUniqueVRegDef(SrcReg);
2344 if (!MI) return false;
2346 // Masked compares sometimes use the same register as the corresponding 'and'.
2347 if (CmpMask != ~0) {
2348 if (!isSuitableForMask(MI, SrcReg, CmpMask, false) || isPredicated(MI)) {
2350 for (MachineRegisterInfo::use_instr_iterator
2351 UI = MRI->use_instr_begin(SrcReg), UE = MRI->use_instr_end();
2353 if (UI->getParent() != CmpInstr->getParent()) continue;
2354 MachineInstr *PotentialAND = &*UI;
2355 if (!isSuitableForMask(PotentialAND, SrcReg, CmpMask, true) ||
2356 isPredicated(PotentialAND))
2361 if (!MI) return false;
2365 // Get ready to iterate backward from CmpInstr.
2366 MachineBasicBlock::iterator I = CmpInstr, E = MI,
2367 B = CmpInstr->getParent()->begin();
2369 // Early exit if CmpInstr is at the beginning of the BB.
2370 if (I == B) return false;
2372 // There are two possible candidates which can be changed to set CPSR:
2373 // One is MI, the other is a SUB instruction.
2374 // For CMPrr(r1,r2), we are looking for SUB(r1,r2) or SUB(r2,r1).
2375 // For CMPri(r1, CmpValue), we are looking for SUBri(r1, CmpValue).
2376 MachineInstr *Sub = nullptr;
2378 // MI is not a candidate for CMPrr.
2380 else if (MI->getParent() != CmpInstr->getParent() || CmpValue != 0) {
2381 // Conservatively refuse to convert an instruction which isn't in the same
2382 // BB as the comparison.
2383 // For CMPri w/ CmpValue != 0, a Sub may still be a candidate.
2384 // Thus we cannot return here.
2385 if (CmpInstr->getOpcode() == ARM::CMPri ||
2386 CmpInstr->getOpcode() == ARM::t2CMPri)
2392 // Check that CPSR isn't set between the comparison instruction and the one we
2393 // want to change. At the same time, search for Sub.
2394 const TargetRegisterInfo *TRI = &getRegisterInfo();
2396 for (; I != E; --I) {
2397 const MachineInstr &Instr = *I;
2399 if (Instr.modifiesRegister(ARM::CPSR, TRI) ||
2400 Instr.readsRegister(ARM::CPSR, TRI))
2401 // This instruction modifies or uses CPSR after the one we want to
2402 // change. We can't do this transformation.
2405 // Check whether CmpInstr can be made redundant by the current instruction.
2406 if (isRedundantFlagInstr(CmpInstr, SrcReg, SrcReg2, CmpValue, &*I)) {
2412 // The 'and' is below the comparison instruction.
2416 // Return false if no candidates exist.
2420 // The single candidate is called MI.
2423 // We can't use a predicated instruction - it doesn't always write the flags.
2424 if (isPredicated(MI))
2427 switch (MI->getOpcode()) {
2461 case ARM::t2EORri: {
2462 // Scan forward for the use of CPSR
2463 // When checking against MI: if it's a conditional code that requires
2464 // checking of the V bit or C bit, then this is not safe to do.
2465 // It is safe to remove CmpInstr if CPSR is redefined or killed.
2466 // If we are done with the basic block, we need to check whether CPSR is
2468 SmallVector<std::pair<MachineOperand*, ARMCC::CondCodes>, 4>
2470 bool isSafe = false;
2472 E = CmpInstr->getParent()->end();
2473 while (!isSafe && ++I != E) {
2474 const MachineInstr &Instr = *I;
2475 for (unsigned IO = 0, EO = Instr.getNumOperands();
2476 !isSafe && IO != EO; ++IO) {
2477 const MachineOperand &MO = Instr.getOperand(IO);
2478 if (MO.isRegMask() && MO.clobbersPhysReg(ARM::CPSR)) {
2482 if (!MO.isReg() || MO.getReg() != ARM::CPSR)
2488 // Condition code is after the operand before CPSR except for VSELs.
2489 ARMCC::CondCodes CC;
2490 bool IsInstrVSel = true;
2491 switch (Instr.getOpcode()) {
2493 IsInstrVSel = false;
2494 CC = (ARMCC::CondCodes)Instr.getOperand(IO - 1).getImm();
2515 ARMCC::CondCodes NewCC = getSwappedCondition(CC);
2516 if (NewCC == ARMCC::AL)
2518 // If we have SUB(r1, r2) and CMP(r2, r1), the condition code based
2519 // on CMP needs to be updated to be based on SUB.
2520 // Push the condition code operands to OperandsToUpdate.
2521 // If it is safe to remove CmpInstr, the condition code of these
2522 // operands will be modified.
2523 if (SrcReg2 != 0 && Sub->getOperand(1).getReg() == SrcReg2 &&
2524 Sub->getOperand(2).getReg() == SrcReg) {
2525 // VSel doesn't support condition code update.
2528 OperandsToUpdate.push_back(
2529 std::make_pair(&((*I).getOperand(IO - 1)), NewCC));
2532 // No Sub, so this is x = <op> y, z; cmp x, 0.
2534 case ARMCC::EQ: // Z
2535 case ARMCC::NE: // Z
2536 case ARMCC::MI: // N
2537 case ARMCC::PL: // N
2538 case ARMCC::AL: // none
2539 // CPSR can be used multiple times, we should continue.
2541 case ARMCC::HS: // C
2542 case ARMCC::LO: // C
2543 case ARMCC::VS: // V
2544 case ARMCC::VC: // V
2545 case ARMCC::HI: // C Z
2546 case ARMCC::LS: // C Z
2547 case ARMCC::GE: // N V
2548 case ARMCC::LT: // N V
2549 case ARMCC::GT: // Z N V
2550 case ARMCC::LE: // Z N V
2551 // The instruction uses the V bit or C bit which is not safe.
2558 // If CPSR is not killed nor re-defined, we should check whether it is
2559 // live-out. If it is live-out, do not optimize.
2561 MachineBasicBlock *MBB = CmpInstr->getParent();
2562 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
2563 SE = MBB->succ_end(); SI != SE; ++SI)
2564 if ((*SI)->isLiveIn(ARM::CPSR))
2568 // Toggle the optional operand to CPSR.
2569 MI->getOperand(5).setReg(ARM::CPSR);
2570 MI->getOperand(5).setIsDef(true);
2571 assert(!isPredicated(MI) && "Can't use flags from predicated instruction");
2572 CmpInstr->eraseFromParent();
2574 // Modify the condition code of operands in OperandsToUpdate.
2575 // Since we have SUB(r1, r2) and CMP(r2, r1), the condition code needs to
2576 // be changed from r2 > r1 to r1 < r2, from r2 < r1 to r1 > r2, etc.
2577 for (unsigned i = 0, e = OperandsToUpdate.size(); i < e; i++)
2578 OperandsToUpdate[i].first->setImm(OperandsToUpdate[i].second);
2586 bool ARMBaseInstrInfo::FoldImmediate(MachineInstr *UseMI,
2587 MachineInstr *DefMI, unsigned Reg,
2588 MachineRegisterInfo *MRI) const {
2589 // Fold large immediates into add, sub, or, xor.
2590 unsigned DefOpc = DefMI->getOpcode();
2591 if (DefOpc != ARM::t2MOVi32imm && DefOpc != ARM::MOVi32imm)
2593 if (!DefMI->getOperand(1).isImm())
2594 // Could be t2MOVi32imm <ga:xx>
2597 if (!MRI->hasOneNonDBGUse(Reg))
2600 const MCInstrDesc &DefMCID = DefMI->getDesc();
2601 if (DefMCID.hasOptionalDef()) {
2602 unsigned NumOps = DefMCID.getNumOperands();
2603 const MachineOperand &MO = DefMI->getOperand(NumOps-1);
2604 if (MO.getReg() == ARM::CPSR && !MO.isDead())
2605 // If DefMI defines CPSR and it is not dead, it's obviously not safe
2610 const MCInstrDesc &UseMCID = UseMI->getDesc();
2611 if (UseMCID.hasOptionalDef()) {
2612 unsigned NumOps = UseMCID.getNumOperands();
2613 if (UseMI->getOperand(NumOps-1).getReg() == ARM::CPSR)
2614 // If the instruction sets the flag, do not attempt this optimization
2615 // since it may change the semantics of the code.
2619 unsigned UseOpc = UseMI->getOpcode();
2620 unsigned NewUseOpc = 0;
2621 uint32_t ImmVal = (uint32_t)DefMI->getOperand(1).getImm();
2622 uint32_t SOImmValV1 = 0, SOImmValV2 = 0;
2623 bool Commute = false;
2625 default: return false;
2633 case ARM::t2EORrr: {
2634 Commute = UseMI->getOperand(2).getReg() != Reg;
2641 NewUseOpc = ARM::SUBri;
2647 if (!ARM_AM::isSOImmTwoPartVal(ImmVal))
2649 SOImmValV1 = (uint32_t)ARM_AM::getSOImmTwoPartFirst(ImmVal);
2650 SOImmValV2 = (uint32_t)ARM_AM::getSOImmTwoPartSecond(ImmVal);
2653 case ARM::ADDrr: NewUseOpc = ARM::ADDri; break;
2654 case ARM::ORRrr: NewUseOpc = ARM::ORRri; break;
2655 case ARM::EORrr: NewUseOpc = ARM::EORri; break;
2659 case ARM::t2SUBrr: {
2663 NewUseOpc = ARM::t2SUBri;
2668 case ARM::t2EORrr: {
2669 if (!ARM_AM::isT2SOImmTwoPartVal(ImmVal))
2671 SOImmValV1 = (uint32_t)ARM_AM::getT2SOImmTwoPartFirst(ImmVal);
2672 SOImmValV2 = (uint32_t)ARM_AM::getT2SOImmTwoPartSecond(ImmVal);
2675 case ARM::t2ADDrr: NewUseOpc = ARM::t2ADDri; break;
2676 case ARM::t2ORRrr: NewUseOpc = ARM::t2ORRri; break;
2677 case ARM::t2EORrr: NewUseOpc = ARM::t2EORri; break;
2685 unsigned OpIdx = Commute ? 2 : 1;
2686 unsigned Reg1 = UseMI->getOperand(OpIdx).getReg();
2687 bool isKill = UseMI->getOperand(OpIdx).isKill();
2688 unsigned NewReg = MRI->createVirtualRegister(MRI->getRegClass(Reg));
2689 AddDefaultCC(AddDefaultPred(BuildMI(*UseMI->getParent(),
2690 UseMI, UseMI->getDebugLoc(),
2691 get(NewUseOpc), NewReg)
2692 .addReg(Reg1, getKillRegState(isKill))
2693 .addImm(SOImmValV1)));
2694 UseMI->setDesc(get(NewUseOpc));
2695 UseMI->getOperand(1).setReg(NewReg);
2696 UseMI->getOperand(1).setIsKill();
2697 UseMI->getOperand(2).ChangeToImmediate(SOImmValV2);
2698 DefMI->eraseFromParent();
2702 static unsigned getNumMicroOpsSwiftLdSt(const InstrItineraryData *ItinData,
2703 const MachineInstr *MI) {
2704 switch (MI->getOpcode()) {
2706 const MCInstrDesc &Desc = MI->getDesc();
2707 int UOps = ItinData->getNumMicroOps(Desc.getSchedClass());
2708 assert(UOps >= 0 && "bad # UOps");
2716 unsigned ShOpVal = MI->getOperand(3).getImm();
2717 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2718 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2721 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2722 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2729 if (!MI->getOperand(2).getReg())
2732 unsigned ShOpVal = MI->getOperand(3).getImm();
2733 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2734 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2737 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2738 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2745 return (ARM_AM::getAM3Op(MI->getOperand(3).getImm()) == ARM_AM::sub) ? 3:2;
2747 case ARM::LDRSB_POST:
2748 case ARM::LDRSH_POST: {
2749 unsigned Rt = MI->getOperand(0).getReg();
2750 unsigned Rm = MI->getOperand(3).getReg();
2751 return (Rt == Rm) ? 4 : 3;
2754 case ARM::LDR_PRE_REG:
2755 case ARM::LDRB_PRE_REG: {
2756 unsigned Rt = MI->getOperand(0).getReg();
2757 unsigned Rm = MI->getOperand(3).getReg();
2760 unsigned ShOpVal = MI->getOperand(4).getImm();
2761 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2762 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2765 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2766 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2771 case ARM::STR_PRE_REG:
2772 case ARM::STRB_PRE_REG: {
2773 unsigned ShOpVal = MI->getOperand(4).getImm();
2774 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2775 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2778 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2779 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2785 case ARM::STRH_PRE: {
2786 unsigned Rt = MI->getOperand(0).getReg();
2787 unsigned Rm = MI->getOperand(3).getReg();
2792 return (ARM_AM::getAM3Op(MI->getOperand(4).getImm()) == ARM_AM::sub)
2796 case ARM::LDR_POST_REG:
2797 case ARM::LDRB_POST_REG:
2798 case ARM::LDRH_POST: {
2799 unsigned Rt = MI->getOperand(0).getReg();
2800 unsigned Rm = MI->getOperand(3).getReg();
2801 return (Rt == Rm) ? 3 : 2;
2804 case ARM::LDR_PRE_IMM:
2805 case ARM::LDRB_PRE_IMM:
2806 case ARM::LDR_POST_IMM:
2807 case ARM::LDRB_POST_IMM:
2808 case ARM::STRB_POST_IMM:
2809 case ARM::STRB_POST_REG:
2810 case ARM::STRB_PRE_IMM:
2811 case ARM::STRH_POST:
2812 case ARM::STR_POST_IMM:
2813 case ARM::STR_POST_REG:
2814 case ARM::STR_PRE_IMM:
2817 case ARM::LDRSB_PRE:
2818 case ARM::LDRSH_PRE: {
2819 unsigned Rm = MI->getOperand(3).getReg();
2822 unsigned Rt = MI->getOperand(0).getReg();
2825 unsigned ShOpVal = MI->getOperand(4).getImm();
2826 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2827 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2830 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2831 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2837 unsigned Rt = MI->getOperand(0).getReg();
2838 unsigned Rn = MI->getOperand(2).getReg();
2839 unsigned Rm = MI->getOperand(3).getReg();
2841 return (ARM_AM::getAM3Op(MI->getOperand(4).getImm()) == ARM_AM::sub) ?4:3;
2842 return (Rt == Rn) ? 3 : 2;
2846 unsigned Rm = MI->getOperand(3).getReg();
2848 return (ARM_AM::getAM3Op(MI->getOperand(4).getImm()) == ARM_AM::sub) ?4:3;
2852 case ARM::LDRD_POST:
2853 case ARM::t2LDRD_POST:
2856 case ARM::STRD_POST:
2857 case ARM::t2STRD_POST:
2860 case ARM::LDRD_PRE: {
2861 unsigned Rt = MI->getOperand(0).getReg();
2862 unsigned Rn = MI->getOperand(3).getReg();
2863 unsigned Rm = MI->getOperand(4).getReg();
2865 return (ARM_AM::getAM3Op(MI->getOperand(5).getImm()) == ARM_AM::sub) ?5:4;
2866 return (Rt == Rn) ? 4 : 3;
2869 case ARM::t2LDRD_PRE: {
2870 unsigned Rt = MI->getOperand(0).getReg();
2871 unsigned Rn = MI->getOperand(3).getReg();
2872 return (Rt == Rn) ? 4 : 3;
2875 case ARM::STRD_PRE: {
2876 unsigned Rm = MI->getOperand(4).getReg();
2878 return (ARM_AM::getAM3Op(MI->getOperand(5).getImm()) == ARM_AM::sub) ?5:4;
2882 case ARM::t2STRD_PRE:
2885 case ARM::t2LDR_POST:
2886 case ARM::t2LDRB_POST:
2887 case ARM::t2LDRB_PRE:
2888 case ARM::t2LDRSBi12:
2889 case ARM::t2LDRSBi8:
2890 case ARM::t2LDRSBpci:
2892 case ARM::t2LDRH_POST:
2893 case ARM::t2LDRH_PRE:
2895 case ARM::t2LDRSB_POST:
2896 case ARM::t2LDRSB_PRE:
2897 case ARM::t2LDRSH_POST:
2898 case ARM::t2LDRSH_PRE:
2899 case ARM::t2LDRSHi12:
2900 case ARM::t2LDRSHi8:
2901 case ARM::t2LDRSHpci:
2905 case ARM::t2LDRDi8: {
2906 unsigned Rt = MI->getOperand(0).getReg();
2907 unsigned Rn = MI->getOperand(2).getReg();
2908 return (Rt == Rn) ? 3 : 2;
2911 case ARM::t2STRB_POST:
2912 case ARM::t2STRB_PRE:
2915 case ARM::t2STRH_POST:
2916 case ARM::t2STRH_PRE:
2918 case ARM::t2STR_POST:
2919 case ARM::t2STR_PRE:
2925 // Return the number of 32-bit words loaded by LDM or stored by STM. If this
2926 // can't be easily determined return 0 (missing MachineMemOperand).
2928 // FIXME: The current MachineInstr design does not support relying on machine
2929 // mem operands to determine the width of a memory access. Instead, we expect
2930 // the target to provide this information based on the instruction opcode and
2931 // operands. However, using MachineMemOperand is the best solution now for
2934 // 1) getNumMicroOps tries to infer LDM memory width from the total number of MI
2935 // operands. This is much more dangerous than using the MachineMemOperand
2936 // sizes because CodeGen passes can insert/remove optional machine operands. In
2937 // fact, it's totally incorrect for preRA passes and appears to be wrong for
2938 // postRA passes as well.
2940 // 2) getNumLDMAddresses is only used by the scheduling machine model and any
2941 // machine model that calls this should handle the unknown (zero size) case.
2943 // Long term, we should require a target hook that verifies MachineMemOperand
2944 // sizes during MC lowering. That target hook should be local to MC lowering
2945 // because we can't ensure that it is aware of other MI forms. Doing this will
2946 // ensure that MachineMemOperands are correctly propagated through all passes.
2947 unsigned ARMBaseInstrInfo::getNumLDMAddresses(const MachineInstr *MI) const {
2949 for (MachineInstr::mmo_iterator I = MI->memoperands_begin(),
2950 E = MI->memoperands_end(); I != E; ++I) {
2951 Size += (*I)->getSize();
2957 ARMBaseInstrInfo::getNumMicroOps(const InstrItineraryData *ItinData,
2958 const MachineInstr *MI) const {
2959 if (!ItinData || ItinData->isEmpty())
2962 const MCInstrDesc &Desc = MI->getDesc();
2963 unsigned Class = Desc.getSchedClass();
2964 int ItinUOps = ItinData->getNumMicroOps(Class);
2965 if (ItinUOps >= 0) {
2966 if (Subtarget.isSwift() && (Desc.mayLoad() || Desc.mayStore()))
2967 return getNumMicroOpsSwiftLdSt(ItinData, MI);
2972 unsigned Opc = MI->getOpcode();
2975 llvm_unreachable("Unexpected multi-uops instruction!");
2980 // The number of uOps for load / store multiple are determined by the number
2983 // On Cortex-A8, each pair of register loads / stores can be scheduled on the
2984 // same cycle. The scheduling for the first load / store must be done
2985 // separately by assuming the address is not 64-bit aligned.
2987 // On Cortex-A9, the formula is simply (#reg / 2) + (#reg % 2). If the address
2988 // is not 64-bit aligned, then AGU would take an extra cycle. For VFP / NEON
2989 // load / store multiple, the formula is (#reg / 2) + (#reg % 2) + 1.
2991 case ARM::VLDMDIA_UPD:
2992 case ARM::VLDMDDB_UPD:
2994 case ARM::VLDMSIA_UPD:
2995 case ARM::VLDMSDB_UPD:
2997 case ARM::VSTMDIA_UPD:
2998 case ARM::VSTMDDB_UPD:
3000 case ARM::VSTMSIA_UPD:
3001 case ARM::VSTMSDB_UPD: {
3002 unsigned NumRegs = MI->getNumOperands() - Desc.getNumOperands();
3003 return (NumRegs / 2) + (NumRegs % 2) + 1;
3006 case ARM::LDMIA_RET:
3011 case ARM::LDMIA_UPD:
3012 case ARM::LDMDA_UPD:
3013 case ARM::LDMDB_UPD:
3014 case ARM::LDMIB_UPD:
3019 case ARM::STMIA_UPD:
3020 case ARM::STMDA_UPD:
3021 case ARM::STMDB_UPD:
3022 case ARM::STMIB_UPD:
3024 case ARM::tLDMIA_UPD:
3025 case ARM::tSTMIA_UPD:
3029 case ARM::t2LDMIA_RET:
3032 case ARM::t2LDMIA_UPD:
3033 case ARM::t2LDMDB_UPD:
3036 case ARM::t2STMIA_UPD:
3037 case ARM::t2STMDB_UPD: {
3038 unsigned NumRegs = MI->getNumOperands() - Desc.getNumOperands() + 1;
3039 if (Subtarget.isSwift()) {
3040 int UOps = 1 + NumRegs; // One for address computation, one for each ld / st.
3043 case ARM::VLDMDIA_UPD:
3044 case ARM::VLDMDDB_UPD:
3045 case ARM::VLDMSIA_UPD:
3046 case ARM::VLDMSDB_UPD:
3047 case ARM::VSTMDIA_UPD:
3048 case ARM::VSTMDDB_UPD:
3049 case ARM::VSTMSIA_UPD:
3050 case ARM::VSTMSDB_UPD:
3051 case ARM::LDMIA_UPD:
3052 case ARM::LDMDA_UPD:
3053 case ARM::LDMDB_UPD:
3054 case ARM::LDMIB_UPD:
3055 case ARM::STMIA_UPD:
3056 case ARM::STMDA_UPD:
3057 case ARM::STMDB_UPD:
3058 case ARM::STMIB_UPD:
3059 case ARM::tLDMIA_UPD:
3060 case ARM::tSTMIA_UPD:
3061 case ARM::t2LDMIA_UPD:
3062 case ARM::t2LDMDB_UPD:
3063 case ARM::t2STMIA_UPD:
3064 case ARM::t2STMDB_UPD:
3065 ++UOps; // One for base register writeback.
3067 case ARM::LDMIA_RET:
3069 case ARM::t2LDMIA_RET:
3070 UOps += 2; // One for base reg wb, one for write to pc.
3074 } else if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
3077 // 4 registers would be issued: 2, 2.
3078 // 5 registers would be issued: 2, 2, 1.
3079 int A8UOps = (NumRegs / 2);
3083 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
3084 int A9UOps = (NumRegs / 2);
3085 // If there are odd number of registers or if it's not 64-bit aligned,
3086 // then it takes an extra AGU (Address Generation Unit) cycle.
3087 if ((NumRegs % 2) ||
3088 !MI->hasOneMemOperand() ||
3089 (*MI->memoperands_begin())->getAlignment() < 8)
3093 // Assume the worst.
3101 ARMBaseInstrInfo::getVLDMDefCycle(const InstrItineraryData *ItinData,
3102 const MCInstrDesc &DefMCID,
3104 unsigned DefIdx, unsigned DefAlign) const {
3105 int RegNo = (int)(DefIdx+1) - DefMCID.getNumOperands() + 1;
3107 // Def is the address writeback.
3108 return ItinData->getOperandCycle(DefClass, DefIdx);
3111 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
3112 // (regno / 2) + (regno % 2) + 1
3113 DefCycle = RegNo / 2 + 1;
3116 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
3118 bool isSLoad = false;
3120 switch (DefMCID.getOpcode()) {
3123 case ARM::VLDMSIA_UPD:
3124 case ARM::VLDMSDB_UPD:
3129 // If there are odd number of 'S' registers or if it's not 64-bit aligned,
3130 // then it takes an extra cycle.
3131 if ((isSLoad && (RegNo % 2)) || DefAlign < 8)
3134 // Assume the worst.
3135 DefCycle = RegNo + 2;
3142 ARMBaseInstrInfo::getLDMDefCycle(const InstrItineraryData *ItinData,
3143 const MCInstrDesc &DefMCID,
3145 unsigned DefIdx, unsigned DefAlign) const {
3146 int RegNo = (int)(DefIdx+1) - DefMCID.getNumOperands() + 1;
3148 // Def is the address writeback.
3149 return ItinData->getOperandCycle(DefClass, DefIdx);
3152 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
3153 // 4 registers would be issued: 1, 2, 1.
3154 // 5 registers would be issued: 1, 2, 2.
3155 DefCycle = RegNo / 2;
3158 // Result latency is issue cycle + 2: E2.
3160 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
3161 DefCycle = (RegNo / 2);
3162 // If there are odd number of registers or if it's not 64-bit aligned,
3163 // then it takes an extra AGU (Address Generation Unit) cycle.
3164 if ((RegNo % 2) || DefAlign < 8)
3166 // Result latency is AGU cycles + 2.
3169 // Assume the worst.
3170 DefCycle = RegNo + 2;
3177 ARMBaseInstrInfo::getVSTMUseCycle(const InstrItineraryData *ItinData,
3178 const MCInstrDesc &UseMCID,
3180 unsigned UseIdx, unsigned UseAlign) const {
3181 int RegNo = (int)(UseIdx+1) - UseMCID.getNumOperands() + 1;
3183 return ItinData->getOperandCycle(UseClass, UseIdx);
3186 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
3187 // (regno / 2) + (regno % 2) + 1
3188 UseCycle = RegNo / 2 + 1;
3191 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
3193 bool isSStore = false;
3195 switch (UseMCID.getOpcode()) {
3198 case ARM::VSTMSIA_UPD:
3199 case ARM::VSTMSDB_UPD:
3204 // If there are odd number of 'S' registers or if it's not 64-bit aligned,
3205 // then it takes an extra cycle.
3206 if ((isSStore && (RegNo % 2)) || UseAlign < 8)
3209 // Assume the worst.
3210 UseCycle = RegNo + 2;
3217 ARMBaseInstrInfo::getSTMUseCycle(const InstrItineraryData *ItinData,
3218 const MCInstrDesc &UseMCID,
3220 unsigned UseIdx, unsigned UseAlign) const {
3221 int RegNo = (int)(UseIdx+1) - UseMCID.getNumOperands() + 1;
3223 return ItinData->getOperandCycle(UseClass, UseIdx);
3226 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
3227 UseCycle = RegNo / 2;
3232 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
3233 UseCycle = (RegNo / 2);
3234 // If there are odd number of registers or if it's not 64-bit aligned,
3235 // then it takes an extra AGU (Address Generation Unit) cycle.
3236 if ((RegNo % 2) || UseAlign < 8)
3239 // Assume the worst.
3246 ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
3247 const MCInstrDesc &DefMCID,
3248 unsigned DefIdx, unsigned DefAlign,
3249 const MCInstrDesc &UseMCID,
3250 unsigned UseIdx, unsigned UseAlign) const {
3251 unsigned DefClass = DefMCID.getSchedClass();
3252 unsigned UseClass = UseMCID.getSchedClass();
3254 if (DefIdx < DefMCID.getNumDefs() && UseIdx < UseMCID.getNumOperands())
3255 return ItinData->getOperandLatency(DefClass, DefIdx, UseClass, UseIdx);
3257 // This may be a def / use of a variable_ops instruction, the operand
3258 // latency might be determinable dynamically. Let the target try to
3261 bool LdmBypass = false;
3262 switch (DefMCID.getOpcode()) {
3264 DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
3268 case ARM::VLDMDIA_UPD:
3269 case ARM::VLDMDDB_UPD:
3271 case ARM::VLDMSIA_UPD:
3272 case ARM::VLDMSDB_UPD:
3273 DefCycle = getVLDMDefCycle(ItinData, DefMCID, DefClass, DefIdx, DefAlign);
3276 case ARM::LDMIA_RET:
3281 case ARM::LDMIA_UPD:
3282 case ARM::LDMDA_UPD:
3283 case ARM::LDMDB_UPD:
3284 case ARM::LDMIB_UPD:
3286 case ARM::tLDMIA_UPD:
3288 case ARM::t2LDMIA_RET:
3291 case ARM::t2LDMIA_UPD:
3292 case ARM::t2LDMDB_UPD:
3294 DefCycle = getLDMDefCycle(ItinData, DefMCID, DefClass, DefIdx, DefAlign);
3299 // We can't seem to determine the result latency of the def, assume it's 2.
3303 switch (UseMCID.getOpcode()) {
3305 UseCycle = ItinData->getOperandCycle(UseClass, UseIdx);
3309 case ARM::VSTMDIA_UPD:
3310 case ARM::VSTMDDB_UPD:
3312 case ARM::VSTMSIA_UPD:
3313 case ARM::VSTMSDB_UPD:
3314 UseCycle = getVSTMUseCycle(ItinData, UseMCID, UseClass, UseIdx, UseAlign);
3321 case ARM::STMIA_UPD:
3322 case ARM::STMDA_UPD:
3323 case ARM::STMDB_UPD:
3324 case ARM::STMIB_UPD:
3325 case ARM::tSTMIA_UPD:
3330 case ARM::t2STMIA_UPD:
3331 case ARM::t2STMDB_UPD:
3332 UseCycle = getSTMUseCycle(ItinData, UseMCID, UseClass, UseIdx, UseAlign);
3337 // Assume it's read in the first stage.
3340 UseCycle = DefCycle - UseCycle + 1;
3343 // It's a variable_ops instruction so we can't use DefIdx here. Just use
3344 // first def operand.
3345 if (ItinData->hasPipelineForwarding(DefClass, DefMCID.getNumOperands()-1,
3348 } else if (ItinData->hasPipelineForwarding(DefClass, DefIdx,
3349 UseClass, UseIdx)) {
3357 static const MachineInstr *getBundledDefMI(const TargetRegisterInfo *TRI,
3358 const MachineInstr *MI, unsigned Reg,
3359 unsigned &DefIdx, unsigned &Dist) {
3362 MachineBasicBlock::const_iterator I = MI; ++I;
3363 MachineBasicBlock::const_instr_iterator II = std::prev(I.getInstrIterator());
3364 assert(II->isInsideBundle() && "Empty bundle?");
3367 while (II->isInsideBundle()) {
3368 Idx = II->findRegisterDefOperandIdx(Reg, false, true, TRI);
3375 assert(Idx != -1 && "Cannot find bundled definition!");
3380 static const MachineInstr *getBundledUseMI(const TargetRegisterInfo *TRI,
3381 const MachineInstr *MI, unsigned Reg,
3382 unsigned &UseIdx, unsigned &Dist) {
3385 MachineBasicBlock::const_instr_iterator II = MI; ++II;
3386 assert(II->isInsideBundle() && "Empty bundle?");
3387 MachineBasicBlock::const_instr_iterator E = MI->getParent()->instr_end();
3389 // FIXME: This doesn't properly handle multiple uses.
3391 while (II != E && II->isInsideBundle()) {
3392 Idx = II->findRegisterUseOperandIdx(Reg, false, TRI);
3395 if (II->getOpcode() != ARM::t2IT)
3409 /// Return the number of cycles to add to (or subtract from) the static
3410 /// itinerary based on the def opcode and alignment. The caller will ensure that
3411 /// adjusted latency is at least one cycle.
3412 static int adjustDefLatency(const ARMSubtarget &Subtarget,
3413 const MachineInstr *DefMI,
3414 const MCInstrDesc *DefMCID, unsigned DefAlign) {
3416 if (Subtarget.isCortexA8() || Subtarget.isLikeA9() || Subtarget.isCortexA7()) {
3417 // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2]
3418 // variants are one cycle cheaper.
3419 switch (DefMCID->getOpcode()) {
3423 unsigned ShOpVal = DefMI->getOperand(3).getImm();
3424 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3426 (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
3433 case ARM::t2LDRSHs: {
3434 // Thumb2 mode: lsl only.
3435 unsigned ShAmt = DefMI->getOperand(3).getImm();
3436 if (ShAmt == 0 || ShAmt == 2)
3441 } else if (Subtarget.isSwift()) {
3442 // FIXME: Properly handle all of the latency adjustments for address
3444 switch (DefMCID->getOpcode()) {
3448 unsigned ShOpVal = DefMI->getOperand(3).getImm();
3449 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
3450 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3453 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
3454 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
3457 ShImm == 1 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsr)
3464 case ARM::t2LDRSHs: {
3465 // Thumb2 mode: lsl only.
3466 unsigned ShAmt = DefMI->getOperand(3).getImm();
3467 if (ShAmt == 0 || ShAmt == 1 || ShAmt == 2 || ShAmt == 3)
3474 if (DefAlign < 8 && Subtarget.isLikeA9()) {
3475 switch (DefMCID->getOpcode()) {
3481 case ARM::VLD1q8wb_fixed:
3482 case ARM::VLD1q16wb_fixed:
3483 case ARM::VLD1q32wb_fixed:
3484 case ARM::VLD1q64wb_fixed:
3485 case ARM::VLD1q8wb_register:
3486 case ARM::VLD1q16wb_register:
3487 case ARM::VLD1q32wb_register:
3488 case ARM::VLD1q64wb_register:
3495 case ARM::VLD2d8wb_fixed:
3496 case ARM::VLD2d16wb_fixed:
3497 case ARM::VLD2d32wb_fixed:
3498 case ARM::VLD2q8wb_fixed:
3499 case ARM::VLD2q16wb_fixed:
3500 case ARM::VLD2q32wb_fixed:
3501 case ARM::VLD2d8wb_register:
3502 case ARM::VLD2d16wb_register:
3503 case ARM::VLD2d32wb_register:
3504 case ARM::VLD2q8wb_register:
3505 case ARM::VLD2q16wb_register:
3506 case ARM::VLD2q32wb_register:
3511 case ARM::VLD3d8_UPD:
3512 case ARM::VLD3d16_UPD:
3513 case ARM::VLD3d32_UPD:
3514 case ARM::VLD1d64Twb_fixed:
3515 case ARM::VLD1d64Twb_register:
3516 case ARM::VLD3q8_UPD:
3517 case ARM::VLD3q16_UPD:
3518 case ARM::VLD3q32_UPD:
3523 case ARM::VLD4d8_UPD:
3524 case ARM::VLD4d16_UPD:
3525 case ARM::VLD4d32_UPD:
3526 case ARM::VLD1d64Qwb_fixed:
3527 case ARM::VLD1d64Qwb_register:
3528 case ARM::VLD4q8_UPD:
3529 case ARM::VLD4q16_UPD:
3530 case ARM::VLD4q32_UPD:
3531 case ARM::VLD1DUPq8:
3532 case ARM::VLD1DUPq16:
3533 case ARM::VLD1DUPq32:
3534 case ARM::VLD1DUPq8wb_fixed:
3535 case ARM::VLD1DUPq16wb_fixed:
3536 case ARM::VLD1DUPq32wb_fixed:
3537 case ARM::VLD1DUPq8wb_register:
3538 case ARM::VLD1DUPq16wb_register:
3539 case ARM::VLD1DUPq32wb_register:
3540 case ARM::VLD2DUPd8:
3541 case ARM::VLD2DUPd16:
3542 case ARM::VLD2DUPd32:
3543 case ARM::VLD2DUPd8wb_fixed:
3544 case ARM::VLD2DUPd16wb_fixed:
3545 case ARM::VLD2DUPd32wb_fixed:
3546 case ARM::VLD2DUPd8wb_register:
3547 case ARM::VLD2DUPd16wb_register:
3548 case ARM::VLD2DUPd32wb_register:
3549 case ARM::VLD4DUPd8:
3550 case ARM::VLD4DUPd16:
3551 case ARM::VLD4DUPd32:
3552 case ARM::VLD4DUPd8_UPD:
3553 case ARM::VLD4DUPd16_UPD:
3554 case ARM::VLD4DUPd32_UPD:
3556 case ARM::VLD1LNd16:
3557 case ARM::VLD1LNd32:
3558 case ARM::VLD1LNd8_UPD:
3559 case ARM::VLD1LNd16_UPD:
3560 case ARM::VLD1LNd32_UPD:
3562 case ARM::VLD2LNd16:
3563 case ARM::VLD2LNd32:
3564 case ARM::VLD2LNq16:
3565 case ARM::VLD2LNq32:
3566 case ARM::VLD2LNd8_UPD:
3567 case ARM::VLD2LNd16_UPD:
3568 case ARM::VLD2LNd32_UPD:
3569 case ARM::VLD2LNq16_UPD:
3570 case ARM::VLD2LNq32_UPD:
3572 case ARM::VLD4LNd16:
3573 case ARM::VLD4LNd32:
3574 case ARM::VLD4LNq16:
3575 case ARM::VLD4LNq32:
3576 case ARM::VLD4LNd8_UPD:
3577 case ARM::VLD4LNd16_UPD:
3578 case ARM::VLD4LNd32_UPD:
3579 case ARM::VLD4LNq16_UPD:
3580 case ARM::VLD4LNq32_UPD:
3581 // If the address is not 64-bit aligned, the latencies of these
3582 // instructions increases by one.
3593 ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
3594 const MachineInstr *DefMI, unsigned DefIdx,
3595 const MachineInstr *UseMI,
3596 unsigned UseIdx) const {
3597 // No operand latency. The caller may fall back to getInstrLatency.
3598 if (!ItinData || ItinData->isEmpty())
3601 const MachineOperand &DefMO = DefMI->getOperand(DefIdx);
3602 unsigned Reg = DefMO.getReg();
3603 const MCInstrDesc *DefMCID = &DefMI->getDesc();
3604 const MCInstrDesc *UseMCID = &UseMI->getDesc();
3606 unsigned DefAdj = 0;
3607 if (DefMI->isBundle()) {
3608 DefMI = getBundledDefMI(&getRegisterInfo(), DefMI, Reg, DefIdx, DefAdj);
3609 DefMCID = &DefMI->getDesc();
3611 if (DefMI->isCopyLike() || DefMI->isInsertSubreg() ||
3612 DefMI->isRegSequence() || DefMI->isImplicitDef()) {
3616 unsigned UseAdj = 0;
3617 if (UseMI->isBundle()) {
3619 const MachineInstr *NewUseMI = getBundledUseMI(&getRegisterInfo(), UseMI,
3620 Reg, NewUseIdx, UseAdj);
3626 UseMCID = &UseMI->getDesc();
3629 if (Reg == ARM::CPSR) {
3630 if (DefMI->getOpcode() == ARM::FMSTAT) {
3631 // fpscr -> cpsr stalls over 20 cycles on A8 (and earlier?)
3632 return Subtarget.isLikeA9() ? 1 : 20;
3635 // CPSR set and branch can be paired in the same cycle.
3636 if (UseMI->isBranch())
3639 // Otherwise it takes the instruction latency (generally one).
3640 unsigned Latency = getInstrLatency(ItinData, DefMI);
3642 // For Thumb2 and -Os, prefer scheduling CPSR setting instruction close to
3643 // its uses. Instructions which are otherwise scheduled between them may
3644 // incur a code size penalty (not able to use the CPSR setting 16-bit
3646 if (Latency > 0 && Subtarget.isThumb2()) {
3647 const MachineFunction *MF = DefMI->getParent()->getParent();
3648 // FIXME: Use Function::optForSize().
3649 if (MF->getFunction()->hasFnAttribute(Attribute::OptimizeForSize))
3655 if (DefMO.isImplicit() || UseMI->getOperand(UseIdx).isImplicit())
3658 unsigned DefAlign = DefMI->hasOneMemOperand()
3659 ? (*DefMI->memoperands_begin())->getAlignment() : 0;
3660 unsigned UseAlign = UseMI->hasOneMemOperand()
3661 ? (*UseMI->memoperands_begin())->getAlignment() : 0;
3663 // Get the itinerary's latency if possible, and handle variable_ops.
3664 int Latency = getOperandLatency(ItinData, *DefMCID, DefIdx, DefAlign,
3665 *UseMCID, UseIdx, UseAlign);
3666 // Unable to find operand latency. The caller may resort to getInstrLatency.
3670 // Adjust for IT block position.
3671 int Adj = DefAdj + UseAdj;
3673 // Adjust for dynamic def-side opcode variants not captured by the itinerary.
3674 Adj += adjustDefLatency(Subtarget, DefMI, DefMCID, DefAlign);
3675 if (Adj >= 0 || (int)Latency > -Adj) {
3676 return Latency + Adj;
3678 // Return the itinerary latency, which may be zero but not less than zero.
3683 ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
3684 SDNode *DefNode, unsigned DefIdx,
3685 SDNode *UseNode, unsigned UseIdx) const {
3686 if (!DefNode->isMachineOpcode())
3689 const MCInstrDesc &DefMCID = get(DefNode->getMachineOpcode());
3691 if (isZeroCost(DefMCID.Opcode))
3694 if (!ItinData || ItinData->isEmpty())
3695 return DefMCID.mayLoad() ? 3 : 1;
3697 if (!UseNode->isMachineOpcode()) {
3698 int Latency = ItinData->getOperandCycle(DefMCID.getSchedClass(), DefIdx);
3699 if (Subtarget.isLikeA9() || Subtarget.isSwift())
3700 return Latency <= 2 ? 1 : Latency - 1;
3702 return Latency <= 3 ? 1 : Latency - 2;
3705 const MCInstrDesc &UseMCID = get(UseNode->getMachineOpcode());
3706 const MachineSDNode *DefMN = dyn_cast<MachineSDNode>(DefNode);
3707 unsigned DefAlign = !DefMN->memoperands_empty()
3708 ? (*DefMN->memoperands_begin())->getAlignment() : 0;
3709 const MachineSDNode *UseMN = dyn_cast<MachineSDNode>(UseNode);
3710 unsigned UseAlign = !UseMN->memoperands_empty()
3711 ? (*UseMN->memoperands_begin())->getAlignment() : 0;
3712 int Latency = getOperandLatency(ItinData, DefMCID, DefIdx, DefAlign,
3713 UseMCID, UseIdx, UseAlign);
3716 (Subtarget.isCortexA8() || Subtarget.isLikeA9() ||
3717 Subtarget.isCortexA7())) {
3718 // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2]
3719 // variants are one cycle cheaper.
3720 switch (DefMCID.getOpcode()) {
3725 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
3726 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3728 (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
3735 case ARM::t2LDRSHs: {
3736 // Thumb2 mode: lsl only.
3738 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
3739 if (ShAmt == 0 || ShAmt == 2)
3744 } else if (DefIdx == 0 && Latency > 2 && Subtarget.isSwift()) {
3745 // FIXME: Properly handle all of the latency adjustments for address
3747 switch (DefMCID.getOpcode()) {
3752 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
3753 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3755 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
3756 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
3758 else if (ShImm == 1 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsr)
3765 case ARM::t2LDRSHs: {
3766 // Thumb2 mode: lsl 0-3 only.
3773 if (DefAlign < 8 && Subtarget.isLikeA9())
3774 switch (DefMCID.getOpcode()) {
3780 case ARM::VLD1q8wb_register:
3781 case ARM::VLD1q16wb_register:
3782 case ARM::VLD1q32wb_register:
3783 case ARM::VLD1q64wb_register:
3784 case ARM::VLD1q8wb_fixed:
3785 case ARM::VLD1q16wb_fixed:
3786 case ARM::VLD1q32wb_fixed:
3787 case ARM::VLD1q64wb_fixed:
3791 case ARM::VLD2q8Pseudo:
3792 case ARM::VLD2q16Pseudo:
3793 case ARM::VLD2q32Pseudo:
3794 case ARM::VLD2d8wb_fixed:
3795 case ARM::VLD2d16wb_fixed:
3796 case ARM::VLD2d32wb_fixed:
3797 case ARM::VLD2q8PseudoWB_fixed:
3798 case ARM::VLD2q16PseudoWB_fixed:
3799 case ARM::VLD2q32PseudoWB_fixed:
3800 case ARM::VLD2d8wb_register:
3801 case ARM::VLD2d16wb_register:
3802 case ARM::VLD2d32wb_register:
3803 case ARM::VLD2q8PseudoWB_register:
3804 case ARM::VLD2q16PseudoWB_register:
3805 case ARM::VLD2q32PseudoWB_register:
3806 case ARM::VLD3d8Pseudo:
3807 case ARM::VLD3d16Pseudo:
3808 case ARM::VLD3d32Pseudo:
3809 case ARM::VLD1d64TPseudo:
3810 case ARM::VLD1d64TPseudoWB_fixed:
3811 case ARM::VLD3d8Pseudo_UPD:
3812 case ARM::VLD3d16Pseudo_UPD:
3813 case ARM::VLD3d32Pseudo_UPD:
3814 case ARM::VLD3q8Pseudo_UPD:
3815 case ARM::VLD3q16Pseudo_UPD:
3816 case ARM::VLD3q32Pseudo_UPD:
3817 case ARM::VLD3q8oddPseudo:
3818 case ARM::VLD3q16oddPseudo:
3819 case ARM::VLD3q32oddPseudo:
3820 case ARM::VLD3q8oddPseudo_UPD:
3821 case ARM::VLD3q16oddPseudo_UPD:
3822 case ARM::VLD3q32oddPseudo_UPD:
3823 case ARM::VLD4d8Pseudo:
3824 case ARM::VLD4d16Pseudo:
3825 case ARM::VLD4d32Pseudo:
3826 case ARM::VLD1d64QPseudo:
3827 case ARM::VLD1d64QPseudoWB_fixed:
3828 case ARM::VLD4d8Pseudo_UPD:
3829 case ARM::VLD4d16Pseudo_UPD:
3830 case ARM::VLD4d32Pseudo_UPD:
3831 case ARM::VLD4q8Pseudo_UPD:
3832 case ARM::VLD4q16Pseudo_UPD:
3833 case ARM::VLD4q32Pseudo_UPD:
3834 case ARM::VLD4q8oddPseudo:
3835 case ARM::VLD4q16oddPseudo:
3836 case ARM::VLD4q32oddPseudo:
3837 case ARM::VLD4q8oddPseudo_UPD:
3838 case ARM::VLD4q16oddPseudo_UPD:
3839 case ARM::VLD4q32oddPseudo_UPD:
3840 case ARM::VLD1DUPq8:
3841 case ARM::VLD1DUPq16:
3842 case ARM::VLD1DUPq32:
3843 case ARM::VLD1DUPq8wb_fixed:
3844 case ARM::VLD1DUPq16wb_fixed:
3845 case ARM::VLD1DUPq32wb_fixed:
3846 case ARM::VLD1DUPq8wb_register:
3847 case ARM::VLD1DUPq16wb_register:
3848 case ARM::VLD1DUPq32wb_register:
3849 case ARM::VLD2DUPd8:
3850 case ARM::VLD2DUPd16:
3851 case ARM::VLD2DUPd32:
3852 case ARM::VLD2DUPd8wb_fixed:
3853 case ARM::VLD2DUPd16wb_fixed:
3854 case ARM::VLD2DUPd32wb_fixed:
3855 case ARM::VLD2DUPd8wb_register:
3856 case ARM::VLD2DUPd16wb_register:
3857 case ARM::VLD2DUPd32wb_register:
3858 case ARM::VLD4DUPd8Pseudo:
3859 case ARM::VLD4DUPd16Pseudo:
3860 case ARM::VLD4DUPd32Pseudo:
3861 case ARM::VLD4DUPd8Pseudo_UPD:
3862 case ARM::VLD4DUPd16Pseudo_UPD:
3863 case ARM::VLD4DUPd32Pseudo_UPD:
3864 case ARM::VLD1LNq8Pseudo:
3865 case ARM::VLD1LNq16Pseudo:
3866 case ARM::VLD1LNq32Pseudo:
3867 case ARM::VLD1LNq8Pseudo_UPD:
3868 case ARM::VLD1LNq16Pseudo_UPD:
3869 case ARM::VLD1LNq32Pseudo_UPD:
3870 case ARM::VLD2LNd8Pseudo:
3871 case ARM::VLD2LNd16Pseudo:
3872 case ARM::VLD2LNd32Pseudo:
3873 case ARM::VLD2LNq16Pseudo:
3874 case ARM::VLD2LNq32Pseudo:
3875 case ARM::VLD2LNd8Pseudo_UPD:
3876 case ARM::VLD2LNd16Pseudo_UPD:
3877 case ARM::VLD2LNd32Pseudo_UPD:
3878 case ARM::VLD2LNq16Pseudo_UPD:
3879 case ARM::VLD2LNq32Pseudo_UPD:
3880 case ARM::VLD4LNd8Pseudo:
3881 case ARM::VLD4LNd16Pseudo:
3882 case ARM::VLD4LNd32Pseudo:
3883 case ARM::VLD4LNq16Pseudo:
3884 case ARM::VLD4LNq32Pseudo:
3885 case ARM::VLD4LNd8Pseudo_UPD:
3886 case ARM::VLD4LNd16Pseudo_UPD:
3887 case ARM::VLD4LNd32Pseudo_UPD:
3888 case ARM::VLD4LNq16Pseudo_UPD:
3889 case ARM::VLD4LNq32Pseudo_UPD:
3890 // If the address is not 64-bit aligned, the latencies of these
3891 // instructions increases by one.
3899 unsigned ARMBaseInstrInfo::getPredicationCost(const MachineInstr *MI) const {
3900 if (MI->isCopyLike() || MI->isInsertSubreg() ||
3901 MI->isRegSequence() || MI->isImplicitDef())
3907 const MCInstrDesc &MCID = MI->getDesc();
3909 if (MCID.isCall() || MCID.hasImplicitDefOfPhysReg(ARM::CPSR)) {
3910 // When predicated, CPSR is an additional source operand for CPSR updating
3911 // instructions, this apparently increases their latencies.
3917 unsigned ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
3918 const MachineInstr *MI,
3919 unsigned *PredCost) const {
3920 if (MI->isCopyLike() || MI->isInsertSubreg() ||
3921 MI->isRegSequence() || MI->isImplicitDef())
3924 // An instruction scheduler typically runs on unbundled instructions, however
3925 // other passes may query the latency of a bundled instruction.
3926 if (MI->isBundle()) {
3927 unsigned Latency = 0;
3928 MachineBasicBlock::const_instr_iterator I = MI;
3929 MachineBasicBlock::const_instr_iterator E = MI->getParent()->instr_end();
3930 while (++I != E && I->isInsideBundle()) {
3931 if (I->getOpcode() != ARM::t2IT)
3932 Latency += getInstrLatency(ItinData, I, PredCost);
3937 const MCInstrDesc &MCID = MI->getDesc();
3938 if (PredCost && (MCID.isCall() || MCID.hasImplicitDefOfPhysReg(ARM::CPSR))) {
3939 // When predicated, CPSR is an additional source operand for CPSR updating
3940 // instructions, this apparently increases their latencies.
3943 // Be sure to call getStageLatency for an empty itinerary in case it has a
3944 // valid MinLatency property.
3946 return MI->mayLoad() ? 3 : 1;
3948 unsigned Class = MCID.getSchedClass();
3950 // For instructions with variable uops, use uops as latency.
3951 if (!ItinData->isEmpty() && ItinData->getNumMicroOps(Class) < 0)
3952 return getNumMicroOps(ItinData, MI);
3954 // For the common case, fall back on the itinerary's latency.
3955 unsigned Latency = ItinData->getStageLatency(Class);
3957 // Adjust for dynamic def-side opcode variants not captured by the itinerary.
3958 unsigned DefAlign = MI->hasOneMemOperand()
3959 ? (*MI->memoperands_begin())->getAlignment() : 0;
3960 int Adj = adjustDefLatency(Subtarget, MI, &MCID, DefAlign);
3961 if (Adj >= 0 || (int)Latency > -Adj) {
3962 return Latency + Adj;
3967 int ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
3968 SDNode *Node) const {
3969 if (!Node->isMachineOpcode())
3972 if (!ItinData || ItinData->isEmpty())
3975 unsigned Opcode = Node->getMachineOpcode();
3978 return ItinData->getStageLatency(get(Opcode).getSchedClass());
3985 bool ARMBaseInstrInfo::
3986 hasHighOperandLatency(const TargetSchedModel &SchedModel,
3987 const MachineRegisterInfo *MRI,
3988 const MachineInstr *DefMI, unsigned DefIdx,
3989 const MachineInstr *UseMI, unsigned UseIdx) const {
3990 unsigned DDomain = DefMI->getDesc().TSFlags & ARMII::DomainMask;
3991 unsigned UDomain = UseMI->getDesc().TSFlags & ARMII::DomainMask;
3992 if (Subtarget.isCortexA8() &&
3993 (DDomain == ARMII::DomainVFP || UDomain == ARMII::DomainVFP))
3994 // CortexA8 VFP instructions are not pipelined.
3997 // Hoist VFP / NEON instructions with 4 or higher latency.
3999 = SchedModel.computeOperandLatency(DefMI, DefIdx, UseMI, UseIdx);
4002 return DDomain == ARMII::DomainVFP || DDomain == ARMII::DomainNEON ||
4003 UDomain == ARMII::DomainVFP || UDomain == ARMII::DomainNEON;
4006 bool ARMBaseInstrInfo::
4007 hasLowDefLatency(const TargetSchedModel &SchedModel,
4008 const MachineInstr *DefMI, unsigned DefIdx) const {
4009 const InstrItineraryData *ItinData = SchedModel.getInstrItineraries();
4010 if (!ItinData || ItinData->isEmpty())
4013 unsigned DDomain = DefMI->getDesc().TSFlags & ARMII::DomainMask;
4014 if (DDomain == ARMII::DomainGeneral) {
4015 unsigned DefClass = DefMI->getDesc().getSchedClass();
4016 int DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
4017 return (DefCycle != -1 && DefCycle <= 2);
4022 bool ARMBaseInstrInfo::verifyInstruction(const MachineInstr *MI,
4023 StringRef &ErrInfo) const {
4024 if (convertAddSubFlagsOpcode(MI->getOpcode())) {
4025 ErrInfo = "Pseudo flag setting opcodes only exist in Selection DAG";
4031 // LoadStackGuard has so far only been implemented for MachO. Different code
4032 // sequence is needed for other targets.
4033 void ARMBaseInstrInfo::expandLoadStackGuardBase(MachineBasicBlock::iterator MI,
4034 unsigned LoadImmOpc,
4036 Reloc::Model RM) const {
4037 MachineBasicBlock &MBB = *MI->getParent();
4038 DebugLoc DL = MI->getDebugLoc();
4039 unsigned Reg = MI->getOperand(0).getReg();
4040 const GlobalValue *GV =
4041 cast<GlobalValue>((*MI->memoperands_begin())->getValue());
4042 MachineInstrBuilder MIB;
4044 BuildMI(MBB, MI, DL, get(LoadImmOpc), Reg)
4045 .addGlobalAddress(GV, 0, ARMII::MO_NONLAZY);
4047 if (Subtarget.GVIsIndirectSymbol(GV, RM)) {
4048 MIB = BuildMI(MBB, MI, DL, get(LoadOpc), Reg);
4049 MIB.addReg(Reg, RegState::Kill).addImm(0);
4050 unsigned Flag = MachineMemOperand::MOLoad | MachineMemOperand::MOInvariant;
4051 MachineMemOperand *MMO = MBB.getParent()->getMachineMemOperand(
4052 MachinePointerInfo::getGOT(*MBB.getParent()), Flag, 4, 4);
4053 MIB.addMemOperand(MMO);
4054 AddDefaultPred(MIB);
4057 MIB = BuildMI(MBB, MI, DL, get(LoadOpc), Reg);
4058 MIB.addReg(Reg, RegState::Kill).addImm(0);
4059 MIB.setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
4060 AddDefaultPred(MIB);
4064 ARMBaseInstrInfo::isFpMLxInstruction(unsigned Opcode, unsigned &MulOpc,
4065 unsigned &AddSubOpc,
4066 bool &NegAcc, bool &HasLane) const {
4067 DenseMap<unsigned, unsigned>::const_iterator I = MLxEntryMap.find(Opcode);
4068 if (I == MLxEntryMap.end())
4071 const ARM_MLxEntry &Entry = ARM_MLxTable[I->second];
4072 MulOpc = Entry.MulOpc;
4073 AddSubOpc = Entry.AddSubOpc;
4074 NegAcc = Entry.NegAcc;
4075 HasLane = Entry.HasLane;
4079 //===----------------------------------------------------------------------===//
4080 // Execution domains.
4081 //===----------------------------------------------------------------------===//
4083 // Some instructions go down the NEON pipeline, some go down the VFP pipeline,
4084 // and some can go down both. The vmov instructions go down the VFP pipeline,
4085 // but they can be changed to vorr equivalents that are executed by the NEON
4088 // We use the following execution domain numbering:
4096 // Also see ARMInstrFormats.td and Domain* enums in ARMBaseInfo.h
4098 std::pair<uint16_t, uint16_t>
4099 ARMBaseInstrInfo::getExecutionDomain(const MachineInstr *MI) const {
4100 // If we don't have access to NEON instructions then we won't be able
4101 // to swizzle anything to the NEON domain. Check to make sure.
4102 if (Subtarget.hasNEON()) {
4103 // VMOVD, VMOVRS and VMOVSR are VFP instructions, but can be changed to NEON
4104 // if they are not predicated.
4105 if (MI->getOpcode() == ARM::VMOVD && !isPredicated(MI))
4106 return std::make_pair(ExeVFP, (1 << ExeVFP) | (1 << ExeNEON));
4108 // CortexA9 is particularly picky about mixing the two and wants these
4110 if (Subtarget.isCortexA9() && !isPredicated(MI) &&
4111 (MI->getOpcode() == ARM::VMOVRS || MI->getOpcode() == ARM::VMOVSR ||
4112 MI->getOpcode() == ARM::VMOVS))
4113 return std::make_pair(ExeVFP, (1 << ExeVFP) | (1 << ExeNEON));
4115 // No other instructions can be swizzled, so just determine their domain.
4116 unsigned Domain = MI->getDesc().TSFlags & ARMII::DomainMask;
4118 if (Domain & ARMII::DomainNEON)
4119 return std::make_pair(ExeNEON, 0);
4121 // Certain instructions can go either way on Cortex-A8.
4122 // Treat them as NEON instructions.
4123 if ((Domain & ARMII::DomainNEONA8) && Subtarget.isCortexA8())
4124 return std::make_pair(ExeNEON, 0);
4126 if (Domain & ARMII::DomainVFP)
4127 return std::make_pair(ExeVFP, 0);
4129 return std::make_pair(ExeGeneric, 0);
4132 static unsigned getCorrespondingDRegAndLane(const TargetRegisterInfo *TRI,
4133 unsigned SReg, unsigned &Lane) {
4134 unsigned DReg = TRI->getMatchingSuperReg(SReg, ARM::ssub_0, &ARM::DPRRegClass);
4137 if (DReg != ARM::NoRegister)
4141 DReg = TRI->getMatchingSuperReg(SReg, ARM::ssub_1, &ARM::DPRRegClass);
4143 assert(DReg && "S-register with no D super-register?");
4147 /// getImplicitSPRUseForDPRUse - Given a use of a DPR register and lane,
4148 /// set ImplicitSReg to a register number that must be marked as implicit-use or
4149 /// zero if no register needs to be defined as implicit-use.
4151 /// If the function cannot determine if an SPR should be marked implicit use or
4152 /// not, it returns false.
4154 /// This function handles cases where an instruction is being modified from taking
4155 /// an SPR to a DPR[Lane]. A use of the DPR is being added, which may conflict
4156 /// with an earlier def of an SPR corresponding to DPR[Lane^1] (i.e. the other
4157 /// lane of the DPR).
4159 /// If the other SPR is defined, an implicit-use of it should be added. Else,
4160 /// (including the case where the DPR itself is defined), it should not.
4162 static bool getImplicitSPRUseForDPRUse(const TargetRegisterInfo *TRI,
4164 unsigned DReg, unsigned Lane,
4165 unsigned &ImplicitSReg) {
4166 // If the DPR is defined or used already, the other SPR lane will be chained
4167 // correctly, so there is nothing to be done.
4168 if (MI->definesRegister(DReg, TRI) || MI->readsRegister(DReg, TRI)) {
4173 // Otherwise we need to go searching to see if the SPR is set explicitly.
4174 ImplicitSReg = TRI->getSubReg(DReg,
4175 (Lane & 1) ? ARM::ssub_0 : ARM::ssub_1);
4176 MachineBasicBlock::LivenessQueryResult LQR =
4177 MI->getParent()->computeRegisterLiveness(TRI, ImplicitSReg, MI);
4179 if (LQR == MachineBasicBlock::LQR_Live)
4181 else if (LQR == MachineBasicBlock::LQR_Unknown)
4184 // If the register is known not to be live, there is no need to add an
4191 ARMBaseInstrInfo::setExecutionDomain(MachineInstr *MI, unsigned Domain) const {
4192 unsigned DstReg, SrcReg, DReg;
4194 MachineInstrBuilder MIB(*MI->getParent()->getParent(), MI);
4195 const TargetRegisterInfo *TRI = &getRegisterInfo();
4196 switch (MI->getOpcode()) {
4198 llvm_unreachable("cannot handle opcode!");
4201 if (Domain != ExeNEON)
4204 // Zap the predicate operands.
4205 assert(!isPredicated(MI) && "Cannot predicate a VORRd");
4207 // Make sure we've got NEON instructions.
4208 assert(Subtarget.hasNEON() && "VORRd requires NEON");
4210 // Source instruction is %DDst = VMOVD %DSrc, 14, %noreg (; implicits)
4211 DstReg = MI->getOperand(0).getReg();
4212 SrcReg = MI->getOperand(1).getReg();
4214 for (unsigned i = MI->getDesc().getNumOperands(); i; --i)
4215 MI->RemoveOperand(i-1);
4217 // Change to a %DDst = VORRd %DSrc, %DSrc, 14, %noreg (; implicits)
4218 MI->setDesc(get(ARM::VORRd));
4219 AddDefaultPred(MIB.addReg(DstReg, RegState::Define)
4224 if (Domain != ExeNEON)
4226 assert(!isPredicated(MI) && "Cannot predicate a VGETLN");
4228 // Source instruction is %RDst = VMOVRS %SSrc, 14, %noreg (; implicits)
4229 DstReg = MI->getOperand(0).getReg();
4230 SrcReg = MI->getOperand(1).getReg();
4232 for (unsigned i = MI->getDesc().getNumOperands(); i; --i)
4233 MI->RemoveOperand(i-1);
4235 DReg = getCorrespondingDRegAndLane(TRI, SrcReg, Lane);
4237 // Convert to %RDst = VGETLNi32 %DSrc, Lane, 14, %noreg (; imps)
4238 // Note that DSrc has been widened and the other lane may be undef, which
4239 // contaminates the entire register.
4240 MI->setDesc(get(ARM::VGETLNi32));
4241 AddDefaultPred(MIB.addReg(DstReg, RegState::Define)
4242 .addReg(DReg, RegState::Undef)
4245 // The old source should be an implicit use, otherwise we might think it
4246 // was dead before here.
4247 MIB.addReg(SrcReg, RegState::Implicit);
4250 if (Domain != ExeNEON)
4252 assert(!isPredicated(MI) && "Cannot predicate a VSETLN");
4254 // Source instruction is %SDst = VMOVSR %RSrc, 14, %noreg (; implicits)
4255 DstReg = MI->getOperand(0).getReg();
4256 SrcReg = MI->getOperand(1).getReg();
4258 DReg = getCorrespondingDRegAndLane(TRI, DstReg, Lane);
4260 unsigned ImplicitSReg;
4261 if (!getImplicitSPRUseForDPRUse(TRI, MI, DReg, Lane, ImplicitSReg))
4264 for (unsigned i = MI->getDesc().getNumOperands(); i; --i)
4265 MI->RemoveOperand(i-1);
4267 // Convert to %DDst = VSETLNi32 %DDst, %RSrc, Lane, 14, %noreg (; imps)
4268 // Again DDst may be undefined at the beginning of this instruction.
4269 MI->setDesc(get(ARM::VSETLNi32));
4270 MIB.addReg(DReg, RegState::Define)
4271 .addReg(DReg, getUndefRegState(!MI->readsRegister(DReg, TRI)))
4274 AddDefaultPred(MIB);
4276 // The narrower destination must be marked as set to keep previous chains
4278 MIB.addReg(DstReg, RegState::Define | RegState::Implicit);
4279 if (ImplicitSReg != 0)
4280 MIB.addReg(ImplicitSReg, RegState::Implicit);
4284 if (Domain != ExeNEON)
4287 // Source instruction is %SDst = VMOVS %SSrc, 14, %noreg (; implicits)
4288 DstReg = MI->getOperand(0).getReg();
4289 SrcReg = MI->getOperand(1).getReg();
4291 unsigned DstLane = 0, SrcLane = 0, DDst, DSrc;
4292 DDst = getCorrespondingDRegAndLane(TRI, DstReg, DstLane);
4293 DSrc = getCorrespondingDRegAndLane(TRI, SrcReg, SrcLane);
4295 unsigned ImplicitSReg;
4296 if (!getImplicitSPRUseForDPRUse(TRI, MI, DSrc, SrcLane, ImplicitSReg))
4299 for (unsigned i = MI->getDesc().getNumOperands(); i; --i)
4300 MI->RemoveOperand(i-1);
4303 // Destination can be:
4304 // %DDst = VDUPLN32d %DDst, Lane, 14, %noreg (; implicits)
4305 MI->setDesc(get(ARM::VDUPLN32d));
4306 MIB.addReg(DDst, RegState::Define)
4307 .addReg(DDst, getUndefRegState(!MI->readsRegister(DDst, TRI)))
4309 AddDefaultPred(MIB);
4311 // Neither the source or the destination are naturally represented any
4312 // more, so add them in manually.
4313 MIB.addReg(DstReg, RegState::Implicit | RegState::Define);
4314 MIB.addReg(SrcReg, RegState::Implicit);
4315 if (ImplicitSReg != 0)
4316 MIB.addReg(ImplicitSReg, RegState::Implicit);
4320 // In general there's no single instruction that can perform an S <-> S
4321 // move in NEON space, but a pair of VEXT instructions *can* do the
4322 // job. It turns out that the VEXTs needed will only use DSrc once, with
4323 // the position based purely on the combination of lane-0 and lane-1
4324 // involved. For example
4325 // vmov s0, s2 -> vext.32 d0, d0, d1, #1 vext.32 d0, d0, d0, #1
4326 // vmov s1, s3 -> vext.32 d0, d1, d0, #1 vext.32 d0, d0, d0, #1
4327 // vmov s0, s3 -> vext.32 d0, d0, d0, #1 vext.32 d0, d1, d0, #1
4328 // vmov s1, s2 -> vext.32 d0, d0, d0, #1 vext.32 d0, d0, d1, #1
4330 // Pattern of the MachineInstrs is:
4331 // %DDst = VEXTd32 %DSrc1, %DSrc2, Lane, 14, %noreg (;implicits)
4332 MachineInstrBuilder NewMIB;
4333 NewMIB = BuildMI(*MI->getParent(), MI, MI->getDebugLoc(),
4334 get(ARM::VEXTd32), DDst);
4336 // On the first instruction, both DSrc and DDst may be <undef> if present.
4337 // Specifically when the original instruction didn't have them as an
4339 unsigned CurReg = SrcLane == 1 && DstLane == 1 ? DSrc : DDst;
4340 bool CurUndef = !MI->readsRegister(CurReg, TRI);
4341 NewMIB.addReg(CurReg, getUndefRegState(CurUndef));
4343 CurReg = SrcLane == 0 && DstLane == 0 ? DSrc : DDst;
4344 CurUndef = !MI->readsRegister(CurReg, TRI);
4345 NewMIB.addReg(CurReg, getUndefRegState(CurUndef));
4348 AddDefaultPred(NewMIB);
4350 if (SrcLane == DstLane)
4351 NewMIB.addReg(SrcReg, RegState::Implicit);
4353 MI->setDesc(get(ARM::VEXTd32));
4354 MIB.addReg(DDst, RegState::Define);
4356 // On the second instruction, DDst has definitely been defined above, so
4357 // it is not <undef>. DSrc, if present, can be <undef> as above.
4358 CurReg = SrcLane == 1 && DstLane == 0 ? DSrc : DDst;
4359 CurUndef = CurReg == DSrc && !MI->readsRegister(CurReg, TRI);
4360 MIB.addReg(CurReg, getUndefRegState(CurUndef));
4362 CurReg = SrcLane == 0 && DstLane == 1 ? DSrc : DDst;
4363 CurUndef = CurReg == DSrc && !MI->readsRegister(CurReg, TRI);
4364 MIB.addReg(CurReg, getUndefRegState(CurUndef));
4367 AddDefaultPred(MIB);
4369 if (SrcLane != DstLane)
4370 MIB.addReg(SrcReg, RegState::Implicit);
4372 // As before, the original destination is no longer represented, add it
4374 MIB.addReg(DstReg, RegState::Define | RegState::Implicit);
4375 if (ImplicitSReg != 0)
4376 MIB.addReg(ImplicitSReg, RegState::Implicit);
4383 //===----------------------------------------------------------------------===//
4384 // Partial register updates
4385 //===----------------------------------------------------------------------===//
4387 // Swift renames NEON registers with 64-bit granularity. That means any
4388 // instruction writing an S-reg implicitly reads the containing D-reg. The
4389 // problem is mostly avoided by translating f32 operations to v2f32 operations
4390 // on D-registers, but f32 loads are still a problem.
4392 // These instructions can load an f32 into a NEON register:
4394 // VLDRS - Only writes S, partial D update.
4395 // VLD1LNd32 - Writes all D-regs, explicit partial D update, 2 uops.
4396 // VLD1DUPd32 - Writes all D-regs, no partial reg update, 2 uops.
4398 // FCONSTD can be used as a dependency-breaking instruction.
4399 unsigned ARMBaseInstrInfo::
4400 getPartialRegUpdateClearance(const MachineInstr *MI,
4402 const TargetRegisterInfo *TRI) const {
4403 if (!SwiftPartialUpdateClearance ||
4404 !(Subtarget.isSwift() || Subtarget.isCortexA15()))
4407 assert(TRI && "Need TRI instance");
4409 const MachineOperand &MO = MI->getOperand(OpNum);
4412 unsigned Reg = MO.getReg();
4415 switch(MI->getOpcode()) {
4416 // Normal instructions writing only an S-register.
4421 case ARM::VMOVv4i16:
4422 case ARM::VMOVv2i32:
4423 case ARM::VMOVv2f32:
4424 case ARM::VMOVv1i64:
4425 UseOp = MI->findRegisterUseOperandIdx(Reg, false, TRI);
4428 // Explicitly reads the dependency.
4429 case ARM::VLD1LNd32:
4436 // If this instruction actually reads a value from Reg, there is no unwanted
4438 if (UseOp != -1 && MI->getOperand(UseOp).readsReg())
4441 // We must be able to clobber the whole D-reg.
4442 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
4443 // Virtual register must be a foo:ssub_0<def,undef> operand.
4444 if (!MO.getSubReg() || MI->readsVirtualRegister(Reg))
4446 } else if (ARM::SPRRegClass.contains(Reg)) {
4447 // Physical register: MI must define the full D-reg.
4448 unsigned DReg = TRI->getMatchingSuperReg(Reg, ARM::ssub_0,
4450 if (!DReg || !MI->definesRegister(DReg, TRI))
4454 // MI has an unwanted D-register dependency.
4455 // Avoid defs in the previous N instructrions.
4456 return SwiftPartialUpdateClearance;
4459 // Break a partial register dependency after getPartialRegUpdateClearance
4460 // returned non-zero.
4461 void ARMBaseInstrInfo::
4462 breakPartialRegDependency(MachineBasicBlock::iterator MI,
4464 const TargetRegisterInfo *TRI) const {
4465 assert(MI && OpNum < MI->getDesc().getNumDefs() && "OpNum is not a def");
4466 assert(TRI && "Need TRI instance");
4468 const MachineOperand &MO = MI->getOperand(OpNum);
4469 unsigned Reg = MO.getReg();
4470 assert(TargetRegisterInfo::isPhysicalRegister(Reg) &&
4471 "Can't break virtual register dependencies.");
4472 unsigned DReg = Reg;
4474 // If MI defines an S-reg, find the corresponding D super-register.
4475 if (ARM::SPRRegClass.contains(Reg)) {
4476 DReg = ARM::D0 + (Reg - ARM::S0) / 2;
4477 assert(TRI->isSuperRegister(Reg, DReg) && "Register enums broken");
4480 assert(ARM::DPRRegClass.contains(DReg) && "Can only break D-reg deps");
4481 assert(MI->definesRegister(DReg, TRI) && "MI doesn't clobber full D-reg");
4483 // FIXME: In some cases, VLDRS can be changed to a VLD1DUPd32 which defines
4484 // the full D-register by loading the same value to both lanes. The
4485 // instruction is micro-coded with 2 uops, so don't do this until we can
4486 // properly schedule micro-coded instructions. The dispatcher stalls cause
4487 // too big regressions.
4489 // Insert the dependency-breaking FCONSTD before MI.
4490 // 96 is the encoding of 0.5, but the actual value doesn't matter here.
4491 AddDefaultPred(BuildMI(*MI->getParent(), MI, MI->getDebugLoc(),
4492 get(ARM::FCONSTD), DReg).addImm(96));
4493 MI->addRegisterKilled(DReg, TRI, true);
4496 bool ARMBaseInstrInfo::hasNOP() const {
4497 return Subtarget.getFeatureBits()[ARM::HasV6KOps];
4500 bool ARMBaseInstrInfo::isSwiftFastImmShift(const MachineInstr *MI) const {
4501 if (MI->getNumOperands() < 4)
4503 unsigned ShOpVal = MI->getOperand(3).getImm();
4504 unsigned ShImm = ARM_AM::getSORegOffset(ShOpVal);
4505 // Swift supports faster shifts for: lsl 2, lsl 1, and lsr 1.
4506 if ((ShImm == 1 && ARM_AM::getSORegShOp(ShOpVal) == ARM_AM::lsr) ||
4507 ((ShImm == 1 || ShImm == 2) &&
4508 ARM_AM::getSORegShOp(ShOpVal) == ARM_AM::lsl))
4514 bool ARMBaseInstrInfo::getRegSequenceLikeInputs(
4515 const MachineInstr &MI, unsigned DefIdx,
4516 SmallVectorImpl<RegSubRegPairAndIdx> &InputRegs) const {
4517 assert(DefIdx < MI.getDesc().getNumDefs() && "Invalid definition index");
4518 assert(MI.isRegSequenceLike() && "Invalid kind of instruction");
4520 switch (MI.getOpcode()) {
4522 // dX = VMOVDRR rY, rZ
4524 // dX = REG_SEQUENCE rY, ssub_0, rZ, ssub_1
4525 // Populate the InputRegs accordingly.
4527 const MachineOperand *MOReg = &MI.getOperand(1);
4528 InputRegs.push_back(
4529 RegSubRegPairAndIdx(MOReg->getReg(), MOReg->getSubReg(), ARM::ssub_0));
4531 MOReg = &MI.getOperand(2);
4532 InputRegs.push_back(
4533 RegSubRegPairAndIdx(MOReg->getReg(), MOReg->getSubReg(), ARM::ssub_1));
4536 llvm_unreachable("Target dependent opcode missing");
4539 bool ARMBaseInstrInfo::getExtractSubregLikeInputs(
4540 const MachineInstr &MI, unsigned DefIdx,
4541 RegSubRegPairAndIdx &InputReg) const {
4542 assert(DefIdx < MI.getDesc().getNumDefs() && "Invalid definition index");
4543 assert(MI.isExtractSubregLike() && "Invalid kind of instruction");
4545 switch (MI.getOpcode()) {
4547 // rX, rY = VMOVRRD dZ
4549 // rX = EXTRACT_SUBREG dZ, ssub_0
4550 // rY = EXTRACT_SUBREG dZ, ssub_1
4551 const MachineOperand &MOReg = MI.getOperand(2);
4552 InputReg.Reg = MOReg.getReg();
4553 InputReg.SubReg = MOReg.getSubReg();
4554 InputReg.SubIdx = DefIdx == 0 ? ARM::ssub_0 : ARM::ssub_1;
4557 llvm_unreachable("Target dependent opcode missing");
4560 bool ARMBaseInstrInfo::getInsertSubregLikeInputs(
4561 const MachineInstr &MI, unsigned DefIdx, RegSubRegPair &BaseReg,
4562 RegSubRegPairAndIdx &InsertedReg) const {
4563 assert(DefIdx < MI.getDesc().getNumDefs() && "Invalid definition index");
4564 assert(MI.isInsertSubregLike() && "Invalid kind of instruction");
4566 switch (MI.getOpcode()) {
4567 case ARM::VSETLNi32:
4568 // dX = VSETLNi32 dY, rZ, imm
4569 const MachineOperand &MOBaseReg = MI.getOperand(1);
4570 const MachineOperand &MOInsertedReg = MI.getOperand(2);
4571 const MachineOperand &MOIndex = MI.getOperand(3);
4572 BaseReg.Reg = MOBaseReg.getReg();
4573 BaseReg.SubReg = MOBaseReg.getSubReg();
4575 InsertedReg.Reg = MOInsertedReg.getReg();
4576 InsertedReg.SubReg = MOInsertedReg.getSubReg();
4577 InsertedReg.SubIdx = MOIndex.getImm() == 0 ? ARM::ssub_0 : ARM::ssub_1;
4580 llvm_unreachable("Target dependent opcode missing");