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 | fb449b9 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 25 | #include "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: |
| 56 | struct InstrSlots |
| 57 | { |
| 58 | enum { |
| 59 | LOAD = 0, |
| 60 | USE = 1, |
| 61 | DEF = 2, |
| 62 | STORE = 3, |
| 63 | NUM = 4, |
| 64 | }; |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 65 | }; |
| 66 | |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 67 | static unsigned getBaseIndex(unsigned index) { |
| 68 | return index - (index % InstrSlots::NUM); |
| 69 | } |
| 70 | static unsigned getBoundaryIndex(unsigned index) { |
| 71 | return getBaseIndex(index + InstrSlots::NUM - 1); |
| 72 | } |
| 73 | static unsigned getLoadIndex(unsigned index) { |
| 74 | return getBaseIndex(index) + InstrSlots::LOAD; |
| 75 | } |
| 76 | static unsigned getUseIndex(unsigned index) { |
| 77 | return getBaseIndex(index) + InstrSlots::USE; |
| 78 | } |
| 79 | static unsigned getDefIndex(unsigned index) { |
| 80 | return getBaseIndex(index) + InstrSlots::DEF; |
| 81 | } |
| 82 | static unsigned getStoreIndex(unsigned index) { |
| 83 | return getBaseIndex(index) + InstrSlots::STORE; |
| 84 | } |
| 85 | |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 86 | typedef Reg2IntervalMap::iterator iterator; |
Chris Lattner | 70ca358 | 2004-09-30 15:59:17 +0000 | [diff] [blame] | 87 | typedef Reg2IntervalMap::const_iterator const_iterator; |
| 88 | const_iterator begin() const { return r2iMap_.begin(); } |
| 89 | const_iterator end() const { return r2iMap_.end(); } |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 90 | iterator begin() { return r2iMap_.begin(); } |
| 91 | iterator end() { return r2iMap_.end(); } |
| 92 | unsigned getNumIntervals() const { return r2iMap_.size(); } |
| 93 | |
| 94 | LiveInterval &getInterval(unsigned reg) { |
| 95 | Reg2IntervalMap::iterator I = r2iMap_.find(reg); |
| 96 | assert(I != r2iMap_.end() && "Interval does not exist for register"); |
| 97 | return I->second; |
| 98 | } |
| 99 | |
| 100 | const LiveInterval &getInterval(unsigned reg) const { |
| 101 | Reg2IntervalMap::const_iterator I = r2iMap_.find(reg); |
| 102 | assert(I != r2iMap_.end() && "Interval does not exist for register"); |
| 103 | return I->second; |
| 104 | } |
| 105 | |
| 106 | /// getInstructionIndex - returns the base index of instr |
| 107 | unsigned getInstructionIndex(MachineInstr* instr) const { |
| 108 | Mi2IndexMap::const_iterator it = mi2iMap_.find(instr); |
| 109 | assert(it != mi2iMap_.end() && "Invalid instruction!"); |
| 110 | return it->second; |
| 111 | } |
| 112 | |
| 113 | /// getInstructionFromIndex - given an index in any slot of an |
| 114 | /// instruction return a pointer the instruction |
| 115 | MachineInstr* getInstructionFromIndex(unsigned index) const { |
| 116 | index /= InstrSlots::NUM; // convert index to vector index |
| 117 | assert(index < i2miMap_.size() && |
| 118 | "index does not correspond to an instruction"); |
| 119 | return i2miMap_[index]; |
| 120 | } |
| 121 | |
| 122 | std::vector<LiveInterval*> addIntervalsForSpills(const LiveInterval& i, |
| 123 | VirtRegMap& vrm, |
| 124 | int slot); |
| 125 | |
| 126 | virtual void getAnalysisUsage(AnalysisUsage &AU) const; |
| 127 | virtual void releaseMemory(); |
| 128 | |
| 129 | /// runOnMachineFunction - pass entry point |
| 130 | virtual bool runOnMachineFunction(MachineFunction&); |
| 131 | |
Chris Lattner | 70ca358 | 2004-09-30 15:59:17 +0000 | [diff] [blame] | 132 | /// print - Implement the dump method. |
Reid Spencer | ce9653c | 2004-12-07 04:03:45 +0000 | [diff] [blame] | 133 | virtual void print(std::ostream &O, const Module* = 0) const; |
Chris Lattner | 70ca358 | 2004-09-30 15:59:17 +0000 | [diff] [blame] | 134 | |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 135 | private: |
| 136 | /// computeIntervals - compute live intervals |
| 137 | void computeIntervals(); |
| 138 | |
| 139 | /// joinIntervals - join compatible live intervals |
| 140 | void joinIntervals(); |
| 141 | |
| 142 | /// joinIntervalsInMachineBB - Join intervals based on move |
| 143 | /// instructions in the specified basic block. |
| 144 | void joinIntervalsInMachineBB(MachineBasicBlock *MBB); |
| 145 | |
| 146 | /// handleRegisterDef - update intervals for a register def |
| 147 | /// (calls handlePhysicalRegisterDef and |
| 148 | /// handleVirtualRegisterDef) |
| 149 | void handleRegisterDef(MachineBasicBlock* mbb, |
| 150 | MachineBasicBlock::iterator mi, |
| 151 | unsigned reg); |
| 152 | |
| 153 | /// handleVirtualRegisterDef - update intervals for a virtual |
| 154 | /// register def |
| 155 | void handleVirtualRegisterDef(MachineBasicBlock* mbb, |
| 156 | MachineBasicBlock::iterator mi, |
| 157 | LiveInterval& interval); |
| 158 | |
Chris Lattner | f768bba | 2005-03-09 23:05:19 +0000 | [diff] [blame] | 159 | /// handlePhysicalRegisterDef - update intervals for a physical register |
| 160 | /// def. If the defining instruction is a move instruction, SrcReg will be |
| 161 | /// the input register, and DestReg will be the result. Note that Interval |
| 162 | /// may not match DestReg (it might be an alias instead). |
| 163 | /// |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 164 | void handlePhysicalRegisterDef(MachineBasicBlock* mbb, |
| 165 | MachineBasicBlock::iterator mi, |
Chris Lattner | f768bba | 2005-03-09 23:05:19 +0000 | [diff] [blame] | 166 | LiveInterval& interval, |
| 167 | unsigned SrcReg, unsigned DestReg); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 168 | |
| 169 | /// Return true if the two specified registers belong to different |
| 170 | /// register classes. The registers may be either phys or virt regs. |
| 171 | bool differingRegisterClasses(unsigned RegA, unsigned RegB) const; |
| 172 | |
Alkis Evlogimenos | 7065157 | 2004-08-04 09:46:56 +0000 | [diff] [blame] | 173 | bool overlapsAliases(const LiveInterval *lhs, |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 174 | const LiveInterval *rhs) const; |
| 175 | |
| 176 | static LiveInterval createInterval(unsigned Reg); |
| 177 | |
| 178 | LiveInterval &getOrCreateInterval(unsigned reg) { |
| 179 | Reg2IntervalMap::iterator I = r2iMap_.find(reg); |
| 180 | if (I == r2iMap_.end()) |
| 181 | I = r2iMap_.insert(I, std::make_pair(reg, createInterval(reg))); |
| 182 | return I->second; |
| 183 | } |
| 184 | |
| 185 | /// rep - returns the representative of this register |
Alkis Evlogimenos | 5d0d1e3 | 2004-09-08 03:01:50 +0000 | [diff] [blame] | 186 | unsigned rep(unsigned Reg) { |
| 187 | unsigned Rep = r2rMap_[Reg]; |
| 188 | if (Rep) |
| 189 | return r2rMap_[Reg] = rep(Rep); |
| 190 | return Reg; |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | void printRegName(unsigned reg) const; |
| 194 | }; |
| 195 | |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 196 | } // End llvm namespace |
| 197 | |
| 198 | #endif |