10b57cec5SDimitry Andric //===- MachineCopyPropagation.cpp - Machine Copy Propagation Pass ---------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This is an extremely simple MachineInstr-level copy propagation pass. 100b57cec5SDimitry Andric // 110b57cec5SDimitry Andric // This pass forwards the source of COPYs to the users of their destinations 120b57cec5SDimitry Andric // when doing so is legal. For example: 130b57cec5SDimitry Andric // 140b57cec5SDimitry Andric // %reg1 = COPY %reg0 150b57cec5SDimitry Andric // ... 160b57cec5SDimitry Andric // ... = OP %reg1 170b57cec5SDimitry Andric // 180b57cec5SDimitry Andric // If 190b57cec5SDimitry Andric // - %reg0 has not been clobbered by the time of the use of %reg1 200b57cec5SDimitry Andric // - the register class constraints are satisfied 210b57cec5SDimitry Andric // - the COPY def is the only value that reaches OP 220b57cec5SDimitry Andric // then this pass replaces the above with: 230b57cec5SDimitry Andric // 240b57cec5SDimitry Andric // %reg1 = COPY %reg0 250b57cec5SDimitry Andric // ... 260b57cec5SDimitry Andric // ... = OP %reg0 270b57cec5SDimitry Andric // 280b57cec5SDimitry Andric // This pass also removes some redundant COPYs. For example: 290b57cec5SDimitry Andric // 300b57cec5SDimitry Andric // %R1 = COPY %R0 310b57cec5SDimitry Andric // ... // No clobber of %R1 320b57cec5SDimitry Andric // %R0 = COPY %R1 <<< Removed 330b57cec5SDimitry Andric // 340b57cec5SDimitry Andric // or 350b57cec5SDimitry Andric // 360b57cec5SDimitry Andric // %R1 = COPY %R0 370b57cec5SDimitry Andric // ... // No clobber of %R0 380b57cec5SDimitry Andric // %R1 = COPY %R0 <<< Removed 390b57cec5SDimitry Andric // 40480093f4SDimitry Andric // or 41480093f4SDimitry Andric // 42480093f4SDimitry Andric // $R0 = OP ... 43480093f4SDimitry Andric // ... // No read/clobber of $R0 and $R1 44480093f4SDimitry Andric // $R1 = COPY $R0 // $R0 is killed 45480093f4SDimitry Andric // Replace $R0 with $R1 and remove the COPY 46480093f4SDimitry Andric // $R1 = OP ... 47480093f4SDimitry Andric // ... 48480093f4SDimitry Andric // 490b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 500b57cec5SDimitry Andric 510b57cec5SDimitry Andric #include "llvm/ADT/DenseMap.h" 520b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h" 530b57cec5SDimitry Andric #include "llvm/ADT/SetVector.h" 545ffd83dbSDimitry Andric #include "llvm/ADT/SmallSet.h" 550b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h" 560b57cec5SDimitry Andric #include "llvm/ADT/Statistic.h" 570b57cec5SDimitry Andric #include "llvm/ADT/iterator_range.h" 580b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h" 590b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h" 600b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h" 610b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstr.h" 620b57cec5SDimitry Andric #include "llvm/CodeGen/MachineOperand.h" 630b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h" 640b57cec5SDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h" 650b57cec5SDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h" 660b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h" 67480093f4SDimitry Andric #include "llvm/InitializePasses.h" 680b57cec5SDimitry Andric #include "llvm/MC/MCRegisterInfo.h" 690b57cec5SDimitry Andric #include "llvm/Pass.h" 700b57cec5SDimitry Andric #include "llvm/Support/Debug.h" 710b57cec5SDimitry Andric #include "llvm/Support/DebugCounter.h" 720b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 730b57cec5SDimitry Andric #include <cassert> 740b57cec5SDimitry Andric #include <iterator> 750b57cec5SDimitry Andric 760b57cec5SDimitry Andric using namespace llvm; 770b57cec5SDimitry Andric 780b57cec5SDimitry Andric #define DEBUG_TYPE "machine-cp" 790b57cec5SDimitry Andric 800b57cec5SDimitry Andric STATISTIC(NumDeletes, "Number of dead copies deleted"); 810b57cec5SDimitry Andric STATISTIC(NumCopyForwards, "Number of copy uses forwarded"); 82480093f4SDimitry Andric STATISTIC(NumCopyBackwardPropagated, "Number of copy defs backward propagated"); 83*06c3fb27SDimitry Andric STATISTIC(SpillageChainsLength, "Length of spillage chains"); 84*06c3fb27SDimitry Andric STATISTIC(NumSpillageChains, "Number of spillage chains"); 850b57cec5SDimitry Andric DEBUG_COUNTER(FwdCounter, "machine-cp-fwd", 860b57cec5SDimitry Andric "Controls which register COPYs are forwarded"); 870b57cec5SDimitry Andric 8881ad6265SDimitry Andric static cl::opt<bool> MCPUseCopyInstr("mcp-use-is-copy-instr", cl::init(false), 8981ad6265SDimitry Andric cl::Hidden); 90*06c3fb27SDimitry Andric static cl::opt<cl::boolOrDefault> 91*06c3fb27SDimitry Andric EnableSpillageCopyElimination("enable-spill-copy-elim", cl::Hidden); 9281ad6265SDimitry Andric 930b57cec5SDimitry Andric namespace { 940b57cec5SDimitry Andric 95bdd1243dSDimitry Andric static std::optional<DestSourcePair> isCopyInstr(const MachineInstr &MI, 9681ad6265SDimitry Andric const TargetInstrInfo &TII, 9781ad6265SDimitry Andric bool UseCopyInstr) { 9881ad6265SDimitry Andric if (UseCopyInstr) 9981ad6265SDimitry Andric return TII.isCopyInstr(MI); 10081ad6265SDimitry Andric 10181ad6265SDimitry Andric if (MI.isCopy()) 102bdd1243dSDimitry Andric return std::optional<DestSourcePair>( 10381ad6265SDimitry Andric DestSourcePair{MI.getOperand(0), MI.getOperand(1)}); 10481ad6265SDimitry Andric 105bdd1243dSDimitry Andric return std::nullopt; 10681ad6265SDimitry Andric } 10781ad6265SDimitry Andric 1080b57cec5SDimitry Andric class CopyTracker { 1090b57cec5SDimitry Andric struct CopyInfo { 110*06c3fb27SDimitry Andric MachineInstr *MI, *LastSeenUseInCopy; 111e8d8bef9SDimitry Andric SmallVector<MCRegister, 4> DefRegs; 1120b57cec5SDimitry Andric bool Avail; 1130b57cec5SDimitry Andric }; 1140b57cec5SDimitry Andric 115e8d8bef9SDimitry Andric DenseMap<MCRegister, CopyInfo> Copies; 1160b57cec5SDimitry Andric 1170b57cec5SDimitry Andric public: 1180b57cec5SDimitry Andric /// Mark all of the given registers and their subregisters as unavailable for 1190b57cec5SDimitry Andric /// copying. 120e8d8bef9SDimitry Andric void markRegsUnavailable(ArrayRef<MCRegister> Regs, 1210b57cec5SDimitry Andric const TargetRegisterInfo &TRI) { 122e8d8bef9SDimitry Andric for (MCRegister Reg : Regs) { 1230b57cec5SDimitry Andric // Source of copy is no longer available for propagation. 124*06c3fb27SDimitry Andric for (MCRegUnit Unit : TRI.regunits(Reg)) { 125*06c3fb27SDimitry Andric auto CI = Copies.find(Unit); 1260b57cec5SDimitry Andric if (CI != Copies.end()) 1270b57cec5SDimitry Andric CI->second.Avail = false; 1280b57cec5SDimitry Andric } 1290b57cec5SDimitry Andric } 1300b57cec5SDimitry Andric } 1310b57cec5SDimitry Andric 132480093f4SDimitry Andric /// Remove register from copy maps. 13381ad6265SDimitry Andric void invalidateRegister(MCRegister Reg, const TargetRegisterInfo &TRI, 13481ad6265SDimitry Andric const TargetInstrInfo &TII, bool UseCopyInstr) { 135480093f4SDimitry Andric // Since Reg might be a subreg of some registers, only invalidate Reg is not 136480093f4SDimitry Andric // enough. We have to find the COPY defines Reg or registers defined by Reg 137480093f4SDimitry Andric // and invalidate all of them. 138e8d8bef9SDimitry Andric SmallSet<MCRegister, 8> RegsToInvalidate; 1395ffd83dbSDimitry Andric RegsToInvalidate.insert(Reg); 140*06c3fb27SDimitry Andric for (MCRegUnit Unit : TRI.regunits(Reg)) { 141*06c3fb27SDimitry Andric auto I = Copies.find(Unit); 142480093f4SDimitry Andric if (I != Copies.end()) { 143480093f4SDimitry Andric if (MachineInstr *MI = I->second.MI) { 144bdd1243dSDimitry Andric std::optional<DestSourcePair> CopyOperands = 14581ad6265SDimitry Andric isCopyInstr(*MI, TII, UseCopyInstr); 14681ad6265SDimitry Andric assert(CopyOperands && "Expect copy"); 14781ad6265SDimitry Andric 14881ad6265SDimitry Andric RegsToInvalidate.insert( 14981ad6265SDimitry Andric CopyOperands->Destination->getReg().asMCReg()); 15081ad6265SDimitry Andric RegsToInvalidate.insert(CopyOperands->Source->getReg().asMCReg()); 151480093f4SDimitry Andric } 152480093f4SDimitry Andric RegsToInvalidate.insert(I->second.DefRegs.begin(), 153480093f4SDimitry Andric I->second.DefRegs.end()); 154480093f4SDimitry Andric } 155480093f4SDimitry Andric } 156e8d8bef9SDimitry Andric for (MCRegister InvalidReg : RegsToInvalidate) 157*06c3fb27SDimitry Andric for (MCRegUnit Unit : TRI.regunits(InvalidReg)) 158*06c3fb27SDimitry Andric Copies.erase(Unit); 159480093f4SDimitry Andric } 160480093f4SDimitry Andric 1610b57cec5SDimitry Andric /// Clobber a single register, removing it from the tracker's copy maps. 16281ad6265SDimitry Andric void clobberRegister(MCRegister Reg, const TargetRegisterInfo &TRI, 16381ad6265SDimitry Andric const TargetInstrInfo &TII, bool UseCopyInstr) { 164*06c3fb27SDimitry Andric for (MCRegUnit Unit : TRI.regunits(Reg)) { 165*06c3fb27SDimitry Andric auto I = Copies.find(Unit); 1660b57cec5SDimitry Andric if (I != Copies.end()) { 1670b57cec5SDimitry Andric // When we clobber the source of a copy, we need to clobber everything 1680b57cec5SDimitry Andric // it defined. 1690b57cec5SDimitry Andric markRegsUnavailable(I->second.DefRegs, TRI); 1700b57cec5SDimitry Andric // When we clobber the destination of a copy, we need to clobber the 1710b57cec5SDimitry Andric // whole register it defined. 17281ad6265SDimitry Andric if (MachineInstr *MI = I->second.MI) { 173bdd1243dSDimitry Andric std::optional<DestSourcePair> CopyOperands = 17481ad6265SDimitry Andric isCopyInstr(*MI, TII, UseCopyInstr); 17581ad6265SDimitry Andric markRegsUnavailable({CopyOperands->Destination->getReg().asMCReg()}, 17681ad6265SDimitry Andric TRI); 17781ad6265SDimitry Andric } 1780b57cec5SDimitry Andric // Now we can erase the copy. 1790b57cec5SDimitry Andric Copies.erase(I); 1800b57cec5SDimitry Andric } 1810b57cec5SDimitry Andric } 1820b57cec5SDimitry Andric } 1830b57cec5SDimitry Andric 1840b57cec5SDimitry Andric /// Add this copy's registers into the tracker's copy maps. 18581ad6265SDimitry Andric void trackCopy(MachineInstr *MI, const TargetRegisterInfo &TRI, 18681ad6265SDimitry Andric const TargetInstrInfo &TII, bool UseCopyInstr) { 187bdd1243dSDimitry Andric std::optional<DestSourcePair> CopyOperands = 188bdd1243dSDimitry Andric isCopyInstr(*MI, TII, UseCopyInstr); 18981ad6265SDimitry Andric assert(CopyOperands && "Tracking non-copy?"); 1900b57cec5SDimitry Andric 19181ad6265SDimitry Andric MCRegister Src = CopyOperands->Source->getReg().asMCReg(); 19281ad6265SDimitry Andric MCRegister Def = CopyOperands->Destination->getReg().asMCReg(); 1930b57cec5SDimitry Andric 1940b57cec5SDimitry Andric // Remember Def is defined by the copy. 195*06c3fb27SDimitry Andric for (MCRegUnit Unit : TRI.regunits(Def)) 196*06c3fb27SDimitry Andric Copies[Unit] = {MI, nullptr, {}, true}; 1970b57cec5SDimitry Andric 1980b57cec5SDimitry Andric // Remember source that's copied to Def. Once it's clobbered, then 1990b57cec5SDimitry Andric // it's no longer available for copy propagation. 200*06c3fb27SDimitry Andric for (MCRegUnit Unit : TRI.regunits(Src)) { 201*06c3fb27SDimitry Andric auto I = Copies.insert({Unit, {nullptr, nullptr, {}, false}}); 2020b57cec5SDimitry Andric auto &Copy = I.first->second; 2030b57cec5SDimitry Andric if (!is_contained(Copy.DefRegs, Def)) 2040b57cec5SDimitry Andric Copy.DefRegs.push_back(Def); 205*06c3fb27SDimitry Andric Copy.LastSeenUseInCopy = MI; 2060b57cec5SDimitry Andric } 2070b57cec5SDimitry Andric } 2080b57cec5SDimitry Andric 2090b57cec5SDimitry Andric bool hasAnyCopies() { 2100b57cec5SDimitry Andric return !Copies.empty(); 2110b57cec5SDimitry Andric } 2120b57cec5SDimitry Andric 213e8d8bef9SDimitry Andric MachineInstr *findCopyForUnit(MCRegister RegUnit, 214e8d8bef9SDimitry Andric const TargetRegisterInfo &TRI, 2150b57cec5SDimitry Andric bool MustBeAvailable = false) { 2160b57cec5SDimitry Andric auto CI = Copies.find(RegUnit); 2170b57cec5SDimitry Andric if (CI == Copies.end()) 2180b57cec5SDimitry Andric return nullptr; 2190b57cec5SDimitry Andric if (MustBeAvailable && !CI->second.Avail) 2200b57cec5SDimitry Andric return nullptr; 2210b57cec5SDimitry Andric return CI->second.MI; 2220b57cec5SDimitry Andric } 2230b57cec5SDimitry Andric 224e8d8bef9SDimitry Andric MachineInstr *findCopyDefViaUnit(MCRegister RegUnit, 225480093f4SDimitry Andric const TargetRegisterInfo &TRI) { 226480093f4SDimitry Andric auto CI = Copies.find(RegUnit); 227480093f4SDimitry Andric if (CI == Copies.end()) 228480093f4SDimitry Andric return nullptr; 229480093f4SDimitry Andric if (CI->second.DefRegs.size() != 1) 230480093f4SDimitry Andric return nullptr; 231*06c3fb27SDimitry Andric MCRegUnit RU = *TRI.regunits(CI->second.DefRegs[0]).begin(); 232*06c3fb27SDimitry Andric return findCopyForUnit(RU, TRI, true); 233480093f4SDimitry Andric } 234480093f4SDimitry Andric 235e8d8bef9SDimitry Andric MachineInstr *findAvailBackwardCopy(MachineInstr &I, MCRegister Reg, 23681ad6265SDimitry Andric const TargetRegisterInfo &TRI, 23781ad6265SDimitry Andric const TargetInstrInfo &TII, 23881ad6265SDimitry Andric bool UseCopyInstr) { 239*06c3fb27SDimitry Andric MCRegUnit RU = *TRI.regunits(Reg).begin(); 240*06c3fb27SDimitry Andric MachineInstr *AvailCopy = findCopyDefViaUnit(RU, TRI); 24181ad6265SDimitry Andric 24281ad6265SDimitry Andric if (!AvailCopy) 243480093f4SDimitry Andric return nullptr; 244480093f4SDimitry Andric 245bdd1243dSDimitry Andric std::optional<DestSourcePair> CopyOperands = 24681ad6265SDimitry Andric isCopyInstr(*AvailCopy, TII, UseCopyInstr); 24781ad6265SDimitry Andric Register AvailSrc = CopyOperands->Source->getReg(); 24881ad6265SDimitry Andric Register AvailDef = CopyOperands->Destination->getReg(); 24981ad6265SDimitry Andric if (!TRI.isSubRegisterEq(AvailSrc, Reg)) 25081ad6265SDimitry Andric return nullptr; 25181ad6265SDimitry Andric 252480093f4SDimitry Andric for (const MachineInstr &MI : 253480093f4SDimitry Andric make_range(AvailCopy->getReverseIterator(), I.getReverseIterator())) 254480093f4SDimitry Andric for (const MachineOperand &MO : MI.operands()) 255480093f4SDimitry Andric if (MO.isRegMask()) 256480093f4SDimitry Andric // FIXME: Shall we simultaneously invalidate AvailSrc or AvailDef? 257480093f4SDimitry Andric if (MO.clobbersPhysReg(AvailSrc) || MO.clobbersPhysReg(AvailDef)) 258480093f4SDimitry Andric return nullptr; 259480093f4SDimitry Andric 260480093f4SDimitry Andric return AvailCopy; 261480093f4SDimitry Andric } 262480093f4SDimitry Andric 263e8d8bef9SDimitry Andric MachineInstr *findAvailCopy(MachineInstr &DestCopy, MCRegister Reg, 26481ad6265SDimitry Andric const TargetRegisterInfo &TRI, 26581ad6265SDimitry Andric const TargetInstrInfo &TII, bool UseCopyInstr) { 2660b57cec5SDimitry Andric // We check the first RegUnit here, since we'll only be interested in the 2670b57cec5SDimitry Andric // copy if it copies the entire register anyway. 268*06c3fb27SDimitry Andric MCRegUnit RU = *TRI.regunits(Reg).begin(); 2690b57cec5SDimitry Andric MachineInstr *AvailCopy = 270*06c3fb27SDimitry Andric findCopyForUnit(RU, TRI, /*MustBeAvailable=*/true); 27181ad6265SDimitry Andric 27281ad6265SDimitry Andric if (!AvailCopy) 27381ad6265SDimitry Andric return nullptr; 27481ad6265SDimitry Andric 275bdd1243dSDimitry Andric std::optional<DestSourcePair> CopyOperands = 27681ad6265SDimitry Andric isCopyInstr(*AvailCopy, TII, UseCopyInstr); 27781ad6265SDimitry Andric Register AvailSrc = CopyOperands->Source->getReg(); 27881ad6265SDimitry Andric Register AvailDef = CopyOperands->Destination->getReg(); 27981ad6265SDimitry Andric if (!TRI.isSubRegisterEq(AvailDef, Reg)) 2800b57cec5SDimitry Andric return nullptr; 2810b57cec5SDimitry Andric 2820b57cec5SDimitry Andric // Check that the available copy isn't clobbered by any regmasks between 2830b57cec5SDimitry Andric // itself and the destination. 2840b57cec5SDimitry Andric for (const MachineInstr &MI : 2850b57cec5SDimitry Andric make_range(AvailCopy->getIterator(), DestCopy.getIterator())) 2860b57cec5SDimitry Andric for (const MachineOperand &MO : MI.operands()) 2870b57cec5SDimitry Andric if (MO.isRegMask()) 2880b57cec5SDimitry Andric if (MO.clobbersPhysReg(AvailSrc) || MO.clobbersPhysReg(AvailDef)) 2890b57cec5SDimitry Andric return nullptr; 2900b57cec5SDimitry Andric 2910b57cec5SDimitry Andric return AvailCopy; 2920b57cec5SDimitry Andric } 2930b57cec5SDimitry Andric 294*06c3fb27SDimitry Andric // Find last COPY that defines Reg before Current MachineInstr. 295*06c3fb27SDimitry Andric MachineInstr *findLastSeenDefInCopy(const MachineInstr &Current, 296*06c3fb27SDimitry Andric MCRegister Reg, 297*06c3fb27SDimitry Andric const TargetRegisterInfo &TRI, 298*06c3fb27SDimitry Andric const TargetInstrInfo &TII, 299*06c3fb27SDimitry Andric bool UseCopyInstr) { 300*06c3fb27SDimitry Andric MCRegUnit RU = *TRI.regunits(Reg).begin(); 301*06c3fb27SDimitry Andric auto CI = Copies.find(RU); 302*06c3fb27SDimitry Andric if (CI == Copies.end() || !CI->second.Avail) 303*06c3fb27SDimitry Andric return nullptr; 304*06c3fb27SDimitry Andric 305*06c3fb27SDimitry Andric MachineInstr *DefCopy = CI->second.MI; 306*06c3fb27SDimitry Andric std::optional<DestSourcePair> CopyOperands = 307*06c3fb27SDimitry Andric isCopyInstr(*DefCopy, TII, UseCopyInstr); 308*06c3fb27SDimitry Andric Register Def = CopyOperands->Destination->getReg(); 309*06c3fb27SDimitry Andric if (!TRI.isSubRegisterEq(Def, Reg)) 310*06c3fb27SDimitry Andric return nullptr; 311*06c3fb27SDimitry Andric 312*06c3fb27SDimitry Andric for (const MachineInstr &MI : 313*06c3fb27SDimitry Andric make_range(static_cast<const MachineInstr *>(DefCopy)->getIterator(), 314*06c3fb27SDimitry Andric Current.getIterator())) 315*06c3fb27SDimitry Andric for (const MachineOperand &MO : MI.operands()) 316*06c3fb27SDimitry Andric if (MO.isRegMask()) 317*06c3fb27SDimitry Andric if (MO.clobbersPhysReg(Def)) { 318*06c3fb27SDimitry Andric LLVM_DEBUG(dbgs() << "MCP: Removed tracking of " 319*06c3fb27SDimitry Andric << printReg(Def, &TRI) << "\n"); 320*06c3fb27SDimitry Andric return nullptr; 321*06c3fb27SDimitry Andric } 322*06c3fb27SDimitry Andric 323*06c3fb27SDimitry Andric return DefCopy; 324*06c3fb27SDimitry Andric } 325*06c3fb27SDimitry Andric 326*06c3fb27SDimitry Andric // Find last COPY that uses Reg. 327*06c3fb27SDimitry Andric MachineInstr *findLastSeenUseInCopy(MCRegister Reg, 328*06c3fb27SDimitry Andric const TargetRegisterInfo &TRI) { 329*06c3fb27SDimitry Andric MCRegUnit RU = *TRI.regunits(Reg).begin(); 330*06c3fb27SDimitry Andric auto CI = Copies.find(RU); 331*06c3fb27SDimitry Andric if (CI == Copies.end()) 332*06c3fb27SDimitry Andric return nullptr; 333*06c3fb27SDimitry Andric return CI->second.LastSeenUseInCopy; 334*06c3fb27SDimitry Andric } 335*06c3fb27SDimitry Andric 3360b57cec5SDimitry Andric void clear() { 3370b57cec5SDimitry Andric Copies.clear(); 3380b57cec5SDimitry Andric } 3390b57cec5SDimitry Andric }; 3400b57cec5SDimitry Andric 3410b57cec5SDimitry Andric class MachineCopyPropagation : public MachineFunctionPass { 342*06c3fb27SDimitry Andric const TargetRegisterInfo *TRI = nullptr; 343*06c3fb27SDimitry Andric const TargetInstrInfo *TII = nullptr; 344*06c3fb27SDimitry Andric const MachineRegisterInfo *MRI = nullptr; 3450b57cec5SDimitry Andric 34681ad6265SDimitry Andric // Return true if this is a copy instruction and false otherwise. 34781ad6265SDimitry Andric bool UseCopyInstr; 34881ad6265SDimitry Andric 3490b57cec5SDimitry Andric public: 3500b57cec5SDimitry Andric static char ID; // Pass identification, replacement for typeid 3510b57cec5SDimitry Andric 35281ad6265SDimitry Andric MachineCopyPropagation(bool CopyInstr = false) 35381ad6265SDimitry Andric : MachineFunctionPass(ID), UseCopyInstr(CopyInstr || MCPUseCopyInstr) { 3540b57cec5SDimitry Andric initializeMachineCopyPropagationPass(*PassRegistry::getPassRegistry()); 3550b57cec5SDimitry Andric } 3560b57cec5SDimitry Andric 3570b57cec5SDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override { 3580b57cec5SDimitry Andric AU.setPreservesCFG(); 3590b57cec5SDimitry Andric MachineFunctionPass::getAnalysisUsage(AU); 3600b57cec5SDimitry Andric } 3610b57cec5SDimitry Andric 3620b57cec5SDimitry Andric bool runOnMachineFunction(MachineFunction &MF) override; 3630b57cec5SDimitry Andric 3640b57cec5SDimitry Andric MachineFunctionProperties getRequiredProperties() const override { 3650b57cec5SDimitry Andric return MachineFunctionProperties().set( 3660b57cec5SDimitry Andric MachineFunctionProperties::Property::NoVRegs); 3670b57cec5SDimitry Andric } 3680b57cec5SDimitry Andric 3690b57cec5SDimitry Andric private: 3708bcb0991SDimitry Andric typedef enum { DebugUse = false, RegularUse = true } DebugType; 3718bcb0991SDimitry Andric 372e8d8bef9SDimitry Andric void ReadRegister(MCRegister Reg, MachineInstr &Reader, DebugType DT); 373480093f4SDimitry Andric void ForwardCopyPropagateBlock(MachineBasicBlock &MBB); 374480093f4SDimitry Andric void BackwardCopyPropagateBlock(MachineBasicBlock &MBB); 375*06c3fb27SDimitry Andric void EliminateSpillageCopies(MachineBasicBlock &MBB); 376e8d8bef9SDimitry Andric bool eraseIfRedundant(MachineInstr &Copy, MCRegister Src, MCRegister Def); 3770b57cec5SDimitry Andric void forwardUses(MachineInstr &MI); 378480093f4SDimitry Andric void propagateDefs(MachineInstr &MI); 3790b57cec5SDimitry Andric bool isForwardableRegClassCopy(const MachineInstr &Copy, 3800b57cec5SDimitry Andric const MachineInstr &UseI, unsigned UseIdx); 381480093f4SDimitry Andric bool isBackwardPropagatableRegClassCopy(const MachineInstr &Copy, 382480093f4SDimitry Andric const MachineInstr &UseI, 383480093f4SDimitry Andric unsigned UseIdx); 3840b57cec5SDimitry Andric bool hasImplicitOverlap(const MachineInstr &MI, const MachineOperand &Use); 385e8d8bef9SDimitry Andric bool hasOverlappingMultipleDef(const MachineInstr &MI, 386e8d8bef9SDimitry Andric const MachineOperand &MODef, Register Def); 3870b57cec5SDimitry Andric 3880b57cec5SDimitry Andric /// Candidates for deletion. 3890b57cec5SDimitry Andric SmallSetVector<MachineInstr *, 8> MaybeDeadCopies; 3900b57cec5SDimitry Andric 3918bcb0991SDimitry Andric /// Multimap tracking debug users in current BB 392fe6060f1SDimitry Andric DenseMap<MachineInstr *, SmallSet<MachineInstr *, 2>> CopyDbgUsers; 3938bcb0991SDimitry Andric 3940b57cec5SDimitry Andric CopyTracker Tracker; 3950b57cec5SDimitry Andric 396*06c3fb27SDimitry Andric bool Changed = false; 3970b57cec5SDimitry Andric }; 3980b57cec5SDimitry Andric 3990b57cec5SDimitry Andric } // end anonymous namespace 4000b57cec5SDimitry Andric 4010b57cec5SDimitry Andric char MachineCopyPropagation::ID = 0; 4020b57cec5SDimitry Andric 4030b57cec5SDimitry Andric char &llvm::MachineCopyPropagationID = MachineCopyPropagation::ID; 4040b57cec5SDimitry Andric 4050b57cec5SDimitry Andric INITIALIZE_PASS(MachineCopyPropagation, DEBUG_TYPE, 4060b57cec5SDimitry Andric "Machine Copy Propagation Pass", false, false) 4070b57cec5SDimitry Andric 408e8d8bef9SDimitry Andric void MachineCopyPropagation::ReadRegister(MCRegister Reg, MachineInstr &Reader, 4098bcb0991SDimitry Andric DebugType DT) { 4100b57cec5SDimitry Andric // If 'Reg' is defined by a copy, the copy is no longer a candidate 4118bcb0991SDimitry Andric // for elimination. If a copy is "read" by a debug user, record the user 4128bcb0991SDimitry Andric // for propagation. 413*06c3fb27SDimitry Andric for (MCRegUnit Unit : TRI->regunits(Reg)) { 414*06c3fb27SDimitry Andric if (MachineInstr *Copy = Tracker.findCopyForUnit(Unit, *TRI)) { 4158bcb0991SDimitry Andric if (DT == RegularUse) { 4160b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "MCP: Copy is used - not dead: "; Copy->dump()); 4170b57cec5SDimitry Andric MaybeDeadCopies.remove(Copy); 4188bcb0991SDimitry Andric } else { 419fe6060f1SDimitry Andric CopyDbgUsers[Copy].insert(&Reader); 4208bcb0991SDimitry Andric } 4210b57cec5SDimitry Andric } 4220b57cec5SDimitry Andric } 4230b57cec5SDimitry Andric } 4240b57cec5SDimitry Andric 4250b57cec5SDimitry Andric /// Return true if \p PreviousCopy did copy register \p Src to register \p Def. 4260b57cec5SDimitry Andric /// This fact may have been obscured by sub register usage or may not be true at 4270b57cec5SDimitry Andric /// all even though Src and Def are subregisters of the registers used in 4280b57cec5SDimitry Andric /// PreviousCopy. e.g. 4290b57cec5SDimitry Andric /// isNopCopy("ecx = COPY eax", AX, CX) == true 4300b57cec5SDimitry Andric /// isNopCopy("ecx = COPY eax", AH, CL) == false 431e8d8bef9SDimitry Andric static bool isNopCopy(const MachineInstr &PreviousCopy, MCRegister Src, 43281ad6265SDimitry Andric MCRegister Def, const TargetRegisterInfo *TRI, 43381ad6265SDimitry Andric const TargetInstrInfo *TII, bool UseCopyInstr) { 43481ad6265SDimitry Andric 435bdd1243dSDimitry Andric std::optional<DestSourcePair> CopyOperands = 43681ad6265SDimitry Andric isCopyInstr(PreviousCopy, *TII, UseCopyInstr); 43781ad6265SDimitry Andric MCRegister PreviousSrc = CopyOperands->Source->getReg().asMCReg(); 43881ad6265SDimitry Andric MCRegister PreviousDef = CopyOperands->Destination->getReg().asMCReg(); 43916d6b3b3SDimitry Andric if (Src == PreviousSrc && Def == PreviousDef) 4400b57cec5SDimitry Andric return true; 4410b57cec5SDimitry Andric if (!TRI->isSubRegister(PreviousSrc, Src)) 4420b57cec5SDimitry Andric return false; 4430b57cec5SDimitry Andric unsigned SubIdx = TRI->getSubRegIndex(PreviousSrc, Src); 4440b57cec5SDimitry Andric return SubIdx == TRI->getSubRegIndex(PreviousDef, Def); 4450b57cec5SDimitry Andric } 4460b57cec5SDimitry Andric 4470b57cec5SDimitry Andric /// Remove instruction \p Copy if there exists a previous copy that copies the 4480b57cec5SDimitry Andric /// register \p Src to the register \p Def; This may happen indirectly by 4490b57cec5SDimitry Andric /// copying the super registers. 450e8d8bef9SDimitry Andric bool MachineCopyPropagation::eraseIfRedundant(MachineInstr &Copy, 451e8d8bef9SDimitry Andric MCRegister Src, MCRegister Def) { 4520b57cec5SDimitry Andric // Avoid eliminating a copy from/to a reserved registers as we cannot predict 4530b57cec5SDimitry Andric // the value (Example: The sparc zero register is writable but stays zero). 4540b57cec5SDimitry Andric if (MRI->isReserved(Src) || MRI->isReserved(Def)) 4550b57cec5SDimitry Andric return false; 4560b57cec5SDimitry Andric 4570b57cec5SDimitry Andric // Search for an existing copy. 45881ad6265SDimitry Andric MachineInstr *PrevCopy = 45981ad6265SDimitry Andric Tracker.findAvailCopy(Copy, Def, *TRI, *TII, UseCopyInstr); 4600b57cec5SDimitry Andric if (!PrevCopy) 4610b57cec5SDimitry Andric return false; 4620b57cec5SDimitry Andric 46381ad6265SDimitry Andric auto PrevCopyOperands = isCopyInstr(*PrevCopy, *TII, UseCopyInstr); 4640b57cec5SDimitry Andric // Check that the existing copy uses the correct sub registers. 46581ad6265SDimitry Andric if (PrevCopyOperands->Destination->isDead()) 4660b57cec5SDimitry Andric return false; 46781ad6265SDimitry Andric if (!isNopCopy(*PrevCopy, Src, Def, TRI, TII, UseCopyInstr)) 4680b57cec5SDimitry Andric return false; 4690b57cec5SDimitry Andric 4700b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "MCP: copy is a NOP, removing: "; Copy.dump()); 4710b57cec5SDimitry Andric 4720b57cec5SDimitry Andric // Copy was redundantly redefining either Src or Def. Remove earlier kill 4730b57cec5SDimitry Andric // flags between Copy and PrevCopy because the value will be reused now. 474bdd1243dSDimitry Andric std::optional<DestSourcePair> CopyOperands = 475bdd1243dSDimitry Andric isCopyInstr(Copy, *TII, UseCopyInstr); 47681ad6265SDimitry Andric assert(CopyOperands); 47781ad6265SDimitry Andric 47881ad6265SDimitry Andric Register CopyDef = CopyOperands->Destination->getReg(); 4790b57cec5SDimitry Andric assert(CopyDef == Src || CopyDef == Def); 4800b57cec5SDimitry Andric for (MachineInstr &MI : 4810b57cec5SDimitry Andric make_range(PrevCopy->getIterator(), Copy.getIterator())) 4820b57cec5SDimitry Andric MI.clearRegisterKills(CopyDef, TRI); 4830b57cec5SDimitry Andric 484*06c3fb27SDimitry Andric // Clear undef flag from remaining copy if needed. 485*06c3fb27SDimitry Andric if (!CopyOperands->Source->isUndef()) { 486*06c3fb27SDimitry Andric PrevCopy->getOperand(PrevCopyOperands->Source->getOperandNo()) 487*06c3fb27SDimitry Andric .setIsUndef(false); 488*06c3fb27SDimitry Andric } 489*06c3fb27SDimitry Andric 4900b57cec5SDimitry Andric Copy.eraseFromParent(); 4910b57cec5SDimitry Andric Changed = true; 4920b57cec5SDimitry Andric ++NumDeletes; 4930b57cec5SDimitry Andric return true; 4940b57cec5SDimitry Andric } 4950b57cec5SDimitry Andric 496480093f4SDimitry Andric bool MachineCopyPropagation::isBackwardPropagatableRegClassCopy( 497480093f4SDimitry Andric const MachineInstr &Copy, const MachineInstr &UseI, unsigned UseIdx) { 498bdd1243dSDimitry Andric std::optional<DestSourcePair> CopyOperands = 499bdd1243dSDimitry Andric isCopyInstr(Copy, *TII, UseCopyInstr); 50081ad6265SDimitry Andric Register Def = CopyOperands->Destination->getReg(); 501480093f4SDimitry Andric 502480093f4SDimitry Andric if (const TargetRegisterClass *URC = 503480093f4SDimitry Andric UseI.getRegClassConstraint(UseIdx, TII, TRI)) 504480093f4SDimitry Andric return URC->contains(Def); 505480093f4SDimitry Andric 506480093f4SDimitry Andric // We don't process further if UseI is a COPY, since forward copy propagation 507480093f4SDimitry Andric // should handle that. 508480093f4SDimitry Andric return false; 509480093f4SDimitry Andric } 510480093f4SDimitry Andric 5110b57cec5SDimitry Andric /// Decide whether we should forward the source of \param Copy to its use in 5120b57cec5SDimitry Andric /// \param UseI based on the physical register class constraints of the opcode 5130b57cec5SDimitry Andric /// and avoiding introducing more cross-class COPYs. 5140b57cec5SDimitry Andric bool MachineCopyPropagation::isForwardableRegClassCopy(const MachineInstr &Copy, 5150b57cec5SDimitry Andric const MachineInstr &UseI, 5160b57cec5SDimitry Andric unsigned UseIdx) { 517bdd1243dSDimitry Andric std::optional<DestSourcePair> CopyOperands = 518bdd1243dSDimitry Andric isCopyInstr(Copy, *TII, UseCopyInstr); 51981ad6265SDimitry Andric Register CopySrcReg = CopyOperands->Source->getReg(); 5200b57cec5SDimitry Andric 5210b57cec5SDimitry Andric // If the new register meets the opcode register constraints, then allow 5220b57cec5SDimitry Andric // forwarding. 5230b57cec5SDimitry Andric if (const TargetRegisterClass *URC = 5240b57cec5SDimitry Andric UseI.getRegClassConstraint(UseIdx, TII, TRI)) 5250b57cec5SDimitry Andric return URC->contains(CopySrcReg); 5260b57cec5SDimitry Andric 52781ad6265SDimitry Andric auto UseICopyOperands = isCopyInstr(UseI, *TII, UseCopyInstr); 52881ad6265SDimitry Andric if (!UseICopyOperands) 5290b57cec5SDimitry Andric return false; 5300b57cec5SDimitry Andric 5310b57cec5SDimitry Andric /// COPYs don't have register class constraints, so if the user instruction 5320b57cec5SDimitry Andric /// is a COPY, we just try to avoid introducing additional cross-class 5330b57cec5SDimitry Andric /// COPYs. For example: 5340b57cec5SDimitry Andric /// 5350b57cec5SDimitry Andric /// RegClassA = COPY RegClassB // Copy parameter 5360b57cec5SDimitry Andric /// ... 5370b57cec5SDimitry Andric /// RegClassB = COPY RegClassA // UseI parameter 5380b57cec5SDimitry Andric /// 5390b57cec5SDimitry Andric /// which after forwarding becomes 5400b57cec5SDimitry Andric /// 5410b57cec5SDimitry Andric /// RegClassA = COPY RegClassB 5420b57cec5SDimitry Andric /// ... 5430b57cec5SDimitry Andric /// RegClassB = COPY RegClassB 5440b57cec5SDimitry Andric /// 5450b57cec5SDimitry Andric /// so we have reduced the number of cross-class COPYs and potentially 5460b57cec5SDimitry Andric /// introduced a nop COPY that can be removed. 5470b57cec5SDimitry Andric 54881ad6265SDimitry Andric // Allow forwarding if src and dst belong to any common class, so long as they 54981ad6265SDimitry Andric // don't belong to any (possibly smaller) common class that requires copies to 55081ad6265SDimitry Andric // go via a different class. 55181ad6265SDimitry Andric Register UseDstReg = UseICopyOperands->Destination->getReg(); 55281ad6265SDimitry Andric bool Found = false; 55381ad6265SDimitry Andric bool IsCrossClass = false; 55481ad6265SDimitry Andric for (const TargetRegisterClass *RC : TRI->regclasses()) { 55581ad6265SDimitry Andric if (RC->contains(CopySrcReg) && RC->contains(UseDstReg)) { 55681ad6265SDimitry Andric Found = true; 55781ad6265SDimitry Andric if (TRI->getCrossCopyRegClass(RC) != RC) { 55881ad6265SDimitry Andric IsCrossClass = true; 55981ad6265SDimitry Andric break; 56081ad6265SDimitry Andric } 56181ad6265SDimitry Andric } 56281ad6265SDimitry Andric } 56381ad6265SDimitry Andric if (!Found) 56481ad6265SDimitry Andric return false; 56581ad6265SDimitry Andric if (!IsCrossClass) 56681ad6265SDimitry Andric return true; 56781ad6265SDimitry Andric // The forwarded copy would be cross-class. Only do this if the original copy 56881ad6265SDimitry Andric // was also cross-class. 56981ad6265SDimitry Andric Register CopyDstReg = CopyOperands->Destination->getReg(); 57081ad6265SDimitry Andric for (const TargetRegisterClass *RC : TRI->regclasses()) { 57181ad6265SDimitry Andric if (RC->contains(CopySrcReg) && RC->contains(CopyDstReg) && 57281ad6265SDimitry Andric TRI->getCrossCopyRegClass(RC) != RC) 57381ad6265SDimitry Andric return true; 57481ad6265SDimitry Andric } 5750b57cec5SDimitry Andric return false; 5760b57cec5SDimitry Andric } 5770b57cec5SDimitry Andric 5780b57cec5SDimitry Andric /// Check that \p MI does not have implicit uses that overlap with it's \p Use 5790b57cec5SDimitry Andric /// operand (the register being replaced), since these can sometimes be 5800b57cec5SDimitry Andric /// implicitly tied to other operands. For example, on AMDGPU: 5810b57cec5SDimitry Andric /// 5820b57cec5SDimitry Andric /// V_MOVRELS_B32_e32 %VGPR2, %M0<imp-use>, %EXEC<imp-use>, %VGPR2_VGPR3_VGPR4_VGPR5<imp-use> 5830b57cec5SDimitry Andric /// 5840b57cec5SDimitry Andric /// the %VGPR2 is implicitly tied to the larger reg operand, but we have no 5850b57cec5SDimitry Andric /// way of knowing we need to update the latter when updating the former. 5860b57cec5SDimitry Andric bool MachineCopyPropagation::hasImplicitOverlap(const MachineInstr &MI, 5870b57cec5SDimitry Andric const MachineOperand &Use) { 5880b57cec5SDimitry Andric for (const MachineOperand &MIUse : MI.uses()) 5890b57cec5SDimitry Andric if (&MIUse != &Use && MIUse.isReg() && MIUse.isImplicit() && 5900b57cec5SDimitry Andric MIUse.isUse() && TRI->regsOverlap(Use.getReg(), MIUse.getReg())) 5910b57cec5SDimitry Andric return true; 5920b57cec5SDimitry Andric 5930b57cec5SDimitry Andric return false; 5940b57cec5SDimitry Andric } 5950b57cec5SDimitry Andric 596e8d8bef9SDimitry Andric /// For an MI that has multiple definitions, check whether \p MI has 597e8d8bef9SDimitry Andric /// a definition that overlaps with another of its definitions. 598e8d8bef9SDimitry Andric /// For example, on ARM: umull r9, r9, lr, r0 599e8d8bef9SDimitry Andric /// The umull instruction is unpredictable unless RdHi and RdLo are different. 600e8d8bef9SDimitry Andric bool MachineCopyPropagation::hasOverlappingMultipleDef( 601e8d8bef9SDimitry Andric const MachineInstr &MI, const MachineOperand &MODef, Register Def) { 602e8d8bef9SDimitry Andric for (const MachineOperand &MIDef : MI.defs()) { 603e8d8bef9SDimitry Andric if ((&MIDef != &MODef) && MIDef.isReg() && 604e8d8bef9SDimitry Andric TRI->regsOverlap(Def, MIDef.getReg())) 605e8d8bef9SDimitry Andric return true; 606e8d8bef9SDimitry Andric } 607e8d8bef9SDimitry Andric 608e8d8bef9SDimitry Andric return false; 609e8d8bef9SDimitry Andric } 610e8d8bef9SDimitry Andric 6110b57cec5SDimitry Andric /// Look for available copies whose destination register is used by \p MI and 6120b57cec5SDimitry Andric /// replace the use in \p MI with the copy's source register. 6130b57cec5SDimitry Andric void MachineCopyPropagation::forwardUses(MachineInstr &MI) { 6140b57cec5SDimitry Andric if (!Tracker.hasAnyCopies()) 6150b57cec5SDimitry Andric return; 6160b57cec5SDimitry Andric 6170b57cec5SDimitry Andric // Look for non-tied explicit vreg uses that have an active COPY 6180b57cec5SDimitry Andric // instruction that defines the physical register allocated to them. 6190b57cec5SDimitry Andric // Replace the vreg with the source of the active COPY. 6200b57cec5SDimitry Andric for (unsigned OpIdx = 0, OpEnd = MI.getNumOperands(); OpIdx < OpEnd; 6210b57cec5SDimitry Andric ++OpIdx) { 6220b57cec5SDimitry Andric MachineOperand &MOUse = MI.getOperand(OpIdx); 6230b57cec5SDimitry Andric // Don't forward into undef use operands since doing so can cause problems 6240b57cec5SDimitry Andric // with the machine verifier, since it doesn't treat undef reads as reads, 6250b57cec5SDimitry Andric // so we can end up with a live range that ends on an undef read, leading to 6260b57cec5SDimitry Andric // an error that the live range doesn't end on a read of the live range 6270b57cec5SDimitry Andric // register. 6280b57cec5SDimitry Andric if (!MOUse.isReg() || MOUse.isTied() || MOUse.isUndef() || MOUse.isDef() || 6290b57cec5SDimitry Andric MOUse.isImplicit()) 6300b57cec5SDimitry Andric continue; 6310b57cec5SDimitry Andric 6320b57cec5SDimitry Andric if (!MOUse.getReg()) 6330b57cec5SDimitry Andric continue; 6340b57cec5SDimitry Andric 6350b57cec5SDimitry Andric // Check that the register is marked 'renamable' so we know it is safe to 6360b57cec5SDimitry Andric // rename it without violating any constraints that aren't expressed in the 6370b57cec5SDimitry Andric // IR (e.g. ABI or opcode requirements). 6380b57cec5SDimitry Andric if (!MOUse.isRenamable()) 6390b57cec5SDimitry Andric continue; 6400b57cec5SDimitry Andric 64181ad6265SDimitry Andric MachineInstr *Copy = Tracker.findAvailCopy(MI, MOUse.getReg().asMCReg(), 64281ad6265SDimitry Andric *TRI, *TII, UseCopyInstr); 6430b57cec5SDimitry Andric if (!Copy) 6440b57cec5SDimitry Andric continue; 6450b57cec5SDimitry Andric 646bdd1243dSDimitry Andric std::optional<DestSourcePair> CopyOperands = 64781ad6265SDimitry Andric isCopyInstr(*Copy, *TII, UseCopyInstr); 64881ad6265SDimitry Andric Register CopyDstReg = CopyOperands->Destination->getReg(); 64981ad6265SDimitry Andric const MachineOperand &CopySrc = *CopyOperands->Source; 6508bcb0991SDimitry Andric Register CopySrcReg = CopySrc.getReg(); 6510b57cec5SDimitry Andric 652*06c3fb27SDimitry Andric Register ForwardedReg = CopySrcReg; 653*06c3fb27SDimitry Andric // MI might use a sub-register of the Copy destination, in which case the 654*06c3fb27SDimitry Andric // forwarded register is the matching sub-register of the Copy source. 6550b57cec5SDimitry Andric if (MOUse.getReg() != CopyDstReg) { 656*06c3fb27SDimitry Andric unsigned SubRegIdx = TRI->getSubRegIndex(CopyDstReg, MOUse.getReg()); 657*06c3fb27SDimitry Andric assert(SubRegIdx && 658*06c3fb27SDimitry Andric "MI source is not a sub-register of Copy destination"); 659*06c3fb27SDimitry Andric ForwardedReg = TRI->getSubReg(CopySrcReg, SubRegIdx); 660*06c3fb27SDimitry Andric if (!ForwardedReg) { 661*06c3fb27SDimitry Andric LLVM_DEBUG(dbgs() << "MCP: Copy source does not have sub-register " 662*06c3fb27SDimitry Andric << TRI->getSubRegIndexName(SubRegIdx) << '\n'); 6630b57cec5SDimitry Andric continue; 6640b57cec5SDimitry Andric } 665*06c3fb27SDimitry Andric } 6660b57cec5SDimitry Andric 6670b57cec5SDimitry Andric // Don't forward COPYs of reserved regs unless they are constant. 6680b57cec5SDimitry Andric if (MRI->isReserved(CopySrcReg) && !MRI->isConstantPhysReg(CopySrcReg)) 6690b57cec5SDimitry Andric continue; 6700b57cec5SDimitry Andric 6710b57cec5SDimitry Andric if (!isForwardableRegClassCopy(*Copy, MI, OpIdx)) 6720b57cec5SDimitry Andric continue; 6730b57cec5SDimitry Andric 6740b57cec5SDimitry Andric if (hasImplicitOverlap(MI, MOUse)) 6750b57cec5SDimitry Andric continue; 6760b57cec5SDimitry Andric 677480093f4SDimitry Andric // Check that the instruction is not a copy that partially overwrites the 678480093f4SDimitry Andric // original copy source that we are about to use. The tracker mechanism 679480093f4SDimitry Andric // cannot cope with that. 68081ad6265SDimitry Andric if (isCopyInstr(MI, *TII, UseCopyInstr) && 68181ad6265SDimitry Andric MI.modifiesRegister(CopySrcReg, TRI) && 682480093f4SDimitry Andric !MI.definesRegister(CopySrcReg)) { 683480093f4SDimitry Andric LLVM_DEBUG(dbgs() << "MCP: Copy source overlap with dest in " << MI); 684480093f4SDimitry Andric continue; 685480093f4SDimitry Andric } 686480093f4SDimitry Andric 6870b57cec5SDimitry Andric if (!DebugCounter::shouldExecute(FwdCounter)) { 6880b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "MCP: Skipping forwarding due to debug counter:\n " 6890b57cec5SDimitry Andric << MI); 6900b57cec5SDimitry Andric continue; 6910b57cec5SDimitry Andric } 6920b57cec5SDimitry Andric 6930b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "MCP: Replacing " << printReg(MOUse.getReg(), TRI) 694*06c3fb27SDimitry Andric << "\n with " << printReg(ForwardedReg, TRI) 6950b57cec5SDimitry Andric << "\n in " << MI << " from " << *Copy); 6960b57cec5SDimitry Andric 697*06c3fb27SDimitry Andric MOUse.setReg(ForwardedReg); 698*06c3fb27SDimitry Andric 6990b57cec5SDimitry Andric if (!CopySrc.isRenamable()) 7000b57cec5SDimitry Andric MOUse.setIsRenamable(false); 701349cc55cSDimitry Andric MOUse.setIsUndef(CopySrc.isUndef()); 7020b57cec5SDimitry Andric 7030b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "MCP: After replacement: " << MI << "\n"); 7040b57cec5SDimitry Andric 7050b57cec5SDimitry Andric // Clear kill markers that may have been invalidated. 7060b57cec5SDimitry Andric for (MachineInstr &KMI : 7070b57cec5SDimitry Andric make_range(Copy->getIterator(), std::next(MI.getIterator()))) 7080b57cec5SDimitry Andric KMI.clearRegisterKills(CopySrcReg, TRI); 7090b57cec5SDimitry Andric 7100b57cec5SDimitry Andric ++NumCopyForwards; 7110b57cec5SDimitry Andric Changed = true; 7120b57cec5SDimitry Andric } 7130b57cec5SDimitry Andric } 7140b57cec5SDimitry Andric 715480093f4SDimitry Andric void MachineCopyPropagation::ForwardCopyPropagateBlock(MachineBasicBlock &MBB) { 716480093f4SDimitry Andric LLVM_DEBUG(dbgs() << "MCP: ForwardCopyPropagateBlock " << MBB.getName() 717480093f4SDimitry Andric << "\n"); 7180b57cec5SDimitry Andric 719349cc55cSDimitry Andric for (MachineInstr &MI : llvm::make_early_inc_range(MBB)) { 7200b57cec5SDimitry Andric // Analyze copies (which don't overlap themselves). 721bdd1243dSDimitry Andric std::optional<DestSourcePair> CopyOperands = 722bdd1243dSDimitry Andric isCopyInstr(MI, *TII, UseCopyInstr); 72381ad6265SDimitry Andric if (CopyOperands) { 72481ad6265SDimitry Andric 72581ad6265SDimitry Andric Register RegSrc = CopyOperands->Source->getReg(); 72681ad6265SDimitry Andric Register RegDef = CopyOperands->Destination->getReg(); 72781ad6265SDimitry Andric 72881ad6265SDimitry Andric if (!TRI->regsOverlap(RegDef, RegSrc)) { 72981ad6265SDimitry Andric assert(RegDef.isPhysical() && RegSrc.isPhysical() && 7300b57cec5SDimitry Andric "MachineCopyPropagation should be run after register allocation!"); 7310b57cec5SDimitry Andric 73281ad6265SDimitry Andric MCRegister Def = RegDef.asMCReg(); 73381ad6265SDimitry Andric MCRegister Src = RegSrc.asMCReg(); 734e8d8bef9SDimitry Andric 7350b57cec5SDimitry Andric // The two copies cancel out and the source of the first copy 7360b57cec5SDimitry Andric // hasn't been overridden, eliminate the second one. e.g. 7370b57cec5SDimitry Andric // %ecx = COPY %eax 7380b57cec5SDimitry Andric // ... nothing clobbered eax. 7390b57cec5SDimitry Andric // %eax = COPY %ecx 7400b57cec5SDimitry Andric // => 7410b57cec5SDimitry Andric // %ecx = COPY %eax 7420b57cec5SDimitry Andric // 7430b57cec5SDimitry Andric // or 7440b57cec5SDimitry Andric // 7450b57cec5SDimitry Andric // %ecx = COPY %eax 7460b57cec5SDimitry Andric // ... nothing clobbered eax. 7470b57cec5SDimitry Andric // %ecx = COPY %eax 7480b57cec5SDimitry Andric // => 7490b57cec5SDimitry Andric // %ecx = COPY %eax 750349cc55cSDimitry Andric if (eraseIfRedundant(MI, Def, Src) || eraseIfRedundant(MI, Src, Def)) 7510b57cec5SDimitry Andric continue; 7520b57cec5SDimitry Andric 753349cc55cSDimitry Andric forwardUses(MI); 7540b57cec5SDimitry Andric 7550b57cec5SDimitry Andric // Src may have been changed by forwardUses() 75681ad6265SDimitry Andric CopyOperands = isCopyInstr(MI, *TII, UseCopyInstr); 75781ad6265SDimitry Andric Src = CopyOperands->Source->getReg().asMCReg(); 7580b57cec5SDimitry Andric 7590b57cec5SDimitry Andric // If Src is defined by a previous copy, the previous copy cannot be 7600b57cec5SDimitry Andric // eliminated. 761349cc55cSDimitry Andric ReadRegister(Src, MI, RegularUse); 762349cc55cSDimitry Andric for (const MachineOperand &MO : MI.implicit_operands()) { 7630b57cec5SDimitry Andric if (!MO.isReg() || !MO.readsReg()) 7640b57cec5SDimitry Andric continue; 765e8d8bef9SDimitry Andric MCRegister Reg = MO.getReg().asMCReg(); 7660b57cec5SDimitry Andric if (!Reg) 7670b57cec5SDimitry Andric continue; 768349cc55cSDimitry Andric ReadRegister(Reg, MI, RegularUse); 7690b57cec5SDimitry Andric } 7700b57cec5SDimitry Andric 771349cc55cSDimitry Andric LLVM_DEBUG(dbgs() << "MCP: Copy is a deletion candidate: "; MI.dump()); 7720b57cec5SDimitry Andric 7730b57cec5SDimitry Andric // Copy is now a candidate for deletion. 7740b57cec5SDimitry Andric if (!MRI->isReserved(Def)) 775349cc55cSDimitry Andric MaybeDeadCopies.insert(&MI); 7760b57cec5SDimitry Andric 7770b57cec5SDimitry Andric // If 'Def' is previously source of another copy, then this earlier copy's 7780b57cec5SDimitry Andric // source is no longer available. e.g. 7790b57cec5SDimitry Andric // %xmm9 = copy %xmm2 7800b57cec5SDimitry Andric // ... 7810b57cec5SDimitry Andric // %xmm2 = copy %xmm0 7820b57cec5SDimitry Andric // ... 7830b57cec5SDimitry Andric // %xmm2 = copy %xmm9 78481ad6265SDimitry Andric Tracker.clobberRegister(Def, *TRI, *TII, UseCopyInstr); 785349cc55cSDimitry Andric for (const MachineOperand &MO : MI.implicit_operands()) { 7860b57cec5SDimitry Andric if (!MO.isReg() || !MO.isDef()) 7870b57cec5SDimitry Andric continue; 788e8d8bef9SDimitry Andric MCRegister Reg = MO.getReg().asMCReg(); 7890b57cec5SDimitry Andric if (!Reg) 7900b57cec5SDimitry Andric continue; 79181ad6265SDimitry Andric Tracker.clobberRegister(Reg, *TRI, *TII, UseCopyInstr); 7920b57cec5SDimitry Andric } 7930b57cec5SDimitry Andric 79481ad6265SDimitry Andric Tracker.trackCopy(&MI, *TRI, *TII, UseCopyInstr); 7950b57cec5SDimitry Andric 7960b57cec5SDimitry Andric continue; 7970b57cec5SDimitry Andric } 79881ad6265SDimitry Andric } 7990b57cec5SDimitry Andric 8000b57cec5SDimitry Andric // Clobber any earlyclobber regs first. 801349cc55cSDimitry Andric for (const MachineOperand &MO : MI.operands()) 8020b57cec5SDimitry Andric if (MO.isReg() && MO.isEarlyClobber()) { 803e8d8bef9SDimitry Andric MCRegister Reg = MO.getReg().asMCReg(); 8040b57cec5SDimitry Andric // If we have a tied earlyclobber, that means it is also read by this 8050b57cec5SDimitry Andric // instruction, so we need to make sure we don't remove it as dead 8060b57cec5SDimitry Andric // later. 8070b57cec5SDimitry Andric if (MO.isTied()) 808349cc55cSDimitry Andric ReadRegister(Reg, MI, RegularUse); 80981ad6265SDimitry Andric Tracker.clobberRegister(Reg, *TRI, *TII, UseCopyInstr); 8100b57cec5SDimitry Andric } 8110b57cec5SDimitry Andric 812349cc55cSDimitry Andric forwardUses(MI); 8130b57cec5SDimitry Andric 8140b57cec5SDimitry Andric // Not a copy. 815e8d8bef9SDimitry Andric SmallVector<Register, 2> Defs; 8160b57cec5SDimitry Andric const MachineOperand *RegMask = nullptr; 817349cc55cSDimitry Andric for (const MachineOperand &MO : MI.operands()) { 8180b57cec5SDimitry Andric if (MO.isRegMask()) 8190b57cec5SDimitry Andric RegMask = &MO; 8200b57cec5SDimitry Andric if (!MO.isReg()) 8210b57cec5SDimitry Andric continue; 8228bcb0991SDimitry Andric Register Reg = MO.getReg(); 8230b57cec5SDimitry Andric if (!Reg) 8240b57cec5SDimitry Andric continue; 8250b57cec5SDimitry Andric 826e8d8bef9SDimitry Andric assert(!Reg.isVirtual() && 8270b57cec5SDimitry Andric "MachineCopyPropagation should be run after register allocation!"); 8280b57cec5SDimitry Andric 8290b57cec5SDimitry Andric if (MO.isDef() && !MO.isEarlyClobber()) { 830e8d8bef9SDimitry Andric Defs.push_back(Reg.asMCReg()); 8310b57cec5SDimitry Andric continue; 8328bcb0991SDimitry Andric } else if (MO.readsReg()) 833349cc55cSDimitry Andric ReadRegister(Reg.asMCReg(), MI, MO.isDebug() ? DebugUse : RegularUse); 8340b57cec5SDimitry Andric } 8350b57cec5SDimitry Andric 8360b57cec5SDimitry Andric // The instruction has a register mask operand which means that it clobbers 8370b57cec5SDimitry Andric // a large set of registers. Treat clobbered registers the same way as 8380b57cec5SDimitry Andric // defined registers. 8390b57cec5SDimitry Andric if (RegMask) { 8400b57cec5SDimitry Andric // Erase any MaybeDeadCopies whose destination register is clobbered. 8410b57cec5SDimitry Andric for (SmallSetVector<MachineInstr *, 8>::iterator DI = 8420b57cec5SDimitry Andric MaybeDeadCopies.begin(); 8430b57cec5SDimitry Andric DI != MaybeDeadCopies.end();) { 8440b57cec5SDimitry Andric MachineInstr *MaybeDead = *DI; 845bdd1243dSDimitry Andric std::optional<DestSourcePair> CopyOperands = 84681ad6265SDimitry Andric isCopyInstr(*MaybeDead, *TII, UseCopyInstr); 84781ad6265SDimitry Andric MCRegister Reg = CopyOperands->Destination->getReg().asMCReg(); 8480b57cec5SDimitry Andric assert(!MRI->isReserved(Reg)); 8490b57cec5SDimitry Andric 8500b57cec5SDimitry Andric if (!RegMask->clobbersPhysReg(Reg)) { 8510b57cec5SDimitry Andric ++DI; 8520b57cec5SDimitry Andric continue; 8530b57cec5SDimitry Andric } 8540b57cec5SDimitry Andric 8550b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "MCP: Removing copy due to regmask clobbering: "; 8560b57cec5SDimitry Andric MaybeDead->dump()); 8570b57cec5SDimitry Andric 8580b57cec5SDimitry Andric // Make sure we invalidate any entries in the copy maps before erasing 8590b57cec5SDimitry Andric // the instruction. 86081ad6265SDimitry Andric Tracker.clobberRegister(Reg, *TRI, *TII, UseCopyInstr); 8610b57cec5SDimitry Andric 8620b57cec5SDimitry Andric // erase() will return the next valid iterator pointing to the next 8630b57cec5SDimitry Andric // element after the erased one. 8640b57cec5SDimitry Andric DI = MaybeDeadCopies.erase(DI); 8650b57cec5SDimitry Andric MaybeDead->eraseFromParent(); 8660b57cec5SDimitry Andric Changed = true; 8670b57cec5SDimitry Andric ++NumDeletes; 8680b57cec5SDimitry Andric } 8690b57cec5SDimitry Andric } 8700b57cec5SDimitry Andric 8710b57cec5SDimitry Andric // Any previous copy definition or reading the Defs is no longer available. 872e8d8bef9SDimitry Andric for (MCRegister Reg : Defs) 87381ad6265SDimitry Andric Tracker.clobberRegister(Reg, *TRI, *TII, UseCopyInstr); 8740b57cec5SDimitry Andric } 8750b57cec5SDimitry Andric 8760b57cec5SDimitry Andric // If MBB doesn't have successors, delete the copies whose defs are not used. 8770b57cec5SDimitry Andric // If MBB does have successors, then conservative assume the defs are live-out 8780b57cec5SDimitry Andric // since we don't want to trust live-in lists. 8790b57cec5SDimitry Andric if (MBB.succ_empty()) { 8800b57cec5SDimitry Andric for (MachineInstr *MaybeDead : MaybeDeadCopies) { 8810b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "MCP: Removing copy due to no live-out succ: "; 8820b57cec5SDimitry Andric MaybeDead->dump()); 88381ad6265SDimitry Andric 884bdd1243dSDimitry Andric std::optional<DestSourcePair> CopyOperands = 88581ad6265SDimitry Andric isCopyInstr(*MaybeDead, *TII, UseCopyInstr); 88681ad6265SDimitry Andric assert(CopyOperands); 88781ad6265SDimitry Andric 88881ad6265SDimitry Andric Register SrcReg = CopyOperands->Source->getReg(); 88981ad6265SDimitry Andric Register DestReg = CopyOperands->Destination->getReg(); 89081ad6265SDimitry Andric assert(!MRI->isReserved(DestReg)); 8910b57cec5SDimitry Andric 8928bcb0991SDimitry Andric // Update matching debug values, if any. 893fe6060f1SDimitry Andric SmallVector<MachineInstr *> MaybeDeadDbgUsers( 894fe6060f1SDimitry Andric CopyDbgUsers[MaybeDead].begin(), CopyDbgUsers[MaybeDead].end()); 895fe6060f1SDimitry Andric MRI->updateDbgUsersToReg(DestReg.asMCReg(), SrcReg.asMCReg(), 896fe6060f1SDimitry Andric MaybeDeadDbgUsers); 8970b57cec5SDimitry Andric 8980b57cec5SDimitry Andric MaybeDead->eraseFromParent(); 8990b57cec5SDimitry Andric Changed = true; 9000b57cec5SDimitry Andric ++NumDeletes; 9010b57cec5SDimitry Andric } 9020b57cec5SDimitry Andric } 9030b57cec5SDimitry Andric 9040b57cec5SDimitry Andric MaybeDeadCopies.clear(); 9058bcb0991SDimitry Andric CopyDbgUsers.clear(); 9060b57cec5SDimitry Andric Tracker.clear(); 9070b57cec5SDimitry Andric } 9080b57cec5SDimitry Andric 909*06c3fb27SDimitry Andric static bool isBackwardPropagatableCopy(const DestSourcePair &CopyOperands, 91081ad6265SDimitry Andric const MachineRegisterInfo &MRI, 911*06c3fb27SDimitry Andric const TargetInstrInfo &TII) { 912*06c3fb27SDimitry Andric Register Def = CopyOperands.Destination->getReg(); 913*06c3fb27SDimitry Andric Register Src = CopyOperands.Source->getReg(); 914480093f4SDimitry Andric 915480093f4SDimitry Andric if (!Def || !Src) 916480093f4SDimitry Andric return false; 917480093f4SDimitry Andric 918480093f4SDimitry Andric if (MRI.isReserved(Def) || MRI.isReserved(Src)) 919480093f4SDimitry Andric return false; 920480093f4SDimitry Andric 921*06c3fb27SDimitry Andric return CopyOperands.Source->isRenamable() && CopyOperands.Source->isKill(); 922480093f4SDimitry Andric } 923480093f4SDimitry Andric 924480093f4SDimitry Andric void MachineCopyPropagation::propagateDefs(MachineInstr &MI) { 925480093f4SDimitry Andric if (!Tracker.hasAnyCopies()) 926480093f4SDimitry Andric return; 927480093f4SDimitry Andric 928480093f4SDimitry Andric for (unsigned OpIdx = 0, OpEnd = MI.getNumOperands(); OpIdx != OpEnd; 929480093f4SDimitry Andric ++OpIdx) { 930480093f4SDimitry Andric MachineOperand &MODef = MI.getOperand(OpIdx); 931480093f4SDimitry Andric 932480093f4SDimitry Andric if (!MODef.isReg() || MODef.isUse()) 933480093f4SDimitry Andric continue; 934480093f4SDimitry Andric 935480093f4SDimitry Andric // Ignore non-trivial cases. 936480093f4SDimitry Andric if (MODef.isTied() || MODef.isUndef() || MODef.isImplicit()) 937480093f4SDimitry Andric continue; 938480093f4SDimitry Andric 939480093f4SDimitry Andric if (!MODef.getReg()) 940480093f4SDimitry Andric continue; 941480093f4SDimitry Andric 942480093f4SDimitry Andric // We only handle if the register comes from a vreg. 943480093f4SDimitry Andric if (!MODef.isRenamable()) 944480093f4SDimitry Andric continue; 945480093f4SDimitry Andric 94681ad6265SDimitry Andric MachineInstr *Copy = Tracker.findAvailBackwardCopy( 94781ad6265SDimitry Andric MI, MODef.getReg().asMCReg(), *TRI, *TII, UseCopyInstr); 948480093f4SDimitry Andric if (!Copy) 949480093f4SDimitry Andric continue; 950480093f4SDimitry Andric 951bdd1243dSDimitry Andric std::optional<DestSourcePair> CopyOperands = 95281ad6265SDimitry Andric isCopyInstr(*Copy, *TII, UseCopyInstr); 95381ad6265SDimitry Andric Register Def = CopyOperands->Destination->getReg(); 95481ad6265SDimitry Andric Register Src = CopyOperands->Source->getReg(); 955480093f4SDimitry Andric 956480093f4SDimitry Andric if (MODef.getReg() != Src) 957480093f4SDimitry Andric continue; 958480093f4SDimitry Andric 959480093f4SDimitry Andric if (!isBackwardPropagatableRegClassCopy(*Copy, MI, OpIdx)) 960480093f4SDimitry Andric continue; 961480093f4SDimitry Andric 962480093f4SDimitry Andric if (hasImplicitOverlap(MI, MODef)) 963480093f4SDimitry Andric continue; 964480093f4SDimitry Andric 965e8d8bef9SDimitry Andric if (hasOverlappingMultipleDef(MI, MODef, Def)) 966e8d8bef9SDimitry Andric continue; 967e8d8bef9SDimitry Andric 968480093f4SDimitry Andric LLVM_DEBUG(dbgs() << "MCP: Replacing " << printReg(MODef.getReg(), TRI) 969480093f4SDimitry Andric << "\n with " << printReg(Def, TRI) << "\n in " 970480093f4SDimitry Andric << MI << " from " << *Copy); 971480093f4SDimitry Andric 972480093f4SDimitry Andric MODef.setReg(Def); 97381ad6265SDimitry Andric MODef.setIsRenamable(CopyOperands->Destination->isRenamable()); 974480093f4SDimitry Andric 975480093f4SDimitry Andric LLVM_DEBUG(dbgs() << "MCP: After replacement: " << MI << "\n"); 976480093f4SDimitry Andric MaybeDeadCopies.insert(Copy); 977480093f4SDimitry Andric Changed = true; 978480093f4SDimitry Andric ++NumCopyBackwardPropagated; 979480093f4SDimitry Andric } 980480093f4SDimitry Andric } 981480093f4SDimitry Andric 982480093f4SDimitry Andric void MachineCopyPropagation::BackwardCopyPropagateBlock( 983480093f4SDimitry Andric MachineBasicBlock &MBB) { 984480093f4SDimitry Andric LLVM_DEBUG(dbgs() << "MCP: BackwardCopyPropagateBlock " << MBB.getName() 985480093f4SDimitry Andric << "\n"); 986480093f4SDimitry Andric 9870eae32dcSDimitry Andric for (MachineInstr &MI : llvm::make_early_inc_range(llvm::reverse(MBB))) { 988480093f4SDimitry Andric // Ignore non-trivial COPYs. 989bdd1243dSDimitry Andric std::optional<DestSourcePair> CopyOperands = 990bdd1243dSDimitry Andric isCopyInstr(MI, *TII, UseCopyInstr); 99181ad6265SDimitry Andric if (CopyOperands && MI.getNumOperands() == 2) { 99281ad6265SDimitry Andric Register DefReg = CopyOperands->Destination->getReg(); 99381ad6265SDimitry Andric Register SrcReg = CopyOperands->Source->getReg(); 994480093f4SDimitry Andric 99581ad6265SDimitry Andric if (!TRI->regsOverlap(DefReg, SrcReg)) { 996480093f4SDimitry Andric // Unlike forward cp, we don't invoke propagateDefs here, 997480093f4SDimitry Andric // just let forward cp do COPY-to-COPY propagation. 998*06c3fb27SDimitry Andric if (isBackwardPropagatableCopy(*CopyOperands, *MRI, *TII)) { 999*06c3fb27SDimitry Andric Tracker.invalidateRegister(SrcReg.asMCReg(), *TRI, *TII, 1000*06c3fb27SDimitry Andric UseCopyInstr); 1001*06c3fb27SDimitry Andric Tracker.invalidateRegister(DefReg.asMCReg(), *TRI, *TII, 1002*06c3fb27SDimitry Andric UseCopyInstr); 100381ad6265SDimitry Andric Tracker.trackCopy(&MI, *TRI, *TII, UseCopyInstr); 1004480093f4SDimitry Andric continue; 1005480093f4SDimitry Andric } 1006480093f4SDimitry Andric } 100781ad6265SDimitry Andric } 1008480093f4SDimitry Andric 1009480093f4SDimitry Andric // Invalidate any earlyclobber regs first. 10100eae32dcSDimitry Andric for (const MachineOperand &MO : MI.operands()) 1011480093f4SDimitry Andric if (MO.isReg() && MO.isEarlyClobber()) { 1012e8d8bef9SDimitry Andric MCRegister Reg = MO.getReg().asMCReg(); 1013480093f4SDimitry Andric if (!Reg) 1014480093f4SDimitry Andric continue; 101581ad6265SDimitry Andric Tracker.invalidateRegister(Reg, *TRI, *TII, UseCopyInstr); 1016480093f4SDimitry Andric } 1017480093f4SDimitry Andric 10180eae32dcSDimitry Andric propagateDefs(MI); 10190eae32dcSDimitry Andric for (const MachineOperand &MO : MI.operands()) { 1020480093f4SDimitry Andric if (!MO.isReg()) 1021480093f4SDimitry Andric continue; 1022480093f4SDimitry Andric 1023480093f4SDimitry Andric if (!MO.getReg()) 1024480093f4SDimitry Andric continue; 1025480093f4SDimitry Andric 1026480093f4SDimitry Andric if (MO.isDef()) 102781ad6265SDimitry Andric Tracker.invalidateRegister(MO.getReg().asMCReg(), *TRI, *TII, 102881ad6265SDimitry Andric UseCopyInstr); 1029480093f4SDimitry Andric 1030fe6060f1SDimitry Andric if (MO.readsReg()) { 1031fe6060f1SDimitry Andric if (MO.isDebug()) { 1032fe6060f1SDimitry Andric // Check if the register in the debug instruction is utilized 1033fe6060f1SDimitry Andric // in a copy instruction, so we can update the debug info if the 1034fe6060f1SDimitry Andric // register is changed. 1035*06c3fb27SDimitry Andric for (MCRegUnit Unit : TRI->regunits(MO.getReg().asMCReg())) { 1036*06c3fb27SDimitry Andric if (auto *Copy = Tracker.findCopyDefViaUnit(Unit, *TRI)) { 10370eae32dcSDimitry Andric CopyDbgUsers[Copy].insert(&MI); 1038fe6060f1SDimitry Andric } 1039fe6060f1SDimitry Andric } 1040fe6060f1SDimitry Andric } else { 104181ad6265SDimitry Andric Tracker.invalidateRegister(MO.getReg().asMCReg(), *TRI, *TII, 104281ad6265SDimitry Andric UseCopyInstr); 1043480093f4SDimitry Andric } 1044480093f4SDimitry Andric } 1045fe6060f1SDimitry Andric } 1046fe6060f1SDimitry Andric } 1047480093f4SDimitry Andric 1048480093f4SDimitry Andric for (auto *Copy : MaybeDeadCopies) { 1049bdd1243dSDimitry Andric std::optional<DestSourcePair> CopyOperands = 105081ad6265SDimitry Andric isCopyInstr(*Copy, *TII, UseCopyInstr); 105181ad6265SDimitry Andric Register Src = CopyOperands->Source->getReg(); 105281ad6265SDimitry Andric Register Def = CopyOperands->Destination->getReg(); 1053fe6060f1SDimitry Andric SmallVector<MachineInstr *> MaybeDeadDbgUsers(CopyDbgUsers[Copy].begin(), 1054fe6060f1SDimitry Andric CopyDbgUsers[Copy].end()); 1055fe6060f1SDimitry Andric 1056fe6060f1SDimitry Andric MRI->updateDbgUsersToReg(Src.asMCReg(), Def.asMCReg(), MaybeDeadDbgUsers); 1057480093f4SDimitry Andric Copy->eraseFromParent(); 1058480093f4SDimitry Andric ++NumDeletes; 1059480093f4SDimitry Andric } 1060480093f4SDimitry Andric 1061480093f4SDimitry Andric MaybeDeadCopies.clear(); 1062480093f4SDimitry Andric CopyDbgUsers.clear(); 1063480093f4SDimitry Andric Tracker.clear(); 1064480093f4SDimitry Andric } 1065480093f4SDimitry Andric 1066*06c3fb27SDimitry Andric static void LLVM_ATTRIBUTE_UNUSED printSpillReloadChain( 1067*06c3fb27SDimitry Andric DenseMap<MachineInstr *, SmallVector<MachineInstr *>> &SpillChain, 1068*06c3fb27SDimitry Andric DenseMap<MachineInstr *, SmallVector<MachineInstr *>> &ReloadChain, 1069*06c3fb27SDimitry Andric MachineInstr *Leader) { 1070*06c3fb27SDimitry Andric auto &SC = SpillChain[Leader]; 1071*06c3fb27SDimitry Andric auto &RC = ReloadChain[Leader]; 1072*06c3fb27SDimitry Andric for (auto I = SC.rbegin(), E = SC.rend(); I != E; ++I) 1073*06c3fb27SDimitry Andric (*I)->dump(); 1074*06c3fb27SDimitry Andric for (MachineInstr *MI : RC) 1075*06c3fb27SDimitry Andric MI->dump(); 1076*06c3fb27SDimitry Andric } 1077*06c3fb27SDimitry Andric 1078*06c3fb27SDimitry Andric // Remove spill-reload like copy chains. For example 1079*06c3fb27SDimitry Andric // r0 = COPY r1 1080*06c3fb27SDimitry Andric // r1 = COPY r2 1081*06c3fb27SDimitry Andric // r2 = COPY r3 1082*06c3fb27SDimitry Andric // r3 = COPY r4 1083*06c3fb27SDimitry Andric // <def-use r4> 1084*06c3fb27SDimitry Andric // r4 = COPY r3 1085*06c3fb27SDimitry Andric // r3 = COPY r2 1086*06c3fb27SDimitry Andric // r2 = COPY r1 1087*06c3fb27SDimitry Andric // r1 = COPY r0 1088*06c3fb27SDimitry Andric // will be folded into 1089*06c3fb27SDimitry Andric // r0 = COPY r1 1090*06c3fb27SDimitry Andric // r1 = COPY r4 1091*06c3fb27SDimitry Andric // <def-use r4> 1092*06c3fb27SDimitry Andric // r4 = COPY r1 1093*06c3fb27SDimitry Andric // r1 = COPY r0 1094*06c3fb27SDimitry Andric // TODO: Currently we don't track usage of r0 outside the chain, so we 1095*06c3fb27SDimitry Andric // conservatively keep its value as it was before the rewrite. 1096*06c3fb27SDimitry Andric // 1097*06c3fb27SDimitry Andric // The algorithm is trying to keep 1098*06c3fb27SDimitry Andric // property#1: No Def of spill COPY in the chain is used or defined until the 1099*06c3fb27SDimitry Andric // paired reload COPY in the chain uses the Def. 1100*06c3fb27SDimitry Andric // 1101*06c3fb27SDimitry Andric // property#2: NO Source of COPY in the chain is used or defined until the next 1102*06c3fb27SDimitry Andric // COPY in the chain defines the Source, except the innermost spill-reload 1103*06c3fb27SDimitry Andric // pair. 1104*06c3fb27SDimitry Andric // 1105*06c3fb27SDimitry Andric // The algorithm is conducted by checking every COPY inside the MBB, assuming 1106*06c3fb27SDimitry Andric // the COPY is a reload COPY, then try to find paired spill COPY by searching 1107*06c3fb27SDimitry Andric // the COPY defines the Src of the reload COPY backward. If such pair is found, 1108*06c3fb27SDimitry Andric // it either belongs to an existing chain or a new chain depends on 1109*06c3fb27SDimitry Andric // last available COPY uses the Def of the reload COPY. 1110*06c3fb27SDimitry Andric // Implementation notes, we use CopyTracker::findLastDefCopy(Reg, ...) to find 1111*06c3fb27SDimitry Andric // out last COPY that defines Reg; we use CopyTracker::findLastUseCopy(Reg, ...) 1112*06c3fb27SDimitry Andric // to find out last COPY that uses Reg. When we are encountered with a Non-COPY 1113*06c3fb27SDimitry Andric // instruction, we check registers in the operands of this instruction. If this 1114*06c3fb27SDimitry Andric // Reg is defined by a COPY, we untrack this Reg via 1115*06c3fb27SDimitry Andric // CopyTracker::clobberRegister(Reg, ...). 1116*06c3fb27SDimitry Andric void MachineCopyPropagation::EliminateSpillageCopies(MachineBasicBlock &MBB) { 1117*06c3fb27SDimitry Andric // ChainLeader maps MI inside a spill-reload chain to its innermost reload COPY. 1118*06c3fb27SDimitry Andric // Thus we can track if a MI belongs to an existing spill-reload chain. 1119*06c3fb27SDimitry Andric DenseMap<MachineInstr *, MachineInstr *> ChainLeader; 1120*06c3fb27SDimitry Andric // SpillChain maps innermost reload COPY of a spill-reload chain to a sequence 1121*06c3fb27SDimitry Andric // of COPYs that forms spills of a spill-reload chain. 1122*06c3fb27SDimitry Andric // ReloadChain maps innermost reload COPY of a spill-reload chain to a 1123*06c3fb27SDimitry Andric // sequence of COPYs that forms reloads of a spill-reload chain. 1124*06c3fb27SDimitry Andric DenseMap<MachineInstr *, SmallVector<MachineInstr *>> SpillChain, ReloadChain; 1125*06c3fb27SDimitry Andric // If a COPY's Source has use or def until next COPY defines the Source, 1126*06c3fb27SDimitry Andric // we put the COPY in this set to keep property#2. 1127*06c3fb27SDimitry Andric DenseSet<const MachineInstr *> CopySourceInvalid; 1128*06c3fb27SDimitry Andric 1129*06c3fb27SDimitry Andric auto TryFoldSpillageCopies = 1130*06c3fb27SDimitry Andric [&, this](const SmallVectorImpl<MachineInstr *> &SC, 1131*06c3fb27SDimitry Andric const SmallVectorImpl<MachineInstr *> &RC) { 1132*06c3fb27SDimitry Andric assert(SC.size() == RC.size() && "Spill-reload should be paired"); 1133*06c3fb27SDimitry Andric 1134*06c3fb27SDimitry Andric // We need at least 3 pairs of copies for the transformation to apply, 1135*06c3fb27SDimitry Andric // because the first outermost pair cannot be removed since we don't 1136*06c3fb27SDimitry Andric // recolor outside of the chain and that we need at least one temporary 1137*06c3fb27SDimitry Andric // spill slot to shorten the chain. If we only have a chain of two 1138*06c3fb27SDimitry Andric // pairs, we already have the shortest sequence this code can handle: 1139*06c3fb27SDimitry Andric // the outermost pair for the temporary spill slot, and the pair that 1140*06c3fb27SDimitry Andric // use that temporary spill slot for the other end of the chain. 1141*06c3fb27SDimitry Andric // TODO: We might be able to simplify to one spill-reload pair if collecting 1142*06c3fb27SDimitry Andric // more infomation about the outermost COPY. 1143*06c3fb27SDimitry Andric if (SC.size() <= 2) 1144*06c3fb27SDimitry Andric return; 1145*06c3fb27SDimitry Andric 1146*06c3fb27SDimitry Andric // If violate property#2, we don't fold the chain. 1147*06c3fb27SDimitry Andric for (const MachineInstr *Spill : make_range(SC.begin() + 1, SC.end())) 1148*06c3fb27SDimitry Andric if (CopySourceInvalid.count(Spill)) 1149*06c3fb27SDimitry Andric return; 1150*06c3fb27SDimitry Andric 1151*06c3fb27SDimitry Andric for (const MachineInstr *Reload : make_range(RC.begin(), RC.end() - 1)) 1152*06c3fb27SDimitry Andric if (CopySourceInvalid.count(Reload)) 1153*06c3fb27SDimitry Andric return; 1154*06c3fb27SDimitry Andric 1155*06c3fb27SDimitry Andric auto CheckCopyConstraint = [this](Register Def, Register Src) { 1156*06c3fb27SDimitry Andric for (const TargetRegisterClass *RC : TRI->regclasses()) { 1157*06c3fb27SDimitry Andric if (RC->contains(Def) && RC->contains(Src)) 1158*06c3fb27SDimitry Andric return true; 1159*06c3fb27SDimitry Andric } 1160*06c3fb27SDimitry Andric return false; 1161*06c3fb27SDimitry Andric }; 1162*06c3fb27SDimitry Andric 1163*06c3fb27SDimitry Andric auto UpdateReg = [](MachineInstr *MI, const MachineOperand *Old, 1164*06c3fb27SDimitry Andric const MachineOperand *New) { 1165*06c3fb27SDimitry Andric for (MachineOperand &MO : MI->operands()) { 1166*06c3fb27SDimitry Andric if (&MO == Old) 1167*06c3fb27SDimitry Andric MO.setReg(New->getReg()); 1168*06c3fb27SDimitry Andric } 1169*06c3fb27SDimitry Andric }; 1170*06c3fb27SDimitry Andric 1171*06c3fb27SDimitry Andric std::optional<DestSourcePair> InnerMostSpillCopy = 1172*06c3fb27SDimitry Andric isCopyInstr(*SC[0], *TII, UseCopyInstr); 1173*06c3fb27SDimitry Andric std::optional<DestSourcePair> OuterMostSpillCopy = 1174*06c3fb27SDimitry Andric isCopyInstr(*SC.back(), *TII, UseCopyInstr); 1175*06c3fb27SDimitry Andric std::optional<DestSourcePair> InnerMostReloadCopy = 1176*06c3fb27SDimitry Andric isCopyInstr(*RC[0], *TII, UseCopyInstr); 1177*06c3fb27SDimitry Andric std::optional<DestSourcePair> OuterMostReloadCopy = 1178*06c3fb27SDimitry Andric isCopyInstr(*RC.back(), *TII, UseCopyInstr); 1179*06c3fb27SDimitry Andric if (!CheckCopyConstraint(OuterMostSpillCopy->Source->getReg(), 1180*06c3fb27SDimitry Andric InnerMostSpillCopy->Source->getReg()) || 1181*06c3fb27SDimitry Andric !CheckCopyConstraint(InnerMostReloadCopy->Destination->getReg(), 1182*06c3fb27SDimitry Andric OuterMostReloadCopy->Destination->getReg())) 1183*06c3fb27SDimitry Andric return; 1184*06c3fb27SDimitry Andric 1185*06c3fb27SDimitry Andric SpillageChainsLength += SC.size() + RC.size(); 1186*06c3fb27SDimitry Andric NumSpillageChains += 1; 1187*06c3fb27SDimitry Andric UpdateReg(SC[0], InnerMostSpillCopy->Destination, 1188*06c3fb27SDimitry Andric OuterMostSpillCopy->Source); 1189*06c3fb27SDimitry Andric UpdateReg(RC[0], InnerMostReloadCopy->Source, 1190*06c3fb27SDimitry Andric OuterMostReloadCopy->Destination); 1191*06c3fb27SDimitry Andric 1192*06c3fb27SDimitry Andric for (size_t I = 1; I < SC.size() - 1; ++I) { 1193*06c3fb27SDimitry Andric SC[I]->eraseFromParent(); 1194*06c3fb27SDimitry Andric RC[I]->eraseFromParent(); 1195*06c3fb27SDimitry Andric NumDeletes += 2; 1196*06c3fb27SDimitry Andric } 1197*06c3fb27SDimitry Andric }; 1198*06c3fb27SDimitry Andric 1199*06c3fb27SDimitry Andric auto IsFoldableCopy = [this](const MachineInstr &MaybeCopy) { 1200*06c3fb27SDimitry Andric if (MaybeCopy.getNumImplicitOperands() > 0) 1201*06c3fb27SDimitry Andric return false; 1202*06c3fb27SDimitry Andric std::optional<DestSourcePair> CopyOperands = 1203*06c3fb27SDimitry Andric isCopyInstr(MaybeCopy, *TII, UseCopyInstr); 1204*06c3fb27SDimitry Andric if (!CopyOperands) 1205*06c3fb27SDimitry Andric return false; 1206*06c3fb27SDimitry Andric Register Src = CopyOperands->Source->getReg(); 1207*06c3fb27SDimitry Andric Register Def = CopyOperands->Destination->getReg(); 1208*06c3fb27SDimitry Andric return Src && Def && !TRI->regsOverlap(Src, Def) && 1209*06c3fb27SDimitry Andric CopyOperands->Source->isRenamable() && 1210*06c3fb27SDimitry Andric CopyOperands->Destination->isRenamable(); 1211*06c3fb27SDimitry Andric }; 1212*06c3fb27SDimitry Andric 1213*06c3fb27SDimitry Andric auto IsSpillReloadPair = [&, this](const MachineInstr &Spill, 1214*06c3fb27SDimitry Andric const MachineInstr &Reload) { 1215*06c3fb27SDimitry Andric if (!IsFoldableCopy(Spill) || !IsFoldableCopy(Reload)) 1216*06c3fb27SDimitry Andric return false; 1217*06c3fb27SDimitry Andric std::optional<DestSourcePair> SpillCopy = 1218*06c3fb27SDimitry Andric isCopyInstr(Spill, *TII, UseCopyInstr); 1219*06c3fb27SDimitry Andric std::optional<DestSourcePair> ReloadCopy = 1220*06c3fb27SDimitry Andric isCopyInstr(Reload, *TII, UseCopyInstr); 1221*06c3fb27SDimitry Andric if (!SpillCopy || !ReloadCopy) 1222*06c3fb27SDimitry Andric return false; 1223*06c3fb27SDimitry Andric return SpillCopy->Source->getReg() == ReloadCopy->Destination->getReg() && 1224*06c3fb27SDimitry Andric SpillCopy->Destination->getReg() == ReloadCopy->Source->getReg(); 1225*06c3fb27SDimitry Andric }; 1226*06c3fb27SDimitry Andric 1227*06c3fb27SDimitry Andric auto IsChainedCopy = [&, this](const MachineInstr &Prev, 1228*06c3fb27SDimitry Andric const MachineInstr &Current) { 1229*06c3fb27SDimitry Andric if (!IsFoldableCopy(Prev) || !IsFoldableCopy(Current)) 1230*06c3fb27SDimitry Andric return false; 1231*06c3fb27SDimitry Andric std::optional<DestSourcePair> PrevCopy = 1232*06c3fb27SDimitry Andric isCopyInstr(Prev, *TII, UseCopyInstr); 1233*06c3fb27SDimitry Andric std::optional<DestSourcePair> CurrentCopy = 1234*06c3fb27SDimitry Andric isCopyInstr(Current, *TII, UseCopyInstr); 1235*06c3fb27SDimitry Andric if (!PrevCopy || !CurrentCopy) 1236*06c3fb27SDimitry Andric return false; 1237*06c3fb27SDimitry Andric return PrevCopy->Source->getReg() == CurrentCopy->Destination->getReg(); 1238*06c3fb27SDimitry Andric }; 1239*06c3fb27SDimitry Andric 1240*06c3fb27SDimitry Andric for (MachineInstr &MI : llvm::make_early_inc_range(MBB)) { 1241*06c3fb27SDimitry Andric std::optional<DestSourcePair> CopyOperands = 1242*06c3fb27SDimitry Andric isCopyInstr(MI, *TII, UseCopyInstr); 1243*06c3fb27SDimitry Andric 1244*06c3fb27SDimitry Andric // Update track information via non-copy instruction. 1245*06c3fb27SDimitry Andric SmallSet<Register, 8> RegsToClobber; 1246*06c3fb27SDimitry Andric if (!CopyOperands) { 1247*06c3fb27SDimitry Andric for (const MachineOperand &MO : MI.operands()) { 1248*06c3fb27SDimitry Andric if (!MO.isReg()) 1249*06c3fb27SDimitry Andric continue; 1250*06c3fb27SDimitry Andric Register Reg = MO.getReg(); 1251*06c3fb27SDimitry Andric if (!Reg) 1252*06c3fb27SDimitry Andric continue; 1253*06c3fb27SDimitry Andric MachineInstr *LastUseCopy = 1254*06c3fb27SDimitry Andric Tracker.findLastSeenUseInCopy(Reg.asMCReg(), *TRI); 1255*06c3fb27SDimitry Andric if (LastUseCopy) { 1256*06c3fb27SDimitry Andric LLVM_DEBUG(dbgs() << "MCP: Copy source of\n"); 1257*06c3fb27SDimitry Andric LLVM_DEBUG(LastUseCopy->dump()); 1258*06c3fb27SDimitry Andric LLVM_DEBUG(dbgs() << "might be invalidated by\n"); 1259*06c3fb27SDimitry Andric LLVM_DEBUG(MI.dump()); 1260*06c3fb27SDimitry Andric CopySourceInvalid.insert(LastUseCopy); 1261*06c3fb27SDimitry Andric } 1262*06c3fb27SDimitry Andric // Must be noted Tracker.clobberRegister(Reg, ...) removes tracking of 1263*06c3fb27SDimitry Andric // Reg, i.e, COPY that defines Reg is removed from the mapping as well 1264*06c3fb27SDimitry Andric // as marking COPYs that uses Reg unavailable. 1265*06c3fb27SDimitry Andric // We don't invoke CopyTracker::clobberRegister(Reg, ...) if Reg is not 1266*06c3fb27SDimitry Andric // defined by a previous COPY, since we don't want to make COPYs uses 1267*06c3fb27SDimitry Andric // Reg unavailable. 1268*06c3fb27SDimitry Andric if (Tracker.findLastSeenDefInCopy(MI, Reg.asMCReg(), *TRI, *TII, 1269*06c3fb27SDimitry Andric UseCopyInstr)) 1270*06c3fb27SDimitry Andric // Thus we can keep the property#1. 1271*06c3fb27SDimitry Andric RegsToClobber.insert(Reg); 1272*06c3fb27SDimitry Andric } 1273*06c3fb27SDimitry Andric for (Register Reg : RegsToClobber) { 1274*06c3fb27SDimitry Andric Tracker.clobberRegister(Reg, *TRI, *TII, UseCopyInstr); 1275*06c3fb27SDimitry Andric LLVM_DEBUG(dbgs() << "MCP: Removed tracking of " << printReg(Reg, TRI) 1276*06c3fb27SDimitry Andric << "\n"); 1277*06c3fb27SDimitry Andric } 1278*06c3fb27SDimitry Andric continue; 1279*06c3fb27SDimitry Andric } 1280*06c3fb27SDimitry Andric 1281*06c3fb27SDimitry Andric Register Src = CopyOperands->Source->getReg(); 1282*06c3fb27SDimitry Andric Register Def = CopyOperands->Destination->getReg(); 1283*06c3fb27SDimitry Andric // Check if we can find a pair spill-reload copy. 1284*06c3fb27SDimitry Andric LLVM_DEBUG(dbgs() << "MCP: Searching paired spill for reload: "); 1285*06c3fb27SDimitry Andric LLVM_DEBUG(MI.dump()); 1286*06c3fb27SDimitry Andric MachineInstr *MaybeSpill = 1287*06c3fb27SDimitry Andric Tracker.findLastSeenDefInCopy(MI, Src.asMCReg(), *TRI, *TII, UseCopyInstr); 1288*06c3fb27SDimitry Andric bool MaybeSpillIsChained = ChainLeader.count(MaybeSpill); 1289*06c3fb27SDimitry Andric if (!MaybeSpillIsChained && MaybeSpill && 1290*06c3fb27SDimitry Andric IsSpillReloadPair(*MaybeSpill, MI)) { 1291*06c3fb27SDimitry Andric // Check if we already have an existing chain. Now we have a 1292*06c3fb27SDimitry Andric // spill-reload pair. 1293*06c3fb27SDimitry Andric // L2: r2 = COPY r3 1294*06c3fb27SDimitry Andric // L5: r3 = COPY r2 1295*06c3fb27SDimitry Andric // Looking for a valid COPY before L5 which uses r3. 1296*06c3fb27SDimitry Andric // This can be serverial cases. 1297*06c3fb27SDimitry Andric // Case #1: 1298*06c3fb27SDimitry Andric // No COPY is found, which can be r3 is def-use between (L2, L5), we 1299*06c3fb27SDimitry Andric // create a new chain for L2 and L5. 1300*06c3fb27SDimitry Andric // Case #2: 1301*06c3fb27SDimitry Andric // L2: r2 = COPY r3 1302*06c3fb27SDimitry Andric // L5: r3 = COPY r2 1303*06c3fb27SDimitry Andric // Such COPY is found and is L2, we create a new chain for L2 and L5. 1304*06c3fb27SDimitry Andric // Case #3: 1305*06c3fb27SDimitry Andric // L2: r2 = COPY r3 1306*06c3fb27SDimitry Andric // L3: r1 = COPY r3 1307*06c3fb27SDimitry Andric // L5: r3 = COPY r2 1308*06c3fb27SDimitry Andric // we create a new chain for L2 and L5. 1309*06c3fb27SDimitry Andric // Case #4: 1310*06c3fb27SDimitry Andric // L2: r2 = COPY r3 1311*06c3fb27SDimitry Andric // L3: r1 = COPY r3 1312*06c3fb27SDimitry Andric // L4: r3 = COPY r1 1313*06c3fb27SDimitry Andric // L5: r3 = COPY r2 1314*06c3fb27SDimitry Andric // Such COPY won't be found since L4 defines r3. we create a new chain 1315*06c3fb27SDimitry Andric // for L2 and L5. 1316*06c3fb27SDimitry Andric // Case #5: 1317*06c3fb27SDimitry Andric // L2: r2 = COPY r3 1318*06c3fb27SDimitry Andric // L3: r3 = COPY r1 1319*06c3fb27SDimitry Andric // L4: r1 = COPY r3 1320*06c3fb27SDimitry Andric // L5: r3 = COPY r2 1321*06c3fb27SDimitry Andric // COPY is found and is L4 which belongs to an existing chain, we add 1322*06c3fb27SDimitry Andric // L2 and L5 to this chain. 1323*06c3fb27SDimitry Andric LLVM_DEBUG(dbgs() << "MCP: Found spill: "); 1324*06c3fb27SDimitry Andric LLVM_DEBUG(MaybeSpill->dump()); 1325*06c3fb27SDimitry Andric MachineInstr *MaybePrevReload = 1326*06c3fb27SDimitry Andric Tracker.findLastSeenUseInCopy(Def.asMCReg(), *TRI); 1327*06c3fb27SDimitry Andric auto Leader = ChainLeader.find(MaybePrevReload); 1328*06c3fb27SDimitry Andric MachineInstr *L = nullptr; 1329*06c3fb27SDimitry Andric if (Leader == ChainLeader.end() || 1330*06c3fb27SDimitry Andric (MaybePrevReload && !IsChainedCopy(*MaybePrevReload, MI))) { 1331*06c3fb27SDimitry Andric L = &MI; 1332*06c3fb27SDimitry Andric assert(!SpillChain.count(L) && 1333*06c3fb27SDimitry Andric "SpillChain should not have contained newly found chain"); 1334*06c3fb27SDimitry Andric } else { 1335*06c3fb27SDimitry Andric assert(MaybePrevReload && 1336*06c3fb27SDimitry Andric "Found a valid leader through nullptr should not happend"); 1337*06c3fb27SDimitry Andric L = Leader->second; 1338*06c3fb27SDimitry Andric assert(SpillChain[L].size() > 0 && 1339*06c3fb27SDimitry Andric "Existing chain's length should be larger than zero"); 1340*06c3fb27SDimitry Andric } 1341*06c3fb27SDimitry Andric assert(!ChainLeader.count(&MI) && !ChainLeader.count(MaybeSpill) && 1342*06c3fb27SDimitry Andric "Newly found paired spill-reload should not belong to any chain " 1343*06c3fb27SDimitry Andric "at this point"); 1344*06c3fb27SDimitry Andric ChainLeader.insert({MaybeSpill, L}); 1345*06c3fb27SDimitry Andric ChainLeader.insert({&MI, L}); 1346*06c3fb27SDimitry Andric SpillChain[L].push_back(MaybeSpill); 1347*06c3fb27SDimitry Andric ReloadChain[L].push_back(&MI); 1348*06c3fb27SDimitry Andric LLVM_DEBUG(dbgs() << "MCP: Chain " << L << " now is:\n"); 1349*06c3fb27SDimitry Andric LLVM_DEBUG(printSpillReloadChain(SpillChain, ReloadChain, L)); 1350*06c3fb27SDimitry Andric } else if (MaybeSpill && !MaybeSpillIsChained) { 1351*06c3fb27SDimitry Andric // MaybeSpill is unable to pair with MI. That's to say adding MI makes 1352*06c3fb27SDimitry Andric // the chain invalid. 1353*06c3fb27SDimitry Andric // The COPY defines Src is no longer considered as a candidate of a 1354*06c3fb27SDimitry Andric // valid chain. Since we expect the Def of a spill copy isn't used by 1355*06c3fb27SDimitry Andric // any COPY instruction until a reload copy. For example: 1356*06c3fb27SDimitry Andric // L1: r1 = COPY r2 1357*06c3fb27SDimitry Andric // L2: r3 = COPY r1 1358*06c3fb27SDimitry Andric // If we later have 1359*06c3fb27SDimitry Andric // L1: r1 = COPY r2 1360*06c3fb27SDimitry Andric // L2: r3 = COPY r1 1361*06c3fb27SDimitry Andric // L3: r2 = COPY r1 1362*06c3fb27SDimitry Andric // L1 and L3 can't be a valid spill-reload pair. 1363*06c3fb27SDimitry Andric // Thus we keep the property#1. 1364*06c3fb27SDimitry Andric LLVM_DEBUG(dbgs() << "MCP: Not paired spill-reload:\n"); 1365*06c3fb27SDimitry Andric LLVM_DEBUG(MaybeSpill->dump()); 1366*06c3fb27SDimitry Andric LLVM_DEBUG(MI.dump()); 1367*06c3fb27SDimitry Andric Tracker.clobberRegister(Src.asMCReg(), *TRI, *TII, UseCopyInstr); 1368*06c3fb27SDimitry Andric LLVM_DEBUG(dbgs() << "MCP: Removed tracking of " << printReg(Src, TRI) 1369*06c3fb27SDimitry Andric << "\n"); 1370*06c3fb27SDimitry Andric } 1371*06c3fb27SDimitry Andric Tracker.trackCopy(&MI, *TRI, *TII, UseCopyInstr); 1372*06c3fb27SDimitry Andric } 1373*06c3fb27SDimitry Andric 1374*06c3fb27SDimitry Andric for (auto I = SpillChain.begin(), E = SpillChain.end(); I != E; ++I) { 1375*06c3fb27SDimitry Andric auto &SC = I->second; 1376*06c3fb27SDimitry Andric assert(ReloadChain.count(I->first) && 1377*06c3fb27SDimitry Andric "Reload chain of the same leader should exist"); 1378*06c3fb27SDimitry Andric auto &RC = ReloadChain[I->first]; 1379*06c3fb27SDimitry Andric TryFoldSpillageCopies(SC, RC); 1380*06c3fb27SDimitry Andric } 1381*06c3fb27SDimitry Andric 1382*06c3fb27SDimitry Andric MaybeDeadCopies.clear(); 1383*06c3fb27SDimitry Andric CopyDbgUsers.clear(); 1384*06c3fb27SDimitry Andric Tracker.clear(); 1385*06c3fb27SDimitry Andric } 1386*06c3fb27SDimitry Andric 13870b57cec5SDimitry Andric bool MachineCopyPropagation::runOnMachineFunction(MachineFunction &MF) { 13880b57cec5SDimitry Andric if (skipFunction(MF.getFunction())) 13890b57cec5SDimitry Andric return false; 13900b57cec5SDimitry Andric 1391*06c3fb27SDimitry Andric bool isSpillageCopyElimEnabled = false; 1392*06c3fb27SDimitry Andric switch (EnableSpillageCopyElimination) { 1393*06c3fb27SDimitry Andric case cl::BOU_UNSET: 1394*06c3fb27SDimitry Andric isSpillageCopyElimEnabled = 1395*06c3fb27SDimitry Andric MF.getSubtarget().enableSpillageCopyElimination(); 1396*06c3fb27SDimitry Andric break; 1397*06c3fb27SDimitry Andric case cl::BOU_TRUE: 1398*06c3fb27SDimitry Andric isSpillageCopyElimEnabled = true; 1399*06c3fb27SDimitry Andric break; 1400*06c3fb27SDimitry Andric case cl::BOU_FALSE: 1401*06c3fb27SDimitry Andric isSpillageCopyElimEnabled = false; 1402*06c3fb27SDimitry Andric break; 1403*06c3fb27SDimitry Andric } 1404*06c3fb27SDimitry Andric 14050b57cec5SDimitry Andric Changed = false; 14060b57cec5SDimitry Andric 14070b57cec5SDimitry Andric TRI = MF.getSubtarget().getRegisterInfo(); 14080b57cec5SDimitry Andric TII = MF.getSubtarget().getInstrInfo(); 14090b57cec5SDimitry Andric MRI = &MF.getRegInfo(); 14100b57cec5SDimitry Andric 1411480093f4SDimitry Andric for (MachineBasicBlock &MBB : MF) { 1412*06c3fb27SDimitry Andric if (isSpillageCopyElimEnabled) 1413*06c3fb27SDimitry Andric EliminateSpillageCopies(MBB); 1414480093f4SDimitry Andric BackwardCopyPropagateBlock(MBB); 1415480093f4SDimitry Andric ForwardCopyPropagateBlock(MBB); 1416480093f4SDimitry Andric } 14170b57cec5SDimitry Andric 14180b57cec5SDimitry Andric return Changed; 14190b57cec5SDimitry Andric } 142081ad6265SDimitry Andric 142181ad6265SDimitry Andric MachineFunctionPass * 142281ad6265SDimitry Andric llvm::createMachineCopyPropagationPass(bool UseCopyInstr = false) { 142381ad6265SDimitry Andric return new MachineCopyPropagation(UseCopyInstr); 142481ad6265SDimitry Andric } 1425