34#define DEBUG_TYPE "machine-latecleanup"
36STATISTIC(NumRemoved,
"Number of redundant instructions removed.");
40class MachineLateInstrsCleanup {
45 struct Reg2MIMap :
public SmallDenseMap<Register, MachineInstr *> {
48 return MI &&
MI->isIdenticalTo(*ArgMI);
52 std::vector<Reg2MIMap> RegDefs;
53 std::vector<Reg2MIVecMap> RegKills;
90char MachineLateInstrsCleanupLegacy::ID = 0;
95 "Machine Late Instructions Cleanup Pass",
false,
false)
98 if (skipFunction(MF.getFunction()))
101 return MachineLateInstrsCleanup().run(MF);
108 if (!MachineLateInstrsCleanup().
run(MF))
125 bool Changed =
false;
128 Changed |= processBlock(
MBB);
135void MachineLateInstrsCleanup::clearKillsForDef(
Register Reg,
146 if (
auto Kills = MBBKills.find(Reg); Kills != MBBKills.end())
147 for (
auto *KillMI : Kills->second)
148 KillMI->clearRegisterKills(Reg,
TRI);
162 if (!VisitedPreds.
test(Pred->getNumber()))
163 clearKillsForDef(Reg, Pred, VisitedPreds, ToRemoveMI);
166void MachineLateInstrsCleanup::removeRedundantDef(
MachineInstr *
MI) {
168 BitVector VisitedPreds(
MI->getMF()->getNumBlockIDs());
169 clearKillsForDef(Reg,
MI->getParent(), VisitedPreds,
MI);
170 MI->eraseFromParent();
182 bool SawStore =
true;
183 if (!
MI->isSafeToMove(SawStore) ||
MI->isImplicitDef() ||
MI->isInlineAsm())
185 for (
unsigned i = 0, e =
MI->getNumOperands(); i != e; ++i) {
203 bool Changed =
false;
215 return RegDefs[Pred->getNumber()].hasIdentical(Reg, DefMI);
230 if (
MI.modifiesRegister(FrameReg,
TRI)) {
240 if (IsCandidate && MBBDefs.hasIdentical(DefedReg, &
MI)) {
243 removeRedundantDef(&
MI);
251 if (
MI.modifiesRegister(Reg,
TRI)) {
254 }
else if (
MI.findRegisterUseOperandIdx(Reg,
TRI,
true ) != -1)
256 MBBKills[
Reg].push_back(&
MI);
263 MBBDefs[DefedReg] = &
MI;
264 assert(!MBBKills.count(DefedReg) &&
"Should already have been removed.");
MachineInstrBuilder MachineInstrBuilder & DefMI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file implements the BitVector class.
const HexagonInstrInfo * TII
static bool lookup(const GsymReader &GR, DataExtractor &Data, uint64_t &Offset, uint64_t BaseAddr, uint64_t Addr, SourceLocations &SrcLocs, llvm::Error &Err)
A Lookup helper functions.
static bool isCandidate(const MachineInstr *MI, Register &DefedReg, Register FrameReg)
Register const TargetRegisterInfo * TRI
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
This file builds on the ADT/GraphTraits.h file to build a generic graph post order iterator.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
A container for analyses that lazily runs them and caches their results.
Represent the analysis usage information of a pass.
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
bool test(unsigned Idx) const
Represents analyses that only rely on functions' control flow.
static constexpr unsigned NoRegister
An RAII based helper class to modify MachineFunctionProperties when running pass.
bool isInlineAsmBrIndirectTarget() const
Returns true if this is the indirect dest of an INLINEASM_BR.
bool isEHPad() const
Returns true if the block is a landing pad.
int getNumber() const
MachineBasicBlocks are uniquely numbered at the function level, unless they're not in a MachineFuncti...
pred_iterator pred_begin()
void addLiveIn(MCRegister PhysReg, LaneBitmask LaneMask=LaneBitmask::getAll())
Adds the specified register as a live in.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
iterator_range< pred_iterator > predecessors()
LLVM_ABI bool isLiveIn(MCRegister Reg, LaneBitmask LaneMask=LaneBitmask::getAll()) const
Return true if the specified register is in the live in set.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
virtual bool runOnMachineFunction(MachineFunction &MF)=0
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
virtual MachineFunctionProperties getRequiredProperties() const
Properties which a MachineFunction may have at a given point in time.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
unsigned getNumBlockIDs() const
getNumBlockIDs - Return the number of MBB ID's allocated.
Representation of each machine instruction.
const MachineBasicBlock * getParent() const
LLVM_ABI bool isIdenticalTo(const MachineInstr &Other, MICheckType Check=CheckDefs) const
Return true if this instruction is identical to Other.
PreservedAnalyses run(MachineFunction &MachineFunction, MachineFunctionAnalysisManager &MachineFunctionAM)
MachineOperand class - Representation of each machine instruction operand.
bool isCImm() const
isCImm - Test if this is a MO_CImmediate operand.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
bool isCPI() const
isCPI - Tests if this is a MO_ConstantPoolIndex operand.
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
bool isSymbol() const
isSymbol - Tests if this is a MO_ExternalSymbol operand.
bool isGlobal() const
isGlobal - Tests if this is a MO_GlobalAddress operand.
Register getReg() const
getReg - Returns the register number.
bool isFPImm() const
isFPImm - Tests if this is a MO_FPImmediate operand.
static LLVM_ABI PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
A set of analyses that are preserved following a run of a transformation pass.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Wrapper class representing virtual and physical registers.
constexpr bool isValid() const
TargetInstrInfo - Interface to description of machine instruction set.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
virtual const TargetInstrInfo * getInstrInfo() const
virtual const TargetRegisterInfo * getRegisterInfo() const =0
Return the target's register information.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Reg
All possible values of the reg field in the ModR/M byte.
PointerTypeMap run(const Module &M)
Compute the PointerTypeMap for the module M.
This is an optimization pass for GlobalISel generic memory operations.
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
LLVM_ABI char & MachineLateInstrsCleanupID
MachineLateInstrsCleanup - This pass removes redundant identical instructions after register allocati...
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
LLVM_ABI void initializeMachineLateInstrsCleanupLegacyPass(PassRegistry &)
LLVM_ABI Printable printMBBReference(const MachineBasicBlock &MBB)
Prints a machine basic block reference.