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" |
Devang Patel | 459a36b | 2010-08-04 18:42:02 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 21 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 22 | #include "llvm/CodeGen/Passes.h" |
| 23 | #include "llvm/CodeGen/RegAllocRegistry.h" |
Andrew Trick | 1525260 | 2012-06-06 20:29:31 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/RegisterClassInfo.h" |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 25 | #include "llvm/Target/TargetInstrInfo.h" |
| 26 | #include "llvm/Target/TargetMachine.h" |
| 27 | #include "llvm/Support/CommandLine.h" |
| 28 | #include "llvm/Support/Debug.h" |
| 29 | #include "llvm/Support/ErrorHandling.h" |
| 30 | #include "llvm/Support/raw_ostream.h" |
| 31 | #include "llvm/ADT/DenseMap.h" |
| 32 | #include "llvm/ADT/IndexedMap.h" |
| 33 | #include "llvm/ADT/SmallSet.h" |
| 34 | #include "llvm/ADT/SmallVector.h" |
Jakob Stoklund Olesen | a240743 | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 35 | #include "llvm/ADT/SparseSet.h" |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 36 | #include "llvm/ADT/Statistic.h" |
| 37 | #include "llvm/ADT/STLExtras.h" |
| 38 | #include <algorithm> |
| 39 | using namespace llvm; |
| 40 | |
| 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; |
Owen Anderson | 90c579d | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 52 | RAFast() : MachineFunctionPass(ID), StackSlotForVirtReg(-1), |
Andrew Trick | 8dd2625 | 2012-02-10 04:10:36 +0000 | [diff] [blame] | 53 | isBulkSpilling(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; |
Jakob Stoklund Olesen | 5d20c31 | 2011-06-02 18:35:30 +0000 | [diff] [blame] | 60 | RegisterClassInfo RegClassInfo; |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 61 | |
Jakob Stoklund Olesen | 6fb69d8 | 2010-05-17 02:07:22 +0000 | [diff] [blame] | 62 | // Basic block currently being allocated. |
| 63 | MachineBasicBlock *MBB; |
| 64 | |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 65 | // StackSlotForVirtReg - Maps virtual regs to the frame index where these |
| 66 | // values are spilled. |
| 67 | IndexedMap<int, VirtReg2IndexFunctor> StackSlotForVirtReg; |
| 68 | |
Jakob Stoklund Olesen | 76b4d5a | 2010-05-11 23:24:45 +0000 | [diff] [blame] | 69 | // Everything we know about a live virtual register. |
| 70 | struct LiveReg { |
Jakob Stoklund Olesen | 210e2af | 2010-05-11 23:24:47 +0000 | [diff] [blame] | 71 | MachineInstr *LastUse; // Last instr to use reg. |
Jakob Stoklund Olesen | a240743 | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 72 | unsigned VirtReg; // Virtual register number. |
Jakob Stoklund Olesen | 210e2af | 2010-05-11 23:24:47 +0000 | [diff] [blame] | 73 | unsigned PhysReg; // Currently held here. |
| 74 | unsigned short LastOpNum; // OpNum on LastUse. |
| 75 | bool Dirty; // Register needs spill. |
Jakob Stoklund Olesen | 76b4d5a | 2010-05-11 23:24:45 +0000 | [diff] [blame] | 76 | |
Jakob Stoklund Olesen | a240743 | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 77 | explicit LiveReg(unsigned v) |
| 78 | : LastUse(0), VirtReg(v), PhysReg(0), LastOpNum(0), Dirty(false) {} |
| 79 | |
Andrew Trick | c0ccb8b | 2012-04-20 20:05:28 +0000 | [diff] [blame] | 80 | unsigned getSparseSetIndex() const { |
Jakob Stoklund Olesen | a240743 | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 81 | return TargetRegisterInfo::virtReg2Index(VirtReg); |
| 82 | } |
Jakob Stoklund Olesen | 76b4d5a | 2010-05-11 23:24:45 +0000 | [diff] [blame] | 83 | }; |
| 84 | |
Jakob Stoklund Olesen | a240743 | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 85 | typedef SparseSet<LiveReg> LiveRegMap; |
Jakob Stoklund Olesen | 76b4d5a | 2010-05-11 23:24:45 +0000 | [diff] [blame] | 86 | |
| 87 | // LiveVirtRegs - This map contains entries for each virtual register |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 88 | // that is currently available in a physical register. |
Jakob Stoklund Olesen | 76b4d5a | 2010-05-11 23:24:45 +0000 | [diff] [blame] | 89 | LiveRegMap LiveVirtRegs; |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 90 | |
Devang Patel | 72d9b0e | 2011-06-21 22:36:03 +0000 | [diff] [blame] | 91 | DenseMap<unsigned, SmallVector<MachineInstr *, 4> > LiveDbgValueMap; |
Devang Patel | 459a36b | 2010-08-04 18:42:02 +0000 | [diff] [blame] | 92 | |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 93 | // RegState - Track the state of a physical register. |
| 94 | enum RegState { |
| 95 | // A disabled register is not available for allocation, but an alias may |
| 96 | // be in use. A register can only be moved out of the disabled state if |
| 97 | // all aliases are disabled. |
| 98 | regDisabled, |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 99 | |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 100 | // A free register is not currently in use and can be allocated |
| 101 | // immediately without checking aliases. |
| 102 | regFree, |
| 103 | |
Evan Cheng | d8a1624 | 2011-04-22 01:40:20 +0000 | [diff] [blame] | 104 | // A reserved register has been assigned explicitly (e.g., setting up a |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 105 | // call parameter), and it remains reserved until it is used. |
| 106 | regReserved |
| 107 | |
| 108 | // A register state may also be a virtual register number, indication that |
| 109 | // the physical register is currently allocated to a virtual register. In |
Jakob Stoklund Olesen | 76b4d5a | 2010-05-11 23:24:45 +0000 | [diff] [blame] | 110 | // that case, LiveVirtRegs contains the inverse mapping. |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | // PhysRegState - One of the RegState enums, or a virtreg. |
| 114 | std::vector<unsigned> PhysRegState; |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 115 | |
Jakob Stoklund Olesen | d7ea7d5 | 2012-10-17 01:37:59 +0000 | [diff] [blame] | 116 | typedef SparseSet<unsigned> UsedInInstrSet; |
| 117 | |
| 118 | // UsedInInstr - Set of physregs that are used in the current instruction, |
| 119 | // and so cannot be allocated. |
| 120 | UsedInInstrSet UsedInInstr; |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 121 | |
Jim Grosbach | 07cb689 | 2010-09-01 19:16:29 +0000 | [diff] [blame] | 122 | // SkippedInstrs - Descriptors of instructions whose clobber list was |
| 123 | // ignored because all registers were spilled. It is still necessary to |
| 124 | // mark all the clobbered registers as used by the function. |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 125 | SmallPtrSet<const MCInstrDesc*, 4> SkippedInstrs; |
Jakob Stoklund Olesen | 6de0717 | 2010-06-04 18:08:29 +0000 | [diff] [blame] | 126 | |
Jakob Stoklund Olesen | e6aba83 | 2010-05-17 02:07:32 +0000 | [diff] [blame] | 127 | // isBulkSpilling - This flag is set when LiveRegMap will be cleared |
| 128 | // completely after spilling all live registers. LiveRegMap entries should |
| 129 | // not be erased. |
| 130 | bool isBulkSpilling; |
Jakob Stoklund Olesen | 7d4f259 | 2010-05-14 00:02:20 +0000 | [diff] [blame] | 131 | |
Jakob Stoklund Olesen | 548643c | 2010-05-17 15:30:32 +0000 | [diff] [blame] | 132 | enum { |
| 133 | spillClean = 1, |
| 134 | spillDirty = 100, |
| 135 | spillImpossible = ~0u |
| 136 | }; |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 137 | public: |
| 138 | virtual const char *getPassName() const { |
| 139 | return "Fast Register Allocator"; |
| 140 | } |
| 141 | |
| 142 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 143 | AU.setPreservesCFG(); |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 144 | MachineFunctionPass::getAnalysisUsage(AU); |
| 145 | } |
| 146 | |
| 147 | private: |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 148 | bool runOnMachineFunction(MachineFunction &Fn); |
Jakob Stoklund Olesen | 6fb69d8 | 2010-05-17 02:07:22 +0000 | [diff] [blame] | 149 | void AllocateBasicBlock(); |
Jakob Stoklund Olesen | d843b39 | 2010-06-28 18:34:34 +0000 | [diff] [blame] | 150 | void handleThroughOperands(MachineInstr *MI, |
| 151 | SmallVectorImpl<unsigned> &VirtDead); |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 152 | int getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC); |
Jakob Stoklund Olesen | 1e03ff4 | 2010-05-15 06:09:08 +0000 | [diff] [blame] | 153 | bool isLastUseOfLocalReg(MachineOperand&); |
| 154 | |
Jakob Stoklund Olesen | 01dcbf8 | 2010-05-17 02:07:29 +0000 | [diff] [blame] | 155 | void addKillFlag(const LiveReg&); |
Jakob Stoklund Olesen | 844db9c | 2010-05-17 02:49:15 +0000 | [diff] [blame] | 156 | void killVirtReg(LiveRegMap::iterator); |
Jakob Stoklund Olesen | 804291e | 2010-05-12 18:46:03 +0000 | [diff] [blame] | 157 | void killVirtReg(unsigned VirtReg); |
Jakob Stoklund Olesen | 844db9c | 2010-05-17 02:49:15 +0000 | [diff] [blame] | 158 | void spillVirtReg(MachineBasicBlock::iterator MI, LiveRegMap::iterator); |
Jakob Stoklund Olesen | e6aba83 | 2010-05-17 02:07:32 +0000 | [diff] [blame] | 159 | void spillVirtReg(MachineBasicBlock::iterator MI, unsigned VirtReg); |
Jakob Stoklund Olesen | 4ed1082 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 160 | |
| 161 | void usePhysReg(MachineOperand&); |
Jakob Stoklund Olesen | 6fb69d8 | 2010-05-17 02:07:22 +0000 | [diff] [blame] | 162 | void definePhysReg(MachineInstr *MI, unsigned PhysReg, RegState NewState); |
Jakob Stoklund Olesen | 548643c | 2010-05-17 15:30:32 +0000 | [diff] [blame] | 163 | unsigned calcSpillCost(unsigned PhysReg) const; |
Jakob Stoklund Olesen | a240743 | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 164 | void assignVirtToPhysReg(LiveReg&, unsigned PhysReg); |
| 165 | LiveRegMap::iterator findLiveVirtReg(unsigned VirtReg) { |
| 166 | return LiveVirtRegs.find(TargetRegisterInfo::virtReg2Index(VirtReg)); |
| 167 | } |
| 168 | LiveRegMap::const_iterator findLiveVirtReg(unsigned VirtReg) const { |
| 169 | return LiveVirtRegs.find(TargetRegisterInfo::virtReg2Index(VirtReg)); |
| 170 | } |
| 171 | LiveRegMap::iterator assignVirtToPhysReg(unsigned VReg, unsigned PhysReg); |
| 172 | LiveRegMap::iterator allocVirtReg(MachineInstr *MI, LiveRegMap::iterator, |
| 173 | unsigned Hint); |
Jakob Stoklund Olesen | 646dd7c | 2010-05-17 03:26:09 +0000 | [diff] [blame] | 174 | LiveRegMap::iterator defineVirtReg(MachineInstr *MI, unsigned OpNum, |
| 175 | unsigned VirtReg, unsigned Hint); |
| 176 | LiveRegMap::iterator reloadVirtReg(MachineInstr *MI, unsigned OpNum, |
| 177 | unsigned VirtReg, unsigned Hint); |
Akira Hatanaka | bab2421 | 2012-10-31 00:56:01 +0000 | [diff] [blame^] | 178 | void spillAll(MachineBasicBlock::iterator MI); |
Jakob Stoklund Olesen | 0eeb05c | 2010-05-18 21:10:50 +0000 | [diff] [blame] | 179 | bool setPhysReg(MachineInstr *MI, unsigned OpNum, unsigned PhysReg); |
Andrew Trick | b3d5847 | 2012-01-31 05:55:32 +0000 | [diff] [blame] | 180 | void addRetOperands(MachineBasicBlock *MBB); |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 181 | }; |
| 182 | char RAFast::ID = 0; |
| 183 | } |
| 184 | |
| 185 | /// getStackSpaceFor - This allocates space for the specified virtual register |
| 186 | /// to be held on the stack. |
| 187 | int RAFast::getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC) { |
| 188 | // Find the location Reg would belong... |
| 189 | int SS = StackSlotForVirtReg[VirtReg]; |
| 190 | if (SS != -1) |
| 191 | return SS; // Already has space allocated? |
| 192 | |
| 193 | // Allocate a new stack object for this spill location... |
| 194 | int FrameIdx = MF->getFrameInfo()->CreateSpillStackObject(RC->getSize(), |
| 195 | RC->getAlignment()); |
| 196 | |
| 197 | // Assign the slot. |
| 198 | StackSlotForVirtReg[VirtReg] = FrameIdx; |
| 199 | return FrameIdx; |
| 200 | } |
| 201 | |
Jakob Stoklund Olesen | 1e03ff4 | 2010-05-15 06:09:08 +0000 | [diff] [blame] | 202 | /// isLastUseOfLocalReg - Return true if MO is the only remaining reference to |
| 203 | /// its virtual register, and it is guaranteed to be a block-local register. |
| 204 | /// |
| 205 | bool RAFast::isLastUseOfLocalReg(MachineOperand &MO) { |
Jakob Stoklund Olesen | 1e03ff4 | 2010-05-15 06:09:08 +0000 | [diff] [blame] | 206 | // If the register has ever been spilled or reloaded, we conservatively assume |
| 207 | // it is a global register used in multiple blocks. |
| 208 | if (StackSlotForVirtReg[MO.getReg()] != -1) |
| 209 | return false; |
| 210 | |
| 211 | // Check that the use/def chain has exactly one operand - MO. |
Jakob Stoklund Olesen | 4e69662 | 2012-08-08 23:44:01 +0000 | [diff] [blame] | 212 | MachineRegisterInfo::reg_nodbg_iterator I = MRI->reg_nodbg_begin(MO.getReg()); |
| 213 | if (&I.getOperand() != &MO) |
| 214 | return false; |
| 215 | return ++I == MRI->reg_nodbg_end(); |
Jakob Stoklund Olesen | 1e03ff4 | 2010-05-15 06:09:08 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Jakob Stoklund Olesen | 804291e | 2010-05-12 18:46:03 +0000 | [diff] [blame] | 218 | /// addKillFlag - Set kill flags on last use of a virtual register. |
Jakob Stoklund Olesen | 01dcbf8 | 2010-05-17 02:07:29 +0000 | [diff] [blame] | 219 | void RAFast::addKillFlag(const LiveReg &LR) { |
| 220 | if (!LR.LastUse) return; |
| 221 | MachineOperand &MO = LR.LastUse->getOperand(LR.LastOpNum); |
Jakob Stoklund Olesen | d32e735 | 2010-05-19 21:36:05 +0000 | [diff] [blame] | 222 | if (MO.isUse() && !LR.LastUse->isRegTiedToDefOperand(LR.LastOpNum)) { |
| 223 | if (MO.getReg() == LR.PhysReg) |
Jakob Stoklund Olesen | 0eeb05c | 2010-05-18 21:10:50 +0000 | [diff] [blame] | 224 | MO.setIsKill(); |
Jakob Stoklund Olesen | 0eeb05c | 2010-05-18 21:10:50 +0000 | [diff] [blame] | 225 | else |
| 226 | LR.LastUse->addRegisterKilled(LR.PhysReg, TRI, true); |
| 227 | } |
Jakob Stoklund Olesen | 804291e | 2010-05-12 18:46:03 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | /// killVirtReg - Mark virtreg as no longer available. |
Jakob Stoklund Olesen | 844db9c | 2010-05-17 02:49:15 +0000 | [diff] [blame] | 231 | void RAFast::killVirtReg(LiveRegMap::iterator LRI) { |
Jakob Stoklund Olesen | a240743 | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 232 | addKillFlag(*LRI); |
Jakob Stoklund Olesen | 91ba63d | 2012-02-22 16:50:46 +0000 | [diff] [blame] | 233 | assert(PhysRegState[LRI->PhysReg] == LRI->VirtReg && |
| 234 | "Broken RegState mapping"); |
Jakob Stoklund Olesen | a240743 | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 235 | PhysRegState[LRI->PhysReg] = regFree; |
Jakob Stoklund Olesen | e6aba83 | 2010-05-17 02:07:32 +0000 | [diff] [blame] | 236 | // Erase from LiveVirtRegs unless we're spilling in bulk. |
| 237 | if (!isBulkSpilling) |
Jakob Stoklund Olesen | 844db9c | 2010-05-17 02:49:15 +0000 | [diff] [blame] | 238 | LiveVirtRegs.erase(LRI); |
Jakob Stoklund Olesen | 76b4d5a | 2010-05-11 23:24:45 +0000 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | /// killVirtReg - Mark virtreg as no longer available. |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 242 | void RAFast::killVirtReg(unsigned VirtReg) { |
| 243 | assert(TargetRegisterInfo::isVirtualRegister(VirtReg) && |
| 244 | "killVirtReg needs a virtual register"); |
Jakob Stoklund Olesen | a240743 | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 245 | LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg); |
Jakob Stoklund Olesen | 844db9c | 2010-05-17 02:49:15 +0000 | [diff] [blame] | 246 | if (LRI != LiveVirtRegs.end()) |
| 247 | killVirtReg(LRI); |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 248 | } |
| 249 | |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 250 | /// spillVirtReg - This method spills the value specified by VirtReg into the |
Eli Friedman | 24a1182 | 2010-08-21 20:19:51 +0000 | [diff] [blame] | 251 | /// corresponding stack slot if needed. |
Jakob Stoklund Olesen | e6aba83 | 2010-05-17 02:07:32 +0000 | [diff] [blame] | 252 | void RAFast::spillVirtReg(MachineBasicBlock::iterator MI, unsigned VirtReg) { |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 253 | assert(TargetRegisterInfo::isVirtualRegister(VirtReg) && |
| 254 | "Spilling a physical register is illegal!"); |
Jakob Stoklund Olesen | a240743 | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 255 | LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg); |
Jakob Stoklund Olesen | 844db9c | 2010-05-17 02:49:15 +0000 | [diff] [blame] | 256 | assert(LRI != LiveVirtRegs.end() && "Spilling unmapped virtual register"); |
| 257 | spillVirtReg(MI, LRI); |
Jakob Stoklund Olesen | 7d4f259 | 2010-05-14 00:02:20 +0000 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | /// spillVirtReg - Do the actual work of spilling. |
Jakob Stoklund Olesen | 6fb69d8 | 2010-05-17 02:07:22 +0000 | [diff] [blame] | 261 | void RAFast::spillVirtReg(MachineBasicBlock::iterator MI, |
Jakob Stoklund Olesen | 844db9c | 2010-05-17 02:49:15 +0000 | [diff] [blame] | 262 | LiveRegMap::iterator LRI) { |
Jakob Stoklund Olesen | a240743 | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 263 | LiveReg &LR = *LRI; |
| 264 | assert(PhysRegState[LR.PhysReg] == LRI->VirtReg && "Broken RegState mapping"); |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 265 | |
Jakob Stoklund Olesen | 210e2af | 2010-05-11 23:24:47 +0000 | [diff] [blame] | 266 | if (LR.Dirty) { |
Jakob Stoklund Olesen | e6aba83 | 2010-05-17 02:07:32 +0000 | [diff] [blame] | 267 | // If this physreg is used by the instruction, we want to kill it on the |
| 268 | // instruction, not on the spill. |
Jakob Stoklund Olesen | 844db9c | 2010-05-17 02:49:15 +0000 | [diff] [blame] | 269 | bool SpillKill = LR.LastUse != MI; |
Jakob Stoklund Olesen | 210e2af | 2010-05-11 23:24:47 +0000 | [diff] [blame] | 270 | LR.Dirty = false; |
Jakob Stoklund Olesen | a240743 | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 271 | DEBUG(dbgs() << "Spilling " << PrintReg(LRI->VirtReg, TRI) |
Jakob Stoklund Olesen | 4314268 | 2011-01-09 03:05:53 +0000 | [diff] [blame] | 272 | << " in " << PrintReg(LR.PhysReg, TRI)); |
Jakob Stoklund Olesen | a240743 | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 273 | const TargetRegisterClass *RC = MRI->getRegClass(LRI->VirtReg); |
| 274 | int FI = getStackSpaceFor(LRI->VirtReg, RC); |
Jakob Stoklund Olesen | 6fb69d8 | 2010-05-17 02:07:22 +0000 | [diff] [blame] | 275 | DEBUG(dbgs() << " to stack slot #" << FI << "\n"); |
Jakob Stoklund Olesen | 844db9c | 2010-05-17 02:49:15 +0000 | [diff] [blame] | 276 | TII->storeRegToStackSlot(*MBB, MI, LR.PhysReg, SpillKill, FI, RC, TRI); |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 277 | ++NumStores; // Update statistics |
Jakob Stoklund Olesen | 76b4d5a | 2010-05-11 23:24:45 +0000 | [diff] [blame] | 278 | |
Jim Grosbach | 07cb689 | 2010-09-01 19:16:29 +0000 | [diff] [blame] | 279 | // If this register is used by DBG_VALUE then insert new DBG_VALUE to |
Devang Patel | 459a36b | 2010-08-04 18:42:02 +0000 | [diff] [blame] | 280 | // identify spilled location as the place to find corresponding variable's |
| 281 | // value. |
Jakob Stoklund Olesen | a240743 | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 282 | SmallVector<MachineInstr *, 4> &LRIDbgValues = |
| 283 | LiveDbgValueMap[LRI->VirtReg]; |
Devang Patel | 72d9b0e | 2011-06-21 22:36:03 +0000 | [diff] [blame] | 284 | for (unsigned li = 0, le = LRIDbgValues.size(); li != le; ++li) { |
| 285 | MachineInstr *DBG = LRIDbgValues[li]; |
Jim Grosbach | 07cb689 | 2010-09-01 19:16:29 +0000 | [diff] [blame] | 286 | const MDNode *MDPtr = |
Devang Patel | 459a36b | 2010-08-04 18:42:02 +0000 | [diff] [blame] | 287 | DBG->getOperand(DBG->getNumOperands()-1).getMetadata(); |
| 288 | int64_t Offset = 0; |
| 289 | if (DBG->getOperand(1).isImm()) |
| 290 | Offset = DBG->getOperand(1).getImm(); |
Devang Patel | 31defcf | 2010-08-06 00:26:18 +0000 | [diff] [blame] | 291 | DebugLoc DL; |
| 292 | if (MI == MBB->end()) { |
| 293 | // If MI is at basic block end then use last instruction's location. |
| 294 | MachineBasicBlock::iterator EI = MI; |
| 295 | DL = (--EI)->getDebugLoc(); |
| 296 | } |
| 297 | else |
| 298 | DL = MI->getDebugLoc(); |
Jim Grosbach | 07cb689 | 2010-09-01 19:16:29 +0000 | [diff] [blame] | 299 | if (MachineInstr *NewDV = |
Devang Patel | 459a36b | 2010-08-04 18:42:02 +0000 | [diff] [blame] | 300 | TII->emitFrameIndexDebugValue(*MF, FI, Offset, MDPtr, DL)) { |
| 301 | MachineBasicBlock *MBB = DBG->getParent(); |
| 302 | MBB->insert(MI, NewDV); |
| 303 | DEBUG(dbgs() << "Inserting debug info due to spill:" << "\n" << *NewDV); |
Devang Patel | 459a36b | 2010-08-04 18:42:02 +0000 | [diff] [blame] | 304 | } |
| 305 | } |
Jakob Stoklund Olesen | 91ba63d | 2012-02-22 16:50:46 +0000 | [diff] [blame] | 306 | // Now this register is spilled there is should not be any DBG_VALUE |
| 307 | // pointing to this register because they are all pointing to spilled value |
| 308 | // now. |
Devang Patel | 6f373a8 | 2011-06-21 23:02:36 +0000 | [diff] [blame] | 309 | LRIDbgValues.clear(); |
Jakob Stoklund Olesen | 844db9c | 2010-05-17 02:49:15 +0000 | [diff] [blame] | 310 | if (SpillKill) |
Jakob Stoklund Olesen | 210e2af | 2010-05-11 23:24:47 +0000 | [diff] [blame] | 311 | LR.LastUse = 0; // Don't kill register again |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 312 | } |
Jakob Stoklund Olesen | 844db9c | 2010-05-17 02:49:15 +0000 | [diff] [blame] | 313 | killVirtReg(LRI); |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 314 | } |
| 315 | |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 316 | /// spillAll - Spill all dirty virtregs without killing them. |
Akira Hatanaka | bab2421 | 2012-10-31 00:56:01 +0000 | [diff] [blame^] | 317 | void RAFast::spillAll(MachineBasicBlock::iterator MI) { |
Jakob Stoklund Olesen | f3ea06b | 2010-05-17 15:30:37 +0000 | [diff] [blame] | 318 | if (LiveVirtRegs.empty()) return; |
Jakob Stoklund Olesen | e6aba83 | 2010-05-17 02:07:32 +0000 | [diff] [blame] | 319 | isBulkSpilling = true; |
Jakob Stoklund Olesen | 2997985 | 2010-05-17 20:01:22 +0000 | [diff] [blame] | 320 | // The LiveRegMap is keyed by an unsigned (the virtreg number), so the order |
| 321 | // of spilling here is deterministic, if arbitrary. |
| 322 | for (LiveRegMap::iterator i = LiveVirtRegs.begin(), e = LiveVirtRegs.end(); |
| 323 | i != e; ++i) |
Jakob Stoklund Olesen | e6aba83 | 2010-05-17 02:07:32 +0000 | [diff] [blame] | 324 | spillVirtReg(MI, i); |
| 325 | LiveVirtRegs.clear(); |
| 326 | isBulkSpilling = false; |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 327 | } |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 328 | |
Jakob Stoklund Olesen | 4ed1082 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 329 | /// usePhysReg - Handle the direct use of a physical register. |
| 330 | /// Check that the register is not used by a virtreg. |
| 331 | /// Kill the physreg, marking it free. |
| 332 | /// This may add implicit kills to MO->getParent() and invalidate MO. |
| 333 | void RAFast::usePhysReg(MachineOperand &MO) { |
| 334 | unsigned PhysReg = MO.getReg(); |
| 335 | assert(TargetRegisterInfo::isPhysicalRegister(PhysReg) && |
| 336 | "Bad usePhysReg operand"); |
| 337 | |
| 338 | switch (PhysRegState[PhysReg]) { |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 339 | case regDisabled: |
| 340 | break; |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 341 | case regReserved: |
| 342 | PhysRegState[PhysReg] = regFree; |
Jakob Stoklund Olesen | 4ed1082 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 343 | // Fall through |
| 344 | case regFree: |
Jakob Stoklund Olesen | d7ea7d5 | 2012-10-17 01:37:59 +0000 | [diff] [blame] | 345 | UsedInInstr.insert(PhysReg); |
Jakob Stoklund Olesen | 4ed1082 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 346 | MO.setIsKill(); |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 347 | return; |
| 348 | default: |
Eric Christopher | f299da8 | 2010-12-08 21:35:09 +0000 | [diff] [blame] | 349 | // The physreg was allocated to a virtual register. That means the value we |
Jakob Stoklund Olesen | 4ed1082 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 350 | // wanted has been clobbered. |
| 351 | llvm_unreachable("Instruction uses an allocated register"); |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 352 | } |
| 353 | |
Jakob Stoklund Olesen | 4ed1082 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 354 | // Maybe a superregister is reserved? |
Jakob Stoklund Olesen | 396618b | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 355 | for (MCRegAliasIterator AI(PhysReg, TRI, false); AI.isValid(); ++AI) { |
| 356 | unsigned Alias = *AI; |
Jakob Stoklund Olesen | 4ed1082 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 357 | switch (PhysRegState[Alias]) { |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 358 | case regDisabled: |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 359 | break; |
| 360 | case regReserved: |
Jakob Stoklund Olesen | 4ed1082 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 361 | assert(TRI->isSuperRegister(PhysReg, Alias) && |
| 362 | "Instruction is not using a subregister of a reserved register"); |
| 363 | // Leave the superregister in the working set. |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 364 | PhysRegState[Alias] = regFree; |
Jakob Stoklund Olesen | d7ea7d5 | 2012-10-17 01:37:59 +0000 | [diff] [blame] | 365 | UsedInInstr.insert(Alias); |
Jakob Stoklund Olesen | 4ed1082 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 366 | MO.getParent()->addRegisterKilled(Alias, TRI, true); |
| 367 | return; |
| 368 | case regFree: |
| 369 | if (TRI->isSuperRegister(PhysReg, Alias)) { |
| 370 | // Leave the superregister in the working set. |
Jakob Stoklund Olesen | d7ea7d5 | 2012-10-17 01:37:59 +0000 | [diff] [blame] | 371 | UsedInInstr.insert(Alias); |
Jakob Stoklund Olesen | 4ed1082 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 372 | MO.getParent()->addRegisterKilled(Alias, TRI, true); |
| 373 | return; |
| 374 | } |
| 375 | // Some other alias was in the working set - clear it. |
| 376 | PhysRegState[Alias] = 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 | llvm_unreachable("Instruction uses an alias of an allocated register"); |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 380 | } |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 381 | } |
Jakob Stoklund Olesen | 4ed1082 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 382 | |
| 383 | // All aliases are disabled, bring register into working set. |
| 384 | PhysRegState[PhysReg] = regFree; |
Jakob Stoklund Olesen | d7ea7d5 | 2012-10-17 01:37:59 +0000 | [diff] [blame] | 385 | UsedInInstr.insert(PhysReg); |
Jakob Stoklund Olesen | 4ed1082 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 386 | MO.setIsKill(); |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 387 | } |
| 388 | |
Jakob Stoklund Olesen | 4ed1082 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 389 | /// definePhysReg - Mark PhysReg as reserved or free after spilling any |
| 390 | /// virtregs. This is very similar to defineVirtReg except the physreg is |
| 391 | /// reserved instead of allocated. |
Jakob Stoklund Olesen | 6fb69d8 | 2010-05-17 02:07:22 +0000 | [diff] [blame] | 392 | void RAFast::definePhysReg(MachineInstr *MI, unsigned PhysReg, |
| 393 | RegState NewState) { |
Jakob Stoklund Olesen | d7ea7d5 | 2012-10-17 01:37:59 +0000 | [diff] [blame] | 394 | UsedInInstr.insert(PhysReg); |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 395 | switch (unsigned VirtReg = PhysRegState[PhysReg]) { |
| 396 | case regDisabled: |
| 397 | break; |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 398 | default: |
Jakob Stoklund Olesen | e6aba83 | 2010-05-17 02:07:32 +0000 | [diff] [blame] | 399 | spillVirtReg(MI, VirtReg); |
Jakob Stoklund Olesen | 4ed1082 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 400 | // Fall through. |
| 401 | case regFree: |
| 402 | case regReserved: |
| 403 | PhysRegState[PhysReg] = NewState; |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 404 | return; |
| 405 | } |
| 406 | |
Jakob Stoklund Olesen | 4ed1082 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 407 | // This is a disabled register, disable all aliases. |
| 408 | PhysRegState[PhysReg] = NewState; |
Jakob Stoklund Olesen | 396618b | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 409 | for (MCRegAliasIterator AI(PhysReg, TRI, false); AI.isValid(); ++AI) { |
| 410 | unsigned Alias = *AI; |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 411 | switch (unsigned VirtReg = PhysRegState[Alias]) { |
| 412 | case regDisabled: |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 413 | break; |
| 414 | default: |
Jakob Stoklund Olesen | e6aba83 | 2010-05-17 02:07:32 +0000 | [diff] [blame] | 415 | spillVirtReg(MI, VirtReg); |
Jakob Stoklund Olesen | 4ed1082 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 416 | // Fall through. |
| 417 | case regFree: |
| 418 | case regReserved: |
| 419 | PhysRegState[Alias] = regDisabled; |
| 420 | if (TRI->isSuperRegister(PhysReg, Alias)) |
| 421 | return; |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 422 | break; |
| 423 | } |
| 424 | } |
| 425 | } |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 426 | |
Jakob Stoklund Olesen | 4ed1082 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 427 | |
Jakob Stoklund Olesen | 548643c | 2010-05-17 15:30:32 +0000 | [diff] [blame] | 428 | // calcSpillCost - Return the cost of spilling clearing out PhysReg and |
| 429 | // aliases so it is free for allocation. |
| 430 | // Returns 0 when PhysReg is free or disabled with all aliases disabled - it |
| 431 | // can be allocated directly. |
| 432 | // Returns spillImpossible when PhysReg or an alias can't be spilled. |
| 433 | unsigned RAFast::calcSpillCost(unsigned PhysReg) const { |
Jakob Stoklund Olesen | d7ea7d5 | 2012-10-17 01:37:59 +0000 | [diff] [blame] | 434 | if (UsedInInstr.count(PhysReg)) { |
Jakob Stoklund Olesen | 27ce3b9 | 2011-06-28 17:24:32 +0000 | [diff] [blame] | 435 | DEBUG(dbgs() << PrintReg(PhysReg, TRI) << " is already used in instr.\n"); |
Jakob Stoklund Olesen | b8acb7b | 2010-05-17 21:02:08 +0000 | [diff] [blame] | 436 | return spillImpossible; |
Eric Christopher | 0b75634 | 2011-04-12 22:17:44 +0000 | [diff] [blame] | 437 | } |
Jakob Stoklund Olesen | 548643c | 2010-05-17 15:30:32 +0000 | [diff] [blame] | 438 | switch (unsigned VirtReg = PhysRegState[PhysReg]) { |
| 439 | case regDisabled: |
| 440 | break; |
| 441 | case regFree: |
| 442 | return 0; |
| 443 | case regReserved: |
Jakob Stoklund Olesen | 27ce3b9 | 2011-06-28 17:24:32 +0000 | [diff] [blame] | 444 | DEBUG(dbgs() << PrintReg(VirtReg, TRI) << " corresponding " |
| 445 | << PrintReg(PhysReg, TRI) << " is reserved already.\n"); |
Jakob Stoklund Olesen | 548643c | 2010-05-17 15:30:32 +0000 | [diff] [blame] | 446 | return spillImpossible; |
Jakob Stoklund Olesen | a240743 | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 447 | default: { |
| 448 | LiveRegMap::const_iterator I = findLiveVirtReg(VirtReg); |
| 449 | assert(I != LiveVirtRegs.end() && "Missing VirtReg entry"); |
| 450 | return I->Dirty ? spillDirty : spillClean; |
| 451 | } |
Jakob Stoklund Olesen | 548643c | 2010-05-17 15:30:32 +0000 | [diff] [blame] | 452 | } |
| 453 | |
Eric Christopher | bbfc3b3 | 2011-04-12 00:48:08 +0000 | [diff] [blame] | 454 | // This is a disabled register, add up cost of aliases. |
Jakob Stoklund Olesen | 27ce3b9 | 2011-06-28 17:24:32 +0000 | [diff] [blame] | 455 | DEBUG(dbgs() << PrintReg(PhysReg, TRI) << " is disabled.\n"); |
Jakob Stoklund Olesen | 548643c | 2010-05-17 15:30:32 +0000 | [diff] [blame] | 456 | unsigned Cost = 0; |
Jakob Stoklund Olesen | 396618b | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 457 | for (MCRegAliasIterator AI(PhysReg, TRI, false); AI.isValid(); ++AI) { |
| 458 | unsigned Alias = *AI; |
Jakob Stoklund Olesen | d7ea7d5 | 2012-10-17 01:37:59 +0000 | [diff] [blame] | 459 | if (UsedInInstr.count(Alias)) |
Eric Christopher | d31df87 | 2011-04-13 00:20:59 +0000 | [diff] [blame] | 460 | return spillImpossible; |
Jakob Stoklund Olesen | 548643c | 2010-05-17 15:30:32 +0000 | [diff] [blame] | 461 | switch (unsigned VirtReg = PhysRegState[Alias]) { |
| 462 | case regDisabled: |
| 463 | break; |
| 464 | case regFree: |
| 465 | ++Cost; |
| 466 | break; |
| 467 | case regReserved: |
| 468 | return spillImpossible; |
Jakob Stoklund Olesen | a240743 | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 469 | default: { |
| 470 | LiveRegMap::const_iterator I = findLiveVirtReg(VirtReg); |
| 471 | assert(I != LiveVirtRegs.end() && "Missing VirtReg entry"); |
| 472 | Cost += I->Dirty ? spillDirty : spillClean; |
Jakob Stoklund Olesen | 548643c | 2010-05-17 15:30:32 +0000 | [diff] [blame] | 473 | break; |
| 474 | } |
Jakob Stoklund Olesen | a240743 | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 475 | } |
Jakob Stoklund Olesen | 548643c | 2010-05-17 15:30:32 +0000 | [diff] [blame] | 476 | } |
| 477 | return Cost; |
| 478 | } |
| 479 | |
| 480 | |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 481 | /// assignVirtToPhysReg - This method updates local state so that we know |
| 482 | /// that PhysReg is the proper container for VirtReg now. The physical |
| 483 | /// register must not be used for anything else when this is called. |
| 484 | /// |
Jakob Stoklund Olesen | a240743 | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 485 | void RAFast::assignVirtToPhysReg(LiveReg &LR, unsigned PhysReg) { |
| 486 | DEBUG(dbgs() << "Assigning " << PrintReg(LR.VirtReg, TRI) << " to " |
Jakob Stoklund Olesen | 4314268 | 2011-01-09 03:05:53 +0000 | [diff] [blame] | 487 | << PrintReg(PhysReg, TRI) << "\n"); |
Jakob Stoklund Olesen | a240743 | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 488 | PhysRegState[PhysReg] = LR.VirtReg; |
| 489 | assert(!LR.PhysReg && "Already assigned a physreg"); |
| 490 | LR.PhysReg = PhysReg; |
| 491 | } |
| 492 | |
| 493 | RAFast::LiveRegMap::iterator |
| 494 | RAFast::assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg) { |
| 495 | LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg); |
| 496 | assert(LRI != LiveVirtRegs.end() && "VirtReg disappeared"); |
| 497 | assignVirtToPhysReg(*LRI, PhysReg); |
| 498 | return LRI; |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 499 | } |
| 500 | |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 501 | /// allocVirtReg - Allocate a physical register for VirtReg. |
Jakob Stoklund Olesen | a240743 | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 502 | RAFast::LiveRegMap::iterator RAFast::allocVirtReg(MachineInstr *MI, |
| 503 | LiveRegMap::iterator LRI, |
| 504 | unsigned Hint) { |
| 505 | const unsigned VirtReg = LRI->VirtReg; |
Jakob Stoklund Olesen | 01dcbf8 | 2010-05-17 02:07:29 +0000 | [diff] [blame] | 506 | |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 507 | assert(TargetRegisterInfo::isVirtualRegister(VirtReg) && |
| 508 | "Can only allocate virtual registers"); |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 509 | |
Jakob Stoklund Olesen | 4bf4baf | 2010-05-13 00:19:43 +0000 | [diff] [blame] | 510 | const TargetRegisterClass *RC = MRI->getRegClass(VirtReg); |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 511 | |
Jakob Stoklund Olesen | 4bf4baf | 2010-05-13 00:19:43 +0000 | [diff] [blame] | 512 | // Ignore invalid hints. |
| 513 | if (Hint && (!TargetRegisterInfo::isPhysicalRegister(Hint) || |
Jakob Stoklund Olesen | 14d1dd9 | 2012-10-15 22:41:03 +0000 | [diff] [blame] | 514 | !RC->contains(Hint) || !MRI->isAllocatable(Hint))) |
Jakob Stoklund Olesen | 4bf4baf | 2010-05-13 00:19:43 +0000 | [diff] [blame] | 515 | Hint = 0; |
| 516 | |
Jakob Stoklund Olesen | 4bf4baf | 2010-05-13 00:19:43 +0000 | [diff] [blame] | 517 | // Take hint when possible. |
| 518 | if (Hint) { |
Jakob Stoklund Olesen | 5e5ed44 | 2011-06-13 03:26:46 +0000 | [diff] [blame] | 519 | // Ignore the hint if we would have to spill a dirty register. |
| 520 | unsigned Cost = calcSpillCost(Hint); |
| 521 | if (Cost < spillDirty) { |
| 522 | if (Cost) |
| 523 | definePhysReg(MI, Hint, regFree); |
Jakob Stoklund Olesen | a240743 | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 524 | // definePhysReg may kill virtual registers and modify LiveVirtRegs. |
| 525 | // That invalidates LRI, so run a new lookup for VirtReg. |
| 526 | return assignVirtToPhysReg(VirtReg, Hint); |
Jakob Stoklund Olesen | 4bf4baf | 2010-05-13 00:19:43 +0000 | [diff] [blame] | 527 | } |
| 528 | } |
| 529 | |
Jakob Stoklund Olesen | 5d20c31 | 2011-06-02 18:35:30 +0000 | [diff] [blame] | 530 | ArrayRef<unsigned> AO = RegClassInfo.getOrder(RC); |
Jakob Stoklund Olesen | 548643c | 2010-05-17 15:30:32 +0000 | [diff] [blame] | 531 | |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 532 | // First try to find a completely free register. |
Jakob Stoklund Olesen | 5d20c31 | 2011-06-02 18:35:30 +0000 | [diff] [blame] | 533 | for (ArrayRef<unsigned>::iterator I = AO.begin(), E = AO.end(); I != E; ++I) { |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 534 | unsigned PhysReg = *I; |
Jakob Stoklund Olesen | d7ea7d5 | 2012-10-17 01:37:59 +0000 | [diff] [blame] | 535 | if (PhysRegState[PhysReg] == regFree && !UsedInInstr.count(PhysReg)) { |
Jakob Stoklund Olesen | a240743 | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 536 | assignVirtToPhysReg(*LRI, PhysReg); |
| 537 | return LRI; |
| 538 | } |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 539 | } |
| 540 | |
Jakob Stoklund Olesen | 4314268 | 2011-01-09 03:05:53 +0000 | [diff] [blame] | 541 | DEBUG(dbgs() << "Allocating " << PrintReg(VirtReg) << " from " |
| 542 | << RC->getName() << "\n"); |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 543 | |
Jakob Stoklund Olesen | 548643c | 2010-05-17 15:30:32 +0000 | [diff] [blame] | 544 | unsigned BestReg = 0, BestCost = spillImpossible; |
Jakob Stoklund Olesen | 5d20c31 | 2011-06-02 18:35:30 +0000 | [diff] [blame] | 545 | for (ArrayRef<unsigned>::iterator I = AO.begin(), E = AO.end(); I != E; ++I) { |
Jakob Stoklund Olesen | 548643c | 2010-05-17 15:30:32 +0000 | [diff] [blame] | 546 | unsigned Cost = calcSpillCost(*I); |
Jakob Stoklund Olesen | 27ce3b9 | 2011-06-28 17:24:32 +0000 | [diff] [blame] | 547 | DEBUG(dbgs() << "\tRegister: " << PrintReg(*I, TRI) << "\n"); |
Eric Christopher | 0b75634 | 2011-04-12 22:17:44 +0000 | [diff] [blame] | 548 | DEBUG(dbgs() << "\tCost: " << Cost << "\n"); |
| 549 | DEBUG(dbgs() << "\tBestCost: " << BestCost << "\n"); |
Jakob Stoklund Olesen | f3ea06b | 2010-05-17 15:30:37 +0000 | [diff] [blame] | 550 | // Cost is 0 when all aliases are already disabled. |
Jakob Stoklund Olesen | a240743 | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 551 | if (Cost == 0) { |
| 552 | assignVirtToPhysReg(*LRI, *I); |
| 553 | return LRI; |
| 554 | } |
Jakob Stoklund Olesen | f3ea06b | 2010-05-17 15:30:37 +0000 | [diff] [blame] | 555 | if (Cost < BestCost) |
| 556 | BestReg = *I, BestCost = Cost; |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | if (BestReg) { |
Jakob Stoklund Olesen | f3ea06b | 2010-05-17 15:30:37 +0000 | [diff] [blame] | 560 | definePhysReg(MI, BestReg, regFree); |
Jakob Stoklund Olesen | a240743 | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 561 | // definePhysReg may kill virtual registers and modify LiveVirtRegs. |
| 562 | // That invalidates LRI, so run a new lookup for VirtReg. |
| 563 | return assignVirtToPhysReg(VirtReg, BestReg); |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 564 | } |
| 565 | |
Jakob Stoklund Olesen | 9d812a2 | 2011-07-02 07:17:37 +0000 | [diff] [blame] | 566 | // Nothing we can do. Report an error and keep going with a bad allocation. |
| 567 | MI->emitError("ran out of registers during register allocation"); |
| 568 | definePhysReg(MI, *AO.begin(), regFree); |
Jakob Stoklund Olesen | a240743 | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 569 | return assignVirtToPhysReg(VirtReg, *AO.begin()); |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 570 | } |
| 571 | |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 572 | /// defineVirtReg - Allocate a register for VirtReg and mark it as dirty. |
Jakob Stoklund Olesen | 646dd7c | 2010-05-17 03:26:09 +0000 | [diff] [blame] | 573 | RAFast::LiveRegMap::iterator |
| 574 | RAFast::defineVirtReg(MachineInstr *MI, unsigned OpNum, |
| 575 | unsigned VirtReg, unsigned Hint) { |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 576 | assert(TargetRegisterInfo::isVirtualRegister(VirtReg) && |
| 577 | "Not a virtual register"); |
Jakob Stoklund Olesen | 844db9c | 2010-05-17 02:49:15 +0000 | [diff] [blame] | 578 | LiveRegMap::iterator LRI; |
Jakob Stoklund Olesen | 01dcbf8 | 2010-05-17 02:07:29 +0000 | [diff] [blame] | 579 | bool New; |
Jakob Stoklund Olesen | a240743 | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 580 | tie(LRI, New) = LiveVirtRegs.insert(LiveReg(VirtReg)); |
Jakob Stoklund Olesen | 0c9e4f5 | 2010-05-17 04:50:57 +0000 | [diff] [blame] | 581 | if (New) { |
| 582 | // If there is no hint, peek at the only use of this register. |
| 583 | if ((!Hint || !TargetRegisterInfo::isPhysicalRegister(Hint)) && |
| 584 | MRI->hasOneNonDBGUse(VirtReg)) { |
Jakob Stoklund Olesen | 273f7e4 | 2010-07-03 00:04:37 +0000 | [diff] [blame] | 585 | const MachineInstr &UseMI = *MRI->use_nodbg_begin(VirtReg); |
Jakob Stoklund Olesen | 0c9e4f5 | 2010-05-17 04:50:57 +0000 | [diff] [blame] | 586 | // It's a copy, use the destination register as a hint. |
Jakob Stoklund Olesen | 273f7e4 | 2010-07-03 00:04:37 +0000 | [diff] [blame] | 587 | if (UseMI.isCopyLike()) |
| 588 | Hint = UseMI.getOperand(0).getReg(); |
Jakob Stoklund Olesen | 0c9e4f5 | 2010-05-17 04:50:57 +0000 | [diff] [blame] | 589 | } |
Jakob Stoklund Olesen | a240743 | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 590 | LRI = allocVirtReg(MI, LRI, Hint); |
| 591 | } else if (LRI->LastUse) { |
Jakob Stoklund Olesen | 0eeb05c | 2010-05-18 21:10:50 +0000 | [diff] [blame] | 592 | // Redefining a live register - kill at the last use, unless it is this |
| 593 | // instruction defining VirtReg multiple times. |
Jakob Stoklund Olesen | a240743 | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 594 | if (LRI->LastUse != MI || LRI->LastUse->getOperand(LRI->LastOpNum).isUse()) |
| 595 | addKillFlag(*LRI); |
Jakob Stoklund Olesen | 0eeb05c | 2010-05-18 21:10:50 +0000 | [diff] [blame] | 596 | } |
Jakob Stoklund Olesen | a240743 | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 597 | assert(LRI->PhysReg && "Register not assigned"); |
| 598 | LRI->LastUse = MI; |
| 599 | LRI->LastOpNum = OpNum; |
| 600 | LRI->Dirty = true; |
Jakob Stoklund Olesen | d7ea7d5 | 2012-10-17 01:37:59 +0000 | [diff] [blame] | 601 | UsedInInstr.insert(LRI->PhysReg); |
Jakob Stoklund Olesen | 646dd7c | 2010-05-17 03:26:09 +0000 | [diff] [blame] | 602 | return LRI; |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 603 | } |
| 604 | |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 605 | /// 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] | 606 | RAFast::LiveRegMap::iterator |
| 607 | RAFast::reloadVirtReg(MachineInstr *MI, unsigned OpNum, |
| 608 | unsigned VirtReg, unsigned Hint) { |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 609 | assert(TargetRegisterInfo::isVirtualRegister(VirtReg) && |
| 610 | "Not a virtual register"); |
Jakob Stoklund Olesen | 844db9c | 2010-05-17 02:49:15 +0000 | [diff] [blame] | 611 | LiveRegMap::iterator LRI; |
Jakob Stoklund Olesen | 01dcbf8 | 2010-05-17 02:07:29 +0000 | [diff] [blame] | 612 | bool New; |
Jakob Stoklund Olesen | a240743 | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 613 | tie(LRI, New) = LiveVirtRegs.insert(LiveReg(VirtReg)); |
Jakob Stoklund Olesen | ac3e529 | 2010-05-17 03:26:06 +0000 | [diff] [blame] | 614 | MachineOperand &MO = MI->getOperand(OpNum); |
Jakob Stoklund Olesen | 01dcbf8 | 2010-05-17 02:07:29 +0000 | [diff] [blame] | 615 | if (New) { |
Jakob Stoklund Olesen | a240743 | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 616 | LRI = allocVirtReg(MI, LRI, Hint); |
Jakob Stoklund Olesen | 4bf4baf | 2010-05-13 00:19:43 +0000 | [diff] [blame] | 617 | const TargetRegisterClass *RC = MRI->getRegClass(VirtReg); |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 618 | int FrameIndex = getStackSpaceFor(VirtReg, RC); |
Jakob Stoklund Olesen | 4314268 | 2011-01-09 03:05:53 +0000 | [diff] [blame] | 619 | DEBUG(dbgs() << "Reloading " << PrintReg(VirtReg, TRI) << " into " |
Jakob Stoklund Olesen | a240743 | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 620 | << PrintReg(LRI->PhysReg, TRI) << "\n"); |
| 621 | TII->loadRegFromStackSlot(*MBB, MI, LRI->PhysReg, FrameIndex, RC, TRI); |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 622 | ++NumLoads; |
Jakob Stoklund Olesen | a240743 | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 623 | } else if (LRI->Dirty) { |
Jakob Stoklund Olesen | 1e03ff4 | 2010-05-15 06:09:08 +0000 | [diff] [blame] | 624 | if (isLastUseOfLocalReg(MO)) { |
| 625 | DEBUG(dbgs() << "Killing last use: " << MO << "\n"); |
Jakob Stoklund Olesen | d1303d2 | 2010-06-29 19:15:30 +0000 | [diff] [blame] | 626 | if (MO.isUse()) |
| 627 | MO.setIsKill(); |
| 628 | else |
| 629 | MO.setIsDead(); |
Jakob Stoklund Olesen | 1e03ff4 | 2010-05-15 06:09:08 +0000 | [diff] [blame] | 630 | } else if (MO.isKill()) { |
| 631 | DEBUG(dbgs() << "Clearing dubious kill: " << MO << "\n"); |
| 632 | MO.setIsKill(false); |
Jakob Stoklund Olesen | d1303d2 | 2010-06-29 19:15:30 +0000 | [diff] [blame] | 633 | } else if (MO.isDead()) { |
| 634 | DEBUG(dbgs() << "Clearing dubious dead: " << MO << "\n"); |
| 635 | MO.setIsDead(false); |
Jakob Stoklund Olesen | 1e03ff4 | 2010-05-15 06:09:08 +0000 | [diff] [blame] | 636 | } |
Jakob Stoklund Olesen | ac3e529 | 2010-05-17 03:26:06 +0000 | [diff] [blame] | 637 | } else if (MO.isKill()) { |
| 638 | // We must remove kill flags from uses of reloaded registers because the |
| 639 | // register would be killed immediately, and there might be a second use: |
| 640 | // %foo = OR %x<kill>, %x |
| 641 | // This would cause a second reload of %x into a different register. |
| 642 | DEBUG(dbgs() << "Clearing clean kill: " << MO << "\n"); |
| 643 | MO.setIsKill(false); |
Jakob Stoklund Olesen | d1303d2 | 2010-06-29 19:15:30 +0000 | [diff] [blame] | 644 | } else if (MO.isDead()) { |
| 645 | DEBUG(dbgs() << "Clearing clean dead: " << MO << "\n"); |
| 646 | MO.setIsDead(false); |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 647 | } |
Jakob Stoklund Olesen | a240743 | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 648 | assert(LRI->PhysReg && "Register not assigned"); |
| 649 | LRI->LastUse = MI; |
| 650 | LRI->LastOpNum = OpNum; |
Jakob Stoklund Olesen | d7ea7d5 | 2012-10-17 01:37:59 +0000 | [diff] [blame] | 651 | UsedInInstr.insert(LRI->PhysReg); |
Jakob Stoklund Olesen | 646dd7c | 2010-05-17 03:26:09 +0000 | [diff] [blame] | 652 | return LRI; |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 653 | } |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 654 | |
Jakob Stoklund Olesen | 0eeb05c | 2010-05-18 21:10:50 +0000 | [diff] [blame] | 655 | // setPhysReg - Change operand OpNum in MI the refer the PhysReg, considering |
| 656 | // subregs. This may invalidate any operand pointers. |
| 657 | // Return true if the operand kills its register. |
| 658 | bool RAFast::setPhysReg(MachineInstr *MI, unsigned OpNum, unsigned PhysReg) { |
| 659 | MachineOperand &MO = MI->getOperand(OpNum); |
Jakob Stoklund Olesen | 6565a70 | 2012-05-14 21:30:58 +0000 | [diff] [blame] | 660 | bool Dead = MO.isDead(); |
Jakob Stoklund Olesen | 41e1401 | 2010-05-17 02:49:21 +0000 | [diff] [blame] | 661 | if (!MO.getSubReg()) { |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 662 | MO.setReg(PhysReg); |
Jakob Stoklund Olesen | 6565a70 | 2012-05-14 21:30:58 +0000 | [diff] [blame] | 663 | return MO.isKill() || Dead; |
Jakob Stoklund Olesen | 41e1401 | 2010-05-17 02:49:21 +0000 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | // Handle subregister index. |
| 667 | MO.setReg(PhysReg ? TRI->getSubReg(PhysReg, MO.getSubReg()) : 0); |
| 668 | MO.setSubReg(0); |
Jakob Stoklund Olesen | d32e735 | 2010-05-19 21:36:05 +0000 | [diff] [blame] | 669 | |
| 670 | // A kill flag implies killing the full register. Add corresponding super |
| 671 | // register kill. |
| 672 | if (MO.isKill()) { |
| 673 | MI->addRegisterKilled(PhysReg, TRI, true); |
Jakob Stoklund Olesen | 41e1401 | 2010-05-17 02:49:21 +0000 | [diff] [blame] | 674 | return true; |
| 675 | } |
Jakob Stoklund Olesen | 4d10829 | 2012-05-14 21:10:25 +0000 | [diff] [blame] | 676 | |
| 677 | // A <def,read-undef> of a sub-register requires an implicit def of the full |
| 678 | // register. |
| 679 | if (MO.isDef() && MO.isUndef()) |
| 680 | MI->addRegisterDefined(PhysReg, TRI); |
| 681 | |
Jakob Stoklund Olesen | 6565a70 | 2012-05-14 21:30:58 +0000 | [diff] [blame] | 682 | return Dead; |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 683 | } |
| 684 | |
Jakob Stoklund Olesen | d843b39 | 2010-06-28 18:34:34 +0000 | [diff] [blame] | 685 | // Handle special instruction operand like early clobbers and tied ops when |
| 686 | // there are additional physreg defines. |
| 687 | void RAFast::handleThroughOperands(MachineInstr *MI, |
| 688 | SmallVectorImpl<unsigned> &VirtDead) { |
| 689 | DEBUG(dbgs() << "Scanning for through registers:"); |
| 690 | SmallSet<unsigned, 8> ThroughRegs; |
| 691 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 692 | MachineOperand &MO = MI->getOperand(i); |
| 693 | if (!MO.isReg()) continue; |
| 694 | unsigned Reg = MO.getReg(); |
Jakob Stoklund Olesen | c9df025 | 2011-01-10 02:58:51 +0000 | [diff] [blame] | 695 | if (!TargetRegisterInfo::isVirtualRegister(Reg)) |
| 696 | continue; |
Jakob Stoklund Olesen | d1303d2 | 2010-06-29 19:15:30 +0000 | [diff] [blame] | 697 | if (MO.isEarlyClobber() || MI->isRegTiedToDefOperand(i) || |
| 698 | (MO.getSubReg() && MI->readsVirtualRegister(Reg))) { |
Jakob Stoklund Olesen | d843b39 | 2010-06-28 18:34:34 +0000 | [diff] [blame] | 699 | if (ThroughRegs.insert(Reg)) |
Jakob Stoklund Olesen | 4314268 | 2011-01-09 03:05:53 +0000 | [diff] [blame] | 700 | DEBUG(dbgs() << ' ' << PrintReg(Reg)); |
Jakob Stoklund Olesen | d843b39 | 2010-06-28 18:34:34 +0000 | [diff] [blame] | 701 | } |
| 702 | } |
| 703 | |
| 704 | // If any physreg defines collide with preallocated through registers, |
| 705 | // we must spill and reallocate. |
| 706 | DEBUG(dbgs() << "\nChecking for physdef collisions.\n"); |
| 707 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 708 | MachineOperand &MO = MI->getOperand(i); |
| 709 | if (!MO.isReg() || !MO.isDef()) continue; |
| 710 | unsigned Reg = MO.getReg(); |
| 711 | if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue; |
Jakob Stoklund Olesen | 8c70ea4 | 2012-06-01 22:38:17 +0000 | [diff] [blame] | 712 | for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) { |
Jakob Stoklund Olesen | d7ea7d5 | 2012-10-17 01:37:59 +0000 | [diff] [blame] | 713 | UsedInInstr.insert(*AI); |
Jakob Stoklund Olesen | 8c70ea4 | 2012-06-01 22:38:17 +0000 | [diff] [blame] | 714 | if (ThroughRegs.count(PhysRegState[*AI])) |
| 715 | definePhysReg(MI, *AI, regFree); |
Jakob Stoklund Olesen | d843b39 | 2010-06-28 18:34:34 +0000 | [diff] [blame] | 716 | } |
| 717 | } |
| 718 | |
Jakob Stoklund Olesen | d1303d2 | 2010-06-29 19:15:30 +0000 | [diff] [blame] | 719 | SmallVector<unsigned, 8> PartialDefs; |
Rafael Espindola | 254a132 | 2011-11-22 06:27:18 +0000 | [diff] [blame] | 720 | DEBUG(dbgs() << "Allocating tied uses.\n"); |
Jakob Stoklund Olesen | d843b39 | 2010-06-28 18:34:34 +0000 | [diff] [blame] | 721 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 722 | MachineOperand &MO = MI->getOperand(i); |
| 723 | if (!MO.isReg()) continue; |
| 724 | unsigned Reg = MO.getReg(); |
Jakob Stoklund Olesen | c9df025 | 2011-01-10 02:58:51 +0000 | [diff] [blame] | 725 | if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue; |
Jakob Stoklund Olesen | d843b39 | 2010-06-28 18:34:34 +0000 | [diff] [blame] | 726 | if (MO.isUse()) { |
| 727 | unsigned DefIdx = 0; |
| 728 | if (!MI->isRegTiedToDefOperand(i, &DefIdx)) continue; |
| 729 | DEBUG(dbgs() << "Operand " << i << "("<< MO << ") is tied to operand " |
| 730 | << DefIdx << ".\n"); |
| 731 | LiveRegMap::iterator LRI = reloadVirtReg(MI, i, Reg, 0); |
Jakob Stoklund Olesen | a240743 | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 732 | unsigned PhysReg = LRI->PhysReg; |
Jakob Stoklund Olesen | d843b39 | 2010-06-28 18:34:34 +0000 | [diff] [blame] | 733 | setPhysReg(MI, i, PhysReg); |
Jakob Stoklund Olesen | d1303d2 | 2010-06-29 19:15:30 +0000 | [diff] [blame] | 734 | // Note: we don't update the def operand yet. That would cause the normal |
| 735 | // def-scan to attempt spilling. |
| 736 | } else if (MO.getSubReg() && MI->readsVirtualRegister(Reg)) { |
| 737 | DEBUG(dbgs() << "Partial redefine: " << MO << "\n"); |
| 738 | // Reload the register, but don't assign to the operand just yet. |
| 739 | // That would confuse the later phys-def processing pass. |
| 740 | LiveRegMap::iterator LRI = reloadVirtReg(MI, i, Reg, 0); |
Jakob Stoklund Olesen | a240743 | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 741 | PartialDefs.push_back(LRI->PhysReg); |
Jakob Stoklund Olesen | d843b39 | 2010-06-28 18:34:34 +0000 | [diff] [blame] | 742 | } |
| 743 | } |
| 744 | |
Rafael Espindola | 254a132 | 2011-11-22 06:27:18 +0000 | [diff] [blame] | 745 | DEBUG(dbgs() << "Allocating early clobbers.\n"); |
| 746 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 747 | MachineOperand &MO = MI->getOperand(i); |
| 748 | if (!MO.isReg()) continue; |
| 749 | unsigned Reg = MO.getReg(); |
| 750 | if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue; |
| 751 | if (!MO.isEarlyClobber()) |
| 752 | continue; |
| 753 | // Note: defineVirtReg may invalidate MO. |
| 754 | LiveRegMap::iterator LRI = defineVirtReg(MI, i, Reg, 0); |
Jakob Stoklund Olesen | a240743 | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 755 | unsigned PhysReg = LRI->PhysReg; |
Rafael Espindola | 254a132 | 2011-11-22 06:27:18 +0000 | [diff] [blame] | 756 | if (setPhysReg(MI, i, PhysReg)) |
| 757 | VirtDead.push_back(Reg); |
| 758 | } |
| 759 | |
Jakob Stoklund Olesen | d843b39 | 2010-06-28 18:34:34 +0000 | [diff] [blame] | 760 | // Restore UsedInInstr to a state usable for allocating normal virtual uses. |
Jakob Stoklund Olesen | d7ea7d5 | 2012-10-17 01:37:59 +0000 | [diff] [blame] | 761 | UsedInInstr.clear(); |
Jakob Stoklund Olesen | d843b39 | 2010-06-28 18:34:34 +0000 | [diff] [blame] | 762 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 763 | MachineOperand &MO = MI->getOperand(i); |
| 764 | if (!MO.isReg() || (MO.isDef() && !MO.isEarlyClobber())) continue; |
| 765 | unsigned Reg = MO.getReg(); |
| 766 | if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue; |
Jakob Stoklund Olesen | 27ce3b9 | 2011-06-28 17:24:32 +0000 | [diff] [blame] | 767 | DEBUG(dbgs() << "\tSetting " << PrintReg(Reg, TRI) |
| 768 | << " as used in instr\n"); |
Jakob Stoklund Olesen | d7ea7d5 | 2012-10-17 01:37:59 +0000 | [diff] [blame] | 769 | UsedInInstr.insert(Reg); |
Jakob Stoklund Olesen | d843b39 | 2010-06-28 18:34:34 +0000 | [diff] [blame] | 770 | } |
Jakob Stoklund Olesen | d1303d2 | 2010-06-29 19:15:30 +0000 | [diff] [blame] | 771 | |
| 772 | // Also mark PartialDefs as used to avoid reallocation. |
| 773 | for (unsigned i = 0, e = PartialDefs.size(); i != e; ++i) |
Jakob Stoklund Olesen | d7ea7d5 | 2012-10-17 01:37:59 +0000 | [diff] [blame] | 774 | UsedInInstr.insert(PartialDefs[i]); |
Jakob Stoklund Olesen | d843b39 | 2010-06-28 18:34:34 +0000 | [diff] [blame] | 775 | } |
| 776 | |
Andrew Trick | b3d5847 | 2012-01-31 05:55:32 +0000 | [diff] [blame] | 777 | /// addRetOperand - ensure that a return instruction has an operand for each |
| 778 | /// value live out of the function. |
| 779 | /// |
| 780 | /// Things marked both call and return are tail calls; do not do this for them. |
| 781 | /// The tail callee need not take the same registers as input that it produces |
| 782 | /// as output, and there are dependencies for its input registers elsewhere. |
| 783 | /// |
| 784 | /// FIXME: This should be done as part of instruction selection, and this helper |
| 785 | /// should be deleted. Until then, we use custom logic here to create the proper |
| 786 | /// operand under all circumstances. We can't use addRegisterKilled because that |
| 787 | /// doesn't make sense for undefined values. We can't simply avoid calling it |
| 788 | /// for undefined values, because we must ensure that the operand always exists. |
| 789 | void RAFast::addRetOperands(MachineBasicBlock *MBB) { |
| 790 | if (MBB->empty() || !MBB->back().isReturn() || MBB->back().isCall()) |
| 791 | return; |
| 792 | |
| 793 | MachineInstr *MI = &MBB->back(); |
| 794 | |
| 795 | for (MachineRegisterInfo::liveout_iterator |
| 796 | I = MBB->getParent()->getRegInfo().liveout_begin(), |
| 797 | E = MBB->getParent()->getRegInfo().liveout_end(); I != E; ++I) { |
| 798 | unsigned Reg = *I; |
| 799 | assert(TargetRegisterInfo::isPhysicalRegister(Reg) && |
| 800 | "Cannot have a live-out virtual register."); |
| 801 | |
| 802 | bool hasDef = PhysRegState[Reg] == regReserved; |
| 803 | |
| 804 | // Check if this register already has an operand. |
| 805 | bool Found = false; |
| 806 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 807 | MachineOperand &MO = MI->getOperand(i); |
| 808 | if (!MO.isReg() || !MO.isUse()) |
| 809 | continue; |
| 810 | |
| 811 | unsigned OperReg = MO.getReg(); |
Andrew Trick | ab78e20 | 2012-01-31 18:54:19 +0000 | [diff] [blame] | 812 | if (!TargetRegisterInfo::isPhysicalRegister(OperReg)) |
| 813 | continue; |
| 814 | |
| 815 | if (OperReg == Reg || TRI->isSuperRegister(OperReg, Reg)) { |
| 816 | // If the ret already has an operand for this physreg or a superset, |
| 817 | // don't duplicate it. Set the kill flag if the value is defined. |
| 818 | if (hasDef && !MO.isKill()) |
| 819 | MO.setIsKill(); |
| 820 | Found = true; |
| 821 | break; |
Andrew Trick | b3d5847 | 2012-01-31 05:55:32 +0000 | [diff] [blame] | 822 | } |
| 823 | } |
| 824 | if (!Found) |
| 825 | MI->addOperand(MachineOperand::CreateReg(Reg, |
| 826 | false /*IsDef*/, |
| 827 | true /*IsImp*/, |
| 828 | hasDef/*IsKill*/)); |
| 829 | } |
| 830 | } |
| 831 | |
Jakob Stoklund Olesen | 6fb69d8 | 2010-05-17 02:07:22 +0000 | [diff] [blame] | 832 | void RAFast::AllocateBasicBlock() { |
| 833 | DEBUG(dbgs() << "\nAllocating " << *MBB); |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 834 | |
| 835 | PhysRegState.assign(TRI->getNumRegs(), regDisabled); |
Jakob Stoklund Olesen | a240743 | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 836 | assert(LiveVirtRegs.empty() && "Mapping not cleared from last block?"); |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 837 | |
Jakob Stoklund Olesen | 6fb69d8 | 2010-05-17 02:07:22 +0000 | [diff] [blame] | 838 | MachineBasicBlock::iterator MII = MBB->begin(); |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 839 | |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 840 | // Add live-in registers as live. |
Jakob Stoklund Olesen | 6fb69d8 | 2010-05-17 02:07:22 +0000 | [diff] [blame] | 841 | for (MachineBasicBlock::livein_iterator I = MBB->livein_begin(), |
| 842 | E = MBB->livein_end(); I != E; ++I) |
Jakob Stoklund Olesen | 14d1dd9 | 2012-10-15 22:41:03 +0000 | [diff] [blame] | 843 | if (MRI->isAllocatable(*I)) |
Jakob Stoklund Olesen | 9d4b51b | 2010-08-31 19:54:25 +0000 | [diff] [blame] | 844 | definePhysReg(MII, *I, regReserved); |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 845 | |
Jakob Stoklund Olesen | d843b39 | 2010-06-28 18:34:34 +0000 | [diff] [blame] | 846 | SmallVector<unsigned, 8> VirtDead; |
Jakob Stoklund Olesen | 7ff82e1 | 2010-05-14 04:30:51 +0000 | [diff] [blame] | 847 | SmallVector<MachineInstr*, 32> Coalesced; |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 848 | |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 849 | // Otherwise, sequentially allocate each instruction in the MBB. |
Jakob Stoklund Olesen | 6fb69d8 | 2010-05-17 02:07:22 +0000 | [diff] [blame] | 850 | while (MII != MBB->end()) { |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 851 | MachineInstr *MI = MII++; |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 852 | const MCInstrDesc &MCID = MI->getDesc(); |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 853 | DEBUG({ |
Jakob Stoklund Olesen | c9c4dac | 2010-05-13 20:43:17 +0000 | [diff] [blame] | 854 | dbgs() << "\n>> " << *MI << "Regs:"; |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 855 | for (unsigned Reg = 1, E = TRI->getNumRegs(); Reg != E; ++Reg) { |
| 856 | if (PhysRegState[Reg] == regDisabled) continue; |
| 857 | dbgs() << " " << TRI->getName(Reg); |
| 858 | switch(PhysRegState[Reg]) { |
| 859 | case regFree: |
| 860 | break; |
| 861 | case regReserved: |
Jakob Stoklund Olesen | c9c4dac | 2010-05-13 20:43:17 +0000 | [diff] [blame] | 862 | dbgs() << "*"; |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 863 | break; |
Jakob Stoklund Olesen | a240743 | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 864 | default: { |
Jakob Stoklund Olesen | 4314268 | 2011-01-09 03:05:53 +0000 | [diff] [blame] | 865 | dbgs() << '=' << PrintReg(PhysRegState[Reg]); |
Jakob Stoklund Olesen | a240743 | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 866 | LiveRegMap::iterator I = findLiveVirtReg(PhysRegState[Reg]); |
| 867 | assert(I != LiveVirtRegs.end() && "Missing VirtReg entry"); |
| 868 | if (I->Dirty) |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 869 | dbgs() << "*"; |
Jakob Stoklund Olesen | a240743 | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 870 | assert(I->PhysReg == Reg && "Bad inverse map"); |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 871 | break; |
| 872 | } |
Jakob Stoklund Olesen | a240743 | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 873 | } |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 874 | } |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 875 | dbgs() << '\n'; |
Jakob Stoklund Olesen | 76b4d5a | 2010-05-11 23:24:45 +0000 | [diff] [blame] | 876 | // Check that LiveVirtRegs is the inverse. |
| 877 | for (LiveRegMap::iterator i = LiveVirtRegs.begin(), |
| 878 | e = LiveVirtRegs.end(); i != e; ++i) { |
Jakob Stoklund Olesen | a240743 | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 879 | assert(TargetRegisterInfo::isVirtualRegister(i->VirtReg) && |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 880 | "Bad map key"); |
Jakob Stoklund Olesen | a240743 | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 881 | assert(TargetRegisterInfo::isPhysicalRegister(i->PhysReg) && |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 882 | "Bad map value"); |
Jakob Stoklund Olesen | a240743 | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 883 | assert(PhysRegState[i->PhysReg] == i->VirtReg && "Bad inverse map"); |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 884 | } |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 885 | }); |
| 886 | |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 887 | // Debug values are not allowed to change codegen in any way. |
| 888 | if (MI->isDebugValue()) { |
Devang Patel | 58b8176 | 2010-07-19 23:25:39 +0000 | [diff] [blame] | 889 | bool ScanDbgValue = true; |
| 890 | while (ScanDbgValue) { |
| 891 | ScanDbgValue = false; |
| 892 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 893 | MachineOperand &MO = MI->getOperand(i); |
| 894 | if (!MO.isReg()) continue; |
| 895 | unsigned Reg = MO.getReg(); |
Jakob Stoklund Olesen | c9df025 | 2011-01-10 02:58:51 +0000 | [diff] [blame] | 896 | if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue; |
Jakob Stoklund Olesen | a240743 | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 897 | LiveRegMap::iterator LRI = findLiveVirtReg(Reg); |
Devang Patel | 58b8176 | 2010-07-19 23:25:39 +0000 | [diff] [blame] | 898 | if (LRI != LiveVirtRegs.end()) |
Jakob Stoklund Olesen | a240743 | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 899 | setPhysReg(MI, i, LRI->PhysReg); |
Devang Patel | 7a029b6 | 2010-07-09 21:48:31 +0000 | [diff] [blame] | 900 | else { |
Devang Patel | 58b8176 | 2010-07-19 23:25:39 +0000 | [diff] [blame] | 901 | int SS = StackSlotForVirtReg[Reg]; |
Devang Patel | 4bafda9 | 2010-09-10 20:32:09 +0000 | [diff] [blame] | 902 | if (SS == -1) { |
Jim Grosbach | 07cb689 | 2010-09-01 19:16:29 +0000 | [diff] [blame] | 903 | // We can't allocate a physreg for a DebugValue, sorry! |
Devang Patel | 4bafda9 | 2010-09-10 20:32:09 +0000 | [diff] [blame] | 904 | DEBUG(dbgs() << "Unable to allocate vreg used by DBG_VALUE"); |
Jim Grosbach | 07cb689 | 2010-09-01 19:16:29 +0000 | [diff] [blame] | 905 | MO.setReg(0); |
Devang Patel | 4bafda9 | 2010-09-10 20:32:09 +0000 | [diff] [blame] | 906 | } |
Devang Patel | 58b8176 | 2010-07-19 23:25:39 +0000 | [diff] [blame] | 907 | else { |
| 908 | // Modify DBG_VALUE now that the value is in a spill slot. |
Devang Patel | 459a36b | 2010-08-04 18:42:02 +0000 | [diff] [blame] | 909 | int64_t Offset = MI->getOperand(1).getImm(); |
Jim Grosbach | 07cb689 | 2010-09-01 19:16:29 +0000 | [diff] [blame] | 910 | const MDNode *MDPtr = |
Devang Patel | 58b8176 | 2010-07-19 23:25:39 +0000 | [diff] [blame] | 911 | MI->getOperand(MI->getNumOperands()-1).getMetadata(); |
| 912 | DebugLoc DL = MI->getDebugLoc(); |
Jim Grosbach | 07cb689 | 2010-09-01 19:16:29 +0000 | [diff] [blame] | 913 | if (MachineInstr *NewDV = |
Devang Patel | 58b8176 | 2010-07-19 23:25:39 +0000 | [diff] [blame] | 914 | TII->emitFrameIndexDebugValue(*MF, SS, Offset, MDPtr, DL)) { |
Jim Grosbach | 07cb689 | 2010-09-01 19:16:29 +0000 | [diff] [blame] | 915 | DEBUG(dbgs() << "Modifying debug info due to spill:" << |
| 916 | "\t" << *MI); |
Devang Patel | 58b8176 | 2010-07-19 23:25:39 +0000 | [diff] [blame] | 917 | MachineBasicBlock *MBB = MI->getParent(); |
| 918 | MBB->insert(MBB->erase(MI), NewDV); |
| 919 | // Scan NewDV operands from the beginning. |
| 920 | MI = NewDV; |
| 921 | ScanDbgValue = true; |
| 922 | break; |
Devang Patel | 4bafda9 | 2010-09-10 20:32:09 +0000 | [diff] [blame] | 923 | } else { |
Jim Grosbach | 07cb689 | 2010-09-01 19:16:29 +0000 | [diff] [blame] | 924 | // We can't allocate a physreg for a DebugValue; sorry! |
Devang Patel | 4bafda9 | 2010-09-10 20:32:09 +0000 | [diff] [blame] | 925 | DEBUG(dbgs() << "Unable to allocate vreg used by DBG_VALUE"); |
Jim Grosbach | 07cb689 | 2010-09-01 19:16:29 +0000 | [diff] [blame] | 926 | MO.setReg(0); |
Devang Patel | 4bafda9 | 2010-09-10 20:32:09 +0000 | [diff] [blame] | 927 | } |
Devang Patel | 58b8176 | 2010-07-19 23:25:39 +0000 | [diff] [blame] | 928 | } |
Devang Patel | 7a029b6 | 2010-07-09 21:48:31 +0000 | [diff] [blame] | 929 | } |
Devang Patel | d2df64f | 2011-11-15 21:03:58 +0000 | [diff] [blame] | 930 | LiveDbgValueMap[Reg].push_back(MI); |
Devang Patel | 7a029b6 | 2010-07-09 21:48:31 +0000 | [diff] [blame] | 931 | } |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 932 | } |
| 933 | // Next instruction. |
| 934 | continue; |
| 935 | } |
| 936 | |
Jakob Stoklund Olesen | 4bf4baf | 2010-05-13 00:19:43 +0000 | [diff] [blame] | 937 | // If this is a copy, we may be able to coalesce. |
Jakob Stoklund Olesen | 04c528a | 2010-07-16 04:45:42 +0000 | [diff] [blame] | 938 | unsigned CopySrc = 0, CopyDst = 0, CopySrcSub = 0, CopyDstSub = 0; |
Jakob Stoklund Olesen | 273f7e4 | 2010-07-03 00:04:37 +0000 | [diff] [blame] | 939 | if (MI->isCopy()) { |
| 940 | CopyDst = MI->getOperand(0).getReg(); |
| 941 | CopySrc = MI->getOperand(1).getReg(); |
| 942 | CopyDstSub = MI->getOperand(0).getSubReg(); |
| 943 | CopySrcSub = MI->getOperand(1).getSubReg(); |
Jakob Stoklund Olesen | 04c528a | 2010-07-16 04:45:42 +0000 | [diff] [blame] | 944 | } |
Jakob Stoklund Olesen | 4bf4baf | 2010-05-13 00:19:43 +0000 | [diff] [blame] | 945 | |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 946 | // Track registers used by instruction. |
Jakob Stoklund Olesen | d7ea7d5 | 2012-10-17 01:37:59 +0000 | [diff] [blame] | 947 | UsedInInstr.clear(); |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 948 | |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 949 | // First scan. |
| 950 | // Mark physreg uses and early clobbers as used. |
Jakob Stoklund Olesen | e97dda4 | 2010-05-14 21:55:52 +0000 | [diff] [blame] | 951 | // Find the end of the virtreg operands |
| 952 | unsigned VirtOpEnd = 0; |
Jakob Stoklund Olesen | d1303d2 | 2010-06-29 19:15:30 +0000 | [diff] [blame] | 953 | bool hasTiedOps = false; |
| 954 | bool hasEarlyClobbers = false; |
| 955 | bool hasPartialRedefs = false; |
| 956 | bool hasPhysDefs = false; |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 957 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 958 | MachineOperand &MO = MI->getOperand(i); |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 959 | if (!MO.isReg()) continue; |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 960 | unsigned Reg = MO.getReg(); |
Jakob Stoklund Olesen | e97dda4 | 2010-05-14 21:55:52 +0000 | [diff] [blame] | 961 | if (!Reg) continue; |
| 962 | if (TargetRegisterInfo::isVirtualRegister(Reg)) { |
| 963 | VirtOpEnd = i+1; |
Jakob Stoklund Olesen | d1303d2 | 2010-06-29 19:15:30 +0000 | [diff] [blame] | 964 | if (MO.isUse()) { |
Jakob Stoklund Olesen | d843b39 | 2010-06-28 18:34:34 +0000 | [diff] [blame] | 965 | hasTiedOps = hasTiedOps || |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 966 | MCID.getOperandConstraint(i, MCOI::TIED_TO) != -1; |
Jakob Stoklund Olesen | d1303d2 | 2010-06-29 19:15:30 +0000 | [diff] [blame] | 967 | } else { |
| 968 | if (MO.isEarlyClobber()) |
| 969 | hasEarlyClobbers = true; |
| 970 | if (MO.getSubReg() && MI->readsVirtualRegister(Reg)) |
| 971 | hasPartialRedefs = true; |
| 972 | } |
Jakob Stoklund Olesen | e97dda4 | 2010-05-14 21:55:52 +0000 | [diff] [blame] | 973 | continue; |
| 974 | } |
Jakob Stoklund Olesen | 14d1dd9 | 2012-10-15 22:41:03 +0000 | [diff] [blame] | 975 | if (!MRI->isAllocatable(Reg)) continue; |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 976 | if (MO.isUse()) { |
Jakob Stoklund Olesen | 4ed1082 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 977 | usePhysReg(MO); |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 978 | } else if (MO.isEarlyClobber()) { |
Jakob Stoklund Olesen | 75ac4d9 | 2010-06-15 16:20:57 +0000 | [diff] [blame] | 979 | definePhysReg(MI, Reg, (MO.isImplicit() || MO.isDead()) ? |
| 980 | regFree : regReserved); |
Jakob Stoklund Olesen | d843b39 | 2010-06-28 18:34:34 +0000 | [diff] [blame] | 981 | hasEarlyClobbers = true; |
| 982 | } else |
| 983 | hasPhysDefs = true; |
| 984 | } |
| 985 | |
| 986 | // The instruction may have virtual register operands that must be allocated |
| 987 | // the same register at use-time and def-time: early clobbers and tied |
| 988 | // operands. If there are also physical defs, these registers must avoid |
| 989 | // both physical defs and uses, making them more constrained than normal |
| 990 | // operands. |
Jim Grosbach | 07cb689 | 2010-09-01 19:16:29 +0000 | [diff] [blame] | 991 | // Similarly, if there are multiple defs and tied operands, we must make |
| 992 | // sure the same register is allocated to uses and defs. |
Jakob Stoklund Olesen | d843b39 | 2010-06-28 18:34:34 +0000 | [diff] [blame] | 993 | // We didn't detect inline asm tied operands above, so just make this extra |
| 994 | // pass for all inline asm. |
Jakob Stoklund Olesen | d1303d2 | 2010-06-29 19:15:30 +0000 | [diff] [blame] | 995 | if (MI->isInlineAsm() || hasEarlyClobbers || hasPartialRedefs || |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 996 | (hasTiedOps && (hasPhysDefs || MCID.getNumDefs() > 1))) { |
Jakob Stoklund Olesen | d843b39 | 2010-06-28 18:34:34 +0000 | [diff] [blame] | 997 | handleThroughOperands(MI, VirtDead); |
| 998 | // Don't attempt coalescing when we have funny stuff going on. |
| 999 | CopyDst = 0; |
Jakob Stoklund Olesen | 4bd94f7 | 2010-07-29 00:52:19 +0000 | [diff] [blame] | 1000 | // Pretend we have early clobbers so the use operands get marked below. |
| 1001 | // This is not necessary for the common case of a single tied use. |
| 1002 | hasEarlyClobbers = true; |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 1003 | } |
| 1004 | |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 1005 | // Second scan. |
Jakob Stoklund Olesen | d843b39 | 2010-06-28 18:34:34 +0000 | [diff] [blame] | 1006 | // Allocate virtreg uses. |
Jakob Stoklund Olesen | e97dda4 | 2010-05-14 21:55:52 +0000 | [diff] [blame] | 1007 | for (unsigned i = 0; i != VirtOpEnd; ++i) { |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 1008 | MachineOperand &MO = MI->getOperand(i); |
| 1009 | if (!MO.isReg()) continue; |
| 1010 | unsigned Reg = MO.getReg(); |
Jakob Stoklund Olesen | c9df025 | 2011-01-10 02:58:51 +0000 | [diff] [blame] | 1011 | if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue; |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 1012 | if (MO.isUse()) { |
Jakob Stoklund Olesen | 646dd7c | 2010-05-17 03:26:09 +0000 | [diff] [blame] | 1013 | LiveRegMap::iterator LRI = reloadVirtReg(MI, i, Reg, CopyDst); |
Jakob Stoklund Olesen | a240743 | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 1014 | unsigned PhysReg = LRI->PhysReg; |
Jakob Stoklund Olesen | 7ff82e1 | 2010-05-14 04:30:51 +0000 | [diff] [blame] | 1015 | CopySrc = (CopySrc == Reg || CopySrc == PhysReg) ? PhysReg : 0; |
Jakob Stoklund Olesen | 0eeb05c | 2010-05-18 21:10:50 +0000 | [diff] [blame] | 1016 | if (setPhysReg(MI, i, PhysReg)) |
Jakob Stoklund Olesen | 646dd7c | 2010-05-17 03:26:09 +0000 | [diff] [blame] | 1017 | killVirtReg(LRI); |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 1018 | } |
| 1019 | } |
| 1020 | |
Jakob Stoklund Olesen | d7ea7d5 | 2012-10-17 01:37:59 +0000 | [diff] [blame] | 1021 | for (UsedInInstrSet::iterator |
| 1022 | I = UsedInInstr.begin(), E = UsedInInstr.end(); I != E; ++I) |
| 1023 | MRI->setPhysRegUsed(*I); |
Jakob Stoklund Olesen | 82b07dc | 2010-05-11 20:30:28 +0000 | [diff] [blame] | 1024 | |
Jakob Stoklund Olesen | 4bd94f7 | 2010-07-29 00:52:19 +0000 | [diff] [blame] | 1025 | // Track registers defined by instruction - early clobbers and tied uses at |
| 1026 | // this point. |
Jakob Stoklund Olesen | d7ea7d5 | 2012-10-17 01:37:59 +0000 | [diff] [blame] | 1027 | UsedInInstr.clear(); |
Jakob Stoklund Olesen | d843b39 | 2010-06-28 18:34:34 +0000 | [diff] [blame] | 1028 | if (hasEarlyClobbers) { |
| 1029 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 1030 | MachineOperand &MO = MI->getOperand(i); |
Jakob Stoklund Olesen | 4bd94f7 | 2010-07-29 00:52:19 +0000 | [diff] [blame] | 1031 | if (!MO.isReg()) continue; |
Jakob Stoklund Olesen | d843b39 | 2010-06-28 18:34:34 +0000 | [diff] [blame] | 1032 | unsigned Reg = MO.getReg(); |
| 1033 | if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue; |
Jakob Stoklund Olesen | 4bd94f7 | 2010-07-29 00:52:19 +0000 | [diff] [blame] | 1034 | // Look for physreg defs and tied uses. |
| 1035 | if (!MO.isDef() && !MI->isRegTiedToDefOperand(i)) continue; |
Jakob Stoklund Olesen | 8c70ea4 | 2012-06-01 22:38:17 +0000 | [diff] [blame] | 1036 | for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) |
Jakob Stoklund Olesen | d7ea7d5 | 2012-10-17 01:37:59 +0000 | [diff] [blame] | 1037 | UsedInInstr.insert(*AI); |
Jakob Stoklund Olesen | d843b39 | 2010-06-28 18:34:34 +0000 | [diff] [blame] | 1038 | } |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 1039 | } |
| 1040 | |
Jakob Stoklund Olesen | 4b6bbe8 | 2010-05-17 02:49:18 +0000 | [diff] [blame] | 1041 | unsigned DefOpEnd = MI->getNumOperands(); |
Evan Cheng | 5a96b3d | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 1042 | if (MI->isCall()) { |
Jakob Stoklund Olesen | 4b6bbe8 | 2010-05-17 02:49:18 +0000 | [diff] [blame] | 1043 | // Spill all virtregs before a call. This serves two purposes: 1. If an |
Jim Grosbach | 07cb689 | 2010-09-01 19:16:29 +0000 | [diff] [blame] | 1044 | // exception is thrown, the landing pad is going to expect to find |
| 1045 | // registers in their spill slots, and 2. we don't have to wade through |
| 1046 | // all the <imp-def> operands on the call instruction. |
Jakob Stoklund Olesen | 4b6bbe8 | 2010-05-17 02:49:18 +0000 | [diff] [blame] | 1047 | DefOpEnd = VirtOpEnd; |
| 1048 | DEBUG(dbgs() << " Spilling remaining registers before call.\n"); |
| 1049 | spillAll(MI); |
Jakob Stoklund Olesen | 6de0717 | 2010-06-04 18:08:29 +0000 | [diff] [blame] | 1050 | |
| 1051 | // The imp-defs are skipped below, but we still need to mark those |
| 1052 | // registers as used by the function. |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 1053 | SkippedInstrs.insert(&MCID); |
Jakob Stoklund Olesen | 4b6bbe8 | 2010-05-17 02:49:18 +0000 | [diff] [blame] | 1054 | } |
| 1055 | |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 1056 | // Third scan. |
| 1057 | // Allocate defs and collect dead defs. |
Jakob Stoklund Olesen | 4b6bbe8 | 2010-05-17 02:49:18 +0000 | [diff] [blame] | 1058 | for (unsigned i = 0; i != DefOpEnd; ++i) { |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 1059 | MachineOperand &MO = MI->getOperand(i); |
Jakob Stoklund Olesen | 75ac4d9 | 2010-06-15 16:20:57 +0000 | [diff] [blame] | 1060 | if (!MO.isReg() || !MO.isDef() || !MO.getReg() || MO.isEarlyClobber()) |
| 1061 | continue; |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 1062 | unsigned Reg = MO.getReg(); |
| 1063 | |
| 1064 | if (TargetRegisterInfo::isPhysicalRegister(Reg)) { |
Jakob Stoklund Olesen | 14d1dd9 | 2012-10-15 22:41:03 +0000 | [diff] [blame] | 1065 | if (!MRI->isAllocatable(Reg)) continue; |
Jakob Stoklund Olesen | 6fb69d8 | 2010-05-17 02:07:22 +0000 | [diff] [blame] | 1066 | definePhysReg(MI, Reg, (MO.isImplicit() || MO.isDead()) ? |
| 1067 | regFree : regReserved); |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 1068 | continue; |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 1069 | } |
Jakob Stoklund Olesen | 646dd7c | 2010-05-17 03:26:09 +0000 | [diff] [blame] | 1070 | LiveRegMap::iterator LRI = defineVirtReg(MI, i, Reg, CopySrc); |
Jakob Stoklund Olesen | a240743 | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 1071 | unsigned PhysReg = LRI->PhysReg; |
Jakob Stoklund Olesen | 0eeb05c | 2010-05-18 21:10:50 +0000 | [diff] [blame] | 1072 | if (setPhysReg(MI, i, PhysReg)) { |
| 1073 | VirtDead.push_back(Reg); |
Jakob Stoklund Olesen | 7ff82e1 | 2010-05-14 04:30:51 +0000 | [diff] [blame] | 1074 | CopyDst = 0; // cancel coalescing; |
| 1075 | } else |
| 1076 | CopyDst = (CopyDst == Reg || CopyDst == PhysReg) ? PhysReg : 0; |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 1077 | } |
| 1078 | |
Jakob Stoklund Olesen | 0eeb05c | 2010-05-18 21:10:50 +0000 | [diff] [blame] | 1079 | // Kill dead defs after the scan to ensure that multiple defs of the same |
| 1080 | // register are allocated identically. We didn't need to do this for uses |
| 1081 | // because we are crerating our own kill flags, and they are always at the |
| 1082 | // last use. |
| 1083 | for (unsigned i = 0, e = VirtDead.size(); i != e; ++i) |
| 1084 | killVirtReg(VirtDead[i]); |
| 1085 | VirtDead.clear(); |
| 1086 | |
Jakob Stoklund Olesen | d7ea7d5 | 2012-10-17 01:37:59 +0000 | [diff] [blame] | 1087 | for (UsedInInstrSet::iterator |
| 1088 | I = UsedInInstr.begin(), E = UsedInInstr.end(); I != E; ++I) |
| 1089 | MRI->setPhysRegUsed(*I); |
Jakob Stoklund Olesen | c9c4dac | 2010-05-13 20:43:17 +0000 | [diff] [blame] | 1090 | |
Jakob Stoklund Olesen | 7ff82e1 | 2010-05-14 04:30:51 +0000 | [diff] [blame] | 1091 | if (CopyDst && CopyDst == CopySrc && CopyDstSub == CopySrcSub) { |
| 1092 | DEBUG(dbgs() << "-- coalescing: " << *MI); |
| 1093 | Coalesced.push_back(MI); |
| 1094 | } else { |
| 1095 | DEBUG(dbgs() << "<< " << *MI); |
| 1096 | } |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 1097 | } |
| 1098 | |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 1099 | // Spill all physical registers holding virtual registers now. |
Jakob Stoklund Olesen | e6aba83 | 2010-05-17 02:07:32 +0000 | [diff] [blame] | 1100 | DEBUG(dbgs() << "Spilling live registers at end of block.\n"); |
| 1101 | spillAll(MBB->getFirstTerminator()); |
Jakob Stoklund Olesen | bbf33b3 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 1102 | |
Jakob Stoklund Olesen | 7ff82e1 | 2010-05-14 04:30:51 +0000 | [diff] [blame] | 1103 | // 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] | 1104 | // LiveVirtRegs might refer to the instrs. |
Jakob Stoklund Olesen | 7ff82e1 | 2010-05-14 04:30:51 +0000 | [diff] [blame] | 1105 | for (unsigned i = 0, e = Coalesced.size(); i != e; ++i) |
Jakob Stoklund Olesen | 6fb69d8 | 2010-05-17 02:07:22 +0000 | [diff] [blame] | 1106 | MBB->erase(Coalesced[i]); |
Jakob Stoklund Olesen | 8a65c51 | 2010-05-14 21:55:50 +0000 | [diff] [blame] | 1107 | NumCopies += Coalesced.size(); |
Jakob Stoklund Olesen | 7ff82e1 | 2010-05-14 04:30:51 +0000 | [diff] [blame] | 1108 | |
Andrew Trick | b3d5847 | 2012-01-31 05:55:32 +0000 | [diff] [blame] | 1109 | // addRetOperands must run after we've seen all defs in this block. |
| 1110 | addRetOperands(MBB); |
| 1111 | |
Jakob Stoklund Olesen | 6fb69d8 | 2010-05-17 02:07:22 +0000 | [diff] [blame] | 1112 | DEBUG(MBB->dump()); |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 1113 | } |
| 1114 | |
| 1115 | /// runOnMachineFunction - Register allocate the whole function |
| 1116 | /// |
| 1117 | bool RAFast::runOnMachineFunction(MachineFunction &Fn) { |
Jakob Stoklund Olesen | c9c4dac | 2010-05-13 20:43:17 +0000 | [diff] [blame] | 1118 | DEBUG(dbgs() << "********** FAST REGISTER ALLOCATION **********\n" |
David Blaikie | 986d76d | 2012-08-22 17:18:53 +0000 | [diff] [blame] | 1119 | << "********** Function: " << Fn.getName() << '\n'); |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 1120 | MF = &Fn; |
Jakob Stoklund Olesen | 4bf4baf | 2010-05-13 00:19:43 +0000 | [diff] [blame] | 1121 | MRI = &MF->getRegInfo(); |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 1122 | TM = &Fn.getTarget(); |
| 1123 | TRI = TM->getRegisterInfo(); |
| 1124 | TII = TM->getInstrInfo(); |
Jakob Stoklund Olesen | d9e5c76 | 2012-01-05 00:26:49 +0000 | [diff] [blame] | 1125 | MRI->freezeReservedRegs(Fn); |
Jakob Stoklund Olesen | 5d20c31 | 2011-06-02 18:35:30 +0000 | [diff] [blame] | 1126 | RegClassInfo.runOnMachineFunction(Fn); |
Jakob Stoklund Olesen | d7ea7d5 | 2012-10-17 01:37:59 +0000 | [diff] [blame] | 1127 | UsedInInstr.clear(); |
| 1128 | UsedInInstr.setUniverse(TRI->getNumRegs()); |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 1129 | |
Andrew Trick | 8dd2625 | 2012-02-10 04:10:36 +0000 | [diff] [blame] | 1130 | assert(!MRI->isSSA() && "regalloc requires leaving SSA"); |
| 1131 | |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 1132 | // initialize the virtual->physical register map to have a 'null' |
| 1133 | // mapping for all virtual registers |
Jakob Stoklund Olesen | 42e9c96 | 2011-01-09 21:58:20 +0000 | [diff] [blame] | 1134 | StackSlotForVirtReg.resize(MRI->getNumVirtRegs()); |
Jakob Stoklund Olesen | a240743 | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 1135 | LiveVirtRegs.setUniverse(MRI->getNumVirtRegs()); |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 1136 | |
| 1137 | // Loop over all of the basic blocks, eliminating virtual register references |
Jakob Stoklund Olesen | 6fb69d8 | 2010-05-17 02:07:22 +0000 | [diff] [blame] | 1138 | for (MachineFunction::iterator MBBi = Fn.begin(), MBBe = Fn.end(); |
| 1139 | MBBi != MBBe; ++MBBi) { |
| 1140 | MBB = &*MBBi; |
| 1141 | AllocateBasicBlock(); |
| 1142 | } |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 1143 | |
Jakob Stoklund Olesen | 6de0717 | 2010-06-04 18:08:29 +0000 | [diff] [blame] | 1144 | // Add the clobber lists for all the instructions we skipped earlier. |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 1145 | for (SmallPtrSet<const MCInstrDesc*, 4>::const_iterator |
Jakob Stoklund Olesen | 6de0717 | 2010-06-04 18:08:29 +0000 | [diff] [blame] | 1146 | I = SkippedInstrs.begin(), E = SkippedInstrs.end(); I != E; ++I) |
Craig Topper | fac2598 | 2012-03-08 08:22:45 +0000 | [diff] [blame] | 1147 | if (const uint16_t *Defs = (*I)->getImplicitDefs()) |
Jakob Stoklund Olesen | 6de0717 | 2010-06-04 18:08:29 +0000 | [diff] [blame] | 1148 | while (*Defs) |
| 1149 | MRI->setPhysRegUsed(*Defs++); |
| 1150 | |
Andrew Trick | 19273ae | 2012-02-21 04:51:23 +0000 | [diff] [blame] | 1151 | // All machine operands and other references to virtual registers have been |
| 1152 | // replaced. Remove the virtual registers. |
| 1153 | MRI->clearVirtRegs(); |
| 1154 | |
Jakob Stoklund Olesen | 6de0717 | 2010-06-04 18:08:29 +0000 | [diff] [blame] | 1155 | SkippedInstrs.clear(); |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 1156 | StackSlotForVirtReg.clear(); |
Devang Patel | 459a36b | 2010-08-04 18:42:02 +0000 | [diff] [blame] | 1157 | LiveDbgValueMap.clear(); |
Jakob Stoklund Olesen | 0020723 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 1158 | return true; |
| 1159 | } |
| 1160 | |
| 1161 | FunctionPass *llvm::createFastRegisterAllocator() { |
| 1162 | return new RAFast(); |
| 1163 | } |