Duraid Madina | a8c7682 | 2007-06-22 08:27:12 +0000 | [diff] [blame] | 1 | //===- RegAllocBigBlock.cpp - A register allocator for large basic blocks -===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Duraid Madina | a8c7682 | 2007-06-22 08:27:12 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Duraid Madina | 837a600 | 2007-06-26 00:21:58 +0000 | [diff] [blame] | 10 | // This file implements the RABigBlock class |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Duraid Madina | a8c7682 | 2007-06-22 08:27:12 +0000 | [diff] [blame] | 14 | // This register allocator is derived from RegAllocLocal.cpp. Like it, this |
| 15 | // allocator works on one basic block at a time, oblivious to others. |
| 16 | // However, the algorithm used here is suited for long blocks of |
| 17 | // instructions - registers are spilled by greedily choosing those holding |
| 18 | // values that will not be needed for the longest amount of time. This works |
| 19 | // particularly well for blocks with 10 or more times as many instructions |
| 20 | // as machine registers, but can be used for general code. |
| 21 | // |
| 22 | //===----------------------------------------------------------------------===// |
| 23 | // |
| 24 | // TODO: - automagically invoke linearscan for (groups of) small BBs? |
| 25 | // - break ties when picking regs? (probably not worth it in a |
| 26 | // JIT context) |
| 27 | // |
| 28 | //===----------------------------------------------------------------------===// |
| 29 | |
| 30 | #define DEBUG_TYPE "regalloc" |
| 31 | #include "llvm/BasicBlock.h" |
| 32 | #include "llvm/CodeGen/Passes.h" |
| 33 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 34 | #include "llvm/CodeGen/MachineInstr.h" |
Duraid Madina | a8c7682 | 2007-06-22 08:27:12 +0000 | [diff] [blame] | 35 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 36 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Duraid Madina | a8c7682 | 2007-06-22 08:27:12 +0000 | [diff] [blame] | 37 | #include "llvm/CodeGen/LiveVariables.h" |
| 38 | #include "llvm/CodeGen/RegAllocRegistry.h" |
| 39 | #include "llvm/Target/TargetInstrInfo.h" |
| 40 | #include "llvm/Target/TargetMachine.h" |
| 41 | #include "llvm/Support/CommandLine.h" |
| 42 | #include "llvm/Support/Debug.h" |
| 43 | #include "llvm/Support/Compiler.h" |
| 44 | #include "llvm/ADT/IndexedMap.h" |
| 45 | #include "llvm/ADT/DenseMap.h" |
| 46 | #include "llvm/ADT/SmallVector.h" |
Duraid Madina | 2e0930c | 2007-06-25 23:46:54 +0000 | [diff] [blame] | 47 | #include "llvm/ADT/SmallPtrSet.h" |
Duraid Madina | a8c7682 | 2007-06-22 08:27:12 +0000 | [diff] [blame] | 48 | #include "llvm/ADT/Statistic.h" |
| 49 | #include <algorithm> |
| 50 | using namespace llvm; |
| 51 | |
| 52 | STATISTIC(NumStores, "Number of stores added"); |
| 53 | STATISTIC(NumLoads , "Number of loads added"); |
| 54 | STATISTIC(NumFolded, "Number of loads/stores folded into instructions"); |
| 55 | |
| 56 | namespace { |
| 57 | static RegisterRegAlloc |
| 58 | bigBlockRegAlloc("bigblock", " Big-block register allocator", |
| 59 | createBigBlockRegisterAllocator); |
| 60 | |
Duraid Madina | 837a600 | 2007-06-26 00:21:58 +0000 | [diff] [blame] | 61 | /// VRegKeyInfo - Defines magic values required to use VirtRegs as DenseMap |
| 62 | /// keys. |
Duraid Madina | a8c7682 | 2007-06-22 08:27:12 +0000 | [diff] [blame] | 63 | struct VRegKeyInfo { |
| 64 | static inline unsigned getEmptyKey() { return -1U; } |
| 65 | static inline unsigned getTombstoneKey() { return -2U; } |
Chris Lattner | 76c1b97 | 2007-09-17 18:34:04 +0000 | [diff] [blame] | 66 | static bool isEqual(unsigned LHS, unsigned RHS) { return LHS == RHS; } |
Duraid Madina | a8c7682 | 2007-06-22 08:27:12 +0000 | [diff] [blame] | 67 | static unsigned getHashValue(const unsigned &Key) { return Key; } |
| 68 | }; |
| 69 | |
Duraid Madina | 837a600 | 2007-06-26 00:21:58 +0000 | [diff] [blame] | 70 | |
| 71 | /// This register allocator is derived from RegAllocLocal.cpp. Like it, this |
| 72 | /// allocator works on one basic block at a time, oblivious to others. |
| 73 | /// However, the algorithm used here is suited for long blocks of |
| 74 | /// instructions - registers are spilled by greedily choosing those holding |
| 75 | /// values that will not be needed for the longest amount of time. This works |
| 76 | /// particularly well for blocks with 10 or more times as many instructions |
| 77 | /// as machine registers, but can be used for general code. |
| 78 | /// |
| 79 | /// TODO: - automagically invoke linearscan for (groups of) small BBs? |
| 80 | /// - break ties when picking regs? (probably not worth it in a |
| 81 | /// JIT context) |
| 82 | /// |
Duraid Madina | a8c7682 | 2007-06-22 08:27:12 +0000 | [diff] [blame] | 83 | class VISIBILITY_HIDDEN RABigBlock : public MachineFunctionPass { |
| 84 | public: |
| 85 | static char ID; |
| 86 | RABigBlock() : MachineFunctionPass((intptr_t)&ID) {} |
| 87 | private: |
Duraid Madina | 837a600 | 2007-06-26 00:21:58 +0000 | [diff] [blame] | 88 | /// TM - For getting at TargetMachine info |
| 89 | /// |
Duraid Madina | a8c7682 | 2007-06-22 08:27:12 +0000 | [diff] [blame] | 90 | const TargetMachine *TM; |
Duraid Madina | 837a600 | 2007-06-26 00:21:58 +0000 | [diff] [blame] | 91 | |
| 92 | /// MF - Our generic MachineFunction pointer |
| 93 | /// |
Duraid Madina | a8c7682 | 2007-06-22 08:27:12 +0000 | [diff] [blame] | 94 | MachineFunction *MF; |
Duraid Madina | 837a600 | 2007-06-26 00:21:58 +0000 | [diff] [blame] | 95 | |
| 96 | /// RegInfo - For dealing with machine register info (aliases, folds |
| 97 | /// etc) |
Duraid Madina | a8c7682 | 2007-06-22 08:27:12 +0000 | [diff] [blame] | 98 | const MRegisterInfo *RegInfo; |
Duraid Madina | 837a600 | 2007-06-26 00:21:58 +0000 | [diff] [blame] | 99 | |
| 100 | /// LV - Our generic LiveVariables pointer |
| 101 | /// |
Duraid Madina | a8c7682 | 2007-06-22 08:27:12 +0000 | [diff] [blame] | 102 | LiveVariables *LV; |
| 103 | |
Duraid Madina | 2e0930c | 2007-06-25 23:46:54 +0000 | [diff] [blame] | 104 | typedef SmallVector<unsigned, 2> VRegTimes; |
| 105 | |
Duraid Madina | 837a600 | 2007-06-26 00:21:58 +0000 | [diff] [blame] | 106 | /// VRegReadTable - maps VRegs in a BB to the set of times they are read |
| 107 | /// |
Duraid Madina | 2e0930c | 2007-06-25 23:46:54 +0000 | [diff] [blame] | 108 | DenseMap<unsigned, VRegTimes*, VRegKeyInfo> VRegReadTable; |
Duraid Madina | 837a600 | 2007-06-26 00:21:58 +0000 | [diff] [blame] | 109 | |
| 110 | /// VRegReadIdx - keeps track of the "current time" in terms of |
| 111 | /// positions in VRegReadTable |
Duraid Madina | 2e0930c | 2007-06-25 23:46:54 +0000 | [diff] [blame] | 112 | DenseMap<unsigned, unsigned , VRegKeyInfo> VRegReadIdx; |
Duraid Madina | a8c7682 | 2007-06-22 08:27:12 +0000 | [diff] [blame] | 113 | |
Duraid Madina | 837a600 | 2007-06-26 00:21:58 +0000 | [diff] [blame] | 114 | /// StackSlotForVirtReg - Maps virtual regs to the frame index where these |
| 115 | /// values are spilled. |
Duraid Madina | 2e0930c | 2007-06-25 23:46:54 +0000 | [diff] [blame] | 116 | IndexedMap<unsigned, VirtReg2IndexFunctor> StackSlotForVirtReg; |
Duraid Madina | a8c7682 | 2007-06-22 08:27:12 +0000 | [diff] [blame] | 117 | |
Duraid Madina | 837a600 | 2007-06-26 00:21:58 +0000 | [diff] [blame] | 118 | /// Virt2PhysRegMap - This map contains entries for each virtual register |
| 119 | /// that is currently available in a physical register. |
Duraid Madina | a8c7682 | 2007-06-22 08:27:12 +0000 | [diff] [blame] | 120 | IndexedMap<unsigned, VirtReg2IndexFunctor> Virt2PhysRegMap; |
| 121 | |
Duraid Madina | 837a600 | 2007-06-26 00:21:58 +0000 | [diff] [blame] | 122 | /// PhysRegsUsed - This array is effectively a map, containing entries for |
| 123 | /// each physical register that currently has a value (ie, it is in |
| 124 | /// Virt2PhysRegMap). The value mapped to is the virtual register |
| 125 | /// corresponding to the physical register (the inverse of the |
| 126 | /// Virt2PhysRegMap), or 0. The value is set to 0 if this register is pinned |
| 127 | /// because it is used by a future instruction, and to -2 if it is not |
| 128 | /// allocatable. If the entry for a physical register is -1, then the |
| 129 | /// physical register is "not in the map". |
| 130 | /// |
| 131 | std::vector<int> PhysRegsUsed; |
| 132 | |
| 133 | /// VirtRegModified - This bitset contains information about which virtual |
| 134 | /// registers need to be spilled back to memory when their registers are |
| 135 | /// scavenged. If a virtual register has simply been rematerialized, there |
| 136 | /// is no reason to spill it to memory when we need the register back. |
| 137 | /// |
| 138 | std::vector<int> VirtRegModified; |
| 139 | |
| 140 | /// MBBLastInsnTime - the number of the the last instruction in MBB |
| 141 | /// |
| 142 | int MBBLastInsnTime; |
| 143 | |
| 144 | /// MBBCurTime - the number of the the instruction being currently processed |
| 145 | /// |
| 146 | int MBBCurTime; |
| 147 | |
Duraid Madina | a8c7682 | 2007-06-22 08:27:12 +0000 | [diff] [blame] | 148 | unsigned &getVirt2PhysRegMapSlot(unsigned VirtReg) { |
| 149 | return Virt2PhysRegMap[VirtReg]; |
| 150 | } |
| 151 | |
Duraid Madina | 2e0930c | 2007-06-25 23:46:54 +0000 | [diff] [blame] | 152 | unsigned &getVirt2StackSlot(unsigned VirtReg) { |
| 153 | return StackSlotForVirtReg[VirtReg]; |
| 154 | } |
| 155 | |
Duraid Madina | 837a600 | 2007-06-26 00:21:58 +0000 | [diff] [blame] | 156 | /// markVirtRegModified - Lets us flip bits in the VirtRegModified bitset |
| 157 | /// |
Duraid Madina | a8c7682 | 2007-06-22 08:27:12 +0000 | [diff] [blame] | 158 | void markVirtRegModified(unsigned Reg, bool Val = true) { |
| 159 | assert(MRegisterInfo::isVirtualRegister(Reg) && "Illegal VirtReg!"); |
| 160 | Reg -= MRegisterInfo::FirstVirtualRegister; |
Duraid Madina | 837a600 | 2007-06-26 00:21:58 +0000 | [diff] [blame] | 161 | if (VirtRegModified.size() <= Reg) |
| 162 | VirtRegModified.resize(Reg+1); |
Duraid Madina | a8c7682 | 2007-06-22 08:27:12 +0000 | [diff] [blame] | 163 | VirtRegModified[Reg] = Val; |
| 164 | } |
| 165 | |
Duraid Madina | 837a600 | 2007-06-26 00:21:58 +0000 | [diff] [blame] | 166 | /// isVirtRegModified - Lets us query the VirtRegModified bitset |
| 167 | /// |
Duraid Madina | a8c7682 | 2007-06-22 08:27:12 +0000 | [diff] [blame] | 168 | bool isVirtRegModified(unsigned Reg) const { |
| 169 | assert(MRegisterInfo::isVirtualRegister(Reg) && "Illegal VirtReg!"); |
| 170 | assert(Reg - MRegisterInfo::FirstVirtualRegister < VirtRegModified.size() |
| 171 | && "Illegal virtual register!"); |
| 172 | return VirtRegModified[Reg - MRegisterInfo::FirstVirtualRegister]; |
| 173 | } |
| 174 | |
Duraid Madina | a8c7682 | 2007-06-22 08:27:12 +0000 | [diff] [blame] | 175 | public: |
Duraid Madina | 837a600 | 2007-06-26 00:21:58 +0000 | [diff] [blame] | 176 | /// getPassName - returns the BigBlock allocator's name |
| 177 | /// |
Duraid Madina | a8c7682 | 2007-06-22 08:27:12 +0000 | [diff] [blame] | 178 | virtual const char *getPassName() const { |
| 179 | return "BigBlock Register Allocator"; |
| 180 | } |
| 181 | |
Duraid Madina | 837a600 | 2007-06-26 00:21:58 +0000 | [diff] [blame] | 182 | /// getAnalaysisUsage - declares the required analyses |
| 183 | /// |
Duraid Madina | a8c7682 | 2007-06-22 08:27:12 +0000 | [diff] [blame] | 184 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 185 | AU.addRequired<LiveVariables>(); |
| 186 | AU.addRequiredID(PHIEliminationID); |
| 187 | AU.addRequiredID(TwoAddressInstructionPassID); |
| 188 | MachineFunctionPass::getAnalysisUsage(AU); |
| 189 | } |
| 190 | |
| 191 | private: |
| 192 | /// runOnMachineFunction - Register allocate the whole function |
Duraid Madina | 837a600 | 2007-06-26 00:21:58 +0000 | [diff] [blame] | 193 | /// |
Duraid Madina | a8c7682 | 2007-06-22 08:27:12 +0000 | [diff] [blame] | 194 | bool runOnMachineFunction(MachineFunction &Fn); |
| 195 | |
| 196 | /// AllocateBasicBlock - Register allocate the specified basic block. |
Duraid Madina | 837a600 | 2007-06-26 00:21:58 +0000 | [diff] [blame] | 197 | /// |
Duraid Madina | a8c7682 | 2007-06-22 08:27:12 +0000 | [diff] [blame] | 198 | void AllocateBasicBlock(MachineBasicBlock &MBB); |
| 199 | |
| 200 | /// FillVRegReadTable - Fill out the table of vreg read times given a BB |
Duraid Madina | 837a600 | 2007-06-26 00:21:58 +0000 | [diff] [blame] | 201 | /// |
Duraid Madina | a8c7682 | 2007-06-22 08:27:12 +0000 | [diff] [blame] | 202 | void FillVRegReadTable(MachineBasicBlock &MBB); |
| 203 | |
| 204 | /// areRegsEqual - This method returns true if the specified registers are |
| 205 | /// related to each other. To do this, it checks to see if they are equal |
| 206 | /// or if the first register is in the alias set of the second register. |
| 207 | /// |
| 208 | bool areRegsEqual(unsigned R1, unsigned R2) const { |
| 209 | if (R1 == R2) return true; |
| 210 | for (const unsigned *AliasSet = RegInfo->getAliasSet(R2); |
| 211 | *AliasSet; ++AliasSet) { |
| 212 | if (*AliasSet == R1) return true; |
| 213 | } |
| 214 | return false; |
| 215 | } |
| 216 | |
| 217 | /// getStackSpaceFor - This returns the frame index of the specified virtual |
| 218 | /// register on the stack, allocating space if necessary. |
| 219 | int getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC); |
| 220 | |
| 221 | /// removePhysReg - This method marks the specified physical register as no |
| 222 | /// longer being in use. |
| 223 | /// |
| 224 | void removePhysReg(unsigned PhysReg); |
| 225 | |
| 226 | /// spillVirtReg - This method spills the value specified by PhysReg into |
| 227 | /// the virtual register slot specified by VirtReg. It then updates the RA |
| 228 | /// data structures to indicate the fact that PhysReg is now available. |
| 229 | /// |
| 230 | void spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, |
| 231 | unsigned VirtReg, unsigned PhysReg); |
| 232 | |
| 233 | /// spillPhysReg - This method spills the specified physical register into |
| 234 | /// the virtual register slot associated with it. If OnlyVirtRegs is set to |
| 235 | /// true, then the request is ignored if the physical register does not |
| 236 | /// contain a virtual register. |
| 237 | /// |
| 238 | void spillPhysReg(MachineBasicBlock &MBB, MachineInstr *I, |
| 239 | unsigned PhysReg, bool OnlyVirtRegs = false); |
| 240 | |
| 241 | /// assignVirtToPhysReg - This method updates local state so that we know |
| 242 | /// that PhysReg is the proper container for VirtReg now. The physical |
| 243 | /// register must not be used for anything else when this is called. |
| 244 | /// |
| 245 | void assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg); |
| 246 | |
Duraid Madina | a8c7682 | 2007-06-22 08:27:12 +0000 | [diff] [blame] | 247 | /// isPhysRegAvailable - Return true if the specified physical register is |
| 248 | /// free and available for use. This also includes checking to see if |
| 249 | /// aliased registers are all free... |
| 250 | /// |
| 251 | bool isPhysRegAvailable(unsigned PhysReg) const; |
| 252 | |
| 253 | /// getFreeReg - Look to see if there is a free register available in the |
| 254 | /// specified register class. If not, return 0. |
| 255 | /// |
| 256 | unsigned getFreeReg(const TargetRegisterClass *RC); |
| 257 | |
| 258 | /// chooseReg - Pick a physical register to hold the specified |
| 259 | /// virtual register by choosing the one which will be read furthest |
| 260 | /// in the future. |
| 261 | /// |
| 262 | unsigned chooseReg(MachineBasicBlock &MBB, MachineInstr *MI, |
| 263 | unsigned VirtReg); |
| 264 | |
| 265 | /// reloadVirtReg - This method transforms the specified specified virtual |
| 266 | /// register use to refer to a physical register. This method may do this |
| 267 | /// in one of several ways: if the register is available in a physical |
| 268 | /// register already, it uses that physical register. If the value is not |
| 269 | /// in a physical register, and if there are physical registers available, |
| 270 | /// it loads it into a register. If register pressure is high, and it is |
| 271 | /// possible, it tries to fold the load of the virtual register into the |
| 272 | /// instruction itself. It avoids doing this if register pressure is low to |
| 273 | /// improve the chance that subsequent instructions can use the reloaded |
| 274 | /// value. This method returns the modified instruction. |
| 275 | /// |
| 276 | MachineInstr *reloadVirtReg(MachineBasicBlock &MBB, MachineInstr *MI, |
| 277 | unsigned OpNum); |
| 278 | |
| 279 | }; |
| 280 | char RABigBlock::ID = 0; |
| 281 | } |
| 282 | |
| 283 | /// getStackSpaceFor - This allocates space for the specified virtual register |
| 284 | /// to be held on the stack. |
| 285 | int RABigBlock::getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC) { |
| 286 | // Find the location Reg would belong... |
Duraid Madina | 2e0930c | 2007-06-25 23:46:54 +0000 | [diff] [blame] | 287 | int FrameIdx = getVirt2StackSlot(VirtReg); |
Duraid Madina | a8c7682 | 2007-06-22 08:27:12 +0000 | [diff] [blame] | 288 | |
Duraid Madina | 2e0930c | 2007-06-25 23:46:54 +0000 | [diff] [blame] | 289 | if (FrameIdx) |
| 290 | return FrameIdx - 1; // Already has space allocated? |
Duraid Madina | a8c7682 | 2007-06-22 08:27:12 +0000 | [diff] [blame] | 291 | |
| 292 | // Allocate a new stack object for this spill location... |
Duraid Madina | 2e0930c | 2007-06-25 23:46:54 +0000 | [diff] [blame] | 293 | FrameIdx = MF->getFrameInfo()->CreateStackObject(RC->getSize(), |
Duraid Madina | a8c7682 | 2007-06-22 08:27:12 +0000 | [diff] [blame] | 294 | RC->getAlignment()); |
| 295 | |
| 296 | // Assign the slot... |
Duraid Madina | 2e0930c | 2007-06-25 23:46:54 +0000 | [diff] [blame] | 297 | getVirt2StackSlot(VirtReg) = FrameIdx + 1; |
Duraid Madina | a8c7682 | 2007-06-22 08:27:12 +0000 | [diff] [blame] | 298 | return FrameIdx; |
| 299 | } |
| 300 | |
| 301 | |
| 302 | /// removePhysReg - This method marks the specified physical register as no |
| 303 | /// longer being in use. |
| 304 | /// |
| 305 | void RABigBlock::removePhysReg(unsigned PhysReg) { |
| 306 | PhysRegsUsed[PhysReg] = -1; // PhyReg no longer used |
Duraid Madina | a8c7682 | 2007-06-22 08:27:12 +0000 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | |
| 310 | /// spillVirtReg - This method spills the value specified by PhysReg into the |
| 311 | /// virtual register slot specified by VirtReg. It then updates the RA data |
| 312 | /// structures to indicate the fact that PhysReg is now available. |
| 313 | /// |
| 314 | void RABigBlock::spillVirtReg(MachineBasicBlock &MBB, |
| 315 | MachineBasicBlock::iterator I, |
| 316 | unsigned VirtReg, unsigned PhysReg) { |
| 317 | assert(VirtReg && "Spilling a physical register is illegal!" |
| 318 | " Must not have appropriate kill for the register or use exists beyond" |
| 319 | " the intended one."); |
| 320 | DOUT << " Spilling register " << RegInfo->getName(PhysReg) |
| 321 | << " containing %reg" << VirtReg; |
Owen Anderson | f6372aa | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 322 | |
| 323 | const TargetInstrInfo* TII = MBB.getParent()->getTarget().getInstrInfo(); |
| 324 | |
Duraid Madina | a8c7682 | 2007-06-22 08:27:12 +0000 | [diff] [blame] | 325 | if (!isVirtRegModified(VirtReg)) |
| 326 | DOUT << " which has not been modified, so no store necessary!"; |
| 327 | |
| 328 | // Otherwise, there is a virtual register corresponding to this physical |
| 329 | // register. We only need to spill it into its stack slot if it has been |
| 330 | // modified. |
| 331 | if (isVirtRegModified(VirtReg)) { |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 332 | const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(VirtReg); |
Duraid Madina | a8c7682 | 2007-06-22 08:27:12 +0000 | [diff] [blame] | 333 | int FrameIndex = getStackSpaceFor(VirtReg, RC); |
| 334 | DOUT << " to stack slot #" << FrameIndex; |
Owen Anderson | f6372aa | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 335 | TII->storeRegToStackSlot(MBB, I, PhysReg, true, FrameIndex, RC); |
Duraid Madina | a8c7682 | 2007-06-22 08:27:12 +0000 | [diff] [blame] | 336 | ++NumStores; // Update statistics |
| 337 | } |
| 338 | |
| 339 | getVirt2PhysRegMapSlot(VirtReg) = 0; // VirtReg no longer available |
| 340 | |
| 341 | DOUT << "\n"; |
| 342 | removePhysReg(PhysReg); |
| 343 | } |
| 344 | |
| 345 | |
| 346 | /// spillPhysReg - This method spills the specified physical register into the |
| 347 | /// virtual register slot associated with it. If OnlyVirtRegs is set to true, |
| 348 | /// then the request is ignored if the physical register does not contain a |
| 349 | /// virtual register. |
| 350 | /// |
| 351 | void RABigBlock::spillPhysReg(MachineBasicBlock &MBB, MachineInstr *I, |
| 352 | unsigned PhysReg, bool OnlyVirtRegs) { |
| 353 | if (PhysRegsUsed[PhysReg] != -1) { // Only spill it if it's used! |
| 354 | assert(PhysRegsUsed[PhysReg] != -2 && "Non allocable reg used!"); |
| 355 | if (PhysRegsUsed[PhysReg] || !OnlyVirtRegs) |
| 356 | spillVirtReg(MBB, I, PhysRegsUsed[PhysReg], PhysReg); |
| 357 | } else { |
| 358 | // If the selected register aliases any other registers, we must make |
| 359 | // sure that one of the aliases isn't alive. |
| 360 | for (const unsigned *AliasSet = RegInfo->getAliasSet(PhysReg); |
| 361 | *AliasSet; ++AliasSet) |
| 362 | if (PhysRegsUsed[*AliasSet] != -1 && // Spill aliased register. |
| 363 | PhysRegsUsed[*AliasSet] != -2) // If allocatable. |
Duraid Madina | b2efabd | 2007-06-27 08:31:07 +0000 | [diff] [blame] | 364 | if (PhysRegsUsed[*AliasSet]) |
Duraid Madina | a8c7682 | 2007-06-22 08:27:12 +0000 | [diff] [blame] | 365 | spillVirtReg(MBB, I, PhysRegsUsed[*AliasSet], *AliasSet); |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | |
| 370 | /// assignVirtToPhysReg - This method updates local state so that we know |
| 371 | /// that PhysReg is the proper container for VirtReg now. The physical |
| 372 | /// register must not be used for anything else when this is called. |
| 373 | /// |
| 374 | void RABigBlock::assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg) { |
| 375 | assert(PhysRegsUsed[PhysReg] == -1 && "Phys reg already assigned!"); |
| 376 | // Update information to note the fact that this register was just used, and |
| 377 | // it holds VirtReg. |
| 378 | PhysRegsUsed[PhysReg] = VirtReg; |
| 379 | getVirt2PhysRegMapSlot(VirtReg) = PhysReg; |
Duraid Madina | a8c7682 | 2007-06-22 08:27:12 +0000 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | |
| 383 | /// isPhysRegAvailable - Return true if the specified physical register is free |
| 384 | /// and available for use. This also includes checking to see if aliased |
| 385 | /// registers are all free... |
| 386 | /// |
| 387 | bool RABigBlock::isPhysRegAvailable(unsigned PhysReg) const { |
| 388 | if (PhysRegsUsed[PhysReg] != -1) return false; |
| 389 | |
| 390 | // If the selected register aliases any other allocated registers, it is |
| 391 | // not free! |
| 392 | for (const unsigned *AliasSet = RegInfo->getAliasSet(PhysReg); |
| 393 | *AliasSet; ++AliasSet) |
| 394 | if (PhysRegsUsed[*AliasSet] != -1) // Aliased register in use? |
| 395 | return false; // Can't use this reg then. |
| 396 | return true; |
| 397 | } |
| 398 | |
Duraid Madina | 837a600 | 2007-06-26 00:21:58 +0000 | [diff] [blame] | 399 | |
Duraid Madina | a8c7682 | 2007-06-22 08:27:12 +0000 | [diff] [blame] | 400 | /// getFreeReg - Look to see if there is a free register available in the |
| 401 | /// specified register class. If not, return 0. |
| 402 | /// |
| 403 | unsigned RABigBlock::getFreeReg(const TargetRegisterClass *RC) { |
| 404 | // Get iterators defining the range of registers that are valid to allocate in |
| 405 | // this class, which also specifies the preferred allocation order. |
| 406 | TargetRegisterClass::iterator RI = RC->allocation_order_begin(*MF); |
| 407 | TargetRegisterClass::iterator RE = RC->allocation_order_end(*MF); |
| 408 | |
| 409 | for (; RI != RE; ++RI) |
| 410 | if (isPhysRegAvailable(*RI)) { // Is reg unused? |
| 411 | assert(*RI != 0 && "Cannot use register!"); |
| 412 | return *RI; // Found an unused register! |
| 413 | } |
| 414 | return 0; |
| 415 | } |
| 416 | |
| 417 | |
Duraid Madina | a8c7682 | 2007-06-22 08:27:12 +0000 | [diff] [blame] | 418 | /// chooseReg - Pick a physical register to hold the specified |
| 419 | /// virtual register by choosing the one whose value will be read |
| 420 | /// furthest in the future. |
| 421 | /// |
| 422 | unsigned RABigBlock::chooseReg(MachineBasicBlock &MBB, MachineInstr *I, |
| 423 | unsigned VirtReg) { |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 424 | const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(VirtReg); |
Duraid Madina | a8c7682 | 2007-06-22 08:27:12 +0000 | [diff] [blame] | 425 | // First check to see if we have a free register of the requested type... |
| 426 | unsigned PhysReg = getFreeReg(RC); |
| 427 | |
| 428 | // If we didn't find an unused register, find the one which will be |
| 429 | // read at the most distant point in time. |
| 430 | if (PhysReg == 0) { |
| 431 | unsigned delay=0, longest_delay=0; |
Duraid Madina | 2e0930c | 2007-06-25 23:46:54 +0000 | [diff] [blame] | 432 | VRegTimes* ReadTimes; |
Duraid Madina | a8c7682 | 2007-06-22 08:27:12 +0000 | [diff] [blame] | 433 | |
Duraid Madina | 2e0930c | 2007-06-25 23:46:54 +0000 | [diff] [blame] | 434 | unsigned curTime = MBBCurTime; |
Duraid Madina | a8c7682 | 2007-06-22 08:27:12 +0000 | [diff] [blame] | 435 | |
| 436 | // for all physical regs in the RC, |
| 437 | for(TargetRegisterClass::iterator pReg = RC->begin(); |
| 438 | pReg != RC->end(); ++pReg) { |
| 439 | // how long until they're read? |
| 440 | if(PhysRegsUsed[*pReg]>0) { // ignore non-allocatable regs |
| 441 | ReadTimes = VRegReadTable[PhysRegsUsed[*pReg]]; |
Duraid Madina | 2e0930c | 2007-06-25 23:46:54 +0000 | [diff] [blame] | 442 | if(ReadTimes && !ReadTimes->empty()) { |
| 443 | unsigned& pt = VRegReadIdx[PhysRegsUsed[*pReg]]; |
| 444 | while(pt < ReadTimes->size() && (*ReadTimes)[pt] < curTime) { |
| 445 | ++pt; |
| 446 | } |
| 447 | |
| 448 | if(pt < ReadTimes->size()) |
| 449 | delay = (*ReadTimes)[pt] - curTime; |
| 450 | else |
| 451 | delay = MBBLastInsnTime + 1 - curTime; |
| 452 | } else { |
| 453 | // This register is only defined, but never |
| 454 | // read in this MBB. Therefore the next read |
| 455 | // happens after the end of this MBB |
| 456 | delay = MBBLastInsnTime + 1 - curTime; |
| 457 | } |
| 458 | |
Duraid Madina | a8c7682 | 2007-06-22 08:27:12 +0000 | [diff] [blame] | 459 | |
| 460 | if(delay > longest_delay) { |
| 461 | longest_delay = delay; |
| 462 | PhysReg = *pReg; |
| 463 | } |
| 464 | } |
| 465 | } |
Duraid Madina | df82c93 | 2007-06-27 09:01:14 +0000 | [diff] [blame] | 466 | |
| 467 | if(PhysReg == 0) { // ok, now we're desperate. We couldn't choose |
| 468 | // a register to spill by looking through the |
| 469 | // read timetable, so now we just spill the |
| 470 | // first allocatable register we find. |
| 471 | |
| 472 | // for all physical regs in the RC, |
| 473 | for(TargetRegisterClass::iterator pReg = RC->begin(); |
| 474 | pReg != RC->end(); ++pReg) { |
| 475 | // if we find a register we can spill |
| 476 | if(PhysRegsUsed[*pReg]>=-1) |
| 477 | PhysReg = *pReg; // choose it to be spilled |
| 478 | } |
| 479 | } |
Duraid Madina | 4e378c6 | 2007-06-27 08:11:59 +0000 | [diff] [blame] | 480 | |
Duraid Madina | df82c93 | 2007-06-27 09:01:14 +0000 | [diff] [blame] | 481 | assert(PhysReg && "couldn't choose a register to spill :( "); |
| 482 | // TODO: assert that RC->contains(PhysReg) / handle aliased registers? |
Duraid Madina | a8c7682 | 2007-06-22 08:27:12 +0000 | [diff] [blame] | 483 | |
| 484 | // since we needed to look in the table we need to spill this register. |
| 485 | spillPhysReg(MBB, I, PhysReg); |
| 486 | } |
| 487 | |
| 488 | // assign the vreg to our chosen physical register |
| 489 | assignVirtToPhysReg(VirtReg, PhysReg); |
| 490 | return PhysReg; // and return it |
| 491 | } |
| 492 | |
| 493 | |
| 494 | /// reloadVirtReg - This method transforms an instruction with a virtual |
| 495 | /// register use to one that references a physical register. It does this as |
| 496 | /// follows: |
| 497 | /// |
| 498 | /// 1) If the register is already in a physical register, it uses it. |
| 499 | /// 2) Otherwise, if there is a free physical register, it uses that. |
| 500 | /// 3) Otherwise, it calls chooseReg() to get the physical register |
| 501 | /// holding the most distantly needed value, generating a spill in |
| 502 | /// the process. |
| 503 | /// |
| 504 | /// This method returns the modified instruction. |
| 505 | MachineInstr *RABigBlock::reloadVirtReg(MachineBasicBlock &MBB, MachineInstr *MI, |
| 506 | unsigned OpNum) { |
| 507 | unsigned VirtReg = MI->getOperand(OpNum).getReg(); |
Owen Anderson | 6425f8b | 2008-01-07 01:35:56 +0000 | [diff] [blame] | 508 | const TargetInstrInfo* TII = MBB.getParent()->getTarget().getInstrInfo(); |
Duraid Madina | a8c7682 | 2007-06-22 08:27:12 +0000 | [diff] [blame] | 509 | |
| 510 | // If the virtual register is already available in a physical register, |
| 511 | // just update the instruction and return. |
| 512 | if (unsigned PR = getVirt2PhysRegMapSlot(VirtReg)) { |
| 513 | MI->getOperand(OpNum).setReg(PR); |
| 514 | return MI; |
| 515 | } |
| 516 | |
| 517 | // Otherwise, if we have free physical registers available to hold the |
| 518 | // value, use them. |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 519 | const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(VirtReg); |
Duraid Madina | a8c7682 | 2007-06-22 08:27:12 +0000 | [diff] [blame] | 520 | unsigned PhysReg = getFreeReg(RC); |
| 521 | int FrameIndex = getStackSpaceFor(VirtReg, RC); |
| 522 | |
| 523 | if (PhysReg) { // we have a free register, so use it. |
| 524 | assignVirtToPhysReg(VirtReg, PhysReg); |
| 525 | } else { // no free registers available. |
| 526 | // try to fold the spill into the instruction |
Evan Cheng | aee4af6 | 2007-12-02 08:30:39 +0000 | [diff] [blame] | 527 | SmallVector<unsigned, 2> Ops; |
| 528 | Ops.push_back(OpNum); |
Owen Anderson | 6425f8b | 2008-01-07 01:35:56 +0000 | [diff] [blame] | 529 | if(MachineInstr* FMI = TII->foldMemoryOperand(MI, Ops, FrameIndex)) { |
Duraid Madina | a8c7682 | 2007-06-22 08:27:12 +0000 | [diff] [blame] | 530 | ++NumFolded; |
| 531 | // Since we changed the address of MI, make sure to update live variables |
| 532 | // to know that the new instruction has the properties of the old one. |
| 533 | LV->instructionChanged(MI, FMI); |
| 534 | return MBB.insert(MBB.erase(MI), FMI); |
| 535 | } |
| 536 | |
| 537 | // determine which of the physical registers we'll kill off, since we |
| 538 | // couldn't fold. |
| 539 | PhysReg = chooseReg(MBB, MI, VirtReg); |
| 540 | } |
| 541 | |
| 542 | // this virtual register is now unmodified (since we just reloaded it) |
| 543 | markVirtRegModified(VirtReg, false); |
| 544 | |
| 545 | DOUT << " Reloading %reg" << VirtReg << " into " |
| 546 | << RegInfo->getName(PhysReg) << "\n"; |
| 547 | |
| 548 | // Add move instruction(s) |
Owen Anderson | f6372aa | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 549 | TII->loadRegFromStackSlot(MBB, MI, PhysReg, FrameIndex, RC); |
Duraid Madina | a8c7682 | 2007-06-22 08:27:12 +0000 | [diff] [blame] | 550 | ++NumLoads; // Update statistics |
| 551 | |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 552 | MF->getRegInfo().setPhysRegUsed(PhysReg); |
Duraid Madina | a8c7682 | 2007-06-22 08:27:12 +0000 | [diff] [blame] | 553 | MI->getOperand(OpNum).setReg(PhysReg); // Assign the input register |
| 554 | return MI; |
| 555 | } |
| 556 | |
| 557 | /// Fill out the vreg read timetable. Since ReadTime increases |
| 558 | /// monotonically, the individual readtime sets will be sorted |
| 559 | /// in ascending order. |
| 560 | void RABigBlock::FillVRegReadTable(MachineBasicBlock &MBB) { |
| 561 | // loop over each instruction |
| 562 | MachineBasicBlock::iterator MII; |
| 563 | unsigned ReadTime; |
| 564 | |
| 565 | for(ReadTime=0, MII = MBB.begin(); MII != MBB.end(); ++ReadTime, ++MII) { |
| 566 | MachineInstr *MI = MII; |
| 567 | |
Duraid Madina | a8c7682 | 2007-06-22 08:27:12 +0000 | [diff] [blame] | 568 | for (unsigned i = 0; i != MI->getNumOperands(); ++i) { |
| 569 | MachineOperand& MO = MI->getOperand(i); |
| 570 | // look for vreg reads.. |
| 571 | if (MO.isRegister() && !MO.isDef() && MO.getReg() && |
| 572 | MRegisterInfo::isVirtualRegister(MO.getReg())) { |
Duraid Madina | 2e0930c | 2007-06-25 23:46:54 +0000 | [diff] [blame] | 573 | // ..and add them to the read table. |
| 574 | VRegTimes* &Times = VRegReadTable[MO.getReg()]; |
| 575 | if(!VRegReadTable[MO.getReg()]) { |
| 576 | Times = new VRegTimes; |
| 577 | VRegReadIdx[MO.getReg()] = 0; |
| 578 | } |
| 579 | Times->push_back(ReadTime); |
Duraid Madina | a8c7682 | 2007-06-22 08:27:12 +0000 | [diff] [blame] | 580 | } |
| 581 | } |
| 582 | |
| 583 | } |
| 584 | |
Duraid Madina | 2e0930c | 2007-06-25 23:46:54 +0000 | [diff] [blame] | 585 | MBBLastInsnTime = ReadTime; |
| 586 | |
| 587 | for(DenseMap<unsigned, VRegTimes*, VRegKeyInfo>::iterator Reads = VRegReadTable.begin(); |
| 588 | Reads != VRegReadTable.end(); ++Reads) { |
| 589 | if(Reads->second) { |
| 590 | DOUT << "Reads[" << Reads->first << "]=" << Reads->second->size() << "\n"; |
| 591 | } |
| 592 | } |
Duraid Madina | a8c7682 | 2007-06-22 08:27:12 +0000 | [diff] [blame] | 593 | } |
| 594 | |
Duraid Madina | b2efabd | 2007-06-27 08:31:07 +0000 | [diff] [blame] | 595 | /// isReadModWriteImplicitKill - True if this is an implicit kill for a |
| 596 | /// read/mod/write register, i.e. update partial register. |
| 597 | static bool isReadModWriteImplicitKill(MachineInstr *MI, unsigned Reg) { |
| 598 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 599 | MachineOperand& MO = MI->getOperand(i); |
| 600 | if (MO.isRegister() && MO.getReg() == Reg && MO.isImplicit() && |
| 601 | MO.isDef() && !MO.isDead()) |
| 602 | return true; |
| 603 | } |
| 604 | return false; |
| 605 | } |
| 606 | |
| 607 | /// isReadModWriteImplicitDef - True if this is an implicit def for a |
| 608 | /// read/mod/write register, i.e. update partial register. |
| 609 | static bool isReadModWriteImplicitDef(MachineInstr *MI, unsigned Reg) { |
| 610 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 611 | MachineOperand& MO = MI->getOperand(i); |
| 612 | if (MO.isRegister() && MO.getReg() == Reg && MO.isImplicit() && |
| 613 | !MO.isDef() && MO.isKill()) |
| 614 | return true; |
| 615 | } |
| 616 | return false; |
| 617 | } |
| 618 | |
Duraid Madina | 2e0930c | 2007-06-25 23:46:54 +0000 | [diff] [blame] | 619 | |
Duraid Madina | a8c7682 | 2007-06-22 08:27:12 +0000 | [diff] [blame] | 620 | void RABigBlock::AllocateBasicBlock(MachineBasicBlock &MBB) { |
| 621 | // loop over each instruction |
| 622 | MachineBasicBlock::iterator MII = MBB.begin(); |
| 623 | const TargetInstrInfo &TII = *TM->getInstrInfo(); |
| 624 | |
| 625 | DEBUG(const BasicBlock *LBB = MBB.getBasicBlock(); |
| 626 | if (LBB) DOUT << "\nStarting RegAlloc of BB: " << LBB->getName()); |
| 627 | |
| 628 | // If this is the first basic block in the machine function, add live-in |
| 629 | // registers as active. |
| 630 | if (&MBB == &*MF->begin()) { |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 631 | for (MachineRegisterInfo::livein_iterator |
| 632 | I = MF->getRegInfo().livein_begin(), |
| 633 | E = MF->getRegInfo().livein_end(); I != E; ++I) { |
Duraid Madina | a8c7682 | 2007-06-22 08:27:12 +0000 | [diff] [blame] | 634 | unsigned Reg = I->first; |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 635 | MF->getRegInfo().setPhysRegUsed(Reg); |
Duraid Madina | a8c7682 | 2007-06-22 08:27:12 +0000 | [diff] [blame] | 636 | PhysRegsUsed[Reg] = 0; // It is free and reserved now |
Duraid Madina | b2efabd | 2007-06-27 08:31:07 +0000 | [diff] [blame] | 637 | for (const unsigned *AliasSet = RegInfo->getSubRegisters(Reg); |
Duraid Madina | a8c7682 | 2007-06-22 08:27:12 +0000 | [diff] [blame] | 638 | *AliasSet; ++AliasSet) { |
| 639 | if (PhysRegsUsed[*AliasSet] != -2) { |
Duraid Madina | a8c7682 | 2007-06-22 08:27:12 +0000 | [diff] [blame] | 640 | PhysRegsUsed[*AliasSet] = 0; // It is free and reserved now |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 641 | MF->getRegInfo().setPhysRegUsed(*AliasSet); |
Duraid Madina | a8c7682 | 2007-06-22 08:27:12 +0000 | [diff] [blame] | 642 | } |
| 643 | } |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | // Otherwise, sequentially allocate each instruction in the MBB. |
Duraid Madina | 4e378c6 | 2007-06-27 08:11:59 +0000 | [diff] [blame] | 648 | MBBCurTime = -1; |
Duraid Madina | a8c7682 | 2007-06-22 08:27:12 +0000 | [diff] [blame] | 649 | while (MII != MBB.end()) { |
| 650 | MachineInstr *MI = MII++; |
Duraid Madina | 4e378c6 | 2007-06-27 08:11:59 +0000 | [diff] [blame] | 651 | MBBCurTime++; |
Duraid Madina | a8c7682 | 2007-06-22 08:27:12 +0000 | [diff] [blame] | 652 | const TargetInstrDescriptor &TID = TII.get(MI->getOpcode()); |
Duraid Madina | 4e378c6 | 2007-06-27 08:11:59 +0000 | [diff] [blame] | 653 | DEBUG(DOUT << "\nTime=" << MBBCurTime << " Starting RegAlloc of: " << *MI; |
Duraid Madina | a8c7682 | 2007-06-22 08:27:12 +0000 | [diff] [blame] | 654 | DOUT << " Regs have values: "; |
| 655 | for (unsigned i = 0; i != RegInfo->getNumRegs(); ++i) |
| 656 | if (PhysRegsUsed[i] != -1 && PhysRegsUsed[i] != -2) |
| 657 | DOUT << "[" << RegInfo->getName(i) |
| 658 | << ",%reg" << PhysRegsUsed[i] << "] "; |
| 659 | DOUT << "\n"); |
| 660 | |
Duraid Madina | a8c7682 | 2007-06-22 08:27:12 +0000 | [diff] [blame] | 661 | SmallVector<unsigned, 8> Kills; |
| 662 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 663 | MachineOperand& MO = MI->getOperand(i); |
Duraid Madina | b2efabd | 2007-06-27 08:31:07 +0000 | [diff] [blame] | 664 | if (MO.isRegister() && MO.isKill()) { |
| 665 | if (!MO.isImplicit()) |
| 666 | Kills.push_back(MO.getReg()); |
| 667 | else if (!isReadModWriteImplicitKill(MI, MO.getReg())) |
| 668 | // These are extra physical register kills when a sub-register |
| 669 | // is defined (def of a sub-register is a read/mod/write of the |
| 670 | // larger registers). Ignore. |
| 671 | Kills.push_back(MO.getReg()); |
| 672 | } |
Duraid Madina | a8c7682 | 2007-06-22 08:27:12 +0000 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | // Get the used operands into registers. This has the potential to spill |
| 676 | // incoming values if we are out of registers. Note that we completely |
| 677 | // ignore physical register uses here. We assume that if an explicit |
| 678 | // physical register is referenced by the instruction, that it is guaranteed |
| 679 | // to be live-in, or the input is badly hosed. |
| 680 | // |
| 681 | for (unsigned i = 0; i != MI->getNumOperands(); ++i) { |
| 682 | MachineOperand& MO = MI->getOperand(i); |
| 683 | // here we are looking for only used operands (never def&use) |
| 684 | if (MO.isRegister() && !MO.isDef() && MO.getReg() && !MO.isImplicit() && |
| 685 | MRegisterInfo::isVirtualRegister(MO.getReg())) |
| 686 | MI = reloadVirtReg(MBB, MI, i); |
| 687 | } |
| 688 | |
| 689 | // If this instruction is the last user of this register, kill the |
| 690 | // value, freeing the register being used, so it doesn't need to be |
| 691 | // spilled to memory. |
| 692 | // |
| 693 | for (unsigned i = 0, e = Kills.size(); i != e; ++i) { |
| 694 | unsigned VirtReg = Kills[i]; |
| 695 | unsigned PhysReg = VirtReg; |
| 696 | if (MRegisterInfo::isVirtualRegister(VirtReg)) { |
| 697 | // If the virtual register was never materialized into a register, it |
| 698 | // might not be in the map, but it won't hurt to zero it out anyway. |
| 699 | unsigned &PhysRegSlot = getVirt2PhysRegMapSlot(VirtReg); |
| 700 | PhysReg = PhysRegSlot; |
| 701 | PhysRegSlot = 0; |
| 702 | } else if (PhysRegsUsed[PhysReg] == -2) { |
| 703 | // Unallocatable register dead, ignore. |
| 704 | continue; |
Duraid Madina | b2efabd | 2007-06-27 08:31:07 +0000 | [diff] [blame] | 705 | } else { |
| 706 | assert(!PhysRegsUsed[PhysReg] || PhysRegsUsed[PhysReg] == -1 && |
| 707 | "Silently clearing a virtual register?"); |
Duraid Madina | a8c7682 | 2007-06-22 08:27:12 +0000 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | if (PhysReg) { |
| 711 | DOUT << " Last use of " << RegInfo->getName(PhysReg) |
| 712 | << "[%reg" << VirtReg <<"], removing it from live set\n"; |
| 713 | removePhysReg(PhysReg); |
Duraid Madina | b2efabd | 2007-06-27 08:31:07 +0000 | [diff] [blame] | 714 | for (const unsigned *AliasSet = RegInfo->getSubRegisters(PhysReg); |
Duraid Madina | a8c7682 | 2007-06-22 08:27:12 +0000 | [diff] [blame] | 715 | *AliasSet; ++AliasSet) { |
| 716 | if (PhysRegsUsed[*AliasSet] != -2) { |
| 717 | DOUT << " Last use of " |
| 718 | << RegInfo->getName(*AliasSet) |
| 719 | << "[%reg" << VirtReg <<"], removing it from live set\n"; |
| 720 | removePhysReg(*AliasSet); |
| 721 | } |
| 722 | } |
| 723 | } |
| 724 | } |
| 725 | |
| 726 | // Loop over all of the operands of the instruction, spilling registers that |
| 727 | // are defined, and marking explicit destinations in the PhysRegsUsed map. |
| 728 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 729 | MachineOperand& MO = MI->getOperand(i); |
| 730 | if (MO.isRegister() && MO.isDef() && !MO.isImplicit() && MO.getReg() && |
| 731 | MRegisterInfo::isPhysicalRegister(MO.getReg())) { |
| 732 | unsigned Reg = MO.getReg(); |
| 733 | if (PhysRegsUsed[Reg] == -2) continue; // Something like ESP. |
Duraid Madina | b2efabd | 2007-06-27 08:31:07 +0000 | [diff] [blame] | 734 | // These are extra physical register defs when a sub-register |
| 735 | // is defined (def of a sub-register is a read/mod/write of the |
| 736 | // larger registers). Ignore. |
| 737 | if (isReadModWriteImplicitDef(MI, MO.getReg())) continue; |
| 738 | |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 739 | MF->getRegInfo().setPhysRegUsed(Reg); |
Duraid Madina | a8c7682 | 2007-06-22 08:27:12 +0000 | [diff] [blame] | 740 | spillPhysReg(MBB, MI, Reg, true); // Spill any existing value in reg |
| 741 | PhysRegsUsed[Reg] = 0; // It is free and reserved now |
Duraid Madina | b2efabd | 2007-06-27 08:31:07 +0000 | [diff] [blame] | 742 | for (const unsigned *AliasSet = RegInfo->getSubRegisters(Reg); |
Duraid Madina | a8c7682 | 2007-06-22 08:27:12 +0000 | [diff] [blame] | 743 | *AliasSet; ++AliasSet) { |
| 744 | if (PhysRegsUsed[*AliasSet] != -2) { |
Duraid Madina | 669f738 | 2007-06-27 07:07:13 +0000 | [diff] [blame] | 745 | PhysRegsUsed[*AliasSet] = 0; // It is free and reserved now |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 746 | MF->getRegInfo().setPhysRegUsed(*AliasSet); |
Duraid Madina | a8c7682 | 2007-06-22 08:27:12 +0000 | [diff] [blame] | 747 | } |
| 748 | } |
| 749 | } |
| 750 | } |
| 751 | |
| 752 | // Loop over the implicit defs, spilling them as well. |
| 753 | if (TID.ImplicitDefs) { |
| 754 | for (const unsigned *ImplicitDefs = TID.ImplicitDefs; |
| 755 | *ImplicitDefs; ++ImplicitDefs) { |
| 756 | unsigned Reg = *ImplicitDefs; |
Duraid Madina | b2efabd | 2007-06-27 08:31:07 +0000 | [diff] [blame] | 757 | if (PhysRegsUsed[Reg] != -2) { |
Duraid Madina | a8c7682 | 2007-06-22 08:27:12 +0000 | [diff] [blame] | 758 | spillPhysReg(MBB, MI, Reg, true); |
Duraid Madina | a8c7682 | 2007-06-22 08:27:12 +0000 | [diff] [blame] | 759 | PhysRegsUsed[Reg] = 0; // It is free and reserved now |
| 760 | } |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 761 | MF->getRegInfo().setPhysRegUsed(Reg); |
Duraid Madina | b2efabd | 2007-06-27 08:31:07 +0000 | [diff] [blame] | 762 | for (const unsigned *AliasSet = RegInfo->getSubRegisters(Reg); |
Duraid Madina | a8c7682 | 2007-06-22 08:27:12 +0000 | [diff] [blame] | 763 | *AliasSet; ++AliasSet) { |
| 764 | if (PhysRegsUsed[*AliasSet] != -2) { |
Duraid Madina | b2efabd | 2007-06-27 08:31:07 +0000 | [diff] [blame] | 765 | PhysRegsUsed[*AliasSet] = 0; // It is free and reserved now |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 766 | MF->getRegInfo().setPhysRegUsed(*AliasSet); |
Duraid Madina | a8c7682 | 2007-06-22 08:27:12 +0000 | [diff] [blame] | 767 | } |
| 768 | } |
| 769 | } |
| 770 | } |
| 771 | |
| 772 | SmallVector<unsigned, 8> DeadDefs; |
| 773 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 774 | MachineOperand& MO = MI->getOperand(i); |
| 775 | if (MO.isRegister() && MO.isDead()) |
| 776 | DeadDefs.push_back(MO.getReg()); |
| 777 | } |
| 778 | |
| 779 | // Okay, we have allocated all of the source operands and spilled any values |
| 780 | // that would be destroyed by defs of this instruction. Loop over the |
| 781 | // explicit defs and assign them to a register, spilling incoming values if |
| 782 | // we need to scavenge a register. |
| 783 | // |
| 784 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 785 | MachineOperand& MO = MI->getOperand(i); |
| 786 | if (MO.isRegister() && MO.isDef() && MO.getReg() && |
| 787 | MRegisterInfo::isVirtualRegister(MO.getReg())) { |
| 788 | unsigned DestVirtReg = MO.getReg(); |
| 789 | unsigned DestPhysReg; |
| 790 | |
| 791 | // If DestVirtReg already has a value, use it. |
| 792 | if (!(DestPhysReg = getVirt2PhysRegMapSlot(DestVirtReg))) |
| 793 | DestPhysReg = chooseReg(MBB, MI, DestVirtReg); |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 794 | MF->getRegInfo().setPhysRegUsed(DestPhysReg); |
Duraid Madina | a8c7682 | 2007-06-22 08:27:12 +0000 | [diff] [blame] | 795 | markVirtRegModified(DestVirtReg); |
| 796 | MI->getOperand(i).setReg(DestPhysReg); // Assign the output register |
| 797 | } |
| 798 | } |
| 799 | |
| 800 | // If this instruction defines any registers that are immediately dead, |
| 801 | // kill them now. |
| 802 | // |
| 803 | for (unsigned i = 0, e = DeadDefs.size(); i != e; ++i) { |
| 804 | unsigned VirtReg = DeadDefs[i]; |
| 805 | unsigned PhysReg = VirtReg; |
| 806 | if (MRegisterInfo::isVirtualRegister(VirtReg)) { |
| 807 | unsigned &PhysRegSlot = getVirt2PhysRegMapSlot(VirtReg); |
| 808 | PhysReg = PhysRegSlot; |
| 809 | assert(PhysReg != 0); |
| 810 | PhysRegSlot = 0; |
| 811 | } else if (PhysRegsUsed[PhysReg] == -2) { |
| 812 | // Unallocatable register dead, ignore. |
| 813 | continue; |
| 814 | } |
| 815 | |
| 816 | if (PhysReg) { |
| 817 | DOUT << " Register " << RegInfo->getName(PhysReg) |
| 818 | << " [%reg" << VirtReg |
| 819 | << "] is never used, removing it frame live list\n"; |
| 820 | removePhysReg(PhysReg); |
| 821 | for (const unsigned *AliasSet = RegInfo->getAliasSet(PhysReg); |
| 822 | *AliasSet; ++AliasSet) { |
| 823 | if (PhysRegsUsed[*AliasSet] != -2) { |
| 824 | DOUT << " Register " << RegInfo->getName(*AliasSet) |
| 825 | << " [%reg" << *AliasSet |
| 826 | << "] is never used, removing it frame live list\n"; |
| 827 | removePhysReg(*AliasSet); |
| 828 | } |
| 829 | } |
| 830 | } |
| 831 | } |
| 832 | |
| 833 | // Finally, if this is a noop copy instruction, zap it. |
| 834 | unsigned SrcReg, DstReg; |
| 835 | if (TII.isMoveInstr(*MI, SrcReg, DstReg) && SrcReg == DstReg) { |
| 836 | LV->removeVirtualRegistersKilled(MI); |
| 837 | LV->removeVirtualRegistersDead(MI); |
| 838 | MBB.erase(MI); |
| 839 | } |
| 840 | } |
| 841 | |
| 842 | MachineBasicBlock::iterator MI = MBB.getFirstTerminator(); |
| 843 | |
| 844 | // Spill all physical registers holding virtual registers now. |
| 845 | for (unsigned i = 0, e = RegInfo->getNumRegs(); i != e; ++i) |
| 846 | if (PhysRegsUsed[i] != -1 && PhysRegsUsed[i] != -2) |
| 847 | if (unsigned VirtReg = PhysRegsUsed[i]) |
| 848 | spillVirtReg(MBB, MI, VirtReg, i); |
| 849 | else |
| 850 | removePhysReg(i); |
Duraid Madina | a8c7682 | 2007-06-22 08:27:12 +0000 | [diff] [blame] | 851 | } |
| 852 | |
| 853 | /// runOnMachineFunction - Register allocate the whole function |
| 854 | /// |
| 855 | bool RABigBlock::runOnMachineFunction(MachineFunction &Fn) { |
| 856 | DOUT << "Machine Function " << "\n"; |
| 857 | MF = &Fn; |
| 858 | TM = &Fn.getTarget(); |
| 859 | RegInfo = TM->getRegisterInfo(); |
| 860 | LV = &getAnalysis<LiveVariables>(); |
| 861 | |
| 862 | PhysRegsUsed.assign(RegInfo->getNumRegs(), -1); |
| 863 | |
| 864 | // At various places we want to efficiently check to see whether a register |
| 865 | // is allocatable. To handle this, we mark all unallocatable registers as |
| 866 | // being pinned down, permanently. |
| 867 | { |
| 868 | BitVector Allocable = RegInfo->getAllocatableSet(Fn); |
| 869 | for (unsigned i = 0, e = Allocable.size(); i != e; ++i) |
| 870 | if (!Allocable[i]) |
| 871 | PhysRegsUsed[i] = -2; // Mark the reg unallocable. |
| 872 | } |
| 873 | |
| 874 | // initialize the virtual->physical register map to have a 'null' |
| 875 | // mapping for all virtual registers |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 876 | Virt2PhysRegMap.grow(MF->getRegInfo().getLastVirtReg()); |
| 877 | StackSlotForVirtReg.grow(MF->getRegInfo().getLastVirtReg()); |
| 878 | VirtRegModified.resize(MF->getRegInfo().getLastVirtReg() - |
| 879 | MRegisterInfo::FirstVirtualRegister + 1, 0); |
Duraid Madina | a8c7682 | 2007-06-22 08:27:12 +0000 | [diff] [blame] | 880 | |
| 881 | // Loop over all of the basic blocks, eliminating virtual register references |
| 882 | for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end(); |
| 883 | MBB != MBBe; ++MBB) { |
| 884 | // fill out the read timetable |
| 885 | FillVRegReadTable(*MBB); |
| 886 | // use it to allocate the BB |
| 887 | AllocateBasicBlock(*MBB); |
| 888 | // clear it |
| 889 | VRegReadTable.clear(); |
| 890 | } |
| 891 | |
| 892 | StackSlotForVirtReg.clear(); |
| 893 | PhysRegsUsed.clear(); |
| 894 | VirtRegModified.clear(); |
| 895 | Virt2PhysRegMap.clear(); |
| 896 | return true; |
| 897 | } |
| 898 | |
| 899 | FunctionPass *llvm::createBigBlockRegisterAllocator() { |
| 900 | return new RABigBlock(); |
| 901 | } |
Duraid Madina | 837a600 | 2007-06-26 00:21:58 +0000 | [diff] [blame] | 902 | |