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 "ARMMachineFunctionInfo.h"
19 #include "ARMRegisterInfo.h"
20 #include "ARMGenInstrInfo.inc"
21 #include "llvm/Constants.h"
22 #include "llvm/Function.h"
23 #include "llvm/GlobalValue.h"
24 #include "llvm/ADT/STLExtras.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"
40 EnableARM3Addr("enable-arm-3-addr-conv", cl::Hidden,
41 cl::desc("Enable ARM 2-addr to 3-addr conv"));
44 OldARMIfCvt("old-arm-ifcvt", cl::Hidden,
45 cl::desc("Use old-style ARM if-conversion heuristics"));
47 ARMBaseInstrInfo::ARMBaseInstrInfo(const ARMSubtarget& STI)
48 : TargetInstrInfoImpl(ARMInsts, array_lengthof(ARMInsts)),
53 ARMBaseInstrInfo::convertToThreeAddress(MachineFunction::iterator &MFI,
54 MachineBasicBlock::iterator &MBBI,
55 LiveVariables *LV) const {
56 // FIXME: Thumb2 support.
61 MachineInstr *MI = MBBI;
62 MachineFunction &MF = *MI->getParent()->getParent();
63 uint64_t TSFlags = MI->getDesc().TSFlags;
65 switch ((TSFlags & ARMII::IndexModeMask) >> ARMII::IndexModeShift) {
67 case ARMII::IndexModePre:
70 case ARMII::IndexModePost:
74 // Try splitting an indexed load/store to an un-indexed one plus an add/sub
76 unsigned MemOpc = getUnindexedOpcode(MI->getOpcode());
80 MachineInstr *UpdateMI = NULL;
81 MachineInstr *MemMI = NULL;
82 unsigned AddrMode = (TSFlags & ARMII::AddrModeMask);
83 const TargetInstrDesc &TID = MI->getDesc();
84 unsigned NumOps = TID.getNumOperands();
85 bool isLoad = !TID.mayStore();
86 const MachineOperand &WB = isLoad ? MI->getOperand(1) : MI->getOperand(0);
87 const MachineOperand &Base = MI->getOperand(2);
88 const MachineOperand &Offset = MI->getOperand(NumOps-3);
89 unsigned WBReg = WB.getReg();
90 unsigned BaseReg = Base.getReg();
91 unsigned OffReg = Offset.getReg();
92 unsigned OffImm = MI->getOperand(NumOps-2).getImm();
93 ARMCC::CondCodes Pred = (ARMCC::CondCodes)MI->getOperand(NumOps-1).getImm();
96 assert(false && "Unknown indexed op!");
98 case ARMII::AddrMode2: {
99 bool isSub = ARM_AM::getAM2Op(OffImm) == ARM_AM::sub;
100 unsigned Amt = ARM_AM::getAM2Offset(OffImm);
102 if (ARM_AM::getSOImmVal(Amt) == -1)
103 // Can't encode it in a so_imm operand. This transformation will
104 // add more than 1 instruction. Abandon!
106 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
107 get(isSub ? ARM::SUBri : ARM::ADDri), WBReg)
108 .addReg(BaseReg).addImm(Amt)
109 .addImm(Pred).addReg(0).addReg(0);
110 } else if (Amt != 0) {
111 ARM_AM::ShiftOpc ShOpc = ARM_AM::getAM2ShiftOpc(OffImm);
112 unsigned SOOpc = ARM_AM::getSORegOpc(ShOpc, Amt);
113 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
114 get(isSub ? ARM::SUBrs : ARM::ADDrs), WBReg)
115 .addReg(BaseReg).addReg(OffReg).addReg(0).addImm(SOOpc)
116 .addImm(Pred).addReg(0).addReg(0);
118 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
119 get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
120 .addReg(BaseReg).addReg(OffReg)
121 .addImm(Pred).addReg(0).addReg(0);
124 case ARMII::AddrMode3 : {
125 bool isSub = ARM_AM::getAM3Op(OffImm) == ARM_AM::sub;
126 unsigned Amt = ARM_AM::getAM3Offset(OffImm);
128 // Immediate is 8-bits. It's guaranteed to fit in a so_imm operand.
129 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
130 get(isSub ? ARM::SUBri : ARM::ADDri), WBReg)
131 .addReg(BaseReg).addImm(Amt)
132 .addImm(Pred).addReg(0).addReg(0);
134 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
135 get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
136 .addReg(BaseReg).addReg(OffReg)
137 .addImm(Pred).addReg(0).addReg(0);
142 std::vector<MachineInstr*> NewMIs;
145 MemMI = BuildMI(MF, MI->getDebugLoc(),
146 get(MemOpc), MI->getOperand(0).getReg())
147 .addReg(WBReg).addImm(0).addImm(Pred);
149 MemMI = BuildMI(MF, MI->getDebugLoc(),
150 get(MemOpc)).addReg(MI->getOperand(1).getReg())
151 .addReg(WBReg).addReg(0).addImm(0).addImm(Pred);
152 NewMIs.push_back(MemMI);
153 NewMIs.push_back(UpdateMI);
156 MemMI = BuildMI(MF, MI->getDebugLoc(),
157 get(MemOpc), MI->getOperand(0).getReg())
158 .addReg(BaseReg).addImm(0).addImm(Pred);
160 MemMI = BuildMI(MF, MI->getDebugLoc(),
161 get(MemOpc)).addReg(MI->getOperand(1).getReg())
162 .addReg(BaseReg).addReg(0).addImm(0).addImm(Pred);
164 UpdateMI->getOperand(0).setIsDead();
165 NewMIs.push_back(UpdateMI);
166 NewMIs.push_back(MemMI);
169 // Transfer LiveVariables states, kill / dead info.
171 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
172 MachineOperand &MO = MI->getOperand(i);
173 if (MO.isReg() && MO.getReg() &&
174 TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
175 unsigned Reg = MO.getReg();
177 LiveVariables::VarInfo &VI = LV->getVarInfo(Reg);
179 MachineInstr *NewMI = (Reg == WBReg) ? UpdateMI : MemMI;
181 LV->addVirtualRegisterDead(Reg, NewMI);
183 if (MO.isUse() && MO.isKill()) {
184 for (unsigned j = 0; j < 2; ++j) {
185 // Look at the two new MI's in reverse order.
186 MachineInstr *NewMI = NewMIs[j];
187 if (!NewMI->readsRegister(Reg))
189 LV->addVirtualRegisterKilled(Reg, NewMI);
190 if (VI.removeKill(MI))
191 VI.Kills.push_back(NewMI);
199 MFI->insert(MBBI, NewMIs[1]);
200 MFI->insert(MBBI, NewMIs[0]);
205 ARMBaseInstrInfo::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
206 MachineBasicBlock::iterator MI,
207 const std::vector<CalleeSavedInfo> &CSI,
208 const TargetRegisterInfo *TRI) const {
213 if (MI != MBB.end()) DL = MI->getDebugLoc();
215 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
216 unsigned Reg = CSI[i].getReg();
219 // Add the callee-saved register as live-in unless it's LR and
220 // @llvm.returnaddress is called. If LR is returned for @llvm.returnaddress
221 // then it's already added to the function and entry block live-in sets.
222 if (Reg == ARM::LR) {
223 MachineFunction &MF = *MBB.getParent();
224 if (MF.getFrameInfo()->isReturnAddressTaken() &&
225 MF.getRegInfo().isLiveIn(Reg))
232 // Insert the spill to the stack frame. The register is killed at the spill
234 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
235 storeRegToStackSlot(MBB, MI, Reg, isKill,
236 CSI[i].getFrameIdx(), RC, TRI);
243 ARMBaseInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
244 MachineBasicBlock *&FBB,
245 SmallVectorImpl<MachineOperand> &Cond,
246 bool AllowModify) const {
247 // If the block has no terminators, it just falls into the block after it.
248 MachineBasicBlock::iterator I = MBB.end();
249 if (I == MBB.begin())
252 while (I->isDebugValue()) {
253 if (I == MBB.begin())
257 if (!isUnpredicatedTerminator(I))
260 // Get the last instruction in the block.
261 MachineInstr *LastInst = I;
263 // If there is only one terminator instruction, process it.
264 unsigned LastOpc = LastInst->getOpcode();
265 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
266 if (isUncondBranchOpcode(LastOpc)) {
267 TBB = LastInst->getOperand(0).getMBB();
270 if (isCondBranchOpcode(LastOpc)) {
271 // Block ends with fall-through condbranch.
272 TBB = LastInst->getOperand(0).getMBB();
273 Cond.push_back(LastInst->getOperand(1));
274 Cond.push_back(LastInst->getOperand(2));
277 return true; // Can't handle indirect branch.
280 // Get the instruction before it if it is a terminator.
281 MachineInstr *SecondLastInst = I;
282 unsigned SecondLastOpc = SecondLastInst->getOpcode();
284 // If AllowModify is true and the block ends with two or more unconditional
285 // branches, delete all but the first unconditional branch.
286 if (AllowModify && isUncondBranchOpcode(LastOpc)) {
287 while (isUncondBranchOpcode(SecondLastOpc)) {
288 LastInst->eraseFromParent();
289 LastInst = SecondLastInst;
290 LastOpc = LastInst->getOpcode();
291 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
292 // Return now the only terminator is an unconditional branch.
293 TBB = LastInst->getOperand(0).getMBB();
297 SecondLastOpc = SecondLastInst->getOpcode();
302 // If there are three terminators, we don't know what sort of block this is.
303 if (SecondLastInst && I != MBB.begin() && isUnpredicatedTerminator(--I))
306 // If the block ends with a B and a Bcc, handle it.
307 if (isCondBranchOpcode(SecondLastOpc) && isUncondBranchOpcode(LastOpc)) {
308 TBB = SecondLastInst->getOperand(0).getMBB();
309 Cond.push_back(SecondLastInst->getOperand(1));
310 Cond.push_back(SecondLastInst->getOperand(2));
311 FBB = LastInst->getOperand(0).getMBB();
315 // If the block ends with two unconditional branches, handle it. The second
316 // one is not executed, so remove it.
317 if (isUncondBranchOpcode(SecondLastOpc) && isUncondBranchOpcode(LastOpc)) {
318 TBB = SecondLastInst->getOperand(0).getMBB();
321 I->eraseFromParent();
325 // ...likewise if it ends with a branch table followed by an unconditional
326 // branch. The branch folder can create these, and we must get rid of them for
327 // correctness of Thumb constant islands.
328 if ((isJumpTableBranchOpcode(SecondLastOpc) ||
329 isIndirectBranchOpcode(SecondLastOpc)) &&
330 isUncondBranchOpcode(LastOpc)) {
333 I->eraseFromParent();
337 // Otherwise, can't handle this.
342 unsigned ARMBaseInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
343 MachineBasicBlock::iterator I = MBB.end();
344 if (I == MBB.begin()) return 0;
346 while (I->isDebugValue()) {
347 if (I == MBB.begin())
351 if (!isUncondBranchOpcode(I->getOpcode()) &&
352 !isCondBranchOpcode(I->getOpcode()))
355 // Remove the branch.
356 I->eraseFromParent();
360 if (I == MBB.begin()) return 1;
362 if (!isCondBranchOpcode(I->getOpcode()))
365 // Remove the branch.
366 I->eraseFromParent();
371 ARMBaseInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
372 MachineBasicBlock *FBB,
373 const SmallVectorImpl<MachineOperand> &Cond,
375 ARMFunctionInfo *AFI = MBB.getParent()->getInfo<ARMFunctionInfo>();
376 int BOpc = !AFI->isThumbFunction()
377 ? ARM::B : (AFI->isThumb2Function() ? ARM::t2B : ARM::tB);
378 int BccOpc = !AFI->isThumbFunction()
379 ? ARM::Bcc : (AFI->isThumb2Function() ? ARM::t2Bcc : ARM::tBcc);
381 // Shouldn't be a fall through.
382 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
383 assert((Cond.size() == 2 || Cond.size() == 0) &&
384 "ARM branch conditions have two components!");
387 if (Cond.empty()) // Unconditional branch?
388 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB);
390 BuildMI(&MBB, DL, get(BccOpc)).addMBB(TBB)
391 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg());
395 // Two-way conditional branch.
396 BuildMI(&MBB, DL, get(BccOpc)).addMBB(TBB)
397 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg());
398 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB);
402 bool ARMBaseInstrInfo::
403 ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
404 ARMCC::CondCodes CC = (ARMCC::CondCodes)(int)Cond[0].getImm();
405 Cond[0].setImm(ARMCC::getOppositeCondition(CC));
409 bool ARMBaseInstrInfo::
410 PredicateInstruction(MachineInstr *MI,
411 const SmallVectorImpl<MachineOperand> &Pred) const {
412 unsigned Opc = MI->getOpcode();
413 if (isUncondBranchOpcode(Opc)) {
414 MI->setDesc(get(getMatchingCondBranchOpcode(Opc)));
415 MI->addOperand(MachineOperand::CreateImm(Pred[0].getImm()));
416 MI->addOperand(MachineOperand::CreateReg(Pred[1].getReg(), false));
420 int PIdx = MI->findFirstPredOperandIdx();
422 MachineOperand &PMO = MI->getOperand(PIdx);
423 PMO.setImm(Pred[0].getImm());
424 MI->getOperand(PIdx+1).setReg(Pred[1].getReg());
430 bool ARMBaseInstrInfo::
431 SubsumesPredicate(const SmallVectorImpl<MachineOperand> &Pred1,
432 const SmallVectorImpl<MachineOperand> &Pred2) const {
433 if (Pred1.size() > 2 || Pred2.size() > 2)
436 ARMCC::CondCodes CC1 = (ARMCC::CondCodes)Pred1[0].getImm();
437 ARMCC::CondCodes CC2 = (ARMCC::CondCodes)Pred2[0].getImm();
447 return CC2 == ARMCC::HI;
449 return CC2 == ARMCC::LO || CC2 == ARMCC::EQ;
451 return CC2 == ARMCC::GT;
453 return CC2 == ARMCC::LT;
457 bool ARMBaseInstrInfo::DefinesPredicate(MachineInstr *MI,
458 std::vector<MachineOperand> &Pred) const {
459 // FIXME: This confuses implicit_def with optional CPSR def.
460 const TargetInstrDesc &TID = MI->getDesc();
461 if (!TID.getImplicitDefs() && !TID.hasOptionalDef())
465 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
466 const MachineOperand &MO = MI->getOperand(i);
467 if (MO.isReg() && MO.getReg() == ARM::CPSR) {
476 /// isPredicable - Return true if the specified instruction can be predicated.
477 /// By default, this returns true for every instruction with a
478 /// PredicateOperand.
479 bool ARMBaseInstrInfo::isPredicable(MachineInstr *MI) const {
480 const TargetInstrDesc &TID = MI->getDesc();
481 if (!TID.isPredicable())
484 if ((TID.TSFlags & ARMII::DomainMask) == ARMII::DomainNEON) {
485 ARMFunctionInfo *AFI =
486 MI->getParent()->getParent()->getInfo<ARMFunctionInfo>();
487 return AFI->isThumb2Function();
492 /// FIXME: Works around a gcc miscompilation with -fstrict-aliasing.
493 LLVM_ATTRIBUTE_NOINLINE
494 static unsigned getNumJTEntries(const std::vector<MachineJumpTableEntry> &JT,
496 static unsigned getNumJTEntries(const std::vector<MachineJumpTableEntry> &JT,
498 assert(JTI < JT.size());
499 return JT[JTI].MBBs.size();
502 /// GetInstSize - Return the size of the specified MachineInstr.
504 unsigned ARMBaseInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
505 const MachineBasicBlock &MBB = *MI->getParent();
506 const MachineFunction *MF = MBB.getParent();
507 const MCAsmInfo *MAI = MF->getTarget().getMCAsmInfo();
509 // Basic size info comes from the TSFlags field.
510 const TargetInstrDesc &TID = MI->getDesc();
511 uint64_t TSFlags = TID.TSFlags;
513 unsigned Opc = MI->getOpcode();
514 switch ((TSFlags & ARMII::SizeMask) >> ARMII::SizeShift) {
516 // If this machine instr is an inline asm, measure it.
517 if (MI->getOpcode() == ARM::INLINEASM)
518 return getInlineAsmLength(MI->getOperand(0).getSymbolName(), *MAI);
523 llvm_unreachable("Unknown or unset size field for instr!");
524 case TargetOpcode::IMPLICIT_DEF:
525 case TargetOpcode::KILL:
526 case TargetOpcode::PROLOG_LABEL:
527 case TargetOpcode::EH_LABEL:
528 case TargetOpcode::DBG_VALUE:
533 case ARMII::Size8Bytes: return 8; // ARM instruction x 2.
534 case ARMII::Size4Bytes: return 4; // ARM / Thumb2 instruction.
535 case ARMII::Size2Bytes: return 2; // Thumb1 instruction.
536 case ARMII::SizeSpecial: {
539 case ARM::t2MOVi32imm:
541 case ARM::CONSTPOOL_ENTRY:
542 // If this machine instr is a constant pool entry, its size is recorded as
544 return MI->getOperand(2).getImm();
545 case ARM::Int_eh_sjlj_longjmp:
547 case ARM::tInt_eh_sjlj_longjmp:
549 case ARM::Int_eh_sjlj_setjmp:
550 case ARM::Int_eh_sjlj_setjmp_nofp:
552 case ARM::tInt_eh_sjlj_setjmp:
553 case ARM::t2Int_eh_sjlj_setjmp:
554 case ARM::t2Int_eh_sjlj_setjmp_nofp:
563 // These are jumptable branches, i.e. a branch followed by an inlined
564 // jumptable. The size is 4 + 4 * number of entries. For TBB, each
565 // entry is one byte; TBH two byte each.
566 unsigned EntrySize = (Opc == ARM::t2TBB)
567 ? 1 : ((Opc == ARM::t2TBH) ? 2 : 4);
568 unsigned NumOps = TID.getNumOperands();
569 MachineOperand JTOP =
570 MI->getOperand(NumOps - (TID.isPredicable() ? 3 : 2));
571 unsigned JTI = JTOP.getIndex();
572 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
574 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
575 assert(JTI < JT.size());
576 // Thumb instructions are 2 byte aligned, but JT entries are 4 byte
577 // 4 aligned. The assembler / linker may add 2 byte padding just before
578 // the JT entries. The size does not include this padding; the
579 // constant islands pass does separate bookkeeping for it.
580 // FIXME: If we know the size of the function is less than (1 << 16) *2
581 // bytes, we can use 16-bit entries instead. Then there won't be an
583 unsigned InstSize = (Opc == ARM::tBR_JTr || Opc == ARM::t2BR_JT) ? 2 : 4;
584 unsigned NumEntries = getNumJTEntries(JT, JTI);
585 if (Opc == ARM::t2TBB && (NumEntries & 1))
586 // Make sure the instruction that follows TBB is 2-byte aligned.
587 // FIXME: Constant island pass should insert an "ALIGN" instruction
590 return NumEntries * EntrySize + InstSize;
593 // Otherwise, pseudo-instruction sizes are zero.
598 return 0; // Not reached
601 void ARMBaseInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
602 MachineBasicBlock::iterator I, DebugLoc DL,
603 unsigned DestReg, unsigned SrcReg,
604 bool KillSrc) const {
605 bool GPRDest = ARM::GPRRegClass.contains(DestReg);
606 bool GPRSrc = ARM::GPRRegClass.contains(SrcReg);
608 if (GPRDest && GPRSrc) {
609 AddDefaultCC(AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::MOVr), DestReg)
610 .addReg(SrcReg, getKillRegState(KillSrc))));
614 bool SPRDest = ARM::SPRRegClass.contains(DestReg);
615 bool SPRSrc = ARM::SPRRegClass.contains(SrcReg);
618 if (SPRDest && SPRSrc)
620 else if (GPRDest && SPRSrc)
622 else if (SPRDest && GPRSrc)
624 else if (ARM::DPRRegClass.contains(DestReg, SrcReg))
626 else if (ARM::QPRRegClass.contains(DestReg, SrcReg))
628 else if (ARM::QQPRRegClass.contains(DestReg, SrcReg))
630 else if (ARM::QQQQPRRegClass.contains(DestReg, SrcReg))
633 llvm_unreachable("Impossible reg-to-reg copy");
635 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(Opc), DestReg);
636 MIB.addReg(SrcReg, getKillRegState(KillSrc));
637 if (Opc != ARM::VMOVQQ && Opc != ARM::VMOVQQQQ)
642 MachineInstrBuilder &AddDReg(MachineInstrBuilder &MIB,
643 unsigned Reg, unsigned SubIdx, unsigned State,
644 const TargetRegisterInfo *TRI) {
646 return MIB.addReg(Reg, State);
648 if (TargetRegisterInfo::isPhysicalRegister(Reg))
649 return MIB.addReg(TRI->getSubReg(Reg, SubIdx), State);
650 return MIB.addReg(Reg, State, SubIdx);
653 void ARMBaseInstrInfo::
654 storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
655 unsigned SrcReg, bool isKill, int FI,
656 const TargetRegisterClass *RC,
657 const TargetRegisterInfo *TRI) const {
659 if (I != MBB.end()) DL = I->getDebugLoc();
660 MachineFunction &MF = *MBB.getParent();
661 MachineFrameInfo &MFI = *MF.getFrameInfo();
662 unsigned Align = MFI.getObjectAlignment(FI);
664 MachineMemOperand *MMO =
665 MF.getMachineMemOperand(MachinePointerInfo(
666 PseudoSourceValue::getFixedStack(FI)),
667 MachineMemOperand::MOStore,
668 MFI.getObjectSize(FI),
671 // tGPR is used sometimes in ARM instructions that need to avoid using
672 // certain registers. Just treat it as GPR here. Likewise, rGPR.
673 if (RC == ARM::tGPRRegisterClass || RC == ARM::tcGPRRegisterClass
674 || RC == ARM::rGPRRegisterClass)
675 RC = ARM::GPRRegisterClass;
677 switch (RC->getID()) {
678 case ARM::GPRRegClassID:
679 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::STRi12))
680 .addReg(SrcReg, getKillRegState(isKill))
681 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
683 case ARM::SPRRegClassID:
684 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTRS))
685 .addReg(SrcReg, getKillRegState(isKill))
686 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
688 case ARM::DPRRegClassID:
689 case ARM::DPR_VFP2RegClassID:
690 case ARM::DPR_8RegClassID:
691 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTRD))
692 .addReg(SrcReg, getKillRegState(isKill))
693 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
695 case ARM::QPRRegClassID:
696 case ARM::QPR_VFP2RegClassID:
697 case ARM::QPR_8RegClassID:
698 if (Align >= 16 && getRegisterInfo().needsStackRealignment(MF)) {
699 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VST1q64Pseudo))
700 .addFrameIndex(FI).addImm(16)
701 .addReg(SrcReg, getKillRegState(isKill))
702 .addMemOperand(MMO));
704 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMQ))
705 .addReg(SrcReg, getKillRegState(isKill))
707 .addImm(ARM_AM::getAM4ModeImm(ARM_AM::ia))
708 .addMemOperand(MMO));
711 case ARM::QQPRRegClassID:
712 case ARM::QQPR_VFP2RegClassID:
713 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
714 // FIXME: It's possible to only store part of the QQ register if the
715 // spilled def has a sub-register index.
716 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VST1d64QPseudo))
717 .addFrameIndex(FI).addImm(16)
718 .addReg(SrcReg, getKillRegState(isKill))
719 .addMemOperand(MMO));
721 MachineInstrBuilder MIB =
722 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMD))
724 .addImm(ARM_AM::getAM4ModeImm(ARM_AM::ia)))
726 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
727 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
728 MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
729 AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI);
732 case ARM::QQQQPRRegClassID: {
733 MachineInstrBuilder MIB =
734 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMD))
736 .addImm(ARM_AM::getAM4ModeImm(ARM_AM::ia)))
738 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
739 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
740 MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
741 MIB = AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI);
742 MIB = AddDReg(MIB, SrcReg, ARM::dsub_4, 0, TRI);
743 MIB = AddDReg(MIB, SrcReg, ARM::dsub_5, 0, TRI);
744 MIB = AddDReg(MIB, SrcReg, ARM::dsub_6, 0, TRI);
745 AddDReg(MIB, SrcReg, ARM::dsub_7, 0, TRI);
749 llvm_unreachable("Unknown regclass!");
754 ARMBaseInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
755 int &FrameIndex) const {
756 switch (MI->getOpcode()) {
759 case ARM::t2STRs: // FIXME: don't use t2STRs to access frame.
760 if (MI->getOperand(1).isFI() &&
761 MI->getOperand(2).isReg() &&
762 MI->getOperand(3).isImm() &&
763 MI->getOperand(2).getReg() == 0 &&
764 MI->getOperand(3).getImm() == 0) {
765 FrameIndex = MI->getOperand(1).getIndex();
766 return MI->getOperand(0).getReg();
774 if (MI->getOperand(1).isFI() &&
775 MI->getOperand(2).isImm() &&
776 MI->getOperand(2).getImm() == 0) {
777 FrameIndex = MI->getOperand(1).getIndex();
778 return MI->getOperand(0).getReg();
781 case ARM::VST1q64Pseudo:
782 if (MI->getOperand(0).isFI() &&
783 MI->getOperand(2).getSubReg() == 0) {
784 FrameIndex = MI->getOperand(0).getIndex();
785 return MI->getOperand(2).getReg();
789 if (MI->getOperand(1).isFI() &&
790 MI->getOperand(2).isImm() &&
791 MI->getOperand(2).getImm() == ARM_AM::getAM4ModeImm(ARM_AM::ia) &&
792 MI->getOperand(0).getSubReg() == 0) {
793 FrameIndex = MI->getOperand(1).getIndex();
794 return MI->getOperand(0).getReg();
802 void ARMBaseInstrInfo::
803 loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
804 unsigned DestReg, int FI,
805 const TargetRegisterClass *RC,
806 const TargetRegisterInfo *TRI) const {
808 if (I != MBB.end()) DL = I->getDebugLoc();
809 MachineFunction &MF = *MBB.getParent();
810 MachineFrameInfo &MFI = *MF.getFrameInfo();
811 unsigned Align = MFI.getObjectAlignment(FI);
812 MachineMemOperand *MMO =
813 MF.getMachineMemOperand(
814 MachinePointerInfo(PseudoSourceValue::getFixedStack(FI)),
815 MachineMemOperand::MOLoad,
816 MFI.getObjectSize(FI),
819 // tGPR is used sometimes in ARM instructions that need to avoid using
820 // certain registers. Just treat it as GPR here.
821 if (RC == ARM::tGPRRegisterClass || RC == ARM::tcGPRRegisterClass
822 || RC == ARM::rGPRRegisterClass)
823 RC = ARM::GPRRegisterClass;
825 switch (RC->getID()) {
826 case ARM::GPRRegClassID:
827 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::LDRi12), DestReg)
828 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
830 case ARM::SPRRegClassID:
831 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDRS), DestReg)
832 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
834 case ARM::DPRRegClassID:
835 case ARM::DPR_VFP2RegClassID:
836 case ARM::DPR_8RegClassID:
837 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDRD), DestReg)
838 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
840 case ARM::QPRRegClassID:
841 case ARM::QPR_VFP2RegClassID:
842 case ARM::QPR_8RegClassID:
843 if (Align >= 16 && getRegisterInfo().needsStackRealignment(MF)) {
844 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLD1q64Pseudo), DestReg)
845 .addFrameIndex(FI).addImm(16)
846 .addMemOperand(MMO));
848 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMQ), DestReg)
850 .addImm(ARM_AM::getAM4ModeImm(ARM_AM::ia))
851 .addMemOperand(MMO));
854 case ARM::QQPRRegClassID:
855 case ARM::QQPR_VFP2RegClassID:
856 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
857 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLD1d64QPseudo), DestReg)
858 .addFrameIndex(FI).addImm(16)
859 .addMemOperand(MMO));
861 MachineInstrBuilder MIB =
862 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMD))
864 .addImm(ARM_AM::getAM4ModeImm(ARM_AM::ia)))
866 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::Define, TRI);
867 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::Define, TRI);
868 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::Define, TRI);
869 AddDReg(MIB, DestReg, ARM::dsub_3, RegState::Define, TRI);
872 case ARM::QQQQPRRegClassID: {
873 MachineInstrBuilder MIB =
874 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMD))
876 .addImm(ARM_AM::getAM4ModeImm(ARM_AM::ia)))
878 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::Define, TRI);
879 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::Define, TRI);
880 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::Define, TRI);
881 MIB = AddDReg(MIB, DestReg, ARM::dsub_3, RegState::Define, TRI);
882 MIB = AddDReg(MIB, DestReg, ARM::dsub_4, RegState::Define, TRI);
883 MIB = AddDReg(MIB, DestReg, ARM::dsub_5, RegState::Define, TRI);
884 MIB = AddDReg(MIB, DestReg, ARM::dsub_6, RegState::Define, TRI);
885 AddDReg(MIB, DestReg, ARM::dsub_7, RegState::Define, TRI);
889 llvm_unreachable("Unknown regclass!");
894 ARMBaseInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
895 int &FrameIndex) const {
896 switch (MI->getOpcode()) {
899 case ARM::t2LDRs: // FIXME: don't use t2LDRs to access frame.
900 if (MI->getOperand(1).isFI() &&
901 MI->getOperand(2).isReg() &&
902 MI->getOperand(3).isImm() &&
903 MI->getOperand(2).getReg() == 0 &&
904 MI->getOperand(3).getImm() == 0) {
905 FrameIndex = MI->getOperand(1).getIndex();
906 return MI->getOperand(0).getReg();
914 if (MI->getOperand(1).isFI() &&
915 MI->getOperand(2).isImm() &&
916 MI->getOperand(2).getImm() == 0) {
917 FrameIndex = MI->getOperand(1).getIndex();
918 return MI->getOperand(0).getReg();
921 case ARM::VLD1q64Pseudo:
922 if (MI->getOperand(1).isFI() &&
923 MI->getOperand(0).getSubReg() == 0) {
924 FrameIndex = MI->getOperand(1).getIndex();
925 return MI->getOperand(0).getReg();
929 if (MI->getOperand(1).isFI() &&
930 MI->getOperand(2).isImm() &&
931 MI->getOperand(2).getImm() == ARM_AM::getAM4ModeImm(ARM_AM::ia) &&
932 MI->getOperand(0).getSubReg() == 0) {
933 FrameIndex = MI->getOperand(1).getIndex();
934 return MI->getOperand(0).getReg();
943 ARMBaseInstrInfo::emitFrameIndexDebugValue(MachineFunction &MF,
944 int FrameIx, uint64_t Offset,
947 MachineInstrBuilder MIB = BuildMI(MF, DL, get(ARM::DBG_VALUE))
948 .addFrameIndex(FrameIx).addImm(0).addImm(Offset).addMetadata(MDPtr);
952 /// Create a copy of a const pool value. Update CPI to the new index and return
954 static unsigned duplicateCPV(MachineFunction &MF, unsigned &CPI) {
955 MachineConstantPool *MCP = MF.getConstantPool();
956 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
958 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPI];
959 assert(MCPE.isMachineConstantPoolEntry() &&
960 "Expecting a machine constantpool entry!");
961 ARMConstantPoolValue *ACPV =
962 static_cast<ARMConstantPoolValue*>(MCPE.Val.MachineCPVal);
964 unsigned PCLabelId = AFI->createConstPoolEntryUId();
965 ARMConstantPoolValue *NewCPV = 0;
966 // FIXME: The below assumes PIC relocation model and that the function
967 // is Thumb mode (t1 or t2). PCAdjustment would be 8 for ARM mode PIC, and
968 // zero for non-PIC in ARM or Thumb. The callers are all of thumb LDR
969 // instructions, so that's probably OK, but is PIC always correct when
971 if (ACPV->isGlobalValue())
972 NewCPV = new ARMConstantPoolValue(ACPV->getGV(), PCLabelId,
974 else if (ACPV->isExtSymbol())
975 NewCPV = new ARMConstantPoolValue(MF.getFunction()->getContext(),
976 ACPV->getSymbol(), PCLabelId, 4);
977 else if (ACPV->isBlockAddress())
978 NewCPV = new ARMConstantPoolValue(ACPV->getBlockAddress(), PCLabelId,
979 ARMCP::CPBlockAddress, 4);
980 else if (ACPV->isLSDA())
981 NewCPV = new ARMConstantPoolValue(MF.getFunction(), PCLabelId,
984 llvm_unreachable("Unexpected ARM constantpool value type!!");
985 CPI = MCP->getConstantPoolIndex(NewCPV, MCPE.getAlignment());
989 void ARMBaseInstrInfo::
990 reMaterialize(MachineBasicBlock &MBB,
991 MachineBasicBlock::iterator I,
992 unsigned DestReg, unsigned SubIdx,
993 const MachineInstr *Orig,
994 const TargetRegisterInfo &TRI) const {
995 unsigned Opcode = Orig->getOpcode();
998 MachineInstr *MI = MBB.getParent()->CloneMachineInstr(Orig);
999 MI->substituteRegister(Orig->getOperand(0).getReg(), DestReg, SubIdx, TRI);
1003 case ARM::tLDRpci_pic:
1004 case ARM::t2LDRpci_pic: {
1005 MachineFunction &MF = *MBB.getParent();
1006 unsigned CPI = Orig->getOperand(1).getIndex();
1007 unsigned PCLabelId = duplicateCPV(MF, CPI);
1008 MachineInstrBuilder MIB = BuildMI(MBB, I, Orig->getDebugLoc(), get(Opcode),
1010 .addConstantPoolIndex(CPI).addImm(PCLabelId);
1011 (*MIB).setMemRefs(Orig->memoperands_begin(), Orig->memoperands_end());
1018 ARMBaseInstrInfo::duplicate(MachineInstr *Orig, MachineFunction &MF) const {
1019 MachineInstr *MI = TargetInstrInfoImpl::duplicate(Orig, MF);
1020 switch(Orig->getOpcode()) {
1021 case ARM::tLDRpci_pic:
1022 case ARM::t2LDRpci_pic: {
1023 unsigned CPI = Orig->getOperand(1).getIndex();
1024 unsigned PCLabelId = duplicateCPV(MF, CPI);
1025 Orig->getOperand(1).setIndex(CPI);
1026 Orig->getOperand(2).setImm(PCLabelId);
1033 bool ARMBaseInstrInfo::produceSameValue(const MachineInstr *MI0,
1034 const MachineInstr *MI1) const {
1035 int Opcode = MI0->getOpcode();
1036 if (Opcode == ARM::t2LDRpci ||
1037 Opcode == ARM::t2LDRpci_pic ||
1038 Opcode == ARM::tLDRpci ||
1039 Opcode == ARM::tLDRpci_pic) {
1040 if (MI1->getOpcode() != Opcode)
1042 if (MI0->getNumOperands() != MI1->getNumOperands())
1045 const MachineOperand &MO0 = MI0->getOperand(1);
1046 const MachineOperand &MO1 = MI1->getOperand(1);
1047 if (MO0.getOffset() != MO1.getOffset())
1050 const MachineFunction *MF = MI0->getParent()->getParent();
1051 const MachineConstantPool *MCP = MF->getConstantPool();
1052 int CPI0 = MO0.getIndex();
1053 int CPI1 = MO1.getIndex();
1054 const MachineConstantPoolEntry &MCPE0 = MCP->getConstants()[CPI0];
1055 const MachineConstantPoolEntry &MCPE1 = MCP->getConstants()[CPI1];
1056 ARMConstantPoolValue *ACPV0 =
1057 static_cast<ARMConstantPoolValue*>(MCPE0.Val.MachineCPVal);
1058 ARMConstantPoolValue *ACPV1 =
1059 static_cast<ARMConstantPoolValue*>(MCPE1.Val.MachineCPVal);
1060 return ACPV0->hasSameValue(ACPV1);
1063 return MI0->isIdenticalTo(MI1, MachineInstr::IgnoreVRegDefs);
1066 /// areLoadsFromSameBasePtr - This is used by the pre-regalloc scheduler to
1067 /// determine if two loads are loading from the same base address. It should
1068 /// only return true if the base pointers are the same and the only differences
1069 /// between the two addresses is the offset. It also returns the offsets by
1071 bool ARMBaseInstrInfo::areLoadsFromSameBasePtr(SDNode *Load1, SDNode *Load2,
1073 int64_t &Offset2) const {
1074 // Don't worry about Thumb: just ARM and Thumb2.
1075 if (Subtarget.isThumb1Only()) return false;
1077 if (!Load1->isMachineOpcode() || !Load2->isMachineOpcode())
1080 switch (Load1->getMachineOpcode()) {
1093 case ARM::t2LDRSHi8:
1095 case ARM::t2LDRSHi12:
1099 switch (Load2->getMachineOpcode()) {
1112 case ARM::t2LDRSHi8:
1114 case ARM::t2LDRSHi12:
1118 // Check if base addresses and chain operands match.
1119 if (Load1->getOperand(0) != Load2->getOperand(0) ||
1120 Load1->getOperand(4) != Load2->getOperand(4))
1123 // Index should be Reg0.
1124 if (Load1->getOperand(3) != Load2->getOperand(3))
1127 // Determine the offsets.
1128 if (isa<ConstantSDNode>(Load1->getOperand(1)) &&
1129 isa<ConstantSDNode>(Load2->getOperand(1))) {
1130 Offset1 = cast<ConstantSDNode>(Load1->getOperand(1))->getSExtValue();
1131 Offset2 = cast<ConstantSDNode>(Load2->getOperand(1))->getSExtValue();
1138 /// shouldScheduleLoadsNear - This is a used by the pre-regalloc scheduler to
1139 /// determine (in conjuction with areLoadsFromSameBasePtr) if two loads should
1140 /// be scheduled togther. On some targets if two loads are loading from
1141 /// addresses in the same cache line, it's better if they are scheduled
1142 /// together. This function takes two integers that represent the load offsets
1143 /// from the common base address. It returns true if it decides it's desirable
1144 /// to schedule the two loads together. "NumLoads" is the number of loads that
1145 /// have already been scheduled after Load1.
1146 bool ARMBaseInstrInfo::shouldScheduleLoadsNear(SDNode *Load1, SDNode *Load2,
1147 int64_t Offset1, int64_t Offset2,
1148 unsigned NumLoads) const {
1149 // Don't worry about Thumb: just ARM and Thumb2.
1150 if (Subtarget.isThumb1Only()) return false;
1152 assert(Offset2 > Offset1);
1154 if ((Offset2 - Offset1) / 8 > 64)
1157 if (Load1->getMachineOpcode() != Load2->getMachineOpcode())
1158 return false; // FIXME: overly conservative?
1160 // Four loads in a row should be sufficient.
1167 bool ARMBaseInstrInfo::isSchedulingBoundary(const MachineInstr *MI,
1168 const MachineBasicBlock *MBB,
1169 const MachineFunction &MF) const {
1170 // Debug info is never a scheduling boundary. It's necessary to be explicit
1171 // due to the special treatment of IT instructions below, otherwise a
1172 // dbg_value followed by an IT will result in the IT instruction being
1173 // considered a scheduling hazard, which is wrong. It should be the actual
1174 // instruction preceding the dbg_value instruction(s), just like it is
1175 // when debug info is not present.
1176 if (MI->isDebugValue())
1179 // Terminators and labels can't be scheduled around.
1180 if (MI->getDesc().isTerminator() || MI->isLabel())
1183 // Treat the start of the IT block as a scheduling boundary, but schedule
1184 // t2IT along with all instructions following it.
1185 // FIXME: This is a big hammer. But the alternative is to add all potential
1186 // true and anti dependencies to IT block instructions as implicit operands
1187 // to the t2IT instruction. The added compile time and complexity does not
1189 MachineBasicBlock::const_iterator I = MI;
1190 // Make sure to skip any dbg_value instructions
1191 while (++I != MBB->end() && I->isDebugValue())
1193 if (I != MBB->end() && I->getOpcode() == ARM::t2IT)
1196 // Don't attempt to schedule around any instruction that defines
1197 // a stack-oriented pointer, as it's unlikely to be profitable. This
1198 // saves compile time, because it doesn't require every single
1199 // stack slot reference to depend on the instruction that does the
1201 if (MI->definesRegister(ARM::SP))
1207 bool ARMBaseInstrInfo::isProfitableToIfCvt(MachineBasicBlock &MBB,
1210 float Confidence) const {
1214 // Use old-style heuristics
1216 if (Subtarget.getCPUString() == "generic")
1217 // Generic (and overly aggressive) if-conversion limits for testing.
1218 return NumInstrs <= 10;
1219 if (Subtarget.hasV7Ops())
1220 return NumInstrs <= 3;
1221 return NumInstrs <= 2;
1224 // Attempt to estimate the relative costs of predication versus branching.
1225 float UnpredCost = Probability * NumInstrs;
1226 UnpredCost += 1.0; // The branch itself
1227 UnpredCost += (1.0 - Confidence) * Subtarget.getMispredictionPenalty();
1229 float PredCost = NumInstrs;
1231 return PredCost < UnpredCost;
1235 bool ARMBaseInstrInfo::
1236 isProfitableToIfCvt(MachineBasicBlock &TMBB, unsigned NumT,
1237 MachineBasicBlock &FMBB, unsigned NumF,
1238 float Probability, float Confidence) const {
1239 // Use old-style if-conversion heuristics
1241 return NumT && NumF && NumT <= 2 && NumF <= 2;
1247 // Attempt to estimate the relative costs of predication versus branching.
1248 float UnpredCost = Probability * NumT + (1.0 - Probability) * NumF;
1249 UnpredCost += 1.0; // The branch itself
1250 UnpredCost += (1.0 - Confidence) * Subtarget.getMispredictionPenalty();
1252 float PredCost = NumT + NumF;
1254 return PredCost < UnpredCost;
1257 /// getInstrPredicate - If instruction is predicated, returns its predicate
1258 /// condition, otherwise returns AL. It also returns the condition code
1259 /// register by reference.
1261 llvm::getInstrPredicate(const MachineInstr *MI, unsigned &PredReg) {
1262 int PIdx = MI->findFirstPredOperandIdx();
1268 PredReg = MI->getOperand(PIdx+1).getReg();
1269 return (ARMCC::CondCodes)MI->getOperand(PIdx).getImm();
1273 int llvm::getMatchingCondBranchOpcode(int Opc) {
1276 else if (Opc == ARM::tB)
1278 else if (Opc == ARM::t2B)
1281 llvm_unreachable("Unknown unconditional branch opcode!");
1286 void llvm::emitARMRegPlusImmediate(MachineBasicBlock &MBB,
1287 MachineBasicBlock::iterator &MBBI, DebugLoc dl,
1288 unsigned DestReg, unsigned BaseReg, int NumBytes,
1289 ARMCC::CondCodes Pred, unsigned PredReg,
1290 const ARMBaseInstrInfo &TII) {
1291 bool isSub = NumBytes < 0;
1292 if (isSub) NumBytes = -NumBytes;
1295 unsigned RotAmt = ARM_AM::getSOImmValRotate(NumBytes);
1296 unsigned ThisVal = NumBytes & ARM_AM::rotr32(0xFF, RotAmt);
1297 assert(ThisVal && "Didn't extract field correctly");
1299 // We will handle these bits from offset, clear them.
1300 NumBytes &= ~ThisVal;
1302 assert(ARM_AM::getSOImmVal(ThisVal) != -1 && "Bit extraction didn't work?");
1304 // Build the new ADD / SUB.
1305 unsigned Opc = isSub ? ARM::SUBri : ARM::ADDri;
1306 BuildMI(MBB, MBBI, dl, TII.get(Opc), DestReg)
1307 .addReg(BaseReg, RegState::Kill).addImm(ThisVal)
1308 .addImm((unsigned)Pred).addReg(PredReg).addReg(0);
1313 bool llvm::rewriteARMFrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
1314 unsigned FrameReg, int &Offset,
1315 const ARMBaseInstrInfo &TII) {
1316 unsigned Opcode = MI.getOpcode();
1317 const TargetInstrDesc &Desc = MI.getDesc();
1318 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
1321 // Memory operands in inline assembly always use AddrMode2.
1322 if (Opcode == ARM::INLINEASM)
1323 AddrMode = ARMII::AddrMode2;
1325 if (Opcode == ARM::ADDri) {
1326 Offset += MI.getOperand(FrameRegIdx+1).getImm();
1328 // Turn it into a move.
1329 MI.setDesc(TII.get(ARM::MOVr));
1330 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
1331 MI.RemoveOperand(FrameRegIdx+1);
1334 } else if (Offset < 0) {
1337 MI.setDesc(TII.get(ARM::SUBri));
1340 // Common case: small offset, fits into instruction.
1341 if (ARM_AM::getSOImmVal(Offset) != -1) {
1342 // Replace the FrameIndex with sp / fp
1343 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
1344 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(Offset);
1349 // Otherwise, pull as much of the immedidate into this ADDri/SUBri
1351 unsigned RotAmt = ARM_AM::getSOImmValRotate(Offset);
1352 unsigned ThisImmVal = Offset & ARM_AM::rotr32(0xFF, RotAmt);
1354 // We will handle these bits from offset, clear them.
1355 Offset &= ~ThisImmVal;
1357 // Get the properly encoded SOImmVal field.
1358 assert(ARM_AM::getSOImmVal(ThisImmVal) != -1 &&
1359 "Bit extraction didn't work?");
1360 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(ThisImmVal);
1362 unsigned ImmIdx = 0;
1364 unsigned NumBits = 0;
1367 case ARMII::AddrMode_i12: {
1368 ImmIdx = FrameRegIdx + 1;
1369 InstrOffs = MI.getOperand(ImmIdx).getImm();
1373 case ARMII::AddrMode2: {
1374 ImmIdx = FrameRegIdx+2;
1375 InstrOffs = ARM_AM::getAM2Offset(MI.getOperand(ImmIdx).getImm());
1376 if (ARM_AM::getAM2Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
1381 case ARMII::AddrMode3: {
1382 ImmIdx = FrameRegIdx+2;
1383 InstrOffs = ARM_AM::getAM3Offset(MI.getOperand(ImmIdx).getImm());
1384 if (ARM_AM::getAM3Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
1389 case ARMII::AddrMode4:
1390 case ARMII::AddrMode6:
1391 // Can't fold any offset even if it's zero.
1393 case ARMII::AddrMode5: {
1394 ImmIdx = FrameRegIdx+1;
1395 InstrOffs = ARM_AM::getAM5Offset(MI.getOperand(ImmIdx).getImm());
1396 if (ARM_AM::getAM5Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
1403 llvm_unreachable("Unsupported addressing mode!");
1407 Offset += InstrOffs * Scale;
1408 assert((Offset & (Scale-1)) == 0 && "Can't encode this offset!");
1414 // Attempt to fold address comp. if opcode has offset bits
1416 // Common case: small offset, fits into instruction.
1417 MachineOperand &ImmOp = MI.getOperand(ImmIdx);
1418 int ImmedOffset = Offset / Scale;
1419 unsigned Mask = (1 << NumBits) - 1;
1420 if ((unsigned)Offset <= Mask * Scale) {
1421 // Replace the FrameIndex with sp
1422 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
1423 // FIXME: When addrmode2 goes away, this will simplify (like the
1424 // T2 version), as the LDR.i12 versions don't need the encoding
1425 // tricks for the offset value.
1427 if (AddrMode == ARMII::AddrMode_i12)
1428 ImmedOffset = -ImmedOffset;
1430 ImmedOffset |= 1 << NumBits;
1432 ImmOp.ChangeToImmediate(ImmedOffset);
1437 // Otherwise, it didn't fit. Pull in what we can to simplify the immed.
1438 ImmedOffset = ImmedOffset & Mask;
1440 if (AddrMode == ARMII::AddrMode_i12)
1441 ImmedOffset = -ImmedOffset;
1443 ImmedOffset |= 1 << NumBits;
1445 ImmOp.ChangeToImmediate(ImmedOffset);
1446 Offset &= ~(Mask*Scale);
1450 Offset = (isSub) ? -Offset : Offset;
1454 bool ARMBaseInstrInfo::
1455 AnalyzeCompare(const MachineInstr *MI, unsigned &SrcReg, int &CmpMask,
1456 int &CmpValue) const {
1457 switch (MI->getOpcode()) {
1463 SrcReg = MI->getOperand(0).getReg();
1465 CmpValue = MI->getOperand(1).getImm();
1469 SrcReg = MI->getOperand(0).getReg();
1470 CmpMask = MI->getOperand(1).getImm();
1478 /// isSuitableForMask - Identify a suitable 'and' instruction that
1479 /// operates on the given source register and applies the same mask
1480 /// as a 'tst' instruction. Provide a limited look-through for copies.
1481 /// When successful, MI will hold the found instruction.
1482 static bool isSuitableForMask(MachineInstr *&MI, unsigned SrcReg,
1483 int CmpMask, bool CommonUse) {
1484 switch (MI->getOpcode()) {
1487 if (CmpMask != MI->getOperand(2).getImm())
1489 if (SrcReg == MI->getOperand(CommonUse ? 1 : 0).getReg())
1493 // Walk down one instruction which is potentially an 'and'.
1494 const MachineInstr &Copy = *MI;
1495 MachineBasicBlock::iterator AND(
1496 llvm::next(MachineBasicBlock::iterator(MI)));
1497 if (AND == MI->getParent()->end()) return false;
1499 return isSuitableForMask(MI, Copy.getOperand(0).getReg(),
1507 /// OptimizeCompareInstr - Convert the instruction supplying the argument to the
1508 /// comparison into one that sets the zero bit in the flags register. Update the
1509 /// iterator *only* if a transformation took place.
1510 bool ARMBaseInstrInfo::
1511 OptimizeCompareInstr(MachineInstr *CmpInstr, unsigned SrcReg, int CmpMask,
1512 int CmpValue, const MachineRegisterInfo *MRI,
1513 MachineBasicBlock::iterator &MII) const {
1517 MachineRegisterInfo::def_iterator DI = MRI->def_begin(SrcReg);
1518 if (llvm::next(DI) != MRI->def_end())
1519 // Only support one definition.
1522 MachineInstr *MI = &*DI;
1524 // Masked compares sometimes use the same register as the corresponding 'and'.
1525 if (CmpMask != ~0) {
1526 if (!isSuitableForMask(MI, SrcReg, CmpMask, false)) {
1528 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(SrcReg),
1529 UE = MRI->use_end(); UI != UE; ++UI) {
1530 if (UI->getParent() != CmpInstr->getParent()) continue;
1531 MachineInstr *PotentialAND = &*UI;
1532 if (!isSuitableForMask(PotentialAND, SrcReg, CmpMask, true))
1537 if (!MI) return false;
1541 // Conservatively refuse to convert an instruction which isn't in the same BB
1542 // as the comparison.
1543 if (MI->getParent() != CmpInstr->getParent())
1546 // Check that CPSR isn't set between the comparison instruction and the one we
1548 MachineBasicBlock::const_iterator I = CmpInstr, E = MI,
1549 B = MI->getParent()->begin();
1551 // Early exit if CmpInstr is at the beginning of the BB.
1552 if (I == B) return false;
1555 for (; I != E; --I) {
1556 const MachineInstr &Instr = *I;
1558 for (unsigned IO = 0, EO = Instr.getNumOperands(); IO != EO; ++IO) {
1559 const MachineOperand &MO = Instr.getOperand(IO);
1560 if (!MO.isReg() || !MO.isDef()) continue;
1562 // This instruction modifies CPSR before the one we want to change. We
1563 // can't do this transformation.
1564 if (MO.getReg() == ARM::CPSR)
1569 // The 'and' is below the comparison instruction.
1573 // Set the "zero" bit in CPSR.
1574 switch (MI->getOpcode()) {
1582 MI->RemoveOperand(5);
1583 MachineInstrBuilder(MI)
1584 .addReg(ARM::CPSR, RegState::Define | RegState::Implicit);
1585 MII = llvm::next(MachineBasicBlock::iterator(CmpInstr));
1586 CmpInstr->eraseFromParent();
1594 ARMBaseInstrInfo::getNumMicroOps(const MachineInstr *MI,
1595 const InstrItineraryData *ItinData) const {
1596 if (!ItinData || ItinData->isEmpty())
1599 const TargetInstrDesc &Desc = MI->getDesc();
1600 unsigned Class = Desc.getSchedClass();
1601 unsigned UOps = ItinData->Itineraries[Class].NumMicroOps;
1605 unsigned Opc = MI->getOpcode();
1608 llvm_unreachable("Unexpected multi-uops instruction!");
1614 // The number of uOps for load / store multiple are determined by the number
1616 // On Cortex-A8, each pair of register loads / stores can be scheduled on the
1617 // same cycle. The scheduling for the first load / store must be done
1618 // separately by assuming the the address is not 64-bit aligned.
1619 // On Cortex-A9, the formula is simply (#reg / 2) + (#reg % 2). If the address
1620 // is not 64-bit aligned, then AGU would take an extra cycle.
1621 // For VFP / NEON load / store multiple, the formula is
1622 // (#reg / 2) + (#reg % 2) + 1.
1625 case ARM::VLDMD_UPD:
1626 case ARM::VLDMS_UPD:
1629 case ARM::VSTMD_UPD:
1630 case ARM::VSTMS_UPD: {
1631 unsigned NumRegs = MI->getNumOperands() - Desc.getNumOperands();
1632 return (NumRegs / 2) + (NumRegs % 2) + 1;
1645 case ARM::t2LDM_RET:
1647 case ARM::t2LDM_UPD:
1649 case ARM::t2STM_UPD: {
1650 unsigned NumRegs = MI->getNumOperands() - Desc.getNumOperands() + 1;
1651 if (Subtarget.isCortexA8()) {
1652 // 4 registers would be issued: 1, 2, 1.
1653 // 5 registers would be issued: 1, 2, 2.
1654 return 1 + (NumRegs / 2);
1655 } else if (Subtarget.isCortexA9()) {
1656 UOps = (NumRegs / 2);
1657 // If there are odd number of registers or if it's not 64-bit aligned,
1658 // then it takes an extra AGU (Address Generation Unit) cycle.
1659 if ((NumRegs % 2) ||
1660 !MI->hasOneMemOperand() ||
1661 (*MI->memoperands_begin())->getAlignment() < 8)
1665 // Assume the worst.
1673 ARMBaseInstrInfo::getVLDMDefCycle(const InstrItineraryData *ItinData,
1674 const TargetInstrDesc &DefTID,
1676 unsigned DefIdx, unsigned DefAlign) const {
1677 int RegNo = (int)(DefIdx+1) - DefTID.getNumOperands() + 1;
1679 // Def is the address writeback.
1680 return ItinData->getOperandCycle(DefClass, DefIdx);
1683 if (Subtarget.isCortexA8()) {
1684 // (regno / 2) + (regno % 2) + 1
1685 DefCycle = RegNo / 2 + 1;
1688 } else if (Subtarget.isCortexA9()) {
1690 bool isSLoad = false;
1691 switch (DefTID.getOpcode()) {
1694 case ARM::VLDMS_UPD:
1698 // If there are odd number of 'S' registers or if it's not 64-bit aligned,
1699 // then it takes an extra cycle.
1700 if ((isSLoad && (RegNo % 2)) || DefAlign < 8)
1703 // Assume the worst.
1704 DefCycle = RegNo + 2;
1711 ARMBaseInstrInfo::getLDMDefCycle(const InstrItineraryData *ItinData,
1712 const TargetInstrDesc &DefTID,
1714 unsigned DefIdx, unsigned DefAlign) const {
1715 int RegNo = (int)(DefIdx+1) - DefTID.getNumOperands() + 1;
1717 // Def is the address writeback.
1718 return ItinData->getOperandCycle(DefClass, DefIdx);
1721 if (Subtarget.isCortexA8()) {
1722 // 4 registers would be issued: 1, 2, 1.
1723 // 5 registers would be issued: 1, 2, 2.
1724 DefCycle = RegNo / 2;
1727 // Result latency is issue cycle + 2: E2.
1729 } else if (Subtarget.isCortexA9()) {
1730 DefCycle = (RegNo / 2);
1731 // If there are odd number of registers or if it's not 64-bit aligned,
1732 // then it takes an extra AGU (Address Generation Unit) cycle.
1733 if ((RegNo % 2) || DefAlign < 8)
1735 // Result latency is AGU cycles + 2.
1738 // Assume the worst.
1739 DefCycle = RegNo + 2;
1746 ARMBaseInstrInfo::getVSTMUseCycle(const InstrItineraryData *ItinData,
1747 const TargetInstrDesc &UseTID,
1749 unsigned UseIdx, unsigned UseAlign) const {
1750 int RegNo = (int)(UseIdx+1) - UseTID.getNumOperands() + 1;
1752 return ItinData->getOperandCycle(UseClass, UseIdx);
1755 if (Subtarget.isCortexA8()) {
1756 // (regno / 2) + (regno % 2) + 1
1757 UseCycle = RegNo / 2 + 1;
1760 } else if (Subtarget.isCortexA9()) {
1762 bool isSStore = false;
1763 switch (UseTID.getOpcode()) {
1766 case ARM::VSTMS_UPD:
1770 // If there are odd number of 'S' registers or if it's not 64-bit aligned,
1771 // then it takes an extra cycle.
1772 if ((isSStore && (RegNo % 2)) || UseAlign < 8)
1775 // Assume the worst.
1776 UseCycle = RegNo + 2;
1783 ARMBaseInstrInfo::getSTMUseCycle(const InstrItineraryData *ItinData,
1784 const TargetInstrDesc &UseTID,
1786 unsigned UseIdx, unsigned UseAlign) const {
1787 int RegNo = (int)(UseIdx+1) - UseTID.getNumOperands() + 1;
1789 return ItinData->getOperandCycle(UseClass, UseIdx);
1792 if (Subtarget.isCortexA8()) {
1793 UseCycle = RegNo / 2;
1798 } else if (Subtarget.isCortexA9()) {
1799 UseCycle = (RegNo / 2);
1800 // If there are odd number of registers or if it's not 64-bit aligned,
1801 // then it takes an extra AGU (Address Generation Unit) cycle.
1802 if ((RegNo % 2) || UseAlign < 8)
1805 // Assume the worst.
1812 ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
1813 const TargetInstrDesc &DefTID,
1814 unsigned DefIdx, unsigned DefAlign,
1815 const TargetInstrDesc &UseTID,
1816 unsigned UseIdx, unsigned UseAlign) const {
1817 unsigned DefClass = DefTID.getSchedClass();
1818 unsigned UseClass = UseTID.getSchedClass();
1820 if (DefIdx < DefTID.getNumDefs() && UseIdx < UseTID.getNumOperands())
1821 return ItinData->getOperandLatency(DefClass, DefIdx, UseClass, UseIdx);
1823 // This may be a def / use of a variable_ops instruction, the operand
1824 // latency might be determinable dynamically. Let the target try to
1827 bool LdmBypass = false;
1828 switch (DefTID.getOpcode()) {
1830 DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
1834 case ARM::VLDMD_UPD:
1835 case ARM::VLDMS_UPD: {
1836 DefCycle = getVLDMDefCycle(ItinData, DefTID, DefClass, DefIdx, DefAlign);
1845 case ARM::t2LDM_RET:
1847 case ARM::t2LDM_UPD: {
1849 DefCycle = getLDMDefCycle(ItinData, DefTID, DefClass, DefIdx, DefAlign);
1855 // We can't seem to determine the result latency of the def, assume it's 2.
1859 switch (UseTID.getOpcode()) {
1861 UseCycle = ItinData->getOperandCycle(UseClass, UseIdx);
1865 case ARM::VSTMD_UPD:
1866 case ARM::VSTMS_UPD: {
1867 UseCycle = getVSTMUseCycle(ItinData, UseTID, UseClass, UseIdx, UseAlign);
1876 case ARM::t2STM_UPD: {
1877 UseCycle = getSTMUseCycle(ItinData, UseTID, UseClass, UseIdx, UseAlign);
1883 // Assume it's read in the first stage.
1886 UseCycle = DefCycle - UseCycle + 1;
1889 // It's a variable_ops instruction so we can't use DefIdx here. Just use
1890 // first def operand.
1891 if (ItinData->hasPipelineForwarding(DefClass, DefTID.getNumOperands()-1,
1894 } else if (ItinData->hasPipelineForwarding(DefClass, DefIdx,
1903 ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
1904 const MachineInstr *DefMI, unsigned DefIdx,
1905 const MachineInstr *UseMI, unsigned UseIdx) const {
1906 if (DefMI->isCopyLike() || DefMI->isInsertSubreg() ||
1907 DefMI->isRegSequence() || DefMI->isImplicitDef())
1910 const TargetInstrDesc &DefTID = DefMI->getDesc();
1911 if (!ItinData || ItinData->isEmpty())
1912 return DefTID.mayLoad() ? 3 : 1;
1915 const TargetInstrDesc &UseTID = UseMI->getDesc();
1916 const MachineOperand &DefMO = DefMI->getOperand(DefIdx);
1917 if (DefMO.getReg() == ARM::CPSR && UseTID.isBranch())
1918 // CPSR set and branch can be paired in the same cycle.
1921 unsigned DefAlign = DefMI->hasOneMemOperand()
1922 ? (*DefMI->memoperands_begin())->getAlignment() : 0;
1923 unsigned UseAlign = UseMI->hasOneMemOperand()
1924 ? (*UseMI->memoperands_begin())->getAlignment() : 0;
1925 int Latency = getOperandLatency(ItinData, DefTID, DefIdx, DefAlign,
1926 UseTID, UseIdx, UseAlign);
1929 (Subtarget.isCortexA8() || Subtarget.isCortexA9())) {
1930 // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2]
1931 // variants are one cycle cheaper.
1932 switch (DefTID.getOpcode()) {
1936 unsigned ShOpVal = DefMI->getOperand(3).getImm();
1937 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
1939 (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
1946 case ARM::t2LDRSHs: {
1947 // Thumb2 mode: lsl only.
1948 unsigned ShAmt = DefMI->getOperand(3).getImm();
1949 if (ShAmt == 0 || ShAmt == 2)
1960 ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
1961 SDNode *DefNode, unsigned DefIdx,
1962 SDNode *UseNode, unsigned UseIdx) const {
1963 if (!DefNode->isMachineOpcode())
1966 const TargetInstrDesc &DefTID = get(DefNode->getMachineOpcode());
1967 if (!ItinData || ItinData->isEmpty())
1968 return DefTID.mayLoad() ? 3 : 1;
1970 if (!UseNode->isMachineOpcode())
1971 return ItinData->getOperandCycle(DefTID.getSchedClass(), DefIdx);
1973 const TargetInstrDesc &UseTID = get(UseNode->getMachineOpcode());
1974 const MachineSDNode *DefMN = dyn_cast<MachineSDNode>(DefNode);
1975 unsigned DefAlign = !DefMN->memoperands_empty()
1976 ? (*DefMN->memoperands_begin())->getAlignment() : 0;
1977 const MachineSDNode *UseMN = dyn_cast<MachineSDNode>(UseNode);
1978 unsigned UseAlign = !UseMN->memoperands_empty()
1979 ? (*UseMN->memoperands_begin())->getAlignment() : 0;
1980 int Latency = getOperandLatency(ItinData, DefTID, DefIdx, DefAlign,
1981 UseTID, UseIdx, UseAlign);
1984 (Subtarget.isCortexA8() || Subtarget.isCortexA9())) {
1985 // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2]
1986 // variants are one cycle cheaper.
1987 switch (DefTID.getOpcode()) {
1992 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
1993 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
1995 (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
2002 case ARM::t2LDRSHs: {
2003 // Thumb2 mode: lsl only.
2005 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
2006 if (ShAmt == 0 || ShAmt == 2)
2016 bool ARMBaseInstrInfo::
2017 hasHighOperandLatency(const InstrItineraryData *ItinData,
2018 const MachineRegisterInfo *MRI,
2019 const MachineInstr *DefMI, unsigned DefIdx,
2020 const MachineInstr *UseMI, unsigned UseIdx) const {
2021 unsigned DDomain = DefMI->getDesc().TSFlags & ARMII::DomainMask;
2022 unsigned UDomain = UseMI->getDesc().TSFlags & ARMII::DomainMask;
2023 if (Subtarget.isCortexA8() &&
2024 (DDomain == ARMII::DomainVFP || UDomain == ARMII::DomainVFP))
2025 // CortexA8 VFP instructions are not pipelined.
2028 // Hoist VFP / NEON instructions with 4 or higher latency.
2029 int Latency = getOperandLatency(ItinData, DefMI, DefIdx, UseMI, UseIdx);
2032 return DDomain == ARMII::DomainVFP || DDomain == ARMII::DomainNEON ||
2033 UDomain == ARMII::DomainVFP || UDomain == ARMII::DomainNEON;
2036 bool ARMBaseInstrInfo::
2037 hasLowDefLatency(const InstrItineraryData *ItinData,
2038 const MachineInstr *DefMI, unsigned DefIdx) const {
2039 if (!ItinData || ItinData->isEmpty())
2042 unsigned DDomain = DefMI->getDesc().TSFlags & ARMII::DomainMask;
2043 if (DDomain == ARMII::DomainGeneral) {
2044 unsigned DefClass = DefMI->getDesc().getSchedClass();
2045 int DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
2046 return (DefCycle != -1 && DefCycle <= 2);