Jakob Stoklund Olesen | f96ae68 | 2011-01-04 21:10:05 +0000 | [diff] [blame] | 1 | //===-------- SplitKit.h - Toolkit for splitting live ranges ----*- C++ -*-===// |
Jakob Stoklund Olesen | 36d12c6 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 2 | // |
| 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 Kramer | a7c40ef | 2014-08-13 16:26:38 +0000 | [diff] [blame] | 15 | #ifndef LLVM_LIB_CODEGEN_SPLITKIT_H |
| 16 | #define LLVM_LIB_CODEGEN_SPLITKIT_H |
Sebastian Redl | b8a62aa | 2011-04-24 15:46:51 +0000 | [diff] [blame] | 17 | |
Jakob Stoklund Olesen | 487f2a3 | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 18 | #include "LiveRangeCalc.h" |
Jakob Stoklund Olesen | bf91c4e | 2011-04-06 03:57:00 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/ArrayRef.h" |
Jakob Stoklund Olesen | 36d12c6 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/DenseMap.h" |
Wei Mi | 9a16d65 | 2016-04-13 03:08:27 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/DenseSet.h" |
Eric Christopher | ede6267 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/IntervalMap.h" |
Jakob Stoklund Olesen | 2530cd2 | 2010-12-21 01:50:21 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/SmallPtrSet.h" |
Jakob Stoklund Olesen | 36d12c6 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 24 | |
| 25 | namespace llvm { |
| 26 | |
Eric Christopher | ede6267 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 27 | class ConnectedVNInfoEqClasses; |
Jakob Stoklund Olesen | 36d12c6 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 28 | class LiveInterval; |
| 29 | class LiveIntervals; |
Jakob Stoklund Olesen | 72911e4 | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 30 | class LiveRangeEdit; |
Benjamin Kramer | e2a1d89 | 2013-06-17 19:00:36 +0000 | [diff] [blame] | 31 | class MachineBlockFrequencyInfo; |
Jakob Stoklund Olesen | 36d12c6 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 32 | class MachineInstr; |
Jakob Stoklund Olesen | 36d12c6 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 33 | class MachineLoopInfo; |
Jakob Stoklund Olesen | c698417 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 34 | class MachineRegisterInfo; |
Jakob Stoklund Olesen | ed4075c | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 35 | class TargetInstrInfo; |
Jakob Stoklund Olesen | 6ee7d9aa | 2010-11-10 19:31:50 +0000 | [diff] [blame] | 36 | class TargetRegisterInfo; |
Jakob Stoklund Olesen | c698417 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 37 | class VirtRegMap; |
| 38 | class VNInfo; |
Jakob Stoklund Olesen | 9a74301 | 2010-10-22 20:28:21 +0000 | [diff] [blame] | 39 | class raw_ostream; |
Jakob Stoklund Olesen | 36d12c6 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 40 | |
Wei Mi | 35ee933 | 2016-05-11 22:28:29 +0000 | [diff] [blame^] | 41 | /// Determines the latest safe point in a block in which we can insert a split, |
| 42 | /// spill or other instruction related with CurLI. |
| 43 | class LLVM_LIBRARY_VISIBILITY InsertPointAnalysis { |
| 44 | private: |
| 45 | const LiveIntervals &LIS; |
| 46 | |
| 47 | /// Current LiveInterval for which to insert split or spill. |
| 48 | const LiveInterval *CurLI; |
| 49 | |
| 50 | /// Last legal insert point in each basic block in the current function. |
| 51 | /// The first entry is the first terminator, the second entry is the |
| 52 | /// last valid point to insert a split or spill for a variable that is |
| 53 | /// live into a landing pad successor. |
| 54 | SmallVector<std::pair<SlotIndex, SlotIndex>, 8> LastInsertPoint; |
| 55 | |
| 56 | SlotIndex computeLastInsertPoint(const MachineBasicBlock &MBB); |
| 57 | |
| 58 | public: |
| 59 | InsertPointAnalysis(const LiveIntervals &lis, unsigned BBNum); |
| 60 | |
| 61 | void setInterval(const LiveInterval *LI) { CurLI = LI; } |
| 62 | |
| 63 | /// Return the base index of the last valid insert point in \pMBB. |
| 64 | SlotIndex getLastInsertPoint(const MachineBasicBlock &MBB) { |
| 65 | unsigned Num = MBB.getNumber(); |
| 66 | // Inline the common simple case. |
| 67 | if (LastInsertPoint[Num].first.isValid() && |
| 68 | !LastInsertPoint[Num].second.isValid()) |
| 69 | return LastInsertPoint[Num].first; |
| 70 | return computeLastInsertPoint(MBB); |
| 71 | } |
| 72 | |
| 73 | /// Returns the last insert point as an iterator. |
| 74 | MachineBasicBlock::iterator getLastInsertPointIter(MachineBasicBlock &); |
| 75 | }; |
| 76 | |
Jakob Stoklund Olesen | c698417 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 77 | /// SplitAnalysis - Analyze a LiveInterval, looking for live range splitting |
| 78 | /// opportunities. |
Benjamin Kramer | f4c2025 | 2015-07-01 14:47:39 +0000 | [diff] [blame] | 79 | class LLVM_LIBRARY_VISIBILITY SplitAnalysis { |
Jakob Stoklund Olesen | 284c2db | 2010-08-10 17:07:22 +0000 | [diff] [blame] | 80 | public: |
Jakob Stoklund Olesen | b308902 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 81 | const MachineFunction &MF; |
Jakob Stoklund Olesen | f1a60a6 | 2011-02-19 00:53:42 +0000 | [diff] [blame] | 82 | const VirtRegMap &VRM; |
Jakob Stoklund Olesen | b308902 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 83 | const LiveIntervals &LIS; |
| 84 | const MachineLoopInfo &Loops; |
| 85 | const TargetInstrInfo &TII; |
Jakob Stoklund Olesen | 36d12c6 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 86 | |
Jakob Stoklund Olesen | b1b76ad | 2011-02-09 22:50:26 +0000 | [diff] [blame] | 87 | /// Additional information about basic blocks where the current variable is |
| 88 | /// live. Such a block will look like one of these templates: |
| 89 | /// |
| 90 | /// 1. | o---x | Internal to block. Variable is only live in this block. |
| 91 | /// 2. |---x | Live-in, kill. |
| 92 | /// 3. | o---| Def, live-out. |
Jakob Stoklund Olesen | ec43d5d | 2011-05-30 01:33:26 +0000 | [diff] [blame] | 93 | /// 4. |---x o---| Live-in, kill, def, live-out. Counted by NumGapBlocks. |
Jakob Stoklund Olesen | b1b76ad | 2011-02-09 22:50:26 +0000 | [diff] [blame] | 94 | /// 5. |---o---o---| Live-through with uses or defs. |
Jakob Stoklund Olesen | ec43d5d | 2011-05-30 01:33:26 +0000 | [diff] [blame] | 95 | /// 6. |-----------| Live-through without uses. Counted by NumThroughBlocks. |
| 96 | /// |
| 97 | /// Two BlockInfo entries are created for template 4. One for the live-in |
| 98 | /// segment, and one for the live-out segment. These entries look as if the |
| 99 | /// block were split in the middle where the live range isn't live. |
| 100 | /// |
| 101 | /// Live-through blocks without any uses don't get BlockInfo entries. They |
| 102 | /// are simply listed in ThroughBlocks instead. |
Jakob Stoklund Olesen | b1b76ad | 2011-02-09 22:50:26 +0000 | [diff] [blame] | 103 | /// |
| 104 | struct BlockInfo { |
| 105 | MachineBasicBlock *MBB; |
Jakob Stoklund Olesen | 43859a6 | 2011-08-02 22:54:14 +0000 | [diff] [blame] | 106 | SlotIndex FirstInstr; ///< First instr accessing current reg. |
| 107 | SlotIndex LastInstr; ///< Last instr accessing current reg. |
Jakob Stoklund Olesen | ae8027c | 2011-08-02 22:37:22 +0000 | [diff] [blame] | 108 | SlotIndex FirstDef; ///< First non-phi valno->def, or SlotIndex(). |
Jakob Stoklund Olesen | b1b76ad | 2011-02-09 22:50:26 +0000 | [diff] [blame] | 109 | bool LiveIn; ///< Current reg is live in. |
| 110 | bool LiveOut; ///< Current reg is live out. |
Jakob Stoklund Olesen | adc6a4c | 2011-06-30 01:30:39 +0000 | [diff] [blame] | 111 | |
| 112 | /// isOneInstr - Returns true when this BlockInfo describes a single |
| 113 | /// instruction. |
| 114 | bool isOneInstr() const { |
Jakob Stoklund Olesen | 43859a6 | 2011-08-02 22:54:14 +0000 | [diff] [blame] | 115 | return SlotIndex::isSameInstr(FirstInstr, LastInstr); |
Jakob Stoklund Olesen | adc6a4c | 2011-06-30 01:30:39 +0000 | [diff] [blame] | 116 | } |
Jakob Stoklund Olesen | b1b76ad | 2011-02-09 22:50:26 +0000 | [diff] [blame] | 117 | }; |
| 118 | |
Jakob Stoklund Olesen | 622848b | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 119 | private: |
| 120 | // Current live interval. |
Jakob Stoklund Olesen | b308902 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 121 | const LiveInterval *CurLI; |
Jakob Stoklund Olesen | 622848b | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 122 | |
Wei Mi | 35ee933 | 2016-05-11 22:28:29 +0000 | [diff] [blame^] | 123 | /// Insert Point Analysis. |
| 124 | InsertPointAnalysis IPA; |
| 125 | |
Jakob Stoklund Olesen | 994fed6 | 2012-01-12 17:53:44 +0000 | [diff] [blame] | 126 | // Sorted slot indexes of using instructions. |
| 127 | SmallVector<SlotIndex, 8> UseSlots; |
| 128 | |
Jakob Stoklund Olesen | bf91c4e | 2011-04-06 03:57:00 +0000 | [diff] [blame] | 129 | /// UseBlocks - Blocks where CurLI has uses. |
| 130 | SmallVector<BlockInfo, 8> UseBlocks; |
| 131 | |
Jakob Stoklund Olesen | ec43d5d | 2011-05-30 01:33:26 +0000 | [diff] [blame] | 132 | /// NumGapBlocks - Number of duplicate entries in UseBlocks for blocks where |
| 133 | /// the live range has a gap. |
| 134 | unsigned NumGapBlocks; |
| 135 | |
Jakob Stoklund Olesen | bf91c4e | 2011-04-06 03:57:00 +0000 | [diff] [blame] | 136 | /// ThroughBlocks - Block numbers where CurLI is live through without uses. |
Jakob Stoklund Olesen | ed47ed4 | 2011-04-09 02:59:09 +0000 | [diff] [blame] | 137 | BitVector ThroughBlocks; |
| 138 | |
| 139 | /// NumThroughBlocks - Number of live-through blocks. |
| 140 | unsigned NumThroughBlocks; |
Jakob Stoklund Olesen | bf91c4e | 2011-04-06 03:57:00 +0000 | [diff] [blame] | 141 | |
Jakob Stoklund Olesen | eaa6ed1 | 2011-05-03 20:42:13 +0000 | [diff] [blame] | 142 | /// DidRepairRange - analyze was forced to shrinkToUses(). |
| 143 | bool DidRepairRange; |
| 144 | |
Jakob Stoklund Olesen | b308902 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 145 | // Sumarize statistics by counting instructions using CurLI. |
Jakob Stoklund Olesen | ff09550 | 2010-07-20 16:12:37 +0000 | [diff] [blame] | 146 | void analyzeUses(); |
Jakob Stoklund Olesen | 36d12c6 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 147 | |
Jakob Stoklund Olesen | b1b76ad | 2011-02-09 22:50:26 +0000 | [diff] [blame] | 148 | /// calcLiveBlockInfo - Compute per-block information about CurLI. |
Jakob Stoklund Olesen | 27e0a4a | 2011-03-05 18:33:49 +0000 | [diff] [blame] | 149 | bool calcLiveBlockInfo(); |
Jakob Stoklund Olesen | b1b76ad | 2011-02-09 22:50:26 +0000 | [diff] [blame] | 150 | |
Jakob Stoklund Olesen | 36d12c6 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 151 | public: |
Jakob Stoklund Olesen | f1a60a6 | 2011-02-19 00:53:42 +0000 | [diff] [blame] | 152 | SplitAnalysis(const VirtRegMap &vrm, const LiveIntervals &lis, |
Jakob Stoklund Olesen | 0fef9dd | 2010-07-20 23:50:15 +0000 | [diff] [blame] | 153 | const MachineLoopInfo &mli); |
Jakob Stoklund Olesen | 36d12c6 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 154 | |
Jakob Stoklund Olesen | b308902 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 155 | /// analyze - set CurLI to the specified interval, and analyze how it may be |
Jakob Stoklund Olesen | 36d12c6 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 156 | /// split. |
| 157 | void analyze(const LiveInterval *li); |
| 158 | |
Jakob Stoklund Olesen | eaa6ed1 | 2011-05-03 20:42:13 +0000 | [diff] [blame] | 159 | /// didRepairRange() - Returns true if CurLI was invalid and has been repaired |
| 160 | /// by analyze(). This really shouldn't happen, but sometimes the coalescer |
| 161 | /// can create live ranges that end in mid-air. |
| 162 | bool didRepairRange() const { return DidRepairRange; } |
| 163 | |
Jakob Stoklund Olesen | 36d12c6 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 164 | /// clear - clear all data structures so SplitAnalysis is ready to analyze a |
| 165 | /// new interval. |
| 166 | void clear(); |
| 167 | |
Jakob Stoklund Olesen | 93c8736 | 2011-02-17 19:13:53 +0000 | [diff] [blame] | 168 | /// getParent - Return the last analyzed interval. |
| 169 | const LiveInterval &getParent() const { return *CurLI; } |
| 170 | |
Jakob Stoklund Olesen | 60a26a6 | 2011-02-21 23:09:46 +0000 | [diff] [blame] | 171 | /// isOriginalEndpoint - Return true if the original live range was killed or |
| 172 | /// (re-)defined at Idx. Idx should be the 'def' slot for a normal kill/def, |
| 173 | /// and 'use' for an early-clobber def. |
| 174 | /// This can be used to recognize code inserted by earlier live range |
| 175 | /// splitting. |
| 176 | bool isOriginalEndpoint(SlotIndex Idx) const; |
| 177 | |
Jakob Stoklund Olesen | 994fed6 | 2012-01-12 17:53:44 +0000 | [diff] [blame] | 178 | /// getUseSlots - Return an array of SlotIndexes of instructions using CurLI. |
| 179 | /// This include both use and def operands, at most one entry per instruction. |
| 180 | ArrayRef<SlotIndex> getUseSlots() const { return UseSlots; } |
| 181 | |
Jakob Stoklund Olesen | bf91c4e | 2011-04-06 03:57:00 +0000 | [diff] [blame] | 182 | /// getUseBlocks - Return an array of BlockInfo objects for the basic blocks |
| 183 | /// where CurLI has uses. |
Jakob Stoklund Olesen | 5cc91b2 | 2011-05-28 02:32:57 +0000 | [diff] [blame] | 184 | ArrayRef<BlockInfo> getUseBlocks() const { return UseBlocks; } |
Jakob Stoklund Olesen | bf91c4e | 2011-04-06 03:57:00 +0000 | [diff] [blame] | 185 | |
Jakob Stoklund Olesen | ed47ed4 | 2011-04-09 02:59:09 +0000 | [diff] [blame] | 186 | /// getNumThroughBlocks - Return the number of through blocks. |
| 187 | unsigned getNumThroughBlocks() const { return NumThroughBlocks; } |
| 188 | |
| 189 | /// isThroughBlock - Return true if CurLI is live through MBB without uses. |
| 190 | bool isThroughBlock(unsigned MBB) const { return ThroughBlocks.test(MBB); } |
Jakob Stoklund Olesen | bf91c4e | 2011-04-06 03:57:00 +0000 | [diff] [blame] | 191 | |
Jakob Stoklund Olesen | c49df2c | 2011-04-12 21:30:53 +0000 | [diff] [blame] | 192 | /// getThroughBlocks - Return the set of through blocks. |
| 193 | const BitVector &getThroughBlocks() const { return ThroughBlocks; } |
| 194 | |
Jakob Stoklund Olesen | 5cc91b2 | 2011-05-28 02:32:57 +0000 | [diff] [blame] | 195 | /// getNumLiveBlocks - Return the number of blocks where CurLI is live. |
| 196 | unsigned getNumLiveBlocks() const { |
Jakob Stoklund Olesen | ec43d5d | 2011-05-30 01:33:26 +0000 | [diff] [blame] | 197 | return getUseBlocks().size() - NumGapBlocks + getNumThroughBlocks(); |
Jakob Stoklund Olesen | 5cc91b2 | 2011-05-28 02:32:57 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | /// countLiveBlocks - Return the number of blocks where li is live. This is |
| 201 | /// guaranteed to return the same number as getNumLiveBlocks() after calling |
| 202 | /// analyze(li). |
Jakob Stoklund Olesen | eef2327 | 2011-04-26 22:33:12 +0000 | [diff] [blame] | 203 | unsigned countLiveBlocks(const LiveInterval *li) const; |
| 204 | |
Jakob Stoklund Olesen | ed4075c | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 205 | typedef SmallPtrSet<const MachineBasicBlock*, 16> BlockPtrSet; |
| 206 | |
Jakob Stoklund Olesen | 8627ea9 | 2011-08-05 22:20:45 +0000 | [diff] [blame] | 207 | /// shouldSplitSingleBlock - Returns true if it would help to create a local |
| 208 | /// live range for the instructions in BI. There is normally no benefit to |
| 209 | /// creating a live range for a single instruction, but it does enable |
| 210 | /// register class inflation if the instruction has a restricted register |
| 211 | /// class. |
| 212 | /// |
| 213 | /// @param BI The block to be isolated. |
| 214 | /// @param SingleInstrs True when single instructions should be isolated. |
| 215 | bool shouldSplitSingleBlock(const BlockInfo &BI, bool SingleInstrs) const; |
Wei Mi | 35ee933 | 2016-05-11 22:28:29 +0000 | [diff] [blame^] | 216 | |
| 217 | SlotIndex getLastSplitPoint(unsigned Num) { |
| 218 | return IPA.getLastInsertPoint(*MF.getBlockNumbered(Num)); |
| 219 | } |
| 220 | |
| 221 | MachineBasicBlock::iterator getLastSplitPointIter(MachineBasicBlock *BB) { |
| 222 | return IPA.getLastInsertPointIter(*BB); |
| 223 | } |
Jakob Stoklund Olesen | 36d12c6 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 224 | }; |
| 225 | |
Jakob Stoklund Olesen | ce6f055 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 226 | |
Jakob Stoklund Olesen | c698417 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 227 | /// SplitEditor - Edit machine code and LiveIntervals for live range |
| 228 | /// splitting. |
| 229 | /// |
Jakob Stoklund Olesen | 45e07c8 | 2010-08-06 22:17:33 +0000 | [diff] [blame] | 230 | /// - Create a SplitEditor from a SplitAnalysis. |
Jakob Stoklund Olesen | dc96e28 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 231 | /// - Start a new live interval with openIntv. |
| 232 | /// - Mark the places where the new interval is entered using enterIntv* |
| 233 | /// - Mark the ranges where the new interval is used with useIntv* |
| 234 | /// - Mark the places where the interval is exited with exitIntv*. |
| 235 | /// - Finish the current interval with closeIntv and repeat from 2. |
Jakob Stoklund Olesen | 959fcc6 | 2010-10-08 23:42:21 +0000 | [diff] [blame] | 236 | /// - Rewrite instructions with finish(). |
Jakob Stoklund Olesen | c698417 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 237 | /// |
Benjamin Kramer | f4c2025 | 2015-07-01 14:47:39 +0000 | [diff] [blame] | 238 | class LLVM_LIBRARY_VISIBILITY SplitEditor { |
Jakob Stoklund Olesen | 04aff70 | 2011-02-19 00:42:33 +0000 | [diff] [blame] | 239 | SplitAnalysis &SA; |
Jakob Stoklund Olesen | b308902 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 240 | LiveIntervals &LIS; |
| 241 | VirtRegMap &VRM; |
| 242 | MachineRegisterInfo &MRI; |
Eric Christopher | ede6267 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 243 | MachineDominatorTree &MDT; |
Jakob Stoklund Olesen | b308902 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 244 | const TargetInstrInfo &TII; |
| 245 | const TargetRegisterInfo &TRI; |
Benjamin Kramer | e2a1d89 | 2013-06-17 19:00:36 +0000 | [diff] [blame] | 246 | const MachineBlockFrequencyInfo &MBFI; |
Jakob Stoklund Olesen | c698417 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 247 | |
Jakob Stoklund Olesen | eecb2fb | 2011-09-12 16:49:21 +0000 | [diff] [blame] | 248 | public: |
| 249 | |
| 250 | /// ComplementSpillMode - Select how the complement live range should be |
| 251 | /// created. SplitEditor automatically creates interval 0 to contain |
| 252 | /// anything that isn't added to another interval. This complement interval |
| 253 | /// can get quite complicated, and it can sometimes be an advantage to allow |
| 254 | /// it to overlap the other intervals. If it is going to spill anyway, no |
| 255 | /// registers are wasted by keeping a value in two places at the same time. |
| 256 | enum ComplementSpillMode { |
| 257 | /// SM_Partition(Default) - Try to create the complement interval so it |
| 258 | /// doesn't overlap any other intervals, and the original interval is |
| 259 | /// partitioned. This may require a large number of back copies and extra |
| 260 | /// PHI-defs. Only segments marked with overlapIntv will be overlapping. |
| 261 | SM_Partition, |
| 262 | |
| 263 | /// SM_Size - Overlap intervals to minimize the number of inserted COPY |
| 264 | /// instructions. Copies to the complement interval are hoisted to their |
| 265 | /// common dominator, so only one COPY is required per value in the |
| 266 | /// complement interval. This also means that no extra PHI-defs need to be |
| 267 | /// inserted in the complement interval. |
| 268 | SM_Size, |
| 269 | |
| 270 | /// SM_Speed - Overlap intervals to minimize the expected execution |
| 271 | /// frequency of the inserted copies. This is very similar to SM_Size, but |
| 272 | /// the complement interval may get some extra PHI-defs. |
| 273 | SM_Speed |
| 274 | }; |
| 275 | |
| 276 | private: |
| 277 | |
Jakob Stoklund Olesen | b308902 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 278 | /// Edit - The current parent register and new intervals created. |
Jakob Stoklund Olesen | 815196c | 2011-03-02 23:31:50 +0000 | [diff] [blame] | 279 | LiveRangeEdit *Edit; |
Jakob Stoklund Olesen | 72911e4 | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 280 | |
Eric Christopher | ede6267 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 281 | /// Index into Edit of the currently open interval. |
| 282 | /// The index 0 is used for the complement, so the first interval started by |
| 283 | /// openIntv will be 1. |
| 284 | unsigned OpenIdx; |
Jakob Stoklund Olesen | c698417 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 285 | |
Jakob Stoklund Olesen | eecb2fb | 2011-09-12 16:49:21 +0000 | [diff] [blame] | 286 | /// The current spill mode, selected by reset(). |
| 287 | ComplementSpillMode SpillMode; |
| 288 | |
Eric Christopher | ede6267 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 289 | typedef IntervalMap<SlotIndex, unsigned> RegAssignMap; |
| 290 | |
| 291 | /// Allocator for the interval map. This will eventually be shared with |
| 292 | /// SlotIndexes and LiveIntervals. |
| 293 | RegAssignMap::Allocator Allocator; |
| 294 | |
| 295 | /// RegAssign - Map of the assigned register indexes. |
| 296 | /// Edit.get(RegAssign.lookup(Idx)) is the register that should be live at |
| 297 | /// Idx. |
| 298 | RegAssignMap RegAssign; |
| 299 | |
Jakob Stoklund Olesen | 5d4277d | 2011-09-13 23:09:04 +0000 | [diff] [blame] | 300 | typedef PointerIntPair<VNInfo*, 1> ValueForcePair; |
| 301 | typedef DenseMap<std::pair<unsigned, unsigned>, ValueForcePair> ValueMap; |
Jakob Stoklund Olesen | 8ef91fc | 2011-03-01 23:14:53 +0000 | [diff] [blame] | 302 | |
| 303 | /// Values - keep track of the mapping from parent values to values in the new |
| 304 | /// intervals. Given a pair (RegIdx, ParentVNI->id), Values contains: |
| 305 | /// |
| 306 | /// 1. No entry - the value is not mapped to Edit.get(RegIdx). |
Jakob Stoklund Olesen | 5d4277d | 2011-09-13 23:09:04 +0000 | [diff] [blame] | 307 | /// 2. (Null, false) - the value is mapped to multiple values in |
| 308 | /// Edit.get(RegIdx). Each value is represented by a minimal live range at |
| 309 | /// its def. The full live range can be inferred exactly from the range |
| 310 | /// of RegIdx in RegAssign. |
| 311 | /// 3. (Null, true). As above, but the ranges in RegAssign are too large, and |
| 312 | /// the live range must be recomputed using LiveRangeCalc::extend(). |
| 313 | /// 4. (VNI, false) The value is mapped to a single new value. |
Jakob Stoklund Olesen | 8ef91fc | 2011-03-01 23:14:53 +0000 | [diff] [blame] | 314 | /// The new value has no live ranges anywhere. |
| 315 | ValueMap Values; |
| 316 | |
Jakob Stoklund Olesen | 054984d | 2011-09-13 16:47:53 +0000 | [diff] [blame] | 317 | /// LRCalc - Cache for computing live ranges and SSA update. Each instance |
| 318 | /// can only handle non-overlapping live ranges, so use a separate |
| 319 | /// LiveRangeCalc instance for the complement interval when in spill mode. |
| 320 | LiveRangeCalc LRCalc[2]; |
| 321 | |
| 322 | /// getLRCalc - Return the LRCalc to use for RegIdx. In spill mode, the |
| 323 | /// complement interval can overlap the other intervals, so it gets its own |
| 324 | /// LRCalc instance. When not in spill mode, all intervals can share one. |
| 325 | LiveRangeCalc &getLRCalc(unsigned RegIdx) { |
| 326 | return LRCalc[SpillMode != SM_Partition && RegIdx != 0]; |
| 327 | } |
Jakob Stoklund Olesen | 1af8b4d | 2011-04-15 17:24:49 +0000 | [diff] [blame] | 328 | |
Jakob Stoklund Olesen | 8ef91fc | 2011-03-01 23:14:53 +0000 | [diff] [blame] | 329 | /// defValue - define a value in RegIdx from ParentVNI at Idx. |
| 330 | /// Idx does not have to be ParentVNI->def, but it must be contained within |
| 331 | /// ParentVNI's live range in ParentLI. The new value is added to the value |
| 332 | /// map. |
| 333 | /// Return the new LI value. |
| 334 | VNInfo *defValue(unsigned RegIdx, const VNInfo *ParentVNI, SlotIndex Idx); |
| 335 | |
Jakob Stoklund Olesen | 5d4277d | 2011-09-13 23:09:04 +0000 | [diff] [blame] | 336 | /// forceRecompute - Force the live range of ParentVNI in RegIdx to be |
| 337 | /// recomputed by LiveRangeCalc::extend regardless of the number of defs. |
| 338 | /// This is used for values whose live range doesn't match RegAssign exactly. |
| 339 | /// They could have rematerialized, or back-copies may have been moved. |
| 340 | void forceRecompute(unsigned RegIdx, const VNInfo *ParentVNI); |
Jakob Stoklund Olesen | 4484f99 | 2011-09-13 18:05:29 +0000 | [diff] [blame] | 341 | |
Jakob Stoklund Olesen | 6ee7d9aa | 2010-11-10 19:31:50 +0000 | [diff] [blame] | 342 | /// defFromParent - Define Reg from ParentVNI at UseIdx using either |
| 343 | /// rematerialization or a COPY from parent. Return the new value. |
Eric Christopher | ede6267 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 344 | VNInfo *defFromParent(unsigned RegIdx, |
Jakob Stoklund Olesen | 6ee7d9aa | 2010-11-10 19:31:50 +0000 | [diff] [blame] | 345 | VNInfo *ParentVNI, |
| 346 | SlotIndex UseIdx, |
| 347 | MachineBasicBlock &MBB, |
| 348 | MachineBasicBlock::iterator I); |
| 349 | |
Jakob Stoklund Olesen | a25330f | 2011-09-13 22:22:39 +0000 | [diff] [blame] | 350 | /// removeBackCopies - Remove the copy instructions that defines the values |
| 351 | /// in the vector in the complement interval. |
| 352 | void removeBackCopies(SmallVectorImpl<VNInfo*> &Copies); |
| 353 | |
Jakob Stoklund Olesen | a98af39 | 2011-09-14 16:45:39 +0000 | [diff] [blame] | 354 | /// getShallowDominator - Returns the least busy dominator of MBB that is |
| 355 | /// also dominated by DefMBB. Busy is measured by loop depth. |
| 356 | MachineBasicBlock *findShallowDominator(MachineBasicBlock *MBB, |
| 357 | MachineBasicBlock *DefMBB); |
| 358 | |
Wei Mi | 9a16d65 | 2016-04-13 03:08:27 +0000 | [diff] [blame] | 359 | /// Find out all the backCopies dominated by others. |
| 360 | void computeRedundantBackCopies(DenseSet<unsigned> &NotToHoistSet, |
| 361 | SmallVectorImpl<VNInfo *> &BackCopies); |
| 362 | |
| 363 | /// Hoist back-copies to the complement interval. It tries to hoist all |
| 364 | /// the back-copies to one BB if it is beneficial, or else simply remove |
Eric Christopher | 75d661a | 2016-05-04 21:45:36 +0000 | [diff] [blame] | 365 | /// redundant backcopies dominated by others. |
Wei Mi | 9a16d65 | 2016-04-13 03:08:27 +0000 | [diff] [blame] | 366 | void hoistCopies(); |
Jakob Stoklund Olesen | a25330f | 2011-09-13 22:22:39 +0000 | [diff] [blame] | 367 | |
Jakob Stoklund Olesen | 1af8b4d | 2011-04-15 17:24:49 +0000 | [diff] [blame] | 368 | /// transferValues - Transfer values to the new ranges. |
| 369 | /// Return true if any ranges were skipped. |
| 370 | bool transferValues(); |
Jakob Stoklund Olesen | 503b143 | 2011-03-02 23:05:19 +0000 | [diff] [blame] | 371 | |
Jakob Stoklund Olesen | 3648263 | 2011-03-02 23:05:16 +0000 | [diff] [blame] | 372 | /// extendPHIKillRanges - Extend the ranges of all values killed by original |
| 373 | /// parent PHIDefs. |
| 374 | void extendPHIKillRanges(); |
| 375 | |
Eric Christopher | ede6267 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 376 | /// rewriteAssigned - Rewrite all uses of Edit.getReg() to assigned registers. |
Jakob Stoklund Olesen | 503b143 | 2011-03-02 23:05:19 +0000 | [diff] [blame] | 377 | void rewriteAssigned(bool ExtendRanges); |
Jakob Stoklund Olesen | 6f8bd42 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 378 | |
Jakob Stoklund Olesen | ea5ebfe | 2011-03-08 22:46:11 +0000 | [diff] [blame] | 379 | /// deleteRematVictims - Delete defs that are dead after rematerializing. |
| 380 | void deleteRematVictims(); |
| 381 | |
Jakob Stoklund Olesen | c698417 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 382 | public: |
| 383 | /// Create a new SplitEditor for editing the LiveInterval analyzed by SA. |
Jakob Stoklund Olesen | dc96e28 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 384 | /// Newly created intervals will be appended to newIntervals. |
Jakob Stoklund Olesen | e172a8b | 2010-10-28 20:34:50 +0000 | [diff] [blame] | 385 | SplitEditor(SplitAnalysis &SA, LiveIntervals&, VirtRegMap&, |
Benjamin Kramer | e2a1d89 | 2013-06-17 19:00:36 +0000 | [diff] [blame] | 386 | MachineDominatorTree&, MachineBlockFrequencyInfo &); |
Jakob Stoklund Olesen | c698417 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 387 | |
Jakob Stoklund Olesen | c960198 | 2011-03-03 01:29:13 +0000 | [diff] [blame] | 388 | /// reset - Prepare for a new split. |
Jakob Stoklund Olesen | eecb2fb | 2011-09-12 16:49:21 +0000 | [diff] [blame] | 389 | void reset(LiveRangeEdit&, ComplementSpillMode = SM_Partition); |
Jakob Stoklund Olesen | c698417 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 390 | |
Jakob Stoklund Olesen | dc96e28 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 391 | /// Create a new virtual register and live interval. |
Jakob Stoklund Olesen | 0840f50 | 2011-04-12 18:11:31 +0000 | [diff] [blame] | 392 | /// Return the interval index, starting from 1. Interval index 0 is the |
| 393 | /// implicit complement interval. |
| 394 | unsigned openIntv(); |
| 395 | |
| 396 | /// currentIntv - Return the current interval index. |
| 397 | unsigned currentIntv() const { return OpenIdx; } |
| 398 | |
| 399 | /// selectIntv - Select a previously opened interval index. |
| 400 | void selectIntv(unsigned Idx); |
Jakob Stoklund Olesen | c698417 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 401 | |
Jakob Stoklund Olesen | f12e120 | 2011-02-03 17:04:16 +0000 | [diff] [blame] | 402 | /// enterIntvBefore - Enter the open interval before the instruction at Idx. |
| 403 | /// If the parent interval is not live before Idx, a COPY is not inserted. |
| 404 | /// Return the beginning of the new live range. |
| 405 | SlotIndex enterIntvBefore(SlotIndex Idx); |
Jakob Stoklund Olesen | 622848b | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 406 | |
Jakob Stoklund Olesen | adc6a4c | 2011-06-30 01:30:39 +0000 | [diff] [blame] | 407 | /// enterIntvAfter - Enter the open interval after the instruction at Idx. |
| 408 | /// Return the beginning of the new live range. |
| 409 | SlotIndex enterIntvAfter(SlotIndex Idx); |
| 410 | |
Jakob Stoklund Olesen | f12e120 | 2011-02-03 17:04:16 +0000 | [diff] [blame] | 411 | /// enterIntvAtEnd - Enter the open interval at the end of MBB. |
Eric Christopher | 650c8f2 | 2014-05-20 17:11:11 +0000 | [diff] [blame] | 412 | /// Use the open interval from the inserted copy to the MBB end. |
Jakob Stoklund Olesen | f12e120 | 2011-02-03 17:04:16 +0000 | [diff] [blame] | 413 | /// Return the beginning of the new live range. |
| 414 | SlotIndex enterIntvAtEnd(MachineBasicBlock &MBB); |
Jakob Stoklund Olesen | c698417 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 415 | |
Jakob Stoklund Olesen | b308902 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 416 | /// useIntv - indicate that all instructions in MBB should use OpenLI. |
Jakob Stoklund Olesen | dc96e28 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 417 | void useIntv(const MachineBasicBlock &MBB); |
Jakob Stoklund Olesen | c698417 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 418 | |
Jakob Stoklund Olesen | b308902 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 419 | /// useIntv - indicate that all instructions in range should use OpenLI. |
Jakob Stoklund Olesen | dc96e28 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 420 | void useIntv(SlotIndex Start, SlotIndex End); |
Jakob Stoklund Olesen | c698417 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 421 | |
Jakob Stoklund Olesen | f12e120 | 2011-02-03 17:04:16 +0000 | [diff] [blame] | 422 | /// leaveIntvAfter - Leave the open interval after the instruction at Idx. |
| 423 | /// Return the end of the live range. |
| 424 | SlotIndex leaveIntvAfter(SlotIndex Idx); |
Jakob Stoklund Olesen | 622848b | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 425 | |
Jakob Stoklund Olesen | 7cb57b3 | 2011-02-09 23:30:25 +0000 | [diff] [blame] | 426 | /// leaveIntvBefore - Leave the open interval before the instruction at Idx. |
| 427 | /// Return the end of the live range. |
| 428 | SlotIndex leaveIntvBefore(SlotIndex Idx); |
| 429 | |
Jakob Stoklund Olesen | dc96e28 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 430 | /// leaveIntvAtTop - Leave the interval at the top of MBB. |
Jakob Stoklund Olesen | f12e120 | 2011-02-03 17:04:16 +0000 | [diff] [blame] | 431 | /// Add liveness from the MBB top to the copy. |
| 432 | /// Return the end of the live range. |
| 433 | SlotIndex leaveIntvAtTop(MachineBasicBlock &MBB); |
Jakob Stoklund Olesen | c698417 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 434 | |
Jakob Stoklund Olesen | 1749935 | 2011-02-08 18:50:21 +0000 | [diff] [blame] | 435 | /// overlapIntv - Indicate that all instructions in range should use the open |
| 436 | /// interval, but also let the complement interval be live. |
| 437 | /// |
| 438 | /// This doubles the register pressure, but is sometimes required to deal with |
| 439 | /// register uses after the last valid split point. |
| 440 | /// |
| 441 | /// The Start index should be a return value from a leaveIntv* call, and End |
| 442 | /// should be in the same basic block. The parent interval must have the same |
| 443 | /// value across the range. |
| 444 | /// |
| 445 | void overlapIntv(SlotIndex Start, SlotIndex End); |
| 446 | |
Jakob Stoklund Olesen | 959fcc6 | 2010-10-08 23:42:21 +0000 | [diff] [blame] | 447 | /// finish - after all the new live ranges have been created, compute the |
| 448 | /// remaining live range, and rewrite instructions to use the new registers. |
Jakob Stoklund Olesen | 6a663b8 | 2011-04-21 18:38:15 +0000 | [diff] [blame] | 449 | /// @param LRMap When not null, this vector will map each live range in Edit |
| 450 | /// back to the indices returned by openIntv. |
| 451 | /// There may be extra indices created by dead code elimination. |
Craig Topper | ada0857 | 2014-04-16 04:21:27 +0000 | [diff] [blame] | 452 | void finish(SmallVectorImpl<unsigned> *LRMap = nullptr); |
Jakob Stoklund Olesen | c698417 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 453 | |
Jim Grosbach | bfe3a9c | 2015-05-02 00:44:07 +0000 | [diff] [blame] | 454 | /// dump - print the current interval mapping to dbgs(). |
Eric Christopher | ede6267 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 455 | void dump() const; |
| 456 | |
Jakob Stoklund Olesen | c698417 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 457 | // ===--- High level methods ---=== |
| 458 | |
Jakob Stoklund Olesen | c70b697 | 2011-04-12 19:32:53 +0000 | [diff] [blame] | 459 | /// splitSingleBlock - Split CurLI into a separate live interval around the |
| 460 | /// uses in a single block. This is intended to be used as part of a larger |
| 461 | /// split, and doesn't call finish(). |
| 462 | void splitSingleBlock(const SplitAnalysis::BlockInfo &BI); |
| 463 | |
Jakob Stoklund Olesen | 795da1c | 2011-07-15 21:47:57 +0000 | [diff] [blame] | 464 | /// splitLiveThroughBlock - Split CurLI in the given block such that it |
| 465 | /// enters the block in IntvIn and leaves it in IntvOut. There may be uses in |
| 466 | /// the block, but they will be ignored when placing split points. |
| 467 | /// |
| 468 | /// @param MBBNum Block number. |
| 469 | /// @param IntvIn Interval index entering the block. |
| 470 | /// @param LeaveBefore When set, leave IntvIn before this point. |
| 471 | /// @param IntvOut Interval index leaving the block. |
| 472 | /// @param EnterAfter When set, enter IntvOut after this point. |
| 473 | void splitLiveThroughBlock(unsigned MBBNum, |
| 474 | unsigned IntvIn, SlotIndex LeaveBefore, |
| 475 | unsigned IntvOut, SlotIndex EnterAfter); |
| 476 | |
| 477 | /// splitRegInBlock - Split CurLI in the given block such that it enters the |
| 478 | /// block in IntvIn and leaves it on the stack (or not at all). Split points |
| 479 | /// are placed in a way that avoids putting uses in the stack interval. This |
| 480 | /// may require creating a local interval when there is interference. |
| 481 | /// |
| 482 | /// @param BI Block descriptor. |
| 483 | /// @param IntvIn Interval index entering the block. Not 0. |
| 484 | /// @param LeaveBefore When set, leave IntvIn before this point. |
| 485 | void splitRegInBlock(const SplitAnalysis::BlockInfo &BI, |
| 486 | unsigned IntvIn, SlotIndex LeaveBefore); |
| 487 | |
| 488 | /// splitRegOutBlock - Split CurLI in the given block such that it enters the |
| 489 | /// block on the stack (or isn't live-in at all) and leaves it in IntvOut. |
| 490 | /// Split points are placed to avoid interference and such that the uses are |
| 491 | /// not in the stack interval. This may require creating a local interval |
| 492 | /// when there is interference. |
| 493 | /// |
| 494 | /// @param BI Block descriptor. |
| 495 | /// @param IntvOut Interval index leaving the block. |
| 496 | /// @param EnterAfter When set, enter IntvOut after this point. |
| 497 | void splitRegOutBlock(const SplitAnalysis::BlockInfo &BI, |
| 498 | unsigned IntvOut, SlotIndex EnterAfter); |
Jakob Stoklund Olesen | d1191ee | 2010-08-13 21:18:48 +0000 | [diff] [blame] | 499 | }; |
Jakob Stoklund Olesen | 36d12c6 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 500 | |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 501 | } |
Sebastian Redl | b8a62aa | 2011-04-24 15:46:51 +0000 | [diff] [blame] | 502 | |
| 503 | #endif |