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" |
Chris Lattner | 94c002a | 2007-02-01 05:32:05 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/IndexedMap.h" |
Alkis Evlogimenos | 34d9bc9 | 2004-02-23 23:08:11 +0000 | [diff] [blame] | 23 | |
| 24 | namespace llvm { |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 25 | class MachineInstr; |
David Greene | 7e23146 | 2007-08-07 16:34:05 +0000 | [diff] [blame] | 26 | class MachineFunction; |
Evan Cheng | 90f95f8 | 2009-06-14 20:22:55 +0000 | [diff] [blame] | 27 | class MachineRegisterInfo; |
Chris Lattner | 2926869 | 2006-09-05 02:12:02 +0000 | [diff] [blame] | 28 | class TargetInstrInfo; |
Daniel Dunbar | 1cd1d98 | 2009-07-24 10:36:58 +0000 | [diff] [blame] | 29 | class raw_ostream; |
Jakob Stoklund Olesen | ba05c01 | 2011-02-18 22:03:18 +0000 | [diff] [blame] | 30 | class SlotIndexes; |
Alkis Evlogimenos | 34d9bc9 | 2004-02-23 23:08:11 +0000 | [diff] [blame] | 31 | |
Owen Anderson | 49c8aa0 | 2009-03-13 05:55:11 +0000 | [diff] [blame] | 32 | class VirtRegMap : public MachineFunctionPass { |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 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 | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 40 | private: |
Evan Cheng | 90f95f8 | 2009-06-14 20:22:55 +0000 | [diff] [blame] | 41 | MachineRegisterInfo *MRI; |
Owen Anderson | 49c8aa0 | 2009-03-13 05:55:11 +0000 | [diff] [blame] | 42 | const TargetInstrInfo *TII; |
Mike Stump | fe095f3 | 2009-05-04 18:40:41 +0000 | [diff] [blame] | 43 | const TargetRegisterInfo *TRI; |
Owen Anderson | 49c8aa0 | 2009-03-13 05:55:11 +0000 | [diff] [blame] | 44 | MachineFunction *MF; |
Mike Stump | fe095f3 | 2009-05-04 18:40:41 +0000 | [diff] [blame] | 45 | |
Alkis Evlogimenos | c736b3a | 2004-10-01 00:35:07 +0000 | [diff] [blame] | 46 | /// Virt2PhysMap - This is a virtual to physical register |
| 47 | /// mapping. Each virtual register is required to have an entry in |
| 48 | /// it; even spilled virtual registers (the register mapped to a |
| 49 | /// spilled register is the temporary used to load it from the |
| 50 | /// stack). |
Chris Lattner | 94c002a | 2007-02-01 05:32:05 +0000 | [diff] [blame] | 51 | IndexedMap<unsigned, VirtReg2IndexFunctor> Virt2PhysMap; |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 52 | |
Alkis Evlogimenos | c736b3a | 2004-10-01 00:35:07 +0000 | [diff] [blame] | 53 | /// Virt2StackSlotMap - This is virtual register to stack slot |
| 54 | /// mapping. Each spilled virtual register has an entry in it |
| 55 | /// which corresponds to the stack slot this register is spilled |
| 56 | /// at. |
Chris Lattner | 94c002a | 2007-02-01 05:32:05 +0000 | [diff] [blame] | 57 | IndexedMap<int, VirtReg2IndexFunctor> Virt2StackSlotMap; |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 58 | |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 59 | /// Virt2SplitMap - This is virtual register to splitted virtual register |
| 60 | /// mapping. |
| 61 | IndexedMap<unsigned, VirtReg2IndexFunctor> Virt2SplitMap; |
| 62 | |
Jakob Stoklund Olesen | b55e91e | 2010-11-16 00:41:01 +0000 | [diff] [blame] | 63 | /// createSpillSlot - Allocate a spill slot for RC from MFI. |
| 64 | unsigned createSpillSlot(const TargetRegisterClass *RC); |
| 65 | |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 66 | VirtRegMap(const VirtRegMap&); // DO NOT IMPLEMENT |
| 67 | void operator=(const VirtRegMap&); // DO NOT IMPLEMENT |
Alkis Evlogimenos | 7974287 | 2004-02-23 23:47:10 +0000 | [diff] [blame] | 68 | |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 69 | public: |
Owen Anderson | 49c8aa0 | 2009-03-13 05:55:11 +0000 | [diff] [blame] | 70 | static char ID; |
Owen Anderson | 90c579d | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 71 | VirtRegMap() : MachineFunctionPass(ID), Virt2PhysMap(NO_PHYS_REG), |
Jakob Stoklund Olesen | cb39064 | 2011-11-13 01:23:30 +0000 | [diff] [blame] | 72 | Virt2StackSlotMap(NO_STACK_SLOT), Virt2SplitMap(0) { } |
Owen Anderson | 49c8aa0 | 2009-03-13 05:55:11 +0000 | [diff] [blame] | 73 | virtual bool runOnMachineFunction(MachineFunction &MF); |
| 74 | |
| 75 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 76 | AU.setPreservesAll(); |
| 77 | MachineFunctionPass::getAnalysisUsage(AU); |
| 78 | } |
Alkis Evlogimenos | 34d9bc9 | 2004-02-23 23:08:11 +0000 | [diff] [blame] | 79 | |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 80 | MachineFunction &getMachineFunction() const { |
Jakob Stoklund Olesen | c9672cb | 2010-12-10 18:36:02 +0000 | [diff] [blame] | 81 | assert(MF && "getMachineFunction called before runOnMachineFunction"); |
Jakob Stoklund Olesen | f017900 | 2010-07-26 23:44:11 +0000 | [diff] [blame] | 82 | return *MF; |
| 83 | } |
| 84 | |
Jakob Stoklund Olesen | c9672cb | 2010-12-10 18:36:02 +0000 | [diff] [blame] | 85 | MachineRegisterInfo &getRegInfo() const { return *MRI; } |
| 86 | const TargetRegisterInfo &getTargetRegInfo() const { return *TRI; } |
| 87 | |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 88 | void grow(); |
Alkis Evlogimenos | dd420e0 | 2004-03-01 23:18:15 +0000 | [diff] [blame] | 89 | |
Alkis Evlogimenos | c736b3a | 2004-10-01 00:35:07 +0000 | [diff] [blame] | 90 | /// @brief returns true if the specified virtual register is |
| 91 | /// mapped to a physical register |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 92 | bool hasPhys(unsigned virtReg) const { |
| 93 | return getPhys(virtReg) != NO_PHYS_REG; |
| 94 | } |
Alkis Evlogimenos | dd420e0 | 2004-03-01 23:18:15 +0000 | [diff] [blame] | 95 | |
Alkis Evlogimenos | c736b3a | 2004-10-01 00:35:07 +0000 | [diff] [blame] | 96 | /// @brief returns the physical register mapped to the specified |
| 97 | /// virtual register |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 98 | unsigned getPhys(unsigned virtReg) const { |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 99 | assert(TargetRegisterInfo::isVirtualRegister(virtReg)); |
Chris Lattner | 7f690e6 | 2004-09-30 02:15:18 +0000 | [diff] [blame] | 100 | return Virt2PhysMap[virtReg]; |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 101 | } |
Alkis Evlogimenos | dd420e0 | 2004-03-01 23:18:15 +0000 | [diff] [blame] | 102 | |
Alkis Evlogimenos | c736b3a | 2004-10-01 00:35:07 +0000 | [diff] [blame] | 103 | /// @brief creates a mapping for the specified virtual register to |
| 104 | /// the specified physical register |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 105 | void assignVirt2Phys(unsigned virtReg, unsigned physReg) { |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 106 | assert(TargetRegisterInfo::isVirtualRegister(virtReg) && |
| 107 | TargetRegisterInfo::isPhysicalRegister(physReg)); |
Chris Lattner | 7f690e6 | 2004-09-30 02:15:18 +0000 | [diff] [blame] | 108 | assert(Virt2PhysMap[virtReg] == NO_PHYS_REG && |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 109 | "attempt to assign physical register to already mapped " |
| 110 | "virtual register"); |
Chris Lattner | 7f690e6 | 2004-09-30 02:15:18 +0000 | [diff] [blame] | 111 | Virt2PhysMap[virtReg] = physReg; |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Alkis Evlogimenos | c736b3a | 2004-10-01 00:35:07 +0000 | [diff] [blame] | 114 | /// @brief clears the specified virtual register's, physical |
| 115 | /// register mapping |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 116 | void clearVirt(unsigned virtReg) { |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 117 | assert(TargetRegisterInfo::isVirtualRegister(virtReg)); |
Chris Lattner | 7f690e6 | 2004-09-30 02:15:18 +0000 | [diff] [blame] | 118 | assert(Virt2PhysMap[virtReg] != NO_PHYS_REG && |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 119 | "attempt to clear a not assigned virtual register"); |
Chris Lattner | 7f690e6 | 2004-09-30 02:15:18 +0000 | [diff] [blame] | 120 | Virt2PhysMap[virtReg] = NO_PHYS_REG; |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Alkis Evlogimenos | c736b3a | 2004-10-01 00:35:07 +0000 | [diff] [blame] | 123 | /// @brief clears all virtual to physical register mappings |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 124 | void clearAllVirt() { |
Chris Lattner | 7f690e6 | 2004-09-30 02:15:18 +0000 | [diff] [blame] | 125 | Virt2PhysMap.clear(); |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 126 | grow(); |
| 127 | } |
| 128 | |
Evan Cheng | 90f95f8 | 2009-06-14 20:22:55 +0000 | [diff] [blame] | 129 | /// @brief returns the register allocation preference. |
| 130 | unsigned getRegAllocPref(unsigned virtReg); |
| 131 | |
Jakob Stoklund Olesen | 51458ed | 2011-07-08 20:46:18 +0000 | [diff] [blame] | 132 | /// @brief returns true if VirtReg is assigned to its preferred physreg. |
| 133 | bool hasPreferredPhys(unsigned VirtReg) { |
| 134 | return getPhys(VirtReg) == getRegAllocPref(VirtReg); |
| 135 | } |
| 136 | |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 137 | /// @brief records virtReg is a split live interval from SReg. |
| 138 | void setIsSplitFromReg(unsigned virtReg, unsigned SReg) { |
| 139 | Virt2SplitMap[virtReg] = SReg; |
| 140 | } |
| 141 | |
| 142 | /// @brief returns the live interval virtReg is split from. |
Jakob Stoklund Olesen | e324f6e | 2011-02-18 22:35:20 +0000 | [diff] [blame] | 143 | unsigned getPreSplitReg(unsigned virtReg) const { |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 144 | return Virt2SplitMap[virtReg]; |
| 145 | } |
| 146 | |
Jakob Stoklund Olesen | fd38917 | 2011-02-19 00:38:43 +0000 | [diff] [blame] | 147 | /// getOriginal - Return the original virtual register that VirtReg descends |
| 148 | /// from through splitting. |
| 149 | /// A register that was not created by splitting is its own original. |
| 150 | /// This operation is idempotent. |
| 151 | unsigned getOriginal(unsigned VirtReg) const { |
| 152 | unsigned Orig = getPreSplitReg(VirtReg); |
| 153 | return Orig ? Orig : VirtReg; |
| 154 | } |
| 155 | |
Dan Gohman | 39e33ac | 2008-03-12 20:50:04 +0000 | [diff] [blame] | 156 | /// @brief returns true if the specified virtual register is not |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 157 | /// mapped to a stack slot or rematerialized. |
| 158 | bool isAssignedReg(unsigned virtReg) const { |
Jakob Stoklund Olesen | 3cb0b0e | 2011-11-13 01:02:04 +0000 | [diff] [blame] | 159 | if (getStackSlot(virtReg) == NO_STACK_SLOT) |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 160 | return true; |
| 161 | // Split register can be assigned a physical register as well as a |
| 162 | // stack slot or remat id. |
| 163 | return (Virt2SplitMap[virtReg] && Virt2PhysMap[virtReg] != NO_PHYS_REG); |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Alkis Evlogimenos | c736b3a | 2004-10-01 00:35:07 +0000 | [diff] [blame] | 166 | /// @brief returns the stack slot mapped to the specified virtual |
| 167 | /// register |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 168 | int getStackSlot(unsigned virtReg) const { |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 169 | assert(TargetRegisterInfo::isVirtualRegister(virtReg)); |
Chris Lattner | 7f690e6 | 2004-09-30 02:15:18 +0000 | [diff] [blame] | 170 | return Virt2StackSlotMap[virtReg]; |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Alkis Evlogimenos | c736b3a | 2004-10-01 00:35:07 +0000 | [diff] [blame] | 173 | /// @brief create a mapping for the specifed virtual register to |
| 174 | /// the next available stack slot |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 175 | int assignVirt2StackSlot(unsigned virtReg); |
Alkis Evlogimenos | c736b3a | 2004-10-01 00:35:07 +0000 | [diff] [blame] | 176 | /// @brief create a mapping for the specified virtual register to |
| 177 | /// the specified stack slot |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 178 | void assignVirt2StackSlot(unsigned virtReg, int frameIndex); |
| 179 | |
Jakob Stoklund Olesen | ba05c01 | 2011-02-18 22:03:18 +0000 | [diff] [blame] | 180 | /// rewrite - Rewrite all instructions in MF to use only physical registers |
| 181 | /// by mapping all virtual register operands to their assigned physical |
| 182 | /// registers. |
| 183 | /// |
| 184 | /// @param Indexes Optionally remove deleted instructions from indexes. |
| 185 | void rewrite(SlotIndexes *Indexes); |
| 186 | |
Daniel Dunbar | 1cd1d98 | 2009-07-24 10:36:58 +0000 | [diff] [blame] | 187 | void print(raw_ostream &OS, const Module* M = 0) const; |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 188 | void dump() const; |
| 189 | }; |
| 190 | |
Daniel Dunbar | 1cd1d98 | 2009-07-24 10:36:58 +0000 | [diff] [blame] | 191 | inline raw_ostream &operator<<(raw_ostream &OS, const VirtRegMap &VRM) { |
| 192 | VRM.print(OS); |
| 193 | return OS; |
| 194 | } |
Alkis Evlogimenos | 34d9bc9 | 2004-02-23 23:08:11 +0000 | [diff] [blame] | 195 | } // End llvm namespace |
| 196 | |
| 197 | #endif |