1 //===-- ARMBaseInstrInfo.cpp - ARM Instruction Information ----------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file contains the Base ARM implementation of the TargetInstrInfo class.
11 //===----------------------------------------------------------------------===//
13 #include "ARMBaseInstrInfo.h"
14 #include "ARMBaseRegisterInfo.h"
15 #include "ARMConstantPoolValue.h"
16 #include "ARMFeatures.h"
17 #include "ARMHazardRecognizer.h"
18 #include "ARMMachineFunctionInfo.h"
19 #include "ARMSubtarget.h"
20 #include "MCTargetDesc/ARMAddressingModes.h"
21 #include "MCTargetDesc/ARMBaseInfo.h"
22 #include "llvm/ADT/DenseMap.h"
23 #include "llvm/ADT/STLExtras.h"
24 #include "llvm/ADT/SmallSet.h"
25 #include "llvm/ADT/SmallVector.h"
26 #include "llvm/ADT/Triple.h"
27 #include "llvm/CodeGen/LiveVariables.h"
28 #include "llvm/CodeGen/MachineBasicBlock.h"
29 #include "llvm/CodeGen/MachineConstantPool.h"
30 #include "llvm/CodeGen/MachineFrameInfo.h"
31 #include "llvm/CodeGen/MachineFunction.h"
32 #include "llvm/CodeGen/MachineInstr.h"
33 #include "llvm/CodeGen/MachineInstrBuilder.h"
34 #include "llvm/CodeGen/MachineMemOperand.h"
35 #include "llvm/CodeGen/MachineOperand.h"
36 #include "llvm/CodeGen/MachineRegisterInfo.h"
37 #include "llvm/CodeGen/ScoreboardHazardRecognizer.h"
38 #include "llvm/CodeGen/SelectionDAGNodes.h"
39 #include "llvm/CodeGen/TargetInstrInfo.h"
40 #include "llvm/CodeGen/TargetRegisterInfo.h"
41 #include "llvm/CodeGen/TargetSchedule.h"
42 #include "llvm/IR/Attributes.h"
43 #include "llvm/IR/Constants.h"
44 #include "llvm/IR/DebugLoc.h"
45 #include "llvm/IR/Function.h"
46 #include "llvm/IR/GlobalValue.h"
47 #include "llvm/MC/MCAsmInfo.h"
48 #include "llvm/MC/MCInstrDesc.h"
49 #include "llvm/MC/MCInstrItineraries.h"
50 #include "llvm/Support/BranchProbability.h"
51 #include "llvm/Support/Casting.h"
52 #include "llvm/Support/CommandLine.h"
53 #include "llvm/Support/Compiler.h"
54 #include "llvm/Support/Debug.h"
55 #include "llvm/Support/ErrorHandling.h"
56 #include "llvm/Support/raw_ostream.h"
57 #include "llvm/Target/TargetMachine.h"
68 #define DEBUG_TYPE "arm-instrinfo"
70 #define GET_INSTRINFO_CTOR_DTOR
71 #include "ARMGenInstrInfo.inc"
74 EnableARM3Addr("enable-arm-3-addr-conv", cl::Hidden
,
75 cl::desc("Enable ARM 2-addr to 3-addr conv"));
77 /// ARM_MLxEntry - Record information about MLA / MLS instructions.
79 uint16_t MLxOpc
; // MLA / MLS opcode
80 uint16_t MulOpc
; // Expanded multiplication opcode
81 uint16_t AddSubOpc
; // Expanded add / sub opcode
82 bool NegAcc
; // True if the acc is negated before the add / sub.
83 bool HasLane
; // True if instruction has an extra "lane" operand.
86 static const ARM_MLxEntry ARM_MLxTable
[] = {
87 // MLxOpc, MulOpc, AddSubOpc, NegAcc, HasLane
89 { ARM::VMLAS
, ARM::VMULS
, ARM::VADDS
, false, false },
90 { ARM::VMLSS
, ARM::VMULS
, ARM::VSUBS
, false, false },
91 { ARM::VMLAD
, ARM::VMULD
, ARM::VADDD
, false, false },
92 { ARM::VMLSD
, ARM::VMULD
, ARM::VSUBD
, false, false },
93 { ARM::VNMLAS
, ARM::VNMULS
, ARM::VSUBS
, true, false },
94 { ARM::VNMLSS
, ARM::VMULS
, ARM::VSUBS
, true, false },
95 { ARM::VNMLAD
, ARM::VNMULD
, ARM::VSUBD
, true, false },
96 { ARM::VNMLSD
, ARM::VMULD
, ARM::VSUBD
, true, false },
99 { ARM::VMLAfd
, ARM::VMULfd
, ARM::VADDfd
, false, false },
100 { ARM::VMLSfd
, ARM::VMULfd
, ARM::VSUBfd
, false, false },
101 { ARM::VMLAfq
, ARM::VMULfq
, ARM::VADDfq
, false, false },
102 { ARM::VMLSfq
, ARM::VMULfq
, ARM::VSUBfq
, false, false },
103 { ARM::VMLAslfd
, ARM::VMULslfd
, ARM::VADDfd
, false, true },
104 { ARM::VMLSslfd
, ARM::VMULslfd
, ARM::VSUBfd
, false, true },
105 { ARM::VMLAslfq
, ARM::VMULslfq
, ARM::VADDfq
, false, true },
106 { ARM::VMLSslfq
, ARM::VMULslfq
, ARM::VSUBfq
, false, true },
109 ARMBaseInstrInfo::ARMBaseInstrInfo(const ARMSubtarget
& STI
)
110 : ARMGenInstrInfo(ARM::ADJCALLSTACKDOWN
, ARM::ADJCALLSTACKUP
),
112 for (unsigned i
= 0, e
= array_lengthof(ARM_MLxTable
); i
!= e
; ++i
) {
113 if (!MLxEntryMap
.insert(std::make_pair(ARM_MLxTable
[i
].MLxOpc
, i
)).second
)
114 llvm_unreachable("Duplicated entries?");
115 MLxHazardOpcodes
.insert(ARM_MLxTable
[i
].AddSubOpc
);
116 MLxHazardOpcodes
.insert(ARM_MLxTable
[i
].MulOpc
);
120 // Use a ScoreboardHazardRecognizer for prepass ARM scheduling. TargetInstrImpl
121 // currently defaults to no prepass hazard recognizer.
122 ScheduleHazardRecognizer
*
123 ARMBaseInstrInfo::CreateTargetHazardRecognizer(const TargetSubtargetInfo
*STI
,
124 const ScheduleDAG
*DAG
) const {
125 if (usePreRAHazardRecognizer()) {
126 const InstrItineraryData
*II
=
127 static_cast<const ARMSubtarget
*>(STI
)->getInstrItineraryData();
128 return new ScoreboardHazardRecognizer(II
, DAG
, "pre-RA-sched");
130 return TargetInstrInfo::CreateTargetHazardRecognizer(STI
, DAG
);
133 ScheduleHazardRecognizer
*ARMBaseInstrInfo::
134 CreateTargetPostRAHazardRecognizer(const InstrItineraryData
*II
,
135 const ScheduleDAG
*DAG
) const {
136 if (Subtarget
.isThumb2() || Subtarget
.hasVFP2())
137 return (ScheduleHazardRecognizer
*)new ARMHazardRecognizer(II
, DAG
);
138 return TargetInstrInfo::CreateTargetPostRAHazardRecognizer(II
, DAG
);
141 MachineInstr
*ARMBaseInstrInfo::convertToThreeAddress(
142 MachineFunction::iterator
&MFI
, MachineInstr
&MI
, LiveVariables
*LV
) const {
143 // FIXME: Thumb2 support.
148 MachineFunction
&MF
= *MI
.getParent()->getParent();
149 uint64_t TSFlags
= MI
.getDesc().TSFlags
;
151 switch ((TSFlags
& ARMII::IndexModeMask
) >> ARMII::IndexModeShift
) {
152 default: return nullptr;
153 case ARMII::IndexModePre
:
156 case ARMII::IndexModePost
:
160 // Try splitting an indexed load/store to an un-indexed one plus an add/sub
162 unsigned MemOpc
= getUnindexedOpcode(MI
.getOpcode());
166 MachineInstr
*UpdateMI
= nullptr;
167 MachineInstr
*MemMI
= nullptr;
168 unsigned AddrMode
= (TSFlags
& ARMII::AddrModeMask
);
169 const MCInstrDesc
&MCID
= MI
.getDesc();
170 unsigned NumOps
= MCID
.getNumOperands();
171 bool isLoad
= !MI
.mayStore();
172 const MachineOperand
&WB
= isLoad
? MI
.getOperand(1) : MI
.getOperand(0);
173 const MachineOperand
&Base
= MI
.getOperand(2);
174 const MachineOperand
&Offset
= MI
.getOperand(NumOps
- 3);
175 unsigned WBReg
= WB
.getReg();
176 unsigned BaseReg
= Base
.getReg();
177 unsigned OffReg
= Offset
.getReg();
178 unsigned OffImm
= MI
.getOperand(NumOps
- 2).getImm();
179 ARMCC::CondCodes Pred
= (ARMCC::CondCodes
)MI
.getOperand(NumOps
- 1).getImm();
181 default: llvm_unreachable("Unknown indexed op!");
182 case ARMII::AddrMode2
: {
183 bool isSub
= ARM_AM::getAM2Op(OffImm
) == ARM_AM::sub
;
184 unsigned Amt
= ARM_AM::getAM2Offset(OffImm
);
186 if (ARM_AM::getSOImmVal(Amt
) == -1)
187 // Can't encode it in a so_imm operand. This transformation will
188 // add more than 1 instruction. Abandon!
190 UpdateMI
= BuildMI(MF
, MI
.getDebugLoc(),
191 get(isSub
? ARM::SUBri
: ARM::ADDri
), WBReg
)
196 } else if (Amt
!= 0) {
197 ARM_AM::ShiftOpc ShOpc
= ARM_AM::getAM2ShiftOpc(OffImm
);
198 unsigned SOOpc
= ARM_AM::getSORegOpc(ShOpc
, Amt
);
199 UpdateMI
= BuildMI(MF
, MI
.getDebugLoc(),
200 get(isSub
? ARM::SUBrsi
: ARM::ADDrsi
), WBReg
)
208 UpdateMI
= BuildMI(MF
, MI
.getDebugLoc(),
209 get(isSub
? ARM::SUBrr
: ARM::ADDrr
), WBReg
)
216 case ARMII::AddrMode3
: {
217 bool isSub
= ARM_AM::getAM3Op(OffImm
) == ARM_AM::sub
;
218 unsigned Amt
= ARM_AM::getAM3Offset(OffImm
);
220 // Immediate is 8-bits. It's guaranteed to fit in a so_imm operand.
221 UpdateMI
= BuildMI(MF
, MI
.getDebugLoc(),
222 get(isSub
? ARM::SUBri
: ARM::ADDri
), WBReg
)
228 UpdateMI
= BuildMI(MF
, MI
.getDebugLoc(),
229 get(isSub
? ARM::SUBrr
: ARM::ADDrr
), WBReg
)
238 std::vector
<MachineInstr
*> NewMIs
;
242 BuildMI(MF
, MI
.getDebugLoc(), get(MemOpc
), MI
.getOperand(0).getReg())
247 MemMI
= BuildMI(MF
, MI
.getDebugLoc(), get(MemOpc
))
248 .addReg(MI
.getOperand(1).getReg())
253 NewMIs
.push_back(MemMI
);
254 NewMIs
.push_back(UpdateMI
);
258 BuildMI(MF
, MI
.getDebugLoc(), get(MemOpc
), MI
.getOperand(0).getReg())
263 MemMI
= BuildMI(MF
, MI
.getDebugLoc(), get(MemOpc
))
264 .addReg(MI
.getOperand(1).getReg())
270 UpdateMI
->getOperand(0).setIsDead();
271 NewMIs
.push_back(UpdateMI
);
272 NewMIs
.push_back(MemMI
);
275 // Transfer LiveVariables states, kill / dead info.
277 for (unsigned i
= 0, e
= MI
.getNumOperands(); i
!= e
; ++i
) {
278 MachineOperand
&MO
= MI
.getOperand(i
);
279 if (MO
.isReg() && TargetRegisterInfo::isVirtualRegister(MO
.getReg())) {
280 unsigned Reg
= MO
.getReg();
282 LiveVariables::VarInfo
&VI
= LV
->getVarInfo(Reg
);
284 MachineInstr
*NewMI
= (Reg
== WBReg
) ? UpdateMI
: MemMI
;
286 LV
->addVirtualRegisterDead(Reg
, *NewMI
);
288 if (MO
.isUse() && MO
.isKill()) {
289 for (unsigned j
= 0; j
< 2; ++j
) {
290 // Look at the two new MI's in reverse order.
291 MachineInstr
*NewMI
= NewMIs
[j
];
292 if (!NewMI
->readsRegister(Reg
))
294 LV
->addVirtualRegisterKilled(Reg
, *NewMI
);
295 if (VI
.removeKill(MI
))
296 VI
.Kills
.push_back(NewMI
);
304 MachineBasicBlock::iterator MBBI
= MI
.getIterator();
305 MFI
->insert(MBBI
, NewMIs
[1]);
306 MFI
->insert(MBBI
, NewMIs
[0]);
311 bool ARMBaseInstrInfo::analyzeBranch(MachineBasicBlock
&MBB
,
312 MachineBasicBlock
*&TBB
,
313 MachineBasicBlock
*&FBB
,
314 SmallVectorImpl
<MachineOperand
> &Cond
,
315 bool AllowModify
) const {
319 MachineBasicBlock::iterator I
= MBB
.end();
320 if (I
== MBB
.begin())
321 return false; // Empty blocks are easy.
324 // Walk backwards from the end of the basic block until the branch is
325 // analyzed or we give up.
326 while (isPredicated(*I
) || I
->isTerminator() || I
->isDebugValue()) {
327 // Flag to be raised on unanalyzeable instructions. This is useful in cases
328 // where we want to clean up on the end of the basic block before we bail
330 bool CantAnalyze
= false;
332 // Skip over DEBUG values and predicated nonterminators.
333 while (I
->isDebugInstr() || !I
->isTerminator()) {
334 if (I
== MBB
.begin())
339 if (isIndirectBranchOpcode(I
->getOpcode()) ||
340 isJumpTableBranchOpcode(I
->getOpcode())) {
341 // Indirect branches and jump tables can't be analyzed, but we still want
342 // to clean up any instructions at the tail of the basic block.
344 } else if (isUncondBranchOpcode(I
->getOpcode())) {
345 TBB
= I
->getOperand(0).getMBB();
346 } else if (isCondBranchOpcode(I
->getOpcode())) {
347 // Bail out if we encounter multiple conditional branches.
351 assert(!FBB
&& "FBB should have been null.");
353 TBB
= I
->getOperand(0).getMBB();
354 Cond
.push_back(I
->getOperand(1));
355 Cond
.push_back(I
->getOperand(2));
356 } else if (I
->isReturn()) {
357 // Returns can't be analyzed, but we should run cleanup.
358 CantAnalyze
= !isPredicated(*I
);
360 // We encountered other unrecognized terminator. Bail out immediately.
364 // Cleanup code - to be run for unpredicated unconditional branches and
366 if (!isPredicated(*I
) &&
367 (isUncondBranchOpcode(I
->getOpcode()) ||
368 isIndirectBranchOpcode(I
->getOpcode()) ||
369 isJumpTableBranchOpcode(I
->getOpcode()) ||
371 // Forget any previous condition branch information - it no longer applies.
375 // If we can modify the function, delete everything below this
376 // unconditional branch.
378 MachineBasicBlock::iterator DI
= std::next(I
);
379 while (DI
!= MBB
.end()) {
380 MachineInstr
&InstToDelete
= *DI
;
382 InstToDelete
.eraseFromParent();
390 if (I
== MBB
.begin())
396 // We made it past the terminators without bailing out - we must have
397 // analyzed this branch successfully.
401 unsigned ARMBaseInstrInfo::removeBranch(MachineBasicBlock
&MBB
,
402 int *BytesRemoved
) const {
403 assert(!BytesRemoved
&& "code size not handled");
405 MachineBasicBlock::iterator I
= MBB
.getLastNonDebugInstr();
409 if (!isUncondBranchOpcode(I
->getOpcode()) &&
410 !isCondBranchOpcode(I
->getOpcode()))
413 // Remove the branch.
414 I
->eraseFromParent();
418 if (I
== MBB
.begin()) return 1;
420 if (!isCondBranchOpcode(I
->getOpcode()))
423 // Remove the branch.
424 I
->eraseFromParent();
428 unsigned ARMBaseInstrInfo::insertBranch(MachineBasicBlock
&MBB
,
429 MachineBasicBlock
*TBB
,
430 MachineBasicBlock
*FBB
,
431 ArrayRef
<MachineOperand
> Cond
,
433 int *BytesAdded
) const {
434 assert(!BytesAdded
&& "code size not handled");
435 ARMFunctionInfo
*AFI
= MBB
.getParent()->getInfo
<ARMFunctionInfo
>();
436 int BOpc
= !AFI
->isThumbFunction()
437 ? ARM::B
: (AFI
->isThumb2Function() ? ARM::t2B
: ARM::tB
);
438 int BccOpc
= !AFI
->isThumbFunction()
439 ? ARM::Bcc
: (AFI
->isThumb2Function() ? ARM::t2Bcc
: ARM::tBcc
);
440 bool isThumb
= AFI
->isThumbFunction() || AFI
->isThumb2Function();
442 // Shouldn't be a fall through.
443 assert(TBB
&& "insertBranch must not be told to insert a fallthrough");
444 assert((Cond
.size() == 2 || Cond
.size() == 0) &&
445 "ARM branch conditions have two components!");
447 // For conditional branches, we use addOperand to preserve CPSR flags.
450 if (Cond
.empty()) { // Unconditional branch?
452 BuildMI(&MBB
, DL
, get(BOpc
)).addMBB(TBB
).add(predOps(ARMCC::AL
));
454 BuildMI(&MBB
, DL
, get(BOpc
)).addMBB(TBB
);
456 BuildMI(&MBB
, DL
, get(BccOpc
))
458 .addImm(Cond
[0].getImm())
463 // Two-way conditional branch.
464 BuildMI(&MBB
, DL
, get(BccOpc
))
466 .addImm(Cond
[0].getImm())
469 BuildMI(&MBB
, DL
, get(BOpc
)).addMBB(FBB
).add(predOps(ARMCC::AL
));
471 BuildMI(&MBB
, DL
, get(BOpc
)).addMBB(FBB
);
475 bool ARMBaseInstrInfo::
476 reverseBranchCondition(SmallVectorImpl
<MachineOperand
> &Cond
) const {
477 ARMCC::CondCodes CC
= (ARMCC::CondCodes
)(int)Cond
[0].getImm();
478 Cond
[0].setImm(ARMCC::getOppositeCondition(CC
));
482 bool ARMBaseInstrInfo::isPredicated(const MachineInstr
&MI
) const {
484 MachineBasicBlock::const_instr_iterator I
= MI
.getIterator();
485 MachineBasicBlock::const_instr_iterator E
= MI
.getParent()->instr_end();
486 while (++I
!= E
&& I
->isInsideBundle()) {
487 int PIdx
= I
->findFirstPredOperandIdx();
488 if (PIdx
!= -1 && I
->getOperand(PIdx
).getImm() != ARMCC::AL
)
494 int PIdx
= MI
.findFirstPredOperandIdx();
495 return PIdx
!= -1 && MI
.getOperand(PIdx
).getImm() != ARMCC::AL
;
498 bool ARMBaseInstrInfo::PredicateInstruction(
499 MachineInstr
&MI
, ArrayRef
<MachineOperand
> Pred
) const {
500 unsigned Opc
= MI
.getOpcode();
501 if (isUncondBranchOpcode(Opc
)) {
502 MI
.setDesc(get(getMatchingCondBranchOpcode(Opc
)));
503 MachineInstrBuilder(*MI
.getParent()->getParent(), MI
)
504 .addImm(Pred
[0].getImm())
505 .addReg(Pred
[1].getReg());
509 int PIdx
= MI
.findFirstPredOperandIdx();
511 MachineOperand
&PMO
= MI
.getOperand(PIdx
);
512 PMO
.setImm(Pred
[0].getImm());
513 MI
.getOperand(PIdx
+1).setReg(Pred
[1].getReg());
519 bool ARMBaseInstrInfo::SubsumesPredicate(ArrayRef
<MachineOperand
> Pred1
,
520 ArrayRef
<MachineOperand
> Pred2
) const {
521 if (Pred1
.size() > 2 || Pred2
.size() > 2)
524 ARMCC::CondCodes CC1
= (ARMCC::CondCodes
)Pred1
[0].getImm();
525 ARMCC::CondCodes CC2
= (ARMCC::CondCodes
)Pred2
[0].getImm();
535 return CC2
== ARMCC::HI
;
537 return CC2
== ARMCC::LO
|| CC2
== ARMCC::EQ
;
539 return CC2
== ARMCC::GT
;
541 return CC2
== ARMCC::LT
;
545 bool ARMBaseInstrInfo::DefinesPredicate(
546 MachineInstr
&MI
, std::vector
<MachineOperand
> &Pred
) const {
548 for (unsigned i
= 0, e
= MI
.getNumOperands(); i
!= e
; ++i
) {
549 const MachineOperand
&MO
= MI
.getOperand(i
);
550 if ((MO
.isRegMask() && MO
.clobbersPhysReg(ARM::CPSR
)) ||
551 (MO
.isReg() && MO
.isDef() && MO
.getReg() == ARM::CPSR
)) {
560 bool ARMBaseInstrInfo::isCPSRDefined(const MachineInstr
&MI
) {
561 for (const auto &MO
: MI
.operands())
562 if (MO
.isReg() && MO
.getReg() == ARM::CPSR
&& MO
.isDef() && !MO
.isDead())
567 bool ARMBaseInstrInfo::isAddrMode3OpImm(const MachineInstr
&MI
,
569 const MachineOperand
&Offset
= MI
.getOperand(Op
+ 1);
570 return Offset
.getReg() != 0;
573 // Load with negative register offset requires additional 1cyc and +I unit
575 bool ARMBaseInstrInfo::isAddrMode3OpMinusReg(const MachineInstr
&MI
,
577 const MachineOperand
&Offset
= MI
.getOperand(Op
+ 1);
578 const MachineOperand
&Opc
= MI
.getOperand(Op
+ 2);
580 assert(Offset
.isReg());
581 int64_t OpcImm
= Opc
.getImm();
583 bool isSub
= ARM_AM::getAM3Op(OpcImm
) == ARM_AM::sub
;
584 return (isSub
&& Offset
.getReg() != 0);
587 bool ARMBaseInstrInfo::isLdstScaledReg(const MachineInstr
&MI
,
589 const MachineOperand
&Opc
= MI
.getOperand(Op
+ 2);
590 unsigned OffImm
= Opc
.getImm();
591 return ARM_AM::getAM2ShiftOpc(OffImm
) != ARM_AM::no_shift
;
594 // Load, scaled register offset, not plus LSL2
595 bool ARMBaseInstrInfo::isLdstScaledRegNotPlusLsl2(const MachineInstr
&MI
,
597 const MachineOperand
&Opc
= MI
.getOperand(Op
+ 2);
598 unsigned OffImm
= Opc
.getImm();
600 bool isAdd
= ARM_AM::getAM2Op(OffImm
) == ARM_AM::add
;
601 unsigned Amt
= ARM_AM::getAM2Offset(OffImm
);
602 ARM_AM::ShiftOpc ShiftOpc
= ARM_AM::getAM2ShiftOpc(OffImm
);
603 if (ShiftOpc
== ARM_AM::no_shift
) return false; // not scaled
604 bool SimpleScaled
= (isAdd
&& ShiftOpc
== ARM_AM::lsl
&& Amt
== 2);
605 return !SimpleScaled
;
608 // Minus reg for ldstso addr mode
609 bool ARMBaseInstrInfo::isLdstSoMinusReg(const MachineInstr
&MI
,
611 unsigned OffImm
= MI
.getOperand(Op
+ 2).getImm();
612 return ARM_AM::getAM2Op(OffImm
) == ARM_AM::sub
;
615 // Load, scaled register offset
616 bool ARMBaseInstrInfo::isAm2ScaledReg(const MachineInstr
&MI
,
618 unsigned OffImm
= MI
.getOperand(Op
+ 2).getImm();
619 return ARM_AM::getAM2ShiftOpc(OffImm
) != ARM_AM::no_shift
;
622 static bool isEligibleForITBlock(const MachineInstr
*MI
) {
623 switch (MI
->getOpcode()) {
624 default: return true;
625 case ARM::tADC
: // ADC (register) T1
626 case ARM::tADDi3
: // ADD (immediate) T1
627 case ARM::tADDi8
: // ADD (immediate) T2
628 case ARM::tADDrr
: // ADD (register) T1
629 case ARM::tAND
: // AND (register) T1
630 case ARM::tASRri
: // ASR (immediate) T1
631 case ARM::tASRrr
: // ASR (register) T1
632 case ARM::tBIC
: // BIC (register) T1
633 case ARM::tEOR
: // EOR (register) T1
634 case ARM::tLSLri
: // LSL (immediate) T1
635 case ARM::tLSLrr
: // LSL (register) T1
636 case ARM::tLSRri
: // LSR (immediate) T1
637 case ARM::tLSRrr
: // LSR (register) T1
638 case ARM::tMUL
: // MUL T1
639 case ARM::tMVN
: // MVN (register) T1
640 case ARM::tORR
: // ORR (register) T1
641 case ARM::tROR
: // ROR (register) T1
642 case ARM::tRSB
: // RSB (immediate) T1
643 case ARM::tSBC
: // SBC (register) T1
644 case ARM::tSUBi3
: // SUB (immediate) T1
645 case ARM::tSUBi8
: // SUB (immediate) T2
646 case ARM::tSUBrr
: // SUB (register) T1
647 return !ARMBaseInstrInfo::isCPSRDefined(*MI
);
651 /// isPredicable - Return true if the specified instruction can be predicated.
652 /// By default, this returns true for every instruction with a
653 /// PredicateOperand.
654 bool ARMBaseInstrInfo::isPredicable(const MachineInstr
&MI
) const {
655 if (!MI
.isPredicable())
661 if (!isEligibleForITBlock(&MI
))
664 const ARMFunctionInfo
*AFI
=
665 MI
.getParent()->getParent()->getInfo
<ARMFunctionInfo
>();
667 // Neon instructions in Thumb2 IT blocks are deprecated, see ARMARM.
668 // In their ARM encoding, they can't be encoded in a conditional form.
669 if ((MI
.getDesc().TSFlags
& ARMII::DomainMask
) == ARMII::DomainNEON
)
672 if (AFI
->isThumb2Function()) {
673 if (getSubtarget().restrictIT())
674 return isV8EligibleForIT(&MI
);
682 template <> bool IsCPSRDead
<MachineInstr
>(const MachineInstr
*MI
) {
683 for (unsigned i
= 0, e
= MI
->getNumOperands(); i
!= e
; ++i
) {
684 const MachineOperand
&MO
= MI
->getOperand(i
);
685 if (!MO
.isReg() || MO
.isUndef() || MO
.isUse())
687 if (MO
.getReg() != ARM::CPSR
)
692 // all definitions of CPSR are dead
696 } // end namespace llvm
698 /// GetInstSize - Return the size of the specified MachineInstr.
700 unsigned ARMBaseInstrInfo::getInstSizeInBytes(const MachineInstr
&MI
) const {
701 const MachineBasicBlock
&MBB
= *MI
.getParent();
702 const MachineFunction
*MF
= MBB
.getParent();
703 const MCAsmInfo
*MAI
= MF
->getTarget().getMCAsmInfo();
705 const MCInstrDesc
&MCID
= MI
.getDesc();
707 return MCID
.getSize();
709 // If this machine instr is an inline asm, measure it.
710 if (MI
.getOpcode() == ARM::INLINEASM
) {
711 unsigned Size
= getInlineAsmLength(MI
.getOperand(0).getSymbolName(), *MAI
);
712 if (!MF
->getInfo
<ARMFunctionInfo
>()->isThumbFunction())
713 Size
= alignTo(Size
, 4);
716 unsigned Opc
= MI
.getOpcode();
719 // pseudo-instruction sizes are zero.
721 case TargetOpcode::BUNDLE
:
722 return getInstBundleLength(MI
);
723 case ARM::MOVi16_ga_pcrel
:
724 case ARM::MOVTi16_ga_pcrel
:
725 case ARM::t2MOVi16_ga_pcrel
:
726 case ARM::t2MOVTi16_ga_pcrel
:
729 case ARM::t2MOVi32imm
:
731 case ARM::CONSTPOOL_ENTRY
:
732 case ARM::JUMPTABLE_INSTS
:
733 case ARM::JUMPTABLE_ADDRS
:
734 case ARM::JUMPTABLE_TBB
:
735 case ARM::JUMPTABLE_TBH
:
736 // If this machine instr is a constant pool entry, its size is recorded as
738 return MI
.getOperand(2).getImm();
739 case ARM::Int_eh_sjlj_longjmp
:
741 case ARM::tInt_eh_sjlj_longjmp
:
743 case ARM::tInt_WIN_eh_sjlj_longjmp
:
745 case ARM::Int_eh_sjlj_setjmp
:
746 case ARM::Int_eh_sjlj_setjmp_nofp
:
748 case ARM::tInt_eh_sjlj_setjmp
:
749 case ARM::t2Int_eh_sjlj_setjmp
:
750 case ARM::t2Int_eh_sjlj_setjmp_nofp
:
753 return MI
.getOperand(1).getImm();
757 unsigned ARMBaseInstrInfo::getInstBundleLength(const MachineInstr
&MI
) const {
759 MachineBasicBlock::const_instr_iterator I
= MI
.getIterator();
760 MachineBasicBlock::const_instr_iterator E
= MI
.getParent()->instr_end();
761 while (++I
!= E
&& I
->isInsideBundle()) {
762 assert(!I
->isBundle() && "No nested bundle!");
763 Size
+= getInstSizeInBytes(*I
);
768 void ARMBaseInstrInfo::copyFromCPSR(MachineBasicBlock
&MBB
,
769 MachineBasicBlock::iterator I
,
770 unsigned DestReg
, bool KillSrc
,
771 const ARMSubtarget
&Subtarget
) const {
772 unsigned Opc
= Subtarget
.isThumb()
773 ? (Subtarget
.isMClass() ? ARM::t2MRS_M
: ARM::t2MRS_AR
)
776 MachineInstrBuilder MIB
=
777 BuildMI(MBB
, I
, I
->getDebugLoc(), get(Opc
), DestReg
);
779 // There is only 1 A/R class MRS instruction, and it always refers to
780 // APSR. However, there are lots of other possibilities on M-class cores.
781 if (Subtarget
.isMClass())
784 MIB
.add(predOps(ARMCC::AL
))
785 .addReg(ARM::CPSR
, RegState::Implicit
| getKillRegState(KillSrc
));
788 void ARMBaseInstrInfo::copyToCPSR(MachineBasicBlock
&MBB
,
789 MachineBasicBlock::iterator I
,
790 unsigned SrcReg
, bool KillSrc
,
791 const ARMSubtarget
&Subtarget
) const {
792 unsigned Opc
= Subtarget
.isThumb()
793 ? (Subtarget
.isMClass() ? ARM::t2MSR_M
: ARM::t2MSR_AR
)
796 MachineInstrBuilder MIB
= BuildMI(MBB
, I
, I
->getDebugLoc(), get(Opc
));
798 if (Subtarget
.isMClass())
803 MIB
.addReg(SrcReg
, getKillRegState(KillSrc
))
804 .add(predOps(ARMCC::AL
))
805 .addReg(ARM::CPSR
, RegState::Implicit
| RegState::Define
);
808 void ARMBaseInstrInfo::copyPhysReg(MachineBasicBlock
&MBB
,
809 MachineBasicBlock::iterator I
,
810 const DebugLoc
&DL
, unsigned DestReg
,
811 unsigned SrcReg
, bool KillSrc
) const {
812 bool GPRDest
= ARM::GPRRegClass
.contains(DestReg
);
813 bool GPRSrc
= ARM::GPRRegClass
.contains(SrcReg
);
815 if (GPRDest
&& GPRSrc
) {
816 BuildMI(MBB
, I
, DL
, get(ARM::MOVr
), DestReg
)
817 .addReg(SrcReg
, getKillRegState(KillSrc
))
818 .add(predOps(ARMCC::AL
))
823 bool SPRDest
= ARM::SPRRegClass
.contains(DestReg
);
824 bool SPRSrc
= ARM::SPRRegClass
.contains(SrcReg
);
827 if (SPRDest
&& SPRSrc
)
829 else if (GPRDest
&& SPRSrc
)
831 else if (SPRDest
&& GPRSrc
)
833 else if (ARM::DPRRegClass
.contains(DestReg
, SrcReg
) && !Subtarget
.isFPOnlySP())
835 else if (ARM::QPRRegClass
.contains(DestReg
, SrcReg
))
839 MachineInstrBuilder MIB
= BuildMI(MBB
, I
, DL
, get(Opc
), DestReg
);
840 MIB
.addReg(SrcReg
, getKillRegState(KillSrc
));
841 if (Opc
== ARM::VORRq
)
842 MIB
.addReg(SrcReg
, getKillRegState(KillSrc
));
843 MIB
.add(predOps(ARMCC::AL
));
847 // Handle register classes that require multiple instructions.
848 unsigned BeginIdx
= 0;
849 unsigned SubRegs
= 0;
852 // Use VORRq when possible.
853 if (ARM::QQPRRegClass
.contains(DestReg
, SrcReg
)) {
855 BeginIdx
= ARM::qsub_0
;
857 } else if (ARM::QQQQPRRegClass
.contains(DestReg
, SrcReg
)) {
859 BeginIdx
= ARM::qsub_0
;
861 // Fall back to VMOVD.
862 } else if (ARM::DPairRegClass
.contains(DestReg
, SrcReg
)) {
864 BeginIdx
= ARM::dsub_0
;
866 } else if (ARM::DTripleRegClass
.contains(DestReg
, SrcReg
)) {
868 BeginIdx
= ARM::dsub_0
;
870 } else if (ARM::DQuadRegClass
.contains(DestReg
, SrcReg
)) {
872 BeginIdx
= ARM::dsub_0
;
874 } else if (ARM::GPRPairRegClass
.contains(DestReg
, SrcReg
)) {
875 Opc
= Subtarget
.isThumb2() ? ARM::tMOVr
: ARM::MOVr
;
876 BeginIdx
= ARM::gsub_0
;
878 } else if (ARM::DPairSpcRegClass
.contains(DestReg
, SrcReg
)) {
880 BeginIdx
= ARM::dsub_0
;
883 } else if (ARM::DTripleSpcRegClass
.contains(DestReg
, SrcReg
)) {
885 BeginIdx
= ARM::dsub_0
;
888 } else if (ARM::DQuadSpcRegClass
.contains(DestReg
, SrcReg
)) {
890 BeginIdx
= ARM::dsub_0
;
893 } else if (ARM::DPRRegClass
.contains(DestReg
, SrcReg
) && Subtarget
.isFPOnlySP()) {
895 BeginIdx
= ARM::ssub_0
;
897 } else if (SrcReg
== ARM::CPSR
) {
898 copyFromCPSR(MBB
, I
, DestReg
, KillSrc
, Subtarget
);
900 } else if (DestReg
== ARM::CPSR
) {
901 copyToCPSR(MBB
, I
, SrcReg
, KillSrc
, Subtarget
);
905 assert(Opc
&& "Impossible reg-to-reg copy");
907 const TargetRegisterInfo
*TRI
= &getRegisterInfo();
908 MachineInstrBuilder Mov
;
910 // Copy register tuples backward when the first Dest reg overlaps with SrcReg.
911 if (TRI
->regsOverlap(SrcReg
, TRI
->getSubReg(DestReg
, BeginIdx
))) {
912 BeginIdx
= BeginIdx
+ ((SubRegs
- 1) * Spacing
);
916 SmallSet
<unsigned, 4> DstRegs
;
918 for (unsigned i
= 0; i
!= SubRegs
; ++i
) {
919 unsigned Dst
= TRI
->getSubReg(DestReg
, BeginIdx
+ i
* Spacing
);
920 unsigned Src
= TRI
->getSubReg(SrcReg
, BeginIdx
+ i
* Spacing
);
921 assert(Dst
&& Src
&& "Bad sub-register");
923 assert(!DstRegs
.count(Src
) && "destructive vector copy");
926 Mov
= BuildMI(MBB
, I
, I
->getDebugLoc(), get(Opc
), Dst
).addReg(Src
);
927 // VORR takes two source operands.
928 if (Opc
== ARM::VORRq
)
930 Mov
= Mov
.add(predOps(ARMCC::AL
));
932 if (Opc
== ARM::MOVr
)
933 Mov
= Mov
.add(condCodeOp());
935 // Add implicit super-register defs and kills to the last instruction.
936 Mov
->addRegisterDefined(DestReg
, TRI
);
938 Mov
->addRegisterKilled(SrcReg
, TRI
);
941 bool ARMBaseInstrInfo::isCopyInstrImpl(const MachineInstr
&MI
,
942 const MachineOperand
*&Src
,
943 const MachineOperand
*&Dest
) const {
944 // VMOVRRD is also a copy instruction but it requires
945 // special way of handling. It is more complex copy version
946 // and since that we are not considering it. For recognition
947 // of such instruction isExtractSubregLike MI interface fuction
949 // VORRq is considered as a move only if two inputs are
950 // the same register.
951 if (!MI
.isMoveReg() ||
952 (MI
.getOpcode() == ARM::VORRq
&&
953 MI
.getOperand(1).getReg() != MI
.getOperand(2).getReg()))
955 Dest
= &MI
.getOperand(0);
956 Src
= &MI
.getOperand(1);
960 const MachineInstrBuilder
&
961 ARMBaseInstrInfo::AddDReg(MachineInstrBuilder
&MIB
, unsigned Reg
,
962 unsigned SubIdx
, unsigned State
,
963 const TargetRegisterInfo
*TRI
) const {
965 return MIB
.addReg(Reg
, State
);
967 if (TargetRegisterInfo::isPhysicalRegister(Reg
))
968 return MIB
.addReg(TRI
->getSubReg(Reg
, SubIdx
), State
);
969 return MIB
.addReg(Reg
, State
, SubIdx
);
972 void ARMBaseInstrInfo::
973 storeRegToStackSlot(MachineBasicBlock
&MBB
, MachineBasicBlock::iterator I
,
974 unsigned SrcReg
, bool isKill
, int FI
,
975 const TargetRegisterClass
*RC
,
976 const TargetRegisterInfo
*TRI
) const {
977 MachineFunction
&MF
= *MBB
.getParent();
978 MachineFrameInfo
&MFI
= MF
.getFrameInfo();
979 unsigned Align
= MFI
.getObjectAlignment(FI
);
981 MachineMemOperand
*MMO
= MF
.getMachineMemOperand(
982 MachinePointerInfo::getFixedStack(MF
, FI
), MachineMemOperand::MOStore
,
983 MFI
.getObjectSize(FI
), Align
);
985 switch (TRI
->getSpillSize(*RC
)) {
987 if (ARM::HPRRegClass
.hasSubClassEq(RC
)) {
988 BuildMI(MBB
, I
, DebugLoc(), get(ARM::VSTRH
))
989 .addReg(SrcReg
, getKillRegState(isKill
))
993 .add(predOps(ARMCC::AL
));
995 llvm_unreachable("Unknown reg class!");
998 if (ARM::GPRRegClass
.hasSubClassEq(RC
)) {
999 BuildMI(MBB
, I
, DebugLoc(), get(ARM::STRi12
))
1000 .addReg(SrcReg
, getKillRegState(isKill
))
1004 .add(predOps(ARMCC::AL
));
1005 } else if (ARM::SPRRegClass
.hasSubClassEq(RC
)) {
1006 BuildMI(MBB
, I
, DebugLoc(), get(ARM::VSTRS
))
1007 .addReg(SrcReg
, getKillRegState(isKill
))
1011 .add(predOps(ARMCC::AL
));
1013 llvm_unreachable("Unknown reg class!");
1016 if (ARM::DPRRegClass
.hasSubClassEq(RC
)) {
1017 BuildMI(MBB
, I
, DebugLoc(), get(ARM::VSTRD
))
1018 .addReg(SrcReg
, getKillRegState(isKill
))
1022 .add(predOps(ARMCC::AL
));
1023 } else if (ARM::GPRPairRegClass
.hasSubClassEq(RC
)) {
1024 if (Subtarget
.hasV5TEOps()) {
1025 MachineInstrBuilder MIB
= BuildMI(MBB
, I
, DebugLoc(), get(ARM::STRD
));
1026 AddDReg(MIB
, SrcReg
, ARM::gsub_0
, getKillRegState(isKill
), TRI
);
1027 AddDReg(MIB
, SrcReg
, ARM::gsub_1
, 0, TRI
);
1028 MIB
.addFrameIndex(FI
).addReg(0).addImm(0).addMemOperand(MMO
)
1029 .add(predOps(ARMCC::AL
));
1031 // Fallback to STM instruction, which has existed since the dawn of
1033 MachineInstrBuilder MIB
= BuildMI(MBB
, I
, DebugLoc(), get(ARM::STMIA
))
1036 .add(predOps(ARMCC::AL
));
1037 AddDReg(MIB
, SrcReg
, ARM::gsub_0
, getKillRegState(isKill
), TRI
);
1038 AddDReg(MIB
, SrcReg
, ARM::gsub_1
, 0, TRI
);
1041 llvm_unreachable("Unknown reg class!");
1044 if (ARM::DPairRegClass
.hasSubClassEq(RC
)) {
1045 // Use aligned spills if the stack can be realigned.
1046 if (Align
>= 16 && getRegisterInfo().canRealignStack(MF
)) {
1047 BuildMI(MBB
, I
, DebugLoc(), get(ARM::VST1q64
))
1050 .addReg(SrcReg
, getKillRegState(isKill
))
1052 .add(predOps(ARMCC::AL
));
1054 BuildMI(MBB
, I
, DebugLoc(), get(ARM::VSTMQIA
))
1055 .addReg(SrcReg
, getKillRegState(isKill
))
1058 .add(predOps(ARMCC::AL
));
1061 llvm_unreachable("Unknown reg class!");
1064 if (ARM::DTripleRegClass
.hasSubClassEq(RC
)) {
1065 // Use aligned spills if the stack can be realigned.
1066 if (Align
>= 16 && getRegisterInfo().canRealignStack(MF
)) {
1067 BuildMI(MBB
, I
, DebugLoc(), get(ARM::VST1d64TPseudo
))
1070 .addReg(SrcReg
, getKillRegState(isKill
))
1072 .add(predOps(ARMCC::AL
));
1074 MachineInstrBuilder MIB
= BuildMI(MBB
, I
, DebugLoc(),
1077 .add(predOps(ARMCC::AL
))
1078 .addMemOperand(MMO
);
1079 MIB
= AddDReg(MIB
, SrcReg
, ARM::dsub_0
, getKillRegState(isKill
), TRI
);
1080 MIB
= AddDReg(MIB
, SrcReg
, ARM::dsub_1
, 0, TRI
);
1081 AddDReg(MIB
, SrcReg
, ARM::dsub_2
, 0, TRI
);
1084 llvm_unreachable("Unknown reg class!");
1087 if (ARM::QQPRRegClass
.hasSubClassEq(RC
) || ARM::DQuadRegClass
.hasSubClassEq(RC
)) {
1088 if (Align
>= 16 && getRegisterInfo().canRealignStack(MF
)) {
1089 // FIXME: It's possible to only store part of the QQ register if the
1090 // spilled def has a sub-register index.
1091 BuildMI(MBB
, I
, DebugLoc(), get(ARM::VST1d64QPseudo
))
1094 .addReg(SrcReg
, getKillRegState(isKill
))
1096 .add(predOps(ARMCC::AL
));
1098 MachineInstrBuilder MIB
= BuildMI(MBB
, I
, DebugLoc(),
1101 .add(predOps(ARMCC::AL
))
1102 .addMemOperand(MMO
);
1103 MIB
= AddDReg(MIB
, SrcReg
, ARM::dsub_0
, getKillRegState(isKill
), TRI
);
1104 MIB
= AddDReg(MIB
, SrcReg
, ARM::dsub_1
, 0, TRI
);
1105 MIB
= AddDReg(MIB
, SrcReg
, ARM::dsub_2
, 0, TRI
);
1106 AddDReg(MIB
, SrcReg
, ARM::dsub_3
, 0, TRI
);
1109 llvm_unreachable("Unknown reg class!");
1112 if (ARM::QQQQPRRegClass
.hasSubClassEq(RC
)) {
1113 MachineInstrBuilder MIB
= BuildMI(MBB
, I
, DebugLoc(), get(ARM::VSTMDIA
))
1115 .add(predOps(ARMCC::AL
))
1116 .addMemOperand(MMO
);
1117 MIB
= AddDReg(MIB
, SrcReg
, ARM::dsub_0
, getKillRegState(isKill
), TRI
);
1118 MIB
= AddDReg(MIB
, SrcReg
, ARM::dsub_1
, 0, TRI
);
1119 MIB
= AddDReg(MIB
, SrcReg
, ARM::dsub_2
, 0, TRI
);
1120 MIB
= AddDReg(MIB
, SrcReg
, ARM::dsub_3
, 0, TRI
);
1121 MIB
= AddDReg(MIB
, SrcReg
, ARM::dsub_4
, 0, TRI
);
1122 MIB
= AddDReg(MIB
, SrcReg
, ARM::dsub_5
, 0, TRI
);
1123 MIB
= AddDReg(MIB
, SrcReg
, ARM::dsub_6
, 0, TRI
);
1124 AddDReg(MIB
, SrcReg
, ARM::dsub_7
, 0, TRI
);
1126 llvm_unreachable("Unknown reg class!");
1129 llvm_unreachable("Unknown reg class!");
1133 unsigned ARMBaseInstrInfo::isStoreToStackSlot(const MachineInstr
&MI
,
1134 int &FrameIndex
) const {
1135 switch (MI
.getOpcode()) {
1138 case ARM::t2STRs
: // FIXME: don't use t2STRs to access frame.
1139 if (MI
.getOperand(1).isFI() && MI
.getOperand(2).isReg() &&
1140 MI
.getOperand(3).isImm() && MI
.getOperand(2).getReg() == 0 &&
1141 MI
.getOperand(3).getImm() == 0) {
1142 FrameIndex
= MI
.getOperand(1).getIndex();
1143 return MI
.getOperand(0).getReg();
1151 if (MI
.getOperand(1).isFI() && MI
.getOperand(2).isImm() &&
1152 MI
.getOperand(2).getImm() == 0) {
1153 FrameIndex
= MI
.getOperand(1).getIndex();
1154 return MI
.getOperand(0).getReg();
1158 case ARM::VST1d64TPseudo
:
1159 case ARM::VST1d64QPseudo
:
1160 if (MI
.getOperand(0).isFI() && MI
.getOperand(2).getSubReg() == 0) {
1161 FrameIndex
= MI
.getOperand(0).getIndex();
1162 return MI
.getOperand(2).getReg();
1166 if (MI
.getOperand(1).isFI() && MI
.getOperand(0).getSubReg() == 0) {
1167 FrameIndex
= MI
.getOperand(1).getIndex();
1168 return MI
.getOperand(0).getReg();
1176 unsigned ARMBaseInstrInfo::isStoreToStackSlotPostFE(const MachineInstr
&MI
,
1177 int &FrameIndex
) const {
1178 SmallVector
<const MachineMemOperand
*, 1> Accesses
;
1179 if (MI
.mayStore() && hasStoreToStackSlot(MI
, Accesses
)) {
1181 cast
<FixedStackPseudoSourceValue
>(Accesses
.front()->getPseudoValue())
1188 void ARMBaseInstrInfo::
1189 loadRegFromStackSlot(MachineBasicBlock
&MBB
, MachineBasicBlock::iterator I
,
1190 unsigned DestReg
, int FI
,
1191 const TargetRegisterClass
*RC
,
1192 const TargetRegisterInfo
*TRI
) const {
1194 if (I
!= MBB
.end()) DL
= I
->getDebugLoc();
1195 MachineFunction
&MF
= *MBB
.getParent();
1196 MachineFrameInfo
&MFI
= MF
.getFrameInfo();
1197 unsigned Align
= MFI
.getObjectAlignment(FI
);
1198 MachineMemOperand
*MMO
= MF
.getMachineMemOperand(
1199 MachinePointerInfo::getFixedStack(MF
, FI
), MachineMemOperand::MOLoad
,
1200 MFI
.getObjectSize(FI
), Align
);
1202 switch (TRI
->getSpillSize(*RC
)) {
1204 if (ARM::HPRRegClass
.hasSubClassEq(RC
)) {
1205 BuildMI(MBB
, I
, DL
, get(ARM::VLDRH
), DestReg
)
1209 .add(predOps(ARMCC::AL
));
1211 llvm_unreachable("Unknown reg class!");
1214 if (ARM::GPRRegClass
.hasSubClassEq(RC
)) {
1215 BuildMI(MBB
, I
, DL
, get(ARM::LDRi12
), DestReg
)
1219 .add(predOps(ARMCC::AL
));
1220 } else if (ARM::SPRRegClass
.hasSubClassEq(RC
)) {
1221 BuildMI(MBB
, I
, DL
, get(ARM::VLDRS
), DestReg
)
1225 .add(predOps(ARMCC::AL
));
1227 llvm_unreachable("Unknown reg class!");
1230 if (ARM::DPRRegClass
.hasSubClassEq(RC
)) {
1231 BuildMI(MBB
, I
, DL
, get(ARM::VLDRD
), DestReg
)
1235 .add(predOps(ARMCC::AL
));
1236 } else if (ARM::GPRPairRegClass
.hasSubClassEq(RC
)) {
1237 MachineInstrBuilder MIB
;
1239 if (Subtarget
.hasV5TEOps()) {
1240 MIB
= BuildMI(MBB
, I
, DL
, get(ARM::LDRD
));
1241 AddDReg(MIB
, DestReg
, ARM::gsub_0
, RegState::DefineNoRead
, TRI
);
1242 AddDReg(MIB
, DestReg
, ARM::gsub_1
, RegState::DefineNoRead
, TRI
);
1243 MIB
.addFrameIndex(FI
).addReg(0).addImm(0).addMemOperand(MMO
)
1244 .add(predOps(ARMCC::AL
));
1246 // Fallback to LDM instruction, which has existed since the dawn of
1248 MIB
= BuildMI(MBB
, I
, DL
, get(ARM::LDMIA
))
1251 .add(predOps(ARMCC::AL
));
1252 MIB
= AddDReg(MIB
, DestReg
, ARM::gsub_0
, RegState::DefineNoRead
, TRI
);
1253 MIB
= AddDReg(MIB
, DestReg
, ARM::gsub_1
, RegState::DefineNoRead
, TRI
);
1256 if (TargetRegisterInfo::isPhysicalRegister(DestReg
))
1257 MIB
.addReg(DestReg
, RegState::ImplicitDefine
);
1259 llvm_unreachable("Unknown reg class!");
1262 if (ARM::DPairRegClass
.hasSubClassEq(RC
)) {
1263 if (Align
>= 16 && getRegisterInfo().canRealignStack(MF
)) {
1264 BuildMI(MBB
, I
, DL
, get(ARM::VLD1q64
), DestReg
)
1268 .add(predOps(ARMCC::AL
));
1270 BuildMI(MBB
, I
, DL
, get(ARM::VLDMQIA
), DestReg
)
1273 .add(predOps(ARMCC::AL
));
1276 llvm_unreachable("Unknown reg class!");
1279 if (ARM::DTripleRegClass
.hasSubClassEq(RC
)) {
1280 if (Align
>= 16 && getRegisterInfo().canRealignStack(MF
)) {
1281 BuildMI(MBB
, I
, DL
, get(ARM::VLD1d64TPseudo
), DestReg
)
1285 .add(predOps(ARMCC::AL
));
1287 MachineInstrBuilder MIB
= BuildMI(MBB
, I
, DL
, get(ARM::VLDMDIA
))
1290 .add(predOps(ARMCC::AL
));
1291 MIB
= AddDReg(MIB
, DestReg
, ARM::dsub_0
, RegState::DefineNoRead
, TRI
);
1292 MIB
= AddDReg(MIB
, DestReg
, ARM::dsub_1
, RegState::DefineNoRead
, TRI
);
1293 MIB
= AddDReg(MIB
, DestReg
, ARM::dsub_2
, RegState::DefineNoRead
, TRI
);
1294 if (TargetRegisterInfo::isPhysicalRegister(DestReg
))
1295 MIB
.addReg(DestReg
, RegState::ImplicitDefine
);
1298 llvm_unreachable("Unknown reg class!");
1301 if (ARM::QQPRRegClass
.hasSubClassEq(RC
) || ARM::DQuadRegClass
.hasSubClassEq(RC
)) {
1302 if (Align
>= 16 && getRegisterInfo().canRealignStack(MF
)) {
1303 BuildMI(MBB
, I
, DL
, get(ARM::VLD1d64QPseudo
), DestReg
)
1307 .add(predOps(ARMCC::AL
));
1309 MachineInstrBuilder MIB
= BuildMI(MBB
, I
, DL
, get(ARM::VLDMDIA
))
1311 .add(predOps(ARMCC::AL
))
1312 .addMemOperand(MMO
);
1313 MIB
= AddDReg(MIB
, DestReg
, ARM::dsub_0
, RegState::DefineNoRead
, TRI
);
1314 MIB
= AddDReg(MIB
, DestReg
, ARM::dsub_1
, RegState::DefineNoRead
, TRI
);
1315 MIB
= AddDReg(MIB
, DestReg
, ARM::dsub_2
, RegState::DefineNoRead
, TRI
);
1316 MIB
= AddDReg(MIB
, DestReg
, ARM::dsub_3
, RegState::DefineNoRead
, TRI
);
1317 if (TargetRegisterInfo::isPhysicalRegister(DestReg
))
1318 MIB
.addReg(DestReg
, RegState::ImplicitDefine
);
1321 llvm_unreachable("Unknown reg class!");
1324 if (ARM::QQQQPRRegClass
.hasSubClassEq(RC
)) {
1325 MachineInstrBuilder MIB
= BuildMI(MBB
, I
, DL
, get(ARM::VLDMDIA
))
1327 .add(predOps(ARMCC::AL
))
1328 .addMemOperand(MMO
);
1329 MIB
= AddDReg(MIB
, DestReg
, ARM::dsub_0
, RegState::DefineNoRead
, TRI
);
1330 MIB
= AddDReg(MIB
, DestReg
, ARM::dsub_1
, RegState::DefineNoRead
, TRI
);
1331 MIB
= AddDReg(MIB
, DestReg
, ARM::dsub_2
, RegState::DefineNoRead
, TRI
);
1332 MIB
= AddDReg(MIB
, DestReg
, ARM::dsub_3
, RegState::DefineNoRead
, TRI
);
1333 MIB
= AddDReg(MIB
, DestReg
, ARM::dsub_4
, RegState::DefineNoRead
, TRI
);
1334 MIB
= AddDReg(MIB
, DestReg
, ARM::dsub_5
, RegState::DefineNoRead
, TRI
);
1335 MIB
= AddDReg(MIB
, DestReg
, ARM::dsub_6
, RegState::DefineNoRead
, TRI
);
1336 MIB
= AddDReg(MIB
, DestReg
, ARM::dsub_7
, RegState::DefineNoRead
, TRI
);
1337 if (TargetRegisterInfo::isPhysicalRegister(DestReg
))
1338 MIB
.addReg(DestReg
, RegState::ImplicitDefine
);
1340 llvm_unreachable("Unknown reg class!");
1343 llvm_unreachable("Unknown regclass!");
1347 unsigned ARMBaseInstrInfo::isLoadFromStackSlot(const MachineInstr
&MI
,
1348 int &FrameIndex
) const {
1349 switch (MI
.getOpcode()) {
1352 case ARM::t2LDRs
: // FIXME: don't use t2LDRs to access frame.
1353 if (MI
.getOperand(1).isFI() && MI
.getOperand(2).isReg() &&
1354 MI
.getOperand(3).isImm() && MI
.getOperand(2).getReg() == 0 &&
1355 MI
.getOperand(3).getImm() == 0) {
1356 FrameIndex
= MI
.getOperand(1).getIndex();
1357 return MI
.getOperand(0).getReg();
1365 if (MI
.getOperand(1).isFI() && MI
.getOperand(2).isImm() &&
1366 MI
.getOperand(2).getImm() == 0) {
1367 FrameIndex
= MI
.getOperand(1).getIndex();
1368 return MI
.getOperand(0).getReg();
1372 case ARM::VLD1d8TPseudo
:
1373 case ARM::VLD1d16TPseudo
:
1374 case ARM::VLD1d32TPseudo
:
1375 case ARM::VLD1d64TPseudo
:
1376 case ARM::VLD1d8QPseudo
:
1377 case ARM::VLD1d16QPseudo
:
1378 case ARM::VLD1d32QPseudo
:
1379 case ARM::VLD1d64QPseudo
:
1380 if (MI
.getOperand(1).isFI() && MI
.getOperand(0).getSubReg() == 0) {
1381 FrameIndex
= MI
.getOperand(1).getIndex();
1382 return MI
.getOperand(0).getReg();
1386 if (MI
.getOperand(1).isFI() && MI
.getOperand(0).getSubReg() == 0) {
1387 FrameIndex
= MI
.getOperand(1).getIndex();
1388 return MI
.getOperand(0).getReg();
1396 unsigned ARMBaseInstrInfo::isLoadFromStackSlotPostFE(const MachineInstr
&MI
,
1397 int &FrameIndex
) const {
1398 SmallVector
<const MachineMemOperand
*, 1> Accesses
;
1399 if (MI
.mayLoad() && hasLoadFromStackSlot(MI
, Accesses
)) {
1401 cast
<FixedStackPseudoSourceValue
>(Accesses
.front()->getPseudoValue())
1408 /// Expands MEMCPY to either LDMIA/STMIA or LDMIA_UPD/STMID_UPD
1409 /// depending on whether the result is used.
1410 void ARMBaseInstrInfo::expandMEMCPY(MachineBasicBlock::iterator MI
) const {
1411 bool isThumb1
= Subtarget
.isThumb1Only();
1412 bool isThumb2
= Subtarget
.isThumb2();
1413 const ARMBaseInstrInfo
*TII
= Subtarget
.getInstrInfo();
1415 DebugLoc dl
= MI
->getDebugLoc();
1416 MachineBasicBlock
*BB
= MI
->getParent();
1418 MachineInstrBuilder LDM
, STM
;
1419 if (isThumb1
|| !MI
->getOperand(1).isDead()) {
1420 MachineOperand
LDWb(MI
->getOperand(1));
1421 LDM
= BuildMI(*BB
, MI
, dl
, TII
->get(isThumb2
? ARM::t2LDMIA_UPD
1422 : isThumb1
? ARM::tLDMIA_UPD
1426 LDM
= BuildMI(*BB
, MI
, dl
, TII
->get(isThumb2
? ARM::t2LDMIA
: ARM::LDMIA
));
1429 if (isThumb1
|| !MI
->getOperand(0).isDead()) {
1430 MachineOperand
STWb(MI
->getOperand(0));
1431 STM
= BuildMI(*BB
, MI
, dl
, TII
->get(isThumb2
? ARM::t2STMIA_UPD
1432 : isThumb1
? ARM::tSTMIA_UPD
1436 STM
= BuildMI(*BB
, MI
, dl
, TII
->get(isThumb2
? ARM::t2STMIA
: ARM::STMIA
));
1439 MachineOperand
LDBase(MI
->getOperand(3));
1440 LDM
.add(LDBase
).add(predOps(ARMCC::AL
));
1442 MachineOperand
STBase(MI
->getOperand(2));
1443 STM
.add(STBase
).add(predOps(ARMCC::AL
));
1445 // Sort the scratch registers into ascending order.
1446 const TargetRegisterInfo
&TRI
= getRegisterInfo();
1447 SmallVector
<unsigned, 6> ScratchRegs
;
1448 for(unsigned I
= 5; I
< MI
->getNumOperands(); ++I
)
1449 ScratchRegs
.push_back(MI
->getOperand(I
).getReg());
1450 llvm::sort(ScratchRegs
,
1451 [&TRI
](const unsigned &Reg1
, const unsigned &Reg2
) -> bool {
1452 return TRI
.getEncodingValue(Reg1
) <
1453 TRI
.getEncodingValue(Reg2
);
1456 for (const auto &Reg
: ScratchRegs
) {
1457 LDM
.addReg(Reg
, RegState::Define
);
1458 STM
.addReg(Reg
, RegState::Kill
);
1464 bool ARMBaseInstrInfo::expandPostRAPseudo(MachineInstr
&MI
) const {
1465 if (MI
.getOpcode() == TargetOpcode::LOAD_STACK_GUARD
) {
1466 assert(getSubtarget().getTargetTriple().isOSBinFormatMachO() &&
1467 "LOAD_STACK_GUARD currently supported only for MachO.");
1468 expandLoadStackGuard(MI
);
1469 MI
.getParent()->erase(MI
);
1473 if (MI
.getOpcode() == ARM::MEMCPY
) {
1478 // This hook gets to expand COPY instructions before they become
1479 // copyPhysReg() calls. Look for VMOVS instructions that can legally be
1480 // widened to VMOVD. We prefer the VMOVD when possible because it may be
1481 // changed into a VORR that can go down the NEON pipeline.
1482 if (!MI
.isCopy() || Subtarget
.dontWidenVMOVS() || Subtarget
.isFPOnlySP())
1485 // Look for a copy between even S-registers. That is where we keep floats
1486 // when using NEON v2f32 instructions for f32 arithmetic.
1487 unsigned DstRegS
= MI
.getOperand(0).getReg();
1488 unsigned SrcRegS
= MI
.getOperand(1).getReg();
1489 if (!ARM::SPRRegClass
.contains(DstRegS
, SrcRegS
))
1492 const TargetRegisterInfo
*TRI
= &getRegisterInfo();
1493 unsigned DstRegD
= TRI
->getMatchingSuperReg(DstRegS
, ARM::ssub_0
,
1495 unsigned SrcRegD
= TRI
->getMatchingSuperReg(SrcRegS
, ARM::ssub_0
,
1497 if (!DstRegD
|| !SrcRegD
)
1500 // We want to widen this into a DstRegD = VMOVD SrcRegD copy. This is only
1501 // legal if the COPY already defines the full DstRegD, and it isn't a
1502 // sub-register insertion.
1503 if (!MI
.definesRegister(DstRegD
, TRI
) || MI
.readsRegister(DstRegD
, TRI
))
1506 // A dead copy shouldn't show up here, but reject it just in case.
1507 if (MI
.getOperand(0).isDead())
1510 // All clear, widen the COPY.
1511 LLVM_DEBUG(dbgs() << "widening: " << MI
);
1512 MachineInstrBuilder
MIB(*MI
.getParent()->getParent(), MI
);
1514 // Get rid of the old implicit-def of DstRegD. Leave it if it defines a Q-reg
1515 // or some other super-register.
1516 int ImpDefIdx
= MI
.findRegisterDefOperandIdx(DstRegD
);
1517 if (ImpDefIdx
!= -1)
1518 MI
.RemoveOperand(ImpDefIdx
);
1520 // Change the opcode and operands.
1521 MI
.setDesc(get(ARM::VMOVD
));
1522 MI
.getOperand(0).setReg(DstRegD
);
1523 MI
.getOperand(1).setReg(SrcRegD
);
1524 MIB
.add(predOps(ARMCC::AL
));
1526 // We are now reading SrcRegD instead of SrcRegS. This may upset the
1527 // register scavenger and machine verifier, so we need to indicate that we
1528 // are reading an undefined value from SrcRegD, but a proper value from
1530 MI
.getOperand(1).setIsUndef();
1531 MIB
.addReg(SrcRegS
, RegState::Implicit
);
1533 // SrcRegD may actually contain an unrelated value in the ssub_1
1534 // sub-register. Don't kill it. Only kill the ssub_0 sub-register.
1535 if (MI
.getOperand(1).isKill()) {
1536 MI
.getOperand(1).setIsKill(false);
1537 MI
.addRegisterKilled(SrcRegS
, TRI
, true);
1540 LLVM_DEBUG(dbgs() << "replaced by: " << MI
);
1544 /// Create a copy of a const pool value. Update CPI to the new index and return
1546 static unsigned duplicateCPV(MachineFunction
&MF
, unsigned &CPI
) {
1547 MachineConstantPool
*MCP
= MF
.getConstantPool();
1548 ARMFunctionInfo
*AFI
= MF
.getInfo
<ARMFunctionInfo
>();
1550 const MachineConstantPoolEntry
&MCPE
= MCP
->getConstants()[CPI
];
1551 assert(MCPE
.isMachineConstantPoolEntry() &&
1552 "Expecting a machine constantpool entry!");
1553 ARMConstantPoolValue
*ACPV
=
1554 static_cast<ARMConstantPoolValue
*>(MCPE
.Val
.MachineCPVal
);
1556 unsigned PCLabelId
= AFI
->createPICLabelUId();
1557 ARMConstantPoolValue
*NewCPV
= nullptr;
1559 // FIXME: The below assumes PIC relocation model and that the function
1560 // is Thumb mode (t1 or t2). PCAdjustment would be 8 for ARM mode PIC, and
1561 // zero for non-PIC in ARM or Thumb. The callers are all of thumb LDR
1562 // instructions, so that's probably OK, but is PIC always correct when
1564 if (ACPV
->isGlobalValue())
1565 NewCPV
= ARMConstantPoolConstant::Create(
1566 cast
<ARMConstantPoolConstant
>(ACPV
)->getGV(), PCLabelId
, ARMCP::CPValue
,
1567 4, ACPV
->getModifier(), ACPV
->mustAddCurrentAddress());
1568 else if (ACPV
->isExtSymbol())
1569 NewCPV
= ARMConstantPoolSymbol::
1570 Create(MF
.getFunction().getContext(),
1571 cast
<ARMConstantPoolSymbol
>(ACPV
)->getSymbol(), PCLabelId
, 4);
1572 else if (ACPV
->isBlockAddress())
1573 NewCPV
= ARMConstantPoolConstant::
1574 Create(cast
<ARMConstantPoolConstant
>(ACPV
)->getBlockAddress(), PCLabelId
,
1575 ARMCP::CPBlockAddress
, 4);
1576 else if (ACPV
->isLSDA())
1577 NewCPV
= ARMConstantPoolConstant::Create(&MF
.getFunction(), PCLabelId
,
1579 else if (ACPV
->isMachineBasicBlock())
1580 NewCPV
= ARMConstantPoolMBB::
1581 Create(MF
.getFunction().getContext(),
1582 cast
<ARMConstantPoolMBB
>(ACPV
)->getMBB(), PCLabelId
, 4);
1584 llvm_unreachable("Unexpected ARM constantpool value type!!");
1585 CPI
= MCP
->getConstantPoolIndex(NewCPV
, MCPE
.getAlignment());
1589 void ARMBaseInstrInfo::reMaterialize(MachineBasicBlock
&MBB
,
1590 MachineBasicBlock::iterator I
,
1591 unsigned DestReg
, unsigned SubIdx
,
1592 const MachineInstr
&Orig
,
1593 const TargetRegisterInfo
&TRI
) const {
1594 unsigned Opcode
= Orig
.getOpcode();
1597 MachineInstr
*MI
= MBB
.getParent()->CloneMachineInstr(&Orig
);
1598 MI
->substituteRegister(Orig
.getOperand(0).getReg(), DestReg
, SubIdx
, TRI
);
1602 case ARM::tLDRpci_pic
:
1603 case ARM::t2LDRpci_pic
: {
1604 MachineFunction
&MF
= *MBB
.getParent();
1605 unsigned CPI
= Orig
.getOperand(1).getIndex();
1606 unsigned PCLabelId
= duplicateCPV(MF
, CPI
);
1607 BuildMI(MBB
, I
, Orig
.getDebugLoc(), get(Opcode
), DestReg
)
1608 .addConstantPoolIndex(CPI
)
1610 .cloneMemRefs(Orig
);
1617 ARMBaseInstrInfo::duplicate(MachineBasicBlock
&MBB
,
1618 MachineBasicBlock::iterator InsertBefore
,
1619 const MachineInstr
&Orig
) const {
1620 MachineInstr
&Cloned
= TargetInstrInfo::duplicate(MBB
, InsertBefore
, Orig
);
1621 MachineBasicBlock::instr_iterator I
= Cloned
.getIterator();
1623 switch (I
->getOpcode()) {
1624 case ARM::tLDRpci_pic
:
1625 case ARM::t2LDRpci_pic
: {
1626 MachineFunction
&MF
= *MBB
.getParent();
1627 unsigned CPI
= I
->getOperand(1).getIndex();
1628 unsigned PCLabelId
= duplicateCPV(MF
, CPI
);
1629 I
->getOperand(1).setIndex(CPI
);
1630 I
->getOperand(2).setImm(PCLabelId
);
1634 if (!I
->isBundledWithSucc())
1641 bool ARMBaseInstrInfo::produceSameValue(const MachineInstr
&MI0
,
1642 const MachineInstr
&MI1
,
1643 const MachineRegisterInfo
*MRI
) const {
1644 unsigned Opcode
= MI0
.getOpcode();
1645 if (Opcode
== ARM::t2LDRpci
||
1646 Opcode
== ARM::t2LDRpci_pic
||
1647 Opcode
== ARM::tLDRpci
||
1648 Opcode
== ARM::tLDRpci_pic
||
1649 Opcode
== ARM::LDRLIT_ga_pcrel
||
1650 Opcode
== ARM::LDRLIT_ga_pcrel_ldr
||
1651 Opcode
== ARM::tLDRLIT_ga_pcrel
||
1652 Opcode
== ARM::MOV_ga_pcrel
||
1653 Opcode
== ARM::MOV_ga_pcrel_ldr
||
1654 Opcode
== ARM::t2MOV_ga_pcrel
) {
1655 if (MI1
.getOpcode() != Opcode
)
1657 if (MI0
.getNumOperands() != MI1
.getNumOperands())
1660 const MachineOperand
&MO0
= MI0
.getOperand(1);
1661 const MachineOperand
&MO1
= MI1
.getOperand(1);
1662 if (MO0
.getOffset() != MO1
.getOffset())
1665 if (Opcode
== ARM::LDRLIT_ga_pcrel
||
1666 Opcode
== ARM::LDRLIT_ga_pcrel_ldr
||
1667 Opcode
== ARM::tLDRLIT_ga_pcrel
||
1668 Opcode
== ARM::MOV_ga_pcrel
||
1669 Opcode
== ARM::MOV_ga_pcrel_ldr
||
1670 Opcode
== ARM::t2MOV_ga_pcrel
)
1671 // Ignore the PC labels.
1672 return MO0
.getGlobal() == MO1
.getGlobal();
1674 const MachineFunction
*MF
= MI0
.getParent()->getParent();
1675 const MachineConstantPool
*MCP
= MF
->getConstantPool();
1676 int CPI0
= MO0
.getIndex();
1677 int CPI1
= MO1
.getIndex();
1678 const MachineConstantPoolEntry
&MCPE0
= MCP
->getConstants()[CPI0
];
1679 const MachineConstantPoolEntry
&MCPE1
= MCP
->getConstants()[CPI1
];
1680 bool isARMCP0
= MCPE0
.isMachineConstantPoolEntry();
1681 bool isARMCP1
= MCPE1
.isMachineConstantPoolEntry();
1682 if (isARMCP0
&& isARMCP1
) {
1683 ARMConstantPoolValue
*ACPV0
=
1684 static_cast<ARMConstantPoolValue
*>(MCPE0
.Val
.MachineCPVal
);
1685 ARMConstantPoolValue
*ACPV1
=
1686 static_cast<ARMConstantPoolValue
*>(MCPE1
.Val
.MachineCPVal
);
1687 return ACPV0
->hasSameValue(ACPV1
);
1688 } else if (!isARMCP0
&& !isARMCP1
) {
1689 return MCPE0
.Val
.ConstVal
== MCPE1
.Val
.ConstVal
;
1692 } else if (Opcode
== ARM::PICLDR
) {
1693 if (MI1
.getOpcode() != Opcode
)
1695 if (MI0
.getNumOperands() != MI1
.getNumOperands())
1698 unsigned Addr0
= MI0
.getOperand(1).getReg();
1699 unsigned Addr1
= MI1
.getOperand(1).getReg();
1700 if (Addr0
!= Addr1
) {
1702 !TargetRegisterInfo::isVirtualRegister(Addr0
) ||
1703 !TargetRegisterInfo::isVirtualRegister(Addr1
))
1706 // This assumes SSA form.
1707 MachineInstr
*Def0
= MRI
->getVRegDef(Addr0
);
1708 MachineInstr
*Def1
= MRI
->getVRegDef(Addr1
);
1709 // Check if the loaded value, e.g. a constantpool of a global address, are
1711 if (!produceSameValue(*Def0
, *Def1
, MRI
))
1715 for (unsigned i
= 3, e
= MI0
.getNumOperands(); i
!= e
; ++i
) {
1716 // %12 = PICLDR %11, 0, 14, %noreg
1717 const MachineOperand
&MO0
= MI0
.getOperand(i
);
1718 const MachineOperand
&MO1
= MI1
.getOperand(i
);
1719 if (!MO0
.isIdenticalTo(MO1
))
1725 return MI0
.isIdenticalTo(MI1
, MachineInstr::IgnoreVRegDefs
);
1728 /// areLoadsFromSameBasePtr - This is used by the pre-regalloc scheduler to
1729 /// determine if two loads are loading from the same base address. It should
1730 /// only return true if the base pointers are the same and the only differences
1731 /// between the two addresses is the offset. It also returns the offsets by
1734 /// FIXME: remove this in favor of the MachineInstr interface once pre-RA-sched
1735 /// is permanently disabled.
1736 bool ARMBaseInstrInfo::areLoadsFromSameBasePtr(SDNode
*Load1
, SDNode
*Load2
,
1738 int64_t &Offset2
) const {
1739 // Don't worry about Thumb: just ARM and Thumb2.
1740 if (Subtarget
.isThumb1Only()) return false;
1742 if (!Load1
->isMachineOpcode() || !Load2
->isMachineOpcode())
1745 switch (Load1
->getMachineOpcode()) {
1759 case ARM::t2LDRSHi8
:
1761 case ARM::t2LDRBi12
:
1762 case ARM::t2LDRSHi12
:
1766 switch (Load2
->getMachineOpcode()) {
1779 case ARM::t2LDRSHi8
:
1781 case ARM::t2LDRBi12
:
1782 case ARM::t2LDRSHi12
:
1786 // Check if base addresses and chain operands match.
1787 if (Load1
->getOperand(0) != Load2
->getOperand(0) ||
1788 Load1
->getOperand(4) != Load2
->getOperand(4))
1791 // Index should be Reg0.
1792 if (Load1
->getOperand(3) != Load2
->getOperand(3))
1795 // Determine the offsets.
1796 if (isa
<ConstantSDNode
>(Load1
->getOperand(1)) &&
1797 isa
<ConstantSDNode
>(Load2
->getOperand(1))) {
1798 Offset1
= cast
<ConstantSDNode
>(Load1
->getOperand(1))->getSExtValue();
1799 Offset2
= cast
<ConstantSDNode
>(Load2
->getOperand(1))->getSExtValue();
1806 /// shouldScheduleLoadsNear - This is a used by the pre-regalloc scheduler to
1807 /// determine (in conjunction with areLoadsFromSameBasePtr) if two loads should
1808 /// be scheduled togther. On some targets if two loads are loading from
1809 /// addresses in the same cache line, it's better if they are scheduled
1810 /// together. This function takes two integers that represent the load offsets
1811 /// from the common base address. It returns true if it decides it's desirable
1812 /// to schedule the two loads together. "NumLoads" is the number of loads that
1813 /// have already been scheduled after Load1.
1815 /// FIXME: remove this in favor of the MachineInstr interface once pre-RA-sched
1816 /// is permanently disabled.
1817 bool ARMBaseInstrInfo::shouldScheduleLoadsNear(SDNode
*Load1
, SDNode
*Load2
,
1818 int64_t Offset1
, int64_t Offset2
,
1819 unsigned NumLoads
) const {
1820 // Don't worry about Thumb: just ARM and Thumb2.
1821 if (Subtarget
.isThumb1Only()) return false;
1823 assert(Offset2
> Offset1
);
1825 if ((Offset2
- Offset1
) / 8 > 64)
1828 // Check if the machine opcodes are different. If they are different
1829 // then we consider them to not be of the same base address,
1830 // EXCEPT in the case of Thumb2 byte loads where one is LDRBi8 and the other LDRBi12.
1831 // In this case, they are considered to be the same because they are different
1832 // encoding forms of the same basic instruction.
1833 if ((Load1
->getMachineOpcode() != Load2
->getMachineOpcode()) &&
1834 !((Load1
->getMachineOpcode() == ARM::t2LDRBi8
&&
1835 Load2
->getMachineOpcode() == ARM::t2LDRBi12
) ||
1836 (Load1
->getMachineOpcode() == ARM::t2LDRBi12
&&
1837 Load2
->getMachineOpcode() == ARM::t2LDRBi8
)))
1838 return false; // FIXME: overly conservative?
1840 // Four loads in a row should be sufficient.
1847 bool ARMBaseInstrInfo::isSchedulingBoundary(const MachineInstr
&MI
,
1848 const MachineBasicBlock
*MBB
,
1849 const MachineFunction
&MF
) const {
1850 // Debug info is never a scheduling boundary. It's necessary to be explicit
1851 // due to the special treatment of IT instructions below, otherwise a
1852 // dbg_value followed by an IT will result in the IT instruction being
1853 // considered a scheduling hazard, which is wrong. It should be the actual
1854 // instruction preceding the dbg_value instruction(s), just like it is
1855 // when debug info is not present.
1856 if (MI
.isDebugInstr())
1859 // Terminators and labels can't be scheduled around.
1860 if (MI
.isTerminator() || MI
.isPosition())
1863 // Treat the start of the IT block as a scheduling boundary, but schedule
1864 // t2IT along with all instructions following it.
1865 // FIXME: This is a big hammer. But the alternative is to add all potential
1866 // true and anti dependencies to IT block instructions as implicit operands
1867 // to the t2IT instruction. The added compile time and complexity does not
1869 MachineBasicBlock::const_iterator I
= MI
;
1870 // Make sure to skip any debug instructions
1871 while (++I
!= MBB
->end() && I
->isDebugInstr())
1873 if (I
!= MBB
->end() && I
->getOpcode() == ARM::t2IT
)
1876 // Don't attempt to schedule around any instruction that defines
1877 // a stack-oriented pointer, as it's unlikely to be profitable. This
1878 // saves compile time, because it doesn't require every single
1879 // stack slot reference to depend on the instruction that does the
1881 // Calls don't actually change the stack pointer, even if they have imp-defs.
1882 // No ARM calling conventions change the stack pointer. (X86 calling
1883 // conventions sometimes do).
1884 if (!MI
.isCall() && MI
.definesRegister(ARM::SP
))
1890 bool ARMBaseInstrInfo::
1891 isProfitableToIfCvt(MachineBasicBlock
&MBB
,
1892 unsigned NumCycles
, unsigned ExtraPredCycles
,
1893 BranchProbability Probability
) const {
1897 // If we are optimizing for size, see if the branch in the predecessor can be
1898 // lowered to cbn?z by the constant island lowering pass, and return false if
1899 // so. This results in a shorter instruction sequence.
1900 if (MBB
.getParent()->getFunction().optForSize()) {
1901 MachineBasicBlock
*Pred
= *MBB
.pred_begin();
1902 if (!Pred
->empty()) {
1903 MachineInstr
*LastMI
= &*Pred
->rbegin();
1904 if (LastMI
->getOpcode() == ARM::t2Bcc
) {
1905 MachineBasicBlock::iterator CmpMI
= LastMI
;
1906 if (CmpMI
!= Pred
->begin()) {
1908 if (CmpMI
->getOpcode() == ARM::tCMPi8
||
1909 CmpMI
->getOpcode() == ARM::t2CMPri
) {
1910 unsigned Reg
= CmpMI
->getOperand(0).getReg();
1911 unsigned PredReg
= 0;
1912 ARMCC::CondCodes P
= getInstrPredicate(*CmpMI
, PredReg
);
1913 if (P
== ARMCC::AL
&& CmpMI
->getOperand(1).getImm() == 0 &&
1914 isARMLowRegister(Reg
))
1921 return isProfitableToIfCvt(MBB
, NumCycles
, ExtraPredCycles
,
1922 MBB
, 0, 0, Probability
);
1925 bool ARMBaseInstrInfo::
1926 isProfitableToIfCvt(MachineBasicBlock
&TBB
,
1927 unsigned TCycles
, unsigned TExtra
,
1928 MachineBasicBlock
&FBB
,
1929 unsigned FCycles
, unsigned FExtra
,
1930 BranchProbability Probability
) const {
1934 // Attempt to estimate the relative costs of predication versus branching.
1935 // Here we scale up each component of UnpredCost to avoid precision issue when
1936 // scaling TCycles/FCycles by Probability.
1937 const unsigned ScalingUpFactor
= 1024;
1939 unsigned PredCost
= (TCycles
+ FCycles
+ TExtra
+ FExtra
) * ScalingUpFactor
;
1940 unsigned UnpredCost
;
1941 if (!Subtarget
.hasBranchPredictor()) {
1942 // When we don't have a branch predictor it's always cheaper to not take a
1943 // branch than take it, so we have to take that into account.
1944 unsigned NotTakenBranchCost
= 1;
1945 unsigned TakenBranchCost
= Subtarget
.getMispredictionPenalty();
1946 unsigned TUnpredCycles
, FUnpredCycles
;
1948 // Triangle: TBB is the fallthrough
1949 TUnpredCycles
= TCycles
+ NotTakenBranchCost
;
1950 FUnpredCycles
= TakenBranchCost
;
1952 // Diamond: TBB is the block that is branched to, FBB is the fallthrough
1953 TUnpredCycles
= TCycles
+ TakenBranchCost
;
1954 FUnpredCycles
= FCycles
+ NotTakenBranchCost
;
1955 // The branch at the end of FBB will disappear when it's predicated, so
1956 // discount it from PredCost.
1957 PredCost
-= 1 * ScalingUpFactor
;
1959 // The total cost is the cost of each path scaled by their probabilites
1960 unsigned TUnpredCost
= Probability
.scale(TUnpredCycles
* ScalingUpFactor
);
1961 unsigned FUnpredCost
= Probability
.getCompl().scale(FUnpredCycles
* ScalingUpFactor
);
1962 UnpredCost
= TUnpredCost
+ FUnpredCost
;
1963 // When predicating assume that the first IT can be folded away but later
1964 // ones cost one cycle each
1965 if (Subtarget
.isThumb2() && TCycles
+ FCycles
> 4) {
1966 PredCost
+= ((TCycles
+ FCycles
- 4) / 4) * ScalingUpFactor
;
1969 unsigned TUnpredCost
= Probability
.scale(TCycles
* ScalingUpFactor
);
1970 unsigned FUnpredCost
=
1971 Probability
.getCompl().scale(FCycles
* ScalingUpFactor
);
1972 UnpredCost
= TUnpredCost
+ FUnpredCost
;
1973 UnpredCost
+= 1 * ScalingUpFactor
; // The branch itself
1974 UnpredCost
+= Subtarget
.getMispredictionPenalty() * ScalingUpFactor
/ 10;
1977 return PredCost
<= UnpredCost
;
1981 ARMBaseInstrInfo::isProfitableToUnpredicate(MachineBasicBlock
&TMBB
,
1982 MachineBasicBlock
&FMBB
) const {
1983 // Reduce false anti-dependencies to let the target's out-of-order execution
1984 // engine do its thing.
1985 return Subtarget
.isProfitableToUnpredicate();
1988 /// getInstrPredicate - If instruction is predicated, returns its predicate
1989 /// condition, otherwise returns AL. It also returns the condition code
1990 /// register by reference.
1991 ARMCC::CondCodes
llvm::getInstrPredicate(const MachineInstr
&MI
,
1992 unsigned &PredReg
) {
1993 int PIdx
= MI
.findFirstPredOperandIdx();
1999 PredReg
= MI
.getOperand(PIdx
+1).getReg();
2000 return (ARMCC::CondCodes
)MI
.getOperand(PIdx
).getImm();
2003 unsigned llvm::getMatchingCondBranchOpcode(unsigned Opc
) {
2008 if (Opc
== ARM::t2B
)
2011 llvm_unreachable("Unknown unconditional branch opcode!");
2014 MachineInstr
*ARMBaseInstrInfo::commuteInstructionImpl(MachineInstr
&MI
,
2017 unsigned OpIdx2
) const {
2018 switch (MI
.getOpcode()) {
2020 case ARM::t2MOVCCr
: {
2021 // MOVCC can be commuted by inverting the condition.
2022 unsigned PredReg
= 0;
2023 ARMCC::CondCodes CC
= getInstrPredicate(MI
, PredReg
);
2024 // MOVCC AL can't be inverted. Shouldn't happen.
2025 if (CC
== ARMCC::AL
|| PredReg
!= ARM::CPSR
)
2027 MachineInstr
*CommutedMI
=
2028 TargetInstrInfo::commuteInstructionImpl(MI
, NewMI
, OpIdx1
, OpIdx2
);
2031 // After swapping the MOVCC operands, also invert the condition.
2032 CommutedMI
->getOperand(CommutedMI
->findFirstPredOperandIdx())
2033 .setImm(ARMCC::getOppositeCondition(CC
));
2037 return TargetInstrInfo::commuteInstructionImpl(MI
, NewMI
, OpIdx1
, OpIdx2
);
2040 /// Identify instructions that can be folded into a MOVCC instruction, and
2041 /// return the defining instruction.
2042 static MachineInstr
*canFoldIntoMOVCC(unsigned Reg
,
2043 const MachineRegisterInfo
&MRI
,
2044 const TargetInstrInfo
*TII
) {
2045 if (!TargetRegisterInfo::isVirtualRegister(Reg
))
2047 if (!MRI
.hasOneNonDBGUse(Reg
))
2049 MachineInstr
*MI
= MRI
.getVRegDef(Reg
);
2052 // MI is folded into the MOVCC by predicating it.
2053 if (!MI
->isPredicable())
2055 // Check if MI has any non-dead defs or physreg uses. This also detects
2056 // predicated instructions which will be reading CPSR.
2057 for (unsigned i
= 1, e
= MI
->getNumOperands(); i
!= e
; ++i
) {
2058 const MachineOperand
&MO
= MI
->getOperand(i
);
2059 // Reject frame index operands, PEI can't handle the predicated pseudos.
2060 if (MO
.isFI() || MO
.isCPI() || MO
.isJTI())
2064 // MI can't have any tied operands, that would conflict with predication.
2067 if (TargetRegisterInfo::isPhysicalRegister(MO
.getReg()))
2069 if (MO
.isDef() && !MO
.isDead())
2072 bool DontMoveAcrossStores
= true;
2073 if (!MI
->isSafeToMove(/* AliasAnalysis = */ nullptr, DontMoveAcrossStores
))
2078 bool ARMBaseInstrInfo::analyzeSelect(const MachineInstr
&MI
,
2079 SmallVectorImpl
<MachineOperand
> &Cond
,
2080 unsigned &TrueOp
, unsigned &FalseOp
,
2081 bool &Optimizable
) const {
2082 assert((MI
.getOpcode() == ARM::MOVCCr
|| MI
.getOpcode() == ARM::t2MOVCCr
) &&
2083 "Unknown select instruction");
2088 // 3: Condition code.
2092 Cond
.push_back(MI
.getOperand(3));
2093 Cond
.push_back(MI
.getOperand(4));
2094 // We can always fold a def.
2100 ARMBaseInstrInfo::optimizeSelect(MachineInstr
&MI
,
2101 SmallPtrSetImpl
<MachineInstr
*> &SeenMIs
,
2102 bool PreferFalse
) const {
2103 assert((MI
.getOpcode() == ARM::MOVCCr
|| MI
.getOpcode() == ARM::t2MOVCCr
) &&
2104 "Unknown select instruction");
2105 MachineRegisterInfo
&MRI
= MI
.getParent()->getParent()->getRegInfo();
2106 MachineInstr
*DefMI
= canFoldIntoMOVCC(MI
.getOperand(2).getReg(), MRI
, this);
2107 bool Invert
= !DefMI
;
2109 DefMI
= canFoldIntoMOVCC(MI
.getOperand(1).getReg(), MRI
, this);
2113 // Find new register class to use.
2114 MachineOperand FalseReg
= MI
.getOperand(Invert
? 2 : 1);
2115 unsigned DestReg
= MI
.getOperand(0).getReg();
2116 const TargetRegisterClass
*PreviousClass
= MRI
.getRegClass(FalseReg
.getReg());
2117 if (!MRI
.constrainRegClass(DestReg
, PreviousClass
))
2120 // Create a new predicated version of DefMI.
2121 // Rfalse is the first use.
2122 MachineInstrBuilder NewMI
=
2123 BuildMI(*MI
.getParent(), MI
, MI
.getDebugLoc(), DefMI
->getDesc(), DestReg
);
2125 // Copy all the DefMI operands, excluding its (null) predicate.
2126 const MCInstrDesc
&DefDesc
= DefMI
->getDesc();
2127 for (unsigned i
= 1, e
= DefDesc
.getNumOperands();
2128 i
!= e
&& !DefDesc
.OpInfo
[i
].isPredicate(); ++i
)
2129 NewMI
.add(DefMI
->getOperand(i
));
2131 unsigned CondCode
= MI
.getOperand(3).getImm();
2133 NewMI
.addImm(ARMCC::getOppositeCondition(ARMCC::CondCodes(CondCode
)));
2135 NewMI
.addImm(CondCode
);
2136 NewMI
.add(MI
.getOperand(4));
2138 // DefMI is not the -S version that sets CPSR, so add an optional %noreg.
2139 if (NewMI
->hasOptionalDef())
2140 NewMI
.add(condCodeOp());
2142 // The output register value when the predicate is false is an implicit
2143 // register operand tied to the first def.
2144 // The tie makes the register allocator ensure the FalseReg is allocated the
2145 // same register as operand 0.
2146 FalseReg
.setImplicit();
2147 NewMI
.add(FalseReg
);
2148 NewMI
->tieOperands(0, NewMI
->getNumOperands() - 1);
2150 // Update SeenMIs set: register newly created MI and erase removed DefMI.
2151 SeenMIs
.insert(NewMI
);
2152 SeenMIs
.erase(DefMI
);
2154 // If MI is inside a loop, and DefMI is outside the loop, then kill flags on
2155 // DefMI would be invalid when tranferred inside the loop. Checking for a
2156 // loop is expensive, but at least remove kill flags if they are in different
2158 if (DefMI
->getParent() != MI
.getParent())
2159 NewMI
->clearKillInfo();
2161 // The caller will erase MI, but not DefMI.
2162 DefMI
->eraseFromParent();
2166 /// Map pseudo instructions that imply an 'S' bit onto real opcodes. Whether the
2167 /// instruction is encoded with an 'S' bit is determined by the optional CPSR
2170 /// This will go away once we can teach tblgen how to set the optional CPSR def
2172 struct AddSubFlagsOpcodePair
{
2174 uint16_t MachineOpc
;
2177 static const AddSubFlagsOpcodePair AddSubFlagsOpcodeMap
[] = {
2178 {ARM::ADDSri
, ARM::ADDri
},
2179 {ARM::ADDSrr
, ARM::ADDrr
},
2180 {ARM::ADDSrsi
, ARM::ADDrsi
},
2181 {ARM::ADDSrsr
, ARM::ADDrsr
},
2183 {ARM::SUBSri
, ARM::SUBri
},
2184 {ARM::SUBSrr
, ARM::SUBrr
},
2185 {ARM::SUBSrsi
, ARM::SUBrsi
},
2186 {ARM::SUBSrsr
, ARM::SUBrsr
},
2188 {ARM::RSBSri
, ARM::RSBri
},
2189 {ARM::RSBSrsi
, ARM::RSBrsi
},
2190 {ARM::RSBSrsr
, ARM::RSBrsr
},
2192 {ARM::tADDSi3
, ARM::tADDi3
},
2193 {ARM::tADDSi8
, ARM::tADDi8
},
2194 {ARM::tADDSrr
, ARM::tADDrr
},
2195 {ARM::tADCS
, ARM::tADC
},
2197 {ARM::tSUBSi3
, ARM::tSUBi3
},
2198 {ARM::tSUBSi8
, ARM::tSUBi8
},
2199 {ARM::tSUBSrr
, ARM::tSUBrr
},
2200 {ARM::tSBCS
, ARM::tSBC
},
2201 {ARM::tRSBS
, ARM::tRSB
},
2203 {ARM::t2ADDSri
, ARM::t2ADDri
},
2204 {ARM::t2ADDSrr
, ARM::t2ADDrr
},
2205 {ARM::t2ADDSrs
, ARM::t2ADDrs
},
2207 {ARM::t2SUBSri
, ARM::t2SUBri
},
2208 {ARM::t2SUBSrr
, ARM::t2SUBrr
},
2209 {ARM::t2SUBSrs
, ARM::t2SUBrs
},
2211 {ARM::t2RSBSri
, ARM::t2RSBri
},
2212 {ARM::t2RSBSrs
, ARM::t2RSBrs
},
2215 unsigned llvm::convertAddSubFlagsOpcode(unsigned OldOpc
) {
2216 for (unsigned i
= 0, e
= array_lengthof(AddSubFlagsOpcodeMap
); i
!= e
; ++i
)
2217 if (OldOpc
== AddSubFlagsOpcodeMap
[i
].PseudoOpc
)
2218 return AddSubFlagsOpcodeMap
[i
].MachineOpc
;
2222 void llvm::emitARMRegPlusImmediate(MachineBasicBlock
&MBB
,
2223 MachineBasicBlock::iterator
&MBBI
,
2224 const DebugLoc
&dl
, unsigned DestReg
,
2225 unsigned BaseReg
, int NumBytes
,
2226 ARMCC::CondCodes Pred
, unsigned PredReg
,
2227 const ARMBaseInstrInfo
&TII
,
2229 if (NumBytes
== 0 && DestReg
!= BaseReg
) {
2230 BuildMI(MBB
, MBBI
, dl
, TII
.get(ARM::MOVr
), DestReg
)
2231 .addReg(BaseReg
, RegState::Kill
)
2232 .add(predOps(Pred
, PredReg
))
2234 .setMIFlags(MIFlags
);
2238 bool isSub
= NumBytes
< 0;
2239 if (isSub
) NumBytes
= -NumBytes
;
2242 unsigned RotAmt
= ARM_AM::getSOImmValRotate(NumBytes
);
2243 unsigned ThisVal
= NumBytes
& ARM_AM::rotr32(0xFF, RotAmt
);
2244 assert(ThisVal
&& "Didn't extract field correctly");
2246 // We will handle these bits from offset, clear them.
2247 NumBytes
&= ~ThisVal
;
2249 assert(ARM_AM::getSOImmVal(ThisVal
) != -1 && "Bit extraction didn't work?");
2251 // Build the new ADD / SUB.
2252 unsigned Opc
= isSub
? ARM::SUBri
: ARM::ADDri
;
2253 BuildMI(MBB
, MBBI
, dl
, TII
.get(Opc
), DestReg
)
2254 .addReg(BaseReg
, RegState::Kill
)
2256 .add(predOps(Pred
, PredReg
))
2258 .setMIFlags(MIFlags
);
2263 bool llvm::tryFoldSPUpdateIntoPushPop(const ARMSubtarget
&Subtarget
,
2264 MachineFunction
&MF
, MachineInstr
*MI
,
2265 unsigned NumBytes
) {
2266 // This optimisation potentially adds lots of load and store
2267 // micro-operations, it's only really a great benefit to code-size.
2268 if (!Subtarget
.optForMinSize())
2271 // If only one register is pushed/popped, LLVM can use an LDR/STR
2272 // instead. We can't modify those so make sure we're dealing with an
2273 // instruction we understand.
2274 bool IsPop
= isPopOpcode(MI
->getOpcode());
2275 bool IsPush
= isPushOpcode(MI
->getOpcode());
2276 if (!IsPush
&& !IsPop
)
2279 bool IsVFPPushPop
= MI
->getOpcode() == ARM::VSTMDDB_UPD
||
2280 MI
->getOpcode() == ARM::VLDMDIA_UPD
;
2281 bool IsT1PushPop
= MI
->getOpcode() == ARM::tPUSH
||
2282 MI
->getOpcode() == ARM::tPOP
||
2283 MI
->getOpcode() == ARM::tPOP_RET
;
2285 assert((IsT1PushPop
|| (MI
->getOperand(0).getReg() == ARM::SP
&&
2286 MI
->getOperand(1).getReg() == ARM::SP
)) &&
2287 "trying to fold sp update into non-sp-updating push/pop");
2289 // The VFP push & pop act on D-registers, so we can only fold an adjustment
2290 // by a multiple of 8 bytes in correctly. Similarly rN is 4-bytes. Don't try
2291 // if this is violated.
2292 if (NumBytes
% (IsVFPPushPop
? 8 : 4) != 0)
2295 // ARM and Thumb2 push/pop insts have explicit "sp, sp" operands (+
2296 // pred) so the list starts at 4. Thumb1 starts after the predicate.
2297 int RegListIdx
= IsT1PushPop
? 2 : 4;
2299 // Calculate the space we'll need in terms of registers.
2300 unsigned RegsNeeded
;
2301 const TargetRegisterClass
*RegClass
;
2303 RegsNeeded
= NumBytes
/ 8;
2304 RegClass
= &ARM::DPRRegClass
;
2306 RegsNeeded
= NumBytes
/ 4;
2307 RegClass
= &ARM::GPRRegClass
;
2310 // We're going to have to strip all list operands off before
2311 // re-adding them since the order matters, so save the existing ones
2313 SmallVector
<MachineOperand
, 4> RegList
;
2315 // We're also going to need the first register transferred by this
2316 // instruction, which won't necessarily be the first register in the list.
2317 unsigned FirstRegEnc
= -1;
2319 const TargetRegisterInfo
*TRI
= MF
.getRegInfo().getTargetRegisterInfo();
2320 for (int i
= MI
->getNumOperands() - 1; i
>= RegListIdx
; --i
) {
2321 MachineOperand
&MO
= MI
->getOperand(i
);
2322 RegList
.push_back(MO
);
2324 if (MO
.isReg() && TRI
->getEncodingValue(MO
.getReg()) < FirstRegEnc
)
2325 FirstRegEnc
= TRI
->getEncodingValue(MO
.getReg());
2328 const MCPhysReg
*CSRegs
= TRI
->getCalleeSavedRegs(&MF
);
2330 // Now try to find enough space in the reglist to allocate NumBytes.
2331 for (int CurRegEnc
= FirstRegEnc
- 1; CurRegEnc
>= 0 && RegsNeeded
;
2333 unsigned CurReg
= RegClass
->getRegister(CurRegEnc
);
2335 // Pushing any register is completely harmless, mark the register involved
2336 // as undef since we don't care about its value and must not restore it
2337 // during stack unwinding.
2338 RegList
.push_back(MachineOperand::CreateReg(CurReg
, false, false,
2339 false, false, true));
2344 // However, we can only pop an extra register if it's not live. For
2345 // registers live within the function we might clobber a return value
2346 // register; the other way a register can be live here is if it's
2348 if (isCalleeSavedRegister(CurReg
, CSRegs
) ||
2349 MI
->getParent()->computeRegisterLiveness(TRI
, CurReg
, MI
) !=
2350 MachineBasicBlock::LQR_Dead
) {
2351 // VFP pops don't allow holes in the register list, so any skip is fatal
2352 // for our transformation. GPR pops do, so we should just keep looking.
2359 // Mark the unimportant registers as <def,dead> in the POP.
2360 RegList
.push_back(MachineOperand::CreateReg(CurReg
, true, false, false,
2368 // Finally we know we can profitably perform the optimisation so go
2369 // ahead: strip all existing registers off and add them back again
2370 // in the right order.
2371 for (int i
= MI
->getNumOperands() - 1; i
>= RegListIdx
; --i
)
2372 MI
->RemoveOperand(i
);
2374 // Add the complete list back in.
2375 MachineInstrBuilder
MIB(MF
, &*MI
);
2376 for (int i
= RegList
.size() - 1; i
>= 0; --i
)
2377 MIB
.add(RegList
[i
]);
2382 bool llvm::rewriteARMFrameIndex(MachineInstr
&MI
, unsigned FrameRegIdx
,
2383 unsigned FrameReg
, int &Offset
,
2384 const ARMBaseInstrInfo
&TII
) {
2385 unsigned Opcode
= MI
.getOpcode();
2386 const MCInstrDesc
&Desc
= MI
.getDesc();
2387 unsigned AddrMode
= (Desc
.TSFlags
& ARMII::AddrModeMask
);
2390 // Memory operands in inline assembly always use AddrMode2.
2391 if (Opcode
== ARM::INLINEASM
)
2392 AddrMode
= ARMII::AddrMode2
;
2394 if (Opcode
== ARM::ADDri
) {
2395 Offset
+= MI
.getOperand(FrameRegIdx
+1).getImm();
2397 // Turn it into a move.
2398 MI
.setDesc(TII
.get(ARM::MOVr
));
2399 MI
.getOperand(FrameRegIdx
).ChangeToRegister(FrameReg
, false);
2400 MI
.RemoveOperand(FrameRegIdx
+1);
2403 } else if (Offset
< 0) {
2406 MI
.setDesc(TII
.get(ARM::SUBri
));
2409 // Common case: small offset, fits into instruction.
2410 if (ARM_AM::getSOImmVal(Offset
) != -1) {
2411 // Replace the FrameIndex with sp / fp
2412 MI
.getOperand(FrameRegIdx
).ChangeToRegister(FrameReg
, false);
2413 MI
.getOperand(FrameRegIdx
+1).ChangeToImmediate(Offset
);
2418 // Otherwise, pull as much of the immedidate into this ADDri/SUBri
2420 unsigned RotAmt
= ARM_AM::getSOImmValRotate(Offset
);
2421 unsigned ThisImmVal
= Offset
& ARM_AM::rotr32(0xFF, RotAmt
);
2423 // We will handle these bits from offset, clear them.
2424 Offset
&= ~ThisImmVal
;
2426 // Get the properly encoded SOImmVal field.
2427 assert(ARM_AM::getSOImmVal(ThisImmVal
) != -1 &&
2428 "Bit extraction didn't work?");
2429 MI
.getOperand(FrameRegIdx
+1).ChangeToImmediate(ThisImmVal
);
2431 unsigned ImmIdx
= 0;
2433 unsigned NumBits
= 0;
2436 case ARMII::AddrMode_i12
:
2437 ImmIdx
= FrameRegIdx
+ 1;
2438 InstrOffs
= MI
.getOperand(ImmIdx
).getImm();
2441 case ARMII::AddrMode2
:
2442 ImmIdx
= FrameRegIdx
+2;
2443 InstrOffs
= ARM_AM::getAM2Offset(MI
.getOperand(ImmIdx
).getImm());
2444 if (ARM_AM::getAM2Op(MI
.getOperand(ImmIdx
).getImm()) == ARM_AM::sub
)
2448 case ARMII::AddrMode3
:
2449 ImmIdx
= FrameRegIdx
+2;
2450 InstrOffs
= ARM_AM::getAM3Offset(MI
.getOperand(ImmIdx
).getImm());
2451 if (ARM_AM::getAM3Op(MI
.getOperand(ImmIdx
).getImm()) == ARM_AM::sub
)
2455 case ARMII::AddrMode4
:
2456 case ARMII::AddrMode6
:
2457 // Can't fold any offset even if it's zero.
2459 case ARMII::AddrMode5
:
2460 ImmIdx
= FrameRegIdx
+1;
2461 InstrOffs
= ARM_AM::getAM5Offset(MI
.getOperand(ImmIdx
).getImm());
2462 if (ARM_AM::getAM5Op(MI
.getOperand(ImmIdx
).getImm()) == ARM_AM::sub
)
2467 case ARMII::AddrMode5FP16
:
2468 ImmIdx
= FrameRegIdx
+1;
2469 InstrOffs
= ARM_AM::getAM5Offset(MI
.getOperand(ImmIdx
).getImm());
2470 if (ARM_AM::getAM5Op(MI
.getOperand(ImmIdx
).getImm()) == ARM_AM::sub
)
2476 llvm_unreachable("Unsupported addressing mode!");
2479 Offset
+= InstrOffs
* Scale
;
2480 assert((Offset
& (Scale
-1)) == 0 && "Can't encode this offset!");
2486 // Attempt to fold address comp. if opcode has offset bits
2488 // Common case: small offset, fits into instruction.
2489 MachineOperand
&ImmOp
= MI
.getOperand(ImmIdx
);
2490 int ImmedOffset
= Offset
/ Scale
;
2491 unsigned Mask
= (1 << NumBits
) - 1;
2492 if ((unsigned)Offset
<= Mask
* Scale
) {
2493 // Replace the FrameIndex with sp
2494 MI
.getOperand(FrameRegIdx
).ChangeToRegister(FrameReg
, false);
2495 // FIXME: When addrmode2 goes away, this will simplify (like the
2496 // T2 version), as the LDR.i12 versions don't need the encoding
2497 // tricks for the offset value.
2499 if (AddrMode
== ARMII::AddrMode_i12
)
2500 ImmedOffset
= -ImmedOffset
;
2502 ImmedOffset
|= 1 << NumBits
;
2504 ImmOp
.ChangeToImmediate(ImmedOffset
);
2509 // Otherwise, it didn't fit. Pull in what we can to simplify the immed.
2510 ImmedOffset
= ImmedOffset
& Mask
;
2512 if (AddrMode
== ARMII::AddrMode_i12
)
2513 ImmedOffset
= -ImmedOffset
;
2515 ImmedOffset
|= 1 << NumBits
;
2517 ImmOp
.ChangeToImmediate(ImmedOffset
);
2518 Offset
&= ~(Mask
*Scale
);
2522 Offset
= (isSub
) ? -Offset
: Offset
;
2526 /// analyzeCompare - For a comparison instruction, return the source registers
2527 /// in SrcReg and SrcReg2 if having two register operands, and the value it
2528 /// compares against in CmpValue. Return true if the comparison instruction
2529 /// can be analyzed.
2530 bool ARMBaseInstrInfo::analyzeCompare(const MachineInstr
&MI
, unsigned &SrcReg
,
2531 unsigned &SrcReg2
, int &CmpMask
,
2532 int &CmpValue
) const {
2533 switch (MI
.getOpcode()) {
2538 SrcReg
= MI
.getOperand(0).getReg();
2541 CmpValue
= MI
.getOperand(1).getImm();
2545 SrcReg
= MI
.getOperand(0).getReg();
2546 SrcReg2
= MI
.getOperand(1).getReg();
2552 SrcReg
= MI
.getOperand(0).getReg();
2554 CmpMask
= MI
.getOperand(1).getImm();
2562 /// isSuitableForMask - Identify a suitable 'and' instruction that
2563 /// operates on the given source register and applies the same mask
2564 /// as a 'tst' instruction. Provide a limited look-through for copies.
2565 /// When successful, MI will hold the found instruction.
2566 static bool isSuitableForMask(MachineInstr
*&MI
, unsigned SrcReg
,
2567 int CmpMask
, bool CommonUse
) {
2568 switch (MI
->getOpcode()) {
2571 if (CmpMask
!= MI
->getOperand(2).getImm())
2573 if (SrcReg
== MI
->getOperand(CommonUse
? 1 : 0).getReg())
2581 /// getSwappedCondition - assume the flags are set by MI(a,b), return
2582 /// the condition code if we modify the instructions such that flags are
2584 inline static ARMCC::CondCodes
getSwappedCondition(ARMCC::CondCodes CC
) {
2586 default: return ARMCC::AL
;
2587 case ARMCC::EQ
: return ARMCC::EQ
;
2588 case ARMCC::NE
: return ARMCC::NE
;
2589 case ARMCC::HS
: return ARMCC::LS
;
2590 case ARMCC::LO
: return ARMCC::HI
;
2591 case ARMCC::HI
: return ARMCC::LO
;
2592 case ARMCC::LS
: return ARMCC::HS
;
2593 case ARMCC::GE
: return ARMCC::LE
;
2594 case ARMCC::LT
: return ARMCC::GT
;
2595 case ARMCC::GT
: return ARMCC::LT
;
2596 case ARMCC::LE
: return ARMCC::GE
;
2600 /// getCmpToAddCondition - assume the flags are set by CMP(a,b), return
2601 /// the condition code if we modify the instructions such that flags are
2602 /// set by ADD(a,b,X).
2603 inline static ARMCC::CondCodes
getCmpToAddCondition(ARMCC::CondCodes CC
) {
2605 default: return ARMCC::AL
;
2606 case ARMCC::HS
: return ARMCC::LO
;
2607 case ARMCC::LO
: return ARMCC::HS
;
2608 case ARMCC::VS
: return ARMCC::VS
;
2609 case ARMCC::VC
: return ARMCC::VC
;
2613 /// isRedundantFlagInstr - check whether the first instruction, whose only
2614 /// purpose is to update flags, can be made redundant.
2615 /// CMPrr can be made redundant by SUBrr if the operands are the same.
2616 /// CMPri can be made redundant by SUBri if the operands are the same.
2617 /// CMPrr(r0, r1) can be made redundant by ADDr[ri](r0, r1, X).
2618 /// This function can be extended later on.
2619 inline static bool isRedundantFlagInstr(const MachineInstr
*CmpI
,
2620 unsigned SrcReg
, unsigned SrcReg2
,
2621 int ImmValue
, const MachineInstr
*OI
) {
2622 if ((CmpI
->getOpcode() == ARM::CMPrr
|| CmpI
->getOpcode() == ARM::t2CMPrr
) &&
2623 (OI
->getOpcode() == ARM::SUBrr
|| OI
->getOpcode() == ARM::t2SUBrr
) &&
2624 ((OI
->getOperand(1).getReg() == SrcReg
&&
2625 OI
->getOperand(2).getReg() == SrcReg2
) ||
2626 (OI
->getOperand(1).getReg() == SrcReg2
&&
2627 OI
->getOperand(2).getReg() == SrcReg
)))
2630 if ((CmpI
->getOpcode() == ARM::CMPri
|| CmpI
->getOpcode() == ARM::t2CMPri
) &&
2631 (OI
->getOpcode() == ARM::SUBri
|| OI
->getOpcode() == ARM::t2SUBri
) &&
2632 OI
->getOperand(1).getReg() == SrcReg
&&
2633 OI
->getOperand(2).getImm() == ImmValue
)
2636 if ((CmpI
->getOpcode() == ARM::CMPrr
|| CmpI
->getOpcode() == ARM::t2CMPrr
) &&
2637 (OI
->getOpcode() == ARM::ADDrr
|| OI
->getOpcode() == ARM::t2ADDrr
||
2638 OI
->getOpcode() == ARM::ADDri
|| OI
->getOpcode() == ARM::t2ADDri
) &&
2639 OI
->getOperand(0).isReg() && OI
->getOperand(1).isReg() &&
2640 OI
->getOperand(0).getReg() == SrcReg
&&
2641 OI
->getOperand(1).getReg() == SrcReg2
)
2646 static bool isOptimizeCompareCandidate(MachineInstr
*MI
, bool &IsThumb1
) {
2647 switch (MI
->getOpcode()) {
2648 default: return false;
2703 /// optimizeCompareInstr - Convert the instruction supplying the argument to the
2704 /// comparison into one that sets the zero bit in the flags register;
2705 /// Remove a redundant Compare instruction if an earlier instruction can set the
2706 /// flags in the same way as Compare.
2707 /// E.g. SUBrr(r1,r2) and CMPrr(r1,r2). We also handle the case where two
2708 /// operands are swapped: SUBrr(r1,r2) and CMPrr(r2,r1), by updating the
2709 /// condition code of instructions which use the flags.
2710 bool ARMBaseInstrInfo::optimizeCompareInstr(
2711 MachineInstr
&CmpInstr
, unsigned SrcReg
, unsigned SrcReg2
, int CmpMask
,
2712 int CmpValue
, const MachineRegisterInfo
*MRI
) const {
2713 // Get the unique definition of SrcReg.
2714 MachineInstr
*MI
= MRI
->getUniqueVRegDef(SrcReg
);
2715 if (!MI
) return false;
2717 // Masked compares sometimes use the same register as the corresponding 'and'.
2718 if (CmpMask
!= ~0) {
2719 if (!isSuitableForMask(MI
, SrcReg
, CmpMask
, false) || isPredicated(*MI
)) {
2721 for (MachineRegisterInfo::use_instr_iterator
2722 UI
= MRI
->use_instr_begin(SrcReg
), UE
= MRI
->use_instr_end();
2724 if (UI
->getParent() != CmpInstr
.getParent())
2726 MachineInstr
*PotentialAND
= &*UI
;
2727 if (!isSuitableForMask(PotentialAND
, SrcReg
, CmpMask
, true) ||
2728 isPredicated(*PotentialAND
))
2733 if (!MI
) return false;
2737 // Get ready to iterate backward from CmpInstr.
2738 MachineBasicBlock::iterator I
= CmpInstr
, E
= MI
,
2739 B
= CmpInstr
.getParent()->begin();
2741 // Early exit if CmpInstr is at the beginning of the BB.
2742 if (I
== B
) return false;
2744 // There are two possible candidates which can be changed to set CPSR:
2745 // One is MI, the other is a SUB or ADD instruction.
2746 // For CMPrr(r1,r2), we are looking for SUB(r1,r2), SUB(r2,r1), or
2747 // ADDr[ri](r1, r2, X).
2748 // For CMPri(r1, CmpValue), we are looking for SUBri(r1, CmpValue).
2749 MachineInstr
*SubAdd
= nullptr;
2751 // MI is not a candidate for CMPrr.
2753 else if (MI
->getParent() != CmpInstr
.getParent() || CmpValue
!= 0) {
2754 // Conservatively refuse to convert an instruction which isn't in the same
2755 // BB as the comparison.
2756 // For CMPri w/ CmpValue != 0, a SubAdd may still be a candidate.
2757 // Thus we cannot return here.
2758 if (CmpInstr
.getOpcode() == ARM::CMPri
||
2759 CmpInstr
.getOpcode() == ARM::t2CMPri
)
2765 bool IsThumb1
= false;
2766 if (MI
&& !isOptimizeCompareCandidate(MI
, IsThumb1
))
2769 // We also want to do this peephole for cases like this: if (a*b == 0),
2770 // and optimise away the CMP instruction from the generated code sequence:
2771 // MULS, MOVS, MOVS, CMP. Here the MOVS instructions load the boolean values
2772 // resulting from the select instruction, but these MOVS instructions for
2773 // Thumb1 (V6M) are flag setting and are thus preventing this optimisation.
2774 // However, if we only have MOVS instructions in between the CMP and the
2775 // other instruction (the MULS in this example), then the CPSR is dead so we
2776 // can safely reorder the sequence into: MOVS, MOVS, MULS, CMP. We do this
2777 // reordering and then continue the analysis hoping we can eliminate the
2778 // CMP. This peephole works on the vregs, so is still in SSA form. As a
2779 // consequence, the movs won't redefine/kill the MUL operands which would
2780 // make this reordering illegal.
2781 if (MI
&& IsThumb1
) {
2783 bool CanReorder
= true;
2784 const bool HasStmts
= I
!= E
;
2785 for (; I
!= E
; --I
) {
2786 if (I
->getOpcode() != ARM::tMOVi8
) {
2791 if (HasStmts
&& CanReorder
) {
2792 MI
= MI
->removeFromParent();
2794 CmpInstr
.getParent()->insert(E
, MI
);
2800 // Check that CPSR isn't set between the comparison instruction and the one we
2801 // want to change. At the same time, search for SubAdd.
2802 const TargetRegisterInfo
*TRI
= &getRegisterInfo();
2804 const MachineInstr
&Instr
= *--I
;
2806 // Check whether CmpInstr can be made redundant by the current instruction.
2807 if (isRedundantFlagInstr(&CmpInstr
, SrcReg
, SrcReg2
, CmpValue
, &Instr
)) {
2812 // Allow E (which was initially MI) to be SubAdd but do not search before E.
2816 if (Instr
.modifiesRegister(ARM::CPSR
, TRI
) ||
2817 Instr
.readsRegister(ARM::CPSR
, TRI
))
2818 // This instruction modifies or uses CPSR after the one we want to
2819 // change. We can't do this transformation.
2824 // Return false if no candidates exist.
2828 // If we found a SubAdd, use it as it will be closer to the CMP
2834 // We can't use a predicated instruction - it doesn't always write the flags.
2835 if (isPredicated(*MI
))
2838 // Scan forward for the use of CPSR
2839 // When checking against MI: if it's a conditional code that requires
2840 // checking of the V bit or C bit, then this is not safe to do.
2841 // It is safe to remove CmpInstr if CPSR is redefined or killed.
2842 // If we are done with the basic block, we need to check whether CPSR is
2844 SmallVector
<std::pair
<MachineOperand
*, ARMCC::CondCodes
>, 4>
2846 bool isSafe
= false;
2848 E
= CmpInstr
.getParent()->end();
2849 while (!isSafe
&& ++I
!= E
) {
2850 const MachineInstr
&Instr
= *I
;
2851 for (unsigned IO
= 0, EO
= Instr
.getNumOperands();
2852 !isSafe
&& IO
!= EO
; ++IO
) {
2853 const MachineOperand
&MO
= Instr
.getOperand(IO
);
2854 if (MO
.isRegMask() && MO
.clobbersPhysReg(ARM::CPSR
)) {
2858 if (!MO
.isReg() || MO
.getReg() != ARM::CPSR
)
2864 // Condition code is after the operand before CPSR except for VSELs.
2865 ARMCC::CondCodes CC
;
2866 bool IsInstrVSel
= true;
2867 switch (Instr
.getOpcode()) {
2869 IsInstrVSel
= false;
2870 CC
= (ARMCC::CondCodes
)Instr
.getOperand(IO
- 1).getImm();
2891 // If we have SUB(r1, r2) and CMP(r2, r1), the condition code based
2892 // on CMP needs to be updated to be based on SUB.
2893 // If we have ADD(r1, r2, X) and CMP(r1, r2), the condition code also
2894 // needs to be modified.
2895 // Push the condition code operands to OperandsToUpdate.
2896 // If it is safe to remove CmpInstr, the condition code of these
2897 // operands will be modified.
2898 unsigned Opc
= SubAdd
->getOpcode();
2899 bool IsSub
= Opc
== ARM::SUBrr
|| Opc
== ARM::t2SUBrr
||
2900 Opc
== ARM::SUBri
|| Opc
== ARM::t2SUBri
;
2901 if (!IsSub
|| (SrcReg2
!= 0 && SubAdd
->getOperand(1).getReg() == SrcReg2
&&
2902 SubAdd
->getOperand(2).getReg() == SrcReg
)) {
2903 // VSel doesn't support condition code update.
2906 // Ensure we can swap the condition.
2907 ARMCC::CondCodes NewCC
= (IsSub
? getSwappedCondition(CC
) : getCmpToAddCondition(CC
));
2908 if (NewCC
== ARMCC::AL
)
2910 OperandsToUpdate
.push_back(
2911 std::make_pair(&((*I
).getOperand(IO
- 1)), NewCC
));
2914 // No SubAdd, so this is x = <op> y, z; cmp x, 0.
2916 case ARMCC::EQ
: // Z
2917 case ARMCC::NE
: // Z
2918 case ARMCC::MI
: // N
2919 case ARMCC::PL
: // N
2920 case ARMCC::AL
: // none
2921 // CPSR can be used multiple times, we should continue.
2923 case ARMCC::HS
: // C
2924 case ARMCC::LO
: // C
2925 case ARMCC::VS
: // V
2926 case ARMCC::VC
: // V
2927 case ARMCC::HI
: // C Z
2928 case ARMCC::LS
: // C Z
2929 case ARMCC::GE
: // N V
2930 case ARMCC::LT
: // N V
2931 case ARMCC::GT
: // Z N V
2932 case ARMCC::LE
: // Z N V
2933 // The instruction uses the V bit or C bit which is not safe.
2940 // If CPSR is not killed nor re-defined, we should check whether it is
2941 // live-out. If it is live-out, do not optimize.
2943 MachineBasicBlock
*MBB
= CmpInstr
.getParent();
2944 for (MachineBasicBlock::succ_iterator SI
= MBB
->succ_begin(),
2945 SE
= MBB
->succ_end(); SI
!= SE
; ++SI
)
2946 if ((*SI
)->isLiveIn(ARM::CPSR
))
2950 // Toggle the optional operand to CPSR (if it exists - in Thumb1 we always
2951 // set CPSR so this is represented as an explicit output)
2953 MI
->getOperand(5).setReg(ARM::CPSR
);
2954 MI
->getOperand(5).setIsDef(true);
2956 assert(!isPredicated(*MI
) && "Can't use flags from predicated instruction");
2957 CmpInstr
.eraseFromParent();
2959 // Modify the condition code of operands in OperandsToUpdate.
2960 // Since we have SUB(r1, r2) and CMP(r2, r1), the condition code needs to
2961 // be changed from r2 > r1 to r1 < r2, from r2 < r1 to r1 > r2, etc.
2962 for (unsigned i
= 0, e
= OperandsToUpdate
.size(); i
< e
; i
++)
2963 OperandsToUpdate
[i
].first
->setImm(OperandsToUpdate
[i
].second
);
2965 MI
->clearRegisterDeads(ARM::CPSR
);
2970 bool ARMBaseInstrInfo::shouldSink(const MachineInstr
&MI
) const {
2971 // Do not sink MI if it might be used to optimize a redundant compare.
2972 // We heuristically only look at the instruction immediately following MI to
2973 // avoid potentially searching the entire basic block.
2974 if (isPredicated(MI
))
2976 MachineBasicBlock::const_iterator Next
= &MI
;
2978 unsigned SrcReg
, SrcReg2
;
2979 int CmpMask
, CmpValue
;
2980 if (Next
!= MI
.getParent()->end() &&
2981 analyzeCompare(*Next
, SrcReg
, SrcReg2
, CmpMask
, CmpValue
) &&
2982 isRedundantFlagInstr(&*Next
, SrcReg
, SrcReg2
, CmpValue
, &MI
))
2987 bool ARMBaseInstrInfo::FoldImmediate(MachineInstr
&UseMI
, MachineInstr
&DefMI
,
2989 MachineRegisterInfo
*MRI
) const {
2990 // Fold large immediates into add, sub, or, xor.
2991 unsigned DefOpc
= DefMI
.getOpcode();
2992 if (DefOpc
!= ARM::t2MOVi32imm
&& DefOpc
!= ARM::MOVi32imm
)
2994 if (!DefMI
.getOperand(1).isImm())
2995 // Could be t2MOVi32imm @xx
2998 if (!MRI
->hasOneNonDBGUse(Reg
))
3001 const MCInstrDesc
&DefMCID
= DefMI
.getDesc();
3002 if (DefMCID
.hasOptionalDef()) {
3003 unsigned NumOps
= DefMCID
.getNumOperands();
3004 const MachineOperand
&MO
= DefMI
.getOperand(NumOps
- 1);
3005 if (MO
.getReg() == ARM::CPSR
&& !MO
.isDead())
3006 // If DefMI defines CPSR and it is not dead, it's obviously not safe
3011 const MCInstrDesc
&UseMCID
= UseMI
.getDesc();
3012 if (UseMCID
.hasOptionalDef()) {
3013 unsigned NumOps
= UseMCID
.getNumOperands();
3014 if (UseMI
.getOperand(NumOps
- 1).getReg() == ARM::CPSR
)
3015 // If the instruction sets the flag, do not attempt this optimization
3016 // since it may change the semantics of the code.
3020 unsigned UseOpc
= UseMI
.getOpcode();
3021 unsigned NewUseOpc
= 0;
3022 uint32_t ImmVal
= (uint32_t)DefMI
.getOperand(1).getImm();
3023 uint32_t SOImmValV1
= 0, SOImmValV2
= 0;
3024 bool Commute
= false;
3026 default: return false;
3034 case ARM::t2EORrr
: {
3035 Commute
= UseMI
.getOperand(2).getReg() != Reg
;
3040 if (UseOpc
== ARM::SUBrr
&& Commute
)
3043 // ADD/SUB are special because they're essentially the same operation, so
3044 // we can handle a larger range of immediates.
3045 if (ARM_AM::isSOImmTwoPartVal(ImmVal
))
3046 NewUseOpc
= UseOpc
== ARM::ADDrr
? ARM::ADDri
: ARM::SUBri
;
3047 else if (ARM_AM::isSOImmTwoPartVal(-ImmVal
)) {
3049 NewUseOpc
= UseOpc
== ARM::ADDrr
? ARM::SUBri
: ARM::ADDri
;
3052 SOImmValV1
= (uint32_t)ARM_AM::getSOImmTwoPartFirst(ImmVal
);
3053 SOImmValV2
= (uint32_t)ARM_AM::getSOImmTwoPartSecond(ImmVal
);
3057 if (!ARM_AM::isSOImmTwoPartVal(ImmVal
))
3059 SOImmValV1
= (uint32_t)ARM_AM::getSOImmTwoPartFirst(ImmVal
);
3060 SOImmValV2
= (uint32_t)ARM_AM::getSOImmTwoPartSecond(ImmVal
);
3063 case ARM::ORRrr
: NewUseOpc
= ARM::ORRri
; break;
3064 case ARM::EORrr
: NewUseOpc
= ARM::EORri
; break;
3069 if (UseOpc
== ARM::t2SUBrr
&& Commute
)
3072 // ADD/SUB are special because they're essentially the same operation, so
3073 // we can handle a larger range of immediates.
3074 if (ARM_AM::isT2SOImmTwoPartVal(ImmVal
))
3075 NewUseOpc
= UseOpc
== ARM::t2ADDrr
? ARM::t2ADDri
: ARM::t2SUBri
;
3076 else if (ARM_AM::isT2SOImmTwoPartVal(-ImmVal
)) {
3078 NewUseOpc
= UseOpc
== ARM::t2ADDrr
? ARM::t2SUBri
: ARM::t2ADDri
;
3081 SOImmValV1
= (uint32_t)ARM_AM::getT2SOImmTwoPartFirst(ImmVal
);
3082 SOImmValV2
= (uint32_t)ARM_AM::getT2SOImmTwoPartSecond(ImmVal
);
3086 if (!ARM_AM::isT2SOImmTwoPartVal(ImmVal
))
3088 SOImmValV1
= (uint32_t)ARM_AM::getT2SOImmTwoPartFirst(ImmVal
);
3089 SOImmValV2
= (uint32_t)ARM_AM::getT2SOImmTwoPartSecond(ImmVal
);
3092 case ARM::t2ORRrr
: NewUseOpc
= ARM::t2ORRri
; break;
3093 case ARM::t2EORrr
: NewUseOpc
= ARM::t2EORri
; break;
3100 unsigned OpIdx
= Commute
? 2 : 1;
3101 unsigned Reg1
= UseMI
.getOperand(OpIdx
).getReg();
3102 bool isKill
= UseMI
.getOperand(OpIdx
).isKill();
3103 unsigned NewReg
= MRI
->createVirtualRegister(MRI
->getRegClass(Reg
));
3104 BuildMI(*UseMI
.getParent(), UseMI
, UseMI
.getDebugLoc(), get(NewUseOpc
),
3106 .addReg(Reg1
, getKillRegState(isKill
))
3108 .add(predOps(ARMCC::AL
))
3110 UseMI
.setDesc(get(NewUseOpc
));
3111 UseMI
.getOperand(1).setReg(NewReg
);
3112 UseMI
.getOperand(1).setIsKill();
3113 UseMI
.getOperand(2).ChangeToImmediate(SOImmValV2
);
3114 DefMI
.eraseFromParent();
3118 static unsigned getNumMicroOpsSwiftLdSt(const InstrItineraryData
*ItinData
,
3119 const MachineInstr
&MI
) {
3120 switch (MI
.getOpcode()) {
3122 const MCInstrDesc
&Desc
= MI
.getDesc();
3123 int UOps
= ItinData
->getNumMicroOps(Desc
.getSchedClass());
3124 assert(UOps
>= 0 && "bad # UOps");
3132 unsigned ShOpVal
= MI
.getOperand(3).getImm();
3133 bool isSub
= ARM_AM::getAM2Op(ShOpVal
) == ARM_AM::sub
;
3134 unsigned ShImm
= ARM_AM::getAM2Offset(ShOpVal
);
3137 ((ShImm
== 1 || ShImm
== 2 || ShImm
== 3) &&
3138 ARM_AM::getAM2ShiftOpc(ShOpVal
) == ARM_AM::lsl
)))
3145 if (!MI
.getOperand(2).getReg())
3148 unsigned ShOpVal
= MI
.getOperand(3).getImm();
3149 bool isSub
= ARM_AM::getAM2Op(ShOpVal
) == ARM_AM::sub
;
3150 unsigned ShImm
= ARM_AM::getAM2Offset(ShOpVal
);
3153 ((ShImm
== 1 || ShImm
== 2 || ShImm
== 3) &&
3154 ARM_AM::getAM2ShiftOpc(ShOpVal
) == ARM_AM::lsl
)))
3161 return (ARM_AM::getAM3Op(MI
.getOperand(3).getImm()) == ARM_AM::sub
) ? 3 : 2;
3163 case ARM::LDRSB_POST
:
3164 case ARM::LDRSH_POST
: {
3165 unsigned Rt
= MI
.getOperand(0).getReg();
3166 unsigned Rm
= MI
.getOperand(3).getReg();
3167 return (Rt
== Rm
) ? 4 : 3;
3170 case ARM::LDR_PRE_REG
:
3171 case ARM::LDRB_PRE_REG
: {
3172 unsigned Rt
= MI
.getOperand(0).getReg();
3173 unsigned Rm
= MI
.getOperand(3).getReg();
3176 unsigned ShOpVal
= MI
.getOperand(4).getImm();
3177 bool isSub
= ARM_AM::getAM2Op(ShOpVal
) == ARM_AM::sub
;
3178 unsigned ShImm
= ARM_AM::getAM2Offset(ShOpVal
);
3181 ((ShImm
== 1 || ShImm
== 2 || ShImm
== 3) &&
3182 ARM_AM::getAM2ShiftOpc(ShOpVal
) == ARM_AM::lsl
)))
3187 case ARM::STR_PRE_REG
:
3188 case ARM::STRB_PRE_REG
: {
3189 unsigned ShOpVal
= MI
.getOperand(4).getImm();
3190 bool isSub
= ARM_AM::getAM2Op(ShOpVal
) == ARM_AM::sub
;
3191 unsigned ShImm
= ARM_AM::getAM2Offset(ShOpVal
);
3194 ((ShImm
== 1 || ShImm
== 2 || ShImm
== 3) &&
3195 ARM_AM::getAM2ShiftOpc(ShOpVal
) == ARM_AM::lsl
)))
3201 case ARM::STRH_PRE
: {
3202 unsigned Rt
= MI
.getOperand(0).getReg();
3203 unsigned Rm
= MI
.getOperand(3).getReg();
3208 return (ARM_AM::getAM3Op(MI
.getOperand(4).getImm()) == ARM_AM::sub
) ? 3 : 2;
3211 case ARM::LDR_POST_REG
:
3212 case ARM::LDRB_POST_REG
:
3213 case ARM::LDRH_POST
: {
3214 unsigned Rt
= MI
.getOperand(0).getReg();
3215 unsigned Rm
= MI
.getOperand(3).getReg();
3216 return (Rt
== Rm
) ? 3 : 2;
3219 case ARM::LDR_PRE_IMM
:
3220 case ARM::LDRB_PRE_IMM
:
3221 case ARM::LDR_POST_IMM
:
3222 case ARM::LDRB_POST_IMM
:
3223 case ARM::STRB_POST_IMM
:
3224 case ARM::STRB_POST_REG
:
3225 case ARM::STRB_PRE_IMM
:
3226 case ARM::STRH_POST
:
3227 case ARM::STR_POST_IMM
:
3228 case ARM::STR_POST_REG
:
3229 case ARM::STR_PRE_IMM
:
3232 case ARM::LDRSB_PRE
:
3233 case ARM::LDRSH_PRE
: {
3234 unsigned Rm
= MI
.getOperand(3).getReg();
3237 unsigned Rt
= MI
.getOperand(0).getReg();
3240 unsigned ShOpVal
= MI
.getOperand(4).getImm();
3241 bool isSub
= ARM_AM::getAM2Op(ShOpVal
) == ARM_AM::sub
;
3242 unsigned ShImm
= ARM_AM::getAM2Offset(ShOpVal
);
3245 ((ShImm
== 1 || ShImm
== 2 || ShImm
== 3) &&
3246 ARM_AM::getAM2ShiftOpc(ShOpVal
) == ARM_AM::lsl
)))
3252 unsigned Rt
= MI
.getOperand(0).getReg();
3253 unsigned Rn
= MI
.getOperand(2).getReg();
3254 unsigned Rm
= MI
.getOperand(3).getReg();
3256 return (ARM_AM::getAM3Op(MI
.getOperand(4).getImm()) == ARM_AM::sub
) ? 4
3258 return (Rt
== Rn
) ? 3 : 2;
3262 unsigned Rm
= MI
.getOperand(3).getReg();
3264 return (ARM_AM::getAM3Op(MI
.getOperand(4).getImm()) == ARM_AM::sub
) ? 4
3269 case ARM::LDRD_POST
:
3270 case ARM::t2LDRD_POST
:
3273 case ARM::STRD_POST
:
3274 case ARM::t2STRD_POST
:
3277 case ARM::LDRD_PRE
: {
3278 unsigned Rt
= MI
.getOperand(0).getReg();
3279 unsigned Rn
= MI
.getOperand(3).getReg();
3280 unsigned Rm
= MI
.getOperand(4).getReg();
3282 return (ARM_AM::getAM3Op(MI
.getOperand(5).getImm()) == ARM_AM::sub
) ? 5
3284 return (Rt
== Rn
) ? 4 : 3;
3287 case ARM::t2LDRD_PRE
: {
3288 unsigned Rt
= MI
.getOperand(0).getReg();
3289 unsigned Rn
= MI
.getOperand(3).getReg();
3290 return (Rt
== Rn
) ? 4 : 3;
3293 case ARM::STRD_PRE
: {
3294 unsigned Rm
= MI
.getOperand(4).getReg();
3296 return (ARM_AM::getAM3Op(MI
.getOperand(5).getImm()) == ARM_AM::sub
) ? 5
3301 case ARM::t2STRD_PRE
:
3304 case ARM::t2LDR_POST
:
3305 case ARM::t2LDRB_POST
:
3306 case ARM::t2LDRB_PRE
:
3307 case ARM::t2LDRSBi12
:
3308 case ARM::t2LDRSBi8
:
3309 case ARM::t2LDRSBpci
:
3311 case ARM::t2LDRH_POST
:
3312 case ARM::t2LDRH_PRE
:
3314 case ARM::t2LDRSB_POST
:
3315 case ARM::t2LDRSB_PRE
:
3316 case ARM::t2LDRSH_POST
:
3317 case ARM::t2LDRSH_PRE
:
3318 case ARM::t2LDRSHi12
:
3319 case ARM::t2LDRSHi8
:
3320 case ARM::t2LDRSHpci
:
3324 case ARM::t2LDRDi8
: {
3325 unsigned Rt
= MI
.getOperand(0).getReg();
3326 unsigned Rn
= MI
.getOperand(2).getReg();
3327 return (Rt
== Rn
) ? 3 : 2;
3330 case ARM::t2STRB_POST
:
3331 case ARM::t2STRB_PRE
:
3334 case ARM::t2STRH_POST
:
3335 case ARM::t2STRH_PRE
:
3337 case ARM::t2STR_POST
:
3338 case ARM::t2STR_PRE
:
3344 // Return the number of 32-bit words loaded by LDM or stored by STM. If this
3345 // can't be easily determined return 0 (missing MachineMemOperand).
3347 // FIXME: The current MachineInstr design does not support relying on machine
3348 // mem operands to determine the width of a memory access. Instead, we expect
3349 // the target to provide this information based on the instruction opcode and
3350 // operands. However, using MachineMemOperand is the best solution now for
3353 // 1) getNumMicroOps tries to infer LDM memory width from the total number of MI
3354 // operands. This is much more dangerous than using the MachineMemOperand
3355 // sizes because CodeGen passes can insert/remove optional machine operands. In
3356 // fact, it's totally incorrect for preRA passes and appears to be wrong for
3357 // postRA passes as well.
3359 // 2) getNumLDMAddresses is only used by the scheduling machine model and any
3360 // machine model that calls this should handle the unknown (zero size) case.
3362 // Long term, we should require a target hook that verifies MachineMemOperand
3363 // sizes during MC lowering. That target hook should be local to MC lowering
3364 // because we can't ensure that it is aware of other MI forms. Doing this will
3365 // ensure that MachineMemOperands are correctly propagated through all passes.
3366 unsigned ARMBaseInstrInfo::getNumLDMAddresses(const MachineInstr
&MI
) const {
3368 for (MachineInstr::mmo_iterator I
= MI
.memoperands_begin(),
3369 E
= MI
.memoperands_end();
3371 Size
+= (*I
)->getSize();
3376 static unsigned getNumMicroOpsSingleIssuePlusExtras(unsigned Opc
,
3378 unsigned UOps
= 1 + NumRegs
; // 1 for address computation.
3382 case ARM::VLDMDIA_UPD
:
3383 case ARM::VLDMDDB_UPD
:
3384 case ARM::VLDMSIA_UPD
:
3385 case ARM::VLDMSDB_UPD
:
3386 case ARM::VSTMDIA_UPD
:
3387 case ARM::VSTMDDB_UPD
:
3388 case ARM::VSTMSIA_UPD
:
3389 case ARM::VSTMSDB_UPD
:
3390 case ARM::LDMIA_UPD
:
3391 case ARM::LDMDA_UPD
:
3392 case ARM::LDMDB_UPD
:
3393 case ARM::LDMIB_UPD
:
3394 case ARM::STMIA_UPD
:
3395 case ARM::STMDA_UPD
:
3396 case ARM::STMDB_UPD
:
3397 case ARM::STMIB_UPD
:
3398 case ARM::tLDMIA_UPD
:
3399 case ARM::tSTMIA_UPD
:
3400 case ARM::t2LDMIA_UPD
:
3401 case ARM::t2LDMDB_UPD
:
3402 case ARM::t2STMIA_UPD
:
3403 case ARM::t2STMDB_UPD
:
3404 ++UOps
; // One for base register writeback.
3406 case ARM::LDMIA_RET
:
3408 case ARM::t2LDMIA_RET
:
3409 UOps
+= 2; // One for base reg wb, one for write to pc.
3415 unsigned ARMBaseInstrInfo::getNumMicroOps(const InstrItineraryData
*ItinData
,
3416 const MachineInstr
&MI
) const {
3417 if (!ItinData
|| ItinData
->isEmpty())
3420 const MCInstrDesc
&Desc
= MI
.getDesc();
3421 unsigned Class
= Desc
.getSchedClass();
3422 int ItinUOps
= ItinData
->getNumMicroOps(Class
);
3423 if (ItinUOps
>= 0) {
3424 if (Subtarget
.isSwift() && (Desc
.mayLoad() || Desc
.mayStore()))
3425 return getNumMicroOpsSwiftLdSt(ItinData
, MI
);
3430 unsigned Opc
= MI
.getOpcode();
3433 llvm_unreachable("Unexpected multi-uops instruction!");
3438 // The number of uOps for load / store multiple are determined by the number
3441 // On Cortex-A8, each pair of register loads / stores can be scheduled on the
3442 // same cycle. The scheduling for the first load / store must be done
3443 // separately by assuming the address is not 64-bit aligned.
3445 // On Cortex-A9, the formula is simply (#reg / 2) + (#reg % 2). If the address
3446 // is not 64-bit aligned, then AGU would take an extra cycle. For VFP / NEON
3447 // load / store multiple, the formula is (#reg / 2) + (#reg % 2) + 1.
3449 case ARM::VLDMDIA_UPD
:
3450 case ARM::VLDMDDB_UPD
:
3452 case ARM::VLDMSIA_UPD
:
3453 case ARM::VLDMSDB_UPD
:
3455 case ARM::VSTMDIA_UPD
:
3456 case ARM::VSTMDDB_UPD
:
3458 case ARM::VSTMSIA_UPD
:
3459 case ARM::VSTMSDB_UPD
: {
3460 unsigned NumRegs
= MI
.getNumOperands() - Desc
.getNumOperands();
3461 return (NumRegs
/ 2) + (NumRegs
% 2) + 1;
3464 case ARM::LDMIA_RET
:
3469 case ARM::LDMIA_UPD
:
3470 case ARM::LDMDA_UPD
:
3471 case ARM::LDMDB_UPD
:
3472 case ARM::LDMIB_UPD
:
3477 case ARM::STMIA_UPD
:
3478 case ARM::STMDA_UPD
:
3479 case ARM::STMDB_UPD
:
3480 case ARM::STMIB_UPD
:
3482 case ARM::tLDMIA_UPD
:
3483 case ARM::tSTMIA_UPD
:
3487 case ARM::t2LDMIA_RET
:
3490 case ARM::t2LDMIA_UPD
:
3491 case ARM::t2LDMDB_UPD
:
3494 case ARM::t2STMIA_UPD
:
3495 case ARM::t2STMDB_UPD
: {
3496 unsigned NumRegs
= MI
.getNumOperands() - Desc
.getNumOperands() + 1;
3497 switch (Subtarget
.getLdStMultipleTiming()) {
3498 case ARMSubtarget::SingleIssuePlusExtras
:
3499 return getNumMicroOpsSingleIssuePlusExtras(Opc
, NumRegs
);
3500 case ARMSubtarget::SingleIssue
:
3501 // Assume the worst.
3503 case ARMSubtarget::DoubleIssue
: {
3506 // 4 registers would be issued: 2, 2.
3507 // 5 registers would be issued: 2, 2, 1.
3508 unsigned UOps
= (NumRegs
/ 2);
3513 case ARMSubtarget::DoubleIssueCheckUnalignedAccess
: {
3514 unsigned UOps
= (NumRegs
/ 2);
3515 // If there are odd number of registers or if it's not 64-bit aligned,
3516 // then it takes an extra AGU (Address Generation Unit) cycle.
3517 if ((NumRegs
% 2) || !MI
.hasOneMemOperand() ||
3518 (*MI
.memoperands_begin())->getAlignment() < 8)
3525 llvm_unreachable("Didn't find the number of microops");
3529 ARMBaseInstrInfo::getVLDMDefCycle(const InstrItineraryData
*ItinData
,
3530 const MCInstrDesc
&DefMCID
,
3532 unsigned DefIdx
, unsigned DefAlign
) const {
3533 int RegNo
= (int)(DefIdx
+1) - DefMCID
.getNumOperands() + 1;
3535 // Def is the address writeback.
3536 return ItinData
->getOperandCycle(DefClass
, DefIdx
);
3539 if (Subtarget
.isCortexA8() || Subtarget
.isCortexA7()) {
3540 // (regno / 2) + (regno % 2) + 1
3541 DefCycle
= RegNo
/ 2 + 1;
3544 } else if (Subtarget
.isLikeA9() || Subtarget
.isSwift()) {
3546 bool isSLoad
= false;
3548 switch (DefMCID
.getOpcode()) {
3551 case ARM::VLDMSIA_UPD
:
3552 case ARM::VLDMSDB_UPD
:
3557 // If there are odd number of 'S' registers or if it's not 64-bit aligned,
3558 // then it takes an extra cycle.
3559 if ((isSLoad
&& (RegNo
% 2)) || DefAlign
< 8)
3562 // Assume the worst.
3563 DefCycle
= RegNo
+ 2;
3569 bool ARMBaseInstrInfo::isLDMBaseRegInList(const MachineInstr
&MI
) const {
3570 unsigned BaseReg
= MI
.getOperand(0).getReg();
3571 for (unsigned i
= 1, sz
= MI
.getNumOperands(); i
< sz
; ++i
) {
3572 const auto &Op
= MI
.getOperand(i
);
3573 if (Op
.isReg() && Op
.getReg() == BaseReg
)
3579 ARMBaseInstrInfo::getLDMVariableDefsSize(const MachineInstr
&MI
) const {
3580 // ins GPR:$Rn, $p (2xOp), reglist:$regs, variable_ops
3581 // (outs GPR:$wb), (ins GPR:$Rn, $p (2xOp), reglist:$regs, variable_ops)
3582 return MI
.getNumOperands() + 1 - MI
.getDesc().getNumOperands();
3586 ARMBaseInstrInfo::getLDMDefCycle(const InstrItineraryData
*ItinData
,
3587 const MCInstrDesc
&DefMCID
,
3589 unsigned DefIdx
, unsigned DefAlign
) const {
3590 int RegNo
= (int)(DefIdx
+1) - DefMCID
.getNumOperands() + 1;
3592 // Def is the address writeback.
3593 return ItinData
->getOperandCycle(DefClass
, DefIdx
);
3596 if (Subtarget
.isCortexA8() || Subtarget
.isCortexA7()) {
3597 // 4 registers would be issued: 1, 2, 1.
3598 // 5 registers would be issued: 1, 2, 2.
3599 DefCycle
= RegNo
/ 2;
3602 // Result latency is issue cycle + 2: E2.
3604 } else if (Subtarget
.isLikeA9() || Subtarget
.isSwift()) {
3605 DefCycle
= (RegNo
/ 2);
3606 // If there are odd number of registers or if it's not 64-bit aligned,
3607 // then it takes an extra AGU (Address Generation Unit) cycle.
3608 if ((RegNo
% 2) || DefAlign
< 8)
3610 // Result latency is AGU cycles + 2.
3613 // Assume the worst.
3614 DefCycle
= RegNo
+ 2;
3621 ARMBaseInstrInfo::getVSTMUseCycle(const InstrItineraryData
*ItinData
,
3622 const MCInstrDesc
&UseMCID
,
3624 unsigned UseIdx
, unsigned UseAlign
) const {
3625 int RegNo
= (int)(UseIdx
+1) - UseMCID
.getNumOperands() + 1;
3627 return ItinData
->getOperandCycle(UseClass
, UseIdx
);
3630 if (Subtarget
.isCortexA8() || Subtarget
.isCortexA7()) {
3631 // (regno / 2) + (regno % 2) + 1
3632 UseCycle
= RegNo
/ 2 + 1;
3635 } else if (Subtarget
.isLikeA9() || Subtarget
.isSwift()) {
3637 bool isSStore
= false;
3639 switch (UseMCID
.getOpcode()) {
3642 case ARM::VSTMSIA_UPD
:
3643 case ARM::VSTMSDB_UPD
:
3648 // If there are odd number of 'S' registers or if it's not 64-bit aligned,
3649 // then it takes an extra cycle.
3650 if ((isSStore
&& (RegNo
% 2)) || UseAlign
< 8)
3653 // Assume the worst.
3654 UseCycle
= RegNo
+ 2;
3661 ARMBaseInstrInfo::getSTMUseCycle(const InstrItineraryData
*ItinData
,
3662 const MCInstrDesc
&UseMCID
,
3664 unsigned UseIdx
, unsigned UseAlign
) const {
3665 int RegNo
= (int)(UseIdx
+1) - UseMCID
.getNumOperands() + 1;
3667 return ItinData
->getOperandCycle(UseClass
, UseIdx
);
3670 if (Subtarget
.isCortexA8() || Subtarget
.isCortexA7()) {
3671 UseCycle
= RegNo
/ 2;
3676 } else if (Subtarget
.isLikeA9() || Subtarget
.isSwift()) {
3677 UseCycle
= (RegNo
/ 2);
3678 // If there are odd number of registers or if it's not 64-bit aligned,
3679 // then it takes an extra AGU (Address Generation Unit) cycle.
3680 if ((RegNo
% 2) || UseAlign
< 8)
3683 // Assume the worst.
3690 ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData
*ItinData
,
3691 const MCInstrDesc
&DefMCID
,
3692 unsigned DefIdx
, unsigned DefAlign
,
3693 const MCInstrDesc
&UseMCID
,
3694 unsigned UseIdx
, unsigned UseAlign
) const {
3695 unsigned DefClass
= DefMCID
.getSchedClass();
3696 unsigned UseClass
= UseMCID
.getSchedClass();
3698 if (DefIdx
< DefMCID
.getNumDefs() && UseIdx
< UseMCID
.getNumOperands())
3699 return ItinData
->getOperandLatency(DefClass
, DefIdx
, UseClass
, UseIdx
);
3701 // This may be a def / use of a variable_ops instruction, the operand
3702 // latency might be determinable dynamically. Let the target try to
3705 bool LdmBypass
= false;
3706 switch (DefMCID
.getOpcode()) {
3708 DefCycle
= ItinData
->getOperandCycle(DefClass
, DefIdx
);
3712 case ARM::VLDMDIA_UPD
:
3713 case ARM::VLDMDDB_UPD
:
3715 case ARM::VLDMSIA_UPD
:
3716 case ARM::VLDMSDB_UPD
:
3717 DefCycle
= getVLDMDefCycle(ItinData
, DefMCID
, DefClass
, DefIdx
, DefAlign
);
3720 case ARM::LDMIA_RET
:
3725 case ARM::LDMIA_UPD
:
3726 case ARM::LDMDA_UPD
:
3727 case ARM::LDMDB_UPD
:
3728 case ARM::LDMIB_UPD
:
3730 case ARM::tLDMIA_UPD
:
3732 case ARM::t2LDMIA_RET
:
3735 case ARM::t2LDMIA_UPD
:
3736 case ARM::t2LDMDB_UPD
:
3738 DefCycle
= getLDMDefCycle(ItinData
, DefMCID
, DefClass
, DefIdx
, DefAlign
);
3743 // We can't seem to determine the result latency of the def, assume it's 2.
3747 switch (UseMCID
.getOpcode()) {
3749 UseCycle
= ItinData
->getOperandCycle(UseClass
, UseIdx
);
3753 case ARM::VSTMDIA_UPD
:
3754 case ARM::VSTMDDB_UPD
:
3756 case ARM::VSTMSIA_UPD
:
3757 case ARM::VSTMSDB_UPD
:
3758 UseCycle
= getVSTMUseCycle(ItinData
, UseMCID
, UseClass
, UseIdx
, UseAlign
);
3765 case ARM::STMIA_UPD
:
3766 case ARM::STMDA_UPD
:
3767 case ARM::STMDB_UPD
:
3768 case ARM::STMIB_UPD
:
3769 case ARM::tSTMIA_UPD
:
3774 case ARM::t2STMIA_UPD
:
3775 case ARM::t2STMDB_UPD
:
3776 UseCycle
= getSTMUseCycle(ItinData
, UseMCID
, UseClass
, UseIdx
, UseAlign
);
3781 // Assume it's read in the first stage.
3784 UseCycle
= DefCycle
- UseCycle
+ 1;
3787 // It's a variable_ops instruction so we can't use DefIdx here. Just use
3788 // first def operand.
3789 if (ItinData
->hasPipelineForwarding(DefClass
, DefMCID
.getNumOperands()-1,
3792 } else if (ItinData
->hasPipelineForwarding(DefClass
, DefIdx
,
3793 UseClass
, UseIdx
)) {
3801 static const MachineInstr
*getBundledDefMI(const TargetRegisterInfo
*TRI
,
3802 const MachineInstr
*MI
, unsigned Reg
,
3803 unsigned &DefIdx
, unsigned &Dist
) {
3806 MachineBasicBlock::const_iterator I
= MI
; ++I
;
3807 MachineBasicBlock::const_instr_iterator II
= std::prev(I
.getInstrIterator());
3808 assert(II
->isInsideBundle() && "Empty bundle?");
3811 while (II
->isInsideBundle()) {
3812 Idx
= II
->findRegisterDefOperandIdx(Reg
, false, true, TRI
);
3819 assert(Idx
!= -1 && "Cannot find bundled definition!");
3824 static const MachineInstr
*getBundledUseMI(const TargetRegisterInfo
*TRI
,
3825 const MachineInstr
&MI
, unsigned Reg
,
3826 unsigned &UseIdx
, unsigned &Dist
) {
3829 MachineBasicBlock::const_instr_iterator II
= ++MI
.getIterator();
3830 assert(II
->isInsideBundle() && "Empty bundle?");
3831 MachineBasicBlock::const_instr_iterator E
= MI
.getParent()->instr_end();
3833 // FIXME: This doesn't properly handle multiple uses.
3835 while (II
!= E
&& II
->isInsideBundle()) {
3836 Idx
= II
->findRegisterUseOperandIdx(Reg
, false, TRI
);
3839 if (II
->getOpcode() != ARM::t2IT
)
3853 /// Return the number of cycles to add to (or subtract from) the static
3854 /// itinerary based on the def opcode and alignment. The caller will ensure that
3855 /// adjusted latency is at least one cycle.
3856 static int adjustDefLatency(const ARMSubtarget
&Subtarget
,
3857 const MachineInstr
&DefMI
,
3858 const MCInstrDesc
&DefMCID
, unsigned DefAlign
) {
3860 if (Subtarget
.isCortexA8() || Subtarget
.isLikeA9() || Subtarget
.isCortexA7()) {
3861 // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2]
3862 // variants are one cycle cheaper.
3863 switch (DefMCID
.getOpcode()) {
3867 unsigned ShOpVal
= DefMI
.getOperand(3).getImm();
3868 unsigned ShImm
= ARM_AM::getAM2Offset(ShOpVal
);
3870 (ShImm
== 2 && ARM_AM::getAM2ShiftOpc(ShOpVal
) == ARM_AM::lsl
))
3877 case ARM::t2LDRSHs
: {
3878 // Thumb2 mode: lsl only.
3879 unsigned ShAmt
= DefMI
.getOperand(3).getImm();
3880 if (ShAmt
== 0 || ShAmt
== 2)
3885 } else if (Subtarget
.isSwift()) {
3886 // FIXME: Properly handle all of the latency adjustments for address
3888 switch (DefMCID
.getOpcode()) {
3892 unsigned ShOpVal
= DefMI
.getOperand(3).getImm();
3893 bool isSub
= ARM_AM::getAM2Op(ShOpVal
) == ARM_AM::sub
;
3894 unsigned ShImm
= ARM_AM::getAM2Offset(ShOpVal
);
3897 ((ShImm
== 1 || ShImm
== 2 || ShImm
== 3) &&
3898 ARM_AM::getAM2ShiftOpc(ShOpVal
) == ARM_AM::lsl
)))
3901 ShImm
== 1 && ARM_AM::getAM2ShiftOpc(ShOpVal
) == ARM_AM::lsr
)
3908 case ARM::t2LDRSHs
: {
3909 // Thumb2 mode: lsl only.
3910 unsigned ShAmt
= DefMI
.getOperand(3).getImm();
3911 if (ShAmt
== 0 || ShAmt
== 1 || ShAmt
== 2 || ShAmt
== 3)
3918 if (DefAlign
< 8 && Subtarget
.checkVLDnAccessAlignment()) {
3919 switch (DefMCID
.getOpcode()) {
3925 case ARM::VLD1q8wb_fixed
:
3926 case ARM::VLD1q16wb_fixed
:
3927 case ARM::VLD1q32wb_fixed
:
3928 case ARM::VLD1q64wb_fixed
:
3929 case ARM::VLD1q8wb_register
:
3930 case ARM::VLD1q16wb_register
:
3931 case ARM::VLD1q32wb_register
:
3932 case ARM::VLD1q64wb_register
:
3939 case ARM::VLD2d8wb_fixed
:
3940 case ARM::VLD2d16wb_fixed
:
3941 case ARM::VLD2d32wb_fixed
:
3942 case ARM::VLD2q8wb_fixed
:
3943 case ARM::VLD2q16wb_fixed
:
3944 case ARM::VLD2q32wb_fixed
:
3945 case ARM::VLD2d8wb_register
:
3946 case ARM::VLD2d16wb_register
:
3947 case ARM::VLD2d32wb_register
:
3948 case ARM::VLD2q8wb_register
:
3949 case ARM::VLD2q16wb_register
:
3950 case ARM::VLD2q32wb_register
:
3955 case ARM::VLD3d8_UPD
:
3956 case ARM::VLD3d16_UPD
:
3957 case ARM::VLD3d32_UPD
:
3958 case ARM::VLD1d64Twb_fixed
:
3959 case ARM::VLD1d64Twb_register
:
3960 case ARM::VLD3q8_UPD
:
3961 case ARM::VLD3q16_UPD
:
3962 case ARM::VLD3q32_UPD
:
3967 case ARM::VLD4d8_UPD
:
3968 case ARM::VLD4d16_UPD
:
3969 case ARM::VLD4d32_UPD
:
3970 case ARM::VLD1d64Qwb_fixed
:
3971 case ARM::VLD1d64Qwb_register
:
3972 case ARM::VLD4q8_UPD
:
3973 case ARM::VLD4q16_UPD
:
3974 case ARM::VLD4q32_UPD
:
3975 case ARM::VLD1DUPq8
:
3976 case ARM::VLD1DUPq16
:
3977 case ARM::VLD1DUPq32
:
3978 case ARM::VLD1DUPq8wb_fixed
:
3979 case ARM::VLD1DUPq16wb_fixed
:
3980 case ARM::VLD1DUPq32wb_fixed
:
3981 case ARM::VLD1DUPq8wb_register
:
3982 case ARM::VLD1DUPq16wb_register
:
3983 case ARM::VLD1DUPq32wb_register
:
3984 case ARM::VLD2DUPd8
:
3985 case ARM::VLD2DUPd16
:
3986 case ARM::VLD2DUPd32
:
3987 case ARM::VLD2DUPd8wb_fixed
:
3988 case ARM::VLD2DUPd16wb_fixed
:
3989 case ARM::VLD2DUPd32wb_fixed
:
3990 case ARM::VLD2DUPd8wb_register
:
3991 case ARM::VLD2DUPd16wb_register
:
3992 case ARM::VLD2DUPd32wb_register
:
3993 case ARM::VLD4DUPd8
:
3994 case ARM::VLD4DUPd16
:
3995 case ARM::VLD4DUPd32
:
3996 case ARM::VLD4DUPd8_UPD
:
3997 case ARM::VLD4DUPd16_UPD
:
3998 case ARM::VLD4DUPd32_UPD
:
4000 case ARM::VLD1LNd16
:
4001 case ARM::VLD1LNd32
:
4002 case ARM::VLD1LNd8_UPD
:
4003 case ARM::VLD1LNd16_UPD
:
4004 case ARM::VLD1LNd32_UPD
:
4006 case ARM::VLD2LNd16
:
4007 case ARM::VLD2LNd32
:
4008 case ARM::VLD2LNq16
:
4009 case ARM::VLD2LNq32
:
4010 case ARM::VLD2LNd8_UPD
:
4011 case ARM::VLD2LNd16_UPD
:
4012 case ARM::VLD2LNd32_UPD
:
4013 case ARM::VLD2LNq16_UPD
:
4014 case ARM::VLD2LNq32_UPD
:
4016 case ARM::VLD4LNd16
:
4017 case ARM::VLD4LNd32
:
4018 case ARM::VLD4LNq16
:
4019 case ARM::VLD4LNq32
:
4020 case ARM::VLD4LNd8_UPD
:
4021 case ARM::VLD4LNd16_UPD
:
4022 case ARM::VLD4LNd32_UPD
:
4023 case ARM::VLD4LNq16_UPD
:
4024 case ARM::VLD4LNq32_UPD
:
4025 // If the address is not 64-bit aligned, the latencies of these
4026 // instructions increases by one.
4034 int ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData
*ItinData
,
4035 const MachineInstr
&DefMI
,
4037 const MachineInstr
&UseMI
,
4038 unsigned UseIdx
) const {
4039 // No operand latency. The caller may fall back to getInstrLatency.
4040 if (!ItinData
|| ItinData
->isEmpty())
4043 const MachineOperand
&DefMO
= DefMI
.getOperand(DefIdx
);
4044 unsigned Reg
= DefMO
.getReg();
4046 const MachineInstr
*ResolvedDefMI
= &DefMI
;
4047 unsigned DefAdj
= 0;
4048 if (DefMI
.isBundle())
4050 getBundledDefMI(&getRegisterInfo(), &DefMI
, Reg
, DefIdx
, DefAdj
);
4051 if (ResolvedDefMI
->isCopyLike() || ResolvedDefMI
->isInsertSubreg() ||
4052 ResolvedDefMI
->isRegSequence() || ResolvedDefMI
->isImplicitDef()) {
4056 const MachineInstr
*ResolvedUseMI
= &UseMI
;
4057 unsigned UseAdj
= 0;
4058 if (UseMI
.isBundle()) {
4060 getBundledUseMI(&getRegisterInfo(), UseMI
, Reg
, UseIdx
, UseAdj
);
4065 return getOperandLatencyImpl(
4066 ItinData
, *ResolvedDefMI
, DefIdx
, ResolvedDefMI
->getDesc(), DefAdj
, DefMO
,
4067 Reg
, *ResolvedUseMI
, UseIdx
, ResolvedUseMI
->getDesc(), UseAdj
);
4070 int ARMBaseInstrInfo::getOperandLatencyImpl(
4071 const InstrItineraryData
*ItinData
, const MachineInstr
&DefMI
,
4072 unsigned DefIdx
, const MCInstrDesc
&DefMCID
, unsigned DefAdj
,
4073 const MachineOperand
&DefMO
, unsigned Reg
, const MachineInstr
&UseMI
,
4074 unsigned UseIdx
, const MCInstrDesc
&UseMCID
, unsigned UseAdj
) const {
4075 if (Reg
== ARM::CPSR
) {
4076 if (DefMI
.getOpcode() == ARM::FMSTAT
) {
4077 // fpscr -> cpsr stalls over 20 cycles on A8 (and earlier?)
4078 return Subtarget
.isLikeA9() ? 1 : 20;
4081 // CPSR set and branch can be paired in the same cycle.
4082 if (UseMI
.isBranch())
4085 // Otherwise it takes the instruction latency (generally one).
4086 unsigned Latency
= getInstrLatency(ItinData
, DefMI
);
4088 // For Thumb2 and -Os, prefer scheduling CPSR setting instruction close to
4089 // its uses. Instructions which are otherwise scheduled between them may
4090 // incur a code size penalty (not able to use the CPSR setting 16-bit
4092 if (Latency
> 0 && Subtarget
.isThumb2()) {
4093 const MachineFunction
*MF
= DefMI
.getParent()->getParent();
4094 // FIXME: Use Function::optForSize().
4095 if (MF
->getFunction().hasFnAttribute(Attribute::OptimizeForSize
))
4101 if (DefMO
.isImplicit() || UseMI
.getOperand(UseIdx
).isImplicit())
4104 unsigned DefAlign
= DefMI
.hasOneMemOperand()
4105 ? (*DefMI
.memoperands_begin())->getAlignment()
4107 unsigned UseAlign
= UseMI
.hasOneMemOperand()
4108 ? (*UseMI
.memoperands_begin())->getAlignment()
4111 // Get the itinerary's latency if possible, and handle variable_ops.
4112 int Latency
= getOperandLatency(ItinData
, DefMCID
, DefIdx
, DefAlign
, UseMCID
,
4114 // Unable to find operand latency. The caller may resort to getInstrLatency.
4118 // Adjust for IT block position.
4119 int Adj
= DefAdj
+ UseAdj
;
4121 // Adjust for dynamic def-side opcode variants not captured by the itinerary.
4122 Adj
+= adjustDefLatency(Subtarget
, DefMI
, DefMCID
, DefAlign
);
4123 if (Adj
>= 0 || (int)Latency
> -Adj
) {
4124 return Latency
+ Adj
;
4126 // Return the itinerary latency, which may be zero but not less than zero.
4131 ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData
*ItinData
,
4132 SDNode
*DefNode
, unsigned DefIdx
,
4133 SDNode
*UseNode
, unsigned UseIdx
) const {
4134 if (!DefNode
->isMachineOpcode())
4137 const MCInstrDesc
&DefMCID
= get(DefNode
->getMachineOpcode());
4139 if (isZeroCost(DefMCID
.Opcode
))
4142 if (!ItinData
|| ItinData
->isEmpty())
4143 return DefMCID
.mayLoad() ? 3 : 1;
4145 if (!UseNode
->isMachineOpcode()) {
4146 int Latency
= ItinData
->getOperandCycle(DefMCID
.getSchedClass(), DefIdx
);
4147 int Adj
= Subtarget
.getPreISelOperandLatencyAdjustment();
4148 int Threshold
= 1 + Adj
;
4149 return Latency
<= Threshold
? 1 : Latency
- Adj
;
4152 const MCInstrDesc
&UseMCID
= get(UseNode
->getMachineOpcode());
4153 const MachineSDNode
*DefMN
= dyn_cast
<MachineSDNode
>(DefNode
);
4154 unsigned DefAlign
= !DefMN
->memoperands_empty()
4155 ? (*DefMN
->memoperands_begin())->getAlignment() : 0;
4156 const MachineSDNode
*UseMN
= dyn_cast
<MachineSDNode
>(UseNode
);
4157 unsigned UseAlign
= !UseMN
->memoperands_empty()
4158 ? (*UseMN
->memoperands_begin())->getAlignment() : 0;
4159 int Latency
= getOperandLatency(ItinData
, DefMCID
, DefIdx
, DefAlign
,
4160 UseMCID
, UseIdx
, UseAlign
);
4163 (Subtarget
.isCortexA8() || Subtarget
.isLikeA9() ||
4164 Subtarget
.isCortexA7())) {
4165 // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2]
4166 // variants are one cycle cheaper.
4167 switch (DefMCID
.getOpcode()) {
4172 cast
<ConstantSDNode
>(DefNode
->getOperand(2))->getZExtValue();
4173 unsigned ShImm
= ARM_AM::getAM2Offset(ShOpVal
);
4175 (ShImm
== 2 && ARM_AM::getAM2ShiftOpc(ShOpVal
) == ARM_AM::lsl
))
4182 case ARM::t2LDRSHs
: {
4183 // Thumb2 mode: lsl only.
4185 cast
<ConstantSDNode
>(DefNode
->getOperand(2))->getZExtValue();
4186 if (ShAmt
== 0 || ShAmt
== 2)
4191 } else if (DefIdx
== 0 && Latency
> 2 && Subtarget
.isSwift()) {
4192 // FIXME: Properly handle all of the latency adjustments for address
4194 switch (DefMCID
.getOpcode()) {
4199 cast
<ConstantSDNode
>(DefNode
->getOperand(2))->getZExtValue();
4200 unsigned ShImm
= ARM_AM::getAM2Offset(ShOpVal
);
4202 ((ShImm
== 1 || ShImm
== 2 || ShImm
== 3) &&
4203 ARM_AM::getAM2ShiftOpc(ShOpVal
) == ARM_AM::lsl
))
4205 else if (ShImm
== 1 && ARM_AM::getAM2ShiftOpc(ShOpVal
) == ARM_AM::lsr
)
4213 // Thumb2 mode: lsl 0-3 only.
4219 if (DefAlign
< 8 && Subtarget
.checkVLDnAccessAlignment())
4220 switch (DefMCID
.getOpcode()) {
4226 case ARM::VLD1q8wb_register
:
4227 case ARM::VLD1q16wb_register
:
4228 case ARM::VLD1q32wb_register
:
4229 case ARM::VLD1q64wb_register
:
4230 case ARM::VLD1q8wb_fixed
:
4231 case ARM::VLD1q16wb_fixed
:
4232 case ARM::VLD1q32wb_fixed
:
4233 case ARM::VLD1q64wb_fixed
:
4237 case ARM::VLD2q8Pseudo
:
4238 case ARM::VLD2q16Pseudo
:
4239 case ARM::VLD2q32Pseudo
:
4240 case ARM::VLD2d8wb_fixed
:
4241 case ARM::VLD2d16wb_fixed
:
4242 case ARM::VLD2d32wb_fixed
:
4243 case ARM::VLD2q8PseudoWB_fixed
:
4244 case ARM::VLD2q16PseudoWB_fixed
:
4245 case ARM::VLD2q32PseudoWB_fixed
:
4246 case ARM::VLD2d8wb_register
:
4247 case ARM::VLD2d16wb_register
:
4248 case ARM::VLD2d32wb_register
:
4249 case ARM::VLD2q8PseudoWB_register
:
4250 case ARM::VLD2q16PseudoWB_register
:
4251 case ARM::VLD2q32PseudoWB_register
:
4252 case ARM::VLD3d8Pseudo
:
4253 case ARM::VLD3d16Pseudo
:
4254 case ARM::VLD3d32Pseudo
:
4255 case ARM::VLD1d8TPseudo
:
4256 case ARM::VLD1d16TPseudo
:
4257 case ARM::VLD1d32TPseudo
:
4258 case ARM::VLD1d64TPseudo
:
4259 case ARM::VLD1d64TPseudoWB_fixed
:
4260 case ARM::VLD1d64TPseudoWB_register
:
4261 case ARM::VLD3d8Pseudo_UPD
:
4262 case ARM::VLD3d16Pseudo_UPD
:
4263 case ARM::VLD3d32Pseudo_UPD
:
4264 case ARM::VLD3q8Pseudo_UPD
:
4265 case ARM::VLD3q16Pseudo_UPD
:
4266 case ARM::VLD3q32Pseudo_UPD
:
4267 case ARM::VLD3q8oddPseudo
:
4268 case ARM::VLD3q16oddPseudo
:
4269 case ARM::VLD3q32oddPseudo
:
4270 case ARM::VLD3q8oddPseudo_UPD
:
4271 case ARM::VLD3q16oddPseudo_UPD
:
4272 case ARM::VLD3q32oddPseudo_UPD
:
4273 case ARM::VLD4d8Pseudo
:
4274 case ARM::VLD4d16Pseudo
:
4275 case ARM::VLD4d32Pseudo
:
4276 case ARM::VLD1d8QPseudo
:
4277 case ARM::VLD1d16QPseudo
:
4278 case ARM::VLD1d32QPseudo
:
4279 case ARM::VLD1d64QPseudo
:
4280 case ARM::VLD1d64QPseudoWB_fixed
:
4281 case ARM::VLD1d64QPseudoWB_register
:
4282 case ARM::VLD1q8HighQPseudo
:
4283 case ARM::VLD1q8LowQPseudo_UPD
:
4284 case ARM::VLD1q8HighTPseudo
:
4285 case ARM::VLD1q8LowTPseudo_UPD
:
4286 case ARM::VLD1q16HighQPseudo
:
4287 case ARM::VLD1q16LowQPseudo_UPD
:
4288 case ARM::VLD1q16HighTPseudo
:
4289 case ARM::VLD1q16LowTPseudo_UPD
:
4290 case ARM::VLD1q32HighQPseudo
:
4291 case ARM::VLD1q32LowQPseudo_UPD
:
4292 case ARM::VLD1q32HighTPseudo
:
4293 case ARM::VLD1q32LowTPseudo_UPD
:
4294 case ARM::VLD1q64HighQPseudo
:
4295 case ARM::VLD1q64LowQPseudo_UPD
:
4296 case ARM::VLD1q64HighTPseudo
:
4297 case ARM::VLD1q64LowTPseudo_UPD
:
4298 case ARM::VLD4d8Pseudo_UPD
:
4299 case ARM::VLD4d16Pseudo_UPD
:
4300 case ARM::VLD4d32Pseudo_UPD
:
4301 case ARM::VLD4q8Pseudo_UPD
:
4302 case ARM::VLD4q16Pseudo_UPD
:
4303 case ARM::VLD4q32Pseudo_UPD
:
4304 case ARM::VLD4q8oddPseudo
:
4305 case ARM::VLD4q16oddPseudo
:
4306 case ARM::VLD4q32oddPseudo
:
4307 case ARM::VLD4q8oddPseudo_UPD
:
4308 case ARM::VLD4q16oddPseudo_UPD
:
4309 case ARM::VLD4q32oddPseudo_UPD
:
4310 case ARM::VLD1DUPq8
:
4311 case ARM::VLD1DUPq16
:
4312 case ARM::VLD1DUPq32
:
4313 case ARM::VLD1DUPq8wb_fixed
:
4314 case ARM::VLD1DUPq16wb_fixed
:
4315 case ARM::VLD1DUPq32wb_fixed
:
4316 case ARM::VLD1DUPq8wb_register
:
4317 case ARM::VLD1DUPq16wb_register
:
4318 case ARM::VLD1DUPq32wb_register
:
4319 case ARM::VLD2DUPd8
:
4320 case ARM::VLD2DUPd16
:
4321 case ARM::VLD2DUPd32
:
4322 case ARM::VLD2DUPd8wb_fixed
:
4323 case ARM::VLD2DUPd16wb_fixed
:
4324 case ARM::VLD2DUPd32wb_fixed
:
4325 case ARM::VLD2DUPd8wb_register
:
4326 case ARM::VLD2DUPd16wb_register
:
4327 case ARM::VLD2DUPd32wb_register
:
4328 case ARM::VLD2DUPq8EvenPseudo
:
4329 case ARM::VLD2DUPq8OddPseudo
:
4330 case ARM::VLD2DUPq16EvenPseudo
:
4331 case ARM::VLD2DUPq16OddPseudo
:
4332 case ARM::VLD2DUPq32EvenPseudo
:
4333 case ARM::VLD2DUPq32OddPseudo
:
4334 case ARM::VLD3DUPq8EvenPseudo
:
4335 case ARM::VLD3DUPq8OddPseudo
:
4336 case ARM::VLD3DUPq16EvenPseudo
:
4337 case ARM::VLD3DUPq16OddPseudo
:
4338 case ARM::VLD3DUPq32EvenPseudo
:
4339 case ARM::VLD3DUPq32OddPseudo
:
4340 case ARM::VLD4DUPd8Pseudo
:
4341 case ARM::VLD4DUPd16Pseudo
:
4342 case ARM::VLD4DUPd32Pseudo
:
4343 case ARM::VLD4DUPd8Pseudo_UPD
:
4344 case ARM::VLD4DUPd16Pseudo_UPD
:
4345 case ARM::VLD4DUPd32Pseudo_UPD
:
4346 case ARM::VLD4DUPq8EvenPseudo
:
4347 case ARM::VLD4DUPq8OddPseudo
:
4348 case ARM::VLD4DUPq16EvenPseudo
:
4349 case ARM::VLD4DUPq16OddPseudo
:
4350 case ARM::VLD4DUPq32EvenPseudo
:
4351 case ARM::VLD4DUPq32OddPseudo
:
4352 case ARM::VLD1LNq8Pseudo
:
4353 case ARM::VLD1LNq16Pseudo
:
4354 case ARM::VLD1LNq32Pseudo
:
4355 case ARM::VLD1LNq8Pseudo_UPD
:
4356 case ARM::VLD1LNq16Pseudo_UPD
:
4357 case ARM::VLD1LNq32Pseudo_UPD
:
4358 case ARM::VLD2LNd8Pseudo
:
4359 case ARM::VLD2LNd16Pseudo
:
4360 case ARM::VLD2LNd32Pseudo
:
4361 case ARM::VLD2LNq16Pseudo
:
4362 case ARM::VLD2LNq32Pseudo
:
4363 case ARM::VLD2LNd8Pseudo_UPD
:
4364 case ARM::VLD2LNd16Pseudo_UPD
:
4365 case ARM::VLD2LNd32Pseudo_UPD
:
4366 case ARM::VLD2LNq16Pseudo_UPD
:
4367 case ARM::VLD2LNq32Pseudo_UPD
:
4368 case ARM::VLD4LNd8Pseudo
:
4369 case ARM::VLD4LNd16Pseudo
:
4370 case ARM::VLD4LNd32Pseudo
:
4371 case ARM::VLD4LNq16Pseudo
:
4372 case ARM::VLD4LNq32Pseudo
:
4373 case ARM::VLD4LNd8Pseudo_UPD
:
4374 case ARM::VLD4LNd16Pseudo_UPD
:
4375 case ARM::VLD4LNd32Pseudo_UPD
:
4376 case ARM::VLD4LNq16Pseudo_UPD
:
4377 case ARM::VLD4LNq32Pseudo_UPD
:
4378 // If the address is not 64-bit aligned, the latencies of these
4379 // instructions increases by one.
4387 unsigned ARMBaseInstrInfo::getPredicationCost(const MachineInstr
&MI
) const {
4388 if (MI
.isCopyLike() || MI
.isInsertSubreg() || MI
.isRegSequence() ||
4395 const MCInstrDesc
&MCID
= MI
.getDesc();
4397 if (MCID
.isCall() || (MCID
.hasImplicitDefOfPhysReg(ARM::CPSR
) &&
4398 !Subtarget
.cheapPredicableCPSRDef())) {
4399 // When predicated, CPSR is an additional source operand for CPSR updating
4400 // instructions, this apparently increases their latencies.
4406 unsigned ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData
*ItinData
,
4407 const MachineInstr
&MI
,
4408 unsigned *PredCost
) const {
4409 if (MI
.isCopyLike() || MI
.isInsertSubreg() || MI
.isRegSequence() ||
4413 // An instruction scheduler typically runs on unbundled instructions, however
4414 // other passes may query the latency of a bundled instruction.
4415 if (MI
.isBundle()) {
4416 unsigned Latency
= 0;
4417 MachineBasicBlock::const_instr_iterator I
= MI
.getIterator();
4418 MachineBasicBlock::const_instr_iterator E
= MI
.getParent()->instr_end();
4419 while (++I
!= E
&& I
->isInsideBundle()) {
4420 if (I
->getOpcode() != ARM::t2IT
)
4421 Latency
+= getInstrLatency(ItinData
, *I
, PredCost
);
4426 const MCInstrDesc
&MCID
= MI
.getDesc();
4427 if (PredCost
&& (MCID
.isCall() || (MCID
.hasImplicitDefOfPhysReg(ARM::CPSR
) &&
4428 !Subtarget
.cheapPredicableCPSRDef()))) {
4429 // When predicated, CPSR is an additional source operand for CPSR updating
4430 // instructions, this apparently increases their latencies.
4433 // Be sure to call getStageLatency for an empty itinerary in case it has a
4434 // valid MinLatency property.
4436 return MI
.mayLoad() ? 3 : 1;
4438 unsigned Class
= MCID
.getSchedClass();
4440 // For instructions with variable uops, use uops as latency.
4441 if (!ItinData
->isEmpty() && ItinData
->getNumMicroOps(Class
) < 0)
4442 return getNumMicroOps(ItinData
, MI
);
4444 // For the common case, fall back on the itinerary's latency.
4445 unsigned Latency
= ItinData
->getStageLatency(Class
);
4447 // Adjust for dynamic def-side opcode variants not captured by the itinerary.
4449 MI
.hasOneMemOperand() ? (*MI
.memoperands_begin())->getAlignment() : 0;
4450 int Adj
= adjustDefLatency(Subtarget
, MI
, MCID
, DefAlign
);
4451 if (Adj
>= 0 || (int)Latency
> -Adj
) {
4452 return Latency
+ Adj
;
4457 int ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData
*ItinData
,
4458 SDNode
*Node
) const {
4459 if (!Node
->isMachineOpcode())
4462 if (!ItinData
|| ItinData
->isEmpty())
4465 unsigned Opcode
= Node
->getMachineOpcode();
4468 return ItinData
->getStageLatency(get(Opcode
).getSchedClass());
4475 bool ARMBaseInstrInfo::hasHighOperandLatency(const TargetSchedModel
&SchedModel
,
4476 const MachineRegisterInfo
*MRI
,
4477 const MachineInstr
&DefMI
,
4479 const MachineInstr
&UseMI
,
4480 unsigned UseIdx
) const {
4481 unsigned DDomain
= DefMI
.getDesc().TSFlags
& ARMII::DomainMask
;
4482 unsigned UDomain
= UseMI
.getDesc().TSFlags
& ARMII::DomainMask
;
4483 if (Subtarget
.nonpipelinedVFP() &&
4484 (DDomain
== ARMII::DomainVFP
|| UDomain
== ARMII::DomainVFP
))
4487 // Hoist VFP / NEON instructions with 4 or higher latency.
4489 SchedModel
.computeOperandLatency(&DefMI
, DefIdx
, &UseMI
, UseIdx
);
4492 return DDomain
== ARMII::DomainVFP
|| DDomain
== ARMII::DomainNEON
||
4493 UDomain
== ARMII::DomainVFP
|| UDomain
== ARMII::DomainNEON
;
4496 bool ARMBaseInstrInfo::hasLowDefLatency(const TargetSchedModel
&SchedModel
,
4497 const MachineInstr
&DefMI
,
4498 unsigned DefIdx
) const {
4499 const InstrItineraryData
*ItinData
= SchedModel
.getInstrItineraries();
4500 if (!ItinData
|| ItinData
->isEmpty())
4503 unsigned DDomain
= DefMI
.getDesc().TSFlags
& ARMII::DomainMask
;
4504 if (DDomain
== ARMII::DomainGeneral
) {
4505 unsigned DefClass
= DefMI
.getDesc().getSchedClass();
4506 int DefCycle
= ItinData
->getOperandCycle(DefClass
, DefIdx
);
4507 return (DefCycle
!= -1 && DefCycle
<= 2);
4512 bool ARMBaseInstrInfo::verifyInstruction(const MachineInstr
&MI
,
4513 StringRef
&ErrInfo
) const {
4514 if (convertAddSubFlagsOpcode(MI
.getOpcode())) {
4515 ErrInfo
= "Pseudo flag setting opcodes only exist in Selection DAG";
4521 // LoadStackGuard has so far only been implemented for MachO. Different code
4522 // sequence is needed for other targets.
4523 void ARMBaseInstrInfo::expandLoadStackGuardBase(MachineBasicBlock::iterator MI
,
4524 unsigned LoadImmOpc
,
4525 unsigned LoadOpc
) const {
4526 assert(!Subtarget
.isROPI() && !Subtarget
.isRWPI() &&
4527 "ROPI/RWPI not currently supported with stack guard");
4529 MachineBasicBlock
&MBB
= *MI
->getParent();
4530 DebugLoc DL
= MI
->getDebugLoc();
4531 unsigned Reg
= MI
->getOperand(0).getReg();
4532 const GlobalValue
*GV
=
4533 cast
<GlobalValue
>((*MI
->memoperands_begin())->getValue());
4534 MachineInstrBuilder MIB
;
4536 BuildMI(MBB
, MI
, DL
, get(LoadImmOpc
), Reg
)
4537 .addGlobalAddress(GV
, 0, ARMII::MO_NONLAZY
);
4539 if (Subtarget
.isGVIndirectSymbol(GV
)) {
4540 MIB
= BuildMI(MBB
, MI
, DL
, get(LoadOpc
), Reg
);
4541 MIB
.addReg(Reg
, RegState::Kill
).addImm(0);
4542 auto Flags
= MachineMemOperand::MOLoad
|
4543 MachineMemOperand::MODereferenceable
|
4544 MachineMemOperand::MOInvariant
;
4545 MachineMemOperand
*MMO
= MBB
.getParent()->getMachineMemOperand(
4546 MachinePointerInfo::getGOT(*MBB
.getParent()), Flags
, 4, 4);
4547 MIB
.addMemOperand(MMO
).add(predOps(ARMCC::AL
));
4550 MIB
= BuildMI(MBB
, MI
, DL
, get(LoadOpc
), Reg
);
4551 MIB
.addReg(Reg
, RegState::Kill
)
4554 .add(predOps(ARMCC::AL
));
4558 ARMBaseInstrInfo::isFpMLxInstruction(unsigned Opcode
, unsigned &MulOpc
,
4559 unsigned &AddSubOpc
,
4560 bool &NegAcc
, bool &HasLane
) const {
4561 DenseMap
<unsigned, unsigned>::const_iterator I
= MLxEntryMap
.find(Opcode
);
4562 if (I
== MLxEntryMap
.end())
4565 const ARM_MLxEntry
&Entry
= ARM_MLxTable
[I
->second
];
4566 MulOpc
= Entry
.MulOpc
;
4567 AddSubOpc
= Entry
.AddSubOpc
;
4568 NegAcc
= Entry
.NegAcc
;
4569 HasLane
= Entry
.HasLane
;
4573 //===----------------------------------------------------------------------===//
4574 // Execution domains.
4575 //===----------------------------------------------------------------------===//
4577 // Some instructions go down the NEON pipeline, some go down the VFP pipeline,
4578 // and some can go down both. The vmov instructions go down the VFP pipeline,
4579 // but they can be changed to vorr equivalents that are executed by the NEON
4582 // We use the following execution domain numbering:
4591 // Also see ARMInstrFormats.td and Domain* enums in ARMBaseInfo.h
4593 std::pair
<uint16_t, uint16_t>
4594 ARMBaseInstrInfo::getExecutionDomain(const MachineInstr
&MI
) const {
4595 // If we don't have access to NEON instructions then we won't be able
4596 // to swizzle anything to the NEON domain. Check to make sure.
4597 if (Subtarget
.hasNEON()) {
4598 // VMOVD, VMOVRS and VMOVSR are VFP instructions, but can be changed to NEON
4599 // if they are not predicated.
4600 if (MI
.getOpcode() == ARM::VMOVD
&& !isPredicated(MI
))
4601 return std::make_pair(ExeVFP
, (1 << ExeVFP
) | (1 << ExeNEON
));
4603 // CortexA9 is particularly picky about mixing the two and wants these
4605 if (Subtarget
.useNEONForFPMovs() && !isPredicated(MI
) &&
4606 (MI
.getOpcode() == ARM::VMOVRS
|| MI
.getOpcode() == ARM::VMOVSR
||
4607 MI
.getOpcode() == ARM::VMOVS
))
4608 return std::make_pair(ExeVFP
, (1 << ExeVFP
) | (1 << ExeNEON
));
4610 // No other instructions can be swizzled, so just determine their domain.
4611 unsigned Domain
= MI
.getDesc().TSFlags
& ARMII::DomainMask
;
4613 if (Domain
& ARMII::DomainNEON
)
4614 return std::make_pair(ExeNEON
, 0);
4616 // Certain instructions can go either way on Cortex-A8.
4617 // Treat them as NEON instructions.
4618 if ((Domain
& ARMII::DomainNEONA8
) && Subtarget
.isCortexA8())
4619 return std::make_pair(ExeNEON
, 0);
4621 if (Domain
& ARMII::DomainVFP
)
4622 return std::make_pair(ExeVFP
, 0);
4624 return std::make_pair(ExeGeneric
, 0);
4627 static unsigned getCorrespondingDRegAndLane(const TargetRegisterInfo
*TRI
,
4628 unsigned SReg
, unsigned &Lane
) {
4629 unsigned DReg
= TRI
->getMatchingSuperReg(SReg
, ARM::ssub_0
, &ARM::DPRRegClass
);
4632 if (DReg
!= ARM::NoRegister
)
4636 DReg
= TRI
->getMatchingSuperReg(SReg
, ARM::ssub_1
, &ARM::DPRRegClass
);
4638 assert(DReg
&& "S-register with no D super-register?");
4642 /// getImplicitSPRUseForDPRUse - Given a use of a DPR register and lane,
4643 /// set ImplicitSReg to a register number that must be marked as implicit-use or
4644 /// zero if no register needs to be defined as implicit-use.
4646 /// If the function cannot determine if an SPR should be marked implicit use or
4647 /// not, it returns false.
4649 /// This function handles cases where an instruction is being modified from taking
4650 /// an SPR to a DPR[Lane]. A use of the DPR is being added, which may conflict
4651 /// with an earlier def of an SPR corresponding to DPR[Lane^1] (i.e. the other
4652 /// lane of the DPR).
4654 /// If the other SPR is defined, an implicit-use of it should be added. Else,
4655 /// (including the case where the DPR itself is defined), it should not.
4657 static bool getImplicitSPRUseForDPRUse(const TargetRegisterInfo
*TRI
,
4658 MachineInstr
&MI
, unsigned DReg
,
4659 unsigned Lane
, unsigned &ImplicitSReg
) {
4660 // If the DPR is defined or used already, the other SPR lane will be chained
4661 // correctly, so there is nothing to be done.
4662 if (MI
.definesRegister(DReg
, TRI
) || MI
.readsRegister(DReg
, TRI
)) {
4667 // Otherwise we need to go searching to see if the SPR is set explicitly.
4668 ImplicitSReg
= TRI
->getSubReg(DReg
,
4669 (Lane
& 1) ? ARM::ssub_0
: ARM::ssub_1
);
4670 MachineBasicBlock::LivenessQueryResult LQR
=
4671 MI
.getParent()->computeRegisterLiveness(TRI
, ImplicitSReg
, MI
);
4673 if (LQR
== MachineBasicBlock::LQR_Live
)
4675 else if (LQR
== MachineBasicBlock::LQR_Unknown
)
4678 // If the register is known not to be live, there is no need to add an
4684 void ARMBaseInstrInfo::setExecutionDomain(MachineInstr
&MI
,
4685 unsigned Domain
) const {
4686 unsigned DstReg
, SrcReg
, DReg
;
4688 MachineInstrBuilder
MIB(*MI
.getParent()->getParent(), MI
);
4689 const TargetRegisterInfo
*TRI
= &getRegisterInfo();
4690 switch (MI
.getOpcode()) {
4692 llvm_unreachable("cannot handle opcode!");
4695 if (Domain
!= ExeNEON
)
4698 // Zap the predicate operands.
4699 assert(!isPredicated(MI
) && "Cannot predicate a VORRd");
4701 // Make sure we've got NEON instructions.
4702 assert(Subtarget
.hasNEON() && "VORRd requires NEON");
4704 // Source instruction is %DDst = VMOVD %DSrc, 14, %noreg (; implicits)
4705 DstReg
= MI
.getOperand(0).getReg();
4706 SrcReg
= MI
.getOperand(1).getReg();
4708 for (unsigned i
= MI
.getDesc().getNumOperands(); i
; --i
)
4709 MI
.RemoveOperand(i
- 1);
4711 // Change to a %DDst = VORRd %DSrc, %DSrc, 14, %noreg (; implicits)
4712 MI
.setDesc(get(ARM::VORRd
));
4713 MIB
.addReg(DstReg
, RegState::Define
)
4716 .add(predOps(ARMCC::AL
));
4719 if (Domain
!= ExeNEON
)
4721 assert(!isPredicated(MI
) && "Cannot predicate a VGETLN");
4723 // Source instruction is %RDst = VMOVRS %SSrc, 14, %noreg (; implicits)
4724 DstReg
= MI
.getOperand(0).getReg();
4725 SrcReg
= MI
.getOperand(1).getReg();
4727 for (unsigned i
= MI
.getDesc().getNumOperands(); i
; --i
)
4728 MI
.RemoveOperand(i
- 1);
4730 DReg
= getCorrespondingDRegAndLane(TRI
, SrcReg
, Lane
);
4732 // Convert to %RDst = VGETLNi32 %DSrc, Lane, 14, %noreg (; imps)
4733 // Note that DSrc has been widened and the other lane may be undef, which
4734 // contaminates the entire register.
4735 MI
.setDesc(get(ARM::VGETLNi32
));
4736 MIB
.addReg(DstReg
, RegState::Define
)
4737 .addReg(DReg
, RegState::Undef
)
4739 .add(predOps(ARMCC::AL
));
4741 // The old source should be an implicit use, otherwise we might think it
4742 // was dead before here.
4743 MIB
.addReg(SrcReg
, RegState::Implicit
);
4746 if (Domain
!= ExeNEON
)
4748 assert(!isPredicated(MI
) && "Cannot predicate a VSETLN");
4750 // Source instruction is %SDst = VMOVSR %RSrc, 14, %noreg (; implicits)
4751 DstReg
= MI
.getOperand(0).getReg();
4752 SrcReg
= MI
.getOperand(1).getReg();
4754 DReg
= getCorrespondingDRegAndLane(TRI
, DstReg
, Lane
);
4756 unsigned ImplicitSReg
;
4757 if (!getImplicitSPRUseForDPRUse(TRI
, MI
, DReg
, Lane
, ImplicitSReg
))
4760 for (unsigned i
= MI
.getDesc().getNumOperands(); i
; --i
)
4761 MI
.RemoveOperand(i
- 1);
4763 // Convert to %DDst = VSETLNi32 %DDst, %RSrc, Lane, 14, %noreg (; imps)
4764 // Again DDst may be undefined at the beginning of this instruction.
4765 MI
.setDesc(get(ARM::VSETLNi32
));
4766 MIB
.addReg(DReg
, RegState::Define
)
4767 .addReg(DReg
, getUndefRegState(!MI
.readsRegister(DReg
, TRI
)))
4770 .add(predOps(ARMCC::AL
));
4772 // The narrower destination must be marked as set to keep previous chains
4774 MIB
.addReg(DstReg
, RegState::Define
| RegState::Implicit
);
4775 if (ImplicitSReg
!= 0)
4776 MIB
.addReg(ImplicitSReg
, RegState::Implicit
);
4780 if (Domain
!= ExeNEON
)
4783 // Source instruction is %SDst = VMOVS %SSrc, 14, %noreg (; implicits)
4784 DstReg
= MI
.getOperand(0).getReg();
4785 SrcReg
= MI
.getOperand(1).getReg();
4787 unsigned DstLane
= 0, SrcLane
= 0, DDst
, DSrc
;
4788 DDst
= getCorrespondingDRegAndLane(TRI
, DstReg
, DstLane
);
4789 DSrc
= getCorrespondingDRegAndLane(TRI
, SrcReg
, SrcLane
);
4791 unsigned ImplicitSReg
;
4792 if (!getImplicitSPRUseForDPRUse(TRI
, MI
, DSrc
, SrcLane
, ImplicitSReg
))
4795 for (unsigned i
= MI
.getDesc().getNumOperands(); i
; --i
)
4796 MI
.RemoveOperand(i
- 1);
4799 // Destination can be:
4800 // %DDst = VDUPLN32d %DDst, Lane, 14, %noreg (; implicits)
4801 MI
.setDesc(get(ARM::VDUPLN32d
));
4802 MIB
.addReg(DDst
, RegState::Define
)
4803 .addReg(DDst
, getUndefRegState(!MI
.readsRegister(DDst
, TRI
)))
4805 .add(predOps(ARMCC::AL
));
4807 // Neither the source or the destination are naturally represented any
4808 // more, so add them in manually.
4809 MIB
.addReg(DstReg
, RegState::Implicit
| RegState::Define
);
4810 MIB
.addReg(SrcReg
, RegState::Implicit
);
4811 if (ImplicitSReg
!= 0)
4812 MIB
.addReg(ImplicitSReg
, RegState::Implicit
);
4816 // In general there's no single instruction that can perform an S <-> S
4817 // move in NEON space, but a pair of VEXT instructions *can* do the
4818 // job. It turns out that the VEXTs needed will only use DSrc once, with
4819 // the position based purely on the combination of lane-0 and lane-1
4820 // involved. For example
4821 // vmov s0, s2 -> vext.32 d0, d0, d1, #1 vext.32 d0, d0, d0, #1
4822 // vmov s1, s3 -> vext.32 d0, d1, d0, #1 vext.32 d0, d0, d0, #1
4823 // vmov s0, s3 -> vext.32 d0, d0, d0, #1 vext.32 d0, d1, d0, #1
4824 // vmov s1, s2 -> vext.32 d0, d0, d0, #1 vext.32 d0, d0, d1, #1
4826 // Pattern of the MachineInstrs is:
4827 // %DDst = VEXTd32 %DSrc1, %DSrc2, Lane, 14, %noreg (;implicits)
4828 MachineInstrBuilder NewMIB
;
4829 NewMIB
= BuildMI(*MI
.getParent(), MI
, MI
.getDebugLoc(), get(ARM::VEXTd32
),
4832 // On the first instruction, both DSrc and DDst may be undef if present.
4833 // Specifically when the original instruction didn't have them as an
4835 unsigned CurReg
= SrcLane
== 1 && DstLane
== 1 ? DSrc
: DDst
;
4836 bool CurUndef
= !MI
.readsRegister(CurReg
, TRI
);
4837 NewMIB
.addReg(CurReg
, getUndefRegState(CurUndef
));
4839 CurReg
= SrcLane
== 0 && DstLane
== 0 ? DSrc
: DDst
;
4840 CurUndef
= !MI
.readsRegister(CurReg
, TRI
);
4841 NewMIB
.addReg(CurReg
, getUndefRegState(CurUndef
))
4843 .add(predOps(ARMCC::AL
));
4845 if (SrcLane
== DstLane
)
4846 NewMIB
.addReg(SrcReg
, RegState::Implicit
);
4848 MI
.setDesc(get(ARM::VEXTd32
));
4849 MIB
.addReg(DDst
, RegState::Define
);
4851 // On the second instruction, DDst has definitely been defined above, so
4852 // it is not undef. DSrc, if present, can be undef as above.
4853 CurReg
= SrcLane
== 1 && DstLane
== 0 ? DSrc
: DDst
;
4854 CurUndef
= CurReg
== DSrc
&& !MI
.readsRegister(CurReg
, TRI
);
4855 MIB
.addReg(CurReg
, getUndefRegState(CurUndef
));
4857 CurReg
= SrcLane
== 0 && DstLane
== 1 ? DSrc
: DDst
;
4858 CurUndef
= CurReg
== DSrc
&& !MI
.readsRegister(CurReg
, TRI
);
4859 MIB
.addReg(CurReg
, getUndefRegState(CurUndef
))
4861 .add(predOps(ARMCC::AL
));
4863 if (SrcLane
!= DstLane
)
4864 MIB
.addReg(SrcReg
, RegState::Implicit
);
4866 // As before, the original destination is no longer represented, add it
4868 MIB
.addReg(DstReg
, RegState::Define
| RegState::Implicit
);
4869 if (ImplicitSReg
!= 0)
4870 MIB
.addReg(ImplicitSReg
, RegState::Implicit
);
4876 //===----------------------------------------------------------------------===//
4877 // Partial register updates
4878 //===----------------------------------------------------------------------===//
4880 // Swift renames NEON registers with 64-bit granularity. That means any
4881 // instruction writing an S-reg implicitly reads the containing D-reg. The
4882 // problem is mostly avoided by translating f32 operations to v2f32 operations
4883 // on D-registers, but f32 loads are still a problem.
4885 // These instructions can load an f32 into a NEON register:
4887 // VLDRS - Only writes S, partial D update.
4888 // VLD1LNd32 - Writes all D-regs, explicit partial D update, 2 uops.
4889 // VLD1DUPd32 - Writes all D-regs, no partial reg update, 2 uops.
4891 // FCONSTD can be used as a dependency-breaking instruction.
4892 unsigned ARMBaseInstrInfo::getPartialRegUpdateClearance(
4893 const MachineInstr
&MI
, unsigned OpNum
,
4894 const TargetRegisterInfo
*TRI
) const {
4895 auto PartialUpdateClearance
= Subtarget
.getPartialUpdateClearance();
4896 if (!PartialUpdateClearance
)
4899 assert(TRI
&& "Need TRI instance");
4901 const MachineOperand
&MO
= MI
.getOperand(OpNum
);
4904 unsigned Reg
= MO
.getReg();
4907 switch (MI
.getOpcode()) {
4908 // Normal instructions writing only an S-register.
4913 case ARM::VMOVv4i16
:
4914 case ARM::VMOVv2i32
:
4915 case ARM::VMOVv2f32
:
4916 case ARM::VMOVv1i64
:
4917 UseOp
= MI
.findRegisterUseOperandIdx(Reg
, false, TRI
);
4920 // Explicitly reads the dependency.
4921 case ARM::VLD1LNd32
:
4928 // If this instruction actually reads a value from Reg, there is no unwanted
4930 if (UseOp
!= -1 && MI
.getOperand(UseOp
).readsReg())
4933 // We must be able to clobber the whole D-reg.
4934 if (TargetRegisterInfo::isVirtualRegister(Reg
)) {
4935 // Virtual register must be a def undef foo:ssub_0 operand.
4936 if (!MO
.getSubReg() || MI
.readsVirtualRegister(Reg
))
4938 } else if (ARM::SPRRegClass
.contains(Reg
)) {
4939 // Physical register: MI must define the full D-reg.
4940 unsigned DReg
= TRI
->getMatchingSuperReg(Reg
, ARM::ssub_0
,
4942 if (!DReg
|| !MI
.definesRegister(DReg
, TRI
))
4946 // MI has an unwanted D-register dependency.
4947 // Avoid defs in the previous N instructrions.
4948 return PartialUpdateClearance
;
4951 // Break a partial register dependency after getPartialRegUpdateClearance
4952 // returned non-zero.
4953 void ARMBaseInstrInfo::breakPartialRegDependency(
4954 MachineInstr
&MI
, unsigned OpNum
, const TargetRegisterInfo
*TRI
) const {
4955 assert(OpNum
< MI
.getDesc().getNumDefs() && "OpNum is not a def");
4956 assert(TRI
&& "Need TRI instance");
4958 const MachineOperand
&MO
= MI
.getOperand(OpNum
);
4959 unsigned Reg
= MO
.getReg();
4960 assert(TargetRegisterInfo::isPhysicalRegister(Reg
) &&
4961 "Can't break virtual register dependencies.");
4962 unsigned DReg
= Reg
;
4964 // If MI defines an S-reg, find the corresponding D super-register.
4965 if (ARM::SPRRegClass
.contains(Reg
)) {
4966 DReg
= ARM::D0
+ (Reg
- ARM::S0
) / 2;
4967 assert(TRI
->isSuperRegister(Reg
, DReg
) && "Register enums broken");
4970 assert(ARM::DPRRegClass
.contains(DReg
) && "Can only break D-reg deps");
4971 assert(MI
.definesRegister(DReg
, TRI
) && "MI doesn't clobber full D-reg");
4973 // FIXME: In some cases, VLDRS can be changed to a VLD1DUPd32 which defines
4974 // the full D-register by loading the same value to both lanes. The
4975 // instruction is micro-coded with 2 uops, so don't do this until we can
4976 // properly schedule micro-coded instructions. The dispatcher stalls cause
4977 // too big regressions.
4979 // Insert the dependency-breaking FCONSTD before MI.
4980 // 96 is the encoding of 0.5, but the actual value doesn't matter here.
4981 BuildMI(*MI
.getParent(), MI
, MI
.getDebugLoc(), get(ARM::FCONSTD
), DReg
)
4983 .add(predOps(ARMCC::AL
));
4984 MI
.addRegisterKilled(DReg
, TRI
, true);
4987 bool ARMBaseInstrInfo::hasNOP() const {
4988 return Subtarget
.getFeatureBits()[ARM::HasV6KOps
];
4991 bool ARMBaseInstrInfo::isSwiftFastImmShift(const MachineInstr
*MI
) const {
4992 if (MI
->getNumOperands() < 4)
4994 unsigned ShOpVal
= MI
->getOperand(3).getImm();
4995 unsigned ShImm
= ARM_AM::getSORegOffset(ShOpVal
);
4996 // Swift supports faster shifts for: lsl 2, lsl 1, and lsr 1.
4997 if ((ShImm
== 1 && ARM_AM::getSORegShOp(ShOpVal
) == ARM_AM::lsr
) ||
4998 ((ShImm
== 1 || ShImm
== 2) &&
4999 ARM_AM::getSORegShOp(ShOpVal
) == ARM_AM::lsl
))
5005 bool ARMBaseInstrInfo::getRegSequenceLikeInputs(
5006 const MachineInstr
&MI
, unsigned DefIdx
,
5007 SmallVectorImpl
<RegSubRegPairAndIdx
> &InputRegs
) const {
5008 assert(DefIdx
< MI
.getDesc().getNumDefs() && "Invalid definition index");
5009 assert(MI
.isRegSequenceLike() && "Invalid kind of instruction");
5011 switch (MI
.getOpcode()) {
5013 // dX = VMOVDRR rY, rZ
5015 // dX = REG_SEQUENCE rY, ssub_0, rZ, ssub_1
5016 // Populate the InputRegs accordingly.
5018 const MachineOperand
*MOReg
= &MI
.getOperand(1);
5019 if (!MOReg
->isUndef())
5020 InputRegs
.push_back(RegSubRegPairAndIdx(MOReg
->getReg(),
5021 MOReg
->getSubReg(), ARM::ssub_0
));
5023 MOReg
= &MI
.getOperand(2);
5024 if (!MOReg
->isUndef())
5025 InputRegs
.push_back(RegSubRegPairAndIdx(MOReg
->getReg(),
5026 MOReg
->getSubReg(), ARM::ssub_1
));
5029 llvm_unreachable("Target dependent opcode missing");
5032 bool ARMBaseInstrInfo::getExtractSubregLikeInputs(
5033 const MachineInstr
&MI
, unsigned DefIdx
,
5034 RegSubRegPairAndIdx
&InputReg
) const {
5035 assert(DefIdx
< MI
.getDesc().getNumDefs() && "Invalid definition index");
5036 assert(MI
.isExtractSubregLike() && "Invalid kind of instruction");
5038 switch (MI
.getOpcode()) {
5040 // rX, rY = VMOVRRD dZ
5042 // rX = EXTRACT_SUBREG dZ, ssub_0
5043 // rY = EXTRACT_SUBREG dZ, ssub_1
5044 const MachineOperand
&MOReg
= MI
.getOperand(2);
5045 if (MOReg
.isUndef())
5047 InputReg
.Reg
= MOReg
.getReg();
5048 InputReg
.SubReg
= MOReg
.getSubReg();
5049 InputReg
.SubIdx
= DefIdx
== 0 ? ARM::ssub_0
: ARM::ssub_1
;
5052 llvm_unreachable("Target dependent opcode missing");
5055 bool ARMBaseInstrInfo::getInsertSubregLikeInputs(
5056 const MachineInstr
&MI
, unsigned DefIdx
, RegSubRegPair
&BaseReg
,
5057 RegSubRegPairAndIdx
&InsertedReg
) const {
5058 assert(DefIdx
< MI
.getDesc().getNumDefs() && "Invalid definition index");
5059 assert(MI
.isInsertSubregLike() && "Invalid kind of instruction");
5061 switch (MI
.getOpcode()) {
5062 case ARM::VSETLNi32
:
5063 // dX = VSETLNi32 dY, rZ, imm
5064 const MachineOperand
&MOBaseReg
= MI
.getOperand(1);
5065 const MachineOperand
&MOInsertedReg
= MI
.getOperand(2);
5066 if (MOInsertedReg
.isUndef())
5068 const MachineOperand
&MOIndex
= MI
.getOperand(3);
5069 BaseReg
.Reg
= MOBaseReg
.getReg();
5070 BaseReg
.SubReg
= MOBaseReg
.getSubReg();
5072 InsertedReg
.Reg
= MOInsertedReg
.getReg();
5073 InsertedReg
.SubReg
= MOInsertedReg
.getSubReg();
5074 InsertedReg
.SubIdx
= MOIndex
.getImm() == 0 ? ARM::ssub_0
: ARM::ssub_1
;
5077 llvm_unreachable("Target dependent opcode missing");
5080 std::pair
<unsigned, unsigned>
5081 ARMBaseInstrInfo::decomposeMachineOperandsTargetFlags(unsigned TF
) const {
5082 const unsigned Mask
= ARMII::MO_OPTION_MASK
;
5083 return std::make_pair(TF
& Mask
, TF
& ~Mask
);
5086 ArrayRef
<std::pair
<unsigned, const char *>>
5087 ARMBaseInstrInfo::getSerializableDirectMachineOperandTargetFlags() const {
5088 using namespace ARMII
;
5090 static const std::pair
<unsigned, const char *> TargetFlags
[] = {
5091 {MO_LO16
, "arm-lo16"}, {MO_HI16
, "arm-hi16"}};
5092 return makeArrayRef(TargetFlags
);
5095 ArrayRef
<std::pair
<unsigned, const char *>>
5096 ARMBaseInstrInfo::getSerializableBitmaskMachineOperandTargetFlags() const {
5097 using namespace ARMII
;
5099 static const std::pair
<unsigned, const char *> TargetFlags
[] = {
5100 {MO_COFFSTUB
, "arm-coffstub"},
5101 {MO_GOT
, "arm-got"},
5102 {MO_SBREL
, "arm-sbrel"},
5103 {MO_DLLIMPORT
, "arm-dllimport"},
5104 {MO_SECREL
, "arm-secrel"},
5105 {MO_NONLAZY
, "arm-nonlazy"}};
5106 return makeArrayRef(TargetFlags
);