LCOV - code coverage report
Current view: top level - lib/CodeGen - ExpandPostRAPseudos.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 62 66 93.9 %
Date: 2018-06-17 00:07:59 Functions: 8 9 88.9 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===-- ExpandPostRAPseudos.cpp - Pseudo instruction expansion pass -------===//
       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 file defines a pass that expands COPY and SUBREG_TO_REG pseudo
      11             : // instructions after register allocation.
      12             : //
      13             : //===----------------------------------------------------------------------===//
      14             : 
      15             : #include "llvm/CodeGen/MachineFunctionPass.h"
      16             : #include "llvm/CodeGen/MachineInstr.h"
      17             : #include "llvm/CodeGen/MachineInstrBuilder.h"
      18             : #include "llvm/CodeGen/MachineRegisterInfo.h"
      19             : #include "llvm/CodeGen/Passes.h"
      20             : #include "llvm/CodeGen/TargetInstrInfo.h"
      21             : #include "llvm/CodeGen/TargetRegisterInfo.h"
      22             : #include "llvm/CodeGen/TargetSubtargetInfo.h"
      23             : #include "llvm/Support/Debug.h"
      24             : #include "llvm/Support/raw_ostream.h"
      25             : 
      26             : using namespace llvm;
      27             : 
      28             : #define DEBUG_TYPE "postrapseudos"
      29             : 
      30             : namespace {
      31       22686 : struct ExpandPostRA : public MachineFunctionPass {
      32             : private:
      33             :   const TargetRegisterInfo *TRI;
      34             :   const TargetInstrInfo *TII;
      35             : 
      36             : public:
      37             :   static char ID; // Pass identification, replacement for typeid
      38       22794 :   ExpandPostRA() : MachineFunctionPass(ID) {}
      39             : 
      40       22626 :   void getAnalysisUsage(AnalysisUsage &AU) const override {
      41       22626 :     AU.setPreservesCFG();
      42       22626 :     AU.addPreservedID(MachineLoopInfoID);
      43       22626 :     AU.addPreservedID(MachineDominatorsID);
      44       22626 :     MachineFunctionPass::getAnalysisUsage(AU);
      45       22626 :   }
      46             : 
      47             :   /// runOnMachineFunction - pass entry point
      48             :   bool runOnMachineFunction(MachineFunction&) override;
      49             : 
      50             : private:
      51             :   bool LowerSubregToReg(MachineInstr *MI);
      52             :   bool LowerCopy(MachineInstr *MI);
      53             : 
      54             :   void TransferImplicitOperands(MachineInstr *MI);
      55             : };
      56             : } // end anonymous namespace
      57             : 
      58             : char ExpandPostRA::ID = 0;
      59             : char &llvm::ExpandPostRAPseudosID = ExpandPostRA::ID;
      60             : 
      61      148112 : INITIALIZE_PASS(ExpandPostRA, DEBUG_TYPE,
      62             :                 "Post-RA pseudo instruction expansion pass", false, false)
      63             : 
      64             : /// TransferImplicitOperands - MI is a pseudo-instruction, and the lowered
      65             : /// replacement instructions immediately precede it.  Copy any implicit
      66             : /// operands from MI to the replacement instruction.
      67       44050 : void ExpandPostRA::TransferImplicitOperands(MachineInstr *MI) {
      68             :   MachineBasicBlock::iterator CopyMI = MI;
      69             :   --CopyMI;
      70             : 
      71      133436 :   for (const MachineOperand &MO : MI->implicit_operands())
      72       44693 :     if (MO.isReg())
      73       44693 :       CopyMI->addOperand(MO);
      74       44050 : }
      75             : 
      76       11866 : bool ExpandPostRA::LowerSubregToReg(MachineInstr *MI) {
      77       11866 :   MachineBasicBlock *MBB = MI->getParent();
      78             :   assert((MI->getOperand(0).isReg() && MI->getOperand(0).isDef()) &&
      79             :          MI->getOperand(1).isImm() &&
      80             :          (MI->getOperand(2).isReg() && MI->getOperand(2).isUse()) &&
      81             :           MI->getOperand(3).isImm() && "Invalid subreg_to_reg");
      82             : 
      83       11866 :   unsigned DstReg  = MI->getOperand(0).getReg();
      84       11866 :   unsigned InsReg  = MI->getOperand(2).getReg();
      85             :   assert(!MI->getOperand(2).getSubReg() && "SubIdx on physreg?");
      86       11866 :   unsigned SubIdx  = MI->getOperand(3).getImm();
      87             : 
      88             :   assert(SubIdx != 0 && "Invalid index for insert_subreg");
      89       11866 :   unsigned DstSubReg = TRI->getSubReg(DstReg, SubIdx);
      90             : 
      91             :   assert(TargetRegisterInfo::isPhysicalRegister(DstReg) &&
      92             :          "Insert destination must be in a physical register");
      93             :   assert(TargetRegisterInfo::isPhysicalRegister(InsReg) &&
      94             :          "Inserted value must be in a physical register");
      95             : 
      96             :   LLVM_DEBUG(dbgs() << "subreg: CONVERTING: " << *MI);
      97             : 
      98       11866 :   if (MI->allDefsAreDead()) {
      99           0 :     MI->setDesc(TII->get(TargetOpcode::KILL));
     100             :     LLVM_DEBUG(dbgs() << "subreg: replaced by: " << *MI);
     101             :     return true;
     102             :   }
     103             : 
     104       11866 :   if (DstSubReg == InsReg) {
     105             :     // No need to insert an identity copy instruction.
     106             :     // Watch out for case like this:
     107             :     // %rax = SUBREG_TO_REG 0, killed %eax, 3
     108             :     // We must leave %rax live.
     109         745 :     if (DstReg != InsReg) {
     110         745 :       MI->setDesc(TII->get(TargetOpcode::KILL));
     111         745 :       MI->RemoveOperand(3);     // SubIdx
     112         745 :       MI->RemoveOperand(1);     // Imm
     113             :       LLVM_DEBUG(dbgs() << "subreg: replace by: " << *MI);
     114             :       return true;
     115             :     }
     116             :     LLVM_DEBUG(dbgs() << "subreg: eliminated!");
     117             :   } else {
     118       33363 :     TII->copyPhysReg(*MBB, MI, MI->getDebugLoc(), DstSubReg, InsReg,
     119       11121 :                      MI->getOperand(2).isKill());
     120             : 
     121             :     // Implicitly define DstReg for subsequent uses.
     122             :     MachineBasicBlock::iterator CopyMI = MI;
     123             :     --CopyMI;
     124       11121 :     CopyMI->addRegisterDefined(DstReg);
     125             :     LLVM_DEBUG(dbgs() << "subreg: " << *CopyMI);
     126             :   }
     127             : 
     128             :   LLVM_DEBUG(dbgs() << '\n');
     129             :   MBB->erase(MI);
     130             :   return true;
     131             : }
     132             : 
     133      225255 : bool ExpandPostRA::LowerCopy(MachineInstr *MI) {
     134             : 
     135      225255 :   if (MI->allDefsAreDead()) {
     136             :     LLVM_DEBUG(dbgs() << "dead copy: " << *MI);
     137         252 :     MI->setDesc(TII->get(TargetOpcode::KILL));
     138             :     LLVM_DEBUG(dbgs() << "replaced by: " << *MI);
     139             :     return true;
     140             :   }
     141             : 
     142      225003 :   MachineOperand &DstMO = MI->getOperand(0);
     143             :   MachineOperand &SrcMO = MI->getOperand(1);
     144             : 
     145      225003 :   bool IdentityCopy = (SrcMO.getReg() == DstMO.getReg());
     146      450001 :   if (IdentityCopy || SrcMO.isUndef()) {
     147             :     LLVM_DEBUG(dbgs() << (IdentityCopy ? "identity copy: " : "undef copy:    ")
     148             :                       << *MI);
     149             :     // No need to insert an identity copy instruction, but replace with a KILL
     150             :     // if liveness is changed.
     151          11 :     if (SrcMO.isUndef() || MI->getNumOperands() > 2) {
     152             :       // We must make sure the super-register gets killed. Replace the
     153             :       // instruction with KILL.
     154           6 :       MI->setDesc(TII->get(TargetOpcode::KILL));
     155             :       LLVM_DEBUG(dbgs() << "replaced by:   " << *MI);
     156             :       return true;
     157             :     }
     158             :     // Vanilla identity copy.
     159           0 :     MI->eraseFromParent();
     160             :     return true;
     161             :   }
     162             : 
     163             :   LLVM_DEBUG(dbgs() << "real copy:   " << *MI);
     164      674991 :   TII->copyPhysReg(*MI->getParent(), MI, MI->getDebugLoc(),
     165      224997 :                    DstMO.getReg(), SrcMO.getReg(), SrcMO.isKill());
     166             : 
     167      224997 :   if (MI->getNumOperands() > 2)
     168       44050 :     TransferImplicitOperands(MI);
     169             :   LLVM_DEBUG({
     170             :     MachineBasicBlock::iterator dMI = MI;
     171             :     dbgs() << "replaced by: " << *(--dMI);
     172             :   });
     173      224997 :   MI->eraseFromParent();
     174             :   return true;
     175             : }
     176             : 
     177             : /// runOnMachineFunction - Reduce subregister inserts and extracts to register
     178             : /// copies.
     179             : ///
     180      227105 : bool ExpandPostRA::runOnMachineFunction(MachineFunction &MF) {
     181             :   LLVM_DEBUG(dbgs() << "Machine Function\n"
     182             :                     << "********** EXPANDING POST-RA PSEUDO INSTRS **********\n"
     183             :                     << "********** Function: " << MF.getName() << '\n');
     184      227105 :   TRI = MF.getSubtarget().getRegisterInfo();
     185      227105 :   TII = MF.getSubtarget().getInstrInfo();
     186             : 
     187             :   bool MadeChange = false;
     188             : 
     189             :   for (MachineFunction::iterator mbbi = MF.begin(), mbbe = MF.end();
     190      663234 :        mbbi != mbbe; ++mbbi) {
     191      436129 :     for (MachineBasicBlock::iterator mi = mbbi->begin(), me = mbbi->end();
     192     4361541 :          mi != me;) {
     193             :       MachineInstr &MI = *mi;
     194             :       // Advance iterator here because MI may be erased.
     195             :       ++mi;
     196             : 
     197             :       // Only expand pseudos.
     198     3925412 :       if (!MI.isPseudo())
     199     2840433 :         continue;
     200             : 
     201             :       // Give targets a chance to expand even standard pseudos.
     202     1084979 :       if (TII->expandPostRAPseudo(MI)) {
     203             :         MadeChange = true;
     204      120591 :         continue;
     205             :       }
     206             : 
     207             :       // Expand standard pseudos.
     208     1957604 :       switch (MI.getOpcode()) {
     209       11866 :       case TargetOpcode::SUBREG_TO_REG:
     210       11866 :         MadeChange |= LowerSubregToReg(&MI);
     211       11866 :         break;
     212      225255 :       case TargetOpcode::COPY:
     213      225255 :         MadeChange |= LowerCopy(&MI);
     214      225255 :         break;
     215       28828 :       case TargetOpcode::DBG_VALUE:
     216       28828 :         continue;
     217           0 :       case TargetOpcode::INSERT_SUBREG:
     218             :       case TargetOpcode::EXTRACT_SUBREG:
     219           0 :         llvm_unreachable("Sub-register pseudos should have been eliminated.");
     220             :       }
     221             :     }
     222             :   }
     223             : 
     224      227105 :   return MadeChange;
     225             : }

Generated by: LCOV version 1.13