blob: 473cc8e099694acd35a1143580b46a2be7b7dd34 [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>
Dan Gohmanc9235d22008-03-21 23:51:57 +000031#include <map>
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000032
33namespace llvm {
34
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 };
57
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_;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000064 LiveVariables* lv_;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000065
Evan Chengf3bb2e62007-09-05 21:46:51 +000066 /// Special pool allocator for VNInfo's (LiveInterval val#).
67 ///
68 BumpPtrAllocator VNInfoAllocator;
69
Evan Cheng549f27d32007-08-13 23:45:17 +000070 /// MBB2IdxMap - The indexes of the first and last instructions in the
71 /// specified basic block.
72 std::vector<std::pair<unsigned, unsigned> > MBB2IdxMap;
David Greene25133302007-06-08 17:18:56 +000073
Evan Cheng4ca980e2007-10-17 02:10:22 +000074 /// Idx2MBBMap - Sorted list of pairs of index of first instruction
75 /// and MBB id.
76 std::vector<IdxMBBPair> Idx2MBBMap;
77
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000078 typedef std::map<MachineInstr*, unsigned> Mi2IndexMap;
79 Mi2IndexMap mi2iMap_;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000080
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000081 typedef std::vector<MachineInstr*> Index2MiMap;
82 Index2MiMap i2miMap_;
Alkis Evlogimenos843b1602004-02-15 10:24:21 +000083
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000084 typedef std::map<unsigned, LiveInterval> Reg2IntervalMap;
85 Reg2IntervalMap r2iMap_;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000086
Evan Cheng61de82d2007-02-15 05:59:24 +000087 BitVector allocatableRegs_;
Evan Cheng88d1f582007-03-01 02:03:03 +000088
Evan Cheng549f27d32007-08-13 23:45:17 +000089 std::vector<MachineInstr*> ClonedMIs;
90
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000091 public:
Nick Lewyckyecd94c82007-05-06 13:37:16 +000092 static char ID; // Pass identification, replacement for typeid
Devang Patel794fd752007-05-01 21:15:47 +000093 LiveIntervals() : MachineFunctionPass((intptr_t)&ID) {}
94
Chris Lattnerf7da2c72006-08-24 22:43:55 +000095 struct InstrSlots {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000096 enum {
97 LOAD = 0,
98 USE = 1,
99 DEF = 2,
100 STORE = 3,
Chris Lattner410354f2006-02-22 16:23:43 +0000101 NUM = 4
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000102 };
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000103 };
104
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000105 static unsigned getBaseIndex(unsigned index) {
106 return index - (index % InstrSlots::NUM);
107 }
108 static unsigned getBoundaryIndex(unsigned index) {
109 return getBaseIndex(index + InstrSlots::NUM - 1);
110 }
111 static unsigned getLoadIndex(unsigned index) {
112 return getBaseIndex(index) + InstrSlots::LOAD;
113 }
114 static unsigned getUseIndex(unsigned index) {
115 return getBaseIndex(index) + InstrSlots::USE;
116 }
117 static unsigned getDefIndex(unsigned index) {
118 return getBaseIndex(index) + InstrSlots::DEF;
119 }
120 static unsigned getStoreIndex(unsigned index) {
121 return getBaseIndex(index) + InstrSlots::STORE;
122 }
123
Evan Cheng81a03822007-11-17 00:40:40 +0000124 static float getSpillWeight(bool isDef, bool isUse, unsigned loopDepth) {
125 return (isDef + isUse) * powf(10.0F, (float)loopDepth);
Evan Chengf2fbca62007-11-12 06:35:08 +0000126 }
127
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000128 typedef Reg2IntervalMap::iterator iterator;
Chris Lattner70ca3582004-09-30 15:59:17 +0000129 typedef Reg2IntervalMap::const_iterator const_iterator;
130 const_iterator begin() const { return r2iMap_.begin(); }
131 const_iterator end() const { return r2iMap_.end(); }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000132 iterator begin() { return r2iMap_.begin(); }
133 iterator end() { return r2iMap_.end(); }
Evan Cheng34cd4a42008-05-05 18:30:58 +0000134 unsigned getNumIntervals() const { return (unsigned)r2iMap_.size(); }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000135
136 LiveInterval &getInterval(unsigned reg) {
137 Reg2IntervalMap::iterator I = r2iMap_.find(reg);
138 assert(I != r2iMap_.end() && "Interval does not exist for register");
139 return I->second;
140 }
141
142 const LiveInterval &getInterval(unsigned reg) const {
143 Reg2IntervalMap::const_iterator I = r2iMap_.find(reg);
144 assert(I != r2iMap_.end() && "Interval does not exist for register");
145 return I->second;
146 }
147
Evan Chengb371f452007-02-19 21:49:54 +0000148 bool hasInterval(unsigned reg) const {
Evan Cheng88d1f582007-03-01 02:03:03 +0000149 return r2iMap_.count(reg);
Evan Chengb371f452007-02-19 21:49:54 +0000150 }
151
Chris Lattner428b92e2006-09-15 03:57:23 +0000152 /// getMBBStartIdx - Return the base index of the first instruction in the
153 /// specified MachineBasicBlock.
154 unsigned getMBBStartIdx(MachineBasicBlock *MBB) const {
155 return getMBBStartIdx(MBB->getNumber());
156 }
Chris Lattner428b92e2006-09-15 03:57:23 +0000157 unsigned getMBBStartIdx(unsigned MBBNo) const {
158 assert(MBBNo < MBB2IdxMap.size() && "Invalid MBB number!");
Evan Cheng549f27d32007-08-13 23:45:17 +0000159 return MBB2IdxMap[MBBNo].first;
160 }
161
162 /// getMBBEndIdx - Return the store index of the last instruction in the
163 /// specified MachineBasicBlock.
164 unsigned getMBBEndIdx(MachineBasicBlock *MBB) const {
165 return getMBBEndIdx(MBB->getNumber());
166 }
167 unsigned getMBBEndIdx(unsigned MBBNo) const {
168 assert(MBBNo < MBB2IdxMap.size() && "Invalid MBB number!");
169 return MBB2IdxMap[MBBNo].second;
Chris Lattner428b92e2006-09-15 03:57:23 +0000170 }
171
Roman Levenstein8dd25282008-02-18 09:35:30 +0000172 /// getMBBFromIndex - given an index in any instruction of an
173 /// MBB return a pointer the MBB
174 MachineBasicBlock* getMBBFromIndex(unsigned index) const {
175 std::vector<IdxMBBPair>::const_iterator I =
Bill Wendlinge85fe662008-02-26 10:49:39 +0000176 std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), index);
Roman Levenstein8dd25282008-02-18 09:35:30 +0000177 // Take the pair containing the index
178 std::vector<IdxMBBPair>::const_iterator J =
Bill Wendlinge85fe662008-02-26 10:49:39 +0000179 ((I != Idx2MBBMap.end() && I->first > index) ||
180 (I == Idx2MBBMap.end() && Idx2MBBMap.size()>0)) ? (I-1): I;
Roman Levenstein8dd25282008-02-18 09:35:30 +0000181
182 assert(J != Idx2MBBMap.end() && J->first < index+1 &&
Bill Wendlinge85fe662008-02-26 10:49:39 +0000183 index <= getMBBEndIdx(J->second) &&
184 "index does not correspond to an MBB");
Roman Levenstein8dd25282008-02-18 09:35:30 +0000185 return J->second;
186 }
187
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000188 /// getInstructionIndex - returns the base index of instr
189 unsigned getInstructionIndex(MachineInstr* instr) const {
190 Mi2IndexMap::const_iterator it = mi2iMap_.find(instr);
191 assert(it != mi2iMap_.end() && "Invalid instruction!");
192 return it->second;
193 }
194
195 /// getInstructionFromIndex - given an index in any slot of an
196 /// instruction return a pointer the instruction
197 MachineInstr* getInstructionFromIndex(unsigned index) const {
198 index /= InstrSlots::NUM; // convert index to vector index
199 assert(index < i2miMap_.size() &&
200 "index does not correspond to an instruction");
201 return i2miMap_[index];
202 }
David Greene25133302007-06-08 17:18:56 +0000203
Evan Chengc92da382007-11-03 07:20:12 +0000204 /// conflictsWithPhysRegDef - Returns true if the specified register
205 /// is defined during the duration of the specified interval.
206 bool conflictsWithPhysRegDef(const LiveInterval &li, VirtRegMap &vrm,
207 unsigned reg);
208
Evan Cheng4ca980e2007-10-17 02:10:22 +0000209 /// findLiveInMBBs - Given a live range, if the value of the range
210 /// is live in any MBB returns true as well as the list of basic blocks
211 /// where the value is live in.
212 bool findLiveInMBBs(const LiveRange &LR,
Evan Chenga5bfc972007-10-17 06:53:44 +0000213 SmallVectorImpl<MachineBasicBlock*> &MBBs) const;
Evan Cheng4ca980e2007-10-17 02:10:22 +0000214
David Greene25133302007-06-08 17:18:56 +0000215 // Interval creation
216
217 LiveInterval &getOrCreateInterval(unsigned reg) {
218 Reg2IntervalMap::iterator I = r2iMap_.find(reg);
219 if (I == r2iMap_.end())
220 I = r2iMap_.insert(I, std::make_pair(reg, createInterval(reg)));
221 return I->second;
222 }
Owen Andersonc4dc1322008-06-05 17:15:43 +0000223
224 /// addLiveRangeToEndOfBlock - Given a register and an instruction,
225 /// adds a live range from that instruction to the end of its MBB.
226 LiveRange addLiveRangeToEndOfBlock(unsigned reg,
227 MachineInstr* startInst);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000228
David Greene25133302007-06-08 17:18:56 +0000229 // Interval removal
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000230
David Greene25133302007-06-08 17:18:56 +0000231 void removeInterval(unsigned Reg) {
232 r2iMap_.erase(Reg);
Bill Wendling5c7e3262006-12-17 05:15:13 +0000233 }
Chris Lattner70ca3582004-09-30 15:59:17 +0000234
Evan Cheng30cac022007-02-22 23:03:39 +0000235 /// isRemoved - returns true if the specified machine instr has been
236 /// removed.
237 bool isRemoved(MachineInstr* instr) const {
Evan Cheng7d35c0e2007-02-22 23:52:23 +0000238 return !mi2iMap_.count(instr);
Evan Cheng30cac022007-02-22 23:03:39 +0000239 }
240
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000241 /// RemoveMachineInstrFromMaps - This marks the specified machine instr as
242 /// deleted.
243 void RemoveMachineInstrFromMaps(MachineInstr *MI) {
244 // remove index -> MachineInstr and
245 // MachineInstr -> index mappings
246 Mi2IndexMap::iterator mi2i = mi2iMap_.find(MI);
247 if (mi2i != mi2iMap_.end()) {
248 i2miMap_[mi2i->second/InstrSlots::NUM] = 0;
249 mi2iMap_.erase(mi2i);
250 }
251 }
David Greene25133302007-06-08 17:18:56 +0000252
Evan Cheng70071432008-02-13 03:01:43 +0000253 /// ReplaceMachineInstrInMaps - Replacing a machine instr with a new one in
254 /// maps used by register allocator.
255 void ReplaceMachineInstrInMaps(MachineInstr *MI, MachineInstr *NewMI) {
256 Mi2IndexMap::iterator mi2i = mi2iMap_.find(MI);
Evan Chengb1f6f912008-02-13 09:18:16 +0000257 if (mi2i == mi2iMap_.end())
258 return;
259 i2miMap_[mi2i->second/InstrSlots::NUM] = NewMI;
260 Mi2IndexMap::iterator it = mi2iMap_.find(MI);
261 assert(it != mi2iMap_.end() && "Invalid instruction!");
262 unsigned Index = it->second;
263 mi2iMap_.erase(it);
264 mi2iMap_[NewMI] = Index;
Evan Cheng70071432008-02-13 03:01:43 +0000265 }
266
Evan Chengf3bb2e62007-09-05 21:46:51 +0000267 BumpPtrAllocator& getVNInfoAllocator() { return VNInfoAllocator; }
268
Evan Chengc8d044e2008-02-15 18:24:29 +0000269 /// getVNInfoSourceReg - Helper function that parses the specified VNInfo
270 /// copy field and returns the source register that defines it.
271 unsigned getVNInfoSourceReg(const VNInfo *VNI) const;
272
David Greene25133302007-06-08 17:18:56 +0000273 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
274 virtual void releaseMemory();
275
276 /// runOnMachineFunction - pass entry point
277 virtual bool runOnMachineFunction(MachineFunction&);
278
279 /// print - Implement the dump method.
280 virtual void print(std::ostream &O, const Module* = 0) const;
281 void print(std::ostream *O, const Module* M = 0) const {
282 if (O) print(*O, M);
283 }
284
Evan Chengf2fbca62007-11-12 06:35:08 +0000285 /// addIntervalsForSpills - Create new intervals for spilled defs / uses of
Evan Cheng9c3c2212008-06-06 07:54:39 +0000286 /// the given interval. FIXME: It also returns the weight of the spill slot
287 /// (if any is created) by reference. This is temporary.
Evan Chengf2fbca62007-11-12 06:35:08 +0000288 std::vector<LiveInterval*>
Evan Cheng81a03822007-11-17 00:40:40 +0000289 addIntervalsForSpills(const LiveInterval& i,
Evan Cheng9c3c2212008-06-06 07:54:39 +0000290 const MachineLoopInfo *loopInfo, VirtRegMap& vrm,
291 float &SSWeight);
Evan Chengf2fbca62007-11-12 06:35:08 +0000292
Evan Cheng676dd7c2008-03-11 07:19:34 +0000293 /// spillPhysRegAroundRegDefsUses - Spill the specified physical register
294 /// around all defs and uses of the specified interval.
295 void spillPhysRegAroundRegDefsUses(const LiveInterval &li,
296 unsigned PhysReg, VirtRegMap &vrm);
297
Evan Cheng5ef3a042007-12-06 00:01:56 +0000298 /// isReMaterializable - Returns true if every definition of MI of every
299 /// val# of the specified interval is re-materializable. Also returns true
300 /// by reference if all of the defs are load instructions.
301 bool isReMaterializable(const LiveInterval &li, bool &isLoad);
302
Evan Cheng676dd7c2008-03-11 07:19:34 +0000303 /// getRepresentativeReg - Find the largest super register of the specified
304 /// physical register.
305 unsigned getRepresentativeReg(unsigned Reg) const;
306
307 /// getNumConflictsWithPhysReg - Return the number of uses and defs of the
308 /// specified interval that conflicts with the specified physical register.
309 unsigned getNumConflictsWithPhysReg(const LiveInterval &li,
310 unsigned PhysReg) const;
311
Owen Anderson15a17f52008-05-30 20:14:04 +0000312 /// computeNumbering - Compute the index numbering.
313 void computeNumbering();
314
David Greene25133302007-06-08 17:18:56 +0000315 private:
Chris Lattner428b92e2006-09-15 03:57:23 +0000316 /// computeIntervals - Compute live intervals.
Chris Lattnerc7695eb2006-09-14 06:42:17 +0000317 void computeIntervals();
Chris Lattner6bda49f2006-09-02 05:26:01 +0000318
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000319 /// handleRegisterDef - update intervals for a register def
320 /// (calls handlePhysicalRegisterDef and
321 /// handleVirtualRegisterDef)
Chris Lattner6b128bd2006-09-03 08:07:11 +0000322 void handleRegisterDef(MachineBasicBlock *MBB,
323 MachineBasicBlock::iterator MI, unsigned MIIdx,
Chris Lattnerf38a05d2006-01-29 07:59:37 +0000324 unsigned reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000325
326 /// handleVirtualRegisterDef - update intervals for a virtual
327 /// register def
Chris Lattner6b128bd2006-09-03 08:07:11 +0000328 void handleVirtualRegisterDef(MachineBasicBlock *MBB,
329 MachineBasicBlock::iterator MI,
330 unsigned MIIdx,
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000331 LiveInterval& interval);
332
Chris Lattnerf768bba2005-03-09 23:05:19 +0000333 /// handlePhysicalRegisterDef - update intervals for a physical register
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000334 /// def.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000335 void handlePhysicalRegisterDef(MachineBasicBlock* mbb,
336 MachineBasicBlock::iterator mi,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000337 unsigned MIIdx,
Chris Lattner91725b72006-08-31 05:54:43 +0000338 LiveInterval &interval,
Evan Chengc8d044e2008-02-15 18:24:29 +0000339 MachineInstr *CopyMI);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000340
Evan Chengb371f452007-02-19 21:49:54 +0000341 /// handleLiveInRegister - Create interval for a livein register.
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000342 void handleLiveInRegister(MachineBasicBlock* mbb,
343 unsigned MIIdx,
Evan Cheng24a3cc42007-04-25 07:30:23 +0000344 LiveInterval &interval, bool isAlias = false);
Evan Chengb371f452007-02-19 21:49:54 +0000345
Evan Chengd70dbb52008-02-22 09:24:50 +0000346 /// getReMatImplicitUse - If the remat definition MI has one (for now, we
347 /// only allow one) virtual register operand, then its uses are implicitly
348 /// using the register. Returns the virtual register.
349 unsigned getReMatImplicitUse(const LiveInterval &li,
350 MachineInstr *MI) const;
351
352 /// isValNoAvailableAt - Return true if the val# of the specified interval
353 /// which reaches the given instruction also reaches the specified use
354 /// index.
355 bool isValNoAvailableAt(const LiveInterval &li, MachineInstr *MI,
356 unsigned UseIdx) const;
357
Evan Cheng549f27d32007-08-13 23:45:17 +0000358 /// isReMaterializable - Returns true if the definition MI of the specified
Evan Cheng5ef3a042007-12-06 00:01:56 +0000359 /// val# of the specified interval is re-materializable. Also returns true
360 /// by reference if the def is a load.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000361 bool isReMaterializable(const LiveInterval &li, const VNInfo *ValNo,
Evan Cheng5ef3a042007-12-06 00:01:56 +0000362 MachineInstr *MI, bool &isLoad);
Evan Cheng549f27d32007-08-13 23:45:17 +0000363
Evan Cheng35b35c52007-08-30 05:52:20 +0000364 /// tryFoldMemoryOperand - Attempts to fold either a spill / restore from
365 /// slot / to reg or any rematerialized load into ith operand of specified
366 /// MI. If it is successul, MI is updated with the newly created MI and
367 /// returns true.
368 bool tryFoldMemoryOperand(MachineInstr* &MI, VirtRegMap &vrm,
Evan Chengcddbb832007-11-30 21:23:43 +0000369 MachineInstr *DefMI, unsigned InstrIdx,
Evan Chengaee4af62007-12-02 08:30:39 +0000370 SmallVector<unsigned, 2> &Ops,
Evan Chengcddbb832007-11-30 21:23:43 +0000371 bool isSS, int Slot, unsigned Reg);
Evan Cheng549f27d32007-08-13 23:45:17 +0000372
Evan Chengd70dbb52008-02-22 09:24:50 +0000373 /// canFoldMemoryOperand - Return true if the specified load / store
Evan Cheng018f9b02007-12-05 03:22:34 +0000374 /// folding is possible.
Evan Chengd64b5c82007-12-05 03:14:33 +0000375 bool canFoldMemoryOperand(MachineInstr *MI,
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000376 SmallVector<unsigned, 2> &Ops,
377 bool ReMatLoadSS) const;
Evan Chengd64b5c82007-12-05 03:14:33 +0000378
Evan Cheng0cbb1162007-11-29 01:06:25 +0000379 /// anyKillInMBBAfterIdx - Returns true if there is a kill of the specified
380 /// VNInfo that's after the specified index but is within the basic block.
381 bool anyKillInMBBAfterIdx(const LiveInterval &li, const VNInfo *VNI,
382 MachineBasicBlock *MBB, unsigned Idx) const;
Evan Cheng81a03822007-11-17 00:40:40 +0000383
Evan Cheng0cbb1162007-11-29 01:06:25 +0000384 /// intervalIsInOneMBB - Returns true if the specified interval is entirely
385 /// within a single basic block.
Evan Cheng81a03822007-11-17 00:40:40 +0000386 bool intervalIsInOneMBB(const LiveInterval &li) const;
387
Evan Cheng676dd7c2008-03-11 07:19:34 +0000388 /// hasAllocatableSuperReg - Return true if the specified physical register
389 /// has any super register that's allocatable.
390 bool hasAllocatableSuperReg(unsigned Reg) const;
391
Evan Cheng1953d0c2007-11-29 10:12:14 +0000392 /// SRInfo - Spill / restore info.
393 struct SRInfo {
394 int index;
395 unsigned vreg;
396 bool canFold;
397 SRInfo(int i, unsigned vr, bool f) : index(i), vreg(vr), canFold(f) {};
398 };
399
400 bool alsoFoldARestore(int Id, int index, unsigned vr,
401 BitVector &RestoreMBBs,
Chris Lattner84bc5422007-12-31 04:13:23 +0000402 std::map<unsigned,std::vector<SRInfo> >&RestoreIdxes);
Evan Cheng1953d0c2007-11-29 10:12:14 +0000403 void eraseRestoreInfo(int Id, int index, unsigned vr,
404 BitVector &RestoreMBBs,
Chris Lattner84bc5422007-12-31 04:13:23 +0000405 std::map<unsigned,std::vector<SRInfo> >&RestoreIdxes);
Evan Cheng1953d0c2007-11-29 10:12:14 +0000406
Evan Cheng4cce6b42008-04-11 17:53:36 +0000407 /// handleSpilledImpDefs - Remove IMPLICIT_DEF instructions which are being
408 /// spilled and create empty intervals for their uses.
409 void handleSpilledImpDefs(const LiveInterval &li, VirtRegMap &vrm,
410 const TargetRegisterClass* rc,
411 std::vector<LiveInterval*> &NewLIs);
Evan Cheng419852c2008-04-03 16:39:43 +0000412
Evan Chengd70dbb52008-02-22 09:24:50 +0000413 /// rewriteImplicitOps - Rewrite implicit use operands of MI (i.e. uses of
414 /// interval on to-be re-materialized operands of MI) with new register.
415 void rewriteImplicitOps(const LiveInterval &li,
416 MachineInstr *MI, unsigned NewVReg, VirtRegMap &vrm);
417
Chris Lattner84bc5422007-12-31 04:13:23 +0000418 /// rewriteInstructionForSpills, rewriteInstructionsForSpills - Helper
419 /// functions for addIntervalsForSpills to rewrite uses / defs for the given
420 /// live range.
Evan Chengd70dbb52008-02-22 09:24:50 +0000421 bool rewriteInstructionForSpills(const LiveInterval &li, const VNInfo *VNI,
422 bool TrySplit, unsigned index, unsigned end, MachineInstr *MI,
Evan Chengf2fbca62007-11-12 06:35:08 +0000423 MachineInstr *OrigDefMI, MachineInstr *DefMI, unsigned Slot, int LdSlot,
424 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
Evan Chengd70dbb52008-02-22 09:24:50 +0000425 VirtRegMap &vrm, const TargetRegisterClass* rc,
426 SmallVector<int, 4> &ReMatIds, const MachineLoopInfo *loopInfo,
Evan Cheng0cc83b62008-02-23 00:46:11 +0000427 unsigned &NewVReg, unsigned ImpUse, bool &HasDef, bool &HasUse,
Evan Cheng1953d0c2007-11-29 10:12:14 +0000428 std::map<unsigned,unsigned> &MBBVRegsMap,
Evan Cheng9c3c2212008-06-06 07:54:39 +0000429 std::vector<LiveInterval*> &NewLIs, float &SSWeight);
Evan Cheng81a03822007-11-17 00:40:40 +0000430 void rewriteInstructionsForSpills(const LiveInterval &li, bool TrySplit,
Evan Chengf2fbca62007-11-12 06:35:08 +0000431 LiveInterval::Ranges::const_iterator &I,
432 MachineInstr *OrigDefMI, MachineInstr *DefMI, unsigned Slot, int LdSlot,
433 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
Evan Chengd70dbb52008-02-22 09:24:50 +0000434 VirtRegMap &vrm, const TargetRegisterClass* rc,
Evan Cheng22f07ff2007-12-11 02:09:15 +0000435 SmallVector<int, 4> &ReMatIds, const MachineLoopInfo *loopInfo,
Evan Cheng81a03822007-11-17 00:40:40 +0000436 BitVector &SpillMBBs,
Evan Cheng1953d0c2007-11-29 10:12:14 +0000437 std::map<unsigned,std::vector<SRInfo> > &SpillIdxes,
Evan Cheng0cbb1162007-11-29 01:06:25 +0000438 BitVector &RestoreMBBs,
Evan Cheng1953d0c2007-11-29 10:12:14 +0000439 std::map<unsigned,std::vector<SRInfo> > &RestoreIdxes,
440 std::map<unsigned,unsigned> &MBBVRegsMap,
Evan Cheng9c3c2212008-06-06 07:54:39 +0000441 std::vector<LiveInterval*> &NewLIs, float &SSWeight);
Evan Chengf2fbca62007-11-12 06:35:08 +0000442
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000443 static LiveInterval createInterval(unsigned Reg);
444
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000445 void printRegName(unsigned reg) const;
446 };
447
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000448} // End llvm namespace
449
450#endif