1 //===---------- SplitKit.cpp - Toolkit for splitting live ranges ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the SplitAnalysis class as well as mutator functions for
11 // live range splitting.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/ADT/SmallPtrSet.h"
16 #include "llvm/ADT/DenseMap.h"
17 #include "llvm/CodeGen/SlotIndexes.h"
23 class MachineDominatorTree;
26 class MachineLoopInfo;
27 class MachineRegisterInfo;
28 class TargetInstrInfo;
32 /// SplitAnalysis - Analyze a LiveInterval, looking for live range splitting
36 const MachineFunction &mf_;
37 const LiveIntervals &lis_;
38 const MachineLoopInfo &loops_;
39 const TargetInstrInfo &tii_;
41 // Instructions using the the current register.
42 typedef SmallPtrSet<const MachineInstr*, 16> InstrPtrSet;
43 InstrPtrSet usingInstrs_;
45 // The number of instructions using curli in each basic block.
46 typedef DenseMap<const MachineBasicBlock*, unsigned> BlockCountMap;
47 BlockCountMap usingBlocks_;
49 // The number of basic block using curli in each loop.
50 typedef DenseMap<const MachineLoop*, unsigned> LoopCountMap;
51 LoopCountMap usingLoops_;
54 // Current live interval.
55 const LiveInterval *curli_;
57 // Sumarize statistics by counting instructions using curli_.
60 /// canAnalyzeBranch - Return true if MBB ends in a branch that can be
62 bool canAnalyzeBranch(const MachineBasicBlock *MBB);
65 SplitAnalysis(const MachineFunction &mf, const LiveIntervals &lis,
66 const MachineLoopInfo &mli);
68 /// analyze - set curli to the specified interval, and analyze how it may be
70 void analyze(const LiveInterval *li);
72 /// removeUse - Update statistics by noting that mi no longer uses curli.
73 void removeUse(const MachineInstr *mi);
75 const LiveInterval *getCurLI() { return curli_; }
77 /// clear - clear all data structures so SplitAnalysis is ready to analyze a
81 typedef SmallPtrSet<const MachineBasicBlock*, 16> BlockPtrSet;
82 typedef SmallPtrSet<const MachineLoop*, 16> LoopPtrSet;
84 // Sets of basic blocks surrounding a machine loop.
86 BlockPtrSet Loop; // Blocks in the loop.
87 BlockPtrSet Preds; // Loop predecessor blocks.
88 BlockPtrSet Exits; // Loop exit blocks.
97 // Calculate the block sets surrounding the loop.
98 void getLoopBlocks(const MachineLoop *Loop, LoopBlocks &Blocks);
100 /// LoopPeripheralUse - how is a variable used in and around a loop?
101 /// Peripheral blocks are the loop predecessors and exit blocks.
102 enum LoopPeripheralUse {
103 ContainedInLoop, // All uses are inside the loop.
104 SinglePeripheral, // At most one instruction per peripheral block.
105 MultiPeripheral, // Multiple instructions in some peripheral blocks.
106 OutsideLoop // Uses outside loop periphery.
109 /// analyzeLoopPeripheralUse - Return an enum describing how curli_ is used in
110 /// and around the Loop.
111 LoopPeripheralUse analyzeLoopPeripheralUse(const LoopBlocks&);
113 /// getCriticalExits - It may be necessary to partially break critical edges
114 /// leaving the loop if an exit block has phi uses of curli. Collect the exit
115 /// blocks that need special treatment into CriticalExits.
116 void getCriticalExits(const LoopBlocks &Blocks, BlockPtrSet &CriticalExits);
118 /// canSplitCriticalExits - Return true if it is possible to insert new exit
119 /// blocks before the blocks in CriticalExits.
120 bool canSplitCriticalExits(const LoopBlocks &Blocks,
121 BlockPtrSet &CriticalExits);
123 /// getBestSplitLoop - Return the loop where curli may best be split to a
124 /// separate register, or NULL.
125 const MachineLoop *getBestSplitLoop();
127 /// getMultiUseBlocks - Add basic blocks to Blocks that may benefit from
128 /// having curli split to a new live interval. Return true if Blocks can be
129 /// passed to SplitEditor::splitSingleBlocks.
130 bool getMultiUseBlocks(BlockPtrSet &Blocks);
132 /// getBlockForInsideSplit - If curli is contained inside a single basic block,
133 /// and it wou pay to subdivide the interval inside that block, return it.
134 /// Otherwise return NULL. The returned block can be passed to
135 /// SplitEditor::splitInsideBlock.
136 const MachineBasicBlock *getBlockForInsideSplit();
140 /// LiveIntervalMap - Map values from a large LiveInterval into a small
141 /// interval that is a subset. Insert phi-def values as needed. This class is
142 /// used by SplitEditor to create new smaller LiveIntervals.
144 /// parentli_ is the larger interval, li_ is the subset interval. Every value
145 /// in li_ corresponds to exactly one value in parentli_, and the live range
146 /// of the value is contained within the live range of the parentli_ value.
147 /// Values in parentli_ may map to any number of openli_ values, including 0.
148 class LiveIntervalMap {
150 MachineDominatorTree &dt_;
152 // The parent interval is never changed.
153 const LiveInterval &parentli_;
155 // The child interval's values are fully contained inside parentli_ values.
158 typedef DenseMap<const VNInfo*, VNInfo*> ValueMap;
160 // Map parentli_ values to simple values in li_ that are defined at the same
161 // SlotIndex, or NULL for parentli_ values that have complex li_ defs.
162 // Note there is a difference between values mapping to NULL (complex), and
163 // values not present (unknown/unmapped).
166 // extendTo - Find the last li_ value defined in MBB at or before Idx. The
167 // parentli_ is assumed to be live at Idx. Extend the live range to Idx.
168 // Return the found VNInfo, or NULL.
169 VNInfo *extendTo(MachineBasicBlock *MBB, SlotIndex Idx);
171 // addSimpleRange - Add a simple range from parentli_ to li_.
172 // ParentVNI must be live in the [Start;End) interval.
173 void addSimpleRange(SlotIndex Start, SlotIndex End, const VNInfo *ParentVNI);
176 LiveIntervalMap(LiveIntervals &lis,
177 MachineDominatorTree &dt,
178 const LiveInterval &parentli,
180 : lis_(lis), dt_(dt), parentli_(parentli), li_(li) {}
182 /// defValue - define a value in li_ from the parentli_ value VNI and Idx.
183 /// Idx does not have to be ParentVNI->def, but it must be contained within
184 /// ParentVNI's live range in parentli_.
185 /// Return the new li_ value.
186 VNInfo *defValue(const VNInfo *ParentVNI, SlotIndex Idx);
188 /// mapValue - map ParentVNI to the corresponding li_ value at Idx. It is
189 /// assumed that ParentVNI is live at Idx.
190 /// If ParentVNI has not been defined by defValue, it is assumed that
191 /// ParentVNI->def dominates Idx.
192 /// If ParentVNI has been defined by defValue one or more times, a value that
193 /// dominates Idx will be returned. This may require creating extra phi-def
194 /// values and adding live ranges to li_.
195 VNInfo *mapValue(const VNInfo *ParentVNI, SlotIndex Idx);
197 /// addRange - Add live ranges to li_ where [Start;End) intersects parentli_.
198 /// All needed values whose def is not inside [Start;End) must be defined
199 /// beforehand so mapValue will work.
200 void addRange(SlotIndex Start, SlotIndex End);
204 /// SplitEditor - Edit machine code and LiveIntervals for live range
207 /// - Create a SplitEditor from a SplitAnalysis.
208 /// - Start a new live interval with openIntv.
209 /// - Mark the places where the new interval is entered using enterIntv*
210 /// - Mark the ranges where the new interval is used with useIntv*
211 /// - Mark the places where the interval is exited with exitIntv*.
212 /// - Finish the current interval with closeIntv and repeat from 2.
213 /// - Rewrite instructions with rewrite().
219 MachineRegisterInfo &mri_;
220 const TargetInstrInfo &tii_;
222 /// curli_ - The immutable interval we are currently splitting.
223 const LiveInterval *const curli_;
225 /// dupli_ - Created as a copy of curli_, ranges are carved out as new
226 /// intervals get added through openIntv / closeIntv. This is used to avoid
228 LiveInterval *dupli_;
230 /// Currently open LiveInterval.
231 LiveInterval *openli_;
233 /// createInterval - Create a new virtual register and LiveInterval with same
234 /// register class and spill slot as curli.
235 LiveInterval *createInterval();
237 /// getDupLI - Ensure dupli is created and return it.
238 LiveInterval *getDupLI();
240 /// valueMap_ - Map values in cupli to values in openli. These are direct 1-1
241 /// mappings, and do not include values created by inserted copies.
242 DenseMap<const VNInfo*, VNInfo*> valueMap_;
244 /// mapValue - Return the openIntv value that corresponds to the given curli
246 VNInfo *mapValue(const VNInfo *curliVNI);
248 /// A dupli value is live through openIntv.
251 /// All the new intervals created for this split are added to intervals_.
252 SmallVectorImpl<LiveInterval*> &intervals_;
254 /// The index into intervals_ of the first interval we added. There may be
255 /// others from before we got it.
256 unsigned firstInterval;
258 /// Insert a COPY instruction curli -> li. Allocate a new value from li
259 /// defined by the COPY
260 VNInfo *insertCopy(LiveInterval &LI,
261 MachineBasicBlock &MBB,
262 MachineBasicBlock::iterator I);
265 /// Create a new SplitEditor for editing the LiveInterval analyzed by SA.
266 /// Newly created intervals will be appended to newIntervals.
267 SplitEditor(SplitAnalysis &SA, LiveIntervals&, VirtRegMap&,
268 SmallVectorImpl<LiveInterval*> &newIntervals);
270 /// getAnalysis - Get the corresponding analysis.
271 SplitAnalysis &getAnalysis() { return sa_; }
273 /// Create a new virtual register and live interval.
276 /// enterIntvBefore - Enter openli before the instruction at Idx. If curli is
277 /// not live before Idx, a COPY is not inserted.
278 void enterIntvBefore(SlotIndex Idx);
280 /// enterIntvAtEnd - Enter openli at the end of MBB.
281 /// PhiMBB is a successor inside openli where a PHI value is created.
282 /// Currently, all entries must share the same PhiMBB.
283 void enterIntvAtEnd(MachineBasicBlock &MBB, MachineBasicBlock &PhiMBB);
285 /// useIntv - indicate that all instructions in MBB should use openli.
286 void useIntv(const MachineBasicBlock &MBB);
288 /// useIntv - indicate that all instructions in range should use openli.
289 void useIntv(SlotIndex Start, SlotIndex End);
291 /// leaveIntvAfter - Leave openli after the instruction at Idx.
292 void leaveIntvAfter(SlotIndex Idx);
294 /// leaveIntvAtTop - Leave the interval at the top of MBB.
295 /// Currently, only one value can leave the interval.
296 void leaveIntvAtTop(MachineBasicBlock &MBB);
298 /// closeIntv - Indicate that we are done editing the currently open
299 /// LiveInterval, and ranges can be trimmed.
302 /// rewrite - after all the new live ranges have been created, rewrite
303 /// instructions using curli to use the new intervals.
306 // ===--- High level methods ---===
308 /// splitAroundLoop - Split curli into a separate live interval inside
309 /// the loop. Return true if curli has been completely replaced, false if
310 /// curli is still intact, and needs to be spilled or split further.
311 bool splitAroundLoop(const MachineLoop*);
313 /// splitSingleBlocks - Split curli into a separate live interval inside each
314 /// basic block in Blocks. Return true if curli has been completely replaced,
315 /// false if curli is still intact, and needs to be spilled or split further.
316 bool splitSingleBlocks(const SplitAnalysis::BlockPtrSet &Blocks);
318 /// splitInsideBlock - Split curli into multiple intervals inside MBB. Return
319 /// true if curli has been completely replaced, false if curli is still
320 /// intact, and needs to be spilled or split further.
321 bool splitInsideBlock(const MachineBasicBlock *);