]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/lib/CodeGen/LiveRangeCalc.h
MFV r325013,r325034: 640 number_to_scaled_string is duplicated in several commands
[FreeBSD/FreeBSD.git] / contrib / llvm / 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/ArrayRef.h"
26 #include "llvm/ADT/BitVector.h"
27 #include "llvm/ADT/DenseMap.h"
28 #include "llvm/ADT/IndexedMap.h"
29 #include "llvm/CodeGen/LiveInterval.h"
30
31 namespace llvm {
32
33 /// Forward declarations for MachineDominators.h:
34 class MachineDominatorTree;
35 template <class NodeT> class DomTreeNodeBase;
36 typedef DomTreeNodeBase<MachineBasicBlock> MachineDomTreeNode;
37
38 class LiveRangeCalc {
39   const MachineFunction *MF;
40   const MachineRegisterInfo *MRI;
41   SlotIndexes *Indexes;
42   MachineDominatorTree *DomTree;
43   VNInfo::Allocator *Alloc;
44
45   /// LiveOutPair - A value and the block that defined it.  The domtree node is
46   /// redundant, it can be computed as: MDT[Indexes.getMBBFromIndex(VNI->def)].
47   typedef std::pair<VNInfo*, MachineDomTreeNode*> LiveOutPair;
48
49   /// LiveOutMap - Map basic blocks to the value leaving the block.
50   typedef IndexedMap<LiveOutPair, MBB2NumberFunctor> LiveOutMap;
51
52   /// Bit vector of active entries in LiveOut, also used as a visited set by
53   /// findReachingDefs.  One entry per basic block, indexed by block number.
54   /// This is kept as a separate bit vector because it can be cleared quickly
55   /// when switching live ranges.
56   BitVector Seen;
57
58   /// Map LiveRange to sets of blocks (represented by bit vectors) that
59   /// in the live range are defined on entry and undefined on entry.
60   /// A block is defined on entry if there is a path from at least one of
61   /// the defs in the live range to the entry of the block, and conversely,
62   /// a block is undefined on entry, if there is no such path (i.e. no
63   /// definition reaches the entry of the block). A single LiveRangeCalc
64   /// object is used to track live-out information for multiple registers
65   /// in live range splitting (which is ok, since the live ranges of these
66   /// registers do not overlap), but the defined/undefined information must
67   /// be kept separate for each individual range.
68   /// By convention, EntryInfoMap[&LR] = { Defined, Undefined }.
69   typedef DenseMap<LiveRange*,std::pair<BitVector,BitVector>> EntryInfoMap;
70   EntryInfoMap EntryInfos;
71
72   /// Map each basic block where a live range is live out to the live-out value
73   /// and its defining block.
74   ///
75   /// For every basic block, MBB, one of these conditions shall be true:
76   ///
77   ///  1. !Seen.count(MBB->getNumber())
78   ///     Blocks without a Seen bit are ignored.
79   ///  2. LiveOut[MBB].second.getNode() == MBB
80   ///     The live-out value is defined in MBB.
81   ///  3. forall P in preds(MBB): LiveOut[P] == LiveOut[MBB]
82   ///     The live-out value passses through MBB. All predecessors must carry
83   ///     the same value.
84   ///
85   /// The domtree node may be null, it can be computed.
86   ///
87   /// The map can be shared by multiple live ranges as long as no two are
88   /// live-out of the same block.
89   LiveOutMap Map;
90
91   /// LiveInBlock - Information about a basic block where a live range is known
92   /// to be live-in, but the value has not yet been determined.
93   struct LiveInBlock {
94     // The live range set that is live-in to this block.  The algorithms can
95     // handle multiple non-overlapping live ranges simultaneously.
96     LiveRange &LR;
97
98     // DomNode - Dominator tree node for the block.
99     // Cleared when the final value has been determined and LI has been updated.
100     MachineDomTreeNode *DomNode;
101
102     // Position in block where the live-in range ends, or SlotIndex() if the
103     // range passes through the block.  When the final value has been
104     // determined, the range from the block start to Kill will be added to LI.
105     SlotIndex Kill;
106
107     // Live-in value filled in by updateSSA once it is known.
108     VNInfo *Value;
109
110     LiveInBlock(LiveRange &LR, MachineDomTreeNode *node, SlotIndex kill)
111       : LR(LR), DomNode(node), Kill(kill), Value(nullptr) {}
112   };
113
114   /// LiveIn - Work list of blocks where the live-in value has yet to be
115   /// determined.  This list is typically computed by findReachingDefs() and
116   /// used as a work list by updateSSA().  The low-level interface may also be
117   /// used to add entries directly.
118   SmallVector<LiveInBlock, 16> LiveIn;
119
120   /// Check if the entry to block @p MBB can be reached by any of the defs
121   /// in @p LR. Return true if none of the defs reach the entry to @p MBB.
122   bool isDefOnEntry(LiveRange &LR, ArrayRef<SlotIndex> Undefs,
123                     MachineBasicBlock &MBB, BitVector &DefOnEntry,
124                     BitVector &UndefOnEntry);
125
126   /// Find the set of defs that can reach @p Kill. @p Kill must belong to
127   /// @p UseMBB.
128   ///
129   /// If exactly one def can reach @p UseMBB, and the def dominates @p Kill,
130   /// all paths from the def to @p UseMBB are added to @p LR, and the function
131   /// returns true.
132   ///
133   /// If multiple values can reach @p UseMBB, the blocks that need @p LR to be
134   /// live in are added to the LiveIn array, and the function returns false.
135   ///
136   /// The array @p Undef provides the locations where the range @p LR becomes
137   /// undefined by <def,read-undef> operands on other subranges. If @p Undef
138   /// is non-empty and @p Kill is jointly dominated only by the entries of
139   /// @p Undef, the function returns false.
140   ///
141   /// PhysReg, when set, is used to verify live-in lists on basic blocks.
142   bool findReachingDefs(LiveRange &LR, MachineBasicBlock &UseMBB,
143                         SlotIndex Kill, unsigned PhysReg,
144                         ArrayRef<SlotIndex> Undefs);
145
146   /// updateSSA - Compute the values that will be live in to all requested
147   /// blocks in LiveIn.  Create PHI-def values as required to preserve SSA form.
148   ///
149   /// Every live-in block must be jointly dominated by the added live-out
150   /// blocks.  No values are read from the live ranges.
151   void updateSSA();
152
153   /// Transfer information from the LiveIn vector to the live ranges and update
154   /// the given @p LiveOuts.
155   void updateFromLiveIns();
156
157   /// Extend the live range of @p LR to reach all uses of Reg.
158   ///
159   /// If @p LR is a main range, or if @p LI is null, then all uses must be
160   /// jointly dominated by the definitions from @p LR. If @p LR is a subrange
161   /// of the live interval @p LI, corresponding to lane mask @p LaneMask,
162   /// all uses must be jointly dominated by the definitions from @p LR
163   /// together with definitions of other lanes where @p LR becomes undefined
164   /// (via <def,read-undef> operands).
165   /// If @p LR is a main range, the @p LaneMask should be set to ~0, i.e.
166   /// LaneBitmask::getAll().
167   void extendToUses(LiveRange &LR, unsigned Reg, LaneBitmask LaneMask,
168                     LiveInterval *LI = nullptr);
169
170   /// Reset Map and Seen fields.
171   void resetLiveOutMap();
172
173 public:
174   LiveRangeCalc() : MF(nullptr), MRI(nullptr), Indexes(nullptr),
175                     DomTree(nullptr), Alloc(nullptr) {}
176
177   //===--------------------------------------------------------------------===//
178   // High-level interface.
179   //===--------------------------------------------------------------------===//
180   //
181   // Calculate live ranges from scratch.
182   //
183
184   /// reset - Prepare caches for a new set of non-overlapping live ranges.  The
185   /// caches must be reset before attempting calculations with a live range
186   /// that may overlap a previously computed live range, and before the first
187   /// live range in a function.  If live ranges are not known to be
188   /// non-overlapping, call reset before each.
189   void reset(const MachineFunction *MF,
190              SlotIndexes*,
191              MachineDominatorTree*,
192              VNInfo::Allocator*);
193
194   //===--------------------------------------------------------------------===//
195   // Mid-level interface.
196   //===--------------------------------------------------------------------===//
197   //
198   // Modify existing live ranges.
199   //
200
201   /// Extend the live range of @p LR to reach @p Use.
202   ///
203   /// The existing values in @p LR must be live so they jointly dominate @p Use.
204   /// If @p Use is not dominated by a single existing value, PHI-defs are
205   /// inserted as required to preserve SSA form.
206   ///
207   /// PhysReg, when set, is used to verify live-in lists on basic blocks.
208   void extend(LiveRange &LR, SlotIndex Use, unsigned PhysReg,
209               ArrayRef<SlotIndex> Undefs);
210
211   /// createDeadDefs - Create a dead def in LI for every def operand of Reg.
212   /// Each instruction defining Reg gets a new VNInfo with a corresponding
213   /// minimal live range.
214   void createDeadDefs(LiveRange &LR, unsigned Reg);
215
216   /// Extend the live range of @p LR to reach all uses of Reg.
217   ///
218   /// All uses must be jointly dominated by existing liveness.  PHI-defs are
219   /// inserted as needed to preserve SSA form.
220   void extendToUses(LiveRange &LR, unsigned PhysReg) {
221     extendToUses(LR, PhysReg, LaneBitmask::getAll());
222   }
223
224   /// Calculates liveness for the register specified in live interval @p LI.
225   /// Creates subregister live ranges as needed if subreg liveness tracking is
226   /// enabled.
227   void calculate(LiveInterval &LI, bool TrackSubRegs);
228
229   /// For live interval \p LI with correct SubRanges construct matching
230   /// information for the main live range. Expects the main live range to not
231   /// have any segments or value numbers.
232   void constructMainRangeFromSubranges(LiveInterval &LI);
233
234   //===--------------------------------------------------------------------===//
235   // Low-level interface.
236   //===--------------------------------------------------------------------===//
237   //
238   // These functions can be used to compute live ranges where the live-in and
239   // live-out blocks are already known, but the SSA value in each block is
240   // unknown.
241   //
242   // After calling reset(), add known live-out values and known live-in blocks.
243   // Then call calculateValues() to compute the actual value that is
244   // live-in to each block, and add liveness to the live ranges.
245   //
246
247   /// setLiveOutValue - Indicate that VNI is live out from MBB.  The
248   /// calculateValues() function will not add liveness for MBB, the caller
249   /// should take care of that.
250   ///
251   /// VNI may be null only if MBB is a live-through block also passed to
252   /// addLiveInBlock().
253   void setLiveOutValue(MachineBasicBlock *MBB, VNInfo *VNI) {
254     Seen.set(MBB->getNumber());
255     Map[MBB] = LiveOutPair(VNI, nullptr);
256   }
257
258   /// addLiveInBlock - Add a block with an unknown live-in value.  This
259   /// function can only be called once per basic block.  Once the live-in value
260   /// has been determined, calculateValues() will add liveness to LI.
261   ///
262   /// @param LR      The live range that is live-in to the block.
263   /// @param DomNode The domtree node for the block.
264   /// @param Kill    Index in block where LI is killed.  If the value is
265   ///                live-through, set Kill = SLotIndex() and also call
266   ///                setLiveOutValue(MBB, 0).
267   void addLiveInBlock(LiveRange &LR,
268                       MachineDomTreeNode *DomNode,
269                       SlotIndex Kill = SlotIndex()) {
270     LiveIn.push_back(LiveInBlock(LR, DomNode, Kill));
271   }
272
273   /// calculateValues - Calculate the value that will be live-in to each block
274   /// added with addLiveInBlock.  Add PHI-def values as needed to preserve SSA
275   /// form.  Add liveness to all live-in blocks up to the Kill point, or the
276   /// whole block for live-through blocks.
277   ///
278   /// Every predecessor of a live-in block must have been given a value with
279   /// setLiveOutValue, the value may be null for live-trough blocks.
280   void calculateValues();
281 };
282
283 } // end namespace llvm
284
285 #endif