blob: c0608893d4e5340b1dfe9bddfd1dbb333ce63cc8 [file] [log] [blame]
Eugene Zelenko900b6332017-08-29 22:32:07 +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"
Eugene Zelenko900b6332017-08-29 22:32:07 +000020#include "llvm/ADT/BitVector.h"
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +000021#include "llvm/ADT/DenseMap.h"
Wei Mi9a16d652016-04-13 03:08:27 +000022#include "llvm/ADT/DenseSet.h"
Eric Christopherede62672011-02-03 06:18:29 +000023#include "llvm/ADT/IntervalMap.h"
Eugene Zelenko900b6332017-08-29 22:32:07 +000024#include "llvm/ADT/PointerIntPair.h"
Jakob Stoklund Olesen2530cd22010-12-21 01:50:21 +000025#include "llvm/ADT/SmallPtrSet.h"
Eugene Zelenko900b6332017-08-29 22:32:07 +000026#include "llvm/ADT/SmallVector.h"
27#include "llvm/CodeGen/LiveInterval.h"
28#include "llvm/CodeGen/MachineBasicBlock.h"
29#include "llvm/CodeGen/MachineFunction.h"
30#include "llvm/CodeGen/SlotIndexes.h"
31#include "llvm/MC/LaneBitmask.h"
32#include "llvm/Support/Compiler.h"
33#include <utility>
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +000034
35namespace llvm {
36
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +000037class LiveIntervals;
Jakob Stoklund Olesen72911e42010-10-14 23:49:52 +000038class LiveRangeEdit;
Benjamin Kramere2a1d892013-06-17 19:00:36 +000039class MachineBlockFrequencyInfo;
Eugene Zelenko900b6332017-08-29 22:32:07 +000040class MachineDominatorTree;
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +000041class MachineLoopInfo;
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +000042class MachineRegisterInfo;
Jakob Stoklund Olesened4075c2010-07-20 21:46:58 +000043class TargetInstrInfo;
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +000044class TargetRegisterInfo;
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +000045class VirtRegMap;
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +000046
Wei Mi35ee9332016-05-11 22:28:29 +000047/// Determines the latest safe point in a block in which we can insert a split,
48/// spill or other instruction related with CurLI.
49class LLVM_LIBRARY_VISIBILITY InsertPointAnalysis {
50private:
51 const LiveIntervals &LIS;
52
Wei Mi35ee9332016-05-11 22:28:29 +000053 /// Last legal insert point in each basic block in the current function.
54 /// The first entry is the first terminator, the second entry is the
55 /// last valid point to insert a split or spill for a variable that is
56 /// live into a landing pad successor.
57 SmallVector<std::pair<SlotIndex, SlotIndex>, 8> LastInsertPoint;
58
Wei Mif3c8f532016-05-23 19:39:19 +000059 SlotIndex computeLastInsertPoint(const LiveInterval &CurLI,
60 const MachineBasicBlock &MBB);
Wei Mi35ee9332016-05-11 22:28:29 +000061
62public:
63 InsertPointAnalysis(const LiveIntervals &lis, unsigned BBNum);
64
Wei Mif3c8f532016-05-23 19:39:19 +000065 /// Return the base index of the last valid insert point for \pCurLI in \pMBB.
66 SlotIndex getLastInsertPoint(const LiveInterval &CurLI,
67 const MachineBasicBlock &MBB) {
Wei Mi35ee9332016-05-11 22:28:29 +000068 unsigned Num = MBB.getNumber();
69 // Inline the common simple case.
70 if (LastInsertPoint[Num].first.isValid() &&
71 !LastInsertPoint[Num].second.isValid())
72 return LastInsertPoint[Num].first;
Wei Mif3c8f532016-05-23 19:39:19 +000073 return computeLastInsertPoint(CurLI, MBB);
Wei Mi35ee9332016-05-11 22:28:29 +000074 }
75
Wei Mif3c8f532016-05-23 19:39:19 +000076 /// Returns the last insert point as an iterator for \pCurLI in \pMBB.
77 MachineBasicBlock::iterator getLastInsertPointIter(const LiveInterval &CurLI,
78 MachineBasicBlock &MBB);
Wei Mi35ee9332016-05-11 22:28:29 +000079};
80
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +000081/// SplitAnalysis - Analyze a LiveInterval, looking for live range splitting
82/// opportunities.
Benjamin Kramerf4c20252015-07-01 14:47:39 +000083class LLVM_LIBRARY_VISIBILITY SplitAnalysis {
Jakob Stoklund Olesen284c2db2010-08-10 17:07:22 +000084public:
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +000085 const MachineFunction &MF;
Jakob Stoklund Olesenf1a60a62011-02-19 00:53:42 +000086 const VirtRegMap &VRM;
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +000087 const LiveIntervals &LIS;
88 const MachineLoopInfo &Loops;
89 const TargetInstrInfo &TII;
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +000090
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +000091 /// Additional information about basic blocks where the current variable is
92 /// live. Such a block will look like one of these templates:
93 ///
94 /// 1. | o---x | Internal to block. Variable is only live in this block.
95 /// 2. |---x | Live-in, kill.
96 /// 3. | o---| Def, live-out.
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +000097 /// 4. |---x o---| Live-in, kill, def, live-out. Counted by NumGapBlocks.
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +000098 /// 5. |---o---o---| Live-through with uses or defs.
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +000099 /// 6. |-----------| Live-through without uses. Counted by NumThroughBlocks.
100 ///
101 /// Two BlockInfo entries are created for template 4. One for the live-in
102 /// segment, and one for the live-out segment. These entries look as if the
103 /// block were split in the middle where the live range isn't live.
104 ///
105 /// Live-through blocks without any uses don't get BlockInfo entries. They
106 /// are simply listed in ThroughBlocks instead.
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000107 ///
108 struct BlockInfo {
109 MachineBasicBlock *MBB;
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +0000110 SlotIndex FirstInstr; ///< First instr accessing current reg.
111 SlotIndex LastInstr; ///< Last instr accessing current reg.
Jakob Stoklund Olesenae8027c2011-08-02 22:37:22 +0000112 SlotIndex FirstDef; ///< First non-phi valno->def, or SlotIndex().
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000113 bool LiveIn; ///< Current reg is live in.
114 bool LiveOut; ///< Current reg is live out.
Jakob Stoklund Olesenadc6a4c2011-06-30 01:30:39 +0000115
116 /// isOneInstr - Returns true when this BlockInfo describes a single
117 /// instruction.
118 bool isOneInstr() const {
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +0000119 return SlotIndex::isSameInstr(FirstInstr, LastInstr);
Jakob Stoklund Olesenadc6a4c2011-06-30 01:30:39 +0000120 }
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000121 };
122
Jakob Stoklund Olesen622848b2010-08-12 17:07:14 +0000123private:
124 // Current live interval.
Eugene Zelenko900b6332017-08-29 22:32:07 +0000125 const LiveInterval *CurLI = nullptr;
Jakob Stoklund Olesen622848b2010-08-12 17:07:14 +0000126
Wei Mi35ee9332016-05-11 22:28:29 +0000127 /// Insert Point Analysis.
128 InsertPointAnalysis IPA;
129
Jakob Stoklund Olesen994fed62012-01-12 17:53:44 +0000130 // Sorted slot indexes of using instructions.
131 SmallVector<SlotIndex, 8> UseSlots;
132
Jakob Stoklund Olesenbf91c4e2011-04-06 03:57:00 +0000133 /// UseBlocks - Blocks where CurLI has uses.
134 SmallVector<BlockInfo, 8> UseBlocks;
135
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000136 /// NumGapBlocks - Number of duplicate entries in UseBlocks for blocks where
137 /// the live range has a gap.
138 unsigned NumGapBlocks;
139
Jakob Stoklund Olesenbf91c4e2011-04-06 03:57:00 +0000140 /// ThroughBlocks - Block numbers where CurLI is live through without uses.
Jakob Stoklund Olesened47ed42011-04-09 02:59:09 +0000141 BitVector ThroughBlocks;
142
143 /// NumThroughBlocks - Number of live-through blocks.
144 unsigned NumThroughBlocks;
Jakob Stoklund Olesenbf91c4e2011-04-06 03:57:00 +0000145
Jakob Stoklund Oleseneaa6ed12011-05-03 20:42:13 +0000146 /// DidRepairRange - analyze was forced to shrinkToUses().
147 bool DidRepairRange;
148
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000149 // Sumarize statistics by counting instructions using CurLI.
Jakob Stoklund Olesenff095502010-07-20 16:12:37 +0000150 void analyzeUses();
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +0000151
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000152 /// calcLiveBlockInfo - Compute per-block information about CurLI.
Jakob Stoklund Olesen27e0a4a2011-03-05 18:33:49 +0000153 bool calcLiveBlockInfo();
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000154
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +0000155public:
Jakob Stoklund Olesenf1a60a62011-02-19 00:53:42 +0000156 SplitAnalysis(const VirtRegMap &vrm, const LiveIntervals &lis,
Jakob Stoklund Olesen0fef9dd2010-07-20 23:50:15 +0000157 const MachineLoopInfo &mli);
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +0000158
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000159 /// analyze - set CurLI to the specified interval, and analyze how it may be
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +0000160 /// split.
161 void analyze(const LiveInterval *li);
162
Jakob Stoklund Oleseneaa6ed12011-05-03 20:42:13 +0000163 /// didRepairRange() - Returns true if CurLI was invalid and has been repaired
164 /// by analyze(). This really shouldn't happen, but sometimes the coalescer
165 /// can create live ranges that end in mid-air.
166 bool didRepairRange() const { return DidRepairRange; }
167
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +0000168 /// clear - clear all data structures so SplitAnalysis is ready to analyze a
169 /// new interval.
170 void clear();
171
Jakob Stoklund Olesen93c87362011-02-17 19:13:53 +0000172 /// getParent - Return the last analyzed interval.
173 const LiveInterval &getParent() const { return *CurLI; }
174
Jakob Stoklund Olesen60a26a62011-02-21 23:09:46 +0000175 /// isOriginalEndpoint - Return true if the original live range was killed or
176 /// (re-)defined at Idx. Idx should be the 'def' slot for a normal kill/def,
177 /// and 'use' for an early-clobber def.
178 /// This can be used to recognize code inserted by earlier live range
179 /// splitting.
180 bool isOriginalEndpoint(SlotIndex Idx) const;
181
Jakob Stoklund Olesen994fed62012-01-12 17:53:44 +0000182 /// getUseSlots - Return an array of SlotIndexes of instructions using CurLI.
183 /// This include both use and def operands, at most one entry per instruction.
184 ArrayRef<SlotIndex> getUseSlots() const { return UseSlots; }
185
Jakob Stoklund Olesenbf91c4e2011-04-06 03:57:00 +0000186 /// getUseBlocks - Return an array of BlockInfo objects for the basic blocks
187 /// where CurLI has uses.
Jakob Stoklund Olesen5cc91b22011-05-28 02:32:57 +0000188 ArrayRef<BlockInfo> getUseBlocks() const { return UseBlocks; }
Jakob Stoklund Olesenbf91c4e2011-04-06 03:57:00 +0000189
Jakob Stoklund Olesened47ed42011-04-09 02:59:09 +0000190 /// getNumThroughBlocks - Return the number of through blocks.
191 unsigned getNumThroughBlocks() const { return NumThroughBlocks; }
192
193 /// isThroughBlock - Return true if CurLI is live through MBB without uses.
194 bool isThroughBlock(unsigned MBB) const { return ThroughBlocks.test(MBB); }
Jakob Stoklund Olesenbf91c4e2011-04-06 03:57:00 +0000195
Jakob Stoklund Olesenc49df2c2011-04-12 21:30:53 +0000196 /// getThroughBlocks - Return the set of through blocks.
197 const BitVector &getThroughBlocks() const { return ThroughBlocks; }
198
Jakob Stoklund Olesen5cc91b22011-05-28 02:32:57 +0000199 /// getNumLiveBlocks - Return the number of blocks where CurLI is live.
200 unsigned getNumLiveBlocks() const {
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000201 return getUseBlocks().size() - NumGapBlocks + getNumThroughBlocks();
Jakob Stoklund Olesen5cc91b22011-05-28 02:32:57 +0000202 }
203
204 /// countLiveBlocks - Return the number of blocks where li is live. This is
205 /// guaranteed to return the same number as getNumLiveBlocks() after calling
206 /// analyze(li).
Jakob Stoklund Oleseneef23272011-04-26 22:33:12 +0000207 unsigned countLiveBlocks(const LiveInterval *li) const;
208
Eugene Zelenko900b6332017-08-29 22:32:07 +0000209 using BlockPtrSet = SmallPtrSet<const MachineBasicBlock *, 16>;
Jakob Stoklund Olesened4075c2010-07-20 21:46:58 +0000210
Jakob Stoklund Olesen8627ea92011-08-05 22:20:45 +0000211 /// shouldSplitSingleBlock - Returns true if it would help to create a local
212 /// live range for the instructions in BI. There is normally no benefit to
213 /// creating a live range for a single instruction, but it does enable
214 /// register class inflation if the instruction has a restricted register
215 /// class.
216 ///
217 /// @param BI The block to be isolated.
218 /// @param SingleInstrs True when single instructions should be isolated.
219 bool shouldSplitSingleBlock(const BlockInfo &BI, bool SingleInstrs) const;
Wei Mi35ee9332016-05-11 22:28:29 +0000220
221 SlotIndex getLastSplitPoint(unsigned Num) {
Wei Mif3c8f532016-05-23 19:39:19 +0000222 return IPA.getLastInsertPoint(*CurLI, *MF.getBlockNumbered(Num));
Wei Mi35ee9332016-05-11 22:28:29 +0000223 }
224
225 MachineBasicBlock::iterator getLastSplitPointIter(MachineBasicBlock *BB) {
Wei Mif3c8f532016-05-23 19:39:19 +0000226 return IPA.getLastInsertPointIter(*CurLI, *BB);
Wei Mi35ee9332016-05-11 22:28:29 +0000227 }
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +0000228};
229
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000230/// SplitEditor - Edit machine code and LiveIntervals for live range
231/// splitting.
232///
Jakob Stoklund Olesen45e07c82010-08-06 22:17:33 +0000233/// - Create a SplitEditor from a SplitAnalysis.
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000234/// - Start a new live interval with openIntv.
235/// - Mark the places where the new interval is entered using enterIntv*
236/// - Mark the ranges where the new interval is used with useIntv*
237/// - Mark the places where the interval is exited with exitIntv*.
238/// - Finish the current interval with closeIntv and repeat from 2.
Jakob Stoklund Olesen959fcc62010-10-08 23:42:21 +0000239/// - Rewrite instructions with finish().
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000240///
Benjamin Kramerf4c20252015-07-01 14:47:39 +0000241class LLVM_LIBRARY_VISIBILITY SplitEditor {
Jakob Stoklund Olesen04aff702011-02-19 00:42:33 +0000242 SplitAnalysis &SA;
Wei Mic0223702016-07-08 21:08:09 +0000243 AliasAnalysis &AA;
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000244 LiveIntervals &LIS;
245 VirtRegMap &VRM;
246 MachineRegisterInfo &MRI;
Eric Christopherede62672011-02-03 06:18:29 +0000247 MachineDominatorTree &MDT;
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000248 const TargetInstrInfo &TII;
249 const TargetRegisterInfo &TRI;
Benjamin Kramere2a1d892013-06-17 19:00:36 +0000250 const MachineBlockFrequencyInfo &MBFI;
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000251
Jakob Stoklund Oleseneecb2fb2011-09-12 16:49:21 +0000252public:
Jakob Stoklund Oleseneecb2fb2011-09-12 16:49:21 +0000253 /// ComplementSpillMode - Select how the complement live range should be
254 /// created. SplitEditor automatically creates interval 0 to contain
255 /// anything that isn't added to another interval. This complement interval
256 /// can get quite complicated, and it can sometimes be an advantage to allow
257 /// it to overlap the other intervals. If it is going to spill anyway, no
258 /// registers are wasted by keeping a value in two places at the same time.
259 enum ComplementSpillMode {
260 /// SM_Partition(Default) - Try to create the complement interval so it
261 /// doesn't overlap any other intervals, and the original interval is
262 /// partitioned. This may require a large number of back copies and extra
263 /// PHI-defs. Only segments marked with overlapIntv will be overlapping.
264 SM_Partition,
265
266 /// SM_Size - Overlap intervals to minimize the number of inserted COPY
267 /// instructions. Copies to the complement interval are hoisted to their
268 /// common dominator, so only one COPY is required per value in the
269 /// complement interval. This also means that no extra PHI-defs need to be
270 /// inserted in the complement interval.
271 SM_Size,
272
273 /// SM_Speed - Overlap intervals to minimize the expected execution
274 /// frequency of the inserted copies. This is very similar to SM_Size, but
275 /// the complement interval may get some extra PHI-defs.
276 SM_Speed
277 };
278
279private:
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000280 /// Edit - The current parent register and new intervals created.
Eugene Zelenko900b6332017-08-29 22:32:07 +0000281 LiveRangeEdit *Edit = nullptr;
Jakob Stoklund Olesen72911e42010-10-14 23:49:52 +0000282
Eric Christopherede62672011-02-03 06:18:29 +0000283 /// Index into Edit of the currently open interval.
284 /// The index 0 is used for the complement, so the first interval started by
285 /// openIntv will be 1.
Eugene Zelenko900b6332017-08-29 22:32:07 +0000286 unsigned OpenIdx = 0;
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000287
Jakob Stoklund Oleseneecb2fb2011-09-12 16:49:21 +0000288 /// The current spill mode, selected by reset().
Eugene Zelenko900b6332017-08-29 22:32:07 +0000289 ComplementSpillMode SpillMode = SM_Partition;
Jakob Stoklund Oleseneecb2fb2011-09-12 16:49:21 +0000290
Eugene Zelenko900b6332017-08-29 22:32:07 +0000291 using RegAssignMap = IntervalMap<SlotIndex, unsigned>;
Eric Christopherede62672011-02-03 06:18:29 +0000292
293 /// Allocator for the interval map. This will eventually be shared with
294 /// SlotIndexes and LiveIntervals.
295 RegAssignMap::Allocator Allocator;
296
297 /// RegAssign - Map of the assigned register indexes.
298 /// Edit.get(RegAssign.lookup(Idx)) is the register that should be live at
299 /// Idx.
300 RegAssignMap RegAssign;
301
Eugene Zelenko900b6332017-08-29 22:32:07 +0000302 using ValueForcePair = PointerIntPair<VNInfo *, 1>;
303 using ValueMap = DenseMap<std::pair<unsigned, unsigned>, ValueForcePair>;
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000304
305 /// Values - keep track of the mapping from parent values to values in the new
306 /// intervals. Given a pair (RegIdx, ParentVNI->id), Values contains:
307 ///
308 /// 1. No entry - the value is not mapped to Edit.get(RegIdx).
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000309 /// 2. (Null, false) - the value is mapped to multiple values in
310 /// Edit.get(RegIdx). Each value is represented by a minimal live range at
311 /// its def. The full live range can be inferred exactly from the range
312 /// of RegIdx in RegAssign.
313 /// 3. (Null, true). As above, but the ranges in RegAssign are too large, and
314 /// the live range must be recomputed using LiveRangeCalc::extend().
315 /// 4. (VNI, false) The value is mapped to a single new value.
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000316 /// The new value has no live ranges anywhere.
317 ValueMap Values;
318
Jakob Stoklund Olesen054984d2011-09-13 16:47:53 +0000319 /// LRCalc - Cache for computing live ranges and SSA update. Each instance
320 /// can only handle non-overlapping live ranges, so use a separate
321 /// LiveRangeCalc instance for the complement interval when in spill mode.
322 LiveRangeCalc LRCalc[2];
323
324 /// getLRCalc - Return the LRCalc to use for RegIdx. In spill mode, the
325 /// complement interval can overlap the other intervals, so it gets its own
326 /// LRCalc instance. When not in spill mode, all intervals can share one.
327 LiveRangeCalc &getLRCalc(unsigned RegIdx) {
328 return LRCalc[SpillMode != SM_Partition && RegIdx != 0];
329 }
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000330
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000331 /// Find a subrange corresponding to the lane mask @p LM in the live
332 /// interval @p LI. The interval @p LI is assumed to contain such a subrange.
333 /// This function is used to find corresponding subranges between the
334 /// original interval and the new intervals.
335 LiveInterval::SubRange &getSubRangeForMask(LaneBitmask LM, LiveInterval &LI);
336
337 /// Add a segment to the interval LI for the value number VNI. If LI has
338 /// subranges, corresponding segments will be added to them as well, but
339 /// with newly created value numbers. If Original is true, dead def will
340 /// only be added a subrange of LI if the corresponding subrange of the
341 /// original interval has a def at this index. Otherwise, all subranges
342 /// of LI will be updated.
343 void addDeadDef(LiveInterval &LI, VNInfo *VNI, bool Original);
344
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000345 /// defValue - define a value in RegIdx from ParentVNI at Idx.
346 /// Idx does not have to be ParentVNI->def, but it must be contained within
347 /// ParentVNI's live range in ParentLI. The new value is added to the value
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000348 /// map. The value being defined may either come from rematerialization
349 /// (or an inserted copy), or it may be coming from the original interval.
350 /// The parameter Original should be true in the latter case, otherwise
351 /// it should be false.
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000352 /// Return the new LI value.
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000353 VNInfo *defValue(unsigned RegIdx, const VNInfo *ParentVNI, SlotIndex Idx,
354 bool Original);
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000355
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000356 /// forceRecompute - Force the live range of ParentVNI in RegIdx to be
357 /// recomputed by LiveRangeCalc::extend regardless of the number of defs.
358 /// This is used for values whose live range doesn't match RegAssign exactly.
359 /// They could have rematerialized, or back-copies may have been moved.
360 void forceRecompute(unsigned RegIdx, const VNInfo *ParentVNI);
Jakob Stoklund Olesen4484f992011-09-13 18:05:29 +0000361
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000362 /// defFromParent - Define Reg from ParentVNI at UseIdx using either
363 /// rematerialization or a COPY from parent. Return the new value.
Eric Christopherede62672011-02-03 06:18:29 +0000364 VNInfo *defFromParent(unsigned RegIdx,
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000365 VNInfo *ParentVNI,
366 SlotIndex UseIdx,
367 MachineBasicBlock &MBB,
368 MachineBasicBlock::iterator I);
369
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000370 /// removeBackCopies - Remove the copy instructions that defines the values
371 /// in the vector in the complement interval.
372 void removeBackCopies(SmallVectorImpl<VNInfo*> &Copies);
373
Jakob Stoklund Olesena98af392011-09-14 16:45:39 +0000374 /// getShallowDominator - Returns the least busy dominator of MBB that is
375 /// also dominated by DefMBB. Busy is measured by loop depth.
376 MachineBasicBlock *findShallowDominator(MachineBasicBlock *MBB,
377 MachineBasicBlock *DefMBB);
378
Wei Mi9a16d652016-04-13 03:08:27 +0000379 /// Find out all the backCopies dominated by others.
380 void computeRedundantBackCopies(DenseSet<unsigned> &NotToHoistSet,
381 SmallVectorImpl<VNInfo *> &BackCopies);
382
383 /// Hoist back-copies to the complement interval. It tries to hoist all
384 /// the back-copies to one BB if it is beneficial, or else simply remove
Eric Christopher75d661a2016-05-04 21:45:36 +0000385 /// redundant backcopies dominated by others.
Wei Mi9a16d652016-04-13 03:08:27 +0000386 void hoistCopies();
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000387
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000388 /// transferValues - Transfer values to the new ranges.
389 /// Return true if any ranges were skipped.
390 bool transferValues();
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +0000391
Krzysztof Parzyszek73c8a9b2016-11-21 20:24:12 +0000392 /// Live range @p LR corresponding to the lane Mask @p LM has a live
393 /// PHI def at the beginning of block @p B. Extend the range @p LR of
394 /// all predecessor values that reach this def. If @p LR is a subrange,
395 /// the array @p Undefs is the set of all locations where it is undefined
396 /// via <def,read-undef> in other subranges for the same register.
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000397 void extendPHIRange(MachineBasicBlock &B, LiveRangeCalc &LRC,
Krzysztof Parzyszek73c8a9b2016-11-21 20:24:12 +0000398 LiveRange &LR, LaneBitmask LM,
399 ArrayRef<SlotIndex> Undefs);
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000400
Jakob Stoklund Olesen36482632011-03-02 23:05:16 +0000401 /// extendPHIKillRanges - Extend the ranges of all values killed by original
402 /// parent PHIDefs.
403 void extendPHIKillRanges();
404
Eric Christopherede62672011-02-03 06:18:29 +0000405 /// rewriteAssigned - Rewrite all uses of Edit.getReg() to assigned registers.
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +0000406 void rewriteAssigned(bool ExtendRanges);
Jakob Stoklund Olesen6f8bd422010-09-21 22:32:21 +0000407
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +0000408 /// deleteRematVictims - Delete defs that are dead after rematerializing.
409 void deleteRematVictims();
410
Matthias Braunf0b68d32017-03-17 00:41:39 +0000411 /// Add a copy instruction copying \p FromReg to \p ToReg before
412 /// \p InsertBefore. This can be invoked with a \p LaneMask which may make it
413 /// necessary to construct a sequence of copies to cover it exactly.
414 SlotIndex buildCopy(unsigned FromReg, unsigned ToReg, LaneBitmask LaneMask,
415 MachineBasicBlock &MBB, MachineBasicBlock::iterator InsertBefore,
416 bool Late, unsigned RegIdx);
417
418 SlotIndex buildSingleSubRegCopy(unsigned FromReg, unsigned ToReg,
419 MachineBasicBlock &MB, MachineBasicBlock::iterator InsertBefore,
420 unsigned SubIdx, LiveInterval &DestLI, bool Late, SlotIndex PrevCopy);
421
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000422public:
423 /// Create a new SplitEditor for editing the LiveInterval analyzed by SA.
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000424 /// Newly created intervals will be appended to newIntervals.
Eugene Zelenko900b6332017-08-29 22:32:07 +0000425 SplitEditor(SplitAnalysis &sa, AliasAnalysis &aa, LiveIntervals &lis,
426 VirtRegMap &vrm, MachineDominatorTree &mdt,
427 MachineBlockFrequencyInfo &mbfi);
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000428
Jakob Stoklund Olesenc9601982011-03-03 01:29:13 +0000429 /// reset - Prepare for a new split.
Jakob Stoklund Oleseneecb2fb2011-09-12 16:49:21 +0000430 void reset(LiveRangeEdit&, ComplementSpillMode = SM_Partition);
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000431
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000432 /// Create a new virtual register and live interval.
Jakob Stoklund Olesen0840f502011-04-12 18:11:31 +0000433 /// Return the interval index, starting from 1. Interval index 0 is the
434 /// implicit complement interval.
435 unsigned openIntv();
436
437 /// currentIntv - Return the current interval index.
438 unsigned currentIntv() const { return OpenIdx; }
439
440 /// selectIntv - Select a previously opened interval index.
441 void selectIntv(unsigned Idx);
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000442
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000443 /// enterIntvBefore - Enter the open interval before the instruction at Idx.
444 /// If the parent interval is not live before Idx, a COPY is not inserted.
445 /// Return the beginning of the new live range.
446 SlotIndex enterIntvBefore(SlotIndex Idx);
Jakob Stoklund Olesen622848b2010-08-12 17:07:14 +0000447
Jakob Stoklund Olesenadc6a4c2011-06-30 01:30:39 +0000448 /// enterIntvAfter - Enter the open interval after the instruction at Idx.
449 /// Return the beginning of the new live range.
450 SlotIndex enterIntvAfter(SlotIndex Idx);
451
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000452 /// enterIntvAtEnd - Enter the open interval at the end of MBB.
Eric Christopher650c8f22014-05-20 17:11:11 +0000453 /// Use the open interval from the inserted copy to the MBB end.
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000454 /// Return the beginning of the new live range.
455 SlotIndex enterIntvAtEnd(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 MBB should use OpenLI.
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000458 void useIntv(const MachineBasicBlock &MBB);
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000459
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000460 /// useIntv - indicate that all instructions in range should use OpenLI.
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000461 void useIntv(SlotIndex Start, SlotIndex End);
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000462
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000463 /// leaveIntvAfter - Leave the open interval after the instruction at Idx.
464 /// Return the end of the live range.
465 SlotIndex leaveIntvAfter(SlotIndex Idx);
Jakob Stoklund Olesen622848b2010-08-12 17:07:14 +0000466
Jakob Stoklund Olesen7cb57b32011-02-09 23:30:25 +0000467 /// leaveIntvBefore - Leave the open interval before the instruction at Idx.
468 /// Return the end of the live range.
469 SlotIndex leaveIntvBefore(SlotIndex Idx);
470
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000471 /// leaveIntvAtTop - Leave the interval at the top of MBB.
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000472 /// Add liveness from the MBB top to the copy.
473 /// Return the end of the live range.
474 SlotIndex leaveIntvAtTop(MachineBasicBlock &MBB);
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000475
Jakob Stoklund Olesen17499352011-02-08 18:50:21 +0000476 /// overlapIntv - Indicate that all instructions in range should use the open
477 /// interval, but also let the complement interval be live.
478 ///
479 /// This doubles the register pressure, but is sometimes required to deal with
480 /// register uses after the last valid split point.
481 ///
482 /// The Start index should be a return value from a leaveIntv* call, and End
483 /// should be in the same basic block. The parent interval must have the same
484 /// value across the range.
485 ///
486 void overlapIntv(SlotIndex Start, SlotIndex End);
487
Jakob Stoklund Olesen959fcc62010-10-08 23:42:21 +0000488 /// finish - after all the new live ranges have been created, compute the
489 /// remaining live range, and rewrite instructions to use the new registers.
Jakob Stoklund Olesen6a663b82011-04-21 18:38:15 +0000490 /// @param LRMap When not null, this vector will map each live range in Edit
491 /// back to the indices returned by openIntv.
492 /// There may be extra indices created by dead code elimination.
Craig Topperada08572014-04-16 04:21:27 +0000493 void finish(SmallVectorImpl<unsigned> *LRMap = nullptr);
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000494
Jim Grosbachbfe3a9c2015-05-02 00:44:07 +0000495 /// dump - print the current interval mapping to dbgs().
Eric Christopherede62672011-02-03 06:18:29 +0000496 void dump() const;
497
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000498 // ===--- High level methods ---===
499
Jakob Stoklund Olesenc70b6972011-04-12 19:32:53 +0000500 /// splitSingleBlock - Split CurLI into a separate live interval around the
501 /// uses in a single block. This is intended to be used as part of a larger
502 /// split, and doesn't call finish().
503 void splitSingleBlock(const SplitAnalysis::BlockInfo &BI);
504
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +0000505 /// splitLiveThroughBlock - Split CurLI in the given block such that it
506 /// enters the block in IntvIn and leaves it in IntvOut. There may be uses in
507 /// the block, but they will be ignored when placing split points.
508 ///
509 /// @param MBBNum Block number.
510 /// @param IntvIn Interval index entering the block.
511 /// @param LeaveBefore When set, leave IntvIn before this point.
512 /// @param IntvOut Interval index leaving the block.
513 /// @param EnterAfter When set, enter IntvOut after this point.
514 void splitLiveThroughBlock(unsigned MBBNum,
515 unsigned IntvIn, SlotIndex LeaveBefore,
516 unsigned IntvOut, SlotIndex EnterAfter);
517
518 /// splitRegInBlock - Split CurLI in the given block such that it enters the
519 /// block in IntvIn and leaves it on the stack (or not at all). Split points
520 /// are placed in a way that avoids putting uses in the stack interval. This
521 /// may require creating a local interval when there is interference.
522 ///
523 /// @param BI Block descriptor.
524 /// @param IntvIn Interval index entering the block. Not 0.
525 /// @param LeaveBefore When set, leave IntvIn before this point.
526 void splitRegInBlock(const SplitAnalysis::BlockInfo &BI,
527 unsigned IntvIn, SlotIndex LeaveBefore);
528
529 /// splitRegOutBlock - Split CurLI in the given block such that it enters the
530 /// block on the stack (or isn't live-in at all) and leaves it in IntvOut.
531 /// Split points are placed to avoid interference and such that the uses are
532 /// not in the stack interval. This may require creating a local interval
533 /// when there is interference.
534 ///
535 /// @param BI Block descriptor.
536 /// @param IntvOut Interval index leaving the block.
537 /// @param EnterAfter When set, enter IntvOut after this point.
538 void splitRegOutBlock(const SplitAnalysis::BlockInfo &BI,
539 unsigned IntvOut, SlotIndex EnterAfter);
Jakob Stoklund Olesend1191ee2010-08-13 21:18:48 +0000540};
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +0000541
Eugene Zelenko900b6332017-08-29 22:32:07 +0000542} // end namespace llvm
Sebastian Redlb8a62aa2011-04-24 15:46:51 +0000543
Eugene Zelenko900b6332017-08-29 22:32:07 +0000544#endif // LLVM_LIB_CODEGEN_SPLITKIT_H