1 //===-------- SplitKit.cpp - 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/IntEqClasses.h"
17 #include "llvm/ADT/SmallPtrSet.h"
18 #include "llvm/CodeGen/SlotIndexes.h"
29 class MachineLoopInfo;
30 class MachineRegisterInfo;
31 class TargetInstrInfo;
32 class TargetRegisterInfo;
37 /// At some point we should just include MachineDominators.h:
38 class MachineDominatorTree;
39 template <class NodeT> class DomTreeNodeBase;
40 typedef DomTreeNodeBase<MachineBasicBlock> MachineDomTreeNode;
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.
48 const MachineFunction *MF;
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.
56 /// compute - Compute the edge bundles for MF. Bundles depend only on the CFG.
57 void compute(const MachineFunction *MF);
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]; }
63 /// getMachineFunction - Return the last machine function computed.
64 const MachineFunction *getMachineFunction() const { return MF; }
66 /// view - Visualize the annotated bipartite CFG with Graphviz.
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 = "");
76 /// SplitAnalysis - Analyze a LiveInterval, looking for live range splitting
80 const MachineFunction &mf_;
81 const LiveIntervals &lis_;
82 const MachineLoopInfo &loops_;
83 const TargetInstrInfo &tii_;
85 // Instructions using the the current register.
86 typedef SmallPtrSet<const MachineInstr*, 16> InstrPtrSet;
87 InstrPtrSet usingInstrs_;
89 // The number of instructions using curli in each basic block.
90 typedef DenseMap<const MachineBasicBlock*, unsigned> BlockCountMap;
91 BlockCountMap usingBlocks_;
93 // The number of basic block using curli in each loop.
94 typedef DenseMap<const MachineLoop*, unsigned> LoopCountMap;
95 LoopCountMap usingLoops_;
98 // Current live interval.
99 const LiveInterval *curli_;
101 // Sumarize statistics by counting instructions using curli_.
104 /// canAnalyzeBranch - Return true if MBB ends in a branch that can be
106 bool canAnalyzeBranch(const MachineBasicBlock *MBB);
109 SplitAnalysis(const MachineFunction &mf, const LiveIntervals &lis,
110 const MachineLoopInfo &mli);
112 /// analyze - set curli to the specified interval, and analyze how it may be
114 void analyze(const LiveInterval *li);
116 /// clear - clear all data structures so SplitAnalysis is ready to analyze a
120 typedef SmallPtrSet<const MachineBasicBlock*, 16> BlockPtrSet;
121 typedef SmallPtrSet<const MachineLoop*, 16> LoopPtrSet;
123 // Print a set of blocks with use counts.
124 void print(const BlockPtrSet&, raw_ostream&) const;
126 // Sets of basic blocks surrounding a machine loop.
128 BlockPtrSet Loop; // Blocks in the loop.
129 BlockPtrSet Preds; // Loop predecessor blocks.
130 BlockPtrSet Exits; // Loop exit blocks.
139 // Print loop blocks with use counts.
140 void print(const LoopBlocks&, raw_ostream&) const;
142 // Calculate the block sets surrounding the loop.
143 void getLoopBlocks(const MachineLoop *Loop, LoopBlocks &Blocks);
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.
154 /// analyzeLoopPeripheralUse - Return an enum describing how curli_ is used in
155 /// and around the Loop.
156 LoopPeripheralUse analyzeLoopPeripheralUse(const LoopBlocks&);
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);
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);
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);
173 /// getSplitLoops - Get the set of loops that have curli uses and would be
174 /// profitable to split.
175 void getSplitLoops(LoopPtrSet&);
177 /// getBestSplitLoop - Return the loop where curli may best be split to a
178 /// separate register, or NULL.
179 const MachineLoop *getBestSplitLoop();
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);
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&);
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);
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();
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.
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 {
213 MachineDominatorTree &mdt_;
215 // The parent interval is never changed.
216 const LiveInterval &parentli_;
218 // The child interval's values are fully contained inside parentli_ values.
221 typedef DenseMap<const VNInfo*, VNInfo*> ValueMap;
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).
229 typedef std::pair<VNInfo*, MachineDomTreeNode*> LiveOutPair;
230 typedef DenseMap<MachineBasicBlock*,LiveOutPair> LiveOutMap;
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:
235 // 1. !liveOutCache_.count(MBB)
236 // 2. liveOutCache_[MBB].second.getNode() == MBB
237 // 3. forall P in preds(MBB): liveOutCache_[P] == liveOutCache_[MBB]
239 // This is only a cache, the values can be computed as:
241 // VNI = li_->getVNInfoAt(lis_.getMBBEndIdx(MBB))
242 // Node = mbt_[lis_.getMBBFromIndex(VNI->def)]
244 // The cache is also used as a visiteed set by mapValue().
245 LiveOutMap liveOutCache_;
248 LiveIntervalMap(LiveIntervals &lis,
249 MachineDominatorTree &mdt,
250 const LiveInterval &parentli)
251 : lis_(lis), mdt_(mdt), parentli_(parentli), li_(0) {}
253 /// reset - clear all data structures and start a new live interval.
254 void reset(LiveInterval *);
256 /// getLI - return the current live interval.
257 LiveInterval *getLI() const { return li_; }
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);
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
274 VNInfo *mapValue(const VNInfo *ParentVNI, SlotIndex Idx, bool *simple = 0);
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);
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);
287 /// isComplexMapped - Return true if ParentVNI has received new definitions
289 bool isComplexMapped(const VNInfo *ParentVNI) const;
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);
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);
302 /// SplitEditor - Edit machine code and LiveIntervals for live range
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().
317 MachineRegisterInfo &mri_;
318 const TargetInstrInfo &tii_;
319 const TargetRegisterInfo &tri_;
321 /// edit_ - The current parent register and new intervals created.
322 LiveRangeEdit &edit_;
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
327 LiveIntervalMap dupli_;
329 /// Currently open LiveInterval.
330 LiveIntervalMap openli_;
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,
337 MachineBasicBlock &MBB,
338 MachineBasicBlock::iterator I);
340 /// intervalsLiveAt - Return true if any member of intervals_ is live at Idx.
341 bool intervalsLiveAt(SlotIndex Idx) const;
343 /// Values in curli whose live range has been truncated when entering an open
345 SmallPtrSet<const VNInfo*, 8> truncatedValues;
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);
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_;
355 /// computeRemainder - Compute the dupli liveness as the complement of all the
357 void computeRemainder();
359 /// rewrite - Rewrite all uses of reg to use the new registers.
360 void rewrite(unsigned reg);
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&);
368 /// getAnalysis - Get the corresponding analysis.
369 SplitAnalysis &getAnalysis() { return sa_; }
371 /// Create a new virtual register and live interval.
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);
378 /// enterIntvAtEnd - Enter openli at the end of MBB.
379 void enterIntvAtEnd(MachineBasicBlock &MBB);
381 /// useIntv - indicate that all instructions in MBB should use openli.
382 void useIntv(const MachineBasicBlock &MBB);
384 /// useIntv - indicate that all instructions in range should use openli.
385 void useIntv(SlotIndex Start, SlotIndex End);
387 /// leaveIntvAfter - Leave openli after the instruction at Idx.
388 void leaveIntvAfter(SlotIndex Idx);
390 /// leaveIntvAtTop - Leave the interval at the top of MBB.
391 /// Currently, only one value can leave the interval.
392 void leaveIntvAtTop(MachineBasicBlock &MBB);
394 /// closeIntv - Indicate that we are done editing the currently open
395 /// LiveInterval, and ranges can be trimmed.
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.
402 // ===--- High level methods ---===
404 /// splitAroundLoop - Split curli into a separate live interval inside
406 void splitAroundLoop(const MachineLoop*);
408 /// splitSingleBlocks - Split curli into a separate live interval inside each
409 /// basic block in Blocks.
410 void splitSingleBlocks(const SplitAnalysis::BlockPtrSet &Blocks);
412 /// splitInsideBlock - Split curli into multiple intervals inside MBB.
413 void splitInsideBlock(const MachineBasicBlock *);