SplitKit.h revision 8dd070edc2209ecfdae49780ec1596b349e2cbd1
1//===-------- SplitKit.h - Toolkit for splitting 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// This file contains the SplitAnalysis class as well as mutator functions for
11// live range splitting.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/ADT/DenseMap.h"
16#include "llvm/ADT/SmallPtrSet.h"
17#include "llvm/CodeGen/SlotIndexes.h"
18
19namespace llvm {
20
21class LiveInterval;
22class LiveIntervals;
23class LiveRangeEdit;
24class MachineInstr;
25class MachineLoop;
26class MachineLoopInfo;
27class MachineRegisterInfo;
28class TargetInstrInfo;
29class TargetRegisterInfo;
30class VirtRegMap;
31class VNInfo;
32class raw_ostream;
33
34/// At some point we should just include MachineDominators.h:
35class MachineDominatorTree;
36template <class NodeT> class DomTreeNodeBase;
37typedef DomTreeNodeBase<MachineBasicBlock> MachineDomTreeNode;
38
39
40/// SplitAnalysis - Analyze a LiveInterval, looking for live range splitting
41/// opportunities.
42class SplitAnalysis {
43public:
44  const MachineFunction &mf_;
45  const LiveIntervals &lis_;
46  const MachineLoopInfo &loops_;
47  const TargetInstrInfo &tii_;
48
49  // Instructions using the the current register.
50  typedef SmallPtrSet<const MachineInstr*, 16> InstrPtrSet;
51  InstrPtrSet usingInstrs_;
52
53  // The number of instructions using curli in each basic block.
54  typedef DenseMap<const MachineBasicBlock*, unsigned> BlockCountMap;
55  BlockCountMap usingBlocks_;
56
57  // The number of basic block using curli in each loop.
58  typedef DenseMap<const MachineLoop*, unsigned> LoopCountMap;
59  LoopCountMap usingLoops_;
60
61private:
62  // Current live interval.
63  const LiveInterval *curli_;
64
65  // Sumarize statistics by counting instructions using curli_.
66  void analyzeUses();
67
68  /// canAnalyzeBranch - Return true if MBB ends in a branch that can be
69  /// analyzed.
70  bool canAnalyzeBranch(const MachineBasicBlock *MBB);
71
72public:
73  SplitAnalysis(const MachineFunction &mf, const LiveIntervals &lis,
74                const MachineLoopInfo &mli);
75
76  /// analyze - set curli to the specified interval, and analyze how it may be
77  /// split.
78  void analyze(const LiveInterval *li);
79
80  /// clear - clear all data structures so SplitAnalysis is ready to analyze a
81  /// new interval.
82  void clear();
83
84  typedef SmallPtrSet<const MachineBasicBlock*, 16> BlockPtrSet;
85  typedef SmallPtrSet<const MachineLoop*, 16> LoopPtrSet;
86
87  // Print a set of blocks with use counts.
88  void print(const BlockPtrSet&, raw_ostream&) const;
89
90  // Sets of basic blocks surrounding a machine loop.
91  struct LoopBlocks {
92    BlockPtrSet Loop;  // Blocks in the loop.
93    BlockPtrSet Preds; // Loop predecessor blocks.
94    BlockPtrSet Exits; // Loop exit blocks.
95
96    void clear() {
97      Loop.clear();
98      Preds.clear();
99      Exits.clear();
100    }
101  };
102
103  // Print loop blocks with use counts.
104  void print(const LoopBlocks&, raw_ostream&) const;
105
106  // Calculate the block sets surrounding the loop.
107  void getLoopBlocks(const MachineLoop *Loop, LoopBlocks &Blocks);
108
109  /// LoopPeripheralUse - how is a variable used in and around a loop?
110  /// Peripheral blocks are the loop predecessors and exit blocks.
111  enum LoopPeripheralUse {
112    ContainedInLoop,  // All uses are inside the loop.
113    SinglePeripheral, // At most one instruction per peripheral block.
114    MultiPeripheral,  // Multiple instructions in some peripheral blocks.
115    OutsideLoop       // Uses outside loop periphery.
116  };
117
118  /// analyzeLoopPeripheralUse - Return an enum describing how curli_ is used in
119  /// and around the Loop.
120  LoopPeripheralUse analyzeLoopPeripheralUse(const LoopBlocks&);
121
122  /// getCriticalExits - It may be necessary to partially break critical edges
123  /// leaving the loop if an exit block has phi uses of curli. Collect the exit
124  /// blocks that need special treatment into CriticalExits.
125  void getCriticalExits(const LoopBlocks &Blocks, BlockPtrSet &CriticalExits);
126
127  /// canSplitCriticalExits - Return true if it is possible to insert new exit
128  /// blocks before the blocks in CriticalExits.
129  bool canSplitCriticalExits(const LoopBlocks &Blocks,
130                             BlockPtrSet &CriticalExits);
131
132  /// getCriticalPreds - Get the set of loop predecessors with critical edges to
133  /// blocks outside the loop that have curli live in. We don't have to break
134  /// these edges, but they do require special treatment.
135  void getCriticalPreds(const LoopBlocks &Blocks, BlockPtrSet &CriticalPreds);
136
137  /// getSplitLoops - Get the set of loops that have curli uses and would be
138  /// profitable to split.
139  void getSplitLoops(LoopPtrSet&);
140
141  /// getBestSplitLoop - Return the loop where curli may best be split to a
142  /// separate register, or NULL.
143  const MachineLoop *getBestSplitLoop();
144
145  /// isBypassLoop - Return true if curli is live through Loop and has no uses
146  /// inside the loop. Bypass loops are candidates for splitting because it can
147  /// prevent interference inside the loop.
148  bool isBypassLoop(const MachineLoop *Loop);
149
150  /// getBypassLoops - Get all the maximal bypass loops. These are the bypass
151  /// loops whose parent is not a bypass loop.
152  void getBypassLoops(LoopPtrSet&);
153
154  /// getMultiUseBlocks - Add basic blocks to Blocks that may benefit from
155  /// having curli split to a new live interval. Return true if Blocks can be
156  /// passed to SplitEditor::splitSingleBlocks.
157  bool getMultiUseBlocks(BlockPtrSet &Blocks);
158
159  /// getBlockForInsideSplit - If curli is contained inside a single basic block,
160  /// and it wou pay to subdivide the interval inside that block, return it.
161  /// Otherwise return NULL. The returned block can be passed to
162  /// SplitEditor::splitInsideBlock.
163  const MachineBasicBlock *getBlockForInsideSplit();
164};
165
166
167/// LiveIntervalMap - Map values from a large LiveInterval into a small
168/// interval that is a subset. Insert phi-def values as needed. This class is
169/// used by SplitEditor to create new smaller LiveIntervals.
170///
171/// parentli_ is the larger interval, li_ is the subset interval. Every value
172/// in li_ corresponds to exactly one value in parentli_, and the live range
173/// of the value is contained within the live range of the parentli_ value.
174/// Values in parentli_ may map to any number of openli_ values, including 0.
175class LiveIntervalMap {
176  LiveIntervals &lis_;
177  MachineDominatorTree &mdt_;
178
179  // The parent interval is never changed.
180  const LiveInterval &parentli_;
181
182  // The child interval's values are fully contained inside parentli_ values.
183  LiveInterval *li_;
184
185  typedef DenseMap<const VNInfo*, VNInfo*> ValueMap;
186
187  // Map parentli_ values to simple values in li_ that are defined at the same
188  // SlotIndex, or NULL for parentli_ values that have complex li_ defs.
189  // Note there is a difference between values mapping to NULL (complex), and
190  // values not present (unknown/unmapped).
191  ValueMap valueMap_;
192
193  typedef std::pair<VNInfo*, MachineDomTreeNode*> LiveOutPair;
194  typedef DenseMap<MachineBasicBlock*,LiveOutPair> LiveOutMap;
195
196  // liveOutCache_ - Map each basic block where li_ is live out to the live-out
197  // value and its defining block. One of these conditions shall be true:
198  //
199  //  1. !liveOutCache_.count(MBB)
200  //  2. liveOutCache_[MBB].second.getNode() == MBB
201  //  3. forall P in preds(MBB): liveOutCache_[P] == liveOutCache_[MBB]
202  //
203  // This is only a cache, the values can be computed as:
204  //
205  //  VNI = li_->getVNInfoAt(lis_.getMBBEndIdx(MBB))
206  //  Node = mbt_[lis_.getMBBFromIndex(VNI->def)]
207  //
208  // The cache is also used as a visiteed set by mapValue().
209  LiveOutMap liveOutCache_;
210
211public:
212  LiveIntervalMap(LiveIntervals &lis,
213                  MachineDominatorTree &mdt,
214                  const LiveInterval &parentli)
215    : lis_(lis), mdt_(mdt), parentli_(parentli), li_(0) {}
216
217  /// reset - clear all data structures and start a new live interval.
218  void reset(LiveInterval *);
219
220  /// getLI - return the current live interval.
221  LiveInterval *getLI() const { return li_; }
222
223  /// defValue - define a value in li_ from the parentli_ value VNI and Idx.
224  /// Idx does not have to be ParentVNI->def, but it must be contained within
225  /// ParentVNI's live range in parentli_.
226  /// Return the new li_ value.
227  VNInfo *defValue(const VNInfo *ParentVNI, SlotIndex Idx);
228
229  /// mapValue - map ParentVNI to the corresponding li_ value at Idx. It is
230  /// assumed that ParentVNI is live at Idx.
231  /// If ParentVNI has not been defined by defValue, it is assumed that
232  /// ParentVNI->def dominates Idx.
233  /// If ParentVNI has been defined by defValue one or more times, a value that
234  /// dominates Idx will be returned. This may require creating extra phi-def
235  /// values and adding live ranges to li_.
236  /// If simple is not NULL, *simple will indicate if ParentVNI is a simply
237  /// mapped value.
238  VNInfo *mapValue(const VNInfo *ParentVNI, SlotIndex Idx, bool *simple = 0);
239
240  // extendTo - Find the last li_ value defined in MBB at or before Idx. The
241  // parentli is assumed to be live at Idx. Extend the live range to include
242  // Idx. Return the found VNInfo, or NULL.
243  VNInfo *extendTo(const MachineBasicBlock *MBB, SlotIndex Idx);
244
245  /// isMapped - Return true is ParentVNI is a known mapped value. It may be a
246  /// simple 1-1 mapping or a complex mapping to later defs.
247  bool isMapped(const VNInfo *ParentVNI) const {
248    return valueMap_.count(ParentVNI);
249  }
250
251  /// isComplexMapped - Return true if ParentVNI has received new definitions
252  /// with defValue.
253  bool isComplexMapped(const VNInfo *ParentVNI) const;
254
255  // addSimpleRange - Add a simple range from parentli_ to li_.
256  // ParentVNI must be live in the [Start;End) interval.
257  void addSimpleRange(SlotIndex Start, SlotIndex End, const VNInfo *ParentVNI);
258
259  /// addRange - Add live ranges to li_ where [Start;End) intersects parentli_.
260  /// All needed values whose def is not inside [Start;End) must be defined
261  /// beforehand so mapValue will work.
262  void addRange(SlotIndex Start, SlotIndex End);
263};
264
265
266/// SplitEditor - Edit machine code and LiveIntervals for live range
267/// splitting.
268///
269/// - Create a SplitEditor from a SplitAnalysis.
270/// - Start a new live interval with openIntv.
271/// - Mark the places where the new interval is entered using enterIntv*
272/// - Mark the ranges where the new interval is used with useIntv*
273/// - Mark the places where the interval is exited with exitIntv*.
274/// - Finish the current interval with closeIntv and repeat from 2.
275/// - Rewrite instructions with finish().
276///
277class SplitEditor {
278  SplitAnalysis &sa_;
279  LiveIntervals &lis_;
280  VirtRegMap &vrm_;
281  MachineRegisterInfo &mri_;
282  const TargetInstrInfo &tii_;
283  const TargetRegisterInfo &tri_;
284
285  /// edit_ - The current parent register and new intervals created.
286  LiveRangeEdit &edit_;
287
288  /// dupli_ - Created as a copy of curli_, ranges are carved out as new
289  /// intervals get added through openIntv / closeIntv. This is used to avoid
290  /// editing curli_.
291  LiveIntervalMap dupli_;
292
293  /// Currently open LiveInterval.
294  LiveIntervalMap openli_;
295
296  /// defFromParent - Define Reg from ParentVNI at UseIdx using either
297  /// rematerialization or a COPY from parent. Return the new value.
298  VNInfo *defFromParent(LiveIntervalMap &Reg,
299                        VNInfo *ParentVNI,
300                        SlotIndex UseIdx,
301                        MachineBasicBlock &MBB,
302                        MachineBasicBlock::iterator I);
303
304  /// intervalsLiveAt - Return true if any member of intervals_ is live at Idx.
305  bool intervalsLiveAt(SlotIndex Idx) const;
306
307  /// Values in curli whose live range has been truncated when entering an open
308  /// li.
309  SmallPtrSet<const VNInfo*, 8> truncatedValues;
310
311  /// addTruncSimpleRange - Add the given simple range to dupli_ after
312  /// truncating any overlap with intervals_.
313  void addTruncSimpleRange(SlotIndex Start, SlotIndex End, VNInfo *VNI);
314
315  /// criticalPreds_ - Set of basic blocks where both dupli and openli should be
316  /// live out because of a critical edge.
317  SplitAnalysis::BlockPtrSet criticalPreds_;
318
319  /// computeRemainder - Compute the dupli liveness as the complement of all the
320  /// new intervals.
321  void computeRemainder();
322
323  /// rewrite - Rewrite all uses of reg to use the new registers.
324  void rewrite(unsigned reg);
325
326public:
327  /// Create a new SplitEditor for editing the LiveInterval analyzed by SA.
328  /// Newly created intervals will be appended to newIntervals.
329  SplitEditor(SplitAnalysis &SA, LiveIntervals&, VirtRegMap&,
330              MachineDominatorTree&, LiveRangeEdit&);
331
332  /// getAnalysis - Get the corresponding analysis.
333  SplitAnalysis &getAnalysis() { return sa_; }
334
335  /// Create a new virtual register and live interval.
336  void openIntv();
337
338  /// enterIntvBefore - Enter openli before the instruction at Idx. If curli is
339  /// not live before Idx, a COPY is not inserted.
340  void enterIntvBefore(SlotIndex Idx);
341
342  /// enterIntvAtEnd - Enter openli at the end of MBB.
343  void enterIntvAtEnd(MachineBasicBlock &MBB);
344
345  /// useIntv - indicate that all instructions in MBB should use openli.
346  void useIntv(const MachineBasicBlock &MBB);
347
348  /// useIntv - indicate that all instructions in range should use openli.
349  void useIntv(SlotIndex Start, SlotIndex End);
350
351  /// leaveIntvAfter - Leave openli after the instruction at Idx.
352  void leaveIntvAfter(SlotIndex Idx);
353
354  /// leaveIntvAtTop - Leave the interval at the top of MBB.
355  /// Currently, only one value can leave the interval.
356  void leaveIntvAtTop(MachineBasicBlock &MBB);
357
358  /// closeIntv - Indicate that we are done editing the currently open
359  /// LiveInterval, and ranges can be trimmed.
360  void closeIntv();
361
362  /// finish - after all the new live ranges have been created, compute the
363  /// remaining live range, and rewrite instructions to use the new registers.
364  void finish();
365
366  // ===--- High level methods ---===
367
368  /// splitAroundLoop - Split curli into a separate live interval inside
369  /// the loop.
370  void splitAroundLoop(const MachineLoop*);
371
372  /// splitSingleBlocks - Split curli into a separate live interval inside each
373  /// basic block in Blocks.
374  void splitSingleBlocks(const SplitAnalysis::BlockPtrSet &Blocks);
375
376  /// splitInsideBlock - Split curli into multiple intervals inside MBB.
377  void splitInsideBlock(const MachineBasicBlock *);
378};
379
380}
381