xref: /freebsd/contrib/llvm-project/llvm/lib/CodeGen/DeadMachineInstructionElim.cpp (revision fe6060f10f634930ff71b7c50291ddc610da2475)
10b57cec5SDimitry Andric //===- DeadMachineInstructionElim.cpp - Remove dead machine instructions --===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This is an extremely simple MachineInstr-level dead-code-elimination pass.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
13e8d8bef9SDimitry Andric #include "llvm/ADT/PostOrderIterator.h"
140b57cec5SDimitry Andric #include "llvm/ADT/Statistic.h"
150b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h"
160b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
170b57cec5SDimitry Andric #include "llvm/CodeGen/Passes.h"
180b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h"
19480093f4SDimitry Andric #include "llvm/InitializePasses.h"
200b57cec5SDimitry Andric #include "llvm/Pass.h"
210b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
220b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
230b57cec5SDimitry Andric 
240b57cec5SDimitry Andric using namespace llvm;
250b57cec5SDimitry Andric 
260b57cec5SDimitry Andric #define DEBUG_TYPE "dead-mi-elimination"
270b57cec5SDimitry Andric 
280b57cec5SDimitry Andric STATISTIC(NumDeletes,          "Number of dead instructions deleted");
290b57cec5SDimitry Andric 
300b57cec5SDimitry Andric namespace {
310b57cec5SDimitry Andric   class DeadMachineInstructionElim : public MachineFunctionPass {
320b57cec5SDimitry Andric     bool runOnMachineFunction(MachineFunction &MF) override;
330b57cec5SDimitry Andric 
340b57cec5SDimitry Andric     const TargetRegisterInfo *TRI;
350b57cec5SDimitry Andric     const MachineRegisterInfo *MRI;
360b57cec5SDimitry Andric     const TargetInstrInfo *TII;
370b57cec5SDimitry Andric     BitVector LivePhysRegs;
380b57cec5SDimitry Andric 
390b57cec5SDimitry Andric   public:
400b57cec5SDimitry Andric     static char ID; // Pass identification, replacement for typeid
410b57cec5SDimitry Andric     DeadMachineInstructionElim() : MachineFunctionPass(ID) {
420b57cec5SDimitry Andric      initializeDeadMachineInstructionElimPass(*PassRegistry::getPassRegistry());
430b57cec5SDimitry Andric     }
440b57cec5SDimitry Andric 
450b57cec5SDimitry Andric     void getAnalysisUsage(AnalysisUsage &AU) const override {
460b57cec5SDimitry Andric       AU.setPreservesCFG();
470b57cec5SDimitry Andric       MachineFunctionPass::getAnalysisUsage(AU);
480b57cec5SDimitry Andric     }
490b57cec5SDimitry Andric 
500b57cec5SDimitry Andric   private:
510b57cec5SDimitry Andric     bool isDead(const MachineInstr *MI) const;
52e8d8bef9SDimitry Andric 
53e8d8bef9SDimitry Andric     bool eliminateDeadMI(MachineFunction &MF);
540b57cec5SDimitry Andric   };
550b57cec5SDimitry Andric }
560b57cec5SDimitry Andric char DeadMachineInstructionElim::ID = 0;
570b57cec5SDimitry Andric char &llvm::DeadMachineInstructionElimID = DeadMachineInstructionElim::ID;
580b57cec5SDimitry Andric 
590b57cec5SDimitry Andric INITIALIZE_PASS(DeadMachineInstructionElim, DEBUG_TYPE,
600b57cec5SDimitry Andric                 "Remove dead machine instructions", false, false)
610b57cec5SDimitry Andric 
620b57cec5SDimitry Andric bool DeadMachineInstructionElim::isDead(const MachineInstr *MI) const {
630b57cec5SDimitry Andric   // Technically speaking inline asm without side effects and no defs can still
640b57cec5SDimitry Andric   // be deleted. But there is so much bad inline asm code out there, we should
650b57cec5SDimitry Andric   // let them be.
660b57cec5SDimitry Andric   if (MI->isInlineAsm())
670b57cec5SDimitry Andric     return false;
680b57cec5SDimitry Andric 
690b57cec5SDimitry Andric   // Don't delete frame allocation labels.
700b57cec5SDimitry Andric   if (MI->getOpcode() == TargetOpcode::LOCAL_ESCAPE)
710b57cec5SDimitry Andric     return false;
720b57cec5SDimitry Andric 
730b57cec5SDimitry Andric   // Don't delete instructions with side effects.
740b57cec5SDimitry Andric   bool SawStore = false;
750b57cec5SDimitry Andric   if (!MI->isSafeToMove(nullptr, SawStore) && !MI->isPHI())
760b57cec5SDimitry Andric     return false;
770b57cec5SDimitry Andric 
780b57cec5SDimitry Andric   // Examine each operand.
790b57cec5SDimitry Andric   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
800b57cec5SDimitry Andric     const MachineOperand &MO = MI->getOperand(i);
810b57cec5SDimitry Andric     if (MO.isReg() && MO.isDef()) {
828bcb0991SDimitry Andric       Register Reg = MO.getReg();
838bcb0991SDimitry Andric       if (Register::isPhysicalRegister(Reg)) {
840b57cec5SDimitry Andric         // Don't delete live physreg defs, or any reserved register defs.
850b57cec5SDimitry Andric         if (LivePhysRegs.test(Reg) || MRI->isReserved(Reg))
860b57cec5SDimitry Andric           return false;
870b57cec5SDimitry Andric       } else {
88480093f4SDimitry Andric         if (MO.isDead()) {
89480093f4SDimitry Andric #ifndef NDEBUG
90480093f4SDimitry Andric           // Sanity check on uses of this dead register. All of them should be
91480093f4SDimitry Andric           // 'undef'.
92480093f4SDimitry Andric           for (auto &U : MRI->use_nodbg_operands(Reg))
93480093f4SDimitry Andric             assert(U.isUndef() && "'Undef' use on a 'dead' register is found!");
94480093f4SDimitry Andric #endif
95480093f4SDimitry Andric           continue;
96480093f4SDimitry Andric         }
970b57cec5SDimitry Andric         for (const MachineInstr &Use : MRI->use_nodbg_instructions(Reg)) {
980b57cec5SDimitry Andric           if (&Use != MI)
990b57cec5SDimitry Andric             // This def has a non-debug use. Don't delete the instruction!
1000b57cec5SDimitry Andric             return false;
1010b57cec5SDimitry Andric         }
1020b57cec5SDimitry Andric       }
1030b57cec5SDimitry Andric     }
1040b57cec5SDimitry Andric   }
1050b57cec5SDimitry Andric 
1060b57cec5SDimitry Andric   // If there are no defs with uses, the instruction is dead.
1070b57cec5SDimitry Andric   return true;
1080b57cec5SDimitry Andric }
1090b57cec5SDimitry Andric 
1100b57cec5SDimitry Andric bool DeadMachineInstructionElim::runOnMachineFunction(MachineFunction &MF) {
1110b57cec5SDimitry Andric   if (skipFunction(MF.getFunction()))
1120b57cec5SDimitry Andric     return false;
113e8d8bef9SDimitry Andric   bool AnyChanges = eliminateDeadMI(MF);
114e8d8bef9SDimitry Andric   while (AnyChanges && eliminateDeadMI(MF))
115e8d8bef9SDimitry Andric     ;
116e8d8bef9SDimitry Andric   return AnyChanges;
117e8d8bef9SDimitry Andric }
1180b57cec5SDimitry Andric 
119e8d8bef9SDimitry Andric bool DeadMachineInstructionElim::eliminateDeadMI(MachineFunction &MF) {
1200b57cec5SDimitry Andric   bool AnyChanges = false;
1210b57cec5SDimitry Andric   MRI = &MF.getRegInfo();
1220b57cec5SDimitry Andric   TRI = MF.getSubtarget().getRegisterInfo();
1230b57cec5SDimitry Andric   TII = MF.getSubtarget().getInstrInfo();
1240b57cec5SDimitry Andric 
1250b57cec5SDimitry Andric   // Loop over all instructions in all blocks, from bottom to top, so that it's
1260b57cec5SDimitry Andric   // more likely that chains of dependent but ultimately dead instructions will
1270b57cec5SDimitry Andric   // be cleaned up.
128e8d8bef9SDimitry Andric   for (MachineBasicBlock *MBB : post_order(&MF)) {
1290b57cec5SDimitry Andric     // Start out assuming that reserved registers are live out of this block.
1300b57cec5SDimitry Andric     LivePhysRegs = MRI->getReservedRegs();
1310b57cec5SDimitry Andric 
1320b57cec5SDimitry Andric     // Add live-ins from successors to LivePhysRegs. Normally, physregs are not
1330b57cec5SDimitry Andric     // live across blocks, but some targets (x86) can have flags live out of a
1340b57cec5SDimitry Andric     // block.
135*fe6060f1SDimitry Andric     for (const MachineBasicBlock *Succ : MBB->successors())
136*fe6060f1SDimitry Andric       for (const auto &LI : Succ->liveins())
1370b57cec5SDimitry Andric         LivePhysRegs.set(LI.PhysReg);
1380b57cec5SDimitry Andric 
1390b57cec5SDimitry Andric     // Now scan the instructions and delete dead ones, tracking physreg
1400b57cec5SDimitry Andric     // liveness as we go.
141e8d8bef9SDimitry Andric     for (MachineBasicBlock::reverse_iterator MII = MBB->rbegin(),
142e8d8bef9SDimitry Andric                                              MIE = MBB->rend();
143e8d8bef9SDimitry Andric          MII != MIE;) {
1440b57cec5SDimitry Andric       MachineInstr *MI = &*MII++;
1450b57cec5SDimitry Andric 
1460b57cec5SDimitry Andric       // If the instruction is dead, delete it!
1470b57cec5SDimitry Andric       if (isDead(MI)) {
1480b57cec5SDimitry Andric         LLVM_DEBUG(dbgs() << "DeadMachineInstructionElim: DELETING: " << *MI);
1490b57cec5SDimitry Andric         // It is possible that some DBG_VALUE instructions refer to this
1500b57cec5SDimitry Andric         // instruction.  They get marked as undef and will be deleted
1510b57cec5SDimitry Andric         // in the live debug variable analysis.
1520b57cec5SDimitry Andric         MI->eraseFromParentAndMarkDBGValuesForRemoval();
1530b57cec5SDimitry Andric         AnyChanges = true;
1540b57cec5SDimitry Andric         ++NumDeletes;
1550b57cec5SDimitry Andric         continue;
1560b57cec5SDimitry Andric       }
1570b57cec5SDimitry Andric 
1580b57cec5SDimitry Andric       // Record the physreg defs.
1590b57cec5SDimitry Andric       for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1600b57cec5SDimitry Andric         const MachineOperand &MO = MI->getOperand(i);
1610b57cec5SDimitry Andric         if (MO.isReg() && MO.isDef()) {
1628bcb0991SDimitry Andric           Register Reg = MO.getReg();
1638bcb0991SDimitry Andric           if (Register::isPhysicalRegister(Reg)) {
1640b57cec5SDimitry Andric             // Check the subreg set, not the alias set, because a def
1650b57cec5SDimitry Andric             // of a super-register may still be partially live after
1660b57cec5SDimitry Andric             // this def.
1670b57cec5SDimitry Andric             for (MCSubRegIterator SR(Reg, TRI,/*IncludeSelf=*/true);
1680b57cec5SDimitry Andric                  SR.isValid(); ++SR)
1690b57cec5SDimitry Andric               LivePhysRegs.reset(*SR);
1700b57cec5SDimitry Andric           }
1710b57cec5SDimitry Andric         } else if (MO.isRegMask()) {
1720b57cec5SDimitry Andric           // Register mask of preserved registers. All clobbers are dead.
1730b57cec5SDimitry Andric           LivePhysRegs.clearBitsNotInMask(MO.getRegMask());
1740b57cec5SDimitry Andric         }
1750b57cec5SDimitry Andric       }
1760b57cec5SDimitry Andric       // Record the physreg uses, after the defs, in case a physreg is
1770b57cec5SDimitry Andric       // both defined and used in the same instruction.
1780b57cec5SDimitry Andric       for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1790b57cec5SDimitry Andric         const MachineOperand &MO = MI->getOperand(i);
1800b57cec5SDimitry Andric         if (MO.isReg() && MO.isUse()) {
1818bcb0991SDimitry Andric           Register Reg = MO.getReg();
1828bcb0991SDimitry Andric           if (Register::isPhysicalRegister(Reg)) {
1830b57cec5SDimitry Andric             for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
1840b57cec5SDimitry Andric               LivePhysRegs.set(*AI);
1850b57cec5SDimitry Andric           }
1860b57cec5SDimitry Andric         }
1870b57cec5SDimitry Andric       }
1880b57cec5SDimitry Andric     }
1890b57cec5SDimitry Andric   }
1900b57cec5SDimitry Andric 
1910b57cec5SDimitry Andric   LivePhysRegs.clear();
1920b57cec5SDimitry Andric   return AnyChanges;
1930b57cec5SDimitry Andric }
194