blob: 01ea6092e8c5cb13079cb61d639f09ac474fc395 [file] [log] [blame]
Chris Lattnera3b8b5c2004-07-23 17:56:30 +00001//===-- LiveIntervalAnalysis.h - Live Interval Analysis ---------*- C++ -*-===//
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner7ed47a12007-12-29 19:59:42 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattner6b929062004-07-19 02:13:59 +000010// This file implements the LiveInterval analysis pass. Given some numbering of
11// each the machine instructions (in this implemention depth-first order) an
12// interval [i, j) is said to be a live interval for register v if there is no
Dan Gohman8131a502008-03-13 23:04:27 +000013// instruction with number j' > j such that v is live at j' and there is no
Chris Lattner6b929062004-07-19 02:13:59 +000014// instruction with number i' < i such that v is live at i'. In this
15// implementation intervals can have holes, i.e. an interval might look like
16// [1,20), [50,65), [1000,1001).
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000017//
18//===----------------------------------------------------------------------===//
19
Chris Lattnera3b8b5c2004-07-23 17:56:30 +000020#ifndef LLVM_CODEGEN_LIVEINTERVAL_ANALYSIS_H
21#define LLVM_CODEGEN_LIVEINTERVAL_ANALYSIS_H
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000022
23#include "llvm/CodeGen/MachineFunctionPass.h"
Chris Lattner779a6512005-09-21 04:18:25 +000024#include "llvm/CodeGen/LiveInterval.h"
Evan Cheng61de82d2007-02-15 05:59:24 +000025#include "llvm/ADT/BitVector.h"
Evan Cheng20b0abc2007-04-17 20:32:26 +000026#include "llvm/ADT/DenseMap.h"
Evan Cheng8f90b6e2009-01-07 02:08:57 +000027#include "llvm/ADT/SmallPtrSet.h"
Evan Cheng549f27d32007-08-13 23:45:17 +000028#include "llvm/ADT/SmallVector.h"
Evan Chengf3bb2e62007-09-05 21:46:51 +000029#include "llvm/Support/Allocator.h"
Hartmut Kaiserffb15de2007-11-13 23:04:28 +000030#include <cmath>
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000031
32namespace llvm {
33
Dan Gohman6d69ba82008-07-25 00:02:30 +000034 class AliasAnalysis;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000035 class LiveVariables;
Evan Cheng22f07ff2007-12-11 02:09:15 +000036 class MachineLoopInfo;
Dan Gohman6f0d0242008-02-10 18:45:23 +000037 class TargetRegisterInfo;
Chris Lattner84bc5422007-12-31 04:13:23 +000038 class MachineRegisterInfo;
Chris Lattnerf768bba2005-03-09 23:05:19 +000039 class TargetInstrInfo;
Evan Cheng20b0abc2007-04-17 20:32:26 +000040 class TargetRegisterClass;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000041 class VirtRegMap;
Evan Cheng4ca980e2007-10-17 02:10:22 +000042 typedef std::pair<unsigned, MachineBasicBlock*> IdxMBBPair;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000043
Roman Levenstein8dd25282008-02-18 09:35:30 +000044 inline bool operator<(unsigned V, const IdxMBBPair &IM) {
45 return V < IM.first;
46 }
47
48 inline bool operator<(const IdxMBBPair &IM, unsigned V) {
49 return IM.first < V;
50 }
51
52 struct Idx2MBBCompare {
53 bool operator()(const IdxMBBPair &LHS, const IdxMBBPair &RHS) const {
54 return LHS.first < RHS.first;
55 }
56 };
Owen Anderson20e28392008-08-13 22:08:30 +000057
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000058 class LiveIntervals : public MachineFunctionPass {
59 MachineFunction* mf_;
Evan Chengd70dbb52008-02-22 09:24:50 +000060 MachineRegisterInfo* mri_;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000061 const TargetMachine* tm_;
Dan Gohman6f0d0242008-02-10 18:45:23 +000062 const TargetRegisterInfo* tri_;
Chris Lattnerf768bba2005-03-09 23:05:19 +000063 const TargetInstrInfo* tii_;
Dan Gohman6d69ba82008-07-25 00:02:30 +000064 AliasAnalysis *aa_;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000065 LiveVariables* lv_;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000066
Evan Chengf3bb2e62007-09-05 21:46:51 +000067 /// Special pool allocator for VNInfo's (LiveInterval val#).
68 ///
69 BumpPtrAllocator VNInfoAllocator;
70
Evan Cheng549f27d32007-08-13 23:45:17 +000071 /// MBB2IdxMap - The indexes of the first and last instructions in the
72 /// specified basic block.
73 std::vector<std::pair<unsigned, unsigned> > MBB2IdxMap;
David Greene25133302007-06-08 17:18:56 +000074
Evan Cheng4ca980e2007-10-17 02:10:22 +000075 /// Idx2MBBMap - Sorted list of pairs of index of first instruction
76 /// and MBB id.
77 std::vector<IdxMBBPair> Idx2MBBMap;
78
Owen Andersona1566f22008-07-22 22:46:49 +000079 /// FunctionSize - The number of instructions present in the function
80 uint64_t FunctionSize;
81
Owen Anderson49bfdd62008-08-13 21:24:24 +000082 typedef DenseMap<MachineInstr*, unsigned> Mi2IndexMap;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000083 Mi2IndexMap mi2iMap_;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000084
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000085 typedef std::vector<MachineInstr*> Index2MiMap;
86 Index2MiMap i2miMap_;
Alkis Evlogimenos843b1602004-02-15 10:24:21 +000087
Owen Anderson20e28392008-08-13 22:08:30 +000088 typedef DenseMap<unsigned, LiveInterval*> Reg2IntervalMap;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000089 Reg2IntervalMap r2iMap_;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000090
Evan Cheng61de82d2007-02-15 05:59:24 +000091 BitVector allocatableRegs_;
Evan Cheng88d1f582007-03-01 02:03:03 +000092
Evan Cheng549f27d32007-08-13 23:45:17 +000093 std::vector<MachineInstr*> ClonedMIs;
94
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000095 public:
Nick Lewyckyecd94c82007-05-06 13:37:16 +000096 static char ID; // Pass identification, replacement for typeid
Dan Gohmanae73dc12008-09-04 17:05:41 +000097 LiveIntervals() : MachineFunctionPass(&ID) {}
Devang Patel794fd752007-05-01 21:15:47 +000098
Chris Lattnerf7da2c72006-08-24 22:43:55 +000099 struct InstrSlots {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000100 enum {
101 LOAD = 0,
102 USE = 1,
103 DEF = 2,
104 STORE = 3,
Chris Lattner410354f2006-02-22 16:23:43 +0000105 NUM = 4
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000106 };
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000107 };
108
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000109 static unsigned getBaseIndex(unsigned index) {
110 return index - (index % InstrSlots::NUM);
111 }
112 static unsigned getBoundaryIndex(unsigned index) {
113 return getBaseIndex(index + InstrSlots::NUM - 1);
114 }
115 static unsigned getLoadIndex(unsigned index) {
116 return getBaseIndex(index) + InstrSlots::LOAD;
117 }
118 static unsigned getUseIndex(unsigned index) {
119 return getBaseIndex(index) + InstrSlots::USE;
120 }
121 static unsigned getDefIndex(unsigned index) {
122 return getBaseIndex(index) + InstrSlots::DEF;
123 }
124 static unsigned getStoreIndex(unsigned index) {
125 return getBaseIndex(index) + InstrSlots::STORE;
126 }
127
Evan Chengc3417602008-06-21 06:45:54 +0000128 static float getSpillWeight(bool isDef, bool isUse, unsigned loopDepth) {
129 return (isDef + isUse) * powf(10.0F, (float)loopDepth);
Evan Chengf2fbca62007-11-12 06:35:08 +0000130 }
131
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000132 typedef Reg2IntervalMap::iterator iterator;
Chris Lattner70ca3582004-09-30 15:59:17 +0000133 typedef Reg2IntervalMap::const_iterator const_iterator;
134 const_iterator begin() const { return r2iMap_.begin(); }
135 const_iterator end() const { return r2iMap_.end(); }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000136 iterator begin() { return r2iMap_.begin(); }
137 iterator end() { return r2iMap_.end(); }
Evan Cheng34cd4a42008-05-05 18:30:58 +0000138 unsigned getNumIntervals() const { return (unsigned)r2iMap_.size(); }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000139
140 LiveInterval &getInterval(unsigned reg) {
141 Reg2IntervalMap::iterator I = r2iMap_.find(reg);
142 assert(I != r2iMap_.end() && "Interval does not exist for register");
Owen Anderson03857b22008-08-13 21:49:13 +0000143 return *I->second;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000144 }
145
146 const LiveInterval &getInterval(unsigned reg) const {
147 Reg2IntervalMap::const_iterator I = r2iMap_.find(reg);
148 assert(I != r2iMap_.end() && "Interval does not exist for register");
Owen Anderson03857b22008-08-13 21:49:13 +0000149 return *I->second;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000150 }
151
Evan Chengb371f452007-02-19 21:49:54 +0000152 bool hasInterval(unsigned reg) const {
Evan Cheng88d1f582007-03-01 02:03:03 +0000153 return r2iMap_.count(reg);
Evan Chengb371f452007-02-19 21:49:54 +0000154 }
155
Chris Lattner428b92e2006-09-15 03:57:23 +0000156 /// getMBBStartIdx - Return the base index of the first instruction in the
157 /// specified MachineBasicBlock.
158 unsigned getMBBStartIdx(MachineBasicBlock *MBB) const {
159 return getMBBStartIdx(MBB->getNumber());
160 }
Chris Lattner428b92e2006-09-15 03:57:23 +0000161 unsigned getMBBStartIdx(unsigned MBBNo) const {
162 assert(MBBNo < MBB2IdxMap.size() && "Invalid MBB number!");
Evan Cheng549f27d32007-08-13 23:45:17 +0000163 return MBB2IdxMap[MBBNo].first;
164 }
165
166 /// getMBBEndIdx - Return the store index of the last instruction in the
167 /// specified MachineBasicBlock.
168 unsigned getMBBEndIdx(MachineBasicBlock *MBB) const {
169 return getMBBEndIdx(MBB->getNumber());
170 }
171 unsigned getMBBEndIdx(unsigned MBBNo) const {
172 assert(MBBNo < MBB2IdxMap.size() && "Invalid MBB number!");
173 return MBB2IdxMap[MBBNo].second;
Chris Lattner428b92e2006-09-15 03:57:23 +0000174 }
175
Owen Andersona1566f22008-07-22 22:46:49 +0000176 /// getScaledIntervalSize - get the size of an interval in "units,"
Owen Anderson72e04092008-06-23 23:25:37 +0000177 /// where every function is composed of one thousand units. This
178 /// measure scales properly with empty index slots in the function.
Owen Andersona1566f22008-07-22 22:46:49 +0000179 double getScaledIntervalSize(LiveInterval& I) {
180 return (1000.0 / InstrSlots::NUM * I.getSize()) / i2miMap_.size();
181 }
182
183 /// getApproximateInstructionCount - computes an estimate of the number
184 /// of instructions in a given LiveInterval.
185 unsigned getApproximateInstructionCount(LiveInterval& I) {
186 double IntervalPercentage = getScaledIntervalSize(I) / 1000.0;
Matthijs Kooijmanb3e15c02008-08-07 13:36:30 +0000187 return (unsigned)(IntervalPercentage * FunctionSize);
Owen Anderson72e04092008-06-23 23:25:37 +0000188 }
189
Roman Levenstein8dd25282008-02-18 09:35:30 +0000190 /// getMBBFromIndex - given an index in any instruction of an
191 /// MBB return a pointer the MBB
192 MachineBasicBlock* getMBBFromIndex(unsigned index) const {
193 std::vector<IdxMBBPair>::const_iterator I =
Bill Wendlinge85fe662008-02-26 10:49:39 +0000194 std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), index);
Roman Levenstein8dd25282008-02-18 09:35:30 +0000195 // Take the pair containing the index
196 std::vector<IdxMBBPair>::const_iterator J =
Bill Wendlinge85fe662008-02-26 10:49:39 +0000197 ((I != Idx2MBBMap.end() && I->first > index) ||
198 (I == Idx2MBBMap.end() && Idx2MBBMap.size()>0)) ? (I-1): I;
Roman Levenstein8dd25282008-02-18 09:35:30 +0000199
200 assert(J != Idx2MBBMap.end() && J->first < index+1 &&
Bill Wendlinge85fe662008-02-26 10:49:39 +0000201 index <= getMBBEndIdx(J->second) &&
202 "index does not correspond to an MBB");
Roman Levenstein8dd25282008-02-18 09:35:30 +0000203 return J->second;
204 }
205
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000206 /// getInstructionIndex - returns the base index of instr
207 unsigned getInstructionIndex(MachineInstr* instr) const {
208 Mi2IndexMap::const_iterator it = mi2iMap_.find(instr);
209 assert(it != mi2iMap_.end() && "Invalid instruction!");
210 return it->second;
211 }
212
213 /// getInstructionFromIndex - given an index in any slot of an
214 /// instruction return a pointer the instruction
215 MachineInstr* getInstructionFromIndex(unsigned index) const {
216 index /= InstrSlots::NUM; // convert index to vector index
217 assert(index < i2miMap_.size() &&
218 "index does not correspond to an instruction");
219 return i2miMap_[index];
220 }
David Greene25133302007-06-08 17:18:56 +0000221
Evan Chengf5cd4f02008-10-23 20:43:13 +0000222 /// hasGapBeforeInstr - Return true if the previous instruction slot,
223 /// i.e. Index - InstrSlots::NUM, is not occupied.
224 bool hasGapBeforeInstr(unsigned Index) {
225 Index = getBaseIndex(Index - InstrSlots::NUM);
226 return getInstructionFromIndex(Index) == 0;
227 }
228
229 /// findGapBeforeInstr - Find an empty instruction slot before the
230 /// specified index. If "Furthest" is true, find one that's furthest
231 /// away from the index (but before any index that's occupied).
232 unsigned findGapBeforeInstr(unsigned Index, bool Furthest = false) {
233 Index = getBaseIndex(Index - InstrSlots::NUM);
234 if (getInstructionFromIndex(Index))
235 return 0; // No gap!
236 if (!Furthest)
237 return Index;
238 unsigned PrevIndex = getBaseIndex(Index - InstrSlots::NUM);
239 while (getInstructionFromIndex(Index)) {
240 Index = PrevIndex;
241 PrevIndex = getBaseIndex(Index - InstrSlots::NUM);
242 }
243 return Index;
244 }
245
246 /// InsertMachineInstrInMaps - Insert the specified machine instruction
247 /// into the instruction index map at the given index.
248 void InsertMachineInstrInMaps(MachineInstr *MI, unsigned Index) {
249 i2miMap_[Index / InstrSlots::NUM] = MI;
250 Mi2IndexMap::iterator it = mi2iMap_.find(MI);
251 assert(it == mi2iMap_.end() && "Already in map!");
252 mi2iMap_[MI] = Index;
253 }
254
Evan Chengc92da382007-11-03 07:20:12 +0000255 /// conflictsWithPhysRegDef - Returns true if the specified register
256 /// is defined during the duration of the specified interval.
257 bool conflictsWithPhysRegDef(const LiveInterval &li, VirtRegMap &vrm,
258 unsigned reg);
259
Evan Cheng8f90b6e2009-01-07 02:08:57 +0000260 /// conflictsWithPhysRegRef - Similar to conflictsWithPhysRegRef except
261 /// it can check use as well.
262 bool conflictsWithPhysRegRef(LiveInterval &li, unsigned Reg,
263 bool CheckUse,
264 SmallPtrSet<MachineInstr*,32> &JoinedCopies);
265
Evan Cheng4ca980e2007-10-17 02:10:22 +0000266 /// findLiveInMBBs - Given a live range, if the value of the range
267 /// is live in any MBB returns true as well as the list of basic blocks
Dan Gohmanca425a22008-07-28 18:42:57 +0000268 /// in which the value is live.
Evan Chengd0e32c52008-10-29 05:06:14 +0000269 bool findLiveInMBBs(unsigned Start, unsigned End,
270 SmallVectorImpl<MachineBasicBlock*> &MBBs) const;
271
272 /// findReachableMBBs - Return a list MBB that can be reached via any
273 /// branch or fallthroughs. Return true if the list is not empty.
274 bool findReachableMBBs(unsigned Start, unsigned End,
Evan Chenga5bfc972007-10-17 06:53:44 +0000275 SmallVectorImpl<MachineBasicBlock*> &MBBs) const;
Evan Cheng4ca980e2007-10-17 02:10:22 +0000276
David Greene25133302007-06-08 17:18:56 +0000277 // Interval creation
278
279 LiveInterval &getOrCreateInterval(unsigned reg) {
280 Reg2IntervalMap::iterator I = r2iMap_.find(reg);
281 if (I == r2iMap_.end())
Owen Anderson20e28392008-08-13 22:08:30 +0000282 I = r2iMap_.insert(std::make_pair(reg, createInterval(reg))).first;
Owen Anderson03857b22008-08-13 21:49:13 +0000283 return *I->second;
David Greene25133302007-06-08 17:18:56 +0000284 }
Evan Cheng0a1fcce2009-02-08 11:04:35 +0000285
286 /// dupInterval - Duplicate a live interval. The caller is responsible for
287 /// managing the allocated memory.
288 LiveInterval *dupInterval(LiveInterval *li);
Owen Andersonc4dc1322008-06-05 17:15:43 +0000289
290 /// addLiveRangeToEndOfBlock - Given a register and an instruction,
291 /// adds a live range from that instruction to the end of its MBB.
292 LiveRange addLiveRangeToEndOfBlock(unsigned reg,
293 MachineInstr* startInst);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000294
David Greene25133302007-06-08 17:18:56 +0000295 // Interval removal
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000296
David Greene25133302007-06-08 17:18:56 +0000297 void removeInterval(unsigned Reg) {
Owen Anderson20e28392008-08-13 22:08:30 +0000298 DenseMap<unsigned, LiveInterval*>::iterator I = r2iMap_.find(Reg);
Owen Anderson03857b22008-08-13 21:49:13 +0000299 delete I->second;
300 r2iMap_.erase(I);
Bill Wendling5c7e3262006-12-17 05:15:13 +0000301 }
Chris Lattner70ca3582004-09-30 15:59:17 +0000302
Evan Cheng5b69eba2009-04-21 22:46:52 +0000303 /// isNotInMIMap - returns true if the specified machine instr has been
304 /// removed or was never entered in the map.
305 bool isNotInMIMap(MachineInstr* instr) const {
Evan Cheng7d35c0e2007-02-22 23:52:23 +0000306 return !mi2iMap_.count(instr);
Evan Cheng30cac022007-02-22 23:03:39 +0000307 }
308
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000309 /// RemoveMachineInstrFromMaps - This marks the specified machine instr as
310 /// deleted.
311 void RemoveMachineInstrFromMaps(MachineInstr *MI) {
312 // remove index -> MachineInstr and
313 // MachineInstr -> index mappings
314 Mi2IndexMap::iterator mi2i = mi2iMap_.find(MI);
315 if (mi2i != mi2iMap_.end()) {
316 i2miMap_[mi2i->second/InstrSlots::NUM] = 0;
317 mi2iMap_.erase(mi2i);
318 }
319 }
David Greene25133302007-06-08 17:18:56 +0000320
Evan Cheng70071432008-02-13 03:01:43 +0000321 /// ReplaceMachineInstrInMaps - Replacing a machine instr with a new one in
322 /// maps used by register allocator.
323 void ReplaceMachineInstrInMaps(MachineInstr *MI, MachineInstr *NewMI) {
324 Mi2IndexMap::iterator mi2i = mi2iMap_.find(MI);
Evan Chengb1f6f912008-02-13 09:18:16 +0000325 if (mi2i == mi2iMap_.end())
326 return;
327 i2miMap_[mi2i->second/InstrSlots::NUM] = NewMI;
328 Mi2IndexMap::iterator it = mi2iMap_.find(MI);
329 assert(it != mi2iMap_.end() && "Invalid instruction!");
330 unsigned Index = it->second;
331 mi2iMap_.erase(it);
332 mi2iMap_[NewMI] = Index;
Evan Cheng70071432008-02-13 03:01:43 +0000333 }
334
Evan Chengf3bb2e62007-09-05 21:46:51 +0000335 BumpPtrAllocator& getVNInfoAllocator() { return VNInfoAllocator; }
336
Evan Chengc8d044e2008-02-15 18:24:29 +0000337 /// getVNInfoSourceReg - Helper function that parses the specified VNInfo
338 /// copy field and returns the source register that defines it.
339 unsigned getVNInfoSourceReg(const VNInfo *VNI) const;
340
David Greene25133302007-06-08 17:18:56 +0000341 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
342 virtual void releaseMemory();
343
344 /// runOnMachineFunction - pass entry point
345 virtual bool runOnMachineFunction(MachineFunction&);
346
347 /// print - Implement the dump method.
348 virtual void print(std::ostream &O, const Module* = 0) const;
349 void print(std::ostream *O, const Module* M = 0) const {
350 if (O) print(*O, M);
351 }
352
Evan Chengf2fbca62007-11-12 06:35:08 +0000353 /// addIntervalsForSpills - Create new intervals for spilled defs / uses of
Evan Cheng9c3c2212008-06-06 07:54:39 +0000354 /// the given interval. FIXME: It also returns the weight of the spill slot
355 /// (if any is created) by reference. This is temporary.
Evan Chengf2fbca62007-11-12 06:35:08 +0000356 std::vector<LiveInterval*>
Evan Cheng81a03822007-11-17 00:40:40 +0000357 addIntervalsForSpills(const LiveInterval& i,
Evan Chengdc377862008-09-30 15:44:16 +0000358 SmallVectorImpl<LiveInterval*> &SpillIs,
Evan Chengc781a242009-05-03 18:32:42 +0000359 const MachineLoopInfo *loopInfo, VirtRegMap& vrm);
Owen Andersond6664312008-08-18 18:05:32 +0000360
361 /// addIntervalsForSpillsFast - Quickly create new intervals for spilled
362 /// defs / uses without remat or splitting.
363 std::vector<LiveInterval*>
364 addIntervalsForSpillsFast(const LiveInterval &li,
Evan Chengc781a242009-05-03 18:32:42 +0000365 const MachineLoopInfo *loopInfo, VirtRegMap &vrm);
Evan Chengf2fbca62007-11-12 06:35:08 +0000366
Evan Cheng676dd7c2008-03-11 07:19:34 +0000367 /// spillPhysRegAroundRegDefsUses - Spill the specified physical register
Evan Cheng2824a652009-03-23 18:24:37 +0000368 /// around all defs and uses of the specified interval. Return true if it
369 /// was able to cut its interval.
370 bool spillPhysRegAroundRegDefsUses(const LiveInterval &li,
Evan Cheng676dd7c2008-03-11 07:19:34 +0000371 unsigned PhysReg, VirtRegMap &vrm);
372
Evan Cheng5ef3a042007-12-06 00:01:56 +0000373 /// isReMaterializable - Returns true if every definition of MI of every
374 /// val# of the specified interval is re-materializable. Also returns true
375 /// by reference if all of the defs are load instructions.
Evan Chengdc377862008-09-30 15:44:16 +0000376 bool isReMaterializable(const LiveInterval &li,
377 SmallVectorImpl<LiveInterval*> &SpillIs,
378 bool &isLoad);
Evan Cheng5ef3a042007-12-06 00:01:56 +0000379
Evan Cheng06587492008-10-24 02:05:00 +0000380 /// isReMaterializable - Returns true if the definition MI of the specified
381 /// val# of the specified interval is re-materializable.
382 bool isReMaterializable(const LiveInterval &li, const VNInfo *ValNo,
383 MachineInstr *MI);
384
Evan Cheng676dd7c2008-03-11 07:19:34 +0000385 /// getRepresentativeReg - Find the largest super register of the specified
386 /// physical register.
387 unsigned getRepresentativeReg(unsigned Reg) const;
388
389 /// getNumConflictsWithPhysReg - Return the number of uses and defs of the
390 /// specified interval that conflicts with the specified physical register.
391 unsigned getNumConflictsWithPhysReg(const LiveInterval &li,
392 unsigned PhysReg) const;
393
Owen Anderson15a17f52008-05-30 20:14:04 +0000394 /// computeNumbering - Compute the index numbering.
395 void computeNumbering();
396
Owen Anderson0c2e7b92009-01-13 06:05:10 +0000397 /// intervalIsInOneMBB - Returns true if the specified interval is entirely
398 /// within a single basic block.
399 bool intervalIsInOneMBB(const LiveInterval &li) const;
400
David Greene25133302007-06-08 17:18:56 +0000401 private:
Chris Lattner428b92e2006-09-15 03:57:23 +0000402 /// computeIntervals - Compute live intervals.
Chris Lattnerc7695eb2006-09-14 06:42:17 +0000403 void computeIntervals();
Chris Lattner6bda49f2006-09-02 05:26:01 +0000404
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000405 /// handleRegisterDef - update intervals for a register def
406 /// (calls handlePhysicalRegisterDef and
407 /// handleVirtualRegisterDef)
Chris Lattner6b128bd2006-09-03 08:07:11 +0000408 void handleRegisterDef(MachineBasicBlock *MBB,
409 MachineBasicBlock::iterator MI, unsigned MIIdx,
Evan Chengef0732d2008-07-10 07:35:43 +0000410 MachineOperand& MO, unsigned MOIdx);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000411
412 /// handleVirtualRegisterDef - update intervals for a virtual
413 /// register def
Chris Lattner6b128bd2006-09-03 08:07:11 +0000414 void handleVirtualRegisterDef(MachineBasicBlock *MBB,
415 MachineBasicBlock::iterator MI,
Owen Anderson6b098de2008-06-25 23:39:39 +0000416 unsigned MIIdx, MachineOperand& MO,
Evan Chengef0732d2008-07-10 07:35:43 +0000417 unsigned MOIdx, LiveInterval& interval);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000418
Chris Lattnerf768bba2005-03-09 23:05:19 +0000419 /// handlePhysicalRegisterDef - update intervals for a physical register
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000420 /// def.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000421 void handlePhysicalRegisterDef(MachineBasicBlock* mbb,
422 MachineBasicBlock::iterator mi,
Owen Anderson6b098de2008-06-25 23:39:39 +0000423 unsigned MIIdx, MachineOperand& MO,
Chris Lattner91725b72006-08-31 05:54:43 +0000424 LiveInterval &interval,
Evan Chengc8d044e2008-02-15 18:24:29 +0000425 MachineInstr *CopyMI);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000426
Evan Chengb371f452007-02-19 21:49:54 +0000427 /// handleLiveInRegister - Create interval for a livein register.
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000428 void handleLiveInRegister(MachineBasicBlock* mbb,
429 unsigned MIIdx,
Evan Cheng24a3cc42007-04-25 07:30:23 +0000430 LiveInterval &interval, bool isAlias = false);
Evan Chengb371f452007-02-19 21:49:54 +0000431
Evan Chengd70dbb52008-02-22 09:24:50 +0000432 /// getReMatImplicitUse - If the remat definition MI has one (for now, we
433 /// only allow one) virtual register operand, then its uses are implicitly
434 /// using the register. Returns the virtual register.
435 unsigned getReMatImplicitUse(const LiveInterval &li,
436 MachineInstr *MI) const;
437
438 /// isValNoAvailableAt - Return true if the val# of the specified interval
439 /// which reaches the given instruction also reaches the specified use
440 /// index.
441 bool isValNoAvailableAt(const LiveInterval &li, MachineInstr *MI,
442 unsigned UseIdx) const;
443
Evan Cheng549f27d32007-08-13 23:45:17 +0000444 /// isReMaterializable - Returns true if the definition MI of the specified
Evan Cheng5ef3a042007-12-06 00:01:56 +0000445 /// val# of the specified interval is re-materializable. Also returns true
446 /// by reference if the def is a load.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000447 bool isReMaterializable(const LiveInterval &li, const VNInfo *ValNo,
Evan Chengdc377862008-09-30 15:44:16 +0000448 MachineInstr *MI,
449 SmallVectorImpl<LiveInterval*> &SpillIs,
450 bool &isLoad);
Evan Cheng549f27d32007-08-13 23:45:17 +0000451
Evan Cheng35b35c52007-08-30 05:52:20 +0000452 /// tryFoldMemoryOperand - Attempts to fold either a spill / restore from
453 /// slot / to reg or any rematerialized load into ith operand of specified
454 /// MI. If it is successul, MI is updated with the newly created MI and
455 /// returns true.
456 bool tryFoldMemoryOperand(MachineInstr* &MI, VirtRegMap &vrm,
Evan Chengcddbb832007-11-30 21:23:43 +0000457 MachineInstr *DefMI, unsigned InstrIdx,
Evan Chengaee4af62007-12-02 08:30:39 +0000458 SmallVector<unsigned, 2> &Ops,
Evan Chengcddbb832007-11-30 21:23:43 +0000459 bool isSS, int Slot, unsigned Reg);
Evan Cheng549f27d32007-08-13 23:45:17 +0000460
Evan Chengd70dbb52008-02-22 09:24:50 +0000461 /// canFoldMemoryOperand - Return true if the specified load / store
Evan Cheng018f9b02007-12-05 03:22:34 +0000462 /// folding is possible.
Evan Chengd64b5c82007-12-05 03:14:33 +0000463 bool canFoldMemoryOperand(MachineInstr *MI,
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000464 SmallVector<unsigned, 2> &Ops,
465 bool ReMatLoadSS) const;
Evan Chengd64b5c82007-12-05 03:14:33 +0000466
Evan Cheng0cbb1162007-11-29 01:06:25 +0000467 /// anyKillInMBBAfterIdx - Returns true if there is a kill of the specified
468 /// VNInfo that's after the specified index but is within the basic block.
469 bool anyKillInMBBAfterIdx(const LiveInterval &li, const VNInfo *VNI,
470 MachineBasicBlock *MBB, unsigned Idx) const;
Evan Cheng81a03822007-11-17 00:40:40 +0000471
Evan Cheng676dd7c2008-03-11 07:19:34 +0000472 /// hasAllocatableSuperReg - Return true if the specified physical register
473 /// has any super register that's allocatable.
474 bool hasAllocatableSuperReg(unsigned Reg) const;
475
Evan Cheng1953d0c2007-11-29 10:12:14 +0000476 /// SRInfo - Spill / restore info.
477 struct SRInfo {
478 int index;
479 unsigned vreg;
480 bool canFold;
481 SRInfo(int i, unsigned vr, bool f) : index(i), vreg(vr), canFold(f) {};
482 };
483
484 bool alsoFoldARestore(int Id, int index, unsigned vr,
485 BitVector &RestoreMBBs,
Owen Anderson28998312008-08-13 22:28:50 +0000486 DenseMap<unsigned,std::vector<SRInfo> >&RestoreIdxes);
Evan Cheng1953d0c2007-11-29 10:12:14 +0000487 void eraseRestoreInfo(int Id, int index, unsigned vr,
488 BitVector &RestoreMBBs,
Owen Anderson28998312008-08-13 22:28:50 +0000489 DenseMap<unsigned,std::vector<SRInfo> >&RestoreIdxes);
Evan Cheng1953d0c2007-11-29 10:12:14 +0000490
Evan Cheng4cce6b42008-04-11 17:53:36 +0000491 /// handleSpilledImpDefs - Remove IMPLICIT_DEF instructions which are being
492 /// spilled and create empty intervals for their uses.
493 void handleSpilledImpDefs(const LiveInterval &li, VirtRegMap &vrm,
494 const TargetRegisterClass* rc,
495 std::vector<LiveInterval*> &NewLIs);
Evan Cheng419852c2008-04-03 16:39:43 +0000496
Evan Chengd70dbb52008-02-22 09:24:50 +0000497 /// rewriteImplicitOps - Rewrite implicit use operands of MI (i.e. uses of
498 /// interval on to-be re-materialized operands of MI) with new register.
499 void rewriteImplicitOps(const LiveInterval &li,
500 MachineInstr *MI, unsigned NewVReg, VirtRegMap &vrm);
501
Chris Lattner84bc5422007-12-31 04:13:23 +0000502 /// rewriteInstructionForSpills, rewriteInstructionsForSpills - Helper
503 /// functions for addIntervalsForSpills to rewrite uses / defs for the given
504 /// live range.
Evan Chengd70dbb52008-02-22 09:24:50 +0000505 bool rewriteInstructionForSpills(const LiveInterval &li, const VNInfo *VNI,
506 bool TrySplit, unsigned index, unsigned end, MachineInstr *MI,
Evan Chengf2fbca62007-11-12 06:35:08 +0000507 MachineInstr *OrigDefMI, MachineInstr *DefMI, unsigned Slot, int LdSlot,
508 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
Evan Chengd70dbb52008-02-22 09:24:50 +0000509 VirtRegMap &vrm, const TargetRegisterClass* rc,
510 SmallVector<int, 4> &ReMatIds, const MachineLoopInfo *loopInfo,
Evan Cheng0cc83b62008-02-23 00:46:11 +0000511 unsigned &NewVReg, unsigned ImpUse, bool &HasDef, bool &HasUse,
Owen Anderson28998312008-08-13 22:28:50 +0000512 DenseMap<unsigned,unsigned> &MBBVRegsMap,
Evan Chengc781a242009-05-03 18:32:42 +0000513 std::vector<LiveInterval*> &NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +0000514 void rewriteInstructionsForSpills(const LiveInterval &li, bool TrySplit,
Evan Chengf2fbca62007-11-12 06:35:08 +0000515 LiveInterval::Ranges::const_iterator &I,
516 MachineInstr *OrigDefMI, MachineInstr *DefMI, unsigned Slot, int LdSlot,
517 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
Evan Chengd70dbb52008-02-22 09:24:50 +0000518 VirtRegMap &vrm, const TargetRegisterClass* rc,
Evan Cheng22f07ff2007-12-11 02:09:15 +0000519 SmallVector<int, 4> &ReMatIds, const MachineLoopInfo *loopInfo,
Evan Cheng81a03822007-11-17 00:40:40 +0000520 BitVector &SpillMBBs,
Owen Anderson28998312008-08-13 22:28:50 +0000521 DenseMap<unsigned,std::vector<SRInfo> > &SpillIdxes,
Evan Cheng0cbb1162007-11-29 01:06:25 +0000522 BitVector &RestoreMBBs,
Owen Anderson28998312008-08-13 22:28:50 +0000523 DenseMap<unsigned,std::vector<SRInfo> > &RestoreIdxes,
524 DenseMap<unsigned,unsigned> &MBBVRegsMap,
Evan Chengc781a242009-05-03 18:32:42 +0000525 std::vector<LiveInterval*> &NewLIs);
Evan Chengf2fbca62007-11-12 06:35:08 +0000526
Owen Anderson03857b22008-08-13 21:49:13 +0000527 static LiveInterval* createInterval(unsigned Reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000528
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000529 void printRegName(unsigned reg) const;
530 };
531
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000532} // End llvm namespace
533
534#endif