LLVM API Documentation

LiveVariables.h
Go to the documentation of this file.
00001 //===-- llvm/CodeGen/LiveVariables.h - Live Variable Analysis ---*- C++ -*-===//
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 LiveVariables 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 #ifndef LLVM_CODEGEN_LIVEVARIABLES_H
00030 #define LLVM_CODEGEN_LIVEVARIABLES_H
00031 
00032 #include "llvm/ADT/DenseMap.h"
00033 #include "llvm/ADT/IndexedMap.h"
00034 #include "llvm/ADT/SmallSet.h"
00035 #include "llvm/ADT/SmallVector.h"
00036 #include "llvm/ADT/SparseBitVector.h"
00037 #include "llvm/CodeGen/MachineFunctionPass.h"
00038 #include "llvm/CodeGen/MachineInstr.h"
00039 #include "llvm/Target/TargetRegisterInfo.h"
00040 
00041 namespace llvm {
00042 
00043 class MachineBasicBlock;
00044 class MachineRegisterInfo;
00045 
00046 class LiveVariables : public MachineFunctionPass {
00047 public:
00048   static char ID; // Pass identification, replacement for typeid
00049   LiveVariables() : MachineFunctionPass(ID) {
00050     initializeLiveVariablesPass(*PassRegistry::getPassRegistry());
00051   }
00052 
00053   /// VarInfo - This represents the regions where a virtual register is live in
00054   /// the program.  We represent this with three different pieces of
00055   /// information: the set of blocks in which the instruction is live
00056   /// throughout, the set of blocks in which the instruction is actually used,
00057   /// and the set of non-phi instructions that are the last users of the value.
00058   ///
00059   /// In the common case where a value is defined and killed in the same block,
00060   /// There is one killing instruction, and AliveBlocks is empty.
00061   ///
00062   /// Otherwise, the value is live out of the block.  If the value is live
00063   /// throughout any blocks, these blocks are listed in AliveBlocks.  Blocks
00064   /// where the liveness range ends are not included in AliveBlocks, instead
00065   /// being captured by the Kills set.  In these blocks, the value is live into
00066   /// the block (unless the value is defined and killed in the same block) and
00067   /// lives until the specified instruction.  Note that there cannot ever be a
00068   /// value whose Kills set contains two instructions from the same basic block.
00069   ///
00070   /// PHI nodes complicate things a bit.  If a PHI node is the last user of a
00071   /// value in one of its predecessor blocks, it is not listed in the kills set,
00072   /// but does include the predecessor block in the AliveBlocks set (unless that
00073   /// block also defines the value).  This leads to the (perfectly sensical)
00074   /// situation where a value is defined in a block, and the last use is a phi
00075   /// node in the successor.  In this case, AliveBlocks is empty (the value is
00076   /// not live across any  blocks) and Kills is empty (phi nodes are not
00077   /// included). This is sensical because the value must be live to the end of
00078   /// the block, but is not live in any successor blocks.
00079   struct VarInfo {
00080     /// AliveBlocks - Set of blocks in which this value is alive completely
00081     /// through.  This is a bit set which uses the basic block number as an
00082     /// index.
00083     ///
00084     SparseBitVector<> AliveBlocks;
00085 
00086     /// Kills - List of MachineInstruction's which are the last use of this
00087     /// virtual register (kill it) in their basic block.
00088     ///
00089     std::vector<MachineInstr*> Kills;
00090 
00091     /// removeKill - Delete a kill corresponding to the specified
00092     /// machine instruction. Returns true if there was a kill
00093     /// corresponding to this instruction, false otherwise.
00094     bool removeKill(MachineInstr *MI) {
00095       std::vector<MachineInstr*>::iterator
00096         I = std::find(Kills.begin(), Kills.end(), MI);
00097       if (I == Kills.end())
00098         return false;
00099       Kills.erase(I);
00100       return true;
00101     }
00102 
00103     /// findKill - Find a kill instruction in MBB. Return NULL if none is found.
00104     MachineInstr *findKill(const MachineBasicBlock *MBB) const;
00105 
00106     /// isLiveIn - Is Reg live in to MBB? This means that Reg is live through
00107     /// MBB, or it is killed in MBB. If Reg is only used by PHI instructions in
00108     /// MBB, it is not considered live in.
00109     bool isLiveIn(const MachineBasicBlock &MBB,
00110                   unsigned Reg,
00111                   MachineRegisterInfo &MRI);
00112 
00113     void dump() const;
00114   };
00115 
00116 private:
00117   /// VirtRegInfo - This list is a mapping from virtual register number to
00118   /// variable information.
00119   ///
00120   IndexedMap<VarInfo, VirtReg2IndexFunctor> VirtRegInfo;
00121 
00122   /// PHIJoins - list of virtual registers that are PHI joins. These registers
00123   /// may have multiple definitions, and they require special handling when
00124   /// building live intervals.
00125   SparseBitVector<> PHIJoins;
00126 
00127 private:   // Intermediate data structures
00128   MachineFunction *MF;
00129 
00130   MachineRegisterInfo* MRI;
00131 
00132   const TargetRegisterInfo *TRI;
00133 
00134   // PhysRegInfo - Keep track of which instruction was the last def of a
00135   // physical register. This is a purely local property, because all physical
00136   // register references are presumed dead across basic blocks.
00137   std::vector<MachineInstr *> PhysRegDef;
00138 
00139   // PhysRegInfo - Keep track of which instruction was the last use of a
00140   // physical register. This is a purely local property, because all physical
00141   // register references are presumed dead across basic blocks.
00142   std::vector<MachineInstr *> PhysRegUse;
00143 
00144   std::vector<SmallVector<unsigned, 4>> PHIVarInfo;
00145 
00146   // DistanceMap - Keep track the distance of a MI from the start of the
00147   // current basic block.
00148   DenseMap<MachineInstr*, unsigned> DistanceMap;
00149 
00150   /// HandlePhysRegKill - Add kills of Reg and its sub-registers to the
00151   /// uses. Pay special attention to the sub-register uses which may come below
00152   /// the last use of the whole register.
00153   bool HandlePhysRegKill(unsigned Reg, MachineInstr *MI);
00154 
00155   /// HandleRegMask - Call HandlePhysRegKill for all registers clobbered by Mask.
00156   void HandleRegMask(const MachineOperand&);
00157 
00158   void HandlePhysRegUse(unsigned Reg, MachineInstr *MI);
00159   void HandlePhysRegDef(unsigned Reg, MachineInstr *MI,
00160                         SmallVectorImpl<unsigned> &Defs);
00161   void UpdatePhysRegDefs(MachineInstr *MI, SmallVectorImpl<unsigned> &Defs);
00162 
00163   /// FindLastRefOrPartRef - Return the last reference or partial reference of
00164   /// the specified register.
00165   MachineInstr *FindLastRefOrPartRef(unsigned Reg);
00166 
00167   /// FindLastPartialDef - Return the last partial def of the specified
00168   /// register. Also returns the sub-registers that're defined by the
00169   /// instruction.
00170   MachineInstr *FindLastPartialDef(unsigned Reg,
00171                                    SmallSet<unsigned,4> &PartDefRegs);
00172 
00173   /// analyzePHINodes - Gather information about the PHI nodes in here. In
00174   /// particular, we want to map the variable information of a virtual
00175   /// register which is used in a PHI node. We map that to the BB the vreg
00176   /// is coming from.
00177   void analyzePHINodes(const MachineFunction& Fn);
00178 
00179   void runOnInstr(MachineInstr *MI, SmallVectorImpl<unsigned> &Defs);
00180 
00181   void runOnBlock(MachineBasicBlock *MBB, unsigned NumRegs);
00182 public:
00183 
00184   bool runOnMachineFunction(MachineFunction &MF) override;
00185 
00186   /// RegisterDefIsDead - Return true if the specified instruction defines the
00187   /// specified register, but that definition is dead.
00188   bool RegisterDefIsDead(MachineInstr *MI, unsigned Reg) const;
00189 
00190   //===--------------------------------------------------------------------===//
00191   //  API to update live variable information
00192 
00193   /// replaceKillInstruction - Update register kill info by replacing a kill
00194   /// instruction with a new one.
00195   void replaceKillInstruction(unsigned Reg, MachineInstr *OldMI,
00196                               MachineInstr *NewMI);
00197 
00198   /// addVirtualRegisterKilled - Add information about the fact that the
00199   /// specified register is killed after being used by the specified
00200   /// instruction. If AddIfNotFound is true, add a implicit operand if it's
00201   /// not found.
00202   void addVirtualRegisterKilled(unsigned IncomingReg, MachineInstr *MI,
00203                                 bool AddIfNotFound = false) {
00204     if (MI->addRegisterKilled(IncomingReg, TRI, AddIfNotFound))
00205       getVarInfo(IncomingReg).Kills.push_back(MI); 
00206   }
00207 
00208   /// removeVirtualRegisterKilled - Remove the specified kill of the virtual
00209   /// register from the live variable information. Returns true if the
00210   /// variable was marked as killed by the specified instruction,
00211   /// false otherwise.
00212   bool removeVirtualRegisterKilled(unsigned reg, MachineInstr *MI) {
00213     if (!getVarInfo(reg).removeKill(MI))
00214       return false;
00215 
00216     bool Removed = false;
00217     for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
00218       MachineOperand &MO = MI->getOperand(i);
00219       if (MO.isReg() && MO.isKill() && MO.getReg() == reg) {
00220         MO.setIsKill(false);
00221         Removed = true;
00222         break;
00223       }
00224     }
00225 
00226     assert(Removed && "Register is not used by this instruction!");
00227     (void)Removed;
00228     return true;
00229   }
00230 
00231   /// removeVirtualRegistersKilled - Remove all killed info for the specified
00232   /// instruction.
00233   void removeVirtualRegistersKilled(MachineInstr *MI);
00234 
00235   /// addVirtualRegisterDead - Add information about the fact that the specified
00236   /// register is dead after being used by the specified instruction. If
00237   /// AddIfNotFound is true, add a implicit operand if it's not found.
00238   void addVirtualRegisterDead(unsigned IncomingReg, MachineInstr *MI,
00239                               bool AddIfNotFound = false) {
00240     if (MI->addRegisterDead(IncomingReg, TRI, AddIfNotFound))
00241       getVarInfo(IncomingReg).Kills.push_back(MI);
00242   }
00243 
00244   /// removeVirtualRegisterDead - Remove the specified kill of the virtual
00245   /// register from the live variable information. Returns true if the
00246   /// variable was marked dead at the specified instruction, false
00247   /// otherwise.
00248   bool removeVirtualRegisterDead(unsigned reg, MachineInstr *MI) {
00249     if (!getVarInfo(reg).removeKill(MI))
00250       return false;
00251 
00252     bool Removed = false;
00253     for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
00254       MachineOperand &MO = MI->getOperand(i);
00255       if (MO.isReg() && MO.isDef() && MO.getReg() == reg) {
00256         MO.setIsDead(false);
00257         Removed = true;
00258         break;
00259       }
00260     }
00261     assert(Removed && "Register is not defined by this instruction!");
00262     (void)Removed;
00263     return true;
00264   }
00265 
00266   void getAnalysisUsage(AnalysisUsage &AU) const override;
00267 
00268   void releaseMemory() override {
00269     VirtRegInfo.clear();
00270   }
00271 
00272   /// getVarInfo - Return the VarInfo structure for the specified VIRTUAL
00273   /// register.
00274   VarInfo &getVarInfo(unsigned RegIdx);
00275 
00276   void MarkVirtRegAliveInBlock(VarInfo& VRInfo, MachineBasicBlock* DefBlock,
00277                                MachineBasicBlock *BB);
00278   void MarkVirtRegAliveInBlock(VarInfo& VRInfo, MachineBasicBlock* DefBlock,
00279                                MachineBasicBlock *BB,
00280                                std::vector<MachineBasicBlock*> &WorkList);
00281   void HandleVirtRegDef(unsigned reg, MachineInstr *MI);
00282   void HandleVirtRegUse(unsigned reg, MachineBasicBlock *MBB,
00283                         MachineInstr *MI);
00284 
00285   bool isLiveIn(unsigned Reg, const MachineBasicBlock &MBB) {
00286     return getVarInfo(Reg).isLiveIn(MBB, Reg, *MRI);
00287   }
00288 
00289   /// isLiveOut - Determine if Reg is live out from MBB, when not considering
00290   /// PHI nodes. This means that Reg is either killed by a successor block or
00291   /// passed through one.
00292   bool isLiveOut(unsigned Reg, const MachineBasicBlock &MBB);
00293 
00294   /// addNewBlock - Add a new basic block BB between DomBB and SuccBB. All
00295   /// variables that are live out of DomBB and live into SuccBB will be marked
00296   /// as passing live through BB. This method assumes that the machine code is
00297   /// still in SSA form.
00298   void addNewBlock(MachineBasicBlock *BB,
00299                    MachineBasicBlock *DomBB,
00300                    MachineBasicBlock *SuccBB);
00301 
00302   /// isPHIJoin - Return true if Reg is a phi join register.
00303   bool isPHIJoin(unsigned Reg) { return PHIJoins.test(Reg); }
00304 
00305   /// setPHIJoin - Mark Reg as a phi join register.
00306   void setPHIJoin(unsigned Reg) { PHIJoins.set(Reg); }
00307 };
00308 
00309 } // End llvm namespace
00310 
00311 #endif