LLVM API Documentation

LiveRangeCalc.h
Go to the documentation of this file.
00001 //===---- LiveRangeCalc.h - Calculate live ranges ---------------*- 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 // The LiveRangeCalc class can be used to compute live ranges from scratch.  It
00011 // caches information about values in the CFG to speed up repeated operations
00012 // on the same live range.  The cache can be shared by non-overlapping live
00013 // ranges.  SplitKit uses that when computing the live range of split products.
00014 //
00015 // A low-level interface is available to clients that know where a variable is
00016 // live, but don't know which value it has as every point.  LiveRangeCalc will
00017 // propagate values down the dominator tree, and even insert PHI-defs where
00018 // needed.  SplitKit uses this faster interface when possible.
00019 //
00020 //===----------------------------------------------------------------------===//
00021 
00022 #ifndef LLVM_LIB_CODEGEN_LIVERANGECALC_H
00023 #define LLVM_LIB_CODEGEN_LIVERANGECALC_H
00024 
00025 #include "llvm/ADT/BitVector.h"
00026 #include "llvm/ADT/IndexedMap.h"
00027 #include "llvm/CodeGen/LiveInterval.h"
00028 
00029 namespace llvm {
00030 
00031 /// Forward declarations for MachineDominators.h:
00032 class MachineDominatorTree;
00033 template <class NodeT> class DomTreeNodeBase;
00034 typedef DomTreeNodeBase<MachineBasicBlock> MachineDomTreeNode;
00035 
00036 class LiveRangeCalc {
00037   const MachineFunction *MF;
00038   const MachineRegisterInfo *MRI;
00039   SlotIndexes *Indexes;
00040   MachineDominatorTree *DomTree;
00041   VNInfo::Allocator *Alloc;
00042 
00043   /// Seen - Bit vector of active entries in LiveOut, also used as a visited
00044   /// set by findReachingDefs.  One entry per basic block, indexed by block
00045   /// number.  This is kept as a separate bit vector because it can be cleared
00046   /// quickly when switching live ranges.
00047   BitVector Seen;
00048 
00049   /// LiveOutPair - A value and the block that defined it.  The domtree node is
00050   /// redundant, it can be computed as: MDT[Indexes.getMBBFromIndex(VNI->def)].
00051   typedef std::pair<VNInfo*, MachineDomTreeNode*> LiveOutPair;
00052 
00053   /// LiveOutMap - Map basic blocks to the value leaving the block.
00054   typedef IndexedMap<LiveOutPair, MBB2NumberFunctor> LiveOutMap;
00055 
00056   /// LiveOut - Map each basic block where a live range is live out to the
00057   /// live-out value and its defining block.
00058   ///
00059   /// For every basic block, MBB, one of these conditions shall be true:
00060   ///
00061   ///  1. !Seen.count(MBB->getNumber())
00062   ///     Blocks without a Seen bit are ignored.
00063   ///  2. LiveOut[MBB].second.getNode() == MBB
00064   ///     The live-out value is defined in MBB.
00065   ///  3. forall P in preds(MBB): LiveOut[P] == LiveOut[MBB]
00066   ///     The live-out value passses through MBB. All predecessors must carry
00067   ///     the same value.
00068   ///
00069   /// The domtree node may be null, it can be computed.
00070   ///
00071   /// The map can be shared by multiple live ranges as long as no two are
00072   /// live-out of the same block.
00073   LiveOutMap LiveOut;
00074 
00075   /// LiveInBlock - Information about a basic block where a live range is known
00076   /// to be live-in, but the value has not yet been determined.
00077   struct LiveInBlock {
00078     // The live range set that is live-in to this block.  The algorithms can
00079     // handle multiple non-overlapping live ranges simultaneously.
00080     LiveRange &LR;
00081 
00082     // DomNode - Dominator tree node for the block.
00083     // Cleared when the final value has been determined and LI has been updated.
00084     MachineDomTreeNode *DomNode;
00085 
00086     // Position in block where the live-in range ends, or SlotIndex() if the
00087     // range passes through the block.  When the final value has been
00088     // determined, the range from the block start to Kill will be added to LI.
00089     SlotIndex Kill;
00090 
00091     // Live-in value filled in by updateSSA once it is known.
00092     VNInfo *Value;
00093 
00094     LiveInBlock(LiveRange &LR, MachineDomTreeNode *node, SlotIndex kill)
00095       : LR(LR), DomNode(node), Kill(kill), Value(nullptr) {}
00096   };
00097 
00098   /// LiveIn - Work list of blocks where the live-in value has yet to be
00099   /// determined.  This list is typically computed by findReachingDefs() and
00100   /// used as a work list by updateSSA().  The low-level interface may also be
00101   /// used to add entries directly.
00102   SmallVector<LiveInBlock, 16> LiveIn;
00103 
00104   /// Assuming that LI is live-in to KillMBB and killed at Kill, find the set
00105   /// of defs that can reach it.
00106   ///
00107   /// If only one def can reach Kill, all paths from the def to kill are added
00108   /// to LI, and the function returns true.
00109   ///
00110   /// If multiple values can reach Kill, the blocks that need LI to be live in
00111   /// are added to the LiveIn array, and the function returns false.
00112   ///
00113   /// PhysReg, when set, is used to verify live-in lists on basic blocks.
00114   bool findReachingDefs(LiveRange &LR, MachineBasicBlock &KillMBB,
00115                         SlotIndex Kill, unsigned PhysReg);
00116 
00117   /// updateSSA - Compute the values that will be live in to all requested
00118   /// blocks in LiveIn.  Create PHI-def values as required to preserve SSA form.
00119   ///
00120   /// Every live-in block must be jointly dominated by the added live-out
00121   /// blocks.  No values are read from the live ranges.
00122   void updateSSA();
00123 
00124   /// Add liveness as specified in the LiveIn vector.
00125   void updateLiveIns();
00126 
00127 public:
00128   LiveRangeCalc() : MF(nullptr), MRI(nullptr), Indexes(nullptr),
00129                     DomTree(nullptr), Alloc(nullptr) {}
00130 
00131   //===--------------------------------------------------------------------===//
00132   // High-level interface.
00133   //===--------------------------------------------------------------------===//
00134   //
00135   // Calculate live ranges from scratch.
00136   //
00137 
00138   /// reset - Prepare caches for a new set of non-overlapping live ranges.  The
00139   /// caches must be reset before attempting calculations with a live range
00140   /// that may overlap a previously computed live range, and before the first
00141   /// live range in a function.  If live ranges are not known to be
00142   /// non-overlapping, call reset before each.
00143   void reset(const MachineFunction *MF,
00144              SlotIndexes*,
00145              MachineDominatorTree*,
00146              VNInfo::Allocator*);
00147 
00148   //===--------------------------------------------------------------------===//
00149   // Mid-level interface.
00150   //===--------------------------------------------------------------------===//
00151   //
00152   // Modify existing live ranges.
00153   //
00154 
00155   /// extend - Extend the live range of LI to reach Kill.
00156   ///
00157   /// The existing values in LI must be live so they jointly dominate Kill.  If
00158   /// Kill is not dominated by a single existing value, PHI-defs are inserted
00159   /// as required to preserve SSA form.  If Kill is known to be dominated by a
00160   /// single existing value, Alloc may be null.
00161   ///
00162   /// PhysReg, when set, is used to verify live-in lists on basic blocks.
00163   void extend(LiveRange &LR, SlotIndex Kill, unsigned PhysReg = 0);
00164 
00165   /// createDeadDefs - Create a dead def in LI for every def operand of Reg.
00166   /// Each instruction defining Reg gets a new VNInfo with a corresponding
00167   /// minimal live range.
00168   void createDeadDefs(LiveRange &LR, unsigned Reg);
00169 
00170   /// createDeadDefs - Create a dead def in LI for every def of LI->reg.
00171   void createDeadDefs(LiveInterval &LI) {
00172     createDeadDefs(LI, LI.reg);
00173   }
00174 
00175   /// extendToUses - Extend the live range of LI to reach all uses of Reg.
00176   ///
00177   /// All uses must be jointly dominated by existing liveness.  PHI-defs are
00178   /// inserted as needed to preserve SSA form.
00179   void extendToUses(LiveRange &LR, unsigned Reg);
00180 
00181   /// extendToUses - Extend the live range of LI to reach all uses of LI->reg.
00182   void extendToUses(LiveInterval &LI) {
00183     extendToUses(LI, LI.reg);
00184   }
00185 
00186   //===--------------------------------------------------------------------===//
00187   // Low-level interface.
00188   //===--------------------------------------------------------------------===//
00189   //
00190   // These functions can be used to compute live ranges where the live-in and
00191   // live-out blocks are already known, but the SSA value in each block is
00192   // unknown.
00193   //
00194   // After calling reset(), add known live-out values and known live-in blocks.
00195   // Then call calculateValues() to compute the actual value that is
00196   // live-in to each block, and add liveness to the live ranges.
00197   //
00198 
00199   /// setLiveOutValue - Indicate that VNI is live out from MBB.  The
00200   /// calculateValues() function will not add liveness for MBB, the caller
00201   /// should take care of that.
00202   ///
00203   /// VNI may be null only if MBB is a live-through block also passed to
00204   /// addLiveInBlock().
00205   void setLiveOutValue(MachineBasicBlock *MBB, VNInfo *VNI) {
00206     Seen.set(MBB->getNumber());
00207     LiveOut[MBB] = LiveOutPair(VNI, nullptr);
00208   }
00209 
00210   /// addLiveInBlock - Add a block with an unknown live-in value.  This
00211   /// function can only be called once per basic block.  Once the live-in value
00212   /// has been determined, calculateValues() will add liveness to LI.
00213   ///
00214   /// @param LR      The live range that is live-in to the block.
00215   /// @param DomNode The domtree node for the block.
00216   /// @param Kill    Index in block where LI is killed.  If the value is
00217   ///                live-through, set Kill = SLotIndex() and also call
00218   ///                setLiveOutValue(MBB, 0).
00219   void addLiveInBlock(LiveRange &LR,
00220                       MachineDomTreeNode *DomNode,
00221                       SlotIndex Kill = SlotIndex()) {
00222     LiveIn.push_back(LiveInBlock(LR, DomNode, Kill));
00223   }
00224 
00225   /// calculateValues - Calculate the value that will be live-in to each block
00226   /// added with addLiveInBlock.  Add PHI-def values as needed to preserve SSA
00227   /// form.  Add liveness to all live-in blocks up to the Kill point, or the
00228   /// whole block for live-through blocks.
00229   ///
00230   /// Every predecessor of a live-in block must have been given a value with
00231   /// setLiveOutValue, the value may be null for live-trough blocks.
00232   void calculateValues();
00233 };
00234 
00235 } // end namespace llvm
00236 
00237 #endif