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" |
Chris Lattner | 3501fea | 2003-01-14 22:00:31 +0000 | [diff] [blame] | 22 | #include "llvm/Target/TargetInstrInfo.h" |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 23 | #include "llvm/Target/TargetMachine.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 24 | #include "llvm/Support/CommandLine.h" |
| 25 | #include "llvm/Support/Debug.h" |
Chris Lattner | 9525528 | 2006-06-28 23:17:24 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Visibility.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/DenseMap.h" |
| 28 | #include "llvm/ADT/Statistic.h" |
Chris Lattner | 27f2916 | 2004-10-26 15:35:58 +0000 | [diff] [blame] | 29 | #include <algorithm> |
Chris Lattner | 2c2c6c6 | 2006-01-22 23:41:00 +0000 | [diff] [blame] | 30 | #include <iostream> |
Chris Lattner | ef09c63 | 2004-01-31 21:27:19 +0000 | [diff] [blame] | 31 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 32 | |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 33 | namespace { |
Alkis Evlogimenos | 2acef2d | 2004-02-19 06:19:09 +0000 | [diff] [blame] | 34 | Statistic<> NumStores("ra-local", "Number of stores added"); |
| 35 | Statistic<> NumLoads ("ra-local", "Number of loads added"); |
Alkis Evlogimenos | d6f6d1a | 2004-02-21 18:07:33 +0000 | [diff] [blame] | 36 | Statistic<> NumFolded("ra-local", "Number of loads/stores folded into " |
| 37 | "instructions"); |
Chris Lattner | 9525528 | 2006-06-28 23:17:24 +0000 | [diff] [blame] | 38 | class VISIBILITY_HIDDEN RA : public MachineFunctionPass { |
Chris Lattner | 580f9be | 2002-12-28 20:40:43 +0000 | [diff] [blame] | 39 | const TargetMachine *TM; |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 40 | MachineFunction *MF; |
Chris Lattner | 580f9be | 2002-12-28 20:40:43 +0000 | [diff] [blame] | 41 | const MRegisterInfo *RegInfo; |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 42 | LiveVariables *LV; |
Chris Lattner | 0648b16 | 2005-01-23 22:51:56 +0000 | [diff] [blame] | 43 | bool *PhysRegsEverUsed; |
Chris Lattner | ff863ba | 2002-12-25 05:05:46 +0000 | [diff] [blame] | 44 | |
Chris Lattner | b8822ad | 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 | 580f9be | 2002-12-28 20:40:43 +0000 | [diff] [blame] | 47 | std::map<unsigned, int> StackSlotForVirtReg; |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 48 | |
| 49 | // Virt2PhysRegMap - This map contains entries for each virtual register |
Alkis Evlogimenos | 4d0d864 | 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 | ecea563 | 2004-02-09 02:12:04 +0000 | [diff] [blame] | 52 | |
| 53 | unsigned &getVirt2PhysRegMapSlot(unsigned VirtReg) { |
Alkis Evlogimenos | 4d0d864 | 2004-02-25 21:55:45 +0000 | [diff] [blame] | 54 | return Virt2PhysRegMap[VirtReg]; |
Chris Lattner | ecea563 | 2004-02-09 02:12:04 +0000 | [diff] [blame] | 55 | } |
Alkis Evlogimenos | 4de473b | 2004-02-13 18:20:47 +0000 | [diff] [blame] | 56 | |
Chris Lattner | 64667b6 | 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 | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 64 | // |
Alkis Evlogimenos | 4de473b | 2004-02-13 18:20:47 +0000 | [diff] [blame] | 65 | std::vector<int> PhysRegsUsed; |
Chris Lattner | b74e83c | 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 | 91a452b | 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 | 82bee0f | 2002-12-18 08:14:26 +0000 | [diff] [blame] | 81 | // |
Chris Lattner | 91a452b | 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 | ef09c63 | 2004-01-31 21:27:19 +0000 | [diff] [blame] | 85 | assert(MRegisterInfo::isVirtualRegister(Reg) && "Illegal VirtReg!"); |
Chris Lattner | 91a452b | 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 | 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 | assert(Reg - MRegisterInfo::FirstVirtualRegister < VirtRegModified.size() |
Alkis Evlogimenos | 4de473b | 2004-02-13 18:20:47 +0000 | [diff] [blame] | 94 | && "Illegal virtual register!"); |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 95 | return VirtRegModified[Reg - MRegisterInfo::FirstVirtualRegister]; |
| 96 | } |
Chris Lattner | 82bee0f | 2002-12-18 08:14:26 +0000 | [diff] [blame] | 97 | |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 98 | void MarkPhysRegRecentlyUsed(unsigned Reg) { |
Chris Lattner | aebcce8 | 2004-06-16 06:57:29 +0000 | [diff] [blame] | 99 | if(PhysRegsUseOrder.empty() || |
| 100 | PhysRegsUseOrder.back() == Reg) return; // Already most recently used |
Chris Lattner | 0eb172c | 2002-12-24 00:04:55 +0000 | [diff] [blame] | 101 | |
| 102 | for (unsigned i = PhysRegsUseOrder.size(); i != 0; --i) |
Alkis Evlogimenos | 4de473b | 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 | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | public: |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 114 | virtual const char *getPassName() const { |
| 115 | return "Local Register Allocator"; |
| 116 | } |
| 117 | |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 118 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | 56ddada | 2004-02-17 17:49:10 +0000 | [diff] [blame] | 119 | AU.addRequired<LiveVariables>(); |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 120 | AU.addRequiredID(PHIEliminationID); |
Alkis Evlogimenos | 4c08086 | 2003-12-18 22:40:24 +0000 | [diff] [blame] | 121 | AU.addRequiredID(TwoAddressInstructionPassID); |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 122 | MachineFunctionPass::getAnalysisUsage(AU); |
| 123 | } |
| 124 | |
Chris Lattner | b74e83c | 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 | 82bee0f | 2002-12-18 08:14:26 +0000 | [diff] [blame] | 132 | |
Chris Lattner | 82bee0f | 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 | 73ff512 | 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 | 82bee0f | 2002-12-18 08:14:26 +0000 | [diff] [blame] | 143 | return false; |
| 144 | } |
| 145 | |
Chris Lattner | 580f9be | 2002-12-28 20:40:43 +0000 | [diff] [blame] | 146 | /// getStackSpaceFor - This returns the frame index of the specified virtual |
Chris Lattner | b8822ad | 2003-08-04 23:36:39 +0000 | [diff] [blame] | 147 | /// register on the stack, allocating space if necessary. |
Chris Lattner | 580f9be | 2002-12-28 20:40:43 +0000 | [diff] [blame] | 148 | int getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC); |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 149 | |
Chris Lattner | b8822ad | 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 | 82bee0f | 2002-12-18 08:14:26 +0000 | [diff] [blame] | 153 | void removePhysReg(unsigned PhysReg); |
Chris Lattner | b74e83c | 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 | 688c825 | 2004-02-22 19:08:15 +0000 | [diff] [blame] | 159 | void spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 160 | unsigned VirtReg, unsigned PhysReg); |
| 161 | |
Chris Lattner | c21be92 | 2002-12-16 17:44:42 +0000 | [diff] [blame] | 162 | /// spillPhysReg - This method spills the specified physical register into |
Chris Lattner | 128c2aa | 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 | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 166 | /// |
Chris Lattner | 42e0a8f | 2004-02-17 03:57:19 +0000 | [diff] [blame] | 167 | void spillPhysReg(MachineBasicBlock &MBB, MachineInstr *I, |
Chris Lattner | 128c2aa | 2003-08-17 18:01:15 +0000 | [diff] [blame] | 168 | unsigned PhysReg, bool OnlyVirtRegs = false); |
Chris Lattner | c21be92 | 2002-12-16 17:44:42 +0000 | [diff] [blame] | 169 | |
Chris Lattner | 91a452b | 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 | 4de473b | 2004-02-13 18:20:47 +0000 | [diff] [blame] | 181 | unsigned PhysReg); |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 182 | |
Chris Lattner | ae64043 | 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 | 82bee0f | 2002-12-18 08:14:26 +0000 | [diff] [blame] | 187 | bool isPhysRegAvailable(unsigned PhysReg) const; |
Chris Lattner | 91a452b | 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 | 4de473b | 2004-02-13 18:20:47 +0000 | [diff] [blame] | 193 | |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 194 | /// getReg - Find a physical register to hold the specified virtual |
Chris Lattner | b74e83c | 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 | 42e0a8f | 2004-02-17 03:57:19 +0000 | [diff] [blame] | 199 | unsigned getReg(MachineBasicBlock &MBB, MachineInstr *MI, |
Alkis Evlogimenos | 4de473b | 2004-02-13 18:20:47 +0000 | [diff] [blame] | 200 | unsigned VirtReg); |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 201 | |
Chris Lattner | 42e0a8f | 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 | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 212 | /// |
Chris Lattner | 42e0a8f | 2004-02-17 03:57:19 +0000 | [diff] [blame] | 213 | MachineInstr *reloadVirtReg(MachineBasicBlock &MBB, MachineInstr *MI, |
| 214 | unsigned OpNum); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 215 | |
Chris Lattner | b8822ad | 2003-08-04 23:36:39 +0000 | [diff] [blame] | 216 | |
| 217 | void reloadPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I, |
| 218 | unsigned PhysReg); |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 219 | }; |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Chris Lattner | b8822ad | 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 | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 227 | |
Chris Lattner | 580f9be | 2002-12-28 20:40:43 +0000 | [diff] [blame] | 228 | if (I != StackSlotForVirtReg.end() && I->first == VirtReg) |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 229 | return I->second; // Already has space allocated? |
| 230 | |
Chris Lattner | 580f9be | 2002-12-28 20:40:43 +0000 | [diff] [blame] | 231 | // Allocate a new stack object for this spill location... |
Chris Lattner | 26eb14b | 2004-08-15 22:02:22 +0000 | [diff] [blame] | 232 | int FrameIdx = MF->getFrameInfo()->CreateStackObject(RC->getSize(), |
| 233 | RC->getAlignment()); |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 234 | |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 235 | // Assign the slot... |
Chris Lattner | 580f9be | 2002-12-28 20:40:43 +0000 | [diff] [blame] | 236 | StackSlotForVirtReg.insert(I, std::make_pair(VirtReg, FrameIdx)); |
| 237 | return FrameIdx; |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Chris Lattner | ae64043 | 2002-12-17 02:50:10 +0000 | [diff] [blame] | 240 | |
Alkis Evlogimenos | 4de473b | 2004-02-13 18:20:47 +0000 | [diff] [blame] | 241 | /// removePhysReg - This method marks the specified physical register as no |
Chris Lattner | 82bee0f | 2002-12-18 08:14:26 +0000 | [diff] [blame] | 242 | /// longer being in use. |
| 243 | /// |
| 244 | void RA::removePhysReg(unsigned PhysReg) { |
Chris Lattner | 64667b6 | 2004-02-09 01:26:13 +0000 | [diff] [blame] | 245 | PhysRegsUsed[PhysReg] = -1; // PhyReg no longer used |
Chris Lattner | 82bee0f | 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 | 19b6486 | 2004-01-13 06:24:30 +0000 | [diff] [blame] | 249 | if (It != PhysRegsUseOrder.end()) |
| 250 | PhysRegsUseOrder.erase(It); |
Chris Lattner | 82bee0f | 2002-12-18 08:14:26 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 253 | |
Chris Lattner | b74e83c | 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 | 688c825 | 2004-02-22 19:08:15 +0000 | [diff] [blame] | 258 | void RA::spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator I, |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 259 | unsigned VirtReg, unsigned PhysReg) { |
Chris Lattner | 8c81945 | 2003-08-05 04:13:58 +0000 | [diff] [blame] | 260 | assert(VirtReg && "Spilling a physical register is illegal!" |
Chris Lattner | d9ac6a7 | 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 | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 267 | |
Chris Lattner | d9ac6a7 | 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 | bf9716b | 2005-09-30 01:29:00 +0000 | [diff] [blame] | 275 | RegInfo->storeRegToStackSlot(MBB, I, PhysReg, FrameIndex, RC); |
Alkis Evlogimenos | 2acef2d | 2004-02-19 06:19:09 +0000 | [diff] [blame] | 276 | ++NumStores; // Update statistics |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 277 | } |
Chris Lattner | ecea563 | 2004-02-09 02:12:04 +0000 | [diff] [blame] | 278 | |
| 279 | getVirt2PhysRegMapSlot(VirtReg) = 0; // VirtReg no longer available |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 280 | |
Chris Lattner | b8822ad | 2003-08-04 23:36:39 +0000 | [diff] [blame] | 281 | DEBUG(std::cerr << "\n"); |
Chris Lattner | 82bee0f | 2002-12-18 08:14:26 +0000 | [diff] [blame] | 282 | removePhysReg(PhysReg); |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Chris Lattner | ae64043 | 2002-12-17 02:50:10 +0000 | [diff] [blame] | 285 | |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 286 | /// spillPhysReg - This method spills the specified physical register into the |
Chris Lattner | 128c2aa | 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 | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 290 | /// |
Chris Lattner | 42e0a8f | 2004-02-17 03:57:19 +0000 | [diff] [blame] | 291 | void RA::spillPhysReg(MachineBasicBlock &MBB, MachineInstr *I, |
Chris Lattner | 128c2aa | 2003-08-17 18:01:15 +0000 | [diff] [blame] | 292 | unsigned PhysReg, bool OnlyVirtRegs) { |
Chris Lattner | 64667b6 | 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 | 73ff512 | 2003-10-08 05:20:08 +0000 | [diff] [blame] | 296 | } else { |
Chris Lattner | 91a452b | 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 | 73ff512 | 2003-10-08 05:20:08 +0000 | [diff] [blame] | 299 | for (const unsigned *AliasSet = RegInfo->getAliasSet(PhysReg); |
Chris Lattner | 64667b6 | 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 | 91a452b | 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 | 64667b6 | 2004-02-09 01:26:13 +0000 | [diff] [blame] | 313 | assert(PhysRegsUsed[PhysReg] == -1 && "Phys reg already assigned!"); |
Chris Lattner | 91a452b | 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 | 4de473b | 2004-02-13 18:20:47 +0000 | [diff] [blame] | 317 | getVirt2PhysRegMapSlot(VirtReg) = PhysReg; |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 318 | PhysRegsUseOrder.push_back(PhysReg); // New use of PhysReg |
| 319 | } |
| 320 | |
| 321 | |
Chris Lattner | ae64043 | 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 | 64667b6 | 2004-02-09 01:26:13 +0000 | [diff] [blame] | 327 | if (PhysRegsUsed[PhysReg] != -1) return false; |
Chris Lattner | ae64043 | 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 | 73ff512 | 2003-10-08 05:20:08 +0000 | [diff] [blame] | 331 | for (const unsigned *AliasSet = RegInfo->getAliasSet(PhysReg); |
| 332 | *AliasSet; ++AliasSet) |
Chris Lattner | 64667b6 | 2004-02-09 01:26:13 +0000 | [diff] [blame] | 333 | if (PhysRegsUsed[*AliasSet] != -1) // Aliased register in use? |
Alkis Evlogimenos | 73ff512 | 2003-10-08 05:20:08 +0000 | [diff] [blame] | 334 | return false; // Can't use this reg then. |
Chris Lattner | ae64043 | 2002-12-17 02:50:10 +0000 | [diff] [blame] | 335 | return true; |
| 336 | } |
| 337 | |
| 338 | |
Chris Lattner | 91a452b | 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 | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 341 | /// |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 342 | unsigned RA::getFreeReg(const TargetRegisterClass *RC) { |
Chris Lattner | 580f9be | 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 | ae64043 | 2002-12-17 02:50:10 +0000 | [diff] [blame] | 347 | |
Chris Lattner | 91a452b | 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 | 4de473b | 2004-02-13 18:20:47 +0000 | [diff] [blame] | 362 | unsigned PhysReg) { |
Chris Lattner | 91a452b | 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 | 42e0a8f | 2004-02-17 03:57:19 +0000 | [diff] [blame] | 371 | unsigned RA::getReg(MachineBasicBlock &MBB, MachineInstr *I, |
Alkis Evlogimenos | 4de473b | 2004-02-13 18:20:47 +0000 | [diff] [blame] | 372 | unsigned VirtReg) { |
Chris Lattner | 91a452b | 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 | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 377 | |
Chris Lattner | ae64043 | 2002-12-17 02:50:10 +0000 | [diff] [blame] | 378 | // If we didn't find an unused register, scavenge one now! |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 379 | if (PhysReg == 0) { |
Chris Lattner | c21be92 | 2002-12-16 17:44:42 +0000 | [diff] [blame] | 380 | assert(!PhysRegsUseOrder.empty() && "No allocated registers??"); |
Chris Lattner | ae64043 | 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 | b74e83c | 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 | 4de473b | 2004-02-13 18:20:47 +0000 | [diff] [blame] | 388 | |
Chris Lattner | ae64043 | 2002-12-17 02:50:10 +0000 | [diff] [blame] | 389 | unsigned R = PhysRegsUseOrder[i]; |
Chris Lattner | 41822c7 | 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 | 64667b6 | 2004-02-09 01:26:13 +0000 | [diff] [blame] | 394 | assert(PhysRegsUsed[R] != -1 && |
Chris Lattner | 41822c7 | 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 | 3bba026 | 2004-08-15 22:23:09 +0000 | [diff] [blame] | 398 | if (RC->contains(R)) { |
Chris Lattner | 41822c7 | 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 | 73ff512 | 2003-10-08 05:20:08 +0000 | [diff] [blame] | 404 | for (const unsigned *AliasSet = RegInfo->getAliasSet(R); |
| 405 | *AliasSet; ++AliasSet) { |
Chris Lattner | 3bba026 | 2004-08-15 22:23:09 +0000 | [diff] [blame] | 406 | if (RC->contains(*AliasSet)) { |
Alkis Evlogimenos | 73ff512 | 2003-10-08 05:20:08 +0000 | [diff] [blame] | 407 | PhysReg = *AliasSet; // Take an aliased register |
| 408 | break; |
| 409 | } |
| 410 | } |
Chris Lattner | 41822c7 | 2003-08-23 23:49:42 +0000 | [diff] [blame] | 411 | } |
Chris Lattner | ae64043 | 2002-12-17 02:50:10 +0000 | [diff] [blame] | 412 | } |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 413 | } |
| 414 | |
Chris Lattner | ae64043 | 2002-12-17 02:50:10 +0000 | [diff] [blame] | 415 | assert(PhysReg && "Physical register not assigned!?!?"); |
| 416 | |
Chris Lattner | b74e83c | 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 | c21be92 | 2002-12-16 17:44:42 +0000 | [diff] [blame] | 419 | spillPhysReg(MBB, I, PhysReg); |
Chris Lattner | b74e83c | 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 | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 423 | assignVirtToPhysReg(VirtReg, PhysReg); |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 424 | return PhysReg; |
| 425 | } |
| 426 | |
Chris Lattner | ae64043 | 2002-12-17 02:50:10 +0000 | [diff] [blame] | 427 | |
Chris Lattner | 42e0a8f | 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 | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 438 | /// |
Chris Lattner | 42e0a8f | 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 | 4de473b | 2004-02-13 18:20:47 +0000 | [diff] [blame] | 445 | if (unsigned PR = getVirt2PhysRegMapSlot(VirtReg)) { |
Chris Lattner | 42e0a8f | 2004-02-17 03:57:19 +0000 | [diff] [blame] | 446 | MarkPhysRegRecentlyUsed(PR); // Already have this value available! |
Chris Lattner | e53f4a0 | 2006-05-04 17:52:23 +0000 | [diff] [blame] | 447 | MI->getOperand(OpNum).setReg(PR); // Assign the input register |
Chris Lattner | 42e0a8f | 2004-02-17 03:57:19 +0000 | [diff] [blame] | 448 | return MI; |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 449 | } |
| 450 | |
Chris Lattner | 1e3812c | 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 | ff863ba | 2002-12-25 05:05:46 +0000 | [diff] [blame] | 453 | const TargetRegisterClass *RC = MF->getSSARegMap()->getRegClass(VirtReg); |
Chris Lattner | 1e3812c | 2004-02-17 04:08:37 +0000 | [diff] [blame] | 454 | unsigned PhysReg = getFreeReg(RC); |
Chris Lattner | 11390e7 | 2004-02-17 08:09:40 +0000 | [diff] [blame] | 455 | int FrameIndex = getStackSpaceFor(VirtReg, RC); |
Chris Lattner | 1e3812c | 2004-02-17 04:08:37 +0000 | [diff] [blame] | 456 | |
Chris Lattner | 11390e7 | 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 | 39354c9 | 2004-03-14 07:19:51 +0000 | [diff] [blame] | 461 | if (MachineInstr* FMI = RegInfo->foldMemoryOperand(MI, OpNum, FrameIndex)){ |
Alkis Evlogimenos | d6f6d1a | 2004-02-21 18:07:33 +0000 | [diff] [blame] | 462 | ++NumFolded; |
Chris Lattner | d368c61 | 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 | 39354c9 | 2004-03-14 07:19:51 +0000 | [diff] [blame] | 465 | LV->instructionChanged(MI, FMI); |
| 466 | return MBB.insert(MBB.erase(MI), FMI); |
Chris Lattner | 1e3812c | 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 | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 475 | markVirtRegModified(VirtReg, false); // Note that this reg was just reloaded |
| 476 | |
Chris Lattner | b8822ad | 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 | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 480 | // Add move instruction(s) |
Chris Lattner | bf9716b | 2005-09-30 01:29:00 +0000 | [diff] [blame] | 481 | RegInfo->loadRegFromStackSlot(MBB, MI, PhysReg, FrameIndex, RC); |
Alkis Evlogimenos | 2acef2d | 2004-02-19 06:19:09 +0000 | [diff] [blame] | 482 | ++NumLoads; // Update statistics |
Chris Lattner | 42e0a8f | 2004-02-17 03:57:19 +0000 | [diff] [blame] | 483 | |
Chris Lattner | 0648b16 | 2005-01-23 22:51:56 +0000 | [diff] [blame] | 484 | PhysRegsEverUsed[PhysReg] = true; |
Chris Lattner | e53f4a0 | 2006-05-04 17:52:23 +0000 | [diff] [blame] | 485 | MI->getOperand(OpNum).setReg(PhysReg); // Assign the input register |
Chris Lattner | 42e0a8f | 2004-02-17 03:57:19 +0000 | [diff] [blame] | 486 | return MI; |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 487 | } |
| 488 | |
Chris Lattner | b8822ad | 2003-08-04 23:36:39 +0000 | [diff] [blame] | 489 | |
| 490 | |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 491 | void RA::AllocateBasicBlock(MachineBasicBlock &MBB) { |
| 492 | // loop over each instruction |
Chris Lattner | e6a88ac | 2005-11-09 18:22:42 +0000 | [diff] [blame] | 493 | MachineBasicBlock::iterator MII = MBB.begin(); |
| 494 | const TargetInstrInfo &TII = *TM->getInstrInfo(); |
Chris Lattner | 44500e3 | 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 | e6a88ac | 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 | b8822ad | 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 | 64667b6 | 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 | b8822ad | 2003-08-04 23:36:39 +0000 | [diff] [blame] | 524 | std::cerr << "\n"); |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 525 | |
Chris Lattner | ae64043 | 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. |
Alkis Evlogimenos | 73ff512 | 2003-10-08 05:20:08 +0000 | [diff] [blame] | 528 | for (const unsigned *ImplicitUses = TID.ImplicitUses; |
| 529 | *ImplicitUses; ++ImplicitUses) |
Chris Lattner | ecea563 | 2004-02-09 02:12:04 +0000 | [diff] [blame] | 530 | MarkPhysRegRecentlyUsed(*ImplicitUses); |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 531 | |
Brian Gaeke | 53b99a0 | 2003-08-15 21:19:25 +0000 | [diff] [blame] | 532 | // Get the used operands into registers. This has the potential to spill |
Chris Lattner | b8822ad | 2003-08-04 23:36:39 +0000 | [diff] [blame] | 533 | // incoming values if we are out of registers. Note that we completely |
| 534 | // ignore physical register uses here. We assume that if an explicit |
| 535 | // physical register is referenced by the instruction, that it is guaranteed |
| 536 | // to be live-in, or the input is badly hosed. |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 537 | // |
Alkis Evlogimenos | 71e353e | 2004-02-26 22:00:20 +0000 | [diff] [blame] | 538 | for (unsigned i = 0; i != MI->getNumOperands(); ++i) { |
| 539 | MachineOperand& MO = MI->getOperand(i); |
| 540 | // here we are looking for only used operands (never def&use) |
| 541 | if (!MO.isDef() && MO.isRegister() && MO.getReg() && |
| 542 | MRegisterInfo::isVirtualRegister(MO.getReg())) |
Chris Lattner | 42e0a8f | 2004-02-17 03:57:19 +0000 | [diff] [blame] | 543 | MI = reloadVirtReg(MBB, MI, i); |
Alkis Evlogimenos | 71e353e | 2004-02-26 22:00:20 +0000 | [diff] [blame] | 544 | } |
Alkis Evlogimenos | 4de473b | 2004-02-13 18:20:47 +0000 | [diff] [blame] | 545 | |
Chris Lattner | 56ddada | 2004-02-17 17:49:10 +0000 | [diff] [blame] | 546 | // If this instruction is the last user of anything in registers, kill the |
| 547 | // value, freeing the register being used, so it doesn't need to be |
| 548 | // spilled to memory. |
| 549 | // |
| 550 | for (LiveVariables::killed_iterator KI = LV->killed_begin(MI), |
| 551 | KE = LV->killed_end(MI); KI != KE; ++KI) { |
Chris Lattner | 44b94c2 | 2005-08-23 23:42:17 +0000 | [diff] [blame] | 552 | unsigned VirtReg = *KI; |
Chris Lattner | 56ddada | 2004-02-17 17:49:10 +0000 | [diff] [blame] | 553 | unsigned PhysReg = VirtReg; |
| 554 | if (MRegisterInfo::isVirtualRegister(VirtReg)) { |
| 555 | // If the virtual register was never materialized into a register, it |
| 556 | // might not be in the map, but it won't hurt to zero it out anyway. |
| 557 | unsigned &PhysRegSlot = getVirt2PhysRegMapSlot(VirtReg); |
| 558 | PhysReg = PhysRegSlot; |
| 559 | PhysRegSlot = 0; |
| 560 | } |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 561 | |
Chris Lattner | 56ddada | 2004-02-17 17:49:10 +0000 | [diff] [blame] | 562 | if (PhysReg) { |
| 563 | DEBUG(std::cerr << " Last use of " << RegInfo->getName(PhysReg) |
| 564 | << "[%reg" << VirtReg <<"], removing it from live set\n"); |
| 565 | removePhysReg(PhysReg); |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 566 | } |
| 567 | } |
| 568 | |
| 569 | // Loop over all of the operands of the instruction, spilling registers that |
| 570 | // are defined, and marking explicit destinations in the PhysRegsUsed map. |
Alkis Evlogimenos | 71e353e | 2004-02-26 22:00:20 +0000 | [diff] [blame] | 571 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 572 | MachineOperand& MO = MI->getOperand(i); |
| 573 | if (MO.isDef() && MO.isRegister() && MO.getReg() && |
| 574 | MRegisterInfo::isPhysicalRegister(MO.getReg())) { |
| 575 | unsigned Reg = MO.getReg(); |
Chris Lattner | 0648b16 | 2005-01-23 22:51:56 +0000 | [diff] [blame] | 576 | PhysRegsEverUsed[Reg] = true; |
Alkis Evlogimenos | c0b9dc5 | 2004-02-12 02:27:10 +0000 | [diff] [blame] | 577 | spillPhysReg(MBB, MI, Reg, true); // Spill any existing value in the reg |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 578 | PhysRegsUsed[Reg] = 0; // It is free and reserved now |
| 579 | PhysRegsUseOrder.push_back(Reg); |
Alkis Evlogimenos | 19b6486 | 2004-01-13 06:24:30 +0000 | [diff] [blame] | 580 | for (const unsigned *AliasSet = RegInfo->getAliasSet(Reg); |
| 581 | *AliasSet; ++AliasSet) { |
Chris Lattner | ecea563 | 2004-02-09 02:12:04 +0000 | [diff] [blame] | 582 | PhysRegsUseOrder.push_back(*AliasSet); |
| 583 | PhysRegsUsed[*AliasSet] = 0; // It is free and reserved now |
Chris Lattner | 0648b16 | 2005-01-23 22:51:56 +0000 | [diff] [blame] | 584 | PhysRegsEverUsed[*AliasSet] = true; |
Alkis Evlogimenos | 19b6486 | 2004-01-13 06:24:30 +0000 | [diff] [blame] | 585 | } |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 586 | } |
Alkis Evlogimenos | 71e353e | 2004-02-26 22:00:20 +0000 | [diff] [blame] | 587 | } |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 588 | |
| 589 | // Loop over the implicit defs, spilling them as well. |
Alkis Evlogimenos | efe995a | 2003-12-13 01:20:58 +0000 | [diff] [blame] | 590 | for (const unsigned *ImplicitDefs = TID.ImplicitDefs; |
| 591 | *ImplicitDefs; ++ImplicitDefs) { |
| 592 | unsigned Reg = *ImplicitDefs; |
Chris Lattner | 11390e7 | 2004-02-17 08:09:40 +0000 | [diff] [blame] | 593 | spillPhysReg(MBB, MI, Reg, true); |
Alkis Evlogimenos | efe995a | 2003-12-13 01:20:58 +0000 | [diff] [blame] | 594 | PhysRegsUseOrder.push_back(Reg); |
| 595 | PhysRegsUsed[Reg] = 0; // It is free and reserved now |
Chris Lattner | 0648b16 | 2005-01-23 22:51:56 +0000 | [diff] [blame] | 596 | PhysRegsEverUsed[Reg] = true; |
| 597 | |
Alkis Evlogimenos | 19b6486 | 2004-01-13 06:24:30 +0000 | [diff] [blame] | 598 | for (const unsigned *AliasSet = RegInfo->getAliasSet(Reg); |
| 599 | *AliasSet; ++AliasSet) { |
Chris Lattner | ecea563 | 2004-02-09 02:12:04 +0000 | [diff] [blame] | 600 | PhysRegsUseOrder.push_back(*AliasSet); |
| 601 | PhysRegsUsed[*AliasSet] = 0; // It is free and reserved now |
Chris Lattner | 0648b16 | 2005-01-23 22:51:56 +0000 | [diff] [blame] | 602 | PhysRegsEverUsed[*AliasSet] = true; |
Alkis Evlogimenos | 19b6486 | 2004-01-13 06:24:30 +0000 | [diff] [blame] | 603 | } |
Alkis Evlogimenos | efe995a | 2003-12-13 01:20:58 +0000 | [diff] [blame] | 604 | } |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 605 | |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 606 | // Okay, we have allocated all of the source operands and spilled any values |
| 607 | // that would be destroyed by defs of this instruction. Loop over the |
Chris Lattner | 0648b16 | 2005-01-23 22:51:56 +0000 | [diff] [blame] | 608 | // explicit defs and assign them to a register, spilling incoming values if |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 609 | // we need to scavenge a register. |
Chris Lattner | 82bee0f | 2002-12-18 08:14:26 +0000 | [diff] [blame] | 610 | // |
Alkis Evlogimenos | 71e353e | 2004-02-26 22:00:20 +0000 | [diff] [blame] | 611 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 612 | MachineOperand& MO = MI->getOperand(i); |
| 613 | if (MO.isDef() && MO.isRegister() && MO.getReg() && |
| 614 | MRegisterInfo::isVirtualRegister(MO.getReg())) { |
| 615 | unsigned DestVirtReg = MO.getReg(); |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 616 | unsigned DestPhysReg; |
| 617 | |
Alkis Evlogimenos | 9af9dbd | 2003-12-18 13:08:52 +0000 | [diff] [blame] | 618 | // If DestVirtReg already has a value, use it. |
Alkis Evlogimenos | 4de473b | 2004-02-13 18:20:47 +0000 | [diff] [blame] | 619 | if (!(DestPhysReg = getVirt2PhysRegMapSlot(DestVirtReg))) |
Alkis Evlogimenos | c0b9dc5 | 2004-02-12 02:27:10 +0000 | [diff] [blame] | 620 | DestPhysReg = getReg(MBB, MI, DestVirtReg); |
Chris Lattner | 0648b16 | 2005-01-23 22:51:56 +0000 | [diff] [blame] | 621 | PhysRegsEverUsed[DestPhysReg] = true; |
Chris Lattner | d572563 | 2003-05-12 03:54:14 +0000 | [diff] [blame] | 622 | markVirtRegModified(DestVirtReg); |
Chris Lattner | e53f4a0 | 2006-05-04 17:52:23 +0000 | [diff] [blame] | 623 | MI->getOperand(i).setReg(DestPhysReg); // Assign the output register |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 624 | } |
Alkis Evlogimenos | 71e353e | 2004-02-26 22:00:20 +0000 | [diff] [blame] | 625 | } |
Chris Lattner | 82bee0f | 2002-12-18 08:14:26 +0000 | [diff] [blame] | 626 | |
Chris Lattner | 56ddada | 2004-02-17 17:49:10 +0000 | [diff] [blame] | 627 | // If this instruction defines any registers that are immediately dead, |
| 628 | // kill them now. |
| 629 | // |
| 630 | for (LiveVariables::killed_iterator KI = LV->dead_begin(MI), |
| 631 | KE = LV->dead_end(MI); KI != KE; ++KI) { |
Chris Lattner | 44b94c2 | 2005-08-23 23:42:17 +0000 | [diff] [blame] | 632 | unsigned VirtReg = *KI; |
Chris Lattner | 56ddada | 2004-02-17 17:49:10 +0000 | [diff] [blame] | 633 | unsigned PhysReg = VirtReg; |
| 634 | if (MRegisterInfo::isVirtualRegister(VirtReg)) { |
| 635 | unsigned &PhysRegSlot = getVirt2PhysRegMapSlot(VirtReg); |
| 636 | PhysReg = PhysRegSlot; |
| 637 | assert(PhysReg != 0); |
| 638 | PhysRegSlot = 0; |
| 639 | } |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 640 | |
Chris Lattner | 56ddada | 2004-02-17 17:49:10 +0000 | [diff] [blame] | 641 | if (PhysReg) { |
| 642 | DEBUG(std::cerr << " Register " << RegInfo->getName(PhysReg) |
| 643 | << " [%reg" << VirtReg |
| 644 | << "] is never used, removing it frame live list\n"); |
| 645 | removePhysReg(PhysReg); |
Chris Lattner | 82bee0f | 2002-12-18 08:14:26 +0000 | [diff] [blame] | 646 | } |
| 647 | } |
Chris Lattner | e6a88ac | 2005-11-09 18:22:42 +0000 | [diff] [blame] | 648 | |
| 649 | // Finally, if this is a noop copy instruction, zap it. |
| 650 | unsigned SrcReg, DstReg; |
| 651 | if (TII.isMoveInstr(*MI, SrcReg, DstReg) && SrcReg == DstReg) |
| 652 | MBB.erase(MI); |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 653 | } |
| 654 | |
Chris Lattner | e6a88ac | 2005-11-09 18:22:42 +0000 | [diff] [blame] | 655 | MachineBasicBlock::iterator MI = MBB.getFirstTerminator(); |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 656 | |
| 657 | // Spill all physical registers holding virtual registers now. |
Chris Lattner | 64667b6 | 2004-02-09 01:26:13 +0000 | [diff] [blame] | 658 | for (unsigned i = 0, e = RegInfo->getNumRegs(); i != e; ++i) |
| 659 | if (PhysRegsUsed[i] != -1) |
| 660 | if (unsigned VirtReg = PhysRegsUsed[i]) |
Alkis Evlogimenos | c0b9dc5 | 2004-02-12 02:27:10 +0000 | [diff] [blame] | 661 | spillVirtReg(MBB, MI, VirtReg, i); |
Chris Lattner | 64667b6 | 2004-02-09 01:26:13 +0000 | [diff] [blame] | 662 | else |
| 663 | removePhysReg(i); |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 664 | |
Chris Lattner | 9a5ef20 | 2005-11-09 05:28:45 +0000 | [diff] [blame] | 665 | #if 0 |
| 666 | // This checking code is very expensive. |
Chris Lattner | ecea563 | 2004-02-09 02:12:04 +0000 | [diff] [blame] | 667 | bool AllOk = true; |
Alkis Evlogimenos | 4d0d864 | 2004-02-25 21:55:45 +0000 | [diff] [blame] | 668 | for (unsigned i = MRegisterInfo::FirstVirtualRegister, |
| 669 | e = MF->getSSARegMap()->getLastVirtReg(); i <= e; ++i) |
Chris Lattner | ecea563 | 2004-02-09 02:12:04 +0000 | [diff] [blame] | 670 | if (unsigned PR = Virt2PhysRegMap[i]) { |
| 671 | std::cerr << "Register still mapped: " << i << " -> " << PR << "\n"; |
| 672 | AllOk = false; |
| 673 | } |
| 674 | assert(AllOk && "Virtual registers still in phys regs?"); |
| 675 | #endif |
Alkis Evlogimenos | 4de473b | 2004-02-13 18:20:47 +0000 | [diff] [blame] | 676 | |
Chris Lattner | 128c2aa | 2003-08-17 18:01:15 +0000 | [diff] [blame] | 677 | // Clear any physical register which appear live at the end of the basic |
| 678 | // block, but which do not hold any virtual registers. e.g., the stack |
| 679 | // pointer. |
| 680 | PhysRegsUseOrder.clear(); |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 681 | } |
| 682 | |
Chris Lattner | 86c69a6 | 2002-12-17 03:16:10 +0000 | [diff] [blame] | 683 | |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 684 | /// runOnMachineFunction - Register allocate the whole function |
| 685 | /// |
| 686 | bool RA::runOnMachineFunction(MachineFunction &Fn) { |
| 687 | DEBUG(std::cerr << "Machine Function " << "\n"); |
| 688 | MF = &Fn; |
Chris Lattner | 580f9be | 2002-12-28 20:40:43 +0000 | [diff] [blame] | 689 | TM = &Fn.getTarget(); |
| 690 | RegInfo = TM->getRegisterInfo(); |
Chris Lattner | 56ddada | 2004-02-17 17:49:10 +0000 | [diff] [blame] | 691 | LV = &getAnalysis<LiveVariables>(); |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 692 | |
Chris Lattner | 0648b16 | 2005-01-23 22:51:56 +0000 | [diff] [blame] | 693 | PhysRegsEverUsed = new bool[RegInfo->getNumRegs()]; |
| 694 | std::fill(PhysRegsEverUsed, PhysRegsEverUsed+RegInfo->getNumRegs(), false); |
| 695 | Fn.setUsedPhysRegs(PhysRegsEverUsed); |
| 696 | |
Alkis Evlogimenos | 4de473b | 2004-02-13 18:20:47 +0000 | [diff] [blame] | 697 | PhysRegsUsed.assign(RegInfo->getNumRegs(), -1); |
Chris Lattner | 64667b6 | 2004-02-09 01:26:13 +0000 | [diff] [blame] | 698 | |
Alkis Evlogimenos | 4de473b | 2004-02-13 18:20:47 +0000 | [diff] [blame] | 699 | // initialize the virtual->physical register map to have a 'null' |
| 700 | // mapping for all virtual registers |
Alkis Evlogimenos | 4d0d864 | 2004-02-25 21:55:45 +0000 | [diff] [blame] | 701 | Virt2PhysRegMap.grow(MF->getSSARegMap()->getLastVirtReg()); |
Chris Lattner | ecea563 | 2004-02-09 02:12:04 +0000 | [diff] [blame] | 702 | |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 703 | // Loop over all of the basic blocks, eliminating virtual register references |
| 704 | for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end(); |
| 705 | MBB != MBBe; ++MBB) |
| 706 | AllocateBasicBlock(*MBB); |
| 707 | |
Chris Lattner | 580f9be | 2002-12-28 20:40:43 +0000 | [diff] [blame] | 708 | StackSlotForVirtReg.clear(); |
Alkis Evlogimenos | 4de473b | 2004-02-13 18:20:47 +0000 | [diff] [blame] | 709 | PhysRegsUsed.clear(); |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 710 | VirtRegModified.clear(); |
Chris Lattner | ecea563 | 2004-02-09 02:12:04 +0000 | [diff] [blame] | 711 | Virt2PhysRegMap.clear(); |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 712 | return true; |
| 713 | } |
| 714 | |
Chris Lattner | ef09c63 | 2004-01-31 21:27:19 +0000 | [diff] [blame] | 715 | FunctionPass *llvm::createLocalRegisterAllocator() { |
Chris Lattner | 580f9be | 2002-12-28 20:40:43 +0000 | [diff] [blame] | 716 | return new RA(); |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 717 | } |