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