Evan Cheng | 977679d | 2012-01-07 03:02:36 +0000 | [diff] [blame] | 1 | //===- MachineCopyPropagation.cpp - Machine Copy Propagation Pass ---------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Geoff Berry | c3ef7ae | 2017-10-03 16:59:13 +0000 | [diff] [blame] | 10 | // This is an extremely simple MachineInstr-level copy propagation pass. |
Evan Cheng | 977679d | 2012-01-07 03:02:36 +0000 | [diff] [blame] | 11 | // |
Geoff Berry | 1bfec90 | 2018-02-27 16:59:10 +0000 | [diff] [blame] | 12 | // This pass forwards the source of COPYs to the users of their destinations |
| 13 | // when doing so is legal. For example: |
| 14 | // |
| 15 | // %reg1 = COPY %reg0 |
| 16 | // ... |
| 17 | // ... = OP %reg1 |
| 18 | // |
| 19 | // If |
| 20 | // - %reg0 has not been clobbered by the time of the use of %reg1 |
| 21 | // - the register class constraints are satisfied |
| 22 | // - the COPY def is the only value that reaches OP |
| 23 | // then this pass replaces the above with: |
| 24 | // |
| 25 | // %reg1 = COPY %reg0 |
| 26 | // ... |
| 27 | // ... = OP %reg0 |
| 28 | // |
| 29 | // This pass also removes some redundant COPYs. For example: |
| 30 | // |
| 31 | // %R1 = COPY %R0 |
| 32 | // ... // No clobber of %R1 |
| 33 | // %R0 = COPY %R1 <<< Removed |
| 34 | // |
| 35 | // or |
| 36 | // |
| 37 | // %R1 = COPY %R0 |
| 38 | // ... // No clobber of %R0 |
| 39 | // %R1 = COPY %R0 <<< Removed |
| 40 | // |
Evan Cheng | 977679d | 2012-01-07 03:02:36 +0000 | [diff] [blame] | 41 | //===----------------------------------------------------------------------===// |
| 42 | |
Evan Cheng | 977679d | 2012-01-07 03:02:36 +0000 | [diff] [blame] | 43 | #include "llvm/ADT/DenseMap.h" |
Eugene Zelenko | 7cf6af5 | 2017-08-29 22:32:07 +0000 | [diff] [blame] | 44 | #include "llvm/ADT/STLExtras.h" |
Evan Cheng | 977679d | 2012-01-07 03:02:36 +0000 | [diff] [blame] | 45 | #include "llvm/ADT/SetVector.h" |
| 46 | #include "llvm/ADT/SmallVector.h" |
| 47 | #include "llvm/ADT/Statistic.h" |
Eugene Zelenko | 7cf6af5 | 2017-08-29 22:32:07 +0000 | [diff] [blame] | 48 | #include "llvm/ADT/iterator_range.h" |
| 49 | #include "llvm/CodeGen/MachineBasicBlock.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 50 | #include "llvm/CodeGen/MachineFunction.h" |
| 51 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Eugene Zelenko | 7cf6af5 | 2017-08-29 22:32:07 +0000 | [diff] [blame] | 52 | #include "llvm/CodeGen/MachineInstr.h" |
| 53 | #include "llvm/CodeGen/MachineOperand.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 54 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Geoff Berry | 1bfec90 | 2018-02-27 16:59:10 +0000 | [diff] [blame] | 55 | #include "llvm/CodeGen/TargetInstrInfo.h" |
David Blaikie | e3a9b4c | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 56 | #include "llvm/CodeGen/TargetRegisterInfo.h" |
| 57 | #include "llvm/CodeGen/TargetSubtargetInfo.h" |
Eugene Zelenko | 7cf6af5 | 2017-08-29 22:32:07 +0000 | [diff] [blame] | 58 | #include "llvm/MC/MCRegisterInfo.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 59 | #include "llvm/Pass.h" |
| 60 | #include "llvm/Support/Debug.h" |
Geoff Berry | 1bfec90 | 2018-02-27 16:59:10 +0000 | [diff] [blame] | 61 | #include "llvm/Support/DebugCounter.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 62 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | 7cf6af5 | 2017-08-29 22:32:07 +0000 | [diff] [blame] | 63 | #include <cassert> |
| 64 | #include <iterator> |
| 65 | |
Evan Cheng | 977679d | 2012-01-07 03:02:36 +0000 | [diff] [blame] | 66 | using namespace llvm; |
| 67 | |
Matthias Braun | 94c4904 | 2017-05-25 21:26:32 +0000 | [diff] [blame] | 68 | #define DEBUG_TYPE "machine-cp" |
Chandler Carruth | 8677f2f | 2014-04-22 02:02:50 +0000 | [diff] [blame] | 69 | |
Evan Cheng | 977679d | 2012-01-07 03:02:36 +0000 | [diff] [blame] | 70 | STATISTIC(NumDeletes, "Number of dead copies deleted"); |
Geoff Berry | 1bfec90 | 2018-02-27 16:59:10 +0000 | [diff] [blame] | 71 | STATISTIC(NumCopyForwards, "Number of copy uses forwarded"); |
| 72 | DEBUG_COUNTER(FwdCounter, "machine-cp-fwd", |
| 73 | "Controls which register COPYs are forwarded"); |
Evan Cheng | 977679d | 2012-01-07 03:02:36 +0000 | [diff] [blame] | 74 | |
| 75 | namespace { |
Eugene Zelenko | 7cf6af5 | 2017-08-29 22:32:07 +0000 | [diff] [blame] | 76 | |
Justin Bogner | 97eaa64 | 2018-09-21 00:51:04 +0000 | [diff] [blame] | 77 | class CopyTracker { |
Justin Bogner | db46784 | 2018-10-22 19:51:31 +0000 | [diff] [blame] | 78 | struct CopyInfo { |
| 79 | MachineInstr *MI; |
| 80 | SmallVector<unsigned, 4> DefRegs; |
| 81 | bool Avail; |
| 82 | }; |
Justin Bogner | 97eaa64 | 2018-09-21 00:51:04 +0000 | [diff] [blame] | 83 | |
Justin Bogner | db46784 | 2018-10-22 19:51:31 +0000 | [diff] [blame] | 84 | DenseMap<unsigned, CopyInfo> Copies; |
Justin Bogner | 97eaa64 | 2018-09-21 00:51:04 +0000 | [diff] [blame] | 85 | |
| 86 | public: |
| 87 | /// Mark all of the given registers and their subregisters as unavailable for |
| 88 | /// copying. |
Justin Bogner | db46784 | 2018-10-22 19:51:31 +0000 | [diff] [blame] | 89 | void markRegsUnavailable(ArrayRef<unsigned> Regs, |
| 90 | const TargetRegisterInfo &TRI) { |
Justin Bogner | 97eaa64 | 2018-09-21 00:51:04 +0000 | [diff] [blame] | 91 | for (unsigned Reg : Regs) { |
| 92 | // Source of copy is no longer available for propagation. |
Justin Bogner | db46784 | 2018-10-22 19:51:31 +0000 | [diff] [blame] | 93 | for (MCRegUnitIterator RUI(Reg, &TRI); RUI.isValid(); ++RUI) { |
| 94 | auto CI = Copies.find(*RUI); |
| 95 | if (CI != Copies.end()) |
| 96 | CI->second.Avail = false; |
| 97 | } |
Justin Bogner | 97eaa64 | 2018-09-21 00:51:04 +0000 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | |
Justin Bogner | 97eaa64 | 2018-09-21 00:51:04 +0000 | [diff] [blame] | 101 | /// Clobber a single register, removing it from the tracker's copy maps. |
| 102 | void clobberRegister(unsigned Reg, const TargetRegisterInfo &TRI) { |
Justin Bogner | db46784 | 2018-10-22 19:51:31 +0000 | [diff] [blame] | 103 | for (MCRegUnitIterator RUI(Reg, &TRI); RUI.isValid(); ++RUI) { |
| 104 | auto I = Copies.find(*RUI); |
| 105 | if (I != Copies.end()) { |
| 106 | // When we clobber the source of a copy, we need to clobber everything |
| 107 | // it defined. |
| 108 | markRegsUnavailable(I->second.DefRegs, TRI); |
| 109 | // When we clobber the destination of a copy, we need to clobber the |
| 110 | // whole register it defined. |
| 111 | if (MachineInstr *MI = I->second.MI) |
| 112 | markRegsUnavailable({MI->getOperand(0).getReg()}, TRI); |
| 113 | // Now we can erase the copy. |
| 114 | Copies.erase(I); |
Justin Bogner | 97eaa64 | 2018-09-21 00:51:04 +0000 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | /// Add this copy's registers into the tracker's copy maps. |
Justin Bogner | db46784 | 2018-10-22 19:51:31 +0000 | [diff] [blame] | 120 | void trackCopy(MachineInstr *MI, const TargetRegisterInfo &TRI) { |
| 121 | assert(MI->isCopy() && "Tracking non-copy?"); |
Justin Bogner | 97eaa64 | 2018-09-21 00:51:04 +0000 | [diff] [blame] | 122 | |
Justin Bogner | db46784 | 2018-10-22 19:51:31 +0000 | [diff] [blame] | 123 | unsigned Def = MI->getOperand(0).getReg(); |
| 124 | unsigned Src = MI->getOperand(1).getReg(); |
Justin Bogner | 97eaa64 | 2018-09-21 00:51:04 +0000 | [diff] [blame] | 125 | |
| 126 | // Remember Def is defined by the copy. |
Justin Bogner | db46784 | 2018-10-22 19:51:31 +0000 | [diff] [blame] | 127 | for (MCRegUnitIterator RUI(Def, &TRI); RUI.isValid(); ++RUI) |
| 128 | Copies[*RUI] = {MI, {}, true}; |
Justin Bogner | 97eaa64 | 2018-09-21 00:51:04 +0000 | [diff] [blame] | 129 | |
| 130 | // Remember source that's copied to Def. Once it's clobbered, then |
| 131 | // it's no longer available for copy propagation. |
Justin Bogner | db46784 | 2018-10-22 19:51:31 +0000 | [diff] [blame] | 132 | for (MCRegUnitIterator RUI(Src, &TRI); RUI.isValid(); ++RUI) { |
| 133 | auto I = Copies.insert({*RUI, {nullptr, {}, false}}); |
| 134 | auto &Copy = I.first->second; |
| 135 | if (!is_contained(Copy.DefRegs, Def)) |
| 136 | Copy.DefRegs.push_back(Def); |
| 137 | } |
Justin Bogner | 97eaa64 | 2018-09-21 00:51:04 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Justin Bogner | db46784 | 2018-10-22 19:51:31 +0000 | [diff] [blame] | 140 | bool hasAnyCopies() { |
| 141 | return !Copies.empty(); |
| 142 | } |
Justin Bogner | 97eaa64 | 2018-09-21 00:51:04 +0000 | [diff] [blame] | 143 | |
Justin Bogner | db46784 | 2018-10-22 19:51:31 +0000 | [diff] [blame] | 144 | MachineInstr *findCopyForUnit(unsigned RegUnit, const TargetRegisterInfo &TRI, |
| 145 | bool MustBeAvailable = false) { |
| 146 | auto CI = Copies.find(RegUnit); |
| 147 | if (CI == Copies.end()) |
Justin Bogner | 60a5c77 | 2018-09-25 04:45:25 +0000 | [diff] [blame] | 148 | return nullptr; |
Justin Bogner | db46784 | 2018-10-22 19:51:31 +0000 | [diff] [blame] | 149 | if (MustBeAvailable && !CI->second.Avail) |
| 150 | return nullptr; |
| 151 | return CI->second.MI; |
| 152 | } |
| 153 | |
| 154 | MachineInstr *findAvailCopy(MachineInstr &DestCopy, unsigned Reg, |
| 155 | const TargetRegisterInfo &TRI) { |
| 156 | // We check the first RegUnit here, since we'll only be interested in the |
| 157 | // copy if it copies the entire register anyway. |
| 158 | MCRegUnitIterator RUI(Reg, &TRI); |
| 159 | MachineInstr *AvailCopy = |
| 160 | findCopyForUnit(*RUI, TRI, /*MustBeAvailable=*/true); |
| 161 | if (!AvailCopy || |
| 162 | !TRI.isSubRegisterEq(AvailCopy->getOperand(0).getReg(), Reg)) |
| 163 | return nullptr; |
Justin Bogner | 60a5c77 | 2018-09-25 04:45:25 +0000 | [diff] [blame] | 164 | |
| 165 | // Check that the available copy isn't clobbered by any regmasks between |
| 166 | // itself and the destination. |
Justin Bogner | db46784 | 2018-10-22 19:51:31 +0000 | [diff] [blame] | 167 | unsigned AvailSrc = AvailCopy->getOperand(1).getReg(); |
| 168 | unsigned AvailDef = AvailCopy->getOperand(0).getReg(); |
Justin Bogner | 60a5c77 | 2018-09-25 04:45:25 +0000 | [diff] [blame] | 169 | for (const MachineInstr &MI : |
Justin Bogner | db46784 | 2018-10-22 19:51:31 +0000 | [diff] [blame] | 170 | make_range(AvailCopy->getIterator(), DestCopy.getIterator())) |
Justin Bogner | 60a5c77 | 2018-09-25 04:45:25 +0000 | [diff] [blame] | 171 | for (const MachineOperand &MO : MI.operands()) |
| 172 | if (MO.isRegMask()) |
| 173 | if (MO.clobbersPhysReg(AvailSrc) || MO.clobbersPhysReg(AvailDef)) |
| 174 | return nullptr; |
| 175 | |
Justin Bogner | db46784 | 2018-10-22 19:51:31 +0000 | [diff] [blame] | 176 | return AvailCopy; |
Justin Bogner | 97eaa64 | 2018-09-21 00:51:04 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | void clear() { |
Justin Bogner | db46784 | 2018-10-22 19:51:31 +0000 | [diff] [blame] | 180 | Copies.clear(); |
Justin Bogner | 97eaa64 | 2018-09-21 00:51:04 +0000 | [diff] [blame] | 181 | } |
| 182 | }; |
Matthias Braun | ed2d4b9 | 2016-02-26 03:18:50 +0000 | [diff] [blame] | 183 | |
Justin Bogner | b69da1b | 2018-09-21 00:08:33 +0000 | [diff] [blame] | 184 | class MachineCopyPropagation : public MachineFunctionPass { |
| 185 | const TargetRegisterInfo *TRI; |
| 186 | const TargetInstrInfo *TII; |
| 187 | const MachineRegisterInfo *MRI; |
Andrew Trick | 1df91b0 | 2012-02-08 21:22:43 +0000 | [diff] [blame] | 188 | |
Justin Bogner | b69da1b | 2018-09-21 00:08:33 +0000 | [diff] [blame] | 189 | public: |
| 190 | static char ID; // Pass identification, replacement for typeid |
Eugene Zelenko | 7cf6af5 | 2017-08-29 22:32:07 +0000 | [diff] [blame] | 191 | |
Justin Bogner | b69da1b | 2018-09-21 00:08:33 +0000 | [diff] [blame] | 192 | MachineCopyPropagation() : MachineFunctionPass(ID) { |
| 193 | initializeMachineCopyPropagationPass(*PassRegistry::getPassRegistry()); |
| 194 | } |
Evan Cheng | 977679d | 2012-01-07 03:02:36 +0000 | [diff] [blame] | 195 | |
Justin Bogner | b69da1b | 2018-09-21 00:08:33 +0000 | [diff] [blame] | 196 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 197 | AU.setPreservesCFG(); |
| 198 | MachineFunctionPass::getAnalysisUsage(AU); |
| 199 | } |
Matt Arsenault | 48de2a9 | 2016-06-02 00:04:26 +0000 | [diff] [blame] | 200 | |
Justin Bogner | b69da1b | 2018-09-21 00:08:33 +0000 | [diff] [blame] | 201 | bool runOnMachineFunction(MachineFunction &MF) override; |
Evan Cheng | 977679d | 2012-01-07 03:02:36 +0000 | [diff] [blame] | 202 | |
Justin Bogner | b69da1b | 2018-09-21 00:08:33 +0000 | [diff] [blame] | 203 | MachineFunctionProperties getRequiredProperties() const override { |
| 204 | return MachineFunctionProperties().set( |
| 205 | MachineFunctionProperties::Property::NoVRegs); |
| 206 | } |
Derek Schuff | fadd113 | 2016-03-28 17:05:30 +0000 | [diff] [blame] | 207 | |
Justin Bogner | b69da1b | 2018-09-21 00:08:33 +0000 | [diff] [blame] | 208 | private: |
| 209 | void ClobberRegister(unsigned Reg); |
| 210 | void ReadRegister(unsigned Reg); |
| 211 | void CopyPropagateBlock(MachineBasicBlock &MBB); |
| 212 | bool eraseIfRedundant(MachineInstr &Copy, unsigned Src, unsigned Def); |
| 213 | void forwardUses(MachineInstr &MI); |
| 214 | bool isForwardableRegClassCopy(const MachineInstr &Copy, |
| 215 | const MachineInstr &UseI, unsigned UseIdx); |
| 216 | bool hasImplicitOverlap(const MachineInstr &MI, const MachineOperand &Use); |
Matthias Braun | 4f08db2 | 2016-02-20 03:56:39 +0000 | [diff] [blame] | 217 | |
Justin Bogner | b69da1b | 2018-09-21 00:08:33 +0000 | [diff] [blame] | 218 | /// Candidates for deletion. |
| 219 | SmallSetVector<MachineInstr *, 8> MaybeDeadCopies; |
Eugene Zelenko | 7cf6af5 | 2017-08-29 22:32:07 +0000 | [diff] [blame] | 220 | |
Justin Bogner | 97eaa64 | 2018-09-21 00:51:04 +0000 | [diff] [blame] | 221 | CopyTracker Tracker; |
Eugene Zelenko | 7cf6af5 | 2017-08-29 22:32:07 +0000 | [diff] [blame] | 222 | |
Justin Bogner | b69da1b | 2018-09-21 00:08:33 +0000 | [diff] [blame] | 223 | bool Changed; |
| 224 | }; |
Eugene Zelenko | 7cf6af5 | 2017-08-29 22:32:07 +0000 | [diff] [blame] | 225 | |
| 226 | } // end anonymous namespace |
| 227 | |
Evan Cheng | 977679d | 2012-01-07 03:02:36 +0000 | [diff] [blame] | 228 | char MachineCopyPropagation::ID = 0; |
Eugene Zelenko | 7cf6af5 | 2017-08-29 22:32:07 +0000 | [diff] [blame] | 229 | |
Andrew Trick | 1dd8c85 | 2012-02-08 21:23:13 +0000 | [diff] [blame] | 230 | char &llvm::MachineCopyPropagationID = MachineCopyPropagation::ID; |
Evan Cheng | 977679d | 2012-01-07 03:02:36 +0000 | [diff] [blame] | 231 | |
Matthias Braun | 94c4904 | 2017-05-25 21:26:32 +0000 | [diff] [blame] | 232 | INITIALIZE_PASS(MachineCopyPropagation, DEBUG_TYPE, |
Evan Cheng | 977679d | 2012-01-07 03:02:36 +0000 | [diff] [blame] | 233 | "Machine Copy Propagation Pass", false, false) |
| 234 | |
Matthias Braun | b6384b2 | 2017-02-04 02:27:20 +0000 | [diff] [blame] | 235 | void MachineCopyPropagation::ReadRegister(unsigned Reg) { |
| 236 | // If 'Reg' is defined by a copy, the copy is no longer a candidate |
| 237 | // for elimination. |
Justin Bogner | db46784 | 2018-10-22 19:51:31 +0000 | [diff] [blame] | 238 | for (MCRegUnitIterator RUI(Reg, TRI); RUI.isValid(); ++RUI) { |
| 239 | if (MachineInstr *Copy = Tracker.findCopyForUnit(*RUI, *TRI)) { |
Justin Bogner | 97eaa64 | 2018-09-21 00:51:04 +0000 | [diff] [blame] | 240 | LLVM_DEBUG(dbgs() << "MCP: Copy is used - not dead: "; Copy->dump()); |
| 241 | MaybeDeadCopies.remove(Copy); |
Matthias Braun | b6384b2 | 2017-02-04 02:27:20 +0000 | [diff] [blame] | 242 | } |
| 243 | } |
| 244 | } |
| 245 | |
Matthias Braun | e0761c4 | 2016-02-26 03:18:55 +0000 | [diff] [blame] | 246 | /// Return true if \p PreviousCopy did copy register \p Src to register \p Def. |
| 247 | /// This fact may have been obscured by sub register usage or may not be true at |
| 248 | /// all even though Src and Def are subregisters of the registers used in |
| 249 | /// PreviousCopy. e.g. |
| 250 | /// isNopCopy("ecx = COPY eax", AX, CX) == true |
| 251 | /// isNopCopy("ecx = COPY eax", AH, CL) == false |
| 252 | static bool isNopCopy(const MachineInstr &PreviousCopy, unsigned Src, |
| 253 | unsigned Def, const TargetRegisterInfo *TRI) { |
| 254 | unsigned PreviousSrc = PreviousCopy.getOperand(1).getReg(); |
| 255 | unsigned PreviousDef = PreviousCopy.getOperand(0).getReg(); |
| 256 | if (Src == PreviousSrc) { |
| 257 | assert(Def == PreviousDef); |
Evan Cheng | 01b623c | 2012-02-20 23:28:17 +0000 | [diff] [blame] | 258 | return true; |
Evan Cheng | 01b623c | 2012-02-20 23:28:17 +0000 | [diff] [blame] | 259 | } |
Matthias Braun | e0761c4 | 2016-02-26 03:18:55 +0000 | [diff] [blame] | 260 | if (!TRI->isSubRegister(PreviousSrc, Src)) |
| 261 | return false; |
| 262 | unsigned SubIdx = TRI->getSubRegIndex(PreviousSrc, Src); |
| 263 | return SubIdx == TRI->getSubRegIndex(PreviousDef, Def); |
| 264 | } |
Evan Cheng | 01b623c | 2012-02-20 23:28:17 +0000 | [diff] [blame] | 265 | |
Matthias Braun | e0761c4 | 2016-02-26 03:18:55 +0000 | [diff] [blame] | 266 | /// Remove instruction \p Copy if there exists a previous copy that copies the |
| 267 | /// register \p Src to the register \p Def; This may happen indirectly by |
| 268 | /// copying the super registers. |
| 269 | bool MachineCopyPropagation::eraseIfRedundant(MachineInstr &Copy, unsigned Src, |
| 270 | unsigned Def) { |
| 271 | // Avoid eliminating a copy from/to a reserved registers as we cannot predict |
| 272 | // the value (Example: The sparc zero register is writable but stays zero). |
| 273 | if (MRI->isReserved(Src) || MRI->isReserved(Def)) |
| 274 | return false; |
| 275 | |
| 276 | // Search for an existing copy. |
Justin Bogner | db46784 | 2018-10-22 19:51:31 +0000 | [diff] [blame] | 277 | MachineInstr *PrevCopy = Tracker.findAvailCopy(Copy, Def, *TRI); |
Justin Bogner | 97eaa64 | 2018-09-21 00:51:04 +0000 | [diff] [blame] | 278 | if (!PrevCopy) |
Matthias Braun | e0761c4 | 2016-02-26 03:18:55 +0000 | [diff] [blame] | 279 | return false; |
| 280 | |
| 281 | // Check that the existing copy uses the correct sub registers. |
Justin Bogner | 97eaa64 | 2018-09-21 00:51:04 +0000 | [diff] [blame] | 282 | if (PrevCopy->getOperand(0).isDead()) |
Alexander Timofeev | 23476c4 | 2017-11-10 12:21:10 +0000 | [diff] [blame] | 283 | return false; |
Justin Bogner | 97eaa64 | 2018-09-21 00:51:04 +0000 | [diff] [blame] | 284 | if (!isNopCopy(*PrevCopy, Src, Def, TRI)) |
Matthias Braun | e0761c4 | 2016-02-26 03:18:55 +0000 | [diff] [blame] | 285 | return false; |
| 286 | |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 287 | LLVM_DEBUG(dbgs() << "MCP: copy is a NOP, removing: "; Copy.dump()); |
Matthias Braun | e0761c4 | 2016-02-26 03:18:55 +0000 | [diff] [blame] | 288 | |
| 289 | // Copy was redundantly redefining either Src or Def. Remove earlier kill |
| 290 | // flags between Copy and PrevCopy because the value will be reused now. |
| 291 | assert(Copy.isCopy()); |
| 292 | unsigned CopyDef = Copy.getOperand(0).getReg(); |
| 293 | assert(CopyDef == Src || CopyDef == Def); |
| 294 | for (MachineInstr &MI : |
Justin Bogner | 97eaa64 | 2018-09-21 00:51:04 +0000 | [diff] [blame] | 295 | make_range(PrevCopy->getIterator(), Copy.getIterator())) |
Matthias Braun | e0761c4 | 2016-02-26 03:18:55 +0000 | [diff] [blame] | 296 | MI.clearRegisterKills(CopyDef, TRI); |
| 297 | |
| 298 | Copy.eraseFromParent(); |
| 299 | Changed = true; |
| 300 | ++NumDeletes; |
| 301 | return true; |
Evan Cheng | 01b623c | 2012-02-20 23:28:17 +0000 | [diff] [blame] | 302 | } |
| 303 | |
Geoff Berry | 1bfec90 | 2018-02-27 16:59:10 +0000 | [diff] [blame] | 304 | /// Decide whether we should forward the source of \param Copy to its use in |
| 305 | /// \param UseI based on the physical register class constraints of the opcode |
| 306 | /// and avoiding introducing more cross-class COPYs. |
| 307 | bool MachineCopyPropagation::isForwardableRegClassCopy(const MachineInstr &Copy, |
| 308 | const MachineInstr &UseI, |
| 309 | unsigned UseIdx) { |
| 310 | |
| 311 | unsigned CopySrcReg = Copy.getOperand(1).getReg(); |
| 312 | |
| 313 | // If the new register meets the opcode register constraints, then allow |
| 314 | // forwarding. |
| 315 | if (const TargetRegisterClass *URC = |
| 316 | UseI.getRegClassConstraint(UseIdx, TII, TRI)) |
| 317 | return URC->contains(CopySrcReg); |
| 318 | |
| 319 | if (!UseI.isCopy()) |
| 320 | return false; |
| 321 | |
| 322 | /// COPYs don't have register class constraints, so if the user instruction |
| 323 | /// is a COPY, we just try to avoid introducing additional cross-class |
| 324 | /// COPYs. For example: |
| 325 | /// |
| 326 | /// RegClassA = COPY RegClassB // Copy parameter |
| 327 | /// ... |
| 328 | /// RegClassB = COPY RegClassA // UseI parameter |
| 329 | /// |
| 330 | /// which after forwarding becomes |
| 331 | /// |
| 332 | /// RegClassA = COPY RegClassB |
| 333 | /// ... |
| 334 | /// RegClassB = COPY RegClassB |
| 335 | /// |
| 336 | /// so we have reduced the number of cross-class COPYs and potentially |
| 337 | /// introduced a nop COPY that can be removed. |
| 338 | const TargetRegisterClass *UseDstRC = |
| 339 | TRI->getMinimalPhysRegClass(UseI.getOperand(0).getReg()); |
| 340 | |
| 341 | const TargetRegisterClass *SuperRC = UseDstRC; |
| 342 | for (TargetRegisterClass::sc_iterator SuperRCI = UseDstRC->getSuperClasses(); |
| 343 | SuperRC; SuperRC = *SuperRCI++) |
| 344 | if (SuperRC->contains(CopySrcReg)) |
| 345 | return true; |
| 346 | |
| 347 | return false; |
| 348 | } |
| 349 | |
| 350 | /// Check that \p MI does not have implicit uses that overlap with it's \p Use |
| 351 | /// operand (the register being replaced), since these can sometimes be |
| 352 | /// implicitly tied to other operands. For example, on AMDGPU: |
| 353 | /// |
| 354 | /// V_MOVRELS_B32_e32 %VGPR2, %M0<imp-use>, %EXEC<imp-use>, %VGPR2_VGPR3_VGPR4_VGPR5<imp-use> |
| 355 | /// |
| 356 | /// the %VGPR2 is implicitly tied to the larger reg operand, but we have no |
| 357 | /// way of knowing we need to update the latter when updating the former. |
| 358 | bool MachineCopyPropagation::hasImplicitOverlap(const MachineInstr &MI, |
| 359 | const MachineOperand &Use) { |
| 360 | for (const MachineOperand &MIUse : MI.uses()) |
| 361 | if (&MIUse != &Use && MIUse.isReg() && MIUse.isImplicit() && |
| 362 | MIUse.isUse() && TRI->regsOverlap(Use.getReg(), MIUse.getReg())) |
| 363 | return true; |
| 364 | |
| 365 | return false; |
| 366 | } |
| 367 | |
| 368 | /// Look for available copies whose destination register is used by \p MI and |
| 369 | /// replace the use in \p MI with the copy's source register. |
| 370 | void MachineCopyPropagation::forwardUses(MachineInstr &MI) { |
Justin Bogner | db46784 | 2018-10-22 19:51:31 +0000 | [diff] [blame] | 371 | if (!Tracker.hasAnyCopies()) |
Geoff Berry | 1bfec90 | 2018-02-27 16:59:10 +0000 | [diff] [blame] | 372 | return; |
| 373 | |
| 374 | // Look for non-tied explicit vreg uses that have an active COPY |
| 375 | // instruction that defines the physical register allocated to them. |
| 376 | // Replace the vreg with the source of the active COPY. |
| 377 | for (unsigned OpIdx = 0, OpEnd = MI.getNumOperands(); OpIdx < OpEnd; |
| 378 | ++OpIdx) { |
| 379 | MachineOperand &MOUse = MI.getOperand(OpIdx); |
| 380 | // Don't forward into undef use operands since doing so can cause problems |
| 381 | // with the machine verifier, since it doesn't treat undef reads as reads, |
| 382 | // so we can end up with a live range that ends on an undef read, leading to |
| 383 | // an error that the live range doesn't end on a read of the live range |
| 384 | // register. |
| 385 | if (!MOUse.isReg() || MOUse.isTied() || MOUse.isUndef() || MOUse.isDef() || |
| 386 | MOUse.isImplicit()) |
| 387 | continue; |
| 388 | |
| 389 | if (!MOUse.getReg()) |
| 390 | continue; |
| 391 | |
| 392 | // Check that the register is marked 'renamable' so we know it is safe to |
| 393 | // rename it without violating any constraints that aren't expressed in the |
| 394 | // IR (e.g. ABI or opcode requirements). |
| 395 | if (!MOUse.isRenamable()) |
| 396 | continue; |
| 397 | |
Justin Bogner | db46784 | 2018-10-22 19:51:31 +0000 | [diff] [blame] | 398 | MachineInstr *Copy = Tracker.findAvailCopy(MI, MOUse.getReg(), *TRI); |
Justin Bogner | 97eaa64 | 2018-09-21 00:51:04 +0000 | [diff] [blame] | 399 | if (!Copy) |
Geoff Berry | 1bfec90 | 2018-02-27 16:59:10 +0000 | [diff] [blame] | 400 | continue; |
| 401 | |
Justin Bogner | 97eaa64 | 2018-09-21 00:51:04 +0000 | [diff] [blame] | 402 | unsigned CopyDstReg = Copy->getOperand(0).getReg(); |
| 403 | const MachineOperand &CopySrc = Copy->getOperand(1); |
Geoff Berry | 1bfec90 | 2018-02-27 16:59:10 +0000 | [diff] [blame] | 404 | unsigned CopySrcReg = CopySrc.getReg(); |
| 405 | |
| 406 | // FIXME: Don't handle partial uses of wider COPYs yet. |
| 407 | if (MOUse.getReg() != CopyDstReg) { |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 408 | LLVM_DEBUG( |
| 409 | dbgs() << "MCP: FIXME! Not forwarding COPY to sub-register use:\n " |
| 410 | << MI); |
Geoff Berry | 1bfec90 | 2018-02-27 16:59:10 +0000 | [diff] [blame] | 411 | continue; |
| 412 | } |
| 413 | |
| 414 | // Don't forward COPYs of reserved regs unless they are constant. |
| 415 | if (MRI->isReserved(CopySrcReg) && !MRI->isConstantPhysReg(CopySrcReg)) |
| 416 | continue; |
| 417 | |
Justin Bogner | 97eaa64 | 2018-09-21 00:51:04 +0000 | [diff] [blame] | 418 | if (!isForwardableRegClassCopy(*Copy, MI, OpIdx)) |
Geoff Berry | 1bfec90 | 2018-02-27 16:59:10 +0000 | [diff] [blame] | 419 | continue; |
| 420 | |
| 421 | if (hasImplicitOverlap(MI, MOUse)) |
| 422 | continue; |
| 423 | |
| 424 | if (!DebugCounter::shouldExecute(FwdCounter)) { |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 425 | LLVM_DEBUG(dbgs() << "MCP: Skipping forwarding due to debug counter:\n " |
| 426 | << MI); |
Geoff Berry | 1bfec90 | 2018-02-27 16:59:10 +0000 | [diff] [blame] | 427 | continue; |
| 428 | } |
| 429 | |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 430 | LLVM_DEBUG(dbgs() << "MCP: Replacing " << printReg(MOUse.getReg(), TRI) |
| 431 | << "\n with " << printReg(CopySrcReg, TRI) |
Justin Bogner | 97eaa64 | 2018-09-21 00:51:04 +0000 | [diff] [blame] | 432 | << "\n in " << MI << " from " << *Copy); |
Geoff Berry | 1bfec90 | 2018-02-27 16:59:10 +0000 | [diff] [blame] | 433 | |
| 434 | MOUse.setReg(CopySrcReg); |
| 435 | if (!CopySrc.isRenamable()) |
| 436 | MOUse.setIsRenamable(false); |
| 437 | |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 438 | LLVM_DEBUG(dbgs() << "MCP: After replacement: " << MI << "\n"); |
Geoff Berry | 1bfec90 | 2018-02-27 16:59:10 +0000 | [diff] [blame] | 439 | |
| 440 | // Clear kill markers that may have been invalidated. |
| 441 | for (MachineInstr &KMI : |
Justin Bogner | 97eaa64 | 2018-09-21 00:51:04 +0000 | [diff] [blame] | 442 | make_range(Copy->getIterator(), std::next(MI.getIterator()))) |
Geoff Berry | 1bfec90 | 2018-02-27 16:59:10 +0000 | [diff] [blame] | 443 | KMI.clearRegisterKills(CopySrcReg, TRI); |
| 444 | |
| 445 | ++NumCopyForwards; |
| 446 | Changed = true; |
| 447 | } |
| 448 | } |
| 449 | |
Matthias Braun | 4f08db2 | 2016-02-20 03:56:39 +0000 | [diff] [blame] | 450 | void MachineCopyPropagation::CopyPropagateBlock(MachineBasicBlock &MBB) { |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 451 | LLVM_DEBUG(dbgs() << "MCP: CopyPropagateBlock " << MBB.getName() << "\n"); |
James Molloy | bb96dfc | 2014-01-22 09:12:27 +0000 | [diff] [blame] | 452 | |
Evan Cheng | 977679d | 2012-01-07 03:02:36 +0000 | [diff] [blame] | 453 | for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ) { |
| 454 | MachineInstr *MI = &*I; |
| 455 | ++I; |
| 456 | |
Eli Friedman | 033a78e | 2018-03-30 00:56:03 +0000 | [diff] [blame] | 457 | // Analyze copies (which don't overlap themselves). |
| 458 | if (MI->isCopy() && !TRI->regsOverlap(MI->getOperand(0).getReg(), |
| 459 | MI->getOperand(1).getReg())) { |
Geoff Berry | c3ef7ae | 2017-10-03 16:59:13 +0000 | [diff] [blame] | 460 | unsigned Def = MI->getOperand(0).getReg(); |
| 461 | unsigned Src = MI->getOperand(1).getReg(); |
| 462 | |
| 463 | assert(!TargetRegisterInfo::isVirtualRegister(Def) && |
| 464 | !TargetRegisterInfo::isVirtualRegister(Src) && |
| 465 | "MachineCopyPropagation should be run after register allocation!"); |
Evan Cheng | 977679d | 2012-01-07 03:02:36 +0000 | [diff] [blame] | 466 | |
Matthias Braun | e0761c4 | 2016-02-26 03:18:55 +0000 | [diff] [blame] | 467 | // The two copies cancel out and the source of the first copy |
| 468 | // hasn't been overridden, eliminate the second one. e.g. |
Francis Visoiu Mistrih | fd11bc0 | 2017-12-07 10:40:31 +0000 | [diff] [blame] | 469 | // %ecx = COPY %eax |
Francis Visoiu Mistrih | a4ec08b | 2017-11-28 17:15:09 +0000 | [diff] [blame] | 470 | // ... nothing clobbered eax. |
Francis Visoiu Mistrih | fd11bc0 | 2017-12-07 10:40:31 +0000 | [diff] [blame] | 471 | // %eax = COPY %ecx |
Matthias Braun | e0761c4 | 2016-02-26 03:18:55 +0000 | [diff] [blame] | 472 | // => |
Francis Visoiu Mistrih | fd11bc0 | 2017-12-07 10:40:31 +0000 | [diff] [blame] | 473 | // %ecx = COPY %eax |
Matthias Braun | e0761c4 | 2016-02-26 03:18:55 +0000 | [diff] [blame] | 474 | // |
| 475 | // or |
| 476 | // |
Francis Visoiu Mistrih | fd11bc0 | 2017-12-07 10:40:31 +0000 | [diff] [blame] | 477 | // %ecx = COPY %eax |
Francis Visoiu Mistrih | a4ec08b | 2017-11-28 17:15:09 +0000 | [diff] [blame] | 478 | // ... nothing clobbered eax. |
Francis Visoiu Mistrih | fd11bc0 | 2017-12-07 10:40:31 +0000 | [diff] [blame] | 479 | // %ecx = COPY %eax |
Matthias Braun | e0761c4 | 2016-02-26 03:18:55 +0000 | [diff] [blame] | 480 | // => |
Francis Visoiu Mistrih | fd11bc0 | 2017-12-07 10:40:31 +0000 | [diff] [blame] | 481 | // %ecx = COPY %eax |
Geoff Berry | c3ef7ae | 2017-10-03 16:59:13 +0000 | [diff] [blame] | 482 | if (eraseIfRedundant(*MI, Def, Src) || eraseIfRedundant(*MI, Src, Def)) |
| 483 | continue; |
Evan Cheng | 977679d | 2012-01-07 03:02:36 +0000 | [diff] [blame] | 484 | |
Geoff Berry | 1bfec90 | 2018-02-27 16:59:10 +0000 | [diff] [blame] | 485 | forwardUses(*MI); |
| 486 | |
| 487 | // Src may have been changed by forwardUses() |
| 488 | Src = MI->getOperand(1).getReg(); |
| 489 | |
Jun Bum Lim | ccdf189 | 2016-02-03 15:56:27 +0000 | [diff] [blame] | 490 | // If Src is defined by a previous copy, the previous copy cannot be |
| 491 | // eliminated. |
Matthias Braun | b6384b2 | 2017-02-04 02:27:20 +0000 | [diff] [blame] | 492 | ReadRegister(Src); |
| 493 | for (const MachineOperand &MO : MI->implicit_operands()) { |
| 494 | if (!MO.isReg() || !MO.readsReg()) |
| 495 | continue; |
| 496 | unsigned Reg = MO.getReg(); |
| 497 | if (!Reg) |
| 498 | continue; |
| 499 | ReadRegister(Reg); |
Evan Cheng | 977679d | 2012-01-07 03:02:36 +0000 | [diff] [blame] | 500 | } |
| 501 | |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 502 | LLVM_DEBUG(dbgs() << "MCP: Copy is a deletion candidate: "; MI->dump()); |
James Molloy | bb96dfc | 2014-01-22 09:12:27 +0000 | [diff] [blame] | 503 | |
Evan Cheng | 977679d | 2012-01-07 03:02:36 +0000 | [diff] [blame] | 504 | // Copy is now a candidate for deletion. |
Geoff Berry | c3ef7ae | 2017-10-03 16:59:13 +0000 | [diff] [blame] | 505 | if (!MRI->isReserved(Def)) |
Matthias Braun | 4d44c95 | 2016-02-20 03:56:36 +0000 | [diff] [blame] | 506 | MaybeDeadCopies.insert(MI); |
Evan Cheng | 977679d | 2012-01-07 03:02:36 +0000 | [diff] [blame] | 507 | |
Jun Bum Lim | ccdf189 | 2016-02-03 15:56:27 +0000 | [diff] [blame] | 508 | // If 'Def' is previously source of another copy, then this earlier copy's |
Evan Cheng | 977679d | 2012-01-07 03:02:36 +0000 | [diff] [blame] | 509 | // source is no longer available. e.g. |
Francis Visoiu Mistrih | fd11bc0 | 2017-12-07 10:40:31 +0000 | [diff] [blame] | 510 | // %xmm9 = copy %xmm2 |
Evan Cheng | 977679d | 2012-01-07 03:02:36 +0000 | [diff] [blame] | 511 | // ... |
Francis Visoiu Mistrih | fd11bc0 | 2017-12-07 10:40:31 +0000 | [diff] [blame] | 512 | // %xmm2 = copy %xmm0 |
Evan Cheng | 977679d | 2012-01-07 03:02:36 +0000 | [diff] [blame] | 513 | // ... |
Francis Visoiu Mistrih | fd11bc0 | 2017-12-07 10:40:31 +0000 | [diff] [blame] | 514 | // %xmm2 = copy %xmm9 |
Justin Bogner | 97eaa64 | 2018-09-21 00:51:04 +0000 | [diff] [blame] | 515 | Tracker.clobberRegister(Def, *TRI); |
Matthias Braun | b6384b2 | 2017-02-04 02:27:20 +0000 | [diff] [blame] | 516 | for (const MachineOperand &MO : MI->implicit_operands()) { |
| 517 | if (!MO.isReg() || !MO.isDef()) |
| 518 | continue; |
Geoff Berry | c3ef7ae | 2017-10-03 16:59:13 +0000 | [diff] [blame] | 519 | unsigned Reg = MO.getReg(); |
Matthias Braun | b6384b2 | 2017-02-04 02:27:20 +0000 | [diff] [blame] | 520 | if (!Reg) |
| 521 | continue; |
Justin Bogner | 97eaa64 | 2018-09-21 00:51:04 +0000 | [diff] [blame] | 522 | Tracker.clobberRegister(Reg, *TRI); |
Matthias Braun | b6384b2 | 2017-02-04 02:27:20 +0000 | [diff] [blame] | 523 | } |
Evan Cheng | 977679d | 2012-01-07 03:02:36 +0000 | [diff] [blame] | 524 | |
Justin Bogner | 97eaa64 | 2018-09-21 00:51:04 +0000 | [diff] [blame] | 525 | Tracker.trackCopy(MI, *TRI); |
Alexander Timofeev | f6eb7ff | 2017-10-16 16:57:37 +0000 | [diff] [blame] | 526 | |
Evan Cheng | 977679d | 2012-01-07 03:02:36 +0000 | [diff] [blame] | 527 | continue; |
| 528 | } |
| 529 | |
Geoff Berry | 1bfec90 | 2018-02-27 16:59:10 +0000 | [diff] [blame] | 530 | // Clobber any earlyclobber regs first. |
| 531 | for (const MachineOperand &MO : MI->operands()) |
| 532 | if (MO.isReg() && MO.isEarlyClobber()) { |
| 533 | unsigned Reg = MO.getReg(); |
| 534 | // If we have a tied earlyclobber, that means it is also read by this |
| 535 | // instruction, so we need to make sure we don't remove it as dead |
| 536 | // later. |
| 537 | if (MO.isTied()) |
| 538 | ReadRegister(Reg); |
Justin Bogner | 97eaa64 | 2018-09-21 00:51:04 +0000 | [diff] [blame] | 539 | Tracker.clobberRegister(Reg, *TRI); |
Geoff Berry | 1bfec90 | 2018-02-27 16:59:10 +0000 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | forwardUses(*MI); |
| 543 | |
Evan Cheng | 977679d | 2012-01-07 03:02:36 +0000 | [diff] [blame] | 544 | // Not a copy. |
| 545 | SmallVector<unsigned, 2> Defs; |
Matthias Braun | 4d44c95 | 2016-02-20 03:56:36 +0000 | [diff] [blame] | 546 | const MachineOperand *RegMask = nullptr; |
| 547 | for (const MachineOperand &MO : MI->operands()) { |
Jakob Stoklund Olesen | a8fc171 | 2012-02-08 22:37:35 +0000 | [diff] [blame] | 548 | if (MO.isRegMask()) |
Matthias Braun | 4d44c95 | 2016-02-20 03:56:36 +0000 | [diff] [blame] | 549 | RegMask = &MO; |
Evan Cheng | 977679d | 2012-01-07 03:02:36 +0000 | [diff] [blame] | 550 | if (!MO.isReg()) |
| 551 | continue; |
Geoff Berry | c3ef7ae | 2017-10-03 16:59:13 +0000 | [diff] [blame] | 552 | unsigned Reg = MO.getReg(); |
Evan Cheng | 977679d | 2012-01-07 03:02:36 +0000 | [diff] [blame] | 553 | if (!Reg) |
| 554 | continue; |
| 555 | |
Geoff Berry | c3ef7ae | 2017-10-03 16:59:13 +0000 | [diff] [blame] | 556 | assert(!TargetRegisterInfo::isVirtualRegister(Reg) && |
| 557 | "MachineCopyPropagation should be run after register allocation!"); |
| 558 | |
Geoff Berry | 1bfec90 | 2018-02-27 16:59:10 +0000 | [diff] [blame] | 559 | if (MO.isDef() && !MO.isEarlyClobber()) { |
Evan Cheng | 977679d | 2012-01-07 03:02:36 +0000 | [diff] [blame] | 560 | Defs.push_back(Reg); |
| 561 | continue; |
Krzysztof Parzyszek | 873b4b6 | 2018-07-11 13:30:27 +0000 | [diff] [blame] | 562 | } else if (!MO.isDebug() && MO.readsReg()) |
Matthias Braun | b6384b2 | 2017-02-04 02:27:20 +0000 | [diff] [blame] | 563 | ReadRegister(Reg); |
Evan Cheng | 977679d | 2012-01-07 03:02:36 +0000 | [diff] [blame] | 564 | } |
| 565 | |
Jakob Stoklund Olesen | a8fc171 | 2012-02-08 22:37:35 +0000 | [diff] [blame] | 566 | // The instruction has a register mask operand which means that it clobbers |
Matthias Braun | ed2d4b9 | 2016-02-26 03:18:50 +0000 | [diff] [blame] | 567 | // a large set of registers. Treat clobbered registers the same way as |
| 568 | // defined registers. |
Matthias Braun | 4d44c95 | 2016-02-20 03:56:36 +0000 | [diff] [blame] | 569 | if (RegMask) { |
Jakob Stoklund Olesen | f56ce53 | 2012-02-09 00:19:08 +0000 | [diff] [blame] | 570 | // Erase any MaybeDeadCopies whose destination register is clobbered. |
Jun Bum Lim | 386c67f | 2016-03-25 21:15:35 +0000 | [diff] [blame] | 571 | for (SmallSetVector<MachineInstr *, 8>::iterator DI = |
| 572 | MaybeDeadCopies.begin(); |
| 573 | DI != MaybeDeadCopies.end();) { |
| 574 | MachineInstr *MaybeDead = *DI; |
Matthias Braun | 4d44c95 | 2016-02-20 03:56:36 +0000 | [diff] [blame] | 575 | unsigned Reg = MaybeDead->getOperand(0).getReg(); |
| 576 | assert(!MRI->isReserved(Reg)); |
Jun Bum Lim | 386c67f | 2016-03-25 21:15:35 +0000 | [diff] [blame] | 577 | |
| 578 | if (!RegMask->clobbersPhysReg(Reg)) { |
| 579 | ++DI; |
Jakob Stoklund Olesen | f56ce53 | 2012-02-09 00:19:08 +0000 | [diff] [blame] | 580 | continue; |
Jun Bum Lim | 386c67f | 2016-03-25 21:15:35 +0000 | [diff] [blame] | 581 | } |
| 582 | |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 583 | LLVM_DEBUG(dbgs() << "MCP: Removing copy due to regmask clobbering: "; |
| 584 | MaybeDead->dump()); |
Jun Bum Lim | 386c67f | 2016-03-25 21:15:35 +0000 | [diff] [blame] | 585 | |
Justin Bogner | 60a5c77 | 2018-09-25 04:45:25 +0000 | [diff] [blame] | 586 | // Make sure we invalidate any entries in the copy maps before erasing |
| 587 | // the instruction. |
| 588 | Tracker.clobberRegister(Reg, *TRI); |
| 589 | |
Jun Bum Lim | 386c67f | 2016-03-25 21:15:35 +0000 | [diff] [blame] | 590 | // erase() will return the next valid iterator pointing to the next |
| 591 | // element after the erased one. |
| 592 | DI = MaybeDeadCopies.erase(DI); |
Matthias Braun | 4d44c95 | 2016-02-20 03:56:36 +0000 | [diff] [blame] | 593 | MaybeDead->eraseFromParent(); |
Jakob Stoklund Olesen | f56ce53 | 2012-02-09 00:19:08 +0000 | [diff] [blame] | 594 | Changed = true; |
| 595 | ++NumDeletes; |
| 596 | } |
Jakob Stoklund Olesen | a8fc171 | 2012-02-08 22:37:35 +0000 | [diff] [blame] | 597 | } |
| 598 | |
Matthias Braun | ed2d4b9 | 2016-02-26 03:18:50 +0000 | [diff] [blame] | 599 | // Any previous copy definition or reading the Defs is no longer available. |
Matthias Braun | e0761c4 | 2016-02-26 03:18:55 +0000 | [diff] [blame] | 600 | for (unsigned Reg : Defs) |
Justin Bogner | 97eaa64 | 2018-09-21 00:51:04 +0000 | [diff] [blame] | 601 | Tracker.clobberRegister(Reg, *TRI); |
Evan Cheng | 977679d | 2012-01-07 03:02:36 +0000 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | // If MBB doesn't have successors, delete the copies whose defs are not used. |
| 605 | // If MBB does have successors, then conservative assume the defs are live-out |
| 606 | // since we don't want to trust live-in lists. |
| 607 | if (MBB.succ_empty()) { |
Matthias Braun | 4d44c95 | 2016-02-20 03:56:36 +0000 | [diff] [blame] | 608 | for (MachineInstr *MaybeDead : MaybeDeadCopies) { |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 609 | LLVM_DEBUG(dbgs() << "MCP: Removing copy due to no live-out succ: "; |
| 610 | MaybeDead->dump()); |
Matthias Braun | 4d44c95 | 2016-02-20 03:56:36 +0000 | [diff] [blame] | 611 | assert(!MRI->isReserved(MaybeDead->getOperand(0).getReg())); |
Carlos Alberto Enciso | 42b5443 | 2018-10-01 08:14:44 +0000 | [diff] [blame] | 612 | |
| 613 | // Update matching debug values. |
| 614 | assert(MaybeDead->isCopy()); |
| 615 | MaybeDead->changeDebugValuesDefReg(MaybeDead->getOperand(1).getReg()); |
| 616 | |
Matthias Braun | 4d44c95 | 2016-02-20 03:56:36 +0000 | [diff] [blame] | 617 | MaybeDead->eraseFromParent(); |
| 618 | Changed = true; |
| 619 | ++NumDeletes; |
Evan Cheng | 977679d | 2012-01-07 03:02:36 +0000 | [diff] [blame] | 620 | } |
| 621 | } |
| 622 | |
Matthias Braun | 4f08db2 | 2016-02-20 03:56:39 +0000 | [diff] [blame] | 623 | MaybeDeadCopies.clear(); |
Justin Bogner | 97eaa64 | 2018-09-21 00:51:04 +0000 | [diff] [blame] | 624 | Tracker.clear(); |
Evan Cheng | 977679d | 2012-01-07 03:02:36 +0000 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | bool MachineCopyPropagation::runOnMachineFunction(MachineFunction &MF) { |
Matthias Braun | d318139 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 628 | if (skipFunction(MF.getFunction())) |
Paul Robinson | 5fa58a5 | 2014-03-31 17:43:35 +0000 | [diff] [blame] | 629 | return false; |
| 630 | |
Matthias Braun | 4f08db2 | 2016-02-20 03:56:39 +0000 | [diff] [blame] | 631 | Changed = false; |
Evan Cheng | 977679d | 2012-01-07 03:02:36 +0000 | [diff] [blame] | 632 | |
Eric Christopher | 6035518 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 633 | TRI = MF.getSubtarget().getRegisterInfo(); |
| 634 | TII = MF.getSubtarget().getInstrInfo(); |
Jakob Stoklund Olesen | fb9ebbf | 2012-10-15 21:57:41 +0000 | [diff] [blame] | 635 | MRI = &MF.getRegInfo(); |
Evan Cheng | 977679d | 2012-01-07 03:02:36 +0000 | [diff] [blame] | 636 | |
Matthias Braun | 4d44c95 | 2016-02-20 03:56:36 +0000 | [diff] [blame] | 637 | for (MachineBasicBlock &MBB : MF) |
Matthias Braun | 4f08db2 | 2016-02-20 03:56:39 +0000 | [diff] [blame] | 638 | CopyPropagateBlock(MBB); |
Evan Cheng | 977679d | 2012-01-07 03:02:36 +0000 | [diff] [blame] | 639 | |
| 640 | return Changed; |
| 641 | } |