LCOV - code coverage report
Current view: top level - lib/CodeGen - DeadMachineInstructionElim.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 60 60 100.0 %
Date: 2017-09-14 15:23:50 Functions: 7 8 87.5 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- DeadMachineInstructionElim.cpp - Remove dead machine instructions --===//
       2             : //
       3             : //                     The LLVM Compiler Infrastructure
       4             : //
       5             : // This file is distributed under the University of Illinois Open Source
       6             : // License. See LICENSE.TXT for details.
       7             : //
       8             : //===----------------------------------------------------------------------===//
       9             : //
      10             : // This is an extremely simple MachineInstr-level dead-code-elimination pass.
      11             : //
      12             : //===----------------------------------------------------------------------===//
      13             : 
      14             : #include "llvm/ADT/Statistic.h"
      15             : #include "llvm/CodeGen/MachineFunctionPass.h"
      16             : #include "llvm/CodeGen/MachineRegisterInfo.h"
      17             : #include "llvm/CodeGen/Passes.h"
      18             : #include "llvm/Pass.h"
      19             : #include "llvm/Support/Debug.h"
      20             : #include "llvm/Support/raw_ostream.h"
      21             : #include "llvm/Target/TargetInstrInfo.h"
      22             : #include "llvm/Target/TargetSubtargetInfo.h"
      23             : 
      24             : using namespace llvm;
      25             : 
      26             : #define DEBUG_TYPE "dead-mi-elimination"
      27             : 
      28             : STATISTIC(NumDeletes,          "Number of dead instructions deleted");
      29             : 
      30             : namespace {
      31       69716 :   class DeadMachineInstructionElim : public MachineFunctionPass {
      32             :     bool runOnMachineFunction(MachineFunction &MF) override;
      33             : 
      34             :     const TargetRegisterInfo *TRI;
      35             :     const MachineRegisterInfo *MRI;
      36             :     const TargetInstrInfo *TII;
      37             :     BitVector LivePhysRegs;
      38             : 
      39             :   public:
      40             :     static char ID; // Pass identification, replacement for typeid
      41       70146 :     DeadMachineInstructionElim() : MachineFunctionPass(ID) {
      42       35073 :      initializeDeadMachineInstructionElimPass(*PassRegistry::getPassRegistry());
      43       35073 :     }
      44             : 
      45       34953 :     void getAnalysisUsage(AnalysisUsage &AU) const override {
      46       34953 :       AU.setPreservesCFG();
      47       34953 :       MachineFunctionPass::getAnalysisUsage(AU);
      48       34953 :     }
      49             : 
      50             :   private:
      51             :     bool isDead(const MachineInstr *MI) const;
      52             :   };
      53             : }
      54             : char DeadMachineInstructionElim::ID = 0;
      55             : char &llvm::DeadMachineInstructionElimID = DeadMachineInstructionElim::ID;
      56             : 
      57      256418 : INITIALIZE_PASS(DeadMachineInstructionElim, DEBUG_TYPE,
      58             :                 "Remove dead machine instructions", false, false)
      59             : 
      60     8784184 : bool DeadMachineInstructionElim::isDead(const MachineInstr *MI) const {
      61             :   // Technically speaking inline asm without side effects and no defs can still
      62             :   // be deleted. But there is so much bad inline asm code out there, we should
      63             :   // let them be.
      64     8784184 :   if (MI->isInlineAsm())
      65             :     return false;
      66             : 
      67             :   // Don't delete frame allocation labels.
      68    17541256 :   if (MI->getOpcode() == TargetOpcode::LOCAL_ESCAPE)
      69             :     return false;
      70             : 
      71             :   // Don't delete instructions with side effects.
      72     8770596 :   bool SawStore = false;
      73     8770596 :   if (!MI->isSafeToMove(nullptr, SawStore) && !MI->isPHI())
      74             :     return false;
      75             : 
      76             :   // Examine each operand.
      77     8715047 :   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
      78    17119394 :     const MachineOperand &MO = MI->getOperand(i);
      79    15249547 :     if (MO.isReg() && MO.isDef()) {
      80     6325573 :       unsigned Reg = MO.getReg();
      81     6325573 :       if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
      82             :         // Don't delete live physreg defs, or any reserved register defs.
      83     3628374 :         if (LivePhysRegs.test(Reg) || MRI->isReserved(Reg))
      84             :           return false;
      85             :       } else {
      86     9028342 :         if (!MRI->use_nodbg_empty(Reg))
      87             :           // This def has a non-debug use. Don't delete the instruction!
      88             :           return false;
      89             :       }
      90             :     }
      91             :   }
      92             : 
      93             :   // If there are no defs with uses, the instruction is dead.
      94             :   return true;
      95             : }
      96             : 
      97      306533 : bool DeadMachineInstructionElim::runOnMachineFunction(MachineFunction &MF) {
      98      306533 :   if (skipFunction(*MF.getFunction()))
      99             :     return false;
     100             : 
     101      306416 :   bool AnyChanges = false;
     102      306416 :   MRI = &MF.getRegInfo();
     103      306416 :   TRI = MF.getSubtarget().getRegisterInfo();
     104      306416 :   TII = MF.getSubtarget().getInstrInfo();
     105             : 
     106             :   // Loop over all instructions in all blocks, from bottom to top, so that it's
     107             :   // more likely that chains of dependent but ultimately dead instructions will
     108             :   // be cleaned up.
     109     1523702 :   for (MachineBasicBlock &MBB : make_range(MF.rbegin(), MF.rend())) {
     110             :     // Start out assuming that reserved registers are live out of this block.
     111     1208908 :     LivePhysRegs = MRI->getReservedRegs();
     112             : 
     113             :     // Add live-ins from successors to LivePhysRegs. Normally, physregs are not
     114             :     // live across blocks, but some targets (x86) can have flags live out of a
     115             :     // block.
     116     1019874 :     for (MachineBasicBlock::succ_iterator S = MBB.succ_begin(),
     117     1624328 :            E = MBB.succ_end(); S != E; S++)
     118     1352218 :       for (const auto &LI : (*S)->liveins())
     119      211916 :         LivePhysRegs.set(LI.PhysReg);
     120             : 
     121             :     // Now scan the instructions and delete dead ones, tracking physreg
     122             :     // liveness as we go.
     123      604454 :     for (MachineBasicBlock::reverse_iterator MII = MBB.rbegin(),
     124     9993092 :          MIE = MBB.rend(); MII != MIE; ) {
     125    26352552 :       MachineInstr *MI = &*MII++;
     126             : 
     127             :       // If the instruction is dead, delete it!
     128     8939534 :       if (isDead(MI)) {
     129             :         DEBUG(dbgs() << "DeadMachineInstructionElim: DELETING: " << *MI);
     130             :         // It is possible that some DBG_VALUE instructions refer to this
     131             :         // instruction.  They get marked as undef and will be deleted
     132             :         // in the live debug variable analysis.
     133      155350 :         MI->eraseFromParentAndMarkDBGValuesForRemoval();
     134      155350 :         AnyChanges = true;
     135      155350 :         ++NumDeletes;
     136      155350 :         continue;
     137             :       }
     138             : 
     139             :       // Record the physreg defs.
     140    43908234 :       for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
     141    70558800 :         const MachineOperand &MO = MI->getOperand(i);
     142    58405717 :         if (MO.isReg() && MO.isDef()) {
     143     8185337 :           unsigned Reg = MO.getReg();
     144     8185337 :           if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
     145             :             // Check the subreg set, not the alias set, because a def
     146             :             // of a super-register may still be partially live after
     147             :             // this def.
     148     3745669 :             for (MCSubRegIterator SR(Reg, TRI,/*IncludeSelf=*/true);
     149    12640995 :                  SR.isValid(); ++SR)
     150    26685978 :               LivePhysRegs.reset(*SR);
     151             :           }
     152    27094063 :         } else if (MO.isRegMask()) {
     153             :           // Register mask of preserved registers. All clobbers are dead.
     154      375783 :           LivePhysRegs.clearBitsNotInMask(MO.getRegMask());
     155             :         }
     156             :       }
     157             :       // Record the physreg uses, after the defs, in case a physreg is
     158             :       // both defined and used in the same instruction.
     159    43908234 :       for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
     160    70558800 :         const MachineOperand &MO = MI->getOperand(i);
     161    58405717 :         if (MO.isReg() && MO.isUse()) {
     162    14940980 :           unsigned Reg = MO.getReg();
     163    14940980 :           if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
     164    47004538 :             for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
     165    57186792 :               LivePhysRegs.set(*AI);
     166             :           }
     167             :         }
     168             :       }
     169             :     }
     170             :   }
     171             : 
     172      306416 :   LivePhysRegs.clear();
     173      306416 :   return AnyChanges;
     174             : }

Generated by: LCOV version 1.13