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