2008-09-17 00:43:24 +00:00
|
|
|
//===- DeadMachineInstructionElim.cpp - Remove dead machine instructions --===//
|
|
|
|
//
|
2019-01-19 08:50:56 +00: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 00:43:24 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This is an extremely simple MachineInstr-level dead-code-elimination pass.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2024-02-06 17:56:56 +08:00
|
|
|
#include "llvm/CodeGen/DeadMachineInstructionElim.h"
|
2020-11-21 00:35:53 +08:00
|
|
|
#include "llvm/ADT/PostOrderIterator.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2022-04-21 09:11:40 -04:00
|
|
|
#include "llvm/CodeGen/LiveRegUnits.h"
|
2008-09-17 00:43:24 +00:00
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2017-11-17 01:07:10 +00: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 13:15:01 -08:00
|
|
|
#include "llvm/InitializePasses.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/Pass.h"
|
2008-09-25 01:06:50 +00:00
|
|
|
#include "llvm/Support/Debug.h"
|
2009-08-22 20:04:03 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2014-08-04 21:25:23 +00:00
|
|
|
|
2008-09-17 00:43:24 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2017-05-25 21:26:32 +00:00
|
|
|
#define DEBUG_TYPE "dead-mi-elimination"
|
2014-04-22 02:02:50 +00:00
|
|
|
|
2010-02-06 09:07:11 +00:00
|
|
|
STATISTIC(NumDeletes, "Number of dead instructions deleted");
|
|
|
|
|
2008-09-17 00:43:24 +00:00
|
|
|
namespace {
|
2024-02-06 17:56:56 +08:00
|
|
|
class DeadMachineInstructionElimImpl {
|
|
|
|
const MachineRegisterInfo *MRI = nullptr;
|
|
|
|
const TargetInstrInfo *TII = nullptr;
|
|
|
|
LiveRegUnits LivePhysRegs;
|
2012-02-08 21:22:43 +00:00
|
|
|
|
2024-02-06 17:56:56 +08:00
|
|
|
public:
|
|
|
|
bool runImpl(MachineFunction &MF);
|
2008-09-24 00:27:38 +00:00
|
|
|
|
2024-02-06 17:56:56 +08:00
|
|
|
private:
|
|
|
|
bool isDead(const MachineInstr *MI) const;
|
|
|
|
bool eliminateDeadMI(MachineFunction &MF);
|
|
|
|
};
|
2008-09-24 00:27:38 +00:00
|
|
|
|
2024-02-06 17:56:56 +08:00
|
|
|
class DeadMachineInstructionElim : public MachineFunctionPass {
|
|
|
|
public:
|
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2016-06-21 23:01:17 +00:00
|
|
|
|
2024-02-06 17:56:56 +08:00
|
|
|
DeadMachineInstructionElim() : MachineFunctionPass(ID) {
|
|
|
|
initializeDeadMachineInstructionElimPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool runOnMachineFunction(MachineFunction &MF) override {
|
|
|
|
if (skipFunction(MF.getFunction()))
|
|
|
|
return false;
|
|
|
|
return DeadMachineInstructionElimImpl().runImpl(MF);
|
|
|
|
}
|
2020-11-21 00:35:53 +08:00
|
|
|
|
2024-02-06 17:56:56 +08:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
|
|
|
AU.setPreservesCFG();
|
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
PreservedAnalyses
|
|
|
|
DeadMachineInstructionElimPass::run(MachineFunction &MF,
|
|
|
|
MachineFunctionAnalysisManager &) {
|
|
|
|
if (!DeadMachineInstructionElimImpl().runImpl(MF))
|
|
|
|
return PreservedAnalyses::all();
|
2024-04-30 09:54:48 +08:00
|
|
|
PreservedAnalyses PA = getMachineFunctionPassPreservedAnalyses();
|
2024-02-06 17:56:56 +08:00
|
|
|
PA.preserveSet<CFGAnalyses>();
|
|
|
|
return PA;
|
2015-06-23 09:49:53 +00:00
|
|
|
}
|
2024-02-06 17:56:56 +08:00
|
|
|
|
2008-09-17 00:43:24 +00:00
|
|
|
char DeadMachineInstructionElim::ID = 0;
|
2012-02-08 21:23:13 +00:00
|
|
|
char &llvm::DeadMachineInstructionElimID = DeadMachineInstructionElim::ID;
|
2008-09-17 00:43:24 +00:00
|
|
|
|
2017-05-25 21:26:32 +00:00
|
|
|
INITIALIZE_PASS(DeadMachineInstructionElim, DEBUG_TYPE,
|
2010-10-07 22:25:06 +00:00
|
|
|
"Remove dead machine instructions", false, false)
|
2008-09-17 00:43:24 +00:00
|
|
|
|
2024-02-06 17:56:56 +08:00
|
|
|
bool DeadMachineInstructionElimImpl::isDead(const MachineInstr *MI) const {
|
2024-09-09 16:30:44 +02:00
|
|
|
// Instructions without side-effects are dead iff they only define dead regs.
|
|
|
|
// This function is hot and this loop returns early in the common case,
|
|
|
|
// so only perform additional checks before this if absolutely necessary.
|
2023-05-24 14:57:23 +01:00
|
|
|
for (const MachineOperand &MO : MI->all_defs()) {
|
|
|
|
Register Reg = MO.getReg();
|
|
|
|
if (Reg.isPhysical()) {
|
|
|
|
// Don't delete live physreg defs, or any reserved register defs.
|
|
|
|
if (!LivePhysRegs.available(Reg) || MRI->isReserved(Reg))
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
if (MO.isDead()) {
|
[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 10:50:23 -05:00
|
|
|
#ifndef NDEBUG
|
2023-05-24 14:57:23 +01:00
|
|
|
// Basic check on the 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!");
|
[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 10:50:23 -05:00
|
|
|
#endif
|
2023-05-24 14:57:23 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
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 00:27:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-09 16:30:44 +02: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;
|
|
|
|
|
|
|
|
// FIXME: See issue #105950 for why LIFETIME markers are considered dead here.
|
|
|
|
if (MI->isLifetimeMarker())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// If there are no defs with uses, the instruction might be dead.
|
|
|
|
return MI->wouldBeTriviallyDead();
|
2008-09-24 00:27:38 +00:00
|
|
|
}
|
|
|
|
|
2024-02-06 17:56:56 +08:00
|
|
|
bool DeadMachineInstructionElimImpl::runImpl(MachineFunction &MF) {
|
2022-09-12 20:58:00 -04:00
|
|
|
MRI = &MF.getRegInfo();
|
|
|
|
|
|
|
|
const TargetSubtargetInfo &ST = MF.getSubtarget();
|
|
|
|
TII = ST.getInstrInfo();
|
|
|
|
LivePhysRegs.init(*ST.getRegisterInfo());
|
|
|
|
|
2020-11-21 00:35:53 +08:00
|
|
|
bool AnyChanges = eliminateDeadMI(MF);
|
|
|
|
while (AnyChanges && eliminateDeadMI(MF))
|
|
|
|
;
|
|
|
|
return AnyChanges;
|
|
|
|
}
|
2014-03-31 17:43:35 +00:00
|
|
|
|
2024-02-06 17:56:56 +08:00
|
|
|
bool DeadMachineInstructionElimImpl::eliminateDeadMI(MachineFunction &MF) {
|
2008-09-17 00:43:24 +00:00
|
|
|
bool AnyChanges = false;
|
2022-04-21 09:11:40 -04:00
|
|
|
|
2008-09-17 00:43:24 +00: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.
|
2020-11-21 00:35:53 +08:00
|
|
|
for (MachineBasicBlock *MBB : post_order(&MF)) {
|
2022-04-21 09:11:40 -04:00
|
|
|
LivePhysRegs.addLiveOuts(*MBB);
|
2010-08-31 21:51:05 +00:00
|
|
|
|
2008-09-23 21:40:44 +00:00
|
|
|
// Now scan the instructions and delete dead ones, tracking physreg
|
|
|
|
// liveness as we go.
|
2022-04-21 09:11:40 -04:00
|
|
|
for (MachineInstr &MI : make_early_inc_range(reverse(*MBB))) {
|
2008-09-24 00:27:38 +00:00
|
|
|
// If the instruction is dead, delete it!
|
2021-11-01 22:38:48 -07:00
|
|
|
if (isDead(&MI)) {
|
|
|
|
LLVM_DEBUG(dbgs() << "DeadMachineInstructionElim: DELETING: " << MI);
|
2010-02-12 18:40:17 +00:00
|
|
|
// It is possible that some DBG_VALUE instructions refer to this
|
2021-12-05 14:55:20 -05:00
|
|
|
// instruction. They will be deleted in the live debug variable
|
|
|
|
// analysis.
|
|
|
|
MI.eraseFromParent();
|
2008-09-24 00:27:38 +00:00
|
|
|
AnyChanges = true;
|
2010-02-06 09:07:11 +00:00
|
|
|
++NumDeletes;
|
2008-09-24 00:27:38 +00:00
|
|
|
continue;
|
2008-09-17 00:43:24 +00:00
|
|
|
}
|
2008-09-23 21:40:44 +00:00
|
|
|
|
2022-04-21 09:11:40 -04:00
|
|
|
LivePhysRegs.stepBackward(MI);
|
2008-09-17 00:43:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-24 00:27:38 +00:00
|
|
|
LivePhysRegs.clear();
|
2008-09-17 00:43:24 +00:00
|
|
|
return AnyChanges;
|
|
|
|
}
|