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