Coverage Report

Created: 2024-01-17 10:31

/src/llvm-project/llvm/lib/CodeGen/MachineCopyPropagation.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- MachineCopyPropagation.cpp - Machine Copy Propagation Pass ---------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// This is an extremely simple MachineInstr-level copy propagation pass.
10
//
11
// This pass forwards the source of COPYs to the users of their destinations
12
// when doing so is legal.  For example:
13
//
14
//   %reg1 = COPY %reg0
15
//   ...
16
//   ... = OP %reg1
17
//
18
// If
19
//   - %reg0 has not been clobbered by the time of the use of %reg1
20
//   - the register class constraints are satisfied
21
//   - the COPY def is the only value that reaches OP
22
// then this pass replaces the above with:
23
//
24
//   %reg1 = COPY %reg0
25
//   ...
26
//   ... = OP %reg0
27
//
28
// This pass also removes some redundant COPYs.  For example:
29
//
30
//    %R1 = COPY %R0
31
//    ... // No clobber of %R1
32
//    %R0 = COPY %R1 <<< Removed
33
//
34
// or
35
//
36
//    %R1 = COPY %R0
37
//    ... // No clobber of %R0
38
//    %R1 = COPY %R0 <<< Removed
39
//
40
// or
41
//
42
//    $R0 = OP ...
43
//    ... // No read/clobber of $R0 and $R1
44
//    $R1 = COPY $R0 // $R0 is killed
45
// Replace $R0 with $R1 and remove the COPY
46
//    $R1 = OP ...
47
//    ...
48
//
49
//===----------------------------------------------------------------------===//
50
51
#include "llvm/ADT/DenseMap.h"
52
#include "llvm/ADT/STLExtras.h"
53
#include "llvm/ADT/SetVector.h"
54
#include "llvm/ADT/SmallSet.h"
55
#include "llvm/ADT/SmallVector.h"
56
#include "llvm/ADT/Statistic.h"
57
#include "llvm/ADT/iterator_range.h"
58
#include "llvm/CodeGen/MachineBasicBlock.h"
59
#include "llvm/CodeGen/MachineFunction.h"
60
#include "llvm/CodeGen/MachineFunctionPass.h"
61
#include "llvm/CodeGen/MachineInstr.h"
62
#include "llvm/CodeGen/MachineOperand.h"
63
#include "llvm/CodeGen/MachineRegisterInfo.h"
64
#include "llvm/CodeGen/TargetInstrInfo.h"
65
#include "llvm/CodeGen/TargetRegisterInfo.h"
66
#include "llvm/CodeGen/TargetSubtargetInfo.h"
67
#include "llvm/InitializePasses.h"
68
#include "llvm/MC/MCRegisterInfo.h"
69
#include "llvm/Pass.h"
70
#include "llvm/Support/Debug.h"
71
#include "llvm/Support/DebugCounter.h"
72
#include "llvm/Support/raw_ostream.h"
73
#include <cassert>
74
#include <iterator>
75
76
using namespace llvm;
77
78
#define DEBUG_TYPE "machine-cp"
79
80
STATISTIC(NumDeletes, "Number of dead copies deleted");
81
STATISTIC(NumCopyForwards, "Number of copy uses forwarded");
82
STATISTIC(NumCopyBackwardPropagated, "Number of copy defs backward propagated");
83
STATISTIC(SpillageChainsLength, "Length of spillage chains");
84
STATISTIC(NumSpillageChains, "Number of spillage chains");
85
DEBUG_COUNTER(FwdCounter, "machine-cp-fwd",
86
              "Controls which register COPYs are forwarded");
87
88
static cl::opt<bool> MCPUseCopyInstr("mcp-use-is-copy-instr", cl::init(false),
89
                                     cl::Hidden);
90
static cl::opt<cl::boolOrDefault>
91
    EnableSpillageCopyElimination("enable-spill-copy-elim", cl::Hidden);
