1 //===- ARMLoadStoreOptimizer.cpp - ARM load / store opt. pass -------------===//
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 /// \file This file contains a pass that performs load / store related peephole
10 /// optimizations. This pass should be run after register allocation.
12 //===----------------------------------------------------------------------===//
15 #include "ARMBaseInstrInfo.h"
16 #include "ARMBaseRegisterInfo.h"
17 #include "ARMISelLowering.h"
18 #include "ARMMachineFunctionInfo.h"
19 #include "ARMSubtarget.h"
20 #include "MCTargetDesc/ARMAddressingModes.h"
21 #include "MCTargetDesc/ARMBaseInfo.h"
22 #include "Utils/ARMBaseInfo.h"
23 #include "llvm/ADT/ArrayRef.h"
24 #include "llvm/ADT/DenseMap.h"
25 #include "llvm/ADT/DenseSet.h"
26 #include "llvm/ADT/STLExtras.h"
27 #include "llvm/ADT/SetVector.h"
28 #include "llvm/ADT/SmallPtrSet.h"
29 #include "llvm/ADT/SmallSet.h"
30 #include "llvm/ADT/SmallVector.h"
31 #include "llvm/ADT/Statistic.h"
32 #include "llvm/ADT/iterator_range.h"
33 #include "llvm/Analysis/AliasAnalysis.h"
34 #include "llvm/CodeGen/LiveRegUnits.h"
35 #include "llvm/CodeGen/MachineBasicBlock.h"
36 #include "llvm/CodeGen/MachineDominators.h"
37 #include "llvm/CodeGen/MachineFrameInfo.h"
38 #include "llvm/CodeGen/MachineFunction.h"
39 #include "llvm/CodeGen/MachineFunctionPass.h"
40 #include "llvm/CodeGen/MachineInstr.h"
41 #include "llvm/CodeGen/MachineInstrBuilder.h"
42 #include "llvm/CodeGen/MachineMemOperand.h"
43 #include "llvm/CodeGen/MachineOperand.h"
44 #include "llvm/CodeGen/MachineRegisterInfo.h"
45 #include "llvm/CodeGen/RegisterClassInfo.h"
46 #include "llvm/CodeGen/TargetFrameLowering.h"
47 #include "llvm/CodeGen/TargetInstrInfo.h"
48 #include "llvm/CodeGen/TargetLowering.h"
49 #include "llvm/CodeGen/TargetRegisterInfo.h"
50 #include "llvm/CodeGen/TargetSubtargetInfo.h"
51 #include "llvm/IR/DataLayout.h"
52 #include "llvm/IR/DebugLoc.h"
53 #include "llvm/IR/Function.h"
54 #include "llvm/IR/Type.h"
55 #include "llvm/InitializePasses.h"
56 #include "llvm/MC/MCInstrDesc.h"
57 #include "llvm/Pass.h"
58 #include "llvm/Support/Allocator.h"
59 #include "llvm/Support/CommandLine.h"
60 #include "llvm/Support/Debug.h"
61 #include "llvm/Support/ErrorHandling.h"
62 #include "llvm/Support/raw_ostream.h"
72 #define DEBUG_TYPE "arm-ldst-opt"
74 STATISTIC(NumLDMGened
, "Number of ldm instructions generated");
75 STATISTIC(NumSTMGened
, "Number of stm instructions generated");
76 STATISTIC(NumVLDMGened
, "Number of vldm instructions generated");
77 STATISTIC(NumVSTMGened
, "Number of vstm instructions generated");
78 STATISTIC(NumLdStMoved
, "Number of load / store instructions moved");
79 STATISTIC(NumLDRDFormed
,"Number of ldrd created before allocation");
80 STATISTIC(NumSTRDFormed
,"Number of strd created before allocation");
81 STATISTIC(NumLDRD2LDM
, "Number of ldrd instructions turned back into ldm");
82 STATISTIC(NumSTRD2STM
, "Number of strd instructions turned back into stm");
83 STATISTIC(NumLDRD2LDR
, "Number of ldrd instructions turned back into ldr's");
84 STATISTIC(NumSTRD2STR
, "Number of strd instructions turned back into str's");
86 /// This switch disables formation of double/multi instructions that could
87 /// potentially lead to (new) alignment traps even with CCR.UNALIGN_TRP
88 /// disabled. This can be used to create libraries that are robust even when
89 /// users provoke undefined behaviour by supplying misaligned pointers.
90 /// \see mayCombineMisaligned()
92 AssumeMisalignedLoadStores("arm-assume-misaligned-load-store", cl::Hidden
,
93 cl::init(false), cl::desc("Be more conservative in ARM load/store opt"));
95 #define ARM_LOAD_STORE_OPT_NAME "ARM load / store optimization pass"
99 /// Post- register allocation pass the combine load / store instructions to
100 /// form ldm / stm instructions.
101 struct ARMLoadStoreOpt
: public MachineFunctionPass
{
104 const MachineFunction
*MF
;
105 const TargetInstrInfo
*TII
;
106 const TargetRegisterInfo
*TRI
;
107 const ARMSubtarget
*STI
;
108 const TargetLowering
*TL
;
109 ARMFunctionInfo
*AFI
;
110 LiveRegUnits LiveRegs
;
111 RegisterClassInfo RegClassInfo
;
112 MachineBasicBlock::const_iterator LiveRegPos
;
114 bool RegClassInfoValid
;
115 bool isThumb1
, isThumb2
;
117 ARMLoadStoreOpt() : MachineFunctionPass(ID
) {}
119 bool runOnMachineFunction(MachineFunction
&Fn
) override
;
121 MachineFunctionProperties
getRequiredProperties() const override
{
122 return MachineFunctionProperties().set(
123 MachineFunctionProperties::Property::NoVRegs
);
126 StringRef
getPassName() const override
{ return ARM_LOAD_STORE_OPT_NAME
; }
129 /// A set of load/store MachineInstrs with same base register sorted by
131 struct MemOpQueueEntry
{
133 int Offset
; ///< Load/Store offset.
134 unsigned Position
; ///< Position as counted from end of basic block.
136 MemOpQueueEntry(MachineInstr
&MI
, int Offset
, unsigned Position
)
137 : MI(&MI
), Offset(Offset
), Position(Position
) {}
139 using MemOpQueue
= SmallVector
<MemOpQueueEntry
, 8>;
141 /// A set of MachineInstrs that fulfill (nearly all) conditions to get
142 /// merged into a LDM/STM.
143 struct MergeCandidate
{
144 /// List of instructions ordered by load/store offset.
145 SmallVector
<MachineInstr
*, 4> Instrs
;
147 /// Index in Instrs of the instruction being latest in the schedule.
148 unsigned LatestMIIdx
;
150 /// Index in Instrs of the instruction being earliest in the schedule.
151 unsigned EarliestMIIdx
;
153 /// Index into the basic block where the merged instruction will be
154 /// inserted. (See MemOpQueueEntry.Position)
157 /// Whether the instructions can be merged into a ldm/stm instruction.
158 bool CanMergeToLSMulti
;
160 /// Whether the instructions can be merged into a ldrd/strd instruction.
161 bool CanMergeToLSDouble
;
163 SpecificBumpPtrAllocator
<MergeCandidate
> Allocator
;
164 SmallVector
<const MergeCandidate
*,4> Candidates
;
165 SmallVector
<MachineInstr
*,4> MergeBaseCandidates
;
167 void moveLiveRegsBefore(const MachineBasicBlock
&MBB
,
168 MachineBasicBlock::const_iterator Before
);
169 unsigned findFreeReg(const TargetRegisterClass
&RegClass
);
170 void UpdateBaseRegUses(MachineBasicBlock
&MBB
,
171 MachineBasicBlock::iterator MBBI
, const DebugLoc
&DL
,
172 unsigned Base
, unsigned WordOffset
,
173 ARMCC::CondCodes Pred
, unsigned PredReg
);
174 MachineInstr
*CreateLoadStoreMulti(
175 MachineBasicBlock
&MBB
, MachineBasicBlock::iterator InsertBefore
,
176 int Offset
, unsigned Base
, bool BaseKill
, unsigned Opcode
,
177 ARMCC::CondCodes Pred
, unsigned PredReg
, const DebugLoc
&DL
,
178 ArrayRef
<std::pair
<unsigned, bool>> Regs
,
179 ArrayRef
<MachineInstr
*> Instrs
);
180 MachineInstr
*CreateLoadStoreDouble(
181 MachineBasicBlock
&MBB
, MachineBasicBlock::iterator InsertBefore
,
182 int Offset
, unsigned Base
, bool BaseKill
, unsigned Opcode
,
183 ARMCC::CondCodes Pred
, unsigned PredReg
, const DebugLoc
&DL
,
184 ArrayRef
<std::pair
<unsigned, bool>> Regs
,
185 ArrayRef
<MachineInstr
*> Instrs
) const;
186 void FormCandidates(const MemOpQueue
&MemOps
);
187 MachineInstr
*MergeOpsUpdate(const MergeCandidate
&Cand
);
188 bool FixInvalidRegPairOp(MachineBasicBlock
&MBB
,
189 MachineBasicBlock::iterator
&MBBI
);
190 bool MergeBaseUpdateLoadStore(MachineInstr
*MI
);
191 bool MergeBaseUpdateLSMultiple(MachineInstr
*MI
);
192 bool MergeBaseUpdateLSDouble(MachineInstr
&MI
) const;
193 bool LoadStoreMultipleOpti(MachineBasicBlock
&MBB
);
194 bool MergeReturnIntoLDM(MachineBasicBlock
&MBB
);
195 bool CombineMovBx(MachineBasicBlock
&MBB
);
198 } // end anonymous namespace
200 char ARMLoadStoreOpt::ID
= 0;
202 INITIALIZE_PASS(ARMLoadStoreOpt
, "arm-ldst-opt", ARM_LOAD_STORE_OPT_NAME
, false,
205 static bool definesCPSR(const MachineInstr
&MI
) {
206 for (const auto &MO
: MI
.operands()) {
209 if (MO
.isDef() && MO
.getReg() == ARM::CPSR
&& !MO
.isDead())
210 // If the instruction has live CPSR def, then it's not safe to fold it
211 // into load / store.
218 static int getMemoryOpOffset(const MachineInstr
&MI
) {
219 unsigned Opcode
= MI
.getOpcode();
220 bool isAM3
= Opcode
== ARM::LDRD
|| Opcode
== ARM::STRD
;
221 unsigned NumOperands
= MI
.getDesc().getNumOperands();
222 unsigned OffField
= MI
.getOperand(NumOperands
- 3).getImm();
224 if (Opcode
== ARM::t2LDRi12
|| Opcode
== ARM::t2LDRi8
||
225 Opcode
== ARM::t2STRi12
|| Opcode
== ARM::t2STRi8
||
226 Opcode
== ARM::t2LDRDi8
|| Opcode
== ARM::t2STRDi8
||
227 Opcode
== ARM::LDRi12
|| Opcode
== ARM::STRi12
)
230 // Thumb1 immediate offsets are scaled by 4
231 if (Opcode
== ARM::tLDRi
|| Opcode
== ARM::tSTRi
||
232 Opcode
== ARM::tLDRspi
|| Opcode
== ARM::tSTRspi
)
235 int Offset
= isAM3
? ARM_AM::getAM3Offset(OffField
)
236 : ARM_AM::getAM5Offset(OffField
) * 4;
237 ARM_AM::AddrOpc Op
= isAM3
? ARM_AM::getAM3Op(OffField
)
238 : ARM_AM::getAM5Op(OffField
);
240 if (Op
== ARM_AM::sub
)
246 static const MachineOperand
&getLoadStoreBaseOp(const MachineInstr
&MI
) {
247 return MI
.getOperand(1);
250 static const MachineOperand
&getLoadStoreRegOp(const MachineInstr
&MI
) {
251 return MI
.getOperand(0);
254 static int getLoadStoreMultipleOpcode(unsigned Opcode
, ARM_AM::AMSubMode Mode
) {
256 default: llvm_unreachable("Unhandled opcode!");
260 default: llvm_unreachable("Unhandled submode!");
261 case ARM_AM::ia
: return ARM::LDMIA
;
262 case ARM_AM::da
: return ARM::LDMDA
;
263 case ARM_AM::db
: return ARM::LDMDB
;
264 case ARM_AM::ib
: return ARM::LDMIB
;
269 default: llvm_unreachable("Unhandled submode!");
270 case ARM_AM::ia
: return ARM::STMIA
;
271 case ARM_AM::da
: return ARM::STMDA
;
272 case ARM_AM::db
: return ARM::STMDB
;
273 case ARM_AM::ib
: return ARM::STMIB
;
277 // tLDMIA is writeback-only - unless the base register is in the input
281 default: llvm_unreachable("Unhandled submode!");
282 case ARM_AM::ia
: return ARM::tLDMIA
;
286 // There is no non-writeback tSTMIA either.
289 default: llvm_unreachable("Unhandled submode!");
290 case ARM_AM::ia
: return ARM::tSTMIA_UPD
;
296 default: llvm_unreachable("Unhandled submode!");
297 case ARM_AM::ia
: return ARM::t2LDMIA
;
298 case ARM_AM::db
: return ARM::t2LDMDB
;
304 default: llvm_unreachable("Unhandled submode!");
305 case ARM_AM::ia
: return ARM::t2STMIA
;
306 case ARM_AM::db
: return ARM::t2STMDB
;
311 default: llvm_unreachable("Unhandled submode!");
312 case ARM_AM::ia
: return ARM::VLDMSIA
;
313 case ARM_AM::db
: return 0; // Only VLDMSDB_UPD exists.
318 default: llvm_unreachable("Unhandled submode!");
319 case ARM_AM::ia
: return ARM::VSTMSIA
;
320 case ARM_AM::db
: return 0; // Only VSTMSDB_UPD exists.
325 default: llvm_unreachable("Unhandled submode!");
326 case ARM_AM::ia
: return ARM::VLDMDIA
;
327 case ARM_AM::db
: return 0; // Only VLDMDDB_UPD exists.
332 default: llvm_unreachable("Unhandled submode!");
333 case ARM_AM::ia
: return ARM::VSTMDIA
;
334 case ARM_AM::db
: return 0; // Only VSTMDDB_UPD exists.
339 static ARM_AM::AMSubMode
getLoadStoreMultipleSubMode(unsigned Opcode
) {
341 default: llvm_unreachable("Unhandled opcode!");
348 case ARM::tLDMIA_UPD
:
349 case ARM::tSTMIA_UPD
:
350 case ARM::t2LDMIA_RET
:
352 case ARM::t2LDMIA_UPD
:
354 case ARM::t2STMIA_UPD
:
356 case ARM::VLDMSIA_UPD
:
358 case ARM::VSTMSIA_UPD
:
360 case ARM::VLDMDIA_UPD
:
362 case ARM::VSTMDIA_UPD
:
376 case ARM::t2LDMDB_UPD
:
378 case ARM::t2STMDB_UPD
:
379 case ARM::VLDMSDB_UPD
:
380 case ARM::VSTMSDB_UPD
:
381 case ARM::VLDMDDB_UPD
:
382 case ARM::VSTMDDB_UPD
:
393 static bool isT1i32Load(unsigned Opc
) {
394 return Opc
== ARM::tLDRi
|| Opc
== ARM::tLDRspi
;
397 static bool isT2i32Load(unsigned Opc
) {
398 return Opc
== ARM::t2LDRi12
|| Opc
== ARM::t2LDRi8
;
401 static bool isi32Load(unsigned Opc
) {
402 return Opc
== ARM::LDRi12
|| isT1i32Load(Opc
) || isT2i32Load(Opc
) ;
405 static bool isT1i32Store(unsigned Opc
) {
406 return Opc
== ARM::tSTRi
|| Opc
== ARM::tSTRspi
;
409 static bool isT2i32Store(unsigned Opc
) {
410 return Opc
== ARM::t2STRi12
|| Opc
== ARM::t2STRi8
;
413 static bool isi32Store(unsigned Opc
) {
414 return Opc
== ARM::STRi12
|| isT1i32Store(Opc
) || isT2i32Store(Opc
);
417 static bool isLoadSingle(unsigned Opc
) {
418 return isi32Load(Opc
) || Opc
== ARM::VLDRS
|| Opc
== ARM::VLDRD
;
421 static unsigned getImmScale(unsigned Opc
) {
423 default: llvm_unreachable("Unhandled opcode!");
438 static unsigned getLSMultipleTransferSize(const MachineInstr
*MI
) {
439 switch (MI
->getOpcode()) {
466 case ARM::tLDMIA_UPD
:
467 case ARM::tSTMIA_UPD
:
474 return (MI
->getNumOperands() - MI
->getDesc().getNumOperands() + 1) * 4;
477 return (MI
->getNumOperands() - MI
->getDesc().getNumOperands() + 1) * 8;
481 /// Update future uses of the base register with the offset introduced
482 /// due to writeback. This function only works on Thumb1.
483 void ARMLoadStoreOpt::UpdateBaseRegUses(MachineBasicBlock
&MBB
,
484 MachineBasicBlock::iterator MBBI
,
485 const DebugLoc
&DL
, unsigned Base
,
487 ARMCC::CondCodes Pred
,
489 assert(isThumb1
&& "Can only update base register uses for Thumb1!");
490 // Start updating any instructions with immediate offsets. Insert a SUB before
491 // the first non-updateable instruction (if any).
492 for (; MBBI
!= MBB
.end(); ++MBBI
) {
493 bool InsertSub
= false;
494 unsigned Opc
= MBBI
->getOpcode();
496 if (MBBI
->readsRegister(Base
, /*TRI=*/nullptr)) {
499 Opc
== ARM::tLDRi
|| Opc
== ARM::tLDRHi
|| Opc
== ARM::tLDRBi
;
501 Opc
== ARM::tSTRi
|| Opc
== ARM::tSTRHi
|| Opc
== ARM::tSTRBi
;
503 if (IsLoad
|| IsStore
) {
504 // Loads and stores with immediate offsets can be updated, but only if
505 // the new offset isn't negative.
506 // The MachineOperand containing the offset immediate is the last one
507 // before predicates.
509 MBBI
->getOperand(MBBI
->getDesc().getNumOperands() - 3);
510 // The offsets are scaled by 1, 2 or 4 depending on the Opcode.
511 Offset
= MO
.getImm() - WordOffset
* getImmScale(Opc
);
513 // If storing the base register, it needs to be reset first.
514 Register InstrSrcReg
= getLoadStoreRegOp(*MBBI
).getReg();
516 if (Offset
>= 0 && !(IsStore
&& InstrSrcReg
== Base
))
520 } else if ((Opc
== ARM::tSUBi8
|| Opc
== ARM::tADDi8
) &&
521 !definesCPSR(*MBBI
)) {
522 // SUBS/ADDS using this register, with a dead def of the CPSR.
523 // Merge it with the update; if the merged offset is too large,
524 // insert a new sub instead.
526 MBBI
->getOperand(MBBI
->getDesc().getNumOperands() - 3);
527 Offset
= (Opc
== ARM::tSUBi8
) ?
528 MO
.getImm() + WordOffset
* 4 :
529 MO
.getImm() - WordOffset
* 4 ;
530 if (Offset
>= 0 && TL
->isLegalAddImmediate(Offset
)) {
531 // FIXME: Swap ADDS<->SUBS if Offset < 0, erase instruction if
534 // The base register has now been reset, so exit early.
540 // Can't update the instruction.
543 } else if (definesCPSR(*MBBI
) || MBBI
->isCall() || MBBI
->isBranch()) {
544 // Since SUBS sets the condition flags, we can't place the base reset
545 // after an instruction that has a live CPSR def.
546 // The base register might also contain an argument for a function call.
551 // An instruction above couldn't be updated, so insert a sub.
552 BuildMI(MBB
, MBBI
, DL
, TII
->get(ARM::tSUBi8
), Base
)
553 .add(t1CondCodeOp(true))
555 .addImm(WordOffset
* 4)
561 if (MBBI
->killsRegister(Base
, /*TRI=*/nullptr) ||
562 MBBI
->definesRegister(Base
, /*TRI=*/nullptr))
563 // Register got killed. Stop updating.
567 // End of block was reached.
568 if (!MBB
.succ_empty()) {
569 // FIXME: Because of a bug, live registers are sometimes missing from
570 // the successor blocks' live-in sets. This means we can't trust that
571 // information and *always* have to reset at the end of a block.
573 if (MBBI
!= MBB
.end()) --MBBI
;
574 BuildMI(MBB
, MBBI
, DL
, TII
->get(ARM::tSUBi8
), Base
)
575 .add(t1CondCodeOp(true))
577 .addImm(WordOffset
* 4)
583 /// Return the first register of class \p RegClass that is not in \p Regs.
584 unsigned ARMLoadStoreOpt::findFreeReg(const TargetRegisterClass
&RegClass
) {
585 if (!RegClassInfoValid
) {
586 RegClassInfo
.runOnMachineFunction(*MF
);
587 RegClassInfoValid
= true;
590 for (unsigned Reg
: RegClassInfo
.getOrder(&RegClass
))
591 if (LiveRegs
.available(Reg
) && !MF
->getRegInfo().isReserved(Reg
))
596 /// Compute live registers just before instruction \p Before (in normal schedule
597 /// direction). Computes backwards so multiple queries in the same block must
598 /// come in reverse order.
599 void ARMLoadStoreOpt::moveLiveRegsBefore(const MachineBasicBlock
&MBB
,
600 MachineBasicBlock::const_iterator Before
) {
601 // Initialize if we never queried in this block.
602 if (!LiveRegsValid
) {
604 LiveRegs
.addLiveOuts(MBB
);
605 LiveRegPos
= MBB
.end();
606 LiveRegsValid
= true;
608 // Move backward just before the "Before" position.
609 while (LiveRegPos
!= Before
) {
611 LiveRegs
.stepBackward(*LiveRegPos
);
615 static bool ContainsReg(const ArrayRef
<std::pair
<unsigned, bool>> &Regs
,
617 for (const std::pair
<unsigned, bool> &R
: Regs
)
623 /// Create and insert a LDM or STM with Base as base register and registers in
624 /// Regs as the register operands that would be loaded / stored. It returns
625 /// true if the transformation is done.
626 MachineInstr
*ARMLoadStoreOpt::CreateLoadStoreMulti(
627 MachineBasicBlock
&MBB
, MachineBasicBlock::iterator InsertBefore
,
628 int Offset
, unsigned Base
, bool BaseKill
, unsigned Opcode
,
629 ARMCC::CondCodes Pred
, unsigned PredReg
, const DebugLoc
&DL
,
630 ArrayRef
<std::pair
<unsigned, bool>> Regs
,
631 ArrayRef
<MachineInstr
*> Instrs
) {
632 unsigned NumRegs
= Regs
.size();
635 // For Thumb1 targets, it might be necessary to clobber the CPSR to merge.
636 // Compute liveness information for that register to make the decision.
637 bool SafeToClobberCPSR
= !isThumb1
||
638 (MBB
.computeRegisterLiveness(TRI
, ARM::CPSR
, InsertBefore
, 20) ==
639 MachineBasicBlock::LQR_Dead
);
641 bool Writeback
= isThumb1
; // Thumb1 LDM/STM have base reg writeback.
643 // Exception: If the base register is in the input reglist, Thumb1 LDM is
645 // It's also not possible to merge an STR of the base register in Thumb1.
646 if (isThumb1
&& ContainsReg(Regs
, Base
)) {
647 assert(Base
!= ARM::SP
&& "Thumb1 does not allow SP in register list");
648 if (Opcode
== ARM::tLDRi
)
650 else if (Opcode
== ARM::tSTRi
)
654 ARM_AM::AMSubMode Mode
= ARM_AM::ia
;
655 // VFP and Thumb2 do not support IB or DA modes. Thumb1 only supports IA.
656 bool isNotVFP
= isi32Load(Opcode
) || isi32Store(Opcode
);
657 bool haveIBAndDA
= isNotVFP
&& !isThumb2
&& !isThumb1
;
659 if (Offset
== 4 && haveIBAndDA
) {
661 } else if (Offset
== -4 * (int)NumRegs
+ 4 && haveIBAndDA
) {
663 } else if (Offset
== -4 * (int)NumRegs
&& isNotVFP
&& !isThumb1
) {
664 // VLDM/VSTM do not support DB mode without also updating the base reg.
666 } else if (Offset
!= 0 || Opcode
== ARM::tLDRspi
|| Opcode
== ARM::tSTRspi
) {
667 // Check if this is a supported opcode before inserting instructions to
668 // calculate a new base register.
669 if (!getLoadStoreMultipleOpcode(Opcode
, Mode
)) return nullptr;
671 // If starting offset isn't zero, insert a MI to materialize a new base.
672 // But only do so if it is cost effective, i.e. merging more than two
677 // On Thumb1, it's not worth materializing a new base register without
678 // clobbering the CPSR (i.e. not using ADDS/SUBS).
679 if (!SafeToClobberCPSR
)
683 if (isi32Load(Opcode
)) {
684 // If it is a load, then just use one of the destination registers
685 // as the new base. Will no longer be writeback in Thumb1.
686 NewBase
= Regs
[NumRegs
-1].first
;
689 // Find a free register that we can use as scratch register.
690 moveLiveRegsBefore(MBB
, InsertBefore
);
691 // The merged instruction does not exist yet but will use several Regs if
693 if (!isLoadSingle(Opcode
))
694 for (const std::pair
<unsigned, bool> &R
: Regs
)
695 LiveRegs
.addReg(R
.first
);
697 NewBase
= findFreeReg(isThumb1
? ARM::tGPRRegClass
: ARM::GPRRegClass
);
702 int BaseOpc
= isThumb2
? (BaseKill
&& Base
== ARM::SP
? ARM::t2ADDspImm
704 : (isThumb1
&& Base
== ARM::SP
)
706 : (isThumb1
&& Offset
< 8)
708 : isThumb1
? ARM::tADDi8
: ARM::ADDri
;
711 // FIXME: There are no Thumb1 load/store instructions with negative
712 // offsets. So the Base != ARM::SP might be unnecessary.
714 BaseOpc
= isThumb2
? (BaseKill
&& Base
== ARM::SP
? ARM::t2SUBspImm
716 : (isThumb1
&& Offset
< 8 && Base
!= ARM::SP
)
718 : isThumb1
? ARM::tSUBi8
: ARM::SUBri
;
721 if (!TL
->isLegalAddImmediate(Offset
))
722 // FIXME: Try add with register operand?
723 return nullptr; // Probably not worth it then.
725 // We can only append a kill flag to the add/sub input if the value is not
726 // used in the register list of the stm as well.
727 bool KillOldBase
= BaseKill
&&
728 (!isi32Store(Opcode
) || !ContainsReg(Regs
, Base
));
731 // Thumb1: depending on immediate size, use either
732 // ADDS NewBase, Base, #imm3
735 // ADDS NewBase, #imm8.
736 if (Base
!= NewBase
&&
737 (BaseOpc
== ARM::tADDi8
|| BaseOpc
== ARM::tSUBi8
)) {
738 // Need to insert a MOV to the new base first.
739 if (isARMLowRegister(NewBase
) && isARMLowRegister(Base
) &&
741 // thumbv4t doesn't have lo->lo copies, and we can't predicate tMOVSr
742 if (Pred
!= ARMCC::AL
)
744 BuildMI(MBB
, InsertBefore
, DL
, TII
->get(ARM::tMOVSr
), NewBase
)
745 .addReg(Base
, getKillRegState(KillOldBase
));
747 BuildMI(MBB
, InsertBefore
, DL
, TII
->get(ARM::tMOVr
), NewBase
)
748 .addReg(Base
, getKillRegState(KillOldBase
))
749 .add(predOps(Pred
, PredReg
));
751 // The following ADDS/SUBS becomes an update.
755 if (BaseOpc
== ARM::tADDrSPi
) {
756 assert(Offset
% 4 == 0 && "tADDrSPi offset is scaled by 4");
757 BuildMI(MBB
, InsertBefore
, DL
, TII
->get(BaseOpc
), NewBase
)
758 .addReg(Base
, getKillRegState(KillOldBase
))
760 .add(predOps(Pred
, PredReg
));
762 BuildMI(MBB
, InsertBefore
, DL
, TII
->get(BaseOpc
), NewBase
)
763 .add(t1CondCodeOp(true))
764 .addReg(Base
, getKillRegState(KillOldBase
))
766 .add(predOps(Pred
, PredReg
));
768 BuildMI(MBB
, InsertBefore
, DL
, TII
->get(BaseOpc
), NewBase
)
769 .addReg(Base
, getKillRegState(KillOldBase
))
771 .add(predOps(Pred
, PredReg
))
775 BaseKill
= true; // New base is always killed straight away.
778 bool isDef
= isLoadSingle(Opcode
);
780 // Get LS multiple opcode. Note that for Thumb1 this might be an opcode with
781 // base register writeback.
782 Opcode
= getLoadStoreMultipleOpcode(Opcode
, Mode
);
786 // Check if a Thumb1 LDM/STM merge is safe. This is the case if:
787 // - There is no writeback (LDM of base register),
788 // - the base register is killed by the merged instruction,
789 // - or it's safe to overwrite the condition flags, i.e. to insert a SUBS
790 // to reset the base register.
791 // Otherwise, don't merge.
792 // It's safe to return here since the code to materialize a new base register
793 // above is also conditional on SafeToClobberCPSR.
794 if (isThumb1
&& !SafeToClobberCPSR
&& Writeback
&& !BaseKill
)
797 MachineInstrBuilder MIB
;
800 assert(isThumb1
&& "expected Writeback only inThumb1");
801 if (Opcode
== ARM::tLDMIA
) {
802 assert(!(ContainsReg(Regs
, Base
)) && "Thumb1 can't LDM ! with Base in Regs");
803 // Update tLDMIA with writeback if necessary.
804 Opcode
= ARM::tLDMIA_UPD
;
807 MIB
= BuildMI(MBB
, InsertBefore
, DL
, TII
->get(Opcode
));
809 // Thumb1: we might need to set base writeback when building the MI.
810 MIB
.addReg(Base
, getDefRegState(true))
811 .addReg(Base
, getKillRegState(BaseKill
));
813 // The base isn't dead after a merged instruction with writeback.
814 // Insert a sub instruction after the newly formed instruction to reset.
816 UpdateBaseRegUses(MBB
, InsertBefore
, DL
, Base
, NumRegs
, Pred
, PredReg
);
818 // No writeback, simply build the MachineInstr.
819 MIB
= BuildMI(MBB
, InsertBefore
, DL
, TII
->get(Opcode
));
820 MIB
.addReg(Base
, getKillRegState(BaseKill
));
823 MIB
.addImm(Pred
).addReg(PredReg
);
825 for (const std::pair
<unsigned, bool> &R
: Regs
)
826 MIB
.addReg(R
.first
, getDefRegState(isDef
) | getKillRegState(R
.second
));
828 MIB
.cloneMergedMemRefs(Instrs
);
830 return MIB
.getInstr();
833 MachineInstr
*ARMLoadStoreOpt::CreateLoadStoreDouble(
834 MachineBasicBlock
&MBB
, MachineBasicBlock::iterator InsertBefore
,
835 int Offset
, unsigned Base
, bool BaseKill
, unsigned Opcode
,
836 ARMCC::CondCodes Pred
, unsigned PredReg
, const DebugLoc
&DL
,
837 ArrayRef
<std::pair
<unsigned, bool>> Regs
,
838 ArrayRef
<MachineInstr
*> Instrs
) const {
839 bool IsLoad
= isi32Load(Opcode
);
840 assert((IsLoad
|| isi32Store(Opcode
)) && "Must have integer load or store");
841 unsigned LoadStoreOpcode
= IsLoad
? ARM::t2LDRDi8
: ARM::t2STRDi8
;
843 assert(Regs
.size() == 2);
844 MachineInstrBuilder MIB
= BuildMI(MBB
, InsertBefore
, DL
,
845 TII
->get(LoadStoreOpcode
));
847 MIB
.addReg(Regs
[0].first
, RegState::Define
)
848 .addReg(Regs
[1].first
, RegState::Define
);
850 MIB
.addReg(Regs
[0].first
, getKillRegState(Regs
[0].second
))
851 .addReg(Regs
[1].first
, getKillRegState(Regs
[1].second
));
853 MIB
.addReg(Base
).addImm(Offset
).addImm(Pred
).addReg(PredReg
);
854 MIB
.cloneMergedMemRefs(Instrs
);
855 return MIB
.getInstr();
858 /// Call MergeOps and update MemOps and merges accordingly on success.
859 MachineInstr
*ARMLoadStoreOpt::MergeOpsUpdate(const MergeCandidate
&Cand
) {
860 const MachineInstr
*First
= Cand
.Instrs
.front();
861 unsigned Opcode
= First
->getOpcode();
862 bool IsLoad
= isLoadSingle(Opcode
);
863 SmallVector
<std::pair
<unsigned, bool>, 8> Regs
;
864 SmallVector
<unsigned, 4> ImpDefs
;
865 DenseSet
<unsigned> KilledRegs
;
866 DenseSet
<unsigned> UsedRegs
;
867 // Determine list of registers and list of implicit super-register defs.
868 for (const MachineInstr
*MI
: Cand
.Instrs
) {
869 const MachineOperand
&MO
= getLoadStoreRegOp(*MI
);
870 Register Reg
= MO
.getReg();
871 bool IsKill
= MO
.isKill();
873 KilledRegs
.insert(Reg
);
874 Regs
.push_back(std::make_pair(Reg
, IsKill
));
875 UsedRegs
.insert(Reg
);
878 // Collect any implicit defs of super-registers, after merging we can't
879 // be sure anymore that we properly preserved these live ranges and must
880 // removed these implicit operands.
881 for (const MachineOperand
&MO
: MI
->implicit_operands()) {
882 if (!MO
.isReg() || !MO
.isDef() || MO
.isDead())
884 assert(MO
.isImplicit());
885 Register DefReg
= MO
.getReg();
887 if (is_contained(ImpDefs
, DefReg
))
889 // We can ignore cases where the super-reg is read and written.
890 if (MI
->readsRegister(DefReg
, /*TRI=*/nullptr))
892 ImpDefs
.push_back(DefReg
);
897 // Attempt the merge.
898 using iterator
= MachineBasicBlock::iterator
;
900 MachineInstr
*LatestMI
= Cand
.Instrs
[Cand
.LatestMIIdx
];
901 iterator InsertBefore
= std::next(iterator(LatestMI
));
902 MachineBasicBlock
&MBB
= *LatestMI
->getParent();
903 unsigned Offset
= getMemoryOpOffset(*First
);
904 Register Base
= getLoadStoreBaseOp(*First
).getReg();
905 bool BaseKill
= LatestMI
->killsRegister(Base
, /*TRI=*/nullptr);
907 ARMCC::CondCodes Pred
= getInstrPredicate(*First
, PredReg
);
908 DebugLoc DL
= First
->getDebugLoc();
909 MachineInstr
*Merged
= nullptr;
910 if (Cand
.CanMergeToLSDouble
)
911 Merged
= CreateLoadStoreDouble(MBB
, InsertBefore
, Offset
, Base
, BaseKill
,
912 Opcode
, Pred
, PredReg
, DL
, Regs
,
914 if (!Merged
&& Cand
.CanMergeToLSMulti
)
915 Merged
= CreateLoadStoreMulti(MBB
, InsertBefore
, Offset
, Base
, BaseKill
,
916 Opcode
, Pred
, PredReg
, DL
, Regs
, Cand
.Instrs
);
920 // Determine earliest instruction that will get removed. We then keep an
921 // iterator just above it so the following erases don't invalidated it.
922 iterator
EarliestI(Cand
.Instrs
[Cand
.EarliestMIIdx
]);
923 bool EarliestAtBegin
= false;
924 if (EarliestI
== MBB
.begin()) {
925 EarliestAtBegin
= true;
927 EarliestI
= std::prev(EarliestI
);
930 // Remove instructions which have been merged.
931 for (MachineInstr
*MI
: Cand
.Instrs
)
934 // Determine range between the earliest removed instruction and the new one.
936 EarliestI
= MBB
.begin();
938 EarliestI
= std::next(EarliestI
);
939 auto FixupRange
= make_range(EarliestI
, iterator(Merged
));
941 if (isLoadSingle(Opcode
)) {
942 // If the previous loads defined a super-reg, then we have to mark earlier
943 // operands undef; Replicate the super-reg def on the merged instruction.
944 for (MachineInstr
&MI
: FixupRange
) {
945 for (unsigned &ImpDefReg
: ImpDefs
) {
946 for (MachineOperand
&MO
: MI
.implicit_operands()) {
947 if (!MO
.isReg() || MO
.getReg() != ImpDefReg
)
957 MachineInstrBuilder
MIB(*Merged
->getParent()->getParent(), Merged
);
958 for (unsigned ImpDef
: ImpDefs
)
959 MIB
.addReg(ImpDef
, RegState::ImplicitDefine
);
961 // Remove kill flags: We are possibly storing the values later now.
962 assert(isi32Store(Opcode
) || Opcode
== ARM::VSTRS
|| Opcode
== ARM::VSTRD
);
963 for (MachineInstr
&MI
: FixupRange
) {
964 for (MachineOperand
&MO
: MI
.uses()) {
965 if (!MO
.isReg() || !MO
.isKill())
967 if (UsedRegs
.count(MO
.getReg()))
971 assert(ImpDefs
.empty());
977 static bool isValidLSDoubleOffset(int Offset
) {
978 unsigned Value
= abs(Offset
);
979 // t2LDRDi8/t2STRDi8 supports an 8 bit immediate which is internally
981 return (Value
% 4) == 0 && Value
< 1024;
984 /// Return true for loads/stores that can be combined to a double/multi
985 /// operation without increasing the requirements for alignment.
986 static bool mayCombineMisaligned(const TargetSubtargetInfo
&STI
,
987 const MachineInstr
&MI
) {
988 // vldr/vstr trap on misaligned pointers anyway, forming vldm makes no
990 unsigned Opcode
= MI
.getOpcode();
991 if (!isi32Load(Opcode
) && !isi32Store(Opcode
))
994 // Stack pointer alignment is out of the programmers control so we can trust
995 // SP-relative loads/stores.
996 if (getLoadStoreBaseOp(MI
).getReg() == ARM::SP
&&
997 STI
.getFrameLowering()->getTransientStackAlign() >= Align(4))
1002 /// Find candidates for load/store multiple merge in list of MemOpQueueEntries.
1003 void ARMLoadStoreOpt::FormCandidates(const MemOpQueue
&MemOps
) {
1004 const MachineInstr
*FirstMI
= MemOps
[0].MI
;
1005 unsigned Opcode
= FirstMI
->getOpcode();
1006 bool isNotVFP
= isi32Load(Opcode
) || isi32Store(Opcode
);
1007 unsigned Size
= getLSMultipleTransferSize(FirstMI
);
1009 unsigned SIndex
= 0;
1010 unsigned EIndex
= MemOps
.size();
1012 // Look at the first instruction.
1013 const MachineInstr
*MI
= MemOps
[SIndex
].MI
;
1014 int Offset
= MemOps
[SIndex
].Offset
;
1015 const MachineOperand
&PMO
= getLoadStoreRegOp(*MI
);
1016 Register PReg
= PMO
.getReg();
1017 unsigned PRegNum
= PMO
.isUndef() ? std::numeric_limits
<unsigned>::max()
1018 : TRI
->getEncodingValue(PReg
);
1019 unsigned Latest
= SIndex
;
1020 unsigned Earliest
= SIndex
;
1022 bool CanMergeToLSDouble
=
1023 STI
->isThumb2() && isNotVFP
&& isValidLSDoubleOffset(Offset
);
1024 // ARM errata 602117: LDRD with base in list may result in incorrect base
1025 // register when interrupted or faulted.
1026 if (STI
->isCortexM3() && isi32Load(Opcode
) &&
1027 PReg
== getLoadStoreBaseOp(*MI
).getReg())
1028 CanMergeToLSDouble
= false;
1030 bool CanMergeToLSMulti
= true;
1031 // On swift vldm/vstm starting with an odd register number as that needs
1032 // more uops than single vldrs.
1033 if (STI
->hasSlowOddRegister() && !isNotVFP
&& (PRegNum
% 2) == 1)
1034 CanMergeToLSMulti
= false;
1036 // LDRD/STRD do not allow SP/PC. LDM/STM do not support it or have it
1037 // deprecated; LDM to PC is fine but cannot happen here.
1038 if (PReg
== ARM::SP
|| PReg
== ARM::PC
)
1039 CanMergeToLSMulti
= CanMergeToLSDouble
= false;
1041 // Should we be conservative?
1042 if (AssumeMisalignedLoadStores
&& !mayCombineMisaligned(*STI
, *MI
))
1043 CanMergeToLSMulti
= CanMergeToLSDouble
= false;
1045 // vldm / vstm limit are 32 for S variants, 16 for D variants.
1057 // Merge following instructions where possible.
1058 for (unsigned I
= SIndex
+1; I
< EIndex
; ++I
, ++Count
) {
1059 int NewOffset
= MemOps
[I
].Offset
;
1060 if (NewOffset
!= Offset
+ (int)Size
)
1062 const MachineOperand
&MO
= getLoadStoreRegOp(*MemOps
[I
].MI
);
1063 Register Reg
= MO
.getReg();
1064 if (Reg
== ARM::SP
|| Reg
== ARM::PC
)
1069 // See if the current load/store may be part of a multi load/store.
1070 unsigned RegNum
= MO
.isUndef() ? std::numeric_limits
<unsigned>::max()
1071 : TRI
->getEncodingValue(Reg
);
1072 bool PartOfLSMulti
= CanMergeToLSMulti
;
1073 if (PartOfLSMulti
) {
1074 // Register numbers must be in ascending order.
1075 if (RegNum
<= PRegNum
)
1076 PartOfLSMulti
= false;
1077 // For VFP / NEON load/store multiples, the registers must be
1078 // consecutive and within the limit on the number of registers per
1080 else if (!isNotVFP
&& RegNum
!= PRegNum
+1)
1081 PartOfLSMulti
= false;
1083 // See if the current load/store may be part of a double load/store.
1084 bool PartOfLSDouble
= CanMergeToLSDouble
&& Count
<= 1;
1086 if (!PartOfLSMulti
&& !PartOfLSDouble
)
1088 CanMergeToLSMulti
&= PartOfLSMulti
;
1089 CanMergeToLSDouble
&= PartOfLSDouble
;
1090 // Track MemOp with latest and earliest position (Positions are
1091 // counted in reverse).
1092 unsigned Position
= MemOps
[I
].Position
;
1093 if (Position
< MemOps
[Latest
].Position
)
1095 else if (Position
> MemOps
[Earliest
].Position
)
1097 // Prepare for next MemOp.
1102 // Form a candidate from the Ops collected so far.
1103 MergeCandidate
*Candidate
= new(Allocator
.Allocate()) MergeCandidate
;
1104 for (unsigned C
= SIndex
, CE
= SIndex
+ Count
; C
< CE
; ++C
)
1105 Candidate
->Instrs
.push_back(MemOps
[C
].MI
);
1106 Candidate
->LatestMIIdx
= Latest
- SIndex
;
1107 Candidate
->EarliestMIIdx
= Earliest
- SIndex
;
1108 Candidate
->InsertPos
= MemOps
[Latest
].Position
;
1110 CanMergeToLSMulti
= CanMergeToLSDouble
= false;
1111 Candidate
->CanMergeToLSMulti
= CanMergeToLSMulti
;
1112 Candidate
->CanMergeToLSDouble
= CanMergeToLSDouble
;
1113 Candidates
.push_back(Candidate
);
1114 // Continue after the chain.
1116 } while (SIndex
< EIndex
);
1119 static unsigned getUpdatingLSMultipleOpcode(unsigned Opc
,
1120 ARM_AM::AMSubMode Mode
) {
1122 default: llvm_unreachable("Unhandled opcode!");
1128 default: llvm_unreachable("Unhandled submode!");
1129 case ARM_AM::ia
: return ARM::LDMIA_UPD
;
1130 case ARM_AM::ib
: return ARM::LDMIB_UPD
;
1131 case ARM_AM::da
: return ARM::LDMDA_UPD
;
1132 case ARM_AM::db
: return ARM::LDMDB_UPD
;
1139 default: llvm_unreachable("Unhandled submode!");
1140 case ARM_AM::ia
: return ARM::STMIA_UPD
;
1141 case ARM_AM::ib
: return ARM::STMIB_UPD
;
1142 case ARM_AM::da
: return ARM::STMDA_UPD
;
1143 case ARM_AM::db
: return ARM::STMDB_UPD
;
1148 default: llvm_unreachable("Unhandled submode!");
1149 case ARM_AM::ia
: return ARM::t2LDMIA_UPD
;
1150 case ARM_AM::db
: return ARM::t2LDMDB_UPD
;
1155 default: llvm_unreachable("Unhandled submode!");
1156 case ARM_AM::ia
: return ARM::t2STMIA_UPD
;
1157 case ARM_AM::db
: return ARM::t2STMDB_UPD
;
1161 default: llvm_unreachable("Unhandled submode!");
1162 case ARM_AM::ia
: return ARM::VLDMSIA_UPD
;
1163 case ARM_AM::db
: return ARM::VLDMSDB_UPD
;
1167 default: llvm_unreachable("Unhandled submode!");
1168 case ARM_AM::ia
: return ARM::VLDMDIA_UPD
;
1169 case ARM_AM::db
: return ARM::VLDMDDB_UPD
;
1173 default: llvm_unreachable("Unhandled submode!");
1174 case ARM_AM::ia
: return ARM::VSTMSIA_UPD
;
1175 case ARM_AM::db
: return ARM::VSTMSDB_UPD
;
1179 default: llvm_unreachable("Unhandled submode!");
1180 case ARM_AM::ia
: return ARM::VSTMDIA_UPD
;
1181 case ARM_AM::db
: return ARM::VSTMDDB_UPD
;
1186 /// Check if the given instruction increments or decrements a register and
1187 /// return the amount it is incremented/decremented. Returns 0 if the CPSR flags
1188 /// generated by the instruction are possibly read as well.
1189 static int isIncrementOrDecrement(const MachineInstr
&MI
, Register Reg
,
1190 ARMCC::CondCodes Pred
, Register PredReg
) {
1193 switch (MI
.getOpcode()) {
1194 case ARM::tADDi8
: Scale
= 4; CheckCPSRDef
= true; break;
1195 case ARM::tSUBi8
: Scale
= -4; CheckCPSRDef
= true; break;
1197 case ARM::t2SUBspImm
:
1198 case ARM::SUBri
: Scale
= -1; CheckCPSRDef
= true; break;
1200 case ARM::t2ADDspImm
:
1201 case ARM::ADDri
: Scale
= 1; CheckCPSRDef
= true; break;
1202 case ARM::tADDspi
: Scale
= 4; CheckCPSRDef
= false; break;
1203 case ARM::tSUBspi
: Scale
= -4; CheckCPSRDef
= false; break;
1208 if (MI
.getOperand(0).getReg() != Reg
||
1209 MI
.getOperand(1).getReg() != Reg
||
1210 getInstrPredicate(MI
, MIPredReg
) != Pred
||
1211 MIPredReg
!= PredReg
)
1214 if (CheckCPSRDef
&& definesCPSR(MI
))
1216 return MI
.getOperand(2).getImm() * Scale
;
1219 /// Searches for an increment or decrement of \p Reg before \p MBBI.
1220 static MachineBasicBlock::iterator
1221 findIncDecBefore(MachineBasicBlock::iterator MBBI
, Register Reg
,
1222 ARMCC::CondCodes Pred
, Register PredReg
, int &Offset
) {
1224 MachineBasicBlock
&MBB
= *MBBI
->getParent();
1225 MachineBasicBlock::iterator BeginMBBI
= MBB
.begin();
1226 MachineBasicBlock::iterator EndMBBI
= MBB
.end();
1227 if (MBBI
== BeginMBBI
)
1230 // Skip debug values.
1231 MachineBasicBlock::iterator PrevMBBI
= std::prev(MBBI
);
1232 while (PrevMBBI
->isDebugInstr() && PrevMBBI
!= BeginMBBI
)
1235 Offset
= isIncrementOrDecrement(*PrevMBBI
, Reg
, Pred
, PredReg
);
1236 return Offset
== 0 ? EndMBBI
: PrevMBBI
;
1239 /// Searches for a increment or decrement of \p Reg after \p MBBI.
1240 static MachineBasicBlock::iterator
1241 findIncDecAfter(MachineBasicBlock::iterator MBBI
, Register Reg
,
1242 ARMCC::CondCodes Pred
, Register PredReg
, int &Offset
,
1243 const TargetRegisterInfo
*TRI
) {
1245 MachineBasicBlock
&MBB
= *MBBI
->getParent();
1246 MachineBasicBlock::iterator EndMBBI
= MBB
.end();
1247 MachineBasicBlock::iterator NextMBBI
= std::next(MBBI
);
1248 while (NextMBBI
!= EndMBBI
) {
1249 // Skip debug values.
1250 while (NextMBBI
!= EndMBBI
&& NextMBBI
->isDebugInstr())
1252 if (NextMBBI
== EndMBBI
)
1255 unsigned Off
= isIncrementOrDecrement(*NextMBBI
, Reg
, Pred
, PredReg
);
1261 // SP can only be combined if it is the next instruction after the original
1262 // MBBI, otherwise we may be incrementing the stack pointer (invalidating
1263 // anything below the new pointer) when its frame elements are still in
1264 // use. Other registers can attempt to look further, until a different use
1265 // or def of the register is found.
1266 if (Reg
== ARM::SP
|| NextMBBI
->readsRegister(Reg
, TRI
) ||
1267 NextMBBI
->definesRegister(Reg
, TRI
))
1275 /// Fold proceeding/trailing inc/dec of base register into the
1276 /// LDM/STM/VLDM{D|S}/VSTM{D|S} op when possible:
1278 /// stmia rn, <ra, rb, rc>
1279 /// rn := rn + 4 * 3;
1281 /// stmia rn!, <ra, rb, rc>
1283 /// rn := rn - 4 * 3;
1284 /// ldmia rn, <ra, rb, rc>
1286 /// ldmdb rn!, <ra, rb, rc>
1287 bool ARMLoadStoreOpt::MergeBaseUpdateLSMultiple(MachineInstr
*MI
) {
1288 // Thumb1 is already using updating loads/stores.
1289 if (isThumb1
) return false;
1290 LLVM_DEBUG(dbgs() << "Attempting to merge update of: " << *MI
);
1292 const MachineOperand
&BaseOP
= MI
->getOperand(0);
1293 Register Base
= BaseOP
.getReg();
1294 bool BaseKill
= BaseOP
.isKill();
1296 ARMCC::CondCodes Pred
= getInstrPredicate(*MI
, PredReg
);
1297 unsigned Opcode
= MI
->getOpcode();
1298 DebugLoc DL
= MI
->getDebugLoc();
1300 // Can't use an updating ld/st if the base register is also a dest
1301 // register. e.g. ldmdb r0!, {r0, r1, r2}. The behavior is undefined.
1302 for (const MachineOperand
&MO
: llvm::drop_begin(MI
->operands(), 2))
1303 if (MO
.getReg() == Base
)
1306 int Bytes
= getLSMultipleTransferSize(MI
);
1307 MachineBasicBlock
&MBB
= *MI
->getParent();
1308 MachineBasicBlock::iterator
MBBI(MI
);
1310 MachineBasicBlock::iterator MergeInstr
1311 = findIncDecBefore(MBBI
, Base
, Pred
, PredReg
, Offset
);
1312 ARM_AM::AMSubMode Mode
= getLoadStoreMultipleSubMode(Opcode
);
1313 if (Mode
== ARM_AM::ia
&& Offset
== -Bytes
) {
1315 } else if (Mode
== ARM_AM::ib
&& Offset
== -Bytes
) {
1318 MergeInstr
= findIncDecAfter(MBBI
, Base
, Pred
, PredReg
, Offset
, TRI
);
1319 if (((Mode
!= ARM_AM::ia
&& Mode
!= ARM_AM::ib
) || Offset
!= Bytes
) &&
1320 ((Mode
!= ARM_AM::da
&& Mode
!= ARM_AM::db
) || Offset
!= -Bytes
)) {
1322 // We couldn't find an inc/dec to merge. But if the base is dead, we
1323 // can still change to a writeback form as that will save us 2 bytes
1324 // of code size. It can create WAW hazards though, so only do it if
1325 // we're minimizing code size.
1326 if (!STI
->hasMinSize() || !BaseKill
)
1329 bool HighRegsUsed
= false;
1330 for (const MachineOperand
&MO
: llvm::drop_begin(MI
->operands(), 2))
1331 if (MO
.getReg() >= ARM::R8
) {
1332 HighRegsUsed
= true;
1337 MergeInstr
= MBB
.end();
1342 if (MergeInstr
!= MBB
.end()) {
1343 LLVM_DEBUG(dbgs() << " Erasing old increment: " << *MergeInstr
);
1344 MBB
.erase(MergeInstr
);
1347 unsigned NewOpc
= getUpdatingLSMultipleOpcode(Opcode
, Mode
);
1348 MachineInstrBuilder MIB
= BuildMI(MBB
, MBBI
, DL
, TII
->get(NewOpc
))
1349 .addReg(Base
, getDefRegState(true)) // WB base register
1350 .addReg(Base
, getKillRegState(BaseKill
))
1351 .addImm(Pred
).addReg(PredReg
);
1353 // Transfer the rest of operands.
1354 for (const MachineOperand
&MO
: llvm::drop_begin(MI
->operands(), 3))
1357 // Transfer memoperands.
1358 MIB
.setMemRefs(MI
->memoperands());
1360 LLVM_DEBUG(dbgs() << " Added new load/store: " << *MIB
);
1365 static unsigned getPreIndexedLoadStoreOpcode(unsigned Opc
,
1366 ARM_AM::AddrOpc Mode
) {
1369 return ARM::LDR_PRE_IMM
;
1371 return ARM::STR_PRE_IMM
;
1373 return Mode
== ARM_AM::add
? ARM::VLDMSIA_UPD
: ARM::VLDMSDB_UPD
;
1375 return Mode
== ARM_AM::add
? ARM::VLDMDIA_UPD
: ARM::VLDMDDB_UPD
;
1377 return Mode
== ARM_AM::add
? ARM::VSTMSIA_UPD
: ARM::VSTMSDB_UPD
;
1379 return Mode
== ARM_AM::add
? ARM::VSTMDIA_UPD
: ARM::VSTMDDB_UPD
;
1382 return ARM::t2LDR_PRE
;
1385 return ARM::t2STR_PRE
;
1386 default: llvm_unreachable("Unhandled opcode!");
1390 static unsigned getPostIndexedLoadStoreOpcode(unsigned Opc
,
1391 ARM_AM::AddrOpc Mode
) {
1394 return ARM::LDR_POST_IMM
;
1396 return ARM::STR_POST_IMM
;
1398 return Mode
== ARM_AM::add
? ARM::VLDMSIA_UPD
: ARM::VLDMSDB_UPD
;
1400 return Mode
== ARM_AM::add
? ARM::VLDMDIA_UPD
: ARM::VLDMDDB_UPD
;
1402 return Mode
== ARM_AM::add
? ARM::VSTMSIA_UPD
: ARM::VSTMSDB_UPD
;
1404 return Mode
== ARM_AM::add
? ARM::VSTMDIA_UPD
: ARM::VSTMDDB_UPD
;
1407 return ARM::t2LDR_POST
;
1409 case ARM::t2LDRBi12
:
1410 return ARM::t2LDRB_POST
;
1411 case ARM::t2LDRSBi8
:
1412 case ARM::t2LDRSBi12
:
1413 return ARM::t2LDRSB_POST
;
1415 case ARM::t2LDRHi12
:
1416 return ARM::t2LDRH_POST
;
1417 case ARM::t2LDRSHi8
:
1418 case ARM::t2LDRSHi12
:
1419 return ARM::t2LDRSH_POST
;
1422 return ARM::t2STR_POST
;
1424 case ARM::t2STRBi12
:
1425 return ARM::t2STRB_POST
;
1427 case ARM::t2STRHi12
:
1428 return ARM::t2STRH_POST
;
1430 case ARM::MVE_VLDRBS16
:
1431 return ARM::MVE_VLDRBS16_post
;
1432 case ARM::MVE_VLDRBS32
:
1433 return ARM::MVE_VLDRBS32_post
;
1434 case ARM::MVE_VLDRBU16
:
1435 return ARM::MVE_VLDRBU16_post
;
1436 case ARM::MVE_VLDRBU32
:
1437 return ARM::MVE_VLDRBU32_post
;
1438 case ARM::MVE_VLDRHS32
:
1439 return ARM::MVE_VLDRHS32_post
;
1440 case ARM::MVE_VLDRHU32
:
1441 return ARM::MVE_VLDRHU32_post
;
1442 case ARM::MVE_VLDRBU8
:
1443 return ARM::MVE_VLDRBU8_post
;
1444 case ARM::MVE_VLDRHU16
:
1445 return ARM::MVE_VLDRHU16_post
;
1446 case ARM::MVE_VLDRWU32
:
1447 return ARM::MVE_VLDRWU32_post
;
1448 case ARM::MVE_VSTRB16
:
1449 return ARM::MVE_VSTRB16_post
;
1450 case ARM::MVE_VSTRB32
:
1451 return ARM::MVE_VSTRB32_post
;
1452 case ARM::MVE_VSTRH32
:
1453 return ARM::MVE_VSTRH32_post
;
1454 case ARM::MVE_VSTRBU8
:
1455 return ARM::MVE_VSTRBU8_post
;
1456 case ARM::MVE_VSTRHU16
:
1457 return ARM::MVE_VSTRHU16_post
;
1458 case ARM::MVE_VSTRWU32
:
1459 return ARM::MVE_VSTRWU32_post
;
1461 default: llvm_unreachable("Unhandled opcode!");
1465 /// Fold proceeding/trailing inc/dec of base register into the
1466 /// LDR/STR/FLD{D|S}/FST{D|S} op when possible:
1467 bool ARMLoadStoreOpt::MergeBaseUpdateLoadStore(MachineInstr
*MI
) {
1468 // Thumb1 doesn't have updating LDR/STR.
1469 // FIXME: Use LDM/STM with single register instead.
1470 if (isThumb1
) return false;
1471 LLVM_DEBUG(dbgs() << "Attempting to merge update of: " << *MI
);
1473 Register Base
= getLoadStoreBaseOp(*MI
).getReg();
1474 bool BaseKill
= getLoadStoreBaseOp(*MI
).isKill();
1475 unsigned Opcode
= MI
->getOpcode();
1476 DebugLoc DL
= MI
->getDebugLoc();
1477 bool isAM5
= (Opcode
== ARM::VLDRD
|| Opcode
== ARM::VLDRS
||
1478 Opcode
== ARM::VSTRD
|| Opcode
== ARM::VSTRS
);
1479 bool isAM2
= (Opcode
== ARM::LDRi12
|| Opcode
== ARM::STRi12
);
1480 if (isi32Load(Opcode
) || isi32Store(Opcode
))
1481 if (MI
->getOperand(2).getImm() != 0)
1483 if (isAM5
&& ARM_AM::getAM5Offset(MI
->getOperand(2).getImm()) != 0)
1486 // Can't do the merge if the destination register is the same as the would-be
1487 // writeback register.
1488 if (MI
->getOperand(0).getReg() == Base
)
1492 ARMCC::CondCodes Pred
= getInstrPredicate(*MI
, PredReg
);
1493 int Bytes
= getLSMultipleTransferSize(MI
);
1494 MachineBasicBlock
&MBB
= *MI
->getParent();
1495 MachineBasicBlock::iterator
MBBI(MI
);
1497 MachineBasicBlock::iterator MergeInstr
1498 = findIncDecBefore(MBBI
, Base
, Pred
, PredReg
, Offset
);
1500 if (!isAM5
&& Offset
== Bytes
) {
1501 NewOpc
= getPreIndexedLoadStoreOpcode(Opcode
, ARM_AM::add
);
1502 } else if (Offset
== -Bytes
) {
1503 NewOpc
= getPreIndexedLoadStoreOpcode(Opcode
, ARM_AM::sub
);
1505 MergeInstr
= findIncDecAfter(MBBI
, Base
, Pred
, PredReg
, Offset
, TRI
);
1506 if (MergeInstr
== MBB
.end())
1509 NewOpc
= getPostIndexedLoadStoreOpcode(Opcode
, ARM_AM::add
);
1510 if ((isAM5
&& Offset
!= Bytes
) ||
1511 (!isAM5
&& !isLegalAddressImm(NewOpc
, Offset
, TII
))) {
1512 NewOpc
= getPostIndexedLoadStoreOpcode(Opcode
, ARM_AM::sub
);
1513 if (isAM5
|| !isLegalAddressImm(NewOpc
, Offset
, TII
))
1517 LLVM_DEBUG(dbgs() << " Erasing old increment: " << *MergeInstr
);
1518 MBB
.erase(MergeInstr
);
1520 ARM_AM::AddrOpc AddSub
= Offset
< 0 ? ARM_AM::sub
: ARM_AM::add
;
1522 bool isLd
= isLoadSingle(Opcode
);
1524 // VLDM[SD]_UPD, VSTM[SD]_UPD
1525 // (There are no base-updating versions of VLDR/VSTR instructions, but the
1526 // updating load/store-multiple instructions can be used with only one
1528 MachineOperand
&MO
= MI
->getOperand(0);
1529 auto MIB
= BuildMI(MBB
, MBBI
, DL
, TII
->get(NewOpc
))
1530 .addReg(Base
, getDefRegState(true)) // WB base register
1531 .addReg(Base
, getKillRegState(isLd
? BaseKill
: false))
1534 .addReg(MO
.getReg(), (isLd
? getDefRegState(true)
1535 : getKillRegState(MO
.isKill())))
1538 LLVM_DEBUG(dbgs() << " Added new instruction: " << *MIB
);
1541 // LDR_PRE, LDR_POST
1542 if (NewOpc
== ARM::LDR_PRE_IMM
|| NewOpc
== ARM::LDRB_PRE_IMM
) {
1544 BuildMI(MBB
, MBBI
, DL
, TII
->get(NewOpc
), MI
->getOperand(0).getReg())
1545 .addReg(Base
, RegState::Define
)
1552 LLVM_DEBUG(dbgs() << " Added new instruction: " << *MIB
);
1554 int Imm
= ARM_AM::getAM2Opc(AddSub
, abs(Offset
), ARM_AM::no_shift
);
1556 BuildMI(MBB
, MBBI
, DL
, TII
->get(NewOpc
), MI
->getOperand(0).getReg())
1557 .addReg(Base
, RegState::Define
)
1561 .add(predOps(Pred
, PredReg
))
1564 LLVM_DEBUG(dbgs() << " Added new instruction: " << *MIB
);
1567 // t2LDR_PRE, t2LDR_POST
1569 BuildMI(MBB
, MBBI
, DL
, TII
->get(NewOpc
), MI
->getOperand(0).getReg())
1570 .addReg(Base
, RegState::Define
)
1573 .add(predOps(Pred
, PredReg
))
1576 LLVM_DEBUG(dbgs() << " Added new instruction: " << *MIB
);
1579 MachineOperand
&MO
= MI
->getOperand(0);
1580 // FIXME: post-indexed stores use am2offset_imm, which still encodes
1581 // the vestigal zero-reg offset register. When that's fixed, this clause
1582 // can be removed entirely.
1583 if (isAM2
&& NewOpc
== ARM::STR_POST_IMM
) {
1584 int Imm
= ARM_AM::getAM2Opc(AddSub
, abs(Offset
), ARM_AM::no_shift
);
1585 // STR_PRE, STR_POST
1586 auto MIB
= BuildMI(MBB
, MBBI
, DL
, TII
->get(NewOpc
), Base
)
1587 .addReg(MO
.getReg(), getKillRegState(MO
.isKill()))
1591 .add(predOps(Pred
, PredReg
))
1594 LLVM_DEBUG(dbgs() << " Added new instruction: " << *MIB
);
1596 // t2STR_PRE, t2STR_POST
1597 auto MIB
= BuildMI(MBB
, MBBI
, DL
, TII
->get(NewOpc
), Base
)
1598 .addReg(MO
.getReg(), getKillRegState(MO
.isKill()))
1601 .add(predOps(Pred
, PredReg
))
1604 LLVM_DEBUG(dbgs() << " Added new instruction: " << *MIB
);
1612 bool ARMLoadStoreOpt::MergeBaseUpdateLSDouble(MachineInstr
&MI
) const {
1613 unsigned Opcode
= MI
.getOpcode();
1614 assert((Opcode
== ARM::t2LDRDi8
|| Opcode
== ARM::t2STRDi8
) &&
1615 "Must have t2STRDi8 or t2LDRDi8");
1616 if (MI
.getOperand(3).getImm() != 0)
1618 LLVM_DEBUG(dbgs() << "Attempting to merge update of: " << MI
);
1620 // Behaviour for writeback is undefined if base register is the same as one
1622 const MachineOperand
&BaseOp
= MI
.getOperand(2);
1623 Register Base
= BaseOp
.getReg();
1624 const MachineOperand
&Reg0Op
= MI
.getOperand(0);
1625 const MachineOperand
&Reg1Op
= MI
.getOperand(1);
1626 if (Reg0Op
.getReg() == Base
|| Reg1Op
.getReg() == Base
)
1630 ARMCC::CondCodes Pred
= getInstrPredicate(MI
, PredReg
);
1631 MachineBasicBlock::iterator
MBBI(MI
);
1632 MachineBasicBlock
&MBB
= *MI
.getParent();
1634 MachineBasicBlock::iterator MergeInstr
= findIncDecBefore(MBBI
, Base
, Pred
,
1637 if (Offset
== 8 || Offset
== -8) {
1638 NewOpc
= Opcode
== ARM::t2LDRDi8
? ARM::t2LDRD_PRE
: ARM::t2STRD_PRE
;
1640 MergeInstr
= findIncDecAfter(MBBI
, Base
, Pred
, PredReg
, Offset
, TRI
);
1641 if (MergeInstr
== MBB
.end())
1643 NewOpc
= Opcode
== ARM::t2LDRDi8
? ARM::t2LDRD_POST
: ARM::t2STRD_POST
;
1644 if (!isLegalAddressImm(NewOpc
, Offset
, TII
))
1647 LLVM_DEBUG(dbgs() << " Erasing old increment: " << *MergeInstr
);
1648 MBB
.erase(MergeInstr
);
1650 DebugLoc DL
= MI
.getDebugLoc();
1651 MachineInstrBuilder MIB
= BuildMI(MBB
, MBBI
, DL
, TII
->get(NewOpc
));
1652 if (NewOpc
== ARM::t2LDRD_PRE
|| NewOpc
== ARM::t2LDRD_POST
) {
1653 MIB
.add(Reg0Op
).add(Reg1Op
).addReg(BaseOp
.getReg(), RegState::Define
);
1655 assert(NewOpc
== ARM::t2STRD_PRE
|| NewOpc
== ARM::t2STRD_POST
);
1656 MIB
.addReg(BaseOp
.getReg(), RegState::Define
).add(Reg0Op
).add(Reg1Op
);
1658 MIB
.addReg(BaseOp
.getReg(), RegState::Kill
)
1659 .addImm(Offset
).addImm(Pred
).addReg(PredReg
);
1660 assert(TII
->get(Opcode
).getNumOperands() == 6 &&
1661 TII
->get(NewOpc
).getNumOperands() == 7 &&
1662 "Unexpected number of operands in Opcode specification.");
1664 // Transfer implicit operands.
1665 for (const MachineOperand
&MO
: MI
.implicit_operands())
1667 MIB
.cloneMemRefs(MI
);
1669 LLVM_DEBUG(dbgs() << " Added new load/store: " << *MIB
);
1674 /// Returns true if instruction is a memory operation that this pass is capable
1675 /// of operating on.
1676 static bool isMemoryOp(const MachineInstr
&MI
) {
1677 unsigned Opcode
= MI
.getOpcode();
1697 if (!MI
.getOperand(1).isReg())
1700 // When no memory operands are present, conservatively assume unaligned,
1701 // volatile, unfoldable.
1702 if (!MI
.hasOneMemOperand())
1705 const MachineMemOperand
&MMO
= **MI
.memoperands_begin();
1707 // Don't touch volatile memory accesses - we may be changing their order.
1708 // TODO: We could allow unordered and monotonic atomics here, but we need to
1709 // make sure the resulting ldm/stm is correctly marked as atomic.
1710 if (MMO
.isVolatile() || MMO
.isAtomic())
1713 // Unaligned ldr/str is emulated by some kernels, but unaligned ldm/stm is
1715 if (MMO
.getAlign() < Align(4))
1718 // str <undef> could probably be eliminated entirely, but for now we just want
1719 // to avoid making a mess of it.
1720 // FIXME: Use str <undef> as a wildcard to enable better stm folding.
1721 if (MI
.getOperand(0).isReg() && MI
.getOperand(0).isUndef())
1724 // Likewise don't mess with references to undefined addresses.
1725 if (MI
.getOperand(1).isUndef())
1731 static void InsertLDR_STR(MachineBasicBlock
&MBB
,
1732 MachineBasicBlock::iterator
&MBBI
, int Offset
,
1733 bool isDef
, unsigned NewOpc
, unsigned Reg
,
1734 bool RegDeadKill
, bool RegUndef
, unsigned BaseReg
,
1735 bool BaseKill
, bool BaseUndef
, ARMCC::CondCodes Pred
,
1736 unsigned PredReg
, const TargetInstrInfo
*TII
,
1739 MachineInstrBuilder MIB
= BuildMI(MBB
, MBBI
, MBBI
->getDebugLoc(),
1741 .addReg(Reg
, getDefRegState(true) | getDeadRegState(RegDeadKill
))
1742 .addReg(BaseReg
, getKillRegState(BaseKill
)|getUndefRegState(BaseUndef
));
1743 MIB
.addImm(Offset
).addImm(Pred
).addReg(PredReg
);
1744 // FIXME: This is overly conservative; the new instruction accesses 4
1746 MIB
.cloneMemRefs(*MI
);
1748 MachineInstrBuilder MIB
= BuildMI(MBB
, MBBI
, MBBI
->getDebugLoc(),
1750 .addReg(Reg
, getKillRegState(RegDeadKill
) | getUndefRegState(RegUndef
))
1751 .addReg(BaseReg
, getKillRegState(BaseKill
)|getUndefRegState(BaseUndef
));
1752 MIB
.addImm(Offset
).addImm(Pred
).addReg(PredReg
);
1753 // FIXME: This is overly conservative; the new instruction accesses 4
1755 MIB
.cloneMemRefs(*MI
);
1759 bool ARMLoadStoreOpt::FixInvalidRegPairOp(MachineBasicBlock
&MBB
,
1760 MachineBasicBlock::iterator
&MBBI
) {
1761 MachineInstr
*MI
= &*MBBI
;
1762 unsigned Opcode
= MI
->getOpcode();
1763 // FIXME: Code/comments below check Opcode == t2STRDi8, but this check returns
1764 // if we see this opcode.
1765 if (Opcode
!= ARM::LDRD
&& Opcode
!= ARM::STRD
&& Opcode
!= ARM::t2LDRDi8
)
1768 const MachineOperand
&BaseOp
= MI
->getOperand(2);
1769 Register BaseReg
= BaseOp
.getReg();
1770 Register EvenReg
= MI
->getOperand(0).getReg();
1771 Register OddReg
= MI
->getOperand(1).getReg();
1772 unsigned EvenRegNum
= TRI
->getDwarfRegNum(EvenReg
, false);
1773 unsigned OddRegNum
= TRI
->getDwarfRegNum(OddReg
, false);
1775 // ARM errata 602117: LDRD with base in list may result in incorrect base
1776 // register when interrupted or faulted.
1777 bool Errata602117
= EvenReg
== BaseReg
&&
1778 (Opcode
== ARM::LDRD
|| Opcode
== ARM::t2LDRDi8
) && STI
->isCortexM3();
1779 // ARM LDRD/STRD needs consecutive registers.
1780 bool NonConsecutiveRegs
= (Opcode
== ARM::LDRD
|| Opcode
== ARM::STRD
) &&
1781 (EvenRegNum
% 2 != 0 || EvenRegNum
+ 1 != OddRegNum
);
1783 if (!Errata602117
&& !NonConsecutiveRegs
)
1786 bool isT2
= Opcode
== ARM::t2LDRDi8
|| Opcode
== ARM::t2STRDi8
;
1787 bool isLd
= Opcode
== ARM::LDRD
|| Opcode
== ARM::t2LDRDi8
;
1788 bool EvenDeadKill
= isLd
?
1789 MI
->getOperand(0).isDead() : MI
->getOperand(0).isKill();
1790 bool EvenUndef
= MI
->getOperand(0).isUndef();
1791 bool OddDeadKill
= isLd
?
1792 MI
->getOperand(1).isDead() : MI
->getOperand(1).isKill();
1793 bool OddUndef
= MI
->getOperand(1).isUndef();
1794 bool BaseKill
= BaseOp
.isKill();
1795 bool BaseUndef
= BaseOp
.isUndef();
1796 assert((isT2
|| MI
->getOperand(3).getReg() == ARM::NoRegister
) &&
1797 "register offset not handled below");
1798 int OffImm
= getMemoryOpOffset(*MI
);
1800 ARMCC::CondCodes Pred
= getInstrPredicate(*MI
, PredReg
);
1802 if (OddRegNum
> EvenRegNum
&& OffImm
== 0) {
1803 // Ascending register numbers and no offset. It's safe to change it to a
1805 unsigned NewOpc
= (isLd
)
1806 ? (isT2
? ARM::t2LDMIA
: ARM::LDMIA
)
1807 : (isT2
? ARM::t2STMIA
: ARM::STMIA
);
1809 BuildMI(MBB
, MBBI
, MBBI
->getDebugLoc(), TII
->get(NewOpc
))
1810 .addReg(BaseReg
, getKillRegState(BaseKill
))
1811 .addImm(Pred
).addReg(PredReg
)
1812 .addReg(EvenReg
, getDefRegState(isLd
) | getDeadRegState(EvenDeadKill
))
1813 .addReg(OddReg
, getDefRegState(isLd
) | getDeadRegState(OddDeadKill
))
1817 BuildMI(MBB
, MBBI
, MBBI
->getDebugLoc(), TII
->get(NewOpc
))
1818 .addReg(BaseReg
, getKillRegState(BaseKill
))
1819 .addImm(Pred
).addReg(PredReg
)
1821 getKillRegState(EvenDeadKill
) | getUndefRegState(EvenUndef
))
1823 getKillRegState(OddDeadKill
) | getUndefRegState(OddUndef
))
1828 // Split into two instructions.
1829 unsigned NewOpc
= (isLd
)
1830 ? (isT2
? (OffImm
< 0 ? ARM::t2LDRi8
: ARM::t2LDRi12
) : ARM::LDRi12
)
1831 : (isT2
? (OffImm
< 0 ? ARM::t2STRi8
: ARM::t2STRi12
) : ARM::STRi12
);
1832 // Be extra careful for thumb2. t2LDRi8 can't reference a zero offset,
1833 // so adjust and use t2LDRi12 here for that.
1834 unsigned NewOpc2
= (isLd
)
1835 ? (isT2
? (OffImm
+4 < 0 ? ARM::t2LDRi8
: ARM::t2LDRi12
) : ARM::LDRi12
)
1836 : (isT2
? (OffImm
+4 < 0 ? ARM::t2STRi8
: ARM::t2STRi12
) : ARM::STRi12
);
1837 // If this is a load, make sure the first load does not clobber the base
1838 // register before the second load reads it.
1839 if (isLd
&& TRI
->regsOverlap(EvenReg
, BaseReg
)) {
1840 assert(!TRI
->regsOverlap(OddReg
, BaseReg
));
1841 InsertLDR_STR(MBB
, MBBI
, OffImm
+ 4, isLd
, NewOpc2
, OddReg
, OddDeadKill
,
1842 false, BaseReg
, false, BaseUndef
, Pred
, PredReg
, TII
, MI
);
1843 InsertLDR_STR(MBB
, MBBI
, OffImm
, isLd
, NewOpc
, EvenReg
, EvenDeadKill
,
1844 false, BaseReg
, BaseKill
, BaseUndef
, Pred
, PredReg
, TII
,
1847 if (OddReg
== EvenReg
&& EvenDeadKill
) {
1848 // If the two source operands are the same, the kill marker is
1849 // probably on the first one. e.g.
1850 // t2STRDi8 killed %r5, %r5, killed %r9, 0, 14, %reg0
1851 EvenDeadKill
= false;
1854 // Never kill the base register in the first instruction.
1855 if (EvenReg
== BaseReg
)
1856 EvenDeadKill
= false;
1857 InsertLDR_STR(MBB
, MBBI
, OffImm
, isLd
, NewOpc
, EvenReg
, EvenDeadKill
,
1858 EvenUndef
, BaseReg
, false, BaseUndef
, Pred
, PredReg
, TII
,
1860 InsertLDR_STR(MBB
, MBBI
, OffImm
+ 4, isLd
, NewOpc2
, OddReg
, OddDeadKill
,
1861 OddUndef
, BaseReg
, BaseKill
, BaseUndef
, Pred
, PredReg
, TII
,
1870 MBBI
= MBB
.erase(MBBI
);
1874 /// An optimization pass to turn multiple LDR / STR ops of the same base and
1875 /// incrementing offset into LDM / STM ops.
1876 bool ARMLoadStoreOpt::LoadStoreMultipleOpti(MachineBasicBlock
&MBB
) {
1878 unsigned CurrBase
= 0;
1879 unsigned CurrOpc
= ~0u;
1880 ARMCC::CondCodes CurrPred
= ARMCC::AL
;
1881 unsigned Position
= 0;
1882 assert(Candidates
.size() == 0);
1883 assert(MergeBaseCandidates
.size() == 0);
1884 LiveRegsValid
= false;
1886 for (MachineBasicBlock::iterator I
= MBB
.end(), MBBI
; I
!= MBB
.begin();
1888 // The instruction in front of the iterator is the one we look at.
1889 MBBI
= std::prev(I
);
1890 if (FixInvalidRegPairOp(MBB
, MBBI
))
1894 if (isMemoryOp(*MBBI
)) {
1895 unsigned Opcode
= MBBI
->getOpcode();
1896 const MachineOperand
&MO
= MBBI
->getOperand(0);
1897 Register Reg
= MO
.getReg();
1898 Register Base
= getLoadStoreBaseOp(*MBBI
).getReg();
1900 ARMCC::CondCodes Pred
= getInstrPredicate(*MBBI
, PredReg
);
1901 int Offset
= getMemoryOpOffset(*MBBI
);
1902 if (CurrBase
== 0) {
1903 // Start of a new chain.
1907 MemOps
.push_back(MemOpQueueEntry(*MBBI
, Offset
, Position
));
1910 // Note: No need to match PredReg in the next if.
1911 if (CurrOpc
== Opcode
&& CurrBase
== Base
&& CurrPred
== Pred
) {
1913 // r4 := ldr [r0, #8]
1914 // r4 := ldr [r0, #4]
1917 // If a load overrides the base register or a register loaded by
1918 // another load in our chain, we cannot take this instruction.
1919 bool Overlap
= false;
1920 if (isLoadSingle(Opcode
)) {
1921 Overlap
= (Base
== Reg
);
1923 for (const MemOpQueueEntry
&E
: MemOps
) {
1924 if (TRI
->regsOverlap(Reg
, E
.MI
->getOperand(0).getReg())) {
1933 // Check offset and sort memory operation into the current chain.
1934 if (Offset
> MemOps
.back().Offset
) {
1935 MemOps
.push_back(MemOpQueueEntry(*MBBI
, Offset
, Position
));
1938 MemOpQueue::iterator MI
, ME
;
1939 for (MI
= MemOps
.begin(), ME
= MemOps
.end(); MI
!= ME
; ++MI
) {
1940 if (Offset
< MI
->Offset
) {
1941 // Found a place to insert.
1944 if (Offset
== MI
->Offset
) {
1945 // Collision, abort.
1950 if (MI
!= MemOps
.end()) {
1951 MemOps
.insert(MI
, MemOpQueueEntry(*MBBI
, Offset
, Position
));
1958 // Don't advance the iterator; The op will start a new chain next.
1961 // Fallthrough to look into existing chain.
1962 } else if (MBBI
->isDebugInstr()) {
1964 } else if (MBBI
->getOpcode() == ARM::t2LDRDi8
||
1965 MBBI
->getOpcode() == ARM::t2STRDi8
) {
1966 // ARMPreAllocLoadStoreOpt has already formed some LDRD/STRD instructions
1967 // remember them because we may still be able to merge add/sub into them.
1968 MergeBaseCandidates
.push_back(&*MBBI
);
1971 // If we are here then the chain is broken; Extract candidates for a merge.
1972 if (MemOps
.size() > 0) {
1973 FormCandidates(MemOps
);
1974 // Reset for the next chain.
1977 CurrPred
= ARMCC::AL
;
1981 if (MemOps
.size() > 0)
1982 FormCandidates(MemOps
);
1984 // Sort candidates so they get processed from end to begin of the basic
1985 // block later; This is necessary for liveness calculation.
1986 auto LessThan
= [](const MergeCandidate
* M0
, const MergeCandidate
*M1
) {
1987 return M0
->InsertPos
< M1
->InsertPos
;
1989 llvm::sort(Candidates
, LessThan
);
1991 // Go through list of candidates and merge.
1992 bool Changed
= false;
1993 for (const MergeCandidate
*Candidate
: Candidates
) {
1994 if (Candidate
->CanMergeToLSMulti
|| Candidate
->CanMergeToLSDouble
) {
1995 MachineInstr
*Merged
= MergeOpsUpdate(*Candidate
);
1996 // Merge preceding/trailing base inc/dec into the merged op.
1999 unsigned Opcode
= Merged
->getOpcode();
2000 if (Opcode
== ARM::t2STRDi8
|| Opcode
== ARM::t2LDRDi8
)
2001 MergeBaseUpdateLSDouble(*Merged
);
2003 MergeBaseUpdateLSMultiple(Merged
);
2005 for (MachineInstr
*MI
: Candidate
->Instrs
) {
2006 if (MergeBaseUpdateLoadStore(MI
))
2011 assert(Candidate
->Instrs
.size() == 1);
2012 if (MergeBaseUpdateLoadStore(Candidate
->Instrs
.front()))
2017 // Try to fold add/sub into the LDRD/STRD formed by ARMPreAllocLoadStoreOpt.
2018 for (MachineInstr
*MI
: MergeBaseCandidates
)
2019 MergeBaseUpdateLSDouble(*MI
);
2020 MergeBaseCandidates
.clear();
2025 /// If this is a exit BB, try merging the return ops ("bx lr" and "mov pc, lr")
2026 /// into the preceding stack restore so it directly restore the value of LR
2028 /// ldmfd sp!, {..., lr}
2031 /// ldmfd sp!, {..., lr}
2034 /// ldmfd sp!, {..., pc}
2035 bool ARMLoadStoreOpt::MergeReturnIntoLDM(MachineBasicBlock
&MBB
) {
2036 // Thumb1 LDM doesn't allow high registers.
2037 if (isThumb1
) return false;
2038 if (MBB
.empty()) return false;
2040 MachineBasicBlock::iterator MBBI
= MBB
.getLastNonDebugInstr();
2041 if (MBBI
!= MBB
.begin() && MBBI
!= MBB
.end() &&
2042 (MBBI
->getOpcode() == ARM::BX_RET
||
2043 MBBI
->getOpcode() == ARM::tBX_RET
||
2044 MBBI
->getOpcode() == ARM::MOVPCLR
)) {
2045 MachineBasicBlock::iterator PrevI
= std::prev(MBBI
);
2046 // Ignore any debug instructions.
2047 while (PrevI
->isDebugInstr() && PrevI
!= MBB
.begin())
2049 MachineInstr
&PrevMI
= *PrevI
;
2050 unsigned Opcode
= PrevMI
.getOpcode();
2051 if (Opcode
== ARM::LDMIA_UPD
|| Opcode
== ARM::LDMDA_UPD
||
2052 Opcode
== ARM::LDMDB_UPD
|| Opcode
== ARM::LDMIB_UPD
||
2053 Opcode
== ARM::t2LDMIA_UPD
|| Opcode
== ARM::t2LDMDB_UPD
) {
2054 MachineOperand
&MO
= PrevMI
.getOperand(PrevMI
.getNumOperands() - 1);
2055 if (MO
.getReg() != ARM::LR
)
2057 unsigned NewOpc
= (isThumb2
? ARM::t2LDMIA_RET
: ARM::LDMIA_RET
);
2058 assert(((isThumb2
&& Opcode
== ARM::t2LDMIA_UPD
) ||
2059 Opcode
== ARM::LDMIA_UPD
) && "Unsupported multiple load-return!");
2060 PrevMI
.setDesc(TII
->get(NewOpc
));
2062 PrevMI
.copyImplicitOps(*MBB
.getParent(), *MBBI
);
2070 bool ARMLoadStoreOpt::CombineMovBx(MachineBasicBlock
&MBB
) {
2071 MachineBasicBlock::iterator MBBI
= MBB
.getFirstTerminator();
2072 if (MBBI
== MBB
.begin() || MBBI
== MBB
.end() ||
2073 MBBI
->getOpcode() != ARM::tBX_RET
)
2076 MachineBasicBlock::iterator Prev
= MBBI
;
2078 if (Prev
->getOpcode() != ARM::tMOVr
||
2079 !Prev
->definesRegister(ARM::LR
, /*TRI=*/nullptr))
2082 for (auto Use
: Prev
->uses())
2084 assert(STI
->hasV4TOps());
2085 BuildMI(MBB
, MBBI
, MBBI
->getDebugLoc(), TII
->get(ARM::tBX
))
2086 .addReg(Use
.getReg(), RegState::Kill
)
2087 .add(predOps(ARMCC::AL
))
2088 .copyImplicitOps(*MBBI
);
2094 llvm_unreachable("tMOVr doesn't kill a reg before tBX_RET?");
2097 bool ARMLoadStoreOpt::runOnMachineFunction(MachineFunction
&Fn
) {
2098 if (skipFunction(Fn
.getFunction()))
2102 STI
= &Fn
.getSubtarget
<ARMSubtarget
>();
2103 TL
= STI
->getTargetLowering();
2104 AFI
= Fn
.getInfo
<ARMFunctionInfo
>();
2105 TII
= STI
->getInstrInfo();
2106 TRI
= STI
->getRegisterInfo();
2108 RegClassInfoValid
= false;
2109 isThumb2
= AFI
->isThumb2Function();
2110 isThumb1
= AFI
->isThumbFunction() && !isThumb2
;
2112 bool Modified
= false, ModifiedLDMReturn
= false;
2113 for (MachineBasicBlock
&MBB
: Fn
) {
2114 Modified
|= LoadStoreMultipleOpti(MBB
);
2115 if (STI
->hasV5TOps() && !AFI
->shouldSignReturnAddress())
2116 ModifiedLDMReturn
|= MergeReturnIntoLDM(MBB
);
2118 Modified
|= CombineMovBx(MBB
);
2120 Modified
|= ModifiedLDMReturn
;
2122 // If we merged a BX instruction into an LDM, we need to re-calculate whether
2123 // LR is restored. This check needs to consider the whole function, not just
2124 // the instruction(s) we changed, because there may be other BX returns which
2125 // still need LR to be restored.
2126 if (ModifiedLDMReturn
)
2127 ARMFrameLowering::updateLRRestored(Fn
);
2129 Allocator
.DestroyAll();
2133 #define ARM_PREALLOC_LOAD_STORE_OPT_NAME \
2134 "ARM pre- register allocation load / store optimization pass"
2138 /// Pre- register allocation pass that move load / stores from consecutive
2139 /// locations close to make it more likely they will be combined later.
2140 struct ARMPreAllocLoadStoreOpt
: public MachineFunctionPass
{
2144 const DataLayout
*TD
;
2145 const TargetInstrInfo
*TII
;
2146 const TargetRegisterInfo
*TRI
;
2147 const ARMSubtarget
*STI
;
2148 MachineRegisterInfo
*MRI
;
2149 MachineDominatorTree
*DT
;
2150 MachineFunction
*MF
;
2152 ARMPreAllocLoadStoreOpt() : MachineFunctionPass(ID
) {}
2154 bool runOnMachineFunction(MachineFunction
&Fn
) override
;
2156 StringRef
getPassName() const override
{
2157 return ARM_PREALLOC_LOAD_STORE_OPT_NAME
;
2160 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
2161 AU
.addRequired
<AAResultsWrapperPass
>();
2162 AU
.addRequired
<MachineDominatorTreeWrapperPass
>();
2163 AU
.addPreserved
<MachineDominatorTreeWrapperPass
>();
2164 MachineFunctionPass::getAnalysisUsage(AU
);
2168 bool CanFormLdStDWord(MachineInstr
*Op0
, MachineInstr
*Op1
, DebugLoc
&dl
,
2169 unsigned &NewOpc
, Register
&EvenReg
, Register
&OddReg
,
2170 Register
&BaseReg
, int &Offset
, Register
&PredReg
,
2171 ARMCC::CondCodes
&Pred
, bool &isT2
);
2173 MachineBasicBlock
*MBB
, SmallVectorImpl
<MachineInstr
*> &Ops
,
2174 unsigned Base
, bool isLd
, DenseMap
<MachineInstr
*, unsigned> &MI2LocMap
,
2175 SmallDenseMap
<Register
, SmallVector
<MachineInstr
*>, 8> &RegisterMap
);
2176 bool RescheduleLoadStoreInstrs(MachineBasicBlock
*MBB
);
2177 bool DistributeIncrements();
2178 bool DistributeIncrements(Register Base
);
2181 } // end anonymous namespace
2183 char ARMPreAllocLoadStoreOpt::ID
= 0;
2185 INITIALIZE_PASS_BEGIN(ARMPreAllocLoadStoreOpt
, "arm-prera-ldst-opt",
2186 ARM_PREALLOC_LOAD_STORE_OPT_NAME
, false, false)
2187 INITIALIZE_PASS_DEPENDENCY(MachineDominatorTreeWrapperPass
)
2188 INITIALIZE_PASS_END(ARMPreAllocLoadStoreOpt
, "arm-prera-ldst-opt",
2189 ARM_PREALLOC_LOAD_STORE_OPT_NAME
, false, false)
2191 // Limit the number of instructions to be rescheduled.
2192 // FIXME: tune this limit, and/or come up with some better heuristics.
2193 static cl::opt
<unsigned> InstReorderLimit("arm-prera-ldst-opt-reorder-limit",
2194 cl::init(8), cl::Hidden
);
2196 bool ARMPreAllocLoadStoreOpt::runOnMachineFunction(MachineFunction
&Fn
) {
2197 if (AssumeMisalignedLoadStores
|| skipFunction(Fn
.getFunction()))
2200 TD
= &Fn
.getDataLayout();
2201 STI
= &Fn
.getSubtarget
<ARMSubtarget
>();
2202 TII
= STI
->getInstrInfo();
2203 TRI
= STI
->getRegisterInfo();
2204 MRI
= &Fn
.getRegInfo();
2205 DT
= &getAnalysis
<MachineDominatorTreeWrapperPass
>().getDomTree();
2207 AA
= &getAnalysis
<AAResultsWrapperPass
>().getAAResults();
2209 bool Modified
= DistributeIncrements();
2210 for (MachineBasicBlock
&MFI
: Fn
)
2211 Modified
|= RescheduleLoadStoreInstrs(&MFI
);
2216 static bool IsSafeAndProfitableToMove(bool isLd
, unsigned Base
,
2217 MachineBasicBlock::iterator I
,
2218 MachineBasicBlock::iterator E
,
2219 SmallPtrSetImpl
<MachineInstr
*> &MemOps
,
2220 SmallSet
<unsigned, 4> &MemRegs
,
2221 const TargetRegisterInfo
*TRI
,
2222 AliasAnalysis
*AA
) {
2223 // Are there stores / loads / calls between them?
2224 SmallSet
<unsigned, 4> AddedRegPressure
;
2226 if (I
->isDebugInstr() || MemOps
.count(&*I
))
2228 if (I
->isCall() || I
->isTerminator() || I
->hasUnmodeledSideEffects())
2230 if (I
->mayStore() || (!isLd
&& I
->mayLoad()))
2231 for (MachineInstr
*MemOp
: MemOps
)
2232 if (I
->mayAlias(AA
, *MemOp
, /*UseTBAA*/ false))
2234 for (unsigned j
= 0, NumOps
= I
->getNumOperands(); j
!= NumOps
; ++j
) {
2235 MachineOperand
&MO
= I
->getOperand(j
);
2238 Register Reg
= MO
.getReg();
2239 if (MO
.isDef() && TRI
->regsOverlap(Reg
, Base
))
2241 if (Reg
!= Base
&& !MemRegs
.count(Reg
))
2242 AddedRegPressure
.insert(Reg
);
2246 // Estimate register pressure increase due to the transformation.
2247 if (MemRegs
.size() <= 4)
2248 // Ok if we are moving small number of instructions.
2250 return AddedRegPressure
.size() <= MemRegs
.size() * 2;
2253 bool ARMPreAllocLoadStoreOpt::CanFormLdStDWord(
2254 MachineInstr
*Op0
, MachineInstr
*Op1
, DebugLoc
&dl
, unsigned &NewOpc
,
2255 Register
&FirstReg
, Register
&SecondReg
, Register
&BaseReg
, int &Offset
,
2256 Register
&PredReg
, ARMCC::CondCodes
&Pred
, bool &isT2
) {
2257 // Make sure we're allowed to generate LDRD/STRD.
2258 if (!STI
->hasV5TEOps())
2261 // FIXME: VLDRS / VSTRS -> VLDRD / VSTRD
2263 unsigned Opcode
= Op0
->getOpcode();
2264 if (Opcode
== ARM::LDRi12
) {
2266 } else if (Opcode
== ARM::STRi12
) {
2268 } else if (Opcode
== ARM::t2LDRi8
|| Opcode
== ARM::t2LDRi12
) {
2269 NewOpc
= ARM::t2LDRDi8
;
2272 } else if (Opcode
== ARM::t2STRi8
|| Opcode
== ARM::t2STRi12
) {
2273 NewOpc
= ARM::t2STRDi8
;
2280 // Make sure the base address satisfies i64 ld / st alignment requirement.
2281 // At the moment, we ignore the memoryoperand's value.
2282 // If we want to use AliasAnalysis, we should check it accordingly.
2283 if (!Op0
->hasOneMemOperand() ||
2284 (*Op0
->memoperands_begin())->isVolatile() ||
2285 (*Op0
->memoperands_begin())->isAtomic())
2288 Align Alignment
= (*Op0
->memoperands_begin())->getAlign();
2289 Align ReqAlign
= STI
->getDualLoadStoreAlignment();
2290 if (Alignment
< ReqAlign
)
2293 // Then make sure the immediate offset fits.
2294 int OffImm
= getMemoryOpOffset(*Op0
);
2296 int Limit
= (1 << 8) * Scale
;
2297 if (OffImm
>= Limit
|| (OffImm
<= -Limit
) || (OffImm
& (Scale
-1)))
2301 ARM_AM::AddrOpc AddSub
= ARM_AM::add
;
2303 AddSub
= ARM_AM::sub
;
2306 int Limit
= (1 << 8) * Scale
;
2307 if (OffImm
>= Limit
|| (OffImm
& (Scale
-1)))
2309 Offset
= ARM_AM::getAM3Opc(AddSub
, OffImm
);
2311 FirstReg
= Op0
->getOperand(0).getReg();
2312 SecondReg
= Op1
->getOperand(0).getReg();
2313 if (FirstReg
== SecondReg
)
2315 BaseReg
= Op0
->getOperand(1).getReg();
2316 Pred
= getInstrPredicate(*Op0
, PredReg
);
2317 dl
= Op0
->getDebugLoc();
2321 bool ARMPreAllocLoadStoreOpt::RescheduleOps(
2322 MachineBasicBlock
*MBB
, SmallVectorImpl
<MachineInstr
*> &Ops
, unsigned Base
,
2323 bool isLd
, DenseMap
<MachineInstr
*, unsigned> &MI2LocMap
,
2324 SmallDenseMap
<Register
, SmallVector
<MachineInstr
*>, 8> &RegisterMap
) {
2325 bool RetVal
= false;
2327 // Sort by offset (in reverse order).
2328 llvm::sort(Ops
, [](const MachineInstr
*LHS
, const MachineInstr
*RHS
) {
2329 int LOffset
= getMemoryOpOffset(*LHS
);
2330 int ROffset
= getMemoryOpOffset(*RHS
);
2331 assert(LHS
== RHS
|| LOffset
!= ROffset
);
2332 return LOffset
> ROffset
;
2335 // The loads / stores of the same base are in order. Scan them from first to
2336 // last and check for the following:
2337 // 1. Any def of base.
2339 while (Ops
.size() > 1) {
2340 unsigned FirstLoc
= ~0U;
2341 unsigned LastLoc
= 0;
2342 MachineInstr
*FirstOp
= nullptr;
2343 MachineInstr
*LastOp
= nullptr;
2345 unsigned LastOpcode
= 0;
2346 unsigned LastBytes
= 0;
2347 unsigned NumMove
= 0;
2348 for (MachineInstr
*Op
: llvm::reverse(Ops
)) {
2349 // Make sure each operation has the same kind.
2351 = getLoadStoreMultipleOpcode(Op
->getOpcode(), ARM_AM::ia
);
2352 if (LastOpcode
&& LSMOpcode
!= LastOpcode
)
2355 // Check that we have a continuous set of offsets.
2356 int Offset
= getMemoryOpOffset(*Op
);
2357 unsigned Bytes
= getLSMultipleTransferSize(Op
);
2359 if (Bytes
!= LastBytes
|| Offset
!= (LastOffset
+ (int)Bytes
))
2363 // Don't try to reschedule too many instructions.
2364 if (NumMove
== InstReorderLimit
)
2367 // Found a mergable instruction; save information about it.
2369 LastOffset
= Offset
;
2371 LastOpcode
= LSMOpcode
;
2373 unsigned Loc
= MI2LocMap
[Op
];
2374 if (Loc
<= FirstLoc
) {
2378 if (Loc
>= LastLoc
) {
2387 SmallPtrSet
<MachineInstr
*, 4> MemOps
;
2388 SmallSet
<unsigned, 4> MemRegs
;
2389 for (size_t i
= Ops
.size() - NumMove
, e
= Ops
.size(); i
!= e
; ++i
) {
2390 MemOps
.insert(Ops
[i
]);
2391 MemRegs
.insert(Ops
[i
]->getOperand(0).getReg());
2394 // Be conservative, if the instructions are too far apart, don't
2395 // move them. We want to limit the increase of register pressure.
2396 bool DoMove
= (LastLoc
- FirstLoc
) <= NumMove
*4; // FIXME: Tune this.
2398 DoMove
= IsSafeAndProfitableToMove(isLd
, Base
, FirstOp
, LastOp
,
2399 MemOps
, MemRegs
, TRI
, AA
);
2401 for (unsigned i
= 0; i
!= NumMove
; ++i
)
2404 // This is the new location for the loads / stores.
2405 MachineBasicBlock::iterator InsertPos
= isLd
? FirstOp
: LastOp
;
2406 while (InsertPos
!= MBB
->end() &&
2407 (MemOps
.count(&*InsertPos
) || InsertPos
->isDebugInstr()))
2410 // If we are moving a pair of loads / stores, see if it makes sense
2411 // to try to allocate a pair of registers that can form register pairs.
2412 MachineInstr
*Op0
= Ops
.back();
2413 MachineInstr
*Op1
= Ops
[Ops
.size()-2];
2414 Register FirstReg
, SecondReg
;
2415 Register BaseReg
, PredReg
;
2416 ARMCC::CondCodes Pred
= ARMCC::AL
;
2418 unsigned NewOpc
= 0;
2421 if (NumMove
== 2 && CanFormLdStDWord(Op0
, Op1
, dl
, NewOpc
,
2422 FirstReg
, SecondReg
, BaseReg
,
2423 Offset
, PredReg
, Pred
, isT2
)) {
2427 const MCInstrDesc
&MCID
= TII
->get(NewOpc
);
2428 const TargetRegisterClass
*TRC
= TII
->getRegClass(MCID
, 0, TRI
, *MF
);
2429 MRI
->constrainRegClass(FirstReg
, TRC
);
2430 MRI
->constrainRegClass(SecondReg
, TRC
);
2432 // Form the pair instruction.
2434 MachineInstrBuilder MIB
= BuildMI(*MBB
, InsertPos
, dl
, MCID
)
2435 .addReg(FirstReg
, RegState::Define
)
2436 .addReg(SecondReg
, RegState::Define
)
2438 // FIXME: We're converting from LDRi12 to an insn that still
2439 // uses addrmode2, so we need an explicit offset reg. It should
2440 // always by reg0 since we're transforming LDRi12s.
2443 MIB
.addImm(Offset
).addImm(Pred
).addReg(PredReg
);
2444 MIB
.cloneMergedMemRefs({Op0
, Op1
});
2445 LLVM_DEBUG(dbgs() << "Formed " << *MIB
<< "\n");
2448 MachineInstrBuilder MIB
= BuildMI(*MBB
, InsertPos
, dl
, MCID
)
2452 // FIXME: We're converting from LDRi12 to an insn that still
2453 // uses addrmode2, so we need an explicit offset reg. It should
2454 // always by reg0 since we're transforming STRi12s.
2457 MIB
.addImm(Offset
).addImm(Pred
).addReg(PredReg
);
2458 MIB
.cloneMergedMemRefs({Op0
, Op1
});
2459 LLVM_DEBUG(dbgs() << "Formed " << *MIB
<< "\n");
2466 // Add register allocation hints to form register pairs.
2467 MRI
->setRegAllocationHint(FirstReg
, ARMRI::RegPairEven
, SecondReg
);
2468 MRI
->setRegAllocationHint(SecondReg
, ARMRI::RegPairOdd
, FirstReg
);
2471 for (unsigned i
= 0; i
!= NumMove
; ++i
) {
2472 MachineInstr
*Op
= Ops
.pop_back_val();
2474 // Populate RegisterMap with all Registers defined by loads.
2475 Register Reg
= Op
->getOperand(0).getReg();
2479 MBB
->splice(InsertPos
, MBB
, Op
);
2483 NumLdStMoved
+= NumMove
;
2492 static void forEachDbgRegOperand(MachineInstr
*MI
,
2493 std::function
<void(MachineOperand
&)> Fn
) {
2494 if (MI
->isNonListDebugValue()) {
2495 auto &Op
= MI
->getOperand(0);
2499 for (unsigned I
= 2; I
< MI
->getNumOperands(); I
++) {
2500 auto &Op
= MI
->getOperand(I
);
2507 // Update the RegisterMap with the instruction that was moved because a
2508 // DBG_VALUE_LIST may need to be moved again.
2509 static void updateRegisterMapForDbgValueListAfterMove(
2510 SmallDenseMap
<Register
, SmallVector
<MachineInstr
*>, 8> &RegisterMap
,
2511 MachineInstr
*DbgValueListInstr
, MachineInstr
*InstrToReplace
) {
2513 forEachDbgRegOperand(DbgValueListInstr
, [&](MachineOperand
&Op
) {
2514 auto RegIt
= RegisterMap
.find(Op
.getReg());
2515 if (RegIt
== RegisterMap
.end())
2517 auto &InstrVec
= RegIt
->getSecond();
2518 llvm::replace(InstrVec
, InstrToReplace
, DbgValueListInstr
);
2522 static DebugVariable
createDebugVariableFromMachineInstr(MachineInstr
*MI
) {
2523 auto DbgVar
= DebugVariable(MI
->getDebugVariable(), MI
->getDebugExpression(),
2524 MI
->getDebugLoc()->getInlinedAt());
2529 ARMPreAllocLoadStoreOpt::RescheduleLoadStoreInstrs(MachineBasicBlock
*MBB
) {
2530 bool RetVal
= false;
2532 DenseMap
<MachineInstr
*, unsigned> MI2LocMap
;
2533 using Base2InstMap
= DenseMap
<unsigned, SmallVector
<MachineInstr
*, 4>>;
2534 using BaseVec
= SmallVector
<unsigned, 4>;
2535 Base2InstMap Base2LdsMap
;
2536 Base2InstMap Base2StsMap
;
2539 // This map is used to track the relationship between the virtual
2540 // register that is the result of a load that is moved and the DBG_VALUE
2541 // MachineInstr pointer that uses that virtual register.
2542 SmallDenseMap
<Register
, SmallVector
<MachineInstr
*>, 8> RegisterMap
;
2545 MachineBasicBlock::iterator MBBI
= MBB
->begin();
2546 MachineBasicBlock::iterator E
= MBB
->end();
2548 for (; MBBI
!= E
; ++MBBI
) {
2549 MachineInstr
&MI
= *MBBI
;
2550 if (MI
.isCall() || MI
.isTerminator()) {
2551 // Stop at barriers.
2556 if (!MI
.isDebugInstr())
2557 MI2LocMap
[&MI
] = ++Loc
;
2559 if (!isMemoryOp(MI
))
2562 if (getInstrPredicate(MI
, PredReg
) != ARMCC::AL
)
2565 int Opc
= MI
.getOpcode();
2566 bool isLd
= isLoadSingle(Opc
);
2567 Register Base
= MI
.getOperand(1).getReg();
2568 int Offset
= getMemoryOpOffset(MI
);
2569 bool StopHere
= false;
2570 auto FindBases
= [&](Base2InstMap
&Base2Ops
, BaseVec
&Bases
) {
2571 auto [BI
, Inserted
] = Base2Ops
.try_emplace(Base
);
2573 BI
->second
.push_back(&MI
);
2574 Bases
.push_back(Base
);
2577 for (const MachineInstr
*MI
: BI
->second
) {
2578 if (Offset
== getMemoryOpOffset(*MI
)) {
2584 BI
->second
.push_back(&MI
);
2588 FindBases(Base2LdsMap
, LdBases
);
2590 FindBases(Base2StsMap
, StBases
);
2593 // Found a duplicate (a base+offset combination that's seen earlier).
2600 // Re-schedule loads.
2601 for (unsigned Base
: LdBases
) {
2602 SmallVectorImpl
<MachineInstr
*> &Lds
= Base2LdsMap
[Base
];
2604 RetVal
|= RescheduleOps(MBB
, Lds
, Base
, true, MI2LocMap
, RegisterMap
);
2607 // Re-schedule stores.
2608 for (unsigned Base
: StBases
) {
2609 SmallVectorImpl
<MachineInstr
*> &Sts
= Base2StsMap
[Base
];
2611 RetVal
|= RescheduleOps(MBB
, Sts
, Base
, false, MI2LocMap
, RegisterMap
);
2615 Base2LdsMap
.clear();
2616 Base2StsMap
.clear();
2622 // Reschedule DBG_VALUEs to match any loads that were moved. When a load is
2623 // sunk beyond a DBG_VALUE that is referring to it, the DBG_VALUE becomes a
2624 // use-before-def, resulting in a loss of debug info.
2627 // Before the Pre Register Allocation Load Store Pass
2631 // DBG_VALUE %2, "x", ...
2637 // DBG_VALUE %2, "x", ...
2641 // The code below addresses this by moving the DBG_VALUE to the position
2642 // immediately after the load.
2645 // After the code below:
2649 // DBG_VALUE %2, "x", ...
2652 // The algorithm works in two phases: First RescheduleOps() populates the
2653 // RegisterMap with registers that were moved as keys, there is no value
2654 // inserted. In the next phase, every MachineInstr in a basic block is
2655 // iterated over. If it is a valid DBG_VALUE or DBG_VALUE_LIST and it uses one
2656 // or more registers in the RegisterMap, the RegisterMap and InstrMap are
2657 // populated with the MachineInstr. If the DBG_VALUE or DBG_VALUE_LIST
2658 // describes debug information for a variable that already exists in the
2659 // DbgValueSinkCandidates, the MachineInstr in the DbgValueSinkCandidates must
2660 // be set to undef. If the current MachineInstr is a load that was moved,
2661 // undef the corresponding DBG_VALUE or DBG_VALUE_LIST and clone it to below
2664 // To illustrate the above algorithm visually let's take this example.
2666 // Before the Pre Register Allocation Load Store Pass:
2668 // DBG_VALUE %2, A, .... # X
2669 // DBG_VALUE 0, A, ... # Y
2671 // DBG_VALUE %3, A, ..., # Z
2674 // After Pre Register Allocation Load Store Pass:
2675 // DBG_VALUE %2, A, .... # X
2676 // DBG_VALUE 0, A, ... # Y
2677 // DBG_VALUE %3, A, ..., # Z
2682 // The algorithm below does the following:
2684 // In the beginning, the RegisterMap will have been populated with the virtual
2685 // registers %2, and %3, the DbgValueSinkCandidates and the InstrMap will be
2686 // empty. DbgValueSinkCandidates = {}, RegisterMap = {2 -> {}, 3 -> {}},
2688 // -> DBG_VALUE %2, A, .... # X
2689 // DBG_VALUE 0, A, ... # Y
2690 // DBG_VALUE %3, A, ..., # Z
2695 // After the first DBG_VALUE (denoted with an X) is processed, the
2696 // DbgValueSinkCandidates and InstrMap will be populated and the RegisterMap
2697 // entry for %2 will be populated as well. DbgValueSinkCandidates = {A -> X},
2698 // RegisterMap = {2 -> {X}, 3 -> {}}, InstrMap {X -> 2}
2699 // DBG_VALUE %2, A, .... # X
2700 // -> DBG_VALUE 0, A, ... # Y
2701 // DBG_VALUE %3, A, ..., # Z
2706 // After the DBG_VALUE Y is processed, the DbgValueSinkCandidates is updated
2707 // to now hold Y for A and the RegisterMap is also updated to remove X from
2708 // %2, this is because both X and Y describe the same debug variable A. X is
2709 // also updated to have a $noreg as the first operand.
2710 // DbgValueSinkCandidates = {A -> {Y}}, RegisterMap = {2 -> {}, 3 -> {}},
2711 // InstrMap = {X-> 2}
2712 // DBG_VALUE $noreg, A, .... # X
2713 // DBG_VALUE 0, A, ... # Y
2714 // -> DBG_VALUE %3, A, ..., # Z
2719 // After DBG_VALUE Z is processed, the DbgValueSinkCandidates is updated to
2720 // hold Z fr A, the RegisterMap is updated to hold Z for %3, and the InstrMap
2721 // is updated to have Z mapped to %3. This is again because Z describes the
2722 // debug variable A, Y is not updated to have $noreg as first operand because
2723 // its first operand is an immediate, not a register.
2724 // DbgValueSinkCandidates = {A -> {Z}}, RegisterMap = {2 -> {}, 3 -> {Z}},
2725 // InstrMap = {X -> 2, Z -> 3}
2726 // DBG_VALUE $noreg, A, .... # X
2727 // DBG_VALUE 0, A, ... # Y
2728 // DBG_VALUE %3, A, ..., # Z
2733 // Nothing happens here since the RegisterMap for %2 contains no value.
2734 // DbgValueSinkCandidates = {A -> {Z}}, RegisterMap = {2 -> {}, 3 -> {Z}},
2735 // InstrMap = {X -> 2, Z -> 3}
2736 // DBG_VALUE $noreg, A, .... # X
2737 // DBG_VALUE 0, A, ... # Y
2738 // DBG_VALUE %3, A, ..., # Z
2743 // Since the RegisterMap contains Z as a value for %3, the MachineInstr
2744 // pointer Z is copied to come after the load for %3 and the old Z's first
2745 // operand is changed to $noreg the Basic Block iterator is moved to after the
2746 // DBG_VALUE Z's new position.
2747 // DbgValueSinkCandidates = {A -> {Z}}, RegisterMap = {2 -> {}, 3 -> {Z}},
2748 // InstrMap = {X -> 2, Z -> 3}
2749 // DBG_VALUE $noreg, A, .... # X
2750 // DBG_VALUE 0, A, ... # Y
2751 // DBG_VALUE $noreg, A, ..., # Old Z
2754 // DBG_VALUE %3, A, ..., # Z
2757 // Nothing happens for %4 and the algorithm exits having processed the entire
2759 // DbgValueSinkCandidates = {A -> {Z}}, RegisterMap = {2 -> {}, 3 -> {Z}},
2760 // InstrMap = {X -> 2, Z -> 3}
2761 // DBG_VALUE $noreg, A, .... # X
2762 // DBG_VALUE 0, A, ... # Y
2763 // DBG_VALUE $noreg, A, ..., # Old Z
2766 // DBG_VALUE %3, A, ..., # Z
2769 // This map is used to track the relationship between
2770 // a Debug Variable and the DBG_VALUE MachineInstr pointer that describes the
2771 // debug information for that Debug Variable.
2772 SmallDenseMap
<DebugVariable
, MachineInstr
*, 8> DbgValueSinkCandidates
;
2773 // This map is used to track the relationship between a DBG_VALUE or
2774 // DBG_VALUE_LIST MachineInstr pointer and Registers that it uses.
2775 SmallDenseMap
<MachineInstr
*, SmallVector
<Register
>, 8> InstrMap
;
2776 for (MBBI
= MBB
->begin(), E
= MBB
->end(); MBBI
!= E
; ++MBBI
) {
2777 MachineInstr
&MI
= *MBBI
;
2779 auto PopulateRegisterAndInstrMapForDebugInstr
= [&](Register Reg
) {
2780 auto RegIt
= RegisterMap
.find(Reg
);
2781 if (RegIt
== RegisterMap
.end())
2783 auto &InstrVec
= RegIt
->getSecond();
2784 InstrVec
.push_back(&MI
);
2785 InstrMap
[&MI
].push_back(Reg
);
2788 if (MI
.isDebugValue()) {
2789 assert(MI
.getDebugVariable() &&
2790 "DBG_VALUE or DBG_VALUE_LIST must contain a DILocalVariable");
2792 auto DbgVar
= createDebugVariableFromMachineInstr(&MI
);
2793 // If the first operand is a register and it exists in the RegisterMap, we
2794 // know this is a DBG_VALUE that uses the result of a load that was moved,
2795 // and is therefore a candidate to also be moved, add it to the
2796 // RegisterMap and InstrMap.
2797 forEachDbgRegOperand(&MI
, [&](MachineOperand
&Op
) {
2798 PopulateRegisterAndInstrMapForDebugInstr(Op
.getReg());
2801 // If the current DBG_VALUE describes the same variable as one of the
2802 // in-flight DBG_VALUEs, remove the candidate from the list and set it to
2803 // undef. Moving one DBG_VALUE past another would result in the variable's
2804 // value going back in time when stepping through the block in the
2806 auto InstrIt
= DbgValueSinkCandidates
.find(DbgVar
);
2807 if (InstrIt
!= DbgValueSinkCandidates
.end()) {
2808 auto *Instr
= InstrIt
->getSecond();
2809 auto RegIt
= InstrMap
.find(Instr
);
2810 if (RegIt
!= InstrMap
.end()) {
2811 const auto &RegVec
= RegIt
->getSecond();
2812 // For every Register in the RegVec, remove the MachineInstr in the
2813 // RegisterMap that describes the DbgVar.
2814 for (auto &Reg
: RegVec
) {
2815 auto RegIt
= RegisterMap
.find(Reg
);
2816 if (RegIt
== RegisterMap
.end())
2818 auto &InstrVec
= RegIt
->getSecond();
2819 auto IsDbgVar
= [&](MachineInstr
*I
) -> bool {
2820 auto Var
= createDebugVariableFromMachineInstr(I
);
2821 return Var
== DbgVar
;
2824 llvm::erase_if(InstrVec
, IsDbgVar
);
2826 forEachDbgRegOperand(Instr
,
2827 [&](MachineOperand
&Op
) { Op
.setReg(0); });
2830 DbgValueSinkCandidates
[DbgVar
] = &MI
;
2832 // If the first operand of a load matches with a DBG_VALUE in RegisterMap,
2833 // then move that DBG_VALUE to below the load.
2834 auto Opc
= MI
.getOpcode();
2835 if (!isLoadSingle(Opc
))
2837 auto Reg
= MI
.getOperand(0).getReg();
2838 auto RegIt
= RegisterMap
.find(Reg
);
2839 if (RegIt
== RegisterMap
.end())
2841 auto &DbgInstrVec
= RegIt
->getSecond();
2842 if (!DbgInstrVec
.size())
2844 for (auto *DbgInstr
: DbgInstrVec
) {
2845 MachineBasicBlock::iterator InsertPos
= std::next(MBBI
);
2846 auto *ClonedMI
= MI
.getMF()->CloneMachineInstr(DbgInstr
);
2847 MBB
->insert(InsertPos
, ClonedMI
);
2849 // Erase the entry into the DbgValueSinkCandidates for the DBG_VALUE
2851 auto DbgVar
= createDebugVariableFromMachineInstr(DbgInstr
);
2852 auto DbgIt
= DbgValueSinkCandidates
.find(DbgVar
);
2853 // If the instruction is a DBG_VALUE_LIST, it may have already been
2854 // erased from the DbgValueSinkCandidates. Only erase if it exists in
2855 // the DbgValueSinkCandidates.
2856 if (DbgIt
!= DbgValueSinkCandidates
.end())
2857 DbgValueSinkCandidates
.erase(DbgIt
);
2858 // Zero out original dbg instr
2859 forEachDbgRegOperand(DbgInstr
,
2860 [&](MachineOperand
&Op
) { Op
.setReg(0); });
2861 // Update RegisterMap with ClonedMI because it might have to be moved
2863 if (DbgInstr
->isDebugValueList())
2864 updateRegisterMapForDbgValueListAfterMove(RegisterMap
, ClonedMI
,
2872 // Get the Base register operand index from the memory access MachineInst if we
2873 // should attempt to distribute postinc on it. Return -1 if not of a valid
2874 // instruction type. If it returns an index, it is assumed that instruction is a
2875 // r+i indexing mode, and getBaseOperandIndex() + 1 is the Offset index.
2876 static int getBaseOperandIndex(MachineInstr
&MI
) {
2877 switch (MI
.getOpcode()) {
2878 case ARM::MVE_VLDRBS16
:
2879 case ARM::MVE_VLDRBS32
:
2880 case ARM::MVE_VLDRBU16
:
2881 case ARM::MVE_VLDRBU32
:
2882 case ARM::MVE_VLDRHS32
:
2883 case ARM::MVE_VLDRHU32
:
2884 case ARM::MVE_VLDRBU8
:
2885 case ARM::MVE_VLDRHU16
:
2886 case ARM::MVE_VLDRWU32
:
2887 case ARM::MVE_VSTRB16
:
2888 case ARM::MVE_VSTRB32
:
2889 case ARM::MVE_VSTRH32
:
2890 case ARM::MVE_VSTRBU8
:
2891 case ARM::MVE_VSTRHU16
:
2892 case ARM::MVE_VSTRWU32
:
2894 case ARM::t2LDRHi12
:
2895 case ARM::t2LDRSHi8
:
2896 case ARM::t2LDRSHi12
:
2898 case ARM::t2LDRBi12
:
2899 case ARM::t2LDRSBi8
:
2900 case ARM::t2LDRSBi12
:
2902 case ARM::t2STRBi12
:
2904 case ARM::t2STRHi12
:
2906 case ARM::MVE_VLDRBS16_post
:
2907 case ARM::MVE_VLDRBS32_post
:
2908 case ARM::MVE_VLDRBU16_post
:
2909 case ARM::MVE_VLDRBU32_post
:
2910 case ARM::MVE_VLDRHS32_post
:
2911 case ARM::MVE_VLDRHU32_post
:
2912 case ARM::MVE_VLDRBU8_post
:
2913 case ARM::MVE_VLDRHU16_post
:
2914 case ARM::MVE_VLDRWU32_post
:
2915 case ARM::MVE_VSTRB16_post
:
2916 case ARM::MVE_VSTRB32_post
:
2917 case ARM::MVE_VSTRH32_post
:
2918 case ARM::MVE_VSTRBU8_post
:
2919 case ARM::MVE_VSTRHU16_post
:
2920 case ARM::MVE_VSTRWU32_post
:
2921 case ARM::MVE_VLDRBS16_pre
:
2922 case ARM::MVE_VLDRBS32_pre
:
2923 case ARM::MVE_VLDRBU16_pre
:
2924 case ARM::MVE_VLDRBU32_pre
:
2925 case ARM::MVE_VLDRHS32_pre
:
2926 case ARM::MVE_VLDRHU32_pre
:
2927 case ARM::MVE_VLDRBU8_pre
:
2928 case ARM::MVE_VLDRHU16_pre
:
2929 case ARM::MVE_VLDRWU32_pre
:
2930 case ARM::MVE_VSTRB16_pre
:
2931 case ARM::MVE_VSTRB32_pre
:
2932 case ARM::MVE_VSTRH32_pre
:
2933 case ARM::MVE_VSTRBU8_pre
:
2934 case ARM::MVE_VSTRHU16_pre
:
2935 case ARM::MVE_VSTRWU32_pre
:
2941 static bool isPostIndex(MachineInstr
&MI
) {
2942 switch (MI
.getOpcode()) {
2943 case ARM::MVE_VLDRBS16_post
:
2944 case ARM::MVE_VLDRBS32_post
:
2945 case ARM::MVE_VLDRBU16_post
:
2946 case ARM::MVE_VLDRBU32_post
:
2947 case ARM::MVE_VLDRHS32_post
:
2948 case ARM::MVE_VLDRHU32_post
:
2949 case ARM::MVE_VLDRBU8_post
:
2950 case ARM::MVE_VLDRHU16_post
:
2951 case ARM::MVE_VLDRWU32_post
:
2952 case ARM::MVE_VSTRB16_post
:
2953 case ARM::MVE_VSTRB32_post
:
2954 case ARM::MVE_VSTRH32_post
:
2955 case ARM::MVE_VSTRBU8_post
:
2956 case ARM::MVE_VSTRHU16_post
:
2957 case ARM::MVE_VSTRWU32_post
:
2963 static bool isPreIndex(MachineInstr
&MI
) {
2964 switch (MI
.getOpcode()) {
2965 case ARM::MVE_VLDRBS16_pre
:
2966 case ARM::MVE_VLDRBS32_pre
:
2967 case ARM::MVE_VLDRBU16_pre
:
2968 case ARM::MVE_VLDRBU32_pre
:
2969 case ARM::MVE_VLDRHS32_pre
:
2970 case ARM::MVE_VLDRHU32_pre
:
2971 case ARM::MVE_VLDRBU8_pre
:
2972 case ARM::MVE_VLDRHU16_pre
:
2973 case ARM::MVE_VLDRWU32_pre
:
2974 case ARM::MVE_VSTRB16_pre
:
2975 case ARM::MVE_VSTRB32_pre
:
2976 case ARM::MVE_VSTRH32_pre
:
2977 case ARM::MVE_VSTRBU8_pre
:
2978 case ARM::MVE_VSTRHU16_pre
:
2979 case ARM::MVE_VSTRWU32_pre
:
2985 // Given a memory access Opcode, check that the give Imm would be a valid Offset
2986 // for this instruction (same as isLegalAddressImm), Or if the instruction
2987 // could be easily converted to one where that was valid. For example converting
2988 // t2LDRi12 to t2LDRi8 for negative offsets. Works in conjunction with
2989 // AdjustBaseAndOffset below.
2990 static bool isLegalOrConvertableAddressImm(unsigned Opcode
, int Imm
,
2991 const TargetInstrInfo
*TII
,
2992 int &CodesizeEstimate
) {
2993 if (isLegalAddressImm(Opcode
, Imm
, TII
))
2996 // We can convert AddrModeT2_i12 to AddrModeT2_i8neg.
2997 const MCInstrDesc
&Desc
= TII
->get(Opcode
);
2998 unsigned AddrMode
= (Desc
.TSFlags
& ARMII::AddrModeMask
);
3000 case ARMII::AddrModeT2_i12
:
3001 CodesizeEstimate
+= 1;
3002 return Imm
< 0 && -Imm
< ((1 << 8) * 1);
3007 // Given an MI adjust its address BaseReg to use NewBaseReg and address offset
3008 // by -Offset. This can either happen in-place or be a replacement as MI is
3009 // converted to another instruction type.
3010 static void AdjustBaseAndOffset(MachineInstr
*MI
, Register NewBaseReg
,
3011 int Offset
, const TargetInstrInfo
*TII
,
3012 const TargetRegisterInfo
*TRI
) {
3014 unsigned BaseOp
= getBaseOperandIndex(*MI
);
3015 MI
->getOperand(BaseOp
).setReg(NewBaseReg
);
3016 // and constrain the reg class to that required by the instruction.
3017 MachineFunction
*MF
= MI
->getMF();
3018 MachineRegisterInfo
&MRI
= MF
->getRegInfo();
3019 const MCInstrDesc
&MCID
= TII
->get(MI
->getOpcode());
3020 const TargetRegisterClass
*TRC
= TII
->getRegClass(MCID
, BaseOp
, TRI
, *MF
);
3021 MRI
.constrainRegClass(NewBaseReg
, TRC
);
3023 int OldOffset
= MI
->getOperand(BaseOp
+ 1).getImm();
3024 if (isLegalAddressImm(MI
->getOpcode(), OldOffset
- Offset
, TII
))
3025 MI
->getOperand(BaseOp
+ 1).setImm(OldOffset
- Offset
);
3027 unsigned ConvOpcode
;
3028 switch (MI
->getOpcode()) {
3029 case ARM::t2LDRHi12
:
3030 ConvOpcode
= ARM::t2LDRHi8
;
3032 case ARM::t2LDRSHi12
:
3033 ConvOpcode
= ARM::t2LDRSHi8
;
3035 case ARM::t2LDRBi12
:
3036 ConvOpcode
= ARM::t2LDRBi8
;
3038 case ARM::t2LDRSBi12
:
3039 ConvOpcode
= ARM::t2LDRSBi8
;
3041 case ARM::t2STRHi12
:
3042 ConvOpcode
= ARM::t2STRHi8
;
3044 case ARM::t2STRBi12
:
3045 ConvOpcode
= ARM::t2STRBi8
;
3048 llvm_unreachable("Unhandled convertable opcode");
3050 assert(isLegalAddressImm(ConvOpcode
, OldOffset
- Offset
, TII
) &&
3051 "Illegal Address Immediate after convert!");
3053 const MCInstrDesc
&MCID
= TII
->get(ConvOpcode
);
3054 BuildMI(*MI
->getParent(), MI
, MI
->getDebugLoc(), MCID
)
3055 .add(MI
->getOperand(0))
3056 .add(MI
->getOperand(1))
3057 .addImm(OldOffset
- Offset
)
3058 .add(MI
->getOperand(3))
3059 .add(MI
->getOperand(4))
3061 MI
->eraseFromParent();
3065 static MachineInstr
*createPostIncLoadStore(MachineInstr
*MI
, int Offset
,
3067 const TargetInstrInfo
*TII
,
3068 const TargetRegisterInfo
*TRI
) {
3069 MachineFunction
*MF
= MI
->getMF();
3070 MachineRegisterInfo
&MRI
= MF
->getRegInfo();
3072 unsigned NewOpcode
= getPostIndexedLoadStoreOpcode(
3073 MI
->getOpcode(), Offset
> 0 ? ARM_AM::add
: ARM_AM::sub
);
3075 const MCInstrDesc
&MCID
= TII
->get(NewOpcode
);
3076 // Constrain the def register class
3077 const TargetRegisterClass
*TRC
= TII
->getRegClass(MCID
, 0, TRI
, *MF
);
3078 MRI
.constrainRegClass(NewReg
, TRC
);
3079 // And do the same for the base operand
3080 TRC
= TII
->getRegClass(MCID
, 2, TRI
, *MF
);
3081 MRI
.constrainRegClass(MI
->getOperand(1).getReg(), TRC
);
3083 unsigned AddrMode
= (MCID
.TSFlags
& ARMII::AddrModeMask
);
3085 case ARMII::AddrModeT2_i7
:
3086 case ARMII::AddrModeT2_i7s2
:
3087 case ARMII::AddrModeT2_i7s4
:
3088 // Any MVE load/store
3089 return BuildMI(*MI
->getParent(), MI
, MI
->getDebugLoc(), MCID
)
3090 .addReg(NewReg
, RegState::Define
)
3091 .add(MI
->getOperand(0))
3092 .add(MI
->getOperand(1))
3094 .add(MI
->getOperand(3))
3095 .add(MI
->getOperand(4))
3096 .add(MI
->getOperand(5))
3098 case ARMII::AddrModeT2_i8
:
3099 if (MI
->mayLoad()) {
3100 return BuildMI(*MI
->getParent(), MI
, MI
->getDebugLoc(), MCID
)
3101 .add(MI
->getOperand(0))
3102 .addReg(NewReg
, RegState::Define
)
3103 .add(MI
->getOperand(1))
3105 .add(MI
->getOperand(3))
3106 .add(MI
->getOperand(4))
3109 return BuildMI(*MI
->getParent(), MI
, MI
->getDebugLoc(), MCID
)
3110 .addReg(NewReg
, RegState::Define
)
3111 .add(MI
->getOperand(0))
3112 .add(MI
->getOperand(1))
3114 .add(MI
->getOperand(3))
3115 .add(MI
->getOperand(4))
3119 llvm_unreachable("Unhandled createPostIncLoadStore");
3123 // Given a Base Register, optimise the load/store uses to attempt to create more
3124 // post-inc accesses and less register moves. We do this by taking zero offset
3125 // loads/stores with an add, and convert them to a postinc load/store of the
3126 // same type. Any subsequent accesses will be adjusted to use and account for
3127 // the post-inc value.
3129 // LDR #0 LDR_POSTINC #16
3135 // At the same time if we do not find an increment but do find an existing
3136 // pre/post inc instruction, we can still adjust the offsets of subsequent
3137 // instructions to save the register move that would otherwise be needed for the
3138 // in-place increment.
3139 bool ARMPreAllocLoadStoreOpt::DistributeIncrements(Register Base
) {
3140 // We are looking for:
3141 // One zero offset load/store that can become postinc
3142 MachineInstr
*BaseAccess
= nullptr;
3143 MachineInstr
*PrePostInc
= nullptr;
3144 // An increment that can be folded in
3145 MachineInstr
*Increment
= nullptr;
3146 // Other accesses after BaseAccess that will need to be updated to use the
3148 SmallPtrSet
<MachineInstr
*, 8> OtherAccesses
;
3149 for (auto &Use
: MRI
->use_nodbg_instructions(Base
)) {
3150 if (!Increment
&& getAddSubImmediate(Use
) != 0) {
3155 int BaseOp
= getBaseOperandIndex(Use
);
3159 if (!Use
.getOperand(BaseOp
).isReg() ||
3160 Use
.getOperand(BaseOp
).getReg() != Base
)
3162 if (isPreIndex(Use
) || isPostIndex(Use
))
3164 else if (Use
.getOperand(BaseOp
+ 1).getImm() == 0)
3167 OtherAccesses
.insert(&Use
);
3170 int IncrementOffset
;
3171 Register NewBaseReg
;
3172 if (BaseAccess
&& Increment
) {
3173 if (PrePostInc
|| BaseAccess
->getParent() != Increment
->getParent())
3176 if (Increment
->definesRegister(ARM::CPSR
, /*TRI=*/nullptr) ||
3177 getInstrPredicate(*Increment
, PredReg
) != ARMCC::AL
)
3180 LLVM_DEBUG(dbgs() << "\nAttempting to distribute increments on VirtualReg "
3181 << Base
.virtRegIndex() << "\n");
3183 // Make sure that Increment has no uses before BaseAccess that are not PHI
3185 for (MachineInstr
&Use
:
3186 MRI
->use_nodbg_instructions(Increment
->getOperand(0).getReg())) {
3187 if (&Use
== BaseAccess
|| (Use
.getOpcode() != TargetOpcode::PHI
&&
3188 !DT
->dominates(BaseAccess
, &Use
))) {
3189 LLVM_DEBUG(dbgs() << " BaseAccess doesn't dominate use of increment\n");
3194 // Make sure that Increment can be folded into Base
3195 IncrementOffset
= getAddSubImmediate(*Increment
);
3196 unsigned NewPostIncOpcode
= getPostIndexedLoadStoreOpcode(
3197 BaseAccess
->getOpcode(), IncrementOffset
> 0 ? ARM_AM::add
: ARM_AM::sub
);
3198 if (!isLegalAddressImm(NewPostIncOpcode
, IncrementOffset
, TII
)) {
3199 LLVM_DEBUG(dbgs() << " Illegal addressing mode immediate on postinc\n");
3203 else if (PrePostInc
) {
3204 // If we already have a pre/post index load/store then set BaseAccess,
3205 // IncrementOffset and NewBaseReg to the values it already produces,
3206 // allowing us to update and subsequent uses of BaseOp reg with the
3207 // incremented value.
3211 LLVM_DEBUG(dbgs() << "\nAttempting to distribute increments on already "
3212 << "indexed VirtualReg " << Base
.virtRegIndex() << "\n");
3213 int BaseOp
= getBaseOperandIndex(*PrePostInc
);
3214 IncrementOffset
= PrePostInc
->getOperand(BaseOp
+1).getImm();
3215 BaseAccess
= PrePostInc
;
3216 NewBaseReg
= PrePostInc
->getOperand(0).getReg();
3221 // And make sure that the negative value of increment can be added to all
3222 // other offsets after the BaseAccess. We rely on either
3223 // dominates(BaseAccess, OtherAccess) or dominates(OtherAccess, BaseAccess)
3224 // to keep things simple.
3225 // This also adds a simple codesize metric, to detect if an instruction (like
3226 // t2LDRBi12) which can often be shrunk to a thumb1 instruction (tLDRBi)
3227 // cannot because it is converted to something else (t2LDRBi8). We start this
3228 // at -1 for the gain from removing the increment.
3229 SmallPtrSet
<MachineInstr
*, 4> SuccessorAccesses
;
3230 int CodesizeEstimate
= -1;
3231 for (auto *Use
: OtherAccesses
) {
3232 if (DT
->dominates(BaseAccess
, Use
)) {
3233 SuccessorAccesses
.insert(Use
);
3234 unsigned BaseOp
= getBaseOperandIndex(*Use
);
3235 if (!isLegalOrConvertableAddressImm(Use
->getOpcode(),
3236 Use
->getOperand(BaseOp
+ 1).getImm() -
3238 TII
, CodesizeEstimate
)) {
3239 LLVM_DEBUG(dbgs() << " Illegal addressing mode immediate on use\n");
3242 } else if (!DT
->dominates(Use
, BaseAccess
)) {
3244 dbgs() << " Unknown dominance relation between Base and Use\n");
3248 if (STI
->hasMinSize() && CodesizeEstimate
> 0) {
3249 LLVM_DEBUG(dbgs() << " Expected to grow instructions under minsize\n");
3254 // Replace BaseAccess with a post inc
3255 LLVM_DEBUG(dbgs() << "Changing: "; BaseAccess
->dump());
3256 LLVM_DEBUG(dbgs() << " And : "; Increment
->dump());
3257 NewBaseReg
= Increment
->getOperand(0).getReg();
3258 MachineInstr
*BaseAccessPost
=
3259 createPostIncLoadStore(BaseAccess
, IncrementOffset
, NewBaseReg
, TII
, TRI
);
3260 BaseAccess
->eraseFromParent();
3261 Increment
->eraseFromParent();
3262 (void)BaseAccessPost
;
3263 LLVM_DEBUG(dbgs() << " To : "; BaseAccessPost
->dump());
3266 for (auto *Use
: SuccessorAccesses
) {
3267 LLVM_DEBUG(dbgs() << "Changing: "; Use
->dump());
3268 AdjustBaseAndOffset(Use
, NewBaseReg
, IncrementOffset
, TII
, TRI
);
3269 LLVM_DEBUG(dbgs() << " To : "; Use
->dump());
3272 // Remove the kill flag from all uses of NewBaseReg, in case any old uses
3274 for (MachineOperand
&Op
: MRI
->use_nodbg_operands(NewBaseReg
))
3275 Op
.setIsKill(false);
3279 bool ARMPreAllocLoadStoreOpt::DistributeIncrements() {
3280 bool Changed
= false;
3281 SmallSetVector
<Register
, 4> Visited
;
3282 for (auto &MBB
: *MF
) {
3283 for (auto &MI
: MBB
) {
3284 int BaseOp
= getBaseOperandIndex(MI
);
3285 if (BaseOp
== -1 || !MI
.getOperand(BaseOp
).isReg())
3288 Register Base
= MI
.getOperand(BaseOp
).getReg();
3289 if (!Base
.isVirtual())
3292 Visited
.insert(Base
);
3296 for (auto Base
: Visited
)
3297 Changed
|= DistributeIncrements(Base
);
3302 /// Returns an instance of the load / store optimization pass.
3303 FunctionPass
*llvm::createARMLoadStoreOptimizationPass(bool PreAlloc
) {
3305 return new ARMPreAllocLoadStoreOpt();
3306 return new ARMLoadStoreOpt();