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 130b57cec5SDimitry Andric #include "llvm/ADT/Statistic.h" 140b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h" 150b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h" 160b57cec5SDimitry Andric #include "llvm/CodeGen/Passes.h" 170b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h" 18*480093f4SDimitry Andric #include "llvm/InitializePasses.h" 190b57cec5SDimitry Andric #include "llvm/Pass.h" 200b57cec5SDimitry Andric #include "llvm/Support/Debug.h" 210b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 220b57cec5SDimitry Andric 230b57cec5SDimitry Andric using namespace llvm; 240b57cec5SDimitry Andric 250b57cec5SDimitry Andric #define DEBUG_TYPE "dead-mi-elimination" 260b57cec5SDimitry Andric 270b57cec5SDimitry Andric STATISTIC(NumDeletes, "Number of dead instructions deleted"); 280b57cec5SDimitry Andric 290b57cec5SDimitry Andric namespace { 300b57cec5SDimitry Andric class DeadMachineInstructionElim : public MachineFunctionPass { 310b57cec5SDimitry Andric bool runOnMachineFunction(MachineFunction &MF) override; 320b57cec5SDimitry Andric 330b57cec5SDimitry Andric const TargetRegisterInfo *TRI; 340b57cec5SDimitry Andric const MachineRegisterInfo *MRI; 350b57cec5SDimitry Andric const TargetInstrInfo *TII; 360b57cec5SDimitry Andric BitVector LivePhysRegs; 370b57cec5SDimitry Andric 380b57cec5SDimitry Andric public: 390b57cec5SDimitry Andric static char ID; // Pass identification, replacement for typeid 400b57cec5SDimitry Andric DeadMachineInstructionElim() : MachineFunctionPass(ID) { 410b57cec5SDimitry Andric initializeDeadMachineInstructionElimPass(*PassRegistry::getPassRegistry()); 420b57cec5SDimitry Andric } 430b57cec5SDimitry Andric 440b57cec5SDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override { 450b57cec5SDimitry Andric AU.setPreservesCFG(); 460b57cec5SDimitry Andric MachineFunctionPass::getAnalysisUsage(AU); 470b57cec5SDimitry Andric } 480b57cec5SDimitry Andric 490b57cec5SDimitry Andric private: 500b57cec5SDimitry Andric bool isDead(const MachineInstr *MI) const; 510b57cec5SDimitry Andric }; 520b57cec5SDimitry Andric } 530b57cec5SDimitry Andric char DeadMachineInstructionElim::ID = 0; 540b57cec5SDimitry Andric char &llvm::DeadMachineInstructionElimID = DeadMachineInstructionElim::ID; 550b57cec5SDimitry Andric 560b57cec5SDimitry Andric INITIALIZE_PASS(DeadMachineInstructionElim, DEBUG_TYPE, 570b57cec5SDimitry Andric "Remove dead machine instructions", false, false) 580b57cec5SDimitry Andric 590b57cec5SDimitry Andric bool DeadMachineInstructionElim::isDead(const MachineInstr *MI) const { 600b57cec5SDimitry Andric // Technically speaking inline asm without side effects and no defs can still 610b57cec5SDimitry Andric // be deleted. But there is so much bad inline asm code out there, we should 620b57cec5SDimitry Andric // let them be. 630b57cec5SDimitry Andric if (MI->isInlineAsm()) 640b57cec5SDimitry Andric return false; 650b57cec5SDimitry Andric 660b57cec5SDimitry Andric // Don't delete frame allocation labels. 670b57cec5SDimitry Andric if (MI->getOpcode() == TargetOpcode::LOCAL_ESCAPE) 680b57cec5SDimitry Andric return false; 690b57cec5SDimitry Andric 700b57cec5SDimitry Andric // Don't delete instructions with side effects. 710b57cec5SDimitry Andric bool SawStore = false; 720b57cec5SDimitry Andric if (!MI->isSafeToMove(nullptr, SawStore) && !MI->isPHI()) 730b57cec5SDimitry Andric return false; 740b57cec5SDimitry Andric 750b57cec5SDimitry Andric // Examine each operand. 760b57cec5SDimitry Andric for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 770b57cec5SDimitry Andric const MachineOperand &MO = MI->getOperand(i); 780b57cec5SDimitry Andric if (MO.isReg() && MO.isDef()) { 798bcb0991SDimitry Andric Register Reg = MO.getReg(); 808bcb0991SDimitry Andric if (Register::isPhysicalRegister(Reg)) { 810b57cec5SDimitry Andric // Don't delete live physreg defs, or any reserved register defs. 820b57cec5SDimitry Andric if (LivePhysRegs.test(Reg) || MRI->isReserved(Reg)) 830b57cec5SDimitry Andric return false; 840b57cec5SDimitry Andric } else { 85*480093f4SDimitry Andric if (MO.isDead()) { 86*480093f4SDimitry Andric #ifndef NDEBUG 87*480093f4SDimitry Andric // Sanity check on uses of this dead register. All of them should be 88*480093f4SDimitry Andric // 'undef'. 89*480093f4SDimitry Andric for (auto &U : MRI->use_nodbg_operands(Reg)) 90*480093f4SDimitry Andric assert(U.isUndef() && "'Undef' use on a 'dead' register is found!"); 91*480093f4SDimitry Andric #endif 92*480093f4SDimitry Andric continue; 93*480093f4SDimitry Andric } 940b57cec5SDimitry Andric for (const MachineInstr &Use : MRI->use_nodbg_instructions(Reg)) { 950b57cec5SDimitry Andric if (&Use != MI) 960b57cec5SDimitry Andric // This def has a non-debug use. Don't delete the instruction! 970b57cec5SDimitry Andric return false; 980b57cec5SDimitry Andric } 990b57cec5SDimitry Andric } 1000b57cec5SDimitry Andric } 1010b57cec5SDimitry Andric } 1020b57cec5SDimitry Andric 1030b57cec5SDimitry Andric // If there are no defs with uses, the instruction is dead. 1040b57cec5SDimitry Andric return true; 1050b57cec5SDimitry Andric } 1060b57cec5SDimitry Andric 1070b57cec5SDimitry Andric bool DeadMachineInstructionElim::runOnMachineFunction(MachineFunction &MF) { 1080b57cec5SDimitry Andric if (skipFunction(MF.getFunction())) 1090b57cec5SDimitry Andric return false; 1100b57cec5SDimitry Andric 1110b57cec5SDimitry Andric bool AnyChanges = false; 1120b57cec5SDimitry Andric MRI = &MF.getRegInfo(); 1130b57cec5SDimitry Andric TRI = MF.getSubtarget().getRegisterInfo(); 1140b57cec5SDimitry Andric TII = MF.getSubtarget().getInstrInfo(); 1150b57cec5SDimitry Andric 1160b57cec5SDimitry Andric // Loop over all instructions in all blocks, from bottom to top, so that it's 1170b57cec5SDimitry Andric // more likely that chains of dependent but ultimately dead instructions will 1180b57cec5SDimitry Andric // be cleaned up. 1190b57cec5SDimitry Andric for (MachineBasicBlock &MBB : make_range(MF.rbegin(), MF.rend())) { 1200b57cec5SDimitry Andric // Start out assuming that reserved registers are live out of this block. 1210b57cec5SDimitry Andric LivePhysRegs = MRI->getReservedRegs(); 1220b57cec5SDimitry Andric 1230b57cec5SDimitry Andric // Add live-ins from successors to LivePhysRegs. Normally, physregs are not 1240b57cec5SDimitry Andric // live across blocks, but some targets (x86) can have flags live out of a 1250b57cec5SDimitry Andric // block. 1260b57cec5SDimitry Andric for (MachineBasicBlock::succ_iterator S = MBB.succ_begin(), 1270b57cec5SDimitry Andric E = MBB.succ_end(); S != E; S++) 1280b57cec5SDimitry Andric for (const auto &LI : (*S)->liveins()) 1290b57cec5SDimitry Andric LivePhysRegs.set(LI.PhysReg); 1300b57cec5SDimitry Andric 1310b57cec5SDimitry Andric // Now scan the instructions and delete dead ones, tracking physreg 1320b57cec5SDimitry Andric // liveness as we go. 1330b57cec5SDimitry Andric for (MachineBasicBlock::reverse_iterator MII = MBB.rbegin(), 1340b57cec5SDimitry Andric MIE = MBB.rend(); MII != MIE; ) { 1350b57cec5SDimitry Andric MachineInstr *MI = &*MII++; 1360b57cec5SDimitry Andric 1370b57cec5SDimitry Andric // If the instruction is dead, delete it! 1380b57cec5SDimitry Andric if (isDead(MI)) { 1390b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "DeadMachineInstructionElim: DELETING: " << *MI); 1400b57cec5SDimitry Andric // It is possible that some DBG_VALUE instructions refer to this 1410b57cec5SDimitry Andric // instruction. They get marked as undef and will be deleted 1420b57cec5SDimitry Andric // in the live debug variable analysis. 1430b57cec5SDimitry Andric MI->eraseFromParentAndMarkDBGValuesForRemoval(); 1440b57cec5SDimitry Andric AnyChanges = true; 1450b57cec5SDimitry Andric ++NumDeletes; 1460b57cec5SDimitry Andric continue; 1470b57cec5SDimitry Andric } 1480b57cec5SDimitry Andric 1490b57cec5SDimitry Andric // Record the physreg defs. 1500b57cec5SDimitry Andric for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 1510b57cec5SDimitry Andric const MachineOperand &MO = MI->getOperand(i); 1520b57cec5SDimitry Andric if (MO.isReg() && MO.isDef()) { 1538bcb0991SDimitry Andric Register Reg = MO.getReg(); 1548bcb0991SDimitry Andric if (Register::isPhysicalRegister(Reg)) { 1550b57cec5SDimitry Andric // Check the subreg set, not the alias set, because a def 1560b57cec5SDimitry Andric // of a super-register may still be partially live after 1570b57cec5SDimitry Andric // this def. 1580b57cec5SDimitry Andric for (MCSubRegIterator SR(Reg, TRI,/*IncludeSelf=*/true); 1590b57cec5SDimitry Andric SR.isValid(); ++SR) 1600b57cec5SDimitry Andric LivePhysRegs.reset(*SR); 1610b57cec5SDimitry Andric } 1620b57cec5SDimitry Andric } else if (MO.isRegMask()) { 1630b57cec5SDimitry Andric // Register mask of preserved registers. All clobbers are dead. 1640b57cec5SDimitry Andric LivePhysRegs.clearBitsNotInMask(MO.getRegMask()); 1650b57cec5SDimitry Andric } 1660b57cec5SDimitry Andric } 1670b57cec5SDimitry Andric // Record the physreg uses, after the defs, in case a physreg is 1680b57cec5SDimitry Andric // both defined and used in the same instruction. 1690b57cec5SDimitry Andric for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 1700b57cec5SDimitry Andric const MachineOperand &MO = MI->getOperand(i); 1710b57cec5SDimitry Andric if (MO.isReg() && MO.isUse()) { 1728bcb0991SDimitry Andric Register Reg = MO.getReg(); 1738bcb0991SDimitry Andric if (Register::isPhysicalRegister(Reg)) { 1740b57cec5SDimitry Andric for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) 1750b57cec5SDimitry Andric LivePhysRegs.set(*AI); 1760b57cec5SDimitry Andric } 1770b57cec5SDimitry Andric } 1780b57cec5SDimitry Andric } 1790b57cec5SDimitry Andric } 1800b57cec5SDimitry Andric } 1810b57cec5SDimitry Andric 1820b57cec5SDimitry Andric LivePhysRegs.clear(); 1830b57cec5SDimitry Andric return AnyChanges; 1840b57cec5SDimitry Andric } 185