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