xref: /freebsd-src/contrib/llvm-project/llvm/lib/CodeGen/MachineSink.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
10b57cec5SDimitry Andric //===- MachineSink.cpp - Sinking for machine instructions -----------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This pass moves instructions into successor blocks when possible, so that
100b57cec5SDimitry Andric // they aren't executed on paths where their results aren't needed.
110b57cec5SDimitry Andric //
120b57cec5SDimitry Andric // This pass is not intended to be a replacement or a complete alternative
130b57cec5SDimitry Andric // for an LLVM-IR-level sinking pass. It is only designed to sink simple
140b57cec5SDimitry Andric // constructs that are not exposed before lowering and instruction selection.
150b57cec5SDimitry Andric //
160b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
170b57cec5SDimitry Andric 
18480093f4SDimitry Andric #include "llvm/ADT/DenseSet.h"
1981ad6265SDimitry Andric #include "llvm/ADT/DepthFirstIterator.h"
20fe6060f1SDimitry Andric #include "llvm/ADT/MapVector.h"
21480093f4SDimitry Andric #include "llvm/ADT/PointerIntPair.h"
22fb03ea46SDimitry Andric #include "llvm/ADT/PostOrderIterator.h"
230b57cec5SDimitry Andric #include "llvm/ADT/SetVector.h"
240b57cec5SDimitry Andric #include "llvm/ADT/SmallSet.h"
250b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h"
260b57cec5SDimitry Andric #include "llvm/ADT/Statistic.h"
270b57cec5SDimitry Andric #include "llvm/Analysis/AliasAnalysis.h"
28fb03ea46SDimitry Andric #include "llvm/Analysis/CFG.h"
290b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h"
300b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
310b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
3281ad6265SDimitry Andric #include "llvm/CodeGen/MachineCycleAnalysis.h"
330b57cec5SDimitry Andric #include "llvm/CodeGen/MachineDominators.h"
340b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
350b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h"
360b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstr.h"
370b57cec5SDimitry Andric #include "llvm/CodeGen/MachineLoopInfo.h"
380b57cec5SDimitry Andric #include "llvm/CodeGen/MachineOperand.h"
390b57cec5SDimitry Andric #include "llvm/CodeGen/MachinePostDominators.h"
400b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
41e8d8bef9SDimitry Andric #include "llvm/CodeGen/RegisterClassInfo.h"
42e8d8bef9SDimitry Andric #include "llvm/CodeGen/RegisterPressure.h"
430b57cec5SDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h"
445f757f3fSDimitry Andric #include "llvm/CodeGen/TargetPassConfig.h"
450b57cec5SDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h"
460b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h"
470b57cec5SDimitry Andric #include "llvm/IR/BasicBlock.h"
480b57cec5SDimitry Andric #include "llvm/IR/DebugInfoMetadata.h"
498bcb0991SDimitry Andric #include "llvm/IR/LLVMContext.h"
50480093f4SDimitry Andric #include "llvm/InitializePasses.h"
518bcb0991SDimitry Andric #include "llvm/MC/MCRegisterInfo.h"
520b57cec5SDimitry Andric #include "llvm/Pass.h"
530b57cec5SDimitry Andric #include "llvm/Support/BranchProbability.h"
540b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h"
550b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
560b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
570b57cec5SDimitry Andric #include <algorithm>
580b57cec5SDimitry Andric #include <cassert>
590b57cec5SDimitry Andric #include <cstdint>
600b57cec5SDimitry Andric #include <utility>
610b57cec5SDimitry Andric #include <vector>
620b57cec5SDimitry Andric 
630b57cec5SDimitry Andric using namespace llvm;
640b57cec5SDimitry Andric 
650b57cec5SDimitry Andric #define DEBUG_TYPE "machine-sink"
660b57cec5SDimitry Andric 
670b57cec5SDimitry Andric static cl::opt<bool>
680b57cec5SDimitry Andric SplitEdges("machine-sink-split",
690b57cec5SDimitry Andric            cl::desc("Split critical edges during machine sinking"),
700b57cec5SDimitry Andric            cl::init(true), cl::Hidden);
710b57cec5SDimitry Andric 
720b57cec5SDimitry Andric static cl::opt<bool>
730b57cec5SDimitry Andric UseBlockFreqInfo("machine-sink-bfi",
740b57cec5SDimitry Andric            cl::desc("Use block frequency info to find successors to sink"),
750b57cec5SDimitry Andric            cl::init(true), cl::Hidden);
760b57cec5SDimitry Andric 
770b57cec5SDimitry Andric static cl::opt<unsigned> SplitEdgeProbabilityThreshold(
780b57cec5SDimitry Andric     "machine-sink-split-probability-threshold",
790b57cec5SDimitry Andric     cl::desc(
800b57cec5SDimitry Andric         "Percentage threshold for splitting single-instruction critical edge. "
810b57cec5SDimitry Andric         "If the branch threshold is higher than this threshold, we allow "
820b57cec5SDimitry Andric         "speculative execution of up to 1 instruction to avoid branching to "
830b57cec5SDimitry Andric         "splitted critical edge"),
840b57cec5SDimitry Andric     cl::init(40), cl::Hidden);
850b57cec5SDimitry Andric 
86e8d8bef9SDimitry Andric static cl::opt<unsigned> SinkLoadInstsPerBlockThreshold(
87e8d8bef9SDimitry Andric     "machine-sink-load-instrs-threshold",
88e8d8bef9SDimitry Andric     cl::desc("Do not try to find alias store for a load if there is a in-path "
89e8d8bef9SDimitry Andric              "block whose instruction number is higher than this threshold."),
90e8d8bef9SDimitry Andric     cl::init(2000), cl::Hidden);
91e8d8bef9SDimitry Andric 
92e8d8bef9SDimitry Andric static cl::opt<unsigned> SinkLoadBlocksThreshold(
93e8d8bef9SDimitry Andric     "machine-sink-load-blocks-threshold",
94e8d8bef9SDimitry Andric     cl::desc("Do not try to find alias store for a load if the block number in "
95e8d8bef9SDimitry Andric              "the straight line is higher than this threshold."),
96e8d8bef9SDimitry Andric     cl::init(20), cl::Hidden);
97e8d8bef9SDimitry Andric 
98fe6060f1SDimitry Andric static cl::opt<bool>
9981ad6265SDimitry Andric     SinkInstsIntoCycle("sink-insts-to-avoid-spills",
10081ad6265SDimitry Andric                        cl::desc("Sink instructions into cycles to avoid "
101fe6060f1SDimitry Andric                                 "register spills"),
102fe6060f1SDimitry Andric                        cl::init(false), cl::Hidden);
103fe6060f1SDimitry Andric 
10481ad6265SDimitry Andric static cl::opt<unsigned> SinkIntoCycleLimit(
10581ad6265SDimitry Andric     "machine-sink-cycle-limit",
10681ad6265SDimitry Andric     cl::desc("The maximum number of instructions considered for cycle sinking."),
107fe6060f1SDimitry Andric     cl::init(50), cl::Hidden);
108fe6060f1SDimitry Andric 
1090b57cec5SDimitry Andric STATISTIC(NumSunk,      "Number of machine instructions sunk");
11081ad6265SDimitry Andric STATISTIC(NumCycleSunk,  "Number of machine instructions sunk into a cycle");
1110b57cec5SDimitry Andric STATISTIC(NumSplit,     "Number of critical edges split");
1120b57cec5SDimitry Andric STATISTIC(NumCoalesces, "Number of copies coalesced");
1130b57cec5SDimitry Andric STATISTIC(NumPostRACopySink, "Number of copies sunk after RA");
1140b57cec5SDimitry Andric 
1150b57cec5SDimitry Andric namespace {
1160b57cec5SDimitry Andric 
1170b57cec5SDimitry Andric   class MachineSinking : public MachineFunctionPass {
1185f757f3fSDimitry Andric     const TargetSubtargetInfo *STI = nullptr;
11906c3fb27SDimitry Andric     const TargetInstrInfo *TII = nullptr;
12006c3fb27SDimitry Andric     const TargetRegisterInfo *TRI = nullptr;
12106c3fb27SDimitry Andric     MachineRegisterInfo *MRI = nullptr;      // Machine register information
12206c3fb27SDimitry Andric     MachineDominatorTree *DT = nullptr;      // Machine dominator tree
12306c3fb27SDimitry Andric     MachinePostDominatorTree *PDT = nullptr; // Machine post dominator tree
12406c3fb27SDimitry Andric     MachineCycleInfo *CI = nullptr;
12506c3fb27SDimitry Andric     MachineBlockFrequencyInfo *MBFI = nullptr;
12606c3fb27SDimitry Andric     const MachineBranchProbabilityInfo *MBPI = nullptr;
12706c3fb27SDimitry Andric     AliasAnalysis *AA = nullptr;
128e8d8bef9SDimitry Andric     RegisterClassInfo RegClassInfo;
1290b57cec5SDimitry Andric 
1300b57cec5SDimitry Andric     // Remember which edges have been considered for breaking.
1310b57cec5SDimitry Andric     SmallSet<std::pair<MachineBasicBlock*, MachineBasicBlock*>, 8>
1320b57cec5SDimitry Andric     CEBCandidates;
133*0fca6ea1SDimitry Andric     // Memorize the register that also wanted to sink into the same block along
134*0fca6ea1SDimitry Andric     // a different critical edge.
135*0fca6ea1SDimitry Andric     // {register to sink, sink-to block} -> the first sink-from block.
136*0fca6ea1SDimitry Andric     // We're recording the first sink-from block because that (critical) edge
137*0fca6ea1SDimitry Andric     // was deferred until we see another register that's going to sink into the
138*0fca6ea1SDimitry Andric     // same block.
139*0fca6ea1SDimitry Andric     DenseMap<std::pair<Register, MachineBasicBlock *>, MachineBasicBlock *>
140*0fca6ea1SDimitry Andric         CEMergeCandidates;
1410b57cec5SDimitry Andric     // Remember which edges we are about to split.
1420b57cec5SDimitry Andric     // This is different from CEBCandidates since those edges
1430b57cec5SDimitry Andric     // will be split.
1440b57cec5SDimitry Andric     SetVector<std::pair<MachineBasicBlock *, MachineBasicBlock *>> ToSplit;
1450b57cec5SDimitry Andric 
146349cc55cSDimitry Andric     DenseSet<Register> RegsToClearKillFlags;
1470b57cec5SDimitry Andric 
1480b57cec5SDimitry Andric     using AllSuccsCache =
149*0fca6ea1SDimitry Andric         SmallDenseMap<MachineBasicBlock *, SmallVector<MachineBasicBlock *, 4>>;
1500b57cec5SDimitry Andric 
151480093f4SDimitry Andric     /// DBG_VALUE pointer and flag. The flag is true if this DBG_VALUE is
152480093f4SDimitry Andric     /// post-dominated by another DBG_VALUE of the same variable location.
153480093f4SDimitry Andric     /// This is necessary to detect sequences such as:
154480093f4SDimitry Andric     ///     %0 = someinst
155480093f4SDimitry Andric     ///     DBG_VALUE %0, !123, !DIExpression()
156480093f4SDimitry Andric     ///     %1 = anotherinst
157480093f4SDimitry Andric     ///     DBG_VALUE %1, !123, !DIExpression()
158480093f4SDimitry Andric     /// Where if %0 were to sink, the DBG_VAUE should not sink with it, as that
159480093f4SDimitry Andric     /// would re-order assignments.
160480093f4SDimitry Andric     using SeenDbgUser = PointerIntPair<MachineInstr *, 1>;
161480093f4SDimitry Andric 
162480093f4SDimitry Andric     /// Record of DBG_VALUE uses of vregs in a block, so that we can identify
163480093f4SDimitry Andric     /// debug instructions to sink.
164480093f4SDimitry Andric     SmallDenseMap<unsigned, TinyPtrVector<SeenDbgUser>> SeenDbgUsers;
165480093f4SDimitry Andric 
166480093f4SDimitry Andric     /// Record of debug variables that have had their locations set in the
167480093f4SDimitry Andric     /// current block.
168480093f4SDimitry Andric     DenseSet<DebugVariable> SeenDbgVars;
169480093f4SDimitry Andric 
1705f757f3fSDimitry Andric     DenseMap<std::pair<MachineBasicBlock *, MachineBasicBlock *>, bool>
171e8d8bef9SDimitry Andric         HasStoreCache;
1725f757f3fSDimitry Andric 
1735f757f3fSDimitry Andric     DenseMap<std::pair<MachineBasicBlock *, MachineBasicBlock *>,
1745f757f3fSDimitry Andric              SmallVector<MachineInstr *>>
175e8d8bef9SDimitry Andric         StoreInstrCache;
176e8d8bef9SDimitry Andric 
177e8d8bef9SDimitry Andric     /// Cached BB's register pressure.
1785f757f3fSDimitry Andric     DenseMap<const MachineBasicBlock *, std::vector<unsigned>>
1795f757f3fSDimitry Andric         CachedRegisterPressure;
1805f757f3fSDimitry Andric 
1815f757f3fSDimitry Andric     bool EnableSinkAndFold;
182e8d8bef9SDimitry Andric 
1830b57cec5SDimitry Andric   public:
1840b57cec5SDimitry Andric     static char ID; // Pass identification
1850b57cec5SDimitry Andric 
1860b57cec5SDimitry Andric     MachineSinking() : MachineFunctionPass(ID) {
1870b57cec5SDimitry Andric       initializeMachineSinkingPass(*PassRegistry::getPassRegistry());
1880b57cec5SDimitry Andric     }
1890b57cec5SDimitry Andric 
1900b57cec5SDimitry Andric     bool runOnMachineFunction(MachineFunction &MF) override;
1910b57cec5SDimitry Andric 
1920b57cec5SDimitry Andric     void getAnalysisUsage(AnalysisUsage &AU) const override {
1930b57cec5SDimitry Andric       MachineFunctionPass::getAnalysisUsage(AU);
1940b57cec5SDimitry Andric       AU.addRequired<AAResultsWrapperPass>();
195*0fca6ea1SDimitry Andric       AU.addRequired<MachineDominatorTreeWrapperPass>();
196*0fca6ea1SDimitry Andric       AU.addRequired<MachinePostDominatorTreeWrapperPass>();
19781ad6265SDimitry Andric       AU.addRequired<MachineCycleInfoWrapperPass>();
198*0fca6ea1SDimitry Andric       AU.addRequired<MachineBranchProbabilityInfoWrapperPass>();
19981ad6265SDimitry Andric       AU.addPreserved<MachineCycleInfoWrapperPass>();
200*0fca6ea1SDimitry Andric       AU.addPreserved<MachineLoopInfoWrapperPass>();
2010b57cec5SDimitry Andric       if (UseBlockFreqInfo)
202*0fca6ea1SDimitry Andric         AU.addRequired<MachineBlockFrequencyInfoWrapperPass>();
2035f757f3fSDimitry Andric       AU.addRequired<TargetPassConfig>();
2040b57cec5SDimitry Andric     }
2050b57cec5SDimitry Andric 
2060b57cec5SDimitry Andric     void releaseMemory() override {
2070b57cec5SDimitry Andric       CEBCandidates.clear();
208*0fca6ea1SDimitry Andric       CEMergeCandidates.clear();
2090b57cec5SDimitry Andric     }
2100b57cec5SDimitry Andric 
2110b57cec5SDimitry Andric   private:
2120b57cec5SDimitry Andric     bool ProcessBlock(MachineBasicBlock &MBB);
213480093f4SDimitry Andric     void ProcessDbgInst(MachineInstr &MI);
214*0fca6ea1SDimitry Andric     bool isLegalToBreakCriticalEdge(MachineInstr &MI, MachineBasicBlock *From,
215*0fca6ea1SDimitry Andric                                     MachineBasicBlock *To, bool BreakPHIEdge);
216*0fca6ea1SDimitry Andric     bool isWorthBreakingCriticalEdge(MachineInstr &MI, MachineBasicBlock *From,
217*0fca6ea1SDimitry Andric                                      MachineBasicBlock *To,
218*0fca6ea1SDimitry Andric                                      MachineBasicBlock *&DeferredFromBlock);
2190b57cec5SDimitry Andric 
220e8d8bef9SDimitry Andric     bool hasStoreBetween(MachineBasicBlock *From, MachineBasicBlock *To,
221e8d8bef9SDimitry Andric                          MachineInstr &MI);
222e8d8bef9SDimitry Andric 
2230b57cec5SDimitry Andric     /// Postpone the splitting of the given critical
2240b57cec5SDimitry Andric     /// edge (\p From, \p To).
2250b57cec5SDimitry Andric     ///
2260b57cec5SDimitry Andric     /// We do not split the edges on the fly. Indeed, this invalidates
2270b57cec5SDimitry Andric     /// the dominance information and thus triggers a lot of updates
2280b57cec5SDimitry Andric     /// of that information underneath.
2290b57cec5SDimitry Andric     /// Instead, we postpone all the splits after each iteration of
2300b57cec5SDimitry Andric     /// the main loop. That way, the information is at least valid
2310b57cec5SDimitry Andric     /// for the lifetime of an iteration.
2320b57cec5SDimitry Andric     ///
2330b57cec5SDimitry Andric     /// \return True if the edge is marked as toSplit, false otherwise.
2340b57cec5SDimitry Andric     /// False can be returned if, for instance, this is not profitable.
2350b57cec5SDimitry Andric     bool PostponeSplitCriticalEdge(MachineInstr &MI,
2360b57cec5SDimitry Andric                                    MachineBasicBlock *From,
2370b57cec5SDimitry Andric                                    MachineBasicBlock *To,
2380b57cec5SDimitry Andric                                    bool BreakPHIEdge);
2390b57cec5SDimitry Andric     bool SinkInstruction(MachineInstr &MI, bool &SawStore,
2400b57cec5SDimitry Andric                          AllSuccsCache &AllSuccessors);
241480093f4SDimitry Andric 
242480093f4SDimitry Andric     /// If we sink a COPY inst, some debug users of it's destination may no
243480093f4SDimitry Andric     /// longer be dominated by the COPY, and will eventually be dropped.
244480093f4SDimitry Andric     /// This is easily rectified by forwarding the non-dominated debug uses
245480093f4SDimitry Andric     /// to the copy source.
246480093f4SDimitry Andric     void SalvageUnsunkDebugUsersOfCopy(MachineInstr &,
247480093f4SDimitry Andric                                        MachineBasicBlock *TargetBlock);
248e8d8bef9SDimitry Andric     bool AllUsesDominatedByBlock(Register Reg, MachineBasicBlock *MBB,
249e8d8bef9SDimitry Andric                                  MachineBasicBlock *DefMBB, bool &BreakPHIEdge,
250e8d8bef9SDimitry Andric                                  bool &LocalUse) const;
2510b57cec5SDimitry Andric     MachineBasicBlock *FindSuccToSinkTo(MachineInstr &MI, MachineBasicBlock *MBB,
2520b57cec5SDimitry Andric                bool &BreakPHIEdge, AllSuccsCache &AllSuccessors);
253fe6060f1SDimitry Andric 
25481ad6265SDimitry Andric     void FindCycleSinkCandidates(MachineCycle *Cycle, MachineBasicBlock *BB,
255fe6060f1SDimitry Andric                                  SmallVectorImpl<MachineInstr *> &Candidates);
25681ad6265SDimitry Andric     bool SinkIntoCycle(MachineCycle *Cycle, MachineInstr &I);
257fe6060f1SDimitry Andric 
258e8d8bef9SDimitry Andric     bool isProfitableToSinkTo(Register Reg, MachineInstr &MI,
2590b57cec5SDimitry Andric                               MachineBasicBlock *MBB,
2600b57cec5SDimitry Andric                               MachineBasicBlock *SuccToSinkTo,
2610b57cec5SDimitry Andric                               AllSuccsCache &AllSuccessors);
2620b57cec5SDimitry Andric 
2630b57cec5SDimitry Andric     bool PerformTrivialForwardCoalescing(MachineInstr &MI,
2640b57cec5SDimitry Andric                                          MachineBasicBlock *MBB);
2650b57cec5SDimitry Andric 
2665f757f3fSDimitry Andric     bool PerformSinkAndFold(MachineInstr &MI, MachineBasicBlock *MBB);
2675f757f3fSDimitry Andric 
2680b57cec5SDimitry Andric     SmallVector<MachineBasicBlock *, 4> &
2690b57cec5SDimitry Andric     GetAllSortedSuccessors(MachineInstr &MI, MachineBasicBlock *MBB,
2700b57cec5SDimitry Andric                            AllSuccsCache &AllSuccessors) const;
271e8d8bef9SDimitry Andric 
2725f757f3fSDimitry Andric     std::vector<unsigned> &getBBRegisterPressure(const MachineBasicBlock &MBB);
2735f757f3fSDimitry Andric 
2745f757f3fSDimitry Andric     bool registerPressureSetExceedsLimit(unsigned NRegs,
2755f757f3fSDimitry Andric                                          const TargetRegisterClass *RC,
2765f757f3fSDimitry Andric                                          const MachineBasicBlock &MBB);
2770b57cec5SDimitry Andric   };
2780b57cec5SDimitry Andric 
2790b57cec5SDimitry Andric } // end anonymous namespace
2800b57cec5SDimitry Andric 
2810b57cec5SDimitry Andric char MachineSinking::ID = 0;
2820b57cec5SDimitry Andric 
2830b57cec5SDimitry Andric char &llvm::MachineSinkingID = MachineSinking::ID;
2840b57cec5SDimitry Andric 
2850b57cec5SDimitry Andric INITIALIZE_PASS_BEGIN(MachineSinking, DEBUG_TYPE,
2860b57cec5SDimitry Andric                       "Machine code sinking", false, false)
287*0fca6ea1SDimitry Andric INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfoWrapperPass)
288*0fca6ea1SDimitry Andric INITIALIZE_PASS_DEPENDENCY(MachineDominatorTreeWrapperPass)
28981ad6265SDimitry Andric INITIALIZE_PASS_DEPENDENCY(MachineCycleInfoWrapperPass)
2900b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
2910b57cec5SDimitry Andric INITIALIZE_PASS_END(MachineSinking, DEBUG_TYPE,
2920b57cec5SDimitry Andric                     "Machine code sinking", false, false)
2930b57cec5SDimitry Andric 
29406c3fb27SDimitry Andric /// Return true if a target defined block prologue instruction interferes
29506c3fb27SDimitry Andric /// with a sink candidate.
29606c3fb27SDimitry Andric static bool blockPrologueInterferes(const MachineBasicBlock *BB,
29706c3fb27SDimitry Andric                                     MachineBasicBlock::const_iterator End,
29806c3fb27SDimitry Andric                                     const MachineInstr &MI,
29906c3fb27SDimitry Andric                                     const TargetRegisterInfo *TRI,
30006c3fb27SDimitry Andric                                     const TargetInstrInfo *TII,
30106c3fb27SDimitry Andric                                     const MachineRegisterInfo *MRI) {
30206c3fb27SDimitry Andric   for (MachineBasicBlock::const_iterator PI = BB->getFirstNonPHI(); PI != End;
30306c3fb27SDimitry Andric        ++PI) {
30406c3fb27SDimitry Andric     // Only check target defined prologue instructions
30506c3fb27SDimitry Andric     if (!TII->isBasicBlockPrologue(*PI))
30606c3fb27SDimitry Andric       continue;
30706c3fb27SDimitry Andric     for (auto &MO : MI.operands()) {
30806c3fb27SDimitry Andric       if (!MO.isReg())
30906c3fb27SDimitry Andric         continue;
31006c3fb27SDimitry Andric       Register Reg = MO.getReg();
31106c3fb27SDimitry Andric       if (!Reg)
31206c3fb27SDimitry Andric         continue;
31306c3fb27SDimitry Andric       if (MO.isUse()) {
3145f757f3fSDimitry Andric         if (Reg.isPhysical() &&
3155f757f3fSDimitry Andric             (TII->isIgnorableUse(MO) || (MRI && MRI->isConstantPhysReg(Reg))))
31606c3fb27SDimitry Andric           continue;
31706c3fb27SDimitry Andric         if (PI->modifiesRegister(Reg, TRI))
31806c3fb27SDimitry Andric           return true;
31906c3fb27SDimitry Andric       } else {
32006c3fb27SDimitry Andric         if (PI->readsRegister(Reg, TRI))
32106c3fb27SDimitry Andric           return true;
32206c3fb27SDimitry Andric         // Check for interference with non-dead defs
323*0fca6ea1SDimitry Andric         auto *DefOp = PI->findRegisterDefOperand(Reg, TRI, false, true);
32406c3fb27SDimitry Andric         if (DefOp && !DefOp->isDead())
32506c3fb27SDimitry Andric           return true;
32606c3fb27SDimitry Andric       }
32706c3fb27SDimitry Andric     }
32806c3fb27SDimitry Andric   }
32906c3fb27SDimitry Andric 
33006c3fb27SDimitry Andric   return false;
33106c3fb27SDimitry Andric }
33206c3fb27SDimitry Andric 
3330b57cec5SDimitry Andric bool MachineSinking::PerformTrivialForwardCoalescing(MachineInstr &MI,
3340b57cec5SDimitry Andric                                                      MachineBasicBlock *MBB) {
3350b57cec5SDimitry Andric   if (!MI.isCopy())
3360b57cec5SDimitry Andric     return false;
3370b57cec5SDimitry Andric 
3388bcb0991SDimitry Andric   Register SrcReg = MI.getOperand(1).getReg();
3398bcb0991SDimitry Andric   Register DstReg = MI.getOperand(0).getReg();
340bdd1243dSDimitry Andric   if (!SrcReg.isVirtual() || !DstReg.isVirtual() ||
341bdd1243dSDimitry Andric       !MRI->hasOneNonDBGUse(SrcReg))
3420b57cec5SDimitry Andric     return false;
3430b57cec5SDimitry Andric 
3440b57cec5SDimitry Andric   const TargetRegisterClass *SRC = MRI->getRegClass(SrcReg);
3450b57cec5SDimitry Andric   const TargetRegisterClass *DRC = MRI->getRegClass(DstReg);
3460b57cec5SDimitry Andric   if (SRC != DRC)
3470b57cec5SDimitry Andric     return false;
3480b57cec5SDimitry Andric 
3490b57cec5SDimitry Andric   MachineInstr *DefMI = MRI->getVRegDef(SrcReg);
3500b57cec5SDimitry Andric   if (DefMI->isCopyLike())
3510b57cec5SDimitry Andric     return false;
3520b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Coalescing: " << *DefMI);
3530b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "*** to: " << MI);
3540b57cec5SDimitry Andric   MRI->replaceRegWith(DstReg, SrcReg);
3550b57cec5SDimitry Andric   MI.eraseFromParent();
3560b57cec5SDimitry Andric 
3570b57cec5SDimitry Andric   // Conservatively, clear any kill flags, since it's possible that they are no
3580b57cec5SDimitry Andric   // longer correct.
3590b57cec5SDimitry Andric   MRI->clearKillFlags(SrcReg);
3600b57cec5SDimitry Andric 
3610b57cec5SDimitry Andric   ++NumCoalesces;
3620b57cec5SDimitry Andric   return true;
3630b57cec5SDimitry Andric }
3640b57cec5SDimitry Andric 
3655f757f3fSDimitry Andric bool MachineSinking::PerformSinkAndFold(MachineInstr &MI,
3665f757f3fSDimitry Andric                                         MachineBasicBlock *MBB) {
3675f757f3fSDimitry Andric   if (MI.isCopy() || MI.mayLoadOrStore() ||
3685f757f3fSDimitry Andric       MI.getOpcode() == TargetOpcode::REG_SEQUENCE)
3695f757f3fSDimitry Andric     return false;
3705f757f3fSDimitry Andric 
3715f757f3fSDimitry Andric   // Don't sink instructions that the target prefers not to sink.
3725f757f3fSDimitry Andric   if (!TII->shouldSink(MI))
3735f757f3fSDimitry Andric     return false;
3745f757f3fSDimitry Andric 
3755f757f3fSDimitry Andric   // Check if it's safe to move the instruction.
3765f757f3fSDimitry Andric   bool SawStore = true;
3775f757f3fSDimitry Andric   if (!MI.isSafeToMove(AA, SawStore))
3785f757f3fSDimitry Andric     return false;
3795f757f3fSDimitry Andric 
3805f757f3fSDimitry Andric   // Convergent operations may not be made control-dependent on additional
3815f757f3fSDimitry Andric   // values.
3825f757f3fSDimitry Andric   if (MI.isConvergent())
3835f757f3fSDimitry Andric     return false;
3845f757f3fSDimitry Andric 
3855f757f3fSDimitry Andric   // Don't sink defs/uses of hard registers or if the instruction defines more
3865f757f3fSDimitry Andric   // than one register.
3875f757f3fSDimitry Andric   // Don't sink more than two register uses - it'll cover most of the cases and
3885f757f3fSDimitry Andric   // greatly simplifies the register pressure checks.
3895f757f3fSDimitry Andric   Register DefReg;
3905f757f3fSDimitry Andric   Register UsedRegA, UsedRegB;
3915f757f3fSDimitry Andric   for (const MachineOperand &MO : MI.operands()) {
3925f757f3fSDimitry Andric     if (MO.isImm() || MO.isRegMask() || MO.isRegLiveOut() || MO.isMetadata() ||
3935f757f3fSDimitry Andric         MO.isMCSymbol() || MO.isDbgInstrRef() || MO.isCFIIndex() ||
3945f757f3fSDimitry Andric         MO.isIntrinsicID() || MO.isPredicate() || MO.isShuffleMask())
3955f757f3fSDimitry Andric       continue;
3965f757f3fSDimitry Andric     if (!MO.isReg())
3975f757f3fSDimitry Andric       return false;
3985f757f3fSDimitry Andric 
3995f757f3fSDimitry Andric     Register Reg = MO.getReg();
4005f757f3fSDimitry Andric     if (Reg == 0)
4015f757f3fSDimitry Andric       continue;
4025f757f3fSDimitry Andric 
4035f757f3fSDimitry Andric     if (Reg.isVirtual()) {
4045f757f3fSDimitry Andric       if (MO.isDef()) {
4055f757f3fSDimitry Andric         if (DefReg)
4065f757f3fSDimitry Andric           return false;
4075f757f3fSDimitry Andric         DefReg = Reg;
4085f757f3fSDimitry Andric         continue;
4095f757f3fSDimitry Andric       }
4105f757f3fSDimitry Andric 
4115f757f3fSDimitry Andric       if (UsedRegA == 0)
4125f757f3fSDimitry Andric         UsedRegA = Reg;
4135f757f3fSDimitry Andric       else if (UsedRegB == 0)
4145f757f3fSDimitry Andric         UsedRegB = Reg;
4155f757f3fSDimitry Andric       else
4165f757f3fSDimitry Andric         return false;
4175f757f3fSDimitry Andric       continue;
4185f757f3fSDimitry Andric     }
4195f757f3fSDimitry Andric 
420*0fca6ea1SDimitry Andric     if (Reg.isPhysical() && MO.isUse() &&
4215f757f3fSDimitry Andric         (MRI->isConstantPhysReg(Reg) || TII->isIgnorableUse(MO)))
4225f757f3fSDimitry Andric       continue;
4235f757f3fSDimitry Andric 
4245f757f3fSDimitry Andric     return false;
4255f757f3fSDimitry Andric   }
4265f757f3fSDimitry Andric 
4275f757f3fSDimitry Andric   // Scan uses of the destination register. Every use, except the last, must be
4285f757f3fSDimitry Andric   // a copy, with a chain of copies terminating with either a copy into a hard
4295f757f3fSDimitry Andric   // register, or a load/store instruction where the use is part of the
4305f757f3fSDimitry Andric   // address (*not* the stored value).
4315f757f3fSDimitry Andric   using SinkInfo = std::pair<MachineInstr *, ExtAddrMode>;
4325f757f3fSDimitry Andric   SmallVector<SinkInfo> SinkInto;
4335f757f3fSDimitry Andric   SmallVector<Register> Worklist;
4345f757f3fSDimitry Andric 
4355f757f3fSDimitry Andric   const TargetRegisterClass *RC = MRI->getRegClass(DefReg);
4365f757f3fSDimitry Andric   const TargetRegisterClass *RCA =
4375f757f3fSDimitry Andric       UsedRegA == 0 ? nullptr : MRI->getRegClass(UsedRegA);
4385f757f3fSDimitry Andric   const TargetRegisterClass *RCB =
4395f757f3fSDimitry Andric       UsedRegB == 0 ? nullptr : MRI->getRegClass(UsedRegB);
4405f757f3fSDimitry Andric 
4415f757f3fSDimitry Andric   Worklist.push_back(DefReg);
4425f757f3fSDimitry Andric   while (!Worklist.empty()) {
4435f757f3fSDimitry Andric     Register Reg = Worklist.pop_back_val();
4445f757f3fSDimitry Andric 
4455f757f3fSDimitry Andric     for (MachineOperand &MO : MRI->use_nodbg_operands(Reg)) {
4465f757f3fSDimitry Andric       ExtAddrMode MaybeAM;
4475f757f3fSDimitry Andric       MachineInstr &UseInst = *MO.getParent();
4485f757f3fSDimitry Andric       if (UseInst.isCopy()) {
4495f757f3fSDimitry Andric         Register DstReg;
4505f757f3fSDimitry Andric         if (const MachineOperand &O = UseInst.getOperand(0); O.isReg())
4515f757f3fSDimitry Andric           DstReg = O.getReg();
4525f757f3fSDimitry Andric         if (DstReg == 0)
4535f757f3fSDimitry Andric           return false;
4545f757f3fSDimitry Andric         if (DstReg.isVirtual()) {
4555f757f3fSDimitry Andric           Worklist.push_back(DstReg);
4565f757f3fSDimitry Andric           continue;
4575f757f3fSDimitry Andric         }
4585f757f3fSDimitry Andric         // If we are going to replace a copy, the original instruction must be
4595f757f3fSDimitry Andric         // as cheap as a copy.
4605f757f3fSDimitry Andric         if (!TII->isAsCheapAsAMove(MI))
4615f757f3fSDimitry Andric           return false;
4625f757f3fSDimitry Andric         // The hard register must be in the register class of the original
4635f757f3fSDimitry Andric         // instruction's destination register.
4645f757f3fSDimitry Andric         if (!RC->contains(DstReg))
4655f757f3fSDimitry Andric           return false;
4665f757f3fSDimitry Andric       } else if (UseInst.mayLoadOrStore()) {
4675f757f3fSDimitry Andric         ExtAddrMode AM;
4685f757f3fSDimitry Andric         if (!TII->canFoldIntoAddrMode(UseInst, Reg, MI, AM))
4695f757f3fSDimitry Andric           return false;
4705f757f3fSDimitry Andric         MaybeAM = AM;
4715f757f3fSDimitry Andric       } else {
4725f757f3fSDimitry Andric         return false;
4735f757f3fSDimitry Andric       }
4745f757f3fSDimitry Andric 
4755f757f3fSDimitry Andric       if (UseInst.getParent() != MI.getParent()) {
4765f757f3fSDimitry Andric         // If the register class of the register we are replacing is a superset
4775f757f3fSDimitry Andric         // of any of the register classes of the operands of the materialized
4785f757f3fSDimitry Andric         // instruction don't consider that live range extended.
4795f757f3fSDimitry Andric         const TargetRegisterClass *RCS = MRI->getRegClass(Reg);
4805f757f3fSDimitry Andric         if (RCA && RCA->hasSuperClassEq(RCS))
4815f757f3fSDimitry Andric           RCA = nullptr;
4825f757f3fSDimitry Andric         else if (RCB && RCB->hasSuperClassEq(RCS))
4835f757f3fSDimitry Andric           RCB = nullptr;
4845f757f3fSDimitry Andric         if (RCA || RCB) {
4855f757f3fSDimitry Andric           if (RCA == nullptr) {
4865f757f3fSDimitry Andric             RCA = RCB;
4875f757f3fSDimitry Andric             RCB = nullptr;
4885f757f3fSDimitry Andric           }
4895f757f3fSDimitry Andric 
4905f757f3fSDimitry Andric           unsigned NRegs = !!RCA + !!RCB;
4915f757f3fSDimitry Andric           if (RCA == RCB)
4925f757f3fSDimitry Andric             RCB = nullptr;
4935f757f3fSDimitry Andric 
4945f757f3fSDimitry Andric           // Check we don't exceed register pressure at the destination.
4955f757f3fSDimitry Andric           const MachineBasicBlock &MBB = *UseInst.getParent();
4965f757f3fSDimitry Andric           if (RCB == nullptr) {
4975f757f3fSDimitry Andric             if (registerPressureSetExceedsLimit(NRegs, RCA, MBB))
4985f757f3fSDimitry Andric               return false;
4995f757f3fSDimitry Andric           } else if (registerPressureSetExceedsLimit(1, RCA, MBB) ||
5005f757f3fSDimitry Andric                      registerPressureSetExceedsLimit(1, RCB, MBB)) {
5015f757f3fSDimitry Andric             return false;
5025f757f3fSDimitry Andric           }
5035f757f3fSDimitry Andric         }
5045f757f3fSDimitry Andric       }
5055f757f3fSDimitry Andric 
5065f757f3fSDimitry Andric       SinkInto.emplace_back(&UseInst, MaybeAM);
5075f757f3fSDimitry Andric     }
5085f757f3fSDimitry Andric   }
5095f757f3fSDimitry Andric 
5105f757f3fSDimitry Andric   if (SinkInto.empty())
5115f757f3fSDimitry Andric     return false;
5125f757f3fSDimitry Andric 
5135f757f3fSDimitry Andric   // Now we know we can fold the instruction in all its users.
5145f757f3fSDimitry Andric   for (auto &[SinkDst, MaybeAM] : SinkInto) {
5155f757f3fSDimitry Andric     MachineInstr *New = nullptr;
5165f757f3fSDimitry Andric     LLVM_DEBUG(dbgs() << "Sinking copy of"; MI.dump(); dbgs() << "into";
5175f757f3fSDimitry Andric                SinkDst->dump());
5185f757f3fSDimitry Andric     if (SinkDst->isCopy()) {
5195f757f3fSDimitry Andric       // TODO: After performing the sink-and-fold, the original instruction is
5205f757f3fSDimitry Andric       // deleted. Its value is still available (in a hard register), so if there
5215f757f3fSDimitry Andric       // are debug instructions which refer to the (now deleted) virtual
5225f757f3fSDimitry Andric       // register they could be updated to refer to the hard register, in
5235f757f3fSDimitry Andric       // principle. However, it's not clear how to do that, moreover in some
5245f757f3fSDimitry Andric       // cases the debug instructions may need to be replicated proportionally
5255f757f3fSDimitry Andric       // to the number of the COPY instructions replaced and in some extreme
5265f757f3fSDimitry Andric       // cases we can end up with quadratic increase in the number of debug
5275f757f3fSDimitry Andric       // instructions.
5285f757f3fSDimitry Andric 
5295f757f3fSDimitry Andric       // Sink a copy of the instruction, replacing a COPY instruction.
5305f757f3fSDimitry Andric       MachineBasicBlock::iterator InsertPt = SinkDst->getIterator();
5315f757f3fSDimitry Andric       Register DstReg = SinkDst->getOperand(0).getReg();
5325f757f3fSDimitry Andric       TII->reMaterialize(*SinkDst->getParent(), InsertPt, DstReg, 0, MI, *TRI);
5335f757f3fSDimitry Andric       New = &*std::prev(InsertPt);
5345f757f3fSDimitry Andric       if (!New->getDebugLoc())
5355f757f3fSDimitry Andric         New->setDebugLoc(SinkDst->getDebugLoc());
5365f757f3fSDimitry Andric 
5375f757f3fSDimitry Andric       // The operand registers of the "sunk" instruction have their live range
5385f757f3fSDimitry Andric       // extended and their kill flags may no longer be correct. Conservatively
5395f757f3fSDimitry Andric       // clear the kill flags.
5405f757f3fSDimitry Andric       if (UsedRegA)
5415f757f3fSDimitry Andric         MRI->clearKillFlags(UsedRegA);
5425f757f3fSDimitry Andric       if (UsedRegB)
5435f757f3fSDimitry Andric         MRI->clearKillFlags(UsedRegB);
5445f757f3fSDimitry Andric     } else {
5455f757f3fSDimitry Andric       // Fold instruction into the addressing mode of a memory instruction.
5465f757f3fSDimitry Andric       New = TII->emitLdStWithAddr(*SinkDst, MaybeAM);
5475f757f3fSDimitry Andric 
5485f757f3fSDimitry Andric       // The registers of the addressing mode may have their live range extended
5495f757f3fSDimitry Andric       // and their kill flags may no longer be correct. Conservatively clear the
5505f757f3fSDimitry Andric       // kill flags.
5515f757f3fSDimitry Andric       if (Register R = MaybeAM.BaseReg; R.isValid() && R.isVirtual())
5525f757f3fSDimitry Andric         MRI->clearKillFlags(R);
5535f757f3fSDimitry Andric       if (Register R = MaybeAM.ScaledReg; R.isValid() && R.isVirtual())
5545f757f3fSDimitry Andric         MRI->clearKillFlags(R);
5555f757f3fSDimitry Andric     }
5565f757f3fSDimitry Andric     LLVM_DEBUG(dbgs() << "yielding"; New->dump());
5575f757f3fSDimitry Andric     // Clear the StoreInstrCache, since we may invalidate it by erasing.
5585f757f3fSDimitry Andric     if (SinkDst->mayStore() && !SinkDst->hasOrderedMemoryRef())
5595f757f3fSDimitry Andric       StoreInstrCache.clear();
5605f757f3fSDimitry Andric     SinkDst->eraseFromParent();
5615f757f3fSDimitry Andric   }
5625f757f3fSDimitry Andric 
5635f757f3fSDimitry Andric   // Collect operands that need to be cleaned up because the registers no longer
5645f757f3fSDimitry Andric   // exist (in COPYs and debug instructions). We cannot delete instructions or
5655f757f3fSDimitry Andric   // clear operands while traversing register uses.
5665f757f3fSDimitry Andric   SmallVector<MachineOperand *> Cleanup;
5675f757f3fSDimitry Andric   Worklist.push_back(DefReg);
5685f757f3fSDimitry Andric   while (!Worklist.empty()) {
5695f757f3fSDimitry Andric     Register Reg = Worklist.pop_back_val();
5705f757f3fSDimitry Andric     for (MachineOperand &MO : MRI->use_operands(Reg)) {
5715f757f3fSDimitry Andric       MachineInstr *U = MO.getParent();
5725f757f3fSDimitry Andric       assert((U->isCopy() || U->isDebugInstr()) &&
5735f757f3fSDimitry Andric              "Only debug uses and copies must remain");
5745f757f3fSDimitry Andric       if (U->isCopy())
5755f757f3fSDimitry Andric         Worklist.push_back(U->getOperand(0).getReg());
5765f757f3fSDimitry Andric       Cleanup.push_back(&MO);
5775f757f3fSDimitry Andric     }
5785f757f3fSDimitry Andric   }
5795f757f3fSDimitry Andric 
5805f757f3fSDimitry Andric   // Delete the dead COPYs and clear operands in debug instructions
5815f757f3fSDimitry Andric   for (MachineOperand *MO : Cleanup) {
5825f757f3fSDimitry Andric     MachineInstr *I = MO->getParent();
5835f757f3fSDimitry Andric     if (I->isCopy()) {
5845f757f3fSDimitry Andric       I->eraseFromParent();
5855f757f3fSDimitry Andric     } else {
5865f757f3fSDimitry Andric       MO->setReg(0);
5875f757f3fSDimitry Andric       MO->setSubReg(0);
5885f757f3fSDimitry Andric     }
5895f757f3fSDimitry Andric   }
5905f757f3fSDimitry Andric 
5915f757f3fSDimitry Andric   MI.eraseFromParent();
5925f757f3fSDimitry Andric   return true;
5935f757f3fSDimitry Andric }
5945f757f3fSDimitry Andric 
5950b57cec5SDimitry Andric /// AllUsesDominatedByBlock - Return true if all uses of the specified register
5960b57cec5SDimitry Andric /// occur in blocks dominated by the specified block. If any use is in the
5970b57cec5SDimitry Andric /// definition block, then return false since it is never legal to move def
5980b57cec5SDimitry Andric /// after uses.
599e8d8bef9SDimitry Andric bool MachineSinking::AllUsesDominatedByBlock(Register Reg,
6000b57cec5SDimitry Andric                                              MachineBasicBlock *MBB,
6010b57cec5SDimitry Andric                                              MachineBasicBlock *DefMBB,
6020b57cec5SDimitry Andric                                              bool &BreakPHIEdge,
6030b57cec5SDimitry Andric                                              bool &LocalUse) const {
604bdd1243dSDimitry Andric   assert(Reg.isVirtual() && "Only makes sense for vregs");
6050b57cec5SDimitry Andric 
6060b57cec5SDimitry Andric   // Ignore debug uses because debug info doesn't affect the code.
6070b57cec5SDimitry Andric   if (MRI->use_nodbg_empty(Reg))
6080b57cec5SDimitry Andric     return true;
6090b57cec5SDimitry Andric 
6100b57cec5SDimitry Andric   // BreakPHIEdge is true if all the uses are in the successor MBB being sunken
6110b57cec5SDimitry Andric   // into and they are all PHI nodes. In this case, machine-sink must break
6120b57cec5SDimitry Andric   // the critical edge first. e.g.
6130b57cec5SDimitry Andric   //
614d65cd7a5SDimitry Andric   // %bb.1:
6150b57cec5SDimitry Andric   //   Predecessors according to CFG: %bb.0
6160b57cec5SDimitry Andric   //     ...
617d65cd7a5SDimitry Andric   //     %def = DEC64_32r %x, implicit-def dead %eflags
6180b57cec5SDimitry Andric   //     ...
6190b57cec5SDimitry Andric   //     JE_4 <%bb.37>, implicit %eflags
6200b57cec5SDimitry Andric   //   Successors according to CFG: %bb.37 %bb.2
6210b57cec5SDimitry Andric   //
622d65cd7a5SDimitry Andric   // %bb.2:
623d65cd7a5SDimitry Andric   //     %p = PHI %y, %bb.0, %def, %bb.1
6245ffd83dbSDimitry Andric   if (all_of(MRI->use_nodbg_operands(Reg), [&](MachineOperand &MO) {
6250b57cec5SDimitry Andric         MachineInstr *UseInst = MO.getParent();
62606c3fb27SDimitry Andric         unsigned OpNo = MO.getOperandNo();
6270b57cec5SDimitry Andric         MachineBasicBlock *UseBlock = UseInst->getParent();
628d65cd7a5SDimitry Andric         return UseBlock == MBB && UseInst->isPHI() &&
629d65cd7a5SDimitry Andric                UseInst->getOperand(OpNo + 1).getMBB() == DefMBB;
630d65cd7a5SDimitry Andric       })) {
631d65cd7a5SDimitry Andric     BreakPHIEdge = true;
6320b57cec5SDimitry Andric     return true;
633d65cd7a5SDimitry Andric   }
6340b57cec5SDimitry Andric 
6350b57cec5SDimitry Andric   for (MachineOperand &MO : MRI->use_nodbg_operands(Reg)) {
6360b57cec5SDimitry Andric     // Determine the block of the use.
6370b57cec5SDimitry Andric     MachineInstr *UseInst = MO.getParent();
6380b57cec5SDimitry Andric     unsigned OpNo = &MO - &UseInst->getOperand(0);
6390b57cec5SDimitry Andric     MachineBasicBlock *UseBlock = UseInst->getParent();
6400b57cec5SDimitry Andric     if (UseInst->isPHI()) {
6410b57cec5SDimitry Andric       // PHI nodes use the operand in the predecessor block, not the block with
6420b57cec5SDimitry Andric       // the PHI.
6430b57cec5SDimitry Andric       UseBlock = UseInst->getOperand(OpNo+1).getMBB();
6440b57cec5SDimitry Andric     } else if (UseBlock == DefMBB) {
6450b57cec5SDimitry Andric       LocalUse = true;
6460b57cec5SDimitry Andric       return false;
6470b57cec5SDimitry Andric     }
6480b57cec5SDimitry Andric 
6490b57cec5SDimitry Andric     // Check that it dominates.
6500b57cec5SDimitry Andric     if (!DT->dominates(MBB, UseBlock))
6510b57cec5SDimitry Andric       return false;
6520b57cec5SDimitry Andric   }
6530b57cec5SDimitry Andric 
6540b57cec5SDimitry Andric   return true;
6550b57cec5SDimitry Andric }
6560b57cec5SDimitry Andric 
657fe6060f1SDimitry Andric /// Return true if this machine instruction loads from global offset table or
658fe6060f1SDimitry Andric /// constant pool.
659fe6060f1SDimitry Andric static bool mayLoadFromGOTOrConstantPool(MachineInstr &MI) {
660fe6060f1SDimitry Andric   assert(MI.mayLoad() && "Expected MI that loads!");
661fe6060f1SDimitry Andric 
662fe6060f1SDimitry Andric   // If we lost memory operands, conservatively assume that the instruction
663fe6060f1SDimitry Andric   // reads from everything..
664fe6060f1SDimitry Andric   if (MI.memoperands_empty())
665fe6060f1SDimitry Andric     return true;
666fe6060f1SDimitry Andric 
667fe6060f1SDimitry Andric   for (MachineMemOperand *MemOp : MI.memoperands())
668fe6060f1SDimitry Andric     if (const PseudoSourceValue *PSV = MemOp->getPseudoValue())
669fe6060f1SDimitry Andric       if (PSV->isGOT() || PSV->isConstantPool())
670fe6060f1SDimitry Andric         return true;
671fe6060f1SDimitry Andric 
672fe6060f1SDimitry Andric   return false;
673fe6060f1SDimitry Andric }
674fe6060f1SDimitry Andric 
67581ad6265SDimitry Andric void MachineSinking::FindCycleSinkCandidates(
67681ad6265SDimitry Andric     MachineCycle *Cycle, MachineBasicBlock *BB,
677fe6060f1SDimitry Andric     SmallVectorImpl<MachineInstr *> &Candidates) {
678fe6060f1SDimitry Andric   for (auto &MI : *BB) {
67981ad6265SDimitry Andric     LLVM_DEBUG(dbgs() << "CycleSink: Analysing candidate: " << MI);
680fe6060f1SDimitry Andric     if (!TII->shouldSink(MI)) {
68181ad6265SDimitry Andric       LLVM_DEBUG(dbgs() << "CycleSink: Instruction not a candidate for this "
682fe6060f1SDimitry Andric                            "target\n");
683fe6060f1SDimitry Andric       continue;
684fe6060f1SDimitry Andric     }
68581ad6265SDimitry Andric     if (!isCycleInvariant(Cycle, MI)) {
68681ad6265SDimitry Andric       LLVM_DEBUG(dbgs() << "CycleSink: Instruction is not cycle invariant\n");
687fe6060f1SDimitry Andric       continue;
688fe6060f1SDimitry Andric     }
689fe6060f1SDimitry Andric     bool DontMoveAcrossStore = true;
690fe6060f1SDimitry Andric     if (!MI.isSafeToMove(AA, DontMoveAcrossStore)) {
69181ad6265SDimitry Andric       LLVM_DEBUG(dbgs() << "CycleSink: Instruction not safe to move.\n");
692fe6060f1SDimitry Andric       continue;
693fe6060f1SDimitry Andric     }
694fe6060f1SDimitry Andric     if (MI.mayLoad() && !mayLoadFromGOTOrConstantPool(MI)) {
69581ad6265SDimitry Andric       LLVM_DEBUG(dbgs() << "CycleSink: Dont sink GOT or constant pool loads\n");
696fe6060f1SDimitry Andric       continue;
697fe6060f1SDimitry Andric     }
698fe6060f1SDimitry Andric     if (MI.isConvergent())
699fe6060f1SDimitry Andric       continue;
700fe6060f1SDimitry Andric 
701fe6060f1SDimitry Andric     const MachineOperand &MO = MI.getOperand(0);
702fe6060f1SDimitry Andric     if (!MO.isReg() || !MO.getReg() || !MO.isDef())
703fe6060f1SDimitry Andric       continue;
704fe6060f1SDimitry Andric     if (!MRI->hasOneDef(MO.getReg()))
705fe6060f1SDimitry Andric       continue;
706fe6060f1SDimitry Andric 
70781ad6265SDimitry Andric     LLVM_DEBUG(dbgs() << "CycleSink: Instruction added as candidate.\n");
708fe6060f1SDimitry Andric     Candidates.push_back(&MI);
709fe6060f1SDimitry Andric   }
710fe6060f1SDimitry Andric }
711fe6060f1SDimitry Andric 
7120b57cec5SDimitry Andric bool MachineSinking::runOnMachineFunction(MachineFunction &MF) {
7130b57cec5SDimitry Andric   if (skipFunction(MF.getFunction()))
7140b57cec5SDimitry Andric     return false;
7150b57cec5SDimitry Andric 
7160b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "******** Machine Sinking ********\n");
7170b57cec5SDimitry Andric 
7185f757f3fSDimitry Andric   STI = &MF.getSubtarget();
7195f757f3fSDimitry Andric   TII = STI->getInstrInfo();
7205f757f3fSDimitry Andric   TRI = STI->getRegisterInfo();
7210b57cec5SDimitry Andric   MRI = &MF.getRegInfo();
722*0fca6ea1SDimitry Andric   DT = &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
723*0fca6ea1SDimitry Andric   PDT = &getAnalysis<MachinePostDominatorTreeWrapperPass>().getPostDomTree();
72481ad6265SDimitry Andric   CI = &getAnalysis<MachineCycleInfoWrapperPass>().getCycleInfo();
725*0fca6ea1SDimitry Andric   MBFI = UseBlockFreqInfo
726*0fca6ea1SDimitry Andric              ? &getAnalysis<MachineBlockFrequencyInfoWrapperPass>().getMBFI()
727*0fca6ea1SDimitry Andric              : nullptr;
728*0fca6ea1SDimitry Andric   MBPI = &getAnalysis<MachineBranchProbabilityInfoWrapperPass>().getMBPI();
7290b57cec5SDimitry Andric   AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
730e8d8bef9SDimitry Andric   RegClassInfo.runOnMachineFunction(MF);
7315f757f3fSDimitry Andric   TargetPassConfig *PassConfig = &getAnalysis<TargetPassConfig>();
7325f757f3fSDimitry Andric   EnableSinkAndFold = PassConfig->getEnableSinkAndFold();
7330b57cec5SDimitry Andric 
7340b57cec5SDimitry Andric   bool EverMadeChange = false;
7350b57cec5SDimitry Andric 
7360b57cec5SDimitry Andric   while (true) {
7370b57cec5SDimitry Andric     bool MadeChange = false;
7380b57cec5SDimitry Andric 
7390b57cec5SDimitry Andric     // Process all basic blocks.
7400b57cec5SDimitry Andric     CEBCandidates.clear();
741*0fca6ea1SDimitry Andric     CEMergeCandidates.clear();
7420b57cec5SDimitry Andric     ToSplit.clear();
7430b57cec5SDimitry Andric     for (auto &MBB: MF)
7440b57cec5SDimitry Andric       MadeChange |= ProcessBlock(MBB);
7450b57cec5SDimitry Andric 
7460b57cec5SDimitry Andric     // If we have anything we marked as toSplit, split it now.
747fcaf7f86SDimitry Andric     for (const auto &Pair : ToSplit) {
7480b57cec5SDimitry Andric       auto NewSucc = Pair.first->SplitCriticalEdge(Pair.second, *this);
7490b57cec5SDimitry Andric       if (NewSucc != nullptr) {
7500b57cec5SDimitry Andric         LLVM_DEBUG(dbgs() << " *** Splitting critical edge: "
7510b57cec5SDimitry Andric                           << printMBBReference(*Pair.first) << " -- "
7520b57cec5SDimitry Andric                           << printMBBReference(*NewSucc) << " -- "
7530b57cec5SDimitry Andric                           << printMBBReference(*Pair.second) << '\n');
754e8d8bef9SDimitry Andric         if (MBFI)
755e8d8bef9SDimitry Andric           MBFI->onEdgeSplit(*Pair.first, *NewSucc, *MBPI);
756e8d8bef9SDimitry Andric 
7570b57cec5SDimitry Andric         MadeChange = true;
7580b57cec5SDimitry Andric         ++NumSplit;
7595f757f3fSDimitry Andric         CI->splitCriticalEdge(Pair.first, Pair.second, NewSucc);
7600b57cec5SDimitry Andric       } else
7610b57cec5SDimitry Andric         LLVM_DEBUG(dbgs() << " *** Not legal to break critical edge\n");
7620b57cec5SDimitry Andric     }
7630b57cec5SDimitry Andric     // If this iteration over the code changed anything, keep iterating.
7640b57cec5SDimitry Andric     if (!MadeChange) break;
7650b57cec5SDimitry Andric     EverMadeChange = true;
7660b57cec5SDimitry Andric   }
7670b57cec5SDimitry Andric 
76881ad6265SDimitry Andric   if (SinkInstsIntoCycle) {
76981ad6265SDimitry Andric     SmallVector<MachineCycle *, 8> Cycles(CI->toplevel_begin(),
77081ad6265SDimitry Andric                                           CI->toplevel_end());
77181ad6265SDimitry Andric     for (auto *Cycle : Cycles) {
77281ad6265SDimitry Andric       MachineBasicBlock *Preheader = Cycle->getCyclePreheader();
773fe6060f1SDimitry Andric       if (!Preheader) {
77481ad6265SDimitry Andric         LLVM_DEBUG(dbgs() << "CycleSink: Can't find preheader\n");
775fe6060f1SDimitry Andric         continue;
776fe6060f1SDimitry Andric       }
777fe6060f1SDimitry Andric       SmallVector<MachineInstr *, 8> Candidates;
77881ad6265SDimitry Andric       FindCycleSinkCandidates(Cycle, Preheader, Candidates);
779fe6060f1SDimitry Andric 
780fe6060f1SDimitry Andric       // Walk the candidates in reverse order so that we start with the use
781fe6060f1SDimitry Andric       // of a def-use chain, if there is any.
782fe6060f1SDimitry Andric       // TODO: Sort the candidates using a cost-model.
783fe6060f1SDimitry Andric       unsigned i = 0;
784349cc55cSDimitry Andric       for (MachineInstr *I : llvm::reverse(Candidates)) {
78581ad6265SDimitry Andric         if (i++ == SinkIntoCycleLimit) {
78681ad6265SDimitry Andric           LLVM_DEBUG(dbgs() << "CycleSink:   Limit reached of instructions to "
787fe6060f1SDimitry Andric                                "be analysed.");
788fe6060f1SDimitry Andric           break;
789fe6060f1SDimitry Andric         }
790fe6060f1SDimitry Andric 
79181ad6265SDimitry Andric         if (!SinkIntoCycle(Cycle, *I))
792fe6060f1SDimitry Andric           break;
793fe6060f1SDimitry Andric         EverMadeChange = true;
79481ad6265SDimitry Andric         ++NumCycleSunk;
795fe6060f1SDimitry Andric       }
796fe6060f1SDimitry Andric     }
797fe6060f1SDimitry Andric   }
798fe6060f1SDimitry Andric 
799e8d8bef9SDimitry Andric   HasStoreCache.clear();
800e8d8bef9SDimitry Andric   StoreInstrCache.clear();
801e8d8bef9SDimitry Andric 
8020b57cec5SDimitry Andric   // Now clear any kill flags for recorded registers.
8030b57cec5SDimitry Andric   for (auto I : RegsToClearKillFlags)
8040b57cec5SDimitry Andric     MRI->clearKillFlags(I);
8050b57cec5SDimitry Andric   RegsToClearKillFlags.clear();
8060b57cec5SDimitry Andric 
8070b57cec5SDimitry Andric   return EverMadeChange;
8080b57cec5SDimitry Andric }
8090b57cec5SDimitry Andric 
8100b57cec5SDimitry Andric bool MachineSinking::ProcessBlock(MachineBasicBlock &MBB) {
8115f757f3fSDimitry Andric   if ((!EnableSinkAndFold && MBB.succ_size() <= 1) || MBB.empty())
8125f757f3fSDimitry Andric     return false;
8130b57cec5SDimitry Andric 
8140b57cec5SDimitry Andric   // Don't bother sinking code out of unreachable blocks. In addition to being
8150b57cec5SDimitry Andric   // unprofitable, it can also lead to infinite looping, because in an
81681ad6265SDimitry Andric   // unreachable cycle there may be nowhere to stop.
8170b57cec5SDimitry Andric   if (!DT->isReachableFromEntry(&MBB)) return false;
8180b57cec5SDimitry Andric 
8190b57cec5SDimitry Andric   bool MadeChange = false;
8200b57cec5SDimitry Andric 
82181ad6265SDimitry Andric   // Cache all successors, sorted by frequency info and cycle depth.
8220b57cec5SDimitry Andric   AllSuccsCache AllSuccessors;
8230b57cec5SDimitry Andric 
8240b57cec5SDimitry Andric   // Walk the basic block bottom-up.  Remember if we saw a store.
8250b57cec5SDimitry Andric   MachineBasicBlock::iterator I = MBB.end();
8260b57cec5SDimitry Andric   --I;
8270b57cec5SDimitry Andric   bool ProcessedBegin, SawStore = false;
8280b57cec5SDimitry Andric   do {
8290b57cec5SDimitry Andric     MachineInstr &MI = *I;  // The instruction to sink.
8300b57cec5SDimitry Andric 
8310b57cec5SDimitry Andric     // Predecrement I (if it's not begin) so that it isn't invalidated by
8320b57cec5SDimitry Andric     // sinking.
8330b57cec5SDimitry Andric     ProcessedBegin = I == MBB.begin();
8340b57cec5SDimitry Andric     if (!ProcessedBegin)
8350b57cec5SDimitry Andric       --I;
8360b57cec5SDimitry Andric 
837fe6060f1SDimitry Andric     if (MI.isDebugOrPseudoInstr()) {
838480093f4SDimitry Andric       if (MI.isDebugValue())
839480093f4SDimitry Andric         ProcessDbgInst(MI);
8400b57cec5SDimitry Andric       continue;
841480093f4SDimitry Andric     }
8420b57cec5SDimitry Andric 
8435f757f3fSDimitry Andric     if (EnableSinkAndFold && PerformSinkAndFold(MI, &MBB)) {
8445f757f3fSDimitry Andric       MadeChange = true;
8455f757f3fSDimitry Andric       continue;
8465f757f3fSDimitry Andric     }
8475f757f3fSDimitry Andric 
8485f757f3fSDimitry Andric     // Can't sink anything out of a block that has less than two successors.
8495f757f3fSDimitry Andric     if (MBB.succ_size() <= 1)
8505f757f3fSDimitry Andric       continue;
8515f757f3fSDimitry Andric 
8525f757f3fSDimitry Andric     if (PerformTrivialForwardCoalescing(MI, &MBB)) {
8530b57cec5SDimitry Andric       MadeChange = true;
8540b57cec5SDimitry Andric       continue;
8550b57cec5SDimitry Andric     }
8560b57cec5SDimitry Andric 
8570b57cec5SDimitry Andric     if (SinkInstruction(MI, SawStore, AllSuccessors)) {
8580b57cec5SDimitry Andric       ++NumSunk;
8590b57cec5SDimitry Andric       MadeChange = true;
8600b57cec5SDimitry Andric     }
8610b57cec5SDimitry Andric 
8620b57cec5SDimitry Andric     // If we just processed the first instruction in the block, we're done.
8630b57cec5SDimitry Andric   } while (!ProcessedBegin);
8640b57cec5SDimitry Andric 
865480093f4SDimitry Andric   SeenDbgUsers.clear();
866480093f4SDimitry Andric   SeenDbgVars.clear();
867e8d8bef9SDimitry Andric   // recalculate the bb register pressure after sinking one BB.
868e8d8bef9SDimitry Andric   CachedRegisterPressure.clear();
8690b57cec5SDimitry Andric   return MadeChange;
8700b57cec5SDimitry Andric }
8710b57cec5SDimitry Andric 
872480093f4SDimitry Andric void MachineSinking::ProcessDbgInst(MachineInstr &MI) {
873480093f4SDimitry Andric   // When we see DBG_VALUEs for registers, record any vreg it reads, so that
874480093f4SDimitry Andric   // we know what to sink if the vreg def sinks.
875480093f4SDimitry Andric   assert(MI.isDebugValue() && "Expected DBG_VALUE for processing");
876480093f4SDimitry Andric 
877480093f4SDimitry Andric   DebugVariable Var(MI.getDebugVariable(), MI.getDebugExpression(),
878480093f4SDimitry Andric                     MI.getDebugLoc()->getInlinedAt());
879e8d8bef9SDimitry Andric   bool SeenBefore = SeenDbgVars.contains(Var);
880480093f4SDimitry Andric 
881fe6060f1SDimitry Andric   for (MachineOperand &MO : MI.debug_operands()) {
882480093f4SDimitry Andric     if (MO.isReg() && MO.getReg().isVirtual())
883480093f4SDimitry Andric       SeenDbgUsers[MO.getReg()].push_back(SeenDbgUser(&MI, SeenBefore));
884fe6060f1SDimitry Andric   }
885480093f4SDimitry Andric 
886480093f4SDimitry Andric   // Record the variable for any DBG_VALUE, to avoid re-ordering any of them.
887480093f4SDimitry Andric   SeenDbgVars.insert(Var);
888480093f4SDimitry Andric }
889480093f4SDimitry Andric 
890*0fca6ea1SDimitry Andric bool MachineSinking::isWorthBreakingCriticalEdge(
891*0fca6ea1SDimitry Andric     MachineInstr &MI, MachineBasicBlock *From, MachineBasicBlock *To,
892*0fca6ea1SDimitry Andric     MachineBasicBlock *&DeferredFromBlock) {
8930b57cec5SDimitry Andric   // FIXME: Need much better heuristics.
8940b57cec5SDimitry Andric 
8950b57cec5SDimitry Andric   // If the pass has already considered breaking this edge (during this pass
8960b57cec5SDimitry Andric   // through the function), then let's go ahead and break it. This means
8970b57cec5SDimitry Andric   // sinking multiple "cheap" instructions into the same block.
8980b57cec5SDimitry Andric   if (!CEBCandidates.insert(std::make_pair(From, To)).second)
8990b57cec5SDimitry Andric     return true;
9000b57cec5SDimitry Andric 
9010b57cec5SDimitry Andric   if (!MI.isCopy() && !TII->isAsCheapAsAMove(MI))
9020b57cec5SDimitry Andric     return true;
9030b57cec5SDimitry Andric 
904*0fca6ea1SDimitry Andric   // Check and record the register and the destination block we want to sink
905*0fca6ea1SDimitry Andric   // into. Note that we want to do the following before the next check on branch
906*0fca6ea1SDimitry Andric   // probability. Because we want to record the initial candidate even if it's
907*0fca6ea1SDimitry Andric   // on hot edge, so that other candidates that might not on hot edges can be
908*0fca6ea1SDimitry Andric   // sinked as well.
909*0fca6ea1SDimitry Andric   for (const auto &MO : MI.all_defs()) {
910*0fca6ea1SDimitry Andric     Register Reg = MO.getReg();
911*0fca6ea1SDimitry Andric     if (!Reg)
912*0fca6ea1SDimitry Andric       continue;
913*0fca6ea1SDimitry Andric     Register SrcReg = Reg.isVirtual() ? TRI->lookThruCopyLike(Reg, MRI) : Reg;
914*0fca6ea1SDimitry Andric     auto Key = std::make_pair(SrcReg, To);
915*0fca6ea1SDimitry Andric     auto Res = CEMergeCandidates.try_emplace(Key, From);
916*0fca6ea1SDimitry Andric     // We wanted to sink the same register into the same block, consider it to
917*0fca6ea1SDimitry Andric     // be profitable.
918*0fca6ea1SDimitry Andric     if (!Res.second) {
919*0fca6ea1SDimitry Andric       // Return the source block that was previously held off.
920*0fca6ea1SDimitry Andric       DeferredFromBlock = Res.first->second;
921*0fca6ea1SDimitry Andric       return true;
922*0fca6ea1SDimitry Andric     }
923*0fca6ea1SDimitry Andric   }
924*0fca6ea1SDimitry Andric 
9250b57cec5SDimitry Andric   if (From->isSuccessor(To) && MBPI->getEdgeProbability(From, To) <=
9260b57cec5SDimitry Andric       BranchProbability(SplitEdgeProbabilityThreshold, 100))
9270b57cec5SDimitry Andric     return true;
9280b57cec5SDimitry Andric 
9290b57cec5SDimitry Andric   // MI is cheap, we probably don't want to break the critical edge for it.
9300b57cec5SDimitry Andric   // However, if this would allow some definitions of its source operands
9310b57cec5SDimitry Andric   // to be sunk then it's probably worth it.
93206c3fb27SDimitry Andric   for (const MachineOperand &MO : MI.all_uses()) {
9338bcb0991SDimitry Andric     Register Reg = MO.getReg();
9340b57cec5SDimitry Andric     if (Reg == 0)
9350b57cec5SDimitry Andric       continue;
9360b57cec5SDimitry Andric 
9370b57cec5SDimitry Andric     // We don't move live definitions of physical registers,
9380b57cec5SDimitry Andric     // so sinking their uses won't enable any opportunities.
939bdd1243dSDimitry Andric     if (Reg.isPhysical())
9400b57cec5SDimitry Andric       continue;
9410b57cec5SDimitry Andric 
9420b57cec5SDimitry Andric     // If this instruction is the only user of a virtual register,
9430b57cec5SDimitry Andric     // check if breaking the edge will enable sinking
9440b57cec5SDimitry Andric     // both this instruction and the defining instruction.
9450b57cec5SDimitry Andric     if (MRI->hasOneNonDBGUse(Reg)) {
9460b57cec5SDimitry Andric       // If the definition resides in same MBB,
9470b57cec5SDimitry Andric       // claim it's likely we can sink these together.
9480b57cec5SDimitry Andric       // If definition resides elsewhere, we aren't
9490b57cec5SDimitry Andric       // blocking it from being sunk so don't break the edge.
9500b57cec5SDimitry Andric       MachineInstr *DefMI = MRI->getVRegDef(Reg);
9510b57cec5SDimitry Andric       if (DefMI->getParent() == MI.getParent())
9520b57cec5SDimitry Andric         return true;
9530b57cec5SDimitry Andric     }
9540b57cec5SDimitry Andric   }
9550b57cec5SDimitry Andric 
9560b57cec5SDimitry Andric   return false;
9570b57cec5SDimitry Andric }
9580b57cec5SDimitry Andric 
959*0fca6ea1SDimitry Andric bool MachineSinking::isLegalToBreakCriticalEdge(MachineInstr &MI,
9600b57cec5SDimitry Andric                                                 MachineBasicBlock *FromBB,
9610b57cec5SDimitry Andric                                                 MachineBasicBlock *ToBB,
9620b57cec5SDimitry Andric                                                 bool BreakPHIEdge) {
96381ad6265SDimitry Andric   // Avoid breaking back edge. From == To means backedge for single BB cycle.
964*0fca6ea1SDimitry Andric   if (!SplitEdges || FromBB == ToBB || !FromBB->isSuccessor(ToBB))
9650b57cec5SDimitry Andric     return false;
9660b57cec5SDimitry Andric 
96781ad6265SDimitry Andric   MachineCycle *FromCycle = CI->getCycle(FromBB);
96881ad6265SDimitry Andric   MachineCycle *ToCycle = CI->getCycle(ToBB);
96981ad6265SDimitry Andric 
97081ad6265SDimitry Andric   // Check for backedges of more "complex" cycles.
97181ad6265SDimitry Andric   if (FromCycle == ToCycle && FromCycle &&
97281ad6265SDimitry Andric       (!FromCycle->isReducible() || FromCycle->getHeader() == ToBB))
9730b57cec5SDimitry Andric     return false;
9740b57cec5SDimitry Andric 
9750b57cec5SDimitry Andric   // It's not always legal to break critical edges and sink the computation
9760b57cec5SDimitry Andric   // to the edge.
9770b57cec5SDimitry Andric   //
9780b57cec5SDimitry Andric   // %bb.1:
9790b57cec5SDimitry Andric   // v1024
9800b57cec5SDimitry Andric   // Beq %bb.3
9810b57cec5SDimitry Andric   // <fallthrough>
9820b57cec5SDimitry Andric   // %bb.2:
9830b57cec5SDimitry Andric   // ... no uses of v1024
9840b57cec5SDimitry Andric   // <fallthrough>
9850b57cec5SDimitry Andric   // %bb.3:
9860b57cec5SDimitry Andric   // ...
9870b57cec5SDimitry Andric   //       = v1024
9880b57cec5SDimitry Andric   //
9890b57cec5SDimitry Andric   // If %bb.1 -> %bb.3 edge is broken and computation of v1024 is inserted:
9900b57cec5SDimitry Andric   //
9910b57cec5SDimitry Andric   // %bb.1:
9920b57cec5SDimitry Andric   // ...
9930b57cec5SDimitry Andric   // Bne %bb.2
9940b57cec5SDimitry Andric   // %bb.4:
9950b57cec5SDimitry Andric   // v1024 =
9960b57cec5SDimitry Andric   // B %bb.3
9970b57cec5SDimitry Andric   // %bb.2:
9980b57cec5SDimitry Andric   // ... no uses of v1024
9990b57cec5SDimitry Andric   // <fallthrough>
10000b57cec5SDimitry Andric   // %bb.3:
10010b57cec5SDimitry Andric   // ...
10020b57cec5SDimitry Andric   //       = v1024
10030b57cec5SDimitry Andric   //
10040b57cec5SDimitry Andric   // This is incorrect since v1024 is not computed along the %bb.1->%bb.2->%bb.3
10050b57cec5SDimitry Andric   // flow. We need to ensure the new basic block where the computation is
10060b57cec5SDimitry Andric   // sunk to dominates all the uses.
10070b57cec5SDimitry Andric   // It's only legal to break critical edge and sink the computation to the
10080b57cec5SDimitry Andric   // new block if all the predecessors of "To", except for "From", are
10090b57cec5SDimitry Andric   // not dominated by "From". Given SSA property, this means these
10100b57cec5SDimitry Andric   // predecessors are dominated by "To".
10110b57cec5SDimitry Andric   //
10120b57cec5SDimitry Andric   // There is no need to do this check if all the uses are PHI nodes. PHI
10130b57cec5SDimitry Andric   // sources are only defined on the specific predecessor edges.
10140b57cec5SDimitry Andric   if (!BreakPHIEdge) {
1015349cc55cSDimitry Andric     for (MachineBasicBlock *Pred : ToBB->predecessors())
1016349cc55cSDimitry Andric       if (Pred != FromBB && !DT->dominates(ToBB, Pred))
10170b57cec5SDimitry Andric         return false;
10180b57cec5SDimitry Andric   }
10190b57cec5SDimitry Andric 
10200b57cec5SDimitry Andric   return true;
10210b57cec5SDimitry Andric }
10220b57cec5SDimitry Andric 
1023*0fca6ea1SDimitry Andric bool MachineSinking::PostponeSplitCriticalEdge(MachineInstr &MI,
1024*0fca6ea1SDimitry Andric                                                MachineBasicBlock *FromBB,
1025*0fca6ea1SDimitry Andric                                                MachineBasicBlock *ToBB,
1026*0fca6ea1SDimitry Andric                                                bool BreakPHIEdge) {
1027*0fca6ea1SDimitry Andric   bool Status = false;
1028*0fca6ea1SDimitry Andric   MachineBasicBlock *DeferredFromBB = nullptr;
1029*0fca6ea1SDimitry Andric   if (isWorthBreakingCriticalEdge(MI, FromBB, ToBB, DeferredFromBB)) {
1030*0fca6ea1SDimitry Andric     // If there is a DeferredFromBB, we consider FromBB only if _both_
1031*0fca6ea1SDimitry Andric     // of them are legal to split.
1032*0fca6ea1SDimitry Andric     if ((!DeferredFromBB ||
1033*0fca6ea1SDimitry Andric          ToSplit.count(std::make_pair(DeferredFromBB, ToBB)) ||
1034*0fca6ea1SDimitry Andric          isLegalToBreakCriticalEdge(MI, DeferredFromBB, ToBB, BreakPHIEdge)) &&
1035*0fca6ea1SDimitry Andric         isLegalToBreakCriticalEdge(MI, FromBB, ToBB, BreakPHIEdge)) {
1036*0fca6ea1SDimitry Andric       ToSplit.insert(std::make_pair(FromBB, ToBB));
1037*0fca6ea1SDimitry Andric       if (DeferredFromBB)
1038*0fca6ea1SDimitry Andric         ToSplit.insert(std::make_pair(DeferredFromBB, ToBB));
1039*0fca6ea1SDimitry Andric       Status = true;
1040*0fca6ea1SDimitry Andric     }
1041*0fca6ea1SDimitry Andric   }
1042*0fca6ea1SDimitry Andric 
1043*0fca6ea1SDimitry Andric   return Status;
1044*0fca6ea1SDimitry Andric }
1045*0fca6ea1SDimitry Andric 
1046e8d8bef9SDimitry Andric std::vector<unsigned> &
10475f757f3fSDimitry Andric MachineSinking::getBBRegisterPressure(const MachineBasicBlock &MBB) {
1048e8d8bef9SDimitry Andric   // Currently to save compiling time, MBB's register pressure will not change
1049e8d8bef9SDimitry Andric   // in one ProcessBlock iteration because of CachedRegisterPressure. but MBB's
1050e8d8bef9SDimitry Andric   // register pressure is changed after sinking any instructions into it.
1051e8d8bef9SDimitry Andric   // FIXME: need a accurate and cheap register pressure estiminate model here.
1052e8d8bef9SDimitry Andric   auto RP = CachedRegisterPressure.find(&MBB);
1053e8d8bef9SDimitry Andric   if (RP != CachedRegisterPressure.end())
1054e8d8bef9SDimitry Andric     return RP->second;
1055e8d8bef9SDimitry Andric 
1056e8d8bef9SDimitry Andric   RegionPressure Pressure;
1057e8d8bef9SDimitry Andric   RegPressureTracker RPTracker(Pressure);
1058e8d8bef9SDimitry Andric 
1059e8d8bef9SDimitry Andric   // Initialize the register pressure tracker.
1060e8d8bef9SDimitry Andric   RPTracker.init(MBB.getParent(), &RegClassInfo, nullptr, &MBB, MBB.end(),
1061e8d8bef9SDimitry Andric                  /*TrackLaneMasks*/ false, /*TrackUntiedDefs=*/true);
1062e8d8bef9SDimitry Andric 
10635f757f3fSDimitry Andric   for (MachineBasicBlock::const_iterator MII = MBB.instr_end(),
1064e8d8bef9SDimitry Andric                                          MIE = MBB.instr_begin();
1065e8d8bef9SDimitry Andric        MII != MIE; --MII) {
10665f757f3fSDimitry Andric     const MachineInstr &MI = *std::prev(MII);
1067fe6060f1SDimitry Andric     if (MI.isDebugInstr() || MI.isPseudoProbe())
1068e8d8bef9SDimitry Andric       continue;
1069e8d8bef9SDimitry Andric     RegisterOperands RegOpers;
1070e8d8bef9SDimitry Andric     RegOpers.collect(MI, *TRI, *MRI, false, false);
1071e8d8bef9SDimitry Andric     RPTracker.recedeSkipDebugValues();
1072e8d8bef9SDimitry Andric     assert(&*RPTracker.getPos() == &MI && "RPTracker sync error!");
1073e8d8bef9SDimitry Andric     RPTracker.recede(RegOpers);
1074e8d8bef9SDimitry Andric   }
1075e8d8bef9SDimitry Andric 
1076e8d8bef9SDimitry Andric   RPTracker.closeRegion();
1077e8d8bef9SDimitry Andric   auto It = CachedRegisterPressure.insert(
1078e8d8bef9SDimitry Andric       std::make_pair(&MBB, RPTracker.getPressure().MaxSetPressure));
1079e8d8bef9SDimitry Andric   return It.first->second;
1080e8d8bef9SDimitry Andric }
1081e8d8bef9SDimitry Andric 
10825f757f3fSDimitry Andric bool MachineSinking::registerPressureSetExceedsLimit(
10835f757f3fSDimitry Andric     unsigned NRegs, const TargetRegisterClass *RC,
10845f757f3fSDimitry Andric     const MachineBasicBlock &MBB) {
10855f757f3fSDimitry Andric   unsigned Weight = NRegs * TRI->getRegClassWeight(RC).RegWeight;
10865f757f3fSDimitry Andric   const int *PS = TRI->getRegClassPressureSets(RC);
10875f757f3fSDimitry Andric   std::vector<unsigned> BBRegisterPressure = getBBRegisterPressure(MBB);
10885f757f3fSDimitry Andric   for (; *PS != -1; PS++)
10895f757f3fSDimitry Andric     if (Weight + BBRegisterPressure[*PS] >=
10905f757f3fSDimitry Andric         TRI->getRegPressureSetLimit(*MBB.getParent(), *PS))
10915f757f3fSDimitry Andric       return true;
10925f757f3fSDimitry Andric   return false;
10935f757f3fSDimitry Andric }
10945f757f3fSDimitry Andric 
10950b57cec5SDimitry Andric /// isProfitableToSinkTo - Return true if it is profitable to sink MI.
1096e8d8bef9SDimitry Andric bool MachineSinking::isProfitableToSinkTo(Register Reg, MachineInstr &MI,
10970b57cec5SDimitry Andric                                           MachineBasicBlock *MBB,
10980b57cec5SDimitry Andric                                           MachineBasicBlock *SuccToSinkTo,
10990b57cec5SDimitry Andric                                           AllSuccsCache &AllSuccessors) {
11000b57cec5SDimitry Andric   assert (SuccToSinkTo && "Invalid SinkTo Candidate BB");
11010b57cec5SDimitry Andric 
11020b57cec5SDimitry Andric   if (MBB == SuccToSinkTo)
11030b57cec5SDimitry Andric     return false;
11040b57cec5SDimitry Andric 
11050b57cec5SDimitry Andric   // It is profitable if SuccToSinkTo does not post dominate current block.
11060b57cec5SDimitry Andric   if (!PDT->dominates(SuccToSinkTo, MBB))
11070b57cec5SDimitry Andric     return true;
11080b57cec5SDimitry Andric 
110981ad6265SDimitry Andric   // It is profitable to sink an instruction from a deeper cycle to a shallower
111081ad6265SDimitry Andric   // cycle, even if the latter post-dominates the former (PR21115).
111181ad6265SDimitry Andric   if (CI->getCycleDepth(MBB) > CI->getCycleDepth(SuccToSinkTo))
11120b57cec5SDimitry Andric     return true;
11130b57cec5SDimitry Andric 
11140b57cec5SDimitry Andric   // Check if only use in post dominated block is PHI instruction.
11150b57cec5SDimitry Andric   bool NonPHIUse = false;
11160b57cec5SDimitry Andric   for (MachineInstr &UseInst : MRI->use_nodbg_instructions(Reg)) {
11170b57cec5SDimitry Andric     MachineBasicBlock *UseBlock = UseInst.getParent();
11180b57cec5SDimitry Andric     if (UseBlock == SuccToSinkTo && !UseInst.isPHI())
11190b57cec5SDimitry Andric       NonPHIUse = true;
11200b57cec5SDimitry Andric   }
11210b57cec5SDimitry Andric   if (!NonPHIUse)
11220b57cec5SDimitry Andric     return true;
11230b57cec5SDimitry Andric 
11240b57cec5SDimitry Andric   // If SuccToSinkTo post dominates then also it may be profitable if MI
11250b57cec5SDimitry Andric   // can further profitably sinked into another block in next round.
11260b57cec5SDimitry Andric   bool BreakPHIEdge = false;
11270b57cec5SDimitry Andric   // FIXME - If finding successor is compile time expensive then cache results.
11280b57cec5SDimitry Andric   if (MachineBasicBlock *MBB2 =
11290b57cec5SDimitry Andric           FindSuccToSinkTo(MI, SuccToSinkTo, BreakPHIEdge, AllSuccessors))
11300b57cec5SDimitry Andric     return isProfitableToSinkTo(Reg, MI, SuccToSinkTo, MBB2, AllSuccessors);
11310b57cec5SDimitry Andric 
113281ad6265SDimitry Andric   MachineCycle *MCycle = CI->getCycle(MBB);
1133e8d8bef9SDimitry Andric 
113481ad6265SDimitry Andric   // If the instruction is not inside a cycle, it is not profitable to sink MI to
1135e8d8bef9SDimitry Andric   // a post dominate block SuccToSinkTo.
113681ad6265SDimitry Andric   if (!MCycle)
11370b57cec5SDimitry Andric     return false;
1138e8d8bef9SDimitry Andric 
113981ad6265SDimitry Andric   // If this instruction is inside a Cycle and sinking this instruction can make
1140e8d8bef9SDimitry Andric   // more registers live range shorten, it is still prifitable.
11414824e7fdSDimitry Andric   for (const MachineOperand &MO : MI.operands()) {
1142e8d8bef9SDimitry Andric     // Ignore non-register operands.
1143e8d8bef9SDimitry Andric     if (!MO.isReg())
1144e8d8bef9SDimitry Andric       continue;
1145e8d8bef9SDimitry Andric     Register Reg = MO.getReg();
1146e8d8bef9SDimitry Andric     if (Reg == 0)
1147e8d8bef9SDimitry Andric       continue;
1148e8d8bef9SDimitry Andric 
1149bdd1243dSDimitry Andric     if (Reg.isPhysical()) {
115006c3fb27SDimitry Andric       // Don't handle non-constant and non-ignorable physical register uses.
115106c3fb27SDimitry Andric       if (MO.isUse() && !MRI->isConstantPhysReg(Reg) && !TII->isIgnorableUse(MO))
1152e8d8bef9SDimitry Andric         return false;
115306c3fb27SDimitry Andric       continue;
115404eeddc0SDimitry Andric     }
1155e8d8bef9SDimitry Andric 
1156e8d8bef9SDimitry Andric     // Users for the defs are all dominated by SuccToSinkTo.
1157e8d8bef9SDimitry Andric     if (MO.isDef()) {
1158e8d8bef9SDimitry Andric       // This def register's live range is shortened after sinking.
1159e8d8bef9SDimitry Andric       bool LocalUse = false;
1160e8d8bef9SDimitry Andric       if (!AllUsesDominatedByBlock(Reg, SuccToSinkTo, MBB, BreakPHIEdge,
1161e8d8bef9SDimitry Andric                                    LocalUse))
1162e8d8bef9SDimitry Andric         return false;
1163e8d8bef9SDimitry Andric     } else {
1164e8d8bef9SDimitry Andric       MachineInstr *DefMI = MRI->getVRegDef(Reg);
116581ad6265SDimitry Andric       if (!DefMI)
1166e8d8bef9SDimitry Andric         continue;
116781ad6265SDimitry Andric       MachineCycle *Cycle = CI->getCycle(DefMI->getParent());
116881ad6265SDimitry Andric       // DefMI is defined outside of cycle. There should be no live range
116981ad6265SDimitry Andric       // impact for this operand. Defination outside of cycle means:
117081ad6265SDimitry Andric       // 1: defination is outside of cycle.
117181ad6265SDimitry Andric       // 2: defination is in this cycle, but it is a PHI in the cycle header.
117281ad6265SDimitry Andric       if (Cycle != MCycle || (DefMI->isPHI() && Cycle && Cycle->isReducible() &&
117381ad6265SDimitry Andric                               Cycle->getHeader() == DefMI->getParent()))
117481ad6265SDimitry Andric         continue;
117581ad6265SDimitry Andric       // The DefMI is defined inside the cycle.
1176e8d8bef9SDimitry Andric       // If sinking this operand makes some register pressure set exceed limit,
1177e8d8bef9SDimitry Andric       // it is not profitable.
11785f757f3fSDimitry Andric       if (registerPressureSetExceedsLimit(1, MRI->getRegClass(Reg),
11795f757f3fSDimitry Andric                                           *SuccToSinkTo)) {
1180e8d8bef9SDimitry Andric         LLVM_DEBUG(dbgs() << "register pressure exceed limit, not profitable.");
1181e8d8bef9SDimitry Andric         return false;
1182e8d8bef9SDimitry Andric       }
1183e8d8bef9SDimitry Andric     }
1184e8d8bef9SDimitry Andric   }
1185e8d8bef9SDimitry Andric 
118681ad6265SDimitry Andric   // If MI is in cycle and all its operands are alive across the whole cycle or
118781ad6265SDimitry Andric   // if no operand sinking make register pressure set exceed limit, it is
1188e8d8bef9SDimitry Andric   // profitable to sink MI.
1189e8d8bef9SDimitry Andric   return true;
11900b57cec5SDimitry Andric }
11910b57cec5SDimitry Andric 
11920b57cec5SDimitry Andric /// Get the sorted sequence of successors for this MachineBasicBlock, possibly
11930b57cec5SDimitry Andric /// computing it if it was not already cached.
11940b57cec5SDimitry Andric SmallVector<MachineBasicBlock *, 4> &
11950b57cec5SDimitry Andric MachineSinking::GetAllSortedSuccessors(MachineInstr &MI, MachineBasicBlock *MBB,
11960b57cec5SDimitry Andric                                        AllSuccsCache &AllSuccessors) const {
11970b57cec5SDimitry Andric   // Do we have the sorted successors in cache ?
11980b57cec5SDimitry Andric   auto Succs = AllSuccessors.find(MBB);
11990b57cec5SDimitry Andric   if (Succs != AllSuccessors.end())
12000b57cec5SDimitry Andric     return Succs->second;
12010b57cec5SDimitry Andric 
1202e8d8bef9SDimitry Andric   SmallVector<MachineBasicBlock *, 4> AllSuccs(MBB->successors());
12030b57cec5SDimitry Andric 
12040b57cec5SDimitry Andric   // Handle cases where sinking can happen but where the sink point isn't a
12050b57cec5SDimitry Andric   // successor. For example:
12060b57cec5SDimitry Andric   //
12070b57cec5SDimitry Andric   //   x = computation
12080b57cec5SDimitry Andric   //   if () {} else {}
12090b57cec5SDimitry Andric   //   use x
12100b57cec5SDimitry Andric   //
12115ffd83dbSDimitry Andric   for (MachineDomTreeNode *DTChild : DT->getNode(MBB)->children()) {
12120b57cec5SDimitry Andric     // DomTree children of MBB that have MBB as immediate dominator are added.
12130b57cec5SDimitry Andric     if (DTChild->getIDom()->getBlock() == MI.getParent() &&
12140b57cec5SDimitry Andric         // Skip MBBs already added to the AllSuccs vector above.
12150b57cec5SDimitry Andric         !MBB->isSuccessor(DTChild->getBlock()))
12160b57cec5SDimitry Andric       AllSuccs.push_back(DTChild->getBlock());
12175ffd83dbSDimitry Andric   }
12180b57cec5SDimitry Andric 
121981ad6265SDimitry Andric   // Sort Successors according to their cycle depth or block frequency info.
12200b57cec5SDimitry Andric   llvm::stable_sort(
12210b57cec5SDimitry Andric       AllSuccs, [this](const MachineBasicBlock *L, const MachineBasicBlock *R) {
12220b57cec5SDimitry Andric         uint64_t LHSFreq = MBFI ? MBFI->getBlockFreq(L).getFrequency() : 0;
12230b57cec5SDimitry Andric         uint64_t RHSFreq = MBFI ? MBFI->getBlockFreq(R).getFrequency() : 0;
12245f757f3fSDimitry Andric         bool HasBlockFreq = LHSFreq != 0 || RHSFreq != 0;
12250b57cec5SDimitry Andric         return HasBlockFreq ? LHSFreq < RHSFreq
122681ad6265SDimitry Andric                             : CI->getCycleDepth(L) < CI->getCycleDepth(R);
12270b57cec5SDimitry Andric       });
12280b57cec5SDimitry Andric 
12290b57cec5SDimitry Andric   auto it = AllSuccessors.insert(std::make_pair(MBB, AllSuccs));
12300b57cec5SDimitry Andric 
12310b57cec5SDimitry Andric   return it.first->second;
12320b57cec5SDimitry Andric }
12330b57cec5SDimitry Andric 
12340b57cec5SDimitry Andric /// FindSuccToSinkTo - Find a successor to sink this instruction to.
12350b57cec5SDimitry Andric MachineBasicBlock *
12360b57cec5SDimitry Andric MachineSinking::FindSuccToSinkTo(MachineInstr &MI, MachineBasicBlock *MBB,
12370b57cec5SDimitry Andric                                  bool &BreakPHIEdge,
12380b57cec5SDimitry Andric                                  AllSuccsCache &AllSuccessors) {
12390b57cec5SDimitry Andric   assert (MBB && "Invalid MachineBasicBlock!");
12400b57cec5SDimitry Andric 
124181ad6265SDimitry Andric   // loop over all the operands of the specified instruction.  If there is
12420b57cec5SDimitry Andric   // anything we can't handle, bail out.
12430b57cec5SDimitry Andric 
12440b57cec5SDimitry Andric   // SuccToSinkTo - This is the successor to sink this instruction to, once we
12450b57cec5SDimitry Andric   // decide.
12460b57cec5SDimitry Andric   MachineBasicBlock *SuccToSinkTo = nullptr;
12474824e7fdSDimitry Andric   for (const MachineOperand &MO : MI.operands()) {
12480b57cec5SDimitry Andric     if (!MO.isReg()) continue;  // Ignore non-register operands.
12490b57cec5SDimitry Andric 
12508bcb0991SDimitry Andric     Register Reg = MO.getReg();
12510b57cec5SDimitry Andric     if (Reg == 0) continue;
12520b57cec5SDimitry Andric 
1253bdd1243dSDimitry Andric     if (Reg.isPhysical()) {
12540b57cec5SDimitry Andric       if (MO.isUse()) {
12550b57cec5SDimitry Andric         // If the physreg has no defs anywhere, it's just an ambient register
12560b57cec5SDimitry Andric         // and we can freely move its uses. Alternatively, if it's allocatable,
12570b57cec5SDimitry Andric         // it could get allocated to something with a def during allocation.
125804eeddc0SDimitry Andric         if (!MRI->isConstantPhysReg(Reg) && !TII->isIgnorableUse(MO))
12590b57cec5SDimitry Andric           return nullptr;
12600b57cec5SDimitry Andric       } else if (!MO.isDead()) {
12610b57cec5SDimitry Andric         // A def that isn't dead. We can't move it.
12620b57cec5SDimitry Andric         return nullptr;
12630b57cec5SDimitry Andric       }
12640b57cec5SDimitry Andric     } else {
12650b57cec5SDimitry Andric       // Virtual register uses are always safe to sink.
12660b57cec5SDimitry Andric       if (MO.isUse()) continue;
12670b57cec5SDimitry Andric 
12680b57cec5SDimitry Andric       // If it's not safe to move defs of the register class, then abort.
12690b57cec5SDimitry Andric       if (!TII->isSafeToMoveRegClassDefs(MRI->getRegClass(Reg)))
12700b57cec5SDimitry Andric         return nullptr;
12710b57cec5SDimitry Andric 
12720b57cec5SDimitry Andric       // Virtual register defs can only be sunk if all their uses are in blocks
12730b57cec5SDimitry Andric       // dominated by one of the successors.
12740b57cec5SDimitry Andric       if (SuccToSinkTo) {
12750b57cec5SDimitry Andric         // If a previous operand picked a block to sink to, then this operand
12760b57cec5SDimitry Andric         // must be sinkable to the same block.
12770b57cec5SDimitry Andric         bool LocalUse = false;
12780b57cec5SDimitry Andric         if (!AllUsesDominatedByBlock(Reg, SuccToSinkTo, MBB,
12790b57cec5SDimitry Andric                                      BreakPHIEdge, LocalUse))
12800b57cec5SDimitry Andric           return nullptr;
12810b57cec5SDimitry Andric 
12820b57cec5SDimitry Andric         continue;
12830b57cec5SDimitry Andric       }
12840b57cec5SDimitry Andric 
12850b57cec5SDimitry Andric       // Otherwise, we should look at all the successors and decide which one
12860b57cec5SDimitry Andric       // we should sink to. If we have reliable block frequency information
12870b57cec5SDimitry Andric       // (frequency != 0) available, give successors with smaller frequencies
128881ad6265SDimitry Andric       // higher priority, otherwise prioritize smaller cycle depths.
12890b57cec5SDimitry Andric       for (MachineBasicBlock *SuccBlock :
12900b57cec5SDimitry Andric            GetAllSortedSuccessors(MI, MBB, AllSuccessors)) {
12910b57cec5SDimitry Andric         bool LocalUse = false;
12920b57cec5SDimitry Andric         if (AllUsesDominatedByBlock(Reg, SuccBlock, MBB,
12930b57cec5SDimitry Andric                                     BreakPHIEdge, LocalUse)) {
12940b57cec5SDimitry Andric           SuccToSinkTo = SuccBlock;
12950b57cec5SDimitry Andric           break;
12960b57cec5SDimitry Andric         }
12970b57cec5SDimitry Andric         if (LocalUse)
12980b57cec5SDimitry Andric           // Def is used locally, it's never safe to move this def.
12990b57cec5SDimitry Andric           return nullptr;
13000b57cec5SDimitry Andric       }
13010b57cec5SDimitry Andric 
13020b57cec5SDimitry Andric       // If we couldn't find a block to sink to, ignore this instruction.
13030b57cec5SDimitry Andric       if (!SuccToSinkTo)
13040b57cec5SDimitry Andric         return nullptr;
13050b57cec5SDimitry Andric       if (!isProfitableToSinkTo(Reg, MI, MBB, SuccToSinkTo, AllSuccessors))
13060b57cec5SDimitry Andric         return nullptr;
13070b57cec5SDimitry Andric     }
13080b57cec5SDimitry Andric   }
13090b57cec5SDimitry Andric 
13100b57cec5SDimitry Andric   // It is not possible to sink an instruction into its own block.  This can
131181ad6265SDimitry Andric   // happen with cycles.
13120b57cec5SDimitry Andric   if (MBB == SuccToSinkTo)
13130b57cec5SDimitry Andric     return nullptr;
13140b57cec5SDimitry Andric 
13150b57cec5SDimitry Andric   // It's not safe to sink instructions to EH landing pad. Control flow into
13160b57cec5SDimitry Andric   // landing pad is implicitly defined.
13175f757f3fSDimitry Andric   if (SuccToSinkTo && SuccToSinkTo->isEHPad())
13180b57cec5SDimitry Andric     return nullptr;
13190b57cec5SDimitry Andric 
13205ffd83dbSDimitry Andric   // It ought to be okay to sink instructions into an INLINEASM_BR target, but
13215ffd83dbSDimitry Andric   // only if we make sure that MI occurs _before_ an INLINEASM_BR instruction in
13225ffd83dbSDimitry Andric   // the source block (which this code does not yet do). So for now, forbid
13235ffd83dbSDimitry Andric   // doing so.
13245f757f3fSDimitry Andric   if (SuccToSinkTo && SuccToSinkTo->isInlineAsmBrIndirectTarget())
132506c3fb27SDimitry Andric     return nullptr;
132606c3fb27SDimitry Andric 
13275f757f3fSDimitry Andric   if (SuccToSinkTo && !TII->isSafeToSink(MI, SuccToSinkTo, CI))
13285ffd83dbSDimitry Andric     return nullptr;
13295ffd83dbSDimitry Andric 
13300b57cec5SDimitry Andric   return SuccToSinkTo;
13310b57cec5SDimitry Andric }
13320b57cec5SDimitry Andric 
13330b57cec5SDimitry Andric /// Return true if MI is likely to be usable as a memory operation by the
13340b57cec5SDimitry Andric /// implicit null check optimization.
13350b57cec5SDimitry Andric ///
13360b57cec5SDimitry Andric /// This is a "best effort" heuristic, and should not be relied upon for
13370b57cec5SDimitry Andric /// correctness.  This returning true does not guarantee that the implicit null
13380b57cec5SDimitry Andric /// check optimization is legal over MI, and this returning false does not
13390b57cec5SDimitry Andric /// guarantee MI cannot possibly be used to do a null check.
13400b57cec5SDimitry Andric static bool SinkingPreventsImplicitNullCheck(MachineInstr &MI,
13410b57cec5SDimitry Andric                                              const TargetInstrInfo *TII,
13420b57cec5SDimitry Andric                                              const TargetRegisterInfo *TRI) {
13430b57cec5SDimitry Andric   using MachineBranchPredicate = TargetInstrInfo::MachineBranchPredicate;
13440b57cec5SDimitry Andric 
13450b57cec5SDimitry Andric   auto *MBB = MI.getParent();
13460b57cec5SDimitry Andric   if (MBB->pred_size() != 1)
13470b57cec5SDimitry Andric     return false;
13480b57cec5SDimitry Andric 
13490b57cec5SDimitry Andric   auto *PredMBB = *MBB->pred_begin();
13500b57cec5SDimitry Andric   auto *PredBB = PredMBB->getBasicBlock();
13510b57cec5SDimitry Andric 
13520b57cec5SDimitry Andric   // Frontends that don't use implicit null checks have no reason to emit
13530b57cec5SDimitry Andric   // branches with make.implicit metadata, and this function should always
13540b57cec5SDimitry Andric   // return false for them.
13550b57cec5SDimitry Andric   if (!PredBB ||
13560b57cec5SDimitry Andric       !PredBB->getTerminator()->getMetadata(LLVMContext::MD_make_implicit))
13570b57cec5SDimitry Andric     return false;
13580b57cec5SDimitry Andric 
13590b57cec5SDimitry Andric   const MachineOperand *BaseOp;
13600b57cec5SDimitry Andric   int64_t Offset;
13615ffd83dbSDimitry Andric   bool OffsetIsScalable;
13625ffd83dbSDimitry Andric   if (!TII->getMemOperandWithOffset(MI, BaseOp, Offset, OffsetIsScalable, TRI))
13630b57cec5SDimitry Andric     return false;
13640b57cec5SDimitry Andric 
13650b57cec5SDimitry Andric   if (!BaseOp->isReg())
13660b57cec5SDimitry Andric     return false;
13670b57cec5SDimitry Andric 
13680b57cec5SDimitry Andric   if (!(MI.mayLoad() && !MI.isPredicable()))
13690b57cec5SDimitry Andric     return false;
13700b57cec5SDimitry Andric 
13710b57cec5SDimitry Andric   MachineBranchPredicate MBP;
13720b57cec5SDimitry Andric   if (TII->analyzeBranchPredicate(*PredMBB, MBP, false))
13730b57cec5SDimitry Andric     return false;
13740b57cec5SDimitry Andric 
13750b57cec5SDimitry Andric   return MBP.LHS.isReg() && MBP.RHS.isImm() && MBP.RHS.getImm() == 0 &&
13760b57cec5SDimitry Andric          (MBP.Predicate == MachineBranchPredicate::PRED_NE ||
13770b57cec5SDimitry Andric           MBP.Predicate == MachineBranchPredicate::PRED_EQ) &&
13780b57cec5SDimitry Andric          MBP.LHS.getReg() == BaseOp->getReg();
13790b57cec5SDimitry Andric }
13800b57cec5SDimitry Andric 
1381480093f4SDimitry Andric /// If the sunk instruction is a copy, try to forward the copy instead of
1382480093f4SDimitry Andric /// leaving an 'undef' DBG_VALUE in the original location. Don't do this if
1383480093f4SDimitry Andric /// there's any subregister weirdness involved. Returns true if copy
1384480093f4SDimitry Andric /// propagation occurred.
1385fe6060f1SDimitry Andric static bool attemptDebugCopyProp(MachineInstr &SinkInst, MachineInstr &DbgMI,
1386fe6060f1SDimitry Andric                                  Register Reg) {
1387480093f4SDimitry Andric   const MachineRegisterInfo &MRI = SinkInst.getMF()->getRegInfo();
1388480093f4SDimitry Andric   const TargetInstrInfo &TII = *SinkInst.getMF()->getSubtarget().getInstrInfo();
1389480093f4SDimitry Andric 
1390480093f4SDimitry Andric   // Copy DBG_VALUE operand and set the original to undef. We then check to
1391480093f4SDimitry Andric   // see whether this is something that can be copy-forwarded. If it isn't,
1392480093f4SDimitry Andric   // continue around the loop.
1393480093f4SDimitry Andric 
1394480093f4SDimitry Andric   const MachineOperand *SrcMO = nullptr, *DstMO = nullptr;
1395480093f4SDimitry Andric   auto CopyOperands = TII.isCopyInstr(SinkInst);
1396480093f4SDimitry Andric   if (!CopyOperands)
1397480093f4SDimitry Andric     return false;
1398480093f4SDimitry Andric   SrcMO = CopyOperands->Source;
1399480093f4SDimitry Andric   DstMO = CopyOperands->Destination;
1400480093f4SDimitry Andric 
1401480093f4SDimitry Andric   // Check validity of forwarding this copy.
1402480093f4SDimitry Andric   bool PostRA = MRI.getNumVirtRegs() == 0;
1403480093f4SDimitry Andric 
1404480093f4SDimitry Andric   // Trying to forward between physical and virtual registers is too hard.
1405fe6060f1SDimitry Andric   if (Reg.isVirtual() != SrcMO->getReg().isVirtual())
1406480093f4SDimitry Andric     return false;
1407480093f4SDimitry Andric 
1408480093f4SDimitry Andric   // Only try virtual register copy-forwarding before regalloc, and physical
1409480093f4SDimitry Andric   // register copy-forwarding after regalloc.
1410fe6060f1SDimitry Andric   bool arePhysRegs = !Reg.isVirtual();
1411480093f4SDimitry Andric   if (arePhysRegs != PostRA)
1412480093f4SDimitry Andric     return false;
1413480093f4SDimitry Andric 
1414480093f4SDimitry Andric   // Pre-regalloc, only forward if all subregisters agree (or there are no
1415480093f4SDimitry Andric   // subregs at all). More analysis might recover some forwardable copies.
1416fe6060f1SDimitry Andric   if (!PostRA)
1417fe6060f1SDimitry Andric     for (auto &DbgMO : DbgMI.getDebugOperandsForReg(Reg))
1418fe6060f1SDimitry Andric       if (DbgMO.getSubReg() != SrcMO->getSubReg() ||
1419fe6060f1SDimitry Andric           DbgMO.getSubReg() != DstMO->getSubReg())
1420480093f4SDimitry Andric         return false;
1421480093f4SDimitry Andric 
1422480093f4SDimitry Andric   // Post-regalloc, we may be sinking a DBG_VALUE of a sub or super-register
1423480093f4SDimitry Andric   // of this copy. Only forward the copy if the DBG_VALUE operand exactly
1424480093f4SDimitry Andric   // matches the copy destination.
1425fe6060f1SDimitry Andric   if (PostRA && Reg != DstMO->getReg())
1426480093f4SDimitry Andric     return false;
1427480093f4SDimitry Andric 
1428fe6060f1SDimitry Andric   for (auto &DbgMO : DbgMI.getDebugOperandsForReg(Reg)) {
14295ffd83dbSDimitry Andric     DbgMO.setReg(SrcMO->getReg());
14305ffd83dbSDimitry Andric     DbgMO.setSubReg(SrcMO->getSubReg());
1431fe6060f1SDimitry Andric   }
1432480093f4SDimitry Andric   return true;
1433480093f4SDimitry Andric }
1434480093f4SDimitry Andric 
1435fe6060f1SDimitry Andric using MIRegs = std::pair<MachineInstr *, SmallVector<unsigned, 2>>;
1436480093f4SDimitry Andric /// Sink an instruction and its associated debug instructions.
14370b57cec5SDimitry Andric static void performSink(MachineInstr &MI, MachineBasicBlock &SuccToSinkTo,
14380b57cec5SDimitry Andric                         MachineBasicBlock::iterator InsertPos,
143981ad6265SDimitry Andric                         ArrayRef<MIRegs> DbgValuesToSink) {
14400b57cec5SDimitry Andric   // If we cannot find a location to use (merge with), then we erase the debug
14410b57cec5SDimitry Andric   // location to prevent debug-info driven tools from potentially reporting
14420b57cec5SDimitry Andric   // wrong location information.
14430b57cec5SDimitry Andric   if (!SuccToSinkTo.empty() && InsertPos != SuccToSinkTo.end())
14440b57cec5SDimitry Andric     MI.setDebugLoc(DILocation::getMergedLocation(MI.getDebugLoc(),
14450b57cec5SDimitry Andric                                                  InsertPos->getDebugLoc()));
14460b57cec5SDimitry Andric   else
14470b57cec5SDimitry Andric     MI.setDebugLoc(DebugLoc());
14480b57cec5SDimitry Andric 
14490b57cec5SDimitry Andric   // Move the instruction.
14500b57cec5SDimitry Andric   MachineBasicBlock *ParentBlock = MI.getParent();
14510b57cec5SDimitry Andric   SuccToSinkTo.splice(InsertPos, ParentBlock, MI,
14520b57cec5SDimitry Andric                       ++MachineBasicBlock::iterator(MI));
14530b57cec5SDimitry Andric 
1454480093f4SDimitry Andric   // Sink a copy of debug users to the insert position. Mark the original
1455480093f4SDimitry Andric   // DBG_VALUE location as 'undef', indicating that any earlier variable
1456480093f4SDimitry Andric   // location should be terminated as we've optimised away the value at this
1457480093f4SDimitry Andric   // point.
145881ad6265SDimitry Andric   for (const auto &DbgValueToSink : DbgValuesToSink) {
1459fe6060f1SDimitry Andric     MachineInstr *DbgMI = DbgValueToSink.first;
1460fe6060f1SDimitry Andric     MachineInstr *NewDbgMI = DbgMI->getMF()->CloneMachineInstr(DbgMI);
1461480093f4SDimitry Andric     SuccToSinkTo.insert(InsertPos, NewDbgMI);
1462480093f4SDimitry Andric 
1463fe6060f1SDimitry Andric     bool PropagatedAllSunkOps = true;
1464fe6060f1SDimitry Andric     for (unsigned Reg : DbgValueToSink.second) {
1465fe6060f1SDimitry Andric       if (DbgMI->hasDebugOperandForReg(Reg)) {
1466fe6060f1SDimitry Andric         if (!attemptDebugCopyProp(MI, *DbgMI, Reg)) {
1467fe6060f1SDimitry Andric           PropagatedAllSunkOps = false;
1468fe6060f1SDimitry Andric           break;
1469fe6060f1SDimitry Andric         }
1470fe6060f1SDimitry Andric       }
1471fe6060f1SDimitry Andric     }
1472fe6060f1SDimitry Andric     if (!PropagatedAllSunkOps)
14735ffd83dbSDimitry Andric       DbgMI->setDebugValueUndef();
14740b57cec5SDimitry Andric   }
14750b57cec5SDimitry Andric }
14760b57cec5SDimitry Andric 
1477e8d8bef9SDimitry Andric /// hasStoreBetween - check if there is store betweeen straight line blocks From
1478e8d8bef9SDimitry Andric /// and To.
1479e8d8bef9SDimitry Andric bool MachineSinking::hasStoreBetween(MachineBasicBlock *From,
1480e8d8bef9SDimitry Andric                                      MachineBasicBlock *To, MachineInstr &MI) {
1481e8d8bef9SDimitry Andric   // Make sure From and To are in straight line which means From dominates To
1482e8d8bef9SDimitry Andric   // and To post dominates From.
1483e8d8bef9SDimitry Andric   if (!DT->dominates(From, To) || !PDT->dominates(To, From))
1484e8d8bef9SDimitry Andric     return true;
1485e8d8bef9SDimitry Andric 
1486e8d8bef9SDimitry Andric   auto BlockPair = std::make_pair(From, To);
1487e8d8bef9SDimitry Andric 
1488e8d8bef9SDimitry Andric   // Does these two blocks pair be queried before and have a definite cached
1489e8d8bef9SDimitry Andric   // result?
14905f757f3fSDimitry Andric   if (auto It = HasStoreCache.find(BlockPair); It != HasStoreCache.end())
14915f757f3fSDimitry Andric     return It->second;
1492e8d8bef9SDimitry Andric 
14935f757f3fSDimitry Andric   if (auto It = StoreInstrCache.find(BlockPair); It != StoreInstrCache.end())
14945f757f3fSDimitry Andric     return llvm::any_of(It->second, [&](MachineInstr *I) {
1495e8d8bef9SDimitry Andric       return I->mayAlias(AA, MI, false);
1496e8d8bef9SDimitry Andric     });
1497e8d8bef9SDimitry Andric 
1498e8d8bef9SDimitry Andric   bool SawStore = false;
1499e8d8bef9SDimitry Andric   bool HasAliasedStore = false;
1500e8d8bef9SDimitry Andric   DenseSet<MachineBasicBlock *> HandledBlocks;
1501e8d8bef9SDimitry Andric   DenseSet<MachineBasicBlock *> HandledDomBlocks;
1502e8d8bef9SDimitry Andric   // Go through all reachable blocks from From.
1503e8d8bef9SDimitry Andric   for (MachineBasicBlock *BB : depth_first(From)) {
1504e8d8bef9SDimitry Andric     // We insert the instruction at the start of block To, so no need to worry
1505e8d8bef9SDimitry Andric     // about stores inside To.
1506e8d8bef9SDimitry Andric     // Store in block From should be already considered when just enter function
1507e8d8bef9SDimitry Andric     // SinkInstruction.
1508e8d8bef9SDimitry Andric     if (BB == To || BB == From)
1509e8d8bef9SDimitry Andric       continue;
1510e8d8bef9SDimitry Andric 
1511e8d8bef9SDimitry Andric     // We already handle this BB in previous iteration.
1512e8d8bef9SDimitry Andric     if (HandledBlocks.count(BB))
1513e8d8bef9SDimitry Andric       continue;
1514e8d8bef9SDimitry Andric 
1515e8d8bef9SDimitry Andric     HandledBlocks.insert(BB);
1516e8d8bef9SDimitry Andric     // To post dominates BB, it must be a path from block From.
1517e8d8bef9SDimitry Andric     if (PDT->dominates(To, BB)) {
1518e8d8bef9SDimitry Andric       if (!HandledDomBlocks.count(BB))
1519e8d8bef9SDimitry Andric         HandledDomBlocks.insert(BB);
1520e8d8bef9SDimitry Andric 
1521e8d8bef9SDimitry Andric       // If this BB is too big or the block number in straight line between From
1522e8d8bef9SDimitry Andric       // and To is too big, stop searching to save compiling time.
152381ad6265SDimitry Andric       if (BB->sizeWithoutDebugLargerThan(SinkLoadInstsPerBlockThreshold) ||
1524e8d8bef9SDimitry Andric           HandledDomBlocks.size() > SinkLoadBlocksThreshold) {
1525e8d8bef9SDimitry Andric         for (auto *DomBB : HandledDomBlocks) {
1526e8d8bef9SDimitry Andric           if (DomBB != BB && DT->dominates(DomBB, BB))
1527e8d8bef9SDimitry Andric             HasStoreCache[std::make_pair(DomBB, To)] = true;
1528e8d8bef9SDimitry Andric           else if(DomBB != BB && DT->dominates(BB, DomBB))
1529e8d8bef9SDimitry Andric             HasStoreCache[std::make_pair(From, DomBB)] = true;
1530e8d8bef9SDimitry Andric         }
1531e8d8bef9SDimitry Andric         HasStoreCache[BlockPair] = true;
1532e8d8bef9SDimitry Andric         return true;
1533e8d8bef9SDimitry Andric       }
1534e8d8bef9SDimitry Andric 
1535e8d8bef9SDimitry Andric       for (MachineInstr &I : *BB) {
1536e8d8bef9SDimitry Andric         // Treat as alias conservatively for a call or an ordered memory
1537e8d8bef9SDimitry Andric         // operation.
1538e8d8bef9SDimitry Andric         if (I.isCall() || I.hasOrderedMemoryRef()) {
1539e8d8bef9SDimitry Andric           for (auto *DomBB : HandledDomBlocks) {
1540e8d8bef9SDimitry Andric             if (DomBB != BB && DT->dominates(DomBB, BB))
1541e8d8bef9SDimitry Andric               HasStoreCache[std::make_pair(DomBB, To)] = true;
1542e8d8bef9SDimitry Andric             else if(DomBB != BB && DT->dominates(BB, DomBB))
1543e8d8bef9SDimitry Andric               HasStoreCache[std::make_pair(From, DomBB)] = true;
1544e8d8bef9SDimitry Andric           }
1545e8d8bef9SDimitry Andric           HasStoreCache[BlockPair] = true;
1546e8d8bef9SDimitry Andric           return true;
1547e8d8bef9SDimitry Andric         }
1548e8d8bef9SDimitry Andric 
1549e8d8bef9SDimitry Andric         if (I.mayStore()) {
1550e8d8bef9SDimitry Andric           SawStore = true;
1551e8d8bef9SDimitry Andric           // We still have chance to sink MI if all stores between are not
1552e8d8bef9SDimitry Andric           // aliased to MI.
1553e8d8bef9SDimitry Andric           // Cache all store instructions, so that we don't need to go through
1554e8d8bef9SDimitry Andric           // all From reachable blocks for next load instruction.
1555e8d8bef9SDimitry Andric           if (I.mayAlias(AA, MI, false))
1556e8d8bef9SDimitry Andric             HasAliasedStore = true;
1557e8d8bef9SDimitry Andric           StoreInstrCache[BlockPair].push_back(&I);
1558e8d8bef9SDimitry Andric         }
1559e8d8bef9SDimitry Andric       }
1560e8d8bef9SDimitry Andric     }
1561e8d8bef9SDimitry Andric   }
1562e8d8bef9SDimitry Andric   // If there is no store at all, cache the result.
1563e8d8bef9SDimitry Andric   if (!SawStore)
1564e8d8bef9SDimitry Andric     HasStoreCache[BlockPair] = false;
1565e8d8bef9SDimitry Andric   return HasAliasedStore;
1566e8d8bef9SDimitry Andric }
1567e8d8bef9SDimitry Andric 
156881ad6265SDimitry Andric /// Sink instructions into cycles if profitable. This especially tries to
156981ad6265SDimitry Andric /// prevent register spills caused by register pressure if there is little to no
157081ad6265SDimitry Andric /// overhead moving instructions into cycles.
157181ad6265SDimitry Andric bool MachineSinking::SinkIntoCycle(MachineCycle *Cycle, MachineInstr &I) {
157281ad6265SDimitry Andric   LLVM_DEBUG(dbgs() << "CycleSink: Finding sink block for: " << I);
157381ad6265SDimitry Andric   MachineBasicBlock *Preheader = Cycle->getCyclePreheader();
157481ad6265SDimitry Andric   assert(Preheader && "Cycle sink needs a preheader block");
1575fe6060f1SDimitry Andric   MachineBasicBlock *SinkBlock = nullptr;
1576fe6060f1SDimitry Andric   bool CanSink = true;
1577fe6060f1SDimitry Andric   const MachineOperand &MO = I.getOperand(0);
1578fe6060f1SDimitry Andric 
1579fe6060f1SDimitry Andric   for (MachineInstr &MI : MRI->use_instructions(MO.getReg())) {
158081ad6265SDimitry Andric     LLVM_DEBUG(dbgs() << "CycleSink:   Analysing use: " << MI);
158181ad6265SDimitry Andric     if (!Cycle->contains(MI.getParent())) {
158281ad6265SDimitry Andric       LLVM_DEBUG(dbgs() << "CycleSink:   Use not in cycle, can't sink.\n");
1583fe6060f1SDimitry Andric       CanSink = false;
1584fe6060f1SDimitry Andric       break;
1585fe6060f1SDimitry Andric     }
1586fe6060f1SDimitry Andric 
1587fe6060f1SDimitry Andric     // FIXME: Come up with a proper cost model that estimates whether sinking
158881ad6265SDimitry Andric     // the instruction (and thus possibly executing it on every cycle
1589fe6060f1SDimitry Andric     // iteration) is more expensive than a register.
1590fe6060f1SDimitry Andric     // For now assumes that copies are cheap and thus almost always worth it.
1591fe6060f1SDimitry Andric     if (!MI.isCopy()) {
159281ad6265SDimitry Andric       LLVM_DEBUG(dbgs() << "CycleSink:   Use is not a copy\n");
1593fe6060f1SDimitry Andric       CanSink = false;
1594fe6060f1SDimitry Andric       break;
1595fe6060f1SDimitry Andric     }
1596fe6060f1SDimitry Andric     if (!SinkBlock) {
1597fe6060f1SDimitry Andric       SinkBlock = MI.getParent();
159881ad6265SDimitry Andric       LLVM_DEBUG(dbgs() << "CycleSink:   Setting sink block to: "
1599fe6060f1SDimitry Andric                         << printMBBReference(*SinkBlock) << "\n");
1600fe6060f1SDimitry Andric       continue;
1601fe6060f1SDimitry Andric     }
1602fe6060f1SDimitry Andric     SinkBlock = DT->findNearestCommonDominator(SinkBlock, MI.getParent());
1603fe6060f1SDimitry Andric     if (!SinkBlock) {
160481ad6265SDimitry Andric       LLVM_DEBUG(dbgs() << "CycleSink:   Can't find nearest dominator\n");
1605fe6060f1SDimitry Andric       CanSink = false;
1606fe6060f1SDimitry Andric       break;
1607fe6060f1SDimitry Andric     }
160881ad6265SDimitry Andric     LLVM_DEBUG(dbgs() << "CycleSink:   Setting nearest common dom block: " <<
1609fe6060f1SDimitry Andric                printMBBReference(*SinkBlock) << "\n");
1610fe6060f1SDimitry Andric   }
1611fe6060f1SDimitry Andric 
1612fe6060f1SDimitry Andric   if (!CanSink) {
161381ad6265SDimitry Andric     LLVM_DEBUG(dbgs() << "CycleSink: Can't sink instruction.\n");
1614fe6060f1SDimitry Andric     return false;
1615fe6060f1SDimitry Andric   }
1616fe6060f1SDimitry Andric   if (!SinkBlock) {
161781ad6265SDimitry Andric     LLVM_DEBUG(dbgs() << "CycleSink: Not sinking, can't find sink block.\n");
1618fe6060f1SDimitry Andric     return false;
1619fe6060f1SDimitry Andric   }
1620fe6060f1SDimitry Andric   if (SinkBlock == Preheader) {
162181ad6265SDimitry Andric     LLVM_DEBUG(
162281ad6265SDimitry Andric         dbgs() << "CycleSink: Not sinking, sink block is the preheader\n");
1623fe6060f1SDimitry Andric     return false;
1624fe6060f1SDimitry Andric   }
162581ad6265SDimitry Andric   if (SinkBlock->sizeWithoutDebugLargerThan(SinkLoadInstsPerBlockThreshold)) {
162681ad6265SDimitry Andric     LLVM_DEBUG(
162781ad6265SDimitry Andric         dbgs() << "CycleSink: Not Sinking, block too large to analyse.\n");
1628fe6060f1SDimitry Andric     return false;
1629fe6060f1SDimitry Andric   }
1630fe6060f1SDimitry Andric 
163181ad6265SDimitry Andric   LLVM_DEBUG(dbgs() << "CycleSink: Sinking instruction!\n");
163281ad6265SDimitry Andric   SinkBlock->splice(SinkBlock->SkipPHIsAndLabels(SinkBlock->begin()), Preheader,
163381ad6265SDimitry Andric                     I);
163481ad6265SDimitry Andric 
163581ad6265SDimitry Andric   // Conservatively clear any kill flags on uses of sunk instruction
163681ad6265SDimitry Andric   for (MachineOperand &MO : I.operands()) {
163781ad6265SDimitry Andric     if (MO.isReg() && MO.readsReg())
163881ad6265SDimitry Andric       RegsToClearKillFlags.insert(MO.getReg());
163981ad6265SDimitry Andric   }
1640fe6060f1SDimitry Andric 
1641fe6060f1SDimitry Andric   // The instruction is moved from its basic block, so do not retain the
1642fe6060f1SDimitry Andric   // debug information.
1643fe6060f1SDimitry Andric   assert(!I.isDebugInstr() && "Should not sink debug inst");
1644fe6060f1SDimitry Andric   I.setDebugLoc(DebugLoc());
1645fe6060f1SDimitry Andric   return true;
1646fe6060f1SDimitry Andric }
1647fe6060f1SDimitry Andric 
16480b57cec5SDimitry Andric /// SinkInstruction - Determine whether it is safe to sink the specified machine
16490b57cec5SDimitry Andric /// instruction out of its current block into a successor.
16500b57cec5SDimitry Andric bool MachineSinking::SinkInstruction(MachineInstr &MI, bool &SawStore,
16510b57cec5SDimitry Andric                                      AllSuccsCache &AllSuccessors) {
16520b57cec5SDimitry Andric   // Don't sink instructions that the target prefers not to sink.
16530b57cec5SDimitry Andric   if (!TII->shouldSink(MI))
16540b57cec5SDimitry Andric     return false;
16550b57cec5SDimitry Andric 
16560b57cec5SDimitry Andric   // Check if it's safe to move the instruction.
16570b57cec5SDimitry Andric   if (!MI.isSafeToMove(AA, SawStore))
16580b57cec5SDimitry Andric     return false;
16590b57cec5SDimitry Andric 
16600b57cec5SDimitry Andric   // Convergent operations may not be made control-dependent on additional
16610b57cec5SDimitry Andric   // values.
16620b57cec5SDimitry Andric   if (MI.isConvergent())
16630b57cec5SDimitry Andric     return false;
16640b57cec5SDimitry Andric 
16650b57cec5SDimitry Andric   // Don't break implicit null checks.  This is a performance heuristic, and not
16660b57cec5SDimitry Andric   // required for correctness.
16670b57cec5SDimitry Andric   if (SinkingPreventsImplicitNullCheck(MI, TII, TRI))
16680b57cec5SDimitry Andric     return false;
16690b57cec5SDimitry Andric 
16700b57cec5SDimitry Andric   // FIXME: This should include support for sinking instructions within the
16710b57cec5SDimitry Andric   // block they are currently in to shorten the live ranges.  We often get
16720b57cec5SDimitry Andric   // instructions sunk into the top of a large block, but it would be better to
16730b57cec5SDimitry Andric   // also sink them down before their first use in the block.  This xform has to
16740b57cec5SDimitry Andric   // be careful not to *increase* register pressure though, e.g. sinking
16750b57cec5SDimitry Andric   // "x = y + z" down if it kills y and z would increase the live ranges of y
16760b57cec5SDimitry Andric   // and z and only shrink the live range of x.
16770b57cec5SDimitry Andric 
16780b57cec5SDimitry Andric   bool BreakPHIEdge = false;
16790b57cec5SDimitry Andric   MachineBasicBlock *ParentBlock = MI.getParent();
16800b57cec5SDimitry Andric   MachineBasicBlock *SuccToSinkTo =
16810b57cec5SDimitry Andric       FindSuccToSinkTo(MI, ParentBlock, BreakPHIEdge, AllSuccessors);
16820b57cec5SDimitry Andric 
16830b57cec5SDimitry Andric   // If there are no outputs, it must have side-effects.
16840b57cec5SDimitry Andric   if (!SuccToSinkTo)
16850b57cec5SDimitry Andric     return false;
16860b57cec5SDimitry Andric 
16870b57cec5SDimitry Andric   // If the instruction to move defines a dead physical register which is live
16880b57cec5SDimitry Andric   // when leaving the basic block, don't move it because it could turn into a
16895f757f3fSDimitry Andric   // "zombie" define of that preg. E.g., EFLAGS.
169006c3fb27SDimitry Andric   for (const MachineOperand &MO : MI.all_defs()) {
16918bcb0991SDimitry Andric     Register Reg = MO.getReg();
1692bdd1243dSDimitry Andric     if (Reg == 0 || !Reg.isPhysical())
16938bcb0991SDimitry Andric       continue;
16940b57cec5SDimitry Andric     if (SuccToSinkTo->isLiveIn(Reg))
16950b57cec5SDimitry Andric       return false;
16960b57cec5SDimitry Andric   }
16970b57cec5SDimitry Andric 
16980b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Sink instr " << MI << "\tinto block " << *SuccToSinkTo);
16990b57cec5SDimitry Andric 
17000b57cec5SDimitry Andric   // If the block has multiple predecessors, this is a critical edge.
17010b57cec5SDimitry Andric   // Decide if we can sink along it or need to break the edge.
17020b57cec5SDimitry Andric   if (SuccToSinkTo->pred_size() > 1) {
17030b57cec5SDimitry Andric     // We cannot sink a load across a critical edge - there may be stores in
17040b57cec5SDimitry Andric     // other code paths.
17050b57cec5SDimitry Andric     bool TryBreak = false;
1706e8d8bef9SDimitry Andric     bool Store =
1707e8d8bef9SDimitry Andric         MI.mayLoad() ? hasStoreBetween(ParentBlock, SuccToSinkTo, MI) : true;
1708e8d8bef9SDimitry Andric     if (!MI.isSafeToMove(AA, Store)) {
17090b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << " *** NOTE: Won't sink load along critical edge.\n");
17100b57cec5SDimitry Andric       TryBreak = true;
17110b57cec5SDimitry Andric     }
17120b57cec5SDimitry Andric 
17130b57cec5SDimitry Andric     // We don't want to sink across a critical edge if we don't dominate the
17140b57cec5SDimitry Andric     // successor. We could be introducing calculations to new code paths.
17150b57cec5SDimitry Andric     if (!TryBreak && !DT->dominates(ParentBlock, SuccToSinkTo)) {
17160b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << " *** NOTE: Critical edge found\n");
17170b57cec5SDimitry Andric       TryBreak = true;
17180b57cec5SDimitry Andric     }
17190b57cec5SDimitry Andric 
172081ad6265SDimitry Andric     // Don't sink instructions into a cycle.
172181ad6265SDimitry Andric     if (!TryBreak && CI->getCycle(SuccToSinkTo) &&
172281ad6265SDimitry Andric         (!CI->getCycle(SuccToSinkTo)->isReducible() ||
172381ad6265SDimitry Andric          CI->getCycle(SuccToSinkTo)->getHeader() == SuccToSinkTo)) {
172481ad6265SDimitry Andric       LLVM_DEBUG(dbgs() << " *** NOTE: cycle header found\n");
17250b57cec5SDimitry Andric       TryBreak = true;
17260b57cec5SDimitry Andric     }
17270b57cec5SDimitry Andric 
17280b57cec5SDimitry Andric     // Otherwise we are OK with sinking along a critical edge.
17290b57cec5SDimitry Andric     if (!TryBreak)
17300b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "Sinking along critical edge.\n");
17310b57cec5SDimitry Andric     else {
17320b57cec5SDimitry Andric       // Mark this edge as to be split.
17330b57cec5SDimitry Andric       // If the edge can actually be split, the next iteration of the main loop
17340b57cec5SDimitry Andric       // will sink MI in the newly created block.
17350b57cec5SDimitry Andric       bool Status =
17360b57cec5SDimitry Andric         PostponeSplitCriticalEdge(MI, ParentBlock, SuccToSinkTo, BreakPHIEdge);
17370b57cec5SDimitry Andric       if (!Status)
17380b57cec5SDimitry Andric         LLVM_DEBUG(dbgs() << " *** PUNTING: Not legal or profitable to "
17390b57cec5SDimitry Andric                              "break critical edge\n");
17400b57cec5SDimitry Andric       // The instruction will not be sunk this time.
17410b57cec5SDimitry Andric       return false;
17420b57cec5SDimitry Andric     }
17430b57cec5SDimitry Andric   }
17440b57cec5SDimitry Andric 
17450b57cec5SDimitry Andric   if (BreakPHIEdge) {
17460b57cec5SDimitry Andric     // BreakPHIEdge is true if all the uses are in the successor MBB being
17470b57cec5SDimitry Andric     // sunken into and they are all PHI nodes. In this case, machine-sink must
17480b57cec5SDimitry Andric     // break the critical edge first.
17490b57cec5SDimitry Andric     bool Status = PostponeSplitCriticalEdge(MI, ParentBlock,
17500b57cec5SDimitry Andric                                             SuccToSinkTo, BreakPHIEdge);
17510b57cec5SDimitry Andric     if (!Status)
17520b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << " *** PUNTING: Not legal or profitable to "
17530b57cec5SDimitry Andric                            "break critical edge\n");
17540b57cec5SDimitry Andric     // The instruction will not be sunk this time.
17550b57cec5SDimitry Andric     return false;
17560b57cec5SDimitry Andric   }
17570b57cec5SDimitry Andric 
17580b57cec5SDimitry Andric   // Determine where to insert into. Skip phi nodes.
175981ad6265SDimitry Andric   MachineBasicBlock::iterator InsertPos =
176081ad6265SDimitry Andric       SuccToSinkTo->SkipPHIsAndLabels(SuccToSinkTo->begin());
176181ad6265SDimitry Andric   if (blockPrologueInterferes(SuccToSinkTo, InsertPos, MI, TRI, TII, MRI)) {
176281ad6265SDimitry Andric     LLVM_DEBUG(dbgs() << " *** Not sinking: prologue interference\n");
176381ad6265SDimitry Andric     return false;
176481ad6265SDimitry Andric   }
17650b57cec5SDimitry Andric 
1766480093f4SDimitry Andric   // Collect debug users of any vreg that this inst defines.
1767fe6060f1SDimitry Andric   SmallVector<MIRegs, 4> DbgUsersToSink;
176806c3fb27SDimitry Andric   for (auto &MO : MI.all_defs()) {
176906c3fb27SDimitry Andric     if (!MO.getReg().isVirtual())
1770480093f4SDimitry Andric       continue;
1771480093f4SDimitry Andric     if (!SeenDbgUsers.count(MO.getReg()))
1772480093f4SDimitry Andric       continue;
1773480093f4SDimitry Andric 
1774480093f4SDimitry Andric     // Sink any users that don't pass any other DBG_VALUEs for this variable.
1775480093f4SDimitry Andric     auto &Users = SeenDbgUsers[MO.getReg()];
1776480093f4SDimitry Andric     for (auto &User : Users) {
1777480093f4SDimitry Andric       MachineInstr *DbgMI = User.getPointer();
1778480093f4SDimitry Andric       if (User.getInt()) {
1779480093f4SDimitry Andric         // This DBG_VALUE would re-order assignments. If we can't copy-propagate
1780480093f4SDimitry Andric         // it, it can't be recovered. Set it undef.
1781fe6060f1SDimitry Andric         if (!attemptDebugCopyProp(MI, *DbgMI, MO.getReg()))
17825ffd83dbSDimitry Andric           DbgMI->setDebugValueUndef();
1783480093f4SDimitry Andric       } else {
1784fe6060f1SDimitry Andric         DbgUsersToSink.push_back(
1785fe6060f1SDimitry Andric             {DbgMI, SmallVector<unsigned, 2>(1, MO.getReg())});
1786480093f4SDimitry Andric       }
1787480093f4SDimitry Andric     }
1788480093f4SDimitry Andric   }
1789480093f4SDimitry Andric 
1790480093f4SDimitry Andric   // After sinking, some debug users may not be dominated any more. If possible,
1791480093f4SDimitry Andric   // copy-propagate their operands. As it's expensive, don't do this if there's
1792480093f4SDimitry Andric   // no debuginfo in the program.
1793480093f4SDimitry Andric   if (MI.getMF()->getFunction().getSubprogram() && MI.isCopy())
1794480093f4SDimitry Andric     SalvageUnsunkDebugUsersOfCopy(MI, SuccToSinkTo);
1795480093f4SDimitry Andric 
1796480093f4SDimitry Andric   performSink(MI, *SuccToSinkTo, InsertPos, DbgUsersToSink);
17970b57cec5SDimitry Andric 
17980b57cec5SDimitry Andric   // Conservatively, clear any kill flags, since it's possible that they are no
17990b57cec5SDimitry Andric   // longer correct.
18000b57cec5SDimitry Andric   // Note that we have to clear the kill flags for any register this instruction
18010b57cec5SDimitry Andric   // uses as we may sink over another instruction which currently kills the
18020b57cec5SDimitry Andric   // used registers.
180306c3fb27SDimitry Andric   for (MachineOperand &MO : MI.all_uses())
1804349cc55cSDimitry Andric     RegsToClearKillFlags.insert(MO.getReg()); // Remember to clear kill flags.
18050b57cec5SDimitry Andric 
18060b57cec5SDimitry Andric   return true;
18070b57cec5SDimitry Andric }
18080b57cec5SDimitry Andric 
1809480093f4SDimitry Andric void MachineSinking::SalvageUnsunkDebugUsersOfCopy(
1810480093f4SDimitry Andric     MachineInstr &MI, MachineBasicBlock *TargetBlock) {
1811480093f4SDimitry Andric   assert(MI.isCopy());
1812480093f4SDimitry Andric   assert(MI.getOperand(1).isReg());
1813480093f4SDimitry Andric 
1814480093f4SDimitry Andric   // Enumerate all users of vreg operands that are def'd. Skip those that will
1815480093f4SDimitry Andric   // be sunk. For the rest, if they are not dominated by the block we will sink
1816480093f4SDimitry Andric   // MI into, propagate the copy source to them.
1817480093f4SDimitry Andric   SmallVector<MachineInstr *, 4> DbgDefUsers;
1818fe6060f1SDimitry Andric   SmallVector<Register, 4> DbgUseRegs;
1819480093f4SDimitry Andric   const MachineRegisterInfo &MRI = MI.getMF()->getRegInfo();
182006c3fb27SDimitry Andric   for (auto &MO : MI.all_defs()) {
182106c3fb27SDimitry Andric     if (!MO.getReg().isVirtual())
1822480093f4SDimitry Andric       continue;
1823fe6060f1SDimitry Andric     DbgUseRegs.push_back(MO.getReg());
1824480093f4SDimitry Andric     for (auto &User : MRI.use_instructions(MO.getReg())) {
1825480093f4SDimitry Andric       if (!User.isDebugValue() || DT->dominates(TargetBlock, User.getParent()))
1826480093f4SDimitry Andric         continue;
1827480093f4SDimitry Andric 
1828480093f4SDimitry Andric       // If is in same block, will either sink or be use-before-def.
1829480093f4SDimitry Andric       if (User.getParent() == MI.getParent())
1830480093f4SDimitry Andric         continue;
1831480093f4SDimitry Andric 
1832fe6060f1SDimitry Andric       assert(User.hasDebugOperandForReg(MO.getReg()) &&
1833fe6060f1SDimitry Andric              "DBG_VALUE user of vreg, but has no operand for it?");
1834480093f4SDimitry Andric       DbgDefUsers.push_back(&User);
1835480093f4SDimitry Andric     }
1836480093f4SDimitry Andric   }
1837480093f4SDimitry Andric 
1838480093f4SDimitry Andric   // Point the users of this copy that are no longer dominated, at the source
1839480093f4SDimitry Andric   // of the copy.
1840480093f4SDimitry Andric   for (auto *User : DbgDefUsers) {
1841fe6060f1SDimitry Andric     for (auto &Reg : DbgUseRegs) {
1842fe6060f1SDimitry Andric       for (auto &DbgOp : User->getDebugOperandsForReg(Reg)) {
1843fe6060f1SDimitry Andric         DbgOp.setReg(MI.getOperand(1).getReg());
1844fe6060f1SDimitry Andric         DbgOp.setSubReg(MI.getOperand(1).getSubReg());
1845fe6060f1SDimitry Andric       }
1846fe6060f1SDimitry Andric     }
1847480093f4SDimitry Andric   }
1848480093f4SDimitry Andric }
1849480093f4SDimitry Andric 
18500b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
18510b57cec5SDimitry Andric // This pass is not intended to be a replacement or a complete alternative
18520b57cec5SDimitry Andric // for the pre-ra machine sink pass. It is only designed to sink COPY
18530b57cec5SDimitry Andric // instructions which should be handled after RA.
18540b57cec5SDimitry Andric //
18550b57cec5SDimitry Andric // This pass sinks COPY instructions into a successor block, if the COPY is not
18560b57cec5SDimitry Andric // used in the current block and the COPY is live-in to a single successor
18570b57cec5SDimitry Andric // (i.e., doesn't require the COPY to be duplicated).  This avoids executing the
18580b57cec5SDimitry Andric // copy on paths where their results aren't needed.  This also exposes
18590b57cec5SDimitry Andric // additional opportunites for dead copy elimination and shrink wrapping.
18600b57cec5SDimitry Andric //
18610b57cec5SDimitry Andric // These copies were either not handled by or are inserted after the MachineSink
18620b57cec5SDimitry Andric // pass. As an example of the former case, the MachineSink pass cannot sink
18630b57cec5SDimitry Andric // COPY instructions with allocatable source registers; for AArch64 these type
18640b57cec5SDimitry Andric // of copy instructions are frequently used to move function parameters (PhyReg)
18650b57cec5SDimitry Andric // into virtual registers in the entry block.
18660b57cec5SDimitry Andric //
18670b57cec5SDimitry Andric // For the machine IR below, this pass will sink %w19 in the entry into its
18680b57cec5SDimitry Andric // successor (%bb.1) because %w19 is only live-in in %bb.1.
18690b57cec5SDimitry Andric // %bb.0:
18700b57cec5SDimitry Andric //   %wzr = SUBSWri %w1, 1
18710b57cec5SDimitry Andric //   %w19 = COPY %w0
18720b57cec5SDimitry Andric //   Bcc 11, %bb.2
18730b57cec5SDimitry Andric // %bb.1:
18740b57cec5SDimitry Andric //   Live Ins: %w19
18750b57cec5SDimitry Andric //   BL @fun
18760b57cec5SDimitry Andric //   %w0 = ADDWrr %w0, %w19
18770b57cec5SDimitry Andric //   RET %w0
18780b57cec5SDimitry Andric // %bb.2:
18790b57cec5SDimitry Andric //   %w0 = COPY %wzr
18800b57cec5SDimitry Andric //   RET %w0
18810b57cec5SDimitry Andric // As we sink %w19 (CSR in AArch64) into %bb.1, the shrink-wrapping pass will be
18820b57cec5SDimitry Andric // able to see %bb.0 as a candidate.
18830b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
18840b57cec5SDimitry Andric namespace {
18850b57cec5SDimitry Andric 
18860b57cec5SDimitry Andric class PostRAMachineSinking : public MachineFunctionPass {
18870b57cec5SDimitry Andric public:
18880b57cec5SDimitry Andric   bool runOnMachineFunction(MachineFunction &MF) override;
18890b57cec5SDimitry Andric 
18900b57cec5SDimitry Andric   static char ID;
18910b57cec5SDimitry Andric   PostRAMachineSinking() : MachineFunctionPass(ID) {}
18920b57cec5SDimitry Andric   StringRef getPassName() const override { return "PostRA Machine Sink"; }
18930b57cec5SDimitry Andric 
18940b57cec5SDimitry Andric   void getAnalysisUsage(AnalysisUsage &AU) const override {
18950b57cec5SDimitry Andric     AU.setPreservesCFG();
18960b57cec5SDimitry Andric     MachineFunctionPass::getAnalysisUsage(AU);
18970b57cec5SDimitry Andric   }
18980b57cec5SDimitry Andric 
18990b57cec5SDimitry Andric   MachineFunctionProperties getRequiredProperties() const override {
19000b57cec5SDimitry Andric     return MachineFunctionProperties().set(
19010b57cec5SDimitry Andric         MachineFunctionProperties::Property::NoVRegs);
19020b57cec5SDimitry Andric   }
19030b57cec5SDimitry Andric 
19040b57cec5SDimitry Andric private:
19050b57cec5SDimitry Andric   /// Track which register units have been modified and used.
19060b57cec5SDimitry Andric   LiveRegUnits ModifiedRegUnits, UsedRegUnits;
19070b57cec5SDimitry Andric 
19088bcb0991SDimitry Andric   /// Track DBG_VALUEs of (unmodified) register units. Each DBG_VALUE has an
1909fe6060f1SDimitry Andric   /// entry in this map for each unit it touches. The DBG_VALUE's entry
1910fe6060f1SDimitry Andric   /// consists of a pointer to the instruction itself, and a vector of registers
1911fe6060f1SDimitry Andric   /// referred to by the instruction that overlap the key register unit.
1912fe6060f1SDimitry Andric   DenseMap<unsigned, SmallVector<MIRegs, 2>> SeenDbgInstrs;
19130b57cec5SDimitry Andric 
19140b57cec5SDimitry Andric   /// Sink Copy instructions unused in the same block close to their uses in
19150b57cec5SDimitry Andric   /// successors.
19160b57cec5SDimitry Andric   bool tryToSinkCopy(MachineBasicBlock &BB, MachineFunction &MF,
19170b57cec5SDimitry Andric                      const TargetRegisterInfo *TRI, const TargetInstrInfo *TII);
19180b57cec5SDimitry Andric };
19190b57cec5SDimitry Andric } // namespace
19200b57cec5SDimitry Andric 
19210b57cec5SDimitry Andric char PostRAMachineSinking::ID = 0;
19220b57cec5SDimitry Andric char &llvm::PostRAMachineSinkingID = PostRAMachineSinking::ID;
19230b57cec5SDimitry Andric 
19240b57cec5SDimitry Andric INITIALIZE_PASS(PostRAMachineSinking, "postra-machine-sink",
19250b57cec5SDimitry Andric                 "PostRA Machine Sink", false, false)
19260b57cec5SDimitry Andric 
19270b57cec5SDimitry Andric static bool aliasWithRegsInLiveIn(MachineBasicBlock &MBB, unsigned Reg,
19280b57cec5SDimitry Andric                                   const TargetRegisterInfo *TRI) {
19290b57cec5SDimitry Andric   LiveRegUnits LiveInRegUnits(*TRI);
19300b57cec5SDimitry Andric   LiveInRegUnits.addLiveIns(MBB);
19310b57cec5SDimitry Andric   return !LiveInRegUnits.available(Reg);
19320b57cec5SDimitry Andric }
19330b57cec5SDimitry Andric 
19340b57cec5SDimitry Andric static MachineBasicBlock *
19350b57cec5SDimitry Andric getSingleLiveInSuccBB(MachineBasicBlock &CurBB,
19360b57cec5SDimitry Andric                       const SmallPtrSetImpl<MachineBasicBlock *> &SinkableBBs,
19370b57cec5SDimitry Andric                       unsigned Reg, const TargetRegisterInfo *TRI) {
19380b57cec5SDimitry Andric   // Try to find a single sinkable successor in which Reg is live-in.
19390b57cec5SDimitry Andric   MachineBasicBlock *BB = nullptr;
19400b57cec5SDimitry Andric   for (auto *SI : SinkableBBs) {
19410b57cec5SDimitry Andric     if (aliasWithRegsInLiveIn(*SI, Reg, TRI)) {
19420b57cec5SDimitry Andric       // If BB is set here, Reg is live-in to at least two sinkable successors,
19430b57cec5SDimitry Andric       // so quit.
19440b57cec5SDimitry Andric       if (BB)
19450b57cec5SDimitry Andric         return nullptr;
19460b57cec5SDimitry Andric       BB = SI;
19470b57cec5SDimitry Andric     }
19480b57cec5SDimitry Andric   }
19490b57cec5SDimitry Andric   // Reg is not live-in to any sinkable successors.
19500b57cec5SDimitry Andric   if (!BB)
19510b57cec5SDimitry Andric     return nullptr;
19520b57cec5SDimitry Andric 
19530b57cec5SDimitry Andric   // Check if any register aliased with Reg is live-in in other successors.
19540b57cec5SDimitry Andric   for (auto *SI : CurBB.successors()) {
19550b57cec5SDimitry Andric     if (!SinkableBBs.count(SI) && aliasWithRegsInLiveIn(*SI, Reg, TRI))
19560b57cec5SDimitry Andric       return nullptr;
19570b57cec5SDimitry Andric   }
19580b57cec5SDimitry Andric   return BB;
19590b57cec5SDimitry Andric }
19600b57cec5SDimitry Andric 
19610b57cec5SDimitry Andric static MachineBasicBlock *
19620b57cec5SDimitry Andric getSingleLiveInSuccBB(MachineBasicBlock &CurBB,
19630b57cec5SDimitry Andric                       const SmallPtrSetImpl<MachineBasicBlock *> &SinkableBBs,
19640b57cec5SDimitry Andric                       ArrayRef<unsigned> DefedRegsInCopy,
19650b57cec5SDimitry Andric                       const TargetRegisterInfo *TRI) {
19660b57cec5SDimitry Andric   MachineBasicBlock *SingleBB = nullptr;
19670b57cec5SDimitry Andric   for (auto DefReg : DefedRegsInCopy) {
19680b57cec5SDimitry Andric     MachineBasicBlock *BB =
19690b57cec5SDimitry Andric         getSingleLiveInSuccBB(CurBB, SinkableBBs, DefReg, TRI);
19700b57cec5SDimitry Andric     if (!BB || (SingleBB && SingleBB != BB))
19710b57cec5SDimitry Andric       return nullptr;
19720b57cec5SDimitry Andric     SingleBB = BB;
19730b57cec5SDimitry Andric   }
19740b57cec5SDimitry Andric   return SingleBB;
19750b57cec5SDimitry Andric }
19760b57cec5SDimitry Andric 
19770b57cec5SDimitry Andric static void clearKillFlags(MachineInstr *MI, MachineBasicBlock &CurBB,
19780b57cec5SDimitry Andric                            SmallVectorImpl<unsigned> &UsedOpsInCopy,
19790b57cec5SDimitry Andric                            LiveRegUnits &UsedRegUnits,
19800b57cec5SDimitry Andric                            const TargetRegisterInfo *TRI) {
19810b57cec5SDimitry Andric   for (auto U : UsedOpsInCopy) {
19820b57cec5SDimitry Andric     MachineOperand &MO = MI->getOperand(U);
19838bcb0991SDimitry Andric     Register SrcReg = MO.getReg();
19840b57cec5SDimitry Andric     if (!UsedRegUnits.available(SrcReg)) {
19850b57cec5SDimitry Andric       MachineBasicBlock::iterator NI = std::next(MI->getIterator());
19860b57cec5SDimitry Andric       for (MachineInstr &UI : make_range(NI, CurBB.end())) {
19870b57cec5SDimitry Andric         if (UI.killsRegister(SrcReg, TRI)) {
19880b57cec5SDimitry Andric           UI.clearRegisterKills(SrcReg, TRI);
19890b57cec5SDimitry Andric           MO.setIsKill(true);
19900b57cec5SDimitry Andric           break;
19910b57cec5SDimitry Andric         }
19920b57cec5SDimitry Andric       }
19930b57cec5SDimitry Andric     }
19940b57cec5SDimitry Andric   }
19950b57cec5SDimitry Andric }
19960b57cec5SDimitry Andric 
19970b57cec5SDimitry Andric static void updateLiveIn(MachineInstr *MI, MachineBasicBlock *SuccBB,
19980b57cec5SDimitry Andric                          SmallVectorImpl<unsigned> &UsedOpsInCopy,
19990b57cec5SDimitry Andric                          SmallVectorImpl<unsigned> &DefedRegsInCopy) {
20000b57cec5SDimitry Andric   MachineFunction &MF = *SuccBB->getParent();
20010b57cec5SDimitry Andric   const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
20020b57cec5SDimitry Andric   for (unsigned DefReg : DefedRegsInCopy)
200306c3fb27SDimitry Andric     for (MCPhysReg S : TRI->subregs_inclusive(DefReg))
200406c3fb27SDimitry Andric       SuccBB->removeLiveIn(S);
2005*0fca6ea1SDimitry Andric   for (auto U : UsedOpsInCopy)
2006*0fca6ea1SDimitry Andric     SuccBB->addLiveIn(MI->getOperand(U).getReg());
2007480093f4SDimitry Andric   SuccBB->sortUniqueLiveIns();
20080b57cec5SDimitry Andric }
20090b57cec5SDimitry Andric 
20100b57cec5SDimitry Andric static bool hasRegisterDependency(MachineInstr *MI,
20110b57cec5SDimitry Andric                                   SmallVectorImpl<unsigned> &UsedOpsInCopy,
20120b57cec5SDimitry Andric                                   SmallVectorImpl<unsigned> &DefedRegsInCopy,
20130b57cec5SDimitry Andric                                   LiveRegUnits &ModifiedRegUnits,
20140b57cec5SDimitry Andric                                   LiveRegUnits &UsedRegUnits) {
20150b57cec5SDimitry Andric   bool HasRegDependency = false;
20160b57cec5SDimitry Andric   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
20170b57cec5SDimitry Andric     MachineOperand &MO = MI->getOperand(i);
20180b57cec5SDimitry Andric     if (!MO.isReg())
20190b57cec5SDimitry Andric       continue;
20208bcb0991SDimitry Andric     Register Reg = MO.getReg();
20210b57cec5SDimitry Andric     if (!Reg)
20220b57cec5SDimitry Andric       continue;
20230b57cec5SDimitry Andric     if (MO.isDef()) {
20240b57cec5SDimitry Andric       if (!ModifiedRegUnits.available(Reg) || !UsedRegUnits.available(Reg)) {
20250b57cec5SDimitry Andric         HasRegDependency = true;
20260b57cec5SDimitry Andric         break;
20270b57cec5SDimitry Andric       }
20280b57cec5SDimitry Andric       DefedRegsInCopy.push_back(Reg);
20290b57cec5SDimitry Andric 
20300b57cec5SDimitry Andric       // FIXME: instead of isUse(), readsReg() would be a better fix here,
20310b57cec5SDimitry Andric       // For example, we can ignore modifications in reg with undef. However,
20320b57cec5SDimitry Andric       // it's not perfectly clear if skipping the internal read is safe in all
20330b57cec5SDimitry Andric       // other targets.
20340b57cec5SDimitry Andric     } else if (MO.isUse()) {
20350b57cec5SDimitry Andric       if (!ModifiedRegUnits.available(Reg)) {
20360b57cec5SDimitry Andric         HasRegDependency = true;
20370b57cec5SDimitry Andric         break;
20380b57cec5SDimitry Andric       }
20390b57cec5SDimitry Andric       UsedOpsInCopy.push_back(i);
20400b57cec5SDimitry Andric     }
20410b57cec5SDimitry Andric   }
20420b57cec5SDimitry Andric   return HasRegDependency;
20430b57cec5SDimitry Andric }
20440b57cec5SDimitry Andric 
20450b57cec5SDimitry Andric bool PostRAMachineSinking::tryToSinkCopy(MachineBasicBlock &CurBB,
20460b57cec5SDimitry Andric                                          MachineFunction &MF,
20470b57cec5SDimitry Andric                                          const TargetRegisterInfo *TRI,
20480b57cec5SDimitry Andric                                          const TargetInstrInfo *TII) {
20490b57cec5SDimitry Andric   SmallPtrSet<MachineBasicBlock *, 2> SinkableBBs;
20500b57cec5SDimitry Andric   // FIXME: For now, we sink only to a successor which has a single predecessor
20510b57cec5SDimitry Andric   // so that we can directly sink COPY instructions to the successor without
20520b57cec5SDimitry Andric   // adding any new block or branch instruction.
20530b57cec5SDimitry Andric   for (MachineBasicBlock *SI : CurBB.successors())
20540b57cec5SDimitry Andric     if (!SI->livein_empty() && SI->pred_size() == 1)
20550b57cec5SDimitry Andric       SinkableBBs.insert(SI);
20560b57cec5SDimitry Andric 
20570b57cec5SDimitry Andric   if (SinkableBBs.empty())
20580b57cec5SDimitry Andric     return false;
20590b57cec5SDimitry Andric 
20600b57cec5SDimitry Andric   bool Changed = false;
20610b57cec5SDimitry Andric 
20620b57cec5SDimitry Andric   // Track which registers have been modified and used between the end of the
20630b57cec5SDimitry Andric   // block and the current instruction.
20640b57cec5SDimitry Andric   ModifiedRegUnits.clear();
20650b57cec5SDimitry Andric   UsedRegUnits.clear();
20660b57cec5SDimitry Andric   SeenDbgInstrs.clear();
20670b57cec5SDimitry Andric 
2068349cc55cSDimitry Andric   for (MachineInstr &MI : llvm::make_early_inc_range(llvm::reverse(CurBB))) {
20690b57cec5SDimitry Andric     // Track the operand index for use in Copy.
20700b57cec5SDimitry Andric     SmallVector<unsigned, 2> UsedOpsInCopy;
20710b57cec5SDimitry Andric     // Track the register number defed in Copy.
20720b57cec5SDimitry Andric     SmallVector<unsigned, 2> DefedRegsInCopy;
20730b57cec5SDimitry Andric 
20740b57cec5SDimitry Andric     // We must sink this DBG_VALUE if its operand is sunk. To avoid searching
20750b57cec5SDimitry Andric     // for DBG_VALUEs later, record them when they're encountered.
2076bdd1243dSDimitry Andric     if (MI.isDebugValue() && !MI.isDebugRef()) {
2077fe6060f1SDimitry Andric       SmallDenseMap<MCRegister, SmallVector<unsigned, 2>, 4> MIUnits;
2078fe6060f1SDimitry Andric       bool IsValid = true;
2079349cc55cSDimitry Andric       for (MachineOperand &MO : MI.debug_operands()) {
2080bdd1243dSDimitry Andric         if (MO.isReg() && MO.getReg().isPhysical()) {
20810b57cec5SDimitry Andric           // Bail if we can already tell the sink would be rejected, rather
20820b57cec5SDimitry Andric           // than needlessly accumulating lots of DBG_VALUEs.
2083349cc55cSDimitry Andric           if (hasRegisterDependency(&MI, UsedOpsInCopy, DefedRegsInCopy,
2084fe6060f1SDimitry Andric                                     ModifiedRegUnits, UsedRegUnits)) {
2085fe6060f1SDimitry Andric             IsValid = false;
2086fe6060f1SDimitry Andric             break;
2087fe6060f1SDimitry Andric           }
20880b57cec5SDimitry Andric 
20898bcb0991SDimitry Andric           // Record debug use of each reg unit.
209006c3fb27SDimitry Andric           for (MCRegUnit Unit : TRI->regunits(MO.getReg()))
209106c3fb27SDimitry Andric             MIUnits[Unit].push_back(MO.getReg());
2092fe6060f1SDimitry Andric         }
2093fe6060f1SDimitry Andric       }
2094fe6060f1SDimitry Andric       if (IsValid) {
209581ad6265SDimitry Andric         for (auto &RegOps : MIUnits)
209681ad6265SDimitry Andric           SeenDbgInstrs[RegOps.first].emplace_back(&MI,
209781ad6265SDimitry Andric                                                    std::move(RegOps.second));
20980b57cec5SDimitry Andric       }
20990b57cec5SDimitry Andric       continue;
21000b57cec5SDimitry Andric     }
21010b57cec5SDimitry Andric 
2102349cc55cSDimitry Andric     if (MI.isDebugOrPseudoInstr())
21030b57cec5SDimitry Andric       continue;
21040b57cec5SDimitry Andric 
21050b57cec5SDimitry Andric     // Do not move any instruction across function call.
2106349cc55cSDimitry Andric     if (MI.isCall())
21070b57cec5SDimitry Andric       return false;
21080b57cec5SDimitry Andric 
2109349cc55cSDimitry Andric     if (!MI.isCopy() || !MI.getOperand(0).isRenamable()) {
2110349cc55cSDimitry Andric       LiveRegUnits::accumulateUsedDefed(MI, ModifiedRegUnits, UsedRegUnits,
21110b57cec5SDimitry Andric                                         TRI);
21120b57cec5SDimitry Andric       continue;
21130b57cec5SDimitry Andric     }
21140b57cec5SDimitry Andric 
21150b57cec5SDimitry Andric     // Don't sink the COPY if it would violate a register dependency.
2116349cc55cSDimitry Andric     if (hasRegisterDependency(&MI, UsedOpsInCopy, DefedRegsInCopy,
21170b57cec5SDimitry Andric                               ModifiedRegUnits, UsedRegUnits)) {
2118349cc55cSDimitry Andric       LiveRegUnits::accumulateUsedDefed(MI, ModifiedRegUnits, UsedRegUnits,
21190b57cec5SDimitry Andric                                         TRI);
21200b57cec5SDimitry Andric       continue;
21210b57cec5SDimitry Andric     }
21220b57cec5SDimitry Andric     assert((!UsedOpsInCopy.empty() && !DefedRegsInCopy.empty()) &&
21230b57cec5SDimitry Andric            "Unexpect SrcReg or DefReg");
21240b57cec5SDimitry Andric     MachineBasicBlock *SuccBB =
21250b57cec5SDimitry Andric         getSingleLiveInSuccBB(CurBB, SinkableBBs, DefedRegsInCopy, TRI);
21260b57cec5SDimitry Andric     // Don't sink if we cannot find a single sinkable successor in which Reg
21270b57cec5SDimitry Andric     // is live-in.
21280b57cec5SDimitry Andric     if (!SuccBB) {
2129349cc55cSDimitry Andric       LiveRegUnits::accumulateUsedDefed(MI, ModifiedRegUnits, UsedRegUnits,
21300b57cec5SDimitry Andric                                         TRI);
21310b57cec5SDimitry Andric       continue;
21320b57cec5SDimitry Andric     }
21330b57cec5SDimitry Andric     assert((SuccBB->pred_size() == 1 && *SuccBB->pred_begin() == &CurBB) &&
21340b57cec5SDimitry Andric            "Unexpected predecessor");
21350b57cec5SDimitry Andric 
21368bcb0991SDimitry Andric     // Collect DBG_VALUEs that must sink with this copy. We've previously
21378bcb0991SDimitry Andric     // recorded which reg units that DBG_VALUEs read, if this instruction
21388bcb0991SDimitry Andric     // writes any of those units then the corresponding DBG_VALUEs must sink.
2139fe6060f1SDimitry Andric     MapVector<MachineInstr *, MIRegs::second_type> DbgValsToSinkMap;
214006c3fb27SDimitry Andric     for (auto &MO : MI.all_defs()) {
214106c3fb27SDimitry Andric       for (MCRegUnit Unit : TRI->regunits(MO.getReg())) {
214206c3fb27SDimitry Andric         for (const auto &MIRegs : SeenDbgInstrs.lookup(Unit)) {
2143fe6060f1SDimitry Andric           auto &Regs = DbgValsToSinkMap[MIRegs.first];
2144fe6060f1SDimitry Andric           for (unsigned Reg : MIRegs.second)
2145fe6060f1SDimitry Andric             Regs.push_back(Reg);
21460b57cec5SDimitry Andric         }
2147fe6060f1SDimitry Andric       }
2148fe6060f1SDimitry Andric     }
214981ad6265SDimitry Andric     auto DbgValsToSink = DbgValsToSinkMap.takeVector();
215081ad6265SDimitry Andric 
215181ad6265SDimitry Andric     LLVM_DEBUG(dbgs() << "Sink instr " << MI << "\tinto block " << *SuccBB);
215281ad6265SDimitry Andric 
215381ad6265SDimitry Andric     MachineBasicBlock::iterator InsertPos =
215481ad6265SDimitry Andric         SuccBB->SkipPHIsAndLabels(SuccBB->begin());
215581ad6265SDimitry Andric     if (blockPrologueInterferes(SuccBB, InsertPos, MI, TRI, TII, nullptr)) {
215681ad6265SDimitry Andric       LLVM_DEBUG(
215781ad6265SDimitry Andric           dbgs() << " *** Not sinking: prologue interference\n");
215881ad6265SDimitry Andric       continue;
215981ad6265SDimitry Andric     }
21600b57cec5SDimitry Andric 
21610b57cec5SDimitry Andric     // Clear the kill flag if SrcReg is killed between MI and the end of the
21620b57cec5SDimitry Andric     // block.
2163349cc55cSDimitry Andric     clearKillFlags(&MI, CurBB, UsedOpsInCopy, UsedRegUnits, TRI);
2164349cc55cSDimitry Andric     performSink(MI, *SuccBB, InsertPos, DbgValsToSink);
2165349cc55cSDimitry Andric     updateLiveIn(&MI, SuccBB, UsedOpsInCopy, DefedRegsInCopy);
21660b57cec5SDimitry Andric 
21670b57cec5SDimitry Andric     Changed = true;
21680b57cec5SDimitry Andric     ++NumPostRACopySink;
21690b57cec5SDimitry Andric   }
21700b57cec5SDimitry Andric   return Changed;
21710b57cec5SDimitry Andric }
21720b57cec5SDimitry Andric 
21730b57cec5SDimitry Andric bool PostRAMachineSinking::runOnMachineFunction(MachineFunction &MF) {
21740b57cec5SDimitry Andric   if (skipFunction(MF.getFunction()))
21750b57cec5SDimitry Andric     return false;
21760b57cec5SDimitry Andric 
21770b57cec5SDimitry Andric   bool Changed = false;
21780b57cec5SDimitry Andric   const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
21790b57cec5SDimitry Andric   const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
21800b57cec5SDimitry Andric 
21810b57cec5SDimitry Andric   ModifiedRegUnits.init(*TRI);
21820b57cec5SDimitry Andric   UsedRegUnits.init(*TRI);
21830b57cec5SDimitry Andric   for (auto &BB : MF)
21840b57cec5SDimitry Andric     Changed |= tryToSinkCopy(BB, MF, TRI, TII);
21850b57cec5SDimitry Andric 
21860b57cec5SDimitry Andric   return Changed;
21870b57cec5SDimitry Andric }
2188