1 //===---------- SplitKit.cpp - Toolkit for splitting live ranges ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the SplitAnalysis class as well as mutator functions for
11 // live range splitting.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/ADT/SmallPtrSet.h"
16 #include "llvm/ADT/DenseMap.h"
22 class MachineBasicBlock;
24 class MachineFunction;
25 class MachineFunctionPass;
27 class MachineLoopInfo;
28 class TargetInstrInfo;
31 const MachineFunction &mf_;
32 const LiveIntervals &lis_;
33 const MachineLoopInfo &loops_;
34 const TargetInstrInfo &tii_;
36 // Current live interval.
37 const LiveInterval *curli_;
39 // Instructions using the the current register.
40 typedef SmallPtrSet<const MachineInstr*, 16> InstrPtrSet;
41 InstrPtrSet usingInstrs_;
43 // The number of instructions using curli in each basic block.
44 typedef DenseMap<const MachineBasicBlock*, unsigned> BlockCountMap;
45 BlockCountMap usingBlocks_;
47 // Loops where the curent interval is used.
48 typedef SmallPtrSet<const MachineLoop*, 16> LoopPtrSet;
49 LoopPtrSet usingLoops_;
51 // Sumarize statistics by counting instructions using curli_.
54 /// canAnalyzeBranch - Return true if MBB ends in a branch that can be
56 bool canAnalyzeBranch(const MachineBasicBlock *MBB);
59 SplitAnalysis(const MachineFunction *mf, const LiveIntervals *lis,
60 const MachineLoopInfo *mli);
62 /// analyze - set curli to the specified interval, and analyze how it may be
64 void analyze(const LiveInterval *li);
66 /// clear - clear all data structures so SplitAnalysis is ready to analyze a
70 typedef SmallPtrSet<const MachineBasicBlock*, 16> BlockPtrSet;
72 // Sets of basic blocks surrounding a machine loop.
74 BlockPtrSet Loop; // Blocks in the loop.
75 BlockPtrSet Preds; // Loop predecessor blocks.
76 BlockPtrSet Exits; // Loop exit blocks.
85 // Calculate the block sets surrounding the loop.
86 void getLoopBlocks(const MachineLoop *Loop, LoopBlocks &Blocks);
88 /// LoopPeripheralUse - how is a variable used in and around a loop?
89 /// Peripheral blocks are the loop predecessors and exit blocks.
90 enum LoopPeripheralUse {
91 ContainedInLoop, // All uses are inside the loop.
92 SinglePeripheral, // At most one instruction per peripheral block.
93 MultiPeripheral, // Multiple instructions in some peripheral blocks.
94 OutsideLoop // Uses outside loop periphery.
97 /// analyzeLoopPeripheralUse - Return an enum describing how curli_ is used in
98 /// and around the Loop.
99 LoopPeripheralUse analyzeLoopPeripheralUse(const LoopBlocks&);
101 /// getCriticalExits - It may be necessary to partially break critical edges
102 /// leaving the loop if an exit block has phi uses of curli. Collect the exit
103 /// blocks that need special treatment into CriticalExits.
104 void getCriticalExits(const LoopBlocks &Blocks, BlockPtrSet &CriticalExits);
106 /// canSplitCriticalExits - Return true if it is possible to insert new exit
107 /// blocks before the blocks in CriticalExits.
108 bool canSplitCriticalExits(const LoopBlocks &Blocks,
109 BlockPtrSet &CriticalExits);
111 /// getBestSplitLoop - Return the loop where curli may best be split to a
112 /// separate register, or NULL.
113 const MachineLoop *getBestSplitLoop();
116 /// splitAroundLoop - Try to split curli into a separate live interval inside
117 /// the loop. Retun true on success.
118 bool splitAroundLoop(SplitAnalysis&, const MachineLoop*);