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