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