xref: /llvm-project/llvm/lib/CodeGen/MachineSink.cpp (revision 1562b70eaf6e0b95910fa684dfc53bd5ca6252e7)
1 //===- MachineSink.cpp - Sinking for machine instructions -----------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This pass moves instructions into successor blocks when possible, so that
10 // they aren't executed on paths where their results aren't needed.
11 //
12 // This pass is not intended to be a replacement or a complete alternative
13 // for an LLVM-IR-level sinking pass. It is only designed to sink simple
14 // constructs that are not exposed before lowering and instruction selection.
15 //
16 //===----------------------------------------------------------------------===//
17 
18 #include "llvm/ADT/DenseSet.h"
19 #include "llvm/ADT/DepthFirstIterator.h"
20 #include "llvm/ADT/MapVector.h"
21 #include "llvm/ADT/PointerIntPair.h"
22 #include "llvm/ADT/SetVector.h"
23 #include "llvm/ADT/SmallSet.h"
24 #include "llvm/ADT/SmallVector.h"
25 #include "llvm/ADT/Statistic.h"
26 #include "llvm/Analysis/AliasAnalysis.h"
27 #include "llvm/Analysis/CFG.h"
28 #include "llvm/Analysis/ProfileSummaryInfo.h"
29 #include "llvm/CodeGen/MachineBasicBlock.h"
30 #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
31 #include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
32 #include "llvm/CodeGen/MachineCycleAnalysis.h"
33 #include "llvm/CodeGen/MachineDomTreeUpdater.h"
34 #include "llvm/CodeGen/MachineDominators.h"
35 #include "llvm/CodeGen/MachineFunction.h"
36 #include "llvm/CodeGen/MachineFunctionPass.h"
37 #include "llvm/CodeGen/MachineInstr.h"
38 #include "llvm/CodeGen/MachineLoopInfo.h"
39 #include "llvm/CodeGen/MachineOperand.h"
40 #include "llvm/CodeGen/MachinePostDominators.h"
41 #include "llvm/CodeGen/MachineRegisterInfo.h"
42 #include "llvm/CodeGen/MachineSizeOpts.h"
43 #include "llvm/CodeGen/RegisterClassInfo.h"
44 #include "llvm/CodeGen/RegisterPressure.h"
45 #include "llvm/CodeGen/TargetInstrInfo.h"
46 #include "llvm/CodeGen/TargetPassConfig.h"
47 #include "llvm/CodeGen/TargetRegisterInfo.h"
48 #include "llvm/CodeGen/TargetSubtargetInfo.h"
49 #include "llvm/IR/BasicBlock.h"
50 #include "llvm/IR/DebugInfoMetadata.h"
51 #include "llvm/IR/LLVMContext.h"
52 #include "llvm/InitializePasses.h"
53 #include "llvm/Pass.h"
54 #include "llvm/Support/BranchProbability.h"
55 #include "llvm/Support/CommandLine.h"
56 #include "llvm/Support/Debug.h"
57 #include "llvm/Support/raw_ostream.h"
58 #include <cassert>
59 #include <cstdint>
60 #include <utility>
61 #include <vector>
62 
63 using namespace llvm;
64 
65 #define DEBUG_TYPE "machine-sink"
66 
67 static cl::opt<bool>
68     SplitEdges("machine-sink-split",
69                cl::desc("Split critical edges during machine sinking"),
70                cl::init(true), cl::Hidden);
71 
72 static cl::opt<bool> UseBlockFreqInfo(
73     "machine-sink-bfi",
74     cl::desc("Use block frequency info to find successors to sink"),
75     cl::init(true), cl::Hidden);
76 
77 static cl::opt<unsigned> SplitEdgeProbabilityThreshold(
78     "machine-sink-split-probability-threshold",
79     cl::desc(
80         "Percentage threshold for splitting single-instruction critical edge. "
81         "If the branch threshold is higher than this threshold, we allow "
82         "speculative execution of up to 1 instruction to avoid branching to "
83         "splitted critical edge"),
84     cl::init(40), cl::Hidden);
85 
86 static cl::opt<unsigned> SinkLoadInstsPerBlockThreshold(
87     "machine-sink-load-instrs-threshold",
88     cl::desc("Do not try to find alias store for a load if there is a in-path "
89              "block whose instruction number is higher than this threshold."),
90     cl::init(2000), cl::Hidden);
91 
92 static cl::opt<unsigned> SinkLoadBlocksThreshold(
93     "machine-sink-load-blocks-threshold",
94     cl::desc("Do not try to find alias store for a load if the block number in "
95              "the straight line is higher than this threshold."),
96     cl::init(20), cl::Hidden);
97 
98 static cl::opt<bool>
99     SinkInstsIntoCycle("sink-insts-to-avoid-spills",
100                        cl::desc("Sink instructions into cycles to avoid "
101                                 "register spills"),
102                        cl::init(false), cl::Hidden);
103 
104 static cl::opt<unsigned> SinkIntoCycleLimit(
105     "machine-sink-cycle-limit",
106     cl::desc(
107         "The maximum number of instructions considered for cycle sinking."),
108     cl::init(50), cl::Hidden);
109 
110 STATISTIC(NumSunk, "Number of machine instructions sunk");
111 STATISTIC(NumCycleSunk, "Number of machine instructions sunk into a cycle");
112 STATISTIC(NumSplit, "Number of critical edges split");
113 STATISTIC(NumCoalesces, "Number of copies coalesced");
114 STATISTIC(NumPostRACopySink, "Number of copies sunk after RA");
115 
116 namespace {
117 
118 class MachineSinking : public MachineFunctionPass {
119   const TargetSubtargetInfo *STI = nullptr;
120   const TargetInstrInfo *TII = nullptr;
121   const TargetRegisterInfo *TRI = nullptr;
122   MachineRegisterInfo *MRI = nullptr;      // Machine register information
123   MachineDominatorTree *DT = nullptr;      // Machine dominator tree
124   MachinePostDominatorTree *PDT = nullptr; // Machine post dominator tree
125   MachineCycleInfo *CI = nullptr;
126   ProfileSummaryInfo *PSI = nullptr;
127   MachineBlockFrequencyInfo *MBFI = nullptr;
128   const MachineBranchProbabilityInfo *MBPI = nullptr;
129   AliasAnalysis *AA = nullptr;
130   RegisterClassInfo RegClassInfo;
131 
132   // Remember which edges have been considered for breaking.
133   SmallSet<std::pair<MachineBasicBlock *, MachineBasicBlock *>, 8>
134       CEBCandidates;
135   // Memorize the register that also wanted to sink into the same block along
136   // a different critical edge.
137   // {register to sink, sink-to block} -> the first sink-from block.
138   // We're recording the first sink-from block because that (critical) edge
139   // was deferred until we see another register that's going to sink into the
140   // same block.
141   DenseMap<std::pair<Register, MachineBasicBlock *>, MachineBasicBlock *>
142       CEMergeCandidates;
143   // Remember which edges we are about to split.
144   // This is different from CEBCandidates since those edges
145   // will be split.
146   SetVector<std::pair<MachineBasicBlock *, MachineBasicBlock *>> ToSplit;
147 
148   DenseSet<Register> RegsToClearKillFlags;
149 
150   using AllSuccsCache =
151       SmallDenseMap<MachineBasicBlock *, SmallVector<MachineBasicBlock *, 4>>;
152 
153   /// DBG_VALUE pointer and flag. The flag is true if this DBG_VALUE is
154   /// post-dominated by another DBG_VALUE of the same variable location.
155   /// This is necessary to detect sequences such as:
156   ///     %0 = someinst
157   ///     DBG_VALUE %0, !123, !DIExpression()
158   ///     %1 = anotherinst
159   ///     DBG_VALUE %1, !123, !DIExpression()
160   /// Where if %0 were to sink, the DBG_VAUE should not sink with it, as that
161   /// would re-order assignments.
162   using SeenDbgUser = PointerIntPair<MachineInstr *, 1>;
163 
164   /// Record of DBG_VALUE uses of vregs in a block, so that we can identify
165   /// debug instructions to sink.
166   SmallDenseMap<unsigned, TinyPtrVector<SeenDbgUser>> SeenDbgUsers;
167 
168   /// Record of debug variables that have had their locations set in the
169   /// current block.
170   DenseSet<DebugVariable> SeenDbgVars;
171 
172   DenseMap<std::pair<MachineBasicBlock *, MachineBasicBlock *>, bool>
173       HasStoreCache;
174 
175   DenseMap<std::pair<MachineBasicBlock *, MachineBasicBlock *>,
176            SmallVector<MachineInstr *>>
177       StoreInstrCache;
178 
179   /// Cached BB's register pressure.
180   DenseMap<const MachineBasicBlock *, std::vector<unsigned>>
181       CachedRegisterPressure;
182 
183   bool EnableSinkAndFold;
184 
185 public:
186   static char ID; // Pass identification
187 
188   MachineSinking() : MachineFunctionPass(ID) {
189     initializeMachineSinkingPass(*PassRegistry::getPassRegistry());
190   }
191 
192   bool runOnMachineFunction(MachineFunction &MF) override;
193 
194   void getAnalysisUsage(AnalysisUsage &AU) const override {
195     MachineFunctionPass::getAnalysisUsage(AU);
196     AU.addRequired<AAResultsWrapperPass>();
197     AU.addRequired<MachineDominatorTreeWrapperPass>();
198     AU.addRequired<MachinePostDominatorTreeWrapperPass>();
199     AU.addRequired<MachineCycleInfoWrapperPass>();
200     AU.addRequired<MachineBranchProbabilityInfoWrapperPass>();
201     AU.addPreserved<MachineCycleInfoWrapperPass>();
202     AU.addPreserved<MachineLoopInfoWrapperPass>();
203     AU.addRequired<ProfileSummaryInfoWrapperPass>();
204     if (UseBlockFreqInfo)
205       AU.addRequired<MachineBlockFrequencyInfoWrapperPass>();
206     AU.addRequired<TargetPassConfig>();
207   }
208 
209   void releaseMemory() override {
210     CEBCandidates.clear();
211     CEMergeCandidates.clear();
212   }
213 
214 private:
215   bool ProcessBlock(MachineBasicBlock &MBB);
216   void ProcessDbgInst(MachineInstr &MI);
217   bool isLegalToBreakCriticalEdge(MachineInstr &MI, MachineBasicBlock *From,
218                                   MachineBasicBlock *To, bool BreakPHIEdge);
219   bool isWorthBreakingCriticalEdge(MachineInstr &MI, MachineBasicBlock *From,
220                                    MachineBasicBlock *To,
221                                    MachineBasicBlock *&DeferredFromBlock);
222 
223   bool hasStoreBetween(MachineBasicBlock *From, MachineBasicBlock *To,
224                        MachineInstr &MI);
225 
226   /// Postpone the splitting of the given critical
227   /// edge (\p From, \p To).
228   ///
229   /// We do not split the edges on the fly. Indeed, this invalidates
230   /// the dominance information and thus triggers a lot of updates
231   /// of that information underneath.
232   /// Instead, we postpone all the splits after each iteration of
233   /// the main loop. That way, the information is at least valid
234   /// for the lifetime of an iteration.
235   ///
236   /// \return True if the edge is marked as toSplit, false otherwise.
237   /// False can be returned if, for instance, this is not profitable.
238   bool PostponeSplitCriticalEdge(MachineInstr &MI, MachineBasicBlock *From,
239                                  MachineBasicBlock *To, bool BreakPHIEdge);
240   bool SinkInstruction(MachineInstr &MI, bool &SawStore,
241                        AllSuccsCache &AllSuccessors);
242 
243   /// If we sink a COPY inst, some debug users of it's destination may no
244   /// longer be dominated by the COPY, and will eventually be dropped.
245   /// This is easily rectified by forwarding the non-dominated debug uses
246   /// to the copy source.
247   void SalvageUnsunkDebugUsersOfCopy(MachineInstr &,
248                                      MachineBasicBlock *TargetBlock);
249   bool AllUsesDominatedByBlock(Register Reg, MachineBasicBlock *MBB,
250                                MachineBasicBlock *DefMBB, bool &BreakPHIEdge,
251                                bool &LocalUse) const;
252   MachineBasicBlock *FindSuccToSinkTo(MachineInstr &MI, MachineBasicBlock *MBB,
253                                       bool &BreakPHIEdge,
254                                       AllSuccsCache &AllSuccessors);
255 
256   void FindCycleSinkCandidates(MachineCycle *Cycle, MachineBasicBlock *BB,
257                                SmallVectorImpl<MachineInstr *> &Candidates);
258   bool SinkIntoCycle(MachineCycle *Cycle, MachineInstr &I);
259 
260   bool isProfitableToSinkTo(Register Reg, MachineInstr &MI,
261                             MachineBasicBlock *MBB,
262                             MachineBasicBlock *SuccToSinkTo,
263                             AllSuccsCache &AllSuccessors);
264 
265   bool PerformTrivialForwardCoalescing(MachineInstr &MI,
266                                        MachineBasicBlock *MBB);
267 
268   bool PerformSinkAndFold(MachineInstr &MI, MachineBasicBlock *MBB);
269 
270   SmallVector<MachineBasicBlock *, 4> &
271   GetAllSortedSuccessors(MachineInstr &MI, MachineBasicBlock *MBB,
272                          AllSuccsCache &AllSuccessors) const;
273 
274   std::vector<unsigned> &getBBRegisterPressure(const MachineBasicBlock &MBB);
275 
276   bool registerPressureSetExceedsLimit(unsigned NRegs,
277                                        const TargetRegisterClass *RC,
278                                        const MachineBasicBlock &MBB);
279 };
280 
281 } // end anonymous namespace
282 
283 char MachineSinking::ID = 0;
284 
285 char &llvm::MachineSinkingID = MachineSinking::ID;
286 
287 INITIALIZE_PASS_BEGIN(MachineSinking, DEBUG_TYPE, "Machine code sinking", false,
288                       false)
289 INITIALIZE_PASS_DEPENDENCY(ProfileSummaryInfoWrapperPass)
290 INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfoWrapperPass)
291 INITIALIZE_PASS_DEPENDENCY(MachineDominatorTreeWrapperPass)
292 INITIALIZE_PASS_DEPENDENCY(MachineCycleInfoWrapperPass)
293 INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
294 INITIALIZE_PASS_END(MachineSinking, DEBUG_TYPE, "Machine code sinking", false,
295                     false)
296 
297 /// Return true if a target defined block prologue instruction interferes
298 /// with a sink candidate.
299 static bool blockPrologueInterferes(const MachineBasicBlock *BB,
300                                     MachineBasicBlock::const_iterator End,
301                                     const MachineInstr &MI,
302                                     const TargetRegisterInfo *TRI,
303                                     const TargetInstrInfo *TII,
304                                     const MachineRegisterInfo *MRI) {
305   for (MachineBasicBlock::const_iterator PI = BB->getFirstNonPHI(); PI != End;
306        ++PI) {
307     // Only check target defined prologue instructions
308     if (!TII->isBasicBlockPrologue(*PI))
309       continue;
310     for (auto &MO : MI.operands()) {
311       if (!MO.isReg())
312         continue;
313       Register Reg = MO.getReg();
314       if (!Reg)
315         continue;
316       if (MO.isUse()) {
317         if (Reg.isPhysical() &&
318             (TII->isIgnorableUse(MO) || (MRI && MRI->isConstantPhysReg(Reg))))
319           continue;
320         if (PI->modifiesRegister(Reg, TRI))
321           return true;
322       } else {
323         if (PI->readsRegister(Reg, TRI))
324           return true;
325         // Check for interference with non-dead defs
326         auto *DefOp = PI->findRegisterDefOperand(Reg, TRI, false, true);
327         if (DefOp && !DefOp->isDead())
328           return true;
329       }
330     }
331   }
332 
333   return false;
334 }
335 
336 bool MachineSinking::PerformTrivialForwardCoalescing(MachineInstr &MI,
337                                                      MachineBasicBlock *MBB) {
338   if (!MI.isCopy())
339     return false;
340 
341   Register SrcReg = MI.getOperand(1).getReg();
342   Register DstReg = MI.getOperand(0).getReg();
343   if (!SrcReg.isVirtual() || !DstReg.isVirtual() ||
344       !MRI->hasOneNonDBGUse(SrcReg))
345     return false;
346 
347   const TargetRegisterClass *SRC = MRI->getRegClass(SrcReg);
348   const TargetRegisterClass *DRC = MRI->getRegClass(DstReg);
349   if (SRC != DRC)
350     return false;
351 
352   MachineInstr *DefMI = MRI->getVRegDef(SrcReg);
353   if (DefMI->isCopyLike())
354     return false;
355   LLVM_DEBUG(dbgs() << "Coalescing: " << *DefMI);
356   LLVM_DEBUG(dbgs() << "*** to: " << MI);
357   MRI->replaceRegWith(DstReg, SrcReg);
358   MI.eraseFromParent();
359 
360   // Conservatively, clear any kill flags, since it's possible that they are no
361   // longer correct.
362   MRI->clearKillFlags(SrcReg);
363 
364   ++NumCoalesces;
365   return true;
366 }
367 
368 bool MachineSinking::PerformSinkAndFold(MachineInstr &MI,
369                                         MachineBasicBlock *MBB) {
370   if (MI.isCopy() || MI.mayLoadOrStore() ||
371       MI.getOpcode() == TargetOpcode::REG_SEQUENCE)
372     return false;
373 
374   // Don't sink instructions that the target prefers not to sink.
375   if (!TII->shouldSink(MI))
376     return false;
377 
378   // Check if it's safe to move the instruction.
379   bool SawStore = true;
380   if (!MI.isSafeToMove(SawStore))
381     return false;
382 
383   // Convergent operations may not be made control-dependent on additional
384   // values.
385   if (MI.isConvergent())
386     return false;
387 
388   // Don't sink defs/uses of hard registers or if the instruction defines more
389   // than one register.
390   // Don't sink more than two register uses - it'll cover most of the cases and
391   // greatly simplifies the register pressure checks.
392   Register DefReg;
393   Register UsedRegA, UsedRegB;
394   for (const MachineOperand &MO : MI.operands()) {
395     if (MO.isImm() || MO.isRegMask() || MO.isRegLiveOut() || MO.isMetadata() ||
396         MO.isMCSymbol() || MO.isDbgInstrRef() || MO.isCFIIndex() ||
397         MO.isIntrinsicID() || MO.isPredicate() || MO.isShuffleMask())
398       continue;
399     if (!MO.isReg())
400       return false;
401 
402     Register Reg = MO.getReg();
403     if (Reg == 0)
404       continue;
405 
406     if (Reg.isVirtual()) {
407       if (MO.isDef()) {
408         if (DefReg)
409           return false;
410         DefReg = Reg;
411         continue;
412       }
413 
414       if (UsedRegA == 0)
415         UsedRegA = Reg;
416       else if (UsedRegB == 0)
417         UsedRegB = Reg;
418       else
419         return false;
420       continue;
421     }
422 
423     if (Reg.isPhysical() && MO.isUse() &&
424         (MRI->isConstantPhysReg(Reg) || TII->isIgnorableUse(MO)))
425       continue;
426 
427     return false;
428   }
429 
430   // Scan uses of the destination register. Every use, except the last, must be
431   // a copy, with a chain of copies terminating with either a copy into a hard
432   // register, or a load/store instruction where the use is part of the
433   // address (*not* the stored value).
434   using SinkInfo = std::pair<MachineInstr *, ExtAddrMode>;
435   SmallVector<SinkInfo> SinkInto;
436   SmallVector<Register> Worklist;
437 
438   const TargetRegisterClass *RC = MRI->getRegClass(DefReg);
439   const TargetRegisterClass *RCA =
440       UsedRegA == 0 ? nullptr : MRI->getRegClass(UsedRegA);
441   const TargetRegisterClass *RCB =
442       UsedRegB == 0 ? nullptr : MRI->getRegClass(UsedRegB);
443 
444   Worklist.push_back(DefReg);
445   while (!Worklist.empty()) {
446     Register Reg = Worklist.pop_back_val();
447 
448     for (MachineOperand &MO : MRI->use_nodbg_operands(Reg)) {
449       ExtAddrMode MaybeAM;
450       MachineInstr &UseInst = *MO.getParent();
451       if (UseInst.isCopy()) {
452         Register DstReg;
453         if (const MachineOperand &O = UseInst.getOperand(0); O.isReg())
454           DstReg = O.getReg();
455         if (DstReg == 0)
456           return false;
457         if (DstReg.isVirtual()) {
458           Worklist.push_back(DstReg);
459           continue;
460         }
461         // If we are going to replace a copy, the original instruction must be
462         // as cheap as a copy.
463         if (!TII->isAsCheapAsAMove(MI))
464           return false;
465         // The hard register must be in the register class of the original
466         // instruction's destination register.
467         if (!RC->contains(DstReg))
468           return false;
469       } else if (UseInst.mayLoadOrStore()) {
470         ExtAddrMode AM;
471         if (!TII->canFoldIntoAddrMode(UseInst, Reg, MI, AM))
472           return false;
473         MaybeAM = AM;
474       } else {
475         return false;
476       }
477 
478       if (UseInst.getParent() != MI.getParent()) {
479         // If the register class of the register we are replacing is a superset
480         // of any of the register classes of the operands of the materialized
481         // instruction don't consider that live range extended.
482         const TargetRegisterClass *RCS = MRI->getRegClass(Reg);
483         if (RCA && RCA->hasSuperClassEq(RCS))
484           RCA = nullptr;
485         else if (RCB && RCB->hasSuperClassEq(RCS))
486           RCB = nullptr;
487         if (RCA || RCB) {
488           if (RCA == nullptr) {
489             RCA = RCB;
490             RCB = nullptr;
491           }
492 
493           unsigned NRegs = !!RCA + !!RCB;
494           if (RCA == RCB)
495             RCB = nullptr;
496 
497           // Check we don't exceed register pressure at the destination.
498           const MachineBasicBlock &MBB = *UseInst.getParent();
499           if (RCB == nullptr) {
500             if (registerPressureSetExceedsLimit(NRegs, RCA, MBB))
501               return false;
502           } else if (registerPressureSetExceedsLimit(1, RCA, MBB) ||
503                      registerPressureSetExceedsLimit(1, RCB, MBB)) {
504             return false;
505           }
506         }
507       }
508 
509       SinkInto.emplace_back(&UseInst, MaybeAM);
510     }
511   }
512 
513   if (SinkInto.empty())
514     return false;
515 
516   // Now we know we can fold the instruction in all its users.
517   for (auto &[SinkDst, MaybeAM] : SinkInto) {
518     MachineInstr *New = nullptr;
519     LLVM_DEBUG(dbgs() << "Sinking copy of"; MI.dump(); dbgs() << "into";
520                SinkDst->dump());
521     if (SinkDst->isCopy()) {
522       // TODO: After performing the sink-and-fold, the original instruction is
523       // deleted. Its value is still available (in a hard register), so if there
524       // are debug instructions which refer to the (now deleted) virtual
525       // register they could be updated to refer to the hard register, in
526       // principle. However, it's not clear how to do that, moreover in some
527       // cases the debug instructions may need to be replicated proportionally
528       // to the number of the COPY instructions replaced and in some extreme
529       // cases we can end up with quadratic increase in the number of debug
530       // instructions.
531 
532       // Sink a copy of the instruction, replacing a COPY instruction.
533       MachineBasicBlock::iterator InsertPt = SinkDst->getIterator();
534       Register DstReg = SinkDst->getOperand(0).getReg();
535       TII->reMaterialize(*SinkDst->getParent(), InsertPt, DstReg, 0, MI, *TRI);
536       New = &*std::prev(InsertPt);
537       if (!New->getDebugLoc())
538         New->setDebugLoc(SinkDst->getDebugLoc());
539 
540       // The operand registers of the "sunk" instruction have their live range
541       // extended and their kill flags may no longer be correct. Conservatively
542       // clear the kill flags.
543       if (UsedRegA)
544         MRI->clearKillFlags(UsedRegA);
545       if (UsedRegB)
546         MRI->clearKillFlags(UsedRegB);
547     } else {
548       // Fold instruction into the addressing mode of a memory instruction.
549       New = TII->emitLdStWithAddr(*SinkDst, MaybeAM);
550 
551       // The registers of the addressing mode may have their live range extended
552       // and their kill flags may no longer be correct. Conservatively clear the
553       // kill flags.
554       if (Register R = MaybeAM.BaseReg; R.isValid() && R.isVirtual())
555         MRI->clearKillFlags(R);
556       if (Register R = MaybeAM.ScaledReg; R.isValid() && R.isVirtual())
557         MRI->clearKillFlags(R);
558     }
559     LLVM_DEBUG(dbgs() << "yielding"; New->dump());
560     // Clear the StoreInstrCache, since we may invalidate it by erasing.
561     if (SinkDst->mayStore() && !SinkDst->hasOrderedMemoryRef())
562       StoreInstrCache.clear();
563     SinkDst->eraseFromParent();
564   }
565 
566   // Collect operands that need to be cleaned up because the registers no longer
567   // exist (in COPYs and debug instructions). We cannot delete instructions or
568   // clear operands while traversing register uses.
569   SmallVector<MachineOperand *> Cleanup;
570   Worklist.push_back(DefReg);
571   while (!Worklist.empty()) {
572     Register Reg = Worklist.pop_back_val();
573     for (MachineOperand &MO : MRI->use_operands(Reg)) {
574       MachineInstr *U = MO.getParent();
575       assert((U->isCopy() || U->isDebugInstr()) &&
576              "Only debug uses and copies must remain");
577       if (U->isCopy())
578         Worklist.push_back(U->getOperand(0).getReg());
579       Cleanup.push_back(&MO);
580     }
581   }
582 
583   // Delete the dead COPYs and clear operands in debug instructions
584   for (MachineOperand *MO : Cleanup) {
585     MachineInstr *I = MO->getParent();
586     if (I->isCopy()) {
587       I->eraseFromParent();
588     } else {
589       MO->setReg(0);
590       MO->setSubReg(0);
591     }
592   }
593 
594   MI.eraseFromParent();
595   return true;
596 }
597 
598 /// AllUsesDominatedByBlock - Return true if all uses of the specified register
599 /// occur in blocks dominated by the specified block. If any use is in the
600 /// definition block, then return false since it is never legal to move def
601 /// after uses.
602 bool MachineSinking::AllUsesDominatedByBlock(Register Reg,
603                                              MachineBasicBlock *MBB,
604                                              MachineBasicBlock *DefMBB,
605                                              bool &BreakPHIEdge,
606                                              bool &LocalUse) const {
607   assert(Reg.isVirtual() && "Only makes sense for vregs");
608 
609   // Ignore debug uses because debug info doesn't affect the code.
610   if (MRI->use_nodbg_empty(Reg))
611     return true;
612 
613   // BreakPHIEdge is true if all the uses are in the successor MBB being sunken
614   // into and they are all PHI nodes. In this case, machine-sink must break
615   // the critical edge first. e.g.
616   //
617   // %bb.1:
618   //   Predecessors according to CFG: %bb.0
619   //     ...
620   //     %def = DEC64_32r %x, implicit-def dead %eflags
621   //     ...
622   //     JE_4 <%bb.37>, implicit %eflags
623   //   Successors according to CFG: %bb.37 %bb.2
624   //
625   // %bb.2:
626   //     %p = PHI %y, %bb.0, %def, %bb.1
627   if (all_of(MRI->use_nodbg_operands(Reg), [&](MachineOperand &MO) {
628         MachineInstr *UseInst = MO.getParent();
629         unsigned OpNo = MO.getOperandNo();
630         MachineBasicBlock *UseBlock = UseInst->getParent();
631         return UseBlock == MBB && UseInst->isPHI() &&
632                UseInst->getOperand(OpNo + 1).getMBB() == DefMBB;
633       })) {
634     BreakPHIEdge = true;
635     return true;
636   }
637 
638   for (MachineOperand &MO : MRI->use_nodbg_operands(Reg)) {
639     // Determine the block of the use.
640     MachineInstr *UseInst = MO.getParent();
641     unsigned OpNo = &MO - &UseInst->getOperand(0);
642     MachineBasicBlock *UseBlock = UseInst->getParent();
643     if (UseInst->isPHI()) {
644       // PHI nodes use the operand in the predecessor block, not the block with
645       // the PHI.
646       UseBlock = UseInst->getOperand(OpNo + 1).getMBB();
647     } else if (UseBlock == DefMBB) {
648       LocalUse = true;
649       return false;
650     }
651 
652     // Check that it dominates.
653     if (!DT->dominates(MBB, UseBlock))
654       return false;
655   }
656 
657   return true;
658 }
659 
660 /// Return true if this machine instruction loads from global offset table or
661 /// constant pool.
662 static bool mayLoadFromGOTOrConstantPool(MachineInstr &MI) {
663   assert(MI.mayLoad() && "Expected MI that loads!");
664 
665   // If we lost memory operands, conservatively assume that the instruction
666   // reads from everything..
667   if (MI.memoperands_empty())
668     return true;
669 
670   for (MachineMemOperand *MemOp : MI.memoperands())
671     if (const PseudoSourceValue *PSV = MemOp->getPseudoValue())
672       if (PSV->isGOT() || PSV->isConstantPool())
673         return true;
674 
675   return false;
676 }
677 
678 void MachineSinking::FindCycleSinkCandidates(
679     MachineCycle *Cycle, MachineBasicBlock *BB,
680     SmallVectorImpl<MachineInstr *> &Candidates) {
681   for (auto &MI : *BB) {
682     LLVM_DEBUG(dbgs() << "CycleSink: Analysing candidate: " << MI);
683     if (!TII->shouldSink(MI)) {
684       LLVM_DEBUG(dbgs() << "CycleSink: Instruction not a candidate for this "
685                            "target\n");
686       continue;
687     }
688     if (!isCycleInvariant(Cycle, MI)) {
689       LLVM_DEBUG(dbgs() << "CycleSink: Instruction is not cycle invariant\n");
690       continue;
691     }
692     bool DontMoveAcrossStore = true;
693     if (!MI.isSafeToMove(DontMoveAcrossStore)) {
694       LLVM_DEBUG(dbgs() << "CycleSink: Instruction not safe to move.\n");
695       continue;
696     }
697     if (MI.mayLoad() && !mayLoadFromGOTOrConstantPool(MI)) {
698       LLVM_DEBUG(dbgs() << "CycleSink: Dont sink GOT or constant pool loads\n");
699       continue;
700     }
701     if (MI.isConvergent())
702       continue;
703 
704     const MachineOperand &MO = MI.getOperand(0);
705     if (!MO.isReg() || !MO.getReg() || !MO.isDef())
706       continue;
707     if (!MRI->hasOneDef(MO.getReg()))
708       continue;
709 
710     LLVM_DEBUG(dbgs() << "CycleSink: Instruction added as candidate.\n");
711     Candidates.push_back(&MI);
712   }
713 }
714 
715 bool MachineSinking::runOnMachineFunction(MachineFunction &MF) {
716   if (skipFunction(MF.getFunction()))
717     return false;
718 
719   LLVM_DEBUG(dbgs() << "******** Machine Sinking ********\n");
720 
721   STI = &MF.getSubtarget();
722   TII = STI->getInstrInfo();
723   TRI = STI->getRegisterInfo();
724   MRI = &MF.getRegInfo();
725   DT = &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
726   PDT = &getAnalysis<MachinePostDominatorTreeWrapperPass>().getPostDomTree();
727   CI = &getAnalysis<MachineCycleInfoWrapperPass>().getCycleInfo();
728   PSI = &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();
729   MBFI = UseBlockFreqInfo
730              ? &getAnalysis<MachineBlockFrequencyInfoWrapperPass>().getMBFI()
731              : nullptr;
732   MBPI = &getAnalysis<MachineBranchProbabilityInfoWrapperPass>().getMBPI();
733   AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
734   RegClassInfo.runOnMachineFunction(MF);
735   TargetPassConfig *PassConfig = &getAnalysis<TargetPassConfig>();
736   EnableSinkAndFold = PassConfig->getEnableSinkAndFold();
737 
738   bool EverMadeChange = false;
739 
740   while (true) {
741     bool MadeChange = false;
742 
743     // Process all basic blocks.
744     CEBCandidates.clear();
745     CEMergeCandidates.clear();
746     ToSplit.clear();
747     for (auto &MBB : MF)
748       MadeChange |= ProcessBlock(MBB);
749 
750     // If we have anything we marked as toSplit, split it now.
751     MachineDomTreeUpdater MDTU(DT, PDT,
752                                MachineDomTreeUpdater::UpdateStrategy::Lazy);
753     for (const auto &Pair : ToSplit) {
754       auto NewSucc =
755           Pair.first->SplitCriticalEdge(Pair.second, *this, nullptr, &MDTU);
756       if (NewSucc != nullptr) {
757         LLVM_DEBUG(dbgs() << " *** Splitting critical edge: "
758                           << printMBBReference(*Pair.first) << " -- "
759                           << printMBBReference(*NewSucc) << " -- "
760                           << printMBBReference(*Pair.second) << '\n');
761         if (MBFI)
762           MBFI->onEdgeSplit(*Pair.first, *NewSucc, *MBPI);
763 
764         MadeChange = true;
765         ++NumSplit;
766         CI->splitCriticalEdge(Pair.first, Pair.second, NewSucc);
767       } else
768         LLVM_DEBUG(dbgs() << " *** Not legal to break critical edge\n");
769     }
770     // If this iteration over the code changed anything, keep iterating.
771     if (!MadeChange)
772       break;
773     EverMadeChange = true;
774   }
775 
776   if (SinkInstsIntoCycle) {
777     SmallVector<MachineCycle *, 8> Cycles(CI->toplevel_cycles());
778     for (auto *Cycle : Cycles) {
779       MachineBasicBlock *Preheader = Cycle->getCyclePreheader();
780       if (!Preheader) {
781         LLVM_DEBUG(dbgs() << "CycleSink: Can't find preheader\n");
782         continue;
783       }
784       SmallVector<MachineInstr *, 8> Candidates;
785       FindCycleSinkCandidates(Cycle, Preheader, Candidates);
786 
787       // Walk the candidates in reverse order so that we start with the use
788       // of a def-use chain, if there is any.
789       // TODO: Sort the candidates using a cost-model.
790       unsigned i = 0;
791       for (MachineInstr *I : llvm::reverse(Candidates)) {
792         if (i++ == SinkIntoCycleLimit) {
793           LLVM_DEBUG(dbgs() << "CycleSink:   Limit reached of instructions to "
794                                "be analysed.");
795           break;
796         }
797 
798         if (!SinkIntoCycle(Cycle, *I))
799           break;
800         EverMadeChange = true;
801         ++NumCycleSunk;
802       }
803     }
804   }
805 
806   HasStoreCache.clear();
807   StoreInstrCache.clear();
808 
809   // Now clear any kill flags for recorded registers.
810   for (auto I : RegsToClearKillFlags)
811     MRI->clearKillFlags(I);
812   RegsToClearKillFlags.clear();
813 
814   return EverMadeChange;
815 }
816 
817 bool MachineSinking::ProcessBlock(MachineBasicBlock &MBB) {
818   if ((!EnableSinkAndFold && MBB.succ_size() <= 1) || MBB.empty())
819     return false;
820 
821   // Don't bother sinking code out of unreachable blocks. In addition to being
822   // unprofitable, it can also lead to infinite looping, because in an
823   // unreachable cycle there may be nowhere to stop.
824   if (!DT->isReachableFromEntry(&MBB))
825     return false;
826 
827   bool MadeChange = false;
828 
829   // Cache all successors, sorted by frequency info and cycle depth.
830   AllSuccsCache AllSuccessors;
831 
832   // Walk the basic block bottom-up.  Remember if we saw a store.
833   MachineBasicBlock::iterator I = MBB.end();
834   --I;
835   bool ProcessedBegin, SawStore = false;
836   do {
837     MachineInstr &MI = *I; // The instruction to sink.
838 
839     // Predecrement I (if it's not begin) so that it isn't invalidated by
840     // sinking.
841     ProcessedBegin = I == MBB.begin();
842     if (!ProcessedBegin)
843       --I;
844 
845     if (MI.isDebugOrPseudoInstr() || MI.isFakeUse()) {
846       if (MI.isDebugValue())
847         ProcessDbgInst(MI);
848       continue;
849     }
850 
851     if (EnableSinkAndFold && PerformSinkAndFold(MI, &MBB)) {
852       MadeChange = true;
853       continue;
854     }
855 
856     // Can't sink anything out of a block that has less than two successors.
857     if (MBB.succ_size() <= 1)
858       continue;
859 
860     if (PerformTrivialForwardCoalescing(MI, &MBB)) {
861       MadeChange = true;
862       continue;
863     }
864 
865     if (SinkInstruction(MI, SawStore, AllSuccessors)) {
866       ++NumSunk;
867       MadeChange = true;
868     }
869 
870     // If we just processed the first instruction in the block, we're done.
871   } while (!ProcessedBegin);
872 
873   SeenDbgUsers.clear();
874   SeenDbgVars.clear();
875   // recalculate the bb register pressure after sinking one BB.
876   CachedRegisterPressure.clear();
877   return MadeChange;
878 }
879 
880 void MachineSinking::ProcessDbgInst(MachineInstr &MI) {
881   // When we see DBG_VALUEs for registers, record any vreg it reads, so that
882   // we know what to sink if the vreg def sinks.
883   assert(MI.isDebugValue() && "Expected DBG_VALUE for processing");
884 
885   DebugVariable Var(MI.getDebugVariable(), MI.getDebugExpression(),
886                     MI.getDebugLoc()->getInlinedAt());
887   bool SeenBefore = SeenDbgVars.contains(Var);
888 
889   for (MachineOperand &MO : MI.debug_operands()) {
890     if (MO.isReg() && MO.getReg().isVirtual())
891       SeenDbgUsers[MO.getReg()].push_back(SeenDbgUser(&MI, SeenBefore));
892   }
893 
894   // Record the variable for any DBG_VALUE, to avoid re-ordering any of them.
895   SeenDbgVars.insert(Var);
896 }
897 
898 bool MachineSinking::isWorthBreakingCriticalEdge(
899     MachineInstr &MI, MachineBasicBlock *From, MachineBasicBlock *To,
900     MachineBasicBlock *&DeferredFromBlock) {
901   // FIXME: Need much better heuristics.
902 
903   // If the pass has already considered breaking this edge (during this pass
904   // through the function), then let's go ahead and break it. This means
905   // sinking multiple "cheap" instructions into the same block.
906   if (!CEBCandidates.insert(std::make_pair(From, To)).second)
907     return true;
908 
909   if (!MI.isCopy() && !TII->isAsCheapAsAMove(MI))
910     return true;
911 
912   // Check and record the register and the destination block we want to sink
913   // into. Note that we want to do the following before the next check on branch
914   // probability. Because we want to record the initial candidate even if it's
915   // on hot edge, so that other candidates that might not on hot edges can be
916   // sinked as well.
917   for (const auto &MO : MI.all_defs()) {
918     Register Reg = MO.getReg();
919     if (!Reg)
920       continue;
921     Register SrcReg = Reg.isVirtual() ? TRI->lookThruCopyLike(Reg, MRI) : Reg;
922     auto Key = std::make_pair(SrcReg, To);
923     auto Res = CEMergeCandidates.try_emplace(Key, From);
924     // We wanted to sink the same register into the same block, consider it to
925     // be profitable.
926     if (!Res.second) {
927       // Return the source block that was previously held off.
928       DeferredFromBlock = Res.first->second;
929       return true;
930     }
931   }
932 
933   if (From->isSuccessor(To) &&
934       MBPI->getEdgeProbability(From, To) <=
935           BranchProbability(SplitEdgeProbabilityThreshold, 100))
936     return true;
937 
938   // MI is cheap, we probably don't want to break the critical edge for it.
939   // However, if this would allow some definitions of its source operands
940   // to be sunk then it's probably worth it.
941   for (const MachineOperand &MO : MI.all_uses()) {
942     Register Reg = MO.getReg();
943     if (Reg == 0)
944       continue;
945 
946     // We don't move live definitions of physical registers,
947     // so sinking their uses won't enable any opportunities.
948     if (Reg.isPhysical())
949       continue;
950 
951     // If this instruction is the only user of a virtual register,
952     // check if breaking the edge will enable sinking
953     // both this instruction and the defining instruction.
954     if (MRI->hasOneNonDBGUse(Reg)) {
955       // If the definition resides in same MBB,
956       // claim it's likely we can sink these together.
957       // If definition resides elsewhere, we aren't
958       // blocking it from being sunk so don't break the edge.
959       MachineInstr *DefMI = MRI->getVRegDef(Reg);
960       if (DefMI->getParent() == MI.getParent())
961         return true;
962     }
963   }
964 
965   // Let the target decide if it's worth breaking this
966   // critical edge for a "cheap" instruction.
967   return TII->shouldBreakCriticalEdgeToSink(MI);
968 }
969 
970 bool MachineSinking::isLegalToBreakCriticalEdge(MachineInstr &MI,
971                                                 MachineBasicBlock *FromBB,
972                                                 MachineBasicBlock *ToBB,
973                                                 bool BreakPHIEdge) {
974   // Avoid breaking back edge. From == To means backedge for single BB cycle.
975   if (!SplitEdges || FromBB == ToBB || !FromBB->isSuccessor(ToBB))
976     return false;
977 
978   MachineCycle *FromCycle = CI->getCycle(FromBB);
979   MachineCycle *ToCycle = CI->getCycle(ToBB);
980 
981   // Check for backedges of more "complex" cycles.
982   if (FromCycle == ToCycle && FromCycle &&
983       (!FromCycle->isReducible() || FromCycle->getHeader() == ToBB))
984     return false;
985 
986   // It's not always legal to break critical edges and sink the computation
987   // to the edge.
988   //
989   // %bb.1:
990   // v1024
991   // Beq %bb.3
992   // <fallthrough>
993   // %bb.2:
994   // ... no uses of v1024
995   // <fallthrough>
996   // %bb.3:
997   // ...
998   //       = v1024
999   //
1000   // If %bb.1 -> %bb.3 edge is broken and computation of v1024 is inserted:
1001   //
1002   // %bb.1:
1003   // ...
1004   // Bne %bb.2
1005   // %bb.4:
1006   // v1024 =
1007   // B %bb.3
1008   // %bb.2:
1009   // ... no uses of v1024
1010   // <fallthrough>
1011   // %bb.3:
1012   // ...
1013   //       = v1024
1014   //
1015   // This is incorrect since v1024 is not computed along the %bb.1->%bb.2->%bb.3
1016   // flow. We need to ensure the new basic block where the computation is
1017   // sunk to dominates all the uses.
1018   // It's only legal to break critical edge and sink the computation to the
1019   // new block if all the predecessors of "To", except for "From", are
1020   // not dominated by "From". Given SSA property, this means these
1021   // predecessors are dominated by "To".
1022   //
1023   // There is no need to do this check if all the uses are PHI nodes. PHI
1024   // sources are only defined on the specific predecessor edges.
1025   if (!BreakPHIEdge) {
1026     for (MachineBasicBlock *Pred : ToBB->predecessors())
1027       if (Pred != FromBB && !DT->dominates(ToBB, Pred))
1028         return false;
1029   }
1030 
1031   return true;
1032 }
1033 
1034 bool MachineSinking::PostponeSplitCriticalEdge(MachineInstr &MI,
1035                                                MachineBasicBlock *FromBB,
1036                                                MachineBasicBlock *ToBB,
1037                                                bool BreakPHIEdge) {
1038   bool Status = false;
1039   MachineBasicBlock *DeferredFromBB = nullptr;
1040   if (isWorthBreakingCriticalEdge(MI, FromBB, ToBB, DeferredFromBB)) {
1041     // If there is a DeferredFromBB, we consider FromBB only if _both_
1042     // of them are legal to split.
1043     if ((!DeferredFromBB ||
1044          ToSplit.count(std::make_pair(DeferredFromBB, ToBB)) ||
1045          isLegalToBreakCriticalEdge(MI, DeferredFromBB, ToBB, BreakPHIEdge)) &&
1046         isLegalToBreakCriticalEdge(MI, FromBB, ToBB, BreakPHIEdge)) {
1047       ToSplit.insert(std::make_pair(FromBB, ToBB));
1048       if (DeferredFromBB)
1049         ToSplit.insert(std::make_pair(DeferredFromBB, ToBB));
1050       Status = true;
1051     }
1052   }
1053 
1054   return Status;
1055 }
1056 
1057 std::vector<unsigned> &
1058 MachineSinking::getBBRegisterPressure(const MachineBasicBlock &MBB) {
1059   // Currently to save compiling time, MBB's register pressure will not change
1060   // in one ProcessBlock iteration because of CachedRegisterPressure. but MBB's
1061   // register pressure is changed after sinking any instructions into it.
1062   // FIXME: need a accurate and cheap register pressure estiminate model here.
1063   auto RP = CachedRegisterPressure.find(&MBB);
1064   if (RP != CachedRegisterPressure.end())
1065     return RP->second;
1066 
1067   RegionPressure Pressure;
1068   RegPressureTracker RPTracker(Pressure);
1069 
1070   // Initialize the register pressure tracker.
1071   RPTracker.init(MBB.getParent(), &RegClassInfo, nullptr, &MBB, MBB.end(),
1072                  /*TrackLaneMasks*/ false, /*TrackUntiedDefs=*/true);
1073 
1074   for (MachineBasicBlock::const_iterator MII = MBB.instr_end(),
1075                                          MIE = MBB.instr_begin();
1076        MII != MIE; --MII) {
1077     const MachineInstr &MI = *std::prev(MII);
1078     if (MI.isDebugInstr() || MI.isPseudoProbe())
1079       continue;
1080     RegisterOperands RegOpers;
1081     RegOpers.collect(MI, *TRI, *MRI, false, false);
1082     RPTracker.recedeSkipDebugValues();
1083     assert(&*RPTracker.getPos() == &MI && "RPTracker sync error!");
1084     RPTracker.recede(RegOpers);
1085   }
1086 
1087   RPTracker.closeRegion();
1088   auto It = CachedRegisterPressure.insert(
1089       std::make_pair(&MBB, RPTracker.getPressure().MaxSetPressure));
1090   return It.first->second;
1091 }
1092 
1093 bool MachineSinking::registerPressureSetExceedsLimit(
1094     unsigned NRegs, const TargetRegisterClass *RC,
1095     const MachineBasicBlock &MBB) {
1096   unsigned Weight = NRegs * TRI->getRegClassWeight(RC).RegWeight;
1097   const int *PS = TRI->getRegClassPressureSets(RC);
1098   std::vector<unsigned> BBRegisterPressure = getBBRegisterPressure(MBB);
1099   for (; *PS != -1; PS++)
1100     if (Weight + BBRegisterPressure[*PS] >=
1101         TRI->getRegPressureSetLimit(*MBB.getParent(), *PS))
1102       return true;
1103   return false;
1104 }
1105 
1106 /// isProfitableToSinkTo - Return true if it is profitable to sink MI.
1107 bool MachineSinking::isProfitableToSinkTo(Register Reg, MachineInstr &MI,
1108                                           MachineBasicBlock *MBB,
1109                                           MachineBasicBlock *SuccToSinkTo,
1110                                           AllSuccsCache &AllSuccessors) {
1111   assert(SuccToSinkTo && "Invalid SinkTo Candidate BB");
1112 
1113   if (MBB == SuccToSinkTo)
1114     return false;
1115 
1116   // It is profitable if SuccToSinkTo does not post dominate current block.
1117   if (!PDT->dominates(SuccToSinkTo, MBB))
1118     return true;
1119 
1120   // It is profitable to sink an instruction from a deeper cycle to a shallower
1121   // cycle, even if the latter post-dominates the former (PR21115).
1122   if (CI->getCycleDepth(MBB) > CI->getCycleDepth(SuccToSinkTo))
1123     return true;
1124 
1125   // Check if only use in post dominated block is PHI instruction.
1126   bool NonPHIUse = false;
1127   for (MachineInstr &UseInst : MRI->use_nodbg_instructions(Reg)) {
1128     MachineBasicBlock *UseBlock = UseInst.getParent();
1129     if (UseBlock == SuccToSinkTo && !UseInst.isPHI())
1130       NonPHIUse = true;
1131   }
1132   if (!NonPHIUse)
1133     return true;
1134 
1135   // If SuccToSinkTo post dominates then also it may be profitable if MI
1136   // can further profitably sinked into another block in next round.
1137   bool BreakPHIEdge = false;
1138   // FIXME - If finding successor is compile time expensive then cache results.
1139   if (MachineBasicBlock *MBB2 =
1140           FindSuccToSinkTo(MI, SuccToSinkTo, BreakPHIEdge, AllSuccessors))
1141     return isProfitableToSinkTo(Reg, MI, SuccToSinkTo, MBB2, AllSuccessors);
1142 
1143   MachineCycle *MCycle = CI->getCycle(MBB);
1144 
1145   // If the instruction is not inside a cycle, it is not profitable to sink MI
1146   // to a post dominate block SuccToSinkTo.
1147   if (!MCycle)
1148     return false;
1149 
1150   // If this instruction is inside a Cycle and sinking this instruction can make
1151   // more registers live range shorten, it is still prifitable.
1152   for (const MachineOperand &MO : MI.operands()) {
1153     // Ignore non-register operands.
1154     if (!MO.isReg())
1155       continue;
1156     Register Reg = MO.getReg();
1157     if (Reg == 0)
1158       continue;
1159 
1160     if (Reg.isPhysical()) {
1161       // Don't handle non-constant and non-ignorable physical register uses.
1162       if (MO.isUse() && !MRI->isConstantPhysReg(Reg) &&
1163           !TII->isIgnorableUse(MO))
1164         return false;
1165       continue;
1166     }
1167 
1168     // Users for the defs are all dominated by SuccToSinkTo.
1169     if (MO.isDef()) {
1170       // This def register's live range is shortened after sinking.
1171       bool LocalUse = false;
1172       if (!AllUsesDominatedByBlock(Reg, SuccToSinkTo, MBB, BreakPHIEdge,
1173                                    LocalUse))
1174         return false;
1175     } else {
1176       MachineInstr *DefMI = MRI->getVRegDef(Reg);
1177       if (!DefMI)
1178         continue;
1179       MachineCycle *Cycle = CI->getCycle(DefMI->getParent());
1180       // DefMI is defined outside of cycle. There should be no live range
1181       // impact for this operand. Defination outside of cycle means:
1182       // 1: defination is outside of cycle.
1183       // 2: defination is in this cycle, but it is a PHI in the cycle header.
1184       if (Cycle != MCycle || (DefMI->isPHI() && Cycle && Cycle->isReducible() &&
1185                               Cycle->getHeader() == DefMI->getParent()))
1186         continue;
1187       // The DefMI is defined inside the cycle.
1188       // If sinking this operand makes some register pressure set exceed limit,
1189       // it is not profitable.
1190       if (registerPressureSetExceedsLimit(1, MRI->getRegClass(Reg),
1191                                           *SuccToSinkTo)) {
1192         LLVM_DEBUG(dbgs() << "register pressure exceed limit, not profitable.");
1193         return false;
1194       }
1195     }
1196   }
1197 
1198   // If MI is in cycle and all its operands are alive across the whole cycle or
1199   // if no operand sinking make register pressure set exceed limit, it is
1200   // profitable to sink MI.
1201   return true;
1202 }
1203 
1204 /// Get the sorted sequence of successors for this MachineBasicBlock, possibly
1205 /// computing it if it was not already cached.
1206 SmallVector<MachineBasicBlock *, 4> &
1207 MachineSinking::GetAllSortedSuccessors(MachineInstr &MI, MachineBasicBlock *MBB,
1208                                        AllSuccsCache &AllSuccessors) const {
1209   // Do we have the sorted successors in cache ?
1210   auto Succs = AllSuccessors.find(MBB);
1211   if (Succs != AllSuccessors.end())
1212     return Succs->second;
1213 
1214   SmallVector<MachineBasicBlock *, 4> AllSuccs(MBB->successors());
1215 
1216   // Handle cases where sinking can happen but where the sink point isn't a
1217   // successor. For example:
1218   //
1219   //   x = computation
1220   //   if () {} else {}
1221   //   use x
1222   //
1223   for (MachineDomTreeNode *DTChild : DT->getNode(MBB)->children()) {
1224     // DomTree children of MBB that have MBB as immediate dominator are added.
1225     if (DTChild->getIDom()->getBlock() == MI.getParent() &&
1226         // Skip MBBs already added to the AllSuccs vector above.
1227         !MBB->isSuccessor(DTChild->getBlock()))
1228       AllSuccs.push_back(DTChild->getBlock());
1229   }
1230 
1231   // Sort Successors according to their cycle depth or block frequency info.
1232   llvm::stable_sort(
1233       AllSuccs, [&](const MachineBasicBlock *L, const MachineBasicBlock *R) {
1234         uint64_t LHSFreq = MBFI ? MBFI->getBlockFreq(L).getFrequency() : 0;
1235         uint64_t RHSFreq = MBFI ? MBFI->getBlockFreq(R).getFrequency() : 0;
1236         if (llvm::shouldOptimizeForSize(MBB, PSI, MBFI) ||
1237             (!LHSFreq && !RHSFreq))
1238           return CI->getCycleDepth(L) < CI->getCycleDepth(R);
1239         return LHSFreq < RHSFreq;
1240       });
1241 
1242   auto it = AllSuccessors.insert(std::make_pair(MBB, AllSuccs));
1243 
1244   return it.first->second;
1245 }
1246 
1247 /// FindSuccToSinkTo - Find a successor to sink this instruction to.
1248 MachineBasicBlock *
1249 MachineSinking::FindSuccToSinkTo(MachineInstr &MI, MachineBasicBlock *MBB,
1250                                  bool &BreakPHIEdge,
1251                                  AllSuccsCache &AllSuccessors) {
1252   assert(MBB && "Invalid MachineBasicBlock!");
1253 
1254   // loop over all the operands of the specified instruction.  If there is
1255   // anything we can't handle, bail out.
1256 
1257   // SuccToSinkTo - This is the successor to sink this instruction to, once we
1258   // decide.
1259   MachineBasicBlock *SuccToSinkTo = nullptr;
1260   for (const MachineOperand &MO : MI.operands()) {
1261     if (!MO.isReg())
1262       continue; // Ignore non-register operands.
1263 
1264     Register Reg = MO.getReg();
1265     if (Reg == 0)
1266       continue;
1267 
1268     if (Reg.isPhysical()) {
1269       if (MO.isUse()) {
1270         // If the physreg has no defs anywhere, it's just an ambient register
1271         // and we can freely move its uses. Alternatively, if it's allocatable,
1272         // it could get allocated to something with a def during allocation.
1273         if (!MRI->isConstantPhysReg(Reg) && !TII->isIgnorableUse(MO))
1274           return nullptr;
1275       } else if (!MO.isDead()) {
1276         // A def that isn't dead. We can't move it.
1277         return nullptr;
1278       }
1279     } else {
1280       // Virtual register uses are always safe to sink.
1281       if (MO.isUse())
1282         continue;
1283 
1284       // If it's not safe to move defs of the register class, then abort.
1285       if (!TII->isSafeToMoveRegClassDefs(MRI->getRegClass(Reg)))
1286         return nullptr;
1287 
1288       // Virtual register defs can only be sunk if all their uses are in blocks
1289       // dominated by one of the successors.
1290       if (SuccToSinkTo) {
1291         // If a previous operand picked a block to sink to, then this operand
1292         // must be sinkable to the same block.
1293         bool LocalUse = false;
1294         if (!AllUsesDominatedByBlock(Reg, SuccToSinkTo, MBB, BreakPHIEdge,
1295                                      LocalUse))
1296           return nullptr;
1297 
1298         continue;
1299       }
1300 
1301       // Otherwise, we should look at all the successors and decide which one
1302       // we should sink to. If we have reliable block frequency information
1303       // (frequency != 0) available, give successors with smaller frequencies
1304       // higher priority, otherwise prioritize smaller cycle depths.
1305       for (MachineBasicBlock *SuccBlock :
1306            GetAllSortedSuccessors(MI, MBB, AllSuccessors)) {
1307         bool LocalUse = false;
1308         if (AllUsesDominatedByBlock(Reg, SuccBlock, MBB, BreakPHIEdge,
1309                                     LocalUse)) {
1310           SuccToSinkTo = SuccBlock;
1311           break;
1312         }
1313         if (LocalUse)
1314           // Def is used locally, it's never safe to move this def.
1315           return nullptr;
1316       }
1317 
1318       // If we couldn't find a block to sink to, ignore this instruction.
1319       if (!SuccToSinkTo)
1320         return nullptr;
1321       if (!isProfitableToSinkTo(Reg, MI, MBB, SuccToSinkTo, AllSuccessors))
1322         return nullptr;
1323     }
1324   }
1325 
1326   // It is not possible to sink an instruction into its own block.  This can
1327   // happen with cycles.
1328   if (MBB == SuccToSinkTo)
1329     return nullptr;
1330 
1331   // It's not safe to sink instructions to EH landing pad. Control flow into
1332   // landing pad is implicitly defined.
1333   if (SuccToSinkTo && SuccToSinkTo->isEHPad())
1334     return nullptr;
1335 
1336   // It ought to be okay to sink instructions into an INLINEASM_BR target, but
1337   // only if we make sure that MI occurs _before_ an INLINEASM_BR instruction in
1338   // the source block (which this code does not yet do). So for now, forbid
1339   // doing so.
1340   if (SuccToSinkTo && SuccToSinkTo->isInlineAsmBrIndirectTarget())
1341     return nullptr;
1342 
1343   if (SuccToSinkTo && !TII->isSafeToSink(MI, SuccToSinkTo, CI))
1344     return nullptr;
1345 
1346   return SuccToSinkTo;
1347 }
1348 
1349 /// Return true if MI is likely to be usable as a memory operation by the
1350 /// implicit null check optimization.
1351 ///
1352 /// This is a "best effort" heuristic, and should not be relied upon for
1353 /// correctness.  This returning true does not guarantee that the implicit null
1354 /// check optimization is legal over MI, and this returning false does not
1355 /// guarantee MI cannot possibly be used to do a null check.
1356 static bool SinkingPreventsImplicitNullCheck(MachineInstr &MI,
1357                                              const TargetInstrInfo *TII,
1358                                              const TargetRegisterInfo *TRI) {
1359   using MachineBranchPredicate = TargetInstrInfo::MachineBranchPredicate;
1360 
1361   auto *MBB = MI.getParent();
1362   if (MBB->pred_size() != 1)
1363     return false;
1364 
1365   auto *PredMBB = *MBB->pred_begin();
1366   auto *PredBB = PredMBB->getBasicBlock();
1367 
1368   // Frontends that don't use implicit null checks have no reason to emit
1369   // branches with make.implicit metadata, and this function should always
1370   // return false for them.
1371   if (!PredBB ||
1372       !PredBB->getTerminator()->getMetadata(LLVMContext::MD_make_implicit))
1373     return false;
1374 
1375   const MachineOperand *BaseOp;
1376   int64_t Offset;
1377   bool OffsetIsScalable;
1378   if (!TII->getMemOperandWithOffset(MI, BaseOp, Offset, OffsetIsScalable, TRI))
1379     return false;
1380 
1381   if (!BaseOp->isReg())
1382     return false;
1383 
1384   if (!(MI.mayLoad() && !MI.isPredicable()))
1385     return false;
1386 
1387   MachineBranchPredicate MBP;
1388   if (TII->analyzeBranchPredicate(*PredMBB, MBP, false))
1389     return false;
1390 
1391   return MBP.LHS.isReg() && MBP.RHS.isImm() && MBP.RHS.getImm() == 0 &&
1392          (MBP.Predicate == MachineBranchPredicate::PRED_NE ||
1393           MBP.Predicate == MachineBranchPredicate::PRED_EQ) &&
1394          MBP.LHS.getReg() == BaseOp->getReg();
1395 }
1396 
1397 /// If the sunk instruction is a copy, try to forward the copy instead of
1398 /// leaving an 'undef' DBG_VALUE in the original location. Don't do this if
1399 /// there's any subregister weirdness involved. Returns true if copy
1400 /// propagation occurred.
1401 static bool attemptDebugCopyProp(MachineInstr &SinkInst, MachineInstr &DbgMI,
1402                                  Register Reg) {
1403   const MachineRegisterInfo &MRI = SinkInst.getMF()->getRegInfo();
1404   const TargetInstrInfo &TII = *SinkInst.getMF()->getSubtarget().getInstrInfo();
1405 
1406   // Copy DBG_VALUE operand and set the original to undef. We then check to
1407   // see whether this is something that can be copy-forwarded. If it isn't,
1408   // continue around the loop.
1409 
1410   const MachineOperand *SrcMO = nullptr, *DstMO = nullptr;
1411   auto CopyOperands = TII.isCopyInstr(SinkInst);
1412   if (!CopyOperands)
1413     return false;
1414   SrcMO = CopyOperands->Source;
1415   DstMO = CopyOperands->Destination;
1416 
1417   // Check validity of forwarding this copy.
1418   bool PostRA = MRI.getNumVirtRegs() == 0;
1419 
1420   // Trying to forward between physical and virtual registers is too hard.
1421   if (Reg.isVirtual() != SrcMO->getReg().isVirtual())
1422     return false;
1423 
1424   // Only try virtual register copy-forwarding before regalloc, and physical
1425   // register copy-forwarding after regalloc.
1426   bool arePhysRegs = !Reg.isVirtual();
1427   if (arePhysRegs != PostRA)
1428     return false;
1429 
1430   // Pre-regalloc, only forward if all subregisters agree (or there are no
1431   // subregs at all). More analysis might recover some forwardable copies.
1432   if (!PostRA)
1433     for (auto &DbgMO : DbgMI.getDebugOperandsForReg(Reg))
1434       if (DbgMO.getSubReg() != SrcMO->getSubReg() ||
1435           DbgMO.getSubReg() != DstMO->getSubReg())
1436         return false;
1437 
1438   // Post-regalloc, we may be sinking a DBG_VALUE of a sub or super-register
1439   // of this copy. Only forward the copy if the DBG_VALUE operand exactly
1440   // matches the copy destination.
1441   if (PostRA && Reg != DstMO->getReg())
1442     return false;
1443 
1444   for (auto &DbgMO : DbgMI.getDebugOperandsForReg(Reg)) {
1445     DbgMO.setReg(SrcMO->getReg());
1446     DbgMO.setSubReg(SrcMO->getSubReg());
1447   }
1448   return true;
1449 }
1450 
1451 using MIRegs = std::pair<MachineInstr *, SmallVector<unsigned, 2>>;
1452 /// Sink an instruction and its associated debug instructions.
1453 static void performSink(MachineInstr &MI, MachineBasicBlock &SuccToSinkTo,
1454                         MachineBasicBlock::iterator InsertPos,
1455                         ArrayRef<MIRegs> DbgValuesToSink) {
1456   // If we cannot find a location to use (merge with), then we erase the debug
1457   // location to prevent debug-info driven tools from potentially reporting
1458   // wrong location information.
1459   if (!SuccToSinkTo.empty() && InsertPos != SuccToSinkTo.end())
1460     MI.setDebugLoc(DILocation::getMergedLocation(MI.getDebugLoc(),
1461                                                  InsertPos->getDebugLoc()));
1462   else
1463     MI.setDebugLoc(DebugLoc());
1464 
1465   // Move the instruction.
1466   MachineBasicBlock *ParentBlock = MI.getParent();
1467   SuccToSinkTo.splice(InsertPos, ParentBlock, MI,
1468                       ++MachineBasicBlock::iterator(MI));
1469 
1470   // Sink a copy of debug users to the insert position. Mark the original
1471   // DBG_VALUE location as 'undef', indicating that any earlier variable
1472   // location should be terminated as we've optimised away the value at this
1473   // point.
1474   for (const auto &DbgValueToSink : DbgValuesToSink) {
1475     MachineInstr *DbgMI = DbgValueToSink.first;
1476     MachineInstr *NewDbgMI = DbgMI->getMF()->CloneMachineInstr(DbgMI);
1477     SuccToSinkTo.insert(InsertPos, NewDbgMI);
1478 
1479     bool PropagatedAllSunkOps = true;
1480     for (unsigned Reg : DbgValueToSink.second) {
1481       if (DbgMI->hasDebugOperandForReg(Reg)) {
1482         if (!attemptDebugCopyProp(MI, *DbgMI, Reg)) {
1483           PropagatedAllSunkOps = false;
1484           break;
1485         }
1486       }
1487     }
1488     if (!PropagatedAllSunkOps)
1489       DbgMI->setDebugValueUndef();
1490   }
1491 }
1492 
1493 /// hasStoreBetween - check if there is store betweeen straight line blocks From
1494 /// and To.
1495 bool MachineSinking::hasStoreBetween(MachineBasicBlock *From,
1496                                      MachineBasicBlock *To, MachineInstr &MI) {
1497   // Make sure From and To are in straight line which means From dominates To
1498   // and To post dominates From.
1499   if (!DT->dominates(From, To) || !PDT->dominates(To, From))
1500     return true;
1501 
1502   auto BlockPair = std::make_pair(From, To);
1503 
1504   // Does these two blocks pair be queried before and have a definite cached
1505   // result?
1506   if (auto It = HasStoreCache.find(BlockPair); It != HasStoreCache.end())
1507     return It->second;
1508 
1509   if (auto It = StoreInstrCache.find(BlockPair); It != StoreInstrCache.end())
1510     return llvm::any_of(It->second, [&](MachineInstr *I) {
1511       return I->mayAlias(AA, MI, false);
1512     });
1513 
1514   bool SawStore = false;
1515   bool HasAliasedStore = false;
1516   DenseSet<MachineBasicBlock *> HandledBlocks;
1517   DenseSet<MachineBasicBlock *> HandledDomBlocks;
1518   // Go through all reachable blocks from From.
1519   for (MachineBasicBlock *BB : depth_first(From)) {
1520     // We insert the instruction at the start of block To, so no need to worry
1521     // about stores inside To.
1522     // Store in block From should be already considered when just enter function
1523     // SinkInstruction.
1524     if (BB == To || BB == From)
1525       continue;
1526 
1527     // We already handle this BB in previous iteration.
1528     if (HandledBlocks.count(BB))
1529       continue;
1530 
1531     HandledBlocks.insert(BB);
1532     // To post dominates BB, it must be a path from block From.
1533     if (PDT->dominates(To, BB)) {
1534       if (!HandledDomBlocks.count(BB))
1535         HandledDomBlocks.insert(BB);
1536 
1537       // If this BB is too big or the block number in straight line between From
1538       // and To is too big, stop searching to save compiling time.
1539       if (BB->sizeWithoutDebugLargerThan(SinkLoadInstsPerBlockThreshold) ||
1540           HandledDomBlocks.size() > SinkLoadBlocksThreshold) {
1541         for (auto *DomBB : HandledDomBlocks) {
1542           if (DomBB != BB && DT->dominates(DomBB, BB))
1543             HasStoreCache[std::make_pair(DomBB, To)] = true;
1544           else if (DomBB != BB && DT->dominates(BB, DomBB))
1545             HasStoreCache[std::make_pair(From, DomBB)] = true;
1546         }
1547         HasStoreCache[BlockPair] = true;
1548         return true;
1549       }
1550 
1551       for (MachineInstr &I : *BB) {
1552         // Treat as alias conservatively for a call or an ordered memory
1553         // operation.
1554         if (I.isCall() || I.hasOrderedMemoryRef()) {
1555           for (auto *DomBB : HandledDomBlocks) {
1556             if (DomBB != BB && DT->dominates(DomBB, BB))
1557               HasStoreCache[std::make_pair(DomBB, To)] = true;
1558             else if (DomBB != BB && DT->dominates(BB, DomBB))
1559               HasStoreCache[std::make_pair(From, DomBB)] = true;
1560           }
1561           HasStoreCache[BlockPair] = true;
1562           return true;
1563         }
1564 
1565         if (I.mayStore()) {
1566           SawStore = true;
1567           // We still have chance to sink MI if all stores between are not
1568           // aliased to MI.
1569           // Cache all store instructions, so that we don't need to go through
1570           // all From reachable blocks for next load instruction.
1571           if (I.mayAlias(AA, MI, false))
1572             HasAliasedStore = true;
1573           StoreInstrCache[BlockPair].push_back(&I);
1574         }
1575       }
1576     }
1577   }
1578   // If there is no store at all, cache the result.
1579   if (!SawStore)
1580     HasStoreCache[BlockPair] = false;
1581   return HasAliasedStore;
1582 }
1583 
1584 /// Sink instructions into cycles if profitable. This especially tries to
1585 /// prevent register spills caused by register pressure if there is little to no
1586 /// overhead moving instructions into cycles.
1587 bool MachineSinking::SinkIntoCycle(MachineCycle *Cycle, MachineInstr &I) {
1588   LLVM_DEBUG(dbgs() << "CycleSink: Finding sink block for: " << I);
1589   MachineBasicBlock *Preheader = Cycle->getCyclePreheader();
1590   assert(Preheader && "Cycle sink needs a preheader block");
1591   MachineBasicBlock *SinkBlock = nullptr;
1592   bool CanSink = true;
1593   const MachineOperand &MO = I.getOperand(0);
1594 
1595   for (MachineInstr &MI : MRI->use_instructions(MO.getReg())) {
1596     LLVM_DEBUG(dbgs() << "CycleSink:   Analysing use: " << MI);
1597     if (!Cycle->contains(MI.getParent())) {
1598       LLVM_DEBUG(dbgs() << "CycleSink:   Use not in cycle, can't sink.\n");
1599       CanSink = false;
1600       break;
1601     }
1602 
1603     // FIXME: Come up with a proper cost model that estimates whether sinking
1604     // the instruction (and thus possibly executing it on every cycle
1605     // iteration) is more expensive than a register.
1606     // For now assumes that copies are cheap and thus almost always worth it.
1607     if (!MI.isCopy()) {
1608       LLVM_DEBUG(dbgs() << "CycleSink:   Use is not a copy\n");
1609       CanSink = false;
1610       break;
1611     }
1612     if (!SinkBlock) {
1613       SinkBlock = MI.getParent();
1614       LLVM_DEBUG(dbgs() << "CycleSink:   Setting sink block to: "
1615                         << printMBBReference(*SinkBlock) << "\n");
1616       continue;
1617     }
1618     SinkBlock = DT->findNearestCommonDominator(SinkBlock, MI.getParent());
1619     if (!SinkBlock) {
1620       LLVM_DEBUG(dbgs() << "CycleSink:   Can't find nearest dominator\n");
1621       CanSink = false;
1622       break;
1623     }
1624     LLVM_DEBUG(dbgs() << "CycleSink:   Setting nearest common dom block: "
1625                       << printMBBReference(*SinkBlock) << "\n");
1626   }
1627 
1628   if (!CanSink) {
1629     LLVM_DEBUG(dbgs() << "CycleSink: Can't sink instruction.\n");
1630     return false;
1631   }
1632   if (!SinkBlock) {
1633     LLVM_DEBUG(dbgs() << "CycleSink: Not sinking, can't find sink block.\n");
1634     return false;
1635   }
1636   if (SinkBlock == Preheader) {
1637     LLVM_DEBUG(
1638         dbgs() << "CycleSink: Not sinking, sink block is the preheader\n");
1639     return false;
1640   }
1641   if (SinkBlock->sizeWithoutDebugLargerThan(SinkLoadInstsPerBlockThreshold)) {
1642     LLVM_DEBUG(
1643         dbgs() << "CycleSink: Not Sinking, block too large to analyse.\n");
1644     return false;
1645   }
1646 
1647   LLVM_DEBUG(dbgs() << "CycleSink: Sinking instruction!\n");
1648   SinkBlock->splice(SinkBlock->SkipPHIsAndLabels(SinkBlock->begin()), Preheader,
1649                     I);
1650 
1651   // Conservatively clear any kill flags on uses of sunk instruction
1652   for (MachineOperand &MO : I.operands()) {
1653     if (MO.isReg() && MO.readsReg())
1654       RegsToClearKillFlags.insert(MO.getReg());
1655   }
1656 
1657   // The instruction is moved from its basic block, so do not retain the
1658   // debug information.
1659   assert(!I.isDebugInstr() && "Should not sink debug inst");
1660   I.setDebugLoc(DebugLoc());
1661   return true;
1662 }
1663 
1664 /// SinkInstruction - Determine whether it is safe to sink the specified machine
1665 /// instruction out of its current block into a successor.
1666 bool MachineSinking::SinkInstruction(MachineInstr &MI, bool &SawStore,
1667                                      AllSuccsCache &AllSuccessors) {
1668   // Don't sink instructions that the target prefers not to sink.
1669   if (!TII->shouldSink(MI))
1670     return false;
1671 
1672   // Check if it's safe to move the instruction.
1673   if (!MI.isSafeToMove(SawStore))
1674     return false;
1675 
1676   // Convergent operations may not be made control-dependent on additional
1677   // values.
1678   if (MI.isConvergent())
1679     return false;
1680 
1681   // Don't break implicit null checks.  This is a performance heuristic, and not
1682   // required for correctness.
1683   if (SinkingPreventsImplicitNullCheck(MI, TII, TRI))
1684     return false;
1685 
1686   // FIXME: This should include support for sinking instructions within the
1687   // block they are currently in to shorten the live ranges.  We often get
1688   // instructions sunk into the top of a large block, but it would be better to
1689   // also sink them down before their first use in the block.  This xform has to
1690   // be careful not to *increase* register pressure though, e.g. sinking
1691   // "x = y + z" down if it kills y and z would increase the live ranges of y
1692   // and z and only shrink the live range of x.
1693 
1694   bool BreakPHIEdge = false;
1695   MachineBasicBlock *ParentBlock = MI.getParent();
1696   MachineBasicBlock *SuccToSinkTo =
1697       FindSuccToSinkTo(MI, ParentBlock, BreakPHIEdge, AllSuccessors);
1698 
1699   // If there are no outputs, it must have side-effects.
1700   if (!SuccToSinkTo)
1701     return false;
1702 
1703   // If the instruction to move defines a dead physical register which is live
1704   // when leaving the basic block, don't move it because it could turn into a
1705   // "zombie" define of that preg. E.g., EFLAGS.
1706   for (const MachineOperand &MO : MI.all_defs()) {
1707     Register Reg = MO.getReg();
1708     if (Reg == 0 || !Reg.isPhysical())
1709       continue;
1710     if (SuccToSinkTo->isLiveIn(Reg))
1711       return false;
1712   }
1713 
1714   LLVM_DEBUG(dbgs() << "Sink instr " << MI << "\tinto block " << *SuccToSinkTo);
1715 
1716   // If the block has multiple predecessors, this is a critical edge.
1717   // Decide if we can sink along it or need to break the edge.
1718   if (SuccToSinkTo->pred_size() > 1) {
1719     // We cannot sink a load across a critical edge - there may be stores in
1720     // other code paths.
1721     bool TryBreak = false;
1722     bool Store =
1723         MI.mayLoad() ? hasStoreBetween(ParentBlock, SuccToSinkTo, MI) : true;
1724     if (!MI.isSafeToMove(Store)) {
1725       LLVM_DEBUG(dbgs() << " *** NOTE: Won't sink load along critical edge.\n");
1726       TryBreak = true;
1727     }
1728 
1729     // We don't want to sink across a critical edge if we don't dominate the
1730     // successor. We could be introducing calculations to new code paths.
1731     if (!TryBreak && !DT->dominates(ParentBlock, SuccToSinkTo)) {
1732       LLVM_DEBUG(dbgs() << " *** NOTE: Critical edge found\n");
1733       TryBreak = true;
1734     }
1735 
1736     // Don't sink instructions into a cycle.
1737     if (!TryBreak && CI->getCycle(SuccToSinkTo) &&
1738         (!CI->getCycle(SuccToSinkTo)->isReducible() ||
1739          CI->getCycle(SuccToSinkTo)->getHeader() == SuccToSinkTo)) {
1740       LLVM_DEBUG(dbgs() << " *** NOTE: cycle header found\n");
1741       TryBreak = true;
1742     }
1743 
1744     // Otherwise we are OK with sinking along a critical edge.
1745     if (!TryBreak)
1746       LLVM_DEBUG(dbgs() << "Sinking along critical edge.\n");
1747     else {
1748       // Mark this edge as to be split.
1749       // If the edge can actually be split, the next iteration of the main loop
1750       // will sink MI in the newly created block.
1751       bool Status = PostponeSplitCriticalEdge(MI, ParentBlock, SuccToSinkTo,
1752                                               BreakPHIEdge);
1753       if (!Status)
1754         LLVM_DEBUG(dbgs() << " *** PUNTING: Not legal or profitable to "
1755                              "break critical edge\n");
1756       // The instruction will not be sunk this time.
1757       return false;
1758     }
1759   }
1760 
1761   if (BreakPHIEdge) {
1762     // BreakPHIEdge is true if all the uses are in the successor MBB being
1763     // sunken into and they are all PHI nodes. In this case, machine-sink must
1764     // break the critical edge first.
1765     bool Status =
1766         PostponeSplitCriticalEdge(MI, ParentBlock, SuccToSinkTo, BreakPHIEdge);
1767     if (!Status)
1768       LLVM_DEBUG(dbgs() << " *** PUNTING: Not legal or profitable to "
1769                            "break critical edge\n");
1770     // The instruction will not be sunk this time.
1771     return false;
1772   }
1773 
1774   // Determine where to insert into. Skip phi nodes.
1775   MachineBasicBlock::iterator InsertPos =
1776       SuccToSinkTo->SkipPHIsAndLabels(SuccToSinkTo->begin());
1777   if (blockPrologueInterferes(SuccToSinkTo, InsertPos, MI, TRI, TII, MRI)) {
1778     LLVM_DEBUG(dbgs() << " *** Not sinking: prologue interference\n");
1779     return false;
1780   }
1781 
1782   // Collect debug users of any vreg that this inst defines.
1783   SmallVector<MIRegs, 4> DbgUsersToSink;
1784   for (auto &MO : MI.all_defs()) {
1785     if (!MO.getReg().isVirtual())
1786       continue;
1787     if (!SeenDbgUsers.count(MO.getReg()))
1788       continue;
1789 
1790     // Sink any users that don't pass any other DBG_VALUEs for this variable.
1791     auto &Users = SeenDbgUsers[MO.getReg()];
1792     for (auto &User : Users) {
1793       MachineInstr *DbgMI = User.getPointer();
1794       if (User.getInt()) {
1795         // This DBG_VALUE would re-order assignments. If we can't copy-propagate
1796         // it, it can't be recovered. Set it undef.
1797         if (!attemptDebugCopyProp(MI, *DbgMI, MO.getReg()))
1798           DbgMI->setDebugValueUndef();
1799       } else {
1800         DbgUsersToSink.push_back(
1801             {DbgMI, SmallVector<unsigned, 2>(1, MO.getReg())});
1802       }
1803     }
1804   }
1805 
1806   // After sinking, some debug users may not be dominated any more. If possible,
1807   // copy-propagate their operands. As it's expensive, don't do this if there's
1808   // no debuginfo in the program.
1809   if (MI.getMF()->getFunction().getSubprogram() && MI.isCopy())
1810     SalvageUnsunkDebugUsersOfCopy(MI, SuccToSinkTo);
1811 
1812   performSink(MI, *SuccToSinkTo, InsertPos, DbgUsersToSink);
1813 
1814   // Conservatively, clear any kill flags, since it's possible that they are no
1815   // longer correct.
1816   // Note that we have to clear the kill flags for any register this instruction
1817   // uses as we may sink over another instruction which currently kills the
1818   // used registers.
1819   for (MachineOperand &MO : MI.all_uses())
1820     RegsToClearKillFlags.insert(MO.getReg()); // Remember to clear kill flags.
1821 
1822   return true;
1823 }
1824 
1825 void MachineSinking::SalvageUnsunkDebugUsersOfCopy(
1826     MachineInstr &MI, MachineBasicBlock *TargetBlock) {
1827   assert(MI.isCopy());
1828   assert(MI.getOperand(1).isReg());
1829 
1830   // Enumerate all users of vreg operands that are def'd. Skip those that will
1831   // be sunk. For the rest, if they are not dominated by the block we will sink
1832   // MI into, propagate the copy source to them.
1833   SmallVector<MachineInstr *, 4> DbgDefUsers;
1834   SmallVector<Register, 4> DbgUseRegs;
1835   const MachineRegisterInfo &MRI = MI.getMF()->getRegInfo();
1836   for (auto &MO : MI.all_defs()) {
1837     if (!MO.getReg().isVirtual())
1838       continue;
1839     DbgUseRegs.push_back(MO.getReg());
1840     for (auto &User : MRI.use_instructions(MO.getReg())) {
1841       if (!User.isDebugValue() || DT->dominates(TargetBlock, User.getParent()))
1842         continue;
1843 
1844       // If is in same block, will either sink or be use-before-def.
1845       if (User.getParent() == MI.getParent())
1846         continue;
1847 
1848       assert(User.hasDebugOperandForReg(MO.getReg()) &&
1849              "DBG_VALUE user of vreg, but has no operand for it?");
1850       DbgDefUsers.push_back(&User);
1851     }
1852   }
1853 
1854   // Point the users of this copy that are no longer dominated, at the source
1855   // of the copy.
1856   for (auto *User : DbgDefUsers) {
1857     for (auto &Reg : DbgUseRegs) {
1858       for (auto &DbgOp : User->getDebugOperandsForReg(Reg)) {
1859         DbgOp.setReg(MI.getOperand(1).getReg());
1860         DbgOp.setSubReg(MI.getOperand(1).getSubReg());
1861       }
1862     }
1863   }
1864 }
1865 
1866 //===----------------------------------------------------------------------===//
1867 // This pass is not intended to be a replacement or a complete alternative
1868 // for the pre-ra machine sink pass. It is only designed to sink COPY
1869 // instructions which should be handled after RA.
1870 //
1871 // This pass sinks COPY instructions into a successor block, if the COPY is not
1872 // used in the current block and the COPY is live-in to a single successor
1873 // (i.e., doesn't require the COPY to be duplicated).  This avoids executing the
1874 // copy on paths where their results aren't needed.  This also exposes
1875 // additional opportunites for dead copy elimination and shrink wrapping.
1876 //
1877 // These copies were either not handled by or are inserted after the MachineSink
1878 // pass. As an example of the former case, the MachineSink pass cannot sink
1879 // COPY instructions with allocatable source registers; for AArch64 these type
1880 // of copy instructions are frequently used to move function parameters (PhyReg)
1881 // into virtual registers in the entry block.
1882 //
1883 // For the machine IR below, this pass will sink %w19 in the entry into its
1884 // successor (%bb.1) because %w19 is only live-in in %bb.1.
1885 // %bb.0:
1886 //   %wzr = SUBSWri %w1, 1
1887 //   %w19 = COPY %w0
1888 //   Bcc 11, %bb.2
1889 // %bb.1:
1890 //   Live Ins: %w19
1891 //   BL @fun
1892 //   %w0 = ADDWrr %w0, %w19
1893 //   RET %w0
1894 // %bb.2:
1895 //   %w0 = COPY %wzr
1896 //   RET %w0
1897 // As we sink %w19 (CSR in AArch64) into %bb.1, the shrink-wrapping pass will be
1898 // able to see %bb.0 as a candidate.
1899 //===----------------------------------------------------------------------===//
1900 namespace {
1901 
1902 class PostRAMachineSinking : public MachineFunctionPass {
1903 public:
1904   bool runOnMachineFunction(MachineFunction &MF) override;
1905 
1906   static char ID;
1907   PostRAMachineSinking() : MachineFunctionPass(ID) {}
1908   StringRef getPassName() const override { return "PostRA Machine Sink"; }
1909 
1910   void getAnalysisUsage(AnalysisUsage &AU) const override {
1911     AU.setPreservesCFG();
1912     MachineFunctionPass::getAnalysisUsage(AU);
1913   }
1914 
1915   MachineFunctionProperties getRequiredProperties() const override {
1916     return MachineFunctionProperties().set(
1917         MachineFunctionProperties::Property::NoVRegs);
1918   }
1919 
1920 private:
1921   /// Track which register units have been modified and used.
1922   LiveRegUnits ModifiedRegUnits, UsedRegUnits;
1923 
1924   /// Track DBG_VALUEs of (unmodified) register units. Each DBG_VALUE has an
1925   /// entry in this map for each unit it touches. The DBG_VALUE's entry
1926   /// consists of a pointer to the instruction itself, and a vector of registers
1927   /// referred to by the instruction that overlap the key register unit.
1928   DenseMap<unsigned, SmallVector<MIRegs, 2>> SeenDbgInstrs;
1929 
1930   /// Sink Copy instructions unused in the same block close to their uses in
1931   /// successors.
1932   bool tryToSinkCopy(MachineBasicBlock &BB, MachineFunction &MF,
1933                      const TargetRegisterInfo *TRI, const TargetInstrInfo *TII);
1934 };
1935 } // namespace
1936 
1937 char PostRAMachineSinking::ID = 0;
1938 char &llvm::PostRAMachineSinkingID = PostRAMachineSinking::ID;
1939 
1940 INITIALIZE_PASS(PostRAMachineSinking, "postra-machine-sink",
1941                 "PostRA Machine Sink", false, false)
1942 
1943 static bool aliasWithRegsInLiveIn(MachineBasicBlock &MBB, unsigned Reg,
1944                                   const TargetRegisterInfo *TRI) {
1945   LiveRegUnits LiveInRegUnits(*TRI);
1946   LiveInRegUnits.addLiveIns(MBB);
1947   return !LiveInRegUnits.available(Reg);
1948 }
1949 
1950 static MachineBasicBlock *
1951 getSingleLiveInSuccBB(MachineBasicBlock &CurBB,
1952                       const SmallPtrSetImpl<MachineBasicBlock *> &SinkableBBs,
1953                       unsigned Reg, const TargetRegisterInfo *TRI) {
1954   // Try to find a single sinkable successor in which Reg is live-in.
1955   MachineBasicBlock *BB = nullptr;
1956   for (auto *SI : SinkableBBs) {
1957     if (aliasWithRegsInLiveIn(*SI, Reg, TRI)) {
1958       // If BB is set here, Reg is live-in to at least two sinkable successors,
1959       // so quit.
1960       if (BB)
1961         return nullptr;
1962       BB = SI;
1963     }
1964   }
1965   // Reg is not live-in to any sinkable successors.
1966   if (!BB)
1967     return nullptr;
1968 
1969   // Check if any register aliased with Reg is live-in in other successors.
1970   for (auto *SI : CurBB.successors()) {
1971     if (!SinkableBBs.count(SI) && aliasWithRegsInLiveIn(*SI, Reg, TRI))
1972       return nullptr;
1973   }
1974   return BB;
1975 }
1976 
1977 static MachineBasicBlock *
1978 getSingleLiveInSuccBB(MachineBasicBlock &CurBB,
1979                       const SmallPtrSetImpl<MachineBasicBlock *> &SinkableBBs,
1980                       ArrayRef<unsigned> DefedRegsInCopy,
1981                       const TargetRegisterInfo *TRI) {
1982   MachineBasicBlock *SingleBB = nullptr;
1983   for (auto DefReg : DefedRegsInCopy) {
1984     MachineBasicBlock *BB =
1985         getSingleLiveInSuccBB(CurBB, SinkableBBs, DefReg, TRI);
1986     if (!BB || (SingleBB && SingleBB != BB))
1987       return nullptr;
1988     SingleBB = BB;
1989   }
1990   return SingleBB;
1991 }
1992 
1993 static void clearKillFlags(MachineInstr *MI, MachineBasicBlock &CurBB,
1994                            SmallVectorImpl<unsigned> &UsedOpsInCopy,
1995                            LiveRegUnits &UsedRegUnits,
1996                            const TargetRegisterInfo *TRI) {
1997   for (auto U : UsedOpsInCopy) {
1998     MachineOperand &MO = MI->getOperand(U);
1999     Register SrcReg = MO.getReg();
2000     if (!UsedRegUnits.available(SrcReg)) {
2001       MachineBasicBlock::iterator NI = std::next(MI->getIterator());
2002       for (MachineInstr &UI : make_range(NI, CurBB.end())) {
2003         if (UI.killsRegister(SrcReg, TRI)) {
2004           UI.clearRegisterKills(SrcReg, TRI);
2005           MO.setIsKill(true);
2006           break;
2007         }
2008       }
2009     }
2010   }
2011 }
2012 
2013 static void updateLiveIn(MachineInstr *MI, MachineBasicBlock *SuccBB,
2014                          SmallVectorImpl<unsigned> &UsedOpsInCopy,
2015                          SmallVectorImpl<unsigned> &DefedRegsInCopy) {
2016   MachineFunction &MF = *SuccBB->getParent();
2017   const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
2018   for (unsigned DefReg : DefedRegsInCopy)
2019     for (MCPhysReg S : TRI->subregs_inclusive(DefReg))
2020       SuccBB->removeLiveIn(S);
2021   for (auto U : UsedOpsInCopy)
2022     SuccBB->addLiveIn(MI->getOperand(U).getReg());
2023   SuccBB->sortUniqueLiveIns();
2024 }
2025 
2026 static bool hasRegisterDependency(MachineInstr *MI,
2027                                   SmallVectorImpl<unsigned> &UsedOpsInCopy,
2028                                   SmallVectorImpl<unsigned> &DefedRegsInCopy,
2029                                   LiveRegUnits &ModifiedRegUnits,
2030                                   LiveRegUnits &UsedRegUnits) {
2031   bool HasRegDependency = false;
2032   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
2033     MachineOperand &MO = MI->getOperand(i);
2034     if (!MO.isReg())
2035       continue;
2036     Register Reg = MO.getReg();
2037     if (!Reg)
2038       continue;
2039     if (MO.isDef()) {
2040       if (!ModifiedRegUnits.available(Reg) || !UsedRegUnits.available(Reg)) {
2041         HasRegDependency = true;
2042         break;
2043       }
2044       DefedRegsInCopy.push_back(Reg);
2045 
2046       // FIXME: instead of isUse(), readsReg() would be a better fix here,
2047       // For example, we can ignore modifications in reg with undef. However,
2048       // it's not perfectly clear if skipping the internal read is safe in all
2049       // other targets.
2050     } else if (MO.isUse()) {
2051       if (!ModifiedRegUnits.available(Reg)) {
2052         HasRegDependency = true;
2053         break;
2054       }
2055       UsedOpsInCopy.push_back(i);
2056     }
2057   }
2058   return HasRegDependency;
2059 }
2060 
2061 bool PostRAMachineSinking::tryToSinkCopy(MachineBasicBlock &CurBB,
2062                                          MachineFunction &MF,
2063                                          const TargetRegisterInfo *TRI,
2064                                          const TargetInstrInfo *TII) {
2065   SmallPtrSet<MachineBasicBlock *, 2> SinkableBBs;
2066   // FIXME: For now, we sink only to a successor which has a single predecessor
2067   // so that we can directly sink COPY instructions to the successor without
2068   // adding any new block or branch instruction.
2069   for (MachineBasicBlock *SI : CurBB.successors())
2070     if (!SI->livein_empty() && SI->pred_size() == 1)
2071       SinkableBBs.insert(SI);
2072 
2073   if (SinkableBBs.empty())
2074     return false;
2075 
2076   bool Changed = false;
2077 
2078   // Track which registers have been modified and used between the end of the
2079   // block and the current instruction.
2080   ModifiedRegUnits.clear();
2081   UsedRegUnits.clear();
2082   SeenDbgInstrs.clear();
2083 
2084   for (MachineInstr &MI : llvm::make_early_inc_range(llvm::reverse(CurBB))) {
2085     // Track the operand index for use in Copy.
2086     SmallVector<unsigned, 2> UsedOpsInCopy;
2087     // Track the register number defed in Copy.
2088     SmallVector<unsigned, 2> DefedRegsInCopy;
2089 
2090     // We must sink this DBG_VALUE if its operand is sunk. To avoid searching
2091     // for DBG_VALUEs later, record them when they're encountered.
2092     if (MI.isDebugValue() && !MI.isDebugRef()) {
2093       SmallDenseMap<MCRegister, SmallVector<unsigned, 2>, 4> MIUnits;
2094       bool IsValid = true;
2095       for (MachineOperand &MO : MI.debug_operands()) {
2096         if (MO.isReg() && MO.getReg().isPhysical()) {
2097           // Bail if we can already tell the sink would be rejected, rather
2098           // than needlessly accumulating lots of DBG_VALUEs.
2099           if (hasRegisterDependency(&MI, UsedOpsInCopy, DefedRegsInCopy,
2100                                     ModifiedRegUnits, UsedRegUnits)) {
2101             IsValid = false;
2102             break;
2103           }
2104 
2105           // Record debug use of each reg unit.
2106           for (MCRegUnit Unit : TRI->regunits(MO.getReg()))
2107             MIUnits[Unit].push_back(MO.getReg());
2108         }
2109       }
2110       if (IsValid) {
2111         for (auto &RegOps : MIUnits)
2112           SeenDbgInstrs[RegOps.first].emplace_back(&MI,
2113                                                    std::move(RegOps.second));
2114       }
2115       continue;
2116     }
2117 
2118     if (MI.isDebugOrPseudoInstr())
2119       continue;
2120 
2121     // Do not move any instruction across function call.
2122     if (MI.isCall())
2123       return false;
2124 
2125     if (!MI.isCopy() || !MI.getOperand(0).isRenamable()) {
2126       LiveRegUnits::accumulateUsedDefed(MI, ModifiedRegUnits, UsedRegUnits,
2127                                         TRI);
2128       continue;
2129     }
2130 
2131     // Don't sink the COPY if it would violate a register dependency.
2132     if (hasRegisterDependency(&MI, UsedOpsInCopy, DefedRegsInCopy,
2133                               ModifiedRegUnits, UsedRegUnits)) {
2134       LiveRegUnits::accumulateUsedDefed(MI, ModifiedRegUnits, UsedRegUnits,
2135                                         TRI);
2136       continue;
2137     }
2138     assert((!UsedOpsInCopy.empty() && !DefedRegsInCopy.empty()) &&
2139            "Unexpect SrcReg or DefReg");
2140     MachineBasicBlock *SuccBB =
2141         getSingleLiveInSuccBB(CurBB, SinkableBBs, DefedRegsInCopy, TRI);
2142     // Don't sink if we cannot find a single sinkable successor in which Reg
2143     // is live-in.
2144     if (!SuccBB) {
2145       LiveRegUnits::accumulateUsedDefed(MI, ModifiedRegUnits, UsedRegUnits,
2146                                         TRI);
2147       continue;
2148     }
2149     assert((SuccBB->pred_size() == 1 && *SuccBB->pred_begin() == &CurBB) &&
2150            "Unexpected predecessor");
2151 
2152     // Collect DBG_VALUEs that must sink with this copy. We've previously
2153     // recorded which reg units that DBG_VALUEs read, if this instruction
2154     // writes any of those units then the corresponding DBG_VALUEs must sink.
2155     MapVector<MachineInstr *, MIRegs::second_type> DbgValsToSinkMap;
2156     for (auto &MO : MI.all_defs()) {
2157       for (MCRegUnit Unit : TRI->regunits(MO.getReg())) {
2158         for (const auto &MIRegs : SeenDbgInstrs.lookup(Unit)) {
2159           auto &Regs = DbgValsToSinkMap[MIRegs.first];
2160           for (unsigned Reg : MIRegs.second)
2161             Regs.push_back(Reg);
2162         }
2163       }
2164     }
2165     auto DbgValsToSink = DbgValsToSinkMap.takeVector();
2166 
2167     LLVM_DEBUG(dbgs() << "Sink instr " << MI << "\tinto block " << *SuccBB);
2168 
2169     MachineBasicBlock::iterator InsertPos =
2170         SuccBB->SkipPHIsAndLabels(SuccBB->begin());
2171     if (blockPrologueInterferes(SuccBB, InsertPos, MI, TRI, TII, nullptr)) {
2172       LiveRegUnits::accumulateUsedDefed(MI, ModifiedRegUnits, UsedRegUnits,
2173                                         TRI);
2174       LLVM_DEBUG(dbgs() << " *** Not sinking: prologue interference\n");
2175       continue;
2176     }
2177 
2178     // Clear the kill flag if SrcReg is killed between MI and the end of the
2179     // block.
2180     clearKillFlags(&MI, CurBB, UsedOpsInCopy, UsedRegUnits, TRI);
2181     performSink(MI, *SuccBB, InsertPos, DbgValsToSink);
2182     updateLiveIn(&MI, SuccBB, UsedOpsInCopy, DefedRegsInCopy);
2183 
2184     Changed = true;
2185     ++NumPostRACopySink;
2186   }
2187   return Changed;
2188 }
2189 
2190 bool PostRAMachineSinking::runOnMachineFunction(MachineFunction &MF) {
2191   if (skipFunction(MF.getFunction()))
2192     return false;
2193 
2194   bool Changed = false;
2195   const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
2196   const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
2197 
2198   ModifiedRegUnits.init(*TRI);
2199   UsedRegUnits.init(*TRI);
2200   for (auto &BB : MF)
2201     Changed |= tryToSinkCopy(BB, MF, TRI, TII);
2202 
2203   return Changed;
2204 }
2205