LLVM API Documentation

LiveVariables.cpp
Go to the documentation of this file.
00001 //===-- LiveVariables.cpp - Live Variable Analysis for Machine Code -------===//
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 // This file implements the LiveVariable analysis pass.  For each machine
00011 // instruction in the function, this pass calculates the set of registers that
00012 // are immediately dead after the instruction (i.e., the instruction calculates
00013 // the value, but it is never used) and the set of registers that are used by
00014 // the instruction, but are never used after the instruction (i.e., they are
00015 // killed).
00016 //
00017 // This class computes live variables using a sparse implementation based on
00018 // the machine code SSA form.  This class computes live variable information for
00019 // each virtual and _register allocatable_ physical register in a function.  It
00020 // uses the dominance properties of SSA form to efficiently compute live
00021 // variables for virtual registers, and assumes that physical registers are only
00022 // live within a single basic block (allowing it to do a single local analysis
00023 // to resolve physical register lifetimes in each basic block).  If a physical
00024 // register is not register allocatable, it is not tracked.  This is useful for
00025 // things like the stack pointer and condition codes.
00026 //
00027 //===----------------------------------------------------------------------===//
00028 
00029 #include "llvm/CodeGen/LiveVariables.h"
00030 #include "llvm/ADT/DepthFirstIterator.h"
00031 #include "llvm/ADT/STLExtras.h"
00032 #include "llvm/ADT/SmallPtrSet.h"
00033 #include "llvm/ADT/SmallSet.h"
00034 #include "llvm/CodeGen/MachineInstr.h"
00035 #include "llvm/CodeGen/MachineRegisterInfo.h"
00036 #include "llvm/CodeGen/Passes.h"
00037 #include "llvm/Support/Debug.h"
00038 #include "llvm/Support/ErrorHandling.h"
00039 #include "llvm/Target/TargetInstrInfo.h"
00040 #include "llvm/Target/TargetMachine.h"
00041 #include <algorithm>
00042 using namespace llvm;
00043 
00044 char LiveVariables::ID = 0;
00045 char &llvm::LiveVariablesID = LiveVariables::ID;
00046 INITIALIZE_PASS_BEGIN(LiveVariables, "livevars",
00047                 "Live Variable Analysis", false, false)
00048 INITIALIZE_PASS_DEPENDENCY(UnreachableMachineBlockElim)
00049 INITIALIZE_PASS_END(LiveVariables, "livevars",
00050                 "Live Variable Analysis", false, false)
00051 
00052 
00053 void LiveVariables::getAnalysisUsage(AnalysisUsage &AU) const {
00054   AU.addRequiredID(UnreachableMachineBlockElimID);
00055   AU.setPreservesAll();
00056   MachineFunctionPass::getAnalysisUsage(AU);
00057 }
00058 
00059 MachineInstr *
00060 LiveVariables::VarInfo::findKill(const MachineBasicBlock *MBB) const {
00061   for (unsigned i = 0, e = Kills.size(); i != e; ++i)
00062     if (Kills[i]->getParent() == MBB)
00063       return Kills[i];
00064   return nullptr;
00065 }
00066 
00067 void LiveVariables::VarInfo::dump() const {
00068 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
00069   dbgs() << "  Alive in blocks: ";
00070   for (SparseBitVector<>::iterator I = AliveBlocks.begin(),
00071            E = AliveBlocks.end(); I != E; ++I)
00072     dbgs() << *I << ", ";
00073   dbgs() << "\n  Killed by:";
00074   if (Kills.empty())
00075     dbgs() << " No instructions.\n";
00076   else {
00077     for (unsigned i = 0, e = Kills.size(); i != e; ++i)
00078       dbgs() << "\n    #" << i << ": " << *Kills[i];
00079     dbgs() << "\n";
00080   }
00081 #endif
00082 }
00083 
00084 /// getVarInfo - Get (possibly creating) a VarInfo object for the given vreg.
00085 LiveVariables::VarInfo &LiveVariables::getVarInfo(unsigned RegIdx) {
00086   assert(TargetRegisterInfo::isVirtualRegister(RegIdx) &&
00087          "getVarInfo: not a virtual register!");
00088   VirtRegInfo.grow(RegIdx);
00089   return VirtRegInfo[RegIdx];
00090 }
00091 
00092 void LiveVariables::MarkVirtRegAliveInBlock(VarInfo& VRInfo,
00093                                             MachineBasicBlock *DefBlock,
00094                                             MachineBasicBlock *MBB,
00095                                     std::vector<MachineBasicBlock*> &WorkList) {
00096   unsigned BBNum = MBB->getNumber();
00097 
00098   // Check to see if this basic block is one of the killing blocks.  If so,
00099   // remove it.
00100   for (unsigned i = 0, e = VRInfo.Kills.size(); i != e; ++i)
00101     if (VRInfo.Kills[i]->getParent() == MBB) {
00102       VRInfo.Kills.erase(VRInfo.Kills.begin()+i);  // Erase entry
00103       break;
00104     }
00105 
00106   if (MBB == DefBlock) return;  // Terminate recursion
00107 
00108   if (VRInfo.AliveBlocks.test(BBNum))
00109     return;  // We already know the block is live
00110 
00111   // Mark the variable known alive in this bb
00112   VRInfo.AliveBlocks.set(BBNum);
00113 
00114   assert(MBB != &MF->front() && "Can't find reaching def for virtreg");
00115   WorkList.insert(WorkList.end(), MBB->pred_rbegin(), MBB->pred_rend());
00116 }
00117 
00118 void LiveVariables::MarkVirtRegAliveInBlock(VarInfo &VRInfo,
00119                                             MachineBasicBlock *DefBlock,
00120                                             MachineBasicBlock *MBB) {
00121   std::vector<MachineBasicBlock*> WorkList;
00122   MarkVirtRegAliveInBlock(VRInfo, DefBlock, MBB, WorkList);
00123 
00124   while (!WorkList.empty()) {
00125     MachineBasicBlock *Pred = WorkList.back();
00126     WorkList.pop_back();
00127     MarkVirtRegAliveInBlock(VRInfo, DefBlock, Pred, WorkList);
00128   }
00129 }
00130 
00131 void LiveVariables::HandleVirtRegUse(unsigned reg, MachineBasicBlock *MBB,
00132                                      MachineInstr *MI) {
00133   assert(MRI->getVRegDef(reg) && "Register use before def!");
00134 
00135   unsigned BBNum = MBB->getNumber();
00136 
00137   VarInfo& VRInfo = getVarInfo(reg);
00138 
00139   // Check to see if this basic block is already a kill block.
00140   if (!VRInfo.Kills.empty() && VRInfo.Kills.back()->getParent() == MBB) {
00141     // Yes, this register is killed in this basic block already. Increase the
00142     // live range by updating the kill instruction.
00143     VRInfo.Kills.back() = MI;
00144     return;
00145   }
00146 
00147 #ifndef NDEBUG
00148   for (unsigned i = 0, e = VRInfo.Kills.size(); i != e; ++i)
00149     assert(VRInfo.Kills[i]->getParent() != MBB && "entry should be at end!");
00150 #endif
00151 
00152   // This situation can occur:
00153   //
00154   //     ,------.
00155   //     |      |
00156   //     |      v
00157   //     |   t2 = phi ... t1 ...
00158   //     |      |
00159   //     |      v
00160   //     |   t1 = ...
00161   //     |  ... = ... t1 ...
00162   //     |      |
00163   //     `------'
00164   //
00165   // where there is a use in a PHI node that's a predecessor to the defining
00166   // block. We don't want to mark all predecessors as having the value "alive"
00167   // in this case.
00168   if (MBB == MRI->getVRegDef(reg)->getParent()) return;
00169 
00170   // Add a new kill entry for this basic block. If this virtual register is
00171   // already marked as alive in this basic block, that means it is alive in at
00172   // least one of the successor blocks, it's not a kill.
00173   if (!VRInfo.AliveBlocks.test(BBNum))
00174     VRInfo.Kills.push_back(MI);
00175 
00176   // Update all dominating blocks to mark them as "known live".
00177   for (MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(),
00178          E = MBB->pred_end(); PI != E; ++PI)
00179     MarkVirtRegAliveInBlock(VRInfo, MRI->getVRegDef(reg)->getParent(), *PI);
00180 }
00181 
00182 void LiveVariables::HandleVirtRegDef(unsigned Reg, MachineInstr *MI) {
00183   VarInfo &VRInfo = getVarInfo(Reg);
00184 
00185   if (VRInfo.AliveBlocks.empty())
00186     // If vr is not alive in any block, then defaults to dead.
00187     VRInfo.Kills.push_back(MI);
00188 }
00189 
00190 /// FindLastPartialDef - Return the last partial def of the specified register.
00191 /// Also returns the sub-registers that're defined by the instruction.
00192 MachineInstr *LiveVariables::FindLastPartialDef(unsigned Reg,
00193                                             SmallSet<unsigned,4> &PartDefRegs) {
00194   unsigned LastDefReg = 0;
00195   unsigned LastDefDist = 0;
00196   MachineInstr *LastDef = nullptr;
00197   for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) {
00198     unsigned SubReg = *SubRegs;
00199     MachineInstr *Def = PhysRegDef[SubReg];
00200     if (!Def)
00201       continue;
00202     unsigned Dist = DistanceMap[Def];
00203     if (Dist > LastDefDist) {
00204       LastDefReg  = SubReg;
00205       LastDef     = Def;
00206       LastDefDist = Dist;
00207     }
00208   }
00209 
00210   if (!LastDef)
00211     return nullptr;
00212 
00213   PartDefRegs.insert(LastDefReg);
00214   for (unsigned i = 0, e = LastDef->getNumOperands(); i != e; ++i) {
00215     MachineOperand &MO = LastDef->getOperand(i);
00216     if (!MO.isReg() || !MO.isDef() || MO.getReg() == 0)
00217       continue;
00218     unsigned DefReg = MO.getReg();
00219     if (TRI->isSubRegister(Reg, DefReg)) {
00220       for (MCSubRegIterator SubRegs(DefReg, TRI, /*IncludeSelf=*/true);
00221            SubRegs.isValid(); ++SubRegs)
00222         PartDefRegs.insert(*SubRegs);
00223     }
00224   }
00225   return LastDef;
00226 }
00227 
00228 /// HandlePhysRegUse - Turn previous partial def's into read/mod/writes. Add
00229 /// implicit defs to a machine instruction if there was an earlier def of its
00230 /// super-register.
00231 void LiveVariables::HandlePhysRegUse(unsigned Reg, MachineInstr *MI) {
00232   MachineInstr *LastDef = PhysRegDef[Reg];
00233   // If there was a previous use or a "full" def all is well.
00234   if (!LastDef && !PhysRegUse[Reg]) {
00235     // Otherwise, the last sub-register def implicitly defines this register.
00236     // e.g.
00237     // AH =
00238     // AL = ... <imp-def EAX>, <imp-kill AH>
00239     //    = AH
00240     // ...
00241     //    = EAX
00242     // All of the sub-registers must have been defined before the use of Reg!
00243     SmallSet<unsigned, 4> PartDefRegs;
00244     MachineInstr *LastPartialDef = FindLastPartialDef(Reg, PartDefRegs);
00245     // If LastPartialDef is NULL, it must be using a livein register.
00246     if (LastPartialDef) {
00247       LastPartialDef->addOperand(MachineOperand::CreateReg(Reg, true/*IsDef*/,
00248                                                            true/*IsImp*/));
00249       PhysRegDef[Reg] = LastPartialDef;
00250       SmallSet<unsigned, 8> Processed;
00251       for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) {
00252         unsigned SubReg = *SubRegs;
00253         if (Processed.count(SubReg))
00254           continue;
00255         if (PartDefRegs.count(SubReg))
00256           continue;
00257         // This part of Reg was defined before the last partial def. It's killed
00258         // here.
00259         LastPartialDef->addOperand(MachineOperand::CreateReg(SubReg,
00260                                                              false/*IsDef*/,
00261                                                              true/*IsImp*/));
00262         PhysRegDef[SubReg] = LastPartialDef;
00263         for (MCSubRegIterator SS(SubReg, TRI); SS.isValid(); ++SS)
00264           Processed.insert(*SS);
00265       }
00266     }
00267   } else if (LastDef && !PhysRegUse[Reg] &&
00268              !LastDef->findRegisterDefOperand(Reg))
00269     // Last def defines the super register, add an implicit def of reg.
00270     LastDef->addOperand(MachineOperand::CreateReg(Reg, true/*IsDef*/,
00271                                                   true/*IsImp*/));
00272 
00273   // Remember this use.
00274   for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true);
00275        SubRegs.isValid(); ++SubRegs)
00276     PhysRegUse[*SubRegs] =  MI;
00277 }
00278 
00279 /// FindLastRefOrPartRef - Return the last reference or partial reference of
00280 /// the specified register.
00281 MachineInstr *LiveVariables::FindLastRefOrPartRef(unsigned Reg) {
00282   MachineInstr *LastDef = PhysRegDef[Reg];
00283   MachineInstr *LastUse = PhysRegUse[Reg];
00284   if (!LastDef && !LastUse)
00285     return nullptr;
00286 
00287   MachineInstr *LastRefOrPartRef = LastUse ? LastUse : LastDef;
00288   unsigned LastRefOrPartRefDist = DistanceMap[LastRefOrPartRef];
00289   unsigned LastPartDefDist = 0;
00290   for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) {
00291     unsigned SubReg = *SubRegs;
00292     MachineInstr *Def = PhysRegDef[SubReg];
00293     if (Def && Def != LastDef) {
00294       // There was a def of this sub-register in between. This is a partial
00295       // def, keep track of the last one.
00296       unsigned Dist = DistanceMap[Def];
00297       if (Dist > LastPartDefDist)
00298         LastPartDefDist = Dist;
00299     } else if (MachineInstr *Use = PhysRegUse[SubReg]) {
00300       unsigned Dist = DistanceMap[Use];
00301       if (Dist > LastRefOrPartRefDist) {
00302         LastRefOrPartRefDist = Dist;
00303         LastRefOrPartRef = Use;
00304       }
00305     }
00306   }
00307 
00308   return LastRefOrPartRef;
00309 }
00310 
00311 bool LiveVariables::HandlePhysRegKill(unsigned Reg, MachineInstr *MI) {
00312   MachineInstr *LastDef = PhysRegDef[Reg];
00313   MachineInstr *LastUse = PhysRegUse[Reg];
00314   if (!LastDef && !LastUse)
00315     return false;
00316 
00317   MachineInstr *LastRefOrPartRef = LastUse ? LastUse : LastDef;
00318   unsigned LastRefOrPartRefDist = DistanceMap[LastRefOrPartRef];
00319   // The whole register is used.
00320   // AL =
00321   // AH =
00322   //
00323   //    = AX
00324   //    = AL, AX<imp-use, kill>
00325   // AX =
00326   //
00327   // Or whole register is defined, but not used at all.
00328   // AX<dead> =
00329   // ...
00330   // AX =
00331   //
00332   // Or whole register is defined, but only partly used.
00333   // AX<dead> = AL<imp-def>
00334   //    = AL<kill>
00335   // AX =
00336   MachineInstr *LastPartDef = nullptr;
00337   unsigned LastPartDefDist = 0;
00338   SmallSet<unsigned, 8> PartUses;
00339   for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) {
00340     unsigned SubReg = *SubRegs;
00341     MachineInstr *Def = PhysRegDef[SubReg];
00342     if (Def && Def != LastDef) {
00343       // There was a def of this sub-register in between. This is a partial
00344       // def, keep track of the last one.
00345       unsigned Dist = DistanceMap[Def];
00346       if (Dist > LastPartDefDist) {
00347         LastPartDefDist = Dist;
00348         LastPartDef = Def;
00349       }
00350       continue;
00351     }
00352     if (MachineInstr *Use = PhysRegUse[SubReg]) {
00353       for (MCSubRegIterator SS(SubReg, TRI, /*IncludeSelf=*/true); SS.isValid();
00354            ++SS)
00355         PartUses.insert(*SS);
00356       unsigned Dist = DistanceMap[Use];
00357       if (Dist > LastRefOrPartRefDist) {
00358         LastRefOrPartRefDist = Dist;
00359         LastRefOrPartRef = Use;
00360       }
00361     }
00362   }
00363 
00364   if (!PhysRegUse[Reg]) {
00365     // Partial uses. Mark register def dead and add implicit def of
00366     // sub-registers which are used.
00367     // EAX<dead>  = op  AL<imp-def>
00368     // That is, EAX def is dead but AL def extends pass it.
00369     PhysRegDef[Reg]->addRegisterDead(Reg, TRI, true);
00370     for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) {
00371       unsigned SubReg = *SubRegs;
00372       if (!PartUses.count(SubReg))
00373         continue;
00374       bool NeedDef = true;
00375       if (PhysRegDef[Reg] == PhysRegDef[SubReg]) {
00376         MachineOperand *MO = PhysRegDef[Reg]->findRegisterDefOperand(SubReg);
00377         if (MO) {
00378           NeedDef = false;
00379           assert(!MO->isDead());
00380         }
00381       }
00382       if (NeedDef)
00383         PhysRegDef[Reg]->addOperand(MachineOperand::CreateReg(SubReg,
00384                                                  true/*IsDef*/, true/*IsImp*/));
00385       MachineInstr *LastSubRef = FindLastRefOrPartRef(SubReg);
00386       if (LastSubRef)
00387         LastSubRef->addRegisterKilled(SubReg, TRI, true);
00388       else {
00389         LastRefOrPartRef->addRegisterKilled(SubReg, TRI, true);
00390         for (MCSubRegIterator SS(SubReg, TRI, /*IncludeSelf=*/true);
00391              SS.isValid(); ++SS)
00392           PhysRegUse[*SS] = LastRefOrPartRef;
00393       }
00394       for (MCSubRegIterator SS(SubReg, TRI); SS.isValid(); ++SS)
00395         PartUses.erase(*SS);
00396     }
00397   } else if (LastRefOrPartRef == PhysRegDef[Reg] && LastRefOrPartRef != MI) {
00398     if (LastPartDef)
00399       // The last partial def kills the register.
00400       LastPartDef->addOperand(MachineOperand::CreateReg(Reg, false/*IsDef*/,
00401                                                 true/*IsImp*/, true/*IsKill*/));
00402     else {
00403       MachineOperand *MO =
00404         LastRefOrPartRef->findRegisterDefOperand(Reg, false, TRI);
00405       bool NeedEC = MO->isEarlyClobber() && MO->getReg() != Reg;
00406       // If the last reference is the last def, then it's not used at all.
00407       // That is, unless we are currently processing the last reference itself.
00408       LastRefOrPartRef->addRegisterDead(Reg, TRI, true);
00409       if (NeedEC) {
00410         // If we are adding a subreg def and the superreg def is marked early
00411         // clobber, add an early clobber marker to the subreg def.
00412         MO = LastRefOrPartRef->findRegisterDefOperand(Reg);
00413         if (MO)
00414           MO->setIsEarlyClobber();
00415       }
00416     }
00417   } else
00418     LastRefOrPartRef->addRegisterKilled(Reg, TRI, true);
00419   return true;
00420 }
00421 
00422 void LiveVariables::HandleRegMask(const MachineOperand &MO) {
00423   // Call HandlePhysRegKill() for all live registers clobbered by Mask.
00424   // Clobbered registers are always dead, sp there is no need to use
00425   // HandlePhysRegDef().
00426   for (unsigned Reg = 1, NumRegs = TRI->getNumRegs(); Reg != NumRegs; ++Reg) {
00427     // Skip dead regs.
00428     if (!PhysRegDef[Reg] && !PhysRegUse[Reg])
00429       continue;
00430     // Skip mask-preserved regs.
00431     if (!MO.clobbersPhysReg(Reg))
00432       continue;
00433     // Kill the largest clobbered super-register.
00434     // This avoids needless implicit operands.
00435     unsigned Super = Reg;
00436     for (MCSuperRegIterator SR(Reg, TRI); SR.isValid(); ++SR)
00437       if ((PhysRegDef[*SR] || PhysRegUse[*SR]) && MO.clobbersPhysReg(*SR))
00438         Super = *SR;
00439     HandlePhysRegKill(Super, nullptr);
00440   }
00441 }
00442 
00443 void LiveVariables::HandlePhysRegDef(unsigned Reg, MachineInstr *MI,
00444                                      SmallVectorImpl<unsigned> &Defs) {
00445   // What parts of the register are previously defined?
00446   SmallSet<unsigned, 32> Live;
00447   if (PhysRegDef[Reg] || PhysRegUse[Reg]) {
00448     for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true);
00449          SubRegs.isValid(); ++SubRegs)
00450       Live.insert(*SubRegs);
00451   } else {
00452     for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) {
00453       unsigned SubReg = *SubRegs;
00454       // If a register isn't itself defined, but all parts that make up of it
00455       // are defined, then consider it also defined.
00456       // e.g.
00457       // AL =
00458       // AH =
00459       //    = AX
00460       if (Live.count(SubReg))
00461         continue;
00462       if (PhysRegDef[SubReg] || PhysRegUse[SubReg]) {
00463         for (MCSubRegIterator SS(SubReg, TRI, /*IncludeSelf=*/true);
00464              SS.isValid(); ++SS)
00465           Live.insert(*SS);
00466       }
00467     }
00468   }
00469 
00470   // Start from the largest piece, find the last time any part of the register
00471   // is referenced.
00472   HandlePhysRegKill(Reg, MI);
00473   // Only some of the sub-registers are used.
00474   for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) {
00475     unsigned SubReg = *SubRegs;
00476     if (!Live.count(SubReg))
00477       // Skip if this sub-register isn't defined.
00478       continue;
00479     HandlePhysRegKill(SubReg, MI);
00480   }
00481 
00482   if (MI)
00483     Defs.push_back(Reg);  // Remember this def.
00484 }
00485 
00486 void LiveVariables::UpdatePhysRegDefs(MachineInstr *MI,
00487                                       SmallVectorImpl<unsigned> &Defs) {
00488   while (!Defs.empty()) {
00489     unsigned Reg = Defs.back();
00490     Defs.pop_back();
00491     for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true);
00492          SubRegs.isValid(); ++SubRegs) {
00493       unsigned SubReg = *SubRegs;
00494       PhysRegDef[SubReg]  = MI;
00495       PhysRegUse[SubReg]  = nullptr;
00496     }
00497   }
00498 }
00499 
00500 void LiveVariables::runOnInstr(MachineInstr *MI,
00501                                SmallVectorImpl<unsigned> &Defs) {
00502   assert(!MI->isDebugValue());
00503   // Process all of the operands of the instruction...
00504   unsigned NumOperandsToProcess = MI->getNumOperands();
00505 
00506   // Unless it is a PHI node.  In this case, ONLY process the DEF, not any
00507   // of the uses.  They will be handled in other basic blocks.
00508   if (MI->isPHI())
00509     NumOperandsToProcess = 1;
00510 
00511   // Clear kill and dead markers. LV will recompute them.
00512   SmallVector<unsigned, 4> UseRegs;
00513   SmallVector<unsigned, 4> DefRegs;
00514   SmallVector<unsigned, 1> RegMasks;
00515   for (unsigned i = 0; i != NumOperandsToProcess; ++i) {
00516     MachineOperand &MO = MI->getOperand(i);
00517     if (MO.isRegMask()) {
00518       RegMasks.push_back(i);
00519       continue;
00520     }
00521     if (!MO.isReg() || MO.getReg() == 0)
00522       continue;
00523     unsigned MOReg = MO.getReg();
00524     if (MO.isUse()) {
00525       MO.setIsKill(false);
00526       if (MO.readsReg())
00527         UseRegs.push_back(MOReg);
00528     } else /*MO.isDef()*/ {
00529       MO.setIsDead(false);
00530       DefRegs.push_back(MOReg);
00531     }
00532   }
00533 
00534   MachineBasicBlock *MBB = MI->getParent();
00535   // Process all uses.
00536   for (unsigned i = 0, e = UseRegs.size(); i != e; ++i) {
00537     unsigned MOReg = UseRegs[i];
00538     if (TargetRegisterInfo::isVirtualRegister(MOReg))
00539       HandleVirtRegUse(MOReg, MBB, MI);
00540     else if (!MRI->isReserved(MOReg))
00541       HandlePhysRegUse(MOReg, MI);
00542   }
00543 
00544   // Process all masked registers. (Call clobbers).
00545   for (unsigned i = 0, e = RegMasks.size(); i != e; ++i)
00546     HandleRegMask(MI->getOperand(RegMasks[i]));
00547 
00548   // Process all defs.
00549   for (unsigned i = 0, e = DefRegs.size(); i != e; ++i) {
00550     unsigned MOReg = DefRegs[i];
00551     if (TargetRegisterInfo::isVirtualRegister(MOReg))
00552       HandleVirtRegDef(MOReg, MI);
00553     else if (!MRI->isReserved(MOReg))
00554       HandlePhysRegDef(MOReg, MI, Defs);
00555   }
00556   UpdatePhysRegDefs(MI, Defs);
00557 }
00558 
00559 void LiveVariables::runOnBlock(MachineBasicBlock *MBB, const unsigned NumRegs) {
00560   // Mark live-in registers as live-in.
00561   SmallVector<unsigned, 4> Defs;
00562   for (MachineBasicBlock::livein_iterator II = MBB->livein_begin(),
00563          EE = MBB->livein_end(); II != EE; ++II) {
00564     assert(TargetRegisterInfo::isPhysicalRegister(*II) &&
00565            "Cannot have a live-in virtual register!");
00566     HandlePhysRegDef(*II, nullptr, Defs);
00567   }
00568 
00569   // Loop over all of the instructions, processing them.
00570   DistanceMap.clear();
00571   unsigned Dist = 0;
00572   for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
00573        I != E; ++I) {
00574     MachineInstr *MI = I;
00575     if (MI->isDebugValue())
00576       continue;
00577     DistanceMap.insert(std::make_pair(MI, Dist++));
00578 
00579     runOnInstr(MI, Defs);
00580   }
00581 
00582   // Handle any virtual assignments from PHI nodes which might be at the
00583   // bottom of this basic block.  We check all of our successor blocks to see
00584   // if they have PHI nodes, and if so, we simulate an assignment at the end
00585   // of the current block.
00586   if (!PHIVarInfo[MBB->getNumber()].empty()) {
00587     SmallVectorImpl<unsigned> &VarInfoVec = PHIVarInfo[MBB->getNumber()];
00588 
00589     for (SmallVectorImpl<unsigned>::iterator I = VarInfoVec.begin(),
00590            E = VarInfoVec.end(); I != E; ++I)
00591       // Mark it alive only in the block we are representing.
00592       MarkVirtRegAliveInBlock(getVarInfo(*I),MRI->getVRegDef(*I)->getParent(),
00593                               MBB);
00594   }
00595 
00596   // MachineCSE may CSE instructions which write to non-allocatable physical
00597   // registers across MBBs. Remember if any reserved register is liveout.
00598   SmallSet<unsigned, 4> LiveOuts;
00599   for (MachineBasicBlock::const_succ_iterator SI = MBB->succ_begin(),
00600          SE = MBB->succ_end(); SI != SE; ++SI) {
00601     MachineBasicBlock *SuccMBB = *SI;
00602     if (SuccMBB->isLandingPad())
00603       continue;
00604     for (MachineBasicBlock::livein_iterator LI = SuccMBB->livein_begin(),
00605            LE = SuccMBB->livein_end(); LI != LE; ++LI) {
00606       unsigned LReg = *LI;
00607       if (!TRI->isInAllocatableClass(LReg))
00608         // Ignore other live-ins, e.g. those that are live into landing pads.
00609         LiveOuts.insert(LReg);
00610     }
00611   }
00612 
00613   // Loop over PhysRegDef / PhysRegUse, killing any registers that are
00614   // available at the end of the basic block.
00615   for (unsigned i = 0; i != NumRegs; ++i)
00616     if ((PhysRegDef[i] || PhysRegUse[i]) && !LiveOuts.count(i))
00617       HandlePhysRegDef(i, nullptr, Defs);
00618 }
00619 
00620 bool LiveVariables::runOnMachineFunction(MachineFunction &mf) {
00621   MF = &mf;
00622   MRI = &mf.getRegInfo();
00623   TRI = MF->getSubtarget().getRegisterInfo();
00624 
00625   const unsigned NumRegs = TRI->getNumRegs();
00626   PhysRegDef.assign(NumRegs, nullptr);
00627   PhysRegUse.assign(NumRegs, nullptr);
00628   PHIVarInfo.resize(MF->getNumBlockIDs());
00629   PHIJoins.clear();
00630 
00631   // FIXME: LiveIntervals will be updated to remove its dependence on
00632   // LiveVariables to improve compilation time and eliminate bizarre pass
00633   // dependencies. Until then, we can't change much in -O0.
00634   if (!MRI->isSSA())
00635     report_fatal_error("regalloc=... not currently supported with -O0");
00636 
00637   analyzePHINodes(mf);
00638 
00639   // Calculate live variable information in depth first order on the CFG of the
00640   // function.  This guarantees that we will see the definition of a virtual
00641   // register before its uses due to dominance properties of SSA (except for PHI
00642   // nodes, which are treated as a special case).
00643   MachineBasicBlock *Entry = MF->begin();
00644   SmallPtrSet<MachineBasicBlock*,16> Visited;
00645 
00646   for (MachineBasicBlock *MBB : depth_first_ext(Entry, Visited)) {
00647     runOnBlock(MBB, NumRegs);
00648 
00649     PhysRegDef.assign(NumRegs, nullptr);
00650     PhysRegUse.assign(NumRegs, nullptr);
00651   }
00652 
00653   // Convert and transfer the dead / killed information we have gathered into
00654   // VirtRegInfo onto MI's.
00655   for (unsigned i = 0, e1 = VirtRegInfo.size(); i != e1; ++i) {
00656     const unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
00657     for (unsigned j = 0, e2 = VirtRegInfo[Reg].Kills.size(); j != e2; ++j)
00658       if (VirtRegInfo[Reg].Kills[j] == MRI->getVRegDef(Reg))
00659         VirtRegInfo[Reg].Kills[j]->addRegisterDead(Reg, TRI);
00660       else
00661         VirtRegInfo[Reg].Kills[j]->addRegisterKilled(Reg, TRI);
00662   }
00663 
00664   // Check to make sure there are no unreachable blocks in the MC CFG for the
00665   // function.  If so, it is due to a bug in the instruction selector or some
00666   // other part of the code generator if this happens.
00667 #ifndef NDEBUG
00668   for(MachineFunction::iterator i = MF->begin(), e = MF->end(); i != e; ++i)
00669     assert(Visited.count(&*i) != 0 && "unreachable basic block found");
00670 #endif
00671 
00672   PhysRegDef.clear();
00673   PhysRegUse.clear();
00674   PHIVarInfo.clear();
00675 
00676   return false;
00677 }
00678 
00679 /// replaceKillInstruction - Update register kill info by replacing a kill
00680 /// instruction with a new one.
00681 void LiveVariables::replaceKillInstruction(unsigned Reg, MachineInstr *OldMI,
00682                                            MachineInstr *NewMI) {
00683   VarInfo &VI = getVarInfo(Reg);
00684   std::replace(VI.Kills.begin(), VI.Kills.end(), OldMI, NewMI);
00685 }
00686 
00687 /// removeVirtualRegistersKilled - Remove all killed info for the specified
00688 /// instruction.
00689 void LiveVariables::removeVirtualRegistersKilled(MachineInstr *MI) {
00690   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
00691     MachineOperand &MO = MI->getOperand(i);
00692     if (MO.isReg() && MO.isKill()) {
00693       MO.setIsKill(false);
00694       unsigned Reg = MO.getReg();
00695       if (TargetRegisterInfo::isVirtualRegister(Reg)) {
00696         bool removed = getVarInfo(Reg).removeKill(MI);
00697         assert(removed && "kill not in register's VarInfo?");
00698         (void)removed;
00699       }
00700     }
00701   }
00702 }
00703 
00704 /// analyzePHINodes - Gather information about the PHI nodes in here. In
00705 /// particular, we want to map the variable information of a virtual register
00706 /// which is used in a PHI node. We map that to the BB the vreg is coming from.
00707 ///
00708 void LiveVariables::analyzePHINodes(const MachineFunction& Fn) {
00709   for (const auto &MBB : Fn)
00710     for (const auto &BBI : MBB) {
00711       if (!BBI.isPHI())
00712         break;
00713       for (unsigned i = 1, e = BBI.getNumOperands(); i != e; i += 2)
00714         if (BBI.getOperand(i).readsReg())
00715           PHIVarInfo[BBI.getOperand(i + 1).getMBB()->getNumber()]
00716             .push_back(BBI.getOperand(i).getReg());
00717     }
00718 }
00719 
00720 bool LiveVariables::VarInfo::isLiveIn(const MachineBasicBlock &MBB,
00721                                       unsigned Reg,
00722                                       MachineRegisterInfo &MRI) {
00723   unsigned Num = MBB.getNumber();
00724 
00725   // Reg is live-through.
00726   if (AliveBlocks.test(Num))
00727     return true;
00728 
00729   // Registers defined in MBB cannot be live in.
00730   const MachineInstr *Def = MRI.getVRegDef(Reg);
00731   if (Def && Def->getParent() == &MBB)
00732     return false;
00733 
00734  // Reg was not defined in MBB, was it killed here?
00735   return findKill(&MBB);
00736 }
00737 
00738 bool LiveVariables::isLiveOut(unsigned Reg, const MachineBasicBlock &MBB) {
00739   LiveVariables::VarInfo &VI = getVarInfo(Reg);
00740 
00741   // Loop over all of the successors of the basic block, checking to see if
00742   // the value is either live in the block, or if it is killed in the block.
00743   SmallVector<MachineBasicBlock*, 8> OpSuccBlocks;
00744   for (MachineBasicBlock::const_succ_iterator SI = MBB.succ_begin(),
00745          E = MBB.succ_end(); SI != E; ++SI) {
00746     MachineBasicBlock *SuccMBB = *SI;
00747 
00748     // Is it alive in this successor?
00749     unsigned SuccIdx = SuccMBB->getNumber();
00750     if (VI.AliveBlocks.test(SuccIdx))
00751       return true;
00752     OpSuccBlocks.push_back(SuccMBB);
00753   }
00754 
00755   // Check to see if this value is live because there is a use in a successor
00756   // that kills it.
00757   switch (OpSuccBlocks.size()) {
00758   case 1: {
00759     MachineBasicBlock *SuccMBB = OpSuccBlocks[0];
00760     for (unsigned i = 0, e = VI.Kills.size(); i != e; ++i)
00761       if (VI.Kills[i]->getParent() == SuccMBB)
00762         return true;
00763     break;
00764   }
00765   case 2: {
00766     MachineBasicBlock *SuccMBB1 = OpSuccBlocks[0], *SuccMBB2 = OpSuccBlocks[1];
00767     for (unsigned i = 0, e = VI.Kills.size(); i != e; ++i)
00768       if (VI.Kills[i]->getParent() == SuccMBB1 ||
00769           VI.Kills[i]->getParent() == SuccMBB2)
00770         return true;
00771     break;
00772   }
00773   default:
00774     std::sort(OpSuccBlocks.begin(), OpSuccBlocks.end());
00775     for (unsigned i = 0, e = VI.Kills.size(); i != e; ++i)
00776       if (std::binary_search(OpSuccBlocks.begin(), OpSuccBlocks.end(),
00777                              VI.Kills[i]->getParent()))
00778         return true;
00779   }
00780   return false;
00781 }
00782 
00783 /// addNewBlock - Add a new basic block BB as an empty succcessor to DomBB. All
00784 /// variables that are live out of DomBB will be marked as passing live through
00785 /// BB.
00786 void LiveVariables::addNewBlock(MachineBasicBlock *BB,
00787                                 MachineBasicBlock *DomBB,
00788                                 MachineBasicBlock *SuccBB) {
00789   const unsigned NumNew = BB->getNumber();
00790 
00791   SmallSet<unsigned, 16> Defs, Kills;
00792 
00793   MachineBasicBlock::iterator BBI = SuccBB->begin(), BBE = SuccBB->end();
00794   for (; BBI != BBE && BBI->isPHI(); ++BBI) {
00795     // Record the def of the PHI node.
00796     Defs.insert(BBI->getOperand(0).getReg());
00797 
00798     // All registers used by PHI nodes in SuccBB must be live through BB.
00799     for (unsigned i = 1, e = BBI->getNumOperands(); i != e; i += 2)
00800       if (BBI->getOperand(i+1).getMBB() == BB)
00801         getVarInfo(BBI->getOperand(i).getReg()).AliveBlocks.set(NumNew);
00802   }
00803 
00804   // Record all vreg defs and kills of all instructions in SuccBB.
00805   for (; BBI != BBE; ++BBI) {
00806     for (MachineInstr::mop_iterator I = BBI->operands_begin(),
00807          E = BBI->operands_end(); I != E; ++I) {
00808       if (I->isReg() && TargetRegisterInfo::isVirtualRegister(I->getReg())) {
00809         if (I->isDef())
00810           Defs.insert(I->getReg());
00811         else if (I->isKill())
00812           Kills.insert(I->getReg());
00813       }
00814     }
00815   }
00816 
00817   // Update info for all live variables
00818   for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
00819     unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
00820 
00821     // If the Defs is defined in the successor it can't be live in BB.
00822     if (Defs.count(Reg))
00823       continue;
00824 
00825     // If the register is either killed in or live through SuccBB it's also live
00826     // through BB.
00827     VarInfo &VI = getVarInfo(Reg);
00828     if (Kills.count(Reg) || VI.AliveBlocks.test(SuccBB->getNumber()))
00829       VI.AliveBlocks.set(NumNew);
00830   }
00831 }