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