[AArch64] Add pass to remove redundant copy after RA
Summary:
This change will add a pass to remove unnecessary zero copies in target blocks
of cbz/cbnz instructions. E.g., the copy instruction in the code below can be
removed because the cbz jumps to BB1 when x0 is zero :
BB0:
cbz x0, .BB1
BB1:
mov x0, xzr
Jun
Reviewers: gberry, jmolloy, HaoLiu, MatzeB, mcrosier
Subscribers: mcrosier, mssimpso, haicheng, bmakam, llvm-commits, aemerson, rengolin
Differential Revision: http://reviews.llvm.org/D16203
llvm-svn: 261004
2016-02-17 04:02:39 +08:00
|
|
|
//=- AArch64RedundantCopyElimination.cpp - Remove useless copy for AArch64 -=//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
// This pass removes unnecessary zero copies in BBs that are targets of
|
|
|
|
// cbz/cbnz instructions. For instance, the copy instruction in the code below
|
|
|
|
// can be removed because the CBZW jumps to BB#2 when W0 is zero.
|
|
|
|
// BB#1:
|
|
|
|
// CBZW %W0, <BB#2>
|
|
|
|
// BB#2:
|
|
|
|
// %W0 = COPY %WZR
|
|
|
|
// This pass should be run after register allocation.
|
|
|
|
//
|
|
|
|
// FIXME: This should be extended to handle any constant other than zero. E.g.,
|
|
|
|
// cmp w0, #1
|
|
|
|
// b.eq .BB1
|
|
|
|
// BB1:
|
|
|
|
// mov w0, #1
|
|
|
|
//
|
|
|
|
// FIXME: This could also be extended to check the whole dominance subtree below
|
|
|
|
// the comparison if the compile time regression is acceptable.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "AArch64.h"
|
|
|
|
#include "llvm/ADT/SetVector.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
|
|
|
#include "llvm/ADT/iterator_range.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "aarch64-copyelim"
|
|
|
|
|
|
|
|
STATISTIC(NumCopiesRemoved, "Number of copies removed.");
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class AArch64RedundantCopyElimination : public MachineFunctionPass {
|
|
|
|
const MachineRegisterInfo *MRI;
|
|
|
|
const TargetRegisterInfo *TRI;
|
|
|
|
|
|
|
|
public:
|
|
|
|
static char ID;
|
2016-08-01 13:56:57 +08:00
|
|
|
AArch64RedundantCopyElimination() : MachineFunctionPass(ID) {
|
|
|
|
initializeAArch64RedundantCopyEliminationPass(
|
|
|
|
*PassRegistry::getPassRegistry());
|
|
|
|
}
|
[AArch64] Add pass to remove redundant copy after RA
Summary:
This change will add a pass to remove unnecessary zero copies in target blocks
of cbz/cbnz instructions. E.g., the copy instruction in the code below can be
removed because the cbz jumps to BB1 when x0 is zero :
BB0:
cbz x0, .BB1
BB1:
mov x0, xzr
Jun
Reviewers: gberry, jmolloy, HaoLiu, MatzeB, mcrosier
Subscribers: mcrosier, mssimpso, haicheng, bmakam, llvm-commits, aemerson, rengolin
Differential Revision: http://reviews.llvm.org/D16203
llvm-svn: 261004
2016-02-17 04:02:39 +08:00
|
|
|
bool optimizeCopy(MachineBasicBlock *MBB);
|
|
|
|
bool runOnMachineFunction(MachineFunction &MF) override;
|
2016-04-05 01:09:25 +08:00
|
|
|
MachineFunctionProperties getRequiredProperties() const override {
|
|
|
|
return MachineFunctionProperties().set(
|
2016-08-25 09:27:13 +08:00
|
|
|
MachineFunctionProperties::Property::NoVRegs);
|
2016-04-05 01:09:25 +08:00
|
|
|
}
|
2016-10-01 10:56:57 +08:00
|
|
|
StringRef getPassName() const override {
|
[AArch64] Add pass to remove redundant copy after RA
Summary:
This change will add a pass to remove unnecessary zero copies in target blocks
of cbz/cbnz instructions. E.g., the copy instruction in the code below can be
removed because the cbz jumps to BB1 when x0 is zero :
BB0:
cbz x0, .BB1
BB1:
mov x0, xzr
Jun
Reviewers: gberry, jmolloy, HaoLiu, MatzeB, mcrosier
Subscribers: mcrosier, mssimpso, haicheng, bmakam, llvm-commits, aemerson, rengolin
Differential Revision: http://reviews.llvm.org/D16203
llvm-svn: 261004
2016-02-17 04:02:39 +08:00
|
|
|
return "AArch64 Redundant Copy Elimination";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
char AArch64RedundantCopyElimination::ID = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
INITIALIZE_PASS(AArch64RedundantCopyElimination, "aarch64-copyelim",
|
|
|
|
"AArch64 redundant copy elimination pass", false, false)
|
|
|
|
|
2016-02-28 03:12:54 +08:00
|
|
|
static bool guaranteesZeroRegInBlock(MachineInstr &MI, MachineBasicBlock *MBB) {
|
|
|
|
unsigned Opc = MI.getOpcode();
|
2016-02-18 05:16:53 +08:00
|
|
|
// Check if the current basic block is the target block to which the
|
|
|
|
// CBZ/CBNZ instruction jumps when its Wt/Xt is zero.
|
|
|
|
if ((Opc == AArch64::CBZW || Opc == AArch64::CBZX) &&
|
2016-02-28 03:12:54 +08:00
|
|
|
MBB == MI.getOperand(1).getMBB())
|
2016-02-18 05:16:53 +08:00
|
|
|
return true;
|
|
|
|
else if ((Opc == AArch64::CBNZW || Opc == AArch64::CBNZX) &&
|
2016-02-28 03:12:54 +08:00
|
|
|
MBB != MI.getOperand(1).getMBB())
|
2016-02-18 05:16:53 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
[AArch64] Add pass to remove redundant copy after RA
Summary:
This change will add a pass to remove unnecessary zero copies in target blocks
of cbz/cbnz instructions. E.g., the copy instruction in the code below can be
removed because the cbz jumps to BB1 when x0 is zero :
BB0:
cbz x0, .BB1
BB1:
mov x0, xzr
Jun
Reviewers: gberry, jmolloy, HaoLiu, MatzeB, mcrosier
Subscribers: mcrosier, mssimpso, haicheng, bmakam, llvm-commits, aemerson, rengolin
Differential Revision: http://reviews.llvm.org/D16203
llvm-svn: 261004
2016-02-17 04:02:39 +08:00
|
|
|
bool AArch64RedundantCopyElimination::optimizeCopy(MachineBasicBlock *MBB) {
|
|
|
|
// Check if the current basic block has a single predecessor.
|
|
|
|
if (MBB->pred_size() != 1)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
MachineBasicBlock *PredMBB = *MBB->pred_begin();
|
|
|
|
MachineBasicBlock::iterator CompBr = PredMBB->getLastNonDebugInstr();
|
|
|
|
if (CompBr == PredMBB->end() || PredMBB->succ_size() != 2)
|
|
|
|
return false;
|
|
|
|
|
2016-02-18 05:16:53 +08:00
|
|
|
++CompBr;
|
|
|
|
do {
|
|
|
|
--CompBr;
|
2016-02-28 03:12:54 +08:00
|
|
|
if (guaranteesZeroRegInBlock(*CompBr, MBB))
|
2016-02-18 05:16:53 +08:00
|
|
|
break;
|
|
|
|
} while (CompBr != PredMBB->begin() && CompBr->isTerminator());
|
|
|
|
|
|
|
|
// We've not found a CBZ/CBNZ, time to bail out.
|
2016-02-28 03:12:54 +08:00
|
|
|
if (!guaranteesZeroRegInBlock(*CompBr, MBB))
|
[AArch64] Add pass to remove redundant copy after RA
Summary:
This change will add a pass to remove unnecessary zero copies in target blocks
of cbz/cbnz instructions. E.g., the copy instruction in the code below can be
removed because the cbz jumps to BB1 when x0 is zero :
BB0:
cbz x0, .BB1
BB1:
mov x0, xzr
Jun
Reviewers: gberry, jmolloy, HaoLiu, MatzeB, mcrosier
Subscribers: mcrosier, mssimpso, haicheng, bmakam, llvm-commits, aemerson, rengolin
Differential Revision: http://reviews.llvm.org/D16203
llvm-svn: 261004
2016-02-17 04:02:39 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
unsigned TargetReg = CompBr->getOperand(0).getReg();
|
|
|
|
if (!TargetReg)
|
|
|
|
return false;
|
|
|
|
assert(TargetRegisterInfo::isPhysicalRegister(TargetReg) &&
|
|
|
|
"Expect physical register");
|
|
|
|
|
|
|
|
// Remember all registers aliasing with TargetReg.
|
|
|
|
SmallSetVector<unsigned, 8> TargetRegs;
|
|
|
|
for (MCRegAliasIterator AI(TargetReg, TRI, true); AI.isValid(); ++AI)
|
|
|
|
TargetRegs.insert(*AI);
|
|
|
|
|
|
|
|
bool Changed = false;
|
2016-02-18 05:16:53 +08:00
|
|
|
MachineBasicBlock::iterator LastChange = MBB->begin();
|
|
|
|
unsigned SmallestDef = TargetReg;
|
[AArch64] Add pass to remove redundant copy after RA
Summary:
This change will add a pass to remove unnecessary zero copies in target blocks
of cbz/cbnz instructions. E.g., the copy instruction in the code below can be
removed because the cbz jumps to BB1 when x0 is zero :
BB0:
cbz x0, .BB1
BB1:
mov x0, xzr
Jun
Reviewers: gberry, jmolloy, HaoLiu, MatzeB, mcrosier
Subscribers: mcrosier, mssimpso, haicheng, bmakam, llvm-commits, aemerson, rengolin
Differential Revision: http://reviews.llvm.org/D16203
llvm-svn: 261004
2016-02-17 04:02:39 +08:00
|
|
|
// Remove redundant Copy instructions unless TargetReg is modified.
|
|
|
|
for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); I != E;) {
|
|
|
|
MachineInstr *MI = &*I;
|
|
|
|
++I;
|
|
|
|
if (MI->isCopy() && MI->getOperand(0).isReg() &&
|
|
|
|
MI->getOperand(1).isReg()) {
|
|
|
|
|
|
|
|
unsigned DefReg = MI->getOperand(0).getReg();
|
|
|
|
unsigned SrcReg = MI->getOperand(1).getReg();
|
|
|
|
|
|
|
|
if ((SrcReg == AArch64::XZR || SrcReg == AArch64::WZR) &&
|
|
|
|
!MRI->isReserved(DefReg) &&
|
|
|
|
(TargetReg == DefReg || TRI->isSuperRegister(DefReg, TargetReg))) {
|
|
|
|
DEBUG(dbgs() << "Remove redundant Copy : ");
|
|
|
|
DEBUG((MI)->print(dbgs()));
|
|
|
|
|
|
|
|
MI->eraseFromParent();
|
|
|
|
Changed = true;
|
2016-02-18 05:16:53 +08:00
|
|
|
LastChange = I;
|
[AArch64] Add pass to remove redundant copy after RA
Summary:
This change will add a pass to remove unnecessary zero copies in target blocks
of cbz/cbnz instructions. E.g., the copy instruction in the code below can be
removed because the cbz jumps to BB1 when x0 is zero :
BB0:
cbz x0, .BB1
BB1:
mov x0, xzr
Jun
Reviewers: gberry, jmolloy, HaoLiu, MatzeB, mcrosier
Subscribers: mcrosier, mssimpso, haicheng, bmakam, llvm-commits, aemerson, rengolin
Differential Revision: http://reviews.llvm.org/D16203
llvm-svn: 261004
2016-02-17 04:02:39 +08:00
|
|
|
NumCopiesRemoved++;
|
2016-02-18 05:16:53 +08:00
|
|
|
SmallestDef =
|
|
|
|
TRI->isSubRegister(SmallestDef, DefReg) ? DefReg : SmallestDef;
|
[AArch64] Add pass to remove redundant copy after RA
Summary:
This change will add a pass to remove unnecessary zero copies in target blocks
of cbz/cbnz instructions. E.g., the copy instruction in the code below can be
removed because the cbz jumps to BB1 when x0 is zero :
BB0:
cbz x0, .BB1
BB1:
mov x0, xzr
Jun
Reviewers: gberry, jmolloy, HaoLiu, MatzeB, mcrosier
Subscribers: mcrosier, mssimpso, haicheng, bmakam, llvm-commits, aemerson, rengolin
Differential Revision: http://reviews.llvm.org/D16203
llvm-svn: 261004
2016-02-17 04:02:39 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-18 05:16:53 +08:00
|
|
|
if (MI->modifiesRegister(TargetReg, TRI))
|
|
|
|
break;
|
|
|
|
}
|
[AArch64] Add pass to remove redundant copy after RA
Summary:
This change will add a pass to remove unnecessary zero copies in target blocks
of cbz/cbnz instructions. E.g., the copy instruction in the code below can be
removed because the cbz jumps to BB1 when x0 is zero :
BB0:
cbz x0, .BB1
BB1:
mov x0, xzr
Jun
Reviewers: gberry, jmolloy, HaoLiu, MatzeB, mcrosier
Subscribers: mcrosier, mssimpso, haicheng, bmakam, llvm-commits, aemerson, rengolin
Differential Revision: http://reviews.llvm.org/D16203
llvm-svn: 261004
2016-02-17 04:02:39 +08:00
|
|
|
|
2016-02-18 05:16:53 +08:00
|
|
|
if (!Changed)
|
|
|
|
return false;
|
[AArch64] Add pass to remove redundant copy after RA
Summary:
This change will add a pass to remove unnecessary zero copies in target blocks
of cbz/cbnz instructions. E.g., the copy instruction in the code below can be
removed because the cbz jumps to BB1 when x0 is zero :
BB0:
cbz x0, .BB1
BB1:
mov x0, xzr
Jun
Reviewers: gberry, jmolloy, HaoLiu, MatzeB, mcrosier
Subscribers: mcrosier, mssimpso, haicheng, bmakam, llvm-commits, aemerson, rengolin
Differential Revision: http://reviews.llvm.org/D16203
llvm-svn: 261004
2016-02-17 04:02:39 +08:00
|
|
|
|
2016-02-18 05:16:53 +08:00
|
|
|
// Otherwise, we have to fixup the use-def chain, starting with the
|
|
|
|
// CBZ/CBNZ. Conservatively mark as much as we can live.
|
|
|
|
CompBr->clearRegisterKills(SmallestDef, TRI);
|
[AArch64] Add pass to remove redundant copy after RA
Summary:
This change will add a pass to remove unnecessary zero copies in target blocks
of cbz/cbnz instructions. E.g., the copy instruction in the code below can be
removed because the cbz jumps to BB1 when x0 is zero :
BB0:
cbz x0, .BB1
BB1:
mov x0, xzr
Jun
Reviewers: gberry, jmolloy, HaoLiu, MatzeB, mcrosier
Subscribers: mcrosier, mssimpso, haicheng, bmakam, llvm-commits, aemerson, rengolin
Differential Revision: http://reviews.llvm.org/D16203
llvm-svn: 261004
2016-02-17 04:02:39 +08:00
|
|
|
|
2016-08-12 12:32:45 +08:00
|
|
|
if (none_of(TargetRegs, [&](unsigned Reg) { return MBB->isLiveIn(Reg); }))
|
2016-02-18 05:16:53 +08:00
|
|
|
MBB->addLiveIn(TargetReg);
|
|
|
|
|
2016-02-18 07:07:04 +08:00
|
|
|
// Clear any kills of TargetReg between CompBr and the last removed COPY.
|
2016-08-19 01:58:09 +08:00
|
|
|
for (MachineInstr &MMI : make_range(MBB->begin(), LastChange))
|
2016-02-18 07:07:04 +08:00
|
|
|
MMI.clearRegisterKills(SmallestDef, TRI);
|
|
|
|
|
2016-02-18 05:16:53 +08:00
|
|
|
return true;
|
[AArch64] Add pass to remove redundant copy after RA
Summary:
This change will add a pass to remove unnecessary zero copies in target blocks
of cbz/cbnz instructions. E.g., the copy instruction in the code below can be
removed because the cbz jumps to BB1 when x0 is zero :
BB0:
cbz x0, .BB1
BB1:
mov x0, xzr
Jun
Reviewers: gberry, jmolloy, HaoLiu, MatzeB, mcrosier
Subscribers: mcrosier, mssimpso, haicheng, bmakam, llvm-commits, aemerson, rengolin
Differential Revision: http://reviews.llvm.org/D16203
llvm-svn: 261004
2016-02-17 04:02:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool AArch64RedundantCopyElimination::runOnMachineFunction(
|
|
|
|
MachineFunction &MF) {
|
2016-04-26 05:58:52 +08:00
|
|
|
if (skipFunction(*MF.getFunction()))
|
|
|
|
return false;
|
[AArch64] Add pass to remove redundant copy after RA
Summary:
This change will add a pass to remove unnecessary zero copies in target blocks
of cbz/cbnz instructions. E.g., the copy instruction in the code below can be
removed because the cbz jumps to BB1 when x0 is zero :
BB0:
cbz x0, .BB1
BB1:
mov x0, xzr
Jun
Reviewers: gberry, jmolloy, HaoLiu, MatzeB, mcrosier
Subscribers: mcrosier, mssimpso, haicheng, bmakam, llvm-commits, aemerson, rengolin
Differential Revision: http://reviews.llvm.org/D16203
llvm-svn: 261004
2016-02-17 04:02:39 +08:00
|
|
|
TRI = MF.getSubtarget().getRegisterInfo();
|
|
|
|
MRI = &MF.getRegInfo();
|
|
|
|
bool Changed = false;
|
|
|
|
for (MachineBasicBlock &MBB : MF)
|
|
|
|
Changed |= optimizeCopy(&MBB);
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
FunctionPass *llvm::createAArch64RedundantCopyEliminationPass() {
|
|
|
|
return new AArch64RedundantCopyElimination();
|
|
|
|
}
|