2008-09-17 00:43:24 +00: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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-02-06 09:07:11 +00:00
|
|
|
#define DEBUG_TYPE "codegen-dce"
|
2008-09-17 00:43:24 +00:00
|
|
|
#include "llvm/CodeGen/Passes.h"
|
|
|
|
#include "llvm/Pass.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.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"
|
2008-09-17 00:43:24 +00:00
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2010-02-06 09:07:11 +00:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2008-09-17 00:43:24 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2010-02-06 09:07:11 +00:00
|
|
|
STATISTIC(NumDeletes, "Number of dead instructions deleted");
|
|
|
|
|
2008-09-17 00:43:24 +00:00
|
|
|
namespace {
|
2009-10-25 06:33:48 +00:00
|
|
|
class DeadMachineInstructionElim : public MachineFunctionPass {
|
2008-09-17 00:43:24 +00:00
|
|
|
virtual bool runOnMachineFunction(MachineFunction &MF);
|
2012-02-08 21:22:43 +00:00
|
|
|
|
2008-09-24 00:27:38 +00:00
|
|
|
const TargetRegisterInfo *TRI;
|
|
|
|
const MachineRegisterInfo *MRI;
|
|
|
|
const TargetInstrInfo *TII;
|
|
|
|
BitVector LivePhysRegs;
|
|
|
|
|
2008-09-17 00:43:24 +00:00
|
|
|
public:
|
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2010-10-19 17:21:58 +00:00
|
|
|
DeadMachineInstructionElim() : MachineFunctionPass(ID) {
|
|
|
|
initializeDeadMachineInstructionElimPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2008-09-24 00:27:38 +00:00
|
|
|
|
|
|
|
private:
|
2009-08-11 15:13:43 +00:00
|
|
|
bool isDead(const MachineInstr *MI) const;
|
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
|
|
|
|
2010-07-21 22:09:45 +00:00
|
|
|
INITIALIZE_PASS(DeadMachineInstructionElim, "dead-mi-elimination",
|
2010-10-07 22:25:06 +00:00
|
|
|
"Remove dead machine instructions", false, false)
|
2008-09-17 00:43:24 +00:00
|
|
|
|
2009-08-11 15:13:43 +00:00
|
|
|
bool DeadMachineInstructionElim::isDead(const MachineInstr *MI) const {
|
2011-01-07 23:50:32 +00: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;
|
|
|
|
|
2008-09-24 00:27:38 +00:00
|
|
|
// Don't delete instructions with side effects.
|
|
|
|
bool SawStore = false;
|
2010-03-02 19:03:01 +00:00
|
|
|
if (!MI->isSafeToMove(TII, 0, SawStore) && !MI->isPHI())
|
2008-09-24 00:27:38 +00: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 15:45:36 +00:00
|
|
|
if (MO.isReg() && MO.isDef()) {
|
2008-09-24 00:27:38 +00:00
|
|
|
unsigned Reg = MO.getReg();
|
2012-02-09 00:15:39 +00:00
|
|
|
if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
|
|
|
|
// Don't delete live physreg defs, or any reserved register defs.
|
2012-10-15 21:57:41 +00:00
|
|
|
if (LivePhysRegs.test(Reg) || MRI->isReserved(Reg))
|
2012-02-09 00:15:39 +00: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 00:27:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there are no defs with uses, the instruction is dead.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-09-17 00:43:24 +00:00
|
|
|
bool DeadMachineInstructionElim::runOnMachineFunction(MachineFunction &MF) {
|
|
|
|
bool AnyChanges = false;
|
2008-09-24 00:27:38 +00:00
|
|
|
MRI = &MF.getRegInfo();
|
|
|
|
TRI = MF.getTarget().getRegisterInfo();
|
|
|
|
TII = MF.getTarget().getInstrInfo();
|
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.
|
|
|
|
for (MachineFunction::reverse_iterator I = MF.rbegin(), E = MF.rend();
|
|
|
|
I != E; ++I) {
|
|
|
|
MachineBasicBlock *MBB = &*I;
|
2008-09-23 21:40:44 +00:00
|
|
|
|
2010-08-31 21:51:05 +00:00
|
|
|
// Start out assuming that reserved registers are live out of this block.
|
2012-10-15 21:57:41 +00:00
|
|
|
LivePhysRegs = MRI->getReservedRegs();
|
2008-09-23 21:40:44 +00:00
|
|
|
|
|
|
|
// Also add any explicit live-out physregs for this block.
|
2011-12-07 07:15:52 +00:00
|
|
|
if (!MBB->empty() && MBB->back().isReturn())
|
2008-09-24 00:27:38 +00:00
|
|
|
for (MachineRegisterInfo::liveout_iterator LOI = MRI->liveout_begin(),
|
|
|
|
LOE = MRI->liveout_end(); LOI != LOE; ++LOI) {
|
2008-09-23 21:40:44 +00:00
|
|
|
unsigned Reg = *LOI;
|
|
|
|
if (TargetRegisterInfo::isPhysicalRegister(Reg))
|
|
|
|
LivePhysRegs.set(Reg);
|
|
|
|
}
|
|
|
|
|
2011-06-27 15:00:36 +00:00
|
|
|
// Add live-ins from sucessors to LivePhysRegs. Normally, physregs are not
|
|
|
|
// live across blocks, but some targets (x86) can have flags live out of a
|
|
|
|
// block.
|
|
|
|
for (MachineBasicBlock::succ_iterator S = MBB->succ_begin(),
|
|
|
|
E = MBB->succ_end(); S != E; S++)
|
|
|
|
for (MachineBasicBlock::livein_iterator LI = (*S)->livein_begin();
|
|
|
|
LI != (*S)->livein_end(); LI++)
|
|
|
|
LivePhysRegs.set(*LI);
|
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.
|
2008-09-17 00:43:24 +00:00
|
|
|
for (MachineBasicBlock::reverse_iterator MII = MBB->rbegin(),
|
|
|
|
MIE = MBB->rend(); MII != MIE; ) {
|
|
|
|
MachineInstr *MI = &*MII;
|
|
|
|
|
2008-09-24 00:27:38 +00:00
|
|
|
// If the instruction is dead, delete it!
|
|
|
|
if (isDead(MI)) {
|
2010-01-04 19:10:20 +00:00
|
|
|
DEBUG(dbgs() << "DeadMachineInstructionElim: DELETING: " << *MI);
|
2010-02-12 18:40:17 +00:00
|
|
|
// It is possible that some DBG_VALUE instructions refer to this
|
|
|
|
// instruction. Examine each def operand for such references;
|
|
|
|
// if found, mark the DBG_VALUE as undef (but don't delete it).
|
|
|
|
for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
|
|
|
|
const MachineOperand &MO = MI->getOperand(i);
|
|
|
|
if (!MO.isReg() || !MO.isDef())
|
|
|
|
continue;
|
|
|
|
unsigned Reg = MO.getReg();
|
|
|
|
if (!TargetRegisterInfo::isVirtualRegister(Reg))
|
|
|
|
continue;
|
|
|
|
MachineRegisterInfo::use_iterator nextI;
|
|
|
|
for (MachineRegisterInfo::use_iterator I = MRI->use_begin(Reg),
|
|
|
|
E = MRI->use_end(); I!=E; I=nextI) {
|
|
|
|
nextI = llvm::next(I); // I is invalidated by the setReg
|
|
|
|
MachineOperand& Use = I.getOperand();
|
|
|
|
MachineInstr *UseMI = Use.getParent();
|
|
|
|
if (UseMI==MI)
|
|
|
|
continue;
|
|
|
|
assert(Use.isDebug());
|
|
|
|
UseMI->getOperand(0).setReg(0U);
|
|
|
|
}
|
|
|
|
}
|
2008-09-24 00:27:38 +00:00
|
|
|
AnyChanges = true;
|
|
|
|
MI->eraseFromParent();
|
2010-02-06 09:07:11 +00:00
|
|
|
++NumDeletes;
|
2008-09-24 00:27:38 +00:00
|
|
|
MIE = MBB->rend();
|
|
|
|
// MII is now pointing to the next instruction to process,
|
|
|
|
// so don't increment it.
|
|
|
|
continue;
|
2008-09-17 00:43:24 +00:00
|
|
|
}
|
2008-09-23 21:40:44 +00:00
|
|
|
|
|
|
|
// Record the physreg defs.
|
|
|
|
for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
|
|
|
|
const MachineOperand &MO = MI->getOperand(i);
|
2008-10-03 15:45:36 +00:00
|
|
|
if (MO.isReg() && MO.isDef()) {
|
2008-09-23 21:40:44 +00:00
|
|
|
unsigned Reg = MO.getReg();
|
2011-01-10 02:58:51 +00:00
|
|
|
if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
|
2008-09-23 21:40:44 +00:00
|
|
|
LivePhysRegs.reset(Reg);
|
2008-10-16 00:11:23 +00:00
|
|
|
// Check the subreg set, not the alias set, because a def
|
|
|
|
// of a super-register may still be partially live after
|
|
|
|
// this def.
|
2012-06-01 23:28:30 +00:00
|
|
|
for (MCSubRegIterator SR(Reg, TRI); SR.isValid(); ++SR)
|
|
|
|
LivePhysRegs.reset(*SR);
|
2008-09-23 21:40:44 +00:00
|
|
|
}
|
2012-01-20 22:27:09 +00:00
|
|
|
} else if (MO.isRegMask()) {
|
|
|
|
// Register mask of preserved registers. All clobbers are dead.
|
2012-02-02 23:52:57 +00:00
|
|
|
LivePhysRegs.clearBitsNotInMask(MO.getRegMask());
|
2008-09-23 21:40:44 +00: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 15:45:36 +00:00
|
|
|
if (MO.isReg() && MO.isUse()) {
|
2008-09-23 21:40:44 +00:00
|
|
|
unsigned Reg = MO.getReg();
|
2011-01-10 02:58:51 +00:00
|
|
|
if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
|
2012-06-01 20:36:54 +00:00
|
|
|
for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
|
|
|
|
LivePhysRegs.set(*AI);
|
2008-09-23 21:40:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-17 00:43:24 +00:00
|
|
|
// We didn't delete the current instruction, so increment MII to
|
|
|
|
// the next one.
|
|
|
|
++MII;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-24 00:27:38 +00:00
|
|
|
LivePhysRegs.clear();
|
2008-09-17 00:43:24 +00:00
|
|
|
return AnyChanges;
|
|
|
|
}
|