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"); 830b57cec5SDimitry Andric DEBUG_COUNTER(FwdCounter, "machine-cp-fwd", 840b57cec5SDimitry Andric "Controls which register COPYs are forwarded"); 850b57cec5SDimitry Andric 860b57cec5SDimitry Andric namespace { 870b57cec5SDimitry Andric 880b57cec5SDimitry Andric class CopyTracker { 890b57cec5SDimitry Andric struct CopyInfo { 900b57cec5SDimitry Andric MachineInstr *MI; 910b57cec5SDimitry Andric SmallVector<unsigned, 4> DefRegs; 920b57cec5SDimitry Andric bool Avail; 930b57cec5SDimitry Andric }; 940b57cec5SDimitry Andric 950b57cec5SDimitry Andric DenseMap<unsigned, CopyInfo> Copies; 960b57cec5SDimitry Andric 970b57cec5SDimitry Andric public: 980b57cec5SDimitry Andric /// Mark all of the given registers and their subregisters as unavailable for 990b57cec5SDimitry Andric /// copying. 1000b57cec5SDimitry Andric void markRegsUnavailable(ArrayRef<unsigned> Regs, 1010b57cec5SDimitry Andric const TargetRegisterInfo &TRI) { 1020b57cec5SDimitry Andric for (unsigned Reg : Regs) { 1030b57cec5SDimitry Andric // Source of copy is no longer available for propagation. 1040b57cec5SDimitry Andric for (MCRegUnitIterator RUI(Reg, &TRI); RUI.isValid(); ++RUI) { 1050b57cec5SDimitry Andric auto CI = Copies.find(*RUI); 1060b57cec5SDimitry Andric if (CI != Copies.end()) 1070b57cec5SDimitry Andric CI->second.Avail = false; 1080b57cec5SDimitry Andric } 1090b57cec5SDimitry Andric } 1100b57cec5SDimitry Andric } 1110b57cec5SDimitry Andric 112480093f4SDimitry Andric /// Remove register from copy maps. 113480093f4SDimitry Andric void invalidateRegister(unsigned Reg, const TargetRegisterInfo &TRI) { 114480093f4SDimitry Andric // Since Reg might be a subreg of some registers, only invalidate Reg is not 115480093f4SDimitry Andric // enough. We have to find the COPY defines Reg or registers defined by Reg 116480093f4SDimitry Andric // and invalidate all of them. 1175ffd83dbSDimitry Andric SmallSet<unsigned, 8> RegsToInvalidate; 1185ffd83dbSDimitry Andric RegsToInvalidate.insert(Reg); 119480093f4SDimitry Andric for (MCRegUnitIterator RUI(Reg, &TRI); RUI.isValid(); ++RUI) { 120480093f4SDimitry Andric auto I = Copies.find(*RUI); 121480093f4SDimitry Andric if (I != Copies.end()) { 122480093f4SDimitry Andric if (MachineInstr *MI = I->second.MI) { 123480093f4SDimitry Andric RegsToInvalidate.insert(MI->getOperand(0).getReg()); 124480093f4SDimitry Andric RegsToInvalidate.insert(MI->getOperand(1).getReg()); 125480093f4SDimitry Andric } 126480093f4SDimitry Andric RegsToInvalidate.insert(I->second.DefRegs.begin(), 127480093f4SDimitry Andric I->second.DefRegs.end()); 128480093f4SDimitry Andric } 129480093f4SDimitry Andric } 130480093f4SDimitry Andric for (unsigned InvalidReg : RegsToInvalidate) 131480093f4SDimitry Andric for (MCRegUnitIterator RUI(InvalidReg, &TRI); RUI.isValid(); ++RUI) 132480093f4SDimitry Andric Copies.erase(*RUI); 133480093f4SDimitry Andric } 134480093f4SDimitry Andric 1350b57cec5SDimitry Andric /// Clobber a single register, removing it from the tracker's copy maps. 1360b57cec5SDimitry Andric void clobberRegister(unsigned Reg, const TargetRegisterInfo &TRI) { 1370b57cec5SDimitry Andric for (MCRegUnitIterator RUI(Reg, &TRI); RUI.isValid(); ++RUI) { 1380b57cec5SDimitry Andric auto I = Copies.find(*RUI); 1390b57cec5SDimitry Andric if (I != Copies.end()) { 1400b57cec5SDimitry Andric // When we clobber the source of a copy, we need to clobber everything 1410b57cec5SDimitry Andric // it defined. 1420b57cec5SDimitry Andric markRegsUnavailable(I->second.DefRegs, TRI); 1430b57cec5SDimitry Andric // When we clobber the destination of a copy, we need to clobber the 1440b57cec5SDimitry Andric // whole register it defined. 1450b57cec5SDimitry Andric if (MachineInstr *MI = I->second.MI) 1460b57cec5SDimitry Andric markRegsUnavailable({MI->getOperand(0).getReg()}, TRI); 1470b57cec5SDimitry Andric // Now we can erase the copy. 1480b57cec5SDimitry Andric Copies.erase(I); 1490b57cec5SDimitry Andric } 1500b57cec5SDimitry Andric } 1510b57cec5SDimitry Andric } 1520b57cec5SDimitry Andric 1530b57cec5SDimitry Andric /// Add this copy's registers into the tracker's copy maps. 1540b57cec5SDimitry Andric void trackCopy(MachineInstr *MI, const TargetRegisterInfo &TRI) { 1550b57cec5SDimitry Andric assert(MI->isCopy() && "Tracking non-copy?"); 1560b57cec5SDimitry Andric 1578bcb0991SDimitry Andric Register Def = MI->getOperand(0).getReg(); 1588bcb0991SDimitry Andric Register Src = MI->getOperand(1).getReg(); 1590b57cec5SDimitry Andric 1600b57cec5SDimitry Andric // Remember Def is defined by the copy. 1610b57cec5SDimitry Andric for (MCRegUnitIterator RUI(Def, &TRI); RUI.isValid(); ++RUI) 1620b57cec5SDimitry Andric Copies[*RUI] = {MI, {}, true}; 1630b57cec5SDimitry Andric 1640b57cec5SDimitry Andric // Remember source that's copied to Def. Once it's clobbered, then 1650b57cec5SDimitry Andric // it's no longer available for copy propagation. 1660b57cec5SDimitry Andric for (MCRegUnitIterator RUI(Src, &TRI); RUI.isValid(); ++RUI) { 1670b57cec5SDimitry Andric auto I = Copies.insert({*RUI, {nullptr, {}, false}}); 1680b57cec5SDimitry Andric auto &Copy = I.first->second; 1690b57cec5SDimitry Andric if (!is_contained(Copy.DefRegs, Def)) 1700b57cec5SDimitry Andric Copy.DefRegs.push_back(Def); 1710b57cec5SDimitry Andric } 1720b57cec5SDimitry Andric } 1730b57cec5SDimitry Andric 1740b57cec5SDimitry Andric bool hasAnyCopies() { 1750b57cec5SDimitry Andric return !Copies.empty(); 1760b57cec5SDimitry Andric } 1770b57cec5SDimitry Andric 1780b57cec5SDimitry Andric MachineInstr *findCopyForUnit(unsigned RegUnit, const TargetRegisterInfo &TRI, 1790b57cec5SDimitry Andric bool MustBeAvailable = false) { 1800b57cec5SDimitry Andric auto CI = Copies.find(RegUnit); 1810b57cec5SDimitry Andric if (CI == Copies.end()) 1820b57cec5SDimitry Andric return nullptr; 1830b57cec5SDimitry Andric if (MustBeAvailable && !CI->second.Avail) 1840b57cec5SDimitry Andric return nullptr; 1850b57cec5SDimitry Andric return CI->second.MI; 1860b57cec5SDimitry Andric } 1870b57cec5SDimitry Andric 188480093f4SDimitry Andric MachineInstr *findCopyDefViaUnit(unsigned RegUnit, 189480093f4SDimitry Andric const TargetRegisterInfo &TRI) { 190480093f4SDimitry Andric auto CI = Copies.find(RegUnit); 191480093f4SDimitry Andric if (CI == Copies.end()) 192480093f4SDimitry Andric return nullptr; 193480093f4SDimitry Andric if (CI->second.DefRegs.size() != 1) 194480093f4SDimitry Andric return nullptr; 195480093f4SDimitry Andric MCRegUnitIterator RUI(CI->second.DefRegs[0], &TRI); 196480093f4SDimitry Andric return findCopyForUnit(*RUI, TRI, true); 197480093f4SDimitry Andric } 198480093f4SDimitry Andric 199480093f4SDimitry Andric MachineInstr *findAvailBackwardCopy(MachineInstr &I, unsigned Reg, 200480093f4SDimitry Andric const TargetRegisterInfo &TRI) { 201480093f4SDimitry Andric MCRegUnitIterator RUI(Reg, &TRI); 202480093f4SDimitry Andric MachineInstr *AvailCopy = findCopyDefViaUnit(*RUI, TRI); 203480093f4SDimitry Andric if (!AvailCopy || 204480093f4SDimitry Andric !TRI.isSubRegisterEq(AvailCopy->getOperand(1).getReg(), Reg)) 205480093f4SDimitry Andric return nullptr; 206480093f4SDimitry Andric 207480093f4SDimitry Andric Register AvailSrc = AvailCopy->getOperand(1).getReg(); 208480093f4SDimitry Andric Register AvailDef = AvailCopy->getOperand(0).getReg(); 209480093f4SDimitry Andric for (const MachineInstr &MI : 210480093f4SDimitry Andric make_range(AvailCopy->getReverseIterator(), I.getReverseIterator())) 211480093f4SDimitry Andric for (const MachineOperand &MO : MI.operands()) 212480093f4SDimitry Andric if (MO.isRegMask()) 213480093f4SDimitry Andric // FIXME: Shall we simultaneously invalidate AvailSrc or AvailDef? 214480093f4SDimitry Andric if (MO.clobbersPhysReg(AvailSrc) || MO.clobbersPhysReg(AvailDef)) 215480093f4SDimitry Andric return nullptr; 216480093f4SDimitry Andric 217480093f4SDimitry Andric return AvailCopy; 218480093f4SDimitry Andric } 219480093f4SDimitry Andric 2200b57cec5SDimitry Andric MachineInstr *findAvailCopy(MachineInstr &DestCopy, unsigned Reg, 2210b57cec5SDimitry Andric const TargetRegisterInfo &TRI) { 2220b57cec5SDimitry Andric // We check the first RegUnit here, since we'll only be interested in the 2230b57cec5SDimitry Andric // copy if it copies the entire register anyway. 2240b57cec5SDimitry Andric MCRegUnitIterator RUI(Reg, &TRI); 2250b57cec5SDimitry Andric MachineInstr *AvailCopy = 2260b57cec5SDimitry Andric findCopyForUnit(*RUI, TRI, /*MustBeAvailable=*/true); 2270b57cec5SDimitry Andric if (!AvailCopy || 2280b57cec5SDimitry Andric !TRI.isSubRegisterEq(AvailCopy->getOperand(0).getReg(), Reg)) 2290b57cec5SDimitry Andric return nullptr; 2300b57cec5SDimitry Andric 2310b57cec5SDimitry Andric // Check that the available copy isn't clobbered by any regmasks between 2320b57cec5SDimitry Andric // itself and the destination. 2338bcb0991SDimitry Andric Register AvailSrc = AvailCopy->getOperand(1).getReg(); 2348bcb0991SDimitry Andric Register AvailDef = AvailCopy->getOperand(0).getReg(); 2350b57cec5SDimitry Andric for (const MachineInstr &MI : 2360b57cec5SDimitry Andric make_range(AvailCopy->getIterator(), DestCopy.getIterator())) 2370b57cec5SDimitry Andric for (const MachineOperand &MO : MI.operands()) 2380b57cec5SDimitry Andric if (MO.isRegMask()) 2390b57cec5SDimitry Andric if (MO.clobbersPhysReg(AvailSrc) || MO.clobbersPhysReg(AvailDef)) 2400b57cec5SDimitry Andric return nullptr; 2410b57cec5SDimitry Andric 2420b57cec5SDimitry Andric return AvailCopy; 2430b57cec5SDimitry Andric } 2440b57cec5SDimitry Andric 2450b57cec5SDimitry Andric void clear() { 2460b57cec5SDimitry Andric Copies.clear(); 2470b57cec5SDimitry Andric } 2480b57cec5SDimitry Andric }; 2490b57cec5SDimitry Andric 2500b57cec5SDimitry Andric class MachineCopyPropagation : public MachineFunctionPass { 2510b57cec5SDimitry Andric const TargetRegisterInfo *TRI; 2520b57cec5SDimitry Andric const TargetInstrInfo *TII; 2530b57cec5SDimitry Andric const MachineRegisterInfo *MRI; 2540b57cec5SDimitry Andric 2550b57cec5SDimitry Andric public: 2560b57cec5SDimitry Andric static char ID; // Pass identification, replacement for typeid 2570b57cec5SDimitry Andric 2580b57cec5SDimitry Andric MachineCopyPropagation() : MachineFunctionPass(ID) { 2590b57cec5SDimitry Andric initializeMachineCopyPropagationPass(*PassRegistry::getPassRegistry()); 2600b57cec5SDimitry Andric } 2610b57cec5SDimitry Andric 2620b57cec5SDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override { 2630b57cec5SDimitry Andric AU.setPreservesCFG(); 2640b57cec5SDimitry Andric MachineFunctionPass::getAnalysisUsage(AU); 2650b57cec5SDimitry Andric } 2660b57cec5SDimitry Andric 2670b57cec5SDimitry Andric bool runOnMachineFunction(MachineFunction &MF) override; 2680b57cec5SDimitry Andric 2690b57cec5SDimitry Andric MachineFunctionProperties getRequiredProperties() const override { 2700b57cec5SDimitry Andric return MachineFunctionProperties().set( 2710b57cec5SDimitry Andric MachineFunctionProperties::Property::NoVRegs); 2720b57cec5SDimitry Andric } 2730b57cec5SDimitry Andric 2740b57cec5SDimitry Andric private: 2758bcb0991SDimitry Andric typedef enum { DebugUse = false, RegularUse = true } DebugType; 2768bcb0991SDimitry Andric 2770b57cec5SDimitry Andric void ClobberRegister(unsigned Reg); 2788bcb0991SDimitry Andric void ReadRegister(unsigned Reg, MachineInstr &Reader, 2798bcb0991SDimitry Andric DebugType DT); 280480093f4SDimitry Andric void ForwardCopyPropagateBlock(MachineBasicBlock &MBB); 281480093f4SDimitry Andric void BackwardCopyPropagateBlock(MachineBasicBlock &MBB); 2820b57cec5SDimitry Andric bool eraseIfRedundant(MachineInstr &Copy, unsigned Src, unsigned Def); 2830b57cec5SDimitry Andric void forwardUses(MachineInstr &MI); 284480093f4SDimitry Andric void propagateDefs(MachineInstr &MI); 2850b57cec5SDimitry Andric bool isForwardableRegClassCopy(const MachineInstr &Copy, 2860b57cec5SDimitry Andric const MachineInstr &UseI, unsigned UseIdx); 287480093f4SDimitry Andric bool isBackwardPropagatableRegClassCopy(const MachineInstr &Copy, 288480093f4SDimitry Andric const MachineInstr &UseI, 289480093f4SDimitry Andric unsigned UseIdx); 2900b57cec5SDimitry Andric bool hasImplicitOverlap(const MachineInstr &MI, const MachineOperand &Use); 2910b57cec5SDimitry Andric 2920b57cec5SDimitry Andric /// Candidates for deletion. 2930b57cec5SDimitry Andric SmallSetVector<MachineInstr *, 8> MaybeDeadCopies; 2940b57cec5SDimitry Andric 2958bcb0991SDimitry Andric /// Multimap tracking debug users in current BB 2968bcb0991SDimitry Andric DenseMap<MachineInstr*, SmallVector<MachineInstr*, 2>> CopyDbgUsers; 2978bcb0991SDimitry Andric 2980b57cec5SDimitry Andric CopyTracker Tracker; 2990b57cec5SDimitry Andric 3000b57cec5SDimitry Andric bool Changed; 3010b57cec5SDimitry Andric }; 3020b57cec5SDimitry Andric 3030b57cec5SDimitry Andric } // end anonymous namespace 3040b57cec5SDimitry Andric 3050b57cec5SDimitry Andric char MachineCopyPropagation::ID = 0; 3060b57cec5SDimitry Andric 3070b57cec5SDimitry Andric char &llvm::MachineCopyPropagationID = MachineCopyPropagation::ID; 3080b57cec5SDimitry Andric 3090b57cec5SDimitry Andric INITIALIZE_PASS(MachineCopyPropagation, DEBUG_TYPE, 3100b57cec5SDimitry Andric "Machine Copy Propagation Pass", false, false) 3110b57cec5SDimitry Andric 3128bcb0991SDimitry Andric void MachineCopyPropagation::ReadRegister(unsigned Reg, MachineInstr &Reader, 3138bcb0991SDimitry Andric DebugType DT) { 3140b57cec5SDimitry Andric // If 'Reg' is defined by a copy, the copy is no longer a candidate 3158bcb0991SDimitry Andric // for elimination. If a copy is "read" by a debug user, record the user 3168bcb0991SDimitry Andric // for propagation. 3170b57cec5SDimitry Andric for (MCRegUnitIterator RUI(Reg, TRI); RUI.isValid(); ++RUI) { 3180b57cec5SDimitry Andric if (MachineInstr *Copy = Tracker.findCopyForUnit(*RUI, *TRI)) { 3198bcb0991SDimitry Andric if (DT == RegularUse) { 3200b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "MCP: Copy is used - not dead: "; Copy->dump()); 3210b57cec5SDimitry Andric MaybeDeadCopies.remove(Copy); 3228bcb0991SDimitry Andric } else { 3238bcb0991SDimitry Andric CopyDbgUsers[Copy].push_back(&Reader); 3248bcb0991SDimitry Andric } 3250b57cec5SDimitry Andric } 3260b57cec5SDimitry Andric } 3270b57cec5SDimitry Andric } 3280b57cec5SDimitry Andric 3290b57cec5SDimitry Andric /// Return true if \p PreviousCopy did copy register \p Src to register \p Def. 3300b57cec5SDimitry Andric /// This fact may have been obscured by sub register usage or may not be true at 3310b57cec5SDimitry Andric /// all even though Src and Def are subregisters of the registers used in 3320b57cec5SDimitry Andric /// PreviousCopy. e.g. 3330b57cec5SDimitry Andric /// isNopCopy("ecx = COPY eax", AX, CX) == true 3340b57cec5SDimitry Andric /// isNopCopy("ecx = COPY eax", AH, CL) == false 3350b57cec5SDimitry Andric static bool isNopCopy(const MachineInstr &PreviousCopy, unsigned Src, 3360b57cec5SDimitry Andric unsigned Def, const TargetRegisterInfo *TRI) { 3378bcb0991SDimitry Andric Register PreviousSrc = PreviousCopy.getOperand(1).getReg(); 3388bcb0991SDimitry Andric Register PreviousDef = PreviousCopy.getOperand(0).getReg(); 339*16d6b3b3SDimitry Andric if (Src == PreviousSrc && Def == PreviousDef) 3400b57cec5SDimitry Andric return true; 3410b57cec5SDimitry Andric if (!TRI->isSubRegister(PreviousSrc, Src)) 3420b57cec5SDimitry Andric return false; 3430b57cec5SDimitry Andric unsigned SubIdx = TRI->getSubRegIndex(PreviousSrc, Src); 3440b57cec5SDimitry Andric return SubIdx == TRI->getSubRegIndex(PreviousDef, Def); 3450b57cec5SDimitry Andric } 3460b57cec5SDimitry Andric 3470b57cec5SDimitry Andric /// Remove instruction \p Copy if there exists a previous copy that copies the 3480b57cec5SDimitry Andric /// register \p Src to the register \p Def; This may happen indirectly by 3490b57cec5SDimitry Andric /// copying the super registers. 3500b57cec5SDimitry Andric bool MachineCopyPropagation::eraseIfRedundant(MachineInstr &Copy, unsigned Src, 3510b57cec5SDimitry Andric unsigned Def) { 3520b57cec5SDimitry Andric // Avoid eliminating a copy from/to a reserved registers as we cannot predict 3530b57cec5SDimitry Andric // the value (Example: The sparc zero register is writable but stays zero). 3540b57cec5SDimitry Andric if (MRI->isReserved(Src) || MRI->isReserved(Def)) 3550b57cec5SDimitry Andric return false; 3560b57cec5SDimitry Andric 3570b57cec5SDimitry Andric // Search for an existing copy. 3580b57cec5SDimitry Andric MachineInstr *PrevCopy = Tracker.findAvailCopy(Copy, Def, *TRI); 3590b57cec5SDimitry Andric if (!PrevCopy) 3600b57cec5SDimitry Andric return false; 3610b57cec5SDimitry Andric 3620b57cec5SDimitry Andric // Check that the existing copy uses the correct sub registers. 3630b57cec5SDimitry Andric if (PrevCopy->getOperand(0).isDead()) 3640b57cec5SDimitry Andric return false; 3650b57cec5SDimitry Andric if (!isNopCopy(*PrevCopy, Src, Def, TRI)) 3660b57cec5SDimitry Andric return false; 3670b57cec5SDimitry Andric 3680b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "MCP: copy is a NOP, removing: "; Copy.dump()); 3690b57cec5SDimitry Andric 3700b57cec5SDimitry Andric // Copy was redundantly redefining either Src or Def. Remove earlier kill 3710b57cec5SDimitry Andric // flags between Copy and PrevCopy because the value will be reused now. 3720b57cec5SDimitry Andric assert(Copy.isCopy()); 3738bcb0991SDimitry Andric Register CopyDef = Copy.getOperand(0).getReg(); 3740b57cec5SDimitry Andric assert(CopyDef == Src || CopyDef == Def); 3750b57cec5SDimitry Andric for (MachineInstr &MI : 3760b57cec5SDimitry Andric make_range(PrevCopy->getIterator(), Copy.getIterator())) 3770b57cec5SDimitry Andric MI.clearRegisterKills(CopyDef, TRI); 3780b57cec5SDimitry Andric 3790b57cec5SDimitry Andric Copy.eraseFromParent(); 3800b57cec5SDimitry Andric Changed = true; 3810b57cec5SDimitry Andric ++NumDeletes; 3820b57cec5SDimitry Andric return true; 3830b57cec5SDimitry Andric } 3840b57cec5SDimitry Andric 385480093f4SDimitry Andric bool MachineCopyPropagation::isBackwardPropagatableRegClassCopy( 386480093f4SDimitry Andric const MachineInstr &Copy, const MachineInstr &UseI, unsigned UseIdx) { 387480093f4SDimitry Andric Register Def = Copy.getOperand(0).getReg(); 388480093f4SDimitry Andric 389480093f4SDimitry Andric if (const TargetRegisterClass *URC = 390480093f4SDimitry Andric UseI.getRegClassConstraint(UseIdx, TII, TRI)) 391480093f4SDimitry Andric return URC->contains(Def); 392480093f4SDimitry Andric 393480093f4SDimitry Andric // We don't process further if UseI is a COPY, since forward copy propagation 394480093f4SDimitry Andric // should handle that. 395480093f4SDimitry Andric return false; 396480093f4SDimitry Andric } 397480093f4SDimitry Andric 3980b57cec5SDimitry Andric /// Decide whether we should forward the source of \param Copy to its use in 3990b57cec5SDimitry Andric /// \param UseI based on the physical register class constraints of the opcode 4000b57cec5SDimitry Andric /// and avoiding introducing more cross-class COPYs. 4010b57cec5SDimitry Andric bool MachineCopyPropagation::isForwardableRegClassCopy(const MachineInstr &Copy, 4020b57cec5SDimitry Andric const MachineInstr &UseI, 4030b57cec5SDimitry Andric unsigned UseIdx) { 4040b57cec5SDimitry Andric 4058bcb0991SDimitry Andric Register CopySrcReg = Copy.getOperand(1).getReg(); 4060b57cec5SDimitry Andric 4070b57cec5SDimitry Andric // If the new register meets the opcode register constraints, then allow 4080b57cec5SDimitry Andric // forwarding. 4090b57cec5SDimitry Andric if (const TargetRegisterClass *URC = 4100b57cec5SDimitry Andric UseI.getRegClassConstraint(UseIdx, TII, TRI)) 4110b57cec5SDimitry Andric return URC->contains(CopySrcReg); 4120b57cec5SDimitry Andric 4130b57cec5SDimitry Andric if (!UseI.isCopy()) 4140b57cec5SDimitry Andric return false; 4150b57cec5SDimitry Andric 4160b57cec5SDimitry Andric /// COPYs don't have register class constraints, so if the user instruction 4170b57cec5SDimitry Andric /// is a COPY, we just try to avoid introducing additional cross-class 4180b57cec5SDimitry Andric /// COPYs. For example: 4190b57cec5SDimitry Andric /// 4200b57cec5SDimitry Andric /// RegClassA = COPY RegClassB // Copy parameter 4210b57cec5SDimitry Andric /// ... 4220b57cec5SDimitry Andric /// RegClassB = COPY RegClassA // UseI parameter 4230b57cec5SDimitry Andric /// 4240b57cec5SDimitry Andric /// which after forwarding becomes 4250b57cec5SDimitry Andric /// 4260b57cec5SDimitry Andric /// RegClassA = COPY RegClassB 4270b57cec5SDimitry Andric /// ... 4280b57cec5SDimitry Andric /// RegClassB = COPY RegClassB 4290b57cec5SDimitry Andric /// 4300b57cec5SDimitry Andric /// so we have reduced the number of cross-class COPYs and potentially 4310b57cec5SDimitry Andric /// introduced a nop COPY that can be removed. 4320b57cec5SDimitry Andric const TargetRegisterClass *UseDstRC = 4330b57cec5SDimitry Andric TRI->getMinimalPhysRegClass(UseI.getOperand(0).getReg()); 4340b57cec5SDimitry Andric 4350b57cec5SDimitry Andric const TargetRegisterClass *SuperRC = UseDstRC; 4360b57cec5SDimitry Andric for (TargetRegisterClass::sc_iterator SuperRCI = UseDstRC->getSuperClasses(); 4370b57cec5SDimitry Andric SuperRC; SuperRC = *SuperRCI++) 4380b57cec5SDimitry Andric if (SuperRC->contains(CopySrcReg)) 4390b57cec5SDimitry Andric return true; 4400b57cec5SDimitry Andric 4410b57cec5SDimitry Andric return false; 4420b57cec5SDimitry Andric } 4430b57cec5SDimitry Andric 4440b57cec5SDimitry Andric /// Check that \p MI does not have implicit uses that overlap with it's \p Use 4450b57cec5SDimitry Andric /// operand (the register being replaced), since these can sometimes be 4460b57cec5SDimitry Andric /// implicitly tied to other operands. For example, on AMDGPU: 4470b57cec5SDimitry Andric /// 4480b57cec5SDimitry Andric /// V_MOVRELS_B32_e32 %VGPR2, %M0<imp-use>, %EXEC<imp-use>, %VGPR2_VGPR3_VGPR4_VGPR5<imp-use> 4490b57cec5SDimitry Andric /// 4500b57cec5SDimitry Andric /// the %VGPR2 is implicitly tied to the larger reg operand, but we have no 4510b57cec5SDimitry Andric /// way of knowing we need to update the latter when updating the former. 4520b57cec5SDimitry Andric bool MachineCopyPropagation::hasImplicitOverlap(const MachineInstr &MI, 4530b57cec5SDimitry Andric const MachineOperand &Use) { 4540b57cec5SDimitry Andric for (const MachineOperand &MIUse : MI.uses()) 4550b57cec5SDimitry Andric if (&MIUse != &Use && MIUse.isReg() && MIUse.isImplicit() && 4560b57cec5SDimitry Andric MIUse.isUse() && TRI->regsOverlap(Use.getReg(), MIUse.getReg())) 4570b57cec5SDimitry Andric return true; 4580b57cec5SDimitry Andric 4590b57cec5SDimitry Andric return false; 4600b57cec5SDimitry Andric } 4610b57cec5SDimitry Andric 4620b57cec5SDimitry Andric /// Look for available copies whose destination register is used by \p MI and 4630b57cec5SDimitry Andric /// replace the use in \p MI with the copy's source register. 4640b57cec5SDimitry Andric void MachineCopyPropagation::forwardUses(MachineInstr &MI) { 4650b57cec5SDimitry Andric if (!Tracker.hasAnyCopies()) 4660b57cec5SDimitry Andric return; 4670b57cec5SDimitry Andric 4680b57cec5SDimitry Andric // Look for non-tied explicit vreg uses that have an active COPY 4690b57cec5SDimitry Andric // instruction that defines the physical register allocated to them. 4700b57cec5SDimitry Andric // Replace the vreg with the source of the active COPY. 4710b57cec5SDimitry Andric for (unsigned OpIdx = 0, OpEnd = MI.getNumOperands(); OpIdx < OpEnd; 4720b57cec5SDimitry Andric ++OpIdx) { 4730b57cec5SDimitry Andric MachineOperand &MOUse = MI.getOperand(OpIdx); 4740b57cec5SDimitry Andric // Don't forward into undef use operands since doing so can cause problems 4750b57cec5SDimitry Andric // with the machine verifier, since it doesn't treat undef reads as reads, 4760b57cec5SDimitry Andric // so we can end up with a live range that ends on an undef read, leading to 4770b57cec5SDimitry Andric // an error that the live range doesn't end on a read of the live range 4780b57cec5SDimitry Andric // register. 4790b57cec5SDimitry Andric if (!MOUse.isReg() || MOUse.isTied() || MOUse.isUndef() || MOUse.isDef() || 4800b57cec5SDimitry Andric MOUse.isImplicit()) 4810b57cec5SDimitry Andric continue; 4820b57cec5SDimitry Andric 4830b57cec5SDimitry Andric if (!MOUse.getReg()) 4840b57cec5SDimitry Andric continue; 4850b57cec5SDimitry Andric 4860b57cec5SDimitry Andric // Check that the register is marked 'renamable' so we know it is safe to 4870b57cec5SDimitry Andric // rename it without violating any constraints that aren't expressed in the 4880b57cec5SDimitry Andric // IR (e.g. ABI or opcode requirements). 4890b57cec5SDimitry Andric if (!MOUse.isRenamable()) 4900b57cec5SDimitry Andric continue; 4910b57cec5SDimitry Andric 4920b57cec5SDimitry Andric MachineInstr *Copy = Tracker.findAvailCopy(MI, MOUse.getReg(), *TRI); 4930b57cec5SDimitry Andric if (!Copy) 4940b57cec5SDimitry Andric continue; 4950b57cec5SDimitry Andric 4968bcb0991SDimitry Andric Register CopyDstReg = Copy->getOperand(0).getReg(); 4970b57cec5SDimitry Andric const MachineOperand &CopySrc = Copy->getOperand(1); 4988bcb0991SDimitry Andric Register CopySrcReg = CopySrc.getReg(); 4990b57cec5SDimitry Andric 5000b57cec5SDimitry Andric // FIXME: Don't handle partial uses of wider COPYs yet. 5010b57cec5SDimitry Andric if (MOUse.getReg() != CopyDstReg) { 5020b57cec5SDimitry Andric LLVM_DEBUG( 5030b57cec5SDimitry Andric dbgs() << "MCP: FIXME! Not forwarding COPY to sub-register use:\n " 5040b57cec5SDimitry Andric << MI); 5050b57cec5SDimitry Andric continue; 5060b57cec5SDimitry Andric } 5070b57cec5SDimitry Andric 5080b57cec5SDimitry Andric // Don't forward COPYs of reserved regs unless they are constant. 5090b57cec5SDimitry Andric if (MRI->isReserved(CopySrcReg) && !MRI->isConstantPhysReg(CopySrcReg)) 5100b57cec5SDimitry Andric continue; 5110b57cec5SDimitry Andric 5120b57cec5SDimitry Andric if (!isForwardableRegClassCopy(*Copy, MI, OpIdx)) 5130b57cec5SDimitry Andric continue; 5140b57cec5SDimitry Andric 5150b57cec5SDimitry Andric if (hasImplicitOverlap(MI, MOUse)) 5160b57cec5SDimitry Andric continue; 5170b57cec5SDimitry Andric 518480093f4SDimitry Andric // Check that the instruction is not a copy that partially overwrites the 519480093f4SDimitry Andric // original copy source that we are about to use. The tracker mechanism 520480093f4SDimitry Andric // cannot cope with that. 521480093f4SDimitry Andric if (MI.isCopy() && MI.modifiesRegister(CopySrcReg, TRI) && 522480093f4SDimitry Andric !MI.definesRegister(CopySrcReg)) { 523480093f4SDimitry Andric LLVM_DEBUG(dbgs() << "MCP: Copy source overlap with dest in " << MI); 524480093f4SDimitry Andric continue; 525480093f4SDimitry Andric } 526480093f4SDimitry Andric 5270b57cec5SDimitry Andric if (!DebugCounter::shouldExecute(FwdCounter)) { 5280b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "MCP: Skipping forwarding due to debug counter:\n " 5290b57cec5SDimitry Andric << MI); 5300b57cec5SDimitry Andric continue; 5310b57cec5SDimitry Andric } 5320b57cec5SDimitry Andric 5330b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "MCP: Replacing " << printReg(MOUse.getReg(), TRI) 5340b57cec5SDimitry Andric << "\n with " << printReg(CopySrcReg, TRI) 5350b57cec5SDimitry Andric << "\n in " << MI << " from " << *Copy); 5360b57cec5SDimitry Andric 5370b57cec5SDimitry Andric MOUse.setReg(CopySrcReg); 5380b57cec5SDimitry Andric if (!CopySrc.isRenamable()) 5390b57cec5SDimitry Andric MOUse.setIsRenamable(false); 5400b57cec5SDimitry Andric 5410b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "MCP: After replacement: " << MI << "\n"); 5420b57cec5SDimitry Andric 5430b57cec5SDimitry Andric // Clear kill markers that may have been invalidated. 5440b57cec5SDimitry Andric for (MachineInstr &KMI : 5450b57cec5SDimitry Andric make_range(Copy->getIterator(), std::next(MI.getIterator()))) 5460b57cec5SDimitry Andric KMI.clearRegisterKills(CopySrcReg, TRI); 5470b57cec5SDimitry Andric 5480b57cec5SDimitry Andric ++NumCopyForwards; 5490b57cec5SDimitry Andric Changed = true; 5500b57cec5SDimitry Andric } 5510b57cec5SDimitry Andric } 5520b57cec5SDimitry Andric 553480093f4SDimitry Andric void MachineCopyPropagation::ForwardCopyPropagateBlock(MachineBasicBlock &MBB) { 554480093f4SDimitry Andric LLVM_DEBUG(dbgs() << "MCP: ForwardCopyPropagateBlock " << MBB.getName() 555480093f4SDimitry Andric << "\n"); 5560b57cec5SDimitry Andric 5570b57cec5SDimitry Andric for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ) { 5580b57cec5SDimitry Andric MachineInstr *MI = &*I; 5590b57cec5SDimitry Andric ++I; 5600b57cec5SDimitry Andric 5610b57cec5SDimitry Andric // Analyze copies (which don't overlap themselves). 5620b57cec5SDimitry Andric if (MI->isCopy() && !TRI->regsOverlap(MI->getOperand(0).getReg(), 5630b57cec5SDimitry Andric MI->getOperand(1).getReg())) { 5648bcb0991SDimitry Andric Register Def = MI->getOperand(0).getReg(); 5658bcb0991SDimitry Andric Register Src = MI->getOperand(1).getReg(); 5660b57cec5SDimitry Andric 5678bcb0991SDimitry Andric assert(!Register::isVirtualRegister(Def) && 5688bcb0991SDimitry Andric !Register::isVirtualRegister(Src) && 5690b57cec5SDimitry Andric "MachineCopyPropagation should be run after register allocation!"); 5700b57cec5SDimitry Andric 5710b57cec5SDimitry Andric // The two copies cancel out and the source of the first copy 5720b57cec5SDimitry Andric // hasn't been overridden, eliminate the second one. e.g. 5730b57cec5SDimitry Andric // %ecx = COPY %eax 5740b57cec5SDimitry Andric // ... nothing clobbered eax. 5750b57cec5SDimitry Andric // %eax = COPY %ecx 5760b57cec5SDimitry Andric // => 5770b57cec5SDimitry Andric // %ecx = COPY %eax 5780b57cec5SDimitry Andric // 5790b57cec5SDimitry Andric // or 5800b57cec5SDimitry Andric // 5810b57cec5SDimitry Andric // %ecx = COPY %eax 5820b57cec5SDimitry Andric // ... nothing clobbered eax. 5830b57cec5SDimitry Andric // %ecx = COPY %eax 5840b57cec5SDimitry Andric // => 5850b57cec5SDimitry Andric // %ecx = COPY %eax 5860b57cec5SDimitry Andric if (eraseIfRedundant(*MI, Def, Src) || eraseIfRedundant(*MI, Src, Def)) 5870b57cec5SDimitry Andric continue; 5880b57cec5SDimitry Andric 5890b57cec5SDimitry Andric forwardUses(*MI); 5900b57cec5SDimitry Andric 5910b57cec5SDimitry Andric // Src may have been changed by forwardUses() 5920b57cec5SDimitry Andric Src = MI->getOperand(1).getReg(); 5930b57cec5SDimitry Andric 5940b57cec5SDimitry Andric // If Src is defined by a previous copy, the previous copy cannot be 5950b57cec5SDimitry Andric // eliminated. 5968bcb0991SDimitry Andric ReadRegister(Src, *MI, RegularUse); 5970b57cec5SDimitry Andric for (const MachineOperand &MO : MI->implicit_operands()) { 5980b57cec5SDimitry Andric if (!MO.isReg() || !MO.readsReg()) 5990b57cec5SDimitry Andric continue; 6008bcb0991SDimitry Andric Register Reg = MO.getReg(); 6010b57cec5SDimitry Andric if (!Reg) 6020b57cec5SDimitry Andric continue; 6038bcb0991SDimitry Andric ReadRegister(Reg, *MI, RegularUse); 6040b57cec5SDimitry Andric } 6050b57cec5SDimitry Andric 6060b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "MCP: Copy is a deletion candidate: "; MI->dump()); 6070b57cec5SDimitry Andric 6080b57cec5SDimitry Andric // Copy is now a candidate for deletion. 6090b57cec5SDimitry Andric if (!MRI->isReserved(Def)) 6100b57cec5SDimitry Andric MaybeDeadCopies.insert(MI); 6110b57cec5SDimitry Andric 6120b57cec5SDimitry Andric // If 'Def' is previously source of another copy, then this earlier copy's 6130b57cec5SDimitry Andric // source is no longer available. e.g. 6140b57cec5SDimitry Andric // %xmm9 = copy %xmm2 6150b57cec5SDimitry Andric // ... 6160b57cec5SDimitry Andric // %xmm2 = copy %xmm0 6170b57cec5SDimitry Andric // ... 6180b57cec5SDimitry Andric // %xmm2 = copy %xmm9 6190b57cec5SDimitry Andric Tracker.clobberRegister(Def, *TRI); 6200b57cec5SDimitry Andric for (const MachineOperand &MO : MI->implicit_operands()) { 6210b57cec5SDimitry Andric if (!MO.isReg() || !MO.isDef()) 6220b57cec5SDimitry Andric continue; 6238bcb0991SDimitry Andric Register Reg = MO.getReg(); 6240b57cec5SDimitry Andric if (!Reg) 6250b57cec5SDimitry Andric continue; 6260b57cec5SDimitry Andric Tracker.clobberRegister(Reg, *TRI); 6270b57cec5SDimitry Andric } 6280b57cec5SDimitry Andric 6290b57cec5SDimitry Andric Tracker.trackCopy(MI, *TRI); 6300b57cec5SDimitry Andric 6310b57cec5SDimitry Andric continue; 6320b57cec5SDimitry Andric } 6330b57cec5SDimitry Andric 6340b57cec5SDimitry Andric // Clobber any earlyclobber regs first. 6350b57cec5SDimitry Andric for (const MachineOperand &MO : MI->operands()) 6360b57cec5SDimitry Andric if (MO.isReg() && MO.isEarlyClobber()) { 6378bcb0991SDimitry Andric Register Reg = MO.getReg(); 6380b57cec5SDimitry Andric // If we have a tied earlyclobber, that means it is also read by this 6390b57cec5SDimitry Andric // instruction, so we need to make sure we don't remove it as dead 6400b57cec5SDimitry Andric // later. 6410b57cec5SDimitry Andric if (MO.isTied()) 6428bcb0991SDimitry Andric ReadRegister(Reg, *MI, RegularUse); 6430b57cec5SDimitry Andric Tracker.clobberRegister(Reg, *TRI); 6440b57cec5SDimitry Andric } 6450b57cec5SDimitry Andric 6460b57cec5SDimitry Andric forwardUses(*MI); 6470b57cec5SDimitry Andric 6480b57cec5SDimitry Andric // Not a copy. 6490b57cec5SDimitry Andric SmallVector<unsigned, 2> Defs; 6500b57cec5SDimitry Andric const MachineOperand *RegMask = nullptr; 6510b57cec5SDimitry Andric for (const MachineOperand &MO : MI->operands()) { 6520b57cec5SDimitry Andric if (MO.isRegMask()) 6530b57cec5SDimitry Andric RegMask = &MO; 6540b57cec5SDimitry Andric if (!MO.isReg()) 6550b57cec5SDimitry Andric continue; 6568bcb0991SDimitry Andric Register Reg = MO.getReg(); 6570b57cec5SDimitry Andric if (!Reg) 6580b57cec5SDimitry Andric continue; 6590b57cec5SDimitry Andric 6608bcb0991SDimitry Andric assert(!Register::isVirtualRegister(Reg) && 6610b57cec5SDimitry Andric "MachineCopyPropagation should be run after register allocation!"); 6620b57cec5SDimitry Andric 6630b57cec5SDimitry Andric if (MO.isDef() && !MO.isEarlyClobber()) { 6640b57cec5SDimitry Andric Defs.push_back(Reg); 6650b57cec5SDimitry Andric continue; 6668bcb0991SDimitry Andric } else if (MO.readsReg()) 6678bcb0991SDimitry Andric ReadRegister(Reg, *MI, MO.isDebug() ? DebugUse : RegularUse); 6680b57cec5SDimitry Andric } 6690b57cec5SDimitry Andric 6700b57cec5SDimitry Andric // The instruction has a register mask operand which means that it clobbers 6710b57cec5SDimitry Andric // a large set of registers. Treat clobbered registers the same way as 6720b57cec5SDimitry Andric // defined registers. 6730b57cec5SDimitry Andric if (RegMask) { 6740b57cec5SDimitry Andric // Erase any MaybeDeadCopies whose destination register is clobbered. 6750b57cec5SDimitry Andric for (SmallSetVector<MachineInstr *, 8>::iterator DI = 6760b57cec5SDimitry Andric MaybeDeadCopies.begin(); 6770b57cec5SDimitry Andric DI != MaybeDeadCopies.end();) { 6780b57cec5SDimitry Andric MachineInstr *MaybeDead = *DI; 6798bcb0991SDimitry Andric Register Reg = MaybeDead->getOperand(0).getReg(); 6800b57cec5SDimitry Andric assert(!MRI->isReserved(Reg)); 6810b57cec5SDimitry Andric 6820b57cec5SDimitry Andric if (!RegMask->clobbersPhysReg(Reg)) { 6830b57cec5SDimitry Andric ++DI; 6840b57cec5SDimitry Andric continue; 6850b57cec5SDimitry Andric } 6860b57cec5SDimitry Andric 6870b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "MCP: Removing copy due to regmask clobbering: "; 6880b57cec5SDimitry Andric MaybeDead->dump()); 6890b57cec5SDimitry Andric 6900b57cec5SDimitry Andric // Make sure we invalidate any entries in the copy maps before erasing 6910b57cec5SDimitry Andric // the instruction. 6920b57cec5SDimitry Andric Tracker.clobberRegister(Reg, *TRI); 6930b57cec5SDimitry Andric 6940b57cec5SDimitry Andric // erase() will return the next valid iterator pointing to the next 6950b57cec5SDimitry Andric // element after the erased one. 6960b57cec5SDimitry Andric DI = MaybeDeadCopies.erase(DI); 6970b57cec5SDimitry Andric MaybeDead->eraseFromParent(); 6980b57cec5SDimitry Andric Changed = true; 6990b57cec5SDimitry Andric ++NumDeletes; 7000b57cec5SDimitry Andric } 7010b57cec5SDimitry Andric } 7020b57cec5SDimitry Andric 7030b57cec5SDimitry Andric // Any previous copy definition or reading the Defs is no longer available. 7040b57cec5SDimitry Andric for (unsigned Reg : Defs) 7050b57cec5SDimitry Andric Tracker.clobberRegister(Reg, *TRI); 7060b57cec5SDimitry Andric } 7070b57cec5SDimitry Andric 7080b57cec5SDimitry Andric // If MBB doesn't have successors, delete the copies whose defs are not used. 7090b57cec5SDimitry Andric // If MBB does have successors, then conservative assume the defs are live-out 7100b57cec5SDimitry Andric // since we don't want to trust live-in lists. 7110b57cec5SDimitry Andric if (MBB.succ_empty()) { 7120b57cec5SDimitry Andric for (MachineInstr *MaybeDead : MaybeDeadCopies) { 7130b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "MCP: Removing copy due to no live-out succ: "; 7140b57cec5SDimitry Andric MaybeDead->dump()); 7150b57cec5SDimitry Andric assert(!MRI->isReserved(MaybeDead->getOperand(0).getReg())); 7160b57cec5SDimitry Andric 7178bcb0991SDimitry Andric // Update matching debug values, if any. 7180b57cec5SDimitry Andric assert(MaybeDead->isCopy()); 7198bcb0991SDimitry Andric unsigned SrcReg = MaybeDead->getOperand(1).getReg(); 7208bcb0991SDimitry Andric MRI->updateDbgUsersToReg(SrcReg, CopyDbgUsers[MaybeDead]); 7210b57cec5SDimitry Andric 7220b57cec5SDimitry Andric MaybeDead->eraseFromParent(); 7230b57cec5SDimitry Andric Changed = true; 7240b57cec5SDimitry Andric ++NumDeletes; 7250b57cec5SDimitry Andric } 7260b57cec5SDimitry Andric } 7270b57cec5SDimitry Andric 7280b57cec5SDimitry Andric MaybeDeadCopies.clear(); 7298bcb0991SDimitry Andric CopyDbgUsers.clear(); 7300b57cec5SDimitry Andric Tracker.clear(); 7310b57cec5SDimitry Andric } 7320b57cec5SDimitry Andric 733480093f4SDimitry Andric static bool isBackwardPropagatableCopy(MachineInstr &MI, 734480093f4SDimitry Andric const MachineRegisterInfo &MRI) { 735480093f4SDimitry Andric assert(MI.isCopy() && "MI is expected to be a COPY"); 736480093f4SDimitry Andric Register Def = MI.getOperand(0).getReg(); 737480093f4SDimitry Andric Register Src = MI.getOperand(1).getReg(); 738480093f4SDimitry Andric 739480093f4SDimitry Andric if (!Def || !Src) 740480093f4SDimitry Andric return false; 741480093f4SDimitry Andric 742480093f4SDimitry Andric if (MRI.isReserved(Def) || MRI.isReserved(Src)) 743480093f4SDimitry Andric return false; 744480093f4SDimitry Andric 745480093f4SDimitry Andric return MI.getOperand(1).isRenamable() && MI.getOperand(1).isKill(); 746480093f4SDimitry Andric } 747480093f4SDimitry Andric 748480093f4SDimitry Andric void MachineCopyPropagation::propagateDefs(MachineInstr &MI) { 749480093f4SDimitry Andric if (!Tracker.hasAnyCopies()) 750480093f4SDimitry Andric return; 751480093f4SDimitry Andric 752480093f4SDimitry Andric for (unsigned OpIdx = 0, OpEnd = MI.getNumOperands(); OpIdx != OpEnd; 753480093f4SDimitry Andric ++OpIdx) { 754480093f4SDimitry Andric MachineOperand &MODef = MI.getOperand(OpIdx); 755480093f4SDimitry Andric 756480093f4SDimitry Andric if (!MODef.isReg() || MODef.isUse()) 757480093f4SDimitry Andric continue; 758480093f4SDimitry Andric 759480093f4SDimitry Andric // Ignore non-trivial cases. 760480093f4SDimitry Andric if (MODef.isTied() || MODef.isUndef() || MODef.isImplicit()) 761480093f4SDimitry Andric continue; 762480093f4SDimitry Andric 763480093f4SDimitry Andric if (!MODef.getReg()) 764480093f4SDimitry Andric continue; 765480093f4SDimitry Andric 766480093f4SDimitry Andric // We only handle if the register comes from a vreg. 767480093f4SDimitry Andric if (!MODef.isRenamable()) 768480093f4SDimitry Andric continue; 769480093f4SDimitry Andric 770480093f4SDimitry Andric MachineInstr *Copy = 771480093f4SDimitry Andric Tracker.findAvailBackwardCopy(MI, MODef.getReg(), *TRI); 772480093f4SDimitry Andric if (!Copy) 773480093f4SDimitry Andric continue; 774480093f4SDimitry Andric 775480093f4SDimitry Andric Register Def = Copy->getOperand(0).getReg(); 776480093f4SDimitry Andric Register Src = Copy->getOperand(1).getReg(); 777480093f4SDimitry Andric 778480093f4SDimitry Andric if (MODef.getReg() != Src) 779480093f4SDimitry Andric continue; 780480093f4SDimitry Andric 781480093f4SDimitry Andric if (!isBackwardPropagatableRegClassCopy(*Copy, MI, OpIdx)) 782480093f4SDimitry Andric continue; 783480093f4SDimitry Andric 784480093f4SDimitry Andric if (hasImplicitOverlap(MI, MODef)) 785480093f4SDimitry Andric continue; 786480093f4SDimitry Andric 787480093f4SDimitry Andric LLVM_DEBUG(dbgs() << "MCP: Replacing " << printReg(MODef.getReg(), TRI) 788480093f4SDimitry Andric << "\n with " << printReg(Def, TRI) << "\n in " 789480093f4SDimitry Andric << MI << " from " << *Copy); 790480093f4SDimitry Andric 791480093f4SDimitry Andric MODef.setReg(Def); 792480093f4SDimitry Andric MODef.setIsRenamable(Copy->getOperand(0).isRenamable()); 793480093f4SDimitry Andric 794480093f4SDimitry Andric LLVM_DEBUG(dbgs() << "MCP: After replacement: " << MI << "\n"); 795480093f4SDimitry Andric MaybeDeadCopies.insert(Copy); 796480093f4SDimitry Andric Changed = true; 797480093f4SDimitry Andric ++NumCopyBackwardPropagated; 798480093f4SDimitry Andric } 799480093f4SDimitry Andric } 800480093f4SDimitry Andric 801480093f4SDimitry Andric void MachineCopyPropagation::BackwardCopyPropagateBlock( 802480093f4SDimitry Andric MachineBasicBlock &MBB) { 803480093f4SDimitry Andric LLVM_DEBUG(dbgs() << "MCP: BackwardCopyPropagateBlock " << MBB.getName() 804480093f4SDimitry Andric << "\n"); 805480093f4SDimitry Andric 806480093f4SDimitry Andric for (MachineBasicBlock::reverse_iterator I = MBB.rbegin(), E = MBB.rend(); 807480093f4SDimitry Andric I != E;) { 808480093f4SDimitry Andric MachineInstr *MI = &*I; 809480093f4SDimitry Andric ++I; 810480093f4SDimitry Andric 811480093f4SDimitry Andric // Ignore non-trivial COPYs. 812480093f4SDimitry Andric if (MI->isCopy() && MI->getNumOperands() == 2 && 813480093f4SDimitry Andric !TRI->regsOverlap(MI->getOperand(0).getReg(), 814480093f4SDimitry Andric MI->getOperand(1).getReg())) { 815480093f4SDimitry Andric 816480093f4SDimitry Andric Register Def = MI->getOperand(0).getReg(); 817480093f4SDimitry Andric Register Src = MI->getOperand(1).getReg(); 818480093f4SDimitry Andric 819480093f4SDimitry Andric // Unlike forward cp, we don't invoke propagateDefs here, 820480093f4SDimitry Andric // just let forward cp do COPY-to-COPY propagation. 821480093f4SDimitry Andric if (isBackwardPropagatableCopy(*MI, *MRI)) { 822480093f4SDimitry Andric Tracker.invalidateRegister(Src, *TRI); 823480093f4SDimitry Andric Tracker.invalidateRegister(Def, *TRI); 824480093f4SDimitry Andric Tracker.trackCopy(MI, *TRI); 825480093f4SDimitry Andric continue; 826480093f4SDimitry Andric } 827480093f4SDimitry Andric } 828480093f4SDimitry Andric 829480093f4SDimitry Andric // Invalidate any earlyclobber regs first. 830480093f4SDimitry Andric for (const MachineOperand &MO : MI->operands()) 831480093f4SDimitry Andric if (MO.isReg() && MO.isEarlyClobber()) { 832480093f4SDimitry Andric Register Reg = MO.getReg(); 833480093f4SDimitry Andric if (!Reg) 834480093f4SDimitry Andric continue; 835480093f4SDimitry Andric Tracker.invalidateRegister(Reg, *TRI); 836480093f4SDimitry Andric } 837480093f4SDimitry Andric 838480093f4SDimitry Andric propagateDefs(*MI); 839480093f4SDimitry Andric for (const MachineOperand &MO : MI->operands()) { 840480093f4SDimitry Andric if (!MO.isReg()) 841480093f4SDimitry Andric continue; 842480093f4SDimitry Andric 843480093f4SDimitry Andric if (!MO.getReg()) 844480093f4SDimitry Andric continue; 845480093f4SDimitry Andric 846480093f4SDimitry Andric if (MO.isDef()) 847480093f4SDimitry Andric Tracker.invalidateRegister(MO.getReg(), *TRI); 848480093f4SDimitry Andric 849480093f4SDimitry Andric if (MO.readsReg()) 850480093f4SDimitry Andric Tracker.invalidateRegister(MO.getReg(), *TRI); 851480093f4SDimitry Andric } 852480093f4SDimitry Andric } 853480093f4SDimitry Andric 854480093f4SDimitry Andric for (auto *Copy : MaybeDeadCopies) { 855480093f4SDimitry Andric Copy->eraseFromParent(); 856480093f4SDimitry Andric ++NumDeletes; 857480093f4SDimitry Andric } 858480093f4SDimitry Andric 859480093f4SDimitry Andric MaybeDeadCopies.clear(); 860480093f4SDimitry Andric CopyDbgUsers.clear(); 861480093f4SDimitry Andric Tracker.clear(); 862480093f4SDimitry Andric } 863480093f4SDimitry Andric 8640b57cec5SDimitry Andric bool MachineCopyPropagation::runOnMachineFunction(MachineFunction &MF) { 8650b57cec5SDimitry Andric if (skipFunction(MF.getFunction())) 8660b57cec5SDimitry Andric return false; 8670b57cec5SDimitry Andric 8680b57cec5SDimitry Andric Changed = false; 8690b57cec5SDimitry Andric 8700b57cec5SDimitry Andric TRI = MF.getSubtarget().getRegisterInfo(); 8710b57cec5SDimitry Andric TII = MF.getSubtarget().getInstrInfo(); 8720b57cec5SDimitry Andric MRI = &MF.getRegInfo(); 8730b57cec5SDimitry Andric 874480093f4SDimitry Andric for (MachineBasicBlock &MBB : MF) { 875480093f4SDimitry Andric BackwardCopyPropagateBlock(MBB); 876480093f4SDimitry Andric ForwardCopyPropagateBlock(MBB); 877480093f4SDimitry Andric } 8780b57cec5SDimitry Andric 8790b57cec5SDimitry Andric return Changed; 8800b57cec5SDimitry Andric } 881