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 "MVETailPredUtils.h"
23 #include "llvm/ADT/DenseMap.h"
24 #include "llvm/ADT/STLExtras.h"
25 #include "llvm/ADT/SmallSet.h"
26 #include "llvm/ADT/SmallVector.h"
27 #include "llvm/ADT/Triple.h"
28 #include "llvm/CodeGen/LiveVariables.h"
29 #include "llvm/CodeGen/MachineBasicBlock.h"
30 #include "llvm/CodeGen/MachineConstantPool.h"
31 #include "llvm/CodeGen/MachineFrameInfo.h"
32 #include "llvm/CodeGen/MachineFunction.h"
33 #include "llvm/CodeGen/MachineInstr.h"
34 #include "llvm/CodeGen/MachineInstrBuilder.h"
35 #include "llvm/CodeGen/MachineMemOperand.h"
36 #include "llvm/CodeGen/MachineModuleInfo.h"
37 #include "llvm/CodeGen/MachineOperand.h"
38 #include "llvm/CodeGen/MachineRegisterInfo.h"
39 #include "llvm/CodeGen/MachineScheduler.h"
40 #include "llvm/CodeGen/MultiHazardRecognizer.h"
41 #include "llvm/CodeGen/ScoreboardHazardRecognizer.h"
42 #include "llvm/CodeGen/SelectionDAGNodes.h"
43 #include "llvm/CodeGen/TargetInstrInfo.h"
44 #include "llvm/CodeGen/TargetRegisterInfo.h"
45 #include "llvm/CodeGen/TargetSchedule.h"
46 #include "llvm/IR/Attributes.h"
47 #include "llvm/IR/Constants.h"
48 #include "llvm/IR/DebugLoc.h"
49 #include "llvm/IR/Function.h"
50 #include "llvm/IR/GlobalValue.h"
51 #include "llvm/MC/MCAsmInfo.h"
52 #include "llvm/MC/MCInstrDesc.h"
53 #include "llvm/MC/MCInstrItineraries.h"
54 #include "llvm/Support/BranchProbability.h"
55 #include "llvm/Support/Casting.h"
56 #include "llvm/Support/CommandLine.h"
57 #include "llvm/Support/Compiler.h"
58 #include "llvm/Support/Debug.h"
59 #include "llvm/Support/ErrorHandling.h"
60 #include "llvm/Support/raw_ostream.h"
61 #include "llvm/Target/TargetMachine.h"
72 #define DEBUG_TYPE "arm-instrinfo"
74 #define GET_INSTRINFO_CTOR_DTOR
75 #include "ARMGenInstrInfo.inc"
78 EnableARM3Addr("enable-arm-3-addr-conv", cl::Hidden
,
79 cl::desc("Enable ARM 2-addr to 3-addr conv"));
81 /// ARM_MLxEntry - Record information about MLA / MLS instructions.
83 uint16_t MLxOpc
; // MLA / MLS opcode
84 uint16_t MulOpc
; // Expanded multiplication opcode
85 uint16_t AddSubOpc
; // Expanded add / sub opcode
86 bool NegAcc
; // True if the acc is negated before the add / sub.
87 bool HasLane
; // True if instruction has an extra "lane" operand.
90 static const ARM_MLxEntry ARM_MLxTable
[] = {
91 // MLxOpc, MulOpc, AddSubOpc, NegAcc, HasLane
93 { ARM::VMLAS
, ARM::VMULS
, ARM::VADDS
, false, false },
94 { ARM::VMLSS
, ARM::VMULS
, ARM::VSUBS
, false, false },
95 { ARM::VMLAD
, ARM::VMULD
, ARM::VADDD
, false, false },
96 { ARM::VMLSD
, ARM::VMULD
, ARM::VSUBD
, false, false },
97 { ARM::VNMLAS
, ARM::VNMULS
, ARM::VSUBS
, true, false },
98 { ARM::VNMLSS
, ARM::VMULS
, ARM::VSUBS
, true, false },
99 { ARM::VNMLAD
, ARM::VNMULD
, ARM::VSUBD
, true, false },
100 { ARM::VNMLSD
, ARM::VMULD
, ARM::VSUBD
, true, false },
103 { ARM::VMLAfd
, ARM::VMULfd
, ARM::VADDfd
, false, false },
104 { ARM::VMLSfd
, ARM::VMULfd
, ARM::VSUBfd
, false, false },
105 { ARM::VMLAfq
, ARM::VMULfq
, ARM::VADDfq
, false, false },
106 { ARM::VMLSfq
, ARM::VMULfq
, ARM::VSUBfq
, false, false },
107 { ARM::VMLAslfd
, ARM::VMULslfd
, ARM::VADDfd
, false, true },
108 { ARM::VMLSslfd
, ARM::VMULslfd
, ARM::VSUBfd
, false, true },
109 { ARM::VMLAslfq
, ARM::VMULslfq
, ARM::VADDfq
, false, true },
110 { ARM::VMLSslfq
, ARM::VMULslfq
, ARM::VSUBfq
, false, true },
113 ARMBaseInstrInfo::ARMBaseInstrInfo(const ARMSubtarget
& STI
)
114 : ARMGenInstrInfo(ARM::ADJCALLSTACKDOWN
, ARM::ADJCALLSTACKUP
),
116 for (unsigned i
= 0, e
= array_lengthof(ARM_MLxTable
); i
!= e
; ++i
) {
117 if (!MLxEntryMap
.insert(std::make_pair(ARM_MLxTable
[i
].MLxOpc
, i
)).second
)
118 llvm_unreachable("Duplicated entries?");
119 MLxHazardOpcodes
.insert(ARM_MLxTable
[i
].AddSubOpc
);
120 MLxHazardOpcodes
.insert(ARM_MLxTable
[i
].MulOpc
);
124 // Use a ScoreboardHazardRecognizer for prepass ARM scheduling. TargetInstrImpl
125 // currently defaults to no prepass hazard recognizer.
126 ScheduleHazardRecognizer
*
127 ARMBaseInstrInfo::CreateTargetHazardRecognizer(const TargetSubtargetInfo
*STI
,
128 const ScheduleDAG
*DAG
) const {
129 if (usePreRAHazardRecognizer()) {
130 const InstrItineraryData
*II
=
131 static_cast<const ARMSubtarget
*>(STI
)->getInstrItineraryData();
132 return new ScoreboardHazardRecognizer(II
, DAG
, "pre-RA-sched");
134 return TargetInstrInfo::CreateTargetHazardRecognizer(STI
, DAG
);
138 // - pre-RA scheduling
139 // - post-RA scheduling when FeatureUseMISched is set
140 ScheduleHazardRecognizer
*ARMBaseInstrInfo::CreateTargetMIHazardRecognizer(
141 const InstrItineraryData
*II
, const ScheduleDAGMI
*DAG
) const {
142 MultiHazardRecognizer
*MHR
= new MultiHazardRecognizer();
144 // We would like to restrict this hazard recognizer to only
145 // post-RA scheduling; we can tell that we're post-RA because we don't
146 // track VRegLiveness.
147 // Cortex-M7: TRM indicates that there is a single ITCM bank and two DTCM
148 // banks banked on bit 2. Assume that TCMs are in use.
149 if (Subtarget
.isCortexM7() && !DAG
->hasVRegLiveness())
150 MHR
->AddHazardRecognizer(
151 std::make_unique
<ARMBankConflictHazardRecognizer
>(DAG
, 0x4, true));
153 // Not inserting ARMHazardRecognizerFPMLx because that would change
156 auto BHR
= TargetInstrInfo::CreateTargetMIHazardRecognizer(II
, DAG
);
157 MHR
->AddHazardRecognizer(std::unique_ptr
<ScheduleHazardRecognizer
>(BHR
));
161 // Called during post-RA scheduling when FeatureUseMISched is not set
162 ScheduleHazardRecognizer
*ARMBaseInstrInfo::
163 CreateTargetPostRAHazardRecognizer(const InstrItineraryData
*II
,
164 const ScheduleDAG
*DAG
) const {
165 MultiHazardRecognizer
*MHR
= new MultiHazardRecognizer();
167 if (Subtarget
.isThumb2() || Subtarget
.hasVFP2Base())
168 MHR
->AddHazardRecognizer(std::make_unique
<ARMHazardRecognizerFPMLx
>());
170 auto BHR
= TargetInstrInfo::CreateTargetPostRAHazardRecognizer(II
, DAG
);
172 MHR
->AddHazardRecognizer(std::unique_ptr
<ScheduleHazardRecognizer
>(BHR
));
176 MachineInstr
*ARMBaseInstrInfo::convertToThreeAddress(
177 MachineFunction::iterator
&MFI
, MachineInstr
&MI
, LiveVariables
*LV
) const {
178 // FIXME: Thumb2 support.
183 MachineFunction
&MF
= *MI
.getParent()->getParent();
184 uint64_t TSFlags
= MI
.getDesc().TSFlags
;
186 switch ((TSFlags
& ARMII::IndexModeMask
) >> ARMII::IndexModeShift
) {
187 default: return nullptr;
188 case ARMII::IndexModePre
:
191 case ARMII::IndexModePost
:
195 // Try splitting an indexed load/store to an un-indexed one plus an add/sub
197 unsigned MemOpc
= getUnindexedOpcode(MI
.getOpcode());
201 MachineInstr
*UpdateMI
= nullptr;
202 MachineInstr
*MemMI
= nullptr;
203 unsigned AddrMode
= (TSFlags
& ARMII::AddrModeMask
);
204 const MCInstrDesc
&MCID
= MI
.getDesc();
205 unsigned NumOps
= MCID
.getNumOperands();
206 bool isLoad
= !MI
.mayStore();
207 const MachineOperand
&WB
= isLoad
? MI
.getOperand(1) : MI
.getOperand(0);
208 const MachineOperand
&Base
= MI
.getOperand(2);
209 const MachineOperand
&Offset
= MI
.getOperand(NumOps
- 3);
210 Register WBReg
= WB
.getReg();
211 Register BaseReg
= Base
.getReg();
212 Register OffReg
= Offset
.getReg();
213 unsigned OffImm
= MI
.getOperand(NumOps
- 2).getImm();
214 ARMCC::CondCodes Pred
= (ARMCC::CondCodes
)MI
.getOperand(NumOps
- 1).getImm();
216 default: llvm_unreachable("Unknown indexed op!");
217 case ARMII::AddrMode2
: {
218 bool isSub
= ARM_AM::getAM2Op(OffImm
) == ARM_AM::sub
;
219 unsigned Amt
= ARM_AM::getAM2Offset(OffImm
);
221 if (ARM_AM::getSOImmVal(Amt
) == -1)
222 // Can't encode it in a so_imm operand. This transformation will
223 // add more than 1 instruction. Abandon!
225 UpdateMI
= BuildMI(MF
, MI
.getDebugLoc(),
226 get(isSub
? ARM::SUBri
: ARM::ADDri
), WBReg
)
231 } else if (Amt
!= 0) {
232 ARM_AM::ShiftOpc ShOpc
= ARM_AM::getAM2ShiftOpc(OffImm
);
233 unsigned SOOpc
= ARM_AM::getSORegOpc(ShOpc
, Amt
);
234 UpdateMI
= BuildMI(MF
, MI
.getDebugLoc(),
235 get(isSub
? ARM::SUBrsi
: ARM::ADDrsi
), WBReg
)
243 UpdateMI
= BuildMI(MF
, MI
.getDebugLoc(),
244 get(isSub
? ARM::SUBrr
: ARM::ADDrr
), WBReg
)
251 case ARMII::AddrMode3
: {
252 bool isSub
= ARM_AM::getAM3Op(OffImm
) == ARM_AM::sub
;
253 unsigned Amt
= ARM_AM::getAM3Offset(OffImm
);
255 // Immediate is 8-bits. It's guaranteed to fit in a so_imm operand.
256 UpdateMI
= BuildMI(MF
, MI
.getDebugLoc(),
257 get(isSub
? ARM::SUBri
: ARM::ADDri
), WBReg
)
263 UpdateMI
= BuildMI(MF
, MI
.getDebugLoc(),
264 get(isSub
? ARM::SUBrr
: ARM::ADDrr
), WBReg
)
273 std::vector
<MachineInstr
*> NewMIs
;
277 BuildMI(MF
, MI
.getDebugLoc(), get(MemOpc
), MI
.getOperand(0).getReg())
282 MemMI
= BuildMI(MF
, MI
.getDebugLoc(), get(MemOpc
))
283 .addReg(MI
.getOperand(1).getReg())
288 NewMIs
.push_back(MemMI
);
289 NewMIs
.push_back(UpdateMI
);
293 BuildMI(MF
, MI
.getDebugLoc(), get(MemOpc
), MI
.getOperand(0).getReg())
298 MemMI
= BuildMI(MF
, MI
.getDebugLoc(), get(MemOpc
))
299 .addReg(MI
.getOperand(1).getReg())
305 UpdateMI
->getOperand(0).setIsDead();
306 NewMIs
.push_back(UpdateMI
);
307 NewMIs
.push_back(MemMI
);
310 // Transfer LiveVariables states, kill / dead info.
312 for (unsigned i
= 0, e
= MI
.getNumOperands(); i
!= e
; ++i
) {
313 MachineOperand
&MO
= MI
.getOperand(i
);
314 if (MO
.isReg() && Register::isVirtualRegister(MO
.getReg())) {
315 Register Reg
= MO
.getReg();
317 LiveVariables::VarInfo
&VI
= LV
->getVarInfo(Reg
);
319 MachineInstr
*NewMI
= (Reg
== WBReg
) ? UpdateMI
: MemMI
;
321 LV
->addVirtualRegisterDead(Reg
, *NewMI
);
323 if (MO
.isUse() && MO
.isKill()) {
324 for (unsigned j
= 0; j
< 2; ++j
) {
325 // Look at the two new MI's in reverse order.
326 MachineInstr
*NewMI
= NewMIs
[j
];
327 if (!NewMI
->readsRegister(Reg
))
329 LV
->addVirtualRegisterKilled(Reg
, *NewMI
);
330 if (VI
.removeKill(MI
))
331 VI
.Kills
.push_back(NewMI
);
339 MachineBasicBlock::iterator MBBI
= MI
.getIterator();
340 MFI
->insert(MBBI
, NewMIs
[1]);
341 MFI
->insert(MBBI
, NewMIs
[0]);
346 bool ARMBaseInstrInfo::analyzeBranch(MachineBasicBlock
&MBB
,
347 MachineBasicBlock
*&TBB
,
348 MachineBasicBlock
*&FBB
,
349 SmallVectorImpl
<MachineOperand
> &Cond
,
350 bool AllowModify
) const {
354 MachineBasicBlock::instr_iterator I
= MBB
.instr_end();
355 if (I
== MBB
.instr_begin())
356 return false; // Empty blocks are easy.
359 // Walk backwards from the end of the basic block until the branch is
360 // analyzed or we give up.
361 while (isPredicated(*I
) || I
->isTerminator() || I
->isDebugValue()) {
362 // Flag to be raised on unanalyzeable instructions. This is useful in cases
363 // where we want to clean up on the end of the basic block before we bail
365 bool CantAnalyze
= false;
367 // Skip over DEBUG values, predicated nonterminators and speculation
368 // barrier terminators.
369 while (I
->isDebugInstr() || !I
->isTerminator() ||
370 isSpeculationBarrierEndBBOpcode(I
->getOpcode()) ||
371 I
->getOpcode() == ARM::t2DoLoopStartTP
){
372 if (I
== MBB
.instr_begin())
377 if (isIndirectBranchOpcode(I
->getOpcode()) ||
378 isJumpTableBranchOpcode(I
->getOpcode())) {
379 // Indirect branches and jump tables can't be analyzed, but we still want
380 // to clean up any instructions at the tail of the basic block.
382 } else if (isUncondBranchOpcode(I
->getOpcode())) {
383 TBB
= I
->getOperand(0).getMBB();
384 } else if (isCondBranchOpcode(I
->getOpcode())) {
385 // Bail out if we encounter multiple conditional branches.
389 assert(!FBB
&& "FBB should have been null.");
391 TBB
= I
->getOperand(0).getMBB();
392 Cond
.push_back(I
->getOperand(1));
393 Cond
.push_back(I
->getOperand(2));
394 } else if (I
->isReturn()) {
395 // Returns can't be analyzed, but we should run cleanup.
398 // We encountered other unrecognized terminator. Bail out immediately.
402 // Cleanup code - to be run for unpredicated unconditional branches and
404 if (!isPredicated(*I
) &&
405 (isUncondBranchOpcode(I
->getOpcode()) ||
406 isIndirectBranchOpcode(I
->getOpcode()) ||
407 isJumpTableBranchOpcode(I
->getOpcode()) ||
409 // Forget any previous condition branch information - it no longer applies.
413 // If we can modify the function, delete everything below this
414 // unconditional branch.
416 MachineBasicBlock::iterator DI
= std::next(I
);
417 while (DI
!= MBB
.instr_end()) {
418 MachineInstr
&InstToDelete
= *DI
;
420 // Speculation barriers must not be deleted.
421 if (isSpeculationBarrierEndBBOpcode(InstToDelete
.getOpcode()))
423 InstToDelete
.eraseFromParent();
429 // We may not be able to analyze the block, but we could still have
430 // an unconditional branch as the last instruction in the block, which
431 // just branches to layout successor. If this is the case, then just
432 // remove it if we're allowed to make modifications.
433 if (AllowModify
&& !isPredicated(MBB
.back()) &&
434 isUncondBranchOpcode(MBB
.back().getOpcode()) &&
435 TBB
&& MBB
.isLayoutSuccessor(TBB
))
440 if (I
== MBB
.instr_begin())
446 // We made it past the terminators without bailing out - we must have
447 // analyzed this branch successfully.
451 unsigned ARMBaseInstrInfo::removeBranch(MachineBasicBlock
&MBB
,
452 int *BytesRemoved
) const {
453 assert(!BytesRemoved
&& "code size not handled");
455 MachineBasicBlock::iterator I
= MBB
.getLastNonDebugInstr();
459 if (!isUncondBranchOpcode(I
->getOpcode()) &&
460 !isCondBranchOpcode(I
->getOpcode()))
463 // Remove the branch.
464 I
->eraseFromParent();
468 if (I
== MBB
.begin()) return 1;
470 if (!isCondBranchOpcode(I
->getOpcode()))
473 // Remove the branch.
474 I
->eraseFromParent();
478 unsigned ARMBaseInstrInfo::insertBranch(MachineBasicBlock
&MBB
,
479 MachineBasicBlock
*TBB
,
480 MachineBasicBlock
*FBB
,
481 ArrayRef
<MachineOperand
> Cond
,
483 int *BytesAdded
) const {
484 assert(!BytesAdded
&& "code size not handled");
485 ARMFunctionInfo
*AFI
= MBB
.getParent()->getInfo
<ARMFunctionInfo
>();
486 int BOpc
= !AFI
->isThumbFunction()
487 ? ARM::B
: (AFI
->isThumb2Function() ? ARM::t2B
: ARM::tB
);
488 int BccOpc
= !AFI
->isThumbFunction()
489 ? ARM::Bcc
: (AFI
->isThumb2Function() ? ARM::t2Bcc
: ARM::tBcc
);
490 bool isThumb
= AFI
->isThumbFunction() || AFI
->isThumb2Function();
492 // Shouldn't be a fall through.
493 assert(TBB
&& "insertBranch must not be told to insert a fallthrough");
494 assert((Cond
.size() == 2 || Cond
.size() == 0) &&
495 "ARM branch conditions have two components!");
497 // For conditional branches, we use addOperand to preserve CPSR flags.
500 if (Cond
.empty()) { // Unconditional branch?
502 BuildMI(&MBB
, DL
, get(BOpc
)).addMBB(TBB
).add(predOps(ARMCC::AL
));
504 BuildMI(&MBB
, DL
, get(BOpc
)).addMBB(TBB
);
506 BuildMI(&MBB
, DL
, get(BccOpc
))
508 .addImm(Cond
[0].getImm())
513 // Two-way conditional branch.
514 BuildMI(&MBB
, DL
, get(BccOpc
))
516 .addImm(Cond
[0].getImm())
519 BuildMI(&MBB
, DL
, get(BOpc
)).addMBB(FBB
).add(predOps(ARMCC::AL
));
521 BuildMI(&MBB
, DL
, get(BOpc
)).addMBB(FBB
);
525 bool ARMBaseInstrInfo::
526 reverseBranchCondition(SmallVectorImpl
<MachineOperand
> &Cond
) const {
527 ARMCC::CondCodes CC
= (ARMCC::CondCodes
)(int)Cond
[0].getImm();
528 Cond
[0].setImm(ARMCC::getOppositeCondition(CC
));
532 bool ARMBaseInstrInfo::isPredicated(const MachineInstr
&MI
) const {
534 MachineBasicBlock::const_instr_iterator I
= MI
.getIterator();
535 MachineBasicBlock::const_instr_iterator E
= MI
.getParent()->instr_end();
536 while (++I
!= E
&& I
->isInsideBundle()) {
537 int PIdx
= I
->findFirstPredOperandIdx();
538 if (PIdx
!= -1 && I
->getOperand(PIdx
).getImm() != ARMCC::AL
)
544 int PIdx
= MI
.findFirstPredOperandIdx();
545 return PIdx
!= -1 && MI
.getOperand(PIdx
).getImm() != ARMCC::AL
;
548 std::string
ARMBaseInstrInfo::createMIROperandComment(
549 const MachineInstr
&MI
, const MachineOperand
&Op
, unsigned OpIdx
,
550 const TargetRegisterInfo
*TRI
) const {
552 // First, let's see if there is a generic comment for this operand
553 std::string GenericComment
=
554 TargetInstrInfo::createMIROperandComment(MI
, Op
, OpIdx
, TRI
);
555 if (!GenericComment
.empty())
556 return GenericComment
;
558 // If not, check if we have an immediate operand.
559 if (Op
.getType() != MachineOperand::MO_Immediate
)
560 return std::string();
562 // And print its corresponding condition code if the immediate is a
564 int FirstPredOp
= MI
.findFirstPredOperandIdx();
565 if (FirstPredOp
!= (int) OpIdx
)
566 return std::string();
568 std::string CC
= "CC::";
569 CC
+= ARMCondCodeToString((ARMCC::CondCodes
)Op
.getImm());
573 bool ARMBaseInstrInfo::PredicateInstruction(
574 MachineInstr
&MI
, ArrayRef
<MachineOperand
> Pred
) const {
575 unsigned Opc
= MI
.getOpcode();
576 if (isUncondBranchOpcode(Opc
)) {
577 MI
.setDesc(get(getMatchingCondBranchOpcode(Opc
)));
578 MachineInstrBuilder(*MI
.getParent()->getParent(), MI
)
579 .addImm(Pred
[0].getImm())
580 .addReg(Pred
[1].getReg());
584 int PIdx
= MI
.findFirstPredOperandIdx();
586 MachineOperand
&PMO
= MI
.getOperand(PIdx
);
587 PMO
.setImm(Pred
[0].getImm());
588 MI
.getOperand(PIdx
+1).setReg(Pred
[1].getReg());
590 // Thumb 1 arithmetic instructions do not set CPSR when executed inside an
591 // IT block. This affects how they are printed.
592 const MCInstrDesc
&MCID
= MI
.getDesc();
593 if (MCID
.TSFlags
& ARMII::ThumbArithFlagSetting
) {
594 assert(MCID
.OpInfo
[1].isOptionalDef() && "CPSR def isn't expected operand");
595 assert((MI
.getOperand(1).isDead() ||
596 MI
.getOperand(1).getReg() != ARM::CPSR
) &&
597 "if conversion tried to stop defining used CPSR");
598 MI
.getOperand(1).setReg(ARM::NoRegister
);
606 bool ARMBaseInstrInfo::SubsumesPredicate(ArrayRef
<MachineOperand
> Pred1
,
607 ArrayRef
<MachineOperand
> Pred2
) const {
608 if (Pred1
.size() > 2 || Pred2
.size() > 2)
611 ARMCC::CondCodes CC1
= (ARMCC::CondCodes
)Pred1
[0].getImm();
612 ARMCC::CondCodes CC2
= (ARMCC::CondCodes
)Pred2
[0].getImm();
622 return CC2
== ARMCC::HI
;
624 return CC2
== ARMCC::LO
|| CC2
== ARMCC::EQ
;
626 return CC2
== ARMCC::GT
;
628 return CC2
== ARMCC::LT
;
632 bool ARMBaseInstrInfo::ClobbersPredicate(MachineInstr
&MI
,
633 std::vector
<MachineOperand
> &Pred
,
634 bool SkipDead
) const {
636 for (unsigned i
= 0, e
= MI
.getNumOperands(); i
!= e
; ++i
) {
637 const MachineOperand
&MO
= MI
.getOperand(i
);
638 bool ClobbersCPSR
= MO
.isRegMask() && MO
.clobbersPhysReg(ARM::CPSR
);
639 bool IsCPSR
= MO
.isReg() && MO
.isDef() && MO
.getReg() == ARM::CPSR
;
640 if (ClobbersCPSR
|| IsCPSR
) {
642 // Filter out T1 instructions that have a dead CPSR,
643 // allowing IT blocks to be generated containing T1 instructions
644 const MCInstrDesc
&MCID
= MI
.getDesc();
645 if (MCID
.TSFlags
& ARMII::ThumbArithFlagSetting
&& MO
.isDead() &&
657 bool ARMBaseInstrInfo::isCPSRDefined(const MachineInstr
&MI
) {
658 for (const auto &MO
: MI
.operands())
659 if (MO
.isReg() && MO
.getReg() == ARM::CPSR
&& MO
.isDef() && !MO
.isDead())
664 static bool isEligibleForITBlock(const MachineInstr
*MI
) {
665 switch (MI
->getOpcode()) {
666 default: return true;
667 case ARM::tADC
: // ADC (register) T1
668 case ARM::tADDi3
: // ADD (immediate) T1
669 case ARM::tADDi8
: // ADD (immediate) T2
670 case ARM::tADDrr
: // ADD (register) T1
671 case ARM::tAND
: // AND (register) T1
672 case ARM::tASRri
: // ASR (immediate) T1
673 case ARM::tASRrr
: // ASR (register) T1
674 case ARM::tBIC
: // BIC (register) T1
675 case ARM::tEOR
: // EOR (register) T1
676 case ARM::tLSLri
: // LSL (immediate) T1
677 case ARM::tLSLrr
: // LSL (register) T1
678 case ARM::tLSRri
: // LSR (immediate) T1
679 case ARM::tLSRrr
: // LSR (register) T1
680 case ARM::tMUL
: // MUL T1
681 case ARM::tMVN
: // MVN (register) T1
682 case ARM::tORR
: // ORR (register) T1
683 case ARM::tROR
: // ROR (register) T1
684 case ARM::tRSB
: // RSB (immediate) T1
685 case ARM::tSBC
: // SBC (register) T1
686 case ARM::tSUBi3
: // SUB (immediate) T1
687 case ARM::tSUBi8
: // SUB (immediate) T2
688 case ARM::tSUBrr
: // SUB (register) T1
689 return !ARMBaseInstrInfo::isCPSRDefined(*MI
);
693 /// isPredicable - Return true if the specified instruction can be predicated.
694 /// By default, this returns true for every instruction with a
695 /// PredicateOperand.
696 bool ARMBaseInstrInfo::isPredicable(const MachineInstr
&MI
) const {
697 if (!MI
.isPredicable())
703 if (!isEligibleForITBlock(&MI
))
706 const MachineFunction
*MF
= MI
.getParent()->getParent();
707 const ARMFunctionInfo
*AFI
=
708 MF
->getInfo
<ARMFunctionInfo
>();
710 // Neon instructions in Thumb2 IT blocks are deprecated, see ARMARM.
711 // In their ARM encoding, they can't be encoded in a conditional form.
712 if ((MI
.getDesc().TSFlags
& ARMII::DomainMask
) == ARMII::DomainNEON
)
715 // Make indirect control flow changes unpredicable when SLS mitigation is
717 const ARMSubtarget
&ST
= MF
->getSubtarget
<ARMSubtarget
>();
718 if (ST
.hardenSlsRetBr() && isIndirectControlFlowNotComingBack(MI
))
720 if (ST
.hardenSlsBlr() && isIndirectCall(MI
))
723 if (AFI
->isThumb2Function()) {
724 if (getSubtarget().restrictIT())
725 return isV8EligibleForIT(&MI
);
733 template <> bool IsCPSRDead
<MachineInstr
>(const MachineInstr
*MI
) {
734 for (unsigned i
= 0, e
= MI
->getNumOperands(); i
!= e
; ++i
) {
735 const MachineOperand
&MO
= MI
->getOperand(i
);
736 if (!MO
.isReg() || MO
.isUndef() || MO
.isUse())
738 if (MO
.getReg() != ARM::CPSR
)
743 // all definitions of CPSR are dead
747 } // end namespace llvm
749 /// GetInstSize - Return the size of the specified MachineInstr.
751 unsigned ARMBaseInstrInfo::getInstSizeInBytes(const MachineInstr
&MI
) const {
752 const MachineBasicBlock
&MBB
= *MI
.getParent();
753 const MachineFunction
*MF
= MBB
.getParent();
754 const MCAsmInfo
*MAI
= MF
->getTarget().getMCAsmInfo();
756 const MCInstrDesc
&MCID
= MI
.getDesc();
758 return MCID
.getSize();
760 switch (MI
.getOpcode()) {
762 // pseudo-instruction sizes are zero.
764 case TargetOpcode::BUNDLE
:
765 return getInstBundleLength(MI
);
766 case ARM::MOVi16_ga_pcrel
:
767 case ARM::MOVTi16_ga_pcrel
:
768 case ARM::t2MOVi16_ga_pcrel
:
769 case ARM::t2MOVTi16_ga_pcrel
:
772 case ARM::t2MOVi32imm
:
774 case ARM::CONSTPOOL_ENTRY
:
775 case ARM::JUMPTABLE_INSTS
:
776 case ARM::JUMPTABLE_ADDRS
:
777 case ARM::JUMPTABLE_TBB
:
778 case ARM::JUMPTABLE_TBH
:
779 // If this machine instr is a constant pool entry, its size is recorded as
781 return MI
.getOperand(2).getImm();
782 case ARM::Int_eh_sjlj_longjmp
:
784 case ARM::tInt_eh_sjlj_longjmp
:
786 case ARM::tInt_WIN_eh_sjlj_longjmp
:
788 case ARM::Int_eh_sjlj_setjmp
:
789 case ARM::Int_eh_sjlj_setjmp_nofp
:
791 case ARM::tInt_eh_sjlj_setjmp
:
792 case ARM::t2Int_eh_sjlj_setjmp
:
793 case ARM::t2Int_eh_sjlj_setjmp_nofp
:
796 return MI
.getOperand(1).getImm();
798 case ARM::INLINEASM_BR
: {
799 // If this machine instr is an inline asm, measure it.
800 unsigned Size
= getInlineAsmLength(MI
.getOperand(0).getSymbolName(), *MAI
);
801 if (!MF
->getInfo
<ARMFunctionInfo
>()->isThumbFunction())
802 Size
= alignTo(Size
, 4);
805 case ARM::SpeculationBarrierISBDSBEndBB
:
806 case ARM::t2SpeculationBarrierISBDSBEndBB
:
807 // This gets lowered to 2 4-byte instructions.
809 case ARM::SpeculationBarrierSBEndBB
:
810 case ARM::t2SpeculationBarrierSBEndBB
:
811 // This gets lowered to 1 4-byte instructions.
816 unsigned ARMBaseInstrInfo::getInstBundleLength(const MachineInstr
&MI
) const {
818 MachineBasicBlock::const_instr_iterator I
= MI
.getIterator();
819 MachineBasicBlock::const_instr_iterator E
= MI
.getParent()->instr_end();
820 while (++I
!= E
&& I
->isInsideBundle()) {
821 assert(!I
->isBundle() && "No nested bundle!");
822 Size
+= getInstSizeInBytes(*I
);
827 void ARMBaseInstrInfo::copyFromCPSR(MachineBasicBlock
&MBB
,
828 MachineBasicBlock::iterator I
,
829 unsigned DestReg
, bool KillSrc
,
830 const ARMSubtarget
&Subtarget
) const {
831 unsigned Opc
= Subtarget
.isThumb()
832 ? (Subtarget
.isMClass() ? ARM::t2MRS_M
: ARM::t2MRS_AR
)
835 MachineInstrBuilder MIB
=
836 BuildMI(MBB
, I
, I
->getDebugLoc(), get(Opc
), DestReg
);
838 // There is only 1 A/R class MRS instruction, and it always refers to
839 // APSR. However, there are lots of other possibilities on M-class cores.
840 if (Subtarget
.isMClass())
843 MIB
.add(predOps(ARMCC::AL
))
844 .addReg(ARM::CPSR
, RegState::Implicit
| getKillRegState(KillSrc
));
847 void ARMBaseInstrInfo::copyToCPSR(MachineBasicBlock
&MBB
,
848 MachineBasicBlock::iterator I
,
849 unsigned SrcReg
, bool KillSrc
,
850 const ARMSubtarget
&Subtarget
) const {
851 unsigned Opc
= Subtarget
.isThumb()
852 ? (Subtarget
.isMClass() ? ARM::t2MSR_M
: ARM::t2MSR_AR
)
855 MachineInstrBuilder MIB
= BuildMI(MBB
, I
, I
->getDebugLoc(), get(Opc
));
857 if (Subtarget
.isMClass())
862 MIB
.addReg(SrcReg
, getKillRegState(KillSrc
))
863 .add(predOps(ARMCC::AL
))
864 .addReg(ARM::CPSR
, RegState::Implicit
| RegState::Define
);
867 void llvm::addUnpredicatedMveVpredNOp(MachineInstrBuilder
&MIB
) {
868 MIB
.addImm(ARMVCC::None
);
872 void llvm::addUnpredicatedMveVpredROp(MachineInstrBuilder
&MIB
,
874 addUnpredicatedMveVpredNOp(MIB
);
875 MIB
.addReg(DestReg
, RegState::Undef
);
878 void llvm::addPredicatedMveVpredNOp(MachineInstrBuilder
&MIB
, unsigned Cond
) {
880 MIB
.addReg(ARM::VPR
, RegState::Implicit
);
883 void llvm::addPredicatedMveVpredROp(MachineInstrBuilder
&MIB
,
884 unsigned Cond
, unsigned Inactive
) {
885 addPredicatedMveVpredNOp(MIB
, Cond
);
886 MIB
.addReg(Inactive
);
889 void ARMBaseInstrInfo::copyPhysReg(MachineBasicBlock
&MBB
,
890 MachineBasicBlock::iterator I
,
891 const DebugLoc
&DL
, MCRegister DestReg
,
892 MCRegister SrcReg
, bool KillSrc
) const {
893 bool GPRDest
= ARM::GPRRegClass
.contains(DestReg
);
894 bool GPRSrc
= ARM::GPRRegClass
.contains(SrcReg
);
896 if (GPRDest
&& GPRSrc
) {
897 BuildMI(MBB
, I
, DL
, get(ARM::MOVr
), DestReg
)
898 .addReg(SrcReg
, getKillRegState(KillSrc
))
899 .add(predOps(ARMCC::AL
))
904 bool SPRDest
= ARM::SPRRegClass
.contains(DestReg
);
905 bool SPRSrc
= ARM::SPRRegClass
.contains(SrcReg
);
908 if (SPRDest
&& SPRSrc
)
910 else if (GPRDest
&& SPRSrc
)
912 else if (SPRDest
&& GPRSrc
)
914 else if (ARM::DPRRegClass
.contains(DestReg
, SrcReg
) && Subtarget
.hasFP64())
916 else if (ARM::QPRRegClass
.contains(DestReg
, SrcReg
))
917 Opc
= Subtarget
.hasNEON() ? ARM::VORRq
: ARM::MVE_VORR
;
920 MachineInstrBuilder MIB
= BuildMI(MBB
, I
, DL
, get(Opc
), DestReg
);
921 MIB
.addReg(SrcReg
, getKillRegState(KillSrc
));
922 if (Opc
== ARM::VORRq
|| Opc
== ARM::MVE_VORR
)
923 MIB
.addReg(SrcReg
, getKillRegState(KillSrc
));
924 if (Opc
== ARM::MVE_VORR
)
925 addUnpredicatedMveVpredROp(MIB
, DestReg
);
927 MIB
.add(predOps(ARMCC::AL
));
931 // Handle register classes that require multiple instructions.
932 unsigned BeginIdx
= 0;
933 unsigned SubRegs
= 0;
936 // Use VORRq when possible.
937 if (ARM::QQPRRegClass
.contains(DestReg
, SrcReg
)) {
938 Opc
= Subtarget
.hasNEON() ? ARM::VORRq
: ARM::MVE_VORR
;
939 BeginIdx
= ARM::qsub_0
;
941 } else if (ARM::QQQQPRRegClass
.contains(DestReg
, SrcReg
)) {
942 Opc
= Subtarget
.hasNEON() ? ARM::VORRq
: ARM::MVE_VORR
;
943 BeginIdx
= ARM::qsub_0
;
945 // Fall back to VMOVD.
946 } else if (ARM::DPairRegClass
.contains(DestReg
, SrcReg
)) {
948 BeginIdx
= ARM::dsub_0
;
950 } else if (ARM::DTripleRegClass
.contains(DestReg
, SrcReg
)) {
952 BeginIdx
= ARM::dsub_0
;
954 } else if (ARM::DQuadRegClass
.contains(DestReg
, SrcReg
)) {
956 BeginIdx
= ARM::dsub_0
;
958 } else if (ARM::GPRPairRegClass
.contains(DestReg
, SrcReg
)) {
959 Opc
= Subtarget
.isThumb2() ? ARM::tMOVr
: ARM::MOVr
;
960 BeginIdx
= ARM::gsub_0
;
962 } else if (ARM::DPairSpcRegClass
.contains(DestReg
, SrcReg
)) {
964 BeginIdx
= ARM::dsub_0
;
967 } else if (ARM::DTripleSpcRegClass
.contains(DestReg
, SrcReg
)) {
969 BeginIdx
= ARM::dsub_0
;
972 } else if (ARM::DQuadSpcRegClass
.contains(DestReg
, SrcReg
)) {
974 BeginIdx
= ARM::dsub_0
;
977 } else if (ARM::DPRRegClass
.contains(DestReg
, SrcReg
) &&
978 !Subtarget
.hasFP64()) {
980 BeginIdx
= ARM::ssub_0
;
982 } else if (SrcReg
== ARM::CPSR
) {
983 copyFromCPSR(MBB
, I
, DestReg
, KillSrc
, Subtarget
);
985 } else if (DestReg
== ARM::CPSR
) {
986 copyToCPSR(MBB
, I
, SrcReg
, KillSrc
, Subtarget
);
988 } else if (DestReg
== ARM::VPR
) {
989 assert(ARM::GPRRegClass
.contains(SrcReg
));
990 BuildMI(MBB
, I
, I
->getDebugLoc(), get(ARM::VMSR_P0
), DestReg
)
991 .addReg(SrcReg
, getKillRegState(KillSrc
))
992 .add(predOps(ARMCC::AL
));
994 } else if (SrcReg
== ARM::VPR
) {
995 assert(ARM::GPRRegClass
.contains(DestReg
));
996 BuildMI(MBB
, I
, I
->getDebugLoc(), get(ARM::VMRS_P0
), DestReg
)
997 .addReg(SrcReg
, getKillRegState(KillSrc
))
998 .add(predOps(ARMCC::AL
));
1000 } else if (DestReg
== ARM::FPSCR_NZCV
) {
1001 assert(ARM::GPRRegClass
.contains(SrcReg
));
1002 BuildMI(MBB
, I
, I
->getDebugLoc(), get(ARM::VMSR_FPSCR_NZCVQC
), DestReg
)
1003 .addReg(SrcReg
, getKillRegState(KillSrc
))
1004 .add(predOps(ARMCC::AL
));
1006 } else if (SrcReg
== ARM::FPSCR_NZCV
) {
1007 assert(ARM::GPRRegClass
.contains(DestReg
));
1008 BuildMI(MBB
, I
, I
->getDebugLoc(), get(ARM::VMRS_FPSCR_NZCVQC
), DestReg
)
1009 .addReg(SrcReg
, getKillRegState(KillSrc
))
1010 .add(predOps(ARMCC::AL
));
1014 assert(Opc
&& "Impossible reg-to-reg copy");
1016 const TargetRegisterInfo
*TRI
= &getRegisterInfo();
1017 MachineInstrBuilder Mov
;
1019 // Copy register tuples backward when the first Dest reg overlaps with SrcReg.
1020 if (TRI
->regsOverlap(SrcReg
, TRI
->getSubReg(DestReg
, BeginIdx
))) {
1021 BeginIdx
= BeginIdx
+ ((SubRegs
- 1) * Spacing
);
1025 SmallSet
<unsigned, 4> DstRegs
;
1027 for (unsigned i
= 0; i
!= SubRegs
; ++i
) {
1028 Register Dst
= TRI
->getSubReg(DestReg
, BeginIdx
+ i
* Spacing
);
1029 Register Src
= TRI
->getSubReg(SrcReg
, BeginIdx
+ i
* Spacing
);
1030 assert(Dst
&& Src
&& "Bad sub-register");
1032 assert(!DstRegs
.count(Src
) && "destructive vector copy");
1033 DstRegs
.insert(Dst
);
1035 Mov
= BuildMI(MBB
, I
, I
->getDebugLoc(), get(Opc
), Dst
).addReg(Src
);
1036 // VORR (NEON or MVE) takes two source operands.
1037 if (Opc
== ARM::VORRq
|| Opc
== ARM::MVE_VORR
) {
1040 // MVE VORR takes predicate operands in place of an ordinary condition.
1041 if (Opc
== ARM::MVE_VORR
)
1042 addUnpredicatedMveVpredROp(Mov
, Dst
);
1044 Mov
= Mov
.add(predOps(ARMCC::AL
));
1046 if (Opc
== ARM::MOVr
)
1047 Mov
= Mov
.add(condCodeOp());
1049 // Add implicit super-register defs and kills to the last instruction.
1050 Mov
->addRegisterDefined(DestReg
, TRI
);
1052 Mov
->addRegisterKilled(SrcReg
, TRI
);
1055 Optional
<DestSourcePair
>
1056 ARMBaseInstrInfo::isCopyInstrImpl(const MachineInstr
&MI
) const {
1057 // VMOVRRD is also a copy instruction but it requires
1058 // special way of handling. It is more complex copy version
1059 // and since that we are not considering it. For recognition
1060 // of such instruction isExtractSubregLike MI interface fuction
1062 // VORRq is considered as a move only if two inputs are
1063 // the same register.
1064 if (!MI
.isMoveReg() ||
1065 (MI
.getOpcode() == ARM::VORRq
&&
1066 MI
.getOperand(1).getReg() != MI
.getOperand(2).getReg()))
1068 return DestSourcePair
{MI
.getOperand(0), MI
.getOperand(1)};
1071 Optional
<ParamLoadedValue
>
1072 ARMBaseInstrInfo::describeLoadedValue(const MachineInstr
&MI
,
1073 Register Reg
) const {
1074 if (auto DstSrcPair
= isCopyInstrImpl(MI
)) {
1075 Register DstReg
= DstSrcPair
->Destination
->getReg();
1077 // TODO: We don't handle cases where the forwarding reg is narrower/wider
1078 // than the copy registers. Consider for example:
1084 // We'd like to describe the call site value of d0 as d8, but this requires
1085 // gathering and merging the descriptions for the two VMOVS instructions.
1087 // We also don't handle the reverse situation, where the forwarding reg is
1088 // narrower than the copy destination:
1093 // We need to produce a fragment description (the call site value of s1 is
1098 return TargetInstrInfo::describeLoadedValue(MI
, Reg
);
1101 const MachineInstrBuilder
&
1102 ARMBaseInstrInfo::AddDReg(MachineInstrBuilder
&MIB
, unsigned Reg
,
1103 unsigned SubIdx
, unsigned State
,
1104 const TargetRegisterInfo
*TRI
) const {
1106 return MIB
.addReg(Reg
, State
);
1108 if (Register::isPhysicalRegister(Reg
))
1109 return MIB
.addReg(TRI
->getSubReg(Reg
, SubIdx
), State
);
1110 return MIB
.addReg(Reg
, State
, SubIdx
);
1113 void ARMBaseInstrInfo::
1114 storeRegToStackSlot(MachineBasicBlock
&MBB
, MachineBasicBlock::iterator I
,
1115 Register SrcReg
, bool isKill
, int FI
,
1116 const TargetRegisterClass
*RC
,
1117 const TargetRegisterInfo
*TRI
) const {
1118 MachineFunction
&MF
= *MBB
.getParent();
1119 MachineFrameInfo
&MFI
= MF
.getFrameInfo();
1120 Align Alignment
= MFI
.getObjectAlign(FI
);
1122 MachineMemOperand
*MMO
= MF
.getMachineMemOperand(
1123 MachinePointerInfo::getFixedStack(MF
, FI
), MachineMemOperand::MOStore
,
1124 MFI
.getObjectSize(FI
), Alignment
);
1126 switch (TRI
->getSpillSize(*RC
)) {
1128 if (ARM::HPRRegClass
.hasSubClassEq(RC
)) {
1129 BuildMI(MBB
, I
, DebugLoc(), get(ARM::VSTRH
))
1130 .addReg(SrcReg
, getKillRegState(isKill
))
1134 .add(predOps(ARMCC::AL
));
1136 llvm_unreachable("Unknown reg class!");
1139 if (ARM::GPRRegClass
.hasSubClassEq(RC
)) {
1140 BuildMI(MBB
, I
, DebugLoc(), get(ARM::STRi12
))
1141 .addReg(SrcReg
, getKillRegState(isKill
))
1145 .add(predOps(ARMCC::AL
));
1146 } else if (ARM::SPRRegClass
.hasSubClassEq(RC
)) {
1147 BuildMI(MBB
, I
, DebugLoc(), get(ARM::VSTRS
))
1148 .addReg(SrcReg
, getKillRegState(isKill
))
1152 .add(predOps(ARMCC::AL
));
1153 } else if (ARM::VCCRRegClass
.hasSubClassEq(RC
)) {
1154 BuildMI(MBB
, I
, DebugLoc(), get(ARM::VSTR_P0_off
))
1155 .addReg(SrcReg
, getKillRegState(isKill
))
1159 .add(predOps(ARMCC::AL
));
1161 llvm_unreachable("Unknown reg class!");
1164 if (ARM::DPRRegClass
.hasSubClassEq(RC
)) {
1165 BuildMI(MBB
, I
, DebugLoc(), get(ARM::VSTRD
))
1166 .addReg(SrcReg
, getKillRegState(isKill
))
1170 .add(predOps(ARMCC::AL
));
1171 } else if (ARM::GPRPairRegClass
.hasSubClassEq(RC
)) {
1172 if (Subtarget
.hasV5TEOps()) {
1173 MachineInstrBuilder MIB
= BuildMI(MBB
, I
, DebugLoc(), get(ARM::STRD
));
1174 AddDReg(MIB
, SrcReg
, ARM::gsub_0
, getKillRegState(isKill
), TRI
);
1175 AddDReg(MIB
, SrcReg
, ARM::gsub_1
, 0, TRI
);
1176 MIB
.addFrameIndex(FI
).addReg(0).addImm(0).addMemOperand(MMO
)
1177 .add(predOps(ARMCC::AL
));
1179 // Fallback to STM instruction, which has existed since the dawn of
1181 MachineInstrBuilder MIB
= BuildMI(MBB
, I
, DebugLoc(), get(ARM::STMIA
))
1184 .add(predOps(ARMCC::AL
));
1185 AddDReg(MIB
, SrcReg
, ARM::gsub_0
, getKillRegState(isKill
), TRI
);
1186 AddDReg(MIB
, SrcReg
, ARM::gsub_1
, 0, TRI
);
1189 llvm_unreachable("Unknown reg class!");
1192 if (ARM::DPairRegClass
.hasSubClassEq(RC
) && Subtarget
.hasNEON()) {
1193 // Use aligned spills if the stack can be realigned.
1194 if (Alignment
>= 16 && getRegisterInfo().canRealignStack(MF
)) {
1195 BuildMI(MBB
, I
, DebugLoc(), get(ARM::VST1q64
))
1198 .addReg(SrcReg
, getKillRegState(isKill
))
1200 .add(predOps(ARMCC::AL
));
1202 BuildMI(MBB
, I
, DebugLoc(), get(ARM::VSTMQIA
))
1203 .addReg(SrcReg
, getKillRegState(isKill
))
1206 .add(predOps(ARMCC::AL
));
1208 } else if (ARM::QPRRegClass
.hasSubClassEq(RC
) &&
1209 Subtarget
.hasMVEIntegerOps()) {
1210 auto MIB
= BuildMI(MBB
, I
, DebugLoc(), get(ARM::MVE_VSTRWU32
));
1211 MIB
.addReg(SrcReg
, getKillRegState(isKill
))
1214 .addMemOperand(MMO
);
1215 addUnpredicatedMveVpredNOp(MIB
);
1217 llvm_unreachable("Unknown reg class!");
1220 if (ARM::DTripleRegClass
.hasSubClassEq(RC
)) {
1221 // Use aligned spills if the stack can be realigned.
1222 if (Alignment
>= 16 && getRegisterInfo().canRealignStack(MF
) &&
1223 Subtarget
.hasNEON()) {
1224 BuildMI(MBB
, I
, DebugLoc(), get(ARM::VST1d64TPseudo
))
1227 .addReg(SrcReg
, getKillRegState(isKill
))
1229 .add(predOps(ARMCC::AL
));
1231 MachineInstrBuilder MIB
= BuildMI(MBB
, I
, DebugLoc(),
1234 .add(predOps(ARMCC::AL
))
1235 .addMemOperand(MMO
);
1236 MIB
= AddDReg(MIB
, SrcReg
, ARM::dsub_0
, getKillRegState(isKill
), TRI
);
1237 MIB
= AddDReg(MIB
, SrcReg
, ARM::dsub_1
, 0, TRI
);
1238 AddDReg(MIB
, SrcReg
, ARM::dsub_2
, 0, TRI
);
1241 llvm_unreachable("Unknown reg class!");
1244 if (ARM::QQPRRegClass
.hasSubClassEq(RC
) ||
1245 ARM::MQQPRRegClass
.hasSubClassEq(RC
) ||
1246 ARM::DQuadRegClass
.hasSubClassEq(RC
)) {
1247 if (Alignment
>= 16 && getRegisterInfo().canRealignStack(MF
) &&
1248 Subtarget
.hasNEON()) {
1249 // FIXME: It's possible to only store part of the QQ register if the
1250 // spilled def has a sub-register index.
1251 BuildMI(MBB
, I
, DebugLoc(), get(ARM::VST1d64QPseudo
))
1254 .addReg(SrcReg
, getKillRegState(isKill
))
1256 .add(predOps(ARMCC::AL
));
1257 } else if (Subtarget
.hasMVEIntegerOps()) {
1258 BuildMI(MBB
, I
, DebugLoc(), get(ARM::MQQPRStore
))
1259 .addReg(SrcReg
, getKillRegState(isKill
))
1261 .addMemOperand(MMO
);
1263 MachineInstrBuilder MIB
= BuildMI(MBB
, I
, DebugLoc(),
1266 .add(predOps(ARMCC::AL
))
1267 .addMemOperand(MMO
);
1268 MIB
= AddDReg(MIB
, SrcReg
, ARM::dsub_0
, getKillRegState(isKill
), TRI
);
1269 MIB
= AddDReg(MIB
, SrcReg
, ARM::dsub_1
, 0, TRI
);
1270 MIB
= AddDReg(MIB
, SrcReg
, ARM::dsub_2
, 0, TRI
);
1271 AddDReg(MIB
, SrcReg
, ARM::dsub_3
, 0, TRI
);
1274 llvm_unreachable("Unknown reg class!");
1277 if (ARM::MQQQQPRRegClass
.hasSubClassEq(RC
) &&
1278 Subtarget
.hasMVEIntegerOps()) {
1279 BuildMI(MBB
, I
, DebugLoc(), get(ARM::MQQQQPRStore
))
1280 .addReg(SrcReg
, getKillRegState(isKill
))
1282 .addMemOperand(MMO
);
1283 } else if (ARM::QQQQPRRegClass
.hasSubClassEq(RC
)) {
1284 MachineInstrBuilder MIB
= BuildMI(MBB
, I
, DebugLoc(), get(ARM::VSTMDIA
))
1286 .add(predOps(ARMCC::AL
))
1287 .addMemOperand(MMO
);
1288 MIB
= AddDReg(MIB
, SrcReg
, ARM::dsub_0
, getKillRegState(isKill
), TRI
);
1289 MIB
= AddDReg(MIB
, SrcReg
, ARM::dsub_1
, 0, TRI
);
1290 MIB
= AddDReg(MIB
, SrcReg
, ARM::dsub_2
, 0, TRI
);
1291 MIB
= AddDReg(MIB
, SrcReg
, ARM::dsub_3
, 0, TRI
);
1292 MIB
= AddDReg(MIB
, SrcReg
, ARM::dsub_4
, 0, TRI
);
1293 MIB
= AddDReg(MIB
, SrcReg
, ARM::dsub_5
, 0, TRI
);
1294 MIB
= AddDReg(MIB
, SrcReg
, ARM::dsub_6
, 0, TRI
);
1295 AddDReg(MIB
, SrcReg
, ARM::dsub_7
, 0, TRI
);
1297 llvm_unreachable("Unknown reg class!");
1300 llvm_unreachable("Unknown reg class!");
1304 unsigned ARMBaseInstrInfo::isStoreToStackSlot(const MachineInstr
&MI
,
1305 int &FrameIndex
) const {
1306 switch (MI
.getOpcode()) {
1309 case ARM::t2STRs
: // FIXME: don't use t2STRs to access frame.
1310 if (MI
.getOperand(1).isFI() && MI
.getOperand(2).isReg() &&
1311 MI
.getOperand(3).isImm() && MI
.getOperand(2).getReg() == 0 &&
1312 MI
.getOperand(3).getImm() == 0) {
1313 FrameIndex
= MI
.getOperand(1).getIndex();
1314 return MI
.getOperand(0).getReg();
1322 case ARM::VSTR_P0_off
:
1323 case ARM::MVE_VSTRWU32
:
1324 if (MI
.getOperand(1).isFI() && MI
.getOperand(2).isImm() &&
1325 MI
.getOperand(2).getImm() == 0) {
1326 FrameIndex
= MI
.getOperand(1).getIndex();
1327 return MI
.getOperand(0).getReg();
1331 case ARM::VST1d64TPseudo
:
1332 case ARM::VST1d64QPseudo
:
1333 if (MI
.getOperand(0).isFI() && MI
.getOperand(2).getSubReg() == 0) {
1334 FrameIndex
= MI
.getOperand(0).getIndex();
1335 return MI
.getOperand(2).getReg();
1339 if (MI
.getOperand(1).isFI() && MI
.getOperand(0).getSubReg() == 0) {
1340 FrameIndex
= MI
.getOperand(1).getIndex();
1341 return MI
.getOperand(0).getReg();
1344 case ARM::MQQPRStore
:
1345 case ARM::MQQQQPRStore
:
1346 if (MI
.getOperand(1).isFI()) {
1347 FrameIndex
= MI
.getOperand(1).getIndex();
1348 return MI
.getOperand(0).getReg();
1356 unsigned ARMBaseInstrInfo::isStoreToStackSlotPostFE(const MachineInstr
&MI
,
1357 int &FrameIndex
) const {
1358 SmallVector
<const MachineMemOperand
*, 1> Accesses
;
1359 if (MI
.mayStore() && hasStoreToStackSlot(MI
, Accesses
) &&
1360 Accesses
.size() == 1) {
1362 cast
<FixedStackPseudoSourceValue
>(Accesses
.front()->getPseudoValue())
1369 void ARMBaseInstrInfo::
1370 loadRegFromStackSlot(MachineBasicBlock
&MBB
, MachineBasicBlock::iterator I
,
1371 Register DestReg
, int FI
,
1372 const TargetRegisterClass
*RC
,
1373 const TargetRegisterInfo
*TRI
) const {
1375 if (I
!= MBB
.end()) DL
= I
->getDebugLoc();
1376 MachineFunction
&MF
= *MBB
.getParent();
1377 MachineFrameInfo
&MFI
= MF
.getFrameInfo();
1378 const Align Alignment
= MFI
.getObjectAlign(FI
);
1379 MachineMemOperand
*MMO
= MF
.getMachineMemOperand(
1380 MachinePointerInfo::getFixedStack(MF
, FI
), MachineMemOperand::MOLoad
,
1381 MFI
.getObjectSize(FI
), Alignment
);
1383 switch (TRI
->getSpillSize(*RC
)) {
1385 if (ARM::HPRRegClass
.hasSubClassEq(RC
)) {
1386 BuildMI(MBB
, I
, DL
, get(ARM::VLDRH
), DestReg
)
1390 .add(predOps(ARMCC::AL
));
1392 llvm_unreachable("Unknown reg class!");
1395 if (ARM::GPRRegClass
.hasSubClassEq(RC
)) {
1396 BuildMI(MBB
, I
, DL
, get(ARM::LDRi12
), DestReg
)
1400 .add(predOps(ARMCC::AL
));
1401 } else if (ARM::SPRRegClass
.hasSubClassEq(RC
)) {
1402 BuildMI(MBB
, I
, DL
, get(ARM::VLDRS
), DestReg
)
1406 .add(predOps(ARMCC::AL
));
1407 } else if (ARM::VCCRRegClass
.hasSubClassEq(RC
)) {
1408 BuildMI(MBB
, I
, DL
, get(ARM::VLDR_P0_off
), DestReg
)
1412 .add(predOps(ARMCC::AL
));
1414 llvm_unreachable("Unknown reg class!");
1417 if (ARM::DPRRegClass
.hasSubClassEq(RC
)) {
1418 BuildMI(MBB
, I
, DL
, get(ARM::VLDRD
), DestReg
)
1422 .add(predOps(ARMCC::AL
));
1423 } else if (ARM::GPRPairRegClass
.hasSubClassEq(RC
)) {
1424 MachineInstrBuilder MIB
;
1426 if (Subtarget
.hasV5TEOps()) {
1427 MIB
= BuildMI(MBB
, I
, DL
, get(ARM::LDRD
));
1428 AddDReg(MIB
, DestReg
, ARM::gsub_0
, RegState::DefineNoRead
, TRI
);
1429 AddDReg(MIB
, DestReg
, ARM::gsub_1
, RegState::DefineNoRead
, TRI
);
1430 MIB
.addFrameIndex(FI
).addReg(0).addImm(0).addMemOperand(MMO
)
1431 .add(predOps(ARMCC::AL
));
1433 // Fallback to LDM instruction, which has existed since the dawn of
1435 MIB
= BuildMI(MBB
, I
, DL
, get(ARM::LDMIA
))
1438 .add(predOps(ARMCC::AL
));
1439 MIB
= AddDReg(MIB
, DestReg
, ARM::gsub_0
, RegState::DefineNoRead
, TRI
);
1440 MIB
= AddDReg(MIB
, DestReg
, ARM::gsub_1
, RegState::DefineNoRead
, TRI
);
1443 if (Register::isPhysicalRegister(DestReg
))
1444 MIB
.addReg(DestReg
, RegState::ImplicitDefine
);
1446 llvm_unreachable("Unknown reg class!");
1449 if (ARM::DPairRegClass
.hasSubClassEq(RC
) && Subtarget
.hasNEON()) {
1450 if (Alignment
>= 16 && getRegisterInfo().canRealignStack(MF
)) {
1451 BuildMI(MBB
, I
, DL
, get(ARM::VLD1q64
), DestReg
)
1455 .add(predOps(ARMCC::AL
));
1457 BuildMI(MBB
, I
, DL
, get(ARM::VLDMQIA
), DestReg
)
1460 .add(predOps(ARMCC::AL
));
1462 } else if (ARM::QPRRegClass
.hasSubClassEq(RC
) &&
1463 Subtarget
.hasMVEIntegerOps()) {
1464 auto MIB
= BuildMI(MBB
, I
, DL
, get(ARM::MVE_VLDRWU32
), DestReg
);
1465 MIB
.addFrameIndex(FI
)
1467 .addMemOperand(MMO
);
1468 addUnpredicatedMveVpredNOp(MIB
);
1470 llvm_unreachable("Unknown reg class!");
1473 if (ARM::DTripleRegClass
.hasSubClassEq(RC
)) {
1474 if (Alignment
>= 16 && getRegisterInfo().canRealignStack(MF
) &&
1475 Subtarget
.hasNEON()) {
1476 BuildMI(MBB
, I
, DL
, get(ARM::VLD1d64TPseudo
), DestReg
)
1480 .add(predOps(ARMCC::AL
));
1482 MachineInstrBuilder MIB
= BuildMI(MBB
, I
, DL
, get(ARM::VLDMDIA
))
1485 .add(predOps(ARMCC::AL
));
1486 MIB
= AddDReg(MIB
, DestReg
, ARM::dsub_0
, RegState::DefineNoRead
, TRI
);
1487 MIB
= AddDReg(MIB
, DestReg
, ARM::dsub_1
, RegState::DefineNoRead
, TRI
);
1488 MIB
= AddDReg(MIB
, DestReg
, ARM::dsub_2
, RegState::DefineNoRead
, TRI
);
1489 if (Register::isPhysicalRegister(DestReg
))
1490 MIB
.addReg(DestReg
, RegState::ImplicitDefine
);
1493 llvm_unreachable("Unknown reg class!");
1496 if (ARM::QQPRRegClass
.hasSubClassEq(RC
) ||
1497 ARM::MQQPRRegClass
.hasSubClassEq(RC
) ||
1498 ARM::DQuadRegClass
.hasSubClassEq(RC
)) {
1499 if (Alignment
>= 16 && getRegisterInfo().canRealignStack(MF
) &&
1500 Subtarget
.hasNEON()) {
1501 BuildMI(MBB
, I
, DL
, get(ARM::VLD1d64QPseudo
), DestReg
)
1505 .add(predOps(ARMCC::AL
));
1506 } else if (Subtarget
.hasMVEIntegerOps()) {
1507 BuildMI(MBB
, I
, DL
, get(ARM::MQQPRLoad
), DestReg
)
1509 .addMemOperand(MMO
);
1511 MachineInstrBuilder MIB
= BuildMI(MBB
, I
, DL
, get(ARM::VLDMDIA
))
1513 .add(predOps(ARMCC::AL
))
1514 .addMemOperand(MMO
);
1515 MIB
= AddDReg(MIB
, DestReg
, ARM::dsub_0
, RegState::DefineNoRead
, TRI
);
1516 MIB
= AddDReg(MIB
, DestReg
, ARM::dsub_1
, RegState::DefineNoRead
, TRI
);
1517 MIB
= AddDReg(MIB
, DestReg
, ARM::dsub_2
, RegState::DefineNoRead
, TRI
);
1518 MIB
= AddDReg(MIB
, DestReg
, ARM::dsub_3
, RegState::DefineNoRead
, TRI
);
1519 if (Register::isPhysicalRegister(DestReg
))
1520 MIB
.addReg(DestReg
, RegState::ImplicitDefine
);
1523 llvm_unreachable("Unknown reg class!");
1526 if (ARM::MQQQQPRRegClass
.hasSubClassEq(RC
) &&
1527 Subtarget
.hasMVEIntegerOps()) {
1528 BuildMI(MBB
, I
, DL
, get(ARM::MQQQQPRLoad
), DestReg
)
1530 .addMemOperand(MMO
);
1531 } else if (ARM::QQQQPRRegClass
.hasSubClassEq(RC
)) {
1532 MachineInstrBuilder MIB
= BuildMI(MBB
, I
, DL
, get(ARM::VLDMDIA
))
1534 .add(predOps(ARMCC::AL
))
1535 .addMemOperand(MMO
);
1536 MIB
= AddDReg(MIB
, DestReg
, ARM::dsub_0
, RegState::DefineNoRead
, TRI
);
1537 MIB
= AddDReg(MIB
, DestReg
, ARM::dsub_1
, RegState::DefineNoRead
, TRI
);
1538 MIB
= AddDReg(MIB
, DestReg
, ARM::dsub_2
, RegState::DefineNoRead
, TRI
);
1539 MIB
= AddDReg(MIB
, DestReg
, ARM::dsub_3
, RegState::DefineNoRead
, TRI
);
1540 MIB
= AddDReg(MIB
, DestReg
, ARM::dsub_4
, RegState::DefineNoRead
, TRI
);
1541 MIB
= AddDReg(MIB
, DestReg
, ARM::dsub_5
, RegState::DefineNoRead
, TRI
);
1542 MIB
= AddDReg(MIB
, DestReg
, ARM::dsub_6
, RegState::DefineNoRead
, TRI
);
1543 MIB
= AddDReg(MIB
, DestReg
, ARM::dsub_7
, RegState::DefineNoRead
, TRI
);
1544 if (Register::isPhysicalRegister(DestReg
))
1545 MIB
.addReg(DestReg
, RegState::ImplicitDefine
);
1547 llvm_unreachable("Unknown reg class!");
1550 llvm_unreachable("Unknown regclass!");
1554 unsigned ARMBaseInstrInfo::isLoadFromStackSlot(const MachineInstr
&MI
,
1555 int &FrameIndex
) const {
1556 switch (MI
.getOpcode()) {
1559 case ARM::t2LDRs
: // FIXME: don't use t2LDRs to access frame.
1560 if (MI
.getOperand(1).isFI() && MI
.getOperand(2).isReg() &&
1561 MI
.getOperand(3).isImm() && MI
.getOperand(2).getReg() == 0 &&
1562 MI
.getOperand(3).getImm() == 0) {
1563 FrameIndex
= MI
.getOperand(1).getIndex();
1564 return MI
.getOperand(0).getReg();
1572 case ARM::VLDR_P0_off
:
1573 case ARM::MVE_VLDRWU32
:
1574 if (MI
.getOperand(1).isFI() && MI
.getOperand(2).isImm() &&
1575 MI
.getOperand(2).getImm() == 0) {
1576 FrameIndex
= MI
.getOperand(1).getIndex();
1577 return MI
.getOperand(0).getReg();
1581 case ARM::VLD1d8TPseudo
:
1582 case ARM::VLD1d16TPseudo
:
1583 case ARM::VLD1d32TPseudo
:
1584 case ARM::VLD1d64TPseudo
:
1585 case ARM::VLD1d8QPseudo
:
1586 case ARM::VLD1d16QPseudo
:
1587 case ARM::VLD1d32QPseudo
:
1588 case ARM::VLD1d64QPseudo
:
1589 if (MI
.getOperand(1).isFI() && MI
.getOperand(0).getSubReg() == 0) {
1590 FrameIndex
= MI
.getOperand(1).getIndex();
1591 return MI
.getOperand(0).getReg();
1595 if (MI
.getOperand(1).isFI() && MI
.getOperand(0).getSubReg() == 0) {
1596 FrameIndex
= MI
.getOperand(1).getIndex();
1597 return MI
.getOperand(0).getReg();
1600 case ARM::MQQPRLoad
:
1601 case ARM::MQQQQPRLoad
:
1602 if (MI
.getOperand(1).isFI()) {
1603 FrameIndex
= MI
.getOperand(1).getIndex();
1604 return MI
.getOperand(0).getReg();
1612 unsigned ARMBaseInstrInfo::isLoadFromStackSlotPostFE(const MachineInstr
&MI
,
1613 int &FrameIndex
) const {
1614 SmallVector
<const MachineMemOperand
*, 1> Accesses
;
1615 if (MI
.mayLoad() && hasLoadFromStackSlot(MI
, Accesses
) &&
1616 Accesses
.size() == 1) {
1618 cast
<FixedStackPseudoSourceValue
>(Accesses
.front()->getPseudoValue())
1625 /// Expands MEMCPY to either LDMIA/STMIA or LDMIA_UPD/STMID_UPD
1626 /// depending on whether the result is used.
1627 void ARMBaseInstrInfo::expandMEMCPY(MachineBasicBlock::iterator MI
) const {
1628 bool isThumb1
= Subtarget
.isThumb1Only();
1629 bool isThumb2
= Subtarget
.isThumb2();
1630 const ARMBaseInstrInfo
*TII
= Subtarget
.getInstrInfo();
1632 DebugLoc dl
= MI
->getDebugLoc();
1633 MachineBasicBlock
*BB
= MI
->getParent();
1635 MachineInstrBuilder LDM
, STM
;
1636 if (isThumb1
|| !MI
->getOperand(1).isDead()) {
1637 MachineOperand
LDWb(MI
->getOperand(1));
1638 LDM
= BuildMI(*BB
, MI
, dl
, TII
->get(isThumb2
? ARM::t2LDMIA_UPD
1639 : isThumb1
? ARM::tLDMIA_UPD
1643 LDM
= BuildMI(*BB
, MI
, dl
, TII
->get(isThumb2
? ARM::t2LDMIA
: ARM::LDMIA
));
1646 if (isThumb1
|| !MI
->getOperand(0).isDead()) {
1647 MachineOperand
STWb(MI
->getOperand(0));
1648 STM
= BuildMI(*BB
, MI
, dl
, TII
->get(isThumb2
? ARM::t2STMIA_UPD
1649 : isThumb1
? ARM::tSTMIA_UPD
1653 STM
= BuildMI(*BB
, MI
, dl
, TII
->get(isThumb2
? ARM::t2STMIA
: ARM::STMIA
));
1656 MachineOperand
LDBase(MI
->getOperand(3));
1657 LDM
.add(LDBase
).add(predOps(ARMCC::AL
));
1659 MachineOperand
STBase(MI
->getOperand(2));
1660 STM
.add(STBase
).add(predOps(ARMCC::AL
));
1662 // Sort the scratch registers into ascending order.
1663 const TargetRegisterInfo
&TRI
= getRegisterInfo();
1664 SmallVector
<unsigned, 6> ScratchRegs
;
1665 for(unsigned I
= 5; I
< MI
->getNumOperands(); ++I
)
1666 ScratchRegs
.push_back(MI
->getOperand(I
).getReg());
1667 llvm::sort(ScratchRegs
,
1668 [&TRI
](const unsigned &Reg1
, const unsigned &Reg2
) -> bool {
1669 return TRI
.getEncodingValue(Reg1
) <
1670 TRI
.getEncodingValue(Reg2
);
1673 for (const auto &Reg
: ScratchRegs
) {
1674 LDM
.addReg(Reg
, RegState::Define
);
1675 STM
.addReg(Reg
, RegState::Kill
);
1681 bool ARMBaseInstrInfo::expandPostRAPseudo(MachineInstr
&MI
) const {
1682 if (MI
.getOpcode() == TargetOpcode::LOAD_STACK_GUARD
) {
1683 assert(getSubtarget().getTargetTriple().isOSBinFormatMachO() &&
1684 "LOAD_STACK_GUARD currently supported only for MachO.");
1685 expandLoadStackGuard(MI
);
1686 MI
.getParent()->erase(MI
);
1690 if (MI
.getOpcode() == ARM::MEMCPY
) {
1695 // This hook gets to expand COPY instructions before they become
1696 // copyPhysReg() calls. Look for VMOVS instructions that can legally be
1697 // widened to VMOVD. We prefer the VMOVD when possible because it may be
1698 // changed into a VORR that can go down the NEON pipeline.
1699 if (!MI
.isCopy() || Subtarget
.dontWidenVMOVS() || !Subtarget
.hasFP64())
1702 // Look for a copy between even S-registers. That is where we keep floats
1703 // when using NEON v2f32 instructions for f32 arithmetic.
1704 Register DstRegS
= MI
.getOperand(0).getReg();
1705 Register SrcRegS
= MI
.getOperand(1).getReg();
1706 if (!ARM::SPRRegClass
.contains(DstRegS
, SrcRegS
))
1709 const TargetRegisterInfo
*TRI
= &getRegisterInfo();
1710 unsigned DstRegD
= TRI
->getMatchingSuperReg(DstRegS
, ARM::ssub_0
,
1712 unsigned SrcRegD
= TRI
->getMatchingSuperReg(SrcRegS
, ARM::ssub_0
,
1714 if (!DstRegD
|| !SrcRegD
)
1717 // We want to widen this into a DstRegD = VMOVD SrcRegD copy. This is only
1718 // legal if the COPY already defines the full DstRegD, and it isn't a
1719 // sub-register insertion.
1720 if (!MI
.definesRegister(DstRegD
, TRI
) || MI
.readsRegister(DstRegD
, TRI
))
1723 // A dead copy shouldn't show up here, but reject it just in case.
1724 if (MI
.getOperand(0).isDead())
1727 // All clear, widen the COPY.
1728 LLVM_DEBUG(dbgs() << "widening: " << MI
);
1729 MachineInstrBuilder
MIB(*MI
.getParent()->getParent(), MI
);
1731 // Get rid of the old implicit-def of DstRegD. Leave it if it defines a Q-reg
1732 // or some other super-register.
1733 int ImpDefIdx
= MI
.findRegisterDefOperandIdx(DstRegD
);
1734 if (ImpDefIdx
!= -1)
1735 MI
.RemoveOperand(ImpDefIdx
);
1737 // Change the opcode and operands.
1738 MI
.setDesc(get(ARM::VMOVD
));
1739 MI
.getOperand(0).setReg(DstRegD
);
1740 MI
.getOperand(1).setReg(SrcRegD
);
1741 MIB
.add(predOps(ARMCC::AL
));
1743 // We are now reading SrcRegD instead of SrcRegS. This may upset the
1744 // register scavenger and machine verifier, so we need to indicate that we
1745 // are reading an undefined value from SrcRegD, but a proper value from
1747 MI
.getOperand(1).setIsUndef();
1748 MIB
.addReg(SrcRegS
, RegState::Implicit
);
1750 // SrcRegD may actually contain an unrelated value in the ssub_1
1751 // sub-register. Don't kill it. Only kill the ssub_0 sub-register.
1752 if (MI
.getOperand(1).isKill()) {
1753 MI
.getOperand(1).setIsKill(false);
1754 MI
.addRegisterKilled(SrcRegS
, TRI
, true);
1757 LLVM_DEBUG(dbgs() << "replaced by: " << MI
);
1761 /// Create a copy of a const pool value. Update CPI to the new index and return
1763 static unsigned duplicateCPV(MachineFunction
&MF
, unsigned &CPI
) {
1764 MachineConstantPool
*MCP
= MF
.getConstantPool();
1765 ARMFunctionInfo
*AFI
= MF
.getInfo
<ARMFunctionInfo
>();
1767 const MachineConstantPoolEntry
&MCPE
= MCP
->getConstants()[CPI
];
1768 assert(MCPE
.isMachineConstantPoolEntry() &&
1769 "Expecting a machine constantpool entry!");
1770 ARMConstantPoolValue
*ACPV
=
1771 static_cast<ARMConstantPoolValue
*>(MCPE
.Val
.MachineCPVal
);
1773 unsigned PCLabelId
= AFI
->createPICLabelUId();
1774 ARMConstantPoolValue
*NewCPV
= nullptr;
1776 // FIXME: The below assumes PIC relocation model and that the function
1777 // is Thumb mode (t1 or t2). PCAdjustment would be 8 for ARM mode PIC, and
1778 // zero for non-PIC in ARM or Thumb. The callers are all of thumb LDR
1779 // instructions, so that's probably OK, but is PIC always correct when
1781 if (ACPV
->isGlobalValue())
1782 NewCPV
= ARMConstantPoolConstant::Create(
1783 cast
<ARMConstantPoolConstant
>(ACPV
)->getGV(), PCLabelId
, ARMCP::CPValue
,
1784 4, ACPV
->getModifier(), ACPV
->mustAddCurrentAddress());
1785 else if (ACPV
->isExtSymbol())
1786 NewCPV
= ARMConstantPoolSymbol::
1787 Create(MF
.getFunction().getContext(),
1788 cast
<ARMConstantPoolSymbol
>(ACPV
)->getSymbol(), PCLabelId
, 4);
1789 else if (ACPV
->isBlockAddress())
1790 NewCPV
= ARMConstantPoolConstant::
1791 Create(cast
<ARMConstantPoolConstant
>(ACPV
)->getBlockAddress(), PCLabelId
,
1792 ARMCP::CPBlockAddress
, 4);
1793 else if (ACPV
->isLSDA())
1794 NewCPV
= ARMConstantPoolConstant::Create(&MF
.getFunction(), PCLabelId
,
1796 else if (ACPV
->isMachineBasicBlock())
1797 NewCPV
= ARMConstantPoolMBB::
1798 Create(MF
.getFunction().getContext(),
1799 cast
<ARMConstantPoolMBB
>(ACPV
)->getMBB(), PCLabelId
, 4);
1801 llvm_unreachable("Unexpected ARM constantpool value type!!");
1802 CPI
= MCP
->getConstantPoolIndex(NewCPV
, MCPE
.getAlign());
1806 void ARMBaseInstrInfo::reMaterialize(MachineBasicBlock
&MBB
,
1807 MachineBasicBlock::iterator I
,
1808 Register DestReg
, unsigned SubIdx
,
1809 const MachineInstr
&Orig
,
1810 const TargetRegisterInfo
&TRI
) const {
1811 unsigned Opcode
= Orig
.getOpcode();
1814 MachineInstr
*MI
= MBB
.getParent()->CloneMachineInstr(&Orig
);
1815 MI
->substituteRegister(Orig
.getOperand(0).getReg(), DestReg
, SubIdx
, TRI
);
1819 case ARM::tLDRpci_pic
:
1820 case ARM::t2LDRpci_pic
: {
1821 MachineFunction
&MF
= *MBB
.getParent();
1822 unsigned CPI
= Orig
.getOperand(1).getIndex();
1823 unsigned PCLabelId
= duplicateCPV(MF
, CPI
);
1824 BuildMI(MBB
, I
, Orig
.getDebugLoc(), get(Opcode
), DestReg
)
1825 .addConstantPoolIndex(CPI
)
1827 .cloneMemRefs(Orig
);
1834 ARMBaseInstrInfo::duplicate(MachineBasicBlock
&MBB
,
1835 MachineBasicBlock::iterator InsertBefore
,
1836 const MachineInstr
&Orig
) const {
1837 MachineInstr
&Cloned
= TargetInstrInfo::duplicate(MBB
, InsertBefore
, Orig
);
1838 MachineBasicBlock::instr_iterator I
= Cloned
.getIterator();
1840 switch (I
->getOpcode()) {
1841 case ARM::tLDRpci_pic
:
1842 case ARM::t2LDRpci_pic
: {
1843 MachineFunction
&MF
= *MBB
.getParent();
1844 unsigned CPI
= I
->getOperand(1).getIndex();
1845 unsigned PCLabelId
= duplicateCPV(MF
, CPI
);
1846 I
->getOperand(1).setIndex(CPI
);
1847 I
->getOperand(2).setImm(PCLabelId
);
1851 if (!I
->isBundledWithSucc())
1858 bool ARMBaseInstrInfo::produceSameValue(const MachineInstr
&MI0
,
1859 const MachineInstr
&MI1
,
1860 const MachineRegisterInfo
*MRI
) const {
1861 unsigned Opcode
= MI0
.getOpcode();
1862 if (Opcode
== ARM::t2LDRpci
||
1863 Opcode
== ARM::t2LDRpci_pic
||
1864 Opcode
== ARM::tLDRpci
||
1865 Opcode
== ARM::tLDRpci_pic
||
1866 Opcode
== ARM::LDRLIT_ga_pcrel
||
1867 Opcode
== ARM::LDRLIT_ga_pcrel_ldr
||
1868 Opcode
== ARM::tLDRLIT_ga_pcrel
||
1869 Opcode
== ARM::MOV_ga_pcrel
||
1870 Opcode
== ARM::MOV_ga_pcrel_ldr
||
1871 Opcode
== ARM::t2MOV_ga_pcrel
) {
1872 if (MI1
.getOpcode() != Opcode
)
1874 if (MI0
.getNumOperands() != MI1
.getNumOperands())
1877 const MachineOperand
&MO0
= MI0
.getOperand(1);
1878 const MachineOperand
&MO1
= MI1
.getOperand(1);
1879 if (MO0
.getOffset() != MO1
.getOffset())
1882 if (Opcode
== ARM::LDRLIT_ga_pcrel
||
1883 Opcode
== ARM::LDRLIT_ga_pcrel_ldr
||
1884 Opcode
== ARM::tLDRLIT_ga_pcrel
||
1885 Opcode
== ARM::MOV_ga_pcrel
||
1886 Opcode
== ARM::MOV_ga_pcrel_ldr
||
1887 Opcode
== ARM::t2MOV_ga_pcrel
)
1888 // Ignore the PC labels.
1889 return MO0
.getGlobal() == MO1
.getGlobal();
1891 const MachineFunction
*MF
= MI0
.getParent()->getParent();
1892 const MachineConstantPool
*MCP
= MF
->getConstantPool();
1893 int CPI0
= MO0
.getIndex();
1894 int CPI1
= MO1
.getIndex();
1895 const MachineConstantPoolEntry
&MCPE0
= MCP
->getConstants()[CPI0
];
1896 const MachineConstantPoolEntry
&MCPE1
= MCP
->getConstants()[CPI1
];
1897 bool isARMCP0
= MCPE0
.isMachineConstantPoolEntry();
1898 bool isARMCP1
= MCPE1
.isMachineConstantPoolEntry();
1899 if (isARMCP0
&& isARMCP1
) {
1900 ARMConstantPoolValue
*ACPV0
=
1901 static_cast<ARMConstantPoolValue
*>(MCPE0
.Val
.MachineCPVal
);
1902 ARMConstantPoolValue
*ACPV1
=
1903 static_cast<ARMConstantPoolValue
*>(MCPE1
.Val
.MachineCPVal
);
1904 return ACPV0
->hasSameValue(ACPV1
);
1905 } else if (!isARMCP0
&& !isARMCP1
) {
1906 return MCPE0
.Val
.ConstVal
== MCPE1
.Val
.ConstVal
;
1909 } else if (Opcode
== ARM::PICLDR
) {
1910 if (MI1
.getOpcode() != Opcode
)
1912 if (MI0
.getNumOperands() != MI1
.getNumOperands())
1915 Register Addr0
= MI0
.getOperand(1).getReg();
1916 Register Addr1
= MI1
.getOperand(1).getReg();
1917 if (Addr0
!= Addr1
) {
1918 if (!MRI
|| !Register::isVirtualRegister(Addr0
) ||
1919 !Register::isVirtualRegister(Addr1
))
1922 // This assumes SSA form.
1923 MachineInstr
*Def0
= MRI
->getVRegDef(Addr0
);
1924 MachineInstr
*Def1
= MRI
->getVRegDef(Addr1
);
1925 // Check if the loaded value, e.g. a constantpool of a global address, are
1927 if (!produceSameValue(*Def0
, *Def1
, MRI
))
1931 for (unsigned i
= 3, e
= MI0
.getNumOperands(); i
!= e
; ++i
) {
1932 // %12 = PICLDR %11, 0, 14, %noreg
1933 const MachineOperand
&MO0
= MI0
.getOperand(i
);
1934 const MachineOperand
&MO1
= MI1
.getOperand(i
);
1935 if (!MO0
.isIdenticalTo(MO1
))
1941 return MI0
.isIdenticalTo(MI1
, MachineInstr::IgnoreVRegDefs
);
1944 /// areLoadsFromSameBasePtr - This is used by the pre-regalloc scheduler to
1945 /// determine if two loads are loading from the same base address. It should
1946 /// only return true if the base pointers are the same and the only differences
1947 /// between the two addresses is the offset. It also returns the offsets by
1950 /// FIXME: remove this in favor of the MachineInstr interface once pre-RA-sched
1951 /// is permanently disabled.
1952 bool ARMBaseInstrInfo::areLoadsFromSameBasePtr(SDNode
*Load1
, SDNode
*Load2
,
1954 int64_t &Offset2
) const {
1955 // Don't worry about Thumb: just ARM and Thumb2.
1956 if (Subtarget
.isThumb1Only()) return false;
1958 if (!Load1
->isMachineOpcode() || !Load2
->isMachineOpcode())
1961 switch (Load1
->getMachineOpcode()) {
1975 case ARM::t2LDRSHi8
:
1977 case ARM::t2LDRBi12
:
1978 case ARM::t2LDRSHi12
:
1982 switch (Load2
->getMachineOpcode()) {
1995 case ARM::t2LDRSHi8
:
1997 case ARM::t2LDRBi12
:
1998 case ARM::t2LDRSHi12
:
2002 // Check if base addresses and chain operands match.
2003 if (Load1
->getOperand(0) != Load2
->getOperand(0) ||
2004 Load1
->getOperand(4) != Load2
->getOperand(4))
2007 // Index should be Reg0.
2008 if (Load1
->getOperand(3) != Load2
->getOperand(3))
2011 // Determine the offsets.
2012 if (isa
<ConstantSDNode
>(Load1
->getOperand(1)) &&
2013 isa
<ConstantSDNode
>(Load2
->getOperand(1))) {
2014 Offset1
= cast
<ConstantSDNode
>(Load1
->getOperand(1))->getSExtValue();
2015 Offset2
= cast
<ConstantSDNode
>(Load2
->getOperand(1))->getSExtValue();
2022 /// shouldScheduleLoadsNear - This is a used by the pre-regalloc scheduler to
2023 /// determine (in conjunction with areLoadsFromSameBasePtr) if two loads should
2024 /// be scheduled togther. On some targets if two loads are loading from
2025 /// addresses in the same cache line, it's better if they are scheduled
2026 /// together. This function takes two integers that represent the load offsets
2027 /// from the common base address. It returns true if it decides it's desirable
2028 /// to schedule the two loads together. "NumLoads" is the number of loads that
2029 /// have already been scheduled after Load1.
2031 /// FIXME: remove this in favor of the MachineInstr interface once pre-RA-sched
2032 /// is permanently disabled.
2033 bool ARMBaseInstrInfo::shouldScheduleLoadsNear(SDNode
*Load1
, SDNode
*Load2
,
2034 int64_t Offset1
, int64_t Offset2
,
2035 unsigned NumLoads
) const {
2036 // Don't worry about Thumb: just ARM and Thumb2.
2037 if (Subtarget
.isThumb1Only()) return false;
2039 assert(Offset2
> Offset1
);
2041 if ((Offset2
- Offset1
) / 8 > 64)
2044 // Check if the machine opcodes are different. If they are different
2045 // then we consider them to not be of the same base address,
2046 // EXCEPT in the case of Thumb2 byte loads where one is LDRBi8 and the other LDRBi12.
2047 // In this case, they are considered to be the same because they are different
2048 // encoding forms of the same basic instruction.
2049 if ((Load1
->getMachineOpcode() != Load2
->getMachineOpcode()) &&
2050 !((Load1
->getMachineOpcode() == ARM::t2LDRBi8
&&
2051 Load2
->getMachineOpcode() == ARM::t2LDRBi12
) ||
2052 (Load1
->getMachineOpcode() == ARM::t2LDRBi12
&&
2053 Load2
->getMachineOpcode() == ARM::t2LDRBi8
)))
2054 return false; // FIXME: overly conservative?
2056 // Four loads in a row should be sufficient.
2063 bool ARMBaseInstrInfo::isSchedulingBoundary(const MachineInstr
&MI
,
2064 const MachineBasicBlock
*MBB
,
2065 const MachineFunction
&MF
) const {
2066 // Debug info is never a scheduling boundary. It's necessary to be explicit
2067 // due to the special treatment of IT instructions below, otherwise a
2068 // dbg_value followed by an IT will result in the IT instruction being
2069 // considered a scheduling hazard, which is wrong. It should be the actual
2070 // instruction preceding the dbg_value instruction(s), just like it is
2071 // when debug info is not present.
2072 if (MI
.isDebugInstr())
2075 // Terminators and labels can't be scheduled around.
2076 if (MI
.isTerminator() || MI
.isPosition())
2079 // INLINEASM_BR can jump to another block
2080 if (MI
.getOpcode() == TargetOpcode::INLINEASM_BR
)
2083 // Treat the start of the IT block as a scheduling boundary, but schedule
2084 // t2IT along with all instructions following it.
2085 // FIXME: This is a big hammer. But the alternative is to add all potential
2086 // true and anti dependencies to IT block instructions as implicit operands
2087 // to the t2IT instruction. The added compile time and complexity does not
2089 MachineBasicBlock::const_iterator I
= MI
;
2090 // Make sure to skip any debug instructions
2091 while (++I
!= MBB
->end() && I
->isDebugInstr())
2093 if (I
!= MBB
->end() && I
->getOpcode() == ARM::t2IT
)
2096 // Don't attempt to schedule around any instruction that defines
2097 // a stack-oriented pointer, as it's unlikely to be profitable. This
2098 // saves compile time, because it doesn't require every single
2099 // stack slot reference to depend on the instruction that does the
2101 // Calls don't actually change the stack pointer, even if they have imp-defs.
2102 // No ARM calling conventions change the stack pointer. (X86 calling
2103 // conventions sometimes do).
2104 if (!MI
.isCall() && MI
.definesRegister(ARM::SP
))
2110 bool ARMBaseInstrInfo::
2111 isProfitableToIfCvt(MachineBasicBlock
&MBB
,
2112 unsigned NumCycles
, unsigned ExtraPredCycles
,
2113 BranchProbability Probability
) const {
2117 // If we are optimizing for size, see if the branch in the predecessor can be
2118 // lowered to cbn?z by the constant island lowering pass, and return false if
2119 // so. This results in a shorter instruction sequence.
2120 if (MBB
.getParent()->getFunction().hasOptSize()) {
2121 MachineBasicBlock
*Pred
= *MBB
.pred_begin();
2122 if (!Pred
->empty()) {
2123 MachineInstr
*LastMI
= &*Pred
->rbegin();
2124 if (LastMI
->getOpcode() == ARM::t2Bcc
) {
2125 const TargetRegisterInfo
*TRI
= &getRegisterInfo();
2126 MachineInstr
*CmpMI
= findCMPToFoldIntoCBZ(LastMI
, TRI
);
2132 return isProfitableToIfCvt(MBB
, NumCycles
, ExtraPredCycles
,
2133 MBB
, 0, 0, Probability
);
2136 bool ARMBaseInstrInfo::
2137 isProfitableToIfCvt(MachineBasicBlock
&TBB
,
2138 unsigned TCycles
, unsigned TExtra
,
2139 MachineBasicBlock
&FBB
,
2140 unsigned FCycles
, unsigned FExtra
,
2141 BranchProbability Probability
) const {
2145 // In thumb code we often end up trading one branch for a IT block, and
2146 // if we are cloning the instruction can increase code size. Prevent
2147 // blocks with multiple predecesors from being ifcvted to prevent this
2149 if (Subtarget
.isThumb2() && TBB
.getParent()->getFunction().hasMinSize()) {
2150 if (TBB
.pred_size() != 1 || FBB
.pred_size() != 1)
2154 // Attempt to estimate the relative costs of predication versus branching.
2155 // Here we scale up each component of UnpredCost to avoid precision issue when
2156 // scaling TCycles/FCycles by Probability.
2157 const unsigned ScalingUpFactor
= 1024;
2159 unsigned PredCost
= (TCycles
+ FCycles
+ TExtra
+ FExtra
) * ScalingUpFactor
;
2160 unsigned UnpredCost
;
2161 if (!Subtarget
.hasBranchPredictor()) {
2162 // When we don't have a branch predictor it's always cheaper to not take a
2163 // branch than take it, so we have to take that into account.
2164 unsigned NotTakenBranchCost
= 1;
2165 unsigned TakenBranchCost
= Subtarget
.getMispredictionPenalty();
2166 unsigned TUnpredCycles
, FUnpredCycles
;
2168 // Triangle: TBB is the fallthrough
2169 TUnpredCycles
= TCycles
+ NotTakenBranchCost
;
2170 FUnpredCycles
= TakenBranchCost
;
2172 // Diamond: TBB is the block that is branched to, FBB is the fallthrough
2173 TUnpredCycles
= TCycles
+ TakenBranchCost
;
2174 FUnpredCycles
= FCycles
+ NotTakenBranchCost
;
2175 // The branch at the end of FBB will disappear when it's predicated, so
2176 // discount it from PredCost.
2177 PredCost
-= 1 * ScalingUpFactor
;
2179 // The total cost is the cost of each path scaled by their probabilites
2180 unsigned TUnpredCost
= Probability
.scale(TUnpredCycles
* ScalingUpFactor
);
2181 unsigned FUnpredCost
= Probability
.getCompl().scale(FUnpredCycles
* ScalingUpFactor
);
2182 UnpredCost
= TUnpredCost
+ FUnpredCost
;
2183 // When predicating assume that the first IT can be folded away but later
2184 // ones cost one cycle each
2185 if (Subtarget
.isThumb2() && TCycles
+ FCycles
> 4) {
2186 PredCost
+= ((TCycles
+ FCycles
- 4) / 4) * ScalingUpFactor
;
2189 unsigned TUnpredCost
= Probability
.scale(TCycles
* ScalingUpFactor
);
2190 unsigned FUnpredCost
=
2191 Probability
.getCompl().scale(FCycles
* ScalingUpFactor
);
2192 UnpredCost
= TUnpredCost
+ FUnpredCost
;
2193 UnpredCost
+= 1 * ScalingUpFactor
; // The branch itself
2194 UnpredCost
+= Subtarget
.getMispredictionPenalty() * ScalingUpFactor
/ 10;
2197 return PredCost
<= UnpredCost
;
2201 ARMBaseInstrInfo::extraSizeToPredicateInstructions(const MachineFunction
&MF
,
2202 unsigned NumInsts
) const {
2203 // Thumb2 needs a 2-byte IT instruction to predicate up to 4 instructions.
2204 // ARM has a condition code field in every predicable instruction, using it
2205 // doesn't change code size.
2206 if (!Subtarget
.isThumb2())
2209 // It's possible that the size of the IT is restricted to a single block.
2210 unsigned MaxInsts
= Subtarget
.restrictIT() ? 1 : 4;
2211 return divideCeil(NumInsts
, MaxInsts
) * 2;
2215 ARMBaseInstrInfo::predictBranchSizeForIfCvt(MachineInstr
&MI
) const {
2216 // If this branch is likely to be folded into the comparison to form a
2217 // CB(N)Z, then removing it won't reduce code size at all, because that will
2218 // just replace the CB(N)Z with a CMP.
2219 if (MI
.getOpcode() == ARM::t2Bcc
&&
2220 findCMPToFoldIntoCBZ(&MI
, &getRegisterInfo()))
2223 unsigned Size
= getInstSizeInBytes(MI
);
2225 // For Thumb2, all branches are 32-bit instructions during the if conversion
2226 // pass, but may be replaced with 16-bit instructions during size reduction.
2227 // Since the branches considered by if conversion tend to be forward branches
2228 // over small basic blocks, they are very likely to be in range for the
2229 // narrow instructions, so we assume the final code size will be half what it
2231 if (Subtarget
.isThumb2())
2238 ARMBaseInstrInfo::isProfitableToUnpredicate(MachineBasicBlock
&TMBB
,
2239 MachineBasicBlock
&FMBB
) const {
2240 // Reduce false anti-dependencies to let the target's out-of-order execution
2241 // engine do its thing.
2242 return Subtarget
.isProfitableToUnpredicate();
2245 /// getInstrPredicate - If instruction is predicated, returns its predicate
2246 /// condition, otherwise returns AL. It also returns the condition code
2247 /// register by reference.
2248 ARMCC::CondCodes
llvm::getInstrPredicate(const MachineInstr
&MI
,
2249 Register
&PredReg
) {
2250 int PIdx
= MI
.findFirstPredOperandIdx();
2256 PredReg
= MI
.getOperand(PIdx
+1).getReg();
2257 return (ARMCC::CondCodes
)MI
.getOperand(PIdx
).getImm();
2260 unsigned llvm::getMatchingCondBranchOpcode(unsigned Opc
) {
2265 if (Opc
== ARM::t2B
)
2268 llvm_unreachable("Unknown unconditional branch opcode!");
2271 MachineInstr
*ARMBaseInstrInfo::commuteInstructionImpl(MachineInstr
&MI
,
2274 unsigned OpIdx2
) const {
2275 switch (MI
.getOpcode()) {
2277 case ARM::t2MOVCCr
: {
2278 // MOVCC can be commuted by inverting the condition.
2280 ARMCC::CondCodes CC
= getInstrPredicate(MI
, PredReg
);
2281 // MOVCC AL can't be inverted. Shouldn't happen.
2282 if (CC
== ARMCC::AL
|| PredReg
!= ARM::CPSR
)
2284 MachineInstr
*CommutedMI
=
2285 TargetInstrInfo::commuteInstructionImpl(MI
, NewMI
, OpIdx1
, OpIdx2
);
2288 // After swapping the MOVCC operands, also invert the condition.
2289 CommutedMI
->getOperand(CommutedMI
->findFirstPredOperandIdx())
2290 .setImm(ARMCC::getOppositeCondition(CC
));
2294 return TargetInstrInfo::commuteInstructionImpl(MI
, NewMI
, OpIdx1
, OpIdx2
);
2297 /// Identify instructions that can be folded into a MOVCC instruction, and
2298 /// return the defining instruction.
2300 ARMBaseInstrInfo::canFoldIntoMOVCC(Register Reg
, const MachineRegisterInfo
&MRI
,
2301 const TargetInstrInfo
*TII
) const {
2302 if (!Reg
.isVirtual())
2304 if (!MRI
.hasOneNonDBGUse(Reg
))
2306 MachineInstr
*MI
= MRI
.getVRegDef(Reg
);
2309 // Check if MI can be predicated and folded into the MOVCC.
2310 if (!isPredicable(*MI
))
2312 // Check if MI has any non-dead defs or physreg uses. This also detects
2313 // predicated instructions which will be reading CPSR.
2314 for (unsigned i
= 1, e
= MI
->getNumOperands(); i
!= e
; ++i
) {
2315 const MachineOperand
&MO
= MI
->getOperand(i
);
2316 // Reject frame index operands, PEI can't handle the predicated pseudos.
2317 if (MO
.isFI() || MO
.isCPI() || MO
.isJTI())
2321 // MI can't have any tied operands, that would conflict with predication.
2324 if (Register::isPhysicalRegister(MO
.getReg()))
2326 if (MO
.isDef() && !MO
.isDead())
2329 bool DontMoveAcrossStores
= true;
2330 if (!MI
->isSafeToMove(/* AliasAnalysis = */ nullptr, DontMoveAcrossStores
))
2335 bool ARMBaseInstrInfo::analyzeSelect(const MachineInstr
&MI
,
2336 SmallVectorImpl
<MachineOperand
> &Cond
,
2337 unsigned &TrueOp
, unsigned &FalseOp
,
2338 bool &Optimizable
) const {
2339 assert((MI
.getOpcode() == ARM::MOVCCr
|| MI
.getOpcode() == ARM::t2MOVCCr
) &&
2340 "Unknown select instruction");
2345 // 3: Condition code.
2349 Cond
.push_back(MI
.getOperand(3));
2350 Cond
.push_back(MI
.getOperand(4));
2351 // We can always fold a def.
2357 ARMBaseInstrInfo::optimizeSelect(MachineInstr
&MI
,
2358 SmallPtrSetImpl
<MachineInstr
*> &SeenMIs
,
2359 bool PreferFalse
) const {
2360 assert((MI
.getOpcode() == ARM::MOVCCr
|| MI
.getOpcode() == ARM::t2MOVCCr
) &&
2361 "Unknown select instruction");
2362 MachineRegisterInfo
&MRI
= MI
.getParent()->getParent()->getRegInfo();
2363 MachineInstr
*DefMI
= canFoldIntoMOVCC(MI
.getOperand(2).getReg(), MRI
, this);
2364 bool Invert
= !DefMI
;
2366 DefMI
= canFoldIntoMOVCC(MI
.getOperand(1).getReg(), MRI
, this);
2370 // Find new register class to use.
2371 MachineOperand FalseReg
= MI
.getOperand(Invert
? 2 : 1);
2372 Register DestReg
= MI
.getOperand(0).getReg();
2373 const TargetRegisterClass
*PreviousClass
= MRI
.getRegClass(FalseReg
.getReg());
2374 if (!MRI
.constrainRegClass(DestReg
, PreviousClass
))
2377 // Create a new predicated version of DefMI.
2378 // Rfalse is the first use.
2379 MachineInstrBuilder NewMI
=
2380 BuildMI(*MI
.getParent(), MI
, MI
.getDebugLoc(), DefMI
->getDesc(), DestReg
);
2382 // Copy all the DefMI operands, excluding its (null) predicate.
2383 const MCInstrDesc
&DefDesc
= DefMI
->getDesc();
2384 for (unsigned i
= 1, e
= DefDesc
.getNumOperands();
2385 i
!= e
&& !DefDesc
.OpInfo
[i
].isPredicate(); ++i
)
2386 NewMI
.add(DefMI
->getOperand(i
));
2388 unsigned CondCode
= MI
.getOperand(3).getImm();
2390 NewMI
.addImm(ARMCC::getOppositeCondition(ARMCC::CondCodes(CondCode
)));
2392 NewMI
.addImm(CondCode
);
2393 NewMI
.add(MI
.getOperand(4));
2395 // DefMI is not the -S version that sets CPSR, so add an optional %noreg.
2396 if (NewMI
->hasOptionalDef())
2397 NewMI
.add(condCodeOp());
2399 // The output register value when the predicate is false is an implicit
2400 // register operand tied to the first def.
2401 // The tie makes the register allocator ensure the FalseReg is allocated the
2402 // same register as operand 0.
2403 FalseReg
.setImplicit();
2404 NewMI
.add(FalseReg
);
2405 NewMI
->tieOperands(0, NewMI
->getNumOperands() - 1);
2407 // Update SeenMIs set: register newly created MI and erase removed DefMI.
2408 SeenMIs
.insert(NewMI
);
2409 SeenMIs
.erase(DefMI
);
2411 // If MI is inside a loop, and DefMI is outside the loop, then kill flags on
2412 // DefMI would be invalid when tranferred inside the loop. Checking for a
2413 // loop is expensive, but at least remove kill flags if they are in different
2415 if (DefMI
->getParent() != MI
.getParent())
2416 NewMI
->clearKillInfo();
2418 // The caller will erase MI, but not DefMI.
2419 DefMI
->eraseFromParent();
2423 /// Map pseudo instructions that imply an 'S' bit onto real opcodes. Whether the
2424 /// instruction is encoded with an 'S' bit is determined by the optional CPSR
2427 /// This will go away once we can teach tblgen how to set the optional CPSR def
2429 struct AddSubFlagsOpcodePair
{
2431 uint16_t MachineOpc
;
2434 static const AddSubFlagsOpcodePair AddSubFlagsOpcodeMap
[] = {
2435 {ARM::ADDSri
, ARM::ADDri
},
2436 {ARM::ADDSrr
, ARM::ADDrr
},
2437 {ARM::ADDSrsi
, ARM::ADDrsi
},
2438 {ARM::ADDSrsr
, ARM::ADDrsr
},
2440 {ARM::SUBSri
, ARM::SUBri
},
2441 {ARM::SUBSrr
, ARM::SUBrr
},
2442 {ARM::SUBSrsi
, ARM::SUBrsi
},
2443 {ARM::SUBSrsr
, ARM::SUBrsr
},
2445 {ARM::RSBSri
, ARM::RSBri
},
2446 {ARM::RSBSrsi
, ARM::RSBrsi
},
2447 {ARM::RSBSrsr
, ARM::RSBrsr
},
2449 {ARM::tADDSi3
, ARM::tADDi3
},
2450 {ARM::tADDSi8
, ARM::tADDi8
},
2451 {ARM::tADDSrr
, ARM::tADDrr
},
2452 {ARM::tADCS
, ARM::tADC
},
2454 {ARM::tSUBSi3
, ARM::tSUBi3
},
2455 {ARM::tSUBSi8
, ARM::tSUBi8
},
2456 {ARM::tSUBSrr
, ARM::tSUBrr
},
2457 {ARM::tSBCS
, ARM::tSBC
},
2458 {ARM::tRSBS
, ARM::tRSB
},
2459 {ARM::tLSLSri
, ARM::tLSLri
},
2461 {ARM::t2ADDSri
, ARM::t2ADDri
},
2462 {ARM::t2ADDSrr
, ARM::t2ADDrr
},
2463 {ARM::t2ADDSrs
, ARM::t2ADDrs
},
2465 {ARM::t2SUBSri
, ARM::t2SUBri
},
2466 {ARM::t2SUBSrr
, ARM::t2SUBrr
},
2467 {ARM::t2SUBSrs
, ARM::t2SUBrs
},
2469 {ARM::t2RSBSri
, ARM::t2RSBri
},
2470 {ARM::t2RSBSrs
, ARM::t2RSBrs
},
2473 unsigned llvm::convertAddSubFlagsOpcode(unsigned OldOpc
) {
2474 for (unsigned i
= 0, e
= array_lengthof(AddSubFlagsOpcodeMap
); i
!= e
; ++i
)
2475 if (OldOpc
== AddSubFlagsOpcodeMap
[i
].PseudoOpc
)
2476 return AddSubFlagsOpcodeMap
[i
].MachineOpc
;
2480 void llvm::emitARMRegPlusImmediate(MachineBasicBlock
&MBB
,
2481 MachineBasicBlock::iterator
&MBBI
,
2482 const DebugLoc
&dl
, Register DestReg
,
2483 Register BaseReg
, int NumBytes
,
2484 ARMCC::CondCodes Pred
, Register PredReg
,
2485 const ARMBaseInstrInfo
&TII
,
2487 if (NumBytes
== 0 && DestReg
!= BaseReg
) {
2488 BuildMI(MBB
, MBBI
, dl
, TII
.get(ARM::MOVr
), DestReg
)
2489 .addReg(BaseReg
, RegState::Kill
)
2490 .add(predOps(Pred
, PredReg
))
2492 .setMIFlags(MIFlags
);
2496 bool isSub
= NumBytes
< 0;
2497 if (isSub
) NumBytes
= -NumBytes
;
2500 unsigned RotAmt
= ARM_AM::getSOImmValRotate(NumBytes
);
2501 unsigned ThisVal
= NumBytes
& ARM_AM::rotr32(0xFF, RotAmt
);
2502 assert(ThisVal
&& "Didn't extract field correctly");
2504 // We will handle these bits from offset, clear them.
2505 NumBytes
&= ~ThisVal
;
2507 assert(ARM_AM::getSOImmVal(ThisVal
) != -1 && "Bit extraction didn't work?");
2509 // Build the new ADD / SUB.
2510 unsigned Opc
= isSub
? ARM::SUBri
: ARM::ADDri
;
2511 BuildMI(MBB
, MBBI
, dl
, TII
.get(Opc
), DestReg
)
2512 .addReg(BaseReg
, RegState::Kill
)
2514 .add(predOps(Pred
, PredReg
))
2516 .setMIFlags(MIFlags
);
2521 bool llvm::tryFoldSPUpdateIntoPushPop(const ARMSubtarget
&Subtarget
,
2522 MachineFunction
&MF
, MachineInstr
*MI
,
2523 unsigned NumBytes
) {
2524 // This optimisation potentially adds lots of load and store
2525 // micro-operations, it's only really a great benefit to code-size.
2526 if (!Subtarget
.hasMinSize())
2529 // If only one register is pushed/popped, LLVM can use an LDR/STR
2530 // instead. We can't modify those so make sure we're dealing with an
2531 // instruction we understand.
2532 bool IsPop
= isPopOpcode(MI
->getOpcode());
2533 bool IsPush
= isPushOpcode(MI
->getOpcode());
2534 if (!IsPush
&& !IsPop
)
2537 bool IsVFPPushPop
= MI
->getOpcode() == ARM::VSTMDDB_UPD
||
2538 MI
->getOpcode() == ARM::VLDMDIA_UPD
;
2539 bool IsT1PushPop
= MI
->getOpcode() == ARM::tPUSH
||
2540 MI
->getOpcode() == ARM::tPOP
||
2541 MI
->getOpcode() == ARM::tPOP_RET
;
2543 assert((IsT1PushPop
|| (MI
->getOperand(0).getReg() == ARM::SP
&&
2544 MI
->getOperand(1).getReg() == ARM::SP
)) &&
2545 "trying to fold sp update into non-sp-updating push/pop");
2547 // The VFP push & pop act on D-registers, so we can only fold an adjustment
2548 // by a multiple of 8 bytes in correctly. Similarly rN is 4-bytes. Don't try
2549 // if this is violated.
2550 if (NumBytes
% (IsVFPPushPop
? 8 : 4) != 0)
2553 // ARM and Thumb2 push/pop insts have explicit "sp, sp" operands (+
2554 // pred) so the list starts at 4. Thumb1 starts after the predicate.
2555 int RegListIdx
= IsT1PushPop
? 2 : 4;
2557 // Calculate the space we'll need in terms of registers.
2558 unsigned RegsNeeded
;
2559 const TargetRegisterClass
*RegClass
;
2561 RegsNeeded
= NumBytes
/ 8;
2562 RegClass
= &ARM::DPRRegClass
;
2564 RegsNeeded
= NumBytes
/ 4;
2565 RegClass
= &ARM::GPRRegClass
;
2568 // We're going to have to strip all list operands off before
2569 // re-adding them since the order matters, so save the existing ones
2571 SmallVector
<MachineOperand
, 4> RegList
;
2573 // We're also going to need the first register transferred by this
2574 // instruction, which won't necessarily be the first register in the list.
2575 unsigned FirstRegEnc
= -1;
2577 const TargetRegisterInfo
*TRI
= MF
.getRegInfo().getTargetRegisterInfo();
2578 for (int i
= MI
->getNumOperands() - 1; i
>= RegListIdx
; --i
) {
2579 MachineOperand
&MO
= MI
->getOperand(i
);
2580 RegList
.push_back(MO
);
2582 if (MO
.isReg() && !MO
.isImplicit() &&
2583 TRI
->getEncodingValue(MO
.getReg()) < FirstRegEnc
)
2584 FirstRegEnc
= TRI
->getEncodingValue(MO
.getReg());
2587 const MCPhysReg
*CSRegs
= TRI
->getCalleeSavedRegs(&MF
);
2589 // Now try to find enough space in the reglist to allocate NumBytes.
2590 for (int CurRegEnc
= FirstRegEnc
- 1; CurRegEnc
>= 0 && RegsNeeded
;
2592 unsigned CurReg
= RegClass
->getRegister(CurRegEnc
);
2593 if (IsT1PushPop
&& CurRegEnc
> TRI
->getEncodingValue(ARM::R7
))
2596 // Pushing any register is completely harmless, mark the register involved
2597 // as undef since we don't care about its value and must not restore it
2598 // during stack unwinding.
2599 RegList
.push_back(MachineOperand::CreateReg(CurReg
, false, false,
2600 false, false, true));
2605 // However, we can only pop an extra register if it's not live. For
2606 // registers live within the function we might clobber a return value
2607 // register; the other way a register can be live here is if it's
2609 if (isCalleeSavedRegister(CurReg
, CSRegs
) ||
2610 MI
->getParent()->computeRegisterLiveness(TRI
, CurReg
, MI
) !=
2611 MachineBasicBlock::LQR_Dead
) {
2612 // VFP pops don't allow holes in the register list, so any skip is fatal
2613 // for our transformation. GPR pops do, so we should just keep looking.
2620 // Mark the unimportant registers as <def,dead> in the POP.
2621 RegList
.push_back(MachineOperand::CreateReg(CurReg
, true, false, false,
2629 // Finally we know we can profitably perform the optimisation so go
2630 // ahead: strip all existing registers off and add them back again
2631 // in the right order.
2632 for (int i
= MI
->getNumOperands() - 1; i
>= RegListIdx
; --i
)
2633 MI
->RemoveOperand(i
);
2635 // Add the complete list back in.
2636 MachineInstrBuilder
MIB(MF
, &*MI
);
2637 for (int i
= RegList
.size() - 1; i
>= 0; --i
)
2638 MIB
.add(RegList
[i
]);
2643 bool llvm::rewriteARMFrameIndex(MachineInstr
&MI
, unsigned FrameRegIdx
,
2644 Register FrameReg
, int &Offset
,
2645 const ARMBaseInstrInfo
&TII
) {
2646 unsigned Opcode
= MI
.getOpcode();
2647 const MCInstrDesc
&Desc
= MI
.getDesc();
2648 unsigned AddrMode
= (Desc
.TSFlags
& ARMII::AddrModeMask
);
2651 // Memory operands in inline assembly always use AddrMode2.
2652 if (Opcode
== ARM::INLINEASM
|| Opcode
== ARM::INLINEASM_BR
)
2653 AddrMode
= ARMII::AddrMode2
;
2655 if (Opcode
== ARM::ADDri
) {
2656 Offset
+= MI
.getOperand(FrameRegIdx
+1).getImm();
2658 // Turn it into a move.
2659 MI
.setDesc(TII
.get(ARM::MOVr
));
2660 MI
.getOperand(FrameRegIdx
).ChangeToRegister(FrameReg
, false);
2661 MI
.RemoveOperand(FrameRegIdx
+1);
2664 } else if (Offset
< 0) {
2667 MI
.setDesc(TII
.get(ARM::SUBri
));
2670 // Common case: small offset, fits into instruction.
2671 if (ARM_AM::getSOImmVal(Offset
) != -1) {
2672 // Replace the FrameIndex with sp / fp
2673 MI
.getOperand(FrameRegIdx
).ChangeToRegister(FrameReg
, false);
2674 MI
.getOperand(FrameRegIdx
+1).ChangeToImmediate(Offset
);
2679 // Otherwise, pull as much of the immedidate into this ADDri/SUBri
2681 unsigned RotAmt
= ARM_AM::getSOImmValRotate(Offset
);
2682 unsigned ThisImmVal
= Offset
& ARM_AM::rotr32(0xFF, RotAmt
);
2684 // We will handle these bits from offset, clear them.
2685 Offset
&= ~ThisImmVal
;
2687 // Get the properly encoded SOImmVal field.
2688 assert(ARM_AM::getSOImmVal(ThisImmVal
) != -1 &&
2689 "Bit extraction didn't work?");
2690 MI
.getOperand(FrameRegIdx
+1).ChangeToImmediate(ThisImmVal
);
2692 unsigned ImmIdx
= 0;
2694 unsigned NumBits
= 0;
2697 case ARMII::AddrMode_i12
:
2698 ImmIdx
= FrameRegIdx
+ 1;
2699 InstrOffs
= MI
.getOperand(ImmIdx
).getImm();
2702 case ARMII::AddrMode2
:
2703 ImmIdx
= FrameRegIdx
+2;
2704 InstrOffs
= ARM_AM::getAM2Offset(MI
.getOperand(ImmIdx
).getImm());
2705 if (ARM_AM::getAM2Op(MI
.getOperand(ImmIdx
).getImm()) == ARM_AM::sub
)
2709 case ARMII::AddrMode3
:
2710 ImmIdx
= FrameRegIdx
+2;
2711 InstrOffs
= ARM_AM::getAM3Offset(MI
.getOperand(ImmIdx
).getImm());
2712 if (ARM_AM::getAM3Op(MI
.getOperand(ImmIdx
).getImm()) == ARM_AM::sub
)
2716 case ARMII::AddrMode4
:
2717 case ARMII::AddrMode6
:
2718 // Can't fold any offset even if it's zero.
2720 case ARMII::AddrMode5
:
2721 ImmIdx
= FrameRegIdx
+1;
2722 InstrOffs
= ARM_AM::getAM5Offset(MI
.getOperand(ImmIdx
).getImm());
2723 if (ARM_AM::getAM5Op(MI
.getOperand(ImmIdx
).getImm()) == ARM_AM::sub
)
2728 case ARMII::AddrMode5FP16
:
2729 ImmIdx
= FrameRegIdx
+1;
2730 InstrOffs
= ARM_AM::getAM5Offset(MI
.getOperand(ImmIdx
).getImm());
2731 if (ARM_AM::getAM5Op(MI
.getOperand(ImmIdx
).getImm()) == ARM_AM::sub
)
2736 case ARMII::AddrModeT2_i7
:
2737 case ARMII::AddrModeT2_i7s2
:
2738 case ARMII::AddrModeT2_i7s4
:
2739 ImmIdx
= FrameRegIdx
+1;
2740 InstrOffs
= MI
.getOperand(ImmIdx
).getImm();
2742 Scale
= (AddrMode
== ARMII::AddrModeT2_i7s2
? 2 :
2743 AddrMode
== ARMII::AddrModeT2_i7s4
? 4 : 1);
2746 llvm_unreachable("Unsupported addressing mode!");
2749 Offset
+= InstrOffs
* Scale
;
2750 assert((Offset
& (Scale
-1)) == 0 && "Can't encode this offset!");
2756 // Attempt to fold address comp. if opcode has offset bits
2758 // Common case: small offset, fits into instruction.
2759 MachineOperand
&ImmOp
= MI
.getOperand(ImmIdx
);
2760 int ImmedOffset
= Offset
/ Scale
;
2761 unsigned Mask
= (1 << NumBits
) - 1;
2762 if ((unsigned)Offset
<= Mask
* Scale
) {
2763 // Replace the FrameIndex with sp
2764 MI
.getOperand(FrameRegIdx
).ChangeToRegister(FrameReg
, false);
2765 // FIXME: When addrmode2 goes away, this will simplify (like the
2766 // T2 version), as the LDR.i12 versions don't need the encoding
2767 // tricks for the offset value.
2769 if (AddrMode
== ARMII::AddrMode_i12
)
2770 ImmedOffset
= -ImmedOffset
;
2772 ImmedOffset
|= 1 << NumBits
;
2774 ImmOp
.ChangeToImmediate(ImmedOffset
);
2779 // Otherwise, it didn't fit. Pull in what we can to simplify the immed.
2780 ImmedOffset
= ImmedOffset
& Mask
;
2782 if (AddrMode
== ARMII::AddrMode_i12
)
2783 ImmedOffset
= -ImmedOffset
;
2785 ImmedOffset
|= 1 << NumBits
;
2787 ImmOp
.ChangeToImmediate(ImmedOffset
);
2788 Offset
&= ~(Mask
*Scale
);
2792 Offset
= (isSub
) ? -Offset
: Offset
;
2796 /// analyzeCompare - For a comparison instruction, return the source registers
2797 /// in SrcReg and SrcReg2 if having two register operands, and the value it
2798 /// compares against in CmpValue. Return true if the comparison instruction
2799 /// can be analyzed.
2800 bool ARMBaseInstrInfo::analyzeCompare(const MachineInstr
&MI
, Register
&SrcReg
,
2801 Register
&SrcReg2
, int &CmpMask
,
2802 int &CmpValue
) const {
2803 switch (MI
.getOpcode()) {
2808 SrcReg
= MI
.getOperand(0).getReg();
2811 CmpValue
= MI
.getOperand(1).getImm();
2816 SrcReg
= MI
.getOperand(0).getReg();
2817 SrcReg2
= MI
.getOperand(1).getReg();
2823 SrcReg
= MI
.getOperand(0).getReg();
2825 CmpMask
= MI
.getOperand(1).getImm();
2833 /// isSuitableForMask - Identify a suitable 'and' instruction that
2834 /// operates on the given source register and applies the same mask
2835 /// as a 'tst' instruction. Provide a limited look-through for copies.
2836 /// When successful, MI will hold the found instruction.
2837 static bool isSuitableForMask(MachineInstr
*&MI
, Register SrcReg
,
2838 int CmpMask
, bool CommonUse
) {
2839 switch (MI
->getOpcode()) {
2842 if (CmpMask
!= MI
->getOperand(2).getImm())
2844 if (SrcReg
== MI
->getOperand(CommonUse
? 1 : 0).getReg())
2852 /// getCmpToAddCondition - assume the flags are set by CMP(a,b), return
2853 /// the condition code if we modify the instructions such that flags are
2854 /// set by ADD(a,b,X).
2855 inline static ARMCC::CondCodes
getCmpToAddCondition(ARMCC::CondCodes CC
) {
2857 default: return ARMCC::AL
;
2858 case ARMCC::HS
: return ARMCC::LO
;
2859 case ARMCC::LO
: return ARMCC::HS
;
2860 case ARMCC::VS
: return ARMCC::VS
;
2861 case ARMCC::VC
: return ARMCC::VC
;
2865 /// isRedundantFlagInstr - check whether the first instruction, whose only
2866 /// purpose is to update flags, can be made redundant.
2867 /// CMPrr can be made redundant by SUBrr if the operands are the same.
2868 /// CMPri can be made redundant by SUBri if the operands are the same.
2869 /// CMPrr(r0, r1) can be made redundant by ADDr[ri](r0, r1, X).
2870 /// This function can be extended later on.
2871 inline static bool isRedundantFlagInstr(const MachineInstr
*CmpI
,
2872 Register SrcReg
, Register SrcReg2
,
2873 int ImmValue
, const MachineInstr
*OI
,
2875 if ((CmpI
->getOpcode() == ARM::CMPrr
|| CmpI
->getOpcode() == ARM::t2CMPrr
) &&
2876 (OI
->getOpcode() == ARM::SUBrr
|| OI
->getOpcode() == ARM::t2SUBrr
) &&
2877 ((OI
->getOperand(1).getReg() == SrcReg
&&
2878 OI
->getOperand(2).getReg() == SrcReg2
) ||
2879 (OI
->getOperand(1).getReg() == SrcReg2
&&
2880 OI
->getOperand(2).getReg() == SrcReg
))) {
2885 if (CmpI
->getOpcode() == ARM::tCMPr
&& OI
->getOpcode() == ARM::tSUBrr
&&
2886 ((OI
->getOperand(2).getReg() == SrcReg
&&
2887 OI
->getOperand(3).getReg() == SrcReg2
) ||
2888 (OI
->getOperand(2).getReg() == SrcReg2
&&
2889 OI
->getOperand(3).getReg() == SrcReg
))) {
2894 if ((CmpI
->getOpcode() == ARM::CMPri
|| CmpI
->getOpcode() == ARM::t2CMPri
) &&
2895 (OI
->getOpcode() == ARM::SUBri
|| OI
->getOpcode() == ARM::t2SUBri
) &&
2896 OI
->getOperand(1).getReg() == SrcReg
&&
2897 OI
->getOperand(2).getImm() == ImmValue
) {
2902 if (CmpI
->getOpcode() == ARM::tCMPi8
&&
2903 (OI
->getOpcode() == ARM::tSUBi8
|| OI
->getOpcode() == ARM::tSUBi3
) &&
2904 OI
->getOperand(2).getReg() == SrcReg
&&
2905 OI
->getOperand(3).getImm() == ImmValue
) {
2910 if ((CmpI
->getOpcode() == ARM::CMPrr
|| CmpI
->getOpcode() == ARM::t2CMPrr
) &&
2911 (OI
->getOpcode() == ARM::ADDrr
|| OI
->getOpcode() == ARM::t2ADDrr
||
2912 OI
->getOpcode() == ARM::ADDri
|| OI
->getOpcode() == ARM::t2ADDri
) &&
2913 OI
->getOperand(0).isReg() && OI
->getOperand(1).isReg() &&
2914 OI
->getOperand(0).getReg() == SrcReg
&&
2915 OI
->getOperand(1).getReg() == SrcReg2
) {
2920 if (CmpI
->getOpcode() == ARM::tCMPr
&&
2921 (OI
->getOpcode() == ARM::tADDi3
|| OI
->getOpcode() == ARM::tADDi8
||
2922 OI
->getOpcode() == ARM::tADDrr
) &&
2923 OI
->getOperand(0).getReg() == SrcReg
&&
2924 OI
->getOperand(2).getReg() == SrcReg2
) {
2932 static bool isOptimizeCompareCandidate(MachineInstr
*MI
, bool &IsThumb1
) {
2933 switch (MI
->getOpcode()) {
2934 default: return false;
3000 /// optimizeCompareInstr - Convert the instruction supplying the argument to the
3001 /// comparison into one that sets the zero bit in the flags register;
3002 /// Remove a redundant Compare instruction if an earlier instruction can set the
3003 /// flags in the same way as Compare.
3004 /// E.g. SUBrr(r1,r2) and CMPrr(r1,r2). We also handle the case where two
3005 /// operands are swapped: SUBrr(r1,r2) and CMPrr(r2,r1), by updating the
3006 /// condition code of instructions which use the flags.
3007 bool ARMBaseInstrInfo::optimizeCompareInstr(
3008 MachineInstr
&CmpInstr
, Register SrcReg
, Register SrcReg2
, int CmpMask
,
3009 int CmpValue
, const MachineRegisterInfo
*MRI
) const {
3010 // Get the unique definition of SrcReg.
3011 MachineInstr
*MI
= MRI
->getUniqueVRegDef(SrcReg
);
3012 if (!MI
) return false;
3014 // Masked compares sometimes use the same register as the corresponding 'and'.
3015 if (CmpMask
!= ~0) {
3016 if (!isSuitableForMask(MI
, SrcReg
, CmpMask
, false) || isPredicated(*MI
)) {
3018 for (MachineRegisterInfo::use_instr_iterator
3019 UI
= MRI
->use_instr_begin(SrcReg
), UE
= MRI
->use_instr_end();
3021 if (UI
->getParent() != CmpInstr
.getParent())
3023 MachineInstr
*PotentialAND
= &*UI
;
3024 if (!isSuitableForMask(PotentialAND
, SrcReg
, CmpMask
, true) ||
3025 isPredicated(*PotentialAND
))
3030 if (!MI
) return false;
3034 // Get ready to iterate backward from CmpInstr.
3035 MachineBasicBlock::iterator I
= CmpInstr
, E
= MI
,
3036 B
= CmpInstr
.getParent()->begin();
3038 // Early exit if CmpInstr is at the beginning of the BB.
3039 if (I
== B
) return false;
3041 // There are two possible candidates which can be changed to set CPSR:
3042 // One is MI, the other is a SUB or ADD instruction.
3043 // For CMPrr(r1,r2), we are looking for SUB(r1,r2), SUB(r2,r1), or
3044 // ADDr[ri](r1, r2, X).
3045 // For CMPri(r1, CmpValue), we are looking for SUBri(r1, CmpValue).
3046 MachineInstr
*SubAdd
= nullptr;
3048 // MI is not a candidate for CMPrr.
3050 else if (MI
->getParent() != CmpInstr
.getParent() || CmpValue
!= 0) {
3051 // Conservatively refuse to convert an instruction which isn't in the same
3052 // BB as the comparison.
3053 // For CMPri w/ CmpValue != 0, a SubAdd may still be a candidate.
3054 // Thus we cannot return here.
3055 if (CmpInstr
.getOpcode() == ARM::CMPri
||
3056 CmpInstr
.getOpcode() == ARM::t2CMPri
||
3057 CmpInstr
.getOpcode() == ARM::tCMPi8
)
3063 bool IsThumb1
= false;
3064 if (MI
&& !isOptimizeCompareCandidate(MI
, IsThumb1
))
3067 // We also want to do this peephole for cases like this: if (a*b == 0),
3068 // and optimise away the CMP instruction from the generated code sequence:
3069 // MULS, MOVS, MOVS, CMP. Here the MOVS instructions load the boolean values
3070 // resulting from the select instruction, but these MOVS instructions for
3071 // Thumb1 (V6M) are flag setting and are thus preventing this optimisation.
3072 // However, if we only have MOVS instructions in between the CMP and the
3073 // other instruction (the MULS in this example), then the CPSR is dead so we
3074 // can safely reorder the sequence into: MOVS, MOVS, MULS, CMP. We do this
3075 // reordering and then continue the analysis hoping we can eliminate the
3076 // CMP. This peephole works on the vregs, so is still in SSA form. As a
3077 // consequence, the movs won't redefine/kill the MUL operands which would
3078 // make this reordering illegal.
3079 const TargetRegisterInfo
*TRI
= &getRegisterInfo();
3080 if (MI
&& IsThumb1
) {
3082 if (I
!= E
&& !MI
->readsRegister(ARM::CPSR
, TRI
)) {
3083 bool CanReorder
= true;
3084 for (; I
!= E
; --I
) {
3085 if (I
->getOpcode() != ARM::tMOVi8
) {
3091 MI
= MI
->removeFromParent();
3093 CmpInstr
.getParent()->insert(E
, MI
);
3100 // Check that CPSR isn't set between the comparison instruction and the one we
3101 // want to change. At the same time, search for SubAdd.
3102 bool SubAddIsThumb1
= false;
3104 const MachineInstr
&Instr
= *--I
;
3106 // Check whether CmpInstr can be made redundant by the current instruction.
3107 if (isRedundantFlagInstr(&CmpInstr
, SrcReg
, SrcReg2
, CmpValue
, &Instr
,
3113 // Allow E (which was initially MI) to be SubAdd but do not search before E.
3117 if (Instr
.modifiesRegister(ARM::CPSR
, TRI
) ||
3118 Instr
.readsRegister(ARM::CPSR
, TRI
))
3119 // This instruction modifies or uses CPSR after the one we want to
3120 // change. We can't do this transformation.
3124 // In some cases, we scan the use-list of an instruction for an AND;
3125 // that AND is in the same BB, but may not be scheduled before the
3126 // corresponding TST. In that case, bail out.
3128 // FIXME: We could try to reschedule the AND.
3133 // Return false if no candidates exist.
3137 // If we found a SubAdd, use it as it will be closer to the CMP
3140 IsThumb1
= SubAddIsThumb1
;
3143 // We can't use a predicated instruction - it doesn't always write the flags.
3144 if (isPredicated(*MI
))
3147 // Scan forward for the use of CPSR
3148 // When checking against MI: if it's a conditional code that requires
3149 // checking of the V bit or C bit, then this is not safe to do.
3150 // It is safe to remove CmpInstr if CPSR is redefined or killed.
3151 // If we are done with the basic block, we need to check whether CPSR is
3153 SmallVector
<std::pair
<MachineOperand
*, ARMCC::CondCodes
>, 4>
3155 bool isSafe
= false;
3157 E
= CmpInstr
.getParent()->end();
3158 while (!isSafe
&& ++I
!= E
) {
3159 const MachineInstr
&Instr
= *I
;
3160 for (unsigned IO
= 0, EO
= Instr
.getNumOperands();
3161 !isSafe
&& IO
!= EO
; ++IO
) {
3162 const MachineOperand
&MO
= Instr
.getOperand(IO
);
3163 if (MO
.isRegMask() && MO
.clobbersPhysReg(ARM::CPSR
)) {
3167 if (!MO
.isReg() || MO
.getReg() != ARM::CPSR
)
3173 // Condition code is after the operand before CPSR except for VSELs.
3174 ARMCC::CondCodes CC
;
3175 bool IsInstrVSel
= true;
3176 switch (Instr
.getOpcode()) {
3178 IsInstrVSel
= false;
3179 CC
= (ARMCC::CondCodes
)Instr
.getOperand(IO
- 1).getImm();
3204 // If we have SUB(r1, r2) and CMP(r2, r1), the condition code based
3205 // on CMP needs to be updated to be based on SUB.
3206 // If we have ADD(r1, r2, X) and CMP(r1, r2), the condition code also
3207 // needs to be modified.
3208 // Push the condition code operands to OperandsToUpdate.
3209 // If it is safe to remove CmpInstr, the condition code of these
3210 // operands will be modified.
3211 unsigned Opc
= SubAdd
->getOpcode();
3212 bool IsSub
= Opc
== ARM::SUBrr
|| Opc
== ARM::t2SUBrr
||
3213 Opc
== ARM::SUBri
|| Opc
== ARM::t2SUBri
||
3214 Opc
== ARM::tSUBrr
|| Opc
== ARM::tSUBi3
||
3216 unsigned OpI
= Opc
!= ARM::tSUBrr
? 1 : 2;
3218 (SrcReg2
!= 0 && SubAdd
->getOperand(OpI
).getReg() == SrcReg2
&&
3219 SubAdd
->getOperand(OpI
+ 1).getReg() == SrcReg
)) {
3220 // VSel doesn't support condition code update.
3223 // Ensure we can swap the condition.
3224 ARMCC::CondCodes NewCC
= (IsSub
? getSwappedCondition(CC
) : getCmpToAddCondition(CC
));
3225 if (NewCC
== ARMCC::AL
)
3227 OperandsToUpdate
.push_back(
3228 std::make_pair(&((*I
).getOperand(IO
- 1)), NewCC
));
3231 // No SubAdd, so this is x = <op> y, z; cmp x, 0.
3233 case ARMCC::EQ
: // Z
3234 case ARMCC::NE
: // Z
3235 case ARMCC::MI
: // N
3236 case ARMCC::PL
: // N
3237 case ARMCC::AL
: // none
3238 // CPSR can be used multiple times, we should continue.
3240 case ARMCC::HS
: // C
3241 case ARMCC::LO
: // C
3242 case ARMCC::VS
: // V
3243 case ARMCC::VC
: // V
3244 case ARMCC::HI
: // C Z
3245 case ARMCC::LS
: // C Z
3246 case ARMCC::GE
: // N V
3247 case ARMCC::LT
: // N V
3248 case ARMCC::GT
: // Z N V
3249 case ARMCC::LE
: // Z N V
3250 // The instruction uses the V bit or C bit which is not safe.
3257 // If CPSR is not killed nor re-defined, we should check whether it is
3258 // live-out. If it is live-out, do not optimize.
3260 MachineBasicBlock
*MBB
= CmpInstr
.getParent();
3261 for (MachineBasicBlock::succ_iterator SI
= MBB
->succ_begin(),
3262 SE
= MBB
->succ_end(); SI
!= SE
; ++SI
)
3263 if ((*SI
)->isLiveIn(ARM::CPSR
))
3267 // Toggle the optional operand to CPSR (if it exists - in Thumb1 we always
3268 // set CPSR so this is represented as an explicit output)
3270 MI
->getOperand(5).setReg(ARM::CPSR
);
3271 MI
->getOperand(5).setIsDef(true);
3273 assert(!isPredicated(*MI
) && "Can't use flags from predicated instruction");
3274 CmpInstr
.eraseFromParent();
3276 // Modify the condition code of operands in OperandsToUpdate.
3277 // Since we have SUB(r1, r2) and CMP(r2, r1), the condition code needs to
3278 // be changed from r2 > r1 to r1 < r2, from r2 < r1 to r1 > r2, etc.
3279 for (unsigned i
= 0, e
= OperandsToUpdate
.size(); i
< e
; i
++)
3280 OperandsToUpdate
[i
].first
->setImm(OperandsToUpdate
[i
].second
);
3282 MI
->clearRegisterDeads(ARM::CPSR
);
3287 bool ARMBaseInstrInfo::shouldSink(const MachineInstr
&MI
) const {
3288 // Do not sink MI if it might be used to optimize a redundant compare.
3289 // We heuristically only look at the instruction immediately following MI to
3290 // avoid potentially searching the entire basic block.
3291 if (isPredicated(MI
))
3293 MachineBasicBlock::const_iterator Next
= &MI
;
3295 Register SrcReg
, SrcReg2
;
3296 int CmpMask
, CmpValue
;
3298 if (Next
!= MI
.getParent()->end() &&
3299 analyzeCompare(*Next
, SrcReg
, SrcReg2
, CmpMask
, CmpValue
) &&
3300 isRedundantFlagInstr(&*Next
, SrcReg
, SrcReg2
, CmpValue
, &MI
, IsThumb1
))
3305 bool ARMBaseInstrInfo::FoldImmediate(MachineInstr
&UseMI
, MachineInstr
&DefMI
,
3307 MachineRegisterInfo
*MRI
) const {
3308 // Fold large immediates into add, sub, or, xor.
3309 unsigned DefOpc
= DefMI
.getOpcode();
3310 if (DefOpc
!= ARM::t2MOVi32imm
&& DefOpc
!= ARM::MOVi32imm
)
3312 if (!DefMI
.getOperand(1).isImm())
3313 // Could be t2MOVi32imm @xx
3316 if (!MRI
->hasOneNonDBGUse(Reg
))
3319 const MCInstrDesc
&DefMCID
= DefMI
.getDesc();
3320 if (DefMCID
.hasOptionalDef()) {
3321 unsigned NumOps
= DefMCID
.getNumOperands();
3322 const MachineOperand
&MO
= DefMI
.getOperand(NumOps
- 1);
3323 if (MO
.getReg() == ARM::CPSR
&& !MO
.isDead())
3324 // If DefMI defines CPSR and it is not dead, it's obviously not safe
3329 const MCInstrDesc
&UseMCID
= UseMI
.getDesc();
3330 if (UseMCID
.hasOptionalDef()) {
3331 unsigned NumOps
= UseMCID
.getNumOperands();
3332 if (UseMI
.getOperand(NumOps
- 1).getReg() == ARM::CPSR
)
3333 // If the instruction sets the flag, do not attempt this optimization
3334 // since it may change the semantics of the code.
3338 unsigned UseOpc
= UseMI
.getOpcode();
3339 unsigned NewUseOpc
= 0;
3340 uint32_t ImmVal
= (uint32_t)DefMI
.getOperand(1).getImm();
3341 uint32_t SOImmValV1
= 0, SOImmValV2
= 0;
3342 bool Commute
= false;
3344 default: return false;
3352 case ARM::t2EORrr
: {
3353 Commute
= UseMI
.getOperand(2).getReg() != Reg
;
3358 if (UseOpc
== ARM::SUBrr
&& Commute
)
3361 // ADD/SUB are special because they're essentially the same operation, so
3362 // we can handle a larger range of immediates.
3363 if (ARM_AM::isSOImmTwoPartVal(ImmVal
))
3364 NewUseOpc
= UseOpc
== ARM::ADDrr
? ARM::ADDri
: ARM::SUBri
;
3365 else if (ARM_AM::isSOImmTwoPartVal(-ImmVal
)) {
3367 NewUseOpc
= UseOpc
== ARM::ADDrr
? ARM::SUBri
: ARM::ADDri
;
3370 SOImmValV1
= (uint32_t)ARM_AM::getSOImmTwoPartFirst(ImmVal
);
3371 SOImmValV2
= (uint32_t)ARM_AM::getSOImmTwoPartSecond(ImmVal
);
3375 if (!ARM_AM::isSOImmTwoPartVal(ImmVal
))
3377 SOImmValV1
= (uint32_t)ARM_AM::getSOImmTwoPartFirst(ImmVal
);
3378 SOImmValV2
= (uint32_t)ARM_AM::getSOImmTwoPartSecond(ImmVal
);
3381 case ARM::ORRrr
: NewUseOpc
= ARM::ORRri
; break;
3382 case ARM::EORrr
: NewUseOpc
= ARM::EORri
; break;
3386 case ARM::t2SUBrr
: {
3387 if (UseOpc
== ARM::t2SUBrr
&& Commute
)
3390 // ADD/SUB are special because they're essentially the same operation, so
3391 // we can handle a larger range of immediates.
3392 const bool ToSP
= DefMI
.getOperand(0).getReg() == ARM::SP
;
3393 const unsigned t2ADD
= ToSP
? ARM::t2ADDspImm
: ARM::t2ADDri
;
3394 const unsigned t2SUB
= ToSP
? ARM::t2SUBspImm
: ARM::t2SUBri
;
3395 if (ARM_AM::isT2SOImmTwoPartVal(ImmVal
))
3396 NewUseOpc
= UseOpc
== ARM::t2ADDrr
? t2ADD
: t2SUB
;
3397 else if (ARM_AM::isT2SOImmTwoPartVal(-ImmVal
)) {
3399 NewUseOpc
= UseOpc
== ARM::t2ADDrr
? t2SUB
: t2ADD
;
3402 SOImmValV1
= (uint32_t)ARM_AM::getT2SOImmTwoPartFirst(ImmVal
);
3403 SOImmValV2
= (uint32_t)ARM_AM::getT2SOImmTwoPartSecond(ImmVal
);
3408 if (!ARM_AM::isT2SOImmTwoPartVal(ImmVal
))
3410 SOImmValV1
= (uint32_t)ARM_AM::getT2SOImmTwoPartFirst(ImmVal
);
3411 SOImmValV2
= (uint32_t)ARM_AM::getT2SOImmTwoPartSecond(ImmVal
);
3414 case ARM::t2ORRrr
: NewUseOpc
= ARM::t2ORRri
; break;
3415 case ARM::t2EORrr
: NewUseOpc
= ARM::t2EORri
; break;
3422 unsigned OpIdx
= Commute
? 2 : 1;
3423 Register Reg1
= UseMI
.getOperand(OpIdx
).getReg();
3424 bool isKill
= UseMI
.getOperand(OpIdx
).isKill();
3425 const TargetRegisterClass
*TRC
= MRI
->getRegClass(Reg
);
3426 Register NewReg
= MRI
->createVirtualRegister(TRC
);
3427 BuildMI(*UseMI
.getParent(), UseMI
, UseMI
.getDebugLoc(), get(NewUseOpc
),
3429 .addReg(Reg1
, getKillRegState(isKill
))
3431 .add(predOps(ARMCC::AL
))
3433 UseMI
.setDesc(get(NewUseOpc
));
3434 UseMI
.getOperand(1).setReg(NewReg
);
3435 UseMI
.getOperand(1).setIsKill();
3436 UseMI
.getOperand(2).ChangeToImmediate(SOImmValV2
);
3437 DefMI
.eraseFromParent();
3438 // FIXME: t2ADDrr should be split, as different rulles apply when writing to SP.
3439 // Just as t2ADDri, that was split to [t2ADDri, t2ADDspImm].
3440 // Then the below code will not be needed, as the input/output register
3441 // classes will be rgpr or gprSP.
3442 // For now, we fix the UseMI operand explicitly here:
3444 case ARM::t2ADDspImm
:
3445 case ARM::t2SUBspImm
:
3448 MRI
->constrainRegClass(UseMI
.getOperand(0).getReg(), TRC
);
3453 static unsigned getNumMicroOpsSwiftLdSt(const InstrItineraryData
*ItinData
,
3454 const MachineInstr
&MI
) {
3455 switch (MI
.getOpcode()) {
3457 const MCInstrDesc
&Desc
= MI
.getDesc();
3458 int UOps
= ItinData
->getNumMicroOps(Desc
.getSchedClass());
3459 assert(UOps
>= 0 && "bad # UOps");
3467 unsigned ShOpVal
= MI
.getOperand(3).getImm();
3468 bool isSub
= ARM_AM::getAM2Op(ShOpVal
) == ARM_AM::sub
;
3469 unsigned ShImm
= ARM_AM::getAM2Offset(ShOpVal
);
3472 ((ShImm
== 1 || ShImm
== 2 || ShImm
== 3) &&
3473 ARM_AM::getAM2ShiftOpc(ShOpVal
) == ARM_AM::lsl
)))
3480 if (!MI
.getOperand(2).getReg())
3483 unsigned ShOpVal
= MI
.getOperand(3).getImm();
3484 bool isSub
= ARM_AM::getAM2Op(ShOpVal
) == ARM_AM::sub
;
3485 unsigned ShImm
= ARM_AM::getAM2Offset(ShOpVal
);
3488 ((ShImm
== 1 || ShImm
== 2 || ShImm
== 3) &&
3489 ARM_AM::getAM2ShiftOpc(ShOpVal
) == ARM_AM::lsl
)))
3496 return (ARM_AM::getAM3Op(MI
.getOperand(3).getImm()) == ARM_AM::sub
) ? 3 : 2;
3498 case ARM::LDRSB_POST
:
3499 case ARM::LDRSH_POST
: {
3500 Register Rt
= MI
.getOperand(0).getReg();
3501 Register Rm
= MI
.getOperand(3).getReg();
3502 return (Rt
== Rm
) ? 4 : 3;
3505 case ARM::LDR_PRE_REG
:
3506 case ARM::LDRB_PRE_REG
: {
3507 Register Rt
= MI
.getOperand(0).getReg();
3508 Register Rm
= MI
.getOperand(3).getReg();
3511 unsigned ShOpVal
= MI
.getOperand(4).getImm();
3512 bool isSub
= ARM_AM::getAM2Op(ShOpVal
) == ARM_AM::sub
;
3513 unsigned ShImm
= ARM_AM::getAM2Offset(ShOpVal
);
3516 ((ShImm
== 1 || ShImm
== 2 || ShImm
== 3) &&
3517 ARM_AM::getAM2ShiftOpc(ShOpVal
) == ARM_AM::lsl
)))
3522 case ARM::STR_PRE_REG
:
3523 case ARM::STRB_PRE_REG
: {
3524 unsigned ShOpVal
= MI
.getOperand(4).getImm();
3525 bool isSub
= ARM_AM::getAM2Op(ShOpVal
) == ARM_AM::sub
;
3526 unsigned ShImm
= ARM_AM::getAM2Offset(ShOpVal
);
3529 ((ShImm
== 1 || ShImm
== 2 || ShImm
== 3) &&
3530 ARM_AM::getAM2ShiftOpc(ShOpVal
) == ARM_AM::lsl
)))
3536 case ARM::STRH_PRE
: {
3537 Register Rt
= MI
.getOperand(0).getReg();
3538 Register Rm
= MI
.getOperand(3).getReg();
3543 return (ARM_AM::getAM3Op(MI
.getOperand(4).getImm()) == ARM_AM::sub
) ? 3 : 2;
3546 case ARM::LDR_POST_REG
:
3547 case ARM::LDRB_POST_REG
:
3548 case ARM::LDRH_POST
: {
3549 Register Rt
= MI
.getOperand(0).getReg();
3550 Register Rm
= MI
.getOperand(3).getReg();
3551 return (Rt
== Rm
) ? 3 : 2;
3554 case ARM::LDR_PRE_IMM
:
3555 case ARM::LDRB_PRE_IMM
:
3556 case ARM::LDR_POST_IMM
:
3557 case ARM::LDRB_POST_IMM
:
3558 case ARM::STRB_POST_IMM
:
3559 case ARM::STRB_POST_REG
:
3560 case ARM::STRB_PRE_IMM
:
3561 case ARM::STRH_POST
:
3562 case ARM::STR_POST_IMM
:
3563 case ARM::STR_POST_REG
:
3564 case ARM::STR_PRE_IMM
:
3567 case ARM::LDRSB_PRE
:
3568 case ARM::LDRSH_PRE
: {
3569 Register Rm
= MI
.getOperand(3).getReg();
3572 Register Rt
= MI
.getOperand(0).getReg();
3575 unsigned ShOpVal
= MI
.getOperand(4).getImm();
3576 bool isSub
= ARM_AM::getAM2Op(ShOpVal
) == ARM_AM::sub
;
3577 unsigned ShImm
= ARM_AM::getAM2Offset(ShOpVal
);
3580 ((ShImm
== 1 || ShImm
== 2 || ShImm
== 3) &&
3581 ARM_AM::getAM2ShiftOpc(ShOpVal
) == ARM_AM::lsl
)))
3587 Register Rt
= MI
.getOperand(0).getReg();
3588 Register Rn
= MI
.getOperand(2).getReg();
3589 Register Rm
= MI
.getOperand(3).getReg();
3591 return (ARM_AM::getAM3Op(MI
.getOperand(4).getImm()) == ARM_AM::sub
) ? 4
3593 return (Rt
== Rn
) ? 3 : 2;
3597 Register Rm
= MI
.getOperand(3).getReg();
3599 return (ARM_AM::getAM3Op(MI
.getOperand(4).getImm()) == ARM_AM::sub
) ? 4
3604 case ARM::LDRD_POST
:
3605 case ARM::t2LDRD_POST
:
3608 case ARM::STRD_POST
:
3609 case ARM::t2STRD_POST
:
3612 case ARM::LDRD_PRE
: {
3613 Register Rt
= MI
.getOperand(0).getReg();
3614 Register Rn
= MI
.getOperand(3).getReg();
3615 Register Rm
= MI
.getOperand(4).getReg();
3617 return (ARM_AM::getAM3Op(MI
.getOperand(5).getImm()) == ARM_AM::sub
) ? 5
3619 return (Rt
== Rn
) ? 4 : 3;
3622 case ARM::t2LDRD_PRE
: {
3623 Register Rt
= MI
.getOperand(0).getReg();
3624 Register Rn
= MI
.getOperand(3).getReg();
3625 return (Rt
== Rn
) ? 4 : 3;
3628 case ARM::STRD_PRE
: {
3629 Register Rm
= MI
.getOperand(4).getReg();
3631 return (ARM_AM::getAM3Op(MI
.getOperand(5).getImm()) == ARM_AM::sub
) ? 5
3636 case ARM::t2STRD_PRE
:
3639 case ARM::t2LDR_POST
:
3640 case ARM::t2LDRB_POST
:
3641 case ARM::t2LDRB_PRE
:
3642 case ARM::t2LDRSBi12
:
3643 case ARM::t2LDRSBi8
:
3644 case ARM::t2LDRSBpci
:
3646 case ARM::t2LDRH_POST
:
3647 case ARM::t2LDRH_PRE
:
3649 case ARM::t2LDRSB_POST
:
3650 case ARM::t2LDRSB_PRE
:
3651 case ARM::t2LDRSH_POST
:
3652 case ARM::t2LDRSH_PRE
:
3653 case ARM::t2LDRSHi12
:
3654 case ARM::t2LDRSHi8
:
3655 case ARM::t2LDRSHpci
:
3659 case ARM::t2LDRDi8
: {
3660 Register Rt
= MI
.getOperand(0).getReg();
3661 Register Rn
= MI
.getOperand(2).getReg();
3662 return (Rt
== Rn
) ? 3 : 2;
3665 case ARM::t2STRB_POST
:
3666 case ARM::t2STRB_PRE
:
3669 case ARM::t2STRH_POST
:
3670 case ARM::t2STRH_PRE
:
3672 case ARM::t2STR_POST
:
3673 case ARM::t2STR_PRE
:
3679 // Return the number of 32-bit words loaded by LDM or stored by STM. If this
3680 // can't be easily determined return 0 (missing MachineMemOperand).
3682 // FIXME: The current MachineInstr design does not support relying on machine
3683 // mem operands to determine the width of a memory access. Instead, we expect
3684 // the target to provide this information based on the instruction opcode and
3685 // operands. However, using MachineMemOperand is the best solution now for
3688 // 1) getNumMicroOps tries to infer LDM memory width from the total number of MI
3689 // operands. This is much more dangerous than using the MachineMemOperand
3690 // sizes because CodeGen passes can insert/remove optional machine operands. In
3691 // fact, it's totally incorrect for preRA passes and appears to be wrong for
3692 // postRA passes as well.
3694 // 2) getNumLDMAddresses is only used by the scheduling machine model and any
3695 // machine model that calls this should handle the unknown (zero size) case.
3697 // Long term, we should require a target hook that verifies MachineMemOperand
3698 // sizes during MC lowering. That target hook should be local to MC lowering
3699 // because we can't ensure that it is aware of other MI forms. Doing this will
3700 // ensure that MachineMemOperands are correctly propagated through all passes.
3701 unsigned ARMBaseInstrInfo::getNumLDMAddresses(const MachineInstr
&MI
) const {
3703 for (MachineInstr::mmo_iterator I
= MI
.memoperands_begin(),
3704 E
= MI
.memoperands_end();
3706 Size
+= (*I
)->getSize();
3708 // FIXME: The scheduler currently can't handle values larger than 16. But
3709 // the values can actually go up to 32 for floating-point load/store
3710 // multiple (VLDMIA etc.). Also, the way this code is reasoning about memory
3711 // operations isn't right; we could end up with "extra" memory operands for
3712 // various reasons, like tail merge merging two memory operations.
3713 return std::min(Size
/ 4, 16U);
3716 static unsigned getNumMicroOpsSingleIssuePlusExtras(unsigned Opc
,
3718 unsigned UOps
= 1 + NumRegs
; // 1 for address computation.
3722 case ARM::VLDMDIA_UPD
:
3723 case ARM::VLDMDDB_UPD
:
3724 case ARM::VLDMSIA_UPD
:
3725 case ARM::VLDMSDB_UPD
:
3726 case ARM::VSTMDIA_UPD
:
3727 case ARM::VSTMDDB_UPD
:
3728 case ARM::VSTMSIA_UPD
:
3729 case ARM::VSTMSDB_UPD
:
3730 case ARM::LDMIA_UPD
:
3731 case ARM::LDMDA_UPD
:
3732 case ARM::LDMDB_UPD
:
3733 case ARM::LDMIB_UPD
:
3734 case ARM::STMIA_UPD
:
3735 case ARM::STMDA_UPD
:
3736 case ARM::STMDB_UPD
:
3737 case ARM::STMIB_UPD
:
3738 case ARM::tLDMIA_UPD
:
3739 case ARM::tSTMIA_UPD
:
3740 case ARM::t2LDMIA_UPD
:
3741 case ARM::t2LDMDB_UPD
:
3742 case ARM::t2STMIA_UPD
:
3743 case ARM::t2STMDB_UPD
:
3744 ++UOps
; // One for base register writeback.
3746 case ARM::LDMIA_RET
:
3748 case ARM::t2LDMIA_RET
:
3749 UOps
+= 2; // One for base reg wb, one for write to pc.
3755 unsigned ARMBaseInstrInfo::getNumMicroOps(const InstrItineraryData
*ItinData
,
3756 const MachineInstr
&MI
) const {
3757 if (!ItinData
|| ItinData
->isEmpty())
3760 const MCInstrDesc
&Desc
= MI
.getDesc();
3761 unsigned Class
= Desc
.getSchedClass();
3762 int ItinUOps
= ItinData
->getNumMicroOps(Class
);
3763 if (ItinUOps
>= 0) {
3764 if (Subtarget
.isSwift() && (Desc
.mayLoad() || Desc
.mayStore()))
3765 return getNumMicroOpsSwiftLdSt(ItinData
, MI
);
3770 unsigned Opc
= MI
.getOpcode();
3773 llvm_unreachable("Unexpected multi-uops instruction!");
3778 // The number of uOps for load / store multiple are determined by the number
3781 // On Cortex-A8, each pair of register loads / stores can be scheduled on the
3782 // same cycle. The scheduling for the first load / store must be done
3783 // separately by assuming the address is not 64-bit aligned.
3785 // On Cortex-A9, the formula is simply (#reg / 2) + (#reg % 2). If the address
3786 // is not 64-bit aligned, then AGU would take an extra cycle. For VFP / NEON
3787 // load / store multiple, the formula is (#reg / 2) + (#reg % 2) + 1.
3789 case ARM::VLDMDIA_UPD
:
3790 case ARM::VLDMDDB_UPD
:
3792 case ARM::VLDMSIA_UPD
:
3793 case ARM::VLDMSDB_UPD
:
3795 case ARM::VSTMDIA_UPD
:
3796 case ARM::VSTMDDB_UPD
:
3798 case ARM::VSTMSIA_UPD
:
3799 case ARM::VSTMSDB_UPD
: {
3800 unsigned NumRegs
= MI
.getNumOperands() - Desc
.getNumOperands();
3801 return (NumRegs
/ 2) + (NumRegs
% 2) + 1;
3804 case ARM::LDMIA_RET
:
3809 case ARM::LDMIA_UPD
:
3810 case ARM::LDMDA_UPD
:
3811 case ARM::LDMDB_UPD
:
3812 case ARM::LDMIB_UPD
:
3817 case ARM::STMIA_UPD
:
3818 case ARM::STMDA_UPD
:
3819 case ARM::STMDB_UPD
:
3820 case ARM::STMIB_UPD
:
3822 case ARM::tLDMIA_UPD
:
3823 case ARM::tSTMIA_UPD
:
3827 case ARM::t2LDMIA_RET
:
3830 case ARM::t2LDMIA_UPD
:
3831 case ARM::t2LDMDB_UPD
:
3834 case ARM::t2STMIA_UPD
:
3835 case ARM::t2STMDB_UPD
: {
3836 unsigned NumRegs
= MI
.getNumOperands() - Desc
.getNumOperands() + 1;
3837 switch (Subtarget
.getLdStMultipleTiming()) {
3838 case ARMSubtarget::SingleIssuePlusExtras
:
3839 return getNumMicroOpsSingleIssuePlusExtras(Opc
, NumRegs
);
3840 case ARMSubtarget::SingleIssue
:
3841 // Assume the worst.
3843 case ARMSubtarget::DoubleIssue
: {
3846 // 4 registers would be issued: 2, 2.
3847 // 5 registers would be issued: 2, 2, 1.
3848 unsigned UOps
= (NumRegs
/ 2);
3853 case ARMSubtarget::DoubleIssueCheckUnalignedAccess
: {
3854 unsigned UOps
= (NumRegs
/ 2);
3855 // If there are odd number of registers or if it's not 64-bit aligned,
3856 // then it takes an extra AGU (Address Generation Unit) cycle.
3857 if ((NumRegs
% 2) || !MI
.hasOneMemOperand() ||
3858 (*MI
.memoperands_begin())->getAlign() < Align(8))
3865 llvm_unreachable("Didn't find the number of microops");
3869 ARMBaseInstrInfo::getVLDMDefCycle(const InstrItineraryData
*ItinData
,
3870 const MCInstrDesc
&DefMCID
,
3872 unsigned DefIdx
, unsigned DefAlign
) const {
3873 int RegNo
= (int)(DefIdx
+1) - DefMCID
.getNumOperands() + 1;
3875 // Def is the address writeback.
3876 return ItinData
->getOperandCycle(DefClass
, DefIdx
);
3879 if (Subtarget
.isCortexA8() || Subtarget
.isCortexA7()) {
3880 // (regno / 2) + (regno % 2) + 1
3881 DefCycle
= RegNo
/ 2 + 1;
3884 } else if (Subtarget
.isLikeA9() || Subtarget
.isSwift()) {
3886 bool isSLoad
= false;
3888 switch (DefMCID
.getOpcode()) {
3891 case ARM::VLDMSIA_UPD
:
3892 case ARM::VLDMSDB_UPD
:
3897 // If there are odd number of 'S' registers or if it's not 64-bit aligned,
3898 // then it takes an extra cycle.
3899 if ((isSLoad
&& (RegNo
% 2)) || DefAlign
< 8)
3902 // Assume the worst.
3903 DefCycle
= RegNo
+ 2;
3910 ARMBaseInstrInfo::getLDMDefCycle(const InstrItineraryData
*ItinData
,
3911 const MCInstrDesc
&DefMCID
,
3913 unsigned DefIdx
, unsigned DefAlign
) const {
3914 int RegNo
= (int)(DefIdx
+1) - DefMCID
.getNumOperands() + 1;
3916 // Def is the address writeback.
3917 return ItinData
->getOperandCycle(DefClass
, DefIdx
);
3920 if (Subtarget
.isCortexA8() || Subtarget
.isCortexA7()) {
3921 // 4 registers would be issued: 1, 2, 1.
3922 // 5 registers would be issued: 1, 2, 2.
3923 DefCycle
= RegNo
/ 2;
3926 // Result latency is issue cycle + 2: E2.
3928 } else if (Subtarget
.isLikeA9() || Subtarget
.isSwift()) {
3929 DefCycle
= (RegNo
/ 2);
3930 // If there are odd number of registers or if it's not 64-bit aligned,
3931 // then it takes an extra AGU (Address Generation Unit) cycle.
3932 if ((RegNo
% 2) || DefAlign
< 8)
3934 // Result latency is AGU cycles + 2.
3937 // Assume the worst.
3938 DefCycle
= RegNo
+ 2;
3945 ARMBaseInstrInfo::getVSTMUseCycle(const InstrItineraryData
*ItinData
,
3946 const MCInstrDesc
&UseMCID
,
3948 unsigned UseIdx
, unsigned UseAlign
) const {
3949 int RegNo
= (int)(UseIdx
+1) - UseMCID
.getNumOperands() + 1;
3951 return ItinData
->getOperandCycle(UseClass
, UseIdx
);
3954 if (Subtarget
.isCortexA8() || Subtarget
.isCortexA7()) {
3955 // (regno / 2) + (regno % 2) + 1
3956 UseCycle
= RegNo
/ 2 + 1;
3959 } else if (Subtarget
.isLikeA9() || Subtarget
.isSwift()) {
3961 bool isSStore
= false;
3963 switch (UseMCID
.getOpcode()) {
3966 case ARM::VSTMSIA_UPD
:
3967 case ARM::VSTMSDB_UPD
:
3972 // If there are odd number of 'S' registers or if it's not 64-bit aligned,
3973 // then it takes an extra cycle.
3974 if ((isSStore
&& (RegNo
% 2)) || UseAlign
< 8)
3977 // Assume the worst.
3978 UseCycle
= RegNo
+ 2;
3985 ARMBaseInstrInfo::getSTMUseCycle(const InstrItineraryData
*ItinData
,
3986 const MCInstrDesc
&UseMCID
,
3988 unsigned UseIdx
, unsigned UseAlign
) const {
3989 int RegNo
= (int)(UseIdx
+1) - UseMCID
.getNumOperands() + 1;
3991 return ItinData
->getOperandCycle(UseClass
, UseIdx
);
3994 if (Subtarget
.isCortexA8() || Subtarget
.isCortexA7()) {
3995 UseCycle
= RegNo
/ 2;
4000 } else if (Subtarget
.isLikeA9() || Subtarget
.isSwift()) {
4001 UseCycle
= (RegNo
/ 2);
4002 // If there are odd number of registers or if it's not 64-bit aligned,
4003 // then it takes an extra AGU (Address Generation Unit) cycle.
4004 if ((RegNo
% 2) || UseAlign
< 8)
4007 // Assume the worst.
4014 ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData
*ItinData
,
4015 const MCInstrDesc
&DefMCID
,
4016 unsigned DefIdx
, unsigned DefAlign
,
4017 const MCInstrDesc
&UseMCID
,
4018 unsigned UseIdx
, unsigned UseAlign
) const {
4019 unsigned DefClass
= DefMCID
.getSchedClass();
4020 unsigned UseClass
= UseMCID
.getSchedClass();
4022 if (DefIdx
< DefMCID
.getNumDefs() && UseIdx
< UseMCID
.getNumOperands())
4023 return ItinData
->getOperandLatency(DefClass
, DefIdx
, UseClass
, UseIdx
);
4025 // This may be a def / use of a variable_ops instruction, the operand
4026 // latency might be determinable dynamically. Let the target try to
4029 bool LdmBypass
= false;
4030 switch (DefMCID
.getOpcode()) {
4032 DefCycle
= ItinData
->getOperandCycle(DefClass
, DefIdx
);
4036 case ARM::VLDMDIA_UPD
:
4037 case ARM::VLDMDDB_UPD
:
4039 case ARM::VLDMSIA_UPD
:
4040 case ARM::VLDMSDB_UPD
:
4041 DefCycle
= getVLDMDefCycle(ItinData
, DefMCID
, DefClass
, DefIdx
, DefAlign
);
4044 case ARM::LDMIA_RET
:
4049 case ARM::LDMIA_UPD
:
4050 case ARM::LDMDA_UPD
:
4051 case ARM::LDMDB_UPD
:
4052 case ARM::LDMIB_UPD
:
4054 case ARM::tLDMIA_UPD
:
4056 case ARM::t2LDMIA_RET
:
4059 case ARM::t2LDMIA_UPD
:
4060 case ARM::t2LDMDB_UPD
:
4062 DefCycle
= getLDMDefCycle(ItinData
, DefMCID
, DefClass
, DefIdx
, DefAlign
);
4067 // We can't seem to determine the result latency of the def, assume it's 2.
4071 switch (UseMCID
.getOpcode()) {
4073 UseCycle
= ItinData
->getOperandCycle(UseClass
, UseIdx
);
4077 case ARM::VSTMDIA_UPD
:
4078 case ARM::VSTMDDB_UPD
:
4080 case ARM::VSTMSIA_UPD
:
4081 case ARM::VSTMSDB_UPD
:
4082 UseCycle
= getVSTMUseCycle(ItinData
, UseMCID
, UseClass
, UseIdx
, UseAlign
);
4089 case ARM::STMIA_UPD
:
4090 case ARM::STMDA_UPD
:
4091 case ARM::STMDB_UPD
:
4092 case ARM::STMIB_UPD
:
4093 case ARM::tSTMIA_UPD
:
4098 case ARM::t2STMIA_UPD
:
4099 case ARM::t2STMDB_UPD
:
4100 UseCycle
= getSTMUseCycle(ItinData
, UseMCID
, UseClass
, UseIdx
, UseAlign
);
4105 // Assume it's read in the first stage.
4108 UseCycle
= DefCycle
- UseCycle
+ 1;
4111 // It's a variable_ops instruction so we can't use DefIdx here. Just use
4112 // first def operand.
4113 if (ItinData
->hasPipelineForwarding(DefClass
, DefMCID
.getNumOperands()-1,
4116 } else if (ItinData
->hasPipelineForwarding(DefClass
, DefIdx
,
4117 UseClass
, UseIdx
)) {
4125 static const MachineInstr
*getBundledDefMI(const TargetRegisterInfo
*TRI
,
4126 const MachineInstr
*MI
, unsigned Reg
,
4127 unsigned &DefIdx
, unsigned &Dist
) {
4130 MachineBasicBlock::const_iterator I
= MI
; ++I
;
4131 MachineBasicBlock::const_instr_iterator II
= std::prev(I
.getInstrIterator());
4132 assert(II
->isInsideBundle() && "Empty bundle?");
4135 while (II
->isInsideBundle()) {
4136 Idx
= II
->findRegisterDefOperandIdx(Reg
, false, true, TRI
);
4143 assert(Idx
!= -1 && "Cannot find bundled definition!");
4148 static const MachineInstr
*getBundledUseMI(const TargetRegisterInfo
*TRI
,
4149 const MachineInstr
&MI
, unsigned Reg
,
4150 unsigned &UseIdx
, unsigned &Dist
) {
4153 MachineBasicBlock::const_instr_iterator II
= ++MI
.getIterator();
4154 assert(II
->isInsideBundle() && "Empty bundle?");
4155 MachineBasicBlock::const_instr_iterator E
= MI
.getParent()->instr_end();
4157 // FIXME: This doesn't properly handle multiple uses.
4159 while (II
!= E
&& II
->isInsideBundle()) {
4160 Idx
= II
->findRegisterUseOperandIdx(Reg
, false, TRI
);
4163 if (II
->getOpcode() != ARM::t2IT
)
4177 /// Return the number of cycles to add to (or subtract from) the static
4178 /// itinerary based on the def opcode and alignment. The caller will ensure that
4179 /// adjusted latency is at least one cycle.
4180 static int adjustDefLatency(const ARMSubtarget
&Subtarget
,
4181 const MachineInstr
&DefMI
,
4182 const MCInstrDesc
&DefMCID
, unsigned DefAlign
) {
4184 if (Subtarget
.isCortexA8() || Subtarget
.isLikeA9() || Subtarget
.isCortexA7()) {
4185 // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2]
4186 // variants are one cycle cheaper.
4187 switch (DefMCID
.getOpcode()) {
4191 unsigned ShOpVal
= DefMI
.getOperand(3).getImm();
4192 unsigned ShImm
= ARM_AM::getAM2Offset(ShOpVal
);
4194 (ShImm
== 2 && ARM_AM::getAM2ShiftOpc(ShOpVal
) == ARM_AM::lsl
))
4201 case ARM::t2LDRSHs
: {
4202 // Thumb2 mode: lsl only.
4203 unsigned ShAmt
= DefMI
.getOperand(3).getImm();
4204 if (ShAmt
== 0 || ShAmt
== 2)
4209 } else if (Subtarget
.isSwift()) {
4210 // FIXME: Properly handle all of the latency adjustments for address
4212 switch (DefMCID
.getOpcode()) {
4216 unsigned ShOpVal
= DefMI
.getOperand(3).getImm();
4217 bool isSub
= ARM_AM::getAM2Op(ShOpVal
) == ARM_AM::sub
;
4218 unsigned ShImm
= ARM_AM::getAM2Offset(ShOpVal
);
4221 ((ShImm
== 1 || ShImm
== 2 || ShImm
== 3) &&
4222 ARM_AM::getAM2ShiftOpc(ShOpVal
) == ARM_AM::lsl
)))
4225 ShImm
== 1 && ARM_AM::getAM2ShiftOpc(ShOpVal
) == ARM_AM::lsr
)
4232 case ARM::t2LDRSHs
: {
4233 // Thumb2 mode: lsl only.
4234 unsigned ShAmt
= DefMI
.getOperand(3).getImm();
4235 if (ShAmt
== 0 || ShAmt
== 1 || ShAmt
== 2 || ShAmt
== 3)
4242 if (DefAlign
< 8 && Subtarget
.checkVLDnAccessAlignment()) {
4243 switch (DefMCID
.getOpcode()) {
4249 case ARM::VLD1q8wb_fixed
:
4250 case ARM::VLD1q16wb_fixed
:
4251 case ARM::VLD1q32wb_fixed
:
4252 case ARM::VLD1q64wb_fixed
:
4253 case ARM::VLD1q8wb_register
:
4254 case ARM::VLD1q16wb_register
:
4255 case ARM::VLD1q32wb_register
:
4256 case ARM::VLD1q64wb_register
:
4263 case ARM::VLD2d8wb_fixed
:
4264 case ARM::VLD2d16wb_fixed
:
4265 case ARM::VLD2d32wb_fixed
:
4266 case ARM::VLD2q8wb_fixed
:
4267 case ARM::VLD2q16wb_fixed
:
4268 case ARM::VLD2q32wb_fixed
:
4269 case ARM::VLD2d8wb_register
:
4270 case ARM::VLD2d16wb_register
:
4271 case ARM::VLD2d32wb_register
:
4272 case ARM::VLD2q8wb_register
:
4273 case ARM::VLD2q16wb_register
:
4274 case ARM::VLD2q32wb_register
:
4279 case ARM::VLD3d8_UPD
:
4280 case ARM::VLD3d16_UPD
:
4281 case ARM::VLD3d32_UPD
:
4282 case ARM::VLD1d64Twb_fixed
:
4283 case ARM::VLD1d64Twb_register
:
4284 case ARM::VLD3q8_UPD
:
4285 case ARM::VLD3q16_UPD
:
4286 case ARM::VLD3q32_UPD
:
4291 case ARM::VLD4d8_UPD
:
4292 case ARM::VLD4d16_UPD
:
4293 case ARM::VLD4d32_UPD
:
4294 case ARM::VLD1d64Qwb_fixed
:
4295 case ARM::VLD1d64Qwb_register
:
4296 case ARM::VLD4q8_UPD
:
4297 case ARM::VLD4q16_UPD
:
4298 case ARM::VLD4q32_UPD
:
4299 case ARM::VLD1DUPq8
:
4300 case ARM::VLD1DUPq16
:
4301 case ARM::VLD1DUPq32
:
4302 case ARM::VLD1DUPq8wb_fixed
:
4303 case ARM::VLD1DUPq16wb_fixed
:
4304 case ARM::VLD1DUPq32wb_fixed
:
4305 case ARM::VLD1DUPq8wb_register
:
4306 case ARM::VLD1DUPq16wb_register
:
4307 case ARM::VLD1DUPq32wb_register
:
4308 case ARM::VLD2DUPd8
:
4309 case ARM::VLD2DUPd16
:
4310 case ARM::VLD2DUPd32
:
4311 case ARM::VLD2DUPd8wb_fixed
:
4312 case ARM::VLD2DUPd16wb_fixed
:
4313 case ARM::VLD2DUPd32wb_fixed
:
4314 case ARM::VLD2DUPd8wb_register
:
4315 case ARM::VLD2DUPd16wb_register
:
4316 case ARM::VLD2DUPd32wb_register
:
4317 case ARM::VLD4DUPd8
:
4318 case ARM::VLD4DUPd16
:
4319 case ARM::VLD4DUPd32
:
4320 case ARM::VLD4DUPd8_UPD
:
4321 case ARM::VLD4DUPd16_UPD
:
4322 case ARM::VLD4DUPd32_UPD
:
4324 case ARM::VLD1LNd16
:
4325 case ARM::VLD1LNd32
:
4326 case ARM::VLD1LNd8_UPD
:
4327 case ARM::VLD1LNd16_UPD
:
4328 case ARM::VLD1LNd32_UPD
:
4330 case ARM::VLD2LNd16
:
4331 case ARM::VLD2LNd32
:
4332 case ARM::VLD2LNq16
:
4333 case ARM::VLD2LNq32
:
4334 case ARM::VLD2LNd8_UPD
:
4335 case ARM::VLD2LNd16_UPD
:
4336 case ARM::VLD2LNd32_UPD
:
4337 case ARM::VLD2LNq16_UPD
:
4338 case ARM::VLD2LNq32_UPD
:
4340 case ARM::VLD4LNd16
:
4341 case ARM::VLD4LNd32
:
4342 case ARM::VLD4LNq16
:
4343 case ARM::VLD4LNq32
:
4344 case ARM::VLD4LNd8_UPD
:
4345 case ARM::VLD4LNd16_UPD
:
4346 case ARM::VLD4LNd32_UPD
:
4347 case ARM::VLD4LNq16_UPD
:
4348 case ARM::VLD4LNq32_UPD
:
4349 // If the address is not 64-bit aligned, the latencies of these
4350 // instructions increases by one.
4358 int ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData
*ItinData
,
4359 const MachineInstr
&DefMI
,
4361 const MachineInstr
&UseMI
,
4362 unsigned UseIdx
) const {
4363 // No operand latency. The caller may fall back to getInstrLatency.
4364 if (!ItinData
|| ItinData
->isEmpty())
4367 const MachineOperand
&DefMO
= DefMI
.getOperand(DefIdx
);
4368 Register Reg
= DefMO
.getReg();
4370 const MachineInstr
*ResolvedDefMI
= &DefMI
;
4371 unsigned DefAdj
= 0;
4372 if (DefMI
.isBundle())
4374 getBundledDefMI(&getRegisterInfo(), &DefMI
, Reg
, DefIdx
, DefAdj
);
4375 if (ResolvedDefMI
->isCopyLike() || ResolvedDefMI
->isInsertSubreg() ||
4376 ResolvedDefMI
->isRegSequence() || ResolvedDefMI
->isImplicitDef()) {
4380 const MachineInstr
*ResolvedUseMI
= &UseMI
;
4381 unsigned UseAdj
= 0;
4382 if (UseMI
.isBundle()) {
4384 getBundledUseMI(&getRegisterInfo(), UseMI
, Reg
, UseIdx
, UseAdj
);
4389 return getOperandLatencyImpl(
4390 ItinData
, *ResolvedDefMI
, DefIdx
, ResolvedDefMI
->getDesc(), DefAdj
, DefMO
,
4391 Reg
, *ResolvedUseMI
, UseIdx
, ResolvedUseMI
->getDesc(), UseAdj
);
4394 int ARMBaseInstrInfo::getOperandLatencyImpl(
4395 const InstrItineraryData
*ItinData
, const MachineInstr
&DefMI
,
4396 unsigned DefIdx
, const MCInstrDesc
&DefMCID
, unsigned DefAdj
,
4397 const MachineOperand
&DefMO
, unsigned Reg
, const MachineInstr
&UseMI
,
4398 unsigned UseIdx
, const MCInstrDesc
&UseMCID
, unsigned UseAdj
) const {
4399 if (Reg
== ARM::CPSR
) {
4400 if (DefMI
.getOpcode() == ARM::FMSTAT
) {
4401 // fpscr -> cpsr stalls over 20 cycles on A8 (and earlier?)
4402 return Subtarget
.isLikeA9() ? 1 : 20;
4405 // CPSR set and branch can be paired in the same cycle.
4406 if (UseMI
.isBranch())
4409 // Otherwise it takes the instruction latency (generally one).
4410 unsigned Latency
= getInstrLatency(ItinData
, DefMI
);
4412 // For Thumb2 and -Os, prefer scheduling CPSR setting instruction close to
4413 // its uses. Instructions which are otherwise scheduled between them may
4414 // incur a code size penalty (not able to use the CPSR setting 16-bit
4416 if (Latency
> 0 && Subtarget
.isThumb2()) {
4417 const MachineFunction
*MF
= DefMI
.getParent()->getParent();
4418 // FIXME: Use Function::hasOptSize().
4419 if (MF
->getFunction().hasFnAttribute(Attribute::OptimizeForSize
))
4425 if (DefMO
.isImplicit() || UseMI
.getOperand(UseIdx
).isImplicit())
4428 unsigned DefAlign
= DefMI
.hasOneMemOperand()
4429 ? (*DefMI
.memoperands_begin())->getAlign().value()
4431 unsigned UseAlign
= UseMI
.hasOneMemOperand()
4432 ? (*UseMI
.memoperands_begin())->getAlign().value()
4435 // Get the itinerary's latency if possible, and handle variable_ops.
4436 int Latency
= getOperandLatency(ItinData
, DefMCID
, DefIdx
, DefAlign
, UseMCID
,
4438 // Unable to find operand latency. The caller may resort to getInstrLatency.
4442 // Adjust for IT block position.
4443 int Adj
= DefAdj
+ UseAdj
;
4445 // Adjust for dynamic def-side opcode variants not captured by the itinerary.
4446 Adj
+= adjustDefLatency(Subtarget
, DefMI
, DefMCID
, DefAlign
);
4447 if (Adj
>= 0 || (int)Latency
> -Adj
) {
4448 return Latency
+ Adj
;
4450 // Return the itinerary latency, which may be zero but not less than zero.
4455 ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData
*ItinData
,
4456 SDNode
*DefNode
, unsigned DefIdx
,
4457 SDNode
*UseNode
, unsigned UseIdx
) const {
4458 if (!DefNode
->isMachineOpcode())
4461 const MCInstrDesc
&DefMCID
= get(DefNode
->getMachineOpcode());
4463 if (isZeroCost(DefMCID
.Opcode
))
4466 if (!ItinData
|| ItinData
->isEmpty())
4467 return DefMCID
.mayLoad() ? 3 : 1;
4469 if (!UseNode
->isMachineOpcode()) {
4470 int Latency
= ItinData
->getOperandCycle(DefMCID
.getSchedClass(), DefIdx
);
4471 int Adj
= Subtarget
.getPreISelOperandLatencyAdjustment();
4472 int Threshold
= 1 + Adj
;
4473 return Latency
<= Threshold
? 1 : Latency
- Adj
;
4476 const MCInstrDesc
&UseMCID
= get(UseNode
->getMachineOpcode());
4477 auto *DefMN
= cast
<MachineSDNode
>(DefNode
);
4478 unsigned DefAlign
= !DefMN
->memoperands_empty()
4479 ? (*DefMN
->memoperands_begin())->getAlign().value()
4481 auto *UseMN
= cast
<MachineSDNode
>(UseNode
);
4482 unsigned UseAlign
= !UseMN
->memoperands_empty()
4483 ? (*UseMN
->memoperands_begin())->getAlign().value()
4485 int Latency
= getOperandLatency(ItinData
, DefMCID
, DefIdx
, DefAlign
,
4486 UseMCID
, UseIdx
, UseAlign
);
4489 (Subtarget
.isCortexA8() || Subtarget
.isLikeA9() ||
4490 Subtarget
.isCortexA7())) {
4491 // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2]
4492 // variants are one cycle cheaper.
4493 switch (DefMCID
.getOpcode()) {
4498 cast
<ConstantSDNode
>(DefNode
->getOperand(2))->getZExtValue();
4499 unsigned ShImm
= ARM_AM::getAM2Offset(ShOpVal
);
4501 (ShImm
== 2 && ARM_AM::getAM2ShiftOpc(ShOpVal
) == ARM_AM::lsl
))
4508 case ARM::t2LDRSHs
: {
4509 // Thumb2 mode: lsl only.
4511 cast
<ConstantSDNode
>(DefNode
->getOperand(2))->getZExtValue();
4512 if (ShAmt
== 0 || ShAmt
== 2)
4517 } else if (DefIdx
== 0 && Latency
> 2 && Subtarget
.isSwift()) {
4518 // FIXME: Properly handle all of the latency adjustments for address
4520 switch (DefMCID
.getOpcode()) {
4525 cast
<ConstantSDNode
>(DefNode
->getOperand(2))->getZExtValue();
4526 unsigned ShImm
= ARM_AM::getAM2Offset(ShOpVal
);
4528 ((ShImm
== 1 || ShImm
== 2 || ShImm
== 3) &&
4529 ARM_AM::getAM2ShiftOpc(ShOpVal
) == ARM_AM::lsl
))
4531 else if (ShImm
== 1 && ARM_AM::getAM2ShiftOpc(ShOpVal
) == ARM_AM::lsr
)
4539 // Thumb2 mode: lsl 0-3 only.
4545 if (DefAlign
< 8 && Subtarget
.checkVLDnAccessAlignment())
4546 switch (DefMCID
.getOpcode()) {
4552 case ARM::VLD1q8wb_register
:
4553 case ARM::VLD1q16wb_register
:
4554 case ARM::VLD1q32wb_register
:
4555 case ARM::VLD1q64wb_register
:
4556 case ARM::VLD1q8wb_fixed
:
4557 case ARM::VLD1q16wb_fixed
:
4558 case ARM::VLD1q32wb_fixed
:
4559 case ARM::VLD1q64wb_fixed
:
4563 case ARM::VLD2q8Pseudo
:
4564 case ARM::VLD2q16Pseudo
:
4565 case ARM::VLD2q32Pseudo
:
4566 case ARM::VLD2d8wb_fixed
:
4567 case ARM::VLD2d16wb_fixed
:
4568 case ARM::VLD2d32wb_fixed
:
4569 case ARM::VLD2q8PseudoWB_fixed
:
4570 case ARM::VLD2q16PseudoWB_fixed
:
4571 case ARM::VLD2q32PseudoWB_fixed
:
4572 case ARM::VLD2d8wb_register
:
4573 case ARM::VLD2d16wb_register
:
4574 case ARM::VLD2d32wb_register
:
4575 case ARM::VLD2q8PseudoWB_register
:
4576 case ARM::VLD2q16PseudoWB_register
:
4577 case ARM::VLD2q32PseudoWB_register
:
4578 case ARM::VLD3d8Pseudo
:
4579 case ARM::VLD3d16Pseudo
:
4580 case ARM::VLD3d32Pseudo
:
4581 case ARM::VLD1d8TPseudo
:
4582 case ARM::VLD1d16TPseudo
:
4583 case ARM::VLD1d32TPseudo
:
4584 case ARM::VLD1d64TPseudo
:
4585 case ARM::VLD1d64TPseudoWB_fixed
:
4586 case ARM::VLD1d64TPseudoWB_register
:
4587 case ARM::VLD3d8Pseudo_UPD
:
4588 case ARM::VLD3d16Pseudo_UPD
:
4589 case ARM::VLD3d32Pseudo_UPD
:
4590 case ARM::VLD3q8Pseudo_UPD
:
4591 case ARM::VLD3q16Pseudo_UPD
:
4592 case ARM::VLD3q32Pseudo_UPD
:
4593 case ARM::VLD3q8oddPseudo
:
4594 case ARM::VLD3q16oddPseudo
:
4595 case ARM::VLD3q32oddPseudo
:
4596 case ARM::VLD3q8oddPseudo_UPD
:
4597 case ARM::VLD3q16oddPseudo_UPD
:
4598 case ARM::VLD3q32oddPseudo_UPD
:
4599 case ARM::VLD4d8Pseudo
:
4600 case ARM::VLD4d16Pseudo
:
4601 case ARM::VLD4d32Pseudo
:
4602 case ARM::VLD1d8QPseudo
:
4603 case ARM::VLD1d16QPseudo
:
4604 case ARM::VLD1d32QPseudo
:
4605 case ARM::VLD1d64QPseudo
:
4606 case ARM::VLD1d64QPseudoWB_fixed
:
4607 case ARM::VLD1d64QPseudoWB_register
:
4608 case ARM::VLD1q8HighQPseudo
:
4609 case ARM::VLD1q8LowQPseudo_UPD
:
4610 case ARM::VLD1q8HighTPseudo
:
4611 case ARM::VLD1q8LowTPseudo_UPD
:
4612 case ARM::VLD1q16HighQPseudo
:
4613 case ARM::VLD1q16LowQPseudo_UPD
:
4614 case ARM::VLD1q16HighTPseudo
:
4615 case ARM::VLD1q16LowTPseudo_UPD
:
4616 case ARM::VLD1q32HighQPseudo
:
4617 case ARM::VLD1q32LowQPseudo_UPD
:
4618 case ARM::VLD1q32HighTPseudo
:
4619 case ARM::VLD1q32LowTPseudo_UPD
:
4620 case ARM::VLD1q64HighQPseudo
:
4621 case ARM::VLD1q64LowQPseudo_UPD
:
4622 case ARM::VLD1q64HighTPseudo
:
4623 case ARM::VLD1q64LowTPseudo_UPD
:
4624 case ARM::VLD4d8Pseudo_UPD
:
4625 case ARM::VLD4d16Pseudo_UPD
:
4626 case ARM::VLD4d32Pseudo_UPD
:
4627 case ARM::VLD4q8Pseudo_UPD
:
4628 case ARM::VLD4q16Pseudo_UPD
:
4629 case ARM::VLD4q32Pseudo_UPD
:
4630 case ARM::VLD4q8oddPseudo
:
4631 case ARM::VLD4q16oddPseudo
:
4632 case ARM::VLD4q32oddPseudo
:
4633 case ARM::VLD4q8oddPseudo_UPD
:
4634 case ARM::VLD4q16oddPseudo_UPD
:
4635 case ARM::VLD4q32oddPseudo_UPD
:
4636 case ARM::VLD1DUPq8
:
4637 case ARM::VLD1DUPq16
:
4638 case ARM::VLD1DUPq32
:
4639 case ARM::VLD1DUPq8wb_fixed
:
4640 case ARM::VLD1DUPq16wb_fixed
:
4641 case ARM::VLD1DUPq32wb_fixed
:
4642 case ARM::VLD1DUPq8wb_register
:
4643 case ARM::VLD1DUPq16wb_register
:
4644 case ARM::VLD1DUPq32wb_register
:
4645 case ARM::VLD2DUPd8
:
4646 case ARM::VLD2DUPd16
:
4647 case ARM::VLD2DUPd32
:
4648 case ARM::VLD2DUPd8wb_fixed
:
4649 case ARM::VLD2DUPd16wb_fixed
:
4650 case ARM::VLD2DUPd32wb_fixed
:
4651 case ARM::VLD2DUPd8wb_register
:
4652 case ARM::VLD2DUPd16wb_register
:
4653 case ARM::VLD2DUPd32wb_register
:
4654 case ARM::VLD2DUPq8EvenPseudo
:
4655 case ARM::VLD2DUPq8OddPseudo
:
4656 case ARM::VLD2DUPq16EvenPseudo
:
4657 case ARM::VLD2DUPq16OddPseudo
:
4658 case ARM::VLD2DUPq32EvenPseudo
:
4659 case ARM::VLD2DUPq32OddPseudo
:
4660 case ARM::VLD3DUPq8EvenPseudo
:
4661 case ARM::VLD3DUPq8OddPseudo
:
4662 case ARM::VLD3DUPq16EvenPseudo
:
4663 case ARM::VLD3DUPq16OddPseudo
:
4664 case ARM::VLD3DUPq32EvenPseudo
:
4665 case ARM::VLD3DUPq32OddPseudo
:
4666 case ARM::VLD4DUPd8Pseudo
:
4667 case ARM::VLD4DUPd16Pseudo
:
4668 case ARM::VLD4DUPd32Pseudo
:
4669 case ARM::VLD4DUPd8Pseudo_UPD
:
4670 case ARM::VLD4DUPd16Pseudo_UPD
:
4671 case ARM::VLD4DUPd32Pseudo_UPD
:
4672 case ARM::VLD4DUPq8EvenPseudo
:
4673 case ARM::VLD4DUPq8OddPseudo
:
4674 case ARM::VLD4DUPq16EvenPseudo
:
4675 case ARM::VLD4DUPq16OddPseudo
:
4676 case ARM::VLD4DUPq32EvenPseudo
:
4677 case ARM::VLD4DUPq32OddPseudo
:
4678 case ARM::VLD1LNq8Pseudo
:
4679 case ARM::VLD1LNq16Pseudo
:
4680 case ARM::VLD1LNq32Pseudo
:
4681 case ARM::VLD1LNq8Pseudo_UPD
:
4682 case ARM::VLD1LNq16Pseudo_UPD
:
4683 case ARM::VLD1LNq32Pseudo_UPD
:
4684 case ARM::VLD2LNd8Pseudo
:
4685 case ARM::VLD2LNd16Pseudo
:
4686 case ARM::VLD2LNd32Pseudo
:
4687 case ARM::VLD2LNq16Pseudo
:
4688 case ARM::VLD2LNq32Pseudo
:
4689 case ARM::VLD2LNd8Pseudo_UPD
:
4690 case ARM::VLD2LNd16Pseudo_UPD
:
4691 case ARM::VLD2LNd32Pseudo_UPD
:
4692 case ARM::VLD2LNq16Pseudo_UPD
:
4693 case ARM::VLD2LNq32Pseudo_UPD
:
4694 case ARM::VLD4LNd8Pseudo
:
4695 case ARM::VLD4LNd16Pseudo
:
4696 case ARM::VLD4LNd32Pseudo
:
4697 case ARM::VLD4LNq16Pseudo
:
4698 case ARM::VLD4LNq32Pseudo
:
4699 case ARM::VLD4LNd8Pseudo_UPD
:
4700 case ARM::VLD4LNd16Pseudo_UPD
:
4701 case ARM::VLD4LNd32Pseudo_UPD
:
4702 case ARM::VLD4LNq16Pseudo_UPD
:
4703 case ARM::VLD4LNq32Pseudo_UPD
:
4704 // If the address is not 64-bit aligned, the latencies of these
4705 // instructions increases by one.
4713 unsigned ARMBaseInstrInfo::getPredicationCost(const MachineInstr
&MI
) const {
4714 if (MI
.isCopyLike() || MI
.isInsertSubreg() || MI
.isRegSequence() ||
4721 const MCInstrDesc
&MCID
= MI
.getDesc();
4723 if (MCID
.isCall() || (MCID
.hasImplicitDefOfPhysReg(ARM::CPSR
) &&
4724 !Subtarget
.cheapPredicableCPSRDef())) {
4725 // When predicated, CPSR is an additional source operand for CPSR updating
4726 // instructions, this apparently increases their latencies.
4732 unsigned ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData
*ItinData
,
4733 const MachineInstr
&MI
,
4734 unsigned *PredCost
) const {
4735 if (MI
.isCopyLike() || MI
.isInsertSubreg() || MI
.isRegSequence() ||
4739 // An instruction scheduler typically runs on unbundled instructions, however
4740 // other passes may query the latency of a bundled instruction.
4741 if (MI
.isBundle()) {
4742 unsigned Latency
= 0;
4743 MachineBasicBlock::const_instr_iterator I
= MI
.getIterator();
4744 MachineBasicBlock::const_instr_iterator E
= MI
.getParent()->instr_end();
4745 while (++I
!= E
&& I
->isInsideBundle()) {
4746 if (I
->getOpcode() != ARM::t2IT
)
4747 Latency
+= getInstrLatency(ItinData
, *I
, PredCost
);
4752 const MCInstrDesc
&MCID
= MI
.getDesc();
4753 if (PredCost
&& (MCID
.isCall() || (MCID
.hasImplicitDefOfPhysReg(ARM::CPSR
) &&
4754 !Subtarget
.cheapPredicableCPSRDef()))) {
4755 // When predicated, CPSR is an additional source operand for CPSR updating
4756 // instructions, this apparently increases their latencies.
4759 // Be sure to call getStageLatency for an empty itinerary in case it has a
4760 // valid MinLatency property.
4762 return MI
.mayLoad() ? 3 : 1;
4764 unsigned Class
= MCID
.getSchedClass();
4766 // For instructions with variable uops, use uops as latency.
4767 if (!ItinData
->isEmpty() && ItinData
->getNumMicroOps(Class
) < 0)
4768 return getNumMicroOps(ItinData
, MI
);
4770 // For the common case, fall back on the itinerary's latency.
4771 unsigned Latency
= ItinData
->getStageLatency(Class
);
4773 // Adjust for dynamic def-side opcode variants not captured by the itinerary.
4775 MI
.hasOneMemOperand() ? (*MI
.memoperands_begin())->getAlign().value() : 0;
4776 int Adj
= adjustDefLatency(Subtarget
, MI
, MCID
, DefAlign
);
4777 if (Adj
>= 0 || (int)Latency
> -Adj
) {
4778 return Latency
+ Adj
;
4783 int ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData
*ItinData
,
4784 SDNode
*Node
) const {
4785 if (!Node
->isMachineOpcode())
4788 if (!ItinData
|| ItinData
->isEmpty())
4791 unsigned Opcode
= Node
->getMachineOpcode();
4794 return ItinData
->getStageLatency(get(Opcode
).getSchedClass());
4801 bool ARMBaseInstrInfo::hasHighOperandLatency(const TargetSchedModel
&SchedModel
,
4802 const MachineRegisterInfo
*MRI
,
4803 const MachineInstr
&DefMI
,
4805 const MachineInstr
&UseMI
,
4806 unsigned UseIdx
) const {
4807 unsigned DDomain
= DefMI
.getDesc().TSFlags
& ARMII::DomainMask
;
4808 unsigned UDomain
= UseMI
.getDesc().TSFlags
& ARMII::DomainMask
;
4809 if (Subtarget
.nonpipelinedVFP() &&
4810 (DDomain
== ARMII::DomainVFP
|| UDomain
== ARMII::DomainVFP
))
4813 // Hoist VFP / NEON instructions with 4 or higher latency.
4815 SchedModel
.computeOperandLatency(&DefMI
, DefIdx
, &UseMI
, UseIdx
);
4818 return DDomain
== ARMII::DomainVFP
|| DDomain
== ARMII::DomainNEON
||
4819 UDomain
== ARMII::DomainVFP
|| UDomain
== ARMII::DomainNEON
;
4822 bool ARMBaseInstrInfo::hasLowDefLatency(const TargetSchedModel
&SchedModel
,
4823 const MachineInstr
&DefMI
,
4824 unsigned DefIdx
) const {
4825 const InstrItineraryData
*ItinData
= SchedModel
.getInstrItineraries();
4826 if (!ItinData
|| ItinData
->isEmpty())
4829 unsigned DDomain
= DefMI
.getDesc().TSFlags
& ARMII::DomainMask
;
4830 if (DDomain
== ARMII::DomainGeneral
) {
4831 unsigned DefClass
= DefMI
.getDesc().getSchedClass();
4832 int DefCycle
= ItinData
->getOperandCycle(DefClass
, DefIdx
);
4833 return (DefCycle
!= -1 && DefCycle
<= 2);
4838 bool ARMBaseInstrInfo::verifyInstruction(const MachineInstr
&MI
,
4839 StringRef
&ErrInfo
) const {
4840 if (convertAddSubFlagsOpcode(MI
.getOpcode())) {
4841 ErrInfo
= "Pseudo flag setting opcodes only exist in Selection DAG";
4844 if (MI
.getOpcode() == ARM::tMOVr
&& !Subtarget
.hasV6Ops()) {
4845 // Make sure we don't generate a lo-lo mov that isn't supported.
4846 if (!ARM::hGPRRegClass
.contains(MI
.getOperand(0).getReg()) &&
4847 !ARM::hGPRRegClass
.contains(MI
.getOperand(1).getReg())) {
4848 ErrInfo
= "Non-flag-setting Thumb1 mov is v6-only";
4852 if (MI
.getOpcode() == ARM::tPUSH
||
4853 MI
.getOpcode() == ARM::tPOP
||
4854 MI
.getOpcode() == ARM::tPOP_RET
) {
4855 for (int i
= 2, e
= MI
.getNumOperands(); i
< e
; ++i
) {
4856 if (MI
.getOperand(i
).isImplicit() ||
4857 !MI
.getOperand(i
).isReg())
4859 Register Reg
= MI
.getOperand(i
).getReg();
4860 if (Reg
< ARM::R0
|| Reg
> ARM::R7
) {
4861 if (!(MI
.getOpcode() == ARM::tPUSH
&& Reg
== ARM::LR
) &&
4862 !(MI
.getOpcode() == ARM::tPOP_RET
&& Reg
== ARM::PC
)) {
4863 ErrInfo
= "Unsupported register in Thumb1 push/pop";
4869 if (MI
.getOpcode() == ARM::MVE_VMOV_q_rr
) {
4870 assert(MI
.getOperand(4).isImm() && MI
.getOperand(5).isImm());
4871 if ((MI
.getOperand(4).getImm() != 2 && MI
.getOperand(4).getImm() != 3) ||
4872 MI
.getOperand(4).getImm() != MI
.getOperand(5).getImm() + 2) {
4873 ErrInfo
= "Incorrect array index for MVE_VMOV_q_rr";
4880 // LoadStackGuard has so far only been implemented for MachO. Different code
4881 // sequence is needed for other targets.
4882 void ARMBaseInstrInfo::expandLoadStackGuardBase(MachineBasicBlock::iterator MI
,
4883 unsigned LoadImmOpc
,
4884 unsigned LoadOpc
) const {
4885 assert(!Subtarget
.isROPI() && !Subtarget
.isRWPI() &&
4886 "ROPI/RWPI not currently supported with stack guard");
4888 MachineBasicBlock
&MBB
= *MI
->getParent();
4889 DebugLoc DL
= MI
->getDebugLoc();
4890 Register Reg
= MI
->getOperand(0).getReg();
4891 const GlobalValue
*GV
=
4892 cast
<GlobalValue
>((*MI
->memoperands_begin())->getValue());
4893 MachineInstrBuilder MIB
;
4895 BuildMI(MBB
, MI
, DL
, get(LoadImmOpc
), Reg
)
4896 .addGlobalAddress(GV
, 0, ARMII::MO_NONLAZY
);
4898 if (Subtarget
.isGVIndirectSymbol(GV
)) {
4899 MIB
= BuildMI(MBB
, MI
, DL
, get(LoadOpc
), Reg
);
4900 MIB
.addReg(Reg
, RegState::Kill
).addImm(0);
4901 auto Flags
= MachineMemOperand::MOLoad
|
4902 MachineMemOperand::MODereferenceable
|
4903 MachineMemOperand::MOInvariant
;
4904 MachineMemOperand
*MMO
= MBB
.getParent()->getMachineMemOperand(
4905 MachinePointerInfo::getGOT(*MBB
.getParent()), Flags
, 4, Align(4));
4906 MIB
.addMemOperand(MMO
).add(predOps(ARMCC::AL
));
4909 MIB
= BuildMI(MBB
, MI
, DL
, get(LoadOpc
), Reg
);
4910 MIB
.addReg(Reg
, RegState::Kill
)
4913 .add(predOps(ARMCC::AL
));
4917 ARMBaseInstrInfo::isFpMLxInstruction(unsigned Opcode
, unsigned &MulOpc
,
4918 unsigned &AddSubOpc
,
4919 bool &NegAcc
, bool &HasLane
) const {
4920 DenseMap
<unsigned, unsigned>::const_iterator I
= MLxEntryMap
.find(Opcode
);
4921 if (I
== MLxEntryMap
.end())
4924 const ARM_MLxEntry
&Entry
= ARM_MLxTable
[I
->second
];
4925 MulOpc
= Entry
.MulOpc
;
4926 AddSubOpc
= Entry
.AddSubOpc
;
4927 NegAcc
= Entry
.NegAcc
;
4928 HasLane
= Entry
.HasLane
;
4932 //===----------------------------------------------------------------------===//
4933 // Execution domains.
4934 //===----------------------------------------------------------------------===//
4936 // Some instructions go down the NEON pipeline, some go down the VFP pipeline,
4937 // and some can go down both. The vmov instructions go down the VFP pipeline,
4938 // but they can be changed to vorr equivalents that are executed by the NEON
4941 // We use the following execution domain numbering:
4950 // Also see ARMInstrFormats.td and Domain* enums in ARMBaseInfo.h
4952 std::pair
<uint16_t, uint16_t>
4953 ARMBaseInstrInfo::getExecutionDomain(const MachineInstr
&MI
) const {
4954 // If we don't have access to NEON instructions then we won't be able
4955 // to swizzle anything to the NEON domain. Check to make sure.
4956 if (Subtarget
.hasNEON()) {
4957 // VMOVD, VMOVRS and VMOVSR are VFP instructions, but can be changed to NEON
4958 // if they are not predicated.
4959 if (MI
.getOpcode() == ARM::VMOVD
&& !isPredicated(MI
))
4960 return std::make_pair(ExeVFP
, (1 << ExeVFP
) | (1 << ExeNEON
));
4962 // CortexA9 is particularly picky about mixing the two and wants these
4964 if (Subtarget
.useNEONForFPMovs() && !isPredicated(MI
) &&
4965 (MI
.getOpcode() == ARM::VMOVRS
|| MI
.getOpcode() == ARM::VMOVSR
||
4966 MI
.getOpcode() == ARM::VMOVS
))
4967 return std::make_pair(ExeVFP
, (1 << ExeVFP
) | (1 << ExeNEON
));
4969 // No other instructions can be swizzled, so just determine their domain.
4970 unsigned Domain
= MI
.getDesc().TSFlags
& ARMII::DomainMask
;
4972 if (Domain
& ARMII::DomainNEON
)
4973 return std::make_pair(ExeNEON
, 0);
4975 // Certain instructions can go either way on Cortex-A8.
4976 // Treat them as NEON instructions.
4977 if ((Domain
& ARMII::DomainNEONA8
) && Subtarget
.isCortexA8())
4978 return std::make_pair(ExeNEON
, 0);
4980 if (Domain
& ARMII::DomainVFP
)
4981 return std::make_pair(ExeVFP
, 0);
4983 return std::make_pair(ExeGeneric
, 0);
4986 static unsigned getCorrespondingDRegAndLane(const TargetRegisterInfo
*TRI
,
4987 unsigned SReg
, unsigned &Lane
) {
4988 unsigned DReg
= TRI
->getMatchingSuperReg(SReg
, ARM::ssub_0
, &ARM::DPRRegClass
);
4991 if (DReg
!= ARM::NoRegister
)
4995 DReg
= TRI
->getMatchingSuperReg(SReg
, ARM::ssub_1
, &ARM::DPRRegClass
);
4997 assert(DReg
&& "S-register with no D super-register?");
5001 /// getImplicitSPRUseForDPRUse - Given a use of a DPR register and lane,
5002 /// set ImplicitSReg to a register number that must be marked as implicit-use or
5003 /// zero if no register needs to be defined as implicit-use.
5005 /// If the function cannot determine if an SPR should be marked implicit use or
5006 /// not, it returns false.
5008 /// This function handles cases where an instruction is being modified from taking
5009 /// an SPR to a DPR[Lane]. A use of the DPR is being added, which may conflict
5010 /// with an earlier def of an SPR corresponding to DPR[Lane^1] (i.e. the other
5011 /// lane of the DPR).
5013 /// If the other SPR is defined, an implicit-use of it should be added. Else,
5014 /// (including the case where the DPR itself is defined), it should not.
5016 static bool getImplicitSPRUseForDPRUse(const TargetRegisterInfo
*TRI
,
5017 MachineInstr
&MI
, unsigned DReg
,
5018 unsigned Lane
, unsigned &ImplicitSReg
) {
5019 // If the DPR is defined or used already, the other SPR lane will be chained
5020 // correctly, so there is nothing to be done.
5021 if (MI
.definesRegister(DReg
, TRI
) || MI
.readsRegister(DReg
, TRI
)) {
5026 // Otherwise we need to go searching to see if the SPR is set explicitly.
5027 ImplicitSReg
= TRI
->getSubReg(DReg
,
5028 (Lane
& 1) ? ARM::ssub_0
: ARM::ssub_1
);
5029 MachineBasicBlock::LivenessQueryResult LQR
=
5030 MI
.getParent()->computeRegisterLiveness(TRI
, ImplicitSReg
, MI
);
5032 if (LQR
== MachineBasicBlock::LQR_Live
)
5034 else if (LQR
== MachineBasicBlock::LQR_Unknown
)
5037 // If the register is known not to be live, there is no need to add an
5043 void ARMBaseInstrInfo::setExecutionDomain(MachineInstr
&MI
,
5044 unsigned Domain
) const {
5045 unsigned DstReg
, SrcReg
, DReg
;
5047 MachineInstrBuilder
MIB(*MI
.getParent()->getParent(), MI
);
5048 const TargetRegisterInfo
*TRI
= &getRegisterInfo();
5049 switch (MI
.getOpcode()) {
5051 llvm_unreachable("cannot handle opcode!");
5054 if (Domain
!= ExeNEON
)
5057 // Zap the predicate operands.
5058 assert(!isPredicated(MI
) && "Cannot predicate a VORRd");
5060 // Make sure we've got NEON instructions.
5061 assert(Subtarget
.hasNEON() && "VORRd requires NEON");
5063 // Source instruction is %DDst = VMOVD %DSrc, 14, %noreg (; implicits)
5064 DstReg
= MI
.getOperand(0).getReg();
5065 SrcReg
= MI
.getOperand(1).getReg();
5067 for (unsigned i
= MI
.getDesc().getNumOperands(); i
; --i
)
5068 MI
.RemoveOperand(i
- 1);
5070 // Change to a %DDst = VORRd %DSrc, %DSrc, 14, %noreg (; implicits)
5071 MI
.setDesc(get(ARM::VORRd
));
5072 MIB
.addReg(DstReg
, RegState::Define
)
5075 .add(predOps(ARMCC::AL
));
5078 if (Domain
!= ExeNEON
)
5080 assert(!isPredicated(MI
) && "Cannot predicate a VGETLN");
5082 // Source instruction is %RDst = VMOVRS %SSrc, 14, %noreg (; implicits)
5083 DstReg
= MI
.getOperand(0).getReg();
5084 SrcReg
= MI
.getOperand(1).getReg();
5086 for (unsigned i
= MI
.getDesc().getNumOperands(); i
; --i
)
5087 MI
.RemoveOperand(i
- 1);
5089 DReg
= getCorrespondingDRegAndLane(TRI
, SrcReg
, Lane
);
5091 // Convert to %RDst = VGETLNi32 %DSrc, Lane, 14, %noreg (; imps)
5092 // Note that DSrc has been widened and the other lane may be undef, which
5093 // contaminates the entire register.
5094 MI
.setDesc(get(ARM::VGETLNi32
));
5095 MIB
.addReg(DstReg
, RegState::Define
)
5096 .addReg(DReg
, RegState::Undef
)
5098 .add(predOps(ARMCC::AL
));
5100 // The old source should be an implicit use, otherwise we might think it
5101 // was dead before here.
5102 MIB
.addReg(SrcReg
, RegState::Implicit
);
5105 if (Domain
!= ExeNEON
)
5107 assert(!isPredicated(MI
) && "Cannot predicate a VSETLN");
5109 // Source instruction is %SDst = VMOVSR %RSrc, 14, %noreg (; implicits)
5110 DstReg
= MI
.getOperand(0).getReg();
5111 SrcReg
= MI
.getOperand(1).getReg();
5113 DReg
= getCorrespondingDRegAndLane(TRI
, DstReg
, Lane
);
5115 unsigned ImplicitSReg
;
5116 if (!getImplicitSPRUseForDPRUse(TRI
, MI
, DReg
, Lane
, ImplicitSReg
))
5119 for (unsigned i
= MI
.getDesc().getNumOperands(); i
; --i
)
5120 MI
.RemoveOperand(i
- 1);
5122 // Convert to %DDst = VSETLNi32 %DDst, %RSrc, Lane, 14, %noreg (; imps)
5123 // Again DDst may be undefined at the beginning of this instruction.
5124 MI
.setDesc(get(ARM::VSETLNi32
));
5125 MIB
.addReg(DReg
, RegState::Define
)
5126 .addReg(DReg
, getUndefRegState(!MI
.readsRegister(DReg
, TRI
)))
5129 .add(predOps(ARMCC::AL
));
5131 // The narrower destination must be marked as set to keep previous chains
5133 MIB
.addReg(DstReg
, RegState::Define
| RegState::Implicit
);
5134 if (ImplicitSReg
!= 0)
5135 MIB
.addReg(ImplicitSReg
, RegState::Implicit
);
5139 if (Domain
!= ExeNEON
)
5142 // Source instruction is %SDst = VMOVS %SSrc, 14, %noreg (; implicits)
5143 DstReg
= MI
.getOperand(0).getReg();
5144 SrcReg
= MI
.getOperand(1).getReg();
5146 unsigned DstLane
= 0, SrcLane
= 0, DDst
, DSrc
;
5147 DDst
= getCorrespondingDRegAndLane(TRI
, DstReg
, DstLane
);
5148 DSrc
= getCorrespondingDRegAndLane(TRI
, SrcReg
, SrcLane
);
5150 unsigned ImplicitSReg
;
5151 if (!getImplicitSPRUseForDPRUse(TRI
, MI
, DSrc
, SrcLane
, ImplicitSReg
))
5154 for (unsigned i
= MI
.getDesc().getNumOperands(); i
; --i
)
5155 MI
.RemoveOperand(i
- 1);
5158 // Destination can be:
5159 // %DDst = VDUPLN32d %DDst, Lane, 14, %noreg (; implicits)
5160 MI
.setDesc(get(ARM::VDUPLN32d
));
5161 MIB
.addReg(DDst
, RegState::Define
)
5162 .addReg(DDst
, getUndefRegState(!MI
.readsRegister(DDst
, TRI
)))
5164 .add(predOps(ARMCC::AL
));
5166 // Neither the source or the destination are naturally represented any
5167 // more, so add them in manually.
5168 MIB
.addReg(DstReg
, RegState::Implicit
| RegState::Define
);
5169 MIB
.addReg(SrcReg
, RegState::Implicit
);
5170 if (ImplicitSReg
!= 0)
5171 MIB
.addReg(ImplicitSReg
, RegState::Implicit
);
5175 // In general there's no single instruction that can perform an S <-> S
5176 // move in NEON space, but a pair of VEXT instructions *can* do the
5177 // job. It turns out that the VEXTs needed will only use DSrc once, with
5178 // the position based purely on the combination of lane-0 and lane-1
5179 // involved. For example
5180 // vmov s0, s2 -> vext.32 d0, d0, d1, #1 vext.32 d0, d0, d0, #1
5181 // vmov s1, s3 -> vext.32 d0, d1, d0, #1 vext.32 d0, d0, d0, #1
5182 // vmov s0, s3 -> vext.32 d0, d0, d0, #1 vext.32 d0, d1, d0, #1
5183 // vmov s1, s2 -> vext.32 d0, d0, d0, #1 vext.32 d0, d0, d1, #1
5185 // Pattern of the MachineInstrs is:
5186 // %DDst = VEXTd32 %DSrc1, %DSrc2, Lane, 14, %noreg (;implicits)
5187 MachineInstrBuilder NewMIB
;
5188 NewMIB
= BuildMI(*MI
.getParent(), MI
, MI
.getDebugLoc(), get(ARM::VEXTd32
),
5191 // On the first instruction, both DSrc and DDst may be undef if present.
5192 // Specifically when the original instruction didn't have them as an
5194 unsigned CurReg
= SrcLane
== 1 && DstLane
== 1 ? DSrc
: DDst
;
5195 bool CurUndef
= !MI
.readsRegister(CurReg
, TRI
);
5196 NewMIB
.addReg(CurReg
, getUndefRegState(CurUndef
));
5198 CurReg
= SrcLane
== 0 && DstLane
== 0 ? DSrc
: DDst
;
5199 CurUndef
= !MI
.readsRegister(CurReg
, TRI
);
5200 NewMIB
.addReg(CurReg
, getUndefRegState(CurUndef
))
5202 .add(predOps(ARMCC::AL
));
5204 if (SrcLane
== DstLane
)
5205 NewMIB
.addReg(SrcReg
, RegState::Implicit
);
5207 MI
.setDesc(get(ARM::VEXTd32
));
5208 MIB
.addReg(DDst
, RegState::Define
);
5210 // On the second instruction, DDst has definitely been defined above, so
5211 // it is not undef. DSrc, if present, can be undef as above.
5212 CurReg
= SrcLane
== 1 && DstLane
== 0 ? DSrc
: DDst
;
5213 CurUndef
= CurReg
== DSrc
&& !MI
.readsRegister(CurReg
, TRI
);
5214 MIB
.addReg(CurReg
, getUndefRegState(CurUndef
));
5216 CurReg
= SrcLane
== 0 && DstLane
== 1 ? DSrc
: DDst
;
5217 CurUndef
= CurReg
== DSrc
&& !MI
.readsRegister(CurReg
, TRI
);
5218 MIB
.addReg(CurReg
, getUndefRegState(CurUndef
))
5220 .add(predOps(ARMCC::AL
));
5222 if (SrcLane
!= DstLane
)
5223 MIB
.addReg(SrcReg
, RegState::Implicit
);
5225 // As before, the original destination is no longer represented, add it
5227 MIB
.addReg(DstReg
, RegState::Define
| RegState::Implicit
);
5228 if (ImplicitSReg
!= 0)
5229 MIB
.addReg(ImplicitSReg
, RegState::Implicit
);
5235 //===----------------------------------------------------------------------===//
5236 // Partial register updates
5237 //===----------------------------------------------------------------------===//
5239 // Swift renames NEON registers with 64-bit granularity. That means any
5240 // instruction writing an S-reg implicitly reads the containing D-reg. The
5241 // problem is mostly avoided by translating f32 operations to v2f32 operations
5242 // on D-registers, but f32 loads are still a problem.
5244 // These instructions can load an f32 into a NEON register:
5246 // VLDRS - Only writes S, partial D update.
5247 // VLD1LNd32 - Writes all D-regs, explicit partial D update, 2 uops.
5248 // VLD1DUPd32 - Writes all D-regs, no partial reg update, 2 uops.
5250 // FCONSTD can be used as a dependency-breaking instruction.
5251 unsigned ARMBaseInstrInfo::getPartialRegUpdateClearance(
5252 const MachineInstr
&MI
, unsigned OpNum
,
5253 const TargetRegisterInfo
*TRI
) const {
5254 auto PartialUpdateClearance
= Subtarget
.getPartialUpdateClearance();
5255 if (!PartialUpdateClearance
)
5258 assert(TRI
&& "Need TRI instance");
5260 const MachineOperand
&MO
= MI
.getOperand(OpNum
);
5263 Register Reg
= MO
.getReg();
5266 switch (MI
.getOpcode()) {
5267 // Normal instructions writing only an S-register.
5272 case ARM::VMOVv4i16
:
5273 case ARM::VMOVv2i32
:
5274 case ARM::VMOVv2f32
:
5275 case ARM::VMOVv1i64
:
5276 UseOp
= MI
.findRegisterUseOperandIdx(Reg
, false, TRI
);
5279 // Explicitly reads the dependency.
5280 case ARM::VLD1LNd32
:
5287 // If this instruction actually reads a value from Reg, there is no unwanted
5289 if (UseOp
!= -1 && MI
.getOperand(UseOp
).readsReg())
5292 // We must be able to clobber the whole D-reg.
5293 if (Register::isVirtualRegister(Reg
)) {
5294 // Virtual register must be a def undef foo:ssub_0 operand.
5295 if (!MO
.getSubReg() || MI
.readsVirtualRegister(Reg
))
5297 } else if (ARM::SPRRegClass
.contains(Reg
)) {
5298 // Physical register: MI must define the full D-reg.
5299 unsigned DReg
= TRI
->getMatchingSuperReg(Reg
, ARM::ssub_0
,
5301 if (!DReg
|| !MI
.definesRegister(DReg
, TRI
))
5305 // MI has an unwanted D-register dependency.
5306 // Avoid defs in the previous N instructrions.
5307 return PartialUpdateClearance
;
5310 // Break a partial register dependency after getPartialRegUpdateClearance
5311 // returned non-zero.
5312 void ARMBaseInstrInfo::breakPartialRegDependency(
5313 MachineInstr
&MI
, unsigned OpNum
, const TargetRegisterInfo
*TRI
) const {
5314 assert(OpNum
< MI
.getDesc().getNumDefs() && "OpNum is not a def");
5315 assert(TRI
&& "Need TRI instance");
5317 const MachineOperand
&MO
= MI
.getOperand(OpNum
);
5318 Register Reg
= MO
.getReg();
5319 assert(Register::isPhysicalRegister(Reg
) &&
5320 "Can't break virtual register dependencies.");
5321 unsigned DReg
= Reg
;
5323 // If MI defines an S-reg, find the corresponding D super-register.
5324 if (ARM::SPRRegClass
.contains(Reg
)) {
5325 DReg
= ARM::D0
+ (Reg
- ARM::S0
) / 2;
5326 assert(TRI
->isSuperRegister(Reg
, DReg
) && "Register enums broken");
5329 assert(ARM::DPRRegClass
.contains(DReg
) && "Can only break D-reg deps");
5330 assert(MI
.definesRegister(DReg
, TRI
) && "MI doesn't clobber full D-reg");
5332 // FIXME: In some cases, VLDRS can be changed to a VLD1DUPd32 which defines
5333 // the full D-register by loading the same value to both lanes. The
5334 // instruction is micro-coded with 2 uops, so don't do this until we can
5335 // properly schedule micro-coded instructions. The dispatcher stalls cause
5336 // too big regressions.
5338 // Insert the dependency-breaking FCONSTD before MI.
5339 // 96 is the encoding of 0.5, but the actual value doesn't matter here.
5340 BuildMI(*MI
.getParent(), MI
, MI
.getDebugLoc(), get(ARM::FCONSTD
), DReg
)
5342 .add(predOps(ARMCC::AL
));
5343 MI
.addRegisterKilled(DReg
, TRI
, true);
5346 bool ARMBaseInstrInfo::hasNOP() const {
5347 return Subtarget
.getFeatureBits()[ARM::HasV6KOps
];
5350 bool ARMBaseInstrInfo::isSwiftFastImmShift(const MachineInstr
*MI
) const {
5351 if (MI
->getNumOperands() < 4)
5353 unsigned ShOpVal
= MI
->getOperand(3).getImm();
5354 unsigned ShImm
= ARM_AM::getSORegOffset(ShOpVal
);
5355 // Swift supports faster shifts for: lsl 2, lsl 1, and lsr 1.
5356 if ((ShImm
== 1 && ARM_AM::getSORegShOp(ShOpVal
) == ARM_AM::lsr
) ||
5357 ((ShImm
== 1 || ShImm
== 2) &&
5358 ARM_AM::getSORegShOp(ShOpVal
) == ARM_AM::lsl
))
5364 bool ARMBaseInstrInfo::getRegSequenceLikeInputs(
5365 const MachineInstr
&MI
, unsigned DefIdx
,
5366 SmallVectorImpl
<RegSubRegPairAndIdx
> &InputRegs
) const {
5367 assert(DefIdx
< MI
.getDesc().getNumDefs() && "Invalid definition index");
5368 assert(MI
.isRegSequenceLike() && "Invalid kind of instruction");
5370 switch (MI
.getOpcode()) {
5372 // dX = VMOVDRR rY, rZ
5374 // dX = REG_SEQUENCE rY, ssub_0, rZ, ssub_1
5375 // Populate the InputRegs accordingly.
5377 const MachineOperand
*MOReg
= &MI
.getOperand(1);
5378 if (!MOReg
->isUndef())
5379 InputRegs
.push_back(RegSubRegPairAndIdx(MOReg
->getReg(),
5380 MOReg
->getSubReg(), ARM::ssub_0
));
5382 MOReg
= &MI
.getOperand(2);
5383 if (!MOReg
->isUndef())
5384 InputRegs
.push_back(RegSubRegPairAndIdx(MOReg
->getReg(),
5385 MOReg
->getSubReg(), ARM::ssub_1
));
5388 llvm_unreachable("Target dependent opcode missing");
5391 bool ARMBaseInstrInfo::getExtractSubregLikeInputs(
5392 const MachineInstr
&MI
, unsigned DefIdx
,
5393 RegSubRegPairAndIdx
&InputReg
) const {
5394 assert(DefIdx
< MI
.getDesc().getNumDefs() && "Invalid definition index");
5395 assert(MI
.isExtractSubregLike() && "Invalid kind of instruction");
5397 switch (MI
.getOpcode()) {
5399 // rX, rY = VMOVRRD dZ
5401 // rX = EXTRACT_SUBREG dZ, ssub_0
5402 // rY = EXTRACT_SUBREG dZ, ssub_1
5403 const MachineOperand
&MOReg
= MI
.getOperand(2);
5404 if (MOReg
.isUndef())
5406 InputReg
.Reg
= MOReg
.getReg();
5407 InputReg
.SubReg
= MOReg
.getSubReg();
5408 InputReg
.SubIdx
= DefIdx
== 0 ? ARM::ssub_0
: ARM::ssub_1
;
5411 llvm_unreachable("Target dependent opcode missing");
5414 bool ARMBaseInstrInfo::getInsertSubregLikeInputs(
5415 const MachineInstr
&MI
, unsigned DefIdx
, RegSubRegPair
&BaseReg
,
5416 RegSubRegPairAndIdx
&InsertedReg
) const {
5417 assert(DefIdx
< MI
.getDesc().getNumDefs() && "Invalid definition index");
5418 assert(MI
.isInsertSubregLike() && "Invalid kind of instruction");
5420 switch (MI
.getOpcode()) {
5421 case ARM::VSETLNi32
:
5422 case ARM::MVE_VMOV_to_lane_32
:
5423 // dX = VSETLNi32 dY, rZ, imm
5424 // qX = MVE_VMOV_to_lane_32 qY, rZ, imm
5425 const MachineOperand
&MOBaseReg
= MI
.getOperand(1);
5426 const MachineOperand
&MOInsertedReg
= MI
.getOperand(2);
5427 if (MOInsertedReg
.isUndef())
5429 const MachineOperand
&MOIndex
= MI
.getOperand(3);
5430 BaseReg
.Reg
= MOBaseReg
.getReg();
5431 BaseReg
.SubReg
= MOBaseReg
.getSubReg();
5433 InsertedReg
.Reg
= MOInsertedReg
.getReg();
5434 InsertedReg
.SubReg
= MOInsertedReg
.getSubReg();
5435 InsertedReg
.SubIdx
= ARM::ssub_0
+ MOIndex
.getImm();
5438 llvm_unreachable("Target dependent opcode missing");
5441 std::pair
<unsigned, unsigned>
5442 ARMBaseInstrInfo::decomposeMachineOperandsTargetFlags(unsigned TF
) const {
5443 const unsigned Mask
= ARMII::MO_OPTION_MASK
;
5444 return std::make_pair(TF
& Mask
, TF
& ~Mask
);
5447 ArrayRef
<std::pair
<unsigned, const char *>>
5448 ARMBaseInstrInfo::getSerializableDirectMachineOperandTargetFlags() const {
5449 using namespace ARMII
;
5451 static const std::pair
<unsigned, const char *> TargetFlags
[] = {
5452 {MO_LO16
, "arm-lo16"}, {MO_HI16
, "arm-hi16"}};
5453 return makeArrayRef(TargetFlags
);
5456 ArrayRef
<std::pair
<unsigned, const char *>>
5457 ARMBaseInstrInfo::getSerializableBitmaskMachineOperandTargetFlags() const {
5458 using namespace ARMII
;
5460 static const std::pair
<unsigned, const char *> TargetFlags
[] = {
5461 {MO_COFFSTUB
, "arm-coffstub"},
5462 {MO_GOT
, "arm-got"},
5463 {MO_SBREL
, "arm-sbrel"},
5464 {MO_DLLIMPORT
, "arm-dllimport"},
5465 {MO_SECREL
, "arm-secrel"},
5466 {MO_NONLAZY
, "arm-nonlazy"}};
5467 return makeArrayRef(TargetFlags
);
5470 Optional
<RegImmPair
> ARMBaseInstrInfo::isAddImmediate(const MachineInstr
&MI
,
5471 Register Reg
) const {
5473 unsigned Opcode
= MI
.getOpcode();
5476 // TODO: Handle cases where Reg is a super- or sub-register of the
5477 // destination register.
5478 const MachineOperand
&Op0
= MI
.getOperand(0);
5479 if (!Op0
.isReg() || Reg
!= Op0
.getReg())
5482 // We describe SUBri or ADDri instructions.
5483 if (Opcode
== ARM::SUBri
)
5485 else if (Opcode
!= ARM::ADDri
)
5488 // TODO: Third operand can be global address (usually some string). Since
5489 // strings can be relocated we cannot calculate their offsets for
5491 if (!MI
.getOperand(1).isReg() || !MI
.getOperand(2).isImm())
5494 Offset
= MI
.getOperand(2).getImm() * Sign
;
5495 return RegImmPair
{MI
.getOperand(1).getReg(), Offset
};
5498 bool llvm::registerDefinedBetween(unsigned Reg
,
5499 MachineBasicBlock::iterator From
,
5500 MachineBasicBlock::iterator To
,
5501 const TargetRegisterInfo
*TRI
) {
5502 for (auto I
= From
; I
!= To
; ++I
)
5503 if (I
->modifiesRegister(Reg
, TRI
))
5508 MachineInstr
*llvm::findCMPToFoldIntoCBZ(MachineInstr
*Br
,
5509 const TargetRegisterInfo
*TRI
) {
5510 // Search backwards to the instruction that defines CSPR. This may or not
5511 // be a CMP, we check that after this loop. If we find another instruction
5512 // that reads cpsr, we return nullptr.
5513 MachineBasicBlock::iterator CmpMI
= Br
;
5514 while (CmpMI
!= Br
->getParent()->begin()) {
5516 if (CmpMI
->modifiesRegister(ARM::CPSR
, TRI
))
5518 if (CmpMI
->readsRegister(ARM::CPSR
, TRI
))
5522 // Check that this inst is a CMP r[0-7], #0 and that the register
5523 // is not redefined between the cmp and the br.
5524 if (CmpMI
->getOpcode() != ARM::tCMPi8
&& CmpMI
->getOpcode() != ARM::t2CMPri
)
5526 Register Reg
= CmpMI
->getOperand(0).getReg();
5528 ARMCC::CondCodes Pred
= getInstrPredicate(*CmpMI
, PredReg
);
5529 if (Pred
!= ARMCC::AL
|| CmpMI
->getOperand(1).getImm() != 0)
5531 if (!isARMLowRegister(Reg
))
5533 if (registerDefinedBetween(Reg
, CmpMI
->getNextNode(), Br
, TRI
))
5539 unsigned llvm::ConstantMaterializationCost(unsigned Val
,
5540 const ARMSubtarget
*Subtarget
,
5542 if (Subtarget
->isThumb()) {
5543 if (Val
<= 255) // MOV
5544 return ForCodesize
? 2 : 1;
5545 if (Subtarget
->hasV6T2Ops() && (Val
<= 0xffff || // MOV
5546 ARM_AM::getT2SOImmVal(Val
) != -1 || // MOVW
5547 ARM_AM::getT2SOImmVal(~Val
) != -1)) // MVN
5548 return ForCodesize
? 4 : 1;
5549 if (Val
<= 510) // MOV + ADDi8
5550 return ForCodesize
? 4 : 2;
5551 if (~Val
<= 255) // MOV + MVN
5552 return ForCodesize
? 4 : 2;
5553 if (ARM_AM::isThumbImmShiftedVal(Val
)) // MOV + LSL
5554 return ForCodesize
? 4 : 2;
5556 if (ARM_AM::getSOImmVal(Val
) != -1) // MOV
5557 return ForCodesize
? 4 : 1;
5558 if (ARM_AM::getSOImmVal(~Val
) != -1) // MVN
5559 return ForCodesize
? 4 : 1;
5560 if (Subtarget
->hasV6T2Ops() && Val
<= 0xffff) // MOVW
5561 return ForCodesize
? 4 : 1;
5562 if (ARM_AM::isSOImmTwoPartVal(Val
)) // two instrs
5563 return ForCodesize
? 8 : 2;
5564 if (ARM_AM::isSOImmTwoPartValNeg(Val
)) // two instrs
5565 return ForCodesize
? 8 : 2;
5567 if (Subtarget
->useMovt()) // MOVW + MOVT
5568 return ForCodesize
? 8 : 2;
5569 return ForCodesize
? 8 : 3; // Literal pool load
5572 bool llvm::HasLowerConstantMaterializationCost(unsigned Val1
, unsigned Val2
,
5573 const ARMSubtarget
*Subtarget
,
5575 // Check with ForCodesize
5576 unsigned Cost1
= ConstantMaterializationCost(Val1
, Subtarget
, ForCodesize
);
5577 unsigned Cost2
= ConstantMaterializationCost(Val2
, Subtarget
, ForCodesize
);
5583 // If they are equal, try with !ForCodesize
5584 return ConstantMaterializationCost(Val1
, Subtarget
, !ForCodesize
) <
5585 ConstantMaterializationCost(Val2
, Subtarget
, !ForCodesize
);
5588 /// Constants defining how certain sequences should be outlined.
5589 /// This encompasses how an outlined function should be called, and what kind of
5590 /// frame should be emitted for that outlined function.
5592 /// \p MachineOutlinerTailCall implies that the function is being created from
5593 /// a sequence of instructions ending in a return.
5597 /// I1 OUTLINED_FUNCTION:
5598 /// I2 --> B OUTLINED_FUNCTION I1
5602 /// +-------------------------+--------+-----+
5603 /// | | Thumb2 | ARM |
5604 /// +-------------------------+--------+-----+
5605 /// | Call overhead in Bytes | 4 | 4 |
5606 /// | Frame overhead in Bytes | 0 | 0 |
5607 /// | Stack fixup required | No | No |
5608 /// +-------------------------+--------+-----+
5610 /// \p MachineOutlinerThunk implies that the function is being created from
5611 /// a sequence of instructions ending in a call. The outlined function is
5612 /// called with a BL instruction, and the outlined function tail-calls the
5613 /// original call destination.
5617 /// I1 OUTLINED_FUNCTION:
5618 /// I2 --> BL OUTLINED_FUNCTION I1
5622 /// +-------------------------+--------+-----+
5623 /// | | Thumb2 | ARM |
5624 /// +-------------------------+--------+-----+
5625 /// | Call overhead in Bytes | 4 | 4 |
5626 /// | Frame overhead in Bytes | 0 | 0 |
5627 /// | Stack fixup required | No | No |
5628 /// +-------------------------+--------+-----+
5630 /// \p MachineOutlinerNoLRSave implies that the function should be called using
5631 /// a BL instruction, but doesn't require LR to be saved and restored. This
5632 /// happens when LR is known to be dead.
5636 /// I1 OUTLINED_FUNCTION:
5637 /// I2 --> BL OUTLINED_FUNCTION I1
5642 /// +-------------------------+--------+-----+
5643 /// | | Thumb2 | ARM |
5644 /// +-------------------------+--------+-----+
5645 /// | Call overhead in Bytes | 4 | 4 |
5646 /// | Frame overhead in Bytes | 4 | 4 |
5647 /// | Stack fixup required | No | No |
5648 /// +-------------------------+--------+-----+
5650 /// \p MachineOutlinerRegSave implies that the function should be called with a
5651 /// save and restore of LR to an available register. This allows us to avoid
5652 /// stack fixups. Note that this outlining variant is compatible with the
5657 /// I1 Save LR OUTLINED_FUNCTION:
5658 /// I2 --> BL OUTLINED_FUNCTION I1
5659 /// I3 Restore LR I2
5663 /// +-------------------------+--------+-----+
5664 /// | | Thumb2 | ARM |
5665 /// +-------------------------+--------+-----+
5666 /// | Call overhead in Bytes | 8 | 12 |
5667 /// | Frame overhead in Bytes | 2 | 4 |
5668 /// | Stack fixup required | No | No |
5669 /// +-------------------------+--------+-----+
5671 /// \p MachineOutlinerDefault implies that the function should be called with
5672 /// a save and restore of LR to the stack.
5676 /// I1 Save LR OUTLINED_FUNCTION:
5677 /// I2 --> BL OUTLINED_FUNCTION I1
5678 /// I3 Restore LR I2
5682 /// +-------------------------+--------+-----+
5683 /// | | Thumb2 | ARM |
5684 /// +-------------------------+--------+-----+
5685 /// | Call overhead in Bytes | 8 | 12 |
5686 /// | Frame overhead in Bytes | 2 | 4 |
5687 /// | Stack fixup required | Yes | Yes |
5688 /// +-------------------------+--------+-----+
5690 enum MachineOutlinerClass
{
5691 MachineOutlinerTailCall
,
5692 MachineOutlinerThunk
,
5693 MachineOutlinerNoLRSave
,
5694 MachineOutlinerRegSave
,
5695 MachineOutlinerDefault
5698 enum MachineOutlinerMBBFlags
{
5699 LRUnavailableSomewhere
= 0x2,
5701 UnsafeRegsDead
= 0x8
5704 struct OutlinerCosts
{
5705 const int CallTailCall
;
5706 const int FrameTailCall
;
5707 const int CallThunk
;
5708 const int FrameThunk
;
5709 const int CallNoLRSave
;
5710 const int FrameNoLRSave
;
5711 const int CallRegSave
;
5712 const int FrameRegSave
;
5713 const int CallDefault
;
5714 const int FrameDefault
;
5715 const int SaveRestoreLROnStack
;
5717 OutlinerCosts(const ARMSubtarget
&target
)
5718 : CallTailCall(target
.isThumb() ? 4 : 4),
5719 FrameTailCall(target
.isThumb() ? 0 : 0),
5720 CallThunk(target
.isThumb() ? 4 : 4),
5721 FrameThunk(target
.isThumb() ? 0 : 0),
5722 CallNoLRSave(target
.isThumb() ? 4 : 4),
5723 FrameNoLRSave(target
.isThumb() ? 4 : 4),
5724 CallRegSave(target
.isThumb() ? 8 : 12),
5725 FrameRegSave(target
.isThumb() ? 2 : 4),
5726 CallDefault(target
.isThumb() ? 8 : 12),
5727 FrameDefault(target
.isThumb() ? 2 : 4),
5728 SaveRestoreLROnStack(target
.isThumb() ? 8 : 8) {}
5732 ARMBaseInstrInfo::findRegisterToSaveLRTo(const outliner::Candidate
&C
) const {
5733 assert(C
.LRUWasSet
&& "LRU wasn't set?");
5734 MachineFunction
*MF
= C
.getMF();
5735 const ARMBaseRegisterInfo
*ARI
= static_cast<const ARMBaseRegisterInfo
*>(
5736 MF
->getSubtarget().getRegisterInfo());
5738 BitVector regsReserved
= ARI
->getReservedRegs(*MF
);
5739 // Check if there is an available register across the sequence that we can
5741 for (unsigned Reg
: ARM::rGPRRegClass
) {
5742 if (!(Reg
< regsReserved
.size() && regsReserved
.test(Reg
)) &&
5743 Reg
!= ARM::LR
&& // LR is not reserved, but don't use it.
5744 Reg
!= ARM::R12
&& // R12 is not guaranteed to be preserved.
5745 C
.LRU
.available(Reg
) && C
.UsedInSequence
.available(Reg
))
5749 // No suitable register. Return 0.
5753 // Compute liveness of LR at the point after the interval [I, E), which
5754 // denotes a *backward* iteration through instructions. Used only for return
5755 // basic blocks, which do not end with a tail call.
5756 static bool isLRAvailable(const TargetRegisterInfo
&TRI
,
5757 MachineBasicBlock::reverse_iterator I
,
5758 MachineBasicBlock::reverse_iterator E
) {
5759 // At the end of the function LR dead.
5761 for (; I
!= E
; ++I
) {
5762 const MachineInstr
&MI
= *I
;
5764 // Check defs of LR.
5765 if (MI
.modifiesRegister(ARM::LR
, &TRI
))
5768 // Check uses of LR.
5769 unsigned Opcode
= MI
.getOpcode();
5770 if (Opcode
== ARM::BX_RET
|| Opcode
== ARM::MOVPCLR
||
5771 Opcode
== ARM::SUBS_PC_LR
|| Opcode
== ARM::tBX_RET
||
5772 Opcode
== ARM::tBXNS_RET
) {
5773 // These instructions use LR, but it's not an (explicit or implicit)
5778 if (MI
.readsRegister(ARM::LR
, &TRI
))
5784 outliner::OutlinedFunction
ARMBaseInstrInfo::getOutliningCandidateInfo(
5785 std::vector
<outliner::Candidate
> &RepeatedSequenceLocs
) const {
5786 outliner::Candidate
&FirstCand
= RepeatedSequenceLocs
[0];
5787 unsigned SequenceSize
=
5788 std::accumulate(FirstCand
.front(), std::next(FirstCand
.back()), 0,
5789 [this](unsigned Sum
, const MachineInstr
&MI
) {
5790 return Sum
+ getInstSizeInBytes(MI
);
5793 // Properties about candidate MBBs that hold for all of them.
5794 unsigned FlagsSetInAll
= 0xF;
5796 // Compute liveness information for each candidate, and set FlagsSetInAll.
5797 const TargetRegisterInfo
&TRI
= getRegisterInfo();
5799 RepeatedSequenceLocs
.begin(), RepeatedSequenceLocs
.end(),
5800 [&FlagsSetInAll
](outliner::Candidate
&C
) { FlagsSetInAll
&= C
.Flags
; });
5802 // According to the ARM Procedure Call Standard, the following are
5803 // undefined on entry/exit from a function call:
5805 // * Register R12(IP),
5806 // * Condition codes (and thus the CPSR register)
5808 // Since we control the instructions which are part of the outlined regions
5809 // we don't need to be fully compliant with the AAPCS, but we have to
5810 // guarantee that if a veneer is inserted at link time the code is still
5811 // correct. Because of this, we can't outline any sequence of instructions
5812 // where one of these registers is live into/across it. Thus, we need to
5813 // delete those candidates.
5814 auto CantGuaranteeValueAcrossCall
= [&TRI
](outliner::Candidate
&C
) {
5815 // If the unsafe registers in this block are all dead, then we don't need
5816 // to compute liveness here.
5817 if (C
.Flags
& UnsafeRegsDead
)
5820 LiveRegUnits LRU
= C
.LRU
;
5821 return (!LRU
.available(ARM::R12
) || !LRU
.available(ARM::CPSR
));
5824 // Are there any candidates where those registers are live?
5825 if (!(FlagsSetInAll
& UnsafeRegsDead
)) {
5826 // Erase every candidate that violates the restrictions above. (It could be
5827 // true that we have viable candidates, so it's not worth bailing out in
5828 // the case that, say, 1 out of 20 candidates violate the restructions.)
5829 llvm::erase_if(RepeatedSequenceLocs
, CantGuaranteeValueAcrossCall
);
5831 // If the sequence doesn't have enough candidates left, then we're done.
5832 if (RepeatedSequenceLocs
.size() < 2)
5833 return outliner::OutlinedFunction();
5836 // At this point, we have only "safe" candidates to outline. Figure out
5837 // frame + call instruction information.
5839 unsigned LastInstrOpcode
= RepeatedSequenceLocs
[0].back()->getOpcode();
5841 // Helper lambda which sets call information for every candidate.
5842 auto SetCandidateCallInfo
=
5843 [&RepeatedSequenceLocs
](unsigned CallID
, unsigned NumBytesForCall
) {
5844 for (outliner::Candidate
&C
: RepeatedSequenceLocs
)
5845 C
.setCallInfo(CallID
, NumBytesForCall
);
5848 OutlinerCosts
Costs(Subtarget
);
5849 unsigned FrameID
= MachineOutlinerDefault
;
5850 unsigned NumBytesToCreateFrame
= Costs
.FrameDefault
;
5852 // If the last instruction in any candidate is a terminator, then we should
5853 // tail call all of the candidates.
5854 if (RepeatedSequenceLocs
[0].back()->isTerminator()) {
5855 FrameID
= MachineOutlinerTailCall
;
5856 NumBytesToCreateFrame
= Costs
.FrameTailCall
;
5857 SetCandidateCallInfo(MachineOutlinerTailCall
, Costs
.CallTailCall
);
5858 } else if (LastInstrOpcode
== ARM::BL
|| LastInstrOpcode
== ARM::BLX
||
5859 LastInstrOpcode
== ARM::BLX_noip
|| LastInstrOpcode
== ARM::tBL
||
5860 LastInstrOpcode
== ARM::tBLXr
||
5861 LastInstrOpcode
== ARM::tBLXr_noip
||
5862 LastInstrOpcode
== ARM::tBLXi
) {
5863 FrameID
= MachineOutlinerThunk
;
5864 NumBytesToCreateFrame
= Costs
.FrameThunk
;
5865 SetCandidateCallInfo(MachineOutlinerThunk
, Costs
.CallThunk
);
5867 // We need to decide how to emit calls + frames. We can always emit the same
5868 // frame if we don't need to save to the stack. If we have to save to the
5869 // stack, then we need a different frame.
5870 unsigned NumBytesNoStackCalls
= 0;
5871 std::vector
<outliner::Candidate
> CandidatesWithoutStackFixups
;
5873 for (outliner::Candidate
&C
: RepeatedSequenceLocs
) {
5875 // LR liveness is overestimated in return blocks, unless they end with a
5877 const auto Last
= C
.getMBB()->rbegin();
5878 const bool LRIsAvailable
=
5879 C
.getMBB()->isReturnBlock() && !Last
->isCall()
5880 ? isLRAvailable(TRI
, Last
,
5881 (MachineBasicBlock::reverse_iterator
)C
.front())
5882 : C
.LRU
.available(ARM::LR
);
5883 if (LRIsAvailable
) {
5884 FrameID
= MachineOutlinerNoLRSave
;
5885 NumBytesNoStackCalls
+= Costs
.CallNoLRSave
;
5886 C
.setCallInfo(MachineOutlinerNoLRSave
, Costs
.CallNoLRSave
);
5887 CandidatesWithoutStackFixups
.push_back(C
);
5890 // Is an unused register available? If so, we won't modify the stack, so
5891 // we can outline with the same frame type as those that don't save LR.
5892 else if (findRegisterToSaveLRTo(C
)) {
5893 FrameID
= MachineOutlinerRegSave
;
5894 NumBytesNoStackCalls
+= Costs
.CallRegSave
;
5895 C
.setCallInfo(MachineOutlinerRegSave
, Costs
.CallRegSave
);
5896 CandidatesWithoutStackFixups
.push_back(C
);
5899 // Is SP used in the sequence at all? If not, we don't have to modify
5900 // the stack, so we are guaranteed to get the same frame.
5901 else if (C
.UsedInSequence
.available(ARM::SP
)) {
5902 NumBytesNoStackCalls
+= Costs
.CallDefault
;
5903 C
.setCallInfo(MachineOutlinerDefault
, Costs
.CallDefault
);
5904 CandidatesWithoutStackFixups
.push_back(C
);
5907 // If we outline this, we need to modify the stack. Pretend we don't
5908 // outline this by saving all of its bytes.
5910 NumBytesNoStackCalls
+= SequenceSize
;
5913 // If there are no places where we have to save LR, then note that we don't
5914 // have to update the stack. Otherwise, give every candidate the default
5916 if (NumBytesNoStackCalls
<=
5917 RepeatedSequenceLocs
.size() * Costs
.CallDefault
) {
5918 RepeatedSequenceLocs
= CandidatesWithoutStackFixups
;
5919 FrameID
= MachineOutlinerNoLRSave
;
5921 SetCandidateCallInfo(MachineOutlinerDefault
, Costs
.CallDefault
);
5924 // Does every candidate's MBB contain a call? If so, then we might have a
5925 // call in the range.
5926 if (FlagsSetInAll
& MachineOutlinerMBBFlags::HasCalls
) {
5927 // check if the range contains a call. These require a save + restore of
5928 // the link register.
5929 if (std::any_of(FirstCand
.front(), FirstCand
.back(),
5930 [](const MachineInstr
&MI
) { return MI
.isCall(); }))
5931 NumBytesToCreateFrame
+= Costs
.SaveRestoreLROnStack
;
5933 // Handle the last instruction separately. If it is tail call, then the
5934 // last instruction is a call, we don't want to save + restore in this
5935 // case. However, it could be possible that the last instruction is a
5936 // call without it being valid to tail call this sequence. We should
5937 // consider this as well.
5938 else if (FrameID
!= MachineOutlinerThunk
&&
5939 FrameID
!= MachineOutlinerTailCall
&& FirstCand
.back()->isCall())
5940 NumBytesToCreateFrame
+= Costs
.SaveRestoreLROnStack
;
5943 return outliner::OutlinedFunction(RepeatedSequenceLocs
, SequenceSize
,
5944 NumBytesToCreateFrame
, FrameID
);
5947 bool ARMBaseInstrInfo::checkAndUpdateStackOffset(MachineInstr
*MI
,
5950 int SPIdx
= MI
->findRegisterUseOperandIdx(ARM::SP
);
5951 unsigned AddrMode
= (MI
->getDesc().TSFlags
& ARMII::AddrModeMask
);
5955 else if (SPIdx
!= 1 && (AddrMode
!= ARMII::AddrModeT2_i8s4
|| SPIdx
!= 2))
5956 // If SP is not the base register we can't do much
5959 // Stack might be involved but addressing mode doesn't handle any offset.
5960 // Rq: AddrModeT1_[1|2|4] don't operate on SP
5961 if (AddrMode
== ARMII::AddrMode1
// Arithmetic instructions
5962 || AddrMode
== ARMII::AddrMode4
// Load/Store Multiple
5963 || AddrMode
== ARMII::AddrMode6
// Neon Load/Store Multiple
5964 || AddrMode
== ARMII::AddrModeT2_so
// SP can't be used as based register
5965 || AddrMode
== ARMII::AddrModeT2_pc
// PCrel access
5966 || AddrMode
== ARMII::AddrMode2
// Used by PRE and POST indexed LD/ST
5967 || AddrMode
== ARMII::AddrModeT2_i7
// v8.1-M MVE
5968 || AddrMode
== ARMII::AddrModeT2_i7s2
// v8.1-M MVE
5969 || AddrMode
== ARMII::AddrModeT2_i7s4
// v8.1-M sys regs VLDR/VSTR
5970 || AddrMode
== ARMII::AddrModeNone
)
5973 unsigned NumOps
= MI
->getDesc().getNumOperands();
5974 unsigned ImmIdx
= NumOps
- 3;
5976 const MachineOperand
&Offset
= MI
->getOperand(ImmIdx
);
5977 assert(Offset
.isImm() && "Is not an immediate");
5978 int64_t OffVal
= Offset
.getImm();
5981 // Don't override data if the are below SP.
5984 unsigned NumBits
= 0;
5988 case ARMII::AddrMode3
:
5989 if (ARM_AM::getAM3Op(OffVal
) == ARM_AM::sub
)
5991 OffVal
= ARM_AM::getAM3Offset(OffVal
);
5994 case ARMII::AddrMode5
:
5995 if (ARM_AM::getAM5Op(OffVal
) == ARM_AM::sub
)
5997 OffVal
= ARM_AM::getAM5Offset(OffVal
);
6001 case ARMII::AddrMode5FP16
:
6002 if (ARM_AM::getAM5FP16Op(OffVal
) == ARM_AM::sub
)
6004 OffVal
= ARM_AM::getAM5FP16Offset(OffVal
);
6008 case ARMII::AddrModeT2_i8
:
6011 case ARMII::AddrModeT2_i8s4
:
6012 // FIXME: Values are already scaled in this addressing mode.
6013 assert((Fixup
& 3) == 0 && "Can't encode this offset!");
6016 case ARMII::AddrModeT2_ldrex
:
6020 case ARMII::AddrModeT2_i12
:
6021 case ARMII::AddrMode_i12
:
6024 case ARMII::AddrModeT1_s
: // SP-relative LD/ST
6029 llvm_unreachable("Unsupported addressing mode!");
6031 // Make sure the offset is encodable for instructions that scale the
6033 assert(((OffVal
* Scale
+ Fixup
) & (Scale
- 1)) == 0 &&
6034 "Can't encode this offset!");
6035 OffVal
+= Fixup
/ Scale
;
6037 unsigned Mask
= (1 << NumBits
) - 1;
6039 if (OffVal
<= Mask
) {
6041 MI
->getOperand(ImmIdx
).setImm(OffVal
);
6049 bool ARMBaseInstrInfo::isFunctionSafeToOutlineFrom(
6050 MachineFunction
&MF
, bool OutlineFromLinkOnceODRs
) const {
6051 const Function
&F
= MF
.getFunction();
6053 // Can F be deduplicated by the linker? If it can, don't outline from it.
6054 if (!OutlineFromLinkOnceODRs
&& F
.hasLinkOnceODRLinkage())
6057 // Don't outline from functions with section markings; the program could
6058 // expect that all the code is in the named section.
6059 // FIXME: Allow outlining from multiple functions with the same section
6064 // FIXME: Thumb1 outlining is not handled
6065 if (MF
.getInfo
<ARMFunctionInfo
>()->isThumb1OnlyFunction())
6068 // It's safe to outline from MF.
6072 bool ARMBaseInstrInfo::isMBBSafeToOutlineFrom(MachineBasicBlock
&MBB
,
6073 unsigned &Flags
) const {
6074 // Check if LR is available through all of the MBB. If it's not, then set
6076 assert(MBB
.getParent()->getRegInfo().tracksLiveness() &&
6077 "Suitable Machine Function for outlining must track liveness");
6079 LiveRegUnits
LRU(getRegisterInfo());
6081 std::for_each(MBB
.rbegin(), MBB
.rend(),
6082 [&LRU
](MachineInstr
&MI
) { LRU
.accumulate(MI
); });
6084 // Check if each of the unsafe registers are available...
6085 bool R12AvailableInBlock
= LRU
.available(ARM::R12
);
6086 bool CPSRAvailableInBlock
= LRU
.available(ARM::CPSR
);
6088 // If all of these are dead (and not live out), we know we don't have to check
6090 if (R12AvailableInBlock
&& CPSRAvailableInBlock
)
6091 Flags
|= MachineOutlinerMBBFlags::UnsafeRegsDead
;
6093 // Now, add the live outs to the set.
6094 LRU
.addLiveOuts(MBB
);
6096 // If any of these registers is available in the MBB, but also a live out of
6097 // the block, then we know outlining is unsafe.
6098 if (R12AvailableInBlock
&& !LRU
.available(ARM::R12
))
6100 if (CPSRAvailableInBlock
&& !LRU
.available(ARM::CPSR
))
6103 // Check if there's a call inside this MachineBasicBlock. If there is, then
6105 if (any_of(MBB
, [](MachineInstr
&MI
) { return MI
.isCall(); }))
6106 Flags
|= MachineOutlinerMBBFlags::HasCalls
;
6108 // LR liveness is overestimated in return blocks.
6110 bool LRIsAvailable
=
6111 MBB
.isReturnBlock() && !MBB
.back().isCall()
6112 ? isLRAvailable(getRegisterInfo(), MBB
.rbegin(), MBB
.rend())
6113 : LRU
.available(ARM::LR
);
6115 Flags
|= MachineOutlinerMBBFlags::LRUnavailableSomewhere
;
6121 ARMBaseInstrInfo::getOutliningType(MachineBasicBlock::iterator
&MIT
,
6122 unsigned Flags
) const {
6123 MachineInstr
&MI
= *MIT
;
6124 const TargetRegisterInfo
*TRI
= &getRegisterInfo();
6126 // Be conservative with inline ASM
6127 if (MI
.isInlineAsm())
6128 return outliner::InstrType::Illegal
;
6130 // Don't allow debug values to impact outlining type.
6131 if (MI
.isDebugInstr() || MI
.isIndirectDebugValue())
6132 return outliner::InstrType::Invisible
;
6134 // At this point, KILL or IMPLICIT_DEF instructions don't really tell us much
6135 // so we can go ahead and skip over them.
6136 if (MI
.isKill() || MI
.isImplicitDef())
6137 return outliner::InstrType::Invisible
;
6139 // PIC instructions contain labels, outlining them would break offset
6140 // computing. unsigned Opc = MI.getOpcode();
6141 unsigned Opc
= MI
.getOpcode();
6142 if (Opc
== ARM::tPICADD
|| Opc
== ARM::PICADD
|| Opc
== ARM::PICSTR
||
6143 Opc
== ARM::PICSTRB
|| Opc
== ARM::PICSTRH
|| Opc
== ARM::PICLDR
||
6144 Opc
== ARM::PICLDRB
|| Opc
== ARM::PICLDRH
|| Opc
== ARM::PICLDRSB
||
6145 Opc
== ARM::PICLDRSH
|| Opc
== ARM::t2LDRpci_pic
||
6146 Opc
== ARM::t2MOVi16_ga_pcrel
|| Opc
== ARM::t2MOVTi16_ga_pcrel
||
6147 Opc
== ARM::t2MOV_ga_pcrel
)
6148 return outliner::InstrType::Illegal
;
6150 // Be conservative with ARMv8.1 MVE instructions.
6151 if (Opc
== ARM::t2BF_LabelPseudo
|| Opc
== ARM::t2DoLoopStart
||
6152 Opc
== ARM::t2DoLoopStartTP
|| Opc
== ARM::t2WhileLoopStart
||
6153 Opc
== ARM::t2WhileLoopStartLR
|| Opc
== ARM::t2WhileLoopStartTP
||
6154 Opc
== ARM::t2LoopDec
|| Opc
== ARM::t2LoopEnd
||
6155 Opc
== ARM::t2LoopEndDec
)
6156 return outliner::InstrType::Illegal
;
6158 const MCInstrDesc
&MCID
= MI
.getDesc();
6159 uint64_t MIFlags
= MCID
.TSFlags
;
6160 if ((MIFlags
& ARMII::DomainMask
) == ARMII::DomainMVE
)
6161 return outliner::InstrType::Illegal
;
6163 // Is this a terminator for a basic block?
6164 if (MI
.isTerminator()) {
6165 // Don't outline if the branch is not unconditional.
6166 if (isPredicated(MI
))
6167 return outliner::InstrType::Illegal
;
6169 // Is this the end of a function?
6170 if (MI
.getParent()->succ_empty())
6171 return outliner::InstrType::Legal
;
6173 // It's not, so don't outline it.
6174 return outliner::InstrType::Illegal
;
6177 // Make sure none of the operands are un-outlinable.
6178 for (const MachineOperand
&MOP
: MI
.operands()) {
6179 if (MOP
.isCPI() || MOP
.isJTI() || MOP
.isCFIIndex() || MOP
.isFI() ||
6180 MOP
.isTargetIndex())
6181 return outliner::InstrType::Illegal
;
6184 // Don't outline if link register or program counter value are used.
6185 if (MI
.readsRegister(ARM::LR
, TRI
) || MI
.readsRegister(ARM::PC
, TRI
))
6186 return outliner::InstrType::Illegal
;
6189 // Get the function associated with the call. Look at each operand and find
6190 // the one that represents the calle and get its name.
6191 const Function
*Callee
= nullptr;
6192 for (const MachineOperand
&MOP
: MI
.operands()) {
6193 if (MOP
.isGlobal()) {
6194 Callee
= dyn_cast
<Function
>(MOP
.getGlobal());
6199 // Dont't outline calls to "mcount" like functions, in particular Linux
6200 // kernel function tracing relies on it.
6202 (Callee
->getName() == "\01__gnu_mcount_nc" ||
6203 Callee
->getName() == "\01mcount" || Callee
->getName() == "__mcount"))
6204 return outliner::InstrType::Illegal
;
6206 // If we don't know anything about the callee, assume it depends on the
6207 // stack layout of the caller. In that case, it's only legal to outline
6208 // as a tail-call. Explicitly list the call instructions we know about so
6209 // we don't get unexpected results with call pseudo-instructions.
6210 auto UnknownCallOutlineType
= outliner::InstrType::Illegal
;
6211 if (Opc
== ARM::BL
|| Opc
== ARM::tBL
|| Opc
== ARM::BLX
||
6212 Opc
== ARM::BLX_noip
|| Opc
== ARM::tBLXr
|| Opc
== ARM::tBLXr_noip
||
6214 UnknownCallOutlineType
= outliner::InstrType::LegalTerminator
;
6217 return UnknownCallOutlineType
;
6219 // We have a function we have information about. Check if it's something we
6220 // can safely outline.
6221 MachineFunction
*MF
= MI
.getParent()->getParent();
6222 MachineFunction
*CalleeMF
= MF
->getMMI().getMachineFunction(*Callee
);
6224 // We don't know what's going on with the callee at all. Don't touch it.
6226 return UnknownCallOutlineType
;
6228 // Check if we know anything about the callee saves on the function. If we
6229 // don't, then don't touch it, since that implies that we haven't computed
6230 // anything about its stack frame yet.
6231 MachineFrameInfo
&MFI
= CalleeMF
->getFrameInfo();
6232 if (!MFI
.isCalleeSavedInfoValid() || MFI
.getStackSize() > 0 ||
6233 MFI
.getNumObjects() > 0)
6234 return UnknownCallOutlineType
;
6236 // At this point, we can say that CalleeMF ought to not pass anything on the
6237 // stack. Therefore, we can outline it.
6238 return outliner::InstrType::Legal
;
6241 // Since calls are handled, don't touch LR or PC
6242 if (MI
.modifiesRegister(ARM::LR
, TRI
) || MI
.modifiesRegister(ARM::PC
, TRI
))
6243 return outliner::InstrType::Illegal
;
6245 // Does this use the stack?
6246 if (MI
.modifiesRegister(ARM::SP
, TRI
) || MI
.readsRegister(ARM::SP
, TRI
)) {
6247 // True if there is no chance that any outlined candidate from this range
6248 // could require stack fixups. That is, both
6249 // * LR is available in the range (No save/restore around call)
6250 // * The range doesn't include calls (No save/restore in outlined frame)
6252 // FIXME: This is very restrictive; the flags check the whole block,
6253 // not just the bit we will try to outline.
6254 bool MightNeedStackFixUp
=
6255 (Flags
& (MachineOutlinerMBBFlags::LRUnavailableSomewhere
|
6256 MachineOutlinerMBBFlags::HasCalls
));
6258 if (!MightNeedStackFixUp
)
6259 return outliner::InstrType::Legal
;
6261 // Any modification of SP will break our code to save/restore LR.
6262 // FIXME: We could handle some instructions which add a constant offset to
6263 // SP, with a bit more work.
6264 if (MI
.modifiesRegister(ARM::SP
, TRI
))
6265 return outliner::InstrType::Illegal
;
6267 // At this point, we have a stack instruction that we might need to fix up.
6268 // up. We'll handle it if it's a load or store.
6269 if (checkAndUpdateStackOffset(&MI
, Subtarget
.getStackAlignment().value(),
6271 return outliner::InstrType::Legal
;
6273 // We can't fix it up, so don't outline it.
6274 return outliner::InstrType::Illegal
;
6277 // Be conservative with IT blocks.
6278 if (MI
.readsRegister(ARM::ITSTATE
, TRI
) ||
6279 MI
.modifiesRegister(ARM::ITSTATE
, TRI
))
6280 return outliner::InstrType::Illegal
;
6282 // Don't outline positions.
6283 if (MI
.isPosition())
6284 return outliner::InstrType::Illegal
;
6286 return outliner::InstrType::Legal
;
6289 void ARMBaseInstrInfo::fixupPostOutline(MachineBasicBlock
&MBB
) const {
6290 for (MachineInstr
&MI
: MBB
) {
6291 checkAndUpdateStackOffset(&MI
, Subtarget
.getStackAlignment().value(), true);
6295 void ARMBaseInstrInfo::saveLROnStack(MachineBasicBlock
&MBB
,
6296 MachineBasicBlock::iterator It
) const {
6297 unsigned Opc
= Subtarget
.isThumb() ? ARM::t2STR_PRE
: ARM::STR_PRE_IMM
;
6298 int Align
= -Subtarget
.getStackAlignment().value();
6299 BuildMI(MBB
, It
, DebugLoc(), get(Opc
), ARM::SP
)
6300 .addReg(ARM::LR
, RegState::Kill
)
6303 .add(predOps(ARMCC::AL
));
6306 void ARMBaseInstrInfo::emitCFIForLRSaveOnStack(
6307 MachineBasicBlock
&MBB
, MachineBasicBlock::iterator It
) const {
6308 MachineFunction
&MF
= *MBB
.getParent();
6309 const MCRegisterInfo
*MRI
= Subtarget
.getRegisterInfo();
6310 unsigned DwarfLR
= MRI
->getDwarfRegNum(ARM::LR
, true);
6311 int Align
= Subtarget
.getStackAlignment().value();
6312 // Add a CFI saying the stack was moved down.
6313 int64_t StackPosEntry
=
6314 MF
.addFrameInst(MCCFIInstruction::cfiDefCfaOffset(nullptr, Align
));
6315 BuildMI(MBB
, It
, DebugLoc(), get(ARM::CFI_INSTRUCTION
))
6316 .addCFIIndex(StackPosEntry
)
6317 .setMIFlags(MachineInstr::FrameSetup
);
6319 // Add a CFI saying that the LR that we want to find is now higher than
6321 int64_t LRPosEntry
=
6322 MF
.addFrameInst(MCCFIInstruction::createOffset(nullptr, DwarfLR
, -Align
));
6323 BuildMI(MBB
, It
, DebugLoc(), get(ARM::CFI_INSTRUCTION
))
6324 .addCFIIndex(LRPosEntry
)
6325 .setMIFlags(MachineInstr::FrameSetup
);
6328 void ARMBaseInstrInfo::emitCFIForLRSaveToReg(MachineBasicBlock
&MBB
,
6329 MachineBasicBlock::iterator It
,
6330 Register Reg
) const {
6331 MachineFunction
&MF
= *MBB
.getParent();
6332 const MCRegisterInfo
*MRI
= Subtarget
.getRegisterInfo();
6333 unsigned DwarfLR
= MRI
->getDwarfRegNum(ARM::LR
, true);
6334 unsigned DwarfReg
= MRI
->getDwarfRegNum(Reg
, true);
6336 int64_t LRPosEntry
= MF
.addFrameInst(
6337 MCCFIInstruction::createRegister(nullptr, DwarfLR
, DwarfReg
));
6338 BuildMI(MBB
, It
, DebugLoc(), get(ARM::CFI_INSTRUCTION
))
6339 .addCFIIndex(LRPosEntry
)
6340 .setMIFlags(MachineInstr::FrameSetup
);
6343 void ARMBaseInstrInfo::restoreLRFromStack(
6344 MachineBasicBlock
&MBB
, MachineBasicBlock::iterator It
) const {
6345 unsigned Opc
= Subtarget
.isThumb() ? ARM::t2LDR_POST
: ARM::LDR_POST_IMM
;
6346 MachineInstrBuilder MIB
= BuildMI(MBB
, It
, DebugLoc(), get(Opc
), ARM::LR
)
6347 .addReg(ARM::SP
, RegState::Define
)
6349 if (!Subtarget
.isThumb())
6351 MIB
.addImm(Subtarget
.getStackAlignment().value()).add(predOps(ARMCC::AL
));
6354 void ARMBaseInstrInfo::emitCFIForLRRestoreFromStack(
6355 MachineBasicBlock
&MBB
, MachineBasicBlock::iterator It
) const {
6356 // Now stack has moved back up...
6357 MachineFunction
&MF
= *MBB
.getParent();
6358 const MCRegisterInfo
*MRI
= Subtarget
.getRegisterInfo();
6359 unsigned DwarfLR
= MRI
->getDwarfRegNum(ARM::LR
, true);
6360 int64_t StackPosEntry
=
6361 MF
.addFrameInst(MCCFIInstruction::cfiDefCfaOffset(nullptr, 0));
6362 BuildMI(MBB
, It
, DebugLoc(), get(ARM::CFI_INSTRUCTION
))
6363 .addCFIIndex(StackPosEntry
)
6364 .setMIFlags(MachineInstr::FrameDestroy
);
6366 // ... and we have restored LR.
6367 int64_t LRPosEntry
=
6368 MF
.addFrameInst(MCCFIInstruction::createRestore(nullptr, DwarfLR
));
6369 BuildMI(MBB
, It
, DebugLoc(), get(ARM::CFI_INSTRUCTION
))
6370 .addCFIIndex(LRPosEntry
)
6371 .setMIFlags(MachineInstr::FrameDestroy
);
6374 void ARMBaseInstrInfo::emitCFIForLRRestoreFromReg(
6375 MachineBasicBlock
&MBB
, MachineBasicBlock::iterator It
) const {
6376 MachineFunction
&MF
= *MBB
.getParent();
6377 const MCRegisterInfo
*MRI
= Subtarget
.getRegisterInfo();
6378 unsigned DwarfLR
= MRI
->getDwarfRegNum(ARM::LR
, true);
6380 int64_t LRPosEntry
=
6381 MF
.addFrameInst(MCCFIInstruction::createRestore(nullptr, DwarfLR
));
6382 BuildMI(MBB
, It
, DebugLoc(), get(ARM::CFI_INSTRUCTION
))
6383 .addCFIIndex(LRPosEntry
)
6384 .setMIFlags(MachineInstr::FrameDestroy
);
6387 void ARMBaseInstrInfo::buildOutlinedFrame(
6388 MachineBasicBlock
&MBB
, MachineFunction
&MF
,
6389 const outliner::OutlinedFunction
&OF
) const {
6390 // For thunk outlining, rewrite the last instruction from a call to a
6392 if (OF
.FrameConstructionID
== MachineOutlinerThunk
) {
6393 MachineInstr
*Call
= &*--MBB
.instr_end();
6394 bool isThumb
= Subtarget
.isThumb();
6395 unsigned FuncOp
= isThumb
? 2 : 0;
6396 unsigned Opc
= Call
->getOperand(FuncOp
).isReg()
6397 ? isThumb
? ARM::tTAILJMPr
: ARM::TAILJMPr
6398 : isThumb
? Subtarget
.isTargetMachO() ? ARM::tTAILJMPd
6401 MachineInstrBuilder MIB
= BuildMI(MBB
, MBB
.end(), DebugLoc(), get(Opc
))
6402 .add(Call
->getOperand(FuncOp
));
6403 if (isThumb
&& !Call
->getOperand(FuncOp
).isReg())
6404 MIB
.add(predOps(ARMCC::AL
));
6405 Call
->eraseFromParent();
6408 // Is there a call in the outlined range?
6409 auto IsNonTailCall
= [](MachineInstr
&MI
) {
6410 return MI
.isCall() && !MI
.isReturn();
6412 if (llvm::any_of(MBB
.instrs(), IsNonTailCall
)) {
6413 MachineBasicBlock::iterator It
= MBB
.begin();
6414 MachineBasicBlock::iterator Et
= MBB
.end();
6416 if (OF
.FrameConstructionID
== MachineOutlinerTailCall
||
6417 OF
.FrameConstructionID
== MachineOutlinerThunk
)
6418 Et
= std::prev(MBB
.end());
6420 // We have to save and restore LR, we need to add it to the liveins if it
6421 // is not already part of the set. This is suffient since outlined
6422 // functions only have one block.
6423 if (!MBB
.isLiveIn(ARM::LR
))
6424 MBB
.addLiveIn(ARM::LR
);
6426 // Insert a save before the outlined region
6427 saveLROnStack(MBB
, It
);
6428 emitCFIForLRSaveOnStack(MBB
, It
);
6430 // Fix up the instructions in the range, since we're going to modify the
6432 assert(OF
.FrameConstructionID
!= MachineOutlinerDefault
&&
6433 "Can only fix up stack references once");
6434 fixupPostOutline(MBB
);
6436 // Insert a restore before the terminator for the function. Restore LR.
6437 restoreLRFromStack(MBB
, Et
);
6438 emitCFIForLRRestoreFromStack(MBB
, Et
);
6441 // If this is a tail call outlined function, then there's already a return.
6442 if (OF
.FrameConstructionID
== MachineOutlinerTailCall
||
6443 OF
.FrameConstructionID
== MachineOutlinerThunk
)
6446 // Here we have to insert the return ourselves. Get the correct opcode from
6447 // current feature set.
6448 BuildMI(MBB
, MBB
.end(), DebugLoc(), get(Subtarget
.getReturnOpcode()))
6449 .add(predOps(ARMCC::AL
));
6451 // Did we have to modify the stack by saving the link register?
6452 if (OF
.FrameConstructionID
!= MachineOutlinerDefault
&&
6453 OF
.Candidates
[0].CallConstructionID
!= MachineOutlinerDefault
)
6456 // We modified the stack.
6457 // Walk over the basic block and fix up all the stack accesses.
6458 fixupPostOutline(MBB
);
6461 MachineBasicBlock::iterator
ARMBaseInstrInfo::insertOutlinedCall(
6462 Module
&M
, MachineBasicBlock
&MBB
, MachineBasicBlock::iterator
&It
,
6463 MachineFunction
&MF
, const outliner::Candidate
&C
) const {
6464 MachineInstrBuilder MIB
;
6465 MachineBasicBlock::iterator CallPt
;
6467 bool isThumb
= Subtarget
.isThumb();
6469 // Are we tail calling?
6470 if (C
.CallConstructionID
== MachineOutlinerTailCall
) {
6471 // If yes, then we can just branch to the label.
6473 ? Subtarget
.isTargetMachO() ? ARM::tTAILJMPd
: ARM::tTAILJMPdND
6475 MIB
= BuildMI(MF
, DebugLoc(), get(Opc
))
6476 .addGlobalAddress(M
.getNamedValue(MF
.getName()));
6478 MIB
.add(predOps(ARMCC::AL
));
6479 It
= MBB
.insert(It
, MIB
);
6483 // Create the call instruction.
6484 Opc
= isThumb
? ARM::tBL
: ARM::BL
;
6485 MachineInstrBuilder CallMIB
= BuildMI(MF
, DebugLoc(), get(Opc
));
6487 CallMIB
.add(predOps(ARMCC::AL
));
6488 CallMIB
.addGlobalAddress(M
.getNamedValue(MF
.getName()));
6490 if (C
.CallConstructionID
== MachineOutlinerNoLRSave
||
6491 C
.CallConstructionID
== MachineOutlinerThunk
) {
6492 // No, so just insert the call.
6493 It
= MBB
.insert(It
, CallMIB
);
6497 const ARMFunctionInfo
&AFI
= *C
.getMF()->getInfo
<ARMFunctionInfo
>();
6498 // Can we save to a register?
6499 if (C
.CallConstructionID
== MachineOutlinerRegSave
) {
6500 unsigned Reg
= findRegisterToSaveLRTo(C
);
6501 assert(Reg
!= 0 && "No callee-saved register available?");
6503 // Save and restore LR from that register.
6504 copyPhysReg(MBB
, It
, DebugLoc(), Reg
, ARM::LR
, true);
6505 if (!AFI
.isLRSpilled())
6506 emitCFIForLRSaveToReg(MBB
, It
, Reg
);
6507 CallPt
= MBB
.insert(It
, CallMIB
);
6508 copyPhysReg(MBB
, It
, DebugLoc(), ARM::LR
, Reg
, true);
6509 if (!AFI
.isLRSpilled())
6510 emitCFIForLRRestoreFromReg(MBB
, It
);
6514 // We have the default case. Save and restore from SP.
6515 if (!MBB
.isLiveIn(ARM::LR
))
6516 MBB
.addLiveIn(ARM::LR
);
6517 saveLROnStack(MBB
, It
);
6518 if (!AFI
.isLRSpilled())
6519 emitCFIForLRSaveOnStack(MBB
, It
);
6520 CallPt
= MBB
.insert(It
, CallMIB
);
6521 restoreLRFromStack(MBB
, It
);
6522 if (!AFI
.isLRSpilled())
6523 emitCFIForLRRestoreFromStack(MBB
, It
);
6528 bool ARMBaseInstrInfo::shouldOutlineFromFunctionByDefault(
6529 MachineFunction
&MF
) const {
6530 return Subtarget
.isMClass() && MF
.getFunction().hasMinSize();
6533 bool ARMBaseInstrInfo::isReallyTriviallyReMaterializable(const MachineInstr
&MI
,
6534 AAResults
*AA
) const {
6535 // Try hard to rematerialize any VCTPs because if we spill P0, it will block
6536 // the tail predication conversion. This means that the element count
6537 // register has to be live for longer, but that has to be better than
6538 // spill/restore and VPT predication.
6539 return isVCTP(&MI
) && !isPredicated(MI
);
6542 unsigned llvm::getBLXOpcode(const MachineFunction
&MF
) {
6543 return (MF
.getSubtarget
<ARMSubtarget
>().hardenSlsBlr()) ? ARM::BLX_noip
6547 unsigned llvm::gettBLXrOpcode(const MachineFunction
&MF
) {
6548 return (MF
.getSubtarget
<ARMSubtarget
>().hardenSlsBlr()) ? ARM::tBLXr_noip
6552 unsigned llvm::getBLXpredOpcode(const MachineFunction
&MF
) {
6553 return (MF
.getSubtarget
<ARMSubtarget
>().hardenSlsBlr()) ? ARM::BLX_pred_noip