xref: /llvm-project/llvm/lib/CodeGen/MachineSink.cpp (revision 44c4ba34d001dcf538d7396007b5611d6f697f86)
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/PointerIntPair.h"
20 #include "llvm/ADT/SetVector.h"
21 #include "llvm/ADT/SmallSet.h"
22 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/ADT/SparseBitVector.h"
24 #include "llvm/ADT/Statistic.h"
25 #include "llvm/Analysis/AliasAnalysis.h"
26 #include "llvm/CodeGen/MachineBasicBlock.h"
27 #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
28 #include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
29 #include "llvm/CodeGen/MachineDominators.h"
30 #include "llvm/CodeGen/MachineFunction.h"
31 #include "llvm/CodeGen/MachineFunctionPass.h"
32 #include "llvm/CodeGen/MachineInstr.h"
33 #include "llvm/CodeGen/MachineLoopInfo.h"
34 #include "llvm/CodeGen/MachineOperand.h"
35 #include "llvm/CodeGen/MachinePostDominators.h"
36 #include "llvm/CodeGen/MachineRegisterInfo.h"
37 #include "llvm/CodeGen/TargetInstrInfo.h"
38 #include "llvm/CodeGen/TargetRegisterInfo.h"
39 #include "llvm/CodeGen/TargetSubtargetInfo.h"
40 #include "llvm/IR/BasicBlock.h"
41 #include "llvm/IR/DebugInfoMetadata.h"
42 #include "llvm/IR/LLVMContext.h"
43 #include "llvm/InitializePasses.h"
44 #include "llvm/MC/MCRegisterInfo.h"
45 #include "llvm/Pass.h"
46 #include "llvm/Support/BranchProbability.h"
47 #include "llvm/Support/CommandLine.h"
48 #include "llvm/Support/Debug.h"
49 #include "llvm/Support/raw_ostream.h"
50 #include <algorithm>
51 #include <cassert>
52 #include <cstdint>
53 #include <map>
54 #include <utility>
55 #include <vector>
56 
57 using namespace llvm;
58 
59 #define DEBUG_TYPE "machine-sink"
60 
61 static cl::opt<bool>
62 SplitEdges("machine-sink-split",
63            cl::desc("Split critical edges during machine sinking"),
64            cl::init(true), cl::Hidden);
65 
66 static cl::opt<bool>
67 UseBlockFreqInfo("machine-sink-bfi",
68            cl::desc("Use block frequency info to find successors to sink"),
69            cl::init(true), cl::Hidden);
70 
71 static cl::opt<unsigned> SplitEdgeProbabilityThreshold(
72     "machine-sink-split-probability-threshold",
73     cl::desc(
74         "Percentage threshold for splitting single-instruction critical edge. "
75         "If the branch threshold is higher than this threshold, we allow "
76         "speculative execution of up to 1 instruction to avoid branching to "
77         "splitted critical edge"),
78     cl::init(40), cl::Hidden);
79 
80 STATISTIC(NumSunk,      "Number of machine instructions sunk");
81 STATISTIC(NumSplit,     "Number of critical edges split");
82 STATISTIC(NumCoalesces, "Number of copies coalesced");
83 STATISTIC(NumPostRACopySink, "Number of copies sunk after RA");
84 
85 namespace {
86 
87   class MachineSinking : public MachineFunctionPass {
88     const TargetInstrInfo *TII;
89     const TargetRegisterInfo *TRI;
90     MachineRegisterInfo  *MRI;     // Machine register information
91     MachineDominatorTree *DT;      // Machine dominator tree
92     MachinePostDominatorTree *PDT; // Machine post dominator tree
93     MachineLoopInfo *LI;
94     MachineBlockFrequencyInfo *MBFI;
95     const MachineBranchProbabilityInfo *MBPI;
96     AliasAnalysis *AA;
97 
98     // Remember which edges have been considered for breaking.
99     SmallSet<std::pair<MachineBasicBlock*, MachineBasicBlock*>, 8>
100     CEBCandidates;
101     // Remember which edges we are about to split.
102     // This is different from CEBCandidates since those edges
103     // will be split.
104     SetVector<std::pair<MachineBasicBlock *, MachineBasicBlock *>> ToSplit;
105 
106     SparseBitVector<> RegsToClearKillFlags;
107 
108     using AllSuccsCache =
109         std::map<MachineBasicBlock *, SmallVector<MachineBasicBlock *, 4>>;
110 
111     /// DBG_VALUE pointer and flag. The flag is true if this DBG_VALUE is
112     /// post-dominated by another DBG_VALUE of the same variable location.
113     /// This is necessary to detect sequences such as:
114     ///     %0 = someinst
115     ///     DBG_VALUE %0, !123, !DIExpression()
116     ///     %1 = anotherinst
117     ///     DBG_VALUE %1, !123, !DIExpression()
118     /// Where if %0 were to sink, the DBG_VAUE should not sink with it, as that
119     /// would re-order assignments.
120     using SeenDbgUser = PointerIntPair<MachineInstr *, 1>;
121 
122     /// Record of DBG_VALUE uses of vregs in a block, so that we can identify
123     /// debug instructions to sink.
124     SmallDenseMap<unsigned, TinyPtrVector<SeenDbgUser>> SeenDbgUsers;
125 
126     /// Record of debug variables that have had their locations set in the
127     /// current block.
128     DenseSet<DebugVariable> SeenDbgVars;
129 
130   public:
131     static char ID; // Pass identification
132 
133     MachineSinking() : MachineFunctionPass(ID) {
134       initializeMachineSinkingPass(*PassRegistry::getPassRegistry());
135     }
136 
137     bool runOnMachineFunction(MachineFunction &MF) override;
138 
139     void getAnalysisUsage(AnalysisUsage &AU) const override {
140       MachineFunctionPass::getAnalysisUsage(AU);
141       AU.addRequired<AAResultsWrapperPass>();
142       AU.addRequired<MachineDominatorTree>();
143       AU.addRequired<MachinePostDominatorTree>();
144       AU.addRequired<MachineLoopInfo>();
145       AU.addRequired<MachineBranchProbabilityInfo>();
146       AU.addPreserved<MachineLoopInfo>();
147       if (UseBlockFreqInfo)
148         AU.addRequired<MachineBlockFrequencyInfo>();
149     }
150 
151     void releaseMemory() override {
152       CEBCandidates.clear();
153     }
154 
155   private:
156     bool ProcessBlock(MachineBasicBlock &MBB);
157     void ProcessDbgInst(MachineInstr &MI);
158     bool isWorthBreakingCriticalEdge(MachineInstr &MI,
159                                      MachineBasicBlock *From,
160                                      MachineBasicBlock *To);
161 
162     /// Postpone the splitting of the given critical
163     /// edge (\p From, \p To).
164     ///
165     /// We do not split the edges on the fly. Indeed, this invalidates
166     /// the dominance information and thus triggers a lot of updates
167     /// of that information underneath.
168     /// Instead, we postpone all the splits after each iteration of
169     /// the main loop. That way, the information is at least valid
170     /// for the lifetime of an iteration.
171     ///
172     /// \return True if the edge is marked as toSplit, false otherwise.
173     /// False can be returned if, for instance, this is not profitable.
174     bool PostponeSplitCriticalEdge(MachineInstr &MI,
175                                    MachineBasicBlock *From,
176                                    MachineBasicBlock *To,
177                                    bool BreakPHIEdge);
178     bool SinkInstruction(MachineInstr &MI, bool &SawStore,
179                          AllSuccsCache &AllSuccessors);
180 
181     /// If we sink a COPY inst, some debug users of it's destination may no
182     /// longer be dominated by the COPY, and will eventually be dropped.
183     /// This is easily rectified by forwarding the non-dominated debug uses
184     /// to the copy source.
185     void SalvageUnsunkDebugUsersOfCopy(MachineInstr &,
186                                        MachineBasicBlock *TargetBlock);
187     bool AllUsesDominatedByBlock(unsigned Reg, MachineBasicBlock *MBB,
188                                  MachineBasicBlock *DefMBB,
189                                  bool &BreakPHIEdge, bool &LocalUse) const;
190     MachineBasicBlock *FindSuccToSinkTo(MachineInstr &MI, MachineBasicBlock *MBB,
191                bool &BreakPHIEdge, AllSuccsCache &AllSuccessors);
192     bool isProfitableToSinkTo(unsigned Reg, MachineInstr &MI,
193                               MachineBasicBlock *MBB,
194                               MachineBasicBlock *SuccToSinkTo,
195                               AllSuccsCache &AllSuccessors);
196 
197     bool PerformTrivialForwardCoalescing(MachineInstr &MI,
198                                          MachineBasicBlock *MBB);
199 
200     SmallVector<MachineBasicBlock *, 4> &
201     GetAllSortedSuccessors(MachineInstr &MI, MachineBasicBlock *MBB,
202                            AllSuccsCache &AllSuccessors) const;
203   };
204 
205 } // end anonymous namespace
206 
207 char MachineSinking::ID = 0;
208 
209 char &llvm::MachineSinkingID = MachineSinking::ID;
210 
211 INITIALIZE_PASS_BEGIN(MachineSinking, DEBUG_TYPE,
212                       "Machine code sinking", false, false)
213 INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)
214 INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
215 INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
216 INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
217 INITIALIZE_PASS_END(MachineSinking, DEBUG_TYPE,
218                     "Machine code sinking", false, false)
219 
220 bool MachineSinking::PerformTrivialForwardCoalescing(MachineInstr &MI,
221                                                      MachineBasicBlock *MBB) {
222   if (!MI.isCopy())
223     return false;
224 
225   Register SrcReg = MI.getOperand(1).getReg();
226   Register DstReg = MI.getOperand(0).getReg();
227   if (!Register::isVirtualRegister(SrcReg) ||
228       !Register::isVirtualRegister(DstReg) || !MRI->hasOneNonDBGUse(SrcReg))
229     return false;
230 
231   const TargetRegisterClass *SRC = MRI->getRegClass(SrcReg);
232   const TargetRegisterClass *DRC = MRI->getRegClass(DstReg);
233   if (SRC != DRC)
234     return false;
235 
236   MachineInstr *DefMI = MRI->getVRegDef(SrcReg);
237   if (DefMI->isCopyLike())
238     return false;
239   LLVM_DEBUG(dbgs() << "Coalescing: " << *DefMI);
240   LLVM_DEBUG(dbgs() << "*** to: " << MI);
241   MRI->replaceRegWith(DstReg, SrcReg);
242   MI.eraseFromParent();
243 
244   // Conservatively, clear any kill flags, since it's possible that they are no
245   // longer correct.
246   MRI->clearKillFlags(SrcReg);
247 
248   ++NumCoalesces;
249   return true;
250 }
251 
252 /// AllUsesDominatedByBlock - Return true if all uses of the specified register
253 /// occur in blocks dominated by the specified block. If any use is in the
254 /// definition block, then return false since it is never legal to move def
255 /// after uses.
256 bool
257 MachineSinking::AllUsesDominatedByBlock(unsigned Reg,
258                                         MachineBasicBlock *MBB,
259                                         MachineBasicBlock *DefMBB,
260                                         bool &BreakPHIEdge,
261                                         bool &LocalUse) const {
262   assert(Register::isVirtualRegister(Reg) && "Only makes sense for vregs");
263 
264   // Ignore debug uses because debug info doesn't affect the code.
265   if (MRI->use_nodbg_empty(Reg))
266     return true;
267 
268   // BreakPHIEdge is true if all the uses are in the successor MBB being sunken
269   // into and they are all PHI nodes. In this case, machine-sink must break
270   // the critical edge first. e.g.
271   //
272   // %bb.1:
273   //   Predecessors according to CFG: %bb.0
274   //     ...
275   //     %def = DEC64_32r %x, implicit-def dead %eflags
276   //     ...
277   //     JE_4 <%bb.37>, implicit %eflags
278   //   Successors according to CFG: %bb.37 %bb.2
279   //
280   // %bb.2:
281   //     %p = PHI %y, %bb.0, %def, %bb.1
282   if (llvm::all_of(MRI->use_nodbg_operands(Reg), [&](MachineOperand &MO) {
283         MachineInstr *UseInst = MO.getParent();
284         unsigned OpNo = UseInst->getOperandNo(&MO);
285         MachineBasicBlock *UseBlock = UseInst->getParent();
286         return UseBlock == MBB && UseInst->isPHI() &&
287                UseInst->getOperand(OpNo + 1).getMBB() == DefMBB;
288       })) {
289     BreakPHIEdge = true;
290     return true;
291   }
292 
293   for (MachineOperand &MO : MRI->use_nodbg_operands(Reg)) {
294     // Determine the block of the use.
295     MachineInstr *UseInst = MO.getParent();
296     unsigned OpNo = &MO - &UseInst->getOperand(0);
297     MachineBasicBlock *UseBlock = UseInst->getParent();
298     if (UseInst->isPHI()) {
299       // PHI nodes use the operand in the predecessor block, not the block with
300       // the PHI.
301       UseBlock = UseInst->getOperand(OpNo+1).getMBB();
302     } else if (UseBlock == DefMBB) {
303       LocalUse = true;
304       return false;
305     }
306 
307     // Check that it dominates.
308     if (!DT->dominates(MBB, UseBlock))
309       return false;
310   }
311 
312   return true;
313 }
314 
315 bool MachineSinking::runOnMachineFunction(MachineFunction &MF) {
316   if (skipFunction(MF.getFunction()))
317     return false;
318 
319   LLVM_DEBUG(dbgs() << "******** Machine Sinking ********\n");
320 
321   TII = MF.getSubtarget().getInstrInfo();
322   TRI = MF.getSubtarget().getRegisterInfo();
323   MRI = &MF.getRegInfo();
324   DT = &getAnalysis<MachineDominatorTree>();
325   PDT = &getAnalysis<MachinePostDominatorTree>();
326   LI = &getAnalysis<MachineLoopInfo>();
327   MBFI = UseBlockFreqInfo ? &getAnalysis<MachineBlockFrequencyInfo>() : nullptr;
328   MBPI = &getAnalysis<MachineBranchProbabilityInfo>();
329   AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
330 
331   bool EverMadeChange = false;
332 
333   while (true) {
334     bool MadeChange = false;
335 
336     // Process all basic blocks.
337     CEBCandidates.clear();
338     ToSplit.clear();
339     for (auto &MBB: MF)
340       MadeChange |= ProcessBlock(MBB);
341 
342     // If we have anything we marked as toSplit, split it now.
343     for (auto &Pair : ToSplit) {
344       auto NewSucc = Pair.first->SplitCriticalEdge(Pair.second, *this);
345       if (NewSucc != nullptr) {
346         LLVM_DEBUG(dbgs() << " *** Splitting critical edge: "
347                           << printMBBReference(*Pair.first) << " -- "
348                           << printMBBReference(*NewSucc) << " -- "
349                           << printMBBReference(*Pair.second) << '\n');
350         if (MBFI) {
351           auto NewSuccFreq = MBFI->getBlockFreq(Pair.first) *
352                              MBPI->getEdgeProbability(Pair.first, NewSucc);
353           MBFI->setBlockFreq(NewSucc, NewSuccFreq.getFrequency());
354         }
355         MadeChange = true;
356         ++NumSplit;
357       } else
358         LLVM_DEBUG(dbgs() << " *** Not legal to break critical edge\n");
359     }
360     // If this iteration over the code changed anything, keep iterating.
361     if (!MadeChange) break;
362     EverMadeChange = true;
363   }
364 
365   // Now clear any kill flags for recorded registers.
366   for (auto I : RegsToClearKillFlags)
367     MRI->clearKillFlags(I);
368   RegsToClearKillFlags.clear();
369 
370   return EverMadeChange;
371 }
372 
373 bool MachineSinking::ProcessBlock(MachineBasicBlock &MBB) {
374   // Can't sink anything out of a block that has less than two successors.
375   if (MBB.succ_size() <= 1 || MBB.empty()) return false;
376 
377   // Don't bother sinking code out of unreachable blocks. In addition to being
378   // unprofitable, it can also lead to infinite looping, because in an
379   // unreachable loop there may be nowhere to stop.
380   if (!DT->isReachableFromEntry(&MBB)) return false;
381 
382   bool MadeChange = false;
383 
384   // Cache all successors, sorted by frequency info and loop depth.
385   AllSuccsCache AllSuccessors;
386 
387   // Walk the basic block bottom-up.  Remember if we saw a store.
388   MachineBasicBlock::iterator I = MBB.end();
389   --I;
390   bool ProcessedBegin, SawStore = false;
391   do {
392     MachineInstr &MI = *I;  // The instruction to sink.
393 
394     // Predecrement I (if it's not begin) so that it isn't invalidated by
395     // sinking.
396     ProcessedBegin = I == MBB.begin();
397     if (!ProcessedBegin)
398       --I;
399 
400     if (MI.isDebugInstr()) {
401       if (MI.isDebugValue())
402         ProcessDbgInst(MI);
403       continue;
404     }
405 
406     bool Joined = PerformTrivialForwardCoalescing(MI, &MBB);
407     if (Joined) {
408       MadeChange = true;
409       continue;
410     }
411 
412     if (SinkInstruction(MI, SawStore, AllSuccessors)) {
413       ++NumSunk;
414       MadeChange = true;
415     }
416 
417     // If we just processed the first instruction in the block, we're done.
418   } while (!ProcessedBegin);
419 
420   SeenDbgUsers.clear();
421   SeenDbgVars.clear();
422 
423   return MadeChange;
424 }
425 
426 void MachineSinking::ProcessDbgInst(MachineInstr &MI) {
427   // When we see DBG_VALUEs for registers, record any vreg it reads, so that
428   // we know what to sink if the vreg def sinks.
429   assert(MI.isDebugValue() && "Expected DBG_VALUE for processing");
430 
431   DebugVariable Var(MI.getDebugVariable(), MI.getDebugExpression(),
432                     MI.getDebugLoc()->getInlinedAt());
433   bool SeenBefore = SeenDbgVars.count(Var) != 0;
434 
435   MachineOperand &MO = MI.getOperand(0);
436   if (MO.isReg() && MO.getReg().isVirtual())
437     SeenDbgUsers[MO.getReg()].push_back(SeenDbgUser(&MI, SeenBefore));
438 
439   // Record the variable for any DBG_VALUE, to avoid re-ordering any of them.
440   SeenDbgVars.insert(Var);
441 }
442 
443 bool MachineSinking::isWorthBreakingCriticalEdge(MachineInstr &MI,
444                                                  MachineBasicBlock *From,
445                                                  MachineBasicBlock *To) {
446   // FIXME: Need much better heuristics.
447 
448   // If the pass has already considered breaking this edge (during this pass
449   // through the function), then let's go ahead and break it. This means
450   // sinking multiple "cheap" instructions into the same block.
451   if (!CEBCandidates.insert(std::make_pair(From, To)).second)
452     return true;
453 
454   if (!MI.isCopy() && !TII->isAsCheapAsAMove(MI))
455     return true;
456 
457   if (From->isSuccessor(To) && MBPI->getEdgeProbability(From, To) <=
458       BranchProbability(SplitEdgeProbabilityThreshold, 100))
459     return true;
460 
461   // MI is cheap, we probably don't want to break the critical edge for it.
462   // However, if this would allow some definitions of its source operands
463   // to be sunk then it's probably worth it.
464   for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
465     const MachineOperand &MO = MI.getOperand(i);
466     if (!MO.isReg() || !MO.isUse())
467       continue;
468     Register Reg = MO.getReg();
469     if (Reg == 0)
470       continue;
471 
472     // We don't move live definitions of physical registers,
473     // so sinking their uses won't enable any opportunities.
474     if (Register::isPhysicalRegister(Reg))
475       continue;
476 
477     // If this instruction is the only user of a virtual register,
478     // check if breaking the edge will enable sinking
479     // both this instruction and the defining instruction.
480     if (MRI->hasOneNonDBGUse(Reg)) {
481       // If the definition resides in same MBB,
482       // claim it's likely we can sink these together.
483       // If definition resides elsewhere, we aren't
484       // blocking it from being sunk so don't break the edge.
485       MachineInstr *DefMI = MRI->getVRegDef(Reg);
486       if (DefMI->getParent() == MI.getParent())
487         return true;
488     }
489   }
490 
491   return false;
492 }
493 
494 bool MachineSinking::PostponeSplitCriticalEdge(MachineInstr &MI,
495                                                MachineBasicBlock *FromBB,
496                                                MachineBasicBlock *ToBB,
497                                                bool BreakPHIEdge) {
498   if (!isWorthBreakingCriticalEdge(MI, FromBB, ToBB))
499     return false;
500 
501   // Avoid breaking back edge. From == To means backedge for single BB loop.
502   if (!SplitEdges || FromBB == ToBB)
503     return false;
504 
505   // Check for backedges of more "complex" loops.
506   if (LI->getLoopFor(FromBB) == LI->getLoopFor(ToBB) &&
507       LI->isLoopHeader(ToBB))
508     return false;
509 
510   // It's not always legal to break critical edges and sink the computation
511   // to the edge.
512   //
513   // %bb.1:
514   // v1024
515   // Beq %bb.3
516   // <fallthrough>
517   // %bb.2:
518   // ... no uses of v1024
519   // <fallthrough>
520   // %bb.3:
521   // ...
522   //       = v1024
523   //
524   // If %bb.1 -> %bb.3 edge is broken and computation of v1024 is inserted:
525   //
526   // %bb.1:
527   // ...
528   // Bne %bb.2
529   // %bb.4:
530   // v1024 =
531   // B %bb.3
532   // %bb.2:
533   // ... no uses of v1024
534   // <fallthrough>
535   // %bb.3:
536   // ...
537   //       = v1024
538   //
539   // This is incorrect since v1024 is not computed along the %bb.1->%bb.2->%bb.3
540   // flow. We need to ensure the new basic block where the computation is
541   // sunk to dominates all the uses.
542   // It's only legal to break critical edge and sink the computation to the
543   // new block if all the predecessors of "To", except for "From", are
544   // not dominated by "From". Given SSA property, this means these
545   // predecessors are dominated by "To".
546   //
547   // There is no need to do this check if all the uses are PHI nodes. PHI
548   // sources are only defined on the specific predecessor edges.
549   if (!BreakPHIEdge) {
550     for (MachineBasicBlock::pred_iterator PI = ToBB->pred_begin(),
551            E = ToBB->pred_end(); PI != E; ++PI) {
552       if (*PI == FromBB)
553         continue;
554       if (!DT->dominates(ToBB, *PI))
555         return false;
556     }
557   }
558 
559   ToSplit.insert(std::make_pair(FromBB, ToBB));
560 
561   return true;
562 }
563 
564 /// isProfitableToSinkTo - Return true if it is profitable to sink MI.
565 bool MachineSinking::isProfitableToSinkTo(unsigned Reg, MachineInstr &MI,
566                                           MachineBasicBlock *MBB,
567                                           MachineBasicBlock *SuccToSinkTo,
568                                           AllSuccsCache &AllSuccessors) {
569   assert (SuccToSinkTo && "Invalid SinkTo Candidate BB");
570 
571   if (MBB == SuccToSinkTo)
572     return false;
573 
574   // It is profitable if SuccToSinkTo does not post dominate current block.
575   if (!PDT->dominates(SuccToSinkTo, MBB))
576     return true;
577 
578   // It is profitable to sink an instruction from a deeper loop to a shallower
579   // loop, even if the latter post-dominates the former (PR21115).
580   if (LI->getLoopDepth(MBB) > LI->getLoopDepth(SuccToSinkTo))
581     return true;
582 
583   // Check if only use in post dominated block is PHI instruction.
584   bool NonPHIUse = false;
585   for (MachineInstr &UseInst : MRI->use_nodbg_instructions(Reg)) {
586     MachineBasicBlock *UseBlock = UseInst.getParent();
587     if (UseBlock == SuccToSinkTo && !UseInst.isPHI())
588       NonPHIUse = true;
589   }
590   if (!NonPHIUse)
591     return true;
592 
593   // If SuccToSinkTo post dominates then also it may be profitable if MI
594   // can further profitably sinked into another block in next round.
595   bool BreakPHIEdge = false;
596   // FIXME - If finding successor is compile time expensive then cache results.
597   if (MachineBasicBlock *MBB2 =
598           FindSuccToSinkTo(MI, SuccToSinkTo, BreakPHIEdge, AllSuccessors))
599     return isProfitableToSinkTo(Reg, MI, SuccToSinkTo, MBB2, AllSuccessors);
600 
601   // If SuccToSinkTo is final destination and it is a post dominator of current
602   // block then it is not profitable to sink MI into SuccToSinkTo block.
603   return false;
604 }
605 
606 /// Get the sorted sequence of successors for this MachineBasicBlock, possibly
607 /// computing it if it was not already cached.
608 SmallVector<MachineBasicBlock *, 4> &
609 MachineSinking::GetAllSortedSuccessors(MachineInstr &MI, MachineBasicBlock *MBB,
610                                        AllSuccsCache &AllSuccessors) const {
611   // Do we have the sorted successors in cache ?
612   auto Succs = AllSuccessors.find(MBB);
613   if (Succs != AllSuccessors.end())
614     return Succs->second;
615 
616   SmallVector<MachineBasicBlock *, 4> AllSuccs(MBB->succ_begin(),
617                                                MBB->succ_end());
618 
619   // Handle cases where sinking can happen but where the sink point isn't a
620   // successor. For example:
621   //
622   //   x = computation
623   //   if () {} else {}
624   //   use x
625   //
626   const std::vector<MachineDomTreeNode *> &Children =
627     DT->getNode(MBB)->getChildren();
628   for (const auto &DTChild : Children)
629     // DomTree children of MBB that have MBB as immediate dominator are added.
630     if (DTChild->getIDom()->getBlock() == MI.getParent() &&
631         // Skip MBBs already added to the AllSuccs vector above.
632         !MBB->isSuccessor(DTChild->getBlock()))
633       AllSuccs.push_back(DTChild->getBlock());
634 
635   // Sort Successors according to their loop depth or block frequency info.
636   llvm::stable_sort(
637       AllSuccs, [this](const MachineBasicBlock *L, const MachineBasicBlock *R) {
638         uint64_t LHSFreq = MBFI ? MBFI->getBlockFreq(L).getFrequency() : 0;
639         uint64_t RHSFreq = MBFI ? MBFI->getBlockFreq(R).getFrequency() : 0;
640         bool HasBlockFreq = LHSFreq != 0 && RHSFreq != 0;
641         return HasBlockFreq ? LHSFreq < RHSFreq
642                             : LI->getLoopDepth(L) < LI->getLoopDepth(R);
643       });
644 
645   auto it = AllSuccessors.insert(std::make_pair(MBB, AllSuccs));
646 
647   return it.first->second;
648 }
649 
650 /// FindSuccToSinkTo - Find a successor to sink this instruction to.
651 MachineBasicBlock *
652 MachineSinking::FindSuccToSinkTo(MachineInstr &MI, MachineBasicBlock *MBB,
653                                  bool &BreakPHIEdge,
654                                  AllSuccsCache &AllSuccessors) {
655   assert (MBB && "Invalid MachineBasicBlock!");
656 
657   // Loop over all the operands of the specified instruction.  If there is
658   // anything we can't handle, bail out.
659 
660   // SuccToSinkTo - This is the successor to sink this instruction to, once we
661   // decide.
662   MachineBasicBlock *SuccToSinkTo = nullptr;
663   for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
664     const MachineOperand &MO = MI.getOperand(i);
665     if (!MO.isReg()) continue;  // Ignore non-register operands.
666 
667     Register Reg = MO.getReg();
668     if (Reg == 0) continue;
669 
670     if (Register::isPhysicalRegister(Reg)) {
671       if (MO.isUse()) {
672         // If the physreg has no defs anywhere, it's just an ambient register
673         // and we can freely move its uses. Alternatively, if it's allocatable,
674         // it could get allocated to something with a def during allocation.
675         if (!MRI->isConstantPhysReg(Reg))
676           return nullptr;
677       } else if (!MO.isDead()) {
678         // A def that isn't dead. We can't move it.
679         return nullptr;
680       }
681     } else {
682       // Virtual register uses are always safe to sink.
683       if (MO.isUse()) continue;
684 
685       // If it's not safe to move defs of the register class, then abort.
686       if (!TII->isSafeToMoveRegClassDefs(MRI->getRegClass(Reg)))
687         return nullptr;
688 
689       // Virtual register defs can only be sunk if all their uses are in blocks
690       // dominated by one of the successors.
691       if (SuccToSinkTo) {
692         // If a previous operand picked a block to sink to, then this operand
693         // must be sinkable to the same block.
694         bool LocalUse = false;
695         if (!AllUsesDominatedByBlock(Reg, SuccToSinkTo, MBB,
696                                      BreakPHIEdge, LocalUse))
697           return nullptr;
698 
699         continue;
700       }
701 
702       // Otherwise, we should look at all the successors and decide which one
703       // we should sink to. If we have reliable block frequency information
704       // (frequency != 0) available, give successors with smaller frequencies
705       // higher priority, otherwise prioritize smaller loop depths.
706       for (MachineBasicBlock *SuccBlock :
707            GetAllSortedSuccessors(MI, MBB, AllSuccessors)) {
708         bool LocalUse = false;
709         if (AllUsesDominatedByBlock(Reg, SuccBlock, MBB,
710                                     BreakPHIEdge, LocalUse)) {
711           SuccToSinkTo = SuccBlock;
712           break;
713         }
714         if (LocalUse)
715           // Def is used locally, it's never safe to move this def.
716           return nullptr;
717       }
718 
719       // If we couldn't find a block to sink to, ignore this instruction.
720       if (!SuccToSinkTo)
721         return nullptr;
722       if (!isProfitableToSinkTo(Reg, MI, MBB, SuccToSinkTo, AllSuccessors))
723         return nullptr;
724     }
725   }
726 
727   // It is not possible to sink an instruction into its own block.  This can
728   // happen with loops.
729   if (MBB == SuccToSinkTo)
730     return nullptr;
731 
732   // It's not safe to sink instructions to EH landing pad. Control flow into
733   // landing pad is implicitly defined.
734   if (SuccToSinkTo && SuccToSinkTo->isEHPad())
735     return nullptr;
736 
737   return SuccToSinkTo;
738 }
739 
740 /// Return true if MI is likely to be usable as a memory operation by the
741 /// implicit null check optimization.
742 ///
743 /// This is a "best effort" heuristic, and should not be relied upon for
744 /// correctness.  This returning true does not guarantee that the implicit null
745 /// check optimization is legal over MI, and this returning false does not
746 /// guarantee MI cannot possibly be used to do a null check.
747 static bool SinkingPreventsImplicitNullCheck(MachineInstr &MI,
748                                              const TargetInstrInfo *TII,
749                                              const TargetRegisterInfo *TRI) {
750   using MachineBranchPredicate = TargetInstrInfo::MachineBranchPredicate;
751 
752   auto *MBB = MI.getParent();
753   if (MBB->pred_size() != 1)
754     return false;
755 
756   auto *PredMBB = *MBB->pred_begin();
757   auto *PredBB = PredMBB->getBasicBlock();
758 
759   // Frontends that don't use implicit null checks have no reason to emit
760   // branches with make.implicit metadata, and this function should always
761   // return false for them.
762   if (!PredBB ||
763       !PredBB->getTerminator()->getMetadata(LLVMContext::MD_make_implicit))
764     return false;
765 
766   const MachineOperand *BaseOp;
767   int64_t Offset;
768   bool OffsetIsScalable;
769   if (!TII->getMemOperandWithOffset(MI, BaseOp, Offset, OffsetIsScalable, TRI))
770     return false;
771 
772   if (!BaseOp->isReg())
773     return false;
774 
775   if (!(MI.mayLoad() && !MI.isPredicable()))
776     return false;
777 
778   MachineBranchPredicate MBP;
779   if (TII->analyzeBranchPredicate(*PredMBB, MBP, false))
780     return false;
781 
782   return MBP.LHS.isReg() && MBP.RHS.isImm() && MBP.RHS.getImm() == 0 &&
783          (MBP.Predicate == MachineBranchPredicate::PRED_NE ||
784           MBP.Predicate == MachineBranchPredicate::PRED_EQ) &&
785          MBP.LHS.getReg() == BaseOp->getReg();
786 }
787 
788 /// If the sunk instruction is a copy, try to forward the copy instead of
789 /// leaving an 'undef' DBG_VALUE in the original location. Don't do this if
790 /// there's any subregister weirdness involved. Returns true if copy
791 /// propagation occurred.
792 static bool attemptDebugCopyProp(MachineInstr &SinkInst, MachineInstr &DbgMI) {
793   const MachineRegisterInfo &MRI = SinkInst.getMF()->getRegInfo();
794   const TargetInstrInfo &TII = *SinkInst.getMF()->getSubtarget().getInstrInfo();
795 
796   // Copy DBG_VALUE operand and set the original to undef. We then check to
797   // see whether this is something that can be copy-forwarded. If it isn't,
798   // continue around the loop.
799   MachineOperand DbgMO = DbgMI.getOperand(0);
800 
801   const MachineOperand *SrcMO = nullptr, *DstMO = nullptr;
802   auto CopyOperands = TII.isCopyInstr(SinkInst);
803   if (!CopyOperands)
804     return false;
805   SrcMO = CopyOperands->Source;
806   DstMO = CopyOperands->Destination;
807 
808   // Check validity of forwarding this copy.
809   bool PostRA = MRI.getNumVirtRegs() == 0;
810 
811   // Trying to forward between physical and virtual registers is too hard.
812   if (DbgMO.getReg().isVirtual() != SrcMO->getReg().isVirtual())
813     return false;
814 
815   // Only try virtual register copy-forwarding before regalloc, and physical
816   // register copy-forwarding after regalloc.
817   bool arePhysRegs = !DbgMO.getReg().isVirtual();
818   if (arePhysRegs != PostRA)
819     return false;
820 
821   // Pre-regalloc, only forward if all subregisters agree (or there are no
822   // subregs at all). More analysis might recover some forwardable copies.
823   if (!PostRA && (DbgMO.getSubReg() != SrcMO->getSubReg() ||
824                   DbgMO.getSubReg() != DstMO->getSubReg()))
825     return false;
826 
827   // Post-regalloc, we may be sinking a DBG_VALUE of a sub or super-register
828   // of this copy. Only forward the copy if the DBG_VALUE operand exactly
829   // matches the copy destination.
830   if (PostRA && DbgMO.getReg() != DstMO->getReg())
831     return false;
832 
833   DbgMI.getOperand(0).setReg(SrcMO->getReg());
834   DbgMI.getOperand(0).setSubReg(SrcMO->getSubReg());
835   return true;
836 }
837 
838 /// Sink an instruction and its associated debug instructions.
839 static void performSink(MachineInstr &MI, MachineBasicBlock &SuccToSinkTo,
840                         MachineBasicBlock::iterator InsertPos,
841                         SmallVectorImpl<MachineInstr *> &DbgValuesToSink) {
842 
843   // If we cannot find a location to use (merge with), then we erase the debug
844   // location to prevent debug-info driven tools from potentially reporting
845   // wrong location information.
846   if (!SuccToSinkTo.empty() && InsertPos != SuccToSinkTo.end())
847     MI.setDebugLoc(DILocation::getMergedLocation(MI.getDebugLoc(),
848                                                  InsertPos->getDebugLoc()));
849   else
850     MI.setDebugLoc(DebugLoc());
851 
852   // Move the instruction.
853   MachineBasicBlock *ParentBlock = MI.getParent();
854   SuccToSinkTo.splice(InsertPos, ParentBlock, MI,
855                       ++MachineBasicBlock::iterator(MI));
856 
857   // Sink a copy of debug users to the insert position. Mark the original
858   // DBG_VALUE location as 'undef', indicating that any earlier variable
859   // location should be terminated as we've optimised away the value at this
860   // point.
861   for (SmallVectorImpl<MachineInstr *>::iterator DBI = DbgValuesToSink.begin(),
862                                                  DBE = DbgValuesToSink.end();
863        DBI != DBE; ++DBI) {
864     MachineInstr *DbgMI = *DBI;
865     MachineInstr *NewDbgMI = DbgMI->getMF()->CloneMachineInstr(*DBI);
866     SuccToSinkTo.insert(InsertPos, NewDbgMI);
867 
868     if (!attemptDebugCopyProp(MI, *DbgMI))
869       DbgMI->getOperand(0).setReg(0);
870   }
871 }
872 
873 /// SinkInstruction - Determine whether it is safe to sink the specified machine
874 /// instruction out of its current block into a successor.
875 bool MachineSinking::SinkInstruction(MachineInstr &MI, bool &SawStore,
876                                      AllSuccsCache &AllSuccessors) {
877   // Don't sink instructions that the target prefers not to sink.
878   if (!TII->shouldSink(MI))
879     return false;
880 
881   // Check if it's safe to move the instruction.
882   if (!MI.isSafeToMove(AA, SawStore))
883     return false;
884 
885   // Convergent operations may not be made control-dependent on additional
886   // values.
887   if (MI.isConvergent())
888     return false;
889 
890   // Don't break implicit null checks.  This is a performance heuristic, and not
891   // required for correctness.
892   if (SinkingPreventsImplicitNullCheck(MI, TII, TRI))
893     return false;
894 
895   // FIXME: This should include support for sinking instructions within the
896   // block they are currently in to shorten the live ranges.  We often get
897   // instructions sunk into the top of a large block, but it would be better to
898   // also sink them down before their first use in the block.  This xform has to
899   // be careful not to *increase* register pressure though, e.g. sinking
900   // "x = y + z" down if it kills y and z would increase the live ranges of y
901   // and z and only shrink the live range of x.
902 
903   bool BreakPHIEdge = false;
904   MachineBasicBlock *ParentBlock = MI.getParent();
905   MachineBasicBlock *SuccToSinkTo =
906       FindSuccToSinkTo(MI, ParentBlock, BreakPHIEdge, AllSuccessors);
907 
908   // If there are no outputs, it must have side-effects.
909   if (!SuccToSinkTo)
910     return false;
911 
912   // If the instruction to move defines a dead physical register which is live
913   // when leaving the basic block, don't move it because it could turn into a
914   // "zombie" define of that preg. E.g., EFLAGS. (<rdar://problem/8030636>)
915   for (unsigned I = 0, E = MI.getNumOperands(); I != E; ++I) {
916     const MachineOperand &MO = MI.getOperand(I);
917     if (!MO.isReg()) continue;
918     Register Reg = MO.getReg();
919     if (Reg == 0 || !Register::isPhysicalRegister(Reg))
920       continue;
921     if (SuccToSinkTo->isLiveIn(Reg))
922       return false;
923   }
924 
925   LLVM_DEBUG(dbgs() << "Sink instr " << MI << "\tinto block " << *SuccToSinkTo);
926 
927   // If the block has multiple predecessors, this is a critical edge.
928   // Decide if we can sink along it or need to break the edge.
929   if (SuccToSinkTo->pred_size() > 1) {
930     // We cannot sink a load across a critical edge - there may be stores in
931     // other code paths.
932     bool TryBreak = false;
933     bool store = true;
934     if (!MI.isSafeToMove(AA, store)) {
935       LLVM_DEBUG(dbgs() << " *** NOTE: Won't sink load along critical edge.\n");
936       TryBreak = true;
937     }
938 
939     // We don't want to sink across a critical edge if we don't dominate the
940     // successor. We could be introducing calculations to new code paths.
941     if (!TryBreak && !DT->dominates(ParentBlock, SuccToSinkTo)) {
942       LLVM_DEBUG(dbgs() << " *** NOTE: Critical edge found\n");
943       TryBreak = true;
944     }
945 
946     // Don't sink instructions into a loop.
947     if (!TryBreak && LI->isLoopHeader(SuccToSinkTo)) {
948       LLVM_DEBUG(dbgs() << " *** NOTE: Loop header found\n");
949       TryBreak = true;
950     }
951 
952     // Otherwise we are OK with sinking along a critical edge.
953     if (!TryBreak)
954       LLVM_DEBUG(dbgs() << "Sinking along critical edge.\n");
955     else {
956       // Mark this edge as to be split.
957       // If the edge can actually be split, the next iteration of the main loop
958       // will sink MI in the newly created block.
959       bool Status =
960         PostponeSplitCriticalEdge(MI, ParentBlock, SuccToSinkTo, BreakPHIEdge);
961       if (!Status)
962         LLVM_DEBUG(dbgs() << " *** PUNTING: Not legal or profitable to "
963                              "break critical edge\n");
964       // The instruction will not be sunk this time.
965       return false;
966     }
967   }
968 
969   if (BreakPHIEdge) {
970     // BreakPHIEdge is true if all the uses are in the successor MBB being
971     // sunken into and they are all PHI nodes. In this case, machine-sink must
972     // break the critical edge first.
973     bool Status = PostponeSplitCriticalEdge(MI, ParentBlock,
974                                             SuccToSinkTo, BreakPHIEdge);
975     if (!Status)
976       LLVM_DEBUG(dbgs() << " *** PUNTING: Not legal or profitable to "
977                            "break critical edge\n");
978     // The instruction will not be sunk this time.
979     return false;
980   }
981 
982   // Determine where to insert into. Skip phi nodes.
983   MachineBasicBlock::iterator InsertPos = SuccToSinkTo->begin();
984   while (InsertPos != SuccToSinkTo->end() && InsertPos->isPHI())
985     ++InsertPos;
986 
987   // Collect debug users of any vreg that this inst defines.
988   SmallVector<MachineInstr *, 4> DbgUsersToSink;
989   for (auto &MO : MI.operands()) {
990     if (!MO.isReg() || !MO.isDef() || !MO.getReg().isVirtual())
991       continue;
992     if (!SeenDbgUsers.count(MO.getReg()))
993       continue;
994 
995     // Sink any users that don't pass any other DBG_VALUEs for this variable.
996     auto &Users = SeenDbgUsers[MO.getReg()];
997     for (auto &User : Users) {
998       MachineInstr *DbgMI = User.getPointer();
999       if (User.getInt()) {
1000         // This DBG_VALUE would re-order assignments. If we can't copy-propagate
1001         // it, it can't be recovered. Set it undef.
1002         if (!attemptDebugCopyProp(MI, *DbgMI))
1003           DbgMI->getOperand(0).setReg(0);
1004       } else {
1005         DbgUsersToSink.push_back(DbgMI);
1006       }
1007     }
1008   }
1009 
1010   // After sinking, some debug users may not be dominated any more. If possible,
1011   // copy-propagate their operands. As it's expensive, don't do this if there's
1012   // no debuginfo in the program.
1013   if (MI.getMF()->getFunction().getSubprogram() && MI.isCopy())
1014     SalvageUnsunkDebugUsersOfCopy(MI, SuccToSinkTo);
1015 
1016   performSink(MI, *SuccToSinkTo, InsertPos, DbgUsersToSink);
1017 
1018   // Conservatively, clear any kill flags, since it's possible that they are no
1019   // longer correct.
1020   // Note that we have to clear the kill flags for any register this instruction
1021   // uses as we may sink over another instruction which currently kills the
1022   // used registers.
1023   for (MachineOperand &MO : MI.operands()) {
1024     if (MO.isReg() && MO.isUse())
1025       RegsToClearKillFlags.set(MO.getReg()); // Remember to clear kill flags.
1026   }
1027 
1028   return true;
1029 }
1030 
1031 void MachineSinking::SalvageUnsunkDebugUsersOfCopy(
1032     MachineInstr &MI, MachineBasicBlock *TargetBlock) {
1033   assert(MI.isCopy());
1034   assert(MI.getOperand(1).isReg());
1035 
1036   // Enumerate all users of vreg operands that are def'd. Skip those that will
1037   // be sunk. For the rest, if they are not dominated by the block we will sink
1038   // MI into, propagate the copy source to them.
1039   SmallVector<MachineInstr *, 4> DbgDefUsers;
1040   const MachineRegisterInfo &MRI = MI.getMF()->getRegInfo();
1041   for (auto &MO : MI.operands()) {
1042     if (!MO.isReg() || !MO.isDef() || !MO.getReg().isVirtual())
1043       continue;
1044     for (auto &User : MRI.use_instructions(MO.getReg())) {
1045       if (!User.isDebugValue() || DT->dominates(TargetBlock, User.getParent()))
1046         continue;
1047 
1048       // If is in same block, will either sink or be use-before-def.
1049       if (User.getParent() == MI.getParent())
1050         continue;
1051 
1052       assert(User.getOperand(0).isReg() &&
1053              "DBG_VALUE user of vreg, but non reg operand?");
1054       DbgDefUsers.push_back(&User);
1055     }
1056   }
1057 
1058   // Point the users of this copy that are no longer dominated, at the source
1059   // of the copy.
1060   for (auto *User : DbgDefUsers) {
1061     User->getOperand(0).setReg(MI.getOperand(1).getReg());
1062     User->getOperand(0).setSubReg(MI.getOperand(1).getSubReg());
1063   }
1064 }
1065 
1066 //===----------------------------------------------------------------------===//
1067 // This pass is not intended to be a replacement or a complete alternative
1068 // for the pre-ra machine sink pass. It is only designed to sink COPY
1069 // instructions which should be handled after RA.
1070 //
1071 // This pass sinks COPY instructions into a successor block, if the COPY is not
1072 // used in the current block and the COPY is live-in to a single successor
1073 // (i.e., doesn't require the COPY to be duplicated).  This avoids executing the
1074 // copy on paths where their results aren't needed.  This also exposes
1075 // additional opportunites for dead copy elimination and shrink wrapping.
1076 //
1077 // These copies were either not handled by or are inserted after the MachineSink
1078 // pass. As an example of the former case, the MachineSink pass cannot sink
1079 // COPY instructions with allocatable source registers; for AArch64 these type
1080 // of copy instructions are frequently used to move function parameters (PhyReg)
1081 // into virtual registers in the entry block.
1082 //
1083 // For the machine IR below, this pass will sink %w19 in the entry into its
1084 // successor (%bb.1) because %w19 is only live-in in %bb.1.
1085 // %bb.0:
1086 //   %wzr = SUBSWri %w1, 1
1087 //   %w19 = COPY %w0
1088 //   Bcc 11, %bb.2
1089 // %bb.1:
1090 //   Live Ins: %w19
1091 //   BL @fun
1092 //   %w0 = ADDWrr %w0, %w19
1093 //   RET %w0
1094 // %bb.2:
1095 //   %w0 = COPY %wzr
1096 //   RET %w0
1097 // As we sink %w19 (CSR in AArch64) into %bb.1, the shrink-wrapping pass will be
1098 // able to see %bb.0 as a candidate.
1099 //===----------------------------------------------------------------------===//
1100 namespace {
1101 
1102 class PostRAMachineSinking : public MachineFunctionPass {
1103 public:
1104   bool runOnMachineFunction(MachineFunction &MF) override;
1105 
1106   static char ID;
1107   PostRAMachineSinking() : MachineFunctionPass(ID) {}
1108   StringRef getPassName() const override { return "PostRA Machine Sink"; }
1109 
1110   void getAnalysisUsage(AnalysisUsage &AU) const override {
1111     AU.setPreservesCFG();
1112     MachineFunctionPass::getAnalysisUsage(AU);
1113   }
1114 
1115   MachineFunctionProperties getRequiredProperties() const override {
1116     return MachineFunctionProperties().set(
1117         MachineFunctionProperties::Property::NoVRegs);
1118   }
1119 
1120 private:
1121   /// Track which register units have been modified and used.
1122   LiveRegUnits ModifiedRegUnits, UsedRegUnits;
1123 
1124   /// Track DBG_VALUEs of (unmodified) register units. Each DBG_VALUE has an
1125   /// entry in this map for each unit it touches.
1126   DenseMap<unsigned, TinyPtrVector<MachineInstr *>> SeenDbgInstrs;
1127 
1128   /// Sink Copy instructions unused in the same block close to their uses in
1129   /// successors.
1130   bool tryToSinkCopy(MachineBasicBlock &BB, MachineFunction &MF,
1131                      const TargetRegisterInfo *TRI, const TargetInstrInfo *TII);
1132 };
1133 } // namespace
1134 
1135 char PostRAMachineSinking::ID = 0;
1136 char &llvm::PostRAMachineSinkingID = PostRAMachineSinking::ID;
1137 
1138 INITIALIZE_PASS(PostRAMachineSinking, "postra-machine-sink",
1139                 "PostRA Machine Sink", false, false)
1140 
1141 static bool aliasWithRegsInLiveIn(MachineBasicBlock &MBB, unsigned Reg,
1142                                   const TargetRegisterInfo *TRI) {
1143   LiveRegUnits LiveInRegUnits(*TRI);
1144   LiveInRegUnits.addLiveIns(MBB);
1145   return !LiveInRegUnits.available(Reg);
1146 }
1147 
1148 static MachineBasicBlock *
1149 getSingleLiveInSuccBB(MachineBasicBlock &CurBB,
1150                       const SmallPtrSetImpl<MachineBasicBlock *> &SinkableBBs,
1151                       unsigned Reg, const TargetRegisterInfo *TRI) {
1152   // Try to find a single sinkable successor in which Reg is live-in.
1153   MachineBasicBlock *BB = nullptr;
1154   for (auto *SI : SinkableBBs) {
1155     if (aliasWithRegsInLiveIn(*SI, Reg, TRI)) {
1156       // If BB is set here, Reg is live-in to at least two sinkable successors,
1157       // so quit.
1158       if (BB)
1159         return nullptr;
1160       BB = SI;
1161     }
1162   }
1163   // Reg is not live-in to any sinkable successors.
1164   if (!BB)
1165     return nullptr;
1166 
1167   // Check if any register aliased with Reg is live-in in other successors.
1168   for (auto *SI : CurBB.successors()) {
1169     if (!SinkableBBs.count(SI) && aliasWithRegsInLiveIn(*SI, Reg, TRI))
1170       return nullptr;
1171   }
1172   return BB;
1173 }
1174 
1175 static MachineBasicBlock *
1176 getSingleLiveInSuccBB(MachineBasicBlock &CurBB,
1177                       const SmallPtrSetImpl<MachineBasicBlock *> &SinkableBBs,
1178                       ArrayRef<unsigned> DefedRegsInCopy,
1179                       const TargetRegisterInfo *TRI) {
1180   MachineBasicBlock *SingleBB = nullptr;
1181   for (auto DefReg : DefedRegsInCopy) {
1182     MachineBasicBlock *BB =
1183         getSingleLiveInSuccBB(CurBB, SinkableBBs, DefReg, TRI);
1184     if (!BB || (SingleBB && SingleBB != BB))
1185       return nullptr;
1186     SingleBB = BB;
1187   }
1188   return SingleBB;
1189 }
1190 
1191 static void clearKillFlags(MachineInstr *MI, MachineBasicBlock &CurBB,
1192                            SmallVectorImpl<unsigned> &UsedOpsInCopy,
1193                            LiveRegUnits &UsedRegUnits,
1194                            const TargetRegisterInfo *TRI) {
1195   for (auto U : UsedOpsInCopy) {
1196     MachineOperand &MO = MI->getOperand(U);
1197     Register SrcReg = MO.getReg();
1198     if (!UsedRegUnits.available(SrcReg)) {
1199       MachineBasicBlock::iterator NI = std::next(MI->getIterator());
1200       for (MachineInstr &UI : make_range(NI, CurBB.end())) {
1201         if (UI.killsRegister(SrcReg, TRI)) {
1202           UI.clearRegisterKills(SrcReg, TRI);
1203           MO.setIsKill(true);
1204           break;
1205         }
1206       }
1207     }
1208   }
1209 }
1210 
1211 static void updateLiveIn(MachineInstr *MI, MachineBasicBlock *SuccBB,
1212                          SmallVectorImpl<unsigned> &UsedOpsInCopy,
1213                          SmallVectorImpl<unsigned> &DefedRegsInCopy) {
1214   MachineFunction &MF = *SuccBB->getParent();
1215   const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
1216   for (unsigned DefReg : DefedRegsInCopy)
1217     for (MCSubRegIterator S(DefReg, TRI, true); S.isValid(); ++S)
1218       SuccBB->removeLiveIn(*S);
1219   for (auto U : UsedOpsInCopy) {
1220     Register SrcReg = MI->getOperand(U).getReg();
1221     LaneBitmask Mask;
1222     for (MCRegUnitMaskIterator S(SrcReg, TRI); S.isValid(); ++S) {
1223       Mask |= (*S).second;
1224     }
1225     SuccBB->addLiveIn(SrcReg, Mask.any() ? Mask : LaneBitmask::getAll());
1226   }
1227   SuccBB->sortUniqueLiveIns();
1228 }
1229 
1230 static bool hasRegisterDependency(MachineInstr *MI,
1231                                   SmallVectorImpl<unsigned> &UsedOpsInCopy,
1232                                   SmallVectorImpl<unsigned> &DefedRegsInCopy,
1233                                   LiveRegUnits &ModifiedRegUnits,
1234                                   LiveRegUnits &UsedRegUnits) {
1235   bool HasRegDependency = false;
1236   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1237     MachineOperand &MO = MI->getOperand(i);
1238     if (!MO.isReg())
1239       continue;
1240     Register Reg = MO.getReg();
1241     if (!Reg)
1242       continue;
1243     if (MO.isDef()) {
1244       if (!ModifiedRegUnits.available(Reg) || !UsedRegUnits.available(Reg)) {
1245         HasRegDependency = true;
1246         break;
1247       }
1248       DefedRegsInCopy.push_back(Reg);
1249 
1250       // FIXME: instead of isUse(), readsReg() would be a better fix here,
1251       // For example, we can ignore modifications in reg with undef. However,
1252       // it's not perfectly clear if skipping the internal read is safe in all
1253       // other targets.
1254     } else if (MO.isUse()) {
1255       if (!ModifiedRegUnits.available(Reg)) {
1256         HasRegDependency = true;
1257         break;
1258       }
1259       UsedOpsInCopy.push_back(i);
1260     }
1261   }
1262   return HasRegDependency;
1263 }
1264 
1265 static SmallSet<unsigned, 4> getRegUnits(unsigned Reg,
1266                                          const TargetRegisterInfo *TRI) {
1267   SmallSet<unsigned, 4> RegUnits;
1268   for (auto RI = MCRegUnitIterator(Reg, TRI); RI.isValid(); ++RI)
1269     RegUnits.insert(*RI);
1270   return RegUnits;
1271 }
1272 
1273 bool PostRAMachineSinking::tryToSinkCopy(MachineBasicBlock &CurBB,
1274                                          MachineFunction &MF,
1275                                          const TargetRegisterInfo *TRI,
1276                                          const TargetInstrInfo *TII) {
1277   SmallPtrSet<MachineBasicBlock *, 2> SinkableBBs;
1278   // FIXME: For now, we sink only to a successor which has a single predecessor
1279   // so that we can directly sink COPY instructions to the successor without
1280   // adding any new block or branch instruction.
1281   for (MachineBasicBlock *SI : CurBB.successors())
1282     if (!SI->livein_empty() && SI->pred_size() == 1)
1283       SinkableBBs.insert(SI);
1284 
1285   if (SinkableBBs.empty())
1286     return false;
1287 
1288   bool Changed = false;
1289 
1290   // Track which registers have been modified and used between the end of the
1291   // block and the current instruction.
1292   ModifiedRegUnits.clear();
1293   UsedRegUnits.clear();
1294   SeenDbgInstrs.clear();
1295 
1296   for (auto I = CurBB.rbegin(), E = CurBB.rend(); I != E;) {
1297     MachineInstr *MI = &*I;
1298     ++I;
1299 
1300     // Track the operand index for use in Copy.
1301     SmallVector<unsigned, 2> UsedOpsInCopy;
1302     // Track the register number defed in Copy.
1303     SmallVector<unsigned, 2> DefedRegsInCopy;
1304 
1305     // We must sink this DBG_VALUE if its operand is sunk. To avoid searching
1306     // for DBG_VALUEs later, record them when they're encountered.
1307     if (MI->isDebugValue()) {
1308       auto &MO = MI->getOperand(0);
1309       if (MO.isReg() && Register::isPhysicalRegister(MO.getReg())) {
1310         // Bail if we can already tell the sink would be rejected, rather
1311         // than needlessly accumulating lots of DBG_VALUEs.
1312         if (hasRegisterDependency(MI, UsedOpsInCopy, DefedRegsInCopy,
1313                                   ModifiedRegUnits, UsedRegUnits))
1314           continue;
1315 
1316         // Record debug use of each reg unit.
1317         SmallSet<unsigned, 4> Units = getRegUnits(MO.getReg(), TRI);
1318         for (unsigned Reg : Units)
1319           SeenDbgInstrs[Reg].push_back(MI);
1320       }
1321       continue;
1322     }
1323 
1324     if (MI->isDebugInstr())
1325       continue;
1326 
1327     // Do not move any instruction across function call.
1328     if (MI->isCall())
1329       return false;
1330 
1331     if (!MI->isCopy() || !MI->getOperand(0).isRenamable()) {
1332       LiveRegUnits::accumulateUsedDefed(*MI, ModifiedRegUnits, UsedRegUnits,
1333                                         TRI);
1334       continue;
1335     }
1336 
1337     // Don't sink the COPY if it would violate a register dependency.
1338     if (hasRegisterDependency(MI, UsedOpsInCopy, DefedRegsInCopy,
1339                               ModifiedRegUnits, UsedRegUnits)) {
1340       LiveRegUnits::accumulateUsedDefed(*MI, ModifiedRegUnits, UsedRegUnits,
1341                                         TRI);
1342       continue;
1343     }
1344     assert((!UsedOpsInCopy.empty() && !DefedRegsInCopy.empty()) &&
1345            "Unexpect SrcReg or DefReg");
1346     MachineBasicBlock *SuccBB =
1347         getSingleLiveInSuccBB(CurBB, SinkableBBs, DefedRegsInCopy, TRI);
1348     // Don't sink if we cannot find a single sinkable successor in which Reg
1349     // is live-in.
1350     if (!SuccBB) {
1351       LiveRegUnits::accumulateUsedDefed(*MI, ModifiedRegUnits, UsedRegUnits,
1352                                         TRI);
1353       continue;
1354     }
1355     assert((SuccBB->pred_size() == 1 && *SuccBB->pred_begin() == &CurBB) &&
1356            "Unexpected predecessor");
1357 
1358     // Collect DBG_VALUEs that must sink with this copy. We've previously
1359     // recorded which reg units that DBG_VALUEs read, if this instruction
1360     // writes any of those units then the corresponding DBG_VALUEs must sink.
1361     SetVector<MachineInstr *> DbgValsToSinkSet;
1362     SmallVector<MachineInstr *, 4> DbgValsToSink;
1363     for (auto &MO : MI->operands()) {
1364       if (!MO.isReg() || !MO.isDef())
1365         continue;
1366 
1367       SmallSet<unsigned, 4> Units = getRegUnits(MO.getReg(), TRI);
1368       for (unsigned Reg : Units)
1369         for (auto *MI : SeenDbgInstrs.lookup(Reg))
1370           DbgValsToSinkSet.insert(MI);
1371     }
1372     DbgValsToSink.insert(DbgValsToSink.begin(), DbgValsToSinkSet.begin(),
1373                          DbgValsToSinkSet.end());
1374 
1375     // Clear the kill flag if SrcReg is killed between MI and the end of the
1376     // block.
1377     clearKillFlags(MI, CurBB, UsedOpsInCopy, UsedRegUnits, TRI);
1378     MachineBasicBlock::iterator InsertPos = SuccBB->getFirstNonPHI();
1379     performSink(*MI, *SuccBB, InsertPos, DbgValsToSink);
1380     updateLiveIn(MI, SuccBB, UsedOpsInCopy, DefedRegsInCopy);
1381 
1382     Changed = true;
1383     ++NumPostRACopySink;
1384   }
1385   return Changed;
1386 }
1387 
1388 bool PostRAMachineSinking::runOnMachineFunction(MachineFunction &MF) {
1389   if (skipFunction(MF.getFunction()))
1390     return false;
1391 
1392   bool Changed = false;
1393   const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
1394   const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
1395 
1396   ModifiedRegUnits.init(*TRI);
1397   UsedRegUnits.init(*TRI);
1398   for (auto &BB : MF)
1399     Changed |= tryToSinkCopy(BB, MF, TRI, TII);
1400 
1401   return Changed;
1402 }
1403