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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/CodeGen/Passes.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2008-09-17 00:43:24 +00:00
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.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"
|
2008-09-17 00:43:24 +00:00
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
2014-08-04 21:25:23 +00:00
|
|
|
#include "llvm/Target/TargetSubtargetInfo.h"
|
|
|
|
|
2008-09-17 00:43:24 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 02:02:50 +00:00
|
|
|
#define DEBUG_TYPE "codegen-dce"
|
|
|
|
|
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 {
|
2014-03-07 09:26:03 +00:00
|
|
|
bool runOnMachineFunction(MachineFunction &MF) override;
|
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
|
|
|
};
|
2015-06-23 09:49:53 +00: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
|
|
|
|
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;
|
|
|
|
|
2015-01-13 00:48:10 +00: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
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@241633 91177308-0d34-0410-b5e6-96231b3b80d8
2015-07-07 22:25:32 +00:00
|
|
|
if (MI->getOpcode() == TargetOpcode::LOCAL_ESCAPE)
|
2015-01-13 00:48:10 +00:00
|
|
|
return false;
|
|
|
|
|
2008-09-24 00:27:38 +00:00
|
|
|
// Don't delete instructions with side effects.
|
|
|
|
bool SawStore = false;
|
2015-05-19 21:22:20 +00:00
|
|
|
if (!MI->isSafeToMove(nullptr, 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) {
|
2014-03-31 17:43:35 +00:00
|
|
|
if (skipOptnoneFunction(*MF.getFunction()))
|
|
|
|
return false;
|
|
|
|
|
2008-09-17 00:43:24 +00:00
|
|
|
bool AnyChanges = false;
|
2008-09-24 00:27:38 +00:00
|
|
|
MRI = &MF.getRegInfo();
|
2014-08-05 02:39:49 +00:00
|
|
|
TRI = MF.getSubtarget().getRegisterInfo();
|
|
|
|
TII = MF.getSubtarget().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
|
|
|
|
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
|
2014-08-13 21:15:23 +00:00
|
|
|
// instruction. They get marked as undef and will be deleted
|
|
|
|
// in the live debug variable analysis.
|
|
|
|
MI->eraseFromParentAndMarkDBGValuesForRemoval();
|
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
|
|
|
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-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.
|
2013-05-22 23:17:36 +00:00
|
|
|
for (MCSubRegIterator SR(Reg, TRI,/*IncludeSelf=*/true);
|
|
|
|
SR.isValid(); ++SR)
|
2012-06-01 23:28:30 +00:00
|
|
|
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;
|
|
|
|
}
|