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