2008-09-17 02:43:24 +02:00
|
|
|
//===- DeadMachineInstructionElim.cpp - Remove dead machine instructions --===//
|
|
|
|
//
|
2019-01-19 09:50:56 +01: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
|
2008-09-17 02:43:24 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This is an extremely simple MachineInstr-level dead-code-elimination pass.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-12-03 17:50:05 +01:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2008-09-17 02:43:24 +02:00
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2017-06-06 13:49:48 +02:00
|
|
|
#include "llvm/CodeGen/Passes.h"
|
2017-11-17 02:07:10 +01:00
|
|
|
#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-13 22:15:01 +01:00
|
|
|
#include "llvm/InitializePasses.h"
|
2012-12-03 17:50:05 +01:00
|
|
|
#include "llvm/Pass.h"
|
2008-09-25 03:06:50 +02:00
|
|
|
#include "llvm/Support/Debug.h"
|
2009-08-22 22:04:03 +02:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2014-08-04 23:25:23 +02:00
|
|
|
|
2008-09-17 02:43:24 +02:00
|
|
|
using namespace llvm;
|
|
|
|
|
2017-05-25 23:26:32 +02:00
|
|
|
#define DEBUG_TYPE "dead-mi-elimination"
|
2014-04-22 04:02:50 +02:00
|
|
|
|
2010-02-06 10:07:11 +01:00
|
|
|
STATISTIC(NumDeletes, "Number of dead instructions deleted");
|
|
|
|
|
2008-09-17 02:43:24 +02:00
|
|
|
namespace {
|
2009-10-25 07:33:48 +01:00
|
|
|
class DeadMachineInstructionElim : public MachineFunctionPass {
|
2014-03-07 10:26:03 +01:00
|
|
|
bool runOnMachineFunction(MachineFunction &MF) override;
|
2012-02-08 22:22:43 +01:00
|
|
|
|
2008-09-24 02:27:38 +02:00
|
|
|
const TargetRegisterInfo *TRI;
|
|
|
|
const MachineRegisterInfo *MRI;
|
|
|
|
const TargetInstrInfo *TII;
|
|
|
|
BitVector LivePhysRegs;
|
|
|
|
|
2008-09-17 02:43:24 +02:00
|
|
|
public:
|
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2010-10-19 19:21:58 +02:00
|
|
|
DeadMachineInstructionElim() : MachineFunctionPass(ID) {
|
|
|
|
initializeDeadMachineInstructionElimPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2008-09-24 02:27:38 +02:00
|
|
|
|
2016-06-22 01:01:17 +02:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2019-04-09 18:13:53 +02:00
|
|
|
AU.setPreservesCFG();
|
2016-06-22 01:01:17 +02:00
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
|
|
|
|
2008-09-24 02:27:38 +02:00
|
|
|
private:
|
2009-08-11 17:13:43 +02:00
|
|
|
bool isDead(const MachineInstr *MI) const;
|
2008-09-17 02:43:24 +02:00
|
|
|
};
|
2015-06-23 11:49:53 +02:00
|
|
|
}
|
2008-09-17 02:43:24 +02:00
|
|
|
char DeadMachineInstructionElim::ID = 0;
|
2012-02-08 22:23:13 +01:00
|
|
|
char &llvm::DeadMachineInstructionElimID = DeadMachineInstructionElim::ID;
|
2008-09-17 02:43:24 +02:00
|
|
|
|
2017-05-25 23:26:32 +02:00
|
|
|
INITIALIZE_PASS(DeadMachineInstructionElim, DEBUG_TYPE,
|
2010-10-08 00:25:06 +02:00
|
|
|
"Remove dead machine instructions", false, false)
|
2008-09-17 02:43:24 +02:00
|
|
|
|
2009-08-11 17:13:43 +02:00
|
|
|
bool DeadMachineInstructionElim::isDead(const MachineInstr *MI) const {
|
2011-01-08 00:50:32 +01:00
|
|
|
// Technically speaking inline asm without side effects and no defs can still
|
|
|
|
// be deleted. But there is so much bad inline asm code out there, we should
|
|
|
|
// let them be.
|
|
|
|
if (MI->isInlineAsm())
|
|
|
|
return false;
|
|
|
|
|
2015-01-13 01:48:10 +01:00
|
|
|
// Don't delete frame allocation labels.
|
Rename llvm.frameescape and llvm.framerecover to localescape and localrecover
Summary:
Initially, these intrinsics seemed like part of a family of "frame"
related intrinsics, but now I think that's more confusing than helpful.
Initially, the LangRef specified that this would create a new kind of
allocation that would be allocated at a fixed offset from the frame
pointer (EBP/RBP). We ended up dropping that design, and leaving the
stack frame layout alone.
These intrinsics are really about sharing local stack allocations, not
frame pointers. I intend to go further and add an `llvm.localaddress()`
intrinsic that returns whatever register (EBP, ESI, ESP, RBX) is being
used to address locals, which should not be confused with the frame
pointer.
Naming suggestions at this point are welcome, I'm happy to re-run sed.
Reviewers: majnemer, nicholas
Subscribers: llvm-commits
Differential Revision: http://reviews.llvm.org/D11011
llvm-svn: 241633
2015-07-08 00:25:32 +02:00
|
|
|
if (MI->getOpcode() == TargetOpcode::LOCAL_ESCAPE)
|
2015-01-13 01:48:10 +01:00
|
|
|
return false;
|
|
|
|
|
2008-09-24 02:27:38 +02:00
|
|
|
// Don't delete instructions with side effects.
|
|
|
|
bool SawStore = false;
|
2015-05-19 23:22:20 +02:00
|
|
|
if (!MI->isSafeToMove(nullptr, SawStore) && !MI->isPHI())
|
2008-09-24 02:27:38 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Examine each operand.
|
|
|
|
for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
|
|
|
|
const MachineOperand &MO = MI->getOperand(i);
|
2008-10-03 17:45:36 +02:00
|
|
|
if (MO.isReg() && MO.isDef()) {
|
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-15 21:22:08 +02:00
|
|
|
Register Reg = MO.getReg();
|
2019-08-02 01:27:28 +02:00
|
|
|
if (Register::isPhysicalRegister(Reg)) {
|
2012-02-09 01:15:39 +01:00
|
|
|
// Don't delete live physreg defs, or any reserved register defs.
|
2019-04-09 18:13:53 +02:00
|
|
|
if (LivePhysRegs.test(Reg) || MRI->isReserved(Reg))
|
2012-02-09 01:15:39 +01:00
|
|
|
return false;
|
|
|
|
} else {
|
[codegen,amdgpu] Enhance MIR DIE and re-arrange it for AMDGPU.
Summary:
- `dead-mi-elimination` assumes MIR in the SSA form and cannot be
arranged after phi elimination or DeSSA. It's enhanced to handle the
dead register definition by skipping use check on it. Once a register
def is `dead`, all its uses, if any, should be `undef`.
- Re-arrange the DIE in RA phase for AMDGPU by placing it directly after
`detect-dead-lanes`.
- Many relevant tests are refined due to different register assignment.
Reviewers: rampitec, qcolombet, sunfish
Subscribers: arsenm, kzhuravl, jvesely, wdng, nhaehnle, yaxunl, dstuttard, tpr, t-tye, hiraditya, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D72709
2020-01-08 16:50:23 +01:00
|
|
|
if (MO.isDead()) {
|
|
|
|
#ifndef NDEBUG
|
|
|
|
// Sanity check on uses of this dead register. All of them should be
|
|
|
|
// 'undef'.
|
|
|
|
for (auto &U : MRI->use_nodbg_operands(Reg))
|
|
|
|
assert(U.isUndef() && "'Undef' use on a 'dead' register is found!");
|
|
|
|
#endif
|
|
|
|
continue;
|
|
|
|
}
|
2019-03-20 22:42:05 +01:00
|
|
|
for (const MachineInstr &Use : MRI->use_nodbg_instructions(Reg)) {
|
|
|
|
if (&Use != MI)
|
|
|
|
// This def has a non-debug use. Don't delete the instruction!
|
|
|
|
return false;
|
|
|
|
}
|
2008-09-24 02:27:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there are no defs with uses, the instruction is dead.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-09-17 02:43:24 +02:00
|
|
|
bool DeadMachineInstructionElim::runOnMachineFunction(MachineFunction &MF) {
|
2017-12-15 23:22:58 +01:00
|
|
|
if (skipFunction(MF.getFunction()))
|
2014-03-31 19:43:35 +02:00
|
|
|
return false;
|
|
|
|
|
2008-09-17 02:43:24 +02:00
|
|
|
bool AnyChanges = false;
|
2008-09-24 02:27:38 +02:00
|
|
|
MRI = &MF.getRegInfo();
|
2014-08-05 04:39:49 +02:00
|
|
|
TRI = MF.getSubtarget().getRegisterInfo();
|
|
|
|
TII = MF.getSubtarget().getInstrInfo();
|
2008-09-17 02:43:24 +02:00
|
|
|
|
|
|
|
// Loop over all instructions in all blocks, from bottom to top, so that it's
|
|
|
|
// more likely that chains of dependent but ultimately dead instructions will
|
|
|
|
// be cleaned up.
|
2015-07-24 23:13:43 +02:00
|
|
|
for (MachineBasicBlock &MBB : make_range(MF.rbegin(), MF.rend())) {
|
2010-08-31 23:51:05 +02:00
|
|
|
// Start out assuming that reserved registers are live out of this block.
|
2012-10-15 23:57:41 +02:00
|
|
|
LivePhysRegs = MRI->getReservedRegs();
|
2008-09-23 23:40:44 +02:00
|
|
|
|
2017-02-15 23:19:06 +01:00
|
|
|
// Add live-ins from successors to LivePhysRegs. Normally, physregs are not
|
2011-06-27 17:00:36 +02:00
|
|
|
// live across blocks, but some targets (x86) can have flags live out of a
|
|
|
|
// block.
|
2015-07-24 23:13:43 +02:00
|
|
|
for (MachineBasicBlock::succ_iterator S = MBB.succ_begin(),
|
|
|
|
E = MBB.succ_end(); S != E; S++)
|
2015-09-09 20:08:03 +02:00
|
|
|
for (const auto &LI : (*S)->liveins())
|
|
|
|
LivePhysRegs.set(LI.PhysReg);
|
2010-08-31 23:51:05 +02:00
|
|
|
|
2008-09-23 23:40:44 +02:00
|
|
|
// Now scan the instructions and delete dead ones, tracking physreg
|
|
|
|
// liveness as we go.
|
2015-07-24 23:13:43 +02:00
|
|
|
for (MachineBasicBlock::reverse_iterator MII = MBB.rbegin(),
|
|
|
|
MIE = MBB.rend(); MII != MIE; ) {
|
2016-09-11 20:51:28 +02:00
|
|
|
MachineInstr *MI = &*MII++;
|
2008-09-17 02:43:24 +02:00
|
|
|
|
2008-09-24 02:27:38 +02:00
|
|
|
// If the instruction is dead, delete it!
|
|
|
|
if (isDead(MI)) {
|
2018-05-14 14:53:11 +02:00
|
|
|
LLVM_DEBUG(dbgs() << "DeadMachineInstructionElim: DELETING: " << *MI);
|
2010-02-12 19:40:17 +01:00
|
|
|
// It is possible that some DBG_VALUE instructions refer to this
|
2014-08-13 23:15:23 +02:00
|
|
|
// instruction. They get marked as undef and will be deleted
|
|
|
|
// in the live debug variable analysis.
|
|
|
|
MI->eraseFromParentAndMarkDBGValuesForRemoval();
|
2008-09-24 02:27:38 +02:00
|
|
|
AnyChanges = true;
|
2010-02-06 10:07:11 +01:00
|
|
|
++NumDeletes;
|
2008-09-24 02:27:38 +02:00
|
|
|
continue;
|
2008-09-17 02:43:24 +02:00
|
|
|
}
|
2008-09-23 23:40:44 +02:00
|
|
|
|
|
|
|
// Record the physreg defs.
|
|
|
|
for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
|
|
|
|
const MachineOperand &MO = MI->getOperand(i);
|
2008-10-03 17:45:36 +02:00
|
|
|
if (MO.isReg() && MO.isDef()) {
|
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-15 21:22:08 +02:00
|
|
|
Register Reg = MO.getReg();
|
2019-08-02 01:27:28 +02:00
|
|
|
if (Register::isPhysicalRegister(Reg)) {
|
2008-10-16 02:11:23 +02:00
|
|
|
// Check the subreg set, not the alias set, because a def
|
|
|
|
// of a super-register may still be partially live after
|
|
|
|
// this def.
|
2013-05-23 01:17:36 +02:00
|
|
|
for (MCSubRegIterator SR(Reg, TRI,/*IncludeSelf=*/true);
|
|
|
|
SR.isValid(); ++SR)
|
2012-06-02 01:28:30 +02:00
|
|
|
LivePhysRegs.reset(*SR);
|
2008-09-23 23:40:44 +02:00
|
|
|
}
|
2012-01-20 23:27:09 +01:00
|
|
|
} else if (MO.isRegMask()) {
|
|
|
|
// Register mask of preserved registers. All clobbers are dead.
|
2012-02-03 00:52:57 +01:00
|
|
|
LivePhysRegs.clearBitsNotInMask(MO.getRegMask());
|
2008-09-23 23:40:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Record the physreg uses, after the defs, in case a physreg is
|
|
|
|
// both defined and used in the same instruction.
|
|
|
|
for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
|
|
|
|
const MachineOperand &MO = MI->getOperand(i);
|
2008-10-03 17:45:36 +02:00
|
|
|
if (MO.isReg() && MO.isUse()) {
|
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-15 21:22:08 +02:00
|
|
|
Register Reg = MO.getReg();
|
2019-08-02 01:27:28 +02:00
|
|
|
if (Register::isPhysicalRegister(Reg)) {
|
2012-06-01 22:36:54 +02:00
|
|
|
for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
|
|
|
|
LivePhysRegs.set(*AI);
|
2008-09-23 23:40:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-09-17 02:43:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-24 02:27:38 +02:00
|
|
|
LivePhysRegs.clear();
|
2008-09-17 02:43:24 +02:00
|
|
|
return AnyChanges;
|
|
|
|
}
|