10b57cec5SDimitry Andric //=- AArch64RedundantCopyElimination.cpp - Remove useless copy for AArch64 -=//
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 // This pass removes unnecessary copies/moves in BBs based on a dominating
80b57cec5SDimitry Andric // condition.
90b57cec5SDimitry Andric //
100b57cec5SDimitry Andric // We handle three cases:
110b57cec5SDimitry Andric // 1. For BBs that are targets of CBZ/CBNZ instructions, we know the value of
120b57cec5SDimitry Andric // the CBZ/CBNZ source register is zero on the taken/not-taken path. For
130b57cec5SDimitry Andric // instance, the copy instruction in the code below can be removed because
140b57cec5SDimitry Andric // the CBZW jumps to %bb.2 when w0 is zero.
150b57cec5SDimitry Andric //
160b57cec5SDimitry Andric // %bb.1:
170b57cec5SDimitry Andric // cbz w0, .LBB0_2
180b57cec5SDimitry Andric // .LBB0_2:
190b57cec5SDimitry Andric // mov w0, wzr ; <-- redundant
200b57cec5SDimitry Andric //
210b57cec5SDimitry Andric // 2. If the flag setting instruction defines a register other than WZR/XZR, we
220b57cec5SDimitry Andric // can remove a zero copy in some cases.
230b57cec5SDimitry Andric //
240b57cec5SDimitry Andric // %bb.0:
250b57cec5SDimitry Andric // subs w0, w1, w2
260b57cec5SDimitry Andric // str w0, [x1]
270b57cec5SDimitry Andric // b.ne .LBB0_2
280b57cec5SDimitry Andric // %bb.1:
290b57cec5SDimitry Andric // mov w0, wzr ; <-- redundant
300b57cec5SDimitry Andric // str w0, [x2]
310b57cec5SDimitry Andric // .LBB0_2
320b57cec5SDimitry Andric //
330b57cec5SDimitry Andric // 3. Finally, if the flag setting instruction is a comparison against a
340b57cec5SDimitry Andric // constant (i.e., ADDS[W|X]ri, SUBS[W|X]ri), we can remove a mov immediate
350b57cec5SDimitry Andric // in some cases.
360b57cec5SDimitry Andric //
370b57cec5SDimitry Andric // %bb.0:
380b57cec5SDimitry Andric // subs xzr, x0, #1
390b57cec5SDimitry Andric // b.eq .LBB0_1
400b57cec5SDimitry Andric // .LBB0_1:
410b57cec5SDimitry Andric // orr x0, xzr, #0x1 ; <-- redundant
420b57cec5SDimitry Andric //
430b57cec5SDimitry Andric // This pass should be run after register allocation.
440b57cec5SDimitry Andric //
450b57cec5SDimitry Andric // FIXME: This could also be extended to check the whole dominance subtree below
460b57cec5SDimitry Andric // the comparison if the compile time regression is acceptable.
470b57cec5SDimitry Andric //
480b57cec5SDimitry Andric // FIXME: Add support for handling CCMP instructions.
490b57cec5SDimitry Andric // FIXME: If the known register value is zero, we should be able to rewrite uses
500b57cec5SDimitry Andric // to use WZR/XZR directly in some cases.
510b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
520b57cec5SDimitry Andric #include "AArch64.h"
530b57cec5SDimitry Andric #include "llvm/ADT/SetVector.h"
540b57cec5SDimitry Andric #include "llvm/ADT/Statistic.h"
550b57cec5SDimitry Andric #include "llvm/ADT/iterator_range.h"
560b57cec5SDimitry Andric #include "llvm/CodeGen/LiveRegUnits.h"
570b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h"
580b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
590b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
600b57cec5SDimitry Andric
610b57cec5SDimitry Andric using namespace llvm;
620b57cec5SDimitry Andric
630b57cec5SDimitry Andric #define DEBUG_TYPE "aarch64-copyelim"
640b57cec5SDimitry Andric
650b57cec5SDimitry Andric STATISTIC(NumCopiesRemoved, "Number of copies removed.");
660b57cec5SDimitry Andric
670b57cec5SDimitry Andric namespace {
680b57cec5SDimitry Andric class AArch64RedundantCopyElimination : public MachineFunctionPass {
690b57cec5SDimitry Andric const MachineRegisterInfo *MRI;
700b57cec5SDimitry Andric const TargetRegisterInfo *TRI;
710b57cec5SDimitry Andric
720b57cec5SDimitry Andric // DomBBClobberedRegs is used when computing known values in the dominating
730b57cec5SDimitry Andric // BB.
740b57cec5SDimitry Andric LiveRegUnits DomBBClobberedRegs, DomBBUsedRegs;
750b57cec5SDimitry Andric
760b57cec5SDimitry Andric // OptBBClobberedRegs is used when optimizing away redundant copies/moves.
770b57cec5SDimitry Andric LiveRegUnits OptBBClobberedRegs, OptBBUsedRegs;
780b57cec5SDimitry Andric
790b57cec5SDimitry Andric public:
800b57cec5SDimitry Andric static char ID;
AArch64RedundantCopyElimination()810b57cec5SDimitry Andric AArch64RedundantCopyElimination() : MachineFunctionPass(ID) {
820b57cec5SDimitry Andric initializeAArch64RedundantCopyEliminationPass(
830b57cec5SDimitry Andric *PassRegistry::getPassRegistry());
840b57cec5SDimitry Andric }
850b57cec5SDimitry Andric
860b57cec5SDimitry Andric struct RegImm {
870b57cec5SDimitry Andric MCPhysReg Reg;
880b57cec5SDimitry Andric int32_t Imm;
RegImm__anond63895000111::AArch64RedundantCopyElimination::RegImm890b57cec5SDimitry Andric RegImm(MCPhysReg Reg, int32_t Imm) : Reg(Reg), Imm(Imm) {}
900b57cec5SDimitry Andric };
910b57cec5SDimitry Andric
920b57cec5SDimitry Andric bool knownRegValInBlock(MachineInstr &CondBr, MachineBasicBlock *MBB,
930b57cec5SDimitry Andric SmallVectorImpl<RegImm> &KnownRegs,
940b57cec5SDimitry Andric MachineBasicBlock::iterator &FirstUse);
950b57cec5SDimitry Andric bool optimizeBlock(MachineBasicBlock *MBB);
960b57cec5SDimitry Andric bool runOnMachineFunction(MachineFunction &MF) override;
getRequiredProperties() const970b57cec5SDimitry Andric MachineFunctionProperties getRequiredProperties() const override {
980b57cec5SDimitry Andric return MachineFunctionProperties().set(
990b57cec5SDimitry Andric MachineFunctionProperties::Property::NoVRegs);
1000b57cec5SDimitry Andric }
getPassName() const1010b57cec5SDimitry Andric StringRef getPassName() const override {
1020b57cec5SDimitry Andric return "AArch64 Redundant Copy Elimination";
1030b57cec5SDimitry Andric }
1040b57cec5SDimitry Andric };
1050b57cec5SDimitry Andric char AArch64RedundantCopyElimination::ID = 0;
1060b57cec5SDimitry Andric }
1070b57cec5SDimitry Andric
1080b57cec5SDimitry Andric INITIALIZE_PASS(AArch64RedundantCopyElimination, "aarch64-copyelim",
1090b57cec5SDimitry Andric "AArch64 redundant copy elimination pass", false, false)
1100b57cec5SDimitry Andric
1110b57cec5SDimitry Andric /// It's possible to determine the value of a register based on a dominating
1120b57cec5SDimitry Andric /// condition. To do so, this function checks to see if the basic block \p MBB
1130b57cec5SDimitry Andric /// is the target of a conditional branch \p CondBr with an equality comparison.
1140b57cec5SDimitry Andric /// If the branch is a CBZ/CBNZ, we know the value of its source operand is zero
1150b57cec5SDimitry Andric /// in \p MBB for some cases. Otherwise, we find and inspect the NZCV setting
1160b57cec5SDimitry Andric /// instruction (e.g., SUBS, ADDS). If this instruction defines a register
1170b57cec5SDimitry Andric /// other than WZR/XZR, we know the value of the destination register is zero in
1180b57cec5SDimitry Andric /// \p MMB for some cases. In addition, if the NZCV setting instruction is
1190b57cec5SDimitry Andric /// comparing against a constant we know the other source register is equal to
1200b57cec5SDimitry Andric /// the constant in \p MBB for some cases. If we find any constant values, push
1210b57cec5SDimitry Andric /// a physical register and constant value pair onto the KnownRegs vector and
1220b57cec5SDimitry Andric /// return true. Otherwise, return false if no known values were found.
knownRegValInBlock(MachineInstr & CondBr,MachineBasicBlock * MBB,SmallVectorImpl<RegImm> & KnownRegs,MachineBasicBlock::iterator & FirstUse)1230b57cec5SDimitry Andric bool AArch64RedundantCopyElimination::knownRegValInBlock(
1240b57cec5SDimitry Andric MachineInstr &CondBr, MachineBasicBlock *MBB,
1250b57cec5SDimitry Andric SmallVectorImpl<RegImm> &KnownRegs, MachineBasicBlock::iterator &FirstUse) {
1260b57cec5SDimitry Andric unsigned Opc = CondBr.getOpcode();
1270b57cec5SDimitry Andric
1280b57cec5SDimitry Andric // Check if the current basic block is the target block to which the
1290b57cec5SDimitry Andric // CBZ/CBNZ instruction jumps when its Wt/Xt is zero.
1300b57cec5SDimitry Andric if (((Opc == AArch64::CBZW || Opc == AArch64::CBZX) &&
1310b57cec5SDimitry Andric MBB == CondBr.getOperand(1).getMBB()) ||
1320b57cec5SDimitry Andric ((Opc == AArch64::CBNZW || Opc == AArch64::CBNZX) &&
1330b57cec5SDimitry Andric MBB != CondBr.getOperand(1).getMBB())) {
1340b57cec5SDimitry Andric FirstUse = CondBr;
1350b57cec5SDimitry Andric KnownRegs.push_back(RegImm(CondBr.getOperand(0).getReg(), 0));
1360b57cec5SDimitry Andric return true;
1370b57cec5SDimitry Andric }
1380b57cec5SDimitry Andric
1390b57cec5SDimitry Andric // Otherwise, must be a conditional branch.
1400b57cec5SDimitry Andric if (Opc != AArch64::Bcc)
1410b57cec5SDimitry Andric return false;
1420b57cec5SDimitry Andric
1430b57cec5SDimitry Andric // Must be an equality check (i.e., == or !=).
1440b57cec5SDimitry Andric AArch64CC::CondCode CC = (AArch64CC::CondCode)CondBr.getOperand(0).getImm();
1450b57cec5SDimitry Andric if (CC != AArch64CC::EQ && CC != AArch64CC::NE)
1460b57cec5SDimitry Andric return false;
1470b57cec5SDimitry Andric
1480b57cec5SDimitry Andric MachineBasicBlock *BrTarget = CondBr.getOperand(1).getMBB();
1490b57cec5SDimitry Andric if ((CC == AArch64CC::EQ && BrTarget != MBB) ||
1500b57cec5SDimitry Andric (CC == AArch64CC::NE && BrTarget == MBB))
1510b57cec5SDimitry Andric return false;
1520b57cec5SDimitry Andric
1530b57cec5SDimitry Andric // Stop if we get to the beginning of PredMBB.
1540b57cec5SDimitry Andric MachineBasicBlock *PredMBB = *MBB->pred_begin();
1550b57cec5SDimitry Andric assert(PredMBB == CondBr.getParent() &&
1560b57cec5SDimitry Andric "Conditional branch not in predecessor block!");
1570b57cec5SDimitry Andric if (CondBr == PredMBB->begin())
1580b57cec5SDimitry Andric return false;
1590b57cec5SDimitry Andric
1600b57cec5SDimitry Andric // Registers clobbered in PredMBB between CondBr instruction and current
1610b57cec5SDimitry Andric // instruction being checked in loop.
1620b57cec5SDimitry Andric DomBBClobberedRegs.clear();
1630b57cec5SDimitry Andric DomBBUsedRegs.clear();
1640b57cec5SDimitry Andric
1650b57cec5SDimitry Andric // Find compare instruction that sets NZCV used by CondBr.
1660b57cec5SDimitry Andric MachineBasicBlock::reverse_iterator RIt = CondBr.getReverseIterator();
1670b57cec5SDimitry Andric for (MachineInstr &PredI : make_range(std::next(RIt), PredMBB->rend())) {
1680b57cec5SDimitry Andric
1690b57cec5SDimitry Andric bool IsCMN = false;
1700b57cec5SDimitry Andric switch (PredI.getOpcode()) {
1710b57cec5SDimitry Andric default:
1720b57cec5SDimitry Andric break;
1730b57cec5SDimitry Andric
1740b57cec5SDimitry Andric // CMN is an alias for ADDS with a dead destination register.
1750b57cec5SDimitry Andric case AArch64::ADDSWri:
1760b57cec5SDimitry Andric case AArch64::ADDSXri:
1770b57cec5SDimitry Andric IsCMN = true;
178bdd1243dSDimitry Andric [[fallthrough]];
1790b57cec5SDimitry Andric // CMP is an alias for SUBS with a dead destination register.
1800b57cec5SDimitry Andric case AArch64::SUBSWri:
1810b57cec5SDimitry Andric case AArch64::SUBSXri: {
1820b57cec5SDimitry Andric // Sometimes the first operand is a FrameIndex. Bail if tht happens.
1830b57cec5SDimitry Andric if (!PredI.getOperand(1).isReg())
1840b57cec5SDimitry Andric return false;
1850b57cec5SDimitry Andric MCPhysReg DstReg = PredI.getOperand(0).getReg();
1860b57cec5SDimitry Andric MCPhysReg SrcReg = PredI.getOperand(1).getReg();
1870b57cec5SDimitry Andric
1880b57cec5SDimitry Andric bool Res = false;
1890b57cec5SDimitry Andric // If we're comparing against a non-symbolic immediate and the source
1900b57cec5SDimitry Andric // register of the compare is not modified (including a self-clobbering
1910b57cec5SDimitry Andric // compare) between the compare and conditional branch we known the value
1920b57cec5SDimitry Andric // of the 1st source operand.
1930b57cec5SDimitry Andric if (PredI.getOperand(2).isImm() && DomBBClobberedRegs.available(SrcReg) &&
1940b57cec5SDimitry Andric SrcReg != DstReg) {
1950b57cec5SDimitry Andric // We've found the instruction that sets NZCV.
1960b57cec5SDimitry Andric int32_t KnownImm = PredI.getOperand(2).getImm();
1970b57cec5SDimitry Andric int32_t Shift = PredI.getOperand(3).getImm();
1980b57cec5SDimitry Andric KnownImm <<= Shift;
1990b57cec5SDimitry Andric if (IsCMN)
2000b57cec5SDimitry Andric KnownImm = -KnownImm;
2010b57cec5SDimitry Andric FirstUse = PredI;
2020b57cec5SDimitry Andric KnownRegs.push_back(RegImm(SrcReg, KnownImm));
2030b57cec5SDimitry Andric Res = true;
2040b57cec5SDimitry Andric }
2050b57cec5SDimitry Andric
2060b57cec5SDimitry Andric // If this instructions defines something other than WZR/XZR, we know it's
2070b57cec5SDimitry Andric // result is zero in some cases.
2080b57cec5SDimitry Andric if (DstReg == AArch64::WZR || DstReg == AArch64::XZR)
2090b57cec5SDimitry Andric return Res;
2100b57cec5SDimitry Andric
2110b57cec5SDimitry Andric // The destination register must not be modified between the NZCV setting
2120b57cec5SDimitry Andric // instruction and the conditional branch.
2130b57cec5SDimitry Andric if (!DomBBClobberedRegs.available(DstReg))
2140b57cec5SDimitry Andric return Res;
2150b57cec5SDimitry Andric
2160b57cec5SDimitry Andric FirstUse = PredI;
2170b57cec5SDimitry Andric KnownRegs.push_back(RegImm(DstReg, 0));
2180b57cec5SDimitry Andric return true;
2190b57cec5SDimitry Andric }
2200b57cec5SDimitry Andric
2210b57cec5SDimitry Andric // Look for NZCV setting instructions that define something other than
2220b57cec5SDimitry Andric // WZR/XZR.
2230b57cec5SDimitry Andric case AArch64::ADCSWr:
2240b57cec5SDimitry Andric case AArch64::ADCSXr:
2250b57cec5SDimitry Andric case AArch64::ADDSWrr:
2260b57cec5SDimitry Andric case AArch64::ADDSWrs:
2270b57cec5SDimitry Andric case AArch64::ADDSWrx:
2280b57cec5SDimitry Andric case AArch64::ADDSXrr:
2290b57cec5SDimitry Andric case AArch64::ADDSXrs:
2300b57cec5SDimitry Andric case AArch64::ADDSXrx:
2310b57cec5SDimitry Andric case AArch64::ADDSXrx64:
2320b57cec5SDimitry Andric case AArch64::ANDSWri:
2330b57cec5SDimitry Andric case AArch64::ANDSWrr:
2340b57cec5SDimitry Andric case AArch64::ANDSWrs:
2350b57cec5SDimitry Andric case AArch64::ANDSXri:
2360b57cec5SDimitry Andric case AArch64::ANDSXrr:
2370b57cec5SDimitry Andric case AArch64::ANDSXrs:
2380b57cec5SDimitry Andric case AArch64::BICSWrr:
2390b57cec5SDimitry Andric case AArch64::BICSWrs:
2400b57cec5SDimitry Andric case AArch64::BICSXrs:
2410b57cec5SDimitry Andric case AArch64::BICSXrr:
2420b57cec5SDimitry Andric case AArch64::SBCSWr:
2430b57cec5SDimitry Andric case AArch64::SBCSXr:
2440b57cec5SDimitry Andric case AArch64::SUBSWrr:
2450b57cec5SDimitry Andric case AArch64::SUBSWrs:
2460b57cec5SDimitry Andric case AArch64::SUBSWrx:
2470b57cec5SDimitry Andric case AArch64::SUBSXrr:
2480b57cec5SDimitry Andric case AArch64::SUBSXrs:
2490b57cec5SDimitry Andric case AArch64::SUBSXrx:
2500b57cec5SDimitry Andric case AArch64::SUBSXrx64: {
2510b57cec5SDimitry Andric MCPhysReg DstReg = PredI.getOperand(0).getReg();
2520b57cec5SDimitry Andric if (DstReg == AArch64::WZR || DstReg == AArch64::XZR)
2530b57cec5SDimitry Andric return false;
2540b57cec5SDimitry Andric
2550b57cec5SDimitry Andric // The destination register of the NZCV setting instruction must not be
2560b57cec5SDimitry Andric // modified before the conditional branch.
2570b57cec5SDimitry Andric if (!DomBBClobberedRegs.available(DstReg))
2580b57cec5SDimitry Andric return false;
2590b57cec5SDimitry Andric
2600b57cec5SDimitry Andric // We've found the instruction that sets NZCV whose DstReg == 0.
2610b57cec5SDimitry Andric FirstUse = PredI;
2620b57cec5SDimitry Andric KnownRegs.push_back(RegImm(DstReg, 0));
2630b57cec5SDimitry Andric return true;
2640b57cec5SDimitry Andric }
2650b57cec5SDimitry Andric }
2660b57cec5SDimitry Andric
2670b57cec5SDimitry Andric // Bail if we see an instruction that defines NZCV that we don't handle.
268*0fca6ea1SDimitry Andric if (PredI.definesRegister(AArch64::NZCV, /*TRI=*/nullptr))
2690b57cec5SDimitry Andric return false;
2700b57cec5SDimitry Andric
2710b57cec5SDimitry Andric // Track clobbered and used registers.
2720b57cec5SDimitry Andric LiveRegUnits::accumulateUsedDefed(PredI, DomBBClobberedRegs, DomBBUsedRegs,
2730b57cec5SDimitry Andric TRI);
2740b57cec5SDimitry Andric }
2750b57cec5SDimitry Andric return false;
2760b57cec5SDimitry Andric }
2770b57cec5SDimitry Andric
optimizeBlock(MachineBasicBlock * MBB)2780b57cec5SDimitry Andric bool AArch64RedundantCopyElimination::optimizeBlock(MachineBasicBlock *MBB) {
2790b57cec5SDimitry Andric // Check if the current basic block has a single predecessor.
2800b57cec5SDimitry Andric if (MBB->pred_size() != 1)
2810b57cec5SDimitry Andric return false;
2820b57cec5SDimitry Andric
2830b57cec5SDimitry Andric // Check if the predecessor has two successors, implying the block ends in a
2840b57cec5SDimitry Andric // conditional branch.
2850b57cec5SDimitry Andric MachineBasicBlock *PredMBB = *MBB->pred_begin();
2860b57cec5SDimitry Andric if (PredMBB->succ_size() != 2)
2870b57cec5SDimitry Andric return false;
2880b57cec5SDimitry Andric
2890b57cec5SDimitry Andric MachineBasicBlock::iterator CondBr = PredMBB->getLastNonDebugInstr();
2900b57cec5SDimitry Andric if (CondBr == PredMBB->end())
2910b57cec5SDimitry Andric return false;
2920b57cec5SDimitry Andric
2930b57cec5SDimitry Andric // Keep track of the earliest point in the PredMBB block where kill markers
2940b57cec5SDimitry Andric // need to be removed if a COPY is removed.
2950b57cec5SDimitry Andric MachineBasicBlock::iterator FirstUse;
2960b57cec5SDimitry Andric // After calling knownRegValInBlock, FirstUse will either point to a CBZ/CBNZ
2970b57cec5SDimitry Andric // or a compare (i.e., SUBS). In the latter case, we must take care when
2980b57cec5SDimitry Andric // updating FirstUse when scanning for COPY instructions. In particular, if
2990b57cec5SDimitry Andric // there's a COPY in between the compare and branch the COPY should not
3000b57cec5SDimitry Andric // update FirstUse.
3010b57cec5SDimitry Andric bool SeenFirstUse = false;
3020b57cec5SDimitry Andric // Registers that contain a known value at the start of MBB.
3030b57cec5SDimitry Andric SmallVector<RegImm, 4> KnownRegs;
3040b57cec5SDimitry Andric
3050b57cec5SDimitry Andric MachineBasicBlock::iterator Itr = std::next(CondBr);
3060b57cec5SDimitry Andric do {
3070b57cec5SDimitry Andric --Itr;
3080b57cec5SDimitry Andric
3090b57cec5SDimitry Andric if (!knownRegValInBlock(*Itr, MBB, KnownRegs, FirstUse))
3100b57cec5SDimitry Andric continue;
3110b57cec5SDimitry Andric
3120b57cec5SDimitry Andric // Reset the clobbered and used register units.
3130b57cec5SDimitry Andric OptBBClobberedRegs.clear();
3140b57cec5SDimitry Andric OptBBUsedRegs.clear();
3150b57cec5SDimitry Andric
3160b57cec5SDimitry Andric // Look backward in PredMBB for COPYs from the known reg to find other
3170b57cec5SDimitry Andric // registers that are known to be a constant value.
3180b57cec5SDimitry Andric for (auto PredI = Itr;; --PredI) {
3190b57cec5SDimitry Andric if (FirstUse == PredI)
3200b57cec5SDimitry Andric SeenFirstUse = true;
3210b57cec5SDimitry Andric
3220b57cec5SDimitry Andric if (PredI->isCopy()) {
3230b57cec5SDimitry Andric MCPhysReg CopyDstReg = PredI->getOperand(0).getReg();
3240b57cec5SDimitry Andric MCPhysReg CopySrcReg = PredI->getOperand(1).getReg();
3250b57cec5SDimitry Andric for (auto &KnownReg : KnownRegs) {
3260b57cec5SDimitry Andric if (!OptBBClobberedRegs.available(KnownReg.Reg))
3270b57cec5SDimitry Andric continue;
3280b57cec5SDimitry Andric // If we have X = COPY Y, and Y is known to be zero, then now X is
3290b57cec5SDimitry Andric // known to be zero.
3300b57cec5SDimitry Andric if (CopySrcReg == KnownReg.Reg &&
3310b57cec5SDimitry Andric OptBBClobberedRegs.available(CopyDstReg)) {
3320b57cec5SDimitry Andric KnownRegs.push_back(RegImm(CopyDstReg, KnownReg.Imm));
3330b57cec5SDimitry Andric if (SeenFirstUse)
3340b57cec5SDimitry Andric FirstUse = PredI;
3350b57cec5SDimitry Andric break;
3360b57cec5SDimitry Andric }
3370b57cec5SDimitry Andric // If we have X = COPY Y, and X is known to be zero, then now Y is
3380b57cec5SDimitry Andric // known to be zero.
3390b57cec5SDimitry Andric if (CopyDstReg == KnownReg.Reg &&
3400b57cec5SDimitry Andric OptBBClobberedRegs.available(CopySrcReg)) {
3410b57cec5SDimitry Andric KnownRegs.push_back(RegImm(CopySrcReg, KnownReg.Imm));
3420b57cec5SDimitry Andric if (SeenFirstUse)
3430b57cec5SDimitry Andric FirstUse = PredI;
3440b57cec5SDimitry Andric break;
3450b57cec5SDimitry Andric }
3460b57cec5SDimitry Andric }
3470b57cec5SDimitry Andric }
3480b57cec5SDimitry Andric
3490b57cec5SDimitry Andric // Stop if we get to the beginning of PredMBB.
3500b57cec5SDimitry Andric if (PredI == PredMBB->begin())
3510b57cec5SDimitry Andric break;
3520b57cec5SDimitry Andric
3530b57cec5SDimitry Andric LiveRegUnits::accumulateUsedDefed(*PredI, OptBBClobberedRegs,
3540b57cec5SDimitry Andric OptBBUsedRegs, TRI);
3550b57cec5SDimitry Andric // Stop if all of the known-zero regs have been clobbered.
3560b57cec5SDimitry Andric if (all_of(KnownRegs, [&](RegImm KnownReg) {
3570b57cec5SDimitry Andric return !OptBBClobberedRegs.available(KnownReg.Reg);
3580b57cec5SDimitry Andric }))
3590b57cec5SDimitry Andric break;
3600b57cec5SDimitry Andric }
3610b57cec5SDimitry Andric break;
3620b57cec5SDimitry Andric
3630b57cec5SDimitry Andric } while (Itr != PredMBB->begin() && Itr->isTerminator());
3640b57cec5SDimitry Andric
3650b57cec5SDimitry Andric // We've not found a registers with a known value, time to bail out.
3660b57cec5SDimitry Andric if (KnownRegs.empty())
3670b57cec5SDimitry Andric return false;
3680b57cec5SDimitry Andric
3690b57cec5SDimitry Andric bool Changed = false;
3700b57cec5SDimitry Andric // UsedKnownRegs is the set of KnownRegs that have had uses added to MBB.
3710b57cec5SDimitry Andric SmallSetVector<unsigned, 4> UsedKnownRegs;
3720b57cec5SDimitry Andric MachineBasicBlock::iterator LastChange = MBB->begin();
3730b57cec5SDimitry Andric // Remove redundant copy/move instructions unless KnownReg is modified.
3740b57cec5SDimitry Andric for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); I != E;) {
3750b57cec5SDimitry Andric MachineInstr *MI = &*I;
3760b57cec5SDimitry Andric ++I;
3770b57cec5SDimitry Andric bool RemovedMI = false;
3780b57cec5SDimitry Andric bool IsCopy = MI->isCopy();
3790b57cec5SDimitry Andric bool IsMoveImm = MI->isMoveImmediate();
3800b57cec5SDimitry Andric if (IsCopy || IsMoveImm) {
3810b57cec5SDimitry Andric Register DefReg = MI->getOperand(0).getReg();
3820b57cec5SDimitry Andric Register SrcReg = IsCopy ? MI->getOperand(1).getReg() : Register();
3830b57cec5SDimitry Andric int64_t SrcImm = IsMoveImm ? MI->getOperand(1).getImm() : 0;
3840b57cec5SDimitry Andric if (!MRI->isReserved(DefReg) &&
3850b57cec5SDimitry Andric ((IsCopy && (SrcReg == AArch64::XZR || SrcReg == AArch64::WZR)) ||
3860b57cec5SDimitry Andric IsMoveImm)) {
3870b57cec5SDimitry Andric for (RegImm &KnownReg : KnownRegs) {
3880b57cec5SDimitry Andric if (KnownReg.Reg != DefReg &&
3890b57cec5SDimitry Andric !TRI->isSuperRegister(DefReg, KnownReg.Reg))
3900b57cec5SDimitry Andric continue;
3910b57cec5SDimitry Andric
3920b57cec5SDimitry Andric // For a copy, the known value must be a zero.
3930b57cec5SDimitry Andric if (IsCopy && KnownReg.Imm != 0)
3940b57cec5SDimitry Andric continue;
3950b57cec5SDimitry Andric
3960b57cec5SDimitry Andric if (IsMoveImm) {
3970b57cec5SDimitry Andric // For a move immediate, the known immediate must match the source
3980b57cec5SDimitry Andric // immediate.
3990b57cec5SDimitry Andric if (KnownReg.Imm != SrcImm)
4000b57cec5SDimitry Andric continue;
4010b57cec5SDimitry Andric
4020b57cec5SDimitry Andric // Don't remove a move immediate that implicitly defines the upper
4030b57cec5SDimitry Andric // bits when only the lower 32 bits are known.
4040b57cec5SDimitry Andric MCPhysReg CmpReg = KnownReg.Reg;
4050b57cec5SDimitry Andric if (any_of(MI->implicit_operands(), [CmpReg](MachineOperand &O) {
4060b57cec5SDimitry Andric return !O.isDead() && O.isReg() && O.isDef() &&
4070b57cec5SDimitry Andric O.getReg() != CmpReg;
4080b57cec5SDimitry Andric }))
4090b57cec5SDimitry Andric continue;
410e8d8bef9SDimitry Andric
411e8d8bef9SDimitry Andric // Don't remove a move immediate that implicitly defines the upper
412e8d8bef9SDimitry Andric // bits as different.
413e8d8bef9SDimitry Andric if (TRI->isSuperRegister(DefReg, KnownReg.Reg) && KnownReg.Imm < 0)
414e8d8bef9SDimitry Andric continue;
4150b57cec5SDimitry Andric }
4160b57cec5SDimitry Andric
4170b57cec5SDimitry Andric if (IsCopy)
4180b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Remove redundant Copy : " << *MI);
4190b57cec5SDimitry Andric else
4200b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Remove redundant Move : " << *MI);
4210b57cec5SDimitry Andric
4220b57cec5SDimitry Andric MI->eraseFromParent();
4230b57cec5SDimitry Andric Changed = true;
4240b57cec5SDimitry Andric LastChange = I;
4250b57cec5SDimitry Andric NumCopiesRemoved++;
4260b57cec5SDimitry Andric UsedKnownRegs.insert(KnownReg.Reg);
4270b57cec5SDimitry Andric RemovedMI = true;
4280b57cec5SDimitry Andric break;
4290b57cec5SDimitry Andric }
4300b57cec5SDimitry Andric }
4310b57cec5SDimitry Andric }
4320b57cec5SDimitry Andric
4330b57cec5SDimitry Andric // Skip to the next instruction if we removed the COPY/MovImm.
4340b57cec5SDimitry Andric if (RemovedMI)
4350b57cec5SDimitry Andric continue;
4360b57cec5SDimitry Andric
4370b57cec5SDimitry Andric // Remove any regs the MI clobbers from the KnownConstRegs set.
4380b57cec5SDimitry Andric for (unsigned RI = 0; RI < KnownRegs.size();)
4390b57cec5SDimitry Andric if (MI->modifiesRegister(KnownRegs[RI].Reg, TRI)) {
4400b57cec5SDimitry Andric std::swap(KnownRegs[RI], KnownRegs[KnownRegs.size() - 1]);
4410b57cec5SDimitry Andric KnownRegs.pop_back();
4420b57cec5SDimitry Andric // Don't increment RI since we need to now check the swapped-in
4430b57cec5SDimitry Andric // KnownRegs[RI].
4440b57cec5SDimitry Andric } else {
4450b57cec5SDimitry Andric ++RI;
4460b57cec5SDimitry Andric }
4470b57cec5SDimitry Andric
4480b57cec5SDimitry Andric // Continue until the KnownRegs set is empty.
4490b57cec5SDimitry Andric if (KnownRegs.empty())
4500b57cec5SDimitry Andric break;
4510b57cec5SDimitry Andric }
4520b57cec5SDimitry Andric
4530b57cec5SDimitry Andric if (!Changed)
4540b57cec5SDimitry Andric return false;
4550b57cec5SDimitry Andric
4560b57cec5SDimitry Andric // Add newly used regs to the block's live-in list if they aren't there
4570b57cec5SDimitry Andric // already.
4580b57cec5SDimitry Andric for (MCPhysReg KnownReg : UsedKnownRegs)
4590b57cec5SDimitry Andric if (!MBB->isLiveIn(KnownReg))
4600b57cec5SDimitry Andric MBB->addLiveIn(KnownReg);
4610b57cec5SDimitry Andric
4620b57cec5SDimitry Andric // Clear kills in the range where changes were made. This is conservative,
4630b57cec5SDimitry Andric // but should be okay since kill markers are being phased out.
4640b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Clearing kill flags.\n\tFirstUse: " << *FirstUse
4655f757f3fSDimitry Andric << "\tLastChange: ";
4665f757f3fSDimitry Andric if (LastChange == MBB->end()) dbgs() << "<end>\n";
4675f757f3fSDimitry Andric else dbgs() << *LastChange);
4680b57cec5SDimitry Andric for (MachineInstr &MMI : make_range(FirstUse, PredMBB->end()))
4690b57cec5SDimitry Andric MMI.clearKillInfo();
4700b57cec5SDimitry Andric for (MachineInstr &MMI : make_range(MBB->begin(), LastChange))
4710b57cec5SDimitry Andric MMI.clearKillInfo();
4720b57cec5SDimitry Andric
4730b57cec5SDimitry Andric return true;
4740b57cec5SDimitry Andric }
4750b57cec5SDimitry Andric
runOnMachineFunction(MachineFunction & MF)4760b57cec5SDimitry Andric bool AArch64RedundantCopyElimination::runOnMachineFunction(
4770b57cec5SDimitry Andric MachineFunction &MF) {
4780b57cec5SDimitry Andric if (skipFunction(MF.getFunction()))
4790b57cec5SDimitry Andric return false;
4800b57cec5SDimitry Andric TRI = MF.getSubtarget().getRegisterInfo();
4810b57cec5SDimitry Andric MRI = &MF.getRegInfo();
4820b57cec5SDimitry Andric
4830b57cec5SDimitry Andric // Resize the clobbered and used register unit trackers. We do this once per
4840b57cec5SDimitry Andric // function.
4850b57cec5SDimitry Andric DomBBClobberedRegs.init(*TRI);
4860b57cec5SDimitry Andric DomBBUsedRegs.init(*TRI);
4870b57cec5SDimitry Andric OptBBClobberedRegs.init(*TRI);
4880b57cec5SDimitry Andric OptBBUsedRegs.init(*TRI);
4890b57cec5SDimitry Andric
4900b57cec5SDimitry Andric bool Changed = false;
4910b57cec5SDimitry Andric for (MachineBasicBlock &MBB : MF)
4920b57cec5SDimitry Andric Changed |= optimizeBlock(&MBB);
4930b57cec5SDimitry Andric return Changed;
4940b57cec5SDimitry Andric }
4950b57cec5SDimitry Andric
createAArch64RedundantCopyEliminationPass()4960b57cec5SDimitry Andric FunctionPass *llvm::createAArch64RedundantCopyEliminationPass() {
4970b57cec5SDimitry Andric return new AArch64RedundantCopyElimination();
4980b57cec5SDimitry Andric }
499