1*9880d681SAndroid Build Coastguard Worker //=- AArch64RedundantCopyElimination.cpp - Remove useless copy for AArch64 -=//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker // This pass removes unnecessary zero copies in BBs that are targets of
9*9880d681SAndroid Build Coastguard Worker // cbz/cbnz instructions. For instance, the copy instruction in the code below
10*9880d681SAndroid Build Coastguard Worker // can be removed because the CBZW jumps to BB#2 when W0 is zero.
11*9880d681SAndroid Build Coastguard Worker // BB#1:
12*9880d681SAndroid Build Coastguard Worker // CBZW %W0, <BB#2>
13*9880d681SAndroid Build Coastguard Worker // BB#2:
14*9880d681SAndroid Build Coastguard Worker // %W0 = COPY %WZR
15*9880d681SAndroid Build Coastguard Worker // This pass should be run after register allocation.
16*9880d681SAndroid Build Coastguard Worker //
17*9880d681SAndroid Build Coastguard Worker // FIXME: This should be extended to handle any constant other than zero. E.g.,
18*9880d681SAndroid Build Coastguard Worker // cmp w0, #1
19*9880d681SAndroid Build Coastguard Worker // b.eq .BB1
20*9880d681SAndroid Build Coastguard Worker // BB1:
21*9880d681SAndroid Build Coastguard Worker // mov w0, #1
22*9880d681SAndroid Build Coastguard Worker //
23*9880d681SAndroid Build Coastguard Worker // FIXME: This could also be extended to check the whole dominance subtree below
24*9880d681SAndroid Build Coastguard Worker // the comparison if the compile time regression is acceptable.
25*9880d681SAndroid Build Coastguard Worker //
26*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
27*9880d681SAndroid Build Coastguard Worker
28*9880d681SAndroid Build Coastguard Worker #include "AArch64.h"
29*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/SetVector.h"
30*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/Statistic.h"
31*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/iterator_range.h"
32*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineFunctionPass.h"
33*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineRegisterInfo.h"
34*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Debug.h"
35*9880d681SAndroid Build Coastguard Worker
36*9880d681SAndroid Build Coastguard Worker using namespace llvm;
37*9880d681SAndroid Build Coastguard Worker
38*9880d681SAndroid Build Coastguard Worker #define DEBUG_TYPE "aarch64-copyelim"
39*9880d681SAndroid Build Coastguard Worker
40*9880d681SAndroid Build Coastguard Worker STATISTIC(NumCopiesRemoved, "Number of copies removed.");
41*9880d681SAndroid Build Coastguard Worker
42*9880d681SAndroid Build Coastguard Worker namespace llvm {
43*9880d681SAndroid Build Coastguard Worker void initializeAArch64RedundantCopyEliminationPass(PassRegistry &);
44*9880d681SAndroid Build Coastguard Worker }
45*9880d681SAndroid Build Coastguard Worker
46*9880d681SAndroid Build Coastguard Worker namespace {
47*9880d681SAndroid Build Coastguard Worker class AArch64RedundantCopyElimination : public MachineFunctionPass {
48*9880d681SAndroid Build Coastguard Worker const MachineRegisterInfo *MRI;
49*9880d681SAndroid Build Coastguard Worker const TargetRegisterInfo *TRI;
50*9880d681SAndroid Build Coastguard Worker
51*9880d681SAndroid Build Coastguard Worker public:
52*9880d681SAndroid Build Coastguard Worker static char ID;
AArch64RedundantCopyElimination()53*9880d681SAndroid Build Coastguard Worker AArch64RedundantCopyElimination() : MachineFunctionPass(ID) {}
54*9880d681SAndroid Build Coastguard Worker bool optimizeCopy(MachineBasicBlock *MBB);
55*9880d681SAndroid Build Coastguard Worker bool runOnMachineFunction(MachineFunction &MF) override;
getRequiredProperties() const56*9880d681SAndroid Build Coastguard Worker MachineFunctionProperties getRequiredProperties() const override {
57*9880d681SAndroid Build Coastguard Worker return MachineFunctionProperties().set(
58*9880d681SAndroid Build Coastguard Worker MachineFunctionProperties::Property::AllVRegsAllocated);
59*9880d681SAndroid Build Coastguard Worker }
getPassName() const60*9880d681SAndroid Build Coastguard Worker const char *getPassName() const override {
61*9880d681SAndroid Build Coastguard Worker return "AArch64 Redundant Copy Elimination";
62*9880d681SAndroid Build Coastguard Worker }
63*9880d681SAndroid Build Coastguard Worker };
64*9880d681SAndroid Build Coastguard Worker char AArch64RedundantCopyElimination::ID = 0;
65*9880d681SAndroid Build Coastguard Worker }
66*9880d681SAndroid Build Coastguard Worker
67*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS(AArch64RedundantCopyElimination, "aarch64-copyelim",
68*9880d681SAndroid Build Coastguard Worker "AArch64 redundant copy elimination pass", false, false)
69*9880d681SAndroid Build Coastguard Worker
guaranteesZeroRegInBlock(MachineInstr & MI,MachineBasicBlock * MBB)70*9880d681SAndroid Build Coastguard Worker static bool guaranteesZeroRegInBlock(MachineInstr &MI, MachineBasicBlock *MBB) {
71*9880d681SAndroid Build Coastguard Worker unsigned Opc = MI.getOpcode();
72*9880d681SAndroid Build Coastguard Worker // Check if the current basic block is the target block to which the
73*9880d681SAndroid Build Coastguard Worker // CBZ/CBNZ instruction jumps when its Wt/Xt is zero.
74*9880d681SAndroid Build Coastguard Worker if ((Opc == AArch64::CBZW || Opc == AArch64::CBZX) &&
75*9880d681SAndroid Build Coastguard Worker MBB == MI.getOperand(1).getMBB())
76*9880d681SAndroid Build Coastguard Worker return true;
77*9880d681SAndroid Build Coastguard Worker else if ((Opc == AArch64::CBNZW || Opc == AArch64::CBNZX) &&
78*9880d681SAndroid Build Coastguard Worker MBB != MI.getOperand(1).getMBB())
79*9880d681SAndroid Build Coastguard Worker return true;
80*9880d681SAndroid Build Coastguard Worker
81*9880d681SAndroid Build Coastguard Worker return false;
82*9880d681SAndroid Build Coastguard Worker }
83*9880d681SAndroid Build Coastguard Worker
optimizeCopy(MachineBasicBlock * MBB)84*9880d681SAndroid Build Coastguard Worker bool AArch64RedundantCopyElimination::optimizeCopy(MachineBasicBlock *MBB) {
85*9880d681SAndroid Build Coastguard Worker // Check if the current basic block has a single predecessor.
86*9880d681SAndroid Build Coastguard Worker if (MBB->pred_size() != 1)
87*9880d681SAndroid Build Coastguard Worker return false;
88*9880d681SAndroid Build Coastguard Worker
89*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *PredMBB = *MBB->pred_begin();
90*9880d681SAndroid Build Coastguard Worker MachineBasicBlock::iterator CompBr = PredMBB->getLastNonDebugInstr();
91*9880d681SAndroid Build Coastguard Worker if (CompBr == PredMBB->end() || PredMBB->succ_size() != 2)
92*9880d681SAndroid Build Coastguard Worker return false;
93*9880d681SAndroid Build Coastguard Worker
94*9880d681SAndroid Build Coastguard Worker ++CompBr;
95*9880d681SAndroid Build Coastguard Worker do {
96*9880d681SAndroid Build Coastguard Worker --CompBr;
97*9880d681SAndroid Build Coastguard Worker if (guaranteesZeroRegInBlock(*CompBr, MBB))
98*9880d681SAndroid Build Coastguard Worker break;
99*9880d681SAndroid Build Coastguard Worker } while (CompBr != PredMBB->begin() && CompBr->isTerminator());
100*9880d681SAndroid Build Coastguard Worker
101*9880d681SAndroid Build Coastguard Worker // We've not found a CBZ/CBNZ, time to bail out.
102*9880d681SAndroid Build Coastguard Worker if (!guaranteesZeroRegInBlock(*CompBr, MBB))
103*9880d681SAndroid Build Coastguard Worker return false;
104*9880d681SAndroid Build Coastguard Worker
105*9880d681SAndroid Build Coastguard Worker unsigned TargetReg = CompBr->getOperand(0).getReg();
106*9880d681SAndroid Build Coastguard Worker if (!TargetReg)
107*9880d681SAndroid Build Coastguard Worker return false;
108*9880d681SAndroid Build Coastguard Worker assert(TargetRegisterInfo::isPhysicalRegister(TargetReg) &&
109*9880d681SAndroid Build Coastguard Worker "Expect physical register");
110*9880d681SAndroid Build Coastguard Worker
111*9880d681SAndroid Build Coastguard Worker // Remember all registers aliasing with TargetReg.
112*9880d681SAndroid Build Coastguard Worker SmallSetVector<unsigned, 8> TargetRegs;
113*9880d681SAndroid Build Coastguard Worker for (MCRegAliasIterator AI(TargetReg, TRI, true); AI.isValid(); ++AI)
114*9880d681SAndroid Build Coastguard Worker TargetRegs.insert(*AI);
115*9880d681SAndroid Build Coastguard Worker
116*9880d681SAndroid Build Coastguard Worker bool Changed = false;
117*9880d681SAndroid Build Coastguard Worker MachineBasicBlock::iterator LastChange = MBB->begin();
118*9880d681SAndroid Build Coastguard Worker unsigned SmallestDef = TargetReg;
119*9880d681SAndroid Build Coastguard Worker // Remove redundant Copy instructions unless TargetReg is modified.
120*9880d681SAndroid Build Coastguard Worker for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); I != E;) {
121*9880d681SAndroid Build Coastguard Worker MachineInstr *MI = &*I;
122*9880d681SAndroid Build Coastguard Worker ++I;
123*9880d681SAndroid Build Coastguard Worker if (MI->isCopy() && MI->getOperand(0).isReg() &&
124*9880d681SAndroid Build Coastguard Worker MI->getOperand(1).isReg()) {
125*9880d681SAndroid Build Coastguard Worker
126*9880d681SAndroid Build Coastguard Worker unsigned DefReg = MI->getOperand(0).getReg();
127*9880d681SAndroid Build Coastguard Worker unsigned SrcReg = MI->getOperand(1).getReg();
128*9880d681SAndroid Build Coastguard Worker
129*9880d681SAndroid Build Coastguard Worker if ((SrcReg == AArch64::XZR || SrcReg == AArch64::WZR) &&
130*9880d681SAndroid Build Coastguard Worker !MRI->isReserved(DefReg) &&
131*9880d681SAndroid Build Coastguard Worker (TargetReg == DefReg || TRI->isSuperRegister(DefReg, TargetReg))) {
132*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Remove redundant Copy : ");
133*9880d681SAndroid Build Coastguard Worker DEBUG((MI)->print(dbgs()));
134*9880d681SAndroid Build Coastguard Worker
135*9880d681SAndroid Build Coastguard Worker MI->eraseFromParent();
136*9880d681SAndroid Build Coastguard Worker Changed = true;
137*9880d681SAndroid Build Coastguard Worker LastChange = I;
138*9880d681SAndroid Build Coastguard Worker NumCopiesRemoved++;
139*9880d681SAndroid Build Coastguard Worker SmallestDef =
140*9880d681SAndroid Build Coastguard Worker TRI->isSubRegister(SmallestDef, DefReg) ? DefReg : SmallestDef;
141*9880d681SAndroid Build Coastguard Worker continue;
142*9880d681SAndroid Build Coastguard Worker }
143*9880d681SAndroid Build Coastguard Worker }
144*9880d681SAndroid Build Coastguard Worker
145*9880d681SAndroid Build Coastguard Worker if (MI->modifiesRegister(TargetReg, TRI))
146*9880d681SAndroid Build Coastguard Worker break;
147*9880d681SAndroid Build Coastguard Worker }
148*9880d681SAndroid Build Coastguard Worker
149*9880d681SAndroid Build Coastguard Worker if (!Changed)
150*9880d681SAndroid Build Coastguard Worker return false;
151*9880d681SAndroid Build Coastguard Worker
152*9880d681SAndroid Build Coastguard Worker // Otherwise, we have to fixup the use-def chain, starting with the
153*9880d681SAndroid Build Coastguard Worker // CBZ/CBNZ. Conservatively mark as much as we can live.
154*9880d681SAndroid Build Coastguard Worker CompBr->clearRegisterKills(SmallestDef, TRI);
155*9880d681SAndroid Build Coastguard Worker
156*9880d681SAndroid Build Coastguard Worker if (std::none_of(TargetRegs.begin(), TargetRegs.end(),
157*9880d681SAndroid Build Coastguard Worker [&](unsigned Reg) { return MBB->isLiveIn(Reg); }))
158*9880d681SAndroid Build Coastguard Worker MBB->addLiveIn(TargetReg);
159*9880d681SAndroid Build Coastguard Worker
160*9880d681SAndroid Build Coastguard Worker // Clear any kills of TargetReg between CompBr and the last removed COPY.
161*9880d681SAndroid Build Coastguard Worker for (MachineInstr &MMI :
162*9880d681SAndroid Build Coastguard Worker make_range(MBB->begin()->getIterator(), LastChange->getIterator()))
163*9880d681SAndroid Build Coastguard Worker MMI.clearRegisterKills(SmallestDef, TRI);
164*9880d681SAndroid Build Coastguard Worker
165*9880d681SAndroid Build Coastguard Worker return true;
166*9880d681SAndroid Build Coastguard Worker }
167*9880d681SAndroid Build Coastguard Worker
runOnMachineFunction(MachineFunction & MF)168*9880d681SAndroid Build Coastguard Worker bool AArch64RedundantCopyElimination::runOnMachineFunction(
169*9880d681SAndroid Build Coastguard Worker MachineFunction &MF) {
170*9880d681SAndroid Build Coastguard Worker if (skipFunction(*MF.getFunction()))
171*9880d681SAndroid Build Coastguard Worker return false;
172*9880d681SAndroid Build Coastguard Worker TRI = MF.getSubtarget().getRegisterInfo();
173*9880d681SAndroid Build Coastguard Worker MRI = &MF.getRegInfo();
174*9880d681SAndroid Build Coastguard Worker bool Changed = false;
175*9880d681SAndroid Build Coastguard Worker for (MachineBasicBlock &MBB : MF)
176*9880d681SAndroid Build Coastguard Worker Changed |= optimizeCopy(&MBB);
177*9880d681SAndroid Build Coastguard Worker return Changed;
178*9880d681SAndroid Build Coastguard Worker }
179*9880d681SAndroid Build Coastguard Worker
createAArch64RedundantCopyEliminationPass()180*9880d681SAndroid Build Coastguard Worker FunctionPass *llvm::createAArch64RedundantCopyEliminationPass() {
181*9880d681SAndroid Build Coastguard Worker return new AArch64RedundantCopyElimination();
182*9880d681SAndroid Build Coastguard Worker }
183