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