1 //===---------- SplitKit.cpp - Toolkit for splitting live ranges ----------===// 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 15 #include "llvm/ADT/SmallPtrSet.h" 16 #include "llvm/ADT/DenseMap.h" 17 #include "llvm/CodeGen/SlotIndexes.h" 18 19 namespace llvm { 20 21 class LiveInterval; 22 class LiveIntervals; 23 class LiveRangeEdit; 24 class MachineInstr; 25 class MachineLoop; 26 class MachineLoopInfo; 27 class MachineRegisterInfo; 28 class TargetInstrInfo; 29 class VirtRegMap; 30 class VNInfo; 31 32 /// SplitAnalysis - Analyze a LiveInterval, looking for live range splitting 33 /// opportunities. 34 class SplitAnalysis { 35 public: 36 const MachineFunction &mf_; 37 const LiveIntervals &lis_; 38 const MachineLoopInfo &loops_; 39 const TargetInstrInfo &tii_; 40 41 // Instructions using the the current register. 42 typedef SmallPtrSet<const MachineInstr*, 16> InstrPtrSet; 43 InstrPtrSet usingInstrs_; 44 45 // The number of instructions using curli in each basic block. 46 typedef DenseMap<const MachineBasicBlock*, unsigned> BlockCountMap; 47 BlockCountMap usingBlocks_; 48 49 // The number of basic block using curli in each loop. 50 typedef DenseMap<const MachineLoop*, unsigned> LoopCountMap; 51 LoopCountMap usingLoops_; 52 53 private: 54 // Current live interval. 55 const LiveInterval *curli_; 56 57 // Sumarize statistics by counting instructions using curli_. 58 void analyzeUses(); 59 60 /// canAnalyzeBranch - Return true if MBB ends in a branch that can be 61 /// analyzed. 62 bool canAnalyzeBranch(const MachineBasicBlock *MBB); 63 64 public: 65 SplitAnalysis(const MachineFunction &mf, const LiveIntervals &lis, 66 const MachineLoopInfo &mli); 67 68 /// analyze - set curli to the specified interval, and analyze how it may be 69 /// split. 70 void analyze(const LiveInterval *li); 71 72 /// clear - clear all data structures so SplitAnalysis is ready to analyze a 73 /// new interval. 74 void clear(); 75 76 typedef SmallPtrSet<const MachineBasicBlock*, 16> BlockPtrSet; 77 typedef SmallPtrSet<const MachineLoop*, 16> LoopPtrSet; 78 79 // Sets of basic blocks surrounding a machine loop. 80 struct LoopBlocks { 81 BlockPtrSet Loop; // Blocks in the loop. 82 BlockPtrSet Preds; // Loop predecessor blocks. 83 BlockPtrSet Exits; // Loop exit blocks. 84 85 void clear() { 86 Loop.clear(); 87 Preds.clear(); 88 Exits.clear(); 89 } 90 }; 91 92 // Calculate the block sets surrounding the loop. 93 void getLoopBlocks(const MachineLoop *Loop, LoopBlocks &Blocks); 94 95 /// LoopPeripheralUse - how is a variable used in and around a loop? 96 /// Peripheral blocks are the loop predecessors and exit blocks. 97 enum LoopPeripheralUse { 98 ContainedInLoop, // All uses are inside the loop. 99 SinglePeripheral, // At most one instruction per peripheral block. 100 MultiPeripheral, // Multiple instructions in some peripheral blocks. 101 OutsideLoop // Uses outside loop periphery. 102 }; 103 104 /// analyzeLoopPeripheralUse - Return an enum describing how curli_ is used in 105 /// and around the Loop. 106 LoopPeripheralUse analyzeLoopPeripheralUse(const LoopBlocks&); 107 108 /// getCriticalExits - It may be necessary to partially break critical edges 109 /// leaving the loop if an exit block has phi uses of curli. Collect the exit 110 /// blocks that need special treatment into CriticalExits. 111 void getCriticalExits(const LoopBlocks &Blocks, BlockPtrSet &CriticalExits); 112 113 /// canSplitCriticalExits - Return true if it is possible to insert new exit 114 /// blocks before the blocks in CriticalExits. 115 bool canSplitCriticalExits(const LoopBlocks &Blocks, 116 BlockPtrSet &CriticalExits); 117 118 /// getBestSplitLoop - Return the loop where curli may best be split to a 119 /// separate register, or NULL. 120 const MachineLoop *getBestSplitLoop(); 121 122 /// getMultiUseBlocks - Add basic blocks to Blocks that may benefit from 123 /// having curli split to a new live interval. Return true if Blocks can be 124 /// passed to SplitEditor::splitSingleBlocks. 125 bool getMultiUseBlocks(BlockPtrSet &Blocks); 126 127 /// getBlockForInsideSplit - If curli is contained inside a single basic block, 128 /// and it wou pay to subdivide the interval inside that block, return it. 129 /// Otherwise return NULL. The returned block can be passed to 130 /// SplitEditor::splitInsideBlock. 131 const MachineBasicBlock *getBlockForInsideSplit(); 132 }; 133 134 135 /// LiveIntervalMap - Map values from a large LiveInterval into a small 136 /// interval that is a subset. Insert phi-def values as needed. This class is 137 /// used by SplitEditor to create new smaller LiveIntervals. 138 /// 139 /// parentli_ is the larger interval, li_ is the subset interval. Every value 140 /// in li_ corresponds to exactly one value in parentli_, and the live range 141 /// of the value is contained within the live range of the parentli_ value. 142 /// Values in parentli_ may map to any number of openli_ values, including 0. 143 class LiveIntervalMap { 144 LiveIntervals &lis_; 145 146 // The parent interval is never changed. 147 const LiveInterval &parentli_; 148 149 // The child interval's values are fully contained inside parentli_ values. 150 LiveInterval *li_; 151 152 typedef DenseMap<const VNInfo*, VNInfo*> ValueMap; 153 154 // Map parentli_ values to simple values in li_ that are defined at the same 155 // SlotIndex, or NULL for parentli_ values that have complex li_ defs. 156 // Note there is a difference between values mapping to NULL (complex), and 157 // values not present (unknown/unmapped). 158 ValueMap valueMap_; 159 160 public: 161 LiveIntervalMap(LiveIntervals &lis, 162 const LiveInterval &parentli) 163 : lis_(lis), parentli_(parentli), li_(0) {} 164 165 /// reset - clear all data structures and start a new live interval. 166 void reset(LiveInterval *); 167 168 /// getLI - return the current live interval. 169 LiveInterval *getLI() const { return li_; } 170 171 /// defValue - define a value in li_ from the parentli_ value VNI and Idx. 172 /// Idx does not have to be ParentVNI->def, but it must be contained within 173 /// ParentVNI's live range in parentli_. 174 /// Return the new li_ value. 175 VNInfo *defValue(const VNInfo *ParentVNI, SlotIndex Idx); 176 177 /// mapValue - map ParentVNI to the corresponding li_ value at Idx. It is 178 /// assumed that ParentVNI is live at Idx. 179 /// If ParentVNI has not been defined by defValue, it is assumed that 180 /// ParentVNI->def dominates Idx. 181 /// If ParentVNI has been defined by defValue one or more times, a value that 182 /// dominates Idx will be returned. This may require creating extra phi-def 183 /// values and adding live ranges to li_. 184 /// If simple is not NULL, *simple will indicate if ParentVNI is a simply 185 /// mapped value. 186 VNInfo *mapValue(const VNInfo *ParentVNI, SlotIndex Idx, bool *simple = 0); 187 188 // extendTo - Find the last li_ value defined in MBB at or before Idx. The 189 // parentli is assumed to be live at Idx. Extend the live range to include 190 // Idx. Return the found VNInfo, or NULL. 191 VNInfo *extendTo(MachineBasicBlock *MBB, SlotIndex Idx); 192 193 /// isMapped - Return true is ParentVNI is a known mapped value. It may be a 194 /// simple 1-1 mapping or a complex mapping to later defs. 195 bool isMapped(const VNInfo *ParentVNI) const { 196 return valueMap_.count(ParentVNI); 197 } 198 199 /// isComplexMapped - Return true if ParentVNI has received new definitions 200 /// with defValue. 201 bool isComplexMapped(const VNInfo *ParentVNI) const; 202 203 // addSimpleRange - Add a simple range from parentli_ to li_. 204 // ParentVNI must be live in the [Start;End) interval. 205 void addSimpleRange(SlotIndex Start, SlotIndex End, const VNInfo *ParentVNI); 206 207 /// addRange - Add live ranges to li_ where [Start;End) intersects parentli_. 208 /// All needed values whose def is not inside [Start;End) must be defined 209 /// beforehand so mapValue will work. 210 void addRange(SlotIndex Start, SlotIndex End); 211 212 /// defByCopyFrom - Insert a copy from Reg to li, assuming that Reg carries 213 /// ParentVNI. Add a minimal live range for the new value and return it. 214 VNInfo *defByCopyFrom(unsigned Reg, 215 const VNInfo *ParentVNI, 216 MachineBasicBlock &MBB, 217 MachineBasicBlock::iterator I); 218 219 }; 220 221 222 /// SplitEditor - Edit machine code and LiveIntervals for live range 223 /// splitting. 224 /// 225 /// - Create a SplitEditor from a SplitAnalysis. 226 /// - Start a new live interval with openIntv. 227 /// - Mark the places where the new interval is entered using enterIntv* 228 /// - Mark the ranges where the new interval is used with useIntv* 229 /// - Mark the places where the interval is exited with exitIntv*. 230 /// - Finish the current interval with closeIntv and repeat from 2. 231 /// - Rewrite instructions with finish(). 232 /// 233 class SplitEditor { 234 SplitAnalysis &sa_; 235 LiveIntervals &lis_; 236 VirtRegMap &vrm_; 237 MachineRegisterInfo &mri_; 238 const TargetInstrInfo &tii_; 239 240 /// edit_ - The current parent register and new intervals created. 241 LiveRangeEdit &edit_; 242 243 /// dupli_ - Created as a copy of curli_, ranges are carved out as new 244 /// intervals get added through openIntv / closeIntv. This is used to avoid 245 /// editing curli_. 246 LiveIntervalMap dupli_; 247 248 /// Currently open LiveInterval. 249 LiveIntervalMap openli_; 250 251 /// intervalsLiveAt - Return true if any member of intervals_ is live at Idx. 252 bool intervalsLiveAt(SlotIndex Idx) const; 253 254 /// Values in curli whose live range has been truncated when entering an open 255 /// li. 256 SmallPtrSet<const VNInfo*, 8> truncatedValues; 257 258 /// addTruncSimpleRange - Add the given simple range to dupli_ after 259 /// truncating any overlap with intervals_. 260 void addTruncSimpleRange(SlotIndex Start, SlotIndex End, VNInfo *VNI); 261 262 /// computeRemainder - Compute the dupli liveness as the complement of all the 263 /// new intervals. 264 void computeRemainder(); 265 266 /// rewrite - Rewrite all uses of reg to use the new registers. 267 void rewrite(unsigned reg); 268 269 public: 270 /// Create a new SplitEditor for editing the LiveInterval analyzed by SA. 271 /// Newly created intervals will be appended to newIntervals. 272 SplitEditor(SplitAnalysis &SA, LiveIntervals&, VirtRegMap&, LiveRangeEdit&); 273 274 /// getAnalysis - Get the corresponding analysis. 275 SplitAnalysis &getAnalysis() { return sa_; } 276 277 /// Create a new virtual register and live interval. 278 void openIntv(); 279 280 /// enterIntvBefore - Enter openli before the instruction at Idx. If curli is 281 /// not live before Idx, a COPY is not inserted. 282 void enterIntvBefore(SlotIndex Idx); 283 284 /// enterIntvAtEnd - Enter openli at the end of MBB. 285 void enterIntvAtEnd(MachineBasicBlock &MBB); 286 287 /// useIntv - indicate that all instructions in MBB should use openli. 288 void useIntv(const MachineBasicBlock &MBB); 289 290 /// useIntv - indicate that all instructions in range should use openli. 291 void useIntv(SlotIndex Start, SlotIndex End); 292 293 /// leaveIntvAfter - Leave openli after the instruction at Idx. 294 void leaveIntvAfter(SlotIndex Idx); 295 296 /// leaveIntvAtTop - Leave the interval at the top of MBB. 297 /// Currently, only one value can leave the interval. 298 void leaveIntvAtTop(MachineBasicBlock &MBB); 299 300 /// closeIntv - Indicate that we are done editing the currently open 301 /// LiveInterval, and ranges can be trimmed. 302 void closeIntv(); 303 304 /// finish - after all the new live ranges have been created, compute the 305 /// remaining live range, and rewrite instructions to use the new registers. 306 void finish(); 307 308 // ===--- High level methods ---=== 309 310 /// splitAroundLoop - Split curli into a separate live interval inside 311 /// the loop. 312 void splitAroundLoop(const MachineLoop*); 313 314 /// splitSingleBlocks - Split curli into a separate live interval inside each 315 /// basic block in Blocks. 316 void splitSingleBlocks(const SplitAnalysis::BlockPtrSet &Blocks); 317 318 /// splitInsideBlock - Split curli into multiple intervals inside MBB. 319 void splitInsideBlock(const MachineBasicBlock *); 320 }; 321 322 } 323