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::t2LDRpci ||
1059 Opcode == ARM::t2LDRpci_pic ||
1060 Opcode == ARM::tLDRpci ||
1061 Opcode == ARM::tLDRpci_pic ||
1062 Opcode == ARM::MOV_pic_ga_add_pc ||
1063 Opcode == ARM::t2MOV_pic_ga_add_pc) {
1064 if (MI1->getOpcode() != Opcode)
1066 if (MI0->getNumOperands() != MI1->getNumOperands())
1069 const MachineOperand &MO0 = MI0->getOperand(1);
1070 const MachineOperand &MO1 = MI1->getOperand(1);
1071 if (MO0.getOffset() != MO1.getOffset())
1074 if (Opcode == ARM::MOV_pic_ga_add_pc ||
1075 Opcode == ARM::t2MOV_pic_ga_add_pc)
1076 // Ignore the PC labels.
1077 return MO0.getGlobal() == MO1.getGlobal();
1079 const MachineFunction *MF = MI0->getParent()->getParent();
1080 const MachineConstantPool *MCP = MF->getConstantPool();
1081 int CPI0 = MO0.getIndex();
1082 int CPI1 = MO1.getIndex();
1083 const MachineConstantPoolEntry &MCPE0 = MCP->getConstants()[CPI0];
1084 const MachineConstantPoolEntry &MCPE1 = MCP->getConstants()[CPI1];
1085 ARMConstantPoolValue *ACPV0 =
1086 static_cast<ARMConstantPoolValue*>(MCPE0.Val.MachineCPVal);
1087 ARMConstantPoolValue *ACPV1 =
1088 static_cast<ARMConstantPoolValue*>(MCPE1.Val.MachineCPVal);
1089 return ACPV0->hasSameValue(ACPV1);
1090 } else if (Opcode == ARM::PICLDR) {
1091 if (MI1->getOpcode() != Opcode)
1093 if (MI0->getNumOperands() != MI1->getNumOperands())
1096 unsigned Addr0 = MI0->getOperand(1).getReg();
1097 unsigned Addr1 = MI1->getOperand(1).getReg();
1098 if (Addr0 != Addr1) {
1100 !TargetRegisterInfo::isVirtualRegister(Addr0) ||
1101 !TargetRegisterInfo::isVirtualRegister(Addr1))
1104 // This assumes SSA form.
1105 MachineInstr *Def0 = MRI->getVRegDef(Addr0);
1106 MachineInstr *Def1 = MRI->getVRegDef(Addr1);
1107 // Check if the loaded value, e.g. a constantpool of a global address, are
1109 if (!produceSameValue(Def0, Def1, MRI))
1113 for (unsigned i = 3, e = MI0->getNumOperands(); i != e; ++i) {
1114 // %vreg12<def> = PICLDR %vreg11, 0, pred:14, pred:%noreg
1115 const MachineOperand &MO0 = MI0->getOperand(i);
1116 const MachineOperand &MO1 = MI1->getOperand(i);
1117 if (!MO0.isIdenticalTo(MO1))
1123 return MI0->isIdenticalTo(MI1, MachineInstr::IgnoreVRegDefs);
1126 /// areLoadsFromSameBasePtr - This is used by the pre-regalloc scheduler to
1127 /// determine if two loads are loading from the same base address. It should
1128 /// only return true if the base pointers are the same and the only differences
1129 /// between the two addresses is the offset. It also returns the offsets by
1131 bool ARMBaseInstrInfo::areLoadsFromSameBasePtr(SDNode *Load1, SDNode *Load2,
1133 int64_t &Offset2) const {
1134 // Don't worry about Thumb: just ARM and Thumb2.
1135 if (Subtarget.isThumb1Only()) return false;
1137 if (!Load1->isMachineOpcode() || !Load2->isMachineOpcode())
1140 switch (Load1->getMachineOpcode()) {
1153 case ARM::t2LDRSHi8:
1155 case ARM::t2LDRSHi12:
1159 switch (Load2->getMachineOpcode()) {
1172 case ARM::t2LDRSHi8:
1174 case ARM::t2LDRSHi12:
1178 // Check if base addresses and chain operands match.
1179 if (Load1->getOperand(0) != Load2->getOperand(0) ||
1180 Load1->getOperand(4) != Load2->getOperand(4))
1183 // Index should be Reg0.
1184 if (Load1->getOperand(3) != Load2->getOperand(3))
1187 // Determine the offsets.
1188 if (isa<ConstantSDNode>(Load1->getOperand(1)) &&
1189 isa<ConstantSDNode>(Load2->getOperand(1))) {
1190 Offset1 = cast<ConstantSDNode>(Load1->getOperand(1))->getSExtValue();
1191 Offset2 = cast<ConstantSDNode>(Load2->getOperand(1))->getSExtValue();
1198 /// shouldScheduleLoadsNear - This is a used by the pre-regalloc scheduler to
1199 /// determine (in conjuction with areLoadsFromSameBasePtr) if two loads should
1200 /// be scheduled togther. On some targets if two loads are loading from
1201 /// addresses in the same cache line, it's better if they are scheduled
1202 /// together. This function takes two integers that represent the load offsets
1203 /// from the common base address. It returns true if it decides it's desirable
1204 /// to schedule the two loads together. "NumLoads" is the number of loads that
1205 /// have already been scheduled after Load1.
1206 bool ARMBaseInstrInfo::shouldScheduleLoadsNear(SDNode *Load1, SDNode *Load2,
1207 int64_t Offset1, int64_t Offset2,
1208 unsigned NumLoads) const {
1209 // Don't worry about Thumb: just ARM and Thumb2.
1210 if (Subtarget.isThumb1Only()) return false;
1212 assert(Offset2 > Offset1);
1214 if ((Offset2 - Offset1) / 8 > 64)
1217 if (Load1->getMachineOpcode() != Load2->getMachineOpcode())
1218 return false; // FIXME: overly conservative?
1220 // Four loads in a row should be sufficient.
1227 bool ARMBaseInstrInfo::isSchedulingBoundary(const MachineInstr *MI,
1228 const MachineBasicBlock *MBB,
1229 const MachineFunction &MF) const {
1230 // Debug info is never a scheduling boundary. It's necessary to be explicit
1231 // due to the special treatment of IT instructions below, otherwise a
1232 // dbg_value followed by an IT will result in the IT instruction being
1233 // considered a scheduling hazard, which is wrong. It should be the actual
1234 // instruction preceding the dbg_value instruction(s), just like it is
1235 // when debug info is not present.
1236 if (MI->isDebugValue())
1239 // Terminators and labels can't be scheduled around.
1240 if (MI->getDesc().isTerminator() || MI->isLabel())
1243 // Treat the start of the IT block as a scheduling boundary, but schedule
1244 // t2IT along with all instructions following it.
1245 // FIXME: This is a big hammer. But the alternative is to add all potential
1246 // true and anti dependencies to IT block instructions as implicit operands
1247 // to the t2IT instruction. The added compile time and complexity does not
1249 MachineBasicBlock::const_iterator I = MI;
1250 // Make sure to skip any dbg_value instructions
1251 while (++I != MBB->end() && I->isDebugValue())
1253 if (I != MBB->end() && I->getOpcode() == ARM::t2IT)
1256 // Don't attempt to schedule around any instruction that defines
1257 // a stack-oriented pointer, as it's unlikely to be profitable. This
1258 // saves compile time, because it doesn't require every single
1259 // stack slot reference to depend on the instruction that does the
1261 if (MI->definesRegister(ARM::SP))
1267 bool ARMBaseInstrInfo::isProfitableToIfCvt(MachineBasicBlock &MBB,
1269 unsigned ExtraPredCycles,
1271 float Confidence) const {
1275 // Attempt to estimate the relative costs of predication versus branching.
1276 float UnpredCost = Probability * NumCyles;
1277 UnpredCost += 1.0; // The branch itself
1278 UnpredCost += (1.0 - Confidence) * Subtarget.getMispredictionPenalty();
1280 return (float)(NumCyles + ExtraPredCycles) < UnpredCost;
1283 bool ARMBaseInstrInfo::
1284 isProfitableToIfCvt(MachineBasicBlock &TMBB,
1285 unsigned TCycles, unsigned TExtra,
1286 MachineBasicBlock &FMBB,
1287 unsigned FCycles, unsigned FExtra,
1288 float Probability, float Confidence) const {
1289 if (!TCycles || !FCycles)
1292 // Attempt to estimate the relative costs of predication versus branching.
1293 float UnpredCost = Probability * TCycles + (1.0 - Probability) * FCycles;
1294 UnpredCost += 1.0; // The branch itself
1295 UnpredCost += (1.0 - Confidence) * Subtarget.getMispredictionPenalty();
1297 return (float)(TCycles + FCycles + TExtra + FExtra) < UnpredCost;
1300 /// getInstrPredicate - If instruction is predicated, returns its predicate
1301 /// condition, otherwise returns AL. It also returns the condition code
1302 /// register by reference.
1304 llvm::getInstrPredicate(const MachineInstr *MI, unsigned &PredReg) {
1305 int PIdx = MI->findFirstPredOperandIdx();
1311 PredReg = MI->getOperand(PIdx+1).getReg();
1312 return (ARMCC::CondCodes)MI->getOperand(PIdx).getImm();
1316 int llvm::getMatchingCondBranchOpcode(int Opc) {
1319 else if (Opc == ARM::tB)
1321 else if (Opc == ARM::t2B)
1324 llvm_unreachable("Unknown unconditional branch opcode!");
1329 void llvm::emitARMRegPlusImmediate(MachineBasicBlock &MBB,
1330 MachineBasicBlock::iterator &MBBI, DebugLoc dl,
1331 unsigned DestReg, unsigned BaseReg, int NumBytes,
1332 ARMCC::CondCodes Pred, unsigned PredReg,
1333 const ARMBaseInstrInfo &TII) {
1334 bool isSub = NumBytes < 0;
1335 if (isSub) NumBytes = -NumBytes;
1338 unsigned RotAmt = ARM_AM::getSOImmValRotate(NumBytes);
1339 unsigned ThisVal = NumBytes & ARM_AM::rotr32(0xFF, RotAmt);
1340 assert(ThisVal && "Didn't extract field correctly");
1342 // We will handle these bits from offset, clear them.
1343 NumBytes &= ~ThisVal;
1345 assert(ARM_AM::getSOImmVal(ThisVal) != -1 && "Bit extraction didn't work?");
1347 // Build the new ADD / SUB.
1348 unsigned Opc = isSub ? ARM::SUBri : ARM::ADDri;
1349 BuildMI(MBB, MBBI, dl, TII.get(Opc), DestReg)
1350 .addReg(BaseReg, RegState::Kill).addImm(ThisVal)
1351 .addImm((unsigned)Pred).addReg(PredReg).addReg(0);
1356 bool llvm::rewriteARMFrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
1357 unsigned FrameReg, int &Offset,
1358 const ARMBaseInstrInfo &TII) {
1359 unsigned Opcode = MI.getOpcode();
1360 const TargetInstrDesc &Desc = MI.getDesc();
1361 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
1364 // Memory operands in inline assembly always use AddrMode2.
1365 if (Opcode == ARM::INLINEASM)
1366 AddrMode = ARMII::AddrMode2;
1368 if (Opcode == ARM::ADDri) {
1369 Offset += MI.getOperand(FrameRegIdx+1).getImm();
1371 // Turn it into a move.
1372 MI.setDesc(TII.get(ARM::MOVr));
1373 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
1374 MI.RemoveOperand(FrameRegIdx+1);
1377 } else if (Offset < 0) {
1380 MI.setDesc(TII.get(ARM::SUBri));
1383 // Common case: small offset, fits into instruction.
1384 if (ARM_AM::getSOImmVal(Offset) != -1) {
1385 // Replace the FrameIndex with sp / fp
1386 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
1387 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(Offset);
1392 // Otherwise, pull as much of the immedidate into this ADDri/SUBri
1394 unsigned RotAmt = ARM_AM::getSOImmValRotate(Offset);
1395 unsigned ThisImmVal = Offset & ARM_AM::rotr32(0xFF, RotAmt);
1397 // We will handle these bits from offset, clear them.
1398 Offset &= ~ThisImmVal;
1400 // Get the properly encoded SOImmVal field.
1401 assert(ARM_AM::getSOImmVal(ThisImmVal) != -1 &&
1402 "Bit extraction didn't work?");
1403 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(ThisImmVal);
1405 unsigned ImmIdx = 0;
1407 unsigned NumBits = 0;
1410 case ARMII::AddrMode_i12: {
1411 ImmIdx = FrameRegIdx + 1;
1412 InstrOffs = MI.getOperand(ImmIdx).getImm();
1416 case ARMII::AddrMode2: {
1417 ImmIdx = FrameRegIdx+2;
1418 InstrOffs = ARM_AM::getAM2Offset(MI.getOperand(ImmIdx).getImm());
1419 if (ARM_AM::getAM2Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
1424 case ARMII::AddrMode3: {
1425 ImmIdx = FrameRegIdx+2;
1426 InstrOffs = ARM_AM::getAM3Offset(MI.getOperand(ImmIdx).getImm());
1427 if (ARM_AM::getAM3Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
1432 case ARMII::AddrMode4:
1433 case ARMII::AddrMode6:
1434 // Can't fold any offset even if it's zero.
1436 case ARMII::AddrMode5: {
1437 ImmIdx = FrameRegIdx+1;
1438 InstrOffs = ARM_AM::getAM5Offset(MI.getOperand(ImmIdx).getImm());
1439 if (ARM_AM::getAM5Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
1446 llvm_unreachable("Unsupported addressing mode!");
1450 Offset += InstrOffs * Scale;
1451 assert((Offset & (Scale-1)) == 0 && "Can't encode this offset!");
1457 // Attempt to fold address comp. if opcode has offset bits
1459 // Common case: small offset, fits into instruction.
1460 MachineOperand &ImmOp = MI.getOperand(ImmIdx);
1461 int ImmedOffset = Offset / Scale;
1462 unsigned Mask = (1 << NumBits) - 1;
1463 if ((unsigned)Offset <= Mask * Scale) {
1464 // Replace the FrameIndex with sp
1465 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
1466 // FIXME: When addrmode2 goes away, this will simplify (like the
1467 // T2 version), as the LDR.i12 versions don't need the encoding
1468 // tricks for the offset value.
1470 if (AddrMode == ARMII::AddrMode_i12)
1471 ImmedOffset = -ImmedOffset;
1473 ImmedOffset |= 1 << NumBits;
1475 ImmOp.ChangeToImmediate(ImmedOffset);
1480 // Otherwise, it didn't fit. Pull in what we can to simplify the immed.
1481 ImmedOffset = ImmedOffset & Mask;
1483 if (AddrMode == ARMII::AddrMode_i12)
1484 ImmedOffset = -ImmedOffset;
1486 ImmedOffset |= 1 << NumBits;
1488 ImmOp.ChangeToImmediate(ImmedOffset);
1489 Offset &= ~(Mask*Scale);
1493 Offset = (isSub) ? -Offset : Offset;
1497 bool ARMBaseInstrInfo::
1498 AnalyzeCompare(const MachineInstr *MI, unsigned &SrcReg, int &CmpMask,
1499 int &CmpValue) const {
1500 switch (MI->getOpcode()) {
1504 SrcReg = MI->getOperand(0).getReg();
1506 CmpValue = MI->getOperand(1).getImm();
1510 SrcReg = MI->getOperand(0).getReg();
1511 CmpMask = MI->getOperand(1).getImm();
1519 /// isSuitableForMask - Identify a suitable 'and' instruction that
1520 /// operates on the given source register and applies the same mask
1521 /// as a 'tst' instruction. Provide a limited look-through for copies.
1522 /// When successful, MI will hold the found instruction.
1523 static bool isSuitableForMask(MachineInstr *&MI, unsigned SrcReg,
1524 int CmpMask, bool CommonUse) {
1525 switch (MI->getOpcode()) {
1528 if (CmpMask != MI->getOperand(2).getImm())
1530 if (SrcReg == MI->getOperand(CommonUse ? 1 : 0).getReg())
1534 // Walk down one instruction which is potentially an 'and'.
1535 const MachineInstr &Copy = *MI;
1536 MachineBasicBlock::iterator AND(
1537 llvm::next(MachineBasicBlock::iterator(MI)));
1538 if (AND == MI->getParent()->end()) return false;
1540 return isSuitableForMask(MI, Copy.getOperand(0).getReg(),
1548 /// OptimizeCompareInstr - Convert the instruction supplying the argument to the
1549 /// comparison into one that sets the zero bit in the flags register.
1550 bool ARMBaseInstrInfo::
1551 OptimizeCompareInstr(MachineInstr *CmpInstr, unsigned SrcReg, int CmpMask,
1552 int CmpValue, const MachineRegisterInfo *MRI) const {
1556 MachineRegisterInfo::def_iterator DI = MRI->def_begin(SrcReg);
1557 if (llvm::next(DI) != MRI->def_end())
1558 // Only support one definition.
1561 MachineInstr *MI = &*DI;
1563 // Masked compares sometimes use the same register as the corresponding 'and'.
1564 if (CmpMask != ~0) {
1565 if (!isSuitableForMask(MI, SrcReg, CmpMask, false)) {
1567 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(SrcReg),
1568 UE = MRI->use_end(); UI != UE; ++UI) {
1569 if (UI->getParent() != CmpInstr->getParent()) continue;
1570 MachineInstr *PotentialAND = &*UI;
1571 if (!isSuitableForMask(PotentialAND, SrcReg, CmpMask, true))
1576 if (!MI) return false;
1580 // Conservatively refuse to convert an instruction which isn't in the same BB
1581 // as the comparison.
1582 if (MI->getParent() != CmpInstr->getParent())
1585 // Check that CPSR isn't set between the comparison instruction and the one we
1587 MachineBasicBlock::const_iterator I = CmpInstr, E = MI,
1588 B = MI->getParent()->begin();
1590 // Early exit if CmpInstr is at the beginning of the BB.
1591 if (I == B) return false;
1594 for (; I != E; --I) {
1595 const MachineInstr &Instr = *I;
1597 for (unsigned IO = 0, EO = Instr.getNumOperands(); IO != EO; ++IO) {
1598 const MachineOperand &MO = Instr.getOperand(IO);
1599 if (!MO.isReg()) continue;
1601 // This instruction modifies or uses CPSR after the one we want to
1602 // change. We can't do this transformation.
1603 if (MO.getReg() == ARM::CPSR)
1608 // The 'and' is below the comparison instruction.
1612 // Set the "zero" bit in CPSR.
1613 switch (MI->getOpcode()) {
1621 // Toggle the optional operand to CPSR.
1622 MI->getOperand(5).setReg(ARM::CPSR);
1623 MI->getOperand(5).setIsDef(true);
1624 CmpInstr->eraseFromParent();
1631 bool ARMBaseInstrInfo::FoldImmediate(MachineInstr *UseMI,
1632 MachineInstr *DefMI, unsigned Reg,
1633 MachineRegisterInfo *MRI) const {
1634 // Fold large immediates into add, sub, or, xor.
1635 unsigned DefOpc = DefMI->getOpcode();
1636 if (DefOpc != ARM::t2MOVi32imm && DefOpc != ARM::MOVi32imm)
1638 if (!DefMI->getOperand(1).isImm())
1639 // Could be t2MOVi32imm <ga:xx>
1642 if (!MRI->hasOneNonDBGUse(Reg))
1645 unsigned UseOpc = UseMI->getOpcode();
1646 unsigned NewUseOpc = 0;
1647 uint32_t ImmVal = (uint32_t)DefMI->getOperand(1).getImm();
1648 uint32_t SOImmValV1 = 0, SOImmValV2 = 0;
1649 bool Commute = false;
1651 default: return false;
1659 case ARM::t2EORrr: {
1660 Commute = UseMI->getOperand(2).getReg() != Reg;
1667 NewUseOpc = ARM::SUBri;
1673 if (!ARM_AM::isSOImmTwoPartVal(ImmVal))
1675 SOImmValV1 = (uint32_t)ARM_AM::getSOImmTwoPartFirst(ImmVal);
1676 SOImmValV2 = (uint32_t)ARM_AM::getSOImmTwoPartSecond(ImmVal);
1679 case ARM::ADDrr: NewUseOpc = ARM::ADDri; break;
1680 case ARM::ORRrr: NewUseOpc = ARM::ORRri; break;
1681 case ARM::EORrr: NewUseOpc = ARM::EORri; break;
1685 case ARM::t2SUBrr: {
1689 NewUseOpc = ARM::t2SUBri;
1694 case ARM::t2EORrr: {
1695 if (!ARM_AM::isT2SOImmTwoPartVal(ImmVal))
1697 SOImmValV1 = (uint32_t)ARM_AM::getT2SOImmTwoPartFirst(ImmVal);
1698 SOImmValV2 = (uint32_t)ARM_AM::getT2SOImmTwoPartSecond(ImmVal);
1701 case ARM::t2ADDrr: NewUseOpc = ARM::t2ADDri; break;
1702 case ARM::t2ORRrr: NewUseOpc = ARM::t2ORRri; break;
1703 case ARM::t2EORrr: NewUseOpc = ARM::t2EORri; break;
1711 unsigned OpIdx = Commute ? 2 : 1;
1712 unsigned Reg1 = UseMI->getOperand(OpIdx).getReg();
1713 bool isKill = UseMI->getOperand(OpIdx).isKill();
1714 unsigned NewReg = MRI->createVirtualRegister(MRI->getRegClass(Reg));
1715 AddDefaultCC(AddDefaultPred(BuildMI(*UseMI->getParent(),
1716 *UseMI, UseMI->getDebugLoc(),
1717 get(NewUseOpc), NewReg)
1718 .addReg(Reg1, getKillRegState(isKill))
1719 .addImm(SOImmValV1)));
1720 UseMI->setDesc(get(NewUseOpc));
1721 UseMI->getOperand(1).setReg(NewReg);
1722 UseMI->getOperand(1).setIsKill();
1723 UseMI->getOperand(2).ChangeToImmediate(SOImmValV2);
1724 DefMI->eraseFromParent();
1729 ARMBaseInstrInfo::getNumMicroOps(const InstrItineraryData *ItinData,
1730 const MachineInstr *MI) const {
1731 if (!ItinData || ItinData->isEmpty())
1734 const TargetInstrDesc &Desc = MI->getDesc();
1735 unsigned Class = Desc.getSchedClass();
1736 unsigned UOps = ItinData->Itineraries[Class].NumMicroOps;
1740 unsigned Opc = MI->getOpcode();
1743 llvm_unreachable("Unexpected multi-uops instruction!");
1751 // The number of uOps for load / store multiple are determined by the number
1754 // On Cortex-A8, each pair of register loads / stores can be scheduled on the
1755 // same cycle. The scheduling for the first load / store must be done
1756 // separately by assuming the the address is not 64-bit aligned.
1758 // On Cortex-A9, the formula is simply (#reg / 2) + (#reg % 2). If the address
1759 // is not 64-bit aligned, then AGU would take an extra cycle. For VFP / NEON
1760 // load / store multiple, the formula is (#reg / 2) + (#reg % 2) + 1.
1763 case ARM::VLDMDIA_UPD:
1764 case ARM::VLDMDDB_UPD:
1767 case ARM::VLDMSIA_UPD:
1768 case ARM::VLDMSDB_UPD:
1771 case ARM::VSTMDIA_UPD:
1772 case ARM::VSTMDDB_UPD:
1775 case ARM::VSTMSIA_UPD:
1776 case ARM::VSTMSDB_UPD: {
1777 unsigned NumRegs = MI->getNumOperands() - Desc.getNumOperands();
1778 return (NumRegs / 2) + (NumRegs % 2) + 1;
1781 case ARM::LDMIA_RET:
1786 case ARM::LDMIA_UPD:
1787 case ARM::LDMDA_UPD:
1788 case ARM::LDMDB_UPD:
1789 case ARM::LDMIB_UPD:
1794 case ARM::STMIA_UPD:
1795 case ARM::STMDA_UPD:
1796 case ARM::STMDB_UPD:
1797 case ARM::STMIB_UPD:
1799 case ARM::tLDMIA_UPD:
1801 case ARM::tSTMIA_UPD:
1805 case ARM::t2LDMIA_RET:
1808 case ARM::t2LDMIA_UPD:
1809 case ARM::t2LDMDB_UPD:
1812 case ARM::t2STMIA_UPD:
1813 case ARM::t2STMDB_UPD: {
1814 unsigned NumRegs = MI->getNumOperands() - Desc.getNumOperands() + 1;
1815 if (Subtarget.isCortexA8()) {
1818 // 4 registers would be issued: 2, 2.
1819 // 5 registers would be issued: 2, 2, 1.
1820 UOps = (NumRegs / 2);
1824 } else if (Subtarget.isCortexA9()) {
1825 UOps = (NumRegs / 2);
1826 // If there are odd number of registers or if it's not 64-bit aligned,
1827 // then it takes an extra AGU (Address Generation Unit) cycle.
1828 if ((NumRegs % 2) ||
1829 !MI->hasOneMemOperand() ||
1830 (*MI->memoperands_begin())->getAlignment() < 8)
1834 // Assume the worst.
1842 ARMBaseInstrInfo::getVLDMDefCycle(const InstrItineraryData *ItinData,
1843 const TargetInstrDesc &DefTID,
1845 unsigned DefIdx, unsigned DefAlign) const {
1846 int RegNo = (int)(DefIdx+1) - DefTID.getNumOperands() + 1;
1848 // Def is the address writeback.
1849 return ItinData->getOperandCycle(DefClass, DefIdx);
1852 if (Subtarget.isCortexA8()) {
1853 // (regno / 2) + (regno % 2) + 1
1854 DefCycle = RegNo / 2 + 1;
1857 } else if (Subtarget.isCortexA9()) {
1859 bool isSLoad = false;
1861 switch (DefTID.getOpcode()) {
1865 case ARM::VLDMSIA_UPD:
1866 case ARM::VLDMSDB_UPD:
1871 // If there are odd number of 'S' registers or if it's not 64-bit aligned,
1872 // then it takes an extra cycle.
1873 if ((isSLoad && (RegNo % 2)) || DefAlign < 8)
1876 // Assume the worst.
1877 DefCycle = RegNo + 2;
1884 ARMBaseInstrInfo::getLDMDefCycle(const InstrItineraryData *ItinData,
1885 const TargetInstrDesc &DefTID,
1887 unsigned DefIdx, unsigned DefAlign) const {
1888 int RegNo = (int)(DefIdx+1) - DefTID.getNumOperands() + 1;
1890 // Def is the address writeback.
1891 return ItinData->getOperandCycle(DefClass, DefIdx);
1894 if (Subtarget.isCortexA8()) {
1895 // 4 registers would be issued: 1, 2, 1.
1896 // 5 registers would be issued: 1, 2, 2.
1897 DefCycle = RegNo / 2;
1900 // Result latency is issue cycle + 2: E2.
1902 } else if (Subtarget.isCortexA9()) {
1903 DefCycle = (RegNo / 2);
1904 // If there are odd number of registers or if it's not 64-bit aligned,
1905 // then it takes an extra AGU (Address Generation Unit) cycle.
1906 if ((RegNo % 2) || DefAlign < 8)
1908 // Result latency is AGU cycles + 2.
1911 // Assume the worst.
1912 DefCycle = RegNo + 2;
1919 ARMBaseInstrInfo::getVSTMUseCycle(const InstrItineraryData *ItinData,
1920 const TargetInstrDesc &UseTID,
1922 unsigned UseIdx, unsigned UseAlign) const {
1923 int RegNo = (int)(UseIdx+1) - UseTID.getNumOperands() + 1;
1925 return ItinData->getOperandCycle(UseClass, UseIdx);
1928 if (Subtarget.isCortexA8()) {
1929 // (regno / 2) + (regno % 2) + 1
1930 UseCycle = RegNo / 2 + 1;
1933 } else if (Subtarget.isCortexA9()) {
1935 bool isSStore = false;
1937 switch (UseTID.getOpcode()) {
1941 case ARM::VSTMSIA_UPD:
1942 case ARM::VSTMSDB_UPD:
1947 // If there are odd number of 'S' registers or if it's not 64-bit aligned,
1948 // then it takes an extra cycle.
1949 if ((isSStore && (RegNo % 2)) || UseAlign < 8)
1952 // Assume the worst.
1953 UseCycle = RegNo + 2;
1960 ARMBaseInstrInfo::getSTMUseCycle(const InstrItineraryData *ItinData,
1961 const TargetInstrDesc &UseTID,
1963 unsigned UseIdx, unsigned UseAlign) const {
1964 int RegNo = (int)(UseIdx+1) - UseTID.getNumOperands() + 1;
1966 return ItinData->getOperandCycle(UseClass, UseIdx);
1969 if (Subtarget.isCortexA8()) {
1970 UseCycle = RegNo / 2;
1975 } else if (Subtarget.isCortexA9()) {
1976 UseCycle = (RegNo / 2);
1977 // If there are odd number of registers or if it's not 64-bit aligned,
1978 // then it takes an extra AGU (Address Generation Unit) cycle.
1979 if ((RegNo % 2) || UseAlign < 8)
1982 // Assume the worst.
1989 ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
1990 const TargetInstrDesc &DefTID,
1991 unsigned DefIdx, unsigned DefAlign,
1992 const TargetInstrDesc &UseTID,
1993 unsigned UseIdx, unsigned UseAlign) const {
1994 unsigned DefClass = DefTID.getSchedClass();
1995 unsigned UseClass = UseTID.getSchedClass();
1997 if (DefIdx < DefTID.getNumDefs() && UseIdx < UseTID.getNumOperands())
1998 return ItinData->getOperandLatency(DefClass, DefIdx, UseClass, UseIdx);
2000 // This may be a def / use of a variable_ops instruction, the operand
2001 // latency might be determinable dynamically. Let the target try to
2004 bool LdmBypass = false;
2005 switch (DefTID.getOpcode()) {
2007 DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
2012 case ARM::VLDMDIA_UPD:
2013 case ARM::VLDMDDB_UPD:
2016 case ARM::VLDMSIA_UPD:
2017 case ARM::VLDMSDB_UPD:
2018 DefCycle = getVLDMDefCycle(ItinData, DefTID, DefClass, DefIdx, DefAlign);
2021 case ARM::LDMIA_RET:
2026 case ARM::LDMIA_UPD:
2027 case ARM::LDMDA_UPD:
2028 case ARM::LDMDB_UPD:
2029 case ARM::LDMIB_UPD:
2031 case ARM::tLDMIA_UPD:
2033 case ARM::t2LDMIA_RET:
2036 case ARM::t2LDMIA_UPD:
2037 case ARM::t2LDMDB_UPD:
2039 DefCycle = getLDMDefCycle(ItinData, DefTID, DefClass, DefIdx, DefAlign);
2044 // We can't seem to determine the result latency of the def, assume it's 2.
2048 switch (UseTID.getOpcode()) {
2050 UseCycle = ItinData->getOperandCycle(UseClass, UseIdx);
2055 case ARM::VSTMDIA_UPD:
2056 case ARM::VSTMDDB_UPD:
2059 case ARM::VSTMSIA_UPD:
2060 case ARM::VSTMSDB_UPD:
2061 UseCycle = getVSTMUseCycle(ItinData, UseTID, UseClass, UseIdx, UseAlign);
2068 case ARM::STMIA_UPD:
2069 case ARM::STMDA_UPD:
2070 case ARM::STMDB_UPD:
2071 case ARM::STMIB_UPD:
2073 case ARM::tSTMIA_UPD:
2078 case ARM::t2STMIA_UPD:
2079 case ARM::t2STMDB_UPD:
2080 UseCycle = getSTMUseCycle(ItinData, UseTID, UseClass, UseIdx, UseAlign);
2085 // Assume it's read in the first stage.
2088 UseCycle = DefCycle - UseCycle + 1;
2091 // It's a variable_ops instruction so we can't use DefIdx here. Just use
2092 // first def operand.
2093 if (ItinData->hasPipelineForwarding(DefClass, DefTID.getNumOperands()-1,
2096 } else if (ItinData->hasPipelineForwarding(DefClass, DefIdx,
2097 UseClass, UseIdx)) {
2106 ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
2107 const MachineInstr *DefMI, unsigned DefIdx,
2108 const MachineInstr *UseMI, unsigned UseIdx) const {
2109 if (DefMI->isCopyLike() || DefMI->isInsertSubreg() ||
2110 DefMI->isRegSequence() || DefMI->isImplicitDef())
2113 const TargetInstrDesc &DefTID = DefMI->getDesc();
2114 if (!ItinData || ItinData->isEmpty())
2115 return DefTID.mayLoad() ? 3 : 1;
2117 const TargetInstrDesc &UseTID = UseMI->getDesc();
2118 const MachineOperand &DefMO = DefMI->getOperand(DefIdx);
2119 if (DefMO.getReg() == ARM::CPSR) {
2120 if (DefMI->getOpcode() == ARM::FMSTAT) {
2121 // fpscr -> cpsr stalls over 20 cycles on A8 (and earlier?)
2122 return Subtarget.isCortexA9() ? 1 : 20;
2125 // CPSR set and branch can be paired in the same cycle.
2126 if (UseTID.isBranch())
2130 unsigned DefAlign = DefMI->hasOneMemOperand()
2131 ? (*DefMI->memoperands_begin())->getAlignment() : 0;
2132 unsigned UseAlign = UseMI->hasOneMemOperand()
2133 ? (*UseMI->memoperands_begin())->getAlignment() : 0;
2134 int Latency = getOperandLatency(ItinData, DefTID, DefIdx, DefAlign,
2135 UseTID, UseIdx, UseAlign);
2138 (Subtarget.isCortexA8() || Subtarget.isCortexA9())) {
2139 // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2]
2140 // variants are one cycle cheaper.
2141 switch (DefTID.getOpcode()) {
2145 unsigned ShOpVal = DefMI->getOperand(3).getImm();
2146 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2148 (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
2155 case ARM::t2LDRSHs: {
2156 // Thumb2 mode: lsl only.
2157 unsigned ShAmt = DefMI->getOperand(3).getImm();
2158 if (ShAmt == 0 || ShAmt == 2)
2169 ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
2170 SDNode *DefNode, unsigned DefIdx,
2171 SDNode *UseNode, unsigned UseIdx) const {
2172 if (!DefNode->isMachineOpcode())
2175 const TargetInstrDesc &DefTID = get(DefNode->getMachineOpcode());
2176 if (!ItinData || ItinData->isEmpty())
2177 return DefTID.mayLoad() ? 3 : 1;
2179 if (!UseNode->isMachineOpcode()) {
2180 int Latency = ItinData->getOperandCycle(DefTID.getSchedClass(), DefIdx);
2181 if (Subtarget.isCortexA9())
2182 return Latency <= 2 ? 1 : Latency - 1;
2184 return Latency <= 3 ? 1 : Latency - 2;
2187 const TargetInstrDesc &UseTID = get(UseNode->getMachineOpcode());
2188 const MachineSDNode *DefMN = dyn_cast<MachineSDNode>(DefNode);
2189 unsigned DefAlign = !DefMN->memoperands_empty()
2190 ? (*DefMN->memoperands_begin())->getAlignment() : 0;
2191 const MachineSDNode *UseMN = dyn_cast<MachineSDNode>(UseNode);
2192 unsigned UseAlign = !UseMN->memoperands_empty()
2193 ? (*UseMN->memoperands_begin())->getAlignment() : 0;
2194 int Latency = getOperandLatency(ItinData, DefTID, DefIdx, DefAlign,
2195 UseTID, UseIdx, UseAlign);
2198 (Subtarget.isCortexA8() || Subtarget.isCortexA9())) {
2199 // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2]
2200 // variants are one cycle cheaper.
2201 switch (DefTID.getOpcode()) {
2206 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
2207 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2209 (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
2216 case ARM::t2LDRSHs: {
2217 // Thumb2 mode: lsl only.
2219 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
2220 if (ShAmt == 0 || ShAmt == 2)
2230 int ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
2231 const MachineInstr *MI,
2232 unsigned *PredCost) const {
2233 if (MI->isCopyLike() || MI->isInsertSubreg() ||
2234 MI->isRegSequence() || MI->isImplicitDef())
2237 if (!ItinData || ItinData->isEmpty())
2240 const TargetInstrDesc &TID = MI->getDesc();
2241 unsigned Class = TID.getSchedClass();
2242 unsigned UOps = ItinData->Itineraries[Class].NumMicroOps;
2243 if (PredCost && TID.hasImplicitDefOfPhysReg(ARM::CPSR))
2244 // When predicated, CPSR is an additional source operand for CPSR updating
2245 // instructions, this apparently increases their latencies.
2248 return ItinData->getStageLatency(Class);
2249 return getNumMicroOps(ItinData, MI);
2252 int ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
2253 SDNode *Node) const {
2254 if (!Node->isMachineOpcode())
2257 if (!ItinData || ItinData->isEmpty())
2260 unsigned Opcode = Node->getMachineOpcode();
2263 return ItinData->getStageLatency(get(Opcode).getSchedClass());
2272 bool ARMBaseInstrInfo::
2273 hasHighOperandLatency(const InstrItineraryData *ItinData,
2274 const MachineRegisterInfo *MRI,
2275 const MachineInstr *DefMI, unsigned DefIdx,
2276 const MachineInstr *UseMI, unsigned UseIdx) const {
2277 unsigned DDomain = DefMI->getDesc().TSFlags & ARMII::DomainMask;
2278 unsigned UDomain = UseMI->getDesc().TSFlags & ARMII::DomainMask;
2279 if (Subtarget.isCortexA8() &&
2280 (DDomain == ARMII::DomainVFP || UDomain == ARMII::DomainVFP))
2281 // CortexA8 VFP instructions are not pipelined.
2284 // Hoist VFP / NEON instructions with 4 or higher latency.
2285 int Latency = getOperandLatency(ItinData, DefMI, DefIdx, UseMI, UseIdx);
2288 return DDomain == ARMII::DomainVFP || DDomain == ARMII::DomainNEON ||
2289 UDomain == ARMII::DomainVFP || UDomain == ARMII::DomainNEON;
2292 bool ARMBaseInstrInfo::
2293 hasLowDefLatency(const InstrItineraryData *ItinData,
2294 const MachineInstr *DefMI, unsigned DefIdx) const {
2295 if (!ItinData || ItinData->isEmpty())
2298 unsigned DDomain = DefMI->getDesc().TSFlags & ARMII::DomainMask;
2299 if (DDomain == ARMII::DomainGeneral) {
2300 unsigned DefClass = DefMI->getDesc().getSchedClass();
2301 int DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
2302 return (DefCycle != -1 && DefCycle <= 2);
2308 ARMBaseInstrInfo::isFpMLxInstruction(unsigned Opcode, unsigned &MulOpc,
2309 unsigned &AddSubOpc,
2310 bool &NegAcc, bool &HasLane) const {
2311 DenseMap<unsigned, unsigned>::const_iterator I = MLxEntryMap.find(Opcode);
2312 if (I == MLxEntryMap.end())
2315 const ARM_MLxEntry &Entry = ARM_MLxTable[I->second];
2316 MulOpc = Entry.MulOpc;
2317 AddSubOpc = Entry.AddSubOpc;
2318 NegAcc = Entry.NegAcc;
2319 HasLane = Entry.HasLane;