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