1 //===- ARMBaseInstrInfo.cpp - ARM Instruction Information -------*- C++ -*-===//
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"
16 #include "ARMConstantPoolValue.h"
17 #include "ARMHazardRecognizer.h"
18 #include "ARMMachineFunctionInfo.h"
19 #include "ARMRegisterInfo.h"
20 #include "MCTargetDesc/ARMAddressingModes.h"
21 #include "llvm/Constants.h"
22 #include "llvm/Function.h"
23 #include "llvm/GlobalValue.h"
24 #include "llvm/CodeGen/LiveVariables.h"
25 #include "llvm/CodeGen/MachineConstantPool.h"
26 #include "llvm/CodeGen/MachineFrameInfo.h"
27 #include "llvm/CodeGen/MachineInstrBuilder.h"
28 #include "llvm/CodeGen/MachineJumpTableInfo.h"
29 #include "llvm/CodeGen/MachineMemOperand.h"
30 #include "llvm/CodeGen/MachineRegisterInfo.h"
31 #include "llvm/CodeGen/PseudoSourceValue.h"
32 #include "llvm/CodeGen/SelectionDAGNodes.h"
33 #include "llvm/MC/MCAsmInfo.h"
34 #include "llvm/Support/BranchProbability.h"
35 #include "llvm/Support/CommandLine.h"
36 #include "llvm/Support/Debug.h"
37 #include "llvm/Support/ErrorHandling.h"
38 #include "llvm/ADT/STLExtras.h"
40 #define GET_INSTRINFO_CTOR
41 #include "ARMGenInstrInfo.inc"
46 EnableARM3Addr("enable-arm-3-addr-conv", cl::Hidden,
47 cl::desc("Enable ARM 2-addr to 3-addr conv"));
50 WidenVMOVS("widen-vmovs", cl::Hidden,
51 cl::desc("Widen ARM vmovs to vmovd when possible"));
53 /// ARM_MLxEntry - Record information about MLA / MLS instructions.
55 unsigned MLxOpc; // MLA / MLS opcode
56 unsigned MulOpc; // Expanded multiplication opcode
57 unsigned AddSubOpc; // Expanded add / sub opcode
58 bool NegAcc; // True if the acc is negated before the add / sub.
59 bool HasLane; // True if instruction has an extra "lane" operand.
62 static const ARM_MLxEntry ARM_MLxTable[] = {
63 // MLxOpc, MulOpc, AddSubOpc, NegAcc, HasLane
65 { ARM::VMLAS, ARM::VMULS, ARM::VADDS, false, false },
66 { ARM::VMLSS, ARM::VMULS, ARM::VSUBS, false, false },
67 { ARM::VMLAD, ARM::VMULD, ARM::VADDD, false, false },
68 { ARM::VMLSD, ARM::VMULD, ARM::VSUBD, false, false },
69 { ARM::VNMLAS, ARM::VNMULS, ARM::VSUBS, true, false },
70 { ARM::VNMLSS, ARM::VMULS, ARM::VSUBS, true, false },
71 { ARM::VNMLAD, ARM::VNMULD, ARM::VSUBD, true, false },
72 { ARM::VNMLSD, ARM::VMULD, ARM::VSUBD, true, false },
75 { ARM::VMLAfd, ARM::VMULfd, ARM::VADDfd, false, false },
76 { ARM::VMLSfd, ARM::VMULfd, ARM::VSUBfd, false, false },
77 { ARM::VMLAfq, ARM::VMULfq, ARM::VADDfq, false, false },
78 { ARM::VMLSfq, ARM::VMULfq, ARM::VSUBfq, false, false },
79 { ARM::VMLAslfd, ARM::VMULslfd, ARM::VADDfd, false, true },
80 { ARM::VMLSslfd, ARM::VMULslfd, ARM::VSUBfd, false, true },
81 { ARM::VMLAslfq, ARM::VMULslfq, ARM::VADDfq, false, true },
82 { ARM::VMLSslfq, ARM::VMULslfq, ARM::VSUBfq, false, true },
85 ARMBaseInstrInfo::ARMBaseInstrInfo(const ARMSubtarget& STI)
86 : ARMGenInstrInfo(ARM::ADJCALLSTACKDOWN, ARM::ADJCALLSTACKUP),
88 for (unsigned i = 0, e = array_lengthof(ARM_MLxTable); i != e; ++i) {
89 if (!MLxEntryMap.insert(std::make_pair(ARM_MLxTable[i].MLxOpc, i)).second)
90 assert(false && "Duplicated entries?");
91 MLxHazardOpcodes.insert(ARM_MLxTable[i].AddSubOpc);
92 MLxHazardOpcodes.insert(ARM_MLxTable[i].MulOpc);
96 // Use a ScoreboardHazardRecognizer for prepass ARM scheduling. TargetInstrImpl
97 // currently defaults to no prepass hazard recognizer.
98 ScheduleHazardRecognizer *ARMBaseInstrInfo::
99 CreateTargetHazardRecognizer(const TargetMachine *TM,
100 const ScheduleDAG *DAG) const {
101 if (usePreRAHazardRecognizer()) {
102 const InstrItineraryData *II = TM->getInstrItineraryData();
103 return new ScoreboardHazardRecognizer(II, DAG, "pre-RA-sched");
105 return TargetInstrInfoImpl::CreateTargetHazardRecognizer(TM, DAG);
108 ScheduleHazardRecognizer *ARMBaseInstrInfo::
109 CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II,
110 const ScheduleDAG *DAG) const {
111 if (Subtarget.isThumb2() || Subtarget.hasVFP2())
112 return (ScheduleHazardRecognizer *)
113 new ARMHazardRecognizer(II, *this, getRegisterInfo(), Subtarget, DAG);
114 return TargetInstrInfoImpl::CreateTargetPostRAHazardRecognizer(II, DAG);
118 ARMBaseInstrInfo::convertToThreeAddress(MachineFunction::iterator &MFI,
119 MachineBasicBlock::iterator &MBBI,
120 LiveVariables *LV) const {
121 // FIXME: Thumb2 support.
126 MachineInstr *MI = MBBI;
127 MachineFunction &MF = *MI->getParent()->getParent();
128 uint64_t TSFlags = MI->getDesc().TSFlags;
130 switch ((TSFlags & ARMII::IndexModeMask) >> ARMII::IndexModeShift) {
131 default: return NULL;
132 case ARMII::IndexModePre:
135 case ARMII::IndexModePost:
139 // Try splitting an indexed load/store to an un-indexed one plus an add/sub
141 unsigned MemOpc = getUnindexedOpcode(MI->getOpcode());
145 MachineInstr *UpdateMI = NULL;
146 MachineInstr *MemMI = NULL;
147 unsigned AddrMode = (TSFlags & ARMII::AddrModeMask);
148 const MCInstrDesc &MCID = MI->getDesc();
149 unsigned NumOps = MCID.getNumOperands();
150 bool isLoad = !MCID.mayStore();
151 const MachineOperand &WB = isLoad ? MI->getOperand(1) : MI->getOperand(0);
152 const MachineOperand &Base = MI->getOperand(2);
153 const MachineOperand &Offset = MI->getOperand(NumOps-3);
154 unsigned WBReg = WB.getReg();
155 unsigned BaseReg = Base.getReg();
156 unsigned OffReg = Offset.getReg();
157 unsigned OffImm = MI->getOperand(NumOps-2).getImm();
158 ARMCC::CondCodes Pred = (ARMCC::CondCodes)MI->getOperand(NumOps-1).getImm();
161 assert(false && "Unknown indexed op!");
163 case ARMII::AddrMode2: {
164 bool isSub = ARM_AM::getAM2Op(OffImm) == ARM_AM::sub;
165 unsigned Amt = ARM_AM::getAM2Offset(OffImm);
167 if (ARM_AM::getSOImmVal(Amt) == -1)
168 // Can't encode it in a so_imm operand. This transformation will
169 // add more than 1 instruction. Abandon!
171 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
172 get(isSub ? ARM::SUBri : ARM::ADDri), WBReg)
173 .addReg(BaseReg).addImm(Amt)
174 .addImm(Pred).addReg(0).addReg(0);
175 } else if (Amt != 0) {
176 ARM_AM::ShiftOpc ShOpc = ARM_AM::getAM2ShiftOpc(OffImm);
177 unsigned SOOpc = ARM_AM::getSORegOpc(ShOpc, Amt);
178 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
179 get(isSub ? ARM::SUBrsi : ARM::ADDrsi), WBReg)
180 .addReg(BaseReg).addReg(OffReg).addReg(0).addImm(SOOpc)
181 .addImm(Pred).addReg(0).addReg(0);
183 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
184 get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
185 .addReg(BaseReg).addReg(OffReg)
186 .addImm(Pred).addReg(0).addReg(0);
189 case ARMII::AddrMode3 : {
190 bool isSub = ARM_AM::getAM3Op(OffImm) == ARM_AM::sub;
191 unsigned Amt = ARM_AM::getAM3Offset(OffImm);
193 // Immediate is 8-bits. It's guaranteed to fit in a so_imm operand.
194 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
195 get(isSub ? ARM::SUBri : ARM::ADDri), WBReg)
196 .addReg(BaseReg).addImm(Amt)
197 .addImm(Pred).addReg(0).addReg(0);
199 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
200 get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
201 .addReg(BaseReg).addReg(OffReg)
202 .addImm(Pred).addReg(0).addReg(0);
207 std::vector<MachineInstr*> NewMIs;
210 MemMI = BuildMI(MF, MI->getDebugLoc(),
211 get(MemOpc), MI->getOperand(0).getReg())
212 .addReg(WBReg).addImm(0).addImm(Pred);
214 MemMI = BuildMI(MF, MI->getDebugLoc(),
215 get(MemOpc)).addReg(MI->getOperand(1).getReg())
216 .addReg(WBReg).addReg(0).addImm(0).addImm(Pred);
217 NewMIs.push_back(MemMI);
218 NewMIs.push_back(UpdateMI);
221 MemMI = BuildMI(MF, MI->getDebugLoc(),
222 get(MemOpc), MI->getOperand(0).getReg())
223 .addReg(BaseReg).addImm(0).addImm(Pred);
225 MemMI = BuildMI(MF, MI->getDebugLoc(),
226 get(MemOpc)).addReg(MI->getOperand(1).getReg())
227 .addReg(BaseReg).addReg(0).addImm(0).addImm(Pred);
229 UpdateMI->getOperand(0).setIsDead();
230 NewMIs.push_back(UpdateMI);
231 NewMIs.push_back(MemMI);
234 // Transfer LiveVariables states, kill / dead info.
236 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
237 MachineOperand &MO = MI->getOperand(i);
238 if (MO.isReg() && TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
239 unsigned Reg = MO.getReg();
241 LiveVariables::VarInfo &VI = LV->getVarInfo(Reg);
243 MachineInstr *NewMI = (Reg == WBReg) ? UpdateMI : MemMI;
245 LV->addVirtualRegisterDead(Reg, NewMI);
247 if (MO.isUse() && MO.isKill()) {
248 for (unsigned j = 0; j < 2; ++j) {
249 // Look at the two new MI's in reverse order.
250 MachineInstr *NewMI = NewMIs[j];
251 if (!NewMI->readsRegister(Reg))
253 LV->addVirtualRegisterKilled(Reg, NewMI);
254 if (VI.removeKill(MI))
255 VI.Kills.push_back(NewMI);
263 MFI->insert(MBBI, NewMIs[1]);
264 MFI->insert(MBBI, NewMIs[0]);
270 ARMBaseInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
271 MachineBasicBlock *&FBB,
272 SmallVectorImpl<MachineOperand> &Cond,
273 bool AllowModify) const {
274 // If the block has no terminators, it just falls into the block after it.
275 MachineBasicBlock::iterator I = MBB.end();
276 if (I == MBB.begin())
279 while (I->isDebugValue()) {
280 if (I == MBB.begin())
284 if (!isUnpredicatedTerminator(I))
287 // Get the last instruction in the block.
288 MachineInstr *LastInst = I;
290 // If there is only one terminator instruction, process it.
291 unsigned LastOpc = LastInst->getOpcode();
292 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
293 if (isUncondBranchOpcode(LastOpc)) {
294 TBB = LastInst->getOperand(0).getMBB();
297 if (isCondBranchOpcode(LastOpc)) {
298 // Block ends with fall-through condbranch.
299 TBB = LastInst->getOperand(0).getMBB();
300 Cond.push_back(LastInst->getOperand(1));
301 Cond.push_back(LastInst->getOperand(2));
304 return true; // Can't handle indirect branch.
307 // Get the instruction before it if it is a terminator.
308 MachineInstr *SecondLastInst = I;
309 unsigned SecondLastOpc = SecondLastInst->getOpcode();
311 // If AllowModify is true and the block ends with two or more unconditional
312 // branches, delete all but the first unconditional branch.
313 if (AllowModify && isUncondBranchOpcode(LastOpc)) {
314 while (isUncondBranchOpcode(SecondLastOpc)) {
315 LastInst->eraseFromParent();
316 LastInst = SecondLastInst;
317 LastOpc = LastInst->getOpcode();
318 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
319 // Return now the only terminator is an unconditional branch.
320 TBB = LastInst->getOperand(0).getMBB();
324 SecondLastOpc = SecondLastInst->getOpcode();
329 // If there are three terminators, we don't know what sort of block this is.
330 if (SecondLastInst && I != MBB.begin() && isUnpredicatedTerminator(--I))
333 // If the block ends with a B and a Bcc, handle it.
334 if (isCondBranchOpcode(SecondLastOpc) && isUncondBranchOpcode(LastOpc)) {
335 TBB = SecondLastInst->getOperand(0).getMBB();
336 Cond.push_back(SecondLastInst->getOperand(1));
337 Cond.push_back(SecondLastInst->getOperand(2));
338 FBB = LastInst->getOperand(0).getMBB();
342 // If the block ends with two unconditional branches, handle it. The second
343 // one is not executed, so remove it.
344 if (isUncondBranchOpcode(SecondLastOpc) && isUncondBranchOpcode(LastOpc)) {
345 TBB = SecondLastInst->getOperand(0).getMBB();
348 I->eraseFromParent();
352 // ...likewise if it ends with a branch table followed by an unconditional
353 // branch. The branch folder can create these, and we must get rid of them for
354 // correctness of Thumb constant islands.
355 if ((isJumpTableBranchOpcode(SecondLastOpc) ||
356 isIndirectBranchOpcode(SecondLastOpc)) &&
357 isUncondBranchOpcode(LastOpc)) {
360 I->eraseFromParent();
364 // Otherwise, can't handle this.
369 unsigned ARMBaseInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
370 MachineBasicBlock::iterator I = MBB.end();
371 if (I == MBB.begin()) return 0;
373 while (I->isDebugValue()) {
374 if (I == MBB.begin())
378 if (!isUncondBranchOpcode(I->getOpcode()) &&
379 !isCondBranchOpcode(I->getOpcode()))
382 // Remove the branch.
383 I->eraseFromParent();
387 if (I == MBB.begin()) return 1;
389 if (!isCondBranchOpcode(I->getOpcode()))
392 // Remove the branch.
393 I->eraseFromParent();
398 ARMBaseInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
399 MachineBasicBlock *FBB,
400 const SmallVectorImpl<MachineOperand> &Cond,
402 ARMFunctionInfo *AFI = MBB.getParent()->getInfo<ARMFunctionInfo>();
403 int BOpc = !AFI->isThumbFunction()
404 ? ARM::B : (AFI->isThumb2Function() ? ARM::t2B : ARM::tB);
405 int BccOpc = !AFI->isThumbFunction()
406 ? ARM::Bcc : (AFI->isThumb2Function() ? ARM::t2Bcc : ARM::tBcc);
408 // Shouldn't be a fall through.
409 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
410 assert((Cond.size() == 2 || Cond.size() == 0) &&
411 "ARM branch conditions have two components!");
414 if (Cond.empty()) // Unconditional branch?
415 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB);
417 BuildMI(&MBB, DL, get(BccOpc)).addMBB(TBB)
418 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg());
422 // Two-way conditional branch.
423 BuildMI(&MBB, DL, get(BccOpc)).addMBB(TBB)
424 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg());
425 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB);
429 bool ARMBaseInstrInfo::
430 ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
431 ARMCC::CondCodes CC = (ARMCC::CondCodes)(int)Cond[0].getImm();
432 Cond[0].setImm(ARMCC::getOppositeCondition(CC));
436 bool ARMBaseInstrInfo::
437 PredicateInstruction(MachineInstr *MI,
438 const SmallVectorImpl<MachineOperand> &Pred) const {
439 unsigned Opc = MI->getOpcode();
440 if (isUncondBranchOpcode(Opc)) {
441 MI->setDesc(get(getMatchingCondBranchOpcode(Opc)));
442 MI->addOperand(MachineOperand::CreateImm(Pred[0].getImm()));
443 MI->addOperand(MachineOperand::CreateReg(Pred[1].getReg(), false));
447 int PIdx = MI->findFirstPredOperandIdx();
449 MachineOperand &PMO = MI->getOperand(PIdx);
450 PMO.setImm(Pred[0].getImm());
451 MI->getOperand(PIdx+1).setReg(Pred[1].getReg());
457 bool ARMBaseInstrInfo::
458 SubsumesPredicate(const SmallVectorImpl<MachineOperand> &Pred1,
459 const SmallVectorImpl<MachineOperand> &Pred2) const {
460 if (Pred1.size() > 2 || Pred2.size() > 2)
463 ARMCC::CondCodes CC1 = (ARMCC::CondCodes)Pred1[0].getImm();
464 ARMCC::CondCodes CC2 = (ARMCC::CondCodes)Pred2[0].getImm();
474 return CC2 == ARMCC::HI;
476 return CC2 == ARMCC::LO || CC2 == ARMCC::EQ;
478 return CC2 == ARMCC::GT;
480 return CC2 == ARMCC::LT;
484 bool ARMBaseInstrInfo::DefinesPredicate(MachineInstr *MI,
485 std::vector<MachineOperand> &Pred) const {
486 // FIXME: This confuses implicit_def with optional CPSR def.
487 const MCInstrDesc &MCID = MI->getDesc();
488 if (!MCID.getImplicitDefs() && !MCID.hasOptionalDef())
492 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
493 const MachineOperand &MO = MI->getOperand(i);
494 if (MO.isReg() && MO.getReg() == ARM::CPSR) {
503 /// isPredicable - Return true if the specified instruction can be predicated.
504 /// By default, this returns true for every instruction with a
505 /// PredicateOperand.
506 bool ARMBaseInstrInfo::isPredicable(MachineInstr *MI) const {
507 const MCInstrDesc &MCID = MI->getDesc();
508 if (!MCID.isPredicable())
511 if ((MCID.TSFlags & ARMII::DomainMask) == ARMII::DomainNEON) {
512 ARMFunctionInfo *AFI =
513 MI->getParent()->getParent()->getInfo<ARMFunctionInfo>();
514 return AFI->isThumb2Function();
519 /// FIXME: Works around a gcc miscompilation with -fstrict-aliasing.
520 LLVM_ATTRIBUTE_NOINLINE
521 static unsigned getNumJTEntries(const std::vector<MachineJumpTableEntry> &JT,
523 static unsigned getNumJTEntries(const std::vector<MachineJumpTableEntry> &JT,
525 assert(JTI < JT.size());
526 return JT[JTI].MBBs.size();
529 /// GetInstSize - Return the size of the specified MachineInstr.
531 unsigned ARMBaseInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
532 const MachineBasicBlock &MBB = *MI->getParent();
533 const MachineFunction *MF = MBB.getParent();
534 const MCAsmInfo *MAI = MF->getTarget().getMCAsmInfo();
536 const MCInstrDesc &MCID = MI->getDesc();
538 return MCID.getSize();
540 // If this machine instr is an inline asm, measure it.
541 if (MI->getOpcode() == ARM::INLINEASM)
542 return getInlineAsmLength(MI->getOperand(0).getSymbolName(), *MAI);
545 unsigned Opc = MI->getOpcode();
547 case TargetOpcode::IMPLICIT_DEF:
548 case TargetOpcode::KILL:
549 case TargetOpcode::PROLOG_LABEL:
550 case TargetOpcode::EH_LABEL:
551 case TargetOpcode::DBG_VALUE:
553 case ARM::MOVi16_ga_pcrel:
554 case ARM::MOVTi16_ga_pcrel:
555 case ARM::t2MOVi16_ga_pcrel:
556 case ARM::t2MOVTi16_ga_pcrel:
559 case ARM::t2MOVi32imm:
561 case ARM::CONSTPOOL_ENTRY:
562 // If this machine instr is a constant pool entry, its size is recorded as
564 return MI->getOperand(2).getImm();
565 case ARM::Int_eh_sjlj_longjmp:
567 case ARM::tInt_eh_sjlj_longjmp:
569 case ARM::Int_eh_sjlj_setjmp:
570 case ARM::Int_eh_sjlj_setjmp_nofp:
572 case ARM::tInt_eh_sjlj_setjmp:
573 case ARM::t2Int_eh_sjlj_setjmp:
574 case ARM::t2Int_eh_sjlj_setjmp_nofp:
582 case ARM::t2TBH_JT: {
583 // These are jumptable branches, i.e. a branch followed by an inlined
584 // jumptable. The size is 4 + 4 * number of entries. For TBB, each
585 // entry is one byte; TBH two byte each.
586 unsigned EntrySize = (Opc == ARM::t2TBB_JT)
587 ? 1 : ((Opc == ARM::t2TBH_JT) ? 2 : 4);
588 unsigned NumOps = MCID.getNumOperands();
589 MachineOperand JTOP =
590 MI->getOperand(NumOps - (MCID.isPredicable() ? 3 : 2));
591 unsigned JTI = JTOP.getIndex();
592 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
594 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
595 assert(JTI < JT.size());
596 // Thumb instructions are 2 byte aligned, but JT entries are 4 byte
597 // 4 aligned. The assembler / linker may add 2 byte padding just before
598 // the JT entries. The size does not include this padding; the
599 // constant islands pass does separate bookkeeping for it.
600 // FIXME: If we know the size of the function is less than (1 << 16) *2
601 // bytes, we can use 16-bit entries instead. Then there won't be an
603 unsigned InstSize = (Opc == ARM::tBR_JTr || Opc == ARM::t2BR_JT) ? 2 : 4;
604 unsigned NumEntries = getNumJTEntries(JT, JTI);
605 if (Opc == ARM::t2TBB_JT && (NumEntries & 1))
606 // Make sure the instruction that follows TBB is 2-byte aligned.
607 // FIXME: Constant island pass should insert an "ALIGN" instruction
610 return NumEntries * EntrySize + InstSize;
613 // Otherwise, pseudo-instruction sizes are zero.
616 return 0; // Not reached
619 void ARMBaseInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
620 MachineBasicBlock::iterator I, DebugLoc DL,
621 unsigned DestReg, unsigned SrcReg,
622 bool KillSrc) const {
623 bool GPRDest = ARM::GPRRegClass.contains(DestReg);
624 bool GPRSrc = ARM::GPRRegClass.contains(SrcReg);
626 if (GPRDest && GPRSrc) {
627 AddDefaultCC(AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::MOVr), DestReg)
628 .addReg(SrcReg, getKillRegState(KillSrc))));
632 bool SPRDest = ARM::SPRRegClass.contains(DestReg);
633 bool SPRSrc = ARM::SPRRegClass.contains(SrcReg);
636 if (SPRDest && SPRSrc) {
639 // An even S-S copy may be feeding a NEON v2f32 instruction being used for
640 // f32 operations. In that case, it is better to copy the full D-regs with
641 // a VMOVD since that can be converted to a NEON-domain move by
642 // NEONMoveFix.cpp. Check that MI is the original COPY instruction, and
643 // that it really defines the whole D-register.
645 (DestReg - ARM::S0) % 2 == 0 && (SrcReg - ARM::S0) % 2 == 0 &&
646 I != MBB.end() && I->isCopy() &&
647 I->getOperand(0).getReg() == DestReg &&
648 I->getOperand(1).getReg() == SrcReg) {
649 // I is pointing to the ortiginal COPY instruction.
650 // Find the parent D-registers.
651 const TargetRegisterInfo *TRI = &getRegisterInfo();
652 unsigned SrcD = TRI->getMatchingSuperReg(SrcReg, ARM::ssub_0,
654 unsigned DestD = TRI->getMatchingSuperReg(DestReg, ARM::ssub_0,
656 // Be careful to not clobber an INSERT_SUBREG that reads and redefines a
657 // D-register. There must be an <imp-def> of destD, and no <imp-use>.
658 if (I->definesRegister(DestD, TRI) && !I->readsRegister(DestD, TRI)) {
663 KillSrc = I->killsRegister(SrcReg, TRI);
666 } else if (GPRDest && SPRSrc)
668 else if (SPRDest && GPRSrc)
670 else if (ARM::DPRRegClass.contains(DestReg, SrcReg))
672 else if (ARM::QPRRegClass.contains(DestReg, SrcReg))
676 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(Opc), DestReg);
677 MIB.addReg(SrcReg, getKillRegState(KillSrc));
678 if (Opc == ARM::VORRq)
679 MIB.addReg(SrcReg, getKillRegState(KillSrc));
684 // Generate instructions for VMOVQQ and VMOVQQQQ pseudos in place.
685 if (ARM::QQPRRegClass.contains(DestReg, SrcReg) ||
686 ARM::QQQQPRRegClass.contains(DestReg, SrcReg)) {
687 const TargetRegisterInfo *TRI = &getRegisterInfo();
688 assert(ARM::qsub_0 + 3 == ARM::qsub_3 && "Expected contiguous enum.");
689 unsigned EndSubReg = ARM::QQPRRegClass.contains(DestReg, SrcReg) ?
690 ARM::qsub_1 : ARM::qsub_3;
691 for (unsigned i = ARM::qsub_0, e = EndSubReg + 1; i != e; ++i) {
692 unsigned Dst = TRI->getSubReg(DestReg, i);
693 unsigned Src = TRI->getSubReg(SrcReg, i);
694 MachineInstrBuilder Mov =
695 AddDefaultPred(BuildMI(MBB, I, I->getDebugLoc(), get(ARM::VORRq))
696 .addReg(Dst, RegState::Define)
697 .addReg(Src, getKillRegState(KillSrc))
698 .addReg(Src, getKillRegState(KillSrc)));
699 if (i == EndSubReg) {
700 Mov->addRegisterDefined(DestReg, TRI);
702 Mov->addRegisterKilled(SrcReg, TRI);
707 llvm_unreachable("Impossible reg-to-reg copy");
711 MachineInstrBuilder &AddDReg(MachineInstrBuilder &MIB,
712 unsigned Reg, unsigned SubIdx, unsigned State,
713 const TargetRegisterInfo *TRI) {
715 return MIB.addReg(Reg, State);
717 if (TargetRegisterInfo::isPhysicalRegister(Reg))
718 return MIB.addReg(TRI->getSubReg(Reg, SubIdx), State);
719 return MIB.addReg(Reg, State, SubIdx);
722 void ARMBaseInstrInfo::
723 storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
724 unsigned SrcReg, bool isKill, int FI,
725 const TargetRegisterClass *RC,
726 const TargetRegisterInfo *TRI) const {
728 if (I != MBB.end()) DL = I->getDebugLoc();
729 MachineFunction &MF = *MBB.getParent();
730 MachineFrameInfo &MFI = *MF.getFrameInfo();
731 unsigned Align = MFI.getObjectAlignment(FI);
733 MachineMemOperand *MMO =
734 MF.getMachineMemOperand(MachinePointerInfo(
735 PseudoSourceValue::getFixedStack(FI)),
736 MachineMemOperand::MOStore,
737 MFI.getObjectSize(FI),
740 switch (RC->getSize()) {
742 if (ARM::GPRRegClass.hasSubClassEq(RC)) {
743 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::STRi12))
744 .addReg(SrcReg, getKillRegState(isKill))
745 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
746 } else if (ARM::SPRRegClass.hasSubClassEq(RC)) {
747 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTRS))
748 .addReg(SrcReg, getKillRegState(isKill))
749 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
751 llvm_unreachable("Unknown reg class!");
754 if (ARM::DPRRegClass.hasSubClassEq(RC)) {
755 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTRD))
756 .addReg(SrcReg, getKillRegState(isKill))
757 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
759 llvm_unreachable("Unknown reg class!");
762 if (ARM::QPRRegClass.hasSubClassEq(RC)) {
763 if (Align >= 16 && getRegisterInfo().needsStackRealignment(MF)) {
764 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VST1q64Pseudo))
765 .addFrameIndex(FI).addImm(16)
766 .addReg(SrcReg, getKillRegState(isKill))
767 .addMemOperand(MMO));
769 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMQIA))
770 .addReg(SrcReg, getKillRegState(isKill))
772 .addMemOperand(MMO));
775 llvm_unreachable("Unknown reg class!");
778 if (ARM::QQPRRegClass.hasSubClassEq(RC)) {
779 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
780 // FIXME: It's possible to only store part of the QQ register if the
781 // spilled def has a sub-register index.
782 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VST1d64QPseudo))
783 .addFrameIndex(FI).addImm(16)
784 .addReg(SrcReg, getKillRegState(isKill))
785 .addMemOperand(MMO));
787 MachineInstrBuilder MIB =
788 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMDIA))
791 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
792 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
793 MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
794 AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI);
797 llvm_unreachable("Unknown reg class!");
800 if (ARM::QQQQPRRegClass.hasSubClassEq(RC)) {
801 MachineInstrBuilder MIB =
802 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMDIA))
805 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
806 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
807 MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
808 MIB = AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI);
809 MIB = AddDReg(MIB, SrcReg, ARM::dsub_4, 0, TRI);
810 MIB = AddDReg(MIB, SrcReg, ARM::dsub_5, 0, TRI);
811 MIB = AddDReg(MIB, SrcReg, ARM::dsub_6, 0, TRI);
812 AddDReg(MIB, SrcReg, ARM::dsub_7, 0, TRI);
814 llvm_unreachable("Unknown reg class!");
817 llvm_unreachable("Unknown reg class!");
822 ARMBaseInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
823 int &FrameIndex) const {
824 switch (MI->getOpcode()) {
827 case ARM::t2STRs: // FIXME: don't use t2STRs to access frame.
828 if (MI->getOperand(1).isFI() &&
829 MI->getOperand(2).isReg() &&
830 MI->getOperand(3).isImm() &&
831 MI->getOperand(2).getReg() == 0 &&
832 MI->getOperand(3).getImm() == 0) {
833 FrameIndex = MI->getOperand(1).getIndex();
834 return MI->getOperand(0).getReg();
842 if (MI->getOperand(1).isFI() &&
843 MI->getOperand(2).isImm() &&
844 MI->getOperand(2).getImm() == 0) {
845 FrameIndex = MI->getOperand(1).getIndex();
846 return MI->getOperand(0).getReg();
849 case ARM::VST1q64Pseudo:
850 if (MI->getOperand(0).isFI() &&
851 MI->getOperand(2).getSubReg() == 0) {
852 FrameIndex = MI->getOperand(0).getIndex();
853 return MI->getOperand(2).getReg();
857 if (MI->getOperand(1).isFI() &&
858 MI->getOperand(0).getSubReg() == 0) {
859 FrameIndex = MI->getOperand(1).getIndex();
860 return MI->getOperand(0).getReg();
868 unsigned ARMBaseInstrInfo::isStoreToStackSlotPostFE(const MachineInstr *MI,
869 int &FrameIndex) const {
870 const MachineMemOperand *Dummy;
871 return MI->getDesc().mayStore() && hasStoreToStackSlot(MI, Dummy, FrameIndex);
874 void ARMBaseInstrInfo::
875 loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
876 unsigned DestReg, int FI,
877 const TargetRegisterClass *RC,
878 const TargetRegisterInfo *TRI) const {
880 if (I != MBB.end()) DL = I->getDebugLoc();
881 MachineFunction &MF = *MBB.getParent();
882 MachineFrameInfo &MFI = *MF.getFrameInfo();
883 unsigned Align = MFI.getObjectAlignment(FI);
884 MachineMemOperand *MMO =
885 MF.getMachineMemOperand(
886 MachinePointerInfo(PseudoSourceValue::getFixedStack(FI)),
887 MachineMemOperand::MOLoad,
888 MFI.getObjectSize(FI),
891 switch (RC->getSize()) {
893 if (ARM::GPRRegClass.hasSubClassEq(RC)) {
894 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::LDRi12), DestReg)
895 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
897 } else if (ARM::SPRRegClass.hasSubClassEq(RC)) {
898 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDRS), DestReg)
899 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
901 llvm_unreachable("Unknown reg class!");
904 if (ARM::DPRRegClass.hasSubClassEq(RC)) {
905 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDRD), DestReg)
906 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
908 llvm_unreachable("Unknown reg class!");
911 if (ARM::QPRRegClass.hasSubClassEq(RC)) {
912 if (Align >= 16 && getRegisterInfo().needsStackRealignment(MF)) {
913 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLD1q64Pseudo), DestReg)
914 .addFrameIndex(FI).addImm(16)
915 .addMemOperand(MMO));
917 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMQIA), DestReg)
919 .addMemOperand(MMO));
922 llvm_unreachable("Unknown reg class!");
925 if (ARM::QQPRRegClass.hasSubClassEq(RC)) {
926 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
927 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLD1d64QPseudo), DestReg)
928 .addFrameIndex(FI).addImm(16)
929 .addMemOperand(MMO));
931 MachineInstrBuilder MIB =
932 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
935 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::Define, TRI);
936 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::Define, TRI);
937 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::Define, TRI);
938 MIB = AddDReg(MIB, DestReg, ARM::dsub_3, RegState::Define, TRI);
939 MIB.addReg(DestReg, RegState::Define | RegState::Implicit);
942 llvm_unreachable("Unknown reg class!");
945 if (ARM::QQQQPRRegClass.hasSubClassEq(RC)) {
946 MachineInstrBuilder MIB =
947 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
950 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::Define, TRI);
951 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::Define, TRI);
952 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::Define, TRI);
953 MIB = AddDReg(MIB, DestReg, ARM::dsub_3, RegState::Define, TRI);
954 MIB = AddDReg(MIB, DestReg, ARM::dsub_4, RegState::Define, TRI);
955 MIB = AddDReg(MIB, DestReg, ARM::dsub_5, RegState::Define, TRI);
956 MIB = AddDReg(MIB, DestReg, ARM::dsub_6, RegState::Define, TRI);
957 MIB = AddDReg(MIB, DestReg, ARM::dsub_7, RegState::Define, TRI);
958 MIB.addReg(DestReg, RegState::Define | RegState::Implicit);
960 llvm_unreachable("Unknown reg class!");
963 llvm_unreachable("Unknown regclass!");
968 ARMBaseInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
969 int &FrameIndex) const {
970 switch (MI->getOpcode()) {
973 case ARM::t2LDRs: // FIXME: don't use t2LDRs to access frame.
974 if (MI->getOperand(1).isFI() &&
975 MI->getOperand(2).isReg() &&
976 MI->getOperand(3).isImm() &&
977 MI->getOperand(2).getReg() == 0 &&
978 MI->getOperand(3).getImm() == 0) {
979 FrameIndex = MI->getOperand(1).getIndex();
980 return MI->getOperand(0).getReg();
988 if (MI->getOperand(1).isFI() &&
989 MI->getOperand(2).isImm() &&
990 MI->getOperand(2).getImm() == 0) {
991 FrameIndex = MI->getOperand(1).getIndex();
992 return MI->getOperand(0).getReg();
995 case ARM::VLD1q64Pseudo:
996 if (MI->getOperand(1).isFI() &&
997 MI->getOperand(0).getSubReg() == 0) {
998 FrameIndex = MI->getOperand(1).getIndex();
999 return MI->getOperand(0).getReg();
1003 if (MI->getOperand(1).isFI() &&
1004 MI->getOperand(0).getSubReg() == 0) {
1005 FrameIndex = MI->getOperand(1).getIndex();
1006 return MI->getOperand(0).getReg();
1014 unsigned ARMBaseInstrInfo::isLoadFromStackSlotPostFE(const MachineInstr *MI,
1015 int &FrameIndex) const {
1016 const MachineMemOperand *Dummy;
1017 return MI->getDesc().mayLoad() && hasLoadFromStackSlot(MI, Dummy, FrameIndex);
1021 ARMBaseInstrInfo::emitFrameIndexDebugValue(MachineFunction &MF,
1022 int FrameIx, uint64_t Offset,
1023 const MDNode *MDPtr,
1024 DebugLoc DL) const {
1025 MachineInstrBuilder MIB = BuildMI(MF, DL, get(ARM::DBG_VALUE))
1026 .addFrameIndex(FrameIx).addImm(0).addImm(Offset).addMetadata(MDPtr);
1030 /// Create a copy of a const pool value. Update CPI to the new index and return
1032 static unsigned duplicateCPV(MachineFunction &MF, unsigned &CPI) {
1033 MachineConstantPool *MCP = MF.getConstantPool();
1034 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1036 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPI];
1037 assert(MCPE.isMachineConstantPoolEntry() &&
1038 "Expecting a machine constantpool entry!");
1039 ARMConstantPoolValue *ACPV =
1040 static_cast<ARMConstantPoolValue*>(MCPE.Val.MachineCPVal);
1042 unsigned PCLabelId = AFI->createPICLabelUId();
1043 ARMConstantPoolValue *NewCPV = 0;
1044 // FIXME: The below assumes PIC relocation model and that the function
1045 // is Thumb mode (t1 or t2). PCAdjustment would be 8 for ARM mode PIC, and
1046 // zero for non-PIC in ARM or Thumb. The callers are all of thumb LDR
1047 // instructions, so that's probably OK, but is PIC always correct when
1049 if (ACPV->isGlobalValue())
1050 NewCPV = new ARMConstantPoolValue(ACPV->getGV(), PCLabelId,
1052 else if (ACPV->isExtSymbol())
1053 NewCPV = new ARMConstantPoolValue(MF.getFunction()->getContext(),
1054 ACPV->getSymbol(), PCLabelId, 4);
1055 else if (ACPV->isBlockAddress())
1056 NewCPV = new ARMConstantPoolValue(ACPV->getBlockAddress(), PCLabelId,
1057 ARMCP::CPBlockAddress, 4);
1058 else if (ACPV->isLSDA())
1059 NewCPV = new ARMConstantPoolValue(MF.getFunction(), PCLabelId,
1062 llvm_unreachable("Unexpected ARM constantpool value type!!");
1063 CPI = MCP->getConstantPoolIndex(NewCPV, MCPE.getAlignment());
1067 void ARMBaseInstrInfo::
1068 reMaterialize(MachineBasicBlock &MBB,
1069 MachineBasicBlock::iterator I,
1070 unsigned DestReg, unsigned SubIdx,
1071 const MachineInstr *Orig,
1072 const TargetRegisterInfo &TRI) const {
1073 unsigned Opcode = Orig->getOpcode();
1076 MachineInstr *MI = MBB.getParent()->CloneMachineInstr(Orig);
1077 MI->substituteRegister(Orig->getOperand(0).getReg(), DestReg, SubIdx, TRI);
1081 case ARM::tLDRpci_pic:
1082 case ARM::t2LDRpci_pic: {
1083 MachineFunction &MF = *MBB.getParent();
1084 unsigned CPI = Orig->getOperand(1).getIndex();
1085 unsigned PCLabelId = duplicateCPV(MF, CPI);
1086 MachineInstrBuilder MIB = BuildMI(MBB, I, Orig->getDebugLoc(), get(Opcode),
1088 .addConstantPoolIndex(CPI).addImm(PCLabelId);
1089 MIB->setMemRefs(Orig->memoperands_begin(), Orig->memoperands_end());
1096 ARMBaseInstrInfo::duplicate(MachineInstr *Orig, MachineFunction &MF) const {
1097 MachineInstr *MI = TargetInstrInfoImpl::duplicate(Orig, MF);
1098 switch(Orig->getOpcode()) {
1099 case ARM::tLDRpci_pic:
1100 case ARM::t2LDRpci_pic: {
1101 unsigned CPI = Orig->getOperand(1).getIndex();
1102 unsigned PCLabelId = duplicateCPV(MF, CPI);
1103 Orig->getOperand(1).setIndex(CPI);
1104 Orig->getOperand(2).setImm(PCLabelId);
1111 bool ARMBaseInstrInfo::produceSameValue(const MachineInstr *MI0,
1112 const MachineInstr *MI1,
1113 const MachineRegisterInfo *MRI) const {
1114 int Opcode = MI0->getOpcode();
1115 if (Opcode == ARM::t2LDRpci ||
1116 Opcode == ARM::t2LDRpci_pic ||
1117 Opcode == ARM::tLDRpci ||
1118 Opcode == ARM::tLDRpci_pic ||
1119 Opcode == ARM::MOV_ga_dyn ||
1120 Opcode == ARM::MOV_ga_pcrel ||
1121 Opcode == ARM::MOV_ga_pcrel_ldr ||
1122 Opcode == ARM::t2MOV_ga_dyn ||
1123 Opcode == ARM::t2MOV_ga_pcrel) {
1124 if (MI1->getOpcode() != Opcode)
1126 if (MI0->getNumOperands() != MI1->getNumOperands())
1129 const MachineOperand &MO0 = MI0->getOperand(1);
1130 const MachineOperand &MO1 = MI1->getOperand(1);
1131 if (MO0.getOffset() != MO1.getOffset())
1134 if (Opcode == ARM::MOV_ga_dyn ||
1135 Opcode == ARM::MOV_ga_pcrel ||
1136 Opcode == ARM::MOV_ga_pcrel_ldr ||
1137 Opcode == ARM::t2MOV_ga_dyn ||
1138 Opcode == ARM::t2MOV_ga_pcrel)
1139 // Ignore the PC labels.
1140 return MO0.getGlobal() == MO1.getGlobal();
1142 const MachineFunction *MF = MI0->getParent()->getParent();
1143 const MachineConstantPool *MCP = MF->getConstantPool();
1144 int CPI0 = MO0.getIndex();
1145 int CPI1 = MO1.getIndex();
1146 const MachineConstantPoolEntry &MCPE0 = MCP->getConstants()[CPI0];
1147 const MachineConstantPoolEntry &MCPE1 = MCP->getConstants()[CPI1];
1148 bool isARMCP0 = MCPE0.isMachineConstantPoolEntry();
1149 bool isARMCP1 = MCPE1.isMachineConstantPoolEntry();
1150 if (isARMCP0 && isARMCP1) {
1151 ARMConstantPoolValue *ACPV0 =
1152 static_cast<ARMConstantPoolValue*>(MCPE0.Val.MachineCPVal);
1153 ARMConstantPoolValue *ACPV1 =
1154 static_cast<ARMConstantPoolValue*>(MCPE1.Val.MachineCPVal);
1155 return ACPV0->hasSameValue(ACPV1);
1156 } else if (!isARMCP0 && !isARMCP1) {
1157 return MCPE0.Val.ConstVal == MCPE1.Val.ConstVal;
1160 } else if (Opcode == ARM::PICLDR) {
1161 if (MI1->getOpcode() != Opcode)
1163 if (MI0->getNumOperands() != MI1->getNumOperands())
1166 unsigned Addr0 = MI0->getOperand(1).getReg();
1167 unsigned Addr1 = MI1->getOperand(1).getReg();
1168 if (Addr0 != Addr1) {
1170 !TargetRegisterInfo::isVirtualRegister(Addr0) ||
1171 !TargetRegisterInfo::isVirtualRegister(Addr1))
1174 // This assumes SSA form.
1175 MachineInstr *Def0 = MRI->getVRegDef(Addr0);
1176 MachineInstr *Def1 = MRI->getVRegDef(Addr1);
1177 // Check if the loaded value, e.g. a constantpool of a global address, are
1179 if (!produceSameValue(Def0, Def1, MRI))
1183 for (unsigned i = 3, e = MI0->getNumOperands(); i != e; ++i) {
1184 // %vreg12<def> = PICLDR %vreg11, 0, pred:14, pred:%noreg
1185 const MachineOperand &MO0 = MI0->getOperand(i);
1186 const MachineOperand &MO1 = MI1->getOperand(i);
1187 if (!MO0.isIdenticalTo(MO1))
1193 return MI0->isIdenticalTo(MI1, MachineInstr::IgnoreVRegDefs);
1196 /// areLoadsFromSameBasePtr - This is used by the pre-regalloc scheduler to
1197 /// determine if two loads are loading from the same base address. It should
1198 /// only return true if the base pointers are the same and the only differences
1199 /// between the two addresses is the offset. It also returns the offsets by
1201 bool ARMBaseInstrInfo::areLoadsFromSameBasePtr(SDNode *Load1, SDNode *Load2,
1203 int64_t &Offset2) const {
1204 // Don't worry about Thumb: just ARM and Thumb2.
1205 if (Subtarget.isThumb1Only()) return false;
1207 if (!Load1->isMachineOpcode() || !Load2->isMachineOpcode())
1210 switch (Load1->getMachineOpcode()) {
1223 case ARM::t2LDRSHi8:
1225 case ARM::t2LDRSHi12:
1229 switch (Load2->getMachineOpcode()) {
1242 case ARM::t2LDRSHi8:
1244 case ARM::t2LDRSHi12:
1248 // Check if base addresses and chain operands match.
1249 if (Load1->getOperand(0) != Load2->getOperand(0) ||
1250 Load1->getOperand(4) != Load2->getOperand(4))
1253 // Index should be Reg0.
1254 if (Load1->getOperand(3) != Load2->getOperand(3))
1257 // Determine the offsets.
1258 if (isa<ConstantSDNode>(Load1->getOperand(1)) &&
1259 isa<ConstantSDNode>(Load2->getOperand(1))) {
1260 Offset1 = cast<ConstantSDNode>(Load1->getOperand(1))->getSExtValue();
1261 Offset2 = cast<ConstantSDNode>(Load2->getOperand(1))->getSExtValue();
1268 /// shouldScheduleLoadsNear - This is a used by the pre-regalloc scheduler to
1269 /// determine (in conjunction with areLoadsFromSameBasePtr) if two loads should
1270 /// be scheduled togther. On some targets if two loads are loading from
1271 /// addresses in the same cache line, it's better if they are scheduled
1272 /// together. This function takes two integers that represent the load offsets
1273 /// from the common base address. It returns true if it decides it's desirable
1274 /// to schedule the two loads together. "NumLoads" is the number of loads that
1275 /// have already been scheduled after Load1.
1276 bool ARMBaseInstrInfo::shouldScheduleLoadsNear(SDNode *Load1, SDNode *Load2,
1277 int64_t Offset1, int64_t Offset2,
1278 unsigned NumLoads) const {
1279 // Don't worry about Thumb: just ARM and Thumb2.
1280 if (Subtarget.isThumb1Only()) return false;
1282 assert(Offset2 > Offset1);
1284 if ((Offset2 - Offset1) / 8 > 64)
1287 if (Load1->getMachineOpcode() != Load2->getMachineOpcode())
1288 return false; // FIXME: overly conservative?
1290 // Four loads in a row should be sufficient.
1297 bool ARMBaseInstrInfo::isSchedulingBoundary(const MachineInstr *MI,
1298 const MachineBasicBlock *MBB,
1299 const MachineFunction &MF) const {
1300 // Debug info is never a scheduling boundary. It's necessary to be explicit
1301 // due to the special treatment of IT instructions below, otherwise a
1302 // dbg_value followed by an IT will result in the IT instruction being
1303 // considered a scheduling hazard, which is wrong. It should be the actual
1304 // instruction preceding the dbg_value instruction(s), just like it is
1305 // when debug info is not present.
1306 if (MI->isDebugValue())
1309 // Terminators and labels can't be scheduled around.
1310 if (MI->getDesc().isTerminator() || MI->isLabel())
1313 // Treat the start of the IT block as a scheduling boundary, but schedule
1314 // t2IT along with all instructions following it.
1315 // FIXME: This is a big hammer. But the alternative is to add all potential
1316 // true and anti dependencies to IT block instructions as implicit operands
1317 // to the t2IT instruction. The added compile time and complexity does not
1319 MachineBasicBlock::const_iterator I = MI;
1320 // Make sure to skip any dbg_value instructions
1321 while (++I != MBB->end() && I->isDebugValue())
1323 if (I != MBB->end() && I->getOpcode() == ARM::t2IT)
1326 // Don't attempt to schedule around any instruction that defines
1327 // a stack-oriented pointer, as it's unlikely to be profitable. This
1328 // saves compile time, because it doesn't require every single
1329 // stack slot reference to depend on the instruction that does the
1331 if (MI->definesRegister(ARM::SP))
1337 bool ARMBaseInstrInfo::
1338 isProfitableToIfCvt(MachineBasicBlock &MBB,
1339 unsigned NumCycles, unsigned ExtraPredCycles,
1340 const BranchProbability &Probability) const {
1344 // Attempt to estimate the relative costs of predication versus branching.
1345 unsigned UnpredCost = Probability.getNumerator() * NumCycles;
1346 UnpredCost /= Probability.getDenominator();
1347 UnpredCost += 1; // The branch itself
1348 UnpredCost += Subtarget.getMispredictionPenalty() / 10;
1350 return (NumCycles + ExtraPredCycles) <= UnpredCost;
1353 bool ARMBaseInstrInfo::
1354 isProfitableToIfCvt(MachineBasicBlock &TMBB,
1355 unsigned TCycles, unsigned TExtra,
1356 MachineBasicBlock &FMBB,
1357 unsigned FCycles, unsigned FExtra,
1358 const BranchProbability &Probability) const {
1359 if (!TCycles || !FCycles)
1362 // Attempt to estimate the relative costs of predication versus branching.
1363 unsigned TUnpredCost = Probability.getNumerator() * TCycles;
1364 TUnpredCost /= Probability.getDenominator();
1366 uint32_t Comp = Probability.getDenominator() - Probability.getNumerator();
1367 unsigned FUnpredCost = Comp * FCycles;
1368 FUnpredCost /= Probability.getDenominator();
1370 unsigned UnpredCost = TUnpredCost + FUnpredCost;
1371 UnpredCost += 1; // The branch itself
1372 UnpredCost += Subtarget.getMispredictionPenalty() / 10;
1374 return (TCycles + FCycles + TExtra + FExtra) <= UnpredCost;
1377 /// getInstrPredicate - If instruction is predicated, returns its predicate
1378 /// condition, otherwise returns AL. It also returns the condition code
1379 /// register by reference.
1381 llvm::getInstrPredicate(const MachineInstr *MI, unsigned &PredReg) {
1382 int PIdx = MI->findFirstPredOperandIdx();
1388 PredReg = MI->getOperand(PIdx+1).getReg();
1389 return (ARMCC::CondCodes)MI->getOperand(PIdx).getImm();
1393 int llvm::getMatchingCondBranchOpcode(int Opc) {
1396 else if (Opc == ARM::tB)
1398 else if (Opc == ARM::t2B)
1401 llvm_unreachable("Unknown unconditional branch opcode!");
1406 void llvm::emitARMRegPlusImmediate(MachineBasicBlock &MBB,
1407 MachineBasicBlock::iterator &MBBI, DebugLoc dl,
1408 unsigned DestReg, unsigned BaseReg, int NumBytes,
1409 ARMCC::CondCodes Pred, unsigned PredReg,
1410 const ARMBaseInstrInfo &TII, unsigned MIFlags) {
1411 bool isSub = NumBytes < 0;
1412 if (isSub) NumBytes = -NumBytes;
1415 unsigned RotAmt = ARM_AM::getSOImmValRotate(NumBytes);
1416 unsigned ThisVal = NumBytes & ARM_AM::rotr32(0xFF, RotAmt);
1417 assert(ThisVal && "Didn't extract field correctly");
1419 // We will handle these bits from offset, clear them.
1420 NumBytes &= ~ThisVal;
1422 assert(ARM_AM::getSOImmVal(ThisVal) != -1 && "Bit extraction didn't work?");
1424 // Build the new ADD / SUB.
1425 unsigned Opc = isSub ? ARM::SUBri : ARM::ADDri;
1426 BuildMI(MBB, MBBI, dl, TII.get(Opc), DestReg)
1427 .addReg(BaseReg, RegState::Kill).addImm(ThisVal)
1428 .addImm((unsigned)Pred).addReg(PredReg).addReg(0)
1429 .setMIFlags(MIFlags);
1434 bool llvm::rewriteARMFrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
1435 unsigned FrameReg, int &Offset,
1436 const ARMBaseInstrInfo &TII) {
1437 unsigned Opcode = MI.getOpcode();
1438 const MCInstrDesc &Desc = MI.getDesc();
1439 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
1442 // Memory operands in inline assembly always use AddrMode2.
1443 if (Opcode == ARM::INLINEASM)
1444 AddrMode = ARMII::AddrMode2;
1446 if (Opcode == ARM::ADDri) {
1447 Offset += MI.getOperand(FrameRegIdx+1).getImm();
1449 // Turn it into a move.
1450 MI.setDesc(TII.get(ARM::MOVr));
1451 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
1452 MI.RemoveOperand(FrameRegIdx+1);
1455 } else if (Offset < 0) {
1458 MI.setDesc(TII.get(ARM::SUBri));
1461 // Common case: small offset, fits into instruction.
1462 if (ARM_AM::getSOImmVal(Offset) != -1) {
1463 // Replace the FrameIndex with sp / fp
1464 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
1465 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(Offset);
1470 // Otherwise, pull as much of the immedidate into this ADDri/SUBri
1472 unsigned RotAmt = ARM_AM::getSOImmValRotate(Offset);
1473 unsigned ThisImmVal = Offset & ARM_AM::rotr32(0xFF, RotAmt);
1475 // We will handle these bits from offset, clear them.
1476 Offset &= ~ThisImmVal;
1478 // Get the properly encoded SOImmVal field.
1479 assert(ARM_AM::getSOImmVal(ThisImmVal) != -1 &&
1480 "Bit extraction didn't work?");
1481 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(ThisImmVal);
1483 unsigned ImmIdx = 0;
1485 unsigned NumBits = 0;
1488 case ARMII::AddrMode_i12: {
1489 ImmIdx = FrameRegIdx + 1;
1490 InstrOffs = MI.getOperand(ImmIdx).getImm();
1494 case ARMII::AddrMode2: {
1495 ImmIdx = FrameRegIdx+2;
1496 InstrOffs = ARM_AM::getAM2Offset(MI.getOperand(ImmIdx).getImm());
1497 if (ARM_AM::getAM2Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
1502 case ARMII::AddrMode3: {
1503 ImmIdx = FrameRegIdx+2;
1504 InstrOffs = ARM_AM::getAM3Offset(MI.getOperand(ImmIdx).getImm());
1505 if (ARM_AM::getAM3Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
1510 case ARMII::AddrMode4:
1511 case ARMII::AddrMode6:
1512 // Can't fold any offset even if it's zero.
1514 case ARMII::AddrMode5: {
1515 ImmIdx = FrameRegIdx+1;
1516 InstrOffs = ARM_AM::getAM5Offset(MI.getOperand(ImmIdx).getImm());
1517 if (ARM_AM::getAM5Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
1524 llvm_unreachable("Unsupported addressing mode!");
1528 Offset += InstrOffs * Scale;
1529 assert((Offset & (Scale-1)) == 0 && "Can't encode this offset!");
1535 // Attempt to fold address comp. if opcode has offset bits
1537 // Common case: small offset, fits into instruction.
1538 MachineOperand &ImmOp = MI.getOperand(ImmIdx);
1539 int ImmedOffset = Offset / Scale;
1540 unsigned Mask = (1 << NumBits) - 1;
1541 if ((unsigned)Offset <= Mask * Scale) {
1542 // Replace the FrameIndex with sp
1543 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
1544 // FIXME: When addrmode2 goes away, this will simplify (like the
1545 // T2 version), as the LDR.i12 versions don't need the encoding
1546 // tricks for the offset value.
1548 if (AddrMode == ARMII::AddrMode_i12)
1549 ImmedOffset = -ImmedOffset;
1551 ImmedOffset |= 1 << NumBits;
1553 ImmOp.ChangeToImmediate(ImmedOffset);
1558 // Otherwise, it didn't fit. Pull in what we can to simplify the immed.
1559 ImmedOffset = ImmedOffset & Mask;
1561 if (AddrMode == ARMII::AddrMode_i12)
1562 ImmedOffset = -ImmedOffset;
1564 ImmedOffset |= 1 << NumBits;
1566 ImmOp.ChangeToImmediate(ImmedOffset);
1567 Offset &= ~(Mask*Scale);
1571 Offset = (isSub) ? -Offset : Offset;
1575 bool ARMBaseInstrInfo::
1576 AnalyzeCompare(const MachineInstr *MI, unsigned &SrcReg, int &CmpMask,
1577 int &CmpValue) const {
1578 switch (MI->getOpcode()) {
1582 SrcReg = MI->getOperand(0).getReg();
1584 CmpValue = MI->getOperand(1).getImm();
1588 SrcReg = MI->getOperand(0).getReg();
1589 CmpMask = MI->getOperand(1).getImm();
1597 /// isSuitableForMask - Identify a suitable 'and' instruction that
1598 /// operates on the given source register and applies the same mask
1599 /// as a 'tst' instruction. Provide a limited look-through for copies.
1600 /// When successful, MI will hold the found instruction.
1601 static bool isSuitableForMask(MachineInstr *&MI, unsigned SrcReg,
1602 int CmpMask, bool CommonUse) {
1603 switch (MI->getOpcode()) {
1606 if (CmpMask != MI->getOperand(2).getImm())
1608 if (SrcReg == MI->getOperand(CommonUse ? 1 : 0).getReg())
1612 // Walk down one instruction which is potentially an 'and'.
1613 const MachineInstr &Copy = *MI;
1614 MachineBasicBlock::iterator AND(
1615 llvm::next(MachineBasicBlock::iterator(MI)));
1616 if (AND == MI->getParent()->end()) return false;
1618 return isSuitableForMask(MI, Copy.getOperand(0).getReg(),
1626 /// OptimizeCompareInstr - Convert the instruction supplying the argument to the
1627 /// comparison into one that sets the zero bit in the flags register.
1628 bool ARMBaseInstrInfo::
1629 OptimizeCompareInstr(MachineInstr *CmpInstr, unsigned SrcReg, int CmpMask,
1630 int CmpValue, const MachineRegisterInfo *MRI) const {
1634 MachineRegisterInfo::def_iterator DI = MRI->def_begin(SrcReg);
1635 if (llvm::next(DI) != MRI->def_end())
1636 // Only support one definition.
1639 MachineInstr *MI = &*DI;
1641 // Masked compares sometimes use the same register as the corresponding 'and'.
1642 if (CmpMask != ~0) {
1643 if (!isSuitableForMask(MI, SrcReg, CmpMask, false)) {
1645 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(SrcReg),
1646 UE = MRI->use_end(); UI != UE; ++UI) {
1647 if (UI->getParent() != CmpInstr->getParent()) continue;
1648 MachineInstr *PotentialAND = &*UI;
1649 if (!isSuitableForMask(PotentialAND, SrcReg, CmpMask, true))
1654 if (!MI) return false;
1658 // Conservatively refuse to convert an instruction which isn't in the same BB
1659 // as the comparison.
1660 if (MI->getParent() != CmpInstr->getParent())
1663 // Check that CPSR isn't set between the comparison instruction and the one we
1665 MachineBasicBlock::const_iterator I = CmpInstr, E = MI,
1666 B = MI->getParent()->begin();
1668 // Early exit if CmpInstr is at the beginning of the BB.
1669 if (I == B) return false;
1672 for (; I != E; --I) {
1673 const MachineInstr &Instr = *I;
1675 for (unsigned IO = 0, EO = Instr.getNumOperands(); IO != EO; ++IO) {
1676 const MachineOperand &MO = Instr.getOperand(IO);
1677 if (!MO.isReg()) continue;
1679 // This instruction modifies or uses CPSR after the one we want to
1680 // change. We can't do this transformation.
1681 if (MO.getReg() == ARM::CPSR)
1686 // The 'and' is below the comparison instruction.
1690 // Set the "zero" bit in CPSR.
1691 switch (MI->getOpcode()) {
1725 case ARM::t2EORri: {
1726 // Scan forward for the use of CPSR, if it's a conditional code requires
1727 // checking of V bit, then this is not safe to do. If we can't find the
1728 // CPSR use (i.e. used in another block), then it's not safe to perform
1729 // the optimization.
1730 bool isSafe = false;
1732 E = MI->getParent()->end();
1733 while (!isSafe && ++I != E) {
1734 const MachineInstr &Instr = *I;
1735 for (unsigned IO = 0, EO = Instr.getNumOperands();
1736 !isSafe && IO != EO; ++IO) {
1737 const MachineOperand &MO = Instr.getOperand(IO);
1738 if (!MO.isReg() || MO.getReg() != ARM::CPSR)
1744 // Condition code is after the operand before CPSR.
1745 ARMCC::CondCodes CC = (ARMCC::CondCodes)Instr.getOperand(IO-1).getImm();
1764 // Toggle the optional operand to CPSR.
1765 MI->getOperand(5).setReg(ARM::CPSR);
1766 MI->getOperand(5).setIsDef(true);
1767 CmpInstr->eraseFromParent();
1775 bool ARMBaseInstrInfo::FoldImmediate(MachineInstr *UseMI,
1776 MachineInstr *DefMI, unsigned Reg,
1777 MachineRegisterInfo *MRI) const {
1778 // Fold large immediates into add, sub, or, xor.
1779 unsigned DefOpc = DefMI->getOpcode();
1780 if (DefOpc != ARM::t2MOVi32imm && DefOpc != ARM::MOVi32imm)
1782 if (!DefMI->getOperand(1).isImm())
1783 // Could be t2MOVi32imm <ga:xx>
1786 if (!MRI->hasOneNonDBGUse(Reg))
1789 unsigned UseOpc = UseMI->getOpcode();
1790 unsigned NewUseOpc = 0;
1791 uint32_t ImmVal = (uint32_t)DefMI->getOperand(1).getImm();
1792 uint32_t SOImmValV1 = 0, SOImmValV2 = 0;
1793 bool Commute = false;
1795 default: return false;
1803 case ARM::t2EORrr: {
1804 Commute = UseMI->getOperand(2).getReg() != Reg;
1811 NewUseOpc = ARM::SUBri;
1817 if (!ARM_AM::isSOImmTwoPartVal(ImmVal))
1819 SOImmValV1 = (uint32_t)ARM_AM::getSOImmTwoPartFirst(ImmVal);
1820 SOImmValV2 = (uint32_t)ARM_AM::getSOImmTwoPartSecond(ImmVal);
1823 case ARM::ADDrr: NewUseOpc = ARM::ADDri; break;
1824 case ARM::ORRrr: NewUseOpc = ARM::ORRri; break;
1825 case ARM::EORrr: NewUseOpc = ARM::EORri; break;
1829 case ARM::t2SUBrr: {
1833 NewUseOpc = ARM::t2SUBri;
1838 case ARM::t2EORrr: {
1839 if (!ARM_AM::isT2SOImmTwoPartVal(ImmVal))
1841 SOImmValV1 = (uint32_t)ARM_AM::getT2SOImmTwoPartFirst(ImmVal);
1842 SOImmValV2 = (uint32_t)ARM_AM::getT2SOImmTwoPartSecond(ImmVal);
1845 case ARM::t2ADDrr: NewUseOpc = ARM::t2ADDri; break;
1846 case ARM::t2ORRrr: NewUseOpc = ARM::t2ORRri; break;
1847 case ARM::t2EORrr: NewUseOpc = ARM::t2EORri; break;
1855 unsigned OpIdx = Commute ? 2 : 1;
1856 unsigned Reg1 = UseMI->getOperand(OpIdx).getReg();
1857 bool isKill = UseMI->getOperand(OpIdx).isKill();
1858 unsigned NewReg = MRI->createVirtualRegister(MRI->getRegClass(Reg));
1859 AddDefaultCC(AddDefaultPred(BuildMI(*UseMI->getParent(),
1860 *UseMI, UseMI->getDebugLoc(),
1861 get(NewUseOpc), NewReg)
1862 .addReg(Reg1, getKillRegState(isKill))
1863 .addImm(SOImmValV1)));
1864 UseMI->setDesc(get(NewUseOpc));
1865 UseMI->getOperand(1).setReg(NewReg);
1866 UseMI->getOperand(1).setIsKill();
1867 UseMI->getOperand(2).ChangeToImmediate(SOImmValV2);
1868 DefMI->eraseFromParent();
1873 ARMBaseInstrInfo::getNumMicroOps(const InstrItineraryData *ItinData,
1874 const MachineInstr *MI) const {
1875 if (!ItinData || ItinData->isEmpty())
1878 const MCInstrDesc &Desc = MI->getDesc();
1879 unsigned Class = Desc.getSchedClass();
1880 unsigned UOps = ItinData->Itineraries[Class].NumMicroOps;
1884 unsigned Opc = MI->getOpcode();
1887 llvm_unreachable("Unexpected multi-uops instruction!");
1893 // The number of uOps for load / store multiple are determined by the number
1896 // On Cortex-A8, each pair of register loads / stores can be scheduled on the
1897 // same cycle. The scheduling for the first load / store must be done
1898 // separately by assuming the the address is not 64-bit aligned.
1900 // On Cortex-A9, the formula is simply (#reg / 2) + (#reg % 2). If the address
1901 // is not 64-bit aligned, then AGU would take an extra cycle. For VFP / NEON
1902 // load / store multiple, the formula is (#reg / 2) + (#reg % 2) + 1.
1904 case ARM::VLDMDIA_UPD:
1905 case ARM::VLDMDDB_UPD:
1907 case ARM::VLDMSIA_UPD:
1908 case ARM::VLDMSDB_UPD:
1910 case ARM::VSTMDIA_UPD:
1911 case ARM::VSTMDDB_UPD:
1913 case ARM::VSTMSIA_UPD:
1914 case ARM::VSTMSDB_UPD: {
1915 unsigned NumRegs = MI->getNumOperands() - Desc.getNumOperands();
1916 return (NumRegs / 2) + (NumRegs % 2) + 1;
1919 case ARM::LDMIA_RET:
1924 case ARM::LDMIA_UPD:
1925 case ARM::LDMDA_UPD:
1926 case ARM::LDMDB_UPD:
1927 case ARM::LDMIB_UPD:
1932 case ARM::STMIA_UPD:
1933 case ARM::STMDA_UPD:
1934 case ARM::STMDB_UPD:
1935 case ARM::STMIB_UPD:
1937 case ARM::tLDMIA_UPD:
1938 case ARM::tSTMIA_UPD:
1942 case ARM::t2LDMIA_RET:
1945 case ARM::t2LDMIA_UPD:
1946 case ARM::t2LDMDB_UPD:
1949 case ARM::t2STMIA_UPD:
1950 case ARM::t2STMDB_UPD: {
1951 unsigned NumRegs = MI->getNumOperands() - Desc.getNumOperands() + 1;
1952 if (Subtarget.isCortexA8()) {
1955 // 4 registers would be issued: 2, 2.
1956 // 5 registers would be issued: 2, 2, 1.
1957 UOps = (NumRegs / 2);
1961 } else if (Subtarget.isCortexA9()) {
1962 UOps = (NumRegs / 2);
1963 // If there are odd number of registers or if it's not 64-bit aligned,
1964 // then it takes an extra AGU (Address Generation Unit) cycle.
1965 if ((NumRegs % 2) ||
1966 !MI->hasOneMemOperand() ||
1967 (*MI->memoperands_begin())->getAlignment() < 8)
1971 // Assume the worst.
1979 ARMBaseInstrInfo::getVLDMDefCycle(const InstrItineraryData *ItinData,
1980 const MCInstrDesc &DefMCID,
1982 unsigned DefIdx, unsigned DefAlign) const {
1983 int RegNo = (int)(DefIdx+1) - DefMCID.getNumOperands() + 1;
1985 // Def is the address writeback.
1986 return ItinData->getOperandCycle(DefClass, DefIdx);
1989 if (Subtarget.isCortexA8()) {
1990 // (regno / 2) + (regno % 2) + 1
1991 DefCycle = RegNo / 2 + 1;
1994 } else if (Subtarget.isCortexA9()) {
1996 bool isSLoad = false;
1998 switch (DefMCID.getOpcode()) {
2001 case ARM::VLDMSIA_UPD:
2002 case ARM::VLDMSDB_UPD:
2007 // If there are odd number of 'S' registers or if it's not 64-bit aligned,
2008 // then it takes an extra cycle.
2009 if ((isSLoad && (RegNo % 2)) || DefAlign < 8)
2012 // Assume the worst.
2013 DefCycle = RegNo + 2;
2020 ARMBaseInstrInfo::getLDMDefCycle(const InstrItineraryData *ItinData,
2021 const MCInstrDesc &DefMCID,
2023 unsigned DefIdx, unsigned DefAlign) const {
2024 int RegNo = (int)(DefIdx+1) - DefMCID.getNumOperands() + 1;
2026 // Def is the address writeback.
2027 return ItinData->getOperandCycle(DefClass, DefIdx);
2030 if (Subtarget.isCortexA8()) {
2031 // 4 registers would be issued: 1, 2, 1.
2032 // 5 registers would be issued: 1, 2, 2.
2033 DefCycle = RegNo / 2;
2036 // Result latency is issue cycle + 2: E2.
2038 } else if (Subtarget.isCortexA9()) {
2039 DefCycle = (RegNo / 2);
2040 // If there are odd number of registers or if it's not 64-bit aligned,
2041 // then it takes an extra AGU (Address Generation Unit) cycle.
2042 if ((RegNo % 2) || DefAlign < 8)
2044 // Result latency is AGU cycles + 2.
2047 // Assume the worst.
2048 DefCycle = RegNo + 2;
2055 ARMBaseInstrInfo::getVSTMUseCycle(const InstrItineraryData *ItinData,
2056 const MCInstrDesc &UseMCID,
2058 unsigned UseIdx, unsigned UseAlign) const {
2059 int RegNo = (int)(UseIdx+1) - UseMCID.getNumOperands() + 1;
2061 return ItinData->getOperandCycle(UseClass, UseIdx);
2064 if (Subtarget.isCortexA8()) {
2065 // (regno / 2) + (regno % 2) + 1
2066 UseCycle = RegNo / 2 + 1;
2069 } else if (Subtarget.isCortexA9()) {
2071 bool isSStore = false;
2073 switch (UseMCID.getOpcode()) {
2076 case ARM::VSTMSIA_UPD:
2077 case ARM::VSTMSDB_UPD:
2082 // If there are odd number of 'S' registers or if it's not 64-bit aligned,
2083 // then it takes an extra cycle.
2084 if ((isSStore && (RegNo % 2)) || UseAlign < 8)
2087 // Assume the worst.
2088 UseCycle = RegNo + 2;
2095 ARMBaseInstrInfo::getSTMUseCycle(const InstrItineraryData *ItinData,
2096 const MCInstrDesc &UseMCID,
2098 unsigned UseIdx, unsigned UseAlign) const {
2099 int RegNo = (int)(UseIdx+1) - UseMCID.getNumOperands() + 1;
2101 return ItinData->getOperandCycle(UseClass, UseIdx);
2104 if (Subtarget.isCortexA8()) {
2105 UseCycle = RegNo / 2;
2110 } else if (Subtarget.isCortexA9()) {
2111 UseCycle = (RegNo / 2);
2112 // If there are odd number of registers or if it's not 64-bit aligned,
2113 // then it takes an extra AGU (Address Generation Unit) cycle.
2114 if ((RegNo % 2) || UseAlign < 8)
2117 // Assume the worst.
2124 ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
2125 const MCInstrDesc &DefMCID,
2126 unsigned DefIdx, unsigned DefAlign,
2127 const MCInstrDesc &UseMCID,
2128 unsigned UseIdx, unsigned UseAlign) const {
2129 unsigned DefClass = DefMCID.getSchedClass();
2130 unsigned UseClass = UseMCID.getSchedClass();
2132 if (DefIdx < DefMCID.getNumDefs() && UseIdx < UseMCID.getNumOperands())
2133 return ItinData->getOperandLatency(DefClass, DefIdx, UseClass, UseIdx);
2135 // This may be a def / use of a variable_ops instruction, the operand
2136 // latency might be determinable dynamically. Let the target try to
2139 bool LdmBypass = false;
2140 switch (DefMCID.getOpcode()) {
2142 DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
2146 case ARM::VLDMDIA_UPD:
2147 case ARM::VLDMDDB_UPD:
2149 case ARM::VLDMSIA_UPD:
2150 case ARM::VLDMSDB_UPD:
2151 DefCycle = getVLDMDefCycle(ItinData, DefMCID, DefClass, DefIdx, DefAlign);
2154 case ARM::LDMIA_RET:
2159 case ARM::LDMIA_UPD:
2160 case ARM::LDMDA_UPD:
2161 case ARM::LDMDB_UPD:
2162 case ARM::LDMIB_UPD:
2164 case ARM::tLDMIA_UPD:
2166 case ARM::t2LDMIA_RET:
2169 case ARM::t2LDMIA_UPD:
2170 case ARM::t2LDMDB_UPD:
2172 DefCycle = getLDMDefCycle(ItinData, DefMCID, DefClass, DefIdx, DefAlign);
2177 // We can't seem to determine the result latency of the def, assume it's 2.
2181 switch (UseMCID.getOpcode()) {
2183 UseCycle = ItinData->getOperandCycle(UseClass, UseIdx);
2187 case ARM::VSTMDIA_UPD:
2188 case ARM::VSTMDDB_UPD:
2190 case ARM::VSTMSIA_UPD:
2191 case ARM::VSTMSDB_UPD:
2192 UseCycle = getVSTMUseCycle(ItinData, UseMCID, UseClass, UseIdx, UseAlign);
2199 case ARM::STMIA_UPD:
2200 case ARM::STMDA_UPD:
2201 case ARM::STMDB_UPD:
2202 case ARM::STMIB_UPD:
2203 case ARM::tSTMIA_UPD:
2208 case ARM::t2STMIA_UPD:
2209 case ARM::t2STMDB_UPD:
2210 UseCycle = getSTMUseCycle(ItinData, UseMCID, UseClass, UseIdx, UseAlign);
2215 // Assume it's read in the first stage.
2218 UseCycle = DefCycle - UseCycle + 1;
2221 // It's a variable_ops instruction so we can't use DefIdx here. Just use
2222 // first def operand.
2223 if (ItinData->hasPipelineForwarding(DefClass, DefMCID.getNumOperands()-1,
2226 } else if (ItinData->hasPipelineForwarding(DefClass, DefIdx,
2227 UseClass, UseIdx)) {
2236 ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
2237 const MachineInstr *DefMI, unsigned DefIdx,
2238 const MachineInstr *UseMI, unsigned UseIdx) const {
2239 if (DefMI->isCopyLike() || DefMI->isInsertSubreg() ||
2240 DefMI->isRegSequence() || DefMI->isImplicitDef())
2243 const MCInstrDesc &DefMCID = DefMI->getDesc();
2244 if (!ItinData || ItinData->isEmpty())
2245 return DefMCID.mayLoad() ? 3 : 1;
2247 const MCInstrDesc &UseMCID = UseMI->getDesc();
2248 const MachineOperand &DefMO = DefMI->getOperand(DefIdx);
2249 if (DefMO.getReg() == ARM::CPSR) {
2250 if (DefMI->getOpcode() == ARM::FMSTAT) {
2251 // fpscr -> cpsr stalls over 20 cycles on A8 (and earlier?)
2252 return Subtarget.isCortexA9() ? 1 : 20;
2255 // CPSR set and branch can be paired in the same cycle.
2256 if (UseMCID.isBranch())
2260 unsigned DefAlign = DefMI->hasOneMemOperand()
2261 ? (*DefMI->memoperands_begin())->getAlignment() : 0;
2262 unsigned UseAlign = UseMI->hasOneMemOperand()
2263 ? (*UseMI->memoperands_begin())->getAlignment() : 0;
2264 int Latency = getOperandLatency(ItinData, DefMCID, DefIdx, DefAlign,
2265 UseMCID, UseIdx, UseAlign);
2268 (Subtarget.isCortexA8() || Subtarget.isCortexA9())) {
2269 // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2]
2270 // variants are one cycle cheaper.
2271 switch (DefMCID.getOpcode()) {
2275 unsigned ShOpVal = DefMI->getOperand(3).getImm();
2276 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2278 (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
2285 case ARM::t2LDRSHs: {
2286 // Thumb2 mode: lsl only.
2287 unsigned ShAmt = DefMI->getOperand(3).getImm();
2288 if (ShAmt == 0 || ShAmt == 2)
2295 if (DefAlign < 8 && Subtarget.isCortexA9())
2296 switch (DefMCID.getOpcode()) {
2302 case ARM::VLD1q8_UPD:
2303 case ARM::VLD1q16_UPD:
2304 case ARM::VLD1q32_UPD:
2305 case ARM::VLD1q64_UPD:
2312 case ARM::VLD2d8_UPD:
2313 case ARM::VLD2d16_UPD:
2314 case ARM::VLD2d32_UPD:
2315 case ARM::VLD2q8_UPD:
2316 case ARM::VLD2q16_UPD:
2317 case ARM::VLD2q32_UPD:
2322 case ARM::VLD3d8_UPD:
2323 case ARM::VLD3d16_UPD:
2324 case ARM::VLD3d32_UPD:
2325 case ARM::VLD1d64T_UPD:
2326 case ARM::VLD3q8_UPD:
2327 case ARM::VLD3q16_UPD:
2328 case ARM::VLD3q32_UPD:
2333 case ARM::VLD4d8_UPD:
2334 case ARM::VLD4d16_UPD:
2335 case ARM::VLD4d32_UPD:
2336 case ARM::VLD1d64Q_UPD:
2337 case ARM::VLD4q8_UPD:
2338 case ARM::VLD4q16_UPD:
2339 case ARM::VLD4q32_UPD:
2340 case ARM::VLD1DUPq8:
2341 case ARM::VLD1DUPq16:
2342 case ARM::VLD1DUPq32:
2343 case ARM::VLD1DUPq8_UPD:
2344 case ARM::VLD1DUPq16_UPD:
2345 case ARM::VLD1DUPq32_UPD:
2346 case ARM::VLD2DUPd8:
2347 case ARM::VLD2DUPd16:
2348 case ARM::VLD2DUPd32:
2349 case ARM::VLD2DUPd8_UPD:
2350 case ARM::VLD2DUPd16_UPD:
2351 case ARM::VLD2DUPd32_UPD:
2352 case ARM::VLD4DUPd8:
2353 case ARM::VLD4DUPd16:
2354 case ARM::VLD4DUPd32:
2355 case ARM::VLD4DUPd8_UPD:
2356 case ARM::VLD4DUPd16_UPD:
2357 case ARM::VLD4DUPd32_UPD:
2359 case ARM::VLD1LNd16:
2360 case ARM::VLD1LNd32:
2361 case ARM::VLD1LNd8_UPD:
2362 case ARM::VLD1LNd16_UPD:
2363 case ARM::VLD1LNd32_UPD:
2365 case ARM::VLD2LNd16:
2366 case ARM::VLD2LNd32:
2367 case ARM::VLD2LNq16:
2368 case ARM::VLD2LNq32:
2369 case ARM::VLD2LNd8_UPD:
2370 case ARM::VLD2LNd16_UPD:
2371 case ARM::VLD2LNd32_UPD:
2372 case ARM::VLD2LNq16_UPD:
2373 case ARM::VLD2LNq32_UPD:
2375 case ARM::VLD4LNd16:
2376 case ARM::VLD4LNd32:
2377 case ARM::VLD4LNq16:
2378 case ARM::VLD4LNq32:
2379 case ARM::VLD4LNd8_UPD:
2380 case ARM::VLD4LNd16_UPD:
2381 case ARM::VLD4LNd32_UPD:
2382 case ARM::VLD4LNq16_UPD:
2383 case ARM::VLD4LNq32_UPD:
2384 // If the address is not 64-bit aligned, the latencies of these
2385 // instructions increases by one.
2394 ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
2395 SDNode *DefNode, unsigned DefIdx,
2396 SDNode *UseNode, unsigned UseIdx) const {
2397 if (!DefNode->isMachineOpcode())
2400 const MCInstrDesc &DefMCID = get(DefNode->getMachineOpcode());
2402 if (isZeroCost(DefMCID.Opcode))
2405 if (!ItinData || ItinData->isEmpty())
2406 return DefMCID.mayLoad() ? 3 : 1;
2408 if (!UseNode->isMachineOpcode()) {
2409 int Latency = ItinData->getOperandCycle(DefMCID.getSchedClass(), DefIdx);
2410 if (Subtarget.isCortexA9())
2411 return Latency <= 2 ? 1 : Latency - 1;
2413 return Latency <= 3 ? 1 : Latency - 2;
2416 const MCInstrDesc &UseMCID = get(UseNode->getMachineOpcode());
2417 const MachineSDNode *DefMN = dyn_cast<MachineSDNode>(DefNode);
2418 unsigned DefAlign = !DefMN->memoperands_empty()
2419 ? (*DefMN->memoperands_begin())->getAlignment() : 0;
2420 const MachineSDNode *UseMN = dyn_cast<MachineSDNode>(UseNode);
2421 unsigned UseAlign = !UseMN->memoperands_empty()
2422 ? (*UseMN->memoperands_begin())->getAlignment() : 0;
2423 int Latency = getOperandLatency(ItinData, DefMCID, DefIdx, DefAlign,
2424 UseMCID, UseIdx, UseAlign);
2427 (Subtarget.isCortexA8() || Subtarget.isCortexA9())) {
2428 // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2]
2429 // variants are one cycle cheaper.
2430 switch (DefMCID.getOpcode()) {
2435 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
2436 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2438 (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
2445 case ARM::t2LDRSHs: {
2446 // Thumb2 mode: lsl only.
2448 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
2449 if (ShAmt == 0 || ShAmt == 2)
2456 if (DefAlign < 8 && Subtarget.isCortexA9())
2457 switch (DefMCID.getOpcode()) {
2459 case ARM::VLD1q8Pseudo:
2460 case ARM::VLD1q16Pseudo:
2461 case ARM::VLD1q32Pseudo:
2462 case ARM::VLD1q64Pseudo:
2463 case ARM::VLD1q8Pseudo_UPD:
2464 case ARM::VLD1q16Pseudo_UPD:
2465 case ARM::VLD1q32Pseudo_UPD:
2466 case ARM::VLD1q64Pseudo_UPD:
2467 case ARM::VLD2d8Pseudo:
2468 case ARM::VLD2d16Pseudo:
2469 case ARM::VLD2d32Pseudo:
2470 case ARM::VLD2q8Pseudo:
2471 case ARM::VLD2q16Pseudo:
2472 case ARM::VLD2q32Pseudo:
2473 case ARM::VLD2d8Pseudo_UPD:
2474 case ARM::VLD2d16Pseudo_UPD:
2475 case ARM::VLD2d32Pseudo_UPD:
2476 case ARM::VLD2q8Pseudo_UPD:
2477 case ARM::VLD2q16Pseudo_UPD:
2478 case ARM::VLD2q32Pseudo_UPD:
2479 case ARM::VLD3d8Pseudo:
2480 case ARM::VLD3d16Pseudo:
2481 case ARM::VLD3d32Pseudo:
2482 case ARM::VLD1d64TPseudo:
2483 case ARM::VLD3d8Pseudo_UPD:
2484 case ARM::VLD3d16Pseudo_UPD:
2485 case ARM::VLD3d32Pseudo_UPD:
2486 case ARM::VLD1d64TPseudo_UPD:
2487 case ARM::VLD3q8Pseudo_UPD:
2488 case ARM::VLD3q16Pseudo_UPD:
2489 case ARM::VLD3q32Pseudo_UPD:
2490 case ARM::VLD3q8oddPseudo:
2491 case ARM::VLD3q16oddPseudo:
2492 case ARM::VLD3q32oddPseudo:
2493 case ARM::VLD3q8oddPseudo_UPD:
2494 case ARM::VLD3q16oddPseudo_UPD:
2495 case ARM::VLD3q32oddPseudo_UPD:
2496 case ARM::VLD4d8Pseudo:
2497 case ARM::VLD4d16Pseudo:
2498 case ARM::VLD4d32Pseudo:
2499 case ARM::VLD1d64QPseudo:
2500 case ARM::VLD4d8Pseudo_UPD:
2501 case ARM::VLD4d16Pseudo_UPD:
2502 case ARM::VLD4d32Pseudo_UPD:
2503 case ARM::VLD1d64QPseudo_UPD:
2504 case ARM::VLD4q8Pseudo_UPD:
2505 case ARM::VLD4q16Pseudo_UPD:
2506 case ARM::VLD4q32Pseudo_UPD:
2507 case ARM::VLD4q8oddPseudo:
2508 case ARM::VLD4q16oddPseudo:
2509 case ARM::VLD4q32oddPseudo:
2510 case ARM::VLD4q8oddPseudo_UPD:
2511 case ARM::VLD4q16oddPseudo_UPD:
2512 case ARM::VLD4q32oddPseudo_UPD:
2513 case ARM::VLD1DUPq8Pseudo:
2514 case ARM::VLD1DUPq16Pseudo:
2515 case ARM::VLD1DUPq32Pseudo:
2516 case ARM::VLD1DUPq8Pseudo_UPD:
2517 case ARM::VLD1DUPq16Pseudo_UPD:
2518 case ARM::VLD1DUPq32Pseudo_UPD:
2519 case ARM::VLD2DUPd8Pseudo:
2520 case ARM::VLD2DUPd16Pseudo:
2521 case ARM::VLD2DUPd32Pseudo:
2522 case ARM::VLD2DUPd8Pseudo_UPD:
2523 case ARM::VLD2DUPd16Pseudo_UPD:
2524 case ARM::VLD2DUPd32Pseudo_UPD:
2525 case ARM::VLD4DUPd8Pseudo:
2526 case ARM::VLD4DUPd16Pseudo:
2527 case ARM::VLD4DUPd32Pseudo:
2528 case ARM::VLD4DUPd8Pseudo_UPD:
2529 case ARM::VLD4DUPd16Pseudo_UPD:
2530 case ARM::VLD4DUPd32Pseudo_UPD:
2531 case ARM::VLD1LNq8Pseudo:
2532 case ARM::VLD1LNq16Pseudo:
2533 case ARM::VLD1LNq32Pseudo:
2534 case ARM::VLD1LNq8Pseudo_UPD:
2535 case ARM::VLD1LNq16Pseudo_UPD:
2536 case ARM::VLD1LNq32Pseudo_UPD:
2537 case ARM::VLD2LNd8Pseudo:
2538 case ARM::VLD2LNd16Pseudo:
2539 case ARM::VLD2LNd32Pseudo:
2540 case ARM::VLD2LNq16Pseudo:
2541 case ARM::VLD2LNq32Pseudo:
2542 case ARM::VLD2LNd8Pseudo_UPD:
2543 case ARM::VLD2LNd16Pseudo_UPD:
2544 case ARM::VLD2LNd32Pseudo_UPD:
2545 case ARM::VLD2LNq16Pseudo_UPD:
2546 case ARM::VLD2LNq32Pseudo_UPD:
2547 case ARM::VLD4LNd8Pseudo:
2548 case ARM::VLD4LNd16Pseudo:
2549 case ARM::VLD4LNd32Pseudo:
2550 case ARM::VLD4LNq16Pseudo:
2551 case ARM::VLD4LNq32Pseudo:
2552 case ARM::VLD4LNd8Pseudo_UPD:
2553 case ARM::VLD4LNd16Pseudo_UPD:
2554 case ARM::VLD4LNd32Pseudo_UPD:
2555 case ARM::VLD4LNq16Pseudo_UPD:
2556 case ARM::VLD4LNq32Pseudo_UPD:
2557 // If the address is not 64-bit aligned, the latencies of these
2558 // instructions increases by one.
2566 int ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
2567 const MachineInstr *MI,
2568 unsigned *PredCost) const {
2569 if (MI->isCopyLike() || MI->isInsertSubreg() ||
2570 MI->isRegSequence() || MI->isImplicitDef())
2573 if (!ItinData || ItinData->isEmpty())
2576 const MCInstrDesc &MCID = MI->getDesc();
2577 unsigned Class = MCID.getSchedClass();
2578 unsigned UOps = ItinData->Itineraries[Class].NumMicroOps;
2579 if (PredCost && MCID.hasImplicitDefOfPhysReg(ARM::CPSR))
2580 // When predicated, CPSR is an additional source operand for CPSR updating
2581 // instructions, this apparently increases their latencies.
2584 return ItinData->getStageLatency(Class);
2585 return getNumMicroOps(ItinData, MI);
2588 int ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
2589 SDNode *Node) const {
2590 if (!Node->isMachineOpcode())
2593 if (!ItinData || ItinData->isEmpty())
2596 unsigned Opcode = Node->getMachineOpcode();
2599 return ItinData->getStageLatency(get(Opcode).getSchedClass());
2606 bool ARMBaseInstrInfo::
2607 hasHighOperandLatency(const InstrItineraryData *ItinData,
2608 const MachineRegisterInfo *MRI,
2609 const MachineInstr *DefMI, unsigned DefIdx,
2610 const MachineInstr *UseMI, unsigned UseIdx) const {
2611 unsigned DDomain = DefMI->getDesc().TSFlags & ARMII::DomainMask;
2612 unsigned UDomain = UseMI->getDesc().TSFlags & ARMII::DomainMask;
2613 if (Subtarget.isCortexA8() &&
2614 (DDomain == ARMII::DomainVFP || UDomain == ARMII::DomainVFP))
2615 // CortexA8 VFP instructions are not pipelined.
2618 // Hoist VFP / NEON instructions with 4 or higher latency.
2619 int Latency = getOperandLatency(ItinData, DefMI, DefIdx, UseMI, UseIdx);
2622 return DDomain == ARMII::DomainVFP || DDomain == ARMII::DomainNEON ||
2623 UDomain == ARMII::DomainVFP || UDomain == ARMII::DomainNEON;
2626 bool ARMBaseInstrInfo::
2627 hasLowDefLatency(const InstrItineraryData *ItinData,
2628 const MachineInstr *DefMI, unsigned DefIdx) const {
2629 if (!ItinData || ItinData->isEmpty())
2632 unsigned DDomain = DefMI->getDesc().TSFlags & ARMII::DomainMask;
2633 if (DDomain == ARMII::DomainGeneral) {
2634 unsigned DefClass = DefMI->getDesc().getSchedClass();
2635 int DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
2636 return (DefCycle != -1 && DefCycle <= 2);
2642 ARMBaseInstrInfo::isFpMLxInstruction(unsigned Opcode, unsigned &MulOpc,
2643 unsigned &AddSubOpc,
2644 bool &NegAcc, bool &HasLane) const {
2645 DenseMap<unsigned, unsigned>::const_iterator I = MLxEntryMap.find(Opcode);
2646 if (I == MLxEntryMap.end())
2649 const ARM_MLxEntry &Entry = ARM_MLxTable[I->second];
2650 MulOpc = Entry.MulOpc;
2651 AddSubOpc = Entry.AddSubOpc;
2652 NegAcc = Entry.NegAcc;
2653 HasLane = Entry.HasLane;