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"
25 class MachineLoopInfo;
26 class MachineRegisterInfo;
27 class TargetInstrInfo;
31 /// SplitAnalysis - Analyze a LiveInterval, looking for live range splitting
35 const MachineFunction &mf_;
36 const LiveIntervals &lis_;
37 const MachineLoopInfo &loops_;
38 const TargetInstrInfo &tii_;
40 // Instructions using the the current register.
41 typedef SmallPtrSet<const MachineInstr*, 16> InstrPtrSet;
42 InstrPtrSet usingInstrs_;
44 // The number of instructions using curli in each basic block.
45 typedef DenseMap<const MachineBasicBlock*, unsigned> BlockCountMap;
46 BlockCountMap usingBlocks_;
48 // The number of basic block using curli in each loop.
49 typedef DenseMap<const MachineLoop*, unsigned> LoopCountMap;
50 LoopCountMap usingLoops_;
53 // Current live interval.
54 const LiveInterval *curli_;
56 // Sumarize statistics by counting instructions using curli_.
59 /// canAnalyzeBranch - Return true if MBB ends in a branch that can be
61 bool canAnalyzeBranch(const MachineBasicBlock *MBB);
64 SplitAnalysis(const MachineFunction &mf, const LiveIntervals &lis,
65 const MachineLoopInfo &mli);
67 /// analyze - set curli to the specified interval, and analyze how it may be
69 void analyze(const LiveInterval *li);
71 const LiveInterval *getCurLI() { return curli_; }
73 /// clear - clear all data structures so SplitAnalysis is ready to analyze a
77 typedef SmallPtrSet<const MachineBasicBlock*, 16> BlockPtrSet;
78 typedef SmallPtrSet<const MachineLoop*, 16> LoopPtrSet;
80 // Sets of basic blocks surrounding a machine loop.
82 BlockPtrSet Loop; // Blocks in the loop.
83 BlockPtrSet Preds; // Loop predecessor blocks.
84 BlockPtrSet Exits; // Loop exit blocks.
93 // Calculate the block sets surrounding the loop.
94 void getLoopBlocks(const MachineLoop *Loop, LoopBlocks &Blocks);
96 /// LoopPeripheralUse - how is a variable used in and around a loop?
97 /// Peripheral blocks are the loop predecessors and exit blocks.
98 enum LoopPeripheralUse {
99 ContainedInLoop, // All uses are inside the loop.
100 SinglePeripheral, // At most one instruction per peripheral block.
101 MultiPeripheral, // Multiple instructions in some peripheral blocks.
102 OutsideLoop // Uses outside loop periphery.
105 /// analyzeLoopPeripheralUse - Return an enum describing how curli_ is used in
106 /// and around the Loop.
107 LoopPeripheralUse analyzeLoopPeripheralUse(const LoopBlocks&);
109 /// getCriticalExits - It may be necessary to partially break critical edges
110 /// leaving the loop if an exit block has phi uses of curli. Collect the exit
111 /// blocks that need special treatment into CriticalExits.
112 void getCriticalExits(const LoopBlocks &Blocks, BlockPtrSet &CriticalExits);
114 /// canSplitCriticalExits - Return true if it is possible to insert new exit
115 /// blocks before the blocks in CriticalExits.
116 bool canSplitCriticalExits(const LoopBlocks &Blocks,
117 BlockPtrSet &CriticalExits);
119 /// getBestSplitLoop - Return the loop where curli may best be split to a
120 /// separate register, or NULL.
121 const MachineLoop *getBestSplitLoop();
123 /// getMultiUseBlocks - Add basic blocks to Blocks that may benefit from
124 /// having curli split to a new live interval. Return true if Blocks can be
125 /// passed to SplitEditor::splitSingleBlocks.
126 bool getMultiUseBlocks(BlockPtrSet &Blocks);
128 /// getBlockForInsideSplit - If curli is contained inside a single basic block,
129 /// and it wou pay to subdivide the interval inside that block, return it.
130 /// Otherwise return NULL. The returned block can be passed to
131 /// SplitEditor::splitInsideBlock.
132 const MachineBasicBlock *getBlockForInsideSplit();
136 /// LiveIntervalMap - Map values from a large LiveInterval into a small
137 /// interval that is a subset. Insert phi-def values as needed. This class is
138 /// used by SplitEditor to create new smaller LiveIntervals.
140 /// parentli_ is the larger interval, li_ is the subset interval. Every value
141 /// in li_ corresponds to exactly one value in parentli_, and the live range
142 /// of the value is contained within the live range of the parentli_ value.
143 /// Values in parentli_ may map to any number of openli_ values, including 0.
144 class LiveIntervalMap {
147 // The parent interval is never changed.
148 const LiveInterval &parentli_;
150 // The child interval's values are fully contained inside parentli_ values.
153 typedef DenseMap<const VNInfo*, VNInfo*> ValueMap;
155 // Map parentli_ values to simple values in li_ that are defined at the same
156 // SlotIndex, or NULL for parentli_ values that have complex li_ defs.
157 // Note there is a difference between values mapping to NULL (complex), and
158 // values not present (unknown/unmapped).
162 LiveIntervalMap(LiveIntervals &lis,
163 const LiveInterval &parentli)
164 : lis_(lis), parentli_(parentli), li_(0) {}
166 /// reset - clear all data structures and start a new live interval.
167 void reset(LiveInterval *);
169 /// getLI - return the current live interval.
170 LiveInterval *getLI() const { return li_; }
172 /// defValue - define a value in li_ from the parentli_ value VNI and Idx.
173 /// Idx does not have to be ParentVNI->def, but it must be contained within
174 /// ParentVNI's live range in parentli_.
175 /// Return the new li_ value.
176 VNInfo *defValue(const VNInfo *ParentVNI, SlotIndex Idx);
178 /// mapValue - map ParentVNI to the corresponding li_ value at Idx. It is
179 /// assumed that ParentVNI is live at Idx.
180 /// If ParentVNI has not been defined by defValue, it is assumed that
181 /// ParentVNI->def dominates Idx.
182 /// If ParentVNI has been defined by defValue one or more times, a value that
183 /// dominates Idx will be returned. This may require creating extra phi-def
184 /// values and adding live ranges to li_.
185 /// If simple is not NULL, *simple will indicate if ParentVNI is a simply
187 VNInfo *mapValue(const VNInfo *ParentVNI, SlotIndex Idx, bool *simple = 0);
189 // extendTo - Find the last li_ value defined in MBB at or before Idx. The
190 // parentli is assumed to be live at Idx. Extend the live range to include
191 // Idx. Return the found VNInfo, or NULL.
192 VNInfo *extendTo(MachineBasicBlock *MBB, SlotIndex Idx);
194 /// isMapped - Return true is ParentVNI is a known mapped value. It may be a
195 /// simple 1-1 mapping or a complex mapping to later defs.
196 bool isMapped(const VNInfo *ParentVNI) const {
197 return valueMap_.count(ParentVNI);
200 /// isComplexMapped - Return true if ParentVNI has received new definitions
202 bool isComplexMapped(const VNInfo *ParentVNI) const;
204 // addSimpleRange - Add a simple range from parentli_ to li_.
205 // ParentVNI must be live in the [Start;End) interval.
206 void addSimpleRange(SlotIndex Start, SlotIndex End, const VNInfo *ParentVNI);
208 /// addRange - Add live ranges to li_ where [Start;End) intersects parentli_.
209 /// All needed values whose def is not inside [Start;End) must be defined
210 /// beforehand so mapValue will work.
211 void addRange(SlotIndex Start, SlotIndex End);
213 /// defByCopyFrom - Insert a copy from Reg to li, assuming that Reg carries
214 /// ParentVNI. Add a minimal live range for the new value and return it.
215 VNInfo *defByCopyFrom(unsigned Reg,
216 const VNInfo *ParentVNI,
217 MachineBasicBlock &MBB,
218 MachineBasicBlock::iterator I);
223 /// SplitEditor - Edit machine code and LiveIntervals for live range
226 /// - Create a SplitEditor from a SplitAnalysis.
227 /// - Start a new live interval with openIntv.
228 /// - Mark the places where the new interval is entered using enterIntv*
229 /// - Mark the ranges where the new interval is used with useIntv*
230 /// - Mark the places where the interval is exited with exitIntv*.
231 /// - Finish the current interval with closeIntv and repeat from 2.
232 /// - Rewrite instructions with rewrite().
238 MachineRegisterInfo &mri_;
239 const TargetInstrInfo &tii_;
241 /// curli_ - The immutable interval we are currently splitting.
242 const LiveInterval *const curli_;
244 /// dupli_ - Created as a copy of curli_, ranges are carved out as new
245 /// intervals get added through openIntv / closeIntv. This is used to avoid
247 LiveIntervalMap dupli_;
249 /// Currently open LiveInterval.
250 LiveIntervalMap openli_;
252 /// createInterval - Create a new virtual register and LiveInterval with same
253 /// register class and spill slot as curli.
254 LiveInterval *createInterval();
256 /// All the new intervals created for this split are added to intervals_.
257 SmallVectorImpl<LiveInterval*> &intervals_;
259 /// The index into intervals_ of the first interval we added. There may be
260 /// others from before we got it.
261 unsigned firstInterval;
263 /// intervalsLiveAt - Return true if any member of intervals_ is live at Idx.
264 bool intervalsLiveAt(SlotIndex Idx) const;
266 /// Values in curli whose live range has been truncated when entering an open
268 SmallPtrSet<const VNInfo*, 8> truncatedValues;
270 /// addTruncSimpleRange - Add the given simple range to dupli_ after
271 /// truncating any overlap with intervals_.
272 void addTruncSimpleRange(SlotIndex Start, SlotIndex End, VNInfo *VNI);
275 /// Create a new SplitEditor for editing the LiveInterval analyzed by SA.
276 /// Newly created intervals will be appended to newIntervals.
277 SplitEditor(SplitAnalysis &SA, LiveIntervals&, VirtRegMap&,
278 SmallVectorImpl<LiveInterval*> &newIntervals);
280 /// getAnalysis - Get the corresponding analysis.
281 SplitAnalysis &getAnalysis() { return sa_; }
283 /// Create a new virtual register and live interval.
286 /// enterIntvBefore - Enter openli before the instruction at Idx. If curli is
287 /// not live before Idx, a COPY is not inserted.
288 void enterIntvBefore(SlotIndex Idx);
290 /// enterIntvAtEnd - Enter openli at the end of MBB.
291 void enterIntvAtEnd(MachineBasicBlock &MBB);
293 /// useIntv - indicate that all instructions in MBB should use openli.
294 void useIntv(const MachineBasicBlock &MBB);
296 /// useIntv - indicate that all instructions in range should use openli.
297 void useIntv(SlotIndex Start, SlotIndex End);
299 /// leaveIntvAfter - Leave openli after the instruction at Idx.
300 void leaveIntvAfter(SlotIndex Idx);
302 /// leaveIntvAtTop - Leave the interval at the top of MBB.
303 /// Currently, only one value can leave the interval.
304 void leaveIntvAtTop(MachineBasicBlock &MBB);
306 /// closeIntv - Indicate that we are done editing the currently open
307 /// LiveInterval, and ranges can be trimmed.
310 /// rewrite - after all the new live ranges have been created, rewrite
311 /// instructions using curli to use the new intervals.
314 // ===--- High level methods ---===
316 /// splitAroundLoop - Split curli into a separate live interval inside
318 void splitAroundLoop(const MachineLoop*);
320 /// splitSingleBlocks - Split curli into a separate live interval inside each
321 /// basic block in Blocks.
322 void splitSingleBlocks(const SplitAnalysis::BlockPtrSet &Blocks);
324 /// splitInsideBlock - Split curli into multiple intervals inside MBB.
325 void splitInsideBlock(const MachineBasicBlock *);