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