blob: 04d0a030578f80a1685af5c182139cc013ecd67a [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 Cheng549f27d32007-08-13 23:45:17 +000027#include "llvm/ADT/SmallPtrSet.h"
28#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
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000034 class LiveVariables;
Evan Cheng22f07ff2007-12-11 02:09:15 +000035 class MachineLoopInfo;
Dan Gohman6f0d0242008-02-10 18:45:23 +000036 class TargetRegisterInfo;
Chris Lattner84bc5422007-12-31 04:13:23 +000037 class MachineRegisterInfo;
Chris Lattnerf768bba2005-03-09 23:05:19 +000038 class TargetInstrInfo;
Evan Cheng20b0abc2007-04-17 20:32:26 +000039 class TargetRegisterClass;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000040 class VirtRegMap;
Evan Cheng4ca980e2007-10-17 02:10:22 +000041 typedef std::pair<unsigned, MachineBasicBlock*> IdxMBBPair;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000042
Roman Levenstein8dd25282008-02-18 09:35:30 +000043 inline bool operator<(unsigned V, const IdxMBBPair &IM) {
44 return V < IM.first;
45 }
46
47 inline bool operator<(const IdxMBBPair &IM, unsigned V) {
48 return IM.first < V;
49 }
50
51 struct Idx2MBBCompare {
52 bool operator()(const IdxMBBPair &LHS, const IdxMBBPair &RHS) const {
53 return LHS.first < RHS.first;
54 }
55 };
56
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000057 class LiveIntervals : public MachineFunctionPass {
58 MachineFunction* mf_;
Evan Chengd70dbb52008-02-22 09:24:50 +000059 MachineRegisterInfo* mri_;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000060 const TargetMachine* tm_;
Dan Gohman6f0d0242008-02-10 18:45:23 +000061 const TargetRegisterInfo* tri_;
Chris Lattnerf768bba2005-03-09 23:05:19 +000062 const TargetInstrInfo* tii_;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000063 LiveVariables* lv_;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000064
Evan Chengf3bb2e62007-09-05 21:46:51 +000065 /// Special pool allocator for VNInfo's (LiveInterval val#).
66 ///
67 BumpPtrAllocator VNInfoAllocator;
68
Evan Cheng549f27d32007-08-13 23:45:17 +000069 /// MBB2IdxMap - The indexes of the first and last instructions in the
70 /// specified basic block.
71 std::vector<std::pair<unsigned, unsigned> > MBB2IdxMap;
David Greene25133302007-06-08 17:18:56 +000072
Evan Cheng4ca980e2007-10-17 02:10:22 +000073 /// Idx2MBBMap - Sorted list of pairs of index of first instruction
74 /// and MBB id.
75 std::vector<IdxMBBPair> Idx2MBBMap;
76
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000077 typedef std::map<MachineInstr*, unsigned> Mi2IndexMap;
78 Mi2IndexMap mi2iMap_;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000079
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000080 typedef std::vector<MachineInstr*> Index2MiMap;
81 Index2MiMap i2miMap_;
Alkis Evlogimenos843b1602004-02-15 10:24:21 +000082
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000083 typedef std::map<unsigned, LiveInterval> Reg2IntervalMap;
84 Reg2IntervalMap r2iMap_;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000085
Evan Cheng61de82d2007-02-15 05:59:24 +000086 BitVector allocatableRegs_;
Evan Cheng88d1f582007-03-01 02:03:03 +000087
Evan Cheng549f27d32007-08-13 23:45:17 +000088 std::vector<MachineInstr*> ClonedMIs;
89
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000090 public:
Nick Lewyckyecd94c82007-05-06 13:37:16 +000091 static char ID; // Pass identification, replacement for typeid
Devang Patel794fd752007-05-01 21:15:47 +000092 LiveIntervals() : MachineFunctionPass((intptr_t)&ID) {}
93
Chris Lattnerf7da2c72006-08-24 22:43:55 +000094 struct InstrSlots {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000095 enum {
96 LOAD = 0,
97 USE = 1,
98 DEF = 2,
99 STORE = 3,
Chris Lattner410354f2006-02-22 16:23:43 +0000100 NUM = 4
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000101 };
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000102 };
103
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 Cheng81a03822007-11-17 00:40:40 +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(); }
133 unsigned getNumIntervals() const { return r2iMap_.size(); }
134
135 LiveInterval &getInterval(unsigned reg) {
136 Reg2IntervalMap::iterator I = r2iMap_.find(reg);
137 assert(I != r2iMap_.end() && "Interval does not exist for register");
138 return I->second;
139 }
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");
144 return I->second;
145 }
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
Roman Levenstein8dd25282008-02-18 09:35:30 +0000171 /// getMBBFromIndex - given an index in any instruction of an
172 /// MBB return a pointer the MBB
173 MachineBasicBlock* getMBBFromIndex(unsigned index) const {
174 std::vector<IdxMBBPair>::const_iterator I =
Bill Wendlinge85fe662008-02-26 10:49:39 +0000175 std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), index);
Roman Levenstein8dd25282008-02-18 09:35:30 +0000176 // Take the pair containing the index
177 std::vector<IdxMBBPair>::const_iterator J =
Bill Wendlinge85fe662008-02-26 10:49:39 +0000178 ((I != Idx2MBBMap.end() && I->first > index) ||
179 (I == Idx2MBBMap.end() && Idx2MBBMap.size()>0)) ? (I-1): I;
Roman Levenstein8dd25282008-02-18 09:35:30 +0000180
181 assert(J != Idx2MBBMap.end() && J->first < index+1 &&
Bill Wendlinge85fe662008-02-26 10:49:39 +0000182 index <= getMBBEndIdx(J->second) &&
183 "index does not correspond to an MBB");
Roman Levenstein8dd25282008-02-18 09:35:30 +0000184 return J->second;
185 }
186
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000187 /// getInstructionIndex - returns the base index of instr
188 unsigned getInstructionIndex(MachineInstr* instr) const {
189 Mi2IndexMap::const_iterator it = mi2iMap_.find(instr);
190 assert(it != mi2iMap_.end() && "Invalid instruction!");
191 return it->second;
192 }
193
194 /// getInstructionFromIndex - given an index in any slot of an
195 /// instruction return a pointer the instruction
196 MachineInstr* getInstructionFromIndex(unsigned index) const {
197 index /= InstrSlots::NUM; // convert index to vector index
198 assert(index < i2miMap_.size() &&
199 "index does not correspond to an instruction");
200 return i2miMap_[index];
201 }
David Greene25133302007-06-08 17:18:56 +0000202
Evan Chengc92da382007-11-03 07:20:12 +0000203 /// conflictsWithPhysRegDef - Returns true if the specified register
204 /// is defined during the duration of the specified interval.
205 bool conflictsWithPhysRegDef(const LiveInterval &li, VirtRegMap &vrm,
206 unsigned reg);
207
Evan Cheng4ca980e2007-10-17 02:10:22 +0000208 /// findLiveInMBBs - Given a live range, if the value of the range
209 /// is live in any MBB returns true as well as the list of basic blocks
210 /// where the value is live in.
211 bool findLiveInMBBs(const LiveRange &LR,
Evan Chenga5bfc972007-10-17 06:53:44 +0000212 SmallVectorImpl<MachineBasicBlock*> &MBBs) const;
Evan Cheng4ca980e2007-10-17 02:10:22 +0000213
David Greene25133302007-06-08 17:18:56 +0000214 // Interval creation
215
216 LiveInterval &getOrCreateInterval(unsigned reg) {
217 Reg2IntervalMap::iterator I = r2iMap_.find(reg);
218 if (I == r2iMap_.end())
219 I = r2iMap_.insert(I, std::make_pair(reg, createInterval(reg)));
220 return I->second;
221 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000222
David Greene25133302007-06-08 17:18:56 +0000223 // Interval removal
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000224
David Greene25133302007-06-08 17:18:56 +0000225 void removeInterval(unsigned Reg) {
226 r2iMap_.erase(Reg);
Bill Wendling5c7e3262006-12-17 05:15:13 +0000227 }
Chris Lattner70ca3582004-09-30 15:59:17 +0000228
Evan Cheng30cac022007-02-22 23:03:39 +0000229 /// isRemoved - returns true if the specified machine instr has been
230 /// removed.
231 bool isRemoved(MachineInstr* instr) const {
Evan Cheng7d35c0e2007-02-22 23:52:23 +0000232 return !mi2iMap_.count(instr);
Evan Cheng30cac022007-02-22 23:03:39 +0000233 }
234
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000235 /// RemoveMachineInstrFromMaps - This marks the specified machine instr as
236 /// deleted.
237 void RemoveMachineInstrFromMaps(MachineInstr *MI) {
238 // remove index -> MachineInstr and
239 // MachineInstr -> index mappings
240 Mi2IndexMap::iterator mi2i = mi2iMap_.find(MI);
241 if (mi2i != mi2iMap_.end()) {
242 i2miMap_[mi2i->second/InstrSlots::NUM] = 0;
243 mi2iMap_.erase(mi2i);
244 }
245 }
David Greene25133302007-06-08 17:18:56 +0000246
Evan Cheng70071432008-02-13 03:01:43 +0000247 /// ReplaceMachineInstrInMaps - Replacing a machine instr with a new one in
248 /// maps used by register allocator.
249 void ReplaceMachineInstrInMaps(MachineInstr *MI, MachineInstr *NewMI) {
250 Mi2IndexMap::iterator mi2i = mi2iMap_.find(MI);
Evan Chengb1f6f912008-02-13 09:18:16 +0000251 if (mi2i == mi2iMap_.end())
252 return;
253 i2miMap_[mi2i->second/InstrSlots::NUM] = NewMI;
254 Mi2IndexMap::iterator it = mi2iMap_.find(MI);
255 assert(it != mi2iMap_.end() && "Invalid instruction!");
256 unsigned Index = it->second;
257 mi2iMap_.erase(it);
258 mi2iMap_[NewMI] = Index;
Evan Cheng70071432008-02-13 03:01:43 +0000259 }
260
Evan Chengf3bb2e62007-09-05 21:46:51 +0000261 BumpPtrAllocator& getVNInfoAllocator() { return VNInfoAllocator; }
262
Evan Chengc8d044e2008-02-15 18:24:29 +0000263 /// getVNInfoSourceReg - Helper function that parses the specified VNInfo
264 /// copy field and returns the source register that defines it.
265 unsigned getVNInfoSourceReg(const VNInfo *VNI) const;
266
David Greene25133302007-06-08 17:18:56 +0000267 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
268 virtual void releaseMemory();
269
270 /// runOnMachineFunction - pass entry point
271 virtual bool runOnMachineFunction(MachineFunction&);
272
273 /// print - Implement the dump method.
274 virtual void print(std::ostream &O, const Module* = 0) const;
275 void print(std::ostream *O, const Module* M = 0) const {
276 if (O) print(*O, M);
277 }
278
Evan Chengf2fbca62007-11-12 06:35:08 +0000279 /// addIntervalsForSpills - Create new intervals for spilled defs / uses of
280 /// the given interval.
281 std::vector<LiveInterval*>
Evan Cheng81a03822007-11-17 00:40:40 +0000282 addIntervalsForSpills(const LiveInterval& i,
Evan Cheng22f07ff2007-12-11 02:09:15 +0000283 const MachineLoopInfo *loopInfo, VirtRegMap& vrm);
Evan Chengf2fbca62007-11-12 06:35:08 +0000284
Evan Cheng676dd7c2008-03-11 07:19:34 +0000285 /// spillPhysRegAroundRegDefsUses - Spill the specified physical register
286 /// around all defs and uses of the specified interval.
287 void spillPhysRegAroundRegDefsUses(const LiveInterval &li,
288 unsigned PhysReg, VirtRegMap &vrm);
289
Evan Cheng5ef3a042007-12-06 00:01:56 +0000290 /// isReMaterializable - Returns true if every definition of MI of every
291 /// val# of the specified interval is re-materializable. Also returns true
292 /// by reference if all of the defs are load instructions.
293 bool isReMaterializable(const LiveInterval &li, bool &isLoad);
294
Evan Cheng676dd7c2008-03-11 07:19:34 +0000295 /// getRepresentativeReg - Find the largest super register of the specified
296 /// physical register.
297 unsigned getRepresentativeReg(unsigned Reg) const;
298
299 /// getNumConflictsWithPhysReg - Return the number of uses and defs of the
300 /// specified interval that conflicts with the specified physical register.
301 unsigned getNumConflictsWithPhysReg(const LiveInterval &li,
302 unsigned PhysReg) const;
303
David Greene25133302007-06-08 17:18:56 +0000304 private:
Chris Lattner428b92e2006-09-15 03:57:23 +0000305 /// computeIntervals - Compute live intervals.
Chris Lattnerc7695eb2006-09-14 06:42:17 +0000306 void computeIntervals();
Chris Lattner6bda49f2006-09-02 05:26:01 +0000307
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000308 /// handleRegisterDef - update intervals for a register def
309 /// (calls handlePhysicalRegisterDef and
310 /// handleVirtualRegisterDef)
Chris Lattner6b128bd2006-09-03 08:07:11 +0000311 void handleRegisterDef(MachineBasicBlock *MBB,
312 MachineBasicBlock::iterator MI, unsigned MIIdx,
Chris Lattnerf38a05d2006-01-29 07:59:37 +0000313 unsigned reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000314
315 /// handleVirtualRegisterDef - update intervals for a virtual
316 /// register def
Chris Lattner6b128bd2006-09-03 08:07:11 +0000317 void handleVirtualRegisterDef(MachineBasicBlock *MBB,
318 MachineBasicBlock::iterator MI,
319 unsigned MIIdx,
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000320 LiveInterval& interval);
321
Chris Lattnerf768bba2005-03-09 23:05:19 +0000322 /// handlePhysicalRegisterDef - update intervals for a physical register
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000323 /// def.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000324 void handlePhysicalRegisterDef(MachineBasicBlock* mbb,
325 MachineBasicBlock::iterator mi,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000326 unsigned MIIdx,
Chris Lattner91725b72006-08-31 05:54:43 +0000327 LiveInterval &interval,
Evan Chengc8d044e2008-02-15 18:24:29 +0000328 MachineInstr *CopyMI);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000329
Evan Chengb371f452007-02-19 21:49:54 +0000330 /// handleLiveInRegister - Create interval for a livein register.
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000331 void handleLiveInRegister(MachineBasicBlock* mbb,
332 unsigned MIIdx,
Evan Cheng24a3cc42007-04-25 07:30:23 +0000333 LiveInterval &interval, bool isAlias = false);
Evan Chengb371f452007-02-19 21:49:54 +0000334
Evan Chengd70dbb52008-02-22 09:24:50 +0000335 /// getReMatImplicitUse - If the remat definition MI has one (for now, we
336 /// only allow one) virtual register operand, then its uses are implicitly
337 /// using the register. Returns the virtual register.
338 unsigned getReMatImplicitUse(const LiveInterval &li,
339 MachineInstr *MI) const;
340
341 /// isValNoAvailableAt - Return true if the val# of the specified interval
342 /// which reaches the given instruction also reaches the specified use
343 /// index.
344 bool isValNoAvailableAt(const LiveInterval &li, MachineInstr *MI,
345 unsigned UseIdx) const;
346
Evan Cheng549f27d32007-08-13 23:45:17 +0000347 /// isReMaterializable - Returns true if the definition MI of the specified
Evan Cheng5ef3a042007-12-06 00:01:56 +0000348 /// val# of the specified interval is re-materializable. Also returns true
349 /// by reference if the def is a load.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000350 bool isReMaterializable(const LiveInterval &li, const VNInfo *ValNo,
Evan Cheng5ef3a042007-12-06 00:01:56 +0000351 MachineInstr *MI, bool &isLoad);
Evan Cheng549f27d32007-08-13 23:45:17 +0000352
Evan Cheng35b35c52007-08-30 05:52:20 +0000353 /// tryFoldMemoryOperand - Attempts to fold either a spill / restore from
354 /// slot / to reg or any rematerialized load into ith operand of specified
355 /// MI. If it is successul, MI is updated with the newly created MI and
356 /// returns true.
357 bool tryFoldMemoryOperand(MachineInstr* &MI, VirtRegMap &vrm,
Evan Chengcddbb832007-11-30 21:23:43 +0000358 MachineInstr *DefMI, unsigned InstrIdx,
Evan Chengaee4af62007-12-02 08:30:39 +0000359 SmallVector<unsigned, 2> &Ops,
Evan Chengcddbb832007-11-30 21:23:43 +0000360 bool isSS, int Slot, unsigned Reg);
Evan Cheng549f27d32007-08-13 23:45:17 +0000361
Evan Chengd70dbb52008-02-22 09:24:50 +0000362 /// canFoldMemoryOperand - Return true if the specified load / store
Evan Cheng018f9b02007-12-05 03:22:34 +0000363 /// folding is possible.
Evan Chengd64b5c82007-12-05 03:14:33 +0000364 bool canFoldMemoryOperand(MachineInstr *MI,
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000365 SmallVector<unsigned, 2> &Ops,
366 bool ReMatLoadSS) const;
Evan Chengd64b5c82007-12-05 03:14:33 +0000367
Evan Cheng0cbb1162007-11-29 01:06:25 +0000368 /// anyKillInMBBAfterIdx - Returns true if there is a kill of the specified
369 /// VNInfo that's after the specified index but is within the basic block.
370 bool anyKillInMBBAfterIdx(const LiveInterval &li, const VNInfo *VNI,
371 MachineBasicBlock *MBB, unsigned Idx) const;
Evan Cheng81a03822007-11-17 00:40:40 +0000372
Evan Cheng0cbb1162007-11-29 01:06:25 +0000373 /// intervalIsInOneMBB - Returns true if the specified interval is entirely
374 /// within a single basic block.
Evan Cheng81a03822007-11-17 00:40:40 +0000375 bool intervalIsInOneMBB(const LiveInterval &li) const;
376
Evan Cheng676dd7c2008-03-11 07:19:34 +0000377 /// hasAllocatableSuperReg - Return true if the specified physical register
378 /// has any super register that's allocatable.
379 bool hasAllocatableSuperReg(unsigned Reg) const;
380
Evan Cheng1953d0c2007-11-29 10:12:14 +0000381 /// SRInfo - Spill / restore info.
382 struct SRInfo {
383 int index;
384 unsigned vreg;
385 bool canFold;
386 SRInfo(int i, unsigned vr, bool f) : index(i), vreg(vr), canFold(f) {};
387 };
388
389 bool alsoFoldARestore(int Id, int index, unsigned vr,
390 BitVector &RestoreMBBs,
Chris Lattner84bc5422007-12-31 04:13:23 +0000391 std::map<unsigned,std::vector<SRInfo> >&RestoreIdxes);
Evan Cheng1953d0c2007-11-29 10:12:14 +0000392 void eraseRestoreInfo(int Id, int index, unsigned vr,
393 BitVector &RestoreMBBs,
Chris Lattner84bc5422007-12-31 04:13:23 +0000394 std::map<unsigned,std::vector<SRInfo> >&RestoreIdxes);
Evan Cheng1953d0c2007-11-29 10:12:14 +0000395
Evan Chengd70dbb52008-02-22 09:24:50 +0000396 /// rewriteImplicitOps - Rewrite implicit use operands of MI (i.e. uses of
397 /// interval on to-be re-materialized operands of MI) with new register.
398 void rewriteImplicitOps(const LiveInterval &li,
399 MachineInstr *MI, unsigned NewVReg, VirtRegMap &vrm);
400
Chris Lattner84bc5422007-12-31 04:13:23 +0000401 /// rewriteInstructionForSpills, rewriteInstructionsForSpills - Helper
402 /// functions for addIntervalsForSpills to rewrite uses / defs for the given
403 /// live range.
Evan Chengd70dbb52008-02-22 09:24:50 +0000404 bool rewriteInstructionForSpills(const LiveInterval &li, const VNInfo *VNI,
405 bool TrySplit, unsigned index, unsigned end, MachineInstr *MI,
Evan Chengf2fbca62007-11-12 06:35:08 +0000406 MachineInstr *OrigDefMI, MachineInstr *DefMI, unsigned Slot, int LdSlot,
407 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
Evan Chengd70dbb52008-02-22 09:24:50 +0000408 VirtRegMap &vrm, const TargetRegisterClass* rc,
409 SmallVector<int, 4> &ReMatIds, const MachineLoopInfo *loopInfo,
Evan Cheng0cc83b62008-02-23 00:46:11 +0000410 unsigned &NewVReg, unsigned ImpUse, bool &HasDef, bool &HasUse,
Evan Cheng1953d0c2007-11-29 10:12:14 +0000411 std::map<unsigned,unsigned> &MBBVRegsMap,
Evan Chengf2fbca62007-11-12 06:35:08 +0000412 std::vector<LiveInterval*> &NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +0000413 void rewriteInstructionsForSpills(const LiveInterval &li, bool TrySplit,
Evan Chengf2fbca62007-11-12 06:35:08 +0000414 LiveInterval::Ranges::const_iterator &I,
415 MachineInstr *OrigDefMI, MachineInstr *DefMI, unsigned Slot, int LdSlot,
416 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
Evan Chengd70dbb52008-02-22 09:24:50 +0000417 VirtRegMap &vrm, const TargetRegisterClass* rc,
Evan Cheng22f07ff2007-12-11 02:09:15 +0000418 SmallVector<int, 4> &ReMatIds, const MachineLoopInfo *loopInfo,
Evan Cheng81a03822007-11-17 00:40:40 +0000419 BitVector &SpillMBBs,
Evan Cheng1953d0c2007-11-29 10:12:14 +0000420 std::map<unsigned,std::vector<SRInfo> > &SpillIdxes,
Evan Cheng0cbb1162007-11-29 01:06:25 +0000421 BitVector &RestoreMBBs,
Evan Cheng1953d0c2007-11-29 10:12:14 +0000422 std::map<unsigned,std::vector<SRInfo> > &RestoreIdxes,
423 std::map<unsigned,unsigned> &MBBVRegsMap,
Evan Chengf2fbca62007-11-12 06:35:08 +0000424 std::vector<LiveInterval*> &NewLIs);
425
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000426 static LiveInterval createInterval(unsigned Reg);
427
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000428 void printRegName(unsigned reg) const;
429 };
430
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000431} // End llvm namespace
432
433#endif