10b57cec5SDimitry Andric //===- ARMLoadStoreOptimizer.cpp - ARM load / store opt. pass -------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric /// \file This file contains a pass that performs load / store related peephole
100b57cec5SDimitry Andric /// optimizations. This pass should be run after register allocation.
110b57cec5SDimitry Andric //
120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
130b57cec5SDimitry Andric
140b57cec5SDimitry Andric #include "ARM.h"
150b57cec5SDimitry Andric #include "ARMBaseInstrInfo.h"
160b57cec5SDimitry Andric #include "ARMBaseRegisterInfo.h"
170b57cec5SDimitry Andric #include "ARMISelLowering.h"
180b57cec5SDimitry Andric #include "ARMMachineFunctionInfo.h"
190b57cec5SDimitry Andric #include "ARMSubtarget.h"
200b57cec5SDimitry Andric #include "MCTargetDesc/ARMAddressingModes.h"
210b57cec5SDimitry Andric #include "MCTargetDesc/ARMBaseInfo.h"
220b57cec5SDimitry Andric #include "Utils/ARMBaseInfo.h"
230b57cec5SDimitry Andric #include "llvm/ADT/ArrayRef.h"
240b57cec5SDimitry Andric #include "llvm/ADT/DenseMap.h"
250b57cec5SDimitry Andric #include "llvm/ADT/DenseSet.h"
260b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h"
2781ad6265SDimitry Andric #include "llvm/ADT/SetVector.h"
280b57cec5SDimitry Andric #include "llvm/ADT/SmallPtrSet.h"
290b57cec5SDimitry Andric #include "llvm/ADT/SmallSet.h"
300b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h"
310b57cec5SDimitry Andric #include "llvm/ADT/Statistic.h"
320b57cec5SDimitry Andric #include "llvm/ADT/iterator_range.h"
330b57cec5SDimitry Andric #include "llvm/Analysis/AliasAnalysis.h"
340b57cec5SDimitry Andric #include "llvm/CodeGen/LivePhysRegs.h"
350b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h"
365ffd83dbSDimitry Andric #include "llvm/CodeGen/MachineDominators.h"
3781ad6265SDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h"
380b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
390b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h"
400b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstr.h"
410b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h"
420b57cec5SDimitry Andric #include "llvm/CodeGen/MachineMemOperand.h"
430b57cec5SDimitry Andric #include "llvm/CodeGen/MachineOperand.h"
440b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
450b57cec5SDimitry Andric #include "llvm/CodeGen/RegisterClassInfo.h"
460b57cec5SDimitry Andric #include "llvm/CodeGen/TargetFrameLowering.h"
470b57cec5SDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h"
480b57cec5SDimitry Andric #include "llvm/CodeGen/TargetLowering.h"
490b57cec5SDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h"
500b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h"
510b57cec5SDimitry Andric #include "llvm/IR/DataLayout.h"
520b57cec5SDimitry Andric #include "llvm/IR/DebugLoc.h"
530b57cec5SDimitry Andric #include "llvm/IR/DerivedTypes.h"
540b57cec5SDimitry Andric #include "llvm/IR/Function.h"
550b57cec5SDimitry Andric #include "llvm/IR/Type.h"
565ffd83dbSDimitry Andric #include "llvm/InitializePasses.h"
570b57cec5SDimitry Andric #include "llvm/MC/MCInstrDesc.h"
580b57cec5SDimitry Andric #include "llvm/Pass.h"
590b57cec5SDimitry Andric #include "llvm/Support/Allocator.h"
600b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h"
610b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
620b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
630b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
640b57cec5SDimitry Andric #include <algorithm>
650b57cec5SDimitry Andric #include <cassert>
660b57cec5SDimitry Andric #include <cstddef>
670b57cec5SDimitry Andric #include <cstdlib>
680b57cec5SDimitry Andric #include <iterator>
690b57cec5SDimitry Andric #include <limits>
700b57cec5SDimitry Andric #include <utility>
710b57cec5SDimitry Andric
720b57cec5SDimitry Andric using namespace llvm;
730b57cec5SDimitry Andric
740b57cec5SDimitry Andric #define DEBUG_TYPE "arm-ldst-opt"
750b57cec5SDimitry Andric
760b57cec5SDimitry Andric STATISTIC(NumLDMGened , "Number of ldm instructions generated");
770b57cec5SDimitry Andric STATISTIC(NumSTMGened , "Number of stm instructions generated");
780b57cec5SDimitry Andric STATISTIC(NumVLDMGened, "Number of vldm instructions generated");
790b57cec5SDimitry Andric STATISTIC(NumVSTMGened, "Number of vstm instructions generated");
800b57cec5SDimitry Andric STATISTIC(NumLdStMoved, "Number of load / store instructions moved");
810b57cec5SDimitry Andric STATISTIC(NumLDRDFormed,"Number of ldrd created before allocation");
820b57cec5SDimitry Andric STATISTIC(NumSTRDFormed,"Number of strd created before allocation");
830b57cec5SDimitry Andric STATISTIC(NumLDRD2LDM, "Number of ldrd instructions turned back into ldm");
840b57cec5SDimitry Andric STATISTIC(NumSTRD2STM, "Number of strd instructions turned back into stm");
850b57cec5SDimitry Andric STATISTIC(NumLDRD2LDR, "Number of ldrd instructions turned back into ldr's");
860b57cec5SDimitry Andric STATISTIC(NumSTRD2STR, "Number of strd instructions turned back into str's");
870b57cec5SDimitry Andric
880b57cec5SDimitry Andric /// This switch disables formation of double/multi instructions that could
890b57cec5SDimitry Andric /// potentially lead to (new) alignment traps even with CCR.UNALIGN_TRP
900b57cec5SDimitry Andric /// disabled. This can be used to create libraries that are robust even when
910b57cec5SDimitry Andric /// users provoke undefined behaviour by supplying misaligned pointers.
920b57cec5SDimitry Andric /// \see mayCombineMisaligned()
930b57cec5SDimitry Andric static cl::opt<bool>
940b57cec5SDimitry Andric AssumeMisalignedLoadStores("arm-assume-misaligned-load-store", cl::Hidden,
950b57cec5SDimitry Andric cl::init(false), cl::desc("Be more conservative in ARM load/store opt"));
960b57cec5SDimitry Andric
970b57cec5SDimitry Andric #define ARM_LOAD_STORE_OPT_NAME "ARM load / store optimization pass"
980b57cec5SDimitry Andric
990b57cec5SDimitry Andric namespace {
1000b57cec5SDimitry Andric
1010b57cec5SDimitry Andric /// Post- register allocation pass the combine load / store instructions to
1020b57cec5SDimitry Andric /// form ldm / stm instructions.
1030b57cec5SDimitry Andric struct ARMLoadStoreOpt : public MachineFunctionPass {
1040b57cec5SDimitry Andric static char ID;
1050b57cec5SDimitry Andric
1060b57cec5SDimitry Andric const MachineFunction *MF;
1070b57cec5SDimitry Andric const TargetInstrInfo *TII;
1080b57cec5SDimitry Andric const TargetRegisterInfo *TRI;
1090b57cec5SDimitry Andric const ARMSubtarget *STI;
1100b57cec5SDimitry Andric const TargetLowering *TL;
1110b57cec5SDimitry Andric ARMFunctionInfo *AFI;
1120b57cec5SDimitry Andric LivePhysRegs LiveRegs;
1130b57cec5SDimitry Andric RegisterClassInfo RegClassInfo;
1140b57cec5SDimitry Andric MachineBasicBlock::const_iterator LiveRegPos;
1150b57cec5SDimitry Andric bool LiveRegsValid;
1160b57cec5SDimitry Andric bool RegClassInfoValid;
1170b57cec5SDimitry Andric bool isThumb1, isThumb2;
1180b57cec5SDimitry Andric
ARMLoadStoreOpt__anon8bf7a5bb0111::ARMLoadStoreOpt1190b57cec5SDimitry Andric ARMLoadStoreOpt() : MachineFunctionPass(ID) {}
1200b57cec5SDimitry Andric
1210b57cec5SDimitry Andric bool runOnMachineFunction(MachineFunction &Fn) override;
1220b57cec5SDimitry Andric
getRequiredProperties__anon8bf7a5bb0111::ARMLoadStoreOpt1230b57cec5SDimitry Andric MachineFunctionProperties getRequiredProperties() const override {
1240b57cec5SDimitry Andric return MachineFunctionProperties().set(
1250b57cec5SDimitry Andric MachineFunctionProperties::Property::NoVRegs);
1260b57cec5SDimitry Andric }
1270b57cec5SDimitry Andric
getPassName__anon8bf7a5bb0111::ARMLoadStoreOpt1280b57cec5SDimitry Andric StringRef getPassName() const override { return ARM_LOAD_STORE_OPT_NAME; }
1290b57cec5SDimitry Andric
1300b57cec5SDimitry Andric private:
1310b57cec5SDimitry Andric /// A set of load/store MachineInstrs with same base register sorted by
1320b57cec5SDimitry Andric /// offset.
1330b57cec5SDimitry Andric struct MemOpQueueEntry {
1340b57cec5SDimitry Andric MachineInstr *MI;
1350b57cec5SDimitry Andric int Offset; ///< Load/Store offset.
1360b57cec5SDimitry Andric unsigned Position; ///< Position as counted from end of basic block.
1370b57cec5SDimitry Andric
MemOpQueueEntry__anon8bf7a5bb0111::ARMLoadStoreOpt::MemOpQueueEntry1380b57cec5SDimitry Andric MemOpQueueEntry(MachineInstr &MI, int Offset, unsigned Position)
1390b57cec5SDimitry Andric : MI(&MI), Offset(Offset), Position(Position) {}
1400b57cec5SDimitry Andric };
1410b57cec5SDimitry Andric using MemOpQueue = SmallVector<MemOpQueueEntry, 8>;
1420b57cec5SDimitry Andric
1430b57cec5SDimitry Andric /// A set of MachineInstrs that fulfill (nearly all) conditions to get
1440b57cec5SDimitry Andric /// merged into a LDM/STM.
1450b57cec5SDimitry Andric struct MergeCandidate {
1460b57cec5SDimitry Andric /// List of instructions ordered by load/store offset.
1470b57cec5SDimitry Andric SmallVector<MachineInstr*, 4> Instrs;
1480b57cec5SDimitry Andric
1490b57cec5SDimitry Andric /// Index in Instrs of the instruction being latest in the schedule.
1500b57cec5SDimitry Andric unsigned LatestMIIdx;
1510b57cec5SDimitry Andric
1520b57cec5SDimitry Andric /// Index in Instrs of the instruction being earliest in the schedule.
1530b57cec5SDimitry Andric unsigned EarliestMIIdx;
1540b57cec5SDimitry Andric
1550b57cec5SDimitry Andric /// Index into the basic block where the merged instruction will be
1560b57cec5SDimitry Andric /// inserted. (See MemOpQueueEntry.Position)
1570b57cec5SDimitry Andric unsigned InsertPos;
1580b57cec5SDimitry Andric
1590b57cec5SDimitry Andric /// Whether the instructions can be merged into a ldm/stm instruction.
1600b57cec5SDimitry Andric bool CanMergeToLSMulti;
1610b57cec5SDimitry Andric
1620b57cec5SDimitry Andric /// Whether the instructions can be merged into a ldrd/strd instruction.
1630b57cec5SDimitry Andric bool CanMergeToLSDouble;
1640b57cec5SDimitry Andric };
1650b57cec5SDimitry Andric SpecificBumpPtrAllocator<MergeCandidate> Allocator;
1660b57cec5SDimitry Andric SmallVector<const MergeCandidate*,4> Candidates;
1670b57cec5SDimitry Andric SmallVector<MachineInstr*,4> MergeBaseCandidates;
1680b57cec5SDimitry Andric
1690b57cec5SDimitry Andric void moveLiveRegsBefore(const MachineBasicBlock &MBB,
1700b57cec5SDimitry Andric MachineBasicBlock::const_iterator Before);
1710b57cec5SDimitry Andric unsigned findFreeReg(const TargetRegisterClass &RegClass);
1720b57cec5SDimitry Andric void UpdateBaseRegUses(MachineBasicBlock &MBB,
1730b57cec5SDimitry Andric MachineBasicBlock::iterator MBBI, const DebugLoc &DL,
1740b57cec5SDimitry Andric unsigned Base, unsigned WordOffset,
1750b57cec5SDimitry Andric ARMCC::CondCodes Pred, unsigned PredReg);
1760b57cec5SDimitry Andric MachineInstr *CreateLoadStoreMulti(
1770b57cec5SDimitry Andric MachineBasicBlock &MBB, MachineBasicBlock::iterator InsertBefore,
1780b57cec5SDimitry Andric int Offset, unsigned Base, bool BaseKill, unsigned Opcode,
1790b57cec5SDimitry Andric ARMCC::CondCodes Pred, unsigned PredReg, const DebugLoc &DL,
1800b57cec5SDimitry Andric ArrayRef<std::pair<unsigned, bool>> Regs,
1810b57cec5SDimitry Andric ArrayRef<MachineInstr*> Instrs);
1820b57cec5SDimitry Andric MachineInstr *CreateLoadStoreDouble(
1830b57cec5SDimitry Andric MachineBasicBlock &MBB, MachineBasicBlock::iterator InsertBefore,
1840b57cec5SDimitry Andric int Offset, unsigned Base, bool BaseKill, unsigned Opcode,
1850b57cec5SDimitry Andric ARMCC::CondCodes Pred, unsigned PredReg, const DebugLoc &DL,
1860b57cec5SDimitry Andric ArrayRef<std::pair<unsigned, bool>> Regs,
1870b57cec5SDimitry Andric ArrayRef<MachineInstr*> Instrs) const;
1880b57cec5SDimitry Andric void FormCandidates(const MemOpQueue &MemOps);
1890b57cec5SDimitry Andric MachineInstr *MergeOpsUpdate(const MergeCandidate &Cand);
1900b57cec5SDimitry Andric bool FixInvalidRegPairOp(MachineBasicBlock &MBB,
1910b57cec5SDimitry Andric MachineBasicBlock::iterator &MBBI);
1920b57cec5SDimitry Andric bool MergeBaseUpdateLoadStore(MachineInstr *MI);
1930b57cec5SDimitry Andric bool MergeBaseUpdateLSMultiple(MachineInstr *MI);
1940b57cec5SDimitry Andric bool MergeBaseUpdateLSDouble(MachineInstr &MI) const;
1950b57cec5SDimitry Andric bool LoadStoreMultipleOpti(MachineBasicBlock &MBB);
1960b57cec5SDimitry Andric bool MergeReturnIntoLDM(MachineBasicBlock &MBB);
1970b57cec5SDimitry Andric bool CombineMovBx(MachineBasicBlock &MBB);
1980b57cec5SDimitry Andric };
1990b57cec5SDimitry Andric
2000b57cec5SDimitry Andric } // end anonymous namespace
2010b57cec5SDimitry Andric
2020b57cec5SDimitry Andric char ARMLoadStoreOpt::ID = 0;
2030b57cec5SDimitry Andric
2040b57cec5SDimitry Andric INITIALIZE_PASS(ARMLoadStoreOpt, "arm-ldst-opt", ARM_LOAD_STORE_OPT_NAME, false,
2050b57cec5SDimitry Andric false)
2060b57cec5SDimitry Andric
definesCPSR(const MachineInstr & MI)2070b57cec5SDimitry Andric static bool definesCPSR(const MachineInstr &MI) {
2080b57cec5SDimitry Andric for (const auto &MO : MI.operands()) {
2090b57cec5SDimitry Andric if (!MO.isReg())
2100b57cec5SDimitry Andric continue;
2110b57cec5SDimitry Andric if (MO.isDef() && MO.getReg() == ARM::CPSR && !MO.isDead())
2120b57cec5SDimitry Andric // If the instruction has live CPSR def, then it's not safe to fold it
2130b57cec5SDimitry Andric // into load / store.
2140b57cec5SDimitry Andric return true;
2150b57cec5SDimitry Andric }
2160b57cec5SDimitry Andric
2170b57cec5SDimitry Andric return false;
2180b57cec5SDimitry Andric }
2190b57cec5SDimitry Andric
getMemoryOpOffset(const MachineInstr & MI)2200b57cec5SDimitry Andric static int getMemoryOpOffset(const MachineInstr &MI) {
2210b57cec5SDimitry Andric unsigned Opcode = MI.getOpcode();
2220b57cec5SDimitry Andric bool isAM3 = Opcode == ARM::LDRD || Opcode == ARM::STRD;
2230b57cec5SDimitry Andric unsigned NumOperands = MI.getDesc().getNumOperands();
2240b57cec5SDimitry Andric unsigned OffField = MI.getOperand(NumOperands - 3).getImm();
2250b57cec5SDimitry Andric
2260b57cec5SDimitry Andric if (Opcode == ARM::t2LDRi12 || Opcode == ARM::t2LDRi8 ||
2270b57cec5SDimitry Andric Opcode == ARM::t2STRi12 || Opcode == ARM::t2STRi8 ||
2280b57cec5SDimitry Andric Opcode == ARM::t2LDRDi8 || Opcode == ARM::t2STRDi8 ||
2290b57cec5SDimitry Andric Opcode == ARM::LDRi12 || Opcode == ARM::STRi12)
2300b57cec5SDimitry Andric return OffField;
2310b57cec5SDimitry Andric
2320b57cec5SDimitry Andric // Thumb1 immediate offsets are scaled by 4
2330b57cec5SDimitry Andric if (Opcode == ARM::tLDRi || Opcode == ARM::tSTRi ||
2340b57cec5SDimitry Andric Opcode == ARM::tLDRspi || Opcode == ARM::tSTRspi)
2350b57cec5SDimitry Andric return OffField * 4;
2360b57cec5SDimitry Andric
2370b57cec5SDimitry Andric int Offset = isAM3 ? ARM_AM::getAM3Offset(OffField)
2380b57cec5SDimitry Andric : ARM_AM::getAM5Offset(OffField) * 4;
2390b57cec5SDimitry Andric ARM_AM::AddrOpc Op = isAM3 ? ARM_AM::getAM3Op(OffField)
2400b57cec5SDimitry Andric : ARM_AM::getAM5Op(OffField);
2410b57cec5SDimitry Andric
2420b57cec5SDimitry Andric if (Op == ARM_AM::sub)
2430b57cec5SDimitry Andric return -Offset;
2440b57cec5SDimitry Andric
2450b57cec5SDimitry Andric return Offset;
2460b57cec5SDimitry Andric }
2470b57cec5SDimitry Andric
getLoadStoreBaseOp(const MachineInstr & MI)2480b57cec5SDimitry Andric static const MachineOperand &getLoadStoreBaseOp(const MachineInstr &MI) {
2490b57cec5SDimitry Andric return MI.getOperand(1);
2500b57cec5SDimitry Andric }
2510b57cec5SDimitry Andric
getLoadStoreRegOp(const MachineInstr & MI)2520b57cec5SDimitry Andric static const MachineOperand &getLoadStoreRegOp(const MachineInstr &MI) {
2530b57cec5SDimitry Andric return MI.getOperand(0);
2540b57cec5SDimitry Andric }
2550b57cec5SDimitry Andric
getLoadStoreMultipleOpcode(unsigned Opcode,ARM_AM::AMSubMode Mode)2560b57cec5SDimitry Andric static int getLoadStoreMultipleOpcode(unsigned Opcode, ARM_AM::AMSubMode Mode) {
2570b57cec5SDimitry Andric switch (Opcode) {
2580b57cec5SDimitry Andric default: llvm_unreachable("Unhandled opcode!");
2590b57cec5SDimitry Andric case ARM::LDRi12:
2600b57cec5SDimitry Andric ++NumLDMGened;
2610b57cec5SDimitry Andric switch (Mode) {
2620b57cec5SDimitry Andric default: llvm_unreachable("Unhandled submode!");
2630b57cec5SDimitry Andric case ARM_AM::ia: return ARM::LDMIA;
2640b57cec5SDimitry Andric case ARM_AM::da: return ARM::LDMDA;
2650b57cec5SDimitry Andric case ARM_AM::db: return ARM::LDMDB;
2660b57cec5SDimitry Andric case ARM_AM::ib: return ARM::LDMIB;
2670b57cec5SDimitry Andric }
2680b57cec5SDimitry Andric case ARM::STRi12:
2690b57cec5SDimitry Andric ++NumSTMGened;
2700b57cec5SDimitry Andric switch (Mode) {
2710b57cec5SDimitry Andric default: llvm_unreachable("Unhandled submode!");
2720b57cec5SDimitry Andric case ARM_AM::ia: return ARM::STMIA;
2730b57cec5SDimitry Andric case ARM_AM::da: return ARM::STMDA;
2740b57cec5SDimitry Andric case ARM_AM::db: return ARM::STMDB;
2750b57cec5SDimitry Andric case ARM_AM::ib: return ARM::STMIB;
2760b57cec5SDimitry Andric }
2770b57cec5SDimitry Andric case ARM::tLDRi:
2780b57cec5SDimitry Andric case ARM::tLDRspi:
2790b57cec5SDimitry Andric // tLDMIA is writeback-only - unless the base register is in the input
2800b57cec5SDimitry Andric // reglist.
2810b57cec5SDimitry Andric ++NumLDMGened;
2820b57cec5SDimitry Andric switch (Mode) {
2830b57cec5SDimitry Andric default: llvm_unreachable("Unhandled submode!");
2840b57cec5SDimitry Andric case ARM_AM::ia: return ARM::tLDMIA;
2850b57cec5SDimitry Andric }
2860b57cec5SDimitry Andric case ARM::tSTRi:
2870b57cec5SDimitry Andric case ARM::tSTRspi:
2880b57cec5SDimitry Andric // There is no non-writeback tSTMIA either.
2890b57cec5SDimitry Andric ++NumSTMGened;
2900b57cec5SDimitry Andric switch (Mode) {
2910b57cec5SDimitry Andric default: llvm_unreachable("Unhandled submode!");
2920b57cec5SDimitry Andric case ARM_AM::ia: return ARM::tSTMIA_UPD;
2930b57cec5SDimitry Andric }
2940b57cec5SDimitry Andric case ARM::t2LDRi8:
2950b57cec5SDimitry Andric case ARM::t2LDRi12:
2960b57cec5SDimitry Andric ++NumLDMGened;
2970b57cec5SDimitry Andric switch (Mode) {
2980b57cec5SDimitry Andric default: llvm_unreachable("Unhandled submode!");
2990b57cec5SDimitry Andric case ARM_AM::ia: return ARM::t2LDMIA;
3000b57cec5SDimitry Andric case ARM_AM::db: return ARM::t2LDMDB;
3010b57cec5SDimitry Andric }
3020b57cec5SDimitry Andric case ARM::t2STRi8:
3030b57cec5SDimitry Andric case ARM::t2STRi12:
3040b57cec5SDimitry Andric ++NumSTMGened;
3050b57cec5SDimitry Andric switch (Mode) {
3060b57cec5SDimitry Andric default: llvm_unreachable("Unhandled submode!");
3070b57cec5SDimitry Andric case ARM_AM::ia: return ARM::t2STMIA;
3080b57cec5SDimitry Andric case ARM_AM::db: return ARM::t2STMDB;
3090b57cec5SDimitry Andric }
3100b57cec5SDimitry Andric case ARM::VLDRS:
3110b57cec5SDimitry Andric ++NumVLDMGened;
3120b57cec5SDimitry Andric switch (Mode) {
3130b57cec5SDimitry Andric default: llvm_unreachable("Unhandled submode!");
3140b57cec5SDimitry Andric case ARM_AM::ia: return ARM::VLDMSIA;
3150b57cec5SDimitry Andric case ARM_AM::db: return 0; // Only VLDMSDB_UPD exists.
3160b57cec5SDimitry Andric }
3170b57cec5SDimitry Andric case ARM::VSTRS:
3180b57cec5SDimitry Andric ++NumVSTMGened;
3190b57cec5SDimitry Andric switch (Mode) {
3200b57cec5SDimitry Andric default: llvm_unreachable("Unhandled submode!");
3210b57cec5SDimitry Andric case ARM_AM::ia: return ARM::VSTMSIA;
3220b57cec5SDimitry Andric case ARM_AM::db: return 0; // Only VSTMSDB_UPD exists.
3230b57cec5SDimitry Andric }
3240b57cec5SDimitry Andric case ARM::VLDRD:
3250b57cec5SDimitry Andric ++NumVLDMGened;
3260b57cec5SDimitry Andric switch (Mode) {
3270b57cec5SDimitry Andric default: llvm_unreachable("Unhandled submode!");
3280b57cec5SDimitry Andric case ARM_AM::ia: return ARM::VLDMDIA;
3290b57cec5SDimitry Andric case ARM_AM::db: return 0; // Only VLDMDDB_UPD exists.
3300b57cec5SDimitry Andric }
3310b57cec5SDimitry Andric case ARM::VSTRD:
3320b57cec5SDimitry Andric ++NumVSTMGened;
3330b57cec5SDimitry Andric switch (Mode) {
3340b57cec5SDimitry Andric default: llvm_unreachable("Unhandled submode!");
3350b57cec5SDimitry Andric case ARM_AM::ia: return ARM::VSTMDIA;
3360b57cec5SDimitry Andric case ARM_AM::db: return 0; // Only VSTMDDB_UPD exists.
3370b57cec5SDimitry Andric }
3380b57cec5SDimitry Andric }
3390b57cec5SDimitry Andric }
3400b57cec5SDimitry Andric
getLoadStoreMultipleSubMode(unsigned Opcode)3410b57cec5SDimitry Andric static ARM_AM::AMSubMode getLoadStoreMultipleSubMode(unsigned Opcode) {
3420b57cec5SDimitry Andric switch (Opcode) {
3430b57cec5SDimitry Andric default: llvm_unreachable("Unhandled opcode!");
3440b57cec5SDimitry Andric case ARM::LDMIA_RET:
3450b57cec5SDimitry Andric case ARM::LDMIA:
3460b57cec5SDimitry Andric case ARM::LDMIA_UPD:
3470b57cec5SDimitry Andric case ARM::STMIA:
3480b57cec5SDimitry Andric case ARM::STMIA_UPD:
3490b57cec5SDimitry Andric case ARM::tLDMIA:
3500b57cec5SDimitry Andric case ARM::tLDMIA_UPD:
3510b57cec5SDimitry Andric case ARM::tSTMIA_UPD:
3520b57cec5SDimitry Andric case ARM::t2LDMIA_RET:
3530b57cec5SDimitry Andric case ARM::t2LDMIA:
3540b57cec5SDimitry Andric case ARM::t2LDMIA_UPD:
3550b57cec5SDimitry Andric case ARM::t2STMIA:
3560b57cec5SDimitry Andric case ARM::t2STMIA_UPD:
3570b57cec5SDimitry Andric case ARM::VLDMSIA:
3580b57cec5SDimitry Andric case ARM::VLDMSIA_UPD:
3590b57cec5SDimitry Andric case ARM::VSTMSIA:
3600b57cec5SDimitry Andric case ARM::VSTMSIA_UPD:
3610b57cec5SDimitry Andric case ARM::VLDMDIA:
3620b57cec5SDimitry Andric case ARM::VLDMDIA_UPD:
3630b57cec5SDimitry Andric case ARM::VSTMDIA:
3640b57cec5SDimitry Andric case ARM::VSTMDIA_UPD:
3650b57cec5SDimitry Andric return ARM_AM::ia;
3660b57cec5SDimitry Andric
3670b57cec5SDimitry Andric case ARM::LDMDA:
3680b57cec5SDimitry Andric case ARM::LDMDA_UPD:
3690b57cec5SDimitry Andric case ARM::STMDA:
3700b57cec5SDimitry Andric case ARM::STMDA_UPD:
3710b57cec5SDimitry Andric return ARM_AM::da;
3720b57cec5SDimitry Andric
3730b57cec5SDimitry Andric case ARM::LDMDB:
3740b57cec5SDimitry Andric case ARM::LDMDB_UPD:
3750b57cec5SDimitry Andric case ARM::STMDB:
3760b57cec5SDimitry Andric case ARM::STMDB_UPD:
3770b57cec5SDimitry Andric case ARM::t2LDMDB:
3780b57cec5SDimitry Andric case ARM::t2LDMDB_UPD:
3790b57cec5SDimitry Andric case ARM::t2STMDB:
3800b57cec5SDimitry Andric case ARM::t2STMDB_UPD:
3810b57cec5SDimitry Andric case ARM::VLDMSDB_UPD:
3820b57cec5SDimitry Andric case ARM::VSTMSDB_UPD:
3830b57cec5SDimitry Andric case ARM::VLDMDDB_UPD:
3840b57cec5SDimitry Andric case ARM::VSTMDDB_UPD:
3850b57cec5SDimitry Andric return ARM_AM::db;
3860b57cec5SDimitry Andric
3870b57cec5SDimitry Andric case ARM::LDMIB:
3880b57cec5SDimitry Andric case ARM::LDMIB_UPD:
3890b57cec5SDimitry Andric case ARM::STMIB:
3900b57cec5SDimitry Andric case ARM::STMIB_UPD:
3910b57cec5SDimitry Andric return ARM_AM::ib;
3920b57cec5SDimitry Andric }
3930b57cec5SDimitry Andric }
3940b57cec5SDimitry Andric
isT1i32Load(unsigned Opc)3950b57cec5SDimitry Andric static bool isT1i32Load(unsigned Opc) {
3960b57cec5SDimitry Andric return Opc == ARM::tLDRi || Opc == ARM::tLDRspi;
3970b57cec5SDimitry Andric }
3980b57cec5SDimitry Andric
isT2i32Load(unsigned Opc)3990b57cec5SDimitry Andric static bool isT2i32Load(unsigned Opc) {
4000b57cec5SDimitry Andric return Opc == ARM::t2LDRi12 || Opc == ARM::t2LDRi8;
4010b57cec5SDimitry Andric }
4020b57cec5SDimitry Andric
isi32Load(unsigned Opc)4030b57cec5SDimitry Andric static bool isi32Load(unsigned Opc) {
4040b57cec5SDimitry Andric return Opc == ARM::LDRi12 || isT1i32Load(Opc) || isT2i32Load(Opc) ;
4050b57cec5SDimitry Andric }
4060b57cec5SDimitry Andric
isT1i32Store(unsigned Opc)4070b57cec5SDimitry Andric static bool isT1i32Store(unsigned Opc) {
4080b57cec5SDimitry Andric return Opc == ARM::tSTRi || Opc == ARM::tSTRspi;
4090b57cec5SDimitry Andric }
4100b57cec5SDimitry Andric
isT2i32Store(unsigned Opc)4110b57cec5SDimitry Andric static bool isT2i32Store(unsigned Opc) {
4120b57cec5SDimitry Andric return Opc == ARM::t2STRi12 || Opc == ARM::t2STRi8;
4130b57cec5SDimitry Andric }
4140b57cec5SDimitry Andric
isi32Store(unsigned Opc)4150b57cec5SDimitry Andric static bool isi32Store(unsigned Opc) {
4160b57cec5SDimitry Andric return Opc == ARM::STRi12 || isT1i32Store(Opc) || isT2i32Store(Opc);
4170b57cec5SDimitry Andric }
4180b57cec5SDimitry Andric
isLoadSingle(unsigned Opc)4190b57cec5SDimitry Andric static bool isLoadSingle(unsigned Opc) {
4200b57cec5SDimitry Andric return isi32Load(Opc) || Opc == ARM::VLDRS || Opc == ARM::VLDRD;
4210b57cec5SDimitry Andric }
4220b57cec5SDimitry Andric
getImmScale(unsigned Opc)4230b57cec5SDimitry Andric static unsigned getImmScale(unsigned Opc) {
4240b57cec5SDimitry Andric switch (Opc) {
4250b57cec5SDimitry Andric default: llvm_unreachable("Unhandled opcode!");
4260b57cec5SDimitry Andric case ARM::tLDRi:
4270b57cec5SDimitry Andric case ARM::tSTRi:
4280b57cec5SDimitry Andric case ARM::tLDRspi:
4290b57cec5SDimitry Andric case ARM::tSTRspi:
4300b57cec5SDimitry Andric return 1;
4310b57cec5SDimitry Andric case ARM::tLDRHi:
4320b57cec5SDimitry Andric case ARM::tSTRHi:
4330b57cec5SDimitry Andric return 2;
4340b57cec5SDimitry Andric case ARM::tLDRBi:
4350b57cec5SDimitry Andric case ARM::tSTRBi:
4360b57cec5SDimitry Andric return 4;
4370b57cec5SDimitry Andric }
4380b57cec5SDimitry Andric }
4390b57cec5SDimitry Andric
getLSMultipleTransferSize(const MachineInstr * MI)4400b57cec5SDimitry Andric static unsigned getLSMultipleTransferSize(const MachineInstr *MI) {
4410b57cec5SDimitry Andric switch (MI->getOpcode()) {
4420b57cec5SDimitry Andric default: return 0;
4430b57cec5SDimitry Andric case ARM::LDRi12:
4440b57cec5SDimitry Andric case ARM::STRi12:
4450b57cec5SDimitry Andric case ARM::tLDRi:
4460b57cec5SDimitry Andric case ARM::tSTRi:
4470b57cec5SDimitry Andric case ARM::tLDRspi:
4480b57cec5SDimitry Andric case ARM::tSTRspi:
4490b57cec5SDimitry Andric case ARM::t2LDRi8:
4500b57cec5SDimitry Andric case ARM::t2LDRi12:
4510b57cec5SDimitry Andric case ARM::t2STRi8:
4520b57cec5SDimitry Andric case ARM::t2STRi12:
4530b57cec5SDimitry Andric case ARM::VLDRS:
4540b57cec5SDimitry Andric case ARM::VSTRS:
4550b57cec5SDimitry Andric return 4;
4560b57cec5SDimitry Andric case ARM::VLDRD:
4570b57cec5SDimitry Andric case ARM::VSTRD:
4580b57cec5SDimitry Andric return 8;
4590b57cec5SDimitry Andric case ARM::LDMIA:
4600b57cec5SDimitry Andric case ARM::LDMDA:
4610b57cec5SDimitry Andric case ARM::LDMDB:
4620b57cec5SDimitry Andric case ARM::LDMIB:
4630b57cec5SDimitry Andric case ARM::STMIA:
4640b57cec5SDimitry Andric case ARM::STMDA:
4650b57cec5SDimitry Andric case ARM::STMDB:
4660b57cec5SDimitry Andric case ARM::STMIB:
4670b57cec5SDimitry Andric case ARM::tLDMIA:
4680b57cec5SDimitry Andric case ARM::tLDMIA_UPD:
4690b57cec5SDimitry Andric case ARM::tSTMIA_UPD:
4700b57cec5SDimitry Andric case ARM::t2LDMIA:
4710b57cec5SDimitry Andric case ARM::t2LDMDB:
4720b57cec5SDimitry Andric case ARM::t2STMIA:
4730b57cec5SDimitry Andric case ARM::t2STMDB:
4740b57cec5SDimitry Andric case ARM::VLDMSIA:
4750b57cec5SDimitry Andric case ARM::VSTMSIA:
4760b57cec5SDimitry Andric return (MI->getNumOperands() - MI->getDesc().getNumOperands() + 1) * 4;
4770b57cec5SDimitry Andric case ARM::VLDMDIA:
4780b57cec5SDimitry Andric case ARM::VSTMDIA:
4790b57cec5SDimitry Andric return (MI->getNumOperands() - MI->getDesc().getNumOperands() + 1) * 8;
4800b57cec5SDimitry Andric }
4810b57cec5SDimitry Andric }
4820b57cec5SDimitry Andric
4830b57cec5SDimitry Andric /// Update future uses of the base register with the offset introduced
4840b57cec5SDimitry Andric /// due to writeback. This function only works on Thumb1.
UpdateBaseRegUses(MachineBasicBlock & MBB,MachineBasicBlock::iterator MBBI,const DebugLoc & DL,unsigned Base,unsigned WordOffset,ARMCC::CondCodes Pred,unsigned PredReg)4850b57cec5SDimitry Andric void ARMLoadStoreOpt::UpdateBaseRegUses(MachineBasicBlock &MBB,
4860b57cec5SDimitry Andric MachineBasicBlock::iterator MBBI,
4870b57cec5SDimitry Andric const DebugLoc &DL, unsigned Base,
4880b57cec5SDimitry Andric unsigned WordOffset,
4890b57cec5SDimitry Andric ARMCC::CondCodes Pred,
4900b57cec5SDimitry Andric unsigned PredReg) {
4910b57cec5SDimitry Andric assert(isThumb1 && "Can only update base register uses for Thumb1!");
4920b57cec5SDimitry Andric // Start updating any instructions with immediate offsets. Insert a SUB before
4930b57cec5SDimitry Andric // the first non-updateable instruction (if any).
4940b57cec5SDimitry Andric for (; MBBI != MBB.end(); ++MBBI) {
4950b57cec5SDimitry Andric bool InsertSub = false;
4960b57cec5SDimitry Andric unsigned Opc = MBBI->getOpcode();
4970b57cec5SDimitry Andric
4980b57cec5SDimitry Andric if (MBBI->readsRegister(Base)) {
4990b57cec5SDimitry Andric int Offset;
5000b57cec5SDimitry Andric bool IsLoad =
5010b57cec5SDimitry Andric Opc == ARM::tLDRi || Opc == ARM::tLDRHi || Opc == ARM::tLDRBi;
5020b57cec5SDimitry Andric bool IsStore =
5030b57cec5SDimitry Andric Opc == ARM::tSTRi || Opc == ARM::tSTRHi || Opc == ARM::tSTRBi;
5040b57cec5SDimitry Andric
5050b57cec5SDimitry Andric if (IsLoad || IsStore) {
5060b57cec5SDimitry Andric // Loads and stores with immediate offsets can be updated, but only if
5070b57cec5SDimitry Andric // the new offset isn't negative.
5080b57cec5SDimitry Andric // The MachineOperand containing the offset immediate is the last one
5090b57cec5SDimitry Andric // before predicates.
5100b57cec5SDimitry Andric MachineOperand &MO =
5110b57cec5SDimitry Andric MBBI->getOperand(MBBI->getDesc().getNumOperands() - 3);
5120b57cec5SDimitry Andric // The offsets are scaled by 1, 2 or 4 depending on the Opcode.
5130b57cec5SDimitry Andric Offset = MO.getImm() - WordOffset * getImmScale(Opc);
5140b57cec5SDimitry Andric
5150b57cec5SDimitry Andric // If storing the base register, it needs to be reset first.
5168bcb0991SDimitry Andric Register InstrSrcReg = getLoadStoreRegOp(*MBBI).getReg();
5170b57cec5SDimitry Andric
5180b57cec5SDimitry Andric if (Offset >= 0 && !(IsStore && InstrSrcReg == Base))
5190b57cec5SDimitry Andric MO.setImm(Offset);
5200b57cec5SDimitry Andric else
5210b57cec5SDimitry Andric InsertSub = true;
5220b57cec5SDimitry Andric } else if ((Opc == ARM::tSUBi8 || Opc == ARM::tADDi8) &&
5230b57cec5SDimitry Andric !definesCPSR(*MBBI)) {
5240b57cec5SDimitry Andric // SUBS/ADDS using this register, with a dead def of the CPSR.
5250b57cec5SDimitry Andric // Merge it with the update; if the merged offset is too large,
5260b57cec5SDimitry Andric // insert a new sub instead.
5270b57cec5SDimitry Andric MachineOperand &MO =
5280b57cec5SDimitry Andric MBBI->getOperand(MBBI->getDesc().getNumOperands() - 3);
5290b57cec5SDimitry Andric Offset = (Opc == ARM::tSUBi8) ?
5300b57cec5SDimitry Andric MO.getImm() + WordOffset * 4 :
5310b57cec5SDimitry Andric MO.getImm() - WordOffset * 4 ;
5320b57cec5SDimitry Andric if (Offset >= 0 && TL->isLegalAddImmediate(Offset)) {
5330b57cec5SDimitry Andric // FIXME: Swap ADDS<->SUBS if Offset < 0, erase instruction if
5340b57cec5SDimitry Andric // Offset == 0.
5350b57cec5SDimitry Andric MO.setImm(Offset);
5360b57cec5SDimitry Andric // The base register has now been reset, so exit early.
5370b57cec5SDimitry Andric return;
5380b57cec5SDimitry Andric } else {
5390b57cec5SDimitry Andric InsertSub = true;
5400b57cec5SDimitry Andric }
5410b57cec5SDimitry Andric } else {
5420b57cec5SDimitry Andric // Can't update the instruction.
5430b57cec5SDimitry Andric InsertSub = true;
5440b57cec5SDimitry Andric }
5450b57cec5SDimitry Andric } else if (definesCPSR(*MBBI) || MBBI->isCall() || MBBI->isBranch()) {
5460b57cec5SDimitry Andric // Since SUBS sets the condition flags, we can't place the base reset
5470b57cec5SDimitry Andric // after an instruction that has a live CPSR def.
5480b57cec5SDimitry Andric // The base register might also contain an argument for a function call.
5490b57cec5SDimitry Andric InsertSub = true;
5500b57cec5SDimitry Andric }
5510b57cec5SDimitry Andric
5520b57cec5SDimitry Andric if (InsertSub) {
5530b57cec5SDimitry Andric // An instruction above couldn't be updated, so insert a sub.
5540b57cec5SDimitry Andric BuildMI(MBB, MBBI, DL, TII->get(ARM::tSUBi8), Base)
5550b57cec5SDimitry Andric .add(t1CondCodeOp(true))
5560b57cec5SDimitry Andric .addReg(Base)
5570b57cec5SDimitry Andric .addImm(WordOffset * 4)
5580b57cec5SDimitry Andric .addImm(Pred)
5590b57cec5SDimitry Andric .addReg(PredReg);
5600b57cec5SDimitry Andric return;
5610b57cec5SDimitry Andric }
5620b57cec5SDimitry Andric
5630b57cec5SDimitry Andric if (MBBI->killsRegister(Base) || MBBI->definesRegister(Base))
5640b57cec5SDimitry Andric // Register got killed. Stop updating.
5650b57cec5SDimitry Andric return;
5660b57cec5SDimitry Andric }
5670b57cec5SDimitry Andric
5680b57cec5SDimitry Andric // End of block was reached.
569349cc55cSDimitry Andric if (!MBB.succ_empty()) {
5700b57cec5SDimitry Andric // FIXME: Because of a bug, live registers are sometimes missing from
5710b57cec5SDimitry Andric // the successor blocks' live-in sets. This means we can't trust that
5720b57cec5SDimitry Andric // information and *always* have to reset at the end of a block.
5730b57cec5SDimitry Andric // See PR21029.
5740b57cec5SDimitry Andric if (MBBI != MBB.end()) --MBBI;
5750b57cec5SDimitry Andric BuildMI(MBB, MBBI, DL, TII->get(ARM::tSUBi8), Base)
5760b57cec5SDimitry Andric .add(t1CondCodeOp(true))
5770b57cec5SDimitry Andric .addReg(Base)
5780b57cec5SDimitry Andric .addImm(WordOffset * 4)
5790b57cec5SDimitry Andric .addImm(Pred)
5800b57cec5SDimitry Andric .addReg(PredReg);
5810b57cec5SDimitry Andric }
5820b57cec5SDimitry Andric }
5830b57cec5SDimitry Andric
5840b57cec5SDimitry Andric /// Return the first register of class \p RegClass that is not in \p Regs.
findFreeReg(const TargetRegisterClass & RegClass)5850b57cec5SDimitry Andric unsigned ARMLoadStoreOpt::findFreeReg(const TargetRegisterClass &RegClass) {
5860b57cec5SDimitry Andric if (!RegClassInfoValid) {
5870b57cec5SDimitry Andric RegClassInfo.runOnMachineFunction(*MF);
5880b57cec5SDimitry Andric RegClassInfoValid = true;
5890b57cec5SDimitry Andric }
5900b57cec5SDimitry Andric
5910b57cec5SDimitry Andric for (unsigned Reg : RegClassInfo.getOrder(&RegClass))
592349cc55cSDimitry Andric if (LiveRegs.available(MF->getRegInfo(), Reg))
5930b57cec5SDimitry Andric return Reg;
5940b57cec5SDimitry Andric return 0;
5950b57cec5SDimitry Andric }
5960b57cec5SDimitry Andric
5970b57cec5SDimitry Andric /// Compute live registers just before instruction \p Before (in normal schedule
5980b57cec5SDimitry Andric /// direction). Computes backwards so multiple queries in the same block must
5990b57cec5SDimitry Andric /// come in reverse order.
moveLiveRegsBefore(const MachineBasicBlock & MBB,MachineBasicBlock::const_iterator Before)6000b57cec5SDimitry Andric void ARMLoadStoreOpt::moveLiveRegsBefore(const MachineBasicBlock &MBB,
6010b57cec5SDimitry Andric MachineBasicBlock::const_iterator Before) {
6020b57cec5SDimitry Andric // Initialize if we never queried in this block.
6030b57cec5SDimitry Andric if (!LiveRegsValid) {
6040b57cec5SDimitry Andric LiveRegs.init(*TRI);
6050b57cec5SDimitry Andric LiveRegs.addLiveOuts(MBB);
6060b57cec5SDimitry Andric LiveRegPos = MBB.end();
6070b57cec5SDimitry Andric LiveRegsValid = true;
6080b57cec5SDimitry Andric }
6090b57cec5SDimitry Andric // Move backward just before the "Before" position.
6100b57cec5SDimitry Andric while (LiveRegPos != Before) {
6110b57cec5SDimitry Andric --LiveRegPos;
6120b57cec5SDimitry Andric LiveRegs.stepBackward(*LiveRegPos);
6130b57cec5SDimitry Andric }
6140b57cec5SDimitry Andric }
6150b57cec5SDimitry Andric
ContainsReg(const ArrayRef<std::pair<unsigned,bool>> & Regs,unsigned Reg)6160b57cec5SDimitry Andric static bool ContainsReg(const ArrayRef<std::pair<unsigned, bool>> &Regs,
6170b57cec5SDimitry Andric unsigned Reg) {
6180b57cec5SDimitry Andric for (const std::pair<unsigned, bool> &R : Regs)
6190b57cec5SDimitry Andric if (R.first == Reg)
6200b57cec5SDimitry Andric return true;
6210b57cec5SDimitry Andric return false;
6220b57cec5SDimitry Andric }
6230b57cec5SDimitry Andric
6240b57cec5SDimitry Andric /// Create and insert a LDM or STM with Base as base register and registers in
6250b57cec5SDimitry Andric /// Regs as the register operands that would be loaded / stored. It returns
6260b57cec5SDimitry Andric /// true if the transformation is done.
CreateLoadStoreMulti(MachineBasicBlock & MBB,MachineBasicBlock::iterator InsertBefore,int Offset,unsigned Base,bool BaseKill,unsigned Opcode,ARMCC::CondCodes Pred,unsigned PredReg,const DebugLoc & DL,ArrayRef<std::pair<unsigned,bool>> Regs,ArrayRef<MachineInstr * > Instrs)6270b57cec5SDimitry Andric MachineInstr *ARMLoadStoreOpt::CreateLoadStoreMulti(
6280b57cec5SDimitry Andric MachineBasicBlock &MBB, MachineBasicBlock::iterator InsertBefore,
6290b57cec5SDimitry Andric int Offset, unsigned Base, bool BaseKill, unsigned Opcode,
6300b57cec5SDimitry Andric ARMCC::CondCodes Pred, unsigned PredReg, const DebugLoc &DL,
6310b57cec5SDimitry Andric ArrayRef<std::pair<unsigned, bool>> Regs,
6320b57cec5SDimitry Andric ArrayRef<MachineInstr*> Instrs) {
6330b57cec5SDimitry Andric unsigned NumRegs = Regs.size();
6340b57cec5SDimitry Andric assert(NumRegs > 1);
6350b57cec5SDimitry Andric
6360b57cec5SDimitry Andric // For Thumb1 targets, it might be necessary to clobber the CPSR to merge.
6370b57cec5SDimitry Andric // Compute liveness information for that register to make the decision.
6380b57cec5SDimitry Andric bool SafeToClobberCPSR = !isThumb1 ||
6390b57cec5SDimitry Andric (MBB.computeRegisterLiveness(TRI, ARM::CPSR, InsertBefore, 20) ==
6400b57cec5SDimitry Andric MachineBasicBlock::LQR_Dead);
6410b57cec5SDimitry Andric
6420b57cec5SDimitry Andric bool Writeback = isThumb1; // Thumb1 LDM/STM have base reg writeback.
6430b57cec5SDimitry Andric
6440b57cec5SDimitry Andric // Exception: If the base register is in the input reglist, Thumb1 LDM is
6450b57cec5SDimitry Andric // non-writeback.
6460b57cec5SDimitry Andric // It's also not possible to merge an STR of the base register in Thumb1.
6470b57cec5SDimitry Andric if (isThumb1 && ContainsReg(Regs, Base)) {
6480b57cec5SDimitry Andric assert(Base != ARM::SP && "Thumb1 does not allow SP in register list");
6490b57cec5SDimitry Andric if (Opcode == ARM::tLDRi)
6500b57cec5SDimitry Andric Writeback = false;
6510b57cec5SDimitry Andric else if (Opcode == ARM::tSTRi)
6520b57cec5SDimitry Andric return nullptr;
6530b57cec5SDimitry Andric }
6540b57cec5SDimitry Andric
6550b57cec5SDimitry Andric ARM_AM::AMSubMode Mode = ARM_AM::ia;
6560b57cec5SDimitry Andric // VFP and Thumb2 do not support IB or DA modes. Thumb1 only supports IA.
6570b57cec5SDimitry Andric bool isNotVFP = isi32Load(Opcode) || isi32Store(Opcode);
6580b57cec5SDimitry Andric bool haveIBAndDA = isNotVFP && !isThumb2 && !isThumb1;
6590b57cec5SDimitry Andric
6600b57cec5SDimitry Andric if (Offset == 4 && haveIBAndDA) {
6610b57cec5SDimitry Andric Mode = ARM_AM::ib;
6620b57cec5SDimitry Andric } else if (Offset == -4 * (int)NumRegs + 4 && haveIBAndDA) {
6630b57cec5SDimitry Andric Mode = ARM_AM::da;
6640b57cec5SDimitry Andric } else if (Offset == -4 * (int)NumRegs && isNotVFP && !isThumb1) {
6650b57cec5SDimitry Andric // VLDM/VSTM do not support DB mode without also updating the base reg.
6660b57cec5SDimitry Andric Mode = ARM_AM::db;
6670b57cec5SDimitry Andric } else if (Offset != 0 || Opcode == ARM::tLDRspi || Opcode == ARM::tSTRspi) {
6680b57cec5SDimitry Andric // Check if this is a supported opcode before inserting instructions to
6690b57cec5SDimitry Andric // calculate a new base register.
6700b57cec5SDimitry Andric if (!getLoadStoreMultipleOpcode(Opcode, Mode)) return nullptr;
6710b57cec5SDimitry Andric
6720b57cec5SDimitry Andric // If starting offset isn't zero, insert a MI to materialize a new base.
6730b57cec5SDimitry Andric // But only do so if it is cost effective, i.e. merging more than two
6740b57cec5SDimitry Andric // loads / stores.
6750b57cec5SDimitry Andric if (NumRegs <= 2)
6760b57cec5SDimitry Andric return nullptr;
6770b57cec5SDimitry Andric
6780b57cec5SDimitry Andric // On Thumb1, it's not worth materializing a new base register without
6790b57cec5SDimitry Andric // clobbering the CPSR (i.e. not using ADDS/SUBS).
6800b57cec5SDimitry Andric if (!SafeToClobberCPSR)
6810b57cec5SDimitry Andric return nullptr;
6820b57cec5SDimitry Andric
6830b57cec5SDimitry Andric unsigned NewBase;
6840b57cec5SDimitry Andric if (isi32Load(Opcode)) {
6850b57cec5SDimitry Andric // If it is a load, then just use one of the destination registers
6860b57cec5SDimitry Andric // as the new base. Will no longer be writeback in Thumb1.
6870b57cec5SDimitry Andric NewBase = Regs[NumRegs-1].first;
6880b57cec5SDimitry Andric Writeback = false;
6890b57cec5SDimitry Andric } else {
6900b57cec5SDimitry Andric // Find a free register that we can use as scratch register.
6910b57cec5SDimitry Andric moveLiveRegsBefore(MBB, InsertBefore);
6920b57cec5SDimitry Andric // The merged instruction does not exist yet but will use several Regs if
6930b57cec5SDimitry Andric // it is a Store.
6940b57cec5SDimitry Andric if (!isLoadSingle(Opcode))
6950b57cec5SDimitry Andric for (const std::pair<unsigned, bool> &R : Regs)
6960b57cec5SDimitry Andric LiveRegs.addReg(R.first);
6970b57cec5SDimitry Andric
6980b57cec5SDimitry Andric NewBase = findFreeReg(isThumb1 ? ARM::tGPRRegClass : ARM::GPRRegClass);
6990b57cec5SDimitry Andric if (NewBase == 0)
7000b57cec5SDimitry Andric return nullptr;
7010b57cec5SDimitry Andric }
7020b57cec5SDimitry Andric
703480093f4SDimitry Andric int BaseOpc = isThumb2 ? (BaseKill && Base == ARM::SP ? ARM::t2ADDspImm
704480093f4SDimitry Andric : ARM::t2ADDri)
705480093f4SDimitry Andric : (isThumb1 && Base == ARM::SP)
706480093f4SDimitry Andric ? ARM::tADDrSPi
707480093f4SDimitry Andric : (isThumb1 && Offset < 8)
708480093f4SDimitry Andric ? ARM::tADDi3
709480093f4SDimitry Andric : isThumb1 ? ARM::tADDi8 : ARM::ADDri;
7100b57cec5SDimitry Andric
7110b57cec5SDimitry Andric if (Offset < 0) {
712480093f4SDimitry Andric // FIXME: There are no Thumb1 load/store instructions with negative
713480093f4SDimitry Andric // offsets. So the Base != ARM::SP might be unnecessary.
7140b57cec5SDimitry Andric Offset = -Offset;
715480093f4SDimitry Andric BaseOpc = isThumb2 ? (BaseKill && Base == ARM::SP ? ARM::t2SUBspImm
716480093f4SDimitry Andric : ARM::t2SUBri)
717480093f4SDimitry Andric : (isThumb1 && Offset < 8 && Base != ARM::SP)
718480093f4SDimitry Andric ? ARM::tSUBi3
719480093f4SDimitry Andric : isThumb1 ? ARM::tSUBi8 : ARM::SUBri;
7200b57cec5SDimitry Andric }
7210b57cec5SDimitry Andric
7220b57cec5SDimitry Andric if (!TL->isLegalAddImmediate(Offset))
7230b57cec5SDimitry Andric // FIXME: Try add with register operand?
7240b57cec5SDimitry Andric return nullptr; // Probably not worth it then.
7250b57cec5SDimitry Andric
7260b57cec5SDimitry Andric // We can only append a kill flag to the add/sub input if the value is not
7270b57cec5SDimitry Andric // used in the register list of the stm as well.
7280b57cec5SDimitry Andric bool KillOldBase = BaseKill &&
7290b57cec5SDimitry Andric (!isi32Store(Opcode) || !ContainsReg(Regs, Base));
7300b57cec5SDimitry Andric
7310b57cec5SDimitry Andric if (isThumb1) {
7320b57cec5SDimitry Andric // Thumb1: depending on immediate size, use either
7330b57cec5SDimitry Andric // ADDS NewBase, Base, #imm3
7340b57cec5SDimitry Andric // or
7350b57cec5SDimitry Andric // MOV NewBase, Base
7360b57cec5SDimitry Andric // ADDS NewBase, #imm8.
7370b57cec5SDimitry Andric if (Base != NewBase &&
7380b57cec5SDimitry Andric (BaseOpc == ARM::tADDi8 || BaseOpc == ARM::tSUBi8)) {
7390b57cec5SDimitry Andric // Need to insert a MOV to the new base first.
7400b57cec5SDimitry Andric if (isARMLowRegister(NewBase) && isARMLowRegister(Base) &&
7410b57cec5SDimitry Andric !STI->hasV6Ops()) {
7420b57cec5SDimitry Andric // thumbv4t doesn't have lo->lo copies, and we can't predicate tMOVSr
7430b57cec5SDimitry Andric if (Pred != ARMCC::AL)
7440b57cec5SDimitry Andric return nullptr;
7450b57cec5SDimitry Andric BuildMI(MBB, InsertBefore, DL, TII->get(ARM::tMOVSr), NewBase)
7460b57cec5SDimitry Andric .addReg(Base, getKillRegState(KillOldBase));
7470b57cec5SDimitry Andric } else
7480b57cec5SDimitry Andric BuildMI(MBB, InsertBefore, DL, TII->get(ARM::tMOVr), NewBase)
7490b57cec5SDimitry Andric .addReg(Base, getKillRegState(KillOldBase))
7500b57cec5SDimitry Andric .add(predOps(Pred, PredReg));
7510b57cec5SDimitry Andric
7520b57cec5SDimitry Andric // The following ADDS/SUBS becomes an update.
7530b57cec5SDimitry Andric Base = NewBase;
7540b57cec5SDimitry Andric KillOldBase = true;
7550b57cec5SDimitry Andric }
7560b57cec5SDimitry Andric if (BaseOpc == ARM::tADDrSPi) {
7570b57cec5SDimitry Andric assert(Offset % 4 == 0 && "tADDrSPi offset is scaled by 4");
7580b57cec5SDimitry Andric BuildMI(MBB, InsertBefore, DL, TII->get(BaseOpc), NewBase)
7590b57cec5SDimitry Andric .addReg(Base, getKillRegState(KillOldBase))
7600b57cec5SDimitry Andric .addImm(Offset / 4)
7610b57cec5SDimitry Andric .add(predOps(Pred, PredReg));
7620b57cec5SDimitry Andric } else
7630b57cec5SDimitry Andric BuildMI(MBB, InsertBefore, DL, TII->get(BaseOpc), NewBase)
7640b57cec5SDimitry Andric .add(t1CondCodeOp(true))
7650b57cec5SDimitry Andric .addReg(Base, getKillRegState(KillOldBase))
7660b57cec5SDimitry Andric .addImm(Offset)
7670b57cec5SDimitry Andric .add(predOps(Pred, PredReg));
7680b57cec5SDimitry Andric } else {
7690b57cec5SDimitry Andric BuildMI(MBB, InsertBefore, DL, TII->get(BaseOpc), NewBase)
7700b57cec5SDimitry Andric .addReg(Base, getKillRegState(KillOldBase))
7710b57cec5SDimitry Andric .addImm(Offset)
7720b57cec5SDimitry Andric .add(predOps(Pred, PredReg))
7730b57cec5SDimitry Andric .add(condCodeOp());
7740b57cec5SDimitry Andric }
7750b57cec5SDimitry Andric Base = NewBase;
7760b57cec5SDimitry Andric BaseKill = true; // New base is always killed straight away.
7770b57cec5SDimitry Andric }
7780b57cec5SDimitry Andric
7790b57cec5SDimitry Andric bool isDef = isLoadSingle(Opcode);
7800b57cec5SDimitry Andric
7810b57cec5SDimitry Andric // Get LS multiple opcode. Note that for Thumb1 this might be an opcode with
7820b57cec5SDimitry Andric // base register writeback.
7830b57cec5SDimitry Andric Opcode = getLoadStoreMultipleOpcode(Opcode, Mode);
7840b57cec5SDimitry Andric if (!Opcode)
7850b57cec5SDimitry Andric return nullptr;
7860b57cec5SDimitry Andric
7870b57cec5SDimitry Andric // Check if a Thumb1 LDM/STM merge is safe. This is the case if:
7880b57cec5SDimitry Andric // - There is no writeback (LDM of base register),
7890b57cec5SDimitry Andric // - the base register is killed by the merged instruction,
7900b57cec5SDimitry Andric // - or it's safe to overwrite the condition flags, i.e. to insert a SUBS
7910b57cec5SDimitry Andric // to reset the base register.
7920b57cec5SDimitry Andric // Otherwise, don't merge.
7930b57cec5SDimitry Andric // It's safe to return here since the code to materialize a new base register
7940b57cec5SDimitry Andric // above is also conditional on SafeToClobberCPSR.
7950b57cec5SDimitry Andric if (isThumb1 && !SafeToClobberCPSR && Writeback && !BaseKill)
7960b57cec5SDimitry Andric return nullptr;
7970b57cec5SDimitry Andric
7980b57cec5SDimitry Andric MachineInstrBuilder MIB;
7990b57cec5SDimitry Andric
8000b57cec5SDimitry Andric if (Writeback) {
8010b57cec5SDimitry Andric assert(isThumb1 && "expected Writeback only inThumb1");
8020b57cec5SDimitry Andric if (Opcode == ARM::tLDMIA) {
8030b57cec5SDimitry Andric assert(!(ContainsReg(Regs, Base)) && "Thumb1 can't LDM ! with Base in Regs");
8040b57cec5SDimitry Andric // Update tLDMIA with writeback if necessary.
8050b57cec5SDimitry Andric Opcode = ARM::tLDMIA_UPD;
8060b57cec5SDimitry Andric }
8070b57cec5SDimitry Andric
8080b57cec5SDimitry Andric MIB = BuildMI(MBB, InsertBefore, DL, TII->get(Opcode));
8090b57cec5SDimitry Andric
8100b57cec5SDimitry Andric // Thumb1: we might need to set base writeback when building the MI.
8110b57cec5SDimitry Andric MIB.addReg(Base, getDefRegState(true))
8120b57cec5SDimitry Andric .addReg(Base, getKillRegState(BaseKill));
8130b57cec5SDimitry Andric
8140b57cec5SDimitry Andric // The base isn't dead after a merged instruction with writeback.
8150b57cec5SDimitry Andric // Insert a sub instruction after the newly formed instruction to reset.
8160b57cec5SDimitry Andric if (!BaseKill)
8170b57cec5SDimitry Andric UpdateBaseRegUses(MBB, InsertBefore, DL, Base, NumRegs, Pred, PredReg);
8180b57cec5SDimitry Andric } else {
8190b57cec5SDimitry Andric // No writeback, simply build the MachineInstr.
8200b57cec5SDimitry Andric MIB = BuildMI(MBB, InsertBefore, DL, TII->get(Opcode));
8210b57cec5SDimitry Andric MIB.addReg(Base, getKillRegState(BaseKill));
8220b57cec5SDimitry Andric }
8230b57cec5SDimitry Andric
8240b57cec5SDimitry Andric MIB.addImm(Pred).addReg(PredReg);
8250b57cec5SDimitry Andric
8260b57cec5SDimitry Andric for (const std::pair<unsigned, bool> &R : Regs)
8270b57cec5SDimitry Andric MIB.addReg(R.first, getDefRegState(isDef) | getKillRegState(R.second));
8280b57cec5SDimitry Andric
8290b57cec5SDimitry Andric MIB.cloneMergedMemRefs(Instrs);
8300b57cec5SDimitry Andric
8310b57cec5SDimitry Andric return MIB.getInstr();
8320b57cec5SDimitry Andric }
8330b57cec5SDimitry Andric
CreateLoadStoreDouble(MachineBasicBlock & MBB,MachineBasicBlock::iterator InsertBefore,int Offset,unsigned Base,bool BaseKill,unsigned Opcode,ARMCC::CondCodes Pred,unsigned PredReg,const DebugLoc & DL,ArrayRef<std::pair<unsigned,bool>> Regs,ArrayRef<MachineInstr * > Instrs) const8340b57cec5SDimitry Andric MachineInstr *ARMLoadStoreOpt::CreateLoadStoreDouble(
8350b57cec5SDimitry Andric MachineBasicBlock &MBB, MachineBasicBlock::iterator InsertBefore,
8360b57cec5SDimitry Andric int Offset, unsigned Base, bool BaseKill, unsigned Opcode,
8370b57cec5SDimitry Andric ARMCC::CondCodes Pred, unsigned PredReg, const DebugLoc &DL,
8380b57cec5SDimitry Andric ArrayRef<std::pair<unsigned, bool>> Regs,
8390b57cec5SDimitry Andric ArrayRef<MachineInstr*> Instrs) const {
8400b57cec5SDimitry Andric bool IsLoad = isi32Load(Opcode);
8410b57cec5SDimitry Andric assert((IsLoad || isi32Store(Opcode)) && "Must have integer load or store");
8420b57cec5SDimitry Andric unsigned LoadStoreOpcode = IsLoad ? ARM::t2LDRDi8 : ARM::t2STRDi8;
8430b57cec5SDimitry Andric
8440b57cec5SDimitry Andric assert(Regs.size() == 2);
8450b57cec5SDimitry Andric MachineInstrBuilder MIB = BuildMI(MBB, InsertBefore, DL,
8460b57cec5SDimitry Andric TII->get(LoadStoreOpcode));
8470b57cec5SDimitry Andric if (IsLoad) {
8480b57cec5SDimitry Andric MIB.addReg(Regs[0].first, RegState::Define)
8490b57cec5SDimitry Andric .addReg(Regs[1].first, RegState::Define);
8500b57cec5SDimitry Andric } else {
8510b57cec5SDimitry Andric MIB.addReg(Regs[0].first, getKillRegState(Regs[0].second))
8520b57cec5SDimitry Andric .addReg(Regs[1].first, getKillRegState(Regs[1].second));
8530b57cec5SDimitry Andric }
8540b57cec5SDimitry Andric MIB.addReg(Base).addImm(Offset).addImm(Pred).addReg(PredReg);
8550b57cec5SDimitry Andric MIB.cloneMergedMemRefs(Instrs);
8560b57cec5SDimitry Andric return MIB.getInstr();
8570b57cec5SDimitry Andric }
8580b57cec5SDimitry Andric
8590b57cec5SDimitry Andric /// Call MergeOps and update MemOps and merges accordingly on success.
MergeOpsUpdate(const MergeCandidate & Cand)8600b57cec5SDimitry Andric MachineInstr *ARMLoadStoreOpt::MergeOpsUpdate(const MergeCandidate &Cand) {
8610b57cec5SDimitry Andric const MachineInstr *First = Cand.Instrs.front();
8620b57cec5SDimitry Andric unsigned Opcode = First->getOpcode();
8630b57cec5SDimitry Andric bool IsLoad = isLoadSingle(Opcode);
8640b57cec5SDimitry Andric SmallVector<std::pair<unsigned, bool>, 8> Regs;
8650b57cec5SDimitry Andric SmallVector<unsigned, 4> ImpDefs;
8660b57cec5SDimitry Andric DenseSet<unsigned> KilledRegs;
8670b57cec5SDimitry Andric DenseSet<unsigned> UsedRegs;
8680b57cec5SDimitry Andric // Determine list of registers and list of implicit super-register defs.
8690b57cec5SDimitry Andric for (const MachineInstr *MI : Cand.Instrs) {
8700b57cec5SDimitry Andric const MachineOperand &MO = getLoadStoreRegOp(*MI);
8718bcb0991SDimitry Andric Register Reg = MO.getReg();
8720b57cec5SDimitry Andric bool IsKill = MO.isKill();
8730b57cec5SDimitry Andric if (IsKill)
8740b57cec5SDimitry Andric KilledRegs.insert(Reg);
8750b57cec5SDimitry Andric Regs.push_back(std::make_pair(Reg, IsKill));
8760b57cec5SDimitry Andric UsedRegs.insert(Reg);
8770b57cec5SDimitry Andric
8780b57cec5SDimitry Andric if (IsLoad) {
8790b57cec5SDimitry Andric // Collect any implicit defs of super-registers, after merging we can't
8800b57cec5SDimitry Andric // be sure anymore that we properly preserved these live ranges and must
8810b57cec5SDimitry Andric // removed these implicit operands.
8820b57cec5SDimitry Andric for (const MachineOperand &MO : MI->implicit_operands()) {
8830b57cec5SDimitry Andric if (!MO.isReg() || !MO.isDef() || MO.isDead())
8840b57cec5SDimitry Andric continue;
8850b57cec5SDimitry Andric assert(MO.isImplicit());
8868bcb0991SDimitry Andric Register DefReg = MO.getReg();
8870b57cec5SDimitry Andric
8880b57cec5SDimitry Andric if (is_contained(ImpDefs, DefReg))
8890b57cec5SDimitry Andric continue;
8900b57cec5SDimitry Andric // We can ignore cases where the super-reg is read and written.
8910b57cec5SDimitry Andric if (MI->readsRegister(DefReg))
8920b57cec5SDimitry Andric continue;
8930b57cec5SDimitry Andric ImpDefs.push_back(DefReg);
8940b57cec5SDimitry Andric }
8950b57cec5SDimitry Andric }
8960b57cec5SDimitry Andric }
8970b57cec5SDimitry Andric
8980b57cec5SDimitry Andric // Attempt the merge.
8990b57cec5SDimitry Andric using iterator = MachineBasicBlock::iterator;
9000b57cec5SDimitry Andric
9010b57cec5SDimitry Andric MachineInstr *LatestMI = Cand.Instrs[Cand.LatestMIIdx];
9020b57cec5SDimitry Andric iterator InsertBefore = std::next(iterator(LatestMI));
9030b57cec5SDimitry Andric MachineBasicBlock &MBB = *LatestMI->getParent();
9040b57cec5SDimitry Andric unsigned Offset = getMemoryOpOffset(*First);
9058bcb0991SDimitry Andric Register Base = getLoadStoreBaseOp(*First).getReg();
9060b57cec5SDimitry Andric bool BaseKill = LatestMI->killsRegister(Base);
9075ffd83dbSDimitry Andric Register PredReg;
9080b57cec5SDimitry Andric ARMCC::CondCodes Pred = getInstrPredicate(*First, PredReg);
9090b57cec5SDimitry Andric DebugLoc DL = First->getDebugLoc();
9100b57cec5SDimitry Andric MachineInstr *Merged = nullptr;
9110b57cec5SDimitry Andric if (Cand.CanMergeToLSDouble)
9120b57cec5SDimitry Andric Merged = CreateLoadStoreDouble(MBB, InsertBefore, Offset, Base, BaseKill,
9130b57cec5SDimitry Andric Opcode, Pred, PredReg, DL, Regs,
9140b57cec5SDimitry Andric Cand.Instrs);
9150b57cec5SDimitry Andric if (!Merged && Cand.CanMergeToLSMulti)
9160b57cec5SDimitry Andric Merged = CreateLoadStoreMulti(MBB, InsertBefore, Offset, Base, BaseKill,
9170b57cec5SDimitry Andric Opcode, Pred, PredReg, DL, Regs, Cand.Instrs);
9180b57cec5SDimitry Andric if (!Merged)
9190b57cec5SDimitry Andric return nullptr;
9200b57cec5SDimitry Andric
9210b57cec5SDimitry Andric // Determine earliest instruction that will get removed. We then keep an
9220b57cec5SDimitry Andric // iterator just above it so the following erases don't invalidated it.
9230b57cec5SDimitry Andric iterator EarliestI(Cand.Instrs[Cand.EarliestMIIdx]);
9240b57cec5SDimitry Andric bool EarliestAtBegin = false;
9250b57cec5SDimitry Andric if (EarliestI == MBB.begin()) {
9260b57cec5SDimitry Andric EarliestAtBegin = true;
9270b57cec5SDimitry Andric } else {
9280b57cec5SDimitry Andric EarliestI = std::prev(EarliestI);
9290b57cec5SDimitry Andric }
9300b57cec5SDimitry Andric
9310b57cec5SDimitry Andric // Remove instructions which have been merged.
9320b57cec5SDimitry Andric for (MachineInstr *MI : Cand.Instrs)
9330b57cec5SDimitry Andric MBB.erase(MI);
9340b57cec5SDimitry Andric
9350b57cec5SDimitry Andric // Determine range between the earliest removed instruction and the new one.
9360b57cec5SDimitry Andric if (EarliestAtBegin)
9370b57cec5SDimitry Andric EarliestI = MBB.begin();
9380b57cec5SDimitry Andric else
9390b57cec5SDimitry Andric EarliestI = std::next(EarliestI);
9400b57cec5SDimitry Andric auto FixupRange = make_range(EarliestI, iterator(Merged));
9410b57cec5SDimitry Andric
9420b57cec5SDimitry Andric if (isLoadSingle(Opcode)) {
9430b57cec5SDimitry Andric // If the previous loads defined a super-reg, then we have to mark earlier
9440b57cec5SDimitry Andric // operands undef; Replicate the super-reg def on the merged instruction.
9450b57cec5SDimitry Andric for (MachineInstr &MI : FixupRange) {
9460b57cec5SDimitry Andric for (unsigned &ImpDefReg : ImpDefs) {
9470b57cec5SDimitry Andric for (MachineOperand &MO : MI.implicit_operands()) {
9480b57cec5SDimitry Andric if (!MO.isReg() || MO.getReg() != ImpDefReg)
9490b57cec5SDimitry Andric continue;
9500b57cec5SDimitry Andric if (MO.readsReg())
9510b57cec5SDimitry Andric MO.setIsUndef();
9520b57cec5SDimitry Andric else if (MO.isDef())
9530b57cec5SDimitry Andric ImpDefReg = 0;
9540b57cec5SDimitry Andric }
9550b57cec5SDimitry Andric }
9560b57cec5SDimitry Andric }
9570b57cec5SDimitry Andric
9580b57cec5SDimitry Andric MachineInstrBuilder MIB(*Merged->getParent()->getParent(), Merged);
9590b57cec5SDimitry Andric for (unsigned ImpDef : ImpDefs)
9600b57cec5SDimitry Andric MIB.addReg(ImpDef, RegState::ImplicitDefine);
9610b57cec5SDimitry Andric } else {
9620b57cec5SDimitry Andric // Remove kill flags: We are possibly storing the values later now.
9630b57cec5SDimitry Andric assert(isi32Store(Opcode) || Opcode == ARM::VSTRS || Opcode == ARM::VSTRD);
9640b57cec5SDimitry Andric for (MachineInstr &MI : FixupRange) {
9650b57cec5SDimitry Andric for (MachineOperand &MO : MI.uses()) {
9660b57cec5SDimitry Andric if (!MO.isReg() || !MO.isKill())
9670b57cec5SDimitry Andric continue;
9680b57cec5SDimitry Andric if (UsedRegs.count(MO.getReg()))
9690b57cec5SDimitry Andric MO.setIsKill(false);
9700b57cec5SDimitry Andric }
9710b57cec5SDimitry Andric }
9720b57cec5SDimitry Andric assert(ImpDefs.empty());
9730b57cec5SDimitry Andric }
9740b57cec5SDimitry Andric
9750b57cec5SDimitry Andric return Merged;
9760b57cec5SDimitry Andric }
9770b57cec5SDimitry Andric
isValidLSDoubleOffset(int Offset)9780b57cec5SDimitry Andric static bool isValidLSDoubleOffset(int Offset) {
9790b57cec5SDimitry Andric unsigned Value = abs(Offset);
9800b57cec5SDimitry Andric // t2LDRDi8/t2STRDi8 supports an 8 bit immediate which is internally
9810b57cec5SDimitry Andric // multiplied by 4.
9820b57cec5SDimitry Andric return (Value % 4) == 0 && Value < 1024;
9830b57cec5SDimitry Andric }
9840b57cec5SDimitry Andric
9850b57cec5SDimitry Andric /// Return true for loads/stores that can be combined to a double/multi
9860b57cec5SDimitry Andric /// operation without increasing the requirements for alignment.
mayCombineMisaligned(const TargetSubtargetInfo & STI,const MachineInstr & MI)9870b57cec5SDimitry Andric static bool mayCombineMisaligned(const TargetSubtargetInfo &STI,
9880b57cec5SDimitry Andric const MachineInstr &MI) {
9890b57cec5SDimitry Andric // vldr/vstr trap on misaligned pointers anyway, forming vldm makes no
9900b57cec5SDimitry Andric // difference.
9910b57cec5SDimitry Andric unsigned Opcode = MI.getOpcode();
9920b57cec5SDimitry Andric if (!isi32Load(Opcode) && !isi32Store(Opcode))
9930b57cec5SDimitry Andric return true;
9940b57cec5SDimitry Andric
9950b57cec5SDimitry Andric // Stack pointer alignment is out of the programmers control so we can trust
9960b57cec5SDimitry Andric // SP-relative loads/stores.
9970b57cec5SDimitry Andric if (getLoadStoreBaseOp(MI).getReg() == ARM::SP &&
9985ffd83dbSDimitry Andric STI.getFrameLowering()->getTransientStackAlign() >= Align(4))
9990b57cec5SDimitry Andric return true;
10000b57cec5SDimitry Andric return false;
10010b57cec5SDimitry Andric }
10020b57cec5SDimitry Andric
10030b57cec5SDimitry Andric /// Find candidates for load/store multiple merge in list of MemOpQueueEntries.
FormCandidates(const MemOpQueue & MemOps)10040b57cec5SDimitry Andric void ARMLoadStoreOpt::FormCandidates(const MemOpQueue &MemOps) {
10050b57cec5SDimitry Andric const MachineInstr *FirstMI = MemOps[0].MI;
10060b57cec5SDimitry Andric unsigned Opcode = FirstMI->getOpcode();
10070b57cec5SDimitry Andric bool isNotVFP = isi32Load(Opcode) || isi32Store(Opcode);
10080b57cec5SDimitry Andric unsigned Size = getLSMultipleTransferSize(FirstMI);
10090b57cec5SDimitry Andric
10100b57cec5SDimitry Andric unsigned SIndex = 0;
10110b57cec5SDimitry Andric unsigned EIndex = MemOps.size();
10120b57cec5SDimitry Andric do {
10130b57cec5SDimitry Andric // Look at the first instruction.
10140b57cec5SDimitry Andric const MachineInstr *MI = MemOps[SIndex].MI;
10150b57cec5SDimitry Andric int Offset = MemOps[SIndex].Offset;
10160b57cec5SDimitry Andric const MachineOperand &PMO = getLoadStoreRegOp(*MI);
10178bcb0991SDimitry Andric Register PReg = PMO.getReg();
10180b57cec5SDimitry Andric unsigned PRegNum = PMO.isUndef() ? std::numeric_limits<unsigned>::max()
10190b57cec5SDimitry Andric : TRI->getEncodingValue(PReg);
10200b57cec5SDimitry Andric unsigned Latest = SIndex;
10210b57cec5SDimitry Andric unsigned Earliest = SIndex;
10220b57cec5SDimitry Andric unsigned Count = 1;
10230b57cec5SDimitry Andric bool CanMergeToLSDouble =
10240b57cec5SDimitry Andric STI->isThumb2() && isNotVFP && isValidLSDoubleOffset(Offset);
10250b57cec5SDimitry Andric // ARM errata 602117: LDRD with base in list may result in incorrect base
10260b57cec5SDimitry Andric // register when interrupted or faulted.
10270b57cec5SDimitry Andric if (STI->isCortexM3() && isi32Load(Opcode) &&
10280b57cec5SDimitry Andric PReg == getLoadStoreBaseOp(*MI).getReg())
10290b57cec5SDimitry Andric CanMergeToLSDouble = false;
10300b57cec5SDimitry Andric
10310b57cec5SDimitry Andric bool CanMergeToLSMulti = true;
10320b57cec5SDimitry Andric // On swift vldm/vstm starting with an odd register number as that needs
10330b57cec5SDimitry Andric // more uops than single vldrs.
10340b57cec5SDimitry Andric if (STI->hasSlowOddRegister() && !isNotVFP && (PRegNum % 2) == 1)
10350b57cec5SDimitry Andric CanMergeToLSMulti = false;
10360b57cec5SDimitry Andric
10370b57cec5SDimitry Andric // LDRD/STRD do not allow SP/PC. LDM/STM do not support it or have it
10380b57cec5SDimitry Andric // deprecated; LDM to PC is fine but cannot happen here.
10390b57cec5SDimitry Andric if (PReg == ARM::SP || PReg == ARM::PC)
10400b57cec5SDimitry Andric CanMergeToLSMulti = CanMergeToLSDouble = false;
10410b57cec5SDimitry Andric
10420b57cec5SDimitry Andric // Should we be conservative?
10430b57cec5SDimitry Andric if (AssumeMisalignedLoadStores && !mayCombineMisaligned(*STI, *MI))
10440b57cec5SDimitry Andric CanMergeToLSMulti = CanMergeToLSDouble = false;
10450b57cec5SDimitry Andric
10460b57cec5SDimitry Andric // vldm / vstm limit are 32 for S variants, 16 for D variants.
10470b57cec5SDimitry Andric unsigned Limit;
10480b57cec5SDimitry Andric switch (Opcode) {
10490b57cec5SDimitry Andric default:
10500b57cec5SDimitry Andric Limit = UINT_MAX;
10510b57cec5SDimitry Andric break;
10520b57cec5SDimitry Andric case ARM::VLDRD:
10530b57cec5SDimitry Andric case ARM::VSTRD:
10540b57cec5SDimitry Andric Limit = 16;
10550b57cec5SDimitry Andric break;
10560b57cec5SDimitry Andric }
10570b57cec5SDimitry Andric
10580b57cec5SDimitry Andric // Merge following instructions where possible.
10590b57cec5SDimitry Andric for (unsigned I = SIndex+1; I < EIndex; ++I, ++Count) {
10600b57cec5SDimitry Andric int NewOffset = MemOps[I].Offset;
10610b57cec5SDimitry Andric if (NewOffset != Offset + (int)Size)
10620b57cec5SDimitry Andric break;
10630b57cec5SDimitry Andric const MachineOperand &MO = getLoadStoreRegOp(*MemOps[I].MI);
10648bcb0991SDimitry Andric Register Reg = MO.getReg();
10650b57cec5SDimitry Andric if (Reg == ARM::SP || Reg == ARM::PC)
10660b57cec5SDimitry Andric break;
10670b57cec5SDimitry Andric if (Count == Limit)
10680b57cec5SDimitry Andric break;
10690b57cec5SDimitry Andric
10700b57cec5SDimitry Andric // See if the current load/store may be part of a multi load/store.
10710b57cec5SDimitry Andric unsigned RegNum = MO.isUndef() ? std::numeric_limits<unsigned>::max()
10720b57cec5SDimitry Andric : TRI->getEncodingValue(Reg);
10730b57cec5SDimitry Andric bool PartOfLSMulti = CanMergeToLSMulti;
10740b57cec5SDimitry Andric if (PartOfLSMulti) {
10750b57cec5SDimitry Andric // Register numbers must be in ascending order.
10760b57cec5SDimitry Andric if (RegNum <= PRegNum)
10770b57cec5SDimitry Andric PartOfLSMulti = false;
10780b57cec5SDimitry Andric // For VFP / NEON load/store multiples, the registers must be
10790b57cec5SDimitry Andric // consecutive and within the limit on the number of registers per
10800b57cec5SDimitry Andric // instruction.
10810b57cec5SDimitry Andric else if (!isNotVFP && RegNum != PRegNum+1)
10820b57cec5SDimitry Andric PartOfLSMulti = false;
10830b57cec5SDimitry Andric }
10840b57cec5SDimitry Andric // See if the current load/store may be part of a double load/store.
10850b57cec5SDimitry Andric bool PartOfLSDouble = CanMergeToLSDouble && Count <= 1;
10860b57cec5SDimitry Andric
10870b57cec5SDimitry Andric if (!PartOfLSMulti && !PartOfLSDouble)
10880b57cec5SDimitry Andric break;
10890b57cec5SDimitry Andric CanMergeToLSMulti &= PartOfLSMulti;
10900b57cec5SDimitry Andric CanMergeToLSDouble &= PartOfLSDouble;
10910b57cec5SDimitry Andric // Track MemOp with latest and earliest position (Positions are
10920b57cec5SDimitry Andric // counted in reverse).
10930b57cec5SDimitry Andric unsigned Position = MemOps[I].Position;
10940b57cec5SDimitry Andric if (Position < MemOps[Latest].Position)
10950b57cec5SDimitry Andric Latest = I;
10960b57cec5SDimitry Andric else if (Position > MemOps[Earliest].Position)
10970b57cec5SDimitry Andric Earliest = I;
10980b57cec5SDimitry Andric // Prepare for next MemOp.
10990b57cec5SDimitry Andric Offset += Size;
11000b57cec5SDimitry Andric PRegNum = RegNum;
11010b57cec5SDimitry Andric }
11020b57cec5SDimitry Andric
11030b57cec5SDimitry Andric // Form a candidate from the Ops collected so far.
11040b57cec5SDimitry Andric MergeCandidate *Candidate = new(Allocator.Allocate()) MergeCandidate;
11050b57cec5SDimitry Andric for (unsigned C = SIndex, CE = SIndex + Count; C < CE; ++C)
11060b57cec5SDimitry Andric Candidate->Instrs.push_back(MemOps[C].MI);
11070b57cec5SDimitry Andric Candidate->LatestMIIdx = Latest - SIndex;
11080b57cec5SDimitry Andric Candidate->EarliestMIIdx = Earliest - SIndex;
11090b57cec5SDimitry Andric Candidate->InsertPos = MemOps[Latest].Position;
11100b57cec5SDimitry Andric if (Count == 1)
11110b57cec5SDimitry Andric CanMergeToLSMulti = CanMergeToLSDouble = false;
11120b57cec5SDimitry Andric Candidate->CanMergeToLSMulti = CanMergeToLSMulti;
11130b57cec5SDimitry Andric Candidate->CanMergeToLSDouble = CanMergeToLSDouble;
11140b57cec5SDimitry Andric Candidates.push_back(Candidate);
11150b57cec5SDimitry Andric // Continue after the chain.
11160b57cec5SDimitry Andric SIndex += Count;
11170b57cec5SDimitry Andric } while (SIndex < EIndex);
11180b57cec5SDimitry Andric }
11190b57cec5SDimitry Andric
getUpdatingLSMultipleOpcode(unsigned Opc,ARM_AM::AMSubMode Mode)11200b57cec5SDimitry Andric static unsigned getUpdatingLSMultipleOpcode(unsigned Opc,
11210b57cec5SDimitry Andric ARM_AM::AMSubMode Mode) {
11220b57cec5SDimitry Andric switch (Opc) {
11230b57cec5SDimitry Andric default: llvm_unreachable("Unhandled opcode!");
11240b57cec5SDimitry Andric case ARM::LDMIA:
11250b57cec5SDimitry Andric case ARM::LDMDA:
11260b57cec5SDimitry Andric case ARM::LDMDB:
11270b57cec5SDimitry Andric case ARM::LDMIB:
11280b57cec5SDimitry Andric switch (Mode) {
11290b57cec5SDimitry Andric default: llvm_unreachable("Unhandled submode!");
11300b57cec5SDimitry Andric case ARM_AM::ia: return ARM::LDMIA_UPD;
11310b57cec5SDimitry Andric case ARM_AM::ib: return ARM::LDMIB_UPD;
11320b57cec5SDimitry Andric case ARM_AM::da: return ARM::LDMDA_UPD;
11330b57cec5SDimitry Andric case ARM_AM::db: return ARM::LDMDB_UPD;
11340b57cec5SDimitry Andric }
11350b57cec5SDimitry Andric case ARM::STMIA:
11360b57cec5SDimitry Andric case ARM::STMDA:
11370b57cec5SDimitry Andric case ARM::STMDB:
11380b57cec5SDimitry Andric case ARM::STMIB:
11390b57cec5SDimitry Andric switch (Mode) {
11400b57cec5SDimitry Andric default: llvm_unreachable("Unhandled submode!");
11410b57cec5SDimitry Andric case ARM_AM::ia: return ARM::STMIA_UPD;
11420b57cec5SDimitry Andric case ARM_AM::ib: return ARM::STMIB_UPD;
11430b57cec5SDimitry Andric case ARM_AM::da: return ARM::STMDA_UPD;
11440b57cec5SDimitry Andric case ARM_AM::db: return ARM::STMDB_UPD;
11450b57cec5SDimitry Andric }
11460b57cec5SDimitry Andric case ARM::t2LDMIA:
11470b57cec5SDimitry Andric case ARM::t2LDMDB:
11480b57cec5SDimitry Andric switch (Mode) {
11490b57cec5SDimitry Andric default: llvm_unreachable("Unhandled submode!");
11500b57cec5SDimitry Andric case ARM_AM::ia: return ARM::t2LDMIA_UPD;
11510b57cec5SDimitry Andric case ARM_AM::db: return ARM::t2LDMDB_UPD;
11520b57cec5SDimitry Andric }
11530b57cec5SDimitry Andric case ARM::t2STMIA:
11540b57cec5SDimitry Andric case ARM::t2STMDB:
11550b57cec5SDimitry Andric switch (Mode) {
11560b57cec5SDimitry Andric default: llvm_unreachable("Unhandled submode!");
11570b57cec5SDimitry Andric case ARM_AM::ia: return ARM::t2STMIA_UPD;
11580b57cec5SDimitry Andric case ARM_AM::db: return ARM::t2STMDB_UPD;
11590b57cec5SDimitry Andric }
11600b57cec5SDimitry Andric case ARM::VLDMSIA:
11610b57cec5SDimitry Andric switch (Mode) {
11620b57cec5SDimitry Andric default: llvm_unreachable("Unhandled submode!");
11630b57cec5SDimitry Andric case ARM_AM::ia: return ARM::VLDMSIA_UPD;
11640b57cec5SDimitry Andric case ARM_AM::db: return ARM::VLDMSDB_UPD;
11650b57cec5SDimitry Andric }
11660b57cec5SDimitry Andric case ARM::VLDMDIA:
11670b57cec5SDimitry Andric switch (Mode) {
11680b57cec5SDimitry Andric default: llvm_unreachable("Unhandled submode!");
11690b57cec5SDimitry Andric case ARM_AM::ia: return ARM::VLDMDIA_UPD;
11700b57cec5SDimitry Andric case ARM_AM::db: return ARM::VLDMDDB_UPD;
11710b57cec5SDimitry Andric }
11720b57cec5SDimitry Andric case ARM::VSTMSIA:
11730b57cec5SDimitry Andric switch (Mode) {
11740b57cec5SDimitry Andric default: llvm_unreachable("Unhandled submode!");
11750b57cec5SDimitry Andric case ARM_AM::ia: return ARM::VSTMSIA_UPD;
11760b57cec5SDimitry Andric case ARM_AM::db: return ARM::VSTMSDB_UPD;
11770b57cec5SDimitry Andric }
11780b57cec5SDimitry Andric case ARM::VSTMDIA:
11790b57cec5SDimitry Andric switch (Mode) {
11800b57cec5SDimitry Andric default: llvm_unreachable("Unhandled submode!");
11810b57cec5SDimitry Andric case ARM_AM::ia: return ARM::VSTMDIA_UPD;
11820b57cec5SDimitry Andric case ARM_AM::db: return ARM::VSTMDDB_UPD;
11830b57cec5SDimitry Andric }
11840b57cec5SDimitry Andric }
11850b57cec5SDimitry Andric }
11860b57cec5SDimitry Andric
11870b57cec5SDimitry Andric /// Check if the given instruction increments or decrements a register and
11880b57cec5SDimitry Andric /// return the amount it is incremented/decremented. Returns 0 if the CPSR flags
11890b57cec5SDimitry Andric /// generated by the instruction are possibly read as well.
isIncrementOrDecrement(const MachineInstr & MI,Register Reg,ARMCC::CondCodes Pred,Register PredReg)11905ffd83dbSDimitry Andric static int isIncrementOrDecrement(const MachineInstr &MI, Register Reg,
11915ffd83dbSDimitry Andric ARMCC::CondCodes Pred, Register PredReg) {
11920b57cec5SDimitry Andric bool CheckCPSRDef;
11930b57cec5SDimitry Andric int Scale;
11940b57cec5SDimitry Andric switch (MI.getOpcode()) {
11950b57cec5SDimitry Andric case ARM::tADDi8: Scale = 4; CheckCPSRDef = true; break;
11960b57cec5SDimitry Andric case ARM::tSUBi8: Scale = -4; CheckCPSRDef = true; break;
11970b57cec5SDimitry Andric case ARM::t2SUBri:
1198480093f4SDimitry Andric case ARM::t2SUBspImm:
11990b57cec5SDimitry Andric case ARM::SUBri: Scale = -1; CheckCPSRDef = true; break;
12000b57cec5SDimitry Andric case ARM::t2ADDri:
1201480093f4SDimitry Andric case ARM::t2ADDspImm:
12020b57cec5SDimitry Andric case ARM::ADDri: Scale = 1; CheckCPSRDef = true; break;
12030b57cec5SDimitry Andric case ARM::tADDspi: Scale = 4; CheckCPSRDef = false; break;
12040b57cec5SDimitry Andric case ARM::tSUBspi: Scale = -4; CheckCPSRDef = false; break;
12050b57cec5SDimitry Andric default: return 0;
12060b57cec5SDimitry Andric }
12070b57cec5SDimitry Andric
12085ffd83dbSDimitry Andric Register MIPredReg;
12090b57cec5SDimitry Andric if (MI.getOperand(0).getReg() != Reg ||
12100b57cec5SDimitry Andric MI.getOperand(1).getReg() != Reg ||
12110b57cec5SDimitry Andric getInstrPredicate(MI, MIPredReg) != Pred ||
12120b57cec5SDimitry Andric MIPredReg != PredReg)
12130b57cec5SDimitry Andric return 0;
12140b57cec5SDimitry Andric
12150b57cec5SDimitry Andric if (CheckCPSRDef && definesCPSR(MI))
12160b57cec5SDimitry Andric return 0;
12170b57cec5SDimitry Andric return MI.getOperand(2).getImm() * Scale;
12180b57cec5SDimitry Andric }
12190b57cec5SDimitry Andric
12200b57cec5SDimitry Andric /// Searches for an increment or decrement of \p Reg before \p MBBI.
12210b57cec5SDimitry Andric static MachineBasicBlock::iterator
findIncDecBefore(MachineBasicBlock::iterator MBBI,Register Reg,ARMCC::CondCodes Pred,Register PredReg,int & Offset)12225ffd83dbSDimitry Andric findIncDecBefore(MachineBasicBlock::iterator MBBI, Register Reg,
12235ffd83dbSDimitry Andric ARMCC::CondCodes Pred, Register PredReg, int &Offset) {
12240b57cec5SDimitry Andric Offset = 0;
12250b57cec5SDimitry Andric MachineBasicBlock &MBB = *MBBI->getParent();
12260b57cec5SDimitry Andric MachineBasicBlock::iterator BeginMBBI = MBB.begin();
12270b57cec5SDimitry Andric MachineBasicBlock::iterator EndMBBI = MBB.end();
12280b57cec5SDimitry Andric if (MBBI == BeginMBBI)
12290b57cec5SDimitry Andric return EndMBBI;
12300b57cec5SDimitry Andric
12310b57cec5SDimitry Andric // Skip debug values.
12320b57cec5SDimitry Andric MachineBasicBlock::iterator PrevMBBI = std::prev(MBBI);
12330b57cec5SDimitry Andric while (PrevMBBI->isDebugInstr() && PrevMBBI != BeginMBBI)
12340b57cec5SDimitry Andric --PrevMBBI;
12350b57cec5SDimitry Andric
12360b57cec5SDimitry Andric Offset = isIncrementOrDecrement(*PrevMBBI, Reg, Pred, PredReg);
12370b57cec5SDimitry Andric return Offset == 0 ? EndMBBI : PrevMBBI;
12380b57cec5SDimitry Andric }
12390b57cec5SDimitry Andric
12400b57cec5SDimitry Andric /// Searches for a increment or decrement of \p Reg after \p MBBI.
12410b57cec5SDimitry Andric static MachineBasicBlock::iterator
findIncDecAfter(MachineBasicBlock::iterator MBBI,Register Reg,ARMCC::CondCodes Pred,Register PredReg,int & Offset,const TargetRegisterInfo * TRI)12425ffd83dbSDimitry Andric findIncDecAfter(MachineBasicBlock::iterator MBBI, Register Reg,
1243fe6060f1SDimitry Andric ARMCC::CondCodes Pred, Register PredReg, int &Offset,
1244fe6060f1SDimitry Andric const TargetRegisterInfo *TRI) {
12450b57cec5SDimitry Andric Offset = 0;
12460b57cec5SDimitry Andric MachineBasicBlock &MBB = *MBBI->getParent();
12470b57cec5SDimitry Andric MachineBasicBlock::iterator EndMBBI = MBB.end();
12480b57cec5SDimitry Andric MachineBasicBlock::iterator NextMBBI = std::next(MBBI);
1249fe6060f1SDimitry Andric while (NextMBBI != EndMBBI) {
12500b57cec5SDimitry Andric // Skip debug values.
12510b57cec5SDimitry Andric while (NextMBBI != EndMBBI && NextMBBI->isDebugInstr())
12520b57cec5SDimitry Andric ++NextMBBI;
12530b57cec5SDimitry Andric if (NextMBBI == EndMBBI)
12540b57cec5SDimitry Andric return EndMBBI;
12550b57cec5SDimitry Andric
1256fe6060f1SDimitry Andric unsigned Off = isIncrementOrDecrement(*NextMBBI, Reg, Pred, PredReg);
1257fe6060f1SDimitry Andric if (Off) {
1258fe6060f1SDimitry Andric Offset = Off;
1259fe6060f1SDimitry Andric return NextMBBI;
1260fe6060f1SDimitry Andric }
1261fe6060f1SDimitry Andric
1262fe6060f1SDimitry Andric // SP can only be combined if it is the next instruction after the original
1263fe6060f1SDimitry Andric // MBBI, otherwise we may be incrementing the stack pointer (invalidating
1264fe6060f1SDimitry Andric // anything below the new pointer) when its frame elements are still in
1265fe6060f1SDimitry Andric // use. Other registers can attempt to look further, until a different use
1266fe6060f1SDimitry Andric // or def of the register is found.
1267fe6060f1SDimitry Andric if (Reg == ARM::SP || NextMBBI->readsRegister(Reg, TRI) ||
1268fe6060f1SDimitry Andric NextMBBI->definesRegister(Reg, TRI))
1269fe6060f1SDimitry Andric return EndMBBI;
1270fe6060f1SDimitry Andric
1271fe6060f1SDimitry Andric ++NextMBBI;
1272fe6060f1SDimitry Andric }
1273fe6060f1SDimitry Andric return EndMBBI;
12740b57cec5SDimitry Andric }
12750b57cec5SDimitry Andric
12760b57cec5SDimitry Andric /// Fold proceeding/trailing inc/dec of base register into the
12770b57cec5SDimitry Andric /// LDM/STM/VLDM{D|S}/VSTM{D|S} op when possible:
12780b57cec5SDimitry Andric ///
12790b57cec5SDimitry Andric /// stmia rn, <ra, rb, rc>
12800b57cec5SDimitry Andric /// rn := rn + 4 * 3;
12810b57cec5SDimitry Andric /// =>
12820b57cec5SDimitry Andric /// stmia rn!, <ra, rb, rc>
12830b57cec5SDimitry Andric ///
12840b57cec5SDimitry Andric /// rn := rn - 4 * 3;
12850b57cec5SDimitry Andric /// ldmia rn, <ra, rb, rc>
12860b57cec5SDimitry Andric /// =>
12870b57cec5SDimitry Andric /// ldmdb rn!, <ra, rb, rc>
MergeBaseUpdateLSMultiple(MachineInstr * MI)12880b57cec5SDimitry Andric bool ARMLoadStoreOpt::MergeBaseUpdateLSMultiple(MachineInstr *MI) {
12890b57cec5SDimitry Andric // Thumb1 is already using updating loads/stores.
12900b57cec5SDimitry Andric if (isThumb1) return false;
1291e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "Attempting to merge update of: " << *MI);
12920b57cec5SDimitry Andric
12930b57cec5SDimitry Andric const MachineOperand &BaseOP = MI->getOperand(0);
12948bcb0991SDimitry Andric Register Base = BaseOP.getReg();
12950b57cec5SDimitry Andric bool BaseKill = BaseOP.isKill();
12965ffd83dbSDimitry Andric Register PredReg;
12970b57cec5SDimitry Andric ARMCC::CondCodes Pred = getInstrPredicate(*MI, PredReg);
12980b57cec5SDimitry Andric unsigned Opcode = MI->getOpcode();
12990b57cec5SDimitry Andric DebugLoc DL = MI->getDebugLoc();
13000b57cec5SDimitry Andric
13010b57cec5SDimitry Andric // Can't use an updating ld/st if the base register is also a dest
13020b57cec5SDimitry Andric // register. e.g. ldmdb r0!, {r0, r1, r2}. The behavior is undefined.
13034824e7fdSDimitry Andric for (const MachineOperand &MO : llvm::drop_begin(MI->operands(), 2))
13044824e7fdSDimitry Andric if (MO.getReg() == Base)
13050b57cec5SDimitry Andric return false;
13060b57cec5SDimitry Andric
13070b57cec5SDimitry Andric int Bytes = getLSMultipleTransferSize(MI);
13080b57cec5SDimitry Andric MachineBasicBlock &MBB = *MI->getParent();
13090b57cec5SDimitry Andric MachineBasicBlock::iterator MBBI(MI);
13100b57cec5SDimitry Andric int Offset;
13110b57cec5SDimitry Andric MachineBasicBlock::iterator MergeInstr
13120b57cec5SDimitry Andric = findIncDecBefore(MBBI, Base, Pred, PredReg, Offset);
13130b57cec5SDimitry Andric ARM_AM::AMSubMode Mode = getLoadStoreMultipleSubMode(Opcode);
13140b57cec5SDimitry Andric if (Mode == ARM_AM::ia && Offset == -Bytes) {
13150b57cec5SDimitry Andric Mode = ARM_AM::db;
13160b57cec5SDimitry Andric } else if (Mode == ARM_AM::ib && Offset == -Bytes) {
13170b57cec5SDimitry Andric Mode = ARM_AM::da;
13180b57cec5SDimitry Andric } else {
1319fe6060f1SDimitry Andric MergeInstr = findIncDecAfter(MBBI, Base, Pred, PredReg, Offset, TRI);
13200b57cec5SDimitry Andric if (((Mode != ARM_AM::ia && Mode != ARM_AM::ib) || Offset != Bytes) &&
13210b57cec5SDimitry Andric ((Mode != ARM_AM::da && Mode != ARM_AM::db) || Offset != -Bytes)) {
13220b57cec5SDimitry Andric
13230b57cec5SDimitry Andric // We couldn't find an inc/dec to merge. But if the base is dead, we
13240b57cec5SDimitry Andric // can still change to a writeback form as that will save us 2 bytes
13250b57cec5SDimitry Andric // of code size. It can create WAW hazards though, so only do it if
13260b57cec5SDimitry Andric // we're minimizing code size.
13270b57cec5SDimitry Andric if (!STI->hasMinSize() || !BaseKill)
13280b57cec5SDimitry Andric return false;
13290b57cec5SDimitry Andric
13300b57cec5SDimitry Andric bool HighRegsUsed = false;
13314824e7fdSDimitry Andric for (const MachineOperand &MO : llvm::drop_begin(MI->operands(), 2))
13324824e7fdSDimitry Andric if (MO.getReg() >= ARM::R8) {
13330b57cec5SDimitry Andric HighRegsUsed = true;
13340b57cec5SDimitry Andric break;
13350b57cec5SDimitry Andric }
13360b57cec5SDimitry Andric
13370b57cec5SDimitry Andric if (!HighRegsUsed)
13380b57cec5SDimitry Andric MergeInstr = MBB.end();
13390b57cec5SDimitry Andric else
13400b57cec5SDimitry Andric return false;
13410b57cec5SDimitry Andric }
13420b57cec5SDimitry Andric }
1343e8d8bef9SDimitry Andric if (MergeInstr != MBB.end()) {
1344e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << " Erasing old increment: " << *MergeInstr);
13450b57cec5SDimitry Andric MBB.erase(MergeInstr);
1346e8d8bef9SDimitry Andric }
13470b57cec5SDimitry Andric
13480b57cec5SDimitry Andric unsigned NewOpc = getUpdatingLSMultipleOpcode(Opcode, Mode);
13490b57cec5SDimitry Andric MachineInstrBuilder MIB = BuildMI(MBB, MBBI, DL, TII->get(NewOpc))
13500b57cec5SDimitry Andric .addReg(Base, getDefRegState(true)) // WB base register
13510b57cec5SDimitry Andric .addReg(Base, getKillRegState(BaseKill))
13520b57cec5SDimitry Andric .addImm(Pred).addReg(PredReg);
13530b57cec5SDimitry Andric
13540b57cec5SDimitry Andric // Transfer the rest of operands.
13554824e7fdSDimitry Andric for (const MachineOperand &MO : llvm::drop_begin(MI->operands(), 3))
13564824e7fdSDimitry Andric MIB.add(MO);
13570b57cec5SDimitry Andric
13580b57cec5SDimitry Andric // Transfer memoperands.
13590b57cec5SDimitry Andric MIB.setMemRefs(MI->memoperands());
13600b57cec5SDimitry Andric
1361e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << " Added new load/store: " << *MIB);
13620b57cec5SDimitry Andric MBB.erase(MBBI);
13630b57cec5SDimitry Andric return true;
13640b57cec5SDimitry Andric }
13650b57cec5SDimitry Andric
getPreIndexedLoadStoreOpcode(unsigned Opc,ARM_AM::AddrOpc Mode)13660b57cec5SDimitry Andric static unsigned getPreIndexedLoadStoreOpcode(unsigned Opc,
13670b57cec5SDimitry Andric ARM_AM::AddrOpc Mode) {
13680b57cec5SDimitry Andric switch (Opc) {
13690b57cec5SDimitry Andric case ARM::LDRi12:
13700b57cec5SDimitry Andric return ARM::LDR_PRE_IMM;
13710b57cec5SDimitry Andric case ARM::STRi12:
13720b57cec5SDimitry Andric return ARM::STR_PRE_IMM;
13730b57cec5SDimitry Andric case ARM::VLDRS:
13740b57cec5SDimitry Andric return Mode == ARM_AM::add ? ARM::VLDMSIA_UPD : ARM::VLDMSDB_UPD;
13750b57cec5SDimitry Andric case ARM::VLDRD:
13760b57cec5SDimitry Andric return Mode == ARM_AM::add ? ARM::VLDMDIA_UPD : ARM::VLDMDDB_UPD;
13770b57cec5SDimitry Andric case ARM::VSTRS:
13780b57cec5SDimitry Andric return Mode == ARM_AM::add ? ARM::VSTMSIA_UPD : ARM::VSTMSDB_UPD;
13790b57cec5SDimitry Andric case ARM::VSTRD:
13800b57cec5SDimitry Andric return Mode == ARM_AM::add ? ARM::VSTMDIA_UPD : ARM::VSTMDDB_UPD;
13810b57cec5SDimitry Andric case ARM::t2LDRi8:
13820b57cec5SDimitry Andric case ARM::t2LDRi12:
13830b57cec5SDimitry Andric return ARM::t2LDR_PRE;
13840b57cec5SDimitry Andric case ARM::t2STRi8:
13850b57cec5SDimitry Andric case ARM::t2STRi12:
13860b57cec5SDimitry Andric return ARM::t2STR_PRE;
13870b57cec5SDimitry Andric default: llvm_unreachable("Unhandled opcode!");
13880b57cec5SDimitry Andric }
13890b57cec5SDimitry Andric }
13900b57cec5SDimitry Andric
getPostIndexedLoadStoreOpcode(unsigned Opc,ARM_AM::AddrOpc Mode)13910b57cec5SDimitry Andric static unsigned getPostIndexedLoadStoreOpcode(unsigned Opc,
13920b57cec5SDimitry Andric ARM_AM::AddrOpc Mode) {
13930b57cec5SDimitry Andric switch (Opc) {
13940b57cec5SDimitry Andric case ARM::LDRi12:
13950b57cec5SDimitry Andric return ARM::LDR_POST_IMM;
13960b57cec5SDimitry Andric case ARM::STRi12:
13970b57cec5SDimitry Andric return ARM::STR_POST_IMM;
13980b57cec5SDimitry Andric case ARM::VLDRS:
13990b57cec5SDimitry Andric return Mode == ARM_AM::add ? ARM::VLDMSIA_UPD : ARM::VLDMSDB_UPD;
14000b57cec5SDimitry Andric case ARM::VLDRD:
14010b57cec5SDimitry Andric return Mode == ARM_AM::add ? ARM::VLDMDIA_UPD : ARM::VLDMDDB_UPD;
14020b57cec5SDimitry Andric case ARM::VSTRS:
14030b57cec5SDimitry Andric return Mode == ARM_AM::add ? ARM::VSTMSIA_UPD : ARM::VSTMSDB_UPD;
14040b57cec5SDimitry Andric case ARM::VSTRD:
14050b57cec5SDimitry Andric return Mode == ARM_AM::add ? ARM::VSTMDIA_UPD : ARM::VSTMDDB_UPD;
14060b57cec5SDimitry Andric case ARM::t2LDRi8:
14070b57cec5SDimitry Andric case ARM::t2LDRi12:
14080b57cec5SDimitry Andric return ARM::t2LDR_POST;
1409e8d8bef9SDimitry Andric case ARM::t2LDRBi8:
1410e8d8bef9SDimitry Andric case ARM::t2LDRBi12:
1411e8d8bef9SDimitry Andric return ARM::t2LDRB_POST;
1412e8d8bef9SDimitry Andric case ARM::t2LDRSBi8:
1413e8d8bef9SDimitry Andric case ARM::t2LDRSBi12:
1414e8d8bef9SDimitry Andric return ARM::t2LDRSB_POST;
1415e8d8bef9SDimitry Andric case ARM::t2LDRHi8:
1416e8d8bef9SDimitry Andric case ARM::t2LDRHi12:
1417e8d8bef9SDimitry Andric return ARM::t2LDRH_POST;
1418e8d8bef9SDimitry Andric case ARM::t2LDRSHi8:
1419e8d8bef9SDimitry Andric case ARM::t2LDRSHi12:
1420e8d8bef9SDimitry Andric return ARM::t2LDRSH_POST;
14210b57cec5SDimitry Andric case ARM::t2STRi8:
14220b57cec5SDimitry Andric case ARM::t2STRi12:
14230b57cec5SDimitry Andric return ARM::t2STR_POST;
1424e8d8bef9SDimitry Andric case ARM::t2STRBi8:
1425e8d8bef9SDimitry Andric case ARM::t2STRBi12:
1426e8d8bef9SDimitry Andric return ARM::t2STRB_POST;
1427e8d8bef9SDimitry Andric case ARM::t2STRHi8:
1428e8d8bef9SDimitry Andric case ARM::t2STRHi12:
1429e8d8bef9SDimitry Andric return ARM::t2STRH_POST;
14305ffd83dbSDimitry Andric
14315ffd83dbSDimitry Andric case ARM::MVE_VLDRBS16:
14325ffd83dbSDimitry Andric return ARM::MVE_VLDRBS16_post;
14335ffd83dbSDimitry Andric case ARM::MVE_VLDRBS32:
14345ffd83dbSDimitry Andric return ARM::MVE_VLDRBS32_post;
14355ffd83dbSDimitry Andric case ARM::MVE_VLDRBU16:
14365ffd83dbSDimitry Andric return ARM::MVE_VLDRBU16_post;
14375ffd83dbSDimitry Andric case ARM::MVE_VLDRBU32:
14385ffd83dbSDimitry Andric return ARM::MVE_VLDRBU32_post;
14395ffd83dbSDimitry Andric case ARM::MVE_VLDRHS32:
14405ffd83dbSDimitry Andric return ARM::MVE_VLDRHS32_post;
14415ffd83dbSDimitry Andric case ARM::MVE_VLDRHU32:
14425ffd83dbSDimitry Andric return ARM::MVE_VLDRHU32_post;
14435ffd83dbSDimitry Andric case ARM::MVE_VLDRBU8:
14445ffd83dbSDimitry Andric return ARM::MVE_VLDRBU8_post;
14455ffd83dbSDimitry Andric case ARM::MVE_VLDRHU16:
14465ffd83dbSDimitry Andric return ARM::MVE_VLDRHU16_post;
14475ffd83dbSDimitry Andric case ARM::MVE_VLDRWU32:
14485ffd83dbSDimitry Andric return ARM::MVE_VLDRWU32_post;
14495ffd83dbSDimitry Andric case ARM::MVE_VSTRB16:
14505ffd83dbSDimitry Andric return ARM::MVE_VSTRB16_post;
14515ffd83dbSDimitry Andric case ARM::MVE_VSTRB32:
14525ffd83dbSDimitry Andric return ARM::MVE_VSTRB32_post;
14535ffd83dbSDimitry Andric case ARM::MVE_VSTRH32:
14545ffd83dbSDimitry Andric return ARM::MVE_VSTRH32_post;
14555ffd83dbSDimitry Andric case ARM::MVE_VSTRBU8:
14565ffd83dbSDimitry Andric return ARM::MVE_VSTRBU8_post;
14575ffd83dbSDimitry Andric case ARM::MVE_VSTRHU16:
14585ffd83dbSDimitry Andric return ARM::MVE_VSTRHU16_post;
14595ffd83dbSDimitry Andric case ARM::MVE_VSTRWU32:
14605ffd83dbSDimitry Andric return ARM::MVE_VSTRWU32_post;
14615ffd83dbSDimitry Andric
14620b57cec5SDimitry Andric default: llvm_unreachable("Unhandled opcode!");
14630b57cec5SDimitry Andric }
14640b57cec5SDimitry Andric }
14650b57cec5SDimitry Andric
14660b57cec5SDimitry Andric /// Fold proceeding/trailing inc/dec of base register into the
14670b57cec5SDimitry Andric /// LDR/STR/FLD{D|S}/FST{D|S} op when possible:
MergeBaseUpdateLoadStore(MachineInstr * MI)14680b57cec5SDimitry Andric bool ARMLoadStoreOpt::MergeBaseUpdateLoadStore(MachineInstr *MI) {
14690b57cec5SDimitry Andric // Thumb1 doesn't have updating LDR/STR.
14700b57cec5SDimitry Andric // FIXME: Use LDM/STM with single register instead.
14710b57cec5SDimitry Andric if (isThumb1) return false;
1472e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "Attempting to merge update of: " << *MI);
14730b57cec5SDimitry Andric
14748bcb0991SDimitry Andric Register Base = getLoadStoreBaseOp(*MI).getReg();
14750b57cec5SDimitry Andric bool BaseKill = getLoadStoreBaseOp(*MI).isKill();
14760b57cec5SDimitry Andric unsigned Opcode = MI->getOpcode();
14770b57cec5SDimitry Andric DebugLoc DL = MI->getDebugLoc();
14780b57cec5SDimitry Andric bool isAM5 = (Opcode == ARM::VLDRD || Opcode == ARM::VLDRS ||
14790b57cec5SDimitry Andric Opcode == ARM::VSTRD || Opcode == ARM::VSTRS);
14800b57cec5SDimitry Andric bool isAM2 = (Opcode == ARM::LDRi12 || Opcode == ARM::STRi12);
14810b57cec5SDimitry Andric if (isi32Load(Opcode) || isi32Store(Opcode))
14820b57cec5SDimitry Andric if (MI->getOperand(2).getImm() != 0)
14830b57cec5SDimitry Andric return false;
14840b57cec5SDimitry Andric if (isAM5 && ARM_AM::getAM5Offset(MI->getOperand(2).getImm()) != 0)
14850b57cec5SDimitry Andric return false;
14860b57cec5SDimitry Andric
14870b57cec5SDimitry Andric // Can't do the merge if the destination register is the same as the would-be
14880b57cec5SDimitry Andric // writeback register.
14890b57cec5SDimitry Andric if (MI->getOperand(0).getReg() == Base)
14900b57cec5SDimitry Andric return false;
14910b57cec5SDimitry Andric
14925ffd83dbSDimitry Andric Register PredReg;
14930b57cec5SDimitry Andric ARMCC::CondCodes Pred = getInstrPredicate(*MI, PredReg);
14940b57cec5SDimitry Andric int Bytes = getLSMultipleTransferSize(MI);
14950b57cec5SDimitry Andric MachineBasicBlock &MBB = *MI->getParent();
14960b57cec5SDimitry Andric MachineBasicBlock::iterator MBBI(MI);
14970b57cec5SDimitry Andric int Offset;
14980b57cec5SDimitry Andric MachineBasicBlock::iterator MergeInstr
14990b57cec5SDimitry Andric = findIncDecBefore(MBBI, Base, Pred, PredReg, Offset);
15000b57cec5SDimitry Andric unsigned NewOpc;
15010b57cec5SDimitry Andric if (!isAM5 && Offset == Bytes) {
15020b57cec5SDimitry Andric NewOpc = getPreIndexedLoadStoreOpcode(Opcode, ARM_AM::add);
15030b57cec5SDimitry Andric } else if (Offset == -Bytes) {
15040b57cec5SDimitry Andric NewOpc = getPreIndexedLoadStoreOpcode(Opcode, ARM_AM::sub);
15050b57cec5SDimitry Andric } else {
1506fe6060f1SDimitry Andric MergeInstr = findIncDecAfter(MBBI, Base, Pred, PredReg, Offset, TRI);
1507fe6060f1SDimitry Andric if (MergeInstr == MBB.end())
15080b57cec5SDimitry Andric return false;
1509fe6060f1SDimitry Andric
1510fe6060f1SDimitry Andric NewOpc = getPostIndexedLoadStoreOpcode(Opcode, ARM_AM::add);
1511fe6060f1SDimitry Andric if ((isAM5 && Offset != Bytes) ||
1512fe6060f1SDimitry Andric (!isAM5 && !isLegalAddressImm(NewOpc, Offset, TII))) {
1513fe6060f1SDimitry Andric NewOpc = getPostIndexedLoadStoreOpcode(Opcode, ARM_AM::sub);
1514fe6060f1SDimitry Andric if (isAM5 || !isLegalAddressImm(NewOpc, Offset, TII))
1515fe6060f1SDimitry Andric return false;
1516fe6060f1SDimitry Andric }
15170b57cec5SDimitry Andric }
1518e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << " Erasing old increment: " << *MergeInstr);
15190b57cec5SDimitry Andric MBB.erase(MergeInstr);
15200b57cec5SDimitry Andric
15210b57cec5SDimitry Andric ARM_AM::AddrOpc AddSub = Offset < 0 ? ARM_AM::sub : ARM_AM::add;
15220b57cec5SDimitry Andric
15230b57cec5SDimitry Andric bool isLd = isLoadSingle(Opcode);
15240b57cec5SDimitry Andric if (isAM5) {
15250b57cec5SDimitry Andric // VLDM[SD]_UPD, VSTM[SD]_UPD
15260b57cec5SDimitry Andric // (There are no base-updating versions of VLDR/VSTR instructions, but the
15270b57cec5SDimitry Andric // updating load/store-multiple instructions can be used with only one
15280b57cec5SDimitry Andric // register.)
15290b57cec5SDimitry Andric MachineOperand &MO = MI->getOperand(0);
1530e8d8bef9SDimitry Andric auto MIB = BuildMI(MBB, MBBI, DL, TII->get(NewOpc))
15310b57cec5SDimitry Andric .addReg(Base, getDefRegState(true)) // WB base register
15320b57cec5SDimitry Andric .addReg(Base, getKillRegState(isLd ? BaseKill : false))
1533e8d8bef9SDimitry Andric .addImm(Pred)
1534e8d8bef9SDimitry Andric .addReg(PredReg)
1535e8d8bef9SDimitry Andric .addReg(MO.getReg(), (isLd ? getDefRegState(true)
1536e8d8bef9SDimitry Andric : getKillRegState(MO.isKill())))
15370b57cec5SDimitry Andric .cloneMemRefs(*MI);
1538e8d8bef9SDimitry Andric (void)MIB;
1539e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << " Added new instruction: " << *MIB);
15400b57cec5SDimitry Andric } else if (isLd) {
15410b57cec5SDimitry Andric if (isAM2) {
15420b57cec5SDimitry Andric // LDR_PRE, LDR_POST
15430b57cec5SDimitry Andric if (NewOpc == ARM::LDR_PRE_IMM || NewOpc == ARM::LDRB_PRE_IMM) {
1544e8d8bef9SDimitry Andric auto MIB =
15450b57cec5SDimitry Andric BuildMI(MBB, MBBI, DL, TII->get(NewOpc), MI->getOperand(0).getReg())
15460b57cec5SDimitry Andric .addReg(Base, RegState::Define)
1547e8d8bef9SDimitry Andric .addReg(Base)
1548e8d8bef9SDimitry Andric .addImm(Offset)
1549e8d8bef9SDimitry Andric .addImm(Pred)
1550e8d8bef9SDimitry Andric .addReg(PredReg)
15510b57cec5SDimitry Andric .cloneMemRefs(*MI);
1552e8d8bef9SDimitry Andric (void)MIB;
1553e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << " Added new instruction: " << *MIB);
15540b57cec5SDimitry Andric } else {
1555fe6060f1SDimitry Andric int Imm = ARM_AM::getAM2Opc(AddSub, abs(Offset), ARM_AM::no_shift);
1556e8d8bef9SDimitry Andric auto MIB =
15570b57cec5SDimitry Andric BuildMI(MBB, MBBI, DL, TII->get(NewOpc), MI->getOperand(0).getReg())
15580b57cec5SDimitry Andric .addReg(Base, RegState::Define)
15590b57cec5SDimitry Andric .addReg(Base)
15600b57cec5SDimitry Andric .addReg(0)
15610b57cec5SDimitry Andric .addImm(Imm)
15620b57cec5SDimitry Andric .add(predOps(Pred, PredReg))
15630b57cec5SDimitry Andric .cloneMemRefs(*MI);
1564e8d8bef9SDimitry Andric (void)MIB;
1565e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << " Added new instruction: " << *MIB);
15660b57cec5SDimitry Andric }
15670b57cec5SDimitry Andric } else {
15680b57cec5SDimitry Andric // t2LDR_PRE, t2LDR_POST
1569e8d8bef9SDimitry Andric auto MIB =
15700b57cec5SDimitry Andric BuildMI(MBB, MBBI, DL, TII->get(NewOpc), MI->getOperand(0).getReg())
15710b57cec5SDimitry Andric .addReg(Base, RegState::Define)
15720b57cec5SDimitry Andric .addReg(Base)
15730b57cec5SDimitry Andric .addImm(Offset)
15740b57cec5SDimitry Andric .add(predOps(Pred, PredReg))
15750b57cec5SDimitry Andric .cloneMemRefs(*MI);
1576e8d8bef9SDimitry Andric (void)MIB;
1577e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << " Added new instruction: " << *MIB);
15780b57cec5SDimitry Andric }
15790b57cec5SDimitry Andric } else {
15800b57cec5SDimitry Andric MachineOperand &MO = MI->getOperand(0);
15810b57cec5SDimitry Andric // FIXME: post-indexed stores use am2offset_imm, which still encodes
15820b57cec5SDimitry Andric // the vestigal zero-reg offset register. When that's fixed, this clause
15830b57cec5SDimitry Andric // can be removed entirely.
15840b57cec5SDimitry Andric if (isAM2 && NewOpc == ARM::STR_POST_IMM) {
1585fe6060f1SDimitry Andric int Imm = ARM_AM::getAM2Opc(AddSub, abs(Offset), ARM_AM::no_shift);
15860b57cec5SDimitry Andric // STR_PRE, STR_POST
1587e8d8bef9SDimitry Andric auto MIB = BuildMI(MBB, MBBI, DL, TII->get(NewOpc), Base)
15880b57cec5SDimitry Andric .addReg(MO.getReg(), getKillRegState(MO.isKill()))
15890b57cec5SDimitry Andric .addReg(Base)
15900b57cec5SDimitry Andric .addReg(0)
15910b57cec5SDimitry Andric .addImm(Imm)
15920b57cec5SDimitry Andric .add(predOps(Pred, PredReg))
15930b57cec5SDimitry Andric .cloneMemRefs(*MI);
1594e8d8bef9SDimitry Andric (void)MIB;
1595e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << " Added new instruction: " << *MIB);
15960b57cec5SDimitry Andric } else {
15970b57cec5SDimitry Andric // t2STR_PRE, t2STR_POST
1598e8d8bef9SDimitry Andric auto MIB = BuildMI(MBB, MBBI, DL, TII->get(NewOpc), Base)
15990b57cec5SDimitry Andric .addReg(MO.getReg(), getKillRegState(MO.isKill()))
16000b57cec5SDimitry Andric .addReg(Base)
16010b57cec5SDimitry Andric .addImm(Offset)
16020b57cec5SDimitry Andric .add(predOps(Pred, PredReg))
16030b57cec5SDimitry Andric .cloneMemRefs(*MI);
1604e8d8bef9SDimitry Andric (void)MIB;
1605e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << " Added new instruction: " << *MIB);
16060b57cec5SDimitry Andric }
16070b57cec5SDimitry Andric }
16080b57cec5SDimitry Andric MBB.erase(MBBI);
16090b57cec5SDimitry Andric
16100b57cec5SDimitry Andric return true;
16110b57cec5SDimitry Andric }
16120b57cec5SDimitry Andric
MergeBaseUpdateLSDouble(MachineInstr & MI) const16130b57cec5SDimitry Andric bool ARMLoadStoreOpt::MergeBaseUpdateLSDouble(MachineInstr &MI) const {
16140b57cec5SDimitry Andric unsigned Opcode = MI.getOpcode();
16150b57cec5SDimitry Andric assert((Opcode == ARM::t2LDRDi8 || Opcode == ARM::t2STRDi8) &&
16160b57cec5SDimitry Andric "Must have t2STRDi8 or t2LDRDi8");
16170b57cec5SDimitry Andric if (MI.getOperand(3).getImm() != 0)
16180b57cec5SDimitry Andric return false;
1619e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "Attempting to merge update of: " << MI);
16200b57cec5SDimitry Andric
16210b57cec5SDimitry Andric // Behaviour for writeback is undefined if base register is the same as one
16220b57cec5SDimitry Andric // of the others.
16230b57cec5SDimitry Andric const MachineOperand &BaseOp = MI.getOperand(2);
16248bcb0991SDimitry Andric Register Base = BaseOp.getReg();
16250b57cec5SDimitry Andric const MachineOperand &Reg0Op = MI.getOperand(0);
16260b57cec5SDimitry Andric const MachineOperand &Reg1Op = MI.getOperand(1);
16270b57cec5SDimitry Andric if (Reg0Op.getReg() == Base || Reg1Op.getReg() == Base)
16280b57cec5SDimitry Andric return false;
16290b57cec5SDimitry Andric
16305ffd83dbSDimitry Andric Register PredReg;
16310b57cec5SDimitry Andric ARMCC::CondCodes Pred = getInstrPredicate(MI, PredReg);
16320b57cec5SDimitry Andric MachineBasicBlock::iterator MBBI(MI);
16330b57cec5SDimitry Andric MachineBasicBlock &MBB = *MI.getParent();
16340b57cec5SDimitry Andric int Offset;
16350b57cec5SDimitry Andric MachineBasicBlock::iterator MergeInstr = findIncDecBefore(MBBI, Base, Pred,
16360b57cec5SDimitry Andric PredReg, Offset);
16370b57cec5SDimitry Andric unsigned NewOpc;
16380b57cec5SDimitry Andric if (Offset == 8 || Offset == -8) {
16390b57cec5SDimitry Andric NewOpc = Opcode == ARM::t2LDRDi8 ? ARM::t2LDRD_PRE : ARM::t2STRD_PRE;
16400b57cec5SDimitry Andric } else {
1641fe6060f1SDimitry Andric MergeInstr = findIncDecAfter(MBBI, Base, Pred, PredReg, Offset, TRI);
1642fe6060f1SDimitry Andric if (MergeInstr == MBB.end())
1643fe6060f1SDimitry Andric return false;
16440b57cec5SDimitry Andric NewOpc = Opcode == ARM::t2LDRDi8 ? ARM::t2LDRD_POST : ARM::t2STRD_POST;
1645fe6060f1SDimitry Andric if (!isLegalAddressImm(NewOpc, Offset, TII))
16460b57cec5SDimitry Andric return false;
16470b57cec5SDimitry Andric }
1648e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << " Erasing old increment: " << *MergeInstr);
16490b57cec5SDimitry Andric MBB.erase(MergeInstr);
16500b57cec5SDimitry Andric
16510b57cec5SDimitry Andric DebugLoc DL = MI.getDebugLoc();
16520b57cec5SDimitry Andric MachineInstrBuilder MIB = BuildMI(MBB, MBBI, DL, TII->get(NewOpc));
16530b57cec5SDimitry Andric if (NewOpc == ARM::t2LDRD_PRE || NewOpc == ARM::t2LDRD_POST) {
16540b57cec5SDimitry Andric MIB.add(Reg0Op).add(Reg1Op).addReg(BaseOp.getReg(), RegState::Define);
16550b57cec5SDimitry Andric } else {
16560b57cec5SDimitry Andric assert(NewOpc == ARM::t2STRD_PRE || NewOpc == ARM::t2STRD_POST);
16570b57cec5SDimitry Andric MIB.addReg(BaseOp.getReg(), RegState::Define).add(Reg0Op).add(Reg1Op);
16580b57cec5SDimitry Andric }
16590b57cec5SDimitry Andric MIB.addReg(BaseOp.getReg(), RegState::Kill)
16600b57cec5SDimitry Andric .addImm(Offset).addImm(Pred).addReg(PredReg);
16610b57cec5SDimitry Andric assert(TII->get(Opcode).getNumOperands() == 6 &&
16620b57cec5SDimitry Andric TII->get(NewOpc).getNumOperands() == 7 &&
16630b57cec5SDimitry Andric "Unexpected number of operands in Opcode specification.");
16640b57cec5SDimitry Andric
16650b57cec5SDimitry Andric // Transfer implicit operands.
16660b57cec5SDimitry Andric for (const MachineOperand &MO : MI.implicit_operands())
16670b57cec5SDimitry Andric MIB.add(MO);
16680b57cec5SDimitry Andric MIB.cloneMemRefs(MI);
16690b57cec5SDimitry Andric
1670e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << " Added new load/store: " << *MIB);
16710b57cec5SDimitry Andric MBB.erase(MBBI);
16720b57cec5SDimitry Andric return true;
16730b57cec5SDimitry Andric }
16740b57cec5SDimitry Andric
16750b57cec5SDimitry Andric /// Returns true if instruction is a memory operation that this pass is capable
16760b57cec5SDimitry Andric /// of operating on.
isMemoryOp(const MachineInstr & MI)16770b57cec5SDimitry Andric static bool isMemoryOp(const MachineInstr &MI) {
16780b57cec5SDimitry Andric unsigned Opcode = MI.getOpcode();
16790b57cec5SDimitry Andric switch (Opcode) {
16800b57cec5SDimitry Andric case ARM::VLDRS:
16810b57cec5SDimitry Andric case ARM::VSTRS:
16820b57cec5SDimitry Andric case ARM::VLDRD:
16830b57cec5SDimitry Andric case ARM::VSTRD:
16840b57cec5SDimitry Andric case ARM::LDRi12:
16850b57cec5SDimitry Andric case ARM::STRi12:
16860b57cec5SDimitry Andric case ARM::tLDRi:
16870b57cec5SDimitry Andric case ARM::tSTRi:
16880b57cec5SDimitry Andric case ARM::tLDRspi:
16890b57cec5SDimitry Andric case ARM::tSTRspi:
16900b57cec5SDimitry Andric case ARM::t2LDRi8:
16910b57cec5SDimitry Andric case ARM::t2LDRi12:
16920b57cec5SDimitry Andric case ARM::t2STRi8:
16930b57cec5SDimitry Andric case ARM::t2STRi12:
16940b57cec5SDimitry Andric break;
16950b57cec5SDimitry Andric default:
16960b57cec5SDimitry Andric return false;
16970b57cec5SDimitry Andric }
16980b57cec5SDimitry Andric if (!MI.getOperand(1).isReg())
16990b57cec5SDimitry Andric return false;
17000b57cec5SDimitry Andric
17010b57cec5SDimitry Andric // When no memory operands are present, conservatively assume unaligned,
17020b57cec5SDimitry Andric // volatile, unfoldable.
17030b57cec5SDimitry Andric if (!MI.hasOneMemOperand())
17040b57cec5SDimitry Andric return false;
17050b57cec5SDimitry Andric
17060b57cec5SDimitry Andric const MachineMemOperand &MMO = **MI.memoperands_begin();
17070b57cec5SDimitry Andric
17080b57cec5SDimitry Andric // Don't touch volatile memory accesses - we may be changing their order.
17090b57cec5SDimitry Andric // TODO: We could allow unordered and monotonic atomics here, but we need to
17100b57cec5SDimitry Andric // make sure the resulting ldm/stm is correctly marked as atomic.
17110b57cec5SDimitry Andric if (MMO.isVolatile() || MMO.isAtomic())
17120b57cec5SDimitry Andric return false;
17130b57cec5SDimitry Andric
17140b57cec5SDimitry Andric // Unaligned ldr/str is emulated by some kernels, but unaligned ldm/stm is
17150b57cec5SDimitry Andric // not.
17165ffd83dbSDimitry Andric if (MMO.getAlign() < Align(4))
17170b57cec5SDimitry Andric return false;
17180b57cec5SDimitry Andric
17190b57cec5SDimitry Andric // str <undef> could probably be eliminated entirely, but for now we just want
17200b57cec5SDimitry Andric // to avoid making a mess of it.
17210b57cec5SDimitry Andric // FIXME: Use str <undef> as a wildcard to enable better stm folding.
17220b57cec5SDimitry Andric if (MI.getOperand(0).isReg() && MI.getOperand(0).isUndef())
17230b57cec5SDimitry Andric return false;
17240b57cec5SDimitry Andric
17250b57cec5SDimitry Andric // Likewise don't mess with references to undefined addresses.
17260b57cec5SDimitry Andric if (MI.getOperand(1).isUndef())
17270b57cec5SDimitry Andric return false;
17280b57cec5SDimitry Andric
17290b57cec5SDimitry Andric return true;
17300b57cec5SDimitry Andric }
17310b57cec5SDimitry Andric
InsertLDR_STR(MachineBasicBlock & MBB,MachineBasicBlock::iterator & MBBI,int Offset,bool isDef,unsigned NewOpc,unsigned Reg,bool RegDeadKill,bool RegUndef,unsigned BaseReg,bool BaseKill,bool BaseUndef,ARMCC::CondCodes Pred,unsigned PredReg,const TargetInstrInfo * TII,MachineInstr * MI)17320b57cec5SDimitry Andric static void InsertLDR_STR(MachineBasicBlock &MBB,
17330b57cec5SDimitry Andric MachineBasicBlock::iterator &MBBI, int Offset,
17340b57cec5SDimitry Andric bool isDef, unsigned NewOpc, unsigned Reg,
17350b57cec5SDimitry Andric bool RegDeadKill, bool RegUndef, unsigned BaseReg,
17360b57cec5SDimitry Andric bool BaseKill, bool BaseUndef, ARMCC::CondCodes Pred,
17370b57cec5SDimitry Andric unsigned PredReg, const TargetInstrInfo *TII,
17380b57cec5SDimitry Andric MachineInstr *MI) {
17390b57cec5SDimitry Andric if (isDef) {
17400b57cec5SDimitry Andric MachineInstrBuilder MIB = BuildMI(MBB, MBBI, MBBI->getDebugLoc(),
17410b57cec5SDimitry Andric TII->get(NewOpc))
17420b57cec5SDimitry Andric .addReg(Reg, getDefRegState(true) | getDeadRegState(RegDeadKill))
17430b57cec5SDimitry Andric .addReg(BaseReg, getKillRegState(BaseKill)|getUndefRegState(BaseUndef));
17440b57cec5SDimitry Andric MIB.addImm(Offset).addImm(Pred).addReg(PredReg);
17450b57cec5SDimitry Andric // FIXME: This is overly conservative; the new instruction accesses 4
17460b57cec5SDimitry Andric // bytes, not 8.
17470b57cec5SDimitry Andric MIB.cloneMemRefs(*MI);
17480b57cec5SDimitry Andric } else {
17490b57cec5SDimitry Andric MachineInstrBuilder MIB = BuildMI(MBB, MBBI, MBBI->getDebugLoc(),
17500b57cec5SDimitry Andric TII->get(NewOpc))
17510b57cec5SDimitry Andric .addReg(Reg, getKillRegState(RegDeadKill) | getUndefRegState(RegUndef))
17520b57cec5SDimitry Andric .addReg(BaseReg, getKillRegState(BaseKill)|getUndefRegState(BaseUndef));
17530b57cec5SDimitry Andric MIB.addImm(Offset).addImm(Pred).addReg(PredReg);
17540b57cec5SDimitry Andric // FIXME: This is overly conservative; the new instruction accesses 4
17550b57cec5SDimitry Andric // bytes, not 8.
17560b57cec5SDimitry Andric MIB.cloneMemRefs(*MI);
17570b57cec5SDimitry Andric }
17580b57cec5SDimitry Andric }
17590b57cec5SDimitry Andric
FixInvalidRegPairOp(MachineBasicBlock & MBB,MachineBasicBlock::iterator & MBBI)17600b57cec5SDimitry Andric bool ARMLoadStoreOpt::FixInvalidRegPairOp(MachineBasicBlock &MBB,
17610b57cec5SDimitry Andric MachineBasicBlock::iterator &MBBI) {
17620b57cec5SDimitry Andric MachineInstr *MI = &*MBBI;
17630b57cec5SDimitry Andric unsigned Opcode = MI->getOpcode();
17640b57cec5SDimitry Andric // FIXME: Code/comments below check Opcode == t2STRDi8, but this check returns
17650b57cec5SDimitry Andric // if we see this opcode.
17660b57cec5SDimitry Andric if (Opcode != ARM::LDRD && Opcode != ARM::STRD && Opcode != ARM::t2LDRDi8)
17670b57cec5SDimitry Andric return false;
17680b57cec5SDimitry Andric
17690b57cec5SDimitry Andric const MachineOperand &BaseOp = MI->getOperand(2);
17708bcb0991SDimitry Andric Register BaseReg = BaseOp.getReg();
17718bcb0991SDimitry Andric Register EvenReg = MI->getOperand(0).getReg();
17728bcb0991SDimitry Andric Register OddReg = MI->getOperand(1).getReg();
17730b57cec5SDimitry Andric unsigned EvenRegNum = TRI->getDwarfRegNum(EvenReg, false);
17740b57cec5SDimitry Andric unsigned OddRegNum = TRI->getDwarfRegNum(OddReg, false);
17750b57cec5SDimitry Andric
17760b57cec5SDimitry Andric // ARM errata 602117: LDRD with base in list may result in incorrect base
17770b57cec5SDimitry Andric // register when interrupted or faulted.
17780b57cec5SDimitry Andric bool Errata602117 = EvenReg == BaseReg &&
17790b57cec5SDimitry Andric (Opcode == ARM::LDRD || Opcode == ARM::t2LDRDi8) && STI->isCortexM3();
17800b57cec5SDimitry Andric // ARM LDRD/STRD needs consecutive registers.
17810b57cec5SDimitry Andric bool NonConsecutiveRegs = (Opcode == ARM::LDRD || Opcode == ARM::STRD) &&
17820b57cec5SDimitry Andric (EvenRegNum % 2 != 0 || EvenRegNum + 1 != OddRegNum);
17830b57cec5SDimitry Andric
17840b57cec5SDimitry Andric if (!Errata602117 && !NonConsecutiveRegs)
17850b57cec5SDimitry Andric return false;
17860b57cec5SDimitry Andric
17870b57cec5SDimitry Andric bool isT2 = Opcode == ARM::t2LDRDi8 || Opcode == ARM::t2STRDi8;
17880b57cec5SDimitry Andric bool isLd = Opcode == ARM::LDRD || Opcode == ARM::t2LDRDi8;
17890b57cec5SDimitry Andric bool EvenDeadKill = isLd ?
17900b57cec5SDimitry Andric MI->getOperand(0).isDead() : MI->getOperand(0).isKill();
17910b57cec5SDimitry Andric bool EvenUndef = MI->getOperand(0).isUndef();
17920b57cec5SDimitry Andric bool OddDeadKill = isLd ?
17930b57cec5SDimitry Andric MI->getOperand(1).isDead() : MI->getOperand(1).isKill();
17940b57cec5SDimitry Andric bool OddUndef = MI->getOperand(1).isUndef();
17950b57cec5SDimitry Andric bool BaseKill = BaseOp.isKill();
17960b57cec5SDimitry Andric bool BaseUndef = BaseOp.isUndef();
17970b57cec5SDimitry Andric assert((isT2 || MI->getOperand(3).getReg() == ARM::NoRegister) &&
17980b57cec5SDimitry Andric "register offset not handled below");
17990b57cec5SDimitry Andric int OffImm = getMemoryOpOffset(*MI);
18005ffd83dbSDimitry Andric Register PredReg;
18010b57cec5SDimitry Andric ARMCC::CondCodes Pred = getInstrPredicate(*MI, PredReg);
18020b57cec5SDimitry Andric
18030b57cec5SDimitry Andric if (OddRegNum > EvenRegNum && OffImm == 0) {
18040b57cec5SDimitry Andric // Ascending register numbers and no offset. It's safe to change it to a
18050b57cec5SDimitry Andric // ldm or stm.
18060b57cec5SDimitry Andric unsigned NewOpc = (isLd)
18070b57cec5SDimitry Andric ? (isT2 ? ARM::t2LDMIA : ARM::LDMIA)
18080b57cec5SDimitry Andric : (isT2 ? ARM::t2STMIA : ARM::STMIA);
18090b57cec5SDimitry Andric if (isLd) {
18100b57cec5SDimitry Andric BuildMI(MBB, MBBI, MBBI->getDebugLoc(), TII->get(NewOpc))
18110b57cec5SDimitry Andric .addReg(BaseReg, getKillRegState(BaseKill))
18120b57cec5SDimitry Andric .addImm(Pred).addReg(PredReg)
18130b57cec5SDimitry Andric .addReg(EvenReg, getDefRegState(isLd) | getDeadRegState(EvenDeadKill))
18140b57cec5SDimitry Andric .addReg(OddReg, getDefRegState(isLd) | getDeadRegState(OddDeadKill))
18150b57cec5SDimitry Andric .cloneMemRefs(*MI);
18160b57cec5SDimitry Andric ++NumLDRD2LDM;
18170b57cec5SDimitry Andric } else {
18180b57cec5SDimitry Andric BuildMI(MBB, MBBI, MBBI->getDebugLoc(), TII->get(NewOpc))
18190b57cec5SDimitry Andric .addReg(BaseReg, getKillRegState(BaseKill))
18200b57cec5SDimitry Andric .addImm(Pred).addReg(PredReg)
18210b57cec5SDimitry Andric .addReg(EvenReg,
18220b57cec5SDimitry Andric getKillRegState(EvenDeadKill) | getUndefRegState(EvenUndef))
18230b57cec5SDimitry Andric .addReg(OddReg,
18240b57cec5SDimitry Andric getKillRegState(OddDeadKill) | getUndefRegState(OddUndef))
18250b57cec5SDimitry Andric .cloneMemRefs(*MI);
18260b57cec5SDimitry Andric ++NumSTRD2STM;
18270b57cec5SDimitry Andric }
18280b57cec5SDimitry Andric } else {
18290b57cec5SDimitry Andric // Split into two instructions.
18300b57cec5SDimitry Andric unsigned NewOpc = (isLd)
18310b57cec5SDimitry Andric ? (isT2 ? (OffImm < 0 ? ARM::t2LDRi8 : ARM::t2LDRi12) : ARM::LDRi12)
18320b57cec5SDimitry Andric : (isT2 ? (OffImm < 0 ? ARM::t2STRi8 : ARM::t2STRi12) : ARM::STRi12);
18330b57cec5SDimitry Andric // Be extra careful for thumb2. t2LDRi8 can't reference a zero offset,
18340b57cec5SDimitry Andric // so adjust and use t2LDRi12 here for that.
18350b57cec5SDimitry Andric unsigned NewOpc2 = (isLd)
18360b57cec5SDimitry Andric ? (isT2 ? (OffImm+4 < 0 ? ARM::t2LDRi8 : ARM::t2LDRi12) : ARM::LDRi12)
18370b57cec5SDimitry Andric : (isT2 ? (OffImm+4 < 0 ? ARM::t2STRi8 : ARM::t2STRi12) : ARM::STRi12);
18380b57cec5SDimitry Andric // If this is a load, make sure the first load does not clobber the base
18390b57cec5SDimitry Andric // register before the second load reads it.
18400b57cec5SDimitry Andric if (isLd && TRI->regsOverlap(EvenReg, BaseReg)) {
18410b57cec5SDimitry Andric assert(!TRI->regsOverlap(OddReg, BaseReg));
18420b57cec5SDimitry Andric InsertLDR_STR(MBB, MBBI, OffImm + 4, isLd, NewOpc2, OddReg, OddDeadKill,
18430b57cec5SDimitry Andric false, BaseReg, false, BaseUndef, Pred, PredReg, TII, MI);
18440b57cec5SDimitry Andric InsertLDR_STR(MBB, MBBI, OffImm, isLd, NewOpc, EvenReg, EvenDeadKill,
18450b57cec5SDimitry Andric false, BaseReg, BaseKill, BaseUndef, Pred, PredReg, TII,
18460b57cec5SDimitry Andric MI);
18470b57cec5SDimitry Andric } else {
18480b57cec5SDimitry Andric if (OddReg == EvenReg && EvenDeadKill) {
18490b57cec5SDimitry Andric // If the two source operands are the same, the kill marker is
18500b57cec5SDimitry Andric // probably on the first one. e.g.
18510b57cec5SDimitry Andric // t2STRDi8 killed %r5, %r5, killed %r9, 0, 14, %reg0
18520b57cec5SDimitry Andric EvenDeadKill = false;
18530b57cec5SDimitry Andric OddDeadKill = true;
18540b57cec5SDimitry Andric }
18550b57cec5SDimitry Andric // Never kill the base register in the first instruction.
18560b57cec5SDimitry Andric if (EvenReg == BaseReg)
18570b57cec5SDimitry Andric EvenDeadKill = false;
18580b57cec5SDimitry Andric InsertLDR_STR(MBB, MBBI, OffImm, isLd, NewOpc, EvenReg, EvenDeadKill,
18590b57cec5SDimitry Andric EvenUndef, BaseReg, false, BaseUndef, Pred, PredReg, TII,
18600b57cec5SDimitry Andric MI);
18610b57cec5SDimitry Andric InsertLDR_STR(MBB, MBBI, OffImm + 4, isLd, NewOpc2, OddReg, OddDeadKill,
18620b57cec5SDimitry Andric OddUndef, BaseReg, BaseKill, BaseUndef, Pred, PredReg, TII,
18630b57cec5SDimitry Andric MI);
18640b57cec5SDimitry Andric }
18650b57cec5SDimitry Andric if (isLd)
18660b57cec5SDimitry Andric ++NumLDRD2LDR;
18670b57cec5SDimitry Andric else
18680b57cec5SDimitry Andric ++NumSTRD2STR;
18690b57cec5SDimitry Andric }
18700b57cec5SDimitry Andric
18710b57cec5SDimitry Andric MBBI = MBB.erase(MBBI);
18720b57cec5SDimitry Andric return true;
18730b57cec5SDimitry Andric }
18740b57cec5SDimitry Andric
18750b57cec5SDimitry Andric /// An optimization pass to turn multiple LDR / STR ops of the same base and
18760b57cec5SDimitry Andric /// incrementing offset into LDM / STM ops.
LoadStoreMultipleOpti(MachineBasicBlock & MBB)18770b57cec5SDimitry Andric bool ARMLoadStoreOpt::LoadStoreMultipleOpti(MachineBasicBlock &MBB) {
18780b57cec5SDimitry Andric MemOpQueue MemOps;
18790b57cec5SDimitry Andric unsigned CurrBase = 0;
18800b57cec5SDimitry Andric unsigned CurrOpc = ~0u;
18810b57cec5SDimitry Andric ARMCC::CondCodes CurrPred = ARMCC::AL;
18820b57cec5SDimitry Andric unsigned Position = 0;
18830b57cec5SDimitry Andric assert(Candidates.size() == 0);
18840b57cec5SDimitry Andric assert(MergeBaseCandidates.size() == 0);
18850b57cec5SDimitry Andric LiveRegsValid = false;
18860b57cec5SDimitry Andric
18870b57cec5SDimitry Andric for (MachineBasicBlock::iterator I = MBB.end(), MBBI; I != MBB.begin();
18880b57cec5SDimitry Andric I = MBBI) {
18890b57cec5SDimitry Andric // The instruction in front of the iterator is the one we look at.
18900b57cec5SDimitry Andric MBBI = std::prev(I);
18910b57cec5SDimitry Andric if (FixInvalidRegPairOp(MBB, MBBI))
18920b57cec5SDimitry Andric continue;
18930b57cec5SDimitry Andric ++Position;
18940b57cec5SDimitry Andric
18950b57cec5SDimitry Andric if (isMemoryOp(*MBBI)) {
18960b57cec5SDimitry Andric unsigned Opcode = MBBI->getOpcode();
18970b57cec5SDimitry Andric const MachineOperand &MO = MBBI->getOperand(0);
18988bcb0991SDimitry Andric Register Reg = MO.getReg();
18998bcb0991SDimitry Andric Register Base = getLoadStoreBaseOp(*MBBI).getReg();
19005ffd83dbSDimitry Andric Register PredReg;
19010b57cec5SDimitry Andric ARMCC::CondCodes Pred = getInstrPredicate(*MBBI, PredReg);
19020b57cec5SDimitry Andric int Offset = getMemoryOpOffset(*MBBI);
19030b57cec5SDimitry Andric if (CurrBase == 0) {
19040b57cec5SDimitry Andric // Start of a new chain.
19050b57cec5SDimitry Andric CurrBase = Base;
19060b57cec5SDimitry Andric CurrOpc = Opcode;
19070b57cec5SDimitry Andric CurrPred = Pred;
19080b57cec5SDimitry Andric MemOps.push_back(MemOpQueueEntry(*MBBI, Offset, Position));
19090b57cec5SDimitry Andric continue;
19100b57cec5SDimitry Andric }
19110b57cec5SDimitry Andric // Note: No need to match PredReg in the next if.
19120b57cec5SDimitry Andric if (CurrOpc == Opcode && CurrBase == Base && CurrPred == Pred) {
19130b57cec5SDimitry Andric // Watch out for:
19140b57cec5SDimitry Andric // r4 := ldr [r0, #8]
19150b57cec5SDimitry Andric // r4 := ldr [r0, #4]
19160b57cec5SDimitry Andric // or
19170b57cec5SDimitry Andric // r0 := ldr [r0]
19180b57cec5SDimitry Andric // If a load overrides the base register or a register loaded by
19190b57cec5SDimitry Andric // another load in our chain, we cannot take this instruction.
19200b57cec5SDimitry Andric bool Overlap = false;
19210b57cec5SDimitry Andric if (isLoadSingle(Opcode)) {
19220b57cec5SDimitry Andric Overlap = (Base == Reg);
19230b57cec5SDimitry Andric if (!Overlap) {
19240b57cec5SDimitry Andric for (const MemOpQueueEntry &E : MemOps) {
19250b57cec5SDimitry Andric if (TRI->regsOverlap(Reg, E.MI->getOperand(0).getReg())) {
19260b57cec5SDimitry Andric Overlap = true;
19270b57cec5SDimitry Andric break;
19280b57cec5SDimitry Andric }
19290b57cec5SDimitry Andric }
19300b57cec5SDimitry Andric }
19310b57cec5SDimitry Andric }
19320b57cec5SDimitry Andric
19330b57cec5SDimitry Andric if (!Overlap) {
19340b57cec5SDimitry Andric // Check offset and sort memory operation into the current chain.
19350b57cec5SDimitry Andric if (Offset > MemOps.back().Offset) {
19360b57cec5SDimitry Andric MemOps.push_back(MemOpQueueEntry(*MBBI, Offset, Position));
19370b57cec5SDimitry Andric continue;
19380b57cec5SDimitry Andric } else {
19390b57cec5SDimitry Andric MemOpQueue::iterator MI, ME;
19400b57cec5SDimitry Andric for (MI = MemOps.begin(), ME = MemOps.end(); MI != ME; ++MI) {
19410b57cec5SDimitry Andric if (Offset < MI->Offset) {
19420b57cec5SDimitry Andric // Found a place to insert.
19430b57cec5SDimitry Andric break;
19440b57cec5SDimitry Andric }
19450b57cec5SDimitry Andric if (Offset == MI->Offset) {
19460b57cec5SDimitry Andric // Collision, abort.
19470b57cec5SDimitry Andric MI = ME;
19480b57cec5SDimitry Andric break;
19490b57cec5SDimitry Andric }
19500b57cec5SDimitry Andric }
19510b57cec5SDimitry Andric if (MI != MemOps.end()) {
19520b57cec5SDimitry Andric MemOps.insert(MI, MemOpQueueEntry(*MBBI, Offset, Position));
19530b57cec5SDimitry Andric continue;
19540b57cec5SDimitry Andric }
19550b57cec5SDimitry Andric }
19560b57cec5SDimitry Andric }
19570b57cec5SDimitry Andric }
19580b57cec5SDimitry Andric
19590b57cec5SDimitry Andric // Don't advance the iterator; The op will start a new chain next.
19600b57cec5SDimitry Andric MBBI = I;
19610b57cec5SDimitry Andric --Position;
19620b57cec5SDimitry Andric // Fallthrough to look into existing chain.
19630b57cec5SDimitry Andric } else if (MBBI->isDebugInstr()) {
19640b57cec5SDimitry Andric continue;
19650b57cec5SDimitry Andric } else if (MBBI->getOpcode() == ARM::t2LDRDi8 ||
19660b57cec5SDimitry Andric MBBI->getOpcode() == ARM::t2STRDi8) {
19670b57cec5SDimitry Andric // ARMPreAllocLoadStoreOpt has already formed some LDRD/STRD instructions
19680b57cec5SDimitry Andric // remember them because we may still be able to merge add/sub into them.
19690b57cec5SDimitry Andric MergeBaseCandidates.push_back(&*MBBI);
19700b57cec5SDimitry Andric }
19710b57cec5SDimitry Andric
19720b57cec5SDimitry Andric // If we are here then the chain is broken; Extract candidates for a merge.
19730b57cec5SDimitry Andric if (MemOps.size() > 0) {
19740b57cec5SDimitry Andric FormCandidates(MemOps);
19750b57cec5SDimitry Andric // Reset for the next chain.
19760b57cec5SDimitry Andric CurrBase = 0;
19770b57cec5SDimitry Andric CurrOpc = ~0u;
19780b57cec5SDimitry Andric CurrPred = ARMCC::AL;
19790b57cec5SDimitry Andric MemOps.clear();
19800b57cec5SDimitry Andric }
19810b57cec5SDimitry Andric }
19820b57cec5SDimitry Andric if (MemOps.size() > 0)
19830b57cec5SDimitry Andric FormCandidates(MemOps);
19840b57cec5SDimitry Andric
19850b57cec5SDimitry Andric // Sort candidates so they get processed from end to begin of the basic
19860b57cec5SDimitry Andric // block later; This is necessary for liveness calculation.
19870b57cec5SDimitry Andric auto LessThan = [](const MergeCandidate* M0, const MergeCandidate *M1) {
19880b57cec5SDimitry Andric return M0->InsertPos < M1->InsertPos;
19890b57cec5SDimitry Andric };
19900b57cec5SDimitry Andric llvm::sort(Candidates, LessThan);
19910b57cec5SDimitry Andric
19920b57cec5SDimitry Andric // Go through list of candidates and merge.
19930b57cec5SDimitry Andric bool Changed = false;
19940b57cec5SDimitry Andric for (const MergeCandidate *Candidate : Candidates) {
19950b57cec5SDimitry Andric if (Candidate->CanMergeToLSMulti || Candidate->CanMergeToLSDouble) {
19960b57cec5SDimitry Andric MachineInstr *Merged = MergeOpsUpdate(*Candidate);
19970b57cec5SDimitry Andric // Merge preceding/trailing base inc/dec into the merged op.
19980b57cec5SDimitry Andric if (Merged) {
19990b57cec5SDimitry Andric Changed = true;
20000b57cec5SDimitry Andric unsigned Opcode = Merged->getOpcode();
20010b57cec5SDimitry Andric if (Opcode == ARM::t2STRDi8 || Opcode == ARM::t2LDRDi8)
20020b57cec5SDimitry Andric MergeBaseUpdateLSDouble(*Merged);
20030b57cec5SDimitry Andric else
20040b57cec5SDimitry Andric MergeBaseUpdateLSMultiple(Merged);
20050b57cec5SDimitry Andric } else {
20060b57cec5SDimitry Andric for (MachineInstr *MI : Candidate->Instrs) {
20070b57cec5SDimitry Andric if (MergeBaseUpdateLoadStore(MI))
20080b57cec5SDimitry Andric Changed = true;
20090b57cec5SDimitry Andric }
20100b57cec5SDimitry Andric }
20110b57cec5SDimitry Andric } else {
20120b57cec5SDimitry Andric assert(Candidate->Instrs.size() == 1);
20130b57cec5SDimitry Andric if (MergeBaseUpdateLoadStore(Candidate->Instrs.front()))
20140b57cec5SDimitry Andric Changed = true;
20150b57cec5SDimitry Andric }
20160b57cec5SDimitry Andric }
20170b57cec5SDimitry Andric Candidates.clear();
20180b57cec5SDimitry Andric // Try to fold add/sub into the LDRD/STRD formed by ARMPreAllocLoadStoreOpt.
20190b57cec5SDimitry Andric for (MachineInstr *MI : MergeBaseCandidates)
20200b57cec5SDimitry Andric MergeBaseUpdateLSDouble(*MI);
20210b57cec5SDimitry Andric MergeBaseCandidates.clear();
20220b57cec5SDimitry Andric
20230b57cec5SDimitry Andric return Changed;
20240b57cec5SDimitry Andric }
20250b57cec5SDimitry Andric
20260b57cec5SDimitry Andric /// If this is a exit BB, try merging the return ops ("bx lr" and "mov pc, lr")
20270b57cec5SDimitry Andric /// into the preceding stack restore so it directly restore the value of LR
20280b57cec5SDimitry Andric /// into pc.
20290b57cec5SDimitry Andric /// ldmfd sp!, {..., lr}
20300b57cec5SDimitry Andric /// bx lr
20310b57cec5SDimitry Andric /// or
20320b57cec5SDimitry Andric /// ldmfd sp!, {..., lr}
20330b57cec5SDimitry Andric /// mov pc, lr
20340b57cec5SDimitry Andric /// =>
20350b57cec5SDimitry Andric /// ldmfd sp!, {..., pc}
MergeReturnIntoLDM(MachineBasicBlock & MBB)20360b57cec5SDimitry Andric bool ARMLoadStoreOpt::MergeReturnIntoLDM(MachineBasicBlock &MBB) {
20370b57cec5SDimitry Andric // Thumb1 LDM doesn't allow high registers.
20380b57cec5SDimitry Andric if (isThumb1) return false;
20390b57cec5SDimitry Andric if (MBB.empty()) return false;
20400b57cec5SDimitry Andric
20410b57cec5SDimitry Andric MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
20420b57cec5SDimitry Andric if (MBBI != MBB.begin() && MBBI != MBB.end() &&
20430b57cec5SDimitry Andric (MBBI->getOpcode() == ARM::BX_RET ||
20440b57cec5SDimitry Andric MBBI->getOpcode() == ARM::tBX_RET ||
20450b57cec5SDimitry Andric MBBI->getOpcode() == ARM::MOVPCLR)) {
20460b57cec5SDimitry Andric MachineBasicBlock::iterator PrevI = std::prev(MBBI);
20470b57cec5SDimitry Andric // Ignore any debug instructions.
20480b57cec5SDimitry Andric while (PrevI->isDebugInstr() && PrevI != MBB.begin())
20490b57cec5SDimitry Andric --PrevI;
20500b57cec5SDimitry Andric MachineInstr &PrevMI = *PrevI;
20510b57cec5SDimitry Andric unsigned Opcode = PrevMI.getOpcode();
20520b57cec5SDimitry Andric if (Opcode == ARM::LDMIA_UPD || Opcode == ARM::LDMDA_UPD ||
20530b57cec5SDimitry Andric Opcode == ARM::LDMDB_UPD || Opcode == ARM::LDMIB_UPD ||
20540b57cec5SDimitry Andric Opcode == ARM::t2LDMIA_UPD || Opcode == ARM::t2LDMDB_UPD) {
20550b57cec5SDimitry Andric MachineOperand &MO = PrevMI.getOperand(PrevMI.getNumOperands() - 1);
20560b57cec5SDimitry Andric if (MO.getReg() != ARM::LR)
20570b57cec5SDimitry Andric return false;
20580b57cec5SDimitry Andric unsigned NewOpc = (isThumb2 ? ARM::t2LDMIA_RET : ARM::LDMIA_RET);
20590b57cec5SDimitry Andric assert(((isThumb2 && Opcode == ARM::t2LDMIA_UPD) ||
20600b57cec5SDimitry Andric Opcode == ARM::LDMIA_UPD) && "Unsupported multiple load-return!");
20610b57cec5SDimitry Andric PrevMI.setDesc(TII->get(NewOpc));
20620b57cec5SDimitry Andric MO.setReg(ARM::PC);
20630b57cec5SDimitry Andric PrevMI.copyImplicitOps(*MBB.getParent(), *MBBI);
20640b57cec5SDimitry Andric MBB.erase(MBBI);
20650b57cec5SDimitry Andric return true;
20660b57cec5SDimitry Andric }
20670b57cec5SDimitry Andric }
20680b57cec5SDimitry Andric return false;
20690b57cec5SDimitry Andric }
20700b57cec5SDimitry Andric
CombineMovBx(MachineBasicBlock & MBB)20710b57cec5SDimitry Andric bool ARMLoadStoreOpt::CombineMovBx(MachineBasicBlock &MBB) {
20720b57cec5SDimitry Andric MachineBasicBlock::iterator MBBI = MBB.getFirstTerminator();
20730b57cec5SDimitry Andric if (MBBI == MBB.begin() || MBBI == MBB.end() ||
20740b57cec5SDimitry Andric MBBI->getOpcode() != ARM::tBX_RET)
20750b57cec5SDimitry Andric return false;
20760b57cec5SDimitry Andric
20770b57cec5SDimitry Andric MachineBasicBlock::iterator Prev = MBBI;
20780b57cec5SDimitry Andric --Prev;
20790b57cec5SDimitry Andric if (Prev->getOpcode() != ARM::tMOVr || !Prev->definesRegister(ARM::LR))
20800b57cec5SDimitry Andric return false;
20810b57cec5SDimitry Andric
20820b57cec5SDimitry Andric for (auto Use : Prev->uses())
20830b57cec5SDimitry Andric if (Use.isKill()) {
20840b57cec5SDimitry Andric assert(STI->hasV4TOps());
20850b57cec5SDimitry Andric BuildMI(MBB, MBBI, MBBI->getDebugLoc(), TII->get(ARM::tBX))
20860b57cec5SDimitry Andric .addReg(Use.getReg(), RegState::Kill)
20870b57cec5SDimitry Andric .add(predOps(ARMCC::AL))
20880b57cec5SDimitry Andric .copyImplicitOps(*MBBI);
20890b57cec5SDimitry Andric MBB.erase(MBBI);
20900b57cec5SDimitry Andric MBB.erase(Prev);
20910b57cec5SDimitry Andric return true;
20920b57cec5SDimitry Andric }
20930b57cec5SDimitry Andric
20940b57cec5SDimitry Andric llvm_unreachable("tMOVr doesn't kill a reg before tBX_RET?");
20950b57cec5SDimitry Andric }
20960b57cec5SDimitry Andric
runOnMachineFunction(MachineFunction & Fn)20970b57cec5SDimitry Andric bool ARMLoadStoreOpt::runOnMachineFunction(MachineFunction &Fn) {
20980b57cec5SDimitry Andric if (skipFunction(Fn.getFunction()))
20990b57cec5SDimitry Andric return false;
21000b57cec5SDimitry Andric
21010b57cec5SDimitry Andric MF = &Fn;
210281ad6265SDimitry Andric STI = &Fn.getSubtarget<ARMSubtarget>();
21030b57cec5SDimitry Andric TL = STI->getTargetLowering();
21040b57cec5SDimitry Andric AFI = Fn.getInfo<ARMFunctionInfo>();
21050b57cec5SDimitry Andric TII = STI->getInstrInfo();
21060b57cec5SDimitry Andric TRI = STI->getRegisterInfo();
21070b57cec5SDimitry Andric
21080b57cec5SDimitry Andric RegClassInfoValid = false;
21090b57cec5SDimitry Andric isThumb2 = AFI->isThumb2Function();
21100b57cec5SDimitry Andric isThumb1 = AFI->isThumbFunction() && !isThumb2;
21110b57cec5SDimitry Andric
2112*c9eafe97SDimitry Andric bool Modified = false, ModifiedLDMReturn = false;
21134824e7fdSDimitry Andric for (MachineBasicBlock &MBB : Fn) {
21140b57cec5SDimitry Andric Modified |= LoadStoreMultipleOpti(MBB);
21150eae32dcSDimitry Andric if (STI->hasV5TOps() && !AFI->shouldSignReturnAddress())
2116*c9eafe97SDimitry Andric ModifiedLDMReturn |= MergeReturnIntoLDM(MBB);
21170b57cec5SDimitry Andric if (isThumb1)
21180b57cec5SDimitry Andric Modified |= CombineMovBx(MBB);
21190b57cec5SDimitry Andric }
2120*c9eafe97SDimitry Andric Modified |= ModifiedLDMReturn;
2121*c9eafe97SDimitry Andric
2122*c9eafe97SDimitry Andric // If we merged a BX instruction into an LDM, we need to re-calculate whether
2123*c9eafe97SDimitry Andric // LR is restored. This check needs to consider the whole function, not just
2124*c9eafe97SDimitry Andric // the instruction(s) we changed, because there may be other BX returns which
2125*c9eafe97SDimitry Andric // still need LR to be restored.
2126*c9eafe97SDimitry Andric if (ModifiedLDMReturn)
2127*c9eafe97SDimitry Andric ARMFrameLowering::updateLRRestored(Fn);
21280b57cec5SDimitry Andric
21290b57cec5SDimitry Andric Allocator.DestroyAll();
21300b57cec5SDimitry Andric return Modified;
21310b57cec5SDimitry Andric }
21320b57cec5SDimitry Andric
21330b57cec5SDimitry Andric #define ARM_PREALLOC_LOAD_STORE_OPT_NAME \
21340b57cec5SDimitry Andric "ARM pre- register allocation load / store optimization pass"
21350b57cec5SDimitry Andric
21360b57cec5SDimitry Andric namespace {
21370b57cec5SDimitry Andric
21380b57cec5SDimitry Andric /// Pre- register allocation pass that move load / stores from consecutive
21390b57cec5SDimitry Andric /// locations close to make it more likely they will be combined later.
21400b57cec5SDimitry Andric struct ARMPreAllocLoadStoreOpt : public MachineFunctionPass{
21410b57cec5SDimitry Andric static char ID;
21420b57cec5SDimitry Andric
21430b57cec5SDimitry Andric AliasAnalysis *AA;
21440b57cec5SDimitry Andric const DataLayout *TD;
21450b57cec5SDimitry Andric const TargetInstrInfo *TII;
21460b57cec5SDimitry Andric const TargetRegisterInfo *TRI;
21470b57cec5SDimitry Andric const ARMSubtarget *STI;
21480b57cec5SDimitry Andric MachineRegisterInfo *MRI;
21495ffd83dbSDimitry Andric MachineDominatorTree *DT;
21500b57cec5SDimitry Andric MachineFunction *MF;
21510b57cec5SDimitry Andric
ARMPreAllocLoadStoreOpt__anon8bf7a5bb0311::ARMPreAllocLoadStoreOpt21520b57cec5SDimitry Andric ARMPreAllocLoadStoreOpt() : MachineFunctionPass(ID) {}
21530b57cec5SDimitry Andric
21540b57cec5SDimitry Andric bool runOnMachineFunction(MachineFunction &Fn) override;
21550b57cec5SDimitry Andric
getPassName__anon8bf7a5bb0311::ARMPreAllocLoadStoreOpt21560b57cec5SDimitry Andric StringRef getPassName() const override {
21570b57cec5SDimitry Andric return ARM_PREALLOC_LOAD_STORE_OPT_NAME;
21580b57cec5SDimitry Andric }
21590b57cec5SDimitry Andric
getAnalysisUsage__anon8bf7a5bb0311::ARMPreAllocLoadStoreOpt21600b57cec5SDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override {
21610b57cec5SDimitry Andric AU.addRequired<AAResultsWrapperPass>();
21625ffd83dbSDimitry Andric AU.addRequired<MachineDominatorTree>();
21635ffd83dbSDimitry Andric AU.addPreserved<MachineDominatorTree>();
21640b57cec5SDimitry Andric MachineFunctionPass::getAnalysisUsage(AU);
21650b57cec5SDimitry Andric }
21660b57cec5SDimitry Andric
21670b57cec5SDimitry Andric private:
21680b57cec5SDimitry Andric bool CanFormLdStDWord(MachineInstr *Op0, MachineInstr *Op1, DebugLoc &dl,
21695ffd83dbSDimitry Andric unsigned &NewOpc, Register &EvenReg, Register &OddReg,
21705ffd83dbSDimitry Andric Register &BaseReg, int &Offset, Register &PredReg,
21715ffd83dbSDimitry Andric ARMCC::CondCodes &Pred, bool &isT2);
2172fe013be4SDimitry Andric bool RescheduleOps(
2173fe013be4SDimitry Andric MachineBasicBlock *MBB, SmallVectorImpl<MachineInstr *> &Ops,
2174fe013be4SDimitry Andric unsigned Base, bool isLd, DenseMap<MachineInstr *, unsigned> &MI2LocMap,
2175fe013be4SDimitry Andric SmallDenseMap<Register, SmallVector<MachineInstr *>, 8> &RegisterMap);
21760b57cec5SDimitry Andric bool RescheduleLoadStoreInstrs(MachineBasicBlock *MBB);
21775ffd83dbSDimitry Andric bool DistributeIncrements();
21785ffd83dbSDimitry Andric bool DistributeIncrements(Register Base);
21790b57cec5SDimitry Andric };
21800b57cec5SDimitry Andric
21810b57cec5SDimitry Andric } // end anonymous namespace
21820b57cec5SDimitry Andric
21830b57cec5SDimitry Andric char ARMPreAllocLoadStoreOpt::ID = 0;
21840b57cec5SDimitry Andric
21855ffd83dbSDimitry Andric INITIALIZE_PASS_BEGIN(ARMPreAllocLoadStoreOpt, "arm-prera-ldst-opt",
21865ffd83dbSDimitry Andric ARM_PREALLOC_LOAD_STORE_OPT_NAME, false, false)
21875ffd83dbSDimitry Andric INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
21885ffd83dbSDimitry Andric INITIALIZE_PASS_END(ARMPreAllocLoadStoreOpt, "arm-prera-ldst-opt",
21890b57cec5SDimitry Andric ARM_PREALLOC_LOAD_STORE_OPT_NAME, false, false)
21900b57cec5SDimitry Andric
21910b57cec5SDimitry Andric // Limit the number of instructions to be rescheduled.
21920b57cec5SDimitry Andric // FIXME: tune this limit, and/or come up with some better heuristics.
21930b57cec5SDimitry Andric static cl::opt<unsigned> InstReorderLimit("arm-prera-ldst-opt-reorder-limit",
21940b57cec5SDimitry Andric cl::init(8), cl::Hidden);
21950b57cec5SDimitry Andric
runOnMachineFunction(MachineFunction & Fn)21960b57cec5SDimitry Andric bool ARMPreAllocLoadStoreOpt::runOnMachineFunction(MachineFunction &Fn) {
21970b57cec5SDimitry Andric if (AssumeMisalignedLoadStores || skipFunction(Fn.getFunction()))
21980b57cec5SDimitry Andric return false;
21990b57cec5SDimitry Andric
22000b57cec5SDimitry Andric TD = &Fn.getDataLayout();
220181ad6265SDimitry Andric STI = &Fn.getSubtarget<ARMSubtarget>();
22020b57cec5SDimitry Andric TII = STI->getInstrInfo();
22030b57cec5SDimitry Andric TRI = STI->getRegisterInfo();
22040b57cec5SDimitry Andric MRI = &Fn.getRegInfo();
22055ffd83dbSDimitry Andric DT = &getAnalysis<MachineDominatorTree>();
22060b57cec5SDimitry Andric MF = &Fn;
22070b57cec5SDimitry Andric AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
22080b57cec5SDimitry Andric
22095ffd83dbSDimitry Andric bool Modified = DistributeIncrements();
22100b57cec5SDimitry Andric for (MachineBasicBlock &MFI : Fn)
22110b57cec5SDimitry Andric Modified |= RescheduleLoadStoreInstrs(&MFI);
22120b57cec5SDimitry Andric
22130b57cec5SDimitry Andric return Modified;
22140b57cec5SDimitry Andric }
22150b57cec5SDimitry Andric
IsSafeAndProfitableToMove(bool isLd,unsigned Base,MachineBasicBlock::iterator I,MachineBasicBlock::iterator E,SmallPtrSetImpl<MachineInstr * > & MemOps,SmallSet<unsigned,4> & MemRegs,const TargetRegisterInfo * TRI,AliasAnalysis * AA)22160b57cec5SDimitry Andric static bool IsSafeAndProfitableToMove(bool isLd, unsigned Base,
22170b57cec5SDimitry Andric MachineBasicBlock::iterator I,
22180b57cec5SDimitry Andric MachineBasicBlock::iterator E,
22190b57cec5SDimitry Andric SmallPtrSetImpl<MachineInstr*> &MemOps,
22200b57cec5SDimitry Andric SmallSet<unsigned, 4> &MemRegs,
22210b57cec5SDimitry Andric const TargetRegisterInfo *TRI,
22220b57cec5SDimitry Andric AliasAnalysis *AA) {
22230b57cec5SDimitry Andric // Are there stores / loads / calls between them?
22240b57cec5SDimitry Andric SmallSet<unsigned, 4> AddedRegPressure;
22250b57cec5SDimitry Andric while (++I != E) {
22260b57cec5SDimitry Andric if (I->isDebugInstr() || MemOps.count(&*I))
22270b57cec5SDimitry Andric continue;
22280b57cec5SDimitry Andric if (I->isCall() || I->isTerminator() || I->hasUnmodeledSideEffects())
22290b57cec5SDimitry Andric return false;
22300b57cec5SDimitry Andric if (I->mayStore() || (!isLd && I->mayLoad()))
22310b57cec5SDimitry Andric for (MachineInstr *MemOp : MemOps)
22320b57cec5SDimitry Andric if (I->mayAlias(AA, *MemOp, /*UseTBAA*/ false))
22330b57cec5SDimitry Andric return false;
22340b57cec5SDimitry Andric for (unsigned j = 0, NumOps = I->getNumOperands(); j != NumOps; ++j) {
22350b57cec5SDimitry Andric MachineOperand &MO = I->getOperand(j);
22360b57cec5SDimitry Andric if (!MO.isReg())
22370b57cec5SDimitry Andric continue;
22388bcb0991SDimitry Andric Register Reg = MO.getReg();
22390b57cec5SDimitry Andric if (MO.isDef() && TRI->regsOverlap(Reg, Base))
22400b57cec5SDimitry Andric return false;
22410b57cec5SDimitry Andric if (Reg != Base && !MemRegs.count(Reg))
22420b57cec5SDimitry Andric AddedRegPressure.insert(Reg);
22430b57cec5SDimitry Andric }
22440b57cec5SDimitry Andric }
22450b57cec5SDimitry Andric
22460b57cec5SDimitry Andric // Estimate register pressure increase due to the transformation.
22470b57cec5SDimitry Andric if (MemRegs.size() <= 4)
22480b57cec5SDimitry Andric // Ok if we are moving small number of instructions.
22490b57cec5SDimitry Andric return true;
22500b57cec5SDimitry Andric return AddedRegPressure.size() <= MemRegs.size() * 2;
22510b57cec5SDimitry Andric }
22520b57cec5SDimitry Andric
CanFormLdStDWord(MachineInstr * Op0,MachineInstr * Op1,DebugLoc & dl,unsigned & NewOpc,Register & FirstReg,Register & SecondReg,Register & BaseReg,int & Offset,Register & PredReg,ARMCC::CondCodes & Pred,bool & isT2)22535ffd83dbSDimitry Andric bool ARMPreAllocLoadStoreOpt::CanFormLdStDWord(
22545ffd83dbSDimitry Andric MachineInstr *Op0, MachineInstr *Op1, DebugLoc &dl, unsigned &NewOpc,
22555ffd83dbSDimitry Andric Register &FirstReg, Register &SecondReg, Register &BaseReg, int &Offset,
22565ffd83dbSDimitry Andric Register &PredReg, ARMCC::CondCodes &Pred, bool &isT2) {
22570b57cec5SDimitry Andric // Make sure we're allowed to generate LDRD/STRD.
22580b57cec5SDimitry Andric if (!STI->hasV5TEOps())
22590b57cec5SDimitry Andric return false;
22600b57cec5SDimitry Andric
22610b57cec5SDimitry Andric // FIXME: VLDRS / VSTRS -> VLDRD / VSTRD
22620b57cec5SDimitry Andric unsigned Scale = 1;
22630b57cec5SDimitry Andric unsigned Opcode = Op0->getOpcode();
22640b57cec5SDimitry Andric if (Opcode == ARM::LDRi12) {
22650b57cec5SDimitry Andric NewOpc = ARM::LDRD;
22660b57cec5SDimitry Andric } else if (Opcode == ARM::STRi12) {
22670b57cec5SDimitry Andric NewOpc = ARM::STRD;
22680b57cec5SDimitry Andric } else if (Opcode == ARM::t2LDRi8 || Opcode == ARM::t2LDRi12) {
22690b57cec5SDimitry Andric NewOpc = ARM::t2LDRDi8;
22700b57cec5SDimitry Andric Scale = 4;
22710b57cec5SDimitry Andric isT2 = true;
22720b57cec5SDimitry Andric } else if (Opcode == ARM::t2STRi8 || Opcode == ARM::t2STRi12) {
22730b57cec5SDimitry Andric NewOpc = ARM::t2STRDi8;
22740b57cec5SDimitry Andric Scale = 4;
22750b57cec5SDimitry Andric isT2 = true;
22760b57cec5SDimitry Andric } else {
22770b57cec5SDimitry Andric return false;
22780b57cec5SDimitry Andric }
22790b57cec5SDimitry Andric
22800b57cec5SDimitry Andric // Make sure the base address satisfies i64 ld / st alignment requirement.
22810b57cec5SDimitry Andric // At the moment, we ignore the memoryoperand's value.
22820b57cec5SDimitry Andric // If we want to use AliasAnalysis, we should check it accordingly.
22830b57cec5SDimitry Andric if (!Op0->hasOneMemOperand() ||
22840b57cec5SDimitry Andric (*Op0->memoperands_begin())->isVolatile() ||
22850b57cec5SDimitry Andric (*Op0->memoperands_begin())->isAtomic())
22860b57cec5SDimitry Andric return false;
22870b57cec5SDimitry Andric
22885ffd83dbSDimitry Andric Align Alignment = (*Op0->memoperands_begin())->getAlign();
2289fe013be4SDimitry Andric Align ReqAlign = STI->getDualLoadStoreAlignment();
22905ffd83dbSDimitry Andric if (Alignment < ReqAlign)
22910b57cec5SDimitry Andric return false;
22920b57cec5SDimitry Andric
22930b57cec5SDimitry Andric // Then make sure the immediate offset fits.
22940b57cec5SDimitry Andric int OffImm = getMemoryOpOffset(*Op0);
22950b57cec5SDimitry Andric if (isT2) {
22960b57cec5SDimitry Andric int Limit = (1 << 8) * Scale;
22970b57cec5SDimitry Andric if (OffImm >= Limit || (OffImm <= -Limit) || (OffImm & (Scale-1)))
22980b57cec5SDimitry Andric return false;
22990b57cec5SDimitry Andric Offset = OffImm;
23000b57cec5SDimitry Andric } else {
23010b57cec5SDimitry Andric ARM_AM::AddrOpc AddSub = ARM_AM::add;
23020b57cec5SDimitry Andric if (OffImm < 0) {
23030b57cec5SDimitry Andric AddSub = ARM_AM::sub;
23040b57cec5SDimitry Andric OffImm = - OffImm;
23050b57cec5SDimitry Andric }
23060b57cec5SDimitry Andric int Limit = (1 << 8) * Scale;
23070b57cec5SDimitry Andric if (OffImm >= Limit || (OffImm & (Scale-1)))
23080b57cec5SDimitry Andric return false;
23090b57cec5SDimitry Andric Offset = ARM_AM::getAM3Opc(AddSub, OffImm);
23100b57cec5SDimitry Andric }
23110b57cec5SDimitry Andric FirstReg = Op0->getOperand(0).getReg();
23120b57cec5SDimitry Andric SecondReg = Op1->getOperand(0).getReg();
23130b57cec5SDimitry Andric if (FirstReg == SecondReg)
23140b57cec5SDimitry Andric return false;
23150b57cec5SDimitry Andric BaseReg = Op0->getOperand(1).getReg();
23160b57cec5SDimitry Andric Pred = getInstrPredicate(*Op0, PredReg);
23170b57cec5SDimitry Andric dl = Op0->getDebugLoc();
23180b57cec5SDimitry Andric return true;
23190b57cec5SDimitry Andric }
23200b57cec5SDimitry Andric
RescheduleOps(MachineBasicBlock * MBB,SmallVectorImpl<MachineInstr * > & Ops,unsigned Base,bool isLd,DenseMap<MachineInstr *,unsigned> & MI2LocMap,SmallDenseMap<Register,SmallVector<MachineInstr * >,8> & RegisterMap)2321fe013be4SDimitry Andric bool ARMPreAllocLoadStoreOpt::RescheduleOps(
2322fe013be4SDimitry Andric MachineBasicBlock *MBB, SmallVectorImpl<MachineInstr *> &Ops, unsigned Base,
2323fe013be4SDimitry Andric bool isLd, DenseMap<MachineInstr *, unsigned> &MI2LocMap,
2324fe013be4SDimitry Andric SmallDenseMap<Register, SmallVector<MachineInstr *>, 8> &RegisterMap) {
23250b57cec5SDimitry Andric bool RetVal = false;
23260b57cec5SDimitry Andric
23270b57cec5SDimitry Andric // Sort by offset (in reverse order).
23280b57cec5SDimitry Andric llvm::sort(Ops, [](const MachineInstr *LHS, const MachineInstr *RHS) {
23290b57cec5SDimitry Andric int LOffset = getMemoryOpOffset(*LHS);
23300b57cec5SDimitry Andric int ROffset = getMemoryOpOffset(*RHS);
23310b57cec5SDimitry Andric assert(LHS == RHS || LOffset != ROffset);
23320b57cec5SDimitry Andric return LOffset > ROffset;
23330b57cec5SDimitry Andric });
23340b57cec5SDimitry Andric
23350b57cec5SDimitry Andric // The loads / stores of the same base are in order. Scan them from first to
23360b57cec5SDimitry Andric // last and check for the following:
23370b57cec5SDimitry Andric // 1. Any def of base.
23380b57cec5SDimitry Andric // 2. Any gaps.
23390b57cec5SDimitry Andric while (Ops.size() > 1) {
23400b57cec5SDimitry Andric unsigned FirstLoc = ~0U;
23410b57cec5SDimitry Andric unsigned LastLoc = 0;
23420b57cec5SDimitry Andric MachineInstr *FirstOp = nullptr;
23430b57cec5SDimitry Andric MachineInstr *LastOp = nullptr;
23440b57cec5SDimitry Andric int LastOffset = 0;
23450b57cec5SDimitry Andric unsigned LastOpcode = 0;
23460b57cec5SDimitry Andric unsigned LastBytes = 0;
23470b57cec5SDimitry Andric unsigned NumMove = 0;
23480eae32dcSDimitry Andric for (MachineInstr *Op : llvm::reverse(Ops)) {
23490b57cec5SDimitry Andric // Make sure each operation has the same kind.
23500b57cec5SDimitry Andric unsigned LSMOpcode
23510b57cec5SDimitry Andric = getLoadStoreMultipleOpcode(Op->getOpcode(), ARM_AM::ia);
23520b57cec5SDimitry Andric if (LastOpcode && LSMOpcode != LastOpcode)
23530b57cec5SDimitry Andric break;
23540b57cec5SDimitry Andric
23550b57cec5SDimitry Andric // Check that we have a continuous set of offsets.
23560b57cec5SDimitry Andric int Offset = getMemoryOpOffset(*Op);
23570b57cec5SDimitry Andric unsigned Bytes = getLSMultipleTransferSize(Op);
23580b57cec5SDimitry Andric if (LastBytes) {
23590b57cec5SDimitry Andric if (Bytes != LastBytes || Offset != (LastOffset + (int)Bytes))
23600b57cec5SDimitry Andric break;
23610b57cec5SDimitry Andric }
23620b57cec5SDimitry Andric
23630b57cec5SDimitry Andric // Don't try to reschedule too many instructions.
23640b57cec5SDimitry Andric if (NumMove == InstReorderLimit)
23650b57cec5SDimitry Andric break;
23660b57cec5SDimitry Andric
23670b57cec5SDimitry Andric // Found a mergable instruction; save information about it.
23680b57cec5SDimitry Andric ++NumMove;
23690b57cec5SDimitry Andric LastOffset = Offset;
23700b57cec5SDimitry Andric LastBytes = Bytes;
23710b57cec5SDimitry Andric LastOpcode = LSMOpcode;
23720b57cec5SDimitry Andric
23730b57cec5SDimitry Andric unsigned Loc = MI2LocMap[Op];
23740b57cec5SDimitry Andric if (Loc <= FirstLoc) {
23750b57cec5SDimitry Andric FirstLoc = Loc;
23760b57cec5SDimitry Andric FirstOp = Op;
23770b57cec5SDimitry Andric }
23780b57cec5SDimitry Andric if (Loc >= LastLoc) {
23790b57cec5SDimitry Andric LastLoc = Loc;
23800b57cec5SDimitry Andric LastOp = Op;
23810b57cec5SDimitry Andric }
23820b57cec5SDimitry Andric }
23830b57cec5SDimitry Andric
23840b57cec5SDimitry Andric if (NumMove <= 1)
23850b57cec5SDimitry Andric Ops.pop_back();
23860b57cec5SDimitry Andric else {
23870b57cec5SDimitry Andric SmallPtrSet<MachineInstr*, 4> MemOps;
23880b57cec5SDimitry Andric SmallSet<unsigned, 4> MemRegs;
23890b57cec5SDimitry Andric for (size_t i = Ops.size() - NumMove, e = Ops.size(); i != e; ++i) {
23900b57cec5SDimitry Andric MemOps.insert(Ops[i]);
23910b57cec5SDimitry Andric MemRegs.insert(Ops[i]->getOperand(0).getReg());
23920b57cec5SDimitry Andric }
23930b57cec5SDimitry Andric
23940b57cec5SDimitry Andric // Be conservative, if the instructions are too far apart, don't
23950b57cec5SDimitry Andric // move them. We want to limit the increase of register pressure.
23960b57cec5SDimitry Andric bool DoMove = (LastLoc - FirstLoc) <= NumMove*4; // FIXME: Tune this.
23970b57cec5SDimitry Andric if (DoMove)
23980b57cec5SDimitry Andric DoMove = IsSafeAndProfitableToMove(isLd, Base, FirstOp, LastOp,
23990b57cec5SDimitry Andric MemOps, MemRegs, TRI, AA);
24000b57cec5SDimitry Andric if (!DoMove) {
24010b57cec5SDimitry Andric for (unsigned i = 0; i != NumMove; ++i)
24020b57cec5SDimitry Andric Ops.pop_back();
24030b57cec5SDimitry Andric } else {
24040b57cec5SDimitry Andric // This is the new location for the loads / stores.
24050b57cec5SDimitry Andric MachineBasicBlock::iterator InsertPos = isLd ? FirstOp : LastOp;
24060b57cec5SDimitry Andric while (InsertPos != MBB->end() &&
24070b57cec5SDimitry Andric (MemOps.count(&*InsertPos) || InsertPos->isDebugInstr()))
24080b57cec5SDimitry Andric ++InsertPos;
24090b57cec5SDimitry Andric
24100b57cec5SDimitry Andric // If we are moving a pair of loads / stores, see if it makes sense
24110b57cec5SDimitry Andric // to try to allocate a pair of registers that can form register pairs.
24120b57cec5SDimitry Andric MachineInstr *Op0 = Ops.back();
24130b57cec5SDimitry Andric MachineInstr *Op1 = Ops[Ops.size()-2];
24145ffd83dbSDimitry Andric Register FirstReg, SecondReg;
24155ffd83dbSDimitry Andric Register BaseReg, PredReg;
24160b57cec5SDimitry Andric ARMCC::CondCodes Pred = ARMCC::AL;
24170b57cec5SDimitry Andric bool isT2 = false;
24180b57cec5SDimitry Andric unsigned NewOpc = 0;
24190b57cec5SDimitry Andric int Offset = 0;
24200b57cec5SDimitry Andric DebugLoc dl;
24210b57cec5SDimitry Andric if (NumMove == 2 && CanFormLdStDWord(Op0, Op1, dl, NewOpc,
24220b57cec5SDimitry Andric FirstReg, SecondReg, BaseReg,
24230b57cec5SDimitry Andric Offset, PredReg, Pred, isT2)) {
24240b57cec5SDimitry Andric Ops.pop_back();
24250b57cec5SDimitry Andric Ops.pop_back();
24260b57cec5SDimitry Andric
24270b57cec5SDimitry Andric const MCInstrDesc &MCID = TII->get(NewOpc);
24280b57cec5SDimitry Andric const TargetRegisterClass *TRC = TII->getRegClass(MCID, 0, TRI, *MF);
24290b57cec5SDimitry Andric MRI->constrainRegClass(FirstReg, TRC);
24300b57cec5SDimitry Andric MRI->constrainRegClass(SecondReg, TRC);
24310b57cec5SDimitry Andric
24320b57cec5SDimitry Andric // Form the pair instruction.
24330b57cec5SDimitry Andric if (isLd) {
24340b57cec5SDimitry Andric MachineInstrBuilder MIB = BuildMI(*MBB, InsertPos, dl, MCID)
24350b57cec5SDimitry Andric .addReg(FirstReg, RegState::Define)
24360b57cec5SDimitry Andric .addReg(SecondReg, RegState::Define)
24370b57cec5SDimitry Andric .addReg(BaseReg);
24380b57cec5SDimitry Andric // FIXME: We're converting from LDRi12 to an insn that still
24390b57cec5SDimitry Andric // uses addrmode2, so we need an explicit offset reg. It should
24400b57cec5SDimitry Andric // always by reg0 since we're transforming LDRi12s.
24410b57cec5SDimitry Andric if (!isT2)
24420b57cec5SDimitry Andric MIB.addReg(0);
24430b57cec5SDimitry Andric MIB.addImm(Offset).addImm(Pred).addReg(PredReg);
24440b57cec5SDimitry Andric MIB.cloneMergedMemRefs({Op0, Op1});
24450b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Formed " << *MIB << "\n");
24460b57cec5SDimitry Andric ++NumLDRDFormed;
24470b57cec5SDimitry Andric } else {
24480b57cec5SDimitry Andric MachineInstrBuilder MIB = BuildMI(*MBB, InsertPos, dl, MCID)
24490b57cec5SDimitry Andric .addReg(FirstReg)
24500b57cec5SDimitry Andric .addReg(SecondReg)
24510b57cec5SDimitry Andric .addReg(BaseReg);
24520b57cec5SDimitry Andric // FIXME: We're converting from LDRi12 to an insn that still
24530b57cec5SDimitry Andric // uses addrmode2, so we need an explicit offset reg. It should
24540b57cec5SDimitry Andric // always by reg0 since we're transforming STRi12s.
24550b57cec5SDimitry Andric if (!isT2)
24560b57cec5SDimitry Andric MIB.addReg(0);
24570b57cec5SDimitry Andric MIB.addImm(Offset).addImm(Pred).addReg(PredReg);
24580b57cec5SDimitry Andric MIB.cloneMergedMemRefs({Op0, Op1});
24590b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Formed " << *MIB << "\n");
24600b57cec5SDimitry Andric ++NumSTRDFormed;
24610b57cec5SDimitry Andric }
24620b57cec5SDimitry Andric MBB->erase(Op0);
24630b57cec5SDimitry Andric MBB->erase(Op1);
24640b57cec5SDimitry Andric
24650b57cec5SDimitry Andric if (!isT2) {
24660b57cec5SDimitry Andric // Add register allocation hints to form register pairs.
24670b57cec5SDimitry Andric MRI->setRegAllocationHint(FirstReg, ARMRI::RegPairEven, SecondReg);
24680b57cec5SDimitry Andric MRI->setRegAllocationHint(SecondReg, ARMRI::RegPairOdd, FirstReg);
24690b57cec5SDimitry Andric }
24700b57cec5SDimitry Andric } else {
24710b57cec5SDimitry Andric for (unsigned i = 0; i != NumMove; ++i) {
2472349cc55cSDimitry Andric MachineInstr *Op = Ops.pop_back_val();
2473fe013be4SDimitry Andric if (isLd) {
2474fe013be4SDimitry Andric // Populate RegisterMap with all Registers defined by loads.
2475fe013be4SDimitry Andric Register Reg = Op->getOperand(0).getReg();
2476fe013be4SDimitry Andric RegisterMap[Reg];
2477fe013be4SDimitry Andric }
2478fe013be4SDimitry Andric
24790b57cec5SDimitry Andric MBB->splice(InsertPos, MBB, Op);
24800b57cec5SDimitry Andric }
24810b57cec5SDimitry Andric }
24820b57cec5SDimitry Andric
24830b57cec5SDimitry Andric NumLdStMoved += NumMove;
24840b57cec5SDimitry Andric RetVal = true;
24850b57cec5SDimitry Andric }
24860b57cec5SDimitry Andric }
24870b57cec5SDimitry Andric }
24880b57cec5SDimitry Andric
24890b57cec5SDimitry Andric return RetVal;
24900b57cec5SDimitry Andric }
24910b57cec5SDimitry Andric
forEachDbgRegOperand(MachineInstr * MI,std::function<void (MachineOperand &)> Fn)2492fe013be4SDimitry Andric static void forEachDbgRegOperand(MachineInstr *MI,
2493fe013be4SDimitry Andric std::function<void(MachineOperand &)> Fn) {
2494fe013be4SDimitry Andric if (MI->isNonListDebugValue()) {
2495fe013be4SDimitry Andric auto &Op = MI->getOperand(0);
2496fe013be4SDimitry Andric if (Op.isReg())
2497fe013be4SDimitry Andric Fn(Op);
2498fe013be4SDimitry Andric } else {
2499fe013be4SDimitry Andric for (unsigned I = 2; I < MI->getNumOperands(); I++) {
2500fe013be4SDimitry Andric auto &Op = MI->getOperand(I);
2501fe013be4SDimitry Andric if (Op.isReg())
2502fe013be4SDimitry Andric Fn(Op);
2503fe013be4SDimitry Andric }
2504fe013be4SDimitry Andric }
2505fe013be4SDimitry Andric }
2506fe013be4SDimitry Andric
2507fe013be4SDimitry Andric // Update the RegisterMap with the instruction that was moved because a
2508fe013be4SDimitry Andric // DBG_VALUE_LIST may need to be moved again.
updateRegisterMapForDbgValueListAfterMove(SmallDenseMap<Register,SmallVector<MachineInstr * >,8> & RegisterMap,MachineInstr * DbgValueListInstr,MachineInstr * InstrToReplace)2509fe013be4SDimitry Andric static void updateRegisterMapForDbgValueListAfterMove(
2510fe013be4SDimitry Andric SmallDenseMap<Register, SmallVector<MachineInstr *>, 8> &RegisterMap,
2511fe013be4SDimitry Andric MachineInstr *DbgValueListInstr, MachineInstr *InstrToReplace) {
2512fe013be4SDimitry Andric
2513fe013be4SDimitry Andric forEachDbgRegOperand(DbgValueListInstr, [&](MachineOperand &Op) {
2514fe013be4SDimitry Andric auto RegIt = RegisterMap.find(Op.getReg());
2515fe013be4SDimitry Andric if (RegIt == RegisterMap.end())
2516fe013be4SDimitry Andric return;
2517fe013be4SDimitry Andric auto &InstrVec = RegIt->getSecond();
2518fe013be4SDimitry Andric for (unsigned I = 0; I < InstrVec.size(); I++)
2519fe013be4SDimitry Andric if (InstrVec[I] == InstrToReplace)
2520fe013be4SDimitry Andric InstrVec[I] = DbgValueListInstr;
2521fe013be4SDimitry Andric });
2522fe013be4SDimitry Andric }
2523fe013be4SDimitry Andric
createDebugVariableFromMachineInstr(MachineInstr * MI)2524fe013be4SDimitry Andric static DebugVariable createDebugVariableFromMachineInstr(MachineInstr *MI) {
2525fe013be4SDimitry Andric auto DbgVar = DebugVariable(MI->getDebugVariable(), MI->getDebugExpression(),
2526fe013be4SDimitry Andric MI->getDebugLoc()->getInlinedAt());
2527fe013be4SDimitry Andric return DbgVar;
2528fe013be4SDimitry Andric }
2529fe013be4SDimitry Andric
25300b57cec5SDimitry Andric bool
RescheduleLoadStoreInstrs(MachineBasicBlock * MBB)25310b57cec5SDimitry Andric ARMPreAllocLoadStoreOpt::RescheduleLoadStoreInstrs(MachineBasicBlock *MBB) {
25320b57cec5SDimitry Andric bool RetVal = false;
25330b57cec5SDimitry Andric
25340b57cec5SDimitry Andric DenseMap<MachineInstr*, unsigned> MI2LocMap;
25350b57cec5SDimitry Andric using MapIt = DenseMap<unsigned, SmallVector<MachineInstr *, 4>>::iterator;
25360b57cec5SDimitry Andric using Base2InstMap = DenseMap<unsigned, SmallVector<MachineInstr *, 4>>;
25370b57cec5SDimitry Andric using BaseVec = SmallVector<unsigned, 4>;
25380b57cec5SDimitry Andric Base2InstMap Base2LdsMap;
25390b57cec5SDimitry Andric Base2InstMap Base2StsMap;
25400b57cec5SDimitry Andric BaseVec LdBases;
25410b57cec5SDimitry Andric BaseVec StBases;
2542fe013be4SDimitry Andric // This map is used to track the relationship between the virtual
2543fe013be4SDimitry Andric // register that is the result of a load that is moved and the DBG_VALUE
2544fe013be4SDimitry Andric // MachineInstr pointer that uses that virtual register.
2545fe013be4SDimitry Andric SmallDenseMap<Register, SmallVector<MachineInstr *>, 8> RegisterMap;
25460b57cec5SDimitry Andric
25470b57cec5SDimitry Andric unsigned Loc = 0;
25480b57cec5SDimitry Andric MachineBasicBlock::iterator MBBI = MBB->begin();
25490b57cec5SDimitry Andric MachineBasicBlock::iterator E = MBB->end();
25500b57cec5SDimitry Andric while (MBBI != E) {
25510b57cec5SDimitry Andric for (; MBBI != E; ++MBBI) {
25520b57cec5SDimitry Andric MachineInstr &MI = *MBBI;
25530b57cec5SDimitry Andric if (MI.isCall() || MI.isTerminator()) {
25540b57cec5SDimitry Andric // Stop at barriers.
25550b57cec5SDimitry Andric ++MBBI;
25560b57cec5SDimitry Andric break;
25570b57cec5SDimitry Andric }
25580b57cec5SDimitry Andric
25590b57cec5SDimitry Andric if (!MI.isDebugInstr())
25600b57cec5SDimitry Andric MI2LocMap[&MI] = ++Loc;
25610b57cec5SDimitry Andric
25620b57cec5SDimitry Andric if (!isMemoryOp(MI))
25630b57cec5SDimitry Andric continue;
25645ffd83dbSDimitry Andric Register PredReg;
25650b57cec5SDimitry Andric if (getInstrPredicate(MI, PredReg) != ARMCC::AL)
25660b57cec5SDimitry Andric continue;
25670b57cec5SDimitry Andric
25680b57cec5SDimitry Andric int Opc = MI.getOpcode();
25690b57cec5SDimitry Andric bool isLd = isLoadSingle(Opc);
25708bcb0991SDimitry Andric Register Base = MI.getOperand(1).getReg();
25710b57cec5SDimitry Andric int Offset = getMemoryOpOffset(MI);
25720b57cec5SDimitry Andric bool StopHere = false;
25730b57cec5SDimitry Andric auto FindBases = [&] (Base2InstMap &Base2Ops, BaseVec &Bases) {
25740b57cec5SDimitry Andric MapIt BI = Base2Ops.find(Base);
25750b57cec5SDimitry Andric if (BI == Base2Ops.end()) {
25760b57cec5SDimitry Andric Base2Ops[Base].push_back(&MI);
25770b57cec5SDimitry Andric Bases.push_back(Base);
25780b57cec5SDimitry Andric return;
25790b57cec5SDimitry Andric }
25800b57cec5SDimitry Andric for (unsigned i = 0, e = BI->second.size(); i != e; ++i) {
25810b57cec5SDimitry Andric if (Offset == getMemoryOpOffset(*BI->second[i])) {
25820b57cec5SDimitry Andric StopHere = true;
25830b57cec5SDimitry Andric break;
25840b57cec5SDimitry Andric }
25850b57cec5SDimitry Andric }
25860b57cec5SDimitry Andric if (!StopHere)
25870b57cec5SDimitry Andric BI->second.push_back(&MI);
25880b57cec5SDimitry Andric };
25890b57cec5SDimitry Andric
25900b57cec5SDimitry Andric if (isLd)
25910b57cec5SDimitry Andric FindBases(Base2LdsMap, LdBases);
25920b57cec5SDimitry Andric else
25930b57cec5SDimitry Andric FindBases(Base2StsMap, StBases);
25940b57cec5SDimitry Andric
25950b57cec5SDimitry Andric if (StopHere) {
25960b57cec5SDimitry Andric // Found a duplicate (a base+offset combination that's seen earlier).
25970b57cec5SDimitry Andric // Backtrack.
25980b57cec5SDimitry Andric --Loc;
25990b57cec5SDimitry Andric break;
26000b57cec5SDimitry Andric }
26010b57cec5SDimitry Andric }
26020b57cec5SDimitry Andric
26030b57cec5SDimitry Andric // Re-schedule loads.
2604e710425bSDimitry Andric for (unsigned Base : LdBases) {
26050b57cec5SDimitry Andric SmallVectorImpl<MachineInstr *> &Lds = Base2LdsMap[Base];
26060b57cec5SDimitry Andric if (Lds.size() > 1)
2607fe013be4SDimitry Andric RetVal |= RescheduleOps(MBB, Lds, Base, true, MI2LocMap, RegisterMap);
26080b57cec5SDimitry Andric }
26090b57cec5SDimitry Andric
26100b57cec5SDimitry Andric // Re-schedule stores.
2611e710425bSDimitry Andric for (unsigned Base : StBases) {
26120b57cec5SDimitry Andric SmallVectorImpl<MachineInstr *> &Sts = Base2StsMap[Base];
26130b57cec5SDimitry Andric if (Sts.size() > 1)
2614fe013be4SDimitry Andric RetVal |= RescheduleOps(MBB, Sts, Base, false, MI2LocMap, RegisterMap);
26150b57cec5SDimitry Andric }
26160b57cec5SDimitry Andric
26170b57cec5SDimitry Andric if (MBBI != E) {
26180b57cec5SDimitry Andric Base2LdsMap.clear();
26190b57cec5SDimitry Andric Base2StsMap.clear();
26200b57cec5SDimitry Andric LdBases.clear();
26210b57cec5SDimitry Andric StBases.clear();
26220b57cec5SDimitry Andric }
26230b57cec5SDimitry Andric }
26240b57cec5SDimitry Andric
2625fe013be4SDimitry Andric // Reschedule DBG_VALUEs to match any loads that were moved. When a load is
2626fe013be4SDimitry Andric // sunk beyond a DBG_VALUE that is referring to it, the DBG_VALUE becomes a
2627fe013be4SDimitry Andric // use-before-def, resulting in a loss of debug info.
2628fe013be4SDimitry Andric
2629fe013be4SDimitry Andric // Example:
2630fe013be4SDimitry Andric // Before the Pre Register Allocation Load Store Pass
2631fe013be4SDimitry Andric // inst_a
2632fe013be4SDimitry Andric // %2 = ld ...
2633fe013be4SDimitry Andric // inst_b
2634fe013be4SDimitry Andric // DBG_VALUE %2, "x", ...
2635fe013be4SDimitry Andric // %3 = ld ...
2636fe013be4SDimitry Andric
2637fe013be4SDimitry Andric // After the Pass:
2638fe013be4SDimitry Andric // inst_a
2639fe013be4SDimitry Andric // inst_b
2640fe013be4SDimitry Andric // DBG_VALUE %2, "x", ...
2641fe013be4SDimitry Andric // %2 = ld ...
2642fe013be4SDimitry Andric // %3 = ld ...
2643fe013be4SDimitry Andric
2644fe013be4SDimitry Andric // The code below addresses this by moving the DBG_VALUE to the position
2645fe013be4SDimitry Andric // immediately after the load.
2646fe013be4SDimitry Andric
2647fe013be4SDimitry Andric // Example:
2648fe013be4SDimitry Andric // After the code below:
2649fe013be4SDimitry Andric // inst_a
2650fe013be4SDimitry Andric // inst_b
2651fe013be4SDimitry Andric // %2 = ld ...
2652fe013be4SDimitry Andric // DBG_VALUE %2, "x", ...
2653fe013be4SDimitry Andric // %3 = ld ...
2654fe013be4SDimitry Andric
2655fe013be4SDimitry Andric // The algorithm works in two phases: First RescheduleOps() populates the
2656fe013be4SDimitry Andric // RegisterMap with registers that were moved as keys, there is no value
2657fe013be4SDimitry Andric // inserted. In the next phase, every MachineInstr in a basic block is
2658fe013be4SDimitry Andric // iterated over. If it is a valid DBG_VALUE or DBG_VALUE_LIST and it uses one
2659fe013be4SDimitry Andric // or more registers in the RegisterMap, the RegisterMap and InstrMap are
2660fe013be4SDimitry Andric // populated with the MachineInstr. If the DBG_VALUE or DBG_VALUE_LIST
2661fe013be4SDimitry Andric // describes debug information for a variable that already exists in the
2662fe013be4SDimitry Andric // DbgValueSinkCandidates, the MachineInstr in the DbgValueSinkCandidates must
2663fe013be4SDimitry Andric // be set to undef. If the current MachineInstr is a load that was moved,
2664fe013be4SDimitry Andric // undef the corresponding DBG_VALUE or DBG_VALUE_LIST and clone it to below
2665fe013be4SDimitry Andric // the load.
2666fe013be4SDimitry Andric
2667fe013be4SDimitry Andric // To illustrate the above algorithm visually let's take this example.
2668fe013be4SDimitry Andric
2669fe013be4SDimitry Andric // Before the Pre Register Allocation Load Store Pass:
2670fe013be4SDimitry Andric // %2 = ld ...
2671fe013be4SDimitry Andric // DBG_VALUE %2, A, .... # X
2672fe013be4SDimitry Andric // DBG_VALUE 0, A, ... # Y
2673fe013be4SDimitry Andric // %3 = ld ...
2674fe013be4SDimitry Andric // DBG_VALUE %3, A, ..., # Z
2675fe013be4SDimitry Andric // %4 = ld ...
2676fe013be4SDimitry Andric
2677fe013be4SDimitry Andric // After Pre Register Allocation Load Store Pass:
2678fe013be4SDimitry Andric // DBG_VALUE %2, A, .... # X
2679fe013be4SDimitry Andric // DBG_VALUE 0, A, ... # Y
2680fe013be4SDimitry Andric // DBG_VALUE %3, A, ..., # Z
2681fe013be4SDimitry Andric // %2 = ld ...
2682fe013be4SDimitry Andric // %3 = ld ...
2683fe013be4SDimitry Andric // %4 = ld ...
2684fe013be4SDimitry Andric
2685fe013be4SDimitry Andric // The algorithm below does the following:
2686fe013be4SDimitry Andric
2687fe013be4SDimitry Andric // In the beginning, the RegisterMap will have been populated with the virtual
2688fe013be4SDimitry Andric // registers %2, and %3, the DbgValueSinkCandidates and the InstrMap will be
2689fe013be4SDimitry Andric // empty. DbgValueSinkCandidates = {}, RegisterMap = {2 -> {}, 3 -> {}},
2690fe013be4SDimitry Andric // InstrMap {}
2691fe013be4SDimitry Andric // -> DBG_VALUE %2, A, .... # X
2692fe013be4SDimitry Andric // DBG_VALUE 0, A, ... # Y
2693fe013be4SDimitry Andric // DBG_VALUE %3, A, ..., # Z
2694fe013be4SDimitry Andric // %2 = ld ...
2695fe013be4SDimitry Andric // %3 = ld ...
2696fe013be4SDimitry Andric // %4 = ld ...
2697fe013be4SDimitry Andric
2698fe013be4SDimitry Andric // After the first DBG_VALUE (denoted with an X) is processed, the
2699fe013be4SDimitry Andric // DbgValueSinkCandidates and InstrMap will be populated and the RegisterMap
2700fe013be4SDimitry Andric // entry for %2 will be populated as well. DbgValueSinkCandidates = {A -> X},
2701fe013be4SDimitry Andric // RegisterMap = {2 -> {X}, 3 -> {}}, InstrMap {X -> 2}
2702fe013be4SDimitry Andric // DBG_VALUE %2, A, .... # X
2703fe013be4SDimitry Andric // -> DBG_VALUE 0, A, ... # Y
2704fe013be4SDimitry Andric // DBG_VALUE %3, A, ..., # Z
2705fe013be4SDimitry Andric // %2 = ld ...
2706fe013be4SDimitry Andric // %3 = ld ...
2707fe013be4SDimitry Andric // %4 = ld ...
2708fe013be4SDimitry Andric
2709fe013be4SDimitry Andric // After the DBG_VALUE Y is processed, the DbgValueSinkCandidates is updated
2710fe013be4SDimitry Andric // to now hold Y for A and the RegisterMap is also updated to remove X from
2711fe013be4SDimitry Andric // %2, this is because both X and Y describe the same debug variable A. X is
2712fe013be4SDimitry Andric // also updated to have a $noreg as the first operand.
2713fe013be4SDimitry Andric // DbgValueSinkCandidates = {A -> {Y}}, RegisterMap = {2 -> {}, 3 -> {}},
2714fe013be4SDimitry Andric // InstrMap = {X-> 2}
2715fe013be4SDimitry Andric // DBG_VALUE $noreg, A, .... # X
2716fe013be4SDimitry Andric // DBG_VALUE 0, A, ... # Y
2717fe013be4SDimitry Andric // -> DBG_VALUE %3, A, ..., # Z
2718fe013be4SDimitry Andric // %2 = ld ...
2719fe013be4SDimitry Andric // %3 = ld ...
2720fe013be4SDimitry Andric // %4 = ld ...
2721fe013be4SDimitry Andric
2722fe013be4SDimitry Andric // After DBG_VALUE Z is processed, the DbgValueSinkCandidates is updated to
2723fe013be4SDimitry Andric // hold Z fr A, the RegisterMap is updated to hold Z for %3, and the InstrMap
2724fe013be4SDimitry Andric // is updated to have Z mapped to %3. This is again because Z describes the
2725fe013be4SDimitry Andric // debug variable A, Y is not updated to have $noreg as first operand because
2726fe013be4SDimitry Andric // its first operand is an immediate, not a register.
2727fe013be4SDimitry Andric // DbgValueSinkCandidates = {A -> {Z}}, RegisterMap = {2 -> {}, 3 -> {Z}},
2728fe013be4SDimitry Andric // InstrMap = {X -> 2, Z -> 3}
2729fe013be4SDimitry Andric // DBG_VALUE $noreg, A, .... # X
2730fe013be4SDimitry Andric // DBG_VALUE 0, A, ... # Y
2731fe013be4SDimitry Andric // DBG_VALUE %3, A, ..., # Z
2732fe013be4SDimitry Andric // -> %2 = ld ...
2733fe013be4SDimitry Andric // %3 = ld ...
2734fe013be4SDimitry Andric // %4 = ld ...
2735fe013be4SDimitry Andric
2736fe013be4SDimitry Andric // Nothing happens here since the RegisterMap for %2 contains no value.
2737fe013be4SDimitry Andric // DbgValueSinkCandidates = {A -> {Z}}, RegisterMap = {2 -> {}, 3 -> {Z}},
2738fe013be4SDimitry Andric // InstrMap = {X -> 2, Z -> 3}
2739fe013be4SDimitry Andric // DBG_VALUE $noreg, A, .... # X
2740fe013be4SDimitry Andric // DBG_VALUE 0, A, ... # Y
2741fe013be4SDimitry Andric // DBG_VALUE %3, A, ..., # Z
2742fe013be4SDimitry Andric // %2 = ld ...
2743fe013be4SDimitry Andric // -> %3 = ld ...
2744fe013be4SDimitry Andric // %4 = ld ...
2745fe013be4SDimitry Andric
2746fe013be4SDimitry Andric // Since the RegisterMap contains Z as a value for %3, the MachineInstr
2747fe013be4SDimitry Andric // pointer Z is copied to come after the load for %3 and the old Z's first
2748fe013be4SDimitry Andric // operand is changed to $noreg the Basic Block iterator is moved to after the
2749fe013be4SDimitry Andric // DBG_VALUE Z's new position.
2750fe013be4SDimitry Andric // DbgValueSinkCandidates = {A -> {Z}}, RegisterMap = {2 -> {}, 3 -> {Z}},
2751fe013be4SDimitry Andric // InstrMap = {X -> 2, Z -> 3}
2752fe013be4SDimitry Andric // DBG_VALUE $noreg, A, .... # X
2753fe013be4SDimitry Andric // DBG_VALUE 0, A, ... # Y
2754fe013be4SDimitry Andric // DBG_VALUE $noreg, A, ..., # Old Z
2755fe013be4SDimitry Andric // %2 = ld ...
2756fe013be4SDimitry Andric // %3 = ld ...
2757fe013be4SDimitry Andric // DBG_VALUE %3, A, ..., # Z
2758fe013be4SDimitry Andric // -> %4 = ld ...
2759fe013be4SDimitry Andric
2760fe013be4SDimitry Andric // Nothing happens for %4 and the algorithm exits having processed the entire
2761fe013be4SDimitry Andric // Basic Block.
2762fe013be4SDimitry Andric // DbgValueSinkCandidates = {A -> {Z}}, RegisterMap = {2 -> {}, 3 -> {Z}},
2763fe013be4SDimitry Andric // InstrMap = {X -> 2, Z -> 3}
2764fe013be4SDimitry Andric // DBG_VALUE $noreg, A, .... # X
2765fe013be4SDimitry Andric // DBG_VALUE 0, A, ... # Y
2766fe013be4SDimitry Andric // DBG_VALUE $noreg, A, ..., # Old Z
2767fe013be4SDimitry Andric // %2 = ld ...
2768fe013be4SDimitry Andric // %3 = ld ...
2769fe013be4SDimitry Andric // DBG_VALUE %3, A, ..., # Z
2770fe013be4SDimitry Andric // %4 = ld ...
2771fe013be4SDimitry Andric
2772fe013be4SDimitry Andric // This map is used to track the relationship between
2773fe013be4SDimitry Andric // a Debug Variable and the DBG_VALUE MachineInstr pointer that describes the
2774fe013be4SDimitry Andric // debug information for that Debug Variable.
2775fe013be4SDimitry Andric SmallDenseMap<DebugVariable, MachineInstr *, 8> DbgValueSinkCandidates;
2776fe013be4SDimitry Andric // This map is used to track the relationship between a DBG_VALUE or
2777fe013be4SDimitry Andric // DBG_VALUE_LIST MachineInstr pointer and Registers that it uses.
2778fe013be4SDimitry Andric SmallDenseMap<MachineInstr *, SmallVector<Register>, 8> InstrMap;
2779fe013be4SDimitry Andric for (MBBI = MBB->begin(), E = MBB->end(); MBBI != E; ++MBBI) {
2780fe013be4SDimitry Andric MachineInstr &MI = *MBBI;
2781fe013be4SDimitry Andric
2782fe013be4SDimitry Andric auto PopulateRegisterAndInstrMapForDebugInstr = [&](Register Reg) {
2783fe013be4SDimitry Andric auto RegIt = RegisterMap.find(Reg);
2784fe013be4SDimitry Andric if (RegIt == RegisterMap.end())
2785fe013be4SDimitry Andric return;
2786fe013be4SDimitry Andric auto &InstrVec = RegIt->getSecond();
2787fe013be4SDimitry Andric InstrVec.push_back(&MI);
2788fe013be4SDimitry Andric InstrMap[&MI].push_back(Reg);
2789fe013be4SDimitry Andric };
2790fe013be4SDimitry Andric
2791fe013be4SDimitry Andric if (MI.isDebugValue()) {
2792fe013be4SDimitry Andric assert(MI.getDebugVariable() &&
2793fe013be4SDimitry Andric "DBG_VALUE or DBG_VALUE_LIST must contain a DILocalVariable");
2794fe013be4SDimitry Andric
2795fe013be4SDimitry Andric auto DbgVar = createDebugVariableFromMachineInstr(&MI);
2796fe013be4SDimitry Andric // If the first operand is a register and it exists in the RegisterMap, we
2797fe013be4SDimitry Andric // know this is a DBG_VALUE that uses the result of a load that was moved,
2798fe013be4SDimitry Andric // and is therefore a candidate to also be moved, add it to the
2799fe013be4SDimitry Andric // RegisterMap and InstrMap.
2800fe013be4SDimitry Andric forEachDbgRegOperand(&MI, [&](MachineOperand &Op) {
2801fe013be4SDimitry Andric PopulateRegisterAndInstrMapForDebugInstr(Op.getReg());
2802fe013be4SDimitry Andric });
2803fe013be4SDimitry Andric
2804fe013be4SDimitry Andric // If the current DBG_VALUE describes the same variable as one of the
2805fe013be4SDimitry Andric // in-flight DBG_VALUEs, remove the candidate from the list and set it to
2806fe013be4SDimitry Andric // undef. Moving one DBG_VALUE past another would result in the variable's
2807fe013be4SDimitry Andric // value going back in time when stepping through the block in the
2808fe013be4SDimitry Andric // debugger.
2809fe013be4SDimitry Andric auto InstrIt = DbgValueSinkCandidates.find(DbgVar);
2810fe013be4SDimitry Andric if (InstrIt != DbgValueSinkCandidates.end()) {
2811fe013be4SDimitry Andric auto *Instr = InstrIt->getSecond();
2812fe013be4SDimitry Andric auto RegIt = InstrMap.find(Instr);
2813fe013be4SDimitry Andric if (RegIt != InstrMap.end()) {
2814fe013be4SDimitry Andric const auto &RegVec = RegIt->getSecond();
2815fe013be4SDimitry Andric // For every Register in the RegVec, remove the MachineInstr in the
2816fe013be4SDimitry Andric // RegisterMap that describes the DbgVar.
2817fe013be4SDimitry Andric for (auto &Reg : RegVec) {
2818fe013be4SDimitry Andric auto RegIt = RegisterMap.find(Reg);
2819fe013be4SDimitry Andric if (RegIt == RegisterMap.end())
2820fe013be4SDimitry Andric continue;
2821fe013be4SDimitry Andric auto &InstrVec = RegIt->getSecond();
2822fe013be4SDimitry Andric auto IsDbgVar = [&](MachineInstr *I) -> bool {
2823fe013be4SDimitry Andric auto Var = createDebugVariableFromMachineInstr(I);
2824fe013be4SDimitry Andric return Var == DbgVar;
2825fe013be4SDimitry Andric };
2826fe013be4SDimitry Andric
2827c9157d92SDimitry Andric llvm::erase_if(InstrVec, IsDbgVar);
2828fe013be4SDimitry Andric }
2829fe013be4SDimitry Andric forEachDbgRegOperand(Instr,
2830fe013be4SDimitry Andric [&](MachineOperand &Op) { Op.setReg(0); });
2831fe013be4SDimitry Andric }
2832fe013be4SDimitry Andric }
2833fe013be4SDimitry Andric DbgValueSinkCandidates[DbgVar] = &MI;
2834fe013be4SDimitry Andric } else {
2835fe013be4SDimitry Andric // If the first operand of a load matches with a DBG_VALUE in RegisterMap,
2836fe013be4SDimitry Andric // then move that DBG_VALUE to below the load.
2837fe013be4SDimitry Andric auto Opc = MI.getOpcode();
2838fe013be4SDimitry Andric if (!isLoadSingle(Opc))
2839fe013be4SDimitry Andric continue;
2840fe013be4SDimitry Andric auto Reg = MI.getOperand(0).getReg();
2841fe013be4SDimitry Andric auto RegIt = RegisterMap.find(Reg);
2842fe013be4SDimitry Andric if (RegIt == RegisterMap.end())
2843fe013be4SDimitry Andric continue;
2844fe013be4SDimitry Andric auto &DbgInstrVec = RegIt->getSecond();
2845fe013be4SDimitry Andric if (!DbgInstrVec.size())
2846fe013be4SDimitry Andric continue;
2847fe013be4SDimitry Andric for (auto *DbgInstr : DbgInstrVec) {
2848fe013be4SDimitry Andric MachineBasicBlock::iterator InsertPos = std::next(MBBI);
2849fe013be4SDimitry Andric auto *ClonedMI = MI.getMF()->CloneMachineInstr(DbgInstr);
2850fe013be4SDimitry Andric MBB->insert(InsertPos, ClonedMI);
2851fe013be4SDimitry Andric MBBI++;
2852fe013be4SDimitry Andric // Erase the entry into the DbgValueSinkCandidates for the DBG_VALUE
2853fe013be4SDimitry Andric // that was moved.
2854fe013be4SDimitry Andric auto DbgVar = createDebugVariableFromMachineInstr(DbgInstr);
2855fe013be4SDimitry Andric auto DbgIt = DbgValueSinkCandidates.find(DbgVar);
2856fe013be4SDimitry Andric // If the instruction is a DBG_VALUE_LIST, it may have already been
2857fe013be4SDimitry Andric // erased from the DbgValueSinkCandidates. Only erase if it exists in
2858fe013be4SDimitry Andric // the DbgValueSinkCandidates.
2859fe013be4SDimitry Andric if (DbgIt != DbgValueSinkCandidates.end())
2860fe013be4SDimitry Andric DbgValueSinkCandidates.erase(DbgIt);
2861fe013be4SDimitry Andric // Zero out original dbg instr
2862fe013be4SDimitry Andric forEachDbgRegOperand(DbgInstr,
2863fe013be4SDimitry Andric [&](MachineOperand &Op) { Op.setReg(0); });
2864fe013be4SDimitry Andric // Update RegisterMap with ClonedMI because it might have to be moved
2865fe013be4SDimitry Andric // again.
2866fe013be4SDimitry Andric if (DbgInstr->isDebugValueList())
2867fe013be4SDimitry Andric updateRegisterMapForDbgValueListAfterMove(RegisterMap, ClonedMI,
2868fe013be4SDimitry Andric DbgInstr);
2869fe013be4SDimitry Andric }
2870fe013be4SDimitry Andric }
2871fe013be4SDimitry Andric }
28720b57cec5SDimitry Andric return RetVal;
28730b57cec5SDimitry Andric }
28740b57cec5SDimitry Andric
28755ffd83dbSDimitry Andric // Get the Base register operand index from the memory access MachineInst if we
28765ffd83dbSDimitry Andric // should attempt to distribute postinc on it. Return -1 if not of a valid
28775ffd83dbSDimitry Andric // instruction type. If it returns an index, it is assumed that instruction is a
28785ffd83dbSDimitry Andric // r+i indexing mode, and getBaseOperandIndex() + 1 is the Offset index.
getBaseOperandIndex(MachineInstr & MI)28795ffd83dbSDimitry Andric static int getBaseOperandIndex(MachineInstr &MI) {
28805ffd83dbSDimitry Andric switch (MI.getOpcode()) {
28815ffd83dbSDimitry Andric case ARM::MVE_VLDRBS16:
28825ffd83dbSDimitry Andric case ARM::MVE_VLDRBS32:
28835ffd83dbSDimitry Andric case ARM::MVE_VLDRBU16:
28845ffd83dbSDimitry Andric case ARM::MVE_VLDRBU32:
28855ffd83dbSDimitry Andric case ARM::MVE_VLDRHS32:
28865ffd83dbSDimitry Andric case ARM::MVE_VLDRHU32:
28875ffd83dbSDimitry Andric case ARM::MVE_VLDRBU8:
28885ffd83dbSDimitry Andric case ARM::MVE_VLDRHU16:
28895ffd83dbSDimitry Andric case ARM::MVE_VLDRWU32:
28905ffd83dbSDimitry Andric case ARM::MVE_VSTRB16:
28915ffd83dbSDimitry Andric case ARM::MVE_VSTRB32:
28925ffd83dbSDimitry Andric case ARM::MVE_VSTRH32:
28935ffd83dbSDimitry Andric case ARM::MVE_VSTRBU8:
28945ffd83dbSDimitry Andric case ARM::MVE_VSTRHU16:
28955ffd83dbSDimitry Andric case ARM::MVE_VSTRWU32:
2896e8d8bef9SDimitry Andric case ARM::t2LDRHi8:
2897e8d8bef9SDimitry Andric case ARM::t2LDRHi12:
2898e8d8bef9SDimitry Andric case ARM::t2LDRSHi8:
2899e8d8bef9SDimitry Andric case ARM::t2LDRSHi12:
2900e8d8bef9SDimitry Andric case ARM::t2LDRBi8:
2901e8d8bef9SDimitry Andric case ARM::t2LDRBi12:
2902e8d8bef9SDimitry Andric case ARM::t2LDRSBi8:
2903e8d8bef9SDimitry Andric case ARM::t2LDRSBi12:
2904e8d8bef9SDimitry Andric case ARM::t2STRBi8:
2905e8d8bef9SDimitry Andric case ARM::t2STRBi12:
2906e8d8bef9SDimitry Andric case ARM::t2STRHi8:
2907e8d8bef9SDimitry Andric case ARM::t2STRHi12:
29085ffd83dbSDimitry Andric return 1;
2909e8d8bef9SDimitry Andric case ARM::MVE_VLDRBS16_post:
2910e8d8bef9SDimitry Andric case ARM::MVE_VLDRBS32_post:
2911e8d8bef9SDimitry Andric case ARM::MVE_VLDRBU16_post:
2912e8d8bef9SDimitry Andric case ARM::MVE_VLDRBU32_post:
2913e8d8bef9SDimitry Andric case ARM::MVE_VLDRHS32_post:
2914e8d8bef9SDimitry Andric case ARM::MVE_VLDRHU32_post:
2915e8d8bef9SDimitry Andric case ARM::MVE_VLDRBU8_post:
2916e8d8bef9SDimitry Andric case ARM::MVE_VLDRHU16_post:
2917e8d8bef9SDimitry Andric case ARM::MVE_VLDRWU32_post:
2918e8d8bef9SDimitry Andric case ARM::MVE_VSTRB16_post:
2919e8d8bef9SDimitry Andric case ARM::MVE_VSTRB32_post:
2920e8d8bef9SDimitry Andric case ARM::MVE_VSTRH32_post:
2921e8d8bef9SDimitry Andric case ARM::MVE_VSTRBU8_post:
2922e8d8bef9SDimitry Andric case ARM::MVE_VSTRHU16_post:
2923e8d8bef9SDimitry Andric case ARM::MVE_VSTRWU32_post:
2924e8d8bef9SDimitry Andric case ARM::MVE_VLDRBS16_pre:
2925e8d8bef9SDimitry Andric case ARM::MVE_VLDRBS32_pre:
2926e8d8bef9SDimitry Andric case ARM::MVE_VLDRBU16_pre:
2927e8d8bef9SDimitry Andric case ARM::MVE_VLDRBU32_pre:
2928e8d8bef9SDimitry Andric case ARM::MVE_VLDRHS32_pre:
2929e8d8bef9SDimitry Andric case ARM::MVE_VLDRHU32_pre:
2930e8d8bef9SDimitry Andric case ARM::MVE_VLDRBU8_pre:
2931e8d8bef9SDimitry Andric case ARM::MVE_VLDRHU16_pre:
2932e8d8bef9SDimitry Andric case ARM::MVE_VLDRWU32_pre:
2933e8d8bef9SDimitry Andric case ARM::MVE_VSTRB16_pre:
2934e8d8bef9SDimitry Andric case ARM::MVE_VSTRB32_pre:
2935e8d8bef9SDimitry Andric case ARM::MVE_VSTRH32_pre:
2936e8d8bef9SDimitry Andric case ARM::MVE_VSTRBU8_pre:
2937e8d8bef9SDimitry Andric case ARM::MVE_VSTRHU16_pre:
2938e8d8bef9SDimitry Andric case ARM::MVE_VSTRWU32_pre:
2939e8d8bef9SDimitry Andric return 2;
29405ffd83dbSDimitry Andric }
29415ffd83dbSDimitry Andric return -1;
29425ffd83dbSDimitry Andric }
29435ffd83dbSDimitry Andric
isPostIndex(MachineInstr & MI)2944e8d8bef9SDimitry Andric static bool isPostIndex(MachineInstr &MI) {
2945e8d8bef9SDimitry Andric switch (MI.getOpcode()) {
2946e8d8bef9SDimitry Andric case ARM::MVE_VLDRBS16_post:
2947e8d8bef9SDimitry Andric case ARM::MVE_VLDRBS32_post:
2948e8d8bef9SDimitry Andric case ARM::MVE_VLDRBU16_post:
2949e8d8bef9SDimitry Andric case ARM::MVE_VLDRBU32_post:
2950e8d8bef9SDimitry Andric case ARM::MVE_VLDRHS32_post:
2951e8d8bef9SDimitry Andric case ARM::MVE_VLDRHU32_post:
2952e8d8bef9SDimitry Andric case ARM::MVE_VLDRBU8_post:
2953e8d8bef9SDimitry Andric case ARM::MVE_VLDRHU16_post:
2954e8d8bef9SDimitry Andric case ARM::MVE_VLDRWU32_post:
2955e8d8bef9SDimitry Andric case ARM::MVE_VSTRB16_post:
2956e8d8bef9SDimitry Andric case ARM::MVE_VSTRB32_post:
2957e8d8bef9SDimitry Andric case ARM::MVE_VSTRH32_post:
2958e8d8bef9SDimitry Andric case ARM::MVE_VSTRBU8_post:
2959e8d8bef9SDimitry Andric case ARM::MVE_VSTRHU16_post:
2960e8d8bef9SDimitry Andric case ARM::MVE_VSTRWU32_post:
2961e8d8bef9SDimitry Andric return true;
2962e8d8bef9SDimitry Andric }
2963e8d8bef9SDimitry Andric return false;
2964e8d8bef9SDimitry Andric }
2965e8d8bef9SDimitry Andric
isPreIndex(MachineInstr & MI)2966e8d8bef9SDimitry Andric static bool isPreIndex(MachineInstr &MI) {
2967e8d8bef9SDimitry Andric switch (MI.getOpcode()) {
2968e8d8bef9SDimitry Andric case ARM::MVE_VLDRBS16_pre:
2969e8d8bef9SDimitry Andric case ARM::MVE_VLDRBS32_pre:
2970e8d8bef9SDimitry Andric case ARM::MVE_VLDRBU16_pre:
2971e8d8bef9SDimitry Andric case ARM::MVE_VLDRBU32_pre:
2972e8d8bef9SDimitry Andric case ARM::MVE_VLDRHS32_pre:
2973e8d8bef9SDimitry Andric case ARM::MVE_VLDRHU32_pre:
2974e8d8bef9SDimitry Andric case ARM::MVE_VLDRBU8_pre:
2975e8d8bef9SDimitry Andric case ARM::MVE_VLDRHU16_pre:
2976e8d8bef9SDimitry Andric case ARM::MVE_VLDRWU32_pre:
2977e8d8bef9SDimitry Andric case ARM::MVE_VSTRB16_pre:
2978e8d8bef9SDimitry Andric case ARM::MVE_VSTRB32_pre:
2979e8d8bef9SDimitry Andric case ARM::MVE_VSTRH32_pre:
2980e8d8bef9SDimitry Andric case ARM::MVE_VSTRBU8_pre:
2981e8d8bef9SDimitry Andric case ARM::MVE_VSTRHU16_pre:
2982e8d8bef9SDimitry Andric case ARM::MVE_VSTRWU32_pre:
2983e8d8bef9SDimitry Andric return true;
2984e8d8bef9SDimitry Andric }
2985e8d8bef9SDimitry Andric return false;
2986e8d8bef9SDimitry Andric }
2987e8d8bef9SDimitry Andric
2988e8d8bef9SDimitry Andric // Given a memory access Opcode, check that the give Imm would be a valid Offset
2989e8d8bef9SDimitry Andric // for this instruction (same as isLegalAddressImm), Or if the instruction
2990e8d8bef9SDimitry Andric // could be easily converted to one where that was valid. For example converting
2991e8d8bef9SDimitry Andric // t2LDRi12 to t2LDRi8 for negative offsets. Works in conjunction with
2992e8d8bef9SDimitry Andric // AdjustBaseAndOffset below.
isLegalOrConvertableAddressImm(unsigned Opcode,int Imm,const TargetInstrInfo * TII,int & CodesizeEstimate)2993e8d8bef9SDimitry Andric static bool isLegalOrConvertableAddressImm(unsigned Opcode, int Imm,
2994e8d8bef9SDimitry Andric const TargetInstrInfo *TII,
2995e8d8bef9SDimitry Andric int &CodesizeEstimate) {
2996e8d8bef9SDimitry Andric if (isLegalAddressImm(Opcode, Imm, TII))
2997e8d8bef9SDimitry Andric return true;
2998e8d8bef9SDimitry Andric
29994824e7fdSDimitry Andric // We can convert AddrModeT2_i12 to AddrModeT2_i8neg.
3000e8d8bef9SDimitry Andric const MCInstrDesc &Desc = TII->get(Opcode);
3001e8d8bef9SDimitry Andric unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
3002e8d8bef9SDimitry Andric switch (AddrMode) {
3003e8d8bef9SDimitry Andric case ARMII::AddrModeT2_i12:
3004e8d8bef9SDimitry Andric CodesizeEstimate += 1;
30054824e7fdSDimitry Andric return Imm < 0 && -Imm < ((1 << 8) * 1);
3006e8d8bef9SDimitry Andric }
3007e8d8bef9SDimitry Andric return false;
3008e8d8bef9SDimitry Andric }
3009e8d8bef9SDimitry Andric
3010e8d8bef9SDimitry Andric // Given an MI adjust its address BaseReg to use NewBaseReg and address offset
3011e8d8bef9SDimitry Andric // by -Offset. This can either happen in-place or be a replacement as MI is
3012e8d8bef9SDimitry Andric // converted to another instruction type.
AdjustBaseAndOffset(MachineInstr * MI,Register NewBaseReg,int Offset,const TargetInstrInfo * TII,const TargetRegisterInfo * TRI)3013e8d8bef9SDimitry Andric static void AdjustBaseAndOffset(MachineInstr *MI, Register NewBaseReg,
3014fe6060f1SDimitry Andric int Offset, const TargetInstrInfo *TII,
3015fe6060f1SDimitry Andric const TargetRegisterInfo *TRI) {
3016fe6060f1SDimitry Andric // Set the Base reg
3017e8d8bef9SDimitry Andric unsigned BaseOp = getBaseOperandIndex(*MI);
3018e8d8bef9SDimitry Andric MI->getOperand(BaseOp).setReg(NewBaseReg);
3019fe6060f1SDimitry Andric // and constrain the reg class to that required by the instruction.
3020fe6060f1SDimitry Andric MachineFunction *MF = MI->getMF();
3021fe6060f1SDimitry Andric MachineRegisterInfo &MRI = MF->getRegInfo();
3022fe6060f1SDimitry Andric const MCInstrDesc &MCID = TII->get(MI->getOpcode());
3023fe6060f1SDimitry Andric const TargetRegisterClass *TRC = TII->getRegClass(MCID, BaseOp, TRI, *MF);
3024fe6060f1SDimitry Andric MRI.constrainRegClass(NewBaseReg, TRC);
3025fe6060f1SDimitry Andric
3026e8d8bef9SDimitry Andric int OldOffset = MI->getOperand(BaseOp + 1).getImm();
3027e8d8bef9SDimitry Andric if (isLegalAddressImm(MI->getOpcode(), OldOffset - Offset, TII))
3028e8d8bef9SDimitry Andric MI->getOperand(BaseOp + 1).setImm(OldOffset - Offset);
3029e8d8bef9SDimitry Andric else {
3030e8d8bef9SDimitry Andric unsigned ConvOpcode;
3031e8d8bef9SDimitry Andric switch (MI->getOpcode()) {
3032e8d8bef9SDimitry Andric case ARM::t2LDRHi12:
3033e8d8bef9SDimitry Andric ConvOpcode = ARM::t2LDRHi8;
3034e8d8bef9SDimitry Andric break;
3035e8d8bef9SDimitry Andric case ARM::t2LDRSHi12:
3036e8d8bef9SDimitry Andric ConvOpcode = ARM::t2LDRSHi8;
3037e8d8bef9SDimitry Andric break;
3038e8d8bef9SDimitry Andric case ARM::t2LDRBi12:
3039e8d8bef9SDimitry Andric ConvOpcode = ARM::t2LDRBi8;
3040e8d8bef9SDimitry Andric break;
3041e8d8bef9SDimitry Andric case ARM::t2LDRSBi12:
3042e8d8bef9SDimitry Andric ConvOpcode = ARM::t2LDRSBi8;
3043e8d8bef9SDimitry Andric break;
3044e8d8bef9SDimitry Andric case ARM::t2STRHi12:
3045e8d8bef9SDimitry Andric ConvOpcode = ARM::t2STRHi8;
3046e8d8bef9SDimitry Andric break;
3047e8d8bef9SDimitry Andric case ARM::t2STRBi12:
3048e8d8bef9SDimitry Andric ConvOpcode = ARM::t2STRBi8;
3049e8d8bef9SDimitry Andric break;
3050e8d8bef9SDimitry Andric default:
3051e8d8bef9SDimitry Andric llvm_unreachable("Unhandled convertable opcode");
3052e8d8bef9SDimitry Andric }
3053e8d8bef9SDimitry Andric assert(isLegalAddressImm(ConvOpcode, OldOffset - Offset, TII) &&
3054e8d8bef9SDimitry Andric "Illegal Address Immediate after convert!");
3055e8d8bef9SDimitry Andric
3056e8d8bef9SDimitry Andric const MCInstrDesc &MCID = TII->get(ConvOpcode);
3057e8d8bef9SDimitry Andric BuildMI(*MI->getParent(), MI, MI->getDebugLoc(), MCID)
3058e8d8bef9SDimitry Andric .add(MI->getOperand(0))
3059e8d8bef9SDimitry Andric .add(MI->getOperand(1))
3060e8d8bef9SDimitry Andric .addImm(OldOffset - Offset)
3061e8d8bef9SDimitry Andric .add(MI->getOperand(3))
3062e8d8bef9SDimitry Andric .add(MI->getOperand(4))
3063e8d8bef9SDimitry Andric .cloneMemRefs(*MI);
3064e8d8bef9SDimitry Andric MI->eraseFromParent();
3065e8d8bef9SDimitry Andric }
3066e8d8bef9SDimitry Andric }
3067e8d8bef9SDimitry Andric
createPostIncLoadStore(MachineInstr * MI,int Offset,Register NewReg,const TargetInstrInfo * TII,const TargetRegisterInfo * TRI)30685ffd83dbSDimitry Andric static MachineInstr *createPostIncLoadStore(MachineInstr *MI, int Offset,
30695ffd83dbSDimitry Andric Register NewReg,
30705ffd83dbSDimitry Andric const TargetInstrInfo *TII,
30715ffd83dbSDimitry Andric const TargetRegisterInfo *TRI) {
30725ffd83dbSDimitry Andric MachineFunction *MF = MI->getMF();
30735ffd83dbSDimitry Andric MachineRegisterInfo &MRI = MF->getRegInfo();
30745ffd83dbSDimitry Andric
30755ffd83dbSDimitry Andric unsigned NewOpcode = getPostIndexedLoadStoreOpcode(
30765ffd83dbSDimitry Andric MI->getOpcode(), Offset > 0 ? ARM_AM::add : ARM_AM::sub);
30775ffd83dbSDimitry Andric
30785ffd83dbSDimitry Andric const MCInstrDesc &MCID = TII->get(NewOpcode);
30795ffd83dbSDimitry Andric // Constrain the def register class
30805ffd83dbSDimitry Andric const TargetRegisterClass *TRC = TII->getRegClass(MCID, 0, TRI, *MF);
30815ffd83dbSDimitry Andric MRI.constrainRegClass(NewReg, TRC);
30825ffd83dbSDimitry Andric // And do the same for the base operand
30835ffd83dbSDimitry Andric TRC = TII->getRegClass(MCID, 2, TRI, *MF);
30845ffd83dbSDimitry Andric MRI.constrainRegClass(MI->getOperand(1).getReg(), TRC);
30855ffd83dbSDimitry Andric
3086e8d8bef9SDimitry Andric unsigned AddrMode = (MCID.TSFlags & ARMII::AddrModeMask);
3087e8d8bef9SDimitry Andric switch (AddrMode) {
3088e8d8bef9SDimitry Andric case ARMII::AddrModeT2_i7:
3089e8d8bef9SDimitry Andric case ARMII::AddrModeT2_i7s2:
3090e8d8bef9SDimitry Andric case ARMII::AddrModeT2_i7s4:
3091e8d8bef9SDimitry Andric // Any MVE load/store
3092e8d8bef9SDimitry Andric return BuildMI(*MI->getParent(), MI, MI->getDebugLoc(), MCID)
3093e8d8bef9SDimitry Andric .addReg(NewReg, RegState::Define)
3094e8d8bef9SDimitry Andric .add(MI->getOperand(0))
3095e8d8bef9SDimitry Andric .add(MI->getOperand(1))
3096e8d8bef9SDimitry Andric .addImm(Offset)
3097e8d8bef9SDimitry Andric .add(MI->getOperand(3))
3098e8d8bef9SDimitry Andric .add(MI->getOperand(4))
3099349cc55cSDimitry Andric .add(MI->getOperand(5))
3100e8d8bef9SDimitry Andric .cloneMemRefs(*MI);
3101e8d8bef9SDimitry Andric case ARMII::AddrModeT2_i8:
3102e8d8bef9SDimitry Andric if (MI->mayLoad()) {
3103e8d8bef9SDimitry Andric return BuildMI(*MI->getParent(), MI, MI->getDebugLoc(), MCID)
3104e8d8bef9SDimitry Andric .add(MI->getOperand(0))
3105e8d8bef9SDimitry Andric .addReg(NewReg, RegState::Define)
3106e8d8bef9SDimitry Andric .add(MI->getOperand(1))
3107e8d8bef9SDimitry Andric .addImm(Offset)
3108e8d8bef9SDimitry Andric .add(MI->getOperand(3))
3109e8d8bef9SDimitry Andric .add(MI->getOperand(4))
3110e8d8bef9SDimitry Andric .cloneMemRefs(*MI);
3111e8d8bef9SDimitry Andric } else {
31125ffd83dbSDimitry Andric return BuildMI(*MI->getParent(), MI, MI->getDebugLoc(), MCID)
31135ffd83dbSDimitry Andric .addReg(NewReg, RegState::Define)
31145ffd83dbSDimitry Andric .add(MI->getOperand(0))
31155ffd83dbSDimitry Andric .add(MI->getOperand(1))
31165ffd83dbSDimitry Andric .addImm(Offset)
31175ffd83dbSDimitry Andric .add(MI->getOperand(3))
31185ffd83dbSDimitry Andric .add(MI->getOperand(4))
31195ffd83dbSDimitry Andric .cloneMemRefs(*MI);
31205ffd83dbSDimitry Andric }
3121e8d8bef9SDimitry Andric default:
3122e8d8bef9SDimitry Andric llvm_unreachable("Unhandled createPostIncLoadStore");
3123e8d8bef9SDimitry Andric }
3124e8d8bef9SDimitry Andric }
31255ffd83dbSDimitry Andric
31265ffd83dbSDimitry Andric // Given a Base Register, optimise the load/store uses to attempt to create more
3127e8d8bef9SDimitry Andric // post-inc accesses and less register moves. We do this by taking zero offset
3128e8d8bef9SDimitry Andric // loads/stores with an add, and convert them to a postinc load/store of the
3129e8d8bef9SDimitry Andric // same type. Any subsequent accesses will be adjusted to use and account for
3130e8d8bef9SDimitry Andric // the post-inc value.
31315ffd83dbSDimitry Andric // For example:
31325ffd83dbSDimitry Andric // LDR #0 LDR_POSTINC #16
31335ffd83dbSDimitry Andric // LDR #4 LDR #-12
31345ffd83dbSDimitry Andric // LDR #8 LDR #-8
31355ffd83dbSDimitry Andric // LDR #12 LDR #-4
31365ffd83dbSDimitry Andric // ADD #16
3137e8d8bef9SDimitry Andric //
3138e8d8bef9SDimitry Andric // At the same time if we do not find an increment but do find an existing
3139e8d8bef9SDimitry Andric // pre/post inc instruction, we can still adjust the offsets of subsequent
3140e8d8bef9SDimitry Andric // instructions to save the register move that would otherwise be needed for the
3141e8d8bef9SDimitry Andric // in-place increment.
DistributeIncrements(Register Base)31425ffd83dbSDimitry Andric bool ARMPreAllocLoadStoreOpt::DistributeIncrements(Register Base) {
31435ffd83dbSDimitry Andric // We are looking for:
31445ffd83dbSDimitry Andric // One zero offset load/store that can become postinc
31455ffd83dbSDimitry Andric MachineInstr *BaseAccess = nullptr;
3146e8d8bef9SDimitry Andric MachineInstr *PrePostInc = nullptr;
31475ffd83dbSDimitry Andric // An increment that can be folded in
31485ffd83dbSDimitry Andric MachineInstr *Increment = nullptr;
31495ffd83dbSDimitry Andric // Other accesses after BaseAccess that will need to be updated to use the
3150e8d8bef9SDimitry Andric // postinc value.
31515ffd83dbSDimitry Andric SmallPtrSet<MachineInstr *, 8> OtherAccesses;
31525ffd83dbSDimitry Andric for (auto &Use : MRI->use_nodbg_instructions(Base)) {
31535ffd83dbSDimitry Andric if (!Increment && getAddSubImmediate(Use) != 0) {
31545ffd83dbSDimitry Andric Increment = &Use;
31555ffd83dbSDimitry Andric continue;
31565ffd83dbSDimitry Andric }
31575ffd83dbSDimitry Andric
31585ffd83dbSDimitry Andric int BaseOp = getBaseOperandIndex(Use);
31595ffd83dbSDimitry Andric if (BaseOp == -1)
31605ffd83dbSDimitry Andric return false;
31615ffd83dbSDimitry Andric
31625ffd83dbSDimitry Andric if (!Use.getOperand(BaseOp).isReg() ||
31635ffd83dbSDimitry Andric Use.getOperand(BaseOp).getReg() != Base)
31645ffd83dbSDimitry Andric return false;
3165e8d8bef9SDimitry Andric if (isPreIndex(Use) || isPostIndex(Use))
3166e8d8bef9SDimitry Andric PrePostInc = &Use;
3167e8d8bef9SDimitry Andric else if (Use.getOperand(BaseOp + 1).getImm() == 0)
31685ffd83dbSDimitry Andric BaseAccess = &Use;
31695ffd83dbSDimitry Andric else
31705ffd83dbSDimitry Andric OtherAccesses.insert(&Use);
31715ffd83dbSDimitry Andric }
31725ffd83dbSDimitry Andric
3173e8d8bef9SDimitry Andric int IncrementOffset;
3174e8d8bef9SDimitry Andric Register NewBaseReg;
3175e8d8bef9SDimitry Andric if (BaseAccess && Increment) {
3176e8d8bef9SDimitry Andric if (PrePostInc || BaseAccess->getParent() != Increment->getParent())
31775ffd83dbSDimitry Andric return false;
31785ffd83dbSDimitry Andric Register PredReg;
31795ffd83dbSDimitry Andric if (Increment->definesRegister(ARM::CPSR) ||
31805ffd83dbSDimitry Andric getInstrPredicate(*Increment, PredReg) != ARMCC::AL)
31815ffd83dbSDimitry Andric return false;
31825ffd83dbSDimitry Andric
31835ffd83dbSDimitry Andric LLVM_DEBUG(dbgs() << "\nAttempting to distribute increments on VirtualReg "
31845ffd83dbSDimitry Andric << Base.virtRegIndex() << "\n");
31855ffd83dbSDimitry Andric
318681ad6265SDimitry Andric // Make sure that Increment has no uses before BaseAccess that are not PHI
318781ad6265SDimitry Andric // uses.
31885ffd83dbSDimitry Andric for (MachineInstr &Use :
31895ffd83dbSDimitry Andric MRI->use_nodbg_instructions(Increment->getOperand(0).getReg())) {
319081ad6265SDimitry Andric if (&Use == BaseAccess || (Use.getOpcode() != TargetOpcode::PHI &&
319181ad6265SDimitry Andric !DT->dominates(BaseAccess, &Use))) {
31925ffd83dbSDimitry Andric LLVM_DEBUG(dbgs() << " BaseAccess doesn't dominate use of increment\n");
31935ffd83dbSDimitry Andric return false;
31945ffd83dbSDimitry Andric }
31955ffd83dbSDimitry Andric }
31965ffd83dbSDimitry Andric
31975ffd83dbSDimitry Andric // Make sure that Increment can be folded into Base
3198e8d8bef9SDimitry Andric IncrementOffset = getAddSubImmediate(*Increment);
31995ffd83dbSDimitry Andric unsigned NewPostIncOpcode = getPostIndexedLoadStoreOpcode(
32005ffd83dbSDimitry Andric BaseAccess->getOpcode(), IncrementOffset > 0 ? ARM_AM::add : ARM_AM::sub);
32015ffd83dbSDimitry Andric if (!isLegalAddressImm(NewPostIncOpcode, IncrementOffset, TII)) {
32025ffd83dbSDimitry Andric LLVM_DEBUG(dbgs() << " Illegal addressing mode immediate on postinc\n");
32035ffd83dbSDimitry Andric return false;
32045ffd83dbSDimitry Andric }
3205e8d8bef9SDimitry Andric }
3206e8d8bef9SDimitry Andric else if (PrePostInc) {
3207e8d8bef9SDimitry Andric // If we already have a pre/post index load/store then set BaseAccess,
3208e8d8bef9SDimitry Andric // IncrementOffset and NewBaseReg to the values it already produces,
3209e8d8bef9SDimitry Andric // allowing us to update and subsequent uses of BaseOp reg with the
3210e8d8bef9SDimitry Andric // incremented value.
3211e8d8bef9SDimitry Andric if (Increment)
3212e8d8bef9SDimitry Andric return false;
3213e8d8bef9SDimitry Andric
3214e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "\nAttempting to distribute increments on already "
3215e8d8bef9SDimitry Andric << "indexed VirtualReg " << Base.virtRegIndex() << "\n");
3216e8d8bef9SDimitry Andric int BaseOp = getBaseOperandIndex(*PrePostInc);
3217e8d8bef9SDimitry Andric IncrementOffset = PrePostInc->getOperand(BaseOp+1).getImm();
3218e8d8bef9SDimitry Andric BaseAccess = PrePostInc;
3219e8d8bef9SDimitry Andric NewBaseReg = PrePostInc->getOperand(0).getReg();
3220e8d8bef9SDimitry Andric }
3221e8d8bef9SDimitry Andric else
3222e8d8bef9SDimitry Andric return false;
32235ffd83dbSDimitry Andric
32245ffd83dbSDimitry Andric // And make sure that the negative value of increment can be added to all
32255ffd83dbSDimitry Andric // other offsets after the BaseAccess. We rely on either
32265ffd83dbSDimitry Andric // dominates(BaseAccess, OtherAccess) or dominates(OtherAccess, BaseAccess)
32275ffd83dbSDimitry Andric // to keep things simple.
3228e8d8bef9SDimitry Andric // This also adds a simple codesize metric, to detect if an instruction (like
3229e8d8bef9SDimitry Andric // t2LDRBi12) which can often be shrunk to a thumb1 instruction (tLDRBi)
3230e8d8bef9SDimitry Andric // cannot because it is converted to something else (t2LDRBi8). We start this
3231e8d8bef9SDimitry Andric // at -1 for the gain from removing the increment.
32325ffd83dbSDimitry Andric SmallPtrSet<MachineInstr *, 4> SuccessorAccesses;
3233e8d8bef9SDimitry Andric int CodesizeEstimate = -1;
32345ffd83dbSDimitry Andric for (auto *Use : OtherAccesses) {
32355ffd83dbSDimitry Andric if (DT->dominates(BaseAccess, Use)) {
32365ffd83dbSDimitry Andric SuccessorAccesses.insert(Use);
32375ffd83dbSDimitry Andric unsigned BaseOp = getBaseOperandIndex(*Use);
3238e8d8bef9SDimitry Andric if (!isLegalOrConvertableAddressImm(Use->getOpcode(),
3239e8d8bef9SDimitry Andric Use->getOperand(BaseOp + 1).getImm() -
3240e8d8bef9SDimitry Andric IncrementOffset,
3241e8d8bef9SDimitry Andric TII, CodesizeEstimate)) {
32425ffd83dbSDimitry Andric LLVM_DEBUG(dbgs() << " Illegal addressing mode immediate on use\n");
32435ffd83dbSDimitry Andric return false;
32445ffd83dbSDimitry Andric }
32455ffd83dbSDimitry Andric } else if (!DT->dominates(Use, BaseAccess)) {
32465ffd83dbSDimitry Andric LLVM_DEBUG(
32475ffd83dbSDimitry Andric dbgs() << " Unknown dominance relation between Base and Use\n");
32485ffd83dbSDimitry Andric return false;
32495ffd83dbSDimitry Andric }
32505ffd83dbSDimitry Andric }
3251e8d8bef9SDimitry Andric if (STI->hasMinSize() && CodesizeEstimate > 0) {
3252e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << " Expected to grow instructions under minsize\n");
3253e8d8bef9SDimitry Andric return false;
3254e8d8bef9SDimitry Andric }
32555ffd83dbSDimitry Andric
3256e8d8bef9SDimitry Andric if (!PrePostInc) {
32575ffd83dbSDimitry Andric // Replace BaseAccess with a post inc
32585ffd83dbSDimitry Andric LLVM_DEBUG(dbgs() << "Changing: "; BaseAccess->dump());
32595ffd83dbSDimitry Andric LLVM_DEBUG(dbgs() << " And : "; Increment->dump());
3260e8d8bef9SDimitry Andric NewBaseReg = Increment->getOperand(0).getReg();
32615ffd83dbSDimitry Andric MachineInstr *BaseAccessPost =
32625ffd83dbSDimitry Andric createPostIncLoadStore(BaseAccess, IncrementOffset, NewBaseReg, TII, TRI);
32635ffd83dbSDimitry Andric BaseAccess->eraseFromParent();
32645ffd83dbSDimitry Andric Increment->eraseFromParent();
32655ffd83dbSDimitry Andric (void)BaseAccessPost;
32665ffd83dbSDimitry Andric LLVM_DEBUG(dbgs() << " To : "; BaseAccessPost->dump());
3267e8d8bef9SDimitry Andric }
32685ffd83dbSDimitry Andric
32695ffd83dbSDimitry Andric for (auto *Use : SuccessorAccesses) {
32705ffd83dbSDimitry Andric LLVM_DEBUG(dbgs() << "Changing: "; Use->dump());
3271fe6060f1SDimitry Andric AdjustBaseAndOffset(Use, NewBaseReg, IncrementOffset, TII, TRI);
32725ffd83dbSDimitry Andric LLVM_DEBUG(dbgs() << " To : "; Use->dump());
32735ffd83dbSDimitry Andric }
32745ffd83dbSDimitry Andric
32755ffd83dbSDimitry Andric // Remove the kill flag from all uses of NewBaseReg, in case any old uses
32765ffd83dbSDimitry Andric // remain.
32775ffd83dbSDimitry Andric for (MachineOperand &Op : MRI->use_nodbg_operands(NewBaseReg))
32785ffd83dbSDimitry Andric Op.setIsKill(false);
32795ffd83dbSDimitry Andric return true;
32805ffd83dbSDimitry Andric }
32815ffd83dbSDimitry Andric
DistributeIncrements()32825ffd83dbSDimitry Andric bool ARMPreAllocLoadStoreOpt::DistributeIncrements() {
32835ffd83dbSDimitry Andric bool Changed = false;
32845ffd83dbSDimitry Andric SmallSetVector<Register, 4> Visited;
32855ffd83dbSDimitry Andric for (auto &MBB : *MF) {
32865ffd83dbSDimitry Andric for (auto &MI : MBB) {
32875ffd83dbSDimitry Andric int BaseOp = getBaseOperandIndex(MI);
32885ffd83dbSDimitry Andric if (BaseOp == -1 || !MI.getOperand(BaseOp).isReg())
32895ffd83dbSDimitry Andric continue;
32905ffd83dbSDimitry Andric
32915ffd83dbSDimitry Andric Register Base = MI.getOperand(BaseOp).getReg();
32925ffd83dbSDimitry Andric if (!Base.isVirtual() || Visited.count(Base))
32935ffd83dbSDimitry Andric continue;
32945ffd83dbSDimitry Andric
32955ffd83dbSDimitry Andric Visited.insert(Base);
32965ffd83dbSDimitry Andric }
32975ffd83dbSDimitry Andric }
32985ffd83dbSDimitry Andric
32995ffd83dbSDimitry Andric for (auto Base : Visited)
33005ffd83dbSDimitry Andric Changed |= DistributeIncrements(Base);
33015ffd83dbSDimitry Andric
33025ffd83dbSDimitry Andric return Changed;
33035ffd83dbSDimitry Andric }
33045ffd83dbSDimitry Andric
33050b57cec5SDimitry Andric /// Returns an instance of the load / store optimization pass.
createARMLoadStoreOptimizationPass(bool PreAlloc)33060b57cec5SDimitry Andric FunctionPass *llvm::createARMLoadStoreOptimizationPass(bool PreAlloc) {
33070b57cec5SDimitry Andric if (PreAlloc)
33080b57cec5SDimitry Andric return new ARMPreAllocLoadStoreOpt();
33090b57cec5SDimitry Andric return new ARMLoadStoreOpt();
33100b57cec5SDimitry Andric }
3311