blob: da9ff30edfbd46d01aad04e1dede3415b8c39e31 [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
David Greenede0cc5a2009-08-19 20:52:54 +000023#include "llvm/CodeGen/MachineBasicBlock.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000024#include "llvm/CodeGen/MachineFunctionPass.h"
Chris Lattner779a6512005-09-21 04:18:25 +000025#include "llvm/CodeGen/LiveInterval.h"
Evan Cheng61de82d2007-02-15 05:59:24 +000026#include "llvm/ADT/BitVector.h"
Evan Cheng20b0abc2007-04-17 20:32:26 +000027#include "llvm/ADT/DenseMap.h"
Evan Cheng8f90b6e2009-01-07 02:08:57 +000028#include "llvm/ADT/SmallPtrSet.h"
Evan Cheng549f27d32007-08-13 23:45:17 +000029#include "llvm/ADT/SmallVector.h"
Evan Chengf3bb2e62007-09-05 21:46:51 +000030#include "llvm/Support/Allocator.h"
Hartmut Kaiserffb15de2007-11-13 23:04:28 +000031#include <cmath>
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000032
33namespace llvm {
34
Dan Gohman6d69ba82008-07-25 00:02:30 +000035 class AliasAnalysis;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000036 class LiveVariables;
Evan Cheng22f07ff2007-12-11 02:09:15 +000037 class MachineLoopInfo;
Dan Gohman6f0d0242008-02-10 18:45:23 +000038 class TargetRegisterInfo;
Chris Lattner84bc5422007-12-31 04:13:23 +000039 class MachineRegisterInfo;
Chris Lattnerf768bba2005-03-09 23:05:19 +000040 class TargetInstrInfo;
Evan Cheng20b0abc2007-04-17 20:32:26 +000041 class TargetRegisterClass;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000042 class VirtRegMap;
Evan Cheng4ca980e2007-10-17 02:10:22 +000043 typedef std::pair<unsigned, MachineBasicBlock*> IdxMBBPair;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000044
Roman Levenstein8dd25282008-02-18 09:35:30 +000045 inline bool operator<(unsigned V, const IdxMBBPair &IM) {
46 return V < IM.first;
47 }
48
49 inline bool operator<(const IdxMBBPair &IM, unsigned V) {
50 return IM.first < V;
51 }
52
53 struct Idx2MBBCompare {
54 bool operator()(const IdxMBBPair &LHS, const IdxMBBPair &RHS) const {
55 return LHS.first < RHS.first;
56 }
57 };
Owen Anderson20e28392008-08-13 22:08:30 +000058
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000059 class LiveIntervals : public MachineFunctionPass {
60 MachineFunction* mf_;
Evan Chengd70dbb52008-02-22 09:24:50 +000061 MachineRegisterInfo* mri_;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000062 const TargetMachine* tm_;
Dan Gohman6f0d0242008-02-10 18:45:23 +000063 const TargetRegisterInfo* tri_;
Chris Lattnerf768bba2005-03-09 23:05:19 +000064 const TargetInstrInfo* tii_;
Dan Gohman6d69ba82008-07-25 00:02:30 +000065 AliasAnalysis *aa_;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000066 LiveVariables* lv_;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000067
Evan Chengf3bb2e62007-09-05 21:46:51 +000068 /// Special pool allocator for VNInfo's (LiveInterval val#).
69 ///
70 BumpPtrAllocator VNInfoAllocator;
71
Evan Cheng549f27d32007-08-13 23:45:17 +000072 /// MBB2IdxMap - The indexes of the first and last instructions in the
73 /// specified basic block.
74 std::vector<std::pair<unsigned, unsigned> > MBB2IdxMap;
David Greene25133302007-06-08 17:18:56 +000075
Evan Cheng4ca980e2007-10-17 02:10:22 +000076 /// Idx2MBBMap - Sorted list of pairs of index of first instruction
77 /// and MBB id.
78 std::vector<IdxMBBPair> Idx2MBBMap;
79
Owen Andersona1566f22008-07-22 22:46:49 +000080 /// FunctionSize - The number of instructions present in the function
81 uint64_t FunctionSize;
82
David Greene340482d2009-07-22 21:56:14 +000083 typedef DenseMap<const MachineInstr*, unsigned> Mi2IndexMap;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000084 Mi2IndexMap mi2iMap_;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000085
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000086 typedef std::vector<MachineInstr*> Index2MiMap;
87 Index2MiMap i2miMap_;
Alkis Evlogimenos843b1602004-02-15 10:24:21 +000088
Owen Anderson20e28392008-08-13 22:08:30 +000089 typedef DenseMap<unsigned, LiveInterval*> Reg2IntervalMap;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000090 Reg2IntervalMap r2iMap_;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000091
Lang Hamesffd13262009-07-09 03:57:02 +000092 DenseMap<MachineBasicBlock*, unsigned> terminatorGaps;
93
Evan Cheng61de82d2007-02-15 05:59:24 +000094 BitVector allocatableRegs_;
Evan Cheng88d1f582007-03-01 02:03:03 +000095
Evan Cheng549f27d32007-08-13 23:45:17 +000096 std::vector<MachineInstr*> ClonedMIs;
97
Lang Hamesf41538d2009-06-02 16:53:25 +000098 typedef LiveInterval::InstrSlots InstrSlots;
99
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000100 public:
Nick Lewyckyecd94c82007-05-06 13:37:16 +0000101 static char ID; // Pass identification, replacement for typeid
Dan Gohmanae73dc12008-09-04 17:05:41 +0000102 LiveIntervals() : MachineFunctionPass(&ID) {}
Devang Patel794fd752007-05-01 21:15:47 +0000103
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000104 static unsigned getBaseIndex(unsigned index) {
105 return index - (index % InstrSlots::NUM);
106 }
107 static unsigned getBoundaryIndex(unsigned index) {
108 return getBaseIndex(index + InstrSlots::NUM - 1);
109 }
110 static unsigned getLoadIndex(unsigned index) {
111 return getBaseIndex(index) + InstrSlots::LOAD;
112 }
113 static unsigned getUseIndex(unsigned index) {
114 return getBaseIndex(index) + InstrSlots::USE;
115 }
116 static unsigned getDefIndex(unsigned index) {
117 return getBaseIndex(index) + InstrSlots::DEF;
118 }
119 static unsigned getStoreIndex(unsigned index) {
120 return getBaseIndex(index) + InstrSlots::STORE;
121 }
122
Evan Chengc3417602008-06-21 06:45:54 +0000123 static float getSpillWeight(bool isDef, bool isUse, unsigned loopDepth) {
124 return (isDef + isUse) * powf(10.0F, (float)loopDepth);
Evan Chengf2fbca62007-11-12 06:35:08 +0000125 }
126
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000127 typedef Reg2IntervalMap::iterator iterator;
Chris Lattner70ca3582004-09-30 15:59:17 +0000128 typedef Reg2IntervalMap::const_iterator const_iterator;
129 const_iterator begin() const { return r2iMap_.begin(); }
130 const_iterator end() const { return r2iMap_.end(); }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000131 iterator begin() { return r2iMap_.begin(); }
132 iterator end() { return r2iMap_.end(); }
Evan Cheng34cd4a42008-05-05 18:30:58 +0000133 unsigned getNumIntervals() const { return (unsigned)r2iMap_.size(); }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000134
135 LiveInterval &getInterval(unsigned reg) {
136 Reg2IntervalMap::iterator I = r2iMap_.find(reg);
137 assert(I != r2iMap_.end() && "Interval does not exist for register");
Owen Anderson03857b22008-08-13 21:49:13 +0000138 return *I->second;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000139 }
140
141 const LiveInterval &getInterval(unsigned reg) const {
142 Reg2IntervalMap::const_iterator I = r2iMap_.find(reg);
143 assert(I != r2iMap_.end() && "Interval does not exist for register");
Owen Anderson03857b22008-08-13 21:49:13 +0000144 return *I->second;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000145 }
146
Evan Chengb371f452007-02-19 21:49:54 +0000147 bool hasInterval(unsigned reg) const {
Evan Cheng88d1f582007-03-01 02:03:03 +0000148 return r2iMap_.count(reg);
Evan Chengb371f452007-02-19 21:49:54 +0000149 }
150
Chris Lattner428b92e2006-09-15 03:57:23 +0000151 /// getMBBStartIdx - Return the base index of the first instruction in the
152 /// specified MachineBasicBlock.
153 unsigned getMBBStartIdx(MachineBasicBlock *MBB) const {
154 return getMBBStartIdx(MBB->getNumber());
155 }
Chris Lattner428b92e2006-09-15 03:57:23 +0000156 unsigned getMBBStartIdx(unsigned MBBNo) const {
157 assert(MBBNo < MBB2IdxMap.size() && "Invalid MBB number!");
Evan Cheng549f27d32007-08-13 23:45:17 +0000158 return MBB2IdxMap[MBBNo].first;
159 }
160
161 /// getMBBEndIdx - Return the store index of the last instruction in the
162 /// specified MachineBasicBlock.
163 unsigned getMBBEndIdx(MachineBasicBlock *MBB) const {
164 return getMBBEndIdx(MBB->getNumber());
165 }
166 unsigned getMBBEndIdx(unsigned MBBNo) const {
167 assert(MBBNo < MBB2IdxMap.size() && "Invalid MBB number!");
168 return MBB2IdxMap[MBBNo].second;
Chris Lattner428b92e2006-09-15 03:57:23 +0000169 }
170
Owen Andersona1566f22008-07-22 22:46:49 +0000171 /// getScaledIntervalSize - get the size of an interval in "units,"
Owen Anderson72e04092008-06-23 23:25:37 +0000172 /// where every function is composed of one thousand units. This
173 /// measure scales properly with empty index slots in the function.
Owen Andersona1566f22008-07-22 22:46:49 +0000174 double getScaledIntervalSize(LiveInterval& I) {
175 return (1000.0 / InstrSlots::NUM * I.getSize()) / i2miMap_.size();
176 }
177
178 /// getApproximateInstructionCount - computes an estimate of the number
179 /// of instructions in a given LiveInterval.
180 unsigned getApproximateInstructionCount(LiveInterval& I) {
181 double IntervalPercentage = getScaledIntervalSize(I) / 1000.0;
Matthijs Kooijmanb3e15c02008-08-07 13:36:30 +0000182 return (unsigned)(IntervalPercentage * FunctionSize);
Owen Anderson72e04092008-06-23 23:25:37 +0000183 }
184
Roman Levenstein8dd25282008-02-18 09:35:30 +0000185 /// getMBBFromIndex - given an index in any instruction of an
186 /// MBB return a pointer the MBB
187 MachineBasicBlock* getMBBFromIndex(unsigned index) const {
188 std::vector<IdxMBBPair>::const_iterator I =
Bill Wendlinge85fe662008-02-26 10:49:39 +0000189 std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), index);
Roman Levenstein8dd25282008-02-18 09:35:30 +0000190 // Take the pair containing the index
191 std::vector<IdxMBBPair>::const_iterator J =
Bill Wendlinge85fe662008-02-26 10:49:39 +0000192 ((I != Idx2MBBMap.end() && I->first > index) ||
193 (I == Idx2MBBMap.end() && Idx2MBBMap.size()>0)) ? (I-1): I;
Roman Levenstein8dd25282008-02-18 09:35:30 +0000194
195 assert(J != Idx2MBBMap.end() && J->first < index+1 &&
Bill Wendlinge85fe662008-02-26 10:49:39 +0000196 index <= getMBBEndIdx(J->second) &&
197 "index does not correspond to an MBB");
Roman Levenstein8dd25282008-02-18 09:35:30 +0000198 return J->second;
199 }
200
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000201 /// getInstructionIndex - returns the base index of instr
David Greene340482d2009-07-22 21:56:14 +0000202 unsigned getInstructionIndex(const MachineInstr* instr) const {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000203 Mi2IndexMap::const_iterator it = mi2iMap_.find(instr);
204 assert(it != mi2iMap_.end() && "Invalid instruction!");
205 return it->second;
206 }
207
208 /// getInstructionFromIndex - given an index in any slot of an
209 /// instruction return a pointer the instruction
210 MachineInstr* getInstructionFromIndex(unsigned index) const {
211 index /= InstrSlots::NUM; // convert index to vector index
212 assert(index < i2miMap_.size() &&
213 "index does not correspond to an instruction");
214 return i2miMap_[index];
215 }
David Greene25133302007-06-08 17:18:56 +0000216
Evan Chengf5cd4f02008-10-23 20:43:13 +0000217 /// hasGapBeforeInstr - Return true if the previous instruction slot,
218 /// i.e. Index - InstrSlots::NUM, is not occupied.
219 bool hasGapBeforeInstr(unsigned Index) {
220 Index = getBaseIndex(Index - InstrSlots::NUM);
221 return getInstructionFromIndex(Index) == 0;
222 }
223
Lang Hamesf41538d2009-06-02 16:53:25 +0000224 /// hasGapAfterInstr - Return true if the successive instruction slot,
225 /// i.e. Index + InstrSlots::Num, is not occupied.
226 bool hasGapAfterInstr(unsigned Index) {
227 Index = getBaseIndex(Index + InstrSlots::NUM);
228 return getInstructionFromIndex(Index) == 0;
229 }
230
Evan Chengf5cd4f02008-10-23 20:43:13 +0000231 /// findGapBeforeInstr - Find an empty instruction slot before the
232 /// specified index. If "Furthest" is true, find one that's furthest
233 /// away from the index (but before any index that's occupied).
234 unsigned findGapBeforeInstr(unsigned Index, bool Furthest = false) {
235 Index = getBaseIndex(Index - InstrSlots::NUM);
236 if (getInstructionFromIndex(Index))
237 return 0; // No gap!
238 if (!Furthest)
239 return Index;
240 unsigned PrevIndex = getBaseIndex(Index - InstrSlots::NUM);
241 while (getInstructionFromIndex(Index)) {
242 Index = PrevIndex;
243 PrevIndex = getBaseIndex(Index - InstrSlots::NUM);
244 }
245 return Index;
246 }
247
248 /// InsertMachineInstrInMaps - Insert the specified machine instruction
249 /// into the instruction index map at the given index.
250 void InsertMachineInstrInMaps(MachineInstr *MI, unsigned Index) {
251 i2miMap_[Index / InstrSlots::NUM] = MI;
252 Mi2IndexMap::iterator it = mi2iMap_.find(MI);
253 assert(it == mi2iMap_.end() && "Already in map!");
254 mi2iMap_[MI] = Index;
255 }
256
Evan Chengc92da382007-11-03 07:20:12 +0000257 /// conflictsWithPhysRegDef - Returns true if the specified register
258 /// is defined during the duration of the specified interval.
259 bool conflictsWithPhysRegDef(const LiveInterval &li, VirtRegMap &vrm,
260 unsigned reg);
261
Evan Cheng8f90b6e2009-01-07 02:08:57 +0000262 /// conflictsWithPhysRegRef - Similar to conflictsWithPhysRegRef except
263 /// it can check use as well.
264 bool conflictsWithPhysRegRef(LiveInterval &li, unsigned Reg,
265 bool CheckUse,
266 SmallPtrSet<MachineInstr*,32> &JoinedCopies);
267
Evan Cheng4ca980e2007-10-17 02:10:22 +0000268 /// findLiveInMBBs - Given a live range, if the value of the range
269 /// is live in any MBB returns true as well as the list of basic blocks
Dan Gohmanca425a22008-07-28 18:42:57 +0000270 /// in which the value is live.
Evan Chengd0e32c52008-10-29 05:06:14 +0000271 bool findLiveInMBBs(unsigned Start, unsigned End,
272 SmallVectorImpl<MachineBasicBlock*> &MBBs) const;
273
274 /// findReachableMBBs - Return a list MBB that can be reached via any
275 /// branch or fallthroughs. Return true if the list is not empty.
276 bool findReachableMBBs(unsigned Start, unsigned End,
Evan Chenga5bfc972007-10-17 06:53:44 +0000277 SmallVectorImpl<MachineBasicBlock*> &MBBs) const;
Evan Cheng4ca980e2007-10-17 02:10:22 +0000278
David Greene25133302007-06-08 17:18:56 +0000279 // Interval creation
280
281 LiveInterval &getOrCreateInterval(unsigned reg) {
282 Reg2IntervalMap::iterator I = r2iMap_.find(reg);
283 if (I == r2iMap_.end())
Owen Anderson20e28392008-08-13 22:08:30 +0000284 I = r2iMap_.insert(std::make_pair(reg, createInterval(reg))).first;
Owen Anderson03857b22008-08-13 21:49:13 +0000285 return *I->second;
David Greene25133302007-06-08 17:18:56 +0000286 }
Evan Cheng0a1fcce2009-02-08 11:04:35 +0000287
288 /// dupInterval - Duplicate a live interval. The caller is responsible for
289 /// managing the allocated memory.
290 LiveInterval *dupInterval(LiveInterval *li);
Owen Andersonc4dc1322008-06-05 17:15:43 +0000291
292 /// addLiveRangeToEndOfBlock - Given a register and an instruction,
293 /// adds a live range from that instruction to the end of its MBB.
294 LiveRange addLiveRangeToEndOfBlock(unsigned reg,
295 MachineInstr* startInst);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000296
David Greene25133302007-06-08 17:18:56 +0000297 // Interval removal
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000298
David Greene25133302007-06-08 17:18:56 +0000299 void removeInterval(unsigned Reg) {
Owen Anderson20e28392008-08-13 22:08:30 +0000300 DenseMap<unsigned, LiveInterval*>::iterator I = r2iMap_.find(Reg);
Owen Anderson03857b22008-08-13 21:49:13 +0000301 delete I->second;
302 r2iMap_.erase(I);
Bill Wendling5c7e3262006-12-17 05:15:13 +0000303 }
Chris Lattner70ca3582004-09-30 15:59:17 +0000304
Evan Cheng5b69eba2009-04-21 22:46:52 +0000305 /// isNotInMIMap - returns true if the specified machine instr has been
306 /// removed or was never entered in the map.
307 bool isNotInMIMap(MachineInstr* instr) const {
Evan Cheng7d35c0e2007-02-22 23:52:23 +0000308 return !mi2iMap_.count(instr);
Evan Cheng30cac022007-02-22 23:03:39 +0000309 }
310
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000311 /// RemoveMachineInstrFromMaps - This marks the specified machine instr as
312 /// deleted.
313 void RemoveMachineInstrFromMaps(MachineInstr *MI) {
314 // remove index -> MachineInstr and
315 // MachineInstr -> index mappings
316 Mi2IndexMap::iterator mi2i = mi2iMap_.find(MI);
317 if (mi2i != mi2iMap_.end()) {
318 i2miMap_[mi2i->second/InstrSlots::NUM] = 0;
319 mi2iMap_.erase(mi2i);
320 }
321 }
David Greene25133302007-06-08 17:18:56 +0000322
Evan Cheng70071432008-02-13 03:01:43 +0000323 /// ReplaceMachineInstrInMaps - Replacing a machine instr with a new one in
324 /// maps used by register allocator.
325 void ReplaceMachineInstrInMaps(MachineInstr *MI, MachineInstr *NewMI) {
326 Mi2IndexMap::iterator mi2i = mi2iMap_.find(MI);
Evan Chengb1f6f912008-02-13 09:18:16 +0000327 if (mi2i == mi2iMap_.end())
328 return;
329 i2miMap_[mi2i->second/InstrSlots::NUM] = NewMI;
330 Mi2IndexMap::iterator it = mi2iMap_.find(MI);
331 assert(it != mi2iMap_.end() && "Invalid instruction!");
332 unsigned Index = it->second;
333 mi2iMap_.erase(it);
334 mi2iMap_[NewMI] = Index;
Evan Cheng70071432008-02-13 03:01:43 +0000335 }
336
Evan Chengf3bb2e62007-09-05 21:46:51 +0000337 BumpPtrAllocator& getVNInfoAllocator() { return VNInfoAllocator; }
338
Evan Chengc8d044e2008-02-15 18:24:29 +0000339 /// getVNInfoSourceReg - Helper function that parses the specified VNInfo
340 /// copy field and returns the source register that defines it.
341 unsigned getVNInfoSourceReg(const VNInfo *VNI) const;
342
David Greene25133302007-06-08 17:18:56 +0000343 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
344 virtual void releaseMemory();
345
346 /// runOnMachineFunction - pass entry point
347 virtual bool runOnMachineFunction(MachineFunction&);
348
349 /// print - Implement the dump method.
Chris Lattner45cfe542009-08-23 06:03:38 +0000350 virtual void print(raw_ostream &O, const Module* = 0) const;
David Greene25133302007-06-08 17:18:56 +0000351
Evan Chengf2fbca62007-11-12 06:35:08 +0000352 /// addIntervalsForSpills - Create new intervals for spilled defs / uses of
Evan Cheng9c3c2212008-06-06 07:54:39 +0000353 /// the given interval. FIXME: It also returns the weight of the spill slot
354 /// (if any is created) by reference. This is temporary.
Evan Chengf2fbca62007-11-12 06:35:08 +0000355 std::vector<LiveInterval*>
Evan Cheng81a03822007-11-17 00:40:40 +0000356 addIntervalsForSpills(const LiveInterval& i,
Evan Chengdc377862008-09-30 15:44:16 +0000357 SmallVectorImpl<LiveInterval*> &SpillIs,
Evan Chengc781a242009-05-03 18:32:42 +0000358 const MachineLoopInfo *loopInfo, VirtRegMap& vrm);
Owen Andersond6664312008-08-18 18:05:32 +0000359
360 /// addIntervalsForSpillsFast - Quickly create new intervals for spilled
361 /// defs / uses without remat or splitting.
362 std::vector<LiveInterval*>
363 addIntervalsForSpillsFast(const LiveInterval &li,
Evan Chengc781a242009-05-03 18:32:42 +0000364 const MachineLoopInfo *loopInfo, VirtRegMap &vrm);
Evan Chengf2fbca62007-11-12 06:35:08 +0000365
Evan Cheng676dd7c2008-03-11 07:19:34 +0000366 /// spillPhysRegAroundRegDefsUses - Spill the specified physical register
Evan Cheng2824a652009-03-23 18:24:37 +0000367 /// around all defs and uses of the specified interval. Return true if it
368 /// was able to cut its interval.
369 bool spillPhysRegAroundRegDefsUses(const LiveInterval &li,
Evan Cheng676dd7c2008-03-11 07:19:34 +0000370 unsigned PhysReg, VirtRegMap &vrm);
371
Evan Cheng5ef3a042007-12-06 00:01:56 +0000372 /// isReMaterializable - Returns true if every definition of MI of every
373 /// val# of the specified interval is re-materializable. Also returns true
374 /// by reference if all of the defs are load instructions.
Evan Chengdc377862008-09-30 15:44:16 +0000375 bool isReMaterializable(const LiveInterval &li,
376 SmallVectorImpl<LiveInterval*> &SpillIs,
377 bool &isLoad);
Evan Cheng5ef3a042007-12-06 00:01:56 +0000378
Evan Cheng06587492008-10-24 02:05:00 +0000379 /// isReMaterializable - Returns true if the definition MI of the specified
380 /// val# of the specified interval is re-materializable.
381 bool isReMaterializable(const LiveInterval &li, const VNInfo *ValNo,
382 MachineInstr *MI);
383
Evan Cheng676dd7c2008-03-11 07:19:34 +0000384 /// getRepresentativeReg - Find the largest super register of the specified
385 /// physical register.
386 unsigned getRepresentativeReg(unsigned Reg) const;
387
388 /// getNumConflictsWithPhysReg - Return the number of uses and defs of the
389 /// specified interval that conflicts with the specified physical register.
390 unsigned getNumConflictsWithPhysReg(const LiveInterval &li,
391 unsigned PhysReg) const;
392
Evan Cheng2578ba22009-07-01 01:59:31 +0000393 /// processImplicitDefs - Process IMPLICIT_DEF instructions. Add isUndef
394 /// marker to implicit_def defs and their uses.
395 void processImplicitDefs();
396
Owen Anderson15a17f52008-05-30 20:14:04 +0000397 /// computeNumbering - Compute the index numbering.
398 void computeNumbering();
399
Lang Hamesf41538d2009-06-02 16:53:25 +0000400 /// scaleNumbering - Rescale interval numbers to introduce gaps for new
401 /// instructions
402 void scaleNumbering(int factor);
403
Owen Anderson0c2e7b92009-01-13 06:05:10 +0000404 /// intervalIsInOneMBB - Returns true if the specified interval is entirely
405 /// within a single basic block.
406 bool intervalIsInOneMBB(const LiveInterval &li) const;
407
David Greene25133302007-06-08 17:18:56 +0000408 private:
Chris Lattner428b92e2006-09-15 03:57:23 +0000409 /// computeIntervals - Compute live intervals.
Chris Lattnerc7695eb2006-09-14 06:42:17 +0000410 void computeIntervals();
Chris Lattner6bda49f2006-09-02 05:26:01 +0000411
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000412 /// handleRegisterDef - update intervals for a register def
413 /// (calls handlePhysicalRegisterDef and
414 /// handleVirtualRegisterDef)
Chris Lattner6b128bd2006-09-03 08:07:11 +0000415 void handleRegisterDef(MachineBasicBlock *MBB,
416 MachineBasicBlock::iterator MI, unsigned MIIdx,
Evan Chengef0732d2008-07-10 07:35:43 +0000417 MachineOperand& MO, unsigned MOIdx);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000418
419 /// handleVirtualRegisterDef - update intervals for a virtual
420 /// register def
Chris Lattner6b128bd2006-09-03 08:07:11 +0000421 void handleVirtualRegisterDef(MachineBasicBlock *MBB,
422 MachineBasicBlock::iterator MI,
Owen Anderson6b098de2008-06-25 23:39:39 +0000423 unsigned MIIdx, MachineOperand& MO,
Evan Chengef0732d2008-07-10 07:35:43 +0000424 unsigned MOIdx, LiveInterval& interval);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000425
Chris Lattnerf768bba2005-03-09 23:05:19 +0000426 /// handlePhysicalRegisterDef - update intervals for a physical register
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000427 /// def.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000428 void handlePhysicalRegisterDef(MachineBasicBlock* mbb,
429 MachineBasicBlock::iterator mi,
Owen Anderson6b098de2008-06-25 23:39:39 +0000430 unsigned MIIdx, MachineOperand& MO,
Chris Lattner91725b72006-08-31 05:54:43 +0000431 LiveInterval &interval,
Evan Chengc8d044e2008-02-15 18:24:29 +0000432 MachineInstr *CopyMI);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000433
Evan Chengb371f452007-02-19 21:49:54 +0000434 /// handleLiveInRegister - Create interval for a livein register.
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000435 void handleLiveInRegister(MachineBasicBlock* mbb,
436 unsigned MIIdx,
Evan Cheng24a3cc42007-04-25 07:30:23 +0000437 LiveInterval &interval, bool isAlias = false);
Evan Chengb371f452007-02-19 21:49:54 +0000438
Evan Chengd70dbb52008-02-22 09:24:50 +0000439 /// getReMatImplicitUse - If the remat definition MI has one (for now, we
440 /// only allow one) virtual register operand, then its uses are implicitly
441 /// using the register. Returns the virtual register.
442 unsigned getReMatImplicitUse(const LiveInterval &li,
443 MachineInstr *MI) const;
444
445 /// isValNoAvailableAt - Return true if the val# of the specified interval
446 /// which reaches the given instruction also reaches the specified use
447 /// index.
448 bool isValNoAvailableAt(const LiveInterval &li, MachineInstr *MI,
449 unsigned UseIdx) const;
450
Evan Cheng549f27d32007-08-13 23:45:17 +0000451 /// isReMaterializable - Returns true if the definition MI of the specified
Evan Cheng5ef3a042007-12-06 00:01:56 +0000452 /// val# of the specified interval is re-materializable. Also returns true
453 /// by reference if the def is a load.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000454 bool isReMaterializable(const LiveInterval &li, const VNInfo *ValNo,
Evan Chengdc377862008-09-30 15:44:16 +0000455 MachineInstr *MI,
456 SmallVectorImpl<LiveInterval*> &SpillIs,
457 bool &isLoad);
Evan Cheng549f27d32007-08-13 23:45:17 +0000458
Evan Cheng35b35c52007-08-30 05:52:20 +0000459 /// tryFoldMemoryOperand - Attempts to fold either a spill / restore from
460 /// slot / to reg or any rematerialized load into ith operand of specified
461 /// MI. If it is successul, MI is updated with the newly created MI and
462 /// returns true.
463 bool tryFoldMemoryOperand(MachineInstr* &MI, VirtRegMap &vrm,
Evan Chengcddbb832007-11-30 21:23:43 +0000464 MachineInstr *DefMI, unsigned InstrIdx,
Evan Chengaee4af62007-12-02 08:30:39 +0000465 SmallVector<unsigned, 2> &Ops,
Evan Chengcddbb832007-11-30 21:23:43 +0000466 bool isSS, int Slot, unsigned Reg);
Evan Cheng549f27d32007-08-13 23:45:17 +0000467
Evan Chengd70dbb52008-02-22 09:24:50 +0000468 /// canFoldMemoryOperand - Return true if the specified load / store
Evan Cheng018f9b02007-12-05 03:22:34 +0000469 /// folding is possible.
Evan Chengd64b5c82007-12-05 03:14:33 +0000470 bool canFoldMemoryOperand(MachineInstr *MI,
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000471 SmallVector<unsigned, 2> &Ops,
472 bool ReMatLoadSS) const;
Evan Chengd64b5c82007-12-05 03:14:33 +0000473
Evan Cheng0cbb1162007-11-29 01:06:25 +0000474 /// anyKillInMBBAfterIdx - Returns true if there is a kill of the specified
475 /// VNInfo that's after the specified index but is within the basic block.
476 bool anyKillInMBBAfterIdx(const LiveInterval &li, const VNInfo *VNI,
477 MachineBasicBlock *MBB, unsigned Idx) const;
Evan Cheng81a03822007-11-17 00:40:40 +0000478
Evan Cheng676dd7c2008-03-11 07:19:34 +0000479 /// hasAllocatableSuperReg - Return true if the specified physical register
480 /// has any super register that's allocatable.
481 bool hasAllocatableSuperReg(unsigned Reg) const;
482
Evan Cheng1953d0c2007-11-29 10:12:14 +0000483 /// SRInfo - Spill / restore info.
484 struct SRInfo {
485 int index;
486 unsigned vreg;
487 bool canFold;
488 SRInfo(int i, unsigned vr, bool f) : index(i), vreg(vr), canFold(f) {};
489 };
490
491 bool alsoFoldARestore(int Id, int index, unsigned vr,
492 BitVector &RestoreMBBs,
Owen Anderson28998312008-08-13 22:28:50 +0000493 DenseMap<unsigned,std::vector<SRInfo> >&RestoreIdxes);
Evan Cheng1953d0c2007-11-29 10:12:14 +0000494 void eraseRestoreInfo(int Id, int index, unsigned vr,
495 BitVector &RestoreMBBs,
Owen Anderson28998312008-08-13 22:28:50 +0000496 DenseMap<unsigned,std::vector<SRInfo> >&RestoreIdxes);
Evan Cheng1953d0c2007-11-29 10:12:14 +0000497
Evan Cheng4cce6b42008-04-11 17:53:36 +0000498 /// handleSpilledImpDefs - Remove IMPLICIT_DEF instructions which are being
499 /// spilled and create empty intervals for their uses.
500 void handleSpilledImpDefs(const LiveInterval &li, VirtRegMap &vrm,
501 const TargetRegisterClass* rc,
502 std::vector<LiveInterval*> &NewLIs);
Evan Cheng419852c2008-04-03 16:39:43 +0000503
Evan Chengd70dbb52008-02-22 09:24:50 +0000504 /// rewriteImplicitOps - Rewrite implicit use operands of MI (i.e. uses of
505 /// interval on to-be re-materialized operands of MI) with new register.
506 void rewriteImplicitOps(const LiveInterval &li,
507 MachineInstr *MI, unsigned NewVReg, VirtRegMap &vrm);
508
Chris Lattner84bc5422007-12-31 04:13:23 +0000509 /// rewriteInstructionForSpills, rewriteInstructionsForSpills - Helper
510 /// functions for addIntervalsForSpills to rewrite uses / defs for the given
511 /// live range.
Evan Chengd70dbb52008-02-22 09:24:50 +0000512 bool rewriteInstructionForSpills(const LiveInterval &li, const VNInfo *VNI,
513 bool TrySplit, unsigned index, unsigned end, MachineInstr *MI,
Evan Chengf2fbca62007-11-12 06:35:08 +0000514 MachineInstr *OrigDefMI, MachineInstr *DefMI, unsigned Slot, int LdSlot,
515 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
Evan Chengd70dbb52008-02-22 09:24:50 +0000516 VirtRegMap &vrm, const TargetRegisterClass* rc,
517 SmallVector<int, 4> &ReMatIds, const MachineLoopInfo *loopInfo,
Evan Cheng0cc83b62008-02-23 00:46:11 +0000518 unsigned &NewVReg, unsigned ImpUse, bool &HasDef, bool &HasUse,
Owen Anderson28998312008-08-13 22:28:50 +0000519 DenseMap<unsigned,unsigned> &MBBVRegsMap,
Evan Chengc781a242009-05-03 18:32:42 +0000520 std::vector<LiveInterval*> &NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +0000521 void rewriteInstructionsForSpills(const LiveInterval &li, bool TrySplit,
Evan Chengf2fbca62007-11-12 06:35:08 +0000522 LiveInterval::Ranges::const_iterator &I,
523 MachineInstr *OrigDefMI, MachineInstr *DefMI, unsigned Slot, int LdSlot,
524 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
Evan Chengd70dbb52008-02-22 09:24:50 +0000525 VirtRegMap &vrm, const TargetRegisterClass* rc,
Evan Cheng22f07ff2007-12-11 02:09:15 +0000526 SmallVector<int, 4> &ReMatIds, const MachineLoopInfo *loopInfo,
Evan Cheng81a03822007-11-17 00:40:40 +0000527 BitVector &SpillMBBs,
Owen Anderson28998312008-08-13 22:28:50 +0000528 DenseMap<unsigned,std::vector<SRInfo> > &SpillIdxes,
Evan Cheng0cbb1162007-11-29 01:06:25 +0000529 BitVector &RestoreMBBs,
Owen Anderson28998312008-08-13 22:28:50 +0000530 DenseMap<unsigned,std::vector<SRInfo> > &RestoreIdxes,
531 DenseMap<unsigned,unsigned> &MBBVRegsMap,
Evan Chengc781a242009-05-03 18:32:42 +0000532 std::vector<LiveInterval*> &NewLIs);
Evan Chengf2fbca62007-11-12 06:35:08 +0000533
Owen Anderson03857b22008-08-13 21:49:13 +0000534 static LiveInterval* createInterval(unsigned Reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000535
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000536 void printRegName(unsigned reg) const;
537 };
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000538} // End llvm namespace
539
540#endif