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 "llvm/Constants.h"
22 #include "llvm/Function.h"
23 #include "llvm/GlobalValue.h"
24 #include "llvm/CodeGen/LiveVariables.h"
25 #include "llvm/CodeGen/MachineConstantPool.h"
26 #include "llvm/CodeGen/MachineFrameInfo.h"
27 #include "llvm/CodeGen/MachineInstrBuilder.h"
28 #include "llvm/CodeGen/MachineJumpTableInfo.h"
29 #include "llvm/CodeGen/MachineMemOperand.h"
30 #include "llvm/CodeGen/MachineRegisterInfo.h"
31 #include "llvm/CodeGen/PseudoSourceValue.h"
32 #include "llvm/MC/MCAsmInfo.h"
33 #include "llvm/Support/CommandLine.h"
34 #include "llvm/Support/Debug.h"
35 #include "llvm/Support/ErrorHandling.h"
36 #include "llvm/ADT/STLExtras.h"
38 #define GET_INSTRINFO_MC_DESC
39 #define GET_INSTRINFO_CTOR
40 #include "ARMGenInstrInfo.inc"
45 EnableARM3Addr("enable-arm-3-addr-conv", cl::Hidden,
46 cl::desc("Enable ARM 2-addr to 3-addr conv"));
48 /// ARM_MLxEntry - Record information about MLA / MLS instructions.
50 unsigned MLxOpc; // MLA / MLS opcode
51 unsigned MulOpc; // Expanded multiplication opcode
52 unsigned AddSubOpc; // Expanded add / sub opcode
53 bool NegAcc; // True if the acc is negated before the add / sub.
54 bool HasLane; // True if instruction has an extra "lane" operand.
57 static const ARM_MLxEntry ARM_MLxTable[] = {
58 // MLxOpc, MulOpc, AddSubOpc, NegAcc, HasLane
60 { ARM::VMLAS, ARM::VMULS, ARM::VADDS, false, false },
61 { ARM::VMLSS, ARM::VMULS, ARM::VSUBS, false, false },
62 { ARM::VMLAD, ARM::VMULD, ARM::VADDD, false, false },
63 { ARM::VMLSD, ARM::VMULD, ARM::VSUBD, false, false },
64 { ARM::VNMLAS, ARM::VNMULS, ARM::VSUBS, true, false },
65 { ARM::VNMLSS, ARM::VMULS, ARM::VSUBS, true, false },
66 { ARM::VNMLAD, ARM::VNMULD, ARM::VSUBD, true, false },
67 { ARM::VNMLSD, ARM::VMULD, ARM::VSUBD, true, false },
70 { ARM::VMLAfd, ARM::VMULfd, ARM::VADDfd, false, false },
71 { ARM::VMLSfd, ARM::VMULfd, ARM::VSUBfd, false, false },
72 { ARM::VMLAfq, ARM::VMULfq, ARM::VADDfq, false, false },
73 { ARM::VMLSfq, ARM::VMULfq, ARM::VSUBfq, false, false },
74 { ARM::VMLAslfd, ARM::VMULslfd, ARM::VADDfd, false, true },
75 { ARM::VMLSslfd, ARM::VMULslfd, ARM::VSUBfd, false, true },
76 { ARM::VMLAslfq, ARM::VMULslfq, ARM::VADDfq, false, true },
77 { ARM::VMLSslfq, ARM::VMULslfq, ARM::VSUBfq, false, true },
80 ARMBaseInstrInfo::ARMBaseInstrInfo(const ARMSubtarget& STI)
81 : ARMGenInstrInfo(ARM::ADJCALLSTACKDOWN, ARM::ADJCALLSTACKUP),
83 for (unsigned i = 0, e = array_lengthof(ARM_MLxTable); i != e; ++i) {
84 if (!MLxEntryMap.insert(std::make_pair(ARM_MLxTable[i].MLxOpc, i)).second)
85 assert(false && "Duplicated entries?");
86 MLxHazardOpcodes.insert(ARM_MLxTable[i].AddSubOpc);
87 MLxHazardOpcodes.insert(ARM_MLxTable[i].MulOpc);
91 // Use a ScoreboardHazardRecognizer for prepass ARM scheduling. TargetInstrImpl
92 // currently defaults to no prepass hazard recognizer.
93 ScheduleHazardRecognizer *ARMBaseInstrInfo::
94 CreateTargetHazardRecognizer(const TargetMachine *TM,
95 const ScheduleDAG *DAG) const {
96 if (usePreRAHazardRecognizer()) {
97 const InstrItineraryData *II = TM->getInstrItineraryData();
98 return new ScoreboardHazardRecognizer(II, DAG, "pre-RA-sched");
100 return TargetInstrInfoImpl::CreateTargetHazardRecognizer(TM, DAG);
103 ScheduleHazardRecognizer *ARMBaseInstrInfo::
104 CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II,
105 const ScheduleDAG *DAG) const {
106 if (Subtarget.isThumb2() || Subtarget.hasVFP2())
107 return (ScheduleHazardRecognizer *)
108 new ARMHazardRecognizer(II, *this, getRegisterInfo(), Subtarget, DAG);
109 return TargetInstrInfoImpl::CreateTargetPostRAHazardRecognizer(II, DAG);
113 ARMBaseInstrInfo::convertToThreeAddress(MachineFunction::iterator &MFI,
114 MachineBasicBlock::iterator &MBBI,
115 LiveVariables *LV) const {
116 // FIXME: Thumb2 support.
121 MachineInstr *MI = MBBI;
122 MachineFunction &MF = *MI->getParent()->getParent();
123 uint64_t TSFlags = MI->getDesc().TSFlags;
125 switch ((TSFlags & ARMII::IndexModeMask) >> ARMII::IndexModeShift) {
126 default: return NULL;
127 case ARMII::IndexModePre:
130 case ARMII::IndexModePost:
134 // Try splitting an indexed load/store to an un-indexed one plus an add/sub
136 unsigned MemOpc = getUnindexedOpcode(MI->getOpcode());
140 MachineInstr *UpdateMI = NULL;
141 MachineInstr *MemMI = NULL;
142 unsigned AddrMode = (TSFlags & ARMII::AddrModeMask);
143 const MCInstrDesc &MCID = MI->getDesc();
144 unsigned NumOps = MCID.getNumOperands();
145 bool isLoad = !MCID.mayStore();
146 const MachineOperand &WB = isLoad ? MI->getOperand(1) : MI->getOperand(0);
147 const MachineOperand &Base = MI->getOperand(2);
148 const MachineOperand &Offset = MI->getOperand(NumOps-3);
149 unsigned WBReg = WB.getReg();
150 unsigned BaseReg = Base.getReg();
151 unsigned OffReg = Offset.getReg();
152 unsigned OffImm = MI->getOperand(NumOps-2).getImm();
153 ARMCC::CondCodes Pred = (ARMCC::CondCodes)MI->getOperand(NumOps-1).getImm();
156 assert(false && "Unknown indexed op!");
158 case ARMII::AddrMode2: {
159 bool isSub = ARM_AM::getAM2Op(OffImm) == ARM_AM::sub;
160 unsigned Amt = ARM_AM::getAM2Offset(OffImm);
162 if (ARM_AM::getSOImmVal(Amt) == -1)
163 // Can't encode it in a so_imm operand. This transformation will
164 // add more than 1 instruction. Abandon!
166 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
167 get(isSub ? ARM::SUBri : ARM::ADDri), WBReg)
168 .addReg(BaseReg).addImm(Amt)
169 .addImm(Pred).addReg(0).addReg(0);
170 } else if (Amt != 0) {
171 ARM_AM::ShiftOpc ShOpc = ARM_AM::getAM2ShiftOpc(OffImm);
172 unsigned SOOpc = ARM_AM::getSORegOpc(ShOpc, Amt);
173 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
174 get(isSub ? ARM::SUBrs : ARM::ADDrs), WBReg)
175 .addReg(BaseReg).addReg(OffReg).addReg(0).addImm(SOOpc)
176 .addImm(Pred).addReg(0).addReg(0);
178 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
179 get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
180 .addReg(BaseReg).addReg(OffReg)
181 .addImm(Pred).addReg(0).addReg(0);
184 case ARMII::AddrMode3 : {
185 bool isSub = ARM_AM::getAM3Op(OffImm) == ARM_AM::sub;
186 unsigned Amt = ARM_AM::getAM3Offset(OffImm);
188 // Immediate is 8-bits. It's guaranteed to fit in a so_imm operand.
189 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
190 get(isSub ? ARM::SUBri : ARM::ADDri), WBReg)
191 .addReg(BaseReg).addImm(Amt)
192 .addImm(Pred).addReg(0).addReg(0);
194 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
195 get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
196 .addReg(BaseReg).addReg(OffReg)
197 .addImm(Pred).addReg(0).addReg(0);
202 std::vector<MachineInstr*> NewMIs;
205 MemMI = BuildMI(MF, MI->getDebugLoc(),
206 get(MemOpc), MI->getOperand(0).getReg())
207 .addReg(WBReg).addImm(0).addImm(Pred);
209 MemMI = BuildMI(MF, MI->getDebugLoc(),
210 get(MemOpc)).addReg(MI->getOperand(1).getReg())
211 .addReg(WBReg).addReg(0).addImm(0).addImm(Pred);
212 NewMIs.push_back(MemMI);
213 NewMIs.push_back(UpdateMI);
216 MemMI = BuildMI(MF, MI->getDebugLoc(),
217 get(MemOpc), MI->getOperand(0).getReg())
218 .addReg(BaseReg).addImm(0).addImm(Pred);
220 MemMI = BuildMI(MF, MI->getDebugLoc(),
221 get(MemOpc)).addReg(MI->getOperand(1).getReg())
222 .addReg(BaseReg).addReg(0).addImm(0).addImm(Pred);
224 UpdateMI->getOperand(0).setIsDead();
225 NewMIs.push_back(UpdateMI);
226 NewMIs.push_back(MemMI);
229 // Transfer LiveVariables states, kill / dead info.
231 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
232 MachineOperand &MO = MI->getOperand(i);
233 if (MO.isReg() && TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
234 unsigned Reg = MO.getReg();
236 LiveVariables::VarInfo &VI = LV->getVarInfo(Reg);
238 MachineInstr *NewMI = (Reg == WBReg) ? UpdateMI : MemMI;
240 LV->addVirtualRegisterDead(Reg, NewMI);
242 if (MO.isUse() && MO.isKill()) {
243 for (unsigned j = 0; j < 2; ++j) {
244 // Look at the two new MI's in reverse order.
245 MachineInstr *NewMI = NewMIs[j];
246 if (!NewMI->readsRegister(Reg))
248 LV->addVirtualRegisterKilled(Reg, NewMI);
249 if (VI.removeKill(MI))
250 VI.Kills.push_back(NewMI);
258 MFI->insert(MBBI, NewMIs[1]);
259 MFI->insert(MBBI, NewMIs[0]);
265 ARMBaseInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
266 MachineBasicBlock *&FBB,
267 SmallVectorImpl<MachineOperand> &Cond,
268 bool AllowModify) const {
269 // If the block has no terminators, it just falls into the block after it.
270 MachineBasicBlock::iterator I = MBB.end();
271 if (I == MBB.begin())
274 while (I->isDebugValue()) {
275 if (I == MBB.begin())
279 if (!isUnpredicatedTerminator(I))
282 // Get the last instruction in the block.
283 MachineInstr *LastInst = I;
285 // If there is only one terminator instruction, process it.
286 unsigned LastOpc = LastInst->getOpcode();
287 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
288 if (isUncondBranchOpcode(LastOpc)) {
289 TBB = LastInst->getOperand(0).getMBB();
292 if (isCondBranchOpcode(LastOpc)) {
293 // Block ends with fall-through condbranch.
294 TBB = LastInst->getOperand(0).getMBB();
295 Cond.push_back(LastInst->getOperand(1));
296 Cond.push_back(LastInst->getOperand(2));
299 return true; // Can't handle indirect branch.
302 // Get the instruction before it if it is a terminator.
303 MachineInstr *SecondLastInst = I;
304 unsigned SecondLastOpc = SecondLastInst->getOpcode();
306 // If AllowModify is true and the block ends with two or more unconditional
307 // branches, delete all but the first unconditional branch.
308 if (AllowModify && isUncondBranchOpcode(LastOpc)) {
309 while (isUncondBranchOpcode(SecondLastOpc)) {
310 LastInst->eraseFromParent();
311 LastInst = SecondLastInst;
312 LastOpc = LastInst->getOpcode();
313 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
314 // Return now the only terminator is an unconditional branch.
315 TBB = LastInst->getOperand(0).getMBB();
319 SecondLastOpc = SecondLastInst->getOpcode();
324 // If there are three terminators, we don't know what sort of block this is.
325 if (SecondLastInst && I != MBB.begin() && isUnpredicatedTerminator(--I))
328 // If the block ends with a B and a Bcc, handle it.
329 if (isCondBranchOpcode(SecondLastOpc) && isUncondBranchOpcode(LastOpc)) {
330 TBB = SecondLastInst->getOperand(0).getMBB();
331 Cond.push_back(SecondLastInst->getOperand(1));
332 Cond.push_back(SecondLastInst->getOperand(2));
333 FBB = LastInst->getOperand(0).getMBB();
337 // If the block ends with two unconditional branches, handle it. The second
338 // one is not executed, so remove it.
339 if (isUncondBranchOpcode(SecondLastOpc) && isUncondBranchOpcode(LastOpc)) {
340 TBB = SecondLastInst->getOperand(0).getMBB();
343 I->eraseFromParent();
347 // ...likewise if it ends with a branch table followed by an unconditional
348 // branch. The branch folder can create these, and we must get rid of them for
349 // correctness of Thumb constant islands.
350 if ((isJumpTableBranchOpcode(SecondLastOpc) ||
351 isIndirectBranchOpcode(SecondLastOpc)) &&
352 isUncondBranchOpcode(LastOpc)) {
355 I->eraseFromParent();
359 // Otherwise, can't handle this.
364 unsigned ARMBaseInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
365 MachineBasicBlock::iterator I = MBB.end();
366 if (I == MBB.begin()) return 0;
368 while (I->isDebugValue()) {
369 if (I == MBB.begin())
373 if (!isUncondBranchOpcode(I->getOpcode()) &&
374 !isCondBranchOpcode(I->getOpcode()))
377 // Remove the branch.
378 I->eraseFromParent();
382 if (I == MBB.begin()) return 1;
384 if (!isCondBranchOpcode(I->getOpcode()))
387 // Remove the branch.
388 I->eraseFromParent();
393 ARMBaseInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
394 MachineBasicBlock *FBB,
395 const SmallVectorImpl<MachineOperand> &Cond,
397 ARMFunctionInfo *AFI = MBB.getParent()->getInfo<ARMFunctionInfo>();
398 int BOpc = !AFI->isThumbFunction()
399 ? ARM::B : (AFI->isThumb2Function() ? ARM::t2B : ARM::tB);
400 int BccOpc = !AFI->isThumbFunction()
401 ? ARM::Bcc : (AFI->isThumb2Function() ? ARM::t2Bcc : ARM::tBcc);
403 // Shouldn't be a fall through.
404 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
405 assert((Cond.size() == 2 || Cond.size() == 0) &&
406 "ARM branch conditions have two components!");
409 if (Cond.empty()) // Unconditional branch?
410 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB);
412 BuildMI(&MBB, DL, get(BccOpc)).addMBB(TBB)
413 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg());
417 // Two-way conditional branch.
418 BuildMI(&MBB, DL, get(BccOpc)).addMBB(TBB)
419 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg());
420 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB);
424 bool ARMBaseInstrInfo::
425 ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
426 ARMCC::CondCodes CC = (ARMCC::CondCodes)(int)Cond[0].getImm();
427 Cond[0].setImm(ARMCC::getOppositeCondition(CC));
431 bool ARMBaseInstrInfo::
432 PredicateInstruction(MachineInstr *MI,
433 const SmallVectorImpl<MachineOperand> &Pred) const {
434 unsigned Opc = MI->getOpcode();
435 if (isUncondBranchOpcode(Opc)) {
436 MI->setDesc(get(getMatchingCondBranchOpcode(Opc)));
437 MI->addOperand(MachineOperand::CreateImm(Pred[0].getImm()));
438 MI->addOperand(MachineOperand::CreateReg(Pred[1].getReg(), false));
442 int PIdx = MI->findFirstPredOperandIdx();
444 MachineOperand &PMO = MI->getOperand(PIdx);
445 PMO.setImm(Pred[0].getImm());
446 MI->getOperand(PIdx+1).setReg(Pred[1].getReg());
452 bool ARMBaseInstrInfo::
453 SubsumesPredicate(const SmallVectorImpl<MachineOperand> &Pred1,
454 const SmallVectorImpl<MachineOperand> &Pred2) const {
455 if (Pred1.size() > 2 || Pred2.size() > 2)
458 ARMCC::CondCodes CC1 = (ARMCC::CondCodes)Pred1[0].getImm();
459 ARMCC::CondCodes CC2 = (ARMCC::CondCodes)Pred2[0].getImm();
469 return CC2 == ARMCC::HI;
471 return CC2 == ARMCC::LO || CC2 == ARMCC::EQ;
473 return CC2 == ARMCC::GT;
475 return CC2 == ARMCC::LT;
479 bool ARMBaseInstrInfo::DefinesPredicate(MachineInstr *MI,
480 std::vector<MachineOperand> &Pred) const {
481 // FIXME: This confuses implicit_def with optional CPSR def.
482 const MCInstrDesc &MCID = MI->getDesc();
483 if (!MCID.getImplicitDefs() && !MCID.hasOptionalDef())
487 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
488 const MachineOperand &MO = MI->getOperand(i);
489 if (MO.isReg() && MO.getReg() == ARM::CPSR) {
498 /// isPredicable - Return true if the specified instruction can be predicated.
499 /// By default, this returns true for every instruction with a
500 /// PredicateOperand.
501 bool ARMBaseInstrInfo::isPredicable(MachineInstr *MI) const {
502 const MCInstrDesc &MCID = MI->getDesc();
503 if (!MCID.isPredicable())
506 if ((MCID.TSFlags & ARMII::DomainMask) == ARMII::DomainNEON) {
507 ARMFunctionInfo *AFI =
508 MI->getParent()->getParent()->getInfo<ARMFunctionInfo>();
509 return AFI->isThumb2Function();
514 /// FIXME: Works around a gcc miscompilation with -fstrict-aliasing.
515 LLVM_ATTRIBUTE_NOINLINE
516 static unsigned getNumJTEntries(const std::vector<MachineJumpTableEntry> &JT,
518 static unsigned getNumJTEntries(const std::vector<MachineJumpTableEntry> &JT,
520 assert(JTI < JT.size());
521 return JT[JTI].MBBs.size();
524 /// GetInstSize - Return the size of the specified MachineInstr.
526 unsigned ARMBaseInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
527 const MachineBasicBlock &MBB = *MI->getParent();
528 const MachineFunction *MF = MBB.getParent();
529 const MCAsmInfo *MAI = MF->getTarget().getMCAsmInfo();
531 // Basic size info comes from the TSFlags field.
532 const MCInstrDesc &MCID = MI->getDesc();
533 uint64_t TSFlags = MCID.TSFlags;
535 unsigned Opc = MI->getOpcode();
536 switch ((TSFlags & ARMII::SizeMask) >> ARMII::SizeShift) {
538 // If this machine instr is an inline asm, measure it.
539 if (MI->getOpcode() == ARM::INLINEASM)
540 return getInlineAsmLength(MI->getOperand(0).getSymbolName(), *MAI);
545 llvm_unreachable("Unknown or unset size field for instr!");
546 case TargetOpcode::IMPLICIT_DEF:
547 case TargetOpcode::KILL:
548 case TargetOpcode::PROLOG_LABEL:
549 case TargetOpcode::EH_LABEL:
550 case TargetOpcode::DBG_VALUE:
555 case ARMII::Size8Bytes: return 8; // ARM instruction x 2.
556 case ARMII::Size4Bytes: return 4; // ARM / Thumb2 instruction.
557 case ARMII::Size2Bytes: return 2; // Thumb1 instruction.
558 case ARMII::SizeSpecial: {
560 case ARM::MOVi16_ga_pcrel:
561 case ARM::MOVTi16_ga_pcrel:
562 case ARM::t2MOVi16_ga_pcrel:
563 case ARM::t2MOVTi16_ga_pcrel:
566 case ARM::t2MOVi32imm:
568 case ARM::CONSTPOOL_ENTRY:
569 // If this machine instr is a constant pool entry, its size is recorded as
571 return MI->getOperand(2).getImm();
572 case ARM::Int_eh_sjlj_longjmp:
574 case ARM::tInt_eh_sjlj_longjmp:
576 case ARM::Int_eh_sjlj_setjmp:
577 case ARM::Int_eh_sjlj_setjmp_nofp:
579 case ARM::tInt_eh_sjlj_setjmp:
580 case ARM::t2Int_eh_sjlj_setjmp:
581 case ARM::t2Int_eh_sjlj_setjmp_nofp:
589 case ARM::t2TBH_JT: {
590 // These are jumptable branches, i.e. a branch followed by an inlined
591 // jumptable. The size is 4 + 4 * number of entries. For TBB, each
592 // entry is one byte; TBH two byte each.
593 unsigned EntrySize = (Opc == ARM::t2TBB_JT)
594 ? 1 : ((Opc == ARM::t2TBH_JT) ? 2 : 4);
595 unsigned NumOps = MCID.getNumOperands();
596 MachineOperand JTOP =
597 MI->getOperand(NumOps - (MCID.isPredicable() ? 3 : 2));
598 unsigned JTI = JTOP.getIndex();
599 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
601 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
602 assert(JTI < JT.size());
603 // Thumb instructions are 2 byte aligned, but JT entries are 4 byte
604 // 4 aligned. The assembler / linker may add 2 byte padding just before
605 // the JT entries. The size does not include this padding; the
606 // constant islands pass does separate bookkeeping for it.
607 // FIXME: If we know the size of the function is less than (1 << 16) *2
608 // bytes, we can use 16-bit entries instead. Then there won't be an
610 unsigned InstSize = (Opc == ARM::tBR_JTr || Opc == ARM::t2BR_JT) ? 2 : 4;
611 unsigned NumEntries = getNumJTEntries(JT, JTI);
612 if (Opc == ARM::t2TBB_JT && (NumEntries & 1))
613 // Make sure the instruction that follows TBB is 2-byte aligned.
614 // FIXME: Constant island pass should insert an "ALIGN" instruction
617 return NumEntries * EntrySize + InstSize;
620 // Otherwise, pseudo-instruction sizes are zero.
625 return 0; // Not reached
628 void ARMBaseInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
629 MachineBasicBlock::iterator I, DebugLoc DL,
630 unsigned DestReg, unsigned SrcReg,
631 bool KillSrc) const {
632 bool GPRDest = ARM::GPRRegClass.contains(DestReg);
633 bool GPRSrc = ARM::GPRRegClass.contains(SrcReg);
635 if (GPRDest && GPRSrc) {
636 AddDefaultCC(AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::MOVr), DestReg)
637 .addReg(SrcReg, getKillRegState(KillSrc))));
641 bool SPRDest = ARM::SPRRegClass.contains(DestReg);
642 bool SPRSrc = ARM::SPRRegClass.contains(SrcReg);
645 if (SPRDest && SPRSrc)
647 else if (GPRDest && SPRSrc)
649 else if (SPRDest && GPRSrc)
651 else if (ARM::DPRRegClass.contains(DestReg, SrcReg))
653 else if (ARM::QPRRegClass.contains(DestReg, SrcReg))
655 else if (ARM::QQPRRegClass.contains(DestReg, SrcReg))
657 else if (ARM::QQQQPRRegClass.contains(DestReg, SrcReg))
660 llvm_unreachable("Impossible reg-to-reg copy");
662 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(Opc), DestReg);
663 MIB.addReg(SrcReg, getKillRegState(KillSrc));
664 if (Opc != ARM::VMOVQQ && Opc != ARM::VMOVQQQQ)
669 MachineInstrBuilder &AddDReg(MachineInstrBuilder &MIB,
670 unsigned Reg, unsigned SubIdx, unsigned State,
671 const TargetRegisterInfo *TRI) {
673 return MIB.addReg(Reg, State);
675 if (TargetRegisterInfo::isPhysicalRegister(Reg))
676 return MIB.addReg(TRI->getSubReg(Reg, SubIdx), State);
677 return MIB.addReg(Reg, State, SubIdx);
680 void ARMBaseInstrInfo::
681 storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
682 unsigned SrcReg, bool isKill, int FI,
683 const TargetRegisterClass *RC,
684 const TargetRegisterInfo *TRI) const {
686 if (I != MBB.end()) DL = I->getDebugLoc();
687 MachineFunction &MF = *MBB.getParent();
688 MachineFrameInfo &MFI = *MF.getFrameInfo();
689 unsigned Align = MFI.getObjectAlignment(FI);
691 MachineMemOperand *MMO =
692 MF.getMachineMemOperand(MachinePointerInfo(
693 PseudoSourceValue::getFixedStack(FI)),
694 MachineMemOperand::MOStore,
695 MFI.getObjectSize(FI),
698 // tGPR is used sometimes in ARM instructions that need to avoid using
699 // certain registers. Just treat it as GPR here. Likewise, rGPR.
700 if (RC == ARM::tGPRRegisterClass || RC == ARM::tcGPRRegisterClass
701 || RC == ARM::rGPRRegisterClass)
702 RC = ARM::GPRRegisterClass;
704 switch (RC->getID()) {
705 case ARM::GPRRegClassID:
706 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::STRi12))
707 .addReg(SrcReg, getKillRegState(isKill))
708 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
710 case ARM::SPRRegClassID:
711 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTRS))
712 .addReg(SrcReg, getKillRegState(isKill))
713 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
715 case ARM::DPRRegClassID:
716 case ARM::DPR_VFP2RegClassID:
717 case ARM::DPR_8RegClassID:
718 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTRD))
719 .addReg(SrcReg, getKillRegState(isKill))
720 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
722 case ARM::QPRRegClassID:
723 case ARM::QPR_VFP2RegClassID:
724 case ARM::QPR_8RegClassID:
725 if (Align >= 16 && getRegisterInfo().needsStackRealignment(MF)) {
726 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VST1q64Pseudo))
727 .addFrameIndex(FI).addImm(16)
728 .addReg(SrcReg, getKillRegState(isKill))
729 .addMemOperand(MMO));
731 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMQIA))
732 .addReg(SrcReg, getKillRegState(isKill))
734 .addMemOperand(MMO));
737 case ARM::QQPRRegClassID:
738 case ARM::QQPR_VFP2RegClassID:
739 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
740 // FIXME: It's possible to only store part of the QQ register if the
741 // spilled def has a sub-register index.
742 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VST1d64QPseudo))
743 .addFrameIndex(FI).addImm(16)
744 .addReg(SrcReg, getKillRegState(isKill))
745 .addMemOperand(MMO));
747 MachineInstrBuilder MIB =
748 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMDIA))
751 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
752 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
753 MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
754 AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI);
757 case ARM::QQQQPRRegClassID: {
758 MachineInstrBuilder MIB =
759 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMDIA))
762 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
763 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
764 MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
765 MIB = AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI);
766 MIB = AddDReg(MIB, SrcReg, ARM::dsub_4, 0, TRI);
767 MIB = AddDReg(MIB, SrcReg, ARM::dsub_5, 0, TRI);
768 MIB = AddDReg(MIB, SrcReg, ARM::dsub_6, 0, TRI);
769 AddDReg(MIB, SrcReg, ARM::dsub_7, 0, TRI);
773 llvm_unreachable("Unknown regclass!");
778 ARMBaseInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
779 int &FrameIndex) const {
780 switch (MI->getOpcode()) {
783 case ARM::t2STRs: // FIXME: don't use t2STRs to access frame.
784 if (MI->getOperand(1).isFI() &&
785 MI->getOperand(2).isReg() &&
786 MI->getOperand(3).isImm() &&
787 MI->getOperand(2).getReg() == 0 &&
788 MI->getOperand(3).getImm() == 0) {
789 FrameIndex = MI->getOperand(1).getIndex();
790 return MI->getOperand(0).getReg();
798 if (MI->getOperand(1).isFI() &&
799 MI->getOperand(2).isImm() &&
800 MI->getOperand(2).getImm() == 0) {
801 FrameIndex = MI->getOperand(1).getIndex();
802 return MI->getOperand(0).getReg();
805 case ARM::VST1q64Pseudo:
806 if (MI->getOperand(0).isFI() &&
807 MI->getOperand(2).getSubReg() == 0) {
808 FrameIndex = MI->getOperand(0).getIndex();
809 return MI->getOperand(2).getReg();
813 if (MI->getOperand(1).isFI() &&
814 MI->getOperand(0).getSubReg() == 0) {
815 FrameIndex = MI->getOperand(1).getIndex();
816 return MI->getOperand(0).getReg();
824 void ARMBaseInstrInfo::
825 loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
826 unsigned DestReg, int FI,
827 const TargetRegisterClass *RC,
828 const TargetRegisterInfo *TRI) const {
830 if (I != MBB.end()) DL = I->getDebugLoc();
831 MachineFunction &MF = *MBB.getParent();
832 MachineFrameInfo &MFI = *MF.getFrameInfo();
833 unsigned Align = MFI.getObjectAlignment(FI);
834 MachineMemOperand *MMO =
835 MF.getMachineMemOperand(
836 MachinePointerInfo(PseudoSourceValue::getFixedStack(FI)),
837 MachineMemOperand::MOLoad,
838 MFI.getObjectSize(FI),
841 // tGPR is used sometimes in ARM instructions that need to avoid using
842 // certain registers. Just treat it as GPR here.
843 if (RC == ARM::tGPRRegisterClass || RC == ARM::tcGPRRegisterClass
844 || RC == ARM::rGPRRegisterClass)
845 RC = ARM::GPRRegisterClass;
847 switch (RC->getID()) {
848 case ARM::GPRRegClassID:
849 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::LDRi12), DestReg)
850 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
852 case ARM::SPRRegClassID:
853 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDRS), DestReg)
854 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
856 case ARM::DPRRegClassID:
857 case ARM::DPR_VFP2RegClassID:
858 case ARM::DPR_8RegClassID:
859 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDRD), DestReg)
860 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
862 case ARM::QPRRegClassID:
863 case ARM::QPR_VFP2RegClassID:
864 case ARM::QPR_8RegClassID:
865 if (Align >= 16 && getRegisterInfo().needsStackRealignment(MF)) {
866 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLD1q64Pseudo), DestReg)
867 .addFrameIndex(FI).addImm(16)
868 .addMemOperand(MMO));
870 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMQIA), DestReg)
872 .addMemOperand(MMO));
875 case ARM::QQPRRegClassID:
876 case ARM::QQPR_VFP2RegClassID:
877 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
878 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLD1d64QPseudo), DestReg)
879 .addFrameIndex(FI).addImm(16)
880 .addMemOperand(MMO));
882 MachineInstrBuilder MIB =
883 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
886 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::Define, TRI);
887 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::Define, TRI);
888 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::Define, TRI);
889 AddDReg(MIB, DestReg, ARM::dsub_3, RegState::Define, TRI);
892 case ARM::QQQQPRRegClassID: {
893 MachineInstrBuilder MIB =
894 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
897 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::Define, TRI);
898 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::Define, TRI);
899 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::Define, TRI);
900 MIB = AddDReg(MIB, DestReg, ARM::dsub_3, RegState::Define, TRI);
901 MIB = AddDReg(MIB, DestReg, ARM::dsub_4, RegState::Define, TRI);
902 MIB = AddDReg(MIB, DestReg, ARM::dsub_5, RegState::Define, TRI);
903 MIB = AddDReg(MIB, DestReg, ARM::dsub_6, RegState::Define, TRI);
904 AddDReg(MIB, DestReg, ARM::dsub_7, RegState::Define, TRI);
908 llvm_unreachable("Unknown regclass!");
913 ARMBaseInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
914 int &FrameIndex) const {
915 switch (MI->getOpcode()) {
918 case ARM::t2LDRs: // FIXME: don't use t2LDRs to access frame.
919 if (MI->getOperand(1).isFI() &&
920 MI->getOperand(2).isReg() &&
921 MI->getOperand(3).isImm() &&
922 MI->getOperand(2).getReg() == 0 &&
923 MI->getOperand(3).getImm() == 0) {
924 FrameIndex = MI->getOperand(1).getIndex();
925 return MI->getOperand(0).getReg();
933 if (MI->getOperand(1).isFI() &&
934 MI->getOperand(2).isImm() &&
935 MI->getOperand(2).getImm() == 0) {
936 FrameIndex = MI->getOperand(1).getIndex();
937 return MI->getOperand(0).getReg();
940 case ARM::VLD1q64Pseudo:
941 if (MI->getOperand(1).isFI() &&
942 MI->getOperand(0).getSubReg() == 0) {
943 FrameIndex = MI->getOperand(1).getIndex();
944 return MI->getOperand(0).getReg();
948 if (MI->getOperand(1).isFI() &&
949 MI->getOperand(0).getSubReg() == 0) {
950 FrameIndex = MI->getOperand(1).getIndex();
951 return MI->getOperand(0).getReg();
960 ARMBaseInstrInfo::emitFrameIndexDebugValue(MachineFunction &MF,
961 int FrameIx, uint64_t Offset,
964 MachineInstrBuilder MIB = BuildMI(MF, DL, get(ARM::DBG_VALUE))
965 .addFrameIndex(FrameIx).addImm(0).addImm(Offset).addMetadata(MDPtr);
969 /// Create a copy of a const pool value. Update CPI to the new index and return
971 static unsigned duplicateCPV(MachineFunction &MF, unsigned &CPI) {
972 MachineConstantPool *MCP = MF.getConstantPool();
973 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
975 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPI];
976 assert(MCPE.isMachineConstantPoolEntry() &&
977 "Expecting a machine constantpool entry!");
978 ARMConstantPoolValue *ACPV =
979 static_cast<ARMConstantPoolValue*>(MCPE.Val.MachineCPVal);
981 unsigned PCLabelId = AFI->createPICLabelUId();
982 ARMConstantPoolValue *NewCPV = 0;
983 // FIXME: The below assumes PIC relocation model and that the function
984 // is Thumb mode (t1 or t2). PCAdjustment would be 8 for ARM mode PIC, and
985 // zero for non-PIC in ARM or Thumb. The callers are all of thumb LDR
986 // instructions, so that's probably OK, but is PIC always correct when
988 if (ACPV->isGlobalValue())
989 NewCPV = new ARMConstantPoolValue(ACPV->getGV(), PCLabelId,
991 else if (ACPV->isExtSymbol())
992 NewCPV = new ARMConstantPoolValue(MF.getFunction()->getContext(),
993 ACPV->getSymbol(), PCLabelId, 4);
994 else if (ACPV->isBlockAddress())
995 NewCPV = new ARMConstantPoolValue(ACPV->getBlockAddress(), PCLabelId,
996 ARMCP::CPBlockAddress, 4);
997 else if (ACPV->isLSDA())
998 NewCPV = new ARMConstantPoolValue(MF.getFunction(), PCLabelId,
1001 llvm_unreachable("Unexpected ARM constantpool value type!!");
1002 CPI = MCP->getConstantPoolIndex(NewCPV, MCPE.getAlignment());
1006 void ARMBaseInstrInfo::
1007 reMaterialize(MachineBasicBlock &MBB,
1008 MachineBasicBlock::iterator I,
1009 unsigned DestReg, unsigned SubIdx,
1010 const MachineInstr *Orig,
1011 const TargetRegisterInfo &TRI) const {
1012 unsigned Opcode = Orig->getOpcode();
1015 MachineInstr *MI = MBB.getParent()->CloneMachineInstr(Orig);
1016 MI->substituteRegister(Orig->getOperand(0).getReg(), DestReg, SubIdx, TRI);
1020 case ARM::tLDRpci_pic:
1021 case ARM::t2LDRpci_pic: {
1022 MachineFunction &MF = *MBB.getParent();
1023 unsigned CPI = Orig->getOperand(1).getIndex();
1024 unsigned PCLabelId = duplicateCPV(MF, CPI);
1025 MachineInstrBuilder MIB = BuildMI(MBB, I, Orig->getDebugLoc(), get(Opcode),
1027 .addConstantPoolIndex(CPI).addImm(PCLabelId);
1028 MIB->setMemRefs(Orig->memoperands_begin(), Orig->memoperands_end());
1035 ARMBaseInstrInfo::duplicate(MachineInstr *Orig, MachineFunction &MF) const {
1036 MachineInstr *MI = TargetInstrInfoImpl::duplicate(Orig, MF);
1037 switch(Orig->getOpcode()) {
1038 case ARM::tLDRpci_pic:
1039 case ARM::t2LDRpci_pic: {
1040 unsigned CPI = Orig->getOperand(1).getIndex();
1041 unsigned PCLabelId = duplicateCPV(MF, CPI);
1042 Orig->getOperand(1).setIndex(CPI);
1043 Orig->getOperand(2).setImm(PCLabelId);
1050 bool ARMBaseInstrInfo::produceSameValue(const MachineInstr *MI0,
1051 const MachineInstr *MI1,
1052 const MachineRegisterInfo *MRI) const {
1053 int Opcode = MI0->getOpcode();
1054 if (Opcode == ARM::t2LDRpci ||
1055 Opcode == ARM::t2LDRpci_pic ||
1056 Opcode == ARM::tLDRpci ||
1057 Opcode == ARM::tLDRpci_pic ||
1058 Opcode == ARM::MOV_ga_dyn ||
1059 Opcode == ARM::MOV_ga_pcrel ||
1060 Opcode == ARM::MOV_ga_pcrel_ldr ||
1061 Opcode == ARM::t2MOV_ga_dyn ||
1062 Opcode == ARM::t2MOV_ga_pcrel) {
1063 if (MI1->getOpcode() != Opcode)
1065 if (MI0->getNumOperands() != MI1->getNumOperands())
1068 const MachineOperand &MO0 = MI0->getOperand(1);
1069 const MachineOperand &MO1 = MI1->getOperand(1);
1070 if (MO0.getOffset() != MO1.getOffset())
1073 if (Opcode == ARM::MOV_ga_dyn ||
1074 Opcode == ARM::MOV_ga_pcrel ||
1075 Opcode == ARM::MOV_ga_pcrel_ldr ||
1076 Opcode == ARM::t2MOV_ga_dyn ||
1077 Opcode == ARM::t2MOV_ga_pcrel)
1078 // Ignore the PC labels.
1079 return MO0.getGlobal() == MO1.getGlobal();
1081 const MachineFunction *MF = MI0->getParent()->getParent();
1082 const MachineConstantPool *MCP = MF->getConstantPool();
1083 int CPI0 = MO0.getIndex();
1084 int CPI1 = MO1.getIndex();
1085 const MachineConstantPoolEntry &MCPE0 = MCP->getConstants()[CPI0];
1086 const MachineConstantPoolEntry &MCPE1 = MCP->getConstants()[CPI1];
1087 bool isARMCP0 = MCPE0.isMachineConstantPoolEntry();
1088 bool isARMCP1 = MCPE1.isMachineConstantPoolEntry();
1089 if (isARMCP0 && isARMCP1) {
1090 ARMConstantPoolValue *ACPV0 =
1091 static_cast<ARMConstantPoolValue*>(MCPE0.Val.MachineCPVal);
1092 ARMConstantPoolValue *ACPV1 =
1093 static_cast<ARMConstantPoolValue*>(MCPE1.Val.MachineCPVal);
1094 return ACPV0->hasSameValue(ACPV1);
1095 } else if (!isARMCP0 && !isARMCP1) {
1096 return MCPE0.Val.ConstVal == MCPE1.Val.ConstVal;
1099 } else if (Opcode == ARM::PICLDR) {
1100 if (MI1->getOpcode() != Opcode)
1102 if (MI0->getNumOperands() != MI1->getNumOperands())
1105 unsigned Addr0 = MI0->getOperand(1).getReg();
1106 unsigned Addr1 = MI1->getOperand(1).getReg();
1107 if (Addr0 != Addr1) {
1109 !TargetRegisterInfo::isVirtualRegister(Addr0) ||
1110 !TargetRegisterInfo::isVirtualRegister(Addr1))
1113 // This assumes SSA form.
1114 MachineInstr *Def0 = MRI->getVRegDef(Addr0);
1115 MachineInstr *Def1 = MRI->getVRegDef(Addr1);
1116 // Check if the loaded value, e.g. a constantpool of a global address, are
1118 if (!produceSameValue(Def0, Def1, MRI))
1122 for (unsigned i = 3, e = MI0->getNumOperands(); i != e; ++i) {
1123 // %vreg12<def> = PICLDR %vreg11, 0, pred:14, pred:%noreg
1124 const MachineOperand &MO0 = MI0->getOperand(i);
1125 const MachineOperand &MO1 = MI1->getOperand(i);
1126 if (!MO0.isIdenticalTo(MO1))
1132 return MI0->isIdenticalTo(MI1, MachineInstr::IgnoreVRegDefs);
1135 /// areLoadsFromSameBasePtr - This is used by the pre-regalloc scheduler to
1136 /// determine if two loads are loading from the same base address. It should
1137 /// only return true if the base pointers are the same and the only differences
1138 /// between the two addresses is the offset. It also returns the offsets by
1140 bool ARMBaseInstrInfo::areLoadsFromSameBasePtr(SDNode *Load1, SDNode *Load2,
1142 int64_t &Offset2) const {
1143 // Don't worry about Thumb: just ARM and Thumb2.
1144 if (Subtarget.isThumb1Only()) return false;
1146 if (!Load1->isMachineOpcode() || !Load2->isMachineOpcode())
1149 switch (Load1->getMachineOpcode()) {
1162 case ARM::t2LDRSHi8:
1164 case ARM::t2LDRSHi12:
1168 switch (Load2->getMachineOpcode()) {
1181 case ARM::t2LDRSHi8:
1183 case ARM::t2LDRSHi12:
1187 // Check if base addresses and chain operands match.
1188 if (Load1->getOperand(0) != Load2->getOperand(0) ||
1189 Load1->getOperand(4) != Load2->getOperand(4))
1192 // Index should be Reg0.
1193 if (Load1->getOperand(3) != Load2->getOperand(3))
1196 // Determine the offsets.
1197 if (isa<ConstantSDNode>(Load1->getOperand(1)) &&
1198 isa<ConstantSDNode>(Load2->getOperand(1))) {
1199 Offset1 = cast<ConstantSDNode>(Load1->getOperand(1))->getSExtValue();
1200 Offset2 = cast<ConstantSDNode>(Load2->getOperand(1))->getSExtValue();
1207 /// shouldScheduleLoadsNear - This is a used by the pre-regalloc scheduler to
1208 /// determine (in conjunction with areLoadsFromSameBasePtr) if two loads should
1209 /// be scheduled togther. On some targets if two loads are loading from
1210 /// addresses in the same cache line, it's better if they are scheduled
1211 /// together. This function takes two integers that represent the load offsets
1212 /// from the common base address. It returns true if it decides it's desirable
1213 /// to schedule the two loads together. "NumLoads" is the number of loads that
1214 /// have already been scheduled after Load1.
1215 bool ARMBaseInstrInfo::shouldScheduleLoadsNear(SDNode *Load1, SDNode *Load2,
1216 int64_t Offset1, int64_t Offset2,
1217 unsigned NumLoads) const {
1218 // Don't worry about Thumb: just ARM and Thumb2.
1219 if (Subtarget.isThumb1Only()) return false;
1221 assert(Offset2 > Offset1);
1223 if ((Offset2 - Offset1) / 8 > 64)
1226 if (Load1->getMachineOpcode() != Load2->getMachineOpcode())
1227 return false; // FIXME: overly conservative?
1229 // Four loads in a row should be sufficient.
1236 bool ARMBaseInstrInfo::isSchedulingBoundary(const MachineInstr *MI,
1237 const MachineBasicBlock *MBB,
1238 const MachineFunction &MF) const {
1239 // Debug info is never a scheduling boundary. It's necessary to be explicit
1240 // due to the special treatment of IT instructions below, otherwise a
1241 // dbg_value followed by an IT will result in the IT instruction being
1242 // considered a scheduling hazard, which is wrong. It should be the actual
1243 // instruction preceding the dbg_value instruction(s), just like it is
1244 // when debug info is not present.
1245 if (MI->isDebugValue())
1248 // Terminators and labels can't be scheduled around.
1249 if (MI->getDesc().isTerminator() || MI->isLabel())
1252 // Treat the start of the IT block as a scheduling boundary, but schedule
1253 // t2IT along with all instructions following it.
1254 // FIXME: This is a big hammer. But the alternative is to add all potential
1255 // true and anti dependencies to IT block instructions as implicit operands
1256 // to the t2IT instruction. The added compile time and complexity does not
1258 MachineBasicBlock::const_iterator I = MI;
1259 // Make sure to skip any dbg_value instructions
1260 while (++I != MBB->end() && I->isDebugValue())
1262 if (I != MBB->end() && I->getOpcode() == ARM::t2IT)
1265 // Don't attempt to schedule around any instruction that defines
1266 // a stack-oriented pointer, as it's unlikely to be profitable. This
1267 // saves compile time, because it doesn't require every single
1268 // stack slot reference to depend on the instruction that does the
1270 if (MI->definesRegister(ARM::SP))
1276 bool ARMBaseInstrInfo::isProfitableToIfCvt(MachineBasicBlock &MBB,
1278 unsigned ExtraPredCycles,
1280 float Confidence) const {
1284 // Attempt to estimate the relative costs of predication versus branching.
1285 float UnpredCost = Probability * NumCycles;
1286 UnpredCost += 1.0; // The branch itself
1287 UnpredCost += (1.0 - Confidence) * Subtarget.getMispredictionPenalty();
1289 return (float)(NumCycles + ExtraPredCycles) < UnpredCost;
1292 bool ARMBaseInstrInfo::
1293 isProfitableToIfCvt(MachineBasicBlock &TMBB,
1294 unsigned TCycles, unsigned TExtra,
1295 MachineBasicBlock &FMBB,
1296 unsigned FCycles, unsigned FExtra,
1297 float Probability, float Confidence) const {
1298 if (!TCycles || !FCycles)
1301 // Attempt to estimate the relative costs of predication versus branching.
1302 float UnpredCost = Probability * TCycles + (1.0 - Probability) * FCycles;
1303 UnpredCost += 1.0; // The branch itself
1304 UnpredCost += (1.0 - Confidence) * Subtarget.getMispredictionPenalty();
1306 return (float)(TCycles + FCycles + TExtra + FExtra) < UnpredCost;
1309 /// getInstrPredicate - If instruction is predicated, returns its predicate
1310 /// condition, otherwise returns AL. It also returns the condition code
1311 /// register by reference.
1313 llvm::getInstrPredicate(const MachineInstr *MI, unsigned &PredReg) {
1314 int PIdx = MI->findFirstPredOperandIdx();
1320 PredReg = MI->getOperand(PIdx+1).getReg();
1321 return (ARMCC::CondCodes)MI->getOperand(PIdx).getImm();
1325 int llvm::getMatchingCondBranchOpcode(int Opc) {
1328 else if (Opc == ARM::tB)
1330 else if (Opc == ARM::t2B)
1333 llvm_unreachable("Unknown unconditional branch opcode!");
1338 void llvm::emitARMRegPlusImmediate(MachineBasicBlock &MBB,
1339 MachineBasicBlock::iterator &MBBI, DebugLoc dl,
1340 unsigned DestReg, unsigned BaseReg, int NumBytes,
1341 ARMCC::CondCodes Pred, unsigned PredReg,
1342 const ARMBaseInstrInfo &TII, unsigned MIFlags) {
1343 bool isSub = NumBytes < 0;
1344 if (isSub) NumBytes = -NumBytes;
1347 unsigned RotAmt = ARM_AM::getSOImmValRotate(NumBytes);
1348 unsigned ThisVal = NumBytes & ARM_AM::rotr32(0xFF, RotAmt);
1349 assert(ThisVal && "Didn't extract field correctly");
1351 // We will handle these bits from offset, clear them.
1352 NumBytes &= ~ThisVal;
1354 assert(ARM_AM::getSOImmVal(ThisVal) != -1 && "Bit extraction didn't work?");
1356 // Build the new ADD / SUB.
1357 unsigned Opc = isSub ? ARM::SUBri : ARM::ADDri;
1358 BuildMI(MBB, MBBI, dl, TII.get(Opc), DestReg)
1359 .addReg(BaseReg, RegState::Kill).addImm(ThisVal)
1360 .addImm((unsigned)Pred).addReg(PredReg).addReg(0)
1361 .setMIFlags(MIFlags);
1366 bool llvm::rewriteARMFrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
1367 unsigned FrameReg, int &Offset,
1368 const ARMBaseInstrInfo &TII) {
1369 unsigned Opcode = MI.getOpcode();
1370 const MCInstrDesc &Desc = MI.getDesc();
1371 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
1374 // Memory operands in inline assembly always use AddrMode2.
1375 if (Opcode == ARM::INLINEASM)
1376 AddrMode = ARMII::AddrMode2;
1378 if (Opcode == ARM::ADDri) {
1379 Offset += MI.getOperand(FrameRegIdx+1).getImm();
1381 // Turn it into a move.
1382 MI.setDesc(TII.get(ARM::MOVr));
1383 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
1384 MI.RemoveOperand(FrameRegIdx+1);
1387 } else if (Offset < 0) {
1390 MI.setDesc(TII.get(ARM::SUBri));
1393 // Common case: small offset, fits into instruction.
1394 if (ARM_AM::getSOImmVal(Offset) != -1) {
1395 // Replace the FrameIndex with sp / fp
1396 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
1397 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(Offset);
1402 // Otherwise, pull as much of the immedidate into this ADDri/SUBri
1404 unsigned RotAmt = ARM_AM::getSOImmValRotate(Offset);
1405 unsigned ThisImmVal = Offset & ARM_AM::rotr32(0xFF, RotAmt);
1407 // We will handle these bits from offset, clear them.
1408 Offset &= ~ThisImmVal;
1410 // Get the properly encoded SOImmVal field.
1411 assert(ARM_AM::getSOImmVal(ThisImmVal) != -1 &&
1412 "Bit extraction didn't work?");
1413 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(ThisImmVal);
1415 unsigned ImmIdx = 0;
1417 unsigned NumBits = 0;
1420 case ARMII::AddrMode_i12: {
1421 ImmIdx = FrameRegIdx + 1;
1422 InstrOffs = MI.getOperand(ImmIdx).getImm();
1426 case ARMII::AddrMode2: {
1427 ImmIdx = FrameRegIdx+2;
1428 InstrOffs = ARM_AM::getAM2Offset(MI.getOperand(ImmIdx).getImm());
1429 if (ARM_AM::getAM2Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
1434 case ARMII::AddrMode3: {
1435 ImmIdx = FrameRegIdx+2;
1436 InstrOffs = ARM_AM::getAM3Offset(MI.getOperand(ImmIdx).getImm());
1437 if (ARM_AM::getAM3Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
1442 case ARMII::AddrMode4:
1443 case ARMII::AddrMode6:
1444 // Can't fold any offset even if it's zero.
1446 case ARMII::AddrMode5: {
1447 ImmIdx = FrameRegIdx+1;
1448 InstrOffs = ARM_AM::getAM5Offset(MI.getOperand(ImmIdx).getImm());
1449 if (ARM_AM::getAM5Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
1456 llvm_unreachable("Unsupported addressing mode!");
1460 Offset += InstrOffs * Scale;
1461 assert((Offset & (Scale-1)) == 0 && "Can't encode this offset!");
1467 // Attempt to fold address comp. if opcode has offset bits
1469 // Common case: small offset, fits into instruction.
1470 MachineOperand &ImmOp = MI.getOperand(ImmIdx);
1471 int ImmedOffset = Offset / Scale;
1472 unsigned Mask = (1 << NumBits) - 1;
1473 if ((unsigned)Offset <= Mask * Scale) {
1474 // Replace the FrameIndex with sp
1475 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
1476 // FIXME: When addrmode2 goes away, this will simplify (like the
1477 // T2 version), as the LDR.i12 versions don't need the encoding
1478 // tricks for the offset value.
1480 if (AddrMode == ARMII::AddrMode_i12)
1481 ImmedOffset = -ImmedOffset;
1483 ImmedOffset |= 1 << NumBits;
1485 ImmOp.ChangeToImmediate(ImmedOffset);
1490 // Otherwise, it didn't fit. Pull in what we can to simplify the immed.
1491 ImmedOffset = ImmedOffset & Mask;
1493 if (AddrMode == ARMII::AddrMode_i12)
1494 ImmedOffset = -ImmedOffset;
1496 ImmedOffset |= 1 << NumBits;
1498 ImmOp.ChangeToImmediate(ImmedOffset);
1499 Offset &= ~(Mask*Scale);
1503 Offset = (isSub) ? -Offset : Offset;
1507 bool ARMBaseInstrInfo::
1508 AnalyzeCompare(const MachineInstr *MI, unsigned &SrcReg, int &CmpMask,
1509 int &CmpValue) const {
1510 switch (MI->getOpcode()) {
1514 SrcReg = MI->getOperand(0).getReg();
1516 CmpValue = MI->getOperand(1).getImm();
1520 SrcReg = MI->getOperand(0).getReg();
1521 CmpMask = MI->getOperand(1).getImm();
1529 /// isSuitableForMask - Identify a suitable 'and' instruction that
1530 /// operates on the given source register and applies the same mask
1531 /// as a 'tst' instruction. Provide a limited look-through for copies.
1532 /// When successful, MI will hold the found instruction.
1533 static bool isSuitableForMask(MachineInstr *&MI, unsigned SrcReg,
1534 int CmpMask, bool CommonUse) {
1535 switch (MI->getOpcode()) {
1538 if (CmpMask != MI->getOperand(2).getImm())
1540 if (SrcReg == MI->getOperand(CommonUse ? 1 : 0).getReg())
1544 // Walk down one instruction which is potentially an 'and'.
1545 const MachineInstr &Copy = *MI;
1546 MachineBasicBlock::iterator AND(
1547 llvm::next(MachineBasicBlock::iterator(MI)));
1548 if (AND == MI->getParent()->end()) return false;
1550 return isSuitableForMask(MI, Copy.getOperand(0).getReg(),
1558 /// OptimizeCompareInstr - Convert the instruction supplying the argument to the
1559 /// comparison into one that sets the zero bit in the flags register.
1560 bool ARMBaseInstrInfo::
1561 OptimizeCompareInstr(MachineInstr *CmpInstr, unsigned SrcReg, int CmpMask,
1562 int CmpValue, const MachineRegisterInfo *MRI) const {
1566 MachineRegisterInfo::def_iterator DI = MRI->def_begin(SrcReg);
1567 if (llvm::next(DI) != MRI->def_end())
1568 // Only support one definition.
1571 MachineInstr *MI = &*DI;
1573 // Masked compares sometimes use the same register as the corresponding 'and'.
1574 if (CmpMask != ~0) {
1575 if (!isSuitableForMask(MI, SrcReg, CmpMask, false)) {
1577 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(SrcReg),
1578 UE = MRI->use_end(); UI != UE; ++UI) {
1579 if (UI->getParent() != CmpInstr->getParent()) continue;
1580 MachineInstr *PotentialAND = &*UI;
1581 if (!isSuitableForMask(PotentialAND, SrcReg, CmpMask, true))
1586 if (!MI) return false;
1590 // Conservatively refuse to convert an instruction which isn't in the same BB
1591 // as the comparison.
1592 if (MI->getParent() != CmpInstr->getParent())
1595 // Check that CPSR isn't set between the comparison instruction and the one we
1597 MachineBasicBlock::const_iterator I = CmpInstr, E = MI,
1598 B = MI->getParent()->begin();
1600 // Early exit if CmpInstr is at the beginning of the BB.
1601 if (I == B) return false;
1604 for (; I != E; --I) {
1605 const MachineInstr &Instr = *I;
1607 for (unsigned IO = 0, EO = Instr.getNumOperands(); IO != EO; ++IO) {
1608 const MachineOperand &MO = Instr.getOperand(IO);
1609 if (!MO.isReg()) continue;
1611 // This instruction modifies or uses CPSR after the one we want to
1612 // change. We can't do this transformation.
1613 if (MO.getReg() == ARM::CPSR)
1618 // The 'and' is below the comparison instruction.
1622 // Set the "zero" bit in CPSR.
1623 switch (MI->getOpcode()) {
1657 case ARM::t2EORri: {
1658 // Scan forward for the use of CPSR, if it's a conditional code requires
1659 // checking of V bit, then this is not safe to do. If we can't find the
1660 // CPSR use (i.e. used in another block), then it's not safe to perform
1661 // the optimization.
1662 bool isSafe = false;
1664 E = MI->getParent()->end();
1665 while (!isSafe && ++I != E) {
1666 const MachineInstr &Instr = *I;
1667 for (unsigned IO = 0, EO = Instr.getNumOperands();
1668 !isSafe && IO != EO; ++IO) {
1669 const MachineOperand &MO = Instr.getOperand(IO);
1670 if (!MO.isReg() || MO.getReg() != ARM::CPSR)
1676 // Condition code is after the operand before CPSR.
1677 ARMCC::CondCodes CC = (ARMCC::CondCodes)Instr.getOperand(IO-1).getImm();
1696 // Toggle the optional operand to CPSR.
1697 MI->getOperand(5).setReg(ARM::CPSR);
1698 MI->getOperand(5).setIsDef(true);
1699 CmpInstr->eraseFromParent();
1707 bool ARMBaseInstrInfo::FoldImmediate(MachineInstr *UseMI,
1708 MachineInstr *DefMI, unsigned Reg,
1709 MachineRegisterInfo *MRI) const {
1710 // Fold large immediates into add, sub, or, xor.
1711 unsigned DefOpc = DefMI->getOpcode();
1712 if (DefOpc != ARM::t2MOVi32imm && DefOpc != ARM::MOVi32imm)
1714 if (!DefMI->getOperand(1).isImm())
1715 // Could be t2MOVi32imm <ga:xx>
1718 if (!MRI->hasOneNonDBGUse(Reg))
1721 unsigned UseOpc = UseMI->getOpcode();
1722 unsigned NewUseOpc = 0;
1723 uint32_t ImmVal = (uint32_t)DefMI->getOperand(1).getImm();
1724 uint32_t SOImmValV1 = 0, SOImmValV2 = 0;
1725 bool Commute = false;
1727 default: return false;
1735 case ARM::t2EORrr: {
1736 Commute = UseMI->getOperand(2).getReg() != Reg;
1743 NewUseOpc = ARM::SUBri;
1749 if (!ARM_AM::isSOImmTwoPartVal(ImmVal))
1751 SOImmValV1 = (uint32_t)ARM_AM::getSOImmTwoPartFirst(ImmVal);
1752 SOImmValV2 = (uint32_t)ARM_AM::getSOImmTwoPartSecond(ImmVal);
1755 case ARM::ADDrr: NewUseOpc = ARM::ADDri; break;
1756 case ARM::ORRrr: NewUseOpc = ARM::ORRri; break;
1757 case ARM::EORrr: NewUseOpc = ARM::EORri; break;
1761 case ARM::t2SUBrr: {
1765 NewUseOpc = ARM::t2SUBri;
1770 case ARM::t2EORrr: {
1771 if (!ARM_AM::isT2SOImmTwoPartVal(ImmVal))
1773 SOImmValV1 = (uint32_t)ARM_AM::getT2SOImmTwoPartFirst(ImmVal);
1774 SOImmValV2 = (uint32_t)ARM_AM::getT2SOImmTwoPartSecond(ImmVal);
1777 case ARM::t2ADDrr: NewUseOpc = ARM::t2ADDri; break;
1778 case ARM::t2ORRrr: NewUseOpc = ARM::t2ORRri; break;
1779 case ARM::t2EORrr: NewUseOpc = ARM::t2EORri; break;
1787 unsigned OpIdx = Commute ? 2 : 1;
1788 unsigned Reg1 = UseMI->getOperand(OpIdx).getReg();
1789 bool isKill = UseMI->getOperand(OpIdx).isKill();
1790 unsigned NewReg = MRI->createVirtualRegister(MRI->getRegClass(Reg));
1791 AddDefaultCC(AddDefaultPred(BuildMI(*UseMI->getParent(),
1792 *UseMI, UseMI->getDebugLoc(),
1793 get(NewUseOpc), NewReg)
1794 .addReg(Reg1, getKillRegState(isKill))
1795 .addImm(SOImmValV1)));
1796 UseMI->setDesc(get(NewUseOpc));
1797 UseMI->getOperand(1).setReg(NewReg);
1798 UseMI->getOperand(1).setIsKill();
1799 UseMI->getOperand(2).ChangeToImmediate(SOImmValV2);
1800 DefMI->eraseFromParent();
1805 ARMBaseInstrInfo::getNumMicroOps(const InstrItineraryData *ItinData,
1806 const MachineInstr *MI) const {
1807 if (!ItinData || ItinData->isEmpty())
1810 const MCInstrDesc &Desc = MI->getDesc();
1811 unsigned Class = Desc.getSchedClass();
1812 unsigned UOps = ItinData->Itineraries[Class].NumMicroOps;
1816 unsigned Opc = MI->getOpcode();
1819 llvm_unreachable("Unexpected multi-uops instruction!");
1825 // The number of uOps for load / store multiple are determined by the number
1828 // On Cortex-A8, each pair of register loads / stores can be scheduled on the
1829 // same cycle. The scheduling for the first load / store must be done
1830 // separately by assuming the the address is not 64-bit aligned.
1832 // On Cortex-A9, the formula is simply (#reg / 2) + (#reg % 2). If the address
1833 // is not 64-bit aligned, then AGU would take an extra cycle. For VFP / NEON
1834 // load / store multiple, the formula is (#reg / 2) + (#reg % 2) + 1.
1836 case ARM::VLDMDIA_UPD:
1837 case ARM::VLDMDDB_UPD:
1839 case ARM::VLDMSIA_UPD:
1840 case ARM::VLDMSDB_UPD:
1842 case ARM::VSTMDIA_UPD:
1843 case ARM::VSTMDDB_UPD:
1845 case ARM::VSTMSIA_UPD:
1846 case ARM::VSTMSDB_UPD: {
1847 unsigned NumRegs = MI->getNumOperands() - Desc.getNumOperands();
1848 return (NumRegs / 2) + (NumRegs % 2) + 1;
1851 case ARM::LDMIA_RET:
1856 case ARM::LDMIA_UPD:
1857 case ARM::LDMDA_UPD:
1858 case ARM::LDMDB_UPD:
1859 case ARM::LDMIB_UPD:
1864 case ARM::STMIA_UPD:
1865 case ARM::STMDA_UPD:
1866 case ARM::STMDB_UPD:
1867 case ARM::STMIB_UPD:
1869 case ARM::tLDMIA_UPD:
1871 case ARM::tSTMIA_UPD:
1875 case ARM::t2LDMIA_RET:
1878 case ARM::t2LDMIA_UPD:
1879 case ARM::t2LDMDB_UPD:
1882 case ARM::t2STMIA_UPD:
1883 case ARM::t2STMDB_UPD: {
1884 unsigned NumRegs = MI->getNumOperands() - Desc.getNumOperands() + 1;
1885 if (Subtarget.isCortexA8()) {
1888 // 4 registers would be issued: 2, 2.
1889 // 5 registers would be issued: 2, 2, 1.
1890 UOps = (NumRegs / 2);
1894 } else if (Subtarget.isCortexA9()) {
1895 UOps = (NumRegs / 2);
1896 // If there are odd number of registers or if it's not 64-bit aligned,
1897 // then it takes an extra AGU (Address Generation Unit) cycle.
1898 if ((NumRegs % 2) ||
1899 !MI->hasOneMemOperand() ||
1900 (*MI->memoperands_begin())->getAlignment() < 8)
1904 // Assume the worst.
1912 ARMBaseInstrInfo::getVLDMDefCycle(const InstrItineraryData *ItinData,
1913 const MCInstrDesc &DefMCID,
1915 unsigned DefIdx, unsigned DefAlign) const {
1916 int RegNo = (int)(DefIdx+1) - DefMCID.getNumOperands() + 1;
1918 // Def is the address writeback.
1919 return ItinData->getOperandCycle(DefClass, DefIdx);
1922 if (Subtarget.isCortexA8()) {
1923 // (regno / 2) + (regno % 2) + 1
1924 DefCycle = RegNo / 2 + 1;
1927 } else if (Subtarget.isCortexA9()) {
1929 bool isSLoad = false;
1931 switch (DefMCID.getOpcode()) {
1934 case ARM::VLDMSIA_UPD:
1935 case ARM::VLDMSDB_UPD:
1940 // If there are odd number of 'S' registers or if it's not 64-bit aligned,
1941 // then it takes an extra cycle.
1942 if ((isSLoad && (RegNo % 2)) || DefAlign < 8)
1945 // Assume the worst.
1946 DefCycle = RegNo + 2;
1953 ARMBaseInstrInfo::getLDMDefCycle(const InstrItineraryData *ItinData,
1954 const MCInstrDesc &DefMCID,
1956 unsigned DefIdx, unsigned DefAlign) const {
1957 int RegNo = (int)(DefIdx+1) - DefMCID.getNumOperands() + 1;
1959 // Def is the address writeback.
1960 return ItinData->getOperandCycle(DefClass, DefIdx);
1963 if (Subtarget.isCortexA8()) {
1964 // 4 registers would be issued: 1, 2, 1.
1965 // 5 registers would be issued: 1, 2, 2.
1966 DefCycle = RegNo / 2;
1969 // Result latency is issue cycle + 2: E2.
1971 } else if (Subtarget.isCortexA9()) {
1972 DefCycle = (RegNo / 2);
1973 // If there are odd number of registers or if it's not 64-bit aligned,
1974 // then it takes an extra AGU (Address Generation Unit) cycle.
1975 if ((RegNo % 2) || DefAlign < 8)
1977 // Result latency is AGU cycles + 2.
1980 // Assume the worst.
1981 DefCycle = RegNo + 2;
1988 ARMBaseInstrInfo::getVSTMUseCycle(const InstrItineraryData *ItinData,
1989 const MCInstrDesc &UseMCID,
1991 unsigned UseIdx, unsigned UseAlign) const {
1992 int RegNo = (int)(UseIdx+1) - UseMCID.getNumOperands() + 1;
1994 return ItinData->getOperandCycle(UseClass, UseIdx);
1997 if (Subtarget.isCortexA8()) {
1998 // (regno / 2) + (regno % 2) + 1
1999 UseCycle = RegNo / 2 + 1;
2002 } else if (Subtarget.isCortexA9()) {
2004 bool isSStore = false;
2006 switch (UseMCID.getOpcode()) {
2009 case ARM::VSTMSIA_UPD:
2010 case ARM::VSTMSDB_UPD:
2015 // If there are odd number of 'S' registers or if it's not 64-bit aligned,
2016 // then it takes an extra cycle.
2017 if ((isSStore && (RegNo % 2)) || UseAlign < 8)
2020 // Assume the worst.
2021 UseCycle = RegNo + 2;
2028 ARMBaseInstrInfo::getSTMUseCycle(const InstrItineraryData *ItinData,
2029 const MCInstrDesc &UseMCID,
2031 unsigned UseIdx, unsigned UseAlign) const {
2032 int RegNo = (int)(UseIdx+1) - UseMCID.getNumOperands() + 1;
2034 return ItinData->getOperandCycle(UseClass, UseIdx);
2037 if (Subtarget.isCortexA8()) {
2038 UseCycle = RegNo / 2;
2043 } else if (Subtarget.isCortexA9()) {
2044 UseCycle = (RegNo / 2);
2045 // If there are odd number of registers or if it's not 64-bit aligned,
2046 // then it takes an extra AGU (Address Generation Unit) cycle.
2047 if ((RegNo % 2) || UseAlign < 8)
2050 // Assume the worst.
2057 ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
2058 const MCInstrDesc &DefMCID,
2059 unsigned DefIdx, unsigned DefAlign,
2060 const MCInstrDesc &UseMCID,
2061 unsigned UseIdx, unsigned UseAlign) const {
2062 unsigned DefClass = DefMCID.getSchedClass();
2063 unsigned UseClass = UseMCID.getSchedClass();
2065 if (DefIdx < DefMCID.getNumDefs() && UseIdx < UseMCID.getNumOperands())
2066 return ItinData->getOperandLatency(DefClass, DefIdx, UseClass, UseIdx);
2068 // This may be a def / use of a variable_ops instruction, the operand
2069 // latency might be determinable dynamically. Let the target try to
2072 bool LdmBypass = false;
2073 switch (DefMCID.getOpcode()) {
2075 DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
2079 case ARM::VLDMDIA_UPD:
2080 case ARM::VLDMDDB_UPD:
2082 case ARM::VLDMSIA_UPD:
2083 case ARM::VLDMSDB_UPD:
2084 DefCycle = getVLDMDefCycle(ItinData, DefMCID, DefClass, DefIdx, DefAlign);
2087 case ARM::LDMIA_RET:
2092 case ARM::LDMIA_UPD:
2093 case ARM::LDMDA_UPD:
2094 case ARM::LDMDB_UPD:
2095 case ARM::LDMIB_UPD:
2097 case ARM::tLDMIA_UPD:
2099 case ARM::t2LDMIA_RET:
2102 case ARM::t2LDMIA_UPD:
2103 case ARM::t2LDMDB_UPD:
2105 DefCycle = getLDMDefCycle(ItinData, DefMCID, DefClass, DefIdx, DefAlign);
2110 // We can't seem to determine the result latency of the def, assume it's 2.
2114 switch (UseMCID.getOpcode()) {
2116 UseCycle = ItinData->getOperandCycle(UseClass, UseIdx);
2120 case ARM::VSTMDIA_UPD:
2121 case ARM::VSTMDDB_UPD:
2123 case ARM::VSTMSIA_UPD:
2124 case ARM::VSTMSDB_UPD:
2125 UseCycle = getVSTMUseCycle(ItinData, UseMCID, UseClass, UseIdx, UseAlign);
2132 case ARM::STMIA_UPD:
2133 case ARM::STMDA_UPD:
2134 case ARM::STMDB_UPD:
2135 case ARM::STMIB_UPD:
2137 case ARM::tSTMIA_UPD:
2142 case ARM::t2STMIA_UPD:
2143 case ARM::t2STMDB_UPD:
2144 UseCycle = getSTMUseCycle(ItinData, UseMCID, UseClass, UseIdx, UseAlign);
2149 // Assume it's read in the first stage.
2152 UseCycle = DefCycle - UseCycle + 1;
2155 // It's a variable_ops instruction so we can't use DefIdx here. Just use
2156 // first def operand.
2157 if (ItinData->hasPipelineForwarding(DefClass, DefMCID.getNumOperands()-1,
2160 } else if (ItinData->hasPipelineForwarding(DefClass, DefIdx,
2161 UseClass, UseIdx)) {
2170 ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
2171 const MachineInstr *DefMI, unsigned DefIdx,
2172 const MachineInstr *UseMI, unsigned UseIdx) const {
2173 if (DefMI->isCopyLike() || DefMI->isInsertSubreg() ||
2174 DefMI->isRegSequence() || DefMI->isImplicitDef())
2177 const MCInstrDesc &DefMCID = DefMI->getDesc();
2178 if (!ItinData || ItinData->isEmpty())
2179 return DefMCID.mayLoad() ? 3 : 1;
2181 const MCInstrDesc &UseMCID = UseMI->getDesc();
2182 const MachineOperand &DefMO = DefMI->getOperand(DefIdx);
2183 if (DefMO.getReg() == ARM::CPSR) {
2184 if (DefMI->getOpcode() == ARM::FMSTAT) {
2185 // fpscr -> cpsr stalls over 20 cycles on A8 (and earlier?)
2186 return Subtarget.isCortexA9() ? 1 : 20;
2189 // CPSR set and branch can be paired in the same cycle.
2190 if (UseMCID.isBranch())
2194 unsigned DefAlign = DefMI->hasOneMemOperand()
2195 ? (*DefMI->memoperands_begin())->getAlignment() : 0;
2196 unsigned UseAlign = UseMI->hasOneMemOperand()
2197 ? (*UseMI->memoperands_begin())->getAlignment() : 0;
2198 int Latency = getOperandLatency(ItinData, DefMCID, DefIdx, DefAlign,
2199 UseMCID, 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 (DefMCID.getOpcode()) {
2209 unsigned ShOpVal = DefMI->getOperand(3).getImm();
2210 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2212 (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
2219 case ARM::t2LDRSHs: {
2220 // Thumb2 mode: lsl only.
2221 unsigned ShAmt = DefMI->getOperand(3).getImm();
2222 if (ShAmt == 0 || ShAmt == 2)
2229 if (DefAlign < 8 && Subtarget.isCortexA9())
2230 switch (DefMCID.getOpcode()) {
2236 case ARM::VLD1q8_UPD:
2237 case ARM::VLD1q16_UPD:
2238 case ARM::VLD1q32_UPD:
2239 case ARM::VLD1q64_UPD:
2246 case ARM::VLD2d8_UPD:
2247 case ARM::VLD2d16_UPD:
2248 case ARM::VLD2d32_UPD:
2249 case ARM::VLD2q8_UPD:
2250 case ARM::VLD2q16_UPD:
2251 case ARM::VLD2q32_UPD:
2256 case ARM::VLD3d8_UPD:
2257 case ARM::VLD3d16_UPD:
2258 case ARM::VLD3d32_UPD:
2259 case ARM::VLD1d64T_UPD:
2260 case ARM::VLD3q8_UPD:
2261 case ARM::VLD3q16_UPD:
2262 case ARM::VLD3q32_UPD:
2267 case ARM::VLD4d8_UPD:
2268 case ARM::VLD4d16_UPD:
2269 case ARM::VLD4d32_UPD:
2270 case ARM::VLD1d64Q_UPD:
2271 case ARM::VLD4q8_UPD:
2272 case ARM::VLD4q16_UPD:
2273 case ARM::VLD4q32_UPD:
2274 case ARM::VLD1DUPq8:
2275 case ARM::VLD1DUPq16:
2276 case ARM::VLD1DUPq32:
2277 case ARM::VLD1DUPq8_UPD:
2278 case ARM::VLD1DUPq16_UPD:
2279 case ARM::VLD1DUPq32_UPD:
2280 case ARM::VLD2DUPd8:
2281 case ARM::VLD2DUPd16:
2282 case ARM::VLD2DUPd32:
2283 case ARM::VLD2DUPd8_UPD:
2284 case ARM::VLD2DUPd16_UPD:
2285 case ARM::VLD2DUPd32_UPD:
2286 case ARM::VLD4DUPd8:
2287 case ARM::VLD4DUPd16:
2288 case ARM::VLD4DUPd32:
2289 case ARM::VLD4DUPd8_UPD:
2290 case ARM::VLD4DUPd16_UPD:
2291 case ARM::VLD4DUPd32_UPD:
2293 case ARM::VLD1LNd16:
2294 case ARM::VLD1LNd32:
2295 case ARM::VLD1LNd8_UPD:
2296 case ARM::VLD1LNd16_UPD:
2297 case ARM::VLD1LNd32_UPD:
2299 case ARM::VLD2LNd16:
2300 case ARM::VLD2LNd32:
2301 case ARM::VLD2LNq16:
2302 case ARM::VLD2LNq32:
2303 case ARM::VLD2LNd8_UPD:
2304 case ARM::VLD2LNd16_UPD:
2305 case ARM::VLD2LNd32_UPD:
2306 case ARM::VLD2LNq16_UPD:
2307 case ARM::VLD2LNq32_UPD:
2309 case ARM::VLD4LNd16:
2310 case ARM::VLD4LNd32:
2311 case ARM::VLD4LNq16:
2312 case ARM::VLD4LNq32:
2313 case ARM::VLD4LNd8_UPD:
2314 case ARM::VLD4LNd16_UPD:
2315 case ARM::VLD4LNd32_UPD:
2316 case ARM::VLD4LNq16_UPD:
2317 case ARM::VLD4LNq32_UPD:
2318 // If the address is not 64-bit aligned, the latencies of these
2319 // instructions increases by one.
2328 ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
2329 SDNode *DefNode, unsigned DefIdx,
2330 SDNode *UseNode, unsigned UseIdx) const {
2331 if (!DefNode->isMachineOpcode())
2334 const MCInstrDesc &DefMCID = get(DefNode->getMachineOpcode());
2336 if (isZeroCost(DefMCID.Opcode))
2339 if (!ItinData || ItinData->isEmpty())
2340 return DefMCID.mayLoad() ? 3 : 1;
2342 if (!UseNode->isMachineOpcode()) {
2343 int Latency = ItinData->getOperandCycle(DefMCID.getSchedClass(), DefIdx);
2344 if (Subtarget.isCortexA9())
2345 return Latency <= 2 ? 1 : Latency - 1;
2347 return Latency <= 3 ? 1 : Latency - 2;
2350 const MCInstrDesc &UseMCID = get(UseNode->getMachineOpcode());
2351 const MachineSDNode *DefMN = dyn_cast<MachineSDNode>(DefNode);
2352 unsigned DefAlign = !DefMN->memoperands_empty()
2353 ? (*DefMN->memoperands_begin())->getAlignment() : 0;
2354 const MachineSDNode *UseMN = dyn_cast<MachineSDNode>(UseNode);
2355 unsigned UseAlign = !UseMN->memoperands_empty()
2356 ? (*UseMN->memoperands_begin())->getAlignment() : 0;
2357 int Latency = getOperandLatency(ItinData, DefMCID, DefIdx, DefAlign,
2358 UseMCID, UseIdx, UseAlign);
2361 (Subtarget.isCortexA8() || Subtarget.isCortexA9())) {
2362 // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2]
2363 // variants are one cycle cheaper.
2364 switch (DefMCID.getOpcode()) {
2369 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
2370 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2372 (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
2379 case ARM::t2LDRSHs: {
2380 // Thumb2 mode: lsl only.
2382 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
2383 if (ShAmt == 0 || ShAmt == 2)
2390 if (DefAlign < 8 && Subtarget.isCortexA9())
2391 switch (DefMCID.getOpcode()) {
2393 case ARM::VLD1q8Pseudo:
2394 case ARM::VLD1q16Pseudo:
2395 case ARM::VLD1q32Pseudo:
2396 case ARM::VLD1q64Pseudo:
2397 case ARM::VLD1q8Pseudo_UPD:
2398 case ARM::VLD1q16Pseudo_UPD:
2399 case ARM::VLD1q32Pseudo_UPD:
2400 case ARM::VLD1q64Pseudo_UPD:
2401 case ARM::VLD2d8Pseudo:
2402 case ARM::VLD2d16Pseudo:
2403 case ARM::VLD2d32Pseudo:
2404 case ARM::VLD2q8Pseudo:
2405 case ARM::VLD2q16Pseudo:
2406 case ARM::VLD2q32Pseudo:
2407 case ARM::VLD2d8Pseudo_UPD:
2408 case ARM::VLD2d16Pseudo_UPD:
2409 case ARM::VLD2d32Pseudo_UPD:
2410 case ARM::VLD2q8Pseudo_UPD:
2411 case ARM::VLD2q16Pseudo_UPD:
2412 case ARM::VLD2q32Pseudo_UPD:
2413 case ARM::VLD3d8Pseudo:
2414 case ARM::VLD3d16Pseudo:
2415 case ARM::VLD3d32Pseudo:
2416 case ARM::VLD1d64TPseudo:
2417 case ARM::VLD3d8Pseudo_UPD:
2418 case ARM::VLD3d16Pseudo_UPD:
2419 case ARM::VLD3d32Pseudo_UPD:
2420 case ARM::VLD1d64TPseudo_UPD:
2421 case ARM::VLD3q8Pseudo_UPD:
2422 case ARM::VLD3q16Pseudo_UPD:
2423 case ARM::VLD3q32Pseudo_UPD:
2424 case ARM::VLD3q8oddPseudo:
2425 case ARM::VLD3q16oddPseudo:
2426 case ARM::VLD3q32oddPseudo:
2427 case ARM::VLD3q8oddPseudo_UPD:
2428 case ARM::VLD3q16oddPseudo_UPD:
2429 case ARM::VLD3q32oddPseudo_UPD:
2430 case ARM::VLD4d8Pseudo:
2431 case ARM::VLD4d16Pseudo:
2432 case ARM::VLD4d32Pseudo:
2433 case ARM::VLD1d64QPseudo:
2434 case ARM::VLD4d8Pseudo_UPD:
2435 case ARM::VLD4d16Pseudo_UPD:
2436 case ARM::VLD4d32Pseudo_UPD:
2437 case ARM::VLD1d64QPseudo_UPD:
2438 case ARM::VLD4q8Pseudo_UPD:
2439 case ARM::VLD4q16Pseudo_UPD:
2440 case ARM::VLD4q32Pseudo_UPD:
2441 case ARM::VLD4q8oddPseudo:
2442 case ARM::VLD4q16oddPseudo:
2443 case ARM::VLD4q32oddPseudo:
2444 case ARM::VLD4q8oddPseudo_UPD:
2445 case ARM::VLD4q16oddPseudo_UPD:
2446 case ARM::VLD4q32oddPseudo_UPD:
2447 case ARM::VLD1DUPq8Pseudo:
2448 case ARM::VLD1DUPq16Pseudo:
2449 case ARM::VLD1DUPq32Pseudo:
2450 case ARM::VLD1DUPq8Pseudo_UPD:
2451 case ARM::VLD1DUPq16Pseudo_UPD:
2452 case ARM::VLD1DUPq32Pseudo_UPD:
2453 case ARM::VLD2DUPd8Pseudo:
2454 case ARM::VLD2DUPd16Pseudo:
2455 case ARM::VLD2DUPd32Pseudo:
2456 case ARM::VLD2DUPd8Pseudo_UPD:
2457 case ARM::VLD2DUPd16Pseudo_UPD:
2458 case ARM::VLD2DUPd32Pseudo_UPD:
2459 case ARM::VLD4DUPd8Pseudo:
2460 case ARM::VLD4DUPd16Pseudo:
2461 case ARM::VLD4DUPd32Pseudo:
2462 case ARM::VLD4DUPd8Pseudo_UPD:
2463 case ARM::VLD4DUPd16Pseudo_UPD:
2464 case ARM::VLD4DUPd32Pseudo_UPD:
2465 case ARM::VLD1LNq8Pseudo:
2466 case ARM::VLD1LNq16Pseudo:
2467 case ARM::VLD1LNq32Pseudo:
2468 case ARM::VLD1LNq8Pseudo_UPD:
2469 case ARM::VLD1LNq16Pseudo_UPD:
2470 case ARM::VLD1LNq32Pseudo_UPD:
2471 case ARM::VLD2LNd8Pseudo:
2472 case ARM::VLD2LNd16Pseudo:
2473 case ARM::VLD2LNd32Pseudo:
2474 case ARM::VLD2LNq16Pseudo:
2475 case ARM::VLD2LNq32Pseudo:
2476 case ARM::VLD2LNd8Pseudo_UPD:
2477 case ARM::VLD2LNd16Pseudo_UPD:
2478 case ARM::VLD2LNd32Pseudo_UPD:
2479 case ARM::VLD2LNq16Pseudo_UPD:
2480 case ARM::VLD2LNq32Pseudo_UPD:
2481 case ARM::VLD4LNd8Pseudo:
2482 case ARM::VLD4LNd16Pseudo:
2483 case ARM::VLD4LNd32Pseudo:
2484 case ARM::VLD4LNq16Pseudo:
2485 case ARM::VLD4LNq32Pseudo:
2486 case ARM::VLD4LNd8Pseudo_UPD:
2487 case ARM::VLD4LNd16Pseudo_UPD:
2488 case ARM::VLD4LNd32Pseudo_UPD:
2489 case ARM::VLD4LNq16Pseudo_UPD:
2490 case ARM::VLD4LNq32Pseudo_UPD:
2491 // If the address is not 64-bit aligned, the latencies of these
2492 // instructions increases by one.
2500 int ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
2501 const MachineInstr *MI,
2502 unsigned *PredCost) const {
2503 if (MI->isCopyLike() || MI->isInsertSubreg() ||
2504 MI->isRegSequence() || MI->isImplicitDef())
2507 if (!ItinData || ItinData->isEmpty())
2510 const MCInstrDesc &MCID = MI->getDesc();
2511 unsigned Class = MCID.getSchedClass();
2512 unsigned UOps = ItinData->Itineraries[Class].NumMicroOps;
2513 if (PredCost && MCID.hasImplicitDefOfPhysReg(ARM::CPSR))
2514 // When predicated, CPSR is an additional source operand for CPSR updating
2515 // instructions, this apparently increases their latencies.
2518 return ItinData->getStageLatency(Class);
2519 return getNumMicroOps(ItinData, MI);
2522 int ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
2523 SDNode *Node) const {
2524 if (!Node->isMachineOpcode())
2527 if (!ItinData || ItinData->isEmpty())
2530 unsigned Opcode = Node->getMachineOpcode();
2533 return ItinData->getStageLatency(get(Opcode).getSchedClass());
2540 bool ARMBaseInstrInfo::
2541 hasHighOperandLatency(const InstrItineraryData *ItinData,
2542 const MachineRegisterInfo *MRI,
2543 const MachineInstr *DefMI, unsigned DefIdx,
2544 const MachineInstr *UseMI, unsigned UseIdx) const {
2545 unsigned DDomain = DefMI->getDesc().TSFlags & ARMII::DomainMask;
2546 unsigned UDomain = UseMI->getDesc().TSFlags & ARMII::DomainMask;
2547 if (Subtarget.isCortexA8() &&
2548 (DDomain == ARMII::DomainVFP || UDomain == ARMII::DomainVFP))
2549 // CortexA8 VFP instructions are not pipelined.
2552 // Hoist VFP / NEON instructions with 4 or higher latency.
2553 int Latency = getOperandLatency(ItinData, DefMI, DefIdx, UseMI, UseIdx);
2556 return DDomain == ARMII::DomainVFP || DDomain == ARMII::DomainNEON ||
2557 UDomain == ARMII::DomainVFP || UDomain == ARMII::DomainNEON;
2560 bool ARMBaseInstrInfo::
2561 hasLowDefLatency(const InstrItineraryData *ItinData,
2562 const MachineInstr *DefMI, unsigned DefIdx) const {
2563 if (!ItinData || ItinData->isEmpty())
2566 unsigned DDomain = DefMI->getDesc().TSFlags & ARMII::DomainMask;
2567 if (DDomain == ARMII::DomainGeneral) {
2568 unsigned DefClass = DefMI->getDesc().getSchedClass();
2569 int DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
2570 return (DefCycle != -1 && DefCycle <= 2);
2576 ARMBaseInstrInfo::isFpMLxInstruction(unsigned Opcode, unsigned &MulOpc,
2577 unsigned &AddSubOpc,
2578 bool &NegAcc, bool &HasLane) const {
2579 DenseMap<unsigned, unsigned>::const_iterator I = MLxEntryMap.find(Opcode);
2580 if (I == MLxEntryMap.end())
2583 const ARM_MLxEntry &Entry = ARM_MLxTable[I->second];
2584 MulOpc = Entry.MulOpc;
2585 AddSubOpc = Entry.AddSubOpc;
2586 NegAcc = Entry.NegAcc;
2587 HasLane = Entry.HasLane;