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 | 4cce6b4 | 2008-04-11 17:53:36 +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" |
Evan Cheng | d365312 | 2008-02-27 03:04:06 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/SmallPtrSet.h" |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Streams.h" |
Alkis Evlogimenos | 5f37502 | 2004-03-01 20:05:10 +0000 | [diff] [blame] | 25 | #include <map> |
Alkis Evlogimenos | 34d9bc9 | 2004-02-23 23:08:11 +0000 | [diff] [blame] | 26 | |
| 27 | namespace llvm { |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 28 | class MachineInstr; |
David Greene | 7e23146 | 2007-08-07 16:34:05 +0000 | [diff] [blame] | 29 | class MachineFunction; |
Chris Lattner | 2926869 | 2006-09-05 02:12:02 +0000 | [diff] [blame] | 30 | class TargetInstrInfo; |
Alkis Evlogimenos | 34d9bc9 | 2004-02-23 23:08:11 +0000 | [diff] [blame] | 31 | |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 32 | class VirtRegMap { |
| 33 | public: |
Evan Cheng | 2638e1a | 2007-03-20 08:13:50 +0000 | [diff] [blame] | 34 | enum { |
| 35 | NO_PHYS_REG = 0, |
Evan Cheng | 9193514 | 2007-04-04 07:40:01 +0000 | [diff] [blame] | 36 | NO_STACK_SLOT = (1L << 30)-1, |
| 37 | MAX_STACK_SLOT = (1L << 18)-1 |
Evan Cheng | 2638e1a | 2007-03-20 08:13:50 +0000 | [diff] [blame] | 38 | }; |
| 39 | |
Chris Lattner | 35f2705 | 2006-05-01 21:16:03 +0000 | [diff] [blame] | 40 | enum ModRef { isRef = 1, isMod = 2, isModRef = 3 }; |
Chris Lattner | bec6a9e | 2004-10-01 23:15:36 +0000 | [diff] [blame] | 41 | typedef std::multimap<MachineInstr*, |
| 42 | std::pair<unsigned, ModRef> > MI2VirtMapTy; |
Alkis Evlogimenos | 5f37502 | 2004-03-01 20:05:10 +0000 | [diff] [blame] | 43 | |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 44 | private: |
Chris Lattner | 2926869 | 2006-09-05 02:12:02 +0000 | [diff] [blame] | 45 | const TargetInstrInfo &TII; |
| 46 | |
Chris Lattner | 7f690e6 | 2004-09-30 02:15:18 +0000 | [diff] [blame] | 47 | MachineFunction &MF; |
Alkis Evlogimenos | c736b3a | 2004-10-01 00:35:07 +0000 | [diff] [blame] | 48 | /// Virt2PhysMap - This is a virtual to physical register |
| 49 | /// mapping. Each virtual register is required to have an entry in |
| 50 | /// it; even spilled virtual registers (the register mapped to a |
| 51 | /// spilled register is the temporary used to load it from the |
| 52 | /// stack). |
Chris Lattner | 94c002a | 2007-02-01 05:32:05 +0000 | [diff] [blame] | 53 | IndexedMap<unsigned, VirtReg2IndexFunctor> Virt2PhysMap; |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 54 | |
Alkis Evlogimenos | c736b3a | 2004-10-01 00:35:07 +0000 | [diff] [blame] | 55 | /// Virt2StackSlotMap - This is virtual register to stack slot |
| 56 | /// mapping. Each spilled virtual register has an entry in it |
| 57 | /// which corresponds to the stack slot this register is spilled |
| 58 | /// at. |
Chris Lattner | 94c002a | 2007-02-01 05:32:05 +0000 | [diff] [blame] | 59 | IndexedMap<int, VirtReg2IndexFunctor> Virt2StackSlotMap; |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 60 | |
Dan Gohman | 39e33ac | 2008-03-12 20:50:04 +0000 | [diff] [blame] | 61 | /// Virt2ReMatIdMap - This is virtual register to rematerialization id |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 62 | /// mapping. Each spilled virtual register that should be remat'd has an |
| 63 | /// entry in it which corresponds to the remat id. |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 64 | IndexedMap<int, VirtReg2IndexFunctor> Virt2ReMatIdMap; |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 65 | |
| 66 | /// Virt2SplitMap - This is virtual register to splitted virtual register |
| 67 | /// mapping. |
| 68 | IndexedMap<unsigned, VirtReg2IndexFunctor> Virt2SplitMap; |
| 69 | |
Evan Cheng | adf8590 | 2007-12-05 09:51:10 +0000 | [diff] [blame] | 70 | /// Virt2SplitKillMap - This is splitted virtual register to its last use |
Evan Cheng | d120ffd | 2007-12-05 10:24:35 +0000 | [diff] [blame] | 71 | /// (kill) index mapping. |
| 72 | IndexedMap<unsigned> Virt2SplitKillMap; |
Evan Cheng | adf8590 | 2007-12-05 09:51:10 +0000 | [diff] [blame] | 73 | |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 74 | /// ReMatMap - This is virtual register to re-materialized instruction |
| 75 | /// mapping. Each virtual register whose definition is going to be |
| 76 | /// re-materialized has an entry in it. |
| 77 | IndexedMap<MachineInstr*, VirtReg2IndexFunctor> ReMatMap; |
| 78 | |
Alkis Evlogimenos | c736b3a | 2004-10-01 00:35:07 +0000 | [diff] [blame] | 79 | /// MI2VirtMap - This is MachineInstr to virtual register |
| 80 | /// mapping. In the case of memory spill code being folded into |
| 81 | /// instructions, we need to know which virtual register was |
| 82 | /// read/written by this instruction. |
Chris Lattner | 7f690e6 | 2004-09-30 02:15:18 +0000 | [diff] [blame] | 83 | MI2VirtMapTy MI2VirtMap; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 84 | |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 85 | /// SpillPt2VirtMap - This records the virtual registers which should |
| 86 | /// be spilled right after the MachineInstr due to live interval |
| 87 | /// splitting. |
Evan Cheng | b50bb8c | 2007-12-05 08:16:32 +0000 | [diff] [blame] | 88 | std::map<MachineInstr*, std::vector<std::pair<unsigned,bool> > > |
| 89 | SpillPt2VirtMap; |
Evan Cheng | 2638e1a | 2007-03-20 08:13:50 +0000 | [diff] [blame] | 90 | |
Evan Cheng | 0cbb116 | 2007-11-29 01:06:25 +0000 | [diff] [blame] | 91 | /// RestorePt2VirtMap - This records the virtual registers which should |
| 92 | /// be restored right before the MachineInstr due to live interval |
| 93 | /// splitting. |
| 94 | std::map<MachineInstr*, std::vector<unsigned> > RestorePt2VirtMap; |
| 95 | |
Evan Cheng | 676dd7c | 2008-03-11 07:19:34 +0000 | [diff] [blame] | 96 | /// EmergencySpillMap - This records the physical registers that should |
| 97 | /// be spilled / restored around the MachineInstr since the register |
| 98 | /// allocator has run out of registers. |
| 99 | std::map<MachineInstr*, std::vector<unsigned> > EmergencySpillMap; |
| 100 | |
| 101 | /// EmergencySpillSlots - This records emergency spill slots used to |
| 102 | /// spill physical registers when the register allocator runs out of |
| 103 | /// registers. Ideally only one stack slot is used per function per |
| 104 | /// register class. |
| 105 | std::map<const TargetRegisterClass*, int> EmergencySpillSlots; |
| 106 | |
Evan Cheng | 2638e1a | 2007-03-20 08:13:50 +0000 | [diff] [blame] | 107 | /// ReMatId - Instead of assigning a stack slot to a to be rematerialized |
Evan Cheng | 9193514 | 2007-04-04 07:40:01 +0000 | [diff] [blame] | 108 | /// virtual register, an unique id is being assigned. This keeps track of |
Evan Cheng | 2638e1a | 2007-03-20 08:13:50 +0000 | [diff] [blame] | 109 | /// the highest id used so far. Note, this starts at (1<<18) to avoid |
| 110 | /// conflicts with stack slot numbers. |
| 111 | int ReMatId; |
| 112 | |
Evan Cheng | d365312 | 2008-02-27 03:04:06 +0000 | [diff] [blame] | 113 | /// LowSpillSlot, HighSpillSlot - Lowest and highest spill slot indexes. |
| 114 | int LowSpillSlot, HighSpillSlot; |
| 115 | |
| 116 | /// SpillSlotToUsesMap - Records uses for each register spill slot. |
| 117 | SmallVector<SmallPtrSet<MachineInstr*, 4>, 8> SpillSlotToUsesMap; |
| 118 | |
Evan Cheng | 4cce6b4 | 2008-04-11 17:53:36 +0000 | [diff] [blame] | 119 | /// ImplicitDefed - One bit for each virtual register. If set it indicates |
| 120 | /// the register is implicitly defined. |
| 121 | BitVector ImplicitDefed; |
| 122 | |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 123 | VirtRegMap(const VirtRegMap&); // DO NOT IMPLEMENT |
| 124 | void operator=(const VirtRegMap&); // DO NOT IMPLEMENT |
Alkis Evlogimenos | 7974287 | 2004-02-23 23:47:10 +0000 | [diff] [blame] | 125 | |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 126 | public: |
Dan Gohman | 61e729e | 2007-08-02 21:21:54 +0000 | [diff] [blame] | 127 | explicit VirtRegMap(MachineFunction &mf); |
Alkis Evlogimenos | 34d9bc9 | 2004-02-23 23:08:11 +0000 | [diff] [blame] | 128 | |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 129 | void grow(); |
Alkis Evlogimenos | dd420e0 | 2004-03-01 23:18:15 +0000 | [diff] [blame] | 130 | |
Alkis Evlogimenos | c736b3a | 2004-10-01 00:35:07 +0000 | [diff] [blame] | 131 | /// @brief returns true if the specified virtual register is |
| 132 | /// mapped to a physical register |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 133 | bool hasPhys(unsigned virtReg) const { |
| 134 | return getPhys(virtReg) != NO_PHYS_REG; |
| 135 | } |
Alkis Evlogimenos | dd420e0 | 2004-03-01 23:18:15 +0000 | [diff] [blame] | 136 | |
Alkis Evlogimenos | c736b3a | 2004-10-01 00:35:07 +0000 | [diff] [blame] | 137 | /// @brief returns the physical register mapped to the specified |
| 138 | /// virtual register |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 139 | unsigned getPhys(unsigned virtReg) const { |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 140 | assert(TargetRegisterInfo::isVirtualRegister(virtReg)); |
Chris Lattner | 7f690e6 | 2004-09-30 02:15:18 +0000 | [diff] [blame] | 141 | return Virt2PhysMap[virtReg]; |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 142 | } |
Alkis Evlogimenos | dd420e0 | 2004-03-01 23:18:15 +0000 | [diff] [blame] | 143 | |
Alkis Evlogimenos | c736b3a | 2004-10-01 00:35:07 +0000 | [diff] [blame] | 144 | /// @brief creates a mapping for the specified virtual register to |
| 145 | /// the specified physical register |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 146 | void assignVirt2Phys(unsigned virtReg, unsigned physReg) { |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 147 | assert(TargetRegisterInfo::isVirtualRegister(virtReg) && |
| 148 | TargetRegisterInfo::isPhysicalRegister(physReg)); |
Chris Lattner | 7f690e6 | 2004-09-30 02:15:18 +0000 | [diff] [blame] | 149 | assert(Virt2PhysMap[virtReg] == NO_PHYS_REG && |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 150 | "attempt to assign physical register to already mapped " |
| 151 | "virtual register"); |
Chris Lattner | 7f690e6 | 2004-09-30 02:15:18 +0000 | [diff] [blame] | 152 | Virt2PhysMap[virtReg] = physReg; |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Alkis Evlogimenos | c736b3a | 2004-10-01 00:35:07 +0000 | [diff] [blame] | 155 | /// @brief clears the specified virtual register's, physical |
| 156 | /// register mapping |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 157 | void clearVirt(unsigned virtReg) { |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 158 | assert(TargetRegisterInfo::isVirtualRegister(virtReg)); |
Chris Lattner | 7f690e6 | 2004-09-30 02:15:18 +0000 | [diff] [blame] | 159 | assert(Virt2PhysMap[virtReg] != NO_PHYS_REG && |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 160 | "attempt to clear a not assigned virtual register"); |
Chris Lattner | 7f690e6 | 2004-09-30 02:15:18 +0000 | [diff] [blame] | 161 | Virt2PhysMap[virtReg] = NO_PHYS_REG; |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Alkis Evlogimenos | c736b3a | 2004-10-01 00:35:07 +0000 | [diff] [blame] | 164 | /// @brief clears all virtual to physical register mappings |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 165 | void clearAllVirt() { |
Chris Lattner | 7f690e6 | 2004-09-30 02:15:18 +0000 | [diff] [blame] | 166 | Virt2PhysMap.clear(); |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 167 | grow(); |
| 168 | } |
| 169 | |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 170 | /// @brief records virtReg is a split live interval from SReg. |
| 171 | void setIsSplitFromReg(unsigned virtReg, unsigned SReg) { |
| 172 | Virt2SplitMap[virtReg] = SReg; |
| 173 | } |
| 174 | |
| 175 | /// @brief returns the live interval virtReg is split from. |
| 176 | unsigned getPreSplitReg(unsigned virtReg) { |
| 177 | return Virt2SplitMap[virtReg]; |
| 178 | } |
| 179 | |
Dan Gohman | 39e33ac | 2008-03-12 20:50:04 +0000 | [diff] [blame] | 180 | /// @brief returns true if the specified virtual register is not |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 181 | /// mapped to a stack slot or rematerialized. |
| 182 | bool isAssignedReg(unsigned virtReg) const { |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 183 | if (getStackSlot(virtReg) == NO_STACK_SLOT && |
| 184 | getReMatId(virtReg) == NO_STACK_SLOT) |
| 185 | return true; |
| 186 | // Split register can be assigned a physical register as well as a |
| 187 | // stack slot or remat id. |
| 188 | return (Virt2SplitMap[virtReg] && Virt2PhysMap[virtReg] != NO_PHYS_REG); |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Alkis Evlogimenos | c736b3a | 2004-10-01 00:35:07 +0000 | [diff] [blame] | 191 | /// @brief returns the stack slot mapped to the specified virtual |
| 192 | /// register |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 193 | int getStackSlot(unsigned virtReg) const { |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 194 | assert(TargetRegisterInfo::isVirtualRegister(virtReg)); |
Chris Lattner | 7f690e6 | 2004-09-30 02:15:18 +0000 | [diff] [blame] | 195 | return Virt2StackSlotMap[virtReg]; |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 198 | /// @brief returns the rematerialization id mapped to the specified virtual |
| 199 | /// register |
| 200 | int getReMatId(unsigned virtReg) const { |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 201 | assert(TargetRegisterInfo::isVirtualRegister(virtReg)); |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 202 | return Virt2ReMatIdMap[virtReg]; |
| 203 | } |
| 204 | |
Alkis Evlogimenos | c736b3a | 2004-10-01 00:35:07 +0000 | [diff] [blame] | 205 | /// @brief create a mapping for the specifed virtual register to |
| 206 | /// the next available stack slot |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 207 | int assignVirt2StackSlot(unsigned virtReg); |
Alkis Evlogimenos | c736b3a | 2004-10-01 00:35:07 +0000 | [diff] [blame] | 208 | /// @brief create a mapping for the specified virtual register to |
| 209 | /// the specified stack slot |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 210 | void assignVirt2StackSlot(unsigned virtReg, int frameIndex); |
| 211 | |
Evan Cheng | 2638e1a | 2007-03-20 08:13:50 +0000 | [diff] [blame] | 212 | /// @brief assign an unique re-materialization id to the specified |
| 213 | /// virtual register. |
| 214 | int assignVirtReMatId(unsigned virtReg); |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 215 | /// @brief assign an unique re-materialization id to the specified |
| 216 | /// virtual register. |
| 217 | void assignVirtReMatId(unsigned virtReg, int id); |
Evan Cheng | 2638e1a | 2007-03-20 08:13:50 +0000 | [diff] [blame] | 218 | |
| 219 | /// @brief returns true if the specified virtual register is being |
| 220 | /// re-materialized. |
| 221 | bool isReMaterialized(unsigned virtReg) const { |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 222 | return ReMatMap[virtReg] != NULL; |
Evan Cheng | 2638e1a | 2007-03-20 08:13:50 +0000 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | /// @brief returns the original machine instruction being re-issued |
| 226 | /// to re-materialize the specified virtual register. |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 227 | MachineInstr *getReMaterializedMI(unsigned virtReg) const { |
Evan Cheng | 2638e1a | 2007-03-20 08:13:50 +0000 | [diff] [blame] | 228 | return ReMatMap[virtReg]; |
| 229 | } |
| 230 | |
| 231 | /// @brief records the specified virtual register will be |
| 232 | /// re-materialized and the original instruction which will be re-issed |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 233 | /// for this purpose. If parameter all is true, then all uses of the |
| 234 | /// registers are rematerialized and it's safe to delete the definition. |
Evan Cheng | 2638e1a | 2007-03-20 08:13:50 +0000 | [diff] [blame] | 235 | void setVirtIsReMaterialized(unsigned virtReg, MachineInstr *def) { |
| 236 | ReMatMap[virtReg] = def; |
| 237 | } |
| 238 | |
Evan Cheng | adf8590 | 2007-12-05 09:51:10 +0000 | [diff] [blame] | 239 | /// @brief record the last use (kill) of a split virtual register. |
Evan Cheng | d120ffd | 2007-12-05 10:24:35 +0000 | [diff] [blame] | 240 | void addKillPoint(unsigned virtReg, unsigned index) { |
| 241 | Virt2SplitKillMap[virtReg] = index; |
Evan Cheng | adf8590 | 2007-12-05 09:51:10 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Evan Cheng | d120ffd | 2007-12-05 10:24:35 +0000 | [diff] [blame] | 244 | unsigned getKillPoint(unsigned virtReg) const { |
| 245 | return Virt2SplitKillMap[virtReg]; |
| 246 | } |
| 247 | |
| 248 | /// @brief remove the last use (kill) of a split virtual register. |
Evan Cheng | adf8590 | 2007-12-05 09:51:10 +0000 | [diff] [blame] | 249 | void removeKillPoint(unsigned virtReg) { |
Evan Cheng | d120ffd | 2007-12-05 10:24:35 +0000 | [diff] [blame] | 250 | Virt2SplitKillMap[virtReg] = 0; |
Evan Cheng | adf8590 | 2007-12-05 09:51:10 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Evan Cheng | cada245 | 2007-11-28 01:28:46 +0000 | [diff] [blame] | 253 | /// @brief returns true if the specified MachineInstr is a spill point. |
| 254 | bool isSpillPt(MachineInstr *Pt) const { |
| 255 | return SpillPt2VirtMap.find(Pt) != SpillPt2VirtMap.end(); |
| 256 | } |
| 257 | |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 258 | /// @brief returns the virtual registers that should be spilled due to |
| 259 | /// splitting right after the specified MachineInstr. |
Evan Cheng | b50bb8c | 2007-12-05 08:16:32 +0000 | [diff] [blame] | 260 | std::vector<std::pair<unsigned,bool> > &getSpillPtSpills(MachineInstr *Pt) { |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 261 | return SpillPt2VirtMap[Pt]; |
| 262 | } |
| 263 | |
| 264 | /// @brief records the specified MachineInstr as a spill point for virtReg. |
Evan Cheng | b50bb8c | 2007-12-05 08:16:32 +0000 | [diff] [blame] | 265 | void addSpillPoint(unsigned virtReg, bool isKill, MachineInstr *Pt) { |
Evan Cheng | cada245 | 2007-11-28 01:28:46 +0000 | [diff] [blame] | 266 | if (SpillPt2VirtMap.find(Pt) != SpillPt2VirtMap.end()) |
Evan Cheng | b50bb8c | 2007-12-05 08:16:32 +0000 | [diff] [blame] | 267 | SpillPt2VirtMap[Pt].push_back(std::make_pair(virtReg, isKill)); |
Evan Cheng | cada245 | 2007-11-28 01:28:46 +0000 | [diff] [blame] | 268 | else { |
Evan Cheng | b50bb8c | 2007-12-05 08:16:32 +0000 | [diff] [blame] | 269 | std::vector<std::pair<unsigned,bool> > Virts; |
| 270 | Virts.push_back(std::make_pair(virtReg, isKill)); |
Evan Cheng | cada245 | 2007-11-28 01:28:46 +0000 | [diff] [blame] | 271 | SpillPt2VirtMap.insert(std::make_pair(Pt, Virts)); |
| 272 | } |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 273 | } |
| 274 | |
Evan Cheng | c1f53c7 | 2008-03-11 21:34:46 +0000 | [diff] [blame] | 275 | /// @brief - transfer spill point information from one instruction to |
| 276 | /// another. |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 277 | void transferSpillPts(MachineInstr *Old, MachineInstr *New) { |
Evan Cheng | b50bb8c | 2007-12-05 08:16:32 +0000 | [diff] [blame] | 278 | std::map<MachineInstr*,std::vector<std::pair<unsigned,bool> > >::iterator |
| 279 | I = SpillPt2VirtMap.find(Old); |
Evan Cheng | cada245 | 2007-11-28 01:28:46 +0000 | [diff] [blame] | 280 | if (I == SpillPt2VirtMap.end()) |
| 281 | return; |
| 282 | while (!I->second.empty()) { |
Evan Cheng | b50bb8c | 2007-12-05 08:16:32 +0000 | [diff] [blame] | 283 | unsigned virtReg = I->second.back().first; |
| 284 | bool isKill = I->second.back().second; |
Evan Cheng | cada245 | 2007-11-28 01:28:46 +0000 | [diff] [blame] | 285 | I->second.pop_back(); |
Evan Cheng | b50bb8c | 2007-12-05 08:16:32 +0000 | [diff] [blame] | 286 | addSpillPoint(virtReg, isKill, New); |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 287 | } |
Evan Cheng | cada245 | 2007-11-28 01:28:46 +0000 | [diff] [blame] | 288 | SpillPt2VirtMap.erase(I); |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 289 | } |
| 290 | |
Evan Cheng | 0cbb116 | 2007-11-29 01:06:25 +0000 | [diff] [blame] | 291 | /// @brief returns true if the specified MachineInstr is a restore point. |
| 292 | bool isRestorePt(MachineInstr *Pt) const { |
| 293 | return RestorePt2VirtMap.find(Pt) != RestorePt2VirtMap.end(); |
| 294 | } |
| 295 | |
| 296 | /// @brief returns the virtual registers that should be restoreed due to |
| 297 | /// splitting right after the specified MachineInstr. |
| 298 | std::vector<unsigned> &getRestorePtRestores(MachineInstr *Pt) { |
| 299 | return RestorePt2VirtMap[Pt]; |
| 300 | } |
| 301 | |
| 302 | /// @brief records the specified MachineInstr as a restore point for virtReg. |
| 303 | void addRestorePoint(unsigned virtReg, MachineInstr *Pt) { |
| 304 | if (RestorePt2VirtMap.find(Pt) != RestorePt2VirtMap.end()) |
| 305 | RestorePt2VirtMap[Pt].push_back(virtReg); |
| 306 | else { |
| 307 | std::vector<unsigned> Virts; |
| 308 | Virts.push_back(virtReg); |
| 309 | RestorePt2VirtMap.insert(std::make_pair(Pt, Virts)); |
| 310 | } |
| 311 | } |
| 312 | |
Evan Cheng | 676dd7c | 2008-03-11 07:19:34 +0000 | [diff] [blame] | 313 | /// @brief - transfer restore point information from one instruction to |
| 314 | /// another. |
Evan Cheng | 0cbb116 | 2007-11-29 01:06:25 +0000 | [diff] [blame] | 315 | void transferRestorePts(MachineInstr *Old, MachineInstr *New) { |
| 316 | std::map<MachineInstr*,std::vector<unsigned> >::iterator I = |
| 317 | RestorePt2VirtMap.find(Old); |
| 318 | if (I == RestorePt2VirtMap.end()) |
| 319 | return; |
| 320 | while (!I->second.empty()) { |
| 321 | unsigned virtReg = I->second.back(); |
| 322 | I->second.pop_back(); |
| 323 | addRestorePoint(virtReg, New); |
| 324 | } |
| 325 | RestorePt2VirtMap.erase(I); |
| 326 | } |
| 327 | |
Evan Cheng | 676dd7c | 2008-03-11 07:19:34 +0000 | [diff] [blame] | 328 | /// @brief records that the specified physical register must be spilled |
| 329 | /// around the specified machine instr. |
| 330 | void addEmergencySpill(unsigned PhysReg, MachineInstr *MI) { |
| 331 | if (EmergencySpillMap.find(MI) != EmergencySpillMap.end()) |
| 332 | EmergencySpillMap[MI].push_back(PhysReg); |
| 333 | else { |
| 334 | std::vector<unsigned> PhysRegs; |
| 335 | PhysRegs.push_back(PhysReg); |
| 336 | EmergencySpillMap.insert(std::make_pair(MI, PhysRegs)); |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | /// @brief returns true if one or more physical registers must be spilled |
| 341 | /// around the specified instruction. |
| 342 | bool hasEmergencySpills(MachineInstr *MI) const { |
| 343 | return EmergencySpillMap.find(MI) != EmergencySpillMap.end(); |
| 344 | } |
| 345 | |
| 346 | /// @brief returns the physical registers to be spilled and restored around |
| 347 | /// the instruction. |
| 348 | std::vector<unsigned> &getEmergencySpills(MachineInstr *MI) { |
| 349 | return EmergencySpillMap[MI]; |
| 350 | } |
| 351 | |
Evan Cheng | c1f53c7 | 2008-03-11 21:34:46 +0000 | [diff] [blame] | 352 | /// @brief - transfer emergency spill information from one instruction to |
| 353 | /// another. |
| 354 | void transferEmergencySpills(MachineInstr *Old, MachineInstr *New) { |
| 355 | std::map<MachineInstr*,std::vector<unsigned> >::iterator I = |
| 356 | EmergencySpillMap.find(Old); |
| 357 | if (I == EmergencySpillMap.end()) |
| 358 | return; |
| 359 | while (!I->second.empty()) { |
| 360 | unsigned virtReg = I->second.back(); |
| 361 | I->second.pop_back(); |
| 362 | addEmergencySpill(virtReg, New); |
| 363 | } |
| 364 | EmergencySpillMap.erase(I); |
| 365 | } |
| 366 | |
Evan Cheng | 676dd7c | 2008-03-11 07:19:34 +0000 | [diff] [blame] | 367 | /// @brief return or get a emergency spill slot for the register class. |
| 368 | int getEmergencySpillSlot(const TargetRegisterClass *RC); |
| 369 | |
Evan Cheng | d365312 | 2008-02-27 03:04:06 +0000 | [diff] [blame] | 370 | /// @brief Return lowest spill slot index. |
| 371 | int getLowSpillSlot() const { |
| 372 | return LowSpillSlot; |
| 373 | } |
| 374 | |
| 375 | /// @brief Return highest spill slot index. |
| 376 | int getHighSpillSlot() const { |
| 377 | return HighSpillSlot; |
| 378 | } |
| 379 | |
| 380 | /// @brief Records a spill slot use. |
| 381 | void addSpillSlotUse(int FrameIndex, MachineInstr *MI); |
| 382 | |
| 383 | /// @brief Returns true if spill slot has been used. |
| 384 | bool isSpillSlotUsed(int FrameIndex) const { |
| 385 | assert(FrameIndex >= 0 && "Spill slot index should not be negative!"); |
| 386 | return !SpillSlotToUsesMap[FrameIndex-LowSpillSlot].empty(); |
| 387 | } |
| 388 | |
Evan Cheng | 4cce6b4 | 2008-04-11 17:53:36 +0000 | [diff] [blame] | 389 | /// @brief Mark the specified register as being implicitly defined. |
| 390 | void setIsImplicitlyDefined(unsigned VirtReg) { |
| 391 | ImplicitDefed.set(VirtReg-TargetRegisterInfo::FirstVirtualRegister); |
| 392 | } |
| 393 | |
| 394 | /// @brief Returns true if the virtual register is implicitly defined. |
| 395 | bool isImplicitlyDefined(unsigned VirtReg) const { |
| 396 | return ImplicitDefed[VirtReg-TargetRegisterInfo::FirstVirtualRegister]; |
| 397 | } |
| 398 | |
Chris Lattner | bec6a9e | 2004-10-01 23:15:36 +0000 | [diff] [blame] | 399 | /// @brief Updates information about the specified virtual register's value |
Evan Cheng | aee4af6 | 2007-12-02 08:30:39 +0000 | [diff] [blame] | 400 | /// folded into newMI machine instruction. |
| 401 | void virtFolded(unsigned VirtReg, MachineInstr *OldMI, MachineInstr *NewMI, |
| 402 | ModRef MRInfo); |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 403 | |
Evan Cheng | 7f56625 | 2007-10-13 02:50:24 +0000 | [diff] [blame] | 404 | /// @brief Updates information about the specified virtual register's value |
| 405 | /// folded into the specified machine instruction. |
| 406 | void virtFolded(unsigned VirtReg, MachineInstr *MI, ModRef MRInfo); |
| 407 | |
Alkis Evlogimenos | c736b3a | 2004-10-01 00:35:07 +0000 | [diff] [blame] | 408 | /// @brief returns the virtual registers' values folded in memory |
| 409 | /// operands of this instruction |
Chris Lattner | 7f690e6 | 2004-09-30 02:15:18 +0000 | [diff] [blame] | 410 | std::pair<MI2VirtMapTy::const_iterator, MI2VirtMapTy::const_iterator> |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 411 | getFoldedVirts(MachineInstr* MI) const { |
Chris Lattner | 7f690e6 | 2004-09-30 02:15:18 +0000 | [diff] [blame] | 412 | return MI2VirtMap.equal_range(MI); |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 413 | } |
Chris Lattner | 35f2705 | 2006-05-01 21:16:03 +0000 | [diff] [blame] | 414 | |
Evan Cheng | cada245 | 2007-11-28 01:28:46 +0000 | [diff] [blame] | 415 | /// RemoveMachineInstrFromMaps - MI is being erased, remove it from the |
| 416 | /// the folded instruction map and spill point map. |
Evan Cheng | d365312 | 2008-02-27 03:04:06 +0000 | [diff] [blame] | 417 | void RemoveMachineInstrFromMaps(MachineInstr *MI); |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 418 | |
| 419 | void print(std::ostream &OS) const; |
Bill Wendling | 5c7e326 | 2006-12-17 05:15:13 +0000 | [diff] [blame] | 420 | void print(std::ostream *OS) const { if (OS) print(*OS); } |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 421 | void dump() const; |
| 422 | }; |
| 423 | |
Bill Wendling | 5c7e326 | 2006-12-17 05:15:13 +0000 | [diff] [blame] | 424 | inline std::ostream *operator<<(std::ostream *OS, const VirtRegMap &VRM) { |
| 425 | VRM.print(OS); |
| 426 | return OS; |
| 427 | } |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 428 | inline std::ostream &operator<<(std::ostream &OS, const VirtRegMap &VRM) { |
| 429 | VRM.print(OS); |
| 430 | return OS; |
| 431 | } |
| 432 | |
| 433 | /// Spiller interface: Implementations of this interface assign spilled |
| 434 | /// virtual registers to stack slots, rewriting the code. |
| 435 | struct Spiller { |
| 436 | virtual ~Spiller(); |
| 437 | virtual bool runOnMachineFunction(MachineFunction &MF, |
Chris Lattner | 35f2705 | 2006-05-01 21:16:03 +0000 | [diff] [blame] | 438 | VirtRegMap &VRM) = 0; |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 439 | }; |
| 440 | |
| 441 | /// createSpiller - Create an return a spiller object, as specified on the |
| 442 | /// command line. |
| 443 | Spiller* createSpiller(); |
Alkis Evlogimenos | 0d6c5b6 | 2004-02-24 08:58:30 +0000 | [diff] [blame] | 444 | |
Alkis Evlogimenos | 34d9bc9 | 2004-02-23 23:08:11 +0000 | [diff] [blame] | 445 | } // End llvm namespace |
| 446 | |
| 447 | #endif |