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" |
Chris Lattner | fb449b9 | 2004-07-23 17:49:16 +0000 | [diff] [blame^] | 24 | #include "LiveInterval.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: |
Chris Lattner | 418da55 | 2004-06-21 13:10:56 +0000 | [diff] [blame] | 36 | typedef std::list<LiveInterval> Intervals; |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 37 | |
| 38 | private: |
| 39 | MachineFunction* mf_; |
| 40 | const TargetMachine* tm_; |
| 41 | const MRegisterInfo* mri_; |
| 42 | MachineBasicBlock* currentMbb_; |
| 43 | MachineBasicBlock::iterator currentInstr_; |
| 44 | LiveVariables* lv_; |
| 45 | |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 46 | typedef std::map<MachineInstr*, unsigned> Mi2IndexMap; |
| 47 | Mi2IndexMap mi2iMap_; |
| 48 | |
Alkis Evlogimenos | 843b160 | 2004-02-15 10:24:21 +0000 | [diff] [blame] | 49 | typedef std::vector<MachineInstr*> Index2MiMap; |
| 50 | Index2MiMap i2miMap_; |
| 51 | |
Alkis Evlogimenos | f5f1689 | 2004-01-16 16:06:59 +0000 | [diff] [blame] | 52 | typedef std::map<unsigned, Intervals::iterator> Reg2IntervalMap; |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 53 | Reg2IntervalMap r2iMap_; |
| 54 | |
Alkis Evlogimenos | 52f8f56 | 2004-02-18 23:14:52 +0000 | [diff] [blame] | 55 | typedef std::map<unsigned, unsigned> Reg2RegMap; |
Alkis Evlogimenos | e88280a | 2004-01-22 23:08:45 +0000 | [diff] [blame] | 56 | Reg2RegMap r2rMap_; |
| 57 | |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 58 | Intervals intervals_; |
| 59 | |
| 60 | public: |
Alkis Evlogimenos | 39a0d5c | 2004-02-20 06:15:40 +0000 | [diff] [blame] | 61 | struct InstrSlots |
| 62 | { |
| 63 | enum { |
| 64 | LOAD = 0, |
| 65 | USE = 1, |
| 66 | DEF = 2, |
| 67 | STORE = 3, |
| 68 | NUM = 4, |
| 69 | }; |
| 70 | }; |
| 71 | |
| 72 | static unsigned getBaseIndex(unsigned index) { |
Alkis Evlogimenos | 7200c6b | 2004-02-22 04:05:13 +0000 | [diff] [blame] | 73 | return index - (index % InstrSlots::NUM); |
| 74 | } |
| 75 | static unsigned getBoundaryIndex(unsigned index) { |
| 76 | return getBaseIndex(index + InstrSlots::NUM - 1); |
Alkis Evlogimenos | 39a0d5c | 2004-02-20 06:15:40 +0000 | [diff] [blame] | 77 | } |
| 78 | static unsigned getLoadIndex(unsigned index) { |
| 79 | return getBaseIndex(index) + InstrSlots::LOAD; |
| 80 | } |
| 81 | static unsigned getUseIndex(unsigned index) { |
| 82 | return getBaseIndex(index) + InstrSlots::USE; |
| 83 | } |
| 84 | static unsigned getDefIndex(unsigned index) { |
| 85 | return getBaseIndex(index) + InstrSlots::DEF; |
| 86 | } |
| 87 | static unsigned getStoreIndex(unsigned index) { |
| 88 | return getBaseIndex(index) + InstrSlots::STORE; |
| 89 | } |
| 90 | |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 91 | virtual void getAnalysisUsage(AnalysisUsage &AU) const; |
Alkis Evlogimenos | 08cec00 | 2004-01-31 19:59:32 +0000 | [diff] [blame] | 92 | virtual void releaseMemory(); |
| 93 | |
| 94 | /// runOnMachineFunction - pass entry point |
| 95 | virtual bool runOnMachineFunction(MachineFunction&); |
Alkis Evlogimenos | e88280a | 2004-01-22 23:08:45 +0000 | [diff] [blame] | 96 | |
Chris Lattner | 418da55 | 2004-06-21 13:10:56 +0000 | [diff] [blame] | 97 | LiveInterval& getInterval(unsigned reg) { |
Alkis Evlogimenos | 52f8f56 | 2004-02-18 23:14:52 +0000 | [diff] [blame] | 98 | assert(r2iMap_.count(reg)&& "Interval does not exist for register"); |
| 99 | return *r2iMap_.find(reg)->second; |
| 100 | } |
| 101 | |
Alkis Evlogimenos | 39a0d5c | 2004-02-20 06:15:40 +0000 | [diff] [blame] | 102 | /// getInstructionIndex - returns the base index of instr |
Alkis Evlogimenos | 843b160 | 2004-02-15 10:24:21 +0000 | [diff] [blame] | 103 | unsigned getInstructionIndex(MachineInstr* instr) const; |
| 104 | |
Alkis Evlogimenos | 39a0d5c | 2004-02-20 06:15:40 +0000 | [diff] [blame] | 105 | /// getInstructionFromIndex - given an index in any slot of an |
| 106 | /// instruction return a pointer the instruction |
Alkis Evlogimenos | 843b160 | 2004-02-15 10:24:21 +0000 | [diff] [blame] | 107 | MachineInstr* getInstructionFromIndex(unsigned index) const; |
| 108 | |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 109 | Intervals& getIntervals() { return intervals_; } |
Alkis Evlogimenos | e88280a | 2004-01-22 23:08:45 +0000 | [diff] [blame] | 110 | |
Chris Lattner | 418da55 | 2004-06-21 13:10:56 +0000 | [diff] [blame] | 111 | std::vector<LiveInterval*> addIntervalsForSpills(const LiveInterval& i, |
| 112 | VirtRegMap& vrm, |
| 113 | int slot); |
Alkis Evlogimenos | e88280a | 2004-01-22 23:08:45 +0000 | [diff] [blame] | 114 | |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 115 | private: |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 116 | /// computeIntervals - compute live intervals |
| 117 | void computeIntervals(); |
| 118 | |
Alkis Evlogimenos | e88280a | 2004-01-22 23:08:45 +0000 | [diff] [blame] | 119 | /// joinIntervals - join compatible live intervals |
| 120 | void joinIntervals(); |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 121 | |
Chris Lattner | 1c5c044 | 2004-07-19 14:08:10 +0000 | [diff] [blame] | 122 | /// joinIntervalsInMachineBB - Join intervals based on move |
| 123 | /// instructions in the specified basic block. |
| 124 | void joinIntervalsInMachineBB(MachineBasicBlock *MBB); |
| 125 | |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 126 | /// handleRegisterDef - update intervals for a register def |
| 127 | /// (calls handlePhysicalRegisterDef and |
| 128 | /// handleVirtualRegisterDef) |
| 129 | void handleRegisterDef(MachineBasicBlock* mbb, |
| 130 | MachineBasicBlock::iterator mi, |
| 131 | unsigned reg); |
| 132 | |
| 133 | /// handleVirtualRegisterDef - update intervals for a virtual |
| 134 | /// register def |
| 135 | void handleVirtualRegisterDef(MachineBasicBlock* mbb, |
| 136 | MachineBasicBlock::iterator mi, |
Chris Lattner | 418da55 | 2004-06-21 13:10:56 +0000 | [diff] [blame] | 137 | LiveInterval& interval); |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 138 | |
| 139 | /// handlePhysicalRegisterDef - update intervals for a |
| 140 | /// physical register def |
| 141 | void handlePhysicalRegisterDef(MachineBasicBlock* mbb, |
| 142 | MachineBasicBlock::iterator mi, |
Chris Lattner | 418da55 | 2004-06-21 13:10:56 +0000 | [diff] [blame] | 143 | LiveInterval& interval); |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 144 | |
Chris Lattner | 418da55 | 2004-06-21 13:10:56 +0000 | [diff] [blame] | 145 | bool overlapsAliases(const LiveInterval& lhs, |
| 146 | const LiveInterval& rhs) const; |
Alkis Evlogimenos | 79b0c3f | 2004-01-23 13:37:51 +0000 | [diff] [blame] | 147 | |
Alkis Evlogimenos | 9a8b490 | 2004-04-09 18:07:57 +0000 | [diff] [blame] | 148 | |
Chris Lattner | 418da55 | 2004-06-21 13:10:56 +0000 | [diff] [blame] | 149 | LiveInterval& getOrCreateInterval(unsigned reg); |
Alkis Evlogimenos | 9a8b490 | 2004-04-09 18:07:57 +0000 | [diff] [blame] | 150 | |
Alkis Evlogimenos | 843b160 | 2004-02-15 10:24:21 +0000 | [diff] [blame] | 151 | /// rep - returns the representative of this register |
| 152 | unsigned rep(unsigned reg); |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 153 | |
| 154 | void printRegName(unsigned reg) const; |
| 155 | }; |
| 156 | |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 157 | } // End llvm namespace |
| 158 | |
| 159 | #endif |