blob: af97e4f8a9cb9df5e0ab7ca4a2eb2da701c1d566 [file] [log] [blame]
Jakob Stoklund Olesenf96ae682011-01-04 21:10:05 +00001//===-------- SplitKit.h - Toolkit for splitting live ranges ----*- C++ -*-===//
Jakob Stoklund Olesen36d12c62010-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
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000015#ifndef LLVM_LIB_CODEGEN_SPLITKIT_H
16#define LLVM_LIB_CODEGEN_SPLITKIT_H
Sebastian Redlb8a62aa2011-04-24 15:46:51 +000017
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +000018#include "LiveRangeCalc.h"
Jakob Stoklund Olesenbf91c4e2011-04-06 03:57:00 +000019#include "llvm/ADT/ArrayRef.h"
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +000020#include "llvm/ADT/DenseMap.h"
Wei Mi9a16d652016-04-13 03:08:27 +000021#include "llvm/ADT/DenseSet.h"
Eric Christopherede62672011-02-03 06:18:29 +000022#include "llvm/ADT/IntervalMap.h"
Jakob Stoklund Olesen2530cd22010-12-21 01:50:21 +000023#include "llvm/ADT/SmallPtrSet.h"
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +000024
25namespace llvm {
26
Eric Christopherede62672011-02-03 06:18:29 +000027class ConnectedVNInfoEqClasses;
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +000028class LiveInterval;
29class LiveIntervals;
Jakob Stoklund Olesen72911e42010-10-14 23:49:52 +000030class LiveRangeEdit;
Benjamin Kramere2a1d892013-06-17 19:00:36 +000031class MachineBlockFrequencyInfo;
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +000032class MachineInstr;
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +000033class MachineLoopInfo;
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +000034class MachineRegisterInfo;
Jakob Stoklund Olesened4075c2010-07-20 21:46:58 +000035class TargetInstrInfo;
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +000036class TargetRegisterInfo;
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +000037class VirtRegMap;
38class VNInfo;
Jakob Stoklund Olesen9a743012010-10-22 20:28:21 +000039class raw_ostream;
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +000040
Wei Mi35ee9332016-05-11 22:28:29 +000041/// Determines the latest safe point in a block in which we can insert a split,
42/// spill or other instruction related with CurLI.
43class LLVM_LIBRARY_VISIBILITY InsertPointAnalysis {
44private:
45 const LiveIntervals &LIS;
46
47 /// Current LiveInterval for which to insert split or spill.
48 const LiveInterval *CurLI;
49
50 /// Last legal insert point in each basic block in the current function.
51 /// The first entry is the first terminator, the second entry is the
52 /// last valid point to insert a split or spill for a variable that is
53 /// live into a landing pad successor.
54 SmallVector<std::pair<SlotIndex, SlotIndex>, 8> LastInsertPoint;
55
56 SlotIndex computeLastInsertPoint(const MachineBasicBlock &MBB);
57
58public:
59 InsertPointAnalysis(const LiveIntervals &lis, unsigned BBNum);
60
61 void setInterval(const LiveInterval *LI) { CurLI = LI; }
62
63 /// Return the base index of the last valid insert point in \pMBB.
64 SlotIndex getLastInsertPoint(const MachineBasicBlock &MBB) {
65 unsigned Num = MBB.getNumber();
66 // Inline the common simple case.
67 if (LastInsertPoint[Num].first.isValid() &&
68 !LastInsertPoint[Num].second.isValid())
69 return LastInsertPoint[Num].first;
70 return computeLastInsertPoint(MBB);
71 }
72
73 /// Returns the last insert point as an iterator.
74 MachineBasicBlock::iterator getLastInsertPointIter(MachineBasicBlock &);
75};
76
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +000077/// SplitAnalysis - Analyze a LiveInterval, looking for live range splitting
78/// opportunities.
Benjamin Kramerf4c20252015-07-01 14:47:39 +000079class LLVM_LIBRARY_VISIBILITY SplitAnalysis {
Jakob Stoklund Olesen284c2db2010-08-10 17:07:22 +000080public:
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +000081 const MachineFunction &MF;
Jakob Stoklund Olesenf1a60a62011-02-19 00:53:42 +000082 const VirtRegMap &VRM;
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +000083 const LiveIntervals &LIS;
84 const MachineLoopInfo &Loops;
85 const TargetInstrInfo &TII;
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +000086
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +000087 /// Additional information about basic blocks where the current variable is
88 /// live. Such a block will look like one of these templates:
89 ///
90 /// 1. | o---x | Internal to block. Variable is only live in this block.
91 /// 2. |---x | Live-in, kill.
92 /// 3. | o---| Def, live-out.
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +000093 /// 4. |---x o---| Live-in, kill, def, live-out. Counted by NumGapBlocks.
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +000094 /// 5. |---o---o---| Live-through with uses or defs.
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +000095 /// 6. |-----------| Live-through without uses. Counted by NumThroughBlocks.
96 ///
97 /// Two BlockInfo entries are created for template 4. One for the live-in
98 /// segment, and one for the live-out segment. These entries look as if the
99 /// block were split in the middle where the live range isn't live.
100 ///
101 /// Live-through blocks without any uses don't get BlockInfo entries. They
102 /// are simply listed in ThroughBlocks instead.
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000103 ///
104 struct BlockInfo {
105 MachineBasicBlock *MBB;
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +0000106 SlotIndex FirstInstr; ///< First instr accessing current reg.
107 SlotIndex LastInstr; ///< Last instr accessing current reg.
Jakob Stoklund Olesenae8027c2011-08-02 22:37:22 +0000108 SlotIndex FirstDef; ///< First non-phi valno->def, or SlotIndex().
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000109 bool LiveIn; ///< Current reg is live in.
110 bool LiveOut; ///< Current reg is live out.
Jakob Stoklund Olesenadc6a4c2011-06-30 01:30:39 +0000111
112 /// isOneInstr - Returns true when this BlockInfo describes a single
113 /// instruction.
114 bool isOneInstr() const {
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +0000115 return SlotIndex::isSameInstr(FirstInstr, LastInstr);
Jakob Stoklund Olesenadc6a4c2011-06-30 01:30:39 +0000116 }
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000117 };
118
Jakob Stoklund Olesen622848b2010-08-12 17:07:14 +0000119private:
120 // Current live interval.
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000121 const LiveInterval *CurLI;
Jakob Stoklund Olesen622848b2010-08-12 17:07:14 +0000122
Wei Mi35ee9332016-05-11 22:28:29 +0000123 /// Insert Point Analysis.
124 InsertPointAnalysis IPA;
125
Jakob Stoklund Olesen994fed62012-01-12 17:53:44 +0000126 // Sorted slot indexes of using instructions.
127 SmallVector<SlotIndex, 8> UseSlots;
128
Jakob Stoklund Olesenbf91c4e2011-04-06 03:57:00 +0000129 /// UseBlocks - Blocks where CurLI has uses.
130 SmallVector<BlockInfo, 8> UseBlocks;
131
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000132 /// NumGapBlocks - Number of duplicate entries in UseBlocks for blocks where
133 /// the live range has a gap.
134 unsigned NumGapBlocks;
135
Jakob Stoklund Olesenbf91c4e2011-04-06 03:57:00 +0000136 /// ThroughBlocks - Block numbers where CurLI is live through without uses.
Jakob Stoklund Olesened47ed42011-04-09 02:59:09 +0000137 BitVector ThroughBlocks;
138
139 /// NumThroughBlocks - Number of live-through blocks.
140 unsigned NumThroughBlocks;
Jakob Stoklund Olesenbf91c4e2011-04-06 03:57:00 +0000141
Jakob Stoklund Oleseneaa6ed12011-05-03 20:42:13 +0000142 /// DidRepairRange - analyze was forced to shrinkToUses().
143 bool DidRepairRange;
144
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000145 // Sumarize statistics by counting instructions using CurLI.
Jakob Stoklund Olesenff095502010-07-20 16:12:37 +0000146 void analyzeUses();
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +0000147
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000148 /// calcLiveBlockInfo - Compute per-block information about CurLI.
Jakob Stoklund Olesen27e0a4a2011-03-05 18:33:49 +0000149 bool calcLiveBlockInfo();
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000150
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +0000151public:
Jakob Stoklund Olesenf1a60a62011-02-19 00:53:42 +0000152 SplitAnalysis(const VirtRegMap &vrm, const LiveIntervals &lis,
Jakob Stoklund Olesen0fef9dd2010-07-20 23:50:15 +0000153 const MachineLoopInfo &mli);
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +0000154
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000155 /// analyze - set CurLI to the specified interval, and analyze how it may be
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +0000156 /// split.
157 void analyze(const LiveInterval *li);
158
Jakob Stoklund Oleseneaa6ed12011-05-03 20:42:13 +0000159 /// didRepairRange() - Returns true if CurLI was invalid and has been repaired
160 /// by analyze(). This really shouldn't happen, but sometimes the coalescer
161 /// can create live ranges that end in mid-air.
162 bool didRepairRange() const { return DidRepairRange; }
163
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +0000164 /// clear - clear all data structures so SplitAnalysis is ready to analyze a
165 /// new interval.
166 void clear();
167
Jakob Stoklund Olesen93c87362011-02-17 19:13:53 +0000168 /// getParent - Return the last analyzed interval.
169 const LiveInterval &getParent() const { return *CurLI; }
170
Jakob Stoklund Olesen60a26a62011-02-21 23:09:46 +0000171 /// isOriginalEndpoint - Return true if the original live range was killed or
172 /// (re-)defined at Idx. Idx should be the 'def' slot for a normal kill/def,
173 /// and 'use' for an early-clobber def.
174 /// This can be used to recognize code inserted by earlier live range
175 /// splitting.
176 bool isOriginalEndpoint(SlotIndex Idx) const;
177
Jakob Stoklund Olesen994fed62012-01-12 17:53:44 +0000178 /// getUseSlots - Return an array of SlotIndexes of instructions using CurLI.
179 /// This include both use and def operands, at most one entry per instruction.
180 ArrayRef<SlotIndex> getUseSlots() const { return UseSlots; }
181
Jakob Stoklund Olesenbf91c4e2011-04-06 03:57:00 +0000182 /// getUseBlocks - Return an array of BlockInfo objects for the basic blocks
183 /// where CurLI has uses.
Jakob Stoklund Olesen5cc91b22011-05-28 02:32:57 +0000184 ArrayRef<BlockInfo> getUseBlocks() const { return UseBlocks; }
Jakob Stoklund Olesenbf91c4e2011-04-06 03:57:00 +0000185
Jakob Stoklund Olesened47ed42011-04-09 02:59:09 +0000186 /// getNumThroughBlocks - Return the number of through blocks.
187 unsigned getNumThroughBlocks() const { return NumThroughBlocks; }
188
189 /// isThroughBlock - Return true if CurLI is live through MBB without uses.
190 bool isThroughBlock(unsigned MBB) const { return ThroughBlocks.test(MBB); }
Jakob Stoklund Olesenbf91c4e2011-04-06 03:57:00 +0000191
Jakob Stoklund Olesenc49df2c2011-04-12 21:30:53 +0000192 /// getThroughBlocks - Return the set of through blocks.
193 const BitVector &getThroughBlocks() const { return ThroughBlocks; }
194
Jakob Stoklund Olesen5cc91b22011-05-28 02:32:57 +0000195 /// getNumLiveBlocks - Return the number of blocks where CurLI is live.
196 unsigned getNumLiveBlocks() const {
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000197 return getUseBlocks().size() - NumGapBlocks + getNumThroughBlocks();
Jakob Stoklund Olesen5cc91b22011-05-28 02:32:57 +0000198 }
199
200 /// countLiveBlocks - Return the number of blocks where li is live. This is
201 /// guaranteed to return the same number as getNumLiveBlocks() after calling
202 /// analyze(li).
Jakob Stoklund Oleseneef23272011-04-26 22:33:12 +0000203 unsigned countLiveBlocks(const LiveInterval *li) const;
204
Jakob Stoklund Olesened4075c2010-07-20 21:46:58 +0000205 typedef SmallPtrSet<const MachineBasicBlock*, 16> BlockPtrSet;
206
Jakob Stoklund Olesen8627ea92011-08-05 22:20:45 +0000207 /// shouldSplitSingleBlock - Returns true if it would help to create a local
208 /// live range for the instructions in BI. There is normally no benefit to
209 /// creating a live range for a single instruction, but it does enable
210 /// register class inflation if the instruction has a restricted register
211 /// class.
212 ///
213 /// @param BI The block to be isolated.
214 /// @param SingleInstrs True when single instructions should be isolated.
215 bool shouldSplitSingleBlock(const BlockInfo &BI, bool SingleInstrs) const;
Wei Mi35ee9332016-05-11 22:28:29 +0000216
217 SlotIndex getLastSplitPoint(unsigned Num) {
218 return IPA.getLastInsertPoint(*MF.getBlockNumbered(Num));
219 }
220
221 MachineBasicBlock::iterator getLastSplitPointIter(MachineBasicBlock *BB) {
222 return IPA.getLastInsertPointIter(*BB);
223 }
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +0000224};
225
Jakob Stoklund Olesence6f0552010-08-18 19:00:08 +0000226
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000227/// SplitEditor - Edit machine code and LiveIntervals for live range
228/// splitting.
229///
Jakob Stoklund Olesen45e07c82010-08-06 22:17:33 +0000230/// - Create a SplitEditor from a SplitAnalysis.
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000231/// - Start a new live interval with openIntv.
232/// - Mark the places where the new interval is entered using enterIntv*
233/// - Mark the ranges where the new interval is used with useIntv*
234/// - Mark the places where the interval is exited with exitIntv*.
235/// - Finish the current interval with closeIntv and repeat from 2.
Jakob Stoklund Olesen959fcc62010-10-08 23:42:21 +0000236/// - Rewrite instructions with finish().
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000237///
Benjamin Kramerf4c20252015-07-01 14:47:39 +0000238class LLVM_LIBRARY_VISIBILITY SplitEditor {
Jakob Stoklund Olesen04aff702011-02-19 00:42:33 +0000239 SplitAnalysis &SA;
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000240 LiveIntervals &LIS;
241 VirtRegMap &VRM;
242 MachineRegisterInfo &MRI;
Eric Christopherede62672011-02-03 06:18:29 +0000243 MachineDominatorTree &MDT;
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000244 const TargetInstrInfo &TII;
245 const TargetRegisterInfo &TRI;
Benjamin Kramere2a1d892013-06-17 19:00:36 +0000246 const MachineBlockFrequencyInfo &MBFI;
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000247
Jakob Stoklund Oleseneecb2fb2011-09-12 16:49:21 +0000248public:
249
250 /// ComplementSpillMode - Select how the complement live range should be
251 /// created. SplitEditor automatically creates interval 0 to contain
252 /// anything that isn't added to another interval. This complement interval
253 /// can get quite complicated, and it can sometimes be an advantage to allow
254 /// it to overlap the other intervals. If it is going to spill anyway, no
255 /// registers are wasted by keeping a value in two places at the same time.
256 enum ComplementSpillMode {
257 /// SM_Partition(Default) - Try to create the complement interval so it
258 /// doesn't overlap any other intervals, and the original interval is
259 /// partitioned. This may require a large number of back copies and extra
260 /// PHI-defs. Only segments marked with overlapIntv will be overlapping.
261 SM_Partition,
262
263 /// SM_Size - Overlap intervals to minimize the number of inserted COPY
264 /// instructions. Copies to the complement interval are hoisted to their
265 /// common dominator, so only one COPY is required per value in the
266 /// complement interval. This also means that no extra PHI-defs need to be
267 /// inserted in the complement interval.
268 SM_Size,
269
270 /// SM_Speed - Overlap intervals to minimize the expected execution
271 /// frequency of the inserted copies. This is very similar to SM_Size, but
272 /// the complement interval may get some extra PHI-defs.
273 SM_Speed
274 };
275
276private:
277
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000278 /// Edit - The current parent register and new intervals created.
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000279 LiveRangeEdit *Edit;
Jakob Stoklund Olesen72911e42010-10-14 23:49:52 +0000280
Eric Christopherede62672011-02-03 06:18:29 +0000281 /// Index into Edit of the currently open interval.
282 /// The index 0 is used for the complement, so the first interval started by
283 /// openIntv will be 1.
284 unsigned OpenIdx;
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000285
Jakob Stoklund Oleseneecb2fb2011-09-12 16:49:21 +0000286 /// The current spill mode, selected by reset().
287 ComplementSpillMode SpillMode;
288
Eric Christopherede62672011-02-03 06:18:29 +0000289 typedef IntervalMap<SlotIndex, unsigned> RegAssignMap;
290
291 /// Allocator for the interval map. This will eventually be shared with
292 /// SlotIndexes and LiveIntervals.
293 RegAssignMap::Allocator Allocator;
294
295 /// RegAssign - Map of the assigned register indexes.
296 /// Edit.get(RegAssign.lookup(Idx)) is the register that should be live at
297 /// Idx.
298 RegAssignMap RegAssign;
299
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000300 typedef PointerIntPair<VNInfo*, 1> ValueForcePair;
301 typedef DenseMap<std::pair<unsigned, unsigned>, ValueForcePair> ValueMap;
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000302
303 /// Values - keep track of the mapping from parent values to values in the new
304 /// intervals. Given a pair (RegIdx, ParentVNI->id), Values contains:
305 ///
306 /// 1. No entry - the value is not mapped to Edit.get(RegIdx).
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000307 /// 2. (Null, false) - the value is mapped to multiple values in
308 /// Edit.get(RegIdx). Each value is represented by a minimal live range at
309 /// its def. The full live range can be inferred exactly from the range
310 /// of RegIdx in RegAssign.
311 /// 3. (Null, true). As above, but the ranges in RegAssign are too large, and
312 /// the live range must be recomputed using LiveRangeCalc::extend().
313 /// 4. (VNI, false) The value is mapped to a single new value.
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000314 /// The new value has no live ranges anywhere.
315 ValueMap Values;
316
Jakob Stoklund Olesen054984d2011-09-13 16:47:53 +0000317 /// LRCalc - Cache for computing live ranges and SSA update. Each instance
318 /// can only handle non-overlapping live ranges, so use a separate
319 /// LiveRangeCalc instance for the complement interval when in spill mode.
320 LiveRangeCalc LRCalc[2];
321
322 /// getLRCalc - Return the LRCalc to use for RegIdx. In spill mode, the
323 /// complement interval can overlap the other intervals, so it gets its own
324 /// LRCalc instance. When not in spill mode, all intervals can share one.
325 LiveRangeCalc &getLRCalc(unsigned RegIdx) {
326 return LRCalc[SpillMode != SM_Partition && RegIdx != 0];
327 }
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000328
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000329 /// defValue - define a value in RegIdx from ParentVNI at Idx.
330 /// Idx does not have to be ParentVNI->def, but it must be contained within
331 /// ParentVNI's live range in ParentLI. The new value is added to the value
332 /// map.
333 /// Return the new LI value.
334 VNInfo *defValue(unsigned RegIdx, const VNInfo *ParentVNI, SlotIndex Idx);
335
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000336 /// forceRecompute - Force the live range of ParentVNI in RegIdx to be
337 /// recomputed by LiveRangeCalc::extend regardless of the number of defs.
338 /// This is used for values whose live range doesn't match RegAssign exactly.
339 /// They could have rematerialized, or back-copies may have been moved.
340 void forceRecompute(unsigned RegIdx, const VNInfo *ParentVNI);
Jakob Stoklund Olesen4484f992011-09-13 18:05:29 +0000341
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000342 /// defFromParent - Define Reg from ParentVNI at UseIdx using either
343 /// rematerialization or a COPY from parent. Return the new value.
Eric Christopherede62672011-02-03 06:18:29 +0000344 VNInfo *defFromParent(unsigned RegIdx,
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000345 VNInfo *ParentVNI,
346 SlotIndex UseIdx,
347 MachineBasicBlock &MBB,
348 MachineBasicBlock::iterator I);
349
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000350 /// removeBackCopies - Remove the copy instructions that defines the values
351 /// in the vector in the complement interval.
352 void removeBackCopies(SmallVectorImpl<VNInfo*> &Copies);
353
Jakob Stoklund Olesena98af392011-09-14 16:45:39 +0000354 /// getShallowDominator - Returns the least busy dominator of MBB that is
355 /// also dominated by DefMBB. Busy is measured by loop depth.
356 MachineBasicBlock *findShallowDominator(MachineBasicBlock *MBB,
357 MachineBasicBlock *DefMBB);
358
Wei Mi9a16d652016-04-13 03:08:27 +0000359 /// Find out all the backCopies dominated by others.
360 void computeRedundantBackCopies(DenseSet<unsigned> &NotToHoistSet,
361 SmallVectorImpl<VNInfo *> &BackCopies);
362
363 /// Hoist back-copies to the complement interval. It tries to hoist all
364 /// the back-copies to one BB if it is beneficial, or else simply remove
Eric Christopher75d661a2016-05-04 21:45:36 +0000365 /// redundant backcopies dominated by others.
Wei Mi9a16d652016-04-13 03:08:27 +0000366 void hoistCopies();
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000367
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000368 /// transferValues - Transfer values to the new ranges.
369 /// Return true if any ranges were skipped.
370 bool transferValues();
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +0000371
Jakob Stoklund Olesen36482632011-03-02 23:05:16 +0000372 /// extendPHIKillRanges - Extend the ranges of all values killed by original
373 /// parent PHIDefs.
374 void extendPHIKillRanges();
375
Eric Christopherede62672011-02-03 06:18:29 +0000376 /// rewriteAssigned - Rewrite all uses of Edit.getReg() to assigned registers.
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +0000377 void rewriteAssigned(bool ExtendRanges);
Jakob Stoklund Olesen6f8bd422010-09-21 22:32:21 +0000378
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +0000379 /// deleteRematVictims - Delete defs that are dead after rematerializing.
380 void deleteRematVictims();
381
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000382public:
383 /// Create a new SplitEditor for editing the LiveInterval analyzed by SA.
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000384 /// Newly created intervals will be appended to newIntervals.
Jakob Stoklund Olesene172a8b2010-10-28 20:34:50 +0000385 SplitEditor(SplitAnalysis &SA, LiveIntervals&, VirtRegMap&,
Benjamin Kramere2a1d892013-06-17 19:00:36 +0000386 MachineDominatorTree&, MachineBlockFrequencyInfo &);
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000387
Jakob Stoklund Olesenc9601982011-03-03 01:29:13 +0000388 /// reset - Prepare for a new split.
Jakob Stoklund Oleseneecb2fb2011-09-12 16:49:21 +0000389 void reset(LiveRangeEdit&, ComplementSpillMode = SM_Partition);
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000390
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000391 /// Create a new virtual register and live interval.
Jakob Stoklund Olesen0840f502011-04-12 18:11:31 +0000392 /// Return the interval index, starting from 1. Interval index 0 is the
393 /// implicit complement interval.
394 unsigned openIntv();
395
396 /// currentIntv - Return the current interval index.
397 unsigned currentIntv() const { return OpenIdx; }
398
399 /// selectIntv - Select a previously opened interval index.
400 void selectIntv(unsigned Idx);
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000401
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000402 /// enterIntvBefore - Enter the open interval before the instruction at Idx.
403 /// If the parent interval is not live before Idx, a COPY is not inserted.
404 /// Return the beginning of the new live range.
405 SlotIndex enterIntvBefore(SlotIndex Idx);
Jakob Stoklund Olesen622848b2010-08-12 17:07:14 +0000406
Jakob Stoklund Olesenadc6a4c2011-06-30 01:30:39 +0000407 /// enterIntvAfter - Enter the open interval after the instruction at Idx.
408 /// Return the beginning of the new live range.
409 SlotIndex enterIntvAfter(SlotIndex Idx);
410
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000411 /// enterIntvAtEnd - Enter the open interval at the end of MBB.
Eric Christopher650c8f22014-05-20 17:11:11 +0000412 /// Use the open interval from the inserted copy to the MBB end.
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000413 /// Return the beginning of the new live range.
414 SlotIndex enterIntvAtEnd(MachineBasicBlock &MBB);
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000415
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000416 /// useIntv - indicate that all instructions in MBB should use OpenLI.
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000417 void useIntv(const MachineBasicBlock &MBB);
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000418
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000419 /// useIntv - indicate that all instructions in range should use OpenLI.
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000420 void useIntv(SlotIndex Start, SlotIndex End);
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000421
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000422 /// leaveIntvAfter - Leave the open interval after the instruction at Idx.
423 /// Return the end of the live range.
424 SlotIndex leaveIntvAfter(SlotIndex Idx);
Jakob Stoklund Olesen622848b2010-08-12 17:07:14 +0000425
Jakob Stoklund Olesen7cb57b32011-02-09 23:30:25 +0000426 /// leaveIntvBefore - Leave the open interval before the instruction at Idx.
427 /// Return the end of the live range.
428 SlotIndex leaveIntvBefore(SlotIndex Idx);
429
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000430 /// leaveIntvAtTop - Leave the interval at the top of MBB.
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000431 /// Add liveness from the MBB top to the copy.
432 /// Return the end of the live range.
433 SlotIndex leaveIntvAtTop(MachineBasicBlock &MBB);
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000434
Jakob Stoklund Olesen17499352011-02-08 18:50:21 +0000435 /// overlapIntv - Indicate that all instructions in range should use the open
436 /// interval, but also let the complement interval be live.
437 ///
438 /// This doubles the register pressure, but is sometimes required to deal with
439 /// register uses after the last valid split point.
440 ///
441 /// The Start index should be a return value from a leaveIntv* call, and End
442 /// should be in the same basic block. The parent interval must have the same
443 /// value across the range.
444 ///
445 void overlapIntv(SlotIndex Start, SlotIndex End);
446
Jakob Stoklund Olesen959fcc62010-10-08 23:42:21 +0000447 /// finish - after all the new live ranges have been created, compute the
448 /// remaining live range, and rewrite instructions to use the new registers.
Jakob Stoklund Olesen6a663b82011-04-21 18:38:15 +0000449 /// @param LRMap When not null, this vector will map each live range in Edit
450 /// back to the indices returned by openIntv.
451 /// There may be extra indices created by dead code elimination.
Craig Topperada08572014-04-16 04:21:27 +0000452 void finish(SmallVectorImpl<unsigned> *LRMap = nullptr);
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000453
Jim Grosbachbfe3a9c2015-05-02 00:44:07 +0000454 /// dump - print the current interval mapping to dbgs().
Eric Christopherede62672011-02-03 06:18:29 +0000455 void dump() const;
456
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000457 // ===--- High level methods ---===
458
Jakob Stoklund Olesenc70b6972011-04-12 19:32:53 +0000459 /// splitSingleBlock - Split CurLI into a separate live interval around the
460 /// uses in a single block. This is intended to be used as part of a larger
461 /// split, and doesn't call finish().
462 void splitSingleBlock(const SplitAnalysis::BlockInfo &BI);
463
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +0000464 /// splitLiveThroughBlock - Split CurLI in the given block such that it
465 /// enters the block in IntvIn and leaves it in IntvOut. There may be uses in
466 /// the block, but they will be ignored when placing split points.
467 ///
468 /// @param MBBNum Block number.
469 /// @param IntvIn Interval index entering the block.
470 /// @param LeaveBefore When set, leave IntvIn before this point.
471 /// @param IntvOut Interval index leaving the block.
472 /// @param EnterAfter When set, enter IntvOut after this point.
473 void splitLiveThroughBlock(unsigned MBBNum,
474 unsigned IntvIn, SlotIndex LeaveBefore,
475 unsigned IntvOut, SlotIndex EnterAfter);
476
477 /// splitRegInBlock - Split CurLI in the given block such that it enters the
478 /// block in IntvIn and leaves it on the stack (or not at all). Split points
479 /// are placed in a way that avoids putting uses in the stack interval. This
480 /// may require creating a local interval when there is interference.
481 ///
482 /// @param BI Block descriptor.
483 /// @param IntvIn Interval index entering the block. Not 0.
484 /// @param LeaveBefore When set, leave IntvIn before this point.
485 void splitRegInBlock(const SplitAnalysis::BlockInfo &BI,
486 unsigned IntvIn, SlotIndex LeaveBefore);
487
488 /// splitRegOutBlock - Split CurLI in the given block such that it enters the
489 /// block on the stack (or isn't live-in at all) and leaves it in IntvOut.
490 /// Split points are placed to avoid interference and such that the uses are
491 /// not in the stack interval. This may require creating a local interval
492 /// when there is interference.
493 ///
494 /// @param BI Block descriptor.
495 /// @param IntvOut Interval index leaving the block.
496 /// @param EnterAfter When set, enter IntvOut after this point.
497 void splitRegOutBlock(const SplitAnalysis::BlockInfo &BI,
498 unsigned IntvOut, SlotIndex EnterAfter);
Jakob Stoklund Olesend1191ee2010-08-13 21:18:48 +0000499};
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +0000500
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000501}
Sebastian Redlb8a62aa2011-04-24 15:46:51 +0000502
503#endif