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