Chris Lattner | a3b8b5c | 2004-07-23 17:56:30 +0000 | [diff] [blame] | 1 | //===-- LiveIntervalAnalysis.h - Live Interval Analysis ---------*- C++ -*-===// |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 7ed47a1 | 2007-12-29 19:59:42 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 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 |
Dan Gohman | 8131a50 | 2008-03-13 23:04:27 +0000 | [diff] [blame^] | 13 | // instruction with number j' > j such that v is live at j' and there is no |
Chris Lattner | 6b92906 | 2004-07-19 02:13:59 +0000 | [diff] [blame] | 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 | |
Chris Lattner | a3b8b5c | 2004-07-23 17:56:30 +0000 | [diff] [blame] | 20 | #ifndef LLVM_CODEGEN_LIVEINTERVAL_ANALYSIS_H |
| 21 | #define LLVM_CODEGEN_LIVEINTERVAL_ANALYSIS_H |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 22 | |
| 23 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Chris Lattner | 779a651 | 2005-09-21 04:18:25 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/LiveInterval.h" |
Evan Cheng | 61de82d | 2007-02-15 05:59:24 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/BitVector.h" |
Evan Cheng | 20b0abc | 2007-04-17 20:32:26 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/DenseMap.h" |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/SmallPtrSet.h" |
| 28 | #include "llvm/ADT/SmallVector.h" |
Evan Cheng | f3bb2e6 | 2007-09-05 21:46:51 +0000 | [diff] [blame] | 29 | #include "llvm/Support/Allocator.h" |
Hartmut Kaiser | ffb15de | 2007-11-13 23:04:28 +0000 | [diff] [blame] | 30 | #include <cmath> |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 31 | |
| 32 | namespace llvm { |
| 33 | |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 34 | class LiveVariables; |
Evan Cheng | 22f07ff | 2007-12-11 02:09:15 +0000 | [diff] [blame] | 35 | class MachineLoopInfo; |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 36 | class TargetRegisterInfo; |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 37 | class MachineRegisterInfo; |
Chris Lattner | f768bba | 2005-03-09 23:05:19 +0000 | [diff] [blame] | 38 | class TargetInstrInfo; |
Evan Cheng | 20b0abc | 2007-04-17 20:32:26 +0000 | [diff] [blame] | 39 | class TargetRegisterClass; |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 40 | class VirtRegMap; |
Evan Cheng | 4ca980e | 2007-10-17 02:10:22 +0000 | [diff] [blame] | 41 | typedef std::pair<unsigned, MachineBasicBlock*> IdxMBBPair; |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 42 | |
Roman Levenstein | 8dd2528 | 2008-02-18 09:35:30 +0000 | [diff] [blame] | 43 | inline bool operator<(unsigned V, const IdxMBBPair &IM) { |
| 44 | return V < IM.first; |
| 45 | } |
| 46 | |
| 47 | inline bool operator<(const IdxMBBPair &IM, unsigned V) { |
| 48 | return IM.first < V; |
| 49 | } |
| 50 | |
| 51 | struct Idx2MBBCompare { |
| 52 | bool operator()(const IdxMBBPair &LHS, const IdxMBBPair &RHS) const { |
| 53 | return LHS.first < RHS.first; |
| 54 | } |
| 55 | }; |
| 56 | |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 57 | class LiveIntervals : public MachineFunctionPass { |
| 58 | MachineFunction* mf_; |
Evan Cheng | d70dbb5 | 2008-02-22 09:24:50 +0000 | [diff] [blame] | 59 | MachineRegisterInfo* mri_; |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 60 | const TargetMachine* tm_; |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 61 | const TargetRegisterInfo* tri_; |
Chris Lattner | f768bba | 2005-03-09 23:05:19 +0000 | [diff] [blame] | 62 | const TargetInstrInfo* tii_; |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 63 | LiveVariables* lv_; |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 64 | |
Evan Cheng | f3bb2e6 | 2007-09-05 21:46:51 +0000 | [diff] [blame] | 65 | /// Special pool allocator for VNInfo's (LiveInterval val#). |
| 66 | /// |
| 67 | BumpPtrAllocator VNInfoAllocator; |
| 68 | |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 69 | /// MBB2IdxMap - The indexes of the first and last instructions in the |
| 70 | /// specified basic block. |
| 71 | std::vector<std::pair<unsigned, unsigned> > MBB2IdxMap; |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 72 | |
Evan Cheng | 4ca980e | 2007-10-17 02:10:22 +0000 | [diff] [blame] | 73 | /// Idx2MBBMap - Sorted list of pairs of index of first instruction |
| 74 | /// and MBB id. |
| 75 | std::vector<IdxMBBPair> Idx2MBBMap; |
| 76 | |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 77 | typedef std::map<MachineInstr*, unsigned> Mi2IndexMap; |
| 78 | Mi2IndexMap mi2iMap_; |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 79 | |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 80 | typedef std::vector<MachineInstr*> Index2MiMap; |
| 81 | Index2MiMap i2miMap_; |
Alkis Evlogimenos | 843b160 | 2004-02-15 10:24:21 +0000 | [diff] [blame] | 82 | |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 83 | typedef std::map<unsigned, LiveInterval> Reg2IntervalMap; |
| 84 | Reg2IntervalMap r2iMap_; |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 85 | |
Evan Cheng | 61de82d | 2007-02-15 05:59:24 +0000 | [diff] [blame] | 86 | BitVector allocatableRegs_; |
Evan Cheng | 88d1f58 | 2007-03-01 02:03:03 +0000 | [diff] [blame] | 87 | |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 88 | std::vector<MachineInstr*> ClonedMIs; |
| 89 | |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 90 | public: |
Nick Lewycky | ecd94c8 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 91 | static char ID; // Pass identification, replacement for typeid |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 92 | LiveIntervals() : MachineFunctionPass((intptr_t)&ID) {} |
| 93 | |
Chris Lattner | f7da2c7 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 94 | struct InstrSlots { |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 95 | enum { |
| 96 | LOAD = 0, |
| 97 | USE = 1, |
| 98 | DEF = 2, |
| 99 | STORE = 3, |
Chris Lattner | 410354f | 2006-02-22 16:23:43 +0000 | [diff] [blame] | 100 | NUM = 4 |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 101 | }; |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 102 | }; |
| 103 | |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 104 | static unsigned getBaseIndex(unsigned index) { |
| 105 | return index - (index % InstrSlots::NUM); |
| 106 | } |
| 107 | static unsigned getBoundaryIndex(unsigned index) { |
| 108 | return getBaseIndex(index + InstrSlots::NUM - 1); |
| 109 | } |
| 110 | static unsigned getLoadIndex(unsigned index) { |
| 111 | return getBaseIndex(index) + InstrSlots::LOAD; |
| 112 | } |
| 113 | static unsigned getUseIndex(unsigned index) { |
| 114 | return getBaseIndex(index) + InstrSlots::USE; |
| 115 | } |
| 116 | static unsigned getDefIndex(unsigned index) { |
| 117 | return getBaseIndex(index) + InstrSlots::DEF; |
| 118 | } |
| 119 | static unsigned getStoreIndex(unsigned index) { |
| 120 | return getBaseIndex(index) + InstrSlots::STORE; |
| 121 | } |
| 122 | |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 123 | static float getSpillWeight(bool isDef, bool isUse, unsigned loopDepth) { |
| 124 | return (isDef + isUse) * powf(10.0F, (float)loopDepth); |
Evan Cheng | f2fbca6 | 2007-11-12 06:35:08 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 127 | typedef Reg2IntervalMap::iterator iterator; |
Chris Lattner | 70ca358 | 2004-09-30 15:59:17 +0000 | [diff] [blame] | 128 | typedef Reg2IntervalMap::const_iterator const_iterator; |
| 129 | const_iterator begin() const { return r2iMap_.begin(); } |
| 130 | const_iterator end() const { return r2iMap_.end(); } |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 131 | iterator begin() { return r2iMap_.begin(); } |
| 132 | iterator end() { return r2iMap_.end(); } |
| 133 | unsigned getNumIntervals() const { return r2iMap_.size(); } |
| 134 | |
| 135 | LiveInterval &getInterval(unsigned reg) { |
| 136 | Reg2IntervalMap::iterator I = r2iMap_.find(reg); |
| 137 | assert(I != r2iMap_.end() && "Interval does not exist for register"); |
| 138 | return I->second; |
| 139 | } |
| 140 | |
| 141 | const LiveInterval &getInterval(unsigned reg) const { |
| 142 | Reg2IntervalMap::const_iterator I = r2iMap_.find(reg); |
| 143 | assert(I != r2iMap_.end() && "Interval does not exist for register"); |
| 144 | return I->second; |
| 145 | } |
| 146 | |
Evan Cheng | b371f45 | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 147 | bool hasInterval(unsigned reg) const { |
Evan Cheng | 88d1f58 | 2007-03-01 02:03:03 +0000 | [diff] [blame] | 148 | return r2iMap_.count(reg); |
Evan Cheng | b371f45 | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Chris Lattner | 428b92e | 2006-09-15 03:57:23 +0000 | [diff] [blame] | 151 | /// getMBBStartIdx - Return the base index of the first instruction in the |
| 152 | /// specified MachineBasicBlock. |
| 153 | unsigned getMBBStartIdx(MachineBasicBlock *MBB) const { |
| 154 | return getMBBStartIdx(MBB->getNumber()); |
| 155 | } |
Chris Lattner | 428b92e | 2006-09-15 03:57:23 +0000 | [diff] [blame] | 156 | unsigned getMBBStartIdx(unsigned MBBNo) const { |
| 157 | assert(MBBNo < MBB2IdxMap.size() && "Invalid MBB number!"); |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 158 | return MBB2IdxMap[MBBNo].first; |
| 159 | } |
| 160 | |
| 161 | /// getMBBEndIdx - Return the store index of the last instruction in the |
| 162 | /// specified MachineBasicBlock. |
| 163 | unsigned getMBBEndIdx(MachineBasicBlock *MBB) const { |
| 164 | return getMBBEndIdx(MBB->getNumber()); |
| 165 | } |
| 166 | unsigned getMBBEndIdx(unsigned MBBNo) const { |
| 167 | assert(MBBNo < MBB2IdxMap.size() && "Invalid MBB number!"); |
| 168 | return MBB2IdxMap[MBBNo].second; |
Chris Lattner | 428b92e | 2006-09-15 03:57:23 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Roman Levenstein | 8dd2528 | 2008-02-18 09:35:30 +0000 | [diff] [blame] | 171 | /// getMBBFromIndex - given an index in any instruction of an |
| 172 | /// MBB return a pointer the MBB |
| 173 | MachineBasicBlock* getMBBFromIndex(unsigned index) const { |
| 174 | std::vector<IdxMBBPair>::const_iterator I = |
Bill Wendling | e85fe66 | 2008-02-26 10:49:39 +0000 | [diff] [blame] | 175 | std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), index); |
Roman Levenstein | 8dd2528 | 2008-02-18 09:35:30 +0000 | [diff] [blame] | 176 | // Take the pair containing the index |
| 177 | std::vector<IdxMBBPair>::const_iterator J = |
Bill Wendling | e85fe66 | 2008-02-26 10:49:39 +0000 | [diff] [blame] | 178 | ((I != Idx2MBBMap.end() && I->first > index) || |
| 179 | (I == Idx2MBBMap.end() && Idx2MBBMap.size()>0)) ? (I-1): I; |
Roman Levenstein | 8dd2528 | 2008-02-18 09:35:30 +0000 | [diff] [blame] | 180 | |
| 181 | assert(J != Idx2MBBMap.end() && J->first < index+1 && |
Bill Wendling | e85fe66 | 2008-02-26 10:49:39 +0000 | [diff] [blame] | 182 | index <= getMBBEndIdx(J->second) && |
| 183 | "index does not correspond to an MBB"); |
Roman Levenstein | 8dd2528 | 2008-02-18 09:35:30 +0000 | [diff] [blame] | 184 | return J->second; |
| 185 | } |
| 186 | |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 187 | /// getInstructionIndex - returns the base index of instr |
| 188 | unsigned getInstructionIndex(MachineInstr* instr) const { |
| 189 | Mi2IndexMap::const_iterator it = mi2iMap_.find(instr); |
| 190 | assert(it != mi2iMap_.end() && "Invalid instruction!"); |
| 191 | return it->second; |
| 192 | } |
| 193 | |
| 194 | /// getInstructionFromIndex - given an index in any slot of an |
| 195 | /// instruction return a pointer the instruction |
| 196 | MachineInstr* getInstructionFromIndex(unsigned index) const { |
| 197 | index /= InstrSlots::NUM; // convert index to vector index |
| 198 | assert(index < i2miMap_.size() && |
| 199 | "index does not correspond to an instruction"); |
| 200 | return i2miMap_[index]; |
| 201 | } |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 202 | |
Evan Cheng | c92da38 | 2007-11-03 07:20:12 +0000 | [diff] [blame] | 203 | /// conflictsWithPhysRegDef - Returns true if the specified register |
| 204 | /// is defined during the duration of the specified interval. |
| 205 | bool conflictsWithPhysRegDef(const LiveInterval &li, VirtRegMap &vrm, |
| 206 | unsigned reg); |
| 207 | |
Evan Cheng | 4ca980e | 2007-10-17 02:10:22 +0000 | [diff] [blame] | 208 | /// findLiveInMBBs - Given a live range, if the value of the range |
| 209 | /// is live in any MBB returns true as well as the list of basic blocks |
| 210 | /// where the value is live in. |
| 211 | bool findLiveInMBBs(const LiveRange &LR, |
Evan Cheng | a5bfc97 | 2007-10-17 06:53:44 +0000 | [diff] [blame] | 212 | SmallVectorImpl<MachineBasicBlock*> &MBBs) const; |
Evan Cheng | 4ca980e | 2007-10-17 02:10:22 +0000 | [diff] [blame] | 213 | |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 214 | // Interval creation |
| 215 | |
| 216 | LiveInterval &getOrCreateInterval(unsigned reg) { |
| 217 | Reg2IntervalMap::iterator I = r2iMap_.find(reg); |
| 218 | if (I == r2iMap_.end()) |
| 219 | I = r2iMap_.insert(I, std::make_pair(reg, createInterval(reg))); |
| 220 | return I->second; |
| 221 | } |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 222 | |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 223 | // Interval removal |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 224 | |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 225 | void removeInterval(unsigned Reg) { |
| 226 | r2iMap_.erase(Reg); |
Bill Wendling | 5c7e326 | 2006-12-17 05:15:13 +0000 | [diff] [blame] | 227 | } |
Chris Lattner | 70ca358 | 2004-09-30 15:59:17 +0000 | [diff] [blame] | 228 | |
Evan Cheng | 30cac02 | 2007-02-22 23:03:39 +0000 | [diff] [blame] | 229 | /// isRemoved - returns true if the specified machine instr has been |
| 230 | /// removed. |
| 231 | bool isRemoved(MachineInstr* instr) const { |
Evan Cheng | 7d35c0e | 2007-02-22 23:52:23 +0000 | [diff] [blame] | 232 | return !mi2iMap_.count(instr); |
Evan Cheng | 30cac02 | 2007-02-22 23:03:39 +0000 | [diff] [blame] | 233 | } |
| 234 | |
Chris Lattner | f7da2c7 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 235 | /// RemoveMachineInstrFromMaps - This marks the specified machine instr as |
| 236 | /// deleted. |
| 237 | void RemoveMachineInstrFromMaps(MachineInstr *MI) { |
| 238 | // remove index -> MachineInstr and |
| 239 | // MachineInstr -> index mappings |
| 240 | Mi2IndexMap::iterator mi2i = mi2iMap_.find(MI); |
| 241 | if (mi2i != mi2iMap_.end()) { |
| 242 | i2miMap_[mi2i->second/InstrSlots::NUM] = 0; |
| 243 | mi2iMap_.erase(mi2i); |
| 244 | } |
| 245 | } |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 246 | |
Evan Cheng | 7007143 | 2008-02-13 03:01:43 +0000 | [diff] [blame] | 247 | /// ReplaceMachineInstrInMaps - Replacing a machine instr with a new one in |
| 248 | /// maps used by register allocator. |
| 249 | void ReplaceMachineInstrInMaps(MachineInstr *MI, MachineInstr *NewMI) { |
| 250 | Mi2IndexMap::iterator mi2i = mi2iMap_.find(MI); |
Evan Cheng | b1f6f91 | 2008-02-13 09:18:16 +0000 | [diff] [blame] | 251 | if (mi2i == mi2iMap_.end()) |
| 252 | return; |
| 253 | i2miMap_[mi2i->second/InstrSlots::NUM] = NewMI; |
| 254 | Mi2IndexMap::iterator it = mi2iMap_.find(MI); |
| 255 | assert(it != mi2iMap_.end() && "Invalid instruction!"); |
| 256 | unsigned Index = it->second; |
| 257 | mi2iMap_.erase(it); |
| 258 | mi2iMap_[NewMI] = Index; |
Evan Cheng | 7007143 | 2008-02-13 03:01:43 +0000 | [diff] [blame] | 259 | } |
| 260 | |
Evan Cheng | f3bb2e6 | 2007-09-05 21:46:51 +0000 | [diff] [blame] | 261 | BumpPtrAllocator& getVNInfoAllocator() { return VNInfoAllocator; } |
| 262 | |
Evan Cheng | c8d044e | 2008-02-15 18:24:29 +0000 | [diff] [blame] | 263 | /// getVNInfoSourceReg - Helper function that parses the specified VNInfo |
| 264 | /// copy field and returns the source register that defines it. |
| 265 | unsigned getVNInfoSourceReg(const VNInfo *VNI) const; |
| 266 | |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 267 | virtual void getAnalysisUsage(AnalysisUsage &AU) const; |
| 268 | virtual void releaseMemory(); |
| 269 | |
| 270 | /// runOnMachineFunction - pass entry point |
| 271 | virtual bool runOnMachineFunction(MachineFunction&); |
| 272 | |
| 273 | /// print - Implement the dump method. |
| 274 | virtual void print(std::ostream &O, const Module* = 0) const; |
| 275 | void print(std::ostream *O, const Module* M = 0) const { |
| 276 | if (O) print(*O, M); |
| 277 | } |
| 278 | |
Evan Cheng | f2fbca6 | 2007-11-12 06:35:08 +0000 | [diff] [blame] | 279 | /// addIntervalsForSpills - Create new intervals for spilled defs / uses of |
| 280 | /// the given interval. |
| 281 | std::vector<LiveInterval*> |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 282 | addIntervalsForSpills(const LiveInterval& i, |
Evan Cheng | 22f07ff | 2007-12-11 02:09:15 +0000 | [diff] [blame] | 283 | const MachineLoopInfo *loopInfo, VirtRegMap& vrm); |
Evan Cheng | f2fbca6 | 2007-11-12 06:35:08 +0000 | [diff] [blame] | 284 | |
Evan Cheng | 676dd7c | 2008-03-11 07:19:34 +0000 | [diff] [blame] | 285 | /// spillPhysRegAroundRegDefsUses - Spill the specified physical register |
| 286 | /// around all defs and uses of the specified interval. |
| 287 | void spillPhysRegAroundRegDefsUses(const LiveInterval &li, |
| 288 | unsigned PhysReg, VirtRegMap &vrm); |
| 289 | |
Evan Cheng | 5ef3a04 | 2007-12-06 00:01:56 +0000 | [diff] [blame] | 290 | /// isReMaterializable - Returns true if every definition of MI of every |
| 291 | /// val# of the specified interval is re-materializable. Also returns true |
| 292 | /// by reference if all of the defs are load instructions. |
| 293 | bool isReMaterializable(const LiveInterval &li, bool &isLoad); |
| 294 | |
Evan Cheng | 676dd7c | 2008-03-11 07:19:34 +0000 | [diff] [blame] | 295 | /// getRepresentativeReg - Find the largest super register of the specified |
| 296 | /// physical register. |
| 297 | unsigned getRepresentativeReg(unsigned Reg) const; |
| 298 | |
| 299 | /// getNumConflictsWithPhysReg - Return the number of uses and defs of the |
| 300 | /// specified interval that conflicts with the specified physical register. |
| 301 | unsigned getNumConflictsWithPhysReg(const LiveInterval &li, |
| 302 | unsigned PhysReg) const; |
| 303 | |
David Greene | 2513330 | 2007-06-08 17:18:56 +0000 | [diff] [blame] | 304 | private: |
Chris Lattner | 428b92e | 2006-09-15 03:57:23 +0000 | [diff] [blame] | 305 | /// computeIntervals - Compute live intervals. |
Chris Lattner | c7695eb | 2006-09-14 06:42:17 +0000 | [diff] [blame] | 306 | void computeIntervals(); |
Chris Lattner | 6bda49f | 2006-09-02 05:26:01 +0000 | [diff] [blame] | 307 | |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 308 | /// handleRegisterDef - update intervals for a register def |
| 309 | /// (calls handlePhysicalRegisterDef and |
| 310 | /// handleVirtualRegisterDef) |
Chris Lattner | 6b128bd | 2006-09-03 08:07:11 +0000 | [diff] [blame] | 311 | void handleRegisterDef(MachineBasicBlock *MBB, |
| 312 | MachineBasicBlock::iterator MI, unsigned MIIdx, |
Chris Lattner | f38a05d | 2006-01-29 07:59:37 +0000 | [diff] [blame] | 313 | unsigned reg); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 314 | |
| 315 | /// handleVirtualRegisterDef - update intervals for a virtual |
| 316 | /// register def |
Chris Lattner | 6b128bd | 2006-09-03 08:07:11 +0000 | [diff] [blame] | 317 | void handleVirtualRegisterDef(MachineBasicBlock *MBB, |
| 318 | MachineBasicBlock::iterator MI, |
| 319 | unsigned MIIdx, |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 320 | LiveInterval& interval); |
| 321 | |
Chris Lattner | f768bba | 2005-03-09 23:05:19 +0000 | [diff] [blame] | 322 | /// handlePhysicalRegisterDef - update intervals for a physical register |
Chris Lattner | f7da2c7 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 323 | /// def. |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 324 | void handlePhysicalRegisterDef(MachineBasicBlock* mbb, |
| 325 | MachineBasicBlock::iterator mi, |
Chris Lattner | 6b128bd | 2006-09-03 08:07:11 +0000 | [diff] [blame] | 326 | unsigned MIIdx, |
Chris Lattner | 91725b7 | 2006-08-31 05:54:43 +0000 | [diff] [blame] | 327 | LiveInterval &interval, |
Evan Cheng | c8d044e | 2008-02-15 18:24:29 +0000 | [diff] [blame] | 328 | MachineInstr *CopyMI); |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 329 | |
Evan Cheng | b371f45 | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 330 | /// handleLiveInRegister - Create interval for a livein register. |
Jim Laskey | 9b25b8c | 2007-02-21 22:41:17 +0000 | [diff] [blame] | 331 | void handleLiveInRegister(MachineBasicBlock* mbb, |
| 332 | unsigned MIIdx, |
Evan Cheng | 24a3cc4 | 2007-04-25 07:30:23 +0000 | [diff] [blame] | 333 | LiveInterval &interval, bool isAlias = false); |
Evan Cheng | b371f45 | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 334 | |
Evan Cheng | d70dbb5 | 2008-02-22 09:24:50 +0000 | [diff] [blame] | 335 | /// getReMatImplicitUse - If the remat definition MI has one (for now, we |
| 336 | /// only allow one) virtual register operand, then its uses are implicitly |
| 337 | /// using the register. Returns the virtual register. |
| 338 | unsigned getReMatImplicitUse(const LiveInterval &li, |
| 339 | MachineInstr *MI) const; |
| 340 | |
| 341 | /// isValNoAvailableAt - Return true if the val# of the specified interval |
| 342 | /// which reaches the given instruction also reaches the specified use |
| 343 | /// index. |
| 344 | bool isValNoAvailableAt(const LiveInterval &li, MachineInstr *MI, |
| 345 | unsigned UseIdx) const; |
| 346 | |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 347 | /// isReMaterializable - Returns true if the definition MI of the specified |
Evan Cheng | 5ef3a04 | 2007-12-06 00:01:56 +0000 | [diff] [blame] | 348 | /// val# of the specified interval is re-materializable. Also returns true |
| 349 | /// by reference if the def is a load. |
Evan Cheng | 7ecb38b | 2007-08-29 20:45:00 +0000 | [diff] [blame] | 350 | bool isReMaterializable(const LiveInterval &li, const VNInfo *ValNo, |
Evan Cheng | 5ef3a04 | 2007-12-06 00:01:56 +0000 | [diff] [blame] | 351 | MachineInstr *MI, bool &isLoad); |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 352 | |
Evan Cheng | 35b35c5 | 2007-08-30 05:52:20 +0000 | [diff] [blame] | 353 | /// tryFoldMemoryOperand - Attempts to fold either a spill / restore from |
| 354 | /// slot / to reg or any rematerialized load into ith operand of specified |
| 355 | /// MI. If it is successul, MI is updated with the newly created MI and |
| 356 | /// returns true. |
| 357 | bool tryFoldMemoryOperand(MachineInstr* &MI, VirtRegMap &vrm, |
Evan Cheng | cddbb83 | 2007-11-30 21:23:43 +0000 | [diff] [blame] | 358 | MachineInstr *DefMI, unsigned InstrIdx, |
Evan Cheng | aee4af6 | 2007-12-02 08:30:39 +0000 | [diff] [blame] | 359 | SmallVector<unsigned, 2> &Ops, |
Evan Cheng | cddbb83 | 2007-11-30 21:23:43 +0000 | [diff] [blame] | 360 | bool isSS, int Slot, unsigned Reg); |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 361 | |
Evan Cheng | d70dbb5 | 2008-02-22 09:24:50 +0000 | [diff] [blame] | 362 | /// canFoldMemoryOperand - Return true if the specified load / store |
Evan Cheng | 018f9b0 | 2007-12-05 03:22:34 +0000 | [diff] [blame] | 363 | /// folding is possible. |
Evan Cheng | d64b5c8 | 2007-12-05 03:14:33 +0000 | [diff] [blame] | 364 | bool canFoldMemoryOperand(MachineInstr *MI, |
Evan Cheng | 79a0c1e | 2008-02-25 08:50:41 +0000 | [diff] [blame] | 365 | SmallVector<unsigned, 2> &Ops, |
| 366 | bool ReMatLoadSS) const; |
Evan Cheng | d64b5c8 | 2007-12-05 03:14:33 +0000 | [diff] [blame] | 367 | |
Evan Cheng | 0cbb116 | 2007-11-29 01:06:25 +0000 | [diff] [blame] | 368 | /// anyKillInMBBAfterIdx - Returns true if there is a kill of the specified |
| 369 | /// VNInfo that's after the specified index but is within the basic block. |
| 370 | bool anyKillInMBBAfterIdx(const LiveInterval &li, const VNInfo *VNI, |
| 371 | MachineBasicBlock *MBB, unsigned Idx) const; |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 372 | |
Evan Cheng | 0cbb116 | 2007-11-29 01:06:25 +0000 | [diff] [blame] | 373 | /// intervalIsInOneMBB - Returns true if the specified interval is entirely |
| 374 | /// within a single basic block. |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 375 | bool intervalIsInOneMBB(const LiveInterval &li) const; |
| 376 | |
Evan Cheng | 676dd7c | 2008-03-11 07:19:34 +0000 | [diff] [blame] | 377 | /// hasAllocatableSuperReg - Return true if the specified physical register |
| 378 | /// has any super register that's allocatable. |
| 379 | bool hasAllocatableSuperReg(unsigned Reg) const; |
| 380 | |
Evan Cheng | 1953d0c | 2007-11-29 10:12:14 +0000 | [diff] [blame] | 381 | /// SRInfo - Spill / restore info. |
| 382 | struct SRInfo { |
| 383 | int index; |
| 384 | unsigned vreg; |
| 385 | bool canFold; |
| 386 | SRInfo(int i, unsigned vr, bool f) : index(i), vreg(vr), canFold(f) {}; |
| 387 | }; |
| 388 | |
| 389 | bool alsoFoldARestore(int Id, int index, unsigned vr, |
| 390 | BitVector &RestoreMBBs, |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 391 | std::map<unsigned,std::vector<SRInfo> >&RestoreIdxes); |
Evan Cheng | 1953d0c | 2007-11-29 10:12:14 +0000 | [diff] [blame] | 392 | void eraseRestoreInfo(int Id, int index, unsigned vr, |
| 393 | BitVector &RestoreMBBs, |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 394 | std::map<unsigned,std::vector<SRInfo> >&RestoreIdxes); |
Evan Cheng | 1953d0c | 2007-11-29 10:12:14 +0000 | [diff] [blame] | 395 | |
Evan Cheng | d70dbb5 | 2008-02-22 09:24:50 +0000 | [diff] [blame] | 396 | /// rewriteImplicitOps - Rewrite implicit use operands of MI (i.e. uses of |
| 397 | /// interval on to-be re-materialized operands of MI) with new register. |
| 398 | void rewriteImplicitOps(const LiveInterval &li, |
| 399 | MachineInstr *MI, unsigned NewVReg, VirtRegMap &vrm); |
| 400 | |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 401 | /// rewriteInstructionForSpills, rewriteInstructionsForSpills - Helper |
| 402 | /// functions for addIntervalsForSpills to rewrite uses / defs for the given |
| 403 | /// live range. |
Evan Cheng | d70dbb5 | 2008-02-22 09:24:50 +0000 | [diff] [blame] | 404 | bool rewriteInstructionForSpills(const LiveInterval &li, const VNInfo *VNI, |
| 405 | bool TrySplit, unsigned index, unsigned end, MachineInstr *MI, |
Evan Cheng | f2fbca6 | 2007-11-12 06:35:08 +0000 | [diff] [blame] | 406 | MachineInstr *OrigDefMI, MachineInstr *DefMI, unsigned Slot, int LdSlot, |
| 407 | bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete, |
Evan Cheng | d70dbb5 | 2008-02-22 09:24:50 +0000 | [diff] [blame] | 408 | VirtRegMap &vrm, const TargetRegisterClass* rc, |
| 409 | SmallVector<int, 4> &ReMatIds, const MachineLoopInfo *loopInfo, |
Evan Cheng | 0cc83b6 | 2008-02-23 00:46:11 +0000 | [diff] [blame] | 410 | unsigned &NewVReg, unsigned ImpUse, bool &HasDef, bool &HasUse, |
Evan Cheng | 1953d0c | 2007-11-29 10:12:14 +0000 | [diff] [blame] | 411 | std::map<unsigned,unsigned> &MBBVRegsMap, |
Evan Cheng | f2fbca6 | 2007-11-12 06:35:08 +0000 | [diff] [blame] | 412 | std::vector<LiveInterval*> &NewLIs); |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 413 | void rewriteInstructionsForSpills(const LiveInterval &li, bool TrySplit, |
Evan Cheng | f2fbca6 | 2007-11-12 06:35:08 +0000 | [diff] [blame] | 414 | LiveInterval::Ranges::const_iterator &I, |
| 415 | MachineInstr *OrigDefMI, MachineInstr *DefMI, unsigned Slot, int LdSlot, |
| 416 | bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete, |
Evan Cheng | d70dbb5 | 2008-02-22 09:24:50 +0000 | [diff] [blame] | 417 | VirtRegMap &vrm, const TargetRegisterClass* rc, |
Evan Cheng | 22f07ff | 2007-12-11 02:09:15 +0000 | [diff] [blame] | 418 | SmallVector<int, 4> &ReMatIds, const MachineLoopInfo *loopInfo, |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 419 | BitVector &SpillMBBs, |
Evan Cheng | 1953d0c | 2007-11-29 10:12:14 +0000 | [diff] [blame] | 420 | std::map<unsigned,std::vector<SRInfo> > &SpillIdxes, |
Evan Cheng | 0cbb116 | 2007-11-29 01:06:25 +0000 | [diff] [blame] | 421 | BitVector &RestoreMBBs, |
Evan Cheng | 1953d0c | 2007-11-29 10:12:14 +0000 | [diff] [blame] | 422 | std::map<unsigned,std::vector<SRInfo> > &RestoreIdxes, |
| 423 | std::map<unsigned,unsigned> &MBBVRegsMap, |
Evan Cheng | f2fbca6 | 2007-11-12 06:35:08 +0000 | [diff] [blame] | 424 | std::vector<LiveInterval*> &NewLIs); |
| 425 | |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 426 | static LiveInterval createInterval(unsigned Reg); |
| 427 | |
Alkis Evlogimenos | 1a8ea01 | 2004-08-04 09:46:26 +0000 | [diff] [blame] | 428 | void printRegName(unsigned reg) const; |
| 429 | }; |
| 430 | |
Alkis Evlogimenos | ff0cbe1 | 2003-11-20 03:32:25 +0000 | [diff] [blame] | 431 | } // End llvm namespace |
| 432 | |
| 433 | #endif |