c419120a08f38e2d461da84e557eebc916649a9d
[oota-llvm.git] / lib / CodeGen / SplitKit.h
1 //===---------- SplitKit.cpp - Toolkit for splitting live ranges ----------===//
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/SmallPtrSet.h"
16 #include "llvm/ADT/DenseMap.h"
17 #include "llvm/CodeGen/SlotIndexes.h"
18
19 namespace llvm {
20
21 class LiveInterval;
22 class LiveIntervals;
23 class MachineInstr;
24 class MachineLoop;
25 class MachineLoopInfo;
26 class MachineRegisterInfo;
27 class TargetInstrInfo;
28 class VirtRegMap;
29 class VNInfo;
30
31 /// SplitAnalysis - Analyze a LiveInterval, looking for live range splitting
32 /// opportunities.
33 class SplitAnalysis {
34 public:
35   const MachineFunction &mf_;
36   const LiveIntervals &lis_;
37   const MachineLoopInfo &loops_;
38   const TargetInstrInfo &tii_;
39
40   // Instructions using the the current register.
41   typedef SmallPtrSet<const MachineInstr*, 16> InstrPtrSet;
42   InstrPtrSet usingInstrs_;
43
44   // The number of instructions using curli in each basic block.
45   typedef DenseMap<const MachineBasicBlock*, unsigned> BlockCountMap;
46   BlockCountMap usingBlocks_;
47
48   // The number of basic block using curli in each loop.
49   typedef DenseMap<const MachineLoop*, unsigned> LoopCountMap;
50   LoopCountMap usingLoops_;
51
52 private:
53   // Current live interval.
54   const LiveInterval *curli_;
55
56   // Sumarize statistics by counting instructions using curli_.
57   void analyzeUses();
58
59   /// canAnalyzeBranch - Return true if MBB ends in a branch that can be
60   /// analyzed.
61   bool canAnalyzeBranch(const MachineBasicBlock *MBB);
62
63 public:
64   SplitAnalysis(const MachineFunction &mf, const LiveIntervals &lis,
65                 const MachineLoopInfo &mli);
66
67   /// analyze - set curli to the specified interval, and analyze how it may be
68   /// split.
69   void analyze(const LiveInterval *li);
70
71   /// removeUse - Update statistics by noting that mi no longer uses curli.
72   void removeUse(const MachineInstr *mi);
73
74   const LiveInterval *getCurLI() { return curli_; }
75
76   /// clear - clear all data structures so SplitAnalysis is ready to analyze a
77   /// new interval.
78   void clear();
79
80   typedef SmallPtrSet<const MachineBasicBlock*, 16> BlockPtrSet;
81   typedef SmallPtrSet<const MachineLoop*, 16> LoopPtrSet;
82
83   // Sets of basic blocks surrounding a machine loop.
84   struct LoopBlocks {
85     BlockPtrSet Loop;  // Blocks in the loop.
86     BlockPtrSet Preds; // Loop predecessor blocks.
87     BlockPtrSet Exits; // Loop exit blocks.
88
89     void clear() {
90       Loop.clear();
91       Preds.clear();
92       Exits.clear();
93     }
94   };
95
96   // Calculate the block sets surrounding the loop.
97   void getLoopBlocks(const MachineLoop *Loop, LoopBlocks &Blocks);
98
99   /// LoopPeripheralUse - how is a variable used in and around a loop?
100   /// Peripheral blocks are the loop predecessors and exit blocks.
101   enum LoopPeripheralUse {
102     ContainedInLoop,  // All uses are inside the loop.
103     SinglePeripheral, // At most one instruction per peripheral block.
104     MultiPeripheral,  // Multiple instructions in some peripheral blocks.
105     OutsideLoop       // Uses outside loop periphery.
106   };
107
108   /// analyzeLoopPeripheralUse - Return an enum describing how curli_ is used in
109   /// and around the Loop.
110   LoopPeripheralUse analyzeLoopPeripheralUse(const LoopBlocks&);
111
112   /// getCriticalExits - It may be necessary to partially break critical edges
113   /// leaving the loop if an exit block has phi uses of curli. Collect the exit
114   /// blocks that need special treatment into CriticalExits.
115   void getCriticalExits(const LoopBlocks &Blocks, BlockPtrSet &CriticalExits);
116
117   /// canSplitCriticalExits - Return true if it is possible to insert new exit
118   /// blocks before the blocks in CriticalExits.
119   bool canSplitCriticalExits(const LoopBlocks &Blocks,
120                              BlockPtrSet &CriticalExits);
121
122   /// getBestSplitLoop - Return the loop where curli may best be split to a
123   /// separate register, or NULL.
124   const MachineLoop *getBestSplitLoop();
125
126   /// getMultiUseBlocks - Add basic blocks to Blocks that may benefit from
127   /// having curli split to a new live interval. Return true if Blocks can be
128   /// passed to SplitEditor::splitSingleBlocks.
129   bool getMultiUseBlocks(BlockPtrSet &Blocks);
130
131   /// getBlockForInsideSplit - If curli is contained inside a single basic block,
132   /// and it wou pay to subdivide the interval inside that block, return it.
133   /// Otherwise return NULL. The returned block can be passed to
134   /// SplitEditor::splitInsideBlock.
135   const MachineBasicBlock *getBlockForInsideSplit();
136 };
137
138 /// SplitEditor - Edit machine code and LiveIntervals for live range
139 /// splitting.
140 ///
141 /// - Create a SplitEditor from a SplitAnalysis.
142 /// - Start a new live interval with openIntv.
143 /// - Mark the places where the new interval is entered using enterIntv*
144 /// - Mark the ranges where the new interval is used with useIntv* 
145 /// - Mark the places where the interval is exited with exitIntv*.
146 /// - Finish the current interval with closeIntv and repeat from 2.
147 /// - Rewrite instructions with rewrite().
148 ///
149 class SplitEditor {
150   SplitAnalysis &sa_;
151   LiveIntervals &lis_;
152   VirtRegMap &vrm_;
153   MachineRegisterInfo &mri_;
154   const TargetInstrInfo &tii_;
155
156   /// curli_ - The immutable interval we are currently splitting.
157   const LiveInterval *const curli_;
158
159   /// dupli_ - Created as a copy of curli_, ranges are carved out as new
160   /// intervals get added through openIntv / closeIntv. This is used to avoid
161   /// editing curli_.
162   LiveInterval *dupli_;
163
164   /// Currently open LiveInterval.
165   LiveInterval *openli_;
166
167   /// createInterval - Create a new virtual register and LiveInterval with same
168   /// register class and spill slot as curli.
169   LiveInterval *createInterval();
170
171   /// getDupLI - Ensure dupli is created and return it.
172   LiveInterval *getDupLI();
173
174   /// valueMap_ - Map values in cupli to values in openli. These are direct 1-1
175   /// mappings, and do not include values created by inserted copies.
176   DenseMap<const VNInfo*, VNInfo*> valueMap_;
177
178   /// mapValue - Return the openIntv value that corresponds to the given curli
179   /// value.
180   VNInfo *mapValue(const VNInfo *curliVNI);
181
182   /// A dupli value is live through openIntv.
183   bool liveThrough_;
184
185   /// All the new intervals created for this split are added to intervals_.
186   SmallVectorImpl<LiveInterval*> &intervals_;
187
188   /// The index into intervals_ of the first interval we added. There may be
189   /// others from before we got it.
190   unsigned firstInterval;
191
192   /// Insert a COPY instruction curli -> li. Allocate a new value from li
193   /// defined by the COPY
194   VNInfo *insertCopy(LiveInterval &LI,
195                      MachineBasicBlock &MBB,
196                      MachineBasicBlock::iterator I);
197
198 public:
199   /// Create a new SplitEditor for editing the LiveInterval analyzed by SA.
200   /// Newly created intervals will be appended to newIntervals.
201   SplitEditor(SplitAnalysis &SA, LiveIntervals&, VirtRegMap&,
202               SmallVectorImpl<LiveInterval*> &newIntervals);
203
204   /// getAnalysis - Get the corresponding analysis.
205   SplitAnalysis &getAnalysis() { return sa_; }
206
207   /// Create a new virtual register and live interval.
208   void openIntv();
209
210   /// enterIntvBefore - Enter openli before the instruction at Idx. If curli is
211   /// not live before Idx, a COPY is not inserted.
212   void enterIntvBefore(SlotIndex Idx);
213
214   /// enterIntvAtEnd - Enter openli at the end of MBB.
215   /// PhiMBB is a successor inside openli where a PHI value is created.
216   /// Currently, all entries must share the same PhiMBB.
217   void enterIntvAtEnd(MachineBasicBlock &MBB, MachineBasicBlock &PhiMBB);
218
219   /// useIntv - indicate that all instructions in MBB should use openli.
220   void useIntv(const MachineBasicBlock &MBB);
221
222   /// useIntv - indicate that all instructions in range should use openli.
223   void useIntv(SlotIndex Start, SlotIndex End);
224
225   /// leaveIntvAfter - Leave openli after the instruction at Idx.
226   void leaveIntvAfter(SlotIndex Idx);
227
228   /// leaveIntvAtTop - Leave the interval at the top of MBB.
229   /// Currently, only one value can leave the interval.
230   void leaveIntvAtTop(MachineBasicBlock &MBB);
231
232   /// closeIntv - Indicate that we are done editing the currently open
233   /// LiveInterval, and ranges can be trimmed.
234   void closeIntv();
235
236   /// rewrite - after all the new live ranges have been created, rewrite
237   /// instructions using curli to use the new intervals.
238   void rewrite();
239
240   // ===--- High level methods ---===
241
242   /// splitAroundLoop - Split curli into a separate live interval inside
243   /// the loop. Return true if curli has been completely replaced, false if
244   /// curli is still intact, and needs to be spilled or split further.
245   bool splitAroundLoop(const MachineLoop*);
246
247   /// splitSingleBlocks - Split curli into a separate live interval inside each
248   /// basic block in Blocks. Return true if curli has been completely replaced,
249   /// false if curli is still intact, and needs to be spilled or split further.
250   bool splitSingleBlocks(const SplitAnalysis::BlockPtrSet &Blocks);
251
252   /// splitInsideBlock - Split curli into multiple intervals inside MBB. Return
253   /// true if curli has been completely replaced, false if curli is still
254   /// intact, and needs to be spilled or split further.
255   bool splitInsideBlock(const MachineBasicBlock *);
256 };
257
258 }