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" |
| 21 | #include "llvm/ADT/BitVector.h" |
| 22 | #include "llvm/ADT/IndexedMap.h" |
| 23 | #include "llvm/Support/Streams.h" |
| 24 | #include <map> |
| 25 | |
| 26 | namespace llvm { |
| 27 | class MachineInstr; |
| 28 | class TargetInstrInfo; |
| 29 | |
| 30 | class VirtRegMap { |
| 31 | public: |
| 32 | enum { |
| 33 | NO_PHYS_REG = 0, |
| 34 | NO_STACK_SLOT = (1L << 30)-1, |
| 35 | MAX_STACK_SLOT = (1L << 18)-1 |
| 36 | }; |
| 37 | |
| 38 | enum ModRef { isRef = 1, isMod = 2, isModRef = 3 }; |
| 39 | typedef std::multimap<MachineInstr*, |
| 40 | std::pair<unsigned, ModRef> > MI2VirtMapTy; |
| 41 | |
| 42 | private: |
| 43 | const TargetInstrInfo &TII; |
| 44 | |
| 45 | MachineFunction &MF; |
| 46 | /// Virt2PhysMap - This is a virtual to physical register |
| 47 | /// mapping. Each virtual register is required to have an entry in |
| 48 | /// it; even spilled virtual registers (the register mapped to a |
| 49 | /// spilled register is the temporary used to load it from the |
| 50 | /// stack). |
| 51 | IndexedMap<unsigned, VirtReg2IndexFunctor> Virt2PhysMap; |
| 52 | /// Virt2StackSlotMap - This is virtual register to stack slot |
| 53 | /// mapping. Each spilled virtual register has an entry in it |
| 54 | /// which corresponds to the stack slot this register is spilled |
| 55 | /// at. |
| 56 | IndexedMap<int, VirtReg2IndexFunctor> Virt2StackSlotMap; |
| 57 | /// MI2VirtMap - This is MachineInstr to virtual register |
| 58 | /// mapping. In the case of memory spill code being folded into |
| 59 | /// instructions, we need to know which virtual register was |
| 60 | /// read/written by this instruction. |
| 61 | MI2VirtMapTy MI2VirtMap; |
| 62 | |
| 63 | /// ReMatMap - This is virtual register to re-materialized instruction |
| 64 | /// mapping. Each virtual register whose definition is going to be |
| 65 | /// re-materialized has an entry in it. |
| 66 | std::map<unsigned, const MachineInstr*> ReMatMap; |
| 67 | |
| 68 | /// ReMatId - Instead of assigning a stack slot to a to be rematerialized |
| 69 | /// virtual register, an unique id is being assigned. This keeps track of |
| 70 | /// the highest id used so far. Note, this starts at (1<<18) to avoid |
| 71 | /// conflicts with stack slot numbers. |
| 72 | int ReMatId; |
| 73 | |
| 74 | VirtRegMap(const VirtRegMap&); // DO NOT IMPLEMENT |
| 75 | void operator=(const VirtRegMap&); // DO NOT IMPLEMENT |
| 76 | |
| 77 | public: |
Dan Gohman | 3a78bbf | 2007-08-02 21:21:54 +0000 | [diff] [blame^] | 78 | explicit VirtRegMap(MachineFunction &mf); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 79 | |
| 80 | void grow(); |
| 81 | |
| 82 | /// @brief returns true if the specified virtual register is |
| 83 | /// mapped to a physical register |
| 84 | bool hasPhys(unsigned virtReg) const { |
| 85 | return getPhys(virtReg) != NO_PHYS_REG; |
| 86 | } |
| 87 | |
| 88 | /// @brief returns the physical register mapped to the specified |
| 89 | /// virtual register |
| 90 | unsigned getPhys(unsigned virtReg) const { |
| 91 | assert(MRegisterInfo::isVirtualRegister(virtReg)); |
| 92 | return Virt2PhysMap[virtReg]; |
| 93 | } |
| 94 | |
| 95 | /// @brief creates a mapping for the specified virtual register to |
| 96 | /// the specified physical register |
| 97 | void assignVirt2Phys(unsigned virtReg, unsigned physReg) { |
| 98 | assert(MRegisterInfo::isVirtualRegister(virtReg) && |
| 99 | MRegisterInfo::isPhysicalRegister(physReg)); |
| 100 | assert(Virt2PhysMap[virtReg] == NO_PHYS_REG && |
| 101 | "attempt to assign physical register to already mapped " |
| 102 | "virtual register"); |
| 103 | Virt2PhysMap[virtReg] = physReg; |
| 104 | } |
| 105 | |
| 106 | /// @brief clears the specified virtual register's, physical |
| 107 | /// register mapping |
| 108 | void clearVirt(unsigned virtReg) { |
| 109 | assert(MRegisterInfo::isVirtualRegister(virtReg)); |
| 110 | assert(Virt2PhysMap[virtReg] != NO_PHYS_REG && |
| 111 | "attempt to clear a not assigned virtual register"); |
| 112 | Virt2PhysMap[virtReg] = NO_PHYS_REG; |
| 113 | } |
| 114 | |
| 115 | /// @brief clears all virtual to physical register mappings |
| 116 | void clearAllVirt() { |
| 117 | Virt2PhysMap.clear(); |
| 118 | grow(); |
| 119 | } |
| 120 | |
| 121 | /// @brief returns true is the specified virtual register is |
| 122 | /// mapped to a stack slot |
| 123 | bool hasStackSlot(unsigned virtReg) const { |
| 124 | return getStackSlot(virtReg) != NO_STACK_SLOT; |
| 125 | } |
| 126 | |
| 127 | /// @brief returns the stack slot mapped to the specified virtual |
| 128 | /// register |
| 129 | int getStackSlot(unsigned virtReg) const { |
| 130 | assert(MRegisterInfo::isVirtualRegister(virtReg)); |
| 131 | return Virt2StackSlotMap[virtReg]; |
| 132 | } |
| 133 | |
| 134 | /// @brief create a mapping for the specifed virtual register to |
| 135 | /// the next available stack slot |
| 136 | int assignVirt2StackSlot(unsigned virtReg); |
| 137 | /// @brief create a mapping for the specified virtual register to |
| 138 | /// the specified stack slot |
| 139 | void assignVirt2StackSlot(unsigned virtReg, int frameIndex); |
| 140 | |
| 141 | /// @brief assign an unique re-materialization id to the specified |
| 142 | /// virtual register. |
| 143 | int assignVirtReMatId(unsigned virtReg); |
| 144 | |
| 145 | /// @brief returns true if the specified virtual register is being |
| 146 | /// re-materialized. |
| 147 | bool isReMaterialized(unsigned virtReg) const { |
| 148 | return ReMatMap.count(virtReg) != 0; |
| 149 | } |
| 150 | |
| 151 | /// @brief returns the original machine instruction being re-issued |
| 152 | /// to re-materialize the specified virtual register. |
| 153 | const MachineInstr *getReMaterializedMI(unsigned virtReg) { |
| 154 | return ReMatMap[virtReg]; |
| 155 | } |
| 156 | |
| 157 | /// @brief records the specified virtual register will be |
| 158 | /// re-materialized and the original instruction which will be re-issed |
| 159 | /// for this purpose. |
| 160 | void setVirtIsReMaterialized(unsigned virtReg, MachineInstr *def) { |
| 161 | ReMatMap[virtReg] = def; |
| 162 | } |
| 163 | |
| 164 | /// @brief Updates information about the specified virtual register's value |
| 165 | /// folded into newMI machine instruction. The OpNum argument indicates the |
| 166 | /// operand number of OldMI that is folded. |
| 167 | void virtFolded(unsigned VirtReg, MachineInstr *OldMI, unsigned OpNum, |
| 168 | MachineInstr *NewMI); |
| 169 | |
| 170 | /// @brief returns the virtual registers' values folded in memory |
| 171 | /// operands of this instruction |
| 172 | std::pair<MI2VirtMapTy::const_iterator, MI2VirtMapTy::const_iterator> |
| 173 | getFoldedVirts(MachineInstr* MI) const { |
| 174 | return MI2VirtMap.equal_range(MI); |
| 175 | } |
| 176 | |
| 177 | /// RemoveFromFoldedVirtMap - If the specified machine instruction is in |
| 178 | /// the folded instruction map, remove its entry from the map. |
| 179 | void RemoveFromFoldedVirtMap(MachineInstr *MI) { |
| 180 | MI2VirtMap.erase(MI); |
| 181 | } |
| 182 | |
| 183 | void print(std::ostream &OS) const; |
| 184 | void print(std::ostream *OS) const { if (OS) print(*OS); } |
| 185 | void dump() const; |
| 186 | }; |
| 187 | |
| 188 | inline std::ostream *operator<<(std::ostream *OS, const VirtRegMap &VRM) { |
| 189 | VRM.print(OS); |
| 190 | return OS; |
| 191 | } |
| 192 | inline std::ostream &operator<<(std::ostream &OS, const VirtRegMap &VRM) { |
| 193 | VRM.print(OS); |
| 194 | return OS; |
| 195 | } |
| 196 | |
| 197 | /// Spiller interface: Implementations of this interface assign spilled |
| 198 | /// virtual registers to stack slots, rewriting the code. |
| 199 | struct Spiller { |
| 200 | virtual ~Spiller(); |
| 201 | virtual bool runOnMachineFunction(MachineFunction &MF, |
| 202 | VirtRegMap &VRM) = 0; |
| 203 | }; |
| 204 | |
| 205 | /// createSpiller - Create an return a spiller object, as specified on the |
| 206 | /// command line. |
| 207 | Spiller* createSpiller(); |
| 208 | |
| 209 | } // End llvm namespace |
| 210 | |
| 211 | #endif |