Eugene Zelenko | 16ffaf8 | 2017-09-13 21:15:20 +0000 | [diff] [blame] | 1 | //===- llvm/CodeGen/VirtRegMap.cpp - Virtual Register Map -----------------===// |
Alkis Evlogimenos | 34d9bc9 | 2004-02-23 23:08:11 +0000 | [diff] [blame] | 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" |
Eugene Zelenko | 16ffaf8 | 2017-09-13 21:15:20 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SmallVector.h" |
Chandler Carruth | 1decd56 | 2014-03-04 10:07:28 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/Statistic.h" |
Eugene Zelenko | 16ffaf8 | 2017-09-13 21:15:20 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/LiveInterval.h" |
Matthias Braun | fa621d2 | 2017-12-13 02:51:04 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/LiveIntervals.h" |
Matthias Braun | 209f048 | 2017-12-18 23:19:44 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/LiveStacks.h" |
Eugene Zelenko | 16ffaf8 | 2017-09-13 21:15:20 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/MachineBasicBlock.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" |
Eugene Zelenko | 16ffaf8 | 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 | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 32 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Eugene Zelenko | 16ffaf8 | 2017-09-13 21:15:20 +0000 | [diff] [blame] | 33 | #include "llvm/CodeGen/SlotIndexes.h" |
David Blaikie | 4831923 | 2017-11-08 01:01:31 +0000 | [diff] [blame] | 34 | #include "llvm/CodeGen/TargetInstrInfo.h" |
David Blaikie | e3a9b4c | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 35 | #include "llvm/CodeGen/TargetOpcodes.h" |
| 36 | #include "llvm/CodeGen/TargetRegisterInfo.h" |
| 37 | #include "llvm/CodeGen/TargetSubtargetInfo.h" |
Nico Weber | 0f38c60 | 2018-04-30 14:59:11 +0000 | [diff] [blame] | 38 | #include "llvm/Config/llvm-config.h" |
Eugene Zelenko | 16ffaf8 | 2017-09-13 21:15:20 +0000 | [diff] [blame] | 39 | #include "llvm/MC/LaneBitmask.h" |
| 40 | #include "llvm/Pass.h" |
Chris Lattner | a4f0b3a | 2006-08-27 12:54:02 +0000 | [diff] [blame] | 41 | #include "llvm/Support/Compiler.h" |
Evan Cheng | 752272a | 2009-02-11 08:24:21 +0000 | [diff] [blame] | 42 | #include "llvm/Support/Debug.h" |
Daniel Dunbar | 1cd1d98 | 2009-07-24 10:36:58 +0000 | [diff] [blame] | 43 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | 16ffaf8 | 2017-09-13 21:15:20 +0000 | [diff] [blame] | 44 | #include <cassert> |
| 45 | #include <iterator> |
| 46 | #include <utility> |
| 47 | |
Alkis Evlogimenos | 34d9bc9 | 2004-02-23 23:08:11 +0000 | [diff] [blame] | 48 | using namespace llvm; |
| 49 | |
Chandler Carruth | 8677f2f | 2014-04-22 02:02:50 +0000 | [diff] [blame] | 50 | #define DEBUG_TYPE "regalloc" |
| 51 | |
Jakob Stoklund Olesen | 01afdb3 | 2011-09-15 18:31:13 +0000 | [diff] [blame] | 52 | STATISTIC(NumSpillSlots, "Number of spill slots allocated"); |
| 53 | STATISTIC(NumIdCopies, "Number of identity moves eliminated after rewriting"); |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 54 | |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 55 | //===----------------------------------------------------------------------===// |
| 56 | // VirtRegMap implementation |
| 57 | //===----------------------------------------------------------------------===// |
| 58 | |
Owen Anderson | 49c8aa0 | 2009-03-13 05:55:11 +0000 | [diff] [blame] | 59 | char VirtRegMap::ID = 0; |
| 60 | |
Owen Anderson | ce665bd | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 61 | INITIALIZE_PASS(VirtRegMap, "virtregmap", "Virtual Register Map", false, false) |
Owen Anderson | 49c8aa0 | 2009-03-13 05:55:11 +0000 | [diff] [blame] | 62 | |
| 63 | bool VirtRegMap::runOnMachineFunction(MachineFunction &mf) { |
Evan Cheng | 90f95f8 | 2009-06-14 20:22:55 +0000 | [diff] [blame] | 64 | MRI = &mf.getRegInfo(); |
Eric Christopher | 6035518 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 65 | TII = mf.getSubtarget().getInstrInfo(); |
| 66 | TRI = mf.getSubtarget().getRegisterInfo(); |
Owen Anderson | 49c8aa0 | 2009-03-13 05:55:11 +0000 | [diff] [blame] | 67 | MF = &mf; |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 68 | |
Owen Anderson | 49c8aa0 | 2009-03-13 05:55:11 +0000 | [diff] [blame] | 69 | Virt2PhysMap.clear(); |
| 70 | Virt2StackSlotMap.clear(); |
Owen Anderson | 49c8aa0 | 2009-03-13 05:55:11 +0000 | [diff] [blame] | 71 | Virt2SplitMap.clear(); |
Mike Stump | fe095f3 | 2009-05-04 18:40:41 +0000 | [diff] [blame] | 72 | |
Chris Lattner | 2926869 | 2006-09-05 02:12:02 +0000 | [diff] [blame] | 73 | grow(); |
Owen Anderson | 49c8aa0 | 2009-03-13 05:55:11 +0000 | [diff] [blame] | 74 | return false; |
Chris Lattner | 2926869 | 2006-09-05 02:12:02 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 77 | void VirtRegMap::grow() { |
Jakob Stoklund Olesen | 42e9c96 | 2011-01-09 21:58:20 +0000 | [diff] [blame] | 78 | unsigned NumRegs = MF->getRegInfo().getNumVirtRegs(); |
| 79 | Virt2PhysMap.resize(NumRegs); |
| 80 | Virt2StackSlotMap.resize(NumRegs); |
Jakob Stoklund Olesen | 42e9c96 | 2011-01-09 21:58:20 +0000 | [diff] [blame] | 81 | Virt2SplitMap.resize(NumRegs); |
Alkis Evlogimenos | 34d9bc9 | 2004-02-23 23:08:11 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Matthias Braun | 9b4cf76 | 2017-06-08 21:30:54 +0000 | [diff] [blame] | 84 | void VirtRegMap::assignVirt2Phys(unsigned virtReg, MCPhysReg physReg) { |
| 85 | assert(TargetRegisterInfo::isVirtualRegister(virtReg) && |
| 86 | TargetRegisterInfo::isPhysicalRegister(physReg)); |
| 87 | assert(Virt2PhysMap[virtReg] == NO_PHYS_REG && |
| 88 | "attempt to assign physical register to already mapped " |
| 89 | "virtual register"); |
| 90 | assert(!getRegInfo().isReserved(physReg) && |
| 91 | "Attempt to map virtReg to a reserved physReg"); |
| 92 | Virt2PhysMap[virtReg] = physReg; |
| 93 | } |
| 94 | |
Jakob Stoklund Olesen | b55e91e | 2010-11-16 00:41:01 +0000 | [diff] [blame] | 95 | unsigned VirtRegMap::createSpillSlot(const TargetRegisterClass *RC) { |
Krzysztof Parzyszek | 36d7c2b | 2017-04-24 18:55:33 +0000 | [diff] [blame] | 96 | unsigned Size = TRI->getSpillSize(*RC); |
| 97 | unsigned Align = TRI->getSpillAlignment(*RC); |
| 98 | int SS = MF->getFrameInfo().CreateSpillStackObject(Size, Align); |
Jakob Stoklund Olesen | 01afdb3 | 2011-09-15 18:31:13 +0000 | [diff] [blame] | 99 | ++NumSpillSlots; |
Jakob Stoklund Olesen | b55e91e | 2010-11-16 00:41:01 +0000 | [diff] [blame] | 100 | return SS; |
| 101 | } |
| 102 | |
Jakob Stoklund Olesen | 980bddf | 2012-12-04 00:30:22 +0000 | [diff] [blame] | 103 | bool VirtRegMap::hasPreferredPhys(unsigned VirtReg) { |
| 104 | unsigned Hint = MRI->getSimpleHint(VirtReg); |
| 105 | if (!Hint) |
Matt Arsenault | 5fff0fe | 2016-06-02 18:37:21 +0000 | [diff] [blame] | 106 | return false; |
Jakob Stoklund Olesen | 980bddf | 2012-12-04 00:30:22 +0000 | [diff] [blame] | 107 | if (TargetRegisterInfo::isVirtualRegister(Hint)) |
| 108 | Hint = getPhys(Hint); |
| 109 | return getPhys(VirtReg) == Hint; |
| 110 | } |
| 111 | |
Jakob Stoklund Olesen | fc63744 | 2012-12-03 23:23:50 +0000 | [diff] [blame] | 112 | bool VirtRegMap::hasKnownPreference(unsigned VirtReg) { |
| 113 | std::pair<unsigned, unsigned> Hint = MRI->getRegAllocationHint(VirtReg); |
| 114 | if (TargetRegisterInfo::isPhysicalRegister(Hint.second)) |
| 115 | return true; |
| 116 | if (TargetRegisterInfo::isVirtualRegister(Hint.second)) |
| 117 | return hasPhys(Hint.second); |
| 118 | return false; |
| 119 | } |
| 120 | |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 121 | int VirtRegMap::assignVirt2StackSlot(unsigned virtReg) { |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 122 | assert(TargetRegisterInfo::isVirtualRegister(virtReg)); |
Chris Lattner | 7f690e6 | 2004-09-30 02:15:18 +0000 | [diff] [blame] | 123 | assert(Virt2StackSlotMap[virtReg] == NO_STACK_SLOT && |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 124 | "attempt to assign stack slot to already spilled register"); |
Owen Anderson | 49c8aa0 | 2009-03-13 05:55:11 +0000 | [diff] [blame] | 125 | const TargetRegisterClass* RC = MF->getRegInfo().getRegClass(virtReg); |
Jakob Stoklund Olesen | b55e91e | 2010-11-16 00:41:01 +0000 | [diff] [blame] | 126 | return Virt2StackSlotMap[virtReg] = createSpillSlot(RC); |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Evan Cheng | d365312 | 2008-02-27 03:04:06 +0000 | [diff] [blame] | 129 | void VirtRegMap::assignVirt2StackSlot(unsigned virtReg, int SS) { |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 130 | assert(TargetRegisterInfo::isVirtualRegister(virtReg)); |
Chris Lattner | 7f690e6 | 2004-09-30 02:15:18 +0000 | [diff] [blame] | 131 | assert(Virt2StackSlotMap[virtReg] == NO_STACK_SLOT && |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 132 | "attempt to assign stack slot to already spilled register"); |
Evan Cheng | d365312 | 2008-02-27 03:04:06 +0000 | [diff] [blame] | 133 | assert((SS >= 0 || |
Matthias Braun | f79c57a | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 134 | (SS >= MF->getFrameInfo().getObjectIndexBegin())) && |
Evan Cheng | 9193514 | 2007-04-04 07:40:01 +0000 | [diff] [blame] | 135 | "illegal fixed frame index"); |
Evan Cheng | d365312 | 2008-02-27 03:04:06 +0000 | [diff] [blame] | 136 | Virt2StackSlotMap[virtReg] = SS; |
Alkis Evlogimenos | 38af59a | 2004-05-29 20:38:05 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Jakob Stoklund Olesen | 05ec712 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 139 | void VirtRegMap::print(raw_ostream &OS, const Module*) const { |
| 140 | OS << "********** REGISTER MAP **********\n"; |
| 141 | for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) { |
| 142 | unsigned Reg = TargetRegisterInfo::index2VirtReg(i); |
| 143 | if (Virt2PhysMap[Reg] != (unsigned)VirtRegMap::NO_PHYS_REG) { |
Francis Visoiu Mistrih | accb337 | 2017-11-28 12:42:37 +0000 | [diff] [blame] | 144 | OS << '[' << printReg(Reg, TRI) << " -> " |
| 145 | << printReg(Virt2PhysMap[Reg], TRI) << "] " |
Craig Topper | a5babc8 | 2014-11-17 05:50:14 +0000 | [diff] [blame] | 146 | << TRI->getRegClassName(MRI->getRegClass(Reg)) << "\n"; |
Jakob Stoklund Olesen | 05ec712 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
| 150 | for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) { |
| 151 | unsigned Reg = TargetRegisterInfo::index2VirtReg(i); |
| 152 | if (Virt2StackSlotMap[Reg] != VirtRegMap::NO_STACK_SLOT) { |
Francis Visoiu Mistrih | accb337 | 2017-11-28 12:42:37 +0000 | [diff] [blame] | 153 | OS << '[' << printReg(Reg, TRI) << " -> fi#" << Virt2StackSlotMap[Reg] |
Craig Topper | a5babc8 | 2014-11-17 05:50:14 +0000 | [diff] [blame] | 154 | << "] " << TRI->getRegClassName(MRI->getRegClass(Reg)) << "\n"; |
Jakob Stoklund Olesen | 05ec712 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | OS << '\n'; |
| 158 | } |
| 159 | |
Aaron Ballman | 1d03d38 | 2017-10-15 14:32:27 +0000 | [diff] [blame] | 160 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Yaron Keren | 5530798 | 2016-01-29 20:50:44 +0000 | [diff] [blame] | 161 | LLVM_DUMP_METHOD void VirtRegMap::dump() const { |
Jakob Stoklund Olesen | 05ec712 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 162 | print(dbgs()); |
| 163 | } |
Manman Ren | 77e300e | 2012-09-06 19:06:06 +0000 | [diff] [blame] | 164 | #endif |
Jakob Stoklund Olesen | 05ec712 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 165 | |
| 166 | //===----------------------------------------------------------------------===// |
| 167 | // VirtRegRewriter |
| 168 | //===----------------------------------------------------------------------===// |
| 169 | // |
| 170 | // The VirtRegRewriter is the last of the register allocator passes. |
| 171 | // It rewrites virtual registers to physical registers as specified in the |
| 172 | // VirtRegMap analysis. It also updates live-in information on basic blocks |
| 173 | // according to LiveIntervals. |
| 174 | // |
| 175 | namespace { |
Eugene Zelenko | 16ffaf8 | 2017-09-13 21:15:20 +0000 | [diff] [blame] | 176 | |
Jakob Stoklund Olesen | 05ec712 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 177 | class VirtRegRewriter : public MachineFunctionPass { |
| 178 | MachineFunction *MF; |
Jakob Stoklund Olesen | 05ec712 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 179 | const TargetRegisterInfo *TRI; |
| 180 | const TargetInstrInfo *TII; |
| 181 | MachineRegisterInfo *MRI; |
| 182 | SlotIndexes *Indexes; |
| 183 | LiveIntervals *LIS; |
| 184 | VirtRegMap *VRM; |
| 185 | |
| 186 | void rewrite(); |
| 187 | void addMBBLiveIns(); |
Matthias Braun | 944aaeb | 2015-06-16 18:22:28 +0000 | [diff] [blame] | 188 | bool readsUndefSubreg(const MachineOperand &MO) const; |
Matthias Braun | 6111cd8 | 2015-09-09 18:07:54 +0000 | [diff] [blame] | 189 | void addLiveInsForSubRanges(const LiveInterval &LI, unsigned PhysReg) const; |
Matthias Braun | 79519fe | 2016-07-09 00:19:07 +0000 | [diff] [blame] | 190 | void handleIdentityCopy(MachineInstr &MI) const; |
Matthias Braun | 94ebfcb | 2017-03-17 00:41:39 +0000 | [diff] [blame] | 191 | void expandCopyBundle(MachineInstr &MI) const; |
Quentin Colombet | 156a103 | 2017-08-16 00:17:05 +0000 | [diff] [blame] | 192 | bool subRegLiveThrough(const MachineInstr &MI, unsigned SuperPhysReg) const; |
Matthias Braun | 6111cd8 | 2015-09-09 18:07:54 +0000 | [diff] [blame] | 193 | |
Jakob Stoklund Olesen | 05ec712 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 194 | public: |
| 195 | static char ID; |
Eugene Zelenko | 16ffaf8 | 2017-09-13 21:15:20 +0000 | [diff] [blame] | 196 | |
Jakob Stoklund Olesen | 05ec712 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 197 | VirtRegRewriter() : MachineFunctionPass(ID) {} |
| 198 | |
Craig Topper | 9f998de | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 199 | void getAnalysisUsage(AnalysisUsage &AU) const override; |
Jakob Stoklund Olesen | 05ec712 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 200 | |
Craig Topper | 9f998de | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 201 | bool runOnMachineFunction(MachineFunction&) override; |
Eugene Zelenko | 16ffaf8 | 2017-09-13 21:15:20 +0000 | [diff] [blame] | 202 | |
Derek Schuff | b65f550 | 2016-03-29 17:40:22 +0000 | [diff] [blame] | 203 | MachineFunctionProperties getSetProperties() const override { |
| 204 | return MachineFunctionProperties().set( |
Matthias Braun | 690a3cb | 2016-08-25 01:27:13 +0000 | [diff] [blame] | 205 | MachineFunctionProperties::Property::NoVRegs); |
Derek Schuff | b65f550 | 2016-03-29 17:40:22 +0000 | [diff] [blame] | 206 | } |
Jakob Stoklund Olesen | 05ec712 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 207 | }; |
Eugene Zelenko | 16ffaf8 | 2017-09-13 21:15:20 +0000 | [diff] [blame] | 208 | |
Jakob Stoklund Olesen | 05ec712 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 209 | } // end anonymous namespace |
| 210 | |
Eugene Zelenko | 16ffaf8 | 2017-09-13 21:15:20 +0000 | [diff] [blame] | 211 | char VirtRegRewriter::ID = 0; |
| 212 | |
Jakob Stoklund Olesen | 05ec712 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 213 | char &llvm::VirtRegRewriterID = VirtRegRewriter::ID; |
| 214 | |
| 215 | INITIALIZE_PASS_BEGIN(VirtRegRewriter, "virtregrewriter", |
| 216 | "Virtual Register Rewriter", false, false) |
| 217 | INITIALIZE_PASS_DEPENDENCY(SlotIndexes) |
| 218 | INITIALIZE_PASS_DEPENDENCY(LiveIntervals) |
| 219 | INITIALIZE_PASS_DEPENDENCY(LiveDebugVariables) |
Evan Cheng | bb36a43 | 2012-09-21 20:04:28 +0000 | [diff] [blame] | 220 | INITIALIZE_PASS_DEPENDENCY(LiveStacks) |
Jakob Stoklund Olesen | 05ec712 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 221 | INITIALIZE_PASS_DEPENDENCY(VirtRegMap) |
| 222 | INITIALIZE_PASS_END(VirtRegRewriter, "virtregrewriter", |
| 223 | "Virtual Register Rewriter", false, false) |
| 224 | |
Jakob Stoklund Olesen | 05ec712 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 225 | void VirtRegRewriter::getAnalysisUsage(AnalysisUsage &AU) const { |
| 226 | AU.setPreservesCFG(); |
| 227 | AU.addRequired<LiveIntervals>(); |
| 228 | AU.addRequired<SlotIndexes>(); |
| 229 | AU.addPreserved<SlotIndexes>(); |
| 230 | AU.addRequired<LiveDebugVariables>(); |
Evan Cheng | bb36a43 | 2012-09-21 20:04:28 +0000 | [diff] [blame] | 231 | AU.addRequired<LiveStacks>(); |
| 232 | AU.addPreserved<LiveStacks>(); |
Jakob Stoklund Olesen | 05ec712 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 233 | AU.addRequired<VirtRegMap>(); |
| 234 | MachineFunctionPass::getAnalysisUsage(AU); |
| 235 | } |
| 236 | |
| 237 | bool VirtRegRewriter::runOnMachineFunction(MachineFunction &fn) { |
| 238 | MF = &fn; |
Eric Christopher | 5db6cf4 | 2014-10-13 21:57:44 +0000 | [diff] [blame] | 239 | TRI = MF->getSubtarget().getRegisterInfo(); |
| 240 | TII = MF->getSubtarget().getInstrInfo(); |
Jakob Stoklund Olesen | 05ec712 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 241 | MRI = &MF->getRegInfo(); |
| 242 | Indexes = &getAnalysis<SlotIndexes>(); |
| 243 | LIS = &getAnalysis<LiveIntervals>(); |
| 244 | VRM = &getAnalysis<VirtRegMap>(); |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 245 | LLVM_DEBUG(dbgs() << "********** REWRITE VIRTUAL REGISTERS **********\n" |
| 246 | << "********** Function: " << MF->getName() << '\n'); |
| 247 | LLVM_DEBUG(VRM->dump()); |
Jakob Stoklund Olesen | 05ec712 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 248 | |
| 249 | // Add kill flags while we still have virtual registers. |
Jakob Stoklund Olesen | e617ccb | 2012-09-06 18:15:18 +0000 | [diff] [blame] | 250 | LIS->addKillFlags(VRM); |
Jakob Stoklund Olesen | 05ec712 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 251 | |
Jakob Stoklund Olesen | fe17bdb | 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 | 05ec712 | 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 | 6111cd8 | 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 | 16ffaf8 | 2017-09-13 21:15:20 +0000 | [diff] [blame] | 273 | using SubRangeIteratorPair = |
| 274 | std::pair<const LiveInterval::SubRange *, LiveInterval::const_iterator>; |
| 275 | |
Matthias Braun | 6111cd8 | 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 | d6ca3f0 | 2016-12-15 14:36:06 +0000 | [diff] [blame] | 294 | LaneBitmask LaneMask; |
Matthias Braun | 6111cd8 | 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 | d6ca3f0 | 2016-12-15 14:36:06 +0000 | [diff] [blame] | 305 | if (LaneMask.none()) |
Matthias Braun | 6111cd8 | 2015-09-09 18:07:54 +0000 | [diff] [blame] | 306 | continue; |
| 307 | MachineBasicBlock *MBB = MBBI->second; |
Matthias Braun | af5ff60 | 2015-09-09 18:08:03 +0000 | [diff] [blame] | 308 | MBB->addLiveIn(PhysReg, LaneMask); |
Matthias Braun | 6111cd8 | 2015-09-09 18:07:54 +0000 | [diff] [blame] | 309 | } |
| 310 | } |
| 311 | |
Jakob Stoklund Olesen | fe17bdb | 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 | fe17bdb | 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 | 8f08002 | 2014-12-10 01:13:08 +0000 | [diff] [blame] | 327 | if (LI.hasSubRanges()) { |
Matthias Braun | 6111cd8 | 2015-09-09 18:07:54 +0000 | [diff] [blame] | 328 | addLiveInsForSubRanges(LI, PhysReg); |
Matthias Braun | 8f08002 | 2014-12-10 01:13:08 +0000 | [diff] [blame] | 329 | } else { |
Matthias Braun | 6111cd8 | 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 | 8f08002 | 2014-12-10 01:13:08 +0000 | [diff] [blame] | 340 | } |
Jakob Stoklund Olesen | fe17bdb | 2012-06-09 00:14:47 +0000 | [diff] [blame] | 341 | } |
| 342 | } |
Puyan Lotfi | d9a08b0 | 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 | fe17bdb | 2012-06-09 00:14:47 +0000 | [diff] [blame] | 348 | } |
| 349 | |
Matthias Braun | 944aaeb | 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 | 42e1835 | 2016-02-27 06:40:41 +0000 | [diff] [blame] | 360 | SlotIndex BaseIndex = LIS->getInstructionIndex(MI); |
Matthias Braun | 944aaeb | 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 | 31a5f88 | 2016-08-24 13:37:55 +0000 | [diff] [blame] | 366 | assert(SubRegIdx != 0 && LI.hasSubRanges()); |
Matthias Braun | dfc5b65 | 2015-09-25 21:51:14 +0000 | [diff] [blame] | 367 | LaneBitmask UseMask = TRI->getSubRegIndexLaneMask(SubRegIdx); |
Matthias Braun | 944aaeb | 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 | 308c60d | 2016-12-16 19:11:56 +0000 | [diff] [blame] | 370 | if ((SR.LaneMask & UseMask).any() && SR.liveAt(BaseIndex)) |
Matthias Braun | 944aaeb | 2015-06-16 18:22:28 +0000 | [diff] [blame] | 371 | return false; |
| 372 | } |
| 373 | return true; |
| 374 | } |
| 375 | |
Matthias Braun | 79519fe | 2016-07-09 00:19:07 +0000 | [diff] [blame] | 376 | void VirtRegRewriter::handleIdentityCopy(MachineInstr &MI) const { |
| 377 | if (!MI.isIdentityCopy()) |
| 378 | return; |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 379 | LLVM_DEBUG(dbgs() << "Identity copy: " << MI); |
Matthias Braun | 79519fe | 2016-07-09 00:19:07 +0000 | [diff] [blame] | 380 | ++NumIdCopies; |
| 381 | |
| 382 | // Copies like: |
Francis Visoiu Mistrih | fd11bc0 | 2017-12-07 10:40:31 +0000 | [diff] [blame] | 383 | // %r0 = COPY undef %r0 |
| 384 | // %al = COPY %al, implicit-def %eax |
Matthias Braun | 79519fe | 2016-07-09 00:19:07 +0000 | [diff] [blame] | 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)); |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 390 | LLVM_DEBUG(dbgs() << " replace by: " << MI); |
Matthias Braun | 79519fe | 2016-07-09 00:19:07 +0000 | [diff] [blame] | 391 | return; |
| 392 | } |
| 393 | |
| 394 | if (Indexes) |
Matthias Braun | 852989f | 2017-03-17 00:41:33 +0000 | [diff] [blame] | 395 | Indexes->removeSingleMachineInstrFromMaps(MI); |
| 396 | MI.eraseFromBundle(); |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 397 | LLVM_DEBUG(dbgs() << " deleted.\n"); |
Matthias Braun | 79519fe | 2016-07-09 00:19:07 +0000 | [diff] [blame] | 398 | } |
| 399 | |
Matthias Braun | 94ebfcb | 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()) { |
Justin Bogner | 3d29404 | 2018-06-14 19:24:03 +0000 | [diff] [blame] | 409 | SmallVector<MachineInstr *, 2> MIs({&MI}); |
| 410 | |
Matthias Braun | 94ebfcb | 2017-03-17 00:41:39 +0000 | [diff] [blame] | 411 | // Only do this when the complete bundle is made out of COPYs. |
Matthias Braun | 9c890fc | 2017-03-21 21:58:08 +0000 | [diff] [blame] | 412 | MachineBasicBlock &MBB = *MI.getParent(); |
Matthias Braun | 94ebfcb | 2017-03-17 00:41:39 +0000 | [diff] [blame] | 413 | for (MachineBasicBlock::reverse_instr_iterator I = |
Matthias Braun | 9c890fc | 2017-03-21 21:58:08 +0000 | [diff] [blame] | 414 | std::next(MI.getReverseIterator()), E = MBB.instr_rend(); |
| 415 | I != E && I->isBundledWithSucc(); ++I) { |
Matthias Braun | 94ebfcb | 2017-03-17 00:41:39 +0000 | [diff] [blame] | 416 | if (!I->isCopy()) |
| 417 | return; |
Justin Bogner | 3d29404 | 2018-06-14 19:24:03 +0000 | [diff] [blame] | 418 | MIs.push_back(&*I); |
| 419 | } |
| 420 | MachineInstr *FirstMI = MIs.back(); |
| 421 | |
| 422 | auto anyRegsAlias = [](const MachineInstr *Dst, |
| 423 | ArrayRef<MachineInstr *> Srcs, |
| 424 | const TargetRegisterInfo *TRI) { |
| 425 | for (const MachineInstr *Src : Srcs) |
| 426 | if (Src != Dst) |
| 427 | if (TRI->regsOverlap(Dst->getOperand(0).getReg(), |
| 428 | Src->getOperand(1).getReg())) |
| 429 | return true; |
| 430 | return false; |
| 431 | }; |
| 432 | |
| 433 | // If any of the destination registers in the bundle of copies alias any of |
| 434 | // the source registers, try to schedule the instructions to avoid any |
| 435 | // clobbering. |
| 436 | for (int E = MIs.size(), PrevE = E; E > 1; PrevE = E) { |
| 437 | for (int I = E; I--; ) |
| 438 | if (!anyRegsAlias(MIs[I], makeArrayRef(MIs).take_front(E), TRI)) { |
| 439 | if (I + 1 != E) |
| 440 | std::swap(MIs[I], MIs[E - 1]); |
| 441 | --E; |
| 442 | } |
| 443 | if (PrevE == E) { |
| 444 | MF->getFunction().getContext().emitError( |
| 445 | "register rewriting failed: cycle in copy bundle"); |
| 446 | break; |
| 447 | } |
Matthias Braun | 94ebfcb | 2017-03-17 00:41:39 +0000 | [diff] [blame] | 448 | } |
| 449 | |
Justin Bogner | 3d29404 | 2018-06-14 19:24:03 +0000 | [diff] [blame] | 450 | MachineInstr *BundleStart = FirstMI; |
| 451 | for (MachineInstr *BundledMI : llvm::reverse(MIs)) { |
| 452 | // If instruction is in the middle of the bundle, move it before the |
| 453 | // bundle starts, otherwise, just unbundle it. When we get to the last |
| 454 | // instruction, the bundle will have been completely undone. |
| 455 | if (BundledMI != BundleStart) { |
| 456 | BundledMI->removeFromBundle(); |
| 457 | MBB.insert(FirstMI, BundledMI); |
| 458 | } else if (BundledMI->isBundledWithSucc()) { |
| 459 | BundledMI->unbundleFromSucc(); |
| 460 | BundleStart = &*std::next(BundledMI->getIterator()); |
| 461 | } |
Matthias Braun | 94ebfcb | 2017-03-17 00:41:39 +0000 | [diff] [blame] | 462 | |
Justin Bogner | 3d29404 | 2018-06-14 19:24:03 +0000 | [diff] [blame] | 463 | if (Indexes && BundledMI != FirstMI) |
| 464 | Indexes->insertMachineInstrInMaps(*BundledMI); |
Matthias Braun | 94ebfcb | 2017-03-17 00:41:39 +0000 | [diff] [blame] | 465 | } |
| 466 | } |
| 467 | } |
| 468 | |
Quentin Colombet | 156a103 | 2017-08-16 00:17:05 +0000 | [diff] [blame] | 469 | /// Check whether (part of) \p SuperPhysReg is live through \p MI. |
| 470 | /// \pre \p MI defines a subregister of a virtual register that |
| 471 | /// has been assigned to \p SuperPhysReg. |
| 472 | bool VirtRegRewriter::subRegLiveThrough(const MachineInstr &MI, |
| 473 | unsigned SuperPhysReg) const { |
| 474 | SlotIndex MIIndex = LIS->getInstructionIndex(MI); |
| 475 | SlotIndex BeforeMIUses = MIIndex.getBaseIndex(); |
| 476 | SlotIndex AfterMIDefs = MIIndex.getBoundaryIndex(); |
| 477 | for (MCRegUnitIterator Unit(SuperPhysReg, TRI); Unit.isValid(); ++Unit) { |
| 478 | const LiveRange &UnitRange = LIS->getRegUnit(*Unit); |
| 479 | // If the regunit is live both before and after MI, |
| 480 | // we assume it is live through. |
| 481 | // Generally speaking, this is not true, because something like |
| 482 | // "RU = op RU" would match that description. |
| 483 | // However, we know that we are trying to assess whether |
| 484 | // a def of a virtual reg, vreg, is live at the same time of RU. |
| 485 | // If we are in the "RU = op RU" situation, that means that vreg |
| 486 | // is defined at the same time as RU (i.e., "vreg, RU = op RU"). |
| 487 | // Thus, vreg and RU interferes and vreg cannot be assigned to |
| 488 | // SuperPhysReg. Therefore, this situation cannot happen. |
| 489 | if (UnitRange.liveAt(AfterMIDefs) && UnitRange.liveAt(BeforeMIUses)) |
| 490 | return true; |
| 491 | } |
| 492 | return false; |
| 493 | } |
| 494 | |
Jakob Stoklund Olesen | 05ec712 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 495 | void VirtRegRewriter::rewrite() { |
Matthias Braun | 5101c89 | 2015-03-19 00:21:58 +0000 | [diff] [blame] | 496 | bool NoSubRegLiveness = !MRI->subRegLivenessEnabled(); |
Jakob Stoklund Olesen | 93e110b | 2011-04-27 17:42:31 +0000 | [diff] [blame] | 497 | SmallVector<unsigned, 8> SuperDeads; |
| 498 | SmallVector<unsigned, 8> SuperDefs; |
Jakob Stoklund Olesen | ba05c01 | 2011-02-18 22:03:18 +0000 | [diff] [blame] | 499 | SmallVector<unsigned, 8> SuperKills; |
Logan Chien | 28713bd | 2014-02-25 16:57:28 +0000 | [diff] [blame] | 500 | |
Jakob Stoklund Olesen | ba05c01 | 2011-02-18 22:03:18 +0000 | [diff] [blame] | 501 | for (MachineFunction::iterator MBBI = MF->begin(), MBBE = MF->end(); |
| 502 | MBBI != MBBE; ++MBBI) { |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 503 | LLVM_DEBUG(MBBI->print(dbgs(), Indexes)); |
Evan Cheng | 3f9c251 | 2012-01-19 07:46:36 +0000 | [diff] [blame] | 504 | for (MachineBasicBlock::instr_iterator |
| 505 | MII = MBBI->instr_begin(), MIE = MBBI->instr_end(); MII != MIE;) { |
Duncan P. N. Exon Smith | ac4d7b6 | 2015-10-09 22:56:24 +0000 | [diff] [blame] | 506 | MachineInstr *MI = &*MII; |
Jakob Stoklund Olesen | ba05c01 | 2011-02-18 22:03:18 +0000 | [diff] [blame] | 507 | ++MII; |
| 508 | |
| 509 | for (MachineInstr::mop_iterator MOI = MI->operands_begin(), |
| 510 | MOE = MI->operands_end(); MOI != MOE; ++MOI) { |
| 511 | MachineOperand &MO = *MOI; |
Jakob Stoklund Olesen | d9f0ff5 | 2012-02-17 19:07:56 +0000 | [diff] [blame] | 512 | |
| 513 | // Make sure MRI knows about registers clobbered by regmasks. |
| 514 | if (MO.isRegMask()) |
| 515 | MRI->addPhysRegsUsedFromRegMask(MO.getRegMask()); |
| 516 | |
Jakob Stoklund Olesen | ba05c01 | 2011-02-18 22:03:18 +0000 | [diff] [blame] | 517 | if (!MO.isReg() || !TargetRegisterInfo::isVirtualRegister(MO.getReg())) |
| 518 | continue; |
| 519 | unsigned VirtReg = MO.getReg(); |
Jakob Stoklund Olesen | 05ec712 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 520 | unsigned PhysReg = VRM->getPhys(VirtReg); |
| 521 | assert(PhysReg != VirtRegMap::NO_PHYS_REG && |
| 522 | "Instruction uses unmapped VirtReg"); |
Jakob Stoklund Olesen | fb9ebbf | 2012-10-15 21:57:41 +0000 | [diff] [blame] | 523 | assert(!MRI->isReserved(PhysReg) && "Reserved register assignment"); |
Jakob Stoklund Olesen | ba05c01 | 2011-02-18 22:03:18 +0000 | [diff] [blame] | 524 | |
| 525 | // Preserve semantics of sub-register operands. |
Matthias Braun | 944aaeb | 2015-06-16 18:22:28 +0000 | [diff] [blame] | 526 | unsigned SubReg = MO.getSubReg(); |
| 527 | if (SubReg != 0) { |
Krzysztof Parzyszek | d7a0236 | 2018-08-15 16:07:47 +0000 | [diff] [blame] | 528 | if (NoSubRegLiveness || !MRI->shouldTrackSubRegLiveness(VirtReg)) { |
Matthias Braun | 944aaeb | 2015-06-16 18:22:28 +0000 | [diff] [blame] | 529 | // A virtual register kill refers to the whole register, so we may |
Francis Visoiu Mistrih | fd11bc0 | 2017-12-07 10:40:31 +0000 | [diff] [blame] | 530 | // have to add implicit killed operands for the super-register. A |
Matthias Braun | 944aaeb | 2015-06-16 18:22:28 +0000 | [diff] [blame] | 531 | // partial redef always kills and redefines the super-register. |
Quentin Colombet | 156a103 | 2017-08-16 00:17:05 +0000 | [diff] [blame] | 532 | if ((MO.readsReg() && (MO.isDef() || MO.isKill())) || |
| 533 | (MO.isDef() && subRegLiveThrough(*MI, PhysReg))) |
Matthias Braun | 944aaeb | 2015-06-16 18:22:28 +0000 | [diff] [blame] | 534 | SuperKills.push_back(PhysReg); |
Jakob Stoklund Olesen | 200a8ce | 2011-10-05 00:01:48 +0000 | [diff] [blame] | 535 | |
Matthias Braun | 944aaeb | 2015-06-16 18:22:28 +0000 | [diff] [blame] | 536 | if (MO.isDef()) { |
| 537 | // Also add implicit defs for the super-register. |
Matthias Braun | 6681324 | 2014-12-10 01:13:04 +0000 | [diff] [blame] | 538 | if (MO.isDead()) |
| 539 | SuperDeads.push_back(PhysReg); |
| 540 | else |
| 541 | SuperDefs.push_back(PhysReg); |
| 542 | } |
Matthias Braun | 944aaeb | 2015-06-16 18:22:28 +0000 | [diff] [blame] | 543 | } else { |
| 544 | if (MO.isUse()) { |
| 545 | if (readsUndefSubreg(MO)) |
| 546 | // We need to add an <undef> flag if the subregister is |
| 547 | // completely undefined (and we are not adding super-register |
| 548 | // defs). |
| 549 | MO.setIsUndef(true); |
| 550 | } else if (!MO.isDead()) { |
| 551 | assert(MO.isDef()); |
Matthias Braun | 944aaeb | 2015-06-16 18:22:28 +0000 | [diff] [blame] | 552 | } |
Jakob Stoklund Olesen | 200a8ce | 2011-10-05 00:01:48 +0000 | [diff] [blame] | 553 | } |
Jakob Stoklund Olesen | ba05c01 | 2011-02-18 22:03:18 +0000 | [diff] [blame] | 554 | |
Francis Visoiu Mistrih | fd11bc0 | 2017-12-07 10:40:31 +0000 | [diff] [blame] | 555 | // The def undef and def internal flags only make sense for |
Matthias Braun | 852989f | 2017-03-17 00:41:33 +0000 | [diff] [blame] | 556 | // sub-register defs, and we are substituting a full physreg. An |
Francis Visoiu Mistrih | fd11bc0 | 2017-12-07 10:40:31 +0000 | [diff] [blame] | 557 | // implicit killed operand from the SuperKills list will represent the |
Matthias Braun | 852989f | 2017-03-17 00:41:33 +0000 | [diff] [blame] | 558 | // partial read of the super-register. |
| 559 | if (MO.isDef()) { |
Matthias Braun | 944aaeb | 2015-06-16 18:22:28 +0000 | [diff] [blame] | 560 | MO.setIsUndef(false); |
Matthias Braun | 852989f | 2017-03-17 00:41:33 +0000 | [diff] [blame] | 561 | MO.setIsInternalRead(false); |
| 562 | } |
Matthias Braun | 944aaeb | 2015-06-16 18:22:28 +0000 | [diff] [blame] | 563 | |
Jakob Stoklund Olesen | ba05c01 | 2011-02-18 22:03:18 +0000 | [diff] [blame] | 564 | // PhysReg operands cannot have subregister indexes. |
Matthias Braun | 944aaeb | 2015-06-16 18:22:28 +0000 | [diff] [blame] | 565 | PhysReg = TRI->getSubReg(PhysReg, SubReg); |
Jakob Stoklund Olesen | ba05c01 | 2011-02-18 22:03:18 +0000 | [diff] [blame] | 566 | assert(PhysReg && "Invalid SubReg for physical register"); |
| 567 | MO.setSubReg(0); |
| 568 | } |
| 569 | // Rewrite. Note we could have used MachineOperand::substPhysReg(), but |
| 570 | // we need the inlining here. |
| 571 | MO.setReg(PhysReg); |
Geoff Berry | 13357c9 | 2018-02-23 18:25:08 +0000 | [diff] [blame] | 572 | MO.setIsRenamable(true); |
Jakob Stoklund Olesen | ba05c01 | 2011-02-18 22:03:18 +0000 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | // Add any missing super-register kills after rewriting the whole |
| 576 | // instruction. |
| 577 | while (!SuperKills.empty()) |
| 578 | MI->addRegisterKilled(SuperKills.pop_back_val(), TRI, true); |
| 579 | |
Jakob Stoklund Olesen | 93e110b | 2011-04-27 17:42:31 +0000 | [diff] [blame] | 580 | while (!SuperDeads.empty()) |
| 581 | MI->addRegisterDead(SuperDeads.pop_back_val(), TRI, true); |
| 582 | |
| 583 | while (!SuperDefs.empty()) |
| 584 | MI->addRegisterDefined(SuperDefs.pop_back_val(), TRI); |
| 585 | |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 586 | LLVM_DEBUG(dbgs() << "> " << *MI); |
Jakob Stoklund Olesen | ba05c01 | 2011-02-18 22:03:18 +0000 | [diff] [blame] | 587 | |
Matthias Braun | 94ebfcb | 2017-03-17 00:41:39 +0000 | [diff] [blame] | 588 | expandCopyBundle(*MI); |
| 589 | |
Matthias Braun | 79519fe | 2016-07-09 00:19:07 +0000 | [diff] [blame] | 590 | // We can remove identity copies right now. |
| 591 | handleIdentityCopy(*MI); |
Jakob Stoklund Olesen | ba05c01 | 2011-02-18 22:03:18 +0000 | [diff] [blame] | 592 | } |
| 593 | } |
Jakob Stoklund Olesen | ba05c01 | 2011-02-18 22:03:18 +0000 | [diff] [blame] | 594 | } |