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