1*9880d681SAndroid Build Coastguard Worker //===---- LiveRangeCalc.h - Calculate live ranges ---------------*- C++ -*-===// 2*9880d681SAndroid Build Coastguard Worker // 3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure 4*9880d681SAndroid Build Coastguard Worker // 5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source 6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details. 7*9880d681SAndroid Build Coastguard Worker // 8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===// 9*9880d681SAndroid Build Coastguard Worker // 10*9880d681SAndroid Build Coastguard Worker // The LiveRangeCalc class can be used to compute live ranges from scratch. It 11*9880d681SAndroid Build Coastguard Worker // caches information about values in the CFG to speed up repeated operations 12*9880d681SAndroid Build Coastguard Worker // on the same live range. The cache can be shared by non-overlapping live 13*9880d681SAndroid Build Coastguard Worker // ranges. SplitKit uses that when computing the live range of split products. 14*9880d681SAndroid Build Coastguard Worker // 15*9880d681SAndroid Build Coastguard Worker // A low-level interface is available to clients that know where a variable is 16*9880d681SAndroid Build Coastguard Worker // live, but don't know which value it has as every point. LiveRangeCalc will 17*9880d681SAndroid Build Coastguard Worker // propagate values down the dominator tree, and even insert PHI-defs where 18*9880d681SAndroid Build Coastguard Worker // needed. SplitKit uses this faster interface when possible. 19*9880d681SAndroid Build Coastguard Worker // 20*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===// 21*9880d681SAndroid Build Coastguard Worker 22*9880d681SAndroid Build Coastguard Worker #ifndef LLVM_LIB_CODEGEN_LIVERANGECALC_H 23*9880d681SAndroid Build Coastguard Worker #define LLVM_LIB_CODEGEN_LIVERANGECALC_H 24*9880d681SAndroid Build Coastguard Worker 25*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/BitVector.h" 26*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/IndexedMap.h" 27*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/LiveInterval.h" 28*9880d681SAndroid Build Coastguard Worker 29*9880d681SAndroid Build Coastguard Worker namespace llvm { 30*9880d681SAndroid Build Coastguard Worker 31*9880d681SAndroid Build Coastguard Worker /// Forward declarations for MachineDominators.h: 32*9880d681SAndroid Build Coastguard Worker class MachineDominatorTree; 33*9880d681SAndroid Build Coastguard Worker template <class NodeT> class DomTreeNodeBase; 34*9880d681SAndroid Build Coastguard Worker typedef DomTreeNodeBase<MachineBasicBlock> MachineDomTreeNode; 35*9880d681SAndroid Build Coastguard Worker 36*9880d681SAndroid Build Coastguard Worker class LiveRangeCalc { 37*9880d681SAndroid Build Coastguard Worker const MachineFunction *MF; 38*9880d681SAndroid Build Coastguard Worker const MachineRegisterInfo *MRI; 39*9880d681SAndroid Build Coastguard Worker SlotIndexes *Indexes; 40*9880d681SAndroid Build Coastguard Worker MachineDominatorTree *DomTree; 41*9880d681SAndroid Build Coastguard Worker VNInfo::Allocator *Alloc; 42*9880d681SAndroid Build Coastguard Worker 43*9880d681SAndroid Build Coastguard Worker /// LiveOutPair - A value and the block that defined it. The domtree node is 44*9880d681SAndroid Build Coastguard Worker /// redundant, it can be computed as: MDT[Indexes.getMBBFromIndex(VNI->def)]. 45*9880d681SAndroid Build Coastguard Worker typedef std::pair<VNInfo*, MachineDomTreeNode*> LiveOutPair; 46*9880d681SAndroid Build Coastguard Worker 47*9880d681SAndroid Build Coastguard Worker /// LiveOutMap - Map basic blocks to the value leaving the block. 48*9880d681SAndroid Build Coastguard Worker typedef IndexedMap<LiveOutPair, MBB2NumberFunctor> LiveOutMap; 49*9880d681SAndroid Build Coastguard Worker 50*9880d681SAndroid Build Coastguard Worker /// Bit vector of active entries in LiveOut, also used as a visited set by 51*9880d681SAndroid Build Coastguard Worker /// findReachingDefs. One entry per basic block, indexed by block number. 52*9880d681SAndroid Build Coastguard Worker /// This is kept as a separate bit vector because it can be cleared quickly 53*9880d681SAndroid Build Coastguard Worker /// when switching live ranges. 54*9880d681SAndroid Build Coastguard Worker BitVector Seen; 55*9880d681SAndroid Build Coastguard Worker 56*9880d681SAndroid Build Coastguard Worker /// Map each basic block where a live range is live out to the live-out value 57*9880d681SAndroid Build Coastguard Worker /// and its defining block. 58*9880d681SAndroid Build Coastguard Worker /// 59*9880d681SAndroid Build Coastguard Worker /// For every basic block, MBB, one of these conditions shall be true: 60*9880d681SAndroid Build Coastguard Worker /// 61*9880d681SAndroid Build Coastguard Worker /// 1. !Seen.count(MBB->getNumber()) 62*9880d681SAndroid Build Coastguard Worker /// Blocks without a Seen bit are ignored. 63*9880d681SAndroid Build Coastguard Worker /// 2. LiveOut[MBB].second.getNode() == MBB 64*9880d681SAndroid Build Coastguard Worker /// The live-out value is defined in MBB. 65*9880d681SAndroid Build Coastguard Worker /// 3. forall P in preds(MBB): LiveOut[P] == LiveOut[MBB] 66*9880d681SAndroid Build Coastguard Worker /// The live-out value passses through MBB. All predecessors must carry 67*9880d681SAndroid Build Coastguard Worker /// the same value. 68*9880d681SAndroid Build Coastguard Worker /// 69*9880d681SAndroid Build Coastguard Worker /// The domtree node may be null, it can be computed. 70*9880d681SAndroid Build Coastguard Worker /// 71*9880d681SAndroid Build Coastguard Worker /// The map can be shared by multiple live ranges as long as no two are 72*9880d681SAndroid Build Coastguard Worker /// live-out of the same block. 73*9880d681SAndroid Build Coastguard Worker LiveOutMap Map; 74*9880d681SAndroid Build Coastguard Worker 75*9880d681SAndroid Build Coastguard Worker /// LiveInBlock - Information about a basic block where a live range is known 76*9880d681SAndroid Build Coastguard Worker /// to be live-in, but the value has not yet been determined. 77*9880d681SAndroid Build Coastguard Worker struct LiveInBlock { 78*9880d681SAndroid Build Coastguard Worker // The live range set that is live-in to this block. The algorithms can 79*9880d681SAndroid Build Coastguard Worker // handle multiple non-overlapping live ranges simultaneously. 80*9880d681SAndroid Build Coastguard Worker LiveRange &LR; 81*9880d681SAndroid Build Coastguard Worker 82*9880d681SAndroid Build Coastguard Worker // DomNode - Dominator tree node for the block. 83*9880d681SAndroid Build Coastguard Worker // Cleared when the final value has been determined and LI has been updated. 84*9880d681SAndroid Build Coastguard Worker MachineDomTreeNode *DomNode; 85*9880d681SAndroid Build Coastguard Worker 86*9880d681SAndroid Build Coastguard Worker // Position in block where the live-in range ends, or SlotIndex() if the 87*9880d681SAndroid Build Coastguard Worker // range passes through the block. When the final value has been 88*9880d681SAndroid Build Coastguard Worker // determined, the range from the block start to Kill will be added to LI. 89*9880d681SAndroid Build Coastguard Worker SlotIndex Kill; 90*9880d681SAndroid Build Coastguard Worker 91*9880d681SAndroid Build Coastguard Worker // Live-in value filled in by updateSSA once it is known. 92*9880d681SAndroid Build Coastguard Worker VNInfo *Value; 93*9880d681SAndroid Build Coastguard Worker LiveInBlockLiveInBlock94*9880d681SAndroid Build Coastguard Worker LiveInBlock(LiveRange &LR, MachineDomTreeNode *node, SlotIndex kill) 95*9880d681SAndroid Build Coastguard Worker : LR(LR), DomNode(node), Kill(kill), Value(nullptr) {} 96*9880d681SAndroid Build Coastguard Worker }; 97*9880d681SAndroid Build Coastguard Worker 98*9880d681SAndroid Build Coastguard Worker /// LiveIn - Work list of blocks where the live-in value has yet to be 99*9880d681SAndroid Build Coastguard Worker /// determined. This list is typically computed by findReachingDefs() and 100*9880d681SAndroid Build Coastguard Worker /// used as a work list by updateSSA(). The low-level interface may also be 101*9880d681SAndroid Build Coastguard Worker /// used to add entries directly. 102*9880d681SAndroid Build Coastguard Worker SmallVector<LiveInBlock, 16> LiveIn; 103*9880d681SAndroid Build Coastguard Worker 104*9880d681SAndroid Build Coastguard Worker /// Assuming that @p LR is live-in to @p UseMBB, find the set of defs that can 105*9880d681SAndroid Build Coastguard Worker /// reach it. 106*9880d681SAndroid Build Coastguard Worker /// 107*9880d681SAndroid Build Coastguard Worker /// If only one def can reach @p UseMBB, all paths from the def to @p UseMBB 108*9880d681SAndroid Build Coastguard Worker /// are added to @p LR, and the function returns true. 109*9880d681SAndroid Build Coastguard Worker /// 110*9880d681SAndroid Build Coastguard Worker /// If multiple values can reach @p UseMBB, the blocks that need @p LR to be 111*9880d681SAndroid Build Coastguard Worker /// live in are added to the LiveIn array, and the function returns false. 112*9880d681SAndroid Build Coastguard Worker /// 113*9880d681SAndroid Build Coastguard Worker /// PhysReg, when set, is used to verify live-in lists on basic blocks. 114*9880d681SAndroid Build Coastguard Worker bool findReachingDefs(LiveRange &LR, MachineBasicBlock &UseMBB, 115*9880d681SAndroid Build Coastguard Worker SlotIndex Kill, unsigned PhysReg); 116*9880d681SAndroid Build Coastguard Worker 117*9880d681SAndroid Build Coastguard Worker /// updateSSA - Compute the values that will be live in to all requested 118*9880d681SAndroid Build Coastguard Worker /// blocks in LiveIn. Create PHI-def values as required to preserve SSA form. 119*9880d681SAndroid Build Coastguard Worker /// 120*9880d681SAndroid Build Coastguard Worker /// Every live-in block must be jointly dominated by the added live-out 121*9880d681SAndroid Build Coastguard Worker /// blocks. No values are read from the live ranges. 122*9880d681SAndroid Build Coastguard Worker void updateSSA(); 123*9880d681SAndroid Build Coastguard Worker 124*9880d681SAndroid Build Coastguard Worker /// Transfer information from the LiveIn vector to the live ranges and update 125*9880d681SAndroid Build Coastguard Worker /// the given @p LiveOuts. 126*9880d681SAndroid Build Coastguard Worker void updateFromLiveIns(); 127*9880d681SAndroid Build Coastguard Worker 128*9880d681SAndroid Build Coastguard Worker /// Extend the live range of @p LR to reach all uses of Reg. 129*9880d681SAndroid Build Coastguard Worker /// 130*9880d681SAndroid Build Coastguard Worker /// All uses must be jointly dominated by existing liveness. PHI-defs are 131*9880d681SAndroid Build Coastguard Worker /// inserted as needed to preserve SSA form. 132*9880d681SAndroid Build Coastguard Worker void extendToUses(LiveRange &LR, unsigned Reg, LaneBitmask LaneMask); 133*9880d681SAndroid Build Coastguard Worker 134*9880d681SAndroid Build Coastguard Worker /// Reset Map and Seen fields. 135*9880d681SAndroid Build Coastguard Worker void resetLiveOutMap(); 136*9880d681SAndroid Build Coastguard Worker 137*9880d681SAndroid Build Coastguard Worker public: LiveRangeCalc()138*9880d681SAndroid Build Coastguard Worker LiveRangeCalc() : MF(nullptr), MRI(nullptr), Indexes(nullptr), 139*9880d681SAndroid Build Coastguard Worker DomTree(nullptr), Alloc(nullptr) {} 140*9880d681SAndroid Build Coastguard Worker 141*9880d681SAndroid Build Coastguard Worker //===--------------------------------------------------------------------===// 142*9880d681SAndroid Build Coastguard Worker // High-level interface. 143*9880d681SAndroid Build Coastguard Worker //===--------------------------------------------------------------------===// 144*9880d681SAndroid Build Coastguard Worker // 145*9880d681SAndroid Build Coastguard Worker // Calculate live ranges from scratch. 146*9880d681SAndroid Build Coastguard Worker // 147*9880d681SAndroid Build Coastguard Worker 148*9880d681SAndroid Build Coastguard Worker /// reset - Prepare caches for a new set of non-overlapping live ranges. The 149*9880d681SAndroid Build Coastguard Worker /// caches must be reset before attempting calculations with a live range 150*9880d681SAndroid Build Coastguard Worker /// that may overlap a previously computed live range, and before the first 151*9880d681SAndroid Build Coastguard Worker /// live range in a function. If live ranges are not known to be 152*9880d681SAndroid Build Coastguard Worker /// non-overlapping, call reset before each. 153*9880d681SAndroid Build Coastguard Worker void reset(const MachineFunction *MF, 154*9880d681SAndroid Build Coastguard Worker SlotIndexes*, 155*9880d681SAndroid Build Coastguard Worker MachineDominatorTree*, 156*9880d681SAndroid Build Coastguard Worker VNInfo::Allocator*); 157*9880d681SAndroid Build Coastguard Worker 158*9880d681SAndroid Build Coastguard Worker //===--------------------------------------------------------------------===// 159*9880d681SAndroid Build Coastguard Worker // Mid-level interface. 160*9880d681SAndroid Build Coastguard Worker //===--------------------------------------------------------------------===// 161*9880d681SAndroid Build Coastguard Worker // 162*9880d681SAndroid Build Coastguard Worker // Modify existing live ranges. 163*9880d681SAndroid Build Coastguard Worker // 164*9880d681SAndroid Build Coastguard Worker 165*9880d681SAndroid Build Coastguard Worker /// Extend the live range of @p LR to reach @p Use. 166*9880d681SAndroid Build Coastguard Worker /// 167*9880d681SAndroid Build Coastguard Worker /// The existing values in @p LR must be live so they jointly dominate @p Use. 168*9880d681SAndroid Build Coastguard Worker /// If @p Use is not dominated by a single existing value, PHI-defs are 169*9880d681SAndroid Build Coastguard Worker /// inserted as required to preserve SSA form. 170*9880d681SAndroid Build Coastguard Worker /// 171*9880d681SAndroid Build Coastguard Worker /// PhysReg, when set, is used to verify live-in lists on basic blocks. 172*9880d681SAndroid Build Coastguard Worker void extend(LiveRange &LR, SlotIndex Use, unsigned PhysReg = 0); 173*9880d681SAndroid Build Coastguard Worker 174*9880d681SAndroid Build Coastguard Worker /// createDeadDefs - Create a dead def in LI for every def operand of Reg. 175*9880d681SAndroid Build Coastguard Worker /// Each instruction defining Reg gets a new VNInfo with a corresponding 176*9880d681SAndroid Build Coastguard Worker /// minimal live range. 177*9880d681SAndroid Build Coastguard Worker void createDeadDefs(LiveRange &LR, unsigned Reg); 178*9880d681SAndroid Build Coastguard Worker 179*9880d681SAndroid Build Coastguard Worker /// Extend the live range of @p LR to reach all uses of Reg. 180*9880d681SAndroid Build Coastguard Worker /// 181*9880d681SAndroid Build Coastguard Worker /// All uses must be jointly dominated by existing liveness. PHI-defs are 182*9880d681SAndroid Build Coastguard Worker /// inserted as needed to preserve SSA form. extendToUses(LiveRange & LR,unsigned PhysReg)183*9880d681SAndroid Build Coastguard Worker void extendToUses(LiveRange &LR, unsigned PhysReg) { 184*9880d681SAndroid Build Coastguard Worker extendToUses(LR, PhysReg, ~0u); 185*9880d681SAndroid Build Coastguard Worker } 186*9880d681SAndroid Build Coastguard Worker 187*9880d681SAndroid Build Coastguard Worker /// Calculates liveness for the register specified in live interval @p LI. 188*9880d681SAndroid Build Coastguard Worker /// Creates subregister live ranges as needed if subreg liveness tracking is 189*9880d681SAndroid Build Coastguard Worker /// enabled. 190*9880d681SAndroid Build Coastguard Worker void calculate(LiveInterval &LI, bool TrackSubRegs); 191*9880d681SAndroid Build Coastguard Worker 192*9880d681SAndroid Build Coastguard Worker /// For live interval \p LI with correct SubRanges construct matching 193*9880d681SAndroid Build Coastguard Worker /// information for the main live range. Expects the main live range to not 194*9880d681SAndroid Build Coastguard Worker /// have any segments or value numbers. 195*9880d681SAndroid Build Coastguard Worker void constructMainRangeFromSubranges(LiveInterval &LI); 196*9880d681SAndroid Build Coastguard Worker 197*9880d681SAndroid Build Coastguard Worker //===--------------------------------------------------------------------===// 198*9880d681SAndroid Build Coastguard Worker // Low-level interface. 199*9880d681SAndroid Build Coastguard Worker //===--------------------------------------------------------------------===// 200*9880d681SAndroid Build Coastguard Worker // 201*9880d681SAndroid Build Coastguard Worker // These functions can be used to compute live ranges where the live-in and 202*9880d681SAndroid Build Coastguard Worker // live-out blocks are already known, but the SSA value in each block is 203*9880d681SAndroid Build Coastguard Worker // unknown. 204*9880d681SAndroid Build Coastguard Worker // 205*9880d681SAndroid Build Coastguard Worker // After calling reset(), add known live-out values and known live-in blocks. 206*9880d681SAndroid Build Coastguard Worker // Then call calculateValues() to compute the actual value that is 207*9880d681SAndroid Build Coastguard Worker // live-in to each block, and add liveness to the live ranges. 208*9880d681SAndroid Build Coastguard Worker // 209*9880d681SAndroid Build Coastguard Worker 210*9880d681SAndroid Build Coastguard Worker /// setLiveOutValue - Indicate that VNI is live out from MBB. The 211*9880d681SAndroid Build Coastguard Worker /// calculateValues() function will not add liveness for MBB, the caller 212*9880d681SAndroid Build Coastguard Worker /// should take care of that. 213*9880d681SAndroid Build Coastguard Worker /// 214*9880d681SAndroid Build Coastguard Worker /// VNI may be null only if MBB is a live-through block also passed to 215*9880d681SAndroid Build Coastguard Worker /// addLiveInBlock(). setLiveOutValue(MachineBasicBlock * MBB,VNInfo * VNI)216*9880d681SAndroid Build Coastguard Worker void setLiveOutValue(MachineBasicBlock *MBB, VNInfo *VNI) { 217*9880d681SAndroid Build Coastguard Worker Seen.set(MBB->getNumber()); 218*9880d681SAndroid Build Coastguard Worker Map[MBB] = LiveOutPair(VNI, nullptr); 219*9880d681SAndroid Build Coastguard Worker } 220*9880d681SAndroid Build Coastguard Worker 221*9880d681SAndroid Build Coastguard Worker /// addLiveInBlock - Add a block with an unknown live-in value. This 222*9880d681SAndroid Build Coastguard Worker /// function can only be called once per basic block. Once the live-in value 223*9880d681SAndroid Build Coastguard Worker /// has been determined, calculateValues() will add liveness to LI. 224*9880d681SAndroid Build Coastguard Worker /// 225*9880d681SAndroid Build Coastguard Worker /// @param LR The live range that is live-in to the block. 226*9880d681SAndroid Build Coastguard Worker /// @param DomNode The domtree node for the block. 227*9880d681SAndroid Build Coastguard Worker /// @param Kill Index in block where LI is killed. If the value is 228*9880d681SAndroid Build Coastguard Worker /// live-through, set Kill = SLotIndex() and also call 229*9880d681SAndroid Build Coastguard Worker /// setLiveOutValue(MBB, 0). 230*9880d681SAndroid Build Coastguard Worker void addLiveInBlock(LiveRange &LR, 231*9880d681SAndroid Build Coastguard Worker MachineDomTreeNode *DomNode, 232*9880d681SAndroid Build Coastguard Worker SlotIndex Kill = SlotIndex()) { 233*9880d681SAndroid Build Coastguard Worker LiveIn.push_back(LiveInBlock(LR, DomNode, Kill)); 234*9880d681SAndroid Build Coastguard Worker } 235*9880d681SAndroid Build Coastguard Worker 236*9880d681SAndroid Build Coastguard Worker /// calculateValues - Calculate the value that will be live-in to each block 237*9880d681SAndroid Build Coastguard Worker /// added with addLiveInBlock. Add PHI-def values as needed to preserve SSA 238*9880d681SAndroid Build Coastguard Worker /// form. Add liveness to all live-in blocks up to the Kill point, or the 239*9880d681SAndroid Build Coastguard Worker /// whole block for live-through blocks. 240*9880d681SAndroid Build Coastguard Worker /// 241*9880d681SAndroid Build Coastguard Worker /// Every predecessor of a live-in block must have been given a value with 242*9880d681SAndroid Build Coastguard Worker /// setLiveOutValue, the value may be null for live-trough blocks. 243*9880d681SAndroid Build Coastguard Worker void calculateValues(); 244*9880d681SAndroid Build Coastguard Worker }; 245*9880d681SAndroid Build Coastguard Worker 246*9880d681SAndroid Build Coastguard Worker } // end namespace llvm 247*9880d681SAndroid Build Coastguard Worker 248*9880d681SAndroid Build Coastguard Worker #endif 249