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