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