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 "ARMAddressingModes.h"
17 #include "ARMConstantPoolValue.h"
18 #include "ARMHazardRecognizer.h"
19 #include "ARMMachineFunctionInfo.h"
20 #include "ARMRegisterInfo.h"
21 #include "ARMGenInstrInfo.inc"
22 #include "llvm/Constants.h"
23 #include "llvm/Function.h"
24 #include "llvm/GlobalValue.h"
25 #include "llvm/CodeGen/LiveVariables.h"
26 #include "llvm/CodeGen/MachineConstantPool.h"
27 #include "llvm/CodeGen/MachineFrameInfo.h"
28 #include "llvm/CodeGen/MachineInstrBuilder.h"
29 #include "llvm/CodeGen/MachineJumpTableInfo.h"
30 #include "llvm/CodeGen/MachineMemOperand.h"
31 #include "llvm/CodeGen/MachineRegisterInfo.h"
32 #include "llvm/CodeGen/PseudoSourceValue.h"
33 #include "llvm/MC/MCAsmInfo.h"
34 #include "llvm/Support/CommandLine.h"
35 #include "llvm/Support/Debug.h"
36 #include "llvm/Support/ErrorHandling.h"
37 #include "llvm/ADT/STLExtras.h"
41 EnableARM3Addr("enable-arm-3-addr-conv", cl::Hidden,
42 cl::desc("Enable ARM 2-addr to 3-addr conv"));
44 // Other targets already have a hazard recognizer enabled by default, so this
45 // flag currently only affects ARM. It will be generalized when it becomes a
47 static cl::opt<bool> EnableHazardRecognizer(
48 "enable-sched-hazard", cl::Hidden,
49 cl::desc("Enable hazard detection during preRA scheduling"),
52 /// ARM_MLxEntry - Record information about MLA / MLS instructions.
54 unsigned MLxOpc; // MLA / MLS opcode
55 unsigned MulOpc; // Expanded multiplication opcode
56 unsigned AddSubOpc; // Expanded add / sub opcode
57 bool NegAcc; // True if the acc is negated before the add / sub.
58 bool HasLane; // True if instruction has an extra "lane" operand.
61 static const ARM_MLxEntry ARM_MLxTable[] = {
62 // MLxOpc, MulOpc, AddSubOpc, NegAcc, HasLane
64 { ARM::VMLAS, ARM::VMULS, ARM::VADDS, false, false },
65 { ARM::VMLSS, ARM::VMULS, ARM::VSUBS, false, false },
66 { ARM::VMLAD, ARM::VMULD, ARM::VADDD, false, false },
67 { ARM::VMLSD, ARM::VMULD, ARM::VSUBD, false, false },
68 { ARM::VNMLAS, ARM::VNMULS, ARM::VSUBS, true, false },
69 { ARM::VNMLSS, ARM::VMULS, ARM::VSUBS, true, false },
70 { ARM::VNMLAD, ARM::VNMULD, ARM::VSUBD, true, false },
71 { ARM::VNMLSD, ARM::VMULD, ARM::VSUBD, true, false },
74 { ARM::VMLAfd, ARM::VMULfd, ARM::VADDfd, false, false },
75 { ARM::VMLSfd, ARM::VMULfd, ARM::VSUBfd, false, false },
76 { ARM::VMLAfq, ARM::VMULfq, ARM::VADDfq, false, false },
77 { ARM::VMLSfq, ARM::VMULfq, ARM::VSUBfq, false, false },
78 { ARM::VMLAslfd, ARM::VMULslfd, ARM::VADDfd, false, true },
79 { ARM::VMLSslfd, ARM::VMULslfd, ARM::VSUBfd, false, true },
80 { ARM::VMLAslfq, ARM::VMULslfq, ARM::VADDfq, false, true },
81 { ARM::VMLSslfq, ARM::VMULslfq, ARM::VSUBfq, false, true },
84 ARMBaseInstrInfo::ARMBaseInstrInfo(const ARMSubtarget& STI)
85 : TargetInstrInfoImpl(ARMInsts, array_lengthof(ARMInsts)),
87 for (unsigned i = 0, e = array_lengthof(ARM_MLxTable); i != e; ++i) {
88 if (!MLxEntryMap.insert(std::make_pair(ARM_MLxTable[i].MLxOpc, i)).second)
89 assert(false && "Duplicated entries?");
90 MLxHazardOpcodes.insert(ARM_MLxTable[i].AddSubOpc);
91 MLxHazardOpcodes.insert(ARM_MLxTable[i].MulOpc);
95 // Use a ScoreboardHazardRecognizer for prepass ARM scheduling. TargetInstrImpl
96 // currently defaults to no prepass hazard recognizer.
97 ScheduleHazardRecognizer *ARMBaseInstrInfo::
98 CreateTargetHazardRecognizer(const TargetMachine *TM,
99 const ScheduleDAG *DAG) const {
100 if (EnableHazardRecognizer) {
101 const InstrItineraryData *II = TM->getInstrItineraryData();
102 return new ScoreboardHazardRecognizer(II, DAG, "pre-RA-sched");
104 return TargetInstrInfoImpl::CreateTargetHazardRecognizer(TM, DAG);
107 ScheduleHazardRecognizer *ARMBaseInstrInfo::
108 CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II,
109 const ScheduleDAG *DAG) const {
110 if (Subtarget.isThumb2() || Subtarget.hasVFP2())
111 return (ScheduleHazardRecognizer *)
112 new ARMHazardRecognizer(II, *this, getRegisterInfo(), Subtarget, DAG);
113 return TargetInstrInfoImpl::CreateTargetPostRAHazardRecognizer(II, DAG);
117 ARMBaseInstrInfo::convertToThreeAddress(MachineFunction::iterator &MFI,
118 MachineBasicBlock::iterator &MBBI,
119 LiveVariables *LV) const {
120 // FIXME: Thumb2 support.
125 MachineInstr *MI = MBBI;
126 MachineFunction &MF = *MI->getParent()->getParent();
127 uint64_t TSFlags = MI->getDesc().TSFlags;
129 switch ((TSFlags & ARMII::IndexModeMask) >> ARMII::IndexModeShift) {
130 default: return NULL;
131 case ARMII::IndexModePre:
134 case ARMII::IndexModePost:
138 // Try splitting an indexed load/store to an un-indexed one plus an add/sub
140 unsigned MemOpc = getUnindexedOpcode(MI->getOpcode());
144 MachineInstr *UpdateMI = NULL;
145 MachineInstr *MemMI = NULL;
146 unsigned AddrMode = (TSFlags & ARMII::AddrModeMask);
147 const TargetInstrDesc &TID = MI->getDesc();
148 unsigned NumOps = TID.getNumOperands();
149 bool isLoad = !TID.mayStore();
150 const MachineOperand &WB = isLoad ? MI->getOperand(1) : MI->getOperand(0);
151 const MachineOperand &Base = MI->getOperand(2);
152 const MachineOperand &Offset = MI->getOperand(NumOps-3);
153 unsigned WBReg = WB.getReg();
154 unsigned BaseReg = Base.getReg();
155 unsigned OffReg = Offset.getReg();
156 unsigned OffImm = MI->getOperand(NumOps-2).getImm();
157 ARMCC::CondCodes Pred = (ARMCC::CondCodes)MI->getOperand(NumOps-1).getImm();
160 assert(false && "Unknown indexed op!");
162 case ARMII::AddrMode2: {
163 bool isSub = ARM_AM::getAM2Op(OffImm) == ARM_AM::sub;
164 unsigned Amt = ARM_AM::getAM2Offset(OffImm);
166 if (ARM_AM::getSOImmVal(Amt) == -1)
167 // Can't encode it in a so_imm operand. This transformation will
168 // add more than 1 instruction. Abandon!
170 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
171 get(isSub ? ARM::SUBri : ARM::ADDri), WBReg)
172 .addReg(BaseReg).addImm(Amt)
173 .addImm(Pred).addReg(0).addReg(0);
174 } else if (Amt != 0) {
175 ARM_AM::ShiftOpc ShOpc = ARM_AM::getAM2ShiftOpc(OffImm);
176 unsigned SOOpc = ARM_AM::getSORegOpc(ShOpc, Amt);
177 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
178 get(isSub ? ARM::SUBrs : ARM::ADDrs), WBReg)
179 .addReg(BaseReg).addReg(OffReg).addReg(0).addImm(SOOpc)
180 .addImm(Pred).addReg(0).addReg(0);
182 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
183 get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
184 .addReg(BaseReg).addReg(OffReg)
185 .addImm(Pred).addReg(0).addReg(0);
188 case ARMII::AddrMode3 : {
189 bool isSub = ARM_AM::getAM3Op(OffImm) == ARM_AM::sub;
190 unsigned Amt = ARM_AM::getAM3Offset(OffImm);
192 // Immediate is 8-bits. It's guaranteed to fit in a so_imm operand.
193 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
194 get(isSub ? ARM::SUBri : ARM::ADDri), WBReg)
195 .addReg(BaseReg).addImm(Amt)
196 .addImm(Pred).addReg(0).addReg(0);
198 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
199 get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
200 .addReg(BaseReg).addReg(OffReg)
201 .addImm(Pred).addReg(0).addReg(0);
206 std::vector<MachineInstr*> NewMIs;
209 MemMI = BuildMI(MF, MI->getDebugLoc(),
210 get(MemOpc), MI->getOperand(0).getReg())
211 .addReg(WBReg).addImm(0).addImm(Pred);
213 MemMI = BuildMI(MF, MI->getDebugLoc(),
214 get(MemOpc)).addReg(MI->getOperand(1).getReg())
215 .addReg(WBReg).addReg(0).addImm(0).addImm(Pred);
216 NewMIs.push_back(MemMI);
217 NewMIs.push_back(UpdateMI);
220 MemMI = BuildMI(MF, MI->getDebugLoc(),
221 get(MemOpc), MI->getOperand(0).getReg())
222 .addReg(BaseReg).addImm(0).addImm(Pred);
224 MemMI = BuildMI(MF, MI->getDebugLoc(),
225 get(MemOpc)).addReg(MI->getOperand(1).getReg())
226 .addReg(BaseReg).addReg(0).addImm(0).addImm(Pred);
228 UpdateMI->getOperand(0).setIsDead();
229 NewMIs.push_back(UpdateMI);
230 NewMIs.push_back(MemMI);
233 // Transfer LiveVariables states, kill / dead info.
235 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
236 MachineOperand &MO = MI->getOperand(i);
237 if (MO.isReg() && TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
238 unsigned Reg = MO.getReg();
240 LiveVariables::VarInfo &VI = LV->getVarInfo(Reg);
242 MachineInstr *NewMI = (Reg == WBReg) ? UpdateMI : MemMI;
244 LV->addVirtualRegisterDead(Reg, NewMI);
246 if (MO.isUse() && MO.isKill()) {
247 for (unsigned j = 0; j < 2; ++j) {
248 // Look at the two new MI's in reverse order.
249 MachineInstr *NewMI = NewMIs[j];
250 if (!NewMI->readsRegister(Reg))
252 LV->addVirtualRegisterKilled(Reg, NewMI);
253 if (VI.removeKill(MI))
254 VI.Kills.push_back(NewMI);
262 MFI->insert(MBBI, NewMIs[1]);
263 MFI->insert(MBBI, NewMIs[0]);
269 ARMBaseInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
270 MachineBasicBlock *&FBB,
271 SmallVectorImpl<MachineOperand> &Cond,
272 bool AllowModify) const {
273 // If the block has no terminators, it just falls into the block after it.
274 MachineBasicBlock::iterator I = MBB.end();
275 if (I == MBB.begin())
278 while (I->isDebugValue()) {
279 if (I == MBB.begin())
283 if (!isUnpredicatedTerminator(I))
286 // Get the last instruction in the block.
287 MachineInstr *LastInst = I;
289 // If there is only one terminator instruction, process it.
290 unsigned LastOpc = LastInst->getOpcode();
291 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
292 if (isUncondBranchOpcode(LastOpc)) {
293 TBB = LastInst->getOperand(0).getMBB();
296 if (isCondBranchOpcode(LastOpc)) {
297 // Block ends with fall-through condbranch.
298 TBB = LastInst->getOperand(0).getMBB();
299 Cond.push_back(LastInst->getOperand(1));
300 Cond.push_back(LastInst->getOperand(2));
303 return true; // Can't handle indirect branch.
306 // Get the instruction before it if it is a terminator.
307 MachineInstr *SecondLastInst = I;
308 unsigned SecondLastOpc = SecondLastInst->getOpcode();
310 // If AllowModify is true and the block ends with two or more unconditional
311 // branches, delete all but the first unconditional branch.
312 if (AllowModify && isUncondBranchOpcode(LastOpc)) {
313 while (isUncondBranchOpcode(SecondLastOpc)) {
314 LastInst->eraseFromParent();
315 LastInst = SecondLastInst;
316 LastOpc = LastInst->getOpcode();
317 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
318 // Return now the only terminator is an unconditional branch.
319 TBB = LastInst->getOperand(0).getMBB();
323 SecondLastOpc = SecondLastInst->getOpcode();
328 // If there are three terminators, we don't know what sort of block this is.
329 if (SecondLastInst && I != MBB.begin() && isUnpredicatedTerminator(--I))
332 // If the block ends with a B and a Bcc, handle it.
333 if (isCondBranchOpcode(SecondLastOpc) && isUncondBranchOpcode(LastOpc)) {
334 TBB = SecondLastInst->getOperand(0).getMBB();
335 Cond.push_back(SecondLastInst->getOperand(1));
336 Cond.push_back(SecondLastInst->getOperand(2));
337 FBB = LastInst->getOperand(0).getMBB();
341 // If the block ends with two unconditional branches, handle it. The second
342 // one is not executed, so remove it.
343 if (isUncondBranchOpcode(SecondLastOpc) && isUncondBranchOpcode(LastOpc)) {
344 TBB = SecondLastInst->getOperand(0).getMBB();
347 I->eraseFromParent();
351 // ...likewise if it ends with a branch table followed by an unconditional
352 // branch. The branch folder can create these, and we must get rid of them for
353 // correctness of Thumb constant islands.
354 if ((isJumpTableBranchOpcode(SecondLastOpc) ||
355 isIndirectBranchOpcode(SecondLastOpc)) &&
356 isUncondBranchOpcode(LastOpc)) {
359 I->eraseFromParent();
363 // Otherwise, can't handle this.
368 unsigned ARMBaseInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
369 MachineBasicBlock::iterator I = MBB.end();
370 if (I == MBB.begin()) return 0;
372 while (I->isDebugValue()) {
373 if (I == MBB.begin())
377 if (!isUncondBranchOpcode(I->getOpcode()) &&
378 !isCondBranchOpcode(I->getOpcode()))
381 // Remove the branch.
382 I->eraseFromParent();
386 if (I == MBB.begin()) return 1;
388 if (!isCondBranchOpcode(I->getOpcode()))
391 // Remove the branch.
392 I->eraseFromParent();
397 ARMBaseInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
398 MachineBasicBlock *FBB,
399 const SmallVectorImpl<MachineOperand> &Cond,
401 ARMFunctionInfo *AFI = MBB.getParent()->getInfo<ARMFunctionInfo>();
402 int BOpc = !AFI->isThumbFunction()
403 ? ARM::B : (AFI->isThumb2Function() ? ARM::t2B : ARM::tB);
404 int BccOpc = !AFI->isThumbFunction()
405 ? ARM::Bcc : (AFI->isThumb2Function() ? ARM::t2Bcc : ARM::tBcc);
407 // Shouldn't be a fall through.
408 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
409 assert((Cond.size() == 2 || Cond.size() == 0) &&
410 "ARM branch conditions have two components!");
413 if (Cond.empty()) // Unconditional branch?
414 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB);
416 BuildMI(&MBB, DL, get(BccOpc)).addMBB(TBB)
417 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg());
421 // Two-way conditional branch.
422 BuildMI(&MBB, DL, get(BccOpc)).addMBB(TBB)
423 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg());
424 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB);
428 bool ARMBaseInstrInfo::
429 ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
430 ARMCC::CondCodes CC = (ARMCC::CondCodes)(int)Cond[0].getImm();
431 Cond[0].setImm(ARMCC::getOppositeCondition(CC));
435 bool ARMBaseInstrInfo::
436 PredicateInstruction(MachineInstr *MI,
437 const SmallVectorImpl<MachineOperand> &Pred) const {
438 unsigned Opc = MI->getOpcode();
439 if (isUncondBranchOpcode(Opc)) {
440 MI->setDesc(get(getMatchingCondBranchOpcode(Opc)));
441 MI->addOperand(MachineOperand::CreateImm(Pred[0].getImm()));
442 MI->addOperand(MachineOperand::CreateReg(Pred[1].getReg(), false));
446 int PIdx = MI->findFirstPredOperandIdx();
448 MachineOperand &PMO = MI->getOperand(PIdx);
449 PMO.setImm(Pred[0].getImm());
450 MI->getOperand(PIdx+1).setReg(Pred[1].getReg());
456 bool ARMBaseInstrInfo::
457 SubsumesPredicate(const SmallVectorImpl<MachineOperand> &Pred1,
458 const SmallVectorImpl<MachineOperand> &Pred2) const {
459 if (Pred1.size() > 2 || Pred2.size() > 2)
462 ARMCC::CondCodes CC1 = (ARMCC::CondCodes)Pred1[0].getImm();
463 ARMCC::CondCodes CC2 = (ARMCC::CondCodes)Pred2[0].getImm();
473 return CC2 == ARMCC::HI;
475 return CC2 == ARMCC::LO || CC2 == ARMCC::EQ;
477 return CC2 == ARMCC::GT;
479 return CC2 == ARMCC::LT;
483 bool ARMBaseInstrInfo::DefinesPredicate(MachineInstr *MI,
484 std::vector<MachineOperand> &Pred) const {
485 // FIXME: This confuses implicit_def with optional CPSR def.
486 const TargetInstrDesc &TID = MI->getDesc();
487 if (!TID.getImplicitDefs() && !TID.hasOptionalDef())
491 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
492 const MachineOperand &MO = MI->getOperand(i);
493 if (MO.isReg() && MO.getReg() == ARM::CPSR) {
502 /// isPredicable - Return true if the specified instruction can be predicated.
503 /// By default, this returns true for every instruction with a
504 /// PredicateOperand.
505 bool ARMBaseInstrInfo::isPredicable(MachineInstr *MI) const {
506 const TargetInstrDesc &TID = MI->getDesc();
507 if (!TID.isPredicable())
510 if ((TID.TSFlags & ARMII::DomainMask) == ARMII::DomainNEON) {
511 ARMFunctionInfo *AFI =
512 MI->getParent()->getParent()->getInfo<ARMFunctionInfo>();
513 return AFI->isThumb2Function();
518 /// FIXME: Works around a gcc miscompilation with -fstrict-aliasing.
519 LLVM_ATTRIBUTE_NOINLINE
520 static unsigned getNumJTEntries(const std::vector<MachineJumpTableEntry> &JT,
522 static unsigned getNumJTEntries(const std::vector<MachineJumpTableEntry> &JT,
524 assert(JTI < JT.size());
525 return JT[JTI].MBBs.size();
528 /// GetInstSize - Return the size of the specified MachineInstr.
530 unsigned ARMBaseInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
531 const MachineBasicBlock &MBB = *MI->getParent();
532 const MachineFunction *MF = MBB.getParent();
533 const MCAsmInfo *MAI = MF->getTarget().getMCAsmInfo();
535 // Basic size info comes from the TSFlags field.
536 const TargetInstrDesc &TID = MI->getDesc();
537 uint64_t TSFlags = TID.TSFlags;
539 unsigned Opc = MI->getOpcode();
540 switch ((TSFlags & ARMII::SizeMask) >> ARMII::SizeShift) {
542 // If this machine instr is an inline asm, measure it.
543 if (MI->getOpcode() == ARM::INLINEASM)
544 return getInlineAsmLength(MI->getOperand(0).getSymbolName(), *MAI);
549 llvm_unreachable("Unknown or unset size field for instr!");
550 case TargetOpcode::IMPLICIT_DEF:
551 case TargetOpcode::KILL:
552 case TargetOpcode::PROLOG_LABEL:
553 case TargetOpcode::EH_LABEL:
554 case TargetOpcode::DBG_VALUE:
559 case ARMII::Size8Bytes: return 8; // ARM instruction x 2.
560 case ARMII::Size4Bytes: return 4; // ARM / Thumb2 instruction.
561 case ARMII::Size2Bytes: return 2; // Thumb1 instruction.
562 case ARMII::SizeSpecial: {
564 case ARM::MOVi16_pic_ga:
565 case ARM::MOVTi16_pic_ga:
566 case ARM::t2MOVi16_pic_ga:
567 case ARM::t2MOVTi16_pic_ga:
570 case ARM::t2MOVi32imm:
572 case ARM::CONSTPOOL_ENTRY:
573 // If this machine instr is a constant pool entry, its size is recorded as
575 return MI->getOperand(2).getImm();
576 case ARM::Int_eh_sjlj_longjmp:
578 case ARM::tInt_eh_sjlj_longjmp:
580 case ARM::Int_eh_sjlj_setjmp:
581 case ARM::Int_eh_sjlj_setjmp_nofp:
583 case ARM::tInt_eh_sjlj_setjmp:
584 case ARM::t2Int_eh_sjlj_setjmp:
585 case ARM::t2Int_eh_sjlj_setjmp_nofp:
593 case ARM::t2TBH_JT: {
594 // These are jumptable branches, i.e. a branch followed by an inlined
595 // jumptable. The size is 4 + 4 * number of entries. For TBB, each
596 // entry is one byte; TBH two byte each.
597 unsigned EntrySize = (Opc == ARM::t2TBB_JT)
598 ? 1 : ((Opc == ARM::t2TBH_JT) ? 2 : 4);
599 unsigned NumOps = TID.getNumOperands();
600 MachineOperand JTOP =
601 MI->getOperand(NumOps - (TID.isPredicable() ? 3 : 2));
602 unsigned JTI = JTOP.getIndex();
603 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
605 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
606 assert(JTI < JT.size());
607 // Thumb instructions are 2 byte aligned, but JT entries are 4 byte
608 // 4 aligned. The assembler / linker may add 2 byte padding just before
609 // the JT entries. The size does not include this padding; the
610 // constant islands pass does separate bookkeeping for it.
611 // FIXME: If we know the size of the function is less than (1 << 16) *2
612 // bytes, we can use 16-bit entries instead. Then there won't be an
614 unsigned InstSize = (Opc == ARM::tBR_JTr || Opc == ARM::t2BR_JT) ? 2 : 4;
615 unsigned NumEntries = getNumJTEntries(JT, JTI);
616 if (Opc == ARM::t2TBB_JT && (NumEntries & 1))
617 // Make sure the instruction that follows TBB is 2-byte aligned.
618 // FIXME: Constant island pass should insert an "ALIGN" instruction
621 return NumEntries * EntrySize + InstSize;
624 // Otherwise, pseudo-instruction sizes are zero.
629 return 0; // Not reached
632 void ARMBaseInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
633 MachineBasicBlock::iterator I, DebugLoc DL,
634 unsigned DestReg, unsigned SrcReg,
635 bool KillSrc) const {
636 bool GPRDest = ARM::GPRRegClass.contains(DestReg);
637 bool GPRSrc = ARM::GPRRegClass.contains(SrcReg);
639 if (GPRDest && GPRSrc) {
640 AddDefaultCC(AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::MOVr), DestReg)
641 .addReg(SrcReg, getKillRegState(KillSrc))));
645 bool SPRDest = ARM::SPRRegClass.contains(DestReg);
646 bool SPRSrc = ARM::SPRRegClass.contains(SrcReg);
649 if (SPRDest && SPRSrc)
651 else if (GPRDest && SPRSrc)
653 else if (SPRDest && GPRSrc)
655 else if (ARM::DPRRegClass.contains(DestReg, SrcReg))
657 else if (ARM::QPRRegClass.contains(DestReg, SrcReg))
659 else if (ARM::QQPRRegClass.contains(DestReg, SrcReg))
661 else if (ARM::QQQQPRRegClass.contains(DestReg, SrcReg))
664 llvm_unreachable("Impossible reg-to-reg copy");
666 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(Opc), DestReg);
667 MIB.addReg(SrcReg, getKillRegState(KillSrc));
668 if (Opc != ARM::VMOVQQ && Opc != ARM::VMOVQQQQ)
673 MachineInstrBuilder &AddDReg(MachineInstrBuilder &MIB,
674 unsigned Reg, unsigned SubIdx, unsigned State,
675 const TargetRegisterInfo *TRI) {
677 return MIB.addReg(Reg, State);
679 if (TargetRegisterInfo::isPhysicalRegister(Reg))
680 return MIB.addReg(TRI->getSubReg(Reg, SubIdx), State);
681 return MIB.addReg(Reg, State, SubIdx);
684 void ARMBaseInstrInfo::
685 storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
686 unsigned SrcReg, bool isKill, int FI,
687 const TargetRegisterClass *RC,
688 const TargetRegisterInfo *TRI) const {
690 if (I != MBB.end()) DL = I->getDebugLoc();
691 MachineFunction &MF = *MBB.getParent();
692 MachineFrameInfo &MFI = *MF.getFrameInfo();
693 unsigned Align = MFI.getObjectAlignment(FI);
695 MachineMemOperand *MMO =
696 MF.getMachineMemOperand(MachinePointerInfo(
697 PseudoSourceValue::getFixedStack(FI)),
698 MachineMemOperand::MOStore,
699 MFI.getObjectSize(FI),
702 // tGPR is used sometimes in ARM instructions that need to avoid using
703 // certain registers. Just treat it as GPR here. Likewise, rGPR.
704 if (RC == ARM::tGPRRegisterClass || RC == ARM::tcGPRRegisterClass
705 || RC == ARM::rGPRRegisterClass)
706 RC = ARM::GPRRegisterClass;
708 switch (RC->getID()) {
709 case ARM::GPRRegClassID:
710 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::STRi12))
711 .addReg(SrcReg, getKillRegState(isKill))
712 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
714 case ARM::SPRRegClassID:
715 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTRS))
716 .addReg(SrcReg, getKillRegState(isKill))
717 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
719 case ARM::DPRRegClassID:
720 case ARM::DPR_VFP2RegClassID:
721 case ARM::DPR_8RegClassID:
722 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTRD))
723 .addReg(SrcReg, getKillRegState(isKill))
724 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
726 case ARM::QPRRegClassID:
727 case ARM::QPR_VFP2RegClassID:
728 case ARM::QPR_8RegClassID:
729 if (Align >= 16 && getRegisterInfo().needsStackRealignment(MF)) {
730 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VST1q64Pseudo))
731 .addFrameIndex(FI).addImm(16)
732 .addReg(SrcReg, getKillRegState(isKill))
733 .addMemOperand(MMO));
735 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMQIA))
736 .addReg(SrcReg, getKillRegState(isKill))
738 .addMemOperand(MMO));
741 case ARM::QQPRRegClassID:
742 case ARM::QQPR_VFP2RegClassID:
743 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
744 // FIXME: It's possible to only store part of the QQ register if the
745 // spilled def has a sub-register index.
746 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VST1d64QPseudo))
747 .addFrameIndex(FI).addImm(16)
748 .addReg(SrcReg, getKillRegState(isKill))
749 .addMemOperand(MMO));
751 MachineInstrBuilder MIB =
752 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMDIA))
755 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
756 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
757 MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
758 AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI);
761 case ARM::QQQQPRRegClassID: {
762 MachineInstrBuilder MIB =
763 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMDIA))
766 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
767 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
768 MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
769 MIB = AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI);
770 MIB = AddDReg(MIB, SrcReg, ARM::dsub_4, 0, TRI);
771 MIB = AddDReg(MIB, SrcReg, ARM::dsub_5, 0, TRI);
772 MIB = AddDReg(MIB, SrcReg, ARM::dsub_6, 0, TRI);
773 AddDReg(MIB, SrcReg, ARM::dsub_7, 0, TRI);
777 llvm_unreachable("Unknown regclass!");
782 ARMBaseInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
783 int &FrameIndex) const {
784 switch (MI->getOpcode()) {
787 case ARM::t2STRs: // FIXME: don't use t2STRs to access frame.
788 if (MI->getOperand(1).isFI() &&
789 MI->getOperand(2).isReg() &&
790 MI->getOperand(3).isImm() &&
791 MI->getOperand(2).getReg() == 0 &&
792 MI->getOperand(3).getImm() == 0) {
793 FrameIndex = MI->getOperand(1).getIndex();
794 return MI->getOperand(0).getReg();
802 if (MI->getOperand(1).isFI() &&
803 MI->getOperand(2).isImm() &&
804 MI->getOperand(2).getImm() == 0) {
805 FrameIndex = MI->getOperand(1).getIndex();
806 return MI->getOperand(0).getReg();
809 case ARM::VST1q64Pseudo:
810 if (MI->getOperand(0).isFI() &&
811 MI->getOperand(2).getSubReg() == 0) {
812 FrameIndex = MI->getOperand(0).getIndex();
813 return MI->getOperand(2).getReg();
817 if (MI->getOperand(1).isFI() &&
818 MI->getOperand(0).getSubReg() == 0) {
819 FrameIndex = MI->getOperand(1).getIndex();
820 return MI->getOperand(0).getReg();
828 void ARMBaseInstrInfo::
829 loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
830 unsigned DestReg, int FI,
831 const TargetRegisterClass *RC,
832 const TargetRegisterInfo *TRI) const {
834 if (I != MBB.end()) DL = I->getDebugLoc();
835 MachineFunction &MF = *MBB.getParent();
836 MachineFrameInfo &MFI = *MF.getFrameInfo();
837 unsigned Align = MFI.getObjectAlignment(FI);
838 MachineMemOperand *MMO =
839 MF.getMachineMemOperand(
840 MachinePointerInfo(PseudoSourceValue::getFixedStack(FI)),
841 MachineMemOperand::MOLoad,
842 MFI.getObjectSize(FI),
845 // tGPR is used sometimes in ARM instructions that need to avoid using
846 // certain registers. Just treat it as GPR here.
847 if (RC == ARM::tGPRRegisterClass || RC == ARM::tcGPRRegisterClass
848 || RC == ARM::rGPRRegisterClass)
849 RC = ARM::GPRRegisterClass;
851 switch (RC->getID()) {
852 case ARM::GPRRegClassID:
853 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::LDRi12), DestReg)
854 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
856 case ARM::SPRRegClassID:
857 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDRS), DestReg)
858 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
860 case ARM::DPRRegClassID:
861 case ARM::DPR_VFP2RegClassID:
862 case ARM::DPR_8RegClassID:
863 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDRD), DestReg)
864 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
866 case ARM::QPRRegClassID:
867 case ARM::QPR_VFP2RegClassID:
868 case ARM::QPR_8RegClassID:
869 if (Align >= 16 && getRegisterInfo().needsStackRealignment(MF)) {
870 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLD1q64Pseudo), DestReg)
871 .addFrameIndex(FI).addImm(16)
872 .addMemOperand(MMO));
874 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMQIA), DestReg)
876 .addMemOperand(MMO));
879 case ARM::QQPRRegClassID:
880 case ARM::QQPR_VFP2RegClassID:
881 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
882 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLD1d64QPseudo), DestReg)
883 .addFrameIndex(FI).addImm(16)
884 .addMemOperand(MMO));
886 MachineInstrBuilder MIB =
887 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
890 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::Define, TRI);
891 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::Define, TRI);
892 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::Define, TRI);
893 AddDReg(MIB, DestReg, ARM::dsub_3, RegState::Define, TRI);
896 case ARM::QQQQPRRegClassID: {
897 MachineInstrBuilder MIB =
898 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
901 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::Define, TRI);
902 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::Define, TRI);
903 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::Define, TRI);
904 MIB = AddDReg(MIB, DestReg, ARM::dsub_3, RegState::Define, TRI);
905 MIB = AddDReg(MIB, DestReg, ARM::dsub_4, RegState::Define, TRI);
906 MIB = AddDReg(MIB, DestReg, ARM::dsub_5, RegState::Define, TRI);
907 MIB = AddDReg(MIB, DestReg, ARM::dsub_6, RegState::Define, TRI);
908 AddDReg(MIB, DestReg, ARM::dsub_7, RegState::Define, TRI);
912 llvm_unreachable("Unknown regclass!");
917 ARMBaseInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
918 int &FrameIndex) const {
919 switch (MI->getOpcode()) {
922 case ARM::t2LDRs: // FIXME: don't use t2LDRs to access frame.
923 if (MI->getOperand(1).isFI() &&
924 MI->getOperand(2).isReg() &&
925 MI->getOperand(3).isImm() &&
926 MI->getOperand(2).getReg() == 0 &&
927 MI->getOperand(3).getImm() == 0) {
928 FrameIndex = MI->getOperand(1).getIndex();
929 return MI->getOperand(0).getReg();
937 if (MI->getOperand(1).isFI() &&
938 MI->getOperand(2).isImm() &&
939 MI->getOperand(2).getImm() == 0) {
940 FrameIndex = MI->getOperand(1).getIndex();
941 return MI->getOperand(0).getReg();
944 case ARM::VLD1q64Pseudo:
945 if (MI->getOperand(1).isFI() &&
946 MI->getOperand(0).getSubReg() == 0) {
947 FrameIndex = MI->getOperand(1).getIndex();
948 return MI->getOperand(0).getReg();
952 if (MI->getOperand(1).isFI() &&
953 MI->getOperand(0).getSubReg() == 0) {
954 FrameIndex = MI->getOperand(1).getIndex();
955 return MI->getOperand(0).getReg();
964 ARMBaseInstrInfo::emitFrameIndexDebugValue(MachineFunction &MF,
965 int FrameIx, uint64_t Offset,
968 MachineInstrBuilder MIB = BuildMI(MF, DL, get(ARM::DBG_VALUE))
969 .addFrameIndex(FrameIx).addImm(0).addImm(Offset).addMetadata(MDPtr);
973 /// Create a copy of a const pool value. Update CPI to the new index and return
975 static unsigned duplicateCPV(MachineFunction &MF, unsigned &CPI) {
976 MachineConstantPool *MCP = MF.getConstantPool();
977 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
979 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPI];
980 assert(MCPE.isMachineConstantPoolEntry() &&
981 "Expecting a machine constantpool entry!");
982 ARMConstantPoolValue *ACPV =
983 static_cast<ARMConstantPoolValue*>(MCPE.Val.MachineCPVal);
985 unsigned PCLabelId = AFI->createPICLabelUId();
986 ARMConstantPoolValue *NewCPV = 0;
987 // FIXME: The below assumes PIC relocation model and that the function
988 // is Thumb mode (t1 or t2). PCAdjustment would be 8 for ARM mode PIC, and
989 // zero for non-PIC in ARM or Thumb. The callers are all of thumb LDR
990 // instructions, so that's probably OK, but is PIC always correct when
992 if (ACPV->isGlobalValue())
993 NewCPV = new ARMConstantPoolValue(ACPV->getGV(), PCLabelId,
995 else if (ACPV->isExtSymbol())
996 NewCPV = new ARMConstantPoolValue(MF.getFunction()->getContext(),
997 ACPV->getSymbol(), PCLabelId, 4);
998 else if (ACPV->isBlockAddress())
999 NewCPV = new ARMConstantPoolValue(ACPV->getBlockAddress(), PCLabelId,
1000 ARMCP::CPBlockAddress, 4);
1001 else if (ACPV->isLSDA())
1002 NewCPV = new ARMConstantPoolValue(MF.getFunction(), PCLabelId,
1005 llvm_unreachable("Unexpected ARM constantpool value type!!");
1006 CPI = MCP->getConstantPoolIndex(NewCPV, MCPE.getAlignment());
1010 void ARMBaseInstrInfo::
1011 reMaterialize(MachineBasicBlock &MBB,
1012 MachineBasicBlock::iterator I,
1013 unsigned DestReg, unsigned SubIdx,
1014 const MachineInstr *Orig,
1015 const TargetRegisterInfo &TRI) const {
1016 unsigned Opcode = Orig->getOpcode();
1019 MachineInstr *MI = MBB.getParent()->CloneMachineInstr(Orig);
1020 MI->substituteRegister(Orig->getOperand(0).getReg(), DestReg, SubIdx, TRI);
1024 case ARM::tLDRpci_pic:
1025 case ARM::t2LDRpci_pic: {
1026 MachineFunction &MF = *MBB.getParent();
1027 unsigned CPI = Orig->getOperand(1).getIndex();
1028 unsigned PCLabelId = duplicateCPV(MF, CPI);
1029 MachineInstrBuilder MIB = BuildMI(MBB, I, Orig->getDebugLoc(), get(Opcode),
1031 .addConstantPoolIndex(CPI).addImm(PCLabelId);
1032 (*MIB).setMemRefs(Orig->memoperands_begin(), Orig->memoperands_end());
1039 ARMBaseInstrInfo::duplicate(MachineInstr *Orig, MachineFunction &MF) const {
1040 MachineInstr *MI = TargetInstrInfoImpl::duplicate(Orig, MF);
1041 switch(Orig->getOpcode()) {
1042 case ARM::tLDRpci_pic:
1043 case ARM::t2LDRpci_pic: {
1044 unsigned CPI = Orig->getOperand(1).getIndex();
1045 unsigned PCLabelId = duplicateCPV(MF, CPI);
1046 Orig->getOperand(1).setIndex(CPI);
1047 Orig->getOperand(2).setImm(PCLabelId);
1054 bool ARMBaseInstrInfo::produceSameValue(const MachineInstr *MI0,
1055 const MachineInstr *MI1,
1056 const MachineRegisterInfo *MRI) const {
1057 int Opcode = MI0->getOpcode();
1058 if (Opcode == ARM::LDRi12 ||
1059 Opcode == ARM::t2LDRpci ||
1060 Opcode == ARM::t2LDRpci_pic ||
1061 Opcode == ARM::tLDRpci ||
1062 Opcode == ARM::tLDRpci_pic ||
1063 Opcode == ARM::MOV_pic_ga_add_pc ||
1064 Opcode == ARM::t2MOV_pic_ga_add_pc) {
1065 if (MI1->getOpcode() != Opcode)
1067 if (MI0->getNumOperands() != MI1->getNumOperands())
1070 const MachineOperand &MO0 = MI0->getOperand(1);
1071 const MachineOperand &MO1 = MI1->getOperand(1);
1072 if (Opcode == ARM::LDRi12 && (!MO0.isCPI() || !MO1.isCPI()))
1075 if (MO0.getOffset() != MO1.getOffset())
1078 if (Opcode == ARM::MOV_pic_ga_add_pc ||
1079 Opcode == ARM::t2MOV_pic_ga_add_pc)
1080 // Ignore the PC labels.
1081 return MO0.getGlobal() == MO1.getGlobal();
1083 const MachineFunction *MF = MI0->getParent()->getParent();
1084 const MachineConstantPool *MCP = MF->getConstantPool();
1085 int CPI0 = MO0.getIndex();
1086 int CPI1 = MO1.getIndex();
1087 const MachineConstantPoolEntry &MCPE0 = MCP->getConstants()[CPI0];
1088 const MachineConstantPoolEntry &MCPE1 = MCP->getConstants()[CPI1];
1089 ARMConstantPoolValue *ACPV0 =
1090 static_cast<ARMConstantPoolValue*>(MCPE0.Val.MachineCPVal);
1091 ARMConstantPoolValue *ACPV1 =
1092 static_cast<ARMConstantPoolValue*>(MCPE1.Val.MachineCPVal);
1093 return ACPV0->hasSameValue(ACPV1);
1094 } else if (Opcode == ARM::PICLDR) {
1095 if (MI1->getOpcode() != Opcode)
1097 if (MI0->getNumOperands() != MI1->getNumOperands())
1100 unsigned Addr0 = MI0->getOperand(1).getReg();
1101 unsigned Addr1 = MI1->getOperand(1).getReg();
1102 if (Addr0 != Addr1) {
1104 !TargetRegisterInfo::isVirtualRegister(Addr0) ||
1105 !TargetRegisterInfo::isVirtualRegister(Addr1))
1108 // This assumes SSA form.
1109 MachineInstr *Def0 = MRI->getVRegDef(Addr0);
1110 MachineInstr *Def1 = MRI->getVRegDef(Addr1);
1111 // Check if the loaded value, e.g. a constantpool of a global address, are
1113 if (!produceSameValue(Def0, Def1, MRI))
1117 for (unsigned i = 3, e = MI0->getNumOperands(); i != e; ++i) {
1118 // %vreg12<def> = PICLDR %vreg11, 0, pred:14, pred:%noreg
1119 const MachineOperand &MO0 = MI0->getOperand(i);
1120 const MachineOperand &MO1 = MI1->getOperand(i);
1121 if (!MO0.isIdenticalTo(MO1))
1127 return MI0->isIdenticalTo(MI1, MachineInstr::IgnoreVRegDefs);
1130 /// areLoadsFromSameBasePtr - This is used by the pre-regalloc scheduler to
1131 /// determine if two loads are loading from the same base address. It should
1132 /// only return true if the base pointers are the same and the only differences
1133 /// between the two addresses is the offset. It also returns the offsets by
1135 bool ARMBaseInstrInfo::areLoadsFromSameBasePtr(SDNode *Load1, SDNode *Load2,
1137 int64_t &Offset2) const {
1138 // Don't worry about Thumb: just ARM and Thumb2.
1139 if (Subtarget.isThumb1Only()) return false;
1141 if (!Load1->isMachineOpcode() || !Load2->isMachineOpcode())
1144 switch (Load1->getMachineOpcode()) {
1157 case ARM::t2LDRSHi8:
1159 case ARM::t2LDRSHi12:
1163 switch (Load2->getMachineOpcode()) {
1176 case ARM::t2LDRSHi8:
1178 case ARM::t2LDRSHi12:
1182 // Check if base addresses and chain operands match.
1183 if (Load1->getOperand(0) != Load2->getOperand(0) ||
1184 Load1->getOperand(4) != Load2->getOperand(4))
1187 // Index should be Reg0.
1188 if (Load1->getOperand(3) != Load2->getOperand(3))
1191 // Determine the offsets.
1192 if (isa<ConstantSDNode>(Load1->getOperand(1)) &&
1193 isa<ConstantSDNode>(Load2->getOperand(1))) {
1194 Offset1 = cast<ConstantSDNode>(Load1->getOperand(1))->getSExtValue();
1195 Offset2 = cast<ConstantSDNode>(Load2->getOperand(1))->getSExtValue();
1202 /// shouldScheduleLoadsNear - This is a used by the pre-regalloc scheduler to
1203 /// determine (in conjuction with areLoadsFromSameBasePtr) if two loads should
1204 /// be scheduled togther. On some targets if two loads are loading from
1205 /// addresses in the same cache line, it's better if they are scheduled
1206 /// together. This function takes two integers that represent the load offsets
1207 /// from the common base address. It returns true if it decides it's desirable
1208 /// to schedule the two loads together. "NumLoads" is the number of loads that
1209 /// have already been scheduled after Load1.
1210 bool ARMBaseInstrInfo::shouldScheduleLoadsNear(SDNode *Load1, SDNode *Load2,
1211 int64_t Offset1, int64_t Offset2,
1212 unsigned NumLoads) const {
1213 // Don't worry about Thumb: just ARM and Thumb2.
1214 if (Subtarget.isThumb1Only()) return false;
1216 assert(Offset2 > Offset1);
1218 if ((Offset2 - Offset1) / 8 > 64)
1221 if (Load1->getMachineOpcode() != Load2->getMachineOpcode())
1222 return false; // FIXME: overly conservative?
1224 // Four loads in a row should be sufficient.
1231 bool ARMBaseInstrInfo::isSchedulingBoundary(const MachineInstr *MI,
1232 const MachineBasicBlock *MBB,
1233 const MachineFunction &MF) const {
1234 // Debug info is never a scheduling boundary. It's necessary to be explicit
1235 // due to the special treatment of IT instructions below, otherwise a
1236 // dbg_value followed by an IT will result in the IT instruction being
1237 // considered a scheduling hazard, which is wrong. It should be the actual
1238 // instruction preceding the dbg_value instruction(s), just like it is
1239 // when debug info is not present.
1240 if (MI->isDebugValue())
1243 // Terminators and labels can't be scheduled around.
1244 if (MI->getDesc().isTerminator() || MI->isLabel())
1247 // Treat the start of the IT block as a scheduling boundary, but schedule
1248 // t2IT along with all instructions following it.
1249 // FIXME: This is a big hammer. But the alternative is to add all potential
1250 // true and anti dependencies to IT block instructions as implicit operands
1251 // to the t2IT instruction. The added compile time and complexity does not
1253 MachineBasicBlock::const_iterator I = MI;
1254 // Make sure to skip any dbg_value instructions
1255 while (++I != MBB->end() && I->isDebugValue())
1257 if (I != MBB->end() && I->getOpcode() == ARM::t2IT)
1260 // Don't attempt to schedule around any instruction that defines
1261 // a stack-oriented pointer, as it's unlikely to be profitable. This
1262 // saves compile time, because it doesn't require every single
1263 // stack slot reference to depend on the instruction that does the
1265 if (MI->definesRegister(ARM::SP))
1271 bool ARMBaseInstrInfo::isProfitableToIfCvt(MachineBasicBlock &MBB,
1273 unsigned ExtraPredCycles,
1275 float Confidence) const {
1279 // Attempt to estimate the relative costs of predication versus branching.
1280 float UnpredCost = Probability * NumCyles;
1281 UnpredCost += 1.0; // The branch itself
1282 UnpredCost += (1.0 - Confidence) * Subtarget.getMispredictionPenalty();
1284 return (float)(NumCyles + ExtraPredCycles) < UnpredCost;
1287 bool ARMBaseInstrInfo::
1288 isProfitableToIfCvt(MachineBasicBlock &TMBB,
1289 unsigned TCycles, unsigned TExtra,
1290 MachineBasicBlock &FMBB,
1291 unsigned FCycles, unsigned FExtra,
1292 float Probability, float Confidence) const {
1293 if (!TCycles || !FCycles)
1296 // Attempt to estimate the relative costs of predication versus branching.
1297 float UnpredCost = Probability * TCycles + (1.0 - Probability) * FCycles;
1298 UnpredCost += 1.0; // The branch itself
1299 UnpredCost += (1.0 - Confidence) * Subtarget.getMispredictionPenalty();
1301 return (float)(TCycles + FCycles + TExtra + FExtra) < UnpredCost;
1304 /// getInstrPredicate - If instruction is predicated, returns its predicate
1305 /// condition, otherwise returns AL. It also returns the condition code
1306 /// register by reference.
1308 llvm::getInstrPredicate(const MachineInstr *MI, unsigned &PredReg) {
1309 int PIdx = MI->findFirstPredOperandIdx();
1315 PredReg = MI->getOperand(PIdx+1).getReg();
1316 return (ARMCC::CondCodes)MI->getOperand(PIdx).getImm();
1320 int llvm::getMatchingCondBranchOpcode(int Opc) {
1323 else if (Opc == ARM::tB)
1325 else if (Opc == ARM::t2B)
1328 llvm_unreachable("Unknown unconditional branch opcode!");
1333 void llvm::emitARMRegPlusImmediate(MachineBasicBlock &MBB,
1334 MachineBasicBlock::iterator &MBBI, DebugLoc dl,
1335 unsigned DestReg, unsigned BaseReg, int NumBytes,
1336 ARMCC::CondCodes Pred, unsigned PredReg,
1337 const ARMBaseInstrInfo &TII) {
1338 bool isSub = NumBytes < 0;
1339 if (isSub) NumBytes = -NumBytes;
1342 unsigned RotAmt = ARM_AM::getSOImmValRotate(NumBytes);
1343 unsigned ThisVal = NumBytes & ARM_AM::rotr32(0xFF, RotAmt);
1344 assert(ThisVal && "Didn't extract field correctly");
1346 // We will handle these bits from offset, clear them.
1347 NumBytes &= ~ThisVal;
1349 assert(ARM_AM::getSOImmVal(ThisVal) != -1 && "Bit extraction didn't work?");
1351 // Build the new ADD / SUB.
1352 unsigned Opc = isSub ? ARM::SUBri : ARM::ADDri;
1353 BuildMI(MBB, MBBI, dl, TII.get(Opc), DestReg)
1354 .addReg(BaseReg, RegState::Kill).addImm(ThisVal)
1355 .addImm((unsigned)Pred).addReg(PredReg).addReg(0);
1360 bool llvm::rewriteARMFrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
1361 unsigned FrameReg, int &Offset,
1362 const ARMBaseInstrInfo &TII) {
1363 unsigned Opcode = MI.getOpcode();
1364 const TargetInstrDesc &Desc = MI.getDesc();
1365 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
1368 // Memory operands in inline assembly always use AddrMode2.
1369 if (Opcode == ARM::INLINEASM)
1370 AddrMode = ARMII::AddrMode2;
1372 if (Opcode == ARM::ADDri) {
1373 Offset += MI.getOperand(FrameRegIdx+1).getImm();
1375 // Turn it into a move.
1376 MI.setDesc(TII.get(ARM::MOVr));
1377 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
1378 MI.RemoveOperand(FrameRegIdx+1);
1381 } else if (Offset < 0) {
1384 MI.setDesc(TII.get(ARM::SUBri));
1387 // Common case: small offset, fits into instruction.
1388 if (ARM_AM::getSOImmVal(Offset) != -1) {
1389 // Replace the FrameIndex with sp / fp
1390 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
1391 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(Offset);
1396 // Otherwise, pull as much of the immedidate into this ADDri/SUBri
1398 unsigned RotAmt = ARM_AM::getSOImmValRotate(Offset);
1399 unsigned ThisImmVal = Offset & ARM_AM::rotr32(0xFF, RotAmt);
1401 // We will handle these bits from offset, clear them.
1402 Offset &= ~ThisImmVal;
1404 // Get the properly encoded SOImmVal field.
1405 assert(ARM_AM::getSOImmVal(ThisImmVal) != -1 &&
1406 "Bit extraction didn't work?");
1407 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(ThisImmVal);
1409 unsigned ImmIdx = 0;
1411 unsigned NumBits = 0;
1414 case ARMII::AddrMode_i12: {
1415 ImmIdx = FrameRegIdx + 1;
1416 InstrOffs = MI.getOperand(ImmIdx).getImm();
1420 case ARMII::AddrMode2: {
1421 ImmIdx = FrameRegIdx+2;
1422 InstrOffs = ARM_AM::getAM2Offset(MI.getOperand(ImmIdx).getImm());
1423 if (ARM_AM::getAM2Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
1428 case ARMII::AddrMode3: {
1429 ImmIdx = FrameRegIdx+2;
1430 InstrOffs = ARM_AM::getAM3Offset(MI.getOperand(ImmIdx).getImm());
1431 if (ARM_AM::getAM3Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
1436 case ARMII::AddrMode4:
1437 case ARMII::AddrMode6:
1438 // Can't fold any offset even if it's zero.
1440 case ARMII::AddrMode5: {
1441 ImmIdx = FrameRegIdx+1;
1442 InstrOffs = ARM_AM::getAM5Offset(MI.getOperand(ImmIdx).getImm());
1443 if (ARM_AM::getAM5Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
1450 llvm_unreachable("Unsupported addressing mode!");
1454 Offset += InstrOffs * Scale;
1455 assert((Offset & (Scale-1)) == 0 && "Can't encode this offset!");
1461 // Attempt to fold address comp. if opcode has offset bits
1463 // Common case: small offset, fits into instruction.
1464 MachineOperand &ImmOp = MI.getOperand(ImmIdx);
1465 int ImmedOffset = Offset / Scale;
1466 unsigned Mask = (1 << NumBits) - 1;
1467 if ((unsigned)Offset <= Mask * Scale) {
1468 // Replace the FrameIndex with sp
1469 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
1470 // FIXME: When addrmode2 goes away, this will simplify (like the
1471 // T2 version), as the LDR.i12 versions don't need the encoding
1472 // tricks for the offset value.
1474 if (AddrMode == ARMII::AddrMode_i12)
1475 ImmedOffset = -ImmedOffset;
1477 ImmedOffset |= 1 << NumBits;
1479 ImmOp.ChangeToImmediate(ImmedOffset);
1484 // Otherwise, it didn't fit. Pull in what we can to simplify the immed.
1485 ImmedOffset = ImmedOffset & Mask;
1487 if (AddrMode == ARMII::AddrMode_i12)
1488 ImmedOffset = -ImmedOffset;
1490 ImmedOffset |= 1 << NumBits;
1492 ImmOp.ChangeToImmediate(ImmedOffset);
1493 Offset &= ~(Mask*Scale);
1497 Offset = (isSub) ? -Offset : Offset;
1501 bool ARMBaseInstrInfo::
1502 AnalyzeCompare(const MachineInstr *MI, unsigned &SrcReg, int &CmpMask,
1503 int &CmpValue) const {
1504 switch (MI->getOpcode()) {
1508 SrcReg = MI->getOperand(0).getReg();
1510 CmpValue = MI->getOperand(1).getImm();
1514 SrcReg = MI->getOperand(0).getReg();
1515 CmpMask = MI->getOperand(1).getImm();
1523 /// isSuitableForMask - Identify a suitable 'and' instruction that
1524 /// operates on the given source register and applies the same mask
1525 /// as a 'tst' instruction. Provide a limited look-through for copies.
1526 /// When successful, MI will hold the found instruction.
1527 static bool isSuitableForMask(MachineInstr *&MI, unsigned SrcReg,
1528 int CmpMask, bool CommonUse) {
1529 switch (MI->getOpcode()) {
1532 if (CmpMask != MI->getOperand(2).getImm())
1534 if (SrcReg == MI->getOperand(CommonUse ? 1 : 0).getReg())
1538 // Walk down one instruction which is potentially an 'and'.
1539 const MachineInstr &Copy = *MI;
1540 MachineBasicBlock::iterator AND(
1541 llvm::next(MachineBasicBlock::iterator(MI)));
1542 if (AND == MI->getParent()->end()) return false;
1544 return isSuitableForMask(MI, Copy.getOperand(0).getReg(),
1552 /// OptimizeCompareInstr - Convert the instruction supplying the argument to the
1553 /// comparison into one that sets the zero bit in the flags register.
1554 bool ARMBaseInstrInfo::
1555 OptimizeCompareInstr(MachineInstr *CmpInstr, unsigned SrcReg, int CmpMask,
1556 int CmpValue, const MachineRegisterInfo *MRI) const {
1560 MachineRegisterInfo::def_iterator DI = MRI->def_begin(SrcReg);
1561 if (llvm::next(DI) != MRI->def_end())
1562 // Only support one definition.
1565 MachineInstr *MI = &*DI;
1567 // Masked compares sometimes use the same register as the corresponding 'and'.
1568 if (CmpMask != ~0) {
1569 if (!isSuitableForMask(MI, SrcReg, CmpMask, false)) {
1571 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(SrcReg),
1572 UE = MRI->use_end(); UI != UE; ++UI) {
1573 if (UI->getParent() != CmpInstr->getParent()) continue;
1574 MachineInstr *PotentialAND = &*UI;
1575 if (!isSuitableForMask(PotentialAND, SrcReg, CmpMask, true))
1580 if (!MI) return false;
1584 // Conservatively refuse to convert an instruction which isn't in the same BB
1585 // as the comparison.
1586 if (MI->getParent() != CmpInstr->getParent())
1589 // Check that CPSR isn't set between the comparison instruction and the one we
1591 MachineBasicBlock::const_iterator I = CmpInstr, E = MI,
1592 B = MI->getParent()->begin();
1594 // Early exit if CmpInstr is at the beginning of the BB.
1595 if (I == B) return false;
1598 for (; I != E; --I) {
1599 const MachineInstr &Instr = *I;
1601 for (unsigned IO = 0, EO = Instr.getNumOperands(); IO != EO; ++IO) {
1602 const MachineOperand &MO = Instr.getOperand(IO);
1603 if (!MO.isReg()) continue;
1605 // This instruction modifies or uses CPSR after the one we want to
1606 // change. We can't do this transformation.
1607 if (MO.getReg() == ARM::CPSR)
1612 // The 'and' is below the comparison instruction.
1616 // Set the "zero" bit in CPSR.
1617 switch (MI->getOpcode()) {
1625 // Toggle the optional operand to CPSR.
1626 MI->getOperand(5).setReg(ARM::CPSR);
1627 MI->getOperand(5).setIsDef(true);
1628 CmpInstr->eraseFromParent();
1635 bool ARMBaseInstrInfo::FoldImmediate(MachineInstr *UseMI,
1636 MachineInstr *DefMI, unsigned Reg,
1637 MachineRegisterInfo *MRI) const {
1638 // Fold large immediates into add, sub, or, xor.
1639 unsigned DefOpc = DefMI->getOpcode();
1640 if (DefOpc != ARM::t2MOVi32imm && DefOpc != ARM::MOVi32imm)
1642 if (!DefMI->getOperand(1).isImm())
1643 // Could be t2MOVi32imm <ga:xx>
1646 if (!MRI->hasOneNonDBGUse(Reg))
1649 unsigned UseOpc = UseMI->getOpcode();
1650 unsigned NewUseOpc = 0;
1651 uint32_t ImmVal = (uint32_t)DefMI->getOperand(1).getImm();
1652 uint32_t SOImmValV1 = 0, SOImmValV2 = 0;
1653 bool Commute = false;
1655 default: return false;
1663 case ARM::t2EORrr: {
1664 Commute = UseMI->getOperand(2).getReg() != Reg;
1671 NewUseOpc = ARM::SUBri;
1677 if (!ARM_AM::isSOImmTwoPartVal(ImmVal))
1679 SOImmValV1 = (uint32_t)ARM_AM::getSOImmTwoPartFirst(ImmVal);
1680 SOImmValV2 = (uint32_t)ARM_AM::getSOImmTwoPartSecond(ImmVal);
1683 case ARM::ADDrr: NewUseOpc = ARM::ADDri; break;
1684 case ARM::ORRrr: NewUseOpc = ARM::ORRri; break;
1685 case ARM::EORrr: NewUseOpc = ARM::EORri; break;
1689 case ARM::t2SUBrr: {
1693 NewUseOpc = ARM::t2SUBri;
1698 case ARM::t2EORrr: {
1699 if (!ARM_AM::isT2SOImmTwoPartVal(ImmVal))
1701 SOImmValV1 = (uint32_t)ARM_AM::getT2SOImmTwoPartFirst(ImmVal);
1702 SOImmValV2 = (uint32_t)ARM_AM::getT2SOImmTwoPartSecond(ImmVal);
1705 case ARM::t2ADDrr: NewUseOpc = ARM::t2ADDri; break;
1706 case ARM::t2ORRrr: NewUseOpc = ARM::t2ORRri; break;
1707 case ARM::t2EORrr: NewUseOpc = ARM::t2EORri; break;
1715 unsigned OpIdx = Commute ? 2 : 1;
1716 unsigned Reg1 = UseMI->getOperand(OpIdx).getReg();
1717 bool isKill = UseMI->getOperand(OpIdx).isKill();
1718 unsigned NewReg = MRI->createVirtualRegister(MRI->getRegClass(Reg));
1719 AddDefaultCC(AddDefaultPred(BuildMI(*UseMI->getParent(),
1720 *UseMI, UseMI->getDebugLoc(),
1721 get(NewUseOpc), NewReg)
1722 .addReg(Reg1, getKillRegState(isKill))
1723 .addImm(SOImmValV1)));
1724 UseMI->setDesc(get(NewUseOpc));
1725 UseMI->getOperand(1).setReg(NewReg);
1726 UseMI->getOperand(1).setIsKill();
1727 UseMI->getOperand(2).ChangeToImmediate(SOImmValV2);
1728 DefMI->eraseFromParent();
1733 ARMBaseInstrInfo::getNumMicroOps(const InstrItineraryData *ItinData,
1734 const MachineInstr *MI) const {
1735 if (!ItinData || ItinData->isEmpty())
1738 const TargetInstrDesc &Desc = MI->getDesc();
1739 unsigned Class = Desc.getSchedClass();
1740 unsigned UOps = ItinData->Itineraries[Class].NumMicroOps;
1744 unsigned Opc = MI->getOpcode();
1747 llvm_unreachable("Unexpected multi-uops instruction!");
1755 // The number of uOps for load / store multiple are determined by the number
1758 // On Cortex-A8, each pair of register loads / stores can be scheduled on the
1759 // same cycle. The scheduling for the first load / store must be done
1760 // separately by assuming the the address is not 64-bit aligned.
1762 // On Cortex-A9, the formula is simply (#reg / 2) + (#reg % 2). If the address
1763 // is not 64-bit aligned, then AGU would take an extra cycle. For VFP / NEON
1764 // load / store multiple, the formula is (#reg / 2) + (#reg % 2) + 1.
1767 case ARM::VLDMDIA_UPD:
1768 case ARM::VLDMDDB_UPD:
1771 case ARM::VLDMSIA_UPD:
1772 case ARM::VLDMSDB_UPD:
1775 case ARM::VSTMDIA_UPD:
1776 case ARM::VSTMDDB_UPD:
1779 case ARM::VSTMSIA_UPD:
1780 case ARM::VSTMSDB_UPD: {
1781 unsigned NumRegs = MI->getNumOperands() - Desc.getNumOperands();
1782 return (NumRegs / 2) + (NumRegs % 2) + 1;
1785 case ARM::LDMIA_RET:
1790 case ARM::LDMIA_UPD:
1791 case ARM::LDMDA_UPD:
1792 case ARM::LDMDB_UPD:
1793 case ARM::LDMIB_UPD:
1798 case ARM::STMIA_UPD:
1799 case ARM::STMDA_UPD:
1800 case ARM::STMDB_UPD:
1801 case ARM::STMIB_UPD:
1803 case ARM::tLDMIA_UPD:
1805 case ARM::tSTMIA_UPD:
1809 case ARM::t2LDMIA_RET:
1812 case ARM::t2LDMIA_UPD:
1813 case ARM::t2LDMDB_UPD:
1816 case ARM::t2STMIA_UPD:
1817 case ARM::t2STMDB_UPD: {
1818 unsigned NumRegs = MI->getNumOperands() - Desc.getNumOperands() + 1;
1819 if (Subtarget.isCortexA8()) {
1822 // 4 registers would be issued: 2, 2.
1823 // 5 registers would be issued: 2, 2, 1.
1824 UOps = (NumRegs / 2);
1828 } else if (Subtarget.isCortexA9()) {
1829 UOps = (NumRegs / 2);
1830 // If there are odd number of registers or if it's not 64-bit aligned,
1831 // then it takes an extra AGU (Address Generation Unit) cycle.
1832 if ((NumRegs % 2) ||
1833 !MI->hasOneMemOperand() ||
1834 (*MI->memoperands_begin())->getAlignment() < 8)
1838 // Assume the worst.
1846 ARMBaseInstrInfo::getVLDMDefCycle(const InstrItineraryData *ItinData,
1847 const TargetInstrDesc &DefTID,
1849 unsigned DefIdx, unsigned DefAlign) const {
1850 int RegNo = (int)(DefIdx+1) - DefTID.getNumOperands() + 1;
1852 // Def is the address writeback.
1853 return ItinData->getOperandCycle(DefClass, DefIdx);
1856 if (Subtarget.isCortexA8()) {
1857 // (regno / 2) + (regno % 2) + 1
1858 DefCycle = RegNo / 2 + 1;
1861 } else if (Subtarget.isCortexA9()) {
1863 bool isSLoad = false;
1865 switch (DefTID.getOpcode()) {
1869 case ARM::VLDMSIA_UPD:
1870 case ARM::VLDMSDB_UPD:
1875 // If there are odd number of 'S' registers or if it's not 64-bit aligned,
1876 // then it takes an extra cycle.
1877 if ((isSLoad && (RegNo % 2)) || DefAlign < 8)
1880 // Assume the worst.
1881 DefCycle = RegNo + 2;
1888 ARMBaseInstrInfo::getLDMDefCycle(const InstrItineraryData *ItinData,
1889 const TargetInstrDesc &DefTID,
1891 unsigned DefIdx, unsigned DefAlign) const {
1892 int RegNo = (int)(DefIdx+1) - DefTID.getNumOperands() + 1;
1894 // Def is the address writeback.
1895 return ItinData->getOperandCycle(DefClass, DefIdx);
1898 if (Subtarget.isCortexA8()) {
1899 // 4 registers would be issued: 1, 2, 1.
1900 // 5 registers would be issued: 1, 2, 2.
1901 DefCycle = RegNo / 2;
1904 // Result latency is issue cycle + 2: E2.
1906 } else if (Subtarget.isCortexA9()) {
1907 DefCycle = (RegNo / 2);
1908 // If there are odd number of registers or if it's not 64-bit aligned,
1909 // then it takes an extra AGU (Address Generation Unit) cycle.
1910 if ((RegNo % 2) || DefAlign < 8)
1912 // Result latency is AGU cycles + 2.
1915 // Assume the worst.
1916 DefCycle = RegNo + 2;
1923 ARMBaseInstrInfo::getVSTMUseCycle(const InstrItineraryData *ItinData,
1924 const TargetInstrDesc &UseTID,
1926 unsigned UseIdx, unsigned UseAlign) const {
1927 int RegNo = (int)(UseIdx+1) - UseTID.getNumOperands() + 1;
1929 return ItinData->getOperandCycle(UseClass, UseIdx);
1932 if (Subtarget.isCortexA8()) {
1933 // (regno / 2) + (regno % 2) + 1
1934 UseCycle = RegNo / 2 + 1;
1937 } else if (Subtarget.isCortexA9()) {
1939 bool isSStore = false;
1941 switch (UseTID.getOpcode()) {
1945 case ARM::VSTMSIA_UPD:
1946 case ARM::VSTMSDB_UPD:
1951 // If there are odd number of 'S' registers or if it's not 64-bit aligned,
1952 // then it takes an extra cycle.
1953 if ((isSStore && (RegNo % 2)) || UseAlign < 8)
1956 // Assume the worst.
1957 UseCycle = RegNo + 2;
1964 ARMBaseInstrInfo::getSTMUseCycle(const InstrItineraryData *ItinData,
1965 const TargetInstrDesc &UseTID,
1967 unsigned UseIdx, unsigned UseAlign) const {
1968 int RegNo = (int)(UseIdx+1) - UseTID.getNumOperands() + 1;
1970 return ItinData->getOperandCycle(UseClass, UseIdx);
1973 if (Subtarget.isCortexA8()) {
1974 UseCycle = RegNo / 2;
1979 } else if (Subtarget.isCortexA9()) {
1980 UseCycle = (RegNo / 2);
1981 // If there are odd number of registers or if it's not 64-bit aligned,
1982 // then it takes an extra AGU (Address Generation Unit) cycle.
1983 if ((RegNo % 2) || UseAlign < 8)
1986 // Assume the worst.
1993 ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
1994 const TargetInstrDesc &DefTID,
1995 unsigned DefIdx, unsigned DefAlign,
1996 const TargetInstrDesc &UseTID,
1997 unsigned UseIdx, unsigned UseAlign) const {
1998 unsigned DefClass = DefTID.getSchedClass();
1999 unsigned UseClass = UseTID.getSchedClass();
2001 if (DefIdx < DefTID.getNumDefs() && UseIdx < UseTID.getNumOperands())
2002 return ItinData->getOperandLatency(DefClass, DefIdx, UseClass, UseIdx);
2004 // This may be a def / use of a variable_ops instruction, the operand
2005 // latency might be determinable dynamically. Let the target try to
2008 bool LdmBypass = false;
2009 switch (DefTID.getOpcode()) {
2011 DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
2016 case ARM::VLDMDIA_UPD:
2017 case ARM::VLDMDDB_UPD:
2020 case ARM::VLDMSIA_UPD:
2021 case ARM::VLDMSDB_UPD:
2022 DefCycle = getVLDMDefCycle(ItinData, DefTID, DefClass, DefIdx, DefAlign);
2025 case ARM::LDMIA_RET:
2030 case ARM::LDMIA_UPD:
2031 case ARM::LDMDA_UPD:
2032 case ARM::LDMDB_UPD:
2033 case ARM::LDMIB_UPD:
2035 case ARM::tLDMIA_UPD:
2037 case ARM::t2LDMIA_RET:
2040 case ARM::t2LDMIA_UPD:
2041 case ARM::t2LDMDB_UPD:
2043 DefCycle = getLDMDefCycle(ItinData, DefTID, DefClass, DefIdx, DefAlign);
2048 // We can't seem to determine the result latency of the def, assume it's 2.
2052 switch (UseTID.getOpcode()) {
2054 UseCycle = ItinData->getOperandCycle(UseClass, UseIdx);
2059 case ARM::VSTMDIA_UPD:
2060 case ARM::VSTMDDB_UPD:
2063 case ARM::VSTMSIA_UPD:
2064 case ARM::VSTMSDB_UPD:
2065 UseCycle = getVSTMUseCycle(ItinData, UseTID, UseClass, UseIdx, UseAlign);
2072 case ARM::STMIA_UPD:
2073 case ARM::STMDA_UPD:
2074 case ARM::STMDB_UPD:
2075 case ARM::STMIB_UPD:
2077 case ARM::tSTMIA_UPD:
2082 case ARM::t2STMIA_UPD:
2083 case ARM::t2STMDB_UPD:
2084 UseCycle = getSTMUseCycle(ItinData, UseTID, UseClass, UseIdx, UseAlign);
2089 // Assume it's read in the first stage.
2092 UseCycle = DefCycle - UseCycle + 1;
2095 // It's a variable_ops instruction so we can't use DefIdx here. Just use
2096 // first def operand.
2097 if (ItinData->hasPipelineForwarding(DefClass, DefTID.getNumOperands()-1,
2100 } else if (ItinData->hasPipelineForwarding(DefClass, DefIdx,
2101 UseClass, UseIdx)) {
2110 ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
2111 const MachineInstr *DefMI, unsigned DefIdx,
2112 const MachineInstr *UseMI, unsigned UseIdx) const {
2113 if (DefMI->isCopyLike() || DefMI->isInsertSubreg() ||
2114 DefMI->isRegSequence() || DefMI->isImplicitDef())
2117 const TargetInstrDesc &DefTID = DefMI->getDesc();
2118 if (!ItinData || ItinData->isEmpty())
2119 return DefTID.mayLoad() ? 3 : 1;
2121 const TargetInstrDesc &UseTID = UseMI->getDesc();
2122 const MachineOperand &DefMO = DefMI->getOperand(DefIdx);
2123 if (DefMO.getReg() == ARM::CPSR) {
2124 if (DefMI->getOpcode() == ARM::FMSTAT) {
2125 // fpscr -> cpsr stalls over 20 cycles on A8 (and earlier?)
2126 return Subtarget.isCortexA9() ? 1 : 20;
2129 // CPSR set and branch can be paired in the same cycle.
2130 if (UseTID.isBranch())
2134 unsigned DefAlign = DefMI->hasOneMemOperand()
2135 ? (*DefMI->memoperands_begin())->getAlignment() : 0;
2136 unsigned UseAlign = UseMI->hasOneMemOperand()
2137 ? (*UseMI->memoperands_begin())->getAlignment() : 0;
2138 int Latency = getOperandLatency(ItinData, DefTID, DefIdx, DefAlign,
2139 UseTID, UseIdx, UseAlign);
2142 (Subtarget.isCortexA8() || Subtarget.isCortexA9())) {
2143 // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2]
2144 // variants are one cycle cheaper.
2145 switch (DefTID.getOpcode()) {
2149 unsigned ShOpVal = DefMI->getOperand(3).getImm();
2150 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2152 (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
2159 case ARM::t2LDRSHs: {
2160 // Thumb2 mode: lsl only.
2161 unsigned ShAmt = DefMI->getOperand(3).getImm();
2162 if (ShAmt == 0 || ShAmt == 2)
2173 ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
2174 SDNode *DefNode, unsigned DefIdx,
2175 SDNode *UseNode, unsigned UseIdx) const {
2176 if (!DefNode->isMachineOpcode())
2179 const TargetInstrDesc &DefTID = get(DefNode->getMachineOpcode());
2180 if (!ItinData || ItinData->isEmpty())
2181 return DefTID.mayLoad() ? 3 : 1;
2183 if (!UseNode->isMachineOpcode()) {
2184 int Latency = ItinData->getOperandCycle(DefTID.getSchedClass(), DefIdx);
2185 if (Subtarget.isCortexA9())
2186 return Latency <= 2 ? 1 : Latency - 1;
2188 return Latency <= 3 ? 1 : Latency - 2;
2191 const TargetInstrDesc &UseTID = get(UseNode->getMachineOpcode());
2192 const MachineSDNode *DefMN = dyn_cast<MachineSDNode>(DefNode);
2193 unsigned DefAlign = !DefMN->memoperands_empty()
2194 ? (*DefMN->memoperands_begin())->getAlignment() : 0;
2195 const MachineSDNode *UseMN = dyn_cast<MachineSDNode>(UseNode);
2196 unsigned UseAlign = !UseMN->memoperands_empty()
2197 ? (*UseMN->memoperands_begin())->getAlignment() : 0;
2198 int Latency = getOperandLatency(ItinData, DefTID, DefIdx, DefAlign,
2199 UseTID, UseIdx, UseAlign);
2202 (Subtarget.isCortexA8() || Subtarget.isCortexA9())) {
2203 // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2]
2204 // variants are one cycle cheaper.
2205 switch (DefTID.getOpcode()) {
2210 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
2211 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2213 (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
2220 case ARM::t2LDRSHs: {
2221 // Thumb2 mode: lsl only.
2223 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
2224 if (ShAmt == 0 || ShAmt == 2)
2234 int ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
2235 const MachineInstr *MI,
2236 unsigned *PredCost) const {
2237 if (MI->isCopyLike() || MI->isInsertSubreg() ||
2238 MI->isRegSequence() || MI->isImplicitDef())
2241 if (!ItinData || ItinData->isEmpty())
2244 const TargetInstrDesc &TID = MI->getDesc();
2245 unsigned Class = TID.getSchedClass();
2246 unsigned UOps = ItinData->Itineraries[Class].NumMicroOps;
2247 if (PredCost && TID.hasImplicitDefOfPhysReg(ARM::CPSR))
2248 // When predicated, CPSR is an additional source operand for CPSR updating
2249 // instructions, this apparently increases their latencies.
2252 return ItinData->getStageLatency(Class);
2253 return getNumMicroOps(ItinData, MI);
2256 int ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
2257 SDNode *Node) const {
2258 if (!Node->isMachineOpcode())
2261 if (!ItinData || ItinData->isEmpty())
2264 unsigned Opcode = Node->getMachineOpcode();
2267 return ItinData->getStageLatency(get(Opcode).getSchedClass());
2276 bool ARMBaseInstrInfo::
2277 hasHighOperandLatency(const InstrItineraryData *ItinData,
2278 const MachineRegisterInfo *MRI,
2279 const MachineInstr *DefMI, unsigned DefIdx,
2280 const MachineInstr *UseMI, unsigned UseIdx) const {
2281 unsigned DDomain = DefMI->getDesc().TSFlags & ARMII::DomainMask;
2282 unsigned UDomain = UseMI->getDesc().TSFlags & ARMII::DomainMask;
2283 if (Subtarget.isCortexA8() &&
2284 (DDomain == ARMII::DomainVFP || UDomain == ARMII::DomainVFP))
2285 // CortexA8 VFP instructions are not pipelined.
2288 // Hoist VFP / NEON instructions with 4 or higher latency.
2289 int Latency = getOperandLatency(ItinData, DefMI, DefIdx, UseMI, UseIdx);
2292 return DDomain == ARMII::DomainVFP || DDomain == ARMII::DomainNEON ||
2293 UDomain == ARMII::DomainVFP || UDomain == ARMII::DomainNEON;
2296 bool ARMBaseInstrInfo::
2297 hasLowDefLatency(const InstrItineraryData *ItinData,
2298 const MachineInstr *DefMI, unsigned DefIdx) const {
2299 if (!ItinData || ItinData->isEmpty())
2302 unsigned DDomain = DefMI->getDesc().TSFlags & ARMII::DomainMask;
2303 if (DDomain == ARMII::DomainGeneral) {
2304 unsigned DefClass = DefMI->getDesc().getSchedClass();
2305 int DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
2306 return (DefCycle != -1 && DefCycle <= 2);
2312 ARMBaseInstrInfo::isFpMLxInstruction(unsigned Opcode, unsigned &MulOpc,
2313 unsigned &AddSubOpc,
2314 bool &NegAcc, bool &HasLane) const {
2315 DenseMap<unsigned, unsigned>::const_iterator I = MLxEntryMap.find(Opcode);
2316 if (I == MLxEntryMap.end())
2319 const ARM_MLxEntry &Entry = ARM_MLxTable[I->second];
2320 MulOpc = Entry.MulOpc;
2321 AddSubOpc = Entry.AddSubOpc;
2322 NegAcc = Entry.NegAcc;
2323 HasLane = Entry.HasLane;