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