2008-09-17 08:43:24 +08: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-04 00:50:05 +08:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2008-09-17 08:43:24 +08:00
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Pass.h"
|
2008-09-25 09:06:50 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2009-08-23 04:04:03 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2008-09-17 08:43:24 +08:00
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
2014-08-05 05:25:23 +08:00
|
|
|
#include "llvm/Target/TargetSubtargetInfo.h"
|
|
|
|
|
2008-09-17 08:43:24 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 10:02:50 +08:00
|
|
|
#define DEBUG_TYPE "codegen-dce"
|
|
|
|
|
2010-02-06 17:07:11 +08:00
|
|
|
STATISTIC(NumDeletes, "Number of dead instructions deleted");
|
|
|
|
|
2008-09-17 08:43:24 +08:00
|
|
|
namespace {
|
2009-10-25 14:33:48 +08:00
|
|
|
class DeadMachineInstructionElim : public MachineFunctionPass {
|
2014-03-07 17:26:03 +08:00
|
|
|
bool runOnMachineFunction(MachineFunction &MF) override;
|
2012-02-09 05:22:43 +08:00
|
|
|
|
2008-09-24 08:27:38 +08:00
|
|
|
const TargetRegisterInfo *TRI;
|
|
|
|
const MachineRegisterInfo *MRI;
|
|
|
|
const TargetInstrInfo *TII;
|
|
|
|
BitVector LivePhysRegs;
|
|
|
|
|
2008-09-17 08:43:24 +08:00
|
|
|
public:
|
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2010-10-20 01:21:58 +08:00
|
|
|
DeadMachineInstructionElim() : MachineFunctionPass(ID) {
|
|
|
|
initializeDeadMachineInstructionElimPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2008-09-24 08:27:38 +08:00
|
|
|
|
2016-06-22 07:01:17 +08:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
|
|
|
AU.setPreservesCFG();
|
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
|
|
|
|
2008-09-24 08:27:38 +08:00
|
|
|
private:
|
2009-08-11 23:13:43 +08:00
|
|
|
bool isDead(const MachineInstr *MI) const;
|
2008-09-17 08:43:24 +08:00
|
|
|
};
|
2015-06-23 17:49:53 +08:00
|
|
|
}
|
2008-09-17 08:43:24 +08:00
|
|
|
char DeadMachineInstructionElim::ID = 0;
|
2012-02-09 05:23:13 +08:00
|
|
|
char &llvm::DeadMachineInstructionElimID = DeadMachineInstructionElim::ID;
|
2008-09-17 08:43:24 +08:00
|
|
|
|
2010-07-22 06:09:45 +08:00
|
|
|
INITIALIZE_PASS(DeadMachineInstructionElim, "dead-mi-elimination",
|
2010-10-08 06:25:06 +08:00
|
|
|
"Remove dead machine instructions", false, false)
|
2008-09-17 08:43:24 +08:00
|
|
|
|
2009-08-11 23:13:43 +08:00
|
|
|
bool DeadMachineInstructionElim::isDead(const MachineInstr *MI) const {
|
2011-01-08 07:50:32 +08: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 08:48:10 +08: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 06:25:32 +08:00
|
|
|
if (MI->getOpcode() == TargetOpcode::LOCAL_ESCAPE)
|
2015-01-13 08:48:10 +08:00
|
|
|
return false;
|
|
|
|
|
2008-09-24 08:27:38 +08:00
|
|
|
// Don't delete instructions with side effects.
|
|
|
|
bool SawStore = false;
|
2015-05-20 05:22:20 +08:00
|
|
|
if (!MI->isSafeToMove(nullptr, SawStore) && !MI->isPHI())
|
2008-09-24 08:27:38 +08: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 23:45:36 +08:00
|
|
|
if (MO.isReg() && MO.isDef()) {
|
2008-09-24 08:27:38 +08:00
|
|
|
unsigned Reg = MO.getReg();
|
2012-02-09 08:15:39 +08:00
|
|
|
if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
|
|
|
|
// Don't delete live physreg defs, or any reserved register defs.
|
2012-10-16 05:57:41 +08:00
|
|
|
if (LivePhysRegs.test(Reg) || MRI->isReserved(Reg))
|
2012-02-09 08:15:39 +08: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 08:27:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there are no defs with uses, the instruction is dead.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-09-17 08:43:24 +08:00
|
|
|
bool DeadMachineInstructionElim::runOnMachineFunction(MachineFunction &MF) {
|
2016-04-23 06:06:11 +08:00
|
|
|
if (skipFunction(*MF.getFunction()))
|
2014-04-01 01:43:35 +08:00
|
|
|
return false;
|
|
|
|
|
2008-09-17 08:43:24 +08:00
|
|
|
bool AnyChanges = false;
|
2008-09-24 08:27:38 +08:00
|
|
|
MRI = &MF.getRegInfo();
|
2014-08-05 10:39:49 +08:00
|
|
|
TRI = MF.getSubtarget().getRegisterInfo();
|
|
|
|
TII = MF.getSubtarget().getInstrInfo();
|
2008-09-17 08:43:24 +08: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-25 05:13:43 +08:00
|
|
|
for (MachineBasicBlock &MBB : make_range(MF.rbegin(), MF.rend())) {
|
2010-09-01 05:51:05 +08:00
|
|
|
// Start out assuming that reserved registers are live out of this block.
|
2012-10-16 05:57:41 +08:00
|
|
|
LivePhysRegs = MRI->getReservedRegs();
|
2008-09-24 05:40:44 +08:00
|
|
|
|
2017-02-16 06:19:06 +08:00
|
|
|
// Add live-ins from successors to LivePhysRegs. Normally, physregs are not
|
2011-06-27 23:00:36 +08:00
|
|
|
// live across blocks, but some targets (x86) can have flags live out of a
|
|
|
|
// block.
|
2015-07-25 05:13:43 +08:00
|
|
|
for (MachineBasicBlock::succ_iterator S = MBB.succ_begin(),
|
|
|
|
E = MBB.succ_end(); S != E; S++)
|
2015-09-10 02:08:03 +08:00
|
|
|
for (const auto &LI : (*S)->liveins())
|
|
|
|
LivePhysRegs.set(LI.PhysReg);
|
2010-09-01 05:51:05 +08:00
|
|
|
|
2008-09-24 05:40:44 +08:00
|
|
|
// Now scan the instructions and delete dead ones, tracking physreg
|
|
|
|
// liveness as we go.
|
2015-07-25 05:13:43 +08:00
|
|
|
for (MachineBasicBlock::reverse_iterator MII = MBB.rbegin(),
|
|
|
|
MIE = MBB.rend(); MII != MIE; ) {
|
2016-09-12 02:51:28 +08:00
|
|
|
MachineInstr *MI = &*MII++;
|
2008-09-17 08:43:24 +08:00
|
|
|
|
2008-09-24 08:27:38 +08:00
|
|
|
// If the instruction is dead, delete it!
|
|
|
|
if (isDead(MI)) {
|
2010-01-05 03:10:20 +08:00
|
|
|
DEBUG(dbgs() << "DeadMachineInstructionElim: DELETING: " << *MI);
|
2010-02-13 02:40:17 +08:00
|
|
|
// It is possible that some DBG_VALUE instructions refer to this
|
2014-08-14 05:15:23 +08:00
|
|
|
// instruction. They get marked as undef and will be deleted
|
|
|
|
// in the live debug variable analysis.
|
|
|
|
MI->eraseFromParentAndMarkDBGValuesForRemoval();
|
2008-09-24 08:27:38 +08:00
|
|
|
AnyChanges = true;
|
2010-02-06 17:07:11 +08:00
|
|
|
++NumDeletes;
|
2008-09-24 08:27:38 +08:00
|
|
|
continue;
|
2008-09-17 08:43:24 +08:00
|
|
|
}
|
2008-09-24 05:40:44 +08:00
|
|
|
|
|
|
|
// Record the physreg defs.
|
|
|
|
for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
|
|
|
|
const MachineOperand &MO = MI->getOperand(i);
|
2008-10-03 23:45:36 +08:00
|
|
|
if (MO.isReg() && MO.isDef()) {
|
2008-09-24 05:40:44 +08:00
|
|
|
unsigned Reg = MO.getReg();
|
2011-01-10 10:58:51 +08:00
|
|
|
if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
|
2008-10-16 08:11:23 +08: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 07:17:36 +08:00
|
|
|
for (MCSubRegIterator SR(Reg, TRI,/*IncludeSelf=*/true);
|
|
|
|
SR.isValid(); ++SR)
|
2012-06-02 07:28:30 +08:00
|
|
|
LivePhysRegs.reset(*SR);
|
2008-09-24 05:40:44 +08:00
|
|
|
}
|
2012-01-21 06:27:09 +08:00
|
|
|
} else if (MO.isRegMask()) {
|
|
|
|
// Register mask of preserved registers. All clobbers are dead.
|
2012-02-03 07:52:57 +08:00
|
|
|
LivePhysRegs.clearBitsNotInMask(MO.getRegMask());
|
2008-09-24 05:40:44 +08: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 23:45:36 +08:00
|
|
|
if (MO.isReg() && MO.isUse()) {
|
2008-09-24 05:40:44 +08:00
|
|
|
unsigned Reg = MO.getReg();
|
2011-01-10 10:58:51 +08:00
|
|
|
if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
|
2012-06-02 04:36:54 +08:00
|
|
|
for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
|
|
|
|
LivePhysRegs.set(*AI);
|
2008-09-24 05:40:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-09-17 08:43:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-24 08:27:38 +08:00
|
|
|
LivePhysRegs.clear();
|
2008-09-17 08:43:24 +08:00
|
|
|
return AnyChanges;
|
|
|
|
}
|