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