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