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 | |
| 23 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Chris Lattner | fb449b9 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 24 | #include "LiveInterval.h" |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 25 | |
| 26 | namespace llvm { |
| 27 | |
| 28 | class LiveVariables; |
| 29 | class MRegisterInfo; |
Alkis Evlogimenos | 5f37502 | 2004-03-01 20:05:10 +0000 | [diff] [blame] | 30 | class VirtRegMap; |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 31 | |
Chris Lattner | 4df98e5 | 2004-07-24 03:32:06 +0000 | [diff] [blame^] | 32 | class LiveIntervals : public MachineFunctionPass { |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 33 | MachineFunction* mf_; |
| 34 | const TargetMachine* tm_; |
| 35 | const MRegisterInfo* mri_; |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 36 | LiveVariables* lv_; |
| 37 | |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 38 | typedef std::map<MachineInstr*, unsigned> Mi2IndexMap; |
| 39 | Mi2IndexMap mi2iMap_; |
| 40 | |
Alkis Evlogimenos | 843b160 | 2004-02-15 10:24:21 +0000 | [diff] [blame] | 41 | typedef std::vector<MachineInstr*> Index2MiMap; |
| 42 | Index2MiMap i2miMap_; |
| 43 | |
Chris Lattner | 4df98e5 | 2004-07-24 03:32:06 +0000 | [diff] [blame^] | 44 | /// r2iMap_ - This map OWNS the interval pointed to by the map. When |
| 45 | /// this map is destroyed or when entries are modified, this intervals |
| 46 | /// should be destroyed or modified as well. |
| 47 | std::map<unsigned, LiveInterval*> r2iMap_; |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 48 | |
Alkis Evlogimenos | 52f8f56 | 2004-02-18 23:14:52 +0000 | [diff] [blame] | 49 | typedef std::map<unsigned, unsigned> Reg2RegMap; |
Alkis Evlogimenos | e88280a | 2004-01-22 23:08:45 +0000 | [diff] [blame] | 50 | Reg2RegMap r2rMap_; |
| 51 | |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 52 | public: |
Alkis Evlogimenos | 39a0d5c | 2004-02-20 06:15:40 +0000 | [diff] [blame] | 53 | struct InstrSlots |
| 54 | { |
| 55 | enum { |
| 56 | LOAD = 0, |
| 57 | USE = 1, |
| 58 | DEF = 2, |
| 59 | STORE = 3, |
| 60 | NUM = 4, |
| 61 | }; |
| 62 | }; |
| 63 | |
| 64 | static unsigned getBaseIndex(unsigned index) { |
Alkis Evlogimenos | 7200c6b | 2004-02-22 04:05:13 +0000 | [diff] [blame] | 65 | return index - (index % InstrSlots::NUM); |
| 66 | } |
| 67 | static unsigned getBoundaryIndex(unsigned index) { |
| 68 | return getBaseIndex(index + InstrSlots::NUM - 1); |
Alkis Evlogimenos | 39a0d5c | 2004-02-20 06:15:40 +0000 | [diff] [blame] | 69 | } |
| 70 | static unsigned getLoadIndex(unsigned index) { |
| 71 | return getBaseIndex(index) + InstrSlots::LOAD; |
| 72 | } |
| 73 | static unsigned getUseIndex(unsigned index) { |
| 74 | return getBaseIndex(index) + InstrSlots::USE; |
| 75 | } |
| 76 | static unsigned getDefIndex(unsigned index) { |
| 77 | return getBaseIndex(index) + InstrSlots::DEF; |
| 78 | } |
| 79 | static unsigned getStoreIndex(unsigned index) { |
| 80 | return getBaseIndex(index) + InstrSlots::STORE; |
| 81 | } |
| 82 | |
Chris Lattner | 4df98e5 | 2004-07-24 03:32:06 +0000 | [diff] [blame^] | 83 | typedef std::map<unsigned, LiveInterval*>::const_iterator iterator; |
| 84 | iterator begin() const { return r2iMap_.begin(); } |
| 85 | iterator end() const { return r2iMap_.end(); } |
| 86 | unsigned getNumIntervals() const { return r2iMap_.size(); } |
Alkis Evlogimenos | 08cec00 | 2004-01-31 19:59:32 +0000 | [diff] [blame] | 87 | |
Chris Lattner | 4df98e5 | 2004-07-24 03:32:06 +0000 | [diff] [blame^] | 88 | LiveInterval &getInterval(unsigned reg) const { |
| 89 | std::map<unsigned, LiveInterval*>::const_iterator I = |
| 90 | r2iMap_.find(reg); |
| 91 | assert(I != r2iMap_.end() && "Interval does not exist for register"); |
Chris Lattner | 0f4c076 | 2004-07-24 02:53:43 +0000 | [diff] [blame] | 92 | return *I->second; |
Alkis Evlogimenos | 52f8f56 | 2004-02-18 23:14:52 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Alkis Evlogimenos | 39a0d5c | 2004-02-20 06:15:40 +0000 | [diff] [blame] | 95 | /// getInstructionIndex - returns the base index of instr |
Chris Lattner | f35fef7 | 2004-07-23 21:24:19 +0000 | [diff] [blame] | 96 | unsigned getInstructionIndex(MachineInstr* instr) const { |
| 97 | Mi2IndexMap::const_iterator it = mi2iMap_.find(instr); |
| 98 | assert(it != mi2iMap_.end() && "Invalid instruction!"); |
| 99 | return it->second; |
| 100 | } |
Alkis Evlogimenos | 843b160 | 2004-02-15 10:24:21 +0000 | [diff] [blame] | 101 | |
Alkis Evlogimenos | 39a0d5c | 2004-02-20 06:15:40 +0000 | [diff] [blame] | 102 | /// getInstructionFromIndex - given an index in any slot of an |
| 103 | /// instruction return a pointer the instruction |
Chris Lattner | f35fef7 | 2004-07-23 21:24:19 +0000 | [diff] [blame] | 104 | MachineInstr* getInstructionFromIndex(unsigned index) const { |
| 105 | index /= InstrSlots::NUM; // convert index to vector index |
| 106 | assert(index < i2miMap_.size() && |
| 107 | "index does not correspond to an instruction"); |
| 108 | return i2miMap_[index]; |
| 109 | } |
Alkis Evlogimenos | 843b160 | 2004-02-15 10:24:21 +0000 | [diff] [blame] | 110 | |
Chris Lattner | 418da55 | 2004-06-21 13:10:56 +0000 | [diff] [blame] | 111 | std::vector<LiveInterval*> addIntervalsForSpills(const LiveInterval& i, |
| 112 | VirtRegMap& vrm, |
| 113 | int slot); |
Alkis Evlogimenos | e88280a | 2004-01-22 23:08:45 +0000 | [diff] [blame] | 114 | |
Chris Lattner | 4df98e5 | 2004-07-24 03:32:06 +0000 | [diff] [blame^] | 115 | virtual void getAnalysisUsage(AnalysisUsage &AU) const; |
| 116 | virtual void releaseMemory(); |
| 117 | |
| 118 | /// runOnMachineFunction - pass entry point |
| 119 | virtual bool runOnMachineFunction(MachineFunction&); |
| 120 | |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 121 | private: |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 122 | /// computeIntervals - compute live intervals |
| 123 | void computeIntervals(); |
| 124 | |
Alkis Evlogimenos | e88280a | 2004-01-22 23:08:45 +0000 | [diff] [blame] | 125 | /// joinIntervals - join compatible live intervals |
| 126 | void joinIntervals(); |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 127 | |
Chris Lattner | 1c5c044 | 2004-07-19 14:08:10 +0000 | [diff] [blame] | 128 | /// joinIntervalsInMachineBB - Join intervals based on move |
| 129 | /// instructions in the specified basic block. |
| 130 | void joinIntervalsInMachineBB(MachineBasicBlock *MBB); |
| 131 | |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 132 | /// handleRegisterDef - update intervals for a register def |
| 133 | /// (calls handlePhysicalRegisterDef and |
| 134 | /// handleVirtualRegisterDef) |
| 135 | void handleRegisterDef(MachineBasicBlock* mbb, |
| 136 | MachineBasicBlock::iterator mi, |
| 137 | unsigned reg); |
| 138 | |
| 139 | /// handleVirtualRegisterDef - update intervals for a virtual |
| 140 | /// register def |
| 141 | void handleVirtualRegisterDef(MachineBasicBlock* mbb, |
| 142 | MachineBasicBlock::iterator mi, |
Chris Lattner | 418da55 | 2004-06-21 13:10:56 +0000 | [diff] [blame] | 143 | LiveInterval& interval); |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 144 | |
| 145 | /// handlePhysicalRegisterDef - update intervals for a |
| 146 | /// physical register def |
| 147 | void handlePhysicalRegisterDef(MachineBasicBlock* mbb, |
| 148 | MachineBasicBlock::iterator mi, |
Chris Lattner | 418da55 | 2004-06-21 13:10:56 +0000 | [diff] [blame] | 149 | LiveInterval& interval); |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 150 | |
Chris Lattner | 0f4c076 | 2004-07-24 02:53:43 +0000 | [diff] [blame] | 151 | /// Return true if the two specified registers belong to different |
| 152 | /// register classes. The registers may be either phys or virt regs. |
| 153 | bool differingRegisterClasses(unsigned RegA, unsigned RegB) const; |
| 154 | |
| 155 | bool overlapsAliases(const LiveInterval *lhs, |
| 156 | const LiveInterval *rhs) const; |
Alkis Evlogimenos | 79b0c3f | 2004-01-23 13:37:51 +0000 | [diff] [blame] | 157 | |
Chris Lattner | 4df98e5 | 2004-07-24 03:32:06 +0000 | [diff] [blame^] | 158 | LiveInterval *createInterval(unsigned Reg) const; |
Alkis Evlogimenos | 9a8b490 | 2004-04-09 18:07:57 +0000 | [diff] [blame] | 159 | |
Chris Lattner | 4df98e5 | 2004-07-24 03:32:06 +0000 | [diff] [blame^] | 160 | LiveInterval &getOrCreateInterval(unsigned reg) { |
| 161 | LiveInterval *&LI = r2iMap_[reg]; |
| 162 | if (LI == 0) |
| 163 | LI = createInterval(reg); |
| 164 | return *LI; |
| 165 | } |
Alkis Evlogimenos | 9a8b490 | 2004-04-09 18:07:57 +0000 | [diff] [blame] | 166 | |
Alkis Evlogimenos | 843b160 | 2004-02-15 10:24:21 +0000 | [diff] [blame] | 167 | /// rep - returns the representative of this register |
Chris Lattner | f35fef7 | 2004-07-23 21:24:19 +0000 | [diff] [blame] | 168 | unsigned rep(unsigned reg) { |
| 169 | Reg2RegMap::iterator it = r2rMap_.find(reg); |
| 170 | if (it != r2rMap_.end()) |
| 171 | return it->second = rep(it->second); |
| 172 | return reg; |
| 173 | } |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 174 | |
| 175 | void printRegName(unsigned reg) const; |
| 176 | }; |
| 177 | |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 178 | } // End llvm namespace |
| 179 | |
| 180 | #endif |