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/TargetSchedule.h"
41 #include "llvm/IR/Attributes.h"
42 #include "llvm/IR/Constants.h"
43 #include "llvm/IR/DebugLoc.h"
44 #include "llvm/IR/Function.h"
45 #include "llvm/IR/GlobalValue.h"
46 #include "llvm/MC/MCAsmInfo.h"
47 #include "llvm/MC/MCInstrDesc.h"
48 #include "llvm/MC/MCInstrItineraries.h"
49 #include "llvm/Support/BranchProbability.h"
50 #include "llvm/Support/Casting.h"
51 #include "llvm/Support/CommandLine.h"
52 #include "llvm/Support/Compiler.h"
53 #include "llvm/Support/Debug.h"
54 #include "llvm/Support/ErrorHandling.h"
55 #include "llvm/Support/raw_ostream.h"
56 #include "llvm/Target/TargetInstrInfo.h"
57 #include "llvm/Target/TargetMachine.h"
58 #include "llvm/Target/TargetRegisterInfo.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->isDebugValue() || !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 return getInlineAsmLength(MI.getOperand(0).getSymbolName(), *MAI);
713 unsigned Opc = MI.getOpcode();
716 // pseudo-instruction sizes are zero.
718 case TargetOpcode::BUNDLE:
719 return getInstBundleLength(MI);
720 case ARM::MOVi16_ga_pcrel:
721 case ARM::MOVTi16_ga_pcrel:
722 case ARM::t2MOVi16_ga_pcrel:
723 case ARM::t2MOVTi16_ga_pcrel:
726 case ARM::t2MOVi32imm:
728 case ARM::CONSTPOOL_ENTRY:
729 case ARM::JUMPTABLE_INSTS:
730 case ARM::JUMPTABLE_ADDRS:
731 case ARM::JUMPTABLE_TBB:
732 case ARM::JUMPTABLE_TBH:
733 // If this machine instr is a constant pool entry, its size is recorded as
735 return MI.getOperand(2).getImm();
736 case ARM::Int_eh_sjlj_longjmp:
738 case ARM::tInt_eh_sjlj_longjmp:
740 case ARM::tInt_WIN_eh_sjlj_longjmp:
742 case ARM::Int_eh_sjlj_setjmp:
743 case ARM::Int_eh_sjlj_setjmp_nofp:
745 case ARM::tInt_eh_sjlj_setjmp:
746 case ARM::t2Int_eh_sjlj_setjmp:
747 case ARM::t2Int_eh_sjlj_setjmp_nofp:
750 return MI.getOperand(1).getImm();
754 unsigned ARMBaseInstrInfo::getInstBundleLength(const MachineInstr &MI) const {
756 MachineBasicBlock::const_instr_iterator I = MI.getIterator();
757 MachineBasicBlock::const_instr_iterator E = MI.getParent()->instr_end();
758 while (++I != E && I->isInsideBundle()) {
759 assert(!I->isBundle() && "No nested bundle!");
760 Size += getInstSizeInBytes(*I);
765 void ARMBaseInstrInfo::copyFromCPSR(MachineBasicBlock &MBB,
766 MachineBasicBlock::iterator I,
767 unsigned DestReg, bool KillSrc,
768 const ARMSubtarget &Subtarget) const {
769 unsigned Opc = Subtarget.isThumb()
770 ? (Subtarget.isMClass() ? ARM::t2MRS_M : ARM::t2MRS_AR)
773 MachineInstrBuilder MIB =
774 BuildMI(MBB, I, I->getDebugLoc(), get(Opc), DestReg);
776 // There is only 1 A/R class MRS instruction, and it always refers to
777 // APSR. However, there are lots of other possibilities on M-class cores.
778 if (Subtarget.isMClass())
781 MIB.add(predOps(ARMCC::AL))
782 .addReg(ARM::CPSR, RegState::Implicit | getKillRegState(KillSrc));
785 void ARMBaseInstrInfo::copyToCPSR(MachineBasicBlock &MBB,
786 MachineBasicBlock::iterator I,
787 unsigned SrcReg, bool KillSrc,
788 const ARMSubtarget &Subtarget) const {
789 unsigned Opc = Subtarget.isThumb()
790 ? (Subtarget.isMClass() ? ARM::t2MSR_M : ARM::t2MSR_AR)
793 MachineInstrBuilder MIB = BuildMI(MBB, I, I->getDebugLoc(), get(Opc));
795 if (Subtarget.isMClass())
800 MIB.addReg(SrcReg, getKillRegState(KillSrc))
801 .add(predOps(ARMCC::AL))
802 .addReg(ARM::CPSR, RegState::Implicit | RegState::Define);
805 void ARMBaseInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
806 MachineBasicBlock::iterator I,
807 const DebugLoc &DL, unsigned DestReg,
808 unsigned SrcReg, bool KillSrc) const {
809 bool GPRDest = ARM::GPRRegClass.contains(DestReg);
810 bool GPRSrc = ARM::GPRRegClass.contains(SrcReg);
812 if (GPRDest && GPRSrc) {
813 BuildMI(MBB, I, DL, get(ARM::MOVr), DestReg)
814 .addReg(SrcReg, getKillRegState(KillSrc))
815 .add(predOps(ARMCC::AL))
820 bool SPRDest = ARM::SPRRegClass.contains(DestReg);
821 bool SPRSrc = ARM::SPRRegClass.contains(SrcReg);
824 if (SPRDest && SPRSrc)
826 else if (GPRDest && SPRSrc)
828 else if (SPRDest && GPRSrc)
830 else if (ARM::DPRRegClass.contains(DestReg, SrcReg) && !Subtarget.isFPOnlySP())
832 else if (ARM::QPRRegClass.contains(DestReg, SrcReg))
836 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(Opc), DestReg);
837 MIB.addReg(SrcReg, getKillRegState(KillSrc));
838 if (Opc == ARM::VORRq)
839 MIB.addReg(SrcReg, getKillRegState(KillSrc));
840 MIB.add(predOps(ARMCC::AL));
844 // Handle register classes that require multiple instructions.
845 unsigned BeginIdx = 0;
846 unsigned SubRegs = 0;
849 // Use VORRq when possible.
850 if (ARM::QQPRRegClass.contains(DestReg, SrcReg)) {
852 BeginIdx = ARM::qsub_0;
854 } else if (ARM::QQQQPRRegClass.contains(DestReg, SrcReg)) {
856 BeginIdx = ARM::qsub_0;
858 // Fall back to VMOVD.
859 } else if (ARM::DPairRegClass.contains(DestReg, SrcReg)) {
861 BeginIdx = ARM::dsub_0;
863 } else if (ARM::DTripleRegClass.contains(DestReg, SrcReg)) {
865 BeginIdx = ARM::dsub_0;
867 } else if (ARM::DQuadRegClass.contains(DestReg, SrcReg)) {
869 BeginIdx = ARM::dsub_0;
871 } else if (ARM::GPRPairRegClass.contains(DestReg, SrcReg)) {
872 Opc = Subtarget.isThumb2() ? ARM::tMOVr : ARM::MOVr;
873 BeginIdx = ARM::gsub_0;
875 } else if (ARM::DPairSpcRegClass.contains(DestReg, SrcReg)) {
877 BeginIdx = ARM::dsub_0;
880 } else if (ARM::DTripleSpcRegClass.contains(DestReg, SrcReg)) {
882 BeginIdx = ARM::dsub_0;
885 } else if (ARM::DQuadSpcRegClass.contains(DestReg, SrcReg)) {
887 BeginIdx = ARM::dsub_0;
890 } else if (ARM::DPRRegClass.contains(DestReg, SrcReg) && Subtarget.isFPOnlySP()) {
892 BeginIdx = ARM::ssub_0;
894 } else if (SrcReg == ARM::CPSR) {
895 copyFromCPSR(MBB, I, DestReg, KillSrc, Subtarget);
897 } else if (DestReg == ARM::CPSR) {
898 copyToCPSR(MBB, I, SrcReg, KillSrc, Subtarget);
902 assert(Opc && "Impossible reg-to-reg copy");
904 const TargetRegisterInfo *TRI = &getRegisterInfo();
905 MachineInstrBuilder Mov;
907 // Copy register tuples backward when the first Dest reg overlaps with SrcReg.
908 if (TRI->regsOverlap(SrcReg, TRI->getSubReg(DestReg, BeginIdx))) {
909 BeginIdx = BeginIdx + ((SubRegs - 1) * Spacing);
913 SmallSet<unsigned, 4> DstRegs;
915 for (unsigned i = 0; i != SubRegs; ++i) {
916 unsigned Dst = TRI->getSubReg(DestReg, BeginIdx + i * Spacing);
917 unsigned Src = TRI->getSubReg(SrcReg, BeginIdx + i * Spacing);
918 assert(Dst && Src && "Bad sub-register");
920 assert(!DstRegs.count(Src) && "destructive vector copy");
923 Mov = BuildMI(MBB, I, I->getDebugLoc(), get(Opc), Dst).addReg(Src);
924 // VORR takes two source operands.
925 if (Opc == ARM::VORRq)
927 Mov = Mov.add(predOps(ARMCC::AL));
929 if (Opc == ARM::MOVr)
930 Mov = Mov.add(condCodeOp());
932 // Add implicit super-register defs and kills to the last instruction.
933 Mov->addRegisterDefined(DestReg, TRI);
935 Mov->addRegisterKilled(SrcReg, TRI);
938 const MachineInstrBuilder &
939 ARMBaseInstrInfo::AddDReg(MachineInstrBuilder &MIB, unsigned Reg,
940 unsigned SubIdx, unsigned State,
941 const TargetRegisterInfo *TRI) const {
943 return MIB.addReg(Reg, State);
945 if (TargetRegisterInfo::isPhysicalRegister(Reg))
946 return MIB.addReg(TRI->getSubReg(Reg, SubIdx), State);
947 return MIB.addReg(Reg, State, SubIdx);
950 void ARMBaseInstrInfo::
951 storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
952 unsigned SrcReg, bool isKill, int FI,
953 const TargetRegisterClass *RC,
954 const TargetRegisterInfo *TRI) const {
956 if (I != MBB.end()) DL = I->getDebugLoc();
957 MachineFunction &MF = *MBB.getParent();
958 MachineFrameInfo &MFI = MF.getFrameInfo();
959 unsigned Align = MFI.getObjectAlignment(FI);
961 MachineMemOperand *MMO = MF.getMachineMemOperand(
962 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOStore,
963 MFI.getObjectSize(FI), Align);
965 switch (TRI->getSpillSize(*RC)) {
967 if (ARM::GPRRegClass.hasSubClassEq(RC)) {
968 BuildMI(MBB, I, DL, get(ARM::STRi12))
969 .addReg(SrcReg, getKillRegState(isKill))
973 .add(predOps(ARMCC::AL));
974 } else if (ARM::SPRRegClass.hasSubClassEq(RC)) {
975 BuildMI(MBB, I, DL, get(ARM::VSTRS))
976 .addReg(SrcReg, getKillRegState(isKill))
980 .add(predOps(ARMCC::AL));
982 llvm_unreachable("Unknown reg class!");
985 if (ARM::DPRRegClass.hasSubClassEq(RC)) {
986 BuildMI(MBB, I, DL, get(ARM::VSTRD))
987 .addReg(SrcReg, getKillRegState(isKill))
991 .add(predOps(ARMCC::AL));
992 } else if (ARM::GPRPairRegClass.hasSubClassEq(RC)) {
993 if (Subtarget.hasV5TEOps()) {
994 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::STRD));
995 AddDReg(MIB, SrcReg, ARM::gsub_0, getKillRegState(isKill), TRI);
996 AddDReg(MIB, SrcReg, ARM::gsub_1, 0, TRI);
997 MIB.addFrameIndex(FI).addReg(0).addImm(0).addMemOperand(MMO)
998 .add(predOps(ARMCC::AL));
1000 // Fallback to STM instruction, which has existed since the dawn of
1002 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::STMIA))
1005 .add(predOps(ARMCC::AL));
1006 AddDReg(MIB, SrcReg, ARM::gsub_0, getKillRegState(isKill), TRI);
1007 AddDReg(MIB, SrcReg, ARM::gsub_1, 0, TRI);
1010 llvm_unreachable("Unknown reg class!");
1013 if (ARM::DPairRegClass.hasSubClassEq(RC)) {
1014 // Use aligned spills if the stack can be realigned.
1015 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
1016 BuildMI(MBB, I, DL, get(ARM::VST1q64))
1019 .addReg(SrcReg, getKillRegState(isKill))
1021 .add(predOps(ARMCC::AL));
1023 BuildMI(MBB, I, DL, get(ARM::VSTMQIA))
1024 .addReg(SrcReg, getKillRegState(isKill))
1027 .add(predOps(ARMCC::AL));
1030 llvm_unreachable("Unknown reg class!");
1033 if (ARM::DTripleRegClass.hasSubClassEq(RC)) {
1034 // Use aligned spills if the stack can be realigned.
1035 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
1036 BuildMI(MBB, I, DL, get(ARM::VST1d64TPseudo))
1039 .addReg(SrcReg, getKillRegState(isKill))
1041 .add(predOps(ARMCC::AL));
1043 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::VSTMDIA))
1045 .add(predOps(ARMCC::AL))
1046 .addMemOperand(MMO);
1047 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
1048 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
1049 AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
1052 llvm_unreachable("Unknown reg class!");
1055 if (ARM::QQPRRegClass.hasSubClassEq(RC) || ARM::DQuadRegClass.hasSubClassEq(RC)) {
1056 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
1057 // FIXME: It's possible to only store part of the QQ register if the
1058 // spilled def has a sub-register index.
1059 BuildMI(MBB, I, DL, get(ARM::VST1d64QPseudo))
1062 .addReg(SrcReg, getKillRegState(isKill))
1064 .add(predOps(ARMCC::AL));
1066 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::VSTMDIA))
1068 .add(predOps(ARMCC::AL))
1069 .addMemOperand(MMO);
1070 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
1071 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
1072 MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
1073 AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI);
1076 llvm_unreachable("Unknown reg class!");
1079 if (ARM::QQQQPRRegClass.hasSubClassEq(RC)) {
1080 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::VSTMDIA))
1082 .add(predOps(ARMCC::AL))
1083 .addMemOperand(MMO);
1084 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
1085 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
1086 MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
1087 MIB = AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI);
1088 MIB = AddDReg(MIB, SrcReg, ARM::dsub_4, 0, TRI);
1089 MIB = AddDReg(MIB, SrcReg, ARM::dsub_5, 0, TRI);
1090 MIB = AddDReg(MIB, SrcReg, ARM::dsub_6, 0, TRI);
1091 AddDReg(MIB, SrcReg, ARM::dsub_7, 0, TRI);
1093 llvm_unreachable("Unknown reg class!");
1096 llvm_unreachable("Unknown reg class!");
1100 unsigned ARMBaseInstrInfo::isStoreToStackSlot(const MachineInstr &MI,
1101 int &FrameIndex) const {
1102 switch (MI.getOpcode()) {
1105 case ARM::t2STRs: // FIXME: don't use t2STRs to access frame.
1106 if (MI.getOperand(1).isFI() && MI.getOperand(2).isReg() &&
1107 MI.getOperand(3).isImm() && MI.getOperand(2).getReg() == 0 &&
1108 MI.getOperand(3).getImm() == 0) {
1109 FrameIndex = MI.getOperand(1).getIndex();
1110 return MI.getOperand(0).getReg();
1118 if (MI.getOperand(1).isFI() && MI.getOperand(2).isImm() &&
1119 MI.getOperand(2).getImm() == 0) {
1120 FrameIndex = MI.getOperand(1).getIndex();
1121 return MI.getOperand(0).getReg();
1125 case ARM::VST1d64TPseudo:
1126 case ARM::VST1d64QPseudo:
1127 if (MI.getOperand(0).isFI() && MI.getOperand(2).getSubReg() == 0) {
1128 FrameIndex = MI.getOperand(0).getIndex();
1129 return MI.getOperand(2).getReg();
1133 if (MI.getOperand(1).isFI() && MI.getOperand(0).getSubReg() == 0) {
1134 FrameIndex = MI.getOperand(1).getIndex();
1135 return MI.getOperand(0).getReg();
1143 unsigned ARMBaseInstrInfo::isStoreToStackSlotPostFE(const MachineInstr &MI,
1144 int &FrameIndex) const {
1145 const MachineMemOperand *Dummy;
1146 return MI.mayStore() && hasStoreToStackSlot(MI, Dummy, FrameIndex);
1149 void ARMBaseInstrInfo::
1150 loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
1151 unsigned DestReg, int FI,
1152 const TargetRegisterClass *RC,
1153 const TargetRegisterInfo *TRI) const {
1155 if (I != MBB.end()) DL = I->getDebugLoc();
1156 MachineFunction &MF = *MBB.getParent();
1157 MachineFrameInfo &MFI = MF.getFrameInfo();
1158 unsigned Align = MFI.getObjectAlignment(FI);
1159 MachineMemOperand *MMO = MF.getMachineMemOperand(
1160 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOLoad,
1161 MFI.getObjectSize(FI), Align);
1163 switch (TRI->getSpillSize(*RC)) {
1165 if (ARM::GPRRegClass.hasSubClassEq(RC)) {
1166 BuildMI(MBB, I, DL, get(ARM::LDRi12), DestReg)
1170 .add(predOps(ARMCC::AL));
1172 } else if (ARM::SPRRegClass.hasSubClassEq(RC)) {
1173 BuildMI(MBB, I, DL, get(ARM::VLDRS), DestReg)
1177 .add(predOps(ARMCC::AL));
1179 llvm_unreachable("Unknown reg class!");
1182 if (ARM::DPRRegClass.hasSubClassEq(RC)) {
1183 BuildMI(MBB, I, DL, get(ARM::VLDRD), DestReg)
1187 .add(predOps(ARMCC::AL));
1188 } else if (ARM::GPRPairRegClass.hasSubClassEq(RC)) {
1189 MachineInstrBuilder MIB;
1191 if (Subtarget.hasV5TEOps()) {
1192 MIB = BuildMI(MBB, I, DL, get(ARM::LDRD));
1193 AddDReg(MIB, DestReg, ARM::gsub_0, RegState::DefineNoRead, TRI);
1194 AddDReg(MIB, DestReg, ARM::gsub_1, RegState::DefineNoRead, TRI);
1195 MIB.addFrameIndex(FI).addReg(0).addImm(0).addMemOperand(MMO)
1196 .add(predOps(ARMCC::AL));
1198 // Fallback to LDM instruction, which has existed since the dawn of
1200 MIB = BuildMI(MBB, I, DL, get(ARM::LDMIA))
1203 .add(predOps(ARMCC::AL));
1204 MIB = AddDReg(MIB, DestReg, ARM::gsub_0, RegState::DefineNoRead, TRI);
1205 MIB = AddDReg(MIB, DestReg, ARM::gsub_1, RegState::DefineNoRead, TRI);
1208 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1209 MIB.addReg(DestReg, RegState::ImplicitDefine);
1211 llvm_unreachable("Unknown reg class!");
1214 if (ARM::DPairRegClass.hasSubClassEq(RC)) {
1215 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
1216 BuildMI(MBB, I, DL, get(ARM::VLD1q64), DestReg)
1220 .add(predOps(ARMCC::AL));
1222 BuildMI(MBB, I, DL, get(ARM::VLDMQIA), DestReg)
1225 .add(predOps(ARMCC::AL));
1228 llvm_unreachable("Unknown reg class!");
1231 if (ARM::DTripleRegClass.hasSubClassEq(RC)) {
1232 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
1233 BuildMI(MBB, I, DL, get(ARM::VLD1d64TPseudo), DestReg)
1237 .add(predOps(ARMCC::AL));
1239 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
1242 .add(predOps(ARMCC::AL));
1243 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::DefineNoRead, TRI);
1244 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::DefineNoRead, TRI);
1245 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::DefineNoRead, TRI);
1246 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1247 MIB.addReg(DestReg, RegState::ImplicitDefine);
1250 llvm_unreachable("Unknown reg class!");
1253 if (ARM::QQPRRegClass.hasSubClassEq(RC) || ARM::DQuadRegClass.hasSubClassEq(RC)) {
1254 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
1255 BuildMI(MBB, I, DL, get(ARM::VLD1d64QPseudo), DestReg)
1259 .add(predOps(ARMCC::AL));
1261 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
1263 .add(predOps(ARMCC::AL))
1264 .addMemOperand(MMO);
1265 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::DefineNoRead, TRI);
1266 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::DefineNoRead, TRI);
1267 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::DefineNoRead, TRI);
1268 MIB = AddDReg(MIB, DestReg, ARM::dsub_3, RegState::DefineNoRead, TRI);
1269 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1270 MIB.addReg(DestReg, RegState::ImplicitDefine);
1273 llvm_unreachable("Unknown reg class!");
1276 if (ARM::QQQQPRRegClass.hasSubClassEq(RC)) {
1277 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
1279 .add(predOps(ARMCC::AL))
1280 .addMemOperand(MMO);
1281 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::DefineNoRead, TRI);
1282 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::DefineNoRead, TRI);
1283 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::DefineNoRead, TRI);
1284 MIB = AddDReg(MIB, DestReg, ARM::dsub_3, RegState::DefineNoRead, TRI);
1285 MIB = AddDReg(MIB, DestReg, ARM::dsub_4, RegState::DefineNoRead, TRI);
1286 MIB = AddDReg(MIB, DestReg, ARM::dsub_5, RegState::DefineNoRead, TRI);
1287 MIB = AddDReg(MIB, DestReg, ARM::dsub_6, RegState::DefineNoRead, TRI);
1288 MIB = AddDReg(MIB, DestReg, ARM::dsub_7, RegState::DefineNoRead, TRI);
1289 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1290 MIB.addReg(DestReg, RegState::ImplicitDefine);
1292 llvm_unreachable("Unknown reg class!");
1295 llvm_unreachable("Unknown regclass!");
1299 unsigned ARMBaseInstrInfo::isLoadFromStackSlot(const MachineInstr &MI,
1300 int &FrameIndex) const {
1301 switch (MI.getOpcode()) {
1304 case ARM::t2LDRs: // FIXME: don't use t2LDRs to access frame.
1305 if (MI.getOperand(1).isFI() && MI.getOperand(2).isReg() &&
1306 MI.getOperand(3).isImm() && MI.getOperand(2).getReg() == 0 &&
1307 MI.getOperand(3).getImm() == 0) {
1308 FrameIndex = MI.getOperand(1).getIndex();
1309 return MI.getOperand(0).getReg();
1317 if (MI.getOperand(1).isFI() && MI.getOperand(2).isImm() &&
1318 MI.getOperand(2).getImm() == 0) {
1319 FrameIndex = MI.getOperand(1).getIndex();
1320 return MI.getOperand(0).getReg();
1324 case ARM::VLD1d64TPseudo:
1325 case ARM::VLD1d64QPseudo:
1326 if (MI.getOperand(1).isFI() && MI.getOperand(0).getSubReg() == 0) {
1327 FrameIndex = MI.getOperand(1).getIndex();
1328 return MI.getOperand(0).getReg();
1332 if (MI.getOperand(1).isFI() && MI.getOperand(0).getSubReg() == 0) {
1333 FrameIndex = MI.getOperand(1).getIndex();
1334 return MI.getOperand(0).getReg();
1342 unsigned ARMBaseInstrInfo::isLoadFromStackSlotPostFE(const MachineInstr &MI,
1343 int &FrameIndex) const {
1344 const MachineMemOperand *Dummy;
1345 return MI.mayLoad() && hasLoadFromStackSlot(MI, Dummy, FrameIndex);
1348 /// \brief Expands MEMCPY to either LDMIA/STMIA or LDMIA_UPD/STMID_UPD
1349 /// depending on whether the result is used.
1350 void ARMBaseInstrInfo::expandMEMCPY(MachineBasicBlock::iterator MI) const {
1351 bool isThumb1 = Subtarget.isThumb1Only();
1352 bool isThumb2 = Subtarget.isThumb2();
1353 const ARMBaseInstrInfo *TII = Subtarget.getInstrInfo();
1355 DebugLoc dl = MI->getDebugLoc();
1356 MachineBasicBlock *BB = MI->getParent();
1358 MachineInstrBuilder LDM, STM;
1359 if (isThumb1 || !MI->getOperand(1).isDead()) {
1360 LDM = BuildMI(*BB, MI, dl, TII->get(isThumb2 ? ARM::t2LDMIA_UPD
1361 : isThumb1 ? ARM::tLDMIA_UPD
1363 .add(MI->getOperand(1));
1365 LDM = BuildMI(*BB, MI, dl, TII->get(isThumb2 ? ARM::t2LDMIA : ARM::LDMIA));
1368 if (isThumb1 || !MI->getOperand(0).isDead()) {
1369 STM = BuildMI(*BB, MI, dl, TII->get(isThumb2 ? ARM::t2STMIA_UPD
1370 : isThumb1 ? ARM::tSTMIA_UPD
1372 .add(MI->getOperand(0));
1374 STM = BuildMI(*BB, MI, dl, TII->get(isThumb2 ? ARM::t2STMIA : ARM::STMIA));
1377 LDM.add(MI->getOperand(3)).add(predOps(ARMCC::AL));
1378 STM.add(MI->getOperand(2)).add(predOps(ARMCC::AL));
1380 // Sort the scratch registers into ascending order.
1381 const TargetRegisterInfo &TRI = getRegisterInfo();
1382 SmallVector<unsigned, 6> ScratchRegs;
1383 for(unsigned I = 5; I < MI->getNumOperands(); ++I)
1384 ScratchRegs.push_back(MI->getOperand(I).getReg());
1385 std::sort(ScratchRegs.begin(), ScratchRegs.end(),
1386 [&TRI](const unsigned &Reg1,
1387 const unsigned &Reg2) -> bool {
1388 return TRI.getEncodingValue(Reg1) <
1389 TRI.getEncodingValue(Reg2);
1392 for (const auto &Reg : ScratchRegs) {
1393 LDM.addReg(Reg, RegState::Define);
1394 STM.addReg(Reg, RegState::Kill);
1400 bool ARMBaseInstrInfo::expandPostRAPseudo(MachineInstr &MI) const {
1401 if (MI.getOpcode() == TargetOpcode::LOAD_STACK_GUARD) {
1402 assert(getSubtarget().getTargetTriple().isOSBinFormatMachO() &&
1403 "LOAD_STACK_GUARD currently supported only for MachO.");
1404 expandLoadStackGuard(MI);
1405 MI.getParent()->erase(MI);
1409 if (MI.getOpcode() == ARM::MEMCPY) {
1414 // This hook gets to expand COPY instructions before they become
1415 // copyPhysReg() calls. Look for VMOVS instructions that can legally be
1416 // widened to VMOVD. We prefer the VMOVD when possible because it may be
1417 // changed into a VORR that can go down the NEON pipeline.
1418 if (!MI.isCopy() || Subtarget.dontWidenVMOVS() || Subtarget.isFPOnlySP())
1421 // Look for a copy between even S-registers. That is where we keep floats
1422 // when using NEON v2f32 instructions for f32 arithmetic.
1423 unsigned DstRegS = MI.getOperand(0).getReg();
1424 unsigned SrcRegS = MI.getOperand(1).getReg();
1425 if (!ARM::SPRRegClass.contains(DstRegS, SrcRegS))
1428 const TargetRegisterInfo *TRI = &getRegisterInfo();
1429 unsigned DstRegD = TRI->getMatchingSuperReg(DstRegS, ARM::ssub_0,
1431 unsigned SrcRegD = TRI->getMatchingSuperReg(SrcRegS, ARM::ssub_0,
1433 if (!DstRegD || !SrcRegD)
1436 // We want to widen this into a DstRegD = VMOVD SrcRegD copy. This is only
1437 // legal if the COPY already defines the full DstRegD, and it isn't a
1438 // sub-register insertion.
1439 if (!MI.definesRegister(DstRegD, TRI) || MI.readsRegister(DstRegD, TRI))
1442 // A dead copy shouldn't show up here, but reject it just in case.
1443 if (MI.getOperand(0).isDead())
1446 // All clear, widen the COPY.
1447 DEBUG(dbgs() << "widening: " << MI);
1448 MachineInstrBuilder MIB(*MI.getParent()->getParent(), MI);
1450 // Get rid of the old <imp-def> of DstRegD. Leave it if it defines a Q-reg
1451 // or some other super-register.
1452 int ImpDefIdx = MI.findRegisterDefOperandIdx(DstRegD);
1453 if (ImpDefIdx != -1)
1454 MI.RemoveOperand(ImpDefIdx);
1456 // Change the opcode and operands.
1457 MI.setDesc(get(ARM::VMOVD));
1458 MI.getOperand(0).setReg(DstRegD);
1459 MI.getOperand(1).setReg(SrcRegD);
1460 MIB.add(predOps(ARMCC::AL));
1462 // We are now reading SrcRegD instead of SrcRegS. This may upset the
1463 // register scavenger and machine verifier, so we need to indicate that we
1464 // are reading an undefined value from SrcRegD, but a proper value from
1466 MI.getOperand(1).setIsUndef();
1467 MIB.addReg(SrcRegS, RegState::Implicit);
1469 // SrcRegD may actually contain an unrelated value in the ssub_1
1470 // sub-register. Don't kill it. Only kill the ssub_0 sub-register.
1471 if (MI.getOperand(1).isKill()) {
1472 MI.getOperand(1).setIsKill(false);
1473 MI.addRegisterKilled(SrcRegS, TRI, true);
1476 DEBUG(dbgs() << "replaced by: " << MI);
1480 /// Create a copy of a const pool value. Update CPI to the new index and return
1482 static unsigned duplicateCPV(MachineFunction &MF, unsigned &CPI) {
1483 MachineConstantPool *MCP = MF.getConstantPool();
1484 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1486 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPI];
1487 assert(MCPE.isMachineConstantPoolEntry() &&
1488 "Expecting a machine constantpool entry!");
1489 ARMConstantPoolValue *ACPV =
1490 static_cast<ARMConstantPoolValue*>(MCPE.Val.MachineCPVal);
1492 unsigned PCLabelId = AFI->createPICLabelUId();
1493 ARMConstantPoolValue *NewCPV = nullptr;
1495 // FIXME: The below assumes PIC relocation model and that the function
1496 // is Thumb mode (t1 or t2). PCAdjustment would be 8 for ARM mode PIC, and
1497 // zero for non-PIC in ARM or Thumb. The callers are all of thumb LDR
1498 // instructions, so that's probably OK, but is PIC always correct when
1500 if (ACPV->isGlobalValue())
1501 NewCPV = ARMConstantPoolConstant::Create(
1502 cast<ARMConstantPoolConstant>(ACPV)->getGV(), PCLabelId, ARMCP::CPValue,
1503 4, ACPV->getModifier(), ACPV->mustAddCurrentAddress());
1504 else if (ACPV->isExtSymbol())
1505 NewCPV = ARMConstantPoolSymbol::
1506 Create(MF.getFunction()->getContext(),
1507 cast<ARMConstantPoolSymbol>(ACPV)->getSymbol(), PCLabelId, 4);
1508 else if (ACPV->isBlockAddress())
1509 NewCPV = ARMConstantPoolConstant::
1510 Create(cast<ARMConstantPoolConstant>(ACPV)->getBlockAddress(), PCLabelId,
1511 ARMCP::CPBlockAddress, 4);
1512 else if (ACPV->isLSDA())
1513 NewCPV = ARMConstantPoolConstant::Create(MF.getFunction(), PCLabelId,
1515 else if (ACPV->isMachineBasicBlock())
1516 NewCPV = ARMConstantPoolMBB::
1517 Create(MF.getFunction()->getContext(),
1518 cast<ARMConstantPoolMBB>(ACPV)->getMBB(), PCLabelId, 4);
1520 llvm_unreachable("Unexpected ARM constantpool value type!!");
1521 CPI = MCP->getConstantPoolIndex(NewCPV, MCPE.getAlignment());
1525 void ARMBaseInstrInfo::reMaterialize(MachineBasicBlock &MBB,
1526 MachineBasicBlock::iterator I,
1527 unsigned DestReg, unsigned SubIdx,
1528 const MachineInstr &Orig,
1529 const TargetRegisterInfo &TRI) const {
1530 unsigned Opcode = Orig.getOpcode();
1533 MachineInstr *MI = MBB.getParent()->CloneMachineInstr(&Orig);
1534 MI->substituteRegister(Orig.getOperand(0).getReg(), DestReg, SubIdx, TRI);
1538 case ARM::tLDRpci_pic:
1539 case ARM::t2LDRpci_pic: {
1540 MachineFunction &MF = *MBB.getParent();
1541 unsigned CPI = Orig.getOperand(1).getIndex();
1542 unsigned PCLabelId = duplicateCPV(MF, CPI);
1543 MachineInstrBuilder MIB =
1544 BuildMI(MBB, I, Orig.getDebugLoc(), get(Opcode), DestReg)
1545 .addConstantPoolIndex(CPI)
1547 MIB->setMemRefs(Orig.memoperands_begin(), Orig.memoperands_end());
1553 MachineInstr *ARMBaseInstrInfo::duplicate(MachineInstr &Orig,
1554 MachineFunction &MF) const {
1555 MachineInstr *MI = TargetInstrInfo::duplicate(Orig, MF);
1556 switch (Orig.getOpcode()) {
1557 case ARM::tLDRpci_pic:
1558 case ARM::t2LDRpci_pic: {
1559 unsigned CPI = Orig.getOperand(1).getIndex();
1560 unsigned PCLabelId = duplicateCPV(MF, CPI);
1561 Orig.getOperand(1).setIndex(CPI);
1562 Orig.getOperand(2).setImm(PCLabelId);
1569 bool ARMBaseInstrInfo::produceSameValue(const MachineInstr &MI0,
1570 const MachineInstr &MI1,
1571 const MachineRegisterInfo *MRI) const {
1572 unsigned Opcode = MI0.getOpcode();
1573 if (Opcode == ARM::t2LDRpci ||
1574 Opcode == ARM::t2LDRpci_pic ||
1575 Opcode == ARM::tLDRpci ||
1576 Opcode == ARM::tLDRpci_pic ||
1577 Opcode == ARM::LDRLIT_ga_pcrel ||
1578 Opcode == ARM::LDRLIT_ga_pcrel_ldr ||
1579 Opcode == ARM::tLDRLIT_ga_pcrel ||
1580 Opcode == ARM::MOV_ga_pcrel ||
1581 Opcode == ARM::MOV_ga_pcrel_ldr ||
1582 Opcode == ARM::t2MOV_ga_pcrel) {
1583 if (MI1.getOpcode() != Opcode)
1585 if (MI0.getNumOperands() != MI1.getNumOperands())
1588 const MachineOperand &MO0 = MI0.getOperand(1);
1589 const MachineOperand &MO1 = MI1.getOperand(1);
1590 if (MO0.getOffset() != MO1.getOffset())
1593 if (Opcode == ARM::LDRLIT_ga_pcrel ||
1594 Opcode == ARM::LDRLIT_ga_pcrel_ldr ||
1595 Opcode == ARM::tLDRLIT_ga_pcrel ||
1596 Opcode == ARM::MOV_ga_pcrel ||
1597 Opcode == ARM::MOV_ga_pcrel_ldr ||
1598 Opcode == ARM::t2MOV_ga_pcrel)
1599 // Ignore the PC labels.
1600 return MO0.getGlobal() == MO1.getGlobal();
1602 const MachineFunction *MF = MI0.getParent()->getParent();
1603 const MachineConstantPool *MCP = MF->getConstantPool();
1604 int CPI0 = MO0.getIndex();
1605 int CPI1 = MO1.getIndex();
1606 const MachineConstantPoolEntry &MCPE0 = MCP->getConstants()[CPI0];
1607 const MachineConstantPoolEntry &MCPE1 = MCP->getConstants()[CPI1];
1608 bool isARMCP0 = MCPE0.isMachineConstantPoolEntry();
1609 bool isARMCP1 = MCPE1.isMachineConstantPoolEntry();
1610 if (isARMCP0 && isARMCP1) {
1611 ARMConstantPoolValue *ACPV0 =
1612 static_cast<ARMConstantPoolValue*>(MCPE0.Val.MachineCPVal);
1613 ARMConstantPoolValue *ACPV1 =
1614 static_cast<ARMConstantPoolValue*>(MCPE1.Val.MachineCPVal);
1615 return ACPV0->hasSameValue(ACPV1);
1616 } else if (!isARMCP0 && !isARMCP1) {
1617 return MCPE0.Val.ConstVal == MCPE1.Val.ConstVal;
1620 } else if (Opcode == ARM::PICLDR) {
1621 if (MI1.getOpcode() != Opcode)
1623 if (MI0.getNumOperands() != MI1.getNumOperands())
1626 unsigned Addr0 = MI0.getOperand(1).getReg();
1627 unsigned Addr1 = MI1.getOperand(1).getReg();
1628 if (Addr0 != Addr1) {
1630 !TargetRegisterInfo::isVirtualRegister(Addr0) ||
1631 !TargetRegisterInfo::isVirtualRegister(Addr1))
1634 // This assumes SSA form.
1635 MachineInstr *Def0 = MRI->getVRegDef(Addr0);
1636 MachineInstr *Def1 = MRI->getVRegDef(Addr1);
1637 // Check if the loaded value, e.g. a constantpool of a global address, are
1639 if (!produceSameValue(*Def0, *Def1, MRI))
1643 for (unsigned i = 3, e = MI0.getNumOperands(); i != e; ++i) {
1644 // %vreg12<def> = PICLDR %vreg11, 0, pred:14, pred:%noreg
1645 const MachineOperand &MO0 = MI0.getOperand(i);
1646 const MachineOperand &MO1 = MI1.getOperand(i);
1647 if (!MO0.isIdenticalTo(MO1))
1653 return MI0.isIdenticalTo(MI1, MachineInstr::IgnoreVRegDefs);
1656 /// areLoadsFromSameBasePtr - This is used by the pre-regalloc scheduler to
1657 /// determine if two loads are loading from the same base address. It should
1658 /// only return true if the base pointers are the same and the only differences
1659 /// between the two addresses is the offset. It also returns the offsets by
1662 /// FIXME: remove this in favor of the MachineInstr interface once pre-RA-sched
1663 /// is permanently disabled.
1664 bool ARMBaseInstrInfo::areLoadsFromSameBasePtr(SDNode *Load1, SDNode *Load2,
1666 int64_t &Offset2) const {
1667 // Don't worry about Thumb: just ARM and Thumb2.
1668 if (Subtarget.isThumb1Only()) return false;
1670 if (!Load1->isMachineOpcode() || !Load2->isMachineOpcode())
1673 switch (Load1->getMachineOpcode()) {
1687 case ARM::t2LDRSHi8:
1689 case ARM::t2LDRBi12:
1690 case ARM::t2LDRSHi12:
1694 switch (Load2->getMachineOpcode()) {
1707 case ARM::t2LDRSHi8:
1709 case ARM::t2LDRBi12:
1710 case ARM::t2LDRSHi12:
1714 // Check if base addresses and chain operands match.
1715 if (Load1->getOperand(0) != Load2->getOperand(0) ||
1716 Load1->getOperand(4) != Load2->getOperand(4))
1719 // Index should be Reg0.
1720 if (Load1->getOperand(3) != Load2->getOperand(3))
1723 // Determine the offsets.
1724 if (isa<ConstantSDNode>(Load1->getOperand(1)) &&
1725 isa<ConstantSDNode>(Load2->getOperand(1))) {
1726 Offset1 = cast<ConstantSDNode>(Load1->getOperand(1))->getSExtValue();
1727 Offset2 = cast<ConstantSDNode>(Load2->getOperand(1))->getSExtValue();
1734 /// shouldScheduleLoadsNear - This is a used by the pre-regalloc scheduler to
1735 /// determine (in conjunction with areLoadsFromSameBasePtr) if two loads should
1736 /// be scheduled togther. On some targets if two loads are loading from
1737 /// addresses in the same cache line, it's better if they are scheduled
1738 /// together. This function takes two integers that represent the load offsets
1739 /// from the common base address. It returns true if it decides it's desirable
1740 /// to schedule the two loads together. "NumLoads" is the number of loads that
1741 /// have already been scheduled after Load1.
1743 /// FIXME: remove this in favor of the MachineInstr interface once pre-RA-sched
1744 /// is permanently disabled.
1745 bool ARMBaseInstrInfo::shouldScheduleLoadsNear(SDNode *Load1, SDNode *Load2,
1746 int64_t Offset1, int64_t Offset2,
1747 unsigned NumLoads) const {
1748 // Don't worry about Thumb: just ARM and Thumb2.
1749 if (Subtarget.isThumb1Only()) return false;
1751 assert(Offset2 > Offset1);
1753 if ((Offset2 - Offset1) / 8 > 64)
1756 // Check if the machine opcodes are different. If they are different
1757 // then we consider them to not be of the same base address,
1758 // EXCEPT in the case of Thumb2 byte loads where one is LDRBi8 and the other LDRBi12.
1759 // In this case, they are considered to be the same because they are different
1760 // encoding forms of the same basic instruction.
1761 if ((Load1->getMachineOpcode() != Load2->getMachineOpcode()) &&
1762 !((Load1->getMachineOpcode() == ARM::t2LDRBi8 &&
1763 Load2->getMachineOpcode() == ARM::t2LDRBi12) ||
1764 (Load1->getMachineOpcode() == ARM::t2LDRBi12 &&
1765 Load2->getMachineOpcode() == ARM::t2LDRBi8)))
1766 return false; // FIXME: overly conservative?
1768 // Four loads in a row should be sufficient.
1775 bool ARMBaseInstrInfo::isSchedulingBoundary(const MachineInstr &MI,
1776 const MachineBasicBlock *MBB,
1777 const MachineFunction &MF) const {
1778 // Debug info is never a scheduling boundary. It's necessary to be explicit
1779 // due to the special treatment of IT instructions below, otherwise a
1780 // dbg_value followed by an IT will result in the IT instruction being
1781 // considered a scheduling hazard, which is wrong. It should be the actual
1782 // instruction preceding the dbg_value instruction(s), just like it is
1783 // when debug info is not present.
1784 if (MI.isDebugValue())
1787 // Terminators and labels can't be scheduled around.
1788 if (MI.isTerminator() || MI.isPosition())
1791 // Treat the start of the IT block as a scheduling boundary, but schedule
1792 // t2IT along with all instructions following it.
1793 // FIXME: This is a big hammer. But the alternative is to add all potential
1794 // true and anti dependencies to IT block instructions as implicit operands
1795 // to the t2IT instruction. The added compile time and complexity does not
1797 MachineBasicBlock::const_iterator I = MI;
1798 // Make sure to skip any dbg_value instructions
1799 while (++I != MBB->end() && I->isDebugValue())
1801 if (I != MBB->end() && I->getOpcode() == ARM::t2IT)
1804 // Don't attempt to schedule around any instruction that defines
1805 // a stack-oriented pointer, as it's unlikely to be profitable. This
1806 // saves compile time, because it doesn't require every single
1807 // stack slot reference to depend on the instruction that does the
1809 // Calls don't actually change the stack pointer, even if they have imp-defs.
1810 // No ARM calling conventions change the stack pointer. (X86 calling
1811 // conventions sometimes do).
1812 if (!MI.isCall() && MI.definesRegister(ARM::SP))
1818 bool ARMBaseInstrInfo::
1819 isProfitableToIfCvt(MachineBasicBlock &MBB,
1820 unsigned NumCycles, unsigned ExtraPredCycles,
1821 BranchProbability Probability) const {
1825 // If we are optimizing for size, see if the branch in the predecessor can be
1826 // lowered to cbn?z by the constant island lowering pass, and return false if
1827 // so. This results in a shorter instruction sequence.
1828 if (MBB.getParent()->getFunction()->optForSize()) {
1829 MachineBasicBlock *Pred = *MBB.pred_begin();
1830 if (!Pred->empty()) {
1831 MachineInstr *LastMI = &*Pred->rbegin();
1832 if (LastMI->getOpcode() == ARM::t2Bcc) {
1833 MachineBasicBlock::iterator CmpMI = LastMI;
1834 if (CmpMI != Pred->begin()) {
1836 if (CmpMI->getOpcode() == ARM::tCMPi8 ||
1837 CmpMI->getOpcode() == ARM::t2CMPri) {
1838 unsigned Reg = CmpMI->getOperand(0).getReg();
1839 unsigned PredReg = 0;
1840 ARMCC::CondCodes P = getInstrPredicate(*CmpMI, PredReg);
1841 if (P == ARMCC::AL && CmpMI->getOperand(1).getImm() == 0 &&
1842 isARMLowRegister(Reg))
1849 return isProfitableToIfCvt(MBB, NumCycles, ExtraPredCycles,
1850 MBB, 0, 0, Probability);
1853 bool ARMBaseInstrInfo::
1854 isProfitableToIfCvt(MachineBasicBlock &TBB,
1855 unsigned TCycles, unsigned TExtra,
1856 MachineBasicBlock &FBB,
1857 unsigned FCycles, unsigned FExtra,
1858 BranchProbability Probability) const {
1862 // Attempt to estimate the relative costs of predication versus branching.
1863 // Here we scale up each component of UnpredCost to avoid precision issue when
1864 // scaling TCycles/FCycles by Probability.
1865 const unsigned ScalingUpFactor = 1024;
1867 unsigned PredCost = (TCycles + FCycles + TExtra + FExtra) * ScalingUpFactor;
1868 unsigned UnpredCost;
1869 if (!Subtarget.hasBranchPredictor()) {
1870 // When we don't have a branch predictor it's always cheaper to not take a
1871 // branch than take it, so we have to take that into account.
1872 unsigned NotTakenBranchCost = 1;
1873 unsigned TakenBranchCost = Subtarget.getMispredictionPenalty();
1874 unsigned TUnpredCycles, FUnpredCycles;
1876 // Triangle: TBB is the fallthrough
1877 TUnpredCycles = TCycles + NotTakenBranchCost;
1878 FUnpredCycles = TakenBranchCost;
1880 // Diamond: TBB is the block that is branched to, FBB is the fallthrough
1881 TUnpredCycles = TCycles + TakenBranchCost;
1882 FUnpredCycles = FCycles + NotTakenBranchCost;
1883 // The branch at the end of FBB will disappear when it's predicated, so
1884 // discount it from PredCost.
1885 PredCost -= 1 * ScalingUpFactor;
1887 // The total cost is the cost of each path scaled by their probabilites
1888 unsigned TUnpredCost = Probability.scale(TUnpredCycles * ScalingUpFactor);
1889 unsigned FUnpredCost = Probability.getCompl().scale(FUnpredCycles * ScalingUpFactor);
1890 UnpredCost = TUnpredCost + FUnpredCost;
1891 // When predicating assume that the first IT can be folded away but later
1892 // ones cost one cycle each
1893 if (Subtarget.isThumb2() && TCycles + FCycles > 4) {
1894 PredCost += ((TCycles + FCycles - 4) / 4) * ScalingUpFactor;
1897 unsigned TUnpredCost = Probability.scale(TCycles * ScalingUpFactor);
1898 unsigned FUnpredCost =
1899 Probability.getCompl().scale(FCycles * ScalingUpFactor);
1900 UnpredCost = TUnpredCost + FUnpredCost;
1901 UnpredCost += 1 * ScalingUpFactor; // The branch itself
1902 UnpredCost += Subtarget.getMispredictionPenalty() * ScalingUpFactor / 10;
1905 return PredCost <= UnpredCost;
1909 ARMBaseInstrInfo::isProfitableToUnpredicate(MachineBasicBlock &TMBB,
1910 MachineBasicBlock &FMBB) const {
1911 // Reduce false anti-dependencies to let the target's out-of-order execution
1912 // engine do its thing.
1913 return Subtarget.isProfitableToUnpredicate();
1916 /// getInstrPredicate - If instruction is predicated, returns its predicate
1917 /// condition, otherwise returns AL. It also returns the condition code
1918 /// register by reference.
1919 ARMCC::CondCodes llvm::getInstrPredicate(const MachineInstr &MI,
1920 unsigned &PredReg) {
1921 int PIdx = MI.findFirstPredOperandIdx();
1927 PredReg = MI.getOperand(PIdx+1).getReg();
1928 return (ARMCC::CondCodes)MI.getOperand(PIdx).getImm();
1931 unsigned llvm::getMatchingCondBranchOpcode(unsigned Opc) {
1936 if (Opc == ARM::t2B)
1939 llvm_unreachable("Unknown unconditional branch opcode!");
1942 MachineInstr *ARMBaseInstrInfo::commuteInstructionImpl(MachineInstr &MI,
1945 unsigned OpIdx2) const {
1946 switch (MI.getOpcode()) {
1948 case ARM::t2MOVCCr: {
1949 // MOVCC can be commuted by inverting the condition.
1950 unsigned PredReg = 0;
1951 ARMCC::CondCodes CC = getInstrPredicate(MI, PredReg);
1952 // MOVCC AL can't be inverted. Shouldn't happen.
1953 if (CC == ARMCC::AL || PredReg != ARM::CPSR)
1955 MachineInstr *CommutedMI =
1956 TargetInstrInfo::commuteInstructionImpl(MI, NewMI, OpIdx1, OpIdx2);
1959 // After swapping the MOVCC operands, also invert the condition.
1960 CommutedMI->getOperand(CommutedMI->findFirstPredOperandIdx())
1961 .setImm(ARMCC::getOppositeCondition(CC));
1965 return TargetInstrInfo::commuteInstructionImpl(MI, NewMI, OpIdx1, OpIdx2);
1968 /// Identify instructions that can be folded into a MOVCC instruction, and
1969 /// return the defining instruction.
1970 static MachineInstr *canFoldIntoMOVCC(unsigned Reg,
1971 const MachineRegisterInfo &MRI,
1972 const TargetInstrInfo *TII) {
1973 if (!TargetRegisterInfo::isVirtualRegister(Reg))
1975 if (!MRI.hasOneNonDBGUse(Reg))
1977 MachineInstr *MI = MRI.getVRegDef(Reg);
1980 // MI is folded into the MOVCC by predicating it.
1981 if (!MI->isPredicable())
1983 // Check if MI has any non-dead defs or physreg uses. This also detects
1984 // predicated instructions which will be reading CPSR.
1985 for (unsigned i = 1, e = MI->getNumOperands(); i != e; ++i) {
1986 const MachineOperand &MO = MI->getOperand(i);
1987 // Reject frame index operands, PEI can't handle the predicated pseudos.
1988 if (MO.isFI() || MO.isCPI() || MO.isJTI())
1992 // MI can't have any tied operands, that would conflict with predication.
1995 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
1997 if (MO.isDef() && !MO.isDead())
2000 bool DontMoveAcrossStores = true;
2001 if (!MI->isSafeToMove(/* AliasAnalysis = */ nullptr, DontMoveAcrossStores))
2006 bool ARMBaseInstrInfo::analyzeSelect(const MachineInstr &MI,
2007 SmallVectorImpl<MachineOperand> &Cond,
2008 unsigned &TrueOp, unsigned &FalseOp,
2009 bool &Optimizable) const {
2010 assert((MI.getOpcode() == ARM::MOVCCr || MI.getOpcode() == ARM::t2MOVCCr) &&
2011 "Unknown select instruction");
2016 // 3: Condition code.
2020 Cond.push_back(MI.getOperand(3));
2021 Cond.push_back(MI.getOperand(4));
2022 // We can always fold a def.
2028 ARMBaseInstrInfo::optimizeSelect(MachineInstr &MI,
2029 SmallPtrSetImpl<MachineInstr *> &SeenMIs,
2030 bool PreferFalse) const {
2031 assert((MI.getOpcode() == ARM::MOVCCr || MI.getOpcode() == ARM::t2MOVCCr) &&
2032 "Unknown select instruction");
2033 MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
2034 MachineInstr *DefMI = canFoldIntoMOVCC(MI.getOperand(2).getReg(), MRI, this);
2035 bool Invert = !DefMI;
2037 DefMI = canFoldIntoMOVCC(MI.getOperand(1).getReg(), MRI, this);
2041 // Find new register class to use.
2042 MachineOperand FalseReg = MI.getOperand(Invert ? 2 : 1);
2043 unsigned DestReg = MI.getOperand(0).getReg();
2044 const TargetRegisterClass *PreviousClass = MRI.getRegClass(FalseReg.getReg());
2045 if (!MRI.constrainRegClass(DestReg, PreviousClass))
2048 // Create a new predicated version of DefMI.
2049 // Rfalse is the first use.
2050 MachineInstrBuilder NewMI =
2051 BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), DefMI->getDesc(), DestReg);
2053 // Copy all the DefMI operands, excluding its (null) predicate.
2054 const MCInstrDesc &DefDesc = DefMI->getDesc();
2055 for (unsigned i = 1, e = DefDesc.getNumOperands();
2056 i != e && !DefDesc.OpInfo[i].isPredicate(); ++i)
2057 NewMI.add(DefMI->getOperand(i));
2059 unsigned CondCode = MI.getOperand(3).getImm();
2061 NewMI.addImm(ARMCC::getOppositeCondition(ARMCC::CondCodes(CondCode)));
2063 NewMI.addImm(CondCode);
2064 NewMI.add(MI.getOperand(4));
2066 // DefMI is not the -S version that sets CPSR, so add an optional %noreg.
2067 if (NewMI->hasOptionalDef())
2068 NewMI.add(condCodeOp());
2070 // The output register value when the predicate is false is an implicit
2071 // register operand tied to the first def.
2072 // The tie makes the register allocator ensure the FalseReg is allocated the
2073 // same register as operand 0.
2074 FalseReg.setImplicit();
2075 NewMI.add(FalseReg);
2076 NewMI->tieOperands(0, NewMI->getNumOperands() - 1);
2078 // Update SeenMIs set: register newly created MI and erase removed DefMI.
2079 SeenMIs.insert(NewMI);
2080 SeenMIs.erase(DefMI);
2082 // If MI is inside a loop, and DefMI is outside the loop, then kill flags on
2083 // DefMI would be invalid when tranferred inside the loop. Checking for a
2084 // loop is expensive, but at least remove kill flags if they are in different
2086 if (DefMI->getParent() != MI.getParent())
2087 NewMI->clearKillInfo();
2089 // The caller will erase MI, but not DefMI.
2090 DefMI->eraseFromParent();
2094 /// Map pseudo instructions that imply an 'S' bit onto real opcodes. Whether the
2095 /// instruction is encoded with an 'S' bit is determined by the optional CPSR
2098 /// This will go away once we can teach tblgen how to set the optional CPSR def
2100 struct AddSubFlagsOpcodePair {
2102 uint16_t MachineOpc;
2105 static const AddSubFlagsOpcodePair AddSubFlagsOpcodeMap[] = {
2106 {ARM::ADDSri, ARM::ADDri},
2107 {ARM::ADDSrr, ARM::ADDrr},
2108 {ARM::ADDSrsi, ARM::ADDrsi},
2109 {ARM::ADDSrsr, ARM::ADDrsr},
2111 {ARM::SUBSri, ARM::SUBri},
2112 {ARM::SUBSrr, ARM::SUBrr},
2113 {ARM::SUBSrsi, ARM::SUBrsi},
2114 {ARM::SUBSrsr, ARM::SUBrsr},
2116 {ARM::RSBSri, ARM::RSBri},
2117 {ARM::RSBSrsi, ARM::RSBrsi},
2118 {ARM::RSBSrsr, ARM::RSBrsr},
2120 {ARM::tADDSi3, ARM::tADDi3},
2121 {ARM::tADDSi8, ARM::tADDi8},
2122 {ARM::tADDSrr, ARM::tADDrr},
2123 {ARM::tADCS, ARM::tADC},
2125 {ARM::tSUBSi3, ARM::tSUBi3},
2126 {ARM::tSUBSi8, ARM::tSUBi8},
2127 {ARM::tSUBSrr, ARM::tSUBrr},
2128 {ARM::tSBCS, ARM::tSBC},
2130 {ARM::t2ADDSri, ARM::t2ADDri},
2131 {ARM::t2ADDSrr, ARM::t2ADDrr},
2132 {ARM::t2ADDSrs, ARM::t2ADDrs},
2134 {ARM::t2SUBSri, ARM::t2SUBri},
2135 {ARM::t2SUBSrr, ARM::t2SUBrr},
2136 {ARM::t2SUBSrs, ARM::t2SUBrs},
2138 {ARM::t2RSBSri, ARM::t2RSBri},
2139 {ARM::t2RSBSrs, ARM::t2RSBrs},
2142 unsigned llvm::convertAddSubFlagsOpcode(unsigned OldOpc) {
2143 for (unsigned i = 0, e = array_lengthof(AddSubFlagsOpcodeMap); i != e; ++i)
2144 if (OldOpc == AddSubFlagsOpcodeMap[i].PseudoOpc)
2145 return AddSubFlagsOpcodeMap[i].MachineOpc;
2149 void llvm::emitARMRegPlusImmediate(MachineBasicBlock &MBB,
2150 MachineBasicBlock::iterator &MBBI,
2151 const DebugLoc &dl, unsigned DestReg,
2152 unsigned BaseReg, int NumBytes,
2153 ARMCC::CondCodes Pred, unsigned PredReg,
2154 const ARMBaseInstrInfo &TII,
2156 if (NumBytes == 0 && DestReg != BaseReg) {
2157 BuildMI(MBB, MBBI, dl, TII.get(ARM::MOVr), DestReg)
2158 .addReg(BaseReg, RegState::Kill)
2159 .add(predOps(Pred, PredReg))
2161 .setMIFlags(MIFlags);
2165 bool isSub = NumBytes < 0;
2166 if (isSub) NumBytes = -NumBytes;
2169 unsigned RotAmt = ARM_AM::getSOImmValRotate(NumBytes);
2170 unsigned ThisVal = NumBytes & ARM_AM::rotr32(0xFF, RotAmt);
2171 assert(ThisVal && "Didn't extract field correctly");
2173 // We will handle these bits from offset, clear them.
2174 NumBytes &= ~ThisVal;
2176 assert(ARM_AM::getSOImmVal(ThisVal) != -1 && "Bit extraction didn't work?");
2178 // Build the new ADD / SUB.
2179 unsigned Opc = isSub ? ARM::SUBri : ARM::ADDri;
2180 BuildMI(MBB, MBBI, dl, TII.get(Opc), DestReg)
2181 .addReg(BaseReg, RegState::Kill)
2183 .add(predOps(Pred, PredReg))
2185 .setMIFlags(MIFlags);
2190 bool llvm::tryFoldSPUpdateIntoPushPop(const ARMSubtarget &Subtarget,
2191 MachineFunction &MF, MachineInstr *MI,
2192 unsigned NumBytes) {
2193 // This optimisation potentially adds lots of load and store
2194 // micro-operations, it's only really a great benefit to code-size.
2195 if (!MF.getFunction()->optForMinSize())
2198 // If only one register is pushed/popped, LLVM can use an LDR/STR
2199 // instead. We can't modify those so make sure we're dealing with an
2200 // instruction we understand.
2201 bool IsPop = isPopOpcode(MI->getOpcode());
2202 bool IsPush = isPushOpcode(MI->getOpcode());
2203 if (!IsPush && !IsPop)
2206 bool IsVFPPushPop = MI->getOpcode() == ARM::VSTMDDB_UPD ||
2207 MI->getOpcode() == ARM::VLDMDIA_UPD;
2208 bool IsT1PushPop = MI->getOpcode() == ARM::tPUSH ||
2209 MI->getOpcode() == ARM::tPOP ||
2210 MI->getOpcode() == ARM::tPOP_RET;
2212 assert((IsT1PushPop || (MI->getOperand(0).getReg() == ARM::SP &&
2213 MI->getOperand(1).getReg() == ARM::SP)) &&
2214 "trying to fold sp update into non-sp-updating push/pop");
2216 // The VFP push & pop act on D-registers, so we can only fold an adjustment
2217 // by a multiple of 8 bytes in correctly. Similarly rN is 4-bytes. Don't try
2218 // if this is violated.
2219 if (NumBytes % (IsVFPPushPop ? 8 : 4) != 0)
2222 // ARM and Thumb2 push/pop insts have explicit "sp, sp" operands (+
2223 // pred) so the list starts at 4. Thumb1 starts after the predicate.
2224 int RegListIdx = IsT1PushPop ? 2 : 4;
2226 // Calculate the space we'll need in terms of registers.
2227 unsigned RegsNeeded;
2228 const TargetRegisterClass *RegClass;
2230 RegsNeeded = NumBytes / 8;
2231 RegClass = &ARM::DPRRegClass;
2233 RegsNeeded = NumBytes / 4;
2234 RegClass = &ARM::GPRRegClass;
2237 // We're going to have to strip all list operands off before
2238 // re-adding them since the order matters, so save the existing ones
2240 SmallVector<MachineOperand, 4> RegList;
2242 // We're also going to need the first register transferred by this
2243 // instruction, which won't necessarily be the first register in the list.
2244 unsigned FirstRegEnc = -1;
2246 const TargetRegisterInfo *TRI = MF.getRegInfo().getTargetRegisterInfo();
2247 for (int i = MI->getNumOperands() - 1; i >= RegListIdx; --i) {
2248 MachineOperand &MO = MI->getOperand(i);
2249 RegList.push_back(MO);
2251 if (MO.isReg() && TRI->getEncodingValue(MO.getReg()) < FirstRegEnc)
2252 FirstRegEnc = TRI->getEncodingValue(MO.getReg());
2255 const MCPhysReg *CSRegs = TRI->getCalleeSavedRegs(&MF);
2257 // Now try to find enough space in the reglist to allocate NumBytes.
2258 for (int CurRegEnc = FirstRegEnc - 1; CurRegEnc >= 0 && RegsNeeded;
2260 unsigned CurReg = RegClass->getRegister(CurRegEnc);
2262 // Pushing any register is completely harmless, mark the
2263 // register involved as undef since we don't care about it in
2265 RegList.push_back(MachineOperand::CreateReg(CurReg, false, false,
2266 false, false, true));
2271 // However, we can only pop an extra register if it's not live. For
2272 // registers live within the function we might clobber a return value
2273 // register; the other way a register can be live here is if it's
2275 if (isCalleeSavedRegister(CurReg, CSRegs) ||
2276 MI->getParent()->computeRegisterLiveness(TRI, CurReg, MI) !=
2277 MachineBasicBlock::LQR_Dead) {
2278 // VFP pops don't allow holes in the register list, so any skip is fatal
2279 // for our transformation. GPR pops do, so we should just keep looking.
2286 // Mark the unimportant registers as <def,dead> in the POP.
2287 RegList.push_back(MachineOperand::CreateReg(CurReg, true, false, false,
2295 // Finally we know we can profitably perform the optimisation so go
2296 // ahead: strip all existing registers off and add them back again
2297 // in the right order.
2298 for (int i = MI->getNumOperands() - 1; i >= RegListIdx; --i)
2299 MI->RemoveOperand(i);
2301 // Add the complete list back in.
2302 MachineInstrBuilder MIB(MF, &*MI);
2303 for (int i = RegList.size() - 1; i >= 0; --i)
2304 MIB.add(RegList[i]);
2309 bool llvm::rewriteARMFrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
2310 unsigned FrameReg, int &Offset,
2311 const ARMBaseInstrInfo &TII) {
2312 unsigned Opcode = MI.getOpcode();
2313 const MCInstrDesc &Desc = MI.getDesc();
2314 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
2317 // Memory operands in inline assembly always use AddrMode2.
2318 if (Opcode == ARM::INLINEASM)
2319 AddrMode = ARMII::AddrMode2;
2321 if (Opcode == ARM::ADDri) {
2322 Offset += MI.getOperand(FrameRegIdx+1).getImm();
2324 // Turn it into a move.
2325 MI.setDesc(TII.get(ARM::MOVr));
2326 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
2327 MI.RemoveOperand(FrameRegIdx+1);
2330 } else if (Offset < 0) {
2333 MI.setDesc(TII.get(ARM::SUBri));
2336 // Common case: small offset, fits into instruction.
2337 if (ARM_AM::getSOImmVal(Offset) != -1) {
2338 // Replace the FrameIndex with sp / fp
2339 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
2340 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(Offset);
2345 // Otherwise, pull as much of the immedidate into this ADDri/SUBri
2347 unsigned RotAmt = ARM_AM::getSOImmValRotate(Offset);
2348 unsigned ThisImmVal = Offset & ARM_AM::rotr32(0xFF, RotAmt);
2350 // We will handle these bits from offset, clear them.
2351 Offset &= ~ThisImmVal;
2353 // Get the properly encoded SOImmVal field.
2354 assert(ARM_AM::getSOImmVal(ThisImmVal) != -1 &&
2355 "Bit extraction didn't work?");
2356 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(ThisImmVal);
2358 unsigned ImmIdx = 0;
2360 unsigned NumBits = 0;
2363 case ARMII::AddrMode_i12:
2364 ImmIdx = FrameRegIdx + 1;
2365 InstrOffs = MI.getOperand(ImmIdx).getImm();
2368 case ARMII::AddrMode2:
2369 ImmIdx = FrameRegIdx+2;
2370 InstrOffs = ARM_AM::getAM2Offset(MI.getOperand(ImmIdx).getImm());
2371 if (ARM_AM::getAM2Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
2375 case ARMII::AddrMode3:
2376 ImmIdx = FrameRegIdx+2;
2377 InstrOffs = ARM_AM::getAM3Offset(MI.getOperand(ImmIdx).getImm());
2378 if (ARM_AM::getAM3Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
2382 case ARMII::AddrMode4:
2383 case ARMII::AddrMode6:
2384 // Can't fold any offset even if it's zero.
2386 case ARMII::AddrMode5:
2387 ImmIdx = FrameRegIdx+1;
2388 InstrOffs = ARM_AM::getAM5Offset(MI.getOperand(ImmIdx).getImm());
2389 if (ARM_AM::getAM5Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
2395 llvm_unreachable("Unsupported addressing mode!");
2398 Offset += InstrOffs * Scale;
2399 assert((Offset & (Scale-1)) == 0 && "Can't encode this offset!");
2405 // Attempt to fold address comp. if opcode has offset bits
2407 // Common case: small offset, fits into instruction.
2408 MachineOperand &ImmOp = MI.getOperand(ImmIdx);
2409 int ImmedOffset = Offset / Scale;
2410 unsigned Mask = (1 << NumBits) - 1;
2411 if ((unsigned)Offset <= Mask * Scale) {
2412 // Replace the FrameIndex with sp
2413 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
2414 // FIXME: When addrmode2 goes away, this will simplify (like the
2415 // T2 version), as the LDR.i12 versions don't need the encoding
2416 // tricks for the offset value.
2418 if (AddrMode == ARMII::AddrMode_i12)
2419 ImmedOffset = -ImmedOffset;
2421 ImmedOffset |= 1 << NumBits;
2423 ImmOp.ChangeToImmediate(ImmedOffset);
2428 // Otherwise, it didn't fit. Pull in what we can to simplify the immed.
2429 ImmedOffset = ImmedOffset & Mask;
2431 if (AddrMode == ARMII::AddrMode_i12)
2432 ImmedOffset = -ImmedOffset;
2434 ImmedOffset |= 1 << NumBits;
2436 ImmOp.ChangeToImmediate(ImmedOffset);
2437 Offset &= ~(Mask*Scale);
2441 Offset = (isSub) ? -Offset : Offset;
2445 /// analyzeCompare - For a comparison instruction, return the source registers
2446 /// in SrcReg and SrcReg2 if having two register operands, and the value it
2447 /// compares against in CmpValue. Return true if the comparison instruction
2448 /// can be analyzed.
2449 bool ARMBaseInstrInfo::analyzeCompare(const MachineInstr &MI, unsigned &SrcReg,
2450 unsigned &SrcReg2, int &CmpMask,
2451 int &CmpValue) const {
2452 switch (MI.getOpcode()) {
2457 SrcReg = MI.getOperand(0).getReg();
2460 CmpValue = MI.getOperand(1).getImm();
2464 SrcReg = MI.getOperand(0).getReg();
2465 SrcReg2 = MI.getOperand(1).getReg();
2471 SrcReg = MI.getOperand(0).getReg();
2473 CmpMask = MI.getOperand(1).getImm();
2481 /// isSuitableForMask - Identify a suitable 'and' instruction that
2482 /// operates on the given source register and applies the same mask
2483 /// as a 'tst' instruction. Provide a limited look-through for copies.
2484 /// When successful, MI will hold the found instruction.
2485 static bool isSuitableForMask(MachineInstr *&MI, unsigned SrcReg,
2486 int CmpMask, bool CommonUse) {
2487 switch (MI->getOpcode()) {
2490 if (CmpMask != MI->getOperand(2).getImm())
2492 if (SrcReg == MI->getOperand(CommonUse ? 1 : 0).getReg())
2500 /// getSwappedCondition - assume the flags are set by MI(a,b), return
2501 /// the condition code if we modify the instructions such that flags are
2503 inline static ARMCC::CondCodes getSwappedCondition(ARMCC::CondCodes CC) {
2505 default: return ARMCC::AL;
2506 case ARMCC::EQ: return ARMCC::EQ;
2507 case ARMCC::NE: return ARMCC::NE;
2508 case ARMCC::HS: return ARMCC::LS;
2509 case ARMCC::LO: return ARMCC::HI;
2510 case ARMCC::HI: return ARMCC::LO;
2511 case ARMCC::LS: return ARMCC::HS;
2512 case ARMCC::GE: return ARMCC::LE;
2513 case ARMCC::LT: return ARMCC::GT;
2514 case ARMCC::GT: return ARMCC::LT;
2515 case ARMCC::LE: return ARMCC::GE;
2519 /// isRedundantFlagInstr - check whether the first instruction, whose only
2520 /// purpose is to update flags, can be made redundant.
2521 /// CMPrr can be made redundant by SUBrr if the operands are the same.
2522 /// CMPri can be made redundant by SUBri if the operands are the same.
2523 /// This function can be extended later on.
2524 inline static bool isRedundantFlagInstr(MachineInstr *CmpI, unsigned SrcReg,
2525 unsigned SrcReg2, int ImmValue,
2527 if ((CmpI->getOpcode() == ARM::CMPrr ||
2528 CmpI->getOpcode() == ARM::t2CMPrr) &&
2529 (OI->getOpcode() == ARM::SUBrr ||
2530 OI->getOpcode() == ARM::t2SUBrr) &&
2531 ((OI->getOperand(1).getReg() == SrcReg &&
2532 OI->getOperand(2).getReg() == SrcReg2) ||
2533 (OI->getOperand(1).getReg() == SrcReg2 &&
2534 OI->getOperand(2).getReg() == SrcReg)))
2537 if ((CmpI->getOpcode() == ARM::CMPri ||
2538 CmpI->getOpcode() == ARM::t2CMPri) &&
2539 (OI->getOpcode() == ARM::SUBri ||
2540 OI->getOpcode() == ARM::t2SUBri) &&
2541 OI->getOperand(1).getReg() == SrcReg &&
2542 OI->getOperand(2).getImm() == ImmValue)
2547 static bool isOptimizeCompareCandidate(MachineInstr *MI, bool &IsThumb1) {
2548 switch (MI->getOpcode()) {
2549 default: return false;
2604 /// optimizeCompareInstr - Convert the instruction supplying the argument to the
2605 /// comparison into one that sets the zero bit in the flags register;
2606 /// Remove a redundant Compare instruction if an earlier instruction can set the
2607 /// flags in the same way as Compare.
2608 /// E.g. SUBrr(r1,r2) and CMPrr(r1,r2). We also handle the case where two
2609 /// operands are swapped: SUBrr(r1,r2) and CMPrr(r2,r1), by updating the
2610 /// condition code of instructions which use the flags.
2611 bool ARMBaseInstrInfo::optimizeCompareInstr(
2612 MachineInstr &CmpInstr, unsigned SrcReg, unsigned SrcReg2, int CmpMask,
2613 int CmpValue, const MachineRegisterInfo *MRI) const {
2614 // Get the unique definition of SrcReg.
2615 MachineInstr *MI = MRI->getUniqueVRegDef(SrcReg);
2616 if (!MI) return false;
2618 // Masked compares sometimes use the same register as the corresponding 'and'.
2619 if (CmpMask != ~0) {
2620 if (!isSuitableForMask(MI, SrcReg, CmpMask, false) || isPredicated(*MI)) {
2622 for (MachineRegisterInfo::use_instr_iterator
2623 UI = MRI->use_instr_begin(SrcReg), UE = MRI->use_instr_end();
2625 if (UI->getParent() != CmpInstr.getParent())
2627 MachineInstr *PotentialAND = &*UI;
2628 if (!isSuitableForMask(PotentialAND, SrcReg, CmpMask, true) ||
2629 isPredicated(*PotentialAND))
2634 if (!MI) return false;
2638 // Get ready to iterate backward from CmpInstr.
2639 MachineBasicBlock::iterator I = CmpInstr, E = MI,
2640 B = CmpInstr.getParent()->begin();
2642 // Early exit if CmpInstr is at the beginning of the BB.
2643 if (I == B) return false;
2645 // There are two possible candidates which can be changed to set CPSR:
2646 // One is MI, the other is a SUB instruction.
2647 // For CMPrr(r1,r2), we are looking for SUB(r1,r2) or SUB(r2,r1).
2648 // For CMPri(r1, CmpValue), we are looking for SUBri(r1, CmpValue).
2649 MachineInstr *Sub = nullptr;
2651 // MI is not a candidate for CMPrr.
2653 else if (MI->getParent() != CmpInstr.getParent() || CmpValue != 0) {
2654 // Conservatively refuse to convert an instruction which isn't in the same
2655 // BB as the comparison.
2656 // For CMPri w/ CmpValue != 0, a Sub may still be a candidate.
2657 // Thus we cannot return here.
2658 if (CmpInstr.getOpcode() == ARM::CMPri ||
2659 CmpInstr.getOpcode() == ARM::t2CMPri)
2665 bool IsThumb1 = false;
2666 if (MI && !isOptimizeCompareCandidate(MI, IsThumb1))
2669 // We also want to do this peephole for cases like this: if (a*b == 0),
2670 // and optimise away the CMP instruction from the generated code sequence:
2671 // MULS, MOVS, MOVS, CMP. Here the MOVS instructions load the boolean values
2672 // resulting from the select instruction, but these MOVS instructions for
2673 // Thumb1 (V6M) are flag setting and are thus preventing this optimisation.
2674 // However, if we only have MOVS instructions in between the CMP and the
2675 // other instruction (the MULS in this example), then the CPSR is dead so we
2676 // can safely reorder the sequence into: MOVS, MOVS, MULS, CMP. We do this
2677 // reordering and then continue the analysis hoping we can eliminate the
2678 // CMP. This peephole works on the vregs, so is still in SSA form. As a
2679 // consequence, the movs won't redefine/kill the MUL operands which would
2680 // make this reordering illegal.
2681 if (MI && IsThumb1) {
2683 bool CanReorder = true;
2684 const bool HasStmts = I != E;
2685 for (; I != E; --I) {
2686 if (I->getOpcode() != ARM::tMOVi8) {
2691 if (HasStmts && CanReorder) {
2692 MI = MI->removeFromParent();
2694 CmpInstr.getParent()->insert(E, MI);
2700 // Check that CPSR isn't set between the comparison instruction and the one we
2701 // want to change. At the same time, search for Sub.
2702 const TargetRegisterInfo *TRI = &getRegisterInfo();
2704 for (; I != E; --I) {
2705 const MachineInstr &Instr = *I;
2707 if (Instr.modifiesRegister(ARM::CPSR, TRI) ||
2708 Instr.readsRegister(ARM::CPSR, TRI))
2709 // This instruction modifies or uses CPSR after the one we want to
2710 // change. We can't do this transformation.
2713 // Check whether CmpInstr can be made redundant by the current instruction.
2714 if (isRedundantFlagInstr(&CmpInstr, SrcReg, SrcReg2, CmpValue, &*I)) {
2720 // The 'and' is below the comparison instruction.
2724 // Return false if no candidates exist.
2728 // The single candidate is called MI.
2731 // We can't use a predicated instruction - it doesn't always write the flags.
2732 if (isPredicated(*MI))
2735 // Scan forward for the use of CPSR
2736 // When checking against MI: if it's a conditional code that requires
2737 // checking of the V bit or C bit, then this is not safe to do.
2738 // It is safe to remove CmpInstr if CPSR is redefined or killed.
2739 // If we are done with the basic block, we need to check whether CPSR is
2741 SmallVector<std::pair<MachineOperand*, ARMCC::CondCodes>, 4>
2743 bool isSafe = false;
2745 E = CmpInstr.getParent()->end();
2746 while (!isSafe && ++I != E) {
2747 const MachineInstr &Instr = *I;
2748 for (unsigned IO = 0, EO = Instr.getNumOperands();
2749 !isSafe && IO != EO; ++IO) {
2750 const MachineOperand &MO = Instr.getOperand(IO);
2751 if (MO.isRegMask() && MO.clobbersPhysReg(ARM::CPSR)) {
2755 if (!MO.isReg() || MO.getReg() != ARM::CPSR)
2761 // Condition code is after the operand before CPSR except for VSELs.
2762 ARMCC::CondCodes CC;
2763 bool IsInstrVSel = true;
2764 switch (Instr.getOpcode()) {
2766 IsInstrVSel = false;
2767 CC = (ARMCC::CondCodes)Instr.getOperand(IO - 1).getImm();
2788 ARMCC::CondCodes NewCC = getSwappedCondition(CC);
2789 if (NewCC == ARMCC::AL)
2791 // If we have SUB(r1, r2) and CMP(r2, r1), the condition code based
2792 // on CMP needs to be updated to be based on SUB.
2793 // Push the condition code operands to OperandsToUpdate.
2794 // If it is safe to remove CmpInstr, the condition code of these
2795 // operands will be modified.
2796 if (SrcReg2 != 0 && Sub->getOperand(1).getReg() == SrcReg2 &&
2797 Sub->getOperand(2).getReg() == SrcReg) {
2798 // VSel doesn't support condition code update.
2801 OperandsToUpdate.push_back(
2802 std::make_pair(&((*I).getOperand(IO - 1)), NewCC));
2805 // No Sub, so this is x = <op> y, z; cmp x, 0.
2807 case ARMCC::EQ: // Z
2808 case ARMCC::NE: // Z
2809 case ARMCC::MI: // N
2810 case ARMCC::PL: // N
2811 case ARMCC::AL: // none
2812 // CPSR can be used multiple times, we should continue.
2814 case ARMCC::HS: // C
2815 case ARMCC::LO: // C
2816 case ARMCC::VS: // V
2817 case ARMCC::VC: // V
2818 case ARMCC::HI: // C Z
2819 case ARMCC::LS: // C Z
2820 case ARMCC::GE: // N V
2821 case ARMCC::LT: // N V
2822 case ARMCC::GT: // Z N V
2823 case ARMCC::LE: // Z N V
2824 // The instruction uses the V bit or C bit which is not safe.
2831 // If CPSR is not killed nor re-defined, we should check whether it is
2832 // live-out. If it is live-out, do not optimize.
2834 MachineBasicBlock *MBB = CmpInstr.getParent();
2835 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
2836 SE = MBB->succ_end(); SI != SE; ++SI)
2837 if ((*SI)->isLiveIn(ARM::CPSR))
2841 // Toggle the optional operand to CPSR (if it exists - in Thumb1 we always
2842 // set CPSR so this is represented as an explicit output)
2844 MI->getOperand(5).setReg(ARM::CPSR);
2845 MI->getOperand(5).setIsDef(true);
2847 assert(!isPredicated(*MI) && "Can't use flags from predicated instruction");
2848 CmpInstr.eraseFromParent();
2850 // Modify the condition code of operands in OperandsToUpdate.
2851 // Since we have SUB(r1, r2) and CMP(r2, r1), the condition code needs to
2852 // be changed from r2 > r1 to r1 < r2, from r2 < r1 to r1 > r2, etc.
2853 for (unsigned i = 0, e = OperandsToUpdate.size(); i < e; i++)
2854 OperandsToUpdate[i].first->setImm(OperandsToUpdate[i].second);
2859 bool ARMBaseInstrInfo::FoldImmediate(MachineInstr &UseMI, MachineInstr &DefMI,
2861 MachineRegisterInfo *MRI) const {
2862 // Fold large immediates into add, sub, or, xor.
2863 unsigned DefOpc = DefMI.getOpcode();
2864 if (DefOpc != ARM::t2MOVi32imm && DefOpc != ARM::MOVi32imm)
2866 if (!DefMI.getOperand(1).isImm())
2867 // Could be t2MOVi32imm <ga:xx>
2870 if (!MRI->hasOneNonDBGUse(Reg))
2873 const MCInstrDesc &DefMCID = DefMI.getDesc();
2874 if (DefMCID.hasOptionalDef()) {
2875 unsigned NumOps = DefMCID.getNumOperands();
2876 const MachineOperand &MO = DefMI.getOperand(NumOps - 1);
2877 if (MO.getReg() == ARM::CPSR && !MO.isDead())
2878 // If DefMI defines CPSR and it is not dead, it's obviously not safe
2883 const MCInstrDesc &UseMCID = UseMI.getDesc();
2884 if (UseMCID.hasOptionalDef()) {
2885 unsigned NumOps = UseMCID.getNumOperands();
2886 if (UseMI.getOperand(NumOps - 1).getReg() == ARM::CPSR)
2887 // If the instruction sets the flag, do not attempt this optimization
2888 // since it may change the semantics of the code.
2892 unsigned UseOpc = UseMI.getOpcode();
2893 unsigned NewUseOpc = 0;
2894 uint32_t ImmVal = (uint32_t)DefMI.getOperand(1).getImm();
2895 uint32_t SOImmValV1 = 0, SOImmValV2 = 0;
2896 bool Commute = false;
2898 default: return false;
2906 case ARM::t2EORrr: {
2907 Commute = UseMI.getOperand(2).getReg() != Reg;
2912 if (UseOpc == ARM::SUBrr && Commute)
2915 // ADD/SUB are special because they're essentially the same operation, so
2916 // we can handle a larger range of immediates.
2917 if (ARM_AM::isSOImmTwoPartVal(ImmVal))
2918 NewUseOpc = UseOpc == ARM::ADDrr ? ARM::ADDri : ARM::SUBri;
2919 else if (ARM_AM::isSOImmTwoPartVal(-ImmVal)) {
2921 NewUseOpc = UseOpc == ARM::ADDrr ? ARM::SUBri : ARM::ADDri;
2924 SOImmValV1 = (uint32_t)ARM_AM::getSOImmTwoPartFirst(ImmVal);
2925 SOImmValV2 = (uint32_t)ARM_AM::getSOImmTwoPartSecond(ImmVal);
2929 if (!ARM_AM::isSOImmTwoPartVal(ImmVal))
2931 SOImmValV1 = (uint32_t)ARM_AM::getSOImmTwoPartFirst(ImmVal);
2932 SOImmValV2 = (uint32_t)ARM_AM::getSOImmTwoPartSecond(ImmVal);
2935 case ARM::ORRrr: NewUseOpc = ARM::ORRri; break;
2936 case ARM::EORrr: NewUseOpc = ARM::EORri; break;
2941 if (UseOpc == ARM::t2SUBrr && Commute)
2944 // ADD/SUB are special because they're essentially the same operation, so
2945 // we can handle a larger range of immediates.
2946 if (ARM_AM::isT2SOImmTwoPartVal(ImmVal))
2947 NewUseOpc = UseOpc == ARM::t2ADDrr ? ARM::t2ADDri : ARM::t2SUBri;
2948 else if (ARM_AM::isT2SOImmTwoPartVal(-ImmVal)) {
2950 NewUseOpc = UseOpc == ARM::t2ADDrr ? ARM::t2SUBri : ARM::t2ADDri;
2953 SOImmValV1 = (uint32_t)ARM_AM::getT2SOImmTwoPartFirst(ImmVal);
2954 SOImmValV2 = (uint32_t)ARM_AM::getT2SOImmTwoPartSecond(ImmVal);
2958 if (!ARM_AM::isT2SOImmTwoPartVal(ImmVal))
2960 SOImmValV1 = (uint32_t)ARM_AM::getT2SOImmTwoPartFirst(ImmVal);
2961 SOImmValV2 = (uint32_t)ARM_AM::getT2SOImmTwoPartSecond(ImmVal);
2964 case ARM::t2ORRrr: NewUseOpc = ARM::t2ORRri; break;
2965 case ARM::t2EORrr: NewUseOpc = ARM::t2EORri; break;
2972 unsigned OpIdx = Commute ? 2 : 1;
2973 unsigned Reg1 = UseMI.getOperand(OpIdx).getReg();
2974 bool isKill = UseMI.getOperand(OpIdx).isKill();
2975 unsigned NewReg = MRI->createVirtualRegister(MRI->getRegClass(Reg));
2976 BuildMI(*UseMI.getParent(), UseMI, UseMI.getDebugLoc(), get(NewUseOpc),
2978 .addReg(Reg1, getKillRegState(isKill))
2980 .add(predOps(ARMCC::AL))
2982 UseMI.setDesc(get(NewUseOpc));
2983 UseMI.getOperand(1).setReg(NewReg);
2984 UseMI.getOperand(1).setIsKill();
2985 UseMI.getOperand(2).ChangeToImmediate(SOImmValV2);
2986 DefMI.eraseFromParent();
2990 static unsigned getNumMicroOpsSwiftLdSt(const InstrItineraryData *ItinData,
2991 const MachineInstr &MI) {
2992 switch (MI.getOpcode()) {
2994 const MCInstrDesc &Desc = MI.getDesc();
2995 int UOps = ItinData->getNumMicroOps(Desc.getSchedClass());
2996 assert(UOps >= 0 && "bad # UOps");
3004 unsigned ShOpVal = MI.getOperand(3).getImm();
3005 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
3006 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3009 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
3010 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
3017 if (!MI.getOperand(2).getReg())
3020 unsigned ShOpVal = MI.getOperand(3).getImm();
3021 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
3022 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3025 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
3026 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
3033 return (ARM_AM::getAM3Op(MI.getOperand(3).getImm()) == ARM_AM::sub) ? 3 : 2;
3035 case ARM::LDRSB_POST:
3036 case ARM::LDRSH_POST: {
3037 unsigned Rt = MI.getOperand(0).getReg();
3038 unsigned Rm = MI.getOperand(3).getReg();
3039 return (Rt == Rm) ? 4 : 3;
3042 case ARM::LDR_PRE_REG:
3043 case ARM::LDRB_PRE_REG: {
3044 unsigned Rt = MI.getOperand(0).getReg();
3045 unsigned Rm = MI.getOperand(3).getReg();
3048 unsigned ShOpVal = MI.getOperand(4).getImm();
3049 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
3050 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3053 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
3054 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
3059 case ARM::STR_PRE_REG:
3060 case ARM::STRB_PRE_REG: {
3061 unsigned ShOpVal = MI.getOperand(4).getImm();
3062 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
3063 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3066 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
3067 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
3073 case ARM::STRH_PRE: {
3074 unsigned Rt = MI.getOperand(0).getReg();
3075 unsigned Rm = MI.getOperand(3).getReg();
3080 return (ARM_AM::getAM3Op(MI.getOperand(4).getImm()) == ARM_AM::sub) ? 3 : 2;
3083 case ARM::LDR_POST_REG:
3084 case ARM::LDRB_POST_REG:
3085 case ARM::LDRH_POST: {
3086 unsigned Rt = MI.getOperand(0).getReg();
3087 unsigned Rm = MI.getOperand(3).getReg();
3088 return (Rt == Rm) ? 3 : 2;
3091 case ARM::LDR_PRE_IMM:
3092 case ARM::LDRB_PRE_IMM:
3093 case ARM::LDR_POST_IMM:
3094 case ARM::LDRB_POST_IMM:
3095 case ARM::STRB_POST_IMM:
3096 case ARM::STRB_POST_REG:
3097 case ARM::STRB_PRE_IMM:
3098 case ARM::STRH_POST:
3099 case ARM::STR_POST_IMM:
3100 case ARM::STR_POST_REG:
3101 case ARM::STR_PRE_IMM:
3104 case ARM::LDRSB_PRE:
3105 case ARM::LDRSH_PRE: {
3106 unsigned Rm = MI.getOperand(3).getReg();
3109 unsigned Rt = MI.getOperand(0).getReg();
3112 unsigned ShOpVal = MI.getOperand(4).getImm();
3113 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
3114 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3117 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
3118 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
3124 unsigned Rt = MI.getOperand(0).getReg();
3125 unsigned Rn = MI.getOperand(2).getReg();
3126 unsigned Rm = MI.getOperand(3).getReg();
3128 return (ARM_AM::getAM3Op(MI.getOperand(4).getImm()) == ARM_AM::sub) ? 4
3130 return (Rt == Rn) ? 3 : 2;
3134 unsigned Rm = MI.getOperand(3).getReg();
3136 return (ARM_AM::getAM3Op(MI.getOperand(4).getImm()) == ARM_AM::sub) ? 4
3141 case ARM::LDRD_POST:
3142 case ARM::t2LDRD_POST:
3145 case ARM::STRD_POST:
3146 case ARM::t2STRD_POST:
3149 case ARM::LDRD_PRE: {
3150 unsigned Rt = MI.getOperand(0).getReg();
3151 unsigned Rn = MI.getOperand(3).getReg();
3152 unsigned Rm = MI.getOperand(4).getReg();
3154 return (ARM_AM::getAM3Op(MI.getOperand(5).getImm()) == ARM_AM::sub) ? 5
3156 return (Rt == Rn) ? 4 : 3;
3159 case ARM::t2LDRD_PRE: {
3160 unsigned Rt = MI.getOperand(0).getReg();
3161 unsigned Rn = MI.getOperand(3).getReg();
3162 return (Rt == Rn) ? 4 : 3;
3165 case ARM::STRD_PRE: {
3166 unsigned Rm = MI.getOperand(4).getReg();
3168 return (ARM_AM::getAM3Op(MI.getOperand(5).getImm()) == ARM_AM::sub) ? 5
3173 case ARM::t2STRD_PRE:
3176 case ARM::t2LDR_POST:
3177 case ARM::t2LDRB_POST:
3178 case ARM::t2LDRB_PRE:
3179 case ARM::t2LDRSBi12:
3180 case ARM::t2LDRSBi8:
3181 case ARM::t2LDRSBpci:
3183 case ARM::t2LDRH_POST:
3184 case ARM::t2LDRH_PRE:
3186 case ARM::t2LDRSB_POST:
3187 case ARM::t2LDRSB_PRE:
3188 case ARM::t2LDRSH_POST:
3189 case ARM::t2LDRSH_PRE:
3190 case ARM::t2LDRSHi12:
3191 case ARM::t2LDRSHi8:
3192 case ARM::t2LDRSHpci:
3196 case ARM::t2LDRDi8: {
3197 unsigned Rt = MI.getOperand(0).getReg();
3198 unsigned Rn = MI.getOperand(2).getReg();
3199 return (Rt == Rn) ? 3 : 2;
3202 case ARM::t2STRB_POST:
3203 case ARM::t2STRB_PRE:
3206 case ARM::t2STRH_POST:
3207 case ARM::t2STRH_PRE:
3209 case ARM::t2STR_POST:
3210 case ARM::t2STR_PRE:
3216 // Return the number of 32-bit words loaded by LDM or stored by STM. If this
3217 // can't be easily determined return 0 (missing MachineMemOperand).
3219 // FIXME: The current MachineInstr design does not support relying on machine
3220 // mem operands to determine the width of a memory access. Instead, we expect
3221 // the target to provide this information based on the instruction opcode and
3222 // operands. However, using MachineMemOperand is the best solution now for
3225 // 1) getNumMicroOps tries to infer LDM memory width from the total number of MI
3226 // operands. This is much more dangerous than using the MachineMemOperand
3227 // sizes because CodeGen passes can insert/remove optional machine operands. In
3228 // fact, it's totally incorrect for preRA passes and appears to be wrong for
3229 // postRA passes as well.
3231 // 2) getNumLDMAddresses is only used by the scheduling machine model and any
3232 // machine model that calls this should handle the unknown (zero size) case.
3234 // Long term, we should require a target hook that verifies MachineMemOperand
3235 // sizes during MC lowering. That target hook should be local to MC lowering
3236 // because we can't ensure that it is aware of other MI forms. Doing this will
3237 // ensure that MachineMemOperands are correctly propagated through all passes.
3238 unsigned ARMBaseInstrInfo::getNumLDMAddresses(const MachineInstr &MI) const {
3240 for (MachineInstr::mmo_iterator I = MI.memoperands_begin(),
3241 E = MI.memoperands_end();
3243 Size += (*I)->getSize();
3248 static unsigned getNumMicroOpsSingleIssuePlusExtras(unsigned Opc,
3250 unsigned UOps = 1 + NumRegs; // 1 for address computation.
3254 case ARM::VLDMDIA_UPD:
3255 case ARM::VLDMDDB_UPD:
3256 case ARM::VLDMSIA_UPD:
3257 case ARM::VLDMSDB_UPD:
3258 case ARM::VSTMDIA_UPD:
3259 case ARM::VSTMDDB_UPD:
3260 case ARM::VSTMSIA_UPD:
3261 case ARM::VSTMSDB_UPD:
3262 case ARM::LDMIA_UPD:
3263 case ARM::LDMDA_UPD:
3264 case ARM::LDMDB_UPD:
3265 case ARM::LDMIB_UPD:
3266 case ARM::STMIA_UPD:
3267 case ARM::STMDA_UPD:
3268 case ARM::STMDB_UPD:
3269 case ARM::STMIB_UPD:
3270 case ARM::tLDMIA_UPD:
3271 case ARM::tSTMIA_UPD:
3272 case ARM::t2LDMIA_UPD:
3273 case ARM::t2LDMDB_UPD:
3274 case ARM::t2STMIA_UPD:
3275 case ARM::t2STMDB_UPD:
3276 ++UOps; // One for base register writeback.
3278 case ARM::LDMIA_RET:
3280 case ARM::t2LDMIA_RET:
3281 UOps += 2; // One for base reg wb, one for write to pc.
3287 unsigned ARMBaseInstrInfo::getNumMicroOps(const InstrItineraryData *ItinData,
3288 const MachineInstr &MI) const {
3289 if (!ItinData || ItinData->isEmpty())
3292 const MCInstrDesc &Desc = MI.getDesc();
3293 unsigned Class = Desc.getSchedClass();
3294 int ItinUOps = ItinData->getNumMicroOps(Class);
3295 if (ItinUOps >= 0) {
3296 if (Subtarget.isSwift() && (Desc.mayLoad() || Desc.mayStore()))
3297 return getNumMicroOpsSwiftLdSt(ItinData, MI);
3302 unsigned Opc = MI.getOpcode();
3305 llvm_unreachable("Unexpected multi-uops instruction!");
3310 // The number of uOps for load / store multiple are determined by the number
3313 // On Cortex-A8, each pair of register loads / stores can be scheduled on the
3314 // same cycle. The scheduling for the first load / store must be done
3315 // separately by assuming the address is not 64-bit aligned.
3317 // On Cortex-A9, the formula is simply (#reg / 2) + (#reg % 2). If the address
3318 // is not 64-bit aligned, then AGU would take an extra cycle. For VFP / NEON
3319 // load / store multiple, the formula is (#reg / 2) + (#reg % 2) + 1.
3321 case ARM::VLDMDIA_UPD:
3322 case ARM::VLDMDDB_UPD:
3324 case ARM::VLDMSIA_UPD:
3325 case ARM::VLDMSDB_UPD:
3327 case ARM::VSTMDIA_UPD:
3328 case ARM::VSTMDDB_UPD:
3330 case ARM::VSTMSIA_UPD:
3331 case ARM::VSTMSDB_UPD: {
3332 unsigned NumRegs = MI.getNumOperands() - Desc.getNumOperands();
3333 return (NumRegs / 2) + (NumRegs % 2) + 1;
3336 case ARM::LDMIA_RET:
3341 case ARM::LDMIA_UPD:
3342 case ARM::LDMDA_UPD:
3343 case ARM::LDMDB_UPD:
3344 case ARM::LDMIB_UPD:
3349 case ARM::STMIA_UPD:
3350 case ARM::STMDA_UPD:
3351 case ARM::STMDB_UPD:
3352 case ARM::STMIB_UPD:
3354 case ARM::tLDMIA_UPD:
3355 case ARM::tSTMIA_UPD:
3359 case ARM::t2LDMIA_RET:
3362 case ARM::t2LDMIA_UPD:
3363 case ARM::t2LDMDB_UPD:
3366 case ARM::t2STMIA_UPD:
3367 case ARM::t2STMDB_UPD: {
3368 unsigned NumRegs = MI.getNumOperands() - Desc.getNumOperands() + 1;
3369 switch (Subtarget.getLdStMultipleTiming()) {
3370 case ARMSubtarget::SingleIssuePlusExtras:
3371 return getNumMicroOpsSingleIssuePlusExtras(Opc, NumRegs);
3372 case ARMSubtarget::SingleIssue:
3373 // Assume the worst.
3375 case ARMSubtarget::DoubleIssue: {
3378 // 4 registers would be issued: 2, 2.
3379 // 5 registers would be issued: 2, 2, 1.
3380 unsigned UOps = (NumRegs / 2);
3385 case ARMSubtarget::DoubleIssueCheckUnalignedAccess: {
3386 unsigned UOps = (NumRegs / 2);
3387 // If there are odd number of registers or if it's not 64-bit aligned,
3388 // then it takes an extra AGU (Address Generation Unit) cycle.
3389 if ((NumRegs % 2) || !MI.hasOneMemOperand() ||
3390 (*MI.memoperands_begin())->getAlignment() < 8)
3397 llvm_unreachable("Didn't find the number of microops");
3401 ARMBaseInstrInfo::getVLDMDefCycle(const InstrItineraryData *ItinData,
3402 const MCInstrDesc &DefMCID,
3404 unsigned DefIdx, unsigned DefAlign) const {
3405 int RegNo = (int)(DefIdx+1) - DefMCID.getNumOperands() + 1;
3407 // Def is the address writeback.
3408 return ItinData->getOperandCycle(DefClass, DefIdx);
3411 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
3412 // (regno / 2) + (regno % 2) + 1
3413 DefCycle = RegNo / 2 + 1;
3416 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
3418 bool isSLoad = false;
3420 switch (DefMCID.getOpcode()) {
3423 case ARM::VLDMSIA_UPD:
3424 case ARM::VLDMSDB_UPD:
3429 // If there are odd number of 'S' registers or if it's not 64-bit aligned,
3430 // then it takes an extra cycle.
3431 if ((isSLoad && (RegNo % 2)) || DefAlign < 8)
3434 // Assume the worst.
3435 DefCycle = RegNo + 2;
3441 bool ARMBaseInstrInfo::isLDMBaseRegInList(const MachineInstr &MI) const {
3442 unsigned BaseReg = MI.getOperand(0).getReg();
3443 for (unsigned i = 1, sz = MI.getNumOperands(); i < sz; ++i) {
3444 const auto &Op = MI.getOperand(i);
3445 if (Op.isReg() && Op.getReg() == BaseReg)
3451 ARMBaseInstrInfo::getLDMVariableDefsSize(const MachineInstr &MI) const {
3452 // ins GPR:$Rn, pred:$p (2xOp), reglist:$regs, variable_ops
3453 // (outs GPR:$wb), (ins GPR:$Rn, pred:$p (2xOp), reglist:$regs, variable_ops)
3454 return MI.getNumOperands() + 1 - MI.getDesc().getNumOperands();
3458 ARMBaseInstrInfo::getLDMDefCycle(const InstrItineraryData *ItinData,
3459 const MCInstrDesc &DefMCID,
3461 unsigned DefIdx, unsigned DefAlign) const {
3462 int RegNo = (int)(DefIdx+1) - DefMCID.getNumOperands() + 1;
3464 // Def is the address writeback.
3465 return ItinData->getOperandCycle(DefClass, DefIdx);
3468 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
3469 // 4 registers would be issued: 1, 2, 1.
3470 // 5 registers would be issued: 1, 2, 2.
3471 DefCycle = RegNo / 2;
3474 // Result latency is issue cycle + 2: E2.
3476 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
3477 DefCycle = (RegNo / 2);
3478 // If there are odd number of registers or if it's not 64-bit aligned,
3479 // then it takes an extra AGU (Address Generation Unit) cycle.
3480 if ((RegNo % 2) || DefAlign < 8)
3482 // Result latency is AGU cycles + 2.
3485 // Assume the worst.
3486 DefCycle = RegNo + 2;
3493 ARMBaseInstrInfo::getVSTMUseCycle(const InstrItineraryData *ItinData,
3494 const MCInstrDesc &UseMCID,
3496 unsigned UseIdx, unsigned UseAlign) const {
3497 int RegNo = (int)(UseIdx+1) - UseMCID.getNumOperands() + 1;
3499 return ItinData->getOperandCycle(UseClass, UseIdx);
3502 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
3503 // (regno / 2) + (regno % 2) + 1
3504 UseCycle = RegNo / 2 + 1;
3507 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
3509 bool isSStore = false;
3511 switch (UseMCID.getOpcode()) {
3514 case ARM::VSTMSIA_UPD:
3515 case ARM::VSTMSDB_UPD:
3520 // If there are odd number of 'S' registers or if it's not 64-bit aligned,
3521 // then it takes an extra cycle.
3522 if ((isSStore && (RegNo % 2)) || UseAlign < 8)
3525 // Assume the worst.
3526 UseCycle = RegNo + 2;
3533 ARMBaseInstrInfo::getSTMUseCycle(const InstrItineraryData *ItinData,
3534 const MCInstrDesc &UseMCID,
3536 unsigned UseIdx, unsigned UseAlign) const {
3537 int RegNo = (int)(UseIdx+1) - UseMCID.getNumOperands() + 1;
3539 return ItinData->getOperandCycle(UseClass, UseIdx);
3542 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
3543 UseCycle = RegNo / 2;
3548 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
3549 UseCycle = (RegNo / 2);
3550 // If there are odd number of registers or if it's not 64-bit aligned,
3551 // then it takes an extra AGU (Address Generation Unit) cycle.
3552 if ((RegNo % 2) || UseAlign < 8)
3555 // Assume the worst.
3562 ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
3563 const MCInstrDesc &DefMCID,
3564 unsigned DefIdx, unsigned DefAlign,
3565 const MCInstrDesc &UseMCID,
3566 unsigned UseIdx, unsigned UseAlign) const {
3567 unsigned DefClass = DefMCID.getSchedClass();
3568 unsigned UseClass = UseMCID.getSchedClass();
3570 if (DefIdx < DefMCID.getNumDefs() && UseIdx < UseMCID.getNumOperands())
3571 return ItinData->getOperandLatency(DefClass, DefIdx, UseClass, UseIdx);
3573 // This may be a def / use of a variable_ops instruction, the operand
3574 // latency might be determinable dynamically. Let the target try to
3577 bool LdmBypass = false;
3578 switch (DefMCID.getOpcode()) {
3580 DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
3584 case ARM::VLDMDIA_UPD:
3585 case ARM::VLDMDDB_UPD:
3587 case ARM::VLDMSIA_UPD:
3588 case ARM::VLDMSDB_UPD:
3589 DefCycle = getVLDMDefCycle(ItinData, DefMCID, DefClass, DefIdx, DefAlign);
3592 case ARM::LDMIA_RET:
3597 case ARM::LDMIA_UPD:
3598 case ARM::LDMDA_UPD:
3599 case ARM::LDMDB_UPD:
3600 case ARM::LDMIB_UPD:
3602 case ARM::tLDMIA_UPD:
3604 case ARM::t2LDMIA_RET:
3607 case ARM::t2LDMIA_UPD:
3608 case ARM::t2LDMDB_UPD:
3610 DefCycle = getLDMDefCycle(ItinData, DefMCID, DefClass, DefIdx, DefAlign);
3615 // We can't seem to determine the result latency of the def, assume it's 2.
3619 switch (UseMCID.getOpcode()) {
3621 UseCycle = ItinData->getOperandCycle(UseClass, UseIdx);
3625 case ARM::VSTMDIA_UPD:
3626 case ARM::VSTMDDB_UPD:
3628 case ARM::VSTMSIA_UPD:
3629 case ARM::VSTMSDB_UPD:
3630 UseCycle = getVSTMUseCycle(ItinData, UseMCID, UseClass, UseIdx, UseAlign);
3637 case ARM::STMIA_UPD:
3638 case ARM::STMDA_UPD:
3639 case ARM::STMDB_UPD:
3640 case ARM::STMIB_UPD:
3641 case ARM::tSTMIA_UPD:
3646 case ARM::t2STMIA_UPD:
3647 case ARM::t2STMDB_UPD:
3648 UseCycle = getSTMUseCycle(ItinData, UseMCID, UseClass, UseIdx, UseAlign);
3653 // Assume it's read in the first stage.
3656 UseCycle = DefCycle - UseCycle + 1;
3659 // It's a variable_ops instruction so we can't use DefIdx here. Just use
3660 // first def operand.
3661 if (ItinData->hasPipelineForwarding(DefClass, DefMCID.getNumOperands()-1,
3664 } else if (ItinData->hasPipelineForwarding(DefClass, DefIdx,
3665 UseClass, UseIdx)) {
3673 static const MachineInstr *getBundledDefMI(const TargetRegisterInfo *TRI,
3674 const MachineInstr *MI, unsigned Reg,
3675 unsigned &DefIdx, unsigned &Dist) {
3678 MachineBasicBlock::const_iterator I = MI; ++I;
3679 MachineBasicBlock::const_instr_iterator II = std::prev(I.getInstrIterator());
3680 assert(II->isInsideBundle() && "Empty bundle?");
3683 while (II->isInsideBundle()) {
3684 Idx = II->findRegisterDefOperandIdx(Reg, false, true, TRI);
3691 assert(Idx != -1 && "Cannot find bundled definition!");
3696 static const MachineInstr *getBundledUseMI(const TargetRegisterInfo *TRI,
3697 const MachineInstr &MI, unsigned Reg,
3698 unsigned &UseIdx, unsigned &Dist) {
3701 MachineBasicBlock::const_instr_iterator II = ++MI.getIterator();
3702 assert(II->isInsideBundle() && "Empty bundle?");
3703 MachineBasicBlock::const_instr_iterator E = MI.getParent()->instr_end();
3705 // FIXME: This doesn't properly handle multiple uses.
3707 while (II != E && II->isInsideBundle()) {
3708 Idx = II->findRegisterUseOperandIdx(Reg, false, TRI);
3711 if (II->getOpcode() != ARM::t2IT)
3725 /// Return the number of cycles to add to (or subtract from) the static
3726 /// itinerary based on the def opcode and alignment. The caller will ensure that
3727 /// adjusted latency is at least one cycle.
3728 static int adjustDefLatency(const ARMSubtarget &Subtarget,
3729 const MachineInstr &DefMI,
3730 const MCInstrDesc &DefMCID, unsigned DefAlign) {
3732 if (Subtarget.isCortexA8() || Subtarget.isLikeA9() || Subtarget.isCortexA7()) {
3733 // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2]
3734 // variants are one cycle cheaper.
3735 switch (DefMCID.getOpcode()) {
3739 unsigned ShOpVal = DefMI.getOperand(3).getImm();
3740 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3742 (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
3749 case ARM::t2LDRSHs: {
3750 // Thumb2 mode: lsl only.
3751 unsigned ShAmt = DefMI.getOperand(3).getImm();
3752 if (ShAmt == 0 || ShAmt == 2)
3757 } else if (Subtarget.isSwift()) {
3758 // FIXME: Properly handle all of the latency adjustments for address
3760 switch (DefMCID.getOpcode()) {
3764 unsigned ShOpVal = DefMI.getOperand(3).getImm();
3765 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
3766 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3769 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
3770 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
3773 ShImm == 1 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsr)
3780 case ARM::t2LDRSHs: {
3781 // Thumb2 mode: lsl only.
3782 unsigned ShAmt = DefMI.getOperand(3).getImm();
3783 if (ShAmt == 0 || ShAmt == 1 || ShAmt == 2 || ShAmt == 3)
3790 if (DefAlign < 8 && Subtarget.checkVLDnAccessAlignment()) {
3791 switch (DefMCID.getOpcode()) {
3797 case ARM::VLD1q8wb_fixed:
3798 case ARM::VLD1q16wb_fixed:
3799 case ARM::VLD1q32wb_fixed:
3800 case ARM::VLD1q64wb_fixed:
3801 case ARM::VLD1q8wb_register:
3802 case ARM::VLD1q16wb_register:
3803 case ARM::VLD1q32wb_register:
3804 case ARM::VLD1q64wb_register:
3811 case ARM::VLD2d8wb_fixed:
3812 case ARM::VLD2d16wb_fixed:
3813 case ARM::VLD2d32wb_fixed:
3814 case ARM::VLD2q8wb_fixed:
3815 case ARM::VLD2q16wb_fixed:
3816 case ARM::VLD2q32wb_fixed:
3817 case ARM::VLD2d8wb_register:
3818 case ARM::VLD2d16wb_register:
3819 case ARM::VLD2d32wb_register:
3820 case ARM::VLD2q8wb_register:
3821 case ARM::VLD2q16wb_register:
3822 case ARM::VLD2q32wb_register:
3827 case ARM::VLD3d8_UPD:
3828 case ARM::VLD3d16_UPD:
3829 case ARM::VLD3d32_UPD:
3830 case ARM::VLD1d64Twb_fixed:
3831 case ARM::VLD1d64Twb_register:
3832 case ARM::VLD3q8_UPD:
3833 case ARM::VLD3q16_UPD:
3834 case ARM::VLD3q32_UPD:
3839 case ARM::VLD4d8_UPD:
3840 case ARM::VLD4d16_UPD:
3841 case ARM::VLD4d32_UPD:
3842 case ARM::VLD1d64Qwb_fixed:
3843 case ARM::VLD1d64Qwb_register:
3844 case ARM::VLD4q8_UPD:
3845 case ARM::VLD4q16_UPD:
3846 case ARM::VLD4q32_UPD:
3847 case ARM::VLD1DUPq8:
3848 case ARM::VLD1DUPq16:
3849 case ARM::VLD1DUPq32:
3850 case ARM::VLD1DUPq8wb_fixed:
3851 case ARM::VLD1DUPq16wb_fixed:
3852 case ARM::VLD1DUPq32wb_fixed:
3853 case ARM::VLD1DUPq8wb_register:
3854 case ARM::VLD1DUPq16wb_register:
3855 case ARM::VLD1DUPq32wb_register:
3856 case ARM::VLD2DUPd8:
3857 case ARM::VLD2DUPd16:
3858 case ARM::VLD2DUPd32:
3859 case ARM::VLD2DUPd8wb_fixed:
3860 case ARM::VLD2DUPd16wb_fixed:
3861 case ARM::VLD2DUPd32wb_fixed:
3862 case ARM::VLD2DUPd8wb_register:
3863 case ARM::VLD2DUPd16wb_register:
3864 case ARM::VLD2DUPd32wb_register:
3865 case ARM::VLD4DUPd8:
3866 case ARM::VLD4DUPd16:
3867 case ARM::VLD4DUPd32:
3868 case ARM::VLD4DUPd8_UPD:
3869 case ARM::VLD4DUPd16_UPD:
3870 case ARM::VLD4DUPd32_UPD:
3872 case ARM::VLD1LNd16:
3873 case ARM::VLD1LNd32:
3874 case ARM::VLD1LNd8_UPD:
3875 case ARM::VLD1LNd16_UPD:
3876 case ARM::VLD1LNd32_UPD:
3878 case ARM::VLD2LNd16:
3879 case ARM::VLD2LNd32:
3880 case ARM::VLD2LNq16:
3881 case ARM::VLD2LNq32:
3882 case ARM::VLD2LNd8_UPD:
3883 case ARM::VLD2LNd16_UPD:
3884 case ARM::VLD2LNd32_UPD:
3885 case ARM::VLD2LNq16_UPD:
3886 case ARM::VLD2LNq32_UPD:
3888 case ARM::VLD4LNd16:
3889 case ARM::VLD4LNd32:
3890 case ARM::VLD4LNq16:
3891 case ARM::VLD4LNq32:
3892 case ARM::VLD4LNd8_UPD:
3893 case ARM::VLD4LNd16_UPD:
3894 case ARM::VLD4LNd32_UPD:
3895 case ARM::VLD4LNq16_UPD:
3896 case ARM::VLD4LNq32_UPD:
3897 // If the address is not 64-bit aligned, the latencies of these
3898 // instructions increases by one.
3906 int ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
3907 const MachineInstr &DefMI,
3909 const MachineInstr &UseMI,
3910 unsigned UseIdx) const {
3911 // No operand latency. The caller may fall back to getInstrLatency.
3912 if (!ItinData || ItinData->isEmpty())
3915 const MachineOperand &DefMO = DefMI.getOperand(DefIdx);
3916 unsigned Reg = DefMO.getReg();
3918 const MachineInstr *ResolvedDefMI = &DefMI;
3919 unsigned DefAdj = 0;
3920 if (DefMI.isBundle())
3922 getBundledDefMI(&getRegisterInfo(), &DefMI, Reg, DefIdx, DefAdj);
3923 if (ResolvedDefMI->isCopyLike() || ResolvedDefMI->isInsertSubreg() ||
3924 ResolvedDefMI->isRegSequence() || ResolvedDefMI->isImplicitDef()) {
3928 const MachineInstr *ResolvedUseMI = &UseMI;
3929 unsigned UseAdj = 0;
3930 if (UseMI.isBundle()) {
3932 getBundledUseMI(&getRegisterInfo(), UseMI, Reg, UseIdx, UseAdj);
3937 return getOperandLatencyImpl(
3938 ItinData, *ResolvedDefMI, DefIdx, ResolvedDefMI->getDesc(), DefAdj, DefMO,
3939 Reg, *ResolvedUseMI, UseIdx, ResolvedUseMI->getDesc(), UseAdj);
3942 int ARMBaseInstrInfo::getOperandLatencyImpl(
3943 const InstrItineraryData *ItinData, const MachineInstr &DefMI,
3944 unsigned DefIdx, const MCInstrDesc &DefMCID, unsigned DefAdj,
3945 const MachineOperand &DefMO, unsigned Reg, const MachineInstr &UseMI,
3946 unsigned UseIdx, const MCInstrDesc &UseMCID, unsigned UseAdj) const {
3947 if (Reg == ARM::CPSR) {
3948 if (DefMI.getOpcode() == ARM::FMSTAT) {
3949 // fpscr -> cpsr stalls over 20 cycles on A8 (and earlier?)
3950 return Subtarget.isLikeA9() ? 1 : 20;
3953 // CPSR set and branch can be paired in the same cycle.
3954 if (UseMI.isBranch())
3957 // Otherwise it takes the instruction latency (generally one).
3958 unsigned Latency = getInstrLatency(ItinData, DefMI);
3960 // For Thumb2 and -Os, prefer scheduling CPSR setting instruction close to
3961 // its uses. Instructions which are otherwise scheduled between them may
3962 // incur a code size penalty (not able to use the CPSR setting 16-bit
3964 if (Latency > 0 && Subtarget.isThumb2()) {
3965 const MachineFunction *MF = DefMI.getParent()->getParent();
3966 // FIXME: Use Function::optForSize().
3967 if (MF->getFunction()->hasFnAttribute(Attribute::OptimizeForSize))
3973 if (DefMO.isImplicit() || UseMI.getOperand(UseIdx).isImplicit())
3976 unsigned DefAlign = DefMI.hasOneMemOperand()
3977 ? (*DefMI.memoperands_begin())->getAlignment()
3979 unsigned UseAlign = UseMI.hasOneMemOperand()
3980 ? (*UseMI.memoperands_begin())->getAlignment()
3983 // Get the itinerary's latency if possible, and handle variable_ops.
3984 int Latency = getOperandLatency(ItinData, DefMCID, DefIdx, DefAlign, UseMCID,
3986 // Unable to find operand latency. The caller may resort to getInstrLatency.
3990 // Adjust for IT block position.
3991 int Adj = DefAdj + UseAdj;
3993 // Adjust for dynamic def-side opcode variants not captured by the itinerary.
3994 Adj += adjustDefLatency(Subtarget, DefMI, DefMCID, DefAlign);
3995 if (Adj >= 0 || (int)Latency > -Adj) {
3996 return Latency + Adj;
3998 // Return the itinerary latency, which may be zero but not less than zero.
4003 ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
4004 SDNode *DefNode, unsigned DefIdx,
4005 SDNode *UseNode, unsigned UseIdx) const {
4006 if (!DefNode->isMachineOpcode())
4009 const MCInstrDesc &DefMCID = get(DefNode->getMachineOpcode());
4011 if (isZeroCost(DefMCID.Opcode))
4014 if (!ItinData || ItinData->isEmpty())
4015 return DefMCID.mayLoad() ? 3 : 1;
4017 if (!UseNode->isMachineOpcode()) {
4018 int Latency = ItinData->getOperandCycle(DefMCID.getSchedClass(), DefIdx);
4019 int Adj = Subtarget.getPreISelOperandLatencyAdjustment();
4020 int Threshold = 1 + Adj;
4021 return Latency <= Threshold ? 1 : Latency - Adj;
4024 const MCInstrDesc &UseMCID = get(UseNode->getMachineOpcode());
4025 const MachineSDNode *DefMN = dyn_cast<MachineSDNode>(DefNode);
4026 unsigned DefAlign = !DefMN->memoperands_empty()
4027 ? (*DefMN->memoperands_begin())->getAlignment() : 0;
4028 const MachineSDNode *UseMN = dyn_cast<MachineSDNode>(UseNode);
4029 unsigned UseAlign = !UseMN->memoperands_empty()
4030 ? (*UseMN->memoperands_begin())->getAlignment() : 0;
4031 int Latency = getOperandLatency(ItinData, DefMCID, DefIdx, DefAlign,
4032 UseMCID, UseIdx, UseAlign);
4035 (Subtarget.isCortexA8() || Subtarget.isLikeA9() ||
4036 Subtarget.isCortexA7())) {
4037 // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2]
4038 // variants are one cycle cheaper.
4039 switch (DefMCID.getOpcode()) {
4044 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
4045 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
4047 (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
4054 case ARM::t2LDRSHs: {
4055 // Thumb2 mode: lsl only.
4057 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
4058 if (ShAmt == 0 || ShAmt == 2)
4063 } else if (DefIdx == 0 && Latency > 2 && Subtarget.isSwift()) {
4064 // FIXME: Properly handle all of the latency adjustments for address
4066 switch (DefMCID.getOpcode()) {
4071 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
4072 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
4074 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
4075 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
4077 else if (ShImm == 1 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsr)
4085 // Thumb2 mode: lsl 0-3 only.
4091 if (DefAlign < 8 && Subtarget.checkVLDnAccessAlignment())
4092 switch (DefMCID.getOpcode()) {
4098 case ARM::VLD1q8wb_register:
4099 case ARM::VLD1q16wb_register:
4100 case ARM::VLD1q32wb_register:
4101 case ARM::VLD1q64wb_register:
4102 case ARM::VLD1q8wb_fixed:
4103 case ARM::VLD1q16wb_fixed:
4104 case ARM::VLD1q32wb_fixed:
4105 case ARM::VLD1q64wb_fixed:
4109 case ARM::VLD2q8Pseudo:
4110 case ARM::VLD2q16Pseudo:
4111 case ARM::VLD2q32Pseudo:
4112 case ARM::VLD2d8wb_fixed:
4113 case ARM::VLD2d16wb_fixed:
4114 case ARM::VLD2d32wb_fixed:
4115 case ARM::VLD2q8PseudoWB_fixed:
4116 case ARM::VLD2q16PseudoWB_fixed:
4117 case ARM::VLD2q32PseudoWB_fixed:
4118 case ARM::VLD2d8wb_register:
4119 case ARM::VLD2d16wb_register:
4120 case ARM::VLD2d32wb_register:
4121 case ARM::VLD2q8PseudoWB_register:
4122 case ARM::VLD2q16PseudoWB_register:
4123 case ARM::VLD2q32PseudoWB_register:
4124 case ARM::VLD3d8Pseudo:
4125 case ARM::VLD3d16Pseudo:
4126 case ARM::VLD3d32Pseudo:
4127 case ARM::VLD1d64TPseudo:
4128 case ARM::VLD1d64TPseudoWB_fixed:
4129 case ARM::VLD3d8Pseudo_UPD:
4130 case ARM::VLD3d16Pseudo_UPD:
4131 case ARM::VLD3d32Pseudo_UPD:
4132 case ARM::VLD3q8Pseudo_UPD:
4133 case ARM::VLD3q16Pseudo_UPD:
4134 case ARM::VLD3q32Pseudo_UPD:
4135 case ARM::VLD3q8oddPseudo:
4136 case ARM::VLD3q16oddPseudo:
4137 case ARM::VLD3q32oddPseudo:
4138 case ARM::VLD3q8oddPseudo_UPD:
4139 case ARM::VLD3q16oddPseudo_UPD:
4140 case ARM::VLD3q32oddPseudo_UPD:
4141 case ARM::VLD4d8Pseudo:
4142 case ARM::VLD4d16Pseudo:
4143 case ARM::VLD4d32Pseudo:
4144 case ARM::VLD1d64QPseudo:
4145 case ARM::VLD1d64QPseudoWB_fixed:
4146 case ARM::VLD4d8Pseudo_UPD:
4147 case ARM::VLD4d16Pseudo_UPD:
4148 case ARM::VLD4d32Pseudo_UPD:
4149 case ARM::VLD4q8Pseudo_UPD:
4150 case ARM::VLD4q16Pseudo_UPD:
4151 case ARM::VLD4q32Pseudo_UPD:
4152 case ARM::VLD4q8oddPseudo:
4153 case ARM::VLD4q16oddPseudo:
4154 case ARM::VLD4q32oddPseudo:
4155 case ARM::VLD4q8oddPseudo_UPD:
4156 case ARM::VLD4q16oddPseudo_UPD:
4157 case ARM::VLD4q32oddPseudo_UPD:
4158 case ARM::VLD1DUPq8:
4159 case ARM::VLD1DUPq16:
4160 case ARM::VLD1DUPq32:
4161 case ARM::VLD1DUPq8wb_fixed:
4162 case ARM::VLD1DUPq16wb_fixed:
4163 case ARM::VLD1DUPq32wb_fixed:
4164 case ARM::VLD1DUPq8wb_register:
4165 case ARM::VLD1DUPq16wb_register:
4166 case ARM::VLD1DUPq32wb_register:
4167 case ARM::VLD2DUPd8:
4168 case ARM::VLD2DUPd16:
4169 case ARM::VLD2DUPd32:
4170 case ARM::VLD2DUPd8wb_fixed:
4171 case ARM::VLD2DUPd16wb_fixed:
4172 case ARM::VLD2DUPd32wb_fixed:
4173 case ARM::VLD2DUPd8wb_register:
4174 case ARM::VLD2DUPd16wb_register:
4175 case ARM::VLD2DUPd32wb_register:
4176 case ARM::VLD4DUPd8Pseudo:
4177 case ARM::VLD4DUPd16Pseudo:
4178 case ARM::VLD4DUPd32Pseudo:
4179 case ARM::VLD4DUPd8Pseudo_UPD:
4180 case ARM::VLD4DUPd16Pseudo_UPD:
4181 case ARM::VLD4DUPd32Pseudo_UPD:
4182 case ARM::VLD1LNq8Pseudo:
4183 case ARM::VLD1LNq16Pseudo:
4184 case ARM::VLD1LNq32Pseudo:
4185 case ARM::VLD1LNq8Pseudo_UPD:
4186 case ARM::VLD1LNq16Pseudo_UPD:
4187 case ARM::VLD1LNq32Pseudo_UPD:
4188 case ARM::VLD2LNd8Pseudo:
4189 case ARM::VLD2LNd16Pseudo:
4190 case ARM::VLD2LNd32Pseudo:
4191 case ARM::VLD2LNq16Pseudo:
4192 case ARM::VLD2LNq32Pseudo:
4193 case ARM::VLD2LNd8Pseudo_UPD:
4194 case ARM::VLD2LNd16Pseudo_UPD:
4195 case ARM::VLD2LNd32Pseudo_UPD:
4196 case ARM::VLD2LNq16Pseudo_UPD:
4197 case ARM::VLD2LNq32Pseudo_UPD:
4198 case ARM::VLD4LNd8Pseudo:
4199 case ARM::VLD4LNd16Pseudo:
4200 case ARM::VLD4LNd32Pseudo:
4201 case ARM::VLD4LNq16Pseudo:
4202 case ARM::VLD4LNq32Pseudo:
4203 case ARM::VLD4LNd8Pseudo_UPD:
4204 case ARM::VLD4LNd16Pseudo_UPD:
4205 case ARM::VLD4LNd32Pseudo_UPD:
4206 case ARM::VLD4LNq16Pseudo_UPD:
4207 case ARM::VLD4LNq32Pseudo_UPD:
4208 // If the address is not 64-bit aligned, the latencies of these
4209 // instructions increases by one.
4217 unsigned ARMBaseInstrInfo::getPredicationCost(const MachineInstr &MI) const {
4218 if (MI.isCopyLike() || MI.isInsertSubreg() || MI.isRegSequence() ||
4225 const MCInstrDesc &MCID = MI.getDesc();
4227 if (MCID.isCall() || (MCID.hasImplicitDefOfPhysReg(ARM::CPSR) &&
4228 !Subtarget.cheapPredicableCPSRDef())) {
4229 // When predicated, CPSR is an additional source operand for CPSR updating
4230 // instructions, this apparently increases their latencies.
4236 unsigned ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
4237 const MachineInstr &MI,
4238 unsigned *PredCost) const {
4239 if (MI.isCopyLike() || MI.isInsertSubreg() || MI.isRegSequence() ||
4243 // An instruction scheduler typically runs on unbundled instructions, however
4244 // other passes may query the latency of a bundled instruction.
4245 if (MI.isBundle()) {
4246 unsigned Latency = 0;
4247 MachineBasicBlock::const_instr_iterator I = MI.getIterator();
4248 MachineBasicBlock::const_instr_iterator E = MI.getParent()->instr_end();
4249 while (++I != E && I->isInsideBundle()) {
4250 if (I->getOpcode() != ARM::t2IT)
4251 Latency += getInstrLatency(ItinData, *I, PredCost);
4256 const MCInstrDesc &MCID = MI.getDesc();
4257 if (PredCost && (MCID.isCall() || (MCID.hasImplicitDefOfPhysReg(ARM::CPSR) &&
4258 !Subtarget.cheapPredicableCPSRDef()))) {
4259 // When predicated, CPSR is an additional source operand for CPSR updating
4260 // instructions, this apparently increases their latencies.
4263 // Be sure to call getStageLatency for an empty itinerary in case it has a
4264 // valid MinLatency property.
4266 return MI.mayLoad() ? 3 : 1;
4268 unsigned Class = MCID.getSchedClass();
4270 // For instructions with variable uops, use uops as latency.
4271 if (!ItinData->isEmpty() && ItinData->getNumMicroOps(Class) < 0)
4272 return getNumMicroOps(ItinData, MI);
4274 // For the common case, fall back on the itinerary's latency.
4275 unsigned Latency = ItinData->getStageLatency(Class);
4277 // Adjust for dynamic def-side opcode variants not captured by the itinerary.
4279 MI.hasOneMemOperand() ? (*MI.memoperands_begin())->getAlignment() : 0;
4280 int Adj = adjustDefLatency(Subtarget, MI, MCID, DefAlign);
4281 if (Adj >= 0 || (int)Latency > -Adj) {
4282 return Latency + Adj;
4287 int ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
4288 SDNode *Node) const {
4289 if (!Node->isMachineOpcode())
4292 if (!ItinData || ItinData->isEmpty())
4295 unsigned Opcode = Node->getMachineOpcode();
4298 return ItinData->getStageLatency(get(Opcode).getSchedClass());
4305 bool ARMBaseInstrInfo::hasHighOperandLatency(const TargetSchedModel &SchedModel,
4306 const MachineRegisterInfo *MRI,
4307 const MachineInstr &DefMI,
4309 const MachineInstr &UseMI,
4310 unsigned UseIdx) const {
4311 unsigned DDomain = DefMI.getDesc().TSFlags & ARMII::DomainMask;
4312 unsigned UDomain = UseMI.getDesc().TSFlags & ARMII::DomainMask;
4313 if (Subtarget.nonpipelinedVFP() &&
4314 (DDomain == ARMII::DomainVFP || UDomain == ARMII::DomainVFP))
4317 // Hoist VFP / NEON instructions with 4 or higher latency.
4319 SchedModel.computeOperandLatency(&DefMI, DefIdx, &UseMI, UseIdx);
4322 return DDomain == ARMII::DomainVFP || DDomain == ARMII::DomainNEON ||
4323 UDomain == ARMII::DomainVFP || UDomain == ARMII::DomainNEON;
4326 bool ARMBaseInstrInfo::hasLowDefLatency(const TargetSchedModel &SchedModel,
4327 const MachineInstr &DefMI,
4328 unsigned DefIdx) const {
4329 const InstrItineraryData *ItinData = SchedModel.getInstrItineraries();
4330 if (!ItinData || ItinData->isEmpty())
4333 unsigned DDomain = DefMI.getDesc().TSFlags & ARMII::DomainMask;
4334 if (DDomain == ARMII::DomainGeneral) {
4335 unsigned DefClass = DefMI.getDesc().getSchedClass();
4336 int DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
4337 return (DefCycle != -1 && DefCycle <= 2);
4342 bool ARMBaseInstrInfo::verifyInstruction(const MachineInstr &MI,
4343 StringRef &ErrInfo) const {
4344 if (convertAddSubFlagsOpcode(MI.getOpcode())) {
4345 ErrInfo = "Pseudo flag setting opcodes only exist in Selection DAG";
4351 // LoadStackGuard has so far only been implemented for MachO. Different code
4352 // sequence is needed for other targets.
4353 void ARMBaseInstrInfo::expandLoadStackGuardBase(MachineBasicBlock::iterator MI,
4354 unsigned LoadImmOpc,
4355 unsigned LoadOpc) const {
4356 assert(!Subtarget.isROPI() && !Subtarget.isRWPI() &&
4357 "ROPI/RWPI not currently supported with stack guard");
4359 MachineBasicBlock &MBB = *MI->getParent();
4360 DebugLoc DL = MI->getDebugLoc();
4361 unsigned Reg = MI->getOperand(0).getReg();
4362 const GlobalValue *GV =
4363 cast<GlobalValue>((*MI->memoperands_begin())->getValue());
4364 MachineInstrBuilder MIB;
4366 BuildMI(MBB, MI, DL, get(LoadImmOpc), Reg)
4367 .addGlobalAddress(GV, 0, ARMII::MO_NONLAZY);
4369 if (Subtarget.isGVIndirectSymbol(GV)) {
4370 MIB = BuildMI(MBB, MI, DL, get(LoadOpc), Reg);
4371 MIB.addReg(Reg, RegState::Kill).addImm(0);
4372 auto Flags = MachineMemOperand::MOLoad |
4373 MachineMemOperand::MODereferenceable |
4374 MachineMemOperand::MOInvariant;
4375 MachineMemOperand *MMO = MBB.getParent()->getMachineMemOperand(
4376 MachinePointerInfo::getGOT(*MBB.getParent()), Flags, 4, 4);
4377 MIB.addMemOperand(MMO).add(predOps(ARMCC::AL));
4380 MIB = BuildMI(MBB, MI, DL, get(LoadOpc), Reg);
4381 MIB.addReg(Reg, RegState::Kill)
4383 .setMemRefs(MI->memoperands_begin(), MI->memoperands_end())
4384 .add(predOps(ARMCC::AL));
4388 ARMBaseInstrInfo::isFpMLxInstruction(unsigned Opcode, unsigned &MulOpc,
4389 unsigned &AddSubOpc,
4390 bool &NegAcc, bool &HasLane) const {
4391 DenseMap<unsigned, unsigned>::const_iterator I = MLxEntryMap.find(Opcode);
4392 if (I == MLxEntryMap.end())
4395 const ARM_MLxEntry &Entry = ARM_MLxTable[I->second];
4396 MulOpc = Entry.MulOpc;
4397 AddSubOpc = Entry.AddSubOpc;
4398 NegAcc = Entry.NegAcc;
4399 HasLane = Entry.HasLane;
4403 //===----------------------------------------------------------------------===//
4404 // Execution domains.
4405 //===----------------------------------------------------------------------===//
4407 // Some instructions go down the NEON pipeline, some go down the VFP pipeline,
4408 // and some can go down both. The vmov instructions go down the VFP pipeline,
4409 // but they can be changed to vorr equivalents that are executed by the NEON
4412 // We use the following execution domain numbering:
4421 // Also see ARMInstrFormats.td and Domain* enums in ARMBaseInfo.h
4423 std::pair<uint16_t, uint16_t>
4424 ARMBaseInstrInfo::getExecutionDomain(const MachineInstr &MI) const {
4425 // If we don't have access to NEON instructions then we won't be able
4426 // to swizzle anything to the NEON domain. Check to make sure.
4427 if (Subtarget.hasNEON()) {
4428 // VMOVD, VMOVRS and VMOVSR are VFP instructions, but can be changed to NEON
4429 // if they are not predicated.
4430 if (MI.getOpcode() == ARM::VMOVD && !isPredicated(MI))
4431 return std::make_pair(ExeVFP, (1 << ExeVFP) | (1 << ExeNEON));
4433 // CortexA9 is particularly picky about mixing the two and wants these
4435 if (Subtarget.useNEONForFPMovs() && !isPredicated(MI) &&
4436 (MI.getOpcode() == ARM::VMOVRS || MI.getOpcode() == ARM::VMOVSR ||
4437 MI.getOpcode() == ARM::VMOVS))
4438 return std::make_pair(ExeVFP, (1 << ExeVFP) | (1 << ExeNEON));
4440 // No other instructions can be swizzled, so just determine their domain.
4441 unsigned Domain = MI.getDesc().TSFlags & ARMII::DomainMask;
4443 if (Domain & ARMII::DomainNEON)
4444 return std::make_pair(ExeNEON, 0);
4446 // Certain instructions can go either way on Cortex-A8.
4447 // Treat them as NEON instructions.
4448 if ((Domain & ARMII::DomainNEONA8) && Subtarget.isCortexA8())
4449 return std::make_pair(ExeNEON, 0);
4451 if (Domain & ARMII::DomainVFP)
4452 return std::make_pair(ExeVFP, 0);
4454 return std::make_pair(ExeGeneric, 0);
4457 static unsigned getCorrespondingDRegAndLane(const TargetRegisterInfo *TRI,
4458 unsigned SReg, unsigned &Lane) {
4459 unsigned DReg = TRI->getMatchingSuperReg(SReg, ARM::ssub_0, &ARM::DPRRegClass);
4462 if (DReg != ARM::NoRegister)
4466 DReg = TRI->getMatchingSuperReg(SReg, ARM::ssub_1, &ARM::DPRRegClass);
4468 assert(DReg && "S-register with no D super-register?");
4472 /// getImplicitSPRUseForDPRUse - Given a use of a DPR register and lane,
4473 /// set ImplicitSReg to a register number that must be marked as implicit-use or
4474 /// zero if no register needs to be defined as implicit-use.
4476 /// If the function cannot determine if an SPR should be marked implicit use or
4477 /// not, it returns false.
4479 /// This function handles cases where an instruction is being modified from taking
4480 /// an SPR to a DPR[Lane]. A use of the DPR is being added, which may conflict
4481 /// with an earlier def of an SPR corresponding to DPR[Lane^1] (i.e. the other
4482 /// lane of the DPR).
4484 /// If the other SPR is defined, an implicit-use of it should be added. Else,
4485 /// (including the case where the DPR itself is defined), it should not.
4487 static bool getImplicitSPRUseForDPRUse(const TargetRegisterInfo *TRI,
4488 MachineInstr &MI, unsigned DReg,
4489 unsigned Lane, unsigned &ImplicitSReg) {
4490 // If the DPR is defined or used already, the other SPR lane will be chained
4491 // correctly, so there is nothing to be done.
4492 if (MI.definesRegister(DReg, TRI) || MI.readsRegister(DReg, TRI)) {
4497 // Otherwise we need to go searching to see if the SPR is set explicitly.
4498 ImplicitSReg = TRI->getSubReg(DReg,
4499 (Lane & 1) ? ARM::ssub_0 : ARM::ssub_1);
4500 MachineBasicBlock::LivenessQueryResult LQR =
4501 MI.getParent()->computeRegisterLiveness(TRI, ImplicitSReg, MI);
4503 if (LQR == MachineBasicBlock::LQR_Live)
4505 else if (LQR == MachineBasicBlock::LQR_Unknown)
4508 // If the register is known not to be live, there is no need to add an
4514 void ARMBaseInstrInfo::setExecutionDomain(MachineInstr &MI,
4515 unsigned Domain) const {
4516 unsigned DstReg, SrcReg, DReg;
4518 MachineInstrBuilder MIB(*MI.getParent()->getParent(), MI);
4519 const TargetRegisterInfo *TRI = &getRegisterInfo();
4520 switch (MI.getOpcode()) {
4522 llvm_unreachable("cannot handle opcode!");
4525 if (Domain != ExeNEON)
4528 // Zap the predicate operands.
4529 assert(!isPredicated(MI) && "Cannot predicate a VORRd");
4531 // Make sure we've got NEON instructions.
4532 assert(Subtarget.hasNEON() && "VORRd requires NEON");
4534 // Source instruction is %DDst = VMOVD %DSrc, 14, %noreg (; implicits)
4535 DstReg = MI.getOperand(0).getReg();
4536 SrcReg = MI.getOperand(1).getReg();
4538 for (unsigned i = MI.getDesc().getNumOperands(); i; --i)
4539 MI.RemoveOperand(i - 1);
4541 // Change to a %DDst = VORRd %DSrc, %DSrc, 14, %noreg (; implicits)
4542 MI.setDesc(get(ARM::VORRd));
4543 MIB.addReg(DstReg, RegState::Define)
4546 .add(predOps(ARMCC::AL));
4549 if (Domain != ExeNEON)
4551 assert(!isPredicated(MI) && "Cannot predicate a VGETLN");
4553 // Source instruction is %RDst = VMOVRS %SSrc, 14, %noreg (; implicits)
4554 DstReg = MI.getOperand(0).getReg();
4555 SrcReg = MI.getOperand(1).getReg();
4557 for (unsigned i = MI.getDesc().getNumOperands(); i; --i)
4558 MI.RemoveOperand(i - 1);
4560 DReg = getCorrespondingDRegAndLane(TRI, SrcReg, Lane);
4562 // Convert to %RDst = VGETLNi32 %DSrc, Lane, 14, %noreg (; imps)
4563 // Note that DSrc has been widened and the other lane may be undef, which
4564 // contaminates the entire register.
4565 MI.setDesc(get(ARM::VGETLNi32));
4566 MIB.addReg(DstReg, RegState::Define)
4567 .addReg(DReg, RegState::Undef)
4569 .add(predOps(ARMCC::AL));
4571 // The old source should be an implicit use, otherwise we might think it
4572 // was dead before here.
4573 MIB.addReg(SrcReg, RegState::Implicit);
4576 if (Domain != ExeNEON)
4578 assert(!isPredicated(MI) && "Cannot predicate a VSETLN");
4580 // Source instruction is %SDst = VMOVSR %RSrc, 14, %noreg (; implicits)
4581 DstReg = MI.getOperand(0).getReg();
4582 SrcReg = MI.getOperand(1).getReg();
4584 DReg = getCorrespondingDRegAndLane(TRI, DstReg, Lane);
4586 unsigned ImplicitSReg;
4587 if (!getImplicitSPRUseForDPRUse(TRI, MI, DReg, Lane, ImplicitSReg))
4590 for (unsigned i = MI.getDesc().getNumOperands(); i; --i)
4591 MI.RemoveOperand(i - 1);
4593 // Convert to %DDst = VSETLNi32 %DDst, %RSrc, Lane, 14, %noreg (; imps)
4594 // Again DDst may be undefined at the beginning of this instruction.
4595 MI.setDesc(get(ARM::VSETLNi32));
4596 MIB.addReg(DReg, RegState::Define)
4597 .addReg(DReg, getUndefRegState(!MI.readsRegister(DReg, TRI)))
4600 .add(predOps(ARMCC::AL));
4602 // The narrower destination must be marked as set to keep previous chains
4604 MIB.addReg(DstReg, RegState::Define | RegState::Implicit);
4605 if (ImplicitSReg != 0)
4606 MIB.addReg(ImplicitSReg, RegState::Implicit);
4610 if (Domain != ExeNEON)
4613 // Source instruction is %SDst = VMOVS %SSrc, 14, %noreg (; implicits)
4614 DstReg = MI.getOperand(0).getReg();
4615 SrcReg = MI.getOperand(1).getReg();
4617 unsigned DstLane = 0, SrcLane = 0, DDst, DSrc;
4618 DDst = getCorrespondingDRegAndLane(TRI, DstReg, DstLane);
4619 DSrc = getCorrespondingDRegAndLane(TRI, SrcReg, SrcLane);
4621 unsigned ImplicitSReg;
4622 if (!getImplicitSPRUseForDPRUse(TRI, MI, DSrc, SrcLane, ImplicitSReg))
4625 for (unsigned i = MI.getDesc().getNumOperands(); i; --i)
4626 MI.RemoveOperand(i - 1);
4629 // Destination can be:
4630 // %DDst = VDUPLN32d %DDst, Lane, 14, %noreg (; implicits)
4631 MI.setDesc(get(ARM::VDUPLN32d));
4632 MIB.addReg(DDst, RegState::Define)
4633 .addReg(DDst, getUndefRegState(!MI.readsRegister(DDst, TRI)))
4635 .add(predOps(ARMCC::AL));
4637 // Neither the source or the destination are naturally represented any
4638 // more, so add them in manually.
4639 MIB.addReg(DstReg, RegState::Implicit | RegState::Define);
4640 MIB.addReg(SrcReg, RegState::Implicit);
4641 if (ImplicitSReg != 0)
4642 MIB.addReg(ImplicitSReg, RegState::Implicit);
4646 // In general there's no single instruction that can perform an S <-> S
4647 // move in NEON space, but a pair of VEXT instructions *can* do the
4648 // job. It turns out that the VEXTs needed will only use DSrc once, with
4649 // the position based purely on the combination of lane-0 and lane-1
4650 // involved. For example
4651 // vmov s0, s2 -> vext.32 d0, d0, d1, #1 vext.32 d0, d0, d0, #1
4652 // vmov s1, s3 -> vext.32 d0, d1, d0, #1 vext.32 d0, d0, d0, #1
4653 // vmov s0, s3 -> vext.32 d0, d0, d0, #1 vext.32 d0, d1, d0, #1
4654 // vmov s1, s2 -> vext.32 d0, d0, d0, #1 vext.32 d0, d0, d1, #1
4656 // Pattern of the MachineInstrs is:
4657 // %DDst = VEXTd32 %DSrc1, %DSrc2, Lane, 14, %noreg (;implicits)
4658 MachineInstrBuilder NewMIB;
4659 NewMIB = BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), get(ARM::VEXTd32),
4662 // On the first instruction, both DSrc and DDst may be <undef> if present.
4663 // Specifically when the original instruction didn't have them as an
4665 unsigned CurReg = SrcLane == 1 && DstLane == 1 ? DSrc : DDst;
4666 bool CurUndef = !MI.readsRegister(CurReg, TRI);
4667 NewMIB.addReg(CurReg, getUndefRegState(CurUndef));
4669 CurReg = SrcLane == 0 && DstLane == 0 ? DSrc : DDst;
4670 CurUndef = !MI.readsRegister(CurReg, TRI);
4671 NewMIB.addReg(CurReg, getUndefRegState(CurUndef))
4673 .add(predOps(ARMCC::AL));
4675 if (SrcLane == DstLane)
4676 NewMIB.addReg(SrcReg, RegState::Implicit);
4678 MI.setDesc(get(ARM::VEXTd32));
4679 MIB.addReg(DDst, RegState::Define);
4681 // On the second instruction, DDst has definitely been defined above, so
4682 // it is not <undef>. DSrc, if present, can be <undef> as above.
4683 CurReg = SrcLane == 1 && DstLane == 0 ? DSrc : DDst;
4684 CurUndef = CurReg == DSrc && !MI.readsRegister(CurReg, TRI);
4685 MIB.addReg(CurReg, getUndefRegState(CurUndef));
4687 CurReg = SrcLane == 0 && DstLane == 1 ? DSrc : DDst;
4688 CurUndef = CurReg == DSrc && !MI.readsRegister(CurReg, TRI);
4689 MIB.addReg(CurReg, getUndefRegState(CurUndef))
4691 .add(predOps(ARMCC::AL));
4693 if (SrcLane != DstLane)
4694 MIB.addReg(SrcReg, RegState::Implicit);
4696 // As before, the original destination is no longer represented, add it
4698 MIB.addReg(DstReg, RegState::Define | RegState::Implicit);
4699 if (ImplicitSReg != 0)
4700 MIB.addReg(ImplicitSReg, RegState::Implicit);
4706 //===----------------------------------------------------------------------===//
4707 // Partial register updates
4708 //===----------------------------------------------------------------------===//
4710 // Swift renames NEON registers with 64-bit granularity. That means any
4711 // instruction writing an S-reg implicitly reads the containing D-reg. The
4712 // problem is mostly avoided by translating f32 operations to v2f32 operations
4713 // on D-registers, but f32 loads are still a problem.
4715 // These instructions can load an f32 into a NEON register:
4717 // VLDRS - Only writes S, partial D update.
4718 // VLD1LNd32 - Writes all D-regs, explicit partial D update, 2 uops.
4719 // VLD1DUPd32 - Writes all D-regs, no partial reg update, 2 uops.
4721 // FCONSTD can be used as a dependency-breaking instruction.
4722 unsigned ARMBaseInstrInfo::getPartialRegUpdateClearance(
4723 const MachineInstr &MI, unsigned OpNum,
4724 const TargetRegisterInfo *TRI) const {
4725 auto PartialUpdateClearance = Subtarget.getPartialUpdateClearance();
4726 if (!PartialUpdateClearance)
4729 assert(TRI && "Need TRI instance");
4731 const MachineOperand &MO = MI.getOperand(OpNum);
4734 unsigned Reg = MO.getReg();
4737 switch (MI.getOpcode()) {
4738 // Normal instructions writing only an S-register.
4743 case ARM::VMOVv4i16:
4744 case ARM::VMOVv2i32:
4745 case ARM::VMOVv2f32:
4746 case ARM::VMOVv1i64:
4747 UseOp = MI.findRegisterUseOperandIdx(Reg, false, TRI);
4750 // Explicitly reads the dependency.
4751 case ARM::VLD1LNd32:
4758 // If this instruction actually reads a value from Reg, there is no unwanted
4760 if (UseOp != -1 && MI.getOperand(UseOp).readsReg())
4763 // We must be able to clobber the whole D-reg.
4764 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
4765 // Virtual register must be a foo:ssub_0<def,undef> operand.
4766 if (!MO.getSubReg() || MI.readsVirtualRegister(Reg))
4768 } else if (ARM::SPRRegClass.contains(Reg)) {
4769 // Physical register: MI must define the full D-reg.
4770 unsigned DReg = TRI->getMatchingSuperReg(Reg, ARM::ssub_0,
4772 if (!DReg || !MI.definesRegister(DReg, TRI))
4776 // MI has an unwanted D-register dependency.
4777 // Avoid defs in the previous N instructrions.
4778 return PartialUpdateClearance;
4781 // Break a partial register dependency after getPartialRegUpdateClearance
4782 // returned non-zero.
4783 void ARMBaseInstrInfo::breakPartialRegDependency(
4784 MachineInstr &MI, unsigned OpNum, const TargetRegisterInfo *TRI) const {
4785 assert(OpNum < MI.getDesc().getNumDefs() && "OpNum is not a def");
4786 assert(TRI && "Need TRI instance");
4788 const MachineOperand &MO = MI.getOperand(OpNum);
4789 unsigned Reg = MO.getReg();
4790 assert(TargetRegisterInfo::isPhysicalRegister(Reg) &&
4791 "Can't break virtual register dependencies.");
4792 unsigned DReg = Reg;
4794 // If MI defines an S-reg, find the corresponding D super-register.
4795 if (ARM::SPRRegClass.contains(Reg)) {
4796 DReg = ARM::D0 + (Reg - ARM::S0) / 2;
4797 assert(TRI->isSuperRegister(Reg, DReg) && "Register enums broken");
4800 assert(ARM::DPRRegClass.contains(DReg) && "Can only break D-reg deps");
4801 assert(MI.definesRegister(DReg, TRI) && "MI doesn't clobber full D-reg");
4803 // FIXME: In some cases, VLDRS can be changed to a VLD1DUPd32 which defines
4804 // the full D-register by loading the same value to both lanes. The
4805 // instruction is micro-coded with 2 uops, so don't do this until we can
4806 // properly schedule micro-coded instructions. The dispatcher stalls cause
4807 // too big regressions.
4809 // Insert the dependency-breaking FCONSTD before MI.
4810 // 96 is the encoding of 0.5, but the actual value doesn't matter here.
4811 BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), get(ARM::FCONSTD), DReg)
4813 .add(predOps(ARMCC::AL));
4814 MI.addRegisterKilled(DReg, TRI, true);
4817 bool ARMBaseInstrInfo::hasNOP() const {
4818 return Subtarget.getFeatureBits()[ARM::HasV6KOps];
4821 bool ARMBaseInstrInfo::isSwiftFastImmShift(const MachineInstr *MI) const {
4822 if (MI->getNumOperands() < 4)
4824 unsigned ShOpVal = MI->getOperand(3).getImm();
4825 unsigned ShImm = ARM_AM::getSORegOffset(ShOpVal);
4826 // Swift supports faster shifts for: lsl 2, lsl 1, and lsr 1.
4827 if ((ShImm == 1 && ARM_AM::getSORegShOp(ShOpVal) == ARM_AM::lsr) ||
4828 ((ShImm == 1 || ShImm == 2) &&
4829 ARM_AM::getSORegShOp(ShOpVal) == ARM_AM::lsl))
4835 bool ARMBaseInstrInfo::getRegSequenceLikeInputs(
4836 const MachineInstr &MI, unsigned DefIdx,
4837 SmallVectorImpl<RegSubRegPairAndIdx> &InputRegs) const {
4838 assert(DefIdx < MI.getDesc().getNumDefs() && "Invalid definition index");
4839 assert(MI.isRegSequenceLike() && "Invalid kind of instruction");
4841 switch (MI.getOpcode()) {
4843 // dX = VMOVDRR rY, rZ
4845 // dX = REG_SEQUENCE rY, ssub_0, rZ, ssub_1
4846 // Populate the InputRegs accordingly.
4848 const MachineOperand *MOReg = &MI.getOperand(1);
4849 InputRegs.push_back(
4850 RegSubRegPairAndIdx(MOReg->getReg(), MOReg->getSubReg(), ARM::ssub_0));
4852 MOReg = &MI.getOperand(2);
4853 InputRegs.push_back(
4854 RegSubRegPairAndIdx(MOReg->getReg(), MOReg->getSubReg(), ARM::ssub_1));
4857 llvm_unreachable("Target dependent opcode missing");
4860 bool ARMBaseInstrInfo::getExtractSubregLikeInputs(
4861 const MachineInstr &MI, unsigned DefIdx,
4862 RegSubRegPairAndIdx &InputReg) const {
4863 assert(DefIdx < MI.getDesc().getNumDefs() && "Invalid definition index");
4864 assert(MI.isExtractSubregLike() && "Invalid kind of instruction");
4866 switch (MI.getOpcode()) {
4868 // rX, rY = VMOVRRD dZ
4870 // rX = EXTRACT_SUBREG dZ, ssub_0
4871 // rY = EXTRACT_SUBREG dZ, ssub_1
4872 const MachineOperand &MOReg = MI.getOperand(2);
4873 InputReg.Reg = MOReg.getReg();
4874 InputReg.SubReg = MOReg.getSubReg();
4875 InputReg.SubIdx = DefIdx == 0 ? ARM::ssub_0 : ARM::ssub_1;
4878 llvm_unreachable("Target dependent opcode missing");
4881 bool ARMBaseInstrInfo::getInsertSubregLikeInputs(
4882 const MachineInstr &MI, unsigned DefIdx, RegSubRegPair &BaseReg,
4883 RegSubRegPairAndIdx &InsertedReg) const {
4884 assert(DefIdx < MI.getDesc().getNumDefs() && "Invalid definition index");
4885 assert(MI.isInsertSubregLike() && "Invalid kind of instruction");
4887 switch (MI.getOpcode()) {
4888 case ARM::VSETLNi32:
4889 // dX = VSETLNi32 dY, rZ, imm
4890 const MachineOperand &MOBaseReg = MI.getOperand(1);
4891 const MachineOperand &MOInsertedReg = MI.getOperand(2);
4892 const MachineOperand &MOIndex = MI.getOperand(3);
4893 BaseReg.Reg = MOBaseReg.getReg();
4894 BaseReg.SubReg = MOBaseReg.getSubReg();
4896 InsertedReg.Reg = MOInsertedReg.getReg();
4897 InsertedReg.SubReg = MOInsertedReg.getSubReg();
4898 InsertedReg.SubIdx = MOIndex.getImm() == 0 ? ARM::ssub_0 : ARM::ssub_1;
4901 llvm_unreachable("Target dependent opcode missing");