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