2008-09-17 02:43:24 +02:00
|
|
|
//===- DeadMachineInstructionElim.cpp - Remove dead machine instructions --===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// 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"
|
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"
|
2008-09-17 02:43:24 +02:00
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
2014-08-04 23:25:23 +02:00
|
|
|
#include "llvm/Target/TargetSubtargetInfo.h"
|
|
|
|
|
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 {
|
|
|
|
AU.setPreservesCFG();
|
|
|
|
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()) {
|
2008-09-24 02:27:38 +02:00
|
|
|
unsigned Reg = MO.getReg();
|
2012-02-09 01:15:39 +01:00
|
|
|
if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
|
|
|
|
// Don't delete live physreg defs, or any reserved register defs.
|
2012-10-15 23:57:41 +02:00
|
|
|
if (LivePhysRegs.test(Reg) || MRI->isReserved(Reg))
|
2012-02-09 01:15:39 +01:00
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
if (!MRI->use_nodbg_empty(Reg))
|
|
|
|
// 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) {
|
2016-04-23 00:06:11 +02: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)) {
|
2010-01-04 20:10:20 +01:00
|
|
|
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()) {
|
2008-09-23 23:40:44 +02:00
|
|
|
unsigned Reg = MO.getReg();
|
2011-01-10 03:58:51 +01:00
|
|
|
if (TargetRegisterInfo::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()) {
|
2008-09-23 23:40:44 +02:00
|
|
|
unsigned Reg = MO.getReg();
|
2011-01-10 03:58:51 +01:00
|
|
|
if (TargetRegisterInfo::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;
|
|
|
|
}
|