blob: 8487c23b260ec6b697ad9bf7f950a9980895227d [file] [log] [blame]
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +00001//===---------- 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"
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +000017#include "llvm/CodeGen/SlotIndexes.h"
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000018
19namespace llvm {
20
21class LiveInterval;
22class LiveIntervals;
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000023class MachineInstr;
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000024class MachineLoop;
25class MachineLoopInfo;
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +000026class MachineRegisterInfo;
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +000027class TargetInstrInfo;
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +000028class VirtRegMap;
29class VNInfo;
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000030
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +000031/// SplitAnalysis - Analyze a LiveInterval, looking for live range splitting
32/// opportunities.
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000033class SplitAnalysis {
Jakob Stoklund Olesen08e93b12010-08-10 17:07:22 +000034public:
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000035 const MachineFunction &mf_;
36 const LiveIntervals &lis_;
37 const MachineLoopInfo &loops_;
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +000038 const TargetInstrInfo &tii_;
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000039
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000040 // 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
Jakob Stoklund Olesen2dee7a52010-08-12 23:02:55 +000048 // The number of basic block using curli in each loop.
49 typedef DenseMap<const MachineLoop*, unsigned> LoopCountMap;
50 LoopCountMap usingLoops_;
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000051
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +000052private:
53 // Current live interval.
54 const LiveInterval *curli_;
55
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000056 // Sumarize statistics by counting instructions using curli_.
Jakob Stoklund Olesenabff2802010-07-20 16:12:37 +000057 void analyzeUses();
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000058
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +000059 /// canAnalyzeBranch - Return true if MBB ends in a branch that can be
60 /// analyzed.
61 bool canAnalyzeBranch(const MachineBasicBlock *MBB);
62
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000063public:
Jakob Stoklund Olesenf2c6e362010-07-20 23:50:15 +000064 SplitAnalysis(const MachineFunction &mf, const LiveIntervals &lis,
65 const MachineLoopInfo &mli);
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000066
67 /// analyze - set curli to the specified interval, and analyze how it may be
68 /// split.
69 void analyze(const LiveInterval *li);
70
Jakob Stoklund Olesen2dee7a52010-08-12 23:02:55 +000071 /// removeUse - Update statistics by noting that mi no longer uses curli.
72 void removeUse(const MachineInstr *mi);
73
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +000074 const LiveInterval *getCurLI() { return curli_; }
75
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000076 /// clear - clear all data structures so SplitAnalysis is ready to analyze a
77 /// new interval.
78 void clear();
79
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +000080 typedef SmallPtrSet<const MachineBasicBlock*, 16> BlockPtrSet;
Jakob Stoklund Olesen2dee7a52010-08-12 23:02:55 +000081 typedef SmallPtrSet<const MachineLoop*, 16> LoopPtrSet;
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +000082
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
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000099 /// 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.
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000110 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);
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000121
122 /// getBestSplitLoop - Return the loop where curli may best be split to a
123 /// separate register, or NULL.
124 const MachineLoop *getBestSplitLoop();
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000125
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);
Jakob Stoklund Olesenfc412d82010-08-13 21:18:48 +0000130
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();
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000136};
137
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000138
139/// LiveIntervalMap - Map values from a large LiveInterval into a small
140/// interval that is a subset. Insert phi-def values as needed. This class is
141/// used by SplitEditor to create new smaller LiveIntervals.
142///
143/// parentli_ is the larger interval, li_ is the subset interval. Every value
144/// in li_ corresponds to exactly one value in parentli_, and the live range
145/// of the value is contained within the live range of the parentli_ value.
146/// Values in parentli_ may map to any number of openli_ values, including 0.
147class LiveIntervalMap {
148 LiveIntervals &lis_;
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000149
150 // The parent interval is never changed.
151 const LiveInterval &parentli_;
152
153 // The child interval's values are fully contained inside parentli_ values.
Jakob Stoklund Olesen9ca2aeb2010-09-13 23:29:09 +0000154 LiveInterval *li_;
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000155
156 typedef DenseMap<const VNInfo*, VNInfo*> ValueMap;
157
158 // Map parentli_ values to simple values in li_ that are defined at the same
159 // SlotIndex, or NULL for parentli_ values that have complex li_ defs.
160 // Note there is a difference between values mapping to NULL (complex), and
161 // values not present (unknown/unmapped).
162 ValueMap valueMap_;
163
164 // extendTo - Find the last li_ value defined in MBB at or before Idx. The
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000165 // parentli is assumed to be live at Idx. Extend the live range to include
166 // Idx. Return the found VNInfo, or NULL.
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000167 VNInfo *extendTo(MachineBasicBlock *MBB, SlotIndex Idx);
168
169 // addSimpleRange - Add a simple range from parentli_ to li_.
170 // ParentVNI must be live in the [Start;End) interval.
171 void addSimpleRange(SlotIndex Start, SlotIndex End, const VNInfo *ParentVNI);
172
173public:
174 LiveIntervalMap(LiveIntervals &lis,
Jakob Stoklund Olesen9ca2aeb2010-09-13 23:29:09 +0000175 const LiveInterval &parentli)
176 : lis_(lis), parentli_(parentli), li_(0) {}
177
178 /// reset - clear all data structures and start a new live interval.
179 void reset(LiveInterval *);
180
181 /// getLI - return the current live interval.
182 LiveInterval *getLI() const { return li_; }
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000183
184 /// defValue - define a value in li_ from the parentli_ value VNI and Idx.
185 /// Idx does not have to be ParentVNI->def, but it must be contained within
186 /// ParentVNI's live range in parentli_.
187 /// Return the new li_ value.
188 VNInfo *defValue(const VNInfo *ParentVNI, SlotIndex Idx);
189
190 /// mapValue - map ParentVNI to the corresponding li_ value at Idx. It is
191 /// assumed that ParentVNI is live at Idx.
192 /// If ParentVNI has not been defined by defValue, it is assumed that
193 /// ParentVNI->def dominates Idx.
194 /// If ParentVNI has been defined by defValue one or more times, a value that
195 /// dominates Idx will be returned. This may require creating extra phi-def
196 /// values and adding live ranges to li_.
197 VNInfo *mapValue(const VNInfo *ParentVNI, SlotIndex Idx);
198
199 /// addRange - Add live ranges to li_ where [Start;End) intersects parentli_.
200 /// All needed values whose def is not inside [Start;End) must be defined
201 /// beforehand so mapValue will work.
202 void addRange(SlotIndex Start, SlotIndex End);
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000203
204 /// defByCopyFrom - Insert a copy from Reg to li, assuming that Reg carries
205 /// ParentVNI. Add a minimal live range for the new value and return it.
206 VNInfo *defByCopyFrom(unsigned Reg,
207 const VNInfo *ParentVNI,
208 MachineBasicBlock &MBB,
209 MachineBasicBlock::iterator I);
210
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000211};
212
213
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000214/// SplitEditor - Edit machine code and LiveIntervals for live range
215/// splitting.
216///
Jakob Stoklund Olesen5eb308b2010-08-06 22:17:33 +0000217/// - Create a SplitEditor from a SplitAnalysis.
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000218/// - Start a new live interval with openIntv.
219/// - Mark the places where the new interval is entered using enterIntv*
220/// - Mark the ranges where the new interval is used with useIntv*
221/// - Mark the places where the interval is exited with exitIntv*.
222/// - Finish the current interval with closeIntv and repeat from 2.
223/// - Rewrite instructions with rewrite().
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000224///
225class SplitEditor {
226 SplitAnalysis &sa_;
227 LiveIntervals &lis_;
228 VirtRegMap &vrm_;
229 MachineRegisterInfo &mri_;
230 const TargetInstrInfo &tii_;
231
Jakob Stoklund Olesen5eb308b2010-08-06 22:17:33 +0000232 /// curli_ - The immutable interval we are currently splitting.
233 const LiveInterval *const curli_;
234
235 /// dupli_ - Created as a copy of curli_, ranges are carved out as new
236 /// intervals get added through openIntv / closeIntv. This is used to avoid
237 /// editing curli_.
Jakob Stoklund Olesendd9f3fd2010-09-13 23:29:11 +0000238 LiveIntervalMap dupli_;
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000239
240 /// Currently open LiveInterval.
Jakob Stoklund Olesendd9f3fd2010-09-13 23:29:11 +0000241 LiveIntervalMap openli_;
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000242
243 /// createInterval - Create a new virtual register and LiveInterval with same
244 /// register class and spill slot as curli.
245 LiveInterval *createInterval();
246
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000247 /// All the new intervals created for this split are added to intervals_.
Jakob Stoklund Olesen0a2b2a12010-08-13 22:56:53 +0000248 SmallVectorImpl<LiveInterval*> &intervals_;
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000249
250 /// The index into intervals_ of the first interval we added. There may be
251 /// others from before we got it.
252 unsigned firstInterval;
253
254 /// Insert a COPY instruction curli -> li. Allocate a new value from li
255 /// defined by the COPY
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000256 VNInfo *insertCopy(LiveIntervalMap &LI,
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000257 MachineBasicBlock &MBB,
258 MachineBasicBlock::iterator I);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000259
260public:
261 /// Create a new SplitEditor for editing the LiveInterval analyzed by SA.
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000262 /// Newly created intervals will be appended to newIntervals.
263 SplitEditor(SplitAnalysis &SA, LiveIntervals&, VirtRegMap&,
Jakob Stoklund Olesen0a2b2a12010-08-13 22:56:53 +0000264 SmallVectorImpl<LiveInterval*> &newIntervals);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000265
Jakob Stoklund Olesen5eb308b2010-08-06 22:17:33 +0000266 /// getAnalysis - Get the corresponding analysis.
267 SplitAnalysis &getAnalysis() { return sa_; }
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000268
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000269 /// Create a new virtual register and live interval.
270 void openIntv();
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000271
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000272 /// enterIntvBefore - Enter openli before the instruction at Idx. If curli is
273 /// not live before Idx, a COPY is not inserted.
274 void enterIntvBefore(SlotIndex Idx);
275
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000276 /// enterIntvAtEnd - Enter openli at the end of MBB.
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000277 void enterIntvAtEnd(MachineBasicBlock &MBB);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000278
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000279 /// useIntv - indicate that all instructions in MBB should use openli.
280 void useIntv(const MachineBasicBlock &MBB);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000281
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000282 /// useIntv - indicate that all instructions in range should use openli.
283 void useIntv(SlotIndex Start, SlotIndex End);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000284
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000285 /// leaveIntvAfter - Leave openli after the instruction at Idx.
286 void leaveIntvAfter(SlotIndex Idx);
287
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000288 /// leaveIntvAtTop - Leave the interval at the top of MBB.
289 /// Currently, only one value can leave the interval.
290 void leaveIntvAtTop(MachineBasicBlock &MBB);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000291
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000292 /// closeIntv - Indicate that we are done editing the currently open
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000293 /// LiveInterval, and ranges can be trimmed.
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000294 void closeIntv();
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000295
296 /// rewrite - after all the new live ranges have been created, rewrite
297 /// instructions using curli to use the new intervals.
Jakob Stoklund Olesendd9f3fd2010-09-13 23:29:11 +0000298 /// Return true if curli has been completely replaced, false if curli is still
299 /// intact, and needs to be spilled or split further.
300 bool rewrite();
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000301
302 // ===--- High level methods ---===
303
304 /// splitAroundLoop - Split curli into a separate live interval inside
Jakob Stoklund Olesen5eb308b2010-08-06 22:17:33 +0000305 /// the loop. Return true if curli has been completely replaced, false if
306 /// curli is still intact, and needs to be spilled or split further.
307 bool splitAroundLoop(const MachineLoop*);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000308
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000309 /// splitSingleBlocks - Split curli into a separate live interval inside each
310 /// basic block in Blocks. Return true if curli has been completely replaced,
311 /// false if curli is still intact, and needs to be spilled or split further.
312 bool splitSingleBlocks(const SplitAnalysis::BlockPtrSet &Blocks);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000313
Jakob Stoklund Olesenfc412d82010-08-13 21:18:48 +0000314 /// splitInsideBlock - Split curli into multiple intervals inside MBB. Return
315 /// true if curli has been completely replaced, false if curli is still
316 /// intact, and needs to be spilled or split further.
317 bool splitInsideBlock(const MachineBasicBlock *);
318};
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000319
320}