blob: 86ad3811e3ad27c18ab5d38b8646b6ab630066e6 [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//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file contains the SplitAnalysis class as well as mutator functions for
10// live range splitting.
11//
12//===----------------------------------------------------------------------===//
13
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000014#ifndef LLVM_LIB_CODEGEN_SPLITKIT_H
15#define LLVM_LIB_CODEGEN_SPLITKIT_H
Sebastian Redlb8a62aa2011-04-24 15:46:51 +000016
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +000017#include "LiveRangeCalc.h"
Jakob Stoklund Olesenbf91c4e2011-04-06 03:57:00 +000018#include "llvm/ADT/ArrayRef.h"
Eugene Zelenko900b6332017-08-29 22:32:07 +000019#include "llvm/ADT/BitVector.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"
Eugene Zelenko900b6332017-08-29 22:32:07 +000023#include "llvm/ADT/PointerIntPair.h"
Jakob Stoklund Olesen2530cd22010-12-21 01:50:21 +000024#include "llvm/ADT/SmallPtrSet.h"
Eugene Zelenko900b6332017-08-29 22:32:07 +000025#include "llvm/ADT/SmallVector.h"
26#include "llvm/CodeGen/LiveInterval.h"
Daniil Fukalov349b5942018-09-25 18:37:38 +000027#include "llvm/CodeGen/LiveIntervals.h"
Eugene Zelenko900b6332017-08-29 22:32:07 +000028#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);
Daniil Fukalov349b5942018-09-25 18:37:38 +000079
80 /// Return the base index of the first insert point in \pMBB.
81 SlotIndex getFirstInsertPoint(MachineBasicBlock &MBB) {
82 SlotIndex Res = LIS.getMBBStartIdx(&MBB);
83 if (!MBB.empty()) {
84 MachineBasicBlock::iterator MII = MBB.SkipPHIsLabelsAndDebug(MBB.begin());
85 if (MII != MBB.end())
86 Res = LIS.getInstructionIndex(*MII);
87 }
88 return Res;
89 }
90
Wei Mi35ee9332016-05-11 22:28:29 +000091};
92
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +000093/// SplitAnalysis - Analyze a LiveInterval, looking for live range splitting
94/// opportunities.
Benjamin Kramerf4c20252015-07-01 14:47:39 +000095class LLVM_LIBRARY_VISIBILITY SplitAnalysis {
Jakob Stoklund Olesen284c2db2010-08-10 17:07:22 +000096public:
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +000097 const MachineFunction &MF;
Jakob Stoklund Olesenf1a60a62011-02-19 00:53:42 +000098 const VirtRegMap &VRM;
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +000099 const LiveIntervals &LIS;
100 const MachineLoopInfo &Loops;
101 const TargetInstrInfo &TII;
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +0000102
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000103 /// Additional information about basic blocks where the current variable is
104 /// live. Such a block will look like one of these templates:
105 ///
106 /// 1. | o---x | Internal to block. Variable is only live in this block.
107 /// 2. |---x | Live-in, kill.
108 /// 3. | o---| Def, live-out.
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000109 /// 4. |---x o---| Live-in, kill, def, live-out. Counted by NumGapBlocks.
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000110 /// 5. |---o---o---| Live-through with uses or defs.
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000111 /// 6. |-----------| Live-through without uses. Counted by NumThroughBlocks.
112 ///
113 /// Two BlockInfo entries are created for template 4. One for the live-in
114 /// segment, and one for the live-out segment. These entries look as if the
115 /// block were split in the middle where the live range isn't live.
116 ///
117 /// Live-through blocks without any uses don't get BlockInfo entries. They
118 /// are simply listed in ThroughBlocks instead.
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000119 ///
120 struct BlockInfo {
121 MachineBasicBlock *MBB;
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +0000122 SlotIndex FirstInstr; ///< First instr accessing current reg.
123 SlotIndex LastInstr; ///< Last instr accessing current reg.
Jakob Stoklund Olesenae8027c2011-08-02 22:37:22 +0000124 SlotIndex FirstDef; ///< First non-phi valno->def, or SlotIndex().
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000125 bool LiveIn; ///< Current reg is live in.
126 bool LiveOut; ///< Current reg is live out.
Jakob Stoklund Olesenadc6a4c2011-06-30 01:30:39 +0000127
128 /// isOneInstr - Returns true when this BlockInfo describes a single
129 /// instruction.
130 bool isOneInstr() const {
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +0000131 return SlotIndex::isSameInstr(FirstInstr, LastInstr);
Jakob Stoklund Olesenadc6a4c2011-06-30 01:30:39 +0000132 }
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000133 };
134
Jakob Stoklund Olesen622848b2010-08-12 17:07:14 +0000135private:
136 // Current live interval.
Eugene Zelenko900b6332017-08-29 22:32:07 +0000137 const LiveInterval *CurLI = nullptr;
Jakob Stoklund Olesen622848b2010-08-12 17:07:14 +0000138
Wei Mi35ee9332016-05-11 22:28:29 +0000139 /// Insert Point Analysis.
140 InsertPointAnalysis IPA;
141
Jakob Stoklund Olesen994fed62012-01-12 17:53:44 +0000142 // Sorted slot indexes of using instructions.
143 SmallVector<SlotIndex, 8> UseSlots;
144
Jakob Stoklund Olesenbf91c4e2011-04-06 03:57:00 +0000145 /// UseBlocks - Blocks where CurLI has uses.
146 SmallVector<BlockInfo, 8> UseBlocks;
147
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000148 /// NumGapBlocks - Number of duplicate entries in UseBlocks for blocks where
149 /// the live range has a gap.
150 unsigned NumGapBlocks;
151
Jakob Stoklund Olesenbf91c4e2011-04-06 03:57:00 +0000152 /// ThroughBlocks - Block numbers where CurLI is live through without uses.
Jakob Stoklund Olesened47ed42011-04-09 02:59:09 +0000153 BitVector ThroughBlocks;
154
155 /// NumThroughBlocks - Number of live-through blocks.
156 unsigned NumThroughBlocks;
Jakob Stoklund Olesenbf91c4e2011-04-06 03:57:00 +0000157
Jakob Stoklund Oleseneaa6ed12011-05-03 20:42:13 +0000158 /// DidRepairRange - analyze was forced to shrinkToUses().
159 bool DidRepairRange;
160
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000161 // Sumarize statistics by counting instructions using CurLI.
Jakob Stoklund Olesenff095502010-07-20 16:12:37 +0000162 void analyzeUses();
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +0000163
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000164 /// calcLiveBlockInfo - Compute per-block information about CurLI.
Jakob Stoklund Olesen27e0a4a2011-03-05 18:33:49 +0000165 bool calcLiveBlockInfo();
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000166
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +0000167public:
Jakob Stoklund Olesenf1a60a62011-02-19 00:53:42 +0000168 SplitAnalysis(const VirtRegMap &vrm, const LiveIntervals &lis,
Jakob Stoklund Olesen0fef9dd2010-07-20 23:50:15 +0000169 const MachineLoopInfo &mli);
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +0000170
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000171 /// analyze - set CurLI to the specified interval, and analyze how it may be
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +0000172 /// split.
173 void analyze(const LiveInterval *li);
174
Jakob Stoklund Oleseneaa6ed12011-05-03 20:42:13 +0000175 /// didRepairRange() - Returns true if CurLI was invalid and has been repaired
176 /// by analyze(). This really shouldn't happen, but sometimes the coalescer
177 /// can create live ranges that end in mid-air.
178 bool didRepairRange() const { return DidRepairRange; }
179
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +0000180 /// clear - clear all data structures so SplitAnalysis is ready to analyze a
181 /// new interval.
182 void clear();
183
Jakob Stoklund Olesen93c87362011-02-17 19:13:53 +0000184 /// getParent - Return the last analyzed interval.
185 const LiveInterval &getParent() const { return *CurLI; }
186
Jakob Stoklund Olesen60a26a62011-02-21 23:09:46 +0000187 /// isOriginalEndpoint - Return true if the original live range was killed or
188 /// (re-)defined at Idx. Idx should be the 'def' slot for a normal kill/def,
189 /// and 'use' for an early-clobber def.
190 /// This can be used to recognize code inserted by earlier live range
191 /// splitting.
192 bool isOriginalEndpoint(SlotIndex Idx) const;
193
Jakob Stoklund Olesen994fed62012-01-12 17:53:44 +0000194 /// getUseSlots - Return an array of SlotIndexes of instructions using CurLI.
195 /// This include both use and def operands, at most one entry per instruction.
196 ArrayRef<SlotIndex> getUseSlots() const { return UseSlots; }
197
Jakob Stoklund Olesenbf91c4e2011-04-06 03:57:00 +0000198 /// getUseBlocks - Return an array of BlockInfo objects for the basic blocks
199 /// where CurLI has uses.
Jakob Stoklund Olesen5cc91b22011-05-28 02:32:57 +0000200 ArrayRef<BlockInfo> getUseBlocks() const { return UseBlocks; }
Jakob Stoklund Olesenbf91c4e2011-04-06 03:57:00 +0000201
Jakob Stoklund Olesened47ed42011-04-09 02:59:09 +0000202 /// getNumThroughBlocks - Return the number of through blocks.
203 unsigned getNumThroughBlocks() const { return NumThroughBlocks; }
204
205 /// isThroughBlock - Return true if CurLI is live through MBB without uses.
206 bool isThroughBlock(unsigned MBB) const { return ThroughBlocks.test(MBB); }
Jakob Stoklund Olesenbf91c4e2011-04-06 03:57:00 +0000207
Jakob Stoklund Olesenc49df2c2011-04-12 21:30:53 +0000208 /// getThroughBlocks - Return the set of through blocks.
209 const BitVector &getThroughBlocks() const { return ThroughBlocks; }
210
Jakob Stoklund Olesen5cc91b22011-05-28 02:32:57 +0000211 /// getNumLiveBlocks - Return the number of blocks where CurLI is live.
212 unsigned getNumLiveBlocks() const {
Jakob Stoklund Olesenec43d5d2011-05-30 01:33:26 +0000213 return getUseBlocks().size() - NumGapBlocks + getNumThroughBlocks();
Jakob Stoklund Olesen5cc91b22011-05-28 02:32:57 +0000214 }
215
216 /// countLiveBlocks - Return the number of blocks where li is live. This is
217 /// guaranteed to return the same number as getNumLiveBlocks() after calling
218 /// analyze(li).
Jakob Stoklund Oleseneef23272011-04-26 22:33:12 +0000219 unsigned countLiveBlocks(const LiveInterval *li) const;
220
Eugene Zelenko900b6332017-08-29 22:32:07 +0000221 using BlockPtrSet = SmallPtrSet<const MachineBasicBlock *, 16>;
Jakob Stoklund Olesened4075c2010-07-20 21:46:58 +0000222
Jakob Stoklund Olesen8627ea92011-08-05 22:20:45 +0000223 /// shouldSplitSingleBlock - Returns true if it would help to create a local
224 /// live range for the instructions in BI. There is normally no benefit to
225 /// creating a live range for a single instruction, but it does enable
226 /// register class inflation if the instruction has a restricted register
227 /// class.
228 ///
229 /// @param BI The block to be isolated.
230 /// @param SingleInstrs True when single instructions should be isolated.
231 bool shouldSplitSingleBlock(const BlockInfo &BI, bool SingleInstrs) const;
Wei Mi35ee9332016-05-11 22:28:29 +0000232
233 SlotIndex getLastSplitPoint(unsigned Num) {
Wei Mif3c8f532016-05-23 19:39:19 +0000234 return IPA.getLastInsertPoint(*CurLI, *MF.getBlockNumbered(Num));
Wei Mi35ee9332016-05-11 22:28:29 +0000235 }
236
237 MachineBasicBlock::iterator getLastSplitPointIter(MachineBasicBlock *BB) {
Wei Mif3c8f532016-05-23 19:39:19 +0000238 return IPA.getLastInsertPointIter(*CurLI, *BB);
Wei Mi35ee9332016-05-11 22:28:29 +0000239 }
Daniil Fukalov349b5942018-09-25 18:37:38 +0000240
241 SlotIndex getFirstSplitPoint(unsigned Num) {
242 return IPA.getFirstInsertPoint(*MF.getBlockNumbered(Num));
243 }
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +0000244};
245
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000246/// SplitEditor - Edit machine code and LiveIntervals for live range
247/// splitting.
248///
Jakob Stoklund Olesen45e07c82010-08-06 22:17:33 +0000249/// - Create a SplitEditor from a SplitAnalysis.
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000250/// - Start a new live interval with openIntv.
251/// - Mark the places where the new interval is entered using enterIntv*
Fangrui Songf78650a2018-07-30 19:41:25 +0000252/// - Mark the ranges where the new interval is used with useIntv*
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000253/// - Mark the places where the interval is exited with exitIntv*.
254/// - Finish the current interval with closeIntv and repeat from 2.
Jakob Stoklund Olesen959fcc62010-10-08 23:42:21 +0000255/// - Rewrite instructions with finish().
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000256///
Benjamin Kramerf4c20252015-07-01 14:47:39 +0000257class LLVM_LIBRARY_VISIBILITY SplitEditor {
Jakob Stoklund Olesen04aff702011-02-19 00:42:33 +0000258 SplitAnalysis &SA;
Wei Mic0223702016-07-08 21:08:09 +0000259 AliasAnalysis &AA;
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000260 LiveIntervals &LIS;
261 VirtRegMap &VRM;
262 MachineRegisterInfo &MRI;
Eric Christopherede62672011-02-03 06:18:29 +0000263 MachineDominatorTree &MDT;
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000264 const TargetInstrInfo &TII;
265 const TargetRegisterInfo &TRI;
Benjamin Kramere2a1d892013-06-17 19:00:36 +0000266 const MachineBlockFrequencyInfo &MBFI;
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000267
Jakob Stoklund Oleseneecb2fb2011-09-12 16:49:21 +0000268public:
Jakob Stoklund Oleseneecb2fb2011-09-12 16:49:21 +0000269 /// ComplementSpillMode - Select how the complement live range should be
270 /// created. SplitEditor automatically creates interval 0 to contain
271 /// anything that isn't added to another interval. This complement interval
272 /// can get quite complicated, and it can sometimes be an advantage to allow
273 /// it to overlap the other intervals. If it is going to spill anyway, no
274 /// registers are wasted by keeping a value in two places at the same time.
275 enum ComplementSpillMode {
276 /// SM_Partition(Default) - Try to create the complement interval so it
277 /// doesn't overlap any other intervals, and the original interval is
278 /// partitioned. This may require a large number of back copies and extra
279 /// PHI-defs. Only segments marked with overlapIntv will be overlapping.
280 SM_Partition,
281
282 /// SM_Size - Overlap intervals to minimize the number of inserted COPY
283 /// instructions. Copies to the complement interval are hoisted to their
284 /// common dominator, so only one COPY is required per value in the
285 /// complement interval. This also means that no extra PHI-defs need to be
286 /// inserted in the complement interval.
287 SM_Size,
288
289 /// SM_Speed - Overlap intervals to minimize the expected execution
290 /// frequency of the inserted copies. This is very similar to SM_Size, but
291 /// the complement interval may get some extra PHI-defs.
292 SM_Speed
293 };
294
295private:
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000296 /// Edit - The current parent register and new intervals created.
Eugene Zelenko900b6332017-08-29 22:32:07 +0000297 LiveRangeEdit *Edit = nullptr;
Jakob Stoklund Olesen72911e42010-10-14 23:49:52 +0000298
Eric Christopherede62672011-02-03 06:18:29 +0000299 /// Index into Edit of the currently open interval.
300 /// The index 0 is used for the complement, so the first interval started by
301 /// openIntv will be 1.
Eugene Zelenko900b6332017-08-29 22:32:07 +0000302 unsigned OpenIdx = 0;
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000303
Jakob Stoklund Oleseneecb2fb2011-09-12 16:49:21 +0000304 /// The current spill mode, selected by reset().
Eugene Zelenko900b6332017-08-29 22:32:07 +0000305 ComplementSpillMode SpillMode = SM_Partition;
Jakob Stoklund Oleseneecb2fb2011-09-12 16:49:21 +0000306
Eugene Zelenko900b6332017-08-29 22:32:07 +0000307 using RegAssignMap = IntervalMap<SlotIndex, unsigned>;
Eric Christopherede62672011-02-03 06:18:29 +0000308
309 /// Allocator for the interval map. This will eventually be shared with
310 /// SlotIndexes and LiveIntervals.
311 RegAssignMap::Allocator Allocator;
312
313 /// RegAssign - Map of the assigned register indexes.
314 /// Edit.get(RegAssign.lookup(Idx)) is the register that should be live at
315 /// Idx.
316 RegAssignMap RegAssign;
317
Eugene Zelenko900b6332017-08-29 22:32:07 +0000318 using ValueForcePair = PointerIntPair<VNInfo *, 1>;
319 using ValueMap = DenseMap<std::pair<unsigned, unsigned>, ValueForcePair>;
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000320
321 /// Values - keep track of the mapping from parent values to values in the new
322 /// intervals. Given a pair (RegIdx, ParentVNI->id), Values contains:
323 ///
324 /// 1. No entry - the value is not mapped to Edit.get(RegIdx).
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000325 /// 2. (Null, false) - the value is mapped to multiple values in
326 /// Edit.get(RegIdx). Each value is represented by a minimal live range at
327 /// its def. The full live range can be inferred exactly from the range
328 /// of RegIdx in RegAssign.
329 /// 3. (Null, true). As above, but the ranges in RegAssign are too large, and
330 /// the live range must be recomputed using LiveRangeCalc::extend().
331 /// 4. (VNI, false) The value is mapped to a single new value.
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000332 /// The new value has no live ranges anywhere.
333 ValueMap Values;
334
Jakob Stoklund Olesen054984d2011-09-13 16:47:53 +0000335 /// LRCalc - Cache for computing live ranges and SSA update. Each instance
336 /// can only handle non-overlapping live ranges, so use a separate
337 /// LiveRangeCalc instance for the complement interval when in spill mode.
338 LiveRangeCalc LRCalc[2];
339
340 /// getLRCalc - Return the LRCalc to use for RegIdx. In spill mode, the
341 /// complement interval can overlap the other intervals, so it gets its own
342 /// LRCalc instance. When not in spill mode, all intervals can share one.
343 LiveRangeCalc &getLRCalc(unsigned RegIdx) {
344 return LRCalc[SpillMode != SM_Partition && RegIdx != 0];
345 }
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000346
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000347 /// Find a subrange corresponding to the lane mask @p LM in the live
348 /// interval @p LI. The interval @p LI is assumed to contain such a subrange.
349 /// This function is used to find corresponding subranges between the
350 /// original interval and the new intervals.
351 LiveInterval::SubRange &getSubRangeForMask(LaneBitmask LM, LiveInterval &LI);
352
353 /// Add a segment to the interval LI for the value number VNI. If LI has
354 /// subranges, corresponding segments will be added to them as well, but
355 /// with newly created value numbers. If Original is true, dead def will
356 /// only be added a subrange of LI if the corresponding subrange of the
357 /// original interval has a def at this index. Otherwise, all subranges
358 /// of LI will be updated.
359 void addDeadDef(LiveInterval &LI, VNInfo *VNI, bool Original);
360
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000361 /// defValue - define a value in RegIdx from ParentVNI at Idx.
362 /// Idx does not have to be ParentVNI->def, but it must be contained within
363 /// ParentVNI's live range in ParentLI. The new value is added to the value
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000364 /// map. The value being defined may either come from rematerialization
365 /// (or an inserted copy), or it may be coming from the original interval.
366 /// The parameter Original should be true in the latter case, otherwise
367 /// it should be false.
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000368 /// Return the new LI value.
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000369 VNInfo *defValue(unsigned RegIdx, const VNInfo *ParentVNI, SlotIndex Idx,
370 bool Original);
Jakob Stoklund Olesen8ef91fc2011-03-01 23:14:53 +0000371
Jakob Stoklund Olesen5d4277d2011-09-13 23:09:04 +0000372 /// forceRecompute - Force the live range of ParentVNI in RegIdx to be
373 /// recomputed by LiveRangeCalc::extend regardless of the number of defs.
374 /// This is used for values whose live range doesn't match RegAssign exactly.
375 /// They could have rematerialized, or back-copies may have been moved.
Matthias Braunca0abae2018-02-02 00:08:19 +0000376 void forceRecompute(unsigned RegIdx, const VNInfo &ParentVNI);
377
378 /// Calls forceRecompute() on any affected regidx and on ParentVNI
379 /// predecessors in case of a phi definition.
380 void forceRecomputeVNI(const VNInfo &ParentVNI);
Jakob Stoklund Olesen4484f992011-09-13 18:05:29 +0000381
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000382 /// defFromParent - Define Reg from ParentVNI at UseIdx using either
383 /// rematerialization or a COPY from parent. Return the new value.
Eric Christopherede62672011-02-03 06:18:29 +0000384 VNInfo *defFromParent(unsigned RegIdx,
Jakob Stoklund Olesen6ee7d9aa2010-11-10 19:31:50 +0000385 VNInfo *ParentVNI,
386 SlotIndex UseIdx,
387 MachineBasicBlock &MBB,
388 MachineBasicBlock::iterator I);
389
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000390 /// removeBackCopies - Remove the copy instructions that defines the values
391 /// in the vector in the complement interval.
392 void removeBackCopies(SmallVectorImpl<VNInfo*> &Copies);
393
Jakob Stoklund Olesena98af392011-09-14 16:45:39 +0000394 /// getShallowDominator - Returns the least busy dominator of MBB that is
395 /// also dominated by DefMBB. Busy is measured by loop depth.
396 MachineBasicBlock *findShallowDominator(MachineBasicBlock *MBB,
397 MachineBasicBlock *DefMBB);
398
Wei Mi9a16d652016-04-13 03:08:27 +0000399 /// Find out all the backCopies dominated by others.
400 void computeRedundantBackCopies(DenseSet<unsigned> &NotToHoistSet,
401 SmallVectorImpl<VNInfo *> &BackCopies);
402
403 /// Hoist back-copies to the complement interval. It tries to hoist all
404 /// the back-copies to one BB if it is beneficial, or else simply remove
Eric Christopher75d661a2016-05-04 21:45:36 +0000405 /// redundant backcopies dominated by others.
Wei Mi9a16d652016-04-13 03:08:27 +0000406 void hoistCopies();
Jakob Stoklund Olesena25330f2011-09-13 22:22:39 +0000407
Jakob Stoklund Olesen1af8b4d2011-04-15 17:24:49 +0000408 /// transferValues - Transfer values to the new ranges.
409 /// Return true if any ranges were skipped.
410 bool transferValues();
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +0000411
Krzysztof Parzyszek73c8a9b2016-11-21 20:24:12 +0000412 /// Live range @p LR corresponding to the lane Mask @p LM has a live
413 /// PHI def at the beginning of block @p B. Extend the range @p LR of
414 /// all predecessor values that reach this def. If @p LR is a subrange,
415 /// the array @p Undefs is the set of all locations where it is undefined
416 /// via <def,read-undef> in other subranges for the same register.
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000417 void extendPHIRange(MachineBasicBlock &B, LiveRangeCalc &LRC,
Krzysztof Parzyszek73c8a9b2016-11-21 20:24:12 +0000418 LiveRange &LR, LaneBitmask LM,
419 ArrayRef<SlotIndex> Undefs);
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000420
Jakob Stoklund Olesen36482632011-03-02 23:05:16 +0000421 /// extendPHIKillRanges - Extend the ranges of all values killed by original
422 /// parent PHIDefs.
423 void extendPHIKillRanges();
424
Eric Christopherede62672011-02-03 06:18:29 +0000425 /// rewriteAssigned - Rewrite all uses of Edit.getReg() to assigned registers.
Jakob Stoklund Olesen503b1432011-03-02 23:05:19 +0000426 void rewriteAssigned(bool ExtendRanges);
Jakob Stoklund Olesen6f8bd422010-09-21 22:32:21 +0000427
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +0000428 /// deleteRematVictims - Delete defs that are dead after rematerializing.
429 void deleteRematVictims();
430
Matthias Braunf0b68d32017-03-17 00:41:39 +0000431 /// Add a copy instruction copying \p FromReg to \p ToReg before
432 /// \p InsertBefore. This can be invoked with a \p LaneMask which may make it
433 /// necessary to construct a sequence of copies to cover it exactly.
434 SlotIndex buildCopy(unsigned FromReg, unsigned ToReg, LaneBitmask LaneMask,
435 MachineBasicBlock &MBB, MachineBasicBlock::iterator InsertBefore,
436 bool Late, unsigned RegIdx);
437
438 SlotIndex buildSingleSubRegCopy(unsigned FromReg, unsigned ToReg,
439 MachineBasicBlock &MB, MachineBasicBlock::iterator InsertBefore,
Fangrui Songcb0bab82018-07-16 18:51:40 +0000440 unsigned SubIdx, LiveInterval &DestLI, bool Late, SlotIndex Def);
Matthias Braunf0b68d32017-03-17 00:41:39 +0000441
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000442public:
443 /// Create a new SplitEditor for editing the LiveInterval analyzed by SA.
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000444 /// Newly created intervals will be appended to newIntervals.
Eugene Zelenko900b6332017-08-29 22:32:07 +0000445 SplitEditor(SplitAnalysis &sa, AliasAnalysis &aa, LiveIntervals &lis,
446 VirtRegMap &vrm, MachineDominatorTree &mdt,
447 MachineBlockFrequencyInfo &mbfi);
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000448
Jakob Stoklund Olesenc9601982011-03-03 01:29:13 +0000449 /// reset - Prepare for a new split.
Jakob Stoklund Oleseneecb2fb2011-09-12 16:49:21 +0000450 void reset(LiveRangeEdit&, ComplementSpillMode = SM_Partition);
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000451
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000452 /// Create a new virtual register and live interval.
Jakob Stoklund Olesen0840f502011-04-12 18:11:31 +0000453 /// Return the interval index, starting from 1. Interval index 0 is the
454 /// implicit complement interval.
455 unsigned openIntv();
456
457 /// currentIntv - Return the current interval index.
458 unsigned currentIntv() const { return OpenIdx; }
459
460 /// selectIntv - Select a previously opened interval index.
461 void selectIntv(unsigned Idx);
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000462
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000463 /// enterIntvBefore - Enter the open interval before the instruction at Idx.
464 /// If the parent interval is not live before Idx, a COPY is not inserted.
465 /// Return the beginning of the new live range.
466 SlotIndex enterIntvBefore(SlotIndex Idx);
Jakob Stoklund Olesen622848b2010-08-12 17:07:14 +0000467
Jakob Stoklund Olesenadc6a4c2011-06-30 01:30:39 +0000468 /// enterIntvAfter - Enter the open interval after the instruction at Idx.
469 /// Return the beginning of the new live range.
470 SlotIndex enterIntvAfter(SlotIndex Idx);
471
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000472 /// enterIntvAtEnd - Enter the open interval at the end of MBB.
Eric Christopher650c8f22014-05-20 17:11:11 +0000473 /// Use the open interval from the inserted copy to the MBB end.
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000474 /// Return the beginning of the new live range.
475 SlotIndex enterIntvAtEnd(MachineBasicBlock &MBB);
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000476
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000477 /// useIntv - indicate that all instructions in MBB should use OpenLI.
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000478 void useIntv(const MachineBasicBlock &MBB);
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000479
Jakob Stoklund Olesenb3089022011-01-26 00:50:53 +0000480 /// useIntv - indicate that all instructions in range should use OpenLI.
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000481 void useIntv(SlotIndex Start, SlotIndex End);
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000482
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000483 /// leaveIntvAfter - Leave the open interval after the instruction at Idx.
484 /// Return the end of the live range.
485 SlotIndex leaveIntvAfter(SlotIndex Idx);
Jakob Stoklund Olesen622848b2010-08-12 17:07:14 +0000486
Jakob Stoklund Olesen7cb57b32011-02-09 23:30:25 +0000487 /// leaveIntvBefore - Leave the open interval before the instruction at Idx.
488 /// Return the end of the live range.
489 SlotIndex leaveIntvBefore(SlotIndex Idx);
490
Jakob Stoklund Olesendc96e282010-08-04 22:08:39 +0000491 /// leaveIntvAtTop - Leave the interval at the top of MBB.
Jakob Stoklund Olesenf12e1202011-02-03 17:04:16 +0000492 /// Add liveness from the MBB top to the copy.
493 /// Return the end of the live range.
494 SlotIndex leaveIntvAtTop(MachineBasicBlock &MBB);
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000495
Jakob Stoklund Olesen17499352011-02-08 18:50:21 +0000496 /// overlapIntv - Indicate that all instructions in range should use the open
497 /// interval, but also let the complement interval be live.
498 ///
499 /// This doubles the register pressure, but is sometimes required to deal with
500 /// register uses after the last valid split point.
501 ///
502 /// The Start index should be a return value from a leaveIntv* call, and End
503 /// should be in the same basic block. The parent interval must have the same
504 /// value across the range.
505 ///
506 void overlapIntv(SlotIndex Start, SlotIndex End);
507
Jakob Stoklund Olesen959fcc62010-10-08 23:42:21 +0000508 /// finish - after all the new live ranges have been created, compute the
509 /// remaining live range, and rewrite instructions to use the new registers.
Jakob Stoklund Olesen6a663b82011-04-21 18:38:15 +0000510 /// @param LRMap When not null, this vector will map each live range in Edit
511 /// back to the indices returned by openIntv.
512 /// There may be extra indices created by dead code elimination.
Craig Topperada08572014-04-16 04:21:27 +0000513 void finish(SmallVectorImpl<unsigned> *LRMap = nullptr);
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000514
Jim Grosbachbfe3a9c2015-05-02 00:44:07 +0000515 /// dump - print the current interval mapping to dbgs().
Eric Christopherede62672011-02-03 06:18:29 +0000516 void dump() const;
517
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000518 // ===--- High level methods ---===
519
Jakob Stoklund Olesenc70b6972011-04-12 19:32:53 +0000520 /// splitSingleBlock - Split CurLI into a separate live interval around the
521 /// uses in a single block. This is intended to be used as part of a larger
522 /// split, and doesn't call finish().
523 void splitSingleBlock(const SplitAnalysis::BlockInfo &BI);
524
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +0000525 /// splitLiveThroughBlock - Split CurLI in the given block such that it
526 /// enters the block in IntvIn and leaves it in IntvOut. There may be uses in
527 /// the block, but they will be ignored when placing split points.
528 ///
529 /// @param MBBNum Block number.
530 /// @param IntvIn Interval index entering the block.
531 /// @param LeaveBefore When set, leave IntvIn before this point.
532 /// @param IntvOut Interval index leaving the block.
533 /// @param EnterAfter When set, enter IntvOut after this point.
534 void splitLiveThroughBlock(unsigned MBBNum,
535 unsigned IntvIn, SlotIndex LeaveBefore,
536 unsigned IntvOut, SlotIndex EnterAfter);
537
538 /// splitRegInBlock - Split CurLI in the given block such that it enters the
539 /// block in IntvIn and leaves it on the stack (or not at all). Split points
540 /// are placed in a way that avoids putting uses in the stack interval. This
541 /// may require creating a local interval when there is interference.
542 ///
543 /// @param BI Block descriptor.
544 /// @param IntvIn Interval index entering the block. Not 0.
545 /// @param LeaveBefore When set, leave IntvIn before this point.
546 void splitRegInBlock(const SplitAnalysis::BlockInfo &BI,
547 unsigned IntvIn, SlotIndex LeaveBefore);
548
549 /// splitRegOutBlock - Split CurLI in the given block such that it enters the
550 /// block on the stack (or isn't live-in at all) and leaves it in IntvOut.
551 /// Split points are placed to avoid interference and such that the uses are
552 /// not in the stack interval. This may require creating a local interval
553 /// when there is interference.
554 ///
555 /// @param BI Block descriptor.
556 /// @param IntvOut Interval index leaving the block.
557 /// @param EnterAfter When set, enter IntvOut after this point.
558 void splitRegOutBlock(const SplitAnalysis::BlockInfo &BI,
559 unsigned IntvOut, SlotIndex EnterAfter);
Jakob Stoklund Olesend1191ee2010-08-13 21:18:48 +0000560};
Jakob Stoklund Olesen36d12c62010-07-20 15:41:07 +0000561
Eugene Zelenko900b6332017-08-29 22:32:07 +0000562} // end namespace llvm
Sebastian Redlb8a62aa2011-04-24 15:46:51 +0000563
Eugene Zelenko900b6332017-08-29 22:32:07 +0000564#endif // LLVM_LIB_CODEGEN_SPLITKIT_H