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