xref: /src/contrib/llvm-project/llvm/lib/CodeGen/MachineSink.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1044eb2f6SDimitry Andric //===- MachineSink.cpp - Sinking for machine instructions -----------------===//
2009b1c42SEd Schouten //
3e6d15924SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e6d15924SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e6d15924SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6009b1c42SEd Schouten //
7009b1c42SEd Schouten //===----------------------------------------------------------------------===//
8009b1c42SEd Schouten //
966e41e3cSRoman Divacky // This pass moves instructions into successor blocks when possible, so that
1059850d08SRoman Divacky // they aren't executed on paths where their results aren't needed.
1159850d08SRoman Divacky //
1259850d08SRoman Divacky // This pass is not intended to be a replacement or a complete alternative
1359850d08SRoman Divacky // for an LLVM-IR-level sinking pass. It is only designed to sink simple
1459850d08SRoman Divacky // constructs that are not exposed before lowering and instruction selection.
15009b1c42SEd Schouten //
16009b1c42SEd Schouten //===----------------------------------------------------------------------===//
17009b1c42SEd Schouten 
18706b4fc4SDimitry Andric #include "llvm/ADT/DenseSet.h"
19145449b1SDimitry Andric #include "llvm/ADT/DepthFirstIterator.h"
20344a3780SDimitry Andric #include "llvm/ADT/MapVector.h"
21706b4fc4SDimitry Andric #include "llvm/ADT/PointerIntPair.h"
22145449b1SDimitry Andric #include "llvm/ADT/PostOrderIterator.h"
2367c32a98SDimitry Andric #include "llvm/ADT/SetVector.h"
24cf099d11SDimitry Andric #include "llvm/ADT/SmallSet.h"
25044eb2f6SDimitry Andric #include "llvm/ADT/SmallVector.h"
26009b1c42SEd Schouten #include "llvm/ADT/Statistic.h"
274a16efa3SDimitry Andric #include "llvm/Analysis/AliasAnalysis.h"
28145449b1SDimitry Andric #include "llvm/Analysis/CFG.h"
29b915e9e0SDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h"
3067c32a98SDimitry Andric #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
31b915e9e0SDimitry Andric #include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
32145449b1SDimitry Andric #include "llvm/CodeGen/MachineCycleAnalysis.h"
334a16efa3SDimitry Andric #include "llvm/CodeGen/MachineDominators.h"
34b915e9e0SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
35b915e9e0SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h"
36b915e9e0SDimitry Andric #include "llvm/CodeGen/MachineInstr.h"
374a16efa3SDimitry Andric #include "llvm/CodeGen/MachineLoopInfo.h"
38b915e9e0SDimitry Andric #include "llvm/CodeGen/MachineOperand.h"
3967c32a98SDimitry Andric #include "llvm/CodeGen/MachinePostDominators.h"
404a16efa3SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
41b60736ecSDimitry Andric #include "llvm/CodeGen/RegisterClassInfo.h"
42b60736ecSDimitry Andric #include "llvm/CodeGen/RegisterPressure.h"
43044eb2f6SDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h"
44b1c73532SDimitry Andric #include "llvm/CodeGen/TargetPassConfig.h"
45044eb2f6SDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h"
46044eb2f6SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h"
47044eb2f6SDimitry Andric #include "llvm/IR/BasicBlock.h"
48044eb2f6SDimitry Andric #include "llvm/IR/DebugInfoMetadata.h"
491d5ae102SDimitry Andric #include "llvm/IR/LLVMContext.h"
50706b4fc4SDimitry Andric #include "llvm/InitializePasses.h"
511d5ae102SDimitry Andric #include "llvm/MC/MCRegisterInfo.h"
52044eb2f6SDimitry Andric #include "llvm/Pass.h"
53044eb2f6SDimitry Andric #include "llvm/Support/BranchProbability.h"
54d39c594dSDimitry Andric #include "llvm/Support/CommandLine.h"
55009b1c42SEd Schouten #include "llvm/Support/Debug.h"
5659850d08SRoman Divacky #include "llvm/Support/raw_ostream.h"
57b915e9e0SDimitry Andric #include <algorithm>
58b915e9e0SDimitry Andric #include <cassert>
59b915e9e0SDimitry Andric #include <cstdint>
60b915e9e0SDimitry Andric #include <utility>
61b915e9e0SDimitry Andric #include <vector>
62b915e9e0SDimitry Andric 
63009b1c42SEd Schouten using namespace llvm;
64009b1c42SEd Schouten 
655ca98fd9SDimitry Andric #define DEBUG_TYPE "machine-sink"
665ca98fd9SDimitry Andric 
67d39c594dSDimitry Andric static cl::opt<bool>
68d39c594dSDimitry Andric SplitEdges("machine-sink-split",
69d39c594dSDimitry Andric            cl::desc("Split critical edges during machine sinking"),
70cf099d11SDimitry Andric            cl::init(true), cl::Hidden);
71d39c594dSDimitry Andric 
7267c32a98SDimitry Andric static cl::opt<bool>
7367c32a98SDimitry Andric UseBlockFreqInfo("machine-sink-bfi",
7467c32a98SDimitry Andric            cl::desc("Use block frequency info to find successors to sink"),
7567c32a98SDimitry Andric            cl::init(true), cl::Hidden);
7667c32a98SDimitry Andric 
77b915e9e0SDimitry Andric static cl::opt<unsigned> SplitEdgeProbabilityThreshold(
78b915e9e0SDimitry Andric     "machine-sink-split-probability-threshold",
79b915e9e0SDimitry Andric     cl::desc(
80b915e9e0SDimitry Andric         "Percentage threshold for splitting single-instruction critical edge. "
81b915e9e0SDimitry Andric         "If the branch threshold is higher than this threshold, we allow "
82b915e9e0SDimitry Andric         "speculative execution of up to 1 instruction to avoid branching to "
83b915e9e0SDimitry Andric         "splitted critical edge"),
84b915e9e0SDimitry Andric     cl::init(40), cl::Hidden);
8567c32a98SDimitry Andric 
86b60736ecSDimitry Andric static cl::opt<unsigned> SinkLoadInstsPerBlockThreshold(
87b60736ecSDimitry Andric     "machine-sink-load-instrs-threshold",
88b60736ecSDimitry Andric     cl::desc("Do not try to find alias store for a load if there is a in-path "
89b60736ecSDimitry Andric              "block whose instruction number is higher than this threshold."),
90b60736ecSDimitry Andric     cl::init(2000), cl::Hidden);
91b60736ecSDimitry Andric 
92b60736ecSDimitry Andric static cl::opt<unsigned> SinkLoadBlocksThreshold(
93b60736ecSDimitry Andric     "machine-sink-load-blocks-threshold",
94b60736ecSDimitry Andric     cl::desc("Do not try to find alias store for a load if the block number in "
95b60736ecSDimitry Andric              "the straight line is higher than this threshold."),
96b60736ecSDimitry Andric     cl::init(20), cl::Hidden);
97b60736ecSDimitry Andric 
98344a3780SDimitry Andric static cl::opt<bool>
99145449b1SDimitry Andric     SinkInstsIntoCycle("sink-insts-to-avoid-spills",
100145449b1SDimitry Andric                        cl::desc("Sink instructions into cycles to avoid "
101344a3780SDimitry Andric                                 "register spills"),
102344a3780SDimitry Andric                        cl::init(false), cl::Hidden);
103344a3780SDimitry Andric 
104145449b1SDimitry Andric static cl::opt<unsigned> SinkIntoCycleLimit(
105145449b1SDimitry Andric     "machine-sink-cycle-limit",
106145449b1SDimitry Andric     cl::desc("The maximum number of instructions considered for cycle sinking."),
107344a3780SDimitry Andric     cl::init(50), cl::Hidden);
108344a3780SDimitry Andric 
109009b1c42SEd Schouten STATISTIC(NumSunk,      "Number of machine instructions sunk");
110145449b1SDimitry Andric STATISTIC(NumCycleSunk,  "Number of machine instructions sunk into a cycle");
111d39c594dSDimitry Andric STATISTIC(NumSplit,     "Number of critical edges split");
112cf099d11SDimitry Andric STATISTIC(NumCoalesces, "Number of copies coalesced");
113eb11fae6SDimitry Andric STATISTIC(NumPostRACopySink, "Number of copies sunk after RA");
114009b1c42SEd Schouten 
115009b1c42SEd Schouten namespace {
116b915e9e0SDimitry Andric 
11736bf506aSRoman Divacky   class MachineSinking : public MachineFunctionPass {
118b1c73532SDimitry Andric     const TargetSubtargetInfo *STI = nullptr;
1197fa27ce4SDimitry Andric     const TargetInstrInfo *TII = nullptr;
1207fa27ce4SDimitry Andric     const TargetRegisterInfo *TRI = nullptr;
1217fa27ce4SDimitry Andric     MachineRegisterInfo *MRI = nullptr;      // Machine register information
1227fa27ce4SDimitry Andric     MachineDominatorTree *DT = nullptr;      // Machine dominator tree
1237fa27ce4SDimitry Andric     MachinePostDominatorTree *PDT = nullptr; // Machine post dominator tree
1247fa27ce4SDimitry Andric     MachineCycleInfo *CI = nullptr;
1257fa27ce4SDimitry Andric     MachineBlockFrequencyInfo *MBFI = nullptr;
1267fa27ce4SDimitry Andric     const MachineBranchProbabilityInfo *MBPI = nullptr;
1277fa27ce4SDimitry Andric     AliasAnalysis *AA = nullptr;
128b60736ecSDimitry Andric     RegisterClassInfo RegClassInfo;
129009b1c42SEd Schouten 
130cf099d11SDimitry Andric     // Remember which edges have been considered for breaking.
131cf099d11SDimitry Andric     SmallSet<std::pair<MachineBasicBlock*, MachineBasicBlock*>, 8>
132cf099d11SDimitry Andric     CEBCandidates;
133ac9a064cSDimitry Andric     // Memorize the register that also wanted to sink into the same block along
134ac9a064cSDimitry Andric     // a different critical edge.
135ac9a064cSDimitry Andric     // {register to sink, sink-to block} -> the first sink-from block.
136ac9a064cSDimitry Andric     // We're recording the first sink-from block because that (critical) edge
137ac9a064cSDimitry Andric     // was deferred until we see another register that's going to sink into the
138ac9a064cSDimitry Andric     // same block.
139ac9a064cSDimitry Andric     DenseMap<std::pair<Register, MachineBasicBlock *>, MachineBasicBlock *>
140ac9a064cSDimitry Andric         CEMergeCandidates;
14167c32a98SDimitry Andric     // Remember which edges we are about to split.
14267c32a98SDimitry Andric     // This is different from CEBCandidates since those edges
14367c32a98SDimitry Andric     // will be split.
14467c32a98SDimitry Andric     SetVector<std::pair<MachineBasicBlock *, MachineBasicBlock *>> ToSplit;
145cf099d11SDimitry Andric 
146c0981da4SDimitry Andric     DenseSet<Register> RegsToClearKillFlags;
1475a5ac124SDimitry Andric 
148044eb2f6SDimitry Andric     using AllSuccsCache =
149ac9a064cSDimitry Andric         SmallDenseMap<MachineBasicBlock *, SmallVector<MachineBasicBlock *, 4>>;
1503a0822f0SDimitry Andric 
151706b4fc4SDimitry Andric     /// DBG_VALUE pointer and flag. The flag is true if this DBG_VALUE is
152706b4fc4SDimitry Andric     /// post-dominated by another DBG_VALUE of the same variable location.
153706b4fc4SDimitry Andric     /// This is necessary to detect sequences such as:
154706b4fc4SDimitry Andric     ///     %0 = someinst
155706b4fc4SDimitry Andric     ///     DBG_VALUE %0, !123, !DIExpression()
156706b4fc4SDimitry Andric     ///     %1 = anotherinst
157706b4fc4SDimitry Andric     ///     DBG_VALUE %1, !123, !DIExpression()
158706b4fc4SDimitry Andric     /// Where if %0 were to sink, the DBG_VAUE should not sink with it, as that
159706b4fc4SDimitry Andric     /// would re-order assignments.
160706b4fc4SDimitry Andric     using SeenDbgUser = PointerIntPair<MachineInstr *, 1>;
161706b4fc4SDimitry Andric 
162706b4fc4SDimitry Andric     /// Record of DBG_VALUE uses of vregs in a block, so that we can identify
163706b4fc4SDimitry Andric     /// debug instructions to sink.
164706b4fc4SDimitry Andric     SmallDenseMap<unsigned, TinyPtrVector<SeenDbgUser>> SeenDbgUsers;
165706b4fc4SDimitry Andric 
166706b4fc4SDimitry Andric     /// Record of debug variables that have had their locations set in the
167706b4fc4SDimitry Andric     /// current block.
168706b4fc4SDimitry Andric     DenseSet<DebugVariable> SeenDbgVars;
169706b4fc4SDimitry Andric 
170b1c73532SDimitry Andric     DenseMap<std::pair<MachineBasicBlock *, MachineBasicBlock *>, bool>
171b60736ecSDimitry Andric         HasStoreCache;
172b1c73532SDimitry Andric 
173b1c73532SDimitry Andric     DenseMap<std::pair<MachineBasicBlock *, MachineBasicBlock *>,
174b1c73532SDimitry Andric              SmallVector<MachineInstr *>>
175b60736ecSDimitry Andric         StoreInstrCache;
176b60736ecSDimitry Andric 
177b60736ecSDimitry Andric     /// Cached BB's register pressure.
178b1c73532SDimitry Andric     DenseMap<const MachineBasicBlock *, std::vector<unsigned>>
179b1c73532SDimitry Andric         CachedRegisterPressure;
180b1c73532SDimitry Andric 
181b1c73532SDimitry Andric     bool EnableSinkAndFold;
182b60736ecSDimitry Andric 
183009b1c42SEd Schouten   public:
184009b1c42SEd Schouten     static char ID; // Pass identification
185b915e9e0SDimitry Andric 
MachineSinking()186cf099d11SDimitry Andric     MachineSinking() : MachineFunctionPass(ID) {
187cf099d11SDimitry Andric       initializeMachineSinkingPass(*PassRegistry::getPassRegistry());
188cf099d11SDimitry Andric     }
189009b1c42SEd Schouten 
1905ca98fd9SDimitry Andric     bool runOnMachineFunction(MachineFunction &MF) override;
191009b1c42SEd Schouten 
getAnalysisUsage(AnalysisUsage & AU) const1925ca98fd9SDimitry Andric     void getAnalysisUsage(AnalysisUsage &AU) const override {
193009b1c42SEd Schouten       MachineFunctionPass::getAnalysisUsage(AU);
194dd58ef01SDimitry Andric       AU.addRequired<AAResultsWrapperPass>();
195ac9a064cSDimitry Andric       AU.addRequired<MachineDominatorTreeWrapperPass>();
196ac9a064cSDimitry Andric       AU.addRequired<MachinePostDominatorTreeWrapperPass>();
197145449b1SDimitry Andric       AU.addRequired<MachineCycleInfoWrapperPass>();
198ac9a064cSDimitry Andric       AU.addRequired<MachineBranchProbabilityInfoWrapperPass>();
199145449b1SDimitry Andric       AU.addPreserved<MachineCycleInfoWrapperPass>();
200ac9a064cSDimitry Andric       AU.addPreserved<MachineLoopInfoWrapperPass>();
20167c32a98SDimitry Andric       if (UseBlockFreqInfo)
202ac9a064cSDimitry Andric         AU.addRequired<MachineBlockFrequencyInfoWrapperPass>();
203b1c73532SDimitry Andric       AU.addRequired<TargetPassConfig>();
204009b1c42SEd Schouten     }
205cf099d11SDimitry Andric 
releaseMemory()2065ca98fd9SDimitry Andric     void releaseMemory() override {
207cf099d11SDimitry Andric       CEBCandidates.clear();
208ac9a064cSDimitry Andric       CEMergeCandidates.clear();
209cf099d11SDimitry Andric     }
210cf099d11SDimitry Andric 
211009b1c42SEd Schouten   private:
212009b1c42SEd Schouten     bool ProcessBlock(MachineBasicBlock &MBB);
213706b4fc4SDimitry Andric     void ProcessDbgInst(MachineInstr &MI);
214ac9a064cSDimitry Andric     bool isLegalToBreakCriticalEdge(MachineInstr &MI, MachineBasicBlock *From,
215ac9a064cSDimitry Andric                                     MachineBasicBlock *To, bool BreakPHIEdge);
216ac9a064cSDimitry Andric     bool isWorthBreakingCriticalEdge(MachineInstr &MI, MachineBasicBlock *From,
217ac9a064cSDimitry Andric                                      MachineBasicBlock *To,
218ac9a064cSDimitry Andric                                      MachineBasicBlock *&DeferredFromBlock);
219044eb2f6SDimitry Andric 
220b60736ecSDimitry Andric     bool hasStoreBetween(MachineBasicBlock *From, MachineBasicBlock *To,
221b60736ecSDimitry Andric                          MachineInstr &MI);
222b60736ecSDimitry Andric 
223eb11fae6SDimitry Andric     /// Postpone the splitting of the given critical
22467c32a98SDimitry Andric     /// edge (\p From, \p To).
22567c32a98SDimitry Andric     ///
22667c32a98SDimitry Andric     /// We do not split the edges on the fly. Indeed, this invalidates
22767c32a98SDimitry Andric     /// the dominance information and thus triggers a lot of updates
22867c32a98SDimitry Andric     /// of that information underneath.
22967c32a98SDimitry Andric     /// Instead, we postpone all the splits after each iteration of
23067c32a98SDimitry Andric     /// the main loop. That way, the information is at least valid
23167c32a98SDimitry Andric     /// for the lifetime of an iteration.
23267c32a98SDimitry Andric     ///
23367c32a98SDimitry Andric     /// \return True if the edge is marked as toSplit, false otherwise.
23467c32a98SDimitry Andric     /// False can be returned if, for instance, this is not profitable.
23501095a5dSDimitry Andric     bool PostponeSplitCriticalEdge(MachineInstr &MI,
236cf099d11SDimitry Andric                                    MachineBasicBlock *From,
237cf099d11SDimitry Andric                                    MachineBasicBlock *To,
238cf099d11SDimitry Andric                                    bool BreakPHIEdge);
23901095a5dSDimitry Andric     bool SinkInstruction(MachineInstr &MI, bool &SawStore,
2403a0822f0SDimitry Andric                          AllSuccsCache &AllSuccessors);
241706b4fc4SDimitry Andric 
242706b4fc4SDimitry Andric     /// If we sink a COPY inst, some debug users of it's destination may no
243706b4fc4SDimitry Andric     /// longer be dominated by the COPY, and will eventually be dropped.
244706b4fc4SDimitry Andric     /// This is easily rectified by forwarding the non-dominated debug uses
245706b4fc4SDimitry Andric     /// to the copy source.
246706b4fc4SDimitry Andric     void SalvageUnsunkDebugUsersOfCopy(MachineInstr &,
247706b4fc4SDimitry Andric                                        MachineBasicBlock *TargetBlock);
248b60736ecSDimitry Andric     bool AllUsesDominatedByBlock(Register Reg, MachineBasicBlock *MBB,
249b60736ecSDimitry Andric                                  MachineBasicBlock *DefMBB, bool &BreakPHIEdge,
250b60736ecSDimitry Andric                                  bool &LocalUse) const;
25101095a5dSDimitry Andric     MachineBasicBlock *FindSuccToSinkTo(MachineInstr &MI, MachineBasicBlock *MBB,
2523a0822f0SDimitry Andric                bool &BreakPHIEdge, AllSuccsCache &AllSuccessors);
253344a3780SDimitry Andric 
254145449b1SDimitry Andric     void FindCycleSinkCandidates(MachineCycle *Cycle, MachineBasicBlock *BB,
255344a3780SDimitry Andric                                  SmallVectorImpl<MachineInstr *> &Candidates);
256145449b1SDimitry Andric     bool SinkIntoCycle(MachineCycle *Cycle, MachineInstr &I);
257344a3780SDimitry Andric 
258b60736ecSDimitry Andric     bool isProfitableToSinkTo(Register Reg, MachineInstr &MI,
25963faed5bSDimitry Andric                               MachineBasicBlock *MBB,
2603a0822f0SDimitry Andric                               MachineBasicBlock *SuccToSinkTo,
2613a0822f0SDimitry Andric                               AllSuccsCache &AllSuccessors);
26263faed5bSDimitry Andric 
26301095a5dSDimitry Andric     bool PerformTrivialForwardCoalescing(MachineInstr &MI,
264cf099d11SDimitry Andric                                          MachineBasicBlock *MBB);
2653a0822f0SDimitry Andric 
266b1c73532SDimitry Andric     bool PerformSinkAndFold(MachineInstr &MI, MachineBasicBlock *MBB);
267b1c73532SDimitry Andric 
2683a0822f0SDimitry Andric     SmallVector<MachineBasicBlock *, 4> &
26901095a5dSDimitry Andric     GetAllSortedSuccessors(MachineInstr &MI, MachineBasicBlock *MBB,
2703a0822f0SDimitry Andric                            AllSuccsCache &AllSuccessors) const;
271b60736ecSDimitry Andric 
272b1c73532SDimitry Andric     std::vector<unsigned> &getBBRegisterPressure(const MachineBasicBlock &MBB);
273b1c73532SDimitry Andric 
274b1c73532SDimitry Andric     bool registerPressureSetExceedsLimit(unsigned NRegs,
275b1c73532SDimitry Andric                                          const TargetRegisterClass *RC,
276b1c73532SDimitry Andric                                          const MachineBasicBlock &MBB);
277009b1c42SEd Schouten   };
278b915e9e0SDimitry Andric 
279009b1c42SEd Schouten } // end anonymous namespace
280009b1c42SEd Schouten 
281009b1c42SEd Schouten char MachineSinking::ID = 0;
282044eb2f6SDimitry Andric 
28363faed5bSDimitry Andric char &llvm::MachineSinkingID = MachineSinking::ID;
284044eb2f6SDimitry Andric 
285ab44ce3dSDimitry Andric INITIALIZE_PASS_BEGIN(MachineSinking, DEBUG_TYPE,
286cf099d11SDimitry Andric                       "Machine code sinking", false, false)
INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfoWrapperPass)287ac9a064cSDimitry Andric INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfoWrapperPass)
288ac9a064cSDimitry Andric INITIALIZE_PASS_DEPENDENCY(MachineDominatorTreeWrapperPass)
289145449b1SDimitry Andric INITIALIZE_PASS_DEPENDENCY(MachineCycleInfoWrapperPass)
290dd58ef01SDimitry Andric INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
291ab44ce3dSDimitry Andric INITIALIZE_PASS_END(MachineSinking, DEBUG_TYPE,
292cf099d11SDimitry Andric                     "Machine code sinking", false, false)
293009b1c42SEd Schouten 
2947fa27ce4SDimitry Andric /// Return true if a target defined block prologue instruction interferes
2957fa27ce4SDimitry Andric /// with a sink candidate.
2967fa27ce4SDimitry Andric static bool blockPrologueInterferes(const MachineBasicBlock *BB,
2977fa27ce4SDimitry Andric                                     MachineBasicBlock::const_iterator End,
2987fa27ce4SDimitry Andric                                     const MachineInstr &MI,
2997fa27ce4SDimitry Andric                                     const TargetRegisterInfo *TRI,
3007fa27ce4SDimitry Andric                                     const TargetInstrInfo *TII,
3017fa27ce4SDimitry Andric                                     const MachineRegisterInfo *MRI) {
3027fa27ce4SDimitry Andric   for (MachineBasicBlock::const_iterator PI = BB->getFirstNonPHI(); PI != End;
3037fa27ce4SDimitry Andric        ++PI) {
3047fa27ce4SDimitry Andric     // Only check target defined prologue instructions
3057fa27ce4SDimitry Andric     if (!TII->isBasicBlockPrologue(*PI))
3067fa27ce4SDimitry Andric       continue;
3077fa27ce4SDimitry Andric     for (auto &MO : MI.operands()) {
3087fa27ce4SDimitry Andric       if (!MO.isReg())
3097fa27ce4SDimitry Andric         continue;
3107fa27ce4SDimitry Andric       Register Reg = MO.getReg();
3117fa27ce4SDimitry Andric       if (!Reg)
3127fa27ce4SDimitry Andric         continue;
3137fa27ce4SDimitry Andric       if (MO.isUse()) {
314b1c73532SDimitry Andric         if (Reg.isPhysical() &&
315b1c73532SDimitry Andric             (TII->isIgnorableUse(MO) || (MRI && MRI->isConstantPhysReg(Reg))))
3167fa27ce4SDimitry Andric           continue;
3177fa27ce4SDimitry Andric         if (PI->modifiesRegister(Reg, TRI))
3187fa27ce4SDimitry Andric           return true;
3197fa27ce4SDimitry Andric       } else {
3207fa27ce4SDimitry Andric         if (PI->readsRegister(Reg, TRI))
3217fa27ce4SDimitry Andric           return true;
3227fa27ce4SDimitry Andric         // Check for interference with non-dead defs
323ac9a064cSDimitry Andric         auto *DefOp = PI->findRegisterDefOperand(Reg, TRI, false, true);
3247fa27ce4SDimitry Andric         if (DefOp && !DefOp->isDead())
3257fa27ce4SDimitry Andric           return true;
3267fa27ce4SDimitry Andric       }
3277fa27ce4SDimitry Andric     }
3287fa27ce4SDimitry Andric   }
3297fa27ce4SDimitry Andric 
3307fa27ce4SDimitry Andric   return false;
3317fa27ce4SDimitry Andric }
3327fa27ce4SDimitry Andric 
PerformTrivialForwardCoalescing(MachineInstr & MI,MachineBasicBlock * MBB)33301095a5dSDimitry Andric bool MachineSinking::PerformTrivialForwardCoalescing(MachineInstr &MI,
334cf099d11SDimitry Andric                                                      MachineBasicBlock *MBB) {
33501095a5dSDimitry Andric   if (!MI.isCopy())
336cf099d11SDimitry Andric     return false;
337cf099d11SDimitry Andric 
3381d5ae102SDimitry Andric   Register SrcReg = MI.getOperand(1).getReg();
3391d5ae102SDimitry Andric   Register DstReg = MI.getOperand(0).getReg();
340e3b55780SDimitry Andric   if (!SrcReg.isVirtual() || !DstReg.isVirtual() ||
341e3b55780SDimitry Andric       !MRI->hasOneNonDBGUse(SrcReg))
342cf099d11SDimitry Andric     return false;
343cf099d11SDimitry Andric 
344cf099d11SDimitry Andric   const TargetRegisterClass *SRC = MRI->getRegClass(SrcReg);
345cf099d11SDimitry Andric   const TargetRegisterClass *DRC = MRI->getRegClass(DstReg);
346cf099d11SDimitry Andric   if (SRC != DRC)
347cf099d11SDimitry Andric     return false;
348cf099d11SDimitry Andric 
349cf099d11SDimitry Andric   MachineInstr *DefMI = MRI->getVRegDef(SrcReg);
350cf099d11SDimitry Andric   if (DefMI->isCopyLike())
351cf099d11SDimitry Andric     return false;
352eb11fae6SDimitry Andric   LLVM_DEBUG(dbgs() << "Coalescing: " << *DefMI);
353eb11fae6SDimitry Andric   LLVM_DEBUG(dbgs() << "*** to: " << MI);
354cf099d11SDimitry Andric   MRI->replaceRegWith(DstReg, SrcReg);
35501095a5dSDimitry Andric   MI.eraseFromParent();
35667c32a98SDimitry Andric 
35767c32a98SDimitry Andric   // Conservatively, clear any kill flags, since it's possible that they are no
35867c32a98SDimitry Andric   // longer correct.
35967c32a98SDimitry Andric   MRI->clearKillFlags(SrcReg);
36067c32a98SDimitry Andric 
361cf099d11SDimitry Andric   ++NumCoalesces;
362cf099d11SDimitry Andric   return true;
363cf099d11SDimitry Andric }
364cf099d11SDimitry Andric 
PerformSinkAndFold(MachineInstr & MI,MachineBasicBlock * MBB)365b1c73532SDimitry Andric bool MachineSinking::PerformSinkAndFold(MachineInstr &MI,
366b1c73532SDimitry Andric                                         MachineBasicBlock *MBB) {
367b1c73532SDimitry Andric   if (MI.isCopy() || MI.mayLoadOrStore() ||
368b1c73532SDimitry Andric       MI.getOpcode() == TargetOpcode::REG_SEQUENCE)
369b1c73532SDimitry Andric     return false;
370b1c73532SDimitry Andric 
371b1c73532SDimitry Andric   // Don't sink instructions that the target prefers not to sink.
372b1c73532SDimitry Andric   if (!TII->shouldSink(MI))
373b1c73532SDimitry Andric     return false;
374b1c73532SDimitry Andric 
375b1c73532SDimitry Andric   // Check if it's safe to move the instruction.
376b1c73532SDimitry Andric   bool SawStore = true;
377b1c73532SDimitry Andric   if (!MI.isSafeToMove(AA, SawStore))
378b1c73532SDimitry Andric     return false;
379b1c73532SDimitry Andric 
380b1c73532SDimitry Andric   // Convergent operations may not be made control-dependent on additional
381b1c73532SDimitry Andric   // values.
382b1c73532SDimitry Andric   if (MI.isConvergent())
383b1c73532SDimitry Andric     return false;
384b1c73532SDimitry Andric 
385b1c73532SDimitry Andric   // Don't sink defs/uses of hard registers or if the instruction defines more
386b1c73532SDimitry Andric   // than one register.
387b1c73532SDimitry Andric   // Don't sink more than two register uses - it'll cover most of the cases and
388b1c73532SDimitry Andric   // greatly simplifies the register pressure checks.
389b1c73532SDimitry Andric   Register DefReg;
390b1c73532SDimitry Andric   Register UsedRegA, UsedRegB;
391b1c73532SDimitry Andric   for (const MachineOperand &MO : MI.operands()) {
392b1c73532SDimitry Andric     if (MO.isImm() || MO.isRegMask() || MO.isRegLiveOut() || MO.isMetadata() ||
393b1c73532SDimitry Andric         MO.isMCSymbol() || MO.isDbgInstrRef() || MO.isCFIIndex() ||
394b1c73532SDimitry Andric         MO.isIntrinsicID() || MO.isPredicate() || MO.isShuffleMask())
395b1c73532SDimitry Andric       continue;
396b1c73532SDimitry Andric     if (!MO.isReg())
397b1c73532SDimitry Andric       return false;
398b1c73532SDimitry Andric 
399b1c73532SDimitry Andric     Register Reg = MO.getReg();
400b1c73532SDimitry Andric     if (Reg == 0)
401b1c73532SDimitry Andric       continue;
402b1c73532SDimitry Andric 
403b1c73532SDimitry Andric     if (Reg.isVirtual()) {
404b1c73532SDimitry Andric       if (MO.isDef()) {
405b1c73532SDimitry Andric         if (DefReg)
406b1c73532SDimitry Andric           return false;
407b1c73532SDimitry Andric         DefReg = Reg;
408b1c73532SDimitry Andric         continue;
409b1c73532SDimitry Andric       }
410b1c73532SDimitry Andric 
411b1c73532SDimitry Andric       if (UsedRegA == 0)
412b1c73532SDimitry Andric         UsedRegA = Reg;
413b1c73532SDimitry Andric       else if (UsedRegB == 0)
414b1c73532SDimitry Andric         UsedRegB = Reg;
415b1c73532SDimitry Andric       else
416b1c73532SDimitry Andric         return false;
417b1c73532SDimitry Andric       continue;
418b1c73532SDimitry Andric     }
419b1c73532SDimitry Andric 
420ac9a064cSDimitry Andric     if (Reg.isPhysical() && MO.isUse() &&
421b1c73532SDimitry Andric         (MRI->isConstantPhysReg(Reg) || TII->isIgnorableUse(MO)))
422b1c73532SDimitry Andric       continue;
423b1c73532SDimitry Andric 
424b1c73532SDimitry Andric     return false;
425b1c73532SDimitry Andric   }
426b1c73532SDimitry Andric 
427b1c73532SDimitry Andric   // Scan uses of the destination register. Every use, except the last, must be
428b1c73532SDimitry Andric   // a copy, with a chain of copies terminating with either a copy into a hard
429b1c73532SDimitry Andric   // register, or a load/store instruction where the use is part of the
430b1c73532SDimitry Andric   // address (*not* the stored value).
431b1c73532SDimitry Andric   using SinkInfo = std::pair<MachineInstr *, ExtAddrMode>;
432b1c73532SDimitry Andric   SmallVector<SinkInfo> SinkInto;
433b1c73532SDimitry Andric   SmallVector<Register> Worklist;
434b1c73532SDimitry Andric 
435b1c73532SDimitry Andric   const TargetRegisterClass *RC = MRI->getRegClass(DefReg);
436b1c73532SDimitry Andric   const TargetRegisterClass *RCA =
437b1c73532SDimitry Andric       UsedRegA == 0 ? nullptr : MRI->getRegClass(UsedRegA);
438b1c73532SDimitry Andric   const TargetRegisterClass *RCB =
439b1c73532SDimitry Andric       UsedRegB == 0 ? nullptr : MRI->getRegClass(UsedRegB);
440b1c73532SDimitry Andric 
441b1c73532SDimitry Andric   Worklist.push_back(DefReg);
442b1c73532SDimitry Andric   while (!Worklist.empty()) {
443b1c73532SDimitry Andric     Register Reg = Worklist.pop_back_val();
444b1c73532SDimitry Andric 
445b1c73532SDimitry Andric     for (MachineOperand &MO : MRI->use_nodbg_operands(Reg)) {
446b1c73532SDimitry Andric       ExtAddrMode MaybeAM;
447b1c73532SDimitry Andric       MachineInstr &UseInst = *MO.getParent();
448b1c73532SDimitry Andric       if (UseInst.isCopy()) {
449b1c73532SDimitry Andric         Register DstReg;
450b1c73532SDimitry Andric         if (const MachineOperand &O = UseInst.getOperand(0); O.isReg())
451b1c73532SDimitry Andric           DstReg = O.getReg();
452b1c73532SDimitry Andric         if (DstReg == 0)
453b1c73532SDimitry Andric           return false;
454b1c73532SDimitry Andric         if (DstReg.isVirtual()) {
455b1c73532SDimitry Andric           Worklist.push_back(DstReg);
456b1c73532SDimitry Andric           continue;
457b1c73532SDimitry Andric         }
458b1c73532SDimitry Andric         // If we are going to replace a copy, the original instruction must be
459b1c73532SDimitry Andric         // as cheap as a copy.
460b1c73532SDimitry Andric         if (!TII->isAsCheapAsAMove(MI))
461b1c73532SDimitry Andric           return false;
462b1c73532SDimitry Andric         // The hard register must be in the register class of the original
463b1c73532SDimitry Andric         // instruction's destination register.
464b1c73532SDimitry Andric         if (!RC->contains(DstReg))
465b1c73532SDimitry Andric           return false;
466b1c73532SDimitry Andric       } else if (UseInst.mayLoadOrStore()) {
467b1c73532SDimitry Andric         ExtAddrMode AM;
468b1c73532SDimitry Andric         if (!TII->canFoldIntoAddrMode(UseInst, Reg, MI, AM))
469b1c73532SDimitry Andric           return false;
470b1c73532SDimitry Andric         MaybeAM = AM;
471b1c73532SDimitry Andric       } else {
472b1c73532SDimitry Andric         return false;
473b1c73532SDimitry Andric       }
474b1c73532SDimitry Andric 
475b1c73532SDimitry Andric       if (UseInst.getParent() != MI.getParent()) {
476b1c73532SDimitry Andric         // If the register class of the register we are replacing is a superset
477b1c73532SDimitry Andric         // of any of the register classes of the operands of the materialized
478b1c73532SDimitry Andric         // instruction don't consider that live range extended.
479b1c73532SDimitry Andric         const TargetRegisterClass *RCS = MRI->getRegClass(Reg);
480b1c73532SDimitry Andric         if (RCA && RCA->hasSuperClassEq(RCS))
481b1c73532SDimitry Andric           RCA = nullptr;
482b1c73532SDimitry Andric         else if (RCB && RCB->hasSuperClassEq(RCS))
483b1c73532SDimitry Andric           RCB = nullptr;
484b1c73532SDimitry Andric         if (RCA || RCB) {
485b1c73532SDimitry Andric           if (RCA == nullptr) {
486b1c73532SDimitry Andric             RCA = RCB;
487b1c73532SDimitry Andric             RCB = nullptr;
488b1c73532SDimitry Andric           }
489b1c73532SDimitry Andric 
490b1c73532SDimitry Andric           unsigned NRegs = !!RCA + !!RCB;
491b1c73532SDimitry Andric           if (RCA == RCB)
492b1c73532SDimitry Andric             RCB = nullptr;
493b1c73532SDimitry Andric 
494b1c73532SDimitry Andric           // Check we don't exceed register pressure at the destination.
495b1c73532SDimitry Andric           const MachineBasicBlock &MBB = *UseInst.getParent();
496b1c73532SDimitry Andric           if (RCB == nullptr) {
497b1c73532SDimitry Andric             if (registerPressureSetExceedsLimit(NRegs, RCA, MBB))
498b1c73532SDimitry Andric               return false;
499b1c73532SDimitry Andric           } else if (registerPressureSetExceedsLimit(1, RCA, MBB) ||
500b1c73532SDimitry Andric                      registerPressureSetExceedsLimit(1, RCB, MBB)) {
501b1c73532SDimitry Andric             return false;
502b1c73532SDimitry Andric           }
503b1c73532SDimitry Andric         }
504b1c73532SDimitry Andric       }
505b1c73532SDimitry Andric 
506b1c73532SDimitry Andric       SinkInto.emplace_back(&UseInst, MaybeAM);
507b1c73532SDimitry Andric     }
508b1c73532SDimitry Andric   }
509b1c73532SDimitry Andric 
510b1c73532SDimitry Andric   if (SinkInto.empty())
511b1c73532SDimitry Andric     return false;
512b1c73532SDimitry Andric 
513b1c73532SDimitry Andric   // Now we know we can fold the instruction in all its users.
514b1c73532SDimitry Andric   for (auto &[SinkDst, MaybeAM] : SinkInto) {
515b1c73532SDimitry Andric     MachineInstr *New = nullptr;
516b1c73532SDimitry Andric     LLVM_DEBUG(dbgs() << "Sinking copy of"; MI.dump(); dbgs() << "into";
517b1c73532SDimitry Andric                SinkDst->dump());
518b1c73532SDimitry Andric     if (SinkDst->isCopy()) {
519b1c73532SDimitry Andric       // TODO: After performing the sink-and-fold, the original instruction is
520b1c73532SDimitry Andric       // deleted. Its value is still available (in a hard register), so if there
521b1c73532SDimitry Andric       // are debug instructions which refer to the (now deleted) virtual
522b1c73532SDimitry Andric       // register they could be updated to refer to the hard register, in
523b1c73532SDimitry Andric       // principle. However, it's not clear how to do that, moreover in some
524b1c73532SDimitry Andric       // cases the debug instructions may need to be replicated proportionally
525b1c73532SDimitry Andric       // to the number of the COPY instructions replaced and in some extreme
526b1c73532SDimitry Andric       // cases we can end up with quadratic increase in the number of debug
527b1c73532SDimitry Andric       // instructions.
528b1c73532SDimitry Andric 
529b1c73532SDimitry Andric       // Sink a copy of the instruction, replacing a COPY instruction.
530b1c73532SDimitry Andric       MachineBasicBlock::iterator InsertPt = SinkDst->getIterator();
531b1c73532SDimitry Andric       Register DstReg = SinkDst->getOperand(0).getReg();
532b1c73532SDimitry Andric       TII->reMaterialize(*SinkDst->getParent(), InsertPt, DstReg, 0, MI, *TRI);
533b1c73532SDimitry Andric       New = &*std::prev(InsertPt);
534b1c73532SDimitry Andric       if (!New->getDebugLoc())
535b1c73532SDimitry Andric         New->setDebugLoc(SinkDst->getDebugLoc());
536312c0ed1SDimitry Andric 
537312c0ed1SDimitry Andric       // The operand registers of the "sunk" instruction have their live range
538312c0ed1SDimitry Andric       // extended and their kill flags may no longer be correct. Conservatively
539312c0ed1SDimitry Andric       // clear the kill flags.
540312c0ed1SDimitry Andric       if (UsedRegA)
541312c0ed1SDimitry Andric         MRI->clearKillFlags(UsedRegA);
542312c0ed1SDimitry Andric       if (UsedRegB)
543312c0ed1SDimitry Andric         MRI->clearKillFlags(UsedRegB);
544b1c73532SDimitry Andric     } else {
545b1c73532SDimitry Andric       // Fold instruction into the addressing mode of a memory instruction.
546b1c73532SDimitry Andric       New = TII->emitLdStWithAddr(*SinkDst, MaybeAM);
547312c0ed1SDimitry Andric 
548312c0ed1SDimitry Andric       // The registers of the addressing mode may have their live range extended
549312c0ed1SDimitry Andric       // and their kill flags may no longer be correct. Conservatively clear the
550312c0ed1SDimitry Andric       // kill flags.
551312c0ed1SDimitry Andric       if (Register R = MaybeAM.BaseReg; R.isValid() && R.isVirtual())
552312c0ed1SDimitry Andric         MRI->clearKillFlags(R);
553312c0ed1SDimitry Andric       if (Register R = MaybeAM.ScaledReg; R.isValid() && R.isVirtual())
554312c0ed1SDimitry Andric         MRI->clearKillFlags(R);
555b1c73532SDimitry Andric     }
556b1c73532SDimitry Andric     LLVM_DEBUG(dbgs() << "yielding"; New->dump());
557b1c73532SDimitry Andric     // Clear the StoreInstrCache, since we may invalidate it by erasing.
558b1c73532SDimitry Andric     if (SinkDst->mayStore() && !SinkDst->hasOrderedMemoryRef())
559b1c73532SDimitry Andric       StoreInstrCache.clear();
560b1c73532SDimitry Andric     SinkDst->eraseFromParent();
561b1c73532SDimitry Andric   }
562b1c73532SDimitry Andric 
563b1c73532SDimitry Andric   // Collect operands that need to be cleaned up because the registers no longer
564b1c73532SDimitry Andric   // exist (in COPYs and debug instructions). We cannot delete instructions or
565b1c73532SDimitry Andric   // clear operands while traversing register uses.
566b1c73532SDimitry Andric   SmallVector<MachineOperand *> Cleanup;
567b1c73532SDimitry Andric   Worklist.push_back(DefReg);
568b1c73532SDimitry Andric   while (!Worklist.empty()) {
569b1c73532SDimitry Andric     Register Reg = Worklist.pop_back_val();
570b1c73532SDimitry Andric     for (MachineOperand &MO : MRI->use_operands(Reg)) {
571b1c73532SDimitry Andric       MachineInstr *U = MO.getParent();
572b1c73532SDimitry Andric       assert((U->isCopy() || U->isDebugInstr()) &&
573b1c73532SDimitry Andric              "Only debug uses and copies must remain");
574b1c73532SDimitry Andric       if (U->isCopy())
575b1c73532SDimitry Andric         Worklist.push_back(U->getOperand(0).getReg());
576b1c73532SDimitry Andric       Cleanup.push_back(&MO);
577b1c73532SDimitry Andric     }
578b1c73532SDimitry Andric   }
579b1c73532SDimitry Andric 
580b1c73532SDimitry Andric   // Delete the dead COPYs and clear operands in debug instructions
581b1c73532SDimitry Andric   for (MachineOperand *MO : Cleanup) {
582b1c73532SDimitry Andric     MachineInstr *I = MO->getParent();
583b1c73532SDimitry Andric     if (I->isCopy()) {
584b1c73532SDimitry Andric       I->eraseFromParent();
585b1c73532SDimitry Andric     } else {
586b1c73532SDimitry Andric       MO->setReg(0);
587b1c73532SDimitry Andric       MO->setSubReg(0);
588b1c73532SDimitry Andric     }
589b1c73532SDimitry Andric   }
590b1c73532SDimitry Andric 
591b1c73532SDimitry Andric   MI.eraseFromParent();
592b1c73532SDimitry Andric   return true;
593b1c73532SDimitry Andric }
594b1c73532SDimitry Andric 
595009b1c42SEd Schouten /// AllUsesDominatedByBlock - Return true if all uses of the specified register
596d39c594dSDimitry Andric /// occur in blocks dominated by the specified block. If any use is in the
597d39c594dSDimitry Andric /// definition block, then return false since it is never legal to move def
598d39c594dSDimitry Andric /// after uses.
AllUsesDominatedByBlock(Register Reg,MachineBasicBlock * MBB,MachineBasicBlock * DefMBB,bool & BreakPHIEdge,bool & LocalUse) const599b60736ecSDimitry Andric bool MachineSinking::AllUsesDominatedByBlock(Register Reg,
600d39c594dSDimitry Andric                                              MachineBasicBlock *MBB,
601d39c594dSDimitry Andric                                              MachineBasicBlock *DefMBB,
602cf099d11SDimitry Andric                                              bool &BreakPHIEdge,
603d39c594dSDimitry Andric                                              bool &LocalUse) const {
604e3b55780SDimitry Andric   assert(Reg.isVirtual() && "Only makes sense for vregs");
605cf099d11SDimitry Andric 
60663faed5bSDimitry Andric   // Ignore debug uses because debug info doesn't affect the code.
607cf099d11SDimitry Andric   if (MRI->use_nodbg_empty(Reg))
608cf099d11SDimitry Andric     return true;
609cf099d11SDimitry Andric 
610cf099d11SDimitry Andric   // BreakPHIEdge is true if all the uses are in the successor MBB being sunken
611cf099d11SDimitry Andric   // into and they are all PHI nodes. In this case, machine-sink must break
612cf099d11SDimitry Andric   // the critical edge first. e.g.
613cf099d11SDimitry Andric   //
614cfca06d7SDimitry Andric   // %bb.1:
615044eb2f6SDimitry Andric   //   Predecessors according to CFG: %bb.0
616cf099d11SDimitry Andric   //     ...
617cfca06d7SDimitry Andric   //     %def = DEC64_32r %x, implicit-def dead %eflags
618cf099d11SDimitry Andric   //     ...
619044eb2f6SDimitry Andric   //     JE_4 <%bb.37>, implicit %eflags
620044eb2f6SDimitry Andric   //   Successors according to CFG: %bb.37 %bb.2
621cf099d11SDimitry Andric   //
622cfca06d7SDimitry Andric   // %bb.2:
623cfca06d7SDimitry Andric   //     %p = PHI %y, %bb.0, %def, %bb.1
624cfca06d7SDimitry Andric   if (all_of(MRI->use_nodbg_operands(Reg), [&](MachineOperand &MO) {
6255ca98fd9SDimitry Andric         MachineInstr *UseInst = MO.getParent();
6267fa27ce4SDimitry Andric         unsigned OpNo = MO.getOperandNo();
627cf099d11SDimitry Andric         MachineBasicBlock *UseBlock = UseInst->getParent();
628cfca06d7SDimitry Andric         return UseBlock == MBB && UseInst->isPHI() &&
629cfca06d7SDimitry Andric                UseInst->getOperand(OpNo + 1).getMBB() == DefMBB;
630cfca06d7SDimitry Andric       })) {
631cfca06d7SDimitry Andric     BreakPHIEdge = true;
632cf099d11SDimitry Andric     return true;
633cfca06d7SDimitry Andric   }
634cf099d11SDimitry Andric 
6355ca98fd9SDimitry Andric   for (MachineOperand &MO : MRI->use_nodbg_operands(Reg)) {
636009b1c42SEd Schouten     // Determine the block of the use.
6375ca98fd9SDimitry Andric     MachineInstr *UseInst = MO.getParent();
6385ca98fd9SDimitry Andric     unsigned OpNo = &MO - &UseInst->getOperand(0);
639009b1c42SEd Schouten     MachineBasicBlock *UseBlock = UseInst->getParent();
6406fe5c7aaSRoman Divacky     if (UseInst->isPHI()) {
641009b1c42SEd Schouten       // PHI nodes use the operand in the predecessor block, not the block with
642009b1c42SEd Schouten       // the PHI.
6435ca98fd9SDimitry Andric       UseBlock = UseInst->getOperand(OpNo+1).getMBB();
644d39c594dSDimitry Andric     } else if (UseBlock == DefMBB) {
645d39c594dSDimitry Andric       LocalUse = true;
646d39c594dSDimitry Andric       return false;
647009b1c42SEd Schouten     }
64866e41e3cSRoman Divacky 
649009b1c42SEd Schouten     // Check that it dominates.
650009b1c42SEd Schouten     if (!DT->dominates(MBB, UseBlock))
651009b1c42SEd Schouten       return false;
652009b1c42SEd Schouten   }
65366e41e3cSRoman Divacky 
654009b1c42SEd Schouten   return true;
655009b1c42SEd Schouten }
656009b1c42SEd Schouten 
657344a3780SDimitry Andric /// Return true if this machine instruction loads from global offset table or
658344a3780SDimitry Andric /// constant pool.
mayLoadFromGOTOrConstantPool(MachineInstr & MI)659344a3780SDimitry Andric static bool mayLoadFromGOTOrConstantPool(MachineInstr &MI) {
660344a3780SDimitry Andric   assert(MI.mayLoad() && "Expected MI that loads!");
661344a3780SDimitry Andric 
662344a3780SDimitry Andric   // If we lost memory operands, conservatively assume that the instruction
663344a3780SDimitry Andric   // reads from everything..
664344a3780SDimitry Andric   if (MI.memoperands_empty())
665344a3780SDimitry Andric     return true;
666344a3780SDimitry Andric 
667344a3780SDimitry Andric   for (MachineMemOperand *MemOp : MI.memoperands())
668344a3780SDimitry Andric     if (const PseudoSourceValue *PSV = MemOp->getPseudoValue())
669344a3780SDimitry Andric       if (PSV->isGOT() || PSV->isConstantPool())
670344a3780SDimitry Andric         return true;
671344a3780SDimitry Andric 
672344a3780SDimitry Andric   return false;
673344a3780SDimitry Andric }
674344a3780SDimitry Andric 
FindCycleSinkCandidates(MachineCycle * Cycle,MachineBasicBlock * BB,SmallVectorImpl<MachineInstr * > & Candidates)675145449b1SDimitry Andric void MachineSinking::FindCycleSinkCandidates(
676145449b1SDimitry Andric     MachineCycle *Cycle, MachineBasicBlock *BB,
677344a3780SDimitry Andric     SmallVectorImpl<MachineInstr *> &Candidates) {
678344a3780SDimitry Andric   for (auto &MI : *BB) {
679145449b1SDimitry Andric     LLVM_DEBUG(dbgs() << "CycleSink: Analysing candidate: " << MI);
680344a3780SDimitry Andric     if (!TII->shouldSink(MI)) {
681145449b1SDimitry Andric       LLVM_DEBUG(dbgs() << "CycleSink: Instruction not a candidate for this "
682344a3780SDimitry Andric                            "target\n");
683344a3780SDimitry Andric       continue;
684344a3780SDimitry Andric     }
685145449b1SDimitry Andric     if (!isCycleInvariant(Cycle, MI)) {
686145449b1SDimitry Andric       LLVM_DEBUG(dbgs() << "CycleSink: Instruction is not cycle invariant\n");
687344a3780SDimitry Andric       continue;
688344a3780SDimitry Andric     }
689344a3780SDimitry Andric     bool DontMoveAcrossStore = true;
690344a3780SDimitry Andric     if (!MI.isSafeToMove(AA, DontMoveAcrossStore)) {
691145449b1SDimitry Andric       LLVM_DEBUG(dbgs() << "CycleSink: Instruction not safe to move.\n");
692344a3780SDimitry Andric       continue;
693344a3780SDimitry Andric     }
694344a3780SDimitry Andric     if (MI.mayLoad() && !mayLoadFromGOTOrConstantPool(MI)) {
695145449b1SDimitry Andric       LLVM_DEBUG(dbgs() << "CycleSink: Dont sink GOT or constant pool loads\n");
696344a3780SDimitry Andric       continue;
697344a3780SDimitry Andric     }
698344a3780SDimitry Andric     if (MI.isConvergent())
699344a3780SDimitry Andric       continue;
700344a3780SDimitry Andric 
701344a3780SDimitry Andric     const MachineOperand &MO = MI.getOperand(0);
702344a3780SDimitry Andric     if (!MO.isReg() || !MO.getReg() || !MO.isDef())
703344a3780SDimitry Andric       continue;
704344a3780SDimitry Andric     if (!MRI->hasOneDef(MO.getReg()))
705344a3780SDimitry Andric       continue;
706344a3780SDimitry Andric 
707145449b1SDimitry Andric     LLVM_DEBUG(dbgs() << "CycleSink: Instruction added as candidate.\n");
708344a3780SDimitry Andric     Candidates.push_back(&MI);
709344a3780SDimitry Andric   }
710344a3780SDimitry Andric }
711344a3780SDimitry Andric 
runOnMachineFunction(MachineFunction & MF)712009b1c42SEd Schouten bool MachineSinking::runOnMachineFunction(MachineFunction &MF) {
713044eb2f6SDimitry Andric   if (skipFunction(MF.getFunction()))
7145ca98fd9SDimitry Andric     return false;
7155ca98fd9SDimitry Andric 
716eb11fae6SDimitry Andric   LLVM_DEBUG(dbgs() << "******** Machine Sinking ********\n");
717009b1c42SEd Schouten 
718b1c73532SDimitry Andric   STI = &MF.getSubtarget();
719b1c73532SDimitry Andric   TII = STI->getInstrInfo();
720b1c73532SDimitry Andric   TRI = STI->getRegisterInfo();
721cf099d11SDimitry Andric   MRI = &MF.getRegInfo();
722ac9a064cSDimitry Andric   DT = &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
723ac9a064cSDimitry Andric   PDT = &getAnalysis<MachinePostDominatorTreeWrapperPass>().getPostDomTree();
724145449b1SDimitry Andric   CI = &getAnalysis<MachineCycleInfoWrapperPass>().getCycleInfo();
725ac9a064cSDimitry Andric   MBFI = UseBlockFreqInfo
726ac9a064cSDimitry Andric              ? &getAnalysis<MachineBlockFrequencyInfoWrapperPass>().getMBFI()
727ac9a064cSDimitry Andric              : nullptr;
728ac9a064cSDimitry Andric   MBPI = &getAnalysis<MachineBranchProbabilityInfoWrapperPass>().getMBPI();
729dd58ef01SDimitry Andric   AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
730b60736ecSDimitry Andric   RegClassInfo.runOnMachineFunction(MF);
731b1c73532SDimitry Andric   TargetPassConfig *PassConfig = &getAnalysis<TargetPassConfig>();
732b1c73532SDimitry Andric   EnableSinkAndFold = PassConfig->getEnableSinkAndFold();
733009b1c42SEd Schouten 
734009b1c42SEd Schouten   bool EverMadeChange = false;
735009b1c42SEd Schouten 
736b915e9e0SDimitry Andric   while (true) {
737009b1c42SEd Schouten     bool MadeChange = false;
738009b1c42SEd Schouten 
739009b1c42SEd Schouten     // Process all basic blocks.
740cf099d11SDimitry Andric     CEBCandidates.clear();
741ac9a064cSDimitry Andric     CEMergeCandidates.clear();
74267c32a98SDimitry Andric     ToSplit.clear();
7433a0822f0SDimitry Andric     for (auto &MBB: MF)
7443a0822f0SDimitry Andric       MadeChange |= ProcessBlock(MBB);
745009b1c42SEd Schouten 
74667c32a98SDimitry Andric     // If we have anything we marked as toSplit, split it now.
7474b4fe385SDimitry Andric     for (const auto &Pair : ToSplit) {
74801095a5dSDimitry Andric       auto NewSucc = Pair.first->SplitCriticalEdge(Pair.second, *this);
74967c32a98SDimitry Andric       if (NewSucc != nullptr) {
750eb11fae6SDimitry Andric         LLVM_DEBUG(dbgs() << " *** Splitting critical edge: "
751044eb2f6SDimitry Andric                           << printMBBReference(*Pair.first) << " -- "
752044eb2f6SDimitry Andric                           << printMBBReference(*NewSucc) << " -- "
753044eb2f6SDimitry Andric                           << printMBBReference(*Pair.second) << '\n');
754b60736ecSDimitry Andric         if (MBFI)
755b60736ecSDimitry Andric           MBFI->onEdgeSplit(*Pair.first, *NewSucc, *MBPI);
756b60736ecSDimitry Andric 
75767c32a98SDimitry Andric         MadeChange = true;
75867c32a98SDimitry Andric         ++NumSplit;
759b1c73532SDimitry Andric         CI->splitCriticalEdge(Pair.first, Pair.second, NewSucc);
76067c32a98SDimitry Andric       } else
761eb11fae6SDimitry Andric         LLVM_DEBUG(dbgs() << " *** Not legal to break critical edge\n");
76267c32a98SDimitry Andric     }
763009b1c42SEd Schouten     // If this iteration over the code changed anything, keep iterating.
764009b1c42SEd Schouten     if (!MadeChange) break;
765009b1c42SEd Schouten     EverMadeChange = true;
766009b1c42SEd Schouten   }
7675a5ac124SDimitry Andric 
768145449b1SDimitry Andric   if (SinkInstsIntoCycle) {
769145449b1SDimitry Andric     SmallVector<MachineCycle *, 8> Cycles(CI->toplevel_begin(),
770145449b1SDimitry Andric                                           CI->toplevel_end());
771145449b1SDimitry Andric     for (auto *Cycle : Cycles) {
772145449b1SDimitry Andric       MachineBasicBlock *Preheader = Cycle->getCyclePreheader();
773344a3780SDimitry Andric       if (!Preheader) {
774145449b1SDimitry Andric         LLVM_DEBUG(dbgs() << "CycleSink: Can't find preheader\n");
775344a3780SDimitry Andric         continue;
776344a3780SDimitry Andric       }
777344a3780SDimitry Andric       SmallVector<MachineInstr *, 8> Candidates;
778145449b1SDimitry Andric       FindCycleSinkCandidates(Cycle, Preheader, Candidates);
779344a3780SDimitry Andric 
780344a3780SDimitry Andric       // Walk the candidates in reverse order so that we start with the use
781344a3780SDimitry Andric       // of a def-use chain, if there is any.
782344a3780SDimitry Andric       // TODO: Sort the candidates using a cost-model.
783344a3780SDimitry Andric       unsigned i = 0;
784c0981da4SDimitry Andric       for (MachineInstr *I : llvm::reverse(Candidates)) {
785145449b1SDimitry Andric         if (i++ == SinkIntoCycleLimit) {
786145449b1SDimitry Andric           LLVM_DEBUG(dbgs() << "CycleSink:   Limit reached of instructions to "
787344a3780SDimitry Andric                                "be analysed.");
788344a3780SDimitry Andric           break;
789344a3780SDimitry Andric         }
790344a3780SDimitry Andric 
791145449b1SDimitry Andric         if (!SinkIntoCycle(Cycle, *I))
792344a3780SDimitry Andric           break;
793344a3780SDimitry Andric         EverMadeChange = true;
794145449b1SDimitry Andric         ++NumCycleSunk;
795344a3780SDimitry Andric       }
796344a3780SDimitry Andric     }
797344a3780SDimitry Andric   }
798344a3780SDimitry Andric 
799b60736ecSDimitry Andric   HasStoreCache.clear();
800b60736ecSDimitry Andric   StoreInstrCache.clear();
801b60736ecSDimitry Andric 
8025a5ac124SDimitry Andric   // Now clear any kill flags for recorded registers.
8035a5ac124SDimitry Andric   for (auto I : RegsToClearKillFlags)
8045a5ac124SDimitry Andric     MRI->clearKillFlags(I);
8055a5ac124SDimitry Andric   RegsToClearKillFlags.clear();
8065a5ac124SDimitry Andric 
807009b1c42SEd Schouten   return EverMadeChange;
808009b1c42SEd Schouten }
809009b1c42SEd Schouten 
ProcessBlock(MachineBasicBlock & MBB)810009b1c42SEd Schouten bool MachineSinking::ProcessBlock(MachineBasicBlock &MBB) {
811b1c73532SDimitry Andric   if ((!EnableSinkAndFold && MBB.succ_size() <= 1) || MBB.empty())
812b1c73532SDimitry Andric     return false;
813009b1c42SEd Schouten 
8149f4a1da9SRoman Divacky   // Don't bother sinking code out of unreachable blocks. In addition to being
81566e41e3cSRoman Divacky   // unprofitable, it can also lead to infinite looping, because in an
816145449b1SDimitry Andric   // unreachable cycle there may be nowhere to stop.
8179f4a1da9SRoman Divacky   if (!DT->isReachableFromEntry(&MBB)) return false;
8189f4a1da9SRoman Divacky 
819009b1c42SEd Schouten   bool MadeChange = false;
820009b1c42SEd Schouten 
821145449b1SDimitry Andric   // Cache all successors, sorted by frequency info and cycle depth.
8223a0822f0SDimitry Andric   AllSuccsCache AllSuccessors;
8233a0822f0SDimitry Andric 
824009b1c42SEd Schouten   // Walk the basic block bottom-up.  Remember if we saw a store.
825009b1c42SEd Schouten   MachineBasicBlock::iterator I = MBB.end();
826009b1c42SEd Schouten   --I;
827009b1c42SEd Schouten   bool ProcessedBegin, SawStore = false;
828009b1c42SEd Schouten   do {
82901095a5dSDimitry Andric     MachineInstr &MI = *I;  // The instruction to sink.
830009b1c42SEd Schouten 
831009b1c42SEd Schouten     // Predecrement I (if it's not begin) so that it isn't invalidated by
832009b1c42SEd Schouten     // sinking.
833009b1c42SEd Schouten     ProcessedBegin = I == MBB.begin();
834009b1c42SEd Schouten     if (!ProcessedBegin)
835009b1c42SEd Schouten       --I;
836009b1c42SEd Schouten 
837344a3780SDimitry Andric     if (MI.isDebugOrPseudoInstr()) {
838706b4fc4SDimitry Andric       if (MI.isDebugValue())
839706b4fc4SDimitry Andric         ProcessDbgInst(MI);
840f5a3459aSRoman Divacky       continue;
841706b4fc4SDimitry Andric     }
842f5a3459aSRoman Divacky 
843b1c73532SDimitry Andric     if (EnableSinkAndFold && PerformSinkAndFold(MI, &MBB)) {
844b1c73532SDimitry Andric       MadeChange = true;
845b1c73532SDimitry Andric       continue;
846b1c73532SDimitry Andric     }
847b1c73532SDimitry Andric 
848b1c73532SDimitry Andric     // Can't sink anything out of a block that has less than two successors.
849b1c73532SDimitry Andric     if (MBB.succ_size() <= 1)
850b1c73532SDimitry Andric       continue;
851b1c73532SDimitry Andric 
852b1c73532SDimitry Andric     if (PerformTrivialForwardCoalescing(MI, &MBB)) {
8536b943ff3SDimitry Andric       MadeChange = true;
854cf099d11SDimitry Andric       continue;
8556b943ff3SDimitry Andric     }
856cf099d11SDimitry Andric 
85701095a5dSDimitry Andric     if (SinkInstruction(MI, SawStore, AllSuccessors)) {
85801095a5dSDimitry Andric       ++NumSunk;
85901095a5dSDimitry Andric       MadeChange = true;
86001095a5dSDimitry Andric     }
861009b1c42SEd Schouten 
862009b1c42SEd Schouten     // If we just processed the first instruction in the block, we're done.
863009b1c42SEd Schouten   } while (!ProcessedBegin);
864009b1c42SEd Schouten 
865706b4fc4SDimitry Andric   SeenDbgUsers.clear();
866706b4fc4SDimitry Andric   SeenDbgVars.clear();
867b60736ecSDimitry Andric   // recalculate the bb register pressure after sinking one BB.
868b60736ecSDimitry Andric   CachedRegisterPressure.clear();
869009b1c42SEd Schouten   return MadeChange;
870009b1c42SEd Schouten }
871009b1c42SEd Schouten 
ProcessDbgInst(MachineInstr & MI)872706b4fc4SDimitry Andric void MachineSinking::ProcessDbgInst(MachineInstr &MI) {
873706b4fc4SDimitry Andric   // When we see DBG_VALUEs for registers, record any vreg it reads, so that
874706b4fc4SDimitry Andric   // we know what to sink if the vreg def sinks.
875706b4fc4SDimitry Andric   assert(MI.isDebugValue() && "Expected DBG_VALUE for processing");
876706b4fc4SDimitry Andric 
877706b4fc4SDimitry Andric   DebugVariable Var(MI.getDebugVariable(), MI.getDebugExpression(),
878706b4fc4SDimitry Andric                     MI.getDebugLoc()->getInlinedAt());
879b60736ecSDimitry Andric   bool SeenBefore = SeenDbgVars.contains(Var);
880706b4fc4SDimitry Andric 
881344a3780SDimitry Andric   for (MachineOperand &MO : MI.debug_operands()) {
882706b4fc4SDimitry Andric     if (MO.isReg() && MO.getReg().isVirtual())
883706b4fc4SDimitry Andric       SeenDbgUsers[MO.getReg()].push_back(SeenDbgUser(&MI, SeenBefore));
884344a3780SDimitry Andric   }
885706b4fc4SDimitry Andric 
886706b4fc4SDimitry Andric   // Record the variable for any DBG_VALUE, to avoid re-ordering any of them.
887706b4fc4SDimitry Andric   SeenDbgVars.insert(Var);
888706b4fc4SDimitry Andric }
889706b4fc4SDimitry Andric 
isWorthBreakingCriticalEdge(MachineInstr & MI,MachineBasicBlock * From,MachineBasicBlock * To,MachineBasicBlock * & DeferredFromBlock)890ac9a064cSDimitry Andric bool MachineSinking::isWorthBreakingCriticalEdge(
891ac9a064cSDimitry Andric     MachineInstr &MI, MachineBasicBlock *From, MachineBasicBlock *To,
892ac9a064cSDimitry Andric     MachineBasicBlock *&DeferredFromBlock) {
893cf099d11SDimitry Andric   // FIXME: Need much better heuristics.
894cf099d11SDimitry Andric 
895cf099d11SDimitry Andric   // If the pass has already considered breaking this edge (during this pass
896cf099d11SDimitry Andric   // through the function), then let's go ahead and break it. This means
897cf099d11SDimitry Andric   // sinking multiple "cheap" instructions into the same block.
89867c32a98SDimitry Andric   if (!CEBCandidates.insert(std::make_pair(From, To)).second)
899cf099d11SDimitry Andric     return true;
900cf099d11SDimitry Andric 
90101095a5dSDimitry Andric   if (!MI.isCopy() && !TII->isAsCheapAsAMove(MI))
902cf099d11SDimitry Andric     return true;
903cf099d11SDimitry Andric 
904ac9a064cSDimitry Andric   // Check and record the register and the destination block we want to sink
905ac9a064cSDimitry Andric   // into. Note that we want to do the following before the next check on branch
906ac9a064cSDimitry Andric   // probability. Because we want to record the initial candidate even if it's
907ac9a064cSDimitry Andric   // on hot edge, so that other candidates that might not on hot edges can be
908ac9a064cSDimitry Andric   // sinked as well.
909ac9a064cSDimitry Andric   for (const auto &MO : MI.all_defs()) {
910ac9a064cSDimitry Andric     Register Reg = MO.getReg();
911ac9a064cSDimitry Andric     if (!Reg)
912ac9a064cSDimitry Andric       continue;
913ac9a064cSDimitry Andric     Register SrcReg = Reg.isVirtual() ? TRI->lookThruCopyLike(Reg, MRI) : Reg;
914ac9a064cSDimitry Andric     auto Key = std::make_pair(SrcReg, To);
915ac9a064cSDimitry Andric     auto Res = CEMergeCandidates.try_emplace(Key, From);
916ac9a064cSDimitry Andric     // We wanted to sink the same register into the same block, consider it to
917ac9a064cSDimitry Andric     // be profitable.
918ac9a064cSDimitry Andric     if (!Res.second) {
919ac9a064cSDimitry Andric       // Return the source block that was previously held off.
920ac9a064cSDimitry Andric       DeferredFromBlock = Res.first->second;
921ac9a064cSDimitry Andric       return true;
922ac9a064cSDimitry Andric     }
923ac9a064cSDimitry Andric   }
924ac9a064cSDimitry Andric 
925b915e9e0SDimitry Andric   if (From->isSuccessor(To) && MBPI->getEdgeProbability(From, To) <=
926b915e9e0SDimitry Andric       BranchProbability(SplitEdgeProbabilityThreshold, 100))
927b915e9e0SDimitry Andric     return true;
928b915e9e0SDimitry Andric 
929cf099d11SDimitry Andric   // MI is cheap, we probably don't want to break the critical edge for it.
930cf099d11SDimitry Andric   // However, if this would allow some definitions of its source operands
931cf099d11SDimitry Andric   // to be sunk then it's probably worth it.
9327fa27ce4SDimitry Andric   for (const MachineOperand &MO : MI.all_uses()) {
9331d5ae102SDimitry Andric     Register Reg = MO.getReg();
934f8af5cf6SDimitry Andric     if (Reg == 0)
935f8af5cf6SDimitry Andric       continue;
936f8af5cf6SDimitry Andric 
937f8af5cf6SDimitry Andric     // We don't move live definitions of physical registers,
938f8af5cf6SDimitry Andric     // so sinking their uses won't enable any opportunities.
939e3b55780SDimitry Andric     if (Reg.isPhysical())
940f8af5cf6SDimitry Andric       continue;
941f8af5cf6SDimitry Andric 
942f8af5cf6SDimitry Andric     // If this instruction is the only user of a virtual register,
943f8af5cf6SDimitry Andric     // check if breaking the edge will enable sinking
944f8af5cf6SDimitry Andric     // both this instruction and the defining instruction.
945f8af5cf6SDimitry Andric     if (MRI->hasOneNonDBGUse(Reg)) {
946f8af5cf6SDimitry Andric       // If the definition resides in same MBB,
947f8af5cf6SDimitry Andric       // claim it's likely we can sink these together.
948f8af5cf6SDimitry Andric       // If definition resides elsewhere, we aren't
949f8af5cf6SDimitry Andric       // blocking it from being sunk so don't break the edge.
950f8af5cf6SDimitry Andric       MachineInstr *DefMI = MRI->getVRegDef(Reg);
95101095a5dSDimitry Andric       if (DefMI->getParent() == MI.getParent())
952cf099d11SDimitry Andric         return true;
953cf099d11SDimitry Andric     }
954f8af5cf6SDimitry Andric   }
955cf099d11SDimitry Andric 
956cf099d11SDimitry Andric   return false;
957cf099d11SDimitry Andric }
958cf099d11SDimitry Andric 
isLegalToBreakCriticalEdge(MachineInstr & MI,MachineBasicBlock * FromBB,MachineBasicBlock * ToBB,bool BreakPHIEdge)959ac9a064cSDimitry Andric bool MachineSinking::isLegalToBreakCriticalEdge(MachineInstr &MI,
960cf099d11SDimitry Andric                                                 MachineBasicBlock *FromBB,
961cf099d11SDimitry Andric                                                 MachineBasicBlock *ToBB,
962cf099d11SDimitry Andric                                                 bool BreakPHIEdge) {
963145449b1SDimitry Andric   // Avoid breaking back edge. From == To means backedge for single BB cycle.
964ac9a064cSDimitry Andric   if (!SplitEdges || FromBB == ToBB || !FromBB->isSuccessor(ToBB))
96567c32a98SDimitry Andric     return false;
966cf099d11SDimitry Andric 
967145449b1SDimitry Andric   MachineCycle *FromCycle = CI->getCycle(FromBB);
968145449b1SDimitry Andric   MachineCycle *ToCycle = CI->getCycle(ToBB);
969145449b1SDimitry Andric 
970145449b1SDimitry Andric   // Check for backedges of more "complex" cycles.
971145449b1SDimitry Andric   if (FromCycle == ToCycle && FromCycle &&
972145449b1SDimitry Andric       (!FromCycle->isReducible() || FromCycle->getHeader() == ToBB))
97367c32a98SDimitry Andric     return false;
974cf099d11SDimitry Andric 
975d39c594dSDimitry Andric   // It's not always legal to break critical edges and sink the computation
976d39c594dSDimitry Andric   // to the edge.
977d39c594dSDimitry Andric   //
978044eb2f6SDimitry Andric   // %bb.1:
979d39c594dSDimitry Andric   // v1024
980044eb2f6SDimitry Andric   // Beq %bb.3
981d39c594dSDimitry Andric   // <fallthrough>
982044eb2f6SDimitry Andric   // %bb.2:
983d39c594dSDimitry Andric   // ... no uses of v1024
984d39c594dSDimitry Andric   // <fallthrough>
985044eb2f6SDimitry Andric   // %bb.3:
986d39c594dSDimitry Andric   // ...
987d39c594dSDimitry Andric   //       = v1024
988d39c594dSDimitry Andric   //
989044eb2f6SDimitry Andric   // If %bb.1 -> %bb.3 edge is broken and computation of v1024 is inserted:
990d39c594dSDimitry Andric   //
991044eb2f6SDimitry Andric   // %bb.1:
992d39c594dSDimitry Andric   // ...
993044eb2f6SDimitry Andric   // Bne %bb.2
994044eb2f6SDimitry Andric   // %bb.4:
995d39c594dSDimitry Andric   // v1024 =
996044eb2f6SDimitry Andric   // B %bb.3
997044eb2f6SDimitry Andric   // %bb.2:
998d39c594dSDimitry Andric   // ... no uses of v1024
999d39c594dSDimitry Andric   // <fallthrough>
1000044eb2f6SDimitry Andric   // %bb.3:
1001d39c594dSDimitry Andric   // ...
1002d39c594dSDimitry Andric   //       = v1024
1003d39c594dSDimitry Andric   //
1004044eb2f6SDimitry Andric   // This is incorrect since v1024 is not computed along the %bb.1->%bb.2->%bb.3
1005d39c594dSDimitry Andric   // flow. We need to ensure the new basic block where the computation is
1006d39c594dSDimitry Andric   // sunk to dominates all the uses.
1007d39c594dSDimitry Andric   // It's only legal to break critical edge and sink the computation to the
1008d39c594dSDimitry Andric   // new block if all the predecessors of "To", except for "From", are
1009d39c594dSDimitry Andric   // not dominated by "From". Given SSA property, this means these
1010d39c594dSDimitry Andric   // predecessors are dominated by "To".
1011cf099d11SDimitry Andric   //
1012cf099d11SDimitry Andric   // There is no need to do this check if all the uses are PHI nodes. PHI
1013cf099d11SDimitry Andric   // sources are only defined on the specific predecessor edges.
1014cf099d11SDimitry Andric   if (!BreakPHIEdge) {
1015c0981da4SDimitry Andric     for (MachineBasicBlock *Pred : ToBB->predecessors())
1016c0981da4SDimitry Andric       if (Pred != FromBB && !DT->dominates(ToBB, Pred))
101767c32a98SDimitry Andric         return false;
1018d39c594dSDimitry Andric   }
1019d39c594dSDimitry Andric 
102067c32a98SDimitry Andric   return true;
1021d39c594dSDimitry Andric }
1022d39c594dSDimitry Andric 
PostponeSplitCriticalEdge(MachineInstr & MI,MachineBasicBlock * FromBB,MachineBasicBlock * ToBB,bool BreakPHIEdge)1023ac9a064cSDimitry Andric bool MachineSinking::PostponeSplitCriticalEdge(MachineInstr &MI,
1024ac9a064cSDimitry Andric                                                MachineBasicBlock *FromBB,
1025ac9a064cSDimitry Andric                                                MachineBasicBlock *ToBB,
1026ac9a064cSDimitry Andric                                                bool BreakPHIEdge) {
1027ac9a064cSDimitry Andric   bool Status = false;
1028ac9a064cSDimitry Andric   MachineBasicBlock *DeferredFromBB = nullptr;
1029ac9a064cSDimitry Andric   if (isWorthBreakingCriticalEdge(MI, FromBB, ToBB, DeferredFromBB)) {
1030ac9a064cSDimitry Andric     // If there is a DeferredFromBB, we consider FromBB only if _both_
1031ac9a064cSDimitry Andric     // of them are legal to split.
1032ac9a064cSDimitry Andric     if ((!DeferredFromBB ||
1033ac9a064cSDimitry Andric          ToSplit.count(std::make_pair(DeferredFromBB, ToBB)) ||
1034ac9a064cSDimitry Andric          isLegalToBreakCriticalEdge(MI, DeferredFromBB, ToBB, BreakPHIEdge)) &&
1035ac9a064cSDimitry Andric         isLegalToBreakCriticalEdge(MI, FromBB, ToBB, BreakPHIEdge)) {
1036ac9a064cSDimitry Andric       ToSplit.insert(std::make_pair(FromBB, ToBB));
1037ac9a064cSDimitry Andric       if (DeferredFromBB)
1038ac9a064cSDimitry Andric         ToSplit.insert(std::make_pair(DeferredFromBB, ToBB));
1039ac9a064cSDimitry Andric       Status = true;
1040ac9a064cSDimitry Andric     }
1041ac9a064cSDimitry Andric   }
1042ac9a064cSDimitry Andric 
1043ac9a064cSDimitry Andric   return Status;
1044ac9a064cSDimitry Andric }
1045ac9a064cSDimitry Andric 
1046b60736ecSDimitry Andric std::vector<unsigned> &
getBBRegisterPressure(const MachineBasicBlock & MBB)1047b1c73532SDimitry Andric MachineSinking::getBBRegisterPressure(const MachineBasicBlock &MBB) {
1048b60736ecSDimitry Andric   // Currently to save compiling time, MBB's register pressure will not change
1049b60736ecSDimitry Andric   // in one ProcessBlock iteration because of CachedRegisterPressure. but MBB's
1050b60736ecSDimitry Andric   // register pressure is changed after sinking any instructions into it.
1051b60736ecSDimitry Andric   // FIXME: need a accurate and cheap register pressure estiminate model here.
1052b60736ecSDimitry Andric   auto RP = CachedRegisterPressure.find(&MBB);
1053b60736ecSDimitry Andric   if (RP != CachedRegisterPressure.end())
1054b60736ecSDimitry Andric     return RP->second;
1055b60736ecSDimitry Andric 
1056b60736ecSDimitry Andric   RegionPressure Pressure;
1057b60736ecSDimitry Andric   RegPressureTracker RPTracker(Pressure);
1058b60736ecSDimitry Andric 
1059b60736ecSDimitry Andric   // Initialize the register pressure tracker.
1060b60736ecSDimitry Andric   RPTracker.init(MBB.getParent(), &RegClassInfo, nullptr, &MBB, MBB.end(),
1061b60736ecSDimitry Andric                  /*TrackLaneMasks*/ false, /*TrackUntiedDefs=*/true);
1062b60736ecSDimitry Andric 
1063b1c73532SDimitry Andric   for (MachineBasicBlock::const_iterator MII = MBB.instr_end(),
1064b60736ecSDimitry Andric                                          MIE = MBB.instr_begin();
1065b60736ecSDimitry Andric        MII != MIE; --MII) {
1066b1c73532SDimitry Andric     const MachineInstr &MI = *std::prev(MII);
1067344a3780SDimitry Andric     if (MI.isDebugInstr() || MI.isPseudoProbe())
1068b60736ecSDimitry Andric       continue;
1069b60736ecSDimitry Andric     RegisterOperands RegOpers;
1070b60736ecSDimitry Andric     RegOpers.collect(MI, *TRI, *MRI, false, false);
1071b60736ecSDimitry Andric     RPTracker.recedeSkipDebugValues();
1072b60736ecSDimitry Andric     assert(&*RPTracker.getPos() == &MI && "RPTracker sync error!");
1073b60736ecSDimitry Andric     RPTracker.recede(RegOpers);
1074b60736ecSDimitry Andric   }
1075b60736ecSDimitry Andric 
1076b60736ecSDimitry Andric   RPTracker.closeRegion();
1077b60736ecSDimitry Andric   auto It = CachedRegisterPressure.insert(
1078b60736ecSDimitry Andric       std::make_pair(&MBB, RPTracker.getPressure().MaxSetPressure));
1079b60736ecSDimitry Andric   return It.first->second;
1080b60736ecSDimitry Andric }
1081b60736ecSDimitry Andric 
registerPressureSetExceedsLimit(unsigned NRegs,const TargetRegisterClass * RC,const MachineBasicBlock & MBB)1082b1c73532SDimitry Andric bool MachineSinking::registerPressureSetExceedsLimit(
1083b1c73532SDimitry Andric     unsigned NRegs, const TargetRegisterClass *RC,
1084b1c73532SDimitry Andric     const MachineBasicBlock &MBB) {
1085b1c73532SDimitry Andric   unsigned Weight = NRegs * TRI->getRegClassWeight(RC).RegWeight;
1086b1c73532SDimitry Andric   const int *PS = TRI->getRegClassPressureSets(RC);
1087b1c73532SDimitry Andric   std::vector<unsigned> BBRegisterPressure = getBBRegisterPressure(MBB);
1088b1c73532SDimitry Andric   for (; *PS != -1; PS++)
1089b1c73532SDimitry Andric     if (Weight + BBRegisterPressure[*PS] >=
1090b1c73532SDimitry Andric         TRI->getRegPressureSetLimit(*MBB.getParent(), *PS))
1091b1c73532SDimitry Andric       return true;
1092b1c73532SDimitry Andric   return false;
1093b1c73532SDimitry Andric }
1094b1c73532SDimitry Andric 
109563faed5bSDimitry Andric /// isProfitableToSinkTo - Return true if it is profitable to sink MI.
isProfitableToSinkTo(Register Reg,MachineInstr & MI,MachineBasicBlock * MBB,MachineBasicBlock * SuccToSinkTo,AllSuccsCache & AllSuccessors)1096b60736ecSDimitry Andric bool MachineSinking::isProfitableToSinkTo(Register Reg, MachineInstr &MI,
109763faed5bSDimitry Andric                                           MachineBasicBlock *MBB,
10983a0822f0SDimitry Andric                                           MachineBasicBlock *SuccToSinkTo,
10993a0822f0SDimitry Andric                                           AllSuccsCache &AllSuccessors) {
110063faed5bSDimitry Andric   assert (SuccToSinkTo && "Invalid SinkTo Candidate BB");
110163faed5bSDimitry Andric 
110263faed5bSDimitry Andric   if (MBB == SuccToSinkTo)
1103009b1c42SEd Schouten     return false;
1104009b1c42SEd Schouten 
110563faed5bSDimitry Andric   // It is profitable if SuccToSinkTo does not post dominate current block.
110667c32a98SDimitry Andric   if (!PDT->dominates(SuccToSinkTo, MBB))
110767c32a98SDimitry Andric     return true;
110867c32a98SDimitry Andric 
1109145449b1SDimitry Andric   // It is profitable to sink an instruction from a deeper cycle to a shallower
1110145449b1SDimitry Andric   // cycle, even if the latter post-dominates the former (PR21115).
1111145449b1SDimitry Andric   if (CI->getCycleDepth(MBB) > CI->getCycleDepth(SuccToSinkTo))
111263faed5bSDimitry Andric     return true;
111363faed5bSDimitry Andric 
111463faed5bSDimitry Andric   // Check if only use in post dominated block is PHI instruction.
111563faed5bSDimitry Andric   bool NonPHIUse = false;
11165ca98fd9SDimitry Andric   for (MachineInstr &UseInst : MRI->use_nodbg_instructions(Reg)) {
11175ca98fd9SDimitry Andric     MachineBasicBlock *UseBlock = UseInst.getParent();
11185ca98fd9SDimitry Andric     if (UseBlock == SuccToSinkTo && !UseInst.isPHI())
111963faed5bSDimitry Andric       NonPHIUse = true;
112063faed5bSDimitry Andric   }
112163faed5bSDimitry Andric   if (!NonPHIUse)
112263faed5bSDimitry Andric     return true;
112363faed5bSDimitry Andric 
112463faed5bSDimitry Andric   // If SuccToSinkTo post dominates then also it may be profitable if MI
112563faed5bSDimitry Andric   // can further profitably sinked into another block in next round.
112663faed5bSDimitry Andric   bool BreakPHIEdge = false;
112767c32a98SDimitry Andric   // FIXME - If finding successor is compile time expensive then cache results.
11283a0822f0SDimitry Andric   if (MachineBasicBlock *MBB2 =
11293a0822f0SDimitry Andric           FindSuccToSinkTo(MI, SuccToSinkTo, BreakPHIEdge, AllSuccessors))
11303a0822f0SDimitry Andric     return isProfitableToSinkTo(Reg, MI, SuccToSinkTo, MBB2, AllSuccessors);
113163faed5bSDimitry Andric 
1132145449b1SDimitry Andric   MachineCycle *MCycle = CI->getCycle(MBB);
1133b60736ecSDimitry Andric 
1134145449b1SDimitry Andric   // If the instruction is not inside a cycle, it is not profitable to sink MI to
1135b60736ecSDimitry Andric   // a post dominate block SuccToSinkTo.
1136145449b1SDimitry Andric   if (!MCycle)
113763faed5bSDimitry Andric     return false;
1138b60736ecSDimitry Andric 
1139145449b1SDimitry Andric   // If this instruction is inside a Cycle and sinking this instruction can make
1140b60736ecSDimitry Andric   // more registers live range shorten, it is still prifitable.
1141f65dcba8SDimitry Andric   for (const MachineOperand &MO : MI.operands()) {
1142b60736ecSDimitry Andric     // Ignore non-register operands.
1143b60736ecSDimitry Andric     if (!MO.isReg())
1144b60736ecSDimitry Andric       continue;
1145b60736ecSDimitry Andric     Register Reg = MO.getReg();
1146b60736ecSDimitry Andric     if (Reg == 0)
1147b60736ecSDimitry Andric       continue;
1148b60736ecSDimitry Andric 
1149e3b55780SDimitry Andric     if (Reg.isPhysical()) {
11507fa27ce4SDimitry Andric       // Don't handle non-constant and non-ignorable physical register uses.
11517fa27ce4SDimitry Andric       if (MO.isUse() && !MRI->isConstantPhysReg(Reg) && !TII->isIgnorableUse(MO))
1152b60736ecSDimitry Andric         return false;
11537fa27ce4SDimitry Andric       continue;
11546f8fc217SDimitry Andric     }
1155b60736ecSDimitry Andric 
1156b60736ecSDimitry Andric     // Users for the defs are all dominated by SuccToSinkTo.
1157b60736ecSDimitry Andric     if (MO.isDef()) {
1158b60736ecSDimitry Andric       // This def register's live range is shortened after sinking.
1159b60736ecSDimitry Andric       bool LocalUse = false;
1160b60736ecSDimitry Andric       if (!AllUsesDominatedByBlock(Reg, SuccToSinkTo, MBB, BreakPHIEdge,
1161b60736ecSDimitry Andric                                    LocalUse))
1162b60736ecSDimitry Andric         return false;
1163b60736ecSDimitry Andric     } else {
1164b60736ecSDimitry Andric       MachineInstr *DefMI = MRI->getVRegDef(Reg);
1165145449b1SDimitry Andric       if (!DefMI)
1166b60736ecSDimitry Andric         continue;
1167145449b1SDimitry Andric       MachineCycle *Cycle = CI->getCycle(DefMI->getParent());
1168145449b1SDimitry Andric       // DefMI is defined outside of cycle. There should be no live range
1169145449b1SDimitry Andric       // impact for this operand. Defination outside of cycle means:
1170145449b1SDimitry Andric       // 1: defination is outside of cycle.
1171145449b1SDimitry Andric       // 2: defination is in this cycle, but it is a PHI in the cycle header.
1172145449b1SDimitry Andric       if (Cycle != MCycle || (DefMI->isPHI() && Cycle && Cycle->isReducible() &&
1173145449b1SDimitry Andric                               Cycle->getHeader() == DefMI->getParent()))
1174145449b1SDimitry Andric         continue;
1175145449b1SDimitry Andric       // The DefMI is defined inside the cycle.
1176b60736ecSDimitry Andric       // If sinking this operand makes some register pressure set exceed limit,
1177b60736ecSDimitry Andric       // it is not profitable.
1178b1c73532SDimitry Andric       if (registerPressureSetExceedsLimit(1, MRI->getRegClass(Reg),
1179b1c73532SDimitry Andric                                           *SuccToSinkTo)) {
1180b60736ecSDimitry Andric         LLVM_DEBUG(dbgs() << "register pressure exceed limit, not profitable.");
1181b60736ecSDimitry Andric         return false;
1182b60736ecSDimitry Andric       }
1183b60736ecSDimitry Andric     }
1184b60736ecSDimitry Andric   }
1185b60736ecSDimitry Andric 
1186145449b1SDimitry Andric   // If MI is in cycle and all its operands are alive across the whole cycle or
1187145449b1SDimitry Andric   // if no operand sinking make register pressure set exceed limit, it is
1188b60736ecSDimitry Andric   // profitable to sink MI.
1189b60736ecSDimitry Andric   return true;
119063faed5bSDimitry Andric }
119163faed5bSDimitry Andric 
11923a0822f0SDimitry Andric /// Get the sorted sequence of successors for this MachineBasicBlock, possibly
11933a0822f0SDimitry Andric /// computing it if it was not already cached.
11943a0822f0SDimitry Andric SmallVector<MachineBasicBlock *, 4> &
GetAllSortedSuccessors(MachineInstr & MI,MachineBasicBlock * MBB,AllSuccsCache & AllSuccessors) const119501095a5dSDimitry Andric MachineSinking::GetAllSortedSuccessors(MachineInstr &MI, MachineBasicBlock *MBB,
11963a0822f0SDimitry Andric                                        AllSuccsCache &AllSuccessors) const {
11973a0822f0SDimitry Andric   // Do we have the sorted successors in cache ?
11983a0822f0SDimitry Andric   auto Succs = AllSuccessors.find(MBB);
11993a0822f0SDimitry Andric   if (Succs != AllSuccessors.end())
12003a0822f0SDimitry Andric     return Succs->second;
12013a0822f0SDimitry Andric 
1202b60736ecSDimitry Andric   SmallVector<MachineBasicBlock *, 4> AllSuccs(MBB->successors());
12033a0822f0SDimitry Andric 
12043a0822f0SDimitry Andric   // Handle cases where sinking can happen but where the sink point isn't a
12053a0822f0SDimitry Andric   // successor. For example:
12063a0822f0SDimitry Andric   //
12073a0822f0SDimitry Andric   //   x = computation
12083a0822f0SDimitry Andric   //   if () {} else {}
12093a0822f0SDimitry Andric   //   use x
12103a0822f0SDimitry Andric   //
1211cfca06d7SDimitry Andric   for (MachineDomTreeNode *DTChild : DT->getNode(MBB)->children()) {
12123a0822f0SDimitry Andric     // DomTree children of MBB that have MBB as immediate dominator are added.
121301095a5dSDimitry Andric     if (DTChild->getIDom()->getBlock() == MI.getParent() &&
12143a0822f0SDimitry Andric         // Skip MBBs already added to the AllSuccs vector above.
12153a0822f0SDimitry Andric         !MBB->isSuccessor(DTChild->getBlock()))
12163a0822f0SDimitry Andric       AllSuccs.push_back(DTChild->getBlock());
1217cfca06d7SDimitry Andric   }
12183a0822f0SDimitry Andric 
1219145449b1SDimitry Andric   // Sort Successors according to their cycle depth or block frequency info.
1220e6d15924SDimitry Andric   llvm::stable_sort(
1221e6d15924SDimitry Andric       AllSuccs, [this](const MachineBasicBlock *L, const MachineBasicBlock *R) {
12223a0822f0SDimitry Andric         uint64_t LHSFreq = MBFI ? MBFI->getBlockFreq(L).getFrequency() : 0;
12233a0822f0SDimitry Andric         uint64_t RHSFreq = MBFI ? MBFI->getBlockFreq(R).getFrequency() : 0;
1224b1c73532SDimitry Andric         bool HasBlockFreq = LHSFreq != 0 || RHSFreq != 0;
12253a0822f0SDimitry Andric         return HasBlockFreq ? LHSFreq < RHSFreq
1226145449b1SDimitry Andric                             : CI->getCycleDepth(L) < CI->getCycleDepth(R);
12273a0822f0SDimitry Andric       });
12283a0822f0SDimitry Andric 
12293a0822f0SDimitry Andric   auto it = AllSuccessors.insert(std::make_pair(MBB, AllSuccs));
12303a0822f0SDimitry Andric 
12313a0822f0SDimitry Andric   return it.first->second;
12323a0822f0SDimitry Andric }
12333a0822f0SDimitry Andric 
123463faed5bSDimitry Andric /// FindSuccToSinkTo - Find a successor to sink this instruction to.
123501095a5dSDimitry Andric MachineBasicBlock *
FindSuccToSinkTo(MachineInstr & MI,MachineBasicBlock * MBB,bool & BreakPHIEdge,AllSuccsCache & AllSuccessors)123601095a5dSDimitry Andric MachineSinking::FindSuccToSinkTo(MachineInstr &MI, MachineBasicBlock *MBB,
12373a0822f0SDimitry Andric                                  bool &BreakPHIEdge,
12383a0822f0SDimitry Andric                                  AllSuccsCache &AllSuccessors) {
123963faed5bSDimitry Andric   assert (MBB && "Invalid MachineBasicBlock!");
1240009b1c42SEd Schouten 
1241145449b1SDimitry Andric   // loop over all the operands of the specified instruction.  If there is
1242009b1c42SEd Schouten   // anything we can't handle, bail out.
1243009b1c42SEd Schouten 
1244009b1c42SEd Schouten   // SuccToSinkTo - This is the successor to sink this instruction to, once we
1245009b1c42SEd Schouten   // decide.
12465ca98fd9SDimitry Andric   MachineBasicBlock *SuccToSinkTo = nullptr;
1247f65dcba8SDimitry Andric   for (const MachineOperand &MO : MI.operands()) {
1248009b1c42SEd Schouten     if (!MO.isReg()) continue;  // Ignore non-register operands.
1249009b1c42SEd Schouten 
12501d5ae102SDimitry Andric     Register Reg = MO.getReg();
1251009b1c42SEd Schouten     if (Reg == 0) continue;
1252009b1c42SEd Schouten 
1253e3b55780SDimitry Andric     if (Reg.isPhysical()) {
125459850d08SRoman Divacky       if (MO.isUse()) {
125559850d08SRoman Divacky         // If the physreg has no defs anywhere, it's just an ambient register
125659850d08SRoman Divacky         // and we can freely move its uses. Alternatively, if it's allocatable,
125759850d08SRoman Divacky         // it could get allocated to something with a def during allocation.
12586f8fc217SDimitry Andric         if (!MRI->isConstantPhysReg(Reg) && !TII->isIgnorableUse(MO))
12595ca98fd9SDimitry Andric           return nullptr;
126059850d08SRoman Divacky       } else if (!MO.isDead()) {
126159850d08SRoman Divacky         // A def that isn't dead. We can't move it.
12625ca98fd9SDimitry Andric         return nullptr;
126359850d08SRoman Divacky       }
1264009b1c42SEd Schouten     } else {
1265009b1c42SEd Schouten       // Virtual register uses are always safe to sink.
1266009b1c42SEd Schouten       if (MO.isUse()) continue;
1267009b1c42SEd Schouten 
1268009b1c42SEd Schouten       // If it's not safe to move defs of the register class, then abort.
1269cf099d11SDimitry Andric       if (!TII->isSafeToMoveRegClassDefs(MRI->getRegClass(Reg)))
12705ca98fd9SDimitry Andric         return nullptr;
1271009b1c42SEd Schouten 
1272009b1c42SEd Schouten       // Virtual register defs can only be sunk if all their uses are in blocks
1273009b1c42SEd Schouten       // dominated by one of the successors.
1274009b1c42SEd Schouten       if (SuccToSinkTo) {
1275009b1c42SEd Schouten         // If a previous operand picked a block to sink to, then this operand
1276009b1c42SEd Schouten         // must be sinkable to the same block.
1277d39c594dSDimitry Andric         bool LocalUse = false;
127863faed5bSDimitry Andric         if (!AllUsesDominatedByBlock(Reg, SuccToSinkTo, MBB,
1279cf099d11SDimitry Andric                                      BreakPHIEdge, LocalUse))
12805ca98fd9SDimitry Andric           return nullptr;
128166e41e3cSRoman Divacky 
1282009b1c42SEd Schouten         continue;
1283009b1c42SEd Schouten       }
1284009b1c42SEd Schouten 
1285009b1c42SEd Schouten       // Otherwise, we should look at all the successors and decide which one
128667c32a98SDimitry Andric       // we should sink to. If we have reliable block frequency information
128767c32a98SDimitry Andric       // (frequency != 0) available, give successors with smaller frequencies
1288145449b1SDimitry Andric       // higher priority, otherwise prioritize smaller cycle depths.
12893a0822f0SDimitry Andric       for (MachineBasicBlock *SuccBlock :
12903a0822f0SDimitry Andric            GetAllSortedSuccessors(MI, MBB, AllSuccessors)) {
1291d39c594dSDimitry Andric         bool LocalUse = false;
129263faed5bSDimitry Andric         if (AllUsesDominatedByBlock(Reg, SuccBlock, MBB,
1293cf099d11SDimitry Andric                                     BreakPHIEdge, LocalUse)) {
129463faed5bSDimitry Andric           SuccToSinkTo = SuccBlock;
1295009b1c42SEd Schouten           break;
1296009b1c42SEd Schouten         }
1297d39c594dSDimitry Andric         if (LocalUse)
1298d39c594dSDimitry Andric           // Def is used locally, it's never safe to move this def.
12995ca98fd9SDimitry Andric           return nullptr;
1300009b1c42SEd Schouten       }
1301009b1c42SEd Schouten 
1302009b1c42SEd Schouten       // If we couldn't find a block to sink to, ignore this instruction.
13035ca98fd9SDimitry Andric       if (!SuccToSinkTo)
13045ca98fd9SDimitry Andric         return nullptr;
13053a0822f0SDimitry Andric       if (!isProfitableToSinkTo(Reg, MI, MBB, SuccToSinkTo, AllSuccessors))
13065ca98fd9SDimitry Andric         return nullptr;
130763faed5bSDimitry Andric     }
130863faed5bSDimitry Andric   }
130963faed5bSDimitry Andric 
131063faed5bSDimitry Andric   // It is not possible to sink an instruction into its own block.  This can
1311145449b1SDimitry Andric   // happen with cycles.
131263faed5bSDimitry Andric   if (MBB == SuccToSinkTo)
13135ca98fd9SDimitry Andric     return nullptr;
131463faed5bSDimitry Andric 
131563faed5bSDimitry Andric   // It's not safe to sink instructions to EH landing pad. Control flow into
131663faed5bSDimitry Andric   // landing pad is implicitly defined.
1317b1c73532SDimitry Andric   if (SuccToSinkTo && SuccToSinkTo->isEHPad())
13185ca98fd9SDimitry Andric     return nullptr;
131963faed5bSDimitry Andric 
1320cfca06d7SDimitry Andric   // It ought to be okay to sink instructions into an INLINEASM_BR target, but
1321cfca06d7SDimitry Andric   // only if we make sure that MI occurs _before_ an INLINEASM_BR instruction in
1322cfca06d7SDimitry Andric   // the source block (which this code does not yet do). So for now, forbid
1323cfca06d7SDimitry Andric   // doing so.
1324b1c73532SDimitry Andric   if (SuccToSinkTo && SuccToSinkTo->isInlineAsmBrIndirectTarget())
13257fa27ce4SDimitry Andric     return nullptr;
13267fa27ce4SDimitry Andric 
1327b1c73532SDimitry Andric   if (SuccToSinkTo && !TII->isSafeToSink(MI, SuccToSinkTo, CI))
1328cfca06d7SDimitry Andric     return nullptr;
1329cfca06d7SDimitry Andric 
133063faed5bSDimitry Andric   return SuccToSinkTo;
133163faed5bSDimitry Andric }
133263faed5bSDimitry Andric 
1333eb11fae6SDimitry Andric /// Return true if MI is likely to be usable as a memory operation by the
133401095a5dSDimitry Andric /// implicit null check optimization.
133501095a5dSDimitry Andric ///
133601095a5dSDimitry Andric /// This is a "best effort" heuristic, and should not be relied upon for
133701095a5dSDimitry Andric /// correctness.  This returning true does not guarantee that the implicit null
133801095a5dSDimitry Andric /// check optimization is legal over MI, and this returning false does not
133901095a5dSDimitry Andric /// guarantee MI cannot possibly be used to do a null check.
SinkingPreventsImplicitNullCheck(MachineInstr & MI,const TargetInstrInfo * TII,const TargetRegisterInfo * TRI)134001095a5dSDimitry Andric static bool SinkingPreventsImplicitNullCheck(MachineInstr &MI,
134101095a5dSDimitry Andric                                              const TargetInstrInfo *TII,
134201095a5dSDimitry Andric                                              const TargetRegisterInfo *TRI) {
1343044eb2f6SDimitry Andric   using MachineBranchPredicate = TargetInstrInfo::MachineBranchPredicate;
134401095a5dSDimitry Andric 
134501095a5dSDimitry Andric   auto *MBB = MI.getParent();
134601095a5dSDimitry Andric   if (MBB->pred_size() != 1)
134701095a5dSDimitry Andric     return false;
134801095a5dSDimitry Andric 
134901095a5dSDimitry Andric   auto *PredMBB = *MBB->pred_begin();
135001095a5dSDimitry Andric   auto *PredBB = PredMBB->getBasicBlock();
135101095a5dSDimitry Andric 
135201095a5dSDimitry Andric   // Frontends that don't use implicit null checks have no reason to emit
135301095a5dSDimitry Andric   // branches with make.implicit metadata, and this function should always
135401095a5dSDimitry Andric   // return false for them.
135501095a5dSDimitry Andric   if (!PredBB ||
135601095a5dSDimitry Andric       !PredBB->getTerminator()->getMetadata(LLVMContext::MD_make_implicit))
135701095a5dSDimitry Andric     return false;
135801095a5dSDimitry Andric 
1359e6d15924SDimitry Andric   const MachineOperand *BaseOp;
136001095a5dSDimitry Andric   int64_t Offset;
1361cfca06d7SDimitry Andric   bool OffsetIsScalable;
1362cfca06d7SDimitry Andric   if (!TII->getMemOperandWithOffset(MI, BaseOp, Offset, OffsetIsScalable, TRI))
1363d8e91e46SDimitry Andric     return false;
1364d8e91e46SDimitry Andric 
1365d8e91e46SDimitry Andric   if (!BaseOp->isReg())
136601095a5dSDimitry Andric     return false;
136701095a5dSDimitry Andric 
136801095a5dSDimitry Andric   if (!(MI.mayLoad() && !MI.isPredicable()))
136901095a5dSDimitry Andric     return false;
137001095a5dSDimitry Andric 
137101095a5dSDimitry Andric   MachineBranchPredicate MBP;
137201095a5dSDimitry Andric   if (TII->analyzeBranchPredicate(*PredMBB, MBP, false))
137301095a5dSDimitry Andric     return false;
137401095a5dSDimitry Andric 
137501095a5dSDimitry Andric   return MBP.LHS.isReg() && MBP.RHS.isImm() && MBP.RHS.getImm() == 0 &&
137601095a5dSDimitry Andric          (MBP.Predicate == MachineBranchPredicate::PRED_NE ||
137701095a5dSDimitry Andric           MBP.Predicate == MachineBranchPredicate::PRED_EQ) &&
1378d8e91e46SDimitry Andric          MBP.LHS.getReg() == BaseOp->getReg();
137901095a5dSDimitry Andric }
138001095a5dSDimitry Andric 
1381706b4fc4SDimitry Andric /// If the sunk instruction is a copy, try to forward the copy instead of
1382706b4fc4SDimitry Andric /// leaving an 'undef' DBG_VALUE in the original location. Don't do this if
1383706b4fc4SDimitry Andric /// there's any subregister weirdness involved. Returns true if copy
1384706b4fc4SDimitry Andric /// propagation occurred.
attemptDebugCopyProp(MachineInstr & SinkInst,MachineInstr & DbgMI,Register Reg)1385344a3780SDimitry Andric static bool attemptDebugCopyProp(MachineInstr &SinkInst, MachineInstr &DbgMI,
1386344a3780SDimitry Andric                                  Register Reg) {
1387706b4fc4SDimitry Andric   const MachineRegisterInfo &MRI = SinkInst.getMF()->getRegInfo();
1388706b4fc4SDimitry Andric   const TargetInstrInfo &TII = *SinkInst.getMF()->getSubtarget().getInstrInfo();
1389706b4fc4SDimitry Andric 
1390706b4fc4SDimitry Andric   // Copy DBG_VALUE operand and set the original to undef. We then check to
1391706b4fc4SDimitry Andric   // see whether this is something that can be copy-forwarded. If it isn't,
1392706b4fc4SDimitry Andric   // continue around the loop.
1393706b4fc4SDimitry Andric 
1394706b4fc4SDimitry Andric   const MachineOperand *SrcMO = nullptr, *DstMO = nullptr;
1395706b4fc4SDimitry Andric   auto CopyOperands = TII.isCopyInstr(SinkInst);
1396706b4fc4SDimitry Andric   if (!CopyOperands)
1397706b4fc4SDimitry Andric     return false;
1398706b4fc4SDimitry Andric   SrcMO = CopyOperands->Source;
1399706b4fc4SDimitry Andric   DstMO = CopyOperands->Destination;
1400706b4fc4SDimitry Andric 
1401706b4fc4SDimitry Andric   // Check validity of forwarding this copy.
1402706b4fc4SDimitry Andric   bool PostRA = MRI.getNumVirtRegs() == 0;
1403706b4fc4SDimitry Andric 
1404706b4fc4SDimitry Andric   // Trying to forward between physical and virtual registers is too hard.
1405344a3780SDimitry Andric   if (Reg.isVirtual() != SrcMO->getReg().isVirtual())
1406706b4fc4SDimitry Andric     return false;
1407706b4fc4SDimitry Andric 
1408706b4fc4SDimitry Andric   // Only try virtual register copy-forwarding before regalloc, and physical
1409706b4fc4SDimitry Andric   // register copy-forwarding after regalloc.
1410344a3780SDimitry Andric   bool arePhysRegs = !Reg.isVirtual();
1411706b4fc4SDimitry Andric   if (arePhysRegs != PostRA)
1412706b4fc4SDimitry Andric     return false;
1413706b4fc4SDimitry Andric 
1414706b4fc4SDimitry Andric   // Pre-regalloc, only forward if all subregisters agree (or there are no
1415706b4fc4SDimitry Andric   // subregs at all). More analysis might recover some forwardable copies.
1416344a3780SDimitry Andric   if (!PostRA)
1417344a3780SDimitry Andric     for (auto &DbgMO : DbgMI.getDebugOperandsForReg(Reg))
1418344a3780SDimitry Andric       if (DbgMO.getSubReg() != SrcMO->getSubReg() ||
1419344a3780SDimitry Andric           DbgMO.getSubReg() != DstMO->getSubReg())
1420706b4fc4SDimitry Andric         return false;
1421706b4fc4SDimitry Andric 
1422706b4fc4SDimitry Andric   // Post-regalloc, we may be sinking a DBG_VALUE of a sub or super-register
1423706b4fc4SDimitry Andric   // of this copy. Only forward the copy if the DBG_VALUE operand exactly
1424706b4fc4SDimitry Andric   // matches the copy destination.
1425344a3780SDimitry Andric   if (PostRA && Reg != DstMO->getReg())
1426706b4fc4SDimitry Andric     return false;
1427706b4fc4SDimitry Andric 
1428344a3780SDimitry Andric   for (auto &DbgMO : DbgMI.getDebugOperandsForReg(Reg)) {
1429cfca06d7SDimitry Andric     DbgMO.setReg(SrcMO->getReg());
1430cfca06d7SDimitry Andric     DbgMO.setSubReg(SrcMO->getSubReg());
1431344a3780SDimitry Andric   }
1432706b4fc4SDimitry Andric   return true;
1433706b4fc4SDimitry Andric }
1434706b4fc4SDimitry Andric 
1435344a3780SDimitry Andric using MIRegs = std::pair<MachineInstr *, SmallVector<unsigned, 2>>;
1436706b4fc4SDimitry Andric /// Sink an instruction and its associated debug instructions.
performSink(MachineInstr & MI,MachineBasicBlock & SuccToSinkTo,MachineBasicBlock::iterator InsertPos,ArrayRef<MIRegs> DbgValuesToSink)1437eb11fae6SDimitry Andric static void performSink(MachineInstr &MI, MachineBasicBlock &SuccToSinkTo,
1438d8e91e46SDimitry Andric                         MachineBasicBlock::iterator InsertPos,
1439145449b1SDimitry Andric                         ArrayRef<MIRegs> DbgValuesToSink) {
1440eb11fae6SDimitry Andric   // If we cannot find a location to use (merge with), then we erase the debug
1441eb11fae6SDimitry Andric   // location to prevent debug-info driven tools from potentially reporting
1442eb11fae6SDimitry Andric   // wrong location information.
1443eb11fae6SDimitry Andric   if (!SuccToSinkTo.empty() && InsertPos != SuccToSinkTo.end())
1444eb11fae6SDimitry Andric     MI.setDebugLoc(DILocation::getMergedLocation(MI.getDebugLoc(),
1445eb11fae6SDimitry Andric                                                  InsertPos->getDebugLoc()));
1446eb11fae6SDimitry Andric   else
1447eb11fae6SDimitry Andric     MI.setDebugLoc(DebugLoc());
1448eb11fae6SDimitry Andric 
1449eb11fae6SDimitry Andric   // Move the instruction.
1450eb11fae6SDimitry Andric   MachineBasicBlock *ParentBlock = MI.getParent();
1451eb11fae6SDimitry Andric   SuccToSinkTo.splice(InsertPos, ParentBlock, MI,
1452eb11fae6SDimitry Andric                       ++MachineBasicBlock::iterator(MI));
1453eb11fae6SDimitry Andric 
1454706b4fc4SDimitry Andric   // Sink a copy of debug users to the insert position. Mark the original
1455706b4fc4SDimitry Andric   // DBG_VALUE location as 'undef', indicating that any earlier variable
1456706b4fc4SDimitry Andric   // location should be terminated as we've optimised away the value at this
1457706b4fc4SDimitry Andric   // point.
1458145449b1SDimitry Andric   for (const auto &DbgValueToSink : DbgValuesToSink) {
1459344a3780SDimitry Andric     MachineInstr *DbgMI = DbgValueToSink.first;
1460344a3780SDimitry Andric     MachineInstr *NewDbgMI = DbgMI->getMF()->CloneMachineInstr(DbgMI);
1461706b4fc4SDimitry Andric     SuccToSinkTo.insert(InsertPos, NewDbgMI);
1462706b4fc4SDimitry Andric 
1463344a3780SDimitry Andric     bool PropagatedAllSunkOps = true;
1464344a3780SDimitry Andric     for (unsigned Reg : DbgValueToSink.second) {
1465344a3780SDimitry Andric       if (DbgMI->hasDebugOperandForReg(Reg)) {
1466344a3780SDimitry Andric         if (!attemptDebugCopyProp(MI, *DbgMI, Reg)) {
1467344a3780SDimitry Andric           PropagatedAllSunkOps = false;
1468344a3780SDimitry Andric           break;
1469344a3780SDimitry Andric         }
1470344a3780SDimitry Andric       }
1471344a3780SDimitry Andric     }
1472344a3780SDimitry Andric     if (!PropagatedAllSunkOps)
1473cfca06d7SDimitry Andric       DbgMI->setDebugValueUndef();
1474eb11fae6SDimitry Andric   }
1475eb11fae6SDimitry Andric }
1476eb11fae6SDimitry Andric 
1477b60736ecSDimitry Andric /// hasStoreBetween - check if there is store betweeen straight line blocks From
1478b60736ecSDimitry Andric /// and To.
hasStoreBetween(MachineBasicBlock * From,MachineBasicBlock * To,MachineInstr & MI)1479b60736ecSDimitry Andric bool MachineSinking::hasStoreBetween(MachineBasicBlock *From,
1480b60736ecSDimitry Andric                                      MachineBasicBlock *To, MachineInstr &MI) {
1481b60736ecSDimitry Andric   // Make sure From and To are in straight line which means From dominates To
1482b60736ecSDimitry Andric   // and To post dominates From.
1483b60736ecSDimitry Andric   if (!DT->dominates(From, To) || !PDT->dominates(To, From))
1484b60736ecSDimitry Andric     return true;
1485b60736ecSDimitry Andric 
1486b60736ecSDimitry Andric   auto BlockPair = std::make_pair(From, To);
1487b60736ecSDimitry Andric 
1488b60736ecSDimitry Andric   // Does these two blocks pair be queried before and have a definite cached
1489b60736ecSDimitry Andric   // result?
1490b1c73532SDimitry Andric   if (auto It = HasStoreCache.find(BlockPair); It != HasStoreCache.end())
1491b1c73532SDimitry Andric     return It->second;
1492b60736ecSDimitry Andric 
1493b1c73532SDimitry Andric   if (auto It = StoreInstrCache.find(BlockPair); It != StoreInstrCache.end())
1494b1c73532SDimitry Andric     return llvm::any_of(It->second, [&](MachineInstr *I) {
1495b60736ecSDimitry Andric       return I->mayAlias(AA, MI, false);
1496b60736ecSDimitry Andric     });
1497b60736ecSDimitry Andric 
1498b60736ecSDimitry Andric   bool SawStore = false;
1499b60736ecSDimitry Andric   bool HasAliasedStore = false;
1500b60736ecSDimitry Andric   DenseSet<MachineBasicBlock *> HandledBlocks;
1501b60736ecSDimitry Andric   DenseSet<MachineBasicBlock *> HandledDomBlocks;
1502b60736ecSDimitry Andric   // Go through all reachable blocks from From.
1503b60736ecSDimitry Andric   for (MachineBasicBlock *BB : depth_first(From)) {
1504b60736ecSDimitry Andric     // We insert the instruction at the start of block To, so no need to worry
1505b60736ecSDimitry Andric     // about stores inside To.
1506b60736ecSDimitry Andric     // Store in block From should be already considered when just enter function
1507b60736ecSDimitry Andric     // SinkInstruction.
1508b60736ecSDimitry Andric     if (BB == To || BB == From)
1509b60736ecSDimitry Andric       continue;
1510b60736ecSDimitry Andric 
1511b60736ecSDimitry Andric     // We already handle this BB in previous iteration.
1512b60736ecSDimitry Andric     if (HandledBlocks.count(BB))
1513b60736ecSDimitry Andric       continue;
1514b60736ecSDimitry Andric 
1515b60736ecSDimitry Andric     HandledBlocks.insert(BB);
1516b60736ecSDimitry Andric     // To post dominates BB, it must be a path from block From.
1517b60736ecSDimitry Andric     if (PDT->dominates(To, BB)) {
1518b60736ecSDimitry Andric       if (!HandledDomBlocks.count(BB))
1519b60736ecSDimitry Andric         HandledDomBlocks.insert(BB);
1520b60736ecSDimitry Andric 
1521b60736ecSDimitry Andric       // If this BB is too big or the block number in straight line between From
1522b60736ecSDimitry Andric       // and To is too big, stop searching to save compiling time.
1523145449b1SDimitry Andric       if (BB->sizeWithoutDebugLargerThan(SinkLoadInstsPerBlockThreshold) ||
1524b60736ecSDimitry Andric           HandledDomBlocks.size() > SinkLoadBlocksThreshold) {
1525b60736ecSDimitry Andric         for (auto *DomBB : HandledDomBlocks) {
1526b60736ecSDimitry Andric           if (DomBB != BB && DT->dominates(DomBB, BB))
1527b60736ecSDimitry Andric             HasStoreCache[std::make_pair(DomBB, To)] = true;
1528b60736ecSDimitry Andric           else if(DomBB != BB && DT->dominates(BB, DomBB))
1529b60736ecSDimitry Andric             HasStoreCache[std::make_pair(From, DomBB)] = true;
1530b60736ecSDimitry Andric         }
1531b60736ecSDimitry Andric         HasStoreCache[BlockPair] = true;
1532b60736ecSDimitry Andric         return true;
1533b60736ecSDimitry Andric       }
1534b60736ecSDimitry Andric 
1535b60736ecSDimitry Andric       for (MachineInstr &I : *BB) {
1536b60736ecSDimitry Andric         // Treat as alias conservatively for a call or an ordered memory
1537b60736ecSDimitry Andric         // operation.
1538b60736ecSDimitry Andric         if (I.isCall() || I.hasOrderedMemoryRef()) {
1539b60736ecSDimitry Andric           for (auto *DomBB : HandledDomBlocks) {
1540b60736ecSDimitry Andric             if (DomBB != BB && DT->dominates(DomBB, BB))
1541b60736ecSDimitry Andric               HasStoreCache[std::make_pair(DomBB, To)] = true;
1542b60736ecSDimitry Andric             else if(DomBB != BB && DT->dominates(BB, DomBB))
1543b60736ecSDimitry Andric               HasStoreCache[std::make_pair(From, DomBB)] = true;
1544b60736ecSDimitry Andric           }
1545b60736ecSDimitry Andric           HasStoreCache[BlockPair] = true;
1546b60736ecSDimitry Andric           return true;
1547b60736ecSDimitry Andric         }
1548b60736ecSDimitry Andric 
1549b60736ecSDimitry Andric         if (I.mayStore()) {
1550b60736ecSDimitry Andric           SawStore = true;
1551b60736ecSDimitry Andric           // We still have chance to sink MI if all stores between are not
1552b60736ecSDimitry Andric           // aliased to MI.
1553b60736ecSDimitry Andric           // Cache all store instructions, so that we don't need to go through
1554b60736ecSDimitry Andric           // all From reachable blocks for next load instruction.
1555b60736ecSDimitry Andric           if (I.mayAlias(AA, MI, false))
1556b60736ecSDimitry Andric             HasAliasedStore = true;
1557b60736ecSDimitry Andric           StoreInstrCache[BlockPair].push_back(&I);
1558b60736ecSDimitry Andric         }
1559b60736ecSDimitry Andric       }
1560b60736ecSDimitry Andric     }
1561b60736ecSDimitry Andric   }
1562b60736ecSDimitry Andric   // If there is no store at all, cache the result.
1563b60736ecSDimitry Andric   if (!SawStore)
1564b60736ecSDimitry Andric     HasStoreCache[BlockPair] = false;
1565b60736ecSDimitry Andric   return HasAliasedStore;
1566b60736ecSDimitry Andric }
1567b60736ecSDimitry Andric 
1568145449b1SDimitry Andric /// Sink instructions into cycles if profitable. This especially tries to
1569145449b1SDimitry Andric /// prevent register spills caused by register pressure if there is little to no
1570145449b1SDimitry Andric /// overhead moving instructions into cycles.
SinkIntoCycle(MachineCycle * Cycle,MachineInstr & I)1571145449b1SDimitry Andric bool MachineSinking::SinkIntoCycle(MachineCycle *Cycle, MachineInstr &I) {
1572145449b1SDimitry Andric   LLVM_DEBUG(dbgs() << "CycleSink: Finding sink block for: " << I);
1573145449b1SDimitry Andric   MachineBasicBlock *Preheader = Cycle->getCyclePreheader();
1574145449b1SDimitry Andric   assert(Preheader && "Cycle sink needs a preheader block");
1575344a3780SDimitry Andric   MachineBasicBlock *SinkBlock = nullptr;
1576344a3780SDimitry Andric   bool CanSink = true;
1577344a3780SDimitry Andric   const MachineOperand &MO = I.getOperand(0);
1578344a3780SDimitry Andric 
1579344a3780SDimitry Andric   for (MachineInstr &MI : MRI->use_instructions(MO.getReg())) {
1580145449b1SDimitry Andric     LLVM_DEBUG(dbgs() << "CycleSink:   Analysing use: " << MI);
1581145449b1SDimitry Andric     if (!Cycle->contains(MI.getParent())) {
1582145449b1SDimitry Andric       LLVM_DEBUG(dbgs() << "CycleSink:   Use not in cycle, can't sink.\n");
1583344a3780SDimitry Andric       CanSink = false;
1584344a3780SDimitry Andric       break;
1585344a3780SDimitry Andric     }
1586344a3780SDimitry Andric 
1587344a3780SDimitry Andric     // FIXME: Come up with a proper cost model that estimates whether sinking
1588145449b1SDimitry Andric     // the instruction (and thus possibly executing it on every cycle
1589344a3780SDimitry Andric     // iteration) is more expensive than a register.
1590344a3780SDimitry Andric     // For now assumes that copies are cheap and thus almost always worth it.
1591344a3780SDimitry Andric     if (!MI.isCopy()) {
1592145449b1SDimitry Andric       LLVM_DEBUG(dbgs() << "CycleSink:   Use is not a copy\n");
1593344a3780SDimitry Andric       CanSink = false;
1594344a3780SDimitry Andric       break;
1595344a3780SDimitry Andric     }
1596344a3780SDimitry Andric     if (!SinkBlock) {
1597344a3780SDimitry Andric       SinkBlock = MI.getParent();
1598145449b1SDimitry Andric       LLVM_DEBUG(dbgs() << "CycleSink:   Setting sink block to: "
1599344a3780SDimitry Andric                         << printMBBReference(*SinkBlock) << "\n");
1600344a3780SDimitry Andric       continue;
1601344a3780SDimitry Andric     }
1602344a3780SDimitry Andric     SinkBlock = DT->findNearestCommonDominator(SinkBlock, MI.getParent());
1603344a3780SDimitry Andric     if (!SinkBlock) {
1604145449b1SDimitry Andric       LLVM_DEBUG(dbgs() << "CycleSink:   Can't find nearest dominator\n");
1605344a3780SDimitry Andric       CanSink = false;
1606344a3780SDimitry Andric       break;
1607344a3780SDimitry Andric     }
1608145449b1SDimitry Andric     LLVM_DEBUG(dbgs() << "CycleSink:   Setting nearest common dom block: " <<
1609344a3780SDimitry Andric                printMBBReference(*SinkBlock) << "\n");
1610344a3780SDimitry Andric   }
1611344a3780SDimitry Andric 
1612344a3780SDimitry Andric   if (!CanSink) {
1613145449b1SDimitry Andric     LLVM_DEBUG(dbgs() << "CycleSink: Can't sink instruction.\n");
1614344a3780SDimitry Andric     return false;
1615344a3780SDimitry Andric   }
1616344a3780SDimitry Andric   if (!SinkBlock) {
1617145449b1SDimitry Andric     LLVM_DEBUG(dbgs() << "CycleSink: Not sinking, can't find sink block.\n");
1618344a3780SDimitry Andric     return false;
1619344a3780SDimitry Andric   }
1620344a3780SDimitry Andric   if (SinkBlock == Preheader) {
1621145449b1SDimitry Andric     LLVM_DEBUG(
1622145449b1SDimitry Andric         dbgs() << "CycleSink: Not sinking, sink block is the preheader\n");
1623344a3780SDimitry Andric     return false;
1624344a3780SDimitry Andric   }
1625145449b1SDimitry Andric   if (SinkBlock->sizeWithoutDebugLargerThan(SinkLoadInstsPerBlockThreshold)) {
1626145449b1SDimitry Andric     LLVM_DEBUG(
1627145449b1SDimitry Andric         dbgs() << "CycleSink: Not Sinking, block too large to analyse.\n");
1628344a3780SDimitry Andric     return false;
1629344a3780SDimitry Andric   }
1630344a3780SDimitry Andric 
1631145449b1SDimitry Andric   LLVM_DEBUG(dbgs() << "CycleSink: Sinking instruction!\n");
1632145449b1SDimitry Andric   SinkBlock->splice(SinkBlock->SkipPHIsAndLabels(SinkBlock->begin()), Preheader,
1633145449b1SDimitry Andric                     I);
1634145449b1SDimitry Andric 
1635145449b1SDimitry Andric   // Conservatively clear any kill flags on uses of sunk instruction
1636145449b1SDimitry Andric   for (MachineOperand &MO : I.operands()) {
1637145449b1SDimitry Andric     if (MO.isReg() && MO.readsReg())
1638145449b1SDimitry Andric       RegsToClearKillFlags.insert(MO.getReg());
1639145449b1SDimitry Andric   }
1640344a3780SDimitry Andric 
1641344a3780SDimitry Andric   // The instruction is moved from its basic block, so do not retain the
1642344a3780SDimitry Andric   // debug information.
1643344a3780SDimitry Andric   assert(!I.isDebugInstr() && "Should not sink debug inst");
1644344a3780SDimitry Andric   I.setDebugLoc(DebugLoc());
1645344a3780SDimitry Andric   return true;
1646344a3780SDimitry Andric }
1647344a3780SDimitry Andric 
164863faed5bSDimitry Andric /// SinkInstruction - Determine whether it is safe to sink the specified machine
164963faed5bSDimitry Andric /// instruction out of its current block into a successor.
SinkInstruction(MachineInstr & MI,bool & SawStore,AllSuccsCache & AllSuccessors)165001095a5dSDimitry Andric bool MachineSinking::SinkInstruction(MachineInstr &MI, bool &SawStore,
16513a0822f0SDimitry Andric                                      AllSuccsCache &AllSuccessors) {
165201095a5dSDimitry Andric   // Don't sink instructions that the target prefers not to sink.
165301095a5dSDimitry Andric   if (!TII->shouldSink(MI))
1654009b1c42SEd Schouten     return false;
165563faed5bSDimitry Andric 
165663faed5bSDimitry Andric   // Check if it's safe to move the instruction.
165701095a5dSDimitry Andric   if (!MI.isSafeToMove(AA, SawStore))
165863faed5bSDimitry Andric     return false;
165963faed5bSDimitry Andric 
1660dd58ef01SDimitry Andric   // Convergent operations may not be made control-dependent on additional
1661dd58ef01SDimitry Andric   // values.
166201095a5dSDimitry Andric   if (MI.isConvergent())
166301095a5dSDimitry Andric     return false;
166401095a5dSDimitry Andric 
166501095a5dSDimitry Andric   // Don't break implicit null checks.  This is a performance heuristic, and not
166601095a5dSDimitry Andric   // required for correctness.
166701095a5dSDimitry Andric   if (SinkingPreventsImplicitNullCheck(MI, TII, TRI))
166885d8b2bbSDimitry Andric     return false;
166985d8b2bbSDimitry Andric 
167063faed5bSDimitry Andric   // FIXME: This should include support for sinking instructions within the
167163faed5bSDimitry Andric   // block they are currently in to shorten the live ranges.  We often get
167263faed5bSDimitry Andric   // instructions sunk into the top of a large block, but it would be better to
167363faed5bSDimitry Andric   // also sink them down before their first use in the block.  This xform has to
167463faed5bSDimitry Andric   // be careful not to *increase* register pressure though, e.g. sinking
167563faed5bSDimitry Andric   // "x = y + z" down if it kills y and z would increase the live ranges of y
167663faed5bSDimitry Andric   // and z and only shrink the live range of x.
167763faed5bSDimitry Andric 
167863faed5bSDimitry Andric   bool BreakPHIEdge = false;
167901095a5dSDimitry Andric   MachineBasicBlock *ParentBlock = MI.getParent();
16803a0822f0SDimitry Andric   MachineBasicBlock *SuccToSinkTo =
16813a0822f0SDimitry Andric       FindSuccToSinkTo(MI, ParentBlock, BreakPHIEdge, AllSuccessors);
1682009b1c42SEd Schouten 
1683009b1c42SEd Schouten   // If there are no outputs, it must have side-effects.
16845ca98fd9SDimitry Andric   if (!SuccToSinkTo)
1685009b1c42SEd Schouten     return false;
1686009b1c42SEd Schouten 
168766e41e3cSRoman Divacky   // If the instruction to move defines a dead physical register which is live
168866e41e3cSRoman Divacky   // when leaving the basic block, don't move it because it could turn into a
1689b1c73532SDimitry Andric   // "zombie" define of that preg. E.g., EFLAGS.
16907fa27ce4SDimitry Andric   for (const MachineOperand &MO : MI.all_defs()) {
16911d5ae102SDimitry Andric     Register Reg = MO.getReg();
1692e3b55780SDimitry Andric     if (Reg == 0 || !Reg.isPhysical())
16931d5ae102SDimitry Andric       continue;
169466e41e3cSRoman Divacky     if (SuccToSinkTo->isLiveIn(Reg))
169566e41e3cSRoman Divacky       return false;
169666e41e3cSRoman Divacky   }
169766e41e3cSRoman Divacky 
1698eb11fae6SDimitry Andric   LLVM_DEBUG(dbgs() << "Sink instr " << MI << "\tinto block " << *SuccToSinkTo);
1699009b1c42SEd Schouten 
1700f8af5cf6SDimitry Andric   // If the block has multiple predecessors, this is a critical edge.
1701f8af5cf6SDimitry Andric   // Decide if we can sink along it or need to break the edge.
1702009b1c42SEd Schouten   if (SuccToSinkTo->pred_size() > 1) {
1703d7f7719eSRoman Divacky     // We cannot sink a load across a critical edge - there may be stores in
1704d7f7719eSRoman Divacky     // other code paths.
1705d39c594dSDimitry Andric     bool TryBreak = false;
1706b60736ecSDimitry Andric     bool Store =
1707b60736ecSDimitry Andric         MI.mayLoad() ? hasStoreBetween(ParentBlock, SuccToSinkTo, MI) : true;
1708b60736ecSDimitry Andric     if (!MI.isSafeToMove(AA, Store)) {
1709eb11fae6SDimitry Andric       LLVM_DEBUG(dbgs() << " *** NOTE: Won't sink load along critical edge.\n");
1710d39c594dSDimitry Andric       TryBreak = true;
1711d7f7719eSRoman Divacky     }
1712d7f7719eSRoman Divacky 
1713d7f7719eSRoman Divacky     // We don't want to sink across a critical edge if we don't dominate the
1714d7f7719eSRoman Divacky     // successor. We could be introducing calculations to new code paths.
1715d39c594dSDimitry Andric     if (!TryBreak && !DT->dominates(ParentBlock, SuccToSinkTo)) {
1716eb11fae6SDimitry Andric       LLVM_DEBUG(dbgs() << " *** NOTE: Critical edge found\n");
1717d39c594dSDimitry Andric       TryBreak = true;
1718009b1c42SEd Schouten     }
1719009b1c42SEd Schouten 
1720145449b1SDimitry Andric     // Don't sink instructions into a cycle.
1721145449b1SDimitry Andric     if (!TryBreak && CI->getCycle(SuccToSinkTo) &&
1722145449b1SDimitry Andric         (!CI->getCycle(SuccToSinkTo)->isReducible() ||
1723145449b1SDimitry Andric          CI->getCycle(SuccToSinkTo)->getHeader() == SuccToSinkTo)) {
1724145449b1SDimitry Andric       LLVM_DEBUG(dbgs() << " *** NOTE: cycle header found\n");
1725d39c594dSDimitry Andric       TryBreak = true;
1726d7f7719eSRoman Divacky     }
1727d7f7719eSRoman Divacky 
1728d7f7719eSRoman Divacky     // Otherwise we are OK with sinking along a critical edge.
1729d39c594dSDimitry Andric     if (!TryBreak)
1730eb11fae6SDimitry Andric       LLVM_DEBUG(dbgs() << "Sinking along critical edge.\n");
1731d39c594dSDimitry Andric     else {
173267c32a98SDimitry Andric       // Mark this edge as to be split.
173367c32a98SDimitry Andric       // If the edge can actually be split, the next iteration of the main loop
173467c32a98SDimitry Andric       // will sink MI in the newly created block.
173567c32a98SDimitry Andric       bool Status =
173667c32a98SDimitry Andric         PostponeSplitCriticalEdge(MI, ParentBlock, SuccToSinkTo, BreakPHIEdge);
173767c32a98SDimitry Andric       if (!Status)
1738eb11fae6SDimitry Andric         LLVM_DEBUG(dbgs() << " *** PUNTING: Not legal or profitable to "
1739cf099d11SDimitry Andric                              "break critical edge\n");
174067c32a98SDimitry Andric       // The instruction will not be sunk this time.
1741d39c594dSDimitry Andric       return false;
1742d39c594dSDimitry Andric     }
1743d7f7719eSRoman Divacky   }
1744d7f7719eSRoman Divacky 
1745cf099d11SDimitry Andric   if (BreakPHIEdge) {
1746cf099d11SDimitry Andric     // BreakPHIEdge is true if all the uses are in the successor MBB being
1747cf099d11SDimitry Andric     // sunken into and they are all PHI nodes. In this case, machine-sink must
1748cf099d11SDimitry Andric     // break the critical edge first.
174967c32a98SDimitry Andric     bool Status = PostponeSplitCriticalEdge(MI, ParentBlock,
1750cf099d11SDimitry Andric                                             SuccToSinkTo, BreakPHIEdge);
175167c32a98SDimitry Andric     if (!Status)
1752eb11fae6SDimitry Andric       LLVM_DEBUG(dbgs() << " *** PUNTING: Not legal or profitable to "
1753cf099d11SDimitry Andric                            "break critical edge\n");
175467c32a98SDimitry Andric     // The instruction will not be sunk this time.
1755cf099d11SDimitry Andric     return false;
1756cf099d11SDimitry Andric   }
1757cf099d11SDimitry Andric 
1758009b1c42SEd Schouten   // Determine where to insert into. Skip phi nodes.
1759145449b1SDimitry Andric   MachineBasicBlock::iterator InsertPos =
1760145449b1SDimitry Andric       SuccToSinkTo->SkipPHIsAndLabels(SuccToSinkTo->begin());
1761145449b1SDimitry Andric   if (blockPrologueInterferes(SuccToSinkTo, InsertPos, MI, TRI, TII, MRI)) {
1762145449b1SDimitry Andric     LLVM_DEBUG(dbgs() << " *** Not sinking: prologue interference\n");
1763145449b1SDimitry Andric     return false;
1764145449b1SDimitry Andric   }
1765009b1c42SEd Schouten 
1766706b4fc4SDimitry Andric   // Collect debug users of any vreg that this inst defines.
1767344a3780SDimitry Andric   SmallVector<MIRegs, 4> DbgUsersToSink;
17687fa27ce4SDimitry Andric   for (auto &MO : MI.all_defs()) {
17697fa27ce4SDimitry Andric     if (!MO.getReg().isVirtual())
1770706b4fc4SDimitry Andric       continue;
1771706b4fc4SDimitry Andric     if (!SeenDbgUsers.count(MO.getReg()))
1772706b4fc4SDimitry Andric       continue;
1773706b4fc4SDimitry Andric 
1774706b4fc4SDimitry Andric     // Sink any users that don't pass any other DBG_VALUEs for this variable.
1775706b4fc4SDimitry Andric     auto &Users = SeenDbgUsers[MO.getReg()];
1776706b4fc4SDimitry Andric     for (auto &User : Users) {
1777706b4fc4SDimitry Andric       MachineInstr *DbgMI = User.getPointer();
1778706b4fc4SDimitry Andric       if (User.getInt()) {
1779706b4fc4SDimitry Andric         // This DBG_VALUE would re-order assignments. If we can't copy-propagate
1780706b4fc4SDimitry Andric         // it, it can't be recovered. Set it undef.
1781344a3780SDimitry Andric         if (!attemptDebugCopyProp(MI, *DbgMI, MO.getReg()))
1782cfca06d7SDimitry Andric           DbgMI->setDebugValueUndef();
1783706b4fc4SDimitry Andric       } else {
1784344a3780SDimitry Andric         DbgUsersToSink.push_back(
1785344a3780SDimitry Andric             {DbgMI, SmallVector<unsigned, 2>(1, MO.getReg())});
1786706b4fc4SDimitry Andric       }
1787706b4fc4SDimitry Andric     }
1788706b4fc4SDimitry Andric   }
1789706b4fc4SDimitry Andric 
1790706b4fc4SDimitry Andric   // After sinking, some debug users may not be dominated any more. If possible,
1791706b4fc4SDimitry Andric   // copy-propagate their operands. As it's expensive, don't do this if there's
1792706b4fc4SDimitry Andric   // no debuginfo in the program.
1793706b4fc4SDimitry Andric   if (MI.getMF()->getFunction().getSubprogram() && MI.isCopy())
1794706b4fc4SDimitry Andric     SalvageUnsunkDebugUsersOfCopy(MI, SuccToSinkTo);
1795706b4fc4SDimitry Andric 
1796706b4fc4SDimitry Andric   performSink(MI, *SuccToSinkTo, InsertPos, DbgUsersToSink);
179730815c53SDimitry Andric 
179866e41e3cSRoman Divacky   // Conservatively, clear any kill flags, since it's possible that they are no
179966e41e3cSRoman Divacky   // longer correct.
18005a5ac124SDimitry Andric   // Note that we have to clear the kill flags for any register this instruction
18015a5ac124SDimitry Andric   // uses as we may sink over another instruction which currently kills the
18025a5ac124SDimitry Andric   // used registers.
18037fa27ce4SDimitry Andric   for (MachineOperand &MO : MI.all_uses())
1804c0981da4SDimitry Andric     RegsToClearKillFlags.insert(MO.getReg()); // Remember to clear kill flags.
1805abdf259dSRoman Divacky 
1806009b1c42SEd Schouten   return true;
1807009b1c42SEd Schouten }
1808eb11fae6SDimitry Andric 
SalvageUnsunkDebugUsersOfCopy(MachineInstr & MI,MachineBasicBlock * TargetBlock)1809706b4fc4SDimitry Andric void MachineSinking::SalvageUnsunkDebugUsersOfCopy(
1810706b4fc4SDimitry Andric     MachineInstr &MI, MachineBasicBlock *TargetBlock) {
1811706b4fc4SDimitry Andric   assert(MI.isCopy());
1812706b4fc4SDimitry Andric   assert(MI.getOperand(1).isReg());
1813706b4fc4SDimitry Andric 
1814706b4fc4SDimitry Andric   // Enumerate all users of vreg operands that are def'd. Skip those that will
1815706b4fc4SDimitry Andric   // be sunk. For the rest, if they are not dominated by the block we will sink
1816706b4fc4SDimitry Andric   // MI into, propagate the copy source to them.
1817706b4fc4SDimitry Andric   SmallVector<MachineInstr *, 4> DbgDefUsers;
1818344a3780SDimitry Andric   SmallVector<Register, 4> DbgUseRegs;
1819706b4fc4SDimitry Andric   const MachineRegisterInfo &MRI = MI.getMF()->getRegInfo();
18207fa27ce4SDimitry Andric   for (auto &MO : MI.all_defs()) {
18217fa27ce4SDimitry Andric     if (!MO.getReg().isVirtual())
1822706b4fc4SDimitry Andric       continue;
1823344a3780SDimitry Andric     DbgUseRegs.push_back(MO.getReg());
1824706b4fc4SDimitry Andric     for (auto &User : MRI.use_instructions(MO.getReg())) {
1825706b4fc4SDimitry Andric       if (!User.isDebugValue() || DT->dominates(TargetBlock, User.getParent()))
1826706b4fc4SDimitry Andric         continue;
1827706b4fc4SDimitry Andric 
1828706b4fc4SDimitry Andric       // If is in same block, will either sink or be use-before-def.
1829706b4fc4SDimitry Andric       if (User.getParent() == MI.getParent())
1830706b4fc4SDimitry Andric         continue;
1831706b4fc4SDimitry Andric 
1832344a3780SDimitry Andric       assert(User.hasDebugOperandForReg(MO.getReg()) &&
1833344a3780SDimitry Andric              "DBG_VALUE user of vreg, but has no operand for it?");
1834706b4fc4SDimitry Andric       DbgDefUsers.push_back(&User);
1835706b4fc4SDimitry Andric     }
1836706b4fc4SDimitry Andric   }
1837706b4fc4SDimitry Andric 
1838706b4fc4SDimitry Andric   // Point the users of this copy that are no longer dominated, at the source
1839706b4fc4SDimitry Andric   // of the copy.
1840706b4fc4SDimitry Andric   for (auto *User : DbgDefUsers) {
1841344a3780SDimitry Andric     for (auto &Reg : DbgUseRegs) {
1842344a3780SDimitry Andric       for (auto &DbgOp : User->getDebugOperandsForReg(Reg)) {
1843344a3780SDimitry Andric         DbgOp.setReg(MI.getOperand(1).getReg());
1844344a3780SDimitry Andric         DbgOp.setSubReg(MI.getOperand(1).getSubReg());
1845344a3780SDimitry Andric       }
1846344a3780SDimitry Andric     }
1847706b4fc4SDimitry Andric   }
1848706b4fc4SDimitry Andric }
1849706b4fc4SDimitry Andric 
1850eb11fae6SDimitry Andric //===----------------------------------------------------------------------===//
1851eb11fae6SDimitry Andric // This pass is not intended to be a replacement or a complete alternative
1852eb11fae6SDimitry Andric // for the pre-ra machine sink pass. It is only designed to sink COPY
1853eb11fae6SDimitry Andric // instructions which should be handled after RA.
1854eb11fae6SDimitry Andric //
1855eb11fae6SDimitry Andric // This pass sinks COPY instructions into a successor block, if the COPY is not
1856eb11fae6SDimitry Andric // used in the current block and the COPY is live-in to a single successor
1857eb11fae6SDimitry Andric // (i.e., doesn't require the COPY to be duplicated).  This avoids executing the
1858eb11fae6SDimitry Andric // copy on paths where their results aren't needed.  This also exposes
1859eb11fae6SDimitry Andric // additional opportunites for dead copy elimination and shrink wrapping.
1860eb11fae6SDimitry Andric //
1861eb11fae6SDimitry Andric // These copies were either not handled by or are inserted after the MachineSink
1862eb11fae6SDimitry Andric // pass. As an example of the former case, the MachineSink pass cannot sink
1863eb11fae6SDimitry Andric // COPY instructions with allocatable source registers; for AArch64 these type
1864eb11fae6SDimitry Andric // of copy instructions are frequently used to move function parameters (PhyReg)
1865eb11fae6SDimitry Andric // into virtual registers in the entry block.
1866eb11fae6SDimitry Andric //
1867eb11fae6SDimitry Andric // For the machine IR below, this pass will sink %w19 in the entry into its
1868eb11fae6SDimitry Andric // successor (%bb.1) because %w19 is only live-in in %bb.1.
1869eb11fae6SDimitry Andric // %bb.0:
1870eb11fae6SDimitry Andric //   %wzr = SUBSWri %w1, 1
1871eb11fae6SDimitry Andric //   %w19 = COPY %w0
1872eb11fae6SDimitry Andric //   Bcc 11, %bb.2
1873eb11fae6SDimitry Andric // %bb.1:
1874eb11fae6SDimitry Andric //   Live Ins: %w19
1875eb11fae6SDimitry Andric //   BL @fun
1876eb11fae6SDimitry Andric //   %w0 = ADDWrr %w0, %w19
1877eb11fae6SDimitry Andric //   RET %w0
1878eb11fae6SDimitry Andric // %bb.2:
1879eb11fae6SDimitry Andric //   %w0 = COPY %wzr
1880eb11fae6SDimitry Andric //   RET %w0
1881eb11fae6SDimitry Andric // As we sink %w19 (CSR in AArch64) into %bb.1, the shrink-wrapping pass will be
1882eb11fae6SDimitry Andric // able to see %bb.0 as a candidate.
1883eb11fae6SDimitry Andric //===----------------------------------------------------------------------===//
1884eb11fae6SDimitry Andric namespace {
1885eb11fae6SDimitry Andric 
1886eb11fae6SDimitry Andric class PostRAMachineSinking : public MachineFunctionPass {
1887eb11fae6SDimitry Andric public:
1888eb11fae6SDimitry Andric   bool runOnMachineFunction(MachineFunction &MF) override;
1889eb11fae6SDimitry Andric 
1890eb11fae6SDimitry Andric   static char ID;
PostRAMachineSinking()1891eb11fae6SDimitry Andric   PostRAMachineSinking() : MachineFunctionPass(ID) {}
getPassName() const1892eb11fae6SDimitry Andric   StringRef getPassName() const override { return "PostRA Machine Sink"; }
1893eb11fae6SDimitry Andric 
getAnalysisUsage(AnalysisUsage & AU) const1894eb11fae6SDimitry Andric   void getAnalysisUsage(AnalysisUsage &AU) const override {
1895eb11fae6SDimitry Andric     AU.setPreservesCFG();
1896eb11fae6SDimitry Andric     MachineFunctionPass::getAnalysisUsage(AU);
1897eb11fae6SDimitry Andric   }
1898eb11fae6SDimitry Andric 
getRequiredProperties() const1899eb11fae6SDimitry Andric   MachineFunctionProperties getRequiredProperties() const override {
1900eb11fae6SDimitry Andric     return MachineFunctionProperties().set(
1901eb11fae6SDimitry Andric         MachineFunctionProperties::Property::NoVRegs);
1902eb11fae6SDimitry Andric   }
1903eb11fae6SDimitry Andric 
1904eb11fae6SDimitry Andric private:
1905eb11fae6SDimitry Andric   /// Track which register units have been modified and used.
1906eb11fae6SDimitry Andric   LiveRegUnits ModifiedRegUnits, UsedRegUnits;
1907eb11fae6SDimitry Andric 
19081d5ae102SDimitry Andric   /// Track DBG_VALUEs of (unmodified) register units. Each DBG_VALUE has an
1909344a3780SDimitry Andric   /// entry in this map for each unit it touches. The DBG_VALUE's entry
1910344a3780SDimitry Andric   /// consists of a pointer to the instruction itself, and a vector of registers
1911344a3780SDimitry Andric   /// referred to by the instruction that overlap the key register unit.
1912344a3780SDimitry Andric   DenseMap<unsigned, SmallVector<MIRegs, 2>> SeenDbgInstrs;
1913d8e91e46SDimitry Andric 
1914eb11fae6SDimitry Andric   /// Sink Copy instructions unused in the same block close to their uses in
1915eb11fae6SDimitry Andric   /// successors.
1916eb11fae6SDimitry Andric   bool tryToSinkCopy(MachineBasicBlock &BB, MachineFunction &MF,
1917eb11fae6SDimitry Andric                      const TargetRegisterInfo *TRI, const TargetInstrInfo *TII);
1918eb11fae6SDimitry Andric };
1919eb11fae6SDimitry Andric } // namespace
1920eb11fae6SDimitry Andric 
1921eb11fae6SDimitry Andric char PostRAMachineSinking::ID = 0;
1922eb11fae6SDimitry Andric char &llvm::PostRAMachineSinkingID = PostRAMachineSinking::ID;
1923eb11fae6SDimitry Andric 
1924eb11fae6SDimitry Andric INITIALIZE_PASS(PostRAMachineSinking, "postra-machine-sink",
1925eb11fae6SDimitry Andric                 "PostRA Machine Sink", false, false)
1926eb11fae6SDimitry Andric 
aliasWithRegsInLiveIn(MachineBasicBlock & MBB,unsigned Reg,const TargetRegisterInfo * TRI)1927eb11fae6SDimitry Andric static bool aliasWithRegsInLiveIn(MachineBasicBlock &MBB, unsigned Reg,
1928eb11fae6SDimitry Andric                                   const TargetRegisterInfo *TRI) {
1929eb11fae6SDimitry Andric   LiveRegUnits LiveInRegUnits(*TRI);
1930eb11fae6SDimitry Andric   LiveInRegUnits.addLiveIns(MBB);
1931eb11fae6SDimitry Andric   return !LiveInRegUnits.available(Reg);
1932eb11fae6SDimitry Andric }
1933eb11fae6SDimitry Andric 
1934eb11fae6SDimitry Andric static MachineBasicBlock *
getSingleLiveInSuccBB(MachineBasicBlock & CurBB,const SmallPtrSetImpl<MachineBasicBlock * > & SinkableBBs,unsigned Reg,const TargetRegisterInfo * TRI)1935eb11fae6SDimitry Andric getSingleLiveInSuccBB(MachineBasicBlock &CurBB,
1936eb11fae6SDimitry Andric                       const SmallPtrSetImpl<MachineBasicBlock *> &SinkableBBs,
1937eb11fae6SDimitry Andric                       unsigned Reg, const TargetRegisterInfo *TRI) {
1938eb11fae6SDimitry Andric   // Try to find a single sinkable successor in which Reg is live-in.
1939eb11fae6SDimitry Andric   MachineBasicBlock *BB = nullptr;
1940eb11fae6SDimitry Andric   for (auto *SI : SinkableBBs) {
1941eb11fae6SDimitry Andric     if (aliasWithRegsInLiveIn(*SI, Reg, TRI)) {
1942eb11fae6SDimitry Andric       // If BB is set here, Reg is live-in to at least two sinkable successors,
1943eb11fae6SDimitry Andric       // so quit.
1944eb11fae6SDimitry Andric       if (BB)
1945eb11fae6SDimitry Andric         return nullptr;
1946eb11fae6SDimitry Andric       BB = SI;
1947eb11fae6SDimitry Andric     }
1948eb11fae6SDimitry Andric   }
1949eb11fae6SDimitry Andric   // Reg is not live-in to any sinkable successors.
1950eb11fae6SDimitry Andric   if (!BB)
1951eb11fae6SDimitry Andric     return nullptr;
1952eb11fae6SDimitry Andric 
1953eb11fae6SDimitry Andric   // Check if any register aliased with Reg is live-in in other successors.
1954eb11fae6SDimitry Andric   for (auto *SI : CurBB.successors()) {
1955eb11fae6SDimitry Andric     if (!SinkableBBs.count(SI) && aliasWithRegsInLiveIn(*SI, Reg, TRI))
1956eb11fae6SDimitry Andric       return nullptr;
1957eb11fae6SDimitry Andric   }
1958eb11fae6SDimitry Andric   return BB;
1959eb11fae6SDimitry Andric }
1960eb11fae6SDimitry Andric 
1961eb11fae6SDimitry Andric static MachineBasicBlock *
getSingleLiveInSuccBB(MachineBasicBlock & CurBB,const SmallPtrSetImpl<MachineBasicBlock * > & SinkableBBs,ArrayRef<unsigned> DefedRegsInCopy,const TargetRegisterInfo * TRI)1962eb11fae6SDimitry Andric getSingleLiveInSuccBB(MachineBasicBlock &CurBB,
1963eb11fae6SDimitry Andric                       const SmallPtrSetImpl<MachineBasicBlock *> &SinkableBBs,
1964eb11fae6SDimitry Andric                       ArrayRef<unsigned> DefedRegsInCopy,
1965eb11fae6SDimitry Andric                       const TargetRegisterInfo *TRI) {
1966eb11fae6SDimitry Andric   MachineBasicBlock *SingleBB = nullptr;
1967eb11fae6SDimitry Andric   for (auto DefReg : DefedRegsInCopy) {
1968eb11fae6SDimitry Andric     MachineBasicBlock *BB =
1969eb11fae6SDimitry Andric         getSingleLiveInSuccBB(CurBB, SinkableBBs, DefReg, TRI);
1970eb11fae6SDimitry Andric     if (!BB || (SingleBB && SingleBB != BB))
1971eb11fae6SDimitry Andric       return nullptr;
1972eb11fae6SDimitry Andric     SingleBB = BB;
1973eb11fae6SDimitry Andric   }
1974eb11fae6SDimitry Andric   return SingleBB;
1975eb11fae6SDimitry Andric }
1976eb11fae6SDimitry Andric 
clearKillFlags(MachineInstr * MI,MachineBasicBlock & CurBB,SmallVectorImpl<unsigned> & UsedOpsInCopy,LiveRegUnits & UsedRegUnits,const TargetRegisterInfo * TRI)1977eb11fae6SDimitry Andric static void clearKillFlags(MachineInstr *MI, MachineBasicBlock &CurBB,
1978eb11fae6SDimitry Andric                            SmallVectorImpl<unsigned> &UsedOpsInCopy,
1979eb11fae6SDimitry Andric                            LiveRegUnits &UsedRegUnits,
1980eb11fae6SDimitry Andric                            const TargetRegisterInfo *TRI) {
1981eb11fae6SDimitry Andric   for (auto U : UsedOpsInCopy) {
1982eb11fae6SDimitry Andric     MachineOperand &MO = MI->getOperand(U);
19831d5ae102SDimitry Andric     Register SrcReg = MO.getReg();
1984eb11fae6SDimitry Andric     if (!UsedRegUnits.available(SrcReg)) {
1985eb11fae6SDimitry Andric       MachineBasicBlock::iterator NI = std::next(MI->getIterator());
1986eb11fae6SDimitry Andric       for (MachineInstr &UI : make_range(NI, CurBB.end())) {
1987eb11fae6SDimitry Andric         if (UI.killsRegister(SrcReg, TRI)) {
1988eb11fae6SDimitry Andric           UI.clearRegisterKills(SrcReg, TRI);
1989eb11fae6SDimitry Andric           MO.setIsKill(true);
1990eb11fae6SDimitry Andric           break;
1991eb11fae6SDimitry Andric         }
1992eb11fae6SDimitry Andric       }
1993eb11fae6SDimitry Andric     }
1994eb11fae6SDimitry Andric   }
1995eb11fae6SDimitry Andric }
1996eb11fae6SDimitry Andric 
updateLiveIn(MachineInstr * MI,MachineBasicBlock * SuccBB,SmallVectorImpl<unsigned> & UsedOpsInCopy,SmallVectorImpl<unsigned> & DefedRegsInCopy)1997eb11fae6SDimitry Andric static void updateLiveIn(MachineInstr *MI, MachineBasicBlock *SuccBB,
1998eb11fae6SDimitry Andric                          SmallVectorImpl<unsigned> &UsedOpsInCopy,
1999eb11fae6SDimitry Andric                          SmallVectorImpl<unsigned> &DefedRegsInCopy) {
2000d8e91e46SDimitry Andric   MachineFunction &MF = *SuccBB->getParent();
2001d8e91e46SDimitry Andric   const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
2002d8e91e46SDimitry Andric   for (unsigned DefReg : DefedRegsInCopy)
20037fa27ce4SDimitry Andric     for (MCPhysReg S : TRI->subregs_inclusive(DefReg))
20047fa27ce4SDimitry Andric       SuccBB->removeLiveIn(S);
2005ac9a064cSDimitry Andric   for (auto U : UsedOpsInCopy)
2006ac9a064cSDimitry Andric     SuccBB->addLiveIn(MI->getOperand(U).getReg());
2007706b4fc4SDimitry Andric   SuccBB->sortUniqueLiveIns();
2008eb11fae6SDimitry Andric }
2009eb11fae6SDimitry Andric 
hasRegisterDependency(MachineInstr * MI,SmallVectorImpl<unsigned> & UsedOpsInCopy,SmallVectorImpl<unsigned> & DefedRegsInCopy,LiveRegUnits & ModifiedRegUnits,LiveRegUnits & UsedRegUnits)2010eb11fae6SDimitry Andric static bool hasRegisterDependency(MachineInstr *MI,
2011eb11fae6SDimitry Andric                                   SmallVectorImpl<unsigned> &UsedOpsInCopy,
2012eb11fae6SDimitry Andric                                   SmallVectorImpl<unsigned> &DefedRegsInCopy,
2013eb11fae6SDimitry Andric                                   LiveRegUnits &ModifiedRegUnits,
2014eb11fae6SDimitry Andric                                   LiveRegUnits &UsedRegUnits) {
2015eb11fae6SDimitry Andric   bool HasRegDependency = false;
2016eb11fae6SDimitry Andric   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
2017eb11fae6SDimitry Andric     MachineOperand &MO = MI->getOperand(i);
2018eb11fae6SDimitry Andric     if (!MO.isReg())
2019eb11fae6SDimitry Andric       continue;
20201d5ae102SDimitry Andric     Register Reg = MO.getReg();
2021eb11fae6SDimitry Andric     if (!Reg)
2022eb11fae6SDimitry Andric       continue;
2023eb11fae6SDimitry Andric     if (MO.isDef()) {
2024eb11fae6SDimitry Andric       if (!ModifiedRegUnits.available(Reg) || !UsedRegUnits.available(Reg)) {
2025eb11fae6SDimitry Andric         HasRegDependency = true;
2026eb11fae6SDimitry Andric         break;
2027eb11fae6SDimitry Andric       }
2028eb11fae6SDimitry Andric       DefedRegsInCopy.push_back(Reg);
2029eb11fae6SDimitry Andric 
2030eb11fae6SDimitry Andric       // FIXME: instead of isUse(), readsReg() would be a better fix here,
2031eb11fae6SDimitry Andric       // For example, we can ignore modifications in reg with undef. However,
2032eb11fae6SDimitry Andric       // it's not perfectly clear if skipping the internal read is safe in all
2033eb11fae6SDimitry Andric       // other targets.
2034eb11fae6SDimitry Andric     } else if (MO.isUse()) {
2035eb11fae6SDimitry Andric       if (!ModifiedRegUnits.available(Reg)) {
2036eb11fae6SDimitry Andric         HasRegDependency = true;
2037eb11fae6SDimitry Andric         break;
2038eb11fae6SDimitry Andric       }
2039eb11fae6SDimitry Andric       UsedOpsInCopy.push_back(i);
2040eb11fae6SDimitry Andric     }
2041eb11fae6SDimitry Andric   }
2042eb11fae6SDimitry Andric   return HasRegDependency;
2043eb11fae6SDimitry Andric }
2044eb11fae6SDimitry Andric 
tryToSinkCopy(MachineBasicBlock & CurBB,MachineFunction & MF,const TargetRegisterInfo * TRI,const TargetInstrInfo * TII)2045eb11fae6SDimitry Andric bool PostRAMachineSinking::tryToSinkCopy(MachineBasicBlock &CurBB,
2046eb11fae6SDimitry Andric                                          MachineFunction &MF,
2047eb11fae6SDimitry Andric                                          const TargetRegisterInfo *TRI,
2048eb11fae6SDimitry Andric                                          const TargetInstrInfo *TII) {
2049eb11fae6SDimitry Andric   SmallPtrSet<MachineBasicBlock *, 2> SinkableBBs;
2050eb11fae6SDimitry Andric   // FIXME: For now, we sink only to a successor which has a single predecessor
2051eb11fae6SDimitry Andric   // so that we can directly sink COPY instructions to the successor without
2052eb11fae6SDimitry Andric   // adding any new block or branch instruction.
2053eb11fae6SDimitry Andric   for (MachineBasicBlock *SI : CurBB.successors())
2054eb11fae6SDimitry Andric     if (!SI->livein_empty() && SI->pred_size() == 1)
2055eb11fae6SDimitry Andric       SinkableBBs.insert(SI);
2056eb11fae6SDimitry Andric 
2057eb11fae6SDimitry Andric   if (SinkableBBs.empty())
2058eb11fae6SDimitry Andric     return false;
2059eb11fae6SDimitry Andric 
2060eb11fae6SDimitry Andric   bool Changed = false;
2061eb11fae6SDimitry Andric 
2062eb11fae6SDimitry Andric   // Track which registers have been modified and used between the end of the
2063eb11fae6SDimitry Andric   // block and the current instruction.
2064eb11fae6SDimitry Andric   ModifiedRegUnits.clear();
2065eb11fae6SDimitry Andric   UsedRegUnits.clear();
2066d8e91e46SDimitry Andric   SeenDbgInstrs.clear();
2067eb11fae6SDimitry Andric 
2068c0981da4SDimitry Andric   for (MachineInstr &MI : llvm::make_early_inc_range(llvm::reverse(CurBB))) {
2069d8e91e46SDimitry Andric     // Track the operand index for use in Copy.
2070d8e91e46SDimitry Andric     SmallVector<unsigned, 2> UsedOpsInCopy;
2071d8e91e46SDimitry Andric     // Track the register number defed in Copy.
2072d8e91e46SDimitry Andric     SmallVector<unsigned, 2> DefedRegsInCopy;
2073d8e91e46SDimitry Andric 
2074d8e91e46SDimitry Andric     // We must sink this DBG_VALUE if its operand is sunk. To avoid searching
2075d8e91e46SDimitry Andric     // for DBG_VALUEs later, record them when they're encountered.
2076e3b55780SDimitry Andric     if (MI.isDebugValue() && !MI.isDebugRef()) {
2077344a3780SDimitry Andric       SmallDenseMap<MCRegister, SmallVector<unsigned, 2>, 4> MIUnits;
2078344a3780SDimitry Andric       bool IsValid = true;
2079c0981da4SDimitry Andric       for (MachineOperand &MO : MI.debug_operands()) {
2080e3b55780SDimitry Andric         if (MO.isReg() && MO.getReg().isPhysical()) {
2081d8e91e46SDimitry Andric           // Bail if we can already tell the sink would be rejected, rather
2082d8e91e46SDimitry Andric           // than needlessly accumulating lots of DBG_VALUEs.
2083c0981da4SDimitry Andric           if (hasRegisterDependency(&MI, UsedOpsInCopy, DefedRegsInCopy,
2084344a3780SDimitry Andric                                     ModifiedRegUnits, UsedRegUnits)) {
2085344a3780SDimitry Andric             IsValid = false;
2086344a3780SDimitry Andric             break;
2087344a3780SDimitry Andric           }
2088d8e91e46SDimitry Andric 
20891d5ae102SDimitry Andric           // Record debug use of each reg unit.
20907fa27ce4SDimitry Andric           for (MCRegUnit Unit : TRI->regunits(MO.getReg()))
20917fa27ce4SDimitry Andric             MIUnits[Unit].push_back(MO.getReg());
2092344a3780SDimitry Andric         }
2093344a3780SDimitry Andric       }
2094344a3780SDimitry Andric       if (IsValid) {
2095145449b1SDimitry Andric         for (auto &RegOps : MIUnits)
2096145449b1SDimitry Andric           SeenDbgInstrs[RegOps.first].emplace_back(&MI,
2097145449b1SDimitry Andric                                                    std::move(RegOps.second));
2098d8e91e46SDimitry Andric       }
2099d8e91e46SDimitry Andric       continue;
2100d8e91e46SDimitry Andric     }
2101d8e91e46SDimitry Andric 
2102c0981da4SDimitry Andric     if (MI.isDebugOrPseudoInstr())
2103eb11fae6SDimitry Andric       continue;
2104eb11fae6SDimitry Andric 
2105eb11fae6SDimitry Andric     // Do not move any instruction across function call.
2106c0981da4SDimitry Andric     if (MI.isCall())
2107eb11fae6SDimitry Andric       return false;
2108eb11fae6SDimitry Andric 
2109c0981da4SDimitry Andric     if (!MI.isCopy() || !MI.getOperand(0).isRenamable()) {
2110c0981da4SDimitry Andric       LiveRegUnits::accumulateUsedDefed(MI, ModifiedRegUnits, UsedRegUnits,
2111eb11fae6SDimitry Andric                                         TRI);
2112eb11fae6SDimitry Andric       continue;
2113eb11fae6SDimitry Andric     }
2114eb11fae6SDimitry Andric 
2115eb11fae6SDimitry Andric     // Don't sink the COPY if it would violate a register dependency.
2116c0981da4SDimitry Andric     if (hasRegisterDependency(&MI, UsedOpsInCopy, DefedRegsInCopy,
2117eb11fae6SDimitry Andric                               ModifiedRegUnits, UsedRegUnits)) {
2118c0981da4SDimitry Andric       LiveRegUnits::accumulateUsedDefed(MI, ModifiedRegUnits, UsedRegUnits,
2119eb11fae6SDimitry Andric                                         TRI);
2120eb11fae6SDimitry Andric       continue;
2121eb11fae6SDimitry Andric     }
2122eb11fae6SDimitry Andric     assert((!UsedOpsInCopy.empty() && !DefedRegsInCopy.empty()) &&
2123eb11fae6SDimitry Andric            "Unexpect SrcReg or DefReg");
2124eb11fae6SDimitry Andric     MachineBasicBlock *SuccBB =
2125eb11fae6SDimitry Andric         getSingleLiveInSuccBB(CurBB, SinkableBBs, DefedRegsInCopy, TRI);
2126eb11fae6SDimitry Andric     // Don't sink if we cannot find a single sinkable successor in which Reg
2127eb11fae6SDimitry Andric     // is live-in.
2128eb11fae6SDimitry Andric     if (!SuccBB) {
2129c0981da4SDimitry Andric       LiveRegUnits::accumulateUsedDefed(MI, ModifiedRegUnits, UsedRegUnits,
2130eb11fae6SDimitry Andric                                         TRI);
2131eb11fae6SDimitry Andric       continue;
2132eb11fae6SDimitry Andric     }
2133eb11fae6SDimitry Andric     assert((SuccBB->pred_size() == 1 && *SuccBB->pred_begin() == &CurBB) &&
2134eb11fae6SDimitry Andric            "Unexpected predecessor");
2135eb11fae6SDimitry Andric 
21361d5ae102SDimitry Andric     // Collect DBG_VALUEs that must sink with this copy. We've previously
21371d5ae102SDimitry Andric     // recorded which reg units that DBG_VALUEs read, if this instruction
21381d5ae102SDimitry Andric     // writes any of those units then the corresponding DBG_VALUEs must sink.
2139344a3780SDimitry Andric     MapVector<MachineInstr *, MIRegs::second_type> DbgValsToSinkMap;
21407fa27ce4SDimitry Andric     for (auto &MO : MI.all_defs()) {
21417fa27ce4SDimitry Andric       for (MCRegUnit Unit : TRI->regunits(MO.getReg())) {
21427fa27ce4SDimitry Andric         for (const auto &MIRegs : SeenDbgInstrs.lookup(Unit)) {
2143344a3780SDimitry Andric           auto &Regs = DbgValsToSinkMap[MIRegs.first];
2144344a3780SDimitry Andric           for (unsigned Reg : MIRegs.second)
2145344a3780SDimitry Andric             Regs.push_back(Reg);
2146d8e91e46SDimitry Andric         }
2147344a3780SDimitry Andric       }
2148344a3780SDimitry Andric     }
2149145449b1SDimitry Andric     auto DbgValsToSink = DbgValsToSinkMap.takeVector();
2150145449b1SDimitry Andric 
2151145449b1SDimitry Andric     LLVM_DEBUG(dbgs() << "Sink instr " << MI << "\tinto block " << *SuccBB);
2152145449b1SDimitry Andric 
2153145449b1SDimitry Andric     MachineBasicBlock::iterator InsertPos =
2154145449b1SDimitry Andric         SuccBB->SkipPHIsAndLabels(SuccBB->begin());
2155145449b1SDimitry Andric     if (blockPrologueInterferes(SuccBB, InsertPos, MI, TRI, TII, nullptr)) {
2156145449b1SDimitry Andric       LLVM_DEBUG(
2157145449b1SDimitry Andric           dbgs() << " *** Not sinking: prologue interference\n");
2158145449b1SDimitry Andric       continue;
2159145449b1SDimitry Andric     }
2160d8e91e46SDimitry Andric 
2161eb11fae6SDimitry Andric     // Clear the kill flag if SrcReg is killed between MI and the end of the
2162eb11fae6SDimitry Andric     // block.
2163c0981da4SDimitry Andric     clearKillFlags(&MI, CurBB, UsedOpsInCopy, UsedRegUnits, TRI);
2164c0981da4SDimitry Andric     performSink(MI, *SuccBB, InsertPos, DbgValsToSink);
2165c0981da4SDimitry Andric     updateLiveIn(&MI, SuccBB, UsedOpsInCopy, DefedRegsInCopy);
2166eb11fae6SDimitry Andric 
2167eb11fae6SDimitry Andric     Changed = true;
2168eb11fae6SDimitry Andric     ++NumPostRACopySink;
2169eb11fae6SDimitry Andric   }
2170eb11fae6SDimitry Andric   return Changed;
2171eb11fae6SDimitry Andric }
2172eb11fae6SDimitry Andric 
runOnMachineFunction(MachineFunction & MF)2173eb11fae6SDimitry Andric bool PostRAMachineSinking::runOnMachineFunction(MachineFunction &MF) {
2174e6d15924SDimitry Andric   if (skipFunction(MF.getFunction()))
2175e6d15924SDimitry Andric     return false;
2176e6d15924SDimitry Andric 
2177eb11fae6SDimitry Andric   bool Changed = false;
2178eb11fae6SDimitry Andric   const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
2179eb11fae6SDimitry Andric   const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
2180eb11fae6SDimitry Andric 
2181eb11fae6SDimitry Andric   ModifiedRegUnits.init(*TRI);
2182eb11fae6SDimitry Andric   UsedRegUnits.init(*TRI);
2183eb11fae6SDimitry Andric   for (auto &BB : MF)
2184eb11fae6SDimitry Andric     Changed |= tryToSinkCopy(BB, MF, TRI, TII);
2185eb11fae6SDimitry Andric 
2186eb11fae6SDimitry Andric   return Changed;
2187eb11fae6SDimitry Andric }
2188