1226584Sdim//===---- LiveRangeCalc.h - Calculate live ranges ---------------*- C++ -*-===//
2226584Sdim//
3226584Sdim//                     The LLVM Compiler Infrastructure
4226584Sdim//
5226584Sdim// This file is distributed under the University of Illinois Open Source
6226584Sdim// License. See LICENSE.TXT for details.
7226584Sdim//
8226584Sdim//===----------------------------------------------------------------------===//
9226584Sdim//
10226584Sdim// The LiveRangeCalc class can be used to compute live ranges from scratch.  It
11226584Sdim// caches information about values in the CFG to speed up repeated operations
12226584Sdim// on the same live range.  The cache can be shared by non-overlapping live
13226584Sdim// ranges.  SplitKit uses that when computing the live range of split products.
14226584Sdim//
15226584Sdim// A low-level interface is available to clients that know where a variable is
16226584Sdim// live, but don't know which value it has as every point.  LiveRangeCalc will
17226584Sdim// propagate values down the dominator tree, and even insert PHI-defs where
18226584Sdim// needed.  SplitKit uses this faster interface when possible.
19226584Sdim//
20226584Sdim//===----------------------------------------------------------------------===//
21226584Sdim
22226584Sdim#ifndef LLVM_CODEGEN_LIVERANGECALC_H
23226584Sdim#define LLVM_CODEGEN_LIVERANGECALC_H
24226584Sdim
25226584Sdim#include "llvm/ADT/BitVector.h"
26226584Sdim#include "llvm/ADT/IndexedMap.h"
27226584Sdim#include "llvm/CodeGen/LiveInterval.h"
28226584Sdim
29226584Sdimnamespace llvm {
30226584Sdim
31226584Sdim/// Forward declarations for MachineDominators.h:
32226584Sdimclass MachineDominatorTree;
33226584Sdimtemplate <class NodeT> class DomTreeNodeBase;
34226584Sdimtypedef DomTreeNodeBase<MachineBasicBlock> MachineDomTreeNode;
35226584Sdim
36226584Sdimclass LiveRangeCalc {
37249423Sdim  const MachineFunction *MF;
38239462Sdim  const MachineRegisterInfo *MRI;
39239462Sdim  SlotIndexes *Indexes;
40239462Sdim  MachineDominatorTree *DomTree;
41239462Sdim  VNInfo::Allocator *Alloc;
42239462Sdim
43226584Sdim  /// Seen - Bit vector of active entries in LiveOut, also used as a visited
44226584Sdim  /// set by findReachingDefs.  One entry per basic block, indexed by block
45226584Sdim  /// number.  This is kept as a separate bit vector because it can be cleared
46226584Sdim  /// quickly when switching live ranges.
47226584Sdim  BitVector Seen;
48226584Sdim
49226584Sdim  /// LiveOutPair - A value and the block that defined it.  The domtree node is
50226584Sdim  /// redundant, it can be computed as: MDT[Indexes.getMBBFromIndex(VNI->def)].
51226584Sdim  typedef std::pair<VNInfo*, MachineDomTreeNode*> LiveOutPair;
52226584Sdim
53226584Sdim  /// LiveOutMap - Map basic blocks to the value leaving the block.
54226584Sdim  typedef IndexedMap<LiveOutPair, MBB2NumberFunctor> LiveOutMap;
55226584Sdim
56226584Sdim  /// LiveOut - Map each basic block where a live range is live out to the
57226584Sdim  /// live-out value and its defining block.
58226584Sdim  ///
59226584Sdim  /// For every basic block, MBB, one of these conditions shall be true:
60226584Sdim  ///
61226584Sdim  ///  1. !Seen.count(MBB->getNumber())
62226584Sdim  ///     Blocks without a Seen bit are ignored.
63226584Sdim  ///  2. LiveOut[MBB].second.getNode() == MBB
64226584Sdim  ///     The live-out value is defined in MBB.
65226584Sdim  ///  3. forall P in preds(MBB): LiveOut[P] == LiveOut[MBB]
66226584Sdim  ///     The live-out value passses through MBB. All predecessors must carry
67226584Sdim  ///     the same value.
68226584Sdim  ///
69226584Sdim  /// The domtree node may be null, it can be computed.
70226584Sdim  ///
71226584Sdim  /// The map can be shared by multiple live ranges as long as no two are
72226584Sdim  /// live-out of the same block.
73226584Sdim  LiveOutMap LiveOut;
74226584Sdim
75226584Sdim  /// LiveInBlock - Information about a basic block where a live range is known
76226584Sdim  /// to be live-in, but the value has not yet been determined.
77226584Sdim  struct LiveInBlock {
78263508Sdim    // The live range set that is live-in to this block.  The algorithms can
79226584Sdim    // handle multiple non-overlapping live ranges simultaneously.
80263508Sdim    LiveRange &LR;
81226584Sdim
82226584Sdim    // DomNode - Dominator tree node for the block.
83226584Sdim    // Cleared when the final value has been determined and LI has been updated.
84226584Sdim    MachineDomTreeNode *DomNode;
85226584Sdim
86226584Sdim    // Position in block where the live-in range ends, or SlotIndex() if the
87226584Sdim    // range passes through the block.  When the final value has been
88226584Sdim    // determined, the range from the block start to Kill will be added to LI.
89226584Sdim    SlotIndex Kill;
90226584Sdim
91226584Sdim    // Live-in value filled in by updateSSA once it is known.
92226584Sdim    VNInfo *Value;
93226584Sdim
94263508Sdim    LiveInBlock(LiveRange &LR, MachineDomTreeNode *node, SlotIndex kill)
95263508Sdim      : LR(LR), DomNode(node), Kill(kill), Value(0) {}
96226584Sdim  };
97226584Sdim
98226584Sdim  /// LiveIn - Work list of blocks where the live-in value has yet to be
99226584Sdim  /// determined.  This list is typically computed by findReachingDefs() and
100226584Sdim  /// used as a work list by updateSSA().  The low-level interface may also be
101226584Sdim  /// used to add entries directly.
102226584Sdim  SmallVector<LiveInBlock, 16> LiveIn;
103226584Sdim
104249423Sdim  /// Assuming that LI is live-in to KillMBB and killed at Kill, find the set
105249423Sdim  /// of defs that can reach it.
106239462Sdim  ///
107249423Sdim  /// If only one def can reach Kill, all paths from the def to kill are added
108249423Sdim  /// to LI, and the function returns true.
109249423Sdim  ///
110249423Sdim  /// If multiple values can reach Kill, the blocks that need LI to be live in
111249423Sdim  /// are added to the LiveIn array, and the function returns false.
112249423Sdim  ///
113239462Sdim  /// PhysReg, when set, is used to verify live-in lists on basic blocks.
114263508Sdim  bool findReachingDefs(LiveRange &LR, MachineBasicBlock &KillMBB,
115263508Sdim                        SlotIndex Kill, unsigned PhysReg);
116226584Sdim
117226584Sdim  /// updateSSA - Compute the values that will be live in to all requested
118226584Sdim  /// blocks in LiveIn.  Create PHI-def values as required to preserve SSA form.
119226584Sdim  ///
120226584Sdim  /// Every live-in block must be jointly dominated by the added live-out
121226584Sdim  /// blocks.  No values are read from the live ranges.
122239462Sdim  void updateSSA();
123226584Sdim
124249423Sdim  /// Add liveness as specified in the LiveIn vector.
125249423Sdim  void updateLiveIns();
126226584Sdim
127226584Sdimpublic:
128249423Sdim  LiveRangeCalc() : MF(0), MRI(0), Indexes(0), DomTree(0), Alloc(0) {}
129239462Sdim
130226584Sdim  //===--------------------------------------------------------------------===//
131226584Sdim  // High-level interface.
132226584Sdim  //===--------------------------------------------------------------------===//
133226584Sdim  //
134226584Sdim  // Calculate live ranges from scratch.
135226584Sdim  //
136226584Sdim
137226584Sdim  /// reset - Prepare caches for a new set of non-overlapping live ranges.  The
138226584Sdim  /// caches must be reset before attempting calculations with a live range
139226584Sdim  /// that may overlap a previously computed live range, and before the first
140226584Sdim  /// live range in a function.  If live ranges are not known to be
141226584Sdim  /// non-overlapping, call reset before each.
142239462Sdim  void reset(const MachineFunction *MF,
143239462Sdim             SlotIndexes*,
144239462Sdim             MachineDominatorTree*,
145239462Sdim             VNInfo::Allocator*);
146226584Sdim
147226584Sdim  //===--------------------------------------------------------------------===//
148226584Sdim  // Mid-level interface.
149226584Sdim  //===--------------------------------------------------------------------===//
150226584Sdim  //
151226584Sdim  // Modify existing live ranges.
152226584Sdim  //
153226584Sdim
154226584Sdim  /// extend - Extend the live range of LI to reach Kill.
155226584Sdim  ///
156226584Sdim  /// The existing values in LI must be live so they jointly dominate Kill.  If
157226584Sdim  /// Kill is not dominated by a single existing value, PHI-defs are inserted
158226584Sdim  /// as required to preserve SSA form.  If Kill is known to be dominated by a
159226584Sdim  /// single existing value, Alloc may be null.
160239462Sdim  ///
161239462Sdim  /// PhysReg, when set, is used to verify live-in lists on basic blocks.
162263508Sdim  void extend(LiveRange &LR, SlotIndex Kill, unsigned PhysReg = 0);
163226584Sdim
164239462Sdim  /// createDeadDefs - Create a dead def in LI for every def operand of Reg.
165239462Sdim  /// Each instruction defining Reg gets a new VNInfo with a corresponding
166239462Sdim  /// minimal live range.
167263508Sdim  void createDeadDefs(LiveRange &LR, unsigned Reg);
168239462Sdim
169239462Sdim  /// createDeadDefs - Create a dead def in LI for every def of LI->reg.
170263508Sdim  void createDeadDefs(LiveInterval &LI) {
171263508Sdim    createDeadDefs(LI, LI.reg);
172239462Sdim  }
173239462Sdim
174239462Sdim  /// extendToUses - Extend the live range of LI to reach all uses of Reg.
175226584Sdim  ///
176226584Sdim  /// All uses must be jointly dominated by existing liveness.  PHI-defs are
177226584Sdim  /// inserted as needed to preserve SSA form.
178263508Sdim  void extendToUses(LiveRange &LR, unsigned Reg);
179226584Sdim
180239462Sdim  /// extendToUses - Extend the live range of LI to reach all uses of LI->reg.
181263508Sdim  void extendToUses(LiveInterval &LI) {
182263508Sdim    extendToUses(LI, LI.reg);
183239462Sdim  }
184239462Sdim
185226584Sdim  //===--------------------------------------------------------------------===//
186226584Sdim  // Low-level interface.
187226584Sdim  //===--------------------------------------------------------------------===//
188226584Sdim  //
189226584Sdim  // These functions can be used to compute live ranges where the live-in and
190226584Sdim  // live-out blocks are already known, but the SSA value in each block is
191226584Sdim  // unknown.
192226584Sdim  //
193226584Sdim  // After calling reset(), add known live-out values and known live-in blocks.
194226584Sdim  // Then call calculateValues() to compute the actual value that is
195226584Sdim  // live-in to each block, and add liveness to the live ranges.
196226584Sdim  //
197226584Sdim
198226584Sdim  /// setLiveOutValue - Indicate that VNI is live out from MBB.  The
199226584Sdim  /// calculateValues() function will not add liveness for MBB, the caller
200226584Sdim  /// should take care of that.
201226584Sdim  ///
202226584Sdim  /// VNI may be null only if MBB is a live-through block also passed to
203226584Sdim  /// addLiveInBlock().
204226584Sdim  void setLiveOutValue(MachineBasicBlock *MBB, VNInfo *VNI) {
205226584Sdim    Seen.set(MBB->getNumber());
206226584Sdim    LiveOut[MBB] = LiveOutPair(VNI, (MachineDomTreeNode *)0);
207226584Sdim  }
208226584Sdim
209226584Sdim  /// addLiveInBlock - Add a block with an unknown live-in value.  This
210226584Sdim  /// function can only be called once per basic block.  Once the live-in value
211226584Sdim  /// has been determined, calculateValues() will add liveness to LI.
212226584Sdim  ///
213263508Sdim  /// @param LR      The live range that is live-in to the block.
214226584Sdim  /// @param DomNode The domtree node for the block.
215226584Sdim  /// @param Kill    Index in block where LI is killed.  If the value is
216226584Sdim  ///                live-through, set Kill = SLotIndex() and also call
217226584Sdim  ///                setLiveOutValue(MBB, 0).
218263508Sdim  void addLiveInBlock(LiveRange &LR,
219226584Sdim                      MachineDomTreeNode *DomNode,
220226584Sdim                      SlotIndex Kill = SlotIndex()) {
221263508Sdim    LiveIn.push_back(LiveInBlock(LR, DomNode, Kill));
222226584Sdim  }
223226584Sdim
224226584Sdim  /// calculateValues - Calculate the value that will be live-in to each block
225226584Sdim  /// added with addLiveInBlock.  Add PHI-def values as needed to preserve SSA
226226584Sdim  /// form.  Add liveness to all live-in blocks up to the Kill point, or the
227226584Sdim  /// whole block for live-through blocks.
228226584Sdim  ///
229226584Sdim  /// Every predecessor of a live-in block must have been given a value with
230226584Sdim  /// setLiveOutValue, the value may be null for live-trough blocks.
231239462Sdim  void calculateValues();
232226584Sdim};
233226584Sdim
234226584Sdim} // end namespace llvm
235226584Sdim
236226584Sdim#endif
237