Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 1 | //===-- RegAllocFast.cpp - A fast register allocator for debug code -------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 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 | |
| 15 | #define DEBUG_TYPE "regalloc" |
| 16 | #include "llvm/BasicBlock.h" |
| 17 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 18 | #include "llvm/CodeGen/MachineInstr.h" |
| 19 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 20 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 21 | #include "llvm/CodeGen/Passes.h" |
| 22 | #include "llvm/CodeGen/RegAllocRegistry.h" |
| 23 | #include "llvm/Target/TargetInstrInfo.h" |
| 24 | #include "llvm/Target/TargetMachine.h" |
| 25 | #include "llvm/Support/CommandLine.h" |
| 26 | #include "llvm/Support/Debug.h" |
| 27 | #include "llvm/Support/ErrorHandling.h" |
| 28 | #include "llvm/Support/raw_ostream.h" |
| 29 | #include "llvm/ADT/DenseMap.h" |
| 30 | #include "llvm/ADT/IndexedMap.h" |
| 31 | #include "llvm/ADT/SmallSet.h" |
| 32 | #include "llvm/ADT/SmallVector.h" |
| 33 | #include "llvm/ADT/Statistic.h" |
| 34 | #include "llvm/ADT/STLExtras.h" |
| 35 | #include <algorithm> |
| 36 | using namespace llvm; |
| 37 | |
Jakob Stoklund Olesen | 1b2c761 | 2010-05-14 20:28:32 +0000 | [diff] [blame] | 38 | static cl::opt<bool> VerifyFastRegalloc("verify-fast-regalloc", cl::Hidden, |
| 39 | cl::desc("Verify machine code before fast regalloc")); |
| 40 | |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 41 | STATISTIC(NumStores, "Number of stores added"); |
| 42 | STATISTIC(NumLoads , "Number of loads added"); |
Jakob Stoklund Olesen | 8a65c51 | 2010-05-14 21:55:50 +0000 | [diff] [blame^] | 43 | STATISTIC(NumCopies, "Number of copies coalesced"); |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 44 | |
| 45 | static RegisterRegAlloc |
| 46 | fastRegAlloc("fast", "fast register allocator", createFastRegisterAllocator); |
| 47 | |
| 48 | namespace { |
| 49 | class RAFast : public MachineFunctionPass { |
| 50 | public: |
| 51 | static char ID; |
Jakob Stoklund Olesen | 7d4f259 | 2010-05-14 00:02:20 +0000 | [diff] [blame] | 52 | RAFast() : MachineFunctionPass(&ID), StackSlotForVirtReg(-1), |
| 53 | atEndOfBlock(false) {} |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 54 | private: |
| 55 | const TargetMachine *TM; |
| 56 | MachineFunction *MF; |
Jakob Stoklund Olesen | 4bf4baf | 2010-05-13 00:19:43 +0000 | [diff] [blame] | 57 | MachineRegisterInfo *MRI; |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 58 | const TargetRegisterInfo *TRI; |
| 59 | const TargetInstrInfo *TII; |
| 60 | |
| 61 | // StackSlotForVirtReg - Maps virtual regs to the frame index where these |
| 62 | // values are spilled. |
| 63 | IndexedMap<int, VirtReg2IndexFunctor> StackSlotForVirtReg; |
| 64 | |
Jakob Stoklund Olesen | 76b4d5a | 2010-05-11 23:24:45 +0000 | [diff] [blame] | 65 | // Everything we know about a live virtual register. |
| 66 | struct LiveReg { |
Jakob Stoklund Olesen | 210e2af | 2010-05-11 23:24:47 +0000 | [diff] [blame] | 67 | MachineInstr *LastUse; // Last instr to use reg. |
| 68 | unsigned PhysReg; // Currently held here. |
| 69 | unsigned short LastOpNum; // OpNum on LastUse. |
| 70 | bool Dirty; // Register needs spill. |
Jakob Stoklund Olesen | 76b4d5a | 2010-05-11 23:24:45 +0000 | [diff] [blame] | 71 | |
Jakob Stoklund Olesen | 210e2af | 2010-05-11 23:24:47 +0000 | [diff] [blame] | 72 | LiveReg(unsigned p=0) : LastUse(0), PhysReg(p), LastOpNum(0), |
| 73 | Dirty(false) { |
Jakob Stoklund Olesen | 76b4d5a | 2010-05-11 23:24:45 +0000 | [diff] [blame] | 74 | assert(p && "Don't create LiveRegs without a PhysReg"); |
| 75 | } |
| 76 | }; |
| 77 | |
| 78 | typedef DenseMap<unsigned, LiveReg> LiveRegMap; |
| 79 | |
| 80 | // LiveVirtRegs - This map contains entries for each virtual register |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 81 | // that is currently available in a physical register. |
Jakob Stoklund Olesen | 76b4d5a | 2010-05-11 23:24:45 +0000 | [diff] [blame] | 82 | LiveRegMap LiveVirtRegs; |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 83 | |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 84 | // RegState - Track the state of a physical register. |
| 85 | enum RegState { |
| 86 | // A disabled register is not available for allocation, but an alias may |
| 87 | // be in use. A register can only be moved out of the disabled state if |
| 88 | // all aliases are disabled. |
| 89 | regDisabled, |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 90 | |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 91 | // A free register is not currently in use and can be allocated |
| 92 | // immediately without checking aliases. |
| 93 | regFree, |
| 94 | |
| 95 | // A reserved register has been assigned expolicitly (e.g., setting up a |
| 96 | // call parameter), and it remains reserved until it is used. |
| 97 | regReserved |
| 98 | |
| 99 | // A register state may also be a virtual register number, indication that |
| 100 | // the physical register is currently allocated to a virtual register. In |
Jakob Stoklund Olesen | 76b4d5a | 2010-05-11 23:24:45 +0000 | [diff] [blame] | 101 | // that case, LiveVirtRegs contains the inverse mapping. |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 102 | }; |
| 103 | |
| 104 | // PhysRegState - One of the RegState enums, or a virtreg. |
| 105 | std::vector<unsigned> PhysRegState; |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 106 | |
| 107 | // UsedInInstr - BitVector of physregs that are used in the current |
| 108 | // instruction, and so cannot be allocated. |
| 109 | BitVector UsedInInstr; |
| 110 | |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 111 | // ReservedRegs - vector of reserved physical registers. |
| 112 | BitVector ReservedRegs; |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 113 | |
Jakob Stoklund Olesen | 7d4f259 | 2010-05-14 00:02:20 +0000 | [diff] [blame] | 114 | // atEndOfBlock - This flag is set after allocating all instructions in a |
| 115 | // block, before emitting final spills. When it is set, LiveRegMap is no |
| 116 | // longer updated properly sonce it will be cleared anyway. |
| 117 | bool atEndOfBlock; |
| 118 | |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 119 | public: |
| 120 | virtual const char *getPassName() const { |
| 121 | return "Fast Register Allocator"; |
| 122 | } |
| 123 | |
| 124 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 125 | AU.setPreservesCFG(); |
| 126 | AU.addRequiredID(PHIEliminationID); |
| 127 | AU.addRequiredID(TwoAddressInstructionPassID); |
| 128 | MachineFunctionPass::getAnalysisUsage(AU); |
| 129 | } |
| 130 | |
| 131 | private: |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 132 | bool runOnMachineFunction(MachineFunction &Fn); |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 133 | void AllocateBasicBlock(MachineBasicBlock &MBB); |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 134 | int getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC); |
Jakob Stoklund Olesen | 804291e | 2010-05-12 18:46:03 +0000 | [diff] [blame] | 135 | void addKillFlag(LiveRegMap::iterator i); |
Jakob Stoklund Olesen | 76b4d5a | 2010-05-11 23:24:45 +0000 | [diff] [blame] | 136 | void killVirtReg(LiveRegMap::iterator i); |
Jakob Stoklund Olesen | 804291e | 2010-05-12 18:46:03 +0000 | [diff] [blame] | 137 | void killVirtReg(unsigned VirtReg); |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 138 | void spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, |
Jakob Stoklund Olesen | 7d4f259 | 2010-05-14 00:02:20 +0000 | [diff] [blame] | 139 | LiveRegMap::iterator i, bool isKill); |
| 140 | void spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 141 | unsigned VirtReg, bool isKill); |
Jakob Stoklund Olesen | 4ed1082 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 142 | |
| 143 | void usePhysReg(MachineOperand&); |
| 144 | void definePhysReg(MachineBasicBlock &MBB, MachineInstr *MI, |
| 145 | unsigned PhysReg, RegState NewState); |
Jakob Stoklund Olesen | 76b4d5a | 2010-05-11 23:24:45 +0000 | [diff] [blame] | 146 | LiveRegMap::iterator assignVirtToPhysReg(unsigned VirtReg, |
| 147 | unsigned PhysReg); |
| 148 | LiveRegMap::iterator allocVirtReg(MachineBasicBlock &MBB, MachineInstr *MI, |
Jakob Stoklund Olesen | 4bf4baf | 2010-05-13 00:19:43 +0000 | [diff] [blame] | 149 | unsigned VirtReg, unsigned Hint); |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 150 | unsigned defineVirtReg(MachineBasicBlock &MBB, MachineInstr *MI, |
Jakob Stoklund Olesen | 4bf4baf | 2010-05-13 00:19:43 +0000 | [diff] [blame] | 151 | unsigned OpNum, unsigned VirtReg, unsigned Hint); |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 152 | unsigned reloadVirtReg(MachineBasicBlock &MBB, MachineInstr *MI, |
Jakob Stoklund Olesen | 4bf4baf | 2010-05-13 00:19:43 +0000 | [diff] [blame] | 153 | unsigned OpNum, unsigned VirtReg, unsigned Hint); |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 154 | void spillAll(MachineBasicBlock &MBB, MachineInstr *MI); |
| 155 | void setPhysReg(MachineOperand &MO, unsigned PhysReg); |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 156 | }; |
| 157 | char RAFast::ID = 0; |
| 158 | } |
| 159 | |
| 160 | /// getStackSpaceFor - This allocates space for the specified virtual register |
| 161 | /// to be held on the stack. |
| 162 | int RAFast::getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC) { |
| 163 | // Find the location Reg would belong... |
| 164 | int SS = StackSlotForVirtReg[VirtReg]; |
| 165 | if (SS != -1) |
| 166 | return SS; // Already has space allocated? |
| 167 | |
| 168 | // Allocate a new stack object for this spill location... |
| 169 | int FrameIdx = MF->getFrameInfo()->CreateSpillStackObject(RC->getSize(), |
| 170 | RC->getAlignment()); |
| 171 | |
| 172 | // Assign the slot. |
| 173 | StackSlotForVirtReg[VirtReg] = FrameIdx; |
| 174 | return FrameIdx; |
| 175 | } |
| 176 | |
Jakob Stoklund Olesen | 804291e | 2010-05-12 18:46:03 +0000 | [diff] [blame] | 177 | /// addKillFlag - Set kill flags on last use of a virtual register. |
| 178 | void RAFast::addKillFlag(LiveRegMap::iterator lri) { |
| 179 | assert(lri != LiveVirtRegs.end() && "Killing unmapped virtual register"); |
| 180 | const LiveReg &LR = lri->second; |
Jakob Stoklund Olesen | 76b4d5a | 2010-05-11 23:24:45 +0000 | [diff] [blame] | 181 | if (LR.LastUse) { |
| 182 | MachineOperand &MO = LR.LastUse->getOperand(LR.LastOpNum); |
Jakob Stoklund Olesen | 804291e | 2010-05-12 18:46:03 +0000 | [diff] [blame] | 183 | if (MO.isDef()) |
| 184 | MO.setIsDead(); |
| 185 | else if (!LR.LastUse->isRegTiedToDefOperand(LR.LastOpNum)) |
| 186 | MO.setIsKill(); |
Jakob Stoklund Olesen | 76b4d5a | 2010-05-11 23:24:45 +0000 | [diff] [blame] | 187 | } |
Jakob Stoklund Olesen | 804291e | 2010-05-12 18:46:03 +0000 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | /// killVirtReg - Mark virtreg as no longer available. |
| 191 | void RAFast::killVirtReg(LiveRegMap::iterator lri) { |
| 192 | addKillFlag(lri); |
| 193 | const LiveReg &LR = lri->second; |
| 194 | assert(PhysRegState[LR.PhysReg] == lri->first && "Broken RegState mapping"); |
| 195 | PhysRegState[LR.PhysReg] = regFree; |
Jakob Stoklund Olesen | 7d4f259 | 2010-05-14 00:02:20 +0000 | [diff] [blame] | 196 | // Erase from LiveVirtRegs unless we're at the end of the block when |
| 197 | // everything will be bulk erased. |
| 198 | if (!atEndOfBlock) |
| 199 | LiveVirtRegs.erase(lri); |
Jakob Stoklund Olesen | 76b4d5a | 2010-05-11 23:24:45 +0000 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | /// killVirtReg - Mark virtreg as no longer available. |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 203 | void RAFast::killVirtReg(unsigned VirtReg) { |
| 204 | assert(TargetRegisterInfo::isVirtualRegister(VirtReg) && |
| 205 | "killVirtReg needs a virtual register"); |
Jakob Stoklund Olesen | 1a1ad57 | 2010-05-12 00:11:19 +0000 | [diff] [blame] | 206 | LiveRegMap::iterator lri = LiveVirtRegs.find(VirtReg); |
| 207 | if (lri != LiveVirtRegs.end()) |
| 208 | killVirtReg(lri); |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 211 | /// spillVirtReg - This method spills the value specified by VirtReg into the |
| 212 | /// corresponding stack slot if needed. If isKill is set, the register is also |
| 213 | /// killed. |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 214 | void RAFast::spillVirtReg(MachineBasicBlock &MBB, |
Jakob Stoklund Olesen | 76b4d5a | 2010-05-11 23:24:45 +0000 | [diff] [blame] | 215 | MachineBasicBlock::iterator MI, |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 216 | unsigned VirtReg, bool isKill) { |
| 217 | assert(TargetRegisterInfo::isVirtualRegister(VirtReg) && |
| 218 | "Spilling a physical register is illegal!"); |
Jakob Stoklund Olesen | 1a1ad57 | 2010-05-12 00:11:19 +0000 | [diff] [blame] | 219 | LiveRegMap::iterator lri = LiveVirtRegs.find(VirtReg); |
| 220 | assert(lri != LiveVirtRegs.end() && "Spilling unmapped virtual register"); |
Jakob Stoklund Olesen | 7d4f259 | 2010-05-14 00:02:20 +0000 | [diff] [blame] | 221 | spillVirtReg(MBB, MI, lri, isKill); |
| 222 | } |
| 223 | |
| 224 | /// spillVirtReg - Do the actual work of spilling. |
| 225 | void RAFast::spillVirtReg(MachineBasicBlock &MBB, |
| 226 | MachineBasicBlock::iterator MI, |
| 227 | LiveRegMap::iterator lri, bool isKill) { |
Jakob Stoklund Olesen | 1a1ad57 | 2010-05-12 00:11:19 +0000 | [diff] [blame] | 228 | LiveReg &LR = lri->second; |
Jakob Stoklund Olesen | 7d4f259 | 2010-05-14 00:02:20 +0000 | [diff] [blame] | 229 | assert(PhysRegState[LR.PhysReg] == lri->first && "Broken RegState mapping"); |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 230 | |
Jakob Stoklund Olesen | 76b4d5a | 2010-05-11 23:24:45 +0000 | [diff] [blame] | 231 | // If this physreg is used by the instruction, we want to kill it on the |
| 232 | // instruction, not on the spill. |
| 233 | bool spillKill = isKill && LR.LastUse != MI; |
| 234 | |
Jakob Stoklund Olesen | 210e2af | 2010-05-11 23:24:47 +0000 | [diff] [blame] | 235 | if (LR.Dirty) { |
| 236 | LR.Dirty = false; |
Jakob Stoklund Olesen | 7d4f259 | 2010-05-14 00:02:20 +0000 | [diff] [blame] | 237 | DEBUG(dbgs() << "Spilling %reg" << lri->first |
| 238 | << " in " << TRI->getName(LR.PhysReg)); |
| 239 | const TargetRegisterClass *RC = MRI->getRegClass(lri->first); |
| 240 | int FrameIndex = getStackSpaceFor(lri->first, RC); |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 241 | DEBUG(dbgs() << " to stack slot #" << FrameIndex << "\n"); |
Jakob Stoklund Olesen | 76b4d5a | 2010-05-11 23:24:45 +0000 | [diff] [blame] | 242 | TII->storeRegToStackSlot(MBB, MI, LR.PhysReg, spillKill, |
| 243 | FrameIndex, RC, TRI); |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 244 | ++NumStores; // Update statistics |
Jakob Stoklund Olesen | 76b4d5a | 2010-05-11 23:24:45 +0000 | [diff] [blame] | 245 | |
| 246 | if (spillKill) |
Jakob Stoklund Olesen | 210e2af | 2010-05-11 23:24:47 +0000 | [diff] [blame] | 247 | LR.LastUse = 0; // Don't kill register again |
Jakob Stoklund Olesen | 76b4d5a | 2010-05-11 23:24:45 +0000 | [diff] [blame] | 248 | else if (!isKill) { |
| 249 | MachineInstr *Spill = llvm::prior(MI); |
Jakob Stoklund Olesen | 210e2af | 2010-05-11 23:24:47 +0000 | [diff] [blame] | 250 | LR.LastUse = Spill; |
| 251 | LR.LastOpNum = Spill->findRegisterUseOperandIdx(LR.PhysReg); |
Jakob Stoklund Olesen | 76b4d5a | 2010-05-11 23:24:45 +0000 | [diff] [blame] | 252 | } |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Jakob Stoklund Olesen | 76b4d5a | 2010-05-11 23:24:45 +0000 | [diff] [blame] | 255 | if (isKill) |
Jakob Stoklund Olesen | 1a1ad57 | 2010-05-12 00:11:19 +0000 | [diff] [blame] | 256 | killVirtReg(lri); |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 259 | /// spillAll - Spill all dirty virtregs without killing them. |
| 260 | void RAFast::spillAll(MachineBasicBlock &MBB, MachineInstr *MI) { |
| 261 | SmallVector<unsigned, 16> Dirty; |
Jakob Stoklund Olesen | 76b4d5a | 2010-05-11 23:24:45 +0000 | [diff] [blame] | 262 | for (LiveRegMap::iterator i = LiveVirtRegs.begin(), |
| 263 | e = LiveVirtRegs.end(); i != e; ++i) |
Jakob Stoklund Olesen | 210e2af | 2010-05-11 23:24:47 +0000 | [diff] [blame] | 264 | if (i->second.Dirty) |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 265 | Dirty.push_back(i->first); |
| 266 | for (unsigned i = 0, e = Dirty.size(); i != e; ++i) |
| 267 | spillVirtReg(MBB, MI, Dirty[i], false); |
| 268 | } |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 269 | |
Jakob Stoklund Olesen | 4ed1082 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 270 | /// usePhysReg - Handle the direct use of a physical register. |
| 271 | /// Check that the register is not used by a virtreg. |
| 272 | /// Kill the physreg, marking it free. |
| 273 | /// This may add implicit kills to MO->getParent() and invalidate MO. |
| 274 | void RAFast::usePhysReg(MachineOperand &MO) { |
| 275 | unsigned PhysReg = MO.getReg(); |
| 276 | assert(TargetRegisterInfo::isPhysicalRegister(PhysReg) && |
| 277 | "Bad usePhysReg operand"); |
| 278 | |
| 279 | switch (PhysRegState[PhysReg]) { |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 280 | case regDisabled: |
| 281 | break; |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 282 | case regReserved: |
| 283 | PhysRegState[PhysReg] = regFree; |
Jakob Stoklund Olesen | 4ed1082 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 284 | // Fall through |
| 285 | case regFree: |
| 286 | UsedInInstr.set(PhysReg); |
| 287 | MO.setIsKill(); |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 288 | return; |
| 289 | default: |
Jakob Stoklund Olesen | 4ed1082 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 290 | // The physreg was allocated to a virtual register. That means to value we |
| 291 | // wanted has been clobbered. |
| 292 | llvm_unreachable("Instruction uses an allocated register"); |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 293 | } |
| 294 | |
Jakob Stoklund Olesen | 4ed1082 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 295 | // Maybe a superregister is reserved? |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 296 | for (const unsigned *AS = TRI->getAliasSet(PhysReg); |
| 297 | unsigned Alias = *AS; ++AS) { |
Jakob Stoklund Olesen | 4ed1082 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 298 | switch (PhysRegState[Alias]) { |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 299 | case regDisabled: |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 300 | break; |
| 301 | case regReserved: |
Jakob Stoklund Olesen | 4ed1082 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 302 | assert(TRI->isSuperRegister(PhysReg, Alias) && |
| 303 | "Instruction is not using a subregister of a reserved register"); |
| 304 | // Leave the superregister in the working set. |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 305 | PhysRegState[Alias] = regFree; |
Jakob Stoklund Olesen | 4ed1082 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 306 | UsedInInstr.set(Alias); |
| 307 | MO.getParent()->addRegisterKilled(Alias, TRI, true); |
| 308 | return; |
| 309 | case regFree: |
| 310 | if (TRI->isSuperRegister(PhysReg, Alias)) { |
| 311 | // Leave the superregister in the working set. |
| 312 | UsedInInstr.set(Alias); |
| 313 | MO.getParent()->addRegisterKilled(Alias, TRI, true); |
| 314 | return; |
| 315 | } |
| 316 | // Some other alias was in the working set - clear it. |
| 317 | PhysRegState[Alias] = regDisabled; |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 318 | break; |
| 319 | default: |
Jakob Stoklund Olesen | 4ed1082 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 320 | llvm_unreachable("Instruction uses an alias of an allocated register"); |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 321 | } |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 322 | } |
Jakob Stoklund Olesen | 4ed1082 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 323 | |
| 324 | // All aliases are disabled, bring register into working set. |
| 325 | PhysRegState[PhysReg] = regFree; |
| 326 | UsedInInstr.set(PhysReg); |
| 327 | MO.setIsKill(); |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 328 | } |
| 329 | |
Jakob Stoklund Olesen | 4ed1082 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 330 | /// definePhysReg - Mark PhysReg as reserved or free after spilling any |
| 331 | /// virtregs. This is very similar to defineVirtReg except the physreg is |
| 332 | /// reserved instead of allocated. |
| 333 | void RAFast::definePhysReg(MachineBasicBlock &MBB, MachineInstr *MI, |
| 334 | unsigned PhysReg, RegState NewState) { |
| 335 | UsedInInstr.set(PhysReg); |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 336 | switch (unsigned VirtReg = PhysRegState[PhysReg]) { |
| 337 | case regDisabled: |
| 338 | break; |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 339 | default: |
Jakob Stoklund Olesen | 4ed1082 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 340 | spillVirtReg(MBB, MI, VirtReg, true); |
| 341 | // Fall through. |
| 342 | case regFree: |
| 343 | case regReserved: |
| 344 | PhysRegState[PhysReg] = NewState; |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 345 | return; |
| 346 | } |
| 347 | |
Jakob Stoklund Olesen | 4ed1082 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 348 | // This is a disabled register, disable all aliases. |
| 349 | PhysRegState[PhysReg] = NewState; |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 350 | for (const unsigned *AS = TRI->getAliasSet(PhysReg); |
| 351 | unsigned Alias = *AS; ++AS) { |
Jakob Stoklund Olesen | 4ed1082 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 352 | UsedInInstr.set(Alias); |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 353 | switch (unsigned VirtReg = PhysRegState[Alias]) { |
| 354 | case regDisabled: |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 355 | break; |
| 356 | default: |
Jakob Stoklund Olesen | 4ed1082 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 357 | spillVirtReg(MBB, MI, VirtReg, true); |
| 358 | // Fall through. |
| 359 | case regFree: |
| 360 | case regReserved: |
| 361 | PhysRegState[Alias] = regDisabled; |
| 362 | if (TRI->isSuperRegister(PhysReg, Alias)) |
| 363 | return; |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 364 | break; |
| 365 | } |
| 366 | } |
| 367 | } |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 368 | |
Jakob Stoklund Olesen | 4ed1082 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 369 | |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 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 | /// |
Jakob Stoklund Olesen | 76b4d5a | 2010-05-11 23:24:45 +0000 | [diff] [blame] | 374 | RAFast::LiveRegMap::iterator |
| 375 | RAFast::assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg) { |
Jakob Stoklund Olesen | c9c4dac | 2010-05-13 20:43:17 +0000 | [diff] [blame] | 376 | DEBUG(dbgs() << "Assigning %reg" << VirtReg << " to " |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 377 | << TRI->getName(PhysReg) << "\n"); |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 378 | PhysRegState[PhysReg] = VirtReg; |
Jakob Stoklund Olesen | 76b4d5a | 2010-05-11 23:24:45 +0000 | [diff] [blame] | 379 | return LiveVirtRegs.insert(std::make_pair(VirtReg, PhysReg)).first; |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 380 | } |
| 381 | |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 382 | /// allocVirtReg - Allocate a physical register for VirtReg. |
Jakob Stoklund Olesen | 76b4d5a | 2010-05-11 23:24:45 +0000 | [diff] [blame] | 383 | RAFast::LiveRegMap::iterator RAFast::allocVirtReg(MachineBasicBlock &MBB, |
| 384 | MachineInstr *MI, |
Jakob Stoklund Olesen | 4bf4baf | 2010-05-13 00:19:43 +0000 | [diff] [blame] | 385 | unsigned VirtReg, |
| 386 | unsigned Hint) { |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 387 | const unsigned spillCost = 100; |
| 388 | assert(TargetRegisterInfo::isVirtualRegister(VirtReg) && |
| 389 | "Can only allocate virtual registers"); |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 390 | |
Jakob Stoklund Olesen | 4bf4baf | 2010-05-13 00:19:43 +0000 | [diff] [blame] | 391 | const TargetRegisterClass *RC = MRI->getRegClass(VirtReg); |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 392 | TargetRegisterClass::iterator AOB = RC->allocation_order_begin(*MF); |
| 393 | TargetRegisterClass::iterator AOE = RC->allocation_order_end(*MF); |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 394 | |
Jakob Stoklund Olesen | 4bf4baf | 2010-05-13 00:19:43 +0000 | [diff] [blame] | 395 | // Ignore invalid hints. |
| 396 | if (Hint && (!TargetRegisterInfo::isPhysicalRegister(Hint) || |
| 397 | !RC->contains(Hint) || UsedInInstr.test(Hint))) |
| 398 | Hint = 0; |
| 399 | |
| 400 | // If there is no hint, peek at the first use of this register. |
| 401 | if (!Hint && !MRI->use_nodbg_empty(VirtReg)) { |
| 402 | MachineInstr &MI = *MRI->use_nodbg_begin(VirtReg); |
| 403 | unsigned SrcReg, DstReg, SrcSubReg, DstSubReg; |
| 404 | // Copy to physreg -> use physreg as hint. |
| 405 | if (TII->isMoveInstr(MI, SrcReg, DstReg, SrcSubReg, DstSubReg) && |
| 406 | SrcReg == VirtReg && TargetRegisterInfo::isPhysicalRegister(DstReg) && |
| 407 | RC->contains(DstReg) && !UsedInInstr.test(DstReg)) { |
| 408 | Hint = DstReg; |
Jakob Stoklund Olesen | c9c4dac | 2010-05-13 20:43:17 +0000 | [diff] [blame] | 409 | DEBUG(dbgs() << "%reg" << VirtReg << " gets hint from " << MI); |
Jakob Stoklund Olesen | 4bf4baf | 2010-05-13 00:19:43 +0000 | [diff] [blame] | 410 | } |
| 411 | } |
| 412 | |
| 413 | // Take hint when possible. |
| 414 | if (Hint) { |
| 415 | assert(RC->contains(Hint) && !UsedInInstr.test(Hint) && |
| 416 | "Invalid hint should have been cleared"); |
| 417 | switch(PhysRegState[Hint]) { |
| 418 | case regDisabled: |
| 419 | case regReserved: |
| 420 | break; |
| 421 | default: |
Jakob Stoklund Olesen | 4bf4baf | 2010-05-13 00:19:43 +0000 | [diff] [blame] | 422 | spillVirtReg(MBB, MI, PhysRegState[Hint], true); |
| 423 | // Fall through. |
| 424 | case regFree: |
| 425 | return assignVirtToPhysReg(VirtReg, Hint); |
| 426 | } |
| 427 | } |
| 428 | |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 429 | // First try to find a completely free register. |
| 430 | unsigned BestCost = 0, BestReg = 0; |
| 431 | bool hasDisabled = false; |
| 432 | for (TargetRegisterClass::iterator I = AOB; I != AOE; ++I) { |
| 433 | unsigned PhysReg = *I; |
| 434 | switch(PhysRegState[PhysReg]) { |
| 435 | case regDisabled: |
| 436 | hasDisabled = true; |
| 437 | case regReserved: |
| 438 | continue; |
| 439 | case regFree: |
Jakob Stoklund Olesen | 76b4d5a | 2010-05-11 23:24:45 +0000 | [diff] [blame] | 440 | if (!UsedInInstr.test(PhysReg)) |
| 441 | return assignVirtToPhysReg(VirtReg, PhysReg); |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 442 | continue; |
| 443 | default: |
| 444 | // Grab the first spillable register we meet. |
Jakob Stoklund Olesen | 210e2af | 2010-05-11 23:24:47 +0000 | [diff] [blame] | 445 | if (!BestReg && !UsedInInstr.test(PhysReg)) |
| 446 | BestReg = PhysReg, BestCost = spillCost; |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 447 | continue; |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 448 | } |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 449 | } |
| 450 | |
Jakob Stoklund Olesen | c9c4dac | 2010-05-13 20:43:17 +0000 | [diff] [blame] | 451 | DEBUG(dbgs() << "Allocating %reg" << VirtReg << " from " << RC->getName() |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 452 | << " candidate=" << TRI->getName(BestReg) << "\n"); |
| 453 | |
| 454 | // Try to extend the working set for RC if there were any disabled registers. |
| 455 | if (hasDisabled && (!BestReg || BestCost >= spillCost)) { |
| 456 | for (TargetRegisterClass::iterator I = AOB; I != AOE; ++I) { |
| 457 | unsigned PhysReg = *I; |
| 458 | if (PhysRegState[PhysReg] != regDisabled || UsedInInstr.test(PhysReg)) |
| 459 | continue; |
| 460 | |
| 461 | // Calculate the cost of bringing PhysReg into the working set. |
| 462 | unsigned Cost=0; |
| 463 | bool Impossible = false; |
| 464 | for (const unsigned *AS = TRI->getAliasSet(PhysReg); |
| 465 | unsigned Alias = *AS; ++AS) { |
| 466 | if (UsedInInstr.test(Alias)) { |
| 467 | Impossible = true; |
| 468 | break; |
| 469 | } |
| 470 | switch (PhysRegState[Alias]) { |
| 471 | case regDisabled: |
| 472 | break; |
| 473 | case regReserved: |
| 474 | Impossible = true; |
| 475 | break; |
| 476 | case regFree: |
| 477 | Cost++; |
| 478 | break; |
| 479 | default: |
Jakob Stoklund Olesen | 210e2af | 2010-05-11 23:24:47 +0000 | [diff] [blame] | 480 | Cost += spillCost; |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 481 | break; |
| 482 | } |
| 483 | } |
| 484 | if (Impossible) continue; |
Jakob Stoklund Olesen | c9c4dac | 2010-05-13 20:43:17 +0000 | [diff] [blame] | 485 | DEBUG(dbgs() << "- candidate " << TRI->getName(PhysReg) |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 486 | << " cost=" << Cost << "\n"); |
| 487 | if (!BestReg || Cost < BestCost) { |
| 488 | BestReg = PhysReg; |
| 489 | BestCost = Cost; |
| 490 | if (Cost < spillCost) break; |
| 491 | } |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | if (BestReg) { |
| 496 | // BestCost is 0 when all aliases are already disabled. |
| 497 | if (BestCost) { |
| 498 | if (PhysRegState[BestReg] != regDisabled) |
| 499 | spillVirtReg(MBB, MI, PhysRegState[BestReg], true); |
| 500 | else { |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 501 | // Make sure all aliases are disabled. |
| 502 | for (const unsigned *AS = TRI->getAliasSet(BestReg); |
| 503 | unsigned Alias = *AS; ++AS) { |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 504 | switch (PhysRegState[Alias]) { |
| 505 | case regDisabled: |
| 506 | continue; |
| 507 | case regFree: |
| 508 | PhysRegState[Alias] = regDisabled; |
| 509 | break; |
| 510 | default: |
| 511 | spillVirtReg(MBB, MI, PhysRegState[Alias], true); |
| 512 | PhysRegState[Alias] = regDisabled; |
| 513 | break; |
| 514 | } |
| 515 | } |
| 516 | } |
| 517 | } |
Jakob Stoklund Olesen | 76b4d5a | 2010-05-11 23:24:45 +0000 | [diff] [blame] | 518 | return assignVirtToPhysReg(VirtReg, BestReg); |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | // Nothing we can do. |
| 522 | std::string msg; |
| 523 | raw_string_ostream Msg(msg); |
| 524 | Msg << "Ran out of registers during register allocation!"; |
| 525 | if (MI->isInlineAsm()) { |
| 526 | Msg << "\nPlease check your inline asm statement for " |
| 527 | << "invalid constraints:\n"; |
| 528 | MI->print(Msg, TM); |
| 529 | } |
| 530 | report_fatal_error(Msg.str()); |
Jakob Stoklund Olesen | 76b4d5a | 2010-05-11 23:24:45 +0000 | [diff] [blame] | 531 | return LiveVirtRegs.end(); |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 532 | } |
| 533 | |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 534 | /// defineVirtReg - Allocate a register for VirtReg and mark it as dirty. |
| 535 | unsigned RAFast::defineVirtReg(MachineBasicBlock &MBB, MachineInstr *MI, |
Jakob Stoklund Olesen | 4bf4baf | 2010-05-13 00:19:43 +0000 | [diff] [blame] | 536 | unsigned OpNum, unsigned VirtReg, unsigned Hint) { |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 537 | assert(TargetRegisterInfo::isVirtualRegister(VirtReg) && |
| 538 | "Not a virtual register"); |
Jakob Stoklund Olesen | 1a1ad57 | 2010-05-12 00:11:19 +0000 | [diff] [blame] | 539 | LiveRegMap::iterator lri = LiveVirtRegs.find(VirtReg); |
| 540 | if (lri == LiveVirtRegs.end()) |
Jakob Stoklund Olesen | 4bf4baf | 2010-05-13 00:19:43 +0000 | [diff] [blame] | 541 | lri = allocVirtReg(MBB, MI, VirtReg, Hint); |
Jakob Stoklund Olesen | 804291e | 2010-05-12 18:46:03 +0000 | [diff] [blame] | 542 | else |
| 543 | addKillFlag(lri); // Kill before redefine. |
Jakob Stoklund Olesen | 1a1ad57 | 2010-05-12 00:11:19 +0000 | [diff] [blame] | 544 | LiveReg &LR = lri->second; |
Jakob Stoklund Olesen | 210e2af | 2010-05-11 23:24:47 +0000 | [diff] [blame] | 545 | LR.LastUse = MI; |
| 546 | LR.LastOpNum = OpNum; |
| 547 | LR.Dirty = true; |
| 548 | UsedInInstr.set(LR.PhysReg); |
| 549 | return LR.PhysReg; |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 550 | } |
| 551 | |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 552 | /// reloadVirtReg - Make sure VirtReg is available in a physreg and return it. |
| 553 | unsigned RAFast::reloadVirtReg(MachineBasicBlock &MBB, MachineInstr *MI, |
Jakob Stoklund Olesen | 4bf4baf | 2010-05-13 00:19:43 +0000 | [diff] [blame] | 554 | unsigned OpNum, unsigned VirtReg, unsigned Hint) { |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 555 | assert(TargetRegisterInfo::isVirtualRegister(VirtReg) && |
| 556 | "Not a virtual register"); |
Jakob Stoklund Olesen | 1a1ad57 | 2010-05-12 00:11:19 +0000 | [diff] [blame] | 557 | LiveRegMap::iterator lri = LiveVirtRegs.find(VirtReg); |
| 558 | if (lri == LiveVirtRegs.end()) { |
Jakob Stoklund Olesen | 4bf4baf | 2010-05-13 00:19:43 +0000 | [diff] [blame] | 559 | lri = allocVirtReg(MBB, MI, VirtReg, Hint); |
| 560 | const TargetRegisterClass *RC = MRI->getRegClass(VirtReg); |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 561 | int FrameIndex = getStackSpaceFor(VirtReg, RC); |
Jakob Stoklund Olesen | c9c4dac | 2010-05-13 20:43:17 +0000 | [diff] [blame] | 562 | DEBUG(dbgs() << "Reloading %reg" << VirtReg << " into " |
Jakob Stoklund Olesen | 1a1ad57 | 2010-05-12 00:11:19 +0000 | [diff] [blame] | 563 | << TRI->getName(lri->second.PhysReg) << "\n"); |
| 564 | TII->loadRegFromStackSlot(MBB, MI, lri->second.PhysReg, FrameIndex, RC, |
| 565 | TRI); |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 566 | ++NumLoads; |
| 567 | } |
Jakob Stoklund Olesen | 1a1ad57 | 2010-05-12 00:11:19 +0000 | [diff] [blame] | 568 | LiveReg &LR = lri->second; |
Jakob Stoklund Olesen | 210e2af | 2010-05-11 23:24:47 +0000 | [diff] [blame] | 569 | LR.LastUse = MI; |
| 570 | LR.LastOpNum = OpNum; |
| 571 | UsedInInstr.set(LR.PhysReg); |
| 572 | return LR.PhysReg; |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 573 | } |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 574 | |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 575 | // setPhysReg - Change MO the refer the PhysReg, considering subregs. |
| 576 | void RAFast::setPhysReg(MachineOperand &MO, unsigned PhysReg) { |
| 577 | if (unsigned Idx = MO.getSubReg()) { |
| 578 | MO.setReg(PhysReg ? TRI->getSubReg(PhysReg, Idx) : 0); |
| 579 | MO.setSubReg(0); |
| 580 | } else |
| 581 | MO.setReg(PhysReg); |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 582 | } |
| 583 | |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 584 | void RAFast::AllocateBasicBlock(MachineBasicBlock &MBB) { |
Jakob Stoklund Olesen | c9c4dac | 2010-05-13 20:43:17 +0000 | [diff] [blame] | 585 | DEBUG(dbgs() << "\nAllocating " << MBB); |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 586 | |
Jakob Stoklund Olesen | 7d4f259 | 2010-05-14 00:02:20 +0000 | [diff] [blame] | 587 | atEndOfBlock = false; |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 588 | PhysRegState.assign(TRI->getNumRegs(), regDisabled); |
Jakob Stoklund Olesen | 76b4d5a | 2010-05-11 23:24:45 +0000 | [diff] [blame] | 589 | assert(LiveVirtRegs.empty() && "Mapping not cleared form last block?"); |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 590 | |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 591 | MachineBasicBlock::iterator MII = MBB.begin(); |
| 592 | |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 593 | // Add live-in registers as live. |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 594 | for (MachineBasicBlock::livein_iterator I = MBB.livein_begin(), |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 595 | E = MBB.livein_end(); I != E; ++I) |
Jakob Stoklund Olesen | 4ed1082 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 596 | definePhysReg(MBB, MII, *I, regReserved); |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 597 | |
Jakob Stoklund Olesen | 4ed1082 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 598 | SmallVector<unsigned, 8> VirtKills, PhysDefs; |
Jakob Stoklund Olesen | 7ff82e1 | 2010-05-14 04:30:51 +0000 | [diff] [blame] | 599 | SmallVector<MachineInstr*, 32> Coalesced; |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 600 | |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 601 | // Otherwise, sequentially allocate each instruction in the MBB. |
| 602 | while (MII != MBB.end()) { |
| 603 | MachineInstr *MI = MII++; |
| 604 | const TargetInstrDesc &TID = MI->getDesc(); |
| 605 | DEBUG({ |
Jakob Stoklund Olesen | c9c4dac | 2010-05-13 20:43:17 +0000 | [diff] [blame] | 606 | dbgs() << "\n>> " << *MI << "Regs:"; |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 607 | for (unsigned Reg = 1, E = TRI->getNumRegs(); Reg != E; ++Reg) { |
| 608 | if (PhysRegState[Reg] == regDisabled) continue; |
| 609 | dbgs() << " " << TRI->getName(Reg); |
| 610 | switch(PhysRegState[Reg]) { |
| 611 | case regFree: |
| 612 | break; |
| 613 | case regReserved: |
Jakob Stoklund Olesen | c9c4dac | 2010-05-13 20:43:17 +0000 | [diff] [blame] | 614 | dbgs() << "*"; |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 615 | break; |
| 616 | default: |
| 617 | dbgs() << "=%reg" << PhysRegState[Reg]; |
Jakob Stoklund Olesen | 210e2af | 2010-05-11 23:24:47 +0000 | [diff] [blame] | 618 | if (LiveVirtRegs[PhysRegState[Reg]].Dirty) |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 619 | dbgs() << "*"; |
Jakob Stoklund Olesen | 76b4d5a | 2010-05-11 23:24:45 +0000 | [diff] [blame] | 620 | assert(LiveVirtRegs[PhysRegState[Reg]].PhysReg == Reg && |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 621 | "Bad inverse map"); |
| 622 | break; |
| 623 | } |
| 624 | } |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 625 | dbgs() << '\n'; |
Jakob Stoklund Olesen | 76b4d5a | 2010-05-11 23:24:45 +0000 | [diff] [blame] | 626 | // Check that LiveVirtRegs is the inverse. |
| 627 | for (LiveRegMap::iterator i = LiveVirtRegs.begin(), |
| 628 | e = LiveVirtRegs.end(); i != e; ++i) { |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 629 | assert(TargetRegisterInfo::isVirtualRegister(i->first) && |
| 630 | "Bad map key"); |
Jakob Stoklund Olesen | 76b4d5a | 2010-05-11 23:24:45 +0000 | [diff] [blame] | 631 | assert(TargetRegisterInfo::isPhysicalRegister(i->second.PhysReg) && |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 632 | "Bad map value"); |
Jakob Stoklund Olesen | 76b4d5a | 2010-05-11 23:24:45 +0000 | [diff] [blame] | 633 | assert(PhysRegState[i->second.PhysReg] == i->first && |
| 634 | "Bad inverse map"); |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 635 | } |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 636 | }); |
| 637 | |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 638 | // Debug values are not allowed to change codegen in any way. |
| 639 | if (MI->isDebugValue()) { |
| 640 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 641 | MachineOperand &MO = MI->getOperand(i); |
| 642 | if (!MO.isReg()) continue; |
| 643 | unsigned Reg = MO.getReg(); |
| 644 | if (!Reg || TargetRegisterInfo::isPhysicalRegister(Reg)) continue; |
Jakob Stoklund Olesen | 1a1ad57 | 2010-05-12 00:11:19 +0000 | [diff] [blame] | 645 | LiveRegMap::iterator lri = LiveVirtRegs.find(Reg); |
| 646 | if (lri != LiveVirtRegs.end()) |
| 647 | setPhysReg(MO, lri->second.PhysReg); |
Jakob Stoklund Olesen | 76b4d5a | 2010-05-11 23:24:45 +0000 | [diff] [blame] | 648 | else |
| 649 | MO.setReg(0); // We can't allocate a physreg for a DebugValue, sorry! |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 650 | } |
| 651 | // Next instruction. |
| 652 | continue; |
| 653 | } |
| 654 | |
Jakob Stoklund Olesen | 4bf4baf | 2010-05-13 00:19:43 +0000 | [diff] [blame] | 655 | // If this is a copy, we may be able to coalesce. |
| 656 | unsigned CopySrc, CopyDst, CopySrcSub, CopyDstSub; |
| 657 | if (!TII->isMoveInstr(*MI, CopySrc, CopyDst, CopySrcSub, CopyDstSub)) |
| 658 | CopySrc = CopyDst = 0; |
| 659 | |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 660 | // Track registers used by instruction. |
| 661 | UsedInInstr.reset(); |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 662 | PhysDefs.clear(); |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 663 | |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 664 | // First scan. |
| 665 | // Mark physreg uses and early clobbers as used. |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 666 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 667 | MachineOperand &MO = MI->getOperand(i); |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 668 | if (!MO.isReg()) continue; |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 669 | unsigned Reg = MO.getReg(); |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 670 | if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg) || |
| 671 | ReservedRegs.test(Reg)) continue; |
| 672 | if (MO.isUse()) { |
Jakob Stoklund Olesen | 4ed1082 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 673 | usePhysReg(MO); |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 674 | } else if (MO.isEarlyClobber()) { |
Jakob Stoklund Olesen | 4ed1082 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 675 | definePhysReg(MBB, MI, Reg, MO.isDead() ? regFree : regReserved); |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 676 | PhysDefs.push_back(Reg); |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 677 | } |
| 678 | } |
| 679 | |
Jakob Stoklund Olesen | 4ed1082 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 680 | |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 681 | // Second scan. |
| 682 | // Allocate virtreg uses and early clobbers. |
| 683 | // Collect VirtKills |
| 684 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 685 | MachineOperand &MO = MI->getOperand(i); |
| 686 | if (!MO.isReg()) continue; |
| 687 | unsigned Reg = MO.getReg(); |
| 688 | if (!Reg || TargetRegisterInfo::isPhysicalRegister(Reg)) continue; |
| 689 | if (MO.isUse()) { |
Jakob Stoklund Olesen | 4bf4baf | 2010-05-13 00:19:43 +0000 | [diff] [blame] | 690 | unsigned PhysReg = reloadVirtReg(MBB, MI, i, Reg, CopyDst); |
Jakob Stoklund Olesen | 7ff82e1 | 2010-05-14 04:30:51 +0000 | [diff] [blame] | 691 | CopySrc = (CopySrc == Reg || CopySrc == PhysReg) ? PhysReg : 0; |
Jakob Stoklund Olesen | 4bf4baf | 2010-05-13 00:19:43 +0000 | [diff] [blame] | 692 | setPhysReg(MO, PhysReg); |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 693 | if (MO.isKill()) |
| 694 | VirtKills.push_back(Reg); |
| 695 | } else if (MO.isEarlyClobber()) { |
Jakob Stoklund Olesen | 4bf4baf | 2010-05-13 00:19:43 +0000 | [diff] [blame] | 696 | unsigned PhysReg = defineVirtReg(MBB, MI, i, Reg, 0); |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 697 | setPhysReg(MO, PhysReg); |
| 698 | PhysDefs.push_back(PhysReg); |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | // Process virtreg kills |
| 703 | for (unsigned i = 0, e = VirtKills.size(); i != e; ++i) |
| 704 | killVirtReg(VirtKills[i]); |
| 705 | VirtKills.clear(); |
| 706 | |
Jakob Stoklund Olesen | 4bf4baf | 2010-05-13 00:19:43 +0000 | [diff] [blame] | 707 | MRI->addPhysRegsUsed(UsedInInstr); |
Jakob Stoklund Olesen | 82b07dc | 2010-05-11 20:30:28 +0000 | [diff] [blame] | 708 | |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 709 | // Track registers defined by instruction - early clobbers at this point. |
| 710 | UsedInInstr.reset(); |
| 711 | for (unsigned i = 0, e = PhysDefs.size(); i != e; ++i) { |
| 712 | unsigned PhysReg = PhysDefs[i]; |
| 713 | UsedInInstr.set(PhysReg); |
| 714 | for (const unsigned *AS = TRI->getAliasSet(PhysReg); |
| 715 | unsigned Alias = *AS; ++AS) |
| 716 | UsedInInstr.set(Alias); |
| 717 | } |
| 718 | |
| 719 | // Third scan. |
| 720 | // Allocate defs and collect dead defs. |
| 721 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 722 | MachineOperand &MO = MI->getOperand(i); |
| 723 | if (!MO.isReg() || !MO.isDef() || !MO.getReg()) continue; |
| 724 | unsigned Reg = MO.getReg(); |
| 725 | |
| 726 | if (TargetRegisterInfo::isPhysicalRegister(Reg)) { |
| 727 | if (ReservedRegs.test(Reg)) continue; |
Jakob Stoklund Olesen | 4ed1082 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 728 | definePhysReg(MBB, MI, Reg, (MO.isImplicit() || MO.isDead()) ? |
| 729 | regFree : regReserved); |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 730 | continue; |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 731 | } |
Jakob Stoklund Olesen | 4bf4baf | 2010-05-13 00:19:43 +0000 | [diff] [blame] | 732 | unsigned PhysReg = defineVirtReg(MBB, MI, i, Reg, CopySrc); |
Jakob Stoklund Olesen | 7ff82e1 | 2010-05-14 04:30:51 +0000 | [diff] [blame] | 733 | if (MO.isDead()) { |
| 734 | VirtKills.push_back(Reg); |
| 735 | CopyDst = 0; // cancel coalescing; |
| 736 | } else |
| 737 | CopyDst = (CopyDst == Reg || CopyDst == PhysReg) ? PhysReg : 0; |
Jakob Stoklund Olesen | 4bf4baf | 2010-05-13 00:19:43 +0000 | [diff] [blame] | 738 | setPhysReg(MO, PhysReg); |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 739 | } |
| 740 | |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 741 | // Spill all dirty virtregs before a call, in case of an exception. |
| 742 | if (TID.isCall()) { |
| 743 | DEBUG(dbgs() << " Spilling remaining registers before call.\n"); |
| 744 | spillAll(MBB, MI); |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 745 | } |
| 746 | |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 747 | // Process virtreg deads. |
| 748 | for (unsigned i = 0, e = VirtKills.size(); i != e; ++i) |
| 749 | killVirtReg(VirtKills[i]); |
| 750 | VirtKills.clear(); |
| 751 | |
Jakob Stoklund Olesen | 4bf4baf | 2010-05-13 00:19:43 +0000 | [diff] [blame] | 752 | MRI->addPhysRegsUsed(UsedInInstr); |
Jakob Stoklund Olesen | c9c4dac | 2010-05-13 20:43:17 +0000 | [diff] [blame] | 753 | |
Jakob Stoklund Olesen | 7ff82e1 | 2010-05-14 04:30:51 +0000 | [diff] [blame] | 754 | if (CopyDst && CopyDst == CopySrc && CopyDstSub == CopySrcSub) { |
| 755 | DEBUG(dbgs() << "-- coalescing: " << *MI); |
| 756 | Coalesced.push_back(MI); |
| 757 | } else { |
| 758 | DEBUG(dbgs() << "<< " << *MI); |
| 759 | } |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 760 | } |
| 761 | |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 762 | // Spill all physical registers holding virtual registers now. |
Jakob Stoklund Olesen | 7d4f259 | 2010-05-14 00:02:20 +0000 | [diff] [blame] | 763 | atEndOfBlock = true; |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 764 | DEBUG(dbgs() << "Killing live registers at end of block.\n"); |
| 765 | MachineBasicBlock::iterator MI = MBB.getFirstTerminator(); |
Jakob Stoklund Olesen | 7d4f259 | 2010-05-14 00:02:20 +0000 | [diff] [blame] | 766 | for (LiveRegMap::iterator i = LiveVirtRegs.begin(), e = LiveVirtRegs.end(); |
| 767 | i != e; ++i) |
| 768 | spillVirtReg(MBB, MI, i, true); |
| 769 | LiveVirtRegs.clear(); |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 770 | |
Jakob Stoklund Olesen | 7ff82e1 | 2010-05-14 04:30:51 +0000 | [diff] [blame] | 771 | // Erase all the coalesced copies. We are delaying it until now because |
| 772 | // LiveVirtsRegs might refer to the instrs. |
| 773 | for (unsigned i = 0, e = Coalesced.size(); i != e; ++i) |
| 774 | MBB.erase(Coalesced[i]); |
Jakob Stoklund Olesen | 8a65c51 | 2010-05-14 21:55:50 +0000 | [diff] [blame^] | 775 | NumCopies += Coalesced.size(); |
Jakob Stoklund Olesen | 7ff82e1 | 2010-05-14 04:30:51 +0000 | [diff] [blame] | 776 | |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 777 | DEBUG(MBB.dump()); |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | /// runOnMachineFunction - Register allocate the whole function |
| 781 | /// |
| 782 | bool RAFast::runOnMachineFunction(MachineFunction &Fn) { |
Jakob Stoklund Olesen | c9c4dac | 2010-05-13 20:43:17 +0000 | [diff] [blame] | 783 | DEBUG(dbgs() << "********** FAST REGISTER ALLOCATION **********\n" |
| 784 | << "********** Function: " |
| 785 | << ((Value*)Fn.getFunction())->getName() << '\n'); |
Jakob Stoklund Olesen | 1b2c761 | 2010-05-14 20:28:32 +0000 | [diff] [blame] | 786 | if (VerifyFastRegalloc) |
Jakob Stoklund Olesen | a0e618d | 2010-05-14 21:55:44 +0000 | [diff] [blame] | 787 | Fn.verify(this, true); |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 788 | MF = &Fn; |
Jakob Stoklund Olesen | 4bf4baf | 2010-05-13 00:19:43 +0000 | [diff] [blame] | 789 | MRI = &MF->getRegInfo(); |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 790 | TM = &Fn.getTarget(); |
| 791 | TRI = TM->getRegisterInfo(); |
| 792 | TII = TM->getInstrInfo(); |
| 793 | |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 794 | UsedInInstr.resize(TRI->getNumRegs()); |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 795 | ReservedRegs = TRI->getReservedRegs(*MF); |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 796 | |
| 797 | // initialize the virtual->physical register map to have a 'null' |
| 798 | // mapping for all virtual registers |
Jakob Stoklund Olesen | 4bf4baf | 2010-05-13 00:19:43 +0000 | [diff] [blame] | 799 | unsigned LastVirtReg = MRI->getLastVirtReg(); |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 800 | StackSlotForVirtReg.grow(LastVirtReg); |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 801 | |
| 802 | // Loop over all of the basic blocks, eliminating virtual register references |
| 803 | for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end(); |
| 804 | MBB != MBBe; ++MBB) |
| 805 | AllocateBasicBlock(*MBB); |
| 806 | |
Jakob Stoklund Olesen | 82b07dc | 2010-05-11 20:30:28 +0000 | [diff] [blame] | 807 | // Make sure the set of used physregs is closed under subreg operations. |
Jakob Stoklund Olesen | 4bf4baf | 2010-05-13 00:19:43 +0000 | [diff] [blame] | 808 | MRI->closePhysRegsUsed(*TRI); |
Jakob Stoklund Olesen | 82b07dc | 2010-05-11 20:30:28 +0000 | [diff] [blame] | 809 | |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 810 | StackSlotForVirtReg.clear(); |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 811 | return true; |
| 812 | } |
| 813 | |
| 814 | FunctionPass *llvm::createFastRegisterAllocator() { |
| 815 | return new RAFast(); |
| 816 | } |