Chris Lattner | a3b8b5c | 2004-07-23 17:56:30 +0000 | [diff] [blame] | 1 | //===-- LiveIntervalAnalysis.h - Live Interval Analysis ---------*- C++ -*-===// |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Chris Lattner | 6b92906 | 2004-07-19 02:13:59 +0000 | [diff] [blame] | 10 | // 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 |
| 13 | // instruction with number j' > j such that v is live at j' abd there is no |
| 14 | // 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 Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 17 | // |
| 18 | //===----------------------------------------------------------------------===// |
| 19 | |
Chris Lattner | a3b8b5c | 2004-07-23 17:56:30 +0000 | [diff] [blame] | 20 | #ifndef LLVM_CODEGEN_LIVEINTERVAL_ANALYSIS_H |
| 21 | #define LLVM_CODEGEN_LIVEINTERVAL_ANALYSIS_H |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 22 | |
Alkis Evlogimenos | 5d0d1e3 | 2004-09-08 03:01:50 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/DenseMap.h" |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Chris Lattner | 779a651 | 2005-09-21 04:18:25 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/LiveInterval.h" |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 26 | |
| 27 | namespace llvm { |
| 28 | |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 29 | class LiveVariables; |
| 30 | class MRegisterInfo; |
Chris Lattner | f768bba | 2005-03-09 23:05:19 +0000 | [diff] [blame] | 31 | class TargetInstrInfo; |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 32 | class VirtRegMap; |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 33 | |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 34 | class LiveIntervals : public MachineFunctionPass { |
| 35 | MachineFunction* mf_; |
| 36 | const TargetMachine* tm_; |
| 37 | const MRegisterInfo* mri_; |
Chris Lattner | f768bba | 2005-03-09 23:05:19 +0000 | [diff] [blame] | 38 | const TargetInstrInfo* tii_; |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 39 | LiveVariables* lv_; |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 40 | |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 41 | typedef std::map<MachineInstr*, unsigned> Mi2IndexMap; |
| 42 | Mi2IndexMap mi2iMap_; |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 43 | |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 44 | typedef std::vector<MachineInstr*> Index2MiMap; |
| 45 | Index2MiMap i2miMap_; |
Alkis Evlogimenos | 843b160 | 2004-02-15 10:24:21 +0000 | [diff] [blame] | 46 | |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 47 | typedef std::map<unsigned, LiveInterval> Reg2IntervalMap; |
| 48 | Reg2IntervalMap r2iMap_; |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 49 | |
Alkis Evlogimenos | 5d0d1e3 | 2004-09-08 03:01:50 +0000 | [diff] [blame] | 50 | typedef DenseMap<unsigned> Reg2RegMap; |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 51 | Reg2RegMap r2rMap_; |
Alkis Evlogimenos | e88280a | 2004-01-22 23:08:45 +0000 | [diff] [blame] | 52 | |
Alkis Evlogimenos | 5327801 | 2004-08-26 22:22:38 +0000 | [diff] [blame] | 53 | std::vector<bool> allocatableRegs_; |
| 54 | |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 55 | public: |
Chris Lattner | f7da2c7 | 2006-08-24 22:43:55 +0000 | [diff] [blame^] | 56 | struct CopyRec { |
| 57 | MachineInstr *MI; |
| 58 | unsigned SrcReg, DstReg; |
| 59 | }; |
| 60 | CopyRec getCopyRec(MachineInstr *MI, unsigned SrcReg, unsigned DstReg) { |
| 61 | CopyRec R; |
| 62 | R.MI = MI; |
| 63 | R.SrcReg = SrcReg; |
| 64 | R.DstReg = DstReg; |
| 65 | return R; |
| 66 | } |
| 67 | struct InstrSlots { |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 68 | enum { |
| 69 | LOAD = 0, |
| 70 | USE = 1, |
| 71 | DEF = 2, |
| 72 | STORE = 3, |
Chris Lattner | 410354f | 2006-02-22 16:23:43 +0000 | [diff] [blame] | 73 | NUM = 4 |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 74 | }; |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 75 | }; |
| 76 | |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 77 | static unsigned getBaseIndex(unsigned index) { |
| 78 | return index - (index % InstrSlots::NUM); |
| 79 | } |
| 80 | static unsigned getBoundaryIndex(unsigned index) { |
| 81 | return getBaseIndex(index + InstrSlots::NUM - 1); |
| 82 | } |
| 83 | static unsigned getLoadIndex(unsigned index) { |
| 84 | return getBaseIndex(index) + InstrSlots::LOAD; |
| 85 | } |
| 86 | static unsigned getUseIndex(unsigned index) { |
| 87 | return getBaseIndex(index) + InstrSlots::USE; |
| 88 | } |
| 89 | static unsigned getDefIndex(unsigned index) { |
| 90 | return getBaseIndex(index) + InstrSlots::DEF; |
| 91 | } |
| 92 | static unsigned getStoreIndex(unsigned index) { |
| 93 | return getBaseIndex(index) + InstrSlots::STORE; |
| 94 | } |
| 95 | |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 96 | typedef Reg2IntervalMap::iterator iterator; |
Chris Lattner | 70ca358 | 2004-09-30 15:59:17 +0000 | [diff] [blame] | 97 | typedef Reg2IntervalMap::const_iterator const_iterator; |
| 98 | const_iterator begin() const { return r2iMap_.begin(); } |
| 99 | const_iterator end() const { return r2iMap_.end(); } |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 100 | iterator begin() { return r2iMap_.begin(); } |
| 101 | iterator end() { return r2iMap_.end(); } |
| 102 | unsigned getNumIntervals() const { return r2iMap_.size(); } |
| 103 | |
| 104 | LiveInterval &getInterval(unsigned reg) { |
| 105 | Reg2IntervalMap::iterator I = r2iMap_.find(reg); |
| 106 | assert(I != r2iMap_.end() && "Interval does not exist for register"); |
| 107 | return I->second; |
| 108 | } |
| 109 | |
| 110 | const LiveInterval &getInterval(unsigned reg) const { |
| 111 | Reg2IntervalMap::const_iterator I = r2iMap_.find(reg); |
| 112 | assert(I != r2iMap_.end() && "Interval does not exist for register"); |
| 113 | return I->second; |
| 114 | } |
| 115 | |
| 116 | /// getInstructionIndex - returns the base index of instr |
| 117 | unsigned getInstructionIndex(MachineInstr* instr) const { |
| 118 | Mi2IndexMap::const_iterator it = mi2iMap_.find(instr); |
| 119 | assert(it != mi2iMap_.end() && "Invalid instruction!"); |
| 120 | return it->second; |
| 121 | } |
| 122 | |
| 123 | /// getInstructionFromIndex - given an index in any slot of an |
| 124 | /// instruction return a pointer the instruction |
| 125 | MachineInstr* getInstructionFromIndex(unsigned index) const { |
| 126 | index /= InstrSlots::NUM; // convert index to vector index |
| 127 | assert(index < i2miMap_.size() && |
| 128 | "index does not correspond to an instruction"); |
| 129 | return i2miMap_[index]; |
| 130 | } |
| 131 | |
| 132 | std::vector<LiveInterval*> addIntervalsForSpills(const LiveInterval& i, |
| 133 | VirtRegMap& vrm, |
| 134 | int slot); |
| 135 | |
| 136 | virtual void getAnalysisUsage(AnalysisUsage &AU) const; |
| 137 | virtual void releaseMemory(); |
| 138 | |
| 139 | /// runOnMachineFunction - pass entry point |
| 140 | virtual bool runOnMachineFunction(MachineFunction&); |
| 141 | |
Chris Lattner | 70ca358 | 2004-09-30 15:59:17 +0000 | [diff] [blame] | 142 | /// print - Implement the dump method. |
Reid Spencer | ce9653c | 2004-12-07 04:03:45 +0000 | [diff] [blame] | 143 | virtual void print(std::ostream &O, const Module* = 0) const; |
Chris Lattner | 70ca358 | 2004-09-30 15:59:17 +0000 | [diff] [blame] | 144 | |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 145 | private: |
Chris Lattner | f7da2c7 | 2006-08-24 22:43:55 +0000 | [diff] [blame^] | 146 | /// RemoveMachineInstrFromMaps - This marks the specified machine instr as |
| 147 | /// deleted. |
| 148 | void RemoveMachineInstrFromMaps(MachineInstr *MI) { |
| 149 | // remove index -> MachineInstr and |
| 150 | // MachineInstr -> index mappings |
| 151 | Mi2IndexMap::iterator mi2i = mi2iMap_.find(MI); |
| 152 | if (mi2i != mi2iMap_.end()) { |
| 153 | i2miMap_[mi2i->second/InstrSlots::NUM] = 0; |
| 154 | mi2iMap_.erase(mi2i); |
| 155 | } |
| 156 | } |
| 157 | |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 158 | /// computeIntervals - compute live intervals |
| 159 | void computeIntervals(); |
| 160 | |
| 161 | /// joinIntervals - join compatible live intervals |
| 162 | void joinIntervals(); |
| 163 | |
Chris Lattner | f7da2c7 | 2006-08-24 22:43:55 +0000 | [diff] [blame^] | 164 | /// CopyCoallesceInMBB - Coallsece copies in the specified MBB, putting |
| 165 | /// copies that cannot yet be coallesced into the "TryAgain" list. |
| 166 | void CopyCoallesceInMBB(MachineBasicBlock *MBB, |
| 167 | std::vector<CopyRec> &TryAgain); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 168 | |
Chris Lattner | f7da2c7 | 2006-08-24 22:43:55 +0000 | [diff] [blame^] | 169 | /// JoinCopy - Attempt to join intervals corresponding to SrcReg/DstReg, |
| 170 | /// which are the src/dst of the copy instruction CopyMI. This returns true |
| 171 | /// if the copy was successfully coallesced away, or if it is never possible |
| 172 | /// to coallesce these this copy, due to register constraints. It returns |
| 173 | /// false if it is not currently possible to coallesce this interval, but |
| 174 | /// it may be possible if other things get coallesced. |
| 175 | bool JoinCopy(MachineInstr *CopyMI, unsigned SrcReg, unsigned DstReg); |
| 176 | |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 177 | /// handleRegisterDef - update intervals for a register def |
| 178 | /// (calls handlePhysicalRegisterDef and |
| 179 | /// handleVirtualRegisterDef) |
| 180 | void handleRegisterDef(MachineBasicBlock* mbb, |
| 181 | MachineBasicBlock::iterator mi, |
Chris Lattner | f38a05d | 2006-01-29 07:59:37 +0000 | [diff] [blame] | 182 | unsigned reg); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 183 | |
| 184 | /// handleVirtualRegisterDef - update intervals for a virtual |
| 185 | /// register def |
| 186 | void handleVirtualRegisterDef(MachineBasicBlock* mbb, |
| 187 | MachineBasicBlock::iterator mi, |
| 188 | LiveInterval& interval); |
| 189 | |
Chris Lattner | f768bba | 2005-03-09 23:05:19 +0000 | [diff] [blame] | 190 | /// handlePhysicalRegisterDef - update intervals for a physical register |
Chris Lattner | f7da2c7 | 2006-08-24 22:43:55 +0000 | [diff] [blame^] | 191 | /// def. |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 192 | void handlePhysicalRegisterDef(MachineBasicBlock* mbb, |
| 193 | MachineBasicBlock::iterator mi, |
Chris Lattner | f768bba | 2005-03-09 23:05:19 +0000 | [diff] [blame] | 194 | LiveInterval& interval, |
Chris Lattner | 5ab6f5f | 2005-09-02 00:20:32 +0000 | [diff] [blame] | 195 | bool isLiveIn = false); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 196 | |
Evan Cheng | 647c15e | 2006-05-12 06:06:34 +0000 | [diff] [blame] | 197 | /// Return true if the two specified registers belong to different |
| 198 | /// register classes. The registers may be either phys or virt regs. |
| 199 | bool differingRegisterClasses(unsigned RegA, unsigned RegB) const; |
| 200 | |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 201 | |
Chris Lattner | f7da2c7 | 2006-08-24 22:43:55 +0000 | [diff] [blame^] | 202 | bool AdjustCopiesBackFrom(LiveInterval &IntA, LiveInterval &IntB, |
| 203 | MachineInstr *CopyMI, unsigned CopyIdx); |
Chris Lattner | 09d3b75 | 2005-10-21 15:49:28 +0000 | [diff] [blame] | 204 | |
Alkis Evlogimenos | 7065157 | 2004-08-04 09:46:56 +0000 | [diff] [blame] | 205 | bool overlapsAliases(const LiveInterval *lhs, |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 206 | const LiveInterval *rhs) const; |
| 207 | |
| 208 | static LiveInterval createInterval(unsigned Reg); |
| 209 | |
| 210 | LiveInterval &getOrCreateInterval(unsigned reg) { |
| 211 | Reg2IntervalMap::iterator I = r2iMap_.find(reg); |
| 212 | if (I == r2iMap_.end()) |
| 213 | I = r2iMap_.insert(I, std::make_pair(reg, createInterval(reg))); |
| 214 | return I->second; |
| 215 | } |
| 216 | |
| 217 | /// rep - returns the representative of this register |
Alkis Evlogimenos | 5d0d1e3 | 2004-09-08 03:01:50 +0000 | [diff] [blame] | 218 | unsigned rep(unsigned Reg) { |
| 219 | unsigned Rep = r2rMap_[Reg]; |
| 220 | if (Rep) |
| 221 | return r2rMap_[Reg] = rep(Rep); |
| 222 | return Reg; |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | void printRegName(unsigned reg) const; |
| 226 | }; |
| 227 | |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 228 | } // End llvm namespace |
| 229 | |
| 230 | #endif |