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 | |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/DenseMap.h" |
Jakob Stoklund Olesen | 8d0963f | 2010-12-21 01:50:21 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/SmallPtrSet.h" |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/SlotIndexes.h" |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 18 | |
| 19 | namespace llvm { |
| 20 | |
| 21 | class LiveInterval; |
| 22 | class LiveIntervals; |
Jakob Stoklund Olesen | a17768f | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 23 | class LiveRangeEdit; |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 24 | class MachineInstr; |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 25 | class MachineLoop; |
| 26 | class MachineLoopInfo; |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 27 | class MachineRegisterInfo; |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 28 | class TargetInstrInfo; |
Jakob Stoklund Olesen | cfa7134 | 2010-11-10 19:31:50 +0000 | [diff] [blame] | 29 | class TargetRegisterInfo; |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 30 | class VirtRegMap; |
| 31 | class VNInfo; |
Jakob Stoklund Olesen | 532de3d | 2010-10-22 20:28:21 +0000 | [diff] [blame] | 32 | class raw_ostream; |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 33 | |
Jakob Stoklund Olesen | e1dde7b | 2010-10-28 20:34:52 +0000 | [diff] [blame] | 34 | /// At some point we should just include MachineDominators.h: |
| 35 | class MachineDominatorTree; |
| 36 | template <class NodeT> class DomTreeNodeBase; |
| 37 | typedef DomTreeNodeBase<MachineBasicBlock> MachineDomTreeNode; |
| 38 | |
Jakob Stoklund Olesen | 8d0963f | 2010-12-21 01:50:21 +0000 | [diff] [blame] | 39 | |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 40 | /// SplitAnalysis - Analyze a LiveInterval, looking for live range splitting |
| 41 | /// opportunities. |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 42 | class SplitAnalysis { |
Jakob Stoklund Olesen | 08e93b1 | 2010-08-10 17:07:22 +0000 | [diff] [blame] | 43 | public: |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame^] | 44 | const MachineFunction &MF; |
| 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 | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 49 | // Instructions using the the current register. |
| 50 | typedef SmallPtrSet<const MachineInstr*, 16> InstrPtrSet; |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame^] | 51 | InstrPtrSet UsingInstrs; |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 52 | |
Jakob Stoklund Olesen | b5fa933 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 53 | // Sorted slot indexes of using instructions. |
| 54 | SmallVector<SlotIndex, 8> UseSlots; |
| 55 | |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame^] | 56 | // The number of instructions using CurLI in each basic block. |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 57 | typedef DenseMap<const MachineBasicBlock*, unsigned> BlockCountMap; |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame^] | 58 | BlockCountMap UsingBlocks; |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 59 | |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame^] | 60 | // The number of basic block using CurLI in each loop. |
Jakob Stoklund Olesen | 2dee7a5 | 2010-08-12 23:02:55 +0000 | [diff] [blame] | 61 | typedef DenseMap<const MachineLoop*, unsigned> LoopCountMap; |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame^] | 62 | LoopCountMap UsingLoops; |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 63 | |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 64 | private: |
| 65 | // Current live interval. |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame^] | 66 | const LiveInterval *CurLI; |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 67 | |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame^] | 68 | // Sumarize statistics by counting instructions using CurLI. |
Jakob Stoklund Olesen | abff280 | 2010-07-20 16:12:37 +0000 | [diff] [blame] | 69 | void analyzeUses(); |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 70 | |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 71 | /// canAnalyzeBranch - Return true if MBB ends in a branch that can be |
| 72 | /// analyzed. |
| 73 | bool canAnalyzeBranch(const MachineBasicBlock *MBB); |
| 74 | |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 75 | public: |
Jakob Stoklund Olesen | f2c6e36 | 2010-07-20 23:50:15 +0000 | [diff] [blame] | 76 | SplitAnalysis(const MachineFunction &mf, const LiveIntervals &lis, |
| 77 | const MachineLoopInfo &mli); |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 78 | |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame^] | 79 | /// 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] | 80 | /// split. |
| 81 | void analyze(const LiveInterval *li); |
| 82 | |
| 83 | /// clear - clear all data structures so SplitAnalysis is ready to analyze a |
| 84 | /// new interval. |
| 85 | void clear(); |
| 86 | |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame^] | 87 | /// hasUses - Return true if MBB has any uses of CurLI. |
Jakob Stoklund Olesen | b5fa933 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 88 | bool hasUses(const MachineBasicBlock *MBB) const { |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame^] | 89 | return UsingBlocks.lookup(MBB); |
Jakob Stoklund Olesen | b5fa933 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 92 | typedef SmallPtrSet<const MachineBasicBlock*, 16> BlockPtrSet; |
Jakob Stoklund Olesen | 2dee7a5 | 2010-08-12 23:02:55 +0000 | [diff] [blame] | 93 | typedef SmallPtrSet<const MachineLoop*, 16> LoopPtrSet; |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 94 | |
Jakob Stoklund Olesen | 532de3d | 2010-10-22 20:28:21 +0000 | [diff] [blame] | 95 | // Print a set of blocks with use counts. |
| 96 | void print(const BlockPtrSet&, raw_ostream&) const; |
| 97 | |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 98 | // Sets of basic blocks surrounding a machine loop. |
| 99 | struct LoopBlocks { |
| 100 | BlockPtrSet Loop; // Blocks in the loop. |
| 101 | BlockPtrSet Preds; // Loop predecessor blocks. |
| 102 | BlockPtrSet Exits; // Loop exit blocks. |
| 103 | |
| 104 | void clear() { |
| 105 | Loop.clear(); |
| 106 | Preds.clear(); |
| 107 | Exits.clear(); |
| 108 | } |
| 109 | }; |
| 110 | |
Jakob Stoklund Olesen | 532de3d | 2010-10-22 20:28:21 +0000 | [diff] [blame] | 111 | // Print loop blocks with use counts. |
| 112 | void print(const LoopBlocks&, raw_ostream&) const; |
| 113 | |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 114 | // Calculate the block sets surrounding the loop. |
| 115 | void getLoopBlocks(const MachineLoop *Loop, LoopBlocks &Blocks); |
| 116 | |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 117 | /// LoopPeripheralUse - how is a variable used in and around a loop? |
| 118 | /// Peripheral blocks are the loop predecessors and exit blocks. |
| 119 | enum LoopPeripheralUse { |
| 120 | ContainedInLoop, // All uses are inside the loop. |
| 121 | SinglePeripheral, // At most one instruction per peripheral block. |
| 122 | MultiPeripheral, // Multiple instructions in some peripheral blocks. |
| 123 | OutsideLoop // Uses outside loop periphery. |
| 124 | }; |
| 125 | |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame^] | 126 | /// analyzeLoopPeripheralUse - Return an enum describing how CurLI is used in |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 127 | /// and around the Loop. |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 128 | LoopPeripheralUse analyzeLoopPeripheralUse(const LoopBlocks&); |
| 129 | |
| 130 | /// getCriticalExits - It may be necessary to partially break critical edges |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame^] | 131 | /// leaving the loop if an exit block has phi uses of CurLI. Collect the exit |
Jakob Stoklund Olesen | 6a0dc07 | 2010-07-20 21:46:58 +0000 | [diff] [blame] | 132 | /// blocks that need special treatment into CriticalExits. |
| 133 | void getCriticalExits(const LoopBlocks &Blocks, BlockPtrSet &CriticalExits); |
| 134 | |
| 135 | /// canSplitCriticalExits - Return true if it is possible to insert new exit |
| 136 | /// blocks before the blocks in CriticalExits. |
| 137 | bool canSplitCriticalExits(const LoopBlocks &Blocks, |
| 138 | BlockPtrSet &CriticalExits); |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 139 | |
Jakob Stoklund Olesen | 0960a65 | 2010-10-27 00:39:05 +0000 | [diff] [blame] | 140 | /// getCriticalPreds - Get the set of loop predecessors with critical edges to |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame^] | 141 | /// blocks outside the loop that have CurLI live in. We don't have to break |
Jakob Stoklund Olesen | 0960a65 | 2010-10-27 00:39:05 +0000 | [diff] [blame] | 142 | /// these edges, but they do require special treatment. |
| 143 | void getCriticalPreds(const LoopBlocks &Blocks, BlockPtrSet &CriticalPreds); |
| 144 | |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame^] | 145 | /// getSplitLoops - Get the set of loops that have CurLI uses and would be |
Jakob Stoklund Olesen | 521a453 | 2010-12-15 17:41:19 +0000 | [diff] [blame] | 146 | /// profitable to split. |
| 147 | void getSplitLoops(LoopPtrSet&); |
| 148 | |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame^] | 149 | /// getBestSplitLoop - Return the loop where CurLI may best be split to a |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 150 | /// separate register, or NULL. |
| 151 | const MachineLoop *getBestSplitLoop(); |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 152 | |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame^] | 153 | /// isBypassLoop - Return true if CurLI is live through Loop and has no uses |
Jakob Stoklund Olesen | 697483a | 2010-12-15 17:49:52 +0000 | [diff] [blame] | 154 | /// inside the loop. Bypass loops are candidates for splitting because it can |
| 155 | /// prevent interference inside the loop. |
| 156 | bool isBypassLoop(const MachineLoop *Loop); |
| 157 | |
| 158 | /// getBypassLoops - Get all the maximal bypass loops. These are the bypass |
| 159 | /// loops whose parent is not a bypass loop. |
| 160 | void getBypassLoops(LoopPtrSet&); |
| 161 | |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 162 | /// getMultiUseBlocks - Add basic blocks to Blocks that may benefit from |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame^] | 163 | /// having CurLI split to a new live interval. Return true if Blocks can be |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 164 | /// passed to SplitEditor::splitSingleBlocks. |
| 165 | bool getMultiUseBlocks(BlockPtrSet &Blocks); |
Jakob Stoklund Olesen | fc412d8 | 2010-08-13 21:18:48 +0000 | [diff] [blame] | 166 | |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame^] | 167 | /// getBlockForInsideSplit - If CurLI is contained inside a single basic |
| 168 | /// block, and it would pay to subdivide the interval inside that block, |
| 169 | /// return it. Otherwise return NULL. The returned block can be passed to |
Jakob Stoklund Olesen | fc412d8 | 2010-08-13 21:18:48 +0000 | [diff] [blame] | 170 | /// SplitEditor::splitInsideBlock. |
| 171 | const MachineBasicBlock *getBlockForInsideSplit(); |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 172 | }; |
| 173 | |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 174 | |
| 175 | /// LiveIntervalMap - Map values from a large LiveInterval into a small |
| 176 | /// interval that is a subset. Insert phi-def values as needed. This class is |
| 177 | /// used by SplitEditor to create new smaller LiveIntervals. |
| 178 | /// |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame^] | 179 | /// ParentLI is the larger interval, LI is the subset interval. Every value |
| 180 | /// in LI corresponds to exactly one value in ParentLI, and the live range |
| 181 | /// of the value is contained within the live range of the ParentLI value. |
| 182 | /// Values in ParentLI may map to any number of OpenLI values, including 0. |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 183 | class LiveIntervalMap { |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame^] | 184 | LiveIntervals &LIS; |
| 185 | MachineDominatorTree &MDT; |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 186 | |
| 187 | // The parent interval is never changed. |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame^] | 188 | const LiveInterval &ParentLI; |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 189 | |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame^] | 190 | // The child interval's values are fully contained inside ParentLI values. |
| 191 | LiveInterval *LI; |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 192 | |
| 193 | typedef DenseMap<const VNInfo*, VNInfo*> ValueMap; |
| 194 | |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame^] | 195 | // Map ParentLI values to simple values in LI that are defined at the same |
| 196 | // SlotIndex, or NULL for ParentLI values that have complex LI defs. |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 197 | // Note there is a difference between values mapping to NULL (complex), and |
| 198 | // values not present (unknown/unmapped). |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame^] | 199 | ValueMap Values; |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 200 | |
Jakob Stoklund Olesen | e1dde7b | 2010-10-28 20:34:52 +0000 | [diff] [blame] | 201 | typedef std::pair<VNInfo*, MachineDomTreeNode*> LiveOutPair; |
| 202 | typedef DenseMap<MachineBasicBlock*,LiveOutPair> LiveOutMap; |
| 203 | |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame^] | 204 | // LiveOutCache - Map each basic block where LI is live out to the live-out |
Jakob Stoklund Olesen | e1dde7b | 2010-10-28 20:34:52 +0000 | [diff] [blame] | 205 | // value and its defining block. One of these conditions shall be true: |
| 206 | // |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame^] | 207 | // 1. !LiveOutCache.count(MBB) |
| 208 | // 2. LiveOutCache[MBB].second.getNode() == MBB |
| 209 | // 3. forall P in preds(MBB): LiveOutCache[P] == LiveOutCache[MBB] |
Jakob Stoklund Olesen | e1dde7b | 2010-10-28 20:34:52 +0000 | [diff] [blame] | 210 | // |
| 211 | // This is only a cache, the values can be computed as: |
| 212 | // |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame^] | 213 | // VNI = LI->getVNInfoAt(LIS.getMBBEndIdx(MBB)) |
| 214 | // Node = mbt_[LIS.getMBBFromIndex(VNI->def)] |
Jakob Stoklund Olesen | e1dde7b | 2010-10-28 20:34:52 +0000 | [diff] [blame] | 215 | // |
| 216 | // The cache is also used as a visiteed set by mapValue(). |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame^] | 217 | LiveOutMap LiveOutCache; |
Jakob Stoklund Olesen | e1dde7b | 2010-10-28 20:34:52 +0000 | [diff] [blame] | 218 | |
Jakob Stoklund Olesen | d7ca577 | 2011-01-20 17:45:20 +0000 | [diff] [blame] | 219 | // Dump the live-out cache to dbgs(). |
| 220 | void dumpCache(); |
| 221 | |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 222 | public: |
| 223 | LiveIntervalMap(LiveIntervals &lis, |
Jakob Stoklund Olesen | d68f458 | 2010-10-28 20:34:50 +0000 | [diff] [blame] | 224 | MachineDominatorTree &mdt, |
Jakob Stoklund Olesen | 9ca2aeb | 2010-09-13 23:29:09 +0000 | [diff] [blame] | 225 | const LiveInterval &parentli) |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame^] | 226 | : LIS(lis), MDT(mdt), ParentLI(parentli), LI(0) {} |
Jakob Stoklund Olesen | 9ca2aeb | 2010-09-13 23:29:09 +0000 | [diff] [blame] | 227 | |
| 228 | /// reset - clear all data structures and start a new live interval. |
| 229 | void reset(LiveInterval *); |
| 230 | |
| 231 | /// getLI - return the current live interval. |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame^] | 232 | LiveInterval *getLI() const { return LI; } |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 233 | |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame^] | 234 | /// defValue - define a value in LI from the ParentLI value VNI and Idx. |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 235 | /// Idx does not have to be ParentVNI->def, but it must be contained within |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame^] | 236 | /// ParentVNI's live range in ParentLI. |
| 237 | /// Return the new LI value. |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 238 | VNInfo *defValue(const VNInfo *ParentVNI, SlotIndex Idx); |
| 239 | |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame^] | 240 | /// mapValue - map ParentVNI to the corresponding LI value at Idx. It is |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 241 | /// assumed that ParentVNI is live at Idx. |
| 242 | /// If ParentVNI has not been defined by defValue, it is assumed that |
| 243 | /// ParentVNI->def dominates Idx. |
| 244 | /// If ParentVNI has been defined by defValue one or more times, a value that |
| 245 | /// dominates Idx will be returned. This may require creating extra phi-def |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame^] | 246 | /// values and adding live ranges to LI. |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 247 | /// If simple is not NULL, *simple will indicate if ParentVNI is a simply |
| 248 | /// mapped value. |
| 249 | VNInfo *mapValue(const VNInfo *ParentVNI, SlotIndex Idx, bool *simple = 0); |
| 250 | |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame^] | 251 | // extendTo - Find the last LI value defined in MBB at or before Idx. The |
Jakob Stoklund Olesen | fc60d77 | 2010-10-05 20:36:25 +0000 | [diff] [blame] | 252 | // parentli is assumed to be live at Idx. Extend the live range to include |
| 253 | // Idx. Return the found VNInfo, or NULL. |
Jakob Stoklund Olesen | c95c146 | 2010-10-27 00:39:07 +0000 | [diff] [blame] | 254 | VNInfo *extendTo(const MachineBasicBlock *MBB, SlotIndex Idx); |
Jakob Stoklund Olesen | fc60d77 | 2010-10-05 20:36:25 +0000 | [diff] [blame] | 255 | |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 256 | /// isMapped - Return true is ParentVNI is a known mapped value. It may be a |
| 257 | /// simple 1-1 mapping or a complex mapping to later defs. |
| 258 | bool isMapped(const VNInfo *ParentVNI) const { |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame^] | 259 | return Values.count(ParentVNI); |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | /// isComplexMapped - Return true if ParentVNI has received new definitions |
| 263 | /// with defValue. |
| 264 | bool isComplexMapped(const VNInfo *ParentVNI) const; |
| 265 | |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame^] | 266 | // addSimpleRange - Add a simple range from ParentLI to LI. |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 267 | // ParentVNI must be live in the [Start;End) interval. |
| 268 | void addSimpleRange(SlotIndex Start, SlotIndex End, const VNInfo *ParentVNI); |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 269 | |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame^] | 270 | /// addRange - Add live ranges to LI where [Start;End) intersects ParentLI. |
Jakob Stoklund Olesen | 1407c84 | 2010-08-18 19:00:08 +0000 | [diff] [blame] | 271 | /// All needed values whose def is not inside [Start;End) must be defined |
| 272 | /// beforehand so mapValue will work. |
| 273 | void addRange(SlotIndex Start, SlotIndex End); |
| 274 | }; |
| 275 | |
| 276 | |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 277 | /// SplitEditor - Edit machine code and LiveIntervals for live range |
| 278 | /// splitting. |
| 279 | /// |
Jakob Stoklund Olesen | 5eb308b | 2010-08-06 22:17:33 +0000 | [diff] [blame] | 280 | /// - Create a SplitEditor from a SplitAnalysis. |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 281 | /// - Start a new live interval with openIntv. |
| 282 | /// - Mark the places where the new interval is entered using enterIntv* |
| 283 | /// - Mark the ranges where the new interval is used with useIntv* |
| 284 | /// - Mark the places where the interval is exited with exitIntv*. |
| 285 | /// - Finish the current interval with closeIntv and repeat from 2. |
Jakob Stoklund Olesen | 7466927 | 2010-10-08 23:42:21 +0000 | [diff] [blame] | 286 | /// - Rewrite instructions with finish(). |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 287 | /// |
| 288 | class SplitEditor { |
| 289 | SplitAnalysis &sa_; |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame^] | 290 | LiveIntervals &LIS; |
| 291 | VirtRegMap &VRM; |
| 292 | MachineRegisterInfo &MRI; |
| 293 | const TargetInstrInfo &TII; |
| 294 | const TargetRegisterInfo &TRI; |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 295 | |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame^] | 296 | /// Edit - The current parent register and new intervals created. |
| 297 | LiveRangeEdit &Edit; |
Jakob Stoklund Olesen | a17768f | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 298 | |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame^] | 299 | /// DupLI - Created as a copy of CurLI, ranges are carved out as new |
Jakob Stoklund Olesen | 5eb308b | 2010-08-06 22:17:33 +0000 | [diff] [blame] | 300 | /// intervals get added through openIntv / closeIntv. This is used to avoid |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame^] | 301 | /// editing CurLI. |
| 302 | LiveIntervalMap DupLI; |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 303 | |
| 304 | /// Currently open LiveInterval. |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame^] | 305 | LiveIntervalMap OpenLI; |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 306 | |
Jakob Stoklund Olesen | cfa7134 | 2010-11-10 19:31:50 +0000 | [diff] [blame] | 307 | /// defFromParent - Define Reg from ParentVNI at UseIdx using either |
| 308 | /// rematerialization or a COPY from parent. Return the new value. |
| 309 | VNInfo *defFromParent(LiveIntervalMap &Reg, |
| 310 | VNInfo *ParentVNI, |
| 311 | SlotIndex UseIdx, |
| 312 | MachineBasicBlock &MBB, |
| 313 | MachineBasicBlock::iterator I); |
| 314 | |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 315 | /// intervalsLiveAt - Return true if any member of intervals_ is live at Idx. |
| 316 | bool intervalsLiveAt(SlotIndex Idx) const; |
| 317 | |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame^] | 318 | /// Values in CurLI whose live range has been truncated when entering an open |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 319 | /// li. |
| 320 | SmallPtrSet<const VNInfo*, 8> truncatedValues; |
| 321 | |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame^] | 322 | /// addTruncSimpleRange - Add the given simple range to DupLI after |
Jakob Stoklund Olesen | 5fa42a4 | 2010-09-21 22:32:21 +0000 | [diff] [blame] | 323 | /// truncating any overlap with intervals_. |
| 324 | void addTruncSimpleRange(SlotIndex Start, SlotIndex End, VNInfo *VNI); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 325 | |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame^] | 326 | /// criticalPreds_ - Set of basic blocks where both dupli and OpenLI should be |
Jakob Stoklund Olesen | c95c146 | 2010-10-27 00:39:07 +0000 | [diff] [blame] | 327 | /// live out because of a critical edge. |
| 328 | SplitAnalysis::BlockPtrSet criticalPreds_; |
| 329 | |
Jakob Stoklund Olesen | 7466927 | 2010-10-08 23:42:21 +0000 | [diff] [blame] | 330 | /// computeRemainder - Compute the dupli liveness as the complement of all the |
| 331 | /// new intervals. |
| 332 | void computeRemainder(); |
| 333 | |
| 334 | /// rewrite - Rewrite all uses of reg to use the new registers. |
| 335 | void rewrite(unsigned reg); |
| 336 | |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 337 | public: |
| 338 | /// Create a new SplitEditor for editing the LiveInterval analyzed by SA. |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 339 | /// Newly created intervals will be appended to newIntervals. |
Jakob Stoklund Olesen | d68f458 | 2010-10-28 20:34:50 +0000 | [diff] [blame] | 340 | SplitEditor(SplitAnalysis &SA, LiveIntervals&, VirtRegMap&, |
| 341 | MachineDominatorTree&, LiveRangeEdit&); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 342 | |
Jakob Stoklund Olesen | 5eb308b | 2010-08-06 22:17:33 +0000 | [diff] [blame] | 343 | /// getAnalysis - Get the corresponding analysis. |
| 344 | SplitAnalysis &getAnalysis() { return sa_; } |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 345 | |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 346 | /// Create a new virtual register and live interval. |
| 347 | void openIntv(); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 348 | |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame^] | 349 | /// enterIntvBefore - Enter OpenLI before the instruction at Idx. If CurLI is |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 350 | /// not live before Idx, a COPY is not inserted. |
| 351 | void enterIntvBefore(SlotIndex Idx); |
| 352 | |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame^] | 353 | /// enterIntvAtEnd - Enter OpenLI at the end of MBB. |
Jakob Stoklund Olesen | f6a129a | 2010-09-16 00:01:36 +0000 | [diff] [blame] | 354 | void enterIntvAtEnd(MachineBasicBlock &MBB); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 355 | |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame^] | 356 | /// useIntv - indicate that all instructions in MBB should use OpenLI. |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 357 | void useIntv(const MachineBasicBlock &MBB); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 358 | |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame^] | 359 | /// useIntv - indicate that all instructions in range should use OpenLI. |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 360 | void useIntv(SlotIndex Start, SlotIndex End); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 361 | |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame^] | 362 | /// leaveIntvAfter - Leave OpenLI after the instruction at Idx. |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 363 | void leaveIntvAfter(SlotIndex Idx); |
| 364 | |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 365 | /// leaveIntvAtTop - Leave the interval at the top of MBB. |
| 366 | /// Currently, only one value can leave the interval. |
| 367 | void leaveIntvAtTop(MachineBasicBlock &MBB); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 368 | |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 369 | /// closeIntv - Indicate that we are done editing the currently open |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 370 | /// LiveInterval, and ranges can be trimmed. |
Jakob Stoklund Olesen | 7536f72 | 2010-08-04 22:08:39 +0000 | [diff] [blame] | 371 | void closeIntv(); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 372 | |
Jakob Stoklund Olesen | 7466927 | 2010-10-08 23:42:21 +0000 | [diff] [blame] | 373 | /// finish - after all the new live ranges have been created, compute the |
| 374 | /// remaining live range, and rewrite instructions to use the new registers. |
| 375 | void finish(); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 376 | |
| 377 | // ===--- High level methods ---=== |
| 378 | |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame^] | 379 | /// splitAroundLoop - Split CurLI into a separate live interval inside |
Jakob Stoklund Olesen | 57d0f2d | 2010-10-05 22:19:33 +0000 | [diff] [blame] | 380 | /// the loop. |
| 381 | void splitAroundLoop(const MachineLoop*); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 382 | |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame^] | 383 | /// splitSingleBlocks - Split CurLI into a separate live interval inside each |
Jakob Stoklund Olesen | 57d0f2d | 2010-10-05 22:19:33 +0000 | [diff] [blame] | 384 | /// basic block in Blocks. |
| 385 | void splitSingleBlocks(const SplitAnalysis::BlockPtrSet &Blocks); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 386 | |
Jakob Stoklund Olesen | 0786284 | 2011-01-26 00:50:53 +0000 | [diff] [blame^] | 387 | /// splitInsideBlock - Split CurLI into multiple intervals inside MBB. |
Jakob Stoklund Olesen | 57d0f2d | 2010-10-05 22:19:33 +0000 | [diff] [blame] | 388 | void splitInsideBlock(const MachineBasicBlock *); |
Jakob Stoklund Olesen | fc412d8 | 2010-08-13 21:18:48 +0000 | [diff] [blame] | 389 | }; |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 390 | |
| 391 | } |