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 //===----------------------------------------------------------------------===//
14 #include "ARMBaseInstrInfo.h"
15 #include "ARMBaseRegisterInfo.h"
16 #include "ARMConstantPoolValue.h"
17 #include "ARMFeatures.h"
18 #include "ARMHazardRecognizer.h"
19 #include "ARMMachineFunctionInfo.h"
20 #include "ARMSubtarget.h"
21 #include "MCTargetDesc/ARMAddressingModes.h"
22 #include "MCTargetDesc/ARMBaseInfo.h"
23 #include "llvm/ADT/DenseMap.h"
24 #include "llvm/ADT/STLExtras.h"
25 #include "llvm/ADT/SmallSet.h"
26 #include "llvm/ADT/SmallVector.h"
27 #include "llvm/ADT/Triple.h"
28 #include "llvm/CodeGen/LiveVariables.h"
29 #include "llvm/CodeGen/MachineBasicBlock.h"
30 #include "llvm/CodeGen/MachineConstantPool.h"
31 #include "llvm/CodeGen/MachineFrameInfo.h"
32 #include "llvm/CodeGen/MachineFunction.h"
33 #include "llvm/CodeGen/MachineInstr.h"
34 #include "llvm/CodeGen/MachineInstrBuilder.h"
35 #include "llvm/CodeGen/MachineMemOperand.h"
36 #include "llvm/CodeGen/MachineOperand.h"
37 #include "llvm/CodeGen/MachineRegisterInfo.h"
38 #include "llvm/CodeGen/ScoreboardHazardRecognizer.h"
39 #include "llvm/CodeGen/SelectionDAGNodes.h"
40 #include "llvm/CodeGen/TargetInstrInfo.h"
41 #include "llvm/CodeGen/TargetRegisterInfo.h"
42 #include "llvm/CodeGen/TargetSchedule.h"
43 #include "llvm/IR/Attributes.h"
44 #include "llvm/IR/Constants.h"
45 #include "llvm/IR/DebugLoc.h"
46 #include "llvm/IR/Function.h"
47 #include "llvm/IR/GlobalValue.h"
48 #include "llvm/MC/MCAsmInfo.h"
49 #include "llvm/MC/MCInstrDesc.h"
50 #include "llvm/MC/MCInstrItineraries.h"
51 #include "llvm/Support/BranchProbability.h"
52 #include "llvm/Support/Casting.h"
53 #include "llvm/Support/CommandLine.h"
54 #include "llvm/Support/Compiler.h"
55 #include "llvm/Support/Debug.h"
56 #include "llvm/Support/ErrorHandling.h"
57 #include "llvm/Support/raw_ostream.h"
58 #include "llvm/Target/TargetMachine.h"
69 #define DEBUG_TYPE "arm-instrinfo"
71 #define GET_INSTRINFO_CTOR_DTOR
72 #include "ARMGenInstrInfo.inc"
75 EnableARM3Addr("enable-arm-3-addr-conv", cl::Hidden,
76 cl::desc("Enable ARM 2-addr to 3-addr conv"));
78 /// ARM_MLxEntry - Record information about MLA / MLS instructions.
80 uint16_t MLxOpc; // MLA / MLS opcode
81 uint16_t MulOpc; // Expanded multiplication opcode
82 uint16_t AddSubOpc; // Expanded add / sub opcode
83 bool NegAcc; // True if the acc is negated before the add / sub.
84 bool HasLane; // True if instruction has an extra "lane" operand.
87 static const ARM_MLxEntry ARM_MLxTable[] = {
88 // MLxOpc, MulOpc, AddSubOpc, NegAcc, HasLane
90 { ARM::VMLAS, ARM::VMULS, ARM::VADDS, false, false },
91 { ARM::VMLSS, ARM::VMULS, ARM::VSUBS, false, false },
92 { ARM::VMLAD, ARM::VMULD, ARM::VADDD, false, false },
93 { ARM::VMLSD, ARM::VMULD, ARM::VSUBD, false, false },
94 { ARM::VNMLAS, ARM::VNMULS, ARM::VSUBS, true, false },
95 { ARM::VNMLSS, ARM::VMULS, ARM::VSUBS, true, false },
96 { ARM::VNMLAD, ARM::VNMULD, ARM::VSUBD, true, false },
97 { ARM::VNMLSD, ARM::VMULD, ARM::VSUBD, true, false },
100 { ARM::VMLAfd, ARM::VMULfd, ARM::VADDfd, false, false },
101 { ARM::VMLSfd, ARM::VMULfd, ARM::VSUBfd, false, false },
102 { ARM::VMLAfq, ARM::VMULfq, ARM::VADDfq, false, false },
103 { ARM::VMLSfq, ARM::VMULfq, ARM::VSUBfq, false, false },
104 { ARM::VMLAslfd, ARM::VMULslfd, ARM::VADDfd, false, true },
105 { ARM::VMLSslfd, ARM::VMULslfd, ARM::VSUBfd, false, true },
106 { ARM::VMLAslfq, ARM::VMULslfq, ARM::VADDfq, false, true },
107 { ARM::VMLSslfq, ARM::VMULslfq, ARM::VSUBfq, false, true },
110 ARMBaseInstrInfo::ARMBaseInstrInfo(const ARMSubtarget& STI)
111 : ARMGenInstrInfo(ARM::ADJCALLSTACKDOWN, ARM::ADJCALLSTACKUP),
113 for (unsigned i = 0, e = array_lengthof(ARM_MLxTable); i != e; ++i) {
114 if (!MLxEntryMap.insert(std::make_pair(ARM_MLxTable[i].MLxOpc, i)).second)
115 llvm_unreachable("Duplicated entries?");
116 MLxHazardOpcodes.insert(ARM_MLxTable[i].AddSubOpc);
117 MLxHazardOpcodes.insert(ARM_MLxTable[i].MulOpc);
121 // Use a ScoreboardHazardRecognizer for prepass ARM scheduling. TargetInstrImpl
122 // currently defaults to no prepass hazard recognizer.
123 ScheduleHazardRecognizer *
124 ARMBaseInstrInfo::CreateTargetHazardRecognizer(const TargetSubtargetInfo *STI,
125 const ScheduleDAG *DAG) const {
126 if (usePreRAHazardRecognizer()) {
127 const InstrItineraryData *II =
128 static_cast<const ARMSubtarget *>(STI)->getInstrItineraryData();
129 return new ScoreboardHazardRecognizer(II, DAG, "pre-RA-sched");
131 return TargetInstrInfo::CreateTargetHazardRecognizer(STI, DAG);
134 ScheduleHazardRecognizer *ARMBaseInstrInfo::
135 CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II,
136 const ScheduleDAG *DAG) const {
137 if (Subtarget.isThumb2() || Subtarget.hasVFP2())
138 return (ScheduleHazardRecognizer *)new ARMHazardRecognizer(II, DAG);
139 return TargetInstrInfo::CreateTargetPostRAHazardRecognizer(II, DAG);
142 MachineInstr *ARMBaseInstrInfo::convertToThreeAddress(
143 MachineFunction::iterator &MFI, MachineInstr &MI, LiveVariables *LV) const {
144 // FIXME: Thumb2 support.
149 MachineFunction &MF = *MI.getParent()->getParent();
150 uint64_t TSFlags = MI.getDesc().TSFlags;
152 switch ((TSFlags & ARMII::IndexModeMask) >> ARMII::IndexModeShift) {
153 default: return nullptr;
154 case ARMII::IndexModePre:
157 case ARMII::IndexModePost:
161 // Try splitting an indexed load/store to an un-indexed one plus an add/sub
163 unsigned MemOpc = getUnindexedOpcode(MI.getOpcode());
167 MachineInstr *UpdateMI = nullptr;
168 MachineInstr *MemMI = nullptr;
169 unsigned AddrMode = (TSFlags & ARMII::AddrModeMask);
170 const MCInstrDesc &MCID = MI.getDesc();
171 unsigned NumOps = MCID.getNumOperands();
172 bool isLoad = !MI.mayStore();
173 const MachineOperand &WB = isLoad ? MI.getOperand(1) : MI.getOperand(0);
174 const MachineOperand &Base = MI.getOperand(2);
175 const MachineOperand &Offset = MI.getOperand(NumOps - 3);
176 unsigned WBReg = WB.getReg();
177 unsigned BaseReg = Base.getReg();
178 unsigned OffReg = Offset.getReg();
179 unsigned OffImm = MI.getOperand(NumOps - 2).getImm();
180 ARMCC::CondCodes Pred = (ARMCC::CondCodes)MI.getOperand(NumOps - 1).getImm();
182 default: llvm_unreachable("Unknown indexed op!");
183 case ARMII::AddrMode2: {
184 bool isSub = ARM_AM::getAM2Op(OffImm) == ARM_AM::sub;
185 unsigned Amt = ARM_AM::getAM2Offset(OffImm);
187 if (ARM_AM::getSOImmVal(Amt) == -1)
188 // Can't encode it in a so_imm operand. This transformation will
189 // add more than 1 instruction. Abandon!
191 UpdateMI = BuildMI(MF, MI.getDebugLoc(),
192 get(isSub ? ARM::SUBri : ARM::ADDri), WBReg)
197 } else if (Amt != 0) {
198 ARM_AM::ShiftOpc ShOpc = ARM_AM::getAM2ShiftOpc(OffImm);
199 unsigned SOOpc = ARM_AM::getSORegOpc(ShOpc, Amt);
200 UpdateMI = BuildMI(MF, MI.getDebugLoc(),
201 get(isSub ? ARM::SUBrsi : ARM::ADDrsi), WBReg)
209 UpdateMI = BuildMI(MF, MI.getDebugLoc(),
210 get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
217 case ARMII::AddrMode3 : {
218 bool isSub = ARM_AM::getAM3Op(OffImm) == ARM_AM::sub;
219 unsigned Amt = ARM_AM::getAM3Offset(OffImm);
221 // Immediate is 8-bits. It's guaranteed to fit in a so_imm operand.
222 UpdateMI = BuildMI(MF, MI.getDebugLoc(),
223 get(isSub ? ARM::SUBri : ARM::ADDri), WBReg)
229 UpdateMI = BuildMI(MF, MI.getDebugLoc(),
230 get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
239 std::vector<MachineInstr*> NewMIs;
243 BuildMI(MF, MI.getDebugLoc(), get(MemOpc), MI.getOperand(0).getReg())
248 MemMI = BuildMI(MF, MI.getDebugLoc(), get(MemOpc))
249 .addReg(MI.getOperand(1).getReg())
254 NewMIs.push_back(MemMI);
255 NewMIs.push_back(UpdateMI);
259 BuildMI(MF, MI.getDebugLoc(), get(MemOpc), MI.getOperand(0).getReg())
264 MemMI = BuildMI(MF, MI.getDebugLoc(), get(MemOpc))
265 .addReg(MI.getOperand(1).getReg())
271 UpdateMI->getOperand(0).setIsDead();
272 NewMIs.push_back(UpdateMI);
273 NewMIs.push_back(MemMI);
276 // Transfer LiveVariables states, kill / dead info.
278 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
279 MachineOperand &MO = MI.getOperand(i);
280 if (MO.isReg() && TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
281 unsigned Reg = MO.getReg();
283 LiveVariables::VarInfo &VI = LV->getVarInfo(Reg);
285 MachineInstr *NewMI = (Reg == WBReg) ? UpdateMI : MemMI;
287 LV->addVirtualRegisterDead(Reg, *NewMI);
289 if (MO.isUse() && MO.isKill()) {
290 for (unsigned j = 0; j < 2; ++j) {
291 // Look at the two new MI's in reverse order.
292 MachineInstr *NewMI = NewMIs[j];
293 if (!NewMI->readsRegister(Reg))
295 LV->addVirtualRegisterKilled(Reg, *NewMI);
296 if (VI.removeKill(MI))
297 VI.Kills.push_back(NewMI);
305 MachineBasicBlock::iterator MBBI = MI.getIterator();
306 MFI->insert(MBBI, NewMIs[1]);
307 MFI->insert(MBBI, NewMIs[0]);
312 bool ARMBaseInstrInfo::analyzeBranch(MachineBasicBlock &MBB,
313 MachineBasicBlock *&TBB,
314 MachineBasicBlock *&FBB,
315 SmallVectorImpl<MachineOperand> &Cond,
316 bool AllowModify) const {
320 MachineBasicBlock::iterator I = MBB.end();
321 if (I == MBB.begin())
322 return false; // Empty blocks are easy.
325 // Walk backwards from the end of the basic block until the branch is
326 // analyzed or we give up.
327 while (isPredicated(*I) || I->isTerminator() || I->isDebugValue()) {
328 // Flag to be raised on unanalyzeable instructions. This is useful in cases
329 // where we want to clean up on the end of the basic block before we bail
331 bool CantAnalyze = false;
333 // Skip over DEBUG values and predicated nonterminators.
334 while (I->isDebugInstr() || !I->isTerminator()) {
335 if (I == MBB.begin())
340 if (isIndirectBranchOpcode(I->getOpcode()) ||
341 isJumpTableBranchOpcode(I->getOpcode())) {
342 // Indirect branches and jump tables can't be analyzed, but we still want
343 // to clean up any instructions at the tail of the basic block.
345 } else if (isUncondBranchOpcode(I->getOpcode())) {
346 TBB = I->getOperand(0).getMBB();
347 } else if (isCondBranchOpcode(I->getOpcode())) {
348 // Bail out if we encounter multiple conditional branches.
352 assert(!FBB && "FBB should have been null.");
354 TBB = I->getOperand(0).getMBB();
355 Cond.push_back(I->getOperand(1));
356 Cond.push_back(I->getOperand(2));
357 } else if (I->isReturn()) {
358 // Returns can't be analyzed, but we should run cleanup.
359 CantAnalyze = !isPredicated(*I);
361 // We encountered other unrecognized terminator. Bail out immediately.
365 // Cleanup code - to be run for unpredicated unconditional branches and
367 if (!isPredicated(*I) &&
368 (isUncondBranchOpcode(I->getOpcode()) ||
369 isIndirectBranchOpcode(I->getOpcode()) ||
370 isJumpTableBranchOpcode(I->getOpcode()) ||
372 // Forget any previous condition branch information - it no longer applies.
376 // If we can modify the function, delete everything below this
377 // unconditional branch.
379 MachineBasicBlock::iterator DI = std::next(I);
380 while (DI != MBB.end()) {
381 MachineInstr &InstToDelete = *DI;
383 InstToDelete.eraseFromParent();
391 if (I == MBB.begin())
397 // We made it past the terminators without bailing out - we must have
398 // analyzed this branch successfully.
402 unsigned ARMBaseInstrInfo::removeBranch(MachineBasicBlock &MBB,
403 int *BytesRemoved) const {
404 assert(!BytesRemoved && "code size not handled");
406 MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr();
410 if (!isUncondBranchOpcode(I->getOpcode()) &&
411 !isCondBranchOpcode(I->getOpcode()))
414 // Remove the branch.
415 I->eraseFromParent();
419 if (I == MBB.begin()) return 1;
421 if (!isCondBranchOpcode(I->getOpcode()))
424 // Remove the branch.
425 I->eraseFromParent();
429 unsigned ARMBaseInstrInfo::insertBranch(MachineBasicBlock &MBB,
430 MachineBasicBlock *TBB,
431 MachineBasicBlock *FBB,
432 ArrayRef<MachineOperand> Cond,
434 int *BytesAdded) const {
435 assert(!BytesAdded && "code size not handled");
436 ARMFunctionInfo *AFI = MBB.getParent()->getInfo<ARMFunctionInfo>();
437 int BOpc = !AFI->isThumbFunction()
438 ? ARM::B : (AFI->isThumb2Function() ? ARM::t2B : ARM::tB);
439 int BccOpc = !AFI->isThumbFunction()
440 ? ARM::Bcc : (AFI->isThumb2Function() ? ARM::t2Bcc : ARM::tBcc);
441 bool isThumb = AFI->isThumbFunction() || AFI->isThumb2Function();
443 // Shouldn't be a fall through.
444 assert(TBB && "insertBranch must not be told to insert a fallthrough");
445 assert((Cond.size() == 2 || Cond.size() == 0) &&
446 "ARM branch conditions have two components!");
448 // For conditional branches, we use addOperand to preserve CPSR flags.
451 if (Cond.empty()) { // Unconditional branch?
453 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB).add(predOps(ARMCC::AL));
455 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB);
457 BuildMI(&MBB, DL, get(BccOpc))
459 .addImm(Cond[0].getImm())
464 // Two-way conditional branch.
465 BuildMI(&MBB, DL, get(BccOpc))
467 .addImm(Cond[0].getImm())
470 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB).add(predOps(ARMCC::AL));
472 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB);
476 bool ARMBaseInstrInfo::
477 reverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
478 ARMCC::CondCodes CC = (ARMCC::CondCodes)(int)Cond[0].getImm();
479 Cond[0].setImm(ARMCC::getOppositeCondition(CC));
483 bool ARMBaseInstrInfo::isPredicated(const MachineInstr &MI) const {
485 MachineBasicBlock::const_instr_iterator I = MI.getIterator();
486 MachineBasicBlock::const_instr_iterator E = MI.getParent()->instr_end();
487 while (++I != E && I->isInsideBundle()) {
488 int PIdx = I->findFirstPredOperandIdx();
489 if (PIdx != -1 && I->getOperand(PIdx).getImm() != ARMCC::AL)
495 int PIdx = MI.findFirstPredOperandIdx();
496 return PIdx != -1 && MI.getOperand(PIdx).getImm() != ARMCC::AL;
499 bool ARMBaseInstrInfo::PredicateInstruction(
500 MachineInstr &MI, ArrayRef<MachineOperand> Pred) const {
501 unsigned Opc = MI.getOpcode();
502 if (isUncondBranchOpcode(Opc)) {
503 MI.setDesc(get(getMatchingCondBranchOpcode(Opc)));
504 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
505 .addImm(Pred[0].getImm())
506 .addReg(Pred[1].getReg());
510 int PIdx = MI.findFirstPredOperandIdx();
512 MachineOperand &PMO = MI.getOperand(PIdx);
513 PMO.setImm(Pred[0].getImm());
514 MI.getOperand(PIdx+1).setReg(Pred[1].getReg());
520 bool ARMBaseInstrInfo::SubsumesPredicate(ArrayRef<MachineOperand> Pred1,
521 ArrayRef<MachineOperand> Pred2) const {
522 if (Pred1.size() > 2 || Pred2.size() > 2)
525 ARMCC::CondCodes CC1 = (ARMCC::CondCodes)Pred1[0].getImm();
526 ARMCC::CondCodes CC2 = (ARMCC::CondCodes)Pred2[0].getImm();
536 return CC2 == ARMCC::HI;
538 return CC2 == ARMCC::LO || CC2 == ARMCC::EQ;
540 return CC2 == ARMCC::GT;
542 return CC2 == ARMCC::LT;
546 bool ARMBaseInstrInfo::DefinesPredicate(
547 MachineInstr &MI, std::vector<MachineOperand> &Pred) const {
549 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
550 const MachineOperand &MO = MI.getOperand(i);
551 if ((MO.isRegMask() && MO.clobbersPhysReg(ARM::CPSR)) ||
552 (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR)) {
561 bool ARMBaseInstrInfo::isCPSRDefined(const MachineInstr &MI) {
562 for (const auto &MO : MI.operands())
563 if (MO.isReg() && MO.getReg() == ARM::CPSR && MO.isDef() && !MO.isDead())
568 bool ARMBaseInstrInfo::isAddrMode3OpImm(const MachineInstr &MI,
570 const MachineOperand &Offset = MI.getOperand(Op + 1);
571 return Offset.getReg() != 0;
574 // Load with negative register offset requires additional 1cyc and +I unit
576 bool ARMBaseInstrInfo::isAddrMode3OpMinusReg(const MachineInstr &MI,
578 const MachineOperand &Offset = MI.getOperand(Op + 1);
579 const MachineOperand &Opc = MI.getOperand(Op + 2);
581 assert(Offset.isReg());
582 int64_t OpcImm = Opc.getImm();
584 bool isSub = ARM_AM::getAM3Op(OpcImm) == ARM_AM::sub;
585 return (isSub && Offset.getReg() != 0);
588 bool ARMBaseInstrInfo::isLdstScaledReg(const MachineInstr &MI,
590 const MachineOperand &Opc = MI.getOperand(Op + 2);
591 unsigned OffImm = Opc.getImm();
592 return ARM_AM::getAM2ShiftOpc(OffImm) != ARM_AM::no_shift;
595 // Load, scaled register offset, not plus LSL2
596 bool ARMBaseInstrInfo::isLdstScaledRegNotPlusLsl2(const MachineInstr &MI,
598 const MachineOperand &Opc = MI.getOperand(Op + 2);
599 unsigned OffImm = Opc.getImm();
601 bool isAdd = ARM_AM::getAM2Op(OffImm) == ARM_AM::add;
602 unsigned Amt = ARM_AM::getAM2Offset(OffImm);
603 ARM_AM::ShiftOpc ShiftOpc = ARM_AM::getAM2ShiftOpc(OffImm);
604 if (ShiftOpc == ARM_AM::no_shift) return false; // not scaled
605 bool SimpleScaled = (isAdd && ShiftOpc == ARM_AM::lsl && Amt == 2);
606 return !SimpleScaled;
609 // Minus reg for ldstso addr mode
610 bool ARMBaseInstrInfo::isLdstSoMinusReg(const MachineInstr &MI,
612 unsigned OffImm = MI.getOperand(Op + 2).getImm();
613 return ARM_AM::getAM2Op(OffImm) == ARM_AM::sub;
616 // Load, scaled register offset
617 bool ARMBaseInstrInfo::isAm2ScaledReg(const MachineInstr &MI,
619 unsigned OffImm = MI.getOperand(Op + 2).getImm();
620 return ARM_AM::getAM2ShiftOpc(OffImm) != ARM_AM::no_shift;
623 static bool isEligibleForITBlock(const MachineInstr *MI) {
624 switch (MI->getOpcode()) {
625 default: return true;
626 case ARM::tADC: // ADC (register) T1
627 case ARM::tADDi3: // ADD (immediate) T1
628 case ARM::tADDi8: // ADD (immediate) T2
629 case ARM::tADDrr: // ADD (register) T1
630 case ARM::tAND: // AND (register) T1
631 case ARM::tASRri: // ASR (immediate) T1
632 case ARM::tASRrr: // ASR (register) T1
633 case ARM::tBIC: // BIC (register) T1
634 case ARM::tEOR: // EOR (register) T1
635 case ARM::tLSLri: // LSL (immediate) T1
636 case ARM::tLSLrr: // LSL (register) T1
637 case ARM::tLSRri: // LSR (immediate) T1
638 case ARM::tLSRrr: // LSR (register) T1
639 case ARM::tMUL: // MUL T1
640 case ARM::tMVN: // MVN (register) T1
641 case ARM::tORR: // ORR (register) T1
642 case ARM::tROR: // ROR (register) T1
643 case ARM::tRSB: // RSB (immediate) T1
644 case ARM::tSBC: // SBC (register) T1
645 case ARM::tSUBi3: // SUB (immediate) T1
646 case ARM::tSUBi8: // SUB (immediate) T2
647 case ARM::tSUBrr: // SUB (register) T1
648 return !ARMBaseInstrInfo::isCPSRDefined(*MI);
652 /// isPredicable - Return true if the specified instruction can be predicated.
653 /// By default, this returns true for every instruction with a
654 /// PredicateOperand.
655 bool ARMBaseInstrInfo::isPredicable(const MachineInstr &MI) const {
656 if (!MI.isPredicable())
662 if (!isEligibleForITBlock(&MI))
665 const ARMFunctionInfo *AFI =
666 MI.getParent()->getParent()->getInfo<ARMFunctionInfo>();
668 // Neon instructions in Thumb2 IT blocks are deprecated, see ARMARM.
669 // In their ARM encoding, they can't be encoded in a conditional form.
670 if ((MI.getDesc().TSFlags & ARMII::DomainMask) == ARMII::DomainNEON)
673 if (AFI->isThumb2Function()) {
674 if (getSubtarget().restrictIT())
675 return isV8EligibleForIT(&MI);
683 template <> bool IsCPSRDead<MachineInstr>(const MachineInstr *MI) {
684 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
685 const MachineOperand &MO = MI->getOperand(i);
686 if (!MO.isReg() || MO.isUndef() || MO.isUse())
688 if (MO.getReg() != ARM::CPSR)
693 // all definitions of CPSR are dead
697 } // end namespace llvm
699 /// GetInstSize - Return the size of the specified MachineInstr.
701 unsigned ARMBaseInstrInfo::getInstSizeInBytes(const MachineInstr &MI) const {
702 const MachineBasicBlock &MBB = *MI.getParent();
703 const MachineFunction *MF = MBB.getParent();
704 const MCAsmInfo *MAI = MF->getTarget().getMCAsmInfo();
706 const MCInstrDesc &MCID = MI.getDesc();
708 return MCID.getSize();
710 // If this machine instr is an inline asm, measure it.
711 if (MI.getOpcode() == ARM::INLINEASM) {
712 unsigned Size = getInlineAsmLength(MI.getOperand(0).getSymbolName(), *MAI);
713 if (!MF->getInfo<ARMFunctionInfo>()->isThumbFunction())
714 Size = alignTo(Size, 4);
717 unsigned Opc = MI.getOpcode();
720 // pseudo-instruction sizes are zero.
722 case TargetOpcode::BUNDLE:
723 return getInstBundleLength(MI);
724 case ARM::MOVi16_ga_pcrel:
725 case ARM::MOVTi16_ga_pcrel:
726 case ARM::t2MOVi16_ga_pcrel:
727 case ARM::t2MOVTi16_ga_pcrel:
730 case ARM::t2MOVi32imm:
732 case ARM::CONSTPOOL_ENTRY:
733 case ARM::JUMPTABLE_INSTS:
734 case ARM::JUMPTABLE_ADDRS:
735 case ARM::JUMPTABLE_TBB:
736 case ARM::JUMPTABLE_TBH:
737 // If this machine instr is a constant pool entry, its size is recorded as
739 return MI.getOperand(2).getImm();
740 case ARM::Int_eh_sjlj_longjmp:
742 case ARM::tInt_eh_sjlj_longjmp:
744 case ARM::tInt_WIN_eh_sjlj_longjmp:
746 case ARM::Int_eh_sjlj_setjmp:
747 case ARM::Int_eh_sjlj_setjmp_nofp:
749 case ARM::tInt_eh_sjlj_setjmp:
750 case ARM::t2Int_eh_sjlj_setjmp:
751 case ARM::t2Int_eh_sjlj_setjmp_nofp:
754 return MI.getOperand(1).getImm();
758 unsigned ARMBaseInstrInfo::getInstBundleLength(const MachineInstr &MI) const {
760 MachineBasicBlock::const_instr_iterator I = MI.getIterator();
761 MachineBasicBlock::const_instr_iterator E = MI.getParent()->instr_end();
762 while (++I != E && I->isInsideBundle()) {
763 assert(!I->isBundle() && "No nested bundle!");
764 Size += getInstSizeInBytes(*I);
769 void ARMBaseInstrInfo::copyFromCPSR(MachineBasicBlock &MBB,
770 MachineBasicBlock::iterator I,
771 unsigned DestReg, bool KillSrc,
772 const ARMSubtarget &Subtarget) const {
773 unsigned Opc = Subtarget.isThumb()
774 ? (Subtarget.isMClass() ? ARM::t2MRS_M : ARM::t2MRS_AR)
777 MachineInstrBuilder MIB =
778 BuildMI(MBB, I, I->getDebugLoc(), get(Opc), DestReg);
780 // There is only 1 A/R class MRS instruction, and it always refers to
781 // APSR. However, there are lots of other possibilities on M-class cores.
782 if (Subtarget.isMClass())
785 MIB.add(predOps(ARMCC::AL))
786 .addReg(ARM::CPSR, RegState::Implicit | getKillRegState(KillSrc));
789 void ARMBaseInstrInfo::copyToCPSR(MachineBasicBlock &MBB,
790 MachineBasicBlock::iterator I,
791 unsigned SrcReg, bool KillSrc,
792 const ARMSubtarget &Subtarget) const {
793 unsigned Opc = Subtarget.isThumb()
794 ? (Subtarget.isMClass() ? ARM::t2MSR_M : ARM::t2MSR_AR)
797 MachineInstrBuilder MIB = BuildMI(MBB, I, I->getDebugLoc(), get(Opc));
799 if (Subtarget.isMClass())
804 MIB.addReg(SrcReg, getKillRegState(KillSrc))
805 .add(predOps(ARMCC::AL))
806 .addReg(ARM::CPSR, RegState::Implicit | RegState::Define);
809 void ARMBaseInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
810 MachineBasicBlock::iterator I,
811 const DebugLoc &DL, unsigned DestReg,
812 unsigned SrcReg, bool KillSrc) const {
813 bool GPRDest = ARM::GPRRegClass.contains(DestReg);
814 bool GPRSrc = ARM::GPRRegClass.contains(SrcReg);
816 if (GPRDest && GPRSrc) {
817 BuildMI(MBB, I, DL, get(ARM::MOVr), DestReg)
818 .addReg(SrcReg, getKillRegState(KillSrc))
819 .add(predOps(ARMCC::AL))
824 bool SPRDest = ARM::SPRRegClass.contains(DestReg);
825 bool SPRSrc = ARM::SPRRegClass.contains(SrcReg);
828 if (SPRDest && SPRSrc)
830 else if (GPRDest && SPRSrc)
832 else if (SPRDest && GPRSrc)
834 else if (ARM::DPRRegClass.contains(DestReg, SrcReg) && !Subtarget.isFPOnlySP())
836 else if (ARM::QPRRegClass.contains(DestReg, SrcReg))
840 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(Opc), DestReg);
841 MIB.addReg(SrcReg, getKillRegState(KillSrc));
842 if (Opc == ARM::VORRq)
843 MIB.addReg(SrcReg, getKillRegState(KillSrc));
844 MIB.add(predOps(ARMCC::AL));
848 // Handle register classes that require multiple instructions.
849 unsigned BeginIdx = 0;
850 unsigned SubRegs = 0;
853 // Use VORRq when possible.
854 if (ARM::QQPRRegClass.contains(DestReg, SrcReg)) {
856 BeginIdx = ARM::qsub_0;
858 } else if (ARM::QQQQPRRegClass.contains(DestReg, SrcReg)) {
860 BeginIdx = ARM::qsub_0;
862 // Fall back to VMOVD.
863 } else if (ARM::DPairRegClass.contains(DestReg, SrcReg)) {
865 BeginIdx = ARM::dsub_0;
867 } else if (ARM::DTripleRegClass.contains(DestReg, SrcReg)) {
869 BeginIdx = ARM::dsub_0;
871 } else if (ARM::DQuadRegClass.contains(DestReg, SrcReg)) {
873 BeginIdx = ARM::dsub_0;
875 } else if (ARM::GPRPairRegClass.contains(DestReg, SrcReg)) {
876 Opc = Subtarget.isThumb2() ? ARM::tMOVr : ARM::MOVr;
877 BeginIdx = ARM::gsub_0;
879 } else if (ARM::DPairSpcRegClass.contains(DestReg, SrcReg)) {
881 BeginIdx = ARM::dsub_0;
884 } else if (ARM::DTripleSpcRegClass.contains(DestReg, SrcReg)) {
886 BeginIdx = ARM::dsub_0;
889 } else if (ARM::DQuadSpcRegClass.contains(DestReg, SrcReg)) {
891 BeginIdx = ARM::dsub_0;
894 } else if (ARM::DPRRegClass.contains(DestReg, SrcReg) && Subtarget.isFPOnlySP()) {
896 BeginIdx = ARM::ssub_0;
898 } else if (SrcReg == ARM::CPSR) {
899 copyFromCPSR(MBB, I, DestReg, KillSrc, Subtarget);
901 } else if (DestReg == ARM::CPSR) {
902 copyToCPSR(MBB, I, SrcReg, KillSrc, Subtarget);
906 assert(Opc && "Impossible reg-to-reg copy");
908 const TargetRegisterInfo *TRI = &getRegisterInfo();
909 MachineInstrBuilder Mov;
911 // Copy register tuples backward when the first Dest reg overlaps with SrcReg.
912 if (TRI->regsOverlap(SrcReg, TRI->getSubReg(DestReg, BeginIdx))) {
913 BeginIdx = BeginIdx + ((SubRegs - 1) * Spacing);
917 SmallSet<unsigned, 4> DstRegs;
919 for (unsigned i = 0; i != SubRegs; ++i) {
920 unsigned Dst = TRI->getSubReg(DestReg, BeginIdx + i * Spacing);
921 unsigned Src = TRI->getSubReg(SrcReg, BeginIdx + i * Spacing);
922 assert(Dst && Src && "Bad sub-register");
924 assert(!DstRegs.count(Src) && "destructive vector copy");
927 Mov = BuildMI(MBB, I, I->getDebugLoc(), get(Opc), Dst).addReg(Src);
928 // VORR takes two source operands.
929 if (Opc == ARM::VORRq)
931 Mov = Mov.add(predOps(ARMCC::AL));
933 if (Opc == ARM::MOVr)
934 Mov = Mov.add(condCodeOp());
936 // Add implicit super-register defs and kills to the last instruction.
937 Mov->addRegisterDefined(DestReg, TRI);
939 Mov->addRegisterKilled(SrcReg, TRI);
942 bool ARMBaseInstrInfo::isCopyInstrImpl(const MachineInstr &MI,
943 const MachineOperand *&Src,
944 const MachineOperand *&Dest) const {
945 // VMOVRRD is also a copy instruction but it requires
946 // special way of handling. It is more complex copy version
947 // and since that we are not considering it. For recognition
948 // of such instruction isExtractSubregLike MI interface fuction
950 // VORRq is considered as a move only if two inputs are
951 // the same register.
952 if (!MI.isMoveReg() ||
953 (MI.getOpcode() == ARM::VORRq &&
954 MI.getOperand(1).getReg() != MI.getOperand(2).getReg()))
956 Dest = &MI.getOperand(0);
957 Src = &MI.getOperand(1);
961 const MachineInstrBuilder &
962 ARMBaseInstrInfo::AddDReg(MachineInstrBuilder &MIB, unsigned Reg,
963 unsigned SubIdx, unsigned State,
964 const TargetRegisterInfo *TRI) const {
966 return MIB.addReg(Reg, State);
968 if (TargetRegisterInfo::isPhysicalRegister(Reg))
969 return MIB.addReg(TRI->getSubReg(Reg, SubIdx), State);
970 return MIB.addReg(Reg, State, SubIdx);
973 void ARMBaseInstrInfo::
974 storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
975 unsigned SrcReg, bool isKill, int FI,
976 const TargetRegisterClass *RC,
977 const TargetRegisterInfo *TRI) const {
978 MachineFunction &MF = *MBB.getParent();
979 MachineFrameInfo &MFI = MF.getFrameInfo();
980 unsigned Align = MFI.getObjectAlignment(FI);
982 MachineMemOperand *MMO = MF.getMachineMemOperand(
983 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOStore,
984 MFI.getObjectSize(FI), Align);
986 switch (TRI->getSpillSize(*RC)) {
988 if (ARM::HPRRegClass.hasSubClassEq(RC)) {
989 BuildMI(MBB, I, DebugLoc(), get(ARM::VSTRH))
990 .addReg(SrcReg, getKillRegState(isKill))
994 .add(predOps(ARMCC::AL));
996 llvm_unreachable("Unknown reg class!");
999 if (ARM::GPRRegClass.hasSubClassEq(RC)) {
1000 BuildMI(MBB, I, DebugLoc(), get(ARM::STRi12))
1001 .addReg(SrcReg, getKillRegState(isKill))
1005 .add(predOps(ARMCC::AL));
1006 } else if (ARM::SPRRegClass.hasSubClassEq(RC)) {
1007 BuildMI(MBB, I, DebugLoc(), get(ARM::VSTRS))
1008 .addReg(SrcReg, getKillRegState(isKill))
1012 .add(predOps(ARMCC::AL));
1014 llvm_unreachable("Unknown reg class!");
1017 if (ARM::DPRRegClass.hasSubClassEq(RC)) {
1018 BuildMI(MBB, I, DebugLoc(), get(ARM::VSTRD))
1019 .addReg(SrcReg, getKillRegState(isKill))
1023 .add(predOps(ARMCC::AL));
1024 } else if (ARM::GPRPairRegClass.hasSubClassEq(RC)) {
1025 if (Subtarget.hasV5TEOps()) {
1026 MachineInstrBuilder MIB = BuildMI(MBB, I, DebugLoc(), get(ARM::STRD));
1027 AddDReg(MIB, SrcReg, ARM::gsub_0, getKillRegState(isKill), TRI);
1028 AddDReg(MIB, SrcReg, ARM::gsub_1, 0, TRI);
1029 MIB.addFrameIndex(FI).addReg(0).addImm(0).addMemOperand(MMO)
1030 .add(predOps(ARMCC::AL));
1032 // Fallback to STM instruction, which has existed since the dawn of
1034 MachineInstrBuilder MIB = BuildMI(MBB, I, DebugLoc(), get(ARM::STMIA))
1037 .add(predOps(ARMCC::AL));
1038 AddDReg(MIB, SrcReg, ARM::gsub_0, getKillRegState(isKill), TRI);
1039 AddDReg(MIB, SrcReg, ARM::gsub_1, 0, TRI);
1042 llvm_unreachable("Unknown reg class!");
1045 if (ARM::DPairRegClass.hasSubClassEq(RC)) {
1046 // Use aligned spills if the stack can be realigned.
1047 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
1048 BuildMI(MBB, I, DebugLoc(), get(ARM::VST1q64))
1051 .addReg(SrcReg, getKillRegState(isKill))
1053 .add(predOps(ARMCC::AL));
1055 BuildMI(MBB, I, DebugLoc(), get(ARM::VSTMQIA))
1056 .addReg(SrcReg, getKillRegState(isKill))
1059 .add(predOps(ARMCC::AL));
1062 llvm_unreachable("Unknown reg class!");
1065 if (ARM::DTripleRegClass.hasSubClassEq(RC)) {
1066 // Use aligned spills if the stack can be realigned.
1067 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
1068 BuildMI(MBB, I, DebugLoc(), get(ARM::VST1d64TPseudo))
1071 .addReg(SrcReg, getKillRegState(isKill))
1073 .add(predOps(ARMCC::AL));
1075 MachineInstrBuilder MIB = BuildMI(MBB, I, DebugLoc(),
1078 .add(predOps(ARMCC::AL))
1079 .addMemOperand(MMO);
1080 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
1081 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
1082 AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
1085 llvm_unreachable("Unknown reg class!");
1088 if (ARM::QQPRRegClass.hasSubClassEq(RC) || ARM::DQuadRegClass.hasSubClassEq(RC)) {
1089 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
1090 // FIXME: It's possible to only store part of the QQ register if the
1091 // spilled def has a sub-register index.
1092 BuildMI(MBB, I, DebugLoc(), get(ARM::VST1d64QPseudo))
1095 .addReg(SrcReg, getKillRegState(isKill))
1097 .add(predOps(ARMCC::AL));
1099 MachineInstrBuilder MIB = BuildMI(MBB, I, DebugLoc(),
1102 .add(predOps(ARMCC::AL))
1103 .addMemOperand(MMO);
1104 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
1105 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
1106 MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
1107 AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI);
1110 llvm_unreachable("Unknown reg class!");
1113 if (ARM::QQQQPRRegClass.hasSubClassEq(RC)) {
1114 MachineInstrBuilder MIB = BuildMI(MBB, I, DebugLoc(), get(ARM::VSTMDIA))
1116 .add(predOps(ARMCC::AL))
1117 .addMemOperand(MMO);
1118 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
1119 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
1120 MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
1121 MIB = AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI);
1122 MIB = AddDReg(MIB, SrcReg, ARM::dsub_4, 0, TRI);
1123 MIB = AddDReg(MIB, SrcReg, ARM::dsub_5, 0, TRI);
1124 MIB = AddDReg(MIB, SrcReg, ARM::dsub_6, 0, TRI);
1125 AddDReg(MIB, SrcReg, ARM::dsub_7, 0, TRI);
1127 llvm_unreachable("Unknown reg class!");
1130 llvm_unreachable("Unknown reg class!");
1134 unsigned ARMBaseInstrInfo::isStoreToStackSlot(const MachineInstr &MI,
1135 int &FrameIndex) const {
1136 switch (MI.getOpcode()) {
1139 case ARM::t2STRs: // FIXME: don't use t2STRs to access frame.
1140 if (MI.getOperand(1).isFI() && MI.getOperand(2).isReg() &&
1141 MI.getOperand(3).isImm() && MI.getOperand(2).getReg() == 0 &&
1142 MI.getOperand(3).getImm() == 0) {
1143 FrameIndex = MI.getOperand(1).getIndex();
1144 return MI.getOperand(0).getReg();
1152 if (MI.getOperand(1).isFI() && MI.getOperand(2).isImm() &&
1153 MI.getOperand(2).getImm() == 0) {
1154 FrameIndex = MI.getOperand(1).getIndex();
1155 return MI.getOperand(0).getReg();
1159 case ARM::VST1d64TPseudo:
1160 case ARM::VST1d64QPseudo:
1161 if (MI.getOperand(0).isFI() && MI.getOperand(2).getSubReg() == 0) {
1162 FrameIndex = MI.getOperand(0).getIndex();
1163 return MI.getOperand(2).getReg();
1167 if (MI.getOperand(1).isFI() && MI.getOperand(0).getSubReg() == 0) {
1168 FrameIndex = MI.getOperand(1).getIndex();
1169 return MI.getOperand(0).getReg();
1177 unsigned ARMBaseInstrInfo::isStoreToStackSlotPostFE(const MachineInstr &MI,
1178 int &FrameIndex) const {
1179 SmallVector<const MachineMemOperand *, 1> Accesses;
1180 if (MI.mayStore() && hasStoreToStackSlot(MI, Accesses)) {
1182 cast<FixedStackPseudoSourceValue>(Accesses.front()->getPseudoValue())
1189 void ARMBaseInstrInfo::
1190 loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
1191 unsigned DestReg, int FI,
1192 const TargetRegisterClass *RC,
1193 const TargetRegisterInfo *TRI) const {
1195 if (I != MBB.end()) DL = I->getDebugLoc();
1196 MachineFunction &MF = *MBB.getParent();
1197 MachineFrameInfo &MFI = MF.getFrameInfo();
1198 unsigned Align = MFI.getObjectAlignment(FI);
1199 MachineMemOperand *MMO = MF.getMachineMemOperand(
1200 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOLoad,
1201 MFI.getObjectSize(FI), Align);
1203 switch (TRI->getSpillSize(*RC)) {
1205 if (ARM::HPRRegClass.hasSubClassEq(RC)) {
1206 BuildMI(MBB, I, DL, get(ARM::VLDRH), DestReg)
1210 .add(predOps(ARMCC::AL));
1212 llvm_unreachable("Unknown reg class!");
1215 if (ARM::GPRRegClass.hasSubClassEq(RC)) {
1216 BuildMI(MBB, I, DL, get(ARM::LDRi12), DestReg)
1220 .add(predOps(ARMCC::AL));
1221 } else if (ARM::SPRRegClass.hasSubClassEq(RC)) {
1222 BuildMI(MBB, I, DL, get(ARM::VLDRS), DestReg)
1226 .add(predOps(ARMCC::AL));
1228 llvm_unreachable("Unknown reg class!");
1231 if (ARM::DPRRegClass.hasSubClassEq(RC)) {
1232 BuildMI(MBB, I, DL, get(ARM::VLDRD), DestReg)
1236 .add(predOps(ARMCC::AL));
1237 } else if (ARM::GPRPairRegClass.hasSubClassEq(RC)) {
1238 MachineInstrBuilder MIB;
1240 if (Subtarget.hasV5TEOps()) {
1241 MIB = BuildMI(MBB, I, DL, get(ARM::LDRD));
1242 AddDReg(MIB, DestReg, ARM::gsub_0, RegState::DefineNoRead, TRI);
1243 AddDReg(MIB, DestReg, ARM::gsub_1, RegState::DefineNoRead, TRI);
1244 MIB.addFrameIndex(FI).addReg(0).addImm(0).addMemOperand(MMO)
1245 .add(predOps(ARMCC::AL));
1247 // Fallback to LDM instruction, which has existed since the dawn of
1249 MIB = BuildMI(MBB, I, DL, get(ARM::LDMIA))
1252 .add(predOps(ARMCC::AL));
1253 MIB = AddDReg(MIB, DestReg, ARM::gsub_0, RegState::DefineNoRead, TRI);
1254 MIB = AddDReg(MIB, DestReg, ARM::gsub_1, RegState::DefineNoRead, TRI);
1257 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1258 MIB.addReg(DestReg, RegState::ImplicitDefine);
1260 llvm_unreachable("Unknown reg class!");
1263 if (ARM::DPairRegClass.hasSubClassEq(RC)) {
1264 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
1265 BuildMI(MBB, I, DL, get(ARM::VLD1q64), DestReg)
1269 .add(predOps(ARMCC::AL));
1271 BuildMI(MBB, I, DL, get(ARM::VLDMQIA), DestReg)
1274 .add(predOps(ARMCC::AL));
1277 llvm_unreachable("Unknown reg class!");
1280 if (ARM::DTripleRegClass.hasSubClassEq(RC)) {
1281 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
1282 BuildMI(MBB, I, DL, get(ARM::VLD1d64TPseudo), DestReg)
1286 .add(predOps(ARMCC::AL));
1288 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
1291 .add(predOps(ARMCC::AL));
1292 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::DefineNoRead, TRI);
1293 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::DefineNoRead, TRI);
1294 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::DefineNoRead, TRI);
1295 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1296 MIB.addReg(DestReg, RegState::ImplicitDefine);
1299 llvm_unreachable("Unknown reg class!");
1302 if (ARM::QQPRRegClass.hasSubClassEq(RC) || ARM::DQuadRegClass.hasSubClassEq(RC)) {
1303 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
1304 BuildMI(MBB, I, DL, get(ARM::VLD1d64QPseudo), DestReg)
1308 .add(predOps(ARMCC::AL));
1310 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
1312 .add(predOps(ARMCC::AL))
1313 .addMemOperand(MMO);
1314 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::DefineNoRead, TRI);
1315 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::DefineNoRead, TRI);
1316 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::DefineNoRead, TRI);
1317 MIB = AddDReg(MIB, DestReg, ARM::dsub_3, RegState::DefineNoRead, TRI);
1318 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1319 MIB.addReg(DestReg, RegState::ImplicitDefine);
1322 llvm_unreachable("Unknown reg class!");
1325 if (ARM::QQQQPRRegClass.hasSubClassEq(RC)) {
1326 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
1328 .add(predOps(ARMCC::AL))
1329 .addMemOperand(MMO);
1330 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::DefineNoRead, TRI);
1331 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::DefineNoRead, TRI);
1332 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::DefineNoRead, TRI);
1333 MIB = AddDReg(MIB, DestReg, ARM::dsub_3, RegState::DefineNoRead, TRI);
1334 MIB = AddDReg(MIB, DestReg, ARM::dsub_4, RegState::DefineNoRead, TRI);
1335 MIB = AddDReg(MIB, DestReg, ARM::dsub_5, RegState::DefineNoRead, TRI);
1336 MIB = AddDReg(MIB, DestReg, ARM::dsub_6, RegState::DefineNoRead, TRI);
1337 MIB = AddDReg(MIB, DestReg, ARM::dsub_7, RegState::DefineNoRead, TRI);
1338 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1339 MIB.addReg(DestReg, RegState::ImplicitDefine);
1341 llvm_unreachable("Unknown reg class!");
1344 llvm_unreachable("Unknown regclass!");
1348 unsigned ARMBaseInstrInfo::isLoadFromStackSlot(const MachineInstr &MI,
1349 int &FrameIndex) const {
1350 switch (MI.getOpcode()) {
1353 case ARM::t2LDRs: // FIXME: don't use t2LDRs to access frame.
1354 if (MI.getOperand(1).isFI() && MI.getOperand(2).isReg() &&
1355 MI.getOperand(3).isImm() && MI.getOperand(2).getReg() == 0 &&
1356 MI.getOperand(3).getImm() == 0) {
1357 FrameIndex = MI.getOperand(1).getIndex();
1358 return MI.getOperand(0).getReg();
1366 if (MI.getOperand(1).isFI() && MI.getOperand(2).isImm() &&
1367 MI.getOperand(2).getImm() == 0) {
1368 FrameIndex = MI.getOperand(1).getIndex();
1369 return MI.getOperand(0).getReg();
1373 case ARM::VLD1d8TPseudo:
1374 case ARM::VLD1d16TPseudo:
1375 case ARM::VLD1d32TPseudo:
1376 case ARM::VLD1d64TPseudo:
1377 case ARM::VLD1d8QPseudo:
1378 case ARM::VLD1d16QPseudo:
1379 case ARM::VLD1d32QPseudo:
1380 case ARM::VLD1d64QPseudo:
1381 if (MI.getOperand(1).isFI() && MI.getOperand(0).getSubReg() == 0) {
1382 FrameIndex = MI.getOperand(1).getIndex();
1383 return MI.getOperand(0).getReg();
1387 if (MI.getOperand(1).isFI() && MI.getOperand(0).getSubReg() == 0) {
1388 FrameIndex = MI.getOperand(1).getIndex();
1389 return MI.getOperand(0).getReg();
1397 unsigned ARMBaseInstrInfo::isLoadFromStackSlotPostFE(const MachineInstr &MI,
1398 int &FrameIndex) const {
1399 SmallVector<const MachineMemOperand *, 1> Accesses;
1400 if (MI.mayLoad() && hasLoadFromStackSlot(MI, Accesses)) {
1402 cast<FixedStackPseudoSourceValue>(Accesses.front()->getPseudoValue())
1409 /// Expands MEMCPY to either LDMIA/STMIA or LDMIA_UPD/STMID_UPD
1410 /// depending on whether the result is used.
1411 void ARMBaseInstrInfo::expandMEMCPY(MachineBasicBlock::iterator MI) const {
1412 bool isThumb1 = Subtarget.isThumb1Only();
1413 bool isThumb2 = Subtarget.isThumb2();
1414 const ARMBaseInstrInfo *TII = Subtarget.getInstrInfo();
1416 DebugLoc dl = MI->getDebugLoc();
1417 MachineBasicBlock *BB = MI->getParent();
1419 MachineInstrBuilder LDM, STM;
1420 if (isThumb1 || !MI->getOperand(1).isDead()) {
1421 MachineOperand LDWb(MI->getOperand(1));
1422 LDM = BuildMI(*BB, MI, dl, TII->get(isThumb2 ? ARM::t2LDMIA_UPD
1423 : isThumb1 ? ARM::tLDMIA_UPD
1427 LDM = BuildMI(*BB, MI, dl, TII->get(isThumb2 ? ARM::t2LDMIA : ARM::LDMIA));
1430 if (isThumb1 || !MI->getOperand(0).isDead()) {
1431 MachineOperand STWb(MI->getOperand(0));
1432 STM = BuildMI(*BB, MI, dl, TII->get(isThumb2 ? ARM::t2STMIA_UPD
1433 : isThumb1 ? ARM::tSTMIA_UPD
1437 STM = BuildMI(*BB, MI, dl, TII->get(isThumb2 ? ARM::t2STMIA : ARM::STMIA));
1440 MachineOperand LDBase(MI->getOperand(3));
1441 LDM.add(LDBase).add(predOps(ARMCC::AL));
1443 MachineOperand STBase(MI->getOperand(2));
1444 STM.add(STBase).add(predOps(ARMCC::AL));
1446 // Sort the scratch registers into ascending order.
1447 const TargetRegisterInfo &TRI = getRegisterInfo();
1448 SmallVector<unsigned, 6> ScratchRegs;
1449 for(unsigned I = 5; I < MI->getNumOperands(); ++I)
1450 ScratchRegs.push_back(MI->getOperand(I).getReg());
1451 llvm::sort(ScratchRegs,
1452 [&TRI](const unsigned &Reg1, const unsigned &Reg2) -> bool {
1453 return TRI.getEncodingValue(Reg1) <
1454 TRI.getEncodingValue(Reg2);
1457 for (const auto &Reg : ScratchRegs) {
1458 LDM.addReg(Reg, RegState::Define);
1459 STM.addReg(Reg, RegState::Kill);
1465 bool ARMBaseInstrInfo::expandPostRAPseudo(MachineInstr &MI) const {
1466 if (MI.getOpcode() == TargetOpcode::LOAD_STACK_GUARD) {
1467 assert(getSubtarget().getTargetTriple().isOSBinFormatMachO() &&
1468 "LOAD_STACK_GUARD currently supported only for MachO.");
1469 expandLoadStackGuard(MI);
1470 MI.getParent()->erase(MI);
1474 if (MI.getOpcode() == ARM::MEMCPY) {
1479 // This hook gets to expand COPY instructions before they become
1480 // copyPhysReg() calls. Look for VMOVS instructions that can legally be
1481 // widened to VMOVD. We prefer the VMOVD when possible because it may be
1482 // changed into a VORR that can go down the NEON pipeline.
1483 if (!MI.isCopy() || Subtarget.dontWidenVMOVS() || Subtarget.isFPOnlySP())
1486 // Look for a copy between even S-registers. That is where we keep floats
1487 // when using NEON v2f32 instructions for f32 arithmetic.
1488 unsigned DstRegS = MI.getOperand(0).getReg();
1489 unsigned SrcRegS = MI.getOperand(1).getReg();
1490 if (!ARM::SPRRegClass.contains(DstRegS, SrcRegS))
1493 const TargetRegisterInfo *TRI = &getRegisterInfo();
1494 unsigned DstRegD = TRI->getMatchingSuperReg(DstRegS, ARM::ssub_0,
1496 unsigned SrcRegD = TRI->getMatchingSuperReg(SrcRegS, ARM::ssub_0,
1498 if (!DstRegD || !SrcRegD)
1501 // We want to widen this into a DstRegD = VMOVD SrcRegD copy. This is only
1502 // legal if the COPY already defines the full DstRegD, and it isn't a
1503 // sub-register insertion.
1504 if (!MI.definesRegister(DstRegD, TRI) || MI.readsRegister(DstRegD, TRI))
1507 // A dead copy shouldn't show up here, but reject it just in case.
1508 if (MI.getOperand(0).isDead())
1511 // All clear, widen the COPY.
1512 LLVM_DEBUG(dbgs() << "widening: " << MI);
1513 MachineInstrBuilder MIB(*MI.getParent()->getParent(), MI);
1515 // Get rid of the old implicit-def of DstRegD. Leave it if it defines a Q-reg
1516 // or some other super-register.
1517 int ImpDefIdx = MI.findRegisterDefOperandIdx(DstRegD);
1518 if (ImpDefIdx != -1)
1519 MI.RemoveOperand(ImpDefIdx);
1521 // Change the opcode and operands.
1522 MI.setDesc(get(ARM::VMOVD));
1523 MI.getOperand(0).setReg(DstRegD);
1524 MI.getOperand(1).setReg(SrcRegD);
1525 MIB.add(predOps(ARMCC::AL));
1527 // We are now reading SrcRegD instead of SrcRegS. This may upset the
1528 // register scavenger and machine verifier, so we need to indicate that we
1529 // are reading an undefined value from SrcRegD, but a proper value from
1531 MI.getOperand(1).setIsUndef();
1532 MIB.addReg(SrcRegS, RegState::Implicit);
1534 // SrcRegD may actually contain an unrelated value in the ssub_1
1535 // sub-register. Don't kill it. Only kill the ssub_0 sub-register.
1536 if (MI.getOperand(1).isKill()) {
1537 MI.getOperand(1).setIsKill(false);
1538 MI.addRegisterKilled(SrcRegS, TRI, true);
1541 LLVM_DEBUG(dbgs() << "replaced by: " << MI);
1545 /// Create a copy of a const pool value. Update CPI to the new index and return
1547 static unsigned duplicateCPV(MachineFunction &MF, unsigned &CPI) {
1548 MachineConstantPool *MCP = MF.getConstantPool();
1549 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1551 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPI];
1552 assert(MCPE.isMachineConstantPoolEntry() &&
1553 "Expecting a machine constantpool entry!");
1554 ARMConstantPoolValue *ACPV =
1555 static_cast<ARMConstantPoolValue*>(MCPE.Val.MachineCPVal);
1557 unsigned PCLabelId = AFI->createPICLabelUId();
1558 ARMConstantPoolValue *NewCPV = nullptr;
1560 // FIXME: The below assumes PIC relocation model and that the function
1561 // is Thumb mode (t1 or t2). PCAdjustment would be 8 for ARM mode PIC, and
1562 // zero for non-PIC in ARM or Thumb. The callers are all of thumb LDR
1563 // instructions, so that's probably OK, but is PIC always correct when
1565 if (ACPV->isGlobalValue())
1566 NewCPV = ARMConstantPoolConstant::Create(
1567 cast<ARMConstantPoolConstant>(ACPV)->getGV(), PCLabelId, ARMCP::CPValue,
1568 4, ACPV->getModifier(), ACPV->mustAddCurrentAddress());
1569 else if (ACPV->isExtSymbol())
1570 NewCPV = ARMConstantPoolSymbol::
1571 Create(MF.getFunction().getContext(),
1572 cast<ARMConstantPoolSymbol>(ACPV)->getSymbol(), PCLabelId, 4);
1573 else if (ACPV->isBlockAddress())
1574 NewCPV = ARMConstantPoolConstant::
1575 Create(cast<ARMConstantPoolConstant>(ACPV)->getBlockAddress(), PCLabelId,
1576 ARMCP::CPBlockAddress, 4);
1577 else if (ACPV->isLSDA())
1578 NewCPV = ARMConstantPoolConstant::Create(&MF.getFunction(), PCLabelId,
1580 else if (ACPV->isMachineBasicBlock())
1581 NewCPV = ARMConstantPoolMBB::
1582 Create(MF.getFunction().getContext(),
1583 cast<ARMConstantPoolMBB>(ACPV)->getMBB(), PCLabelId, 4);
1585 llvm_unreachable("Unexpected ARM constantpool value type!!");
1586 CPI = MCP->getConstantPoolIndex(NewCPV, MCPE.getAlignment());
1590 void ARMBaseInstrInfo::reMaterialize(MachineBasicBlock &MBB,
1591 MachineBasicBlock::iterator I,
1592 unsigned DestReg, unsigned SubIdx,
1593 const MachineInstr &Orig,
1594 const TargetRegisterInfo &TRI) const {
1595 unsigned Opcode = Orig.getOpcode();
1598 MachineInstr *MI = MBB.getParent()->CloneMachineInstr(&Orig);
1599 MI->substituteRegister(Orig.getOperand(0).getReg(), DestReg, SubIdx, TRI);
1603 case ARM::tLDRpci_pic:
1604 case ARM::t2LDRpci_pic: {
1605 MachineFunction &MF = *MBB.getParent();
1606 unsigned CPI = Orig.getOperand(1).getIndex();
1607 unsigned PCLabelId = duplicateCPV(MF, CPI);
1608 BuildMI(MBB, I, Orig.getDebugLoc(), get(Opcode), DestReg)
1609 .addConstantPoolIndex(CPI)
1611 .cloneMemRefs(Orig);
1618 ARMBaseInstrInfo::duplicate(MachineBasicBlock &MBB,
1619 MachineBasicBlock::iterator InsertBefore,
1620 const MachineInstr &Orig) const {
1621 MachineInstr &Cloned = TargetInstrInfo::duplicate(MBB, InsertBefore, Orig);
1622 MachineBasicBlock::instr_iterator I = Cloned.getIterator();
1624 switch (I->getOpcode()) {
1625 case ARM::tLDRpci_pic:
1626 case ARM::t2LDRpci_pic: {
1627 MachineFunction &MF = *MBB.getParent();
1628 unsigned CPI = I->getOperand(1).getIndex();
1629 unsigned PCLabelId = duplicateCPV(MF, CPI);
1630 I->getOperand(1).setIndex(CPI);
1631 I->getOperand(2).setImm(PCLabelId);
1635 if (!I->isBundledWithSucc())
1642 bool ARMBaseInstrInfo::produceSameValue(const MachineInstr &MI0,
1643 const MachineInstr &MI1,
1644 const MachineRegisterInfo *MRI) const {
1645 unsigned Opcode = MI0.getOpcode();
1646 if (Opcode == ARM::t2LDRpci ||
1647 Opcode == ARM::t2LDRpci_pic ||
1648 Opcode == ARM::tLDRpci ||
1649 Opcode == ARM::tLDRpci_pic ||
1650 Opcode == ARM::LDRLIT_ga_pcrel ||
1651 Opcode == ARM::LDRLIT_ga_pcrel_ldr ||
1652 Opcode == ARM::tLDRLIT_ga_pcrel ||
1653 Opcode == ARM::MOV_ga_pcrel ||
1654 Opcode == ARM::MOV_ga_pcrel_ldr ||
1655 Opcode == ARM::t2MOV_ga_pcrel) {
1656 if (MI1.getOpcode() != Opcode)
1658 if (MI0.getNumOperands() != MI1.getNumOperands())
1661 const MachineOperand &MO0 = MI0.getOperand(1);
1662 const MachineOperand &MO1 = MI1.getOperand(1);
1663 if (MO0.getOffset() != MO1.getOffset())
1666 if (Opcode == ARM::LDRLIT_ga_pcrel ||
1667 Opcode == ARM::LDRLIT_ga_pcrel_ldr ||
1668 Opcode == ARM::tLDRLIT_ga_pcrel ||
1669 Opcode == ARM::MOV_ga_pcrel ||
1670 Opcode == ARM::MOV_ga_pcrel_ldr ||
1671 Opcode == ARM::t2MOV_ga_pcrel)
1672 // Ignore the PC labels.
1673 return MO0.getGlobal() == MO1.getGlobal();
1675 const MachineFunction *MF = MI0.getParent()->getParent();
1676 const MachineConstantPool *MCP = MF->getConstantPool();
1677 int CPI0 = MO0.getIndex();
1678 int CPI1 = MO1.getIndex();
1679 const MachineConstantPoolEntry &MCPE0 = MCP->getConstants()[CPI0];
1680 const MachineConstantPoolEntry &MCPE1 = MCP->getConstants()[CPI1];
1681 bool isARMCP0 = MCPE0.isMachineConstantPoolEntry();
1682 bool isARMCP1 = MCPE1.isMachineConstantPoolEntry();
1683 if (isARMCP0 && isARMCP1) {
1684 ARMConstantPoolValue *ACPV0 =
1685 static_cast<ARMConstantPoolValue*>(MCPE0.Val.MachineCPVal);
1686 ARMConstantPoolValue *ACPV1 =
1687 static_cast<ARMConstantPoolValue*>(MCPE1.Val.MachineCPVal);
1688 return ACPV0->hasSameValue(ACPV1);
1689 } else if (!isARMCP0 && !isARMCP1) {
1690 return MCPE0.Val.ConstVal == MCPE1.Val.ConstVal;
1693 } else if (Opcode == ARM::PICLDR) {
1694 if (MI1.getOpcode() != Opcode)
1696 if (MI0.getNumOperands() != MI1.getNumOperands())
1699 unsigned Addr0 = MI0.getOperand(1).getReg();
1700 unsigned Addr1 = MI1.getOperand(1).getReg();
1701 if (Addr0 != Addr1) {
1703 !TargetRegisterInfo::isVirtualRegister(Addr0) ||
1704 !TargetRegisterInfo::isVirtualRegister(Addr1))
1707 // This assumes SSA form.
1708 MachineInstr *Def0 = MRI->getVRegDef(Addr0);
1709 MachineInstr *Def1 = MRI->getVRegDef(Addr1);
1710 // Check if the loaded value, e.g. a constantpool of a global address, are
1712 if (!produceSameValue(*Def0, *Def1, MRI))
1716 for (unsigned i = 3, e = MI0.getNumOperands(); i != e; ++i) {
1717 // %12 = PICLDR %11, 0, 14, %noreg
1718 const MachineOperand &MO0 = MI0.getOperand(i);
1719 const MachineOperand &MO1 = MI1.getOperand(i);
1720 if (!MO0.isIdenticalTo(MO1))
1726 return MI0.isIdenticalTo(MI1, MachineInstr::IgnoreVRegDefs);
1729 /// areLoadsFromSameBasePtr - This is used by the pre-regalloc scheduler to
1730 /// determine if two loads are loading from the same base address. It should
1731 /// only return true if the base pointers are the same and the only differences
1732 /// between the two addresses is the offset. It also returns the offsets by
1735 /// FIXME: remove this in favor of the MachineInstr interface once pre-RA-sched
1736 /// is permanently disabled.
1737 bool ARMBaseInstrInfo::areLoadsFromSameBasePtr(SDNode *Load1, SDNode *Load2,
1739 int64_t &Offset2) const {
1740 // Don't worry about Thumb: just ARM and Thumb2.
1741 if (Subtarget.isThumb1Only()) return false;
1743 if (!Load1->isMachineOpcode() || !Load2->isMachineOpcode())
1746 switch (Load1->getMachineOpcode()) {
1760 case ARM::t2LDRSHi8:
1762 case ARM::t2LDRBi12:
1763 case ARM::t2LDRSHi12:
1767 switch (Load2->getMachineOpcode()) {
1780 case ARM::t2LDRSHi8:
1782 case ARM::t2LDRBi12:
1783 case ARM::t2LDRSHi12:
1787 // Check if base addresses and chain operands match.
1788 if (Load1->getOperand(0) != Load2->getOperand(0) ||
1789 Load1->getOperand(4) != Load2->getOperand(4))
1792 // Index should be Reg0.
1793 if (Load1->getOperand(3) != Load2->getOperand(3))
1796 // Determine the offsets.
1797 if (isa<ConstantSDNode>(Load1->getOperand(1)) &&
1798 isa<ConstantSDNode>(Load2->getOperand(1))) {
1799 Offset1 = cast<ConstantSDNode>(Load1->getOperand(1))->getSExtValue();
1800 Offset2 = cast<ConstantSDNode>(Load2->getOperand(1))->getSExtValue();
1807 /// shouldScheduleLoadsNear - This is a used by the pre-regalloc scheduler to
1808 /// determine (in conjunction with areLoadsFromSameBasePtr) if two loads should
1809 /// be scheduled togther. On some targets if two loads are loading from
1810 /// addresses in the same cache line, it's better if they are scheduled
1811 /// together. This function takes two integers that represent the load offsets
1812 /// from the common base address. It returns true if it decides it's desirable
1813 /// to schedule the two loads together. "NumLoads" is the number of loads that
1814 /// have already been scheduled after Load1.
1816 /// FIXME: remove this in favor of the MachineInstr interface once pre-RA-sched
1817 /// is permanently disabled.
1818 bool ARMBaseInstrInfo::shouldScheduleLoadsNear(SDNode *Load1, SDNode *Load2,
1819 int64_t Offset1, int64_t Offset2,
1820 unsigned NumLoads) const {
1821 // Don't worry about Thumb: just ARM and Thumb2.
1822 if (Subtarget.isThumb1Only()) return false;
1824 assert(Offset2 > Offset1);
1826 if ((Offset2 - Offset1) / 8 > 64)
1829 // Check if the machine opcodes are different. If they are different
1830 // then we consider them to not be of the same base address,
1831 // EXCEPT in the case of Thumb2 byte loads where one is LDRBi8 and the other LDRBi12.
1832 // In this case, they are considered to be the same because they are different
1833 // encoding forms of the same basic instruction.
1834 if ((Load1->getMachineOpcode() != Load2->getMachineOpcode()) &&
1835 !((Load1->getMachineOpcode() == ARM::t2LDRBi8 &&
1836 Load2->getMachineOpcode() == ARM::t2LDRBi12) ||
1837 (Load1->getMachineOpcode() == ARM::t2LDRBi12 &&
1838 Load2->getMachineOpcode() == ARM::t2LDRBi8)))
1839 return false; // FIXME: overly conservative?
1841 // Four loads in a row should be sufficient.
1848 bool ARMBaseInstrInfo::isSchedulingBoundary(const MachineInstr &MI,
1849 const MachineBasicBlock *MBB,
1850 const MachineFunction &MF) const {
1851 // Debug info is never a scheduling boundary. It's necessary to be explicit
1852 // due to the special treatment of IT instructions below, otherwise a
1853 // dbg_value followed by an IT will result in the IT instruction being
1854 // considered a scheduling hazard, which is wrong. It should be the actual
1855 // instruction preceding the dbg_value instruction(s), just like it is
1856 // when debug info is not present.
1857 if (MI.isDebugInstr())
1860 // Terminators and labels can't be scheduled around.
1861 if (MI.isTerminator() || MI.isPosition())
1864 // Treat the start of the IT block as a scheduling boundary, but schedule
1865 // t2IT along with all instructions following it.
1866 // FIXME: This is a big hammer. But the alternative is to add all potential
1867 // true and anti dependencies to IT block instructions as implicit operands
1868 // to the t2IT instruction. The added compile time and complexity does not
1870 MachineBasicBlock::const_iterator I = MI;
1871 // Make sure to skip any debug instructions
1872 while (++I != MBB->end() && I->isDebugInstr())
1874 if (I != MBB->end() && I->getOpcode() == ARM::t2IT)
1877 // Don't attempt to schedule around any instruction that defines
1878 // a stack-oriented pointer, as it's unlikely to be profitable. This
1879 // saves compile time, because it doesn't require every single
1880 // stack slot reference to depend on the instruction that does the
1882 // Calls don't actually change the stack pointer, even if they have imp-defs.
1883 // No ARM calling conventions change the stack pointer. (X86 calling
1884 // conventions sometimes do).
1885 if (!MI.isCall() && MI.definesRegister(ARM::SP))
1891 bool ARMBaseInstrInfo::
1892 isProfitableToIfCvt(MachineBasicBlock &MBB,
1893 unsigned NumCycles, unsigned ExtraPredCycles,
1894 BranchProbability Probability) const {
1898 // If we are optimizing for size, see if the branch in the predecessor can be
1899 // lowered to cbn?z by the constant island lowering pass, and return false if
1900 // so. This results in a shorter instruction sequence.
1901 if (MBB.getParent()->getFunction().optForSize()) {
1902 MachineBasicBlock *Pred = *MBB.pred_begin();
1903 if (!Pred->empty()) {
1904 MachineInstr *LastMI = &*Pred->rbegin();
1905 if (LastMI->getOpcode() == ARM::t2Bcc) {
1906 MachineBasicBlock::iterator CmpMI = LastMI;
1907 if (CmpMI != Pred->begin()) {
1909 if (CmpMI->getOpcode() == ARM::tCMPi8 ||
1910 CmpMI->getOpcode() == ARM::t2CMPri) {
1911 unsigned Reg = CmpMI->getOperand(0).getReg();
1912 unsigned PredReg = 0;
1913 ARMCC::CondCodes P = getInstrPredicate(*CmpMI, PredReg);
1914 if (P == ARMCC::AL && CmpMI->getOperand(1).getImm() == 0 &&
1915 isARMLowRegister(Reg))
1922 return isProfitableToIfCvt(MBB, NumCycles, ExtraPredCycles,
1923 MBB, 0, 0, Probability);
1926 bool ARMBaseInstrInfo::
1927 isProfitableToIfCvt(MachineBasicBlock &TBB,
1928 unsigned TCycles, unsigned TExtra,
1929 MachineBasicBlock &FBB,
1930 unsigned FCycles, unsigned FExtra,
1931 BranchProbability Probability) const {
1935 // Attempt to estimate the relative costs of predication versus branching.
1936 // Here we scale up each component of UnpredCost to avoid precision issue when
1937 // scaling TCycles/FCycles by Probability.
1938 const unsigned ScalingUpFactor = 1024;
1940 unsigned PredCost = (TCycles + FCycles + TExtra + FExtra) * ScalingUpFactor;
1941 unsigned UnpredCost;
1942 if (!Subtarget.hasBranchPredictor()) {
1943 // When we don't have a branch predictor it's always cheaper to not take a
1944 // branch than take it, so we have to take that into account.
1945 unsigned NotTakenBranchCost = 1;
1946 unsigned TakenBranchCost = Subtarget.getMispredictionPenalty();
1947 unsigned TUnpredCycles, FUnpredCycles;
1949 // Triangle: TBB is the fallthrough
1950 TUnpredCycles = TCycles + NotTakenBranchCost;
1951 FUnpredCycles = TakenBranchCost;
1953 // Diamond: TBB is the block that is branched to, FBB is the fallthrough
1954 TUnpredCycles = TCycles + TakenBranchCost;
1955 FUnpredCycles = FCycles + NotTakenBranchCost;
1956 // The branch at the end of FBB will disappear when it's predicated, so
1957 // discount it from PredCost.
1958 PredCost -= 1 * ScalingUpFactor;
1960 // The total cost is the cost of each path scaled by their probabilites
1961 unsigned TUnpredCost = Probability.scale(TUnpredCycles * ScalingUpFactor);
1962 unsigned FUnpredCost = Probability.getCompl().scale(FUnpredCycles * ScalingUpFactor);
1963 UnpredCost = TUnpredCost + FUnpredCost;
1964 // When predicating assume that the first IT can be folded away but later
1965 // ones cost one cycle each
1966 if (Subtarget.isThumb2() && TCycles + FCycles > 4) {
1967 PredCost += ((TCycles + FCycles - 4) / 4) * ScalingUpFactor;
1970 unsigned TUnpredCost = Probability.scale(TCycles * ScalingUpFactor);
1971 unsigned FUnpredCost =
1972 Probability.getCompl().scale(FCycles * ScalingUpFactor);
1973 UnpredCost = TUnpredCost + FUnpredCost;
1974 UnpredCost += 1 * ScalingUpFactor; // The branch itself
1975 UnpredCost += Subtarget.getMispredictionPenalty() * ScalingUpFactor / 10;
1978 return PredCost <= UnpredCost;
1982 ARMBaseInstrInfo::isProfitableToUnpredicate(MachineBasicBlock &TMBB,
1983 MachineBasicBlock &FMBB) const {
1984 // Reduce false anti-dependencies to let the target's out-of-order execution
1985 // engine do its thing.
1986 return Subtarget.isProfitableToUnpredicate();
1989 /// getInstrPredicate - If instruction is predicated, returns its predicate
1990 /// condition, otherwise returns AL. It also returns the condition code
1991 /// register by reference.
1992 ARMCC::CondCodes llvm::getInstrPredicate(const MachineInstr &MI,
1993 unsigned &PredReg) {
1994 int PIdx = MI.findFirstPredOperandIdx();
2000 PredReg = MI.getOperand(PIdx+1).getReg();
2001 return (ARMCC::CondCodes)MI.getOperand(PIdx).getImm();
2004 unsigned llvm::getMatchingCondBranchOpcode(unsigned Opc) {
2009 if (Opc == ARM::t2B)
2012 llvm_unreachable("Unknown unconditional branch opcode!");
2015 MachineInstr *ARMBaseInstrInfo::commuteInstructionImpl(MachineInstr &MI,
2018 unsigned OpIdx2) const {
2019 switch (MI.getOpcode()) {
2021 case ARM::t2MOVCCr: {
2022 // MOVCC can be commuted by inverting the condition.
2023 unsigned PredReg = 0;
2024 ARMCC::CondCodes CC = getInstrPredicate(MI, PredReg);
2025 // MOVCC AL can't be inverted. Shouldn't happen.
2026 if (CC == ARMCC::AL || PredReg != ARM::CPSR)
2028 MachineInstr *CommutedMI =
2029 TargetInstrInfo::commuteInstructionImpl(MI, NewMI, OpIdx1, OpIdx2);
2032 // After swapping the MOVCC operands, also invert the condition.
2033 CommutedMI->getOperand(CommutedMI->findFirstPredOperandIdx())
2034 .setImm(ARMCC::getOppositeCondition(CC));
2038 return TargetInstrInfo::commuteInstructionImpl(MI, NewMI, OpIdx1, OpIdx2);
2041 /// Identify instructions that can be folded into a MOVCC instruction, and
2042 /// return the defining instruction.
2043 static MachineInstr *canFoldIntoMOVCC(unsigned Reg,
2044 const MachineRegisterInfo &MRI,
2045 const TargetInstrInfo *TII) {
2046 if (!TargetRegisterInfo::isVirtualRegister(Reg))
2048 if (!MRI.hasOneNonDBGUse(Reg))
2050 MachineInstr *MI = MRI.getVRegDef(Reg);
2053 // MI is folded into the MOVCC by predicating it.
2054 if (!MI->isPredicable())
2056 // Check if MI has any non-dead defs or physreg uses. This also detects
2057 // predicated instructions which will be reading CPSR.
2058 for (unsigned i = 1, e = MI->getNumOperands(); i != e; ++i) {
2059 const MachineOperand &MO = MI->getOperand(i);
2060 // Reject frame index operands, PEI can't handle the predicated pseudos.
2061 if (MO.isFI() || MO.isCPI() || MO.isJTI())
2065 // MI can't have any tied operands, that would conflict with predication.
2068 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
2070 if (MO.isDef() && !MO.isDead())
2073 bool DontMoveAcrossStores = true;
2074 if (!MI->isSafeToMove(/* AliasAnalysis = */ nullptr, DontMoveAcrossStores))
2079 bool ARMBaseInstrInfo::analyzeSelect(const MachineInstr &MI,
2080 SmallVectorImpl<MachineOperand> &Cond,
2081 unsigned &TrueOp, unsigned &FalseOp,
2082 bool &Optimizable) const {
2083 assert((MI.getOpcode() == ARM::MOVCCr || MI.getOpcode() == ARM::t2MOVCCr) &&
2084 "Unknown select instruction");
2089 // 3: Condition code.
2093 Cond.push_back(MI.getOperand(3));
2094 Cond.push_back(MI.getOperand(4));
2095 // We can always fold a def.
2101 ARMBaseInstrInfo::optimizeSelect(MachineInstr &MI,
2102 SmallPtrSetImpl<MachineInstr *> &SeenMIs,
2103 bool PreferFalse) const {
2104 assert((MI.getOpcode() == ARM::MOVCCr || MI.getOpcode() == ARM::t2MOVCCr) &&
2105 "Unknown select instruction");
2106 MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
2107 MachineInstr *DefMI = canFoldIntoMOVCC(MI.getOperand(2).getReg(), MRI, this);
2108 bool Invert = !DefMI;
2110 DefMI = canFoldIntoMOVCC(MI.getOperand(1).getReg(), MRI, this);
2114 // Find new register class to use.
2115 MachineOperand FalseReg = MI.getOperand(Invert ? 2 : 1);
2116 unsigned DestReg = MI.getOperand(0).getReg();
2117 const TargetRegisterClass *PreviousClass = MRI.getRegClass(FalseReg.getReg());
2118 if (!MRI.constrainRegClass(DestReg, PreviousClass))
2121 // Create a new predicated version of DefMI.
2122 // Rfalse is the first use.
2123 MachineInstrBuilder NewMI =
2124 BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), DefMI->getDesc(), DestReg);
2126 // Copy all the DefMI operands, excluding its (null) predicate.
2127 const MCInstrDesc &DefDesc = DefMI->getDesc();
2128 for (unsigned i = 1, e = DefDesc.getNumOperands();
2129 i != e && !DefDesc.OpInfo[i].isPredicate(); ++i)
2130 NewMI.add(DefMI->getOperand(i));
2132 unsigned CondCode = MI.getOperand(3).getImm();
2134 NewMI.addImm(ARMCC::getOppositeCondition(ARMCC::CondCodes(CondCode)));
2136 NewMI.addImm(CondCode);
2137 NewMI.add(MI.getOperand(4));
2139 // DefMI is not the -S version that sets CPSR, so add an optional %noreg.
2140 if (NewMI->hasOptionalDef())
2141 NewMI.add(condCodeOp());
2143 // The output register value when the predicate is false is an implicit
2144 // register operand tied to the first def.
2145 // The tie makes the register allocator ensure the FalseReg is allocated the
2146 // same register as operand 0.
2147 FalseReg.setImplicit();
2148 NewMI.add(FalseReg);
2149 NewMI->tieOperands(0, NewMI->getNumOperands() - 1);
2151 // Update SeenMIs set: register newly created MI and erase removed DefMI.
2152 SeenMIs.insert(NewMI);
2153 SeenMIs.erase(DefMI);
2155 // If MI is inside a loop, and DefMI is outside the loop, then kill flags on
2156 // DefMI would be invalid when tranferred inside the loop. Checking for a
2157 // loop is expensive, but at least remove kill flags if they are in different
2159 if (DefMI->getParent() != MI.getParent())
2160 NewMI->clearKillInfo();
2162 // The caller will erase MI, but not DefMI.
2163 DefMI->eraseFromParent();
2167 /// Map pseudo instructions that imply an 'S' bit onto real opcodes. Whether the
2168 /// instruction is encoded with an 'S' bit is determined by the optional CPSR
2171 /// This will go away once we can teach tblgen how to set the optional CPSR def
2173 struct AddSubFlagsOpcodePair {
2175 uint16_t MachineOpc;
2178 static const AddSubFlagsOpcodePair AddSubFlagsOpcodeMap[] = {
2179 {ARM::ADDSri, ARM::ADDri},
2180 {ARM::ADDSrr, ARM::ADDrr},
2181 {ARM::ADDSrsi, ARM::ADDrsi},
2182 {ARM::ADDSrsr, ARM::ADDrsr},
2184 {ARM::SUBSri, ARM::SUBri},
2185 {ARM::SUBSrr, ARM::SUBrr},
2186 {ARM::SUBSrsi, ARM::SUBrsi},
2187 {ARM::SUBSrsr, ARM::SUBrsr},
2189 {ARM::RSBSri, ARM::RSBri},
2190 {ARM::RSBSrsi, ARM::RSBrsi},
2191 {ARM::RSBSrsr, ARM::RSBrsr},
2193 {ARM::tADDSi3, ARM::tADDi3},
2194 {ARM::tADDSi8, ARM::tADDi8},
2195 {ARM::tADDSrr, ARM::tADDrr},
2196 {ARM::tADCS, ARM::tADC},
2198 {ARM::tSUBSi3, ARM::tSUBi3},
2199 {ARM::tSUBSi8, ARM::tSUBi8},
2200 {ARM::tSUBSrr, ARM::tSUBrr},
2201 {ARM::tSBCS, ARM::tSBC},
2202 {ARM::tRSBS, ARM::tRSB},
2204 {ARM::t2ADDSri, ARM::t2ADDri},
2205 {ARM::t2ADDSrr, ARM::t2ADDrr},
2206 {ARM::t2ADDSrs, ARM::t2ADDrs},
2208 {ARM::t2SUBSri, ARM::t2SUBri},
2209 {ARM::t2SUBSrr, ARM::t2SUBrr},
2210 {ARM::t2SUBSrs, ARM::t2SUBrs},
2212 {ARM::t2RSBSri, ARM::t2RSBri},
2213 {ARM::t2RSBSrs, ARM::t2RSBrs},
2216 unsigned llvm::convertAddSubFlagsOpcode(unsigned OldOpc) {
2217 for (unsigned i = 0, e = array_lengthof(AddSubFlagsOpcodeMap); i != e; ++i)
2218 if (OldOpc == AddSubFlagsOpcodeMap[i].PseudoOpc)
2219 return AddSubFlagsOpcodeMap[i].MachineOpc;
2223 void llvm::emitARMRegPlusImmediate(MachineBasicBlock &MBB,
2224 MachineBasicBlock::iterator &MBBI,
2225 const DebugLoc &dl, unsigned DestReg,
2226 unsigned BaseReg, int NumBytes,
2227 ARMCC::CondCodes Pred, unsigned PredReg,
2228 const ARMBaseInstrInfo &TII,
2230 if (NumBytes == 0 && DestReg != BaseReg) {
2231 BuildMI(MBB, MBBI, dl, TII.get(ARM::MOVr), DestReg)
2232 .addReg(BaseReg, RegState::Kill)
2233 .add(predOps(Pred, PredReg))
2235 .setMIFlags(MIFlags);
2239 bool isSub = NumBytes < 0;
2240 if (isSub) NumBytes = -NumBytes;
2243 unsigned RotAmt = ARM_AM::getSOImmValRotate(NumBytes);
2244 unsigned ThisVal = NumBytes & ARM_AM::rotr32(0xFF, RotAmt);
2245 assert(ThisVal && "Didn't extract field correctly");
2247 // We will handle these bits from offset, clear them.
2248 NumBytes &= ~ThisVal;
2250 assert(ARM_AM::getSOImmVal(ThisVal) != -1 && "Bit extraction didn't work?");
2252 // Build the new ADD / SUB.
2253 unsigned Opc = isSub ? ARM::SUBri : ARM::ADDri;
2254 BuildMI(MBB, MBBI, dl, TII.get(Opc), DestReg)
2255 .addReg(BaseReg, RegState::Kill)
2257 .add(predOps(Pred, PredReg))
2259 .setMIFlags(MIFlags);
2264 bool llvm::tryFoldSPUpdateIntoPushPop(const ARMSubtarget &Subtarget,
2265 MachineFunction &MF, MachineInstr *MI,
2266 unsigned NumBytes) {
2267 // This optimisation potentially adds lots of load and store
2268 // micro-operations, it's only really a great benefit to code-size.
2269 if (!MF.getFunction().optForMinSize())
2272 // If only one register is pushed/popped, LLVM can use an LDR/STR
2273 // instead. We can't modify those so make sure we're dealing with an
2274 // instruction we understand.
2275 bool IsPop = isPopOpcode(MI->getOpcode());
2276 bool IsPush = isPushOpcode(MI->getOpcode());
2277 if (!IsPush && !IsPop)
2280 bool IsVFPPushPop = MI->getOpcode() == ARM::VSTMDDB_UPD ||
2281 MI->getOpcode() == ARM::VLDMDIA_UPD;
2282 bool IsT1PushPop = MI->getOpcode() == ARM::tPUSH ||
2283 MI->getOpcode() == ARM::tPOP ||
2284 MI->getOpcode() == ARM::tPOP_RET;
2286 assert((IsT1PushPop || (MI->getOperand(0).getReg() == ARM::SP &&
2287 MI->getOperand(1).getReg() == ARM::SP)) &&
2288 "trying to fold sp update into non-sp-updating push/pop");
2290 // The VFP push & pop act on D-registers, so we can only fold an adjustment
2291 // by a multiple of 8 bytes in correctly. Similarly rN is 4-bytes. Don't try
2292 // if this is violated.
2293 if (NumBytes % (IsVFPPushPop ? 8 : 4) != 0)
2296 // ARM and Thumb2 push/pop insts have explicit "sp, sp" operands (+
2297 // pred) so the list starts at 4. Thumb1 starts after the predicate.
2298 int RegListIdx = IsT1PushPop ? 2 : 4;
2300 // Calculate the space we'll need in terms of registers.
2301 unsigned RegsNeeded;
2302 const TargetRegisterClass *RegClass;
2304 RegsNeeded = NumBytes / 8;
2305 RegClass = &ARM::DPRRegClass;
2307 RegsNeeded = NumBytes / 4;
2308 RegClass = &ARM::GPRRegClass;
2311 // We're going to have to strip all list operands off before
2312 // re-adding them since the order matters, so save the existing ones
2314 SmallVector<MachineOperand, 4> RegList;
2316 // We're also going to need the first register transferred by this
2317 // instruction, which won't necessarily be the first register in the list.
2318 unsigned FirstRegEnc = -1;
2320 const TargetRegisterInfo *TRI = MF.getRegInfo().getTargetRegisterInfo();
2321 for (int i = MI->getNumOperands() - 1; i >= RegListIdx; --i) {
2322 MachineOperand &MO = MI->getOperand(i);
2323 RegList.push_back(MO);
2325 if (MO.isReg() && TRI->getEncodingValue(MO.getReg()) < FirstRegEnc)
2326 FirstRegEnc = TRI->getEncodingValue(MO.getReg());
2329 const MCPhysReg *CSRegs = TRI->getCalleeSavedRegs(&MF);
2331 // Now try to find enough space in the reglist to allocate NumBytes.
2332 for (int CurRegEnc = FirstRegEnc - 1; CurRegEnc >= 0 && RegsNeeded;
2334 unsigned CurReg = RegClass->getRegister(CurRegEnc);
2336 // Pushing any register is completely harmless, mark the register involved
2337 // as undef since we don't care about its value and must not restore it
2338 // during stack unwinding.
2339 RegList.push_back(MachineOperand::CreateReg(CurReg, false, false,
2340 false, false, true));
2345 // However, we can only pop an extra register if it's not live. For
2346 // registers live within the function we might clobber a return value
2347 // register; the other way a register can be live here is if it's
2349 if (isCalleeSavedRegister(CurReg, CSRegs) ||
2350 MI->getParent()->computeRegisterLiveness(TRI, CurReg, MI) !=
2351 MachineBasicBlock::LQR_Dead) {
2352 // VFP pops don't allow holes in the register list, so any skip is fatal
2353 // for our transformation. GPR pops do, so we should just keep looking.
2360 // Mark the unimportant registers as <def,dead> in the POP.
2361 RegList.push_back(MachineOperand::CreateReg(CurReg, true, false, false,
2369 // Finally we know we can profitably perform the optimisation so go
2370 // ahead: strip all existing registers off and add them back again
2371 // in the right order.
2372 for (int i = MI->getNumOperands() - 1; i >= RegListIdx; --i)
2373 MI->RemoveOperand(i);
2375 // Add the complete list back in.
2376 MachineInstrBuilder MIB(MF, &*MI);
2377 for (int i = RegList.size() - 1; i >= 0; --i)
2378 MIB.add(RegList[i]);
2383 bool llvm::rewriteARMFrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
2384 unsigned FrameReg, int &Offset,
2385 const ARMBaseInstrInfo &TII) {
2386 unsigned Opcode = MI.getOpcode();
2387 const MCInstrDesc &Desc = MI.getDesc();
2388 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
2391 // Memory operands in inline assembly always use AddrMode2.
2392 if (Opcode == ARM::INLINEASM)
2393 AddrMode = ARMII::AddrMode2;
2395 if (Opcode == ARM::ADDri) {
2396 Offset += MI.getOperand(FrameRegIdx+1).getImm();
2398 // Turn it into a move.
2399 MI.setDesc(TII.get(ARM::MOVr));
2400 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
2401 MI.RemoveOperand(FrameRegIdx+1);
2404 } else if (Offset < 0) {
2407 MI.setDesc(TII.get(ARM::SUBri));
2410 // Common case: small offset, fits into instruction.
2411 if (ARM_AM::getSOImmVal(Offset) != -1) {
2412 // Replace the FrameIndex with sp / fp
2413 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
2414 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(Offset);
2419 // Otherwise, pull as much of the immedidate into this ADDri/SUBri
2421 unsigned RotAmt = ARM_AM::getSOImmValRotate(Offset);
2422 unsigned ThisImmVal = Offset & ARM_AM::rotr32(0xFF, RotAmt);
2424 // We will handle these bits from offset, clear them.
2425 Offset &= ~ThisImmVal;
2427 // Get the properly encoded SOImmVal field.
2428 assert(ARM_AM::getSOImmVal(ThisImmVal) != -1 &&
2429 "Bit extraction didn't work?");
2430 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(ThisImmVal);
2432 unsigned ImmIdx = 0;
2434 unsigned NumBits = 0;
2437 case ARMII::AddrMode_i12:
2438 ImmIdx = FrameRegIdx + 1;
2439 InstrOffs = MI.getOperand(ImmIdx).getImm();
2442 case ARMII::AddrMode2:
2443 ImmIdx = FrameRegIdx+2;
2444 InstrOffs = ARM_AM::getAM2Offset(MI.getOperand(ImmIdx).getImm());
2445 if (ARM_AM::getAM2Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
2449 case ARMII::AddrMode3:
2450 ImmIdx = FrameRegIdx+2;
2451 InstrOffs = ARM_AM::getAM3Offset(MI.getOperand(ImmIdx).getImm());
2452 if (ARM_AM::getAM3Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
2456 case ARMII::AddrMode4:
2457 case ARMII::AddrMode6:
2458 // Can't fold any offset even if it's zero.
2460 case ARMII::AddrMode5:
2461 ImmIdx = FrameRegIdx+1;
2462 InstrOffs = ARM_AM::getAM5Offset(MI.getOperand(ImmIdx).getImm());
2463 if (ARM_AM::getAM5Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
2468 case ARMII::AddrMode5FP16:
2469 ImmIdx = FrameRegIdx+1;
2470 InstrOffs = ARM_AM::getAM5Offset(MI.getOperand(ImmIdx).getImm());
2471 if (ARM_AM::getAM5Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
2477 llvm_unreachable("Unsupported addressing mode!");
2480 Offset += InstrOffs * Scale;
2481 assert((Offset & (Scale-1)) == 0 && "Can't encode this offset!");
2487 // Attempt to fold address comp. if opcode has offset bits
2489 // Common case: small offset, fits into instruction.
2490 MachineOperand &ImmOp = MI.getOperand(ImmIdx);
2491 int ImmedOffset = Offset / Scale;
2492 unsigned Mask = (1 << NumBits) - 1;
2493 if ((unsigned)Offset <= Mask * Scale) {
2494 // Replace the FrameIndex with sp
2495 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
2496 // FIXME: When addrmode2 goes away, this will simplify (like the
2497 // T2 version), as the LDR.i12 versions don't need the encoding
2498 // tricks for the offset value.
2500 if (AddrMode == ARMII::AddrMode_i12)
2501 ImmedOffset = -ImmedOffset;
2503 ImmedOffset |= 1 << NumBits;
2505 ImmOp.ChangeToImmediate(ImmedOffset);
2510 // Otherwise, it didn't fit. Pull in what we can to simplify the immed.
2511 ImmedOffset = ImmedOffset & Mask;
2513 if (AddrMode == ARMII::AddrMode_i12)
2514 ImmedOffset = -ImmedOffset;
2516 ImmedOffset |= 1 << NumBits;
2518 ImmOp.ChangeToImmediate(ImmedOffset);
2519 Offset &= ~(Mask*Scale);
2523 Offset = (isSub) ? -Offset : Offset;
2527 /// analyzeCompare - For a comparison instruction, return the source registers
2528 /// in SrcReg and SrcReg2 if having two register operands, and the value it
2529 /// compares against in CmpValue. Return true if the comparison instruction
2530 /// can be analyzed.
2531 bool ARMBaseInstrInfo::analyzeCompare(const MachineInstr &MI, unsigned &SrcReg,
2532 unsigned &SrcReg2, int &CmpMask,
2533 int &CmpValue) const {
2534 switch (MI.getOpcode()) {
2539 SrcReg = MI.getOperand(0).getReg();
2542 CmpValue = MI.getOperand(1).getImm();
2546 SrcReg = MI.getOperand(0).getReg();
2547 SrcReg2 = MI.getOperand(1).getReg();
2553 SrcReg = MI.getOperand(0).getReg();
2555 CmpMask = MI.getOperand(1).getImm();
2563 /// isSuitableForMask - Identify a suitable 'and' instruction that
2564 /// operates on the given source register and applies the same mask
2565 /// as a 'tst' instruction. Provide a limited look-through for copies.
2566 /// When successful, MI will hold the found instruction.
2567 static bool isSuitableForMask(MachineInstr *&MI, unsigned SrcReg,
2568 int CmpMask, bool CommonUse) {
2569 switch (MI->getOpcode()) {
2572 if (CmpMask != MI->getOperand(2).getImm())
2574 if (SrcReg == MI->getOperand(CommonUse ? 1 : 0).getReg())
2582 /// getSwappedCondition - assume the flags are set by MI(a,b), return
2583 /// the condition code if we modify the instructions such that flags are
2585 inline static ARMCC::CondCodes getSwappedCondition(ARMCC::CondCodes CC) {
2587 default: return ARMCC::AL;
2588 case ARMCC::EQ: return ARMCC::EQ;
2589 case ARMCC::NE: return ARMCC::NE;
2590 case ARMCC::HS: return ARMCC::LS;
2591 case ARMCC::LO: return ARMCC::HI;
2592 case ARMCC::HI: return ARMCC::LO;
2593 case ARMCC::LS: return ARMCC::HS;
2594 case ARMCC::GE: return ARMCC::LE;
2595 case ARMCC::LT: return ARMCC::GT;
2596 case ARMCC::GT: return ARMCC::LT;
2597 case ARMCC::LE: return ARMCC::GE;
2601 /// getCmpToAddCondition - assume the flags are set by CMP(a,b), return
2602 /// the condition code if we modify the instructions such that flags are
2603 /// set by ADD(a,b,X).
2604 inline static ARMCC::CondCodes getCmpToAddCondition(ARMCC::CondCodes CC) {
2606 default: return ARMCC::AL;
2607 case ARMCC::HS: return ARMCC::LO;
2608 case ARMCC::LO: return ARMCC::HS;
2609 case ARMCC::VS: return ARMCC::VS;
2610 case ARMCC::VC: return ARMCC::VC;
2614 /// isRedundantFlagInstr - check whether the first instruction, whose only
2615 /// purpose is to update flags, can be made redundant.
2616 /// CMPrr can be made redundant by SUBrr if the operands are the same.
2617 /// CMPri can be made redundant by SUBri if the operands are the same.
2618 /// CMPrr(r0, r1) can be made redundant by ADDr[ri](r0, r1, X).
2619 /// This function can be extended later on.
2620 inline static bool isRedundantFlagInstr(const MachineInstr *CmpI,
2621 unsigned SrcReg, unsigned SrcReg2,
2622 int ImmValue, const MachineInstr *OI) {
2623 if ((CmpI->getOpcode() == ARM::CMPrr ||
2624 CmpI->getOpcode() == ARM::t2CMPrr) &&
2625 (OI->getOpcode() == ARM::SUBrr ||
2626 OI->getOpcode() == ARM::t2SUBrr) &&
2627 ((OI->getOperand(1).getReg() == SrcReg &&
2628 OI->getOperand(2).getReg() == SrcReg2) ||
2629 (OI->getOperand(1).getReg() == SrcReg2 &&
2630 OI->getOperand(2).getReg() == SrcReg)))
2633 if ((CmpI->getOpcode() == ARM::CMPri ||
2634 CmpI->getOpcode() == ARM::t2CMPri) &&
2635 (OI->getOpcode() == ARM::SUBri ||
2636 OI->getOpcode() == ARM::t2SUBri) &&
2637 OI->getOperand(1).getReg() == SrcReg &&
2638 OI->getOperand(2).getImm() == ImmValue)
2641 if ((CmpI->getOpcode() == ARM::CMPrr || CmpI->getOpcode() == ARM::t2CMPrr) &&
2642 (OI->getOpcode() == ARM::ADDrr || OI->getOpcode() == ARM::t2ADDrr ||
2643 OI->getOpcode() == ARM::ADDri || OI->getOpcode() == ARM::t2ADDri) &&
2644 OI->getOperand(0).isReg() && OI->getOperand(1).isReg() &&
2645 OI->getOperand(0).getReg() == SrcReg &&
2646 OI->getOperand(1).getReg() == SrcReg2)
2651 static bool isOptimizeCompareCandidate(MachineInstr *MI, bool &IsThumb1) {
2652 switch (MI->getOpcode()) {
2653 default: return false;
2708 /// optimizeCompareInstr - Convert the instruction supplying the argument to the
2709 /// comparison into one that sets the zero bit in the flags register;
2710 /// Remove a redundant Compare instruction if an earlier instruction can set the
2711 /// flags in the same way as Compare.
2712 /// E.g. SUBrr(r1,r2) and CMPrr(r1,r2). We also handle the case where two
2713 /// operands are swapped: SUBrr(r1,r2) and CMPrr(r2,r1), by updating the
2714 /// condition code of instructions which use the flags.
2715 bool ARMBaseInstrInfo::optimizeCompareInstr(
2716 MachineInstr &CmpInstr, unsigned SrcReg, unsigned SrcReg2, int CmpMask,
2717 int CmpValue, const MachineRegisterInfo *MRI) const {
2718 // Get the unique definition of SrcReg.
2719 MachineInstr *MI = MRI->getUniqueVRegDef(SrcReg);
2720 if (!MI) return false;
2722 // Masked compares sometimes use the same register as the corresponding 'and'.
2723 if (CmpMask != ~0) {
2724 if (!isSuitableForMask(MI, SrcReg, CmpMask, false) || isPredicated(*MI)) {
2726 for (MachineRegisterInfo::use_instr_iterator
2727 UI = MRI->use_instr_begin(SrcReg), UE = MRI->use_instr_end();
2729 if (UI->getParent() != CmpInstr.getParent())
2731 MachineInstr *PotentialAND = &*UI;
2732 if (!isSuitableForMask(PotentialAND, SrcReg, CmpMask, true) ||
2733 isPredicated(*PotentialAND))
2738 if (!MI) return false;
2742 // Get ready to iterate backward from CmpInstr.
2743 MachineBasicBlock::iterator I = CmpInstr, E = MI,
2744 B = CmpInstr.getParent()->begin();
2746 // Early exit if CmpInstr is at the beginning of the BB.
2747 if (I == B) return false;
2749 // There are two possible candidates which can be changed to set CPSR:
2750 // One is MI, the other is a SUB or ADD instruction.
2751 // For CMPrr(r1,r2), we are looking for SUB(r1,r2), SUB(r2,r1), or
2752 // ADDr[ri](r1, r2, X).
2753 // For CMPri(r1, CmpValue), we are looking for SUBri(r1, CmpValue).
2754 MachineInstr *SubAdd = nullptr;
2756 // MI is not a candidate for CMPrr.
2758 else if (MI->getParent() != CmpInstr.getParent() || CmpValue != 0) {
2759 // Conservatively refuse to convert an instruction which isn't in the same
2760 // BB as the comparison.
2761 // For CMPri w/ CmpValue != 0, a SubAdd may still be a candidate.
2762 // Thus we cannot return here.
2763 if (CmpInstr.getOpcode() == ARM::CMPri ||
2764 CmpInstr.getOpcode() == ARM::t2CMPri)
2770 bool IsThumb1 = false;
2771 if (MI && !isOptimizeCompareCandidate(MI, IsThumb1))
2774 // We also want to do this peephole for cases like this: if (a*b == 0),
2775 // and optimise away the CMP instruction from the generated code sequence:
2776 // MULS, MOVS, MOVS, CMP. Here the MOVS instructions load the boolean values
2777 // resulting from the select instruction, but these MOVS instructions for
2778 // Thumb1 (V6M) are flag setting and are thus preventing this optimisation.
2779 // However, if we only have MOVS instructions in between the CMP and the
2780 // other instruction (the MULS in this example), then the CPSR is dead so we
2781 // can safely reorder the sequence into: MOVS, MOVS, MULS, CMP. We do this
2782 // reordering and then continue the analysis hoping we can eliminate the
2783 // CMP. This peephole works on the vregs, so is still in SSA form. As a
2784 // consequence, the movs won't redefine/kill the MUL operands which would
2785 // make this reordering illegal.
2786 if (MI && IsThumb1) {
2788 bool CanReorder = true;
2789 const bool HasStmts = I != E;
2790 for (; I != E; --I) {
2791 if (I->getOpcode() != ARM::tMOVi8) {
2796 if (HasStmts && CanReorder) {
2797 MI = MI->removeFromParent();
2799 CmpInstr.getParent()->insert(E, MI);
2805 // Check that CPSR isn't set between the comparison instruction and the one we
2806 // want to change. At the same time, search for SubAdd.
2807 const TargetRegisterInfo *TRI = &getRegisterInfo();
2809 const MachineInstr &Instr = *--I;
2811 // Check whether CmpInstr can be made redundant by the current instruction.
2812 if (isRedundantFlagInstr(&CmpInstr, SrcReg, SrcReg2, CmpValue, &Instr)) {
2817 // Allow E (which was initially MI) to be SubAdd but do not search before E.
2821 if (Instr.modifiesRegister(ARM::CPSR, TRI) ||
2822 Instr.readsRegister(ARM::CPSR, TRI))
2823 // This instruction modifies or uses CPSR after the one we want to
2824 // change. We can't do this transformation.
2828 // In some cases, we scan the use-list of an instruction for an AND;
2829 // that AND is in the same BB, but may not be scheduled before the
2830 // corresponding TST. In that case, bail out.
2832 // FIXME: We could try to reschedule the AND.
2837 // Return false if no candidates exist.
2841 // The single candidate is called MI.
2842 if (!MI) MI = SubAdd;
2844 // We can't use a predicated instruction - it doesn't always write the flags.
2845 if (isPredicated(*MI))
2848 // Scan forward for the use of CPSR
2849 // When checking against MI: if it's a conditional code that requires
2850 // checking of the V bit or C bit, then this is not safe to do.
2851 // It is safe to remove CmpInstr if CPSR is redefined or killed.
2852 // If we are done with the basic block, we need to check whether CPSR is
2854 SmallVector<std::pair<MachineOperand*, ARMCC::CondCodes>, 4>
2856 bool isSafe = false;
2858 E = CmpInstr.getParent()->end();
2859 while (!isSafe && ++I != E) {
2860 const MachineInstr &Instr = *I;
2861 for (unsigned IO = 0, EO = Instr.getNumOperands();
2862 !isSafe && IO != EO; ++IO) {
2863 const MachineOperand &MO = Instr.getOperand(IO);
2864 if (MO.isRegMask() && MO.clobbersPhysReg(ARM::CPSR)) {
2868 if (!MO.isReg() || MO.getReg() != ARM::CPSR)
2874 // Condition code is after the operand before CPSR except for VSELs.
2875 ARMCC::CondCodes CC;
2876 bool IsInstrVSel = true;
2877 switch (Instr.getOpcode()) {
2879 IsInstrVSel = false;
2880 CC = (ARMCC::CondCodes)Instr.getOperand(IO - 1).getImm();
2901 // If we have SUB(r1, r2) and CMP(r2, r1), the condition code based
2902 // on CMP needs to be updated to be based on SUB.
2903 // If we have ADD(r1, r2, X) and CMP(r1, r2), the condition code also
2904 // needs to be modified.
2905 // Push the condition code operands to OperandsToUpdate.
2906 // If it is safe to remove CmpInstr, the condition code of these
2907 // operands will be modified.
2908 unsigned Opc = SubAdd->getOpcode();
2909 bool IsSub = Opc == ARM::SUBrr || Opc == ARM::t2SUBrr ||
2910 Opc == ARM::SUBri || Opc == ARM::t2SUBri;
2911 if (!IsSub || (SrcReg2 != 0 && SubAdd->getOperand(1).getReg() == SrcReg2 &&
2912 SubAdd->getOperand(2).getReg() == SrcReg)) {
2913 // VSel doesn't support condition code update.
2916 // Ensure we can swap the condition.
2917 ARMCC::CondCodes NewCC = (IsSub ? getSwappedCondition(CC) : getCmpToAddCondition(CC));
2918 if (NewCC == ARMCC::AL)
2920 OperandsToUpdate.push_back(
2921 std::make_pair(&((*I).getOperand(IO - 1)), NewCC));
2924 // No SubAdd, so this is x = <op> y, z; cmp x, 0.
2926 case ARMCC::EQ: // Z
2927 case ARMCC::NE: // Z
2928 case ARMCC::MI: // N
2929 case ARMCC::PL: // N
2930 case ARMCC::AL: // none
2931 // CPSR can be used multiple times, we should continue.
2933 case ARMCC::HS: // C
2934 case ARMCC::LO: // C
2935 case ARMCC::VS: // V
2936 case ARMCC::VC: // V
2937 case ARMCC::HI: // C Z
2938 case ARMCC::LS: // C Z
2939 case ARMCC::GE: // N V
2940 case ARMCC::LT: // N V
2941 case ARMCC::GT: // Z N V
2942 case ARMCC::LE: // Z N V
2943 // The instruction uses the V bit or C bit which is not safe.
2950 // If CPSR is not killed nor re-defined, we should check whether it is
2951 // live-out. If it is live-out, do not optimize.
2953 MachineBasicBlock *MBB = CmpInstr.getParent();
2954 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
2955 SE = MBB->succ_end(); SI != SE; ++SI)
2956 if ((*SI)->isLiveIn(ARM::CPSR))
2960 // Toggle the optional operand to CPSR (if it exists - in Thumb1 we always
2961 // set CPSR so this is represented as an explicit output)
2963 MI->getOperand(5).setReg(ARM::CPSR);
2964 MI->getOperand(5).setIsDef(true);
2966 assert(!isPredicated(*MI) && "Can't use flags from predicated instruction");
2967 CmpInstr.eraseFromParent();
2969 // Modify the condition code of operands in OperandsToUpdate.
2970 // Since we have SUB(r1, r2) and CMP(r2, r1), the condition code needs to
2971 // be changed from r2 > r1 to r1 < r2, from r2 < r1 to r1 > r2, etc.
2972 for (unsigned i = 0, e = OperandsToUpdate.size(); i < e; i++)
2973 OperandsToUpdate[i].first->setImm(OperandsToUpdate[i].second);
2975 MI->clearRegisterDeads(ARM::CPSR);
2980 bool ARMBaseInstrInfo::shouldSink(const MachineInstr &MI) const {
2981 // Do not sink MI if it might be used to optimize a redundant compare.
2982 // We heuristically only look at the instruction immediately following MI to
2983 // avoid potentially searching the entire basic block.
2984 if (isPredicated(MI))
2986 MachineBasicBlock::const_iterator Next = &MI;
2988 unsigned SrcReg, SrcReg2;
2989 int CmpMask, CmpValue;
2990 if (Next != MI.getParent()->end() &&
2991 analyzeCompare(*Next, SrcReg, SrcReg2, CmpMask, CmpValue) &&
2992 isRedundantFlagInstr(&*Next, SrcReg, SrcReg2, CmpValue, &MI))
2997 bool ARMBaseInstrInfo::FoldImmediate(MachineInstr &UseMI, MachineInstr &DefMI,
2999 MachineRegisterInfo *MRI) const {
3000 // Fold large immediates into add, sub, or, xor.
3001 unsigned DefOpc = DefMI.getOpcode();
3002 if (DefOpc != ARM::t2MOVi32imm && DefOpc != ARM::MOVi32imm)
3004 if (!DefMI.getOperand(1).isImm())
3005 // Could be t2MOVi32imm @xx
3008 if (!MRI->hasOneNonDBGUse(Reg))
3011 const MCInstrDesc &DefMCID = DefMI.getDesc();
3012 if (DefMCID.hasOptionalDef()) {
3013 unsigned NumOps = DefMCID.getNumOperands();
3014 const MachineOperand &MO = DefMI.getOperand(NumOps - 1);
3015 if (MO.getReg() == ARM::CPSR && !MO.isDead())
3016 // If DefMI defines CPSR and it is not dead, it's obviously not safe
3021 const MCInstrDesc &UseMCID = UseMI.getDesc();
3022 if (UseMCID.hasOptionalDef()) {
3023 unsigned NumOps = UseMCID.getNumOperands();
3024 if (UseMI.getOperand(NumOps - 1).getReg() == ARM::CPSR)
3025 // If the instruction sets the flag, do not attempt this optimization
3026 // since it may change the semantics of the code.
3030 unsigned UseOpc = UseMI.getOpcode();
3031 unsigned NewUseOpc = 0;
3032 uint32_t ImmVal = (uint32_t)DefMI.getOperand(1).getImm();
3033 uint32_t SOImmValV1 = 0, SOImmValV2 = 0;
3034 bool Commute = false;
3036 default: return false;
3044 case ARM::t2EORrr: {
3045 Commute = UseMI.getOperand(2).getReg() != Reg;
3050 if (UseOpc == ARM::SUBrr && Commute)
3053 // ADD/SUB are special because they're essentially the same operation, so
3054 // we can handle a larger range of immediates.
3055 if (ARM_AM::isSOImmTwoPartVal(ImmVal))
3056 NewUseOpc = UseOpc == ARM::ADDrr ? ARM::ADDri : ARM::SUBri;
3057 else if (ARM_AM::isSOImmTwoPartVal(-ImmVal)) {
3059 NewUseOpc = UseOpc == ARM::ADDrr ? ARM::SUBri : ARM::ADDri;
3062 SOImmValV1 = (uint32_t)ARM_AM::getSOImmTwoPartFirst(ImmVal);
3063 SOImmValV2 = (uint32_t)ARM_AM::getSOImmTwoPartSecond(ImmVal);
3067 if (!ARM_AM::isSOImmTwoPartVal(ImmVal))
3069 SOImmValV1 = (uint32_t)ARM_AM::getSOImmTwoPartFirst(ImmVal);
3070 SOImmValV2 = (uint32_t)ARM_AM::getSOImmTwoPartSecond(ImmVal);
3073 case ARM::ORRrr: NewUseOpc = ARM::ORRri; break;
3074 case ARM::EORrr: NewUseOpc = ARM::EORri; break;
3079 if (UseOpc == ARM::t2SUBrr && Commute)
3082 // ADD/SUB are special because they're essentially the same operation, so
3083 // we can handle a larger range of immediates.
3084 if (ARM_AM::isT2SOImmTwoPartVal(ImmVal))
3085 NewUseOpc = UseOpc == ARM::t2ADDrr ? ARM::t2ADDri : ARM::t2SUBri;
3086 else if (ARM_AM::isT2SOImmTwoPartVal(-ImmVal)) {
3088 NewUseOpc = UseOpc == ARM::t2ADDrr ? ARM::t2SUBri : ARM::t2ADDri;
3091 SOImmValV1 = (uint32_t)ARM_AM::getT2SOImmTwoPartFirst(ImmVal);
3092 SOImmValV2 = (uint32_t)ARM_AM::getT2SOImmTwoPartSecond(ImmVal);
3096 if (!ARM_AM::isT2SOImmTwoPartVal(ImmVal))
3098 SOImmValV1 = (uint32_t)ARM_AM::getT2SOImmTwoPartFirst(ImmVal);
3099 SOImmValV2 = (uint32_t)ARM_AM::getT2SOImmTwoPartSecond(ImmVal);
3102 case ARM::t2ORRrr: NewUseOpc = ARM::t2ORRri; break;
3103 case ARM::t2EORrr: NewUseOpc = ARM::t2EORri; break;
3110 unsigned OpIdx = Commute ? 2 : 1;
3111 unsigned Reg1 = UseMI.getOperand(OpIdx).getReg();
3112 bool isKill = UseMI.getOperand(OpIdx).isKill();
3113 unsigned NewReg = MRI->createVirtualRegister(MRI->getRegClass(Reg));
3114 BuildMI(*UseMI.getParent(), UseMI, UseMI.getDebugLoc(), get(NewUseOpc),
3116 .addReg(Reg1, getKillRegState(isKill))
3118 .add(predOps(ARMCC::AL))
3120 UseMI.setDesc(get(NewUseOpc));
3121 UseMI.getOperand(1).setReg(NewReg);
3122 UseMI.getOperand(1).setIsKill();
3123 UseMI.getOperand(2).ChangeToImmediate(SOImmValV2);
3124 DefMI.eraseFromParent();
3128 static unsigned getNumMicroOpsSwiftLdSt(const InstrItineraryData *ItinData,
3129 const MachineInstr &MI) {
3130 switch (MI.getOpcode()) {
3132 const MCInstrDesc &Desc = MI.getDesc();
3133 int UOps = ItinData->getNumMicroOps(Desc.getSchedClass());
3134 assert(UOps >= 0 && "bad # UOps");
3142 unsigned ShOpVal = MI.getOperand(3).getImm();
3143 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
3144 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3147 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
3148 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
3155 if (!MI.getOperand(2).getReg())
3158 unsigned ShOpVal = MI.getOperand(3).getImm();
3159 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
3160 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3163 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
3164 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
3171 return (ARM_AM::getAM3Op(MI.getOperand(3).getImm()) == ARM_AM::sub) ? 3 : 2;
3173 case ARM::LDRSB_POST:
3174 case ARM::LDRSH_POST: {
3175 unsigned Rt = MI.getOperand(0).getReg();
3176 unsigned Rm = MI.getOperand(3).getReg();
3177 return (Rt == Rm) ? 4 : 3;
3180 case ARM::LDR_PRE_REG:
3181 case ARM::LDRB_PRE_REG: {
3182 unsigned Rt = MI.getOperand(0).getReg();
3183 unsigned Rm = MI.getOperand(3).getReg();
3186 unsigned ShOpVal = MI.getOperand(4).getImm();
3187 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
3188 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3191 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
3192 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
3197 case ARM::STR_PRE_REG:
3198 case ARM::STRB_PRE_REG: {
3199 unsigned ShOpVal = MI.getOperand(4).getImm();
3200 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
3201 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3204 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
3205 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
3211 case ARM::STRH_PRE: {
3212 unsigned Rt = MI.getOperand(0).getReg();
3213 unsigned Rm = MI.getOperand(3).getReg();
3218 return (ARM_AM::getAM3Op(MI.getOperand(4).getImm()) == ARM_AM::sub) ? 3 : 2;
3221 case ARM::LDR_POST_REG:
3222 case ARM::LDRB_POST_REG:
3223 case ARM::LDRH_POST: {
3224 unsigned Rt = MI.getOperand(0).getReg();
3225 unsigned Rm = MI.getOperand(3).getReg();
3226 return (Rt == Rm) ? 3 : 2;
3229 case ARM::LDR_PRE_IMM:
3230 case ARM::LDRB_PRE_IMM:
3231 case ARM::LDR_POST_IMM:
3232 case ARM::LDRB_POST_IMM:
3233 case ARM::STRB_POST_IMM:
3234 case ARM::STRB_POST_REG:
3235 case ARM::STRB_PRE_IMM:
3236 case ARM::STRH_POST:
3237 case ARM::STR_POST_IMM:
3238 case ARM::STR_POST_REG:
3239 case ARM::STR_PRE_IMM:
3242 case ARM::LDRSB_PRE:
3243 case ARM::LDRSH_PRE: {
3244 unsigned Rm = MI.getOperand(3).getReg();
3247 unsigned Rt = MI.getOperand(0).getReg();
3250 unsigned ShOpVal = MI.getOperand(4).getImm();
3251 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
3252 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3255 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
3256 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
3262 unsigned Rt = MI.getOperand(0).getReg();
3263 unsigned Rn = MI.getOperand(2).getReg();
3264 unsigned Rm = MI.getOperand(3).getReg();
3266 return (ARM_AM::getAM3Op(MI.getOperand(4).getImm()) == ARM_AM::sub) ? 4
3268 return (Rt == Rn) ? 3 : 2;
3272 unsigned Rm = MI.getOperand(3).getReg();
3274 return (ARM_AM::getAM3Op(MI.getOperand(4).getImm()) == ARM_AM::sub) ? 4
3279 case ARM::LDRD_POST:
3280 case ARM::t2LDRD_POST:
3283 case ARM::STRD_POST:
3284 case ARM::t2STRD_POST:
3287 case ARM::LDRD_PRE: {
3288 unsigned Rt = MI.getOperand(0).getReg();
3289 unsigned Rn = MI.getOperand(3).getReg();
3290 unsigned Rm = MI.getOperand(4).getReg();
3292 return (ARM_AM::getAM3Op(MI.getOperand(5).getImm()) == ARM_AM::sub) ? 5
3294 return (Rt == Rn) ? 4 : 3;
3297 case ARM::t2LDRD_PRE: {
3298 unsigned Rt = MI.getOperand(0).getReg();
3299 unsigned Rn = MI.getOperand(3).getReg();
3300 return (Rt == Rn) ? 4 : 3;
3303 case ARM::STRD_PRE: {
3304 unsigned Rm = MI.getOperand(4).getReg();
3306 return (ARM_AM::getAM3Op(MI.getOperand(5).getImm()) == ARM_AM::sub) ? 5
3311 case ARM::t2STRD_PRE:
3314 case ARM::t2LDR_POST:
3315 case ARM::t2LDRB_POST:
3316 case ARM::t2LDRB_PRE:
3317 case ARM::t2LDRSBi12:
3318 case ARM::t2LDRSBi8:
3319 case ARM::t2LDRSBpci:
3321 case ARM::t2LDRH_POST:
3322 case ARM::t2LDRH_PRE:
3324 case ARM::t2LDRSB_POST:
3325 case ARM::t2LDRSB_PRE:
3326 case ARM::t2LDRSH_POST:
3327 case ARM::t2LDRSH_PRE:
3328 case ARM::t2LDRSHi12:
3329 case ARM::t2LDRSHi8:
3330 case ARM::t2LDRSHpci:
3334 case ARM::t2LDRDi8: {
3335 unsigned Rt = MI.getOperand(0).getReg();
3336 unsigned Rn = MI.getOperand(2).getReg();
3337 return (Rt == Rn) ? 3 : 2;
3340 case ARM::t2STRB_POST:
3341 case ARM::t2STRB_PRE:
3344 case ARM::t2STRH_POST:
3345 case ARM::t2STRH_PRE:
3347 case ARM::t2STR_POST:
3348 case ARM::t2STR_PRE:
3354 // Return the number of 32-bit words loaded by LDM or stored by STM. If this
3355 // can't be easily determined return 0 (missing MachineMemOperand).
3357 // FIXME: The current MachineInstr design does not support relying on machine
3358 // mem operands to determine the width of a memory access. Instead, we expect
3359 // the target to provide this information based on the instruction opcode and
3360 // operands. However, using MachineMemOperand is the best solution now for
3363 // 1) getNumMicroOps tries to infer LDM memory width from the total number of MI
3364 // operands. This is much more dangerous than using the MachineMemOperand
3365 // sizes because CodeGen passes can insert/remove optional machine operands. In
3366 // fact, it's totally incorrect for preRA passes and appears to be wrong for
3367 // postRA passes as well.
3369 // 2) getNumLDMAddresses is only used by the scheduling machine model and any
3370 // machine model that calls this should handle the unknown (zero size) case.
3372 // Long term, we should require a target hook that verifies MachineMemOperand
3373 // sizes during MC lowering. That target hook should be local to MC lowering
3374 // because we can't ensure that it is aware of other MI forms. Doing this will
3375 // ensure that MachineMemOperands are correctly propagated through all passes.
3376 unsigned ARMBaseInstrInfo::getNumLDMAddresses(const MachineInstr &MI) const {
3378 for (MachineInstr::mmo_iterator I = MI.memoperands_begin(),
3379 E = MI.memoperands_end();
3381 Size += (*I)->getSize();
3386 static unsigned getNumMicroOpsSingleIssuePlusExtras(unsigned Opc,
3388 unsigned UOps = 1 + NumRegs; // 1 for address computation.
3392 case ARM::VLDMDIA_UPD:
3393 case ARM::VLDMDDB_UPD:
3394 case ARM::VLDMSIA_UPD:
3395 case ARM::VLDMSDB_UPD:
3396 case ARM::VSTMDIA_UPD:
3397 case ARM::VSTMDDB_UPD:
3398 case ARM::VSTMSIA_UPD:
3399 case ARM::VSTMSDB_UPD:
3400 case ARM::LDMIA_UPD:
3401 case ARM::LDMDA_UPD:
3402 case ARM::LDMDB_UPD:
3403 case ARM::LDMIB_UPD:
3404 case ARM::STMIA_UPD:
3405 case ARM::STMDA_UPD:
3406 case ARM::STMDB_UPD:
3407 case ARM::STMIB_UPD:
3408 case ARM::tLDMIA_UPD:
3409 case ARM::tSTMIA_UPD:
3410 case ARM::t2LDMIA_UPD:
3411 case ARM::t2LDMDB_UPD:
3412 case ARM::t2STMIA_UPD:
3413 case ARM::t2STMDB_UPD:
3414 ++UOps; // One for base register writeback.
3416 case ARM::LDMIA_RET:
3418 case ARM::t2LDMIA_RET:
3419 UOps += 2; // One for base reg wb, one for write to pc.
3425 unsigned ARMBaseInstrInfo::getNumMicroOps(const InstrItineraryData *ItinData,
3426 const MachineInstr &MI) const {
3427 if (!ItinData || ItinData->isEmpty())
3430 const MCInstrDesc &Desc = MI.getDesc();
3431 unsigned Class = Desc.getSchedClass();
3432 int ItinUOps = ItinData->getNumMicroOps(Class);
3433 if (ItinUOps >= 0) {
3434 if (Subtarget.isSwift() && (Desc.mayLoad() || Desc.mayStore()))
3435 return getNumMicroOpsSwiftLdSt(ItinData, MI);
3440 unsigned Opc = MI.getOpcode();
3443 llvm_unreachable("Unexpected multi-uops instruction!");
3448 // The number of uOps for load / store multiple are determined by the number
3451 // On Cortex-A8, each pair of register loads / stores can be scheduled on the
3452 // same cycle. The scheduling for the first load / store must be done
3453 // separately by assuming the address is not 64-bit aligned.
3455 // On Cortex-A9, the formula is simply (#reg / 2) + (#reg % 2). If the address
3456 // is not 64-bit aligned, then AGU would take an extra cycle. For VFP / NEON
3457 // load / store multiple, the formula is (#reg / 2) + (#reg % 2) + 1.
3459 case ARM::VLDMDIA_UPD:
3460 case ARM::VLDMDDB_UPD:
3462 case ARM::VLDMSIA_UPD:
3463 case ARM::VLDMSDB_UPD:
3465 case ARM::VSTMDIA_UPD:
3466 case ARM::VSTMDDB_UPD:
3468 case ARM::VSTMSIA_UPD:
3469 case ARM::VSTMSDB_UPD: {
3470 unsigned NumRegs = MI.getNumOperands() - Desc.getNumOperands();
3471 return (NumRegs / 2) + (NumRegs % 2) + 1;
3474 case ARM::LDMIA_RET:
3479 case ARM::LDMIA_UPD:
3480 case ARM::LDMDA_UPD:
3481 case ARM::LDMDB_UPD:
3482 case ARM::LDMIB_UPD:
3487 case ARM::STMIA_UPD:
3488 case ARM::STMDA_UPD:
3489 case ARM::STMDB_UPD:
3490 case ARM::STMIB_UPD:
3492 case ARM::tLDMIA_UPD:
3493 case ARM::tSTMIA_UPD:
3497 case ARM::t2LDMIA_RET:
3500 case ARM::t2LDMIA_UPD:
3501 case ARM::t2LDMDB_UPD:
3504 case ARM::t2STMIA_UPD:
3505 case ARM::t2STMDB_UPD: {
3506 unsigned NumRegs = MI.getNumOperands() - Desc.getNumOperands() + 1;
3507 switch (Subtarget.getLdStMultipleTiming()) {
3508 case ARMSubtarget::SingleIssuePlusExtras:
3509 return getNumMicroOpsSingleIssuePlusExtras(Opc, NumRegs);
3510 case ARMSubtarget::SingleIssue:
3511 // Assume the worst.
3513 case ARMSubtarget::DoubleIssue: {
3516 // 4 registers would be issued: 2, 2.
3517 // 5 registers would be issued: 2, 2, 1.
3518 unsigned UOps = (NumRegs / 2);
3523 case ARMSubtarget::DoubleIssueCheckUnalignedAccess: {
3524 unsigned UOps = (NumRegs / 2);
3525 // If there are odd number of registers or if it's not 64-bit aligned,
3526 // then it takes an extra AGU (Address Generation Unit) cycle.
3527 if ((NumRegs % 2) || !MI.hasOneMemOperand() ||
3528 (*MI.memoperands_begin())->getAlignment() < 8)
3535 llvm_unreachable("Didn't find the number of microops");
3539 ARMBaseInstrInfo::getVLDMDefCycle(const InstrItineraryData *ItinData,
3540 const MCInstrDesc &DefMCID,
3542 unsigned DefIdx, unsigned DefAlign) const {
3543 int RegNo = (int)(DefIdx+1) - DefMCID.getNumOperands() + 1;
3545 // Def is the address writeback.
3546 return ItinData->getOperandCycle(DefClass, DefIdx);
3549 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
3550 // (regno / 2) + (regno % 2) + 1
3551 DefCycle = RegNo / 2 + 1;
3554 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
3556 bool isSLoad = false;
3558 switch (DefMCID.getOpcode()) {
3561 case ARM::VLDMSIA_UPD:
3562 case ARM::VLDMSDB_UPD:
3567 // If there are odd number of 'S' registers or if it's not 64-bit aligned,
3568 // then it takes an extra cycle.
3569 if ((isSLoad && (RegNo % 2)) || DefAlign < 8)
3572 // Assume the worst.
3573 DefCycle = RegNo + 2;
3579 bool ARMBaseInstrInfo::isLDMBaseRegInList(const MachineInstr &MI) const {
3580 unsigned BaseReg = MI.getOperand(0).getReg();
3581 for (unsigned i = 1, sz = MI.getNumOperands(); i < sz; ++i) {
3582 const auto &Op = MI.getOperand(i);
3583 if (Op.isReg() && Op.getReg() == BaseReg)
3589 ARMBaseInstrInfo::getLDMVariableDefsSize(const MachineInstr &MI) const {
3590 // ins GPR:$Rn, $p (2xOp), reglist:$regs, variable_ops
3591 // (outs GPR:$wb), (ins GPR:$Rn, $p (2xOp), reglist:$regs, variable_ops)
3592 return MI.getNumOperands() + 1 - MI.getDesc().getNumOperands();
3596 ARMBaseInstrInfo::getLDMDefCycle(const InstrItineraryData *ItinData,
3597 const MCInstrDesc &DefMCID,
3599 unsigned DefIdx, unsigned DefAlign) const {
3600 int RegNo = (int)(DefIdx+1) - DefMCID.getNumOperands() + 1;
3602 // Def is the address writeback.
3603 return ItinData->getOperandCycle(DefClass, DefIdx);
3606 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
3607 // 4 registers would be issued: 1, 2, 1.
3608 // 5 registers would be issued: 1, 2, 2.
3609 DefCycle = RegNo / 2;
3612 // Result latency is issue cycle + 2: E2.
3614 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
3615 DefCycle = (RegNo / 2);
3616 // If there are odd number of registers or if it's not 64-bit aligned,
3617 // then it takes an extra AGU (Address Generation Unit) cycle.
3618 if ((RegNo % 2) || DefAlign < 8)
3620 // Result latency is AGU cycles + 2.
3623 // Assume the worst.
3624 DefCycle = RegNo + 2;
3631 ARMBaseInstrInfo::getVSTMUseCycle(const InstrItineraryData *ItinData,
3632 const MCInstrDesc &UseMCID,
3634 unsigned UseIdx, unsigned UseAlign) const {
3635 int RegNo = (int)(UseIdx+1) - UseMCID.getNumOperands() + 1;
3637 return ItinData->getOperandCycle(UseClass, UseIdx);
3640 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
3641 // (regno / 2) + (regno % 2) + 1
3642 UseCycle = RegNo / 2 + 1;
3645 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
3647 bool isSStore = false;
3649 switch (UseMCID.getOpcode()) {
3652 case ARM::VSTMSIA_UPD:
3653 case ARM::VSTMSDB_UPD:
3658 // If there are odd number of 'S' registers or if it's not 64-bit aligned,
3659 // then it takes an extra cycle.
3660 if ((isSStore && (RegNo % 2)) || UseAlign < 8)
3663 // Assume the worst.
3664 UseCycle = RegNo + 2;
3671 ARMBaseInstrInfo::getSTMUseCycle(const InstrItineraryData *ItinData,
3672 const MCInstrDesc &UseMCID,
3674 unsigned UseIdx, unsigned UseAlign) const {
3675 int RegNo = (int)(UseIdx+1) - UseMCID.getNumOperands() + 1;
3677 return ItinData->getOperandCycle(UseClass, UseIdx);
3680 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
3681 UseCycle = RegNo / 2;
3686 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
3687 UseCycle = (RegNo / 2);
3688 // If there are odd number of registers or if it's not 64-bit aligned,
3689 // then it takes an extra AGU (Address Generation Unit) cycle.
3690 if ((RegNo % 2) || UseAlign < 8)
3693 // Assume the worst.
3700 ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
3701 const MCInstrDesc &DefMCID,
3702 unsigned DefIdx, unsigned DefAlign,
3703 const MCInstrDesc &UseMCID,
3704 unsigned UseIdx, unsigned UseAlign) const {
3705 unsigned DefClass = DefMCID.getSchedClass();
3706 unsigned UseClass = UseMCID.getSchedClass();
3708 if (DefIdx < DefMCID.getNumDefs() && UseIdx < UseMCID.getNumOperands())
3709 return ItinData->getOperandLatency(DefClass, DefIdx, UseClass, UseIdx);
3711 // This may be a def / use of a variable_ops instruction, the operand
3712 // latency might be determinable dynamically. Let the target try to
3715 bool LdmBypass = false;
3716 switch (DefMCID.getOpcode()) {
3718 DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
3722 case ARM::VLDMDIA_UPD:
3723 case ARM::VLDMDDB_UPD:
3725 case ARM::VLDMSIA_UPD:
3726 case ARM::VLDMSDB_UPD:
3727 DefCycle = getVLDMDefCycle(ItinData, DefMCID, DefClass, DefIdx, DefAlign);
3730 case ARM::LDMIA_RET:
3735 case ARM::LDMIA_UPD:
3736 case ARM::LDMDA_UPD:
3737 case ARM::LDMDB_UPD:
3738 case ARM::LDMIB_UPD:
3740 case ARM::tLDMIA_UPD:
3742 case ARM::t2LDMIA_RET:
3745 case ARM::t2LDMIA_UPD:
3746 case ARM::t2LDMDB_UPD:
3748 DefCycle = getLDMDefCycle(ItinData, DefMCID, DefClass, DefIdx, DefAlign);
3753 // We can't seem to determine the result latency of the def, assume it's 2.
3757 switch (UseMCID.getOpcode()) {
3759 UseCycle = ItinData->getOperandCycle(UseClass, UseIdx);
3763 case ARM::VSTMDIA_UPD:
3764 case ARM::VSTMDDB_UPD:
3766 case ARM::VSTMSIA_UPD:
3767 case ARM::VSTMSDB_UPD:
3768 UseCycle = getVSTMUseCycle(ItinData, UseMCID, UseClass, UseIdx, UseAlign);
3775 case ARM::STMIA_UPD:
3776 case ARM::STMDA_UPD:
3777 case ARM::STMDB_UPD:
3778 case ARM::STMIB_UPD:
3779 case ARM::tSTMIA_UPD:
3784 case ARM::t2STMIA_UPD:
3785 case ARM::t2STMDB_UPD:
3786 UseCycle = getSTMUseCycle(ItinData, UseMCID, UseClass, UseIdx, UseAlign);
3791 // Assume it's read in the first stage.
3794 UseCycle = DefCycle - UseCycle + 1;
3797 // It's a variable_ops instruction so we can't use DefIdx here. Just use
3798 // first def operand.
3799 if (ItinData->hasPipelineForwarding(DefClass, DefMCID.getNumOperands()-1,
3802 } else if (ItinData->hasPipelineForwarding(DefClass, DefIdx,
3803 UseClass, UseIdx)) {
3811 static const MachineInstr *getBundledDefMI(const TargetRegisterInfo *TRI,
3812 const MachineInstr *MI, unsigned Reg,
3813 unsigned &DefIdx, unsigned &Dist) {
3816 MachineBasicBlock::const_iterator I = MI; ++I;
3817 MachineBasicBlock::const_instr_iterator II = std::prev(I.getInstrIterator());
3818 assert(II->isInsideBundle() && "Empty bundle?");
3821 while (II->isInsideBundle()) {
3822 Idx = II->findRegisterDefOperandIdx(Reg, false, true, TRI);
3829 assert(Idx != -1 && "Cannot find bundled definition!");
3834 static const MachineInstr *getBundledUseMI(const TargetRegisterInfo *TRI,
3835 const MachineInstr &MI, unsigned Reg,
3836 unsigned &UseIdx, unsigned &Dist) {
3839 MachineBasicBlock::const_instr_iterator II = ++MI.getIterator();
3840 assert(II->isInsideBundle() && "Empty bundle?");
3841 MachineBasicBlock::const_instr_iterator E = MI.getParent()->instr_end();
3843 // FIXME: This doesn't properly handle multiple uses.
3845 while (II != E && II->isInsideBundle()) {
3846 Idx = II->findRegisterUseOperandIdx(Reg, false, TRI);
3849 if (II->getOpcode() != ARM::t2IT)
3863 /// Return the number of cycles to add to (or subtract from) the static
3864 /// itinerary based on the def opcode and alignment. The caller will ensure that
3865 /// adjusted latency is at least one cycle.
3866 static int adjustDefLatency(const ARMSubtarget &Subtarget,
3867 const MachineInstr &DefMI,
3868 const MCInstrDesc &DefMCID, unsigned DefAlign) {
3870 if (Subtarget.isCortexA8() || Subtarget.isLikeA9() || Subtarget.isCortexA7()) {
3871 // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2]
3872 // variants are one cycle cheaper.
3873 switch (DefMCID.getOpcode()) {
3877 unsigned ShOpVal = DefMI.getOperand(3).getImm();
3878 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3880 (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
3887 case ARM::t2LDRSHs: {
3888 // Thumb2 mode: lsl only.
3889 unsigned ShAmt = DefMI.getOperand(3).getImm();
3890 if (ShAmt == 0 || ShAmt == 2)
3895 } else if (Subtarget.isSwift()) {
3896 // FIXME: Properly handle all of the latency adjustments for address
3898 switch (DefMCID.getOpcode()) {
3902 unsigned ShOpVal = DefMI.getOperand(3).getImm();
3903 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
3904 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3907 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
3908 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
3911 ShImm == 1 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsr)
3918 case ARM::t2LDRSHs: {
3919 // Thumb2 mode: lsl only.
3920 unsigned ShAmt = DefMI.getOperand(3).getImm();
3921 if (ShAmt == 0 || ShAmt == 1 || ShAmt == 2 || ShAmt == 3)
3928 if (DefAlign < 8 && Subtarget.checkVLDnAccessAlignment()) {
3929 switch (DefMCID.getOpcode()) {
3935 case ARM::VLD1q8wb_fixed:
3936 case ARM::VLD1q16wb_fixed:
3937 case ARM::VLD1q32wb_fixed:
3938 case ARM::VLD1q64wb_fixed:
3939 case ARM::VLD1q8wb_register:
3940 case ARM::VLD1q16wb_register:
3941 case ARM::VLD1q32wb_register:
3942 case ARM::VLD1q64wb_register:
3949 case ARM::VLD2d8wb_fixed:
3950 case ARM::VLD2d16wb_fixed:
3951 case ARM::VLD2d32wb_fixed:
3952 case ARM::VLD2q8wb_fixed:
3953 case ARM::VLD2q16wb_fixed:
3954 case ARM::VLD2q32wb_fixed:
3955 case ARM::VLD2d8wb_register:
3956 case ARM::VLD2d16wb_register:
3957 case ARM::VLD2d32wb_register:
3958 case ARM::VLD2q8wb_register:
3959 case ARM::VLD2q16wb_register:
3960 case ARM::VLD2q32wb_register:
3965 case ARM::VLD3d8_UPD:
3966 case ARM::VLD3d16_UPD:
3967 case ARM::VLD3d32_UPD:
3968 case ARM::VLD1d64Twb_fixed:
3969 case ARM::VLD1d64Twb_register:
3970 case ARM::VLD3q8_UPD:
3971 case ARM::VLD3q16_UPD:
3972 case ARM::VLD3q32_UPD:
3977 case ARM::VLD4d8_UPD:
3978 case ARM::VLD4d16_UPD:
3979 case ARM::VLD4d32_UPD:
3980 case ARM::VLD1d64Qwb_fixed:
3981 case ARM::VLD1d64Qwb_register:
3982 case ARM::VLD4q8_UPD:
3983 case ARM::VLD4q16_UPD:
3984 case ARM::VLD4q32_UPD:
3985 case ARM::VLD1DUPq8:
3986 case ARM::VLD1DUPq16:
3987 case ARM::VLD1DUPq32:
3988 case ARM::VLD1DUPq8wb_fixed:
3989 case ARM::VLD1DUPq16wb_fixed:
3990 case ARM::VLD1DUPq32wb_fixed:
3991 case ARM::VLD1DUPq8wb_register:
3992 case ARM::VLD1DUPq16wb_register:
3993 case ARM::VLD1DUPq32wb_register:
3994 case ARM::VLD2DUPd8:
3995 case ARM::VLD2DUPd16:
3996 case ARM::VLD2DUPd32:
3997 case ARM::VLD2DUPd8wb_fixed:
3998 case ARM::VLD2DUPd16wb_fixed:
3999 case ARM::VLD2DUPd32wb_fixed:
4000 case ARM::VLD2DUPd8wb_register:
4001 case ARM::VLD2DUPd16wb_register:
4002 case ARM::VLD2DUPd32wb_register:
4003 case ARM::VLD4DUPd8:
4004 case ARM::VLD4DUPd16:
4005 case ARM::VLD4DUPd32:
4006 case ARM::VLD4DUPd8_UPD:
4007 case ARM::VLD4DUPd16_UPD:
4008 case ARM::VLD4DUPd32_UPD:
4010 case ARM::VLD1LNd16:
4011 case ARM::VLD1LNd32:
4012 case ARM::VLD1LNd8_UPD:
4013 case ARM::VLD1LNd16_UPD:
4014 case ARM::VLD1LNd32_UPD:
4016 case ARM::VLD2LNd16:
4017 case ARM::VLD2LNd32:
4018 case ARM::VLD2LNq16:
4019 case ARM::VLD2LNq32:
4020 case ARM::VLD2LNd8_UPD:
4021 case ARM::VLD2LNd16_UPD:
4022 case ARM::VLD2LNd32_UPD:
4023 case ARM::VLD2LNq16_UPD:
4024 case ARM::VLD2LNq32_UPD:
4026 case ARM::VLD4LNd16:
4027 case ARM::VLD4LNd32:
4028 case ARM::VLD4LNq16:
4029 case ARM::VLD4LNq32:
4030 case ARM::VLD4LNd8_UPD:
4031 case ARM::VLD4LNd16_UPD:
4032 case ARM::VLD4LNd32_UPD:
4033 case ARM::VLD4LNq16_UPD:
4034 case ARM::VLD4LNq32_UPD:
4035 // If the address is not 64-bit aligned, the latencies of these
4036 // instructions increases by one.
4044 int ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
4045 const MachineInstr &DefMI,
4047 const MachineInstr &UseMI,
4048 unsigned UseIdx) const {
4049 // No operand latency. The caller may fall back to getInstrLatency.
4050 if (!ItinData || ItinData->isEmpty())
4053 const MachineOperand &DefMO = DefMI.getOperand(DefIdx);
4054 unsigned Reg = DefMO.getReg();
4056 const MachineInstr *ResolvedDefMI = &DefMI;
4057 unsigned DefAdj = 0;
4058 if (DefMI.isBundle())
4060 getBundledDefMI(&getRegisterInfo(), &DefMI, Reg, DefIdx, DefAdj);
4061 if (ResolvedDefMI->isCopyLike() || ResolvedDefMI->isInsertSubreg() ||
4062 ResolvedDefMI->isRegSequence() || ResolvedDefMI->isImplicitDef()) {
4066 const MachineInstr *ResolvedUseMI = &UseMI;
4067 unsigned UseAdj = 0;
4068 if (UseMI.isBundle()) {
4070 getBundledUseMI(&getRegisterInfo(), UseMI, Reg, UseIdx, UseAdj);
4075 return getOperandLatencyImpl(
4076 ItinData, *ResolvedDefMI, DefIdx, ResolvedDefMI->getDesc(), DefAdj, DefMO,
4077 Reg, *ResolvedUseMI, UseIdx, ResolvedUseMI->getDesc(), UseAdj);
4080 int ARMBaseInstrInfo::getOperandLatencyImpl(
4081 const InstrItineraryData *ItinData, const MachineInstr &DefMI,
4082 unsigned DefIdx, const MCInstrDesc &DefMCID, unsigned DefAdj,
4083 const MachineOperand &DefMO, unsigned Reg, const MachineInstr &UseMI,
4084 unsigned UseIdx, const MCInstrDesc &UseMCID, unsigned UseAdj) const {
4085 if (Reg == ARM::CPSR) {
4086 if (DefMI.getOpcode() == ARM::FMSTAT) {
4087 // fpscr -> cpsr stalls over 20 cycles on A8 (and earlier?)
4088 return Subtarget.isLikeA9() ? 1 : 20;
4091 // CPSR set and branch can be paired in the same cycle.
4092 if (UseMI.isBranch())
4095 // Otherwise it takes the instruction latency (generally one).
4096 unsigned Latency = getInstrLatency(ItinData, DefMI);
4098 // For Thumb2 and -Os, prefer scheduling CPSR setting instruction close to
4099 // its uses. Instructions which are otherwise scheduled between them may
4100 // incur a code size penalty (not able to use the CPSR setting 16-bit
4102 if (Latency > 0 && Subtarget.isThumb2()) {
4103 const MachineFunction *MF = DefMI.getParent()->getParent();
4104 // FIXME: Use Function::optForSize().
4105 if (MF->getFunction().hasFnAttribute(Attribute::OptimizeForSize))
4111 if (DefMO.isImplicit() || UseMI.getOperand(UseIdx).isImplicit())
4114 unsigned DefAlign = DefMI.hasOneMemOperand()
4115 ? (*DefMI.memoperands_begin())->getAlignment()
4117 unsigned UseAlign = UseMI.hasOneMemOperand()
4118 ? (*UseMI.memoperands_begin())->getAlignment()
4121 // Get the itinerary's latency if possible, and handle variable_ops.
4122 int Latency = getOperandLatency(ItinData, DefMCID, DefIdx, DefAlign, UseMCID,
4124 // Unable to find operand latency. The caller may resort to getInstrLatency.
4128 // Adjust for IT block position.
4129 int Adj = DefAdj + UseAdj;
4131 // Adjust for dynamic def-side opcode variants not captured by the itinerary.
4132 Adj += adjustDefLatency(Subtarget, DefMI, DefMCID, DefAlign);
4133 if (Adj >= 0 || (int)Latency > -Adj) {
4134 return Latency + Adj;
4136 // Return the itinerary latency, which may be zero but not less than zero.
4141 ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
4142 SDNode *DefNode, unsigned DefIdx,
4143 SDNode *UseNode, unsigned UseIdx) const {
4144 if (!DefNode->isMachineOpcode())
4147 const MCInstrDesc &DefMCID = get(DefNode->getMachineOpcode());
4149 if (isZeroCost(DefMCID.Opcode))
4152 if (!ItinData || ItinData->isEmpty())
4153 return DefMCID.mayLoad() ? 3 : 1;
4155 if (!UseNode->isMachineOpcode()) {
4156 int Latency = ItinData->getOperandCycle(DefMCID.getSchedClass(), DefIdx);
4157 int Adj = Subtarget.getPreISelOperandLatencyAdjustment();
4158 int Threshold = 1 + Adj;
4159 return Latency <= Threshold ? 1 : Latency - Adj;
4162 const MCInstrDesc &UseMCID = get(UseNode->getMachineOpcode());
4163 const MachineSDNode *DefMN = dyn_cast<MachineSDNode>(DefNode);
4164 unsigned DefAlign = !DefMN->memoperands_empty()
4165 ? (*DefMN->memoperands_begin())->getAlignment() : 0;
4166 const MachineSDNode *UseMN = dyn_cast<MachineSDNode>(UseNode);
4167 unsigned UseAlign = !UseMN->memoperands_empty()
4168 ? (*UseMN->memoperands_begin())->getAlignment() : 0;
4169 int Latency = getOperandLatency(ItinData, DefMCID, DefIdx, DefAlign,
4170 UseMCID, UseIdx, UseAlign);
4173 (Subtarget.isCortexA8() || Subtarget.isLikeA9() ||
4174 Subtarget.isCortexA7())) {
4175 // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2]
4176 // variants are one cycle cheaper.
4177 switch (DefMCID.getOpcode()) {
4182 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
4183 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
4185 (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
4192 case ARM::t2LDRSHs: {
4193 // Thumb2 mode: lsl only.
4195 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
4196 if (ShAmt == 0 || ShAmt == 2)
4201 } else if (DefIdx == 0 && Latency > 2 && Subtarget.isSwift()) {
4202 // FIXME: Properly handle all of the latency adjustments for address
4204 switch (DefMCID.getOpcode()) {
4209 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
4210 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
4212 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
4213 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
4215 else if (ShImm == 1 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsr)
4223 // Thumb2 mode: lsl 0-3 only.
4229 if (DefAlign < 8 && Subtarget.checkVLDnAccessAlignment())
4230 switch (DefMCID.getOpcode()) {
4236 case ARM::VLD1q8wb_register:
4237 case ARM::VLD1q16wb_register:
4238 case ARM::VLD1q32wb_register:
4239 case ARM::VLD1q64wb_register:
4240 case ARM::VLD1q8wb_fixed:
4241 case ARM::VLD1q16wb_fixed:
4242 case ARM::VLD1q32wb_fixed:
4243 case ARM::VLD1q64wb_fixed:
4247 case ARM::VLD2q8Pseudo:
4248 case ARM::VLD2q16Pseudo:
4249 case ARM::VLD2q32Pseudo:
4250 case ARM::VLD2d8wb_fixed:
4251 case ARM::VLD2d16wb_fixed:
4252 case ARM::VLD2d32wb_fixed:
4253 case ARM::VLD2q8PseudoWB_fixed:
4254 case ARM::VLD2q16PseudoWB_fixed:
4255 case ARM::VLD2q32PseudoWB_fixed:
4256 case ARM::VLD2d8wb_register:
4257 case ARM::VLD2d16wb_register:
4258 case ARM::VLD2d32wb_register:
4259 case ARM::VLD2q8PseudoWB_register:
4260 case ARM::VLD2q16PseudoWB_register:
4261 case ARM::VLD2q32PseudoWB_register:
4262 case ARM::VLD3d8Pseudo:
4263 case ARM::VLD3d16Pseudo:
4264 case ARM::VLD3d32Pseudo:
4265 case ARM::VLD1d8TPseudo:
4266 case ARM::VLD1d16TPseudo:
4267 case ARM::VLD1d32TPseudo:
4268 case ARM::VLD1d64TPseudo:
4269 case ARM::VLD1d64TPseudoWB_fixed:
4270 case ARM::VLD1d64TPseudoWB_register:
4271 case ARM::VLD3d8Pseudo_UPD:
4272 case ARM::VLD3d16Pseudo_UPD:
4273 case ARM::VLD3d32Pseudo_UPD:
4274 case ARM::VLD3q8Pseudo_UPD:
4275 case ARM::VLD3q16Pseudo_UPD:
4276 case ARM::VLD3q32Pseudo_UPD:
4277 case ARM::VLD3q8oddPseudo:
4278 case ARM::VLD3q16oddPseudo:
4279 case ARM::VLD3q32oddPseudo:
4280 case ARM::VLD3q8oddPseudo_UPD:
4281 case ARM::VLD3q16oddPseudo_UPD:
4282 case ARM::VLD3q32oddPseudo_UPD:
4283 case ARM::VLD4d8Pseudo:
4284 case ARM::VLD4d16Pseudo:
4285 case ARM::VLD4d32Pseudo:
4286 case ARM::VLD1d8QPseudo:
4287 case ARM::VLD1d16QPseudo:
4288 case ARM::VLD1d32QPseudo:
4289 case ARM::VLD1d64QPseudo:
4290 case ARM::VLD1d64QPseudoWB_fixed:
4291 case ARM::VLD1d64QPseudoWB_register:
4292 case ARM::VLD1q8HighQPseudo:
4293 case ARM::VLD1q8LowQPseudo_UPD:
4294 case ARM::VLD1q8HighTPseudo:
4295 case ARM::VLD1q8LowTPseudo_UPD:
4296 case ARM::VLD1q16HighQPseudo:
4297 case ARM::VLD1q16LowQPseudo_UPD:
4298 case ARM::VLD1q16HighTPseudo:
4299 case ARM::VLD1q16LowTPseudo_UPD:
4300 case ARM::VLD1q32HighQPseudo:
4301 case ARM::VLD1q32LowQPseudo_UPD:
4302 case ARM::VLD1q32HighTPseudo:
4303 case ARM::VLD1q32LowTPseudo_UPD:
4304 case ARM::VLD1q64HighQPseudo:
4305 case ARM::VLD1q64LowQPseudo_UPD:
4306 case ARM::VLD1q64HighTPseudo:
4307 case ARM::VLD1q64LowTPseudo_UPD:
4308 case ARM::VLD4d8Pseudo_UPD:
4309 case ARM::VLD4d16Pseudo_UPD:
4310 case ARM::VLD4d32Pseudo_UPD:
4311 case ARM::VLD4q8Pseudo_UPD:
4312 case ARM::VLD4q16Pseudo_UPD:
4313 case ARM::VLD4q32Pseudo_UPD:
4314 case ARM::VLD4q8oddPseudo:
4315 case ARM::VLD4q16oddPseudo:
4316 case ARM::VLD4q32oddPseudo:
4317 case ARM::VLD4q8oddPseudo_UPD:
4318 case ARM::VLD4q16oddPseudo_UPD:
4319 case ARM::VLD4q32oddPseudo_UPD:
4320 case ARM::VLD1DUPq8:
4321 case ARM::VLD1DUPq16:
4322 case ARM::VLD1DUPq32:
4323 case ARM::VLD1DUPq8wb_fixed:
4324 case ARM::VLD1DUPq16wb_fixed:
4325 case ARM::VLD1DUPq32wb_fixed:
4326 case ARM::VLD1DUPq8wb_register:
4327 case ARM::VLD1DUPq16wb_register:
4328 case ARM::VLD1DUPq32wb_register:
4329 case ARM::VLD2DUPd8:
4330 case ARM::VLD2DUPd16:
4331 case ARM::VLD2DUPd32:
4332 case ARM::VLD2DUPd8wb_fixed:
4333 case ARM::VLD2DUPd16wb_fixed:
4334 case ARM::VLD2DUPd32wb_fixed:
4335 case ARM::VLD2DUPd8wb_register:
4336 case ARM::VLD2DUPd16wb_register:
4337 case ARM::VLD2DUPd32wb_register:
4338 case ARM::VLD2DUPq8EvenPseudo:
4339 case ARM::VLD2DUPq8OddPseudo:
4340 case ARM::VLD2DUPq16EvenPseudo:
4341 case ARM::VLD2DUPq16OddPseudo:
4342 case ARM::VLD2DUPq32EvenPseudo:
4343 case ARM::VLD2DUPq32OddPseudo:
4344 case ARM::VLD3DUPq8EvenPseudo:
4345 case ARM::VLD3DUPq8OddPseudo:
4346 case ARM::VLD3DUPq16EvenPseudo:
4347 case ARM::VLD3DUPq16OddPseudo:
4348 case ARM::VLD3DUPq32EvenPseudo:
4349 case ARM::VLD3DUPq32OddPseudo:
4350 case ARM::VLD4DUPd8Pseudo:
4351 case ARM::VLD4DUPd16Pseudo:
4352 case ARM::VLD4DUPd32Pseudo:
4353 case ARM::VLD4DUPd8Pseudo_UPD:
4354 case ARM::VLD4DUPd16Pseudo_UPD:
4355 case ARM::VLD4DUPd32Pseudo_UPD:
4356 case ARM::VLD4DUPq8EvenPseudo:
4357 case ARM::VLD4DUPq8OddPseudo:
4358 case ARM::VLD4DUPq16EvenPseudo:
4359 case ARM::VLD4DUPq16OddPseudo:
4360 case ARM::VLD4DUPq32EvenPseudo:
4361 case ARM::VLD4DUPq32OddPseudo:
4362 case ARM::VLD1LNq8Pseudo:
4363 case ARM::VLD1LNq16Pseudo:
4364 case ARM::VLD1LNq32Pseudo:
4365 case ARM::VLD1LNq8Pseudo_UPD:
4366 case ARM::VLD1LNq16Pseudo_UPD:
4367 case ARM::VLD1LNq32Pseudo_UPD:
4368 case ARM::VLD2LNd8Pseudo:
4369 case ARM::VLD2LNd16Pseudo:
4370 case ARM::VLD2LNd32Pseudo:
4371 case ARM::VLD2LNq16Pseudo:
4372 case ARM::VLD2LNq32Pseudo:
4373 case ARM::VLD2LNd8Pseudo_UPD:
4374 case ARM::VLD2LNd16Pseudo_UPD:
4375 case ARM::VLD2LNd32Pseudo_UPD:
4376 case ARM::VLD2LNq16Pseudo_UPD:
4377 case ARM::VLD2LNq32Pseudo_UPD:
4378 case ARM::VLD4LNd8Pseudo:
4379 case ARM::VLD4LNd16Pseudo:
4380 case ARM::VLD4LNd32Pseudo:
4381 case ARM::VLD4LNq16Pseudo:
4382 case ARM::VLD4LNq32Pseudo:
4383 case ARM::VLD4LNd8Pseudo_UPD:
4384 case ARM::VLD4LNd16Pseudo_UPD:
4385 case ARM::VLD4LNd32Pseudo_UPD:
4386 case ARM::VLD4LNq16Pseudo_UPD:
4387 case ARM::VLD4LNq32Pseudo_UPD:
4388 // If the address is not 64-bit aligned, the latencies of these
4389 // instructions increases by one.
4397 unsigned ARMBaseInstrInfo::getPredicationCost(const MachineInstr &MI) const {
4398 if (MI.isCopyLike() || MI.isInsertSubreg() || MI.isRegSequence() ||
4405 const MCInstrDesc &MCID = MI.getDesc();
4407 if (MCID.isCall() || (MCID.hasImplicitDefOfPhysReg(ARM::CPSR) &&
4408 !Subtarget.cheapPredicableCPSRDef())) {
4409 // When predicated, CPSR is an additional source operand for CPSR updating
4410 // instructions, this apparently increases their latencies.
4416 unsigned ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
4417 const MachineInstr &MI,
4418 unsigned *PredCost) const {
4419 if (MI.isCopyLike() || MI.isInsertSubreg() || MI.isRegSequence() ||
4423 // An instruction scheduler typically runs on unbundled instructions, however
4424 // other passes may query the latency of a bundled instruction.
4425 if (MI.isBundle()) {
4426 unsigned Latency = 0;
4427 MachineBasicBlock::const_instr_iterator I = MI.getIterator();
4428 MachineBasicBlock::const_instr_iterator E = MI.getParent()->instr_end();
4429 while (++I != E && I->isInsideBundle()) {
4430 if (I->getOpcode() != ARM::t2IT)
4431 Latency += getInstrLatency(ItinData, *I, PredCost);
4436 const MCInstrDesc &MCID = MI.getDesc();
4437 if (PredCost && (MCID.isCall() || (MCID.hasImplicitDefOfPhysReg(ARM::CPSR) &&
4438 !Subtarget.cheapPredicableCPSRDef()))) {
4439 // When predicated, CPSR is an additional source operand for CPSR updating
4440 // instructions, this apparently increases their latencies.
4443 // Be sure to call getStageLatency for an empty itinerary in case it has a
4444 // valid MinLatency property.
4446 return MI.mayLoad() ? 3 : 1;
4448 unsigned Class = MCID.getSchedClass();
4450 // For instructions with variable uops, use uops as latency.
4451 if (!ItinData->isEmpty() && ItinData->getNumMicroOps(Class) < 0)
4452 return getNumMicroOps(ItinData, MI);
4454 // For the common case, fall back on the itinerary's latency.
4455 unsigned Latency = ItinData->getStageLatency(Class);
4457 // Adjust for dynamic def-side opcode variants not captured by the itinerary.
4459 MI.hasOneMemOperand() ? (*MI.memoperands_begin())->getAlignment() : 0;
4460 int Adj = adjustDefLatency(Subtarget, MI, MCID, DefAlign);
4461 if (Adj >= 0 || (int)Latency > -Adj) {
4462 return Latency + Adj;
4467 int ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
4468 SDNode *Node) const {
4469 if (!Node->isMachineOpcode())
4472 if (!ItinData || ItinData->isEmpty())
4475 unsigned Opcode = Node->getMachineOpcode();
4478 return ItinData->getStageLatency(get(Opcode).getSchedClass());
4485 bool ARMBaseInstrInfo::hasHighOperandLatency(const TargetSchedModel &SchedModel,
4486 const MachineRegisterInfo *MRI,
4487 const MachineInstr &DefMI,
4489 const MachineInstr &UseMI,
4490 unsigned UseIdx) const {
4491 unsigned DDomain = DefMI.getDesc().TSFlags & ARMII::DomainMask;
4492 unsigned UDomain = UseMI.getDesc().TSFlags & ARMII::DomainMask;
4493 if (Subtarget.nonpipelinedVFP() &&
4494 (DDomain == ARMII::DomainVFP || UDomain == ARMII::DomainVFP))
4497 // Hoist VFP / NEON instructions with 4 or higher latency.
4499 SchedModel.computeOperandLatency(&DefMI, DefIdx, &UseMI, UseIdx);
4502 return DDomain == ARMII::DomainVFP || DDomain == ARMII::DomainNEON ||
4503 UDomain == ARMII::DomainVFP || UDomain == ARMII::DomainNEON;
4506 bool ARMBaseInstrInfo::hasLowDefLatency(const TargetSchedModel &SchedModel,
4507 const MachineInstr &DefMI,
4508 unsigned DefIdx) const {
4509 const InstrItineraryData *ItinData = SchedModel.getInstrItineraries();
4510 if (!ItinData || ItinData->isEmpty())
4513 unsigned DDomain = DefMI.getDesc().TSFlags & ARMII::DomainMask;
4514 if (DDomain == ARMII::DomainGeneral) {
4515 unsigned DefClass = DefMI.getDesc().getSchedClass();
4516 int DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
4517 return (DefCycle != -1 && DefCycle <= 2);
4522 bool ARMBaseInstrInfo::verifyInstruction(const MachineInstr &MI,
4523 StringRef &ErrInfo) const {
4524 if (convertAddSubFlagsOpcode(MI.getOpcode())) {
4525 ErrInfo = "Pseudo flag setting opcodes only exist in Selection DAG";
4531 // LoadStackGuard has so far only been implemented for MachO. Different code
4532 // sequence is needed for other targets.
4533 void ARMBaseInstrInfo::expandLoadStackGuardBase(MachineBasicBlock::iterator MI,
4534 unsigned LoadImmOpc,
4535 unsigned LoadOpc) const {
4536 assert(!Subtarget.isROPI() && !Subtarget.isRWPI() &&
4537 "ROPI/RWPI not currently supported with stack guard");
4539 MachineBasicBlock &MBB = *MI->getParent();
4540 DebugLoc DL = MI->getDebugLoc();
4541 unsigned Reg = MI->getOperand(0).getReg();
4542 const GlobalValue *GV =
4543 cast<GlobalValue>((*MI->memoperands_begin())->getValue());
4544 MachineInstrBuilder MIB;
4546 BuildMI(MBB, MI, DL, get(LoadImmOpc), Reg)
4547 .addGlobalAddress(GV, 0, ARMII::MO_NONLAZY);
4549 if (Subtarget.isGVIndirectSymbol(GV)) {
4550 MIB = BuildMI(MBB, MI, DL, get(LoadOpc), Reg);
4551 MIB.addReg(Reg, RegState::Kill).addImm(0);
4552 auto Flags = MachineMemOperand::MOLoad |
4553 MachineMemOperand::MODereferenceable |
4554 MachineMemOperand::MOInvariant;
4555 MachineMemOperand *MMO = MBB.getParent()->getMachineMemOperand(
4556 MachinePointerInfo::getGOT(*MBB.getParent()), Flags, 4, 4);
4557 MIB.addMemOperand(MMO).add(predOps(ARMCC::AL));
4560 MIB = BuildMI(MBB, MI, DL, get(LoadOpc), Reg);
4561 MIB.addReg(Reg, RegState::Kill)
4564 .add(predOps(ARMCC::AL));
4568 ARMBaseInstrInfo::isFpMLxInstruction(unsigned Opcode, unsigned &MulOpc,
4569 unsigned &AddSubOpc,
4570 bool &NegAcc, bool &HasLane) const {
4571 DenseMap<unsigned, unsigned>::const_iterator I = MLxEntryMap.find(Opcode);
4572 if (I == MLxEntryMap.end())
4575 const ARM_MLxEntry &Entry = ARM_MLxTable[I->second];
4576 MulOpc = Entry.MulOpc;
4577 AddSubOpc = Entry.AddSubOpc;
4578 NegAcc = Entry.NegAcc;
4579 HasLane = Entry.HasLane;
4583 //===----------------------------------------------------------------------===//
4584 // Execution domains.
4585 //===----------------------------------------------------------------------===//
4587 // Some instructions go down the NEON pipeline, some go down the VFP pipeline,
4588 // and some can go down both. The vmov instructions go down the VFP pipeline,
4589 // but they can be changed to vorr equivalents that are executed by the NEON
4592 // We use the following execution domain numbering:
4601 // Also see ARMInstrFormats.td and Domain* enums in ARMBaseInfo.h
4603 std::pair<uint16_t, uint16_t>
4604 ARMBaseInstrInfo::getExecutionDomain(const MachineInstr &MI) const {
4605 // If we don't have access to NEON instructions then we won't be able
4606 // to swizzle anything to the NEON domain. Check to make sure.
4607 if (Subtarget.hasNEON()) {
4608 // VMOVD, VMOVRS and VMOVSR are VFP instructions, but can be changed to NEON
4609 // if they are not predicated.
4610 if (MI.getOpcode() == ARM::VMOVD && !isPredicated(MI))
4611 return std::make_pair(ExeVFP, (1 << ExeVFP) | (1 << ExeNEON));
4613 // CortexA9 is particularly picky about mixing the two and wants these
4615 if (Subtarget.useNEONForFPMovs() && !isPredicated(MI) &&
4616 (MI.getOpcode() == ARM::VMOVRS || MI.getOpcode() == ARM::VMOVSR ||
4617 MI.getOpcode() == ARM::VMOVS))
4618 return std::make_pair(ExeVFP, (1 << ExeVFP) | (1 << ExeNEON));
4620 // No other instructions can be swizzled, so just determine their domain.
4621 unsigned Domain = MI.getDesc().TSFlags & ARMII::DomainMask;
4623 if (Domain & ARMII::DomainNEON)
4624 return std::make_pair(ExeNEON, 0);
4626 // Certain instructions can go either way on Cortex-A8.
4627 // Treat them as NEON instructions.
4628 if ((Domain & ARMII::DomainNEONA8) && Subtarget.isCortexA8())
4629 return std::make_pair(ExeNEON, 0);
4631 if (Domain & ARMII::DomainVFP)
4632 return std::make_pair(ExeVFP, 0);
4634 return std::make_pair(ExeGeneric, 0);
4637 static unsigned getCorrespondingDRegAndLane(const TargetRegisterInfo *TRI,
4638 unsigned SReg, unsigned &Lane) {
4639 unsigned DReg = TRI->getMatchingSuperReg(SReg, ARM::ssub_0, &ARM::DPRRegClass);
4642 if (DReg != ARM::NoRegister)
4646 DReg = TRI->getMatchingSuperReg(SReg, ARM::ssub_1, &ARM::DPRRegClass);
4648 assert(DReg && "S-register with no D super-register?");
4652 /// getImplicitSPRUseForDPRUse - Given a use of a DPR register and lane,
4653 /// set ImplicitSReg to a register number that must be marked as implicit-use or
4654 /// zero if no register needs to be defined as implicit-use.
4656 /// If the function cannot determine if an SPR should be marked implicit use or
4657 /// not, it returns false.
4659 /// This function handles cases where an instruction is being modified from taking
4660 /// an SPR to a DPR[Lane]. A use of the DPR is being added, which may conflict
4661 /// with an earlier def of an SPR corresponding to DPR[Lane^1] (i.e. the other
4662 /// lane of the DPR).
4664 /// If the other SPR is defined, an implicit-use of it should be added. Else,
4665 /// (including the case where the DPR itself is defined), it should not.
4667 static bool getImplicitSPRUseForDPRUse(const TargetRegisterInfo *TRI,
4668 MachineInstr &MI, unsigned DReg,
4669 unsigned Lane, unsigned &ImplicitSReg) {
4670 // If the DPR is defined or used already, the other SPR lane will be chained
4671 // correctly, so there is nothing to be done.
4672 if (MI.definesRegister(DReg, TRI) || MI.readsRegister(DReg, TRI)) {
4677 // Otherwise we need to go searching to see if the SPR is set explicitly.
4678 ImplicitSReg = TRI->getSubReg(DReg,
4679 (Lane & 1) ? ARM::ssub_0 : ARM::ssub_1);
4680 MachineBasicBlock::LivenessQueryResult LQR =
4681 MI.getParent()->computeRegisterLiveness(TRI, ImplicitSReg, MI);
4683 if (LQR == MachineBasicBlock::LQR_Live)
4685 else if (LQR == MachineBasicBlock::LQR_Unknown)
4688 // If the register is known not to be live, there is no need to add an
4694 void ARMBaseInstrInfo::setExecutionDomain(MachineInstr &MI,
4695 unsigned Domain) const {
4696 unsigned DstReg, SrcReg, DReg;
4698 MachineInstrBuilder MIB(*MI.getParent()->getParent(), MI);
4699 const TargetRegisterInfo *TRI = &getRegisterInfo();
4700 switch (MI.getOpcode()) {
4702 llvm_unreachable("cannot handle opcode!");
4705 if (Domain != ExeNEON)
4708 // Zap the predicate operands.
4709 assert(!isPredicated(MI) && "Cannot predicate a VORRd");
4711 // Make sure we've got NEON instructions.
4712 assert(Subtarget.hasNEON() && "VORRd requires NEON");
4714 // Source instruction is %DDst = VMOVD %DSrc, 14, %noreg (; implicits)
4715 DstReg = MI.getOperand(0).getReg();
4716 SrcReg = MI.getOperand(1).getReg();
4718 for (unsigned i = MI.getDesc().getNumOperands(); i; --i)
4719 MI.RemoveOperand(i - 1);
4721 // Change to a %DDst = VORRd %DSrc, %DSrc, 14, %noreg (; implicits)
4722 MI.setDesc(get(ARM::VORRd));
4723 MIB.addReg(DstReg, RegState::Define)
4726 .add(predOps(ARMCC::AL));
4729 if (Domain != ExeNEON)
4731 assert(!isPredicated(MI) && "Cannot predicate a VGETLN");
4733 // Source instruction is %RDst = VMOVRS %SSrc, 14, %noreg (; implicits)
4734 DstReg = MI.getOperand(0).getReg();
4735 SrcReg = MI.getOperand(1).getReg();
4737 for (unsigned i = MI.getDesc().getNumOperands(); i; --i)
4738 MI.RemoveOperand(i - 1);
4740 DReg = getCorrespondingDRegAndLane(TRI, SrcReg, Lane);
4742 // Convert to %RDst = VGETLNi32 %DSrc, Lane, 14, %noreg (; imps)
4743 // Note that DSrc has been widened and the other lane may be undef, which
4744 // contaminates the entire register.
4745 MI.setDesc(get(ARM::VGETLNi32));
4746 MIB.addReg(DstReg, RegState::Define)
4747 .addReg(DReg, RegState::Undef)
4749 .add(predOps(ARMCC::AL));
4751 // The old source should be an implicit use, otherwise we might think it
4752 // was dead before here.
4753 MIB.addReg(SrcReg, RegState::Implicit);
4756 if (Domain != ExeNEON)
4758 assert(!isPredicated(MI) && "Cannot predicate a VSETLN");
4760 // Source instruction is %SDst = VMOVSR %RSrc, 14, %noreg (; implicits)
4761 DstReg = MI.getOperand(0).getReg();
4762 SrcReg = MI.getOperand(1).getReg();
4764 DReg = getCorrespondingDRegAndLane(TRI, DstReg, Lane);
4766 unsigned ImplicitSReg;
4767 if (!getImplicitSPRUseForDPRUse(TRI, MI, DReg, Lane, ImplicitSReg))
4770 for (unsigned i = MI.getDesc().getNumOperands(); i; --i)
4771 MI.RemoveOperand(i - 1);
4773 // Convert to %DDst = VSETLNi32 %DDst, %RSrc, Lane, 14, %noreg (; imps)
4774 // Again DDst may be undefined at the beginning of this instruction.
4775 MI.setDesc(get(ARM::VSETLNi32));
4776 MIB.addReg(DReg, RegState::Define)
4777 .addReg(DReg, getUndefRegState(!MI.readsRegister(DReg, TRI)))
4780 .add(predOps(ARMCC::AL));
4782 // The narrower destination must be marked as set to keep previous chains
4784 MIB.addReg(DstReg, RegState::Define | RegState::Implicit);
4785 if (ImplicitSReg != 0)
4786 MIB.addReg(ImplicitSReg, RegState::Implicit);
4790 if (Domain != ExeNEON)
4793 // Source instruction is %SDst = VMOVS %SSrc, 14, %noreg (; implicits)
4794 DstReg = MI.getOperand(0).getReg();
4795 SrcReg = MI.getOperand(1).getReg();
4797 unsigned DstLane = 0, SrcLane = 0, DDst, DSrc;
4798 DDst = getCorrespondingDRegAndLane(TRI, DstReg, DstLane);
4799 DSrc = getCorrespondingDRegAndLane(TRI, SrcReg, SrcLane);
4801 unsigned ImplicitSReg;
4802 if (!getImplicitSPRUseForDPRUse(TRI, MI, DSrc, SrcLane, ImplicitSReg))
4805 for (unsigned i = MI.getDesc().getNumOperands(); i; --i)
4806 MI.RemoveOperand(i - 1);
4809 // Destination can be:
4810 // %DDst = VDUPLN32d %DDst, Lane, 14, %noreg (; implicits)
4811 MI.setDesc(get(ARM::VDUPLN32d));
4812 MIB.addReg(DDst, RegState::Define)
4813 .addReg(DDst, getUndefRegState(!MI.readsRegister(DDst, TRI)))
4815 .add(predOps(ARMCC::AL));
4817 // Neither the source or the destination are naturally represented any
4818 // more, so add them in manually.
4819 MIB.addReg(DstReg, RegState::Implicit | RegState::Define);
4820 MIB.addReg(SrcReg, RegState::Implicit);
4821 if (ImplicitSReg != 0)
4822 MIB.addReg(ImplicitSReg, RegState::Implicit);
4826 // In general there's no single instruction that can perform an S <-> S
4827 // move in NEON space, but a pair of VEXT instructions *can* do the
4828 // job. It turns out that the VEXTs needed will only use DSrc once, with
4829 // the position based purely on the combination of lane-0 and lane-1
4830 // involved. For example
4831 // vmov s0, s2 -> vext.32 d0, d0, d1, #1 vext.32 d0, d0, d0, #1
4832 // vmov s1, s3 -> vext.32 d0, d1, d0, #1 vext.32 d0, d0, d0, #1
4833 // vmov s0, s3 -> vext.32 d0, d0, d0, #1 vext.32 d0, d1, d0, #1
4834 // vmov s1, s2 -> vext.32 d0, d0, d0, #1 vext.32 d0, d0, d1, #1
4836 // Pattern of the MachineInstrs is:
4837 // %DDst = VEXTd32 %DSrc1, %DSrc2, Lane, 14, %noreg (;implicits)
4838 MachineInstrBuilder NewMIB;
4839 NewMIB = BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), get(ARM::VEXTd32),
4842 // On the first instruction, both DSrc and DDst may be undef if present.
4843 // Specifically when the original instruction didn't have them as an
4845 unsigned CurReg = SrcLane == 1 && DstLane == 1 ? DSrc : DDst;
4846 bool CurUndef = !MI.readsRegister(CurReg, TRI);
4847 NewMIB.addReg(CurReg, getUndefRegState(CurUndef));
4849 CurReg = SrcLane == 0 && DstLane == 0 ? DSrc : DDst;
4850 CurUndef = !MI.readsRegister(CurReg, TRI);
4851 NewMIB.addReg(CurReg, getUndefRegState(CurUndef))
4853 .add(predOps(ARMCC::AL));
4855 if (SrcLane == DstLane)
4856 NewMIB.addReg(SrcReg, RegState::Implicit);
4858 MI.setDesc(get(ARM::VEXTd32));
4859 MIB.addReg(DDst, RegState::Define);
4861 // On the second instruction, DDst has definitely been defined above, so
4862 // it is not undef. DSrc, if present, can be undef as above.
4863 CurReg = SrcLane == 1 && DstLane == 0 ? DSrc : DDst;
4864 CurUndef = CurReg == DSrc && !MI.readsRegister(CurReg, TRI);
4865 MIB.addReg(CurReg, getUndefRegState(CurUndef));
4867 CurReg = SrcLane == 0 && DstLane == 1 ? DSrc : DDst;
4868 CurUndef = CurReg == DSrc && !MI.readsRegister(CurReg, TRI);
4869 MIB.addReg(CurReg, getUndefRegState(CurUndef))
4871 .add(predOps(ARMCC::AL));
4873 if (SrcLane != DstLane)
4874 MIB.addReg(SrcReg, RegState::Implicit);
4876 // As before, the original destination is no longer represented, add it
4878 MIB.addReg(DstReg, RegState::Define | RegState::Implicit);
4879 if (ImplicitSReg != 0)
4880 MIB.addReg(ImplicitSReg, RegState::Implicit);
4886 //===----------------------------------------------------------------------===//
4887 // Partial register updates
4888 //===----------------------------------------------------------------------===//
4890 // Swift renames NEON registers with 64-bit granularity. That means any
4891 // instruction writing an S-reg implicitly reads the containing D-reg. The
4892 // problem is mostly avoided by translating f32 operations to v2f32 operations
4893 // on D-registers, but f32 loads are still a problem.
4895 // These instructions can load an f32 into a NEON register:
4897 // VLDRS - Only writes S, partial D update.
4898 // VLD1LNd32 - Writes all D-regs, explicit partial D update, 2 uops.
4899 // VLD1DUPd32 - Writes all D-regs, no partial reg update, 2 uops.
4901 // FCONSTD can be used as a dependency-breaking instruction.
4902 unsigned ARMBaseInstrInfo::getPartialRegUpdateClearance(
4903 const MachineInstr &MI, unsigned OpNum,
4904 const TargetRegisterInfo *TRI) const {
4905 auto PartialUpdateClearance = Subtarget.getPartialUpdateClearance();
4906 if (!PartialUpdateClearance)
4909 assert(TRI && "Need TRI instance");
4911 const MachineOperand &MO = MI.getOperand(OpNum);
4914 unsigned Reg = MO.getReg();
4917 switch (MI.getOpcode()) {
4918 // Normal instructions writing only an S-register.
4923 case ARM::VMOVv4i16:
4924 case ARM::VMOVv2i32:
4925 case ARM::VMOVv2f32:
4926 case ARM::VMOVv1i64:
4927 UseOp = MI.findRegisterUseOperandIdx(Reg, false, TRI);
4930 // Explicitly reads the dependency.
4931 case ARM::VLD1LNd32:
4938 // If this instruction actually reads a value from Reg, there is no unwanted
4940 if (UseOp != -1 && MI.getOperand(UseOp).readsReg())
4943 // We must be able to clobber the whole D-reg.
4944 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
4945 // Virtual register must be a def undef foo:ssub_0 operand.
4946 if (!MO.getSubReg() || MI.readsVirtualRegister(Reg))
4948 } else if (ARM::SPRRegClass.contains(Reg)) {
4949 // Physical register: MI must define the full D-reg.
4950 unsigned DReg = TRI->getMatchingSuperReg(Reg, ARM::ssub_0,
4952 if (!DReg || !MI.definesRegister(DReg, TRI))
4956 // MI has an unwanted D-register dependency.
4957 // Avoid defs in the previous N instructrions.
4958 return PartialUpdateClearance;
4961 // Break a partial register dependency after getPartialRegUpdateClearance
4962 // returned non-zero.
4963 void ARMBaseInstrInfo::breakPartialRegDependency(
4964 MachineInstr &MI, unsigned OpNum, const TargetRegisterInfo *TRI) const {
4965 assert(OpNum < MI.getDesc().getNumDefs() && "OpNum is not a def");
4966 assert(TRI && "Need TRI instance");
4968 const MachineOperand &MO = MI.getOperand(OpNum);
4969 unsigned Reg = MO.getReg();
4970 assert(TargetRegisterInfo::isPhysicalRegister(Reg) &&
4971 "Can't break virtual register dependencies.");
4972 unsigned DReg = Reg;
4974 // If MI defines an S-reg, find the corresponding D super-register.
4975 if (ARM::SPRRegClass.contains(Reg)) {
4976 DReg = ARM::D0 + (Reg - ARM::S0) / 2;
4977 assert(TRI->isSuperRegister(Reg, DReg) && "Register enums broken");
4980 assert(ARM::DPRRegClass.contains(DReg) && "Can only break D-reg deps");
4981 assert(MI.definesRegister(DReg, TRI) && "MI doesn't clobber full D-reg");
4983 // FIXME: In some cases, VLDRS can be changed to a VLD1DUPd32 which defines
4984 // the full D-register by loading the same value to both lanes. The
4985 // instruction is micro-coded with 2 uops, so don't do this until we can
4986 // properly schedule micro-coded instructions. The dispatcher stalls cause
4987 // too big regressions.
4989 // Insert the dependency-breaking FCONSTD before MI.
4990 // 96 is the encoding of 0.5, but the actual value doesn't matter here.
4991 BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), get(ARM::FCONSTD), DReg)
4993 .add(predOps(ARMCC::AL));
4994 MI.addRegisterKilled(DReg, TRI, true);
4997 bool ARMBaseInstrInfo::hasNOP() const {
4998 return Subtarget.getFeatureBits()[ARM::HasV6KOps];
5001 bool ARMBaseInstrInfo::isSwiftFastImmShift(const MachineInstr *MI) const {
5002 if (MI->getNumOperands() < 4)
5004 unsigned ShOpVal = MI->getOperand(3).getImm();
5005 unsigned ShImm = ARM_AM::getSORegOffset(ShOpVal);
5006 // Swift supports faster shifts for: lsl 2, lsl 1, and lsr 1.
5007 if ((ShImm == 1 && ARM_AM::getSORegShOp(ShOpVal) == ARM_AM::lsr) ||
5008 ((ShImm == 1 || ShImm == 2) &&
5009 ARM_AM::getSORegShOp(ShOpVal) == ARM_AM::lsl))
5015 bool ARMBaseInstrInfo::getRegSequenceLikeInputs(
5016 const MachineInstr &MI, unsigned DefIdx,
5017 SmallVectorImpl<RegSubRegPairAndIdx> &InputRegs) const {
5018 assert(DefIdx < MI.getDesc().getNumDefs() && "Invalid definition index");
5019 assert(MI.isRegSequenceLike() && "Invalid kind of instruction");
5021 switch (MI.getOpcode()) {
5023 // dX = VMOVDRR rY, rZ
5025 // dX = REG_SEQUENCE rY, ssub_0, rZ, ssub_1
5026 // Populate the InputRegs accordingly.
5028 const MachineOperand *MOReg = &MI.getOperand(1);
5029 if (!MOReg->isUndef())
5030 InputRegs.push_back(RegSubRegPairAndIdx(MOReg->getReg(),
5031 MOReg->getSubReg(), ARM::ssub_0));
5033 MOReg = &MI.getOperand(2);
5034 if (!MOReg->isUndef())
5035 InputRegs.push_back(RegSubRegPairAndIdx(MOReg->getReg(),
5036 MOReg->getSubReg(), ARM::ssub_1));
5039 llvm_unreachable("Target dependent opcode missing");
5042 bool ARMBaseInstrInfo::getExtractSubregLikeInputs(
5043 const MachineInstr &MI, unsigned DefIdx,
5044 RegSubRegPairAndIdx &InputReg) const {
5045 assert(DefIdx < MI.getDesc().getNumDefs() && "Invalid definition index");
5046 assert(MI.isExtractSubregLike() && "Invalid kind of instruction");
5048 switch (MI.getOpcode()) {
5050 // rX, rY = VMOVRRD dZ
5052 // rX = EXTRACT_SUBREG dZ, ssub_0
5053 // rY = EXTRACT_SUBREG dZ, ssub_1
5054 const MachineOperand &MOReg = MI.getOperand(2);
5055 if (MOReg.isUndef())
5057 InputReg.Reg = MOReg.getReg();
5058 InputReg.SubReg = MOReg.getSubReg();
5059 InputReg.SubIdx = DefIdx == 0 ? ARM::ssub_0 : ARM::ssub_1;
5062 llvm_unreachable("Target dependent opcode missing");
5065 bool ARMBaseInstrInfo::getInsertSubregLikeInputs(
5066 const MachineInstr &MI, unsigned DefIdx, RegSubRegPair &BaseReg,
5067 RegSubRegPairAndIdx &InsertedReg) const {
5068 assert(DefIdx < MI.getDesc().getNumDefs() && "Invalid definition index");
5069 assert(MI.isInsertSubregLike() && "Invalid kind of instruction");
5071 switch (MI.getOpcode()) {
5072 case ARM::VSETLNi32:
5073 // dX = VSETLNi32 dY, rZ, imm
5074 const MachineOperand &MOBaseReg = MI.getOperand(1);
5075 const MachineOperand &MOInsertedReg = MI.getOperand(2);
5076 if (MOInsertedReg.isUndef())
5078 const MachineOperand &MOIndex = MI.getOperand(3);
5079 BaseReg.Reg = MOBaseReg.getReg();
5080 BaseReg.SubReg = MOBaseReg.getSubReg();
5082 InsertedReg.Reg = MOInsertedReg.getReg();
5083 InsertedReg.SubReg = MOInsertedReg.getSubReg();
5084 InsertedReg.SubIdx = MOIndex.getImm() == 0 ? ARM::ssub_0 : ARM::ssub_1;
5087 llvm_unreachable("Target dependent opcode missing");
5090 std::pair<unsigned, unsigned>
5091 ARMBaseInstrInfo::decomposeMachineOperandsTargetFlags(unsigned TF) const {
5092 const unsigned Mask = ARMII::MO_OPTION_MASK;
5093 return std::make_pair(TF & Mask, TF & ~Mask);
5096 ArrayRef<std::pair<unsigned, const char *>>
5097 ARMBaseInstrInfo::getSerializableDirectMachineOperandTargetFlags() const {
5098 using namespace ARMII;
5100 static const std::pair<unsigned, const char *> TargetFlags[] = {
5101 {MO_LO16, "arm-lo16"}, {MO_HI16, "arm-hi16"}};
5102 return makeArrayRef(TargetFlags);
5105 ArrayRef<std::pair<unsigned, const char *>>
5106 ARMBaseInstrInfo::getSerializableBitmaskMachineOperandTargetFlags() const {
5107 using namespace ARMII;
5109 static const std::pair<unsigned, const char *> TargetFlags[] = {
5110 {MO_COFFSTUB, "arm-coffstub"},
5111 {MO_GOT, "arm-got"},
5112 {MO_SBREL, "arm-sbrel"},
5113 {MO_DLLIMPORT, "arm-dllimport"},
5114 {MO_SECREL, "arm-secrel"},
5115 {MO_NONLAZY, "arm-nonlazy"}};
5116 return makeArrayRef(TargetFlags);