2012-01-07 11:02:36 +08:00
|
|
|
//===- MachineCopyPropagation.cpp - Machine Copy Propagation Pass ---------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2012-01-07 11:02:36 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2017-10-04 00:59:13 +08:00
|
|
|
// This is an extremely simple MachineInstr-level copy propagation pass.
|
2012-01-07 11:02:36 +08:00
|
|
|
//
|
2018-02-28 00:59:10 +08:00
|
|
|
// This pass forwards the source of COPYs to the users of their destinations
|
|
|
|
// when doing so is legal. For example:
|
|
|
|
//
|
|
|
|
// %reg1 = COPY %reg0
|
|
|
|
// ...
|
|
|
|
// ... = OP %reg1
|
|
|
|
//
|
|
|
|
// If
|
|
|
|
// - %reg0 has not been clobbered by the time of the use of %reg1
|
|
|
|
// - the register class constraints are satisfied
|
|
|
|
// - the COPY def is the only value that reaches OP
|
|
|
|
// then this pass replaces the above with:
|
|
|
|
//
|
|
|
|
// %reg1 = COPY %reg0
|
|
|
|
// ...
|
|
|
|
// ... = OP %reg0
|
|
|
|
//
|
|
|
|
// This pass also removes some redundant COPYs. For example:
|
|
|
|
//
|
|
|
|
// %R1 = COPY %R0
|
|
|
|
// ... // No clobber of %R1
|
|
|
|
// %R0 = COPY %R1 <<< Removed
|
|
|
|
//
|
|
|
|
// or
|
|
|
|
//
|
|
|
|
// %R1 = COPY %R0
|
|
|
|
// ... // No clobber of %R0
|
|
|
|
// %R1 = COPY %R0 <<< Removed
|
|
|
|
//
|
2019-12-05 14:01:00 +08:00
|
|
|
// or
|
|
|
|
//
|
|
|
|
// $R0 = OP ...
|
|
|
|
// ... // No read/clobber of $R0 and $R1
|
|
|
|
// $R1 = COPY $R0 // $R0 is killed
|
|
|
|
// Replace $R0 with $R1 and remove the COPY
|
|
|
|
// $R1 = OP ...
|
|
|
|
// ...
|
|
|
|
//
|
2012-01-07 11:02:36 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2017-08-30 06:32:07 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/ADT/SetVector.h"
|
2020-06-13 00:08:01 +08:00
|
|
|
#include "llvm/ADT/SmallSet.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
2017-08-30 06:32:07 +08:00
|
|
|
#include "llvm/ADT/iterator_range.h"
|
|
|
|
#include "llvm/CodeGen/MachineBasicBlock.h"
|
2012-01-07 11:02:36 +08:00
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
2017-08-30 06:32:07 +08:00
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
|
|
|
#include "llvm/CodeGen/MachineOperand.h"
|
2012-10-16 05:57:41 +08:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2022-03-10 20:54:41 +08:00
|
|
|
#include "llvm/CodeGen/TargetInstrInfo.h"
|
2017-11-17 09:07:10 +08:00
|
|
|
#include "llvm/CodeGen/TargetRegisterInfo.h"
|
|
|
|
#include "llvm/CodeGen/TargetSubtargetInfo.h"
|
Sink all InitializePasses.h includes
This file lists every pass in LLVM, and is included by Pass.h, which is
very popular. Every time we add, remove, or rename a pass in LLVM, it
caused lots of recompilation.
I found this fact by looking at this table, which is sorted by the
number of times a file was changed over the last 100,000 git commits
multiplied by the number of object files that depend on it in the
current checkout:
recompiles touches affected_files header
342380 95 3604 llvm/include/llvm/ADT/STLExtras.h
314730 234 1345 llvm/include/llvm/InitializePasses.h
307036 118 2602 llvm/include/llvm/ADT/APInt.h
213049 59 3611 llvm/include/llvm/Support/MathExtras.h
170422 47 3626 llvm/include/llvm/Support/Compiler.h
162225 45 3605 llvm/include/llvm/ADT/Optional.h
158319 63 2513 llvm/include/llvm/ADT/Triple.h
140322 39 3598 llvm/include/llvm/ADT/StringRef.h
137647 59 2333 llvm/include/llvm/Support/Error.h
131619 73 1803 llvm/include/llvm/Support/FileSystem.h
Before this change, touching InitializePasses.h would cause 1345 files
to recompile. After this change, touching it only causes 550 compiles in
an incremental rebuild.
Reviewers: bkramer, asbirlea, bollu, jdoerfert
Differential Revision: https://reviews.llvm.org/D70211
2019-11-14 05:15:01 +08:00
|
|
|
#include "llvm/InitializePasses.h"
|
2017-08-30 06:32:07 +08:00
|
|
|
#include "llvm/MC/MCRegisterInfo.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Pass.h"
|
2012-01-07 11:02:36 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2018-02-28 00:59:10 +08:00
|
|
|
#include "llvm/Support/DebugCounter.h"
|
2012-01-07 11:02:36 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2017-08-30 06:32:07 +08:00
|
|
|
#include <cassert>
|
|
|
|
#include <iterator>
|
|
|
|
|
2012-01-07 11:02:36 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2017-05-26 05:26:32 +08:00
|
|
|
#define DEBUG_TYPE "machine-cp"
|
2014-04-22 10:02:50 +08:00
|
|
|
|
2012-01-07 11:02:36 +08:00
|
|
|
STATISTIC(NumDeletes, "Number of dead copies deleted");
|
2018-02-28 00:59:10 +08:00
|
|
|
STATISTIC(NumCopyForwards, "Number of copy uses forwarded");
|
2019-12-30 16:31:41 +08:00
|
|
|
STATISTIC(NumCopyBackwardPropagated, "Number of copy defs backward propagated");
|
2018-02-28 00:59:10 +08:00
|
|
|
DEBUG_COUNTER(FwdCounter, "machine-cp-fwd",
|
|
|
|
"Controls which register COPYs are forwarded");
|
2012-01-07 11:02:36 +08:00
|
|
|
|
|
|
|
namespace {
|
2017-08-30 06:32:07 +08:00
|
|
|
|
2018-09-21 08:51:04 +08:00
|
|
|
class CopyTracker {
|
2018-10-23 03:51:31 +08:00
|
|
|
struct CopyInfo {
|
|
|
|
MachineInstr *MI;
|
2020-10-13 00:36:01 +08:00
|
|
|
SmallVector<MCRegister, 4> DefRegs;
|
2018-10-23 03:51:31 +08:00
|
|
|
bool Avail;
|
|
|
|
};
|
2018-09-21 08:51:04 +08:00
|
|
|
|
2020-10-13 00:36:01 +08:00
|
|
|
DenseMap<MCRegister, CopyInfo> Copies;
|
2018-09-21 08:51:04 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
/// Mark all of the given registers and their subregisters as unavailable for
|
|
|
|
/// copying.
|
2020-10-13 00:36:01 +08:00
|
|
|
void markRegsUnavailable(ArrayRef<MCRegister> Regs,
|
2018-10-23 03:51:31 +08:00
|
|
|
const TargetRegisterInfo &TRI) {
|
2020-10-13 00:36:01 +08:00
|
|
|
for (MCRegister Reg : Regs) {
|
2018-09-21 08:51:04 +08:00
|
|
|
// Source of copy is no longer available for propagation.
|
2018-10-23 03:51:31 +08:00
|
|
|
for (MCRegUnitIterator RUI(Reg, &TRI); RUI.isValid(); ++RUI) {
|
|
|
|
auto CI = Copies.find(*RUI);
|
|
|
|
if (CI != Copies.end())
|
|
|
|
CI->second.Avail = false;
|
|
|
|
}
|
2018-09-21 08:51:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-05 14:01:00 +08:00
|
|
|
/// Remove register from copy maps.
|
2020-10-13 00:36:01 +08:00
|
|
|
void invalidateRegister(MCRegister Reg, const TargetRegisterInfo &TRI) {
|
2019-12-05 14:01:00 +08:00
|
|
|
// Since Reg might be a subreg of some registers, only invalidate Reg is not
|
|
|
|
// enough. We have to find the COPY defines Reg or registers defined by Reg
|
|
|
|
// and invalidate all of them.
|
2020-10-13 00:36:01 +08:00
|
|
|
SmallSet<MCRegister, 8> RegsToInvalidate;
|
2020-06-13 00:08:01 +08:00
|
|
|
RegsToInvalidate.insert(Reg);
|
2019-12-05 14:01:00 +08:00
|
|
|
for (MCRegUnitIterator RUI(Reg, &TRI); RUI.isValid(); ++RUI) {
|
|
|
|
auto I = Copies.find(*RUI);
|
|
|
|
if (I != Copies.end()) {
|
|
|
|
if (MachineInstr *MI = I->second.MI) {
|
2020-10-13 00:36:01 +08:00
|
|
|
RegsToInvalidate.insert(MI->getOperand(0).getReg().asMCReg());
|
|
|
|
RegsToInvalidate.insert(MI->getOperand(1).getReg().asMCReg());
|
2019-12-05 14:01:00 +08:00
|
|
|
}
|
|
|
|
RegsToInvalidate.insert(I->second.DefRegs.begin(),
|
|
|
|
I->second.DefRegs.end());
|
|
|
|
}
|
|
|
|
}
|
2020-10-13 00:36:01 +08:00
|
|
|
for (MCRegister InvalidReg : RegsToInvalidate)
|
2019-12-05 14:01:00 +08:00
|
|
|
for (MCRegUnitIterator RUI(InvalidReg, &TRI); RUI.isValid(); ++RUI)
|
|
|
|
Copies.erase(*RUI);
|
|
|
|
}
|
|
|
|
|
2018-09-21 08:51:04 +08:00
|
|
|
/// Clobber a single register, removing it from the tracker's copy maps.
|
2020-10-13 00:36:01 +08:00
|
|
|
void clobberRegister(MCRegister Reg, const TargetRegisterInfo &TRI) {
|
2018-10-23 03:51:31 +08:00
|
|
|
for (MCRegUnitIterator RUI(Reg, &TRI); RUI.isValid(); ++RUI) {
|
|
|
|
auto I = Copies.find(*RUI);
|
|
|
|
if (I != Copies.end()) {
|
|
|
|
// When we clobber the source of a copy, we need to clobber everything
|
|
|
|
// it defined.
|
|
|
|
markRegsUnavailable(I->second.DefRegs, TRI);
|
|
|
|
// When we clobber the destination of a copy, we need to clobber the
|
|
|
|
// whole register it defined.
|
|
|
|
if (MachineInstr *MI = I->second.MI)
|
2020-10-13 00:36:01 +08:00
|
|
|
markRegsUnavailable({MI->getOperand(0).getReg().asMCReg()}, TRI);
|
2018-10-23 03:51:31 +08:00
|
|
|
// Now we can erase the copy.
|
|
|
|
Copies.erase(I);
|
2018-09-21 08:51:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Add this copy's registers into the tracker's copy maps.
|
2018-10-23 03:51:31 +08:00
|
|
|
void trackCopy(MachineInstr *MI, const TargetRegisterInfo &TRI) {
|
|
|
|
assert(MI->isCopy() && "Tracking non-copy?");
|
2018-09-21 08:51:04 +08:00
|
|
|
|
2020-10-13 00:36:01 +08:00
|
|
|
MCRegister Def = MI->getOperand(0).getReg().asMCReg();
|
|
|
|
MCRegister Src = MI->getOperand(1).getReg().asMCReg();
|
2018-09-21 08:51:04 +08:00
|
|
|
|
|
|
|
// Remember Def is defined by the copy.
|
2018-10-23 03:51:31 +08:00
|
|
|
for (MCRegUnitIterator RUI(Def, &TRI); RUI.isValid(); ++RUI)
|
|
|
|
Copies[*RUI] = {MI, {}, true};
|
2018-09-21 08:51:04 +08:00
|
|
|
|
|
|
|
// Remember source that's copied to Def. Once it's clobbered, then
|
|
|
|
// it's no longer available for copy propagation.
|
2018-10-23 03:51:31 +08:00
|
|
|
for (MCRegUnitIterator RUI(Src, &TRI); RUI.isValid(); ++RUI) {
|
|
|
|
auto I = Copies.insert({*RUI, {nullptr, {}, false}});
|
|
|
|
auto &Copy = I.first->second;
|
|
|
|
if (!is_contained(Copy.DefRegs, Def))
|
|
|
|
Copy.DefRegs.push_back(Def);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool hasAnyCopies() {
|
|
|
|
return !Copies.empty();
|
2018-09-21 08:51:04 +08:00
|
|
|
}
|
|
|
|
|
2020-10-13 00:36:01 +08:00
|
|
|
MachineInstr *findCopyForUnit(MCRegister RegUnit,
|
|
|
|
const TargetRegisterInfo &TRI,
|
|
|
|
bool MustBeAvailable = false) {
|
2018-10-23 03:51:31 +08:00
|
|
|
auto CI = Copies.find(RegUnit);
|
|
|
|
if (CI == Copies.end())
|
|
|
|
return nullptr;
|
|
|
|
if (MustBeAvailable && !CI->second.Avail)
|
|
|
|
return nullptr;
|
|
|
|
return CI->second.MI;
|
|
|
|
}
|
2018-09-21 08:51:04 +08:00
|
|
|
|
2020-10-13 00:36:01 +08:00
|
|
|
MachineInstr *findCopyDefViaUnit(MCRegister RegUnit,
|
|
|
|
const TargetRegisterInfo &TRI) {
|
2019-12-05 14:01:00 +08:00
|
|
|
auto CI = Copies.find(RegUnit);
|
|
|
|
if (CI == Copies.end())
|
|
|
|
return nullptr;
|
|
|
|
if (CI->second.DefRegs.size() != 1)
|
|
|
|
return nullptr;
|
|
|
|
MCRegUnitIterator RUI(CI->second.DefRegs[0], &TRI);
|
|
|
|
return findCopyForUnit(*RUI, TRI, true);
|
|
|
|
}
|
|
|
|
|
2020-10-13 00:36:01 +08:00
|
|
|
MachineInstr *findAvailBackwardCopy(MachineInstr &I, MCRegister Reg,
|
2019-12-05 14:01:00 +08:00
|
|
|
const TargetRegisterInfo &TRI) {
|
|
|
|
MCRegUnitIterator RUI(Reg, &TRI);
|
|
|
|
MachineInstr *AvailCopy = findCopyDefViaUnit(*RUI, TRI);
|
|
|
|
if (!AvailCopy ||
|
|
|
|
!TRI.isSubRegisterEq(AvailCopy->getOperand(1).getReg(), Reg))
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
Register AvailSrc = AvailCopy->getOperand(1).getReg();
|
|
|
|
Register AvailDef = AvailCopy->getOperand(0).getReg();
|
|
|
|
for (const MachineInstr &MI :
|
|
|
|
make_range(AvailCopy->getReverseIterator(), I.getReverseIterator()))
|
|
|
|
for (const MachineOperand &MO : MI.operands())
|
|
|
|
if (MO.isRegMask())
|
|
|
|
// FIXME: Shall we simultaneously invalidate AvailSrc or AvailDef?
|
|
|
|
if (MO.clobbersPhysReg(AvailSrc) || MO.clobbersPhysReg(AvailDef))
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
return AvailCopy;
|
|
|
|
}
|
|
|
|
|
2020-10-13 00:36:01 +08:00
|
|
|
MachineInstr *findAvailCopy(MachineInstr &DestCopy, MCRegister Reg,
|
2018-10-23 03:51:31 +08:00
|
|
|
const TargetRegisterInfo &TRI) {
|
|
|
|
// We check the first RegUnit here, since we'll only be interested in the
|
|
|
|
// copy if it copies the entire register anyway.
|
|
|
|
MCRegUnitIterator RUI(Reg, &TRI);
|
|
|
|
MachineInstr *AvailCopy =
|
|
|
|
findCopyForUnit(*RUI, TRI, /*MustBeAvailable=*/true);
|
|
|
|
if (!AvailCopy ||
|
|
|
|
!TRI.isSubRegisterEq(AvailCopy->getOperand(0).getReg(), Reg))
|
2018-09-25 12:45:25 +08:00
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
// Check that the available copy isn't clobbered by any regmasks between
|
|
|
|
// itself and the destination.
|
Apply llvm-prefer-register-over-unsigned from clang-tidy to LLVM
Summary:
This clang-tidy check is looking for unsigned integer variables whose initializer
starts with an implicit cast from llvm::Register and changes the type of the
variable to llvm::Register (dropping the llvm:: where possible).
Partial reverts in:
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
X86FixupLEAs.cpp - Some functions return unsigned and arguably should be MCRegister
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
HexagonBitSimplify.cpp - Function takes BitTracker::RegisterRef which appears to be unsigned&
MachineVerifier.cpp - Ambiguous operator==() given MCRegister and const Register
PPCFastISel.cpp - No Register::operator-=()
PeepholeOptimizer.cpp - TargetInstrInfo::optimizeLoadInstr() takes an unsigned&
MachineTraceMetrics.cpp - MachineTraceMetrics lacks a suitable constructor
Manual fixups in:
ARMFastISel.cpp - ARMEmitLoad() now takes a Register& instead of unsigned&
HexagonSplitDouble.cpp - Ternary operator was ambiguous between unsigned/Register
HexagonConstExtenders.cpp - Has a local class named Register, used llvm::Register instead of Register.
PPCFastISel.cpp - PPCEmitLoad() now takes a Register& instead of unsigned&
Depends on D65919
Reviewers: arsenm, bogner, craig.topper, RKSimon
Reviewed By: arsenm
Subscribers: RKSimon, craig.topper, lenary, aemerson, wuzish, jholewinski, MatzeB, qcolombet, dschuff, jyknight, dylanmckay, sdardis, nemanjai, jvesely, wdng, nhaehnle, sbc100, jgravelle-google, kristof.beyls, hiraditya, aheejin, kbarton, fedor.sergeev, javed.absar, asb, rbar, johnrusso, simoncook, apazos, sabuasal, niosHD, jrtc27, MaskRay, zzheng, edward-jones, atanasyan, rogfer01, MartinMosbeck, brucehoult, the_o, tpr, PkmX, jocewei, jsji, Petar.Avramovic, asbirlea, Jim, s.egerton, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D65962
llvm-svn: 369041
2019-08-16 03:22:08 +08:00
|
|
|
Register AvailSrc = AvailCopy->getOperand(1).getReg();
|
|
|
|
Register AvailDef = AvailCopy->getOperand(0).getReg();
|
2018-09-25 12:45:25 +08:00
|
|
|
for (const MachineInstr &MI :
|
2018-10-23 03:51:31 +08:00
|
|
|
make_range(AvailCopy->getIterator(), DestCopy.getIterator()))
|
2018-09-25 12:45:25 +08:00
|
|
|
for (const MachineOperand &MO : MI.operands())
|
|
|
|
if (MO.isRegMask())
|
|
|
|
if (MO.clobbersPhysReg(AvailSrc) || MO.clobbersPhysReg(AvailDef))
|
|
|
|
return nullptr;
|
|
|
|
|
2018-10-23 03:51:31 +08:00
|
|
|
return AvailCopy;
|
2018-09-21 08:51:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void clear() {
|
2018-10-23 03:51:31 +08:00
|
|
|
Copies.clear();
|
2018-09-21 08:51:04 +08:00
|
|
|
}
|
|
|
|
};
|
2016-02-26 11:18:50 +08:00
|
|
|
|
2018-09-21 08:08:33 +08:00
|
|
|
class MachineCopyPropagation : public MachineFunctionPass {
|
|
|
|
const TargetRegisterInfo *TRI;
|
|
|
|
const TargetInstrInfo *TII;
|
|
|
|
const MachineRegisterInfo *MRI;
|
|
|
|
|
|
|
|
public:
|
|
|
|
static char ID; // Pass identification, replacement for typeid
|
|
|
|
|
|
|
|
MachineCopyPropagation() : MachineFunctionPass(ID) {
|
|
|
|
initializeMachineCopyPropagationPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
|
|
|
AU.setPreservesCFG();
|
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool runOnMachineFunction(MachineFunction &MF) override;
|
|
|
|
|
|
|
|
MachineFunctionProperties getRequiredProperties() const override {
|
|
|
|
return MachineFunctionProperties().set(
|
|
|
|
MachineFunctionProperties::Property::NoVRegs);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2019-08-14 20:20:02 +08:00
|
|
|
typedef enum { DebugUse = false, RegularUse = true } DebugType;
|
|
|
|
|
2020-10-13 00:36:01 +08:00
|
|
|
void ReadRegister(MCRegister Reg, MachineInstr &Reader, DebugType DT);
|
2019-12-05 14:01:00 +08:00
|
|
|
void ForwardCopyPropagateBlock(MachineBasicBlock &MBB);
|
|
|
|
void BackwardCopyPropagateBlock(MachineBasicBlock &MBB);
|
2020-10-13 00:36:01 +08:00
|
|
|
bool eraseIfRedundant(MachineInstr &Copy, MCRegister Src, MCRegister Def);
|
2018-09-21 08:08:33 +08:00
|
|
|
void forwardUses(MachineInstr &MI);
|
2019-12-05 14:01:00 +08:00
|
|
|
void propagateDefs(MachineInstr &MI);
|
2018-09-21 08:08:33 +08:00
|
|
|
bool isForwardableRegClassCopy(const MachineInstr &Copy,
|
|
|
|
const MachineInstr &UseI, unsigned UseIdx);
|
2019-12-05 14:01:00 +08:00
|
|
|
bool isBackwardPropagatableRegClassCopy(const MachineInstr &Copy,
|
|
|
|
const MachineInstr &UseI,
|
|
|
|
unsigned UseIdx);
|
2018-09-21 08:08:33 +08:00
|
|
|
bool hasImplicitOverlap(const MachineInstr &MI, const MachineOperand &Use);
|
2020-07-29 23:17:47 +08:00
|
|
|
bool hasOverlappingMultipleDef(const MachineInstr &MI,
|
|
|
|
const MachineOperand &MODef, Register Def);
|
2018-09-21 08:08:33 +08:00
|
|
|
|
|
|
|
/// Candidates for deletion.
|
|
|
|
SmallSetVector<MachineInstr *, 8> MaybeDeadCopies;
|
|
|
|
|
2019-08-14 20:20:02 +08:00
|
|
|
/// Multimap tracking debug users in current BB
|
2021-05-10 21:00:01 +08:00
|
|
|
DenseMap<MachineInstr *, SmallSet<MachineInstr *, 2>> CopyDbgUsers;
|
2019-08-14 20:20:02 +08:00
|
|
|
|
2018-09-21 08:51:04 +08:00
|
|
|
CopyTracker Tracker;
|
2018-09-21 08:08:33 +08:00
|
|
|
|
|
|
|
bool Changed;
|
|
|
|
};
|
2017-08-30 06:32:07 +08:00
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2012-01-07 11:02:36 +08:00
|
|
|
char MachineCopyPropagation::ID = 0;
|
2017-08-30 06:32:07 +08:00
|
|
|
|
2012-02-09 05:23:13 +08:00
|
|
|
char &llvm::MachineCopyPropagationID = MachineCopyPropagation::ID;
|
2012-01-07 11:02:36 +08:00
|
|
|
|
2017-05-26 05:26:32 +08:00
|
|
|
INITIALIZE_PASS(MachineCopyPropagation, DEBUG_TYPE,
|
2012-01-07 11:02:36 +08:00
|
|
|
"Machine Copy Propagation Pass", false, false)
|
|
|
|
|
2020-10-13 00:36:01 +08:00
|
|
|
void MachineCopyPropagation::ReadRegister(MCRegister Reg, MachineInstr &Reader,
|
2019-08-14 20:20:02 +08:00
|
|
|
DebugType DT) {
|
2017-02-04 10:27:20 +08:00
|
|
|
// If 'Reg' is defined by a copy, the copy is no longer a candidate
|
2019-08-14 20:20:02 +08:00
|
|
|
// for elimination. If a copy is "read" by a debug user, record the user
|
|
|
|
// for propagation.
|
2018-10-23 03:51:31 +08:00
|
|
|
for (MCRegUnitIterator RUI(Reg, TRI); RUI.isValid(); ++RUI) {
|
|
|
|
if (MachineInstr *Copy = Tracker.findCopyForUnit(*RUI, *TRI)) {
|
2019-08-14 20:20:02 +08:00
|
|
|
if (DT == RegularUse) {
|
|
|
|
LLVM_DEBUG(dbgs() << "MCP: Copy is used - not dead: "; Copy->dump());
|
|
|
|
MaybeDeadCopies.remove(Copy);
|
|
|
|
} else {
|
2021-05-10 21:00:01 +08:00
|
|
|
CopyDbgUsers[Copy].insert(&Reader);
|
2019-08-14 20:20:02 +08:00
|
|
|
}
|
2017-02-04 10:27:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-26 11:18:55 +08:00
|
|
|
/// Return true if \p PreviousCopy did copy register \p Src to register \p Def.
|
|
|
|
/// This fact may have been obscured by sub register usage or may not be true at
|
|
|
|
/// all even though Src and Def are subregisters of the registers used in
|
|
|
|
/// PreviousCopy. e.g.
|
|
|
|
/// isNopCopy("ecx = COPY eax", AX, CX) == true
|
|
|
|
/// isNopCopy("ecx = COPY eax", AH, CL) == false
|
2020-10-13 00:36:01 +08:00
|
|
|
static bool isNopCopy(const MachineInstr &PreviousCopy, MCRegister Src,
|
|
|
|
MCRegister Def, const TargetRegisterInfo *TRI) {
|
|
|
|
MCRegister PreviousSrc = PreviousCopy.getOperand(1).getReg().asMCReg();
|
|
|
|
MCRegister PreviousDef = PreviousCopy.getOperand(0).getReg().asMCReg();
|
2020-09-02 03:14:32 +08:00
|
|
|
if (Src == PreviousSrc && Def == PreviousDef)
|
2012-02-21 07:28:17 +08:00
|
|
|
return true;
|
2016-02-26 11:18:55 +08:00
|
|
|
if (!TRI->isSubRegister(PreviousSrc, Src))
|
|
|
|
return false;
|
|
|
|
unsigned SubIdx = TRI->getSubRegIndex(PreviousSrc, Src);
|
|
|
|
return SubIdx == TRI->getSubRegIndex(PreviousDef, Def);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Remove instruction \p Copy if there exists a previous copy that copies the
|
|
|
|
/// register \p Src to the register \p Def; This may happen indirectly by
|
|
|
|
/// copying the super registers.
|
2020-10-13 00:36:01 +08:00
|
|
|
bool MachineCopyPropagation::eraseIfRedundant(MachineInstr &Copy,
|
|
|
|
MCRegister Src, MCRegister Def) {
|
2016-02-26 11:18:55 +08:00
|
|
|
// Avoid eliminating a copy from/to a reserved registers as we cannot predict
|
|
|
|
// the value (Example: The sparc zero register is writable but stays zero).
|
|
|
|
if (MRI->isReserved(Src) || MRI->isReserved(Def))
|
|
|
|
return false;
|
2012-02-21 07:28:17 +08:00
|
|
|
|
2016-02-26 11:18:55 +08:00
|
|
|
// Search for an existing copy.
|
2018-10-23 03:51:31 +08:00
|
|
|
MachineInstr *PrevCopy = Tracker.findAvailCopy(Copy, Def, *TRI);
|
2018-09-21 08:51:04 +08:00
|
|
|
if (!PrevCopy)
|
2016-02-26 11:18:55 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Check that the existing copy uses the correct sub registers.
|
2018-09-21 08:51:04 +08:00
|
|
|
if (PrevCopy->getOperand(0).isDead())
|
2017-11-10 20:21:10 +08:00
|
|
|
return false;
|
2018-09-21 08:51:04 +08:00
|
|
|
if (!isNopCopy(*PrevCopy, Src, Def, TRI))
|
2016-02-26 11:18:55 +08:00
|
|
|
return false;
|
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "MCP: copy is a NOP, removing: "; Copy.dump());
|
2016-02-26 11:18:55 +08:00
|
|
|
|
|
|
|
// Copy was redundantly redefining either Src or Def. Remove earlier kill
|
|
|
|
// flags between Copy and PrevCopy because the value will be reused now.
|
|
|
|
assert(Copy.isCopy());
|
Apply llvm-prefer-register-over-unsigned from clang-tidy to LLVM
Summary:
This clang-tidy check is looking for unsigned integer variables whose initializer
starts with an implicit cast from llvm::Register and changes the type of the
variable to llvm::Register (dropping the llvm:: where possible).
Partial reverts in:
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
X86FixupLEAs.cpp - Some functions return unsigned and arguably should be MCRegister
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
HexagonBitSimplify.cpp - Function takes BitTracker::RegisterRef which appears to be unsigned&
MachineVerifier.cpp - Ambiguous operator==() given MCRegister and const Register
PPCFastISel.cpp - No Register::operator-=()
PeepholeOptimizer.cpp - TargetInstrInfo::optimizeLoadInstr() takes an unsigned&
MachineTraceMetrics.cpp - MachineTraceMetrics lacks a suitable constructor
Manual fixups in:
ARMFastISel.cpp - ARMEmitLoad() now takes a Register& instead of unsigned&
HexagonSplitDouble.cpp - Ternary operator was ambiguous between unsigned/Register
HexagonConstExtenders.cpp - Has a local class named Register, used llvm::Register instead of Register.
PPCFastISel.cpp - PPCEmitLoad() now takes a Register& instead of unsigned&
Depends on D65919
Reviewers: arsenm, bogner, craig.topper, RKSimon
Reviewed By: arsenm
Subscribers: RKSimon, craig.topper, lenary, aemerson, wuzish, jholewinski, MatzeB, qcolombet, dschuff, jyknight, dylanmckay, sdardis, nemanjai, jvesely, wdng, nhaehnle, sbc100, jgravelle-google, kristof.beyls, hiraditya, aheejin, kbarton, fedor.sergeev, javed.absar, asb, rbar, johnrusso, simoncook, apazos, sabuasal, niosHD, jrtc27, MaskRay, zzheng, edward-jones, atanasyan, rogfer01, MartinMosbeck, brucehoult, the_o, tpr, PkmX, jocewei, jsji, Petar.Avramovic, asbirlea, Jim, s.egerton, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D65962
llvm-svn: 369041
2019-08-16 03:22:08 +08:00
|
|
|
Register CopyDef = Copy.getOperand(0).getReg();
|
2016-02-26 11:18:55 +08:00
|
|
|
assert(CopyDef == Src || CopyDef == Def);
|
|
|
|
for (MachineInstr &MI :
|
2018-09-21 08:51:04 +08:00
|
|
|
make_range(PrevCopy->getIterator(), Copy.getIterator()))
|
2016-02-26 11:18:55 +08:00
|
|
|
MI.clearRegisterKills(CopyDef, TRI);
|
|
|
|
|
|
|
|
Copy.eraseFromParent();
|
|
|
|
Changed = true;
|
|
|
|
++NumDeletes;
|
|
|
|
return true;
|
2012-02-21 07:28:17 +08:00
|
|
|
}
|
|
|
|
|
2019-12-05 14:01:00 +08:00
|
|
|
bool MachineCopyPropagation::isBackwardPropagatableRegClassCopy(
|
|
|
|
const MachineInstr &Copy, const MachineInstr &UseI, unsigned UseIdx) {
|
|
|
|
Register Def = Copy.getOperand(0).getReg();
|
|
|
|
|
|
|
|
if (const TargetRegisterClass *URC =
|
|
|
|
UseI.getRegClassConstraint(UseIdx, TII, TRI))
|
|
|
|
return URC->contains(Def);
|
|
|
|
|
|
|
|
// We don't process further if UseI is a COPY, since forward copy propagation
|
|
|
|
// should handle that.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-02-28 00:59:10 +08:00
|
|
|
/// Decide whether we should forward the source of \param Copy to its use in
|
|
|
|
/// \param UseI based on the physical register class constraints of the opcode
|
|
|
|
/// and avoiding introducing more cross-class COPYs.
|
|
|
|
bool MachineCopyPropagation::isForwardableRegClassCopy(const MachineInstr &Copy,
|
|
|
|
const MachineInstr &UseI,
|
|
|
|
unsigned UseIdx) {
|
|
|
|
|
Apply llvm-prefer-register-over-unsigned from clang-tidy to LLVM
Summary:
This clang-tidy check is looking for unsigned integer variables whose initializer
starts with an implicit cast from llvm::Register and changes the type of the
variable to llvm::Register (dropping the llvm:: where possible).
Partial reverts in:
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
X86FixupLEAs.cpp - Some functions return unsigned and arguably should be MCRegister
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
HexagonBitSimplify.cpp - Function takes BitTracker::RegisterRef which appears to be unsigned&
MachineVerifier.cpp - Ambiguous operator==() given MCRegister and const Register
PPCFastISel.cpp - No Register::operator-=()
PeepholeOptimizer.cpp - TargetInstrInfo::optimizeLoadInstr() takes an unsigned&
MachineTraceMetrics.cpp - MachineTraceMetrics lacks a suitable constructor
Manual fixups in:
ARMFastISel.cpp - ARMEmitLoad() now takes a Register& instead of unsigned&
HexagonSplitDouble.cpp - Ternary operator was ambiguous between unsigned/Register
HexagonConstExtenders.cpp - Has a local class named Register, used llvm::Register instead of Register.
PPCFastISel.cpp - PPCEmitLoad() now takes a Register& instead of unsigned&
Depends on D65919
Reviewers: arsenm, bogner, craig.topper, RKSimon
Reviewed By: arsenm
Subscribers: RKSimon, craig.topper, lenary, aemerson, wuzish, jholewinski, MatzeB, qcolombet, dschuff, jyknight, dylanmckay, sdardis, nemanjai, jvesely, wdng, nhaehnle, sbc100, jgravelle-google, kristof.beyls, hiraditya, aheejin, kbarton, fedor.sergeev, javed.absar, asb, rbar, johnrusso, simoncook, apazos, sabuasal, niosHD, jrtc27, MaskRay, zzheng, edward-jones, atanasyan, rogfer01, MartinMosbeck, brucehoult, the_o, tpr, PkmX, jocewei, jsji, Petar.Avramovic, asbirlea, Jim, s.egerton, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D65962
llvm-svn: 369041
2019-08-16 03:22:08 +08:00
|
|
|
Register CopySrcReg = Copy.getOperand(1).getReg();
|
2018-02-28 00:59:10 +08:00
|
|
|
|
|
|
|
// If the new register meets the opcode register constraints, then allow
|
|
|
|
// forwarding.
|
|
|
|
if (const TargetRegisterClass *URC =
|
|
|
|
UseI.getRegClassConstraint(UseIdx, TII, TRI))
|
|
|
|
return URC->contains(CopySrcReg);
|
|
|
|
|
|
|
|
if (!UseI.isCopy())
|
|
|
|
return false;
|
|
|
|
|
2021-08-13 12:39:32 +08:00
|
|
|
const TargetRegisterClass *CopySrcRC =
|
|
|
|
TRI->getMinimalPhysRegClass(CopySrcReg);
|
|
|
|
const TargetRegisterClass *UseDstRC =
|
|
|
|
TRI->getMinimalPhysRegClass(UseI.getOperand(0).getReg());
|
|
|
|
const TargetRegisterClass *CrossCopyRC = TRI->getCrossCopyRegClass(CopySrcRC);
|
|
|
|
|
|
|
|
// If cross copy register class is not the same as copy source register class
|
|
|
|
// then it is not possible to copy the register directly and requires a cross
|
|
|
|
// register class copy. Fowarding this copy without checking register class of
|
|
|
|
// UseDst may create additional cross register copies when expanding the copy
|
|
|
|
// instruction in later passes.
|
|
|
|
if (CopySrcRC != CrossCopyRC) {
|
|
|
|
const TargetRegisterClass *CopyDstRC =
|
|
|
|
TRI->getMinimalPhysRegClass(Copy.getOperand(0).getReg());
|
|
|
|
|
|
|
|
// Check if UseDstRC matches the necessary register class to copy from
|
|
|
|
// CopySrc's register class. If so then forwarding the copy will not
|
|
|
|
// introduce any cross-class copys. Else if CopyDstRC matches then keep the
|
|
|
|
// copy and do not forward. If neither UseDstRC or CopyDstRC matches then
|
|
|
|
// we may need a cross register copy later but we do not worry about it
|
|
|
|
// here.
|
|
|
|
if (UseDstRC != CrossCopyRC && CopyDstRC == CrossCopyRC)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-02-28 00:59:10 +08:00
|
|
|
/// COPYs don't have register class constraints, so if the user instruction
|
|
|
|
/// is a COPY, we just try to avoid introducing additional cross-class
|
|
|
|
/// COPYs. For example:
|
|
|
|
///
|
|
|
|
/// RegClassA = COPY RegClassB // Copy parameter
|
|
|
|
/// ...
|
|
|
|
/// RegClassB = COPY RegClassA // UseI parameter
|
|
|
|
///
|
|
|
|
/// which after forwarding becomes
|
|
|
|
///
|
|
|
|
/// RegClassA = COPY RegClassB
|
|
|
|
/// ...
|
|
|
|
/// RegClassB = COPY RegClassB
|
|
|
|
///
|
|
|
|
/// so we have reduced the number of cross-class COPYs and potentially
|
|
|
|
/// introduced a nop COPY that can be removed.
|
|
|
|
const TargetRegisterClass *SuperRC = UseDstRC;
|
|
|
|
for (TargetRegisterClass::sc_iterator SuperRCI = UseDstRC->getSuperClasses();
|
|
|
|
SuperRC; SuperRC = *SuperRCI++)
|
|
|
|
if (SuperRC->contains(CopySrcReg))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Check that \p MI does not have implicit uses that overlap with it's \p Use
|
|
|
|
/// operand (the register being replaced), since these can sometimes be
|
|
|
|
/// implicitly tied to other operands. For example, on AMDGPU:
|
|
|
|
///
|
|
|
|
/// V_MOVRELS_B32_e32 %VGPR2, %M0<imp-use>, %EXEC<imp-use>, %VGPR2_VGPR3_VGPR4_VGPR5<imp-use>
|
|
|
|
///
|
|
|
|
/// the %VGPR2 is implicitly tied to the larger reg operand, but we have no
|
|
|
|
/// way of knowing we need to update the latter when updating the former.
|
|
|
|
bool MachineCopyPropagation::hasImplicitOverlap(const MachineInstr &MI,
|
|
|
|
const MachineOperand &Use) {
|
|
|
|
for (const MachineOperand &MIUse : MI.uses())
|
|
|
|
if (&MIUse != &Use && MIUse.isReg() && MIUse.isImplicit() &&
|
|
|
|
MIUse.isUse() && TRI->regsOverlap(Use.getReg(), MIUse.getReg()))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-07-29 23:17:47 +08:00
|
|
|
/// For an MI that has multiple definitions, check whether \p MI has
|
|
|
|
/// a definition that overlaps with another of its definitions.
|
|
|
|
/// For example, on ARM: umull r9, r9, lr, r0
|
|
|
|
/// The umull instruction is unpredictable unless RdHi and RdLo are different.
|
|
|
|
bool MachineCopyPropagation::hasOverlappingMultipleDef(
|
|
|
|
const MachineInstr &MI, const MachineOperand &MODef, Register Def) {
|
|
|
|
for (const MachineOperand &MIDef : MI.defs()) {
|
|
|
|
if ((&MIDef != &MODef) && MIDef.isReg() &&
|
|
|
|
TRI->regsOverlap(Def, MIDef.getReg()))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-02-28 00:59:10 +08:00
|
|
|
/// Look for available copies whose destination register is used by \p MI and
|
|
|
|
/// replace the use in \p MI with the copy's source register.
|
|
|
|
void MachineCopyPropagation::forwardUses(MachineInstr &MI) {
|
2018-10-23 03:51:31 +08:00
|
|
|
if (!Tracker.hasAnyCopies())
|
2018-02-28 00:59:10 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Look for non-tied explicit vreg uses that have an active COPY
|
|
|
|
// instruction that defines the physical register allocated to them.
|
|
|
|
// Replace the vreg with the source of the active COPY.
|
|
|
|
for (unsigned OpIdx = 0, OpEnd = MI.getNumOperands(); OpIdx < OpEnd;
|
|
|
|
++OpIdx) {
|
|
|
|
MachineOperand &MOUse = MI.getOperand(OpIdx);
|
|
|
|
// Don't forward into undef use operands since doing so can cause problems
|
|
|
|
// with the machine verifier, since it doesn't treat undef reads as reads,
|
|
|
|
// so we can end up with a live range that ends on an undef read, leading to
|
|
|
|
// an error that the live range doesn't end on a read of the live range
|
|
|
|
// register.
|
|
|
|
if (!MOUse.isReg() || MOUse.isTied() || MOUse.isUndef() || MOUse.isDef() ||
|
|
|
|
MOUse.isImplicit())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!MOUse.getReg())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Check that the register is marked 'renamable' so we know it is safe to
|
|
|
|
// rename it without violating any constraints that aren't expressed in the
|
|
|
|
// IR (e.g. ABI or opcode requirements).
|
|
|
|
if (!MOUse.isRenamable())
|
|
|
|
continue;
|
|
|
|
|
2020-10-13 00:36:01 +08:00
|
|
|
MachineInstr *Copy =
|
|
|
|
Tracker.findAvailCopy(MI, MOUse.getReg().asMCReg(), *TRI);
|
2018-09-21 08:51:04 +08:00
|
|
|
if (!Copy)
|
2018-02-28 00:59:10 +08:00
|
|
|
continue;
|
|
|
|
|
Apply llvm-prefer-register-over-unsigned from clang-tidy to LLVM
Summary:
This clang-tidy check is looking for unsigned integer variables whose initializer
starts with an implicit cast from llvm::Register and changes the type of the
variable to llvm::Register (dropping the llvm:: where possible).
Partial reverts in:
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
X86FixupLEAs.cpp - Some functions return unsigned and arguably should be MCRegister
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
HexagonBitSimplify.cpp - Function takes BitTracker::RegisterRef which appears to be unsigned&
MachineVerifier.cpp - Ambiguous operator==() given MCRegister and const Register
PPCFastISel.cpp - No Register::operator-=()
PeepholeOptimizer.cpp - TargetInstrInfo::optimizeLoadInstr() takes an unsigned&
MachineTraceMetrics.cpp - MachineTraceMetrics lacks a suitable constructor
Manual fixups in:
ARMFastISel.cpp - ARMEmitLoad() now takes a Register& instead of unsigned&
HexagonSplitDouble.cpp - Ternary operator was ambiguous between unsigned/Register
HexagonConstExtenders.cpp - Has a local class named Register, used llvm::Register instead of Register.
PPCFastISel.cpp - PPCEmitLoad() now takes a Register& instead of unsigned&
Depends on D65919
Reviewers: arsenm, bogner, craig.topper, RKSimon
Reviewed By: arsenm
Subscribers: RKSimon, craig.topper, lenary, aemerson, wuzish, jholewinski, MatzeB, qcolombet, dschuff, jyknight, dylanmckay, sdardis, nemanjai, jvesely, wdng, nhaehnle, sbc100, jgravelle-google, kristof.beyls, hiraditya, aheejin, kbarton, fedor.sergeev, javed.absar, asb, rbar, johnrusso, simoncook, apazos, sabuasal, niosHD, jrtc27, MaskRay, zzheng, edward-jones, atanasyan, rogfer01, MartinMosbeck, brucehoult, the_o, tpr, PkmX, jocewei, jsji, Petar.Avramovic, asbirlea, Jim, s.egerton, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D65962
llvm-svn: 369041
2019-08-16 03:22:08 +08:00
|
|
|
Register CopyDstReg = Copy->getOperand(0).getReg();
|
2018-09-21 08:51:04 +08:00
|
|
|
const MachineOperand &CopySrc = Copy->getOperand(1);
|
Apply llvm-prefer-register-over-unsigned from clang-tidy to LLVM
Summary:
This clang-tidy check is looking for unsigned integer variables whose initializer
starts with an implicit cast from llvm::Register and changes the type of the
variable to llvm::Register (dropping the llvm:: where possible).
Partial reverts in:
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
X86FixupLEAs.cpp - Some functions return unsigned and arguably should be MCRegister
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
HexagonBitSimplify.cpp - Function takes BitTracker::RegisterRef which appears to be unsigned&
MachineVerifier.cpp - Ambiguous operator==() given MCRegister and const Register
PPCFastISel.cpp - No Register::operator-=()
PeepholeOptimizer.cpp - TargetInstrInfo::optimizeLoadInstr() takes an unsigned&
MachineTraceMetrics.cpp - MachineTraceMetrics lacks a suitable constructor
Manual fixups in:
ARMFastISel.cpp - ARMEmitLoad() now takes a Register& instead of unsigned&
HexagonSplitDouble.cpp - Ternary operator was ambiguous between unsigned/Register
HexagonConstExtenders.cpp - Has a local class named Register, used llvm::Register instead of Register.
PPCFastISel.cpp - PPCEmitLoad() now takes a Register& instead of unsigned&
Depends on D65919
Reviewers: arsenm, bogner, craig.topper, RKSimon
Reviewed By: arsenm
Subscribers: RKSimon, craig.topper, lenary, aemerson, wuzish, jholewinski, MatzeB, qcolombet, dschuff, jyknight, dylanmckay, sdardis, nemanjai, jvesely, wdng, nhaehnle, sbc100, jgravelle-google, kristof.beyls, hiraditya, aheejin, kbarton, fedor.sergeev, javed.absar, asb, rbar, johnrusso, simoncook, apazos, sabuasal, niosHD, jrtc27, MaskRay, zzheng, edward-jones, atanasyan, rogfer01, MartinMosbeck, brucehoult, the_o, tpr, PkmX, jocewei, jsji, Petar.Avramovic, asbirlea, Jim, s.egerton, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D65962
llvm-svn: 369041
2019-08-16 03:22:08 +08:00
|
|
|
Register CopySrcReg = CopySrc.getReg();
|
2018-02-28 00:59:10 +08:00
|
|
|
|
|
|
|
// FIXME: Don't handle partial uses of wider COPYs yet.
|
|
|
|
if (MOUse.getReg() != CopyDstReg) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(
|
|
|
|
dbgs() << "MCP: FIXME! Not forwarding COPY to sub-register use:\n "
|
|
|
|
<< MI);
|
2018-02-28 00:59:10 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't forward COPYs of reserved regs unless they are constant.
|
|
|
|
if (MRI->isReserved(CopySrcReg) && !MRI->isConstantPhysReg(CopySrcReg))
|
|
|
|
continue;
|
|
|
|
|
2018-09-21 08:51:04 +08:00
|
|
|
if (!isForwardableRegClassCopy(*Copy, MI, OpIdx))
|
2018-02-28 00:59:10 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (hasImplicitOverlap(MI, MOUse))
|
|
|
|
continue;
|
|
|
|
|
2019-11-11 19:06:09 +08:00
|
|
|
// Check that the instruction is not a copy that partially overwrites the
|
|
|
|
// original copy source that we are about to use. The tracker mechanism
|
|
|
|
// cannot cope with that.
|
|
|
|
if (MI.isCopy() && MI.modifiesRegister(CopySrcReg, TRI) &&
|
|
|
|
!MI.definesRegister(CopySrcReg)) {
|
|
|
|
LLVM_DEBUG(dbgs() << "MCP: Copy source overlap with dest in " << MI);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-02-28 00:59:10 +08:00
|
|
|
if (!DebugCounter::shouldExecute(FwdCounter)) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "MCP: Skipping forwarding due to debug counter:\n "
|
|
|
|
<< MI);
|
2018-02-28 00:59:10 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "MCP: Replacing " << printReg(MOUse.getReg(), TRI)
|
|
|
|
<< "\n with " << printReg(CopySrcReg, TRI)
|
2018-09-21 08:51:04 +08:00
|
|
|
<< "\n in " << MI << " from " << *Copy);
|
2018-02-28 00:59:10 +08:00
|
|
|
|
|
|
|
MOUse.setReg(CopySrcReg);
|
|
|
|
if (!CopySrc.isRenamable())
|
|
|
|
MOUse.setIsRenamable(false);
|
2021-10-07 18:36:10 +08:00
|
|
|
MOUse.setIsUndef(CopySrc.isUndef());
|
2018-02-28 00:59:10 +08:00
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "MCP: After replacement: " << MI << "\n");
|
2018-02-28 00:59:10 +08:00
|
|
|
|
|
|
|
// Clear kill markers that may have been invalidated.
|
|
|
|
for (MachineInstr &KMI :
|
2018-09-21 08:51:04 +08:00
|
|
|
make_range(Copy->getIterator(), std::next(MI.getIterator())))
|
2018-02-28 00:59:10 +08:00
|
|
|
KMI.clearRegisterKills(CopySrcReg, TRI);
|
|
|
|
|
|
|
|
++NumCopyForwards;
|
|
|
|
Changed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-05 14:01:00 +08:00
|
|
|
void MachineCopyPropagation::ForwardCopyPropagateBlock(MachineBasicBlock &MBB) {
|
|
|
|
LLVM_DEBUG(dbgs() << "MCP: ForwardCopyPropagateBlock " << MBB.getName()
|
|
|
|
<< "\n");
|
MachineCopyPropagation has special logic for removing COPY instructions. It will remove plain COPYs using eraseFromParent(), but if the COPY has imp-defs/imp-uses it will convert it to a KILL, to keep the imp-def around.
This actually totally breaks and causes the machine verifier to cry in several cases, one of which being:
%RAX<def> = COPY %RCX<kill>
%ECX<def> = COPY %EAX<kill>, %RAX<imp-use,kill>
These subregister copies are together identified as noops, so are both removed. However, the second one as it has an imp-use gets converted into a kill:
%ECX<def> = KILL %EAX<kill>, %RAX<imp-use,kill>
As the original COPY has been removed, the verifier goes into tears at the use of undefined EAX and RAX.
There are several hacky solutions to this hacky problem (which is all to do with imp-use/def weirdnesses), but the least hacky I've come up with is to *always* remove COPYs by converting to KILLs. KILLs are no-ops to the code generator so the generated code doesn't change (which is why they were partially used in the first place), but using them also keeps the def/use and imp-def/imp-use chains alive:
%RAX<def> = KILL %RCX<kill>
%ECX<def> = KILL %EAX<kill>, %RAX<imp-use,kill>
The patch passes all test cases including the ones that check the removal of MOVs in this circumstance, along with an extra test I added to check subregister behaviour (which made the machine verifier fall over before my patch).
The patch also adds some DEBUG() statements because the file hadn't got any.
llvm-svn: 199797
2014-01-22 17:12:27 +08:00
|
|
|
|
2021-10-31 22:57:36 +08:00
|
|
|
for (MachineInstr &MI : llvm::make_early_inc_range(MBB)) {
|
2018-03-30 08:56:03 +08:00
|
|
|
// Analyze copies (which don't overlap themselves).
|
2021-10-31 22:57:36 +08:00
|
|
|
if (MI.isCopy() && !TRI->regsOverlap(MI.getOperand(0).getReg(),
|
|
|
|
MI.getOperand(1).getReg())) {
|
|
|
|
assert(MI.getOperand(0).getReg().isPhysical() &&
|
|
|
|
MI.getOperand(1).getReg().isPhysical() &&
|
2017-10-04 00:59:13 +08:00
|
|
|
"MachineCopyPropagation should be run after register allocation!");
|
2012-01-07 11:02:36 +08:00
|
|
|
|
2021-10-31 22:57:36 +08:00
|
|
|
MCRegister Def = MI.getOperand(0).getReg().asMCReg();
|
|
|
|
MCRegister Src = MI.getOperand(1).getReg().asMCReg();
|
2020-10-13 00:36:01 +08:00
|
|
|
|
2016-02-26 11:18:55 +08:00
|
|
|
// The two copies cancel out and the source of the first copy
|
|
|
|
// hasn't been overridden, eliminate the second one. e.g.
|
2017-12-07 18:40:31 +08:00
|
|
|
// %ecx = COPY %eax
|
2017-11-29 01:15:09 +08:00
|
|
|
// ... nothing clobbered eax.
|
2017-12-07 18:40:31 +08:00
|
|
|
// %eax = COPY %ecx
|
2016-02-26 11:18:55 +08:00
|
|
|
// =>
|
2017-12-07 18:40:31 +08:00
|
|
|
// %ecx = COPY %eax
|
2016-02-26 11:18:55 +08:00
|
|
|
//
|
|
|
|
// or
|
|
|
|
//
|
2017-12-07 18:40:31 +08:00
|
|
|
// %ecx = COPY %eax
|
2017-11-29 01:15:09 +08:00
|
|
|
// ... nothing clobbered eax.
|
2017-12-07 18:40:31 +08:00
|
|
|
// %ecx = COPY %eax
|
2016-02-26 11:18:55 +08:00
|
|
|
// =>
|
2017-12-07 18:40:31 +08:00
|
|
|
// %ecx = COPY %eax
|
2021-10-31 22:57:36 +08:00
|
|
|
if (eraseIfRedundant(MI, Def, Src) || eraseIfRedundant(MI, Src, Def))
|
2017-10-04 00:59:13 +08:00
|
|
|
continue;
|
2012-01-07 11:02:36 +08:00
|
|
|
|
2021-10-31 22:57:36 +08:00
|
|
|
forwardUses(MI);
|
2018-02-28 00:59:10 +08:00
|
|
|
|
|
|
|
// Src may have been changed by forwardUses()
|
2021-10-31 22:57:36 +08:00
|
|
|
Src = MI.getOperand(1).getReg().asMCReg();
|
2018-02-28 00:59:10 +08:00
|
|
|
|
2016-02-03 23:56:27 +08:00
|
|
|
// If Src is defined by a previous copy, the previous copy cannot be
|
|
|
|
// eliminated.
|
2021-10-31 22:57:36 +08:00
|
|
|
ReadRegister(Src, MI, RegularUse);
|
|
|
|
for (const MachineOperand &MO : MI.implicit_operands()) {
|
2017-02-04 10:27:20 +08:00
|
|
|
if (!MO.isReg() || !MO.readsReg())
|
|
|
|
continue;
|
2020-10-13 00:36:01 +08:00
|
|
|
MCRegister Reg = MO.getReg().asMCReg();
|
2017-02-04 10:27:20 +08:00
|
|
|
if (!Reg)
|
|
|
|
continue;
|
2021-10-31 22:57:36 +08:00
|
|
|
ReadRegister(Reg, MI, RegularUse);
|
2012-01-07 11:02:36 +08:00
|
|
|
}
|
|
|
|
|
2021-10-31 22:57:36 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "MCP: Copy is a deletion candidate: "; MI.dump());
|
MachineCopyPropagation has special logic for removing COPY instructions. It will remove plain COPYs using eraseFromParent(), but if the COPY has imp-defs/imp-uses it will convert it to a KILL, to keep the imp-def around.
This actually totally breaks and causes the machine verifier to cry in several cases, one of which being:
%RAX<def> = COPY %RCX<kill>
%ECX<def> = COPY %EAX<kill>, %RAX<imp-use,kill>
These subregister copies are together identified as noops, so are both removed. However, the second one as it has an imp-use gets converted into a kill:
%ECX<def> = KILL %EAX<kill>, %RAX<imp-use,kill>
As the original COPY has been removed, the verifier goes into tears at the use of undefined EAX and RAX.
There are several hacky solutions to this hacky problem (which is all to do with imp-use/def weirdnesses), but the least hacky I've come up with is to *always* remove COPYs by converting to KILLs. KILLs are no-ops to the code generator so the generated code doesn't change (which is why they were partially used in the first place), but using them also keeps the def/use and imp-def/imp-use chains alive:
%RAX<def> = KILL %RCX<kill>
%ECX<def> = KILL %EAX<kill>, %RAX<imp-use,kill>
The patch passes all test cases including the ones that check the removal of MOVs in this circumstance, along with an extra test I added to check subregister behaviour (which made the machine verifier fall over before my patch).
The patch also adds some DEBUG() statements because the file hadn't got any.
llvm-svn: 199797
2014-01-22 17:12:27 +08:00
|
|
|
|
2012-01-07 11:02:36 +08:00
|
|
|
// Copy is now a candidate for deletion.
|
2017-10-04 00:59:13 +08:00
|
|
|
if (!MRI->isReserved(Def))
|
2021-10-31 22:57:36 +08:00
|
|
|
MaybeDeadCopies.insert(&MI);
|
2012-01-07 11:02:36 +08:00
|
|
|
|
2016-02-03 23:56:27 +08:00
|
|
|
// If 'Def' is previously source of another copy, then this earlier copy's
|
2012-01-07 11:02:36 +08:00
|
|
|
// source is no longer available. e.g.
|
2017-12-07 18:40:31 +08:00
|
|
|
// %xmm9 = copy %xmm2
|
2012-01-07 11:02:36 +08:00
|
|
|
// ...
|
2017-12-07 18:40:31 +08:00
|
|
|
// %xmm2 = copy %xmm0
|
2012-01-07 11:02:36 +08:00
|
|
|
// ...
|
2017-12-07 18:40:31 +08:00
|
|
|
// %xmm2 = copy %xmm9
|
2018-09-21 08:51:04 +08:00
|
|
|
Tracker.clobberRegister(Def, *TRI);
|
2021-10-31 22:57:36 +08:00
|
|
|
for (const MachineOperand &MO : MI.implicit_operands()) {
|
2017-02-04 10:27:20 +08:00
|
|
|
if (!MO.isReg() || !MO.isDef())
|
|
|
|
continue;
|
2020-10-13 00:36:01 +08:00
|
|
|
MCRegister Reg = MO.getReg().asMCReg();
|
2017-02-04 10:27:20 +08:00
|
|
|
if (!Reg)
|
|
|
|
continue;
|
2018-09-21 08:51:04 +08:00
|
|
|
Tracker.clobberRegister(Reg, *TRI);
|
2012-01-07 11:02:36 +08:00
|
|
|
}
|
|
|
|
|
2021-10-31 22:57:36 +08:00
|
|
|
Tracker.trackCopy(&MI, *TRI);
|
2017-10-17 00:57:37 +08:00
|
|
|
|
2012-01-07 11:02:36 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-02-28 00:59:10 +08:00
|
|
|
// Clobber any earlyclobber regs first.
|
2021-10-31 22:57:36 +08:00
|
|
|
for (const MachineOperand &MO : MI.operands())
|
2018-02-28 00:59:10 +08:00
|
|
|
if (MO.isReg() && MO.isEarlyClobber()) {
|
2020-10-13 00:36:01 +08:00
|
|
|
MCRegister Reg = MO.getReg().asMCReg();
|
2018-02-28 00:59:10 +08:00
|
|
|
// If we have a tied earlyclobber, that means it is also read by this
|
|
|
|
// instruction, so we need to make sure we don't remove it as dead
|
|
|
|
// later.
|
|
|
|
if (MO.isTied())
|
2021-10-31 22:57:36 +08:00
|
|
|
ReadRegister(Reg, MI, RegularUse);
|
2018-09-21 08:51:04 +08:00
|
|
|
Tracker.clobberRegister(Reg, *TRI);
|
2018-02-28 00:59:10 +08:00
|
|
|
}
|
|
|
|
|
2021-10-31 22:57:36 +08:00
|
|
|
forwardUses(MI);
|
2018-02-28 00:59:10 +08:00
|
|
|
|
2012-01-07 11:02:36 +08:00
|
|
|
// Not a copy.
|
2020-10-13 00:36:01 +08:00
|
|
|
SmallVector<Register, 2> Defs;
|
2016-02-20 11:56:36 +08:00
|
|
|
const MachineOperand *RegMask = nullptr;
|
2021-10-31 22:57:36 +08:00
|
|
|
for (const MachineOperand &MO : MI.operands()) {
|
2012-02-09 06:37:35 +08:00
|
|
|
if (MO.isRegMask())
|
2016-02-20 11:56:36 +08:00
|
|
|
RegMask = &MO;
|
2012-01-07 11:02:36 +08:00
|
|
|
if (!MO.isReg())
|
|
|
|
continue;
|
Apply llvm-prefer-register-over-unsigned from clang-tidy to LLVM
Summary:
This clang-tidy check is looking for unsigned integer variables whose initializer
starts with an implicit cast from llvm::Register and changes the type of the
variable to llvm::Register (dropping the llvm:: where possible).
Partial reverts in:
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
X86FixupLEAs.cpp - Some functions return unsigned and arguably should be MCRegister
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
HexagonBitSimplify.cpp - Function takes BitTracker::RegisterRef which appears to be unsigned&
MachineVerifier.cpp - Ambiguous operator==() given MCRegister and const Register
PPCFastISel.cpp - No Register::operator-=()
PeepholeOptimizer.cpp - TargetInstrInfo::optimizeLoadInstr() takes an unsigned&
MachineTraceMetrics.cpp - MachineTraceMetrics lacks a suitable constructor
Manual fixups in:
ARMFastISel.cpp - ARMEmitLoad() now takes a Register& instead of unsigned&
HexagonSplitDouble.cpp - Ternary operator was ambiguous between unsigned/Register
HexagonConstExtenders.cpp - Has a local class named Register, used llvm::Register instead of Register.
PPCFastISel.cpp - PPCEmitLoad() now takes a Register& instead of unsigned&
Depends on D65919
Reviewers: arsenm, bogner, craig.topper, RKSimon
Reviewed By: arsenm
Subscribers: RKSimon, craig.topper, lenary, aemerson, wuzish, jholewinski, MatzeB, qcolombet, dschuff, jyknight, dylanmckay, sdardis, nemanjai, jvesely, wdng, nhaehnle, sbc100, jgravelle-google, kristof.beyls, hiraditya, aheejin, kbarton, fedor.sergeev, javed.absar, asb, rbar, johnrusso, simoncook, apazos, sabuasal, niosHD, jrtc27, MaskRay, zzheng, edward-jones, atanasyan, rogfer01, MartinMosbeck, brucehoult, the_o, tpr, PkmX, jocewei, jsji, Petar.Avramovic, asbirlea, Jim, s.egerton, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D65962
llvm-svn: 369041
2019-08-16 03:22:08 +08:00
|
|
|
Register Reg = MO.getReg();
|
2012-01-07 11:02:36 +08:00
|
|
|
if (!Reg)
|
|
|
|
continue;
|
|
|
|
|
2020-10-13 00:36:01 +08:00
|
|
|
assert(!Reg.isVirtual() &&
|
2017-10-04 00:59:13 +08:00
|
|
|
"MachineCopyPropagation should be run after register allocation!");
|
|
|
|
|
2018-02-28 00:59:10 +08:00
|
|
|
if (MO.isDef() && !MO.isEarlyClobber()) {
|
2020-10-13 00:36:01 +08:00
|
|
|
Defs.push_back(Reg.asMCReg());
|
2012-01-07 11:02:36 +08:00
|
|
|
continue;
|
2019-08-14 20:20:02 +08:00
|
|
|
} else if (MO.readsReg())
|
2021-10-31 22:57:36 +08:00
|
|
|
ReadRegister(Reg.asMCReg(), MI, MO.isDebug() ? DebugUse : RegularUse);
|
2012-01-07 11:02:36 +08:00
|
|
|
}
|
|
|
|
|
2012-02-09 06:37:35 +08:00
|
|
|
// The instruction has a register mask operand which means that it clobbers
|
2016-02-26 11:18:50 +08:00
|
|
|
// a large set of registers. Treat clobbered registers the same way as
|
|
|
|
// defined registers.
|
2016-02-20 11:56:36 +08:00
|
|
|
if (RegMask) {
|
2012-02-09 08:19:08 +08:00
|
|
|
// Erase any MaybeDeadCopies whose destination register is clobbered.
|
2016-03-26 05:15:35 +08:00
|
|
|
for (SmallSetVector<MachineInstr *, 8>::iterator DI =
|
|
|
|
MaybeDeadCopies.begin();
|
|
|
|
DI != MaybeDeadCopies.end();) {
|
|
|
|
MachineInstr *MaybeDead = *DI;
|
2020-10-13 00:36:01 +08:00
|
|
|
MCRegister Reg = MaybeDead->getOperand(0).getReg().asMCReg();
|
2016-02-20 11:56:36 +08:00
|
|
|
assert(!MRI->isReserved(Reg));
|
2016-03-26 05:15:35 +08:00
|
|
|
|
|
|
|
if (!RegMask->clobbersPhysReg(Reg)) {
|
|
|
|
++DI;
|
2012-02-09 08:19:08 +08:00
|
|
|
continue;
|
2016-03-26 05:15:35 +08:00
|
|
|
}
|
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "MCP: Removing copy due to regmask clobbering: ";
|
|
|
|
MaybeDead->dump());
|
2016-03-26 05:15:35 +08:00
|
|
|
|
2018-09-25 12:45:25 +08:00
|
|
|
// Make sure we invalidate any entries in the copy maps before erasing
|
|
|
|
// the instruction.
|
|
|
|
Tracker.clobberRegister(Reg, *TRI);
|
|
|
|
|
2016-03-26 05:15:35 +08:00
|
|
|
// erase() will return the next valid iterator pointing to the next
|
|
|
|
// element after the erased one.
|
|
|
|
DI = MaybeDeadCopies.erase(DI);
|
2016-02-20 11:56:36 +08:00
|
|
|
MaybeDead->eraseFromParent();
|
2012-02-09 08:19:08 +08:00
|
|
|
Changed = true;
|
|
|
|
++NumDeletes;
|
|
|
|
}
|
2016-02-26 11:18:50 +08:00
|
|
|
}
|
2012-01-07 11:02:36 +08:00
|
|
|
|
2016-02-26 11:18:50 +08:00
|
|
|
// Any previous copy definition or reading the Defs is no longer available.
|
2020-10-13 00:36:01 +08:00
|
|
|
for (MCRegister Reg : Defs)
|
2018-09-21 08:51:04 +08:00
|
|
|
Tracker.clobberRegister(Reg, *TRI);
|
2012-01-07 11:02:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// If MBB doesn't have successors, delete the copies whose defs are not used.
|
|
|
|
// If MBB does have successors, then conservative assume the defs are live-out
|
|
|
|
// since we don't want to trust live-in lists.
|
|
|
|
if (MBB.succ_empty()) {
|
2016-02-20 11:56:36 +08:00
|
|
|
for (MachineInstr *MaybeDead : MaybeDeadCopies) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "MCP: Removing copy due to no live-out succ: ";
|
|
|
|
MaybeDead->dump());
|
2016-02-20 11:56:36 +08:00
|
|
|
assert(!MRI->isReserved(MaybeDead->getOperand(0).getReg()));
|
2018-10-01 16:14:44 +08:00
|
|
|
|
2019-08-14 20:20:02 +08:00
|
|
|
// Update matching debug values, if any.
|
2018-10-01 16:14:44 +08:00
|
|
|
assert(MaybeDead->isCopy());
|
2020-10-13 00:36:01 +08:00
|
|
|
Register SrcReg = MaybeDead->getOperand(1).getReg();
|
2021-05-10 21:00:01 +08:00
|
|
|
Register DestReg = MaybeDead->getOperand(0).getReg();
|
|
|
|
SmallVector<MachineInstr *> MaybeDeadDbgUsers(
|
|
|
|
CopyDbgUsers[MaybeDead].begin(), CopyDbgUsers[MaybeDead].end());
|
|
|
|
MRI->updateDbgUsersToReg(DestReg.asMCReg(), SrcReg.asMCReg(),
|
|
|
|
MaybeDeadDbgUsers);
|
2018-10-01 16:14:44 +08:00
|
|
|
|
2016-02-20 11:56:36 +08:00
|
|
|
MaybeDead->eraseFromParent();
|
|
|
|
Changed = true;
|
|
|
|
++NumDeletes;
|
2012-01-07 11:02:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-20 11:56:39 +08:00
|
|
|
MaybeDeadCopies.clear();
|
2019-08-14 20:20:02 +08:00
|
|
|
CopyDbgUsers.clear();
|
2018-09-21 08:51:04 +08:00
|
|
|
Tracker.clear();
|
2012-01-07 11:02:36 +08:00
|
|
|
}
|
|
|
|
|
2019-12-05 14:01:00 +08:00
|
|
|
static bool isBackwardPropagatableCopy(MachineInstr &MI,
|
|
|
|
const MachineRegisterInfo &MRI) {
|
|
|
|
assert(MI.isCopy() && "MI is expected to be a COPY");
|
|
|
|
Register Def = MI.getOperand(0).getReg();
|
|
|
|
Register Src = MI.getOperand(1).getReg();
|
|
|
|
|
|
|
|
if (!Def || !Src)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (MRI.isReserved(Def) || MRI.isReserved(Src))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return MI.getOperand(1).isRenamable() && MI.getOperand(1).isKill();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MachineCopyPropagation::propagateDefs(MachineInstr &MI) {
|
|
|
|
if (!Tracker.hasAnyCopies())
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (unsigned OpIdx = 0, OpEnd = MI.getNumOperands(); OpIdx != OpEnd;
|
|
|
|
++OpIdx) {
|
|
|
|
MachineOperand &MODef = MI.getOperand(OpIdx);
|
|
|
|
|
|
|
|
if (!MODef.isReg() || MODef.isUse())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Ignore non-trivial cases.
|
|
|
|
if (MODef.isTied() || MODef.isUndef() || MODef.isImplicit())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!MODef.getReg())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// We only handle if the register comes from a vreg.
|
|
|
|
if (!MODef.isRenamable())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
MachineInstr *Copy =
|
2020-10-13 00:36:01 +08:00
|
|
|
Tracker.findAvailBackwardCopy(MI, MODef.getReg().asMCReg(), *TRI);
|
2019-12-05 14:01:00 +08:00
|
|
|
if (!Copy)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Register Def = Copy->getOperand(0).getReg();
|
|
|
|
Register Src = Copy->getOperand(1).getReg();
|
|
|
|
|
|
|
|
if (MODef.getReg() != Src)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!isBackwardPropagatableRegClassCopy(*Copy, MI, OpIdx))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (hasImplicitOverlap(MI, MODef))
|
|
|
|
continue;
|
|
|
|
|
2020-07-29 23:17:47 +08:00
|
|
|
if (hasOverlappingMultipleDef(MI, MODef, Def))
|
|
|
|
continue;
|
|
|
|
|
2019-12-05 14:01:00 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "MCP: Replacing " << printReg(MODef.getReg(), TRI)
|
|
|
|
<< "\n with " << printReg(Def, TRI) << "\n in "
|
|
|
|
<< MI << " from " << *Copy);
|
|
|
|
|
|
|
|
MODef.setReg(Def);
|
|
|
|
MODef.setIsRenamable(Copy->getOperand(0).isRenamable());
|
|
|
|
|
|
|
|
LLVM_DEBUG(dbgs() << "MCP: After replacement: " << MI << "\n");
|
|
|
|
MaybeDeadCopies.insert(Copy);
|
|
|
|
Changed = true;
|
2019-12-30 16:31:41 +08:00
|
|
|
++NumCopyBackwardPropagated;
|
2019-12-05 14:01:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MachineCopyPropagation::BackwardCopyPropagateBlock(
|
|
|
|
MachineBasicBlock &MBB) {
|
|
|
|
LLVM_DEBUG(dbgs() << "MCP: BackwardCopyPropagateBlock " << MBB.getName()
|
|
|
|
<< "\n");
|
|
|
|
|
2021-12-05 00:48:04 +08:00
|
|
|
for (MachineInstr &MI : llvm::make_early_inc_range(llvm::reverse(MBB))) {
|
2019-12-05 14:01:00 +08:00
|
|
|
// Ignore non-trivial COPYs.
|
2021-12-05 00:48:04 +08:00
|
|
|
if (MI.isCopy() && MI.getNumOperands() == 2 &&
|
|
|
|
!TRI->regsOverlap(MI.getOperand(0).getReg(),
|
|
|
|
MI.getOperand(1).getReg())) {
|
2019-12-05 14:01:00 +08:00
|
|
|
|
2021-12-05 00:48:04 +08:00
|
|
|
MCRegister Def = MI.getOperand(0).getReg().asMCReg();
|
|
|
|
MCRegister Src = MI.getOperand(1).getReg().asMCReg();
|
2019-12-05 14:01:00 +08:00
|
|
|
|
|
|
|
// Unlike forward cp, we don't invoke propagateDefs here,
|
|
|
|
// just let forward cp do COPY-to-COPY propagation.
|
2021-12-05 00:48:04 +08:00
|
|
|
if (isBackwardPropagatableCopy(MI, *MRI)) {
|
2019-12-05 14:01:00 +08:00
|
|
|
Tracker.invalidateRegister(Src, *TRI);
|
|
|
|
Tracker.invalidateRegister(Def, *TRI);
|
2021-12-05 00:48:04 +08:00
|
|
|
Tracker.trackCopy(&MI, *TRI);
|
2019-12-05 14:01:00 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Invalidate any earlyclobber regs first.
|
2021-12-05 00:48:04 +08:00
|
|
|
for (const MachineOperand &MO : MI.operands())
|
2019-12-05 14:01:00 +08:00
|
|
|
if (MO.isReg() && MO.isEarlyClobber()) {
|
2020-10-13 00:36:01 +08:00
|
|
|
MCRegister Reg = MO.getReg().asMCReg();
|
2019-12-05 14:01:00 +08:00
|
|
|
if (!Reg)
|
|
|
|
continue;
|
|
|
|
Tracker.invalidateRegister(Reg, *TRI);
|
|
|
|
}
|
|
|
|
|
2021-12-05 00:48:04 +08:00
|
|
|
propagateDefs(MI);
|
|
|
|
for (const MachineOperand &MO : MI.operands()) {
|
2019-12-05 14:01:00 +08:00
|
|
|
if (!MO.isReg())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!MO.getReg())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (MO.isDef())
|
2020-10-13 00:36:01 +08:00
|
|
|
Tracker.invalidateRegister(MO.getReg().asMCReg(), *TRI);
|
2019-12-05 14:01:00 +08:00
|
|
|
|
2021-07-02 18:50:07 +08:00
|
|
|
if (MO.readsReg()) {
|
|
|
|
if (MO.isDebug()) {
|
|
|
|
// Check if the register in the debug instruction is utilized
|
|
|
|
// in a copy instruction, so we can update the debug info if the
|
|
|
|
// register is changed.
|
|
|
|
for (MCRegUnitIterator RUI(MO.getReg().asMCReg(), TRI); RUI.isValid();
|
|
|
|
++RUI) {
|
|
|
|
if (auto *Copy = Tracker.findCopyDefViaUnit(*RUI, *TRI)) {
|
2021-12-05 00:48:04 +08:00
|
|
|
CopyDbgUsers[Copy].insert(&MI);
|
2021-07-02 18:50:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Tracker.invalidateRegister(MO.getReg().asMCReg(), *TRI);
|
|
|
|
}
|
|
|
|
}
|
2019-12-05 14:01:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-30 16:31:41 +08:00
|
|
|
for (auto *Copy : MaybeDeadCopies) {
|
2021-07-02 18:50:07 +08:00
|
|
|
|
|
|
|
Register Src = Copy->getOperand(1).getReg();
|
|
|
|
Register Def = Copy->getOperand(0).getReg();
|
|
|
|
SmallVector<MachineInstr *> MaybeDeadDbgUsers(CopyDbgUsers[Copy].begin(),
|
|
|
|
CopyDbgUsers[Copy].end());
|
|
|
|
|
|
|
|
MRI->updateDbgUsersToReg(Src.asMCReg(), Def.asMCReg(), MaybeDeadDbgUsers);
|
2019-12-05 14:01:00 +08:00
|
|
|
Copy->eraseFromParent();
|
2019-12-30 16:31:41 +08:00
|
|
|
++NumDeletes;
|
|
|
|
}
|
2019-12-05 14:01:00 +08:00
|
|
|
|
|
|
|
MaybeDeadCopies.clear();
|
|
|
|
CopyDbgUsers.clear();
|
|
|
|
Tracker.clear();
|
|
|
|
}
|
|
|
|
|
2012-01-07 11:02:36 +08:00
|
|
|
bool MachineCopyPropagation::runOnMachineFunction(MachineFunction &MF) {
|
2017-12-16 06:22:58 +08:00
|
|
|
if (skipFunction(MF.getFunction()))
|
2014-04-01 01:43:35 +08:00
|
|
|
return false;
|
|
|
|
|
2016-02-20 11:56:39 +08:00
|
|
|
Changed = false;
|
2012-01-07 11:02:36 +08:00
|
|
|
|
2014-08-05 10:39:49 +08:00
|
|
|
TRI = MF.getSubtarget().getRegisterInfo();
|
|
|
|
TII = MF.getSubtarget().getInstrInfo();
|
2012-10-16 05:57:41 +08:00
|
|
|
MRI = &MF.getRegInfo();
|
2012-01-07 11:02:36 +08:00
|
|
|
|
2019-12-05 14:01:00 +08:00
|
|
|
for (MachineBasicBlock &MBB : MF) {
|
|
|
|
BackwardCopyPropagateBlock(MBB);
|
|
|
|
ForwardCopyPropagateBlock(MBB);
|
|
|
|
}
|
2012-01-07 11:02:36 +08:00
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|