blob: 9d409e924a3d85392ee26ca25079f21043d330e1 [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
Wei Mi35ee9332016-05-11 22:28:29 +000047 /// Last legal insert point in each basic block in the current function.
48 /// The first entry is the first terminator, the second entry is the
49 /// last valid point to insert a split or spill for a variable that is
50 /// live into a landing pad successor.
51 SmallVector<std::pair<SlotIndex, SlotIndex>, 8> LastInsertPoint;
52
Wei Mif3c8f532016-05-23 19:39:19 +000053 SlotIndex computeLastInsertPoint(const LiveInterval &CurLI,
54 const MachineBasicBlock &MBB);
Wei Mi35ee9332016-05-11 22:28:29 +000055
56public:
57 InsertPointAnalysis(const LiveIntervals &lis, unsigned BBNum);
58
Wei Mif3c8f532016-05-23 19:39:19 +000059 /// Return the base index of the last valid insert point for \pCurLI in \pMBB.
60 SlotIndex getLastInsertPoint(const LiveInterval &CurLI,
61 const MachineBasicBlock &MBB) {
Wei Mi35ee9332016-05-11 22:28:29 +000062 unsigned Num = MBB.getNumber();
63 // Inline the common simple case.
64 if (LastInsertPoint[Num].first.isValid() &&
65 !LastInsertPoint[Num].second.isValid())
66 return LastInsertPoint[Num].first;
Wei Mif3c8f532016-05-23 19:39:19 +000067 return computeLastInsertPoint(CurLI, MBB);
Wei Mi35ee9332016-05-11 22:28:29 +000068 }
69
Wei Mif3c8f532016-05-23 19:39:19 +000070 /// Returns the last insert point as an iterator for \pCurLI in \pMBB.
71 MachineBasicBlock::iterator getLastInsertPointIter(const LiveInterval &CurLI,
72 MachineBasicBlock &MBB);
Wei Mi35ee9332016-05-11 22:28:29 +000073};
74
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +000075/// SplitAnalysis - Analyze a LiveInterval, looking for live range splitting
76/// opportunities.
Benjamin Kramerf4c20252015-07-01 14:47:39 +000077class LLVM_LIBRARY_VISIBILITY SplitAnalysis {
Jakob Stoklund Olesen284c2db2010-08-10 17:07:22 +000078public:
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +000079 const MachineFunction &MF;
Jakob Stoklund Olesenf1a60a62011-02-19 00:53:42 +000080 const VirtRegMap &VRM;
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +000081 const LiveIntervals &LIS;
82 const MachineLoopInfo &Loops;
83 const TargetInstrInfo &TII;
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +000084
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +000085 /// Additional information about basic blocks where the current variable is
86 /// live. Such a block will look like one of these templates:
87 ///
88 /// 1. | o---x | Internal to block. Variable is only live in this block.
89 /// 2. |---x | Live-in, kill.
90 /// 3. | o---| Def, live-out.
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +000091 /// 4. |---x o---| Live-in, kill, def, live-out. Counted by NumGapBlocks.
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +000092 /// 5. |---o---o---| Live-through with uses or defs.
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +000093 /// 6. |-----------| Live-through without uses. Counted by NumThroughBlocks.
94 ///
95 /// Two BlockInfo entries are created for template 4. One for the live-in
96 /// segment, and one for the live-out segment. These entries look as if the
97 /// block were split in the middle where the live range isn't live.
98 ///
99 /// Live-through blocks without any uses don't get BlockInfo entries. They
100 /// are simply listed in ThroughBlocks instead.
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000101 ///
102 struct BlockInfo {
103 MachineBasicBlock *MBB;
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +0000104 SlotIndex FirstInstr; ///< First instr accessing current reg.
105 SlotIndex LastInstr; ///< Last instr accessing current reg.
Jakob Stoklund Olesenae8027c2011-08-02 22:37:22 +0000106 SlotIndex FirstDef; ///< First non-phi valno->def, or SlotIndex().
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000107 bool LiveIn; ///< Current reg is live in.
108 bool LiveOut; ///< Current reg is live out.
Jakob Stoklund Olesenadc6a4c2011-06-30 01:30:39 +0000109
110 /// isOneInstr - Returns true when this BlockInfo describes a single
111 /// instruction.
112 bool isOneInstr() const {
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +0000113 return SlotIndex::isSameInstr(FirstInstr, LastInstr);
Jakob Stoklund Olesenadc6a4c2011-06-30 01:30:39 +0000114 }
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000115 };
116
Jakob Stoklund Olesen622848b2010-08-12 17:07:14 +0000117private:
118 // Current live interval.
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000119 const LiveInterval *CurLI;
Jakob Stoklund Olesen622848b2010-08-12 17:07:14 +0000120
Wei Mi35ee9332016-05-11 22:28:29 +0000121 /// Insert Point Analysis.
122 InsertPointAnalysis IPA;
123
Jakob Stoklund Olesen994fed62012-01-12 17:53:44 +0000124 // Sorted slot indexes of using instructions.
125 SmallVector<SlotIndex, 8> UseSlots;
126
Jakob Stoklund Olesenbf91c4e2011-04-06 03:57:00 +0000127 /// UseBlocks - Blocks where CurLI has uses.
128 SmallVector<BlockInfo, 8> UseBlocks;
129
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000130 /// NumGapBlocks - Number of duplicate entries in UseBlocks for blocks where
131 /// the live range has a gap.
132 unsigned NumGapBlocks;
133
Jakob Stoklund Olesenbf91c4e2011-04-06 03:57:00 +0000134 /// ThroughBlocks - Block numbers where CurLI is live through without uses.
Jakob Stoklund Olesened47ed42011-04-09 02:59:09 +0000135 BitVector ThroughBlocks;
136
137 /// NumThroughBlocks - Number of live-through blocks.
138 unsigned NumThroughBlocks;
Jakob Stoklund Olesenbf91c4e2011-04-06 03:57:00 +0000139
Jakob Stoklund Oleseneaa6ed12011-05-03 20:42:13 +0000140 /// DidRepairRange - analyze was forced to shrinkToUses().
141 bool DidRepairRange;
142
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000143 // Sumarize statistics by counting instructions using CurLI.
Jakob Stoklund Olesenff095502010-07-20 16:12:37 +0000144 void analyzeUses();
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +0000145
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000146 /// calcLiveBlockInfo - Compute per-block information about CurLI.
Jakob Stoklund Olesen27e0a4a2011-03-05 18:33:49 +0000147 bool calcLiveBlockInfo();
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000148
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +0000149public:
Jakob Stoklund Olesenf1a60a62011-02-19 00:53:42 +0000150 SplitAnalysis(const VirtRegMap &vrm, const LiveIntervals &lis,
Jakob Stoklund Olesen0fef9dd2010-07-20 23:50:15 +0000151 const MachineLoopInfo &mli);
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +0000152
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000153 /// analyze - set CurLI to the specified interval, and analyze how it may be
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +0000154 /// split.
155 void analyze(const LiveInterval *li);
156
Jakob Stoklund Oleseneaa6ed12011-05-03 20:42:13 +0000157 /// didRepairRange() - Returns true if CurLI was invalid and has been repaired
158 /// by analyze(). This really shouldn't happen, but sometimes the coalescer
159 /// can create live ranges that end in mid-air.
160 bool didRepairRange() const { return DidRepairRange; }
161
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +0000162 /// clear - clear all data structures so SplitAnalysis is ready to analyze a
163 /// new interval.
164 void clear();
165
Jakob Stoklund Olesen93c87362011-02-17 19:13:53 +0000166 /// getParent - Return the last analyzed interval.
167 const LiveInterval &getParent() const { return *CurLI; }
168
Jakob Stoklund Olesen60a26a62011-02-21 23:09:46 +0000169 /// isOriginalEndpoint - Return true if the original live range was killed or
170 /// (re-)defined at Idx. Idx should be the 'def' slot for a normal kill/def,
171 /// and 'use' for an early-clobber def.
172 /// This can be used to recognize code inserted by earlier live range
173 /// splitting.
174 bool isOriginalEndpoint(SlotIndex Idx) const;
175
Jakob Stoklund Olesen994fed62012-01-12 17:53:44 +0000176 /// getUseSlots - Return an array of SlotIndexes of instructions using CurLI.
177 /// This include both use and def operands, at most one entry per instruction.
178 ArrayRef<SlotIndex> getUseSlots() const { return UseSlots; }
179
Jakob Stoklund Olesenbf91c4e2011-04-06 03:57:00 +0000180 /// getUseBlocks - Return an array of BlockInfo objects for the basic blocks
181 /// where CurLI has uses.
Jakob Stoklund Olesen5cc91b22011-05-28 02:32:57 +0000182 ArrayRef<BlockInfo> getUseBlocks() const { return UseBlocks; }
Jakob Stoklund Olesenbf91c4e2011-04-06 03:57:00 +0000183
Jakob Stoklund Olesened47ed42011-04-09 02:59:09 +0000184 /// getNumThroughBlocks - Return the number of through blocks.
185 unsigned getNumThroughBlocks() const { return NumThroughBlocks; }
186
187 /// isThroughBlock - Return true if CurLI is live through MBB without uses.
188 bool isThroughBlock(unsigned MBB) const { return ThroughBlocks.test(MBB); }
Jakob Stoklund Olesenbf91c4e2011-04-06 03:57:00 +0000189
Jakob Stoklund Olesenc49df2c2011-04-12 21:30:53 +0000190 /// getThroughBlocks - Return the set of through blocks.
191 const BitVector &getThroughBlocks() const { return ThroughBlocks; }
192
Jakob Stoklund Olesen5cc91b22011-05-28 02:32:57 +0000193 /// getNumLiveBlocks - Return the number of blocks where CurLI is live.
194 unsigned getNumLiveBlocks() const {
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000195 return getUseBlocks().size() - NumGapBlocks + getNumThroughBlocks();
Jakob Stoklund Olesen5cc91b22011-05-28 02:32:57 +0000196 }
197
198 /// countLiveBlocks - Return the number of blocks where li is live. This is
199 /// guaranteed to return the same number as getNumLiveBlocks() after calling
200 /// analyze(li).
Jakob Stoklund Oleseneef23272011-04-26 22:33:12 +0000201 unsigned countLiveBlocks(const LiveInterval *li) const;
202
Jakob Stoklund Olesened4075c2010-07-20 21:46:58 +0000203 typedef SmallPtrSet<const MachineBasicBlock*, 16> BlockPtrSet;
204
Jakob Stoklund Olesen8627ea92011-08-05 22:20:45 +0000205 /// shouldSplitSingleBlock - Returns true if it would help to create a local
206 /// live range for the instructions in BI. There is normally no benefit to
207 /// creating a live range for a single instruction, but it does enable
208 /// register class inflation if the instruction has a restricted register
209 /// class.
210 ///
211 /// @param BI The block to be isolated.
212 /// @param SingleInstrs True when single instructions should be isolated.
213 bool shouldSplitSingleBlock(const BlockInfo &BI, bool SingleInstrs) const;
Wei Mi35ee9332016-05-11 22:28:29 +0000214
215 SlotIndex getLastSplitPoint(unsigned Num) {
Wei Mif3c8f532016-05-23 19:39:19 +0000216 return IPA.getLastInsertPoint(*CurLI, *MF.getBlockNumbered(Num));
Wei Mi35ee9332016-05-11 22:28:29 +0000217 }
218
219 MachineBasicBlock::iterator getLastSplitPointIter(MachineBasicBlock *BB) {
Wei Mif3c8f532016-05-23 19:39:19 +0000220 return IPA.getLastInsertPointIter(*CurLI, *BB);
Wei Mi35ee9332016-05-11 22:28:29 +0000221 }
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +0000222};
223
Jakob Stoklund Olesence6f0552010-08-18 19:00:08 +0000224
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000225/// SplitEditor - Edit machine code and LiveIntervals for live range
226/// splitting.
227///
Jakob Stoklund Olesen45e07c82010-08-06 22:17:33 +0000228/// - Create a SplitEditor from a SplitAnalysis.
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000229/// - Start a new live interval with openIntv.
230/// - Mark the places where the new interval is entered using enterIntv*
231/// - Mark the ranges where the new interval is used with useIntv*
232/// - Mark the places where the interval is exited with exitIntv*.
233/// - Finish the current interval with closeIntv and repeat from 2.
Jakob Stoklund Olesen959fcc62010-10-08 23:42:21 +0000234/// - Rewrite instructions with finish().
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000235///
Benjamin Kramerf4c20252015-07-01 14:47:39 +0000236class LLVM_LIBRARY_VISIBILITY SplitEditor {
Jakob Stoklund Olesen04aff702011-02-19 00:42:33 +0000237 SplitAnalysis &SA;
Wei Mic0223702016-07-08 21:08:09 +0000238 AliasAnalysis &AA;
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000239 LiveIntervals &LIS;
240 VirtRegMap &VRM;
241 MachineRegisterInfo &MRI;
Eric Christopherede62672011-02-03 06:18:29 +0000242 MachineDominatorTree &MDT;
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000243 const TargetInstrInfo &TII;
244 const TargetRegisterInfo &TRI;
Benjamin Kramere2a1d892013-06-17 19:00:36 +0000245 const MachineBlockFrequencyInfo &MBFI;
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000246
Jakob Stoklund Oleseneecb2fb2011-09-12 16:49:21 +0000247public:
248
249 /// ComplementSpillMode - Select how the complement live range should be
250 /// created. SplitEditor automatically creates interval 0 to contain
251 /// anything that isn't added to another interval. This complement interval
252 /// can get quite complicated, and it can sometimes be an advantage to allow
253 /// it to overlap the other intervals. If it is going to spill anyway, no
254 /// registers are wasted by keeping a value in two places at the same time.
255 enum ComplementSpillMode {
256 /// SM_Partition(Default) - Try to create the complement interval so it
257 /// doesn't overlap any other intervals, and the original interval is
258 /// partitioned. This may require a large number of back copies and extra
259 /// PHI-defs. Only segments marked with overlapIntv will be overlapping.
260 SM_Partition,
261
262 /// SM_Size - Overlap intervals to minimize the number of inserted COPY
263 /// instructions. Copies to the complement interval are hoisted to their
264 /// common dominator, so only one COPY is required per value in the
265 /// complement interval. This also means that no extra PHI-defs need to be
266 /// inserted in the complement interval.
267 SM_Size,
268
269 /// SM_Speed - Overlap intervals to minimize the expected execution
270 /// frequency of the inserted copies. This is very similar to SM_Size, but
271 /// the complement interval may get some extra PHI-defs.
272 SM_Speed
273 };
274
275private:
276
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000277 /// Edit - The current parent register and new intervals created.
Jakob Stoklund Olesen815196c2011-03-02 23:31:50 +0000278 LiveRangeEdit *Edit;
Jakob Stoklund Olesen72911e42010-10-14 23:49:52 +0000279
Eric Christopherede62672011-02-03 06:18:29 +0000280 /// Index into Edit of the currently open interval.
281 /// The index 0 is used for the complement, so the first interval started by
282 /// openIntv will be 1.
283 unsigned OpenIdx;
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000284
Jakob Stoklund Oleseneecb2fb2011-09-12 16:49:21 +0000285 /// The current spill mode, selected by reset().
286 ComplementSpillMode SpillMode;
287
Eric Christopherede62672011-02-03 06:18:29 +0000288 typedef IntervalMap<SlotIndex, unsigned> RegAssignMap;
289
290 /// Allocator for the interval map. This will eventually be shared with
291 /// SlotIndexes and LiveIntervals.
292 RegAssignMap::Allocator Allocator;
293
294 /// RegAssign - Map of the assigned register indexes.
295 /// Edit.get(RegAssign.lookup(Idx)) is the register that should be live at
296 /// Idx.
297 RegAssignMap RegAssign;
298
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000299 typedef PointerIntPair<VNInfo*, 1> ValueForcePair;
300 typedef DenseMap<std::pair<unsigned, unsigned>, ValueForcePair> ValueMap;
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000301
302 /// Values - keep track of the mapping from parent values to values in the new
303 /// intervals. Given a pair (RegIdx, ParentVNI->id), Values contains:
304 ///
305 /// 1. No entry - the value is not mapped to Edit.get(RegIdx).
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000306 /// 2. (Null, false) - the value is mapped to multiple values in
307 /// Edit.get(RegIdx). Each value is represented by a minimal live range at
308 /// its def. The full live range can be inferred exactly from the range
309 /// of RegIdx in RegAssign.
310 /// 3. (Null, true). As above, but the ranges in RegAssign are too large, and
311 /// the live range must be recomputed using LiveRangeCalc::extend().
312 /// 4. (VNI, false) The value is mapped to a single new value.
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000313 /// The new value has no live ranges anywhere.
314 ValueMap Values;
315
Jakob Stoklund Olesen054984d2011-09-13 16:47:53 +0000316 /// LRCalc - Cache for computing live ranges and SSA update. Each instance
317 /// can only handle non-overlapping live ranges, so use a separate
318 /// LiveRangeCalc instance for the complement interval when in spill mode.
319 LiveRangeCalc LRCalc[2];
320
321 /// getLRCalc - Return the LRCalc to use for RegIdx. In spill mode, the
322 /// complement interval can overlap the other intervals, so it gets its own
323 /// LRCalc instance. When not in spill mode, all intervals can share one.
324 LiveRangeCalc &getLRCalc(unsigned RegIdx) {
325 return LRCalc[SpillMode != SM_Partition && RegIdx != 0];
326 }
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000327
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000328 /// Find a subrange corresponding to the lane mask @p LM in the live
329 /// interval @p LI. The interval @p LI is assumed to contain such a subrange.
330 /// This function is used to find corresponding subranges between the
331 /// original interval and the new intervals.
332 LiveInterval::SubRange &getSubRangeForMask(LaneBitmask LM, LiveInterval &LI);
333
334 /// Add a segment to the interval LI for the value number VNI. If LI has
335 /// subranges, corresponding segments will be added to them as well, but
336 /// with newly created value numbers. If Original is true, dead def will
337 /// only be added a subrange of LI if the corresponding subrange of the
338 /// original interval has a def at this index. Otherwise, all subranges
339 /// of LI will be updated.
340 void addDeadDef(LiveInterval &LI, VNInfo *VNI, bool Original);
341
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000342 /// defValue - define a value in RegIdx from ParentVNI at Idx.
343 /// Idx does not have to be ParentVNI->def, but it must be contained within
344 /// ParentVNI's live range in ParentLI. The new value is added to the value
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000345 /// map. The value being defined may either come from rematerialization
346 /// (or an inserted copy), or it may be coming from the original interval.
347 /// The parameter Original should be true in the latter case, otherwise
348 /// it should be false.
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000349 /// Return the new LI value.
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000350 VNInfo *defValue(unsigned RegIdx, const VNInfo *ParentVNI, SlotIndex Idx,
351 bool Original);
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000352
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000353 /// forceRecompute - Force the live range of ParentVNI in RegIdx to be
354 /// recomputed by LiveRangeCalc::extend regardless of the number of defs.
355 /// This is used for values whose live range doesn't match RegAssign exactly.
356 /// They could have rematerialized, or back-copies may have been moved.
357 void forceRecompute(unsigned RegIdx, const VNInfo *ParentVNI);
Jakob Stoklund Olesen4484f992011-09-13 18:05:29 +0000358
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000359 /// defFromParent - Define Reg from ParentVNI at UseIdx using either
360 /// rematerialization or a COPY from parent. Return the new value.
Eric Christopherede62672011-02-03 06:18:29 +0000361 VNInfo *defFromParent(unsigned RegIdx,
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000362 VNInfo *ParentVNI,
363 SlotIndex UseIdx,
364 MachineBasicBlock &MBB,
365 MachineBasicBlock::iterator I);
366
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000367 /// removeBackCopies - Remove the copy instructions that defines the values
368 /// in the vector in the complement interval.
369 void removeBackCopies(SmallVectorImpl<VNInfo*> &Copies);
370
Jakob Stoklund Olesena98af392011-09-14 16:45:39 +0000371 /// getShallowDominator - Returns the least busy dominator of MBB that is
372 /// also dominated by DefMBB. Busy is measured by loop depth.
373 MachineBasicBlock *findShallowDominator(MachineBasicBlock *MBB,
374 MachineBasicBlock *DefMBB);
375
Wei Mi9a16d652016-04-13 03:08:27 +0000376 /// Find out all the backCopies dominated by others.
377 void computeRedundantBackCopies(DenseSet<unsigned> &NotToHoistSet,
378 SmallVectorImpl<VNInfo *> &BackCopies);
379
380 /// Hoist back-copies to the complement interval. It tries to hoist all
381 /// the back-copies to one BB if it is beneficial, or else simply remove
Eric Christopher75d661a2016-05-04 21:45:36 +0000382 /// redundant backcopies dominated by others.
Wei Mi9a16d652016-04-13 03:08:27 +0000383 void hoistCopies();
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000384
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000385 /// transferValues - Transfer values to the new ranges.
386 /// Return true if any ranges were skipped.
387 bool transferValues();
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +0000388
Krzysztof Parzyszek73c8a9b2016-11-21 20:24:12 +0000389 /// Live range @p LR corresponding to the lane Mask @p LM has a live
390 /// PHI def at the beginning of block @p B. Extend the range @p LR of
391 /// all predecessor values that reach this def. If @p LR is a subrange,
392 /// the array @p Undefs is the set of all locations where it is undefined
393 /// via <def,read-undef> in other subranges for the same register.
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000394 void extendPHIRange(MachineBasicBlock &B, LiveRangeCalc &LRC,
Krzysztof Parzyszek73c8a9b2016-11-21 20:24:12 +0000395 LiveRange &LR, LaneBitmask LM,
396 ArrayRef<SlotIndex> Undefs);
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000397
Jakob Stoklund Olesen36482632011-03-02 23:05:16 +0000398 /// extendPHIKillRanges - Extend the ranges of all values killed by original
399 /// parent PHIDefs.
400 void extendPHIKillRanges();
401
Eric Christopherede62672011-02-03 06:18:29 +0000402 /// rewriteAssigned - Rewrite all uses of Edit.getReg() to assigned registers.
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +0000403 void rewriteAssigned(bool ExtendRanges);
Jakob Stoklund Olesen6f8bd422010-09-21 22:32:21 +0000404
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +0000405 /// deleteRematVictims - Delete defs that are dead after rematerializing.
406 void deleteRematVictims();
407
Matthias Braunf0b68d32017-03-17 00:41:39 +0000408 /// Add a copy instruction copying \p FromReg to \p ToReg before
409 /// \p InsertBefore. This can be invoked with a \p LaneMask which may make it
410 /// necessary to construct a sequence of copies to cover it exactly.
411 SlotIndex buildCopy(unsigned FromReg, unsigned ToReg, LaneBitmask LaneMask,
412 MachineBasicBlock &MBB, MachineBasicBlock::iterator InsertBefore,
413 bool Late, unsigned RegIdx);
414
415 SlotIndex buildSingleSubRegCopy(unsigned FromReg, unsigned ToReg,
416 MachineBasicBlock &MB, MachineBasicBlock::iterator InsertBefore,
417 unsigned SubIdx, LiveInterval &DestLI, bool Late, SlotIndex PrevCopy);
418
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000419public:
420 /// Create a new SplitEditor for editing the LiveInterval analyzed by SA.
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000421 /// Newly created intervals will be appended to newIntervals.
Wei Mic0223702016-07-08 21:08:09 +0000422 SplitEditor(SplitAnalysis &SA, AliasAnalysis &AA, LiveIntervals&,
423 VirtRegMap&, MachineDominatorTree&,
424 MachineBlockFrequencyInfo &);
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000425
Jakob Stoklund Olesenc9601982011-03-03 01:29:13 +0000426 /// reset - Prepare for a new split.
Jakob Stoklund Oleseneecb2fb2011-09-12 16:49:21 +0000427 void reset(LiveRangeEdit&, ComplementSpillMode = SM_Partition);
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000428
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000429 /// Create a new virtual register and live interval.
Jakob Stoklund Olesen0840f502011-04-12 18:11:31 +0000430 /// Return the interval index, starting from 1. Interval index 0 is the
431 /// implicit complement interval.
432 unsigned openIntv();
433
434 /// currentIntv - Return the current interval index.
435 unsigned currentIntv() const { return OpenIdx; }
436
437 /// selectIntv - Select a previously opened interval index.
438 void selectIntv(unsigned Idx);
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000439
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000440 /// enterIntvBefore - Enter the open interval before the instruction at Idx.
441 /// If the parent interval is not live before Idx, a COPY is not inserted.
442 /// Return the beginning of the new live range.
443 SlotIndex enterIntvBefore(SlotIndex Idx);
Jakob Stoklund Olesen622848b2010-08-12 17:07:14 +0000444
Jakob Stoklund Olesenadc6a4c2011-06-30 01:30:39 +0000445 /// enterIntvAfter - Enter the open interval after the instruction at Idx.
446 /// Return the beginning of the new live range.
447 SlotIndex enterIntvAfter(SlotIndex Idx);
448
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000449 /// enterIntvAtEnd - Enter the open interval at the end of MBB.
Eric Christopher650c8f22014-05-20 17:11:11 +0000450 /// Use the open interval from the inserted copy to the MBB end.
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000451 /// Return the beginning of the new live range.
452 SlotIndex enterIntvAtEnd(MachineBasicBlock &MBB);
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000453
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000454 /// useIntv - indicate that all instructions in MBB should use OpenLI.
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000455 void useIntv(const MachineBasicBlock &MBB);
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000456
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000457 /// useIntv - indicate that all instructions in range should use OpenLI.
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000458 void useIntv(SlotIndex Start, SlotIndex End);
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000459
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000460 /// leaveIntvAfter - Leave the open interval after the instruction at Idx.
461 /// Return the end of the live range.
462 SlotIndex leaveIntvAfter(SlotIndex Idx);
Jakob Stoklund Olesen622848b2010-08-12 17:07:14 +0000463
Jakob Stoklund Olesen7cb57b32011-02-09 23:30:25 +0000464 /// leaveIntvBefore - Leave the open interval before the instruction at Idx.
465 /// Return the end of the live range.
466 SlotIndex leaveIntvBefore(SlotIndex Idx);
467
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000468 /// leaveIntvAtTop - Leave the interval at the top of MBB.
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000469 /// Add liveness from the MBB top to the copy.
470 /// Return the end of the live range.
471 SlotIndex leaveIntvAtTop(MachineBasicBlock &MBB);
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000472
Jakob Stoklund Olesen17499352011-02-08 18:50:21 +0000473 /// overlapIntv - Indicate that all instructions in range should use the open
474 /// interval, but also let the complement interval be live.
475 ///
476 /// This doubles the register pressure, but is sometimes required to deal with
477 /// register uses after the last valid split point.
478 ///
479 /// The Start index should be a return value from a leaveIntv* call, and End
480 /// should be in the same basic block. The parent interval must have the same
481 /// value across the range.
482 ///
483 void overlapIntv(SlotIndex Start, SlotIndex End);
484
Jakob Stoklund Olesen959fcc62010-10-08 23:42:21 +0000485 /// finish - after all the new live ranges have been created, compute the
486 /// remaining live range, and rewrite instructions to use the new registers.
Jakob Stoklund Olesen6a663b82011-04-21 18:38:15 +0000487 /// @param LRMap When not null, this vector will map each live range in Edit
488 /// back to the indices returned by openIntv.
489 /// There may be extra indices created by dead code elimination.
Craig Topperada08572014-04-16 04:21:27 +0000490 void finish(SmallVectorImpl<unsigned> *LRMap = nullptr);
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000491
Jim Grosbachbfe3a9c2015-05-02 00:44:07 +0000492 /// dump - print the current interval mapping to dbgs().
Eric Christopherede62672011-02-03 06:18:29 +0000493 void dump() const;
494
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000495 // ===--- High level methods ---===
496
Jakob Stoklund Olesenc70b6972011-04-12 19:32:53 +0000497 /// splitSingleBlock - Split CurLI into a separate live interval around the
498 /// uses in a single block. This is intended to be used as part of a larger
499 /// split, and doesn't call finish().
500 void splitSingleBlock(const SplitAnalysis::BlockInfo &BI);
501
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +0000502 /// splitLiveThroughBlock - Split CurLI in the given block such that it
503 /// enters the block in IntvIn and leaves it in IntvOut. There may be uses in
504 /// the block, but they will be ignored when placing split points.
505 ///
506 /// @param MBBNum Block number.
507 /// @param IntvIn Interval index entering the block.
508 /// @param LeaveBefore When set, leave IntvIn before this point.
509 /// @param IntvOut Interval index leaving the block.
510 /// @param EnterAfter When set, enter IntvOut after this point.
511 void splitLiveThroughBlock(unsigned MBBNum,
512 unsigned IntvIn, SlotIndex LeaveBefore,
513 unsigned IntvOut, SlotIndex EnterAfter);
514
515 /// splitRegInBlock - Split CurLI in the given block such that it enters the
516 /// block in IntvIn and leaves it on the stack (or not at all). Split points
517 /// are placed in a way that avoids putting uses in the stack interval. This
518 /// may require creating a local interval when there is interference.
519 ///
520 /// @param BI Block descriptor.
521 /// @param IntvIn Interval index entering the block. Not 0.
522 /// @param LeaveBefore When set, leave IntvIn before this point.
523 void splitRegInBlock(const SplitAnalysis::BlockInfo &BI,
524 unsigned IntvIn, SlotIndex LeaveBefore);
525
526 /// splitRegOutBlock - Split CurLI in the given block such that it enters the
527 /// block on the stack (or isn't live-in at all) and leaves it in IntvOut.
528 /// Split points are placed to avoid interference and such that the uses are
529 /// not in the stack interval. This may require creating a local interval
530 /// when there is interference.
531 ///
532 /// @param BI Block descriptor.
533 /// @param IntvOut Interval index leaving the block.
534 /// @param EnterAfter When set, enter IntvOut after this point.
535 void splitRegOutBlock(const SplitAnalysis::BlockInfo &BI,
536 unsigned IntvOut, SlotIndex EnterAfter);
Jakob Stoklund Olesend1191ee2010-08-13 21:18:48 +0000537};
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +0000538
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000539}
Sebastian Redlb8a62aa2011-04-24 15:46:51 +0000540
541#endif