92
93
namespace {
94
95
static std::optional<DestSourcePair> isCopyInstr(const MachineInstr &MI,
96
                                                 const TargetInstrInfo &TII,
97
22.7M
                                                 bool UseCopyInstr) {
98
22.7M
  if (UseCopyInstr)
99
2.17M
    return TII.isCopyInstr(MI);
100
101
20.5M
  if (MI.isCopy())
102
3.18M
    return std::optional<DestSourcePair>(
103
3.18M
        DestSourcePair{MI.getOperand(0), MI.getOperand(1)});
104
105
17.4M
  return std::nullopt;
106
20.5M
}
107
108
class CopyTracker {
109
  struct CopyInfo {
110
    MachineInstr *MI, *LastSeenUseInCopy;
111
    SmallVector<MCRegister, 4> DefRegs;
112
    bool Avail;
113
  };
114
115
  DenseMap<MCRegister, CopyInfo> Copies;
116
117
public:
118
  /// Mark all of the given registers and their subregisters as unavailable for
119
  /// copying.
120
  void markRegsUnavailable(ArrayRef<MCRegister> Regs,
121
1.28M
                           const TargetRegisterInfo &TRI) {
122
1.28M
    for (MCRegister Reg : Regs) {
123
      // Source of copy is no longer available for propagation.
124
1.05M
      for (MCRegUnit Unit : TRI.regunits(Reg)) {
125
1.05M
        auto CI = Copies.find(Unit);
126
1.05M
        if (CI != Copies.end())
127
971k
          CI->second.Avail = false;
128
1.05M
      }
129
775k
    }
130
1.28M
  }
131
132
  /// Remove register from copy maps.
133
  void invalidateRegister(MCRegister Reg, const TargetRegisterInfo &TRI,
134
18.8M
                          const TargetInstrInfo &TII, bool UseCopyInstr) {
135
    // Since Reg might be a subreg of some registers, only invalidate Reg is not
136
    // enough. We have to find the COPY defines Reg or registers defined by Reg
137
    // and invalidate all of them. Similarly, we must invalidate all of the
138
    // the subregisters used in the source of the COPY.
139
18.8M
    SmallSet<MCRegUnit, 8> RegUnitsToInvalidate;
140
18.8M
    auto InvalidateCopy = [&](MachineInstr *MI) {
141
135k
      std::optional<DestSourcePair> CopyOperands =
142
135k
          isCopyInstr(*MI, TII, UseCopyInstr);
143
135k
      assert(CopyOperands && "Expect copy");
144
145
0
      auto Dest = TRI.regunits(CopyOperands->Destination->getReg().asMCReg());
146
135k
      auto Src = TRI.regunits(CopyOperands->Source->getReg().asMCReg());
147
135k
      RegUnitsToInvalidate.insert(Dest.begin(), Dest.end());
148
135k
      RegUnitsToInvalidate.insert(Src.begin(), Src.end());
149
135k
    };
150
151
20.7M
    for (MCRegUnit Unit : TRI.regunits(Reg)) {
152
20.7M
      auto I = Copies.find(Unit);
153
20.7M
      if (I != Copies.end()) {
154
135k
        if (MachineInstr *MI = I->second.MI)
155
106k
          InvalidateCopy(MI);
156
135k
        if (MachineInstr *MI = I->second.LastSeenUseInCopy)
157
28.8k
          InvalidateCopy(MI);
158
135k
      }
159
20.7M
    }
160
18.8M
    for (MCRegUnit Unit : RegUnitsToInvalidate)
161
277k
      Copies.erase(Unit);
162
18.8M
  }
163
164
  /// Clobber a single register, removing it from the tracker's copy maps.
165
  void clobberRegister(MCRegister Reg, const TargetRegisterInfo &TRI,
166
7.43M
                       const TargetInstrInfo &TII, bool UseCopyInstr) {
167
8.32M
    for (MCRegUnit Unit : TRI.regunits(Reg)) {
168
8.32M
      auto I = Copies.find(Unit);
169
8.32M
      if (I != Copies.end()) {
170
        // When we clobber the source of a copy, we need to clobber everything
171
        // it defined.
172
756k
        markRegsUnavailable(I->second.DefRegs, TRI);
173
        // When we clobber the destination of a copy, we need to clobber the
174
        // whole register it defined.
175
756k
        if (MachineInstr *MI = I->second.MI) {
176
527k
          std::optional<DestSourcePair> CopyOperands =
177
527k
              isCopyInstr(*MI, TII, UseCopyInstr);
178
179
527k
          MCRegister Def = CopyOperands->Destination->getReg().asMCReg();
180
527k
          MCRegister Src = CopyOperands->Source->getReg().asMCReg();
181
182
527k
          markRegsUnavailable(Def, TRI);
183
184
          // Since we clobber the destination of a copy, the semantic of Src's
185
          // "DefRegs" to contain Def is no longer effectual. We will also need
186
          // to remove the record from the copy maps that indicates Src defined
187
          // Def. Failing to do so might cause the target to miss some
188
          // opportunities to further eliminate redundant copy instructions.
189
          // Consider the following sequence during the
190
          // ForwardCopyPropagateBlock procedure:
191
          // L1: r0 = COPY r9     <- TrackMI
192
          // L2: r0 = COPY r8     <- TrackMI (Remove r9 defined r0 from tracker)
193
          // L3: use r0           <- Remove L2 from MaybeDeadCopies
194
          // L4: early-clobber r9 <- Clobber r9 (L2 is still valid in tracker)
195
          // L5: r0 = COPY r8     <- Remove NopCopy
196
694k
          for (MCRegUnit SrcUnit : TRI.regunits(Src)) {
197
694k
            auto SrcCopy = Copies.find(SrcUnit);
198
694k
            if (SrcCopy != Copies.end() && SrcCopy->second.LastSeenUseInCopy) {
199
              // If SrcCopy defines multiple values, we only need
200
              // to erase the record for Def in DefRegs.
201
407k
              for (auto itr = SrcCopy->second.DefRegs.begin();
202
427k
                   itr != SrcCopy->second.DefRegs.end(); itr++) {
203
405k
                if (*itr == Def) {
204
384k
                  SrcCopy->second.DefRegs.erase(itr);
205
                  // If DefReg becomes empty after removal, we can remove the
206
                  // SrcCopy from the tracker's copy maps. We only remove those
207
                  // entries solely record the Def is defined by Src. If an
208
                  // entry also contains the definition record of other Def'
209
                  // registers, it cannot be cleared.
210
384k
                  if (SrcCopy->second.DefRegs.empty() && !SrcCopy->second.MI) {
211
294k
                    Copies.erase(SrcCopy);
212
294k
                  }
213
384k
                  break;
214
384k
                }
215
405k
              }
216
407k
            }
217
694k
          }
218
527k
        }
219
        // Now we can erase the copy.
220
756k
        Copies.erase(I);
221
756k
      }
222
8.32M
    }
223
7.43M
  }
224
225
  /// Add this copy's registers into the tracker's copy maps.
226
  void trackCopy(MachineInstr *MI, const TargetRegisterInfo &TRI,
227
752k
                 const TargetInstrInfo &TII, bool UseCopyInstr) {
228
752k
    std::optional<DestSourcePair> CopyOperands =
229
752k
        isCopyInstr(*MI, TII, UseCopyInstr);
230
752k
    assert(CopyOperands && "Tracking non-copy?");
231
232
0
    MCRegister Src = CopyOperands->Source->getReg().asMCReg();
233
752k
    MCRegister Def = CopyOperands->Destination->getReg().asMCReg();
234
235
    // Remember Def is defined by the copy.
236
752k
    for (MCRegUnit Unit : TRI.regunits(Def))
237
863k
      Copies[Unit] = {MI, nullptr, {}, true};
238
239
    // Remember source that's copied to Def. Once it's clobbered, then
240
    // it's no longer available for copy propagation.
241
863k
    for (MCRegUnit Unit : TRI.regunits(Src)) {
242
863k
      auto I = Copies.insert({Unit, {nullptr, nullptr, {}, false}});
243
863k
      auto &Copy = I.first->second;
244
863k
      if (!is_contained(Copy.DefRegs, Def))
245
862k
        Copy.DefRegs.push_back(Def);
246
863k
      Copy.LastSeenUseInCopy = MI;
247
863k
    }
248
752k
  }
249
250
17.6M
  bool hasAnyCopies() {
251
17.6M
    return !Copies.empty();
252
17.6M
  }
253
254
  MachineInstr *findCopyForUnit(MCRegister RegUnit,
255
                                const TargetRegisterInfo &TRI,
256
17.0M
                                bool MustBeAvailable = false) {
257
17.0M
    auto CI = Copies.find(RegUnit);
258
17.0M
    if (CI == Copies.end())
259
15.4M
      return nullptr;
260
1.53M
    if (MustBeAvailable && !CI->second.Avail)
261
461k
      return nullptr;
262
1.07M
    return CI->second.MI;
263
1.53M
  }
264
265
  MachineInstr *findCopyDefViaUnit(MCRegister RegUnit,
266
208k
                                   const TargetRegisterInfo &TRI) {
267
208k
    auto CI = Copies.find(RegUnit);
268
208k
    if (CI == Copies.end())
269
203k
      return nullptr;
270
4.60k
    if (CI->second.DefRegs.size() != 1)
271
122
      return nullptr;
272
4.48k
    MCRegUnit RU = *TRI.regunits(CI->second.DefRegs[0]).begin();
273
4.48k
    return findCopyForUnit(RU, TRI, true);
274
4.60k
  }
275
276
  MachineInstr *findAvailBackwardCopy(MachineInstr &I, MCRegister Reg,
277
                                      const TargetRegisterInfo &TRI,
278
                                      const TargetInstrInfo &TII,
279
206k
                                      bool UseCopyInstr) {
280
206k
    MCRegUnit RU = *TRI.regunits(Reg).begin();
281
206k
    MachineInstr *AvailCopy = findCopyDefViaUnit(RU, TRI);
282
283
206k
    if (!AvailCopy)
284
201k
      return nullptr;
285
286
4.46k
    std::optional<DestSourcePair> CopyOperands =
287
4.46k
        isCopyInstr(*AvailCopy, TII, UseCopyInstr);
288
4.46k
    Register AvailSrc = CopyOperands->Source->getReg();
289
4.46k
    Register AvailDef = CopyOperands->Destination->getReg();
290
4.46k
    if (!TRI.isSubRegisterEq(AvailSrc, Reg))
291
27
      return nullptr;
292
293
4.43k
    for (const MachineInstr &MI :
294
4.43k
         make_range(AvailCopy->getReverseIterator(), I.getReverseIterator()))
295
10.8k
      for (const MachineOperand &MO : MI.operands())
296
31.2k
        if (MO.isRegMask())
297
          // FIXME: Shall we simultaneously invalidate AvailSrc or AvailDef?
298
571
          if (MO.clobbersPhysReg(AvailSrc) || MO.clobbersPhysReg(AvailDef))
299
532
            return nullptr;
300
301
3.90k
    return AvailCopy;
302
4.43k
  }
303
304
  MachineInstr *findAvailCopy(MachineInstr &DestCopy, MCRegister Reg,
305
                              const TargetRegisterInfo &TRI,
306
4.56M
                              const TargetInstrInfo &TII, bool UseCopyInstr) {
307
    // We check the first RegUnit here, since we'll only be interested in the
308
    // copy if it copies the entire register anyway.
309
4.56M
    MCRegUnit RU = *TRI.regunits(Reg).begin();
310
4.56M
    MachineInstr *AvailCopy =
311
4.56M
        findCopyForUnit(RU, TRI, /*MustBeAvailable=*/true);
312
313
4.56M
    if (!AvailCopy)
314
4.43M
      return nullptr;
315
316
134k
    std::optional<DestSourcePair> CopyOperands =
317
134k
        isCopyInstr(*AvailCopy, TII, UseCopyInstr);
318
134k
    Register AvailSrc = CopyOperands->Source->getReg();
319
134k
    Register AvailDef = CopyOperands->Destination->getReg();
320
134k
    if (!TRI.isSubRegisterEq(AvailDef, Reg))
321
11.0k
      return nullptr;
322
323
    // Check that the available copy isn't clobbered by any regmasks between
324
    // itself and the destination.
325
123k
    for (const MachineInstr &MI :
326
123k
         make_range(AvailCopy->getIterator(), DestCopy.getIterator()))
327
668k
      for (const MachineOperand &MO : MI.operands())
328
1.92M
        if (MO.isRegMask())
329
61.8k
          if (MO.clobbersPhysReg(AvailSrc) || MO.clobbersPhysReg(AvailDef))
330
61.0k
            return nullptr;
331
332
62.3k
    return AvailCopy;
333
123k
  }
334
335
  // Find last COPY that defines Reg before Current MachineInstr.
336
  MachineInstr *findLastSeenDefInCopy(const MachineInstr &Current,
337
                                      MCRegister Reg,
338
                                      const TargetRegisterInfo &TRI,
339
                                      const TargetInstrInfo &TII,
340
5.54M
                                      bool UseCopyInstr) {
341
5.54M
    MCRegUnit RU = *TRI.regunits(Reg).begin();
342
5.54M
    auto CI = Copies.find(RU);
343
5.54M
    if (CI == Copies.end() || !CI->second.Avail)
344
5.48M
      return nullptr;
345
346
55.5k
    MachineInstr *DefCopy = CI->second.MI;
347
55.5k
    std::optional<DestSourcePair> CopyOperands =
348
55.5k
        isCopyInstr(*DefCopy, TII, UseCopyInstr);
349
55.5k
    Register Def = CopyOperands->Destination->getReg();
350
55.5k
    if (!TRI.isSubRegisterEq(Def, Reg))
351
166
      return nullptr;
352
353
55.4k
    for (const MachineInstr &MI :
354
55.4k
         make_range(static_cast<const MachineInstr *>(DefCopy)->getIterator(),
355
55.4k
                    Current.getIterator()))
356
791k
      for (const MachineOperand &MO : MI.operands())
357
2.50M
        if (MO.isRegMask())
358
10.3k
          if (MO.clobbersPhysReg(Def)) {
359
0
            LLVM_DEBUG(dbgs() << "MCP: Removed tracking of "
360
0
                              << printReg(Def, &TRI) << "\n");
361
0
            return nullptr;
362
0
          }
363
364
55.4k
    return DefCopy;
365
55.4k
  }
366
367
  // Find last COPY that uses Reg.
368
  MachineInstr *findLastSeenUseInCopy(MCRegister Reg,
369
5.49M
                                      const TargetRegisterInfo &TRI) {
370
5.49M
    MCRegUnit RU = *TRI.regunits(Reg).begin();
371
5.49M
    auto CI = Copies.find(RU);
372
5.49M
    if (CI == Copies.end())
373
5.36M
      return nullptr;
374
132k
    return CI->second.LastSeenUseInCopy;
375
5.49M
  }
376
377
888k
  void clear() {
378
888k
    Copies.clear();
379
888k
  }
380
};
381
382
class MachineCopyPropagation : public MachineFunctionPass {
383
  const TargetRegisterInfo *TRI = nullptr;
384
  const TargetInstrInfo *TII = nullptr;
385
  const MachineRegisterInfo *MRI = nullptr;
386
387
  // Return true if this is a copy instruction and false otherwise.
388
  bool UseCopyInstr;
389
390
public:
391
  static char ID; // Pass identification, replacement for typeid
392
393
  MachineCopyPropagation(bool CopyInstr = false)
394
72.6k
      : MachineFunctionPass(ID), UseCopyInstr(CopyInstr || MCPUseCopyInstr) {
395
72.6k
    initializeMachineCopyPropagationPass(*PassRegistry::getPassRegistry());
396
72.6k
  }
397
398
72.6k
  void getAnalysisUsage(AnalysisUsage &AU) const override {
399
72.6k
    AU.setPreservesCFG();
400
72.6k
    MachineFunctionPass::getAnalysisUsage(AU);
401
72.6k
  }
402
403
  bool runOnMachineFunction(MachineFunction &MF) override;
404
405
72.6k
  MachineFunctionProperties getRequiredProperties() const override {
406
72.6k
    return MachineFunctionProperties().set(
407
72.6k
        MachineFunctionProperties::Property::NoVRegs);
408
72.6k
  }
409
410
private:
411
  typedef enum { DebugUse = false, RegularUse = true } DebugType;
412
413
  void ReadRegister(MCRegister Reg, MachineInstr &Reader, DebugType DT);
414
  void ForwardCopyPropagateBlock(MachineBasicBlock &MBB);
415
  void BackwardCopyPropagateBlock(MachineBasicBlock &MBB);
416
  void EliminateSpillageCopies(MachineBasicBlock &MBB);
417
  bool eraseIfRedundant(MachineInstr &Copy, MCRegister Src, MCRegister Def);
418
  void forwardUses(MachineInstr &MI);
419
  void propagateDefs(MachineInstr &MI);
420
  bool isForwardableRegClassCopy(const MachineInstr &Copy,
421
                                 const MachineInstr &UseI, unsigned UseIdx);
422
  bool isBackwardPropagatableRegClassCopy(const MachineInstr &Copy,
423
                                          const MachineInstr &UseI,
424
                                          unsigned UseIdx);
425
  bool hasImplicitOverlap(const MachineInstr &MI, const MachineOperand &Use);
426
  bool hasOverlappingMultipleDef(const MachineInstr &MI,
427
                                 const MachineOperand &MODef, Register Def);
428
429
  /// Candidates for deletion.
430
  SmallSetVector<MachineInstr *, 8> MaybeDeadCopies;
431
432
  /// Multimap tracking debug users in current BB
433
  DenseMap<MachineInstr *, SmallSet<MachineInstr *, 2>> CopyDbgUsers;
434
435
  CopyTracker Tracker;
436
437
  bool Changed = false;
438
};
439
440
} // end anonymous namespace
441
442
char MachineCopyPropagation::ID = 0;
443
444
char &llvm::MachineCopyPropagationID = MachineCopyPropagation::ID;
445
446
INITIALIZE_PASS(MachineCopyPropagation, DEBUG_TYPE,
447
                "Machine Copy Propagation Pass", false, false)
