1 //===-------- SplitKit.h - Toolkit for splitting live ranges ----*- C++ -*-===//
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/DenseMap.h"
16 #include "llvm/ADT/IntervalMap.h"
17 #include "llvm/ADT/SmallPtrSet.h"
18 #include "llvm/CodeGen/SlotIndexes.h"
22 class ConnectedVNInfoEqClasses;
28 class MachineLoopInfo;
29 class MachineRegisterInfo;
30 class TargetInstrInfo;
31 class TargetRegisterInfo;
36 /// At some point we should just include MachineDominators.h:
37 class MachineDominatorTree;
38 template <class NodeT> class DomTreeNodeBase;
39 typedef DomTreeNodeBase<MachineBasicBlock> MachineDomTreeNode;
42 /// SplitAnalysis - Analyze a LiveInterval, looking for live range splitting
46 const MachineFunction &MF;
47 const LiveIntervals &LIS;
48 const MachineLoopInfo &Loops;
49 const TargetInstrInfo &TII;
51 // Instructions using the the current register.
52 typedef SmallPtrSet<const MachineInstr*, 16> InstrPtrSet;
53 InstrPtrSet UsingInstrs;
55 // Sorted slot indexes of using instructions.
56 SmallVector<SlotIndex, 8> UseSlots;
58 // The number of instructions using CurLI in each basic block.
59 typedef DenseMap<const MachineBasicBlock*, unsigned> BlockCountMap;
60 BlockCountMap UsingBlocks;
62 // The number of basic block using CurLI in each loop.
63 typedef DenseMap<const MachineLoop*, unsigned> LoopCountMap;
64 LoopCountMap UsingLoops;
67 // Current live interval.
68 const LiveInterval *CurLI;
70 // Sumarize statistics by counting instructions using CurLI.
73 /// canAnalyzeBranch - Return true if MBB ends in a branch that can be
75 bool canAnalyzeBranch(const MachineBasicBlock *MBB);
78 SplitAnalysis(const MachineFunction &mf, const LiveIntervals &lis,
79 const MachineLoopInfo &mli);
81 /// analyze - set CurLI to the specified interval, and analyze how it may be
83 void analyze(const LiveInterval *li);
85 /// clear - clear all data structures so SplitAnalysis is ready to analyze a
89 /// hasUses - Return true if MBB has any uses of CurLI.
90 bool hasUses(const MachineBasicBlock *MBB) const {
91 return UsingBlocks.lookup(MBB);
94 typedef SmallPtrSet<const MachineBasicBlock*, 16> BlockPtrSet;
95 typedef SmallPtrSet<const MachineLoop*, 16> LoopPtrSet;
97 // Print a set of blocks with use counts.
98 void print(const BlockPtrSet&, raw_ostream&) const;
100 // Sets of basic blocks surrounding a machine loop.
102 BlockPtrSet Loop; // Blocks in the loop.
103 BlockPtrSet Preds; // Loop predecessor blocks.
104 BlockPtrSet Exits; // Loop exit blocks.
113 // Print loop blocks with use counts.
114 void print(const LoopBlocks&, raw_ostream&) const;
116 // Calculate the block sets surrounding the loop.
117 void getLoopBlocks(const MachineLoop *Loop, LoopBlocks &Blocks);
119 /// LoopPeripheralUse - how is a variable used in and around a loop?
120 /// Peripheral blocks are the loop predecessors and exit blocks.
121 enum LoopPeripheralUse {
122 ContainedInLoop, // All uses are inside the loop.
123 SinglePeripheral, // At most one instruction per peripheral block.
124 MultiPeripheral, // Multiple instructions in some peripheral blocks.
125 OutsideLoop // Uses outside loop periphery.
128 /// analyzeLoopPeripheralUse - Return an enum describing how CurLI is used in
129 /// and around the Loop.
130 LoopPeripheralUse analyzeLoopPeripheralUse(const LoopBlocks&);
132 /// getCriticalExits - It may be necessary to partially break critical edges
133 /// leaving the loop if an exit block has phi uses of CurLI. Collect the exit
134 /// blocks that need special treatment into CriticalExits.
135 void getCriticalExits(const LoopBlocks &Blocks, BlockPtrSet &CriticalExits);
137 /// canSplitCriticalExits - Return true if it is possible to insert new exit
138 /// blocks before the blocks in CriticalExits.
139 bool canSplitCriticalExits(const LoopBlocks &Blocks,
140 BlockPtrSet &CriticalExits);
142 /// getCriticalPreds - Get the set of loop predecessors with critical edges to
143 /// blocks outside the loop that have CurLI live in. We don't have to break
144 /// these edges, but they do require special treatment.
145 void getCriticalPreds(const LoopBlocks &Blocks, BlockPtrSet &CriticalPreds);
147 /// getSplitLoops - Get the set of loops that have CurLI uses and would be
148 /// profitable to split.
149 void getSplitLoops(LoopPtrSet&);
151 /// getBestSplitLoop - Return the loop where CurLI may best be split to a
152 /// separate register, or NULL.
153 const MachineLoop *getBestSplitLoop();
155 /// isBypassLoop - Return true if CurLI is live through Loop and has no uses
156 /// inside the loop. Bypass loops are candidates for splitting because it can
157 /// prevent interference inside the loop.
158 bool isBypassLoop(const MachineLoop *Loop);
160 /// getBypassLoops - Get all the maximal bypass loops. These are the bypass
161 /// loops whose parent is not a bypass loop.
162 void getBypassLoops(LoopPtrSet&);
164 /// getMultiUseBlocks - Add basic blocks to Blocks that may benefit from
165 /// having CurLI split to a new live interval. Return true if Blocks can be
166 /// passed to SplitEditor::splitSingleBlocks.
167 bool getMultiUseBlocks(BlockPtrSet &Blocks);
169 /// getBlockForInsideSplit - If CurLI is contained inside a single basic
170 /// block, and it would pay to subdivide the interval inside that block,
171 /// return it. Otherwise return NULL. The returned block can be passed to
172 /// SplitEditor::splitInsideBlock.
173 const MachineBasicBlock *getBlockForInsideSplit();
177 /// LiveIntervalMap - Map values from a large LiveInterval into a small
178 /// interval that is a subset. Insert phi-def values as needed. This class is
179 /// used by SplitEditor to create new smaller LiveIntervals.
181 /// ParentLI is the larger interval, LI is the subset interval. Every value
182 /// in LI corresponds to exactly one value in ParentLI, and the live range
183 /// of the value is contained within the live range of the ParentLI value.
184 /// Values in ParentLI may map to any number of OpenLI values, including 0.
185 class LiveIntervalMap {
187 MachineDominatorTree &MDT;
189 // The parent interval is never changed.
190 const LiveInterval &ParentLI;
192 // The child interval's values are fully contained inside ParentLI values.
195 typedef DenseMap<const VNInfo*, VNInfo*> ValueMap;
197 // Map ParentLI values to simple values in LI that are defined at the same
198 // SlotIndex, or NULL for ParentLI values that have complex LI defs.
199 // Note there is a difference between values mapping to NULL (complex), and
200 // values not present (unknown/unmapped).
203 typedef std::pair<VNInfo*, MachineDomTreeNode*> LiveOutPair;
204 typedef DenseMap<MachineBasicBlock*,LiveOutPair> LiveOutMap;
206 // LiveOutCache - Map each basic block where LI is live out to the live-out
207 // value and its defining block. One of these conditions shall be true:
209 // 1. !LiveOutCache.count(MBB)
210 // 2. LiveOutCache[MBB].second.getNode() == MBB
211 // 3. forall P in preds(MBB): LiveOutCache[P] == LiveOutCache[MBB]
213 // This is only a cache, the values can be computed as:
215 // VNI = LI->getVNInfoAt(LIS.getMBBEndIdx(MBB))
216 // Node = mbt_[LIS.getMBBFromIndex(VNI->def)]
218 // The cache is also used as a visiteed set by mapValue().
219 LiveOutMap LiveOutCache;
221 // Dump the live-out cache to dbgs().
225 LiveIntervalMap(LiveIntervals &lis,
226 MachineDominatorTree &mdt,
227 const LiveInterval &parentli)
228 : LIS(lis), MDT(mdt), ParentLI(parentli), LI(0) {}
230 /// reset - clear all data structures and start a new live interval.
231 void reset(LiveInterval *);
233 /// getLI - return the current live interval.
234 LiveInterval *getLI() const { return LI; }
236 /// defValue - define a value in LI from the ParentLI value VNI and Idx.
237 /// Idx does not have to be ParentVNI->def, but it must be contained within
238 /// ParentVNI's live range in ParentLI.
239 /// Return the new LI value.
240 VNInfo *defValue(const VNInfo *ParentVNI, SlotIndex Idx);
242 /// mapValue - map ParentVNI to the corresponding LI value at Idx. It is
243 /// assumed that ParentVNI is live at Idx.
244 /// If ParentVNI has not been defined by defValue, it is assumed that
245 /// ParentVNI->def dominates Idx.
246 /// If ParentVNI has been defined by defValue one or more times, a value that
247 /// dominates Idx will be returned. This may require creating extra phi-def
248 /// values and adding live ranges to LI.
249 /// If simple is not NULL, *simple will indicate if ParentVNI is a simply
251 VNInfo *mapValue(const VNInfo *ParentVNI, SlotIndex Idx, bool *simple = 0);
253 // extendTo - Find the last LI value defined in MBB at or before Idx. The
254 // parentli is assumed to be live at Idx. Extend the live range to include
255 // Idx. Return the found VNInfo, or NULL.
256 VNInfo *extendTo(const MachineBasicBlock *MBB, SlotIndex Idx);
258 /// isMapped - Return true is ParentVNI is a known mapped value. It may be a
259 /// simple 1-1 mapping or a complex mapping to later defs.
260 bool isMapped(const VNInfo *ParentVNI) const {
261 return Values.count(ParentVNI);
264 /// isComplexMapped - Return true if ParentVNI has received new definitions
266 bool isComplexMapped(const VNInfo *ParentVNI) const;
268 /// markComplexMapped - Mark ParentVNI as complex mapped regardless of the
269 /// number of definitions.
270 void markComplexMapped(const VNInfo *ParentVNI) { Values[ParentVNI] = 0; }
272 // addSimpleRange - Add a simple range from ParentLI to LI.
273 // ParentVNI must be live in the [Start;End) interval.
274 void addSimpleRange(SlotIndex Start, SlotIndex End, const VNInfo *ParentVNI);
276 /// addRange - Add live ranges to LI where [Start;End) intersects ParentLI.
277 /// All needed values whose def is not inside [Start;End) must be defined
278 /// beforehand so mapValue will work.
279 void addRange(SlotIndex Start, SlotIndex End);
283 /// SplitEditor - Edit machine code and LiveIntervals for live range
286 /// - Create a SplitEditor from a SplitAnalysis.
287 /// - Start a new live interval with openIntv.
288 /// - Mark the places where the new interval is entered using enterIntv*
289 /// - Mark the ranges where the new interval is used with useIntv*
290 /// - Mark the places where the interval is exited with exitIntv*.
291 /// - Finish the current interval with closeIntv and repeat from 2.
292 /// - Rewrite instructions with finish().
298 MachineRegisterInfo &MRI;
299 MachineDominatorTree &MDT;
300 const TargetInstrInfo &TII;
301 const TargetRegisterInfo &TRI;
303 /// Edit - The current parent register and new intervals created.
306 /// Index into Edit of the currently open interval.
307 /// The index 0 is used for the complement, so the first interval started by
308 /// openIntv will be 1.
311 typedef IntervalMap<SlotIndex, unsigned> RegAssignMap;
313 /// Allocator for the interval map. This will eventually be shared with
314 /// SlotIndexes and LiveIntervals.
315 RegAssignMap::Allocator Allocator;
317 /// RegAssign - Map of the assigned register indexes.
318 /// Edit.get(RegAssign.lookup(Idx)) is the register that should be live at
320 RegAssignMap RegAssign;
322 /// LIMappers - One LiveIntervalMap or each interval in Edit.
323 SmallVector<LiveIntervalMap, 4> LIMappers;
325 /// defFromParent - Define Reg from ParentVNI at UseIdx using either
326 /// rematerialization or a COPY from parent. Return the new value.
327 VNInfo *defFromParent(unsigned RegIdx,
330 MachineBasicBlock &MBB,
331 MachineBasicBlock::iterator I);
333 /// rewriteAssigned - Rewrite all uses of Edit.getReg() to assigned registers.
334 void rewriteAssigned();
336 /// rewriteComponents - Rewrite all uses of Intv[0] according to the eq
337 /// classes in ConEQ.
338 /// This must be done when Intvs[0] is styill live at all uses, before calling
339 /// ConEq.Distribute().
340 void rewriteComponents(const SmallVectorImpl<LiveInterval*> &Intvs,
341 const ConnectedVNInfoEqClasses &ConEq);
344 /// Create a new SplitEditor for editing the LiveInterval analyzed by SA.
345 /// Newly created intervals will be appended to newIntervals.
346 SplitEditor(SplitAnalysis &SA, LiveIntervals&, VirtRegMap&,
347 MachineDominatorTree&, LiveRangeEdit&);
349 /// getAnalysis - Get the corresponding analysis.
350 SplitAnalysis &getAnalysis() { return sa_; }
352 /// Create a new virtual register and live interval.
355 /// enterIntvBefore - Enter the open interval before the instruction at Idx.
356 /// If the parent interval is not live before Idx, a COPY is not inserted.
357 /// Return the beginning of the new live range.
358 SlotIndex enterIntvBefore(SlotIndex Idx);
360 /// enterIntvAtEnd - Enter the open interval at the end of MBB.
361 /// Use the open interval from he inserted copy to the MBB end.
362 /// Return the beginning of the new live range.
363 SlotIndex enterIntvAtEnd(MachineBasicBlock &MBB);
365 /// useIntv - indicate that all instructions in MBB should use OpenLI.
366 void useIntv(const MachineBasicBlock &MBB);
368 /// useIntv - indicate that all instructions in range should use OpenLI.
369 void useIntv(SlotIndex Start, SlotIndex End);
371 /// leaveIntvAfter - Leave the open interval after the instruction at Idx.
372 /// Return the end of the live range.
373 SlotIndex leaveIntvAfter(SlotIndex Idx);
375 /// leaveIntvAtTop - Leave the interval at the top of MBB.
376 /// Add liveness from the MBB top to the copy.
377 /// Return the end of the live range.
378 SlotIndex leaveIntvAtTop(MachineBasicBlock &MBB);
380 /// overlapIntv - Indicate that all instructions in range should use the open
381 /// interval, but also let the complement interval be live.
383 /// This doubles the register pressure, but is sometimes required to deal with
384 /// register uses after the last valid split point.
386 /// The Start index should be a return value from a leaveIntv* call, and End
387 /// should be in the same basic block. The parent interval must have the same
388 /// value across the range.
390 void overlapIntv(SlotIndex Start, SlotIndex End);
392 /// closeIntv - Indicate that we are done editing the currently open
393 /// LiveInterval, and ranges can be trimmed.
396 /// finish - after all the new live ranges have been created, compute the
397 /// remaining live range, and rewrite instructions to use the new registers.
400 /// dump - print the current interval maping to dbgs().
403 // ===--- High level methods ---===
405 /// splitAroundLoop - Split CurLI into a separate live interval inside
407 void splitAroundLoop(const MachineLoop*);
409 /// splitSingleBlocks - Split CurLI into a separate live interval inside each
410 /// basic block in Blocks.
411 void splitSingleBlocks(const SplitAnalysis::BlockPtrSet &Blocks);
413 /// splitInsideBlock - Split CurLI into multiple intervals inside MBB.
414 void splitInsideBlock(const MachineBasicBlock *);