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