448
449
void MachineCopyPropagation::ReadRegister(MCRegister Reg, MachineInstr &Reader,
450
11.4M
                                          DebugType DT) {
451
  // If 'Reg' is defined by a copy, the copy is no longer a candidate
452
  // for elimination. If a copy is "read" by a debug user, record the user
453
  // for propagation.
454
12.4M
  for (MCRegUnit Unit : TRI->regunits(Reg)) {
455
12.4M
    if (MachineInstr *Copy = Tracker.findCopyForUnit(Unit, *TRI)) {
456
823k
      if (DT == RegularUse) {
457
823k
        LLVM_DEBUG(dbgs() << "MCP: Copy is used - not dead: "; Copy->dump());
458
823k
        MaybeDeadCopies.remove(Copy);
459
823k
      } else {
460
361
        CopyDbgUsers[Copy].insert(&Reader);
461
361
      }
462
823k
    }
463
12.4M
  }
464
11.4M
}
465
466
/// Return true if \p PreviousCopy did copy register \p Src to register \p Def.
467
/// This fact may have been obscured by sub register usage or may not be true at
468
/// all even though Src and Def are subregisters of the registers used in
469
/// PreviousCopy. e.g.
470
/// isNopCopy("ecx = COPY eax", AX, CX) == true
471
/// isNopCopy("ecx = COPY eax", AH, CL) == false
472
static bool isNopCopy(const MachineInstr &PreviousCopy, MCRegister Src,
473
                      MCRegister Def, const TargetRegisterInfo *TRI,
474
8.82k
                      const TargetInstrInfo *TII, bool UseCopyInstr) {
475
476
8.82k
  std::optional<DestSourcePair> CopyOperands =
477
8.82k
      isCopyInstr(PreviousCopy, *TII, UseCopyInstr);
478
8.82k
  MCRegister PreviousSrc = CopyOperands->Source->getReg().asMCReg();
479
8.82k
  MCRegister PreviousDef = CopyOperands->Destination->getReg().asMCReg();
480
8.82k
  if (Src == PreviousSrc && Def == PreviousDef)
481
2.16k
    return true;
482
6.66k
  if (!TRI->isSubRegister(PreviousSrc, Src))
483
6.66k
    return false;
484
2
  unsigned SubIdx = TRI->getSubRegIndex(PreviousSrc, Src);
485
2
  return SubIdx == TRI->getSubRegIndex(PreviousDef, Def);
486
6.66k
}
487
488
/// Remove instruction \p Copy if there exists a previous copy that copies the
489
/// register \p Src to the register \p Def; This may happen indirectly by
490
/// copying the super registers.
491
bool MachineCopyPropagation::eraseIfRedundant(MachineInstr &Copy,
492
1.13M
                                              MCRegister Src, MCRegister Def) {
493
  // Avoid eliminating a copy from/to a reserved registers as we cannot predict
494
  // the value (Example: The sparc zero register is writable but stays zero).
495
1.13M
  if (MRI->isReserved(Src) || MRI->isReserved(Def))
496
160k
    return false;
497
498
  // Search for an existing copy.
499
977k
  MachineInstr *PrevCopy =
500
977k
      Tracker.findAvailCopy(Copy, Def, *TRI, *TII, UseCopyInstr);
501
977k
  if (!PrevCopy)
502
968k
    return false;
503
504
8.82k
  auto PrevCopyOperands = isCopyInstr(*PrevCopy, *TII, UseCopyInstr);
505
  // Check that the existing copy uses the correct sub registers.
506
8.82k
  if (PrevCopyOperands->Destination->isDead())
507
0
    return false;
508
8.82k
  if (!isNopCopy(*PrevCopy, Src, Def, TRI, TII, UseCopyInstr))
509
6.66k
    return false;
510
511
2.16k
  LLVM_DEBUG(dbgs() << "MCP: copy is a NOP, removing: "; Copy.dump());
512
513
  // Copy was redundantly redefining either Src or Def. Remove earlier kill
514
  // flags between Copy and PrevCopy because the value will be reused now.
515
2.16k
  std::optional<DestSourcePair> CopyOperands =
516
2.16k
      isCopyInstr(Copy, *TII, UseCopyInstr);
517
2.16k
  assert(CopyOperands);
518
519
0
  Register CopyDef = CopyOperands->Destination->getReg();
520
2.16k
  assert(CopyDef == Src || CopyDef == Def);
521
0
  for (MachineInstr &MI :
522
2.16k
       make_range(PrevCopy->getIterator(), Copy.getIterator()))
523
12.0k
    MI.clearRegisterKills(CopyDef, TRI);
524
525
  // Clear undef flag from remaining copy if needed.
526
2.16k
  if (!CopyOperands->Source->isUndef()) {
527
2.16k
    PrevCopy->getOperand(PrevCopyOperands->Source->getOperandNo())
528
2.16k
        .setIsUndef(false);
529
2.16k
  }
530
531
2.16k
  Copy.eraseFromParent();
532
2.16k
  Changed = true;
533
2.16k
  ++NumDeletes;
534
2.16k
  return true;
535
8.82k
}
536
537
bool MachineCopyPropagation::isBackwardPropagatableRegClassCopy(
538
3.68k
    const MachineInstr &Copy, const MachineInstr &UseI, unsigned UseIdx) {
539
3.68k
  std::optional<DestSourcePair> CopyOperands =
540
3.68k
      isCopyInstr(Copy, *TII, UseCopyInstr);
541
3.68k
  Register Def = CopyOperands->Destination->getReg();
542
543
3.68k
  if (const TargetRegisterClass *URC =
544
3.68k
          UseI.getRegClassConstraint(UseIdx, TII, TRI))
545
3.61k
    return URC->contains(Def);
546
547
  // We don't process further if UseI is a COPY, since forward copy propagation
548
  // should handle that.
549
72
  return false;
550
3.68k
}
551
552
/// Decide whether we should forward the source of \param Copy to its use in
553
/// \param UseI based on the physical register class constraints of the opcode
554
/// and avoiding introducing more cross-class COPYs.
555
bool MachineCopyPropagation::isForwardableRegClassCopy(const MachineInstr &Copy,
556
                                                       const MachineInstr &UseI,
557
22.8k
                                                       unsigned UseIdx) {
558
22.8k
  std::optional<DestSourcePair> CopyOperands =
559
22.8k
      isCopyInstr(Copy, *TII, UseCopyInstr);
560
22.8k
  Register CopySrcReg = CopyOperands->Source->getReg();
561
562
  // If the new register meets the opcode register constraints, then allow
563
  // forwarding.
564
22.8k
  if (const TargetRegisterClass *URC =
565
22.8k
          UseI.getRegClassConstraint(UseIdx, TII, TRI))
566
20.3k
    return URC->contains(CopySrcReg);
567
568
2.43k
  auto UseICopyOperands = isCopyInstr(UseI, *TII, UseCopyInstr);
569
2.43k
  if (!UseICopyOperands)
570
410
    return false;
571
572
  /// COPYs don't have register class constraints, so if the user instruction
573
  /// is a COPY, we just try to avoid introducing additional cross-class
574
  /// COPYs.  For example:
575
  ///
576
  ///   RegClassA = COPY RegClassB  // Copy parameter
577
  ///   ...
578
  ///   RegClassB = COPY RegClassA  // UseI parameter
579
  ///
580
  /// which after forwarding becomes
581
  ///
582
  ///   RegClassA = COPY RegClassB
583
  ///   ...
584
  ///   RegClassB = COPY RegClassB
585
  ///
586
  /// so we have reduced the number of cross-class COPYs and potentially
587
  /// introduced a nop COPY that can be removed.
588
589
  // Allow forwarding if src and dst belong to any common class, so long as they
590
  // don't belong to any (possibly smaller) common class that requires copies to
591
  // go via a different class.
592
2.02k
  Register UseDstReg = UseICopyOperands->Destination->getReg();
593
2.02k
  bool Found = false;
594
2.02k
  bool IsCrossClass = false;
595
214k
  for (const TargetRegisterClass *RC : TRI->regclasses()) {
596
214k
    if (RC->contains(CopySrcReg) && RC->contains(UseDstReg)) {
597
14.1k
      Found = true;
598
14.1k
      if (TRI->getCrossCopyRegClass(RC) != RC) {
599
0
        IsCrossClass = true;
600
0
        break;
601
0
      }
602
14.1k
    }
603
214k
  }
604
2.02k
  if (!Found)
605
169
    return false;
606
1.85k
  if (!IsCrossClass)
607
1.85k
    return true;
608
  // The forwarded copy would be cross-class. Only do this if the original copy
609
  // was also cross-class.
610
0
  Register CopyDstReg = CopyOperands->Destination->getReg();
611
0
  for (const TargetRegisterClass *RC : TRI->regclasses()) {
612
0
    if (RC->contains(CopySrcReg) && RC->contains(CopyDstReg) &&
613
0
        TRI->getCrossCopyRegClass(RC) != RC)
614
0
      return true;
615
0
  }
616
0
  return false;
617
0
}
618
619
/// Check that \p MI does not have implicit uses that overlap with it's \p Use
620
/// operand (the register being replaced), since these can sometimes be
621
/// implicitly tied to other operands.  For example, on AMDGPU:
622
///
623
/// V_MOVRELS_B32_e32 %VGPR2, %M0<imp-use>, %EXEC<imp-use>, %VGPR2_VGPR3_VGPR4_VGPR5<imp-use>
624
///
625
/// the %VGPR2 is implicitly tied to the larger reg operand, but we have no
626
/// way of knowing we need to update the latter when updating the former.
627
bool MachineCopyPropagation::hasImplicitOverlap(const MachineInstr &MI,
628
17.3k
                                                const MachineOperand &Use) {
629
17.3k
  for (const MachineOperand &MIUse : MI.uses())
630
47.7k
    if (&MIUse != &Use && MIUse.isReg() && MIUse.isImplicit() &&
631
47.7k
        MIUse.isUse() && TRI->regsOverlap(Use.getReg(), MIUse.getReg()))
632
43
      return true;
633
634
17.2k
  return false;
635
17.3k
}
636
637
/// For an MI that has multiple definitions, check whether \p MI has
638
/// a definition that overlaps with another of its definitions.
639
/// For example, on ARM: umull   r9, r9, lr, r0
640
/// The umull instruction is unpredictable unless RdHi and RdLo are different.
641
bool MachineCopyPropagation::hasOverlappingMultipleDef(
642
163
    const MachineInstr &MI, const MachineOperand &MODef, Register Def) {
643
163
  for (const MachineOperand &MIDef : MI.defs()) {
644
163
    if ((&MIDef != &MODef) && MIDef.isReg() &&
645
163
        TRI->regsOverlap(Def, MIDef.getReg()))
646
0
      return true;
647
163
  }
648
649
163
  return false;
650
163
}
651
652
/// Look for available copies whose destination register is used by \p MI and
653
/// replace the use in \p MI with the copy's source register.
654
8.89M
void MachineCopyPropagation::forwardUses(MachineInstr &MI) {
655
8.89M
  if (!Tracker.hasAnyCopies())
656
4.39M
    return;
657
658
  // Look for non-tied explicit vreg uses that have an active COPY
659
  // instruction that defines the physical register allocated to them.
660
  // Replace the vreg with the source of the active COPY.
661
19.9M
  for (unsigned OpIdx = 0, OpEnd = MI.getNumOperands(); OpIdx < OpEnd;
662
15.4M
       ++OpIdx) {
663
15.4M
    MachineOperand &MOUse = MI.getOperand(OpIdx);
664
    // Don't forward into undef use operands since doing so can cause problems
665
    // with the machine verifier, since it doesn't treat undef reads as reads,
666
    // so we can end up with a live range that ends on an undef read, leading to
667
    // an error that the live range doesn't end on a read of the live range
668
    // register.
669
15.4M
    if (!MOUse.isReg() || MOUse.isTied() || MOUse.isUndef() || MOUse.isDef() ||
670
15.4M
        MOUse.isImplicit())
671
10.1M
      continue;
672
673
5.24M
    if (!MOUse.getReg())
674
453k
      continue;
675
676
    // Check that the register is marked 'renamable' so we know it is safe to
677
    // rename it without violating any constraints that aren't expressed in the
678
    // IR (e.g. ABI or opcode requirements).
679
4.78M
    if (!MOUse.isRenamable())
680
1.19M
      continue;
681
682
3.58M
    MachineInstr *Copy = Tracker.findAvailCopy(MI, MOUse.getReg().asMCReg(),
683
3.58M
                                               *TRI, *TII, UseCopyInstr);
684
3.58M
    if (!Copy)
685
3.53M
      continue;
686
687
53.5k
    std::optional<DestSourcePair> CopyOperands =
688
53.5k
        isCopyInstr(*Copy, *TII, UseCopyInstr);
689
53.5k
    Register CopyDstReg = CopyOperands->Destination->getReg();
690
53.5k
    const MachineOperand &CopySrc = *CopyOperands->Source;
691
53.5k
    Register CopySrcReg = CopySrc.getReg();
692
693
53.5k
    Register ForwardedReg = CopySrcReg;
694
    // MI might use a sub-register of the Copy destination, in which case the
695
    // forwarded register is the matching sub-register of the Copy source.
696
53.5k
    if (MOUse.getReg() != CopyDstReg) {
697
2.03k
      unsigned SubRegIdx = TRI->getSubRegIndex(CopyDstReg, MOUse.getReg());
698
2.03k
      assert(SubRegIdx &&
699
2.03k
             "MI source is not a sub-register of Copy destination");
700
0
      ForwardedReg = TRI->getSubReg(CopySrcReg, SubRegIdx);
701
2.03k
      if (!ForwardedReg) {
702
110
        LLVM_DEBUG(dbgs() << "MCP: Copy source does not have sub-register "
703
110
                          << TRI->getSubRegIndexName(SubRegIdx) << '\n');
704
110
        continue;
705
110
      }
706
2.03k
    }
707
708
    // Don't forward COPYs of reserved regs unless they are constant.
709
53.4k
    if (MRI->isReserved(CopySrcReg) && !MRI->isConstantPhysReg(CopySrcReg))
710
30.6k
      continue;
711
712
22.8k
    if (!isForwardableRegClassCopy(*Copy, MI, OpIdx))
713
5.62k
      continue;
714
715
17.1k
    if (hasImplicitOverlap(MI, MOUse))
716
43
      continue;
717
718
    // Check that the instruction is not a copy that partially overwrites the
719
    // original copy source that we are about to use. The tracker mechanism
720
    // cannot cope with that.
721
17.1k
    if (isCopyInstr(MI, *TII, UseCopyInstr) &&
722
17.1k
        MI.modifiesRegister(CopySrcReg, TRI) &&
723
17.1k
        !MI.definesRegister(CopySrcReg)) {
724
0
      LLVM_DEBUG(dbgs() << "MCP: Copy source overlap with dest in " << MI);
725
0
      continue;
726
0
    }
727
728
17.1k
    if (!DebugCounter::shouldExecute(FwdCounter)) {
729
0
      LLVM_DEBUG(dbgs() << "MCP: Skipping forwarding due to debug counter:\n  "
730
0
                        << MI);
731
0
      continue;
732
0
    }
733
734
17.1k
    LLVM_DEBUG(dbgs() << "MCP: Replacing " << printReg(MOUse.getReg(), TRI)
735
17.1k
                      << "\n     with " << printReg(ForwardedReg, TRI)
736
17.1k
                      << "\n     in " << MI << "     from " << *Copy);
737
738
17.1k
    MOUse.setReg(ForwardedReg);
739
740
17.1k
    if (!CopySrc.isRenamable())
741
8.43k
      MOUse.setIsRenamable(false);
742
17.1k
    MOUse.setIsUndef(CopySrc.isUndef());
743
744
17.1k
    LLVM_DEBUG(dbgs() << "MCP: After replacement: " << MI << "\n");
745
746
    // Clear kill markers that may have been invalidated.
747
17.1k
    for (MachineInstr &KMI :
748
17.1k
         make_range(Copy->getIterator(), std::next(MI.getIterator())))
749
118k
      KMI.clearRegisterKills(CopySrcReg, TRI);
750
751
17.1k
    ++NumCopyForwards;
752
17.1k
    Changed = true;
753
17.1k
  }
754
4.50M
}
755
756
387k
void MachineCopyPropagation::ForwardCopyPropagateBlock(MachineBasicBlock &MBB) {
757
387k
  LLVM_DEBUG(dbgs() << "MCP: ForwardCopyPropagateBlock " << MBB.getName()
758
387k
                    << "\n");
759
760
8.89M
  for (MachineInstr &MI : llvm::make_early_inc_range(MBB)) {
761
    // Analyze copies (which don't overlap themselves).
762
8.89M
    std::optional<DestSourcePair> CopyOperands =
763
8.89M
        isCopyInstr(MI, *TII, UseCopyInstr);
764
8.89M
    if (CopyOperands) {
765
766
570k
      Register RegSrc = CopyOperands->Source->getReg();
767
570k
      Register RegDef = CopyOperands->Destination->getReg();
768
769
570k
      if (!TRI->regsOverlap(RegDef, RegSrc)) {
770
569k
        assert(RegDef.isPhysical() && RegSrc.isPhysical() &&
771
569k
              "MachineCopyPropagation should be run after register allocation!");
772
773
0
        MCRegister Def = RegDef.asMCReg();
774
569k
        MCRegister Src = RegSrc.asMCReg();
775
776
        // The two copies cancel out and the source of the first copy
777
        // hasn't been overridden, eliminate the second one. e.g.
778
        //  %ecx = COPY %eax
779
        //  ... nothing clobbered eax.
780
        //  %eax = COPY %ecx
781
        // =>
782
        //  %ecx = COPY %eax
783
        //
784
        // or
785
        //
786
        //  %ecx = COPY %eax
787
        //  ... nothing clobbered eax.
788
        //  %ecx = COPY %eax
789
        // =>
790
        //  %ecx = COPY %eax
791
569k
        if (eraseIfRedundant(MI, Def, Src) || eraseIfRedundant(MI, Src, Def))
792
2.16k
          continue;
793
794
567k
        forwardUses(MI);
795
796
        // Src may have been changed by forwardUses()
797
567k
        CopyOperands = isCopyInstr(MI, *TII, UseCopyInstr);
798
567k
        Src = CopyOperands->Source->getReg().asMCReg();
799
800
        // If Src is defined by a previous copy, the previous copy cannot be
801
        // eliminated.
802
567k
        ReadRegister(Src, MI, RegularUse);
803
567k
        for (const MachineOperand &MO : MI.implicit_operands()) {
804
5.18k
          if (!MO.isReg() || !MO.readsReg())
805
3.03k
            continue;
806
2.15k
          MCRegister Reg = MO.getReg().asMCReg();
807
2.15k
          if (!Reg)
808
0
            continue;
809
2.15k
          ReadRegister(Reg, MI, RegularUse);
810
2.15k
        }
811
812
567k
        LLVM_DEBUG(dbgs() << "MCP: Copy is a deletion candidate: "; MI.dump());
813
814
        // Copy is now a candidate for deletion.
815
567k
        if (!MRI->isReserved(Def))
816
537k
          MaybeDeadCopies.insert(&MI);
817
818
        // If 'Def' is previously source of another copy, then this earlier copy's
819
        // source is no longer available. e.g.
820
        // %xmm9 = copy %xmm2
821
        // ...
822
        // %xmm2 = copy %xmm0
823
        // ...
824
        // %xmm2 = copy %xmm9
825
567k
        Tracker.clobberRegister(Def, *TRI, *TII, UseCopyInstr);
826
567k
        for (const MachineOperand &MO : MI.implicit_operands()) {
827
5.18k
          if (!MO.isReg() || !MO.isDef())
828
2.15k
            continue;
829
3.03k
          MCRegister Reg = MO.getReg().asMCReg();
830
3.03k
          if (!Reg)
831
0
            continue;
832
3.03k
          Tracker.clobberRegister(Reg, *TRI, *TII, UseCopyInstr);
833
3.03k
        }
834
835
567k
        Tracker.trackCopy(&MI, *TRI, *TII, UseCopyInstr);
836
837
567k
        continue;
838
569k
      }
839
570k
    }
840
841
    // Clobber any earlyclobber regs first.
842
8.32M
    for (const MachineOperand &MO : MI.operands())
843
28.1M
      if (MO.isReg() && MO.isEarlyClobber()) {
844
18.9k
        MCRegister Reg = MO.getReg().asMCReg();
845
        // If we have a tied earlyclobber, that means it is also read by this
846
        // instruction, so we need to make sure we don't remove it as dead
847
        // later.
848
18.9k
        if (MO.isTied())
849
15.1k
          ReadRegister(Reg, MI, RegularUse);
850
18.9k
        Tracker.clobberRegister(Reg, *TRI, *TII, UseCopyInstr);
851
18.9k
      }
852
853
8.32M
    forwardUses(MI);
854
855
    // Not a copy.
856
8.32M
    SmallVector<Register, 2> Defs;
857
8.32M
    const MachineOperand *RegMask = nullptr;
858
28.1M
    for (const MachineOperand &MO : MI.operands()) {
859
28.1M
      if (MO.isRegMask())
860
218k
        RegMask = &MO;
861
28.1M
      if (!MO.isReg())
862
9.51M
        continue;
863
18.6M
      Register Reg = MO.getReg();
864
18.6M
      if (!Reg)
865
714k
        continue;
866
867
17.9M
      assert(!Reg.isVirtual() &&
868
17.9M
             "MachineCopyPropagation should be run after register allocation!");
869
870
17.9M
      if (MO.isDef() && !MO.isEarlyClobber()) {
871
6.79M
        Defs.push_back(Reg.asMCReg());
872
6.79M
        continue;
873
11.1M
      } else if (MO.readsReg())
874
10.8M
        ReadRegister(Reg.asMCReg(), MI, MO.isDebug() ? DebugUse : RegularUse);
875
17.9M
    }
876
877
    // The instruction has a register mask operand which means that it clobbers
878
    // a large set of registers.  Treat clobbered registers the same way as
879
    // defined registers.
880
8.32M
    if (RegMask) {
881
      // Erase any MaybeDeadCopies whose destination register is clobbered.
882
218k
      for (SmallSetVector<MachineInstr *, 8>::iterator DI =
883
218k
               MaybeDeadCopies.begin();
884
392k
           DI != MaybeDeadCopies.end();) {
885
173k
        MachineInstr *MaybeDead = *DI;
886
173k
        std::optional<DestSourcePair> CopyOperands =
887
173k
            isCopyInstr(*MaybeDead, *TII, UseCopyInstr);
888
173k
        MCRegister Reg = CopyOperands->Destination->getReg().asMCReg();
889
173k
        assert(!MRI->isReserved(Reg));
890
891
173k
        if (!RegMask->clobbersPhysReg(Reg)) {
892
173k
          ++DI;
893
173k
          continue;
894
173k
        }
895
896
176
        LLVM_DEBUG(dbgs() << "MCP: Removing copy due to regmask clobbering: ";
897
176
                   MaybeDead->dump());
898
899
        // Make sure we invalidate any entries in the copy maps before erasing
900
        // the instruction.
901
176
        Tracker.clobberRegister(Reg, *TRI, *TII, UseCopyInstr);
902
903
        // erase() will return the next valid iterator pointing to the next
904
        // element after the erased one.
905
176
        DI = MaybeDeadCopies.erase(DI);
906
176
        MaybeDead->eraseFromParent();
907
176
        Changed = true;
908
176
        ++NumDeletes;
909
176
      }
910
218k
    }
911
912
    // Any previous copy definition or reading the Defs is no longer available.
913
8.32M
    for (MCRegister Reg : Defs)
914
6.79M
      Tracker.clobberRegister(Reg, *TRI, *TII, UseCopyInstr);
915
8.32M
  }
916
917
  // If MBB doesn't have successors, delete the copies whose defs are not used.
918
  // If MBB does have successors, then conservative assume the defs are live-out
919
  // since we don't want to trust live-in lists.
920
387k
  if (MBB.succ_empty()) {
921
219k
    for (MachineInstr *MaybeDead : MaybeDeadCopies) {
922
759
      LLVM_DEBUG(dbgs() << "MCP: Removing copy due to no live-out succ: ";
923
759
                 MaybeDead->dump());
924
925
759
      std::optional<DestSourcePair> CopyOperands =
926
759
          isCopyInstr(*MaybeDead, *TII, UseCopyInstr);
927
759
      assert(CopyOperands);
928
929
0
      Register SrcReg = CopyOperands->Source->getReg();
930
759
      Register DestReg = CopyOperands->Destination->getReg();
931
759
      assert(!MRI->isReserved(DestReg));
932
933
      // Update matching debug values, if any.
934
0
      SmallVector<MachineInstr *> MaybeDeadDbgUsers(
935
759
          CopyDbgUsers[MaybeDead].begin(), CopyDbgUsers[MaybeDead].end());
936
759
      MRI->updateDbgUsersToReg(DestReg.asMCReg(), SrcReg.asMCReg(),
937
759
                               MaybeDeadDbgUsers);
938
939
759
      MaybeDead->eraseFromParent();
940
759
      Changed = true;
941
759
      ++NumDeletes;
942
759
    }
943
219k
  }
944
945
387k
  MaybeDeadCopies.clear();
946
387k
  CopyDbgUsers.clear();
947
387k
  Tracker.clear();
948
387k
}
949
950
static bool isBackwardPropagatableCopy(const DestSourcePair &CopyOperands,
951
                                       const MachineRegisterInfo &MRI,
952
478k
                                       const TargetInstrInfo &TII) {
953
478k
  Register Def = CopyOperands.Destination->getReg();
954
478k
  Register Src = CopyOperands.Source->getReg();
955
956
478k
  if (!Def || !Src)
957
0
    return false;
958
959
478k
  if (MRI.isReserved(Def) || MRI.isReserved(Src))
960
72.8k
    return false;
961
962
405k
  return CopyOperands.Source->isRenamable() && CopyOperands.Source->isKill();
963
478k
}
964
965
8.76M
void MachineCopyPropagation::propagateDefs(MachineInstr &MI) {
966
8.76M
  if (!Tracker.hasAnyCopies())
967
8.32M
    return;
968
969
2.05M
  for (unsigned OpIdx = 0, OpEnd = MI.getNumOperands(); OpIdx != OpEnd;
970
1.61M
       ++OpIdx) {
971
1.61M
    MachineOperand &MODef = MI.getOperand(OpIdx);
972
973
1.61M
    if (!MODef.isReg() || MODef.isUse())
974
1.21M
      continue;
975
976
    // Ignore non-trivial cases.
977
404k
    if (MODef.isTied() || MODef.isUndef() || MODef.isImplicit())
978
157k
      continue;
979
980
246k
    if (!MODef.getReg())
981
0
      continue;
982
983
    // We only handle if the register comes from a vreg.
984
246k
    if (!MODef.isRenamable())
985
40.2k
      continue;
986
987
206k
    MachineInstr *Copy = Tracker.findAvailBackwardCopy(
988
206k
        MI, MODef.getReg().asMCReg(), *TRI, *TII, UseCopyInstr);
989
206k
    if (!Copy)
990
202k
      continue;
991
992
3.90k
    std::optional<DestSourcePair> CopyOperands =
993
3.90k
        isCopyInstr(*Copy, *TII, UseCopyInstr);
994
3.90k
    Register Def = CopyOperands->Destination->getReg();
995
3.90k
    Register Src = CopyOperands->Source->getReg();
996
997
3.90k
    if (MODef.getReg() != Src)
998
216
      continue;
999
1000
3.68k
    if (!isBackwardPropagatableRegClassCopy(*Copy, MI, OpIdx))
1001
3.52k
      continue;
1002
1003
163
    if (hasImplicitOverlap(MI, MODef))
1004
0
      continue;
1005
1006
163
    if (hasOverlappingMultipleDef(MI, MODef, Def))
1007
0
      continue;
1008
1009
163
    LLVM_DEBUG(dbgs() << "MCP: Replacing " << printReg(MODef.getReg(), TRI)
1010
163
                      << "\n     with " << printReg(Def, TRI) << "\n     in "
1011
163
                      << MI << "     from " << *Copy);
1012
1013
163
    MODef.setReg(Def);
1014
163
    MODef.setIsRenamable(CopyOperands->Destination->isRenamable());
1015
1016
163
    LLVM_DEBUG(dbgs() << "MCP: After replacement: " << MI << "\n");
1017
163
    MaybeDeadCopies.insert(Copy);
1018
163
    Changed = true;
1019
163
    ++NumCopyBackwardPropagated;
1020
163
  }
1021
440k
}
1022
1023
void MachineCopyPropagation::BackwardCopyPropagateBlock(
1024
387k
    MachineBasicBlock &MBB) {
1025
387k
  LLVM_DEBUG(dbgs() << "MCP: BackwardCopyPropagateBlock " << MBB.getName()
1026
387k
                    << "\n");
1027
1028
8.89M
  for (MachineInstr &MI : llvm::make_early_inc_range(llvm::reverse(MBB))) {
1029
    // Ignore non-trivial COPYs.
1030
8.89M
    std::optional<DestSourcePair> CopyOperands =
1031
8.89M
        isCopyInstr(MI, *TII, UseCopyInstr);
1032
8.89M
    if (CopyOperands && MI.getNumOperands() == 2) {
1033
479k
      Register DefReg = CopyOperands->Destination->getReg();
1034
479k
      Register SrcReg = CopyOperands->Source->getReg();
1035
1036
479k
      if (!TRI->regsOverlap(DefReg, SrcReg)) {
1037
        // Unlike forward cp, we don't invoke propagateDefs here,
1038
        // just let forward cp do COPY-to-COPY propagation.
1039
478k
        if (isBackwardPropagatableCopy(*CopyOperands, *MRI, *TII)) {
1040
134k
          Tracker.invalidateRegister(SrcReg.asMCReg(), *TRI, *TII,
1041
134k
                                     UseCopyInstr);
1042
134k
          Tracker.invalidateRegister(DefReg.asMCReg(), *TRI, *TII,
1043
134k
                                     UseCopyInstr);
1044
134k
          Tracker.trackCopy(&MI, *TRI, *TII, UseCopyInstr);
1045
134k
          continue;
1046
134k
        }
1047
478k
      }
1048
479k
    }
1049
1050
    // Invalidate any earlyclobber regs first.
1051
8.76M
    for (const MachineOperand &MO : MI.operands())
1052
29.1M
      if (MO.isReg() && MO.isEarlyClobber()) {
1053
18.9k
        MCRegister Reg = MO.getReg().asMCReg();
1054
18.9k
        if (!Reg)
1055
0
          continue;
1056
18.9k
        Tracker.invalidateRegister(Reg, *TRI, *TII, UseCopyInstr);
1057
18.9k
      }
1058
1059
8.76M
    propagateDefs(MI);
1060
29.1M
    for (const MachineOperand &MO : MI.operands()) {
1061
29.1M
      if (!MO.isReg())
1062
9.60M
        continue;
1063
1064
19.5M
      if (!MO.getReg())
1065
714k
        continue;
1066
1067
18.8M
      if (MO.isDef())
1068
7.25M
        Tracker.invalidateRegister(MO.getReg().asMCReg(), *TRI, *TII,
1069
7.25M
                                   UseCopyInstr);
1070
1071
18.8M
      if (MO.readsReg()) {
1072
11.3M
        if (MO.isDebug()) {
1073
          //  Check if the register in the debug instruction is utilized
1074
          // in a copy instruction, so we can update the debug info if the
1075
          // register is changed.
1076
2.51k
          for (MCRegUnit Unit : TRI->regunits(MO.getReg().asMCReg())) {
1077
2.51k
            if (auto *Copy = Tracker.findCopyDefViaUnit(Unit, *TRI)) {
1078
21
              CopyDbgUsers[Copy].insert(&MI);
1079
21
            }
1080
2.51k
          }
1081
11.2M
        } else {
1082
11.2M
          Tracker.invalidateRegister(MO.getReg().asMCReg(), *TRI, *TII,
1083
11.2M
                                     UseCopyInstr);
1084
11.2M
        }
1085
11.3M
      }
1086
18.8M
    }
1087
8.76M
  }
1088
1089
387k
  for (auto *Copy : MaybeDeadCopies) {
1090
163
    std::optional<DestSourcePair> CopyOperands =
1091
163
        isCopyInstr(*Copy, *TII, UseCopyInstr);
1092
163
    Register Src = CopyOperands->Source->getReg();
1093
163
    Register Def = CopyOperands->Destination->getReg();
1094
163
    SmallVector<MachineInstr *> MaybeDeadDbgUsers(CopyDbgUsers[Copy].begin(),
1095
163
                                                  CopyDbgUsers[Copy].end());
1096
1097
163
    MRI->updateDbgUsersToReg(Src.asMCReg(), Def.asMCReg(), MaybeDeadDbgUsers);
1098
163
    Copy->eraseFromParent();
1099
163
    ++NumDeletes;
1100
163
  }
1101
1102
387k
  MaybeDeadCopies.clear();
1103
387k
  CopyDbgUsers.clear();
1104
387k
  Tracker.clear();
1105
387k
}
1106
1107
static void LLVM_ATTRIBUTE_UNUSED printSpillReloadChain(
1108
    DenseMap<MachineInstr *, SmallVector<MachineInstr *>> &SpillChain,
1109
    DenseMap<MachineInstr *, SmallVector<MachineInstr *>> &ReloadChain,
1110
0
    MachineInstr *Leader) {
1111
0
  auto &SC = SpillChain[Leader];
1112
0
  auto &RC = ReloadChain[Leader];
1113
0
  for (auto I = SC.rbegin(), E = SC.rend(); I != E; ++I)
1114
0
    (*I)->dump();
1115
0
  for (MachineInstr *MI : RC)
1116
0
    MI->dump();
1117
0
}
1118
1119
// Remove spill-reload like copy chains. For example
1120
// r0 = COPY r1
1121
// r1 = COPY r2
1122
// r2 = COPY r3
1123
// r3 = COPY r4
1124
// <def-use r4>
1125
// r4 = COPY r3
1126
// r3 = COPY r2
1127
// r2 = COPY r1
1128
// r1 = COPY r0
1129
// will be folded into
1130
// r0 = COPY r1
1131
// r1 = COPY r4
1132
// <def-use r4>
1133
// r4 = COPY r1
1134
// r1 = COPY r0
1135
// TODO: Currently we don't track usage of r0 outside the chain, so we
1136
// conservatively keep its value as it was before the rewrite.
1137
//
1138
// The algorithm is trying to keep
1139
// property#1: No Def of spill COPY in the chain is used or defined until the
1140
// paired reload COPY in the chain uses the Def.
1141
//
1142
// property#2: NO Source of COPY in the chain is used or defined until the next
1143
// COPY in the chain defines the Source, except the innermost spill-reload
1144
// pair.
1145
//
1146
// The algorithm is conducted by checking every COPY inside the MBB, assuming
1147
// the COPY is a reload COPY, then try to find paired spill COPY by searching
1148
// the COPY defines the Src of the reload COPY backward. If such pair is found,
1149
// it either belongs to an existing chain or a new chain depends on
1150
// last available COPY uses the Def of the reload COPY.
1151
// Implementation notes, we use CopyTracker::findLastDefCopy(Reg, ...) to find
1152
// out last COPY that defines Reg; we use CopyTracker::findLastUseCopy(Reg, ...)
1153
// to find out last COPY that uses Reg. When we are encountered with a Non-COPY
1154
// instruction, we check registers in the operands of this instruction. If this
1155
// Reg is defined by a COPY, we untrack this Reg via
1156
// CopyTracker::clobberRegister(Reg, ...).
1157
112k
void MachineCopyPropagation::EliminateSpillageCopies(MachineBasicBlock &MBB) {
1158
  // ChainLeader maps MI inside a spill-reload chain to its innermost reload COPY.
1159
  // Thus we can track if a MI belongs to an existing spill-reload chain.
1160
112k
  DenseMap<MachineInstr *, MachineInstr *> ChainLeader;
1161
  // SpillChain maps innermost reload COPY of a spill-reload chain to a sequence
1162
  // of COPYs that forms spills of a spill-reload chain.
1163
  // ReloadChain maps innermost reload COPY of a spill-reload chain to a
1164
  // sequence of COPYs that forms reloads of a spill-reload chain.
1165
112k
  DenseMap<MachineInstr *, SmallVector<MachineInstr *>> SpillChain, ReloadChain;
1166
  // If a COPY's Source has use or def until next COPY defines the Source,
1167
  // we put the COPY in this set to keep property#2.
1168
112k
  DenseSet<const MachineInstr *> CopySourceInvalid;
1169
1170
112k
  auto TryFoldSpillageCopies =
1171
112k
      [&, this](const SmallVectorImpl<MachineInstr *> &SC,
1172
112k
                const SmallVectorImpl<MachineInstr *> &RC) {
1173
389
        assert(SC.size() == RC.size() && "Spill-reload should be paired");
1174
1175
        // We need at least 3 pairs of copies for the transformation to apply,
1176
        // because the first outermost pair cannot be removed since we don't
1177
        // recolor outside of the chain and that we need at least one temporary
1178
        // spill slot to shorten the chain. If we only have a chain of two
1179
        // pairs, we already have the shortest sequence this code can handle:
1180
        // the outermost pair for the temporary spill slot, and the pair that
1181
        // use that temporary spill slot for the other end of the chain.
1182
        // TODO: We might be able to simplify to one spill-reload pair if collecting
1183
        // more infomation about the outermost COPY.
1184
389
        if (SC.size() <= 2)
1185
361
          return;
1186
1187
        // If violate property#2, we don't fold the chain.
1188
28
        for (const MachineInstr *Spill : drop_begin(SC))
1189
228
          if (CopySourceInvalid.count(Spill))
1190
0
            return;
1191
1192
28
        for (const MachineInstr *Reload : drop_end(RC))
1193
228
          if (CopySourceInvalid.count(Reload))
1194
0
            return;
1195
1196
56
        auto CheckCopyConstraint = [this](Register Def, Register Src) {
1197
266
          for (const TargetRegisterClass *RC : TRI->regclasses()) {
1198
266
            if (RC->contains(Def) && RC->contains(Src))
1199
56
              return true;
1200
266
          }
1201
0
          return false;
1202
56
        };
1203
1204
28
        auto UpdateReg = [](MachineInstr *MI, const MachineOperand *Old,
1205
56
                            const MachineOperand *New) {
1206
112
          for (MachineOperand &MO : MI->operands()) {
1207
112
            if (&MO == Old)
1208
56
              MO.setReg(New->getReg());
1209
112
          }
1210
56
        };
1211
1212
28
        std::optional<DestSourcePair> InnerMostSpillCopy =
1213
28
            isCopyInstr(*SC[0], *TII, UseCopyInstr);
1214
28
        std::optional<DestSourcePair> OuterMostSpillCopy =
1215
28
            isCopyInstr(*SC.back(), *TII, UseCopyInstr);
1216
28
        std::optional<DestSourcePair> InnerMostReloadCopy =
1217
28
            isCopyInstr(*RC[0], *TII, UseCopyInstr);
1218
28
        std::optional<DestSourcePair> OuterMostReloadCopy =
1219
28
            isCopyInstr(*RC.back(), *TII, UseCopyInstr);
1220
28
        if (!CheckCopyConstraint(OuterMostSpillCopy->Source->getReg(),
1221
28
                                 InnerMostSpillCopy->Source->getReg()) ||
1222
28
            !CheckCopyConstraint(InnerMostReloadCopy->Destination->getReg(),
1223
28
                                 OuterMostReloadCopy->Destination->getReg()))
1224
0
          return;
1225
1226
28
        SpillageChainsLength += SC.size() + RC.size();
1227
28
        NumSpillageChains += 1;
1228
28
        UpdateReg(SC[0], InnerMostSpillCopy->Destination,
1229
28
                  OuterMostSpillCopy->Source);
1230
28
        UpdateReg(RC[0], InnerMostReloadCopy->Source,
1231
28
                  OuterMostReloadCopy->Destination);
1232
1233
228
        for (size_t I = 1; I < SC.size() - 1; ++I) {
1234
200
          SC[I]->eraseFromParent();
1235
200
          RC[I]->eraseFromParent();
1236
200
          NumDeletes += 2;
1237
200
        }
1238
28
      };
1239
1240
112k
  auto IsFoldableCopy = [this](const MachineInstr &MaybeCopy) {
1241
5.66k
    if (MaybeCopy.getNumImplicitOperands() > 0)
1242
0
      return false;
1243
5.66k
    std::optional<DestSourcePair> CopyOperands =
1244
5.66k
        isCopyInstr(MaybeCopy, *TII, UseCopyInstr);
1245
5.66k
    if (!CopyOperands)
1246
0
      return false;
1247
5.66k
    Register Src = CopyOperands->Source->getReg();
1248
5.66k
    Register Def = CopyOperands->Destination->getReg();
1249
5.66k
    return Src && Def && !TRI->regsOverlap(Src, Def) &&
1250
5.66k
           CopyOperands->Source->isRenamable() &&
1251
5.66k
           CopyOperands->Destination->isRenamable();
1252
5.66k
  };
1253
1254
112k
  auto IsSpillReloadPair = [&, this](const MachineInstr &Spill,
1255
112k
                                     const MachineInstr &Reload) {
1256
3.93k
    if (!IsFoldableCopy(Spill) || !IsFoldableCopy(Reload))
1257
2.79k
      return false;
1258
1.13k
    std::optional<DestSourcePair> SpillCopy =
1259
1.13k
        isCopyInstr(Spill, *TII, UseCopyInstr);
1260
1.13k
    std::optional<DestSourcePair> ReloadCopy =
1261
1.13k
        isCopyInstr(Reload, *TII, UseCopyInstr);
1262
1.13k
    if (!SpillCopy || !ReloadCopy)
1263
0
      return false;
1264
1.13k
    return SpillCopy->Source->getReg() == ReloadCopy->Destination->getReg() &&
1265
1.13k
           SpillCopy->Destination->getReg() == ReloadCopy->Source->getReg();
1266
1.13k
  };
1267
1268
112k
  auto IsChainedCopy = [&, this](const MachineInstr &Prev,
1269
112k
                                 const MachineInstr &Current) {
1270
281
    if (!IsFoldableCopy(Prev) || !IsFoldableCopy(Current))
1271
0
      return false;
1272
281
    std::optional<DestSourcePair> PrevCopy =
1273
281
        isCopyInstr(Prev, *TII, UseCopyInstr);
1274
281
    std::optional<DestSourcePair> CurrentCopy =
1275
281
        isCopyInstr(Current, *TII, UseCopyInstr);
1276
281
    if (!PrevCopy || !CurrentCopy)
1277
0
      return false;
1278
281
    return PrevCopy->Source->getReg() == CurrentCopy->Destination->getReg();
1279
281
  };
1280
1281
2.47M
  for (MachineInstr &MI : llvm::make_early_inc_range(MBB)) {
1282
2.47M
    std::optional<DestSourcePair> CopyOperands =
1283
2.47M
        isCopyInstr(MI, *TII, UseCopyInstr);
1284
1285
    // Update track information via non-copy instruction.
1286
2.47M
    SmallSet<Register, 8> RegsToClobber;
1287
2.47M
    if (!CopyOperands) {
1288
7.86M
      for (const MachineOperand &MO : MI.operands()) {
1289
7.86M
        if (!MO.isReg())
1290
2.36M
          continue;
1291
5.49M
        Register Reg = MO.getReg();
1292
5.49M
        if (!Reg)
1293
2.30k
          continue;
1294
5.49M
        MachineInstr *LastUseCopy =
1295
5.49M
            Tracker.findLastSeenUseInCopy(Reg.asMCReg(), *TRI);
1296
5.49M
        if (LastUseCopy) {
1297
69.7k
          LLVM_DEBUG(dbgs() << "MCP: Copy source of\n");
1298
69.7k
          LLVM_DEBUG(LastUseCopy->dump());
1299
69.7k
          LLVM_DEBUG(dbgs() << "might be invalidated by\n");
1300
69.7k
          LLVM_DEBUG(MI.dump());
1301
69.7k
          CopySourceInvalid.insert(LastUseCopy);
1302
69.7k
        }
1303
        // Must be noted Tracker.clobberRegister(Reg, ...) removes tracking of
1304
        // Reg, i.e, COPY that defines Reg is removed from the mapping as well
1305
        // as marking COPYs that uses Reg unavailable.
1306
        // We don't invoke CopyTracker::clobberRegister(Reg, ...) if Reg is not
1307
        // defined by a previous COPY, since we don't want to make COPYs uses
1308
        // Reg unavailable.
1309
5.49M
        if (Tracker.findLastSeenDefInCopy(MI, Reg.asMCReg(), *TRI, *TII,
1310
5.49M
                                    UseCopyInstr))
1311
          // Thus we can keep the property#1.
1312
51.4k
          RegsToClobber.insert(Reg);
1313
5.49M
      }
1314
2.41M
      for (Register Reg : RegsToClobber) {
1315
41.5k
        Tracker.clobberRegister(Reg, *TRI, *TII, UseCopyInstr);
1316
41.5k
        LLVM_DEBUG(dbgs() << "MCP: Removed tracking of " << printReg(Reg, TRI)
1317
41.5k
                          << "\n");
1318
41.5k
      }
1319
2.41M
      continue;
1320
2.41M
    }
1321
1322
50.9k
    Register Src = CopyOperands->Source->getReg();
1323
50.9k
    Register Def = CopyOperands->Destination->getReg();
1324
    // Check if we can find a pair spill-reload copy.
1325
50.9k
    LLVM_DEBUG(dbgs() << "MCP: Searching paired spill for reload: ");
1326
50.9k
    LLVM_DEBUG(MI.dump());
1327
50.9k
    MachineInstr *MaybeSpill =
1328
50.9k
        Tracker.findLastSeenDefInCopy(MI, Src.asMCReg(), *TRI, *TII, UseCopyInstr);
1329
50.9k
    bool MaybeSpillIsChained = ChainLeader.count(MaybeSpill);
1330
50.9k
    if (!MaybeSpillIsChained && MaybeSpill &&
1331
50.9k
        IsSpillReloadPair(*MaybeSpill, MI)) {
1332
      // Check if we already have an existing chain. Now we have a
1333
      // spill-reload pair.
1334
      // L2: r2 = COPY r3
1335
      // L5: r3 = COPY r2
1336
      // Looking for a valid COPY before L5 which uses r3.
1337
      // This can be serverial cases.
1338
      // Case #1:
1339
      // No COPY is found, which can be r3 is def-use between (L2, L5), we
1340
      // create a new chain for L2 and L5.
1341
      // Case #2:
1342
      // L2: r2 = COPY r3
1343
      // L5: r3 = COPY r2
1344
      // Such COPY is found and is L2, we create a new chain for L2 and L5.
1345
      // Case #3:
1346
      // L2: r2 = COPY r3
1347
      // L3: r1 = COPY r3
1348
      // L5: r3 = COPY r2
1349
      // we create a new chain for L2 and L5.
1350
      // Case #4:
1351
      // L2: r2 = COPY r3
1352
      // L3: r1 = COPY r3
1353
      // L4: r3 = COPY r1
1354
      // L5: r3 = COPY r2
1355
      // Such COPY won't be found since L4 defines r3. we create a new chain
1356
      // for L2 and L5.
1357
      // Case #5:
1358
      // L2: r2 = COPY r3
1359
      // L3: r3 = COPY r1
1360
      // L4: r1 = COPY r3
1361
      // L5: r3 = COPY r2
1362
      // COPY is found and is L4 which belongs to an existing chain, we add
1363
      // L2 and L5 to this chain.
1364
650
      LLVM_DEBUG(dbgs() << "MCP: Found spill: ");
1365
650
      LLVM_DEBUG(MaybeSpill->dump());
1366
650
      MachineInstr *MaybePrevReload =
1367
650
          Tracker.findLastSeenUseInCopy(Def.asMCReg(), *TRI);
1368
650
      auto Leader = ChainLeader.find(MaybePrevReload);
1369
650
      MachineInstr *L = nullptr;
1370
650
      if (Leader == ChainLeader.end() ||
1371
650
          (MaybePrevReload && !IsChainedCopy(*MaybePrevReload, MI))) {
1372
389
        L = &MI;
1373
389
        assert(!SpillChain.count(L) &&
1374
389
               "SpillChain should not have contained newly found chain");
1375
389
      } else {
1376
261
        assert(MaybePrevReload &&
1377
261
               "Found a valid leader through nullptr should not happend");
1378
0
        L = Leader->second;
1379
261
        assert(SpillChain[L].size() > 0 &&
1380
261
               "Existing chain's length should be larger than zero");
1381
261
      }
1382
0
      assert(!ChainLeader.count(&MI) && !ChainLeader.count(MaybeSpill) &&
1383
650
             "Newly found paired spill-reload should not belong to any chain "
1384
650
             "at this point");
1385
0
      ChainLeader.insert({MaybeSpill, L});
1386
650
      ChainLeader.insert({&MI, L});
1387
650
      SpillChain[L].push_back(MaybeSpill);
1388
650
      ReloadChain[L].push_back(&MI);
1389
650
      LLVM_DEBUG(dbgs() << "MCP: Chain " << L << " now is:\n");
1390
650
      LLVM_DEBUG(printSpillReloadChain(SpillChain, ReloadChain, L));
1391
50.3k
    } else if (MaybeSpill && !MaybeSpillIsChained) {
1392
      // MaybeSpill is unable to pair with MI. That's to say adding MI makes
1393
      // the chain invalid.
1394
      // The COPY defines Src is no longer considered as a candidate of a
1395
      // valid chain. Since we expect the Def of a spill copy isn't used by
1396
      // any COPY instruction until a reload copy. For example:
1397
      // L1: r1 = COPY r2
1398
      // L2: r3 = COPY r1
1399
      // If we later have
1400
      // L1: r1 = COPY r2
1401
      // L2: r3 = COPY r1
1402
      // L3: r2 = COPY r1
1403
      // L1 and L3 can't be a valid spill-reload pair.
1404
      // Thus we keep the property#1.
1405
3.28k
      LLVM_DEBUG(dbgs() << "MCP: Not paired spill-reload:\n");
1406
3.28k
      LLVM_DEBUG(MaybeSpill->dump());
1407
3.28k
      LLVM_DEBUG(MI.dump());
1408
3.28k
      Tracker.clobberRegister(Src.asMCReg(), *TRI, *TII, UseCopyInstr);
1409
3.28k
      LLVM_DEBUG(dbgs() << "MCP: Removed tracking of " << printReg(Src, TRI)
1410
3.28k
                        << "\n");
1411
3.28k
    }
1412
0
    Tracker.trackCopy(&MI, *TRI, *TII, UseCopyInstr);
1413
50.9k
  }
1414
1415
113k
  for (auto I = SpillChain.begin(), E = SpillChain.end(); I != E; ++I) {
1416
389
    auto &SC = I->second;
1417
389
    assert(ReloadChain.count(I->first) &&
1418
389
           "Reload chain of the same leader should exist");
1419
0
    auto &RC = ReloadChain[I->first];
1420
389
    TryFoldSpillageCopies(SC, RC);
1421
389
  }
1422
1423
112k
  MaybeDeadCopies.clear();
1424
112k
  CopyDbgUsers.clear();
1425
112k
  Tracker.clear();
1426
112k
}
1427
1428
219k
bool MachineCopyPropagation::runOnMachineFunction(MachineFunction &MF) {
1429
219k
  if (skipFunction(MF.getFunction()))
1430
0
    return false;
1431
1432
219k
  bool isSpillageCopyElimEnabled = false;
1433
219k
  switch (EnableSpillageCopyElimination) {
1434
219k
  case cl::BOU_UNSET:
1435
219k
    isSpillageCopyElimEnabled =
1436
219k
        MF.getSubtarget().enableSpillageCopyElimination();
1437
219k
    break;
1438
0
  case cl::BOU_TRUE:
1439
0
    isSpillageCopyElimEnabled = true;
1440
0
    break;
1441
0
  case cl::BOU_FALSE:
1442
0
    isSpillageCopyElimEnabled = false;
1443
0
    break;
1444
219k
  }
1445
1446
219k
  Changed = false;
1447
1448
219k
  TRI = MF.getSubtarget().getRegisterInfo();
1449
219k
  TII = MF.getSubtarget().getInstrInfo();
1450
219k
  MRI = &MF.getRegInfo();
1451
1452
387k
  for (MachineBasicBlock &MBB : MF) {
1453
387k
    if (isSpillageCopyElimEnabled)
1454
112k
      EliminateSpillageCopies(MBB);
1455
387k
    BackwardCopyPropagateBlock(MBB);
1456
387k
    ForwardCopyPropagateBlock(MBB);
1457
387k
  }
1458
1459
219k
  return Changed;
1460
219k
}
1461
1462
MachineFunctionPass *
1463
6.24k
llvm::createMachineCopyPropagationPass(bool UseCopyInstr = false) {
1464
6.24k
  return new MachineCopyPropagation(UseCopyInstr);
1465
6.24k
}