Jakob Stoklund Olesen | 8dd070e | 2011-01-04 21:10:05 +0000 | [diff] [blame] | 1 | //===-------- SplitKit.h - Toolkit for splitting live ranges ----*- C++ -*-===// |
Jakob Stoklund Olesen | 8ae0263 | 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 | |
Sebastian Redl | 6796e4f | 2011-04-24 15:46:51 +0000 | [diff] [blame] | 15 | #ifndef LLVM_CODEGEN_SPLITKIT_H |
| 16 | #define LLVM_CODEGEN_SPLITKIT_H |
| 17 | |
Jakob Stoklund Olesen | b5a457c | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 18 | #include "LiveRangeCalc.h" |
Jakob Stoklund Olesen | db529a8 | 2011-04-06 03:57:00 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/ArrayRef.h" |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/DenseMap.h" |
Eric Christopher | 0f43811 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/IntervalMap.h" |
Jakob Stoklund Olesen | 8d0963f | 2010-12-21 01:50:21 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/SmallPtrSet.h" |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 23 | |
| 24 | namespace llvm { |
| 25 | |
Eric Christopher | 0f43811 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 26 | class ConnectedVNInfoEqClasses; |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 27 | class LiveInterval; |
| 28 | class LiveIntervals; |
Jakob Stoklund Olesen | a17768f | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 29 | class LiveRangeEdit; |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 30 | class MachineInstr; |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 31 | class MachineLoopInfo; |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 32 | class MachineRegisterInfo; |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 33 | class TargetInstrInfo; |
Jakob Stoklund Olesen | cfa7134 | 2010-11-10 19:31:50 +0000 | [diff] [blame] | 34 | class TargetRegisterInfo; |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 35 | class VirtRegMap; |
| 36 | class VNInfo; |
Jakob Stoklund Olesen | 532de3d | 2010-10-22 20:28:21 +0000 | [diff] [blame] | 37 | class raw_ostream; |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 38 | |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 39 | /// SplitAnalysis - Analyze a LiveInterval, looking for live range splitting |
| 40 | /// opportunities. |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 41 | class SplitAnalysis { |
Jakob Stoklund Olesen | 08e93b1 | 2010-08-10 17:07:22 +0000 | [diff] [blame] | 42 | public: |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 43 | const MachineFunction &MF; |
Jakob Stoklund Olesen | 1b847de | 2011-02-19 00:53:42 +0000 | [diff] [blame] | 44 | const VirtRegMap &VRM; |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 45 | const LiveIntervals &LIS; |
| 46 | const MachineLoopInfo &Loops; |
| 47 | const TargetInstrInfo &TII; |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 48 | |
Jakob Stoklund Olesen | b5fa933 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 49 | // Sorted slot indexes of using instructions. |
| 50 | SmallVector<SlotIndex, 8> UseSlots; |
| 51 | |
Jakob Stoklund Olesen | f0ac26c | 2011-02-09 22:50:26 +0000 | [diff] [blame] | 52 | /// Additional information about basic blocks where the current variable is |
| 53 | /// live. Such a block will look like one of these templates: |
| 54 | /// |
| 55 | /// 1. | o---x | Internal to block. Variable is only live in this block. |
| 56 | /// 2. |---x | Live-in, kill. |
| 57 | /// 3. | o---| Def, live-out. |
Jakob Stoklund Olesen | a2e79ef | 2011-05-30 01:33:26 +0000 | [diff] [blame] | 58 | /// 4. |---x o---| Live-in, kill, def, live-out. Counted by NumGapBlocks. |
Jakob Stoklund Olesen | f0ac26c | 2011-02-09 22:50:26 +0000 | [diff] [blame] | 59 | /// 5. |---o---o---| Live-through with uses or defs. |
Jakob Stoklund Olesen | a2e79ef | 2011-05-30 01:33:26 +0000 | [diff] [blame] | 60 | /// 6. |-----------| Live-through without uses. Counted by NumThroughBlocks. |
| 61 | /// |
| 62 | /// Two BlockInfo entries are created for template 4. One for the live-in |
| 63 | /// segment, and one for the live-out segment. These entries look as if the |
| 64 | /// block were split in the middle where the live range isn't live. |
| 65 | /// |
| 66 | /// Live-through blocks without any uses don't get BlockInfo entries. They |
| 67 | /// are simply listed in ThroughBlocks instead. |
Jakob Stoklund Olesen | f0ac26c | 2011-02-09 22:50:26 +0000 | [diff] [blame] | 68 | /// |
| 69 | struct BlockInfo { |
| 70 | MachineBasicBlock *MBB; |
Jakob Stoklund Olesen | fe62d92 | 2011-08-02 22:54:14 +0000 | [diff] [blame] | 71 | SlotIndex FirstInstr; ///< First instr accessing current reg. |
| 72 | SlotIndex LastInstr; ///< Last instr accessing current reg. |
Jakob Stoklund Olesen | 77ee114 | 2011-08-02 22:37:22 +0000 | [diff] [blame] | 73 | SlotIndex FirstDef; ///< First non-phi valno->def, or SlotIndex(). |
Jakob Stoklund Olesen | f0ac26c | 2011-02-09 22:50:26 +0000 | [diff] [blame] | 74 | bool LiveIn; ///< Current reg is live in. |
| 75 | bool LiveOut; ///< Current reg is live out. |
Jakob Stoklund Olesen | 87360f7 | 2011-06-30 01:30:39 +0000 | [diff] [blame] | 76 | |
| 77 | /// isOneInstr - Returns true when this BlockInfo describes a single |
| 78 | /// instruction. |
| 79 | bool isOneInstr() const { |
Jakob Stoklund Olesen | fe62d92 | 2011-08-02 22:54:14 +0000 | [diff] [blame] | 80 | return SlotIndex::isSameInstr(FirstInstr, LastInstr); |
Jakob Stoklund Olesen | 87360f7 | 2011-06-30 01:30:39 +0000 | [diff] [blame] | 81 | } |
Jakob Stoklund Olesen | f0ac26c | 2011-02-09 22:50:26 +0000 | [diff] [blame] | 82 | }; |
| 83 | |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 84 | private: |
| 85 | // Current live interval. |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 86 | const LiveInterval *CurLI; |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 87 | |
Jakob Stoklund Olesen | 1a77445 | 2011-04-05 04:20:27 +0000 | [diff] [blame] | 88 | /// LastSplitPoint - Last legal split point in each basic block in the current |
| 89 | /// function. The first entry is the first terminator, the second entry is the |
| 90 | /// last valid split point for a variable that is live in to a landing pad |
| 91 | /// successor. |
| 92 | SmallVector<std::pair<SlotIndex, SlotIndex>, 8> LastSplitPoint; |
| 93 | |
Jakob Stoklund Olesen | db529a8 | 2011-04-06 03:57:00 +0000 | [diff] [blame] | 94 | /// UseBlocks - Blocks where CurLI has uses. |
| 95 | SmallVector<BlockInfo, 8> UseBlocks; |
| 96 | |
Jakob Stoklund Olesen | a2e79ef | 2011-05-30 01:33:26 +0000 | [diff] [blame] | 97 | /// NumGapBlocks - Number of duplicate entries in UseBlocks for blocks where |
| 98 | /// the live range has a gap. |
| 99 | unsigned NumGapBlocks; |
| 100 | |
Jakob Stoklund Olesen | db529a8 | 2011-04-06 03:57:00 +0000 | [diff] [blame] | 101 | /// ThroughBlocks - Block numbers where CurLI is live through without uses. |
Jakob Stoklund Olesen | f4afdfc | 2011-04-09 02:59:09 +0000 | [diff] [blame] | 102 | BitVector ThroughBlocks; |
| 103 | |
| 104 | /// NumThroughBlocks - Number of live-through blocks. |
| 105 | unsigned NumThroughBlocks; |
Jakob Stoklund Olesen | db529a8 | 2011-04-06 03:57:00 +0000 | [diff] [blame] | 106 | |
Jakob Stoklund Olesen | 7d6b6a0 | 2011-05-03 20:42:13 +0000 | [diff] [blame] | 107 | /// DidRepairRange - analyze was forced to shrinkToUses(). |
| 108 | bool DidRepairRange; |
| 109 | |
Jakob Stoklund Olesen | 1a77445 | 2011-04-05 04:20:27 +0000 | [diff] [blame] | 110 | SlotIndex computeLastSplitPoint(unsigned Num); |
| 111 | |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 112 | // Sumarize statistics by counting instructions using CurLI. |
Jakob Stoklund Olesen | abff280 | 2010-07-20 16:12:37 +0000 | [diff] [blame] | 113 | void analyzeUses(); |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 114 | |
Jakob Stoklund Olesen | f0ac26c | 2011-02-09 22:50:26 +0000 | [diff] [blame] | 115 | /// calcLiveBlockInfo - Compute per-block information about CurLI. |
Jakob Stoklund Olesen | 2b0f9e7 | 2011-03-05 18:33:49 +0000 | [diff] [blame] | 116 | bool calcLiveBlockInfo(); |
Jakob Stoklund Olesen | f0ac26c | 2011-02-09 22:50:26 +0000 | [diff] [blame] | 117 | |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 118 | public: |
Jakob Stoklund Olesen | 1b847de | 2011-02-19 00:53:42 +0000 | [diff] [blame] | 119 | SplitAnalysis(const VirtRegMap &vrm, const LiveIntervals &lis, |
Jakob Stoklund Olesen | f2c6e36 | 2010-07-20 23:50:15 +0000 | [diff] [blame] | 120 | const MachineLoopInfo &mli); |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 121 | |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 122 | /// analyze - set CurLI to the specified interval, and analyze how it may be |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 123 | /// split. |
| 124 | void analyze(const LiveInterval *li); |
| 125 | |
Jakob Stoklund Olesen | 7d6b6a0 | 2011-05-03 20:42:13 +0000 | [diff] [blame] | 126 | /// didRepairRange() - Returns true if CurLI was invalid and has been repaired |
| 127 | /// by analyze(). This really shouldn't happen, but sometimes the coalescer |
| 128 | /// can create live ranges that end in mid-air. |
| 129 | bool didRepairRange() const { return DidRepairRange; } |
| 130 | |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 131 | /// clear - clear all data structures so SplitAnalysis is ready to analyze a |
| 132 | /// new interval. |
| 133 | void clear(); |
| 134 | |
Jakob Stoklund Olesen | 034a80d | 2011-02-17 19:13:53 +0000 | [diff] [blame] | 135 | /// getParent - Return the last analyzed interval. |
| 136 | const LiveInterval &getParent() const { return *CurLI; } |
| 137 | |
Jakob Stoklund Olesen | 1a77445 | 2011-04-05 04:20:27 +0000 | [diff] [blame] | 138 | /// getLastSplitPoint - Return that base index of the last valid split point |
| 139 | /// in the basic block numbered Num. |
| 140 | SlotIndex getLastSplitPoint(unsigned Num) { |
| 141 | // Inline the common simple case. |
| 142 | if (LastSplitPoint[Num].first.isValid() && |
| 143 | !LastSplitPoint[Num].second.isValid()) |
| 144 | return LastSplitPoint[Num].first; |
| 145 | return computeLastSplitPoint(Num); |
| 146 | } |
| 147 | |
Jakob Stoklund Olesen | 06c0f25 | 2011-02-21 23:09:46 +0000 | [diff] [blame] | 148 | /// isOriginalEndpoint - Return true if the original live range was killed or |
| 149 | /// (re-)defined at Idx. Idx should be the 'def' slot for a normal kill/def, |
| 150 | /// and 'use' for an early-clobber def. |
| 151 | /// This can be used to recognize code inserted by earlier live range |
| 152 | /// splitting. |
| 153 | bool isOriginalEndpoint(SlotIndex Idx) const; |
| 154 | |
Jakob Stoklund Olesen | db529a8 | 2011-04-06 03:57:00 +0000 | [diff] [blame] | 155 | /// getUseBlocks - Return an array of BlockInfo objects for the basic blocks |
| 156 | /// where CurLI has uses. |
Jakob Stoklund Olesen | b2abfa0 | 2011-05-28 02:32:57 +0000 | [diff] [blame] | 157 | ArrayRef<BlockInfo> getUseBlocks() const { return UseBlocks; } |
Jakob Stoklund Olesen | db529a8 | 2011-04-06 03:57:00 +0000 | [diff] [blame] | 158 | |
Jakob Stoklund Olesen | f4afdfc | 2011-04-09 02:59:09 +0000 | [diff] [blame] | 159 | /// getNumThroughBlocks - Return the number of through blocks. |
| 160 | unsigned getNumThroughBlocks() const { return NumThroughBlocks; } |
| 161 | |
| 162 | /// isThroughBlock - Return true if CurLI is live through MBB without uses. |
| 163 | bool isThroughBlock(unsigned MBB) const { return ThroughBlocks.test(MBB); } |
Jakob Stoklund Olesen | db529a8 | 2011-04-06 03:57:00 +0000 | [diff] [blame] | 164 | |
Jakob Stoklund Olesen | 5db4289 | 2011-04-12 21:30:53 +0000 | [diff] [blame] | 165 | /// getThroughBlocks - Return the set of through blocks. |
| 166 | const BitVector &getThroughBlocks() const { return ThroughBlocks; } |
| 167 | |
Jakob Stoklund Olesen | b2abfa0 | 2011-05-28 02:32:57 +0000 | [diff] [blame] | 168 | /// getNumLiveBlocks - Return the number of blocks where CurLI is live. |
| 169 | unsigned getNumLiveBlocks() const { |
Jakob Stoklund Olesen | a2e79ef | 2011-05-30 01:33:26 +0000 | [diff] [blame] | 170 | return getUseBlocks().size() - NumGapBlocks + getNumThroughBlocks(); |
Jakob Stoklund Olesen | b2abfa0 | 2011-05-28 02:32:57 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | /// countLiveBlocks - Return the number of blocks where li is live. This is |
| 174 | /// guaranteed to return the same number as getNumLiveBlocks() after calling |
| 175 | /// analyze(li). |
Jakob Stoklund Olesen | 9f4b893 | 2011-04-26 22:33:12 +0000 | [diff] [blame] | 176 | unsigned countLiveBlocks(const LiveInterval *li) const; |
| 177 | |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 178 | typedef SmallPtrSet<const MachineBasicBlock*, 16> BlockPtrSet; |
| 179 | |
Jakob Stoklund Olesen | 2d6d86b | 2011-08-05 22:20:45 +0000 | [diff] [blame] | 180 | /// shouldSplitSingleBlock - Returns true if it would help to create a local |
| 181 | /// live range for the instructions in BI. There is normally no benefit to |
| 182 | /// creating a live range for a single instruction, but it does enable |
| 183 | /// register class inflation if the instruction has a restricted register |
| 184 | /// class. |
| 185 | /// |
| 186 | /// @param BI The block to be isolated. |
| 187 | /// @param SingleInstrs True when single instructions should be isolated. |
| 188 | bool shouldSplitSingleBlock(const BlockInfo &BI, bool SingleInstrs) const; |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 189 | }; |
| 190 | |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 191 | |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 192 | /// SplitEditor - Edit machine code and LiveIntervals for live range |
| 193 | /// splitting. |
| 194 | /// |
Jakob Stoklund Olesen | 5eb308b | 2010-08-06 22:17:33 +0000 | [diff] [blame] | 195 | /// - Create a SplitEditor from a SplitAnalysis. |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 196 | /// - Start a new live interval with openIntv. |
| 197 | /// - Mark the places where the new interval is entered using enterIntv* |
| 198 | /// - Mark the ranges where the new interval is used with useIntv* |
| 199 | /// - Mark the places where the interval is exited with exitIntv*. |
| 200 | /// - Finish the current interval with closeIntv and repeat from 2. |
Jakob Stoklund Olesen | 7466927 | 2010-10-08 23:42:21 +0000 | [diff] [blame] | 201 | /// - Rewrite instructions with finish(). |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 202 | /// |
| 203 | class SplitEditor { |
Jakob Stoklund Olesen | 0eeca44 | 2011-02-19 00:42:33 +0000 | [diff] [blame] | 204 | SplitAnalysis &SA; |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 205 | LiveIntervals &LIS; |
| 206 | VirtRegMap &VRM; |
| 207 | MachineRegisterInfo &MRI; |
Eric Christopher | 0f43811 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 208 | MachineDominatorTree &MDT; |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 209 | const TargetInstrInfo &TII; |
| 210 | const TargetRegisterInfo &TRI; |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 211 | |
Jakob Stoklund Olesen | 708d06f | 2011-09-12 16:49:21 +0000 | [diff] [blame] | 212 | public: |
| 213 | |
| 214 | /// ComplementSpillMode - Select how the complement live range should be |
| 215 | /// created. SplitEditor automatically creates interval 0 to contain |
| 216 | /// anything that isn't added to another interval. This complement interval |
| 217 | /// can get quite complicated, and it can sometimes be an advantage to allow |
| 218 | /// it to overlap the other intervals. If it is going to spill anyway, no |
| 219 | /// registers are wasted by keeping a value in two places at the same time. |
| 220 | enum ComplementSpillMode { |
| 221 | /// SM_Partition(Default) - Try to create the complement interval so it |
| 222 | /// doesn't overlap any other intervals, and the original interval is |
| 223 | /// partitioned. This may require a large number of back copies and extra |
| 224 | /// PHI-defs. Only segments marked with overlapIntv will be overlapping. |
| 225 | SM_Partition, |
| 226 | |
| 227 | /// SM_Size - Overlap intervals to minimize the number of inserted COPY |
| 228 | /// instructions. Copies to the complement interval are hoisted to their |
| 229 | /// common dominator, so only one COPY is required per value in the |
| 230 | /// complement interval. This also means that no extra PHI-defs need to be |
| 231 | /// inserted in the complement interval. |
| 232 | SM_Size, |
| 233 | |
| 234 | /// SM_Speed - Overlap intervals to minimize the expected execution |
| 235 | /// frequency of the inserted copies. This is very similar to SM_Size, but |
| 236 | /// the complement interval may get some extra PHI-defs. |
| 237 | SM_Speed |
| 238 | }; |
| 239 | |
| 240 | private: |
| 241 | |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 242 | /// Edit - The current parent register and new intervals created. |
Jakob Stoklund Olesen | a2cae58 | 2011-03-02 23:31:50 +0000 | [diff] [blame] | 243 | LiveRangeEdit *Edit; |
Jakob Stoklund Olesen | a17768f | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 244 | |
Eric Christopher | 0f43811 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 245 | /// Index into Edit of the currently open interval. |
| 246 | /// The index 0 is used for the complement, so the first interval started by |
| 247 | /// openIntv will be 1. |
| 248 | unsigned OpenIdx; |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 249 | |
Jakob Stoklund Olesen | 708d06f | 2011-09-12 16:49:21 +0000 | [diff] [blame] | 250 | /// The current spill mode, selected by reset(). |
| 251 | ComplementSpillMode SpillMode; |
| 252 | |
Eric Christopher | 0f43811 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 253 | typedef IntervalMap<SlotIndex, unsigned> RegAssignMap; |
| 254 | |
| 255 | /// Allocator for the interval map. This will eventually be shared with |
| 256 | /// SlotIndexes and LiveIntervals. |
| 257 | RegAssignMap::Allocator Allocator; |
| 258 | |
| 259 | /// RegAssign - Map of the assigned register indexes. |
| 260 | /// Edit.get(RegAssign.lookup(Idx)) is the register that should be live at |
| 261 | /// Idx. |
| 262 | RegAssignMap RegAssign; |
| 263 | |
Jakob Stoklund Olesen | 670ccd1 | 2011-03-01 23:14:53 +0000 | [diff] [blame] | 264 | typedef DenseMap<std::pair<unsigned, unsigned>, VNInfo*> ValueMap; |
| 265 | |
| 266 | /// Values - keep track of the mapping from parent values to values in the new |
| 267 | /// intervals. Given a pair (RegIdx, ParentVNI->id), Values contains: |
| 268 | /// |
| 269 | /// 1. No entry - the value is not mapped to Edit.get(RegIdx). |
| 270 | /// 2. Null - the value is mapped to multiple values in Edit.get(RegIdx). |
| 271 | /// Each value is represented by a minimal live range at its def. |
| 272 | /// 3. A non-null VNInfo - the value is mapped to a single new value. |
| 273 | /// The new value has no live ranges anywhere. |
| 274 | ValueMap Values; |
| 275 | |
Jakob Stoklund Olesen | c1c622e | 2011-09-13 16:47:53 +0000 | [diff] [blame] | 276 | /// LRCalc - Cache for computing live ranges and SSA update. Each instance |
| 277 | /// can only handle non-overlapping live ranges, so use a separate |
| 278 | /// LiveRangeCalc instance for the complement interval when in spill mode. |
| 279 | LiveRangeCalc LRCalc[2]; |
| 280 | |
| 281 | /// getLRCalc - Return the LRCalc to use for RegIdx. In spill mode, the |
| 282 | /// complement interval can overlap the other intervals, so it gets its own |
| 283 | /// LRCalc instance. When not in spill mode, all intervals can share one. |
| 284 | LiveRangeCalc &getLRCalc(unsigned RegIdx) { |
| 285 | return LRCalc[SpillMode != SM_Partition && RegIdx != 0]; |
| 286 | } |
Jakob Stoklund Olesen | 44b7ae2 | 2011-04-15 17:24:49 +0000 | [diff] [blame] | 287 | |
Jakob Stoklund Olesen | 670ccd1 | 2011-03-01 23:14:53 +0000 | [diff] [blame] | 288 | /// defValue - define a value in RegIdx from ParentVNI at Idx. |
| 289 | /// Idx does not have to be ParentVNI->def, but it must be contained within |
| 290 | /// ParentVNI's live range in ParentLI. The new value is added to the value |
| 291 | /// map. |
| 292 | /// Return the new LI value. |
| 293 | VNInfo *defValue(unsigned RegIdx, const VNInfo *ParentVNI, SlotIndex Idx); |
| 294 | |
| 295 | /// markComplexMapped - Mark ParentVNI as complex mapped in RegIdx regardless |
| 296 | /// of the number of defs. |
| 297 | void markComplexMapped(unsigned RegIdx, const VNInfo *ParentVNI); |
| 298 | |
Jakob Stoklund Olesen | cfa7134 | 2010-11-10 19:31:50 +0000 | [diff] [blame] | 299 | /// defFromParent - Define Reg from ParentVNI at UseIdx using either |
| 300 | /// rematerialization or a COPY from parent. Return the new value. |
Eric Christopher | 0f43811 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 301 | VNInfo *defFromParent(unsigned RegIdx, |
Jakob Stoklund Olesen | cfa7134 | 2010-11-10 19:31:50 +0000 | [diff] [blame] | 302 | VNInfo *ParentVNI, |
| 303 | SlotIndex UseIdx, |
| 304 | MachineBasicBlock &MBB, |
| 305 | MachineBasicBlock::iterator I); |
| 306 | |
Jakob Stoklund Olesen | 44b7ae2 | 2011-04-15 17:24:49 +0000 | [diff] [blame] | 307 | /// transferValues - Transfer values to the new ranges. |
| 308 | /// Return true if any ranges were skipped. |
| 309 | bool transferValues(); |
Jakob Stoklund Olesen | 4670353 | 2011-03-02 23:05:19 +0000 | [diff] [blame] | 310 | |
Jakob Stoklund Olesen | e2dc0c9 | 2011-03-02 23:05:16 +0000 | [diff] [blame] | 311 | /// extendPHIKillRanges - Extend the ranges of all values killed by original |
| 312 | /// parent PHIDefs. |
| 313 | void extendPHIKillRanges(); |
| 314 | |
Eric Christopher | 0f43811 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 315 | /// rewriteAssigned - Rewrite all uses of Edit.getReg() to assigned registers. |
Jakob Stoklund Olesen | 4670353 | 2011-03-02 23:05:19 +0000 | [diff] [blame] | 316 | void rewriteAssigned(bool ExtendRanges); |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 317 | |
Jakob Stoklund Olesen | 5881799 | 2011-03-08 22:46:11 +0000 | [diff] [blame] | 318 | /// deleteRematVictims - Delete defs that are dead after rematerializing. |
| 319 | void deleteRematVictims(); |
| 320 | |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 321 | public: |
| 322 | /// Create a new SplitEditor for editing the LiveInterval analyzed by SA. |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 323 | /// Newly created intervals will be appended to newIntervals. |
Jakob Stoklund Olesen | d68f458 | 2010-10-28 20:34:50 +0000 | [diff] [blame] | 324 | SplitEditor(SplitAnalysis &SA, LiveIntervals&, VirtRegMap&, |
Jakob Stoklund Olesen | bece06f | 2011-03-03 01:29:13 +0000 | [diff] [blame] | 325 | MachineDominatorTree&); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 326 | |
Jakob Stoklund Olesen | bece06f | 2011-03-03 01:29:13 +0000 | [diff] [blame] | 327 | /// reset - Prepare for a new split. |
Jakob Stoklund Olesen | 708d06f | 2011-09-12 16:49:21 +0000 | [diff] [blame] | 328 | void reset(LiveRangeEdit&, ComplementSpillMode = SM_Partition); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 329 | |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 330 | /// Create a new virtual register and live interval. |
Jakob Stoklund Olesen | e1b43c3 | 2011-04-12 18:11:31 +0000 | [diff] [blame] | 331 | /// Return the interval index, starting from 1. Interval index 0 is the |
| 332 | /// implicit complement interval. |
| 333 | unsigned openIntv(); |
| 334 | |
| 335 | /// currentIntv - Return the current interval index. |
| 336 | unsigned currentIntv() const { return OpenIdx; } |
| 337 | |
| 338 | /// selectIntv - Select a previously opened interval index. |
| 339 | void selectIntv(unsigned Idx); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 340 | |
Jakob Stoklund Olesen | 207c868 | 2011-02-03 17:04:16 +0000 | [diff] [blame] | 341 | /// enterIntvBefore - Enter the open interval before the instruction at Idx. |
| 342 | /// If the parent interval is not live before Idx, a COPY is not inserted. |
| 343 | /// Return the beginning of the new live range. |
| 344 | SlotIndex enterIntvBefore(SlotIndex Idx); |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 345 | |
Jakob Stoklund Olesen | 87360f7 | 2011-06-30 01:30:39 +0000 | [diff] [blame] | 346 | /// enterIntvAfter - Enter the open interval after the instruction at Idx. |
| 347 | /// Return the beginning of the new live range. |
| 348 | SlotIndex enterIntvAfter(SlotIndex Idx); |
| 349 | |
Jakob Stoklund Olesen | 207c868 | 2011-02-03 17:04:16 +0000 | [diff] [blame] | 350 | /// enterIntvAtEnd - Enter the open interval at the end of MBB. |
| 351 | /// Use the open interval from he inserted copy to the MBB end. |
| 352 | /// Return the beginning of the new live range. |
| 353 | SlotIndex enterIntvAtEnd(MachineBasicBlock &MBB); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 354 | |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 355 | /// useIntv - indicate that all instructions in MBB should use OpenLI. |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 356 | void useIntv(const MachineBasicBlock &MBB); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 357 | |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame] | 358 | /// useIntv - indicate that all instructions in range should use OpenLI. |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 359 | void useIntv(SlotIndex Start, SlotIndex End); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 360 | |
Jakob Stoklund Olesen | 207c868 | 2011-02-03 17:04:16 +0000 | [diff] [blame] | 361 | /// leaveIntvAfter - Leave the open interval after the instruction at Idx. |
| 362 | /// Return the end of the live range. |
| 363 | SlotIndex leaveIntvAfter(SlotIndex Idx); |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 364 | |
Jakob Stoklund Olesen | 9b05777 | 2011-02-09 23:30:25 +0000 | [diff] [blame] | 365 | /// leaveIntvBefore - Leave the open interval before the instruction at Idx. |
| 366 | /// Return the end of the live range. |
| 367 | SlotIndex leaveIntvBefore(SlotIndex Idx); |
| 368 | |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 369 | /// leaveIntvAtTop - Leave the interval at the top of MBB. |
Jakob Stoklund Olesen | 207c868 | 2011-02-03 17:04:16 +0000 | [diff] [blame] | 370 | /// Add liveness from the MBB top to the copy. |
| 371 | /// Return the end of the live range. |
| 372 | SlotIndex leaveIntvAtTop(MachineBasicBlock &MBB); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 373 | |
Jakob Stoklund Olesen | 5c716bd | 2011-02-08 18:50:21 +0000 | [diff] [blame] | 374 | /// overlapIntv - Indicate that all instructions in range should use the open |
| 375 | /// interval, but also let the complement interval be live. |
| 376 | /// |
| 377 | /// This doubles the register pressure, but is sometimes required to deal with |
| 378 | /// register uses after the last valid split point. |
| 379 | /// |
| 380 | /// The Start index should be a return value from a leaveIntv* call, and End |
| 381 | /// should be in the same basic block. The parent interval must have the same |
| 382 | /// value across the range. |
| 383 | /// |
| 384 | void overlapIntv(SlotIndex Start, SlotIndex End); |
| 385 | |
Jakob Stoklund Olesen | 7466927 | 2010-10-08 23:42:21 +0000 | [diff] [blame] | 386 | /// finish - after all the new live ranges have been created, compute the |
| 387 | /// remaining live range, and rewrite instructions to use the new registers. |
Jakob Stoklund Olesen | 5928046 | 2011-04-21 18:38:15 +0000 | [diff] [blame] | 388 | /// @param LRMap When not null, this vector will map each live range in Edit |
| 389 | /// back to the indices returned by openIntv. |
| 390 | /// There may be extra indices created by dead code elimination. |
| 391 | void finish(SmallVectorImpl<unsigned> *LRMap = 0); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 392 | |
Eric Christopher | 0f43811 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 393 | /// dump - print the current interval maping to dbgs(). |
| 394 | void dump() const; |
| 395 | |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 396 | // ===--- High level methods ---=== |
| 397 | |
Jakob Stoklund Olesen | fd5c513 | 2011-04-12 19:32:53 +0000 | [diff] [blame] | 398 | /// splitSingleBlock - Split CurLI into a separate live interval around the |
| 399 | /// uses in a single block. This is intended to be used as part of a larger |
| 400 | /// split, and doesn't call finish(). |
| 401 | void splitSingleBlock(const SplitAnalysis::BlockInfo &BI); |
| 402 | |
Jakob Stoklund Olesen | b4ddedc | 2011-07-15 21:47:57 +0000 | [diff] [blame] | 403 | /// splitLiveThroughBlock - Split CurLI in the given block such that it |
| 404 | /// enters the block in IntvIn and leaves it in IntvOut. There may be uses in |
| 405 | /// the block, but they will be ignored when placing split points. |
| 406 | /// |
| 407 | /// @param MBBNum Block number. |
| 408 | /// @param IntvIn Interval index entering the block. |
| 409 | /// @param LeaveBefore When set, leave IntvIn before this point. |
| 410 | /// @param IntvOut Interval index leaving the block. |
| 411 | /// @param EnterAfter When set, enter IntvOut after this point. |
| 412 | void splitLiveThroughBlock(unsigned MBBNum, |
| 413 | unsigned IntvIn, SlotIndex LeaveBefore, |
| 414 | unsigned IntvOut, SlotIndex EnterAfter); |
| 415 | |
| 416 | /// splitRegInBlock - Split CurLI in the given block such that it enters the |
| 417 | /// block in IntvIn and leaves it on the stack (or not at all). Split points |
| 418 | /// are placed in a way that avoids putting uses in the stack interval. This |
| 419 | /// may require creating a local interval when there is interference. |
| 420 | /// |
| 421 | /// @param BI Block descriptor. |
| 422 | /// @param IntvIn Interval index entering the block. Not 0. |
| 423 | /// @param LeaveBefore When set, leave IntvIn before this point. |
| 424 | void splitRegInBlock(const SplitAnalysis::BlockInfo &BI, |
| 425 | unsigned IntvIn, SlotIndex LeaveBefore); |
| 426 | |
| 427 | /// splitRegOutBlock - Split CurLI in the given block such that it enters the |
| 428 | /// block on the stack (or isn't live-in at all) and leaves it in IntvOut. |
| 429 | /// Split points are placed to avoid interference and such that the uses are |
| 430 | /// not in the stack interval. This may require creating a local interval |
| 431 | /// when there is interference. |
| 432 | /// |
| 433 | /// @param BI Block descriptor. |
| 434 | /// @param IntvOut Interval index leaving the block. |
| 435 | /// @param EnterAfter When set, enter IntvOut after this point. |
| 436 | void splitRegOutBlock(const SplitAnalysis::BlockInfo &BI, |
| 437 | unsigned IntvOut, SlotIndex EnterAfter); |
Jakob Stoklund Olesen | fc412d8 | 2010-08-13 21:18:48 +0000 | [diff] [blame] | 438 | }; |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 439 | |
| 440 | } |
Sebastian Redl | 6796e4f | 2011-04-24 15:46:51 +0000 | [diff] [blame] | 441 | |
| 442 | #endif |