| Eugene Zelenko | 618c555 | 2017-09-13 21:15:20 +0000 | [diff] [blame] | 1 | //===- llvm/CodeGen/VirtRegMap.cpp - Virtual Register Map -----------------===// | 
| Alkis Evlogimenos | c794a90 | 2004-02-23 23:08:11 +0000 | [diff] [blame] | 2 | // | 
|  | 3 | //                     The LLVM Compiler Infrastructure | 
|  | 4 | // | 
| Chris Lattner | f3ebc3f | 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 | c794a90 | 2004-02-23 23:08:11 +0000 | [diff] [blame] | 7 | // | 
|  | 8 | //===----------------------------------------------------------------------===// | 
|  | 9 | // | 
| Chris Lattner | e2b77d5 | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 10 | // This file implements the VirtRegMap class. | 
|  | 11 | // | 
| Dan Gohman | 4a61882 | 2010-02-10 16:03:48 +0000 | [diff] [blame] | 12 | // It also contains implementations of the Spiller interface, which, given a | 
| Chris Lattner | e2b77d5 | 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 | 1dd872c | 2004-02-24 08:58:30 +0000 | [diff] [blame] | 15 | // code as necessary. | 
| Alkis Evlogimenos | c794a90 | 2004-02-23 23:08:11 +0000 | [diff] [blame] | 16 | // | 
|  | 17 | //===----------------------------------------------------------------------===// | 
|  | 18 |  | 
| Jakob Stoklund Olesen | 26c9d70 | 2012-11-28 19:13:06 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/VirtRegMap.h" | 
| Jakob Stoklund Olesen | 1224312 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 20 | #include "LiveDebugVariables.h" | 
| Eugene Zelenko | 618c555 | 2017-09-13 21:15:20 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SmallVector.h" | 
| Chandler Carruth | 442f784 | 2014-03-04 10:07:28 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/Statistic.h" | 
| Eugene Zelenko | 618c555 | 2017-09-13 21:15:20 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/LiveInterval.h" | 
| Jakob Stoklund Olesen | 1224312 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/LiveIntervalAnalysis.h" | 
| Evan Cheng | b53825b | 2012-09-21 20:04:28 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/LiveStackAnalysis.h" | 
| Eugene Zelenko | 618c555 | 2017-09-13 21:15:20 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/MachineBasicBlock.h" | 
| Alkis Evlogimenos | c794a90 | 2004-02-23 23:08:11 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/MachineFrameInfo.h" | 
| Chris Lattner | e2b77d5 | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/MachineFunction.h" | 
| Eugene Zelenko | 618c555 | 2017-09-13 21:15:20 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/MachineFunctionPass.h" | 
|  | 30 | #include "llvm/CodeGen/MachineInstr.h" | 
|  | 31 | #include "llvm/CodeGen/MachineOperand.h" | 
| Chris Lattner | a10fff5 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 32 | #include "llvm/CodeGen/MachineRegisterInfo.h" | 
| Eugene Zelenko | 618c555 | 2017-09-13 21:15:20 +0000 | [diff] [blame] | 33 | #include "llvm/CodeGen/SlotIndexes.h" | 
|  | 34 | #include "llvm/MC/LaneBitmask.h" | 
|  | 35 | #include "llvm/Pass.h" | 
| Chris Lattner | 3d27be1 | 2006-08-27 12:54:02 +0000 | [diff] [blame] | 36 | #include "llvm/Support/Compiler.h" | 
| Evan Cheng | a1968b0 | 2009-02-11 08:24:21 +0000 | [diff] [blame] | 37 | #include "llvm/Support/Debug.h" | 
| Daniel Dunbar | 796e43e | 2009-07-24 10:36:58 +0000 | [diff] [blame] | 38 | #include "llvm/Support/raw_ostream.h" | 
| Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 39 | #include "llvm/Target/TargetInstrInfo.h" | 
| Eugene Zelenko | 618c555 | 2017-09-13 21:15:20 +0000 | [diff] [blame] | 40 | #include "llvm/Target/TargetOpcodes.h" | 
| Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 41 | #include "llvm/Target/TargetRegisterInfo.h" | 
| Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 42 | #include "llvm/Target/TargetSubtargetInfo.h" | 
| Eugene Zelenko | 618c555 | 2017-09-13 21:15:20 +0000 | [diff] [blame] | 43 | #include <cassert> | 
|  | 44 | #include <iterator> | 
|  | 45 | #include <utility> | 
|  | 46 |  | 
| Alkis Evlogimenos | c794a90 | 2004-02-23 23:08:11 +0000 | [diff] [blame] | 47 | using namespace llvm; | 
|  | 48 |  | 
| Chandler Carruth | 1b9dde0 | 2014-04-22 02:02:50 +0000 | [diff] [blame] | 49 | #define DEBUG_TYPE "regalloc" | 
|  | 50 |  | 
| Jakob Stoklund Olesen | 53e2e48 | 2011-09-15 18:31:13 +0000 | [diff] [blame] | 51 | STATISTIC(NumSpillSlots, "Number of spill slots allocated"); | 
|  | 52 | STATISTIC(NumIdCopies,   "Number of identity moves eliminated after rewriting"); | 
| Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 53 |  | 
| Chris Lattner | e2b77d5 | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 54 | //===----------------------------------------------------------------------===// | 
|  | 55 | //  VirtRegMap implementation | 
|  | 56 | //===----------------------------------------------------------------------===// | 
|  | 57 |  | 
| Owen Anderson | d37ddf5 | 2009-03-13 05:55:11 +0000 | [diff] [blame] | 58 | char VirtRegMap::ID = 0; | 
|  | 59 |  | 
| Owen Anderson | df7a4f2 | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 60 | INITIALIZE_PASS(VirtRegMap, "virtregmap", "Virtual Register Map", false, false) | 
| Owen Anderson | d37ddf5 | 2009-03-13 05:55:11 +0000 | [diff] [blame] | 61 |  | 
|  | 62 | bool VirtRegMap::runOnMachineFunction(MachineFunction &mf) { | 
| Evan Cheng | 085caf1 | 2009-06-14 20:22:55 +0000 | [diff] [blame] | 63 | MRI = &mf.getRegInfo(); | 
| Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 64 | TII = mf.getSubtarget().getInstrInfo(); | 
|  | 65 | TRI = mf.getSubtarget().getRegisterInfo(); | 
| Owen Anderson | d37ddf5 | 2009-03-13 05:55:11 +0000 | [diff] [blame] | 66 | MF = &mf; | 
| Lang Hames | 05fb963 | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 67 |  | 
| Owen Anderson | d37ddf5 | 2009-03-13 05:55:11 +0000 | [diff] [blame] | 68 | Virt2PhysMap.clear(); | 
|  | 69 | Virt2StackSlotMap.clear(); | 
| Owen Anderson | d37ddf5 | 2009-03-13 05:55:11 +0000 | [diff] [blame] | 70 | Virt2SplitMap.clear(); | 
| Evan Cheng | 3f77805 | 2009-05-04 03:30:11 +0000 | [diff] [blame] | 71 |  | 
| Chris Lattner | 13a5dcd | 2006-09-05 02:12:02 +0000 | [diff] [blame] | 72 | grow(); | 
| Owen Anderson | d37ddf5 | 2009-03-13 05:55:11 +0000 | [diff] [blame] | 73 | return false; | 
| Chris Lattner | 13a5dcd | 2006-09-05 02:12:02 +0000 | [diff] [blame] | 74 | } | 
|  | 75 |  | 
| Chris Lattner | e2b77d5 | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 76 | void VirtRegMap::grow() { | 
| Jakob Stoklund Olesen | d82ac37 | 2011-01-09 21:58:20 +0000 | [diff] [blame] | 77 | unsigned NumRegs = MF->getRegInfo().getNumVirtRegs(); | 
|  | 78 | Virt2PhysMap.resize(NumRegs); | 
|  | 79 | Virt2StackSlotMap.resize(NumRegs); | 
| Jakob Stoklund Olesen | d82ac37 | 2011-01-09 21:58:20 +0000 | [diff] [blame] | 80 | Virt2SplitMap.resize(NumRegs); | 
| Alkis Evlogimenos | c794a90 | 2004-02-23 23:08:11 +0000 | [diff] [blame] | 81 | } | 
|  | 82 |  | 
| Matthias Braun | 1ee25e0 | 2017-06-08 21:30:54 +0000 | [diff] [blame] | 83 | void VirtRegMap::assignVirt2Phys(unsigned virtReg, MCPhysReg physReg) { | 
|  | 84 | assert(TargetRegisterInfo::isVirtualRegister(virtReg) && | 
|  | 85 | TargetRegisterInfo::isPhysicalRegister(physReg)); | 
|  | 86 | assert(Virt2PhysMap[virtReg] == NO_PHYS_REG && | 
|  | 87 | "attempt to assign physical register to already mapped " | 
|  | 88 | "virtual register"); | 
|  | 89 | assert(!getRegInfo().isReserved(physReg) && | 
|  | 90 | "Attempt to map virtReg to a reserved physReg"); | 
|  | 91 | Virt2PhysMap[virtReg] = physReg; | 
|  | 92 | } | 
|  | 93 |  | 
| Jakob Stoklund Olesen | 39aed73 | 2010-11-16 00:41:01 +0000 | [diff] [blame] | 94 | unsigned VirtRegMap::createSpillSlot(const TargetRegisterClass *RC) { | 
| Krzysztof Parzyszek | 44e25f3 | 2017-04-24 18:55:33 +0000 | [diff] [blame] | 95 | unsigned Size = TRI->getSpillSize(*RC); | 
|  | 96 | unsigned Align = TRI->getSpillAlignment(*RC); | 
|  | 97 | int SS = MF->getFrameInfo().CreateSpillStackObject(Size, Align); | 
| Jakob Stoklund Olesen | 53e2e48 | 2011-09-15 18:31:13 +0000 | [diff] [blame] | 98 | ++NumSpillSlots; | 
| Jakob Stoklund Olesen | 39aed73 | 2010-11-16 00:41:01 +0000 | [diff] [blame] | 99 | return SS; | 
|  | 100 | } | 
|  | 101 |  | 
| Jakob Stoklund Olesen | 1dd82dd | 2012-12-04 00:30:22 +0000 | [diff] [blame] | 102 | bool VirtRegMap::hasPreferredPhys(unsigned VirtReg) { | 
|  | 103 | unsigned Hint = MRI->getSimpleHint(VirtReg); | 
|  | 104 | if (!Hint) | 
| Matt Arsenault | 50451d4 | 2016-06-02 18:37:21 +0000 | [diff] [blame] | 105 | return false; | 
| Jakob Stoklund Olesen | 1dd82dd | 2012-12-04 00:30:22 +0000 | [diff] [blame] | 106 | if (TargetRegisterInfo::isVirtualRegister(Hint)) | 
|  | 107 | Hint = getPhys(Hint); | 
|  | 108 | return getPhys(VirtReg) == Hint; | 
|  | 109 | } | 
|  | 110 |  | 
| Jakob Stoklund Olesen | 74052b0 | 2012-12-03 23:23:50 +0000 | [diff] [blame] | 111 | bool VirtRegMap::hasKnownPreference(unsigned VirtReg) { | 
|  | 112 | std::pair<unsigned, unsigned> Hint = MRI->getRegAllocationHint(VirtReg); | 
|  | 113 | if (TargetRegisterInfo::isPhysicalRegister(Hint.second)) | 
|  | 114 | return true; | 
|  | 115 | if (TargetRegisterInfo::isVirtualRegister(Hint.second)) | 
|  | 116 | return hasPhys(Hint.second); | 
|  | 117 | return false; | 
|  | 118 | } | 
|  | 119 |  | 
| Chris Lattner | e2b77d5 | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 120 | int VirtRegMap::assignVirt2StackSlot(unsigned virtReg) { | 
| Dan Gohman | 3a4be0f | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 121 | assert(TargetRegisterInfo::isVirtualRegister(virtReg)); | 
| Chris Lattner | 39fef8d | 2004-09-30 02:15:18 +0000 | [diff] [blame] | 122 | assert(Virt2StackSlotMap[virtReg] == NO_STACK_SLOT && | 
| Chris Lattner | e2b77d5 | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 123 | "attempt to assign stack slot to already spilled register"); | 
| Owen Anderson | d37ddf5 | 2009-03-13 05:55:11 +0000 | [diff] [blame] | 124 | const TargetRegisterClass* RC = MF->getRegInfo().getRegClass(virtReg); | 
| Jakob Stoklund Olesen | 39aed73 | 2010-11-16 00:41:01 +0000 | [diff] [blame] | 125 | return Virt2StackSlotMap[virtReg] = createSpillSlot(RC); | 
| Chris Lattner | e2b77d5 | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 126 | } | 
|  | 127 |  | 
| Evan Cheng | 6d56368 | 2008-02-27 03:04:06 +0000 | [diff] [blame] | 128 | void VirtRegMap::assignVirt2StackSlot(unsigned virtReg, int SS) { | 
| Dan Gohman | 3a4be0f | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 129 | assert(TargetRegisterInfo::isVirtualRegister(virtReg)); | 
| Chris Lattner | 39fef8d | 2004-09-30 02:15:18 +0000 | [diff] [blame] | 130 | assert(Virt2StackSlotMap[virtReg] == NO_STACK_SLOT && | 
| Chris Lattner | e2b77d5 | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 131 | "attempt to assign stack slot to already spilled register"); | 
| Evan Cheng | 6d56368 | 2008-02-27 03:04:06 +0000 | [diff] [blame] | 132 | assert((SS >= 0 || | 
| Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 133 | (SS >= MF->getFrameInfo().getObjectIndexBegin())) && | 
| Evan Cheng | 8be98c1 | 2007-04-04 07:40:01 +0000 | [diff] [blame] | 134 | "illegal fixed frame index"); | 
| Evan Cheng | 6d56368 | 2008-02-27 03:04:06 +0000 | [diff] [blame] | 135 | Virt2StackSlotMap[virtReg] = SS; | 
| Alkis Evlogimenos | fd735bc | 2004-05-29 20:38:05 +0000 | [diff] [blame] | 136 | } | 
|  | 137 |  | 
| Jakob Stoklund Olesen | 1224312 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 138 | void VirtRegMap::print(raw_ostream &OS, const Module*) const { | 
|  | 139 | OS << "********** REGISTER MAP **********\n"; | 
|  | 140 | for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) { | 
|  | 141 | unsigned Reg = TargetRegisterInfo::index2VirtReg(i); | 
|  | 142 | if (Virt2PhysMap[Reg] != (unsigned)VirtRegMap::NO_PHYS_REG) { | 
|  | 143 | OS << '[' << PrintReg(Reg, TRI) << " -> " | 
|  | 144 | << PrintReg(Virt2PhysMap[Reg], TRI) << "] " | 
| Craig Topper | cf0444b | 2014-11-17 05:50:14 +0000 | [diff] [blame] | 145 | << TRI->getRegClassName(MRI->getRegClass(Reg)) << "\n"; | 
| Jakob Stoklund Olesen | 1224312 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 146 | } | 
|  | 147 | } | 
|  | 148 |  | 
|  | 149 | for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) { | 
|  | 150 | unsigned Reg = TargetRegisterInfo::index2VirtReg(i); | 
|  | 151 | if (Virt2StackSlotMap[Reg] != VirtRegMap::NO_STACK_SLOT) { | 
|  | 152 | OS << '[' << PrintReg(Reg, TRI) << " -> fi#" << Virt2StackSlotMap[Reg] | 
| Craig Topper | cf0444b | 2014-11-17 05:50:14 +0000 | [diff] [blame] | 153 | << "] " << TRI->getRegClassName(MRI->getRegClass(Reg)) << "\n"; | 
| Jakob Stoklund Olesen | 1224312 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 154 | } | 
|  | 155 | } | 
|  | 156 | OS << '\n'; | 
|  | 157 | } | 
|  | 158 |  | 
| Aaron Ballman | 615eb47 | 2017-10-15 14:32:27 +0000 | [diff] [blame] | 159 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) | 
| Yaron Keren | eb2a254 | 2016-01-29 20:50:44 +0000 | [diff] [blame] | 160 | LLVM_DUMP_METHOD void VirtRegMap::dump() const { | 
| Jakob Stoklund Olesen | 1224312 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 161 | print(dbgs()); | 
|  | 162 | } | 
| Manman Ren | 742534c | 2012-09-06 19:06:06 +0000 | [diff] [blame] | 163 | #endif | 
| Jakob Stoklund Olesen | 1224312 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 164 |  | 
|  | 165 | //===----------------------------------------------------------------------===// | 
|  | 166 | //                              VirtRegRewriter | 
|  | 167 | //===----------------------------------------------------------------------===// | 
|  | 168 | // | 
|  | 169 | // The VirtRegRewriter is the last of the register allocator passes. | 
|  | 170 | // It rewrites virtual registers to physical registers as specified in the | 
|  | 171 | // VirtRegMap analysis. It also updates live-in information on basic blocks | 
|  | 172 | // according to LiveIntervals. | 
|  | 173 | // | 
|  | 174 | namespace { | 
| Eugene Zelenko | 618c555 | 2017-09-13 21:15:20 +0000 | [diff] [blame] | 175 |  | 
| Jakob Stoklund Olesen | 1224312 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 176 | class VirtRegRewriter : public MachineFunctionPass { | 
|  | 177 | MachineFunction *MF; | 
| Jakob Stoklund Olesen | 1224312 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 178 | const TargetRegisterInfo *TRI; | 
|  | 179 | const TargetInstrInfo *TII; | 
|  | 180 | MachineRegisterInfo *MRI; | 
|  | 181 | SlotIndexes *Indexes; | 
|  | 182 | LiveIntervals *LIS; | 
|  | 183 | VirtRegMap *VRM; | 
|  | 184 |  | 
|  | 185 | void rewrite(); | 
|  | 186 | void addMBBLiveIns(); | 
| Matthias Braun | ca4e842 | 2015-06-16 18:22:28 +0000 | [diff] [blame] | 187 | bool readsUndefSubreg(const MachineOperand &MO) const; | 
| Matthias Braun | cc58005 | 2015-09-09 18:07:54 +0000 | [diff] [blame] | 188 | void addLiveInsForSubRanges(const LiveInterval &LI, unsigned PhysReg) const; | 
| Matthias Braun | 152e7c8 | 2016-07-09 00:19:07 +0000 | [diff] [blame] | 189 | void handleIdentityCopy(MachineInstr &MI) const; | 
| Matthias Braun | f0b68d3 | 2017-03-17 00:41:39 +0000 | [diff] [blame] | 190 | void expandCopyBundle(MachineInstr &MI) const; | 
| Quentin Colombet | 647b482 | 2017-08-16 00:17:05 +0000 | [diff] [blame] | 191 | bool subRegLiveThrough(const MachineInstr &MI, unsigned SuperPhysReg) const; | 
| Matthias Braun | cc58005 | 2015-09-09 18:07:54 +0000 | [diff] [blame] | 192 |  | 
| Jakob Stoklund Olesen | 1224312 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 193 | public: | 
|  | 194 | static char ID; | 
| Eugene Zelenko | 618c555 | 2017-09-13 21:15:20 +0000 | [diff] [blame] | 195 |  | 
| Jakob Stoklund Olesen | 1224312 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 196 | VirtRegRewriter() : MachineFunctionPass(ID) {} | 
|  | 197 |  | 
| Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 198 | void getAnalysisUsage(AnalysisUsage &AU) const override; | 
| Jakob Stoklund Olesen | 1224312 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 199 |  | 
| Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 200 | bool runOnMachineFunction(MachineFunction&) override; | 
| Eugene Zelenko | 618c555 | 2017-09-13 21:15:20 +0000 | [diff] [blame] | 201 |  | 
| Derek Schuff | 42666ee | 2016-03-29 17:40:22 +0000 | [diff] [blame] | 202 | MachineFunctionProperties getSetProperties() const override { | 
|  | 203 | return MachineFunctionProperties().set( | 
| Matthias Braun | 1eb4736 | 2016-08-25 01:27:13 +0000 | [diff] [blame] | 204 | MachineFunctionProperties::Property::NoVRegs); | 
| Derek Schuff | 42666ee | 2016-03-29 17:40:22 +0000 | [diff] [blame] | 205 | } | 
| Jakob Stoklund Olesen | 1224312 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 206 | }; | 
| Eugene Zelenko | 618c555 | 2017-09-13 21:15:20 +0000 | [diff] [blame] | 207 |  | 
| Jakob Stoklund Olesen | 1224312 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 208 | } // end anonymous namespace | 
|  | 209 |  | 
| Eugene Zelenko | 618c555 | 2017-09-13 21:15:20 +0000 | [diff] [blame] | 210 | char VirtRegRewriter::ID = 0; | 
|  | 211 |  | 
| Jakob Stoklund Olesen | 1224312 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 212 | char &llvm::VirtRegRewriterID = VirtRegRewriter::ID; | 
|  | 213 |  | 
|  | 214 | INITIALIZE_PASS_BEGIN(VirtRegRewriter, "virtregrewriter", | 
|  | 215 | "Virtual Register Rewriter", false, false) | 
|  | 216 | INITIALIZE_PASS_DEPENDENCY(SlotIndexes) | 
|  | 217 | INITIALIZE_PASS_DEPENDENCY(LiveIntervals) | 
|  | 218 | INITIALIZE_PASS_DEPENDENCY(LiveDebugVariables) | 
| Evan Cheng | b53825b | 2012-09-21 20:04:28 +0000 | [diff] [blame] | 219 | INITIALIZE_PASS_DEPENDENCY(LiveStacks) | 
| Jakob Stoklund Olesen | 1224312 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 220 | INITIALIZE_PASS_DEPENDENCY(VirtRegMap) | 
|  | 221 | INITIALIZE_PASS_END(VirtRegRewriter, "virtregrewriter", | 
|  | 222 | "Virtual Register Rewriter", false, false) | 
|  | 223 |  | 
| Jakob Stoklund Olesen | 1224312 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 224 | void VirtRegRewriter::getAnalysisUsage(AnalysisUsage &AU) const { | 
|  | 225 | AU.setPreservesCFG(); | 
|  | 226 | AU.addRequired<LiveIntervals>(); | 
|  | 227 | AU.addRequired<SlotIndexes>(); | 
|  | 228 | AU.addPreserved<SlotIndexes>(); | 
|  | 229 | AU.addRequired<LiveDebugVariables>(); | 
| Evan Cheng | b53825b | 2012-09-21 20:04:28 +0000 | [diff] [blame] | 230 | AU.addRequired<LiveStacks>(); | 
|  | 231 | AU.addPreserved<LiveStacks>(); | 
| Jakob Stoklund Olesen | 1224312 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 232 | AU.addRequired<VirtRegMap>(); | 
|  | 233 | MachineFunctionPass::getAnalysisUsage(AU); | 
|  | 234 | } | 
|  | 235 |  | 
|  | 236 | bool VirtRegRewriter::runOnMachineFunction(MachineFunction &fn) { | 
|  | 237 | MF = &fn; | 
| Eric Christopher | 1c5fce0 | 2014-10-13 21:57:44 +0000 | [diff] [blame] | 238 | TRI = MF->getSubtarget().getRegisterInfo(); | 
|  | 239 | TII = MF->getSubtarget().getInstrInfo(); | 
| Jakob Stoklund Olesen | 1224312 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 240 | MRI = &MF->getRegInfo(); | 
|  | 241 | Indexes = &getAnalysis<SlotIndexes>(); | 
|  | 242 | LIS = &getAnalysis<LiveIntervals>(); | 
|  | 243 | VRM = &getAnalysis<VirtRegMap>(); | 
| Jakob Stoklund Olesen | 5bfec69 | 2011-02-18 22:03:18 +0000 | [diff] [blame] | 244 | DEBUG(dbgs() << "********** REWRITE VIRTUAL REGISTERS **********\n" | 
|  | 245 | << "********** Function: " | 
| Craig Topper | a538d83 | 2012-08-22 06:07:19 +0000 | [diff] [blame] | 246 | << MF->getName() << '\n'); | 
| Jakob Stoklund Olesen | 1224312 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 247 | DEBUG(VRM->dump()); | 
|  | 248 |  | 
|  | 249 | // Add kill flags while we still have virtual registers. | 
| Jakob Stoklund Olesen | bb4bdd8 | 2012-09-06 18:15:18 +0000 | [diff] [blame] | 250 | LIS->addKillFlags(VRM); | 
| Jakob Stoklund Olesen | 1224312 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 251 |  | 
| Jakob Stoklund Olesen | be33629 | 2012-06-09 00:14:47 +0000 | [diff] [blame] | 252 | // Live-in lists on basic blocks are required for physregs. | 
|  | 253 | addMBBLiveIns(); | 
|  | 254 |  | 
| Jakob Stoklund Olesen | 1224312 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 255 | // Rewrite virtual registers. | 
|  | 256 | rewrite(); | 
|  | 257 |  | 
|  | 258 | // Write out new DBG_VALUE instructions. | 
|  | 259 | getAnalysis<LiveDebugVariables>().emitDebugValues(VRM); | 
|  | 260 |  | 
|  | 261 | // All machine operands and other references to virtual registers have been | 
|  | 262 | // replaced. Remove the virtual registers and release all the transient data. | 
|  | 263 | VRM->clearAllVirt(); | 
|  | 264 | MRI->clearVirtRegs(); | 
|  | 265 | return true; | 
|  | 266 | } | 
|  | 267 |  | 
| Matthias Braun | cc58005 | 2015-09-09 18:07:54 +0000 | [diff] [blame] | 268 | void VirtRegRewriter::addLiveInsForSubRanges(const LiveInterval &LI, | 
|  | 269 | unsigned PhysReg) const { | 
|  | 270 | assert(!LI.empty()); | 
|  | 271 | assert(LI.hasSubRanges()); | 
|  | 272 |  | 
| Eugene Zelenko | 618c555 | 2017-09-13 21:15:20 +0000 | [diff] [blame] | 273 | using SubRangeIteratorPair = | 
|  | 274 | std::pair<const LiveInterval::SubRange *, LiveInterval::const_iterator>; | 
|  | 275 |  | 
| Matthias Braun | cc58005 | 2015-09-09 18:07:54 +0000 | [diff] [blame] | 276 | SmallVector<SubRangeIteratorPair, 4> SubRanges; | 
|  | 277 | SlotIndex First; | 
|  | 278 | SlotIndex Last; | 
|  | 279 | for (const LiveInterval::SubRange &SR : LI.subranges()) { | 
|  | 280 | SubRanges.push_back(std::make_pair(&SR, SR.begin())); | 
|  | 281 | if (!First.isValid() || SR.segments.front().start < First) | 
|  | 282 | First = SR.segments.front().start; | 
|  | 283 | if (!Last.isValid() || SR.segments.back().end > Last) | 
|  | 284 | Last = SR.segments.back().end; | 
|  | 285 | } | 
|  | 286 |  | 
|  | 287 | // Check all mbb start positions between First and Last while | 
|  | 288 | // simulatenously advancing an iterator for each subrange. | 
|  | 289 | for (SlotIndexes::MBBIndexIterator MBBI = Indexes->findMBBIndex(First); | 
|  | 290 | MBBI != Indexes->MBBIndexEnd() && MBBI->first <= Last; ++MBBI) { | 
|  | 291 | SlotIndex MBBBegin = MBBI->first; | 
|  | 292 | // Advance all subrange iterators so that their end position is just | 
|  | 293 | // behind MBBBegin (or the iterator is at the end). | 
| Krzysztof Parzyszek | 91b5cf8 | 2016-12-15 14:36:06 +0000 | [diff] [blame] | 294 | LaneBitmask LaneMask; | 
| Matthias Braun | cc58005 | 2015-09-09 18:07:54 +0000 | [diff] [blame] | 295 | for (auto &RangeIterPair : SubRanges) { | 
|  | 296 | const LiveInterval::SubRange *SR = RangeIterPair.first; | 
|  | 297 | LiveInterval::const_iterator &SRI = RangeIterPair.second; | 
|  | 298 | while (SRI != SR->end() && SRI->end <= MBBBegin) | 
|  | 299 | ++SRI; | 
|  | 300 | if (SRI == SR->end()) | 
|  | 301 | continue; | 
|  | 302 | if (SRI->start <= MBBBegin) | 
|  | 303 | LaneMask |= SR->LaneMask; | 
|  | 304 | } | 
| Krzysztof Parzyszek | 91b5cf8 | 2016-12-15 14:36:06 +0000 | [diff] [blame] | 305 | if (LaneMask.none()) | 
| Matthias Braun | cc58005 | 2015-09-09 18:07:54 +0000 | [diff] [blame] | 306 | continue; | 
|  | 307 | MachineBasicBlock *MBB = MBBI->second; | 
| Matthias Braun | d9da162 | 2015-09-09 18:08:03 +0000 | [diff] [blame] | 308 | MBB->addLiveIn(PhysReg, LaneMask); | 
| Matthias Braun | cc58005 | 2015-09-09 18:07:54 +0000 | [diff] [blame] | 309 | } | 
|  | 310 | } | 
|  | 311 |  | 
| Jakob Stoklund Olesen | be33629 | 2012-06-09 00:14:47 +0000 | [diff] [blame] | 312 | // Compute MBB live-in lists from virtual register live ranges and their | 
|  | 313 | // assignments. | 
|  | 314 | void VirtRegRewriter::addMBBLiveIns() { | 
| Jakob Stoklund Olesen | be33629 | 2012-06-09 00:14:47 +0000 | [diff] [blame] | 315 | for (unsigned Idx = 0, IdxE = MRI->getNumVirtRegs(); Idx != IdxE; ++Idx) { | 
|  | 316 | unsigned VirtReg = TargetRegisterInfo::index2VirtReg(Idx); | 
|  | 317 | if (MRI->reg_nodbg_empty(VirtReg)) | 
|  | 318 | continue; | 
|  | 319 | LiveInterval &LI = LIS->getInterval(VirtReg); | 
|  | 320 | if (LI.empty() || LIS->intervalIsInOneMBB(LI)) | 
|  | 321 | continue; | 
|  | 322 | // This is a virtual register that is live across basic blocks. Its | 
|  | 323 | // assigned PhysReg must be marked as live-in to those blocks. | 
|  | 324 | unsigned PhysReg = VRM->getPhys(VirtReg); | 
|  | 325 | assert(PhysReg != VirtRegMap::NO_PHYS_REG && "Unmapped virtual register."); | 
|  | 326 |  | 
| Matthias Braun | 279f836 | 2014-12-10 01:13:08 +0000 | [diff] [blame] | 327 | if (LI.hasSubRanges()) { | 
| Matthias Braun | cc58005 | 2015-09-09 18:07:54 +0000 | [diff] [blame] | 328 | addLiveInsForSubRanges(LI, PhysReg); | 
| Matthias Braun | 279f836 | 2014-12-10 01:13:08 +0000 | [diff] [blame] | 329 | } else { | 
| Matthias Braun | cc58005 | 2015-09-09 18:07:54 +0000 | [diff] [blame] | 330 | // Go over MBB begin positions and see if we have segments covering them. | 
|  | 331 | // The following works because segments and the MBBIndex list are both | 
|  | 332 | // sorted by slot indexes. | 
|  | 333 | SlotIndexes::MBBIndexIterator I = Indexes->MBBIndexBegin(); | 
|  | 334 | for (const auto &Seg : LI) { | 
|  | 335 | I = Indexes->advanceMBBIndex(I, Seg.start); | 
|  | 336 | for (; I != Indexes->MBBIndexEnd() && I->first < Seg.end; ++I) { | 
|  | 337 | MachineBasicBlock *MBB = I->second; | 
|  | 338 | MBB->addLiveIn(PhysReg); | 
|  | 339 | } | 
| Matthias Braun | 279f836 | 2014-12-10 01:13:08 +0000 | [diff] [blame] | 340 | } | 
| Jakob Stoklund Olesen | be33629 | 2012-06-09 00:14:47 +0000 | [diff] [blame] | 341 | } | 
|  | 342 | } | 
| Puyan Lotfi | bb457b9 | 2015-05-22 08:11:26 +0000 | [diff] [blame] | 343 |  | 
|  | 344 | // Sort and unique MBB LiveIns as we've not checked if SubReg/PhysReg were in | 
|  | 345 | // each MBB's LiveIns set before calling addLiveIn on them. | 
|  | 346 | for (MachineBasicBlock &MBB : *MF) | 
|  | 347 | MBB.sortUniqueLiveIns(); | 
| Jakob Stoklund Olesen | be33629 | 2012-06-09 00:14:47 +0000 | [diff] [blame] | 348 | } | 
|  | 349 |  | 
| Matthias Braun | ca4e842 | 2015-06-16 18:22:28 +0000 | [diff] [blame] | 350 | /// Returns true if the given machine operand \p MO only reads undefined lanes. | 
|  | 351 | /// The function only works for use operands with a subregister set. | 
|  | 352 | bool VirtRegRewriter::readsUndefSubreg(const MachineOperand &MO) const { | 
|  | 353 | // Shortcut if the operand is already marked undef. | 
|  | 354 | if (MO.isUndef()) | 
|  | 355 | return true; | 
|  | 356 |  | 
|  | 357 | unsigned Reg = MO.getReg(); | 
|  | 358 | const LiveInterval &LI = LIS->getInterval(Reg); | 
|  | 359 | const MachineInstr &MI = *MO.getParent(); | 
| Duncan P. N. Exon Smith | 3ac9cc6 | 2016-02-27 06:40:41 +0000 | [diff] [blame] | 360 | SlotIndex BaseIndex = LIS->getInstructionIndex(MI); | 
| Matthias Braun | ca4e842 | 2015-06-16 18:22:28 +0000 | [diff] [blame] | 361 | // This code is only meant to handle reading undefined subregisters which | 
|  | 362 | // we couldn't properly detect before. | 
|  | 363 | assert(LI.liveAt(BaseIndex) && | 
|  | 364 | "Reads of completely dead register should be marked undef already"); | 
|  | 365 | unsigned SubRegIdx = MO.getSubReg(); | 
| Krzysztof Parzyszek | a7ed090 | 2016-08-24 13:37:55 +0000 | [diff] [blame] | 366 | assert(SubRegIdx != 0 && LI.hasSubRanges()); | 
| Matthias Braun | e6a2485 | 2015-09-25 21:51:14 +0000 | [diff] [blame] | 367 | LaneBitmask UseMask = TRI->getSubRegIndexLaneMask(SubRegIdx); | 
| Matthias Braun | ca4e842 | 2015-06-16 18:22:28 +0000 | [diff] [blame] | 368 | // See if any of the relevant subregister liveranges is defined at this point. | 
|  | 369 | for (const LiveInterval::SubRange &SR : LI.subranges()) { | 
| Krzysztof Parzyszek | ea9f8ce | 2016-12-16 19:11:56 +0000 | [diff] [blame] | 370 | if ((SR.LaneMask & UseMask).any() && SR.liveAt(BaseIndex)) | 
| Matthias Braun | ca4e842 | 2015-06-16 18:22:28 +0000 | [diff] [blame] | 371 | return false; | 
|  | 372 | } | 
|  | 373 | return true; | 
|  | 374 | } | 
|  | 375 |  | 
| Matthias Braun | 152e7c8 | 2016-07-09 00:19:07 +0000 | [diff] [blame] | 376 | void VirtRegRewriter::handleIdentityCopy(MachineInstr &MI) const { | 
|  | 377 | if (!MI.isIdentityCopy()) | 
|  | 378 | return; | 
|  | 379 | DEBUG(dbgs() << "Identity copy: " << MI); | 
|  | 380 | ++NumIdCopies; | 
|  | 381 |  | 
|  | 382 | // Copies like: | 
|  | 383 | //    %R0 = COPY %R0<undef> | 
|  | 384 | //    %AL = COPY %AL, %EAX<imp-def> | 
|  | 385 | // give us additional liveness information: The target (super-)register | 
|  | 386 | // must not be valid before this point. Replace the COPY with a KILL | 
|  | 387 | // instruction to maintain this information. | 
|  | 388 | if (MI.getOperand(0).isUndef() || MI.getNumOperands() > 2) { | 
|  | 389 | MI.setDesc(TII->get(TargetOpcode::KILL)); | 
|  | 390 | DEBUG(dbgs() << "  replace by: " << MI); | 
|  | 391 | return; | 
|  | 392 | } | 
|  | 393 |  | 
|  | 394 | if (Indexes) | 
| Matthias Braun | fa289ec | 2017-03-17 00:41:33 +0000 | [diff] [blame] | 395 | Indexes->removeSingleMachineInstrFromMaps(MI); | 
|  | 396 | MI.eraseFromBundle(); | 
| Matthias Braun | 152e7c8 | 2016-07-09 00:19:07 +0000 | [diff] [blame] | 397 | DEBUG(dbgs() << "  deleted.\n"); | 
|  | 398 | } | 
|  | 399 |  | 
| Matthias Braun | f0b68d3 | 2017-03-17 00:41:39 +0000 | [diff] [blame] | 400 | /// The liverange splitting logic sometimes produces bundles of copies when | 
|  | 401 | /// subregisters are involved. Expand these into a sequence of copy instructions | 
|  | 402 | /// after processing the last in the bundle. Does not update LiveIntervals | 
|  | 403 | /// which we shouldn't need for this instruction anymore. | 
|  | 404 | void VirtRegRewriter::expandCopyBundle(MachineInstr &MI) const { | 
|  | 405 | if (!MI.isCopy()) | 
|  | 406 | return; | 
|  | 407 |  | 
|  | 408 | if (MI.isBundledWithPred() && !MI.isBundledWithSucc()) { | 
|  | 409 | // Only do this when the complete bundle is made out of COPYs. | 
| Matthias Braun | 8445cbd | 2017-03-21 21:58:08 +0000 | [diff] [blame] | 410 | MachineBasicBlock &MBB = *MI.getParent(); | 
| Matthias Braun | f0b68d3 | 2017-03-17 00:41:39 +0000 | [diff] [blame] | 411 | for (MachineBasicBlock::reverse_instr_iterator I = | 
| Matthias Braun | 8445cbd | 2017-03-21 21:58:08 +0000 | [diff] [blame] | 412 | std::next(MI.getReverseIterator()), E = MBB.instr_rend(); | 
|  | 413 | I != E && I->isBundledWithSucc(); ++I) { | 
| Matthias Braun | f0b68d3 | 2017-03-17 00:41:39 +0000 | [diff] [blame] | 414 | if (!I->isCopy()) | 
|  | 415 | return; | 
|  | 416 | } | 
|  | 417 |  | 
|  | 418 | for (MachineBasicBlock::reverse_instr_iterator I = MI.getReverseIterator(); | 
|  | 419 | I->isBundledWithPred(); ) { | 
|  | 420 | MachineInstr &MI = *I; | 
|  | 421 | ++I; | 
|  | 422 |  | 
|  | 423 | MI.unbundleFromPred(); | 
|  | 424 | if (Indexes) | 
|  | 425 | Indexes->insertMachineInstrInMaps(MI); | 
|  | 426 | } | 
|  | 427 | } | 
|  | 428 | } | 
|  | 429 |  | 
| Quentin Colombet | 647b482 | 2017-08-16 00:17:05 +0000 | [diff] [blame] | 430 | /// Check whether (part of) \p SuperPhysReg is live through \p MI. | 
|  | 431 | /// \pre \p MI defines a subregister of a virtual register that | 
|  | 432 | /// has been assigned to \p SuperPhysReg. | 
|  | 433 | bool VirtRegRewriter::subRegLiveThrough(const MachineInstr &MI, | 
|  | 434 | unsigned SuperPhysReg) const { | 
|  | 435 | SlotIndex MIIndex = LIS->getInstructionIndex(MI); | 
|  | 436 | SlotIndex BeforeMIUses = MIIndex.getBaseIndex(); | 
|  | 437 | SlotIndex AfterMIDefs = MIIndex.getBoundaryIndex(); | 
|  | 438 | for (MCRegUnitIterator Unit(SuperPhysReg, TRI); Unit.isValid(); ++Unit) { | 
|  | 439 | const LiveRange &UnitRange = LIS->getRegUnit(*Unit); | 
|  | 440 | // If the regunit is live both before and after MI, | 
|  | 441 | // we assume it is live through. | 
|  | 442 | // Generally speaking, this is not true, because something like | 
|  | 443 | // "RU = op RU" would match that description. | 
|  | 444 | // However, we know that we are trying to assess whether | 
|  | 445 | // a def of a virtual reg, vreg, is live at the same time of RU. | 
|  | 446 | // If we are in the "RU = op RU" situation, that means that vreg | 
|  | 447 | // is defined at the same time as RU (i.e., "vreg, RU = op RU"). | 
|  | 448 | // Thus, vreg and RU interferes and vreg cannot be assigned to | 
|  | 449 | // SuperPhysReg. Therefore, this situation cannot happen. | 
|  | 450 | if (UnitRange.liveAt(AfterMIDefs) && UnitRange.liveAt(BeforeMIUses)) | 
|  | 451 | return true; | 
|  | 452 | } | 
|  | 453 | return false; | 
|  | 454 | } | 
|  | 455 |  | 
| Jakob Stoklund Olesen | 1224312 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 456 | void VirtRegRewriter::rewrite() { | 
| Matthias Braun | a25e13a | 2015-03-19 00:21:58 +0000 | [diff] [blame] | 457 | bool NoSubRegLiveness = !MRI->subRegLivenessEnabled(); | 
| Jakob Stoklund Olesen | 71d3b89 | 2011-04-27 17:42:31 +0000 | [diff] [blame] | 458 | SmallVector<unsigned, 8> SuperDeads; | 
|  | 459 | SmallVector<unsigned, 8> SuperDefs; | 
| Jakob Stoklund Olesen | 5bfec69 | 2011-02-18 22:03:18 +0000 | [diff] [blame] | 460 | SmallVector<unsigned, 8> SuperKills; | 
| Logan Chien | 18583d7 | 2014-02-25 16:57:28 +0000 | [diff] [blame] | 461 |  | 
| Jakob Stoklund Olesen | 5bfec69 | 2011-02-18 22:03:18 +0000 | [diff] [blame] | 462 | for (MachineFunction::iterator MBBI = MF->begin(), MBBE = MF->end(); | 
|  | 463 | MBBI != MBBE; ++MBBI) { | 
|  | 464 | DEBUG(MBBI->print(dbgs(), Indexes)); | 
| Evan Cheng | d42aba5 | 2012-01-19 07:46:36 +0000 | [diff] [blame] | 465 | for (MachineBasicBlock::instr_iterator | 
|  | 466 | MII = MBBI->instr_begin(), MIE = MBBI->instr_end(); MII != MIE;) { | 
| Duncan P. N. Exon Smith | f1ff53e | 2015-10-09 22:56:24 +0000 | [diff] [blame] | 467 | MachineInstr *MI = &*MII; | 
| Jakob Stoklund Olesen | 5bfec69 | 2011-02-18 22:03:18 +0000 | [diff] [blame] | 468 | ++MII; | 
|  | 469 |  | 
|  | 470 | for (MachineInstr::mop_iterator MOI = MI->operands_begin(), | 
|  | 471 | MOE = MI->operands_end(); MOI != MOE; ++MOI) { | 
|  | 472 | MachineOperand &MO = *MOI; | 
| Jakob Stoklund Olesen | a0cf42f | 2012-02-17 19:07:56 +0000 | [diff] [blame] | 473 |  | 
|  | 474 | // Make sure MRI knows about registers clobbered by regmasks. | 
|  | 475 | if (MO.isRegMask()) | 
|  | 476 | MRI->addPhysRegsUsedFromRegMask(MO.getRegMask()); | 
|  | 477 |  | 
| Jakob Stoklund Olesen | 5bfec69 | 2011-02-18 22:03:18 +0000 | [diff] [blame] | 478 | if (!MO.isReg() || !TargetRegisterInfo::isVirtualRegister(MO.getReg())) | 
|  | 479 | continue; | 
|  | 480 | unsigned VirtReg = MO.getReg(); | 
| Jakob Stoklund Olesen | 1224312 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 481 | unsigned PhysReg = VRM->getPhys(VirtReg); | 
|  | 482 | assert(PhysReg != VirtRegMap::NO_PHYS_REG && | 
|  | 483 | "Instruction uses unmapped VirtReg"); | 
| Jakob Stoklund Olesen | c30a9af | 2012-10-15 21:57:41 +0000 | [diff] [blame] | 484 | assert(!MRI->isReserved(PhysReg) && "Reserved register assignment"); | 
| Jakob Stoklund Olesen | 5bfec69 | 2011-02-18 22:03:18 +0000 | [diff] [blame] | 485 |  | 
|  | 486 | // Preserve semantics of sub-register operands. | 
| Matthias Braun | ca4e842 | 2015-06-16 18:22:28 +0000 | [diff] [blame] | 487 | unsigned SubReg = MO.getSubReg(); | 
|  | 488 | if (SubReg != 0) { | 
|  | 489 | if (NoSubRegLiveness) { | 
|  | 490 | // A virtual register kill refers to the whole register, so we may | 
|  | 491 | // have to add <imp-use,kill> operands for the super-register.  A | 
|  | 492 | // partial redef always kills and redefines the super-register. | 
| Quentin Colombet | 647b482 | 2017-08-16 00:17:05 +0000 | [diff] [blame] | 493 | if ((MO.readsReg() && (MO.isDef() || MO.isKill())) || | 
|  | 494 | (MO.isDef() && subRegLiveThrough(*MI, PhysReg))) | 
| Matthias Braun | ca4e842 | 2015-06-16 18:22:28 +0000 | [diff] [blame] | 495 | SuperKills.push_back(PhysReg); | 
| Jakob Stoklund Olesen | d5d39bb | 2011-10-05 00:01:48 +0000 | [diff] [blame] | 496 |  | 
| Matthias Braun | ca4e842 | 2015-06-16 18:22:28 +0000 | [diff] [blame] | 497 | if (MO.isDef()) { | 
|  | 498 | // Also add implicit defs for the super-register. | 
| Matthias Braun | d70caaf | 2014-12-10 01:13:04 +0000 | [diff] [blame] | 499 | if (MO.isDead()) | 
|  | 500 | SuperDeads.push_back(PhysReg); | 
|  | 501 | else | 
|  | 502 | SuperDefs.push_back(PhysReg); | 
|  | 503 | } | 
| Matthias Braun | ca4e842 | 2015-06-16 18:22:28 +0000 | [diff] [blame] | 504 | } else { | 
|  | 505 | if (MO.isUse()) { | 
|  | 506 | if (readsUndefSubreg(MO)) | 
|  | 507 | // We need to add an <undef> flag if the subregister is | 
|  | 508 | // completely undefined (and we are not adding super-register | 
|  | 509 | // defs). | 
|  | 510 | MO.setIsUndef(true); | 
|  | 511 | } else if (!MO.isDead()) { | 
|  | 512 | assert(MO.isDef()); | 
| Matthias Braun | ca4e842 | 2015-06-16 18:22:28 +0000 | [diff] [blame] | 513 | } | 
| Jakob Stoklund Olesen | d5d39bb | 2011-10-05 00:01:48 +0000 | [diff] [blame] | 514 | } | 
| Jakob Stoklund Olesen | 5bfec69 | 2011-02-18 22:03:18 +0000 | [diff] [blame] | 515 |  | 
| Matthias Braun | fa289ec | 2017-03-17 00:41:33 +0000 | [diff] [blame] | 516 | // The <def,undef> and <def,internal> flags only make sense for | 
|  | 517 | // sub-register defs, and we are substituting a full physreg.  An | 
|  | 518 | // <imp-use,kill> operand from the SuperKills list will represent the | 
|  | 519 | // partial read of the super-register. | 
|  | 520 | if (MO.isDef()) { | 
| Matthias Braun | ca4e842 | 2015-06-16 18:22:28 +0000 | [diff] [blame] | 521 | MO.setIsUndef(false); | 
| Matthias Braun | fa289ec | 2017-03-17 00:41:33 +0000 | [diff] [blame] | 522 | MO.setIsInternalRead(false); | 
|  | 523 | } | 
| Matthias Braun | ca4e842 | 2015-06-16 18:22:28 +0000 | [diff] [blame] | 524 |  | 
| Jakob Stoklund Olesen | 5bfec69 | 2011-02-18 22:03:18 +0000 | [diff] [blame] | 525 | // PhysReg operands cannot have subregister indexes. | 
| Matthias Braun | ca4e842 | 2015-06-16 18:22:28 +0000 | [diff] [blame] | 526 | PhysReg = TRI->getSubReg(PhysReg, SubReg); | 
| Jakob Stoklund Olesen | 5bfec69 | 2011-02-18 22:03:18 +0000 | [diff] [blame] | 527 | assert(PhysReg && "Invalid SubReg for physical register"); | 
|  | 528 | MO.setSubReg(0); | 
|  | 529 | } | 
|  | 530 | // Rewrite. Note we could have used MachineOperand::substPhysReg(), but | 
|  | 531 | // we need the inlining here. | 
|  | 532 | MO.setReg(PhysReg); | 
|  | 533 | } | 
|  | 534 |  | 
|  | 535 | // Add any missing super-register kills after rewriting the whole | 
|  | 536 | // instruction. | 
|  | 537 | while (!SuperKills.empty()) | 
|  | 538 | MI->addRegisterKilled(SuperKills.pop_back_val(), TRI, true); | 
|  | 539 |  | 
| Jakob Stoklund Olesen | 71d3b89 | 2011-04-27 17:42:31 +0000 | [diff] [blame] | 540 | while (!SuperDeads.empty()) | 
|  | 541 | MI->addRegisterDead(SuperDeads.pop_back_val(), TRI, true); | 
|  | 542 |  | 
|  | 543 | while (!SuperDefs.empty()) | 
|  | 544 | MI->addRegisterDefined(SuperDefs.pop_back_val(), TRI); | 
|  | 545 |  | 
| Jakob Stoklund Olesen | 5bfec69 | 2011-02-18 22:03:18 +0000 | [diff] [blame] | 546 | DEBUG(dbgs() << "> " << *MI); | 
|  | 547 |  | 
| Matthias Braun | f0b68d3 | 2017-03-17 00:41:39 +0000 | [diff] [blame] | 548 | expandCopyBundle(*MI); | 
|  | 549 |  | 
| Matthias Braun | 152e7c8 | 2016-07-09 00:19:07 +0000 | [diff] [blame] | 550 | // We can remove identity copies right now. | 
|  | 551 | handleIdentityCopy(*MI); | 
| Jakob Stoklund Olesen | 5bfec69 | 2011-02-18 22:03:18 +0000 | [diff] [blame] | 552 | } | 
|  | 553 | } | 
| Jakob Stoklund Olesen | 5bfec69 | 2011-02-18 22:03:18 +0000 | [diff] [blame] | 554 | } |