LLVM API Documentation

ProcessImplicitDefs.cpp
Go to the documentation of this file.
00001 //===---------------------- ProcessImplicitDefs.cpp -----------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 
00010 #include "llvm/ADT/SetVector.h"
00011 #include "llvm/Analysis/AliasAnalysis.h"
00012 #include "llvm/CodeGen/MachineFunctionPass.h"
00013 #include "llvm/CodeGen/MachineInstr.h"
00014 #include "llvm/CodeGen/MachineRegisterInfo.h"
00015 #include "llvm/CodeGen/Passes.h"
00016 #include "llvm/Support/Debug.h"
00017 #include "llvm/Support/raw_ostream.h"
00018 #include "llvm/Target/TargetInstrInfo.h"
00019 #include "llvm/Target/TargetSubtargetInfo.h"
00020 
00021 using namespace llvm;
00022 
00023 #define DEBUG_TYPE "processimplicitdefs"
00024 
00025 namespace {
00026 /// Process IMPLICIT_DEF instructions and make sure there is one implicit_def
00027 /// for each use. Add isUndef marker to implicit_def defs and their uses.
00028 class ProcessImplicitDefs : public MachineFunctionPass {
00029   const TargetInstrInfo *TII;
00030   const TargetRegisterInfo *TRI;
00031   MachineRegisterInfo *MRI;
00032 
00033   SmallSetVector<MachineInstr*, 16> WorkList;
00034 
00035   void processImplicitDef(MachineInstr *MI);
00036   bool canTurnIntoImplicitDef(MachineInstr *MI);
00037 
00038 public:
00039   static char ID;
00040 
00041   ProcessImplicitDefs() : MachineFunctionPass(ID) {
00042     initializeProcessImplicitDefsPass(*PassRegistry::getPassRegistry());
00043   }
00044 
00045   void getAnalysisUsage(AnalysisUsage &au) const override;
00046 
00047   bool runOnMachineFunction(MachineFunction &fn) override;
00048 };
00049 } // end anonymous namespace
00050 
00051 char ProcessImplicitDefs::ID = 0;
00052 char &llvm::ProcessImplicitDefsID = ProcessImplicitDefs::ID;
00053 
00054 INITIALIZE_PASS_BEGIN(ProcessImplicitDefs, "processimpdefs",
00055                 "Process Implicit Definitions", false, false)
00056 INITIALIZE_PASS_END(ProcessImplicitDefs, "processimpdefs",
00057                 "Process Implicit Definitions", false, false)
00058 
00059 void ProcessImplicitDefs::getAnalysisUsage(AnalysisUsage &AU) const {
00060   AU.setPreservesCFG();
00061   AU.addPreserved<AliasAnalysis>();
00062   MachineFunctionPass::getAnalysisUsage(AU);
00063 }
00064 
00065 bool ProcessImplicitDefs::canTurnIntoImplicitDef(MachineInstr *MI) {
00066   if (!MI->isCopyLike() &&
00067       !MI->isInsertSubreg() &&
00068       !MI->isRegSequence() &&
00069       !MI->isPHI())
00070     return false;
00071   for (MIOperands MO(MI); MO.isValid(); ++MO)
00072     if (MO->isReg() && MO->isUse() && MO->readsReg())
00073       return false;
00074   return true;
00075 }
00076 
00077 void ProcessImplicitDefs::processImplicitDef(MachineInstr *MI) {
00078   DEBUG(dbgs() << "Processing " << *MI);
00079   unsigned Reg = MI->getOperand(0).getReg();
00080 
00081   if (TargetRegisterInfo::isVirtualRegister(Reg)) {
00082     // For virtual registers, mark all uses as <undef>, and convert users to
00083     // implicit-def when possible.
00084     for (MachineOperand &MO : MRI->use_nodbg_operands(Reg)) {
00085       MO.setIsUndef();
00086       MachineInstr *UserMI = MO.getParent();
00087       if (!canTurnIntoImplicitDef(UserMI))
00088         continue;
00089       DEBUG(dbgs() << "Converting to IMPLICIT_DEF: " << *UserMI);
00090       UserMI->setDesc(TII->get(TargetOpcode::IMPLICIT_DEF));
00091       WorkList.insert(UserMI);
00092     }
00093     MI->eraseFromParent();
00094     return;
00095   }
00096 
00097   // This is a physreg implicit-def.
00098   // Look for the first instruction to use or define an alias.
00099   MachineBasicBlock::instr_iterator UserMI = MI;
00100   MachineBasicBlock::instr_iterator UserE = MI->getParent()->instr_end();
00101   bool Found = false;
00102   for (++UserMI; UserMI != UserE; ++UserMI) {
00103     for (MIOperands MO(UserMI); MO.isValid(); ++MO) {
00104       if (!MO->isReg())
00105         continue;
00106       unsigned UserReg = MO->getReg();
00107       if (!TargetRegisterInfo::isPhysicalRegister(UserReg) ||
00108           !TRI->regsOverlap(Reg, UserReg))
00109         continue;
00110       // UserMI uses or redefines Reg. Set <undef> flags on all uses.
00111       Found = true;
00112       if (MO->isUse())
00113         MO->setIsUndef();
00114     }
00115     if (Found)
00116       break;
00117   }
00118 
00119   // If we found the using MI, we can erase the IMPLICIT_DEF.
00120   if (Found) {
00121     DEBUG(dbgs() << "Physreg user: " << *UserMI);
00122     MI->eraseFromParent();
00123     return;
00124   }
00125 
00126   // Using instr wasn't found, it could be in another block.
00127   // Leave the physreg IMPLICIT_DEF, but trim any extra operands.
00128   for (unsigned i = MI->getNumOperands() - 1; i; --i)
00129     MI->RemoveOperand(i);
00130   DEBUG(dbgs() << "Keeping physreg: " << *MI);
00131 }
00132 
00133 /// processImplicitDefs - Process IMPLICIT_DEF instructions and turn them into
00134 /// <undef> operands.
00135 bool ProcessImplicitDefs::runOnMachineFunction(MachineFunction &MF) {
00136 
00137   DEBUG(dbgs() << "********** PROCESS IMPLICIT DEFS **********\n"
00138                << "********** Function: " << MF.getName() << '\n');
00139 
00140   bool Changed = false;
00141 
00142   TII = MF.getSubtarget().getInstrInfo();
00143   TRI = MF.getSubtarget().getRegisterInfo();
00144   MRI = &MF.getRegInfo();
00145   assert(MRI->isSSA() && "ProcessImplicitDefs only works on SSA form.");
00146   assert(WorkList.empty() && "Inconsistent worklist state");
00147 
00148   for (MachineFunction::iterator MFI = MF.begin(), MFE = MF.end();
00149        MFI != MFE; ++MFI) {
00150     // Scan the basic block for implicit defs.
00151     for (MachineBasicBlock::instr_iterator MBBI = MFI->instr_begin(),
00152          MBBE = MFI->instr_end(); MBBI != MBBE; ++MBBI)
00153       if (MBBI->isImplicitDef())
00154         WorkList.insert(MBBI);
00155 
00156     if (WorkList.empty())
00157       continue;
00158 
00159     DEBUG(dbgs() << "BB#" << MFI->getNumber() << " has " << WorkList.size()
00160                  << " implicit defs.\n");
00161     Changed = true;
00162 
00163     // Drain the WorkList to recursively process any new implicit defs.
00164     do processImplicitDef(WorkList.pop_back_val());
00165     while (!WorkList.empty());
00166   }
00167   return Changed;
00168 }