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