Alkis Evlogimenos | 34d9bc9 | 2004-02-23 23:08:11 +0000 | [diff] [blame] | 1 | //===-- llvm/CodeGen/VirtRegMap.cpp - Virtual Register Map ----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Alkis Evlogimenos | 34d9bc9 | 2004-02-23 23:08:11 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 10 | // This file implements the VirtRegMap class. |
| 11 | // |
Dan Gohman | f451cb8 | 2010-02-10 16:03:48 +0000 | [diff] [blame] | 12 | // It also contains implementations of the Spiller interface, which, given a |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 13 | // virtual register map and a machine function, eliminates all virtual |
| 14 | // references by replacing them with physical register references - adding spill |
Alkis Evlogimenos | 0d6c5b6 | 2004-02-24 08:58:30 +0000 | [diff] [blame] | 15 | // code as necessary. |
Alkis Evlogimenos | 34d9bc9 | 2004-02-23 23:08:11 +0000 | [diff] [blame] | 16 | // |
| 17 | //===----------------------------------------------------------------------===// |
| 18 | |
Jakob Stoklund Olesen | 4281e20 | 2012-01-07 07:39:47 +0000 | [diff] [blame] | 19 | #define DEBUG_TYPE "regalloc" |
Alkis Evlogimenos | 34d9bc9 | 2004-02-23 23:08:11 +0000 | [diff] [blame] | 20 | #include "VirtRegMap.h" |
Jakob Stoklund Olesen | 05ec712 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 21 | #include "LiveDebugVariables.h" |
Jakob Stoklund Olesen | 05ec712 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/LiveIntervalAnalysis.h" |
Alkis Evlogimenos | 34d9bc9 | 2004-02-23 23:08:11 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/MachineFunction.h" |
Evan Cheng | 4cce6b4 | 2008-04-11 17:53:36 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Jakob Stoklund Olesen | 05ec712 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/Passes.h" |
Alkis Evlogimenos | 34d9bc9 | 2004-02-23 23:08:11 +0000 | [diff] [blame] | 28 | #include "llvm/Target/TargetMachine.h" |
Alkis Evlogimenos | 0d6c5b6 | 2004-02-24 08:58:30 +0000 | [diff] [blame] | 29 | #include "llvm/Target/TargetInstrInfo.h" |
Mike Stump | fe095f3 | 2009-05-04 18:40:41 +0000 | [diff] [blame] | 30 | #include "llvm/Target/TargetRegisterInfo.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 31 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | a4f0b3a | 2006-08-27 12:54:02 +0000 | [diff] [blame] | 32 | #include "llvm/Support/Compiler.h" |
Evan Cheng | 752272a | 2009-02-11 08:24:21 +0000 | [diff] [blame] | 33 | #include "llvm/Support/Debug.h" |
Daniel Dunbar | 1cd1d98 | 2009-07-24 10:36:58 +0000 | [diff] [blame] | 34 | #include "llvm/Support/raw_ostream.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 35 | #include "llvm/ADT/Statistic.h" |
| 36 | #include "llvm/ADT/STLExtras.h" |
Chris Lattner | 27f2916 | 2004-10-26 15:35:58 +0000 | [diff] [blame] | 37 | #include <algorithm> |
Alkis Evlogimenos | 34d9bc9 | 2004-02-23 23:08:11 +0000 | [diff] [blame] | 38 | using namespace llvm; |
| 39 | |
Jakob Stoklund Olesen | 01afdb3 | 2011-09-15 18:31:13 +0000 | [diff] [blame] | 40 | STATISTIC(NumSpillSlots, "Number of spill slots allocated"); |
| 41 | STATISTIC(NumIdCopies, "Number of identity moves eliminated after rewriting"); |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 42 | |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 43 | //===----------------------------------------------------------------------===// |
| 44 | // VirtRegMap implementation |
| 45 | //===----------------------------------------------------------------------===// |
| 46 | |
Owen Anderson | 49c8aa0 | 2009-03-13 05:55:11 +0000 | [diff] [blame] | 47 | char VirtRegMap::ID = 0; |
| 48 | |
Owen Anderson | ce665bd | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 49 | INITIALIZE_PASS(VirtRegMap, "virtregmap", "Virtual Register Map", false, false) |
Owen Anderson | 49c8aa0 | 2009-03-13 05:55:11 +0000 | [diff] [blame] | 50 | |
| 51 | bool VirtRegMap::runOnMachineFunction(MachineFunction &mf) { |
Evan Cheng | 90f95f8 | 2009-06-14 20:22:55 +0000 | [diff] [blame] | 52 | MRI = &mf.getRegInfo(); |
Owen Anderson | 49c8aa0 | 2009-03-13 05:55:11 +0000 | [diff] [blame] | 53 | TII = mf.getTarget().getInstrInfo(); |
Mike Stump | fe095f3 | 2009-05-04 18:40:41 +0000 | [diff] [blame] | 54 | TRI = mf.getTarget().getRegisterInfo(); |
Owen Anderson | 49c8aa0 | 2009-03-13 05:55:11 +0000 | [diff] [blame] | 55 | MF = &mf; |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 56 | |
Owen Anderson | 49c8aa0 | 2009-03-13 05:55:11 +0000 | [diff] [blame] | 57 | Virt2PhysMap.clear(); |
| 58 | Virt2StackSlotMap.clear(); |
Owen Anderson | 49c8aa0 | 2009-03-13 05:55:11 +0000 | [diff] [blame] | 59 | Virt2SplitMap.clear(); |
Mike Stump | fe095f3 | 2009-05-04 18:40:41 +0000 | [diff] [blame] | 60 | |
Chris Lattner | 2926869 | 2006-09-05 02:12:02 +0000 | [diff] [blame] | 61 | grow(); |
Owen Anderson | 49c8aa0 | 2009-03-13 05:55:11 +0000 | [diff] [blame] | 62 | return false; |
Chris Lattner | 2926869 | 2006-09-05 02:12:02 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 65 | void VirtRegMap::grow() { |
Jakob Stoklund Olesen | 42e9c96 | 2011-01-09 21:58:20 +0000 | [diff] [blame] | 66 | unsigned NumRegs = MF->getRegInfo().getNumVirtRegs(); |
| 67 | Virt2PhysMap.resize(NumRegs); |
| 68 | Virt2StackSlotMap.resize(NumRegs); |
Jakob Stoklund Olesen | 42e9c96 | 2011-01-09 21:58:20 +0000 | [diff] [blame] | 69 | Virt2SplitMap.resize(NumRegs); |
Alkis Evlogimenos | 34d9bc9 | 2004-02-23 23:08:11 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Jakob Stoklund Olesen | b55e91e | 2010-11-16 00:41:01 +0000 | [diff] [blame] | 72 | unsigned VirtRegMap::createSpillSlot(const TargetRegisterClass *RC) { |
| 73 | int SS = MF->getFrameInfo()->CreateSpillStackObject(RC->getSize(), |
| 74 | RC->getAlignment()); |
Jakob Stoklund Olesen | 01afdb3 | 2011-09-15 18:31:13 +0000 | [diff] [blame] | 75 | ++NumSpillSlots; |
Jakob Stoklund Olesen | b55e91e | 2010-11-16 00:41:01 +0000 | [diff] [blame] | 76 | return SS; |
| 77 | } |
| 78 | |
Evan Cheng | 90f95f8 | 2009-06-14 20:22:55 +0000 | [diff] [blame] | 79 | unsigned VirtRegMap::getRegAllocPref(unsigned virtReg) { |
Evan Cheng | 358dec5 | 2009-06-15 08:28:29 +0000 | [diff] [blame] | 80 | std::pair<unsigned, unsigned> Hint = MRI->getRegAllocationHint(virtReg); |
| 81 | unsigned physReg = Hint.second; |
Jakob Stoklund Olesen | c9df025 | 2011-01-10 02:58:51 +0000 | [diff] [blame] | 82 | if (TargetRegisterInfo::isVirtualRegister(physReg) && hasPhys(physReg)) |
Evan Cheng | 358dec5 | 2009-06-15 08:28:29 +0000 | [diff] [blame] | 83 | physReg = getPhys(physReg); |
| 84 | if (Hint.first == 0) |
Jakob Stoklund Olesen | c9df025 | 2011-01-10 02:58:51 +0000 | [diff] [blame] | 85 | return (TargetRegisterInfo::isPhysicalRegister(physReg)) |
Evan Cheng | 358dec5 | 2009-06-15 08:28:29 +0000 | [diff] [blame] | 86 | ? physReg : 0; |
| 87 | return TRI->ResolveRegAllocHint(Hint.first, physReg, *MF); |
Evan Cheng | 90f95f8 | 2009-06-14 20:22:55 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 90 | int VirtRegMap::assignVirt2StackSlot(unsigned virtReg) { |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 91 | assert(TargetRegisterInfo::isVirtualRegister(virtReg)); |
Chris Lattner | 7f690e6 | 2004-09-30 02:15:18 +0000 | [diff] [blame] | 92 | assert(Virt2StackSlotMap[virtReg] == NO_STACK_SLOT && |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 93 | "attempt to assign stack slot to already spilled register"); |
Owen Anderson | 49c8aa0 | 2009-03-13 05:55:11 +0000 | [diff] [blame] | 94 | const TargetRegisterClass* RC = MF->getRegInfo().getRegClass(virtReg); |
Jakob Stoklund Olesen | b55e91e | 2010-11-16 00:41:01 +0000 | [diff] [blame] | 95 | return Virt2StackSlotMap[virtReg] = createSpillSlot(RC); |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Evan Cheng | d365312 | 2008-02-27 03:04:06 +0000 | [diff] [blame] | 98 | void VirtRegMap::assignVirt2StackSlot(unsigned virtReg, int SS) { |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 99 | assert(TargetRegisterInfo::isVirtualRegister(virtReg)); |
Chris Lattner | 7f690e6 | 2004-09-30 02:15:18 +0000 | [diff] [blame] | 100 | assert(Virt2StackSlotMap[virtReg] == NO_STACK_SLOT && |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 101 | "attempt to assign stack slot to already spilled register"); |
Evan Cheng | d365312 | 2008-02-27 03:04:06 +0000 | [diff] [blame] | 102 | assert((SS >= 0 || |
Owen Anderson | 49c8aa0 | 2009-03-13 05:55:11 +0000 | [diff] [blame] | 103 | (SS >= MF->getFrameInfo()->getObjectIndexBegin())) && |
Evan Cheng | 9193514 | 2007-04-04 07:40:01 +0000 | [diff] [blame] | 104 | "illegal fixed frame index"); |
Evan Cheng | d365312 | 2008-02-27 03:04:06 +0000 | [diff] [blame] | 105 | Virt2StackSlotMap[virtReg] = SS; |
Alkis Evlogimenos | 38af59a | 2004-05-29 20:38:05 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Jakob Stoklund Olesen | 05ec712 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 108 | void VirtRegMap::print(raw_ostream &OS, const Module*) const { |
| 109 | OS << "********** REGISTER MAP **********\n"; |
| 110 | for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) { |
| 111 | unsigned Reg = TargetRegisterInfo::index2VirtReg(i); |
| 112 | if (Virt2PhysMap[Reg] != (unsigned)VirtRegMap::NO_PHYS_REG) { |
| 113 | OS << '[' << PrintReg(Reg, TRI) << " -> " |
| 114 | << PrintReg(Virt2PhysMap[Reg], TRI) << "] " |
| 115 | << MRI->getRegClass(Reg)->getName() << "\n"; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) { |
| 120 | unsigned Reg = TargetRegisterInfo::index2VirtReg(i); |
| 121 | if (Virt2StackSlotMap[Reg] != VirtRegMap::NO_STACK_SLOT) { |
| 122 | OS << '[' << PrintReg(Reg, TRI) << " -> fi#" << Virt2StackSlotMap[Reg] |
| 123 | << "] " << MRI->getRegClass(Reg)->getName() << "\n"; |
| 124 | } |
| 125 | } |
| 126 | OS << '\n'; |
| 127 | } |
| 128 | |
| 129 | void VirtRegMap::dump() const { |
| 130 | print(dbgs()); |
| 131 | } |
| 132 | |
| 133 | //===----------------------------------------------------------------------===// |
| 134 | // VirtRegRewriter |
| 135 | //===----------------------------------------------------------------------===// |
| 136 | // |
| 137 | // The VirtRegRewriter is the last of the register allocator passes. |
| 138 | // It rewrites virtual registers to physical registers as specified in the |
| 139 | // VirtRegMap analysis. It also updates live-in information on basic blocks |
| 140 | // according to LiveIntervals. |
| 141 | // |
| 142 | namespace { |
| 143 | class VirtRegRewriter : public MachineFunctionPass { |
| 144 | MachineFunction *MF; |
| 145 | const TargetMachine *TM; |
| 146 | const TargetRegisterInfo *TRI; |
| 147 | const TargetInstrInfo *TII; |
| 148 | MachineRegisterInfo *MRI; |
| 149 | SlotIndexes *Indexes; |
| 150 | LiveIntervals *LIS; |
| 151 | VirtRegMap *VRM; |
| 152 | |
| 153 | void rewrite(); |
| 154 | void addMBBLiveIns(); |
| 155 | public: |
| 156 | static char ID; |
| 157 | VirtRegRewriter() : MachineFunctionPass(ID) {} |
| 158 | |
| 159 | virtual void getAnalysisUsage(AnalysisUsage &AU) const; |
| 160 | |
| 161 | virtual bool runOnMachineFunction(MachineFunction&); |
| 162 | }; |
| 163 | } // end anonymous namespace |
| 164 | |
| 165 | char &llvm::VirtRegRewriterID = VirtRegRewriter::ID; |
| 166 | |
| 167 | INITIALIZE_PASS_BEGIN(VirtRegRewriter, "virtregrewriter", |
| 168 | "Virtual Register Rewriter", false, false) |
| 169 | INITIALIZE_PASS_DEPENDENCY(SlotIndexes) |
| 170 | INITIALIZE_PASS_DEPENDENCY(LiveIntervals) |
| 171 | INITIALIZE_PASS_DEPENDENCY(LiveDebugVariables) |
| 172 | INITIALIZE_PASS_DEPENDENCY(VirtRegMap) |
| 173 | INITIALIZE_PASS_END(VirtRegRewriter, "virtregrewriter", |
| 174 | "Virtual Register Rewriter", false, false) |
| 175 | |
| 176 | char VirtRegRewriter::ID = 0; |
| 177 | |
| 178 | void VirtRegRewriter::getAnalysisUsage(AnalysisUsage &AU) const { |
| 179 | AU.setPreservesCFG(); |
| 180 | AU.addRequired<LiveIntervals>(); |
| 181 | AU.addRequired<SlotIndexes>(); |
| 182 | AU.addPreserved<SlotIndexes>(); |
| 183 | AU.addRequired<LiveDebugVariables>(); |
| 184 | AU.addRequired<VirtRegMap>(); |
| 185 | MachineFunctionPass::getAnalysisUsage(AU); |
| 186 | } |
| 187 | |
| 188 | bool VirtRegRewriter::runOnMachineFunction(MachineFunction &fn) { |
| 189 | MF = &fn; |
| 190 | TM = &MF->getTarget(); |
| 191 | TRI = TM->getRegisterInfo(); |
| 192 | TII = TM->getInstrInfo(); |
| 193 | MRI = &MF->getRegInfo(); |
| 194 | Indexes = &getAnalysis<SlotIndexes>(); |
| 195 | LIS = &getAnalysis<LiveIntervals>(); |
| 196 | VRM = &getAnalysis<VirtRegMap>(); |
Jakob Stoklund Olesen | ba05c01 | 2011-02-18 22:03:18 +0000 | [diff] [blame] | 197 | DEBUG(dbgs() << "********** REWRITE VIRTUAL REGISTERS **********\n" |
| 198 | << "********** Function: " |
Craig Topper | 96601ca | 2012-08-22 06:07:19 +0000 | [diff] [blame] | 199 | << MF->getName() << '\n'); |
Jakob Stoklund Olesen | 05ec712 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 200 | DEBUG(VRM->dump()); |
| 201 | |
| 202 | // Add kill flags while we still have virtual registers. |
Jakob Stoklund Olesen | e617ccb | 2012-09-06 18:15:18 +0000 | [diff] [blame] | 203 | LIS->addKillFlags(VRM); |
Jakob Stoklund Olesen | 05ec712 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 204 | |
Jakob Stoklund Olesen | fe17bdb | 2012-06-09 00:14:47 +0000 | [diff] [blame] | 205 | // Live-in lists on basic blocks are required for physregs. |
| 206 | addMBBLiveIns(); |
| 207 | |
Jakob Stoklund Olesen | 05ec712 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 208 | // Rewrite virtual registers. |
| 209 | rewrite(); |
| 210 | |
| 211 | // Write out new DBG_VALUE instructions. |
| 212 | getAnalysis<LiveDebugVariables>().emitDebugValues(VRM); |
| 213 | |
| 214 | // All machine operands and other references to virtual registers have been |
| 215 | // replaced. Remove the virtual registers and release all the transient data. |
| 216 | VRM->clearAllVirt(); |
| 217 | MRI->clearVirtRegs(); |
| 218 | return true; |
| 219 | } |
| 220 | |
Jakob Stoklund Olesen | fe17bdb | 2012-06-09 00:14:47 +0000 | [diff] [blame] | 221 | // Compute MBB live-in lists from virtual register live ranges and their |
| 222 | // assignments. |
| 223 | void VirtRegRewriter::addMBBLiveIns() { |
| 224 | SmallVector<MachineBasicBlock*, 16> LiveIn; |
| 225 | for (unsigned Idx = 0, IdxE = MRI->getNumVirtRegs(); Idx != IdxE; ++Idx) { |
| 226 | unsigned VirtReg = TargetRegisterInfo::index2VirtReg(Idx); |
| 227 | if (MRI->reg_nodbg_empty(VirtReg)) |
| 228 | continue; |
| 229 | LiveInterval &LI = LIS->getInterval(VirtReg); |
| 230 | if (LI.empty() || LIS->intervalIsInOneMBB(LI)) |
| 231 | continue; |
| 232 | // This is a virtual register that is live across basic blocks. Its |
| 233 | // assigned PhysReg must be marked as live-in to those blocks. |
| 234 | unsigned PhysReg = VRM->getPhys(VirtReg); |
| 235 | assert(PhysReg != VirtRegMap::NO_PHYS_REG && "Unmapped virtual register."); |
| 236 | |
| 237 | // Scan the segments of LI. |
| 238 | for (LiveInterval::const_iterator I = LI.begin(), E = LI.end(); I != E; |
| 239 | ++I) { |
| 240 | if (!Indexes->findLiveInMBBs(I->start, I->end, LiveIn)) |
| 241 | continue; |
| 242 | for (unsigned i = 0, e = LiveIn.size(); i != e; ++i) |
| 243 | if (!LiveIn[i]->isLiveIn(PhysReg)) |
| 244 | LiveIn[i]->addLiveIn(PhysReg); |
| 245 | LiveIn.clear(); |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | |
Jakob Stoklund Olesen | 05ec712 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 250 | void VirtRegRewriter::rewrite() { |
Jakob Stoklund Olesen | 93e110b | 2011-04-27 17:42:31 +0000 | [diff] [blame] | 251 | SmallVector<unsigned, 8> SuperDeads; |
| 252 | SmallVector<unsigned, 8> SuperDefs; |
Jakob Stoklund Olesen | ba05c01 | 2011-02-18 22:03:18 +0000 | [diff] [blame] | 253 | SmallVector<unsigned, 8> SuperKills; |
Jakob Stoklund Olesen | 2d44e02 | 2012-01-03 22:34:31 +0000 | [diff] [blame] | 254 | #ifndef NDEBUG |
| 255 | BitVector Reserved = TRI->getReservedRegs(*MF); |
| 256 | #endif |
Jakob Stoklund Olesen | ba05c01 | 2011-02-18 22:03:18 +0000 | [diff] [blame] | 257 | |
| 258 | for (MachineFunction::iterator MBBI = MF->begin(), MBBE = MF->end(); |
| 259 | MBBI != MBBE; ++MBBI) { |
| 260 | DEBUG(MBBI->print(dbgs(), Indexes)); |
Evan Cheng | 3f9c251 | 2012-01-19 07:46:36 +0000 | [diff] [blame] | 261 | for (MachineBasicBlock::instr_iterator |
| 262 | MII = MBBI->instr_begin(), MIE = MBBI->instr_end(); MII != MIE;) { |
Jakob Stoklund Olesen | ba05c01 | 2011-02-18 22:03:18 +0000 | [diff] [blame] | 263 | MachineInstr *MI = MII; |
| 264 | ++MII; |
| 265 | |
| 266 | for (MachineInstr::mop_iterator MOI = MI->operands_begin(), |
| 267 | MOE = MI->operands_end(); MOI != MOE; ++MOI) { |
| 268 | MachineOperand &MO = *MOI; |
Jakob Stoklund Olesen | d9f0ff5 | 2012-02-17 19:07:56 +0000 | [diff] [blame] | 269 | |
| 270 | // Make sure MRI knows about registers clobbered by regmasks. |
| 271 | if (MO.isRegMask()) |
| 272 | MRI->addPhysRegsUsedFromRegMask(MO.getRegMask()); |
| 273 | |
Jakob Stoklund Olesen | ba05c01 | 2011-02-18 22:03:18 +0000 | [diff] [blame] | 274 | if (!MO.isReg() || !TargetRegisterInfo::isVirtualRegister(MO.getReg())) |
| 275 | continue; |
| 276 | unsigned VirtReg = MO.getReg(); |
Jakob Stoklund Olesen | 05ec712 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 277 | unsigned PhysReg = VRM->getPhys(VirtReg); |
| 278 | assert(PhysReg != VirtRegMap::NO_PHYS_REG && |
| 279 | "Instruction uses unmapped VirtReg"); |
Jakob Stoklund Olesen | 2d44e02 | 2012-01-03 22:34:31 +0000 | [diff] [blame] | 280 | assert(!Reserved.test(PhysReg) && "Reserved register assignment"); |
Jakob Stoklund Olesen | ba05c01 | 2011-02-18 22:03:18 +0000 | [diff] [blame] | 281 | |
| 282 | // Preserve semantics of sub-register operands. |
| 283 | if (MO.getSubReg()) { |
| 284 | // A virtual register kill refers to the whole register, so we may |
Jakob Stoklund Olesen | 200a8ce | 2011-10-05 00:01:48 +0000 | [diff] [blame] | 285 | // have to add <imp-use,kill> operands for the super-register. A |
| 286 | // partial redef always kills and redefines the super-register. |
| 287 | if (MO.readsReg() && (MO.isDef() || MO.isKill())) |
| 288 | SuperKills.push_back(PhysReg); |
| 289 | |
| 290 | if (MO.isDef()) { |
| 291 | // The <def,undef> flag only makes sense for sub-register defs, and |
| 292 | // we are substituting a full physreg. An <imp-use,kill> operand |
| 293 | // from the SuperKills list will represent the partial read of the |
| 294 | // super-register. |
| 295 | MO.setIsUndef(false); |
| 296 | |
| 297 | // Also add implicit defs for the super-register. |
| 298 | if (MO.isDead()) |
| 299 | SuperDeads.push_back(PhysReg); |
| 300 | else |
| 301 | SuperDefs.push_back(PhysReg); |
| 302 | } |
Jakob Stoklund Olesen | ba05c01 | 2011-02-18 22:03:18 +0000 | [diff] [blame] | 303 | |
| 304 | // PhysReg operands cannot have subregister indexes. |
| 305 | PhysReg = TRI->getSubReg(PhysReg, MO.getSubReg()); |
| 306 | assert(PhysReg && "Invalid SubReg for physical register"); |
| 307 | MO.setSubReg(0); |
| 308 | } |
| 309 | // Rewrite. Note we could have used MachineOperand::substPhysReg(), but |
| 310 | // we need the inlining here. |
| 311 | MO.setReg(PhysReg); |
| 312 | } |
| 313 | |
| 314 | // Add any missing super-register kills after rewriting the whole |
| 315 | // instruction. |
| 316 | while (!SuperKills.empty()) |
| 317 | MI->addRegisterKilled(SuperKills.pop_back_val(), TRI, true); |
| 318 | |
Jakob Stoklund Olesen | 93e110b | 2011-04-27 17:42:31 +0000 | [diff] [blame] | 319 | while (!SuperDeads.empty()) |
| 320 | MI->addRegisterDead(SuperDeads.pop_back_val(), TRI, true); |
| 321 | |
| 322 | while (!SuperDefs.empty()) |
| 323 | MI->addRegisterDefined(SuperDefs.pop_back_val(), TRI); |
| 324 | |
Jakob Stoklund Olesen | ba05c01 | 2011-02-18 22:03:18 +0000 | [diff] [blame] | 325 | DEBUG(dbgs() << "> " << *MI); |
| 326 | |
| 327 | // Finally, remove any identity copies. |
| 328 | if (MI->isIdentityCopy()) { |
Jakob Stoklund Olesen | cf5e5f3 | 2011-05-06 17:59:57 +0000 | [diff] [blame] | 329 | ++NumIdCopies; |
Jakob Stoklund Olesen | 280ea1a | 2011-03-31 17:55:25 +0000 | [diff] [blame] | 330 | if (MI->getNumOperands() == 2) { |
| 331 | DEBUG(dbgs() << "Deleting identity copy.\n"); |
Jakob Stoklund Olesen | 280ea1a | 2011-03-31 17:55:25 +0000 | [diff] [blame] | 332 | if (Indexes) |
| 333 | Indexes->removeMachineInstrFromMaps(MI); |
| 334 | // It's safe to erase MI because MII has already been incremented. |
| 335 | MI->eraseFromParent(); |
| 336 | } else { |
| 337 | // Transform identity copy to a KILL to deal with subregisters. |
| 338 | MI->setDesc(TII->get(TargetOpcode::KILL)); |
| 339 | DEBUG(dbgs() << "Identity copy: " << *MI); |
| 340 | } |
Jakob Stoklund Olesen | ba05c01 | 2011-02-18 22:03:18 +0000 | [diff] [blame] | 341 | } |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | // Tell MRI about physical registers in use. |
| 346 | for (unsigned Reg = 1, RegE = TRI->getNumRegs(); Reg != RegE; ++Reg) |
| 347 | if (!MRI->reg_nodbg_empty(Reg)) |
| 348 | MRI->setPhysRegUsed(Reg); |
| 349 | } |