blob: 005ceda3c2a00a67f959b59a7e39053ac7fbe6c7 [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 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 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 Olesenf0179002010-07-26 23:44:11 +000072 const LiveInterval *getCurLI() { return curli_; }
73
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000074 /// clear - clear all data structures so SplitAnalysis is ready to analyze a
75 /// new interval.
76 void clear();
77
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +000078 typedef SmallPtrSet<const MachineBasicBlock*, 16> BlockPtrSet;
Jakob Stoklund Olesen2dee7a52010-08-12 23:02:55 +000079 typedef SmallPtrSet<const MachineLoop*, 16> LoopPtrSet;
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +000080
81 // Sets of basic blocks surrounding a machine loop.
82 struct LoopBlocks {
83 BlockPtrSet Loop; // Blocks in the loop.
84 BlockPtrSet Preds; // Loop predecessor blocks.
85 BlockPtrSet Exits; // Loop exit blocks.
86
87 void clear() {
88 Loop.clear();
89 Preds.clear();
90 Exits.clear();
91 }
92 };
93
94 // Calculate the block sets surrounding the loop.
95 void getLoopBlocks(const MachineLoop *Loop, LoopBlocks &Blocks);
96
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000097 /// LoopPeripheralUse - how is a variable used in and around a loop?
98 /// Peripheral blocks are the loop predecessors and exit blocks.
99 enum LoopPeripheralUse {
100 ContainedInLoop, // All uses are inside the loop.
101 SinglePeripheral, // At most one instruction per peripheral block.
102 MultiPeripheral, // Multiple instructions in some peripheral blocks.
103 OutsideLoop // Uses outside loop periphery.
104 };
105
106 /// analyzeLoopPeripheralUse - Return an enum describing how curli_ is used in
107 /// and around the Loop.
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000108 LoopPeripheralUse analyzeLoopPeripheralUse(const LoopBlocks&);
109
110 /// getCriticalExits - It may be necessary to partially break critical edges
111 /// leaving the loop if an exit block has phi uses of curli. Collect the exit
112 /// blocks that need special treatment into CriticalExits.
113 void getCriticalExits(const LoopBlocks &Blocks, BlockPtrSet &CriticalExits);
114
115 /// canSplitCriticalExits - Return true if it is possible to insert new exit
116 /// blocks before the blocks in CriticalExits.
117 bool canSplitCriticalExits(const LoopBlocks &Blocks,
118 BlockPtrSet &CriticalExits);
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000119
120 /// getBestSplitLoop - Return the loop where curli may best be split to a
121 /// separate register, or NULL.
122 const MachineLoop *getBestSplitLoop();
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000123
124 /// getMultiUseBlocks - Add basic blocks to Blocks that may benefit from
125 /// having curli split to a new live interval. Return true if Blocks can be
126 /// passed to SplitEditor::splitSingleBlocks.
127 bool getMultiUseBlocks(BlockPtrSet &Blocks);
Jakob Stoklund Olesenfc412d82010-08-13 21:18:48 +0000128
129 /// getBlockForInsideSplit - If curli is contained inside a single basic block,
130 /// and it wou pay to subdivide the interval inside that block, return it.
131 /// Otherwise return NULL. The returned block can be passed to
132 /// SplitEditor::splitInsideBlock.
133 const MachineBasicBlock *getBlockForInsideSplit();
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000134};
135
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000136
137/// LiveIntervalMap - Map values from a large LiveInterval into a small
138/// interval that is a subset. Insert phi-def values as needed. This class is
139/// used by SplitEditor to create new smaller LiveIntervals.
140///
141/// parentli_ is the larger interval, li_ is the subset interval. Every value
142/// in li_ corresponds to exactly one value in parentli_, and the live range
143/// of the value is contained within the live range of the parentli_ value.
144/// Values in parentli_ may map to any number of openli_ values, including 0.
145class LiveIntervalMap {
146 LiveIntervals &lis_;
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000147
148 // The parent interval is never changed.
149 const LiveInterval &parentli_;
150
151 // The child interval's values are fully contained inside parentli_ values.
Jakob Stoklund Olesen9ca2aeb2010-09-13 23:29:09 +0000152 LiveInterval *li_;
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000153
154 typedef DenseMap<const VNInfo*, VNInfo*> ValueMap;
155
156 // Map parentli_ values to simple values in li_ that are defined at the same
157 // SlotIndex, or NULL for parentli_ values that have complex li_ defs.
158 // Note there is a difference between values mapping to NULL (complex), and
159 // values not present (unknown/unmapped).
160 ValueMap valueMap_;
161
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000162public:
163 LiveIntervalMap(LiveIntervals &lis,
Jakob Stoklund Olesen9ca2aeb2010-09-13 23:29:09 +0000164 const LiveInterval &parentli)
165 : lis_(lis), parentli_(parentli), li_(0) {}
166
167 /// reset - clear all data structures and start a new live interval.
168 void reset(LiveInterval *);
169
170 /// getLI - return the current live interval.
171 LiveInterval *getLI() const { return li_; }
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000172
173 /// defValue - define a value in li_ from the parentli_ value VNI and Idx.
174 /// Idx does not have to be ParentVNI->def, but it must be contained within
175 /// ParentVNI's live range in parentli_.
176 /// Return the new li_ value.
177 VNInfo *defValue(const VNInfo *ParentVNI, SlotIndex Idx);
178
179 /// mapValue - map ParentVNI to the corresponding li_ value at Idx. It is
180 /// assumed that ParentVNI is live at Idx.
181 /// If ParentVNI has not been defined by defValue, it is assumed that
182 /// ParentVNI->def dominates Idx.
183 /// If ParentVNI has been defined by defValue one or more times, a value that
184 /// dominates Idx will be returned. This may require creating extra phi-def
185 /// values and adding live ranges to li_.
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000186 /// If simple is not NULL, *simple will indicate if ParentVNI is a simply
187 /// mapped value.
188 VNInfo *mapValue(const VNInfo *ParentVNI, SlotIndex Idx, bool *simple = 0);
189
Jakob Stoklund Olesenfc60d772010-10-05 20:36:25 +0000190 // extendTo - Find the last li_ value defined in MBB at or before Idx. The
191 // parentli is assumed to be live at Idx. Extend the live range to include
192 // Idx. Return the found VNInfo, or NULL.
193 VNInfo *extendTo(MachineBasicBlock *MBB, SlotIndex Idx);
194
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000195 /// isMapped - Return true is ParentVNI is a known mapped value. It may be a
196 /// simple 1-1 mapping or a complex mapping to later defs.
197 bool isMapped(const VNInfo *ParentVNI) const {
198 return valueMap_.count(ParentVNI);
199 }
200
201 /// isComplexMapped - Return true if ParentVNI has received new definitions
202 /// with defValue.
203 bool isComplexMapped(const VNInfo *ParentVNI) const;
204
205 // addSimpleRange - Add a simple range from parentli_ to li_.
206 // ParentVNI must be live in the [Start;End) interval.
207 void addSimpleRange(SlotIndex Start, SlotIndex End, const VNInfo *ParentVNI);
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000208
209 /// addRange - Add live ranges to li_ where [Start;End) intersects parentli_.
210 /// All needed values whose def is not inside [Start;End) must be defined
211 /// beforehand so mapValue will work.
212 void addRange(SlotIndex Start, SlotIndex End);
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000213
214 /// defByCopyFrom - Insert a copy from Reg to li, assuming that Reg carries
215 /// ParentVNI. Add a minimal live range for the new value and return it.
216 VNInfo *defByCopyFrom(unsigned Reg,
217 const VNInfo *ParentVNI,
218 MachineBasicBlock &MBB,
219 MachineBasicBlock::iterator I);
220
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000221};
222
223
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000224/// SplitEditor - Edit machine code and LiveIntervals for live range
225/// splitting.
226///
Jakob Stoklund Olesen5eb308b2010-08-06 22:17:33 +0000227/// - Create a SplitEditor from a SplitAnalysis.
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000228/// - Start a new live interval with openIntv.
229/// - Mark the places where the new interval is entered using enterIntv*
230/// - Mark the ranges where the new interval is used with useIntv*
231/// - Mark the places where the interval is exited with exitIntv*.
232/// - Finish the current interval with closeIntv and repeat from 2.
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000233/// - Rewrite instructions with finish().
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000234///
235class SplitEditor {
236 SplitAnalysis &sa_;
237 LiveIntervals &lis_;
238 VirtRegMap &vrm_;
239 MachineRegisterInfo &mri_;
240 const TargetInstrInfo &tii_;
241
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000242 /// edit_ - The current parent register and new intervals created.
243 LiveRangeEdit &edit_;
244
Jakob Stoklund Olesen5eb308b2010-08-06 22:17:33 +0000245 /// curli_ - The immutable interval we are currently splitting.
246 const LiveInterval *const curli_;
247
248 /// dupli_ - Created as a copy of curli_, ranges are carved out as new
249 /// intervals get added through openIntv / closeIntv. This is used to avoid
250 /// editing curli_.
Jakob Stoklund Olesendd9f3fd2010-09-13 23:29:11 +0000251 LiveIntervalMap dupli_;
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000252
253 /// Currently open LiveInterval.
Jakob Stoklund Olesendd9f3fd2010-09-13 23:29:11 +0000254 LiveIntervalMap openli_;
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000255
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000256 /// intervalsLiveAt - Return true if any member of intervals_ is live at Idx.
257 bool intervalsLiveAt(SlotIndex Idx) const;
258
259 /// Values in curli whose live range has been truncated when entering an open
260 /// li.
261 SmallPtrSet<const VNInfo*, 8> truncatedValues;
262
263 /// addTruncSimpleRange - Add the given simple range to dupli_ after
264 /// truncating any overlap with intervals_.
265 void addTruncSimpleRange(SlotIndex Start, SlotIndex End, VNInfo *VNI);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000266
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000267 /// computeRemainder - Compute the dupli liveness as the complement of all the
268 /// new intervals.
269 void computeRemainder();
270
271 /// rewrite - Rewrite all uses of reg to use the new registers.
272 void rewrite(unsigned reg);
273
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000274public:
275 /// Create a new SplitEditor for editing the LiveInterval analyzed by SA.
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000276 /// Newly created intervals will be appended to newIntervals.
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000277 SplitEditor(SplitAnalysis &SA, LiveIntervals&, VirtRegMap&, LiveRangeEdit&);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000278
Jakob Stoklund Olesen5eb308b2010-08-06 22:17:33 +0000279 /// getAnalysis - Get the corresponding analysis.
280 SplitAnalysis &getAnalysis() { return sa_; }
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000281
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000282 /// Create a new virtual register and live interval.
283 void openIntv();
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000284
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000285 /// enterIntvBefore - Enter openli before the instruction at Idx. If curli is
286 /// not live before Idx, a COPY is not inserted.
287 void enterIntvBefore(SlotIndex Idx);
288
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000289 /// enterIntvAtEnd - Enter openli at the end of MBB.
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000290 void enterIntvAtEnd(MachineBasicBlock &MBB);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000291
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000292 /// useIntv - indicate that all instructions in MBB should use openli.
293 void useIntv(const MachineBasicBlock &MBB);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000294
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000295 /// useIntv - indicate that all instructions in range should use openli.
296 void useIntv(SlotIndex Start, SlotIndex End);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000297
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000298 /// leaveIntvAfter - Leave openli after the instruction at Idx.
299 void leaveIntvAfter(SlotIndex Idx);
300
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000301 /// leaveIntvAtTop - Leave the interval at the top of MBB.
302 /// Currently, only one value can leave the interval.
303 void leaveIntvAtTop(MachineBasicBlock &MBB);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000304
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000305 /// closeIntv - Indicate that we are done editing the currently open
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000306 /// LiveInterval, and ranges can be trimmed.
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000307 void closeIntv();
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000308
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000309 /// finish - after all the new live ranges have been created, compute the
310 /// remaining live range, and rewrite instructions to use the new registers.
311 void finish();
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000312
313 // ===--- High level methods ---===
314
315 /// splitAroundLoop - Split curli into a separate live interval inside
Jakob Stoklund Olesen57d0f2d2010-10-05 22:19:33 +0000316 /// the loop.
317 void splitAroundLoop(const MachineLoop*);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000318
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000319 /// splitSingleBlocks - Split curli into a separate live interval inside each
Jakob Stoklund Olesen57d0f2d2010-10-05 22:19:33 +0000320 /// basic block in Blocks.
321 void splitSingleBlocks(const SplitAnalysis::BlockPtrSet &Blocks);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000322
Jakob Stoklund Olesen57d0f2d2010-10-05 22:19:33 +0000323 /// splitInsideBlock - Split curli into multiple intervals inside MBB.
324 void splitInsideBlock(const MachineBasicBlock *);
Jakob Stoklund Olesenfc412d82010-08-13 21:18:48 +0000325};
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000326
327}