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