Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 1 | //===-- RegAllocLocal.cpp - A BasicBlock generic register allocator -------===// |
Alkis Evlogimenos | 4de473b | 2004-02-13 18:20:47 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
Alkis Evlogimenos | 4de473b | 2004-02-13 18:20:47 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 9 | // |
| 10 | // This register allocator allocates registers to a basic block at a time, |
| 11 | // attempting to keep values in registers and reusing registers as appropriate. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Chris Lattner | 4cc662b | 2003-08-03 21:47:31 +0000 | [diff] [blame] | 15 | #define DEBUG_TYPE "regalloc" |
Evan Cheng | ddee842 | 2006-11-15 20:55:15 +0000 | [diff] [blame] | 16 | #include "llvm/BasicBlock.h" |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/Passes.h" |
Chris Lattner | 580f9be | 2002-12-28 20:40:43 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/MachineInstr.h" |
Chris Lattner | ff863ba | 2002-12-25 05:05:46 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/SSARegMap.h" |
Chris Lattner | eb24db9 | 2002-12-28 21:08:26 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/LiveVariables.h" |
Jim Laskey | eb577ba | 2006-08-02 12:30:23 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/RegAllocRegistry.h" |
Chris Lattner | 3501fea | 2003-01-14 22:00:31 +0000 | [diff] [blame] | 24 | #include "llvm/Target/TargetInstrInfo.h" |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 25 | #include "llvm/Target/TargetMachine.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 26 | #include "llvm/Support/CommandLine.h" |
| 27 | #include "llvm/Support/Debug.h" |
Chris Lattner | a4f0b3a | 2006-08-27 12:54:02 +0000 | [diff] [blame] | 28 | #include "llvm/Support/Compiler.h" |
Chris Lattner | 94c002a | 2007-02-01 05:32:05 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/IndexedMap.h" |
Evan Cheng | ddee842 | 2006-11-15 20:55:15 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/SmallVector.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/Statistic.h" |
Chris Lattner | 27f2916 | 2004-10-26 15:35:58 +0000 | [diff] [blame] | 32 | #include <algorithm> |
Chris Lattner | ef09c63 | 2004-01-31 21:27:19 +0000 | [diff] [blame] | 33 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 34 | |
Chris Lattner | cd3245a | 2006-12-19 22:41:21 +0000 | [diff] [blame] | 35 | STATISTIC(NumStores, "Number of stores added"); |
| 36 | STATISTIC(NumLoads , "Number of loads added"); |
| 37 | STATISTIC(NumFolded, "Number of loads/stores folded into instructions"); |
Jim Laskey | 13ec702 | 2006-08-01 14:21:23 +0000 | [diff] [blame] | 38 | |
Chris Lattner | cd3245a | 2006-12-19 22:41:21 +0000 | [diff] [blame] | 39 | namespace { |
Jim Laskey | 13ec702 | 2006-08-01 14:21:23 +0000 | [diff] [blame] | 40 | static RegisterRegAlloc |
| 41 | localRegAlloc("local", " local register allocator", |
| 42 | createLocalRegisterAllocator); |
| 43 | |
| 44 | |
Chris Lattner | 9525528 | 2006-06-28 23:17:24 +0000 | [diff] [blame] | 45 | class VISIBILITY_HIDDEN RA : public MachineFunctionPass { |
Chris Lattner | 580f9be | 2002-12-28 20:40:43 +0000 | [diff] [blame] | 46 | const TargetMachine *TM; |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 47 | MachineFunction *MF; |
Chris Lattner | 580f9be | 2002-12-28 20:40:43 +0000 | [diff] [blame] | 48 | const MRegisterInfo *RegInfo; |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 49 | LiveVariables *LV; |
Chris Lattner | ff863ba | 2002-12-25 05:05:46 +0000 | [diff] [blame] | 50 | |
Chris Lattner | b8822ad | 2003-08-04 23:36:39 +0000 | [diff] [blame] | 51 | // StackSlotForVirtReg - Maps virtual regs to the frame index where these |
| 52 | // values are spilled. |
Chris Lattner | 580f9be | 2002-12-28 20:40:43 +0000 | [diff] [blame] | 53 | std::map<unsigned, int> StackSlotForVirtReg; |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 54 | |
| 55 | // Virt2PhysRegMap - This map contains entries for each virtual register |
Alkis Evlogimenos | 4d0d864 | 2004-02-25 21:55:45 +0000 | [diff] [blame] | 56 | // that is currently available in a physical register. |
Chris Lattner | 94c002a | 2007-02-01 05:32:05 +0000 | [diff] [blame] | 57 | IndexedMap<unsigned, VirtReg2IndexFunctor> Virt2PhysRegMap; |
Chris Lattner | ecea563 | 2004-02-09 02:12:04 +0000 | [diff] [blame] | 58 | |
| 59 | unsigned &getVirt2PhysRegMapSlot(unsigned VirtReg) { |
Alkis Evlogimenos | 4d0d864 | 2004-02-25 21:55:45 +0000 | [diff] [blame] | 60 | return Virt2PhysRegMap[VirtReg]; |
Chris Lattner | ecea563 | 2004-02-09 02:12:04 +0000 | [diff] [blame] | 61 | } |
Alkis Evlogimenos | 4de473b | 2004-02-13 18:20:47 +0000 | [diff] [blame] | 62 | |
Chris Lattner | 64667b6 | 2004-02-09 01:26:13 +0000 | [diff] [blame] | 63 | // PhysRegsUsed - This array is effectively a map, containing entries for |
| 64 | // each physical register that currently has a value (ie, it is in |
| 65 | // Virt2PhysRegMap). The value mapped to is the virtual register |
| 66 | // corresponding to the physical register (the inverse of the |
| 67 | // Virt2PhysRegMap), or 0. The value is set to 0 if this register is pinned |
Chris Lattner | 45d5788 | 2006-09-08 19:03:30 +0000 | [diff] [blame] | 68 | // because it is used by a future instruction, and to -2 if it is not |
| 69 | // allocatable. If the entry for a physical register is -1, then the |
| 70 | // physical register is "not in the map". |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 71 | // |
Alkis Evlogimenos | 4de473b | 2004-02-13 18:20:47 +0000 | [diff] [blame] | 72 | std::vector<int> PhysRegsUsed; |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 73 | |
| 74 | // PhysRegsUseOrder - This contains a list of the physical registers that |
| 75 | // currently have a virtual register value in them. This list provides an |
| 76 | // ordering of registers, imposing a reallocation order. This list is only |
| 77 | // used if all registers are allocated and we have to spill one, in which |
| 78 | // case we spill the least recently used register. Entries at the front of |
| 79 | // the list are the least recently used registers, entries at the back are |
| 80 | // the most recently used. |
| 81 | // |
| 82 | std::vector<unsigned> PhysRegsUseOrder; |
| 83 | |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 84 | // VirtRegModified - This bitset contains information about which virtual |
| 85 | // registers need to be spilled back to memory when their registers are |
| 86 | // scavenged. If a virtual register has simply been rematerialized, there |
| 87 | // is no reason to spill it to memory when we need the register back. |
Chris Lattner | 82bee0f | 2002-12-18 08:14:26 +0000 | [diff] [blame] | 88 | // |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 89 | std::vector<bool> VirtRegModified; |
| 90 | |
| 91 | void markVirtRegModified(unsigned Reg, bool Val = true) { |
Chris Lattner | ef09c63 | 2004-01-31 21:27:19 +0000 | [diff] [blame] | 92 | assert(MRegisterInfo::isVirtualRegister(Reg) && "Illegal VirtReg!"); |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 93 | Reg -= MRegisterInfo::FirstVirtualRegister; |
| 94 | if (VirtRegModified.size() <= Reg) VirtRegModified.resize(Reg+1); |
| 95 | VirtRegModified[Reg] = Val; |
| 96 | } |
| 97 | |
| 98 | bool isVirtRegModified(unsigned Reg) const { |
Chris Lattner | ef09c63 | 2004-01-31 21:27:19 +0000 | [diff] [blame] | 99 | assert(MRegisterInfo::isVirtualRegister(Reg) && "Illegal VirtReg!"); |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 100 | assert(Reg - MRegisterInfo::FirstVirtualRegister < VirtRegModified.size() |
Alkis Evlogimenos | 4de473b | 2004-02-13 18:20:47 +0000 | [diff] [blame] | 101 | && "Illegal virtual register!"); |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 102 | return VirtRegModified[Reg - MRegisterInfo::FirstVirtualRegister]; |
| 103 | } |
Chris Lattner | 82bee0f | 2002-12-18 08:14:26 +0000 | [diff] [blame] | 104 | |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 105 | void MarkPhysRegRecentlyUsed(unsigned Reg) { |
Chris Lattner | 5e50349 | 2006-09-03 07:15:37 +0000 | [diff] [blame] | 106 | if (PhysRegsUseOrder.empty() || |
| 107 | PhysRegsUseOrder.back() == Reg) return; // Already most recently used |
Chris Lattner | 0eb172c | 2002-12-24 00:04:55 +0000 | [diff] [blame] | 108 | |
| 109 | for (unsigned i = PhysRegsUseOrder.size(); i != 0; --i) |
Alkis Evlogimenos | 4de473b | 2004-02-13 18:20:47 +0000 | [diff] [blame] | 110 | if (areRegsEqual(Reg, PhysRegsUseOrder[i-1])) { |
| 111 | unsigned RegMatch = PhysRegsUseOrder[i-1]; // remove from middle |
| 112 | PhysRegsUseOrder.erase(PhysRegsUseOrder.begin()+i-1); |
| 113 | // Add it to the end of the list |
| 114 | PhysRegsUseOrder.push_back(RegMatch); |
| 115 | if (RegMatch == Reg) |
| 116 | return; // Found an exact match, exit early |
| 117 | } |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | public: |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 121 | virtual const char *getPassName() const { |
| 122 | return "Local Register Allocator"; |
| 123 | } |
| 124 | |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 125 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | 56ddada | 2004-02-17 17:49:10 +0000 | [diff] [blame] | 126 | AU.addRequired<LiveVariables>(); |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 127 | AU.addRequiredID(PHIEliminationID); |
Alkis Evlogimenos | 4c08086 | 2003-12-18 22:40:24 +0000 | [diff] [blame] | 128 | AU.addRequiredID(TwoAddressInstructionPassID); |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 129 | MachineFunctionPass::getAnalysisUsage(AU); |
| 130 | } |
| 131 | |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 132 | private: |
| 133 | /// runOnMachineFunction - Register allocate the whole function |
| 134 | bool runOnMachineFunction(MachineFunction &Fn); |
| 135 | |
| 136 | /// AllocateBasicBlock - Register allocate the specified basic block. |
| 137 | void AllocateBasicBlock(MachineBasicBlock &MBB); |
| 138 | |
Chris Lattner | 82bee0f | 2002-12-18 08:14:26 +0000 | [diff] [blame] | 139 | |
Chris Lattner | 82bee0f | 2002-12-18 08:14:26 +0000 | [diff] [blame] | 140 | /// areRegsEqual - This method returns true if the specified registers are |
| 141 | /// related to each other. To do this, it checks to see if they are equal |
| 142 | /// or if the first register is in the alias set of the second register. |
| 143 | /// |
| 144 | bool areRegsEqual(unsigned R1, unsigned R2) const { |
| 145 | if (R1 == R2) return true; |
Alkis Evlogimenos | 73ff512 | 2003-10-08 05:20:08 +0000 | [diff] [blame] | 146 | for (const unsigned *AliasSet = RegInfo->getAliasSet(R2); |
| 147 | *AliasSet; ++AliasSet) { |
| 148 | if (*AliasSet == R1) return true; |
| 149 | } |
Chris Lattner | 82bee0f | 2002-12-18 08:14:26 +0000 | [diff] [blame] | 150 | return false; |
| 151 | } |
| 152 | |
Chris Lattner | 580f9be | 2002-12-28 20:40:43 +0000 | [diff] [blame] | 153 | /// getStackSpaceFor - This returns the frame index of the specified virtual |
Chris Lattner | b8822ad | 2003-08-04 23:36:39 +0000 | [diff] [blame] | 154 | /// register on the stack, allocating space if necessary. |
Chris Lattner | 580f9be | 2002-12-28 20:40:43 +0000 | [diff] [blame] | 155 | int getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC); |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 156 | |
Chris Lattner | b8822ad | 2003-08-04 23:36:39 +0000 | [diff] [blame] | 157 | /// removePhysReg - This method marks the specified physical register as no |
| 158 | /// longer being in use. |
| 159 | /// |
Chris Lattner | 82bee0f | 2002-12-18 08:14:26 +0000 | [diff] [blame] | 160 | void removePhysReg(unsigned PhysReg); |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 161 | |
| 162 | /// spillVirtReg - This method spills the value specified by PhysReg into |
| 163 | /// the virtual register slot specified by VirtReg. It then updates the RA |
| 164 | /// data structures to indicate the fact that PhysReg is now available. |
| 165 | /// |
Chris Lattner | 688c825 | 2004-02-22 19:08:15 +0000 | [diff] [blame] | 166 | void spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 167 | unsigned VirtReg, unsigned PhysReg); |
| 168 | |
Chris Lattner | c21be92 | 2002-12-16 17:44:42 +0000 | [diff] [blame] | 169 | /// spillPhysReg - This method spills the specified physical register into |
Chris Lattner | 128c2aa | 2003-08-17 18:01:15 +0000 | [diff] [blame] | 170 | /// the virtual register slot associated with it. If OnlyVirtRegs is set to |
| 171 | /// true, then the request is ignored if the physical register does not |
| 172 | /// contain a virtual register. |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 173 | /// |
Chris Lattner | 42e0a8f | 2004-02-17 03:57:19 +0000 | [diff] [blame] | 174 | void spillPhysReg(MachineBasicBlock &MBB, MachineInstr *I, |
Chris Lattner | 128c2aa | 2003-08-17 18:01:15 +0000 | [diff] [blame] | 175 | unsigned PhysReg, bool OnlyVirtRegs = false); |
Chris Lattner | c21be92 | 2002-12-16 17:44:42 +0000 | [diff] [blame] | 176 | |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 177 | /// assignVirtToPhysReg - This method updates local state so that we know |
| 178 | /// that PhysReg is the proper container for VirtReg now. The physical |
| 179 | /// register must not be used for anything else when this is called. |
| 180 | /// |
| 181 | void assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg); |
| 182 | |
| 183 | /// liberatePhysReg - Make sure the specified physical register is available |
| 184 | /// for use. If there is currently a value in it, it is either moved out of |
| 185 | /// the way or spilled to memory. |
| 186 | /// |
| 187 | void liberatePhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I, |
Alkis Evlogimenos | 4de473b | 2004-02-13 18:20:47 +0000 | [diff] [blame] | 188 | unsigned PhysReg); |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 189 | |
Chris Lattner | ae64043 | 2002-12-17 02:50:10 +0000 | [diff] [blame] | 190 | /// isPhysRegAvailable - Return true if the specified physical register is |
| 191 | /// free and available for use. This also includes checking to see if |
| 192 | /// aliased registers are all free... |
| 193 | /// |
Chris Lattner | 82bee0f | 2002-12-18 08:14:26 +0000 | [diff] [blame] | 194 | bool isPhysRegAvailable(unsigned PhysReg) const; |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 195 | |
| 196 | /// getFreeReg - Look to see if there is a free register available in the |
| 197 | /// specified register class. If not, return 0. |
| 198 | /// |
| 199 | unsigned getFreeReg(const TargetRegisterClass *RC); |
Alkis Evlogimenos | 4de473b | 2004-02-13 18:20:47 +0000 | [diff] [blame] | 200 | |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 201 | /// getReg - Find a physical register to hold the specified virtual |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 202 | /// register. If all compatible physical registers are used, this method |
| 203 | /// spills the last used virtual register to the stack, and uses that |
| 204 | /// register. |
| 205 | /// |
Chris Lattner | 42e0a8f | 2004-02-17 03:57:19 +0000 | [diff] [blame] | 206 | unsigned getReg(MachineBasicBlock &MBB, MachineInstr *MI, |
Alkis Evlogimenos | 4de473b | 2004-02-13 18:20:47 +0000 | [diff] [blame] | 207 | unsigned VirtReg); |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 208 | |
Chris Lattner | 42e0a8f | 2004-02-17 03:57:19 +0000 | [diff] [blame] | 209 | /// reloadVirtReg - This method transforms the specified specified virtual |
| 210 | /// register use to refer to a physical register. This method may do this |
| 211 | /// in one of several ways: if the register is available in a physical |
| 212 | /// register already, it uses that physical register. If the value is not |
| 213 | /// in a physical register, and if there are physical registers available, |
| 214 | /// it loads it into a register. If register pressure is high, and it is |
| 215 | /// possible, it tries to fold the load of the virtual register into the |
| 216 | /// instruction itself. It avoids doing this if register pressure is low to |
| 217 | /// improve the chance that subsequent instructions can use the reloaded |
| 218 | /// value. This method returns the modified instruction. |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 219 | /// |
Chris Lattner | 42e0a8f | 2004-02-17 03:57:19 +0000 | [diff] [blame] | 220 | MachineInstr *reloadVirtReg(MachineBasicBlock &MBB, MachineInstr *MI, |
| 221 | unsigned OpNum); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 222 | |
Chris Lattner | b8822ad | 2003-08-04 23:36:39 +0000 | [diff] [blame] | 223 | |
| 224 | void reloadPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I, |
| 225 | unsigned PhysReg); |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 226 | }; |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 227 | } |
| 228 | |
Chris Lattner | b8822ad | 2003-08-04 23:36:39 +0000 | [diff] [blame] | 229 | /// getStackSpaceFor - This allocates space for the specified virtual register |
| 230 | /// to be held on the stack. |
| 231 | int RA::getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC) { |
| 232 | // Find the location Reg would belong... |
| 233 | std::map<unsigned, int>::iterator I =StackSlotForVirtReg.lower_bound(VirtReg); |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 234 | |
Chris Lattner | 580f9be | 2002-12-28 20:40:43 +0000 | [diff] [blame] | 235 | if (I != StackSlotForVirtReg.end() && I->first == VirtReg) |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 236 | return I->second; // Already has space allocated? |
| 237 | |
Chris Lattner | 580f9be | 2002-12-28 20:40:43 +0000 | [diff] [blame] | 238 | // Allocate a new stack object for this spill location... |
Chris Lattner | 26eb14b | 2004-08-15 22:02:22 +0000 | [diff] [blame] | 239 | int FrameIdx = MF->getFrameInfo()->CreateStackObject(RC->getSize(), |
| 240 | RC->getAlignment()); |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 241 | |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 242 | // Assign the slot... |
Chris Lattner | 580f9be | 2002-12-28 20:40:43 +0000 | [diff] [blame] | 243 | StackSlotForVirtReg.insert(I, std::make_pair(VirtReg, FrameIdx)); |
| 244 | return FrameIdx; |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 245 | } |
| 246 | |
Chris Lattner | ae64043 | 2002-12-17 02:50:10 +0000 | [diff] [blame] | 247 | |
Alkis Evlogimenos | 4de473b | 2004-02-13 18:20:47 +0000 | [diff] [blame] | 248 | /// removePhysReg - This method marks the specified physical register as no |
Chris Lattner | 82bee0f | 2002-12-18 08:14:26 +0000 | [diff] [blame] | 249 | /// longer being in use. |
| 250 | /// |
| 251 | void RA::removePhysReg(unsigned PhysReg) { |
Chris Lattner | 64667b6 | 2004-02-09 01:26:13 +0000 | [diff] [blame] | 252 | PhysRegsUsed[PhysReg] = -1; // PhyReg no longer used |
Chris Lattner | 82bee0f | 2002-12-18 08:14:26 +0000 | [diff] [blame] | 253 | |
| 254 | std::vector<unsigned>::iterator It = |
| 255 | std::find(PhysRegsUseOrder.begin(), PhysRegsUseOrder.end(), PhysReg); |
Alkis Evlogimenos | 19b6486 | 2004-01-13 06:24:30 +0000 | [diff] [blame] | 256 | if (It != PhysRegsUseOrder.end()) |
| 257 | PhysRegsUseOrder.erase(It); |
Chris Lattner | 82bee0f | 2002-12-18 08:14:26 +0000 | [diff] [blame] | 258 | } |
| 259 | |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 260 | |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 261 | /// spillVirtReg - This method spills the value specified by PhysReg into the |
| 262 | /// virtual register slot specified by VirtReg. It then updates the RA data |
| 263 | /// structures to indicate the fact that PhysReg is now available. |
| 264 | /// |
Chris Lattner | 688c825 | 2004-02-22 19:08:15 +0000 | [diff] [blame] | 265 | void RA::spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator I, |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 266 | unsigned VirtReg, unsigned PhysReg) { |
Chris Lattner | 8c81945 | 2003-08-05 04:13:58 +0000 | [diff] [blame] | 267 | assert(VirtReg && "Spilling a physical register is illegal!" |
Chris Lattner | d9ac6a7 | 2003-08-05 00:49:09 +0000 | [diff] [blame] | 268 | " Must not have appropriate kill for the register or use exists beyond" |
| 269 | " the intended one."); |
Bill Wendling | b2b9c20 | 2006-11-17 02:09:07 +0000 | [diff] [blame] | 270 | DOUT << " Spilling register " << RegInfo->getName(PhysReg) |
| 271 | << " containing %reg" << VirtReg; |
| 272 | if (!isVirtRegModified(VirtReg)) |
| 273 | DOUT << " which has not been modified, so no store necessary!"; |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 274 | |
Chris Lattner | d9ac6a7 | 2003-08-05 00:49:09 +0000 | [diff] [blame] | 275 | // Otherwise, there is a virtual register corresponding to this physical |
| 276 | // register. We only need to spill it into its stack slot if it has been |
| 277 | // modified. |
| 278 | if (isVirtRegModified(VirtReg)) { |
| 279 | const TargetRegisterClass *RC = MF->getSSARegMap()->getRegClass(VirtReg); |
| 280 | int FrameIndex = getStackSpaceFor(VirtReg, RC); |
Bill Wendling | b2b9c20 | 2006-11-17 02:09:07 +0000 | [diff] [blame] | 281 | DOUT << " to stack slot #" << FrameIndex; |
Chris Lattner | bf9716b | 2005-09-30 01:29:00 +0000 | [diff] [blame] | 282 | RegInfo->storeRegToStackSlot(MBB, I, PhysReg, FrameIndex, RC); |
Alkis Evlogimenos | 2acef2d | 2004-02-19 06:19:09 +0000 | [diff] [blame] | 283 | ++NumStores; // Update statistics |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 284 | } |
Chris Lattner | ecea563 | 2004-02-09 02:12:04 +0000 | [diff] [blame] | 285 | |
| 286 | getVirt2PhysRegMapSlot(VirtReg) = 0; // VirtReg no longer available |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 287 | |
Bill Wendling | b2b9c20 | 2006-11-17 02:09:07 +0000 | [diff] [blame] | 288 | DOUT << "\n"; |
Chris Lattner | 82bee0f | 2002-12-18 08:14:26 +0000 | [diff] [blame] | 289 | removePhysReg(PhysReg); |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 290 | } |
| 291 | |
Chris Lattner | ae64043 | 2002-12-17 02:50:10 +0000 | [diff] [blame] | 292 | |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 293 | /// spillPhysReg - This method spills the specified physical register into the |
Chris Lattner | 128c2aa | 2003-08-17 18:01:15 +0000 | [diff] [blame] | 294 | /// virtual register slot associated with it. If OnlyVirtRegs is set to true, |
| 295 | /// then the request is ignored if the physical register does not contain a |
| 296 | /// virtual register. |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 297 | /// |
Chris Lattner | 42e0a8f | 2004-02-17 03:57:19 +0000 | [diff] [blame] | 298 | void RA::spillPhysReg(MachineBasicBlock &MBB, MachineInstr *I, |
Chris Lattner | 128c2aa | 2003-08-17 18:01:15 +0000 | [diff] [blame] | 299 | unsigned PhysReg, bool OnlyVirtRegs) { |
Chris Lattner | 64667b6 | 2004-02-09 01:26:13 +0000 | [diff] [blame] | 300 | if (PhysRegsUsed[PhysReg] != -1) { // Only spill it if it's used! |
Chris Lattner | 45d5788 | 2006-09-08 19:03:30 +0000 | [diff] [blame] | 301 | assert(PhysRegsUsed[PhysReg] != -2 && "Non allocable reg used!"); |
Chris Lattner | 64667b6 | 2004-02-09 01:26:13 +0000 | [diff] [blame] | 302 | if (PhysRegsUsed[PhysReg] || !OnlyVirtRegs) |
| 303 | spillVirtReg(MBB, I, PhysRegsUsed[PhysReg], PhysReg); |
Alkis Evlogimenos | 73ff512 | 2003-10-08 05:20:08 +0000 | [diff] [blame] | 304 | } else { |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 305 | // If the selected register aliases any other registers, we must make |
Chris Lattner | 45d5788 | 2006-09-08 19:03:30 +0000 | [diff] [blame] | 306 | // sure that one of the aliases isn't alive. |
Alkis Evlogimenos | 73ff512 | 2003-10-08 05:20:08 +0000 | [diff] [blame] | 307 | for (const unsigned *AliasSet = RegInfo->getAliasSet(PhysReg); |
Chris Lattner | 64667b6 | 2004-02-09 01:26:13 +0000 | [diff] [blame] | 308 | *AliasSet; ++AliasSet) |
Chris Lattner | 45d5788 | 2006-09-08 19:03:30 +0000 | [diff] [blame] | 309 | if (PhysRegsUsed[*AliasSet] != -1 && // Spill aliased register. |
| 310 | PhysRegsUsed[*AliasSet] != -2) // If allocatable. |
Evan Cheng | ddee842 | 2006-11-15 20:55:15 +0000 | [diff] [blame] | 311 | if (PhysRegsUsed[*AliasSet] == 0) { |
| 312 | // This must have been a dead def due to something like this: |
| 313 | // %EAX := |
| 314 | // := op %AL |
| 315 | // No more use of %EAX, %AH, etc. |
| 316 | // %EAX isn't dead upon definition, but %AH is. However %AH isn't |
| 317 | // an operand of definition MI so it's not marked as such. |
Bill Wendling | b2b9c20 | 2006-11-17 02:09:07 +0000 | [diff] [blame] | 318 | DOUT << " Register " << RegInfo->getName(*AliasSet) |
| 319 | << " [%reg" << *AliasSet |
| 320 | << "] is never used, removing it frame live list\n"; |
Evan Cheng | ddee842 | 2006-11-15 20:55:15 +0000 | [diff] [blame] | 321 | removePhysReg(*AliasSet); |
| 322 | } else |
Chris Lattner | 64667b6 | 2004-02-09 01:26:13 +0000 | [diff] [blame] | 323 | spillVirtReg(MBB, I, PhysRegsUsed[*AliasSet], *AliasSet); |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 324 | } |
| 325 | } |
| 326 | |
| 327 | |
| 328 | /// assignVirtToPhysReg - This method updates local state so that we know |
| 329 | /// that PhysReg is the proper container for VirtReg now. The physical |
| 330 | /// register must not be used for anything else when this is called. |
| 331 | /// |
| 332 | void RA::assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg) { |
Chris Lattner | 64667b6 | 2004-02-09 01:26:13 +0000 | [diff] [blame] | 333 | assert(PhysRegsUsed[PhysReg] == -1 && "Phys reg already assigned!"); |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 334 | // Update information to note the fact that this register was just used, and |
| 335 | // it holds VirtReg. |
| 336 | PhysRegsUsed[PhysReg] = VirtReg; |
Alkis Evlogimenos | 4de473b | 2004-02-13 18:20:47 +0000 | [diff] [blame] | 337 | getVirt2PhysRegMapSlot(VirtReg) = PhysReg; |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 338 | PhysRegsUseOrder.push_back(PhysReg); // New use of PhysReg |
| 339 | } |
| 340 | |
| 341 | |
Chris Lattner | ae64043 | 2002-12-17 02:50:10 +0000 | [diff] [blame] | 342 | /// isPhysRegAvailable - Return true if the specified physical register is free |
| 343 | /// and available for use. This also includes checking to see if aliased |
| 344 | /// registers are all free... |
| 345 | /// |
| 346 | bool RA::isPhysRegAvailable(unsigned PhysReg) const { |
Chris Lattner | 64667b6 | 2004-02-09 01:26:13 +0000 | [diff] [blame] | 347 | if (PhysRegsUsed[PhysReg] != -1) return false; |
Chris Lattner | ae64043 | 2002-12-17 02:50:10 +0000 | [diff] [blame] | 348 | |
| 349 | // If the selected register aliases any other allocated registers, it is |
| 350 | // not free! |
Alkis Evlogimenos | 73ff512 | 2003-10-08 05:20:08 +0000 | [diff] [blame] | 351 | for (const unsigned *AliasSet = RegInfo->getAliasSet(PhysReg); |
| 352 | *AliasSet; ++AliasSet) |
Chris Lattner | 64667b6 | 2004-02-09 01:26:13 +0000 | [diff] [blame] | 353 | if (PhysRegsUsed[*AliasSet] != -1) // Aliased register in use? |
Alkis Evlogimenos | 73ff512 | 2003-10-08 05:20:08 +0000 | [diff] [blame] | 354 | return false; // Can't use this reg then. |
Chris Lattner | ae64043 | 2002-12-17 02:50:10 +0000 | [diff] [blame] | 355 | return true; |
| 356 | } |
| 357 | |
| 358 | |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 359 | /// getFreeReg - Look to see if there is a free register available in the |
| 360 | /// specified register class. If not, return 0. |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 361 | /// |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 362 | unsigned RA::getFreeReg(const TargetRegisterClass *RC) { |
Chris Lattner | 580f9be | 2002-12-28 20:40:43 +0000 | [diff] [blame] | 363 | // Get iterators defining the range of registers that are valid to allocate in |
| 364 | // this class, which also specifies the preferred allocation order. |
| 365 | TargetRegisterClass::iterator RI = RC->allocation_order_begin(*MF); |
| 366 | TargetRegisterClass::iterator RE = RC->allocation_order_end(*MF); |
Chris Lattner | ae64043 | 2002-12-17 02:50:10 +0000 | [diff] [blame] | 367 | |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 368 | for (; RI != RE; ++RI) |
| 369 | if (isPhysRegAvailable(*RI)) { // Is reg unused? |
| 370 | assert(*RI != 0 && "Cannot use register!"); |
| 371 | return *RI; // Found an unused register! |
| 372 | } |
| 373 | return 0; |
| 374 | } |
| 375 | |
| 376 | |
| 377 | /// liberatePhysReg - Make sure the specified physical register is available for |
| 378 | /// use. If there is currently a value in it, it is either moved out of the way |
| 379 | /// or spilled to memory. |
| 380 | /// |
| 381 | void RA::liberatePhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I, |
Alkis Evlogimenos | 4de473b | 2004-02-13 18:20:47 +0000 | [diff] [blame] | 382 | unsigned PhysReg) { |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 383 | spillPhysReg(MBB, I, PhysReg); |
| 384 | } |
| 385 | |
| 386 | |
| 387 | /// getReg - Find a physical register to hold the specified virtual |
| 388 | /// register. If all compatible physical registers are used, this method spills |
| 389 | /// the last used virtual register to the stack, and uses that register. |
| 390 | /// |
Chris Lattner | 42e0a8f | 2004-02-17 03:57:19 +0000 | [diff] [blame] | 391 | unsigned RA::getReg(MachineBasicBlock &MBB, MachineInstr *I, |
Alkis Evlogimenos | 4de473b | 2004-02-13 18:20:47 +0000 | [diff] [blame] | 392 | unsigned VirtReg) { |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 393 | const TargetRegisterClass *RC = MF->getSSARegMap()->getRegClass(VirtReg); |
| 394 | |
| 395 | // First check to see if we have a free register of the requested type... |
| 396 | unsigned PhysReg = getFreeReg(RC); |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 397 | |
Chris Lattner | ae64043 | 2002-12-17 02:50:10 +0000 | [diff] [blame] | 398 | // If we didn't find an unused register, scavenge one now! |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 399 | if (PhysReg == 0) { |
Chris Lattner | c21be92 | 2002-12-16 17:44:42 +0000 | [diff] [blame] | 400 | assert(!PhysRegsUseOrder.empty() && "No allocated registers??"); |
Chris Lattner | ae64043 | 2002-12-17 02:50:10 +0000 | [diff] [blame] | 401 | |
| 402 | // Loop over all of the preallocated registers from the least recently used |
| 403 | // to the most recently used. When we find one that is capable of holding |
| 404 | // our register, use it. |
| 405 | for (unsigned i = 0; PhysReg == 0; ++i) { |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 406 | assert(i != PhysRegsUseOrder.size() && |
| 407 | "Couldn't find a register of the appropriate class!"); |
Alkis Evlogimenos | 4de473b | 2004-02-13 18:20:47 +0000 | [diff] [blame] | 408 | |
Chris Lattner | ae64043 | 2002-12-17 02:50:10 +0000 | [diff] [blame] | 409 | unsigned R = PhysRegsUseOrder[i]; |
Chris Lattner | 41822c7 | 2003-08-23 23:49:42 +0000 | [diff] [blame] | 410 | |
| 411 | // We can only use this register if it holds a virtual register (ie, it |
| 412 | // can be spilled). Do not use it if it is an explicitly allocated |
| 413 | // physical register! |
Chris Lattner | 64667b6 | 2004-02-09 01:26:13 +0000 | [diff] [blame] | 414 | assert(PhysRegsUsed[R] != -1 && |
Chris Lattner | 41822c7 | 2003-08-23 23:49:42 +0000 | [diff] [blame] | 415 | "PhysReg in PhysRegsUseOrder, but is not allocated?"); |
Chris Lattner | 45d5788 | 2006-09-08 19:03:30 +0000 | [diff] [blame] | 416 | if (PhysRegsUsed[R] && PhysRegsUsed[R] != -2) { |
Chris Lattner | 41822c7 | 2003-08-23 23:49:42 +0000 | [diff] [blame] | 417 | // If the current register is compatible, use it. |
Chris Lattner | 3bba026 | 2004-08-15 22:23:09 +0000 | [diff] [blame] | 418 | if (RC->contains(R)) { |
Chris Lattner | 41822c7 | 2003-08-23 23:49:42 +0000 | [diff] [blame] | 419 | PhysReg = R; |
| 420 | break; |
| 421 | } else { |
| 422 | // If one of the registers aliased to the current register is |
| 423 | // compatible, use it. |
Chris Lattner | 5e50349 | 2006-09-03 07:15:37 +0000 | [diff] [blame] | 424 | for (const unsigned *AliasIt = RegInfo->getAliasSet(R); |
| 425 | *AliasIt; ++AliasIt) { |
| 426 | if (RC->contains(*AliasIt) && |
| 427 | // If this is pinned down for some reason, don't use it. For |
| 428 | // example, if CL is pinned, and we run across CH, don't use |
| 429 | // CH as justification for using scavenging ECX (which will |
| 430 | // fail). |
Chris Lattner | 45d5788 | 2006-09-08 19:03:30 +0000 | [diff] [blame] | 431 | PhysRegsUsed[*AliasIt] != 0 && |
| 432 | |
| 433 | // Make sure the register is allocatable. Don't allocate SIL on |
| 434 | // x86-32. |
| 435 | PhysRegsUsed[*AliasIt] != -2) { |
Chris Lattner | 5e50349 | 2006-09-03 07:15:37 +0000 | [diff] [blame] | 436 | PhysReg = *AliasIt; // Take an aliased register |
Alkis Evlogimenos | 73ff512 | 2003-10-08 05:20:08 +0000 | [diff] [blame] | 437 | break; |
| 438 | } |
| 439 | } |
Chris Lattner | 41822c7 | 2003-08-23 23:49:42 +0000 | [diff] [blame] | 440 | } |
Chris Lattner | ae64043 | 2002-12-17 02:50:10 +0000 | [diff] [blame] | 441 | } |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 442 | } |
| 443 | |
Chris Lattner | ae64043 | 2002-12-17 02:50:10 +0000 | [diff] [blame] | 444 | assert(PhysReg && "Physical register not assigned!?!?"); |
| 445 | |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 446 | // At this point PhysRegsUseOrder[i] is the least recently used register of |
| 447 | // compatible register class. Spill it to memory and reap its remains. |
Chris Lattner | c21be92 | 2002-12-16 17:44:42 +0000 | [diff] [blame] | 448 | spillPhysReg(MBB, I, PhysReg); |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | // Now that we know which register we need to assign this to, do it now! |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 452 | assignVirtToPhysReg(VirtReg, PhysReg); |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 453 | return PhysReg; |
| 454 | } |
| 455 | |
Chris Lattner | ae64043 | 2002-12-17 02:50:10 +0000 | [diff] [blame] | 456 | |
Chris Lattner | 42e0a8f | 2004-02-17 03:57:19 +0000 | [diff] [blame] | 457 | /// reloadVirtReg - This method transforms the specified specified virtual |
| 458 | /// register use to refer to a physical register. This method may do this in |
| 459 | /// one of several ways: if the register is available in a physical register |
| 460 | /// already, it uses that physical register. If the value is not in a physical |
| 461 | /// register, and if there are physical registers available, it loads it into a |
| 462 | /// register. If register pressure is high, and it is possible, it tries to |
| 463 | /// fold the load of the virtual register into the instruction itself. It |
| 464 | /// avoids doing this if register pressure is low to improve the chance that |
| 465 | /// subsequent instructions can use the reloaded value. This method returns the |
| 466 | /// modified instruction. |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 467 | /// |
Chris Lattner | 42e0a8f | 2004-02-17 03:57:19 +0000 | [diff] [blame] | 468 | MachineInstr *RA::reloadVirtReg(MachineBasicBlock &MBB, MachineInstr *MI, |
| 469 | unsigned OpNum) { |
| 470 | unsigned VirtReg = MI->getOperand(OpNum).getReg(); |
| 471 | |
| 472 | // If the virtual register is already available, just update the instruction |
| 473 | // and return. |
Alkis Evlogimenos | 4de473b | 2004-02-13 18:20:47 +0000 | [diff] [blame] | 474 | if (unsigned PR = getVirt2PhysRegMapSlot(VirtReg)) { |
Chris Lattner | 42e0a8f | 2004-02-17 03:57:19 +0000 | [diff] [blame] | 475 | MarkPhysRegRecentlyUsed(PR); // Already have this value available! |
Chris Lattner | e53f4a0 | 2006-05-04 17:52:23 +0000 | [diff] [blame] | 476 | MI->getOperand(OpNum).setReg(PR); // Assign the input register |
Chris Lattner | 42e0a8f | 2004-02-17 03:57:19 +0000 | [diff] [blame] | 477 | return MI; |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 478 | } |
| 479 | |
Chris Lattner | 1e3812c | 2004-02-17 04:08:37 +0000 | [diff] [blame] | 480 | // Otherwise, we need to fold it into the current instruction, or reload it. |
| 481 | // If we have registers available to hold the value, use them. |
Chris Lattner | ff863ba | 2002-12-25 05:05:46 +0000 | [diff] [blame] | 482 | const TargetRegisterClass *RC = MF->getSSARegMap()->getRegClass(VirtReg); |
Chris Lattner | 1e3812c | 2004-02-17 04:08:37 +0000 | [diff] [blame] | 483 | unsigned PhysReg = getFreeReg(RC); |
Chris Lattner | 11390e7 | 2004-02-17 08:09:40 +0000 | [diff] [blame] | 484 | int FrameIndex = getStackSpaceFor(VirtReg, RC); |
Chris Lattner | 1e3812c | 2004-02-17 04:08:37 +0000 | [diff] [blame] | 485 | |
Chris Lattner | 11390e7 | 2004-02-17 08:09:40 +0000 | [diff] [blame] | 486 | if (PhysReg) { // Register is available, allocate it! |
| 487 | assignVirtToPhysReg(VirtReg, PhysReg); |
| 488 | } else { // No registers available. |
| 489 | // If we can fold this spill into this instruction, do so now. |
Alkis Evlogimenos | 39354c9 | 2004-03-14 07:19:51 +0000 | [diff] [blame] | 490 | if (MachineInstr* FMI = RegInfo->foldMemoryOperand(MI, OpNum, FrameIndex)){ |
Alkis Evlogimenos | d6f6d1a | 2004-02-21 18:07:33 +0000 | [diff] [blame] | 491 | ++NumFolded; |
Chris Lattner | d368c61 | 2004-02-19 18:34:02 +0000 | [diff] [blame] | 492 | // Since we changed the address of MI, make sure to update live variables |
| 493 | // to know that the new instruction has the properties of the old one. |
Alkis Evlogimenos | 39354c9 | 2004-03-14 07:19:51 +0000 | [diff] [blame] | 494 | LV->instructionChanged(MI, FMI); |
| 495 | return MBB.insert(MBB.erase(MI), FMI); |
Chris Lattner | 1e3812c | 2004-02-17 04:08:37 +0000 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | // It looks like we can't fold this virtual register load into this |
| 499 | // instruction. Force some poor hapless value out of the register file to |
| 500 | // make room for the new register, and reload it. |
| 501 | PhysReg = getReg(MBB, MI, VirtReg); |
| 502 | } |
| 503 | |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 504 | markVirtRegModified(VirtReg, false); // Note that this reg was just reloaded |
| 505 | |
Bill Wendling | b2b9c20 | 2006-11-17 02:09:07 +0000 | [diff] [blame] | 506 | DOUT << " Reloading %reg" << VirtReg << " into " |
| 507 | << RegInfo->getName(PhysReg) << "\n"; |
Chris Lattner | b8822ad | 2003-08-04 23:36:39 +0000 | [diff] [blame] | 508 | |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 509 | // Add move instruction(s) |
Chris Lattner | bf9716b | 2005-09-30 01:29:00 +0000 | [diff] [blame] | 510 | RegInfo->loadRegFromStackSlot(MBB, MI, PhysReg, FrameIndex, RC); |
Alkis Evlogimenos | 2acef2d | 2004-02-19 06:19:09 +0000 | [diff] [blame] | 511 | ++NumLoads; // Update statistics |
Chris Lattner | 42e0a8f | 2004-02-17 03:57:19 +0000 | [diff] [blame] | 512 | |
Evan Cheng | 6c087e5 | 2007-04-25 22:13:27 +0000 | [diff] [blame^] | 513 | MF->setPhysRegUsed(PhysReg); |
Chris Lattner | e53f4a0 | 2006-05-04 17:52:23 +0000 | [diff] [blame] | 514 | MI->getOperand(OpNum).setReg(PhysReg); // Assign the input register |
Chris Lattner | 42e0a8f | 2004-02-17 03:57:19 +0000 | [diff] [blame] | 515 | return MI; |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 516 | } |
| 517 | |
Chris Lattner | b8822ad | 2003-08-04 23:36:39 +0000 | [diff] [blame] | 518 | |
| 519 | |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 520 | void RA::AllocateBasicBlock(MachineBasicBlock &MBB) { |
| 521 | // loop over each instruction |
Chris Lattner | e6a88ac | 2005-11-09 18:22:42 +0000 | [diff] [blame] | 522 | MachineBasicBlock::iterator MII = MBB.begin(); |
| 523 | const TargetInstrInfo &TII = *TM->getInstrInfo(); |
Chris Lattner | 44500e3 | 2006-06-15 22:21:53 +0000 | [diff] [blame] | 524 | |
Evan Cheng | ddee842 | 2006-11-15 20:55:15 +0000 | [diff] [blame] | 525 | DEBUG(const BasicBlock *LBB = MBB.getBasicBlock(); |
Bill Wendling | b2b9c20 | 2006-11-17 02:09:07 +0000 | [diff] [blame] | 526 | if (LBB) DOUT << "\nStarting RegAlloc of BB: " << LBB->getName()); |
Evan Cheng | ddee842 | 2006-11-15 20:55:15 +0000 | [diff] [blame] | 527 | |
Chris Lattner | 44500e3 | 2006-06-15 22:21:53 +0000 | [diff] [blame] | 528 | // If this is the first basic block in the machine function, add live-in |
| 529 | // registers as active. |
| 530 | if (&MBB == &*MF->begin()) { |
| 531 | for (MachineFunction::livein_iterator I = MF->livein_begin(), |
| 532 | E = MF->livein_end(); I != E; ++I) { |
| 533 | unsigned Reg = I->first; |
Evan Cheng | 6c087e5 | 2007-04-25 22:13:27 +0000 | [diff] [blame^] | 534 | MF->setPhysRegUsed(Reg); |
Chris Lattner | 44500e3 | 2006-06-15 22:21:53 +0000 | [diff] [blame] | 535 | PhysRegsUsed[Reg] = 0; // It is free and reserved now |
| 536 | PhysRegsUseOrder.push_back(Reg); |
| 537 | for (const unsigned *AliasSet = RegInfo->getAliasSet(Reg); |
| 538 | *AliasSet; ++AliasSet) { |
Chris Lattner | 45d5788 | 2006-09-08 19:03:30 +0000 | [diff] [blame] | 539 | if (PhysRegsUsed[*AliasSet] != -2) { |
| 540 | PhysRegsUseOrder.push_back(*AliasSet); |
| 541 | PhysRegsUsed[*AliasSet] = 0; // It is free and reserved now |
Evan Cheng | 6c087e5 | 2007-04-25 22:13:27 +0000 | [diff] [blame^] | 542 | MF->setPhysRegUsed(*AliasSet); |
Chris Lattner | 45d5788 | 2006-09-08 19:03:30 +0000 | [diff] [blame] | 543 | } |
Chris Lattner | 44500e3 | 2006-06-15 22:21:53 +0000 | [diff] [blame] | 544 | } |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | // Otherwise, sequentially allocate each instruction in the MBB. |
Chris Lattner | e6a88ac | 2005-11-09 18:22:42 +0000 | [diff] [blame] | 549 | while (MII != MBB.end()) { |
| 550 | MachineInstr *MI = MII++; |
| 551 | const TargetInstrDescriptor &TID = TII.get(MI->getOpcode()); |
Bill Wendling | b2b9c20 | 2006-11-17 02:09:07 +0000 | [diff] [blame] | 552 | DEBUG(DOUT << "\nStarting RegAlloc of: " << *MI; |
| 553 | DOUT << " Regs have values: "; |
Chris Lattner | 64667b6 | 2004-02-09 01:26:13 +0000 | [diff] [blame] | 554 | for (unsigned i = 0; i != RegInfo->getNumRegs(); ++i) |
Chris Lattner | 45d5788 | 2006-09-08 19:03:30 +0000 | [diff] [blame] | 555 | if (PhysRegsUsed[i] != -1 && PhysRegsUsed[i] != -2) |
Bill Wendling | b2b9c20 | 2006-11-17 02:09:07 +0000 | [diff] [blame] | 556 | DOUT << "[" << RegInfo->getName(i) |
| 557 | << ",%reg" << PhysRegsUsed[i] << "] "; |
| 558 | DOUT << "\n"); |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 559 | |
Chris Lattner | ae64043 | 2002-12-17 02:50:10 +0000 | [diff] [blame] | 560 | // Loop over the implicit uses, making sure that they are at the head of the |
| 561 | // use order list, so they don't get reallocated. |
Jim Laskey | cd4317e | 2006-07-21 21:15:20 +0000 | [diff] [blame] | 562 | if (TID.ImplicitUses) { |
| 563 | for (const unsigned *ImplicitUses = TID.ImplicitUses; |
| 564 | *ImplicitUses; ++ImplicitUses) |
| 565 | MarkPhysRegRecentlyUsed(*ImplicitUses); |
| 566 | } |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 567 | |
Evan Cheng | ddee842 | 2006-11-15 20:55:15 +0000 | [diff] [blame] | 568 | SmallVector<unsigned, 8> Kills; |
| 569 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 570 | MachineOperand& MO = MI->getOperand(i); |
| 571 | if (MO.isRegister() && MO.isKill()) |
| 572 | Kills.push_back(MO.getReg()); |
| 573 | } |
| 574 | |
Brian Gaeke | 53b99a0 | 2003-08-15 21:19:25 +0000 | [diff] [blame] | 575 | // Get the used operands into registers. This has the potential to spill |
Chris Lattner | b8822ad | 2003-08-04 23:36:39 +0000 | [diff] [blame] | 576 | // incoming values if we are out of registers. Note that we completely |
| 577 | // ignore physical register uses here. We assume that if an explicit |
| 578 | // physical register is referenced by the instruction, that it is guaranteed |
| 579 | // to be live-in, or the input is badly hosed. |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 580 | // |
Alkis Evlogimenos | 71e353e | 2004-02-26 22:00:20 +0000 | [diff] [blame] | 581 | for (unsigned i = 0; i != MI->getNumOperands(); ++i) { |
| 582 | MachineOperand& MO = MI->getOperand(i); |
| 583 | // here we are looking for only used operands (never def&use) |
Evan Cheng | ddee842 | 2006-11-15 20:55:15 +0000 | [diff] [blame] | 584 | if (MO.isRegister() && !MO.isDef() && MO.getReg() && !MO.isImplicit() && |
Alkis Evlogimenos | 71e353e | 2004-02-26 22:00:20 +0000 | [diff] [blame] | 585 | MRegisterInfo::isVirtualRegister(MO.getReg())) |
Chris Lattner | 42e0a8f | 2004-02-17 03:57:19 +0000 | [diff] [blame] | 586 | MI = reloadVirtReg(MBB, MI, i); |
Alkis Evlogimenos | 71e353e | 2004-02-26 22:00:20 +0000 | [diff] [blame] | 587 | } |
Alkis Evlogimenos | 4de473b | 2004-02-13 18:20:47 +0000 | [diff] [blame] | 588 | |
Evan Cheng | ddee842 | 2006-11-15 20:55:15 +0000 | [diff] [blame] | 589 | // If this instruction is the last user of this register, kill the |
Chris Lattner | 56ddada | 2004-02-17 17:49:10 +0000 | [diff] [blame] | 590 | // value, freeing the register being used, so it doesn't need to be |
| 591 | // spilled to memory. |
| 592 | // |
Evan Cheng | ddee842 | 2006-11-15 20:55:15 +0000 | [diff] [blame] | 593 | for (unsigned i = 0, e = Kills.size(); i != e; ++i) { |
| 594 | unsigned VirtReg = Kills[i]; |
Chris Lattner | 56ddada | 2004-02-17 17:49:10 +0000 | [diff] [blame] | 595 | unsigned PhysReg = VirtReg; |
| 596 | if (MRegisterInfo::isVirtualRegister(VirtReg)) { |
| 597 | // If the virtual register was never materialized into a register, it |
| 598 | // might not be in the map, but it won't hurt to zero it out anyway. |
| 599 | unsigned &PhysRegSlot = getVirt2PhysRegMapSlot(VirtReg); |
| 600 | PhysReg = PhysRegSlot; |
| 601 | PhysRegSlot = 0; |
Chris Lattner | 0c5b8da | 2006-09-08 20:21:31 +0000 | [diff] [blame] | 602 | } else if (PhysRegsUsed[PhysReg] == -2) { |
| 603 | // Unallocatable register dead, ignore. |
| 604 | continue; |
Chris Lattner | 56ddada | 2004-02-17 17:49:10 +0000 | [diff] [blame] | 605 | } |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 606 | |
Chris Lattner | 56ddada | 2004-02-17 17:49:10 +0000 | [diff] [blame] | 607 | if (PhysReg) { |
Bill Wendling | b2b9c20 | 2006-11-17 02:09:07 +0000 | [diff] [blame] | 608 | DOUT << " Last use of " << RegInfo->getName(PhysReg) |
| 609 | << "[%reg" << VirtReg <<"], removing it from live set\n"; |
Chris Lattner | 56ddada | 2004-02-17 17:49:10 +0000 | [diff] [blame] | 610 | removePhysReg(PhysReg); |
Evan Cheng | ddee842 | 2006-11-15 20:55:15 +0000 | [diff] [blame] | 611 | for (const unsigned *AliasSet = RegInfo->getAliasSet(PhysReg); |
| 612 | *AliasSet; ++AliasSet) { |
| 613 | if (PhysRegsUsed[*AliasSet] != -2) { |
Bill Wendling | b2b9c20 | 2006-11-17 02:09:07 +0000 | [diff] [blame] | 614 | DOUT << " Last use of " |
Evan Cheng | ddee842 | 2006-11-15 20:55:15 +0000 | [diff] [blame] | 615 | << RegInfo->getName(*AliasSet) |
Bill Wendling | b2b9c20 | 2006-11-17 02:09:07 +0000 | [diff] [blame] | 616 | << "[%reg" << VirtReg <<"], removing it from live set\n"; |
Evan Cheng | ddee842 | 2006-11-15 20:55:15 +0000 | [diff] [blame] | 617 | removePhysReg(*AliasSet); |
| 618 | } |
| 619 | } |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 620 | } |
| 621 | } |
| 622 | |
| 623 | // Loop over all of the operands of the instruction, spilling registers that |
| 624 | // are defined, and marking explicit destinations in the PhysRegsUsed map. |
Alkis Evlogimenos | 71e353e | 2004-02-26 22:00:20 +0000 | [diff] [blame] | 625 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 626 | MachineOperand& MO = MI->getOperand(i); |
Evan Cheng | 438f7bc | 2006-11-10 08:43:01 +0000 | [diff] [blame] | 627 | if (MO.isRegister() && MO.isDef() && !MO.isImplicit() && MO.getReg() && |
Alkis Evlogimenos | 71e353e | 2004-02-26 22:00:20 +0000 | [diff] [blame] | 628 | MRegisterInfo::isPhysicalRegister(MO.getReg())) { |
| 629 | unsigned Reg = MO.getReg(); |
Chris Lattner | cc40632 | 2006-09-08 19:11:11 +0000 | [diff] [blame] | 630 | if (PhysRegsUsed[Reg] == -2) continue; // Something like ESP. |
| 631 | |
Evan Cheng | 6c087e5 | 2007-04-25 22:13:27 +0000 | [diff] [blame^] | 632 | MF->setPhysRegUsed(Reg); |
Evan Cheng | ddee842 | 2006-11-15 20:55:15 +0000 | [diff] [blame] | 633 | spillPhysReg(MBB, MI, Reg, true); // Spill any existing value in reg |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 634 | PhysRegsUsed[Reg] = 0; // It is free and reserved now |
| 635 | PhysRegsUseOrder.push_back(Reg); |
Alkis Evlogimenos | 19b6486 | 2004-01-13 06:24:30 +0000 | [diff] [blame] | 636 | for (const unsigned *AliasSet = RegInfo->getAliasSet(Reg); |
| 637 | *AliasSet; ++AliasSet) { |
Chris Lattner | 45d5788 | 2006-09-08 19:03:30 +0000 | [diff] [blame] | 638 | if (PhysRegsUsed[*AliasSet] != -2) { |
| 639 | PhysRegsUseOrder.push_back(*AliasSet); |
| 640 | PhysRegsUsed[*AliasSet] = 0; // It is free and reserved now |
Evan Cheng | 6c087e5 | 2007-04-25 22:13:27 +0000 | [diff] [blame^] | 641 | MF->setPhysRegUsed(*AliasSet); |
Chris Lattner | 45d5788 | 2006-09-08 19:03:30 +0000 | [diff] [blame] | 642 | } |
Alkis Evlogimenos | 19b6486 | 2004-01-13 06:24:30 +0000 | [diff] [blame] | 643 | } |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 644 | } |
Alkis Evlogimenos | 71e353e | 2004-02-26 22:00:20 +0000 | [diff] [blame] | 645 | } |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 646 | |
| 647 | // Loop over the implicit defs, spilling them as well. |
Jim Laskey | cd4317e | 2006-07-21 21:15:20 +0000 | [diff] [blame] | 648 | if (TID.ImplicitDefs) { |
| 649 | for (const unsigned *ImplicitDefs = TID.ImplicitDefs; |
| 650 | *ImplicitDefs; ++ImplicitDefs) { |
| 651 | unsigned Reg = *ImplicitDefs; |
Chris Lattner | 2b41b8e | 2006-09-19 18:02:01 +0000 | [diff] [blame] | 652 | bool IsNonAllocatable = PhysRegsUsed[Reg] == -2; |
| 653 | if (!IsNonAllocatable) { |
| 654 | spillPhysReg(MBB, MI, Reg, true); |
| 655 | PhysRegsUseOrder.push_back(Reg); |
| 656 | PhysRegsUsed[Reg] = 0; // It is free and reserved now |
| 657 | } |
Evan Cheng | 6c087e5 | 2007-04-25 22:13:27 +0000 | [diff] [blame^] | 658 | MF->setPhysRegUsed(Reg); |
Chris Lattner | 0648b16 | 2005-01-23 22:51:56 +0000 | [diff] [blame] | 659 | |
Jim Laskey | cd4317e | 2006-07-21 21:15:20 +0000 | [diff] [blame] | 660 | for (const unsigned *AliasSet = RegInfo->getAliasSet(Reg); |
| 661 | *AliasSet; ++AliasSet) { |
Chris Lattner | 45d5788 | 2006-09-08 19:03:30 +0000 | [diff] [blame] | 662 | if (PhysRegsUsed[*AliasSet] != -2) { |
Chris Lattner | 2b41b8e | 2006-09-19 18:02:01 +0000 | [diff] [blame] | 663 | if (!IsNonAllocatable) { |
| 664 | PhysRegsUseOrder.push_back(*AliasSet); |
| 665 | PhysRegsUsed[*AliasSet] = 0; // It is free and reserved now |
| 666 | } |
Evan Cheng | 6c087e5 | 2007-04-25 22:13:27 +0000 | [diff] [blame^] | 667 | MF->setPhysRegUsed(*AliasSet); |
Chris Lattner | 45d5788 | 2006-09-08 19:03:30 +0000 | [diff] [blame] | 668 | } |
Jim Laskey | cd4317e | 2006-07-21 21:15:20 +0000 | [diff] [blame] | 669 | } |
Alkis Evlogimenos | 19b6486 | 2004-01-13 06:24:30 +0000 | [diff] [blame] | 670 | } |
Alkis Evlogimenos | efe995a | 2003-12-13 01:20:58 +0000 | [diff] [blame] | 671 | } |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 672 | |
Evan Cheng | ddee842 | 2006-11-15 20:55:15 +0000 | [diff] [blame] | 673 | SmallVector<unsigned, 8> DeadDefs; |
| 674 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 675 | MachineOperand& MO = MI->getOperand(i); |
| 676 | if (MO.isRegister() && MO.isDead()) |
| 677 | DeadDefs.push_back(MO.getReg()); |
| 678 | } |
| 679 | |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 680 | // Okay, we have allocated all of the source operands and spilled any values |
| 681 | // that would be destroyed by defs of this instruction. Loop over the |
Chris Lattner | 0648b16 | 2005-01-23 22:51:56 +0000 | [diff] [blame] | 682 | // explicit defs and assign them to a register, spilling incoming values if |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 683 | // we need to scavenge a register. |
Chris Lattner | 82bee0f | 2002-12-18 08:14:26 +0000 | [diff] [blame] | 684 | // |
Alkis Evlogimenos | 71e353e | 2004-02-26 22:00:20 +0000 | [diff] [blame] | 685 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 686 | MachineOperand& MO = MI->getOperand(i); |
Evan Cheng | 5d8062b | 2006-09-05 20:32:06 +0000 | [diff] [blame] | 687 | if (MO.isRegister() && MO.isDef() && MO.getReg() && |
Alkis Evlogimenos | 71e353e | 2004-02-26 22:00:20 +0000 | [diff] [blame] | 688 | MRegisterInfo::isVirtualRegister(MO.getReg())) { |
| 689 | unsigned DestVirtReg = MO.getReg(); |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 690 | unsigned DestPhysReg; |
| 691 | |
Alkis Evlogimenos | 9af9dbd | 2003-12-18 13:08:52 +0000 | [diff] [blame] | 692 | // If DestVirtReg already has a value, use it. |
Alkis Evlogimenos | 4de473b | 2004-02-13 18:20:47 +0000 | [diff] [blame] | 693 | if (!(DestPhysReg = getVirt2PhysRegMapSlot(DestVirtReg))) |
Alkis Evlogimenos | c0b9dc5 | 2004-02-12 02:27:10 +0000 | [diff] [blame] | 694 | DestPhysReg = getReg(MBB, MI, DestVirtReg); |
Evan Cheng | 6c087e5 | 2007-04-25 22:13:27 +0000 | [diff] [blame^] | 695 | MF->setPhysRegUsed(DestPhysReg); |
Chris Lattner | d572563 | 2003-05-12 03:54:14 +0000 | [diff] [blame] | 696 | markVirtRegModified(DestVirtReg); |
Chris Lattner | e53f4a0 | 2006-05-04 17:52:23 +0000 | [diff] [blame] | 697 | MI->getOperand(i).setReg(DestPhysReg); // Assign the output register |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 698 | } |
Alkis Evlogimenos | 71e353e | 2004-02-26 22:00:20 +0000 | [diff] [blame] | 699 | } |
Chris Lattner | 82bee0f | 2002-12-18 08:14:26 +0000 | [diff] [blame] | 700 | |
Chris Lattner | 56ddada | 2004-02-17 17:49:10 +0000 | [diff] [blame] | 701 | // If this instruction defines any registers that are immediately dead, |
| 702 | // kill them now. |
| 703 | // |
Evan Cheng | ddee842 | 2006-11-15 20:55:15 +0000 | [diff] [blame] | 704 | for (unsigned i = 0, e = DeadDefs.size(); i != e; ++i) { |
| 705 | unsigned VirtReg = DeadDefs[i]; |
Chris Lattner | 56ddada | 2004-02-17 17:49:10 +0000 | [diff] [blame] | 706 | unsigned PhysReg = VirtReg; |
| 707 | if (MRegisterInfo::isVirtualRegister(VirtReg)) { |
| 708 | unsigned &PhysRegSlot = getVirt2PhysRegMapSlot(VirtReg); |
| 709 | PhysReg = PhysRegSlot; |
| 710 | assert(PhysReg != 0); |
| 711 | PhysRegSlot = 0; |
Chris Lattner | 0c5b8da | 2006-09-08 20:21:31 +0000 | [diff] [blame] | 712 | } else if (PhysRegsUsed[PhysReg] == -2) { |
| 713 | // Unallocatable register dead, ignore. |
| 714 | continue; |
Chris Lattner | 56ddada | 2004-02-17 17:49:10 +0000 | [diff] [blame] | 715 | } |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 716 | |
Chris Lattner | 56ddada | 2004-02-17 17:49:10 +0000 | [diff] [blame] | 717 | if (PhysReg) { |
Bill Wendling | b2b9c20 | 2006-11-17 02:09:07 +0000 | [diff] [blame] | 718 | DOUT << " Register " << RegInfo->getName(PhysReg) |
Chris Lattner | 56ddada | 2004-02-17 17:49:10 +0000 | [diff] [blame] | 719 | << " [%reg" << VirtReg |
Bill Wendling | b2b9c20 | 2006-11-17 02:09:07 +0000 | [diff] [blame] | 720 | << "] is never used, removing it frame live list\n"; |
Chris Lattner | 56ddada | 2004-02-17 17:49:10 +0000 | [diff] [blame] | 721 | removePhysReg(PhysReg); |
Evan Cheng | ddee842 | 2006-11-15 20:55:15 +0000 | [diff] [blame] | 722 | for (const unsigned *AliasSet = RegInfo->getAliasSet(PhysReg); |
| 723 | *AliasSet; ++AliasSet) { |
| 724 | if (PhysRegsUsed[*AliasSet] != -2) { |
Bill Wendling | b2b9c20 | 2006-11-17 02:09:07 +0000 | [diff] [blame] | 725 | DOUT << " Register " << RegInfo->getName(*AliasSet) |
Evan Cheng | ddee842 | 2006-11-15 20:55:15 +0000 | [diff] [blame] | 726 | << " [%reg" << *AliasSet |
Bill Wendling | b2b9c20 | 2006-11-17 02:09:07 +0000 | [diff] [blame] | 727 | << "] is never used, removing it frame live list\n"; |
Evan Cheng | ddee842 | 2006-11-15 20:55:15 +0000 | [diff] [blame] | 728 | removePhysReg(*AliasSet); |
| 729 | } |
| 730 | } |
Chris Lattner | 82bee0f | 2002-12-18 08:14:26 +0000 | [diff] [blame] | 731 | } |
| 732 | } |
Chris Lattner | e6a88ac | 2005-11-09 18:22:42 +0000 | [diff] [blame] | 733 | |
| 734 | // Finally, if this is a noop copy instruction, zap it. |
| 735 | unsigned SrcReg, DstReg; |
Chris Lattner | 2ac0d43 | 2006-09-03 00:06:08 +0000 | [diff] [blame] | 736 | if (TII.isMoveInstr(*MI, SrcReg, DstReg) && SrcReg == DstReg) { |
| 737 | LV->removeVirtualRegistersKilled(MI); |
| 738 | LV->removeVirtualRegistersDead(MI); |
Chris Lattner | e6a88ac | 2005-11-09 18:22:42 +0000 | [diff] [blame] | 739 | MBB.erase(MI); |
Chris Lattner | 2ac0d43 | 2006-09-03 00:06:08 +0000 | [diff] [blame] | 740 | } |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 741 | } |
| 742 | |
Chris Lattner | e6a88ac | 2005-11-09 18:22:42 +0000 | [diff] [blame] | 743 | MachineBasicBlock::iterator MI = MBB.getFirstTerminator(); |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 744 | |
| 745 | // Spill all physical registers holding virtual registers now. |
Chris Lattner | 64667b6 | 2004-02-09 01:26:13 +0000 | [diff] [blame] | 746 | for (unsigned i = 0, e = RegInfo->getNumRegs(); i != e; ++i) |
Chris Lattner | 45d5788 | 2006-09-08 19:03:30 +0000 | [diff] [blame] | 747 | if (PhysRegsUsed[i] != -1 && PhysRegsUsed[i] != -2) |
Chris Lattner | 64667b6 | 2004-02-09 01:26:13 +0000 | [diff] [blame] | 748 | if (unsigned VirtReg = PhysRegsUsed[i]) |
Alkis Evlogimenos | c0b9dc5 | 2004-02-12 02:27:10 +0000 | [diff] [blame] | 749 | spillVirtReg(MBB, MI, VirtReg, i); |
Chris Lattner | 64667b6 | 2004-02-09 01:26:13 +0000 | [diff] [blame] | 750 | else |
| 751 | removePhysReg(i); |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 752 | |
Chris Lattner | 9a5ef20 | 2005-11-09 05:28:45 +0000 | [diff] [blame] | 753 | #if 0 |
| 754 | // This checking code is very expensive. |
Chris Lattner | ecea563 | 2004-02-09 02:12:04 +0000 | [diff] [blame] | 755 | bool AllOk = true; |
Alkis Evlogimenos | 4d0d864 | 2004-02-25 21:55:45 +0000 | [diff] [blame] | 756 | for (unsigned i = MRegisterInfo::FirstVirtualRegister, |
| 757 | e = MF->getSSARegMap()->getLastVirtReg(); i <= e; ++i) |
Chris Lattner | ecea563 | 2004-02-09 02:12:04 +0000 | [diff] [blame] | 758 | if (unsigned PR = Virt2PhysRegMap[i]) { |
Bill Wendling | 832171c | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 759 | cerr << "Register still mapped: " << i << " -> " << PR << "\n"; |
Chris Lattner | ecea563 | 2004-02-09 02:12:04 +0000 | [diff] [blame] | 760 | AllOk = false; |
| 761 | } |
| 762 | assert(AllOk && "Virtual registers still in phys regs?"); |
| 763 | #endif |
Alkis Evlogimenos | 4de473b | 2004-02-13 18:20:47 +0000 | [diff] [blame] | 764 | |
Chris Lattner | 128c2aa | 2003-08-17 18:01:15 +0000 | [diff] [blame] | 765 | // Clear any physical register which appear live at the end of the basic |
| 766 | // block, but which do not hold any virtual registers. e.g., the stack |
| 767 | // pointer. |
| 768 | PhysRegsUseOrder.clear(); |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 769 | } |
| 770 | |
Chris Lattner | 86c69a6 | 2002-12-17 03:16:10 +0000 | [diff] [blame] | 771 | |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 772 | /// runOnMachineFunction - Register allocate the whole function |
| 773 | /// |
| 774 | bool RA::runOnMachineFunction(MachineFunction &Fn) { |
Bill Wendling | b2b9c20 | 2006-11-17 02:09:07 +0000 | [diff] [blame] | 775 | DOUT << "Machine Function " << "\n"; |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 776 | MF = &Fn; |
Chris Lattner | 580f9be | 2002-12-28 20:40:43 +0000 | [diff] [blame] | 777 | TM = &Fn.getTarget(); |
| 778 | RegInfo = TM->getRegisterInfo(); |
Chris Lattner | 56ddada | 2004-02-17 17:49:10 +0000 | [diff] [blame] | 779 | LV = &getAnalysis<LiveVariables>(); |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 780 | |
Alkis Evlogimenos | 4de473b | 2004-02-13 18:20:47 +0000 | [diff] [blame] | 781 | PhysRegsUsed.assign(RegInfo->getNumRegs(), -1); |
Chris Lattner | 45d5788 | 2006-09-08 19:03:30 +0000 | [diff] [blame] | 782 | |
| 783 | // At various places we want to efficiently check to see whether a register |
| 784 | // is allocatable. To handle this, we mark all unallocatable registers as |
| 785 | // being pinned down, permanently. |
| 786 | { |
Evan Cheng | 61de82d | 2007-02-15 05:59:24 +0000 | [diff] [blame] | 787 | BitVector Allocable = RegInfo->getAllocatableSet(Fn); |
Chris Lattner | 45d5788 | 2006-09-08 19:03:30 +0000 | [diff] [blame] | 788 | for (unsigned i = 0, e = Allocable.size(); i != e; ++i) |
| 789 | if (!Allocable[i]) |
| 790 | PhysRegsUsed[i] = -2; // Mark the reg unallocable. |
| 791 | } |
Chris Lattner | 64667b6 | 2004-02-09 01:26:13 +0000 | [diff] [blame] | 792 | |
Alkis Evlogimenos | 4de473b | 2004-02-13 18:20:47 +0000 | [diff] [blame] | 793 | // initialize the virtual->physical register map to have a 'null' |
| 794 | // mapping for all virtual registers |
Alkis Evlogimenos | 4d0d864 | 2004-02-25 21:55:45 +0000 | [diff] [blame] | 795 | Virt2PhysRegMap.grow(MF->getSSARegMap()->getLastVirtReg()); |
Chris Lattner | ecea563 | 2004-02-09 02:12:04 +0000 | [diff] [blame] | 796 | |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 797 | // Loop over all of the basic blocks, eliminating virtual register references |
| 798 | for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end(); |
| 799 | MBB != MBBe; ++MBB) |
| 800 | AllocateBasicBlock(*MBB); |
| 801 | |
Chris Lattner | 580f9be | 2002-12-28 20:40:43 +0000 | [diff] [blame] | 802 | StackSlotForVirtReg.clear(); |
Alkis Evlogimenos | 4de473b | 2004-02-13 18:20:47 +0000 | [diff] [blame] | 803 | PhysRegsUsed.clear(); |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 804 | VirtRegModified.clear(); |
Chris Lattner | ecea563 | 2004-02-09 02:12:04 +0000 | [diff] [blame] | 805 | Virt2PhysRegMap.clear(); |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 806 | return true; |
| 807 | } |
| 808 | |
Chris Lattner | ef09c63 | 2004-01-31 21:27:19 +0000 | [diff] [blame] | 809 | FunctionPass *llvm::createLocalRegisterAllocator() { |
Chris Lattner | 580f9be | 2002-12-28 20:40:43 +0000 | [diff] [blame] | 810 | return new RA(); |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 811 | } |