blob: 9ba7cbeb1001616326a2d7fcaf54dd25c4f7f5dc [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 Olesend68f4582010-10-28 20:34:50 +000025class MachineDominatorTree;
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000026class MachineLoop;
27class MachineLoopInfo;
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +000028class MachineRegisterInfo;
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +000029class TargetInstrInfo;
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +000030class VirtRegMap;
31class VNInfo;
Jakob Stoklund Olesen532de3d2010-10-22 20:28:21 +000032class raw_ostream;
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000033
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +000034/// SplitAnalysis - Analyze a LiveInterval, looking for live range splitting
35/// opportunities.
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000036class SplitAnalysis {
Jakob Stoklund Olesen08e93b12010-08-10 17:07:22 +000037public:
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000038 const MachineFunction &mf_;
39 const LiveIntervals &lis_;
40 const MachineLoopInfo &loops_;
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +000041 const TargetInstrInfo &tii_;
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000042
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000043 // Instructions using the the current register.
44 typedef SmallPtrSet<const MachineInstr*, 16> InstrPtrSet;
45 InstrPtrSet usingInstrs_;
46
47 // The number of instructions using curli in each basic block.
48 typedef DenseMap<const MachineBasicBlock*, unsigned> BlockCountMap;
49 BlockCountMap usingBlocks_;
50
Jakob Stoklund Olesen2dee7a52010-08-12 23:02:55 +000051 // The number of basic block using curli in each loop.
52 typedef DenseMap<const MachineLoop*, unsigned> LoopCountMap;
53 LoopCountMap usingLoops_;
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000054
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +000055private:
56 // Current live interval.
57 const LiveInterval *curli_;
58
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000059 // Sumarize statistics by counting instructions using curli_.
Jakob Stoklund Olesenabff2802010-07-20 16:12:37 +000060 void analyzeUses();
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000061
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +000062 /// canAnalyzeBranch - Return true if MBB ends in a branch that can be
63 /// analyzed.
64 bool canAnalyzeBranch(const MachineBasicBlock *MBB);
65
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000066public:
Jakob Stoklund Olesenf2c6e362010-07-20 23:50:15 +000067 SplitAnalysis(const MachineFunction &mf, const LiveIntervals &lis,
68 const MachineLoopInfo &mli);
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000069
70 /// analyze - set curli to the specified interval, and analyze how it may be
71 /// split.
72 void analyze(const LiveInterval *li);
73
74 /// 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
Jakob Stoklund Olesen532de3d2010-10-22 20:28:21 +000081 // Print a set of blocks with use counts.
82 void print(const BlockPtrSet&, raw_ostream&) const;
83
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +000084 // 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
Jakob Stoklund Olesen532de3d2010-10-22 20:28:21 +000097 // Print loop blocks with use counts.
98 void print(const LoopBlocks&, raw_ostream&) const;
99
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000100 // Calculate the block sets surrounding the loop.
101 void getLoopBlocks(const MachineLoop *Loop, LoopBlocks &Blocks);
102
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000103 /// LoopPeripheralUse - how is a variable used in and around a loop?
104 /// Peripheral blocks are the loop predecessors and exit blocks.
105 enum LoopPeripheralUse {
106 ContainedInLoop, // All uses are inside the loop.
107 SinglePeripheral, // At most one instruction per peripheral block.
108 MultiPeripheral, // Multiple instructions in some peripheral blocks.
109 OutsideLoop // Uses outside loop periphery.
110 };
111
112 /// analyzeLoopPeripheralUse - Return an enum describing how curli_ is used in
113 /// and around the Loop.
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000114 LoopPeripheralUse analyzeLoopPeripheralUse(const LoopBlocks&);
115
116 /// getCriticalExits - It may be necessary to partially break critical edges
117 /// leaving the loop if an exit block has phi uses of curli. Collect the exit
118 /// blocks that need special treatment into CriticalExits.
119 void getCriticalExits(const LoopBlocks &Blocks, BlockPtrSet &CriticalExits);
120
121 /// canSplitCriticalExits - Return true if it is possible to insert new exit
122 /// blocks before the blocks in CriticalExits.
123 bool canSplitCriticalExits(const LoopBlocks &Blocks,
124 BlockPtrSet &CriticalExits);
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000125
Jakob Stoklund Olesen0960a652010-10-27 00:39:05 +0000126 /// getCriticalPreds - Get the set of loop predecessors with critical edges to
127 /// blocks outside the loop that have curli live in. We don't have to break
128 /// these edges, but they do require special treatment.
129 void getCriticalPreds(const LoopBlocks &Blocks, BlockPtrSet &CriticalPreds);
130
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000131 /// getBestSplitLoop - Return the loop where curli may best be split to a
132 /// separate register, or NULL.
133 const MachineLoop *getBestSplitLoop();
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000134
135 /// getMultiUseBlocks - Add basic blocks to Blocks that may benefit from
136 /// having curli split to a new live interval. Return true if Blocks can be
137 /// passed to SplitEditor::splitSingleBlocks.
138 bool getMultiUseBlocks(BlockPtrSet &Blocks);
Jakob Stoklund Olesenfc412d82010-08-13 21:18:48 +0000139
140 /// getBlockForInsideSplit - If curli is contained inside a single basic block,
141 /// and it wou pay to subdivide the interval inside that block, return it.
142 /// Otherwise return NULL. The returned block can be passed to
143 /// SplitEditor::splitInsideBlock.
144 const MachineBasicBlock *getBlockForInsideSplit();
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000145};
146
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000147
148/// LiveIntervalMap - Map values from a large LiveInterval into a small
149/// interval that is a subset. Insert phi-def values as needed. This class is
150/// used by SplitEditor to create new smaller LiveIntervals.
151///
152/// parentli_ is the larger interval, li_ is the subset interval. Every value
153/// in li_ corresponds to exactly one value in parentli_, and the live range
154/// of the value is contained within the live range of the parentli_ value.
155/// Values in parentli_ may map to any number of openli_ values, including 0.
156class LiveIntervalMap {
157 LiveIntervals &lis_;
Jakob Stoklund Olesend68f4582010-10-28 20:34:50 +0000158 MachineDominatorTree &mdt_;
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000159
160 // The parent interval is never changed.
161 const LiveInterval &parentli_;
162
163 // The child interval's values are fully contained inside parentli_ values.
Jakob Stoklund Olesen9ca2aeb2010-09-13 23:29:09 +0000164 LiveInterval *li_;
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000165
166 typedef DenseMap<const VNInfo*, VNInfo*> ValueMap;
167
168 // Map parentli_ values to simple values in li_ that are defined at the same
169 // SlotIndex, or NULL for parentli_ values that have complex li_ defs.
170 // Note there is a difference between values mapping to NULL (complex), and
171 // values not present (unknown/unmapped).
172 ValueMap valueMap_;
173
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000174public:
175 LiveIntervalMap(LiveIntervals &lis,
Jakob Stoklund Olesend68f4582010-10-28 20:34:50 +0000176 MachineDominatorTree &mdt,
Jakob Stoklund Olesen9ca2aeb2010-09-13 23:29:09 +0000177 const LiveInterval &parentli)
Jakob Stoklund Olesend68f4582010-10-28 20:34:50 +0000178 : lis_(lis), mdt_(mdt), parentli_(parentli), li_(0) {}
Jakob Stoklund Olesen9ca2aeb2010-09-13 23:29:09 +0000179
180 /// reset - clear all data structures and start a new live interval.
181 void reset(LiveInterval *);
182
183 /// getLI - return the current live interval.
184 LiveInterval *getLI() const { return li_; }
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000185
186 /// defValue - define a value in li_ from the parentli_ value VNI and Idx.
187 /// Idx does not have to be ParentVNI->def, but it must be contained within
188 /// ParentVNI's live range in parentli_.
189 /// Return the new li_ value.
190 VNInfo *defValue(const VNInfo *ParentVNI, SlotIndex Idx);
191
192 /// mapValue - map ParentVNI to the corresponding li_ value at Idx. It is
193 /// assumed that ParentVNI is live at Idx.
194 /// If ParentVNI has not been defined by defValue, it is assumed that
195 /// ParentVNI->def dominates Idx.
196 /// If ParentVNI has been defined by defValue one or more times, a value that
197 /// dominates Idx will be returned. This may require creating extra phi-def
198 /// values and adding live ranges to li_.
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000199 /// If simple is not NULL, *simple will indicate if ParentVNI is a simply
200 /// mapped value.
201 VNInfo *mapValue(const VNInfo *ParentVNI, SlotIndex Idx, bool *simple = 0);
202
Jakob Stoklund Olesenfc60d772010-10-05 20:36:25 +0000203 // extendTo - Find the last li_ value defined in MBB at or before Idx. The
204 // parentli is assumed to be live at Idx. Extend the live range to include
205 // Idx. Return the found VNInfo, or NULL.
Jakob Stoklund Olesenc95c1462010-10-27 00:39:07 +0000206 VNInfo *extendTo(const MachineBasicBlock *MBB, SlotIndex Idx);
Jakob Stoklund Olesenfc60d772010-10-05 20:36:25 +0000207
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000208 /// isMapped - Return true is ParentVNI is a known mapped value. It may be a
209 /// simple 1-1 mapping or a complex mapping to later defs.
210 bool isMapped(const VNInfo *ParentVNI) const {
211 return valueMap_.count(ParentVNI);
212 }
213
214 /// isComplexMapped - Return true if ParentVNI has received new definitions
215 /// with defValue.
216 bool isComplexMapped(const VNInfo *ParentVNI) const;
217
218 // addSimpleRange - Add a simple range from parentli_ to li_.
219 // ParentVNI must be live in the [Start;End) interval.
220 void addSimpleRange(SlotIndex Start, SlotIndex End, const VNInfo *ParentVNI);
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000221
222 /// addRange - Add live ranges to li_ where [Start;End) intersects parentli_.
223 /// All needed values whose def is not inside [Start;End) must be defined
224 /// beforehand so mapValue will work.
225 void addRange(SlotIndex Start, SlotIndex End);
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000226
227 /// defByCopyFrom - Insert a copy from Reg to li, assuming that Reg carries
228 /// ParentVNI. Add a minimal live range for the new value and return it.
229 VNInfo *defByCopyFrom(unsigned Reg,
230 const VNInfo *ParentVNI,
231 MachineBasicBlock &MBB,
232 MachineBasicBlock::iterator I);
233
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000234};
235
236
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000237/// SplitEditor - Edit machine code and LiveIntervals for live range
238/// splitting.
239///
Jakob Stoklund Olesen5eb308b2010-08-06 22:17:33 +0000240/// - Create a SplitEditor from a SplitAnalysis.
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000241/// - Start a new live interval with openIntv.
242/// - Mark the places where the new interval is entered using enterIntv*
243/// - Mark the ranges where the new interval is used with useIntv*
244/// - Mark the places where the interval is exited with exitIntv*.
245/// - Finish the current interval with closeIntv and repeat from 2.
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000246/// - Rewrite instructions with finish().
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000247///
248class SplitEditor {
249 SplitAnalysis &sa_;
250 LiveIntervals &lis_;
251 VirtRegMap &vrm_;
252 MachineRegisterInfo &mri_;
253 const TargetInstrInfo &tii_;
254
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000255 /// edit_ - The current parent register and new intervals created.
256 LiveRangeEdit &edit_;
257
Jakob Stoklund Olesen5eb308b2010-08-06 22:17:33 +0000258 /// dupli_ - Created as a copy of curli_, ranges are carved out as new
259 /// intervals get added through openIntv / closeIntv. This is used to avoid
260 /// editing curli_.
Jakob Stoklund Olesendd9f3fd2010-09-13 23:29:11 +0000261 LiveIntervalMap dupli_;
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000262
263 /// Currently open LiveInterval.
Jakob Stoklund Olesendd9f3fd2010-09-13 23:29:11 +0000264 LiveIntervalMap openli_;
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000265
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000266 /// intervalsLiveAt - Return true if any member of intervals_ is live at Idx.
267 bool intervalsLiveAt(SlotIndex Idx) const;
268
269 /// Values in curli whose live range has been truncated when entering an open
270 /// li.
271 SmallPtrSet<const VNInfo*, 8> truncatedValues;
272
273 /// addTruncSimpleRange - Add the given simple range to dupli_ after
274 /// truncating any overlap with intervals_.
275 void addTruncSimpleRange(SlotIndex Start, SlotIndex End, VNInfo *VNI);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000276
Jakob Stoklund Olesenc95c1462010-10-27 00:39:07 +0000277 /// criticalPreds_ - Set of basic blocks where both dupli and openli should be
278 /// live out because of a critical edge.
279 SplitAnalysis::BlockPtrSet criticalPreds_;
280
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000281 /// computeRemainder - Compute the dupli liveness as the complement of all the
282 /// new intervals.
283 void computeRemainder();
284
285 /// rewrite - Rewrite all uses of reg to use the new registers.
286 void rewrite(unsigned reg);
287
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000288public:
289 /// Create a new SplitEditor for editing the LiveInterval analyzed by SA.
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000290 /// Newly created intervals will be appended to newIntervals.
Jakob Stoklund Olesend68f4582010-10-28 20:34:50 +0000291 SplitEditor(SplitAnalysis &SA, LiveIntervals&, VirtRegMap&,
292 MachineDominatorTree&, LiveRangeEdit&);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000293
Jakob Stoklund Olesen5eb308b2010-08-06 22:17:33 +0000294 /// getAnalysis - Get the corresponding analysis.
295 SplitAnalysis &getAnalysis() { return sa_; }
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000296
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000297 /// Create a new virtual register and live interval.
298 void openIntv();
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000299
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000300 /// enterIntvBefore - Enter openli before the instruction at Idx. If curli is
301 /// not live before Idx, a COPY is not inserted.
302 void enterIntvBefore(SlotIndex Idx);
303
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000304 /// enterIntvAtEnd - Enter openli at the end of MBB.
Jakob Stoklund Olesenf6a129a2010-09-16 00:01:36 +0000305 void enterIntvAtEnd(MachineBasicBlock &MBB);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000306
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000307 /// useIntv - indicate that all instructions in MBB should use openli.
308 void useIntv(const MachineBasicBlock &MBB);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000309
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000310 /// useIntv - indicate that all instructions in range should use openli.
311 void useIntv(SlotIndex Start, SlotIndex End);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000312
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000313 /// leaveIntvAfter - Leave openli after the instruction at Idx.
314 void leaveIntvAfter(SlotIndex Idx);
315
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000316 /// leaveIntvAtTop - Leave the interval at the top of MBB.
317 /// Currently, only one value can leave the interval.
318 void leaveIntvAtTop(MachineBasicBlock &MBB);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000319
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000320 /// closeIntv - Indicate that we are done editing the currently open
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000321 /// LiveInterval, and ranges can be trimmed.
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000322 void closeIntv();
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000323
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000324 /// finish - after all the new live ranges have been created, compute the
325 /// remaining live range, and rewrite instructions to use the new registers.
326 void finish();
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000327
328 // ===--- High level methods ---===
329
330 /// splitAroundLoop - Split curli into a separate live interval inside
Jakob Stoklund Olesen57d0f2d2010-10-05 22:19:33 +0000331 /// the loop.
332 void splitAroundLoop(const MachineLoop*);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000333
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000334 /// splitSingleBlocks - Split curli into a separate live interval inside each
Jakob Stoklund Olesen57d0f2d2010-10-05 22:19:33 +0000335 /// basic block in Blocks.
336 void splitSingleBlocks(const SplitAnalysis::BlockPtrSet &Blocks);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000337
Jakob Stoklund Olesen57d0f2d2010-10-05 22:19:33 +0000338 /// splitInsideBlock - Split curli into multiple intervals inside MBB.
339 void splitInsideBlock(const MachineBasicBlock *);
Jakob Stoklund Olesenfc412d82010-08-13 21:18:48 +0000340};
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000341
342}