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