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