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