blob: 6ded4f464c026ae11673b64e9b7c04f3b2679f2d [file] [log] [blame]
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +00001//===---------- 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"
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +000017#include "llvm/CodeGen/SlotIndexes.h"
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000018
19namespace llvm {
20
21class LiveInterval;
22class LiveIntervals;
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000023class MachineInstr;
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000024class MachineLoop;
25class MachineLoopInfo;
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +000026class MachineRegisterInfo;
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +000027class TargetInstrInfo;
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +000028class VirtRegMap;
29class VNInfo;
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000030
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +000031/// SplitAnalysis - Analyze a LiveInterval, looking for live range splitting
32/// opportunities.
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000033class SplitAnalysis {
34 const MachineFunction &mf_;
35 const LiveIntervals &lis_;
36 const MachineLoopInfo &loops_;
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +000037 const TargetInstrInfo &tii_;
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000038
39 // Current live interval.
40 const LiveInterval *curli_;
41
42 // Instructions using the the current register.
43 typedef SmallPtrSet<const MachineInstr*, 16> InstrPtrSet;
44 InstrPtrSet usingInstrs_;
45
46 // The number of instructions using curli in each basic block.
47 typedef DenseMap<const MachineBasicBlock*, unsigned> BlockCountMap;
48 BlockCountMap usingBlocks_;
49
50 // Loops where the curent interval is used.
51 typedef SmallPtrSet<const MachineLoop*, 16> LoopPtrSet;
52 LoopPtrSet usingLoops_;
53
54 // Sumarize statistics by counting instructions using curli_.
Jakob Stoklund Olesenabff2802010-07-20 16:12:37 +000055 void analyzeUses();
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000056
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +000057 /// canAnalyzeBranch - Return true if MBB ends in a branch that can be
58 /// analyzed.
59 bool canAnalyzeBranch(const MachineBasicBlock *MBB);
60
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000061public:
Jakob Stoklund Olesenf2c6e362010-07-20 23:50:15 +000062 SplitAnalysis(const MachineFunction &mf, const LiveIntervals &lis,
63 const MachineLoopInfo &mli);
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000064
65 /// analyze - set curli to the specified interval, and analyze how it may be
66 /// split.
67 void analyze(const LiveInterval *li);
68
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +000069 const LiveInterval *getCurLI() { return curli_; }
70
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000071 /// clear - clear all data structures so SplitAnalysis is ready to analyze a
72 /// new interval.
73 void clear();
74
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +000075 typedef SmallPtrSet<const MachineBasicBlock*, 16> BlockPtrSet;
76
77 // Sets of basic blocks surrounding a machine loop.
78 struct LoopBlocks {
79 BlockPtrSet Loop; // Blocks in the loop.
80 BlockPtrSet Preds; // Loop predecessor blocks.
81 BlockPtrSet Exits; // Loop exit blocks.
82
83 void clear() {
84 Loop.clear();
85 Preds.clear();
86 Exits.clear();
87 }
88 };
89
90 // Calculate the block sets surrounding the loop.
91 void getLoopBlocks(const MachineLoop *Loop, LoopBlocks &Blocks);
92
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000093 /// LoopPeripheralUse - how is a variable used in and around a loop?
94 /// Peripheral blocks are the loop predecessors and exit blocks.
95 enum LoopPeripheralUse {
96 ContainedInLoop, // All uses are inside the loop.
97 SinglePeripheral, // At most one instruction per peripheral block.
98 MultiPeripheral, // Multiple instructions in some peripheral blocks.
99 OutsideLoop // Uses outside loop periphery.
100 };
101
102 /// analyzeLoopPeripheralUse - Return an enum describing how curli_ is used in
103 /// and around the Loop.
Jakob Stoklund Olesen6a0dc072010-07-20 21:46:58 +0000104 LoopPeripheralUse analyzeLoopPeripheralUse(const LoopBlocks&);
105
106 /// getCriticalExits - It may be necessary to partially break critical edges
107 /// leaving the loop if an exit block has phi uses of curli. Collect the exit
108 /// blocks that need special treatment into CriticalExits.
109 void getCriticalExits(const LoopBlocks &Blocks, BlockPtrSet &CriticalExits);
110
111 /// canSplitCriticalExits - Return true if it is possible to insert new exit
112 /// blocks before the blocks in CriticalExits.
113 bool canSplitCriticalExits(const LoopBlocks &Blocks,
114 BlockPtrSet &CriticalExits);
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000115
116 /// getBestSplitLoop - Return the loop where curli may best be split to a
117 /// separate register, or NULL.
118 const MachineLoop *getBestSplitLoop();
119};
120
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000121/// SplitEditor - Edit machine code and LiveIntervals for live range
122/// splitting.
123///
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000124/// - Create a SplitEditor from a SplitAnalysis. This will create a new
125/// LiveInterval, dupli, that is identical to SA.curli.
126/// - Start a new live interval with openIntv.
127/// - Mark the places where the new interval is entered using enterIntv*
128/// - Mark the ranges where the new interval is used with useIntv*
129/// - Mark the places where the interval is exited with exitIntv*.
130/// - Finish the current interval with closeIntv and repeat from 2.
131/// - Rewrite instructions with rewrite().
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000132///
133class SplitEditor {
134 SplitAnalysis &sa_;
135 LiveIntervals &lis_;
136 VirtRegMap &vrm_;
137 MachineRegisterInfo &mri_;
138 const TargetInstrInfo &tii_;
139
140 /// dupli_ - Created as a copy of sa_.curli_, ranges are carved out as new
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000141 /// intervals get added through openIntv / closeIntv.
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000142 LiveInterval *dupli_;
143
144 /// Currently open LiveInterval.
145 LiveInterval *openli_;
146
147 /// createInterval - Create a new virtual register and LiveInterval with same
148 /// register class and spill slot as curli.
149 LiveInterval *createInterval();
150
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000151 /// valueMap_ - Map values in dupli to values in openIntv. These are direct 1-1
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000152 /// mappings, and do not include values created by inserted copies.
153 DenseMap<VNInfo*,VNInfo*> valueMap_;
154
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000155 /// mapValue - Return the openIntv value that corresponds to the given dupli
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000156 /// value.
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000157 VNInfo *mapValue(VNInfo *dupliVNI);
158
159 /// A dupli value is live through openIntv.
160 bool liveThrough_;
161
162 /// All the new intervals created for this split are added to intervals_.
163 std::vector<LiveInterval*> &intervals_;
164
165 /// The index into intervals_ of the first interval we added. There may be
166 /// others from before we got it.
167 unsigned firstInterval;
168
169 /// Insert a COPY instruction curli -> li. Allocate a new value from li
170 /// defined by the COPY
171 VNInfo *insertCopy(LiveInterval &LI,
172 MachineBasicBlock &MBB,
173 MachineBasicBlock::iterator I);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000174
175public:
176 /// Create a new SplitEditor for editing the LiveInterval analyzed by SA.
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000177 /// Newly created intervals will be appended to newIntervals.
178 SplitEditor(SplitAnalysis &SA, LiveIntervals&, VirtRegMap&,
179 std::vector<LiveInterval*> &newIntervals);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000180
181 /// getAnalysis - Get the corresponding analysis.
182 SplitAnalysis &getAnalysis() { return sa_; }
183
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000184 /// Create a new virtual register and live interval.
185 void openIntv();
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000186
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000187 /// enterIntvAtEnd - Enter openli at the end of MBB.
188 /// PhiMBB is a successor inside openli where a PHI value is created.
189 /// Currently, all entries must share the same PhiMBB.
190 void enterIntvAtEnd(MachineBasicBlock &MBB, MachineBasicBlock &PhiMBB);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000191
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000192 /// useIntv - indicate that all instructions in MBB should use openli.
193 void useIntv(const MachineBasicBlock &MBB);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000194
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000195 /// useIntv - indicate that all instructions in range should use openli.
196 void useIntv(SlotIndex Start, SlotIndex End);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000197
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000198 /// leaveIntvAtTop - Leave the interval at the top of MBB.
199 /// Currently, only one value can leave the interval.
200 void leaveIntvAtTop(MachineBasicBlock &MBB);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000201
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000202 /// closeIntv - Indicate that we are done editing the currently open
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000203 /// LiveInterval, and ranges can be trimmed.
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000204 void closeIntv();
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000205
206 /// rewrite - after all the new live ranges have been created, rewrite
207 /// instructions using curli to use the new intervals.
208 void rewrite();
209
210 // ===--- High level methods ---===
211
212 /// splitAroundLoop - Split curli into a separate live interval inside
213 /// the loop.
214 void splitAroundLoop(const MachineLoop*);
215
216};
217
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000218
219}