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