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