1 //===- DeadMachineInstructionElim.cpp - Remove dead machine instructions --===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This is an extremely simple MachineInstr-level dead-code-elimination pass. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "llvm/CodeGen/DeadMachineInstructionElim.h" 14 #include "llvm/ADT/PostOrderIterator.h" 15 #include "llvm/ADT/Statistic.h" 16 #include "llvm/CodeGen/LiveRegUnits.h" 17 #include "llvm/CodeGen/MachineFunctionPass.h" 18 #include "llvm/CodeGen/MachineRegisterInfo.h" 19 #include "llvm/CodeGen/TargetSubtargetInfo.h" 20 #include "llvm/InitializePasses.h" 21 #include "llvm/Pass.h" 22 #include "llvm/Support/Debug.h" 23 #include "llvm/Support/raw_ostream.h" 24 25 using namespace llvm; 26 27 #define DEBUG_TYPE "dead-mi-elimination" 28 29 STATISTIC(NumDeletes, "Number of dead instructions deleted"); 30 31 namespace { 32 class DeadMachineInstructionElimImpl { 33 const MachineRegisterInfo *MRI = nullptr; 34 const TargetInstrInfo *TII = nullptr; 35 LiveRegUnits LivePhysRegs; 36 37 public: 38 bool runImpl(MachineFunction &MF); 39 40 private: 41 bool isDead(const MachineInstr *MI) const; 42 bool eliminateDeadMI(MachineFunction &MF); 43 }; 44 45 class DeadMachineInstructionElim : public MachineFunctionPass { 46 public: 47 static char ID; // Pass identification, replacement for typeid 48 49 DeadMachineInstructionElim() : MachineFunctionPass(ID) { 50 initializeDeadMachineInstructionElimPass(*PassRegistry::getPassRegistry()); 51 } 52 53 bool runOnMachineFunction(MachineFunction &MF) override { 54 if (skipFunction(MF.getFunction())) 55 return false; 56 return DeadMachineInstructionElimImpl().runImpl(MF); 57 } 58 59 void getAnalysisUsage(AnalysisUsage &AU) const override { 60 AU.setPreservesCFG(); 61 MachineFunctionPass::getAnalysisUsage(AU); 62 } 63 }; 64 } // namespace 65 66 PreservedAnalyses 67 DeadMachineInstructionElimPass::run(MachineFunction &MF, 68 MachineFunctionAnalysisManager &) { 69 if (!DeadMachineInstructionElimImpl().runImpl(MF)) 70 return PreservedAnalyses::all(); 71 PreservedAnalyses PA = getMachineFunctionPassPreservedAnalyses(); 72 PA.preserveSet<CFGAnalyses>(); 73 return PA; 74 } 75 76 char DeadMachineInstructionElim::ID = 0; 77 char &llvm::DeadMachineInstructionElimID = DeadMachineInstructionElim::ID; 78 79 INITIALIZE_PASS(DeadMachineInstructionElim, DEBUG_TYPE, 80 "Remove dead machine instructions", false, false) 81 82 bool DeadMachineInstructionElimImpl::isDead(const MachineInstr *MI) const { 83 // Technically speaking inline asm without side effects and no defs can still 84 // be deleted. But there is so much bad inline asm code out there, we should 85 // let them be. 86 if (MI->isInlineAsm()) 87 return false; 88 89 // Don't delete frame allocation labels. 90 if (MI->getOpcode() == TargetOpcode::LOCAL_ESCAPE || 91 MI->getOpcode() == TargetOpcode::FAKE_USE) 92 return false; 93 94 // Don't delete instructions with side effects. 95 bool SawStore = false; 96 if (!MI->isSafeToMove(SawStore) && !MI->isPHI()) 97 return false; 98 99 // Examine each operand. 100 for (const MachineOperand &MO : MI->all_defs()) { 101 Register Reg = MO.getReg(); 102 if (Reg.isPhysical()) { 103 // Don't delete live physreg defs, or any reserved register defs. 104 if (!LivePhysRegs.available(Reg) || MRI->isReserved(Reg)) 105 return false; 106 } else { 107 if (MO.isDead()) { 108 #ifndef NDEBUG 109 // Basic check on the register. All of them should be 'undef'. 110 for (auto &U : MRI->use_nodbg_operands(Reg)) 111 assert(U.isUndef() && "'Undef' use on a 'dead' register is found!"); 112 #endif 113 continue; 114 } 115 for (const MachineInstr &Use : MRI->use_nodbg_instructions(Reg)) { 116 if (&Use != MI) 117 // This def has a non-debug use. Don't delete the instruction! 118 return false; 119 } 120 } 121 } 122 123 // If there are no defs with uses, the instruction is dead. 124 return true; 125 } 126 127 bool DeadMachineInstructionElimImpl::runImpl(MachineFunction &MF) { 128 MRI = &MF.getRegInfo(); 129 130 const TargetSubtargetInfo &ST = MF.getSubtarget(); 131 TII = ST.getInstrInfo(); 132 LivePhysRegs.init(*ST.getRegisterInfo()); 133 134 bool AnyChanges = eliminateDeadMI(MF); 135 while (AnyChanges && eliminateDeadMI(MF)) 136 ; 137 return AnyChanges; 138 } 139 140 bool DeadMachineInstructionElimImpl::eliminateDeadMI(MachineFunction &MF) { 141 bool AnyChanges = false; 142 143 // Loop over all instructions in all blocks, from bottom to top, so that it's 144 // more likely that chains of dependent but ultimately dead instructions will 145 // be cleaned up. 146 for (MachineBasicBlock *MBB : post_order(&MF)) { 147 LivePhysRegs.addLiveOuts(*MBB); 148 149 // Now scan the instructions and delete dead ones, tracking physreg 150 // liveness as we go. 151 for (MachineInstr &MI : make_early_inc_range(reverse(*MBB))) { 152 // If the instruction is dead, delete it! 153 if (isDead(&MI)) { 154 LLVM_DEBUG(dbgs() << "DeadMachineInstructionElim: DELETING: " << MI); 155 // It is possible that some DBG_VALUE instructions refer to this 156 // instruction. They will be deleted in the live debug variable 157 // analysis. 158 MI.eraseFromParent(); 159 AnyChanges = true; 160 ++NumDeletes; 161 continue; 162 } 163 164 LivePhysRegs.stepBackward(MI); 165 } 166 } 167 168 LivePhysRegs.clear(); 169 return AnyChanges; 170 } 171