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 | efa155f | 2010-05-14 22:02:56 +0000 | [diff] [blame] | 111 | // Allocatable - vector of allocatable physical registers. |
| 112 | BitVector Allocatable; |
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 | 1e03ff4 | 2010-05-15 06:09:08 +0000 | [diff] [blame] | 135 | bool isLastUseOfLocalReg(MachineOperand&); |
| 136 | |
Jakob Stoklund Olesen | 804291e | 2010-05-12 18:46:03 +0000 | [diff] [blame] | 137 | void addKillFlag(LiveRegMap::iterator i); |
Jakob Stoklund Olesen | 76b4d5a | 2010-05-11 23:24:45 +0000 | [diff] [blame] | 138 | void killVirtReg(LiveRegMap::iterator i); |
Jakob Stoklund Olesen | 804291e | 2010-05-12 18:46:03 +0000 | [diff] [blame] | 139 | void killVirtReg(unsigned VirtReg); |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 140 | void spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, |
Jakob Stoklund Olesen | 7d4f259 | 2010-05-14 00:02:20 +0000 | [diff] [blame] | 141 | LiveRegMap::iterator i, bool isKill); |
| 142 | void spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 143 | unsigned VirtReg, bool isKill); |
Jakob Stoklund Olesen | 4ed1082 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 144 | |
| 145 | void usePhysReg(MachineOperand&); |
| 146 | void definePhysReg(MachineBasicBlock &MBB, MachineInstr *MI, |
| 147 | unsigned PhysReg, RegState NewState); |
Jakob Stoklund Olesen | 76b4d5a | 2010-05-11 23:24:45 +0000 | [diff] [blame] | 148 | LiveRegMap::iterator assignVirtToPhysReg(unsigned VirtReg, |
| 149 | unsigned PhysReg); |
| 150 | LiveRegMap::iterator allocVirtReg(MachineBasicBlock &MBB, MachineInstr *MI, |
Jakob Stoklund Olesen | 4bf4baf | 2010-05-13 00:19:43 +0000 | [diff] [blame] | 151 | unsigned VirtReg, unsigned Hint); |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 152 | unsigned defineVirtReg(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 | unsigned reloadVirtReg(MachineBasicBlock &MBB, MachineInstr *MI, |
Jakob Stoklund Olesen | 4bf4baf | 2010-05-13 00:19:43 +0000 | [diff] [blame] | 155 | unsigned OpNum, unsigned VirtReg, unsigned Hint); |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 156 | void spillAll(MachineBasicBlock &MBB, MachineInstr *MI); |
| 157 | void setPhysReg(MachineOperand &MO, unsigned PhysReg); |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 158 | }; |
| 159 | char RAFast::ID = 0; |
| 160 | } |
| 161 | |
| 162 | /// getStackSpaceFor - This allocates space for the specified virtual register |
| 163 | /// to be held on the stack. |
| 164 | int RAFast::getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC) { |
| 165 | // Find the location Reg would belong... |
| 166 | int SS = StackSlotForVirtReg[VirtReg]; |
| 167 | if (SS != -1) |
| 168 | return SS; // Already has space allocated? |
| 169 | |
| 170 | // Allocate a new stack object for this spill location... |
| 171 | int FrameIdx = MF->getFrameInfo()->CreateSpillStackObject(RC->getSize(), |
| 172 | RC->getAlignment()); |
| 173 | |
| 174 | // Assign the slot. |
| 175 | StackSlotForVirtReg[VirtReg] = FrameIdx; |
| 176 | return FrameIdx; |
| 177 | } |
| 178 | |
Jakob Stoklund Olesen | 1e03ff4 | 2010-05-15 06:09:08 +0000 | [diff] [blame] | 179 | /// isLastUseOfLocalReg - Return true if MO is the only remaining reference to |
| 180 | /// its virtual register, and it is guaranteed to be a block-local register. |
| 181 | /// |
| 182 | bool RAFast::isLastUseOfLocalReg(MachineOperand &MO) { |
| 183 | // Check for non-debug uses or defs following MO. |
| 184 | // This is the most likely way to fail - fast path it. |
| 185 | MachineOperand *i = &MO; |
| 186 | while ((i = i->getNextOperandForReg())) |
| 187 | if (!i->isDebug()) |
| 188 | return false; |
| 189 | |
| 190 | // If the register has ever been spilled or reloaded, we conservatively assume |
| 191 | // it is a global register used in multiple blocks. |
| 192 | if (StackSlotForVirtReg[MO.getReg()] != -1) |
| 193 | return false; |
| 194 | |
| 195 | // Check that the use/def chain has exactly one operand - MO. |
| 196 | return &MRI->reg_nodbg_begin(MO.getReg()).getOperand() == &MO; |
| 197 | } |
| 198 | |
Jakob Stoklund Olesen | 804291e | 2010-05-12 18:46:03 +0000 | [diff] [blame] | 199 | /// addKillFlag - Set kill flags on last use of a virtual register. |
| 200 | void RAFast::addKillFlag(LiveRegMap::iterator lri) { |
| 201 | assert(lri != LiveVirtRegs.end() && "Killing unmapped virtual register"); |
| 202 | const LiveReg &LR = lri->second; |
Jakob Stoklund Olesen | 76b4d5a | 2010-05-11 23:24:45 +0000 | [diff] [blame] | 203 | if (LR.LastUse) { |
| 204 | MachineOperand &MO = LR.LastUse->getOperand(LR.LastOpNum); |
Jakob Stoklund Olesen | 804291e | 2010-05-12 18:46:03 +0000 | [diff] [blame] | 205 | if (MO.isDef()) |
| 206 | MO.setIsDead(); |
| 207 | else if (!LR.LastUse->isRegTiedToDefOperand(LR.LastOpNum)) |
| 208 | MO.setIsKill(); |
Jakob Stoklund Olesen | 76b4d5a | 2010-05-11 23:24:45 +0000 | [diff] [blame] | 209 | } |
Jakob Stoklund Olesen | 804291e | 2010-05-12 18:46:03 +0000 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | /// killVirtReg - Mark virtreg as no longer available. |
| 213 | void RAFast::killVirtReg(LiveRegMap::iterator lri) { |
| 214 | addKillFlag(lri); |
| 215 | const LiveReg &LR = lri->second; |
| 216 | assert(PhysRegState[LR.PhysReg] == lri->first && "Broken RegState mapping"); |
| 217 | PhysRegState[LR.PhysReg] = regFree; |
Jakob Stoklund Olesen | 7d4f259 | 2010-05-14 00:02:20 +0000 | [diff] [blame] | 218 | // Erase from LiveVirtRegs unless we're at the end of the block when |
| 219 | // everything will be bulk erased. |
| 220 | if (!atEndOfBlock) |
| 221 | LiveVirtRegs.erase(lri); |
Jakob Stoklund Olesen | 76b4d5a | 2010-05-11 23:24:45 +0000 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | /// killVirtReg - Mark virtreg as no longer available. |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 225 | void RAFast::killVirtReg(unsigned VirtReg) { |
| 226 | assert(TargetRegisterInfo::isVirtualRegister(VirtReg) && |
| 227 | "killVirtReg needs a virtual register"); |
Jakob Stoklund Olesen | 1a1ad57 | 2010-05-12 00:11:19 +0000 | [diff] [blame] | 228 | LiveRegMap::iterator lri = LiveVirtRegs.find(VirtReg); |
| 229 | if (lri != LiveVirtRegs.end()) |
| 230 | killVirtReg(lri); |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 231 | } |
| 232 | |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 233 | /// spillVirtReg - This method spills the value specified by VirtReg into the |
| 234 | /// corresponding stack slot if needed. If isKill is set, the register is also |
| 235 | /// killed. |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 236 | void RAFast::spillVirtReg(MachineBasicBlock &MBB, |
Jakob Stoklund Olesen | 76b4d5a | 2010-05-11 23:24:45 +0000 | [diff] [blame] | 237 | MachineBasicBlock::iterator MI, |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 238 | unsigned VirtReg, bool isKill) { |
| 239 | assert(TargetRegisterInfo::isVirtualRegister(VirtReg) && |
| 240 | "Spilling a physical register is illegal!"); |
Jakob Stoklund Olesen | 1a1ad57 | 2010-05-12 00:11:19 +0000 | [diff] [blame] | 241 | LiveRegMap::iterator lri = LiveVirtRegs.find(VirtReg); |
| 242 | assert(lri != LiveVirtRegs.end() && "Spilling unmapped virtual register"); |
Jakob Stoklund Olesen | 7d4f259 | 2010-05-14 00:02:20 +0000 | [diff] [blame] | 243 | spillVirtReg(MBB, MI, lri, isKill); |
| 244 | } |
| 245 | |
| 246 | /// spillVirtReg - Do the actual work of spilling. |
| 247 | void RAFast::spillVirtReg(MachineBasicBlock &MBB, |
| 248 | MachineBasicBlock::iterator MI, |
| 249 | LiveRegMap::iterator lri, bool isKill) { |
Jakob Stoklund Olesen | 1a1ad57 | 2010-05-12 00:11:19 +0000 | [diff] [blame] | 250 | LiveReg &LR = lri->second; |
Jakob Stoklund Olesen | 7d4f259 | 2010-05-14 00:02:20 +0000 | [diff] [blame] | 251 | assert(PhysRegState[LR.PhysReg] == lri->first && "Broken RegState mapping"); |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 252 | |
Jakob Stoklund Olesen | 76b4d5a | 2010-05-11 23:24:45 +0000 | [diff] [blame] | 253 | // If this physreg is used by the instruction, we want to kill it on the |
| 254 | // instruction, not on the spill. |
| 255 | bool spillKill = isKill && LR.LastUse != MI; |
| 256 | |
Jakob Stoklund Olesen | 210e2af | 2010-05-11 23:24:47 +0000 | [diff] [blame] | 257 | if (LR.Dirty) { |
| 258 | LR.Dirty = false; |
Jakob Stoklund Olesen | 7d4f259 | 2010-05-14 00:02:20 +0000 | [diff] [blame] | 259 | DEBUG(dbgs() << "Spilling %reg" << lri->first |
| 260 | << " in " << TRI->getName(LR.PhysReg)); |
| 261 | const TargetRegisterClass *RC = MRI->getRegClass(lri->first); |
| 262 | int FrameIndex = getStackSpaceFor(lri->first, RC); |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 263 | DEBUG(dbgs() << " to stack slot #" << FrameIndex << "\n"); |
Jakob Stoklund Olesen | 76b4d5a | 2010-05-11 23:24:45 +0000 | [diff] [blame] | 264 | TII->storeRegToStackSlot(MBB, MI, LR.PhysReg, spillKill, |
| 265 | FrameIndex, RC, TRI); |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 266 | ++NumStores; // Update statistics |
Jakob Stoklund Olesen | 76b4d5a | 2010-05-11 23:24:45 +0000 | [diff] [blame] | 267 | |
| 268 | if (spillKill) |
Jakob Stoklund Olesen | 210e2af | 2010-05-11 23:24:47 +0000 | [diff] [blame] | 269 | LR.LastUse = 0; // Don't kill register again |
Jakob Stoklund Olesen | 76b4d5a | 2010-05-11 23:24:45 +0000 | [diff] [blame] | 270 | else if (!isKill) { |
| 271 | MachineInstr *Spill = llvm::prior(MI); |
Jakob Stoklund Olesen | 210e2af | 2010-05-11 23:24:47 +0000 | [diff] [blame] | 272 | LR.LastUse = Spill; |
| 273 | LR.LastOpNum = Spill->findRegisterUseOperandIdx(LR.PhysReg); |
Jakob Stoklund Olesen | 76b4d5a | 2010-05-11 23:24:45 +0000 | [diff] [blame] | 274 | } |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 275 | } |
| 276 | |
Jakob Stoklund Olesen | 76b4d5a | 2010-05-11 23:24:45 +0000 | [diff] [blame] | 277 | if (isKill) |
Jakob Stoklund Olesen | 1a1ad57 | 2010-05-12 00:11:19 +0000 | [diff] [blame] | 278 | killVirtReg(lri); |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 279 | } |
| 280 | |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 281 | /// spillAll - Spill all dirty virtregs without killing them. |
| 282 | void RAFast::spillAll(MachineBasicBlock &MBB, MachineInstr *MI) { |
| 283 | SmallVector<unsigned, 16> Dirty; |
Jakob Stoklund Olesen | 76b4d5a | 2010-05-11 23:24:45 +0000 | [diff] [blame] | 284 | for (LiveRegMap::iterator i = LiveVirtRegs.begin(), |
| 285 | e = LiveVirtRegs.end(); i != e; ++i) |
Jakob Stoklund Olesen | 210e2af | 2010-05-11 23:24:47 +0000 | [diff] [blame] | 286 | if (i->second.Dirty) |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 287 | Dirty.push_back(i->first); |
| 288 | for (unsigned i = 0, e = Dirty.size(); i != e; ++i) |
| 289 | spillVirtReg(MBB, MI, Dirty[i], false); |
| 290 | } |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 291 | |
Jakob Stoklund Olesen | 4ed1082 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 292 | /// usePhysReg - Handle the direct use of a physical register. |
| 293 | /// Check that the register is not used by a virtreg. |
| 294 | /// Kill the physreg, marking it free. |
| 295 | /// This may add implicit kills to MO->getParent() and invalidate MO. |
| 296 | void RAFast::usePhysReg(MachineOperand &MO) { |
| 297 | unsigned PhysReg = MO.getReg(); |
| 298 | assert(TargetRegisterInfo::isPhysicalRegister(PhysReg) && |
| 299 | "Bad usePhysReg operand"); |
| 300 | |
| 301 | switch (PhysRegState[PhysReg]) { |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 302 | case regDisabled: |
| 303 | break; |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 304 | case regReserved: |
| 305 | PhysRegState[PhysReg] = regFree; |
Jakob Stoklund Olesen | 4ed1082 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 306 | // Fall through |
| 307 | case regFree: |
| 308 | UsedInInstr.set(PhysReg); |
| 309 | MO.setIsKill(); |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 310 | return; |
| 311 | default: |
Jakob Stoklund Olesen | 4ed1082 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 312 | // The physreg was allocated to a virtual register. That means to value we |
| 313 | // wanted has been clobbered. |
| 314 | llvm_unreachable("Instruction uses an allocated register"); |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 315 | } |
| 316 | |
Jakob Stoklund Olesen | 4ed1082 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 317 | // Maybe a superregister is reserved? |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 318 | for (const unsigned *AS = TRI->getAliasSet(PhysReg); |
| 319 | unsigned Alias = *AS; ++AS) { |
Jakob Stoklund Olesen | 4ed1082 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 320 | switch (PhysRegState[Alias]) { |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 321 | case regDisabled: |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 322 | break; |
| 323 | case regReserved: |
Jakob Stoklund Olesen | 4ed1082 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 324 | assert(TRI->isSuperRegister(PhysReg, Alias) && |
| 325 | "Instruction is not using a subregister of a reserved register"); |
| 326 | // Leave the superregister in the working set. |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 327 | PhysRegState[Alias] = regFree; |
Jakob Stoklund Olesen | 4ed1082 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 328 | UsedInInstr.set(Alias); |
| 329 | MO.getParent()->addRegisterKilled(Alias, TRI, true); |
| 330 | return; |
| 331 | case regFree: |
| 332 | if (TRI->isSuperRegister(PhysReg, Alias)) { |
| 333 | // Leave the superregister in the working set. |
| 334 | UsedInInstr.set(Alias); |
| 335 | MO.getParent()->addRegisterKilled(Alias, TRI, true); |
| 336 | return; |
| 337 | } |
| 338 | // Some other alias was in the working set - clear it. |
| 339 | PhysRegState[Alias] = regDisabled; |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 340 | break; |
| 341 | default: |
Jakob Stoklund Olesen | 4ed1082 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 342 | llvm_unreachable("Instruction uses an alias of an allocated register"); |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 343 | } |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 344 | } |
Jakob Stoklund Olesen | 4ed1082 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 345 | |
| 346 | // All aliases are disabled, bring register into working set. |
| 347 | PhysRegState[PhysReg] = regFree; |
| 348 | UsedInInstr.set(PhysReg); |
| 349 | MO.setIsKill(); |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 350 | } |
| 351 | |
Jakob Stoklund Olesen | 4ed1082 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 352 | /// definePhysReg - Mark PhysReg as reserved or free after spilling any |
| 353 | /// virtregs. This is very similar to defineVirtReg except the physreg is |
| 354 | /// reserved instead of allocated. |
| 355 | void RAFast::definePhysReg(MachineBasicBlock &MBB, MachineInstr *MI, |
| 356 | unsigned PhysReg, RegState NewState) { |
| 357 | UsedInInstr.set(PhysReg); |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 358 | switch (unsigned VirtReg = PhysRegState[PhysReg]) { |
| 359 | case regDisabled: |
| 360 | break; |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 361 | default: |
Jakob Stoklund Olesen | 4ed1082 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 362 | spillVirtReg(MBB, MI, VirtReg, true); |
| 363 | // Fall through. |
| 364 | case regFree: |
| 365 | case regReserved: |
| 366 | PhysRegState[PhysReg] = NewState; |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 367 | return; |
| 368 | } |
| 369 | |
Jakob Stoklund Olesen | 4ed1082 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 370 | // This is a disabled register, disable all aliases. |
| 371 | PhysRegState[PhysReg] = NewState; |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 372 | for (const unsigned *AS = TRI->getAliasSet(PhysReg); |
| 373 | unsigned Alias = *AS; ++AS) { |
Jakob Stoklund Olesen | 4ed1082 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 374 | UsedInInstr.set(Alias); |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 375 | switch (unsigned VirtReg = PhysRegState[Alias]) { |
| 376 | case regDisabled: |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 377 | break; |
| 378 | default: |
Jakob Stoklund Olesen | 4ed1082 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 379 | spillVirtReg(MBB, MI, VirtReg, true); |
| 380 | // Fall through. |
| 381 | case regFree: |
| 382 | case regReserved: |
| 383 | PhysRegState[Alias] = regDisabled; |
| 384 | if (TRI->isSuperRegister(PhysReg, Alias)) |
| 385 | return; |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 386 | break; |
| 387 | } |
| 388 | } |
| 389 | } |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 390 | |
Jakob Stoklund Olesen | 4ed1082 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 391 | |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 392 | /// assignVirtToPhysReg - This method updates local state so that we know |
| 393 | /// that PhysReg is the proper container for VirtReg now. The physical |
| 394 | /// register must not be used for anything else when this is called. |
| 395 | /// |
Jakob Stoklund Olesen | 76b4d5a | 2010-05-11 23:24:45 +0000 | [diff] [blame] | 396 | RAFast::LiveRegMap::iterator |
| 397 | RAFast::assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg) { |
Jakob Stoklund Olesen | c9c4dac | 2010-05-13 20:43:17 +0000 | [diff] [blame] | 398 | DEBUG(dbgs() << "Assigning %reg" << VirtReg << " to " |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 399 | << TRI->getName(PhysReg) << "\n"); |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 400 | PhysRegState[PhysReg] = VirtReg; |
Jakob Stoklund Olesen | 76b4d5a | 2010-05-11 23:24:45 +0000 | [diff] [blame] | 401 | return LiveVirtRegs.insert(std::make_pair(VirtReg, PhysReg)).first; |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 402 | } |
| 403 | |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 404 | /// allocVirtReg - Allocate a physical register for VirtReg. |
Jakob Stoklund Olesen | 76b4d5a | 2010-05-11 23:24:45 +0000 | [diff] [blame] | 405 | RAFast::LiveRegMap::iterator RAFast::allocVirtReg(MachineBasicBlock &MBB, |
| 406 | MachineInstr *MI, |
Jakob Stoklund Olesen | 4bf4baf | 2010-05-13 00:19:43 +0000 | [diff] [blame] | 407 | unsigned VirtReg, |
| 408 | unsigned Hint) { |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 409 | const unsigned spillCost = 100; |
| 410 | assert(TargetRegisterInfo::isVirtualRegister(VirtReg) && |
| 411 | "Can only allocate virtual registers"); |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 412 | |
Jakob Stoklund Olesen | 4bf4baf | 2010-05-13 00:19:43 +0000 | [diff] [blame] | 413 | const TargetRegisterClass *RC = MRI->getRegClass(VirtReg); |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 414 | TargetRegisterClass::iterator AOB = RC->allocation_order_begin(*MF); |
| 415 | TargetRegisterClass::iterator AOE = RC->allocation_order_end(*MF); |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 416 | |
Jakob Stoklund Olesen | 4bf4baf | 2010-05-13 00:19:43 +0000 | [diff] [blame] | 417 | // Ignore invalid hints. |
| 418 | if (Hint && (!TargetRegisterInfo::isPhysicalRegister(Hint) || |
Chandler Carruth | 2c13ab2 | 2010-05-15 10:23:23 +0000 | [diff] [blame^] | 419 | !RC->contains(Hint) || UsedInInstr.test(Hint) || |
| 420 | !Allocatable.test(Hint))) |
Jakob Stoklund Olesen | 4bf4baf | 2010-05-13 00:19:43 +0000 | [diff] [blame] | 421 | Hint = 0; |
| 422 | |
| 423 | // If there is no hint, peek at the first use of this register. |
| 424 | if (!Hint && !MRI->use_nodbg_empty(VirtReg)) { |
| 425 | MachineInstr &MI = *MRI->use_nodbg_begin(VirtReg); |
| 426 | unsigned SrcReg, DstReg, SrcSubReg, DstSubReg; |
| 427 | // Copy to physreg -> use physreg as hint. |
| 428 | if (TII->isMoveInstr(MI, SrcReg, DstReg, SrcSubReg, DstSubReg) && |
| 429 | SrcReg == VirtReg && TargetRegisterInfo::isPhysicalRegister(DstReg) && |
Jakob Stoklund Olesen | efa155f | 2010-05-14 22:02:56 +0000 | [diff] [blame] | 430 | RC->contains(DstReg) && !UsedInInstr.test(DstReg) && |
| 431 | Allocatable.test(DstReg)) { |
Jakob Stoklund Olesen | 4bf4baf | 2010-05-13 00:19:43 +0000 | [diff] [blame] | 432 | Hint = DstReg; |
Jakob Stoklund Olesen | c9c4dac | 2010-05-13 20:43:17 +0000 | [diff] [blame] | 433 | DEBUG(dbgs() << "%reg" << VirtReg << " gets hint from " << MI); |
Jakob Stoklund Olesen | 4bf4baf | 2010-05-13 00:19:43 +0000 | [diff] [blame] | 434 | } |
| 435 | } |
| 436 | |
| 437 | // Take hint when possible. |
| 438 | if (Hint) { |
| 439 | assert(RC->contains(Hint) && !UsedInInstr.test(Hint) && |
Jakob Stoklund Olesen | efa155f | 2010-05-14 22:02:56 +0000 | [diff] [blame] | 440 | Allocatable.test(Hint) && "Invalid hint should have been cleared"); |
Jakob Stoklund Olesen | 4bf4baf | 2010-05-13 00:19:43 +0000 | [diff] [blame] | 441 | switch(PhysRegState[Hint]) { |
| 442 | case regDisabled: |
| 443 | case regReserved: |
| 444 | break; |
| 445 | default: |
Jakob Stoklund Olesen | 4bf4baf | 2010-05-13 00:19:43 +0000 | [diff] [blame] | 446 | spillVirtReg(MBB, MI, PhysRegState[Hint], true); |
| 447 | // Fall through. |
| 448 | case regFree: |
| 449 | return assignVirtToPhysReg(VirtReg, Hint); |
| 450 | } |
| 451 | } |
| 452 | |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 453 | // First try to find a completely free register. |
| 454 | unsigned BestCost = 0, BestReg = 0; |
| 455 | bool hasDisabled = false; |
| 456 | for (TargetRegisterClass::iterator I = AOB; I != AOE; ++I) { |
| 457 | unsigned PhysReg = *I; |
| 458 | switch(PhysRegState[PhysReg]) { |
| 459 | case regDisabled: |
| 460 | hasDisabled = true; |
| 461 | case regReserved: |
| 462 | continue; |
| 463 | case regFree: |
Jakob Stoklund Olesen | 76b4d5a | 2010-05-11 23:24:45 +0000 | [diff] [blame] | 464 | if (!UsedInInstr.test(PhysReg)) |
| 465 | return assignVirtToPhysReg(VirtReg, PhysReg); |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 466 | continue; |
| 467 | default: |
| 468 | // Grab the first spillable register we meet. |
Jakob Stoklund Olesen | 210e2af | 2010-05-11 23:24:47 +0000 | [diff] [blame] | 469 | if (!BestReg && !UsedInInstr.test(PhysReg)) |
| 470 | BestReg = PhysReg, BestCost = spillCost; |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 471 | continue; |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 472 | } |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 473 | } |
| 474 | |
Jakob Stoklund Olesen | c9c4dac | 2010-05-13 20:43:17 +0000 | [diff] [blame] | 475 | DEBUG(dbgs() << "Allocating %reg" << VirtReg << " from " << RC->getName() |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 476 | << " candidate=" << TRI->getName(BestReg) << "\n"); |
| 477 | |
| 478 | // Try to extend the working set for RC if there were any disabled registers. |
| 479 | if (hasDisabled && (!BestReg || BestCost >= spillCost)) { |
| 480 | for (TargetRegisterClass::iterator I = AOB; I != AOE; ++I) { |
| 481 | unsigned PhysReg = *I; |
| 482 | if (PhysRegState[PhysReg] != regDisabled || UsedInInstr.test(PhysReg)) |
| 483 | continue; |
| 484 | |
| 485 | // Calculate the cost of bringing PhysReg into the working set. |
| 486 | unsigned Cost=0; |
| 487 | bool Impossible = false; |
| 488 | for (const unsigned *AS = TRI->getAliasSet(PhysReg); |
| 489 | unsigned Alias = *AS; ++AS) { |
| 490 | if (UsedInInstr.test(Alias)) { |
| 491 | Impossible = true; |
| 492 | break; |
| 493 | } |
| 494 | switch (PhysRegState[Alias]) { |
| 495 | case regDisabled: |
| 496 | break; |
| 497 | case regReserved: |
| 498 | Impossible = true; |
| 499 | break; |
| 500 | case regFree: |
| 501 | Cost++; |
| 502 | break; |
| 503 | default: |
Jakob Stoklund Olesen | 210e2af | 2010-05-11 23:24:47 +0000 | [diff] [blame] | 504 | Cost += spillCost; |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 505 | break; |
| 506 | } |
| 507 | } |
| 508 | if (Impossible) continue; |
Jakob Stoklund Olesen | c9c4dac | 2010-05-13 20:43:17 +0000 | [diff] [blame] | 509 | DEBUG(dbgs() << "- candidate " << TRI->getName(PhysReg) |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 510 | << " cost=" << Cost << "\n"); |
| 511 | if (!BestReg || Cost < BestCost) { |
| 512 | BestReg = PhysReg; |
| 513 | BestCost = Cost; |
| 514 | if (Cost < spillCost) break; |
| 515 | } |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | if (BestReg) { |
| 520 | // BestCost is 0 when all aliases are already disabled. |
| 521 | if (BestCost) { |
| 522 | if (PhysRegState[BestReg] != regDisabled) |
| 523 | spillVirtReg(MBB, MI, PhysRegState[BestReg], true); |
| 524 | else { |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 525 | // Make sure all aliases are disabled. |
| 526 | for (const unsigned *AS = TRI->getAliasSet(BestReg); |
| 527 | unsigned Alias = *AS; ++AS) { |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 528 | switch (PhysRegState[Alias]) { |
| 529 | case regDisabled: |
| 530 | continue; |
| 531 | case regFree: |
| 532 | PhysRegState[Alias] = regDisabled; |
| 533 | break; |
| 534 | default: |
| 535 | spillVirtReg(MBB, MI, PhysRegState[Alias], true); |
| 536 | PhysRegState[Alias] = regDisabled; |
| 537 | break; |
| 538 | } |
| 539 | } |
| 540 | } |
| 541 | } |
Jakob Stoklund Olesen | 76b4d5a | 2010-05-11 23:24:45 +0000 | [diff] [blame] | 542 | return assignVirtToPhysReg(VirtReg, BestReg); |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | // Nothing we can do. |
| 546 | std::string msg; |
| 547 | raw_string_ostream Msg(msg); |
| 548 | Msg << "Ran out of registers during register allocation!"; |
| 549 | if (MI->isInlineAsm()) { |
| 550 | Msg << "\nPlease check your inline asm statement for " |
| 551 | << "invalid constraints:\n"; |
| 552 | MI->print(Msg, TM); |
| 553 | } |
| 554 | report_fatal_error(Msg.str()); |
Jakob Stoklund Olesen | 76b4d5a | 2010-05-11 23:24:45 +0000 | [diff] [blame] | 555 | return LiveVirtRegs.end(); |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 556 | } |
| 557 | |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 558 | /// defineVirtReg - Allocate a register for VirtReg and mark it as dirty. |
| 559 | unsigned RAFast::defineVirtReg(MachineBasicBlock &MBB, MachineInstr *MI, |
Jakob Stoklund Olesen | 4bf4baf | 2010-05-13 00:19:43 +0000 | [diff] [blame] | 560 | unsigned OpNum, unsigned VirtReg, unsigned Hint) { |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 561 | assert(TargetRegisterInfo::isVirtualRegister(VirtReg) && |
| 562 | "Not a virtual register"); |
Jakob Stoklund Olesen | 1a1ad57 | 2010-05-12 00:11:19 +0000 | [diff] [blame] | 563 | LiveRegMap::iterator lri = LiveVirtRegs.find(VirtReg); |
| 564 | if (lri == LiveVirtRegs.end()) |
Jakob Stoklund Olesen | 4bf4baf | 2010-05-13 00:19:43 +0000 | [diff] [blame] | 565 | lri = allocVirtReg(MBB, MI, VirtReg, Hint); |
Jakob Stoklund Olesen | 804291e | 2010-05-12 18:46:03 +0000 | [diff] [blame] | 566 | else |
| 567 | addKillFlag(lri); // Kill before redefine. |
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 | LR.Dirty = true; |
| 572 | UsedInInstr.set(LR.PhysReg); |
| 573 | return LR.PhysReg; |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 574 | } |
| 575 | |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 576 | /// reloadVirtReg - Make sure VirtReg is available in a physreg and return it. |
| 577 | unsigned RAFast::reloadVirtReg(MachineBasicBlock &MBB, MachineInstr *MI, |
Jakob Stoklund Olesen | 4bf4baf | 2010-05-13 00:19:43 +0000 | [diff] [blame] | 578 | unsigned OpNum, unsigned VirtReg, unsigned Hint) { |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 579 | assert(TargetRegisterInfo::isVirtualRegister(VirtReg) && |
| 580 | "Not a virtual register"); |
Jakob Stoklund Olesen | 1a1ad57 | 2010-05-12 00:11:19 +0000 | [diff] [blame] | 581 | LiveRegMap::iterator lri = LiveVirtRegs.find(VirtReg); |
| 582 | if (lri == LiveVirtRegs.end()) { |
Jakob Stoklund Olesen | 4bf4baf | 2010-05-13 00:19:43 +0000 | [diff] [blame] | 583 | lri = allocVirtReg(MBB, MI, VirtReg, Hint); |
| 584 | const TargetRegisterClass *RC = MRI->getRegClass(VirtReg); |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 585 | int FrameIndex = getStackSpaceFor(VirtReg, RC); |
Jakob Stoklund Olesen | c9c4dac | 2010-05-13 20:43:17 +0000 | [diff] [blame] | 586 | DEBUG(dbgs() << "Reloading %reg" << VirtReg << " into " |
Jakob Stoklund Olesen | 1a1ad57 | 2010-05-12 00:11:19 +0000 | [diff] [blame] | 587 | << TRI->getName(lri->second.PhysReg) << "\n"); |
| 588 | TII->loadRegFromStackSlot(MBB, MI, lri->second.PhysReg, FrameIndex, RC, |
| 589 | TRI); |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 590 | ++NumLoads; |
Jakob Stoklund Olesen | 1e03ff4 | 2010-05-15 06:09:08 +0000 | [diff] [blame] | 591 | } else if (lri->second.Dirty) { |
| 592 | MachineOperand &MO = MI->getOperand(OpNum); |
| 593 | if (isLastUseOfLocalReg(MO)) { |
| 594 | DEBUG(dbgs() << "Killing last use: " << MO << "\n"); |
| 595 | MO.setIsKill(); |
| 596 | } else if (MO.isKill()) { |
| 597 | DEBUG(dbgs() << "Clearing dubious kill: " << MO << "\n"); |
| 598 | MO.setIsKill(false); |
| 599 | } |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 600 | } |
Jakob Stoklund Olesen | 1a1ad57 | 2010-05-12 00:11:19 +0000 | [diff] [blame] | 601 | LiveReg &LR = lri->second; |
Jakob Stoklund Olesen | 210e2af | 2010-05-11 23:24:47 +0000 | [diff] [blame] | 602 | LR.LastUse = MI; |
| 603 | LR.LastOpNum = OpNum; |
| 604 | UsedInInstr.set(LR.PhysReg); |
| 605 | return LR.PhysReg; |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 606 | } |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 607 | |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 608 | // setPhysReg - Change MO the refer the PhysReg, considering subregs. |
| 609 | void RAFast::setPhysReg(MachineOperand &MO, unsigned PhysReg) { |
| 610 | if (unsigned Idx = MO.getSubReg()) { |
| 611 | MO.setReg(PhysReg ? TRI->getSubReg(PhysReg, Idx) : 0); |
| 612 | MO.setSubReg(0); |
| 613 | } else |
| 614 | MO.setReg(PhysReg); |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 615 | } |
| 616 | |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 617 | void RAFast::AllocateBasicBlock(MachineBasicBlock &MBB) { |
Jakob Stoklund Olesen | c9c4dac | 2010-05-13 20:43:17 +0000 | [diff] [blame] | 618 | DEBUG(dbgs() << "\nAllocating " << MBB); |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 619 | |
Jakob Stoklund Olesen | 7d4f259 | 2010-05-14 00:02:20 +0000 | [diff] [blame] | 620 | atEndOfBlock = false; |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 621 | PhysRegState.assign(TRI->getNumRegs(), regDisabled); |
Jakob Stoklund Olesen | 76b4d5a | 2010-05-11 23:24:45 +0000 | [diff] [blame] | 622 | assert(LiveVirtRegs.empty() && "Mapping not cleared form last block?"); |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 623 | |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 624 | MachineBasicBlock::iterator MII = MBB.begin(); |
| 625 | |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 626 | // Add live-in registers as live. |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 627 | for (MachineBasicBlock::livein_iterator I = MBB.livein_begin(), |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 628 | E = MBB.livein_end(); I != E; ++I) |
Jakob Stoklund Olesen | 4ed1082 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 629 | definePhysReg(MBB, MII, *I, regReserved); |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 630 | |
Jakob Stoklund Olesen | 4ed1082 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 631 | SmallVector<unsigned, 8> VirtKills, PhysDefs; |
Jakob Stoklund Olesen | 7ff82e1 | 2010-05-14 04:30:51 +0000 | [diff] [blame] | 632 | SmallVector<MachineInstr*, 32> Coalesced; |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 633 | |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 634 | // Otherwise, sequentially allocate each instruction in the MBB. |
| 635 | while (MII != MBB.end()) { |
| 636 | MachineInstr *MI = MII++; |
| 637 | const TargetInstrDesc &TID = MI->getDesc(); |
| 638 | DEBUG({ |
Jakob Stoklund Olesen | c9c4dac | 2010-05-13 20:43:17 +0000 | [diff] [blame] | 639 | dbgs() << "\n>> " << *MI << "Regs:"; |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 640 | for (unsigned Reg = 1, E = TRI->getNumRegs(); Reg != E; ++Reg) { |
| 641 | if (PhysRegState[Reg] == regDisabled) continue; |
| 642 | dbgs() << " " << TRI->getName(Reg); |
| 643 | switch(PhysRegState[Reg]) { |
| 644 | case regFree: |
| 645 | break; |
| 646 | case regReserved: |
Jakob Stoklund Olesen | c9c4dac | 2010-05-13 20:43:17 +0000 | [diff] [blame] | 647 | dbgs() << "*"; |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 648 | break; |
| 649 | default: |
| 650 | dbgs() << "=%reg" << PhysRegState[Reg]; |
Jakob Stoklund Olesen | 210e2af | 2010-05-11 23:24:47 +0000 | [diff] [blame] | 651 | if (LiveVirtRegs[PhysRegState[Reg]].Dirty) |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 652 | dbgs() << "*"; |
Jakob Stoklund Olesen | 76b4d5a | 2010-05-11 23:24:45 +0000 | [diff] [blame] | 653 | assert(LiveVirtRegs[PhysRegState[Reg]].PhysReg == Reg && |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 654 | "Bad inverse map"); |
| 655 | break; |
| 656 | } |
| 657 | } |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 658 | dbgs() << '\n'; |
Jakob Stoklund Olesen | 76b4d5a | 2010-05-11 23:24:45 +0000 | [diff] [blame] | 659 | // Check that LiveVirtRegs is the inverse. |
| 660 | for (LiveRegMap::iterator i = LiveVirtRegs.begin(), |
| 661 | e = LiveVirtRegs.end(); i != e; ++i) { |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 662 | assert(TargetRegisterInfo::isVirtualRegister(i->first) && |
| 663 | "Bad map key"); |
Jakob Stoklund Olesen | 76b4d5a | 2010-05-11 23:24:45 +0000 | [diff] [blame] | 664 | assert(TargetRegisterInfo::isPhysicalRegister(i->second.PhysReg) && |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 665 | "Bad map value"); |
Jakob Stoklund Olesen | 76b4d5a | 2010-05-11 23:24:45 +0000 | [diff] [blame] | 666 | assert(PhysRegState[i->second.PhysReg] == i->first && |
| 667 | "Bad inverse map"); |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 668 | } |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 669 | }); |
| 670 | |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 671 | // Debug values are not allowed to change codegen in any way. |
| 672 | if (MI->isDebugValue()) { |
| 673 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 674 | MachineOperand &MO = MI->getOperand(i); |
| 675 | if (!MO.isReg()) continue; |
| 676 | unsigned Reg = MO.getReg(); |
| 677 | if (!Reg || TargetRegisterInfo::isPhysicalRegister(Reg)) continue; |
Jakob Stoklund Olesen | 1a1ad57 | 2010-05-12 00:11:19 +0000 | [diff] [blame] | 678 | LiveRegMap::iterator lri = LiveVirtRegs.find(Reg); |
| 679 | if (lri != LiveVirtRegs.end()) |
| 680 | setPhysReg(MO, lri->second.PhysReg); |
Jakob Stoklund Olesen | 76b4d5a | 2010-05-11 23:24:45 +0000 | [diff] [blame] | 681 | else |
| 682 | 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] | 683 | } |
| 684 | // Next instruction. |
| 685 | continue; |
| 686 | } |
| 687 | |
Jakob Stoklund Olesen | 4bf4baf | 2010-05-13 00:19:43 +0000 | [diff] [blame] | 688 | // If this is a copy, we may be able to coalesce. |
| 689 | unsigned CopySrc, CopyDst, CopySrcSub, CopyDstSub; |
| 690 | if (!TII->isMoveInstr(*MI, CopySrc, CopyDst, CopySrcSub, CopyDstSub)) |
| 691 | CopySrc = CopyDst = 0; |
| 692 | |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 693 | // Track registers used by instruction. |
| 694 | UsedInInstr.reset(); |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 695 | PhysDefs.clear(); |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 696 | |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 697 | // First scan. |
| 698 | // Mark physreg uses and early clobbers as used. |
Jakob Stoklund Olesen | e97dda4 | 2010-05-14 21:55:52 +0000 | [diff] [blame] | 699 | // Find the end of the virtreg operands |
| 700 | unsigned VirtOpEnd = 0; |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 701 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 702 | MachineOperand &MO = MI->getOperand(i); |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 703 | if (!MO.isReg()) continue; |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 704 | unsigned Reg = MO.getReg(); |
Jakob Stoklund Olesen | e97dda4 | 2010-05-14 21:55:52 +0000 | [diff] [blame] | 705 | if (!Reg) continue; |
| 706 | if (TargetRegisterInfo::isVirtualRegister(Reg)) { |
| 707 | VirtOpEnd = i+1; |
| 708 | continue; |
| 709 | } |
Jakob Stoklund Olesen | efa155f | 2010-05-14 22:02:56 +0000 | [diff] [blame] | 710 | if (!Allocatable.test(Reg)) continue; |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 711 | if (MO.isUse()) { |
Jakob Stoklund Olesen | 4ed1082 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 712 | usePhysReg(MO); |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 713 | } else if (MO.isEarlyClobber()) { |
Jakob Stoklund Olesen | 4ed1082 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 714 | definePhysReg(MBB, MI, Reg, MO.isDead() ? regFree : regReserved); |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 715 | PhysDefs.push_back(Reg); |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 716 | } |
| 717 | } |
| 718 | |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 719 | // Second scan. |
| 720 | // Allocate virtreg uses and early clobbers. |
| 721 | // Collect VirtKills |
Jakob Stoklund Olesen | e97dda4 | 2010-05-14 21:55:52 +0000 | [diff] [blame] | 722 | for (unsigned i = 0; i != VirtOpEnd; ++i) { |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 723 | MachineOperand &MO = MI->getOperand(i); |
| 724 | if (!MO.isReg()) continue; |
| 725 | unsigned Reg = MO.getReg(); |
| 726 | if (!Reg || TargetRegisterInfo::isPhysicalRegister(Reg)) continue; |
| 727 | if (MO.isUse()) { |
Jakob Stoklund Olesen | 4bf4baf | 2010-05-13 00:19:43 +0000 | [diff] [blame] | 728 | unsigned PhysReg = reloadVirtReg(MBB, MI, i, Reg, CopyDst); |
Jakob Stoklund Olesen | 7ff82e1 | 2010-05-14 04:30:51 +0000 | [diff] [blame] | 729 | CopySrc = (CopySrc == Reg || CopySrc == PhysReg) ? PhysReg : 0; |
Jakob Stoklund Olesen | 4bf4baf | 2010-05-13 00:19:43 +0000 | [diff] [blame] | 730 | setPhysReg(MO, PhysReg); |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 731 | if (MO.isKill()) |
| 732 | VirtKills.push_back(Reg); |
| 733 | } else if (MO.isEarlyClobber()) { |
Jakob Stoklund Olesen | 4bf4baf | 2010-05-13 00:19:43 +0000 | [diff] [blame] | 734 | unsigned PhysReg = defineVirtReg(MBB, MI, i, Reg, 0); |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 735 | setPhysReg(MO, PhysReg); |
| 736 | PhysDefs.push_back(PhysReg); |
| 737 | } |
| 738 | } |
| 739 | |
| 740 | // Process virtreg kills |
| 741 | for (unsigned i = 0, e = VirtKills.size(); i != e; ++i) |
| 742 | killVirtReg(VirtKills[i]); |
| 743 | VirtKills.clear(); |
| 744 | |
Jakob Stoklund Olesen | 4bf4baf | 2010-05-13 00:19:43 +0000 | [diff] [blame] | 745 | MRI->addPhysRegsUsed(UsedInInstr); |
Jakob Stoklund Olesen | 82b07dc | 2010-05-11 20:30:28 +0000 | [diff] [blame] | 746 | |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 747 | // Track registers defined by instruction - early clobbers at this point. |
| 748 | UsedInInstr.reset(); |
| 749 | for (unsigned i = 0, e = PhysDefs.size(); i != e; ++i) { |
| 750 | unsigned PhysReg = PhysDefs[i]; |
| 751 | UsedInInstr.set(PhysReg); |
| 752 | for (const unsigned *AS = TRI->getAliasSet(PhysReg); |
| 753 | unsigned Alias = *AS; ++AS) |
| 754 | UsedInInstr.set(Alias); |
| 755 | } |
| 756 | |
| 757 | // Third scan. |
| 758 | // Allocate defs and collect dead defs. |
| 759 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 760 | MachineOperand &MO = MI->getOperand(i); |
| 761 | if (!MO.isReg() || !MO.isDef() || !MO.getReg()) continue; |
| 762 | unsigned Reg = MO.getReg(); |
| 763 | |
| 764 | if (TargetRegisterInfo::isPhysicalRegister(Reg)) { |
Jakob Stoklund Olesen | efa155f | 2010-05-14 22:02:56 +0000 | [diff] [blame] | 765 | if (!Allocatable.test(Reg)) continue; |
Jakob Stoklund Olesen | 4ed1082 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 766 | definePhysReg(MBB, MI, Reg, (MO.isImplicit() || MO.isDead()) ? |
| 767 | regFree : regReserved); |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 768 | continue; |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 769 | } |
Jakob Stoklund Olesen | 4bf4baf | 2010-05-13 00:19:43 +0000 | [diff] [blame] | 770 | unsigned PhysReg = defineVirtReg(MBB, MI, i, Reg, CopySrc); |
Jakob Stoklund Olesen | 7ff82e1 | 2010-05-14 04:30:51 +0000 | [diff] [blame] | 771 | if (MO.isDead()) { |
| 772 | VirtKills.push_back(Reg); |
| 773 | CopyDst = 0; // cancel coalescing; |
| 774 | } else |
| 775 | CopyDst = (CopyDst == Reg || CopyDst == PhysReg) ? PhysReg : 0; |
Jakob Stoklund Olesen | 4bf4baf | 2010-05-13 00:19:43 +0000 | [diff] [blame] | 776 | setPhysReg(MO, PhysReg); |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 777 | } |
| 778 | |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 779 | // Spill all dirty virtregs before a call, in case of an exception. |
| 780 | if (TID.isCall()) { |
| 781 | DEBUG(dbgs() << " Spilling remaining registers before call.\n"); |
| 782 | spillAll(MBB, MI); |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 783 | } |
| 784 | |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 785 | // Process virtreg deads. |
| 786 | for (unsigned i = 0, e = VirtKills.size(); i != e; ++i) |
| 787 | killVirtReg(VirtKills[i]); |
| 788 | VirtKills.clear(); |
| 789 | |
Jakob Stoklund Olesen | 4bf4baf | 2010-05-13 00:19:43 +0000 | [diff] [blame] | 790 | MRI->addPhysRegsUsed(UsedInInstr); |
Jakob Stoklund Olesen | c9c4dac | 2010-05-13 20:43:17 +0000 | [diff] [blame] | 791 | |
Jakob Stoklund Olesen | 7ff82e1 | 2010-05-14 04:30:51 +0000 | [diff] [blame] | 792 | if (CopyDst && CopyDst == CopySrc && CopyDstSub == CopySrcSub) { |
| 793 | DEBUG(dbgs() << "-- coalescing: " << *MI); |
| 794 | Coalesced.push_back(MI); |
| 795 | } else { |
| 796 | DEBUG(dbgs() << "<< " << *MI); |
| 797 | } |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 798 | } |
| 799 | |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 800 | // Spill all physical registers holding virtual registers now. |
Jakob Stoklund Olesen | 7d4f259 | 2010-05-14 00:02:20 +0000 | [diff] [blame] | 801 | atEndOfBlock = true; |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 802 | MachineBasicBlock::iterator MI = MBB.getFirstTerminator(); |
Jakob Stoklund Olesen | 6a6328b | 2010-05-14 22:40:43 +0000 | [diff] [blame] | 803 | if (MI != MBB.end() && MI->getDesc().isReturn()) { |
| 804 | // This is a return block, kill all virtual registers. |
| 805 | DEBUG(dbgs() << "Killing live registers at end of return block.\n"); |
| 806 | for (LiveRegMap::iterator i = LiveVirtRegs.begin(), e = LiveVirtRegs.end(); |
| 807 | i != e; ++i) |
| 808 | killVirtReg(i); |
| 809 | } else { |
| 810 | // This is a normal block, spill any dirty virtregs. |
| 811 | DEBUG(dbgs() << "Spilling live registers at end of block.\n"); |
| 812 | for (LiveRegMap::iterator i = LiveVirtRegs.begin(), e = LiveVirtRegs.end(); |
| 813 | i != e; ++i) |
| 814 | spillVirtReg(MBB, MI, i, true); |
| 815 | } |
Jakob Stoklund Olesen | 7d4f259 | 2010-05-14 00:02:20 +0000 | [diff] [blame] | 816 | LiveVirtRegs.clear(); |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 817 | |
Jakob Stoklund Olesen | 7ff82e1 | 2010-05-14 04:30:51 +0000 | [diff] [blame] | 818 | // Erase all the coalesced copies. We are delaying it until now because |
| 819 | // LiveVirtsRegs might refer to the instrs. |
| 820 | for (unsigned i = 0, e = Coalesced.size(); i != e; ++i) |
| 821 | MBB.erase(Coalesced[i]); |
Jakob Stoklund Olesen | 8a65c51 | 2010-05-14 21:55:50 +0000 | [diff] [blame] | 822 | NumCopies += Coalesced.size(); |
Jakob Stoklund Olesen | 7ff82e1 | 2010-05-14 04:30:51 +0000 | [diff] [blame] | 823 | |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 824 | DEBUG(MBB.dump()); |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 825 | } |
| 826 | |
| 827 | /// runOnMachineFunction - Register allocate the whole function |
| 828 | /// |
| 829 | bool RAFast::runOnMachineFunction(MachineFunction &Fn) { |
Jakob Stoklund Olesen | c9c4dac | 2010-05-13 20:43:17 +0000 | [diff] [blame] | 830 | DEBUG(dbgs() << "********** FAST REGISTER ALLOCATION **********\n" |
| 831 | << "********** Function: " |
| 832 | << ((Value*)Fn.getFunction())->getName() << '\n'); |
Jakob Stoklund Olesen | 1b2c761 | 2010-05-14 20:28:32 +0000 | [diff] [blame] | 833 | if (VerifyFastRegalloc) |
Jakob Stoklund Olesen | a0e618d | 2010-05-14 21:55:44 +0000 | [diff] [blame] | 834 | Fn.verify(this, true); |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 835 | MF = &Fn; |
Jakob Stoklund Olesen | 4bf4baf | 2010-05-13 00:19:43 +0000 | [diff] [blame] | 836 | MRI = &MF->getRegInfo(); |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 837 | TM = &Fn.getTarget(); |
| 838 | TRI = TM->getRegisterInfo(); |
| 839 | TII = TM->getInstrInfo(); |
| 840 | |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 841 | UsedInInstr.resize(TRI->getNumRegs()); |
Jakob Stoklund Olesen | efa155f | 2010-05-14 22:02:56 +0000 | [diff] [blame] | 842 | Allocatable = TRI->getAllocatableSet(*MF); |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 843 | |
| 844 | // initialize the virtual->physical register map to have a 'null' |
| 845 | // mapping for all virtual registers |
Jakob Stoklund Olesen | 4bf4baf | 2010-05-13 00:19:43 +0000 | [diff] [blame] | 846 | unsigned LastVirtReg = MRI->getLastVirtReg(); |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 847 | StackSlotForVirtReg.grow(LastVirtReg); |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 848 | |
| 849 | // Loop over all of the basic blocks, eliminating virtual register references |
| 850 | for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end(); |
| 851 | MBB != MBBe; ++MBB) |
| 852 | AllocateBasicBlock(*MBB); |
| 853 | |
Jakob Stoklund Olesen | 82b07dc | 2010-05-11 20:30:28 +0000 | [diff] [blame] | 854 | // 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] | 855 | MRI->closePhysRegsUsed(*TRI); |
Jakob Stoklund Olesen | 82b07dc | 2010-05-11 20:30:28 +0000 | [diff] [blame] | 856 | |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 857 | StackSlotForVirtReg.clear(); |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 858 | return true; |
| 859 | } |
| 860 | |
| 861 | FunctionPass *llvm::createFastRegisterAllocator() { |
| 862 | return new RAFast(); |
| 863 | } |