blob: 4005a3d5cbbfbd1b4c76d2752e60861e9da127c0 [file] [log] [blame]
Jakob Stoklund Olesen8dd070e2011-01-04 21:10:05 +00001//===-------- SplitKit.h - 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
Sebastian Redl6796e4f2011-04-24 15:46:51 +000015#ifndef LLVM_CODEGEN_SPLITKIT_H
16#define LLVM_CODEGEN_SPLITKIT_H
17
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +000018#include "LiveRangeCalc.h"
Jakob Stoklund Olesendb529a82011-04-06 03:57:00 +000019#include "llvm/ADT/ArrayRef.h"
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000020#include "llvm/ADT/DenseMap.h"
Eric Christopher0f438112011-02-03 06:18:29 +000021#include "llvm/ADT/IntervalMap.h"
Jakob Stoklund Olesen8d0963f2010-12-21 01:50:21 +000022#include "llvm/ADT/SmallPtrSet.h"
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000023
24namespace llvm {
25
Eric Christopher0f438112011-02-03 06:18:29 +000026class ConnectedVNInfoEqClasses;
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000027class LiveInterval;
28class LiveIntervals;
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +000029class LiveRangeEdit;
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000030class MachineInstr;
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000031class MachineLoopInfo;
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +000032class MachineRegisterInfo;
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +000033class TargetInstrInfo;
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +000034class TargetRegisterInfo;
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +000035class VirtRegMap;
36class VNInfo;
Jakob Stoklund Olesen532de3d2010-10-22 20:28:21 +000037class raw_ostream;
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000038
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +000039/// SplitAnalysis - Analyze a LiveInterval, looking for live range splitting
40/// opportunities.
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000041class SplitAnalysis {
Jakob Stoklund Olesen08e93b12010-08-10 17:07:22 +000042public:
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +000043 const MachineFunction &MF;
Jakob Stoklund Olesen1b847de2011-02-19 00:53:42 +000044 const VirtRegMap &VRM;
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +000045 const LiveIntervals &LIS;
46 const MachineLoopInfo &Loops;
47 const TargetInstrInfo &TII;
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000048
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +000049 /// Additional information about basic blocks where the current variable is
50 /// live. Such a block will look like one of these templates:
51 ///
52 /// 1. | o---x | Internal to block. Variable is only live in this block.
53 /// 2. |---x | Live-in, kill.
54 /// 3. | o---| Def, live-out.
Jakob Stoklund Olesena2e79ef2011-05-30 01:33:26 +000055 /// 4. |---x o---| Live-in, kill, def, live-out. Counted by NumGapBlocks.
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +000056 /// 5. |---o---o---| Live-through with uses or defs.
Jakob Stoklund Olesena2e79ef2011-05-30 01:33:26 +000057 /// 6. |-----------| Live-through without uses. Counted by NumThroughBlocks.
58 ///
59 /// Two BlockInfo entries are created for template 4. One for the live-in
60 /// segment, and one for the live-out segment. These entries look as if the
61 /// block were split in the middle where the live range isn't live.
62 ///
63 /// Live-through blocks without any uses don't get BlockInfo entries. They
64 /// are simply listed in ThroughBlocks instead.
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +000065 ///
66 struct BlockInfo {
67 MachineBasicBlock *MBB;
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +000068 SlotIndex FirstInstr; ///< First instr accessing current reg.
69 SlotIndex LastInstr; ///< Last instr accessing current reg.
Jakob Stoklund Olesen77ee1142011-08-02 22:37:22 +000070 SlotIndex FirstDef; ///< First non-phi valno->def, or SlotIndex().
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +000071 bool LiveIn; ///< Current reg is live in.
72 bool LiveOut; ///< Current reg is live out.
Jakob Stoklund Olesen87360f72011-06-30 01:30:39 +000073
74 /// isOneInstr - Returns true when this BlockInfo describes a single
75 /// instruction.
76 bool isOneInstr() const {
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +000077 return SlotIndex::isSameInstr(FirstInstr, LastInstr);
Jakob Stoklund Olesen87360f72011-06-30 01:30:39 +000078 }
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +000079 };
80
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +000081private:
82 // Current live interval.
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +000083 const LiveInterval *CurLI;
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +000084
Jakob Stoklund Olesenb20b5182012-01-12 17:53:44 +000085 // Sorted slot indexes of using instructions.
86 SmallVector<SlotIndex, 8> UseSlots;
87
Jakob Stoklund Olesen1a774452011-04-05 04:20:27 +000088 /// LastSplitPoint - Last legal split point in each basic block in the current
89 /// function. The first entry is the first terminator, the second entry is the
90 /// last valid split point for a variable that is live in to a landing pad
91 /// successor.
92 SmallVector<std::pair<SlotIndex, SlotIndex>, 8> LastSplitPoint;
93
Jakob Stoklund Olesendb529a82011-04-06 03:57:00 +000094 /// UseBlocks - Blocks where CurLI has uses.
95 SmallVector<BlockInfo, 8> UseBlocks;
96
Jakob Stoklund Olesena2e79ef2011-05-30 01:33:26 +000097 /// NumGapBlocks - Number of duplicate entries in UseBlocks for blocks where
98 /// the live range has a gap.
99 unsigned NumGapBlocks;
100
Jakob Stoklund Olesendb529a82011-04-06 03:57:00 +0000101 /// ThroughBlocks - Block numbers where CurLI is live through without uses.
Jakob Stoklund Olesenf4afdfc2011-04-09 02:59:09 +0000102 BitVector ThroughBlocks;
103
104 /// NumThroughBlocks - Number of live-through blocks.
105 unsigned NumThroughBlocks;
Jakob Stoklund Olesendb529a82011-04-06 03:57:00 +0000106
Jakob Stoklund Olesen7d6b6a02011-05-03 20:42:13 +0000107 /// DidRepairRange - analyze was forced to shrinkToUses().
108 bool DidRepairRange;
109
Jakob Stoklund Olesen1a774452011-04-05 04:20:27 +0000110 SlotIndex computeLastSplitPoint(unsigned Num);
111
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000112 // Sumarize statistics by counting instructions using CurLI.
Jakob Stoklund Olesenabff2802010-07-20 16:12:37 +0000113 void analyzeUses();
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000114
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000115 /// calcLiveBlockInfo - Compute per-block information about CurLI.
Jakob Stoklund Olesen2b0f9e72011-03-05 18:33:49 +0000116 bool calcLiveBlockInfo();
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000117
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000118public:
Jakob Stoklund Olesen1b847de2011-02-19 00:53:42 +0000119 SplitAnalysis(const VirtRegMap &vrm, const LiveIntervals &lis,
Jakob Stoklund Olesenf2c6e362010-07-20 23:50:15 +0000120 const MachineLoopInfo &mli);
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000121
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000122 /// analyze - set CurLI to the specified interval, and analyze how it may be
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000123 /// split.
124 void analyze(const LiveInterval *li);
125
Jakob Stoklund Olesen7d6b6a02011-05-03 20:42:13 +0000126 /// didRepairRange() - Returns true if CurLI was invalid and has been repaired
127 /// by analyze(). This really shouldn't happen, but sometimes the coalescer
128 /// can create live ranges that end in mid-air.
129 bool didRepairRange() const { return DidRepairRange; }
130
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000131 /// clear - clear all data structures so SplitAnalysis is ready to analyze a
132 /// new interval.
133 void clear();
134
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +0000135 /// getParent - Return the last analyzed interval.
136 const LiveInterval &getParent() const { return *CurLI; }
137
Jakob Stoklund Olesen74c4f972012-01-11 02:07:00 +0000138 /// getLastSplitPoint - Return the base index of the last valid split point
Jakob Stoklund Olesen1a774452011-04-05 04:20:27 +0000139 /// in the basic block numbered Num.
140 SlotIndex getLastSplitPoint(unsigned Num) {
141 // Inline the common simple case.
142 if (LastSplitPoint[Num].first.isValid() &&
143 !LastSplitPoint[Num].second.isValid())
144 return LastSplitPoint[Num].first;
145 return computeLastSplitPoint(Num);
146 }
147
Jakob Stoklund Olesen74c4f972012-01-11 02:07:00 +0000148 /// getLastSplitPointIter - Returns the last split point as an iterator.
149 MachineBasicBlock::iterator getLastSplitPointIter(MachineBasicBlock*);
150
Jakob Stoklund Olesen06c0f252011-02-21 23:09:46 +0000151 /// isOriginalEndpoint - Return true if the original live range was killed or
152 /// (re-)defined at Idx. Idx should be the 'def' slot for a normal kill/def,
153 /// and 'use' for an early-clobber def.
154 /// This can be used to recognize code inserted by earlier live range
155 /// splitting.
156 bool isOriginalEndpoint(SlotIndex Idx) const;
157
Jakob Stoklund Olesenb20b5182012-01-12 17:53:44 +0000158 /// getUseSlots - Return an array of SlotIndexes of instructions using CurLI.
159 /// This include both use and def operands, at most one entry per instruction.
160 ArrayRef<SlotIndex> getUseSlots() const { return UseSlots; }
161
Jakob Stoklund Olesendb529a82011-04-06 03:57:00 +0000162 /// getUseBlocks - Return an array of BlockInfo objects for the basic blocks
163 /// where CurLI has uses.
Jakob Stoklund Olesenb2abfa02011-05-28 02:32:57 +0000164 ArrayRef<BlockInfo> getUseBlocks() const { return UseBlocks; }
Jakob Stoklund Olesendb529a82011-04-06 03:57:00 +0000165
Jakob Stoklund Olesenf4afdfc2011-04-09 02:59:09 +0000166 /// getNumThroughBlocks - Return the number of through blocks.
167 unsigned getNumThroughBlocks() const { return NumThroughBlocks; }
168
169 /// isThroughBlock - Return true if CurLI is live through MBB without uses.
170 bool isThroughBlock(unsigned MBB) const { return ThroughBlocks.test(MBB); }
Jakob Stoklund Olesendb529a82011-04-06 03:57:00 +0000171
Jakob Stoklund Olesen5db42892011-04-12 21:30:53 +0000172 /// getThroughBlocks - Return the set of through blocks.
173 const BitVector &getThroughBlocks() const { return ThroughBlocks; }
174
Jakob Stoklund Olesenb2abfa02011-05-28 02:32:57 +0000175 /// getNumLiveBlocks - Return the number of blocks where CurLI is live.
176 unsigned getNumLiveBlocks() const {
Jakob Stoklund Olesena2e79ef2011-05-30 01:33:26 +0000177 return getUseBlocks().size() - NumGapBlocks + getNumThroughBlocks();
Jakob Stoklund Olesenb2abfa02011-05-28 02:32:57 +0000178 }
179
180 /// countLiveBlocks - Return the number of blocks where li is live. This is
181 /// guaranteed to return the same number as getNumLiveBlocks() after calling
182 /// analyze(li).
Jakob Stoklund Olesen9f4b8932011-04-26 22:33:12 +0000183 unsigned countLiveBlocks(const LiveInterval *li) const;
184
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000185 typedef SmallPtrSet<const MachineBasicBlock*, 16> BlockPtrSet;
186
Jakob Stoklund Olesen2d6d86b2011-08-05 22:20:45 +0000187 /// shouldSplitSingleBlock - Returns true if it would help to create a local
188 /// live range for the instructions in BI. There is normally no benefit to
189 /// creating a live range for a single instruction, but it does enable
190 /// register class inflation if the instruction has a restricted register
191 /// class.
192 ///
193 /// @param BI The block to be isolated.
194 /// @param SingleInstrs True when single instructions should be isolated.
195 bool shouldSplitSingleBlock(const BlockInfo &BI, bool SingleInstrs) const;
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000196};
197
Jakob Stoklund Olesen1407c842010-08-18 19:00:08 +0000198
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000199/// SplitEditor - Edit machine code and LiveIntervals for live range
200/// splitting.
201///
Jakob Stoklund Olesen5eb308b2010-08-06 22:17:33 +0000202/// - Create a SplitEditor from a SplitAnalysis.
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000203/// - Start a new live interval with openIntv.
204/// - Mark the places where the new interval is entered using enterIntv*
205/// - Mark the ranges where the new interval is used with useIntv*
206/// - Mark the places where the interval is exited with exitIntv*.
207/// - Finish the current interval with closeIntv and repeat from 2.
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000208/// - Rewrite instructions with finish().
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000209///
210class SplitEditor {
Jakob Stoklund Olesen0eeca442011-02-19 00:42:33 +0000211 SplitAnalysis &SA;
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000212 LiveIntervals &LIS;
213 VirtRegMap &VRM;
214 MachineRegisterInfo &MRI;
Eric Christopher0f438112011-02-03 06:18:29 +0000215 MachineDominatorTree &MDT;
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000216 const TargetInstrInfo &TII;
217 const TargetRegisterInfo &TRI;
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000218
Jakob Stoklund Olesen708d06f2011-09-12 16:49:21 +0000219public:
220
221 /// ComplementSpillMode - Select how the complement live range should be
222 /// created. SplitEditor automatically creates interval 0 to contain
223 /// anything that isn't added to another interval. This complement interval
224 /// can get quite complicated, and it can sometimes be an advantage to allow
225 /// it to overlap the other intervals. If it is going to spill anyway, no
226 /// registers are wasted by keeping a value in two places at the same time.
227 enum ComplementSpillMode {
228 /// SM_Partition(Default) - Try to create the complement interval so it
229 /// doesn't overlap any other intervals, and the original interval is
230 /// partitioned. This may require a large number of back copies and extra
231 /// PHI-defs. Only segments marked with overlapIntv will be overlapping.
232 SM_Partition,
233
234 /// SM_Size - Overlap intervals to minimize the number of inserted COPY
235 /// instructions. Copies to the complement interval are hoisted to their
236 /// common dominator, so only one COPY is required per value in the
237 /// complement interval. This also means that no extra PHI-defs need to be
238 /// inserted in the complement interval.
239 SM_Size,
240
241 /// SM_Speed - Overlap intervals to minimize the expected execution
242 /// frequency of the inserted copies. This is very similar to SM_Size, but
243 /// the complement interval may get some extra PHI-defs.
244 SM_Speed
245 };
246
247private:
248
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000249 /// Edit - The current parent register and new intervals created.
Jakob Stoklund Olesena2cae582011-03-02 23:31:50 +0000250 LiveRangeEdit *Edit;
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000251
Eric Christopher0f438112011-02-03 06:18:29 +0000252 /// Index into Edit of the currently open interval.
253 /// The index 0 is used for the complement, so the first interval started by
254 /// openIntv will be 1.
255 unsigned OpenIdx;
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000256
Jakob Stoklund Olesen708d06f2011-09-12 16:49:21 +0000257 /// The current spill mode, selected by reset().
258 ComplementSpillMode SpillMode;
259
Eric Christopher0f438112011-02-03 06:18:29 +0000260 typedef IntervalMap<SlotIndex, unsigned> RegAssignMap;
261
262 /// Allocator for the interval map. This will eventually be shared with
263 /// SlotIndexes and LiveIntervals.
264 RegAssignMap::Allocator Allocator;
265
266 /// RegAssign - Map of the assigned register indexes.
267 /// Edit.get(RegAssign.lookup(Idx)) is the register that should be live at
268 /// Idx.
269 RegAssignMap RegAssign;
270
Jakob Stoklund Olesen393bfcb2011-09-13 23:09:04 +0000271 typedef PointerIntPair<VNInfo*, 1> ValueForcePair;
272 typedef DenseMap<std::pair<unsigned, unsigned>, ValueForcePair> ValueMap;
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000273
274 /// Values - keep track of the mapping from parent values to values in the new
275 /// intervals. Given a pair (RegIdx, ParentVNI->id), Values contains:
276 ///
277 /// 1. No entry - the value is not mapped to Edit.get(RegIdx).
Jakob Stoklund Olesen393bfcb2011-09-13 23:09:04 +0000278 /// 2. (Null, false) - the value is mapped to multiple values in
279 /// Edit.get(RegIdx). Each value is represented by a minimal live range at
280 /// its def. The full live range can be inferred exactly from the range
281 /// of RegIdx in RegAssign.
282 /// 3. (Null, true). As above, but the ranges in RegAssign are too large, and
283 /// the live range must be recomputed using LiveRangeCalc::extend().
284 /// 4. (VNI, false) The value is mapped to a single new value.
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000285 /// The new value has no live ranges anywhere.
286 ValueMap Values;
287
Jakob Stoklund Olesenc1c622e2011-09-13 16:47:53 +0000288 /// LRCalc - Cache for computing live ranges and SSA update. Each instance
289 /// can only handle non-overlapping live ranges, so use a separate
290 /// LiveRangeCalc instance for the complement interval when in spill mode.
291 LiveRangeCalc LRCalc[2];
292
293 /// getLRCalc - Return the LRCalc to use for RegIdx. In spill mode, the
294 /// complement interval can overlap the other intervals, so it gets its own
295 /// LRCalc instance. When not in spill mode, all intervals can share one.
296 LiveRangeCalc &getLRCalc(unsigned RegIdx) {
297 return LRCalc[SpillMode != SM_Partition && RegIdx != 0];
298 }
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000299
Jakob Stoklund Olesen670ccd12011-03-01 23:14:53 +0000300 /// defValue - define a value in RegIdx from ParentVNI at Idx.
301 /// Idx does not have to be ParentVNI->def, but it must be contained within
302 /// ParentVNI's live range in ParentLI. The new value is added to the value
303 /// map.
304 /// Return the new LI value.
305 VNInfo *defValue(unsigned RegIdx, const VNInfo *ParentVNI, SlotIndex Idx);
306
Jakob Stoklund Olesen393bfcb2011-09-13 23:09:04 +0000307 /// forceRecompute - Force the live range of ParentVNI in RegIdx to be
308 /// recomputed by LiveRangeCalc::extend regardless of the number of defs.
309 /// This is used for values whose live range doesn't match RegAssign exactly.
310 /// They could have rematerialized, or back-copies may have been moved.
311 void forceRecompute(unsigned RegIdx, const VNInfo *ParentVNI);
Jakob Stoklund Olesene5a2e362011-09-13 18:05:29 +0000312
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000313 /// defFromParent - Define Reg from ParentVNI at UseIdx using either
314 /// rematerialization or a COPY from parent. Return the new value.
Eric Christopher0f438112011-02-03 06:18:29 +0000315 VNInfo *defFromParent(unsigned RegIdx,
Jakob Stoklund Olesencfa71342010-11-10 19:31:50 +0000316 VNInfo *ParentVNI,
317 SlotIndex UseIdx,
318 MachineBasicBlock &MBB,
319 MachineBasicBlock::iterator I);
320
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +0000321 /// removeBackCopies - Remove the copy instructions that defines the values
322 /// in the vector in the complement interval.
323 void removeBackCopies(SmallVectorImpl<VNInfo*> &Copies);
324
Jakob Stoklund Olesenc4c63382011-09-14 16:45:39 +0000325 /// getShallowDominator - Returns the least busy dominator of MBB that is
326 /// also dominated by DefMBB. Busy is measured by loop depth.
327 MachineBasicBlock *findShallowDominator(MachineBasicBlock *MBB,
328 MachineBasicBlock *DefMBB);
329
Jakob Stoklund Olesenb21abfe2011-09-13 22:22:39 +0000330 /// hoistCopiesForSize - Hoist back-copies to the complement interval in a
331 /// way that minimizes code size. This implements the SM_Size spill mode.
332 void hoistCopiesForSize();
333
Jakob Stoklund Olesen44b7ae22011-04-15 17:24:49 +0000334 /// transferValues - Transfer values to the new ranges.
335 /// Return true if any ranges were skipped.
336 bool transferValues();
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +0000337
Jakob Stoklund Olesene2dc0c92011-03-02 23:05:16 +0000338 /// extendPHIKillRanges - Extend the ranges of all values killed by original
339 /// parent PHIDefs.
340 void extendPHIKillRanges();
341
Eric Christopher0f438112011-02-03 06:18:29 +0000342 /// rewriteAssigned - Rewrite all uses of Edit.getReg() to assigned registers.
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +0000343 void rewriteAssigned(bool ExtendRanges);
Jakob Stoklund Olesen5fa42a42010-09-21 22:32:21 +0000344
Jakob Stoklund Olesen58817992011-03-08 22:46:11 +0000345 /// deleteRematVictims - Delete defs that are dead after rematerializing.
346 void deleteRematVictims();
347
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000348public:
349 /// Create a new SplitEditor for editing the LiveInterval analyzed by SA.
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000350 /// Newly created intervals will be appended to newIntervals.
Jakob Stoklund Olesend68f4582010-10-28 20:34:50 +0000351 SplitEditor(SplitAnalysis &SA, LiveIntervals&, VirtRegMap&,
Jakob Stoklund Olesenbece06f2011-03-03 01:29:13 +0000352 MachineDominatorTree&);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000353
Jakob Stoklund Olesenbece06f2011-03-03 01:29:13 +0000354 /// reset - Prepare for a new split.
Jakob Stoklund Olesen708d06f2011-09-12 16:49:21 +0000355 void reset(LiveRangeEdit&, ComplementSpillMode = SM_Partition);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000356
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000357 /// Create a new virtual register and live interval.
Jakob Stoklund Olesene1b43c32011-04-12 18:11:31 +0000358 /// Return the interval index, starting from 1. Interval index 0 is the
359 /// implicit complement interval.
360 unsigned openIntv();
361
362 /// currentIntv - Return the current interval index.
363 unsigned currentIntv() const { return OpenIdx; }
364
365 /// selectIntv - Select a previously opened interval index.
366 void selectIntv(unsigned Idx);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000367
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000368 /// enterIntvBefore - Enter the open interval before the instruction at Idx.
369 /// If the parent interval is not live before Idx, a COPY is not inserted.
370 /// Return the beginning of the new live range.
371 SlotIndex enterIntvBefore(SlotIndex Idx);
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000372
Jakob Stoklund Olesen87360f72011-06-30 01:30:39 +0000373 /// enterIntvAfter - Enter the open interval after the instruction at Idx.
374 /// Return the beginning of the new live range.
375 SlotIndex enterIntvAfter(SlotIndex Idx);
376
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000377 /// enterIntvAtEnd - Enter the open interval at the end of MBB.
378 /// Use the open interval from he inserted copy to the MBB end.
379 /// Return the beginning of the new live range.
380 SlotIndex enterIntvAtEnd(MachineBasicBlock &MBB);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000381
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000382 /// useIntv - indicate that all instructions in MBB should use OpenLI.
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000383 void useIntv(const MachineBasicBlock &MBB);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000384
Jakob Stoklund Olesen07862842011-01-26 00:50:53 +0000385 /// useIntv - indicate that all instructions in range should use OpenLI.
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000386 void useIntv(SlotIndex Start, SlotIndex End);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000387
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000388 /// leaveIntvAfter - Leave the open interval after the instruction at Idx.
389 /// Return the end of the live range.
390 SlotIndex leaveIntvAfter(SlotIndex Idx);
Jakob Stoklund Olesenf1b05f22010-08-12 17:07:14 +0000391
Jakob Stoklund Olesen9b057772011-02-09 23:30:25 +0000392 /// leaveIntvBefore - Leave the open interval before the instruction at Idx.
393 /// Return the end of the live range.
394 SlotIndex leaveIntvBefore(SlotIndex Idx);
395
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000396 /// leaveIntvAtTop - Leave the interval at the top of MBB.
Jakob Stoklund Olesen207c8682011-02-03 17:04:16 +0000397 /// Add liveness from the MBB top to the copy.
398 /// Return the end of the live range.
399 SlotIndex leaveIntvAtTop(MachineBasicBlock &MBB);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000400
Jakob Stoklund Olesen5c716bd2011-02-08 18:50:21 +0000401 /// overlapIntv - Indicate that all instructions in range should use the open
402 /// interval, but also let the complement interval be live.
403 ///
404 /// This doubles the register pressure, but is sometimes required to deal with
405 /// register uses after the last valid split point.
406 ///
407 /// The Start index should be a return value from a leaveIntv* call, and End
408 /// should be in the same basic block. The parent interval must have the same
409 /// value across the range.
410 ///
411 void overlapIntv(SlotIndex Start, SlotIndex End);
412
Jakob Stoklund Olesen74669272010-10-08 23:42:21 +0000413 /// finish - after all the new live ranges have been created, compute the
414 /// remaining live range, and rewrite instructions to use the new registers.
Jakob Stoklund Olesen59280462011-04-21 18:38:15 +0000415 /// @param LRMap When not null, this vector will map each live range in Edit
416 /// back to the indices returned by openIntv.
417 /// There may be extra indices created by dead code elimination.
418 void finish(SmallVectorImpl<unsigned> *LRMap = 0);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000419
Eric Christopher0f438112011-02-03 06:18:29 +0000420 /// dump - print the current interval maping to dbgs().
421 void dump() const;
422
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000423 // ===--- High level methods ---===
424
Jakob Stoklund Olesenfd5c5132011-04-12 19:32:53 +0000425 /// splitSingleBlock - Split CurLI into a separate live interval around the
426 /// uses in a single block. This is intended to be used as part of a larger
427 /// split, and doesn't call finish().
428 void splitSingleBlock(const SplitAnalysis::BlockInfo &BI);
429
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +0000430 /// splitLiveThroughBlock - Split CurLI in the given block such that it
431 /// enters the block in IntvIn and leaves it in IntvOut. There may be uses in
432 /// the block, but they will be ignored when placing split points.
433 ///
434 /// @param MBBNum Block number.
435 /// @param IntvIn Interval index entering the block.
436 /// @param LeaveBefore When set, leave IntvIn before this point.
437 /// @param IntvOut Interval index leaving the block.
438 /// @param EnterAfter When set, enter IntvOut after this point.
439 void splitLiveThroughBlock(unsigned MBBNum,
440 unsigned IntvIn, SlotIndex LeaveBefore,
441 unsigned IntvOut, SlotIndex EnterAfter);
442
443 /// splitRegInBlock - Split CurLI in the given block such that it enters the
444 /// block in IntvIn and leaves it on the stack (or not at all). Split points
445 /// are placed in a way that avoids putting uses in the stack interval. This
446 /// may require creating a local interval when there is interference.
447 ///
448 /// @param BI Block descriptor.
449 /// @param IntvIn Interval index entering the block. Not 0.
450 /// @param LeaveBefore When set, leave IntvIn before this point.
451 void splitRegInBlock(const SplitAnalysis::BlockInfo &BI,
452 unsigned IntvIn, SlotIndex LeaveBefore);
453
454 /// splitRegOutBlock - Split CurLI in the given block such that it enters the
455 /// block on the stack (or isn't live-in at all) and leaves it in IntvOut.
456 /// Split points are placed to avoid interference and such that the uses are
457 /// not in the stack interval. This may require creating a local interval
458 /// when there is interference.
459 ///
460 /// @param BI Block descriptor.
461 /// @param IntvOut Interval index leaving the block.
462 /// @param EnterAfter When set, enter IntvOut after this point.
463 void splitRegOutBlock(const SplitAnalysis::BlockInfo &BI,
464 unsigned IntvOut, SlotIndex EnterAfter);
Jakob Stoklund Olesenfc412d82010-08-13 21:18:48 +0000465};
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000466
467}
Sebastian Redl6796e4f2011-04-24 15:46:51 +0000468
469#endif