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