Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 1 | //===-- llvm/CodeGen/LiveInterval.h - Live Interval Analysis ----*- C++ -*-===// |
| 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 | |
| 20 | #ifndef LLVM_CODEGEN_LIVEINTERVALS_H |
| 21 | #define LLVM_CODEGEN_LIVEINTERVALS_H |
| 22 | |
| 23 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Alkis Evlogimenos | f5f1689 | 2004-01-16 16:06:59 +0000 | [diff] [blame] | 24 | #include <list> |
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 | 418da55 | 2004-06-21 13:10:56 +0000 | [diff] [blame] | 32 | struct LiveInterval { |
Alkis Evlogimenos | 6924063 | 2004-05-30 07:46:27 +0000 | [diff] [blame] | 33 | typedef std::pair<unsigned, unsigned> Range; |
| 34 | typedef std::vector<Range> Ranges; |
| 35 | unsigned reg; // the register of this interval |
| 36 | float weight; // weight of this interval: |
| 37 | // (number of uses *10^loopDepth) |
| 38 | Ranges ranges; // the ranges in which this register is live |
| 39 | |
Chris Lattner | 418da55 | 2004-06-21 13:10:56 +0000 | [diff] [blame] | 40 | explicit LiveInterval(unsigned r); |
Alkis Evlogimenos | 6924063 | 2004-05-30 07:46:27 +0000 | [diff] [blame] | 41 | |
| 42 | bool empty() const { return ranges.empty(); } |
| 43 | |
| 44 | bool spilled() const; |
| 45 | |
| 46 | unsigned start() const { |
| 47 | assert(!empty() && "empty interval for register"); |
| 48 | return ranges.front().first; |
| 49 | } |
| 50 | |
| 51 | unsigned end() const { |
| 52 | assert(!empty() && "empty interval for register"); |
| 53 | return ranges.back().second; |
| 54 | } |
| 55 | |
| 56 | bool expiredAt(unsigned index) const { |
| 57 | return end() <= (index + 1); |
| 58 | } |
| 59 | |
| 60 | bool liveAt(unsigned index) const; |
| 61 | |
Chris Lattner | 418da55 | 2004-06-21 13:10:56 +0000 | [diff] [blame] | 62 | bool overlaps(const LiveInterval& other) const; |
Alkis Evlogimenos | 6924063 | 2004-05-30 07:46:27 +0000 | [diff] [blame] | 63 | |
| 64 | void addRange(unsigned start, unsigned end); |
| 65 | |
Chris Lattner | 418da55 | 2004-06-21 13:10:56 +0000 | [diff] [blame] | 66 | void join(const LiveInterval& other); |
Alkis Evlogimenos | 6924063 | 2004-05-30 07:46:27 +0000 | [diff] [blame] | 67 | |
Chris Lattner | 418da55 | 2004-06-21 13:10:56 +0000 | [diff] [blame] | 68 | bool operator<(const LiveInterval& other) const { |
Alkis Evlogimenos | 6924063 | 2004-05-30 07:46:27 +0000 | [diff] [blame] | 69 | return start() < other.start(); |
| 70 | } |
| 71 | |
Chris Lattner | 418da55 | 2004-06-21 13:10:56 +0000 | [diff] [blame] | 72 | bool operator==(const LiveInterval& other) const { |
Alkis Evlogimenos | 6924063 | 2004-05-30 07:46:27 +0000 | [diff] [blame] | 73 | return reg == other.reg; |
| 74 | } |
| 75 | |
| 76 | private: |
| 77 | Ranges::iterator mergeRangesForward(Ranges::iterator it); |
| 78 | Ranges::iterator mergeRangesBackward(Ranges::iterator it); |
| 79 | }; |
| 80 | |
Chris Lattner | 418da55 | 2004-06-21 13:10:56 +0000 | [diff] [blame] | 81 | std::ostream& operator<<(std::ostream& os, const LiveInterval& li); |
Alkis Evlogimenos | 6924063 | 2004-05-30 07:46:27 +0000 | [diff] [blame] | 82 | |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 83 | class LiveIntervals : public MachineFunctionPass |
| 84 | { |
| 85 | public: |
Chris Lattner | 418da55 | 2004-06-21 13:10:56 +0000 | [diff] [blame] | 86 | typedef std::list<LiveInterval> Intervals; |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 87 | |
| 88 | private: |
| 89 | MachineFunction* mf_; |
| 90 | const TargetMachine* tm_; |
| 91 | const MRegisterInfo* mri_; |
| 92 | MachineBasicBlock* currentMbb_; |
| 93 | MachineBasicBlock::iterator currentInstr_; |
| 94 | LiveVariables* lv_; |
| 95 | |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 96 | typedef std::map<MachineInstr*, unsigned> Mi2IndexMap; |
| 97 | Mi2IndexMap mi2iMap_; |
| 98 | |
Alkis Evlogimenos | 843b160 | 2004-02-15 10:24:21 +0000 | [diff] [blame] | 99 | typedef std::vector<MachineInstr*> Index2MiMap; |
| 100 | Index2MiMap i2miMap_; |
| 101 | |
Alkis Evlogimenos | f5f1689 | 2004-01-16 16:06:59 +0000 | [diff] [blame] | 102 | typedef std::map<unsigned, Intervals::iterator> Reg2IntervalMap; |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 103 | Reg2IntervalMap r2iMap_; |
| 104 | |
Alkis Evlogimenos | 52f8f56 | 2004-02-18 23:14:52 +0000 | [diff] [blame] | 105 | typedef std::map<unsigned, unsigned> Reg2RegMap; |
Alkis Evlogimenos | e88280a | 2004-01-22 23:08:45 +0000 | [diff] [blame] | 106 | Reg2RegMap r2rMap_; |
| 107 | |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 108 | Intervals intervals_; |
| 109 | |
| 110 | public: |
Alkis Evlogimenos | 39a0d5c | 2004-02-20 06:15:40 +0000 | [diff] [blame] | 111 | struct InstrSlots |
| 112 | { |
| 113 | enum { |
| 114 | LOAD = 0, |
| 115 | USE = 1, |
| 116 | DEF = 2, |
| 117 | STORE = 3, |
| 118 | NUM = 4, |
| 119 | }; |
| 120 | }; |
| 121 | |
| 122 | static unsigned getBaseIndex(unsigned index) { |
Alkis Evlogimenos | 7200c6b | 2004-02-22 04:05:13 +0000 | [diff] [blame] | 123 | return index - (index % InstrSlots::NUM); |
| 124 | } |
| 125 | static unsigned getBoundaryIndex(unsigned index) { |
| 126 | return getBaseIndex(index + InstrSlots::NUM - 1); |
Alkis Evlogimenos | 39a0d5c | 2004-02-20 06:15:40 +0000 | [diff] [blame] | 127 | } |
| 128 | static unsigned getLoadIndex(unsigned index) { |
| 129 | return getBaseIndex(index) + InstrSlots::LOAD; |
| 130 | } |
| 131 | static unsigned getUseIndex(unsigned index) { |
| 132 | return getBaseIndex(index) + InstrSlots::USE; |
| 133 | } |
| 134 | static unsigned getDefIndex(unsigned index) { |
| 135 | return getBaseIndex(index) + InstrSlots::DEF; |
| 136 | } |
| 137 | static unsigned getStoreIndex(unsigned index) { |
| 138 | return getBaseIndex(index) + InstrSlots::STORE; |
| 139 | } |
| 140 | |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 141 | virtual void getAnalysisUsage(AnalysisUsage &AU) const; |
Alkis Evlogimenos | 08cec00 | 2004-01-31 19:59:32 +0000 | [diff] [blame] | 142 | virtual void releaseMemory(); |
| 143 | |
| 144 | /// runOnMachineFunction - pass entry point |
| 145 | virtual bool runOnMachineFunction(MachineFunction&); |
Alkis Evlogimenos | e88280a | 2004-01-22 23:08:45 +0000 | [diff] [blame] | 146 | |
Chris Lattner | 418da55 | 2004-06-21 13:10:56 +0000 | [diff] [blame] | 147 | LiveInterval& getInterval(unsigned reg) { |
Alkis Evlogimenos | 52f8f56 | 2004-02-18 23:14:52 +0000 | [diff] [blame] | 148 | assert(r2iMap_.count(reg)&& "Interval does not exist for register"); |
| 149 | return *r2iMap_.find(reg)->second; |
| 150 | } |
| 151 | |
Alkis Evlogimenos | 39a0d5c | 2004-02-20 06:15:40 +0000 | [diff] [blame] | 152 | /// getInstructionIndex - returns the base index of instr |
Alkis Evlogimenos | 843b160 | 2004-02-15 10:24:21 +0000 | [diff] [blame] | 153 | unsigned getInstructionIndex(MachineInstr* instr) const; |
| 154 | |
Alkis Evlogimenos | 39a0d5c | 2004-02-20 06:15:40 +0000 | [diff] [blame] | 155 | /// getInstructionFromIndex - given an index in any slot of an |
| 156 | /// instruction return a pointer the instruction |
Alkis Evlogimenos | 843b160 | 2004-02-15 10:24:21 +0000 | [diff] [blame] | 157 | MachineInstr* getInstructionFromIndex(unsigned index) const; |
| 158 | |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 159 | Intervals& getIntervals() { return intervals_; } |
Alkis Evlogimenos | e88280a | 2004-01-22 23:08:45 +0000 | [diff] [blame] | 160 | |
Chris Lattner | 418da55 | 2004-06-21 13:10:56 +0000 | [diff] [blame] | 161 | std::vector<LiveInterval*> addIntervalsForSpills(const LiveInterval& i, |
| 162 | VirtRegMap& vrm, |
| 163 | int slot); |
Alkis Evlogimenos | e88280a | 2004-01-22 23:08:45 +0000 | [diff] [blame] | 164 | |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 165 | private: |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 166 | /// computeIntervals - compute live intervals |
| 167 | void computeIntervals(); |
| 168 | |
Alkis Evlogimenos | e88280a | 2004-01-22 23:08:45 +0000 | [diff] [blame] | 169 | /// joinIntervals - join compatible live intervals |
| 170 | void joinIntervals(); |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 171 | |
| 172 | /// handleRegisterDef - update intervals for a register def |
| 173 | /// (calls handlePhysicalRegisterDef and |
| 174 | /// handleVirtualRegisterDef) |
| 175 | void handleRegisterDef(MachineBasicBlock* mbb, |
| 176 | MachineBasicBlock::iterator mi, |
| 177 | unsigned reg); |
| 178 | |
| 179 | /// handleVirtualRegisterDef - update intervals for a virtual |
| 180 | /// register def |
| 181 | void handleVirtualRegisterDef(MachineBasicBlock* mbb, |
| 182 | MachineBasicBlock::iterator mi, |
Chris Lattner | 418da55 | 2004-06-21 13:10:56 +0000 | [diff] [blame] | 183 | LiveInterval& interval); |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 184 | |
| 185 | /// handlePhysicalRegisterDef - update intervals for a |
| 186 | /// physical register def |
| 187 | void handlePhysicalRegisterDef(MachineBasicBlock* mbb, |
| 188 | MachineBasicBlock::iterator mi, |
Chris Lattner | 418da55 | 2004-06-21 13:10:56 +0000 | [diff] [blame] | 189 | LiveInterval& interval); |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 190 | |
Chris Lattner | 418da55 | 2004-06-21 13:10:56 +0000 | [diff] [blame] | 191 | bool overlapsAliases(const LiveInterval& lhs, |
| 192 | const LiveInterval& rhs) const; |
Alkis Evlogimenos | 79b0c3f | 2004-01-23 13:37:51 +0000 | [diff] [blame] | 193 | |
Alkis Evlogimenos | 9a8b490 | 2004-04-09 18:07:57 +0000 | [diff] [blame] | 194 | |
Chris Lattner | 418da55 | 2004-06-21 13:10:56 +0000 | [diff] [blame] | 195 | LiveInterval& getOrCreateInterval(unsigned reg); |
Alkis Evlogimenos | 9a8b490 | 2004-04-09 18:07:57 +0000 | [diff] [blame] | 196 | |
Alkis Evlogimenos | 843b160 | 2004-02-15 10:24:21 +0000 | [diff] [blame] | 197 | /// rep - returns the representative of this register |
| 198 | unsigned rep(unsigned reg); |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 199 | |
| 200 | void printRegName(unsigned reg) const; |
| 201 | }; |
| 202 | |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 203 | } // End llvm namespace |
| 204 | |
| 205 | #endif |