xref: /llvm-project/llvm/lib/CodeGen/MachineCopyPropagation.cpp (revision 87f8d25150facee0e5ae1884871db420c925877c)
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 //
10 // This is a simple MachineInstr-level copy forwarding pass.  It may be run at
11 // two places in the codegen pipeline:
12 //   - After register allocation but before virtual registers have been remapped
13 //     to physical registers.
14 //   - After physical register remapping.
15 //
16 // The optimizations done vary slightly based on whether virtual registers are
17 // still present.  In both cases, this pass forwards the source of COPYs to the
18 // users of their destinations when doing so is legal.  For example:
19 //
20 //   %vreg1 = COPY %vreg0
21 //   ...
22 //   ... = OP %vreg1
23 //
24 // If
25 //   - the physical register assigned to %vreg0 has not been clobbered by the
26 //     time of the use of %vreg1
27 //   - the register class constraints are satisfied
28 //   - the COPY def is the only value that reaches OP
29 // then this pass replaces the above with:
30 //
31 //   %vreg1 = COPY %vreg0
32 //   ...
33 //   ... = OP %vreg0
34 //
35 // and updates the relevant state required by VirtRegMap (e.g. LiveIntervals).
36 // COPYs whose LiveIntervals become dead as a result of this forwarding (i.e. if
37 // all uses of %vreg1 are changed to %vreg0) are removed.
38 //
39 // When being run with only physical registers, this pass will also remove some
40 // redundant COPYs.  For example:
41 //
42 //    %R1 = COPY %R0
43 //    ... // No clobber of %R1
44 //    %R0 = COPY %R1 <<< Removed
45 //
46 // or
47 //
48 //    %R1 = COPY %R0
49 //    ... // No clobber of %R0
50 //    %R1 = COPY %R0 <<< Removed
51 //
52 //===----------------------------------------------------------------------===//
53 
54 #include "LiveDebugVariables.h"
55 #include "llvm/ADT/DenseMap.h"
56 #include "llvm/ADT/SetVector.h"
57 #include "llvm/ADT/SmallVector.h"
58 #include "llvm/ADT/Statistic.h"
59 #include "llvm/CodeGen/LiveRangeEdit.h"
60 #include "llvm/CodeGen/LiveStackAnalysis.h"
61 #include "llvm/CodeGen/MachineFunction.h"
62 #include "llvm/CodeGen/MachineFunctionPass.h"
63 #include "llvm/CodeGen/MachineRegisterInfo.h"
64 #include "llvm/CodeGen/Passes.h"
65 #include "llvm/CodeGen/VirtRegMap.h"
66 #include "llvm/Pass.h"
67 #include "llvm/Support/Debug.h"
68 #include "llvm/Support/raw_ostream.h"
69 #include "llvm/Target/TargetInstrInfo.h"
70 #include "llvm/Target/TargetRegisterInfo.h"
71 #include "llvm/Target/TargetSubtargetInfo.h"
72 using namespace llvm;
73 
74 #define DEBUG_TYPE "machine-cp"
75 
76 STATISTIC(NumDeletes, "Number of dead copies deleted");
77 STATISTIC(NumCopyForwards, "Number of copy uses forwarded");
78 
79 namespace {
80   typedef SmallVector<unsigned, 4> RegList;
81   typedef DenseMap<unsigned, RegList> SourceMap;
82   typedef DenseMap<unsigned, MachineInstr*> Reg2MIMap;
83 
84   class MachineCopyPropagation : public MachineFunctionPass,
85                                  private LiveRangeEdit::Delegate {
86     const TargetRegisterInfo *TRI;
87     const TargetInstrInfo *TII;
88     MachineRegisterInfo *MRI;
89     MachineFunction *MF;
90     SlotIndexes *Indexes;
91     LiveIntervals *LIS;
92     const VirtRegMap *VRM;
93     // True if this pass being run before virtual registers are remapped to
94     // physical ones.
95     bool PreRegRewrite;
96     bool NoSubRegLiveness;
97 
98   protected:
99     MachineCopyPropagation(char &ID, bool PreRegRewrite)
100         : MachineFunctionPass(ID), PreRegRewrite(PreRegRewrite) {}
101 
102   public:
103     static char ID; // Pass identification, replacement for typeid
104     MachineCopyPropagation() : MachineCopyPropagation(ID, false) {
105       initializeMachineCopyPropagationPass(*PassRegistry::getPassRegistry());
106     }
107 
108     void getAnalysisUsage(AnalysisUsage &AU) const override {
109       if (PreRegRewrite) {
110         AU.addRequired<SlotIndexes>();
111         AU.addPreserved<SlotIndexes>();
112         AU.addRequired<LiveIntervals>();
113         AU.addPreserved<LiveIntervals>();
114         AU.addRequired<VirtRegMap>();
115         AU.addPreserved<VirtRegMap>();
116         AU.addPreserved<LiveDebugVariables>();
117         AU.addPreserved<LiveStacks>();
118       }
119       AU.setPreservesCFG();
120       MachineFunctionPass::getAnalysisUsage(AU);
121     }
122 
123     bool runOnMachineFunction(MachineFunction &MF) override;
124 
125     MachineFunctionProperties getRequiredProperties() const override {
126       if (PreRegRewrite)
127         return MachineFunctionProperties()
128             .set(MachineFunctionProperties::Property::NoPHIs)
129             .set(MachineFunctionProperties::Property::TracksLiveness);
130       return MachineFunctionProperties().set(
131           MachineFunctionProperties::Property::NoVRegs);
132     }
133 
134   private:
135     void ClobberRegister(unsigned Reg);
136     void ReadRegister(unsigned Reg);
137     void CopyPropagateBlock(MachineBasicBlock &MBB);
138     bool eraseIfRedundant(MachineInstr &Copy, unsigned Src, unsigned Def);
139     unsigned getPhysReg(unsigned Reg, unsigned SubReg);
140     unsigned getPhysReg(const MachineOperand &Opnd) {
141       return getPhysReg(Opnd.getReg(), Opnd.getSubReg());
142     }
143     unsigned getFullPhysReg(const MachineOperand &Opnd) {
144       return getPhysReg(Opnd.getReg(), 0);
145     }
146     void forwardUses(MachineInstr &MI);
147     bool isForwardableRegClassCopy(const MachineInstr &Copy,
148                                    const MachineInstr &UseI);
149     std::tuple<unsigned, unsigned, bool>
150     checkUseSubReg(const MachineOperand &CopySrc, const MachineOperand &MOUse);
151     bool hasImplicitOverlap(const MachineInstr &MI, const MachineOperand &Use);
152     void narrowRegClass(const MachineInstr &MI, const MachineOperand &MOUse,
153                         unsigned NewUseReg, unsigned NewUseSubReg);
154     void updateForwardedCopyLiveInterval(const MachineInstr &Copy,
155                                          const MachineInstr &UseMI,
156                                          unsigned OrigUseReg,
157                                          unsigned NewUseReg,
158                                          unsigned NewUseSubReg);
159     /// LiveRangeEdit callback for eliminateDeadDefs().
160     void LRE_WillEraseInstruction(MachineInstr *MI) override;
161 
162     /// Candidates for deletion.
163     SmallSetVector<MachineInstr*, 8> MaybeDeadCopies;
164     /// Def -> available copies map.
165     Reg2MIMap AvailCopyMap;
166     /// Def -> copies map.
167     Reg2MIMap CopyMap;
168     /// Src -> Def map
169     SourceMap SrcMap;
170     bool Changed;
171   };
172 
173   class MachineCopyPropagationPreRegRewrite : public MachineCopyPropagation {
174   public:
175     static char ID; // Pass identification, replacement for typeid
176     MachineCopyPropagationPreRegRewrite()
177         : MachineCopyPropagation(ID, true) {
178       initializeMachineCopyPropagationPreRegRewritePass(*PassRegistry::getPassRegistry());
179     }
180   };
181 }
182 char MachineCopyPropagation::ID = 0;
183 char &llvm::MachineCopyPropagationID = MachineCopyPropagation::ID;
184 
185 INITIALIZE_PASS(MachineCopyPropagation, DEBUG_TYPE,
186                 "Machine Copy Propagation Pass", false, false)
187 
188 /// We have two separate passes that are very similar, the only difference being
189 /// where they are meant to be run in the pipeline.  This is done for several
190 /// reasons:
191 /// - the two passes have different dependencies
192 /// - some targets want to disable the later run of this pass, but not the
193 ///   earlier one (e.g. NVPTX and WebAssembly)
194 /// - it allows for easier debugging via llc
195 
196 char MachineCopyPropagationPreRegRewrite::ID = 0;
197 char &llvm::MachineCopyPropagationPreRegRewriteID = MachineCopyPropagationPreRegRewrite::ID;
198 
199 INITIALIZE_PASS_BEGIN(MachineCopyPropagationPreRegRewrite,
200                       "machine-cp-prerewrite",
201                       "Machine Copy Propagation Pre-Register Rewrite Pass",
202                       false, false)
203 INITIALIZE_PASS_DEPENDENCY(SlotIndexes)
204 INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
205 INITIALIZE_PASS_DEPENDENCY(VirtRegMap)
206 INITIALIZE_PASS_END(MachineCopyPropagationPreRegRewrite,
207                     "machine-cp-prerewrite",
208                     "Machine Copy Propagation Pre-Register Rewrite Pass", false,
209                     false)
210 
211 /// Remove any entry in \p Map where the register is a subregister or equal to
212 /// a register contained in \p Regs.
213 static void removeRegsFromMap(Reg2MIMap &Map, const RegList &Regs,
214                               const TargetRegisterInfo &TRI) {
215   for (unsigned Reg : Regs) {
216     // Source of copy is no longer available for propagation.
217     for (MCSubRegIterator SR(Reg, &TRI, true); SR.isValid(); ++SR)
218       Map.erase(*SR);
219   }
220 }
221 
222 /// Remove any entry in \p Map that is marked clobbered in \p RegMask.
223 /// The map will typically have a lot fewer entries than the regmask clobbers,
224 /// so this is more efficient than iterating the clobbered registers and calling
225 /// ClobberRegister() on them.
226 static void removeClobberedRegsFromMap(Reg2MIMap &Map,
227                                        const MachineOperand &RegMask) {
228   for (Reg2MIMap::iterator I = Map.begin(), E = Map.end(), Next; I != E;
229        I = Next) {
230     Next = std::next(I);
231     unsigned Reg = I->first;
232     if (RegMask.clobbersPhysReg(Reg))
233       Map.erase(I);
234   }
235 }
236 
237 void MachineCopyPropagation::ClobberRegister(unsigned Reg) {
238   for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
239     CopyMap.erase(*AI);
240     AvailCopyMap.erase(*AI);
241 
242     SourceMap::iterator SI = SrcMap.find(*AI);
243     if (SI != SrcMap.end()) {
244       removeRegsFromMap(AvailCopyMap, SI->second, *TRI);
245       SrcMap.erase(SI);
246     }
247   }
248 }
249 
250 void MachineCopyPropagation::ReadRegister(unsigned Reg) {
251   // We don't track MaybeDeadCopies when running pre-VirtRegRewriter.
252   if (PreRegRewrite)
253     return;
254 
255   // If 'Reg' is defined by a copy, the copy is no longer a candidate
256   // for elimination.
257   for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
258     Reg2MIMap::iterator CI = CopyMap.find(*AI);
259     if (CI != CopyMap.end()) {
260       DEBUG(dbgs() << "MCP: Copy is used - not dead: "; CI->second->dump());
261       MaybeDeadCopies.remove(CI->second);
262     }
263   }
264 }
265 
266 /// Return true if \p PreviousCopy did copy register \p Src to register \p Def.
267 /// This fact may have been obscured by sub register usage or may not be true at
268 /// all even though Src and Def are subregisters of the registers used in
269 /// PreviousCopy. e.g.
270 /// isNopCopy("ecx = COPY eax", AX, CX) == true
271 /// isNopCopy("ecx = COPY eax", AH, CL) == false
272 static bool isNopCopy(const MachineInstr &PreviousCopy, unsigned Src,
273                       unsigned Def, const TargetRegisterInfo *TRI) {
274   unsigned PreviousSrc = PreviousCopy.getOperand(1).getReg();
275   unsigned PreviousDef = PreviousCopy.getOperand(0).getReg();
276   if (Src == PreviousSrc) {
277     assert(Def == PreviousDef);
278     return true;
279   }
280   if (!TRI->isSubRegister(PreviousSrc, Src))
281     return false;
282   unsigned SubIdx = TRI->getSubRegIndex(PreviousSrc, Src);
283   return SubIdx == TRI->getSubRegIndex(PreviousDef, Def);
284 }
285 
286 /// Return the physical register assigned to \p Reg if it is a virtual register,
287 /// otherwise just return the physical reg from the operand itself.
288 ///
289 /// If \p SubReg is 0 then return the full physical register assigned to the
290 /// virtual register ignoring subregs.  If we aren't tracking sub-reg liveness
291 /// then we need to use this to be more conservative with clobbers by killing
292 /// all super reg and their sub reg COPYs as well.  This is to prevent COPY
293 /// forwarding in cases like the following:
294 ///
295 ///    %vreg2 = COPY %vreg1:sub1
296 ///    %vreg3 = COPY %vreg1:sub0
297 ///    ...    = OP1 %vreg2
298 ///    ...    = OP2 %vreg3
299 ///
300 /// After forward %vreg2 (assuming this is the last use of %vreg1) and
301 /// VirtRegRewriter adding kill markers we have:
302 ///
303 ///    %vreg3 = COPY %vreg1:sub0
304 ///    ...    = OP1 %vreg1:sub1<kill>
305 ///    ...    = OP2 %vreg3
306 ///
307 /// If %vreg3 is assigned to a sub-reg of %vreg1, then after rewriting we have:
308 ///
309 ///    ...     = OP1 R0:sub1, R0<imp-use,kill>
310 ///    ...     = OP2 R0:sub0
311 ///
312 /// and the use of R0 by OP2 will not have a valid definition.
313 unsigned MachineCopyPropagation::getPhysReg(unsigned Reg, unsigned SubReg) {
314 
315   // Physical registers cannot have subregs.
316   if (!TargetRegisterInfo::isVirtualRegister(Reg))
317     return Reg;
318 
319   assert(PreRegRewrite && "Unexpected virtual register encountered");
320   Reg = VRM->getPhys(Reg);
321   if (SubReg && !NoSubRegLiveness)
322     Reg = TRI->getSubReg(Reg, SubReg);
323   return Reg;
324 }
325 
326 /// Remove instruction \p Copy if there exists a previous copy that copies the
327 /// register \p Src to the register \p Def; This may happen indirectly by
328 /// copying the super registers.
329 bool MachineCopyPropagation::eraseIfRedundant(MachineInstr &Copy, unsigned Src,
330                                               unsigned Def) {
331   // Avoid eliminating a copy from/to a reserved registers as we cannot predict
332   // the value (Example: The sparc zero register is writable but stays zero).
333   if (MRI->isReserved(Src) || MRI->isReserved(Def))
334     return false;
335 
336   // Search for an existing copy.
337   Reg2MIMap::iterator CI = AvailCopyMap.find(Def);
338   if (CI == AvailCopyMap.end())
339     return false;
340 
341   // Check that the existing copy uses the correct sub registers.
342   MachineInstr &PrevCopy = *CI->second;
343   if (!isNopCopy(PrevCopy, Src, Def, TRI))
344     return false;
345 
346   DEBUG(dbgs() << "MCP: copy is a NOP, removing: "; Copy.dump());
347 
348   // Copy was redundantly redefining either Src or Def. Remove earlier kill
349   // flags between Copy and PrevCopy because the value will be reused now.
350   assert(Copy.isCopy());
351   unsigned CopyDef = Copy.getOperand(0).getReg();
352   assert(CopyDef == Src || CopyDef == Def);
353   for (MachineInstr &MI :
354        make_range(PrevCopy.getIterator(), Copy.getIterator()))
355     MI.clearRegisterKills(CopyDef, TRI);
356 
357   Copy.eraseFromParent();
358   Changed = true;
359   ++NumDeletes;
360   return true;
361 }
362 
363 
364 /// Decide whether we should forward the destination of \param Copy to its use
365 /// in \param UseI based on the register class of the Copy operands.  Same-class
366 /// COPYs are always accepted by this function, but cross-class COPYs are only
367 /// accepted if they are forwarded to another COPY with the operand register
368 /// classes reversed.  For example:
369 ///
370 ///   RegClassA = COPY RegClassB  // Copy parameter
371 ///   ...
372 ///   RegClassB = COPY RegClassA  // UseI parameter
373 ///
374 /// which after forwarding becomes
375 ///
376 ///   RegClassA = COPY RegClassB
377 ///   ...
378 ///   RegClassB = COPY RegClassB
379 ///
380 /// so we have reduced the number of cross-class COPYs and potentially
381 /// introduced a no COPY that can be removed.
382 bool MachineCopyPropagation::isForwardableRegClassCopy(
383     const MachineInstr &Copy, const MachineInstr &UseI) {
384   auto isCross = [&](const MachineOperand &Dst, const MachineOperand &Src) {
385     unsigned DstReg = Dst.getReg();
386     unsigned SrcPhysReg = getPhysReg(Src);
387     const TargetRegisterClass *DstRC;
388     if (TargetRegisterInfo::isVirtualRegister(DstReg)) {
389       DstRC = MRI->getRegClass(DstReg);
390       unsigned DstSubReg = Dst.getSubReg();
391       if (DstSubReg)
392         SrcPhysReg = TRI->getMatchingSuperReg(SrcPhysReg, DstSubReg, DstRC);
393     } else
394       DstRC = TRI->getMinimalPhysRegClass(DstReg);
395 
396     return !DstRC->contains(SrcPhysReg);
397   };
398 
399   const MachineOperand &CopyDst = Copy.getOperand(0);
400   const MachineOperand &CopySrc = Copy.getOperand(1);
401 
402   if (!isCross(CopyDst, CopySrc))
403     return true;
404 
405   if (!UseI.isCopy())
406     return false;
407 
408   assert(getFullPhysReg(UseI.getOperand(1)) == getFullPhysReg(CopyDst));
409   return !isCross(UseI.getOperand(0), CopySrc);
410 }
411 
412 /// Check that the subregs on the copy source operand (\p CopySrc) and the use
413 /// operand to be forwarded to (\p MOUse) are compatible with doing the
414 /// forwarding.  Also computes the new register and subregister to be used in
415 /// the forwarded-to instruction.
416 std::tuple<unsigned, unsigned, bool> MachineCopyPropagation::checkUseSubReg(
417     const MachineOperand &CopySrc, const MachineOperand &MOUse) {
418   unsigned NewUseReg = CopySrc.getReg();
419   unsigned NewUseSubReg;
420 
421   if (TargetRegisterInfo::isPhysicalRegister(NewUseReg)) {
422     // If MOUse is a virtual reg, we need to apply it to the new physical reg
423     // we're going to replace it with.
424     if (MOUse.getSubReg())
425       NewUseReg = TRI->getSubReg(NewUseReg, MOUse.getSubReg());
426     // If the original use subreg isn't valid on the new src reg, we can't
427     // forward it here.
428     if (!NewUseReg)
429       return std::make_tuple(0, 0, false);
430     NewUseSubReg = 0;
431   } else {
432     // %v1 = COPY %v2:sub1
433     //    USE %v1:sub2
434     // The new use is %v2:sub1:sub2
435     NewUseSubReg =
436         TRI->composeSubRegIndices(CopySrc.getSubReg(), MOUse.getSubReg());
437     // Check that NewUseSubReg is valid on NewUseReg
438     if (NewUseSubReg &&
439         !TRI->getSubClassWithSubReg(MRI->getRegClass(NewUseReg), NewUseSubReg))
440       return std::make_tuple(0, 0, false);
441   }
442 
443   return std::make_tuple(NewUseReg, NewUseSubReg, true);
444 }
445 
446 /// Check that \p MI does not have implicit uses that overlap with it's \p Use
447 /// operand (the register being replaced), since these can sometimes be
448 /// implicitly tied to other operands.  For example, on AMDGPU:
449 ///
450 /// V_MOVRELS_B32_e32 %VGPR2, %M0<imp-use>, %EXEC<imp-use>, %VGPR2_VGPR3_VGPR4_VGPR5<imp-use>
451 ///
452 /// the %VGPR2 is implicitly tied to the larger reg operand, but we have no
453 /// way of knowing we need to update the latter when updating the former.
454 bool MachineCopyPropagation::hasImplicitOverlap(const MachineInstr &MI,
455                                                 const MachineOperand &Use) {
456   if (!TargetRegisterInfo::isPhysicalRegister(Use.getReg()))
457     return false;
458 
459   for (const MachineOperand &MIUse : MI.uses())
460     if (&MIUse != &Use && MIUse.isReg() && MIUse.isImplicit() &&
461         TRI->regsOverlap(Use.getReg(), MIUse.getReg()))
462       return true;
463 
464   return false;
465 }
466 
467 /// Narrow the register class of the forwarded vreg so it matches any
468 /// instruction constraints.  \p MI is the instruction being forwarded to. \p
469 /// MOUse is the operand being replaced in \p MI (which hasn't yet been updated
470 /// at the time this function is called).  \p NewUseReg and \p NewUseSubReg are
471 /// what the \p MOUse will be changed to after forwarding.
472 ///
473 /// If we are forwarding
474 ///    A:RCA = COPY B:RCB
475 /// into
476 ///    ... = OP A:RCA
477 ///
478 /// then we need to narrow the register class of B so that it is a subclass
479 /// of RCA so that it meets the instruction register class constraints.
480 void MachineCopyPropagation::narrowRegClass(const MachineInstr &MI,
481                                             const MachineOperand &MOUse,
482                                             unsigned NewUseReg,
483                                             unsigned NewUseSubReg) {
484   if (!TargetRegisterInfo::isVirtualRegister(NewUseReg))
485     return;
486 
487   // Make sure the virtual reg class allows the subreg.
488   if (NewUseSubReg) {
489     const TargetRegisterClass *CurUseRC = MRI->getRegClass(NewUseReg);
490     const TargetRegisterClass *NewUseRC =
491         TRI->getSubClassWithSubReg(CurUseRC, NewUseSubReg);
492     if (CurUseRC != NewUseRC) {
493       DEBUG(dbgs() << "MCP: Setting regclass of " << PrintReg(NewUseReg, TRI)
494                    << " to " << TRI->getRegClassName(NewUseRC) << "\n");
495       MRI->setRegClass(NewUseReg, NewUseRC);
496     }
497   }
498 
499   unsigned MOUseOpNo = &MOUse - &MI.getOperand(0);
500   const TargetRegisterClass *InstRC =
501       TII->getRegClass(MI.getDesc(), MOUseOpNo, TRI, *MF);
502   if (InstRC) {
503     const TargetRegisterClass *CurUseRC = MRI->getRegClass(NewUseReg);
504     if (NewUseSubReg)
505       InstRC = TRI->getMatchingSuperRegClass(CurUseRC, InstRC, NewUseSubReg);
506     if (!InstRC->hasSubClassEq(CurUseRC)) {
507       const TargetRegisterClass *NewUseRC =
508           TRI->getCommonSubClass(InstRC, CurUseRC);
509       DEBUG(dbgs() << "MCP: Setting regclass of " << PrintReg(NewUseReg, TRI)
510                    << " to " << TRI->getRegClassName(NewUseRC) << "\n");
511       MRI->setRegClass(NewUseReg, NewUseRC);
512     }
513   }
514 }
515 
516 /// Update the LiveInterval information to reflect the destination of \p Copy
517 /// being forwarded to a use in \p UseMI.  \p OrigUseReg is the register being
518 /// forwarded through. It should be the destination register of \p Copy and has
519 /// already been replaced in \p UseMI at the point this function is called.  \p
520 /// NewUseReg and \p NewUseSubReg are the register and subregister being
521 /// forwarded.  They should be the source register of the \p Copy and should be
522 /// the value of the \p UseMI operand being forwarded at the point this function
523 /// is called.
524 void MachineCopyPropagation::updateForwardedCopyLiveInterval(
525     const MachineInstr &Copy, const MachineInstr &UseMI, unsigned OrigUseReg,
526     unsigned NewUseReg, unsigned NewUseSubReg) {
527 
528   assert(TRI->isSubRegisterEq(getPhysReg(OrigUseReg, 0),
529                               getFullPhysReg(Copy.getOperand(0))) &&
530          "OrigUseReg mismatch");
531   assert(TRI->isSubRegisterEq(getFullPhysReg(Copy.getOperand(1)),
532                               getPhysReg(NewUseReg, 0)) &&
533          "NewUseReg mismatch");
534 
535   // Extend live range starting from COPY early-clobber slot, since that
536   // is where the original src live range ends.
537   SlotIndex CopyUseIdx =
538       Indexes->getInstructionIndex(Copy).getRegSlot(true /*=EarlyClobber*/);
539   SlotIndex UseIdx = Indexes->getInstructionIndex(UseMI).getRegSlot();
540   if (TargetRegisterInfo::isVirtualRegister(NewUseReg)) {
541     LiveInterval &LI = LIS->getInterval(NewUseReg);
542     LI.extendInBlock(CopyUseIdx, UseIdx);
543     LaneBitmask UseMask = TRI->getSubRegIndexLaneMask(NewUseSubReg);
544     for (auto &S : LI.subranges())
545       if ((S.LaneMask & UseMask).any() && S.find(CopyUseIdx))
546         S.extendInBlock(CopyUseIdx, UseIdx);
547   } else {
548     assert(NewUseSubReg == 0 && "Unexpected subreg on physical register!");
549     for (MCRegUnitIterator UI(NewUseReg, TRI); UI.isValid(); ++UI) {
550       LiveRange &LR = LIS->getRegUnit(*UI);
551       LR.extendInBlock(CopyUseIdx, UseIdx);
552     }
553   }
554 
555   if (!TargetRegisterInfo::isVirtualRegister(OrigUseReg))
556     return;
557 
558   LiveInterval &LI = LIS->getInterval(OrigUseReg);
559 
560   // Can happen for undef uses.
561   if (LI.empty())
562     return;
563 
564   SlotIndex UseIndex = Indexes->getInstructionIndex(UseMI);
565   const LiveRange::Segment *UseSeg = LI.getSegmentContaining(UseIndex);
566 
567   // Only shrink if forwarded use is the end of a segment.
568   if (UseSeg->end != UseIndex.getRegSlot())
569     return;
570 
571   SmallVector<MachineInstr *, 4> DeadInsts;
572   LIS->shrinkToUses(&LI, &DeadInsts);
573   if (!DeadInsts.empty()) {
574     SmallVector<unsigned, 8> NewRegs;
575     LiveRangeEdit(nullptr, NewRegs, *MF, *LIS, nullptr, this)
576         .eliminateDeadDefs(DeadInsts);
577   }
578 }
579 
580 void MachineCopyPropagation::LRE_WillEraseInstruction(MachineInstr *MI) {
581   // Remove this COPY from further consideration for forwarding.
582   ClobberRegister(getFullPhysReg(MI->getOperand(0)));
583   Changed = true;
584 }
585 
586 /// Look for available copies whose destination register is used by \p MI and
587 /// replace the use in \p MI with the copy's source register.
588 void MachineCopyPropagation::forwardUses(MachineInstr &MI) {
589   if (AvailCopyMap.empty())
590     return;
591 
592   // Look for non-tied explicit vreg uses that have an active COPY
593   // instruction that defines the physical register allocated to them.
594   // Replace the vreg with the source of the active COPY.
595   for (MachineOperand &MOUse : MI.explicit_uses()) {
596     if (!MOUse.isReg() || MOUse.isTied())
597       continue;
598 
599     unsigned UseReg = MOUse.getReg();
600     if (!UseReg)
601       continue;
602 
603     if (TargetRegisterInfo::isVirtualRegister(UseReg))
604       UseReg = VRM->getPhys(UseReg);
605     else if (MI.isCall() || MI.isReturn() || MI.isInlineAsm() ||
606              MI.hasUnmodeledSideEffects() || MI.isDebugValue() || MI.isKill())
607       // Some instructions seem to have ABI uses e.g. not marked as
608       // implicit, which can lead to forwarding them when we shouldn't, so
609       // restrict the types of instructions we forward physical regs into.
610       continue;
611 
612     // Don't forward COPYs via non-allocatable regs since they can have
613     // non-standard semantics.
614     if (!MRI->isAllocatable(UseReg))
615       continue;
616 
617     auto CI = AvailCopyMap.find(UseReg);
618     if (CI == AvailCopyMap.end())
619       continue;
620 
621     MachineInstr &Copy = *CI->second;
622     MachineOperand &CopyDst = Copy.getOperand(0);
623     MachineOperand &CopySrc = Copy.getOperand(1);
624 
625     // Don't forward COPYs that are already NOPs due to register assignment.
626     if (getPhysReg(CopyDst) == getPhysReg(CopySrc))
627       continue;
628 
629     // FIXME: Don't handle partial uses of wider COPYs yet.
630     if (CopyDst.getSubReg() != 0 || UseReg != getPhysReg(CopyDst))
631       continue;
632 
633     // Don't forward COPYs of non-allocatable regs unless they are constant.
634     unsigned CopySrcReg = CopySrc.getReg();
635     if (TargetRegisterInfo::isPhysicalRegister(CopySrcReg) &&
636         !MRI->isAllocatable(CopySrcReg) && !MRI->isConstantPhysReg(CopySrcReg))
637       continue;
638 
639     if (!isForwardableRegClassCopy(Copy, MI))
640       continue;
641 
642     unsigned NewUseReg, NewUseSubReg;
643     bool SubRegOK;
644     std::tie(NewUseReg, NewUseSubReg, SubRegOK) =
645         checkUseSubReg(CopySrc, MOUse);
646     if (!SubRegOK)
647       continue;
648 
649     if (hasImplicitOverlap(MI, MOUse))
650       continue;
651 
652     DEBUG(dbgs() << "MCP: Replacing "
653           << PrintReg(MOUse.getReg(), TRI, MOUse.getSubReg())
654           << "\n     with "
655           << PrintReg(NewUseReg, TRI, CopySrc.getSubReg())
656           << "\n     in "
657           << MI
658           << "     from "
659           << Copy);
660 
661     narrowRegClass(MI, MOUse, NewUseReg, NewUseSubReg);
662 
663     unsigned OrigUseReg = MOUse.getReg();
664     MOUse.setReg(NewUseReg);
665     MOUse.setSubReg(NewUseSubReg);
666 
667     DEBUG(dbgs() << "MCP: After replacement: " << MI << "\n");
668 
669     if (PreRegRewrite)
670       updateForwardedCopyLiveInterval(Copy, MI, OrigUseReg, NewUseReg,
671                                       NewUseSubReg);
672     else
673       for (MachineInstr &KMI :
674              make_range(Copy.getIterator(), std::next(MI.getIterator())))
675         KMI.clearRegisterKills(NewUseReg, TRI);
676 
677     ++NumCopyForwards;
678     Changed = true;
679   }
680 }
681 
682 void MachineCopyPropagation::CopyPropagateBlock(MachineBasicBlock &MBB) {
683   DEBUG(dbgs() << "MCP: CopyPropagateBlock " << MBB.getName() << "\n");
684 
685   for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ) {
686     MachineInstr *MI = &*I;
687     ++I;
688 
689     if (MI->isCopy()) {
690       unsigned Def = getPhysReg(MI->getOperand(0));
691       unsigned Src = getPhysReg(MI->getOperand(1));
692 
693       // The two copies cancel out and the source of the first copy
694       // hasn't been overridden, eliminate the second one. e.g.
695       //  %ECX<def> = COPY %EAX
696       //  ... nothing clobbered EAX.
697       //  %EAX<def> = COPY %ECX
698       // =>
699       //  %ECX<def> = COPY %EAX
700       //
701       // or
702       //
703       //  %ECX<def> = COPY %EAX
704       //  ... nothing clobbered EAX.
705       //  %ECX<def> = COPY %EAX
706       // =>
707       //  %ECX<def> = COPY %EAX
708       if (!PreRegRewrite)
709         if (eraseIfRedundant(*MI, Def, Src) || eraseIfRedundant(*MI, Src, Def))
710           continue;
711 
712       forwardUses(*MI);
713 
714       // Src may have been changed by forwardUses()
715       Src = getPhysReg(MI->getOperand(1));
716       unsigned DefClobber = getFullPhysReg(MI->getOperand(0));
717       unsigned SrcClobber = getFullPhysReg(MI->getOperand(1));
718 
719       // If Src is defined by a previous copy, the previous copy cannot be
720       // eliminated.
721       ReadRegister(Src);
722       for (const MachineOperand &MO : MI->implicit_operands()) {
723         if (!MO.isReg() || !MO.readsReg())
724           continue;
725         unsigned Reg = MO.getReg();
726         if (!Reg)
727           continue;
728         ReadRegister(Reg);
729       }
730 
731       DEBUG(dbgs() << "MCP: Copy is a deletion candidate: "; MI->dump());
732 
733       // Copy is now a candidate for deletion.
734       // Only look for dead COPYs if we're not running just before
735       // VirtRegRewriter, since presumably these COPYs will have already been
736       // removed.
737       if (!PreRegRewrite && !MRI->isReserved(Def))
738         MaybeDeadCopies.insert(MI);
739 
740       // If 'Def' is previously source of another copy, then this earlier copy's
741       // source is no longer available. e.g.
742       // %xmm9<def> = copy %xmm2
743       // ...
744       // %xmm2<def> = copy %xmm0
745       // ...
746       // %xmm2<def> = copy %xmm9
747       ClobberRegister(DefClobber);
748       for (const MachineOperand &MO : MI->implicit_operands()) {
749         if (!MO.isReg() || !MO.isDef())
750           continue;
751         unsigned Reg = getFullPhysReg(MO);
752         if (!Reg)
753           continue;
754         ClobberRegister(Reg);
755       }
756 
757       // Remember Def is defined by the copy.
758       for (MCSubRegIterator SR(Def, TRI, /*IncludeSelf=*/true); SR.isValid();
759            ++SR) {
760         CopyMap[*SR] = MI;
761         AvailCopyMap[*SR] = MI;
762       }
763 
764       // Remember source that's copied to Def. Once it's clobbered, then
765       // it's no longer available for copy propagation.
766       RegList &DestList = SrcMap[SrcClobber];
767       if (!is_contained(DestList, DefClobber))
768         DestList.push_back(DefClobber);
769 
770       continue;
771     }
772 
773     // Clobber any earlyclobber regs first.
774     for (const MachineOperand &MO : MI->operands())
775       if (MO.isReg() && MO.isEarlyClobber()) {
776         unsigned Reg = getFullPhysReg(MO);
777         // If we have a tied earlyclobber, that means it is also read by this
778         // instruction, so we need to make sure we don't remove it as dead
779         // later.
780         if (MO.isTied())
781           ReadRegister(Reg);
782         ClobberRegister(Reg);
783       }
784 
785     forwardUses(*MI);
786 
787     // Not a copy.
788     SmallVector<unsigned, 2> Defs;
789     const MachineOperand *RegMask = nullptr;
790     for (const MachineOperand &MO : MI->operands()) {
791       if (MO.isRegMask())
792         RegMask = &MO;
793       if (!MO.isReg())
794         continue;
795       unsigned Reg = getFullPhysReg(MO);
796       if (!Reg)
797         continue;
798 
799       if (MO.isDef() && !MO.isEarlyClobber()) {
800         Defs.push_back(Reg);
801         continue;
802       } else if (MO.readsReg())
803         ReadRegister(Reg);
804     }
805 
806     // The instruction has a register mask operand which means that it clobbers
807     // a large set of registers.  Treat clobbered registers the same way as
808     // defined registers.
809     if (RegMask) {
810       // Erase any MaybeDeadCopies whose destination register is clobbered.
811       for (SmallSetVector<MachineInstr *, 8>::iterator DI =
812                MaybeDeadCopies.begin();
813            DI != MaybeDeadCopies.end();) {
814         MachineInstr *MaybeDead = *DI;
815         unsigned Reg = MaybeDead->getOperand(0).getReg();
816         assert(!MRI->isReserved(Reg));
817 
818         if (!RegMask->clobbersPhysReg(Reg)) {
819           ++DI;
820           continue;
821         }
822 
823         DEBUG(dbgs() << "MCP: Removing copy due to regmask clobbering: ";
824               MaybeDead->dump());
825 
826         // erase() will return the next valid iterator pointing to the next
827         // element after the erased one.
828         DI = MaybeDeadCopies.erase(DI);
829         MaybeDead->eraseFromParent();
830         Changed = true;
831         ++NumDeletes;
832       }
833 
834       removeClobberedRegsFromMap(AvailCopyMap, *RegMask);
835       removeClobberedRegsFromMap(CopyMap, *RegMask);
836       for (SourceMap::iterator I = SrcMap.begin(), E = SrcMap.end(), Next;
837            I != E; I = Next) {
838         Next = std::next(I);
839         if (RegMask->clobbersPhysReg(I->first)) {
840           removeRegsFromMap(AvailCopyMap, I->second, *TRI);
841           SrcMap.erase(I);
842         }
843       }
844     }
845 
846     // Any previous copy definition or reading the Defs is no longer available.
847     for (unsigned Reg : Defs)
848       ClobberRegister(Reg);
849   }
850 
851   // If MBB doesn't have successors, delete the copies whose defs are not used.
852   // If MBB does have successors, then conservative assume the defs are live-out
853   // since we don't want to trust live-in lists.
854   if (MBB.succ_empty()) {
855     for (MachineInstr *MaybeDead : MaybeDeadCopies) {
856       DEBUG(dbgs() << "MCP: Removing copy due to no live-out succ: ";
857             MaybeDead->dump());
858       assert(!MRI->isReserved(MaybeDead->getOperand(0).getReg()));
859       MaybeDead->eraseFromParent();
860       Changed = true;
861       ++NumDeletes;
862     }
863   }
864 
865   MaybeDeadCopies.clear();
866   AvailCopyMap.clear();
867   CopyMap.clear();
868   SrcMap.clear();
869 }
870 
871 bool MachineCopyPropagation::runOnMachineFunction(MachineFunction &MF) {
872   if (skipFunction(*MF.getFunction()))
873     return false;
874 
875   Changed = false;
876 
877   TRI = MF.getSubtarget().getRegisterInfo();
878   TII = MF.getSubtarget().getInstrInfo();
879   MRI = &MF.getRegInfo();
880   this->MF = &MF;
881   if (PreRegRewrite) {
882     Indexes = &getAnalysis<SlotIndexes>();
883     LIS = &getAnalysis<LiveIntervals>();
884     VRM = &getAnalysis<VirtRegMap>();
885   }
886   NoSubRegLiveness = !MRI->subRegLivenessEnabled();
887 
888   for (MachineBasicBlock &MBB : MF)
889     CopyPropagateBlock(MBB);
890 
891   return Changed;
892 }
893