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. 794824e7fdSDimitry Andric for (const MachineOperand &MO : MI->operands()) { 800b57cec5SDimitry Andric if (MO.isReg() && MO.isDef()) { 818bcb0991SDimitry Andric Register Reg = MO.getReg(); 828bcb0991SDimitry Andric if (Register::isPhysicalRegister(Reg)) { 830b57cec5SDimitry Andric // Don't delete live physreg defs, or any reserved register defs. 840b57cec5SDimitry Andric if (LivePhysRegs.test(Reg) || MRI->isReserved(Reg)) 850b57cec5SDimitry Andric return false; 860b57cec5SDimitry Andric } else { 87480093f4SDimitry Andric if (MO.isDead()) { 88480093f4SDimitry Andric #ifndef NDEBUG 894824e7fdSDimitry Andric // Baisc check on the register. All of them should be 90480093f4SDimitry Andric // 'undef'. 91480093f4SDimitry Andric for (auto &U : MRI->use_nodbg_operands(Reg)) 92480093f4SDimitry Andric assert(U.isUndef() && "'Undef' use on a 'dead' register is found!"); 93480093f4SDimitry Andric #endif 94480093f4SDimitry Andric continue; 95480093f4SDimitry Andric } 960b57cec5SDimitry Andric for (const MachineInstr &Use : MRI->use_nodbg_instructions(Reg)) { 970b57cec5SDimitry Andric if (&Use != MI) 980b57cec5SDimitry Andric // This def has a non-debug use. Don't delete the instruction! 990b57cec5SDimitry Andric return false; 1000b57cec5SDimitry Andric } 1010b57cec5SDimitry Andric } 1020b57cec5SDimitry Andric } 1030b57cec5SDimitry Andric } 1040b57cec5SDimitry Andric 1050b57cec5SDimitry Andric // If there are no defs with uses, the instruction is dead. 1060b57cec5SDimitry Andric return true; 1070b57cec5SDimitry Andric } 1080b57cec5SDimitry Andric 1090b57cec5SDimitry Andric bool DeadMachineInstructionElim::runOnMachineFunction(MachineFunction &MF) { 1100b57cec5SDimitry Andric if (skipFunction(MF.getFunction())) 1110b57cec5SDimitry Andric return false; 112e8d8bef9SDimitry Andric bool AnyChanges = eliminateDeadMI(MF); 113e8d8bef9SDimitry Andric while (AnyChanges && eliminateDeadMI(MF)) 114e8d8bef9SDimitry Andric ; 115e8d8bef9SDimitry Andric return AnyChanges; 116e8d8bef9SDimitry Andric } 1170b57cec5SDimitry Andric 118e8d8bef9SDimitry Andric bool DeadMachineInstructionElim::eliminateDeadMI(MachineFunction &MF) { 1190b57cec5SDimitry Andric bool AnyChanges = false; 1200b57cec5SDimitry Andric MRI = &MF.getRegInfo(); 1210b57cec5SDimitry Andric TRI = MF.getSubtarget().getRegisterInfo(); 1220b57cec5SDimitry Andric TII = MF.getSubtarget().getInstrInfo(); 1230b57cec5SDimitry Andric 1240b57cec5SDimitry Andric // Loop over all instructions in all blocks, from bottom to top, so that it's 1250b57cec5SDimitry Andric // more likely that chains of dependent but ultimately dead instructions will 1260b57cec5SDimitry Andric // be cleaned up. 127e8d8bef9SDimitry Andric for (MachineBasicBlock *MBB : post_order(&MF)) { 1280b57cec5SDimitry Andric // Start out assuming that reserved registers are live out of this block. 1290b57cec5SDimitry Andric LivePhysRegs = MRI->getReservedRegs(); 1300b57cec5SDimitry Andric 1310b57cec5SDimitry Andric // Add live-ins from successors to LivePhysRegs. Normally, physregs are not 1320b57cec5SDimitry Andric // live across blocks, but some targets (x86) can have flags live out of a 1330b57cec5SDimitry Andric // block. 134fe6060f1SDimitry Andric for (const MachineBasicBlock *Succ : MBB->successors()) 135fe6060f1SDimitry Andric for (const auto &LI : Succ->liveins()) 1360b57cec5SDimitry Andric LivePhysRegs.set(LI.PhysReg); 1370b57cec5SDimitry Andric 1380b57cec5SDimitry Andric // Now scan the instructions and delete dead ones, tracking physreg 1390b57cec5SDimitry Andric // liveness as we go. 140349cc55cSDimitry Andric for (MachineInstr &MI : llvm::make_early_inc_range(llvm::reverse(*MBB))) { 1410b57cec5SDimitry Andric // If the instruction is dead, delete it! 142349cc55cSDimitry Andric if (isDead(&MI)) { 143349cc55cSDimitry Andric LLVM_DEBUG(dbgs() << "DeadMachineInstructionElim: DELETING: " << MI); 1440b57cec5SDimitry Andric // It is possible that some DBG_VALUE instructions refer to this 145*0eae32dcSDimitry Andric // instruction. They will be deleted in the live debug variable 146*0eae32dcSDimitry Andric // analysis. 147*0eae32dcSDimitry Andric MI.eraseFromParent(); 1480b57cec5SDimitry Andric AnyChanges = true; 1490b57cec5SDimitry Andric ++NumDeletes; 1500b57cec5SDimitry Andric continue; 1510b57cec5SDimitry Andric } 1520b57cec5SDimitry Andric 1530b57cec5SDimitry Andric // Record the physreg defs. 1544824e7fdSDimitry Andric for (const MachineOperand &MO : MI.operands()) { 1550b57cec5SDimitry Andric if (MO.isReg() && MO.isDef()) { 1568bcb0991SDimitry Andric Register Reg = MO.getReg(); 1578bcb0991SDimitry Andric if (Register::isPhysicalRegister(Reg)) { 1580b57cec5SDimitry Andric // Check the subreg set, not the alias set, because a def 1590b57cec5SDimitry Andric // of a super-register may still be partially live after 1600b57cec5SDimitry Andric // this def. 1610b57cec5SDimitry Andric for (MCSubRegIterator SR(Reg, TRI,/*IncludeSelf=*/true); 1620b57cec5SDimitry Andric SR.isValid(); ++SR) 1630b57cec5SDimitry Andric LivePhysRegs.reset(*SR); 1640b57cec5SDimitry Andric } 1650b57cec5SDimitry Andric } else if (MO.isRegMask()) { 1660b57cec5SDimitry Andric // Register mask of preserved registers. All clobbers are dead. 1670b57cec5SDimitry Andric LivePhysRegs.clearBitsNotInMask(MO.getRegMask()); 1680b57cec5SDimitry Andric } 1690b57cec5SDimitry Andric } 1700b57cec5SDimitry Andric // Record the physreg uses, after the defs, in case a physreg is 1710b57cec5SDimitry Andric // both defined and used in the same instruction. 1724824e7fdSDimitry Andric for (const MachineOperand &MO : MI.operands()) { 1730b57cec5SDimitry Andric if (MO.isReg() && MO.isUse()) { 1748bcb0991SDimitry Andric Register Reg = MO.getReg(); 1758bcb0991SDimitry Andric if (Register::isPhysicalRegister(Reg)) { 1760b57cec5SDimitry Andric for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) 1770b57cec5SDimitry Andric LivePhysRegs.set(*AI); 1780b57cec5SDimitry Andric } 1790b57cec5SDimitry Andric } 1800b57cec5SDimitry Andric } 1810b57cec5SDimitry Andric } 1820b57cec5SDimitry Andric } 1830b57cec5SDimitry Andric 1840b57cec5SDimitry Andric LivePhysRegs.clear(); 1850b57cec5SDimitry Andric return AnyChanges; 1860b57cec5SDimitry Andric } 187