blob: 9c109dc4d5f1008136878cff0a047d5f755aeba2 [file] [log] [blame]
Andrew Trick14e8d712010-10-22 23:09:15 +00001//===-------- SplitKit.cpp - Toolkit for splitting live ranges --*- C++ -*-===//
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +00002//
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 Olesena17768f2010-10-14 23:49:52 +000023class LiveRangeEdit;
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 Olesen532de3d2010-10-22 20:28:21 +000031class raw_ostream;
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000032
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +000033/// SplitAnalysis - Analyze a LiveInterval, looking for live range splitting
34/// opportunities.
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000035class SplitAnalysis {
Jakob Stoklund Olesen08e93b12010-08-10 17:07:22 +000036public:
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000037 const MachineFunction &mf_;
38 const LiveIntervals &lis_;
39 const MachineLoopInfo &loops_;
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +000040 const TargetInstrInfo &tii_;
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000041
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000042 // Instructions using the the current register.
43 typedef SmallPtrSet<const MachineInstr*, 16> InstrPtrSet;
44 InstrPtrSet usingInstrs_;
45
46 // The number of instructions using curli in each basic block.
47 typedef DenseMap<const MachineBasicBlock*, unsigned> BlockCountMap;
48 BlockCountMap usingBlocks_;
49
Jakob Stoklund Olesen2dee7a52010-08-12 23:02:55 +000050 // The number of basic block using curli in each loop.
51 typedef DenseMap<const MachineLoop*, unsigned> LoopCountMap;
52 LoopCountMap usingLoops_;
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000053
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +000054private:
55 // Current live interval.
56 const LiveInterval *curli_;
57
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000058 // Sumarize statistics by counting instructions using curli_.
Jakob Stoklund Olesenabff2802010-07-20 16:12:37 +000059 void analyzeUses();
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000060
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +000061 /// canAnalyzeBranch - Return true if MBB ends in a branch that can be
62 /// analyzed.
63 bool canAnalyzeBranch(const MachineBasicBlock *MBB);
64
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000065public:
Jakob Stoklund Olesenf2c6e362010-07-20 23:50:15 +000066 SplitAnalysis(const MachineFunction &mf, const LiveIntervals &lis,
67 const MachineLoopInfo &mli);
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000068
69 /// analyze - set curli to the specified interval, and analyze how it may be
70 /// split.
71 void analyze(const LiveInterval *li);
72
73 /// clear - clear all data structures so SplitAnalysis is ready to analyze a
74 /// new interval.
75 void clear();
76
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +000077 typedef SmallPtrSet<const MachineBasicBlock*, 16> BlockPtrSet;
Jakob Stoklund Olesen2dee7a52010-08-12 23:02:55 +000078 typedef SmallPtrSet<const MachineLoop*, 16> LoopPtrSet;
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +000079
Jakob Stoklund Olesen532de3d2010-10-22 20:28:21 +000080 // Print a set of blocks with use counts.
81 void print(const BlockPtrSet&, raw_ostream&) const;
82
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +000083 // 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
Jakob Stoklund Olesen532de3d2010-10-22 20:28:21 +000096 // Print loop blocks with use counts.
97 void print(const LoopBlocks&, raw_ostream&) const;
98
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +000099 // Calculate the block sets surrounding the loop.
100 void getLoopBlocks(const MachineLoop *Loop, LoopBlocks &Blocks);
101
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000102 /// LoopPeripheralUse - how is a variable used in and around a loop?
103 /// Peripheral blocks are the loop predecessors and exit blocks.
104 enum LoopPeripheralUse {
105 ContainedInLoop, // All uses are inside the loop.
106 SinglePeripheral, // At most one instruction per peripheral block.
107 MultiPeripheral, // Multiple instructions in some peripheral blocks.
108 OutsideLoop // Uses outside loop periphery.
109 };
110
111 /// analyzeLoopPeripheralUse - Return an enum describing how curli_ is used in
112 /// and around the Loop.
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000113 LoopPeripheralUse analyzeLoopPeripheralUse(const LoopBlocks&);
114
115 /// getCriticalExits - It may be necessary to partially break critical edges
116 /// leaving the loop if an exit block has phi uses of curli. Collect the exit
117 /// blocks that need special treatment into CriticalExits.
118 void getCriticalExits(const LoopBlocks &Blocks, BlockPtrSet &CriticalExits);
119
120 /// canSplitCriticalExits - Return true if it is possible to insert new exit
121 /// blocks before the blocks in CriticalExits.
122 bool canSplitCriticalExits(const LoopBlocks &Blocks,
123 BlockPtrSet &CriticalExits);
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000124
Jakob Stoklund Olesen0960a652010-10-27 00:39:05 +0000125 /// getCriticalPreds - Get the set of loop predecessors with critical edges to
126 /// blocks outside the loop that have curli live in. We don't have to break
127 /// these edges, but they do require special treatment.
128 void getCriticalPreds(const LoopBlocks &Blocks, BlockPtrSet &CriticalPreds);
129
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000130 /// getBestSplitLoop - Return the loop where curli may best be split to a
131 /// separate register, or NULL.
132 const MachineLoop *getBestSplitLoop();
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000133
134 /// getMultiUseBlocks - Add basic blocks to Blocks that may benefit from
135 /// having curli split to a new live interval. Return true if Blocks can be
136 /// passed to SplitEditor::splitSingleBlocks.
137 bool getMultiUseBlocks(BlockPtrSet &Blocks);
Jakob Stoklund Olesenfc412d82010-08-13 21:18:48 +0000138
139 /// getBlockForInsideSplit - If curli is contained inside a single basic block,
140 /// and it wou pay to subdivide the interval inside that block, return it.
141 /// Otherwise return NULL. The returned block can be passed to
142 /// SplitEditor::splitInsideBlock.
143 const MachineBasicBlock *getBlockForInsideSplit();
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000144};
145
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000146
147/// LiveIntervalMap - Map values from a large LiveInterval into a small
148/// interval that is a subset. Insert phi-def values as needed. This class is
149/// used by SplitEditor to create new smaller LiveIntervals.
150///
151/// parentli_ is the larger interval, li_ is the subset interval. Every value
152/// in li_ corresponds to exactly one value in parentli_, and the live range
153/// of the value is contained within the live range of the parentli_ value.
154/// Values in parentli_ may map to any number of openli_ values, including 0.
155class LiveIntervalMap {
156 LiveIntervals &lis_;
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000157
158 // The parent interval is never changed.
159 const LiveInterval &parentli_;
160
161 // The child interval's values are fully contained inside parentli_ values.
Jakob Stoklund Olesen9ca2aeb2010-09-13 23:29:09 +0000162 LiveInterval *li_;
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000163
164 typedef DenseMap<const VNInfo*, VNInfo*> ValueMap;
165
166 // Map parentli_ values to simple values in li_ that are defined at the same
167 // SlotIndex, or NULL for parentli_ values that have complex li_ defs.
168 // Note there is a difference between values mapping to NULL (complex), and
169 // values not present (unknown/unmapped).
170 ValueMap valueMap_;
171
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000172public:
173 LiveIntervalMap(LiveIntervals &lis,
Jakob Stoklund Olesen9ca2aeb2010-09-13 23:29:09 +0000174 const LiveInterval &parentli)
175 : lis_(lis), parentli_(parentli), li_(0) {}
176
177 /// reset - clear all data structures and start a new live interval.
178 void reset(LiveInterval *);
179
180 /// getLI - return the current live interval.
181 LiveInterval *getLI() const { return li_; }
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000182
183 /// defValue - define a value in li_ from the parentli_ value VNI and Idx.
184 /// Idx does not have to be ParentVNI->def, but it must be contained within
185 /// ParentVNI's live range in parentli_.
186 /// Return the new li_ value.
187 VNInfo *defValue(const VNInfo *ParentVNI, SlotIndex Idx);
188
189 /// mapValue - map ParentVNI to the corresponding li_ value at Idx. It is
190 /// assumed that ParentVNI is live at Idx.
191 /// If ParentVNI has not been defined by defValue, it is assumed that
192 /// ParentVNI->def dominates Idx.
193 /// If ParentVNI has been defined by defValue one or more times, a value that
194 /// dominates Idx will be returned. This may require creating extra phi-def
195 /// values and adding live ranges to li_.
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000196 /// If simple is not NULL, *simple will indicate if ParentVNI is a simply
197 /// mapped value.
198 VNInfo *mapValue(const VNInfo *ParentVNI, SlotIndex Idx, bool *simple = 0);
199
Jakob Stoklund Olesenfc60d772010-10-05 20:36:25 +0000200 // extendTo - Find the last li_ value defined in MBB at or before Idx. The
201 // parentli is assumed to be live at Idx. Extend the live range to include
202 // Idx. Return the found VNInfo, or NULL.
Jakob Stoklund Olesenc95c1462010-10-27 00:39:07 +0000203 VNInfo *extendTo(const MachineBasicBlock *MBB, SlotIndex Idx);
Jakob Stoklund Olesenfc60d772010-10-05 20:36:25 +0000204
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000205 /// isMapped - Return true is ParentVNI is a known mapped value. It may be a
206 /// simple 1-1 mapping or a complex mapping to later defs.
207 bool isMapped(const VNInfo *ParentVNI) const {
208 return valueMap_.count(ParentVNI);
209 }
210
211 /// isComplexMapped - Return true if ParentVNI has received new definitions
212 /// with defValue.
213 bool isComplexMapped(const VNInfo *ParentVNI) const;
214
215 // addSimpleRange - Add a simple range from parentli_ to li_.
216 // ParentVNI must be live in the [Start;End) interval.
217 void addSimpleRange(SlotIndex Start, SlotIndex End, const VNInfo *ParentVNI);
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000218
219 /// addRange - Add live ranges to li_ where [Start;End) intersects parentli_.
220 /// All needed values whose def is not inside [Start;End) must be defined
221 /// beforehand so mapValue will work.
222 void addRange(SlotIndex Start, SlotIndex End);
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000223
224 /// defByCopyFrom - Insert a copy from Reg to li, assuming that Reg carries
225 /// ParentVNI. Add a minimal live range for the new value and return it.
226 VNInfo *defByCopyFrom(unsigned Reg,
227 const VNInfo *ParentVNI,
228 MachineBasicBlock &MBB,
229 MachineBasicBlock::iterator I);
230
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000231};
232
233
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000234/// SplitEditor - Edit machine code and LiveIntervals for live range
235/// splitting.
236///
Jakob Stoklund Olesen5eb308b2010-08-06 22:17:33 +0000237/// - Create a SplitEditor from a SplitAnalysis.
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000238/// - Start a new live interval with openIntv.
239/// - Mark the places where the new interval is entered using enterIntv*
240/// - Mark the ranges where the new interval is used with useIntv*
241/// - Mark the places where the interval is exited with exitIntv*.
242/// - Finish the current interval with closeIntv and repeat from 2.
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000243/// - Rewrite instructions with finish().
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000244///
245class SplitEditor {
246 SplitAnalysis &sa_;
247 LiveIntervals &lis_;
248 VirtRegMap &vrm_;
249 MachineRegisterInfo &mri_;
250 const TargetInstrInfo &tii_;
251
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000252 /// edit_ - The current parent register and new intervals created.
253 LiveRangeEdit &edit_;
254
Jakob Stoklund Olesen5eb308b2010-08-06 22:17:33 +0000255 /// dupli_ - Created as a copy of curli_, ranges are carved out as new
256 /// intervals get added through openIntv / closeIntv. This is used to avoid
257 /// editing curli_.
Jakob Stoklund Olesendd9f3fd2010-09-13 23:29:11 +0000258 LiveIntervalMap dupli_;
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000259
260 /// Currently open LiveInterval.
Jakob Stoklund Olesendd9f3fd2010-09-13 23:29:11 +0000261 LiveIntervalMap openli_;
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000262
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000263 /// intervalsLiveAt - Return true if any member of intervals_ is live at Idx.
264 bool intervalsLiveAt(SlotIndex Idx) const;
265
266 /// Values in curli whose live range has been truncated when entering an open
267 /// li.
268 SmallPtrSet<const VNInfo*, 8> truncatedValues;
269
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);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000273
Jakob Stoklund Olesenc95c1462010-10-27 00:39:07 +0000274 /// criticalPreds_ - Set of basic blocks where both dupli and openli should be
275 /// live out because of a critical edge.
276 SplitAnalysis::BlockPtrSet criticalPreds_;
277
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000278 /// computeRemainder - Compute the dupli liveness as the complement of all the
279 /// new intervals.
280 void computeRemainder();
281
282 /// rewrite - Rewrite all uses of reg to use the new registers.
283 void rewrite(unsigned reg);
284
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000285public:
286 /// Create a new SplitEditor for editing the LiveInterval analyzed by SA.
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000287 /// Newly created intervals will be appended to newIntervals.
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000288 SplitEditor(SplitAnalysis &SA, LiveIntervals&, VirtRegMap&, LiveRangeEdit&);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000289
Jakob Stoklund Olesen5eb308b2010-08-06 22:17:33 +0000290 /// getAnalysis - Get the corresponding analysis.
291 SplitAnalysis &getAnalysis() { return sa_; }
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000292
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000293 /// Create a new virtual register and live interval.
294 void openIntv();
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000295
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000296 /// enterIntvBefore - Enter openli before the instruction at Idx. If curli is
297 /// not live before Idx, a COPY is not inserted.
298 void enterIntvBefore(SlotIndex Idx);
299
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000300 /// enterIntvAtEnd - Enter openli at the end of MBB.
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000301 void enterIntvAtEnd(MachineBasicBlock &MBB);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000302
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000303 /// useIntv - indicate that all instructions in MBB should use openli.
304 void useIntv(const MachineBasicBlock &MBB);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000305
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000306 /// useIntv - indicate that all instructions in range should use openli.
307 void useIntv(SlotIndex Start, SlotIndex End);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000308
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000309 /// leaveIntvAfter - Leave openli after the instruction at Idx.
310 void leaveIntvAfter(SlotIndex Idx);
311
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000312 /// leaveIntvAtTop - Leave the interval at the top of MBB.
313 /// Currently, only one value can leave the interval.
314 void leaveIntvAtTop(MachineBasicBlock &MBB);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000315
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000316 /// closeIntv - Indicate that we are done editing the currently open
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000317 /// LiveInterval, and ranges can be trimmed.
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000318 void closeIntv();
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000319
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000320 /// finish - after all the new live ranges have been created, compute the
321 /// remaining live range, and rewrite instructions to use the new registers.
322 void finish();
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000323
324 // ===--- High level methods ---===
325
326 /// splitAroundLoop - Split curli into a separate live interval inside
Jakob Stoklund Olesen57d0f2d2010-10-05 22:19:33 +0000327 /// the loop.
328 void splitAroundLoop(const MachineLoop*);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000329
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000330 /// splitSingleBlocks - Split curli into a separate live interval inside each
Jakob Stoklund Olesen57d0f2d2010-10-05 22:19:33 +0000331 /// basic block in Blocks.
332 void splitSingleBlocks(const SplitAnalysis::BlockPtrSet &Blocks);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000333
Jakob Stoklund Olesen57d0f2d2010-10-05 22:19:33 +0000334 /// splitInsideBlock - Split curli into multiple intervals inside MBB.
335 void splitInsideBlock(const MachineBasicBlock *);
Jakob Stoklund Olesenfc412d82010-08-13 21:18:48 +0000336};
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000337
338}