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" |
Jakob Stoklund Olesen | 1ead68d | 2012-11-28 19:13:06 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/VirtRegMap.h" |
Jakob Stoklund Olesen | 05ec712 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 21 | #include "LiveDebugVariables.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/STLExtras.h" |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 23 | #include "llvm/ADT/SparseSet.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/Statistic.h" |
Jakob Stoklund Olesen | 05ec712 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/LiveIntervalAnalysis.h" |
Evan Cheng | bb36a43 | 2012-09-21 20:04:28 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/LiveStackAnalysis.h" |
Alkis Evlogimenos | 34d9bc9 | 2004-02-23 23:08:11 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/MachineFunction.h" |
Evan Cheng | 4cce6b4 | 2008-04-11 17:53:36 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 30 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Jakob Stoklund Olesen | 05ec712 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/Passes.h" |
Quentin Colombet | ce734f1 | 2013-09-25 00:26:17 +0000 | [diff] [blame] | 32 | #include "llvm/IR/Function.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 33 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | a4f0b3a | 2006-08-27 12:54:02 +0000 | [diff] [blame] | 34 | #include "llvm/Support/Compiler.h" |
Evan Cheng | 752272a | 2009-02-11 08:24:21 +0000 | [diff] [blame] | 35 | #include "llvm/Support/Debug.h" |
Daniel Dunbar | 1cd1d98 | 2009-07-24 10:36:58 +0000 | [diff] [blame] | 36 | #include "llvm/Support/raw_ostream.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 37 | #include "llvm/Target/TargetInstrInfo.h" |
| 38 | #include "llvm/Target/TargetMachine.h" |
| 39 | #include "llvm/Target/TargetRegisterInfo.h" |
Chris Lattner | 27f2916 | 2004-10-26 15:35:58 +0000 | [diff] [blame] | 40 | #include <algorithm> |
Alkis Evlogimenos | 34d9bc9 | 2004-02-23 23:08:11 +0000 | [diff] [blame] | 41 | using namespace llvm; |
| 42 | |
Jakob Stoklund Olesen | 01afdb3 | 2011-09-15 18:31:13 +0000 | [diff] [blame] | 43 | STATISTIC(NumSpillSlots, "Number of spill slots allocated"); |
| 44 | STATISTIC(NumIdCopies, "Number of identity moves eliminated after rewriting"); |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 45 | |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 46 | //===----------------------------------------------------------------------===// |
| 47 | // VirtRegMap implementation |
| 48 | //===----------------------------------------------------------------------===// |
| 49 | |
Owen Anderson | 49c8aa0 | 2009-03-13 05:55:11 +0000 | [diff] [blame] | 50 | char VirtRegMap::ID = 0; |
| 51 | |
Owen Anderson | ce665bd | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 52 | INITIALIZE_PASS(VirtRegMap, "virtregmap", "Virtual Register Map", false, false) |
Owen Anderson | 49c8aa0 | 2009-03-13 05:55:11 +0000 | [diff] [blame] | 53 | |
| 54 | bool VirtRegMap::runOnMachineFunction(MachineFunction &mf) { |
Evan Cheng | 90f95f8 | 2009-06-14 20:22:55 +0000 | [diff] [blame] | 55 | MRI = &mf.getRegInfo(); |
Owen Anderson | 49c8aa0 | 2009-03-13 05:55:11 +0000 | [diff] [blame] | 56 | TII = mf.getTarget().getInstrInfo(); |
Mike Stump | fe095f3 | 2009-05-04 18:40:41 +0000 | [diff] [blame] | 57 | TRI = mf.getTarget().getRegisterInfo(); |
Owen Anderson | 49c8aa0 | 2009-03-13 05:55:11 +0000 | [diff] [blame] | 58 | MF = &mf; |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 59 | |
Owen Anderson | 49c8aa0 | 2009-03-13 05:55:11 +0000 | [diff] [blame] | 60 | Virt2PhysMap.clear(); |
| 61 | Virt2StackSlotMap.clear(); |
Owen Anderson | 49c8aa0 | 2009-03-13 05:55:11 +0000 | [diff] [blame] | 62 | Virt2SplitMap.clear(); |
Mike Stump | fe095f3 | 2009-05-04 18:40:41 +0000 | [diff] [blame] | 63 | |
Chris Lattner | 2926869 | 2006-09-05 02:12:02 +0000 | [diff] [blame] | 64 | grow(); |
Owen Anderson | 49c8aa0 | 2009-03-13 05:55:11 +0000 | [diff] [blame] | 65 | return false; |
Chris Lattner | 2926869 | 2006-09-05 02:12:02 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 68 | void VirtRegMap::grow() { |
Jakob Stoklund Olesen | 42e9c96 | 2011-01-09 21:58:20 +0000 | [diff] [blame] | 69 | unsigned NumRegs = MF->getRegInfo().getNumVirtRegs(); |
| 70 | Virt2PhysMap.resize(NumRegs); |
| 71 | Virt2StackSlotMap.resize(NumRegs); |
Jakob Stoklund Olesen | 42e9c96 | 2011-01-09 21:58:20 +0000 | [diff] [blame] | 72 | Virt2SplitMap.resize(NumRegs); |
Alkis Evlogimenos | 34d9bc9 | 2004-02-23 23:08:11 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Jakob Stoklund Olesen | b55e91e | 2010-11-16 00:41:01 +0000 | [diff] [blame] | 75 | unsigned VirtRegMap::createSpillSlot(const TargetRegisterClass *RC) { |
| 76 | int SS = MF->getFrameInfo()->CreateSpillStackObject(RC->getSize(), |
| 77 | RC->getAlignment()); |
Jakob Stoklund Olesen | 01afdb3 | 2011-09-15 18:31:13 +0000 | [diff] [blame] | 78 | ++NumSpillSlots; |
Jakob Stoklund Olesen | b55e91e | 2010-11-16 00:41:01 +0000 | [diff] [blame] | 79 | return SS; |
| 80 | } |
| 81 | |
Jakob Stoklund Olesen | 980bddf | 2012-12-04 00:30:22 +0000 | [diff] [blame] | 82 | bool VirtRegMap::hasPreferredPhys(unsigned VirtReg) { |
| 83 | unsigned Hint = MRI->getSimpleHint(VirtReg); |
| 84 | if (!Hint) |
| 85 | return 0; |
| 86 | if (TargetRegisterInfo::isVirtualRegister(Hint)) |
| 87 | Hint = getPhys(Hint); |
| 88 | return getPhys(VirtReg) == Hint; |
| 89 | } |
| 90 | |
Jakob Stoklund Olesen | fc63744 | 2012-12-03 23:23:50 +0000 | [diff] [blame] | 91 | bool VirtRegMap::hasKnownPreference(unsigned VirtReg) { |
| 92 | std::pair<unsigned, unsigned> Hint = MRI->getRegAllocationHint(VirtReg); |
| 93 | if (TargetRegisterInfo::isPhysicalRegister(Hint.second)) |
| 94 | return true; |
| 95 | if (TargetRegisterInfo::isVirtualRegister(Hint.second)) |
| 96 | return hasPhys(Hint.second); |
| 97 | return false; |
| 98 | } |
| 99 | |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 100 | int VirtRegMap::assignVirt2StackSlot(unsigned virtReg) { |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 101 | assert(TargetRegisterInfo::isVirtualRegister(virtReg)); |
Chris Lattner | 7f690e6 | 2004-09-30 02:15:18 +0000 | [diff] [blame] | 102 | assert(Virt2StackSlotMap[virtReg] == NO_STACK_SLOT && |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 103 | "attempt to assign stack slot to already spilled register"); |
Owen Anderson | 49c8aa0 | 2009-03-13 05:55:11 +0000 | [diff] [blame] | 104 | const TargetRegisterClass* RC = MF->getRegInfo().getRegClass(virtReg); |
Jakob Stoklund Olesen | b55e91e | 2010-11-16 00:41:01 +0000 | [diff] [blame] | 105 | return Virt2StackSlotMap[virtReg] = createSpillSlot(RC); |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Evan Cheng | d365312 | 2008-02-27 03:04:06 +0000 | [diff] [blame] | 108 | void VirtRegMap::assignVirt2StackSlot(unsigned virtReg, int SS) { |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 109 | assert(TargetRegisterInfo::isVirtualRegister(virtReg)); |
Chris Lattner | 7f690e6 | 2004-09-30 02:15:18 +0000 | [diff] [blame] | 110 | assert(Virt2StackSlotMap[virtReg] == NO_STACK_SLOT && |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 111 | "attempt to assign stack slot to already spilled register"); |
Evan Cheng | d365312 | 2008-02-27 03:04:06 +0000 | [diff] [blame] | 112 | assert((SS >= 0 || |
Owen Anderson | 49c8aa0 | 2009-03-13 05:55:11 +0000 | [diff] [blame] | 113 | (SS >= MF->getFrameInfo()->getObjectIndexBegin())) && |
Evan Cheng | 9193514 | 2007-04-04 07:40:01 +0000 | [diff] [blame] | 114 | "illegal fixed frame index"); |
Evan Cheng | d365312 | 2008-02-27 03:04:06 +0000 | [diff] [blame] | 115 | Virt2StackSlotMap[virtReg] = SS; |
Alkis Evlogimenos | 38af59a | 2004-05-29 20:38:05 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Jakob Stoklund Olesen | 05ec712 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 118 | void VirtRegMap::print(raw_ostream &OS, const Module*) const { |
| 119 | OS << "********** REGISTER MAP **********\n"; |
| 120 | for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) { |
| 121 | unsigned Reg = TargetRegisterInfo::index2VirtReg(i); |
| 122 | if (Virt2PhysMap[Reg] != (unsigned)VirtRegMap::NO_PHYS_REG) { |
| 123 | OS << '[' << PrintReg(Reg, TRI) << " -> " |
| 124 | << PrintReg(Virt2PhysMap[Reg], TRI) << "] " |
| 125 | << MRI->getRegClass(Reg)->getName() << "\n"; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) { |
| 130 | unsigned Reg = TargetRegisterInfo::index2VirtReg(i); |
| 131 | if (Virt2StackSlotMap[Reg] != VirtRegMap::NO_STACK_SLOT) { |
| 132 | OS << '[' << PrintReg(Reg, TRI) << " -> fi#" << Virt2StackSlotMap[Reg] |
| 133 | << "] " << MRI->getRegClass(Reg)->getName() << "\n"; |
| 134 | } |
| 135 | } |
| 136 | OS << '\n'; |
| 137 | } |
| 138 | |
Manman Ren | b720be6 | 2012-09-11 22:23:19 +0000 | [diff] [blame] | 139 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Jakob Stoklund Olesen | 05ec712 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 140 | void VirtRegMap::dump() const { |
| 141 | print(dbgs()); |
| 142 | } |
Manman Ren | 77e300e | 2012-09-06 19:06:06 +0000 | [diff] [blame] | 143 | #endif |
Jakob Stoklund Olesen | 05ec712 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 144 | |
| 145 | //===----------------------------------------------------------------------===// |
| 146 | // VirtRegRewriter |
| 147 | //===----------------------------------------------------------------------===// |
| 148 | // |
| 149 | // The VirtRegRewriter is the last of the register allocator passes. |
| 150 | // It rewrites virtual registers to physical registers as specified in the |
| 151 | // VirtRegMap analysis. It also updates live-in information on basic blocks |
| 152 | // according to LiveIntervals. |
| 153 | // |
| 154 | namespace { |
| 155 | class VirtRegRewriter : public MachineFunctionPass { |
| 156 | MachineFunction *MF; |
| 157 | const TargetMachine *TM; |
| 158 | const TargetRegisterInfo *TRI; |
| 159 | const TargetInstrInfo *TII; |
| 160 | MachineRegisterInfo *MRI; |
| 161 | SlotIndexes *Indexes; |
| 162 | LiveIntervals *LIS; |
| 163 | VirtRegMap *VRM; |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 164 | SparseSet<unsigned> PhysRegs; |
Jakob Stoklund Olesen | 05ec712 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 165 | |
| 166 | void rewrite(); |
| 167 | void addMBBLiveIns(); |
| 168 | public: |
| 169 | static char ID; |
| 170 | VirtRegRewriter() : MachineFunctionPass(ID) {} |
| 171 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 172 | void getAnalysisUsage(AnalysisUsage &AU) const override; |
Jakob Stoklund Olesen | 05ec712 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 173 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 174 | bool runOnMachineFunction(MachineFunction&) override; |
Jakob Stoklund Olesen | 05ec712 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 175 | }; |
| 176 | } // end anonymous namespace |
| 177 | |
| 178 | char &llvm::VirtRegRewriterID = VirtRegRewriter::ID; |
| 179 | |
| 180 | INITIALIZE_PASS_BEGIN(VirtRegRewriter, "virtregrewriter", |
| 181 | "Virtual Register Rewriter", false, false) |
| 182 | INITIALIZE_PASS_DEPENDENCY(SlotIndexes) |
| 183 | INITIALIZE_PASS_DEPENDENCY(LiveIntervals) |
| 184 | INITIALIZE_PASS_DEPENDENCY(LiveDebugVariables) |
Evan Cheng | bb36a43 | 2012-09-21 20:04:28 +0000 | [diff] [blame] | 185 | INITIALIZE_PASS_DEPENDENCY(LiveStacks) |
Jakob Stoklund Olesen | 05ec712 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 186 | INITIALIZE_PASS_DEPENDENCY(VirtRegMap) |
| 187 | INITIALIZE_PASS_END(VirtRegRewriter, "virtregrewriter", |
| 188 | "Virtual Register Rewriter", false, false) |
| 189 | |
| 190 | char VirtRegRewriter::ID = 0; |
| 191 | |
| 192 | void VirtRegRewriter::getAnalysisUsage(AnalysisUsage &AU) const { |
| 193 | AU.setPreservesCFG(); |
| 194 | AU.addRequired<LiveIntervals>(); |
| 195 | AU.addRequired<SlotIndexes>(); |
| 196 | AU.addPreserved<SlotIndexes>(); |
| 197 | AU.addRequired<LiveDebugVariables>(); |
Evan Cheng | bb36a43 | 2012-09-21 20:04:28 +0000 | [diff] [blame] | 198 | AU.addRequired<LiveStacks>(); |
| 199 | AU.addPreserved<LiveStacks>(); |
Jakob Stoklund Olesen | 05ec712 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 200 | AU.addRequired<VirtRegMap>(); |
| 201 | MachineFunctionPass::getAnalysisUsage(AU); |
| 202 | } |
| 203 | |
| 204 | bool VirtRegRewriter::runOnMachineFunction(MachineFunction &fn) { |
| 205 | MF = &fn; |
| 206 | TM = &MF->getTarget(); |
| 207 | TRI = TM->getRegisterInfo(); |
| 208 | TII = TM->getInstrInfo(); |
| 209 | MRI = &MF->getRegInfo(); |
| 210 | Indexes = &getAnalysis<SlotIndexes>(); |
| 211 | LIS = &getAnalysis<LiveIntervals>(); |
| 212 | VRM = &getAnalysis<VirtRegMap>(); |
Jakob Stoklund Olesen | ba05c01 | 2011-02-18 22:03:18 +0000 | [diff] [blame] | 213 | DEBUG(dbgs() << "********** REWRITE VIRTUAL REGISTERS **********\n" |
| 214 | << "********** Function: " |
Craig Topper | 96601ca | 2012-08-22 06:07:19 +0000 | [diff] [blame] | 215 | << MF->getName() << '\n'); |
Jakob Stoklund Olesen | 05ec712 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 216 | DEBUG(VRM->dump()); |
| 217 | |
| 218 | // Add kill flags while we still have virtual registers. |
Jakob Stoklund Olesen | e617ccb | 2012-09-06 18:15:18 +0000 | [diff] [blame] | 219 | LIS->addKillFlags(VRM); |
Jakob Stoklund Olesen | 05ec712 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 220 | |
Jakob Stoklund Olesen | fe17bdb | 2012-06-09 00:14:47 +0000 | [diff] [blame] | 221 | // Live-in lists on basic blocks are required for physregs. |
| 222 | addMBBLiveIns(); |
| 223 | |
Jakob Stoklund Olesen | 05ec712 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 224 | // Rewrite virtual registers. |
| 225 | rewrite(); |
| 226 | |
| 227 | // Write out new DBG_VALUE instructions. |
| 228 | getAnalysis<LiveDebugVariables>().emitDebugValues(VRM); |
| 229 | |
| 230 | // All machine operands and other references to virtual registers have been |
| 231 | // replaced. Remove the virtual registers and release all the transient data. |
| 232 | VRM->clearAllVirt(); |
| 233 | MRI->clearVirtRegs(); |
| 234 | return true; |
| 235 | } |
| 236 | |
Jakob Stoklund Olesen | fe17bdb | 2012-06-09 00:14:47 +0000 | [diff] [blame] | 237 | // Compute MBB live-in lists from virtual register live ranges and their |
| 238 | // assignments. |
| 239 | void VirtRegRewriter::addMBBLiveIns() { |
| 240 | SmallVector<MachineBasicBlock*, 16> LiveIn; |
| 241 | for (unsigned Idx = 0, IdxE = MRI->getNumVirtRegs(); Idx != IdxE; ++Idx) { |
| 242 | unsigned VirtReg = TargetRegisterInfo::index2VirtReg(Idx); |
| 243 | if (MRI->reg_nodbg_empty(VirtReg)) |
| 244 | continue; |
| 245 | LiveInterval &LI = LIS->getInterval(VirtReg); |
| 246 | if (LI.empty() || LIS->intervalIsInOneMBB(LI)) |
| 247 | continue; |
| 248 | // This is a virtual register that is live across basic blocks. Its |
| 249 | // assigned PhysReg must be marked as live-in to those blocks. |
| 250 | unsigned PhysReg = VRM->getPhys(VirtReg); |
| 251 | assert(PhysReg != VirtRegMap::NO_PHYS_REG && "Unmapped virtual register."); |
| 252 | |
| 253 | // Scan the segments of LI. |
| 254 | for (LiveInterval::const_iterator I = LI.begin(), E = LI.end(); I != E; |
| 255 | ++I) { |
| 256 | if (!Indexes->findLiveInMBBs(I->start, I->end, LiveIn)) |
| 257 | continue; |
| 258 | for (unsigned i = 0, e = LiveIn.size(); i != e; ++i) |
| 259 | if (!LiveIn[i]->isLiveIn(PhysReg)) |
| 260 | LiveIn[i]->addLiveIn(PhysReg); |
| 261 | LiveIn.clear(); |
| 262 | } |
| 263 | } |
| 264 | } |
| 265 | |
Jakob Stoklund Olesen | 05ec712 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 266 | void VirtRegRewriter::rewrite() { |
Jakob Stoklund Olesen | 93e110b | 2011-04-27 17:42:31 +0000 | [diff] [blame] | 267 | SmallVector<unsigned, 8> SuperDeads; |
| 268 | SmallVector<unsigned, 8> SuperDefs; |
Jakob Stoklund Olesen | ba05c01 | 2011-02-18 22:03:18 +0000 | [diff] [blame] | 269 | SmallVector<unsigned, 8> SuperKills; |
Quentin Colombet | ce734f1 | 2013-09-25 00:26:17 +0000 | [diff] [blame] | 270 | SmallPtrSet<const MachineInstr *, 4> NoReturnInsts; |
Jakob Stoklund Olesen | ba05c01 | 2011-02-18 22:03:18 +0000 | [diff] [blame] | 271 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 272 | // Here we have a SparseSet to hold which PhysRegs are actually encountered |
| 273 | // in the MF we are about to iterate over so that later when we call |
| 274 | // setPhysRegUsed, we are only doing it for physRegs that were actually found |
| 275 | // in the program and not for all of the possible physRegs for the given |
| 276 | // target architecture. If the target has a lot of physRegs, then for a small |
| 277 | // program there will be a significant compile time reduction here. |
| 278 | PhysRegs.clear(); |
| 279 | PhysRegs.setUniverse(TRI->getNumRegs()); |
| 280 | |
| 281 | // The function with uwtable should guarantee that the stack unwinder |
| 282 | // can unwind the stack to the previous frame. Thus, we can't apply the |
| 283 | // noreturn optimization if the caller function has uwtable attribute. |
| 284 | bool HasUWTable = MF->getFunction()->hasFnAttribute(Attribute::UWTable); |
| 285 | |
Jakob Stoklund Olesen | ba05c01 | 2011-02-18 22:03:18 +0000 | [diff] [blame] | 286 | for (MachineFunction::iterator MBBI = MF->begin(), MBBE = MF->end(); |
| 287 | MBBI != MBBE; ++MBBI) { |
| 288 | DEBUG(MBBI->print(dbgs(), Indexes)); |
Quentin Colombet | ce734f1 | 2013-09-25 00:26:17 +0000 | [diff] [blame] | 289 | bool IsExitBB = MBBI->succ_empty(); |
Evan Cheng | 3f9c251 | 2012-01-19 07:46:36 +0000 | [diff] [blame] | 290 | for (MachineBasicBlock::instr_iterator |
| 291 | MII = MBBI->instr_begin(), MIE = MBBI->instr_end(); MII != MIE;) { |
Jakob Stoklund Olesen | ba05c01 | 2011-02-18 22:03:18 +0000 | [diff] [blame] | 292 | MachineInstr *MI = MII; |
| 293 | ++MII; |
| 294 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 295 | // Check if this instruction is a call to a noreturn function. If this |
| 296 | // is a call to noreturn function and we don't need the stack unwinding |
| 297 | // functionality (i.e. this function does not have uwtable attribute and |
| 298 | // the callee function has the nounwind attribute), then we can ignore |
| 299 | // the definitions set by this instruction. |
| 300 | if (!HasUWTable && IsExitBB && MI->isCall()) { |
Quentin Colombet | ce734f1 | 2013-09-25 00:26:17 +0000 | [diff] [blame] | 301 | for (MachineInstr::mop_iterator MOI = MI->operands_begin(), |
| 302 | MOE = MI->operands_end(); MOI != MOE; ++MOI) { |
| 303 | MachineOperand &MO = *MOI; |
| 304 | if (!MO.isGlobal()) |
| 305 | continue; |
| 306 | const Function *Func = dyn_cast<Function>(MO.getGlobal()); |
Quentin Colombet | f0c6ab6 | 2013-11-08 18:14:17 +0000 | [diff] [blame] | 307 | if (!Func || !Func->hasFnAttribute(Attribute::NoReturn) || |
| 308 | // We need to keep correct unwind information |
| 309 | // even if the function will not return, since the |
| 310 | // runtime may need it. |
| 311 | !Func->hasFnAttribute(Attribute::NoUnwind)) |
Quentin Colombet | ce734f1 | 2013-09-25 00:26:17 +0000 | [diff] [blame] | 312 | continue; |
| 313 | NoReturnInsts.insert(MI); |
| 314 | break; |
| 315 | } |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 316 | } |
Quentin Colombet | ce734f1 | 2013-09-25 00:26:17 +0000 | [diff] [blame] | 317 | |
Jakob Stoklund Olesen | ba05c01 | 2011-02-18 22:03:18 +0000 | [diff] [blame] | 318 | for (MachineInstr::mop_iterator MOI = MI->operands_begin(), |
| 319 | MOE = MI->operands_end(); MOI != MOE; ++MOI) { |
| 320 | MachineOperand &MO = *MOI; |
Jakob Stoklund Olesen | d9f0ff5 | 2012-02-17 19:07:56 +0000 | [diff] [blame] | 321 | |
| 322 | // Make sure MRI knows about registers clobbered by regmasks. |
| 323 | if (MO.isRegMask()) |
| 324 | MRI->addPhysRegsUsedFromRegMask(MO.getRegMask()); |
| 325 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 326 | // If we encounter a VirtReg or PhysReg then get at the PhysReg and add |
| 327 | // it to the physreg bitset. Later we use only the PhysRegs that were |
| 328 | // actually encountered in the MF to populate the MRI's used physregs. |
| 329 | if (MO.isReg() && MO.getReg()) |
| 330 | PhysRegs.insert( |
| 331 | TargetRegisterInfo::isVirtualRegister(MO.getReg()) ? |
| 332 | VRM->getPhys(MO.getReg()) : |
| 333 | MO.getReg()); |
| 334 | |
Jakob Stoklund Olesen | ba05c01 | 2011-02-18 22:03:18 +0000 | [diff] [blame] | 335 | if (!MO.isReg() || !TargetRegisterInfo::isVirtualRegister(MO.getReg())) |
| 336 | continue; |
| 337 | unsigned VirtReg = MO.getReg(); |
Jakob Stoklund Olesen | 05ec712 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 338 | unsigned PhysReg = VRM->getPhys(VirtReg); |
| 339 | assert(PhysReg != VirtRegMap::NO_PHYS_REG && |
| 340 | "Instruction uses unmapped VirtReg"); |
Jakob Stoklund Olesen | fb9ebbf | 2012-10-15 21:57:41 +0000 | [diff] [blame] | 341 | assert(!MRI->isReserved(PhysReg) && "Reserved register assignment"); |
Jakob Stoklund Olesen | ba05c01 | 2011-02-18 22:03:18 +0000 | [diff] [blame] | 342 | |
| 343 | // Preserve semantics of sub-register operands. |
| 344 | if (MO.getSubReg()) { |
| 345 | // 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] | 346 | // have to add <imp-use,kill> operands for the super-register. A |
| 347 | // partial redef always kills and redefines the super-register. |
| 348 | if (MO.readsReg() && (MO.isDef() || MO.isKill())) |
| 349 | SuperKills.push_back(PhysReg); |
| 350 | |
| 351 | if (MO.isDef()) { |
| 352 | // The <def,undef> flag only makes sense for sub-register defs, and |
| 353 | // we are substituting a full physreg. An <imp-use,kill> operand |
| 354 | // from the SuperKills list will represent the partial read of the |
| 355 | // super-register. |
| 356 | MO.setIsUndef(false); |
| 357 | |
| 358 | // Also add implicit defs for the super-register. |
| 359 | if (MO.isDead()) |
| 360 | SuperDeads.push_back(PhysReg); |
| 361 | else |
| 362 | SuperDefs.push_back(PhysReg); |
| 363 | } |
Jakob Stoklund Olesen | ba05c01 | 2011-02-18 22:03:18 +0000 | [diff] [blame] | 364 | |
| 365 | // PhysReg operands cannot have subregister indexes. |
| 366 | PhysReg = TRI->getSubReg(PhysReg, MO.getSubReg()); |
| 367 | assert(PhysReg && "Invalid SubReg for physical register"); |
| 368 | MO.setSubReg(0); |
| 369 | } |
| 370 | // Rewrite. Note we could have used MachineOperand::substPhysReg(), but |
| 371 | // we need the inlining here. |
| 372 | MO.setReg(PhysReg); |
| 373 | } |
| 374 | |
| 375 | // Add any missing super-register kills after rewriting the whole |
| 376 | // instruction. |
| 377 | while (!SuperKills.empty()) |
| 378 | MI->addRegisterKilled(SuperKills.pop_back_val(), TRI, true); |
| 379 | |
Jakob Stoklund Olesen | 93e110b | 2011-04-27 17:42:31 +0000 | [diff] [blame] | 380 | while (!SuperDeads.empty()) |
| 381 | MI->addRegisterDead(SuperDeads.pop_back_val(), TRI, true); |
| 382 | |
| 383 | while (!SuperDefs.empty()) |
| 384 | MI->addRegisterDefined(SuperDefs.pop_back_val(), TRI); |
| 385 | |
Jakob Stoklund Olesen | ba05c01 | 2011-02-18 22:03:18 +0000 | [diff] [blame] | 386 | DEBUG(dbgs() << "> " << *MI); |
| 387 | |
| 388 | // Finally, remove any identity copies. |
| 389 | if (MI->isIdentityCopy()) { |
Jakob Stoklund Olesen | cf5e5f3 | 2011-05-06 17:59:57 +0000 | [diff] [blame] | 390 | ++NumIdCopies; |
Jakob Stoklund Olesen | 280ea1a | 2011-03-31 17:55:25 +0000 | [diff] [blame] | 391 | if (MI->getNumOperands() == 2) { |
| 392 | DEBUG(dbgs() << "Deleting identity copy.\n"); |
Jakob Stoklund Olesen | 280ea1a | 2011-03-31 17:55:25 +0000 | [diff] [blame] | 393 | if (Indexes) |
| 394 | Indexes->removeMachineInstrFromMaps(MI); |
| 395 | // It's safe to erase MI because MII has already been incremented. |
| 396 | MI->eraseFromParent(); |
| 397 | } else { |
| 398 | // Transform identity copy to a KILL to deal with subregisters. |
| 399 | MI->setDesc(TII->get(TargetOpcode::KILL)); |
| 400 | DEBUG(dbgs() << "Identity copy: " << *MI); |
| 401 | } |
Jakob Stoklund Olesen | ba05c01 | 2011-02-18 22:03:18 +0000 | [diff] [blame] | 402 | } |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | // Tell MRI about physical registers in use. |
Quentin Colombet | ce734f1 | 2013-09-25 00:26:17 +0000 | [diff] [blame] | 407 | if (NoReturnInsts.empty()) { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 408 | for (SparseSet<unsigned>::iterator |
| 409 | RegI = PhysRegs.begin(), E = PhysRegs.end(); RegI != E; ++RegI) |
| 410 | if (!MRI->reg_nodbg_empty(*RegI)) |
| 411 | MRI->setPhysRegUsed(*RegI); |
Quentin Colombet | ce734f1 | 2013-09-25 00:26:17 +0000 | [diff] [blame] | 412 | } else { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 413 | for (SparseSet<unsigned>::iterator |
| 414 | I = PhysRegs.begin(), E = PhysRegs.end(); I != E; ++I) { |
| 415 | unsigned Reg = *I; |
Quentin Colombet | ce734f1 | 2013-09-25 00:26:17 +0000 | [diff] [blame] | 416 | if (MRI->reg_nodbg_empty(Reg)) |
| 417 | continue; |
| 418 | // Check if this register has a use that will impact the rest of the |
| 419 | // code. Uses in debug and noreturn instructions do not impact the |
| 420 | // generated code. |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 421 | for (MachineInstr &It : MRI->reg_nodbg_instructions(Reg)) { |
| 422 | if (!NoReturnInsts.count(&It)) { |
Quentin Colombet | ce734f1 | 2013-09-25 00:26:17 +0000 | [diff] [blame] | 423 | MRI->setPhysRegUsed(Reg); |
| 424 | break; |
| 425 | } |
| 426 | } |
| 427 | } |
| 428 | } |
Jakob Stoklund Olesen | ba05c01 | 2011-02-18 22:03:18 +0000 | [diff] [blame] | 429 | } |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 430 | |