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