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