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