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 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Alkis Evlogimenos | 34d9bc9 | 2004-02-23 23:08:11 +0000 | [diff] [blame] | 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 | |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame^] | 20 | #include "llvm/Target/TargetRegisterInfo.h" |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/DenseMap.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; |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 53 | |
Alkis Evlogimenos | c736b3a | 2004-10-01 00:35:07 +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. |
Chris Lattner | 94c002a | 2007-02-01 05:32:05 +0000 | [diff] [blame] | 58 | IndexedMap<int, VirtReg2IndexFunctor> Virt2StackSlotMap; |
Evan Cheng | 81a0382 | 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 | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 63 | IndexedMap<int, VirtReg2IndexFunctor> Virt2ReMatIdMap; |
Evan Cheng | 81a0382 | 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 | |
Evan Cheng | adf8590 | 2007-12-05 09:51:10 +0000 | [diff] [blame] | 69 | /// Virt2SplitKillMap - This is splitted virtual register to its last use |
Evan Cheng | d120ffd | 2007-12-05 10:24:35 +0000 | [diff] [blame] | 70 | /// (kill) index mapping. |
| 71 | IndexedMap<unsigned> Virt2SplitKillMap; |
Evan Cheng | adf8590 | 2007-12-05 09:51:10 +0000 | [diff] [blame] | 72 | |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 73 | /// ReMatMap - This is virtual register to re-materialized instruction |
| 74 | /// mapping. Each virtual register whose definition is going to be |
| 75 | /// re-materialized has an entry in it. |
| 76 | IndexedMap<MachineInstr*, VirtReg2IndexFunctor> ReMatMap; |
| 77 | |
Alkis Evlogimenos | c736b3a | 2004-10-01 00:35:07 +0000 | [diff] [blame] | 78 | /// MI2VirtMap - This is MachineInstr to virtual register |
| 79 | /// mapping. In the case of memory spill code being folded into |
| 80 | /// instructions, we need to know which virtual register was |
| 81 | /// read/written by this instruction. |
Chris Lattner | 7f690e6 | 2004-09-30 02:15:18 +0000 | [diff] [blame] | 82 | MI2VirtMapTy MI2VirtMap; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 83 | |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 84 | /// SpillPt2VirtMap - This records the virtual registers which should |
| 85 | /// be spilled right after the MachineInstr due to live interval |
| 86 | /// splitting. |
Evan Cheng | b50bb8c | 2007-12-05 08:16:32 +0000 | [diff] [blame] | 87 | std::map<MachineInstr*, std::vector<std::pair<unsigned,bool> > > |
| 88 | SpillPt2VirtMap; |
Evan Cheng | 2638e1a | 2007-03-20 08:13:50 +0000 | [diff] [blame] | 89 | |
Evan Cheng | 0cbb116 | 2007-11-29 01:06:25 +0000 | [diff] [blame] | 90 | /// RestorePt2VirtMap - This records the virtual registers which should |
| 91 | /// be restored right before the MachineInstr due to live interval |
| 92 | /// splitting. |
| 93 | std::map<MachineInstr*, std::vector<unsigned> > RestorePt2VirtMap; |
| 94 | |
Evan Cheng | 2638e1a | 2007-03-20 08:13:50 +0000 | [diff] [blame] | 95 | /// ReMatId - Instead of assigning a stack slot to a to be rematerialized |
Evan Cheng | 9193514 | 2007-04-04 07:40:01 +0000 | [diff] [blame] | 96 | /// virtual register, an unique id is being assigned. This keeps track of |
Evan Cheng | 2638e1a | 2007-03-20 08:13:50 +0000 | [diff] [blame] | 97 | /// the highest id used so far. Note, this starts at (1<<18) to avoid |
| 98 | /// conflicts with stack slot numbers. |
| 99 | int ReMatId; |
| 100 | |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 101 | VirtRegMap(const VirtRegMap&); // DO NOT IMPLEMENT |
| 102 | void operator=(const VirtRegMap&); // DO NOT IMPLEMENT |
Alkis Evlogimenos | 7974287 | 2004-02-23 23:47:10 +0000 | [diff] [blame] | 103 | |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 104 | public: |
Dan Gohman | 61e729e | 2007-08-02 21:21:54 +0000 | [diff] [blame] | 105 | explicit VirtRegMap(MachineFunction &mf); |
Alkis Evlogimenos | 34d9bc9 | 2004-02-23 23:08:11 +0000 | [diff] [blame] | 106 | |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 107 | void grow(); |
Alkis Evlogimenos | dd420e0 | 2004-03-01 23:18:15 +0000 | [diff] [blame] | 108 | |
Alkis Evlogimenos | c736b3a | 2004-10-01 00:35:07 +0000 | [diff] [blame] | 109 | /// @brief returns true if the specified virtual register is |
| 110 | /// mapped to a physical register |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 111 | bool hasPhys(unsigned virtReg) const { |
| 112 | return getPhys(virtReg) != NO_PHYS_REG; |
| 113 | } |
Alkis Evlogimenos | dd420e0 | 2004-03-01 23:18:15 +0000 | [diff] [blame] | 114 | |
Alkis Evlogimenos | c736b3a | 2004-10-01 00:35:07 +0000 | [diff] [blame] | 115 | /// @brief returns the physical register mapped to the specified |
| 116 | /// virtual register |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 117 | unsigned getPhys(unsigned virtReg) const { |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame^] | 118 | assert(TargetRegisterInfo::isVirtualRegister(virtReg)); |
Chris Lattner | 7f690e6 | 2004-09-30 02:15:18 +0000 | [diff] [blame] | 119 | return Virt2PhysMap[virtReg]; |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 120 | } |
Alkis Evlogimenos | dd420e0 | 2004-03-01 23:18:15 +0000 | [diff] [blame] | 121 | |
Alkis Evlogimenos | c736b3a | 2004-10-01 00:35:07 +0000 | [diff] [blame] | 122 | /// @brief creates a mapping for the specified virtual register to |
| 123 | /// the specified physical register |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 124 | void assignVirt2Phys(unsigned virtReg, unsigned physReg) { |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame^] | 125 | assert(TargetRegisterInfo::isVirtualRegister(virtReg) && |
| 126 | TargetRegisterInfo::isPhysicalRegister(physReg)); |
Chris Lattner | 7f690e6 | 2004-09-30 02:15:18 +0000 | [diff] [blame] | 127 | assert(Virt2PhysMap[virtReg] == NO_PHYS_REG && |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 128 | "attempt to assign physical register to already mapped " |
| 129 | "virtual register"); |
Chris Lattner | 7f690e6 | 2004-09-30 02:15:18 +0000 | [diff] [blame] | 130 | Virt2PhysMap[virtReg] = physReg; |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Alkis Evlogimenos | c736b3a | 2004-10-01 00:35:07 +0000 | [diff] [blame] | 133 | /// @brief clears the specified virtual register's, physical |
| 134 | /// register mapping |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 135 | void clearVirt(unsigned virtReg) { |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame^] | 136 | assert(TargetRegisterInfo::isVirtualRegister(virtReg)); |
Chris Lattner | 7f690e6 | 2004-09-30 02:15:18 +0000 | [diff] [blame] | 137 | assert(Virt2PhysMap[virtReg] != NO_PHYS_REG && |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 138 | "attempt to clear a not assigned virtual register"); |
Chris Lattner | 7f690e6 | 2004-09-30 02:15:18 +0000 | [diff] [blame] | 139 | Virt2PhysMap[virtReg] = NO_PHYS_REG; |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Alkis Evlogimenos | c736b3a | 2004-10-01 00:35:07 +0000 | [diff] [blame] | 142 | /// @brief clears all virtual to physical register mappings |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 143 | void clearAllVirt() { |
Chris Lattner | 7f690e6 | 2004-09-30 02:15:18 +0000 | [diff] [blame] | 144 | Virt2PhysMap.clear(); |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 145 | grow(); |
| 146 | } |
| 147 | |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 148 | /// @brief records virtReg is a split live interval from SReg. |
| 149 | void setIsSplitFromReg(unsigned virtReg, unsigned SReg) { |
| 150 | Virt2SplitMap[virtReg] = SReg; |
| 151 | } |
| 152 | |
| 153 | /// @brief returns the live interval virtReg is split from. |
| 154 | unsigned getPreSplitReg(unsigned virtReg) { |
| 155 | return Virt2SplitMap[virtReg]; |
| 156 | } |
| 157 | |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 158 | /// @brief returns true is the specified virtual register is not |
| 159 | /// mapped to a stack slot or rematerialized. |
| 160 | bool isAssignedReg(unsigned virtReg) const { |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 161 | if (getStackSlot(virtReg) == NO_STACK_SLOT && |
| 162 | getReMatId(virtReg) == NO_STACK_SLOT) |
| 163 | return true; |
| 164 | // Split register can be assigned a physical register as well as a |
| 165 | // stack slot or remat id. |
| 166 | return (Virt2SplitMap[virtReg] && Virt2PhysMap[virtReg] != NO_PHYS_REG); |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Alkis Evlogimenos | c736b3a | 2004-10-01 00:35:07 +0000 | [diff] [blame] | 169 | /// @brief returns the stack slot mapped to the specified virtual |
| 170 | /// register |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 171 | int getStackSlot(unsigned virtReg) const { |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame^] | 172 | assert(TargetRegisterInfo::isVirtualRegister(virtReg)); |
Chris Lattner | 7f690e6 | 2004-09-30 02:15:18 +0000 | [diff] [blame] | 173 | return Virt2StackSlotMap[virtReg]; |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 176 | /// @brief returns the rematerialization id mapped to the specified virtual |
| 177 | /// register |
| 178 | int getReMatId(unsigned virtReg) const { |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame^] | 179 | assert(TargetRegisterInfo::isVirtualRegister(virtReg)); |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 180 | return Virt2ReMatIdMap[virtReg]; |
| 181 | } |
| 182 | |
Alkis Evlogimenos | c736b3a | 2004-10-01 00:35:07 +0000 | [diff] [blame] | 183 | /// @brief create a mapping for the specifed virtual register to |
| 184 | /// the next available stack slot |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 185 | int assignVirt2StackSlot(unsigned virtReg); |
Alkis Evlogimenos | c736b3a | 2004-10-01 00:35:07 +0000 | [diff] [blame] | 186 | /// @brief create a mapping for the specified virtual register to |
| 187 | /// the specified stack slot |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 188 | void assignVirt2StackSlot(unsigned virtReg, int frameIndex); |
| 189 | |
Evan Cheng | 2638e1a | 2007-03-20 08:13:50 +0000 | [diff] [blame] | 190 | /// @brief assign an unique re-materialization id to the specified |
| 191 | /// virtual register. |
| 192 | int assignVirtReMatId(unsigned virtReg); |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 193 | /// @brief assign an unique re-materialization id to the specified |
| 194 | /// virtual register. |
| 195 | void assignVirtReMatId(unsigned virtReg, int id); |
Evan Cheng | 2638e1a | 2007-03-20 08:13:50 +0000 | [diff] [blame] | 196 | |
| 197 | /// @brief returns true if the specified virtual register is being |
| 198 | /// re-materialized. |
| 199 | bool isReMaterialized(unsigned virtReg) const { |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 200 | return ReMatMap[virtReg] != NULL; |
Evan Cheng | 2638e1a | 2007-03-20 08:13:50 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | /// @brief returns the original machine instruction being re-issued |
| 204 | /// to re-materialize the specified virtual register. |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 205 | MachineInstr *getReMaterializedMI(unsigned virtReg) const { |
Evan Cheng | 2638e1a | 2007-03-20 08:13:50 +0000 | [diff] [blame] | 206 | return ReMatMap[virtReg]; |
| 207 | } |
| 208 | |
| 209 | /// @brief records the specified virtual register will be |
| 210 | /// re-materialized and the original instruction which will be re-issed |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 211 | /// for this purpose. If parameter all is true, then all uses of the |
| 212 | /// registers are rematerialized and it's safe to delete the definition. |
Evan Cheng | 2638e1a | 2007-03-20 08:13:50 +0000 | [diff] [blame] | 213 | void setVirtIsReMaterialized(unsigned virtReg, MachineInstr *def) { |
| 214 | ReMatMap[virtReg] = def; |
| 215 | } |
| 216 | |
Evan Cheng | adf8590 | 2007-12-05 09:51:10 +0000 | [diff] [blame] | 217 | /// @brief record the last use (kill) of a split virtual register. |
Evan Cheng | d120ffd | 2007-12-05 10:24:35 +0000 | [diff] [blame] | 218 | void addKillPoint(unsigned virtReg, unsigned index) { |
| 219 | Virt2SplitKillMap[virtReg] = index; |
Evan Cheng | adf8590 | 2007-12-05 09:51:10 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Evan Cheng | d120ffd | 2007-12-05 10:24:35 +0000 | [diff] [blame] | 222 | unsigned getKillPoint(unsigned virtReg) const { |
| 223 | return Virt2SplitKillMap[virtReg]; |
| 224 | } |
| 225 | |
| 226 | /// @brief remove the last use (kill) of a split virtual register. |
Evan Cheng | adf8590 | 2007-12-05 09:51:10 +0000 | [diff] [blame] | 227 | void removeKillPoint(unsigned virtReg) { |
Evan Cheng | d120ffd | 2007-12-05 10:24:35 +0000 | [diff] [blame] | 228 | Virt2SplitKillMap[virtReg] = 0; |
Evan Cheng | adf8590 | 2007-12-05 09:51:10 +0000 | [diff] [blame] | 229 | } |
| 230 | |
Evan Cheng | cada245 | 2007-11-28 01:28:46 +0000 | [diff] [blame] | 231 | /// @brief returns true if the specified MachineInstr is a spill point. |
| 232 | bool isSpillPt(MachineInstr *Pt) const { |
| 233 | return SpillPt2VirtMap.find(Pt) != SpillPt2VirtMap.end(); |
| 234 | } |
| 235 | |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 236 | /// @brief returns the virtual registers that should be spilled due to |
| 237 | /// splitting right after the specified MachineInstr. |
Evan Cheng | b50bb8c | 2007-12-05 08:16:32 +0000 | [diff] [blame] | 238 | std::vector<std::pair<unsigned,bool> > &getSpillPtSpills(MachineInstr *Pt) { |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 239 | return SpillPt2VirtMap[Pt]; |
| 240 | } |
| 241 | |
| 242 | /// @brief records the specified MachineInstr as a spill point for virtReg. |
Evan Cheng | b50bb8c | 2007-12-05 08:16:32 +0000 | [diff] [blame] | 243 | void addSpillPoint(unsigned virtReg, bool isKill, MachineInstr *Pt) { |
Evan Cheng | cada245 | 2007-11-28 01:28:46 +0000 | [diff] [blame] | 244 | if (SpillPt2VirtMap.find(Pt) != SpillPt2VirtMap.end()) |
Evan Cheng | b50bb8c | 2007-12-05 08:16:32 +0000 | [diff] [blame] | 245 | SpillPt2VirtMap[Pt].push_back(std::make_pair(virtReg, isKill)); |
Evan Cheng | cada245 | 2007-11-28 01:28:46 +0000 | [diff] [blame] | 246 | else { |
Evan Cheng | b50bb8c | 2007-12-05 08:16:32 +0000 | [diff] [blame] | 247 | std::vector<std::pair<unsigned,bool> > Virts; |
| 248 | Virts.push_back(std::make_pair(virtReg, isKill)); |
Evan Cheng | cada245 | 2007-11-28 01:28:46 +0000 | [diff] [blame] | 249 | SpillPt2VirtMap.insert(std::make_pair(Pt, Virts)); |
| 250 | } |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | void transferSpillPts(MachineInstr *Old, MachineInstr *New) { |
Evan Cheng | b50bb8c | 2007-12-05 08:16:32 +0000 | [diff] [blame] | 254 | std::map<MachineInstr*,std::vector<std::pair<unsigned,bool> > >::iterator |
| 255 | I = SpillPt2VirtMap.find(Old); |
Evan Cheng | cada245 | 2007-11-28 01:28:46 +0000 | [diff] [blame] | 256 | if (I == SpillPt2VirtMap.end()) |
| 257 | return; |
| 258 | while (!I->second.empty()) { |
Evan Cheng | b50bb8c | 2007-12-05 08:16:32 +0000 | [diff] [blame] | 259 | unsigned virtReg = I->second.back().first; |
| 260 | bool isKill = I->second.back().second; |
Evan Cheng | cada245 | 2007-11-28 01:28:46 +0000 | [diff] [blame] | 261 | I->second.pop_back(); |
Evan Cheng | b50bb8c | 2007-12-05 08:16:32 +0000 | [diff] [blame] | 262 | addSpillPoint(virtReg, isKill, New); |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 263 | } |
Evan Cheng | cada245 | 2007-11-28 01:28:46 +0000 | [diff] [blame] | 264 | SpillPt2VirtMap.erase(I); |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 265 | } |
| 266 | |
Evan Cheng | 0cbb116 | 2007-11-29 01:06:25 +0000 | [diff] [blame] | 267 | /// @brief returns true if the specified MachineInstr is a restore point. |
| 268 | bool isRestorePt(MachineInstr *Pt) const { |
| 269 | return RestorePt2VirtMap.find(Pt) != RestorePt2VirtMap.end(); |
| 270 | } |
| 271 | |
| 272 | /// @brief returns the virtual registers that should be restoreed due to |
| 273 | /// splitting right after the specified MachineInstr. |
| 274 | std::vector<unsigned> &getRestorePtRestores(MachineInstr *Pt) { |
| 275 | return RestorePt2VirtMap[Pt]; |
| 276 | } |
| 277 | |
| 278 | /// @brief records the specified MachineInstr as a restore point for virtReg. |
| 279 | void addRestorePoint(unsigned virtReg, MachineInstr *Pt) { |
| 280 | if (RestorePt2VirtMap.find(Pt) != RestorePt2VirtMap.end()) |
| 281 | RestorePt2VirtMap[Pt].push_back(virtReg); |
| 282 | else { |
| 283 | std::vector<unsigned> Virts; |
| 284 | Virts.push_back(virtReg); |
| 285 | RestorePt2VirtMap.insert(std::make_pair(Pt, Virts)); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | void transferRestorePts(MachineInstr *Old, MachineInstr *New) { |
| 290 | std::map<MachineInstr*,std::vector<unsigned> >::iterator I = |
| 291 | RestorePt2VirtMap.find(Old); |
| 292 | if (I == RestorePt2VirtMap.end()) |
| 293 | return; |
| 294 | while (!I->second.empty()) { |
| 295 | unsigned virtReg = I->second.back(); |
| 296 | I->second.pop_back(); |
| 297 | addRestorePoint(virtReg, New); |
| 298 | } |
| 299 | RestorePt2VirtMap.erase(I); |
| 300 | } |
| 301 | |
Chris Lattner | bec6a9e | 2004-10-01 23:15:36 +0000 | [diff] [blame] | 302 | /// @brief Updates information about the specified virtual register's value |
Evan Cheng | aee4af6 | 2007-12-02 08:30:39 +0000 | [diff] [blame] | 303 | /// folded into newMI machine instruction. |
| 304 | void virtFolded(unsigned VirtReg, MachineInstr *OldMI, MachineInstr *NewMI, |
| 305 | ModRef MRInfo); |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 306 | |
Evan Cheng | 7f56625 | 2007-10-13 02:50:24 +0000 | [diff] [blame] | 307 | /// @brief Updates information about the specified virtual register's value |
| 308 | /// folded into the specified machine instruction. |
| 309 | void virtFolded(unsigned VirtReg, MachineInstr *MI, ModRef MRInfo); |
| 310 | |
Alkis Evlogimenos | c736b3a | 2004-10-01 00:35:07 +0000 | [diff] [blame] | 311 | /// @brief returns the virtual registers' values folded in memory |
| 312 | /// operands of this instruction |
Chris Lattner | 7f690e6 | 2004-09-30 02:15:18 +0000 | [diff] [blame] | 313 | std::pair<MI2VirtMapTy::const_iterator, MI2VirtMapTy::const_iterator> |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 314 | getFoldedVirts(MachineInstr* MI) const { |
Chris Lattner | 7f690e6 | 2004-09-30 02:15:18 +0000 | [diff] [blame] | 315 | return MI2VirtMap.equal_range(MI); |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 316 | } |
Chris Lattner | 35f2705 | 2006-05-01 21:16:03 +0000 | [diff] [blame] | 317 | |
Evan Cheng | cada245 | 2007-11-28 01:28:46 +0000 | [diff] [blame] | 318 | /// RemoveMachineInstrFromMaps - MI is being erased, remove it from the |
| 319 | /// the folded instruction map and spill point map. |
| 320 | void RemoveMachineInstrFromMaps(MachineInstr *MI) { |
Chris Lattner | 229924a | 2006-05-01 22:03:24 +0000 | [diff] [blame] | 321 | MI2VirtMap.erase(MI); |
Evan Cheng | cada245 | 2007-11-28 01:28:46 +0000 | [diff] [blame] | 322 | SpillPt2VirtMap.erase(MI); |
Evan Cheng | 0cbb116 | 2007-11-29 01:06:25 +0000 | [diff] [blame] | 323 | RestorePt2VirtMap.erase(MI); |
Chris Lattner | 35f2705 | 2006-05-01 21:16:03 +0000 | [diff] [blame] | 324 | } |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 325 | |
| 326 | void print(std::ostream &OS) const; |
Bill Wendling | 5c7e326 | 2006-12-17 05:15:13 +0000 | [diff] [blame] | 327 | void print(std::ostream *OS) const { if (OS) print(*OS); } |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 328 | void dump() const; |
| 329 | }; |
| 330 | |
Bill Wendling | 5c7e326 | 2006-12-17 05:15:13 +0000 | [diff] [blame] | 331 | inline std::ostream *operator<<(std::ostream *OS, const VirtRegMap &VRM) { |
| 332 | VRM.print(OS); |
| 333 | return OS; |
| 334 | } |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 335 | inline std::ostream &operator<<(std::ostream &OS, const VirtRegMap &VRM) { |
| 336 | VRM.print(OS); |
| 337 | return OS; |
| 338 | } |
| 339 | |
| 340 | /// Spiller interface: Implementations of this interface assign spilled |
| 341 | /// virtual registers to stack slots, rewriting the code. |
| 342 | struct Spiller { |
| 343 | virtual ~Spiller(); |
| 344 | virtual bool runOnMachineFunction(MachineFunction &MF, |
Chris Lattner | 35f2705 | 2006-05-01 21:16:03 +0000 | [diff] [blame] | 345 | VirtRegMap &VRM) = 0; |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 346 | }; |
| 347 | |
| 348 | /// createSpiller - Create an return a spiller object, as specified on the |
| 349 | /// command line. |
| 350 | Spiller* createSpiller(); |
Alkis Evlogimenos | 0d6c5b6 | 2004-02-24 08:58:30 +0000 | [diff] [blame] | 351 | |
Alkis Evlogimenos | 34d9bc9 | 2004-02-23 23:08:11 +0000 | [diff] [blame] | 352 | } // End llvm namespace |
| 353 | |
| 354 | #endif |