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