blob: 622d7e77496d9ef03707604d5d8b36553fad23a7 [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 Olesen1407c842010-08-18 19:00:08 +000023class MachineDominatorTree;
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000024class MachineInstr;
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000025class MachineLoop;
26class MachineLoopInfo;
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +000027class MachineRegisterInfo;
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +000028class TargetInstrInfo;
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +000029class VirtRegMap;
30class VNInfo;
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000031
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +000032/// SplitAnalysis - Analyze a LiveInterval, looking for live range splitting
33/// opportunities.
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000034class SplitAnalysis {
Jakob Stoklund Olesen08e93b12010-08-10 17:07:22 +000035public:
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000036 const MachineFunction &mf_;
37 const LiveIntervals &lis_;
38 const MachineLoopInfo &loops_;
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +000039 const TargetInstrInfo &tii_;
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000040
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000041 // Instructions using the the current register.
42 typedef SmallPtrSet<const MachineInstr*, 16> InstrPtrSet;
43 InstrPtrSet usingInstrs_;
44
45 // The number of instructions using curli in each basic block.
46 typedef DenseMap<const MachineBasicBlock*, unsigned> BlockCountMap;
47 BlockCountMap usingBlocks_;
48
Jakob Stoklund Olesen2dee7a52010-08-12 23:02:55 +000049 // The number of basic block using curli in each loop.
50 typedef DenseMap<const MachineLoop*, unsigned> LoopCountMap;
51 LoopCountMap usingLoops_;
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000052
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +000053private:
54 // Current live interval.
55 const LiveInterval *curli_;
56
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000057 // Sumarize statistics by counting instructions using curli_.
Jakob Stoklund Olesenabff2802010-07-20 16:12:37 +000058 void analyzeUses();
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000059
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +000060 /// canAnalyzeBranch - Return true if MBB ends in a branch that can be
61 /// analyzed.
62 bool canAnalyzeBranch(const MachineBasicBlock *MBB);
63
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000064public:
Jakob Stoklund Olesenf2c6e362010-07-20 23:50:15 +000065 SplitAnalysis(const MachineFunction &mf, const LiveIntervals &lis,
66 const MachineLoopInfo &mli);
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000067
68 /// analyze - set curli to the specified interval, and analyze how it may be
69 /// split.
70 void analyze(const LiveInterval *li);
71
Jakob Stoklund Olesen2dee7a52010-08-12 23:02:55 +000072 /// removeUse - Update statistics by noting that mi no longer uses curli.
73 void removeUse(const MachineInstr *mi);
74
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +000075 const LiveInterval *getCurLI() { return curli_; }
76
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000077 /// clear - clear all data structures so SplitAnalysis is ready to analyze a
78 /// new interval.
79 void clear();
80
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +000081 typedef SmallPtrSet<const MachineBasicBlock*, 16> BlockPtrSet;
Jakob Stoklund Olesen2dee7a52010-08-12 23:02:55 +000082 typedef SmallPtrSet<const MachineLoop*, 16> LoopPtrSet;
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +000083
84 // Sets of basic blocks surrounding a machine loop.
85 struct LoopBlocks {
86 BlockPtrSet Loop; // Blocks in the loop.
87 BlockPtrSet Preds; // Loop predecessor blocks.
88 BlockPtrSet Exits; // Loop exit blocks.
89
90 void clear() {
91 Loop.clear();
92 Preds.clear();
93 Exits.clear();
94 }
95 };
96
97 // Calculate the block sets surrounding the loop.
98 void getLoopBlocks(const MachineLoop *Loop, LoopBlocks &Blocks);
99
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000100 /// 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.
107 };
108
109 /// analyzeLoopPeripheralUse - Return an enum describing how curli_ is used in
110 /// and around the Loop.
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000111 LoopPeripheralUse analyzeLoopPeripheralUse(const LoopBlocks&);
112
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);
117
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);
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000122
123 /// getBestSplitLoop - Return the loop where curli may best be split to a
124 /// separate register, or NULL.
125 const MachineLoop *getBestSplitLoop();
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000126
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);
Jakob Stoklund Olesenfc412d82010-08-13 21:18:48 +0000131
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();
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000137};
138
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000139
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.
143///
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.
148class LiveIntervalMap {
149 LiveIntervals &lis_;
150 MachineDominatorTree &dt_;
151
152 // The parent interval is never changed.
153 const LiveInterval &parentli_;
154
155 // The child interval's values are fully contained inside parentli_ values.
156 LiveInterval &li_;
157
158 typedef DenseMap<const VNInfo*, VNInfo*> ValueMap;
159
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).
164 ValueMap valueMap_;
165
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);
170
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);
174
175public:
176 LiveIntervalMap(LiveIntervals &lis,
177 MachineDominatorTree &dt,
178 const LiveInterval &parentli,
179 LiveInterval &li)
180 : lis_(lis), dt_(dt), parentli_(parentli), li_(li) {}
181
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);
187
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);
196
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);
201};
202
203
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000204/// SplitEditor - Edit machine code and LiveIntervals for live range
205/// splitting.
206///
Jakob Stoklund Olesen5eb308b2010-08-06 22:17:33 +0000207/// - Create a SplitEditor from a SplitAnalysis.
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000208/// - 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().
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000214///
215class SplitEditor {
216 SplitAnalysis &sa_;
217 LiveIntervals &lis_;
218 VirtRegMap &vrm_;
219 MachineRegisterInfo &mri_;
220 const TargetInstrInfo &tii_;
221
Jakob Stoklund Olesen5eb308b2010-08-06 22:17:33 +0000222 /// curli_ - The immutable interval we are currently splitting.
223 const LiveInterval *const curli_;
224
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
227 /// editing curli_.
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000228 LiveInterval *dupli_;
229
230 /// Currently open LiveInterval.
231 LiveInterval *openli_;
232
233 /// createInterval - Create a new virtual register and LiveInterval with same
234 /// register class and spill slot as curli.
235 LiveInterval *createInterval();
236
Jakob Stoklund Olesen5eb308b2010-08-06 22:17:33 +0000237 /// getDupLI - Ensure dupli is created and return it.
238 LiveInterval *getDupLI();
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000239
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000240 /// valueMap_ - Map values in cupli to values in openli. These are direct 1-1
Jakob Stoklund Olesen5eb308b2010-08-06 22:17:33 +0000241 /// mappings, and do not include values created by inserted copies.
242 DenseMap<const VNInfo*, VNInfo*> valueMap_;
243
244 /// mapValue - Return the openIntv value that corresponds to the given curli
245 /// value.
246 VNInfo *mapValue(const VNInfo *curliVNI);
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000247
248 /// A dupli value is live through openIntv.
249 bool liveThrough_;
250
251 /// All the new intervals created for this split are added to intervals_.
Jakob Stoklund Olesen0a2b2a12010-08-13 22:56:53 +0000252 SmallVectorImpl<LiveInterval*> &intervals_;
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000253
254 /// The index into intervals_ of the first interval we added. There may be
255 /// others from before we got it.
256 unsigned firstInterval;
257
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);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000263
264public:
265 /// Create a new SplitEditor for editing the LiveInterval analyzed by SA.
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000266 /// Newly created intervals will be appended to newIntervals.
267 SplitEditor(SplitAnalysis &SA, LiveIntervals&, VirtRegMap&,
Jakob Stoklund Olesen0a2b2a12010-08-13 22:56:53 +0000268 SmallVectorImpl<LiveInterval*> &newIntervals);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000269
Jakob Stoklund Olesen5eb308b2010-08-06 22:17:33 +0000270 /// getAnalysis - Get the corresponding analysis.
271 SplitAnalysis &getAnalysis() { return sa_; }
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000272
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000273 /// Create a new virtual register and live interval.
274 void openIntv();
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000275
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000276 /// 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);
279
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000280 /// 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);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000284
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000285 /// useIntv - indicate that all instructions in MBB should use openli.
286 void useIntv(const MachineBasicBlock &MBB);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000287
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000288 /// useIntv - indicate that all instructions in range should use openli.
289 void useIntv(SlotIndex Start, SlotIndex End);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000290
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000291 /// leaveIntvAfter - Leave openli after the instruction at Idx.
292 void leaveIntvAfter(SlotIndex Idx);
293
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000294 /// leaveIntvAtTop - Leave the interval at the top of MBB.
295 /// Currently, only one value can leave the interval.
296 void leaveIntvAtTop(MachineBasicBlock &MBB);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000297
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000298 /// closeIntv - Indicate that we are done editing the currently open
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000299 /// LiveInterval, and ranges can be trimmed.
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000300 void closeIntv();
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000301
302 /// rewrite - after all the new live ranges have been created, rewrite
303 /// instructions using curli to use the new intervals.
304 void rewrite();
305
306 // ===--- High level methods ---===
307
308 /// splitAroundLoop - Split curli into a separate live interval inside
Jakob Stoklund Olesen5eb308b2010-08-06 22:17:33 +0000309 /// 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*);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000312
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000313 /// 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);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000317
Jakob Stoklund Olesenfc412d82010-08-13 21:18:48 +0000318 /// 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 *);
322};
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000323
324}