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 |
| 12 | // depth-first order) an interval [i, j] is said to be a live interval |
| 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 |
| 16 | // have holes, i.e. an interval might look like [1,20], [50,65], |
| 17 | // [1000,1001] |
| 18 | // |
| 19 | //===----------------------------------------------------------------------===// |
| 20 | |
| 21 | #ifndef LLVM_CODEGEN_LIVEINTERVALS_H |
| 22 | #define LLVM_CODEGEN_LIVEINTERVALS_H |
| 23 | |
| 24 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 25 | #include "llvm/CodeGen/MachineBasicBlock.h" |
| 26 | #include <iostream> |
Alkis Evlogimenos | f5f1689 | 2004-01-16 16:06:59 +0000 | [diff] [blame^] | 27 | #include <list> |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 28 | #include <map> |
| 29 | |
| 30 | namespace llvm { |
| 31 | |
| 32 | class LiveVariables; |
| 33 | class MRegisterInfo; |
| 34 | |
| 35 | class LiveIntervals : public MachineFunctionPass |
| 36 | { |
| 37 | public: |
| 38 | struct Interval { |
| 39 | typedef std::pair<unsigned, unsigned> Range; |
| 40 | typedef std::vector<Range> Ranges; |
| 41 | unsigned reg; // the register of this interval |
Alkis Evlogimenos | 26bfc08 | 2003-12-28 17:58:18 +0000 | [diff] [blame] | 42 | unsigned hint; |
Alkis Evlogimenos | 6b4edba | 2003-12-21 20:19:10 +0000 | [diff] [blame] | 43 | float weight; // weight of this interval (number of uses |
| 44 | // * 10^loopDepth) |
| 45 | Ranges ranges; // the ranges this register is valid |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 46 | |
Alkis Evlogimenos | 169cfd0 | 2003-12-21 05:43:40 +0000 | [diff] [blame] | 47 | Interval(unsigned r); |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 48 | |
| 49 | unsigned start() const { |
| 50 | assert(!ranges.empty() && "empty interval for register"); |
| 51 | return ranges.front().first; |
| 52 | } |
| 53 | |
| 54 | unsigned end() const { |
| 55 | assert(!ranges.empty() && "empty interval for register"); |
| 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 | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 60 | return end() <= index; |
| 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 | |
| 69 | private: |
Alkis Evlogimenos | dd2cc65 | 2003-12-18 08:48:48 +0000 | [diff] [blame] | 70 | void mergeRangesForward(Ranges::iterator it); |
| 71 | |
| 72 | void mergeRangesBackward(Ranges::iterator it); |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | struct StartPointComp { |
| 76 | bool operator()(const Interval& lhs, const Interval& rhs) { |
| 77 | return lhs.ranges.front().first < rhs.ranges.front().first; |
| 78 | } |
| 79 | }; |
| 80 | |
| 81 | struct EndPointComp { |
| 82 | bool operator()(const Interval& lhs, const Interval& rhs) { |
| 83 | return lhs.ranges.back().second < rhs.ranges.back().second; |
| 84 | } |
| 85 | }; |
| 86 | |
Alkis Evlogimenos | f5f1689 | 2004-01-16 16:06:59 +0000 | [diff] [blame^] | 87 | typedef std::list<Interval> Intervals; |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 88 | typedef std::vector<MachineBasicBlock*> MachineBasicBlockPtrs; |
| 89 | |
| 90 | private: |
| 91 | MachineFunction* mf_; |
| 92 | const TargetMachine* tm_; |
| 93 | const MRegisterInfo* mri_; |
| 94 | MachineBasicBlock* currentMbb_; |
| 95 | MachineBasicBlock::iterator currentInstr_; |
| 96 | LiveVariables* lv_; |
| 97 | |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 98 | typedef std::map<unsigned, MachineBasicBlock*> MbbIndex2MbbMap; |
| 99 | MbbIndex2MbbMap mbbi2mbbMap_; |
| 100 | |
| 101 | typedef std::map<MachineInstr*, unsigned> Mi2IndexMap; |
| 102 | Mi2IndexMap mi2iMap_; |
| 103 | |
Alkis Evlogimenos | f5f1689 | 2004-01-16 16:06:59 +0000 | [diff] [blame^] | 104 | typedef std::map<unsigned, Intervals::iterator> Reg2IntervalMap; |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 105 | Reg2IntervalMap r2iMap_; |
| 106 | |
| 107 | Intervals intervals_; |
| 108 | |
| 109 | public: |
| 110 | virtual void getAnalysisUsage(AnalysisUsage &AU) const; |
| 111 | Intervals& getIntervals() { return intervals_; } |
| 112 | MachineBasicBlockPtrs getOrderedMachineBasicBlockPtrs() const { |
| 113 | MachineBasicBlockPtrs result; |
| 114 | for (MbbIndex2MbbMap::const_iterator |
| 115 | it = mbbi2mbbMap_.begin(), itEnd = mbbi2mbbMap_.end(); |
| 116 | it != itEnd; ++it) { |
| 117 | result.push_back(it->second); |
| 118 | } |
| 119 | return result; |
| 120 | } |
| 121 | |
| 122 | private: |
| 123 | /// runOnMachineFunction - pass entry point |
| 124 | bool runOnMachineFunction(MachineFunction&); |
| 125 | |
| 126 | /// computeIntervals - compute live intervals |
| 127 | void computeIntervals(); |
| 128 | |
| 129 | |
| 130 | /// handleRegisterDef - update intervals for a register def |
| 131 | /// (calls handlePhysicalRegisterDef and |
| 132 | /// handleVirtualRegisterDef) |
| 133 | void handleRegisterDef(MachineBasicBlock* mbb, |
| 134 | MachineBasicBlock::iterator mi, |
| 135 | unsigned reg); |
| 136 | |
| 137 | /// handleVirtualRegisterDef - update intervals for a virtual |
| 138 | /// register def |
| 139 | void handleVirtualRegisterDef(MachineBasicBlock* mbb, |
| 140 | MachineBasicBlock::iterator mi, |
| 141 | unsigned reg); |
| 142 | |
| 143 | /// handlePhysicalRegisterDef - update intervals for a |
| 144 | /// physical register def |
| 145 | void handlePhysicalRegisterDef(MachineBasicBlock* mbb, |
| 146 | MachineBasicBlock::iterator mi, |
| 147 | unsigned reg); |
| 148 | |
| 149 | unsigned getInstructionIndex(MachineInstr* instr) const; |
| 150 | |
| 151 | void printRegName(unsigned reg) const; |
| 152 | }; |
| 153 | |
| 154 | inline bool operator==(const LiveIntervals::Interval& lhs, |
| 155 | const LiveIntervals::Interval& rhs) { |
| 156 | return lhs.reg == rhs.reg; |
| 157 | } |
| 158 | |
Alkis Evlogimenos | b27ef24 | 2003-12-05 10:38:28 +0000 | [diff] [blame] | 159 | std::ostream& operator<<(std::ostream& os, |
| 160 | const LiveIntervals::Interval& li); |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 161 | |
| 162 | } // End llvm namespace |
| 163 | |
| 164 | #endif |