Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1 | //===-- llvm/CodeGen/VirtRegMap.h - Virtual Register Map -*- 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 a virtual register map. This maps virtual registers to |
| 11 | // physical registers and virtual registers to stack slots. It is created and |
| 12 | // updated by a register allocator and then used by a machine code rewriter that |
| 13 | // adds spill code and rewrites virtual into physical register references. |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
| 17 | #ifndef LLVM_CODEGEN_VIRTREGMAP_H |
| 18 | #define LLVM_CODEGEN_VIRTREGMAP_H |
| 19 | |
| 20 | #include "llvm/Target/MRegisterInfo.h" |
Evan Cheng | cecc822 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/DenseMap.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/IndexedMap.h" |
| 23 | #include "llvm/Support/Streams.h" |
| 24 | #include <map> |
| 25 | |
| 26 | namespace llvm { |
| 27 | class MachineInstr; |
David Greene | 44a3bfb | 2007-08-07 16:34:05 +0000 | [diff] [blame] | 28 | class MachineFunction; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 29 | class TargetInstrInfo; |
| 30 | |
| 31 | class VirtRegMap { |
| 32 | public: |
| 33 | enum { |
| 34 | NO_PHYS_REG = 0, |
| 35 | NO_STACK_SLOT = (1L << 30)-1, |
| 36 | MAX_STACK_SLOT = (1L << 18)-1 |
| 37 | }; |
| 38 | |
| 39 | enum ModRef { isRef = 1, isMod = 2, isModRef = 3 }; |
| 40 | typedef std::multimap<MachineInstr*, |
| 41 | std::pair<unsigned, ModRef> > MI2VirtMapTy; |
| 42 | |
| 43 | private: |
| 44 | const TargetInstrInfo &TII; |
| 45 | |
| 46 | MachineFunction &MF; |
| 47 | /// Virt2PhysMap - This is a virtual to physical register |
| 48 | /// mapping. Each virtual register is required to have an entry in |
| 49 | /// it; even spilled virtual registers (the register mapped to a |
| 50 | /// spilled register is the temporary used to load it from the |
| 51 | /// stack). |
| 52 | IndexedMap<unsigned, VirtReg2IndexFunctor> Virt2PhysMap; |
Evan Cheng | cecc822 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 53 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 54 | /// Virt2StackSlotMap - This is virtual register to stack slot |
| 55 | /// mapping. Each spilled virtual register has an entry in it |
| 56 | /// which corresponds to the stack slot this register is spilled |
| 57 | /// at. |
| 58 | IndexedMap<int, VirtReg2IndexFunctor> Virt2StackSlotMap; |
Evan Cheng | cecc822 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 59 | |
| 60 | /// Virt2StackSlotMap - This is virtual register to rematerialization id |
| 61 | /// mapping. Each spilled virtual register that should be remat'd has an |
| 62 | /// entry in it which corresponds to the remat id. |
Evan Cheng | 1204d17 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 63 | IndexedMap<int, VirtReg2IndexFunctor> Virt2ReMatIdMap; |
Evan Cheng | cecc822 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 64 | |
| 65 | /// Virt2SplitMap - This is virtual register to splitted virtual register |
| 66 | /// mapping. |
| 67 | IndexedMap<unsigned, VirtReg2IndexFunctor> Virt2SplitMap; |
| 68 | |
| 69 | /// ReMatMap - This is virtual register to re-materialized instruction |
| 70 | /// mapping. Each virtual register whose definition is going to be |
| 71 | /// re-materialized has an entry in it. |
| 72 | IndexedMap<MachineInstr*, VirtReg2IndexFunctor> ReMatMap; |
| 73 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 74 | /// MI2VirtMap - This is MachineInstr to virtual register |
| 75 | /// mapping. In the case of memory spill code being folded into |
| 76 | /// instructions, we need to know which virtual register was |
| 77 | /// read/written by this instruction. |
| 78 | MI2VirtMapTy MI2VirtMap; |
| 79 | |
Evan Cheng | cecc822 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 80 | /// SpillPt2VirtMap - This records the virtual registers which should |
| 81 | /// be spilled right after the MachineInstr due to live interval |
| 82 | /// splitting. |
Evan Cheng | 91e32d0 | 2007-11-28 01:28:46 +0000 | [diff] [blame] | 83 | std::map<MachineInstr*, std::vector<unsigned> > SpillPt2VirtMap; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 84 | |
Evan Cheng | 96c6131 | 2007-11-29 01:06:25 +0000 | [diff] [blame^] | 85 | /// RestorePt2VirtMap - This records the virtual registers which should |
| 86 | /// be restored right before the MachineInstr due to live interval |
| 87 | /// splitting. |
| 88 | std::map<MachineInstr*, std::vector<unsigned> > RestorePt2VirtMap; |
| 89 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 90 | /// ReMatId - Instead of assigning a stack slot to a to be rematerialized |
| 91 | /// virtual register, an unique id is being assigned. This keeps track of |
| 92 | /// the highest id used so far. Note, this starts at (1<<18) to avoid |
| 93 | /// conflicts with stack slot numbers. |
| 94 | int ReMatId; |
| 95 | |
| 96 | VirtRegMap(const VirtRegMap&); // DO NOT IMPLEMENT |
| 97 | void operator=(const VirtRegMap&); // DO NOT IMPLEMENT |
| 98 | |
| 99 | public: |
Dan Gohman | 3a78bbf | 2007-08-02 21:21:54 +0000 | [diff] [blame] | 100 | explicit VirtRegMap(MachineFunction &mf); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 101 | |
| 102 | void grow(); |
| 103 | |
| 104 | /// @brief returns true if the specified virtual register is |
| 105 | /// mapped to a physical register |
| 106 | bool hasPhys(unsigned virtReg) const { |
| 107 | return getPhys(virtReg) != NO_PHYS_REG; |
| 108 | } |
| 109 | |
| 110 | /// @brief returns the physical register mapped to the specified |
| 111 | /// virtual register |
| 112 | unsigned getPhys(unsigned virtReg) const { |
| 113 | assert(MRegisterInfo::isVirtualRegister(virtReg)); |
| 114 | return Virt2PhysMap[virtReg]; |
| 115 | } |
| 116 | |
| 117 | /// @brief creates a mapping for the specified virtual register to |
| 118 | /// the specified physical register |
| 119 | void assignVirt2Phys(unsigned virtReg, unsigned physReg) { |
| 120 | assert(MRegisterInfo::isVirtualRegister(virtReg) && |
| 121 | MRegisterInfo::isPhysicalRegister(physReg)); |
| 122 | assert(Virt2PhysMap[virtReg] == NO_PHYS_REG && |
| 123 | "attempt to assign physical register to already mapped " |
| 124 | "virtual register"); |
| 125 | Virt2PhysMap[virtReg] = physReg; |
| 126 | } |
| 127 | |
| 128 | /// @brief clears the specified virtual register's, physical |
| 129 | /// register mapping |
| 130 | void clearVirt(unsigned virtReg) { |
| 131 | assert(MRegisterInfo::isVirtualRegister(virtReg)); |
| 132 | assert(Virt2PhysMap[virtReg] != NO_PHYS_REG && |
| 133 | "attempt to clear a not assigned virtual register"); |
| 134 | Virt2PhysMap[virtReg] = NO_PHYS_REG; |
| 135 | } |
| 136 | |
| 137 | /// @brief clears all virtual to physical register mappings |
| 138 | void clearAllVirt() { |
| 139 | Virt2PhysMap.clear(); |
| 140 | grow(); |
| 141 | } |
| 142 | |
Evan Cheng | cecc822 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 143 | /// @brief records virtReg is a split live interval from SReg. |
| 144 | void setIsSplitFromReg(unsigned virtReg, unsigned SReg) { |
| 145 | Virt2SplitMap[virtReg] = SReg; |
| 146 | } |
| 147 | |
| 148 | /// @brief returns the live interval virtReg is split from. |
| 149 | unsigned getPreSplitReg(unsigned virtReg) { |
| 150 | return Virt2SplitMap[virtReg]; |
| 151 | } |
| 152 | |
Evan Cheng | 1204d17 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 153 | /// @brief returns true is the specified virtual register is not |
| 154 | /// mapped to a stack slot or rematerialized. |
| 155 | bool isAssignedReg(unsigned virtReg) const { |
Evan Cheng | cecc822 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 156 | if (getStackSlot(virtReg) == NO_STACK_SLOT && |
| 157 | getReMatId(virtReg) == NO_STACK_SLOT) |
| 158 | return true; |
| 159 | // Split register can be assigned a physical register as well as a |
| 160 | // stack slot or remat id. |
| 161 | return (Virt2SplitMap[virtReg] && Virt2PhysMap[virtReg] != NO_PHYS_REG); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | /// @brief returns the stack slot mapped to the specified virtual |
| 165 | /// register |
| 166 | int getStackSlot(unsigned virtReg) const { |
| 167 | assert(MRegisterInfo::isVirtualRegister(virtReg)); |
| 168 | return Virt2StackSlotMap[virtReg]; |
| 169 | } |
| 170 | |
Evan Cheng | 1204d17 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 171 | /// @brief returns the rematerialization id mapped to the specified virtual |
| 172 | /// register |
| 173 | int getReMatId(unsigned virtReg) const { |
| 174 | assert(MRegisterInfo::isVirtualRegister(virtReg)); |
| 175 | return Virt2ReMatIdMap[virtReg]; |
| 176 | } |
| 177 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 178 | /// @brief create a mapping for the specifed virtual register to |
| 179 | /// the next available stack slot |
| 180 | int assignVirt2StackSlot(unsigned virtReg); |
| 181 | /// @brief create a mapping for the specified virtual register to |
| 182 | /// the specified stack slot |
| 183 | void assignVirt2StackSlot(unsigned virtReg, int frameIndex); |
| 184 | |
| 185 | /// @brief assign an unique re-materialization id to the specified |
| 186 | /// virtual register. |
| 187 | int assignVirtReMatId(unsigned virtReg); |
Evan Cheng | 1204d17 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 188 | /// @brief assign an unique re-materialization id to the specified |
| 189 | /// virtual register. |
| 190 | void assignVirtReMatId(unsigned virtReg, int id); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 191 | |
| 192 | /// @brief returns true if the specified virtual register is being |
| 193 | /// re-materialized. |
| 194 | bool isReMaterialized(unsigned virtReg) const { |
Evan Cheng | 1204d17 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 195 | return ReMatMap[virtReg] != NULL; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | /// @brief returns the original machine instruction being re-issued |
| 199 | /// to re-materialize the specified virtual register. |
Evan Cheng | 1204d17 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 200 | MachineInstr *getReMaterializedMI(unsigned virtReg) const { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 201 | return ReMatMap[virtReg]; |
| 202 | } |
| 203 | |
| 204 | /// @brief records the specified virtual register will be |
| 205 | /// re-materialized and the original instruction which will be re-issed |
Evan Cheng | 1204d17 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 206 | /// for this purpose. If parameter all is true, then all uses of the |
| 207 | /// registers are rematerialized and it's safe to delete the definition. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 208 | void setVirtIsReMaterialized(unsigned virtReg, MachineInstr *def) { |
| 209 | ReMatMap[virtReg] = def; |
| 210 | } |
| 211 | |
Evan Cheng | 91e32d0 | 2007-11-28 01:28:46 +0000 | [diff] [blame] | 212 | /// @brief returns true if the specified MachineInstr is a spill point. |
| 213 | bool isSpillPt(MachineInstr *Pt) const { |
| 214 | return SpillPt2VirtMap.find(Pt) != SpillPt2VirtMap.end(); |
| 215 | } |
| 216 | |
Evan Cheng | cecc822 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 217 | /// @brief returns the virtual registers that should be spilled due to |
| 218 | /// splitting right after the specified MachineInstr. |
| 219 | std::vector<unsigned> &getSpillPtSpills(MachineInstr *Pt) { |
| 220 | return SpillPt2VirtMap[Pt]; |
| 221 | } |
| 222 | |
| 223 | /// @brief records the specified MachineInstr as a spill point for virtReg. |
| 224 | void addSpillPoint(unsigned virtReg, MachineInstr *Pt) { |
Evan Cheng | 91e32d0 | 2007-11-28 01:28:46 +0000 | [diff] [blame] | 225 | if (SpillPt2VirtMap.find(Pt) != SpillPt2VirtMap.end()) |
| 226 | SpillPt2VirtMap[Pt].push_back(virtReg); |
| 227 | else { |
| 228 | std::vector<unsigned> Virts; |
| 229 | Virts.push_back(virtReg); |
| 230 | SpillPt2VirtMap.insert(std::make_pair(Pt, Virts)); |
| 231 | } |
Evan Cheng | cecc822 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | void transferSpillPts(MachineInstr *Old, MachineInstr *New) { |
Evan Cheng | 91e32d0 | 2007-11-28 01:28:46 +0000 | [diff] [blame] | 235 | std::map<MachineInstr*,std::vector<unsigned> >::iterator I = |
| 236 | SpillPt2VirtMap.find(Old); |
| 237 | if (I == SpillPt2VirtMap.end()) |
| 238 | return; |
| 239 | while (!I->second.empty()) { |
| 240 | unsigned virtReg = I->second.back(); |
| 241 | I->second.pop_back(); |
Evan Cheng | cecc822 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 242 | addSpillPoint(virtReg, New); |
| 243 | } |
Evan Cheng | 91e32d0 | 2007-11-28 01:28:46 +0000 | [diff] [blame] | 244 | SpillPt2VirtMap.erase(I); |
Evan Cheng | cecc822 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 245 | } |
| 246 | |
Evan Cheng | 96c6131 | 2007-11-29 01:06:25 +0000 | [diff] [blame^] | 247 | /// @brief returns true if the specified MachineInstr is a restore point. |
| 248 | bool isRestorePt(MachineInstr *Pt) const { |
| 249 | return RestorePt2VirtMap.find(Pt) != RestorePt2VirtMap.end(); |
| 250 | } |
| 251 | |
| 252 | /// @brief returns the virtual registers that should be restoreed due to |
| 253 | /// splitting right after the specified MachineInstr. |
| 254 | std::vector<unsigned> &getRestorePtRestores(MachineInstr *Pt) { |
| 255 | return RestorePt2VirtMap[Pt]; |
| 256 | } |
| 257 | |
| 258 | /// @brief records the specified MachineInstr as a restore point for virtReg. |
| 259 | void addRestorePoint(unsigned virtReg, MachineInstr *Pt) { |
| 260 | if (RestorePt2VirtMap.find(Pt) != RestorePt2VirtMap.end()) |
| 261 | RestorePt2VirtMap[Pt].push_back(virtReg); |
| 262 | else { |
| 263 | std::vector<unsigned> Virts; |
| 264 | Virts.push_back(virtReg); |
| 265 | RestorePt2VirtMap.insert(std::make_pair(Pt, Virts)); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | void transferRestorePts(MachineInstr *Old, MachineInstr *New) { |
| 270 | std::map<MachineInstr*,std::vector<unsigned> >::iterator I = |
| 271 | RestorePt2VirtMap.find(Old); |
| 272 | if (I == RestorePt2VirtMap.end()) |
| 273 | return; |
| 274 | while (!I->second.empty()) { |
| 275 | unsigned virtReg = I->second.back(); |
| 276 | I->second.pop_back(); |
| 277 | addRestorePoint(virtReg, New); |
| 278 | } |
| 279 | RestorePt2VirtMap.erase(I); |
| 280 | } |
| 281 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 282 | /// @brief Updates information about the specified virtual register's value |
| 283 | /// folded into newMI machine instruction. The OpNum argument indicates the |
| 284 | /// operand number of OldMI that is folded. |
| 285 | void virtFolded(unsigned VirtReg, MachineInstr *OldMI, unsigned OpNum, |
| 286 | MachineInstr *NewMI); |
| 287 | |
Evan Cheng | f325584 | 2007-10-13 02:50:24 +0000 | [diff] [blame] | 288 | /// @brief Updates information about the specified virtual register's value |
| 289 | /// folded into the specified machine instruction. |
| 290 | void virtFolded(unsigned VirtReg, MachineInstr *MI, ModRef MRInfo); |
| 291 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 292 | /// @brief returns the virtual registers' values folded in memory |
| 293 | /// operands of this instruction |
| 294 | std::pair<MI2VirtMapTy::const_iterator, MI2VirtMapTy::const_iterator> |
| 295 | getFoldedVirts(MachineInstr* MI) const { |
| 296 | return MI2VirtMap.equal_range(MI); |
| 297 | } |
| 298 | |
Evan Cheng | 91e32d0 | 2007-11-28 01:28:46 +0000 | [diff] [blame] | 299 | /// RemoveMachineInstrFromMaps - MI is being erased, remove it from the |
| 300 | /// the folded instruction map and spill point map. |
| 301 | void RemoveMachineInstrFromMaps(MachineInstr *MI) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 302 | MI2VirtMap.erase(MI); |
Evan Cheng | 91e32d0 | 2007-11-28 01:28:46 +0000 | [diff] [blame] | 303 | SpillPt2VirtMap.erase(MI); |
Evan Cheng | 96c6131 | 2007-11-29 01:06:25 +0000 | [diff] [blame^] | 304 | RestorePt2VirtMap.erase(MI); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | void print(std::ostream &OS) const; |
| 308 | void print(std::ostream *OS) const { if (OS) print(*OS); } |
| 309 | void dump() const; |
| 310 | }; |
| 311 | |
| 312 | inline std::ostream *operator<<(std::ostream *OS, const VirtRegMap &VRM) { |
| 313 | VRM.print(OS); |
| 314 | return OS; |
| 315 | } |
| 316 | inline std::ostream &operator<<(std::ostream &OS, const VirtRegMap &VRM) { |
| 317 | VRM.print(OS); |
| 318 | return OS; |
| 319 | } |
| 320 | |
| 321 | /// Spiller interface: Implementations of this interface assign spilled |
| 322 | /// virtual registers to stack slots, rewriting the code. |
| 323 | struct Spiller { |
| 324 | virtual ~Spiller(); |
| 325 | virtual bool runOnMachineFunction(MachineFunction &MF, |
| 326 | VirtRegMap &VRM) = 0; |
| 327 | }; |
| 328 | |
| 329 | /// createSpiller - Create an return a spiller object, as specified on the |
| 330 | /// command line. |
| 331 | Spiller* createSpiller(); |
| 332 | |
| 333 | } // End llvm namespace |
| 334 | |
| 335 | #endif |