Revert "Give internal classes hidden visibility."
[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_CODEGEN_LIVERANGECALC_H
23 #define LLVM_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   /// Seen - Bit vector of active entries in LiveOut, also used as a visited
44   /// set by findReachingDefs.  One entry per basic block, indexed by block
45   /// number.  This is kept as a separate bit vector because it can be cleared
46   /// quickly when switching live ranges.
47   BitVector Seen;
48
49   /// LiveOutPair - A value and the block that defined it.  The domtree node is
50   /// redundant, it can be computed as: MDT[Indexes.getMBBFromIndex(VNI->def)].
51   typedef std::pair<VNInfo*, MachineDomTreeNode*> LiveOutPair;
52
53   /// LiveOutMap - Map basic blocks to the value leaving the block.
54   typedef IndexedMap<LiveOutPair, MBB2NumberFunctor> LiveOutMap;
55
56   /// LiveOut - Map each basic block where a live range is live out to the
57   /// live-out value and its defining block.
58   ///
59   /// For every basic block, MBB, one of these conditions shall be true:
60   ///
61   ///  1. !Seen.count(MBB->getNumber())
62   ///     Blocks without a Seen bit are ignored.
63   ///  2. LiveOut[MBB].second.getNode() == MBB
64   ///     The live-out value is defined in MBB.
65   ///  3. forall P in preds(MBB): LiveOut[P] == LiveOut[MBB]
66   ///     The live-out value passses through MBB. All predecessors must carry
67   ///     the same value.
68   ///
69   /// The domtree node may be null, it can be computed.
70   ///
71   /// The map can be shared by multiple live ranges as long as no two are
72   /// live-out of the same block.
73   LiveOutMap LiveOut;
74
75   /// LiveInBlock - Information about a basic block where a live range is known
76   /// to be live-in, but the value has not yet been determined.
77   struct LiveInBlock {
78     // LI - The live range that is live-in to this block.  The algorithms can
79     // handle multiple non-overlapping live ranges simultaneously.
80     LiveInterval *LI;
81
82     // DomNode - Dominator tree node for the block.
83     // Cleared when the final value has been determined and LI has been updated.
84     MachineDomTreeNode *DomNode;
85
86     // Position in block where the live-in range ends, or SlotIndex() if the
87     // range passes through the block.  When the final value has been
88     // determined, the range from the block start to Kill will be added to LI.
89     SlotIndex Kill;
90
91     // Live-in value filled in by updateSSA once it is known.
92     VNInfo *Value;
93
94     LiveInBlock(LiveInterval *li, MachineDomTreeNode *node, SlotIndex kill)
95       : LI(li), DomNode(node), Kill(kill), Value(0) {}
96   };
97
98   /// LiveIn - Work list of blocks where the live-in value has yet to be
99   /// determined.  This list is typically computed by findReachingDefs() and
100   /// used as a work list by updateSSA().  The low-level interface may also be
101   /// used to add entries directly.
102   SmallVector<LiveInBlock, 16> LiveIn;
103
104   /// Assuming that LI is live-in to KillMBB and killed at Kill, find the set
105   /// of defs that can reach it.
106   ///
107   /// If only one def can reach Kill, all paths from the def to kill are added
108   /// to LI, and the function returns true.
109   ///
110   /// If multiple values can reach Kill, the blocks that need LI to be live in
111   /// are added to the LiveIn array, and the function returns false.
112   ///
113   /// PhysReg, when set, is used to verify live-in lists on basic blocks.
114   bool findReachingDefs(LiveInterval *LI,
115                         MachineBasicBlock *KillMBB,
116                         SlotIndex Kill,
117                         unsigned PhysReg);
118
119   /// updateSSA - Compute the values that will be live in to all requested
120   /// blocks in LiveIn.  Create PHI-def values as required to preserve SSA form.
121   ///
122   /// Every live-in block must be jointly dominated by the added live-out
123   /// blocks.  No values are read from the live ranges.
124   void updateSSA();
125
126   /// Add liveness as specified in the LiveIn vector.
127   void updateLiveIns();
128
129 public:
130   LiveRangeCalc() : MF(0), MRI(0), Indexes(0), DomTree(0), Alloc(0) {}
131
132   //===--------------------------------------------------------------------===//
133   // High-level interface.
134   //===--------------------------------------------------------------------===//
135   //
136   // Calculate live ranges from scratch.
137   //
138
139   /// reset - Prepare caches for a new set of non-overlapping live ranges.  The
140   /// caches must be reset before attempting calculations with a live range
141   /// that may overlap a previously computed live range, and before the first
142   /// live range in a function.  If live ranges are not known to be
143   /// non-overlapping, call reset before each.
144   void reset(const MachineFunction *MF,
145              SlotIndexes*,
146              MachineDominatorTree*,
147              VNInfo::Allocator*);
148
149   //===--------------------------------------------------------------------===//
150   // Mid-level interface.
151   //===--------------------------------------------------------------------===//
152   //
153   // Modify existing live ranges.
154   //
155
156   /// extend - Extend the live range of LI to reach Kill.
157   ///
158   /// The existing values in LI must be live so they jointly dominate Kill.  If
159   /// Kill is not dominated by a single existing value, PHI-defs are inserted
160   /// as required to preserve SSA form.  If Kill is known to be dominated by a
161   /// single existing value, Alloc may be null.
162   ///
163   /// PhysReg, when set, is used to verify live-in lists on basic blocks.
164   void extend(LiveInterval *LI, SlotIndex Kill, unsigned PhysReg = 0);
165
166   /// createDeadDefs - Create a dead def in LI for every def operand of Reg.
167   /// Each instruction defining Reg gets a new VNInfo with a corresponding
168   /// minimal live range.
169   void createDeadDefs(LiveInterval *LI, unsigned Reg);
170
171   /// createDeadDefs - Create a dead def in LI for every def of LI->reg.
172   void createDeadDefs(LiveInterval *LI) {
173     createDeadDefs(LI, LI->reg);
174   }
175
176   /// extendToUses - Extend the live range of LI to reach all uses of Reg.
177   ///
178   /// All uses must be jointly dominated by existing liveness.  PHI-defs are
179   /// inserted as needed to preserve SSA form.
180   void extendToUses(LiveInterval *LI, unsigned Reg);
181
182   /// extendToUses - Extend the live range of LI to reach all uses of LI->reg.
183   void extendToUses(LiveInterval *LI) {
184     extendToUses(LI, LI->reg);
185   }
186
187   //===--------------------------------------------------------------------===//
188   // Low-level interface.
189   //===--------------------------------------------------------------------===//
190   //
191   // These functions can be used to compute live ranges where the live-in and
192   // live-out blocks are already known, but the SSA value in each block is
193   // unknown.
194   //
195   // After calling reset(), add known live-out values and known live-in blocks.
196   // Then call calculateValues() to compute the actual value that is
197   // live-in to each block, and add liveness to the live ranges.
198   //
199
200   /// setLiveOutValue - Indicate that VNI is live out from MBB.  The
201   /// calculateValues() function will not add liveness for MBB, the caller
202   /// should take care of that.
203   ///
204   /// VNI may be null only if MBB is a live-through block also passed to
205   /// addLiveInBlock().
206   void setLiveOutValue(MachineBasicBlock *MBB, VNInfo *VNI) {
207     Seen.set(MBB->getNumber());
208     LiveOut[MBB] = LiveOutPair(VNI, (MachineDomTreeNode *)0);
209   }
210
211   /// addLiveInBlock - Add a block with an unknown live-in value.  This
212   /// function can only be called once per basic block.  Once the live-in value
213   /// has been determined, calculateValues() will add liveness to LI.
214   ///
215   /// @param LI      The live range that is live-in to the block.
216   /// @param DomNode The domtree node for the block.
217   /// @param Kill    Index in block where LI is killed.  If the value is
218   ///                live-through, set Kill = SLotIndex() and also call
219   ///                setLiveOutValue(MBB, 0).
220   void addLiveInBlock(LiveInterval *LI,
221                       MachineDomTreeNode *DomNode,
222                       SlotIndex Kill = SlotIndex()) {
223     LiveIn.push_back(LiveInBlock(LI, DomNode, Kill));
224   }
225
226   /// calculateValues - Calculate the value that will be live-in to each block
227   /// added with addLiveInBlock.  Add PHI-def values as needed to preserve SSA
228   /// form.  Add liveness to all live-in blocks up to the Kill point, or the
229   /// whole block for live-through blocks.
230   ///
231   /// Every predecessor of a live-in block must have been given a value with
232   /// setLiveOutValue, the value may be null for live-trough blocks.
233   void calculateValues();
234 };
235
236 } // end namespace llvm
237
238 #endif